2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 86c3caaf 2018-03-09 stsp * Permission to use, copy, modify, and distribute this software for any
5 86c3caaf 2018-03-09 stsp * purpose with or without fee is hereby granted, provided that the above
6 86c3caaf 2018-03-09 stsp * copyright notice and this permission notice appear in all copies.
8 86c3caaf 2018-03-09 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 86c3caaf 2018-03-09 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 86c3caaf 2018-03-09 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 86c3caaf 2018-03-09 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 86c3caaf 2018-03-09 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 86c3caaf 2018-03-09 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 86c3caaf 2018-03-09 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 86c3caaf 2018-03-09 stsp #include <sys/stat.h>
18 9d31a1d8 2018-03-11 stsp #include <sys/queue.h>
19 133d2798 2019-01-08 stsp #include <sys/tree.h>
21 d1f6d47b 2019-02-04 stsp #include <dirent.h>
22 12ce7a6c 2019-08-12 stsp #include <limits.h>
23 f5c49f82 2019-01-06 stsp #include <stddef.h>
24 86c3caaf 2018-03-09 stsp #include <string.h>
25 86c3caaf 2018-03-09 stsp #include <stdio.h>
26 86c3caaf 2018-03-09 stsp #include <stdlib.h>
27 303e14b5 2019-09-23 stsp #include <time.h>
28 86c3caaf 2018-03-09 stsp #include <fcntl.h>
29 86c3caaf 2018-03-09 stsp #include <errno.h>
30 86c3caaf 2018-03-09 stsp #include <unistd.h>
31 9d31a1d8 2018-03-11 stsp #include <sha1.h>
32 9d31a1d8 2018-03-11 stsp #include <zlib.h>
33 9d31a1d8 2018-03-11 stsp #include <fnmatch.h>
34 512f0d0e 2019-01-02 stsp #include <libgen.h>
35 ec22038e 2019-03-10 stsp #include <uuid.h>
36 7154f6ce 2019-03-27 stsp #include <util.h>
38 86c3caaf 2018-03-09 stsp #include "got_error.h"
39 86c3caaf 2018-03-09 stsp #include "got_repository.h"
40 5261c201 2018-04-01 stsp #include "got_reference.h"
41 9d31a1d8 2018-03-11 stsp #include "got_object.h"
42 1dd54920 2019-05-11 stsp #include "got_path.h"
43 e6209546 2019-08-22 stsp #include "got_cancel.h"
44 86c3caaf 2018-03-09 stsp #include "got_worktree.h"
45 511a516b 2018-05-19 stsp #include "got_opentemp.h"
46 234035bc 2019-06-01 stsp #include "got_diff.h"
48 718b3ab0 2018-03-17 stsp #include "got_lib_worktree.h"
49 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
50 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
51 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
52 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
53 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
54 ed175427 2019-05-09 stsp #include "got_lib_object_parse.h"
55 cf066bf8 2019-05-09 stsp #include "got_lib_object_create.h"
56 24519714 2019-05-09 stsp #include "got_lib_object_idset.h"
57 6353ad76 2019-02-08 stsp #include "got_lib_diff.h"
60 9d31a1d8 2018-03-11 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
63 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_MERGED "merged change"
64 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_BASE "3-way merge base"
66 99724ed4 2018-03-10 stsp static const struct got_error *
67 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
69 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
72 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1)
73 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
75 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, content);
80 507dc3bb 2018-12-29 stsp static const struct got_error *
81 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
83 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
84 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
85 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
86 507dc3bb 2018-12-29 stsp char *path = NULL;
88 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
89 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
90 507dc3bb 2018-12-29 stsp path = NULL;
94 507dc3bb 2018-12-29 stsp err = got_opentemp_named(&tmppath, &tmpfile, path);
98 507dc3bb 2018-12-29 stsp if (content) {
99 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
100 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
101 638f9024 2019-05-13 stsp err = got_error_from_errno2("fprintf", tmppath);
106 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
107 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
108 2a57020b 2019-02-20 stsp unlink(tmppath);
113 fb43ecf1 2019-02-11 stsp if (fclose(tmpfile) != 0 && err == NULL)
114 638f9024 2019-05-13 stsp err = got_error_from_errno2("fclose", tmppath);
115 230a42bd 2019-05-11 jcs free(tmppath);
116 99724ed4 2018-03-10 stsp return err;
119 09fe317a 2018-03-11 stsp static const struct got_error *
120 7ac97322 2018-03-11 stsp read_meta_file(char **content, const char *path_got, const char *name)
122 09fe317a 2018-03-11 stsp const struct got_error *err = NULL;
123 09fe317a 2018-03-11 stsp char *path;
124 09fe317a 2018-03-11 stsp int fd = -1;
126 09fe317a 2018-03-11 stsp struct stat sb;
128 09fe317a 2018-03-11 stsp *content = NULL;
130 7ac97322 2018-03-11 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
131 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
132 09fe317a 2018-03-11 stsp path = NULL;
136 ef99fdb1 2018-03-11 stsp fd = open(path, O_RDONLY | O_NOFOLLOW);
137 09fe317a 2018-03-11 stsp if (fd == -1) {
138 f02eaa22 2019-03-11 stsp if (errno == ENOENT)
139 a2e6d162 2019-07-27 stsp err = got_error_path(path, GOT_ERR_WORKTREE_META);
141 638f9024 2019-05-13 stsp err = got_error_from_errno2("open", path);
144 ef99fdb1 2018-03-11 stsp if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
145 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
146 638f9024 2019-05-13 stsp : got_error_from_errno2("flock", path));
150 e4b9a50c 2019-07-27 stsp if (fstat(fd, &sb) != 0) {
151 e4b9a50c 2019-07-27 stsp err = got_error_from_errno2("fstat", path);
154 09fe317a 2018-03-11 stsp *content = calloc(1, sb.st_size);
155 09fe317a 2018-03-11 stsp if (*content == NULL) {
156 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
160 09fe317a 2018-03-11 stsp n = read(fd, *content, sb.st_size);
161 09fe317a 2018-03-11 stsp if (n != sb.st_size) {
162 638f9024 2019-05-13 stsp err = (n == -1 ? got_error_from_errno2("read", path) :
163 a2e6d162 2019-07-27 stsp got_error_path(path, GOT_ERR_WORKTREE_META));
166 09fe317a 2018-03-11 stsp if ((*content)[sb.st_size - 1] != '\n') {
167 a2e6d162 2019-07-27 stsp err = got_error_path(path, GOT_ERR_WORKTREE_META);
170 09fe317a 2018-03-11 stsp (*content)[sb.st_size - 1] = '\0';
173 09fe317a 2018-03-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
174 638f9024 2019-05-13 stsp err = got_error_from_errno2("close", path_got);
175 09fe317a 2018-03-11 stsp free(path);
177 09fe317a 2018-03-11 stsp free(*content);
178 09fe317a 2018-03-11 stsp *content = NULL;
180 09fe317a 2018-03-11 stsp return err;
183 024e9686 2019-05-14 stsp static const struct got_error *
184 024e9686 2019-05-14 stsp write_head_ref(const char *path_got, struct got_reference *head_ref)
186 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
187 024e9686 2019-05-14 stsp char *refstr = NULL;
189 024e9686 2019-05-14 stsp if (got_ref_is_symbolic(head_ref)) {
190 024e9686 2019-05-14 stsp refstr = got_ref_to_str(head_ref);
191 024e9686 2019-05-14 stsp if (refstr == NULL)
192 024e9686 2019-05-14 stsp return got_error_from_errno("got_ref_to_str");
194 024e9686 2019-05-14 stsp refstr = strdup(got_ref_get_name(head_ref));
195 024e9686 2019-05-14 stsp if (refstr == NULL)
196 024e9686 2019-05-14 stsp return got_error_from_errno("strdup");
198 024e9686 2019-05-14 stsp err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
199 024e9686 2019-05-14 stsp free(refstr);
200 024e9686 2019-05-14 stsp return err;
203 86c3caaf 2018-03-09 stsp const struct got_error *
204 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
205 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
207 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
208 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
209 ec22038e 2019-03-10 stsp uuid_t uuid;
210 ec22038e 2019-03-10 stsp uint32_t uuid_status;
211 65596e15 2018-12-24 stsp int obj_type;
212 7ac97322 2018-03-11 stsp char *path_got = NULL;
213 1451e70d 2018-03-10 stsp char *formatstr = NULL;
214 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
215 65596e15 2018-12-24 stsp char *basestr = NULL;
216 ec22038e 2019-03-10 stsp char *uuidstr = NULL;
218 0c48fee2 2019-03-11 stsp if (strcmp(path, got_repo_get_path(repo)) == 0) {
219 0c48fee2 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_REPO);
223 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
225 65596e15 2018-12-24 stsp return err;
226 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
228 65596e15 2018-12-24 stsp return err;
229 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
230 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
232 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
233 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
234 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
237 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
238 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
239 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path);
243 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
244 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
245 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
248 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
249 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path_got);
253 056e7441 2018-03-11 stsp /* Create an empty lock file. */
254 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
258 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
259 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
263 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
264 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
268 65596e15 2018-12-24 stsp /* Record our base commit. */
269 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
272 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
276 1451e70d 2018-03-10 stsp /* Store path to repository. */
277 7839bc15 2019-01-06 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
278 7839bc15 2019-01-06 stsp got_repo_get_path(repo));
282 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
283 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
284 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
288 ec22038e 2019-03-10 stsp /* Generate UUID. */
289 ec22038e 2019-03-10 stsp uuid_create(&uuid, &uuid_status);
290 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
291 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_create");
294 ec22038e 2019-03-10 stsp uuid_to_string(&uuid, &uuidstr, &uuid_status);
295 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
296 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_to_string");
299 ec22038e 2019-03-10 stsp err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
303 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
304 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
305 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
308 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
313 65596e15 2018-12-24 stsp free(commit_id);
314 7ac97322 2018-03-11 stsp free(path_got);
315 1451e70d 2018-03-10 stsp free(formatstr);
316 0bb8a95e 2018-03-12 stsp free(absprefix);
317 65596e15 2018-12-24 stsp free(basestr);
318 ec22038e 2019-03-10 stsp free(uuidstr);
319 86c3caaf 2018-03-09 stsp return err;
322 247140b2 2019-02-05 stsp static const struct got_error *
323 247140b2 2019-02-05 stsp open_worktree(struct got_worktree **worktree, const char *path)
325 6d9d28c3 2018-03-11 stsp const struct got_error *err = NULL;
326 7ac97322 2018-03-11 stsp char *path_got;
327 6d9d28c3 2018-03-11 stsp char *formatstr = NULL;
328 c442a90d 2019-03-10 stsp char *uuidstr = NULL;
329 056e7441 2018-03-11 stsp char *path_lock = NULL;
330 eaccb85f 2018-12-25 stsp char *base_commit_id_str = NULL;
331 6d9d28c3 2018-03-11 stsp int version, fd = -1;
332 6d9d28c3 2018-03-11 stsp const char *errstr;
333 eaccb85f 2018-12-25 stsp struct got_repository *repo = NULL;
334 c442a90d 2019-03-10 stsp uint32_t uuid_status;
336 6d9d28c3 2018-03-11 stsp *worktree = NULL;
338 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
339 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
340 7ac97322 2018-03-11 stsp path_got = NULL;
344 7ac97322 2018-03-11 stsp if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
345 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
346 056e7441 2018-03-11 stsp path_lock = NULL;
350 056e7441 2018-03-11 stsp fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
351 6d9d28c3 2018-03-11 stsp if (fd == -1) {
352 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
353 638f9024 2019-05-13 stsp : got_error_from_errno2("open", path_lock));
357 7ac97322 2018-03-11 stsp err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
361 6d9d28c3 2018-03-11 stsp version = strtonum(formatstr, 1, INT_MAX, &errstr);
362 6d9d28c3 2018-03-11 stsp if (errstr) {
363 a2e6d162 2019-07-27 stsp err = got_error_msg(GOT_ERR_WORKTREE_META,
364 a2e6d162 2019-07-27 stsp "could not parse work tree format version number");
367 6d9d28c3 2018-03-11 stsp if (version != GOT_WORKTREE_FORMAT_VERSION) {
368 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_VERS);
372 6d9d28c3 2018-03-11 stsp *worktree = calloc(1, sizeof(**worktree));
373 6d9d28c3 2018-03-11 stsp if (*worktree == NULL) {
374 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
377 056e7441 2018-03-11 stsp (*worktree)->lockfd = -1;
379 7d61d891 2020-07-23 stsp (*worktree)->root_path = realpath(path, NULL);
380 c88eb298 2018-03-11 stsp if ((*worktree)->root_path == NULL) {
381 7d61d891 2020-07-23 stsp err = got_error_from_errno2("realpath", path);
384 cde76477 2018-03-11 stsp err = read_meta_file(&(*worktree)->repo_path, path_got,
385 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_REPOSITORY);
389 7ac97322 2018-03-11 stsp err = read_meta_file(&(*worktree)->path_prefix, path_got,
390 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_PATH_PREFIX);
394 eaccb85f 2018-12-25 stsp err = read_meta_file(&base_commit_id_str, path_got,
395 0f92850e 2018-12-25 stsp GOT_WORKTREE_BASE_COMMIT);
399 c442a90d 2019-03-10 stsp err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
402 c442a90d 2019-03-10 stsp uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
403 c442a90d 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
404 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_from_string");
408 c9956ddf 2019-09-08 stsp err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
412 eaccb85f 2018-12-25 stsp err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
413 eaccb85f 2018-12-25 stsp base_commit_id_str);
417 36a38700 2019-05-10 stsp err = read_meta_file(&(*worktree)->head_ref_name, path_got,
418 36a38700 2019-05-10 stsp GOT_WORKTREE_HEAD_REF);
421 eaccb85f 2018-12-25 stsp got_repo_close(repo);
422 7ac97322 2018-03-11 stsp free(path_got);
423 056e7441 2018-03-11 stsp free(path_lock);
424 eaccb85f 2018-12-25 stsp free(base_commit_id_str);
425 c442a90d 2019-03-10 stsp free(uuidstr);
426 bd165944 2019-03-10 stsp free(formatstr);
428 6d9d28c3 2018-03-11 stsp if (fd != -1)
430 6d9d28c3 2018-03-11 stsp if (*worktree != NULL)
431 6d9d28c3 2018-03-11 stsp got_worktree_close(*worktree);
432 6d9d28c3 2018-03-11 stsp *worktree = NULL;
434 056e7441 2018-03-11 stsp (*worktree)->lockfd = fd;
436 6d9d28c3 2018-03-11 stsp return err;
439 247140b2 2019-02-05 stsp const struct got_error *
440 247140b2 2019-02-05 stsp got_worktree_open(struct got_worktree **worktree, const char *path)
442 247140b2 2019-02-05 stsp const struct got_error *err = NULL;
445 247140b2 2019-02-05 stsp err = open_worktree(worktree, path);
446 f02eaa22 2019-03-11 stsp if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
447 247140b2 2019-02-05 stsp return err;
448 247140b2 2019-02-05 stsp if (*worktree)
449 247140b2 2019-02-05 stsp return NULL;
450 247140b2 2019-02-05 stsp path = dirname(path);
451 d1542a27 2019-02-05 stsp if (path == NULL)
452 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", path);
453 d1542a27 2019-02-05 stsp } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
455 247140b2 2019-02-05 stsp return got_error(GOT_ERR_NOT_WORKTREE);
458 3a6ce05a 2019-02-11 stsp const struct got_error *
459 86c3caaf 2018-03-09 stsp got_worktree_close(struct got_worktree *worktree)
461 3a6ce05a 2019-02-11 stsp const struct got_error *err = NULL;
462 cde76477 2018-03-11 stsp free(worktree->repo_path);
463 6d9d28c3 2018-03-11 stsp free(worktree->path_prefix);
464 eaccb85f 2018-12-25 stsp free(worktree->base_commit_id);
465 36a38700 2019-05-10 stsp free(worktree->head_ref_name);
466 056e7441 2018-03-11 stsp if (worktree->lockfd != -1)
467 3a6ce05a 2019-02-11 stsp if (close(worktree->lockfd) != 0)
468 638f9024 2019-05-13 stsp err = got_error_from_errno2("close",
469 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree));
470 7f11502c 2019-08-28 hiltjo free(worktree->root_path);
471 6d9d28c3 2018-03-11 stsp free(worktree);
472 3a6ce05a 2019-02-11 stsp return err;
475 2fbdb5ae 2018-12-29 stsp const char *
476 c7f4312f 2019-02-05 stsp got_worktree_get_root_path(struct got_worktree *worktree)
478 c7f4312f 2019-02-05 stsp return worktree->root_path;
481 c7f4312f 2019-02-05 stsp const char *
482 86c3caaf 2018-03-09 stsp got_worktree_get_repo_path(struct got_worktree *worktree)
484 2fbdb5ae 2018-12-29 stsp return worktree->repo_path;
487 49520a32 2018-12-29 stsp const char *
488 49520a32 2018-12-29 stsp got_worktree_get_path_prefix(struct got_worktree *worktree)
490 f609be2e 2018-12-29 stsp return worktree->path_prefix;
493 e5dc7198 2018-12-29 stsp const struct got_error *
494 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
495 e5dc7198 2018-12-29 stsp const char *path_prefix)
497 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
499 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
500 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
501 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
503 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
504 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
505 e5dc7198 2018-12-29 stsp free(absprefix);
506 e5dc7198 2018-12-29 stsp return NULL;
509 bc70eb79 2019-05-09 stsp const char *
510 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
512 36a38700 2019-05-10 stsp return worktree->head_ref_name;
515 024e9686 2019-05-14 stsp const struct got_error *
516 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
517 024e9686 2019-05-14 stsp struct got_reference *head_ref)
519 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
520 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
522 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
523 024e9686 2019-05-14 stsp GOT_WORKTREE_GOT_DIR) == -1) {
524 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
525 024e9686 2019-05-14 stsp path_got = NULL;
529 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
530 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
531 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
535 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
539 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
540 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
542 024e9686 2019-05-14 stsp free(path_got);
544 024e9686 2019-05-14 stsp free(head_ref_name);
545 024e9686 2019-05-14 stsp return err;
548 b72f483a 2019-02-05 stsp struct got_object_id *
549 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
551 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
554 507dc3bb 2018-12-29 stsp const struct got_error *
555 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
556 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
558 507dc3bb 2018-12-29 stsp const struct got_error *err;
559 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
560 507dc3bb 2018-12-29 stsp char *id_str = NULL;
561 507dc3bb 2018-12-29 stsp char *path_got = NULL;
563 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
564 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
565 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
566 507dc3bb 2018-12-29 stsp path_got = NULL;
570 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
572 507dc3bb 2018-12-29 stsp return err;
574 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
575 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
579 507dc3bb 2018-12-29 stsp /* Record our base commit. */
580 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
583 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
587 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
588 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
589 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
590 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
595 507dc3bb 2018-12-29 stsp got_object_close(obj);
596 507dc3bb 2018-12-29 stsp free(id_str);
597 507dc3bb 2018-12-29 stsp free(path_got);
598 507dc3bb 2018-12-29 stsp return err;
601 9d31a1d8 2018-03-11 stsp static const struct got_error *
602 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
604 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
605 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
606 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
607 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
608 86c3caaf 2018-03-09 stsp return NULL;
611 21908da4 2019-01-13 stsp static const struct got_error *
612 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
614 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
615 4a1ddfc2 2019-01-12 stsp char *abspath;
617 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
618 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
620 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
621 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
622 ddcd8544 2019-03-11 stsp struct stat sb;
623 ddcd8544 2019-03-11 stsp err = NULL;
624 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
625 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
626 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
627 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
628 3665fce0 2020-07-13 stsp err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
631 4a1ddfc2 2019-01-12 stsp free(abspath);
632 68c76935 2019-02-19 stsp return err;
635 68c76935 2019-02-19 stsp static const struct got_error *
636 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
638 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
639 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
640 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
641 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
646 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
647 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
648 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
651 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
652 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
653 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
656 68c76935 2019-02-19 stsp if (flen1 == 0) {
657 68c76935 2019-02-19 stsp if (flen2 != 0)
660 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
661 68c76935 2019-02-19 stsp if (flen1 != 0)
664 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
665 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
675 68c76935 2019-02-19 stsp return err;
678 68c76935 2019-02-19 stsp static const struct got_error *
679 68c76935 2019-02-19 stsp check_files_equal(int *same, const char *f1_path, const char *f2_path)
681 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
682 68c76935 2019-02-19 stsp struct stat sb;
683 68c76935 2019-02-19 stsp size_t size1, size2;
684 68c76935 2019-02-19 stsp FILE *f1 = NULL, *f2 = NULL;
688 68c76935 2019-02-19 stsp if (lstat(f1_path, &sb) != 0) {
689 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", f1_path);
692 68c76935 2019-02-19 stsp size1 = sb.st_size;
694 68c76935 2019-02-19 stsp if (lstat(f2_path, &sb) != 0) {
695 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", f2_path);
698 68c76935 2019-02-19 stsp size2 = sb.st_size;
700 68c76935 2019-02-19 stsp if (size1 != size2) {
702 68c76935 2019-02-19 stsp return NULL;
705 68c76935 2019-02-19 stsp f1 = fopen(f1_path, "r");
706 68c76935 2019-02-19 stsp if (f1 == NULL)
707 60522982 2019-12-15 stsp return got_error_from_errno2("fopen", f1_path);
709 68c76935 2019-02-19 stsp f2 = fopen(f2_path, "r");
710 68c76935 2019-02-19 stsp if (f2 == NULL) {
711 60522982 2019-12-15 stsp err = got_error_from_errno2("fopen", f2_path);
715 68c76935 2019-02-19 stsp err = check_file_contents_equal(same, f1, f2);
717 68c76935 2019-02-19 stsp if (f1 && fclose(f1) != 0 && err == NULL)
718 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
719 68c76935 2019-02-19 stsp if (f2 && fclose(f2) != 0 && err == NULL)
720 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
722 6353ad76 2019-02-08 stsp return err;
726 46b6ee73 2019-06-04 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
727 14c901f1 2019-08-08 stsp * the file at deriv_path acts as the first derived version, and the
728 14c901f1 2019-08-08 stsp * file on disk acts as the second derived version.
730 6353ad76 2019-02-08 stsp static const struct got_error *
731 14c901f1 2019-08-08 stsp merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
732 46b6ee73 2019-06-04 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
733 14c901f1 2019-08-08 stsp const char *path, uint16_t st_mode, const char *deriv_path,
734 f69721c3 2019-10-21 stsp const char *label_orig, const char *label_deriv,
735 f69721c3 2019-10-21 stsp struct got_repository *repo,
736 14c901f1 2019-08-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
738 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
739 6353ad76 2019-02-08 stsp int merged_fd = -1;
740 14c901f1 2019-08-08 stsp FILE *f_orig = NULL;
741 14c901f1 2019-08-08 stsp char *blob_orig_path = NULL;
742 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
743 234035bc 2019-06-01 stsp int overlapcnt = 0;
744 af54ae4a 2019-02-19 stsp char *parent;
746 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
748 af54ae4a 2019-02-19 stsp parent = dirname(ondisk_path);
749 af54ae4a 2019-02-19 stsp if (parent == NULL)
750 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", ondisk_path);
752 af54ae4a 2019-02-19 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1)
753 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
755 af54ae4a 2019-02-19 stsp err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
759 af54ae4a 2019-02-19 stsp free(base_path);
760 46b6ee73 2019-06-04 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
761 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
762 af54ae4a 2019-02-19 stsp base_path = NULL;
766 46b6ee73 2019-06-04 stsp err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
769 46b6ee73 2019-06-04 stsp if (blob_orig) {
770 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
771 46b6ee73 2019-06-04 stsp blob_orig);
776 1430b4e0 2019-03-27 stsp * If the file has no blob, this is an "add vs add" conflict,
777 1430b4e0 2019-03-27 stsp * and we simply use an empty ancestor file to make both files
778 1430b4e0 2019-03-27 stsp * appear in the merged result in their entirety.
782 14c901f1 2019-08-08 stsp err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
783 f69721c3 2019-10-21 stsp blob_orig_path, ondisk_path, label_deriv, label_orig, NULL);
787 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
788 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
792 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
793 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
797 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
798 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
799 14c901f1 2019-08-08 stsp err = check_files_equal(local_changes_subsumed, deriv_path,
800 68c76935 2019-02-19 stsp merged_path);
805 2ad902c0 2019-12-15 stsp if (fchmod(merged_fd, st_mode) != 0) {
806 2ad902c0 2019-12-15 stsp err = got_error_from_errno2("fchmod", merged_path);
810 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
811 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
812 230a42bd 2019-05-11 jcs ondisk_path);
817 fdcb7daf 2019-12-15 stsp if (merged_path)
818 fdcb7daf 2019-12-15 stsp unlink(merged_path);
820 3a6ce05a 2019-02-11 stsp if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
821 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
822 46b6ee73 2019-06-04 stsp if (f_orig && fclose(f_orig) != 0 && err == NULL)
823 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
824 6353ad76 2019-02-08 stsp free(merged_path);
825 af54ae4a 2019-02-19 stsp free(base_path);
826 46b6ee73 2019-06-04 stsp if (blob_orig_path) {
827 46b6ee73 2019-06-04 stsp unlink(blob_orig_path);
828 46b6ee73 2019-06-04 stsp free(blob_orig_path);
830 af57b12a 2020-07-23 stsp return err;
833 af57b12a 2020-07-23 stsp static const struct got_error *
834 af57b12a 2020-07-23 stsp update_symlink(const char *ondisk_path, const char *target_path,
835 af57b12a 2020-07-23 stsp size_t target_len)
837 af57b12a 2020-07-23 stsp /* This is not atomic but matches what 'ln -sf' does. */
838 af57b12a 2020-07-23 stsp if (unlink(ondisk_path) == -1)
839 af57b12a 2020-07-23 stsp return got_error_from_errno2("unlink", ondisk_path);
840 af57b12a 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1)
841 af57b12a 2020-07-23 stsp return got_error_from_errno3("symlink", target_path,
842 af57b12a 2020-07-23 stsp ondisk_path);
843 af57b12a 2020-07-23 stsp return NULL;
847 11cc08c1 2020-07-23 stsp * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
848 11cc08c1 2020-07-23 stsp * in the work tree with a file that contains conflict markers and the
849 993e2a1b 2020-07-23 stsp * conflicting target paths of the original version, a "derived version"
850 993e2a1b 2020-07-23 stsp * of a symlink from an incoming change, and a local version of the symlink.
852 993e2a1b 2020-07-23 stsp * The original versions's target path can be NULL if it is not available,
853 993e2a1b 2020-07-23 stsp * such as if both derived versions added a new symlink at the same path.
855 993e2a1b 2020-07-23 stsp * The incoming derived symlink target is NULL in case the incoming change
856 993e2a1b 2020-07-23 stsp * has deleted this symlink.
858 11cc08c1 2020-07-23 stsp static const struct got_error *
859 11cc08c1 2020-07-23 stsp install_symlink_conflict(const char *deriv_target,
860 11cc08c1 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, const char *orig_target,
861 11cc08c1 2020-07-23 stsp const char *label_orig, const char *local_target, const char *ondisk_path)
863 11cc08c1 2020-07-23 stsp const struct got_error *err;
864 11cc08c1 2020-07-23 stsp char *id_str = NULL, *label_deriv = NULL, *path = NULL;
865 11cc08c1 2020-07-23 stsp FILE *f = NULL;
867 11cc08c1 2020-07-23 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
869 11cc08c1 2020-07-23 stsp return got_error_from_errno("asprintf");
871 11cc08c1 2020-07-23 stsp if (asprintf(&label_deriv, "%s: commit %s",
872 11cc08c1 2020-07-23 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
873 11cc08c1 2020-07-23 stsp err = got_error_from_errno("asprintf");
877 11cc08c1 2020-07-23 stsp err = got_opentemp_named(&path, &f, "got-symlink-conflict");
881 11cc08c1 2020-07-23 stsp if (fprintf(f, "%s: Could not install symbolic link because of merge "
882 11cc08c1 2020-07-23 stsp "conflict.\nln(1) may be used to fix the situation. If this is "
883 11cc08c1 2020-07-23 stsp "intended to be a\nregular file instead then its expected "
884 11cc08c1 2020-07-23 stsp "contents may be filled in.\nThe following conflicting symlink "
885 11cc08c1 2020-07-23 stsp "target paths were found:\n"
886 11cc08c1 2020-07-23 stsp "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n", getprogname(),
887 993e2a1b 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
888 993e2a1b 2020-07-23 stsp deriv_target ? deriv_target : "(symlink was deleted)",
889 11cc08c1 2020-07-23 stsp orig_target ? label_orig : "",
890 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
891 11cc08c1 2020-07-23 stsp orig_target ? orig_target : "",
892 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
893 11cc08c1 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
894 11cc08c1 2020-07-23 stsp local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
895 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fprintf", path);
899 11cc08c1 2020-07-23 stsp if (unlink(ondisk_path) == -1) {
900 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("unlink", ondisk_path);
903 11cc08c1 2020-07-23 stsp if (rename(path, ondisk_path) == -1) {
904 11cc08c1 2020-07-23 stsp err = got_error_from_errno3("rename", path, ondisk_path);
907 11cc08c1 2020-07-23 stsp if (chmod(ondisk_path, GOT_DEFAULT_FILE_MODE) == -1) {
908 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("chmod", ondisk_path);
912 11cc08c1 2020-07-23 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
913 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fclose", path);
914 11cc08c1 2020-07-23 stsp free(path);
915 11cc08c1 2020-07-23 stsp free(id_str);
916 11cc08c1 2020-07-23 stsp free(label_deriv);
917 11cc08c1 2020-07-23 stsp return err;
920 d219f183 2020-07-23 stsp /* forward declaration */
921 d219f183 2020-07-23 stsp static const struct got_error *
922 d219f183 2020-07-23 stsp merge_blob(int *, struct got_worktree *, struct got_blob_object *,
923 d219f183 2020-07-23 stsp const char *, const char *, uint16_t, const char *,
924 d219f183 2020-07-23 stsp struct got_blob_object *, struct got_object_id *,
925 d219f183 2020-07-23 stsp struct got_repository *, got_worktree_checkout_cb, void *);
928 af57b12a 2020-07-23 stsp * Merge a symlink into the work tree, where blob_orig acts as the common
929 af57b12a 2020-07-23 stsp * ancestor, blob_deriv acts as the first derived version, and the symlink
930 af57b12a 2020-07-23 stsp * on disk acts as the second derived version.
931 af57b12a 2020-07-23 stsp * Assume that contents of both blobs represent symlinks.
933 af57b12a 2020-07-23 stsp static const struct got_error *
934 af57b12a 2020-07-23 stsp merge_symlink(struct got_worktree *worktree,
935 af57b12a 2020-07-23 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
936 af57b12a 2020-07-23 stsp const char *path, uint16_t st_mode, const char *label_orig,
937 af57b12a 2020-07-23 stsp struct got_blob_object *blob_deriv,
938 af57b12a 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
939 af57b12a 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
941 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
942 af57b12a 2020-07-23 stsp char *ancestor_target = NULL, *deriv_target = NULL;
943 af57b12a 2020-07-23 stsp struct stat sb;
944 11cc08c1 2020-07-23 stsp ssize_t ondisk_len, deriv_len;
945 af57b12a 2020-07-23 stsp char ondisk_target[PATH_MAX];
946 11cc08c1 2020-07-23 stsp int have_local_change = 0;
947 11cc08c1 2020-07-23 stsp int have_incoming_change = 0;
949 af57b12a 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1)
950 af57b12a 2020-07-23 stsp return got_error_from_errno2("lstat", ondisk_path);
952 af57b12a 2020-07-23 stsp if (!S_ISLNK(sb.st_mode)) {
954 d219f183 2020-07-23 stsp * If there is a regular file on disk, merge the symlink
955 d219f183 2020-07-23 stsp * target path into this file, which will usually cause
956 d219f183 2020-07-23 stsp * a merge conflict.
958 d219f183 2020-07-23 stsp if (S_ISREG(sb.st_mode)) {
959 d219f183 2020-07-23 stsp int local_changes_subsumed;
960 d219f183 2020-07-23 stsp return merge_blob(&local_changes_subsumed, worktree,
961 d219f183 2020-07-23 stsp NULL, ondisk_path, path, sb.st_mode, label_orig,
962 d219f183 2020-07-23 stsp blob_deriv, deriv_base_commit_id,
963 d219f183 2020-07-23 stsp repo, progress_cb, progress_arg);
966 af57b12a 2020-07-23 stsp /* TODO symlink is obstructed; do something */
967 af57b12a 2020-07-23 stsp return got_error_path(ondisk_path, GOT_ERR_FILE_OBSTRUCTED);
970 af57b12a 2020-07-23 stsp ondisk_len = readlink(ondisk_path, ondisk_target,
971 af57b12a 2020-07-23 stsp sizeof(ondisk_target));
972 af57b12a 2020-07-23 stsp if (ondisk_len == -1) {
973 af57b12a 2020-07-23 stsp err = got_error_from_errno2("readlink",
974 af57b12a 2020-07-23 stsp ondisk_path);
977 56d815a9 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
979 526a746f 2020-07-23 stsp if (blob_orig) {
980 526a746f 2020-07-23 stsp err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
985 af57b12a 2020-07-23 stsp err = got_object_blob_read_to_str(&deriv_target, blob_deriv);
989 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
990 11cc08c1 2020-07-23 stsp (ondisk_len != strlen(ancestor_target) ||
991 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
992 11cc08c1 2020-07-23 stsp have_local_change = 1;
994 11cc08c1 2020-07-23 stsp deriv_len = strlen(deriv_target);
995 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
996 11cc08c1 2020-07-23 stsp (deriv_len != strlen(ancestor_target) ||
997 11cc08c1 2020-07-23 stsp memcmp(deriv_target, ancestor_target, deriv_len) != 0))
998 11cc08c1 2020-07-23 stsp have_incoming_change = 1;
1000 11cc08c1 2020-07-23 stsp if (!have_local_change && !have_incoming_change) {
1001 11cc08c1 2020-07-23 stsp if (ancestor_target) {
1002 11cc08c1 2020-07-23 stsp /* Both sides made the same change. */
1003 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1005 11cc08c1 2020-07-23 stsp } else if (deriv_len == ondisk_len &&
1006 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1007 11cc08c1 2020-07-23 stsp /* Both sides added the same symlink. */
1008 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1011 11cc08c1 2020-07-23 stsp /* Both sides added symlinks which don't match. */
1012 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
1013 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target,
1014 11cc08c1 2020-07-23 stsp label_orig, ondisk_target, ondisk_path);
1016 11cc08c1 2020-07-23 stsp goto done;
1017 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1020 11cc08c1 2020-07-23 stsp } else if (!have_local_change && have_incoming_change) {
1021 af57b12a 2020-07-23 stsp /* Apply the incoming change. */
1022 af57b12a 2020-07-23 stsp err = update_symlink(ondisk_path, deriv_target,
1023 af57b12a 2020-07-23 stsp strlen(deriv_target));
1025 af57b12a 2020-07-23 stsp goto done;
1026 af57b12a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1027 11cc08c1 2020-07-23 stsp } else if (have_local_change && have_incoming_change) {
1028 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
1029 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target, label_orig,
1030 11cc08c1 2020-07-23 stsp ondisk_target, ondisk_path);
1032 11cc08c1 2020-07-23 stsp goto done;
1033 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1038 af57b12a 2020-07-23 stsp free(ancestor_target);
1039 af57b12a 2020-07-23 stsp free(deriv_target);
1040 14c901f1 2019-08-08 stsp return err;
1044 14c901f1 2019-08-08 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
1045 14c901f1 2019-08-08 stsp * blob_deriv acts as the first derived version, and the file on disk
1046 14c901f1 2019-08-08 stsp * acts as the second derived version.
1048 14c901f1 2019-08-08 stsp static const struct got_error *
1049 14c901f1 2019-08-08 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1050 14c901f1 2019-08-08 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
1051 f69721c3 2019-10-21 stsp const char *path, uint16_t st_mode, const char *label_orig,
1052 f69721c3 2019-10-21 stsp struct got_blob_object *blob_deriv,
1053 f69721c3 2019-10-21 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1054 f69721c3 2019-10-21 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1056 14c901f1 2019-08-08 stsp const struct got_error *err = NULL;
1057 14c901f1 2019-08-08 stsp FILE *f_deriv = NULL;
1058 14c901f1 2019-08-08 stsp char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1059 14c901f1 2019-08-08 stsp char *label_deriv = NULL, *parent;
1061 14c901f1 2019-08-08 stsp *local_changes_subsumed = 0;
1063 14c901f1 2019-08-08 stsp parent = dirname(ondisk_path);
1064 14c901f1 2019-08-08 stsp if (parent == NULL)
1065 14c901f1 2019-08-08 stsp return got_error_from_errno2("dirname", ondisk_path);
1067 14c901f1 2019-08-08 stsp free(base_path);
1068 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1069 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1070 14c901f1 2019-08-08 stsp base_path = NULL;
1071 14c901f1 2019-08-08 stsp goto done;
1074 14c901f1 2019-08-08 stsp err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1076 14c901f1 2019-08-08 stsp goto done;
1077 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1078 14c901f1 2019-08-08 stsp blob_deriv);
1080 14c901f1 2019-08-08 stsp goto done;
1082 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
1084 14c901f1 2019-08-08 stsp goto done;
1085 f69721c3 2019-10-21 stsp if (asprintf(&label_deriv, "%s: commit %s",
1086 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1087 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1088 14c901f1 2019-08-08 stsp goto done;
1091 14c901f1 2019-08-08 stsp err = merge_file(local_changes_subsumed, worktree, blob_orig,
1092 f69721c3 2019-10-21 stsp ondisk_path, path, st_mode, blob_deriv_path, label_orig,
1093 f69721c3 2019-10-21 stsp label_deriv, repo, progress_cb, progress_arg);
1095 14c901f1 2019-08-08 stsp if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
1096 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
1097 14c901f1 2019-08-08 stsp free(base_path);
1098 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
1099 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
1100 14c901f1 2019-08-08 stsp free(blob_deriv_path);
1102 6353ad76 2019-02-08 stsp free(id_str);
1103 818c7501 2019-07-11 stsp free(label_deriv);
1104 4a1ddfc2 2019-01-12 stsp return err;
1107 4a1ddfc2 2019-01-12 stsp static const struct got_error *
1108 054041d0 2020-06-24 stsp create_fileindex_entry(struct got_fileindex *fileindex,
1109 054041d0 2020-06-24 stsp struct got_object_id *base_commit_id, const char *ondisk_path,
1110 054041d0 2020-06-24 stsp const char *path, struct got_object_id *blob_id)
1112 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
1113 054041d0 2020-06-24 stsp struct got_fileindex_entry *new_ie;
1115 054041d0 2020-06-24 stsp err = got_fileindex_entry_alloc(&new_ie, path);
1117 054041d0 2020-06-24 stsp return err;
1119 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(new_ie, ondisk_path,
1120 054041d0 2020-06-24 stsp blob_id->sha1, base_commit_id->sha1, 1);
1122 054041d0 2020-06-24 stsp goto done;
1124 054041d0 2020-06-24 stsp err = got_fileindex_entry_add(fileindex, new_ie);
1127 054041d0 2020-06-24 stsp got_fileindex_entry_free(new_ie);
1128 13d9040b 2019-03-27 stsp return err;
1131 1ebedb77 2019-10-19 stsp static mode_t
1132 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
1134 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
1136 1ebedb77 2019-10-19 stsp if (executable) {
1137 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
1138 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
1139 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
1140 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
1141 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
1142 1ebedb77 2019-10-19 stsp return st_mode | xbits;
1145 1ebedb77 2019-10-19 stsp return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1148 8ba819a3 2020-07-23 stsp /* forward declaration */
1149 13d9040b 2019-03-27 stsp static const struct got_error *
1150 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1151 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
1152 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
1153 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1154 bd6aa359 2020-07-23 stsp struct got_repository *repo,
1155 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
1158 5a1fbc73 2020-07-23 stsp * This function assumes that the provided symlink target points at a
1159 5a1fbc73 2020-07-23 stsp * safe location in the work tree!
1161 8ba819a3 2020-07-23 stsp static const struct got_error *
1162 5a1fbc73 2020-07-23 stsp replace_existing_symlink(const char *ondisk_path, const char *target_path,
1163 5a1fbc73 2020-07-23 stsp size_t target_len)
1165 5a1fbc73 2020-07-23 stsp const struct got_error *err = NULL;
1166 5a1fbc73 2020-07-23 stsp ssize_t elen;
1167 5a1fbc73 2020-07-23 stsp char etarget[PATH_MAX];
1171 5a1fbc73 2020-07-23 stsp * "Bad" symlinks (those pointing outside the work tree or into the
1172 5a1fbc73 2020-07-23 stsp * .got directory) are installed in the work tree as a regular file
1173 5a1fbc73 2020-07-23 stsp * which contains the bad symlink target path.
1174 5a1fbc73 2020-07-23 stsp * The new symlink target has already been checked for safety by our
1175 5a1fbc73 2020-07-23 stsp * caller. If we can successfully open a regular file then we simply
1176 5a1fbc73 2020-07-23 stsp * replace this file with a symlink below.
1178 5a1fbc73 2020-07-23 stsp fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1179 5a1fbc73 2020-07-23 stsp if (fd == -1) {
1180 5a1fbc73 2020-07-23 stsp if (errno != ELOOP)
1181 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
1183 5a1fbc73 2020-07-23 stsp /* We are updating an existing on-disk symlink. */
1184 5a1fbc73 2020-07-23 stsp elen = readlink(ondisk_path, etarget, sizeof(etarget));
1185 5a1fbc73 2020-07-23 stsp if (elen == -1)
1186 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("readlink", ondisk_path);
1188 5a1fbc73 2020-07-23 stsp if (elen == target_len &&
1189 5a1fbc73 2020-07-23 stsp memcmp(etarget, target_path, target_len) == 0)
1190 5a1fbc73 2020-07-23 stsp return NULL; /* nothing to do */
1193 5a1fbc73 2020-07-23 stsp err = update_symlink(ondisk_path, target_path, target_len);
1194 5a1fbc73 2020-07-23 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1195 5a1fbc73 2020-07-23 stsp err = got_error_from_errno2("close", ondisk_path);
1196 5a1fbc73 2020-07-23 stsp return err;
1199 5a1fbc73 2020-07-23 stsp static const struct got_error *
1200 8ba819a3 2020-07-23 stsp install_symlink(struct got_worktree *worktree, const char *ondisk_path,
1201 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1202 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1203 8ba819a3 2020-07-23 stsp int reverting_versioned_file, struct got_repository *repo,
1204 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1206 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1207 8ba819a3 2020-07-23 stsp char target_path[PATH_MAX];
1208 8ba819a3 2020-07-23 stsp size_t len, target_len = 0;
1209 8ba819a3 2020-07-23 stsp char *resolved_path = NULL, *abspath = NULL;
1210 906c123b 2020-07-23 stsp char *path_got = NULL;
1211 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1212 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1215 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
1216 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
1217 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
1218 8ba819a3 2020-07-23 stsp * in the blob object.
1221 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1222 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1223 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
1224 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1225 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
1226 8ba819a3 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, st_mode, blob,
1227 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1228 bd6aa359 2020-07-23 stsp 1, repo, progress_cb, progress_arg);
1230 8ba819a3 2020-07-23 stsp if (len > 0) {
1231 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
1232 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1233 8ba819a3 2020-07-23 stsp len - hdrlen);
1234 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
1235 8ba819a3 2020-07-23 stsp hdrlen = 0;
1237 8ba819a3 2020-07-23 stsp } while (len != 0);
1238 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
1241 8ba819a3 2020-07-23 stsp * Relative symlink target lookup should begin at the directory
1242 8ba819a3 2020-07-23 stsp * in which the blob object is being installed.
1244 8ba819a3 2020-07-23 stsp if (!got_path_is_absolute(target_path)) {
1245 8ba819a3 2020-07-23 stsp char *parent = dirname(ondisk_path);
1246 ef68ca6f 2020-07-23 stsp if (parent == NULL) {
1247 ef68ca6f 2020-07-23 stsp err = got_error_from_errno2("dirname", ondisk_path);
1248 ef68ca6f 2020-07-23 stsp goto done;
1250 8ba819a3 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1251 8ba819a3 2020-07-23 stsp err = got_error_from_errno("asprintf");
1252 8ba819a3 2020-07-23 stsp goto done;
1257 8ba819a3 2020-07-23 stsp * unveil(2) restricts our view of paths in the filesystem.
1258 8ba819a3 2020-07-23 stsp * ENOENT will occur if a link target path does not exist or
1259 8ba819a3 2020-07-23 stsp * if it points outside our unveiled path space.
1261 8ba819a3 2020-07-23 stsp resolved_path = realpath(abspath ? abspath : target_path, NULL);
1262 8ba819a3 2020-07-23 stsp if (resolved_path == NULL) {
1263 b15bc87b 2020-07-23 stsp if (errno != ENOENT) {
1264 b15bc87b 2020-07-23 stsp err = got_error_from_errno2("realpath", target_path);
1265 b15bc87b 2020-07-23 stsp goto done;
1269 8ba819a3 2020-07-23 stsp /* Only allow symlinks pointing at paths within the work tree. */
1270 0ab20ee9 2020-07-23 stsp if (!got_path_is_child(resolved_path ? resolved_path : (abspath ?
1271 0ab20ee9 2020-07-23 stsp abspath : target_path), worktree->root_path,
1272 0ab20ee9 2020-07-23 stsp strlen(worktree->root_path))) {
1273 8ba819a3 2020-07-23 stsp /* install as a regular file */
1274 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1275 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1276 8ba819a3 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, st_mode, blob,
1277 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1278 8ba819a3 2020-07-23 stsp repo, progress_cb, progress_arg);
1279 8ba819a3 2020-07-23 stsp goto done;
1282 906c123b 2020-07-23 stsp /* Do not allow symlinks pointing into the .got directory. */
1283 906c123b 2020-07-23 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
1284 906c123b 2020-07-23 stsp GOT_WORKTREE_GOT_DIR) == -1) {
1285 906c123b 2020-07-23 stsp err = got_error_from_errno("asprintf");
1286 906c123b 2020-07-23 stsp goto done;
1288 906c123b 2020-07-23 stsp if (got_path_is_child(resolved_path ? resolved_path : (abspath ?
1289 906c123b 2020-07-23 stsp abspath : target_path), path_got, strlen(path_got))) {
1290 906c123b 2020-07-23 stsp /* install as a regular file */
1291 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1292 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1293 906c123b 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, st_mode, blob,
1294 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1295 906c123b 2020-07-23 stsp repo, progress_cb, progress_arg);
1296 906c123b 2020-07-23 stsp goto done;
1299 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1300 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1301 5a1fbc73 2020-07-23 stsp err = replace_existing_symlink(ondisk_path,
1302 5a1fbc73 2020-07-23 stsp target_path, target_len);
1304 f35fa46a 2020-07-23 stsp goto done;
1305 5a1fbc73 2020-07-23 stsp if (progress_cb) {
1306 5a1fbc73 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1307 5a1fbc73 2020-07-23 stsp GOT_STATUS_UPDATE, path);
1309 5a1fbc73 2020-07-23 stsp goto done; /* Nothing else to do. */
1312 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1313 f35fa46a 2020-07-23 stsp char *parent = dirname(ondisk_path);
1314 f35fa46a 2020-07-23 stsp if (parent == NULL) {
1315 f35fa46a 2020-07-23 stsp err = got_error_from_errno2("dirname",
1316 f35fa46a 2020-07-23 stsp ondisk_path);
1317 f35fa46a 2020-07-23 stsp goto done;
1319 f35fa46a 2020-07-23 stsp err = add_dir_on_disk(worktree, parent);
1321 f35fa46a 2020-07-23 stsp goto done;
1323 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1324 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1326 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1327 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1328 f35fa46a 2020-07-23 stsp goto done;
1332 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1333 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1334 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1335 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1336 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1337 8ba819a3 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, st_mode, blob,
1338 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1339 8ba819a3 2020-07-23 stsp repo, progress_cb, progress_arg);
1340 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1341 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1342 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1344 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1345 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1347 bd6aa359 2020-07-23 stsp } else if (progress_cb)
1348 f35fa46a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_ADD, path);
1350 8ba819a3 2020-07-23 stsp free(resolved_path);
1351 8ba819a3 2020-07-23 stsp free(abspath);
1352 906c123b 2020-07-23 stsp free(path_got);
1353 8ba819a3 2020-07-23 stsp return err;
1356 8ba819a3 2020-07-23 stsp static const struct got_error *
1357 8ba819a3 2020-07-23 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1358 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1359 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1360 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1361 bd6aa359 2020-07-23 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1362 bd6aa359 2020-07-23 stsp void *progress_arg)
1364 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1365 507dc3bb 2018-12-29 stsp int fd = -1;
1366 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1367 507dc3bb 2018-12-29 stsp int update = 0;
1368 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1370 8ba819a3 2020-07-23 stsp if (S_ISLNK(te_mode))
1371 8ba819a3 2020-07-23 stsp return install_symlink(worktree, ondisk_path, path, te_mode,
1372 8ba819a3 2020-07-23 stsp st_mode, blob, restoring_missing_file,
1373 8ba819a3 2020-07-23 stsp reverting_versioned_file, repo, progress_cb, progress_arg);
1375 c34b20a2 2018-03-12 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1376 9d31a1d8 2018-03-11 stsp GOT_DEFAULT_FILE_MODE);
1377 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1378 21908da4 2019-01-13 stsp if (errno == ENOENT) {
1379 21908da4 2019-01-13 stsp char *parent = dirname(path);
1380 21908da4 2019-01-13 stsp if (parent == NULL)
1381 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", path);
1382 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
1384 21908da4 2019-01-13 stsp return err;
1385 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1386 21908da4 2019-01-13 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1387 21908da4 2019-01-13 stsp GOT_DEFAULT_FILE_MODE);
1388 21908da4 2019-01-13 stsp if (fd == -1)
1389 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1390 230a42bd 2019-05-11 jcs ondisk_path);
1391 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1392 bd6aa359 2020-07-23 stsp if (!S_ISREG(st_mode) && !installing_bad_symlink) {
1393 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1394 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1395 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1396 507dc3bb 2018-12-29 stsp goto done;
1398 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1399 507dc3bb 2018-12-29 stsp ondisk_path);
1401 507dc3bb 2018-12-29 stsp goto done;
1402 507dc3bb 2018-12-29 stsp update = 1;
1405 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1408 bd6aa359 2020-07-23 stsp if (progress_cb) {
1409 bd6aa359 2020-07-23 stsp if (restoring_missing_file)
1410 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1412 bd6aa359 2020-07-23 stsp else if (reverting_versioned_file)
1413 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1416 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1417 bd6aa359 2020-07-23 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1419 bd6aa359 2020-07-23 stsp goto done;
1422 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1424 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1425 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1428 9d31a1d8 2018-03-11 stsp if (len > 0) {
1429 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1430 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1431 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1432 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1433 b87c6f83 2018-12-24 stsp goto done;
1434 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1435 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1436 b87c6f83 2018-12-24 stsp goto done;
1438 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1440 9d31a1d8 2018-03-11 stsp } while (len != 0);
1442 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1443 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1444 816dc654 2019-02-16 stsp goto done;
1447 507dc3bb 2018-12-29 stsp if (update) {
1448 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1449 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1450 230a42bd 2019-05-11 jcs ondisk_path);
1451 2a57020b 2019-02-20 stsp unlink(tmppath);
1452 68ed9ba5 2019-02-10 stsp goto done;
1456 1ebedb77 2019-10-19 stsp if (chmod(ondisk_path,
1457 1ebedb77 2019-10-19 stsp get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1458 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("chmod", ondisk_path);
1459 1ebedb77 2019-10-19 stsp goto done;
1463 3a6ce05a 2019-02-11 stsp if (fd != -1 && close(fd) != 0 && err == NULL)
1464 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1465 507dc3bb 2018-12-29 stsp free(tmppath);
1466 6353ad76 2019-02-08 stsp return err;
1469 7154f6ce 2019-03-27 stsp /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1470 6353ad76 2019-02-08 stsp static const struct got_error *
1471 7154f6ce 2019-03-27 stsp get_modified_file_content_status(unsigned char *status, FILE *f)
1473 7154f6ce 2019-03-27 stsp const struct got_error *err = NULL;
1474 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1475 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1476 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1477 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1479 7154f6ce 2019-03-27 stsp int i = 0;
1480 7154f6ce 2019-03-27 stsp char *line;
1481 7154f6ce 2019-03-27 stsp size_t len;
1482 7154f6ce 2019-03-27 stsp const char delim[3] = {'\0', '\0', '\0'};
1484 7154f6ce 2019-03-27 stsp while (*status == GOT_STATUS_MODIFY) {
1485 7154f6ce 2019-03-27 stsp line = fparseln(f, &len, NULL, delim, 0);
1486 7154f6ce 2019-03-27 stsp if (line == NULL) {
1487 7154f6ce 2019-03-27 stsp if (feof(f))
1489 7154f6ce 2019-03-27 stsp err = got_ferror(f, GOT_ERR_IO);
1493 7154f6ce 2019-03-27 stsp if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1494 19332e6d 2019-05-13 stsp if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1496 7154f6ce 2019-03-27 stsp *status = GOT_STATUS_CONFLICT;
1502 7154f6ce 2019-03-27 stsp return err;
1505 e2b1e152 2019-07-13 stsp static int
1506 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1508 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1509 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1512 1ebedb77 2019-10-19 stsp static int
1513 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1515 e2b1e152 2019-07-13 stsp return !(ie->ctime_sec == sb->st_ctime &&
1516 e2b1e152 2019-07-13 stsp ie->ctime_nsec == sb->st_ctimensec &&
1517 e2b1e152 2019-07-13 stsp ie->mtime_sec == sb->st_mtime &&
1518 e2b1e152 2019-07-13 stsp ie->mtime_nsec == sb->st_mtimensec &&
1519 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1520 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1523 c363b2c1 2019-08-03 stsp static unsigned char
1524 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1526 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1527 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1528 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1529 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1530 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1531 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1532 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1534 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1538 a919d5c4 2020-07-23 stsp static const struct got_error *
1539 a919d5c4 2020-07-23 stsp get_symlink_status(unsigned char *status, struct stat *sb,
1540 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1541 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1543 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1544 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1545 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1546 a919d5c4 2020-07-23 stsp ssize_t elen;
1547 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1548 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1549 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1551 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1553 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1555 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1557 a919d5c4 2020-07-23 stsp return err;
1558 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1560 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1561 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1563 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1565 a919d5c4 2020-07-23 stsp if (len > 0) {
1566 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1567 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1568 a919d5c4 2020-07-23 stsp len - hdrlen);
1569 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1570 a919d5c4 2020-07-23 stsp hdrlen = 0;
1572 a919d5c4 2020-07-23 stsp } while (len != 0);
1573 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1575 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1576 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1577 a919d5c4 2020-07-23 stsp if (elen == -1)
1578 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1580 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1581 a919d5c4 2020-07-23 stsp if (elen == -1)
1582 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1585 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1586 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1588 a919d5c4 2020-07-23 stsp return NULL;
1591 7154f6ce 2019-03-27 stsp static const struct got_error *
1592 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1593 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1594 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1596 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1597 6353ad76 2019-02-08 stsp struct got_object_id id;
1598 6353ad76 2019-02-08 stsp size_t hdrlen;
1599 1338848f 2019-12-13 stsp int fd = -1;
1600 6353ad76 2019-02-08 stsp FILE *f = NULL;
1601 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1602 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1603 6353ad76 2019-02-08 stsp size_t flen, blen;
1604 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
1606 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1609 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1610 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1611 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1613 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1614 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1615 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1616 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1617 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1619 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1620 882ef1b9 2019-12-13 stsp goto done;
1622 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1623 3d35a492 2019-12-13 stsp goto done;
1626 7f91a133 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1627 a919d5c4 2020-07-23 stsp if (fd == -1 && errno != ENOENT && errno != ELOOP)
1628 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1629 a919d5c4 2020-07-23 stsp else if (fd == -1 && errno == ELOOP) {
1630 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1631 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1632 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1633 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1634 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1635 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1637 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1638 3d35a492 2019-12-13 stsp goto done;
1640 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1641 1338848f 2019-12-13 stsp goto done;
1645 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1646 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1647 1338848f 2019-12-13 stsp goto done;
1650 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1651 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1652 1338848f 2019-12-13 stsp goto done;
1653 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1654 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1655 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1656 1338848f 2019-12-13 stsp goto done;
1659 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1660 1338848f 2019-12-13 stsp goto done;
1662 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1663 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1664 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1666 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1668 6353ad76 2019-02-08 stsp err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1670 1338848f 2019-12-13 stsp goto done;
1672 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1673 a919d5c4 2020-07-23 stsp /* Staging changes to symlinks is not yet(?) supported. */
1674 a919d5c4 2020-07-23 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
1675 a919d5c4 2020-07-23 stsp err = got_error_path(abspath, GOT_ERR_FILE_STATUS);
1676 a919d5c4 2020-07-23 stsp goto done;
1678 a919d5c4 2020-07-23 stsp err = get_symlink_status(status, sb, ie, abspath, dirfd,
1679 a919d5c4 2020-07-23 stsp de_name, blob);
1680 a919d5c4 2020-07-23 stsp goto done;
1684 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1685 3d35a492 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1686 ab0d4361 2019-12-13 stsp if (fd == -1) {
1687 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1688 ab0d4361 2019-12-13 stsp goto done;
1692 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1693 6353ad76 2019-02-08 stsp if (f == NULL) {
1694 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1695 6353ad76 2019-02-08 stsp goto done;
1698 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1700 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1701 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1703 7154f6ce 2019-03-27 stsp goto done;
1704 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1705 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1706 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1707 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1708 7154f6ce 2019-03-27 stsp goto done;
1710 6353ad76 2019-02-08 stsp if (blen == 0) {
1711 6353ad76 2019-02-08 stsp if (flen != 0)
1712 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1714 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1715 6353ad76 2019-02-08 stsp if (blen != 0)
1716 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1718 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1719 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1720 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1721 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1725 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1728 6353ad76 2019-02-08 stsp hdrlen = 0;
1731 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1732 7154f6ce 2019-03-27 stsp rewind(f);
1733 7154f6ce 2019-03-27 stsp err = get_modified_file_content_status(status, f);
1734 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1735 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1738 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1739 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1740 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1741 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1742 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1743 9d31a1d8 2018-03-11 stsp return err;
1747 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1748 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1750 e2b1e152 2019-07-13 stsp static const struct got_error *
1751 e2b1e152 2019-07-13 stsp sync_timestamps(char *ondisk_path, unsigned char status,
1752 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1754 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1755 e2b1e152 2019-07-13 stsp return got_fileindex_entry_update(ie, ondisk_path,
1756 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1758 e2b1e152 2019-07-13 stsp return NULL;
1761 9d31a1d8 2018-03-11 stsp static const struct got_error *
1762 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1763 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1764 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1765 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1766 0584f854 2019-04-06 stsp void *progress_arg)
1768 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1769 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1770 6353ad76 2019-02-08 stsp char *ondisk_path;
1771 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1772 b8f41171 2019-02-10 stsp struct stat sb;
1774 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1775 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1778 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1779 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1780 a76c42e6 2019-08-03 stsp goto done;
1782 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1785 abb4604f 2019-07-27 stsp goto done;
1786 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1787 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1789 abb4604f 2019-07-27 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1791 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1792 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1793 b8f41171 2019-02-10 stsp goto done;
1795 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1796 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1798 5036ab18 2020-04-18 stsp goto done;
1801 523b8417 2019-10-19 stsp if (ie && status != GOT_STATUS_MISSING &&
1802 523b8417 2019-10-19 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1803 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1804 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1805 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1806 e2b1e152 2019-07-13 stsp err = sync_timestamps(ondisk_path, status, ie, &sb);
1808 e2b1e152 2019-07-13 stsp goto done;
1809 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1811 6353ad76 2019-02-08 stsp goto done;
1813 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1814 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
1815 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1816 e2b1e152 2019-07-13 stsp err = sync_timestamps(ondisk_path, status, ie, &sb);
1817 b8f41171 2019-02-10 stsp goto done;
1821 56e0773d 2019-11-28 stsp err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1823 6353ad76 2019-02-08 stsp goto done;
1825 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1826 234035bc 2019-06-01 stsp int update_timestamps;
1827 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
1828 f69721c3 2019-10-21 stsp char *label_orig = NULL;
1829 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
1830 234035bc 2019-06-01 stsp struct got_object_id id2;
1831 234035bc 2019-06-01 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1832 234035bc 2019-06-01 stsp err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1834 f69721c3 2019-10-21 stsp goto done;
1836 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
1837 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
1838 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1839 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
1840 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
1841 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
1842 f69721c3 2019-10-21 stsp goto done;
1844 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
1845 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
1846 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
1847 234035bc 2019-06-01 stsp goto done;
1850 993e2a1b 2020-07-23 stsp if (S_ISLNK(te->mode)) {
1851 993e2a1b 2020-07-23 stsp err = merge_symlink(worktree, blob2,
1852 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig,
1853 993e2a1b 2020-07-23 stsp blob, worktree->base_commit_id, repo,
1854 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
1856 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
1857 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
1858 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
1859 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
1861 f69721c3 2019-10-21 stsp free(label_orig);
1862 234035bc 2019-06-01 stsp if (blob2)
1863 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
1865 909d120e 2019-09-22 stsp goto done;
1867 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
1868 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
1869 234035bc 2019-06-01 stsp * unmodified files again.
1871 234035bc 2019-06-01 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1872 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
1873 234035bc 2019-06-01 stsp update_timestamps);
1874 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
1875 1ebedb77 2019-10-19 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1876 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
1877 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
1878 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1880 1ee397ad 2019-07-12 stsp goto done;
1881 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1882 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
1884 13d9040b 2019-03-27 stsp goto done;
1886 13d9040b 2019-03-27 stsp err = install_blob(worktree, ondisk_path, path, te->mode,
1887 bd6aa359 2020-07-23 stsp sb.st_mode, blob, status == GOT_STATUS_MISSING, 0, 0,
1888 a129376b 2019-03-28 stsp repo, progress_cb, progress_arg);
1890 13d9040b 2019-03-27 stsp goto done;
1892 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1893 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 1);
1895 054041d0 2020-06-24 stsp err = create_fileindex_entry(fileindex,
1896 054041d0 2020-06-24 stsp worktree->base_commit_id, ondisk_path, path,
1897 054041d0 2020-06-24 stsp &blob->id);
1900 13d9040b 2019-03-27 stsp goto done;
1902 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
1904 6353ad76 2019-02-08 stsp free(ondisk_path);
1905 8da9e5f4 2019-01-12 stsp return err;
1908 90285c3b 2019-01-08 stsp static const struct got_error *
1909 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
1911 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
1912 90285c3b 2019-01-08 stsp char *ondisk_path = NULL;
1914 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1915 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1917 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
1918 c97665ae 2019-01-13 stsp if (errno != ENOENT)
1919 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
1921 90285c3b 2019-01-08 stsp char *parent = dirname(ondisk_path);
1922 7a9df742 2019-01-08 stsp while (parent && strcmp(parent, root_path) != 0) {
1923 26c4ac4d 2019-01-08 stsp if (rmdir(parent) == -1) {
1924 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
1925 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
1929 90285c3b 2019-01-08 stsp parent = dirname(parent);
1932 90285c3b 2019-01-08 stsp free(ondisk_path);
1933 90285c3b 2019-01-08 stsp return err;
1936 708d8e67 2019-03-27 stsp static const struct got_error *
1937 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1938 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
1939 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1941 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
1942 708d8e67 2019-03-27 stsp unsigned char status;
1943 708d8e67 2019-03-27 stsp struct stat sb;
1944 708d8e67 2019-03-27 stsp char *ondisk_path;
1946 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1947 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1949 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1951 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1953 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
1955 5a58a424 2020-06-23 stsp goto done;
1957 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
1958 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
1959 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
1960 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
1961 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
1962 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
1963 993e2a1b 2020-07-23 stsp goto done;
1965 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
1966 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
1967 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
1968 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
1970 993e2a1b 2020-07-23 stsp goto done;
1971 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1972 993e2a1b 2020-07-23 stsp ie->path);
1973 993e2a1b 2020-07-23 stsp goto done;
1976 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1977 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
1978 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1980 5a58a424 2020-06-23 stsp goto done;
1982 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
1983 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
1985 fc6346c4 2019-03-27 stsp err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1988 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1990 5a58a424 2020-06-23 stsp goto done;
1991 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
1992 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
1994 5a58a424 2020-06-23 stsp goto done;
1996 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
1999 5a58a424 2020-06-23 stsp free(ondisk_path);
2000 fc6346c4 2019-03-27 stsp return err;
2003 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2004 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2005 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2006 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2007 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2008 8da9e5f4 2019-01-12 stsp void *progress_arg;
2009 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2010 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2013 512f0d0e 2019-01-02 stsp static const struct got_error *
2014 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2015 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2017 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2019 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2020 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2022 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2023 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2026 8da9e5f4 2019-01-12 stsp static const struct got_error *
2027 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2029 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2031 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2032 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2034 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2035 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2038 9d31a1d8 2018-03-11 stsp static const struct got_error *
2039 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2041 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2042 8da9e5f4 2019-01-12 stsp const struct got_error *err;
2043 8da9e5f4 2019-01-12 stsp char *path;
2045 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2046 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2048 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2049 63c5ca5d 2019-08-24 stsp return NULL;
2051 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2052 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2054 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2056 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
2057 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
2059 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2060 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2062 8da9e5f4 2019-01-12 stsp free(path);
2063 0cd1c46a 2019-03-11 stsp return err;
2066 818c7501 2019-07-11 stsp static const struct got_error *
2067 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2069 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2070 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2071 0cd1c46a 2019-03-11 stsp uint32_t uuid_status;
2073 517bab73 2019-03-11 stsp *refname = NULL;
2075 0cd1c46a 2019-03-11 stsp uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
2076 0cd1c46a 2019-03-11 stsp if (uuid_status != uuid_s_ok)
2077 cc483380 2019-09-01 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2079 818c7501 2019-07-11 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr)
2081 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2082 0647c563 2019-03-11 stsp *refname = NULL;
2084 517bab73 2019-03-11 stsp free(uuidstr);
2085 517bab73 2019-03-11 stsp return err;
2088 818c7501 2019-07-11 stsp const struct got_error *
2089 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2091 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2094 818c7501 2019-07-11 stsp static const struct got_error *
2095 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2097 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2098 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2101 818c7501 2019-07-11 stsp static const struct got_error *
2102 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2104 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2107 818c7501 2019-07-11 stsp static const struct got_error *
2108 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2110 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2111 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2114 818c7501 2019-07-11 stsp static const struct got_error *
2115 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2117 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2118 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2121 0ebf8283 2019-07-24 stsp static const struct got_error *
2122 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2124 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2125 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2128 0ebf8283 2019-07-24 stsp static const struct got_error *
2129 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2131 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2132 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2135 0ebf8283 2019-07-24 stsp static const struct got_error *
2136 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2138 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2139 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2142 0ebf8283 2019-07-24 stsp static const struct got_error *
2143 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2145 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2146 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2149 0ebf8283 2019-07-24 stsp const struct got_error *
2150 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2151 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2153 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2154 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2155 0ebf8283 2019-07-24 stsp *path = NULL;
2156 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2158 0ebf8283 2019-07-24 stsp return NULL;
2162 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2163 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2165 517bab73 2019-03-11 stsp static const struct got_error *
2166 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2168 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2169 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2170 517bab73 2019-03-11 stsp char *refname;
2172 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
2174 517bab73 2019-03-11 stsp return err;
2176 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2178 0cd1c46a 2019-03-11 stsp goto done;
2180 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2182 0cd1c46a 2019-03-11 stsp free(refname);
2184 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2185 3e3a69f1 2019-07-25 stsp return err;
2188 3e3a69f1 2019-07-25 stsp static const struct got_error *
2189 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2191 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2193 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2194 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2195 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2196 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2198 9d31a1d8 2018-03-11 stsp return err;
2202 ebf99748 2019-05-09 stsp static const struct got_error *
2203 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2204 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2206 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2207 ebf99748 2019-05-09 stsp FILE *index = NULL;
2209 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2210 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2211 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2212 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2214 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2216 ebf99748 2019-05-09 stsp goto done;
2218 ebf99748 2019-05-09 stsp index = fopen(*fileindex_path, "rb");
2219 ebf99748 2019-05-09 stsp if (index == NULL) {
2220 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2221 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2223 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2224 ebf99748 2019-05-09 stsp if (fclose(index) != 0 && err == NULL)
2225 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2228 ebf99748 2019-05-09 stsp if (err) {
2229 ebf99748 2019-05-09 stsp free(*fileindex_path);
2230 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2231 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2232 ebf99748 2019-05-09 stsp *fileindex = NULL;
2234 ebf99748 2019-05-09 stsp return err;
2237 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2238 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2239 c932eeeb 2019-05-22 stsp const char *path;
2240 c932eeeb 2019-05-22 stsp size_t path_len;
2241 c932eeeb 2019-05-22 stsp const char *entry_name;
2242 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2243 a484d721 2019-06-10 stsp void *progress_arg;
2246 c932eeeb 2019-05-22 stsp /* Bump base commit ID of all files within an updated part of the work tree. */
2247 c932eeeb 2019-05-22 stsp static const struct got_error *
2248 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2250 1ee397ad 2019-07-12 stsp const struct got_error *err;
2251 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2253 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2254 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2255 c932eeeb 2019-05-22 stsp return NULL;
2256 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2257 102ff934 2019-06-11 stsp return NULL;
2259 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2260 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2261 c932eeeb 2019-05-22 stsp return NULL;
2263 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2264 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2265 edd02c5e 2019-07-11 stsp ie->path);
2267 1ee397ad 2019-07-12 stsp return err;
2269 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2270 c932eeeb 2019-05-22 stsp return NULL;
2273 9c6338c4 2019-06-02 stsp static const struct got_error *
2274 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2276 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2277 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2278 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2279 867630bb 2020-01-17 stsp struct timespec timeout;
2281 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2282 9c6338c4 2019-06-02 stsp fileindex_path);
2284 9c6338c4 2019-06-02 stsp goto done;
2286 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2288 9c6338c4 2019-06-02 stsp goto done;
2290 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2291 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2292 9c6338c4 2019-06-02 stsp fileindex_path);
2293 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2297 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2298 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2299 867630bb 2020-01-17 stsp * was recorded in the file index.
2301 867630bb 2020-01-17 stsp timeout.tv_sec = 0;
2302 867630bb 2020-01-17 stsp timeout.tv_nsec = 1;
2303 867630bb 2020-01-17 stsp nanosleep(&timeout, NULL);
2305 9c6338c4 2019-06-02 stsp if (new_index)
2306 9c6338c4 2019-06-02 stsp fclose(new_index);
2307 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2308 9c6338c4 2019-06-02 stsp return err;
2311 6ced4176 2019-07-12 stsp static const struct got_error *
2312 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2313 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2314 6ced4176 2019-07-12 stsp struct got_worktree *worktree, struct got_repository *repo)
2316 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2317 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2318 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2319 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2321 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2322 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2323 6ced4176 2019-07-12 stsp *tree_id = NULL;
2325 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2326 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2327 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2328 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2329 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2330 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2331 6ced4176 2019-07-12 stsp goto done;
2333 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2334 6ced4176 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
2336 6ced4176 2019-07-12 stsp goto done;
2337 6ced4176 2019-07-12 stsp return NULL;
2340 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2342 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2343 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2344 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2345 6ced4176 2019-07-12 stsp goto done;
2348 6ced4176 2019-07-12 stsp err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2349 6ced4176 2019-07-12 stsp in_repo_path);
2351 6ced4176 2019-07-12 stsp goto done;
2353 6ced4176 2019-07-12 stsp free(in_repo_path);
2354 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2356 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2358 6ced4176 2019-07-12 stsp goto done;
2360 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2361 6ced4176 2019-07-12 stsp /* Check out a single file. */
2362 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2363 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2364 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2365 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2366 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2367 6ced4176 2019-07-12 stsp goto done;
2369 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2370 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2371 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2372 6ced4176 2019-07-12 stsp goto done;
2375 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2376 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2378 6ced4176 2019-07-12 stsp return err;
2379 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2380 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2381 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2382 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2383 6ced4176 2019-07-12 stsp goto done;
2386 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2387 6ced4176 2019-07-12 stsp worktree->base_commit_id, in_repo_path);
2389 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2390 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2391 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2392 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2393 6ced4176 2019-07-12 stsp goto done;
2395 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2396 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2397 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2398 6ced4176 2019-07-12 stsp goto done;
2403 6ced4176 2019-07-12 stsp free(in_repo_path);
2404 6ced4176 2019-07-12 stsp if (err) {
2405 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2406 6ced4176 2019-07-12 stsp free(*tree_relpath);
2407 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2408 6ced4176 2019-07-12 stsp free(*tree_id);
2409 6ced4176 2019-07-12 stsp *tree_id = NULL;
2411 07ed472d 2019-07-12 stsp return err;
2414 07ed472d 2019-07-12 stsp static const struct got_error *
2415 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2416 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2417 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2418 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2420 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2421 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2422 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2423 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2424 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2426 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2427 7f47418f 2019-12-20 stsp if (err) {
2428 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2429 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2430 7f47418f 2019-12-20 stsp goto done;
2431 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2432 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2434 7f47418f 2019-12-20 stsp return err;
2437 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2438 07ed472d 2019-07-12 stsp worktree->base_commit_id);
2440 07ed472d 2019-07-12 stsp goto done;
2442 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2444 07ed472d 2019-07-12 stsp goto done;
2446 07ed472d 2019-07-12 stsp if (entry_name &&
2447 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2448 07ed472d 2019-07-12 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
2449 07ed472d 2019-07-12 stsp goto done;
2452 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2453 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2454 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2455 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2456 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2457 07ed472d 2019-07-12 stsp arg.repo = repo;
2458 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2459 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2460 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2461 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2462 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2463 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2466 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2467 07ed472d 2019-07-12 stsp if (commit)
2468 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2469 6ced4176 2019-07-12 stsp return err;
2472 9d31a1d8 2018-03-11 stsp const struct got_error *
2473 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2474 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2475 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2476 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2478 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2479 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2480 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2481 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2482 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2483 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2484 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2485 f2ea84fa 2019-07-27 stsp SIMPLEQ_ENTRY(tree_path_data) entry;
2486 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2487 f2ea84fa 2019-07-27 stsp int entry_type;
2488 f2ea84fa 2019-07-27 stsp char *relpath;
2489 f2ea84fa 2019-07-27 stsp char *entry_name;
2490 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2491 f2ea84fa 2019-07-27 stsp SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2493 f2ea84fa 2019-07-27 stsp SIMPLEQ_INIT(&tree_paths);
2495 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2497 9d31a1d8 2018-03-11 stsp return err;
2499 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2500 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2501 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2502 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2503 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2504 f2ea84fa 2019-07-27 stsp goto done;
2507 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2508 f2ea84fa 2019-07-27 stsp &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2509 f2ea84fa 2019-07-27 stsp if (err) {
2510 f2ea84fa 2019-07-27 stsp free(tpd);
2511 6ced4176 2019-07-12 stsp goto done;
2514 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2515 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2516 f2ea84fa 2019-07-27 stsp if (err) {
2517 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2518 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2519 f2ea84fa 2019-07-27 stsp free(tpd);
2520 f2ea84fa 2019-07-27 stsp goto done;
2523 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2525 f2ea84fa 2019-07-27 stsp SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2529 51514078 2018-12-25 stsp * Read the file index.
2530 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2531 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2533 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2535 c4cdcb68 2019-04-03 stsp goto done;
2537 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_FIRST(&tree_paths);
2538 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2539 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2541 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2542 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2543 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2547 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2548 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2549 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2550 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2551 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2552 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2553 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2554 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2558 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_NEXT(tpd, entry);
2560 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2561 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2562 af12c6b9 2019-06-04 stsp err = sync_err;
2564 9c6338c4 2019-06-02 stsp free(fileindex_path);
2566 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2567 9d31a1d8 2018-03-11 stsp if (commit)
2568 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2569 5ade8233 2019-07-12 stsp if (fileindex)
2570 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2571 f2ea84fa 2019-07-27 stsp while (!SIMPLEQ_EMPTY(&tree_paths)) {
2572 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_FIRST(&tree_paths);
2573 f2ea84fa 2019-07-27 stsp SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2574 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2575 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2576 f2ea84fa 2019-07-27 stsp free(tpd);
2578 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2579 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2580 9d31a1d8 2018-03-11 stsp err = unlockerr;
2581 f8d1f275 2019-02-04 stsp return err;
2584 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2585 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2586 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2587 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2588 234035bc 2019-06-01 stsp void *progress_arg;
2589 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2590 234035bc 2019-06-01 stsp void *cancel_arg;
2591 f69721c3 2019-10-21 stsp const char *label_orig;
2592 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2595 234035bc 2019-06-01 stsp static const struct got_error *
2596 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2597 234035bc 2019-06-01 stsp struct got_blob_object *blob2, struct got_object_id *id1,
2598 234035bc 2019-06-01 stsp struct got_object_id *id2, const char *path1, const char *path2,
2599 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2601 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2602 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2603 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2604 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2605 234035bc 2019-06-01 stsp struct stat sb;
2606 234035bc 2019-06-01 stsp unsigned char status;
2607 234035bc 2019-06-01 stsp int local_changes_subsumed;
2609 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2610 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2611 d6c87207 2019-08-02 stsp strlen(path2));
2612 1ee397ad 2019-07-12 stsp if (ie == NULL)
2613 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2614 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2616 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2617 234035bc 2019-06-01 stsp path2) == -1)
2618 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2620 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2623 234035bc 2019-06-01 stsp goto done;
2625 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2626 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2627 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
2628 234035bc 2019-06-01 stsp goto done;
2630 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2631 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2632 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2633 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2634 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
2635 234035bc 2019-06-01 stsp goto done;
2638 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2639 af57b12a 2020-07-23 stsp err = merge_symlink(a->worktree, blob1,
2640 af57b12a 2020-07-23 stsp ondisk_path, path2, sb.st_mode, a->label_orig,
2641 af57b12a 2020-07-23 stsp blob2, a->commit_id2, repo, a->progress_cb,
2642 af57b12a 2020-07-23 stsp a->progress_arg);
2644 af57b12a 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
2645 af57b12a 2020-07-23 stsp blob1, ondisk_path, path2, sb.st_mode,
2646 af57b12a 2020-07-23 stsp a->label_orig, blob2, a->commit_id2, repo,
2647 af57b12a 2020-07-23 stsp a->progress_cb, a->progress_arg);
2649 234035bc 2019-06-01 stsp } else if (blob1) {
2650 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
2651 d6c87207 2019-08-02 stsp strlen(path1));
2652 1ee397ad 2019-07-12 stsp if (ie == NULL)
2653 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2654 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
2656 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2657 2b92fad7 2019-06-02 stsp path1) == -1)
2658 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
2660 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2663 2b92fad7 2019-06-02 stsp goto done;
2665 2b92fad7 2019-06-02 stsp switch (status) {
2666 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
2667 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2668 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2670 1ee397ad 2019-07-12 stsp goto done;
2671 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
2673 2b92fad7 2019-06-02 stsp goto done;
2675 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2677 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
2678 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
2679 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2680 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2682 1ee397ad 2019-07-12 stsp goto done;
2684 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2686 2b92fad7 2019-06-02 stsp case GOT_STATUS_ADD:
2687 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
2688 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
2689 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2690 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
2692 1ee397ad 2019-07-12 stsp goto done;
2694 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
2695 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
2697 1ee397ad 2019-07-12 stsp goto done;
2702 234035bc 2019-06-01 stsp } else if (blob2) {
2703 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2704 234035bc 2019-06-01 stsp path2) == -1)
2705 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2706 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2707 d6c87207 2019-08-02 stsp strlen(path2));
2709 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
2710 7f91a133 2019-12-13 stsp -1, NULL, repo);
2712 234035bc 2019-06-01 stsp goto done;
2713 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2714 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2715 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2716 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2717 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2718 1ee397ad 2019-07-12 stsp status, path2);
2719 234035bc 2019-06-01 stsp goto done;
2721 526a746f 2020-07-23 stsp if (S_ISLNK(mode2)) {
2722 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
2723 526a746f 2020-07-23 stsp ondisk_path, path2, sb.st_mode,
2724 526a746f 2020-07-23 stsp a->label_orig, blob2, a->commit_id2,
2725 526a746f 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
2727 526a746f 2020-07-23 stsp goto done;
2729 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
2730 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
2731 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
2732 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
2733 526a746f 2020-07-23 stsp a->progress_arg);
2735 526a746f 2020-07-23 stsp goto done;
2737 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2738 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
2739 054041d0 2020-06-24 stsp ondisk_path, blob2->id.sha1,
2740 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 0);
2742 234035bc 2019-06-01 stsp goto done;
2745 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
2746 234035bc 2019-06-01 stsp err = install_blob(a->worktree, ondisk_path, path2,
2747 960094b4 2020-07-23 stsp mode2, sb.st_mode, blob2, 0, 0, 0, repo,
2748 234035bc 2019-06-01 stsp a->progress_cb, a->progress_arg);
2750 234035bc 2019-06-01 stsp goto done;
2751 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, path2);
2753 234035bc 2019-06-01 stsp goto done;
2754 3969253a 2020-03-07 stsp err = got_fileindex_entry_update(ie, ondisk_path,
2755 3969253a 2020-03-07 stsp NULL, NULL, 1);
2756 3969253a 2020-03-07 stsp if (err) {
2757 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
2758 3969253a 2020-03-07 stsp goto done;
2760 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
2761 2b92fad7 2019-06-02 stsp if (err) {
2762 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
2763 2b92fad7 2019-06-02 stsp goto done;
2768 234035bc 2019-06-01 stsp free(ondisk_path);
2769 234035bc 2019-06-01 stsp return err;
2772 234035bc 2019-06-01 stsp struct check_merge_ok_arg {
2773 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2774 234035bc 2019-06-01 stsp struct got_repository *repo;
2777 234035bc 2019-06-01 stsp static const struct got_error *
2778 234035bc 2019-06-01 stsp check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2780 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
2781 234035bc 2019-06-01 stsp struct check_merge_ok_arg *a = arg;
2782 234035bc 2019-06-01 stsp unsigned char status;
2783 234035bc 2019-06-01 stsp struct stat sb;
2784 234035bc 2019-06-01 stsp char *ondisk_path;
2786 234035bc 2019-06-01 stsp /* Reject merges into a work tree with mixed base commits. */
2787 234035bc 2019-06-01 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2788 234035bc 2019-06-01 stsp SHA1_DIGEST_LENGTH))
2789 234035bc 2019-06-01 stsp return got_error(GOT_ERR_MIXED_COMMITS);
2791 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2793 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2795 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
2796 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2798 234035bc 2019-06-01 stsp return err;
2799 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
2800 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
2802 234035bc 2019-06-01 stsp return NULL;
2805 818c7501 2019-07-11 stsp static const struct got_error *
2806 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2807 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
2808 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
2809 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2810 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2812 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
2813 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2814 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2815 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
2816 f69721c3 2019-10-21 stsp char *label_orig = NULL;
2818 03415a1a 2019-06-02 stsp if (commit_id1) {
2819 f69721c3 2019-10-21 stsp char *id_str;
2821 03415a1a 2019-06-02 stsp err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2822 03415a1a 2019-06-02 stsp worktree->path_prefix);
2824 03415a1a 2019-06-02 stsp goto done;
2826 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
2828 03415a1a 2019-06-02 stsp goto done;
2830 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
2832 f69721c3 2019-10-21 stsp goto done;
2834 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
2835 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
2836 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
2837 f69721c3 2019-10-21 stsp free(id_str);
2838 f69721c3 2019-10-21 stsp goto done;
2840 f69721c3 2019-10-21 stsp free(id_str);
2843 234035bc 2019-06-01 stsp err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2844 234035bc 2019-06-01 stsp worktree->path_prefix);
2846 234035bc 2019-06-01 stsp goto done;
2848 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
2850 234035bc 2019-06-01 stsp goto done;
2852 234035bc 2019-06-01 stsp arg.worktree = worktree;
2853 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
2854 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
2855 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
2856 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
2857 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
2858 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
2859 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
2860 31b4484f 2019-07-27 stsp err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2861 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2862 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2863 af12c6b9 2019-06-04 stsp err = sync_err;
2865 234035bc 2019-06-01 stsp if (tree1)
2866 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
2867 234035bc 2019-06-01 stsp if (tree2)
2868 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
2869 f69721c3 2019-10-21 stsp free(label_orig);
2870 818c7501 2019-07-11 stsp return err;
2873 818c7501 2019-07-11 stsp const struct got_error *
2874 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
2875 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2876 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2877 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2879 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
2880 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
2881 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
2882 818c7501 2019-07-11 stsp struct check_merge_ok_arg mok_arg;
2884 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
2886 818c7501 2019-07-11 stsp return err;
2888 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2890 818c7501 2019-07-11 stsp goto done;
2892 818c7501 2019-07-11 stsp mok_arg.worktree = worktree;
2893 818c7501 2019-07-11 stsp mok_arg.repo = repo;
2894 818c7501 2019-07-11 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2895 818c7501 2019-07-11 stsp &mok_arg);
2897 818c7501 2019-07-11 stsp goto done;
2899 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2900 818c7501 2019-07-11 stsp commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2902 818c7501 2019-07-11 stsp if (fileindex)
2903 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
2904 818c7501 2019-07-11 stsp free(fileindex_path);
2905 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2906 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
2907 234035bc 2019-06-01 stsp err = unlockerr;
2908 234035bc 2019-06-01 stsp return err;
2911 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
2912 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
2913 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
2914 927df6b7 2019-02-10 stsp const char *status_path;
2915 927df6b7 2019-02-10 stsp size_t status_path_len;
2916 f8d1f275 2019-02-04 stsp struct got_repository *repo;
2917 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
2918 f8d1f275 2019-02-04 stsp void *status_arg;
2919 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2920 f8d1f275 2019-02-04 stsp void *cancel_arg;
2921 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
2922 6841da00 2019-08-08 stsp struct got_pathlist_head ignores;
2923 f2a9dc41 2019-12-13 tracey int report_unchanged;
2924 3143d852 2020-06-25 stsp int no_ignores;
2927 f8d1f275 2019-02-04 stsp static const struct got_error *
2928 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2929 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
2930 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
2931 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
2933 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
2934 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
2935 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
2936 927df6b7 2019-02-10 stsp struct stat sb;
2937 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
2938 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2939 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
2941 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
2943 98eaaa12 2019-08-03 stsp return err;
2945 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
2946 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
2947 98eaaa12 2019-08-03 stsp return NULL;
2949 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_blob(ie)) {
2950 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2951 98eaaa12 2019-08-03 stsp blob_idp = &blob_id;
2953 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
2954 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2955 98eaaa12 2019-08-03 stsp commit_idp = &commit_id;
2957 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
2958 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
2959 98eaaa12 2019-08-03 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2960 98eaaa12 2019-08-03 stsp SHA1_DIGEST_LENGTH);
2961 98eaaa12 2019-08-03 stsp staged_blob_idp = &staged_blob_id;
2964 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
2965 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
2968 927df6b7 2019-02-10 stsp static const struct got_error *
2969 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
2970 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
2972 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
2973 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
2974 f8d1f275 2019-02-04 stsp char *abspath;
2976 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2977 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2979 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
2980 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
2981 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2982 927df6b7 2019-02-10 stsp return NULL;
2984 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
2985 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2986 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
2987 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2989 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2990 f8d1f275 2019-02-04 stsp de->d_name) == -1)
2991 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2994 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
2995 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
2996 f8d1f275 2019-02-04 stsp free(abspath);
2997 9d31a1d8 2018-03-11 stsp return err;
3000 f8d1f275 2019-02-04 stsp static const struct got_error *
3001 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3003 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3004 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3005 2ec1f75b 2019-03-26 stsp unsigned char status;
3007 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3008 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3010 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3011 927df6b7 2019-02-10 stsp return NULL;
3013 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3014 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3015 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3016 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3018 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3019 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3020 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3024 6841da00 2019-08-08 stsp free_ignorelist(struct got_pathlist_head *ignorelist)
3026 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3028 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignorelist, entry)
3029 6841da00 2019-08-08 stsp free((char *)pe->path);
3030 6841da00 2019-08-08 stsp got_pathlist_free(ignorelist);
3034 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3036 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3038 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3039 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3040 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3041 6841da00 2019-08-08 stsp free((char *)pe->path);
3043 6841da00 2019-08-08 stsp got_pathlist_free(ignores);
3046 6841da00 2019-08-08 stsp static const struct got_error *
3047 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3049 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3050 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3051 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3052 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3053 6841da00 2019-08-08 stsp size_t linesize = 0;
3054 6841da00 2019-08-08 stsp ssize_t linelen;
3056 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3057 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3058 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3059 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3061 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3062 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3063 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3065 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3066 bd8de430 2019-10-04 stsp if (line[0] == '#')
3069 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3070 bd8de430 2019-10-04 stsp if (line[0] == '!')
3073 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3074 6841da00 2019-08-08 stsp line) == -1) {
3075 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3076 6841da00 2019-08-08 stsp goto done;
3078 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3080 6841da00 2019-08-08 stsp goto done;
3082 6841da00 2019-08-08 stsp if (ferror(f)) {
3083 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3084 6841da00 2019-08-08 stsp goto done;
3087 6841da00 2019-08-08 stsp dirpath = strdup(path);
3088 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3089 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3090 6841da00 2019-08-08 stsp goto done;
3092 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3094 6841da00 2019-08-08 stsp free(line);
3095 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3096 6841da00 2019-08-08 stsp free(dirpath);
3097 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3099 6841da00 2019-08-08 stsp return err;
3103 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3105 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3107 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3108 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3109 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3110 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3112 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3113 bd8de430 2019-10-04 stsp const char *p, *pattern = pi->path;
3115 bd8de430 2019-10-04 stsp if (strncmp(pattern, "**/", 3) != 0)
3117 bd8de430 2019-10-04 stsp pattern += 3;
3119 bd8de430 2019-10-04 stsp while (*p) {
3120 bd8de430 2019-10-04 stsp if (fnmatch(pattern, p,
3121 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3122 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3123 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3125 bd8de430 2019-10-04 stsp while (*p == '/')
3135 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3136 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3137 6841da00 2019-08-08 stsp * ignores backwards.
3139 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3140 6841da00 2019-08-08 stsp while (pe) {
3141 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3142 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3143 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3144 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3145 bd8de430 2019-10-04 stsp const char *pattern = pi->path;
3146 bd8de430 2019-10-04 stsp int flags = FNM_LEADING_DIR;
3147 bd8de430 2019-10-04 stsp if (strstr(pattern, "/**/") == NULL)
3148 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3149 bd8de430 2019-10-04 stsp if (fnmatch(pattern, path, flags))
3154 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3160 6841da00 2019-08-08 stsp static const struct got_error *
3161 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3162 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3164 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3165 6841da00 2019-08-08 stsp char *ignorespath;
3166 886cec17 2019-12-15 stsp int fd = -1;
3167 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3169 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3170 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3171 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3173 886cec17 2019-12-15 stsp if (dirfd != -1) {
3174 886cec17 2019-12-15 stsp fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3175 886cec17 2019-12-15 stsp if (fd == -1) {
3176 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3177 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3178 886cec17 2019-12-15 stsp ignorespath);
3180 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3181 886cec17 2019-12-15 stsp if (ignoresfile == NULL)
3182 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3183 886cec17 2019-12-15 stsp ignorespath);
3186 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3190 886cec17 2019-12-15 stsp ignoresfile = fopen(ignorespath, "r");
3191 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3192 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3193 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3194 886cec17 2019-12-15 stsp ignorespath);
3196 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3199 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3200 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3201 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3202 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3203 6841da00 2019-08-08 stsp free(ignorespath);
3204 6841da00 2019-08-08 stsp return err;
3207 f8d1f275 2019-02-04 stsp static const struct got_error *
3208 7f91a133 2019-12-13 stsp status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3210 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3211 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3212 f8d1f275 2019-02-04 stsp char *path = NULL;
3214 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3215 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3217 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3218 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3219 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3221 f8d1f275 2019-02-04 stsp path = de->d_name;
3224 3143d852 2020-06-25 stsp if (de->d_type != DT_DIR &&
3225 3143d852 2020-06-25 stsp got_path_is_child(path, a->status_path, a->status_path_len)
3226 6841da00 2019-08-08 stsp && !match_ignores(&a->ignores, path))
3227 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3228 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3229 f8d1f275 2019-02-04 stsp if (parent_path[0])
3230 f8d1f275 2019-02-04 stsp free(path);
3231 b72f483a 2019-02-05 stsp return err;
3234 347d1d3e 2019-07-12 stsp static const struct got_error *
3235 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3237 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3238 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3240 3143d852 2020-06-25 stsp if (a->no_ignores)
3241 3143d852 2020-06-25 stsp return NULL;
3243 3143d852 2020-06-25 stsp err = add_ignores(&a->ignores, a->worktree->root_path,
3244 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3246 3143d852 2020-06-25 stsp return err;
3248 3143d852 2020-06-25 stsp err = add_ignores(&a->ignores, a->worktree->root_path, path,
3249 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3251 3143d852 2020-06-25 stsp return err;
3254 3143d852 2020-06-25 stsp static const struct got_error *
3255 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3256 abb4604f 2019-07-27 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3257 f2a9dc41 2019-12-13 tracey void *status_arg, struct got_repository *repo, int report_unchanged)
3259 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3260 abb4604f 2019-07-27 stsp struct stat sb;
3262 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3264 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3265 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3267 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3268 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3269 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3270 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3271 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3272 abb4604f 2019-07-27 stsp return NULL;
3275 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3276 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3277 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3279 abb4604f 2019-07-27 stsp return NULL;
3282 3143d852 2020-06-25 stsp static const struct got_error *
3283 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3284 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3286 3143d852 2020-06-25 stsp const struct got_error *err;
3287 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3289 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3290 3143d852 2020-06-25 stsp ".cvsignore");
3292 3143d852 2020-06-25 stsp return err;
3294 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3295 3143d852 2020-06-25 stsp ".gitignore");
3297 3143d852 2020-06-25 stsp return err;
3299 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3300 3143d852 2020-06-25 stsp if (err) {
3301 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3302 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3303 3143d852 2020-06-25 stsp return err;
3305 3143d852 2020-06-25 stsp for (;;) {
3306 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3307 3143d852 2020-06-25 stsp ".cvsignore");
3310 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3311 3143d852 2020-06-25 stsp ".gitignore");
3314 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3315 3143d852 2020-06-25 stsp if (err) {
3316 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3317 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3320 b737c85a 2020-06-26 stsp free(parent_path);
3321 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3322 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3325 b737c85a 2020-06-26 stsp free(parent_path);
3326 b737c85a 2020-06-26 stsp free(next_parent_path);
3327 3143d852 2020-06-25 stsp return err;
3330 abb4604f 2019-07-27 stsp static const struct got_error *
3331 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3332 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3333 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3334 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3335 f2a9dc41 2019-12-13 tracey int report_unchanged)
3337 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3338 6fc93f37 2019-12-13 stsp int fd = -1;
3339 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3340 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3341 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3343 3143d852 2020-06-25 stsp TAILQ_INIT(&arg.ignores);
3345 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3346 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3347 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3349 6fc93f37 2019-12-13 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3350 6fc93f37 2019-12-13 stsp if (fd == -1) {
3351 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3352 00bb5ea0 2020-07-23 stsp errno != ELOOP)
3353 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
3355 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
3356 f2a9dc41 2019-12-13 tracey fileindex, status_cb, status_arg, repo,
3357 f2a9dc41 2019-12-13 tracey report_unchanged);
3359 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
3360 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
3361 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
3362 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
3363 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
3364 abb4604f 2019-07-27 stsp arg.worktree = worktree;
3365 abb4604f 2019-07-27 stsp arg.status_path = path;
3366 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
3367 abb4604f 2019-07-27 stsp arg.repo = repo;
3368 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
3369 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
3370 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
3371 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
3372 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
3373 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
3374 022fae89 2019-12-06 tracey if (!no_ignores) {
3375 3143d852 2020-06-25 stsp err = add_ignores_from_parent_paths(&arg.ignores,
3376 3143d852 2020-06-25 stsp worktree->root_path, path);
3378 3143d852 2020-06-25 stsp goto done;
3380 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
3381 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
3384 3143d852 2020-06-25 stsp free_ignores(&arg.ignores);
3385 6fc93f37 2019-12-13 stsp if (fd != -1 && close(fd) != 0 && err == NULL)
3386 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
3387 927df6b7 2019-02-10 stsp free(ondisk_path);
3388 347d1d3e 2019-07-12 stsp return err;
3391 347d1d3e 2019-07-12 stsp const struct got_error *
3392 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
3393 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
3394 72ea6654 2019-07-27 stsp got_worktree_status_cb status_cb, void *status_arg,
3395 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3397 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
3398 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
3399 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3400 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
3402 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3404 347d1d3e 2019-07-12 stsp return err;
3406 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
3407 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3408 f2a9dc41 2019-12-13 tracey status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3412 f8d1f275 2019-02-04 stsp free(fileindex_path);
3413 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
3414 f8d1f275 2019-02-04 stsp return err;
3417 6c7ab921 2019-03-18 stsp const struct got_error *
3418 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3419 6c7ab921 2019-03-18 stsp const char *arg)
3421 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
3422 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
3423 6c7ab921 2019-03-18 stsp size_t len;
3424 00bb5ea0 2020-07-23 stsp struct stat sb;
3426 6c7ab921 2019-03-18 stsp *wt_path = NULL;
3428 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
3429 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
3430 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
3432 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
3433 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3434 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
3435 00bb5ea0 2020-07-23 stsp goto done;
3438 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
3440 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
3441 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
3442 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
3443 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
3445 00bb5ea0 2020-07-23 stsp char *abspath = NULL;
3446 00bb5ea0 2020-07-23 stsp char canonpath[PATH_MAX];
3447 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
3448 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3449 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3450 00bb5ea0 2020-07-23 stsp goto done;
3454 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
3455 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
3457 d0710d08 2019-07-22 stsp goto done;
3458 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
3459 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3460 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
3461 00bb5ea0 2020-07-23 stsp goto done;
3464 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
3465 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3466 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3467 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
3468 00bb5ea0 2020-07-23 stsp goto done;
3470 00bb5ea0 2020-07-23 stsp if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
3471 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3472 00bb5ea0 2020-07-23 stsp goto done;
3477 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
3478 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
3479 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3480 6c7ab921 2019-03-18 stsp goto done;
3483 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3484 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
3485 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
3487 f6d88e1a 2019-05-29 stsp goto done;
3489 f6d88e1a 2019-05-29 stsp path = strdup("");
3490 f6d88e1a 2019-05-29 stsp if (path == NULL) {
3491 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
3492 f6d88e1a 2019-05-29 stsp goto done;
3496 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
3497 6c7ab921 2019-03-18 stsp len = strlen(path);
3498 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
3499 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
3503 6c7ab921 2019-03-18 stsp free(resolved);
3504 d0710d08 2019-07-22 stsp free(cwd);
3505 6c7ab921 2019-03-18 stsp if (err == NULL)
3506 6c7ab921 2019-03-18 stsp *wt_path = path;
3508 6c7ab921 2019-03-18 stsp free(path);
3509 d00136be 2019-03-26 stsp return err;
3512 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
3513 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
3514 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
3515 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
3516 4e68cba3 2019-11-23 stsp void *progress_arg;
3517 4e68cba3 2019-11-23 stsp struct got_repository *repo;
3520 4e68cba3 2019-11-23 stsp static const struct got_error *
3521 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3522 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
3523 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3524 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3526 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
3527 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
3528 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
3529 6d022e97 2019-08-04 stsp struct stat sb;
3530 dbb83fbd 2019-12-12 stsp char *ondisk_path;
3532 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3533 dbb83fbd 2019-12-12 stsp relpath) == -1)
3534 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
3536 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3538 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3539 12463d8b 2019-12-13 stsp de_name, a->repo);
3541 dbb83fbd 2019-12-12 stsp goto done;
3542 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
3543 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
3544 dbb83fbd 2019-12-12 stsp goto done;
3545 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3547 dbb83fbd 2019-12-12 stsp goto done;
3550 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
3551 dbb83fbd 2019-12-12 stsp err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3552 dbb83fbd 2019-12-12 stsp goto done;
3555 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
3557 4e68cba3 2019-11-23 stsp goto done;
3558 3969253a 2020-03-07 stsp err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3559 3969253a 2020-03-07 stsp if (err) {
3560 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3561 3969253a 2020-03-07 stsp goto done;
3563 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3564 07f5b47a 2019-06-02 stsp if (err) {
3565 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
3566 4e68cba3 2019-11-23 stsp goto done;
3569 dbb83fbd 2019-12-12 stsp free(ondisk_path);
3571 dbb83fbd 2019-12-12 stsp return err;
3572 dbb83fbd 2019-12-12 stsp if (status == GOT_STATUS_ADD)
3573 dbb83fbd 2019-12-12 stsp return NULL;
3574 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3577 d00136be 2019-03-26 stsp const struct got_error *
3578 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
3579 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
3580 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3581 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
3583 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
3584 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
3585 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
3586 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
3587 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
3589 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
3591 d00136be 2019-03-26 stsp return err;
3593 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3595 d00136be 2019-03-26 stsp goto done;
3597 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
3598 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
3599 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
3600 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
3601 4e68cba3 2019-11-23 stsp saa.repo = repo;
3603 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
3604 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3605 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3609 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3610 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3611 af12c6b9 2019-06-04 stsp err = sync_err;
3613 fb399478 2019-07-12 stsp free(fileindex_path);
3614 d00136be 2019-03-26 stsp if (fileindex)
3615 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
3616 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3617 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
3618 d00136be 2019-03-26 stsp err = unlockerr;
3619 6c7ab921 2019-03-18 stsp return err;
3622 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
3623 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
3624 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
3625 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
3626 f2a9dc41 2019-12-13 tracey void *progress_arg;
3627 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
3628 f2a9dc41 2019-12-13 tracey int delete_local_mods;
3629 70e3e7f5 2019-12-13 tracey int keep_on_disk;
3632 f2a9dc41 2019-12-13 tracey static const struct got_error *
3633 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
3634 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
3635 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3636 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
3638 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
3639 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
3640 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
3641 17ed4618 2019-06-02 stsp struct stat sb;
3642 15341bfd 2020-03-05 tracey char *ondisk_path, *parent = NULL;
3644 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3645 17ed4618 2019-06-02 stsp if (ie == NULL)
3646 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
3648 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
3649 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
3650 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
3651 9acbc4fa 2019-08-03 stsp return NULL;
3652 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3655 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3656 f2a9dc41 2019-12-13 tracey relpath) == -1)
3657 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
3659 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3662 f2a9dc41 2019-12-13 tracey goto done;
3664 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
3665 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
3666 f2a9dc41 2019-12-13 tracey goto done;
3667 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3668 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3669 f2a9dc41 2019-12-13 tracey goto done;
3671 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
3672 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
3673 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3674 f2a9dc41 2019-12-13 tracey goto done;
3678 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3679 12463d8b 2019-12-13 stsp if (dirfd != -1) {
3680 12463d8b 2019-12-13 stsp if (unlinkat(dirfd, de_name, 0) != 0) {
3681 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
3682 12463d8b 2019-12-13 stsp ondisk_path);
3683 12463d8b 2019-12-13 stsp goto done;
3685 12463d8b 2019-12-13 stsp } else if (unlink(ondisk_path) != 0) {
3686 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
3687 12463d8b 2019-12-13 stsp goto done;
3690 15341bfd 2020-03-05 tracey parent = dirname(ondisk_path);
3692 15341bfd 2020-03-05 tracey if (parent == NULL) {
3693 15341bfd 2020-03-05 tracey err = got_error_from_errno2("dirname", ondisk_path);
3694 15341bfd 2020-03-05 tracey goto done;
3696 15341bfd 2020-03-05 tracey while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3697 15341bfd 2020-03-05 tracey if (rmdir(parent) == -1) {
3698 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
3699 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
3700 15341bfd 2020-03-05 tracey parent);
3703 15341bfd 2020-03-05 tracey parent = dirname(parent);
3704 15341bfd 2020-03-05 tracey if (parent == NULL) {
3705 15341bfd 2020-03-05 tracey err = got_error_from_errno2("dirname", parent);
3706 15341bfd 2020-03-05 tracey goto done;
3711 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3713 f2a9dc41 2019-12-13 tracey free(ondisk_path);
3714 f2a9dc41 2019-12-13 tracey if (err)
3715 f2a9dc41 2019-12-13 tracey return err;
3716 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
3717 f2a9dc41 2019-12-13 tracey return NULL;
3718 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3719 f2a9dc41 2019-12-13 tracey staged_status, relpath);
3722 2ec1f75b 2019-03-26 stsp const struct got_error *
3723 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
3724 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
3725 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
3726 70e3e7f5 2019-12-13 tracey struct got_repository *repo, int keep_on_disk)
3728 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
3729 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
3730 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
3731 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
3732 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
3734 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
3736 2ec1f75b 2019-03-26 stsp return err;
3738 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3740 2ec1f75b 2019-03-26 stsp goto done;
3742 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
3743 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
3744 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
3745 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
3746 f2a9dc41 2019-12-13 tracey sda.repo = repo;
3747 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
3748 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
3750 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
3751 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
3752 f2a9dc41 2019-12-13 tracey schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3756 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3757 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3758 af12c6b9 2019-06-04 stsp err = sync_err;
3760 fb399478 2019-07-12 stsp free(fileindex_path);
3761 2ec1f75b 2019-03-26 stsp if (fileindex)
3762 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
3763 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3764 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
3765 2ec1f75b 2019-03-26 stsp err = unlockerr;
3766 33aa809d 2019-08-08 stsp return err;
3769 33aa809d 2019-08-08 stsp static const struct got_error *
3770 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3772 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
3773 33aa809d 2019-08-08 stsp char *line = NULL;
3774 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
3775 33aa809d 2019-08-08 stsp ssize_t linelen;
3777 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
3778 33aa809d 2019-08-08 stsp if (linelen == -1) {
3779 33aa809d 2019-08-08 stsp if (ferror(infile)) {
3780 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
3781 33aa809d 2019-08-08 stsp goto done;
3783 33aa809d 2019-08-08 stsp return NULL;
3785 33aa809d 2019-08-08 stsp if (outfile) {
3786 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
3787 33aa809d 2019-08-08 stsp if (n != linelen) {
3788 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
3789 33aa809d 2019-08-08 stsp goto done;
3792 33aa809d 2019-08-08 stsp if (rejectfile) {
3793 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
3794 33aa809d 2019-08-08 stsp if (n != linelen)
3795 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
3798 33aa809d 2019-08-08 stsp free(line);
3799 2ec1f75b 2019-03-26 stsp return err;
3802 33aa809d 2019-08-08 stsp static const struct got_error *
3803 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
3805 33aa809d 2019-08-08 stsp char *line = NULL;
3806 33aa809d 2019-08-08 stsp size_t linesize = 0;
3807 33aa809d 2019-08-08 stsp ssize_t linelen;
3809 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
3810 500467ff 2019-09-25 hiltjo if (linelen == -1) {
3811 500467ff 2019-09-25 hiltjo if (ferror(f))
3812 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
3813 500467ff 2019-09-25 hiltjo return NULL;
3815 33aa809d 2019-08-08 stsp free(line);
3816 33aa809d 2019-08-08 stsp return NULL;
3819 33aa809d 2019-08-08 stsp static const struct got_error *
3820 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3821 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
3822 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
3824 33aa809d 2019-08-08 stsp const struct got_error *err;
3826 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
3827 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
3828 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
3830 33aa809d 2019-08-08 stsp return err;
3831 33aa809d 2019-08-08 stsp (*line_cur1)++;
3833 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
3834 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
3835 33aa809d 2019-08-08 stsp if (rejectfile)
3836 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
3838 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
3840 33aa809d 2019-08-08 stsp return err;
3841 33aa809d 2019-08-08 stsp (*line_cur2)++;
3843 33aa809d 2019-08-08 stsp /* Copy patched lines. */
3844 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
3845 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
3847 33aa809d 2019-08-08 stsp return err;
3848 33aa809d 2019-08-08 stsp (*line_cur2)++;
3850 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
3851 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
3852 33aa809d 2019-08-08 stsp if (rejectfile)
3853 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
3855 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
3857 33aa809d 2019-08-08 stsp return err;
3858 33aa809d 2019-08-08 stsp (*line_cur1)++;
3861 f1e81a05 2019-08-10 stsp return NULL;
3864 f1e81a05 2019-08-10 stsp static const struct got_error *
3865 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3866 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
3868 f1e81a05 2019-08-10 stsp const struct got_error *err;
3870 f1e81a05 2019-08-10 stsp if (outfile) {
3871 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
3872 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
3873 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
3875 f1e81a05 2019-08-10 stsp return err;
3876 f1e81a05 2019-08-10 stsp (*line_cur1)++;
3879 f1e81a05 2019-08-10 stsp if (rejectfile) {
3880 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
3881 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
3882 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
3884 f1e81a05 2019-08-10 stsp return err;
3885 f1e81a05 2019-08-10 stsp (*line_cur2)++;
3889 33aa809d 2019-08-08 stsp return NULL;
3892 33aa809d 2019-08-08 stsp static const struct got_error *
3893 33aa809d 2019-08-08 stsp apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3894 33aa809d 2019-08-08 stsp int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3895 33aa809d 2019-08-08 stsp int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3896 33aa809d 2019-08-08 stsp int *line_cur2, FILE *outfile, FILE *rejectfile,
3897 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
3899 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
3900 33aa809d 2019-08-08 stsp int start_old = change->cv.a;
3901 33aa809d 2019-08-08 stsp int end_old = change->cv.b;
3902 33aa809d 2019-08-08 stsp int start_new = change->cv.c;
3903 33aa809d 2019-08-08 stsp int end_new = change->cv.d;
3904 33aa809d 2019-08-08 stsp long pos1, pos2;
3905 33aa809d 2019-08-08 stsp FILE *hunkfile;
3907 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
3909 33aa809d 2019-08-08 stsp hunkfile = got_opentemp();
3910 33aa809d 2019-08-08 stsp if (hunkfile == NULL)
3911 33aa809d 2019-08-08 stsp return got_error_from_errno("got_opentemp");
3913 33aa809d 2019-08-08 stsp pos1 = ftell(f1);
3914 33aa809d 2019-08-08 stsp pos2 = ftell(f2);
3916 33aa809d 2019-08-08 stsp /* XXX TODO needs error checking */
3917 33aa809d 2019-08-08 stsp got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
3919 33aa809d 2019-08-08 stsp if (fseek(f1, pos1, SEEK_SET) == -1) {
3920 33aa809d 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
3921 33aa809d 2019-08-08 stsp goto done;
3923 33aa809d 2019-08-08 stsp if (fseek(f2, pos2, SEEK_SET) == -1) {
3924 33aa809d 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
3925 33aa809d 2019-08-08 stsp goto done;
3927 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
3928 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
3929 33aa809d 2019-08-08 stsp goto done;
3932 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
3933 33aa809d 2019-08-08 stsp hunkfile, n, nchanges);
3935 33aa809d 2019-08-08 stsp goto done;
3937 33aa809d 2019-08-08 stsp switch (*choice) {
3938 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
3939 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3940 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
3942 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
3943 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3944 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
3946 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
3949 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
3953 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3954 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
3955 33aa809d 2019-08-08 stsp return err;
3958 1f1abb7e 2019-08-08 stsp struct revert_file_args {
3959 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
3960 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
3961 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
3962 1f1abb7e 2019-08-08 stsp void *progress_arg;
3963 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
3964 33aa809d 2019-08-08 stsp void *patch_arg;
3965 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
3968 e20a8b6f 2019-06-04 stsp static const struct got_error *
3969 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
3970 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
3971 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
3972 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
3973 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
3975 33aa809d 2019-08-08 stsp const struct got_error *err;
3976 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
3977 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3978 1ebedb77 2019-10-19 stsp int fd2 = -1;
3979 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
3980 33aa809d 2019-08-08 stsp struct stat sb1, sb2;
3981 33aa809d 2019-08-08 stsp struct got_diff_changes *changes = NULL;
3982 33aa809d 2019-08-08 stsp struct got_diff_state *ds = NULL;
3983 33aa809d 2019-08-08 stsp struct got_diff_args *args = NULL;
3984 33aa809d 2019-08-08 stsp struct got_diff_change *change;
3985 33aa809d 2019-08-08 stsp int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3986 33aa809d 2019-08-08 stsp int n = 0;
3988 33aa809d 2019-08-08 stsp *path_outfile = NULL;
3990 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
3992 33aa809d 2019-08-08 stsp return err;
3994 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
3995 12463d8b 2019-12-13 stsp fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
3996 12463d8b 2019-12-13 stsp if (fd2 == -1) {
3997 12463d8b 2019-12-13 stsp err = got_error_from_errno2("openat", path2);
3998 12463d8b 2019-12-13 stsp goto done;
4001 12463d8b 2019-12-13 stsp fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4002 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4003 12463d8b 2019-12-13 stsp err = got_error_from_errno2("open", path2);
4004 12463d8b 2019-12-13 stsp goto done;
4007 1ebedb77 2019-10-19 stsp if (fstat(fd2, &sb2) == -1) {
4008 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("fstat", path2);
4009 1ebedb77 2019-10-19 stsp goto done;
4012 1ebedb77 2019-10-19 stsp f2 = fdopen(fd2, "r");
4013 33aa809d 2019-08-08 stsp if (f2 == NULL) {
4014 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", path2);
4015 33aa809d 2019-08-08 stsp goto done;
4019 33aa809d 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4021 33aa809d 2019-08-08 stsp goto done;
4023 e635744c 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4025 33aa809d 2019-08-08 stsp goto done;
4027 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4029 33aa809d 2019-08-08 stsp goto done;
4031 33aa809d 2019-08-08 stsp if (stat(path1, &sb1) == -1) {
4032 33aa809d 2019-08-08 stsp err = got_error_from_errno2("stat", path1);
4033 33aa809d 2019-08-08 stsp goto done;
4036 33aa809d 2019-08-08 stsp err = got_diff_files(&changes, &ds, &args, &diff_flags,
4037 33aa809d 2019-08-08 stsp f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
4039 33aa809d 2019-08-08 stsp goto done;
4041 e635744c 2019-08-08 stsp err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4043 33aa809d 2019-08-08 stsp goto done;
4045 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4046 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4047 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4048 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4049 33aa809d 2019-08-08 stsp SIMPLEQ_FOREACH(change, &changes->entries, entry) {
4050 33aa809d 2019-08-08 stsp int choice;
4051 33aa809d 2019-08-08 stsp err = apply_or_reject_change(&choice, change, ++n,
4052 33aa809d 2019-08-08 stsp changes->nchanges, ds, args, diff_flags, relpath,
4053 33aa809d 2019-08-08 stsp f1, f2, &line_cur1, &line_cur2,
4054 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4055 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4056 e635744c 2019-08-08 stsp patch_cb, patch_arg);
4058 33aa809d 2019-08-08 stsp goto done;
4059 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4060 33aa809d 2019-08-08 stsp have_content = 1;
4061 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4064 1ebedb77 2019-10-19 stsp if (have_content) {
4065 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4066 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4067 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4069 1ebedb77 2019-10-19 stsp goto done;
4071 1ebedb77 2019-10-19 stsp if (chmod(*path_outfile, sb2.st_mode) == -1) {
4072 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("chmod", path2);
4073 1ebedb77 2019-10-19 stsp goto done;
4077 33aa809d 2019-08-08 stsp free(id_str);
4079 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4080 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4081 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4082 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4083 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4084 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4085 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4086 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4087 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4088 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4089 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4090 33aa809d 2019-08-08 stsp if (err || !have_content) {
4091 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4092 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4093 33aa809d 2019-08-08 stsp free(*path_outfile);
4094 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4096 33aa809d 2019-08-08 stsp free(args);
4098 33aa809d 2019-08-08 stsp got_diff_state_free(ds);
4101 33aa809d 2019-08-08 stsp if (changes)
4102 33aa809d 2019-08-08 stsp got_diff_free_changes(changes);
4103 33aa809d 2019-08-08 stsp free(path1);
4104 33aa809d 2019-08-08 stsp return err;
4107 33aa809d 2019-08-08 stsp static const struct got_error *
4108 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4109 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4110 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4111 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4113 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4114 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4115 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4116 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4117 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4118 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4119 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4120 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4121 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4122 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
4124 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
4125 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
4126 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
4127 d3bcc3d1 2019-08-08 stsp return NULL;
4129 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
4130 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
4131 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
4133 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4134 65084dad 2019-08-08 stsp if (ie == NULL)
4135 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4137 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
4138 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
4139 a129376b 2019-03-28 stsp if (err) {
4140 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
4141 a129376b 2019-03-28 stsp goto done;
4142 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
4143 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
4144 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
4145 e20a8b6f 2019-06-04 stsp goto done;
4148 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
4149 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
4150 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4151 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4152 a129376b 2019-03-28 stsp goto done;
4155 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
4156 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
4157 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4158 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4159 a129376b 2019-03-28 stsp goto done;
4162 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
4163 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
4164 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4165 a129376b 2019-03-28 stsp goto done;
4170 1f1abb7e 2019-08-08 stsp err = got_object_id_by_path(&tree_id, a->repo,
4171 1f1abb7e 2019-08-08 stsp a->worktree->base_commit_id, tree_path);
4172 a9fa2909 2019-07-27 stsp if (err) {
4173 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4174 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
4175 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
4176 a9fa2909 2019-07-27 stsp goto done;
4178 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
4180 a9fa2909 2019-07-27 stsp goto done;
4182 a9fa2909 2019-07-27 stsp te_name = basename(ie->path);
4183 a9fa2909 2019-07-27 stsp if (te_name == NULL) {
4184 a9fa2909 2019-07-27 stsp err = got_error_from_errno2("basename", ie->path);
4185 a9fa2909 2019-07-27 stsp goto done;
4188 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
4189 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
4190 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
4191 a9fa2909 2019-07-27 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
4192 a9fa2909 2019-07-27 stsp goto done;
4196 a129376b 2019-03-28 stsp switch (status) {
4197 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
4198 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4199 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4200 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4201 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4203 33aa809d 2019-08-08 stsp goto done;
4204 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4207 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4208 1f1abb7e 2019-08-08 stsp ie->path);
4210 1ee397ad 2019-07-12 stsp goto done;
4211 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
4213 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
4214 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4215 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4216 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4217 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4219 33aa809d 2019-08-08 stsp goto done;
4220 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4223 33aa809d 2019-08-08 stsp /* fall through */
4224 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
4225 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
4226 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
4227 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
4228 e20a8b6f 2019-06-04 stsp struct got_object_id id;
4229 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4230 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
4231 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1,
4232 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4234 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1,
4235 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4236 1f1abb7e 2019-08-08 stsp err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4238 65084dad 2019-08-08 stsp goto done;
4240 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4241 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
4242 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
4243 a129376b 2019-03-28 stsp goto done;
4246 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4247 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
4248 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
4249 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
4250 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
4251 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
4253 33aa809d 2019-08-08 stsp if (rename(path_content, ondisk_path) == -1) {
4254 33aa809d 2019-08-08 stsp err = got_error_from_errno3("rename",
4255 33aa809d 2019-08-08 stsp path_content, ondisk_path);
4256 33aa809d 2019-08-08 stsp goto done;
4259 33aa809d 2019-08-08 stsp err = install_blob(a->worktree, ondisk_path, ie->path,
4260 33aa809d 2019-08-08 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
4261 bd6aa359 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob, 0, 1, 0,
4262 33aa809d 2019-08-08 stsp a->repo, a->progress_cb, a->progress_arg);
4264 a129376b 2019-03-28 stsp goto done;
4265 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
4266 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
4267 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
4268 054041d0 2020-06-24 stsp ondisk_path, blob->id.sha1,
4269 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
4271 33aa809d 2019-08-08 stsp goto done;
4280 1f1abb7e 2019-08-08 stsp free(ondisk_path);
4281 33aa809d 2019-08-08 stsp free(path_content);
4282 e20a8b6f 2019-06-04 stsp free(parent_path);
4283 a129376b 2019-03-28 stsp free(tree_path);
4285 a129376b 2019-03-28 stsp got_object_blob_close(blob);
4287 a129376b 2019-03-28 stsp got_object_tree_close(tree);
4288 a129376b 2019-03-28 stsp free(tree_id);
4289 e20a8b6f 2019-06-04 stsp return err;
4292 e20a8b6f 2019-06-04 stsp const struct got_error *
4293 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
4294 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
4295 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4296 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
4297 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
4299 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
4300 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
4301 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
4302 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
4303 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
4304 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
4306 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
4308 e20a8b6f 2019-06-04 stsp return err;
4310 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4312 e20a8b6f 2019-06-04 stsp goto done;
4314 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
4315 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
4316 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
4317 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
4318 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
4319 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
4320 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
4321 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
4322 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4323 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
4327 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4328 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
4329 e20a8b6f 2019-06-04 stsp err = sync_err;
4331 fb399478 2019-07-12 stsp free(fileindex_path);
4332 a129376b 2019-03-28 stsp if (fileindex)
4333 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
4334 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4335 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
4336 a129376b 2019-03-28 stsp err = unlockerr;
4337 c4296144 2019-05-09 stsp return err;
4340 cf066bf8 2019-05-09 stsp static void
4341 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
4343 24519714 2019-05-09 stsp free(ct->path);
4344 44d03001 2019-05-09 stsp free(ct->in_repo_path);
4345 768aea60 2019-05-09 stsp free(ct->ondisk_path);
4346 e75eb4da 2019-05-10 stsp free(ct->blob_id);
4347 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
4348 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
4349 b416585c 2019-05-13 stsp free(ct->base_commit_id);
4353 ed175427 2019-05-09 stsp struct collect_commitables_arg {
4354 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
4355 24519714 2019-05-09 stsp struct got_repository *repo;
4356 24519714 2019-05-09 stsp struct got_worktree *worktree;
4357 5f8a88c6 2019-08-03 stsp int have_staged_files;
4360 c4296144 2019-05-09 stsp static const struct got_error *
4361 88d0e355 2019-08-03 stsp collect_commitables(void *arg, unsigned char status,
4362 88d0e355 2019-08-03 stsp unsigned char staged_status, const char *relpath,
4363 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4364 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4366 ed175427 2019-05-09 stsp struct collect_commitables_arg *a = arg;
4367 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
4368 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
4369 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
4370 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
4371 768aea60 2019-05-09 stsp struct stat sb;
4373 5f8a88c6 2019-08-03 stsp if (a->have_staged_files) {
4374 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
4375 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
4376 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
4377 5f8a88c6 2019-08-03 stsp return NULL;
4379 5f8a88c6 2019-08-03 stsp if (status == GOT_STATUS_CONFLICT)
4380 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
4382 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
4383 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
4384 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
4385 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_DELETE)
4386 5f8a88c6 2019-08-03 stsp return NULL;
4389 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
4390 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4391 036813ee 2019-05-09 stsp goto done;
4393 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
4394 036813ee 2019-05-09 stsp parent_path = strdup("");
4395 69960a46 2019-05-09 stsp if (parent_path == NULL)
4396 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
4398 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
4400 69960a46 2019-05-09 stsp return err;
4403 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
4404 cf066bf8 2019-05-09 stsp if (ct == NULL) {
4405 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4406 c4296144 2019-05-09 stsp goto done;
4409 768aea60 2019-05-09 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4410 768aea60 2019-05-09 stsp relpath) == -1) {
4411 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4412 768aea60 2019-05-09 stsp goto done;
4414 5f8a88c6 2019-08-03 stsp if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
4415 768aea60 2019-05-09 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
4417 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4418 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
4419 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
4420 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
4421 12463d8b 2019-12-13 stsp ct->ondisk_path);
4422 12463d8b 2019-12-13 stsp goto done;
4424 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
4425 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
4426 768aea60 2019-05-09 stsp goto done;
4428 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
4431 44d03001 2019-05-09 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4432 44d03001 2019-05-09 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4433 44d03001 2019-05-09 stsp relpath) == -1) {
4434 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4435 44d03001 2019-05-09 stsp goto done;
4438 cf066bf8 2019-05-09 stsp ct->status = status;
4439 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
4440 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
4441 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
4442 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
4443 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
4444 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
4445 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4446 b416585c 2019-05-13 stsp goto done;
4448 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
4449 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
4450 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
4451 f0b75401 2019-08-03 stsp goto done;
4454 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
4455 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
4456 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4457 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
4458 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4459 036813ee 2019-05-09 stsp goto done;
4462 24519714 2019-05-09 stsp ct->path = strdup(path);
4463 24519714 2019-05-09 stsp if (ct->path == NULL) {
4464 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4465 24519714 2019-05-09 stsp goto done;
4467 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4469 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
4470 ed175427 2019-05-09 stsp free_commitable(ct);
4471 24519714 2019-05-09 stsp free(parent_path);
4472 036813ee 2019-05-09 stsp free(path);
4473 a129376b 2019-03-28 stsp return err;
4476 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
4477 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
4478 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4479 036813ee 2019-05-09 stsp struct got_repository *);
4481 ed175427 2019-05-09 stsp static const struct got_error *
4482 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4483 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
4484 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
4485 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4486 afa376bf 2019-05-09 stsp struct got_repository *repo)
4488 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
4489 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
4490 036813ee 2019-05-09 stsp char *subpath;
4492 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
4493 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4494 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4496 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
4498 ed175427 2019-05-09 stsp return err;
4500 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
4501 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
4502 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
4503 036813ee 2019-05-09 stsp free(subpath);
4504 036813ee 2019-05-09 stsp return err;
4507 036813ee 2019-05-09 stsp static const struct got_error *
4508 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4510 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4511 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
4513 036813ee 2019-05-09 stsp *match = 0;
4515 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
4516 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
4517 0f63689d 2019-05-10 stsp return NULL;
4520 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4522 0f63689d 2019-05-10 stsp return err;
4523 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
4524 036813ee 2019-05-09 stsp free(ct_parent_path);
4525 036813ee 2019-05-09 stsp return err;
4528 768aea60 2019-05-09 stsp static mode_t
4529 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
4531 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
4532 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
4534 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4537 036813ee 2019-05-09 stsp static const struct got_error *
4538 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4539 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
4541 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4543 036813ee 2019-05-09 stsp *new_te = NULL;
4545 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
4547 036813ee 2019-05-09 stsp goto done;
4549 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
4551 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
4552 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
4553 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
4555 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4557 036813ee 2019-05-09 stsp if (err && *new_te) {
4558 56e0773d 2019-11-28 stsp free(*new_te);
4559 036813ee 2019-05-09 stsp *new_te = NULL;
4561 036813ee 2019-05-09 stsp return err;
4564 036813ee 2019-05-09 stsp static const struct got_error *
4565 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4566 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
4568 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4569 036813ee 2019-05-09 stsp char *ct_name;
4571 036813ee 2019-05-09 stsp *new_te = NULL;
4573 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
4574 036813ee 2019-05-09 stsp if (*new_te == NULL)
4575 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
4577 036813ee 2019-05-09 stsp ct_name = basename(ct->path);
4578 036813ee 2019-05-09 stsp if (ct_name == NULL) {
4579 638f9024 2019-05-13 stsp err = got_error_from_errno2("basename", ct->path);
4580 036813ee 2019-05-09 stsp goto done;
4582 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4583 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
4584 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
4585 036813ee 2019-05-09 stsp goto done;
4588 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
4590 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
4591 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
4592 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
4594 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4596 036813ee 2019-05-09 stsp if (err && *new_te) {
4597 56e0773d 2019-11-28 stsp free(*new_te);
4598 036813ee 2019-05-09 stsp *new_te = NULL;
4600 036813ee 2019-05-09 stsp return err;
4603 036813ee 2019-05-09 stsp static const struct got_error *
4604 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
4605 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
4607 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4608 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
4610 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4612 036813ee 2019-05-09 stsp return err;
4613 036813ee 2019-05-09 stsp if (new_pe == NULL)
4614 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
4615 036813ee 2019-05-09 stsp return NULL;
4618 afa376bf 2019-05-09 stsp static const struct got_error *
4619 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
4620 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
4622 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
4623 5f8a88c6 2019-08-03 stsp unsigned char status;
4625 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
4626 afa376bf 2019-05-09 stsp ct_path++;
4628 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4629 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
4631 5f8a88c6 2019-08-03 stsp status = ct->status;
4633 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4634 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4637 036813ee 2019-05-09 stsp static const struct got_error *
4638 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
4639 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4641 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
4642 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
4643 44d03001 2019-05-09 stsp char *te_path;
4645 44d03001 2019-05-09 stsp *modified = 0;
4647 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
4648 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
4649 44d03001 2019-05-09 stsp te->name) == -1)
4650 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4652 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
4653 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4654 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
4655 44d03001 2019-05-09 stsp strlen(te_path));
4656 44d03001 2019-05-09 stsp if (*modified)
4660 44d03001 2019-05-09 stsp free(te_path);
4661 44d03001 2019-05-09 stsp return err;
4664 44d03001 2019-05-09 stsp static const struct got_error *
4665 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
4666 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
4667 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
4669 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4670 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
4672 036813ee 2019-05-09 stsp *ctp = NULL;
4674 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
4675 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4676 036813ee 2019-05-09 stsp char *ct_name = NULL;
4677 036813ee 2019-05-09 stsp int path_matches;
4679 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4680 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
4681 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
4682 5f8a88c6 2019-08-03 stsp ct->status != GOT_STATUS_DELETE)
4685 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
4686 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
4690 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4693 036813ee 2019-05-09 stsp err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4695 036813ee 2019-05-09 stsp return err;
4696 036813ee 2019-05-09 stsp if (!path_matches)
4699 036813ee 2019-05-09 stsp ct_name = basename(pe->path);
4700 036813ee 2019-05-09 stsp if (ct_name == NULL)
4701 638f9024 2019-05-13 stsp return got_error_from_errno2("basename", pe->path);
4703 036813ee 2019-05-09 stsp if (strcmp(te->name, ct_name) != 0)
4706 036813ee 2019-05-09 stsp *ctp = ct;
4710 2b496619 2019-07-10 stsp return err;
4713 2b496619 2019-07-10 stsp static const struct got_error *
4714 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4715 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
4716 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
4717 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
4718 2b496619 2019-07-10 stsp struct got_repository *repo)
4720 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
4721 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
4722 2b496619 2019-07-10 stsp char *subtree_path;
4723 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
4724 ba580f68 2020-03-22 stsp int nentries;
4726 2b496619 2019-07-10 stsp *new_tep = NULL;
4728 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4729 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
4730 2b496619 2019-07-10 stsp child_path) == -1)
4731 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
4733 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
4734 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
4735 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
4736 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
4738 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4739 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
4740 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
4741 2b496619 2019-07-10 stsp goto done;
4743 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
4744 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
4745 2b496619 2019-07-10 stsp if (err) {
4746 56e0773d 2019-11-28 stsp free(new_te);
4747 2b496619 2019-07-10 stsp goto done;
4749 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
4752 2b496619 2019-07-10 stsp free(subtree_path);
4753 2b496619 2019-07-10 stsp if (err == NULL)
4754 2b496619 2019-07-10 stsp *new_tep = new_te;
4755 036813ee 2019-05-09 stsp return err;
4758 036813ee 2019-05-09 stsp static const struct got_error *
4759 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
4760 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
4761 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
4762 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4763 036813ee 2019-05-09 stsp struct got_repository *repo)
4765 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4766 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
4767 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
4768 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
4770 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
4771 ba580f68 2020-03-22 stsp *nentries = 0;
4773 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
4774 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
4775 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4776 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
4778 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
4779 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
4780 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
4783 036813ee 2019-05-09 stsp if (!got_path_is_child(pe->path, path_base_tree,
4784 2f51b5b3 2019-05-09 stsp strlen(path_base_tree)))
4787 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
4788 036813ee 2019-05-09 stsp pe->path);
4790 036813ee 2019-05-09 stsp goto done;
4792 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
4793 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
4794 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
4796 036813ee 2019-05-09 stsp goto done;
4797 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
4799 afa376bf 2019-05-09 stsp goto done;
4800 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
4801 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
4803 2f51b5b3 2019-05-09 stsp goto done;
4804 ba580f68 2020-03-22 stsp (*nentries)++;
4806 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
4807 2b496619 2019-07-10 stsp if (base_tree == NULL ||
4808 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
4809 2b496619 2019-07-10 stsp == NULL) {
4810 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
4811 2b496619 2019-07-10 stsp child_path, path_base_tree,
4812 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
4815 2b496619 2019-07-10 stsp goto done;
4816 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
4818 2b496619 2019-07-10 stsp goto done;
4819 ba580f68 2020-03-22 stsp (*nentries)++;
4824 2f51b5b3 2019-05-09 stsp if (base_tree) {
4825 56e0773d 2019-11-28 stsp int i, nbase_entries;
4826 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
4827 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
4828 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
4829 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
4831 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
4832 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
4833 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
4834 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
4836 63c5ca5d 2019-08-24 stsp goto done;
4837 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
4839 63c5ca5d 2019-08-24 stsp goto done;
4840 ba580f68 2020-03-22 stsp (*nentries)++;
4844 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
4845 44d03001 2019-05-09 stsp int modified;
4846 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
4848 036813ee 2019-05-09 stsp goto done;
4849 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
4850 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
4852 2f51b5b3 2019-05-09 stsp goto done;
4853 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
4854 44d03001 2019-05-09 stsp if (modified) {
4855 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
4856 ba580f68 2020-03-22 stsp int nsubentries;
4857 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
4858 ba580f68 2020-03-22 stsp &nsubentries, te,
4859 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
4860 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
4862 44d03001 2019-05-09 stsp goto done;
4863 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
4864 ba580f68 2020-03-22 stsp /* All entries were deleted. */
4865 ba580f68 2020-03-22 stsp free(new_id);
4868 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
4869 56e0773d 2019-11-28 stsp sizeof(new_te->id));
4870 56e0773d 2019-11-28 stsp free(new_id);
4872 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
4874 036813ee 2019-05-09 stsp goto done;
4875 ba580f68 2020-03-22 stsp (*nentries)++;
4879 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
4880 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
4882 f66c734c 2019-09-22 stsp goto done;
4884 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
4885 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
4886 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
4887 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
4888 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
4889 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
4891 2f51b5b3 2019-05-09 stsp goto done;
4892 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
4894 2f51b5b3 2019-05-09 stsp goto done;
4895 ba580f68 2020-03-22 stsp (*nentries)++;
4897 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
4898 b416585c 2019-05-13 stsp status_arg);
4900 afa376bf 2019-05-09 stsp goto done;
4902 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
4903 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
4905 2f51b5b3 2019-05-09 stsp goto done;
4906 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
4908 2f51b5b3 2019-05-09 stsp goto done;
4909 ba580f68 2020-03-22 stsp (*nentries)++;
4914 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
4915 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
4917 036813ee 2019-05-09 stsp got_pathlist_free(&paths);
4918 ed175427 2019-05-09 stsp return err;
4921 ebf99748 2019-05-09 stsp static const struct got_error *
4922 ebf99748 2019-05-09 stsp update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
4923 72fd46fa 2019-09-06 stsp struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
4924 72fd46fa 2019-09-06 stsp int have_staged_files)
4926 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
4927 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
4929 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
4930 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
4931 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4933 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4935 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
4936 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
4937 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
4938 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
4939 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
4940 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
4941 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
4942 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
4943 5f8a88c6 2019-08-03 stsp ct->ondisk_path, ct->staged_blob_id->sha1,
4944 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
4945 72fd46fa 2019-09-06 stsp !have_staged_files);
4947 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
4948 e75eb4da 2019-05-10 stsp ct->ondisk_path, ct->blob_id->sha1,
4949 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
4950 72fd46fa 2019-09-06 stsp !have_staged_files);
4952 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
4955 3969253a 2020-03-07 stsp err = got_fileindex_entry_update(ie, ct->ondisk_path,
4956 3969253a 2020-03-07 stsp ct->blob_id->sha1, new_base_commit_id->sha1, 1);
4957 3969253a 2020-03-07 stsp if (err) {
4958 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
4961 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
4962 3969253a 2020-03-07 stsp if (err) {
4963 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
4968 d56d26ce 2019-05-10 stsp return err;
4972 d56d26ce 2019-05-10 stsp static const struct got_error *
4973 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
4974 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
4975 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
4976 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
4977 735ef5ac 2019-08-03 stsp int ood_errcode)
4979 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
4980 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
4982 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
4983 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
4984 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
4985 1a36436d 2019-06-10 stsp return NULL;
4987 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
4988 9bead371 2019-07-28 stsp * on matches file content in the branch head.
4990 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
4991 735ef5ac 2019-08-03 stsp in_repo_path);
4992 9bead371 2019-07-28 stsp if (err) {
4993 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
4994 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
4995 1a36436d 2019-06-10 stsp goto done;
4996 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
4997 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
4999 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
5000 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
5001 735ef5ac 2019-08-03 stsp in_repo_path);
5002 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5003 1a36436d 2019-06-10 stsp goto done;
5004 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
5008 1a36436d 2019-06-10 stsp return err;
5011 c4296144 2019-05-09 stsp const struct got_error *
5012 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
5013 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
5014 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id, struct got_worktree *worktree,
5015 5c1e53bc 2019-07-28 stsp const char *author, const char *committer,
5016 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5017 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5018 afa376bf 2019-05-09 stsp struct got_repository *repo)
5020 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5021 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
5022 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
5023 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
5024 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
5025 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
5026 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
5027 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
5028 ba580f68 2020-03-22 stsp int nentries;
5029 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
5030 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
5031 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
5033 c4296144 2019-05-09 stsp *new_commit_id = NULL;
5035 de18fc63 2019-05-09 stsp SIMPLEQ_INIT(&parent_ids);
5037 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5039 588edf97 2019-05-10 stsp goto done;
5041 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5043 588edf97 2019-05-10 stsp goto done;
5045 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
5046 39cd0ff6 2019-07-12 stsp err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5051 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
5052 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5056 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
5057 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5058 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5059 cf066bf8 2019-05-09 stsp char *ondisk_path;
5061 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
5062 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5063 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
5066 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
5067 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
5068 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE)
5071 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
5072 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
5073 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5074 cf066bf8 2019-05-09 stsp goto done;
5076 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5077 bd6aa359 2020-07-23 stsp if (err) {
5078 bd6aa359 2020-07-23 stsp free(ondisk_path);
5079 cf066bf8 2019-05-09 stsp goto done;
5083 bd6aa359 2020-07-23 stsp * When comitting a symlink we convert "bad" symlinks (those
5084 bd6aa359 2020-07-23 stsp * which point outside the work tree or into .got) to regular
5085 bd6aa359 2020-07-23 stsp * files. This way, the post-commit work tree state matches
5086 bd6aa359 2020-07-23 stsp * a fresh checkout of the tree which was committed.
5088 bd6aa359 2020-07-23 stsp if (S_ISLNK(get_ct_file_mode(ct))) {
5089 bd6aa359 2020-07-23 stsp struct got_blob_object *blob;
5090 bd6aa359 2020-07-23 stsp err = got_object_open_as_blob(&blob, repo, ct->blob_id,
5091 bd6aa359 2020-07-23 stsp PATH_MAX);
5092 bd6aa359 2020-07-23 stsp if (err) {
5093 bd6aa359 2020-07-23 stsp free(ondisk_path);
5094 bd6aa359 2020-07-23 stsp goto done;
5096 bd6aa359 2020-07-23 stsp err = install_symlink(worktree, ondisk_path, ct->path,
5097 bd6aa359 2020-07-23 stsp get_ct_file_mode(ct), GOT_DEFAULT_FILE_MODE, blob,
5098 bd6aa359 2020-07-23 stsp 0, 0, repo, NULL, NULL);
5099 bd6aa359 2020-07-23 stsp got_object_blob_close(blob);
5100 bd6aa359 2020-07-23 stsp if (err) {
5101 bd6aa359 2020-07-23 stsp free(ondisk_path);
5102 bd6aa359 2020-07-23 stsp goto done;
5105 bd6aa359 2020-07-23 stsp free(ondisk_path);
5108 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
5109 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5110 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5112 036813ee 2019-05-09 stsp goto done;
5114 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5116 de18fc63 2019-05-09 stsp goto done;
5117 de18fc63 2019-05-09 stsp SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5118 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5119 de18fc63 2019-05-09 stsp 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5120 de18fc63 2019-05-09 stsp got_object_qid_free(pid);
5121 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
5122 33ad4cbe 2019-05-12 jcs free(logmsg);
5124 9d40349a 2019-05-09 stsp goto done;
5126 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
5127 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
5128 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
5129 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
5130 09f5bd90 2019-05-09 stsp goto done;
5132 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
5133 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5135 09f5bd90 2019-05-09 stsp goto done;
5136 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5138 ebf99748 2019-05-09 stsp goto done;
5139 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5140 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5141 09f5bd90 2019-05-09 stsp goto done;
5143 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
5144 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
5146 f2c16586 2019-05-09 stsp goto done;
5147 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
5149 f2c16586 2019-05-09 stsp goto done;
5151 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5153 09f5bd90 2019-05-09 stsp goto done;
5155 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
5157 ebf99748 2019-05-09 stsp goto done;
5159 588edf97 2019-05-10 stsp if (head_tree)
5160 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
5161 588edf97 2019-05-10 stsp if (head_commit)
5162 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
5163 09f5bd90 2019-05-09 stsp free(head_commit_id2);
5164 2f17228e 2019-05-12 stsp if (head_ref2) {
5165 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
5166 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
5167 2f17228e 2019-05-12 stsp err = unlockerr;
5168 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
5170 39cd0ff6 2019-07-12 stsp return err;
5173 5c1e53bc 2019-07-28 stsp static const struct got_error *
5174 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
5175 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
5177 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
5178 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
5180 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
5181 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
5182 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
5184 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
5185 5c1e53bc 2019-07-28 stsp ct_path++;
5187 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
5188 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
5192 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
5193 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
5195 5c1e53bc 2019-07-28 stsp return NULL;
5198 f0b75401 2019-08-03 stsp static const struct got_error *
5199 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
5201 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
5203 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5204 f0b75401 2019-08-03 stsp *have_staged_files = 1;
5205 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
5208 f0b75401 2019-08-03 stsp return NULL;
5211 5f8a88c6 2019-08-03 stsp static const struct got_error *
5212 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
5213 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
5215 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
5216 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
5218 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
5219 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
5221 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5222 5f8a88c6 2019-08-03 stsp if (ie == NULL)
5223 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5224 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5225 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
5226 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
5229 5f8a88c6 2019-08-03 stsp return NULL;
5232 39cd0ff6 2019-07-12 stsp const struct got_error *
5233 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
5234 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
5235 39cd0ff6 2019-07-12 stsp const char *author, const char *committer,
5236 39cd0ff6 2019-07-12 stsp got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5237 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
5238 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
5240 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5241 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
5242 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
5243 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
5244 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
5245 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
5246 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
5247 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
5248 f0b75401 2019-08-03 stsp int have_staged_files = 0;
5250 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
5252 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
5254 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
5256 39cd0ff6 2019-07-12 stsp goto done;
5258 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5260 39cd0ff6 2019-07-12 stsp goto done;
5262 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
5264 39cd0ff6 2019-07-12 stsp goto done;
5266 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5268 39cd0ff6 2019-07-12 stsp goto done;
5270 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5271 5f8a88c6 2019-08-03 stsp &have_staged_files);
5272 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
5273 5f8a88c6 2019-08-03 stsp goto done;
5274 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
5275 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
5277 5f8a88c6 2019-08-03 stsp goto done;
5280 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
5281 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
5282 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
5283 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
5284 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5285 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5286 f2a9dc41 2019-12-13 tracey collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5288 5c1e53bc 2019-07-28 stsp goto done;
5291 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
5292 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5293 39cd0ff6 2019-07-12 stsp goto done;
5296 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5297 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
5299 5c1e53bc 2019-07-28 stsp goto done;
5302 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5303 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
5304 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
5306 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
5307 f0b75401 2019-08-03 stsp ct_path++;
5308 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
5309 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5310 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5312 b50cabdf 2019-07-12 stsp goto done;
5316 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
5317 5c1e53bc 2019-07-28 stsp head_commit_id, worktree, author, committer,
5318 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5320 39cd0ff6 2019-07-12 stsp goto done;
5322 93ec1b5f 2019-07-12 stsp err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5323 72fd46fa 2019-09-06 stsp fileindex, have_staged_files);
5324 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5325 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
5326 39cd0ff6 2019-07-12 stsp err = sync_err;
5328 39cd0ff6 2019-07-12 stsp if (fileindex)
5329 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
5330 39cd0ff6 2019-07-12 stsp free(fileindex_path);
5331 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5332 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
5333 39cd0ff6 2019-07-12 stsp err = unlockerr;
5334 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5335 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
5336 39cd0ff6 2019-07-12 stsp free_commitable(ct);
5338 39cd0ff6 2019-07-12 stsp got_pathlist_free(&commitable_paths);
5339 c4296144 2019-05-09 stsp return err;
5342 8656d6c4 2019-05-20 stsp const char *
5343 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
5345 8656d6c4 2019-05-20 stsp return ct->path;
5348 8656d6c4 2019-05-20 stsp unsigned int
5349 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
5351 8656d6c4 2019-05-20 stsp return ct->status;
5354 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
5355 818c7501 2019-07-11 stsp struct got_worktree *worktree;
5356 818c7501 2019-07-11 stsp struct got_repository *repo;
5359 818c7501 2019-07-11 stsp static const struct got_error *
5360 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5362 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5363 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
5364 818c7501 2019-07-11 stsp unsigned char status;
5365 818c7501 2019-07-11 stsp struct stat sb;
5366 818c7501 2019-07-11 stsp char *ondisk_path;
5368 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
5369 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5370 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
5371 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
5373 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5375 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
5377 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
5378 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5379 818c7501 2019-07-11 stsp free(ondisk_path);
5381 818c7501 2019-07-11 stsp return err;
5383 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
5384 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
5385 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5386 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5388 818c7501 2019-07-11 stsp return NULL;
5391 818c7501 2019-07-11 stsp const struct got_error *
5392 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5393 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5394 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
5395 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
5397 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5398 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5399 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
5400 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
5401 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
5402 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5403 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
5405 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
5406 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5407 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5409 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
5411 818c7501 2019-07-11 stsp return err;
5413 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
5415 818c7501 2019-07-11 stsp goto done;
5417 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
5418 818c7501 2019-07-11 stsp ok_arg.repo = repo;
5419 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5422 818c7501 2019-07-11 stsp goto done;
5424 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5426 818c7501 2019-07-11 stsp goto done;
5428 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5430 818c7501 2019-07-11 stsp goto done;
5432 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5434 818c7501 2019-07-11 stsp goto done;
5436 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5439 e51d7b55 2020-01-04 stsp goto done;
5441 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5443 818c7501 2019-07-11 stsp goto done;
5444 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5445 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5446 e51d7b55 2020-01-04 stsp goto done;
5449 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
5450 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
5452 818c7501 2019-07-11 stsp goto done;
5453 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
5455 818c7501 2019-07-11 stsp goto done;
5457 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
5459 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5461 818c7501 2019-07-11 stsp goto done;
5463 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
5465 818c7501 2019-07-11 stsp goto done;
5467 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
5468 818c7501 2019-07-11 stsp worktree->base_commit_id);
5470 818c7501 2019-07-11 stsp goto done;
5471 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
5473 818c7501 2019-07-11 stsp goto done;
5475 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
5477 818c7501 2019-07-11 stsp goto done;
5479 818c7501 2019-07-11 stsp free(fileindex_path);
5480 818c7501 2019-07-11 stsp free(tmp_branch_name);
5481 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
5482 818c7501 2019-07-11 stsp free(branch_ref_name);
5483 818c7501 2019-07-11 stsp if (branch_ref)
5484 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
5485 818c7501 2019-07-11 stsp if (wt_branch)
5486 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
5487 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
5488 818c7501 2019-07-11 stsp if (err) {
5489 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
5490 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
5491 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
5493 818c7501 2019-07-11 stsp if (*tmp_branch) {
5494 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
5495 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5497 3e3a69f1 2019-07-25 stsp if (*fileindex) {
5498 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
5499 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5501 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
5503 818c7501 2019-07-11 stsp return err;
5506 818c7501 2019-07-11 stsp const struct got_error *
5507 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
5508 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5509 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
5510 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
5512 818c7501 2019-07-11 stsp const struct got_error *err;
5513 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5514 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5515 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5516 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
5517 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
5519 818c7501 2019-07-11 stsp *commit_id = NULL;
5520 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
5521 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
5522 3e3a69f1 2019-07-25 stsp *branch = NULL;
5523 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5525 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
5527 3e3a69f1 2019-07-25 stsp return err;
5529 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
5531 f032f1f7 2019-08-04 stsp goto done;
5533 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5534 f032f1f7 2019-08-04 stsp &have_staged_files);
5535 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
5536 f032f1f7 2019-08-04 stsp goto done;
5537 f032f1f7 2019-08-04 stsp if (have_staged_files) {
5538 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
5539 3e3a69f1 2019-07-25 stsp goto done;
5542 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5544 f032f1f7 2019-08-04 stsp goto done;
5546 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5548 818c7501 2019-07-11 stsp goto done;
5550 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5552 818c7501 2019-07-11 stsp goto done;
5554 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5556 818c7501 2019-07-11 stsp goto done;
5558 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5560 818c7501 2019-07-11 stsp goto done;
5562 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
5563 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
5565 818c7501 2019-07-11 stsp goto done;
5567 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5569 818c7501 2019-07-11 stsp goto done;
5571 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
5573 818c7501 2019-07-11 stsp goto done;
5575 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
5576 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
5578 818c7501 2019-07-11 stsp goto done;
5580 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5582 818c7501 2019-07-11 stsp goto done;
5584 818c7501 2019-07-11 stsp free(commit_ref_name);
5585 818c7501 2019-07-11 stsp free(branch_ref_name);
5586 3e3a69f1 2019-07-25 stsp free(fileindex_path);
5587 818c7501 2019-07-11 stsp if (commit_ref)
5588 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
5589 818c7501 2019-07-11 stsp if (branch_ref)
5590 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
5591 818c7501 2019-07-11 stsp if (err) {
5592 818c7501 2019-07-11 stsp free(*commit_id);
5593 818c7501 2019-07-11 stsp *commit_id = NULL;
5594 818c7501 2019-07-11 stsp if (*tmp_branch) {
5595 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
5596 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5598 818c7501 2019-07-11 stsp if (*new_base_branch) {
5599 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
5600 818c7501 2019-07-11 stsp *new_base_branch = NULL;
5602 818c7501 2019-07-11 stsp if (*branch) {
5603 818c7501 2019-07-11 stsp got_ref_close(*branch);
5604 818c7501 2019-07-11 stsp *branch = NULL;
5606 3e3a69f1 2019-07-25 stsp if (*fileindex) {
5607 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
5608 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5610 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
5612 818c7501 2019-07-11 stsp return err;
5615 818c7501 2019-07-11 stsp const struct got_error *
5616 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5618 818c7501 2019-07-11 stsp const struct got_error *err;
5619 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
5621 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5623 818c7501 2019-07-11 stsp return err;
5625 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5626 818c7501 2019-07-11 stsp free(tmp_branch_name);
5627 818c7501 2019-07-11 stsp return NULL;
5630 818c7501 2019-07-11 stsp static const struct got_error *
5631 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5632 818c7501 2019-07-11 stsp char **logmsg, void *arg)
5634 0ebf8283 2019-07-24 stsp *logmsg = arg;
5635 818c7501 2019-07-11 stsp return NULL;
5638 818c7501 2019-07-11 stsp static const struct got_error *
5639 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5640 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
5641 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5642 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
5644 818c7501 2019-07-11 stsp return NULL;
5647 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
5648 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
5649 01757395 2019-07-12 stsp void *progress_arg;
5650 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
5653 01757395 2019-07-12 stsp static const struct got_error *
5654 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
5656 01757395 2019-07-12 stsp const struct got_error *err;
5657 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
5659 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
5661 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
5663 01757395 2019-07-12 stsp return err;
5665 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
5666 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
5667 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
5668 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
5669 01757395 2019-07-12 stsp return NULL;
5671 01757395 2019-07-12 stsp p = strdup(path);
5672 01757395 2019-07-12 stsp if (p == NULL)
5673 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
5675 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5676 01757395 2019-07-12 stsp if (err || new == NULL)
5678 01757395 2019-07-12 stsp return err;
5682 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5684 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
5686 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry)
5687 01757395 2019-07-12 stsp free((char *)pe->path);
5689 01757395 2019-07-12 stsp got_pathlist_free(merged_paths);
5692 0ebf8283 2019-07-24 stsp static const struct got_error *
5693 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5694 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
5696 818c7501 2019-07-11 stsp const struct got_error *err;
5697 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
5699 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5700 818c7501 2019-07-11 stsp if (err) {
5701 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
5702 818c7501 2019-07-11 stsp goto done;
5703 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
5705 818c7501 2019-07-11 stsp goto done;
5706 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
5708 818c7501 2019-07-11 stsp goto done;
5709 de05890f 2020-03-05 stsp } else if (is_rebase) {
5710 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
5713 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
5715 818c7501 2019-07-11 stsp goto done;
5716 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
5717 818c7501 2019-07-11 stsp free(stored_id);
5718 818c7501 2019-07-11 stsp if (cmp != 0) {
5719 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
5720 818c7501 2019-07-11 stsp goto done;
5724 0ebf8283 2019-07-24 stsp if (commit_ref)
5725 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
5726 0ebf8283 2019-07-24 stsp return err;
5729 0ebf8283 2019-07-24 stsp static const struct got_error *
5730 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
5731 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
5732 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
5733 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
5734 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
5735 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
5737 0ebf8283 2019-07-24 stsp const struct got_error *err;
5738 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
5739 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
5740 3e3a69f1 2019-07-25 stsp char *fileindex_path;
5742 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
5744 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
5746 0ebf8283 2019-07-24 stsp return err;
5748 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
5749 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
5750 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
5751 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
5752 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
5753 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
5754 818c7501 2019-07-11 stsp if (commit_ref)
5755 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
5756 818c7501 2019-07-11 stsp return err;
5759 818c7501 2019-07-11 stsp const struct got_error *
5760 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
5761 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
5762 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5763 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
5764 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
5765 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
5767 0ebf8283 2019-07-24 stsp const struct got_error *err;
5768 0ebf8283 2019-07-24 stsp char *commit_ref_name;
5770 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5772 0ebf8283 2019-07-24 stsp return err;
5774 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
5776 0ebf8283 2019-07-24 stsp goto done;
5778 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5779 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
5780 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
5782 0ebf8283 2019-07-24 stsp free(commit_ref_name);
5783 0ebf8283 2019-07-24 stsp return err;
5786 0ebf8283 2019-07-24 stsp const struct got_error *
5787 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
5788 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
5789 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5790 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
5791 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
5792 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
5794 0ebf8283 2019-07-24 stsp const struct got_error *err;
5795 0ebf8283 2019-07-24 stsp char *commit_ref_name;
5797 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5799 0ebf8283 2019-07-24 stsp return err;
5801 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
5803 0ebf8283 2019-07-24 stsp goto done;
5805 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5806 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
5807 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
5809 0ebf8283 2019-07-24 stsp free(commit_ref_name);
5810 0ebf8283 2019-07-24 stsp return err;
5813 0ebf8283 2019-07-24 stsp static const struct got_error *
5814 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
5815 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
5816 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
5817 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
5818 3e3a69f1 2019-07-25 stsp const char *new_logmsg, struct got_repository *repo)
5820 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
5821 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
5822 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
5823 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
5824 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
5825 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
5826 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
5828 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
5829 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
5831 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
5833 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
5835 3e3a69f1 2019-07-25 stsp return err;
5837 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
5838 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
5839 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
5840 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
5842 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
5843 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
5844 01757395 2019-07-12 stsp * TODO: Ideally, merged_paths would contain a list of commitables
5845 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
5847 01757395 2019-07-12 stsp if (merged_paths) {
5848 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
5849 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
5850 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
5851 f2a9dc41 2019-12-13 tracey repo, collect_commitables, &cc_arg, NULL, NULL, 0,
5854 01757395 2019-07-12 stsp goto done;
5857 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
5858 f2a9dc41 2019-12-13 tracey collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5860 01757395 2019-07-12 stsp goto done;
5863 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
5864 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
5865 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
5867 a0e95631 2019-07-12 stsp goto done;
5868 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5869 a0e95631 2019-07-12 stsp goto done;
5872 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5874 edd02c5e 2019-07-11 stsp goto done;
5876 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
5878 818c7501 2019-07-11 stsp goto done;
5880 5943eee2 2019-08-13 stsp if (new_logmsg) {
5881 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
5882 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
5883 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
5884 5943eee2 2019-08-13 stsp goto done;
5887 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
5889 5943eee2 2019-08-13 stsp goto done;
5892 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
5893 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
5894 5c1e53bc 2019-07-28 stsp worktree, got_object_commit_get_author(orig_commit),
5895 a0e95631 2019-07-12 stsp got_object_commit_get_committer(orig_commit),
5896 0ebf8283 2019-07-24 stsp collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
5898 a0e95631 2019-07-12 stsp goto done;
5900 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
5902 a0e95631 2019-07-12 stsp goto done;
5904 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
5906 a0e95631 2019-07-12 stsp goto done;
5908 93ec1b5f 2019-07-12 stsp err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5909 72fd46fa 2019-09-06 stsp fileindex, 0);
5910 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5911 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
5912 a0e95631 2019-07-12 stsp err = sync_err;
5914 a0e95631 2019-07-12 stsp free(fileindex_path);
5915 a0e95631 2019-07-12 stsp free(head_commit_id);
5916 a0e95631 2019-07-12 stsp if (head_ref)
5917 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
5918 818c7501 2019-07-11 stsp if (err) {
5919 818c7501 2019-07-11 stsp free(*new_commit_id);
5920 818c7501 2019-07-11 stsp *new_commit_id = NULL;
5922 818c7501 2019-07-11 stsp return err;
5925 818c7501 2019-07-11 stsp const struct got_error *
5926 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
5927 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5928 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5929 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
5930 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, struct got_repository *repo)
5932 0ebf8283 2019-07-24 stsp const struct got_error *err;
5933 0ebf8283 2019-07-24 stsp char *commit_ref_name;
5934 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
5935 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
5937 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5939 0ebf8283 2019-07-24 stsp return err;
5941 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5943 0ebf8283 2019-07-24 stsp goto done;
5944 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
5946 0ebf8283 2019-07-24 stsp goto done;
5947 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5948 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
5949 0ebf8283 2019-07-24 stsp goto done;
5952 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5953 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
5955 0ebf8283 2019-07-24 stsp if (commit_ref)
5956 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
5957 0ebf8283 2019-07-24 stsp free(commit_ref_name);
5958 0ebf8283 2019-07-24 stsp free(commit_id);
5959 0ebf8283 2019-07-24 stsp return err;
5962 0ebf8283 2019-07-24 stsp const struct got_error *
5963 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
5964 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5965 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5966 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
5967 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
5968 0ebf8283 2019-07-24 stsp struct got_repository *repo)
5970 0ebf8283 2019-07-24 stsp const struct got_error *err;
5971 0ebf8283 2019-07-24 stsp char *commit_ref_name;
5972 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
5974 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5976 0ebf8283 2019-07-24 stsp return err;
5978 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5980 0ebf8283 2019-07-24 stsp goto done;
5982 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5983 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
5985 0ebf8283 2019-07-24 stsp if (commit_ref)
5986 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
5987 0ebf8283 2019-07-24 stsp free(commit_ref_name);
5988 0ebf8283 2019-07-24 stsp return err;
5991 0ebf8283 2019-07-24 stsp const struct got_error *
5992 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
5993 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
5995 3e3a69f1 2019-07-25 stsp if (fileindex)
5996 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
5997 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
6000 69844fba 2019-07-11 stsp static const struct got_error *
6001 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
6003 69844fba 2019-07-11 stsp const struct got_error *err;
6004 69844fba 2019-07-11 stsp struct got_reference *ref;
6006 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
6007 69844fba 2019-07-11 stsp if (err) {
6008 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
6009 69844fba 2019-07-11 stsp return NULL;
6010 69844fba 2019-07-11 stsp return err;
6013 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
6014 69844fba 2019-07-11 stsp got_ref_close(ref);
6015 69844fba 2019-07-11 stsp return err;
6018 69844fba 2019-07-11 stsp static const struct got_error *
6019 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6021 69844fba 2019-07-11 stsp const struct got_error *err;
6022 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6023 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6025 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6027 69844fba 2019-07-11 stsp goto done;
6028 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
6030 69844fba 2019-07-11 stsp goto done;
6032 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6034 69844fba 2019-07-11 stsp goto done;
6035 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
6037 69844fba 2019-07-11 stsp goto done;
6039 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6041 69844fba 2019-07-11 stsp goto done;
6042 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
6044 69844fba 2019-07-11 stsp goto done;
6046 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6048 69844fba 2019-07-11 stsp goto done;
6049 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
6051 69844fba 2019-07-11 stsp goto done;
6054 69844fba 2019-07-11 stsp free(tmp_branch_name);
6055 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
6056 69844fba 2019-07-11 stsp free(branch_ref_name);
6057 69844fba 2019-07-11 stsp free(commit_ref_name);
6058 69844fba 2019-07-11 stsp return err;
6061 818c7501 2019-07-11 stsp const struct got_error *
6062 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
6063 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6064 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6065 818c7501 2019-07-11 stsp struct got_repository *repo)
6067 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
6068 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
6070 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6072 818c7501 2019-07-11 stsp return err;
6074 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6076 818c7501 2019-07-11 stsp goto done;
6078 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
6080 818c7501 2019-07-11 stsp goto done;
6082 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
6084 818c7501 2019-07-11 stsp goto done;
6086 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
6088 3e3a69f1 2019-07-25 stsp if (fileindex)
6089 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6090 818c7501 2019-07-11 stsp free(new_head_commit_id);
6091 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6092 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
6093 818c7501 2019-07-11 stsp err = unlockerr;
6094 818c7501 2019-07-11 stsp return err;
6097 818c7501 2019-07-11 stsp const struct got_error *
6098 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
6099 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6100 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
6101 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
6103 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
6104 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
6105 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
6106 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6107 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
6108 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
6110 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6112 818c7501 2019-07-11 stsp return err;
6114 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
6115 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
6117 818c7501 2019-07-11 stsp goto done;
6119 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
6121 818c7501 2019-07-11 stsp goto done;
6124 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
6125 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
6126 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
6128 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
6130 818c7501 2019-07-11 stsp goto done;
6132 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6134 818c7501 2019-07-11 stsp goto done;
6136 ca355955 2019-07-12 stsp err = got_object_id_by_path(&tree_id, repo,
6137 ca355955 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
6139 ca355955 2019-07-12 stsp goto done;
6141 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
6143 ca355955 2019-07-12 stsp goto done;
6145 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6147 818c7501 2019-07-11 stsp goto done;
6149 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
6150 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
6151 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
6152 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
6153 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
6154 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
6155 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
6156 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6157 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
6159 1f1abb7e 2019-08-08 stsp goto sync;
6161 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6162 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
6164 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6165 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
6166 ca355955 2019-07-12 stsp err = sync_err;
6168 818c7501 2019-07-11 stsp got_ref_close(resolved);
6169 a3a2faf2 2019-07-12 stsp free(tree_id);
6170 818c7501 2019-07-11 stsp free(commit_id);
6171 0ebf8283 2019-07-24 stsp if (fileindex)
6172 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
6173 0ebf8283 2019-07-24 stsp free(fileindex_path);
6175 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6176 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
6177 0ebf8283 2019-07-24 stsp err = unlockerr;
6178 0ebf8283 2019-07-24 stsp return err;
6181 0ebf8283 2019-07-24 stsp const struct got_error *
6182 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6183 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6184 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
6185 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6187 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6188 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6189 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
6190 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
6191 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6192 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
6193 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
6194 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6196 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6197 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6198 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6199 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6201 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
6203 0ebf8283 2019-07-24 stsp return err;
6205 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6207 0ebf8283 2019-07-24 stsp goto done;
6209 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
6210 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
6211 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6214 0ebf8283 2019-07-24 stsp goto done;
6216 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6218 0ebf8283 2019-07-24 stsp goto done;
6220 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6222 0ebf8283 2019-07-24 stsp goto done;
6224 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6225 0ebf8283 2019-07-24 stsp worktree);
6227 0ebf8283 2019-07-24 stsp goto done;
6229 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6232 0ebf8283 2019-07-24 stsp goto done;
6234 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6236 0ebf8283 2019-07-24 stsp goto done;
6238 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
6240 0ebf8283 2019-07-24 stsp goto done;
6242 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6243 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6245 0ebf8283 2019-07-24 stsp goto done;
6246 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
6248 0ebf8283 2019-07-24 stsp goto done;
6249 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6250 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
6251 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
6252 0ebf8283 2019-07-24 stsp goto done;
6255 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6256 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6258 0ebf8283 2019-07-24 stsp goto done;
6259 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
6261 0ebf8283 2019-07-24 stsp goto done;
6263 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6265 0ebf8283 2019-07-24 stsp goto done;
6267 0ebf8283 2019-07-24 stsp free(fileindex_path);
6268 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6269 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6270 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
6271 0ebf8283 2019-07-24 stsp if (wt_branch)
6272 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
6273 0ebf8283 2019-07-24 stsp if (err) {
6274 0ebf8283 2019-07-24 stsp if (*branch_ref) {
6275 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
6276 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6278 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6279 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6280 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6282 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6283 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6284 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6285 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6287 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
6289 0ebf8283 2019-07-24 stsp return err;
6292 0ebf8283 2019-07-24 stsp const struct got_error *
6293 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
6294 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6296 3e3a69f1 2019-07-25 stsp if (fileindex)
6297 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6298 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
6301 0ebf8283 2019-07-24 stsp const struct got_error *
6302 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
6303 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
6305 0ebf8283 2019-07-24 stsp const struct got_error *err;
6306 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6308 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6310 0ebf8283 2019-07-24 stsp return err;
6312 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6313 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6314 0ebf8283 2019-07-24 stsp return NULL;
6317 0ebf8283 2019-07-24 stsp const struct got_error *
6318 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
6319 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
6320 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6321 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6323 0ebf8283 2019-07-24 stsp const struct got_error *err;
6324 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6325 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6326 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6327 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6328 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6329 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6331 0ebf8283 2019-07-24 stsp *commit_id = NULL;
6332 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6333 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6334 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6336 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6338 3e3a69f1 2019-07-25 stsp return err;
6340 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6342 f032f1f7 2019-08-04 stsp goto done;
6344 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6345 f032f1f7 2019-08-04 stsp &have_staged_files);
6346 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6347 f032f1f7 2019-08-04 stsp goto done;
6348 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6349 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6350 3e3a69f1 2019-07-25 stsp goto done;
6353 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6355 f032f1f7 2019-08-04 stsp goto done;
6357 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6359 0ebf8283 2019-07-24 stsp goto done;
6361 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6363 0ebf8283 2019-07-24 stsp goto done;
6365 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6366 0ebf8283 2019-07-24 stsp worktree);
6368 0ebf8283 2019-07-24 stsp goto done;
6370 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6372 0ebf8283 2019-07-24 stsp goto done;
6374 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6376 0ebf8283 2019-07-24 stsp goto done;
6377 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6379 0ebf8283 2019-07-24 stsp goto done;
6381 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6383 0ebf8283 2019-07-24 stsp goto done;
6384 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6386 0ebf8283 2019-07-24 stsp goto done;
6388 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6390 0ebf8283 2019-07-24 stsp goto done;
6392 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6393 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6394 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6395 0ebf8283 2019-07-24 stsp if (commit_ref)
6396 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6397 0ebf8283 2019-07-24 stsp if (base_commit_ref)
6398 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
6399 0ebf8283 2019-07-24 stsp if (err) {
6400 0ebf8283 2019-07-24 stsp free(*commit_id);
6401 0ebf8283 2019-07-24 stsp *commit_id = NULL;
6402 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6403 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6404 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6405 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6406 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6408 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6409 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6410 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6412 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
6414 0ebf8283 2019-07-24 stsp return err;
6417 0ebf8283 2019-07-24 stsp static const struct got_error *
6418 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6420 0ebf8283 2019-07-24 stsp const struct got_error *err;
6421 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6422 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6424 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6426 0ebf8283 2019-07-24 stsp goto done;
6427 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
6429 0ebf8283 2019-07-24 stsp goto done;
6431 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6432 0ebf8283 2019-07-24 stsp worktree);
6434 0ebf8283 2019-07-24 stsp goto done;
6435 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
6437 0ebf8283 2019-07-24 stsp goto done;
6439 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6441 0ebf8283 2019-07-24 stsp goto done;
6442 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
6444 0ebf8283 2019-07-24 stsp goto done;
6446 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6448 0ebf8283 2019-07-24 stsp goto done;
6449 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
6451 0ebf8283 2019-07-24 stsp goto done;
6453 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6454 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
6455 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6456 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6457 0ebf8283 2019-07-24 stsp return err;
6460 0ebf8283 2019-07-24 stsp const struct got_error *
6461 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
6462 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6463 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
6464 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
6466 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
6467 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
6468 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6469 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
6470 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
6472 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
6474 0ebf8283 2019-07-24 stsp return err;
6476 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
6477 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
6479 0ebf8283 2019-07-24 stsp goto done;
6481 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
6483 0ebf8283 2019-07-24 stsp goto done;
6485 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6487 0ebf8283 2019-07-24 stsp goto done;
6489 0ebf8283 2019-07-24 stsp err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6490 0ebf8283 2019-07-24 stsp worktree->path_prefix);
6492 0ebf8283 2019-07-24 stsp goto done;
6494 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
6496 0ebf8283 2019-07-24 stsp goto done;
6498 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6500 0ebf8283 2019-07-24 stsp goto done;
6502 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
6503 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
6504 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
6505 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
6506 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
6507 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
6508 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
6509 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
6510 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
6512 1f1abb7e 2019-08-08 stsp goto sync;
6514 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6515 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
6517 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6518 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
6519 0ebf8283 2019-07-24 stsp err = sync_err;
6521 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
6522 0ebf8283 2019-07-24 stsp free(tree_id);
6523 818c7501 2019-07-11 stsp free(fileindex_path);
6525 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6526 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
6527 818c7501 2019-07-11 stsp err = unlockerr;
6528 818c7501 2019-07-11 stsp return err;
6531 0ebf8283 2019-07-24 stsp const struct got_error *
6532 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
6533 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6534 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
6536 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr;
6537 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
6538 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
6540 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6542 0ebf8283 2019-07-24 stsp return err;
6544 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
6545 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
6547 0ebf8283 2019-07-24 stsp goto done;
6549 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
6551 0ebf8283 2019-07-24 stsp goto done;
6553 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
6555 0ebf8283 2019-07-24 stsp goto done;
6557 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
6559 0ebf8283 2019-07-24 stsp goto done;
6561 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
6563 3e3a69f1 2019-07-25 stsp if (fileindex)
6564 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6565 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
6566 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6567 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
6568 0ebf8283 2019-07-24 stsp err = unlockerr;
6569 0ebf8283 2019-07-24 stsp return err;
6572 0ebf8283 2019-07-24 stsp const struct got_error *
6573 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6574 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6576 0ebf8283 2019-07-24 stsp const struct got_error *err;
6577 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6579 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6581 0ebf8283 2019-07-24 stsp return err;
6583 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6585 0ebf8283 2019-07-24 stsp goto done;
6587 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
6589 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6590 2822a352 2019-10-15 stsp return err;
6593 2822a352 2019-10-15 stsp const struct got_error *
6594 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6595 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6596 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
6597 2822a352 2019-10-15 stsp struct got_repository *repo)
6599 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
6600 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
6601 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
6603 2822a352 2019-10-15 stsp *fileindex = NULL;
6604 2822a352 2019-10-15 stsp *branch_ref = NULL;
6605 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
6607 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
6609 2822a352 2019-10-15 stsp return err;
6611 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6612 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
6613 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
6614 2822a352 2019-10-15 stsp "update -b or different branch name required");
6615 2822a352 2019-10-15 stsp goto done;
6618 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6620 2822a352 2019-10-15 stsp goto done;
6622 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
6623 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
6624 2822a352 2019-10-15 stsp ok_arg.repo = repo;
6625 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6628 2822a352 2019-10-15 stsp goto done;
6630 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
6632 2822a352 2019-10-15 stsp goto done;
6634 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
6635 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
6637 2822a352 2019-10-15 stsp if (err) {
6638 2822a352 2019-10-15 stsp if (*branch_ref) {
6639 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
6640 2822a352 2019-10-15 stsp *branch_ref = NULL;
6642 2822a352 2019-10-15 stsp if (*base_branch_ref) {
6643 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
6644 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
6646 2822a352 2019-10-15 stsp if (*fileindex) {
6647 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
6648 2822a352 2019-10-15 stsp *fileindex = NULL;
6650 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
6652 0cb83759 2019-08-03 stsp return err;
6655 2822a352 2019-10-15 stsp const struct got_error *
6656 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
6657 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6658 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6659 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6660 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6662 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
6663 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
6664 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
6666 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
6668 2822a352 2019-10-15 stsp goto done;
6670 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
6672 2822a352 2019-10-15 stsp goto done;
6674 2822a352 2019-10-15 stsp err = got_object_id_by_path(&tree_id, repo, commit_id,
6675 2822a352 2019-10-15 stsp worktree->path_prefix);
6677 2822a352 2019-10-15 stsp goto done;
6679 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6681 2822a352 2019-10-15 stsp goto done;
6683 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6684 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
6686 2822a352 2019-10-15 stsp goto sync;
6688 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
6690 2822a352 2019-10-15 stsp goto sync;
6692 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
6694 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6695 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
6696 2822a352 2019-10-15 stsp err = sync_err;
6699 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
6700 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
6701 2822a352 2019-10-15 stsp err = unlockerr;
6702 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
6704 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
6705 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
6706 2822a352 2019-10-15 stsp err = unlockerr;
6707 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
6709 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
6710 2822a352 2019-10-15 stsp free(fileindex_path);
6711 2822a352 2019-10-15 stsp free(tree_id);
6713 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6714 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
6715 2822a352 2019-10-15 stsp err = unlockerr;
6716 2822a352 2019-10-15 stsp return err;
6719 2822a352 2019-10-15 stsp const struct got_error *
6720 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
6721 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6722 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
6724 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
6726 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
6728 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
6730 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
6731 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
6732 8b692cd0 2019-10-21 stsp err = unlockerr;
6733 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
6735 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
6736 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
6737 8b692cd0 2019-10-21 stsp err = unlockerr;
6738 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
6740 8b692cd0 2019-10-21 stsp return err;
6743 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
6744 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
6745 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
6746 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
6747 2db2652d 2019-08-07 stsp struct got_repository *repo;
6748 2db2652d 2019-08-07 stsp int have_changes;
6751 2db2652d 2019-08-07 stsp const struct got_error *
6752 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
6753 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
6754 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6755 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
6757 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
6758 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
6759 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
6760 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
6761 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
6762 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
6764 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
6765 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
6766 8b13ce36 2019-08-08 stsp return NULL;
6767 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
6768 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
6770 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6771 735ef5ac 2019-08-03 stsp if (ie == NULL)
6772 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6774 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
6775 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
6776 735ef5ac 2019-08-03 stsp relpath) == -1)
6777 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
6779 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
6780 735ef5ac 2019-08-03 stsp memcpy(base_commit_id.sha1, ie->commit_sha1,
6781 735ef5ac 2019-08-03 stsp SHA1_DIGEST_LENGTH);
6782 735ef5ac 2019-08-03 stsp base_commit_idp = &base_commit_id;
6785 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
6786 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
6787 3aa5969e 2019-08-06 stsp goto done;
6788 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
6789 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
6790 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
6791 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
6792 735ef5ac 2019-08-03 stsp goto done;
6795 2db2652d 2019-08-07 stsp a->have_changes = 1;
6797 735ef5ac 2019-08-03 stsp p = in_repo_path;
6798 735ef5ac 2019-08-03 stsp while (p[0] == '/')
6800 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
6801 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
6802 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
6804 735ef5ac 2019-08-03 stsp free(in_repo_path);
6805 dc424a06 2019-08-07 stsp return err;
6808 2db2652d 2019-08-07 stsp struct stage_path_arg {
6809 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
6810 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
6811 2db2652d 2019-08-07 stsp struct got_repository *repo;
6812 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
6813 2db2652d 2019-08-07 stsp void *status_arg;
6814 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
6815 2db2652d 2019-08-07 stsp void *patch_arg;
6816 7b5dc508 2019-10-28 stsp int staged_something;
6819 2db2652d 2019-08-07 stsp static const struct got_error *
6820 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
6821 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
6822 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6823 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
6825 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
6826 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
6827 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
6828 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
6829 0cb83759 2019-08-03 stsp uint32_t stage;
6830 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
6832 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
6833 8b13ce36 2019-08-08 stsp return NULL;
6835 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6836 d3e7c587 2019-08-03 stsp if (ie == NULL)
6837 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6839 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
6840 2db2652d 2019-08-07 stsp relpath)== -1)
6841 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
6843 0cb83759 2019-08-03 stsp switch (status) {
6844 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
6845 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
6846 2db2652d 2019-08-07 stsp if (a->patch_cb) {
6847 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
6848 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
6849 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
6850 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
6853 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
6856 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
6857 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
6858 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
6859 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
6860 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
6864 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
6865 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
6868 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
6869 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
6870 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
6871 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
6873 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
6874 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
6875 7b5dc508 2019-10-28 stsp a->staged_something = 1;
6876 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
6878 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6879 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
6880 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
6882 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
6883 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
6885 2db2652d 2019-08-07 stsp if (a->patch_cb) {
6886 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
6887 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
6888 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
6891 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
6893 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
6894 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
6898 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
6899 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
6900 7b5dc508 2019-10-28 stsp a->staged_something = 1;
6901 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
6903 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6904 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
6907 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
6909 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
6910 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
6912 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
6913 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
6916 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
6920 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
6921 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
6922 dc424a06 2019-08-07 stsp free(path_content);
6923 2db2652d 2019-08-07 stsp free(ondisk_path);
6924 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
6925 0ebf8283 2019-07-24 stsp return err;
6928 0cb83759 2019-08-03 stsp const struct got_error *
6929 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
6930 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
6931 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
6932 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
6933 1e71573e 2019-08-03 stsp struct got_repository *repo)
6935 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
6936 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
6937 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
6938 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
6939 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
6940 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
6941 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
6942 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
6944 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
6946 0cb83759 2019-08-03 stsp return err;
6948 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
6949 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
6951 735ef5ac 2019-08-03 stsp goto done;
6952 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6954 735ef5ac 2019-08-03 stsp goto done;
6955 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
6957 0cb83759 2019-08-03 stsp goto done;
6959 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
6960 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
6961 2db2652d 2019-08-07 stsp oka.worktree = worktree;
6962 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
6963 2db2652d 2019-08-07 stsp oka.repo = repo;
6964 2db2652d 2019-08-07 stsp oka.have_changes = 0;
6965 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
6966 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
6967 f2a9dc41 2019-12-13 tracey check_stage_ok, &oka, NULL, NULL, 0, 0);
6969 735ef5ac 2019-08-03 stsp goto done;
6971 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
6972 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6973 2db2652d 2019-08-07 stsp goto done;
6976 2db2652d 2019-08-07 stsp spa.worktree = worktree;
6977 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
6978 2db2652d 2019-08-07 stsp spa.repo = repo;
6979 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
6980 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
6981 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
6982 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
6983 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
6984 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
6985 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
6986 f2a9dc41 2019-12-13 tracey stage_path, &spa, NULL, NULL, 0, 0);
6988 2db2652d 2019-08-07 stsp goto done;
6990 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
6991 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6992 7b5dc508 2019-10-28 stsp goto done;
6995 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6996 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
6997 0cb83759 2019-08-03 stsp err = sync_err;
6999 735ef5ac 2019-08-03 stsp if (head_ref)
7000 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
7001 735ef5ac 2019-08-03 stsp free(head_commit_id);
7002 0cb83759 2019-08-03 stsp free(fileindex_path);
7003 0cb83759 2019-08-03 stsp if (fileindex)
7004 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
7005 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7006 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
7007 0cb83759 2019-08-03 stsp err = unlockerr;
7008 0cb83759 2019-08-03 stsp return err;
7011 ad493afc 2019-08-03 stsp struct unstage_path_arg {
7012 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
7013 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
7014 ad493afc 2019-08-03 stsp struct got_repository *repo;
7015 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
7016 ad493afc 2019-08-03 stsp void *progress_arg;
7017 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
7018 2e1f37b0 2019-08-08 stsp void *patch_arg;
7021 2e1f37b0 2019-08-08 stsp static const struct got_error *
7022 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
7023 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
7024 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
7025 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
7026 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
7028 2e1f37b0 2019-08-08 stsp const struct got_error *err;
7029 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
7030 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7031 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7032 2e1f37b0 2019-08-08 stsp struct stat sb1, sb2;
7033 2e1f37b0 2019-08-08 stsp struct got_diff_changes *changes = NULL;
7034 2e1f37b0 2019-08-08 stsp struct got_diff_state *ds = NULL;
7035 2e1f37b0 2019-08-08 stsp struct got_diff_args *args = NULL;
7036 2e1f37b0 2019-08-08 stsp struct got_diff_change *change;
7037 2e1f37b0 2019-08-08 stsp int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
7038 2e1f37b0 2019-08-08 stsp int have_content = 0, have_rejected_content = 0;
7040 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
7041 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
7043 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
7045 2e1f37b0 2019-08-08 stsp return err;
7046 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7048 2e1f37b0 2019-08-08 stsp goto done;
7050 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7052 2e1f37b0 2019-08-08 stsp goto done;
7054 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7056 2e1f37b0 2019-08-08 stsp goto done;
7058 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7060 2e1f37b0 2019-08-08 stsp goto done;
7062 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7064 2e1f37b0 2019-08-08 stsp goto done;
7066 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7068 2e1f37b0 2019-08-08 stsp goto done;
7070 2e1f37b0 2019-08-08 stsp if (stat(path1, &sb1) == -1) {
7071 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("stat", path1);
7072 2e1f37b0 2019-08-08 stsp goto done;
7075 2e1f37b0 2019-08-08 stsp if (stat(path2, &sb2) == -1) {
7076 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("stat", path2);
7077 2e1f37b0 2019-08-08 stsp goto done;
7080 2e1f37b0 2019-08-08 stsp err = got_diff_files(&changes, &ds, &args, &diff_flags,
7081 2e1f37b0 2019-08-08 stsp f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
7083 2e1f37b0 2019-08-08 stsp goto done;
7085 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
7086 2e1f37b0 2019-08-08 stsp "got-unstaged-content");
7088 2e1f37b0 2019-08-08 stsp goto done;
7089 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
7090 2e1f37b0 2019-08-08 stsp "got-new-staged-content");
7092 2e1f37b0 2019-08-08 stsp goto done;
7094 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
7095 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
7096 2e1f37b0 2019-08-08 stsp goto done;
7098 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
7099 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
7100 2e1f37b0 2019-08-08 stsp goto done;
7102 2e1f37b0 2019-08-08 stsp SIMPLEQ_FOREACH(change, &changes->entries, entry) {
7103 2e1f37b0 2019-08-08 stsp int choice;
7104 2e1f37b0 2019-08-08 stsp err = apply_or_reject_change(&choice, change, ++n,
7105 2e1f37b0 2019-08-08 stsp changes->nchanges, ds, args, diff_flags, relpath,
7106 2e1f37b0 2019-08-08 stsp f1, f2, &line_cur1, &line_cur2,
7107 2e1f37b0 2019-08-08 stsp outfile, rejectfile, patch_cb, patch_arg);
7109 2e1f37b0 2019-08-08 stsp goto done;
7110 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
7111 2e1f37b0 2019-08-08 stsp have_content = 1;
7113 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
7114 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
7117 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
7118 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7119 f1e81a05 2019-08-10 stsp outfile, rejectfile);
7121 2e1f37b0 2019-08-08 stsp free(label1);
7123 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
7124 2e1f37b0 2019-08-08 stsp if (staged_blob)
7125 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
7126 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
7127 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
7128 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
7129 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
7130 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
7131 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
7132 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7133 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
7134 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
7135 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
7136 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
7137 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
7138 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
7139 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
7140 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
7141 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
7142 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
7143 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
7144 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
7146 2e1f37b0 2019-08-08 stsp if (err || !have_rejected_content) {
7147 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
7148 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
7149 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
7150 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
7151 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
7152 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
7154 2e1f37b0 2019-08-08 stsp free(args);
7156 2e1f37b0 2019-08-08 stsp got_diff_state_free(ds);
7159 2e1f37b0 2019-08-08 stsp if (changes)
7160 2e1f37b0 2019-08-08 stsp got_diff_free_changes(changes);
7161 2e1f37b0 2019-08-08 stsp free(path1);
7162 2e1f37b0 2019-08-08 stsp free(path2);
7163 2e1f37b0 2019-08-08 stsp return err;
7166 ad493afc 2019-08-03 stsp static const struct got_error *
7167 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
7168 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
7169 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7170 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7172 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
7173 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
7174 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
7175 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7176 2e1f37b0 2019-08-08 stsp char *ondisk_path = NULL, *path_unstaged_content = NULL;
7177 2e1f37b0 2019-08-08 stsp char *path_new_staged_content = NULL;
7178 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
7179 ad493afc 2019-08-03 stsp int local_changes_subsumed;
7180 9bc94a15 2019-08-03 stsp struct stat sb;
7182 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
7183 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
7184 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
7185 2e1f37b0 2019-08-08 stsp return NULL;
7187 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7188 ad493afc 2019-08-03 stsp if (ie == NULL)
7189 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7191 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7193 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
7195 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
7196 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
7198 f69721c3 2019-10-21 stsp goto done;
7199 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7200 f69721c3 2019-10-21 stsp id_str) == -1) {
7201 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
7202 f69721c3 2019-10-21 stsp goto done;
7205 ad493afc 2019-08-03 stsp switch (staged_status) {
7206 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
7207 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
7208 ad493afc 2019-08-03 stsp blob_id, 8192);
7211 ad493afc 2019-08-03 stsp /* fall through */
7212 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
7213 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
7214 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
7215 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
7216 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7217 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
7220 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
7223 2e1f37b0 2019-08-08 stsp err = create_unstaged_content(
7224 2e1f37b0 2019-08-08 stsp &path_unstaged_content,
7225 2e1f37b0 2019-08-08 stsp &path_new_staged_content, blob_id,
7226 2e1f37b0 2019-08-08 stsp staged_blob_id, ie->path, a->repo,
7227 2e1f37b0 2019-08-08 stsp a->patch_cb, a->patch_arg);
7228 2e1f37b0 2019-08-08 stsp if (err || path_unstaged_content == NULL)
7230 2e1f37b0 2019-08-08 stsp if (path_new_staged_content) {
7231 2e1f37b0 2019-08-08 stsp err = got_object_blob_create(
7232 2e1f37b0 2019-08-08 stsp &staged_blob_id,
7233 2e1f37b0 2019-08-08 stsp path_new_staged_content,
7237 2e1f37b0 2019-08-08 stsp memcpy(ie->staged_blob_sha1,
7238 2e1f37b0 2019-08-08 stsp staged_blob_id->sha1,
7239 2e1f37b0 2019-08-08 stsp SHA1_DIGEST_LENGTH);
7241 2e1f37b0 2019-08-08 stsp err = merge_file(&local_changes_subsumed,
7242 2e1f37b0 2019-08-08 stsp a->worktree, blob_base, ondisk_path,
7243 2e1f37b0 2019-08-08 stsp relpath, got_fileindex_perms_to_st(ie),
7244 f69721c3 2019-10-21 stsp path_unstaged_content, label_orig,
7245 f69721c3 2019-10-21 stsp "unstaged", a->repo, a->progress_cb,
7246 f69721c3 2019-10-21 stsp a->progress_arg);
7247 2e1f37b0 2019-08-08 stsp if (err == NULL &&
7248 2e1f37b0 2019-08-08 stsp path_new_staged_content == NULL)
7249 2e1f37b0 2019-08-08 stsp got_fileindex_entry_stage_set(ie,
7250 2e1f37b0 2019-08-08 stsp GOT_FILEIDX_STAGE_NONE);
7251 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
7254 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
7255 ad493afc 2019-08-03 stsp staged_blob_id, 8192);
7258 ad493afc 2019-08-03 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
7259 ad493afc 2019-08-03 stsp blob_base, ondisk_path, relpath,
7260 f69721c3 2019-10-21 stsp got_fileindex_perms_to_st(ie), label_orig, blob_staged,
7261 ad493afc 2019-08-03 stsp commit_id ? commit_id : a->worktree->base_commit_id,
7262 ad493afc 2019-08-03 stsp a->repo, a->progress_cb, a->progress_arg);
7263 ad493afc 2019-08-03 stsp if (err == NULL)
7264 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
7265 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
7267 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
7268 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
7269 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
7270 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7271 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
7274 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
7276 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
7277 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
7281 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7282 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
7283 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
7286 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
7290 ad493afc 2019-08-03 stsp free(ondisk_path);
7291 2e1f37b0 2019-08-08 stsp if (path_unstaged_content &&
7292 2e1f37b0 2019-08-08 stsp unlink(path_unstaged_content) == -1 && err == NULL)
7293 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
7294 2e1f37b0 2019-08-08 stsp if (path_new_staged_content &&
7295 2e1f37b0 2019-08-08 stsp unlink(path_new_staged_content) == -1 && err == NULL)
7296 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
7297 2e1f37b0 2019-08-08 stsp free(path_unstaged_content);
7298 2e1f37b0 2019-08-08 stsp free(path_new_staged_content);
7299 ad493afc 2019-08-03 stsp if (blob_base)
7300 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
7301 ad493afc 2019-08-03 stsp if (blob_staged)
7302 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
7303 f69721c3 2019-10-21 stsp free(id_str);
7304 f69721c3 2019-10-21 stsp free(label_orig);
7305 ad493afc 2019-08-03 stsp return err;
7308 ad493afc 2019-08-03 stsp const struct got_error *
7309 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
7310 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
7311 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7312 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
7313 ad493afc 2019-08-03 stsp struct got_repository *repo)
7315 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7316 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
7317 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
7318 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
7319 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
7321 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
7323 ad493afc 2019-08-03 stsp return err;
7325 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
7327 ad493afc 2019-08-03 stsp goto done;
7329 ad493afc 2019-08-03 stsp upa.worktree = worktree;
7330 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
7331 ad493afc 2019-08-03 stsp upa.repo = repo;
7332 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
7333 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
7334 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
7335 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
7336 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
7337 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
7338 f2a9dc41 2019-12-13 tracey unstage_path, &upa, NULL, NULL, 0, 0);
7340 ad493afc 2019-08-03 stsp goto done;
7343 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7344 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
7345 ad493afc 2019-08-03 stsp err = sync_err;
7347 ad493afc 2019-08-03 stsp free(fileindex_path);
7348 ad493afc 2019-08-03 stsp if (fileindex)
7349 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
7350 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7351 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
7352 ad493afc 2019-08-03 stsp err = unlockerr;
7353 ad493afc 2019-08-03 stsp return err;