Blob


1 /*
2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/stat.h>
18 #include <sys/queue.h>
19 #include <sys/tree.h>
21 #include <dirent.h>
22 #include <limits.h>
23 #include <stddef.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <time.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <sha1.h>
32 #include <zlib.h>
33 #include <fnmatch.h>
34 #include <libgen.h>
35 #include <uuid.h>
36 #include <util.h>
38 #include "got_error.h"
39 #include "got_repository.h"
40 #include "got_reference.h"
41 #include "got_object.h"
42 #include "got_path.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
45 #include "got_opentemp.h"
46 #include "got_diff.h"
48 #include "got_lib_worktree.h"
49 #include "got_lib_sha1.h"
50 #include "got_lib_fileindex.h"
51 #include "got_lib_inflate.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_object_idset.h"
57 #include "got_lib_diff.h"
59 #ifndef MIN
60 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 #endif
63 #define GOT_MERGE_LABEL_MERGED "merged change"
64 #define GOT_MERGE_LABEL_BASE "3-way merge base"
66 static const struct got_error *
67 create_meta_file(const char *path_got, const char *name, const char *content)
68 {
69 const struct got_error *err = NULL;
70 char *path;
72 if (asprintf(&path, "%s/%s", path_got, name) == -1)
73 return got_error_from_errno("asprintf");
75 err = got_path_create_file(path, content);
76 free(path);
77 return err;
78 }
80 static const struct got_error *
81 update_meta_file(const char *path_got, const char *name, const char *content)
82 {
83 const struct got_error *err = NULL;
84 FILE *tmpfile = NULL;
85 char *tmppath = NULL;
86 char *path = NULL;
88 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
89 err = got_error_from_errno("asprintf");
90 path = NULL;
91 goto done;
92 }
94 err = got_opentemp_named(&tmppath, &tmpfile, path);
95 if (err)
96 goto done;
98 if (content) {
99 int len = fprintf(tmpfile, "%s\n", content);
100 if (len != strlen(content) + 1) {
101 err = got_error_from_errno2("fprintf", tmppath);
102 goto done;
106 if (rename(tmppath, path) != 0) {
107 err = got_error_from_errno3("rename", tmppath, path);
108 unlink(tmppath);
109 goto done;
112 done:
113 if (fclose(tmpfile) != 0 && err == NULL)
114 err = got_error_from_errno2("fclose", tmppath);
115 free(tmppath);
116 return err;
119 static const struct got_error *
120 read_meta_file(char **content, const char *path_got, const char *name)
122 const struct got_error *err = NULL;
123 char *path;
124 int fd = -1;
125 ssize_t n;
126 struct stat sb;
128 *content = NULL;
130 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
131 err = got_error_from_errno("asprintf");
132 path = NULL;
133 goto done;
136 fd = open(path, O_RDONLY | O_NOFOLLOW);
137 if (fd == -1) {
138 if (errno == ENOENT)
139 err = got_error_path(path, GOT_ERR_WORKTREE_META);
140 else
141 err = got_error_from_errno2("open", path);
142 goto done;
144 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
145 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
146 : got_error_from_errno2("flock", path));
147 goto done;
150 if (fstat(fd, &sb) != 0) {
151 err = got_error_from_errno2("fstat", path);
152 goto done;
154 *content = calloc(1, sb.st_size);
155 if (*content == NULL) {
156 err = got_error_from_errno("calloc");
157 goto done;
160 n = read(fd, *content, sb.st_size);
161 if (n != sb.st_size) {
162 err = (n == -1 ? got_error_from_errno2("read", path) :
163 got_error_path(path, GOT_ERR_WORKTREE_META));
164 goto done;
166 if ((*content)[sb.st_size - 1] != '\n') {
167 err = got_error_path(path, GOT_ERR_WORKTREE_META);
168 goto done;
170 (*content)[sb.st_size - 1] = '\0';
172 done:
173 if (fd != -1 && close(fd) == -1 && err == NULL)
174 err = got_error_from_errno2("close", path_got);
175 free(path);
176 if (err) {
177 free(*content);
178 *content = NULL;
180 return err;
183 static const struct got_error *
184 write_head_ref(const char *path_got, struct got_reference *head_ref)
186 const struct got_error *err = NULL;
187 char *refstr = NULL;
189 if (got_ref_is_symbolic(head_ref)) {
190 refstr = got_ref_to_str(head_ref);
191 if (refstr == NULL)
192 return got_error_from_errno("got_ref_to_str");
193 } else {
194 refstr = strdup(got_ref_get_name(head_ref));
195 if (refstr == NULL)
196 return got_error_from_errno("strdup");
198 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
199 free(refstr);
200 return err;
203 const struct got_error *
204 got_worktree_init(const char *path, struct got_reference *head_ref,
205 const char *prefix, struct got_repository *repo)
207 const struct got_error *err = NULL;
208 struct got_object_id *commit_id = NULL;
209 uuid_t uuid;
210 uint32_t uuid_status;
211 int obj_type;
212 char *path_got = NULL;
213 char *formatstr = NULL;
214 char *absprefix = NULL;
215 char *basestr = NULL;
216 char *uuidstr = NULL;
218 if (strcmp(path, got_repo_get_path(repo)) == 0) {
219 err = got_error(GOT_ERR_WORKTREE_REPO);
220 goto done;
223 err = got_ref_resolve(&commit_id, repo, head_ref);
224 if (err)
225 return err;
226 err = got_object_get_type(&obj_type, repo, commit_id);
227 if (err)
228 return err;
229 if (obj_type != GOT_OBJ_TYPE_COMMIT)
230 return got_error(GOT_ERR_OBJ_TYPE);
232 if (!got_path_is_absolute(prefix)) {
233 if (asprintf(&absprefix, "/%s", prefix) == -1)
234 return got_error_from_errno("asprintf");
237 /* Create top-level directory (may already exist). */
238 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
239 err = got_error_from_errno2("mkdir", path);
240 goto done;
243 /* Create .got directory (may already exist). */
244 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
245 err = got_error_from_errno("asprintf");
246 goto done;
248 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
249 err = got_error_from_errno2("mkdir", path_got);
250 goto done;
253 /* Create an empty lock file. */
254 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
255 if (err)
256 goto done;
258 /* Create an empty file index. */
259 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
260 if (err)
261 goto done;
263 /* Write the HEAD reference. */
264 err = write_head_ref(path_got, head_ref);
265 if (err)
266 goto done;
268 /* Record our base commit. */
269 err = got_object_id_str(&basestr, commit_id);
270 if (err)
271 goto done;
272 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
273 if (err)
274 goto done;
276 /* Store path to repository. */
277 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
278 got_repo_get_path(repo));
279 if (err)
280 goto done;
282 /* Store in-repository path prefix. */
283 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
284 absprefix ? absprefix : prefix);
285 if (err)
286 goto done;
288 /* Generate UUID. */
289 uuid_create(&uuid, &uuid_status);
290 if (uuid_status != uuid_s_ok) {
291 err = got_error_uuid(uuid_status, "uuid_create");
292 goto done;
294 uuid_to_string(&uuid, &uuidstr, &uuid_status);
295 if (uuid_status != uuid_s_ok) {
296 err = got_error_uuid(uuid_status, "uuid_to_string");
297 goto done;
299 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
300 if (err)
301 goto done;
303 /* Stamp work tree with format file. */
304 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
305 err = got_error_from_errno("asprintf");
306 goto done;
308 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
309 if (err)
310 goto done;
312 done:
313 free(commit_id);
314 free(path_got);
315 free(formatstr);
316 free(absprefix);
317 free(basestr);
318 free(uuidstr);
319 return err;
322 static const struct got_error *
323 open_worktree(struct got_worktree **worktree, const char *path)
325 const struct got_error *err = NULL;
326 char *path_got;
327 char *formatstr = NULL;
328 char *uuidstr = NULL;
329 char *path_lock = NULL;
330 char *base_commit_id_str = NULL;
331 int version, fd = -1;
332 const char *errstr;
333 struct got_repository *repo = NULL;
334 uint32_t uuid_status;
336 *worktree = NULL;
338 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
339 err = got_error_from_errno("asprintf");
340 path_got = NULL;
341 goto done;
344 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
345 err = got_error_from_errno("asprintf");
346 path_lock = NULL;
347 goto done;
350 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
351 if (fd == -1) {
352 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
353 : got_error_from_errno2("open", path_lock));
354 goto done;
357 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
358 if (err)
359 goto done;
361 version = strtonum(formatstr, 1, INT_MAX, &errstr);
362 if (errstr) {
363 err = got_error_msg(GOT_ERR_WORKTREE_META,
364 "could not parse work tree format version number");
365 goto done;
367 if (version != GOT_WORKTREE_FORMAT_VERSION) {
368 err = got_error(GOT_ERR_WORKTREE_VERS);
369 goto done;
372 *worktree = calloc(1, sizeof(**worktree));
373 if (*worktree == NULL) {
374 err = got_error_from_errno("calloc");
375 goto done;
377 (*worktree)->lockfd = -1;
379 (*worktree)->root_path = strdup(path);
380 if ((*worktree)->root_path == NULL) {
381 err = got_error_from_errno("strdup");
382 goto done;
384 err = read_meta_file(&(*worktree)->repo_path, path_got,
385 GOT_WORKTREE_REPOSITORY);
386 if (err)
387 goto done;
389 err = read_meta_file(&(*worktree)->path_prefix, path_got,
390 GOT_WORKTREE_PATH_PREFIX);
391 if (err)
392 goto done;
394 err = read_meta_file(&base_commit_id_str, path_got,
395 GOT_WORKTREE_BASE_COMMIT);
396 if (err)
397 goto done;
399 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
400 if (err)
401 goto done;
402 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
403 if (uuid_status != uuid_s_ok) {
404 err = got_error_uuid(uuid_status, "uuid_from_string");
405 goto done;
408 err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
409 if (err)
410 goto done;
412 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
413 base_commit_id_str);
414 if (err)
415 goto done;
417 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
418 GOT_WORKTREE_HEAD_REF);
419 done:
420 if (repo)
421 got_repo_close(repo);
422 free(path_got);
423 free(path_lock);
424 free(base_commit_id_str);
425 free(uuidstr);
426 free(formatstr);
427 if (err) {
428 if (fd != -1)
429 close(fd);
430 if (*worktree != NULL)
431 got_worktree_close(*worktree);
432 *worktree = NULL;
433 } else
434 (*worktree)->lockfd = fd;
436 return err;
439 const struct got_error *
440 got_worktree_open(struct got_worktree **worktree, const char *path)
442 const struct got_error *err = NULL;
444 do {
445 err = open_worktree(worktree, path);
446 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
447 return err;
448 if (*worktree)
449 return NULL;
450 path = dirname(path);
451 if (path == NULL)
452 return got_error_from_errno2("dirname", path);
453 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
455 return got_error(GOT_ERR_NOT_WORKTREE);
458 const struct got_error *
459 got_worktree_close(struct got_worktree *worktree)
461 const struct got_error *err = NULL;
462 free(worktree->repo_path);
463 free(worktree->path_prefix);
464 free(worktree->base_commit_id);
465 free(worktree->head_ref_name);
466 if (worktree->lockfd != -1)
467 if (close(worktree->lockfd) != 0)
468 err = got_error_from_errno2("close",
469 got_worktree_get_root_path(worktree));
470 free(worktree->root_path);
471 free(worktree);
472 return err;
475 const char *
476 got_worktree_get_root_path(struct got_worktree *worktree)
478 return worktree->root_path;
481 const char *
482 got_worktree_get_repo_path(struct got_worktree *worktree)
484 return worktree->repo_path;
487 const char *
488 got_worktree_get_path_prefix(struct got_worktree *worktree)
490 return worktree->path_prefix;
493 const struct got_error *
494 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
495 const char *path_prefix)
497 char *absprefix = NULL;
499 if (!got_path_is_absolute(path_prefix)) {
500 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
501 return got_error_from_errno("asprintf");
503 *match = (strcmp(absprefix ? absprefix : path_prefix,
504 worktree->path_prefix) == 0);
505 free(absprefix);
506 return NULL;
509 const char *
510 got_worktree_get_head_ref_name(struct got_worktree *worktree)
512 return worktree->head_ref_name;
515 const struct got_error *
516 got_worktree_set_head_ref(struct got_worktree *worktree,
517 struct got_reference *head_ref)
519 const struct got_error *err = NULL;
520 char *path_got = NULL, *head_ref_name = NULL;
522 if (asprintf(&path_got, "%s/%s", worktree->root_path,
523 GOT_WORKTREE_GOT_DIR) == -1) {
524 err = got_error_from_errno("asprintf");
525 path_got = NULL;
526 goto done;
529 head_ref_name = strdup(got_ref_get_name(head_ref));
530 if (head_ref_name == NULL) {
531 err = got_error_from_errno("strdup");
532 goto done;
535 err = write_head_ref(path_got, head_ref);
536 if (err)
537 goto done;
539 free(worktree->head_ref_name);
540 worktree->head_ref_name = head_ref_name;
541 done:
542 free(path_got);
543 if (err)
544 free(head_ref_name);
545 return err;
548 struct got_object_id *
549 got_worktree_get_base_commit_id(struct got_worktree *worktree)
551 return worktree->base_commit_id;
554 const struct got_error *
555 got_worktree_set_base_commit_id(struct got_worktree *worktree,
556 struct got_repository *repo, struct got_object_id *commit_id)
558 const struct got_error *err;
559 struct got_object *obj = NULL;
560 char *id_str = NULL;
561 char *path_got = NULL;
563 if (asprintf(&path_got, "%s/%s", worktree->root_path,
564 GOT_WORKTREE_GOT_DIR) == -1) {
565 err = got_error_from_errno("asprintf");
566 path_got = NULL;
567 goto done;
570 err = got_object_open(&obj, repo, commit_id);
571 if (err)
572 return err;
574 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
575 err = got_error(GOT_ERR_OBJ_TYPE);
576 goto done;
579 /* Record our base commit. */
580 err = got_object_id_str(&id_str, commit_id);
581 if (err)
582 goto done;
583 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
584 if (err)
585 goto done;
587 free(worktree->base_commit_id);
588 worktree->base_commit_id = got_object_id_dup(commit_id);
589 if (worktree->base_commit_id == NULL) {
590 err = got_error_from_errno("got_object_id_dup");
591 goto done;
593 done:
594 if (obj)
595 got_object_close(obj);
596 free(id_str);
597 free(path_got);
598 return err;
601 static const struct got_error *
602 lock_worktree(struct got_worktree *worktree, int operation)
604 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
605 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
606 : got_error_from_errno2("flock",
607 got_worktree_get_root_path(worktree)));
608 return NULL;
611 static const struct got_error *
612 add_dir_on_disk(struct got_worktree *worktree, const char *path)
614 const struct got_error *err = NULL;
615 char *abspath;
617 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
618 return got_error_from_errno("asprintf");
620 err = got_path_mkdir(abspath);
621 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
622 struct stat sb;
623 err = NULL;
624 if (lstat(abspath, &sb) == -1) {
625 err = got_error_from_errno2("lstat", abspath);
626 } else if (!S_ISDIR(sb.st_mode)) {
627 /* TODO directory is obstructed; do something */
628 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
631 free(abspath);
632 return err;
635 static const struct got_error *
636 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
638 const struct got_error *err = NULL;
639 uint8_t fbuf1[8192];
640 uint8_t fbuf2[8192];
641 size_t flen1 = 0, flen2 = 0;
643 *same = 1;
645 for (;;) {
646 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
647 if (flen1 == 0 && ferror(f1)) {
648 err = got_error_from_errno("fread");
649 break;
651 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
652 if (flen2 == 0 && ferror(f2)) {
653 err = got_error_from_errno("fread");
654 break;
656 if (flen1 == 0) {
657 if (flen2 != 0)
658 *same = 0;
659 break;
660 } else if (flen2 == 0) {
661 if (flen1 != 0)
662 *same = 0;
663 break;
664 } else if (flen1 == flen2) {
665 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
666 *same = 0;
667 break;
669 } else {
670 *same = 0;
671 break;
675 return err;
678 static const struct got_error *
679 check_files_equal(int *same, const char *f1_path, const char *f2_path)
681 const struct got_error *err = NULL;
682 struct stat sb;
683 size_t size1, size2;
684 FILE *f1 = NULL, *f2 = NULL;
686 *same = 1;
688 if (lstat(f1_path, &sb) != 0) {
689 err = got_error_from_errno2("lstat", f1_path);
690 goto done;
692 size1 = sb.st_size;
694 if (lstat(f2_path, &sb) != 0) {
695 err = got_error_from_errno2("lstat", f2_path);
696 goto done;
698 size2 = sb.st_size;
700 if (size1 != size2) {
701 *same = 0;
702 return NULL;
705 f1 = fopen(f1_path, "r");
706 if (f1 == NULL)
707 return got_error_from_errno2("open", f1_path);
709 f2 = fopen(f2_path, "r");
710 if (f2 == NULL) {
711 err = got_error_from_errno2("open", f2_path);
712 goto done;
715 err = check_file_contents_equal(same, f1, f2);
716 done:
717 if (f1 && fclose(f1) != 0 && err == NULL)
718 err = got_error_from_errno("fclose");
719 if (f2 && fclose(f2) != 0 && err == NULL)
720 err = got_error_from_errno("fclose");
722 return err;
725 /*
726 * Perform a 3-way merge where blob_orig acts as the common ancestor,
727 * the file at deriv_path acts as the first derived version, and the
728 * file on disk acts as the second derived version.
729 */
730 static const struct got_error *
731 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
732 struct got_blob_object *blob_orig, const char *ondisk_path,
733 const char *path, uint16_t st_mode, const char *deriv_path,
734 const char *label_orig, const char *label_deriv,
735 struct got_repository *repo,
736 got_worktree_checkout_cb progress_cb, void *progress_arg)
738 const struct got_error *err = NULL;
739 int merged_fd = -1;
740 FILE *f_orig = NULL;
741 char *blob_orig_path = NULL;
742 char *merged_path = NULL, *base_path = NULL;
743 int overlapcnt = 0;
744 char *parent;
746 *local_changes_subsumed = 0;
748 parent = dirname(ondisk_path);
749 if (parent == NULL)
750 return got_error_from_errno2("dirname", ondisk_path);
752 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
753 return got_error_from_errno("asprintf");
755 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
756 if (err)
757 goto done;
759 free(base_path);
760 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
761 err = got_error_from_errno("asprintf");
762 base_path = NULL;
763 goto done;
766 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
767 if (err)
768 goto done;
769 if (blob_orig) {
770 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
771 blob_orig);
772 if (err)
773 goto done;
774 } else {
775 /*
776 * If the file has no blob, this is an "add vs add" conflict,
777 * and we simply use an empty ancestor file to make both files
778 * appear in the merged result in their entirety.
779 */
782 err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
783 blob_orig_path, ondisk_path, label_deriv, label_orig, NULL);
784 if (err)
785 goto done;
787 err = (*progress_cb)(progress_arg,
788 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
789 if (err)
790 goto done;
792 if (fsync(merged_fd) != 0) {
793 err = got_error_from_errno("fsync");
794 goto done;
797 /* Check if a clean merge has subsumed all local changes. */
798 if (overlapcnt == 0) {
799 err = check_files_equal(local_changes_subsumed, deriv_path,
800 merged_path);
801 if (err)
802 goto done;
805 if (chmod(merged_path, st_mode) != 0) {
806 err = got_error_from_errno2("chmod", merged_path);
807 goto done;
810 if (rename(merged_path, ondisk_path) != 0) {
811 err = got_error_from_errno3("rename", merged_path,
812 ondisk_path);
813 unlink(merged_path);
814 goto done;
817 done:
818 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
819 err = got_error_from_errno("close");
820 if (f_orig && fclose(f_orig) != 0 && err == NULL)
821 err = got_error_from_errno("fclose");
822 free(merged_path);
823 free(base_path);
824 if (blob_orig_path) {
825 unlink(blob_orig_path);
826 free(blob_orig_path);
828 return err;
831 /*
832 * Perform a 3-way merge where blob_orig acts as the common ancestor,
833 * blob_deriv acts as the first derived version, and the file on disk
834 * acts as the second derived version.
835 */
836 static const struct got_error *
837 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
838 struct got_blob_object *blob_orig, const char *ondisk_path,
839 const char *path, uint16_t st_mode, const char *label_orig,
840 struct got_blob_object *blob_deriv,
841 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
842 got_worktree_checkout_cb progress_cb, void *progress_arg)
844 const struct got_error *err = NULL;
845 FILE *f_deriv = NULL;
846 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
847 char *label_deriv = NULL, *parent;
849 *local_changes_subsumed = 0;
851 parent = dirname(ondisk_path);
852 if (parent == NULL)
853 return got_error_from_errno2("dirname", ondisk_path);
855 free(base_path);
856 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
857 err = got_error_from_errno("asprintf");
858 base_path = NULL;
859 goto done;
862 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
863 if (err)
864 goto done;
865 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
866 blob_deriv);
867 if (err)
868 goto done;
870 err = got_object_id_str(&id_str, deriv_base_commit_id);
871 if (err)
872 goto done;
873 if (asprintf(&label_deriv, "%s: commit %s",
874 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
875 err = got_error_from_errno("asprintf");
876 goto done;
879 err = merge_file(local_changes_subsumed, worktree, blob_orig,
880 ondisk_path, path, st_mode, blob_deriv_path, label_orig,
881 label_deriv, repo, progress_cb, progress_arg);
882 done:
883 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
884 err = got_error_from_errno("fclose");
885 free(base_path);
886 if (blob_deriv_path) {
887 unlink(blob_deriv_path);
888 free(blob_deriv_path);
890 free(id_str);
891 free(label_deriv);
892 return err;
895 static const struct got_error *
896 update_blob_fileindex_entry(struct got_worktree *worktree,
897 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
898 const char *ondisk_path, const char *path, struct got_blob_object *blob,
899 int update_timestamps)
901 const struct got_error *err = NULL;
903 if (ie == NULL)
904 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
905 if (ie)
906 err = got_fileindex_entry_update(ie, ondisk_path,
907 blob->id.sha1, worktree->base_commit_id->sha1,
908 update_timestamps);
909 else {
910 struct got_fileindex_entry *new_ie;
911 err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
912 path, blob->id.sha1, worktree->base_commit_id->sha1);
913 if (!err)
914 err = got_fileindex_entry_add(fileindex, new_ie);
916 return err;
919 static mode_t
920 get_ondisk_perms(int executable, mode_t st_mode)
922 mode_t xbits = S_IXUSR;
924 if (executable) {
925 /* Map read bits to execute bits. */
926 if (st_mode & S_IRGRP)
927 xbits |= S_IXGRP;
928 if (st_mode & S_IROTH)
929 xbits |= S_IXOTH;
930 return st_mode | xbits;
933 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
936 static const struct got_error *
937 install_blob(struct got_worktree *worktree, const char *ondisk_path,
938 const char *path, uint16_t te_mode, uint16_t st_mode,
939 struct got_blob_object *blob, int restoring_missing_file,
940 int reverting_versioned_file, struct got_repository *repo,
941 got_worktree_checkout_cb progress_cb, void *progress_arg)
943 const struct got_error *err = NULL;
944 int fd = -1;
945 size_t len, hdrlen;
946 int update = 0;
947 char *tmppath = NULL;
949 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
950 GOT_DEFAULT_FILE_MODE);
951 if (fd == -1) {
952 if (errno == ENOENT) {
953 char *parent = dirname(path);
954 if (parent == NULL)
955 return got_error_from_errno2("dirname", path);
956 err = add_dir_on_disk(worktree, parent);
957 if (err)
958 return err;
959 fd = open(ondisk_path,
960 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
961 GOT_DEFAULT_FILE_MODE);
962 if (fd == -1)
963 return got_error_from_errno2("open",
964 ondisk_path);
965 } else if (errno == EEXIST) {
966 if (!S_ISREG(st_mode)) {
967 /* TODO file is obstructed; do something */
968 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
969 goto done;
970 } else {
971 err = got_opentemp_named_fd(&tmppath, &fd,
972 ondisk_path);
973 if (err)
974 goto done;
975 update = 1;
977 } else
978 return got_error_from_errno2("open", ondisk_path);
981 if (restoring_missing_file)
982 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
983 else if (reverting_versioned_file)
984 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
985 else
986 err = (*progress_cb)(progress_arg,
987 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
988 if (err)
989 goto done;
991 hdrlen = got_object_blob_get_hdrlen(blob);
992 do {
993 const uint8_t *buf = got_object_blob_get_read_buf(blob);
994 err = got_object_blob_read_block(&len, blob);
995 if (err)
996 break;
997 if (len > 0) {
998 /* Skip blob object header first time around. */
999 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1000 if (outlen == -1) {
1001 err = got_error_from_errno("write");
1002 goto done;
1003 } else if (outlen != len - hdrlen) {
1004 err = got_error(GOT_ERR_IO);
1005 goto done;
1007 hdrlen = 0;
1009 } while (len != 0);
1011 if (fsync(fd) != 0) {
1012 err = got_error_from_errno("fsync");
1013 goto done;
1016 if (update) {
1017 if (rename(tmppath, ondisk_path) != 0) {
1018 err = got_error_from_errno3("rename", tmppath,
1019 ondisk_path);
1020 unlink(tmppath);
1021 goto done;
1025 if (chmod(ondisk_path,
1026 get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1027 err = got_error_from_errno2("chmod", ondisk_path);
1028 goto done;
1031 done:
1032 if (fd != -1 && close(fd) != 0 && err == NULL)
1033 err = got_error_from_errno("close");
1034 free(tmppath);
1035 return err;
1038 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1039 static const struct got_error *
1040 get_modified_file_content_status(unsigned char *status, FILE *f)
1042 const struct got_error *err = NULL;
1043 const char *markers[3] = {
1044 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1045 GOT_DIFF_CONFLICT_MARKER_SEP,
1046 GOT_DIFF_CONFLICT_MARKER_END
1048 int i = 0;
1049 char *line;
1050 size_t len;
1051 const char delim[3] = {'\0', '\0', '\0'};
1053 while (*status == GOT_STATUS_MODIFY) {
1054 line = fparseln(f, &len, NULL, delim, 0);
1055 if (line == NULL) {
1056 if (feof(f))
1057 break;
1058 err = got_ferror(f, GOT_ERR_IO);
1059 break;
1062 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1063 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1064 == 0)
1065 *status = GOT_STATUS_CONFLICT;
1066 else
1067 i++;
1071 return err;
1074 static int
1075 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1077 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1078 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1081 static int
1082 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1084 return !(ie->ctime_sec == sb->st_ctime &&
1085 ie->ctime_nsec == sb->st_ctimensec &&
1086 ie->mtime_sec == sb->st_mtime &&
1087 ie->mtime_nsec == sb->st_mtimensec &&
1088 ie->size == (sb->st_size & 0xffffffff) &&
1089 !xbit_differs(ie, sb->st_mode));
1092 static unsigned char
1093 get_staged_status(struct got_fileindex_entry *ie)
1095 switch (got_fileindex_entry_stage_get(ie)) {
1096 case GOT_FILEIDX_STAGE_ADD:
1097 return GOT_STATUS_ADD;
1098 case GOT_FILEIDX_STAGE_DELETE:
1099 return GOT_STATUS_DELETE;
1100 case GOT_FILEIDX_STAGE_MODIFY:
1101 return GOT_STATUS_MODIFY;
1102 default:
1103 return GOT_STATUS_NO_CHANGE;
1107 static const struct got_error *
1108 get_file_status(unsigned char *status, struct stat *sb,
1109 struct got_fileindex_entry *ie, const char *abspath,
1110 struct got_repository *repo)
1112 const struct got_error *err = NULL;
1113 struct got_object_id id;
1114 size_t hdrlen;
1115 FILE *f = NULL;
1116 uint8_t fbuf[8192];
1117 struct got_blob_object *blob = NULL;
1118 size_t flen, blen;
1119 unsigned char staged_status = get_staged_status(ie);
1121 *status = GOT_STATUS_NO_CHANGE;
1123 if (lstat(abspath, sb) == -1) {
1124 if (errno == ENOENT) {
1125 if (got_fileindex_entry_has_file_on_disk(ie))
1126 *status = GOT_STATUS_MISSING;
1127 else
1128 *status = GOT_STATUS_DELETE;
1129 return NULL;
1131 return got_error_from_errno2("lstat", abspath);
1134 if (!S_ISREG(sb->st_mode)) {
1135 *status = GOT_STATUS_OBSTRUCTED;
1136 return NULL;
1139 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1140 *status = GOT_STATUS_DELETE;
1141 return NULL;
1142 } else if (!got_fileindex_entry_has_blob(ie) &&
1143 staged_status != GOT_STATUS_ADD) {
1144 *status = GOT_STATUS_ADD;
1145 return NULL;
1148 if (!stat_info_differs(ie, sb))
1149 return NULL;
1151 if (staged_status == GOT_STATUS_MODIFY ||
1152 staged_status == GOT_STATUS_ADD)
1153 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1154 else
1155 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1157 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1158 if (err)
1159 return err;
1161 f = fopen(abspath, "r");
1162 if (f == NULL) {
1163 err = got_error_from_errno2("fopen", abspath);
1164 goto done;
1166 hdrlen = got_object_blob_get_hdrlen(blob);
1167 for (;;) {
1168 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1169 err = got_object_blob_read_block(&blen, blob);
1170 if (err)
1171 goto done;
1172 /* Skip length of blob object header first time around. */
1173 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1174 if (flen == 0 && ferror(f)) {
1175 err = got_error_from_errno("fread");
1176 goto done;
1178 if (blen == 0) {
1179 if (flen != 0)
1180 *status = GOT_STATUS_MODIFY;
1181 break;
1182 } else if (flen == 0) {
1183 if (blen != 0)
1184 *status = GOT_STATUS_MODIFY;
1185 break;
1186 } else if (blen - hdrlen == flen) {
1187 /* Skip blob object header first time around. */
1188 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1189 *status = GOT_STATUS_MODIFY;
1190 break;
1192 } else {
1193 *status = GOT_STATUS_MODIFY;
1194 break;
1196 hdrlen = 0;
1199 if (*status == GOT_STATUS_MODIFY) {
1200 rewind(f);
1201 err = get_modified_file_content_status(status, f);
1202 } else if (xbit_differs(ie, sb->st_mode))
1203 *status = GOT_STATUS_MODE_CHANGE;
1204 done:
1205 if (blob)
1206 got_object_blob_close(blob);
1207 if (f)
1208 fclose(f);
1209 return err;
1213 * Update timestamps in the file index if a file is unmodified and
1214 * we had to run a full content comparison to find out.
1216 static const struct got_error *
1217 sync_timestamps(char *ondisk_path, unsigned char status,
1218 struct got_fileindex_entry *ie, struct stat *sb)
1220 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1221 return got_fileindex_entry_update(ie, ondisk_path,
1222 ie->blob_sha1, ie->commit_sha1, 1);
1224 return NULL;
1227 static const struct got_error *
1228 update_blob(struct got_worktree *worktree,
1229 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1230 struct got_tree_entry *te, const char *path,
1231 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1232 void *progress_arg)
1234 const struct got_error *err = NULL;
1235 struct got_blob_object *blob = NULL;
1236 char *ondisk_path;
1237 unsigned char status = GOT_STATUS_NO_CHANGE;
1238 struct stat sb;
1240 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1241 return got_error_from_errno("asprintf");
1243 if (ie) {
1244 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1245 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1246 goto done;
1248 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1249 if (err)
1250 goto done;
1251 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1252 sb.st_mode = got_fileindex_perms_to_st(ie);
1253 } else
1254 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1256 if (status == GOT_STATUS_OBSTRUCTED) {
1257 err = (*progress_cb)(progress_arg, status, path);
1258 goto done;
1261 if (ie && status != GOT_STATUS_MISSING &&
1262 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1263 if (got_fileindex_entry_has_commit(ie) &&
1264 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1265 SHA1_DIGEST_LENGTH) == 0) {
1266 err = sync_timestamps(ondisk_path, status, ie, &sb);
1267 if (err)
1268 goto done;
1269 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1270 path);
1271 goto done;
1273 if (got_fileindex_entry_has_blob(ie) &&
1274 memcmp(ie->blob_sha1, te->id.sha1,
1275 SHA1_DIGEST_LENGTH) == 0) {
1276 err = sync_timestamps(ondisk_path, status, ie, &sb);
1277 goto done;
1281 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1282 if (err)
1283 goto done;
1285 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1286 int update_timestamps;
1287 struct got_blob_object *blob2 = NULL;
1288 char *label_orig = NULL;
1289 if (got_fileindex_entry_has_blob(ie)) {
1290 struct got_object_id id2;
1291 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1292 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1293 if (err)
1294 goto done;
1296 if (got_fileindex_entry_has_commit(ie)) {
1297 char id_str[SHA1_DIGEST_STRING_LENGTH];
1298 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1299 sizeof(id_str)) == NULL) {
1300 err = got_error_path(id_str,
1301 GOT_ERR_BAD_OBJ_ID_STR);
1302 goto done;
1304 if (asprintf(&label_orig, "%s: commit %s",
1305 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1306 err = got_error_from_errno("asprintf");
1307 goto done;
1310 err = merge_blob(&update_timestamps, worktree, blob2,
1311 ondisk_path, path, sb.st_mode, label_orig, blob,
1312 worktree->base_commit_id, repo,
1313 progress_cb, progress_arg);
1314 free(label_orig);
1315 if (blob2)
1316 got_object_blob_close(blob2);
1317 if (err)
1318 goto done;
1320 * Do not update timestamps of files with local changes.
1321 * Otherwise, a future status walk would treat them as
1322 * unmodified files again.
1324 err = got_fileindex_entry_update(ie, ondisk_path,
1325 blob->id.sha1, worktree->base_commit_id->sha1,
1326 update_timestamps);
1327 } else if (status == GOT_STATUS_MODE_CHANGE) {
1328 err = got_fileindex_entry_update(ie, ondisk_path,
1329 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1330 } else if (status == GOT_STATUS_DELETE) {
1331 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1332 if (err)
1333 goto done;
1334 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1335 ondisk_path, path, blob, 0);
1336 if (err)
1337 goto done;
1338 } else {
1339 err = install_blob(worktree, ondisk_path, path, te->mode,
1340 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1341 repo, progress_cb, progress_arg);
1342 if (err)
1343 goto done;
1344 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1345 ondisk_path, path, blob, 1);
1346 if (err)
1347 goto done;
1349 got_object_blob_close(blob);
1350 done:
1351 free(ondisk_path);
1352 return err;
1355 static const struct got_error *
1356 remove_ondisk_file(const char *root_path, const char *path)
1358 const struct got_error *err = NULL;
1359 char *ondisk_path = NULL;
1361 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1362 return got_error_from_errno("asprintf");
1364 if (unlink(ondisk_path) == -1) {
1365 if (errno != ENOENT)
1366 err = got_error_from_errno2("unlink", ondisk_path);
1367 } else {
1368 char *parent = dirname(ondisk_path);
1369 while (parent && strcmp(parent, root_path) != 0) {
1370 if (rmdir(parent) == -1) {
1371 if (errno != ENOTEMPTY)
1372 err = got_error_from_errno2("rmdir",
1373 parent);
1374 break;
1376 parent = dirname(parent);
1379 free(ondisk_path);
1380 return err;
1383 static const struct got_error *
1384 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1385 struct got_fileindex_entry *ie, struct got_repository *repo,
1386 got_worktree_checkout_cb progress_cb, void *progress_arg)
1388 const struct got_error *err = NULL;
1389 unsigned char status;
1390 struct stat sb;
1391 char *ondisk_path;
1393 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1394 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1396 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1397 == -1)
1398 return got_error_from_errno("asprintf");
1400 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1401 if (err)
1402 return err;
1404 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1405 status == GOT_STATUS_ADD) {
1406 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1407 if (err)
1408 return err;
1410 * Preserve the working file and change the deleted blob's
1411 * entry into a schedule-add entry.
1413 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1414 0);
1415 if (err)
1416 return err;
1417 } else {
1418 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1419 if (err)
1420 return err;
1421 if (status == GOT_STATUS_NO_CHANGE) {
1422 err = remove_ondisk_file(worktree->root_path, ie->path);
1423 if (err)
1424 return err;
1426 got_fileindex_entry_remove(fileindex, ie);
1429 return err;
1432 struct diff_cb_arg {
1433 struct got_fileindex *fileindex;
1434 struct got_worktree *worktree;
1435 struct got_repository *repo;
1436 got_worktree_checkout_cb progress_cb;
1437 void *progress_arg;
1438 got_cancel_cb cancel_cb;
1439 void *cancel_arg;
1442 static const struct got_error *
1443 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1444 struct got_tree_entry *te, const char *parent_path)
1446 struct diff_cb_arg *a = arg;
1448 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1449 return got_error(GOT_ERR_CANCELLED);
1451 return update_blob(a->worktree, a->fileindex, ie, te,
1452 ie->path, a->repo, a->progress_cb, a->progress_arg);
1455 static const struct got_error *
1456 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1458 struct diff_cb_arg *a = arg;
1460 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1461 return got_error(GOT_ERR_CANCELLED);
1463 return delete_blob(a->worktree, a->fileindex, ie,
1464 a->repo, a->progress_cb, a->progress_arg);
1467 static const struct got_error *
1468 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1470 struct diff_cb_arg *a = arg;
1471 const struct got_error *err;
1472 char *path;
1474 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1475 return got_error(GOT_ERR_CANCELLED);
1477 if (got_object_tree_entry_is_submodule(te))
1478 return NULL;
1480 if (asprintf(&path, "%s%s%s", parent_path,
1481 parent_path[0] ? "/" : "", te->name)
1482 == -1)
1483 return got_error_from_errno("asprintf");
1485 if (S_ISDIR(te->mode))
1486 err = add_dir_on_disk(a->worktree, path);
1487 else
1488 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1489 a->repo, a->progress_cb, a->progress_arg);
1491 free(path);
1492 return err;
1495 static const struct got_error *
1496 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1498 const struct got_error *err = NULL;
1499 char *uuidstr = NULL;
1500 uint32_t uuid_status;
1502 *refname = NULL;
1504 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1505 if (uuid_status != uuid_s_ok)
1506 return got_error_uuid(uuid_status, "uuid_to_string");
1508 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1509 == -1) {
1510 err = got_error_from_errno("asprintf");
1511 *refname = NULL;
1513 free(uuidstr);
1514 return err;
1517 const struct got_error *
1518 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1520 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1523 static const struct got_error *
1524 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1526 return get_ref_name(refname, worktree,
1527 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1530 static const struct got_error *
1531 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1533 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1536 static const struct got_error *
1537 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1539 return get_ref_name(refname, worktree,
1540 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1543 static const struct got_error *
1544 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1546 return get_ref_name(refname, worktree,
1547 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1550 static const struct got_error *
1551 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1553 return get_ref_name(refname, worktree,
1554 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1557 static const struct got_error *
1558 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1560 return get_ref_name(refname, worktree,
1561 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1564 static const struct got_error *
1565 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1567 return get_ref_name(refname, worktree,
1568 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1571 static const struct got_error *
1572 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1574 return get_ref_name(refname, worktree,
1575 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1578 const struct got_error *
1579 got_worktree_get_histedit_script_path(char **path,
1580 struct got_worktree *worktree)
1582 if (asprintf(path, "%s/%s/%s", worktree->root_path,
1583 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
1584 *path = NULL;
1585 return got_error_from_errno("asprintf");
1587 return NULL;
1591 * Prevent Git's garbage collector from deleting our base commit by
1592 * setting a reference to our base commit's ID.
1594 static const struct got_error *
1595 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1597 const struct got_error *err = NULL;
1598 struct got_reference *ref = NULL;
1599 char *refname;
1601 err = got_worktree_get_base_ref_name(&refname, worktree);
1602 if (err)
1603 return err;
1605 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1606 if (err)
1607 goto done;
1609 err = got_ref_write(ref, repo);
1610 done:
1611 free(refname);
1612 if (ref)
1613 got_ref_close(ref);
1614 return err;
1617 static const struct got_error *
1618 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
1620 const struct got_error *err = NULL;
1622 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1623 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1624 err = got_error_from_errno("asprintf");
1625 *fileindex_path = NULL;
1627 return err;
1631 static const struct got_error *
1632 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1633 struct got_worktree *worktree)
1635 const struct got_error *err = NULL;
1636 FILE *index = NULL;
1638 *fileindex_path = NULL;
1639 *fileindex = got_fileindex_alloc();
1640 if (*fileindex == NULL)
1641 return got_error_from_errno("got_fileindex_alloc");
1643 err = get_fileindex_path(fileindex_path, worktree);
1644 if (err)
1645 goto done;
1647 index = fopen(*fileindex_path, "rb");
1648 if (index == NULL) {
1649 if (errno != ENOENT)
1650 err = got_error_from_errno2("fopen", *fileindex_path);
1651 } else {
1652 err = got_fileindex_read(*fileindex, index);
1653 if (fclose(index) != 0 && err == NULL)
1654 err = got_error_from_errno("fclose");
1656 done:
1657 if (err) {
1658 free(*fileindex_path);
1659 *fileindex_path = NULL;
1660 got_fileindex_free(*fileindex);
1661 *fileindex = NULL;
1663 return err;
1666 struct bump_base_commit_id_arg {
1667 struct got_object_id *base_commit_id;
1668 const char *path;
1669 size_t path_len;
1670 const char *entry_name;
1671 got_worktree_checkout_cb progress_cb;
1672 void *progress_arg;
1675 /* Bump base commit ID of all files within an updated part of the work tree. */
1676 static const struct got_error *
1677 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1679 const struct got_error *err;
1680 struct bump_base_commit_id_arg *a = arg;
1682 if (a->entry_name) {
1683 if (strcmp(ie->path, a->path) != 0)
1684 return NULL;
1685 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1686 return NULL;
1688 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1689 SHA1_DIGEST_LENGTH) == 0)
1690 return NULL;
1692 if (a->progress_cb) {
1693 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1694 ie->path);
1695 if (err)
1696 return err;
1698 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1699 return NULL;
1702 static const struct got_error *
1703 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1705 const struct got_error *err = NULL;
1706 char *new_fileindex_path = NULL;
1707 FILE *new_index = NULL;
1709 err = got_opentemp_named(&new_fileindex_path, &new_index,
1710 fileindex_path);
1711 if (err)
1712 goto done;
1714 err = got_fileindex_write(fileindex, new_index);
1715 if (err)
1716 goto done;
1718 if (rename(new_fileindex_path, fileindex_path) != 0) {
1719 err = got_error_from_errno3("rename", new_fileindex_path,
1720 fileindex_path);
1721 unlink(new_fileindex_path);
1723 done:
1724 if (new_index)
1725 fclose(new_index);
1726 free(new_fileindex_path);
1727 return err;
1730 static const struct got_error *
1731 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1732 struct got_object_id **tree_id, const char *wt_relpath,
1733 struct got_worktree *worktree, struct got_repository *repo)
1735 const struct got_error *err = NULL;
1736 struct got_object_id *id = NULL;
1737 char *in_repo_path = NULL;
1738 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1740 *entry_type = GOT_OBJ_TYPE_ANY;
1741 *tree_relpath = NULL;
1742 *tree_id = NULL;
1744 if (wt_relpath[0] == '\0') {
1745 /* Check out all files within the work tree. */
1746 *entry_type = GOT_OBJ_TYPE_TREE;
1747 *tree_relpath = strdup("");
1748 if (*tree_relpath == NULL) {
1749 err = got_error_from_errno("strdup");
1750 goto done;
1752 err = got_object_id_by_path(tree_id, repo,
1753 worktree->base_commit_id, worktree->path_prefix);
1754 if (err)
1755 goto done;
1756 return NULL;
1759 /* Check out a subset of files in the work tree. */
1761 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1762 is_root_wt ? "" : "/", wt_relpath) == -1) {
1763 err = got_error_from_errno("asprintf");
1764 goto done;
1767 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
1768 in_repo_path);
1769 if (err)
1770 goto done;
1772 free(in_repo_path);
1773 in_repo_path = NULL;
1775 err = got_object_get_type(entry_type, repo, id);
1776 if (err)
1777 goto done;
1779 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
1780 /* Check out a single file. */
1781 if (strchr(wt_relpath, '/') == NULL) {
1782 /* Check out a single file in work tree's root dir. */
1783 in_repo_path = strdup(worktree->path_prefix);
1784 if (in_repo_path == NULL) {
1785 err = got_error_from_errno("strdup");
1786 goto done;
1788 *tree_relpath = strdup("");
1789 if (*tree_relpath == NULL) {
1790 err = got_error_from_errno("strdup");
1791 goto done;
1793 } else {
1794 /* Check out a single file in a subdirectory. */
1795 err = got_path_dirname(tree_relpath, wt_relpath);
1796 if (err)
1797 return err;
1798 if (asprintf(&in_repo_path, "%s%s%s",
1799 worktree->path_prefix, is_root_wt ? "" : "/",
1800 *tree_relpath) == -1) {
1801 err = got_error_from_errno("asprintf");
1802 goto done;
1805 err = got_object_id_by_path(tree_id, repo,
1806 worktree->base_commit_id, in_repo_path);
1807 } else {
1808 /* Check out all files within a subdirectory. */
1809 *tree_id = got_object_id_dup(id);
1810 if (*tree_id == NULL) {
1811 err = got_error_from_errno("got_object_id_dup");
1812 goto done;
1814 *tree_relpath = strdup(wt_relpath);
1815 if (*tree_relpath == NULL) {
1816 err = got_error_from_errno("strdup");
1817 goto done;
1820 done:
1821 free(id);
1822 free(in_repo_path);
1823 if (err) {
1824 *entry_type = GOT_OBJ_TYPE_ANY;
1825 free(*tree_relpath);
1826 *tree_relpath = NULL;
1827 free(*tree_id);
1828 *tree_id = NULL;
1830 return err;
1833 static const struct got_error *
1834 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1835 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
1836 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1837 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
1839 const struct got_error *err = NULL;
1840 struct got_commit_object *commit = NULL;
1841 struct got_tree_object *tree = NULL;
1842 struct got_fileindex_diff_tree_cb diff_cb;
1843 struct diff_cb_arg arg;
1845 err = ref_base_commit(worktree, repo);
1846 if (err)
1847 goto done;
1849 err = got_object_open_as_commit(&commit, repo,
1850 worktree->base_commit_id);
1851 if (err)
1852 goto done;
1854 err = got_object_open_as_tree(&tree, repo, tree_id);
1855 if (err)
1856 goto done;
1858 if (entry_name &&
1859 got_object_tree_find_entry(tree, entry_name) == NULL) {
1860 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1861 goto done;
1864 diff_cb.diff_old_new = diff_old_new;
1865 diff_cb.diff_old = diff_old;
1866 diff_cb.diff_new = diff_new;
1867 arg.fileindex = fileindex;
1868 arg.worktree = worktree;
1869 arg.repo = repo;
1870 arg.progress_cb = progress_cb;
1871 arg.progress_arg = progress_arg;
1872 arg.cancel_cb = cancel_cb;
1873 arg.cancel_arg = cancel_arg;
1874 err = got_fileindex_diff_tree(fileindex, tree, relpath,
1875 entry_name, repo, &diff_cb, &arg);
1876 done:
1877 if (tree)
1878 got_object_tree_close(tree);
1879 if (commit)
1880 got_object_commit_close(commit);
1881 return err;
1884 const struct got_error *
1885 got_worktree_checkout_files(struct got_worktree *worktree,
1886 struct got_pathlist_head *paths, struct got_repository *repo,
1887 got_worktree_checkout_cb progress_cb, void *progress_arg,
1888 got_cancel_cb cancel_cb, void *cancel_arg)
1890 const struct got_error *err = NULL, *sync_err, *unlockerr;
1891 struct got_commit_object *commit = NULL;
1892 struct got_tree_object *tree = NULL;
1893 struct got_fileindex *fileindex = NULL;
1894 char *fileindex_path = NULL;
1895 struct got_pathlist_entry *pe;
1896 struct tree_path_data {
1897 SIMPLEQ_ENTRY(tree_path_data) entry;
1898 struct got_object_id *tree_id;
1899 int entry_type;
1900 char *relpath;
1901 char *entry_name;
1902 } *tpd = NULL;
1903 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
1905 SIMPLEQ_INIT(&tree_paths);
1907 err = lock_worktree(worktree, LOCK_EX);
1908 if (err)
1909 return err;
1911 /* Map all specified paths to in-repository trees. */
1912 TAILQ_FOREACH(pe, paths, entry) {
1913 tpd = malloc(sizeof(*tpd));
1914 if (tpd == NULL) {
1915 err = got_error_from_errno("malloc");
1916 goto done;
1919 err = find_tree_entry_for_checkout(&tpd->entry_type,
1920 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
1921 if (err) {
1922 free(tpd);
1923 goto done;
1926 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
1927 err = got_path_basename(&tpd->entry_name, pe->path);
1928 if (err) {
1929 free(tpd->relpath);
1930 free(tpd->tree_id);
1931 free(tpd);
1932 goto done;
1934 } else
1935 tpd->entry_name = NULL;
1937 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
1941 * Read the file index.
1942 * Checking out files is supposed to be an idempotent operation.
1943 * If the on-disk file index is incomplete we will try to complete it.
1945 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1946 if (err)
1947 goto done;
1949 tpd = SIMPLEQ_FIRST(&tree_paths);
1950 TAILQ_FOREACH(pe, paths, entry) {
1951 struct bump_base_commit_id_arg bbc_arg;
1953 err = checkout_files(worktree, fileindex, tpd->relpath,
1954 tpd->tree_id, tpd->entry_name, repo,
1955 progress_cb, progress_arg, cancel_cb, cancel_arg);
1956 if (err)
1957 break;
1959 bbc_arg.base_commit_id = worktree->base_commit_id;
1960 bbc_arg.entry_name = tpd->entry_name;
1961 bbc_arg.path = pe->path;
1962 bbc_arg.path_len = pe->path_len;
1963 bbc_arg.progress_cb = progress_cb;
1964 bbc_arg.progress_arg = progress_arg;
1965 err = got_fileindex_for_each_entry_safe(fileindex,
1966 bump_base_commit_id, &bbc_arg);
1967 if (err)
1968 break;
1970 tpd = SIMPLEQ_NEXT(tpd, entry);
1972 sync_err = sync_fileindex(fileindex, fileindex_path);
1973 if (sync_err && err == NULL)
1974 err = sync_err;
1975 done:
1976 free(fileindex_path);
1977 if (tree)
1978 got_object_tree_close(tree);
1979 if (commit)
1980 got_object_commit_close(commit);
1981 if (fileindex)
1982 got_fileindex_free(fileindex);
1983 while (!SIMPLEQ_EMPTY(&tree_paths)) {
1984 tpd = SIMPLEQ_FIRST(&tree_paths);
1985 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
1986 free(tpd->relpath);
1987 free(tpd->tree_id);
1988 free(tpd);
1990 unlockerr = lock_worktree(worktree, LOCK_SH);
1991 if (unlockerr && err == NULL)
1992 err = unlockerr;
1993 return err;
1996 struct merge_file_cb_arg {
1997 struct got_worktree *worktree;
1998 struct got_fileindex *fileindex;
1999 got_worktree_checkout_cb progress_cb;
2000 void *progress_arg;
2001 got_cancel_cb cancel_cb;
2002 void *cancel_arg;
2003 const char *label_orig;
2004 struct got_object_id *commit_id2;
2007 static const struct got_error *
2008 merge_file_cb(void *arg, struct got_blob_object *blob1,
2009 struct got_blob_object *blob2, struct got_object_id *id1,
2010 struct got_object_id *id2, const char *path1, const char *path2,
2011 mode_t mode1, mode_t mode2, struct got_repository *repo)
2013 static const struct got_error *err = NULL;
2014 struct merge_file_cb_arg *a = arg;
2015 struct got_fileindex_entry *ie;
2016 char *ondisk_path = NULL;
2017 struct stat sb;
2018 unsigned char status;
2019 int local_changes_subsumed;
2021 if (blob1 && blob2) {
2022 ie = got_fileindex_entry_get(a->fileindex, path2,
2023 strlen(path2));
2024 if (ie == NULL)
2025 return (*a->progress_cb)(a->progress_arg,
2026 GOT_STATUS_MISSING, path2);
2028 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2029 path2) == -1)
2030 return got_error_from_errno("asprintf");
2032 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2033 if (err)
2034 goto done;
2036 if (status == GOT_STATUS_DELETE) {
2037 err = (*a->progress_cb)(a->progress_arg,
2038 GOT_STATUS_MERGE, path2);
2039 goto done;
2041 if (status != GOT_STATUS_NO_CHANGE &&
2042 status != GOT_STATUS_MODIFY &&
2043 status != GOT_STATUS_CONFLICT &&
2044 status != GOT_STATUS_ADD) {
2045 err = (*a->progress_cb)(a->progress_arg, status, path2);
2046 goto done;
2049 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
2050 ondisk_path, path2, sb.st_mode, a->label_orig, blob2,
2051 a->commit_id2, repo, a->progress_cb, a->progress_arg);
2052 } else if (blob1) {
2053 ie = got_fileindex_entry_get(a->fileindex, path1,
2054 strlen(path1));
2055 if (ie == NULL)
2056 return (*a->progress_cb)(a->progress_arg,
2057 GOT_STATUS_MISSING, path2);
2059 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2060 path1) == -1)
2061 return got_error_from_errno("asprintf");
2063 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2064 if (err)
2065 goto done;
2067 switch (status) {
2068 case GOT_STATUS_NO_CHANGE:
2069 err = (*a->progress_cb)(a->progress_arg,
2070 GOT_STATUS_DELETE, path1);
2071 if (err)
2072 goto done;
2073 err = remove_ondisk_file(a->worktree->root_path, path1);
2074 if (err)
2075 goto done;
2076 if (ie)
2077 got_fileindex_entry_mark_deleted_from_disk(ie);
2078 break;
2079 case GOT_STATUS_DELETE:
2080 case GOT_STATUS_MISSING:
2081 err = (*a->progress_cb)(a->progress_arg,
2082 GOT_STATUS_DELETE, path1);
2083 if (err)
2084 goto done;
2085 if (ie)
2086 got_fileindex_entry_mark_deleted_from_disk(ie);
2087 break;
2088 case GOT_STATUS_ADD:
2089 case GOT_STATUS_MODIFY:
2090 case GOT_STATUS_CONFLICT:
2091 err = (*a->progress_cb)(a->progress_arg,
2092 GOT_STATUS_CANNOT_DELETE, path1);
2093 if (err)
2094 goto done;
2095 break;
2096 case GOT_STATUS_OBSTRUCTED:
2097 err = (*a->progress_cb)(a->progress_arg, status, path1);
2098 if (err)
2099 goto done;
2100 break;
2101 default:
2102 break;
2104 } else if (blob2) {
2105 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2106 path2) == -1)
2107 return got_error_from_errno("asprintf");
2108 ie = got_fileindex_entry_get(a->fileindex, path2,
2109 strlen(path2));
2110 if (ie) {
2111 err = get_file_status(&status, &sb, ie, ondisk_path,
2112 repo);
2113 if (err)
2114 goto done;
2115 if (status != GOT_STATUS_NO_CHANGE &&
2116 status != GOT_STATUS_MODIFY &&
2117 status != GOT_STATUS_CONFLICT &&
2118 status != GOT_STATUS_ADD) {
2119 err = (*a->progress_cb)(a->progress_arg,
2120 status, path2);
2121 goto done;
2123 err = merge_blob(&local_changes_subsumed, a->worktree,
2124 NULL, ondisk_path, path2, sb.st_mode,
2125 a->label_orig, blob2, a->commit_id2, repo,
2126 a->progress_cb,
2127 a->progress_arg);
2128 if (status == GOT_STATUS_DELETE) {
2129 err = update_blob_fileindex_entry(a->worktree,
2130 a->fileindex, ie, ondisk_path, ie->path,
2131 blob2, 0);
2132 if (err)
2133 goto done;
2135 } else {
2136 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2137 err = install_blob(a->worktree, ondisk_path, path2,
2138 /* XXX get this from parent tree! */
2139 GOT_DEFAULT_FILE_MODE,
2140 sb.st_mode, blob2, 0, 0, repo,
2141 a->progress_cb, a->progress_arg);
2142 if (err)
2143 goto done;
2144 err = got_fileindex_entry_alloc(&ie,
2145 ondisk_path, path2, NULL, NULL);
2146 if (err)
2147 goto done;
2148 err = got_fileindex_entry_add(a->fileindex, ie);
2149 if (err) {
2150 got_fileindex_entry_free(ie);
2151 goto done;
2155 done:
2156 free(ondisk_path);
2157 return err;
2160 struct check_merge_ok_arg {
2161 struct got_worktree *worktree;
2162 struct got_repository *repo;
2165 static const struct got_error *
2166 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2168 const struct got_error *err = NULL;
2169 struct check_merge_ok_arg *a = arg;
2170 unsigned char status;
2171 struct stat sb;
2172 char *ondisk_path;
2174 /* Reject merges into a work tree with mixed base commits. */
2175 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2176 SHA1_DIGEST_LENGTH))
2177 return got_error(GOT_ERR_MIXED_COMMITS);
2179 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2180 == -1)
2181 return got_error_from_errno("asprintf");
2183 /* Reject merges into a work tree with conflicted files. */
2184 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
2185 if (err)
2186 return err;
2187 if (status == GOT_STATUS_CONFLICT)
2188 return got_error(GOT_ERR_CONFLICTS);
2190 return NULL;
2193 static const struct got_error *
2194 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2195 const char *fileindex_path, struct got_object_id *commit_id1,
2196 struct got_object_id *commit_id2, struct got_repository *repo,
2197 got_worktree_checkout_cb progress_cb, void *progress_arg,
2198 got_cancel_cb cancel_cb, void *cancel_arg)
2200 const struct got_error *err = NULL, *sync_err;
2201 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2202 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2203 struct merge_file_cb_arg arg;
2204 char *label_orig = NULL;
2206 if (commit_id1) {
2207 char *id_str;
2209 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2210 worktree->path_prefix);
2211 if (err)
2212 goto done;
2214 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2215 if (err)
2216 goto done;
2218 err = got_object_id_str(&id_str, commit_id1);
2219 if (err)
2220 goto done;
2222 if (asprintf(&label_orig, "%s: commit %s",
2223 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2224 err = got_error_from_errno("asprintf");
2225 free(id_str);
2226 goto done;
2228 free(id_str);
2231 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2232 worktree->path_prefix);
2233 if (err)
2234 goto done;
2236 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2237 if (err)
2238 goto done;
2240 arg.worktree = worktree;
2241 arg.fileindex = fileindex;
2242 arg.progress_cb = progress_cb;
2243 arg.progress_arg = progress_arg;
2244 arg.cancel_cb = cancel_cb;
2245 arg.cancel_arg = cancel_arg;
2246 arg.label_orig = label_orig;
2247 arg.commit_id2 = commit_id2;
2248 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2249 sync_err = sync_fileindex(fileindex, fileindex_path);
2250 if (sync_err && err == NULL)
2251 err = sync_err;
2252 done:
2253 if (tree1)
2254 got_object_tree_close(tree1);
2255 if (tree2)
2256 got_object_tree_close(tree2);
2257 free(label_orig);
2258 return err;
2261 const struct got_error *
2262 got_worktree_merge_files(struct got_worktree *worktree,
2263 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2264 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2265 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2267 const struct got_error *err, *unlockerr;
2268 char *fileindex_path = NULL;
2269 struct got_fileindex *fileindex = NULL;
2270 struct check_merge_ok_arg mok_arg;
2272 err = lock_worktree(worktree, LOCK_EX);
2273 if (err)
2274 return err;
2276 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2277 if (err)
2278 goto done;
2280 mok_arg.worktree = worktree;
2281 mok_arg.repo = repo;
2282 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2283 &mok_arg);
2284 if (err)
2285 goto done;
2287 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2288 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2289 done:
2290 if (fileindex)
2291 got_fileindex_free(fileindex);
2292 free(fileindex_path);
2293 unlockerr = lock_worktree(worktree, LOCK_SH);
2294 if (unlockerr && err == NULL)
2295 err = unlockerr;
2296 return err;
2299 struct diff_dir_cb_arg {
2300 struct got_fileindex *fileindex;
2301 struct got_worktree *worktree;
2302 const char *status_path;
2303 size_t status_path_len;
2304 struct got_repository *repo;
2305 got_worktree_status_cb status_cb;
2306 void *status_arg;
2307 got_cancel_cb cancel_cb;
2308 void *cancel_arg;
2309 /* A pathlist containing per-directory pathlists of ignore patterns. */
2310 struct got_pathlist_head ignores;
2313 static const struct got_error *
2314 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2315 got_worktree_status_cb status_cb, void *status_arg,
2316 struct got_repository *repo)
2318 const struct got_error *err = NULL;
2319 unsigned char status = GOT_STATUS_NO_CHANGE;
2320 unsigned char staged_status = get_staged_status(ie);
2321 struct stat sb;
2322 struct got_object_id blob_id, commit_id, staged_blob_id;
2323 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2324 struct got_object_id *staged_blob_idp = NULL;
2326 err = get_file_status(&status, &sb, ie, abspath, repo);
2327 if (err)
2328 return err;
2330 if (status == GOT_STATUS_NO_CHANGE &&
2331 staged_status == GOT_STATUS_NO_CHANGE)
2332 return NULL;
2334 if (got_fileindex_entry_has_blob(ie)) {
2335 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2336 blob_idp = &blob_id;
2338 if (got_fileindex_entry_has_commit(ie)) {
2339 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2340 commit_idp = &commit_id;
2342 if (staged_status == GOT_STATUS_ADD ||
2343 staged_status == GOT_STATUS_MODIFY) {
2344 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2345 SHA1_DIGEST_LENGTH);
2346 staged_blob_idp = &staged_blob_id;
2349 return (*status_cb)(status_arg, status, staged_status,
2350 ie->path, blob_idp, staged_blob_idp, commit_idp);
2353 static const struct got_error *
2354 status_old_new(void *arg, struct got_fileindex_entry *ie,
2355 struct dirent *de, const char *parent_path)
2357 const struct got_error *err = NULL;
2358 struct diff_dir_cb_arg *a = arg;
2359 char *abspath;
2361 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2362 return got_error(GOT_ERR_CANCELLED);
2364 if (got_path_cmp(parent_path, a->status_path,
2365 strlen(parent_path), a->status_path_len) != 0 &&
2366 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2367 return NULL;
2369 if (parent_path[0]) {
2370 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2371 parent_path, de->d_name) == -1)
2372 return got_error_from_errno("asprintf");
2373 } else {
2374 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2375 de->d_name) == -1)
2376 return got_error_from_errno("asprintf");
2379 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
2380 a->repo);
2381 free(abspath);
2382 return err;
2385 static const struct got_error *
2386 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2388 struct diff_dir_cb_arg *a = arg;
2389 struct got_object_id blob_id, commit_id;
2390 unsigned char status;
2392 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2393 return got_error(GOT_ERR_CANCELLED);
2395 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2396 return NULL;
2398 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2399 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2400 if (got_fileindex_entry_has_file_on_disk(ie))
2401 status = GOT_STATUS_MISSING;
2402 else
2403 status = GOT_STATUS_DELETE;
2404 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2405 ie->path, &blob_id, NULL, &commit_id);
2408 void
2409 free_ignorelist(struct got_pathlist_head *ignorelist)
2411 struct got_pathlist_entry *pe;
2413 TAILQ_FOREACH(pe, ignorelist, entry)
2414 free((char *)pe->path);
2415 got_pathlist_free(ignorelist);
2418 void
2419 free_ignores(struct got_pathlist_head *ignores)
2421 struct got_pathlist_entry *pe;
2423 TAILQ_FOREACH(pe, ignores, entry) {
2424 struct got_pathlist_head *ignorelist = pe->data;
2425 free_ignorelist(ignorelist);
2426 free((char *)pe->path);
2428 got_pathlist_free(ignores);
2431 static const struct got_error *
2432 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
2434 const struct got_error *err = NULL;
2435 struct got_pathlist_entry *pe = NULL;
2436 struct got_pathlist_head *ignorelist;
2437 char *line = NULL, *pattern, *dirpath = NULL;
2438 size_t linesize = 0;
2439 ssize_t linelen;
2441 ignorelist = calloc(1, sizeof(*ignorelist));
2442 if (ignorelist == NULL)
2443 return got_error_from_errno("calloc");
2444 TAILQ_INIT(ignorelist);
2446 while ((linelen = getline(&line, &linesize, f)) != -1) {
2447 if (linelen > 0 && line[linelen - 1] == '\n')
2448 line[linelen - 1] = '\0';
2450 /* Git's ignores may contain comments. */
2451 if (line[0] == '#')
2452 continue;
2454 /* Git's negated patterns are not (yet?) supported. */
2455 if (line[0] == '!')
2456 continue;
2458 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
2459 line) == -1) {
2460 err = got_error_from_errno("asprintf");
2461 goto done;
2463 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
2464 if (err)
2465 goto done;
2467 if (ferror(f)) {
2468 err = got_error_from_errno("getline");
2469 goto done;
2472 dirpath = strdup(path);
2473 if (dirpath == NULL) {
2474 err = got_error_from_errno("strdup");
2475 goto done;
2477 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
2478 done:
2479 free(line);
2480 if (err || pe == NULL) {
2481 free(dirpath);
2482 free_ignorelist(ignorelist);
2484 return err;
2487 int
2488 match_ignores(struct got_pathlist_head *ignores, const char *path)
2490 struct got_pathlist_entry *pe;
2492 /* Handle patterns which match in all directories. */
2493 TAILQ_FOREACH(pe, ignores, entry) {
2494 struct got_pathlist_head *ignorelist = pe->data;
2495 struct got_pathlist_entry *pi;
2497 TAILQ_FOREACH(pi, ignorelist, entry) {
2498 const char *p, *pattern = pi->path;
2500 if (strncmp(pattern, "**/", 3) != 0)
2501 continue;
2502 pattern += 3;
2503 p = path;
2504 while (*p) {
2505 if (fnmatch(pattern, p,
2506 FNM_PATHNAME | FNM_LEADING_DIR)) {
2507 /* Retry in next directory. */
2508 while (*p && *p != '/')
2509 p++;
2510 while (*p == '/')
2511 p++;
2512 continue;
2514 return 1;
2520 * The ignores pathlist contains ignore lists from children before
2521 * parents, so we can find the most specific ignorelist by walking
2522 * ignores backwards.
2524 pe = TAILQ_LAST(ignores, got_pathlist_head);
2525 while (pe) {
2526 if (got_path_is_child(path, pe->path, pe->path_len)) {
2527 struct got_pathlist_head *ignorelist = pe->data;
2528 struct got_pathlist_entry *pi;
2529 TAILQ_FOREACH(pi, ignorelist, entry) {
2530 const char *pattern = pi->path;
2531 int flags = FNM_LEADING_DIR;
2532 if (strstr(pattern, "/**/") == NULL)
2533 flags |= FNM_PATHNAME;
2534 if (fnmatch(pattern, path, flags))
2535 continue;
2536 return 1;
2539 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
2542 return 0;
2545 static const struct got_error *
2546 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
2547 const char *path, const char *ignores_filename)
2549 const struct got_error *err = NULL;
2550 char *ignorespath;
2551 FILE *ignoresfile = NULL;
2553 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
2554 path[0] ? "/" : "", ignores_filename) == -1)
2555 return got_error_from_errno("asprintf");
2557 ignoresfile = fopen(ignorespath, "r");
2558 if (ignoresfile == NULL) {
2559 if (errno != ENOENT && errno != EACCES)
2560 err = got_error_from_errno2("fopen",
2561 ignorespath);
2562 } else
2563 err = read_ignores(ignores, path, ignoresfile);
2565 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
2566 err = got_error_from_errno2("fclose", path);
2567 free(ignorespath);
2568 return err;
2571 static const struct got_error *
2572 status_new(void *arg, struct dirent *de, const char *parent_path)
2574 const struct got_error *err = NULL;
2575 struct diff_dir_cb_arg *a = arg;
2576 char *path = NULL;
2578 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2579 return got_error(GOT_ERR_CANCELLED);
2581 /* XXX ignore symlinks for now */
2582 if (de->d_type == DT_LNK)
2583 return NULL;
2585 if (parent_path[0]) {
2586 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2587 return got_error_from_errno("asprintf");
2588 } else {
2589 path = de->d_name;
2592 if (de->d_type == DT_DIR) {
2593 err = add_ignores(&a->ignores, a->worktree->root_path, path,
2594 ".cvsignore");
2595 if (err == NULL)
2596 err = add_ignores(&a->ignores, a->worktree->root_path,
2597 path, ".gitignore");
2599 else if (got_path_is_child(path, a->status_path, a->status_path_len)
2600 && !match_ignores(&a->ignores, path))
2601 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2602 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2603 if (parent_path[0])
2604 free(path);
2605 return err;
2608 static const struct got_error *
2609 report_single_file_status(const char *path, const char *ondisk_path,
2610 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2611 void *status_arg, struct got_repository *repo)
2613 struct got_fileindex_entry *ie;
2614 struct stat sb;
2616 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
2617 if (ie)
2618 return report_file_status(ie, ondisk_path, status_cb,
2619 status_arg, repo);
2621 if (lstat(ondisk_path, &sb) == -1) {
2622 if (errno != ENOENT)
2623 return got_error_from_errno2("lstat", ondisk_path);
2624 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
2625 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2626 return NULL;
2629 if (S_ISREG(sb.st_mode))
2630 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
2631 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2633 return NULL;
2636 static const struct got_error *
2637 worktree_status(struct got_worktree *worktree, const char *path,
2638 struct got_fileindex *fileindex, struct got_repository *repo,
2639 got_worktree_status_cb status_cb, void *status_arg,
2640 got_cancel_cb cancel_cb, void *cancel_arg)
2642 const struct got_error *err = NULL;
2643 DIR *workdir = NULL;
2644 struct got_fileindex_diff_dir_cb fdiff_cb;
2645 struct diff_dir_cb_arg arg;
2646 char *ondisk_path = NULL;
2648 if (asprintf(&ondisk_path, "%s%s%s",
2649 worktree->root_path, path[0] ? "/" : "", path) == -1)
2650 return got_error_from_errno("asprintf");
2652 workdir = opendir(ondisk_path);
2653 if (workdir == NULL) {
2654 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES)
2655 err = got_error_from_errno2("opendir", ondisk_path);
2656 else
2657 err = report_single_file_status(path, ondisk_path,
2658 fileindex, status_cb, status_arg, repo);
2659 } else {
2660 fdiff_cb.diff_old_new = status_old_new;
2661 fdiff_cb.diff_old = status_old;
2662 fdiff_cb.diff_new = status_new;
2663 arg.fileindex = fileindex;
2664 arg.worktree = worktree;
2665 arg.status_path = path;
2666 arg.status_path_len = strlen(path);
2667 arg.repo = repo;
2668 arg.status_cb = status_cb;
2669 arg.status_arg = status_arg;
2670 arg.cancel_cb = cancel_cb;
2671 arg.cancel_arg = cancel_arg;
2672 TAILQ_INIT(&arg.ignores);
2673 err = add_ignores(&arg.ignores, worktree->root_path, path,
2674 ".cvsignore");
2675 if (err == NULL)
2676 err = add_ignores(&arg.ignores, worktree->root_path,
2677 path, ".gitignore");
2678 if (err == NULL)
2679 err = got_fileindex_diff_dir(fileindex, workdir,
2680 worktree->root_path, path, repo, &fdiff_cb, &arg);
2681 free_ignores(&arg.ignores);
2684 if (workdir)
2685 closedir(workdir);
2686 free(ondisk_path);
2687 return err;
2690 const struct got_error *
2691 got_worktree_status(struct got_worktree *worktree,
2692 struct got_pathlist_head *paths, struct got_repository *repo,
2693 got_worktree_status_cb status_cb, void *status_arg,
2694 got_cancel_cb cancel_cb, void *cancel_arg)
2696 const struct got_error *err = NULL;
2697 char *fileindex_path = NULL;
2698 struct got_fileindex *fileindex = NULL;
2699 struct got_pathlist_entry *pe;
2701 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2702 if (err)
2703 return err;
2705 TAILQ_FOREACH(pe, paths, entry) {
2706 err = worktree_status(worktree, pe->path, fileindex, repo,
2707 status_cb, status_arg, cancel_cb, cancel_arg);
2708 if (err)
2709 break;
2711 free(fileindex_path);
2712 got_fileindex_free(fileindex);
2713 return err;
2716 const struct got_error *
2717 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2718 const char *arg)
2720 const struct got_error *err = NULL;
2721 char *resolved, *cwd = NULL, *path = NULL;
2722 size_t len;
2724 *wt_path = NULL;
2726 resolved = realpath(arg, NULL);
2727 if (resolved == NULL) {
2728 if (errno != ENOENT)
2729 return got_error_from_errno2("realpath", arg);
2730 cwd = getcwd(NULL, 0);
2731 if (cwd == NULL)
2732 return got_error_from_errno("getcwd");
2733 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
2734 err = got_error_from_errno("asprintf");
2735 goto done;
2739 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2740 strlen(got_worktree_get_root_path(worktree)))) {
2741 err = got_error(GOT_ERR_BAD_PATH);
2742 goto done;
2745 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2746 err = got_path_skip_common_ancestor(&path,
2747 got_worktree_get_root_path(worktree), resolved);
2748 if (err)
2749 goto done;
2750 } else {
2751 path = strdup("");
2752 if (path == NULL) {
2753 err = got_error_from_errno("strdup");
2754 goto done;
2758 /* XXX status walk can't deal with trailing slash! */
2759 len = strlen(path);
2760 while (len > 0 && path[len - 1] == '/') {
2761 path[len - 1] = '\0';
2762 len--;
2764 done:
2765 free(resolved);
2766 free(cwd);
2767 if (err == NULL)
2768 *wt_path = path;
2769 else
2770 free(path);
2771 return err;
2774 struct schedule_addition_args {
2775 struct got_worktree *worktree;
2776 struct got_fileindex *fileindex;
2777 const char *ondisk_path;
2778 got_worktree_checkout_cb progress_cb;
2779 void *progress_arg;
2780 struct got_repository *repo;
2783 static const struct got_error *
2784 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
2785 const char *relpath, struct got_object_id *blob_id,
2786 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2788 struct schedule_addition_args *a = arg;
2789 const struct got_error *err = NULL;
2790 struct got_fileindex_entry *ie;
2791 struct stat sb;
2792 char *path = NULL;
2794 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
2795 if (ie) {
2796 err = get_file_status(&status, &sb, ie, a->ondisk_path,
2797 a->repo);
2798 if (err)
2799 return err;
2800 /* Re-adding an existing entry is a no-op. */
2801 if (status == GOT_STATUS_ADD)
2802 return NULL;
2803 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
2806 if (status == GOT_STATUS_UNVERSIONED) {
2807 if (lstat(a->ondisk_path, &sb) != 0) {
2808 err = got_error_from_errno2("lstat", a->ondisk_path);
2809 return err;
2811 if (S_ISDIR(sb.st_mode)) {
2812 if (asprintf(&path, "%s/%s",
2813 got_worktree_get_root_path(a->worktree),
2814 relpath) == -1) {
2815 err = got_error_from_errno("asprintf");
2816 return err;
2818 } else
2819 path = strdup(a->ondisk_path);
2822 err = got_fileindex_entry_alloc(&ie, path, relpath, NULL, NULL);
2823 if (err)
2824 goto done;
2826 err = got_fileindex_entry_add(a->fileindex, ie);
2827 if (err) {
2828 got_fileindex_entry_free(ie);
2829 goto done;
2832 free(path);
2833 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
2834 done:
2835 free(path);
2836 return err;
2839 const struct got_error *
2840 got_worktree_schedule_add(struct got_worktree *worktree,
2841 struct got_pathlist_head *paths,
2842 got_worktree_checkout_cb progress_cb, void *progress_arg,
2843 struct got_repository *repo)
2845 struct got_fileindex *fileindex = NULL;
2846 char *fileindex_path = NULL;
2847 const struct got_error *err = NULL, *sync_err, *unlockerr;
2848 struct got_pathlist_entry *pe;
2849 struct schedule_addition_args saa;
2851 err = lock_worktree(worktree, LOCK_EX);
2852 if (err)
2853 return err;
2855 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2856 if (err)
2857 goto done;
2859 saa.worktree = worktree;
2860 saa.fileindex = fileindex;
2861 saa.progress_cb = progress_cb;
2862 saa.progress_arg = progress_arg;
2863 saa.repo = repo;
2865 TAILQ_FOREACH(pe, paths, entry) {
2866 char *ondisk_path;
2867 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
2868 pe->path) == -1)
2869 return got_error_from_errno("asprintf");
2870 saa.ondisk_path = ondisk_path;
2871 err = worktree_status(worktree, pe->path, fileindex, repo,
2872 schedule_addition, &saa, NULL, NULL);
2873 free(ondisk_path);
2874 if (err)
2875 break;
2877 sync_err = sync_fileindex(fileindex, fileindex_path);
2878 if (sync_err && err == NULL)
2879 err = sync_err;
2880 done:
2881 free(fileindex_path);
2882 if (fileindex)
2883 got_fileindex_free(fileindex);
2884 unlockerr = lock_worktree(worktree, LOCK_SH);
2885 if (unlockerr && err == NULL)
2886 err = unlockerr;
2887 return err;
2890 static const struct got_error *
2891 schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
2892 const char *relpath, int delete_local_mods,
2893 got_worktree_status_cb status_cb, void *status_arg,
2894 struct got_repository *repo)
2896 const struct got_error *err = NULL;
2897 struct got_fileindex_entry *ie = NULL;
2898 unsigned char status, staged_status;
2899 struct stat sb;
2901 ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
2902 if (ie == NULL)
2903 return got_error(GOT_ERR_BAD_PATH);
2905 staged_status = get_staged_status(ie);
2906 if (staged_status != GOT_STATUS_NO_CHANGE) {
2907 if (staged_status == GOT_STATUS_DELETE)
2908 return NULL;
2909 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
2912 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2913 if (err)
2914 return err;
2916 if (status != GOT_STATUS_NO_CHANGE) {
2917 if (status == GOT_STATUS_DELETE)
2918 return NULL;
2919 if (status == GOT_STATUS_MODIFY && !delete_local_mods)
2920 return got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
2921 if (status != GOT_STATUS_MODIFY &&
2922 status != GOT_STATUS_MISSING)
2923 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
2926 if (status != GOT_STATUS_MISSING && unlink(ondisk_path) != 0)
2927 return got_error_from_errno2("unlink", ondisk_path);
2929 got_fileindex_entry_mark_deleted_from_disk(ie);
2930 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2933 const struct got_error *
2934 got_worktree_schedule_delete(struct got_worktree *worktree,
2935 struct got_pathlist_head *paths, int delete_local_mods,
2936 got_worktree_status_cb status_cb, void *status_arg,
2937 struct got_repository *repo)
2939 struct got_fileindex *fileindex = NULL;
2940 char *fileindex_path = NULL;
2941 const struct got_error *err = NULL, *sync_err, *unlockerr;
2942 struct got_pathlist_entry *pe;
2944 err = lock_worktree(worktree, LOCK_EX);
2945 if (err)
2946 return err;
2948 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2949 if (err)
2950 goto done;
2952 TAILQ_FOREACH(pe, paths, entry) {
2953 char *ondisk_path;
2954 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
2955 pe->path) == -1)
2956 return got_error_from_errno("asprintf");
2957 err = schedule_for_deletion(ondisk_path, fileindex, pe->path,
2958 delete_local_mods, status_cb, status_arg, repo);
2959 free(ondisk_path);
2960 if (err)
2961 break;
2963 sync_err = sync_fileindex(fileindex, fileindex_path);
2964 if (sync_err && err == NULL)
2965 err = sync_err;
2966 done:
2967 free(fileindex_path);
2968 if (fileindex)
2969 got_fileindex_free(fileindex);
2970 unlockerr = lock_worktree(worktree, LOCK_SH);
2971 if (unlockerr && err == NULL)
2972 err = unlockerr;
2973 return err;
2976 static const struct got_error *
2977 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
2979 const struct got_error *err = NULL;
2980 char *line = NULL;
2981 size_t linesize = 0, n;
2982 ssize_t linelen;
2984 linelen = getline(&line, &linesize, infile);
2985 if (linelen == -1) {
2986 if (ferror(infile)) {
2987 err = got_error_from_errno("getline");
2988 goto done;
2990 return NULL;
2992 if (outfile) {
2993 n = fwrite(line, 1, linelen, outfile);
2994 if (n != linelen) {
2995 err = got_ferror(outfile, GOT_ERR_IO);
2996 goto done;
2999 if (rejectfile) {
3000 n = fwrite(line, 1, linelen, rejectfile);
3001 if (n != linelen)
3002 err = got_ferror(outfile, GOT_ERR_IO);
3004 done:
3005 free(line);
3006 return err;
3009 static const struct got_error *
3010 skip_one_line(FILE *f)
3012 char *line = NULL;
3013 size_t linesize = 0;
3014 ssize_t linelen;
3016 linelen = getline(&line, &linesize, f);
3017 if (linelen == -1) {
3018 if (ferror(f))
3019 return got_error_from_errno("getline");
3020 return NULL;
3022 free(line);
3023 return NULL;
3026 static const struct got_error *
3027 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3028 int start_old, int end_old, int start_new, int end_new,
3029 FILE *outfile, FILE *rejectfile)
3031 const struct got_error *err;
3033 /* Copy old file's lines leading up to patch. */
3034 while (!feof(f1) && *line_cur1 < start_old) {
3035 err = copy_one_line(f1, outfile, NULL);
3036 if (err)
3037 return err;
3038 (*line_cur1)++;
3040 /* Skip new file's lines leading up to patch. */
3041 while (!feof(f2) && *line_cur2 < start_new) {
3042 if (rejectfile)
3043 err = copy_one_line(f2, NULL, rejectfile);
3044 else
3045 err = skip_one_line(f2);
3046 if (err)
3047 return err;
3048 (*line_cur2)++;
3050 /* Copy patched lines. */
3051 while (!feof(f2) && *line_cur2 <= end_new) {
3052 err = copy_one_line(f2, outfile, NULL);
3053 if (err)
3054 return err;
3055 (*line_cur2)++;
3057 /* Skip over old file's replaced lines. */
3058 while (!feof(f1) && *line_cur1 <= end_old) {
3059 if (rejectfile)
3060 err = copy_one_line(f1, NULL, rejectfile);
3061 else
3062 err = skip_one_line(f1);
3063 if (err)
3064 return err;
3065 (*line_cur1)++;
3068 return NULL;
3071 static const struct got_error *
3072 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3073 FILE *outfile, FILE *rejectfile)
3075 const struct got_error *err;
3077 if (outfile) {
3078 /* Copy old file's lines until EOF. */
3079 while (!feof(f1)) {
3080 err = copy_one_line(f1, outfile, NULL);
3081 if (err)
3082 return err;
3083 (*line_cur1)++;
3086 if (rejectfile) {
3087 /* Copy new file's lines until EOF. */
3088 while (!feof(f2)) {
3089 err = copy_one_line(f2, NULL, rejectfile);
3090 if (err)
3091 return err;
3092 (*line_cur2)++;
3096 return NULL;
3099 static const struct got_error *
3100 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3101 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3102 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3103 int *line_cur2, FILE *outfile, FILE *rejectfile,
3104 got_worktree_patch_cb patch_cb, void *patch_arg)
3106 const struct got_error *err = NULL;
3107 int start_old = change->cv.a;
3108 int end_old = change->cv.b;
3109 int start_new = change->cv.c;
3110 int end_new = change->cv.d;
3111 long pos1, pos2;
3112 FILE *hunkfile;
3114 *choice = GOT_PATCH_CHOICE_NONE;
3116 hunkfile = got_opentemp();
3117 if (hunkfile == NULL)
3118 return got_error_from_errno("got_opentemp");
3120 pos1 = ftell(f1);
3121 pos2 = ftell(f2);
3123 /* XXX TODO needs error checking */
3124 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
3126 if (fseek(f1, pos1, SEEK_SET) == -1) {
3127 err = got_ferror(f1, GOT_ERR_IO);
3128 goto done;
3130 if (fseek(f2, pos2, SEEK_SET) == -1) {
3131 err = got_ferror(f1, GOT_ERR_IO);
3132 goto done;
3134 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
3135 err = got_ferror(hunkfile, GOT_ERR_IO);
3136 goto done;
3139 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
3140 hunkfile, n, nchanges);
3141 if (err)
3142 goto done;
3144 switch (*choice) {
3145 case GOT_PATCH_CHOICE_YES:
3146 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3147 end_old, start_new, end_new, outfile, rejectfile);
3148 break;
3149 case GOT_PATCH_CHOICE_NO:
3150 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3151 end_old, start_new, end_new, rejectfile, outfile);
3152 break;
3153 case GOT_PATCH_CHOICE_QUIT:
3154 break;
3155 default:
3156 err = got_error(GOT_ERR_PATCH_CHOICE);
3157 break;
3159 done:
3160 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3161 err = got_error_from_errno("fclose");
3162 return err;
3165 struct revert_file_args {
3166 struct got_worktree *worktree;
3167 struct got_fileindex *fileindex;
3168 got_worktree_checkout_cb progress_cb;
3169 void *progress_arg;
3170 got_worktree_patch_cb patch_cb;
3171 void *patch_arg;
3172 struct got_repository *repo;
3175 static const struct got_error *
3176 create_patched_content(char **path_outfile, int reverse_patch,
3177 struct got_object_id *blob_id, const char *path2,
3178 const char *relpath, struct got_repository *repo,
3179 got_worktree_patch_cb patch_cb, void *patch_arg)
3181 const struct got_error *err;
3182 struct got_blob_object *blob = NULL;
3183 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3184 int fd2 = -1;
3185 char *path1 = NULL, *id_str = NULL;
3186 struct stat sb1, sb2;
3187 struct got_diff_changes *changes = NULL;
3188 struct got_diff_state *ds = NULL;
3189 struct got_diff_args *args = NULL;
3190 struct got_diff_change *change;
3191 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3192 int n = 0;
3194 *path_outfile = NULL;
3196 err = got_object_id_str(&id_str, blob_id);
3197 if (err)
3198 return err;
3200 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
3201 if (fd2 == -1) {
3202 err = got_error_from_errno2("open", path2);
3203 goto done;
3205 if (fstat(fd2, &sb2) == -1) {
3206 err = got_error_from_errno2("fstat", path2);
3207 goto done;
3210 f2 = fdopen(fd2, "r");
3211 if (f2 == NULL) {
3212 err = got_error_from_errno2("fopen", path2);
3213 goto done;
3215 fd2 = -1;
3217 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
3218 if (err)
3219 goto done;
3221 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
3222 if (err)
3223 goto done;
3225 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
3226 if (err)
3227 goto done;
3229 if (stat(path1, &sb1) == -1) {
3230 err = got_error_from_errno2("stat", path1);
3231 goto done;
3234 err = got_diff_files(&changes, &ds, &args, &diff_flags,
3235 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
3236 if (err)
3237 goto done;
3239 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
3240 if (err)
3241 goto done;
3243 if (fseek(f1, 0L, SEEK_SET) == -1)
3244 return got_ferror(f1, GOT_ERR_IO);
3245 if (fseek(f2, 0L, SEEK_SET) == -1)
3246 return got_ferror(f2, GOT_ERR_IO);
3247 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
3248 int choice;
3249 err = apply_or_reject_change(&choice, change, ++n,
3250 changes->nchanges, ds, args, diff_flags, relpath,
3251 f1, f2, &line_cur1, &line_cur2,
3252 reverse_patch ? NULL : outfile,
3253 reverse_patch ? outfile : NULL,
3254 patch_cb, patch_arg);
3255 if (err)
3256 goto done;
3257 if (choice == GOT_PATCH_CHOICE_YES)
3258 have_content = 1;
3259 else if (choice == GOT_PATCH_CHOICE_QUIT)
3260 break;
3262 if (have_content) {
3263 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
3264 reverse_patch ? NULL : outfile,
3265 reverse_patch ? outfile : NULL);
3266 if (err)
3267 goto done;
3269 if (chmod(*path_outfile, sb2.st_mode) == -1) {
3270 err = got_error_from_errno2("chmod", path2);
3271 goto done;
3274 done:
3275 free(id_str);
3276 if (blob)
3277 got_object_blob_close(blob);
3278 if (f1 && fclose(f1) == EOF && err == NULL)
3279 err = got_error_from_errno2("fclose", path1);
3280 if (f2 && fclose(f2) == EOF && err == NULL)
3281 err = got_error_from_errno2("fclose", path2);
3282 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3283 err = got_error_from_errno2("close", path2);
3284 if (outfile && fclose(outfile) == EOF && err == NULL)
3285 err = got_error_from_errno2("fclose", *path_outfile);
3286 if (path1 && unlink(path1) == -1 && err == NULL)
3287 err = got_error_from_errno2("unlink", path1);
3288 if (err || !have_content) {
3289 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
3290 err = got_error_from_errno2("unlink", *path_outfile);
3291 free(*path_outfile);
3292 *path_outfile = NULL;
3294 free(args);
3295 if (ds) {
3296 got_diff_state_free(ds);
3297 free(ds);
3299 if (changes)
3300 got_diff_free_changes(changes);
3301 free(path1);
3302 return err;
3305 static const struct got_error *
3306 revert_file(void *arg, unsigned char status, unsigned char staged_status,
3307 const char *relpath, struct got_object_id *blob_id,
3308 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
3310 struct revert_file_args *a = arg;
3311 const struct got_error *err = NULL;
3312 char *parent_path = NULL;
3313 struct got_fileindex_entry *ie;
3314 struct got_tree_object *tree = NULL;
3315 struct got_object_id *tree_id = NULL;
3316 const struct got_tree_entry *te = NULL;
3317 char *tree_path = NULL, *te_name;
3318 char *ondisk_path = NULL, *path_content = NULL;
3319 struct got_blob_object *blob = NULL;
3321 /* Reverting a staged deletion is a no-op. */
3322 if (status == GOT_STATUS_DELETE &&
3323 staged_status != GOT_STATUS_NO_CHANGE)
3324 return NULL;
3326 if (status == GOT_STATUS_UNVERSIONED)
3327 return (*a->progress_cb)(a->progress_arg,
3328 GOT_STATUS_UNVERSIONED, relpath);
3330 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3331 if (ie == NULL)
3332 return got_error(GOT_ERR_BAD_PATH);
3334 /* Construct in-repository path of tree which contains this blob. */
3335 err = got_path_dirname(&parent_path, ie->path);
3336 if (err) {
3337 if (err->code != GOT_ERR_BAD_PATH)
3338 goto done;
3339 parent_path = strdup("/");
3340 if (parent_path == NULL) {
3341 err = got_error_from_errno("strdup");
3342 goto done;
3345 if (got_path_is_root_dir(a->worktree->path_prefix)) {
3346 tree_path = strdup(parent_path);
3347 if (tree_path == NULL) {
3348 err = got_error_from_errno("strdup");
3349 goto done;
3351 } else {
3352 if (got_path_is_root_dir(parent_path)) {
3353 tree_path = strdup(a->worktree->path_prefix);
3354 if (tree_path == NULL) {
3355 err = got_error_from_errno("strdup");
3356 goto done;
3358 } else {
3359 if (asprintf(&tree_path, "%s/%s",
3360 a->worktree->path_prefix, parent_path) == -1) {
3361 err = got_error_from_errno("asprintf");
3362 goto done;
3367 err = got_object_id_by_path(&tree_id, a->repo,
3368 a->worktree->base_commit_id, tree_path);
3369 if (err) {
3370 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
3371 (status == GOT_STATUS_ADD ||
3372 staged_status == GOT_STATUS_ADD)))
3373 goto done;
3374 } else {
3375 err = got_object_open_as_tree(&tree, a->repo, tree_id);
3376 if (err)
3377 goto done;
3379 te_name = basename(ie->path);
3380 if (te_name == NULL) {
3381 err = got_error_from_errno2("basename", ie->path);
3382 goto done;
3385 te = got_object_tree_find_entry(tree, te_name);
3386 if (te == NULL && status != GOT_STATUS_ADD &&
3387 staged_status != GOT_STATUS_ADD) {
3388 err = got_error(GOT_ERR_NO_TREE_ENTRY);
3389 goto done;
3393 switch (status) {
3394 case GOT_STATUS_ADD:
3395 if (a->patch_cb) {
3396 int choice = GOT_PATCH_CHOICE_NONE;
3397 err = (*a->patch_cb)(&choice, a->patch_arg,
3398 status, ie->path, NULL, 1, 1);
3399 if (err)
3400 goto done;
3401 if (choice != GOT_PATCH_CHOICE_YES)
3402 break;
3404 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
3405 ie->path);
3406 if (err)
3407 goto done;
3408 got_fileindex_entry_remove(a->fileindex, ie);
3409 break;
3410 case GOT_STATUS_DELETE:
3411 if (a->patch_cb) {
3412 int choice = GOT_PATCH_CHOICE_NONE;
3413 err = (*a->patch_cb)(&choice, a->patch_arg,
3414 status, ie->path, NULL, 1, 1);
3415 if (err)
3416 goto done;
3417 if (choice != GOT_PATCH_CHOICE_YES)
3418 break;
3420 /* fall through */
3421 case GOT_STATUS_MODIFY:
3422 case GOT_STATUS_MODE_CHANGE:
3423 case GOT_STATUS_CONFLICT:
3424 case GOT_STATUS_MISSING: {
3425 struct got_object_id id;
3426 if (staged_status == GOT_STATUS_ADD ||
3427 staged_status == GOT_STATUS_MODIFY) {
3428 memcpy(id.sha1, ie->staged_blob_sha1,
3429 SHA1_DIGEST_LENGTH);
3430 } else
3431 memcpy(id.sha1, ie->blob_sha1,
3432 SHA1_DIGEST_LENGTH);
3433 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
3434 if (err)
3435 goto done;
3437 if (asprintf(&ondisk_path, "%s/%s",
3438 got_worktree_get_root_path(a->worktree), relpath) == -1) {
3439 err = got_error_from_errno("asprintf");
3440 goto done;
3443 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
3444 status == GOT_STATUS_CONFLICT)) {
3445 err = create_patched_content(&path_content, 1, &id,
3446 ondisk_path, ie->path, a->repo,
3447 a->patch_cb, a->patch_arg);
3448 if (err || path_content == NULL)
3449 break;
3450 if (rename(path_content, ondisk_path) == -1) {
3451 err = got_error_from_errno3("rename",
3452 path_content, ondisk_path);
3453 goto done;
3455 } else {
3456 err = install_blob(a->worktree, ondisk_path, ie->path,
3457 te ? te->mode : GOT_DEFAULT_FILE_MODE,
3458 got_fileindex_perms_to_st(ie), blob, 0, 1,
3459 a->repo, a->progress_cb, a->progress_arg);
3460 if (err)
3461 goto done;
3462 if (status == GOT_STATUS_DELETE ||
3463 status == GOT_STATUS_MODE_CHANGE) {
3464 err = update_blob_fileindex_entry(a->worktree,
3465 a->fileindex, ie, ondisk_path, ie->path,
3466 blob, 1);
3467 if (err)
3468 goto done;
3471 break;
3473 default:
3474 break;
3476 done:
3477 free(ondisk_path);
3478 free(path_content);
3479 free(parent_path);
3480 free(tree_path);
3481 if (blob)
3482 got_object_blob_close(blob);
3483 if (tree)
3484 got_object_tree_close(tree);
3485 free(tree_id);
3486 return err;
3489 const struct got_error *
3490 got_worktree_revert(struct got_worktree *worktree,
3491 struct got_pathlist_head *paths,
3492 got_worktree_checkout_cb progress_cb, void *progress_arg,
3493 got_worktree_patch_cb patch_cb, void *patch_arg,
3494 struct got_repository *repo)
3496 struct got_fileindex *fileindex = NULL;
3497 char *fileindex_path = NULL;
3498 const struct got_error *err = NULL, *unlockerr = NULL;
3499 const struct got_error *sync_err = NULL;
3500 struct got_pathlist_entry *pe;
3501 struct revert_file_args rfa;
3503 err = lock_worktree(worktree, LOCK_EX);
3504 if (err)
3505 return err;
3507 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3508 if (err)
3509 goto done;
3511 rfa.worktree = worktree;
3512 rfa.fileindex = fileindex;
3513 rfa.progress_cb = progress_cb;
3514 rfa.progress_arg = progress_arg;
3515 rfa.patch_cb = patch_cb;
3516 rfa.patch_arg = patch_arg;
3517 rfa.repo = repo;
3518 TAILQ_FOREACH(pe, paths, entry) {
3519 err = worktree_status(worktree, pe->path, fileindex, repo,
3520 revert_file, &rfa, NULL, NULL);
3521 if (err)
3522 break;
3524 sync_err = sync_fileindex(fileindex, fileindex_path);
3525 if (sync_err && err == NULL)
3526 err = sync_err;
3527 done:
3528 free(fileindex_path);
3529 if (fileindex)
3530 got_fileindex_free(fileindex);
3531 unlockerr = lock_worktree(worktree, LOCK_SH);
3532 if (unlockerr && err == NULL)
3533 err = unlockerr;
3534 return err;
3537 static void
3538 free_commitable(struct got_commitable *ct)
3540 free(ct->path);
3541 free(ct->in_repo_path);
3542 free(ct->ondisk_path);
3543 free(ct->blob_id);
3544 free(ct->base_blob_id);
3545 free(ct->staged_blob_id);
3546 free(ct->base_commit_id);
3547 free(ct);
3550 struct collect_commitables_arg {
3551 struct got_pathlist_head *commitable_paths;
3552 struct got_repository *repo;
3553 struct got_worktree *worktree;
3554 int have_staged_files;
3557 static const struct got_error *
3558 collect_commitables(void *arg, unsigned char status,
3559 unsigned char staged_status, const char *relpath,
3560 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3561 struct got_object_id *commit_id)
3563 struct collect_commitables_arg *a = arg;
3564 const struct got_error *err = NULL;
3565 struct got_commitable *ct = NULL;
3566 struct got_pathlist_entry *new = NULL;
3567 char *parent_path = NULL, *path = NULL;
3568 struct stat sb;
3570 if (a->have_staged_files) {
3571 if (staged_status != GOT_STATUS_MODIFY &&
3572 staged_status != GOT_STATUS_ADD &&
3573 staged_status != GOT_STATUS_DELETE)
3574 return NULL;
3575 } else {
3576 if (status == GOT_STATUS_CONFLICT)
3577 return got_error(GOT_ERR_COMMIT_CONFLICT);
3579 if (status != GOT_STATUS_MODIFY &&
3580 status != GOT_STATUS_MODE_CHANGE &&
3581 status != GOT_STATUS_ADD &&
3582 status != GOT_STATUS_DELETE)
3583 return NULL;
3586 if (asprintf(&path, "/%s", relpath) == -1) {
3587 err = got_error_from_errno("asprintf");
3588 goto done;
3590 if (strcmp(path, "/") == 0) {
3591 parent_path = strdup("");
3592 if (parent_path == NULL)
3593 return got_error_from_errno("strdup");
3594 } else {
3595 err = got_path_dirname(&parent_path, path);
3596 if (err)
3597 return err;
3600 ct = calloc(1, sizeof(*ct));
3601 if (ct == NULL) {
3602 err = got_error_from_errno("calloc");
3603 goto done;
3606 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
3607 relpath) == -1) {
3608 err = got_error_from_errno("asprintf");
3609 goto done;
3611 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
3612 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3613 } else {
3614 if (lstat(ct->ondisk_path, &sb) != 0) {
3615 err = got_error_from_errno2("lstat", ct->ondisk_path);
3616 goto done;
3618 ct->mode = sb.st_mode;
3621 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
3622 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
3623 relpath) == -1) {
3624 err = got_error_from_errno("asprintf");
3625 goto done;
3628 ct->status = status;
3629 ct->staged_status = staged_status;
3630 ct->blob_id = NULL; /* will be filled in when blob gets created */
3631 if (ct->status != GOT_STATUS_ADD &&
3632 ct->staged_status != GOT_STATUS_ADD) {
3633 ct->base_blob_id = got_object_id_dup(blob_id);
3634 if (ct->base_blob_id == NULL) {
3635 err = got_error_from_errno("got_object_id_dup");
3636 goto done;
3638 ct->base_commit_id = got_object_id_dup(commit_id);
3639 if (ct->base_commit_id == NULL) {
3640 err = got_error_from_errno("got_object_id_dup");
3641 goto done;
3644 if (ct->staged_status == GOT_STATUS_ADD ||
3645 ct->staged_status == GOT_STATUS_MODIFY) {
3646 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
3647 if (ct->staged_blob_id == NULL) {
3648 err = got_error_from_errno("got_object_id_dup");
3649 goto done;
3652 ct->path = strdup(path);
3653 if (ct->path == NULL) {
3654 err = got_error_from_errno("strdup");
3655 goto done;
3657 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
3658 done:
3659 if (ct && (err || new == NULL))
3660 free_commitable(ct);
3661 free(parent_path);
3662 free(path);
3663 return err;
3666 static const struct got_error *write_tree(struct got_object_id **,
3667 struct got_tree_object *, const char *, struct got_pathlist_head *,
3668 got_worktree_status_cb status_cb, void *status_arg,
3669 struct got_repository *);
3671 static const struct got_error *
3672 write_subtree(struct got_object_id **new_subtree_id,
3673 struct got_tree_entry *te, const char *parent_path,
3674 struct got_pathlist_head *commitable_paths,
3675 got_worktree_status_cb status_cb, void *status_arg,
3676 struct got_repository *repo)
3678 const struct got_error *err = NULL;
3679 struct got_tree_object *subtree;
3680 char *subpath;
3682 if (asprintf(&subpath, "%s%s%s", parent_path,
3683 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
3684 return got_error_from_errno("asprintf");
3686 err = got_object_open_as_tree(&subtree, repo, &te->id);
3687 if (err)
3688 return err;
3690 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
3691 status_cb, status_arg, repo);
3692 got_object_tree_close(subtree);
3693 free(subpath);
3694 return err;
3697 static const struct got_error *
3698 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
3700 const struct got_error *err = NULL;
3701 char *ct_parent_path = NULL;
3703 *match = 0;
3705 if (strchr(ct->in_repo_path, '/') == NULL) {
3706 *match = got_path_is_root_dir(path);
3707 return NULL;
3710 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
3711 if (err)
3712 return err;
3713 *match = (strcmp(path, ct_parent_path) == 0);
3714 free(ct_parent_path);
3715 return err;
3718 static mode_t
3719 get_ct_file_mode(struct got_commitable *ct)
3721 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
3724 static const struct got_error *
3725 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
3726 struct got_tree_entry *te, struct got_commitable *ct)
3728 const struct got_error *err = NULL;
3730 *new_te = NULL;
3732 err = got_object_tree_entry_dup(new_te, te);
3733 if (err)
3734 goto done;
3736 (*new_te)->mode = get_ct_file_mode(ct);
3738 if (ct->staged_status == GOT_STATUS_MODIFY)
3739 memcpy(&(*new_te)->id, ct->staged_blob_id,
3740 sizeof((*new_te)->id));
3741 else
3742 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
3743 done:
3744 if (err && *new_te) {
3745 free(*new_te);
3746 *new_te = NULL;
3748 return err;
3751 static const struct got_error *
3752 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
3753 struct got_commitable *ct)
3755 const struct got_error *err = NULL;
3756 char *ct_name;
3758 *new_te = NULL;
3760 *new_te = calloc(1, sizeof(**new_te));
3761 if (*new_te == NULL)
3762 return got_error_from_errno("calloc");
3764 ct_name = basename(ct->path);
3765 if (ct_name == NULL) {
3766 err = got_error_from_errno2("basename", ct->path);
3767 goto done;
3769 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
3770 sizeof((*new_te)->name)) {
3771 err = got_error(GOT_ERR_NO_SPACE);
3772 goto done;
3775 (*new_te)->mode = get_ct_file_mode(ct);
3777 if (ct->staged_status == GOT_STATUS_ADD)
3778 memcpy(&(*new_te)->id, ct->staged_blob_id,
3779 sizeof((*new_te)->id));
3780 else
3781 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
3782 done:
3783 if (err && *new_te) {
3784 free(*new_te);
3785 *new_te = NULL;
3787 return err;
3790 static const struct got_error *
3791 insert_tree_entry(struct got_tree_entry *new_te,
3792 struct got_pathlist_head *paths)
3794 const struct got_error *err = NULL;
3795 struct got_pathlist_entry *new_pe;
3797 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
3798 if (err)
3799 return err;
3800 if (new_pe == NULL)
3801 return got_error(GOT_ERR_TREE_DUP_ENTRY);
3802 return NULL;
3805 static const struct got_error *
3806 report_ct_status(struct got_commitable *ct,
3807 got_worktree_status_cb status_cb, void *status_arg)
3809 const char *ct_path = ct->path;
3810 unsigned char status;
3812 while (ct_path[0] == '/')
3813 ct_path++;
3815 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
3816 status = ct->staged_status;
3817 else
3818 status = ct->status;
3820 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
3821 ct_path, ct->blob_id, NULL, NULL);
3824 static const struct got_error *
3825 match_modified_subtree(int *modified, struct got_tree_entry *te,
3826 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
3828 const struct got_error *err = NULL;
3829 struct got_pathlist_entry *pe;
3830 char *te_path;
3832 *modified = 0;
3834 if (asprintf(&te_path, "%s%s%s", base_tree_path,
3835 got_path_is_root_dir(base_tree_path) ? "" : "/",
3836 te->name) == -1)
3837 return got_error_from_errno("asprintf");
3839 TAILQ_FOREACH(pe, commitable_paths, entry) {
3840 struct got_commitable *ct = pe->data;
3841 *modified = got_path_is_child(ct->in_repo_path, te_path,
3842 strlen(te_path));
3843 if (*modified)
3844 break;
3847 free(te_path);
3848 return err;
3851 static const struct got_error *
3852 match_deleted_or_modified_ct(struct got_commitable **ctp,
3853 struct got_tree_entry *te, const char *base_tree_path,
3854 struct got_pathlist_head *commitable_paths)
3856 const struct got_error *err = NULL;
3857 struct got_pathlist_entry *pe;
3859 *ctp = NULL;
3861 TAILQ_FOREACH(pe, commitable_paths, entry) {
3862 struct got_commitable *ct = pe->data;
3863 char *ct_name = NULL;
3864 int path_matches;
3866 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
3867 if (ct->status != GOT_STATUS_MODIFY &&
3868 ct->status != GOT_STATUS_MODE_CHANGE &&
3869 ct->status != GOT_STATUS_DELETE)
3870 continue;
3871 } else {
3872 if (ct->staged_status != GOT_STATUS_MODIFY &&
3873 ct->staged_status != GOT_STATUS_DELETE)
3874 continue;
3877 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
3878 continue;
3880 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
3881 if (err)
3882 return err;
3883 if (!path_matches)
3884 continue;
3886 ct_name = basename(pe->path);
3887 if (ct_name == NULL)
3888 return got_error_from_errno2("basename", pe->path);
3890 if (strcmp(te->name, ct_name) != 0)
3891 continue;
3893 *ctp = ct;
3894 break;
3897 return err;
3900 static const struct got_error *
3901 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
3902 const char *child_path, const char *path_base_tree,
3903 struct got_pathlist_head *commitable_paths,
3904 got_worktree_status_cb status_cb, void *status_arg,
3905 struct got_repository *repo)
3907 const struct got_error *err = NULL;
3908 struct got_tree_entry *new_te;
3909 char *subtree_path;
3910 struct got_object_id *id = NULL;
3912 *new_tep = NULL;
3914 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
3915 got_path_is_root_dir(path_base_tree) ? "" : "/",
3916 child_path) == -1)
3917 return got_error_from_errno("asprintf");
3919 new_te = calloc(1, sizeof(*new_te));
3920 if (new_te == NULL)
3921 return got_error_from_errno("calloc");
3922 new_te->mode = S_IFDIR;
3924 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
3925 sizeof(new_te->name)) {
3926 err = got_error(GOT_ERR_NO_SPACE);
3927 goto done;
3929 err = write_tree(&id, NULL, subtree_path,
3930 commitable_paths, status_cb, status_arg, repo);
3931 if (err) {
3932 free(new_te);
3933 goto done;
3935 memcpy(&new_te->id, id, sizeof(new_te->id));
3936 done:
3937 free(id);
3938 free(subtree_path);
3939 if (err == NULL)
3940 *new_tep = new_te;
3941 return err;
3944 static const struct got_error *
3945 write_tree(struct got_object_id **new_tree_id,
3946 struct got_tree_object *base_tree, const char *path_base_tree,
3947 struct got_pathlist_head *commitable_paths,
3948 got_worktree_status_cb status_cb, void *status_arg,
3949 struct got_repository *repo)
3951 const struct got_error *err = NULL;
3952 struct got_pathlist_head paths;
3953 struct got_tree_entry *te, *new_te = NULL;
3954 struct got_pathlist_entry *pe;
3955 int nentries = 0;
3957 TAILQ_INIT(&paths);
3959 /* Insert, and recurse into, newly added entries first. */
3960 TAILQ_FOREACH(pe, commitable_paths, entry) {
3961 struct got_commitable *ct = pe->data;
3962 char *child_path = NULL, *slash;
3964 if ((ct->status != GOT_STATUS_ADD &&
3965 ct->staged_status != GOT_STATUS_ADD) ||
3966 (ct->flags & GOT_COMMITABLE_ADDED))
3967 continue;
3969 if (!got_path_is_child(pe->path, path_base_tree,
3970 strlen(path_base_tree)))
3971 continue;
3973 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
3974 pe->path);
3975 if (err)
3976 goto done;
3978 slash = strchr(child_path, '/');
3979 if (slash == NULL) {
3980 err = alloc_added_blob_tree_entry(&new_te, ct);
3981 if (err)
3982 goto done;
3983 err = report_ct_status(ct, status_cb, status_arg);
3984 if (err)
3985 goto done;
3986 ct->flags |= GOT_COMMITABLE_ADDED;
3987 err = insert_tree_entry(new_te, &paths);
3988 if (err)
3989 goto done;
3990 nentries++;
3991 } else {
3992 *slash = '\0'; /* trim trailing path components */
3993 if (base_tree == NULL ||
3994 got_object_tree_find_entry(base_tree, child_path)
3995 == NULL) {
3996 err = make_subtree_for_added_blob(&new_te,
3997 child_path, path_base_tree,
3998 commitable_paths, status_cb, status_arg,
3999 repo);
4000 if (err)
4001 goto done;
4002 err = insert_tree_entry(new_te, &paths);
4003 if (err)
4004 goto done;
4005 nentries++;
4010 if (base_tree) {
4011 int i, nbase_entries;
4012 /* Handle modified and deleted entries. */
4013 nbase_entries = got_object_tree_get_nentries(base_tree);
4014 for (i = 0; i < nbase_entries; i++) {
4015 struct got_commitable *ct = NULL;
4017 te = got_object_tree_get_entry(base_tree, i);
4018 if (got_object_tree_entry_is_submodule(te)) {
4019 /* Entry is a submodule; just copy it. */
4020 err = got_object_tree_entry_dup(&new_te, te);
4021 if (err)
4022 goto done;
4023 err = insert_tree_entry(new_te, &paths);
4024 if (err)
4025 goto done;
4026 nentries++;
4027 continue;
4030 if (S_ISDIR(te->mode)) {
4031 int modified;
4032 err = got_object_tree_entry_dup(&new_te, te);
4033 if (err)
4034 goto done;
4035 err = match_modified_subtree(&modified, te,
4036 path_base_tree, commitable_paths);
4037 if (err)
4038 goto done;
4039 /* Avoid recursion into unmodified subtrees. */
4040 if (modified) {
4041 struct got_object_id *new_id;
4042 err = write_subtree(&new_id, te,
4043 path_base_tree, commitable_paths,
4044 status_cb, status_arg, repo);
4045 if (err)
4046 goto done;
4047 memcpy(&new_te->id, new_id,
4048 sizeof(new_te->id));
4049 free(new_id);
4051 err = insert_tree_entry(new_te, &paths);
4052 if (err)
4053 goto done;
4054 nentries++;
4055 continue;
4058 err = match_deleted_or_modified_ct(&ct, te,
4059 path_base_tree, commitable_paths);
4060 if (err)
4061 goto done;
4062 if (ct) {
4063 /* NB: Deleted entries get dropped here. */
4064 if (ct->status == GOT_STATUS_MODIFY ||
4065 ct->status == GOT_STATUS_MODE_CHANGE ||
4066 ct->staged_status == GOT_STATUS_MODIFY) {
4067 err = alloc_modified_blob_tree_entry(
4068 &new_te, te, ct);
4069 if (err)
4070 goto done;
4071 err = insert_tree_entry(new_te, &paths);
4072 if (err)
4073 goto done;
4074 nentries++;
4076 err = report_ct_status(ct, status_cb,
4077 status_arg);
4078 if (err)
4079 goto done;
4080 } else {
4081 /* Entry is unchanged; just copy it. */
4082 err = got_object_tree_entry_dup(&new_te, te);
4083 if (err)
4084 goto done;
4085 err = insert_tree_entry(new_te, &paths);
4086 if (err)
4087 goto done;
4088 nentries++;
4093 /* Write new list of entries; deleted entries have been dropped. */
4094 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
4095 done:
4096 got_pathlist_free(&paths);
4097 return err;
4100 static const struct got_error *
4101 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
4102 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
4103 int have_staged_files)
4105 const struct got_error *err = NULL;
4106 struct got_pathlist_entry *pe;
4108 TAILQ_FOREACH(pe, commitable_paths, entry) {
4109 struct got_fileindex_entry *ie;
4110 struct got_commitable *ct = pe->data;
4112 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4113 if (ie) {
4114 if (ct->status == GOT_STATUS_DELETE ||
4115 ct->staged_status == GOT_STATUS_DELETE) {
4116 got_fileindex_entry_remove(fileindex, ie);
4117 got_fileindex_entry_free(ie);
4118 } else if (ct->staged_status == GOT_STATUS_ADD ||
4119 ct->staged_status == GOT_STATUS_MODIFY) {
4120 got_fileindex_entry_stage_set(ie,
4121 GOT_FILEIDX_STAGE_NONE);
4122 err = got_fileindex_entry_update(ie,
4123 ct->ondisk_path, ct->staged_blob_id->sha1,
4124 new_base_commit_id->sha1,
4125 !have_staged_files);
4126 } else
4127 err = got_fileindex_entry_update(ie,
4128 ct->ondisk_path, ct->blob_id->sha1,
4129 new_base_commit_id->sha1,
4130 !have_staged_files);
4131 } else {
4132 err = got_fileindex_entry_alloc(&ie,
4133 ct->ondisk_path, pe->path, ct->blob_id->sha1,
4134 new_base_commit_id->sha1);
4135 if (err)
4136 break;
4137 err = got_fileindex_entry_add(fileindex, ie);
4138 if (err)
4139 break;
4142 return err;
4146 static const struct got_error *
4147 check_out_of_date(const char *in_repo_path, unsigned char status,
4148 unsigned char staged_status, struct got_object_id *base_blob_id,
4149 struct got_object_id *base_commit_id,
4150 struct got_object_id *head_commit_id, struct got_repository *repo,
4151 int ood_errcode)
4153 const struct got_error *err = NULL;
4154 struct got_object_id *id = NULL;
4156 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
4157 /* Trivial case: base commit == head commit */
4158 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
4159 return NULL;
4161 * Ensure file content which local changes were based
4162 * on matches file content in the branch head.
4164 err = got_object_id_by_path(&id, repo, head_commit_id,
4165 in_repo_path);
4166 if (err) {
4167 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4168 err = got_error(ood_errcode);
4169 goto done;
4170 } else if (got_object_id_cmp(id, base_blob_id) != 0)
4171 err = got_error(ood_errcode);
4172 } else {
4173 /* Require that added files don't exist in the branch head. */
4174 err = got_object_id_by_path(&id, repo, head_commit_id,
4175 in_repo_path);
4176 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
4177 goto done;
4178 err = id ? got_error(ood_errcode) : NULL;
4180 done:
4181 free(id);
4182 return err;
4185 const struct got_error *
4186 commit_worktree(struct got_object_id **new_commit_id,
4187 struct got_pathlist_head *commitable_paths,
4188 struct got_object_id *head_commit_id, struct got_worktree *worktree,
4189 const char *author, const char *committer,
4190 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4191 got_worktree_status_cb status_cb, void *status_arg,
4192 struct got_repository *repo)
4194 const struct got_error *err = NULL, *unlockerr = NULL;
4195 struct got_pathlist_entry *pe;
4196 const char *head_ref_name = NULL;
4197 struct got_commit_object *head_commit = NULL;
4198 struct got_reference *head_ref2 = NULL;
4199 struct got_object_id *head_commit_id2 = NULL;
4200 struct got_tree_object *head_tree = NULL;
4201 struct got_object_id *new_tree_id = NULL;
4202 struct got_object_id_queue parent_ids;
4203 struct got_object_qid *pid = NULL;
4204 char *logmsg = NULL;
4206 *new_commit_id = NULL;
4208 SIMPLEQ_INIT(&parent_ids);
4210 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
4211 if (err)
4212 goto done;
4214 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
4215 if (err)
4216 goto done;
4218 if (commit_msg_cb != NULL) {
4219 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
4220 if (err)
4221 goto done;
4224 if (logmsg == NULL || strlen(logmsg) == 0) {
4225 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
4226 goto done;
4229 /* Create blobs from added and modified files and record their IDs. */
4230 TAILQ_FOREACH(pe, commitable_paths, entry) {
4231 struct got_commitable *ct = pe->data;
4232 char *ondisk_path;
4234 /* Blobs for staged files already exist. */
4235 if (ct->staged_status == GOT_STATUS_ADD ||
4236 ct->staged_status == GOT_STATUS_MODIFY)
4237 continue;
4239 if (ct->status != GOT_STATUS_ADD &&
4240 ct->status != GOT_STATUS_MODIFY &&
4241 ct->status != GOT_STATUS_MODE_CHANGE)
4242 continue;
4244 if (asprintf(&ondisk_path, "%s/%s",
4245 worktree->root_path, pe->path) == -1) {
4246 err = got_error_from_errno("asprintf");
4247 goto done;
4249 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
4250 free(ondisk_path);
4251 if (err)
4252 goto done;
4255 /* Recursively write new tree objects. */
4256 err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
4257 status_cb, status_arg, repo);
4258 if (err)
4259 goto done;
4261 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
4262 if (err)
4263 goto done;
4264 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
4265 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
4266 1, author, time(NULL), committer, time(NULL), logmsg, repo);
4267 got_object_qid_free(pid);
4268 if (logmsg != NULL)
4269 free(logmsg);
4270 if (err)
4271 goto done;
4273 /* Check if a concurrent commit to our branch has occurred. */
4274 head_ref_name = got_worktree_get_head_ref_name(worktree);
4275 if (head_ref_name == NULL) {
4276 err = got_error_from_errno("got_worktree_get_head_ref_name");
4277 goto done;
4279 /* Lock the reference here to prevent concurrent modification. */
4280 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
4281 if (err)
4282 goto done;
4283 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
4284 if (err)
4285 goto done;
4286 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
4287 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
4288 goto done;
4290 /* Update branch head in repository. */
4291 err = got_ref_change_ref(head_ref2, *new_commit_id);
4292 if (err)
4293 goto done;
4294 err = got_ref_write(head_ref2, repo);
4295 if (err)
4296 goto done;
4298 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
4299 if (err)
4300 goto done;
4302 err = ref_base_commit(worktree, repo);
4303 if (err)
4304 goto done;
4305 done:
4306 if (head_tree)
4307 got_object_tree_close(head_tree);
4308 if (head_commit)
4309 got_object_commit_close(head_commit);
4310 free(head_commit_id2);
4311 if (head_ref2) {
4312 unlockerr = got_ref_unlock(head_ref2);
4313 if (unlockerr && err == NULL)
4314 err = unlockerr;
4315 got_ref_close(head_ref2);
4317 return err;
4320 static const struct got_error *
4321 check_path_is_commitable(const char *path,
4322 struct got_pathlist_head *commitable_paths)
4324 struct got_pathlist_entry *cpe = NULL;
4325 size_t path_len = strlen(path);
4327 TAILQ_FOREACH(cpe, commitable_paths, entry) {
4328 struct got_commitable *ct = cpe->data;
4329 const char *ct_path = ct->path;
4331 while (ct_path[0] == '/')
4332 ct_path++;
4334 if (strcmp(path, ct_path) == 0 ||
4335 got_path_is_child(ct_path, path, path_len))
4336 break;
4339 if (cpe == NULL)
4340 return got_error_path(path, GOT_ERR_BAD_PATH);
4342 return NULL;
4345 static const struct got_error *
4346 check_staged_file(void *arg, struct got_fileindex_entry *ie)
4348 int *have_staged_files = arg;
4350 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
4351 *have_staged_files = 1;
4352 return got_error(GOT_ERR_CANCELLED);
4355 return NULL;
4358 static const struct got_error *
4359 check_non_staged_files(struct got_fileindex *fileindex,
4360 struct got_pathlist_head *paths)
4362 struct got_pathlist_entry *pe;
4363 struct got_fileindex_entry *ie;
4365 TAILQ_FOREACH(pe, paths, entry) {
4366 if (pe->path[0] == '\0')
4367 continue;
4368 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4369 if (ie == NULL)
4370 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
4371 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
4372 return got_error_path(pe->path,
4373 GOT_ERR_FILE_NOT_STAGED);
4376 return NULL;
4379 const struct got_error *
4380 got_worktree_commit(struct got_object_id **new_commit_id,
4381 struct got_worktree *worktree, struct got_pathlist_head *paths,
4382 const char *author, const char *committer,
4383 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4384 got_worktree_status_cb status_cb, void *status_arg,
4385 struct got_repository *repo)
4387 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
4388 struct got_fileindex *fileindex = NULL;
4389 char *fileindex_path = NULL;
4390 struct got_pathlist_head commitable_paths;
4391 struct collect_commitables_arg cc_arg;
4392 struct got_pathlist_entry *pe;
4393 struct got_reference *head_ref = NULL;
4394 struct got_object_id *head_commit_id = NULL;
4395 int have_staged_files = 0;
4397 *new_commit_id = NULL;
4399 TAILQ_INIT(&commitable_paths);
4401 err = lock_worktree(worktree, LOCK_EX);
4402 if (err)
4403 goto done;
4405 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4406 if (err)
4407 goto done;
4409 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4410 if (err)
4411 goto done;
4413 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4414 if (err)
4415 goto done;
4417 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
4418 &have_staged_files);
4419 if (err && err->code != GOT_ERR_CANCELLED)
4420 goto done;
4421 if (have_staged_files) {
4422 err = check_non_staged_files(fileindex, paths);
4423 if (err)
4424 goto done;
4427 cc_arg.commitable_paths = &commitable_paths;
4428 cc_arg.worktree = worktree;
4429 cc_arg.repo = repo;
4430 cc_arg.have_staged_files = have_staged_files;
4431 TAILQ_FOREACH(pe, paths, entry) {
4432 err = worktree_status(worktree, pe->path, fileindex, repo,
4433 collect_commitables, &cc_arg, NULL, NULL);
4434 if (err)
4435 goto done;
4438 if (TAILQ_EMPTY(&commitable_paths)) {
4439 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4440 goto done;
4443 TAILQ_FOREACH(pe, paths, entry) {
4444 err = check_path_is_commitable(pe->path, &commitable_paths);
4445 if (err)
4446 goto done;
4449 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4450 struct got_commitable *ct = pe->data;
4451 const char *ct_path = ct->in_repo_path;
4453 while (ct_path[0] == '/')
4454 ct_path++;
4455 err = check_out_of_date(ct_path, ct->status,
4456 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
4457 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
4458 if (err)
4459 goto done;
4463 err = commit_worktree(new_commit_id, &commitable_paths,
4464 head_commit_id, worktree, author, committer,
4465 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
4466 if (err)
4467 goto done;
4469 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4470 fileindex, have_staged_files);
4471 sync_err = sync_fileindex(fileindex, fileindex_path);
4472 if (sync_err && err == NULL)
4473 err = sync_err;
4474 done:
4475 if (fileindex)
4476 got_fileindex_free(fileindex);
4477 free(fileindex_path);
4478 unlockerr = lock_worktree(worktree, LOCK_SH);
4479 if (unlockerr && err == NULL)
4480 err = unlockerr;
4481 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4482 struct got_commitable *ct = pe->data;
4483 free_commitable(ct);
4485 got_pathlist_free(&commitable_paths);
4486 return err;
4489 const char *
4490 got_commitable_get_path(struct got_commitable *ct)
4492 return ct->path;
4495 unsigned int
4496 got_commitable_get_status(struct got_commitable *ct)
4498 return ct->status;
4501 struct check_rebase_ok_arg {
4502 struct got_worktree *worktree;
4503 struct got_repository *repo;
4506 static const struct got_error *
4507 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
4509 const struct got_error *err = NULL;
4510 struct check_rebase_ok_arg *a = arg;
4511 unsigned char status;
4512 struct stat sb;
4513 char *ondisk_path;
4515 /* Reject rebase of a work tree with mixed base commits. */
4516 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
4517 SHA1_DIGEST_LENGTH))
4518 return got_error(GOT_ERR_MIXED_COMMITS);
4520 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
4521 == -1)
4522 return got_error_from_errno("asprintf");
4524 /* Reject rebase of a work tree with modified or staged files. */
4525 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
4526 free(ondisk_path);
4527 if (err)
4528 return err;
4530 if (status != GOT_STATUS_NO_CHANGE)
4531 return got_error(GOT_ERR_MODIFIED);
4532 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
4533 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
4535 return NULL;
4538 const struct got_error *
4539 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
4540 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
4541 struct got_worktree *worktree, struct got_reference *branch,
4542 struct got_repository *repo)
4544 const struct got_error *err = NULL;
4545 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4546 char *branch_ref_name = NULL;
4547 char *fileindex_path = NULL;
4548 struct check_rebase_ok_arg ok_arg;
4549 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
4551 *new_base_branch_ref = NULL;
4552 *tmp_branch = NULL;
4553 *fileindex = NULL;
4555 err = lock_worktree(worktree, LOCK_EX);
4556 if (err)
4557 return err;
4559 err = open_fileindex(fileindex, &fileindex_path, worktree);
4560 if (err)
4561 goto done;
4563 ok_arg.worktree = worktree;
4564 ok_arg.repo = repo;
4565 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4566 &ok_arg);
4567 if (err)
4568 goto done;
4570 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4571 if (err)
4572 goto done;
4574 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4575 if (err)
4576 goto done;
4578 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4579 if (err)
4580 goto done;
4582 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
4583 0);
4584 if (err)
4585 goto done;
4587 err = got_ref_alloc_symref(new_base_branch_ref,
4588 new_base_branch_ref_name, wt_branch);
4589 if (err)
4590 goto done;
4591 err = got_ref_write(*new_base_branch_ref, repo);
4592 if (err)
4593 goto done;
4595 /* TODO Lock original branch's ref while rebasing? */
4597 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
4598 if (err)
4599 goto done;
4601 err = got_ref_write(branch_ref, repo);
4602 if (err)
4603 goto done;
4605 err = got_ref_alloc(tmp_branch, tmp_branch_name,
4606 worktree->base_commit_id);
4607 if (err)
4608 goto done;
4609 err = got_ref_write(*tmp_branch, repo);
4610 if (err)
4611 goto done;
4613 err = got_worktree_set_head_ref(worktree, *tmp_branch);
4614 if (err)
4615 goto done;
4616 done:
4617 free(fileindex_path);
4618 free(tmp_branch_name);
4619 free(new_base_branch_ref_name);
4620 free(branch_ref_name);
4621 if (branch_ref)
4622 got_ref_close(branch_ref);
4623 if (wt_branch)
4624 got_ref_close(wt_branch);
4625 if (err) {
4626 if (*new_base_branch_ref) {
4627 got_ref_close(*new_base_branch_ref);
4628 *new_base_branch_ref = NULL;
4630 if (*tmp_branch) {
4631 got_ref_close(*tmp_branch);
4632 *tmp_branch = NULL;
4634 if (*fileindex) {
4635 got_fileindex_free(*fileindex);
4636 *fileindex = NULL;
4638 lock_worktree(worktree, LOCK_SH);
4640 return err;
4643 const struct got_error *
4644 got_worktree_rebase_continue(struct got_object_id **commit_id,
4645 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
4646 struct got_reference **branch, struct got_fileindex **fileindex,
4647 struct got_worktree *worktree, struct got_repository *repo)
4649 const struct got_error *err;
4650 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
4651 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
4652 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
4653 char *fileindex_path = NULL;
4654 int have_staged_files = 0;
4656 *commit_id = NULL;
4657 *new_base_branch = NULL;
4658 *tmp_branch = NULL;
4659 *branch = NULL;
4660 *fileindex = NULL;
4662 err = lock_worktree(worktree, LOCK_EX);
4663 if (err)
4664 return err;
4666 err = open_fileindex(fileindex, &fileindex_path, worktree);
4667 if (err)
4668 goto done;
4670 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
4671 &have_staged_files);
4672 if (err && err->code != GOT_ERR_CANCELLED)
4673 goto done;
4674 if (have_staged_files) {
4675 err = got_error(GOT_ERR_STAGED_PATHS);
4676 goto done;
4679 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4680 if (err)
4681 goto done;
4683 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4684 if (err)
4685 goto done;
4687 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4688 if (err)
4689 goto done;
4691 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4692 if (err)
4693 goto done;
4695 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
4696 if (err)
4697 goto done;
4699 err = got_ref_open(branch, repo,
4700 got_ref_get_symref_target(branch_ref), 0);
4701 if (err)
4702 goto done;
4704 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4705 if (err)
4706 goto done;
4708 err = got_ref_resolve(commit_id, repo, commit_ref);
4709 if (err)
4710 goto done;
4712 err = got_ref_open(new_base_branch, repo,
4713 new_base_branch_ref_name, 0);
4714 if (err)
4715 goto done;
4717 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4718 if (err)
4719 goto done;
4720 done:
4721 free(commit_ref_name);
4722 free(branch_ref_name);
4723 free(fileindex_path);
4724 if (commit_ref)
4725 got_ref_close(commit_ref);
4726 if (branch_ref)
4727 got_ref_close(branch_ref);
4728 if (err) {
4729 free(*commit_id);
4730 *commit_id = NULL;
4731 if (*tmp_branch) {
4732 got_ref_close(*tmp_branch);
4733 *tmp_branch = NULL;
4735 if (*new_base_branch) {
4736 got_ref_close(*new_base_branch);
4737 *new_base_branch = NULL;
4739 if (*branch) {
4740 got_ref_close(*branch);
4741 *branch = NULL;
4743 if (*fileindex) {
4744 got_fileindex_free(*fileindex);
4745 *fileindex = NULL;
4747 lock_worktree(worktree, LOCK_SH);
4749 return err;
4752 const struct got_error *
4753 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
4755 const struct got_error *err;
4756 char *tmp_branch_name = NULL;
4758 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4759 if (err)
4760 return err;
4762 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4763 free(tmp_branch_name);
4764 return NULL;
4767 static const struct got_error *
4768 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
4769 char **logmsg, void *arg)
4771 *logmsg = arg;
4772 return NULL;
4775 static const struct got_error *
4776 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
4777 const char *path, struct got_object_id *blob_id,
4778 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
4780 return NULL;
4783 struct collect_merged_paths_arg {
4784 got_worktree_checkout_cb progress_cb;
4785 void *progress_arg;
4786 struct got_pathlist_head *merged_paths;
4789 static const struct got_error *
4790 collect_merged_paths(void *arg, unsigned char status, const char *path)
4792 const struct got_error *err;
4793 struct collect_merged_paths_arg *a = arg;
4794 char *p;
4795 struct got_pathlist_entry *new;
4797 err = (*a->progress_cb)(a->progress_arg, status, path);
4798 if (err)
4799 return err;
4801 if (status != GOT_STATUS_MERGE &&
4802 status != GOT_STATUS_ADD &&
4803 status != GOT_STATUS_DELETE &&
4804 status != GOT_STATUS_CONFLICT)
4805 return NULL;
4807 p = strdup(path);
4808 if (p == NULL)
4809 return got_error_from_errno("strdup");
4811 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
4812 if (err || new == NULL)
4813 free(p);
4814 return err;
4817 void
4818 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
4820 struct got_pathlist_entry *pe;
4822 TAILQ_FOREACH(pe, merged_paths, entry)
4823 free((char *)pe->path);
4825 got_pathlist_free(merged_paths);
4828 static const struct got_error *
4829 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
4830 struct got_repository *repo)
4832 const struct got_error *err;
4833 struct got_reference *commit_ref = NULL;
4835 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4836 if (err) {
4837 if (err->code != GOT_ERR_NOT_REF)
4838 goto done;
4839 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
4840 if (err)
4841 goto done;
4842 err = got_ref_write(commit_ref, repo);
4843 if (err)
4844 goto done;
4845 } else {
4846 struct got_object_id *stored_id;
4847 int cmp;
4849 err = got_ref_resolve(&stored_id, repo, commit_ref);
4850 if (err)
4851 goto done;
4852 cmp = got_object_id_cmp(commit_id, stored_id);
4853 free(stored_id);
4854 if (cmp != 0) {
4855 err = got_error(GOT_ERR_REBASE_COMMITID);
4856 goto done;
4859 done:
4860 if (commit_ref)
4861 got_ref_close(commit_ref);
4862 return err;
4865 static const struct got_error *
4866 rebase_merge_files(struct got_pathlist_head *merged_paths,
4867 const char *commit_ref_name, struct got_worktree *worktree,
4868 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
4869 struct got_object_id *commit_id, struct got_repository *repo,
4870 got_worktree_checkout_cb progress_cb, void *progress_arg,
4871 got_cancel_cb cancel_cb, void *cancel_arg)
4873 const struct got_error *err;
4874 struct got_reference *commit_ref = NULL;
4875 struct collect_merged_paths_arg cmp_arg;
4876 char *fileindex_path;
4878 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4880 err = get_fileindex_path(&fileindex_path, worktree);
4881 if (err)
4882 return err;
4884 cmp_arg.progress_cb = progress_cb;
4885 cmp_arg.progress_arg = progress_arg;
4886 cmp_arg.merged_paths = merged_paths;
4887 err = merge_files(worktree, fileindex, fileindex_path,
4888 parent_commit_id, commit_id, repo, collect_merged_paths,
4889 &cmp_arg, cancel_cb, cancel_arg);
4890 if (commit_ref)
4891 got_ref_close(commit_ref);
4892 return err;
4895 const struct got_error *
4896 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
4897 struct got_worktree *worktree, struct got_fileindex *fileindex,
4898 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4899 struct got_repository *repo,
4900 got_worktree_checkout_cb progress_cb, void *progress_arg,
4901 got_cancel_cb cancel_cb, void *cancel_arg)
4903 const struct got_error *err;
4904 char *commit_ref_name;
4906 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4907 if (err)
4908 return err;
4910 err = store_commit_id(commit_ref_name, commit_id, repo);
4911 if (err)
4912 goto done;
4914 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4915 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4916 progress_arg, cancel_cb, cancel_arg);
4917 done:
4918 free(commit_ref_name);
4919 return err;
4922 const struct got_error *
4923 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
4924 struct got_worktree *worktree, struct got_fileindex *fileindex,
4925 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4926 struct got_repository *repo,
4927 got_worktree_checkout_cb progress_cb, void *progress_arg,
4928 got_cancel_cb cancel_cb, void *cancel_arg)
4930 const struct got_error *err;
4931 char *commit_ref_name;
4933 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4934 if (err)
4935 return err;
4937 err = store_commit_id(commit_ref_name, commit_id, repo);
4938 if (err)
4939 goto done;
4941 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4942 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4943 progress_arg, cancel_cb, cancel_arg);
4944 done:
4945 free(commit_ref_name);
4946 return err;
4949 static const struct got_error *
4950 rebase_commit(struct got_object_id **new_commit_id,
4951 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
4952 struct got_worktree *worktree, struct got_fileindex *fileindex,
4953 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
4954 const char *new_logmsg, struct got_repository *repo)
4956 const struct got_error *err, *sync_err;
4957 struct got_pathlist_head commitable_paths;
4958 struct collect_commitables_arg cc_arg;
4959 char *fileindex_path = NULL;
4960 struct got_reference *head_ref = NULL;
4961 struct got_object_id *head_commit_id = NULL;
4962 char *logmsg = NULL;
4964 TAILQ_INIT(&commitable_paths);
4965 *new_commit_id = NULL;
4967 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4969 err = get_fileindex_path(&fileindex_path, worktree);
4970 if (err)
4971 return err;
4973 cc_arg.commitable_paths = &commitable_paths;
4974 cc_arg.worktree = worktree;
4975 cc_arg.repo = repo;
4976 cc_arg.have_staged_files = 0;
4978 * If possible get the status of individual files directly to
4979 * avoid crawling the entire work tree once per rebased commit.
4980 * TODO: Ideally, merged_paths would contain a list of commitables
4981 * we could use so we could skip worktree_status() entirely.
4983 if (merged_paths) {
4984 struct got_pathlist_entry *pe;
4985 if (TAILQ_EMPTY(merged_paths)) {
4986 err = got_error(GOT_ERR_NO_MERGED_PATHS);
4987 goto done;
4989 TAILQ_FOREACH(pe, merged_paths, entry) {
4990 err = worktree_status(worktree, pe->path, fileindex,
4991 repo, collect_commitables, &cc_arg, NULL, NULL);
4992 if (err)
4993 goto done;
4995 } else {
4996 err = worktree_status(worktree, "", fileindex, repo,
4997 collect_commitables, &cc_arg, NULL, NULL);
4998 if (err)
4999 goto done;
5002 if (TAILQ_EMPTY(&commitable_paths)) {
5003 /* No-op change; commit will be elided. */
5004 err = got_ref_delete(commit_ref, repo);
5005 if (err)
5006 goto done;
5007 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5008 goto done;
5011 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5012 if (err)
5013 goto done;
5015 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5016 if (err)
5017 goto done;
5019 if (new_logmsg) {
5020 logmsg = strdup(new_logmsg);
5021 if (logmsg == NULL) {
5022 err = got_error_from_errno("strdup");
5023 goto done;
5025 } else {
5026 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
5027 if (err)
5028 goto done;
5031 /* NB: commit_worktree will call free(logmsg) */
5032 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
5033 worktree, got_object_commit_get_author(orig_commit),
5034 got_object_commit_get_committer(orig_commit),
5035 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
5036 if (err)
5037 goto done;
5039 err = got_ref_change_ref(tmp_branch, *new_commit_id);
5040 if (err)
5041 goto done;
5043 err = got_ref_delete(commit_ref, repo);
5044 if (err)
5045 goto done;
5047 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5048 fileindex, 0);
5049 sync_err = sync_fileindex(fileindex, fileindex_path);
5050 if (sync_err && err == NULL)
5051 err = sync_err;
5052 done:
5053 free(fileindex_path);
5054 free(head_commit_id);
5055 if (head_ref)
5056 got_ref_close(head_ref);
5057 if (err) {
5058 free(*new_commit_id);
5059 *new_commit_id = NULL;
5061 return err;
5064 const struct got_error *
5065 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
5066 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5067 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5068 struct got_commit_object *orig_commit,
5069 struct got_object_id *orig_commit_id, struct got_repository *repo)
5071 const struct got_error *err;
5072 char *commit_ref_name;
5073 struct got_reference *commit_ref = NULL;
5074 struct got_object_id *commit_id = NULL;
5076 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5077 if (err)
5078 return err;
5080 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5081 if (err)
5082 goto done;
5083 err = got_ref_resolve(&commit_id, repo, commit_ref);
5084 if (err)
5085 goto done;
5086 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5087 err = got_error(GOT_ERR_REBASE_COMMITID);
5088 goto done;
5091 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5092 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
5093 done:
5094 if (commit_ref)
5095 got_ref_close(commit_ref);
5096 free(commit_ref_name);
5097 free(commit_id);
5098 return err;
5101 const struct got_error *
5102 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
5103 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5104 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5105 struct got_commit_object *orig_commit,
5106 struct got_object_id *orig_commit_id, const char *new_logmsg,
5107 struct got_repository *repo)
5109 const struct got_error *err;
5110 char *commit_ref_name;
5111 struct got_reference *commit_ref = NULL;
5112 struct got_object_id *commit_id = NULL;
5114 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5115 if (err)
5116 return err;
5118 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5119 if (err)
5120 goto done;
5121 err = got_ref_resolve(&commit_id, repo, commit_ref);
5122 if (err)
5123 goto done;
5124 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5125 err = got_error(GOT_ERR_HISTEDIT_COMMITID);
5126 goto done;
5129 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5130 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
5131 done:
5132 if (commit_ref)
5133 got_ref_close(commit_ref);
5134 free(commit_ref_name);
5135 free(commit_id);
5136 return err;
5139 const struct got_error *
5140 got_worktree_rebase_postpone(struct got_worktree *worktree,
5141 struct got_fileindex *fileindex)
5143 if (fileindex)
5144 got_fileindex_free(fileindex);
5145 return lock_worktree(worktree, LOCK_SH);
5148 static const struct got_error *
5149 delete_ref(const char *name, struct got_repository *repo)
5151 const struct got_error *err;
5152 struct got_reference *ref;
5154 err = got_ref_open(&ref, repo, name, 0);
5155 if (err) {
5156 if (err->code == GOT_ERR_NOT_REF)
5157 return NULL;
5158 return err;
5161 err = got_ref_delete(ref, repo);
5162 got_ref_close(ref);
5163 return err;
5166 static const struct got_error *
5167 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
5169 const struct got_error *err;
5170 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5171 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5173 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5174 if (err)
5175 goto done;
5176 err = delete_ref(tmp_branch_name, repo);
5177 if (err)
5178 goto done;
5180 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5181 if (err)
5182 goto done;
5183 err = delete_ref(new_base_branch_ref_name, repo);
5184 if (err)
5185 goto done;
5187 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5188 if (err)
5189 goto done;
5190 err = delete_ref(branch_ref_name, repo);
5191 if (err)
5192 goto done;
5194 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5195 if (err)
5196 goto done;
5197 err = delete_ref(commit_ref_name, repo);
5198 if (err)
5199 goto done;
5201 done:
5202 free(tmp_branch_name);
5203 free(new_base_branch_ref_name);
5204 free(branch_ref_name);
5205 free(commit_ref_name);
5206 return err;
5209 const struct got_error *
5210 got_worktree_rebase_complete(struct got_worktree *worktree,
5211 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
5212 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
5213 struct got_repository *repo)
5215 const struct got_error *err, *unlockerr;
5216 struct got_object_id *new_head_commit_id = NULL;
5218 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5219 if (err)
5220 return err;
5222 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
5223 if (err)
5224 goto done;
5226 err = got_ref_write(rebased_branch, repo);
5227 if (err)
5228 goto done;
5230 err = got_worktree_set_head_ref(worktree, rebased_branch);
5231 if (err)
5232 goto done;
5234 err = delete_rebase_refs(worktree, repo);
5235 done:
5236 if (fileindex)
5237 got_fileindex_free(fileindex);
5238 free(new_head_commit_id);
5239 unlockerr = lock_worktree(worktree, LOCK_SH);
5240 if (unlockerr && err == NULL)
5241 err = unlockerr;
5242 return err;
5245 const struct got_error *
5246 got_worktree_rebase_abort(struct got_worktree *worktree,
5247 struct got_fileindex *fileindex, struct got_repository *repo,
5248 struct got_reference *new_base_branch,
5249 got_worktree_checkout_cb progress_cb, void *progress_arg)
5251 const struct got_error *err, *unlockerr, *sync_err;
5252 struct got_reference *resolved = NULL;
5253 struct got_object_id *commit_id = NULL;
5254 char *fileindex_path = NULL;
5255 struct revert_file_args rfa;
5256 struct got_object_id *tree_id = NULL;
5258 err = lock_worktree(worktree, LOCK_EX);
5259 if (err)
5260 return err;
5262 err = got_ref_open(&resolved, repo,
5263 got_ref_get_symref_target(new_base_branch), 0);
5264 if (err)
5265 goto done;
5267 err = got_worktree_set_head_ref(worktree, resolved);
5268 if (err)
5269 goto done;
5272 * XXX commits to the base branch could have happened while
5273 * we were busy rebasing; should we store the original commit ID
5274 * when rebase begins and read it back here?
5276 err = got_ref_resolve(&commit_id, repo, resolved);
5277 if (err)
5278 goto done;
5280 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5281 if (err)
5282 goto done;
5284 err = got_object_id_by_path(&tree_id, repo,
5285 worktree->base_commit_id, worktree->path_prefix);
5286 if (err)
5287 goto done;
5289 err = delete_rebase_refs(worktree, repo);
5290 if (err)
5291 goto done;
5293 err = get_fileindex_path(&fileindex_path, worktree);
5294 if (err)
5295 goto done;
5297 rfa.worktree = worktree;
5298 rfa.fileindex = fileindex;
5299 rfa.progress_cb = progress_cb;
5300 rfa.progress_arg = progress_arg;
5301 rfa.patch_cb = NULL;
5302 rfa.patch_arg = NULL;
5303 rfa.repo = repo;
5304 err = worktree_status(worktree, "", fileindex, repo,
5305 revert_file, &rfa, NULL, NULL);
5306 if (err)
5307 goto sync;
5309 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5310 repo, progress_cb, progress_arg, NULL, NULL);
5311 sync:
5312 sync_err = sync_fileindex(fileindex, fileindex_path);
5313 if (sync_err && err == NULL)
5314 err = sync_err;
5315 done:
5316 got_ref_close(resolved);
5317 free(tree_id);
5318 free(commit_id);
5319 if (fileindex)
5320 got_fileindex_free(fileindex);
5321 free(fileindex_path);
5323 unlockerr = lock_worktree(worktree, LOCK_SH);
5324 if (unlockerr && err == NULL)
5325 err = unlockerr;
5326 return err;
5329 const struct got_error *
5330 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
5331 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
5332 struct got_fileindex **fileindex, struct got_worktree *worktree,
5333 struct got_repository *repo)
5335 const struct got_error *err = NULL;
5336 char *tmp_branch_name = NULL;
5337 char *branch_ref_name = NULL;
5338 char *base_commit_ref_name = NULL;
5339 char *fileindex_path = NULL;
5340 struct check_rebase_ok_arg ok_arg;
5341 struct got_reference *wt_branch = NULL;
5342 struct got_reference *base_commit_ref = NULL;
5344 *tmp_branch = NULL;
5345 *branch_ref = NULL;
5346 *base_commit_id = NULL;
5347 *fileindex = NULL;
5349 err = lock_worktree(worktree, LOCK_EX);
5350 if (err)
5351 return err;
5353 err = open_fileindex(fileindex, &fileindex_path, worktree);
5354 if (err)
5355 goto done;
5357 ok_arg.worktree = worktree;
5358 ok_arg.repo = repo;
5359 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5360 &ok_arg);
5361 if (err)
5362 goto done;
5364 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5365 if (err)
5366 goto done;
5368 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5369 if (err)
5370 goto done;
5372 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5373 worktree);
5374 if (err)
5375 goto done;
5377 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5378 0);
5379 if (err)
5380 goto done;
5382 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
5383 if (err)
5384 goto done;
5386 err = got_ref_write(*branch_ref, repo);
5387 if (err)
5388 goto done;
5390 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
5391 worktree->base_commit_id);
5392 if (err)
5393 goto done;
5394 err = got_ref_write(base_commit_ref, repo);
5395 if (err)
5396 goto done;
5397 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
5398 if (*base_commit_id == NULL) {
5399 err = got_error_from_errno("got_object_id_dup");
5400 goto done;
5403 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5404 worktree->base_commit_id);
5405 if (err)
5406 goto done;
5407 err = got_ref_write(*tmp_branch, repo);
5408 if (err)
5409 goto done;
5411 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5412 if (err)
5413 goto done;
5414 done:
5415 free(fileindex_path);
5416 free(tmp_branch_name);
5417 free(branch_ref_name);
5418 free(base_commit_ref_name);
5419 if (wt_branch)
5420 got_ref_close(wt_branch);
5421 if (err) {
5422 if (*branch_ref) {
5423 got_ref_close(*branch_ref);
5424 *branch_ref = NULL;
5426 if (*tmp_branch) {
5427 got_ref_close(*tmp_branch);
5428 *tmp_branch = NULL;
5430 free(*base_commit_id);
5431 if (*fileindex) {
5432 got_fileindex_free(*fileindex);
5433 *fileindex = NULL;
5435 lock_worktree(worktree, LOCK_SH);
5437 return err;
5440 const struct got_error *
5441 got_worktree_histedit_postpone(struct got_worktree *worktree,
5442 struct got_fileindex *fileindex)
5444 if (fileindex)
5445 got_fileindex_free(fileindex);
5446 return lock_worktree(worktree, LOCK_SH);
5449 const struct got_error *
5450 got_worktree_histedit_in_progress(int *in_progress,
5451 struct got_worktree *worktree)
5453 const struct got_error *err;
5454 char *tmp_branch_name = NULL;
5456 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5457 if (err)
5458 return err;
5460 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5461 free(tmp_branch_name);
5462 return NULL;
5465 const struct got_error *
5466 got_worktree_histedit_continue(struct got_object_id **commit_id,
5467 struct got_reference **tmp_branch, struct got_reference **branch_ref,
5468 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
5469 struct got_worktree *worktree, struct got_repository *repo)
5471 const struct got_error *err;
5472 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
5473 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5474 struct got_reference *commit_ref = NULL;
5475 struct got_reference *base_commit_ref = NULL;
5476 char *fileindex_path = NULL;
5477 int have_staged_files = 0;
5479 *commit_id = NULL;
5480 *tmp_branch = NULL;
5481 *base_commit_id = NULL;
5482 *fileindex = NULL;
5484 err = lock_worktree(worktree, LOCK_EX);
5485 if (err)
5486 return err;
5488 err = open_fileindex(fileindex, &fileindex_path, worktree);
5489 if (err)
5490 goto done;
5492 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5493 &have_staged_files);
5494 if (err && err->code != GOT_ERR_CANCELLED)
5495 goto done;
5496 if (have_staged_files) {
5497 err = got_error(GOT_ERR_STAGED_PATHS);
5498 goto done;
5501 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5502 if (err)
5503 goto done;
5505 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5506 if (err)
5507 goto done;
5509 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5510 if (err)
5511 goto done;
5513 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5514 worktree);
5515 if (err)
5516 goto done;
5518 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
5519 if (err)
5520 goto done;
5522 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5523 if (err)
5524 goto done;
5525 err = got_ref_resolve(commit_id, repo, commit_ref);
5526 if (err)
5527 goto done;
5529 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
5530 if (err)
5531 goto done;
5532 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
5533 if (err)
5534 goto done;
5536 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5537 if (err)
5538 goto done;
5539 done:
5540 free(commit_ref_name);
5541 free(branch_ref_name);
5542 free(fileindex_path);
5543 if (commit_ref)
5544 got_ref_close(commit_ref);
5545 if (base_commit_ref)
5546 got_ref_close(base_commit_ref);
5547 if (err) {
5548 free(*commit_id);
5549 *commit_id = NULL;
5550 free(*base_commit_id);
5551 *base_commit_id = NULL;
5552 if (*tmp_branch) {
5553 got_ref_close(*tmp_branch);
5554 *tmp_branch = NULL;
5556 if (*fileindex) {
5557 got_fileindex_free(*fileindex);
5558 *fileindex = NULL;
5560 lock_worktree(worktree, LOCK_EX);
5562 return err;
5565 static const struct got_error *
5566 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
5568 const struct got_error *err;
5569 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
5570 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5572 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5573 if (err)
5574 goto done;
5575 err = delete_ref(tmp_branch_name, repo);
5576 if (err)
5577 goto done;
5579 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5580 worktree);
5581 if (err)
5582 goto done;
5583 err = delete_ref(base_commit_ref_name, repo);
5584 if (err)
5585 goto done;
5587 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5588 if (err)
5589 goto done;
5590 err = delete_ref(branch_ref_name, repo);
5591 if (err)
5592 goto done;
5594 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5595 if (err)
5596 goto done;
5597 err = delete_ref(commit_ref_name, repo);
5598 if (err)
5599 goto done;
5600 done:
5601 free(tmp_branch_name);
5602 free(base_commit_ref_name);
5603 free(branch_ref_name);
5604 free(commit_ref_name);
5605 return err;
5608 const struct got_error *
5609 got_worktree_histedit_abort(struct got_worktree *worktree,
5610 struct got_fileindex *fileindex, struct got_repository *repo,
5611 struct got_reference *branch, struct got_object_id *base_commit_id,
5612 got_worktree_checkout_cb progress_cb, void *progress_arg)
5614 const struct got_error *err, *unlockerr, *sync_err;
5615 struct got_reference *resolved = NULL;
5616 char *fileindex_path = NULL;
5617 struct got_object_id *tree_id = NULL;
5618 struct revert_file_args rfa;
5620 err = lock_worktree(worktree, LOCK_EX);
5621 if (err)
5622 return err;
5624 err = got_ref_open(&resolved, repo,
5625 got_ref_get_symref_target(branch), 0);
5626 if (err)
5627 goto done;
5629 err = got_worktree_set_head_ref(worktree, resolved);
5630 if (err)
5631 goto done;
5633 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
5634 if (err)
5635 goto done;
5637 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
5638 worktree->path_prefix);
5639 if (err)
5640 goto done;
5642 err = delete_histedit_refs(worktree, repo);
5643 if (err)
5644 goto done;
5646 err = get_fileindex_path(&fileindex_path, worktree);
5647 if (err)
5648 goto done;
5650 rfa.worktree = worktree;
5651 rfa.fileindex = fileindex;
5652 rfa.progress_cb = progress_cb;
5653 rfa.progress_arg = progress_arg;
5654 rfa.patch_cb = NULL;
5655 rfa.patch_arg = NULL;
5656 rfa.repo = repo;
5657 err = worktree_status(worktree, "", fileindex, repo,
5658 revert_file, &rfa, NULL, NULL);
5659 if (err)
5660 goto sync;
5662 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5663 repo, progress_cb, progress_arg, NULL, NULL);
5664 sync:
5665 sync_err = sync_fileindex(fileindex, fileindex_path);
5666 if (sync_err && err == NULL)
5667 err = sync_err;
5668 done:
5669 got_ref_close(resolved);
5670 free(tree_id);
5671 free(fileindex_path);
5673 unlockerr = lock_worktree(worktree, LOCK_SH);
5674 if (unlockerr && err == NULL)
5675 err = unlockerr;
5676 return err;
5679 const struct got_error *
5680 got_worktree_histedit_complete(struct got_worktree *worktree,
5681 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5682 struct got_reference *edited_branch, struct got_repository *repo)
5684 const struct got_error *err, *unlockerr;
5685 struct got_object_id *new_head_commit_id = NULL;
5686 struct got_reference *resolved = NULL;
5688 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5689 if (err)
5690 return err;
5692 err = got_ref_open(&resolved, repo,
5693 got_ref_get_symref_target(edited_branch), 0);
5694 if (err)
5695 goto done;
5697 err = got_ref_change_ref(resolved, new_head_commit_id);
5698 if (err)
5699 goto done;
5701 err = got_ref_write(resolved, repo);
5702 if (err)
5703 goto done;
5705 err = got_worktree_set_head_ref(worktree, resolved);
5706 if (err)
5707 goto done;
5709 err = delete_histedit_refs(worktree, repo);
5710 done:
5711 if (fileindex)
5712 got_fileindex_free(fileindex);
5713 free(new_head_commit_id);
5714 unlockerr = lock_worktree(worktree, LOCK_SH);
5715 if (unlockerr && err == NULL)
5716 err = unlockerr;
5717 return err;
5720 const struct got_error *
5721 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
5722 struct got_object_id *commit_id, struct got_repository *repo)
5724 const struct got_error *err;
5725 char *commit_ref_name;
5727 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5728 if (err)
5729 return err;
5731 err = store_commit_id(commit_ref_name, commit_id, repo);
5732 if (err)
5733 goto done;
5735 err = delete_ref(commit_ref_name, repo);
5736 done:
5737 free(commit_ref_name);
5738 return err;
5741 const struct got_error *
5742 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
5743 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
5744 struct got_worktree *worktree, const char *refname,
5745 struct got_repository *repo)
5747 const struct got_error *err = NULL;
5748 char *fileindex_path = NULL;
5749 struct check_rebase_ok_arg ok_arg;
5751 *fileindex = NULL;
5752 *branch_ref = NULL;
5753 *base_branch_ref = NULL;
5755 err = lock_worktree(worktree, LOCK_EX);
5756 if (err)
5757 return err;
5759 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
5760 err = got_error_msg(GOT_ERR_SAME_BRANCH,
5761 "cannot integrate a branch into itself; "
5762 "update -b or different branch name required");
5763 goto done;
5766 err = open_fileindex(fileindex, &fileindex_path, worktree);
5767 if (err)
5768 goto done;
5770 /* Preconditions are the same as for rebase. */
5771 ok_arg.worktree = worktree;
5772 ok_arg.repo = repo;
5773 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5774 &ok_arg);
5775 if (err)
5776 goto done;
5778 err = got_ref_open(branch_ref, repo, refname, 1);
5779 if (err)
5780 goto done;
5782 err = got_ref_open(base_branch_ref, repo,
5783 got_worktree_get_head_ref_name(worktree), 1);
5784 done:
5785 if (err) {
5786 if (*branch_ref) {
5787 got_ref_close(*branch_ref);
5788 *branch_ref = NULL;
5790 if (*base_branch_ref) {
5791 got_ref_close(*base_branch_ref);
5792 *base_branch_ref = NULL;
5794 if (*fileindex) {
5795 got_fileindex_free(*fileindex);
5796 *fileindex = NULL;
5798 lock_worktree(worktree, LOCK_SH);
5800 return err;
5803 const struct got_error *
5804 got_worktree_integrate_continue(struct got_worktree *worktree,
5805 struct got_fileindex *fileindex, struct got_repository *repo,
5806 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
5807 got_worktree_checkout_cb progress_cb, void *progress_arg,
5808 got_cancel_cb cancel_cb, void *cancel_arg)
5810 const struct got_error *err = NULL, *sync_err, *unlockerr;
5811 char *fileindex_path = NULL;
5812 struct got_object_id *tree_id = NULL, *commit_id = NULL;
5814 err = get_fileindex_path(&fileindex_path, worktree);
5815 if (err)
5816 goto done;
5818 err = got_ref_resolve(&commit_id, repo, branch_ref);
5819 if (err)
5820 goto done;
5822 err = got_object_id_by_path(&tree_id, repo, commit_id,
5823 worktree->path_prefix);
5824 if (err)
5825 goto done;
5827 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5828 if (err)
5829 goto done;
5831 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
5832 progress_cb, progress_arg, cancel_cb, cancel_arg);
5833 if (err)
5834 goto sync;
5836 err = got_ref_change_ref(base_branch_ref, commit_id);
5837 if (err)
5838 goto sync;
5840 err = got_ref_write(base_branch_ref, repo);
5841 sync:
5842 sync_err = sync_fileindex(fileindex, fileindex_path);
5843 if (sync_err && err == NULL)
5844 err = sync_err;
5846 done:
5847 unlockerr = got_ref_unlock(branch_ref);
5848 if (unlockerr && err == NULL)
5849 err = unlockerr;
5850 got_ref_close(branch_ref);
5852 unlockerr = got_ref_unlock(base_branch_ref);
5853 if (unlockerr && err == NULL)
5854 err = unlockerr;
5855 got_ref_close(base_branch_ref);
5857 got_fileindex_free(fileindex);
5858 free(fileindex_path);
5859 free(tree_id);
5861 unlockerr = lock_worktree(worktree, LOCK_SH);
5862 if (unlockerr && err == NULL)
5863 err = unlockerr;
5864 return err;
5867 const struct got_error *
5868 got_worktree_integrate_abort(struct got_worktree *worktree,
5869 struct got_fileindex *fileindex, struct got_repository *repo,
5870 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
5872 const struct got_error *err = NULL, *unlockerr = NULL;
5874 got_fileindex_free(fileindex);
5876 err = lock_worktree(worktree, LOCK_SH);
5878 unlockerr = got_ref_unlock(branch_ref);
5879 if (unlockerr && err == NULL)
5880 err = unlockerr;
5881 got_ref_close(branch_ref);
5883 unlockerr = got_ref_unlock(base_branch_ref);
5884 if (unlockerr && err == NULL)
5885 err = unlockerr;
5886 got_ref_close(base_branch_ref);
5888 return err;
5891 struct check_stage_ok_arg {
5892 struct got_object_id *head_commit_id;
5893 struct got_worktree *worktree;
5894 struct got_fileindex *fileindex;
5895 struct got_repository *repo;
5896 int have_changes;
5899 const struct got_error *
5900 check_stage_ok(void *arg, unsigned char status,
5901 unsigned char staged_status, const char *relpath,
5902 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5903 struct got_object_id *commit_id)
5905 struct check_stage_ok_arg *a = arg;
5906 const struct got_error *err = NULL;
5907 struct got_fileindex_entry *ie;
5908 struct got_object_id base_commit_id;
5909 struct got_object_id *base_commit_idp = NULL;
5910 char *in_repo_path = NULL, *p;
5912 if (status == GOT_STATUS_UNVERSIONED ||
5913 status == GOT_STATUS_NO_CHANGE)
5914 return NULL;
5915 if (status == GOT_STATUS_NONEXISTENT)
5916 return got_error_set_errno(ENOENT, relpath);
5918 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5919 if (ie == NULL)
5920 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
5922 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
5923 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5924 relpath) == -1)
5925 return got_error_from_errno("asprintf");
5927 if (got_fileindex_entry_has_commit(ie)) {
5928 memcpy(base_commit_id.sha1, ie->commit_sha1,
5929 SHA1_DIGEST_LENGTH);
5930 base_commit_idp = &base_commit_id;
5933 if (status == GOT_STATUS_CONFLICT) {
5934 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
5935 goto done;
5936 } else if (status != GOT_STATUS_ADD &&
5937 status != GOT_STATUS_MODIFY &&
5938 status != GOT_STATUS_DELETE) {
5939 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
5940 goto done;
5943 a->have_changes = 1;
5945 p = in_repo_path;
5946 while (p[0] == '/')
5947 p++;
5948 err = check_out_of_date(p, status, staged_status,
5949 blob_id, base_commit_idp, a->head_commit_id, a->repo,
5950 GOT_ERR_STAGE_OUT_OF_DATE);
5951 done:
5952 free(in_repo_path);
5953 return err;
5956 struct stage_path_arg {
5957 struct got_worktree *worktree;
5958 struct got_fileindex *fileindex;
5959 struct got_repository *repo;
5960 got_worktree_status_cb status_cb;
5961 void *status_arg;
5962 got_worktree_patch_cb patch_cb;
5963 void *patch_arg;
5964 int staged_something;
5967 static const struct got_error *
5968 stage_path(void *arg, unsigned char status,
5969 unsigned char staged_status, const char *relpath,
5970 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5971 struct got_object_id *commit_id)
5973 struct stage_path_arg *a = arg;
5974 const struct got_error *err = NULL;
5975 struct got_fileindex_entry *ie;
5976 char *ondisk_path = NULL, *path_content = NULL;
5977 uint32_t stage;
5978 struct got_object_id *new_staged_blob_id = NULL;
5980 if (status == GOT_STATUS_UNVERSIONED)
5981 return NULL;
5983 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5984 if (ie == NULL)
5985 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
5987 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
5988 relpath)== -1)
5989 return got_error_from_errno("asprintf");
5991 switch (status) {
5992 case GOT_STATUS_ADD:
5993 case GOT_STATUS_MODIFY:
5994 if (a->patch_cb) {
5995 if (status == GOT_STATUS_ADD) {
5996 int choice = GOT_PATCH_CHOICE_NONE;
5997 err = (*a->patch_cb)(&choice, a->patch_arg,
5998 status, ie->path, NULL, 1, 1);
5999 if (err)
6000 break;
6001 if (choice != GOT_PATCH_CHOICE_YES)
6002 break;
6003 } else {
6004 err = create_patched_content(&path_content, 0,
6005 staged_blob_id ? staged_blob_id : blob_id,
6006 ondisk_path, ie->path, a->repo,
6007 a->patch_cb, a->patch_arg);
6008 if (err || path_content == NULL)
6009 break;
6012 err = got_object_blob_create(&new_staged_blob_id,
6013 path_content ? path_content : ondisk_path, a->repo);
6014 if (err)
6015 break;
6016 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
6017 SHA1_DIGEST_LENGTH);
6018 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
6019 stage = GOT_FILEIDX_STAGE_ADD;
6020 else
6021 stage = GOT_FILEIDX_STAGE_MODIFY;
6022 got_fileindex_entry_stage_set(ie, stage);
6023 a->staged_something = 1;
6024 if (a->status_cb == NULL)
6025 break;
6026 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6027 get_staged_status(ie), relpath, blob_id,
6028 new_staged_blob_id, NULL);
6029 break;
6030 case GOT_STATUS_DELETE:
6031 if (staged_status == GOT_STATUS_DELETE)
6032 break;
6033 if (a->patch_cb) {
6034 int choice = GOT_PATCH_CHOICE_NONE;
6035 err = (*a->patch_cb)(&choice, a->patch_arg, status,
6036 ie->path, NULL, 1, 1);
6037 if (err)
6038 break;
6039 if (choice == GOT_PATCH_CHOICE_NO)
6040 break;
6041 if (choice != GOT_PATCH_CHOICE_YES) {
6042 err = got_error(GOT_ERR_PATCH_CHOICE);
6043 break;
6046 stage = GOT_FILEIDX_STAGE_DELETE;
6047 got_fileindex_entry_stage_set(ie, stage);
6048 a->staged_something = 1;
6049 if (a->status_cb == NULL)
6050 break;
6051 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6052 get_staged_status(ie), relpath, NULL, NULL, NULL);
6053 break;
6054 case GOT_STATUS_NO_CHANGE:
6055 break;
6056 case GOT_STATUS_CONFLICT:
6057 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
6058 break;
6059 case GOT_STATUS_NONEXISTENT:
6060 err = got_error_set_errno(ENOENT, relpath);
6061 break;
6062 default:
6063 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
6064 break;
6067 if (path_content && unlink(path_content) == -1 && err == NULL)
6068 err = got_error_from_errno2("unlink", path_content);
6069 free(path_content);
6070 free(ondisk_path);
6071 free(new_staged_blob_id);
6072 return err;
6075 const struct got_error *
6076 got_worktree_stage(struct got_worktree *worktree,
6077 struct got_pathlist_head *paths,
6078 got_worktree_status_cb status_cb, void *status_arg,
6079 got_worktree_patch_cb patch_cb, void *patch_arg,
6080 struct got_repository *repo)
6082 const struct got_error *err = NULL, *sync_err, *unlockerr;
6083 struct got_pathlist_entry *pe;
6084 struct got_fileindex *fileindex = NULL;
6085 char *fileindex_path = NULL;
6086 struct got_reference *head_ref = NULL;
6087 struct got_object_id *head_commit_id = NULL;
6088 struct check_stage_ok_arg oka;
6089 struct stage_path_arg spa;
6091 err = lock_worktree(worktree, LOCK_EX);
6092 if (err)
6093 return err;
6095 err = got_ref_open(&head_ref, repo,
6096 got_worktree_get_head_ref_name(worktree), 0);
6097 if (err)
6098 goto done;
6099 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6100 if (err)
6101 goto done;
6102 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6103 if (err)
6104 goto done;
6106 /* Check pre-conditions before staging anything. */
6107 oka.head_commit_id = head_commit_id;
6108 oka.worktree = worktree;
6109 oka.fileindex = fileindex;
6110 oka.repo = repo;
6111 oka.have_changes = 0;
6112 TAILQ_FOREACH(pe, paths, entry) {
6113 err = worktree_status(worktree, pe->path, fileindex, repo,
6114 check_stage_ok, &oka, NULL, NULL);
6115 if (err)
6116 goto done;
6118 if (!oka.have_changes) {
6119 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6120 goto done;
6123 spa.worktree = worktree;
6124 spa.fileindex = fileindex;
6125 spa.repo = repo;
6126 spa.patch_cb = patch_cb;
6127 spa.patch_arg = patch_arg;
6128 spa.status_cb = status_cb;
6129 spa.status_arg = status_arg;
6130 spa.staged_something = 0;
6131 TAILQ_FOREACH(pe, paths, entry) {
6132 err = worktree_status(worktree, pe->path, fileindex, repo,
6133 stage_path, &spa, NULL, NULL);
6134 if (err)
6135 goto done;
6137 if (!spa.staged_something) {
6138 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6139 goto done;
6142 sync_err = sync_fileindex(fileindex, fileindex_path);
6143 if (sync_err && err == NULL)
6144 err = sync_err;
6145 done:
6146 if (head_ref)
6147 got_ref_close(head_ref);
6148 free(head_commit_id);
6149 free(fileindex_path);
6150 if (fileindex)
6151 got_fileindex_free(fileindex);
6152 unlockerr = lock_worktree(worktree, LOCK_SH);
6153 if (unlockerr && err == NULL)
6154 err = unlockerr;
6155 return err;
6158 struct unstage_path_arg {
6159 struct got_worktree *worktree;
6160 struct got_fileindex *fileindex;
6161 struct got_repository *repo;
6162 got_worktree_checkout_cb progress_cb;
6163 void *progress_arg;
6164 got_worktree_patch_cb patch_cb;
6165 void *patch_arg;
6168 static const struct got_error *
6169 create_unstaged_content(char **path_unstaged_content,
6170 char **path_new_staged_content, struct got_object_id *blob_id,
6171 struct got_object_id *staged_blob_id, const char *relpath,
6172 struct got_repository *repo,
6173 got_worktree_patch_cb patch_cb, void *patch_arg)
6175 const struct got_error *err;
6176 struct got_blob_object *blob = NULL, *staged_blob = NULL;
6177 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
6178 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
6179 struct stat sb1, sb2;
6180 struct got_diff_changes *changes = NULL;
6181 struct got_diff_state *ds = NULL;
6182 struct got_diff_args *args = NULL;
6183 struct got_diff_change *change;
6184 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
6185 int have_content = 0, have_rejected_content = 0;
6187 *path_unstaged_content = NULL;
6188 *path_new_staged_content = NULL;
6190 err = got_object_id_str(&label1, blob_id);
6191 if (err)
6192 return err;
6193 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
6194 if (err)
6195 goto done;
6197 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
6198 if (err)
6199 goto done;
6201 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
6202 if (err)
6203 goto done;
6205 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
6206 if (err)
6207 goto done;
6209 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
6210 if (err)
6211 goto done;
6213 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
6214 if (err)
6215 goto done;
6217 if (stat(path1, &sb1) == -1) {
6218 err = got_error_from_errno2("stat", path1);
6219 goto done;
6222 if (stat(path2, &sb2) == -1) {
6223 err = got_error_from_errno2("stat", path2);
6224 goto done;
6227 err = got_diff_files(&changes, &ds, &args, &diff_flags,
6228 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
6229 if (err)
6230 goto done;
6232 err = got_opentemp_named(path_unstaged_content, &outfile,
6233 "got-unstaged-content");
6234 if (err)
6235 goto done;
6236 err = got_opentemp_named(path_new_staged_content, &rejectfile,
6237 "got-new-staged-content");
6238 if (err)
6239 goto done;
6241 if (fseek(f1, 0L, SEEK_SET) == -1) {
6242 err = got_ferror(f1, GOT_ERR_IO);
6243 goto done;
6245 if (fseek(f2, 0L, SEEK_SET) == -1) {
6246 err = got_ferror(f2, GOT_ERR_IO);
6247 goto done;
6249 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
6250 int choice;
6251 err = apply_or_reject_change(&choice, change, ++n,
6252 changes->nchanges, ds, args, diff_flags, relpath,
6253 f1, f2, &line_cur1, &line_cur2,
6254 outfile, rejectfile, patch_cb, patch_arg);
6255 if (err)
6256 goto done;
6257 if (choice == GOT_PATCH_CHOICE_YES)
6258 have_content = 1;
6259 else
6260 have_rejected_content = 1;
6261 if (choice == GOT_PATCH_CHOICE_QUIT)
6262 break;
6264 if (have_content || have_rejected_content)
6265 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
6266 outfile, rejectfile);
6267 done:
6268 free(label1);
6269 if (blob)
6270 got_object_blob_close(blob);
6271 if (staged_blob)
6272 got_object_blob_close(staged_blob);
6273 if (f1 && fclose(f1) == EOF && err == NULL)
6274 err = got_error_from_errno2("fclose", path1);
6275 if (f2 && fclose(f2) == EOF && err == NULL)
6276 err = got_error_from_errno2("fclose", path2);
6277 if (outfile && fclose(outfile) == EOF && err == NULL)
6278 err = got_error_from_errno2("fclose", *path_unstaged_content);
6279 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
6280 err = got_error_from_errno2("fclose", *path_new_staged_content);
6281 if (path1 && unlink(path1) == -1 && err == NULL)
6282 err = got_error_from_errno2("unlink", path1);
6283 if (path2 && unlink(path2) == -1 && err == NULL)
6284 err = got_error_from_errno2("unlink", path2);
6285 if (err || !have_content) {
6286 if (*path_unstaged_content &&
6287 unlink(*path_unstaged_content) == -1 && err == NULL)
6288 err = got_error_from_errno2("unlink",
6289 *path_unstaged_content);
6290 free(*path_unstaged_content);
6291 *path_unstaged_content = NULL;
6293 if (err || !have_rejected_content) {
6294 if (*path_new_staged_content &&
6295 unlink(*path_new_staged_content) == -1 && err == NULL)
6296 err = got_error_from_errno2("unlink",
6297 *path_new_staged_content);
6298 free(*path_new_staged_content);
6299 *path_new_staged_content = NULL;
6301 free(args);
6302 if (ds) {
6303 got_diff_state_free(ds);
6304 free(ds);
6306 if (changes)
6307 got_diff_free_changes(changes);
6308 free(path1);
6309 free(path2);
6310 return err;
6313 static const struct got_error *
6314 unstage_path(void *arg, unsigned char status,
6315 unsigned char staged_status, const char *relpath,
6316 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6317 struct got_object_id *commit_id)
6319 const struct got_error *err = NULL;
6320 struct unstage_path_arg *a = arg;
6321 struct got_fileindex_entry *ie;
6322 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
6323 char *ondisk_path = NULL, *path_unstaged_content = NULL;
6324 char *path_new_staged_content = NULL;
6325 char *id_str = NULL, *label_orig = NULL;
6326 int local_changes_subsumed;
6327 struct stat sb;
6329 if (staged_status != GOT_STATUS_ADD &&
6330 staged_status != GOT_STATUS_MODIFY &&
6331 staged_status != GOT_STATUS_DELETE)
6332 return NULL;
6334 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6335 if (ie == NULL)
6336 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6338 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
6339 == -1)
6340 return got_error_from_errno("asprintf");
6342 err = got_object_id_str(&id_str,
6343 commit_id ? commit_id : a->worktree->base_commit_id);
6344 if (err)
6345 goto done;
6346 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
6347 id_str) == -1) {
6348 err = got_error_from_errno("asprintf");
6349 goto done;
6352 switch (staged_status) {
6353 case GOT_STATUS_MODIFY:
6354 err = got_object_open_as_blob(&blob_base, a->repo,
6355 blob_id, 8192);
6356 if (err)
6357 break;
6358 /* fall through */
6359 case GOT_STATUS_ADD:
6360 if (a->patch_cb) {
6361 if (staged_status == GOT_STATUS_ADD) {
6362 int choice = GOT_PATCH_CHOICE_NONE;
6363 err = (*a->patch_cb)(&choice, a->patch_arg,
6364 staged_status, ie->path, NULL, 1, 1);
6365 if (err)
6366 break;
6367 if (choice != GOT_PATCH_CHOICE_YES)
6368 break;
6369 } else {
6370 err = create_unstaged_content(
6371 &path_unstaged_content,
6372 &path_new_staged_content, blob_id,
6373 staged_blob_id, ie->path, a->repo,
6374 a->patch_cb, a->patch_arg);
6375 if (err || path_unstaged_content == NULL)
6376 break;
6377 if (path_new_staged_content) {
6378 err = got_object_blob_create(
6379 &staged_blob_id,
6380 path_new_staged_content,
6381 a->repo);
6382 if (err)
6383 break;
6384 memcpy(ie->staged_blob_sha1,
6385 staged_blob_id->sha1,
6386 SHA1_DIGEST_LENGTH);
6388 err = merge_file(&local_changes_subsumed,
6389 a->worktree, blob_base, ondisk_path,
6390 relpath, got_fileindex_perms_to_st(ie),
6391 path_unstaged_content, label_orig,
6392 "unstaged", a->repo, a->progress_cb,
6393 a->progress_arg);
6394 if (err == NULL &&
6395 path_new_staged_content == NULL)
6396 got_fileindex_entry_stage_set(ie,
6397 GOT_FILEIDX_STAGE_NONE);
6398 break; /* Done with this file. */
6401 err = got_object_open_as_blob(&blob_staged, a->repo,
6402 staged_blob_id, 8192);
6403 if (err)
6404 break;
6405 err = merge_blob(&local_changes_subsumed, a->worktree,
6406 blob_base, ondisk_path, relpath,
6407 got_fileindex_perms_to_st(ie), label_orig, blob_staged,
6408 commit_id ? commit_id : a->worktree->base_commit_id,
6409 a->repo, a->progress_cb, a->progress_arg);
6410 if (err == NULL)
6411 got_fileindex_entry_stage_set(ie,
6412 GOT_FILEIDX_STAGE_NONE);
6413 break;
6414 case GOT_STATUS_DELETE:
6415 if (a->patch_cb) {
6416 int choice = GOT_PATCH_CHOICE_NONE;
6417 err = (*a->patch_cb)(&choice, a->patch_arg,
6418 staged_status, ie->path, NULL, 1, 1);
6419 if (err)
6420 break;
6421 if (choice == GOT_PATCH_CHOICE_NO)
6422 break;
6423 if (choice != GOT_PATCH_CHOICE_YES) {
6424 err = got_error(GOT_ERR_PATCH_CHOICE);
6425 break;
6428 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
6429 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
6430 if (err)
6431 break;
6432 err = (*a->progress_cb)(a->progress_arg, status, relpath);
6433 break;
6435 done:
6436 free(ondisk_path);
6437 if (path_unstaged_content &&
6438 unlink(path_unstaged_content) == -1 && err == NULL)
6439 err = got_error_from_errno2("unlink", path_unstaged_content);
6440 if (path_new_staged_content &&
6441 unlink(path_new_staged_content) == -1 && err == NULL)
6442 err = got_error_from_errno2("unlink", path_new_staged_content);
6443 free(path_unstaged_content);
6444 free(path_new_staged_content);
6445 if (blob_base)
6446 got_object_blob_close(blob_base);
6447 if (blob_staged)
6448 got_object_blob_close(blob_staged);
6449 free(id_str);
6450 free(label_orig);
6451 return err;
6454 const struct got_error *
6455 got_worktree_unstage(struct got_worktree *worktree,
6456 struct got_pathlist_head *paths,
6457 got_worktree_checkout_cb progress_cb, void *progress_arg,
6458 got_worktree_patch_cb patch_cb, void *patch_arg,
6459 struct got_repository *repo)
6461 const struct got_error *err = NULL, *sync_err, *unlockerr;
6462 struct got_pathlist_entry *pe;
6463 struct got_fileindex *fileindex = NULL;
6464 char *fileindex_path = NULL;
6465 struct unstage_path_arg upa;
6467 err = lock_worktree(worktree, LOCK_EX);
6468 if (err)
6469 return err;
6471 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6472 if (err)
6473 goto done;
6475 upa.worktree = worktree;
6476 upa.fileindex = fileindex;
6477 upa.repo = repo;
6478 upa.progress_cb = progress_cb;
6479 upa.progress_arg = progress_arg;
6480 upa.patch_cb = patch_cb;
6481 upa.patch_arg = patch_arg;
6482 TAILQ_FOREACH(pe, paths, entry) {
6483 err = worktree_status(worktree, pe->path, fileindex, repo,
6484 unstage_path, &upa, NULL, NULL);
6485 if (err)
6486 goto done;
6489 sync_err = sync_fileindex(fileindex, fileindex_path);
6490 if (sync_err && err == NULL)
6491 err = sync_err;
6492 done:
6493 free(fileindex_path);
6494 if (fileindex)
6495 got_fileindex_free(fileindex);
6496 unlockerr = lock_worktree(worktree, LOCK_SH);
6497 if (unlockerr && err == NULL)
6498 err = unlockerr;
6499 return err;