2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
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.
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.
34 #include "got_compat.h"
36 #include "got_error.h"
37 #include "got_repository.h"
38 #include "got_reference.h"
39 #include "got_object.h"
41 #include "got_cancel.h"
42 #include "got_worktree.h"
43 #include "got_opentemp.h"
46 #include "got_lib_worktree.h"
47 #include "got_lib_sha1.h"
48 #include "got_lib_fileindex.h"
49 #include "got_lib_inflate.h"
50 #include "got_lib_delta.h"
51 #include "got_lib_object.h"
52 #include "got_lib_object_parse.h"
53 #include "got_lib_object_create.h"
54 #include "got_lib_object_idset.h"
55 #include "got_lib_diff.h"
56 #include "got_lib_gotconfig.h"
59 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
62 #define GOT_MERGE_LABEL_MERGED "merged change"
63 #define GOT_MERGE_LABEL_BASE "3-way merge base"
65 static const struct got_error *
66 create_meta_file(const char *path_got, const char *name, const char *content)
68 const struct got_error *err = NULL;
71 if (asprintf(&path, "%s/%s", path_got, name) == -1)
72 return got_error_from_errno("asprintf");
74 err = got_path_create_file(path, content);
79 static const struct got_error *
80 update_meta_file(const char *path_got, const char *name, const char *content)
82 const struct got_error *err = NULL;
87 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
88 err = got_error_from_errno("asprintf");
93 err = got_opentemp_named(&tmppath, &tmpfile, path);
98 int len = fprintf(tmpfile, "%s\n", content);
99 if (len != strlen(content) + 1) {
100 err = got_error_from_errno2("fprintf", tmppath);
105 if (rename(tmppath, path) != 0) {
106 err = got_error_from_errno3("rename", tmppath, path);
112 if (fclose(tmpfile) == EOF && err == NULL)
113 err = got_error_from_errno2("fclose", tmppath);
118 static const struct got_error *
119 write_head_ref(const char *path_got, struct got_reference *head_ref)
121 const struct got_error *err = NULL;
124 if (got_ref_is_symbolic(head_ref)) {
125 refstr = got_ref_to_str(head_ref);
127 return got_error_from_errno("got_ref_to_str");
129 refstr = strdup(got_ref_get_name(head_ref));
131 return got_error_from_errno("strdup");
133 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
138 const struct got_error *
139 got_worktree_init(const char *path, struct got_reference *head_ref,
140 const char *prefix, struct got_repository *repo)
142 const struct got_error *err = NULL;
143 struct got_object_id *commit_id = NULL;
145 uint32_t uuid_status;
147 char *path_got = NULL;
148 char *formatstr = NULL;
149 char *absprefix = NULL;
150 char *basestr = NULL;
151 char *uuidstr = NULL;
153 if (strcmp(path, got_repo_get_path(repo)) == 0) {
154 err = got_error(GOT_ERR_WORKTREE_REPO);
158 err = got_ref_resolve(&commit_id, repo, head_ref);
161 err = got_object_get_type(&obj_type, repo, commit_id);
164 if (obj_type != GOT_OBJ_TYPE_COMMIT)
165 return got_error(GOT_ERR_OBJ_TYPE);
167 if (!got_path_is_absolute(prefix)) {
168 if (asprintf(&absprefix, "/%s", prefix) == -1)
169 return got_error_from_errno("asprintf");
172 /* Create top-level directory (may already exist). */
173 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
174 err = got_error_from_errno2("mkdir", path);
178 /* Create .got directory (may already exist). */
179 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
180 err = got_error_from_errno("asprintf");
183 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
184 err = got_error_from_errno2("mkdir", path_got);
188 /* Create an empty lock file. */
189 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
193 /* Create an empty file index. */
194 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
198 /* Write the HEAD reference. */
199 err = write_head_ref(path_got, head_ref);
203 /* Record our base commit. */
204 err = got_object_id_str(&basestr, commit_id);
207 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
211 /* Store path to repository. */
212 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
213 got_repo_get_path(repo));
217 /* Store in-repository path prefix. */
218 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
219 absprefix ? absprefix : prefix);
224 uuid_create(&uuid, &uuid_status);
225 if (uuid_status != uuid_s_ok) {
226 err = got_error_uuid(uuid_status, "uuid_create");
229 uuid_to_string(&uuid, &uuidstr, &uuid_status);
230 if (uuid_status != uuid_s_ok) {
231 err = got_error_uuid(uuid_status, "uuid_to_string");
234 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
238 /* Stamp work tree with format file. */
239 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
240 err = got_error_from_errno("asprintf");
243 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
257 const struct got_error *
258 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
259 const char *path_prefix)
261 char *absprefix = NULL;
263 if (!got_path_is_absolute(path_prefix)) {
264 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
265 return got_error_from_errno("asprintf");
267 *match = (strcmp(absprefix ? absprefix : path_prefix,
268 worktree->path_prefix) == 0);
274 got_worktree_get_head_ref_name(struct got_worktree *worktree)
276 return worktree->head_ref_name;
279 const struct got_error *
280 got_worktree_set_head_ref(struct got_worktree *worktree,
281 struct got_reference *head_ref)
283 const struct got_error *err = NULL;
284 char *path_got = NULL, *head_ref_name = NULL;
286 if (asprintf(&path_got, "%s/%s", worktree->root_path,
287 GOT_WORKTREE_GOT_DIR) == -1) {
288 err = got_error_from_errno("asprintf");
293 head_ref_name = strdup(got_ref_get_name(head_ref));
294 if (head_ref_name == NULL) {
295 err = got_error_from_errno("strdup");
299 err = write_head_ref(path_got, head_ref);
303 free(worktree->head_ref_name);
304 worktree->head_ref_name = head_ref_name;
312 struct got_object_id *
313 got_worktree_get_base_commit_id(struct got_worktree *worktree)
315 return worktree->base_commit_id;
318 const struct got_error *
319 got_worktree_set_base_commit_id(struct got_worktree *worktree,
320 struct got_repository *repo, struct got_object_id *commit_id)
322 const struct got_error *err;
323 struct got_object *obj = NULL;
325 char *path_got = NULL;
327 if (asprintf(&path_got, "%s/%s", worktree->root_path,
328 GOT_WORKTREE_GOT_DIR) == -1) {
329 err = got_error_from_errno("asprintf");
334 err = got_object_open(&obj, repo, commit_id);
338 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
339 err = got_error(GOT_ERR_OBJ_TYPE);
343 /* Record our base commit. */
344 err = got_object_id_str(&id_str, commit_id);
347 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
351 free(worktree->base_commit_id);
352 worktree->base_commit_id = got_object_id_dup(commit_id);
353 if (worktree->base_commit_id == NULL) {
354 err = got_error_from_errno("got_object_id_dup");
359 got_object_close(obj);
365 const struct got_gotconfig *
366 got_worktree_get_gotconfig(struct got_worktree *worktree)
368 return worktree->gotconfig;
371 static const struct got_error *
372 lock_worktree(struct got_worktree *worktree, int operation)
374 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
375 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
376 : got_error_from_errno2("flock",
377 got_worktree_get_root_path(worktree)));
381 static const struct got_error *
382 add_dir_on_disk(struct got_worktree *worktree, const char *path)
384 const struct got_error *err = NULL;
387 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
388 return got_error_from_errno("asprintf");
390 err = got_path_mkdir(abspath);
391 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
394 if (lstat(abspath, &sb) == -1) {
395 err = got_error_from_errno2("lstat", abspath);
396 } else if (!S_ISDIR(sb.st_mode)) {
397 /* TODO directory is obstructed; do something */
398 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
405 static const struct got_error *
406 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
408 const struct got_error *err = NULL;
411 size_t flen1 = 0, flen2 = 0;
416 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
417 if (flen1 == 0 && ferror(f1)) {
418 err = got_error_from_errno("fread");
421 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
422 if (flen2 == 0 && ferror(f2)) {
423 err = got_error_from_errno("fread");
430 } else if (flen2 == 0) {
434 } else if (flen1 == flen2) {
435 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
448 static const struct got_error *
449 check_files_equal(int *same, FILE *f1, FILE *f2)
456 if (fstat(fileno(f1), &sb) != 0)
457 return got_error_from_errno("fstat");
460 if (fstat(fileno(f2), &sb) != 0)
461 return got_error_from_errno("fstat");
464 if (size1 != size2) {
469 if (fseek(f1, 0L, SEEK_SET) == -1)
470 return got_ferror(f1, GOT_ERR_IO);
471 if (fseek(f2, 0L, SEEK_SET) == -1)
472 return got_ferror(f2, GOT_ERR_IO);
474 return check_file_contents_equal(same, f1, f2);
477 static const struct got_error *
478 copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
486 if (fseek(f, 0L, SEEK_SET) == -1)
487 return got_ferror(f, GOT_ERR_IO);
490 flen = fread(fbuf, 1, sizeof(fbuf), f);
493 return got_error_from_errno("fread");
497 outlen = write(outfd, fbuf, flen);
499 return got_error_from_errno("write");
501 return got_error(GOT_ERR_IO);
508 static const struct got_error *
509 merge_binary_file(int *overlapcnt, int merged_fd,
510 FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
511 const char *label_deriv, const char *label_orig, const char *label_deriv2,
512 const char *ondisk_path)
514 const struct got_error *err = NULL;
515 int same_content, changed_deriv, changed_deriv2;
516 int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
517 off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
518 char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
519 char *base_path_orig = NULL, *base_path_deriv = NULL;
520 char *base_path_deriv2 = NULL;
524 err = check_files_equal(&same_content, f_deriv, f_deriv2);
529 return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
531 err = check_files_equal(&same_content, f_deriv, f_orig);
534 changed_deriv = !same_content;
535 err = check_files_equal(&same_content, f_deriv2, f_orig);
538 changed_deriv2 = !same_content;
540 if (changed_deriv && changed_deriv2) {
542 if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
543 err = got_error_from_errno("asprintf");
546 if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
547 err = got_error_from_errno("asprintf");
550 if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
551 err = got_error_from_errno("asprintf");
554 err = got_opentemp_named_fd(&path_orig, &fd_orig,
558 err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
562 err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
566 err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
569 err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
572 err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
575 if (dprintf(merged_fd, "Binary files differ and cannot be "
576 "merged automatically:\n") < 0) {
577 err = got_error_from_errno("dprintf");
580 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
581 GOT_DIFF_CONFLICT_MARKER_BEGIN,
582 label_deriv ? " " : "",
583 label_deriv ? label_deriv : "",
585 err = got_error_from_errno("dprintf");
589 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
590 GOT_DIFF_CONFLICT_MARKER_ORIG,
591 label_orig ? " " : "",
592 label_orig ? label_orig : "",
594 err = got_error_from_errno("dprintf");
598 if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
599 GOT_DIFF_CONFLICT_MARKER_SEP,
601 GOT_DIFF_CONFLICT_MARKER_END,
602 label_deriv2 ? " " : "",
603 label_deriv2 ? label_deriv2 : "") < 0) {
604 err = got_error_from_errno("dprintf");
607 } else if (changed_deriv)
608 err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
609 else if (changed_deriv2)
610 err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
612 if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
614 err = got_error_from_errno2("unlink", path_orig);
615 if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
616 err = got_error_from_errno2("close", path_orig);
617 if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
618 err = got_error_from_errno2("close", path_deriv);
619 if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
620 err = got_error_from_errno2("close", path_deriv2);
624 free(base_path_orig);
625 free(base_path_deriv);
626 free(base_path_deriv2);
631 * Perform a 3-way merge where the file f_orig acts as the common
632 * ancestor, the file f_deriv acts as the first derived version,
633 * and the file f_deriv2 acts as the second derived version.
634 * The merge result will be written to a new file at ondisk_path; any
635 * existing file at this path will be replaced.
637 static const struct got_error *
638 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
639 FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
640 const char *path, uint16_t st_mode,
641 const char *label_orig, const char *label_deriv, const char *label_deriv2,
642 enum got_diff_algorithm diff_algo, struct got_repository *repo,
643 got_worktree_checkout_cb progress_cb, void *progress_arg)
645 const struct got_error *err = NULL;
647 FILE *f_merged = NULL;
648 char *merged_path = NULL, *base_path = NULL;
652 *local_changes_subsumed = 0;
654 err = got_path_dirname(&parent, ondisk_path);
658 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
659 err = got_error_from_errno("asprintf");
663 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
667 err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
668 f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
670 if (err->code != GOT_ERR_FILE_BINARY)
672 err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
673 f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
679 err = (*progress_cb)(progress_arg,
680 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
684 if (fsync(merged_fd) != 0) {
685 err = got_error_from_errno("fsync");
689 f_merged = fdopen(merged_fd, "r");
690 if (f_merged == NULL) {
691 err = got_error_from_errno("fdopen");
696 /* Check if a clean merge has subsumed all local changes. */
697 if (overlapcnt == 0) {
698 err = check_files_equal(local_changes_subsumed, f_deriv,
704 if (fchmod(fileno(f_merged), st_mode) != 0) {
705 err = got_error_from_errno2("fchmod", merged_path);
709 if (rename(merged_path, ondisk_path) != 0) {
710 err = got_error_from_errno3("rename", merged_path,
719 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
720 err = got_error_from_errno("close");
721 if (f_merged && fclose(f_merged) == EOF && err == NULL)
722 err = got_error_from_errno("fclose");
729 static const struct got_error *
730 update_symlink(const char *ondisk_path, const char *target_path,
733 /* This is not atomic but matches what 'ln -sf' does. */
734 if (unlink(ondisk_path) == -1)
735 return got_error_from_errno2("unlink", ondisk_path);
736 if (symlink(target_path, ondisk_path) == -1)
737 return got_error_from_errno3("symlink", target_path,
743 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
744 * in the work tree with a file that contains conflict markers and the
745 * conflicting target paths of the original version, a "derived version"
746 * of a symlink from an incoming change, and a local version of the symlink.
748 * The original versions's target path can be NULL if it is not available,
749 * such as if both derived versions added a new symlink at the same path.
751 * The incoming derived symlink target is NULL in case the incoming change
752 * has deleted this symlink.
754 static const struct got_error *
755 install_symlink_conflict(const char *deriv_target,
756 struct got_object_id *deriv_base_commit_id, const char *orig_target,
757 const char *label_orig, const char *local_target, const char *ondisk_path)
759 const struct got_error *err;
760 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
763 err = got_object_id_str(&id_str, deriv_base_commit_id);
765 return got_error_from_errno("asprintf");
767 if (asprintf(&label_deriv, "%s: commit %s",
768 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
769 err = got_error_from_errno("asprintf");
773 err = got_opentemp_named(&path, &f, "got-symlink-conflict");
777 if (fchmod(fileno(f), GOT_DEFAULT_FILE_MODE) == -1) {
778 err = got_error_from_errno2("fchmod", path);
782 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
783 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
784 deriv_target ? deriv_target : "(symlink was deleted)",
785 orig_target ? label_orig : "",
786 orig_target ? "\n" : "",
787 orig_target ? orig_target : "",
788 orig_target ? "\n" : "",
789 GOT_DIFF_CONFLICT_MARKER_SEP,
790 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
791 err = got_error_from_errno2("fprintf", path);
795 if (unlink(ondisk_path) == -1) {
796 err = got_error_from_errno2("unlink", ondisk_path);
799 if (rename(path, ondisk_path) == -1) {
800 err = got_error_from_errno3("rename", path, ondisk_path);
804 if (f != NULL && fclose(f) == EOF && err == NULL)
805 err = got_error_from_errno2("fclose", path);
812 /* forward declaration */
813 static const struct got_error *
814 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
815 const char *, const char *, uint16_t, const char *,
816 struct got_blob_object *, struct got_object_id *,
817 struct got_repository *, got_worktree_checkout_cb, void *);
820 * Merge a symlink into the work tree, where blob_orig acts as the common
821 * ancestor, deriv_target is the link target of the first derived version,
822 * and the symlink on disk acts as the second derived version.
823 * Assume that contents of both blobs represent symlinks.
825 static const struct got_error *
826 merge_symlink(struct got_worktree *worktree,
827 struct got_blob_object *blob_orig, const char *ondisk_path,
828 const char *path, const char *label_orig, const char *deriv_target,
829 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
830 got_worktree_checkout_cb progress_cb, void *progress_arg)
832 const struct got_error *err = NULL;
833 char *ancestor_target = NULL;
835 ssize_t ondisk_len, deriv_len;
836 char ondisk_target[PATH_MAX];
837 int have_local_change = 0;
838 int have_incoming_change = 0;
840 if (lstat(ondisk_path, &sb) == -1)
841 return got_error_from_errno2("lstat", ondisk_path);
843 ondisk_len = readlink(ondisk_path, ondisk_target,
844 sizeof(ondisk_target));
845 if (ondisk_len == -1) {
846 err = got_error_from_errno2("readlink",
850 ondisk_target[ondisk_len] = '\0';
853 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
858 if (ancestor_target == NULL ||
859 (ondisk_len != strlen(ancestor_target) ||
860 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
861 have_local_change = 1;
863 deriv_len = strlen(deriv_target);
864 if (ancestor_target == NULL ||
865 (deriv_len != strlen(ancestor_target) ||
866 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
867 have_incoming_change = 1;
869 if (!have_local_change && !have_incoming_change) {
870 if (ancestor_target) {
871 /* Both sides made the same change. */
872 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
874 } else if (deriv_len == ondisk_len &&
875 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
876 /* Both sides added the same symlink. */
877 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
880 /* Both sides added symlinks which don't match. */
881 err = install_symlink_conflict(deriv_target,
882 deriv_base_commit_id, ancestor_target,
883 label_orig, ondisk_target, ondisk_path);
886 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
889 } else if (!have_local_change && have_incoming_change) {
890 /* Apply the incoming change. */
891 err = update_symlink(ondisk_path, deriv_target,
892 strlen(deriv_target));
895 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
896 } else if (have_local_change && have_incoming_change) {
897 if (deriv_len == ondisk_len &&
898 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
899 /* Both sides made the same change. */
900 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
903 err = install_symlink_conflict(deriv_target,
904 deriv_base_commit_id, ancestor_target, label_orig,
905 ondisk_target, ondisk_path);
908 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
914 free(ancestor_target);
918 static const struct got_error *
919 dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
921 const struct got_error *err = NULL;
922 char target_path[PATH_MAX];
931 return got_error_from_errno("got_opentemp");
932 target_len = readlink(ondisk_path, target_path, sizeof(target_path));
933 if (target_len == -1) {
934 err = got_error_from_errno2("readlink", ondisk_path);
937 n = fwrite(target_path, 1, target_len, f);
938 if (n != target_len) {
939 err = got_ferror(f, GOT_ERR_IO);
942 if (fflush(f) == EOF) {
943 err = got_error_from_errno("fflush");
946 if (fseek(f, 0L, SEEK_SET) == -1) {
947 err = got_ferror(f, GOT_ERR_IO);
959 * Perform a 3-way merge where blob_orig acts as the common ancestor,
960 * blob_deriv acts as the first derived version, and the file on disk
961 * acts as the second derived version.
963 static const struct got_error *
964 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
965 struct got_blob_object *blob_orig, const char *ondisk_path,
966 const char *path, uint16_t st_mode, const char *label_orig,
967 struct got_blob_object *blob_deriv,
968 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
969 got_worktree_checkout_cb progress_cb, void *progress_arg)
971 const struct got_error *err = NULL;
972 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
973 char *blob_orig_path = NULL;
974 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
975 char *label_deriv = NULL, *parent = NULL;
977 *local_changes_subsumed = 0;
979 err = got_path_dirname(&parent, ondisk_path);
984 if (asprintf(&base_path, "%s/got-merge-blob-orig",
986 err = got_error_from_errno("asprintf");
991 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
994 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1001 * No common ancestor exists. This is an "add vs add" conflict
1002 * and we simply use an empty ancestor file to make both files
1003 * appear in the merged result in their entirety.
1005 f_orig = got_opentemp();
1006 if (f_orig == NULL) {
1007 err = got_error_from_errno("got_opentemp");
1012 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1013 err = got_error_from_errno("asprintf");
1018 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1021 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1026 err = got_object_id_str(&id_str, deriv_base_commit_id);
1029 if (asprintf(&label_deriv, "%s: commit %s",
1030 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1031 err = got_error_from_errno("asprintf");
1036 * In order the run a 3-way merge with a symlink we copy the symlink's
1037 * target path into a temporary file and use that file with diff3.
1039 if (S_ISLNK(st_mode)) {
1040 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1045 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1047 err = got_error_from_errno2("open", ondisk_path);
1050 f_deriv2 = fdopen(fd, "r");
1051 if (f_deriv2 == NULL) {
1052 err = got_error_from_errno2("fdopen", ondisk_path);
1058 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1059 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1060 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1062 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1063 err = got_error_from_errno("fclose");
1064 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1065 err = got_error_from_errno("fclose");
1066 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1067 err = got_error_from_errno("fclose");
1069 if (blob_orig_path) {
1070 unlink(blob_orig_path);
1071 free(blob_orig_path);
1073 if (blob_deriv_path) {
1074 unlink(blob_deriv_path);
1075 free(blob_deriv_path);
1083 static const struct got_error *
1084 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1085 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1086 int wt_fd, const char *path, struct got_object_id *blob_id)
1088 const struct got_error *err = NULL;
1089 struct got_fileindex_entry *new_ie;
1093 err = got_fileindex_entry_alloc(&new_ie, path);
1097 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1098 blob_id->sha1, base_commit_id->sha1, 1);
1102 err = got_fileindex_entry_add(fileindex, new_ie);
1105 got_fileindex_entry_free(new_ie);
1112 get_ondisk_perms(int executable, mode_t st_mode)
1114 mode_t xbits = S_IXUSR;
1117 /* Map read bits to execute bits. */
1118 if (st_mode & S_IRGRP)
1120 if (st_mode & S_IROTH)
1122 return st_mode | xbits;
1125 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1128 /* forward declaration */
1129 static const struct got_error *
1130 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1131 const char *path, mode_t te_mode, mode_t st_mode,
1132 struct got_blob_object *blob, int restoring_missing_file,
1133 int reverting_versioned_file, int installing_bad_symlink,
1134 int path_is_unversioned, struct got_repository *repo,
1135 got_worktree_checkout_cb progress_cb, void *progress_arg);
1138 * This function assumes that the provided symlink target points at a
1139 * safe location in the work tree!
1141 static const struct got_error *
1142 replace_existing_symlink(int *did_something, const char *ondisk_path,
1143 const char *target_path, size_t target_len)
1145 const struct got_error *err = NULL;
1147 char etarget[PATH_MAX];
1153 * "Bad" symlinks (those pointing outside the work tree or into the
1154 * .got directory) are installed in the work tree as a regular file
1155 * which contains the bad symlink target path.
1156 * The new symlink target has already been checked for safety by our
1157 * caller. If we can successfully open a regular file then we simply
1158 * replace this file with a symlink below.
1160 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1162 if (!got_err_open_nofollow_on_symlink())
1163 return got_error_from_errno2("open", ondisk_path);
1165 /* We are updating an existing on-disk symlink. */
1166 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1168 return got_error_from_errno2("readlink", ondisk_path);
1170 if (elen == target_len &&
1171 memcmp(etarget, target_path, target_len) == 0)
1172 return NULL; /* nothing to do */
1176 err = update_symlink(ondisk_path, target_path, target_len);
1177 if (fd != -1 && close(fd) == -1 && err == NULL)
1178 err = got_error_from_errno2("close", ondisk_path);
1182 static const struct got_error *
1183 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1184 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1186 const struct got_error *err = NULL;
1187 char canonpath[PATH_MAX];
1188 char *path_got = NULL;
1190 *is_bad_symlink = 0;
1192 if (target_len >= sizeof(canonpath)) {
1193 *is_bad_symlink = 1;
1198 * We do not use realpath(3) to resolve the symlink's target
1199 * path because we don't want to resolve symlinks recursively.
1200 * Instead we make the path absolute and then canonicalize it.
1201 * Relative symlink target lookup should begin at the directory
1202 * in which the blob object is being installed.
1204 if (!got_path_is_absolute(target_path)) {
1205 char *abspath, *parent;
1206 err = got_path_dirname(&parent, ondisk_path);
1209 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1211 return got_error_from_errno("asprintf");
1214 if (strlen(abspath) >= sizeof(canonpath)) {
1215 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1219 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1224 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1229 /* Only allow symlinks pointing at paths within the work tree. */
1230 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1231 *is_bad_symlink = 1;
1235 /* Do not allow symlinks pointing into the .got directory. */
1236 if (asprintf(&path_got, "%s/%s", wtroot_path,
1237 GOT_WORKTREE_GOT_DIR) == -1)
1238 return got_error_from_errno("asprintf");
1239 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1240 *is_bad_symlink = 1;
1246 static const struct got_error *
1247 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1248 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1249 int restoring_missing_file, int reverting_versioned_file,
1250 int path_is_unversioned, int allow_bad_symlinks,
1251 struct got_repository *repo,
1252 got_worktree_checkout_cb progress_cb, void *progress_arg)
1254 const struct got_error *err = NULL;
1255 char target_path[PATH_MAX];
1256 size_t len, target_len = 0;
1257 char *path_got = NULL;
1258 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1259 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1261 *is_bad_symlink = 0;
1264 * Blob object content specifies the target path of the link.
1265 * If a symbolic link cannot be installed we instead create
1266 * a regular file which contains the link target path stored
1267 * in the blob object.
1270 err = got_object_blob_read_block(&len, blob);
1271 if (len + target_len >= sizeof(target_path)) {
1272 /* Path too long; install as a regular file. */
1273 *is_bad_symlink = 1;
1274 got_object_blob_rewind(blob);
1275 return install_blob(worktree, ondisk_path, path,
1276 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1277 restoring_missing_file, reverting_versioned_file,
1278 1, path_is_unversioned, repo, progress_cb,
1282 /* Skip blob object header first time around. */
1283 memcpy(target_path + target_len, buf + hdrlen,
1285 target_len += len - hdrlen;
1289 target_path[target_len] = '\0';
1291 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1292 ondisk_path, worktree->root_path);
1296 if (*is_bad_symlink && !allow_bad_symlinks) {
1297 /* install as a regular file */
1298 got_object_blob_rewind(blob);
1299 err = install_blob(worktree, ondisk_path, path,
1300 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1301 restoring_missing_file, reverting_versioned_file, 1,
1302 path_is_unversioned, repo, progress_cb, progress_arg);
1306 if (symlink(target_path, ondisk_path) == -1) {
1307 if (errno == EEXIST) {
1308 int symlink_replaced;
1309 if (path_is_unversioned) {
1310 err = (*progress_cb)(progress_arg,
1311 GOT_STATUS_UNVERSIONED, path);
1314 err = replace_existing_symlink(&symlink_replaced,
1315 ondisk_path, target_path, target_len);
1319 if (symlink_replaced) {
1320 err = (*progress_cb)(progress_arg,
1321 reverting_versioned_file ?
1323 GOT_STATUS_UPDATE, path);
1325 err = (*progress_cb)(progress_arg,
1326 GOT_STATUS_EXISTS, path);
1329 goto done; /* Nothing else to do. */
1332 if (errno == ENOENT) {
1334 err = got_path_dirname(&parent, ondisk_path);
1337 err = add_dir_on_disk(worktree, parent);
1342 * Retry, and fall through to error handling
1343 * below if this second attempt fails.
1345 if (symlink(target_path, ondisk_path) != -1) {
1346 err = NULL; /* success */
1351 /* Handle errors from first or second creation attempt. */
1352 if (errno == ENAMETOOLONG) {
1353 /* bad target path; install as a regular file */
1354 *is_bad_symlink = 1;
1355 got_object_blob_rewind(blob);
1356 err = install_blob(worktree, ondisk_path, path,
1357 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1358 restoring_missing_file, reverting_versioned_file, 1,
1359 path_is_unversioned, repo,
1360 progress_cb, progress_arg);
1361 } else if (errno == ENOTDIR) {
1362 err = got_error_path(ondisk_path,
1363 GOT_ERR_FILE_OBSTRUCTED);
1365 err = got_error_from_errno3("symlink",
1366 target_path, ondisk_path);
1368 } else if (progress_cb)
1369 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1370 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1376 static const struct got_error *
1377 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1378 const char *path, mode_t te_mode, mode_t st_mode,
1379 struct got_blob_object *blob, int restoring_missing_file,
1380 int reverting_versioned_file, int installing_bad_symlink,
1381 int path_is_unversioned, struct got_repository *repo,
1382 got_worktree_checkout_cb progress_cb, void *progress_arg)
1384 const struct got_error *err = NULL;
1388 char *tmppath = NULL;
1390 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1391 O_CLOEXEC, GOT_DEFAULT_FILE_MODE);
1393 if (errno == ENOENT) {
1395 err = got_path_dirname(&parent, path);
1398 err = add_dir_on_disk(worktree, parent);
1402 fd = open(ondisk_path,
1403 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1404 GOT_DEFAULT_FILE_MODE);
1406 return got_error_from_errno2("open",
1408 } else if (errno == EEXIST) {
1409 if (path_is_unversioned) {
1410 err = (*progress_cb)(progress_arg,
1411 GOT_STATUS_UNVERSIONED, path);
1414 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1415 !S_ISREG(st_mode) && !installing_bad_symlink) {
1416 /* TODO file is obstructed; do something */
1417 err = got_error_path(ondisk_path,
1418 GOT_ERR_FILE_OBSTRUCTED);
1421 err = got_opentemp_named_fd(&tmppath, &fd,
1428 return got_error_from_errno2("open", ondisk_path);
1431 if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1432 err = got_error_from_errno2("fchmod",
1433 update ? tmppath : ondisk_path);
1438 if (restoring_missing_file)
1439 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1441 else if (reverting_versioned_file)
1442 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1445 err = (*progress_cb)(progress_arg,
1446 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1451 hdrlen = got_object_blob_get_hdrlen(blob);
1453 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1454 err = got_object_blob_read_block(&len, blob);
1458 /* Skip blob object header first time around. */
1459 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1461 err = got_error_from_errno("write");
1463 } else if (outlen != len - hdrlen) {
1464 err = got_error(GOT_ERR_IO);
1471 if (fsync(fd) != 0) {
1472 err = got_error_from_errno("fsync");
1477 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1478 err = got_error_from_errno2("unlink", ondisk_path);
1481 if (rename(tmppath, ondisk_path) != 0) {
1482 err = got_error_from_errno3("rename", tmppath,
1491 if (fd != -1 && close(fd) == -1 && err == NULL)
1492 err = got_error_from_errno("close");
1493 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1494 err = got_error_from_errno2("unlink", tmppath);
1499 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1500 static const struct got_error *
1501 get_modified_file_content_status(unsigned char *status, FILE *f)
1503 const struct got_error *err = NULL;
1504 const char *markers[3] = {
1505 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1506 GOT_DIFF_CONFLICT_MARKER_SEP,
1507 GOT_DIFF_CONFLICT_MARKER_END
1511 size_t linesize = 0;
1514 while (*status == GOT_STATUS_MODIFY) {
1515 linelen = getline(&line, &linesize, f);
1516 if (linelen == -1) {
1519 err = got_ferror(f, GOT_ERR_IO);
1523 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1524 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1526 *status = GOT_STATUS_CONFLICT;
1537 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1539 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1540 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1544 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1546 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1547 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1548 ie->mtime_sec == sb->st_mtim.tv_sec &&
1549 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1550 ie->size == (sb->st_size & 0xffffffff) &&
1551 !xbit_differs(ie, sb->st_mode));
1554 static unsigned char
1555 get_staged_status(struct got_fileindex_entry *ie)
1557 switch (got_fileindex_entry_stage_get(ie)) {
1558 case GOT_FILEIDX_STAGE_ADD:
1559 return GOT_STATUS_ADD;
1560 case GOT_FILEIDX_STAGE_DELETE:
1561 return GOT_STATUS_DELETE;
1562 case GOT_FILEIDX_STAGE_MODIFY:
1563 return GOT_STATUS_MODIFY;
1565 return GOT_STATUS_NO_CHANGE;
1569 static const struct got_error *
1570 get_symlink_modification_status(unsigned char *status,
1571 struct got_fileindex_entry *ie, const char *abspath,
1572 int dirfd, const char *de_name, struct got_blob_object *blob)
1574 const struct got_error *err = NULL;
1575 char target_path[PATH_MAX];
1576 char etarget[PATH_MAX];
1578 size_t len, target_len = 0;
1579 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1580 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1582 *status = GOT_STATUS_NO_CHANGE;
1584 /* Blob object content specifies the target path of the link. */
1586 err = got_object_blob_read_block(&len, blob);
1589 if (len + target_len >= sizeof(target_path)) {
1591 * Should not happen. The blob contents were OK
1592 * when this symlink was installed.
1594 return got_error(GOT_ERR_NO_SPACE);
1597 /* Skip blob object header first time around. */
1598 memcpy(target_path + target_len, buf + hdrlen,
1600 target_len += len - hdrlen;
1604 target_path[target_len] = '\0';
1607 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1609 return got_error_from_errno2("readlinkat", abspath);
1611 elen = readlink(abspath, etarget, sizeof(etarget));
1613 return got_error_from_errno2("readlink", abspath);
1616 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1617 *status = GOT_STATUS_MODIFY;
1622 static const struct got_error *
1623 get_file_status(unsigned char *status, struct stat *sb,
1624 struct got_fileindex_entry *ie, const char *abspath,
1625 int dirfd, const char *de_name, struct got_repository *repo)
1627 const struct got_error *err = NULL;
1628 struct got_object_id id;
1633 struct got_blob_object *blob = NULL;
1635 unsigned char staged_status = get_staged_status(ie);
1637 *status = GOT_STATUS_NO_CHANGE;
1638 memset(sb, 0, sizeof(*sb));
1641 * Whenever the caller provides a directory descriptor and a
1642 * directory entry name for the file, use them! This prevents
1643 * race conditions if filesystem paths change beneath our feet.
1646 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1647 if (errno == ENOENT) {
1648 if (got_fileindex_entry_has_file_on_disk(ie))
1649 *status = GOT_STATUS_MISSING;
1651 *status = GOT_STATUS_DELETE;
1654 err = got_error_from_errno2("fstatat", abspath);
1658 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1659 if (fd == -1 && errno != ENOENT &&
1660 !got_err_open_nofollow_on_symlink())
1661 return got_error_from_errno2("open", abspath);
1662 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1663 if (lstat(abspath, sb) == -1)
1664 return got_error_from_errno2("lstat", abspath);
1665 } else if (fd == -1 || fstat(fd, sb) == -1) {
1666 if (errno == ENOENT) {
1667 if (got_fileindex_entry_has_file_on_disk(ie))
1668 *status = GOT_STATUS_MISSING;
1670 *status = GOT_STATUS_DELETE;
1673 err = got_error_from_errno2("fstat", abspath);
1678 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1679 *status = GOT_STATUS_OBSTRUCTED;
1683 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1684 *status = GOT_STATUS_DELETE;
1686 } else if (!got_fileindex_entry_has_blob(ie) &&
1687 staged_status != GOT_STATUS_ADD) {
1688 *status = GOT_STATUS_ADD;
1692 if (!stat_info_differs(ie, sb))
1695 if (S_ISLNK(sb->st_mode) &&
1696 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1697 *status = GOT_STATUS_MODIFY;
1701 if (staged_status == GOT_STATUS_MODIFY ||
1702 staged_status == GOT_STATUS_ADD)
1703 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1705 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1707 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1711 if (S_ISLNK(sb->st_mode)) {
1712 err = get_symlink_modification_status(status, ie,
1713 abspath, dirfd, de_name, blob);
1718 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1720 err = got_error_from_errno2("openat", abspath);
1725 f = fdopen(fd, "r");
1727 err = got_error_from_errno2("fdopen", abspath);
1731 hdrlen = got_object_blob_get_hdrlen(blob);
1733 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1734 err = got_object_blob_read_block(&blen, blob);
1737 /* Skip length of blob object header first time around. */
1738 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1739 if (flen == 0 && ferror(f)) {
1740 err = got_error_from_errno("fread");
1743 if (blen - hdrlen == 0) {
1745 *status = GOT_STATUS_MODIFY;
1747 } else if (flen == 0) {
1748 if (blen - hdrlen != 0)
1749 *status = GOT_STATUS_MODIFY;
1751 } else if (blen - hdrlen == flen) {
1752 /* Skip blob object header first time around. */
1753 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1754 *status = GOT_STATUS_MODIFY;
1758 *status = GOT_STATUS_MODIFY;
1764 if (*status == GOT_STATUS_MODIFY) {
1766 err = get_modified_file_content_status(status, f);
1767 } else if (xbit_differs(ie, sb->st_mode))
1768 *status = GOT_STATUS_MODE_CHANGE;
1771 got_object_blob_close(blob);
1772 if (f != NULL && fclose(f) == EOF && err == NULL)
1773 err = got_error_from_errno2("fclose", abspath);
1774 if (fd != -1 && close(fd) == -1 && err == NULL)
1775 err = got_error_from_errno2("close", abspath);
1780 * Update timestamps in the file index if a file is unmodified and
1781 * we had to run a full content comparison to find out.
1783 static const struct got_error *
1784 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1785 struct got_fileindex_entry *ie, struct stat *sb)
1787 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1788 return got_fileindex_entry_update(ie, wt_fd, path,
1789 ie->blob_sha1, ie->commit_sha1, 1);
1794 static const struct got_error *
1795 update_blob(struct got_worktree *worktree,
1796 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1797 struct got_tree_entry *te, const char *path,
1798 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1801 const struct got_error *err = NULL;
1802 struct got_blob_object *blob = NULL;
1804 unsigned char status = GOT_STATUS_NO_CHANGE;
1807 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1808 return got_error_from_errno("asprintf");
1811 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1812 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1815 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1819 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1820 sb.st_mode = got_fileindex_perms_to_st(ie);
1822 if (stat(ondisk_path, &sb) == -1) {
1823 if (errno != ENOENT) {
1824 err = got_error_from_errno2("stat",
1828 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1829 status = GOT_STATUS_UNVERSIONED;
1831 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1832 status = GOT_STATUS_UNVERSIONED;
1834 status = GOT_STATUS_OBSTRUCTED;
1838 if (status == GOT_STATUS_OBSTRUCTED) {
1840 got_fileindex_entry_mark_skipped(ie);
1841 err = (*progress_cb)(progress_arg, status, path);
1844 if (status == GOT_STATUS_CONFLICT) {
1846 got_fileindex_entry_mark_skipped(ie);
1847 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1852 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1853 (S_ISLNK(te->mode) ||
1854 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1856 * This is a regular file or an installed bad symlink.
1857 * If the file index indicates that this file is already
1858 * up-to-date with respect to the repository we can skip
1859 * updating contents of this file.
1861 if (got_fileindex_entry_has_commit(ie) &&
1862 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1863 SHA1_DIGEST_LENGTH) == 0) {
1865 err = sync_timestamps(worktree->root_fd,
1866 path, status, ie, &sb);
1869 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1873 if (got_fileindex_entry_has_blob(ie) &&
1874 memcmp(ie->blob_sha1, te->id.sha1,
1875 SHA1_DIGEST_LENGTH) == 0) {
1876 /* Different commit but the same blob. */
1877 err = sync_timestamps(worktree->root_fd,
1878 path, status, ie, &sb);
1881 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1887 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1891 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1892 int update_timestamps;
1893 struct got_blob_object *blob2 = NULL;
1894 char *label_orig = NULL;
1895 if (got_fileindex_entry_has_blob(ie)) {
1896 struct got_object_id id2;
1897 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1898 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1902 if (got_fileindex_entry_has_commit(ie)) {
1903 char id_str[SHA1_DIGEST_STRING_LENGTH];
1904 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1905 sizeof(id_str)) == NULL) {
1906 err = got_error_path(id_str,
1907 GOT_ERR_BAD_OBJ_ID_STR);
1910 if (asprintf(&label_orig, "%s: commit %s",
1911 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1912 err = got_error_from_errno("asprintf");
1916 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1918 err = got_object_blob_read_to_str(&link_target, blob);
1921 err = merge_symlink(worktree, blob2, ondisk_path, path,
1922 label_orig, link_target, worktree->base_commit_id,
1923 repo, progress_cb, progress_arg);
1926 err = merge_blob(&update_timestamps, worktree, blob2,
1927 ondisk_path, path, sb.st_mode, label_orig, blob,
1928 worktree->base_commit_id, repo,
1929 progress_cb, progress_arg);
1933 got_object_blob_close(blob2);
1937 * Do not update timestamps of files with local changes.
1938 * Otherwise, a future status walk would treat them as
1939 * unmodified files again.
1941 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1942 blob->id.sha1, worktree->base_commit_id->sha1,
1944 } else if (status == GOT_STATUS_MODE_CHANGE) {
1945 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1946 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1947 } else if (status == GOT_STATUS_DELETE) {
1948 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1951 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1952 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1956 int is_bad_symlink = 0;
1957 if (S_ISLNK(te->mode)) {
1958 err = install_symlink(&is_bad_symlink, worktree,
1959 ondisk_path, path, blob,
1960 status == GOT_STATUS_MISSING, 0,
1961 status == GOT_STATUS_UNVERSIONED, 0,
1962 repo, progress_cb, progress_arg);
1964 err = install_blob(worktree, ondisk_path, path,
1965 te->mode, sb.st_mode, blob,
1966 status == GOT_STATUS_MISSING, 0, 0,
1967 status == GOT_STATUS_UNVERSIONED, repo,
1968 progress_cb, progress_arg);
1974 err = got_fileindex_entry_update(ie,
1975 worktree->root_fd, path, blob->id.sha1,
1976 worktree->base_commit_id->sha1, 1);
1978 err = create_fileindex_entry(&ie, fileindex,
1979 worktree->base_commit_id, worktree->root_fd, path,
1985 if (is_bad_symlink) {
1986 got_fileindex_entry_filetype_set(ie,
1987 GOT_FILEIDX_MODE_BAD_SYMLINK);
1990 got_object_blob_close(blob);
1996 static const struct got_error *
1997 remove_ondisk_file(const char *root_path, const char *path)
1999 const struct got_error *err = NULL;
2000 char *ondisk_path = NULL, *parent = NULL;
2002 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2003 return got_error_from_errno("asprintf");
2005 if (unlink(ondisk_path) == -1) {
2006 if (errno != ENOENT)
2007 err = got_error_from_errno2("unlink", ondisk_path);
2009 size_t root_len = strlen(root_path);
2010 err = got_path_dirname(&parent, ondisk_path);
2013 while (got_path_cmp(parent, root_path,
2014 strlen(parent), root_len) != 0) {
2016 ondisk_path = parent;
2018 if (rmdir(ondisk_path) == -1) {
2019 if (errno != ENOTEMPTY)
2020 err = got_error_from_errno2("rmdir",
2024 err = got_path_dirname(&parent, ondisk_path);
2035 static const struct got_error *
2036 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2037 struct got_fileindex_entry *ie, struct got_repository *repo,
2038 got_worktree_checkout_cb progress_cb, void *progress_arg)
2040 const struct got_error *err = NULL;
2041 unsigned char status;
2045 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2046 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2048 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2050 return got_error_from_errno("asprintf");
2052 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2056 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2057 char ondisk_target[PATH_MAX];
2058 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2059 sizeof(ondisk_target));
2060 if (ondisk_len == -1) {
2061 err = got_error_from_errno2("readlink", ondisk_path);
2064 ondisk_target[ondisk_len] = '\0';
2065 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2066 NULL, NULL, /* XXX pass common ancestor info? */
2067 ondisk_target, ondisk_path);
2070 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2075 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2076 status == GOT_STATUS_ADD) {
2077 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2081 * Preserve the working file and change the deleted blob's
2082 * entry into a schedule-add entry.
2084 err = got_fileindex_entry_update(ie, worktree->root_fd,
2085 ie->path, NULL, NULL, 0);
2087 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2090 if (status == GOT_STATUS_NO_CHANGE) {
2091 err = remove_ondisk_file(worktree->root_path, ie->path);
2095 got_fileindex_entry_remove(fileindex, ie);
2102 struct diff_cb_arg {
2103 struct got_fileindex *fileindex;
2104 struct got_worktree *worktree;
2105 struct got_repository *repo;
2106 got_worktree_checkout_cb progress_cb;
2108 got_cancel_cb cancel_cb;
2112 static const struct got_error *
2113 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2114 struct got_tree_entry *te, const char *parent_path)
2116 struct diff_cb_arg *a = arg;
2118 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2119 return got_error(GOT_ERR_CANCELLED);
2121 return update_blob(a->worktree, a->fileindex, ie, te,
2122 ie->path, a->repo, a->progress_cb, a->progress_arg);
2125 static const struct got_error *
2126 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2128 struct diff_cb_arg *a = arg;
2130 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2131 return got_error(GOT_ERR_CANCELLED);
2133 return delete_blob(a->worktree, a->fileindex, ie,
2134 a->repo, a->progress_cb, a->progress_arg);
2137 static const struct got_error *
2138 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2140 struct diff_cb_arg *a = arg;
2141 const struct got_error *err;
2144 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2145 return got_error(GOT_ERR_CANCELLED);
2147 if (got_object_tree_entry_is_submodule(te))
2150 if (asprintf(&path, "%s%s%s", parent_path,
2151 parent_path[0] ? "/" : "", te->name)
2153 return got_error_from_errno("asprintf");
2155 if (S_ISDIR(te->mode))
2156 err = add_dir_on_disk(a->worktree, path);
2158 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2159 a->repo, a->progress_cb, a->progress_arg);
2165 const struct got_error *
2166 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2168 uint32_t uuid_status;
2170 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2171 if (uuid_status != uuid_s_ok) {
2173 return got_error_uuid(uuid_status, "uuid_to_string");
2179 static const struct got_error *
2180 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2182 const struct got_error *err = NULL;
2183 char *uuidstr = NULL;
2187 err = got_worktree_get_uuid(&uuidstr, worktree);
2191 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2192 err = got_error_from_errno("asprintf");
2199 const struct got_error *
2200 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2202 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2205 static const struct got_error *
2206 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2208 return get_ref_name(refname, worktree,
2209 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2212 static const struct got_error *
2213 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2215 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2218 static const struct got_error *
2219 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2221 return get_ref_name(refname, worktree,
2222 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2225 static const struct got_error *
2226 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2228 return get_ref_name(refname, worktree,
2229 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2232 static const struct got_error *
2233 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2235 return get_ref_name(refname, worktree,
2236 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2239 static const struct got_error *
2240 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2242 return get_ref_name(refname, worktree,
2243 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2246 static const struct got_error *
2247 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2249 return get_ref_name(refname, worktree,
2250 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2253 static const struct got_error *
2254 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2256 return get_ref_name(refname, worktree,
2257 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2260 const struct got_error *
2261 got_worktree_get_histedit_script_path(char **path,
2262 struct got_worktree *worktree)
2264 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2265 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2267 return got_error_from_errno("asprintf");
2272 static const struct got_error *
2273 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2275 return get_ref_name(refname, worktree,
2276 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2279 static const struct got_error *
2280 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2282 return get_ref_name(refname, worktree,
2283 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2287 * Prevent Git's garbage collector from deleting our base commit by
2288 * setting a reference to our base commit's ID.
2290 static const struct got_error *
2291 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2293 const struct got_error *err = NULL;
2294 struct got_reference *ref = NULL;
2297 err = got_worktree_get_base_ref_name(&refname, worktree);
2301 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2305 err = got_ref_write(ref, repo);
2313 static const struct got_error *
2314 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2316 const struct got_error *err = NULL;
2318 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2319 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2320 err = got_error_from_errno("asprintf");
2321 *fileindex_path = NULL;
2327 static const struct got_error *
2328 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2329 struct got_worktree *worktree)
2331 const struct got_error *err = NULL;
2334 *fileindex_path = NULL;
2335 *fileindex = got_fileindex_alloc();
2336 if (*fileindex == NULL)
2337 return got_error_from_errno("got_fileindex_alloc");
2339 err = get_fileindex_path(fileindex_path, worktree);
2343 index = fopen(*fileindex_path, "rbe");
2344 if (index == NULL) {
2345 if (errno != ENOENT)
2346 err = got_error_from_errno2("fopen", *fileindex_path);
2348 err = got_fileindex_read(*fileindex, index);
2349 if (fclose(index) == EOF && err == NULL)
2350 err = got_error_from_errno("fclose");
2354 free(*fileindex_path);
2355 *fileindex_path = NULL;
2356 got_fileindex_free(*fileindex);
2362 struct bump_base_commit_id_arg {
2363 struct got_object_id *base_commit_id;
2366 const char *entry_name;
2367 got_worktree_checkout_cb progress_cb;
2371 /* Bump base commit ID of all files within an updated part of the work tree. */
2372 static const struct got_error *
2373 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2375 const struct got_error *err;
2376 struct bump_base_commit_id_arg *a = arg;
2378 if (a->entry_name) {
2379 if (strcmp(ie->path, a->path) != 0)
2381 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2384 if (got_fileindex_entry_was_skipped(ie))
2387 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2388 SHA1_DIGEST_LENGTH) == 0)
2391 if (a->progress_cb) {
2392 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2397 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2401 static const struct got_error *
2402 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2403 struct got_fileindex *fileindex,
2404 got_worktree_checkout_cb progress_cb, void *progress_arg)
2406 struct bump_base_commit_id_arg bbc_arg;
2408 bbc_arg.base_commit_id = worktree->base_commit_id;
2409 bbc_arg.entry_name = NULL;
2411 bbc_arg.path_len = 0;
2412 bbc_arg.progress_cb = progress_cb;
2413 bbc_arg.progress_arg = progress_arg;
2415 return got_fileindex_for_each_entry_safe(fileindex,
2416 bump_base_commit_id, &bbc_arg);
2419 static const struct got_error *
2420 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2422 const struct got_error *err = NULL;
2423 char *new_fileindex_path = NULL;
2424 FILE *new_index = NULL;
2425 struct timespec timeout;
2427 err = got_opentemp_named(&new_fileindex_path, &new_index,
2432 err = got_fileindex_write(fileindex, new_index);
2436 if (rename(new_fileindex_path, fileindex_path) != 0) {
2437 err = got_error_from_errno3("rename", new_fileindex_path,
2439 unlink(new_fileindex_path);
2443 * Sleep for a short amount of time to ensure that files modified after
2444 * this program exits have a different time stamp from the one which
2445 * was recorded in the file index.
2448 timeout.tv_nsec = 1;
2449 nanosleep(&timeout, NULL);
2453 free(new_fileindex_path);
2457 static const struct got_error *
2458 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2459 struct got_object_id **tree_id, const char *wt_relpath,
2460 struct got_worktree *worktree, struct got_repository *repo)
2462 const struct got_error *err = NULL;
2463 struct got_object_id *id = NULL;
2464 char *in_repo_path = NULL;
2465 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2467 *entry_type = GOT_OBJ_TYPE_ANY;
2468 *tree_relpath = NULL;
2471 if (wt_relpath[0] == '\0') {
2472 /* Check out all files within the work tree. */
2473 *entry_type = GOT_OBJ_TYPE_TREE;
2474 *tree_relpath = strdup("");
2475 if (*tree_relpath == NULL) {
2476 err = got_error_from_errno("strdup");
2479 err = got_object_id_by_path(tree_id, repo,
2480 worktree->base_commit_id, worktree->path_prefix);
2486 /* Check out a subset of files in the work tree. */
2488 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2489 is_root_wt ? "" : "/", wt_relpath) == -1) {
2490 err = got_error_from_errno("asprintf");
2494 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2500 in_repo_path = NULL;
2502 err = got_object_get_type(entry_type, repo, id);
2506 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2507 /* Check out a single file. */
2508 if (strchr(wt_relpath, '/') == NULL) {
2509 /* Check out a single file in work tree's root dir. */
2510 in_repo_path = strdup(worktree->path_prefix);
2511 if (in_repo_path == NULL) {
2512 err = got_error_from_errno("strdup");
2515 *tree_relpath = strdup("");
2516 if (*tree_relpath == NULL) {
2517 err = got_error_from_errno("strdup");
2521 /* Check out a single file in a subdirectory. */
2522 err = got_path_dirname(tree_relpath, wt_relpath);
2525 if (asprintf(&in_repo_path, "%s%s%s",
2526 worktree->path_prefix, is_root_wt ? "" : "/",
2527 *tree_relpath) == -1) {
2528 err = got_error_from_errno("asprintf");
2532 err = got_object_id_by_path(tree_id, repo,
2533 worktree->base_commit_id, in_repo_path);
2535 /* Check out all files within a subdirectory. */
2536 *tree_id = got_object_id_dup(id);
2537 if (*tree_id == NULL) {
2538 err = got_error_from_errno("got_object_id_dup");
2541 *tree_relpath = strdup(wt_relpath);
2542 if (*tree_relpath == NULL) {
2543 err = got_error_from_errno("strdup");
2551 *entry_type = GOT_OBJ_TYPE_ANY;
2552 free(*tree_relpath);
2553 *tree_relpath = NULL;
2560 static const struct got_error *
2561 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2562 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2563 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2564 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2566 const struct got_error *err = NULL;
2567 struct got_commit_object *commit = NULL;
2568 struct got_tree_object *tree = NULL;
2569 struct got_fileindex_diff_tree_cb diff_cb;
2570 struct diff_cb_arg arg;
2572 err = ref_base_commit(worktree, repo);
2574 if (!(err->code == GOT_ERR_ERRNO &&
2575 (errno == EACCES || errno == EROFS)))
2577 err = (*progress_cb)(progress_arg,
2578 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2583 err = got_object_open_as_commit(&commit, repo,
2584 worktree->base_commit_id);
2588 err = got_object_open_as_tree(&tree, repo, tree_id);
2593 got_object_tree_find_entry(tree, entry_name) == NULL) {
2594 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2598 diff_cb.diff_old_new = diff_old_new;
2599 diff_cb.diff_old = diff_old;
2600 diff_cb.diff_new = diff_new;
2601 arg.fileindex = fileindex;
2602 arg.worktree = worktree;
2604 arg.progress_cb = progress_cb;
2605 arg.progress_arg = progress_arg;
2606 arg.cancel_cb = cancel_cb;
2607 arg.cancel_arg = cancel_arg;
2608 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2609 entry_name, repo, &diff_cb, &arg);
2612 got_object_tree_close(tree);
2614 got_object_commit_close(commit);
2618 const struct got_error *
2619 got_worktree_checkout_files(struct got_worktree *worktree,
2620 struct got_pathlist_head *paths, struct got_repository *repo,
2621 got_worktree_checkout_cb progress_cb, void *progress_arg,
2622 got_cancel_cb cancel_cb, void *cancel_arg)
2624 const struct got_error *err = NULL, *sync_err, *unlockerr;
2625 struct got_commit_object *commit = NULL;
2626 struct got_tree_object *tree = NULL;
2627 struct got_fileindex *fileindex = NULL;
2628 char *fileindex_path = NULL;
2629 struct got_pathlist_entry *pe;
2630 struct tree_path_data {
2631 STAILQ_ENTRY(tree_path_data) entry;
2632 struct got_object_id *tree_id;
2637 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2639 STAILQ_INIT(&tree_paths);
2641 err = lock_worktree(worktree, LOCK_EX);
2645 /* Map all specified paths to in-repository trees. */
2646 TAILQ_FOREACH(pe, paths, entry) {
2647 tpd = malloc(sizeof(*tpd));
2649 err = got_error_from_errno("malloc");
2653 err = find_tree_entry_for_checkout(&tpd->entry_type,
2654 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2660 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2661 err = got_path_basename(&tpd->entry_name, pe->path);
2669 tpd->entry_name = NULL;
2671 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2675 * Read the file index.
2676 * Checking out files is supposed to be an idempotent operation.
2677 * If the on-disk file index is incomplete we will try to complete it.
2679 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2683 tpd = STAILQ_FIRST(&tree_paths);
2684 TAILQ_FOREACH(pe, paths, entry) {
2685 struct bump_base_commit_id_arg bbc_arg;
2687 err = checkout_files(worktree, fileindex, tpd->relpath,
2688 tpd->tree_id, tpd->entry_name, repo,
2689 progress_cb, progress_arg, cancel_cb, cancel_arg);
2693 bbc_arg.base_commit_id = worktree->base_commit_id;
2694 bbc_arg.entry_name = tpd->entry_name;
2695 bbc_arg.path = pe->path;
2696 bbc_arg.path_len = pe->path_len;
2697 bbc_arg.progress_cb = progress_cb;
2698 bbc_arg.progress_arg = progress_arg;
2699 err = got_fileindex_for_each_entry_safe(fileindex,
2700 bump_base_commit_id, &bbc_arg);
2704 tpd = STAILQ_NEXT(tpd, entry);
2706 sync_err = sync_fileindex(fileindex, fileindex_path);
2707 if (sync_err && err == NULL)
2710 free(fileindex_path);
2712 got_object_tree_close(tree);
2714 got_object_commit_close(commit);
2716 got_fileindex_free(fileindex);
2717 while (!STAILQ_EMPTY(&tree_paths)) {
2718 tpd = STAILQ_FIRST(&tree_paths);
2719 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2724 unlockerr = lock_worktree(worktree, LOCK_SH);
2725 if (unlockerr && err == NULL)
2730 struct merge_file_cb_arg {
2731 struct got_worktree *worktree;
2732 struct got_fileindex *fileindex;
2733 got_worktree_checkout_cb progress_cb;
2735 got_cancel_cb cancel_cb;
2737 const char *label_orig;
2738 struct got_object_id *commit_id2;
2739 int allow_bad_symlinks;
2742 static const struct got_error *
2743 merge_file_cb(void *arg, struct got_blob_object *blob1,
2744 struct got_blob_object *blob2, struct got_object_id *id1,
2745 struct got_object_id *id2, const char *path1, const char *path2,
2746 mode_t mode1, mode_t mode2, struct got_repository *repo)
2748 static const struct got_error *err = NULL;
2749 struct merge_file_cb_arg *a = arg;
2750 struct got_fileindex_entry *ie;
2751 char *ondisk_path = NULL;
2753 unsigned char status;
2754 int local_changes_subsumed;
2755 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2756 char *id_str = NULL, *label_deriv2 = NULL;
2758 if (blob1 && blob2) {
2759 ie = got_fileindex_entry_get(a->fileindex, path2,
2762 return (*a->progress_cb)(a->progress_arg,
2763 GOT_STATUS_MISSING, path2);
2765 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2767 return got_error_from_errno("asprintf");
2769 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2774 if (status == GOT_STATUS_DELETE) {
2775 err = (*a->progress_cb)(a->progress_arg,
2776 GOT_STATUS_MERGE, path2);
2779 if (status != GOT_STATUS_NO_CHANGE &&
2780 status != GOT_STATUS_MODIFY &&
2781 status != GOT_STATUS_CONFLICT &&
2782 status != GOT_STATUS_ADD) {
2783 err = (*a->progress_cb)(a->progress_arg, status, path2);
2787 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2789 err = got_object_blob_read_to_str(&link_target2, blob2);
2792 err = merge_symlink(a->worktree, blob1, ondisk_path,
2793 path2, a->label_orig, link_target2, a->commit_id2,
2794 repo, a->progress_cb, a->progress_arg);
2799 f_orig = got_opentemp();
2800 if (f_orig == NULL) {
2801 err = got_error_from_errno("got_opentemp");
2804 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2809 f_deriv2 = got_opentemp();
2810 if (f_deriv2 == NULL)
2812 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2817 fd = open(ondisk_path,
2818 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2820 err = got_error_from_errno2("open",
2824 f_deriv = fdopen(fd, "r");
2825 if (f_deriv == NULL) {
2826 err = got_error_from_errno2("fdopen",
2831 err = got_object_id_str(&id_str, a->commit_id2);
2834 if (asprintf(&label_deriv2, "%s: commit %s",
2835 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2836 err = got_error_from_errno("asprintf");
2839 err = merge_file(&local_changes_subsumed, a->worktree,
2840 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2841 sb.st_mode, a->label_orig, NULL, label_deriv2,
2842 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2843 a->progress_cb, a->progress_arg);
2846 ie = got_fileindex_entry_get(a->fileindex, path1,
2849 return (*a->progress_cb)(a->progress_arg,
2850 GOT_STATUS_MISSING, path1);
2852 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2854 return got_error_from_errno("asprintf");
2856 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2862 case GOT_STATUS_NO_CHANGE:
2863 err = (*a->progress_cb)(a->progress_arg,
2864 GOT_STATUS_DELETE, path1);
2867 err = remove_ondisk_file(a->worktree->root_path, path1);
2871 got_fileindex_entry_mark_deleted_from_disk(ie);
2873 case GOT_STATUS_DELETE:
2874 case GOT_STATUS_MISSING:
2875 err = (*a->progress_cb)(a->progress_arg,
2876 GOT_STATUS_DELETE, path1);
2880 got_fileindex_entry_mark_deleted_from_disk(ie);
2882 case GOT_STATUS_ADD: {
2883 struct got_object_id *id;
2886 * Delete the added file only if its content already
2887 * exists in the repository.
2889 err = got_object_blob_file_create(&id, &blob1_f, path1);
2892 if (got_object_id_cmp(id, id1) == 0) {
2893 err = (*a->progress_cb)(a->progress_arg,
2894 GOT_STATUS_DELETE, path1);
2897 err = remove_ondisk_file(a->worktree->root_path,
2902 got_fileindex_entry_remove(a->fileindex,
2905 err = (*a->progress_cb)(a->progress_arg,
2906 GOT_STATUS_CANNOT_DELETE, path1);
2908 if (fclose(blob1_f) == EOF && err == NULL)
2909 err = got_error_from_errno("fclose");
2915 case GOT_STATUS_MODIFY:
2916 case GOT_STATUS_CONFLICT:
2917 err = (*a->progress_cb)(a->progress_arg,
2918 GOT_STATUS_CANNOT_DELETE, path1);
2922 case GOT_STATUS_OBSTRUCTED:
2923 err = (*a->progress_cb)(a->progress_arg, status, path1);
2931 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2933 return got_error_from_errno("asprintf");
2934 ie = got_fileindex_entry_get(a->fileindex, path2,
2937 err = get_file_status(&status, &sb, ie, ondisk_path,
2941 if (status != GOT_STATUS_NO_CHANGE &&
2942 status != GOT_STATUS_MODIFY &&
2943 status != GOT_STATUS_CONFLICT &&
2944 status != GOT_STATUS_ADD) {
2945 err = (*a->progress_cb)(a->progress_arg,
2949 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2951 err = got_object_blob_read_to_str(&link_target2,
2955 err = merge_symlink(a->worktree, NULL,
2956 ondisk_path, path2, a->label_orig,
2957 link_target2, a->commit_id2, repo,
2958 a->progress_cb, a->progress_arg);
2960 } else if (S_ISREG(sb.st_mode)) {
2961 err = merge_blob(&local_changes_subsumed,
2962 a->worktree, NULL, ondisk_path, path2,
2963 sb.st_mode, a->label_orig, blob2,
2964 a->commit_id2, repo, a->progress_cb,
2967 err = got_error_path(ondisk_path,
2968 GOT_ERR_FILE_OBSTRUCTED);
2972 if (status == GOT_STATUS_DELETE) {
2973 err = got_fileindex_entry_update(ie,
2974 a->worktree->root_fd, path2, blob2->id.sha1,
2975 a->worktree->base_commit_id->sha1, 0);
2980 int is_bad_symlink = 0;
2981 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2982 if (S_ISLNK(mode2)) {
2983 err = install_symlink(&is_bad_symlink,
2984 a->worktree, ondisk_path, path2, blob2, 0,
2985 0, 1, a->allow_bad_symlinks, repo,
2986 a->progress_cb, a->progress_arg);
2988 err = install_blob(a->worktree, ondisk_path, path2,
2989 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2990 a->progress_cb, a->progress_arg);
2994 err = got_fileindex_entry_alloc(&ie, path2);
2997 err = got_fileindex_entry_update(ie,
2998 a->worktree->root_fd, path2, NULL, NULL, 1);
3000 got_fileindex_entry_free(ie);
3003 err = got_fileindex_entry_add(a->fileindex, ie);
3005 got_fileindex_entry_free(ie);
3008 if (is_bad_symlink) {
3009 got_fileindex_entry_filetype_set(ie,
3010 GOT_FILEIDX_MODE_BAD_SYMLINK);
3015 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3016 err = got_error_from_errno("fclose");
3017 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3018 err = got_error_from_errno("fclose");
3019 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3020 err = got_error_from_errno("fclose");
3027 static const struct got_error *
3028 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3030 struct got_worktree *worktree = arg;
3032 /* Reject merges into a work tree with mixed base commits. */
3033 if (got_fileindex_entry_has_commit(ie) &&
3034 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3035 SHA1_DIGEST_LENGTH) != 0)
3036 return got_error(GOT_ERR_MIXED_COMMITS);
3041 struct check_merge_conflicts_arg {
3042 struct got_worktree *worktree;
3043 struct got_fileindex *fileindex;
3044 struct got_repository *repo;
3047 static const struct got_error *
3048 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3049 struct got_blob_object *blob2, struct got_object_id *id1,
3050 struct got_object_id *id2, const char *path1, const char *path2,
3051 mode_t mode1, mode_t mode2, struct got_repository *repo)
3053 const struct got_error *err = NULL;
3054 struct check_merge_conflicts_arg *a = arg;
3055 unsigned char status;
3057 struct got_fileindex_entry *ie;
3058 const char *path = path2 ? path2 : path1;
3059 struct got_object_id *id = id2 ? id2 : id1;
3065 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3069 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3071 return got_error_from_errno("asprintf");
3073 /* Reject merges into a work tree with conflicted files. */
3074 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3078 if (status == GOT_STATUS_CONFLICT)
3079 return got_error(GOT_ERR_CONFLICTS);
3084 static const struct got_error *
3085 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3086 const char *fileindex_path, struct got_object_id *commit_id1,
3087 struct got_object_id *commit_id2, struct got_repository *repo,
3088 got_worktree_checkout_cb progress_cb, void *progress_arg,
3089 got_cancel_cb cancel_cb, void *cancel_arg)
3091 const struct got_error *err = NULL, *sync_err;
3092 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3093 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3094 struct check_merge_conflicts_arg cmc_arg;
3095 struct merge_file_cb_arg arg;
3096 char *label_orig = NULL;
3099 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3100 worktree->path_prefix);
3101 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3107 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3111 err = got_object_id_str(&id_str, commit_id1);
3115 if (asprintf(&label_orig, "%s: commit %s",
3116 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3117 err = got_error_from_errno("asprintf");
3124 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3125 worktree->path_prefix);
3129 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3133 cmc_arg.worktree = worktree;
3134 cmc_arg.fileindex = fileindex;
3135 cmc_arg.repo = repo;
3136 err = got_diff_tree(tree1, tree2, "", "", repo,
3137 check_merge_conflicts, &cmc_arg, 0);
3141 arg.worktree = worktree;
3142 arg.fileindex = fileindex;
3143 arg.progress_cb = progress_cb;
3144 arg.progress_arg = progress_arg;
3145 arg.cancel_cb = cancel_cb;
3146 arg.cancel_arg = cancel_arg;
3147 arg.label_orig = label_orig;
3148 arg.commit_id2 = commit_id2;
3149 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3150 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3151 sync_err = sync_fileindex(fileindex, fileindex_path);
3152 if (sync_err && err == NULL)
3156 got_object_tree_close(tree1);
3158 got_object_tree_close(tree2);
3163 const struct got_error *
3164 got_worktree_merge_files(struct got_worktree *worktree,
3165 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3166 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3167 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3169 const struct got_error *err, *unlockerr;
3170 char *fileindex_path = NULL;
3171 struct got_fileindex *fileindex = NULL;
3173 err = lock_worktree(worktree, LOCK_EX);
3177 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3181 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3186 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3187 commit_id2, repo, progress_cb, progress_arg,
3188 cancel_cb, cancel_arg);
3191 got_fileindex_free(fileindex);
3192 free(fileindex_path);
3193 unlockerr = lock_worktree(worktree, LOCK_SH);
3194 if (unlockerr && err == NULL)
3199 struct diff_dir_cb_arg {
3200 struct got_fileindex *fileindex;
3201 struct got_worktree *worktree;
3202 const char *status_path;
3203 size_t status_path_len;
3204 struct got_repository *repo;
3205 got_worktree_status_cb status_cb;
3207 got_cancel_cb cancel_cb;
3209 /* A pathlist containing per-directory pathlists of ignore patterns. */
3210 struct got_pathlist_head *ignores;
3211 int report_unchanged;
3215 static const struct got_error *
3216 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3217 int dirfd, const char *de_name,
3218 got_worktree_status_cb status_cb, void *status_arg,
3219 struct got_repository *repo, int report_unchanged)
3221 const struct got_error *err = NULL;
3222 unsigned char status = GOT_STATUS_NO_CHANGE;
3223 unsigned char staged_status = get_staged_status(ie);
3225 struct got_object_id blob_id, commit_id, staged_blob_id;
3226 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3227 struct got_object_id *staged_blob_idp = NULL;
3229 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3233 if (status == GOT_STATUS_NO_CHANGE &&
3234 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3237 if (got_fileindex_entry_has_blob(ie)) {
3238 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3239 blob_idp = &blob_id;
3241 if (got_fileindex_entry_has_commit(ie)) {
3242 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3243 commit_idp = &commit_id;
3245 if (staged_status == GOT_STATUS_ADD ||
3246 staged_status == GOT_STATUS_MODIFY) {
3247 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3248 SHA1_DIGEST_LENGTH);
3249 staged_blob_idp = &staged_blob_id;
3252 return (*status_cb)(status_arg, status, staged_status,
3253 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3256 static const struct got_error *
3257 status_old_new(void *arg, struct got_fileindex_entry *ie,
3258 struct dirent *de, const char *parent_path, int dirfd)
3260 const struct got_error *err = NULL;
3261 struct diff_dir_cb_arg *a = arg;
3264 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3265 return got_error(GOT_ERR_CANCELLED);
3267 if (got_path_cmp(parent_path, a->status_path,
3268 strlen(parent_path), a->status_path_len) != 0 &&
3269 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3272 if (parent_path[0]) {
3273 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3274 parent_path, de->d_name) == -1)
3275 return got_error_from_errno("asprintf");
3277 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3279 return got_error_from_errno("asprintf");
3282 err = report_file_status(ie, abspath, dirfd, de->d_name,
3283 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3288 static const struct got_error *
3289 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3291 struct diff_dir_cb_arg *a = arg;
3292 struct got_object_id blob_id, commit_id;
3293 unsigned char status;
3295 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3296 return got_error(GOT_ERR_CANCELLED);
3298 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3301 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3302 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3303 if (got_fileindex_entry_has_file_on_disk(ie))
3304 status = GOT_STATUS_MISSING;
3306 status = GOT_STATUS_DELETE;
3307 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3308 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3312 free_ignorelist(struct got_pathlist_head *ignorelist)
3314 struct got_pathlist_entry *pe;
3316 TAILQ_FOREACH(pe, ignorelist, entry)
3317 free((char *)pe->path);
3318 got_pathlist_free(ignorelist);
3322 free_ignores(struct got_pathlist_head *ignores)
3324 struct got_pathlist_entry *pe;
3326 TAILQ_FOREACH(pe, ignores, entry) {
3327 struct got_pathlist_head *ignorelist = pe->data;
3328 free_ignorelist(ignorelist);
3329 free((char *)pe->path);
3331 got_pathlist_free(ignores);
3334 static const struct got_error *
3335 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3337 const struct got_error *err = NULL;
3338 struct got_pathlist_entry *pe = NULL;
3339 struct got_pathlist_head *ignorelist;
3340 char *line = NULL, *pattern, *dirpath = NULL;
3341 size_t linesize = 0;
3344 ignorelist = calloc(1, sizeof(*ignorelist));
3345 if (ignorelist == NULL)
3346 return got_error_from_errno("calloc");
3347 TAILQ_INIT(ignorelist);
3349 while ((linelen = getline(&line, &linesize, f)) != -1) {
3350 if (linelen > 0 && line[linelen - 1] == '\n')
3351 line[linelen - 1] = '\0';
3353 /* Git's ignores may contain comments. */
3357 /* Git's negated patterns are not (yet?) supported. */
3361 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3363 err = got_error_from_errno("asprintf");
3366 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3371 err = got_error_from_errno("getline");
3375 dirpath = strdup(path);
3376 if (dirpath == NULL) {
3377 err = got_error_from_errno("strdup");
3380 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3383 if (err || pe == NULL) {
3385 free_ignorelist(ignorelist);
3391 match_ignores(struct got_pathlist_head *ignores, const char *path)
3393 struct got_pathlist_entry *pe;
3395 /* Handle patterns which match in all directories. */
3396 TAILQ_FOREACH(pe, ignores, entry) {
3397 struct got_pathlist_head *ignorelist = pe->data;
3398 struct got_pathlist_entry *pi;
3400 TAILQ_FOREACH(pi, ignorelist, entry) {
3401 const char *p, *pattern = pi->path;
3403 if (strncmp(pattern, "**/", 3) != 0)
3408 if (fnmatch(pattern, p,
3409 FNM_PATHNAME | FNM_LEADING_DIR)) {
3410 /* Retry in next directory. */
3411 while (*p && *p != '/')
3423 * The ignores pathlist contains ignore lists from children before
3424 * parents, so we can find the most specific ignorelist by walking
3425 * ignores backwards.
3427 pe = TAILQ_LAST(ignores, got_pathlist_head);
3429 if (got_path_is_child(path, pe->path, pe->path_len)) {
3430 struct got_pathlist_head *ignorelist = pe->data;
3431 struct got_pathlist_entry *pi;
3432 TAILQ_FOREACH(pi, ignorelist, entry) {
3433 const char *pattern = pi->path;
3434 int flags = FNM_LEADING_DIR;
3435 if (strstr(pattern, "/**/") == NULL)
3436 flags |= FNM_PATHNAME;
3437 if (fnmatch(pattern, path, flags))
3442 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3448 static const struct got_error *
3449 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3450 const char *path, int dirfd, const char *ignores_filename)
3452 const struct got_error *err = NULL;
3455 FILE *ignoresfile = NULL;
3457 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3458 path[0] ? "/" : "", ignores_filename) == -1)
3459 return got_error_from_errno("asprintf");
3462 fd = openat(dirfd, ignores_filename,
3463 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3465 if (errno != ENOENT && errno != EACCES)
3466 err = got_error_from_errno2("openat",
3469 ignoresfile = fdopen(fd, "r");
3470 if (ignoresfile == NULL)
3471 err = got_error_from_errno2("fdopen",
3475 err = read_ignores(ignores, path, ignoresfile);
3479 ignoresfile = fopen(ignorespath, "re");
3480 if (ignoresfile == NULL) {
3481 if (errno != ENOENT && errno != EACCES)
3482 err = got_error_from_errno2("fopen",
3485 err = read_ignores(ignores, path, ignoresfile);
3488 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3489 err = got_error_from_errno2("fclose", path);
3490 if (fd != -1 && close(fd) == -1 && err == NULL)
3491 err = got_error_from_errno2("close", path);
3496 static const struct got_error *
3497 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3500 const struct got_error *err = NULL;
3501 struct diff_dir_cb_arg *a = arg;
3507 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3508 return got_error(GOT_ERR_CANCELLED);
3510 if (parent_path[0]) {
3511 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3512 return got_error_from_errno("asprintf");
3517 if (de->d_type == DT_DIR) {
3518 if (!a->no_ignores && ignore != NULL &&
3519 match_ignores(a->ignores, path))
3521 } else if (!match_ignores(a->ignores, path) &&
3522 got_path_is_child(path, a->status_path, a->status_path_len))
3523 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3524 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3530 static const struct got_error *
3531 status_traverse(void *arg, const char *path, int dirfd)
3533 const struct got_error *err = NULL;
3534 struct diff_dir_cb_arg *a = arg;
3539 err = add_ignores(a->ignores, a->worktree->root_path,
3540 path, dirfd, ".cvsignore");
3544 err = add_ignores(a->ignores, a->worktree->root_path, path,
3545 dirfd, ".gitignore");
3550 static const struct got_error *
3551 report_single_file_status(const char *path, const char *ondisk_path,
3552 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3553 void *status_arg, struct got_repository *repo, int report_unchanged,
3554 struct got_pathlist_head *ignores, int no_ignores)
3556 struct got_fileindex_entry *ie;
3559 if (!no_ignores && match_ignores(ignores, path))
3562 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3564 return report_file_status(ie, ondisk_path, -1, NULL,
3565 status_cb, status_arg, repo, report_unchanged);
3567 if (lstat(ondisk_path, &sb) == -1) {
3568 if (errno != ENOENT)
3569 return got_error_from_errno2("lstat", ondisk_path);
3570 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3571 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3575 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3576 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3577 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3582 static const struct got_error *
3583 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3584 const char *root_path, const char *path)
3586 const struct got_error *err;
3587 char *parent_path, *next_parent_path = NULL;
3589 err = add_ignores(ignores, root_path, "", -1,
3594 err = add_ignores(ignores, root_path, "", -1,
3599 err = got_path_dirname(&parent_path, path);
3601 if (err->code == GOT_ERR_BAD_PATH)
3602 return NULL; /* cannot traverse parent */
3606 err = add_ignores(ignores, root_path, parent_path, -1,
3610 err = add_ignores(ignores, root_path, parent_path, -1,
3614 err = got_path_dirname(&next_parent_path, parent_path);
3616 if (err->code == GOT_ERR_BAD_PATH)
3617 err = NULL; /* traversed everything */
3620 if (got_path_is_root_dir(parent_path))
3623 parent_path = next_parent_path;
3624 next_parent_path = NULL;
3628 free(next_parent_path);
3632 static const struct got_error *
3633 worktree_status(struct got_worktree *worktree, const char *path,
3634 struct got_fileindex *fileindex, struct got_repository *repo,
3635 got_worktree_status_cb status_cb, void *status_arg,
3636 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3637 int report_unchanged)
3639 const struct got_error *err = NULL;
3641 struct got_fileindex_diff_dir_cb fdiff_cb;
3642 struct diff_dir_cb_arg arg;
3643 char *ondisk_path = NULL;
3644 struct got_pathlist_head ignores;
3646 TAILQ_INIT(&ignores);
3648 if (asprintf(&ondisk_path, "%s%s%s",
3649 worktree->root_path, path[0] ? "/" : "", path) == -1)
3650 return got_error_from_errno("asprintf");
3652 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3654 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3655 !got_err_open_nofollow_on_symlink())
3656 err = got_error_from_errno2("open", ondisk_path);
3659 err = add_ignores_from_parent_paths(&ignores,
3660 worktree->root_path, ondisk_path);
3664 err = report_single_file_status(path, ondisk_path,
3665 fileindex, status_cb, status_arg, repo,
3666 report_unchanged, &ignores, no_ignores);
3669 fdiff_cb.diff_old_new = status_old_new;
3670 fdiff_cb.diff_old = status_old;
3671 fdiff_cb.diff_new = status_new;
3672 fdiff_cb.diff_traverse = status_traverse;
3673 arg.fileindex = fileindex;
3674 arg.worktree = worktree;
3675 arg.status_path = path;
3676 arg.status_path_len = strlen(path);
3678 arg.status_cb = status_cb;
3679 arg.status_arg = status_arg;
3680 arg.cancel_cb = cancel_cb;
3681 arg.cancel_arg = cancel_arg;
3682 arg.report_unchanged = report_unchanged;
3683 arg.no_ignores = no_ignores;
3685 err = add_ignores_from_parent_paths(&ignores,
3686 worktree->root_path, path);
3690 arg.ignores = &ignores;
3691 err = got_fileindex_diff_dir(fileindex, fd,
3692 worktree->root_path, path, repo, &fdiff_cb, &arg);
3695 free_ignores(&ignores);
3696 if (fd != -1 && close(fd) == -1 && err == NULL)
3697 err = got_error_from_errno("close");
3702 const struct got_error *
3703 got_worktree_status(struct got_worktree *worktree,
3704 struct got_pathlist_head *paths, struct got_repository *repo,
3705 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3706 got_cancel_cb cancel_cb, void *cancel_arg)
3708 const struct got_error *err = NULL;
3709 char *fileindex_path = NULL;
3710 struct got_fileindex *fileindex = NULL;
3711 struct got_pathlist_entry *pe;
3713 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3717 TAILQ_FOREACH(pe, paths, entry) {
3718 err = worktree_status(worktree, pe->path, fileindex, repo,
3719 status_cb, status_arg, cancel_cb, cancel_arg,
3724 free(fileindex_path);
3725 got_fileindex_free(fileindex);
3729 const struct got_error *
3730 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3733 const struct got_error *err = NULL;
3734 char *resolved = NULL, *cwd = NULL, *path = NULL;
3737 char *abspath = NULL;
3738 char canonpath[PATH_MAX];
3742 cwd = getcwd(NULL, 0);
3744 return got_error_from_errno("getcwd");
3746 if (lstat(arg, &sb) == -1) {
3747 if (errno != ENOENT) {
3748 err = got_error_from_errno2("lstat", arg);
3753 if (S_ISLNK(sb.st_mode)) {
3755 * We cannot use realpath(3) with symlinks since we want to
3756 * operate on the symlink itself.
3757 * But we can make the path absolute, assuming it is relative
3758 * to the current working directory, and then canonicalize it.
3760 if (!got_path_is_absolute(arg)) {
3761 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3762 err = got_error_from_errno("asprintf");
3767 err = got_canonpath(abspath ? abspath : arg, canonpath,
3771 resolved = strdup(canonpath);
3772 if (resolved == NULL) {
3773 err = got_error_from_errno("strdup");
3777 resolved = realpath(arg, NULL);
3778 if (resolved == NULL) {
3779 if (errno != ENOENT) {
3780 err = got_error_from_errno2("realpath", arg);
3783 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3784 err = got_error_from_errno("asprintf");
3787 err = got_canonpath(abspath, canonpath,
3791 resolved = strdup(canonpath);
3792 if (resolved == NULL) {
3793 err = got_error_from_errno("strdup");
3799 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3800 strlen(got_worktree_get_root_path(worktree)))) {
3801 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3805 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3806 err = got_path_skip_common_ancestor(&path,
3807 got_worktree_get_root_path(worktree), resolved);
3813 err = got_error_from_errno("strdup");
3818 /* XXX status walk can't deal with trailing slash! */
3820 while (len > 0 && path[len - 1] == '/') {
3821 path[len - 1] = '\0';
3835 struct schedule_addition_args {
3836 struct got_worktree *worktree;
3837 struct got_fileindex *fileindex;
3838 got_worktree_checkout_cb progress_cb;
3840 struct got_repository *repo;
3843 static const struct got_error *
3844 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3845 const char *relpath, struct got_object_id *blob_id,
3846 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3847 int dirfd, const char *de_name)
3849 struct schedule_addition_args *a = arg;
3850 const struct got_error *err = NULL;
3851 struct got_fileindex_entry *ie;
3855 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3857 return got_error_from_errno("asprintf");
3859 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3861 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3865 /* Re-adding an existing entry is a no-op. */
3866 if (status == GOT_STATUS_ADD)
3868 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3873 if (status != GOT_STATUS_UNVERSIONED) {
3874 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3878 err = got_fileindex_entry_alloc(&ie, relpath);
3881 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3882 relpath, NULL, NULL, 1);
3884 got_fileindex_entry_free(ie);
3887 err = got_fileindex_entry_add(a->fileindex, ie);
3889 got_fileindex_entry_free(ie);
3896 if (status == GOT_STATUS_ADD)
3898 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3901 const struct got_error *
3902 got_worktree_schedule_add(struct got_worktree *worktree,
3903 struct got_pathlist_head *paths,
3904 got_worktree_checkout_cb progress_cb, void *progress_arg,
3905 struct got_repository *repo, int no_ignores)
3907 struct got_fileindex *fileindex = NULL;
3908 char *fileindex_path = NULL;
3909 const struct got_error *err = NULL, *sync_err, *unlockerr;
3910 struct got_pathlist_entry *pe;
3911 struct schedule_addition_args saa;
3913 err = lock_worktree(worktree, LOCK_EX);
3917 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3921 saa.worktree = worktree;
3922 saa.fileindex = fileindex;
3923 saa.progress_cb = progress_cb;
3924 saa.progress_arg = progress_arg;
3927 TAILQ_FOREACH(pe, paths, entry) {
3928 err = worktree_status(worktree, pe->path, fileindex, repo,
3929 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3933 sync_err = sync_fileindex(fileindex, fileindex_path);
3934 if (sync_err && err == NULL)
3937 free(fileindex_path);
3939 got_fileindex_free(fileindex);
3940 unlockerr = lock_worktree(worktree, LOCK_SH);
3941 if (unlockerr && err == NULL)
3946 struct schedule_deletion_args {
3947 struct got_worktree *worktree;
3948 struct got_fileindex *fileindex;
3949 got_worktree_delete_cb progress_cb;
3951 struct got_repository *repo;
3952 int delete_local_mods;
3954 const char *status_codes;
3957 static const struct got_error *
3958 schedule_for_deletion(void *arg, unsigned char status,
3959 unsigned char staged_status, const char *relpath,
3960 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3961 struct got_object_id *commit_id, int dirfd, const char *de_name)
3963 struct schedule_deletion_args *a = arg;
3964 const struct got_error *err = NULL;
3965 struct got_fileindex_entry *ie = NULL;
3969 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3971 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3973 staged_status = get_staged_status(ie);
3974 if (staged_status != GOT_STATUS_NO_CHANGE) {
3975 if (staged_status == GOT_STATUS_DELETE)
3977 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3980 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3982 return got_error_from_errno("asprintf");
3984 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3989 if (a->status_codes) {
3990 size_t ncodes = strlen(a->status_codes);
3992 for (i = 0; i < ncodes ; i++) {
3993 if (status == a->status_codes[i])
3997 /* Do not delete files in non-matching status. */
4001 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4002 a->status_codes[i] != GOT_STATUS_MISSING) {
4003 static char msg[64];
4004 snprintf(msg, sizeof(msg),
4005 "invalid status code '%c'", a->status_codes[i]);
4006 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4011 if (status != GOT_STATUS_NO_CHANGE) {
4012 if (status == GOT_STATUS_DELETE)
4014 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4015 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4018 if (status != GOT_STATUS_MODIFY &&
4019 status != GOT_STATUS_MISSING) {
4020 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4025 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4029 if (unlinkat(dirfd, de_name, 0) != 0) {
4030 err = got_error_from_errno2("unlinkat",
4034 } else if (unlink(ondisk_path) != 0) {
4035 err = got_error_from_errno2("unlink", ondisk_path);
4039 root_len = strlen(a->worktree->root_path);
4042 err = got_path_dirname(&parent, ondisk_path);
4046 ondisk_path = parent;
4047 if (rmdir(ondisk_path) == -1) {
4048 if (errno != ENOTEMPTY)
4049 err = got_error_from_errno2("rmdir",
4053 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4054 strlen(ondisk_path), root_len) != 0);
4057 got_fileindex_entry_mark_deleted_from_disk(ie);
4062 if (status == GOT_STATUS_DELETE)
4064 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4065 staged_status, relpath);
4068 const struct got_error *
4069 got_worktree_schedule_delete(struct got_worktree *worktree,
4070 struct got_pathlist_head *paths, int delete_local_mods,
4071 const char *status_codes,
4072 got_worktree_delete_cb progress_cb, void *progress_arg,
4073 struct got_repository *repo, int keep_on_disk)
4075 struct got_fileindex *fileindex = NULL;
4076 char *fileindex_path = NULL;
4077 const struct got_error *err = NULL, *sync_err, *unlockerr;
4078 struct got_pathlist_entry *pe;
4079 struct schedule_deletion_args sda;
4081 err = lock_worktree(worktree, LOCK_EX);
4085 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4089 sda.worktree = worktree;
4090 sda.fileindex = fileindex;
4091 sda.progress_cb = progress_cb;
4092 sda.progress_arg = progress_arg;
4094 sda.delete_local_mods = delete_local_mods;
4095 sda.keep_on_disk = keep_on_disk;
4096 sda.status_codes = status_codes;
4098 TAILQ_FOREACH(pe, paths, entry) {
4099 err = worktree_status(worktree, pe->path, fileindex, repo,
4100 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4104 sync_err = sync_fileindex(fileindex, fileindex_path);
4105 if (sync_err && err == NULL)
4108 free(fileindex_path);
4110 got_fileindex_free(fileindex);
4111 unlockerr = lock_worktree(worktree, LOCK_SH);
4112 if (unlockerr && err == NULL)
4117 static const struct got_error *
4118 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4120 const struct got_error *err = NULL;
4122 size_t linesize = 0, n;
4125 linelen = getline(&line, &linesize, infile);
4126 if (linelen == -1) {
4127 if (ferror(infile)) {
4128 err = got_error_from_errno("getline");
4134 n = fwrite(line, 1, linelen, outfile);
4136 err = got_ferror(outfile, GOT_ERR_IO);
4141 n = fwrite(line, 1, linelen, rejectfile);
4143 err = got_ferror(outfile, GOT_ERR_IO);
4150 static const struct got_error *
4151 skip_one_line(FILE *f)
4154 size_t linesize = 0;
4157 linelen = getline(&line, &linesize, f);
4158 if (linelen == -1) {
4160 return got_error_from_errno("getline");
4167 static const struct got_error *
4168 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4169 int start_old, int end_old, int start_new, int end_new,
4170 FILE *outfile, FILE *rejectfile)
4172 const struct got_error *err;
4174 /* Copy old file's lines leading up to patch. */
4175 while (!feof(f1) && *line_cur1 < start_old) {
4176 err = copy_one_line(f1, outfile, NULL);
4181 /* Skip new file's lines leading up to patch. */
4182 while (!feof(f2) && *line_cur2 < start_new) {
4184 err = copy_one_line(f2, NULL, rejectfile);
4186 err = skip_one_line(f2);
4191 /* Copy patched lines. */
4192 while (!feof(f2) && *line_cur2 <= end_new) {
4193 err = copy_one_line(f2, outfile, NULL);
4198 /* Skip over old file's replaced lines. */
4199 while (!feof(f1) && *line_cur1 <= end_old) {
4201 err = copy_one_line(f1, NULL, rejectfile);
4203 err = skip_one_line(f1);
4212 static const struct got_error *
4213 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4214 FILE *outfile, FILE *rejectfile)
4216 const struct got_error *err;
4219 /* Copy old file's lines until EOF. */
4221 err = copy_one_line(f1, outfile, NULL);
4228 /* Copy new file's lines until EOF. */
4230 err = copy_one_line(f2, NULL, rejectfile);
4240 static const struct got_error *
4241 apply_or_reject_change(int *choice, int *nchunks_used,
4242 struct diff_result *diff_result, int n,
4243 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4244 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4245 got_worktree_patch_cb patch_cb, void *patch_arg)
4247 const struct got_error *err = NULL;
4248 struct diff_chunk_context cc = {};
4249 int start_old, end_old, start_new, end_new;
4251 struct diff_output_unidiff_state *diff_state;
4252 struct diff_input_info diff_info;
4255 *choice = GOT_PATCH_CHOICE_NONE;
4257 /* Get changed line numbers without context lines for copy_change(). */
4258 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4259 start_old = cc.left.start;
4260 end_old = cc.left.end;
4261 start_new = cc.right.start;
4262 end_new = cc.right.end;
4264 /* Get the same change with context lines for display. */
4265 memset(&cc, 0, sizeof(cc));
4266 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4268 memset(&diff_info, 0, sizeof(diff_info));
4269 diff_info.left_path = relpath;
4270 diff_info.right_path = relpath;
4272 diff_state = diff_output_unidiff_state_alloc();
4273 if (diff_state == NULL)
4274 return got_error_set_errno(ENOMEM,
4275 "diff_output_unidiff_state_alloc");
4277 hunkfile = got_opentemp();
4278 if (hunkfile == NULL) {
4279 err = got_error_from_errno("got_opentemp");
4283 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4285 if (rc != DIFF_RC_OK) {
4286 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4290 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4291 err = got_ferror(hunkfile, GOT_ERR_IO);
4295 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4296 hunkfile, changeno, nchanges);
4301 case GOT_PATCH_CHOICE_YES:
4302 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4303 end_old, start_new, end_new, outfile, rejectfile);
4305 case GOT_PATCH_CHOICE_NO:
4306 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4307 end_old, start_new, end_new, rejectfile, outfile);
4309 case GOT_PATCH_CHOICE_QUIT:
4312 err = got_error(GOT_ERR_PATCH_CHOICE);
4316 diff_output_unidiff_state_free(diff_state);
4317 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4318 err = got_error_from_errno("fclose");
4322 struct revert_file_args {
4323 struct got_worktree *worktree;
4324 struct got_fileindex *fileindex;
4325 got_worktree_checkout_cb progress_cb;
4327 got_worktree_patch_cb patch_cb;
4329 struct got_repository *repo;
4330 int unlink_added_files;
4333 static const struct got_error *
4334 create_patched_content(char **path_outfile, int reverse_patch,
4335 struct got_object_id *blob_id, const char *path2,
4336 int dirfd2, const char *de_name2,
4337 const char *relpath, struct got_repository *repo,
4338 got_worktree_patch_cb patch_cb, void *patch_arg)
4340 const struct got_error *err, *free_err;
4341 struct got_blob_object *blob = NULL;
4342 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4344 char link_target[PATH_MAX];
4345 ssize_t link_len = 0;
4346 char *path1 = NULL, *id_str = NULL;
4348 struct got_diffreg_result *diffreg_result = NULL;
4349 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4350 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4352 *path_outfile = NULL;
4354 err = got_object_id_str(&id_str, blob_id);
4359 fd2 = openat(dirfd2, de_name2,
4360 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4362 if (!got_err_open_nofollow_on_symlink()) {
4363 err = got_error_from_errno2("openat", path2);
4366 link_len = readlinkat(dirfd2, de_name2,
4367 link_target, sizeof(link_target));
4368 if (link_len == -1) {
4369 return got_error_from_errno2("readlinkat",
4372 sb2.st_mode = S_IFLNK;
4373 sb2.st_size = link_len;
4376 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4378 if (!got_err_open_nofollow_on_symlink()) {
4379 err = got_error_from_errno2("open", path2);
4382 link_len = readlink(path2, link_target,
4383 sizeof(link_target));
4385 return got_error_from_errno2("readlink", path2);
4386 sb2.st_mode = S_IFLNK;
4387 sb2.st_size = link_len;
4391 if (fstat(fd2, &sb2) == -1) {
4392 err = got_error_from_errno2("fstat", path2);
4396 f2 = fdopen(fd2, "r");
4398 err = got_error_from_errno2("fdopen", path2);
4404 f2 = got_opentemp();
4406 err = got_error_from_errno2("got_opentemp", path2);
4409 n = fwrite(link_target, 1, link_len, f2);
4410 if (n != link_len) {
4411 err = got_ferror(f2, GOT_ERR_IO);
4414 if (fflush(f2) == EOF) {
4415 err = got_error_from_errno("fflush");
4421 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4425 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4429 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4433 err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4438 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4442 if (fseek(f1, 0L, SEEK_SET) == -1)
4443 return got_ferror(f1, GOT_ERR_IO);
4444 if (fseek(f2, 0L, SEEK_SET) == -1)
4445 return got_ferror(f2, GOT_ERR_IO);
4447 /* Count the number of actual changes in the diff result. */
4448 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4449 struct diff_chunk_context cc = {};
4450 diff_chunk_context_load_change(&cc, &nchunks_used,
4451 diffreg_result->result, n, 0);
4454 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4456 err = apply_or_reject_change(&choice, &nchunks_used,
4457 diffreg_result->result, n, relpath, f1, f2,
4458 &line_cur1, &line_cur2,
4459 reverse_patch ? NULL : outfile,
4460 reverse_patch ? outfile : NULL,
4461 ++i, nchanges, patch_cb, patch_arg);
4464 if (choice == GOT_PATCH_CHOICE_YES)
4466 else if (choice == GOT_PATCH_CHOICE_QUIT)
4470 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4471 reverse_patch ? NULL : outfile,
4472 reverse_patch ? outfile : NULL);
4476 if (!S_ISLNK(sb2.st_mode)) {
4477 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4478 err = got_error_from_errno2("fchmod", path2);
4486 got_object_blob_close(blob);
4487 free_err = got_diffreg_result_free(diffreg_result);
4490 if (f1 && fclose(f1) == EOF && err == NULL)
4491 err = got_error_from_errno2("fclose", path1);
4492 if (f2 && fclose(f2) == EOF && err == NULL)
4493 err = got_error_from_errno2("fclose", path2);
4494 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4495 err = got_error_from_errno2("close", path2);
4496 if (outfile && fclose(outfile) == EOF && err == NULL)
4497 err = got_error_from_errno2("fclose", *path_outfile);
4498 if (path1 && unlink(path1) == -1 && err == NULL)
4499 err = got_error_from_errno2("unlink", path1);
4500 if (err || !have_content) {
4501 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4502 err = got_error_from_errno2("unlink", *path_outfile);
4503 free(*path_outfile);
4504 *path_outfile = NULL;
4510 static const struct got_error *
4511 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4512 const char *relpath, struct got_object_id *blob_id,
4513 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4514 int dirfd, const char *de_name)
4516 struct revert_file_args *a = arg;
4517 const struct got_error *err = NULL;
4518 char *parent_path = NULL;
4519 struct got_fileindex_entry *ie;
4520 struct got_tree_object *tree = NULL;
4521 struct got_object_id *tree_id = NULL;
4522 const struct got_tree_entry *te = NULL;
4523 char *tree_path = NULL, *te_name;
4524 char *ondisk_path = NULL, *path_content = NULL;
4525 struct got_blob_object *blob = NULL;
4527 /* Reverting a staged deletion is a no-op. */
4528 if (status == GOT_STATUS_DELETE &&
4529 staged_status != GOT_STATUS_NO_CHANGE)
4532 if (status == GOT_STATUS_UNVERSIONED)
4533 return (*a->progress_cb)(a->progress_arg,
4534 GOT_STATUS_UNVERSIONED, relpath);
4536 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4538 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4540 /* Construct in-repository path of tree which contains this blob. */
4541 err = got_path_dirname(&parent_path, ie->path);
4543 if (err->code != GOT_ERR_BAD_PATH)
4545 parent_path = strdup("/");
4546 if (parent_path == NULL) {
4547 err = got_error_from_errno("strdup");
4551 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4552 tree_path = strdup(parent_path);
4553 if (tree_path == NULL) {
4554 err = got_error_from_errno("strdup");
4558 if (got_path_is_root_dir(parent_path)) {
4559 tree_path = strdup(a->worktree->path_prefix);
4560 if (tree_path == NULL) {
4561 err = got_error_from_errno("strdup");
4565 if (asprintf(&tree_path, "%s/%s",
4566 a->worktree->path_prefix, parent_path) == -1) {
4567 err = got_error_from_errno("asprintf");
4573 err = got_object_id_by_path(&tree_id, a->repo,
4574 a->worktree->base_commit_id, tree_path);
4576 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4577 (status == GOT_STATUS_ADD ||
4578 staged_status == GOT_STATUS_ADD)))
4581 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4585 err = got_path_basename(&te_name, ie->path);
4589 te = got_object_tree_find_entry(tree, te_name);
4591 if (te == NULL && status != GOT_STATUS_ADD &&
4592 staged_status != GOT_STATUS_ADD) {
4593 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4599 case GOT_STATUS_ADD:
4601 int choice = GOT_PATCH_CHOICE_NONE;
4602 err = (*a->patch_cb)(&choice, a->patch_arg,
4603 status, ie->path, NULL, 1, 1);
4606 if (choice != GOT_PATCH_CHOICE_YES)
4609 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4613 got_fileindex_entry_remove(a->fileindex, ie);
4614 if (a->unlink_added_files) {
4615 if (asprintf(&ondisk_path, "%s/%s",
4616 got_worktree_get_root_path(a->worktree),
4618 err = got_error_from_errno("asprintf");
4621 if (unlink(ondisk_path) == -1) {
4622 err = got_error_from_errno2("unlink",
4628 case GOT_STATUS_DELETE:
4630 int choice = GOT_PATCH_CHOICE_NONE;
4631 err = (*a->patch_cb)(&choice, a->patch_arg,
4632 status, ie->path, NULL, 1, 1);
4635 if (choice != GOT_PATCH_CHOICE_YES)
4639 case GOT_STATUS_MODIFY:
4640 case GOT_STATUS_MODE_CHANGE:
4641 case GOT_STATUS_CONFLICT:
4642 case GOT_STATUS_MISSING: {
4643 struct got_object_id id;
4644 if (staged_status == GOT_STATUS_ADD ||
4645 staged_status == GOT_STATUS_MODIFY) {
4646 memcpy(id.sha1, ie->staged_blob_sha1,
4647 SHA1_DIGEST_LENGTH);
4649 memcpy(id.sha1, ie->blob_sha1,
4650 SHA1_DIGEST_LENGTH);
4651 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4655 if (asprintf(&ondisk_path, "%s/%s",
4656 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4657 err = got_error_from_errno("asprintf");
4661 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4662 status == GOT_STATUS_CONFLICT)) {
4663 int is_bad_symlink = 0;
4664 err = create_patched_content(&path_content, 1, &id,
4665 ondisk_path, dirfd, de_name, ie->path, a->repo,
4666 a->patch_cb, a->patch_arg);
4667 if (err || path_content == NULL)
4669 if (te && S_ISLNK(te->mode)) {
4670 if (unlink(path_content) == -1) {
4671 err = got_error_from_errno2("unlink",
4675 err = install_symlink(&is_bad_symlink,
4676 a->worktree, ondisk_path, ie->path,
4677 blob, 0, 1, 0, 0, a->repo,
4678 a->progress_cb, a->progress_arg);
4680 if (rename(path_content, ondisk_path) == -1) {
4681 err = got_error_from_errno3("rename",
4682 path_content, ondisk_path);
4687 int is_bad_symlink = 0;
4688 if (te && S_ISLNK(te->mode)) {
4689 err = install_symlink(&is_bad_symlink,
4690 a->worktree, ondisk_path, ie->path,
4691 blob, 0, 1, 0, 0, a->repo,
4692 a->progress_cb, a->progress_arg);
4694 err = install_blob(a->worktree, ondisk_path,
4696 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4697 got_fileindex_perms_to_st(ie), blob,
4698 0, 1, 0, 0, a->repo,
4699 a->progress_cb, a->progress_arg);
4703 if (status == GOT_STATUS_DELETE ||
4704 status == GOT_STATUS_MODE_CHANGE) {
4705 err = got_fileindex_entry_update(ie,
4706 a->worktree->root_fd, relpath,
4708 a->worktree->base_commit_id->sha1, 1);
4712 if (is_bad_symlink) {
4713 got_fileindex_entry_filetype_set(ie,
4714 GOT_FILEIDX_MODE_BAD_SYMLINK);
4728 got_object_blob_close(blob);
4730 got_object_tree_close(tree);
4735 const struct got_error *
4736 got_worktree_revert(struct got_worktree *worktree,
4737 struct got_pathlist_head *paths,
4738 got_worktree_checkout_cb progress_cb, void *progress_arg,
4739 got_worktree_patch_cb patch_cb, void *patch_arg,
4740 struct got_repository *repo)
4742 struct got_fileindex *fileindex = NULL;
4743 char *fileindex_path = NULL;
4744 const struct got_error *err = NULL, *unlockerr = NULL;
4745 const struct got_error *sync_err = NULL;
4746 struct got_pathlist_entry *pe;
4747 struct revert_file_args rfa;
4749 err = lock_worktree(worktree, LOCK_EX);
4753 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4757 rfa.worktree = worktree;
4758 rfa.fileindex = fileindex;
4759 rfa.progress_cb = progress_cb;
4760 rfa.progress_arg = progress_arg;
4761 rfa.patch_cb = patch_cb;
4762 rfa.patch_arg = patch_arg;
4764 rfa.unlink_added_files = 0;
4765 TAILQ_FOREACH(pe, paths, entry) {
4766 err = worktree_status(worktree, pe->path, fileindex, repo,
4767 revert_file, &rfa, NULL, NULL, 1, 0);
4771 sync_err = sync_fileindex(fileindex, fileindex_path);
4772 if (sync_err && err == NULL)
4775 free(fileindex_path);
4777 got_fileindex_free(fileindex);
4778 unlockerr = lock_worktree(worktree, LOCK_SH);
4779 if (unlockerr && err == NULL)
4785 free_commitable(struct got_commitable *ct)
4788 free(ct->in_repo_path);
4789 free(ct->ondisk_path);
4791 free(ct->base_blob_id);
4792 free(ct->staged_blob_id);
4793 free(ct->base_commit_id);
4797 struct collect_commitables_arg {
4798 struct got_pathlist_head *commitable_paths;
4799 struct got_repository *repo;
4800 struct got_worktree *worktree;
4801 struct got_fileindex *fileindex;
4802 int have_staged_files;
4803 int allow_bad_symlinks;
4806 static const struct got_error *
4807 collect_commitables(void *arg, unsigned char status,
4808 unsigned char staged_status, const char *relpath,
4809 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4810 struct got_object_id *commit_id, int dirfd, const char *de_name)
4812 struct collect_commitables_arg *a = arg;
4813 const struct got_error *err = NULL;
4814 struct got_commitable *ct = NULL;
4815 struct got_pathlist_entry *new = NULL;
4816 char *parent_path = NULL, *path = NULL;
4819 if (a->have_staged_files) {
4820 if (staged_status != GOT_STATUS_MODIFY &&
4821 staged_status != GOT_STATUS_ADD &&
4822 staged_status != GOT_STATUS_DELETE)
4825 if (status == GOT_STATUS_CONFLICT)
4826 return got_error(GOT_ERR_COMMIT_CONFLICT);
4828 if (status != GOT_STATUS_MODIFY &&
4829 status != GOT_STATUS_MODE_CHANGE &&
4830 status != GOT_STATUS_ADD &&
4831 status != GOT_STATUS_DELETE)
4835 if (asprintf(&path, "/%s", relpath) == -1) {
4836 err = got_error_from_errno("asprintf");
4839 if (strcmp(path, "/") == 0) {
4840 parent_path = strdup("");
4841 if (parent_path == NULL)
4842 return got_error_from_errno("strdup");
4844 err = got_path_dirname(&parent_path, path);
4849 ct = calloc(1, sizeof(*ct));
4851 err = got_error_from_errno("calloc");
4855 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4857 err = got_error_from_errno("asprintf");
4861 if (staged_status == GOT_STATUS_ADD ||
4862 staged_status == GOT_STATUS_MODIFY) {
4863 struct got_fileindex_entry *ie;
4864 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4865 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4866 case GOT_FILEIDX_MODE_REGULAR_FILE:
4867 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4870 case GOT_FILEIDX_MODE_SYMLINK:
4874 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4877 ct->mode |= got_fileindex_entry_perms_get(ie);
4878 } else if (status != GOT_STATUS_DELETE &&
4879 staged_status != GOT_STATUS_DELETE) {
4881 if (fstatat(dirfd, de_name, &sb,
4882 AT_SYMLINK_NOFOLLOW) == -1) {
4883 err = got_error_from_errno2("fstatat",
4887 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4888 err = got_error_from_errno2("lstat", ct->ondisk_path);
4891 ct->mode = sb.st_mode;
4894 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4895 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4897 err = got_error_from_errno("asprintf");
4901 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4902 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4904 char target_path[PATH_MAX];
4906 target_len = readlink(ct->ondisk_path, target_path,
4907 sizeof(target_path));
4908 if (target_len == -1) {
4909 err = got_error_from_errno2("readlink",
4913 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4914 target_len, ct->ondisk_path, a->worktree->root_path);
4917 if (is_bad_symlink) {
4918 err = got_error_path(ct->ondisk_path,
4919 GOT_ERR_BAD_SYMLINK);
4925 ct->status = status;
4926 ct->staged_status = staged_status;
4927 ct->blob_id = NULL; /* will be filled in when blob gets created */
4928 if (ct->status != GOT_STATUS_ADD &&
4929 ct->staged_status != GOT_STATUS_ADD) {
4930 ct->base_blob_id = got_object_id_dup(blob_id);
4931 if (ct->base_blob_id == NULL) {
4932 err = got_error_from_errno("got_object_id_dup");
4935 ct->base_commit_id = got_object_id_dup(commit_id);
4936 if (ct->base_commit_id == NULL) {
4937 err = got_error_from_errno("got_object_id_dup");
4941 if (ct->staged_status == GOT_STATUS_ADD ||
4942 ct->staged_status == GOT_STATUS_MODIFY) {
4943 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4944 if (ct->staged_blob_id == NULL) {
4945 err = got_error_from_errno("got_object_id_dup");
4949 ct->path = strdup(path);
4950 if (ct->path == NULL) {
4951 err = got_error_from_errno("strdup");
4954 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4956 if (ct && (err || new == NULL))
4957 free_commitable(ct);
4963 static const struct got_error *write_tree(struct got_object_id **, int *,
4964 struct got_tree_object *, const char *, struct got_pathlist_head *,
4965 got_worktree_status_cb status_cb, void *status_arg,
4966 struct got_repository *);
4968 static const struct got_error *
4969 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4970 struct got_tree_entry *te, const char *parent_path,
4971 struct got_pathlist_head *commitable_paths,
4972 got_worktree_status_cb status_cb, void *status_arg,
4973 struct got_repository *repo)
4975 const struct got_error *err = NULL;
4976 struct got_tree_object *subtree;
4979 if (asprintf(&subpath, "%s%s%s", parent_path,
4980 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4981 return got_error_from_errno("asprintf");
4983 err = got_object_open_as_tree(&subtree, repo, &te->id);
4987 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4988 commitable_paths, status_cb, status_arg, repo);
4989 got_object_tree_close(subtree);
4994 static const struct got_error *
4995 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4997 const struct got_error *err = NULL;
4998 char *ct_parent_path = NULL;
5002 if (strchr(ct->in_repo_path, '/') == NULL) {
5003 *match = got_path_is_root_dir(path);
5007 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5010 *match = (strcmp(path, ct_parent_path) == 0);
5011 free(ct_parent_path);
5016 get_ct_file_mode(struct got_commitable *ct)
5018 if (S_ISLNK(ct->mode))
5021 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5024 static const struct got_error *
5025 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5026 struct got_tree_entry *te, struct got_commitable *ct)
5028 const struct got_error *err = NULL;
5032 err = got_object_tree_entry_dup(new_te, te);
5036 (*new_te)->mode = get_ct_file_mode(ct);
5038 if (ct->staged_status == GOT_STATUS_MODIFY)
5039 memcpy(&(*new_te)->id, ct->staged_blob_id,
5040 sizeof((*new_te)->id));
5042 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5044 if (err && *new_te) {
5051 static const struct got_error *
5052 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5053 struct got_commitable *ct)
5055 const struct got_error *err = NULL;
5056 char *ct_name = NULL;
5060 *new_te = calloc(1, sizeof(**new_te));
5061 if (*new_te == NULL)
5062 return got_error_from_errno("calloc");
5064 err = got_path_basename(&ct_name, ct->path);
5067 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5068 sizeof((*new_te)->name)) {
5069 err = got_error(GOT_ERR_NO_SPACE);
5073 (*new_te)->mode = get_ct_file_mode(ct);
5075 if (ct->staged_status == GOT_STATUS_ADD)
5076 memcpy(&(*new_te)->id, ct->staged_blob_id,
5077 sizeof((*new_te)->id));
5079 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5082 if (err && *new_te) {
5089 static const struct got_error *
5090 insert_tree_entry(struct got_tree_entry *new_te,
5091 struct got_pathlist_head *paths)
5093 const struct got_error *err = NULL;
5094 struct got_pathlist_entry *new_pe;
5096 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5100 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5104 static const struct got_error *
5105 report_ct_status(struct got_commitable *ct,
5106 got_worktree_status_cb status_cb, void *status_arg)
5108 const char *ct_path = ct->path;
5109 unsigned char status;
5111 if (status_cb == NULL) /* no commit progress output desired */
5114 while (ct_path[0] == '/')
5117 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5118 status = ct->staged_status;
5120 status = ct->status;
5122 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5123 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5126 static const struct got_error *
5127 match_modified_subtree(int *modified, struct got_tree_entry *te,
5128 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5130 const struct got_error *err = NULL;
5131 struct got_pathlist_entry *pe;
5136 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5137 got_path_is_root_dir(base_tree_path) ? "" : "/",
5139 return got_error_from_errno("asprintf");
5141 TAILQ_FOREACH(pe, commitable_paths, entry) {
5142 struct got_commitable *ct = pe->data;
5143 *modified = got_path_is_child(ct->in_repo_path, te_path,
5153 static const struct got_error *
5154 match_deleted_or_modified_ct(struct got_commitable **ctp,
5155 struct got_tree_entry *te, const char *base_tree_path,
5156 struct got_pathlist_head *commitable_paths)
5158 const struct got_error *err = NULL;
5159 struct got_pathlist_entry *pe;
5163 TAILQ_FOREACH(pe, commitable_paths, entry) {
5164 struct got_commitable *ct = pe->data;
5165 char *ct_name = NULL;
5168 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5169 if (ct->status != GOT_STATUS_MODIFY &&
5170 ct->status != GOT_STATUS_MODE_CHANGE &&
5171 ct->status != GOT_STATUS_DELETE)
5174 if (ct->staged_status != GOT_STATUS_MODIFY &&
5175 ct->staged_status != GOT_STATUS_DELETE)
5179 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5182 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5188 err = got_path_basename(&ct_name, pe->path);
5192 if (strcmp(te->name, ct_name) != 0) {
5205 static const struct got_error *
5206 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5207 const char *child_path, const char *path_base_tree,
5208 struct got_pathlist_head *commitable_paths,
5209 got_worktree_status_cb status_cb, void *status_arg,
5210 struct got_repository *repo)
5212 const struct got_error *err = NULL;
5213 struct got_tree_entry *new_te;
5215 struct got_object_id *id = NULL;
5220 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5221 got_path_is_root_dir(path_base_tree) ? "" : "/",
5223 return got_error_from_errno("asprintf");
5225 new_te = calloc(1, sizeof(*new_te));
5227 return got_error_from_errno("calloc");
5228 new_te->mode = S_IFDIR;
5230 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5231 sizeof(new_te->name)) {
5232 err = got_error(GOT_ERR_NO_SPACE);
5235 err = write_tree(&id, &nentries, NULL, subtree_path,
5236 commitable_paths, status_cb, status_arg, repo);
5241 memcpy(&new_te->id, id, sizeof(new_te->id));
5250 static const struct got_error *
5251 write_tree(struct got_object_id **new_tree_id, int *nentries,
5252 struct got_tree_object *base_tree, const char *path_base_tree,
5253 struct got_pathlist_head *commitable_paths,
5254 got_worktree_status_cb status_cb, void *status_arg,
5255 struct got_repository *repo)
5257 const struct got_error *err = NULL;
5258 struct got_pathlist_head paths;
5259 struct got_tree_entry *te, *new_te = NULL;
5260 struct got_pathlist_entry *pe;
5265 /* Insert, and recurse into, newly added entries first. */
5266 TAILQ_FOREACH(pe, commitable_paths, entry) {
5267 struct got_commitable *ct = pe->data;
5268 char *child_path = NULL, *slash;
5270 if ((ct->status != GOT_STATUS_ADD &&
5271 ct->staged_status != GOT_STATUS_ADD) ||
5272 (ct->flags & GOT_COMMITABLE_ADDED))
5275 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5276 strlen(path_base_tree)))
5279 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5284 slash = strchr(child_path, '/');
5285 if (slash == NULL) {
5286 err = alloc_added_blob_tree_entry(&new_te, ct);
5289 err = report_ct_status(ct, status_cb, status_arg);
5292 ct->flags |= GOT_COMMITABLE_ADDED;
5293 err = insert_tree_entry(new_te, &paths);
5298 *slash = '\0'; /* trim trailing path components */
5299 if (base_tree == NULL ||
5300 got_object_tree_find_entry(base_tree, child_path)
5302 err = make_subtree_for_added_blob(&new_te,
5303 child_path, path_base_tree,
5304 commitable_paths, status_cb, status_arg,
5308 err = insert_tree_entry(new_te, &paths);
5317 int i, nbase_entries;
5318 /* Handle modified and deleted entries. */
5319 nbase_entries = got_object_tree_get_nentries(base_tree);
5320 for (i = 0; i < nbase_entries; i++) {
5321 struct got_commitable *ct = NULL;
5323 te = got_object_tree_get_entry(base_tree, i);
5324 if (got_object_tree_entry_is_submodule(te)) {
5325 /* Entry is a submodule; just copy it. */
5326 err = got_object_tree_entry_dup(&new_te, te);
5329 err = insert_tree_entry(new_te, &paths);
5336 if (S_ISDIR(te->mode)) {
5338 err = got_object_tree_entry_dup(&new_te, te);
5341 err = match_modified_subtree(&modified, te,
5342 path_base_tree, commitable_paths);
5345 /* Avoid recursion into unmodified subtrees. */
5347 struct got_object_id *new_id;
5349 err = write_subtree(&new_id,
5351 path_base_tree, commitable_paths,
5352 status_cb, status_arg, repo);
5355 if (nsubentries == 0) {
5356 /* All entries were deleted. */
5360 memcpy(&new_te->id, new_id,
5361 sizeof(new_te->id));
5364 err = insert_tree_entry(new_te, &paths);
5371 err = match_deleted_or_modified_ct(&ct, te,
5372 path_base_tree, commitable_paths);
5376 /* NB: Deleted entries get dropped here. */
5377 if (ct->status == GOT_STATUS_MODIFY ||
5378 ct->status == GOT_STATUS_MODE_CHANGE ||
5379 ct->staged_status == GOT_STATUS_MODIFY) {
5380 err = alloc_modified_blob_tree_entry(
5384 err = insert_tree_entry(new_te, &paths);
5389 err = report_ct_status(ct, status_cb,
5394 /* Entry is unchanged; just copy it. */
5395 err = got_object_tree_entry_dup(&new_te, te);
5398 err = insert_tree_entry(new_te, &paths);
5406 /* Write new list of entries; deleted entries have been dropped. */
5407 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5409 got_pathlist_free(&paths);
5413 static const struct got_error *
5414 update_fileindex_after_commit(struct got_worktree *worktree,
5415 struct got_pathlist_head *commitable_paths,
5416 struct got_object_id *new_base_commit_id,
5417 struct got_fileindex *fileindex, int have_staged_files)
5419 const struct got_error *err = NULL;
5420 struct got_pathlist_entry *pe;
5421 char *relpath = NULL;
5423 TAILQ_FOREACH(pe, commitable_paths, entry) {
5424 struct got_fileindex_entry *ie;
5425 struct got_commitable *ct = pe->data;
5427 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5429 err = got_path_skip_common_ancestor(&relpath,
5430 worktree->root_path, ct->ondisk_path);
5435 if (ct->status == GOT_STATUS_DELETE ||
5436 ct->staged_status == GOT_STATUS_DELETE) {
5437 got_fileindex_entry_remove(fileindex, ie);
5438 } else if (ct->staged_status == GOT_STATUS_ADD ||
5439 ct->staged_status == GOT_STATUS_MODIFY) {
5440 got_fileindex_entry_stage_set(ie,
5441 GOT_FILEIDX_STAGE_NONE);
5442 got_fileindex_entry_staged_filetype_set(ie, 0);
5444 err = got_fileindex_entry_update(ie,
5445 worktree->root_fd, relpath,
5446 ct->staged_blob_id->sha1,
5447 new_base_commit_id->sha1,
5448 !have_staged_files);
5450 err = got_fileindex_entry_update(ie,
5451 worktree->root_fd, relpath,
5453 new_base_commit_id->sha1,
5454 !have_staged_files);
5456 err = got_fileindex_entry_alloc(&ie, pe->path);
5459 err = got_fileindex_entry_update(ie,
5460 worktree->root_fd, relpath, ct->blob_id->sha1,
5461 new_base_commit_id->sha1, 1);
5463 got_fileindex_entry_free(ie);
5466 err = got_fileindex_entry_add(fileindex, ie);
5468 got_fileindex_entry_free(ie);
5481 static const struct got_error *
5482 check_out_of_date(const char *in_repo_path, unsigned char status,
5483 unsigned char staged_status, struct got_object_id *base_blob_id,
5484 struct got_object_id *base_commit_id,
5485 struct got_object_id *head_commit_id, struct got_repository *repo,
5488 const struct got_error *err = NULL;
5489 struct got_object_id *id = NULL;
5491 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5492 /* Trivial case: base commit == head commit */
5493 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5496 * Ensure file content which local changes were based
5497 * on matches file content in the branch head.
5499 err = got_object_id_by_path(&id, repo, head_commit_id,
5502 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5503 err = got_error(ood_errcode);
5505 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5506 err = got_error(ood_errcode);
5508 /* Require that added files don't exist in the branch head. */
5509 err = got_object_id_by_path(&id, repo, head_commit_id,
5511 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5513 err = id ? got_error(ood_errcode) : NULL;
5520 const struct got_error *
5521 commit_worktree(struct got_object_id **new_commit_id,
5522 struct got_pathlist_head *commitable_paths,
5523 struct got_object_id *head_commit_id,
5524 struct got_object_id *parent_id2,
5525 struct got_worktree *worktree,
5526 const char *author, const char *committer,
5527 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5528 got_worktree_status_cb status_cb, void *status_arg,
5529 struct got_repository *repo)
5531 const struct got_error *err = NULL, *unlockerr = NULL;
5532 struct got_pathlist_entry *pe;
5533 const char *head_ref_name = NULL;
5534 struct got_commit_object *head_commit = NULL;
5535 struct got_reference *head_ref2 = NULL;
5536 struct got_object_id *head_commit_id2 = NULL;
5537 struct got_tree_object *head_tree = NULL;
5538 struct got_object_id *new_tree_id = NULL;
5539 int nentries, nparents = 0;
5540 struct got_object_id_queue parent_ids;
5541 struct got_object_qid *pid = NULL;
5542 char *logmsg = NULL;
5544 *new_commit_id = NULL;
5546 STAILQ_INIT(&parent_ids);
5548 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5552 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5556 if (commit_msg_cb != NULL) {
5557 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5562 if (logmsg == NULL || strlen(logmsg) == 0) {
5563 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5567 /* Create blobs from added and modified files and record their IDs. */
5568 TAILQ_FOREACH(pe, commitable_paths, entry) {
5569 struct got_commitable *ct = pe->data;
5572 /* Blobs for staged files already exist. */
5573 if (ct->staged_status == GOT_STATUS_ADD ||
5574 ct->staged_status == GOT_STATUS_MODIFY)
5577 if (ct->status != GOT_STATUS_ADD &&
5578 ct->status != GOT_STATUS_MODIFY &&
5579 ct->status != GOT_STATUS_MODE_CHANGE)
5582 if (asprintf(&ondisk_path, "%s/%s",
5583 worktree->root_path, pe->path) == -1) {
5584 err = got_error_from_errno("asprintf");
5587 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5593 /* Recursively write new tree objects. */
5594 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5595 commitable_paths, status_cb, status_arg, repo);
5599 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5602 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5605 err = got_object_qid_alloc(&pid, parent_id2);
5608 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5611 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5612 nparents, author, time(NULL), committer, time(NULL), logmsg, repo);
5618 /* Check if a concurrent commit to our branch has occurred. */
5619 head_ref_name = got_worktree_get_head_ref_name(worktree);
5620 if (head_ref_name == NULL) {
5621 err = got_error_from_errno("got_worktree_get_head_ref_name");
5624 /* Lock the reference here to prevent concurrent modification. */
5625 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5628 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5631 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5632 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5635 /* Update branch head in repository. */
5636 err = got_ref_change_ref(head_ref2, *new_commit_id);
5639 err = got_ref_write(head_ref2, repo);
5643 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5647 err = ref_base_commit(worktree, repo);
5651 got_object_id_queue_free(&parent_ids);
5653 got_object_tree_close(head_tree);
5655 got_object_commit_close(head_commit);
5656 free(head_commit_id2);
5658 unlockerr = got_ref_unlock(head_ref2);
5659 if (unlockerr && err == NULL)
5661 got_ref_close(head_ref2);
5666 static const struct got_error *
5667 check_path_is_commitable(const char *path,
5668 struct got_pathlist_head *commitable_paths)
5670 struct got_pathlist_entry *cpe = NULL;
5671 size_t path_len = strlen(path);
5673 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5674 struct got_commitable *ct = cpe->data;
5675 const char *ct_path = ct->path;
5677 while (ct_path[0] == '/')
5680 if (strcmp(path, ct_path) == 0 ||
5681 got_path_is_child(ct_path, path, path_len))
5686 return got_error_path(path, GOT_ERR_BAD_PATH);
5691 static const struct got_error *
5692 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5694 int *have_staged_files = arg;
5696 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5697 *have_staged_files = 1;
5698 return got_error(GOT_ERR_CANCELLED);
5704 static const struct got_error *
5705 check_non_staged_files(struct got_fileindex *fileindex,
5706 struct got_pathlist_head *paths)
5708 struct got_pathlist_entry *pe;
5709 struct got_fileindex_entry *ie;
5711 TAILQ_FOREACH(pe, paths, entry) {
5712 if (pe->path[0] == '\0')
5714 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5716 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5717 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5718 return got_error_path(pe->path,
5719 GOT_ERR_FILE_NOT_STAGED);
5725 const struct got_error *
5726 got_worktree_commit(struct got_object_id **new_commit_id,
5727 struct got_worktree *worktree, struct got_pathlist_head *paths,
5728 const char *author, const char *committer, int allow_bad_symlinks,
5729 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5730 got_worktree_status_cb status_cb, void *status_arg,
5731 struct got_repository *repo)
5733 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5734 struct got_fileindex *fileindex = NULL;
5735 char *fileindex_path = NULL;
5736 struct got_pathlist_head commitable_paths;
5737 struct collect_commitables_arg cc_arg;
5738 struct got_pathlist_entry *pe;
5739 struct got_reference *head_ref = NULL;
5740 struct got_object_id *head_commit_id = NULL;
5741 int have_staged_files = 0;
5743 *new_commit_id = NULL;
5745 TAILQ_INIT(&commitable_paths);
5747 err = lock_worktree(worktree, LOCK_EX);
5751 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5755 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5759 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5763 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5764 &have_staged_files);
5765 if (err && err->code != GOT_ERR_CANCELLED)
5767 if (have_staged_files) {
5768 err = check_non_staged_files(fileindex, paths);
5773 cc_arg.commitable_paths = &commitable_paths;
5774 cc_arg.worktree = worktree;
5775 cc_arg.fileindex = fileindex;
5777 cc_arg.have_staged_files = have_staged_files;
5778 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5779 TAILQ_FOREACH(pe, paths, entry) {
5780 err = worktree_status(worktree, pe->path, fileindex, repo,
5781 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
5786 if (TAILQ_EMPTY(&commitable_paths)) {
5787 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5791 TAILQ_FOREACH(pe, paths, entry) {
5792 err = check_path_is_commitable(pe->path, &commitable_paths);
5797 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5798 struct got_commitable *ct = pe->data;
5799 const char *ct_path = ct->in_repo_path;
5801 while (ct_path[0] == '/')
5803 err = check_out_of_date(ct_path, ct->status,
5804 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5805 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5811 err = commit_worktree(new_commit_id, &commitable_paths,
5812 head_commit_id, NULL, worktree, author, committer,
5813 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5817 err = update_fileindex_after_commit(worktree, &commitable_paths,
5818 *new_commit_id, fileindex, have_staged_files);
5819 sync_err = sync_fileindex(fileindex, fileindex_path);
5820 if (sync_err && err == NULL)
5824 got_fileindex_free(fileindex);
5825 free(fileindex_path);
5826 unlockerr = lock_worktree(worktree, LOCK_SH);
5827 if (unlockerr && err == NULL)
5829 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5830 struct got_commitable *ct = pe->data;
5831 free_commitable(ct);
5833 got_pathlist_free(&commitable_paths);
5838 got_commitable_get_path(struct got_commitable *ct)
5844 got_commitable_get_status(struct got_commitable *ct)
5849 struct check_rebase_ok_arg {
5850 struct got_worktree *worktree;
5851 struct got_repository *repo;
5854 static const struct got_error *
5855 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5857 const struct got_error *err = NULL;
5858 struct check_rebase_ok_arg *a = arg;
5859 unsigned char status;
5863 /* Reject rebase of a work tree with mixed base commits. */
5864 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5865 SHA1_DIGEST_LENGTH))
5866 return got_error(GOT_ERR_MIXED_COMMITS);
5868 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5870 return got_error_from_errno("asprintf");
5872 /* Reject rebase of a work tree with modified or staged files. */
5873 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5878 if (status != GOT_STATUS_NO_CHANGE)
5879 return got_error(GOT_ERR_MODIFIED);
5880 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5881 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5886 const struct got_error *
5887 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5888 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5889 struct got_worktree *worktree, struct got_reference *branch,
5890 struct got_repository *repo)
5892 const struct got_error *err = NULL;
5893 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5894 char *branch_ref_name = NULL;
5895 char *fileindex_path = NULL;
5896 struct check_rebase_ok_arg ok_arg;
5897 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5898 struct got_object_id *wt_branch_tip = NULL;
5900 *new_base_branch_ref = NULL;
5904 err = lock_worktree(worktree, LOCK_EX);
5908 err = open_fileindex(fileindex, &fileindex_path, worktree);
5912 ok_arg.worktree = worktree;
5914 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5919 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5923 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5927 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5931 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5936 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5939 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5940 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5944 err = got_ref_alloc_symref(new_base_branch_ref,
5945 new_base_branch_ref_name, wt_branch);
5948 err = got_ref_write(*new_base_branch_ref, repo);
5952 /* TODO Lock original branch's ref while rebasing? */
5954 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5958 err = got_ref_write(branch_ref, repo);
5962 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5963 worktree->base_commit_id);
5966 err = got_ref_write(*tmp_branch, repo);
5970 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5974 free(fileindex_path);
5975 free(tmp_branch_name);
5976 free(new_base_branch_ref_name);
5977 free(branch_ref_name);
5979 got_ref_close(branch_ref);
5981 got_ref_close(wt_branch);
5982 free(wt_branch_tip);
5984 if (*new_base_branch_ref) {
5985 got_ref_close(*new_base_branch_ref);
5986 *new_base_branch_ref = NULL;
5989 got_ref_close(*tmp_branch);
5993 got_fileindex_free(*fileindex);
5996 lock_worktree(worktree, LOCK_SH);
6001 const struct got_error *
6002 got_worktree_rebase_continue(struct got_object_id **commit_id,
6003 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6004 struct got_reference **branch, struct got_fileindex **fileindex,
6005 struct got_worktree *worktree, struct got_repository *repo)
6007 const struct got_error *err;
6008 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6009 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6010 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6011 char *fileindex_path = NULL;
6012 int have_staged_files = 0;
6015 *new_base_branch = NULL;
6020 err = lock_worktree(worktree, LOCK_EX);
6024 err = open_fileindex(fileindex, &fileindex_path, worktree);
6028 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6029 &have_staged_files);
6030 if (err && err->code != GOT_ERR_CANCELLED)
6032 if (have_staged_files) {
6033 err = got_error(GOT_ERR_STAGED_PATHS);
6037 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6041 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6045 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6049 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6053 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6057 err = got_ref_open(branch, repo,
6058 got_ref_get_symref_target(branch_ref), 0);
6062 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6066 err = got_ref_resolve(commit_id, repo, commit_ref);
6070 err = got_ref_open(new_base_branch, repo,
6071 new_base_branch_ref_name, 0);
6075 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6079 free(commit_ref_name);
6080 free(branch_ref_name);
6081 free(fileindex_path);
6083 got_ref_close(commit_ref);
6085 got_ref_close(branch_ref);
6090 got_ref_close(*tmp_branch);
6093 if (*new_base_branch) {
6094 got_ref_close(*new_base_branch);
6095 *new_base_branch = NULL;
6098 got_ref_close(*branch);
6102 got_fileindex_free(*fileindex);
6105 lock_worktree(worktree, LOCK_SH);
6110 const struct got_error *
6111 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6113 const struct got_error *err;
6114 char *tmp_branch_name = NULL;
6116 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6120 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6121 free(tmp_branch_name);
6125 static const struct got_error *
6126 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6127 char **logmsg, void *arg)
6133 static const struct got_error *
6134 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6135 const char *path, struct got_object_id *blob_id,
6136 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6137 int dirfd, const char *de_name)
6142 struct collect_merged_paths_arg {
6143 got_worktree_checkout_cb progress_cb;
6145 struct got_pathlist_head *merged_paths;
6148 static const struct got_error *
6149 collect_merged_paths(void *arg, unsigned char status, const char *path)
6151 const struct got_error *err;
6152 struct collect_merged_paths_arg *a = arg;
6154 struct got_pathlist_entry *new;
6156 err = (*a->progress_cb)(a->progress_arg, status, path);
6160 if (status != GOT_STATUS_MERGE &&
6161 status != GOT_STATUS_ADD &&
6162 status != GOT_STATUS_DELETE &&
6163 status != GOT_STATUS_CONFLICT)
6168 return got_error_from_errno("strdup");
6170 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6171 if (err || new == NULL)
6177 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6179 struct got_pathlist_entry *pe;
6181 TAILQ_FOREACH(pe, merged_paths, entry)
6182 free((char *)pe->path);
6184 got_pathlist_free(merged_paths);
6187 static const struct got_error *
6188 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6189 int is_rebase, struct got_repository *repo)
6191 const struct got_error *err;
6192 struct got_reference *commit_ref = NULL;
6194 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6196 if (err->code != GOT_ERR_NOT_REF)
6198 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6201 err = got_ref_write(commit_ref, repo);
6204 } else if (is_rebase) {
6205 struct got_object_id *stored_id;
6208 err = got_ref_resolve(&stored_id, repo, commit_ref);
6211 cmp = got_object_id_cmp(commit_id, stored_id);
6214 err = got_error(GOT_ERR_REBASE_COMMITID);
6220 got_ref_close(commit_ref);
6224 static const struct got_error *
6225 rebase_merge_files(struct got_pathlist_head *merged_paths,
6226 const char *commit_ref_name, struct got_worktree *worktree,
6227 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6228 struct got_object_id *commit_id, struct got_repository *repo,
6229 got_worktree_checkout_cb progress_cb, void *progress_arg,
6230 got_cancel_cb cancel_cb, void *cancel_arg)
6232 const struct got_error *err;
6233 struct got_reference *commit_ref = NULL;
6234 struct collect_merged_paths_arg cmp_arg;
6235 char *fileindex_path;
6237 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6239 err = get_fileindex_path(&fileindex_path, worktree);
6243 cmp_arg.progress_cb = progress_cb;
6244 cmp_arg.progress_arg = progress_arg;
6245 cmp_arg.merged_paths = merged_paths;
6246 err = merge_files(worktree, fileindex, fileindex_path,
6247 parent_commit_id, commit_id, repo, collect_merged_paths,
6248 &cmp_arg, cancel_cb, cancel_arg);
6250 got_ref_close(commit_ref);
6254 const struct got_error *
6255 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6256 struct got_worktree *worktree, struct got_fileindex *fileindex,
6257 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6258 struct got_repository *repo,
6259 got_worktree_checkout_cb progress_cb, void *progress_arg,
6260 got_cancel_cb cancel_cb, void *cancel_arg)
6262 const struct got_error *err;
6263 char *commit_ref_name;
6265 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6269 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6273 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6274 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6275 progress_arg, cancel_cb, cancel_arg);
6277 free(commit_ref_name);
6281 const struct got_error *
6282 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6283 struct got_worktree *worktree, struct got_fileindex *fileindex,
6284 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6285 struct got_repository *repo,
6286 got_worktree_checkout_cb progress_cb, void *progress_arg,
6287 got_cancel_cb cancel_cb, void *cancel_arg)
6289 const struct got_error *err;
6290 char *commit_ref_name;
6292 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6296 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6300 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6301 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6302 progress_arg, cancel_cb, cancel_arg);
6304 free(commit_ref_name);
6308 static const struct got_error *
6309 rebase_commit(struct got_object_id **new_commit_id,
6310 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6311 struct got_worktree *worktree, struct got_fileindex *fileindex,
6312 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6313 const char *new_logmsg, struct got_repository *repo)
6315 const struct got_error *err, *sync_err;
6316 struct got_pathlist_head commitable_paths;
6317 struct collect_commitables_arg cc_arg;
6318 char *fileindex_path = NULL;
6319 struct got_reference *head_ref = NULL;
6320 struct got_object_id *head_commit_id = NULL;
6321 char *logmsg = NULL;
6323 TAILQ_INIT(&commitable_paths);
6324 *new_commit_id = NULL;
6326 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6328 err = get_fileindex_path(&fileindex_path, worktree);
6332 cc_arg.commitable_paths = &commitable_paths;
6333 cc_arg.worktree = worktree;
6335 cc_arg.have_staged_files = 0;
6337 * If possible get the status of individual files directly to
6338 * avoid crawling the entire work tree once per rebased commit.
6340 * Ideally, merged_paths would contain a list of commitables
6341 * we could use so we could skip worktree_status() entirely.
6342 * However, we would then need carefully keep track of cumulative
6343 * effects of operations such as file additions and deletions
6344 * in 'got histedit -f' (folding multiple commits into one),
6345 * and this extra complexity is not really worth it.
6348 struct got_pathlist_entry *pe;
6349 TAILQ_FOREACH(pe, merged_paths, entry) {
6350 err = worktree_status(worktree, pe->path, fileindex,
6351 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6357 err = worktree_status(worktree, "", fileindex, repo,
6358 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6363 if (TAILQ_EMPTY(&commitable_paths)) {
6364 /* No-op change; commit will be elided. */
6365 err = got_ref_delete(commit_ref, repo);
6368 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6372 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6376 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6381 logmsg = strdup(new_logmsg);
6382 if (logmsg == NULL) {
6383 err = got_error_from_errno("strdup");
6387 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6392 /* NB: commit_worktree will call free(logmsg) */
6393 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6394 NULL, worktree, got_object_commit_get_author(orig_commit),
6395 got_object_commit_get_committer(orig_commit),
6396 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6400 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6404 err = got_ref_delete(commit_ref, repo);
6408 err = update_fileindex_after_commit(worktree, &commitable_paths,
6409 *new_commit_id, fileindex, 0);
6410 sync_err = sync_fileindex(fileindex, fileindex_path);
6411 if (sync_err && err == NULL)
6414 free(fileindex_path);
6415 free(head_commit_id);
6417 got_ref_close(head_ref);
6419 free(*new_commit_id);
6420 *new_commit_id = NULL;
6425 const struct got_error *
6426 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6427 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6428 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6429 struct got_commit_object *orig_commit,
6430 struct got_object_id *orig_commit_id, struct got_repository *repo)
6432 const struct got_error *err;
6433 char *commit_ref_name;
6434 struct got_reference *commit_ref = NULL;
6435 struct got_object_id *commit_id = NULL;
6437 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6441 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6444 err = got_ref_resolve(&commit_id, repo, commit_ref);
6447 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6448 err = got_error(GOT_ERR_REBASE_COMMITID);
6452 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6453 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6456 got_ref_close(commit_ref);
6457 free(commit_ref_name);
6462 const struct got_error *
6463 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6464 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6465 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6466 struct got_commit_object *orig_commit,
6467 struct got_object_id *orig_commit_id, const char *new_logmsg,
6468 struct got_repository *repo)
6470 const struct got_error *err;
6471 char *commit_ref_name;
6472 struct got_reference *commit_ref = NULL;
6474 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6478 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6482 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6483 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6486 got_ref_close(commit_ref);
6487 free(commit_ref_name);
6491 const struct got_error *
6492 got_worktree_rebase_postpone(struct got_worktree *worktree,
6493 struct got_fileindex *fileindex)
6496 got_fileindex_free(fileindex);
6497 return lock_worktree(worktree, LOCK_SH);
6500 static const struct got_error *
6501 delete_ref(const char *name, struct got_repository *repo)
6503 const struct got_error *err;
6504 struct got_reference *ref;
6506 err = got_ref_open(&ref, repo, name, 0);
6508 if (err->code == GOT_ERR_NOT_REF)
6513 err = got_ref_delete(ref, repo);
6518 static const struct got_error *
6519 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6521 const struct got_error *err;
6522 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6523 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6525 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6528 err = delete_ref(tmp_branch_name, repo);
6532 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6535 err = delete_ref(new_base_branch_ref_name, repo);
6539 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6542 err = delete_ref(branch_ref_name, repo);
6546 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6549 err = delete_ref(commit_ref_name, repo);
6554 free(tmp_branch_name);
6555 free(new_base_branch_ref_name);
6556 free(branch_ref_name);
6557 free(commit_ref_name);
6561 const struct got_error *
6562 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6563 struct got_object_id *new_commit_id, struct got_repository *repo)
6565 const struct got_error *err;
6566 struct got_reference *ref = NULL;
6567 struct got_object_id *old_commit_id = NULL;
6568 const char *branch_name = NULL;
6569 char *new_id_str = NULL;
6570 char *refname = NULL;
6572 branch_name = got_ref_get_name(branch);
6573 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6574 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6577 err = got_object_id_str(&new_id_str, new_commit_id);
6581 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6582 new_id_str) == -1) {
6583 err = got_error_from_errno("asprintf");
6587 err = got_ref_resolve(&old_commit_id, repo, branch);
6591 err = got_ref_alloc(&ref, refname, old_commit_id);
6595 err = got_ref_write(ref, repo);
6599 free(old_commit_id);
6605 const struct got_error *
6606 got_worktree_rebase_complete(struct got_worktree *worktree,
6607 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6608 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6609 struct got_repository *repo, int create_backup)
6611 const struct got_error *err, *unlockerr, *sync_err;
6612 struct got_object_id *new_head_commit_id = NULL;
6613 char *fileindex_path = NULL;
6615 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6619 if (create_backup) {
6620 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6621 rebased_branch, new_head_commit_id, repo);
6626 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6630 err = got_ref_write(rebased_branch, repo);
6634 err = got_worktree_set_head_ref(worktree, rebased_branch);
6638 err = delete_rebase_refs(worktree, repo);
6642 err = get_fileindex_path(&fileindex_path, worktree);
6645 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6646 sync_err = sync_fileindex(fileindex, fileindex_path);
6647 if (sync_err && err == NULL)
6650 got_fileindex_free(fileindex);
6651 free(fileindex_path);
6652 free(new_head_commit_id);
6653 unlockerr = lock_worktree(worktree, LOCK_SH);
6654 if (unlockerr && err == NULL)
6659 const struct got_error *
6660 got_worktree_rebase_abort(struct got_worktree *worktree,
6661 struct got_fileindex *fileindex, struct got_repository *repo,
6662 struct got_reference *new_base_branch,
6663 got_worktree_checkout_cb progress_cb, void *progress_arg)
6665 const struct got_error *err, *unlockerr, *sync_err;
6666 struct got_reference *resolved = NULL;
6667 struct got_object_id *commit_id = NULL;
6668 char *fileindex_path = NULL;
6669 struct revert_file_args rfa;
6670 struct got_object_id *tree_id = NULL;
6672 err = lock_worktree(worktree, LOCK_EX);
6676 err = got_ref_open(&resolved, repo,
6677 got_ref_get_symref_target(new_base_branch), 0);
6681 err = got_worktree_set_head_ref(worktree, resolved);
6686 * XXX commits to the base branch could have happened while
6687 * we were busy rebasing; should we store the original commit ID
6688 * when rebase begins and read it back here?
6690 err = got_ref_resolve(&commit_id, repo, resolved);
6694 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6698 err = got_object_id_by_path(&tree_id, repo,
6699 worktree->base_commit_id, worktree->path_prefix);
6703 err = delete_rebase_refs(worktree, repo);
6707 err = get_fileindex_path(&fileindex_path, worktree);
6711 rfa.worktree = worktree;
6712 rfa.fileindex = fileindex;
6713 rfa.progress_cb = progress_cb;
6714 rfa.progress_arg = progress_arg;
6715 rfa.patch_cb = NULL;
6716 rfa.patch_arg = NULL;
6718 rfa.unlink_added_files = 0;
6719 err = worktree_status(worktree, "", fileindex, repo,
6720 revert_file, &rfa, NULL, NULL, 1, 0);
6724 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6725 repo, progress_cb, progress_arg, NULL, NULL);
6727 sync_err = sync_fileindex(fileindex, fileindex_path);
6728 if (sync_err && err == NULL)
6731 got_ref_close(resolved);
6735 got_fileindex_free(fileindex);
6736 free(fileindex_path);
6738 unlockerr = lock_worktree(worktree, LOCK_SH);
6739 if (unlockerr && err == NULL)
6744 const struct got_error *
6745 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6746 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6747 struct got_fileindex **fileindex, struct got_worktree *worktree,
6748 struct got_repository *repo)
6750 const struct got_error *err = NULL;
6751 char *tmp_branch_name = NULL;
6752 char *branch_ref_name = NULL;
6753 char *base_commit_ref_name = NULL;
6754 char *fileindex_path = NULL;
6755 struct check_rebase_ok_arg ok_arg;
6756 struct got_reference *wt_branch = NULL;
6757 struct got_reference *base_commit_ref = NULL;
6761 *base_commit_id = NULL;
6764 err = lock_worktree(worktree, LOCK_EX);
6768 err = open_fileindex(fileindex, &fileindex_path, worktree);
6772 ok_arg.worktree = worktree;
6774 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6779 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6783 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6787 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6792 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6797 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6801 err = got_ref_write(*branch_ref, repo);
6805 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6806 worktree->base_commit_id);
6809 err = got_ref_write(base_commit_ref, repo);
6812 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6813 if (*base_commit_id == NULL) {
6814 err = got_error_from_errno("got_object_id_dup");
6818 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6819 worktree->base_commit_id);
6822 err = got_ref_write(*tmp_branch, repo);
6826 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6830 free(fileindex_path);
6831 free(tmp_branch_name);
6832 free(branch_ref_name);
6833 free(base_commit_ref_name);
6835 got_ref_close(wt_branch);
6838 got_ref_close(*branch_ref);
6842 got_ref_close(*tmp_branch);
6845 free(*base_commit_id);
6847 got_fileindex_free(*fileindex);
6850 lock_worktree(worktree, LOCK_SH);
6855 const struct got_error *
6856 got_worktree_histedit_postpone(struct got_worktree *worktree,
6857 struct got_fileindex *fileindex)
6860 got_fileindex_free(fileindex);
6861 return lock_worktree(worktree, LOCK_SH);
6864 const struct got_error *
6865 got_worktree_histedit_in_progress(int *in_progress,
6866 struct got_worktree *worktree)
6868 const struct got_error *err;
6869 char *tmp_branch_name = NULL;
6871 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6875 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6876 free(tmp_branch_name);
6880 const struct got_error *
6881 got_worktree_histedit_continue(struct got_object_id **commit_id,
6882 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6883 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6884 struct got_worktree *worktree, struct got_repository *repo)
6886 const struct got_error *err;
6887 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6888 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6889 struct got_reference *commit_ref = NULL;
6890 struct got_reference *base_commit_ref = NULL;
6891 char *fileindex_path = NULL;
6892 int have_staged_files = 0;
6896 *base_commit_id = NULL;
6899 err = lock_worktree(worktree, LOCK_EX);
6903 err = open_fileindex(fileindex, &fileindex_path, worktree);
6907 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6908 &have_staged_files);
6909 if (err && err->code != GOT_ERR_CANCELLED)
6911 if (have_staged_files) {
6912 err = got_error(GOT_ERR_STAGED_PATHS);
6916 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6920 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6924 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6928 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6933 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6937 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6940 err = got_ref_resolve(commit_id, repo, commit_ref);
6944 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6947 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6951 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6955 free(commit_ref_name);
6956 free(branch_ref_name);
6957 free(fileindex_path);
6959 got_ref_close(commit_ref);
6960 if (base_commit_ref)
6961 got_ref_close(base_commit_ref);
6965 free(*base_commit_id);
6966 *base_commit_id = NULL;
6968 got_ref_close(*tmp_branch);
6972 got_fileindex_free(*fileindex);
6975 lock_worktree(worktree, LOCK_EX);
6980 static const struct got_error *
6981 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6983 const struct got_error *err;
6984 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6985 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6987 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6990 err = delete_ref(tmp_branch_name, repo);
6994 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6998 err = delete_ref(base_commit_ref_name, repo);
7002 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7005 err = delete_ref(branch_ref_name, repo);
7009 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7012 err = delete_ref(commit_ref_name, repo);
7016 free(tmp_branch_name);
7017 free(base_commit_ref_name);
7018 free(branch_ref_name);
7019 free(commit_ref_name);
7023 const struct got_error *
7024 got_worktree_histedit_abort(struct got_worktree *worktree,
7025 struct got_fileindex *fileindex, struct got_repository *repo,
7026 struct got_reference *branch, struct got_object_id *base_commit_id,
7027 got_worktree_checkout_cb progress_cb, void *progress_arg)
7029 const struct got_error *err, *unlockerr, *sync_err;
7030 struct got_reference *resolved = NULL;
7031 char *fileindex_path = NULL;
7032 struct got_object_id *tree_id = NULL;
7033 struct revert_file_args rfa;
7035 err = lock_worktree(worktree, LOCK_EX);
7039 err = got_ref_open(&resolved, repo,
7040 got_ref_get_symref_target(branch), 0);
7044 err = got_worktree_set_head_ref(worktree, resolved);
7048 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7052 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
7053 worktree->path_prefix);
7057 err = delete_histedit_refs(worktree, repo);
7061 err = get_fileindex_path(&fileindex_path, worktree);
7065 rfa.worktree = worktree;
7066 rfa.fileindex = fileindex;
7067 rfa.progress_cb = progress_cb;
7068 rfa.progress_arg = progress_arg;
7069 rfa.patch_cb = NULL;
7070 rfa.patch_arg = NULL;
7072 rfa.unlink_added_files = 0;
7073 err = worktree_status(worktree, "", fileindex, repo,
7074 revert_file, &rfa, NULL, NULL, 1, 0);
7078 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7079 repo, progress_cb, progress_arg, NULL, NULL);
7081 sync_err = sync_fileindex(fileindex, fileindex_path);
7082 if (sync_err && err == NULL)
7085 got_ref_close(resolved);
7087 free(fileindex_path);
7089 unlockerr = lock_worktree(worktree, LOCK_SH);
7090 if (unlockerr && err == NULL)
7095 const struct got_error *
7096 got_worktree_histedit_complete(struct got_worktree *worktree,
7097 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7098 struct got_reference *edited_branch, struct got_repository *repo)
7100 const struct got_error *err, *unlockerr, *sync_err;
7101 struct got_object_id *new_head_commit_id = NULL;
7102 struct got_reference *resolved = NULL;
7103 char *fileindex_path = NULL;
7105 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7109 err = got_ref_open(&resolved, repo,
7110 got_ref_get_symref_target(edited_branch), 0);
7114 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7115 resolved, new_head_commit_id, repo);
7119 err = got_ref_change_ref(resolved, new_head_commit_id);
7123 err = got_ref_write(resolved, repo);
7127 err = got_worktree_set_head_ref(worktree, resolved);
7131 err = delete_histedit_refs(worktree, repo);
7135 err = get_fileindex_path(&fileindex_path, worktree);
7138 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7139 sync_err = sync_fileindex(fileindex, fileindex_path);
7140 if (sync_err && err == NULL)
7143 got_fileindex_free(fileindex);
7144 free(fileindex_path);
7145 free(new_head_commit_id);
7146 unlockerr = lock_worktree(worktree, LOCK_SH);
7147 if (unlockerr && err == NULL)
7152 const struct got_error *
7153 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7154 struct got_object_id *commit_id, struct got_repository *repo)
7156 const struct got_error *err;
7157 char *commit_ref_name;
7159 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7163 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7167 err = delete_ref(commit_ref_name, repo);
7169 free(commit_ref_name);
7173 const struct got_error *
7174 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7175 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7176 struct got_worktree *worktree, const char *refname,
7177 struct got_repository *repo)
7179 const struct got_error *err = NULL;
7180 char *fileindex_path = NULL;
7181 struct check_rebase_ok_arg ok_arg;
7185 *base_branch_ref = NULL;
7187 err = lock_worktree(worktree, LOCK_EX);
7191 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7192 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7193 "cannot integrate a branch into itself; "
7194 "update -b or different branch name required");
7198 err = open_fileindex(fileindex, &fileindex_path, worktree);
7202 /* Preconditions are the same as for rebase. */
7203 ok_arg.worktree = worktree;
7205 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7210 err = got_ref_open(branch_ref, repo, refname, 1);
7214 err = got_ref_open(base_branch_ref, repo,
7215 got_worktree_get_head_ref_name(worktree), 1);
7219 got_ref_close(*branch_ref);
7222 if (*base_branch_ref) {
7223 got_ref_close(*base_branch_ref);
7224 *base_branch_ref = NULL;
7227 got_fileindex_free(*fileindex);
7230 lock_worktree(worktree, LOCK_SH);
7235 const struct got_error *
7236 got_worktree_integrate_continue(struct got_worktree *worktree,
7237 struct got_fileindex *fileindex, struct got_repository *repo,
7238 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7239 got_worktree_checkout_cb progress_cb, void *progress_arg,
7240 got_cancel_cb cancel_cb, void *cancel_arg)
7242 const struct got_error *err = NULL, *sync_err, *unlockerr;
7243 char *fileindex_path = NULL;
7244 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7246 err = get_fileindex_path(&fileindex_path, worktree);
7250 err = got_ref_resolve(&commit_id, repo, branch_ref);
7254 err = got_object_id_by_path(&tree_id, repo, commit_id,
7255 worktree->path_prefix);
7259 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7263 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7264 progress_cb, progress_arg, cancel_cb, cancel_arg);
7268 err = got_ref_change_ref(base_branch_ref, commit_id);
7272 err = got_ref_write(base_branch_ref, repo);
7276 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7278 sync_err = sync_fileindex(fileindex, fileindex_path);
7279 if (sync_err && err == NULL)
7283 unlockerr = got_ref_unlock(branch_ref);
7284 if (unlockerr && err == NULL)
7286 got_ref_close(branch_ref);
7288 unlockerr = got_ref_unlock(base_branch_ref);
7289 if (unlockerr && err == NULL)
7291 got_ref_close(base_branch_ref);
7293 got_fileindex_free(fileindex);
7294 free(fileindex_path);
7297 unlockerr = lock_worktree(worktree, LOCK_SH);
7298 if (unlockerr && err == NULL)
7303 const struct got_error *
7304 got_worktree_integrate_abort(struct got_worktree *worktree,
7305 struct got_fileindex *fileindex, struct got_repository *repo,
7306 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7308 const struct got_error *err = NULL, *unlockerr = NULL;
7310 got_fileindex_free(fileindex);
7312 err = lock_worktree(worktree, LOCK_SH);
7314 unlockerr = got_ref_unlock(branch_ref);
7315 if (unlockerr && err == NULL)
7317 got_ref_close(branch_ref);
7319 unlockerr = got_ref_unlock(base_branch_ref);
7320 if (unlockerr && err == NULL)
7322 got_ref_close(base_branch_ref);
7327 const struct got_error *
7328 got_worktree_merge_postpone(struct got_worktree *worktree,
7329 struct got_fileindex *fileindex)
7331 const struct got_error *err, *sync_err;
7332 char *fileindex_path = NULL;
7334 err = get_fileindex_path(&fileindex_path, worktree);
7338 sync_err = sync_fileindex(fileindex, fileindex_path);
7340 err = lock_worktree(worktree, LOCK_SH);
7341 if (sync_err && err == NULL)
7344 got_fileindex_free(fileindex);
7345 free(fileindex_path);
7349 static const struct got_error *
7350 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7352 const struct got_error *err;
7353 char *branch_refname = NULL, *commit_refname = NULL;
7355 err = get_merge_branch_ref_name(&branch_refname, worktree);
7358 err = delete_ref(branch_refname, repo);
7362 err = get_merge_commit_ref_name(&commit_refname, worktree);
7365 err = delete_ref(commit_refname, repo);
7370 free(branch_refname);
7371 free(commit_refname);
7375 struct merge_commit_msg_arg {
7376 struct got_worktree *worktree;
7377 const char *branch_name;
7380 static const struct got_error *
7381 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths, char **logmsg,
7384 struct merge_commit_msg_arg *a = arg;
7386 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7387 got_worktree_get_head_ref_name(a->worktree)) == -1)
7388 return got_error_from_errno("asprintf");
7394 const struct got_error *
7395 got_worktree_merge_branch(struct got_worktree *worktree,
7396 struct got_fileindex *fileindex,
7397 struct got_object_id *yca_commit_id,
7398 struct got_object_id *branch_tip,
7399 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7400 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7402 const struct got_error *err;
7403 char *fileindex_path = NULL;
7405 err = get_fileindex_path(&fileindex_path, worktree);
7409 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7414 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7415 branch_tip, repo, progress_cb, progress_arg,
7416 cancel_cb, cancel_arg);
7418 free(fileindex_path);
7422 const struct got_error *
7423 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7424 struct got_worktree *worktree, struct got_fileindex *fileindex,
7425 const char *author, const char *committer, int allow_bad_symlinks,
7426 struct got_object_id *branch_tip, const char *branch_name,
7427 struct got_repository *repo,
7428 got_worktree_status_cb status_cb, void *status_arg)
7431 const struct got_error *err = NULL, *sync_err;
7432 struct got_pathlist_head commitable_paths;
7433 struct collect_commitables_arg cc_arg;
7434 struct got_pathlist_entry *pe;
7435 struct got_reference *head_ref = NULL;
7436 struct got_object_id *head_commit_id = NULL;
7437 int have_staged_files = 0;
7438 struct merge_commit_msg_arg mcm_arg;
7439 char *fileindex_path = NULL;
7441 *new_commit_id = NULL;
7443 TAILQ_INIT(&commitable_paths);
7445 err = get_fileindex_path(&fileindex_path, worktree);
7449 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7453 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7457 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7458 &have_staged_files);
7459 if (err && err->code != GOT_ERR_CANCELLED)
7461 if (have_staged_files) {
7462 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7466 cc_arg.commitable_paths = &commitable_paths;
7467 cc_arg.worktree = worktree;
7468 cc_arg.fileindex = fileindex;
7470 cc_arg.have_staged_files = have_staged_files;
7471 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7472 err = worktree_status(worktree, "", fileindex, repo,
7473 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7477 if (TAILQ_EMPTY(&commitable_paths)) {
7478 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7479 "merge of %s cannot proceed", branch_name);
7483 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7484 struct got_commitable *ct = pe->data;
7485 const char *ct_path = ct->in_repo_path;
7487 while (ct_path[0] == '/')
7489 err = check_out_of_date(ct_path, ct->status,
7490 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7491 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7497 mcm_arg.worktree = worktree;
7498 mcm_arg.branch_name = branch_name;
7499 err = commit_worktree(new_commit_id, &commitable_paths,
7500 head_commit_id, branch_tip, worktree, author, committer,
7501 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7505 err = update_fileindex_after_commit(worktree, &commitable_paths,
7506 *new_commit_id, fileindex, have_staged_files);
7507 sync_err = sync_fileindex(fileindex, fileindex_path);
7508 if (sync_err && err == NULL)
7511 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7512 struct got_commitable *ct = pe->data;
7513 free_commitable(ct);
7515 got_pathlist_free(&commitable_paths);
7516 free(fileindex_path);
7520 const struct got_error *
7521 got_worktree_merge_complete(struct got_worktree *worktree,
7522 struct got_fileindex *fileindex, struct got_repository *repo)
7524 const struct got_error *err, *unlockerr, *sync_err;
7525 char *fileindex_path = NULL;
7527 err = delete_merge_refs(worktree, repo);
7531 err = get_fileindex_path(&fileindex_path, worktree);
7534 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7535 sync_err = sync_fileindex(fileindex, fileindex_path);
7536 if (sync_err && err == NULL)
7539 got_fileindex_free(fileindex);
7540 free(fileindex_path);
7541 unlockerr = lock_worktree(worktree, LOCK_SH);
7542 if (unlockerr && err == NULL)
7547 const struct got_error *
7548 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7549 struct got_repository *repo)
7551 const struct got_error *err;
7552 char *branch_refname = NULL;
7553 struct got_reference *branch_ref = NULL;
7557 err = get_merge_branch_ref_name(&branch_refname, worktree);
7560 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7561 free(branch_refname);
7563 if (err->code != GOT_ERR_NOT_REF)
7571 const struct got_error *got_worktree_merge_prepare(
7572 struct got_fileindex **fileindex, struct got_worktree *worktree,
7573 struct got_reference *branch, struct got_repository *repo)
7575 const struct got_error *err = NULL;
7576 char *fileindex_path = NULL;
7577 char *branch_refname = NULL, *commit_refname = NULL;
7578 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
7579 struct got_reference *commit_ref = NULL;
7580 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
7581 struct check_rebase_ok_arg ok_arg;
7585 err = lock_worktree(worktree, LOCK_EX);
7589 err = open_fileindex(fileindex, &fileindex_path, worktree);
7593 /* Preconditions are the same as for rebase. */
7594 ok_arg.worktree = worktree;
7596 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7601 err = get_merge_branch_ref_name(&branch_refname, worktree);
7605 err = get_merge_commit_ref_name(&commit_refname, worktree);
7609 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7614 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
7618 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
7619 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
7623 err = got_ref_resolve(&branch_tip, repo, branch);
7627 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
7630 err = got_ref_write(branch_ref, repo);
7634 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
7637 err = got_ref_write(commit_ref, repo);
7642 free(branch_refname);
7643 free(commit_refname);
7644 free(fileindex_path);
7646 got_ref_close(branch_ref);
7648 got_ref_close(commit_ref);
7650 got_ref_close(wt_branch);
7651 free(wt_branch_tip);
7654 got_fileindex_free(*fileindex);
7657 lock_worktree(worktree, LOCK_SH);
7662 const struct got_error *
7663 got_worktree_merge_continue(char **branch_name,
7664 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
7665 struct got_worktree *worktree, struct got_repository *repo)
7667 const struct got_error *err;
7668 char *commit_refname = NULL, *branch_refname = NULL;
7669 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
7670 char *fileindex_path = NULL;
7671 int have_staged_files = 0;
7673 *branch_name = NULL;
7677 err = lock_worktree(worktree, LOCK_EX);
7681 err = open_fileindex(fileindex, &fileindex_path, worktree);
7685 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7686 &have_staged_files);
7687 if (err && err->code != GOT_ERR_CANCELLED)
7689 if (have_staged_files) {
7690 err = got_error(GOT_ERR_STAGED_PATHS);
7694 err = get_merge_branch_ref_name(&branch_refname, worktree);
7698 err = get_merge_commit_ref_name(&commit_refname, worktree);
7702 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7706 if (!got_ref_is_symbolic(branch_ref)) {
7707 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7708 "%s is not a symbolic reference",
7709 got_ref_get_name(branch_ref));
7712 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
7713 if (*branch_name == NULL) {
7714 err = got_error_from_errno("strdup");
7718 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
7722 err = got_ref_resolve(branch_tip, repo, commit_ref);
7726 free(commit_refname);
7727 free(branch_refname);
7728 free(fileindex_path);
7730 got_ref_close(commit_ref);
7732 got_ref_close(branch_ref);
7736 *branch_name = NULL;
7741 got_fileindex_free(*fileindex);
7744 lock_worktree(worktree, LOCK_SH);
7749 const struct got_error *
7750 got_worktree_merge_abort(struct got_worktree *worktree,
7751 struct got_fileindex *fileindex, struct got_repository *repo,
7752 got_worktree_checkout_cb progress_cb, void *progress_arg)
7754 const struct got_error *err, *unlockerr, *sync_err;
7755 struct got_object_id *commit_id = NULL;
7756 char *fileindex_path = NULL;
7757 struct revert_file_args rfa;
7758 struct got_object_id *tree_id = NULL;
7760 err = got_object_id_by_path(&tree_id, repo,
7761 worktree->base_commit_id, worktree->path_prefix);
7765 err = delete_merge_refs(worktree, repo);
7769 err = get_fileindex_path(&fileindex_path, worktree);
7773 rfa.worktree = worktree;
7774 rfa.fileindex = fileindex;
7775 rfa.progress_cb = progress_cb;
7776 rfa.progress_arg = progress_arg;
7777 rfa.patch_cb = NULL;
7778 rfa.patch_arg = NULL;
7780 rfa.unlink_added_files = 1;
7781 err = worktree_status(worktree, "", fileindex, repo,
7782 revert_file, &rfa, NULL, NULL, 1, 0);
7786 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7787 repo, progress_cb, progress_arg, NULL, NULL);
7789 sync_err = sync_fileindex(fileindex, fileindex_path);
7790 if (sync_err && err == NULL)
7796 got_fileindex_free(fileindex);
7797 free(fileindex_path);
7799 unlockerr = lock_worktree(worktree, LOCK_SH);
7800 if (unlockerr && err == NULL)
7805 struct check_stage_ok_arg {
7806 struct got_object_id *head_commit_id;
7807 struct got_worktree *worktree;
7808 struct got_fileindex *fileindex;
7809 struct got_repository *repo;
7813 const struct got_error *
7814 check_stage_ok(void *arg, unsigned char status,
7815 unsigned char staged_status, const char *relpath,
7816 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7817 struct got_object_id *commit_id, int dirfd, const char *de_name)
7819 struct check_stage_ok_arg *a = arg;
7820 const struct got_error *err = NULL;
7821 struct got_fileindex_entry *ie;
7822 struct got_object_id base_commit_id;
7823 struct got_object_id *base_commit_idp = NULL;
7824 char *in_repo_path = NULL, *p;
7826 if (status == GOT_STATUS_UNVERSIONED ||
7827 status == GOT_STATUS_NO_CHANGE)
7829 if (status == GOT_STATUS_NONEXISTENT)
7830 return got_error_set_errno(ENOENT, relpath);
7832 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7834 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7836 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7837 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7839 return got_error_from_errno("asprintf");
7841 if (got_fileindex_entry_has_commit(ie)) {
7842 memcpy(base_commit_id.sha1, ie->commit_sha1,
7843 SHA1_DIGEST_LENGTH);
7844 base_commit_idp = &base_commit_id;
7847 if (status == GOT_STATUS_CONFLICT) {
7848 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7850 } else if (status != GOT_STATUS_ADD &&
7851 status != GOT_STATUS_MODIFY &&
7852 status != GOT_STATUS_DELETE) {
7853 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7857 a->have_changes = 1;
7862 err = check_out_of_date(p, status, staged_status,
7863 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7864 GOT_ERR_STAGE_OUT_OF_DATE);
7870 struct stage_path_arg {
7871 struct got_worktree *worktree;
7872 struct got_fileindex *fileindex;
7873 struct got_repository *repo;
7874 got_worktree_status_cb status_cb;
7876 got_worktree_patch_cb patch_cb;
7878 int staged_something;
7879 int allow_bad_symlinks;
7882 static const struct got_error *
7883 stage_path(void *arg, unsigned char status,
7884 unsigned char staged_status, const char *relpath,
7885 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7886 struct got_object_id *commit_id, int dirfd, const char *de_name)
7888 struct stage_path_arg *a = arg;
7889 const struct got_error *err = NULL;
7890 struct got_fileindex_entry *ie;
7891 char *ondisk_path = NULL, *path_content = NULL;
7893 struct got_object_id *new_staged_blob_id = NULL;
7896 if (status == GOT_STATUS_UNVERSIONED)
7899 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7901 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7903 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7905 return got_error_from_errno("asprintf");
7908 case GOT_STATUS_ADD:
7909 case GOT_STATUS_MODIFY:
7910 /* XXX could sb.st_mode be passed in by our caller? */
7911 if (lstat(ondisk_path, &sb) == -1) {
7912 err = got_error_from_errno2("lstat", ondisk_path);
7916 if (status == GOT_STATUS_ADD) {
7917 int choice = GOT_PATCH_CHOICE_NONE;
7918 err = (*a->patch_cb)(&choice, a->patch_arg,
7919 status, ie->path, NULL, 1, 1);
7922 if (choice != GOT_PATCH_CHOICE_YES)
7925 err = create_patched_content(&path_content, 0,
7926 staged_blob_id ? staged_blob_id : blob_id,
7927 ondisk_path, dirfd, de_name, ie->path,
7928 a->repo, a->patch_cb, a->patch_arg);
7929 if (err || path_content == NULL)
7933 err = got_object_blob_create(&new_staged_blob_id,
7934 path_content ? path_content : ondisk_path, a->repo);
7937 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7938 SHA1_DIGEST_LENGTH);
7939 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7940 stage = GOT_FILEIDX_STAGE_ADD;
7942 stage = GOT_FILEIDX_STAGE_MODIFY;
7943 got_fileindex_entry_stage_set(ie, stage);
7944 if (S_ISLNK(sb.st_mode)) {
7945 int is_bad_symlink = 0;
7946 if (!a->allow_bad_symlinks) {
7947 char target_path[PATH_MAX];
7949 target_len = readlink(ondisk_path, target_path,
7950 sizeof(target_path));
7951 if (target_len == -1) {
7952 err = got_error_from_errno2("readlink",
7956 err = is_bad_symlink_target(&is_bad_symlink,
7957 target_path, target_len, ondisk_path,
7958 a->worktree->root_path);
7961 if (is_bad_symlink) {
7962 err = got_error_path(ondisk_path,
7963 GOT_ERR_BAD_SYMLINK);
7968 got_fileindex_entry_staged_filetype_set(ie,
7969 GOT_FILEIDX_MODE_BAD_SYMLINK);
7971 got_fileindex_entry_staged_filetype_set(ie,
7972 GOT_FILEIDX_MODE_SYMLINK);
7974 got_fileindex_entry_staged_filetype_set(ie,
7975 GOT_FILEIDX_MODE_REGULAR_FILE);
7977 a->staged_something = 1;
7978 if (a->status_cb == NULL)
7980 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7981 get_staged_status(ie), relpath, blob_id,
7982 new_staged_blob_id, NULL, dirfd, de_name);
7984 case GOT_STATUS_DELETE:
7985 if (staged_status == GOT_STATUS_DELETE)
7988 int choice = GOT_PATCH_CHOICE_NONE;
7989 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7990 ie->path, NULL, 1, 1);
7993 if (choice == GOT_PATCH_CHOICE_NO)
7995 if (choice != GOT_PATCH_CHOICE_YES) {
7996 err = got_error(GOT_ERR_PATCH_CHOICE);
8000 stage = GOT_FILEIDX_STAGE_DELETE;
8001 got_fileindex_entry_stage_set(ie, stage);
8002 a->staged_something = 1;
8003 if (a->status_cb == NULL)
8005 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8006 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8009 case GOT_STATUS_NO_CHANGE:
8011 case GOT_STATUS_CONFLICT:
8012 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8014 case GOT_STATUS_NONEXISTENT:
8015 err = got_error_set_errno(ENOENT, relpath);
8018 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8022 if (path_content && unlink(path_content) == -1 && err == NULL)
8023 err = got_error_from_errno2("unlink", path_content);
8026 free(new_staged_blob_id);
8030 const struct got_error *
8031 got_worktree_stage(struct got_worktree *worktree,
8032 struct got_pathlist_head *paths,
8033 got_worktree_status_cb status_cb, void *status_arg,
8034 got_worktree_patch_cb patch_cb, void *patch_arg,
8035 int allow_bad_symlinks, struct got_repository *repo)
8037 const struct got_error *err = NULL, *sync_err, *unlockerr;
8038 struct got_pathlist_entry *pe;
8039 struct got_fileindex *fileindex = NULL;
8040 char *fileindex_path = NULL;
8041 struct got_reference *head_ref = NULL;
8042 struct got_object_id *head_commit_id = NULL;
8043 struct check_stage_ok_arg oka;
8044 struct stage_path_arg spa;
8046 err = lock_worktree(worktree, LOCK_EX);
8050 err = got_ref_open(&head_ref, repo,
8051 got_worktree_get_head_ref_name(worktree), 0);
8054 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8057 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8061 /* Check pre-conditions before staging anything. */
8062 oka.head_commit_id = head_commit_id;
8063 oka.worktree = worktree;
8064 oka.fileindex = fileindex;
8066 oka.have_changes = 0;
8067 TAILQ_FOREACH(pe, paths, entry) {
8068 err = worktree_status(worktree, pe->path, fileindex, repo,
8069 check_stage_ok, &oka, NULL, NULL, 1, 0);
8073 if (!oka.have_changes) {
8074 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8078 spa.worktree = worktree;
8079 spa.fileindex = fileindex;
8081 spa.patch_cb = patch_cb;
8082 spa.patch_arg = patch_arg;
8083 spa.status_cb = status_cb;
8084 spa.status_arg = status_arg;
8085 spa.staged_something = 0;
8086 spa.allow_bad_symlinks = allow_bad_symlinks;
8087 TAILQ_FOREACH(pe, paths, entry) {
8088 err = worktree_status(worktree, pe->path, fileindex, repo,
8089 stage_path, &spa, NULL, NULL, 1, 0);
8093 if (!spa.staged_something) {
8094 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8098 sync_err = sync_fileindex(fileindex, fileindex_path);
8099 if (sync_err && err == NULL)
8103 got_ref_close(head_ref);
8104 free(head_commit_id);
8105 free(fileindex_path);
8107 got_fileindex_free(fileindex);
8108 unlockerr = lock_worktree(worktree, LOCK_SH);
8109 if (unlockerr && err == NULL)
8114 struct unstage_path_arg {
8115 struct got_worktree *worktree;
8116 struct got_fileindex *fileindex;
8117 struct got_repository *repo;
8118 got_worktree_checkout_cb progress_cb;
8120 got_worktree_patch_cb patch_cb;
8124 static const struct got_error *
8125 create_unstaged_content(char **path_unstaged_content,
8126 char **path_new_staged_content, struct got_object_id *blob_id,
8127 struct got_object_id *staged_blob_id, const char *relpath,
8128 struct got_repository *repo,
8129 got_worktree_patch_cb patch_cb, void *patch_arg)
8131 const struct got_error *err, *free_err;
8132 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8133 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8134 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8135 struct got_diffreg_result *diffreg_result = NULL;
8136 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8137 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8139 *path_unstaged_content = NULL;
8140 *path_new_staged_content = NULL;
8142 err = got_object_id_str(&label1, blob_id);
8145 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
8149 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
8153 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8157 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
8161 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
8165 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8169 err = got_diff_files(&diffreg_result, f1, label1, f2,
8170 path2, 3, 0, 1, NULL);
8174 err = got_opentemp_named(path_unstaged_content, &outfile,
8175 "got-unstaged-content");
8178 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8179 "got-new-staged-content");
8183 if (fseek(f1, 0L, SEEK_SET) == -1) {
8184 err = got_ferror(f1, GOT_ERR_IO);
8187 if (fseek(f2, 0L, SEEK_SET) == -1) {
8188 err = got_ferror(f2, GOT_ERR_IO);
8191 /* Count the number of actual changes in the diff result. */
8192 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8193 struct diff_chunk_context cc = {};
8194 diff_chunk_context_load_change(&cc, &nchunks_used,
8195 diffreg_result->result, n, 0);
8198 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8200 err = apply_or_reject_change(&choice, &nchunks_used,
8201 diffreg_result->result, n, relpath, f1, f2,
8202 &line_cur1, &line_cur2,
8203 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8206 if (choice == GOT_PATCH_CHOICE_YES)
8209 have_rejected_content = 1;
8210 if (choice == GOT_PATCH_CHOICE_QUIT)
8213 if (have_content || have_rejected_content)
8214 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8215 outfile, rejectfile);
8219 got_object_blob_close(blob);
8221 got_object_blob_close(staged_blob);
8222 free_err = got_diffreg_result_free(diffreg_result);
8223 if (free_err && err == NULL)
8225 if (f1 && fclose(f1) == EOF && err == NULL)
8226 err = got_error_from_errno2("fclose", path1);
8227 if (f2 && fclose(f2) == EOF && err == NULL)
8228 err = got_error_from_errno2("fclose", path2);
8229 if (outfile && fclose(outfile) == EOF && err == NULL)
8230 err = got_error_from_errno2("fclose", *path_unstaged_content);
8231 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8232 err = got_error_from_errno2("fclose", *path_new_staged_content);
8233 if (path1 && unlink(path1) == -1 && err == NULL)
8234 err = got_error_from_errno2("unlink", path1);
8235 if (path2 && unlink(path2) == -1 && err == NULL)
8236 err = got_error_from_errno2("unlink", path2);
8237 if (err || !have_content) {
8238 if (*path_unstaged_content &&
8239 unlink(*path_unstaged_content) == -1 && err == NULL)
8240 err = got_error_from_errno2("unlink",
8241 *path_unstaged_content);
8242 free(*path_unstaged_content);
8243 *path_unstaged_content = NULL;
8245 if (err || !have_content || !have_rejected_content) {
8246 if (*path_new_staged_content &&
8247 unlink(*path_new_staged_content) == -1 && err == NULL)
8248 err = got_error_from_errno2("unlink",
8249 *path_new_staged_content);
8250 free(*path_new_staged_content);
8251 *path_new_staged_content = NULL;
8258 static const struct got_error *
8259 unstage_hunks(struct got_object_id *staged_blob_id,
8260 struct got_blob_object *blob_base,
8261 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8262 const char *ondisk_path, const char *label_orig,
8263 struct got_worktree *worktree, struct got_repository *repo,
8264 got_worktree_patch_cb patch_cb, void *patch_arg,
8265 got_worktree_checkout_cb progress_cb, void *progress_arg)
8267 const struct got_error *err = NULL;
8268 char *path_unstaged_content = NULL;
8269 char *path_new_staged_content = NULL;
8270 char *parent = NULL, *base_path = NULL;
8271 char *blob_base_path = NULL;
8272 struct got_object_id *new_staged_blob_id = NULL;
8273 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8276 err = create_unstaged_content(&path_unstaged_content,
8277 &path_new_staged_content, blob_id, staged_blob_id,
8278 ie->path, repo, patch_cb, patch_arg);
8282 if (path_unstaged_content == NULL)
8285 if (path_new_staged_content) {
8286 err = got_object_blob_create(&new_staged_blob_id,
8287 path_new_staged_content, repo);
8292 f = fopen(path_unstaged_content, "re");
8294 err = got_error_from_errno2("fopen",
8295 path_unstaged_content);
8298 if (fstat(fileno(f), &sb) == -1) {
8299 err = got_error_from_errno2("fstat", path_unstaged_content);
8302 if (got_fileindex_entry_staged_filetype_get(ie) ==
8303 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8304 char link_target[PATH_MAX];
8306 r = fread(link_target, 1, sizeof(link_target), f);
8307 if (r == 0 && ferror(f)) {
8308 err = got_error_from_errno("fread");
8311 if (r >= sizeof(link_target)) { /* should not happen */
8312 err = got_error(GOT_ERR_NO_SPACE);
8315 link_target[r] = '\0';
8316 err = merge_symlink(worktree, blob_base,
8317 ondisk_path, ie->path, label_orig, link_target,
8318 worktree->base_commit_id, repo, progress_cb,
8321 int local_changes_subsumed;
8323 err = got_path_dirname(&parent, ondisk_path);
8327 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8329 err = got_error_from_errno("asprintf");
8334 err = got_opentemp_named(&blob_base_path, &f_base, base_path);
8337 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8343 * In order the run a 3-way merge with a symlink we copy the symlink's
8344 * target path into a temporary file and use that file with diff3.
8346 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8347 err = dump_symlink_target_path_to_file(&f_deriv2,
8353 fd = open(ondisk_path,
8354 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8356 err = got_error_from_errno2("open", ondisk_path);
8359 f_deriv2 = fdopen(fd, "r");
8360 if (f_deriv2 == NULL) {
8361 err = got_error_from_errno2("fdopen", ondisk_path);
8367 err = merge_file(&local_changes_subsumed, worktree,
8368 f_base, f, f_deriv2, ondisk_path, ie->path,
8369 got_fileindex_perms_to_st(ie),
8370 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8371 repo, progress_cb, progress_arg);
8376 if (new_staged_blob_id) {
8377 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8378 SHA1_DIGEST_LENGTH);
8380 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8381 got_fileindex_entry_staged_filetype_set(ie, 0);
8384 free(new_staged_blob_id);
8385 if (path_unstaged_content &&
8386 unlink(path_unstaged_content) == -1 && err == NULL)
8387 err = got_error_from_errno2("unlink", path_unstaged_content);
8388 if (path_new_staged_content &&
8389 unlink(path_new_staged_content) == -1 && err == NULL)
8390 err = got_error_from_errno2("unlink", path_new_staged_content);
8391 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8392 err = got_error_from_errno2("unlink", blob_base_path);
8393 if (f_base && fclose(f_base) == EOF && err == NULL)
8394 err = got_error_from_errno2("fclose", path_unstaged_content);
8395 if (f && fclose(f) == EOF && err == NULL)
8396 err = got_error_from_errno2("fclose", path_unstaged_content);
8397 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8398 err = got_error_from_errno2("fclose", ondisk_path);
8399 free(path_unstaged_content);
8400 free(path_new_staged_content);
8401 free(blob_base_path);
8407 static const struct got_error *
8408 unstage_path(void *arg, unsigned char status,
8409 unsigned char staged_status, const char *relpath,
8410 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8411 struct got_object_id *commit_id, int dirfd, const char *de_name)
8413 const struct got_error *err = NULL;
8414 struct unstage_path_arg *a = arg;
8415 struct got_fileindex_entry *ie;
8416 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8417 char *ondisk_path = NULL;
8418 char *id_str = NULL, *label_orig = NULL;
8419 int local_changes_subsumed;
8422 if (staged_status != GOT_STATUS_ADD &&
8423 staged_status != GOT_STATUS_MODIFY &&
8424 staged_status != GOT_STATUS_DELETE)
8427 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8429 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8431 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8433 return got_error_from_errno("asprintf");
8435 err = got_object_id_str(&id_str,
8436 commit_id ? commit_id : a->worktree->base_commit_id);
8439 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8441 err = got_error_from_errno("asprintf");
8445 switch (staged_status) {
8446 case GOT_STATUS_MODIFY:
8447 err = got_object_open_as_blob(&blob_base, a->repo,
8452 case GOT_STATUS_ADD:
8454 if (staged_status == GOT_STATUS_ADD) {
8455 int choice = GOT_PATCH_CHOICE_NONE;
8456 err = (*a->patch_cb)(&choice, a->patch_arg,
8457 staged_status, ie->path, NULL, 1, 1);
8460 if (choice != GOT_PATCH_CHOICE_YES)
8463 err = unstage_hunks(staged_blob_id,
8464 blob_base, blob_id, ie, ondisk_path,
8465 label_orig, a->worktree, a->repo,
8466 a->patch_cb, a->patch_arg,
8467 a->progress_cb, a->progress_arg);
8468 break; /* Done with this file. */
8471 err = got_object_open_as_blob(&blob_staged, a->repo,
8472 staged_blob_id, 8192);
8475 switch (got_fileindex_entry_staged_filetype_get(ie)) {
8476 case GOT_FILEIDX_MODE_BAD_SYMLINK:
8477 case GOT_FILEIDX_MODE_REGULAR_FILE:
8478 err = merge_blob(&local_changes_subsumed, a->worktree,
8479 blob_base, ondisk_path, relpath,
8480 got_fileindex_perms_to_st(ie), label_orig,
8481 blob_staged, commit_id ? commit_id :
8482 a->worktree->base_commit_id, a->repo,
8483 a->progress_cb, a->progress_arg);
8485 case GOT_FILEIDX_MODE_SYMLINK:
8486 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8487 char *staged_target;
8488 err = got_object_blob_read_to_str(
8489 &staged_target, blob_staged);
8492 err = merge_symlink(a->worktree, blob_base,
8493 ondisk_path, relpath, label_orig,
8494 staged_target, commit_id ? commit_id :
8495 a->worktree->base_commit_id,
8496 a->repo, a->progress_cb, a->progress_arg);
8497 free(staged_target);
8499 err = merge_blob(&local_changes_subsumed,
8500 a->worktree, blob_base, ondisk_path,
8501 relpath, got_fileindex_perms_to_st(ie),
8502 label_orig, blob_staged,
8503 commit_id ? commit_id :
8504 a->worktree->base_commit_id, a->repo,
8505 a->progress_cb, a->progress_arg);
8509 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8513 got_fileindex_entry_stage_set(ie,
8514 GOT_FILEIDX_STAGE_NONE);
8515 got_fileindex_entry_staged_filetype_set(ie, 0);
8518 case GOT_STATUS_DELETE:
8520 int choice = GOT_PATCH_CHOICE_NONE;
8521 err = (*a->patch_cb)(&choice, a->patch_arg,
8522 staged_status, ie->path, NULL, 1, 1);
8525 if (choice == GOT_PATCH_CHOICE_NO)
8527 if (choice != GOT_PATCH_CHOICE_YES) {
8528 err = got_error(GOT_ERR_PATCH_CHOICE);
8532 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8533 got_fileindex_entry_staged_filetype_set(ie, 0);
8534 err = get_file_status(&status, &sb, ie, ondisk_path,
8535 dirfd, de_name, a->repo);
8538 err = (*a->progress_cb)(a->progress_arg, status, relpath);
8544 got_object_blob_close(blob_base);
8546 got_object_blob_close(blob_staged);
8552 const struct got_error *
8553 got_worktree_unstage(struct got_worktree *worktree,
8554 struct got_pathlist_head *paths,
8555 got_worktree_checkout_cb progress_cb, void *progress_arg,
8556 got_worktree_patch_cb patch_cb, void *patch_arg,
8557 struct got_repository *repo)
8559 const struct got_error *err = NULL, *sync_err, *unlockerr;
8560 struct got_pathlist_entry *pe;
8561 struct got_fileindex *fileindex = NULL;
8562 char *fileindex_path = NULL;
8563 struct unstage_path_arg upa;
8565 err = lock_worktree(worktree, LOCK_EX);
8569 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8573 upa.worktree = worktree;
8574 upa.fileindex = fileindex;
8576 upa.progress_cb = progress_cb;
8577 upa.progress_arg = progress_arg;
8578 upa.patch_cb = patch_cb;
8579 upa.patch_arg = patch_arg;
8580 TAILQ_FOREACH(pe, paths, entry) {
8581 err = worktree_status(worktree, pe->path, fileindex, repo,
8582 unstage_path, &upa, NULL, NULL, 1, 0);
8587 sync_err = sync_fileindex(fileindex, fileindex_path);
8588 if (sync_err && err == NULL)
8591 free(fileindex_path);
8593 got_fileindex_free(fileindex);
8594 unlockerr = lock_worktree(worktree, LOCK_SH);
8595 if (unlockerr && err == NULL)
8600 struct report_file_info_arg {
8601 struct got_worktree *worktree;
8602 got_worktree_path_info_cb info_cb;
8604 struct got_pathlist_head *paths;
8605 got_cancel_cb cancel_cb;
8609 static const struct got_error *
8610 report_file_info(void *arg, struct got_fileindex_entry *ie)
8612 struct report_file_info_arg *a = arg;
8613 struct got_pathlist_entry *pe;
8614 struct got_object_id blob_id, staged_blob_id, commit_id;
8615 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8616 struct got_object_id *commit_idp = NULL;
8619 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8620 return got_error(GOT_ERR_CANCELLED);
8622 TAILQ_FOREACH(pe, a->paths, entry) {
8623 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8624 got_path_is_child(ie->path, pe->path, pe->path_len))
8627 if (pe == NULL) /* not found */
8630 if (got_fileindex_entry_has_blob(ie)) {
8631 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8632 blob_idp = &blob_id;
8634 stage = got_fileindex_entry_stage_get(ie);
8635 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8636 stage == GOT_FILEIDX_STAGE_ADD) {
8637 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8638 SHA1_DIGEST_LENGTH);
8639 staged_blob_idp = &staged_blob_id;
8642 if (got_fileindex_entry_has_commit(ie)) {
8643 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8644 commit_idp = &commit_id;
8647 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8648 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8651 const struct got_error *
8652 got_worktree_path_info(struct got_worktree *worktree,
8653 struct got_pathlist_head *paths,
8654 got_worktree_path_info_cb info_cb, void *info_arg,
8655 got_cancel_cb cancel_cb, void *cancel_arg)
8658 const struct got_error *err = NULL, *unlockerr;
8659 struct got_fileindex *fileindex = NULL;
8660 char *fileindex_path = NULL;
8661 struct report_file_info_arg arg;
8663 err = lock_worktree(worktree, LOCK_SH);
8667 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8671 arg.worktree = worktree;
8672 arg.info_cb = info_cb;
8673 arg.info_arg = info_arg;
8675 arg.cancel_cb = cancel_cb;
8676 arg.cancel_arg = cancel_arg;
8677 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8680 free(fileindex_path);
8682 got_fileindex_free(fileindex);
8683 unlockerr = lock_worktree(worktree, LOCK_UN);
8684 if (unlockerr && err == NULL)