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);
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);
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 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,
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);
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);
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, "rb");
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, O_RDONLY | O_NOFOLLOW);
2819 err = got_error_from_errno2("open",
2823 f_deriv = fdopen(fd, "r");
2824 if (f_deriv == NULL) {
2825 err = got_error_from_errno2("fdopen",
2830 err = got_object_id_str(&id_str, a->commit_id2);
2833 if (asprintf(&label_deriv2, "%s: commit %s",
2834 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2835 err = got_error_from_errno("asprintf");
2838 err = merge_file(&local_changes_subsumed, a->worktree,
2839 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2840 sb.st_mode, a->label_orig, NULL, label_deriv2,
2841 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2842 a->progress_cb, a->progress_arg);
2845 ie = got_fileindex_entry_get(a->fileindex, path1,
2848 return (*a->progress_cb)(a->progress_arg,
2849 GOT_STATUS_MISSING, path1);
2851 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2853 return got_error_from_errno("asprintf");
2855 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2861 case GOT_STATUS_NO_CHANGE:
2862 err = (*a->progress_cb)(a->progress_arg,
2863 GOT_STATUS_DELETE, path1);
2866 err = remove_ondisk_file(a->worktree->root_path, path1);
2870 got_fileindex_entry_mark_deleted_from_disk(ie);
2872 case GOT_STATUS_DELETE:
2873 case GOT_STATUS_MISSING:
2874 err = (*a->progress_cb)(a->progress_arg,
2875 GOT_STATUS_DELETE, path1);
2879 got_fileindex_entry_mark_deleted_from_disk(ie);
2881 case GOT_STATUS_ADD: {
2882 struct got_object_id *id;
2885 * Delete the added file only if its content already
2886 * exists in the repository.
2888 err = got_object_blob_file_create(&id, &blob1_f, path1);
2891 if (got_object_id_cmp(id, id1) == 0) {
2892 err = (*a->progress_cb)(a->progress_arg,
2893 GOT_STATUS_DELETE, path1);
2896 err = remove_ondisk_file(a->worktree->root_path,
2901 got_fileindex_entry_remove(a->fileindex,
2904 err = (*a->progress_cb)(a->progress_arg,
2905 GOT_STATUS_CANNOT_DELETE, path1);
2907 if (fclose(blob1_f) == EOF && err == NULL)
2908 err = got_error_from_errno("fclose");
2914 case GOT_STATUS_MODIFY:
2915 case GOT_STATUS_CONFLICT:
2916 err = (*a->progress_cb)(a->progress_arg,
2917 GOT_STATUS_CANNOT_DELETE, path1);
2921 case GOT_STATUS_OBSTRUCTED:
2922 err = (*a->progress_cb)(a->progress_arg, status, path1);
2930 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2932 return got_error_from_errno("asprintf");
2933 ie = got_fileindex_entry_get(a->fileindex, path2,
2936 err = get_file_status(&status, &sb, ie, ondisk_path,
2940 if (status != GOT_STATUS_NO_CHANGE &&
2941 status != GOT_STATUS_MODIFY &&
2942 status != GOT_STATUS_CONFLICT &&
2943 status != GOT_STATUS_ADD) {
2944 err = (*a->progress_cb)(a->progress_arg,
2948 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2950 err = got_object_blob_read_to_str(&link_target2,
2954 err = merge_symlink(a->worktree, NULL,
2955 ondisk_path, path2, a->label_orig,
2956 link_target2, a->commit_id2, repo,
2957 a->progress_cb, a->progress_arg);
2959 } else if (S_ISREG(sb.st_mode)) {
2960 err = merge_blob(&local_changes_subsumed,
2961 a->worktree, NULL, ondisk_path, path2,
2962 sb.st_mode, a->label_orig, blob2,
2963 a->commit_id2, repo, a->progress_cb,
2966 err = got_error_path(ondisk_path,
2967 GOT_ERR_FILE_OBSTRUCTED);
2971 if (status == GOT_STATUS_DELETE) {
2972 err = got_fileindex_entry_update(ie,
2973 a->worktree->root_fd, path2, blob2->id.sha1,
2974 a->worktree->base_commit_id->sha1, 0);
2979 int is_bad_symlink = 0;
2980 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2981 if (S_ISLNK(mode2)) {
2982 err = install_symlink(&is_bad_symlink,
2983 a->worktree, ondisk_path, path2, blob2, 0,
2984 0, 1, a->allow_bad_symlinks, repo,
2985 a->progress_cb, a->progress_arg);
2987 err = install_blob(a->worktree, ondisk_path, path2,
2988 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2989 a->progress_cb, a->progress_arg);
2993 err = got_fileindex_entry_alloc(&ie, path2);
2996 err = got_fileindex_entry_update(ie,
2997 a->worktree->root_fd, path2, NULL, NULL, 1);
2999 got_fileindex_entry_free(ie);
3002 err = got_fileindex_entry_add(a->fileindex, ie);
3004 got_fileindex_entry_free(ie);
3007 if (is_bad_symlink) {
3008 got_fileindex_entry_filetype_set(ie,
3009 GOT_FILEIDX_MODE_BAD_SYMLINK);
3014 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3015 err = got_error_from_errno("fclose");
3016 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3017 err = got_error_from_errno("fclose");
3018 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3019 err = got_error_from_errno("fclose");
3026 static const struct got_error *
3027 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3029 struct got_worktree *worktree = arg;
3031 /* Reject merges into a work tree with mixed base commits. */
3032 if (got_fileindex_entry_has_commit(ie) &&
3033 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3034 SHA1_DIGEST_LENGTH) != 0)
3035 return got_error(GOT_ERR_MIXED_COMMITS);
3040 struct check_merge_conflicts_arg {
3041 struct got_worktree *worktree;
3042 struct got_fileindex *fileindex;
3043 struct got_repository *repo;
3046 static const struct got_error *
3047 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3048 struct got_blob_object *blob2, struct got_object_id *id1,
3049 struct got_object_id *id2, const char *path1, const char *path2,
3050 mode_t mode1, mode_t mode2, struct got_repository *repo)
3052 const struct got_error *err = NULL;
3053 struct check_merge_conflicts_arg *a = arg;
3054 unsigned char status;
3056 struct got_fileindex_entry *ie;
3057 const char *path = path2 ? path2 : path1;
3058 struct got_object_id *id = id2 ? id2 : id1;
3064 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3068 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3070 return got_error_from_errno("asprintf");
3072 /* Reject merges into a work tree with conflicted files. */
3073 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3077 if (status == GOT_STATUS_CONFLICT)
3078 return got_error(GOT_ERR_CONFLICTS);
3083 static const struct got_error *
3084 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3085 const char *fileindex_path, struct got_object_id *commit_id1,
3086 struct got_object_id *commit_id2, struct got_repository *repo,
3087 got_worktree_checkout_cb progress_cb, void *progress_arg,
3088 got_cancel_cb cancel_cb, void *cancel_arg)
3090 const struct got_error *err = NULL, *sync_err;
3091 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3092 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3093 struct check_merge_conflicts_arg cmc_arg;
3094 struct merge_file_cb_arg arg;
3095 char *label_orig = NULL;
3098 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3099 worktree->path_prefix);
3100 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3106 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3110 err = got_object_id_str(&id_str, commit_id1);
3114 if (asprintf(&label_orig, "%s: commit %s",
3115 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3116 err = got_error_from_errno("asprintf");
3123 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3124 worktree->path_prefix);
3128 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3132 cmc_arg.worktree = worktree;
3133 cmc_arg.fileindex = fileindex;
3134 cmc_arg.repo = repo;
3135 err = got_diff_tree(tree1, tree2, "", "", repo,
3136 check_merge_conflicts, &cmc_arg, 0);
3140 arg.worktree = worktree;
3141 arg.fileindex = fileindex;
3142 arg.progress_cb = progress_cb;
3143 arg.progress_arg = progress_arg;
3144 arg.cancel_cb = cancel_cb;
3145 arg.cancel_arg = cancel_arg;
3146 arg.label_orig = label_orig;
3147 arg.commit_id2 = commit_id2;
3148 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3149 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3150 sync_err = sync_fileindex(fileindex, fileindex_path);
3151 if (sync_err && err == NULL)
3155 got_object_tree_close(tree1);
3157 got_object_tree_close(tree2);
3162 const struct got_error *
3163 got_worktree_merge_files(struct got_worktree *worktree,
3164 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3165 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3166 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3168 const struct got_error *err, *unlockerr;
3169 char *fileindex_path = NULL;
3170 struct got_fileindex *fileindex = NULL;
3172 err = lock_worktree(worktree, LOCK_EX);
3176 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3180 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3185 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3186 commit_id2, repo, progress_cb, progress_arg,
3187 cancel_cb, cancel_arg);
3190 got_fileindex_free(fileindex);
3191 free(fileindex_path);
3192 unlockerr = lock_worktree(worktree, LOCK_SH);
3193 if (unlockerr && err == NULL)
3198 struct diff_dir_cb_arg {
3199 struct got_fileindex *fileindex;
3200 struct got_worktree *worktree;
3201 const char *status_path;
3202 size_t status_path_len;
3203 struct got_repository *repo;
3204 got_worktree_status_cb status_cb;
3206 got_cancel_cb cancel_cb;
3208 /* A pathlist containing per-directory pathlists of ignore patterns. */
3209 struct got_pathlist_head *ignores;
3210 int report_unchanged;
3214 static const struct got_error *
3215 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3216 int dirfd, const char *de_name,
3217 got_worktree_status_cb status_cb, void *status_arg,
3218 struct got_repository *repo, int report_unchanged)
3220 const struct got_error *err = NULL;
3221 unsigned char status = GOT_STATUS_NO_CHANGE;
3222 unsigned char staged_status = get_staged_status(ie);
3224 struct got_object_id blob_id, commit_id, staged_blob_id;
3225 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3226 struct got_object_id *staged_blob_idp = NULL;
3228 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3232 if (status == GOT_STATUS_NO_CHANGE &&
3233 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3236 if (got_fileindex_entry_has_blob(ie)) {
3237 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3238 blob_idp = &blob_id;
3240 if (got_fileindex_entry_has_commit(ie)) {
3241 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3242 commit_idp = &commit_id;
3244 if (staged_status == GOT_STATUS_ADD ||
3245 staged_status == GOT_STATUS_MODIFY) {
3246 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3247 SHA1_DIGEST_LENGTH);
3248 staged_blob_idp = &staged_blob_id;
3251 return (*status_cb)(status_arg, status, staged_status,
3252 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3255 static const struct got_error *
3256 status_old_new(void *arg, struct got_fileindex_entry *ie,
3257 struct dirent *de, const char *parent_path, int dirfd)
3259 const struct got_error *err = NULL;
3260 struct diff_dir_cb_arg *a = arg;
3263 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3264 return got_error(GOT_ERR_CANCELLED);
3266 if (got_path_cmp(parent_path, a->status_path,
3267 strlen(parent_path), a->status_path_len) != 0 &&
3268 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3271 if (parent_path[0]) {
3272 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3273 parent_path, de->d_name) == -1)
3274 return got_error_from_errno("asprintf");
3276 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3278 return got_error_from_errno("asprintf");
3281 err = report_file_status(ie, abspath, dirfd, de->d_name,
3282 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3287 static const struct got_error *
3288 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3290 struct diff_dir_cb_arg *a = arg;
3291 struct got_object_id blob_id, commit_id;
3292 unsigned char status;
3294 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3295 return got_error(GOT_ERR_CANCELLED);
3297 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3300 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3301 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3302 if (got_fileindex_entry_has_file_on_disk(ie))
3303 status = GOT_STATUS_MISSING;
3305 status = GOT_STATUS_DELETE;
3306 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3307 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3311 free_ignorelist(struct got_pathlist_head *ignorelist)
3313 struct got_pathlist_entry *pe;
3315 TAILQ_FOREACH(pe, ignorelist, entry)
3316 free((char *)pe->path);
3317 got_pathlist_free(ignorelist);
3321 free_ignores(struct got_pathlist_head *ignores)
3323 struct got_pathlist_entry *pe;
3325 TAILQ_FOREACH(pe, ignores, entry) {
3326 struct got_pathlist_head *ignorelist = pe->data;
3327 free_ignorelist(ignorelist);
3328 free((char *)pe->path);
3330 got_pathlist_free(ignores);
3333 static const struct got_error *
3334 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3336 const struct got_error *err = NULL;
3337 struct got_pathlist_entry *pe = NULL;
3338 struct got_pathlist_head *ignorelist;
3339 char *line = NULL, *pattern, *dirpath = NULL;
3340 size_t linesize = 0;
3343 ignorelist = calloc(1, sizeof(*ignorelist));
3344 if (ignorelist == NULL)
3345 return got_error_from_errno("calloc");
3346 TAILQ_INIT(ignorelist);
3348 while ((linelen = getline(&line, &linesize, f)) != -1) {
3349 if (linelen > 0 && line[linelen - 1] == '\n')
3350 line[linelen - 1] = '\0';
3352 /* Git's ignores may contain comments. */
3356 /* Git's negated patterns are not (yet?) supported. */
3360 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3362 err = got_error_from_errno("asprintf");
3365 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3370 err = got_error_from_errno("getline");
3374 dirpath = strdup(path);
3375 if (dirpath == NULL) {
3376 err = got_error_from_errno("strdup");
3379 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3382 if (err || pe == NULL) {
3384 free_ignorelist(ignorelist);
3390 match_ignores(struct got_pathlist_head *ignores, const char *path)
3392 struct got_pathlist_entry *pe;
3394 /* Handle patterns which match in all directories. */
3395 TAILQ_FOREACH(pe, ignores, entry) {
3396 struct got_pathlist_head *ignorelist = pe->data;
3397 struct got_pathlist_entry *pi;
3399 TAILQ_FOREACH(pi, ignorelist, entry) {
3400 const char *p, *pattern = pi->path;
3402 if (strncmp(pattern, "**/", 3) != 0)
3407 if (fnmatch(pattern, p,
3408 FNM_PATHNAME | FNM_LEADING_DIR)) {
3409 /* Retry in next directory. */
3410 while (*p && *p != '/')
3422 * The ignores pathlist contains ignore lists from children before
3423 * parents, so we can find the most specific ignorelist by walking
3424 * ignores backwards.
3426 pe = TAILQ_LAST(ignores, got_pathlist_head);
3428 if (got_path_is_child(path, pe->path, pe->path_len)) {
3429 struct got_pathlist_head *ignorelist = pe->data;
3430 struct got_pathlist_entry *pi;
3431 TAILQ_FOREACH(pi, ignorelist, entry) {
3432 const char *pattern = pi->path;
3433 int flags = FNM_LEADING_DIR;
3434 if (strstr(pattern, "/**/") == NULL)
3435 flags |= FNM_PATHNAME;
3436 if (fnmatch(pattern, path, flags))
3441 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3447 static const struct got_error *
3448 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3449 const char *path, int dirfd, const char *ignores_filename)
3451 const struct got_error *err = NULL;
3454 FILE *ignoresfile = NULL;
3456 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3457 path[0] ? "/" : "", ignores_filename) == -1)
3458 return got_error_from_errno("asprintf");
3461 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3463 if (errno != ENOENT && errno != EACCES)
3464 err = got_error_from_errno2("openat",
3467 ignoresfile = fdopen(fd, "r");
3468 if (ignoresfile == NULL)
3469 err = got_error_from_errno2("fdopen",
3473 err = read_ignores(ignores, path, ignoresfile);
3477 ignoresfile = fopen(ignorespath, "r");
3478 if (ignoresfile == NULL) {
3479 if (errno != ENOENT && errno != EACCES)
3480 err = got_error_from_errno2("fopen",
3483 err = read_ignores(ignores, path, ignoresfile);
3486 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3487 err = got_error_from_errno2("fclose", path);
3488 if (fd != -1 && close(fd) == -1 && err == NULL)
3489 err = got_error_from_errno2("close", path);
3494 static const struct got_error *
3495 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3498 const struct got_error *err = NULL;
3499 struct diff_dir_cb_arg *a = arg;
3505 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3506 return got_error(GOT_ERR_CANCELLED);
3508 if (parent_path[0]) {
3509 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3510 return got_error_from_errno("asprintf");
3515 if (de->d_type == DT_DIR) {
3516 if (!a->no_ignores && ignore != NULL &&
3517 match_ignores(a->ignores, path))
3519 } else if (!match_ignores(a->ignores, path) &&
3520 got_path_is_child(path, a->status_path, a->status_path_len))
3521 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3522 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3528 static const struct got_error *
3529 status_traverse(void *arg, const char *path, int dirfd)
3531 const struct got_error *err = NULL;
3532 struct diff_dir_cb_arg *a = arg;
3537 err = add_ignores(a->ignores, a->worktree->root_path,
3538 path, dirfd, ".cvsignore");
3542 err = add_ignores(a->ignores, a->worktree->root_path, path,
3543 dirfd, ".gitignore");
3548 static const struct got_error *
3549 report_single_file_status(const char *path, const char *ondisk_path,
3550 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3551 void *status_arg, struct got_repository *repo, int report_unchanged,
3552 struct got_pathlist_head *ignores, int no_ignores)
3554 struct got_fileindex_entry *ie;
3557 if (!no_ignores && match_ignores(ignores, path))
3560 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3562 return report_file_status(ie, ondisk_path, -1, NULL,
3563 status_cb, status_arg, repo, report_unchanged);
3565 if (lstat(ondisk_path, &sb) == -1) {
3566 if (errno != ENOENT)
3567 return got_error_from_errno2("lstat", ondisk_path);
3568 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3569 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3573 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3574 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3575 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3580 static const struct got_error *
3581 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3582 const char *root_path, const char *path)
3584 const struct got_error *err;
3585 char *parent_path, *next_parent_path = NULL;
3587 err = add_ignores(ignores, root_path, "", -1,
3592 err = add_ignores(ignores, root_path, "", -1,
3597 err = got_path_dirname(&parent_path, path);
3599 if (err->code == GOT_ERR_BAD_PATH)
3600 return NULL; /* cannot traverse parent */
3604 err = add_ignores(ignores, root_path, parent_path, -1,
3608 err = add_ignores(ignores, root_path, parent_path, -1,
3612 err = got_path_dirname(&next_parent_path, parent_path);
3614 if (err->code == GOT_ERR_BAD_PATH)
3615 err = NULL; /* traversed everything */
3618 if (got_path_is_root_dir(parent_path))
3621 parent_path = next_parent_path;
3622 next_parent_path = NULL;
3626 free(next_parent_path);
3630 static const struct got_error *
3631 worktree_status(struct got_worktree *worktree, const char *path,
3632 struct got_fileindex *fileindex, struct got_repository *repo,
3633 got_worktree_status_cb status_cb, void *status_arg,
3634 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3635 int report_unchanged)
3637 const struct got_error *err = NULL;
3639 struct got_fileindex_diff_dir_cb fdiff_cb;
3640 struct diff_dir_cb_arg arg;
3641 char *ondisk_path = NULL;
3642 struct got_pathlist_head ignores;
3644 TAILQ_INIT(&ignores);
3646 if (asprintf(&ondisk_path, "%s%s%s",
3647 worktree->root_path, path[0] ? "/" : "", path) == -1)
3648 return got_error_from_errno("asprintf");
3650 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3652 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3653 !got_err_open_nofollow_on_symlink())
3654 err = got_error_from_errno2("open", ondisk_path);
3657 err = add_ignores_from_parent_paths(&ignores,
3658 worktree->root_path, ondisk_path);
3662 err = report_single_file_status(path, ondisk_path,
3663 fileindex, status_cb, status_arg, repo,
3664 report_unchanged, &ignores, no_ignores);
3667 fdiff_cb.diff_old_new = status_old_new;
3668 fdiff_cb.diff_old = status_old;
3669 fdiff_cb.diff_new = status_new;
3670 fdiff_cb.diff_traverse = status_traverse;
3671 arg.fileindex = fileindex;
3672 arg.worktree = worktree;
3673 arg.status_path = path;
3674 arg.status_path_len = strlen(path);
3676 arg.status_cb = status_cb;
3677 arg.status_arg = status_arg;
3678 arg.cancel_cb = cancel_cb;
3679 arg.cancel_arg = cancel_arg;
3680 arg.report_unchanged = report_unchanged;
3681 arg.no_ignores = no_ignores;
3683 err = add_ignores_from_parent_paths(&ignores,
3684 worktree->root_path, path);
3688 arg.ignores = &ignores;
3689 err = got_fileindex_diff_dir(fileindex, fd,
3690 worktree->root_path, path, repo, &fdiff_cb, &arg);
3693 free_ignores(&ignores);
3694 if (fd != -1 && close(fd) == -1 && err == NULL)
3695 err = got_error_from_errno("close");
3700 const struct got_error *
3701 got_worktree_status(struct got_worktree *worktree,
3702 struct got_pathlist_head *paths, struct got_repository *repo,
3703 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3704 got_cancel_cb cancel_cb, void *cancel_arg)
3706 const struct got_error *err = NULL;
3707 char *fileindex_path = NULL;
3708 struct got_fileindex *fileindex = NULL;
3709 struct got_pathlist_entry *pe;
3711 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3715 TAILQ_FOREACH(pe, paths, entry) {
3716 err = worktree_status(worktree, pe->path, fileindex, repo,
3717 status_cb, status_arg, cancel_cb, cancel_arg,
3722 free(fileindex_path);
3723 got_fileindex_free(fileindex);
3727 const struct got_error *
3728 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3731 const struct got_error *err = NULL;
3732 char *resolved = NULL, *cwd = NULL, *path = NULL;
3735 char *abspath = NULL;
3736 char canonpath[PATH_MAX];
3740 cwd = getcwd(NULL, 0);
3742 return got_error_from_errno("getcwd");
3744 if (lstat(arg, &sb) == -1) {
3745 if (errno != ENOENT) {
3746 err = got_error_from_errno2("lstat", arg);
3751 if (S_ISLNK(sb.st_mode)) {
3753 * We cannot use realpath(3) with symlinks since we want to
3754 * operate on the symlink itself.
3755 * But we can make the path absolute, assuming it is relative
3756 * to the current working directory, and then canonicalize it.
3758 if (!got_path_is_absolute(arg)) {
3759 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3760 err = got_error_from_errno("asprintf");
3765 err = got_canonpath(abspath ? abspath : arg, canonpath,
3769 resolved = strdup(canonpath);
3770 if (resolved == NULL) {
3771 err = got_error_from_errno("strdup");
3775 resolved = realpath(arg, NULL);
3776 if (resolved == NULL) {
3777 if (errno != ENOENT) {
3778 err = got_error_from_errno2("realpath", arg);
3781 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3782 err = got_error_from_errno("asprintf");
3785 err = got_canonpath(abspath, canonpath,
3789 resolved = strdup(canonpath);
3790 if (resolved == NULL) {
3791 err = got_error_from_errno("strdup");
3797 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3798 strlen(got_worktree_get_root_path(worktree)))) {
3799 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3803 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3804 err = got_path_skip_common_ancestor(&path,
3805 got_worktree_get_root_path(worktree), resolved);
3811 err = got_error_from_errno("strdup");
3816 /* XXX status walk can't deal with trailing slash! */
3818 while (len > 0 && path[len - 1] == '/') {
3819 path[len - 1] = '\0';
3833 struct schedule_addition_args {
3834 struct got_worktree *worktree;
3835 struct got_fileindex *fileindex;
3836 got_worktree_checkout_cb progress_cb;
3838 struct got_repository *repo;
3841 static const struct got_error *
3842 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3843 const char *relpath, struct got_object_id *blob_id,
3844 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3845 int dirfd, const char *de_name)
3847 struct schedule_addition_args *a = arg;
3848 const struct got_error *err = NULL;
3849 struct got_fileindex_entry *ie;
3853 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3855 return got_error_from_errno("asprintf");
3857 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3859 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3863 /* Re-adding an existing entry is a no-op. */
3864 if (status == GOT_STATUS_ADD)
3866 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3871 if (status != GOT_STATUS_UNVERSIONED) {
3872 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3876 err = got_fileindex_entry_alloc(&ie, relpath);
3879 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3880 relpath, NULL, NULL, 1);
3882 got_fileindex_entry_free(ie);
3885 err = got_fileindex_entry_add(a->fileindex, ie);
3887 got_fileindex_entry_free(ie);
3894 if (status == GOT_STATUS_ADD)
3896 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3899 const struct got_error *
3900 got_worktree_schedule_add(struct got_worktree *worktree,
3901 struct got_pathlist_head *paths,
3902 got_worktree_checkout_cb progress_cb, void *progress_arg,
3903 struct got_repository *repo, int no_ignores)
3905 struct got_fileindex *fileindex = NULL;
3906 char *fileindex_path = NULL;
3907 const struct got_error *err = NULL, *sync_err, *unlockerr;
3908 struct got_pathlist_entry *pe;
3909 struct schedule_addition_args saa;
3911 err = lock_worktree(worktree, LOCK_EX);
3915 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3919 saa.worktree = worktree;
3920 saa.fileindex = fileindex;
3921 saa.progress_cb = progress_cb;
3922 saa.progress_arg = progress_arg;
3925 TAILQ_FOREACH(pe, paths, entry) {
3926 err = worktree_status(worktree, pe->path, fileindex, repo,
3927 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3931 sync_err = sync_fileindex(fileindex, fileindex_path);
3932 if (sync_err && err == NULL)
3935 free(fileindex_path);
3937 got_fileindex_free(fileindex);
3938 unlockerr = lock_worktree(worktree, LOCK_SH);
3939 if (unlockerr && err == NULL)
3944 struct schedule_deletion_args {
3945 struct got_worktree *worktree;
3946 struct got_fileindex *fileindex;
3947 got_worktree_delete_cb progress_cb;
3949 struct got_repository *repo;
3950 int delete_local_mods;
3952 const char *status_codes;
3955 static const struct got_error *
3956 schedule_for_deletion(void *arg, unsigned char status,
3957 unsigned char staged_status, const char *relpath,
3958 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3959 struct got_object_id *commit_id, int dirfd, const char *de_name)
3961 struct schedule_deletion_args *a = arg;
3962 const struct got_error *err = NULL;
3963 struct got_fileindex_entry *ie = NULL;
3967 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3969 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3971 staged_status = get_staged_status(ie);
3972 if (staged_status != GOT_STATUS_NO_CHANGE) {
3973 if (staged_status == GOT_STATUS_DELETE)
3975 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3978 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3980 return got_error_from_errno("asprintf");
3982 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3987 if (a->status_codes) {
3988 size_t ncodes = strlen(a->status_codes);
3990 for (i = 0; i < ncodes ; i++) {
3991 if (status == a->status_codes[i])
3995 /* Do not delete files in non-matching status. */
3999 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4000 a->status_codes[i] != GOT_STATUS_MISSING) {
4001 static char msg[64];
4002 snprintf(msg, sizeof(msg),
4003 "invalid status code '%c'", a->status_codes[i]);
4004 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4009 if (status != GOT_STATUS_NO_CHANGE) {
4010 if (status == GOT_STATUS_DELETE)
4012 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4013 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4016 if (status != GOT_STATUS_MODIFY &&
4017 status != GOT_STATUS_MISSING) {
4018 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4023 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4027 if (unlinkat(dirfd, de_name, 0) != 0) {
4028 err = got_error_from_errno2("unlinkat",
4032 } else if (unlink(ondisk_path) != 0) {
4033 err = got_error_from_errno2("unlink", ondisk_path);
4037 root_len = strlen(a->worktree->root_path);
4040 err = got_path_dirname(&parent, ondisk_path);
4044 ondisk_path = parent;
4045 if (rmdir(ondisk_path) == -1) {
4046 if (errno != ENOTEMPTY)
4047 err = got_error_from_errno2("rmdir",
4051 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4052 strlen(ondisk_path), root_len) != 0);
4055 got_fileindex_entry_mark_deleted_from_disk(ie);
4060 if (status == GOT_STATUS_DELETE)
4062 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4063 staged_status, relpath);
4066 const struct got_error *
4067 got_worktree_schedule_delete(struct got_worktree *worktree,
4068 struct got_pathlist_head *paths, int delete_local_mods,
4069 const char *status_codes,
4070 got_worktree_delete_cb progress_cb, void *progress_arg,
4071 struct got_repository *repo, int keep_on_disk)
4073 struct got_fileindex *fileindex = NULL;
4074 char *fileindex_path = NULL;
4075 const struct got_error *err = NULL, *sync_err, *unlockerr;
4076 struct got_pathlist_entry *pe;
4077 struct schedule_deletion_args sda;
4079 err = lock_worktree(worktree, LOCK_EX);
4083 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4087 sda.worktree = worktree;
4088 sda.fileindex = fileindex;
4089 sda.progress_cb = progress_cb;
4090 sda.progress_arg = progress_arg;
4092 sda.delete_local_mods = delete_local_mods;
4093 sda.keep_on_disk = keep_on_disk;
4094 sda.status_codes = status_codes;
4096 TAILQ_FOREACH(pe, paths, entry) {
4097 err = worktree_status(worktree, pe->path, fileindex, repo,
4098 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4102 sync_err = sync_fileindex(fileindex, fileindex_path);
4103 if (sync_err && err == NULL)
4106 free(fileindex_path);
4108 got_fileindex_free(fileindex);
4109 unlockerr = lock_worktree(worktree, LOCK_SH);
4110 if (unlockerr && err == NULL)
4115 static const struct got_error *
4116 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4118 const struct got_error *err = NULL;
4120 size_t linesize = 0, n;
4123 linelen = getline(&line, &linesize, infile);
4124 if (linelen == -1) {
4125 if (ferror(infile)) {
4126 err = got_error_from_errno("getline");
4132 n = fwrite(line, 1, linelen, outfile);
4134 err = got_ferror(outfile, GOT_ERR_IO);
4139 n = fwrite(line, 1, linelen, rejectfile);
4141 err = got_ferror(outfile, GOT_ERR_IO);
4148 static const struct got_error *
4149 skip_one_line(FILE *f)
4152 size_t linesize = 0;
4155 linelen = getline(&line, &linesize, f);
4156 if (linelen == -1) {
4158 return got_error_from_errno("getline");
4165 static const struct got_error *
4166 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4167 int start_old, int end_old, int start_new, int end_new,
4168 FILE *outfile, FILE *rejectfile)
4170 const struct got_error *err;
4172 /* Copy old file's lines leading up to patch. */
4173 while (!feof(f1) && *line_cur1 < start_old) {
4174 err = copy_one_line(f1, outfile, NULL);
4179 /* Skip new file's lines leading up to patch. */
4180 while (!feof(f2) && *line_cur2 < start_new) {
4182 err = copy_one_line(f2, NULL, rejectfile);
4184 err = skip_one_line(f2);
4189 /* Copy patched lines. */
4190 while (!feof(f2) && *line_cur2 <= end_new) {
4191 err = copy_one_line(f2, outfile, NULL);
4196 /* Skip over old file's replaced lines. */
4197 while (!feof(f1) && *line_cur1 <= end_old) {
4199 err = copy_one_line(f1, NULL, rejectfile);
4201 err = skip_one_line(f1);
4210 static const struct got_error *
4211 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4212 FILE *outfile, FILE *rejectfile)
4214 const struct got_error *err;
4217 /* Copy old file's lines until EOF. */
4219 err = copy_one_line(f1, outfile, NULL);
4226 /* Copy new file's lines until EOF. */
4228 err = copy_one_line(f2, NULL, rejectfile);
4238 static const struct got_error *
4239 apply_or_reject_change(int *choice, int *nchunks_used,
4240 struct diff_result *diff_result, int n,
4241 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4242 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4243 got_worktree_patch_cb patch_cb, void *patch_arg)
4245 const struct got_error *err = NULL;
4246 struct diff_chunk_context cc = {};
4247 int start_old, end_old, start_new, end_new;
4249 struct diff_output_unidiff_state *diff_state;
4250 struct diff_input_info diff_info;
4253 *choice = GOT_PATCH_CHOICE_NONE;
4255 /* Get changed line numbers without context lines for copy_change(). */
4256 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4257 start_old = cc.left.start;
4258 end_old = cc.left.end;
4259 start_new = cc.right.start;
4260 end_new = cc.right.end;
4262 /* Get the same change with context lines for display. */
4263 memset(&cc, 0, sizeof(cc));
4264 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4266 memset(&diff_info, 0, sizeof(diff_info));
4267 diff_info.left_path = relpath;
4268 diff_info.right_path = relpath;
4270 diff_state = diff_output_unidiff_state_alloc();
4271 if (diff_state == NULL)
4272 return got_error_set_errno(ENOMEM,
4273 "diff_output_unidiff_state_alloc");
4275 hunkfile = got_opentemp();
4276 if (hunkfile == NULL) {
4277 err = got_error_from_errno("got_opentemp");
4281 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4283 if (rc != DIFF_RC_OK) {
4284 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4288 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4289 err = got_ferror(hunkfile, GOT_ERR_IO);
4293 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4294 hunkfile, changeno, nchanges);
4299 case GOT_PATCH_CHOICE_YES:
4300 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4301 end_old, start_new, end_new, outfile, rejectfile);
4303 case GOT_PATCH_CHOICE_NO:
4304 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4305 end_old, start_new, end_new, rejectfile, outfile);
4307 case GOT_PATCH_CHOICE_QUIT:
4310 err = got_error(GOT_ERR_PATCH_CHOICE);
4314 diff_output_unidiff_state_free(diff_state);
4315 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4316 err = got_error_from_errno("fclose");
4320 struct revert_file_args {
4321 struct got_worktree *worktree;
4322 struct got_fileindex *fileindex;
4323 got_worktree_checkout_cb progress_cb;
4325 got_worktree_patch_cb patch_cb;
4327 struct got_repository *repo;
4328 int unlink_added_files;
4331 static const struct got_error *
4332 create_patched_content(char **path_outfile, int reverse_patch,
4333 struct got_object_id *blob_id, const char *path2,
4334 int dirfd2, const char *de_name2,
4335 const char *relpath, struct got_repository *repo,
4336 got_worktree_patch_cb patch_cb, void *patch_arg)
4338 const struct got_error *err, *free_err;
4339 struct got_blob_object *blob = NULL;
4340 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4342 char link_target[PATH_MAX];
4343 ssize_t link_len = 0;
4344 char *path1 = NULL, *id_str = NULL;
4346 struct got_diffreg_result *diffreg_result = NULL;
4347 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4348 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4350 *path_outfile = NULL;
4352 err = got_object_id_str(&id_str, blob_id);
4357 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4359 if (!got_err_open_nofollow_on_symlink()) {
4360 err = got_error_from_errno2("openat", path2);
4363 link_len = readlinkat(dirfd2, de_name2,
4364 link_target, sizeof(link_target));
4366 return got_error_from_errno2("readlinkat", path2);
4367 sb2.st_mode = S_IFLNK;
4368 sb2.st_size = link_len;
4371 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4373 if (!got_err_open_nofollow_on_symlink()) {
4374 err = got_error_from_errno2("open", path2);
4377 link_len = readlink(path2, link_target,
4378 sizeof(link_target));
4380 return got_error_from_errno2("readlink", path2);
4381 sb2.st_mode = S_IFLNK;
4382 sb2.st_size = link_len;
4386 if (fstat(fd2, &sb2) == -1) {
4387 err = got_error_from_errno2("fstat", path2);
4391 f2 = fdopen(fd2, "r");
4393 err = got_error_from_errno2("fdopen", path2);
4399 f2 = got_opentemp();
4401 err = got_error_from_errno2("got_opentemp", path2);
4404 n = fwrite(link_target, 1, link_len, f2);
4405 if (n != link_len) {
4406 err = got_ferror(f2, GOT_ERR_IO);
4409 if (fflush(f2) == EOF) {
4410 err = got_error_from_errno("fflush");
4416 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4420 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4424 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4428 err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4433 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4437 if (fseek(f1, 0L, SEEK_SET) == -1)
4438 return got_ferror(f1, GOT_ERR_IO);
4439 if (fseek(f2, 0L, SEEK_SET) == -1)
4440 return got_ferror(f2, GOT_ERR_IO);
4442 /* Count the number of actual changes in the diff result. */
4443 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4444 struct diff_chunk_context cc = {};
4445 diff_chunk_context_load_change(&cc, &nchunks_used,
4446 diffreg_result->result, n, 0);
4449 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4451 err = apply_or_reject_change(&choice, &nchunks_used,
4452 diffreg_result->result, n, relpath, f1, f2,
4453 &line_cur1, &line_cur2,
4454 reverse_patch ? NULL : outfile,
4455 reverse_patch ? outfile : NULL,
4456 ++i, nchanges, patch_cb, patch_arg);
4459 if (choice == GOT_PATCH_CHOICE_YES)
4461 else if (choice == GOT_PATCH_CHOICE_QUIT)
4465 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4466 reverse_patch ? NULL : outfile,
4467 reverse_patch ? outfile : NULL);
4471 if (!S_ISLNK(sb2.st_mode)) {
4472 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4473 err = got_error_from_errno2("fchmod", path2);
4481 got_object_blob_close(blob);
4482 free_err = got_diffreg_result_free(diffreg_result);
4485 if (f1 && fclose(f1) == EOF && err == NULL)
4486 err = got_error_from_errno2("fclose", path1);
4487 if (f2 && fclose(f2) == EOF && err == NULL)
4488 err = got_error_from_errno2("fclose", path2);
4489 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4490 err = got_error_from_errno2("close", path2);
4491 if (outfile && fclose(outfile) == EOF && err == NULL)
4492 err = got_error_from_errno2("fclose", *path_outfile);
4493 if (path1 && unlink(path1) == -1 && err == NULL)
4494 err = got_error_from_errno2("unlink", path1);
4495 if (err || !have_content) {
4496 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4497 err = got_error_from_errno2("unlink", *path_outfile);
4498 free(*path_outfile);
4499 *path_outfile = NULL;
4505 static const struct got_error *
4506 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4507 const char *relpath, struct got_object_id *blob_id,
4508 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4509 int dirfd, const char *de_name)
4511 struct revert_file_args *a = arg;
4512 const struct got_error *err = NULL;
4513 char *parent_path = NULL;
4514 struct got_fileindex_entry *ie;
4515 struct got_tree_object *tree = NULL;
4516 struct got_object_id *tree_id = NULL;
4517 const struct got_tree_entry *te = NULL;
4518 char *tree_path = NULL, *te_name;
4519 char *ondisk_path = NULL, *path_content = NULL;
4520 struct got_blob_object *blob = NULL;
4522 /* Reverting a staged deletion is a no-op. */
4523 if (status == GOT_STATUS_DELETE &&
4524 staged_status != GOT_STATUS_NO_CHANGE)
4527 if (status == GOT_STATUS_UNVERSIONED)
4528 return (*a->progress_cb)(a->progress_arg,
4529 GOT_STATUS_UNVERSIONED, relpath);
4531 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4533 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4535 /* Construct in-repository path of tree which contains this blob. */
4536 err = got_path_dirname(&parent_path, ie->path);
4538 if (err->code != GOT_ERR_BAD_PATH)
4540 parent_path = strdup("/");
4541 if (parent_path == NULL) {
4542 err = got_error_from_errno("strdup");
4546 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4547 tree_path = strdup(parent_path);
4548 if (tree_path == NULL) {
4549 err = got_error_from_errno("strdup");
4553 if (got_path_is_root_dir(parent_path)) {
4554 tree_path = strdup(a->worktree->path_prefix);
4555 if (tree_path == NULL) {
4556 err = got_error_from_errno("strdup");
4560 if (asprintf(&tree_path, "%s/%s",
4561 a->worktree->path_prefix, parent_path) == -1) {
4562 err = got_error_from_errno("asprintf");
4568 err = got_object_id_by_path(&tree_id, a->repo,
4569 a->worktree->base_commit_id, tree_path);
4571 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4572 (status == GOT_STATUS_ADD ||
4573 staged_status == GOT_STATUS_ADD)))
4576 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4580 err = got_path_basename(&te_name, ie->path);
4584 te = got_object_tree_find_entry(tree, te_name);
4586 if (te == NULL && status != GOT_STATUS_ADD &&
4587 staged_status != GOT_STATUS_ADD) {
4588 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4594 case GOT_STATUS_ADD:
4596 int choice = GOT_PATCH_CHOICE_NONE;
4597 err = (*a->patch_cb)(&choice, a->patch_arg,
4598 status, ie->path, NULL, 1, 1);
4601 if (choice != GOT_PATCH_CHOICE_YES)
4604 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4608 got_fileindex_entry_remove(a->fileindex, ie);
4609 if (a->unlink_added_files) {
4610 if (asprintf(&ondisk_path, "%s/%s",
4611 got_worktree_get_root_path(a->worktree),
4613 err = got_error_from_errno("asprintf");
4616 if (unlink(ondisk_path) == -1) {
4617 err = got_error_from_errno2("unlink",
4623 case GOT_STATUS_DELETE:
4625 int choice = GOT_PATCH_CHOICE_NONE;
4626 err = (*a->patch_cb)(&choice, a->patch_arg,
4627 status, ie->path, NULL, 1, 1);
4630 if (choice != GOT_PATCH_CHOICE_YES)
4634 case GOT_STATUS_MODIFY:
4635 case GOT_STATUS_MODE_CHANGE:
4636 case GOT_STATUS_CONFLICT:
4637 case GOT_STATUS_MISSING: {
4638 struct got_object_id id;
4639 if (staged_status == GOT_STATUS_ADD ||
4640 staged_status == GOT_STATUS_MODIFY) {
4641 memcpy(id.sha1, ie->staged_blob_sha1,
4642 SHA1_DIGEST_LENGTH);
4644 memcpy(id.sha1, ie->blob_sha1,
4645 SHA1_DIGEST_LENGTH);
4646 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4650 if (asprintf(&ondisk_path, "%s/%s",
4651 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4652 err = got_error_from_errno("asprintf");
4656 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4657 status == GOT_STATUS_CONFLICT)) {
4658 int is_bad_symlink = 0;
4659 err = create_patched_content(&path_content, 1, &id,
4660 ondisk_path, dirfd, de_name, ie->path, a->repo,
4661 a->patch_cb, a->patch_arg);
4662 if (err || path_content == NULL)
4664 if (te && S_ISLNK(te->mode)) {
4665 if (unlink(path_content) == -1) {
4666 err = got_error_from_errno2("unlink",
4670 err = install_symlink(&is_bad_symlink,
4671 a->worktree, ondisk_path, ie->path,
4672 blob, 0, 1, 0, 0, a->repo,
4673 a->progress_cb, a->progress_arg);
4675 if (rename(path_content, ondisk_path) == -1) {
4676 err = got_error_from_errno3("rename",
4677 path_content, ondisk_path);
4682 int is_bad_symlink = 0;
4683 if (te && S_ISLNK(te->mode)) {
4684 err = install_symlink(&is_bad_symlink,
4685 a->worktree, ondisk_path, ie->path,
4686 blob, 0, 1, 0, 0, a->repo,
4687 a->progress_cb, a->progress_arg);
4689 err = install_blob(a->worktree, ondisk_path,
4691 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4692 got_fileindex_perms_to_st(ie), blob,
4693 0, 1, 0, 0, a->repo,
4694 a->progress_cb, a->progress_arg);
4698 if (status == GOT_STATUS_DELETE ||
4699 status == GOT_STATUS_MODE_CHANGE) {
4700 err = got_fileindex_entry_update(ie,
4701 a->worktree->root_fd, relpath,
4703 a->worktree->base_commit_id->sha1, 1);
4707 if (is_bad_symlink) {
4708 got_fileindex_entry_filetype_set(ie,
4709 GOT_FILEIDX_MODE_BAD_SYMLINK);
4723 got_object_blob_close(blob);
4725 got_object_tree_close(tree);
4730 const struct got_error *
4731 got_worktree_revert(struct got_worktree *worktree,
4732 struct got_pathlist_head *paths,
4733 got_worktree_checkout_cb progress_cb, void *progress_arg,
4734 got_worktree_patch_cb patch_cb, void *patch_arg,
4735 struct got_repository *repo)
4737 struct got_fileindex *fileindex = NULL;
4738 char *fileindex_path = NULL;
4739 const struct got_error *err = NULL, *unlockerr = NULL;
4740 const struct got_error *sync_err = NULL;
4741 struct got_pathlist_entry *pe;
4742 struct revert_file_args rfa;
4744 err = lock_worktree(worktree, LOCK_EX);
4748 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4752 rfa.worktree = worktree;
4753 rfa.fileindex = fileindex;
4754 rfa.progress_cb = progress_cb;
4755 rfa.progress_arg = progress_arg;
4756 rfa.patch_cb = patch_cb;
4757 rfa.patch_arg = patch_arg;
4759 rfa.unlink_added_files = 0;
4760 TAILQ_FOREACH(pe, paths, entry) {
4761 err = worktree_status(worktree, pe->path, fileindex, repo,
4762 revert_file, &rfa, NULL, NULL, 1, 0);
4766 sync_err = sync_fileindex(fileindex, fileindex_path);
4767 if (sync_err && err == NULL)
4770 free(fileindex_path);
4772 got_fileindex_free(fileindex);
4773 unlockerr = lock_worktree(worktree, LOCK_SH);
4774 if (unlockerr && err == NULL)
4780 free_commitable(struct got_commitable *ct)
4783 free(ct->in_repo_path);
4784 free(ct->ondisk_path);
4786 free(ct->base_blob_id);
4787 free(ct->staged_blob_id);
4788 free(ct->base_commit_id);
4792 struct collect_commitables_arg {
4793 struct got_pathlist_head *commitable_paths;
4794 struct got_repository *repo;
4795 struct got_worktree *worktree;
4796 struct got_fileindex *fileindex;
4797 int have_staged_files;
4798 int allow_bad_symlinks;
4801 static const struct got_error *
4802 collect_commitables(void *arg, unsigned char status,
4803 unsigned char staged_status, const char *relpath,
4804 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4805 struct got_object_id *commit_id, int dirfd, const char *de_name)
4807 struct collect_commitables_arg *a = arg;
4808 const struct got_error *err = NULL;
4809 struct got_commitable *ct = NULL;
4810 struct got_pathlist_entry *new = NULL;
4811 char *parent_path = NULL, *path = NULL;
4814 if (a->have_staged_files) {
4815 if (staged_status != GOT_STATUS_MODIFY &&
4816 staged_status != GOT_STATUS_ADD &&
4817 staged_status != GOT_STATUS_DELETE)
4820 if (status == GOT_STATUS_CONFLICT)
4821 return got_error(GOT_ERR_COMMIT_CONFLICT);
4823 if (status != GOT_STATUS_MODIFY &&
4824 status != GOT_STATUS_MODE_CHANGE &&
4825 status != GOT_STATUS_ADD &&
4826 status != GOT_STATUS_DELETE)
4830 if (asprintf(&path, "/%s", relpath) == -1) {
4831 err = got_error_from_errno("asprintf");
4834 if (strcmp(path, "/") == 0) {
4835 parent_path = strdup("");
4836 if (parent_path == NULL)
4837 return got_error_from_errno("strdup");
4839 err = got_path_dirname(&parent_path, path);
4844 ct = calloc(1, sizeof(*ct));
4846 err = got_error_from_errno("calloc");
4850 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4852 err = got_error_from_errno("asprintf");
4856 if (staged_status == GOT_STATUS_ADD ||
4857 staged_status == GOT_STATUS_MODIFY) {
4858 struct got_fileindex_entry *ie;
4859 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4860 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4861 case GOT_FILEIDX_MODE_REGULAR_FILE:
4862 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4865 case GOT_FILEIDX_MODE_SYMLINK:
4869 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4872 ct->mode |= got_fileindex_entry_perms_get(ie);
4873 } else if (status != GOT_STATUS_DELETE &&
4874 staged_status != GOT_STATUS_DELETE) {
4876 if (fstatat(dirfd, de_name, &sb,
4877 AT_SYMLINK_NOFOLLOW) == -1) {
4878 err = got_error_from_errno2("fstatat",
4882 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4883 err = got_error_from_errno2("lstat", ct->ondisk_path);
4886 ct->mode = sb.st_mode;
4889 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4890 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4892 err = got_error_from_errno("asprintf");
4896 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4897 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4899 char target_path[PATH_MAX];
4901 target_len = readlink(ct->ondisk_path, target_path,
4902 sizeof(target_path));
4903 if (target_len == -1) {
4904 err = got_error_from_errno2("readlink",
4908 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4909 target_len, ct->ondisk_path, a->worktree->root_path);
4912 if (is_bad_symlink) {
4913 err = got_error_path(ct->ondisk_path,
4914 GOT_ERR_BAD_SYMLINK);
4920 ct->status = status;
4921 ct->staged_status = staged_status;
4922 ct->blob_id = NULL; /* will be filled in when blob gets created */
4923 if (ct->status != GOT_STATUS_ADD &&
4924 ct->staged_status != GOT_STATUS_ADD) {
4925 ct->base_blob_id = got_object_id_dup(blob_id);
4926 if (ct->base_blob_id == NULL) {
4927 err = got_error_from_errno("got_object_id_dup");
4930 ct->base_commit_id = got_object_id_dup(commit_id);
4931 if (ct->base_commit_id == NULL) {
4932 err = got_error_from_errno("got_object_id_dup");
4936 if (ct->staged_status == GOT_STATUS_ADD ||
4937 ct->staged_status == GOT_STATUS_MODIFY) {
4938 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4939 if (ct->staged_blob_id == NULL) {
4940 err = got_error_from_errno("got_object_id_dup");
4944 ct->path = strdup(path);
4945 if (ct->path == NULL) {
4946 err = got_error_from_errno("strdup");
4949 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4951 if (ct && (err || new == NULL))
4952 free_commitable(ct);
4958 static const struct got_error *write_tree(struct got_object_id **, int *,
4959 struct got_tree_object *, const char *, struct got_pathlist_head *,
4960 got_worktree_status_cb status_cb, void *status_arg,
4961 struct got_repository *);
4963 static const struct got_error *
4964 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4965 struct got_tree_entry *te, const char *parent_path,
4966 struct got_pathlist_head *commitable_paths,
4967 got_worktree_status_cb status_cb, void *status_arg,
4968 struct got_repository *repo)
4970 const struct got_error *err = NULL;
4971 struct got_tree_object *subtree;
4974 if (asprintf(&subpath, "%s%s%s", parent_path,
4975 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4976 return got_error_from_errno("asprintf");
4978 err = got_object_open_as_tree(&subtree, repo, &te->id);
4982 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4983 commitable_paths, status_cb, status_arg, repo);
4984 got_object_tree_close(subtree);
4989 static const struct got_error *
4990 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4992 const struct got_error *err = NULL;
4993 char *ct_parent_path = NULL;
4997 if (strchr(ct->in_repo_path, '/') == NULL) {
4998 *match = got_path_is_root_dir(path);
5002 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5005 *match = (strcmp(path, ct_parent_path) == 0);
5006 free(ct_parent_path);
5011 get_ct_file_mode(struct got_commitable *ct)
5013 if (S_ISLNK(ct->mode))
5016 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5019 static const struct got_error *
5020 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5021 struct got_tree_entry *te, struct got_commitable *ct)
5023 const struct got_error *err = NULL;
5027 err = got_object_tree_entry_dup(new_te, te);
5031 (*new_te)->mode = get_ct_file_mode(ct);
5033 if (ct->staged_status == GOT_STATUS_MODIFY)
5034 memcpy(&(*new_te)->id, ct->staged_blob_id,
5035 sizeof((*new_te)->id));
5037 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5039 if (err && *new_te) {
5046 static const struct got_error *
5047 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5048 struct got_commitable *ct)
5050 const struct got_error *err = NULL;
5051 char *ct_name = NULL;
5055 *new_te = calloc(1, sizeof(**new_te));
5056 if (*new_te == NULL)
5057 return got_error_from_errno("calloc");
5059 err = got_path_basename(&ct_name, ct->path);
5062 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5063 sizeof((*new_te)->name)) {
5064 err = got_error(GOT_ERR_NO_SPACE);
5068 (*new_te)->mode = get_ct_file_mode(ct);
5070 if (ct->staged_status == GOT_STATUS_ADD)
5071 memcpy(&(*new_te)->id, ct->staged_blob_id,
5072 sizeof((*new_te)->id));
5074 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5077 if (err && *new_te) {
5084 static const struct got_error *
5085 insert_tree_entry(struct got_tree_entry *new_te,
5086 struct got_pathlist_head *paths)
5088 const struct got_error *err = NULL;
5089 struct got_pathlist_entry *new_pe;
5091 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5095 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5099 static const struct got_error *
5100 report_ct_status(struct got_commitable *ct,
5101 got_worktree_status_cb status_cb, void *status_arg)
5103 const char *ct_path = ct->path;
5104 unsigned char status;
5106 if (status_cb == NULL) /* no commit progress output desired */
5109 while (ct_path[0] == '/')
5112 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5113 status = ct->staged_status;
5115 status = ct->status;
5117 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5118 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5121 static const struct got_error *
5122 match_modified_subtree(int *modified, struct got_tree_entry *te,
5123 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5125 const struct got_error *err = NULL;
5126 struct got_pathlist_entry *pe;
5131 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5132 got_path_is_root_dir(base_tree_path) ? "" : "/",
5134 return got_error_from_errno("asprintf");
5136 TAILQ_FOREACH(pe, commitable_paths, entry) {
5137 struct got_commitable *ct = pe->data;
5138 *modified = got_path_is_child(ct->in_repo_path, te_path,
5148 static const struct got_error *
5149 match_deleted_or_modified_ct(struct got_commitable **ctp,
5150 struct got_tree_entry *te, const char *base_tree_path,
5151 struct got_pathlist_head *commitable_paths)
5153 const struct got_error *err = NULL;
5154 struct got_pathlist_entry *pe;
5158 TAILQ_FOREACH(pe, commitable_paths, entry) {
5159 struct got_commitable *ct = pe->data;
5160 char *ct_name = NULL;
5163 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5164 if (ct->status != GOT_STATUS_MODIFY &&
5165 ct->status != GOT_STATUS_MODE_CHANGE &&
5166 ct->status != GOT_STATUS_DELETE)
5169 if (ct->staged_status != GOT_STATUS_MODIFY &&
5170 ct->staged_status != GOT_STATUS_DELETE)
5174 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5177 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5183 err = got_path_basename(&ct_name, pe->path);
5187 if (strcmp(te->name, ct_name) != 0) {
5200 static const struct got_error *
5201 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5202 const char *child_path, const char *path_base_tree,
5203 struct got_pathlist_head *commitable_paths,
5204 got_worktree_status_cb status_cb, void *status_arg,
5205 struct got_repository *repo)
5207 const struct got_error *err = NULL;
5208 struct got_tree_entry *new_te;
5210 struct got_object_id *id = NULL;
5215 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5216 got_path_is_root_dir(path_base_tree) ? "" : "/",
5218 return got_error_from_errno("asprintf");
5220 new_te = calloc(1, sizeof(*new_te));
5222 return got_error_from_errno("calloc");
5223 new_te->mode = S_IFDIR;
5225 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5226 sizeof(new_te->name)) {
5227 err = got_error(GOT_ERR_NO_SPACE);
5230 err = write_tree(&id, &nentries, NULL, subtree_path,
5231 commitable_paths, status_cb, status_arg, repo);
5236 memcpy(&new_te->id, id, sizeof(new_te->id));
5245 static const struct got_error *
5246 write_tree(struct got_object_id **new_tree_id, int *nentries,
5247 struct got_tree_object *base_tree, const char *path_base_tree,
5248 struct got_pathlist_head *commitable_paths,
5249 got_worktree_status_cb status_cb, void *status_arg,
5250 struct got_repository *repo)
5252 const struct got_error *err = NULL;
5253 struct got_pathlist_head paths;
5254 struct got_tree_entry *te, *new_te = NULL;
5255 struct got_pathlist_entry *pe;
5260 /* Insert, and recurse into, newly added entries first. */
5261 TAILQ_FOREACH(pe, commitable_paths, entry) {
5262 struct got_commitable *ct = pe->data;
5263 char *child_path = NULL, *slash;
5265 if ((ct->status != GOT_STATUS_ADD &&
5266 ct->staged_status != GOT_STATUS_ADD) ||
5267 (ct->flags & GOT_COMMITABLE_ADDED))
5270 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5271 strlen(path_base_tree)))
5274 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5279 slash = strchr(child_path, '/');
5280 if (slash == NULL) {
5281 err = alloc_added_blob_tree_entry(&new_te, ct);
5284 err = report_ct_status(ct, status_cb, status_arg);
5287 ct->flags |= GOT_COMMITABLE_ADDED;
5288 err = insert_tree_entry(new_te, &paths);
5293 *slash = '\0'; /* trim trailing path components */
5294 if (base_tree == NULL ||
5295 got_object_tree_find_entry(base_tree, child_path)
5297 err = make_subtree_for_added_blob(&new_te,
5298 child_path, path_base_tree,
5299 commitable_paths, status_cb, status_arg,
5303 err = insert_tree_entry(new_te, &paths);
5312 int i, nbase_entries;
5313 /* Handle modified and deleted entries. */
5314 nbase_entries = got_object_tree_get_nentries(base_tree);
5315 for (i = 0; i < nbase_entries; i++) {
5316 struct got_commitable *ct = NULL;
5318 te = got_object_tree_get_entry(base_tree, i);
5319 if (got_object_tree_entry_is_submodule(te)) {
5320 /* Entry is a submodule; just copy it. */
5321 err = got_object_tree_entry_dup(&new_te, te);
5324 err = insert_tree_entry(new_te, &paths);
5331 if (S_ISDIR(te->mode)) {
5333 err = got_object_tree_entry_dup(&new_te, te);
5336 err = match_modified_subtree(&modified, te,
5337 path_base_tree, commitable_paths);
5340 /* Avoid recursion into unmodified subtrees. */
5342 struct got_object_id *new_id;
5344 err = write_subtree(&new_id,
5346 path_base_tree, commitable_paths,
5347 status_cb, status_arg, repo);
5350 if (nsubentries == 0) {
5351 /* All entries were deleted. */
5355 memcpy(&new_te->id, new_id,
5356 sizeof(new_te->id));
5359 err = insert_tree_entry(new_te, &paths);
5366 err = match_deleted_or_modified_ct(&ct, te,
5367 path_base_tree, commitable_paths);
5371 /* NB: Deleted entries get dropped here. */
5372 if (ct->status == GOT_STATUS_MODIFY ||
5373 ct->status == GOT_STATUS_MODE_CHANGE ||
5374 ct->staged_status == GOT_STATUS_MODIFY) {
5375 err = alloc_modified_blob_tree_entry(
5379 err = insert_tree_entry(new_te, &paths);
5384 err = report_ct_status(ct, status_cb,
5389 /* Entry is unchanged; just copy it. */
5390 err = got_object_tree_entry_dup(&new_te, te);
5393 err = insert_tree_entry(new_te, &paths);
5401 /* Write new list of entries; deleted entries have been dropped. */
5402 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5404 got_pathlist_free(&paths);
5408 static const struct got_error *
5409 update_fileindex_after_commit(struct got_worktree *worktree,
5410 struct got_pathlist_head *commitable_paths,
5411 struct got_object_id *new_base_commit_id,
5412 struct got_fileindex *fileindex, int have_staged_files)
5414 const struct got_error *err = NULL;
5415 struct got_pathlist_entry *pe;
5416 char *relpath = NULL;
5418 TAILQ_FOREACH(pe, commitable_paths, entry) {
5419 struct got_fileindex_entry *ie;
5420 struct got_commitable *ct = pe->data;
5422 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5424 err = got_path_skip_common_ancestor(&relpath,
5425 worktree->root_path, ct->ondisk_path);
5430 if (ct->status == GOT_STATUS_DELETE ||
5431 ct->staged_status == GOT_STATUS_DELETE) {
5432 got_fileindex_entry_remove(fileindex, ie);
5433 } else if (ct->staged_status == GOT_STATUS_ADD ||
5434 ct->staged_status == GOT_STATUS_MODIFY) {
5435 got_fileindex_entry_stage_set(ie,
5436 GOT_FILEIDX_STAGE_NONE);
5437 got_fileindex_entry_staged_filetype_set(ie, 0);
5439 err = got_fileindex_entry_update(ie,
5440 worktree->root_fd, relpath,
5441 ct->staged_blob_id->sha1,
5442 new_base_commit_id->sha1,
5443 !have_staged_files);
5445 err = got_fileindex_entry_update(ie,
5446 worktree->root_fd, relpath,
5448 new_base_commit_id->sha1,
5449 !have_staged_files);
5451 err = got_fileindex_entry_alloc(&ie, pe->path);
5454 err = got_fileindex_entry_update(ie,
5455 worktree->root_fd, relpath, ct->blob_id->sha1,
5456 new_base_commit_id->sha1, 1);
5458 got_fileindex_entry_free(ie);
5461 err = got_fileindex_entry_add(fileindex, ie);
5463 got_fileindex_entry_free(ie);
5476 static const struct got_error *
5477 check_out_of_date(const char *in_repo_path, unsigned char status,
5478 unsigned char staged_status, struct got_object_id *base_blob_id,
5479 struct got_object_id *base_commit_id,
5480 struct got_object_id *head_commit_id, struct got_repository *repo,
5483 const struct got_error *err = NULL;
5484 struct got_object_id *id = NULL;
5486 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5487 /* Trivial case: base commit == head commit */
5488 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5491 * Ensure file content which local changes were based
5492 * on matches file content in the branch head.
5494 err = got_object_id_by_path(&id, repo, head_commit_id,
5497 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5498 err = got_error(ood_errcode);
5500 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5501 err = got_error(ood_errcode);
5503 /* Require that added files don't exist in the branch head. */
5504 err = got_object_id_by_path(&id, repo, head_commit_id,
5506 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5508 err = id ? got_error(ood_errcode) : NULL;
5515 const struct got_error *
5516 commit_worktree(struct got_object_id **new_commit_id,
5517 struct got_pathlist_head *commitable_paths,
5518 struct got_object_id *head_commit_id,
5519 struct got_object_id *parent_id2,
5520 struct got_worktree *worktree,
5521 const char *author, const char *committer,
5522 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5523 got_worktree_status_cb status_cb, void *status_arg,
5524 struct got_repository *repo)
5526 const struct got_error *err = NULL, *unlockerr = NULL;
5527 struct got_pathlist_entry *pe;
5528 const char *head_ref_name = NULL;
5529 struct got_commit_object *head_commit = NULL;
5530 struct got_reference *head_ref2 = NULL;
5531 struct got_object_id *head_commit_id2 = NULL;
5532 struct got_tree_object *head_tree = NULL;
5533 struct got_object_id *new_tree_id = NULL;
5534 int nentries, nparents = 0;
5535 struct got_object_id_queue parent_ids;
5536 struct got_object_qid *pid = NULL;
5537 char *logmsg = NULL;
5539 *new_commit_id = NULL;
5541 STAILQ_INIT(&parent_ids);
5543 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5547 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5551 if (commit_msg_cb != NULL) {
5552 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5557 if (logmsg == NULL || strlen(logmsg) == 0) {
5558 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5562 /* Create blobs from added and modified files and record their IDs. */
5563 TAILQ_FOREACH(pe, commitable_paths, entry) {
5564 struct got_commitable *ct = pe->data;
5567 /* Blobs for staged files already exist. */
5568 if (ct->staged_status == GOT_STATUS_ADD ||
5569 ct->staged_status == GOT_STATUS_MODIFY)
5572 if (ct->status != GOT_STATUS_ADD &&
5573 ct->status != GOT_STATUS_MODIFY &&
5574 ct->status != GOT_STATUS_MODE_CHANGE)
5577 if (asprintf(&ondisk_path, "%s/%s",
5578 worktree->root_path, pe->path) == -1) {
5579 err = got_error_from_errno("asprintf");
5582 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5588 /* Recursively write new tree objects. */
5589 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5590 commitable_paths, status_cb, status_arg, repo);
5594 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5597 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5600 err = got_object_qid_alloc(&pid, parent_id2);
5603 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5606 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5607 nparents, author, time(NULL), committer, time(NULL), logmsg, repo);
5613 /* Check if a concurrent commit to our branch has occurred. */
5614 head_ref_name = got_worktree_get_head_ref_name(worktree);
5615 if (head_ref_name == NULL) {
5616 err = got_error_from_errno("got_worktree_get_head_ref_name");
5619 /* Lock the reference here to prevent concurrent modification. */
5620 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5623 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5626 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5627 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5630 /* Update branch head in repository. */
5631 err = got_ref_change_ref(head_ref2, *new_commit_id);
5634 err = got_ref_write(head_ref2, repo);
5638 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5642 err = ref_base_commit(worktree, repo);
5646 got_object_id_queue_free(&parent_ids);
5648 got_object_tree_close(head_tree);
5650 got_object_commit_close(head_commit);
5651 free(head_commit_id2);
5653 unlockerr = got_ref_unlock(head_ref2);
5654 if (unlockerr && err == NULL)
5656 got_ref_close(head_ref2);
5661 static const struct got_error *
5662 check_path_is_commitable(const char *path,
5663 struct got_pathlist_head *commitable_paths)
5665 struct got_pathlist_entry *cpe = NULL;
5666 size_t path_len = strlen(path);
5668 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5669 struct got_commitable *ct = cpe->data;
5670 const char *ct_path = ct->path;
5672 while (ct_path[0] == '/')
5675 if (strcmp(path, ct_path) == 0 ||
5676 got_path_is_child(ct_path, path, path_len))
5681 return got_error_path(path, GOT_ERR_BAD_PATH);
5686 static const struct got_error *
5687 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5689 int *have_staged_files = arg;
5691 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5692 *have_staged_files = 1;
5693 return got_error(GOT_ERR_CANCELLED);
5699 static const struct got_error *
5700 check_non_staged_files(struct got_fileindex *fileindex,
5701 struct got_pathlist_head *paths)
5703 struct got_pathlist_entry *pe;
5704 struct got_fileindex_entry *ie;
5706 TAILQ_FOREACH(pe, paths, entry) {
5707 if (pe->path[0] == '\0')
5709 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5711 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5712 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5713 return got_error_path(pe->path,
5714 GOT_ERR_FILE_NOT_STAGED);
5720 const struct got_error *
5721 got_worktree_commit(struct got_object_id **new_commit_id,
5722 struct got_worktree *worktree, struct got_pathlist_head *paths,
5723 const char *author, const char *committer, int allow_bad_symlinks,
5724 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5725 got_worktree_status_cb status_cb, void *status_arg,
5726 struct got_repository *repo)
5728 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5729 struct got_fileindex *fileindex = NULL;
5730 char *fileindex_path = NULL;
5731 struct got_pathlist_head commitable_paths;
5732 struct collect_commitables_arg cc_arg;
5733 struct got_pathlist_entry *pe;
5734 struct got_reference *head_ref = NULL;
5735 struct got_object_id *head_commit_id = NULL;
5736 int have_staged_files = 0;
5738 *new_commit_id = NULL;
5740 TAILQ_INIT(&commitable_paths);
5742 err = lock_worktree(worktree, LOCK_EX);
5746 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5750 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5754 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5758 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5759 &have_staged_files);
5760 if (err && err->code != GOT_ERR_CANCELLED)
5762 if (have_staged_files) {
5763 err = check_non_staged_files(fileindex, paths);
5768 cc_arg.commitable_paths = &commitable_paths;
5769 cc_arg.worktree = worktree;
5770 cc_arg.fileindex = fileindex;
5772 cc_arg.have_staged_files = have_staged_files;
5773 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5774 TAILQ_FOREACH(pe, paths, entry) {
5775 err = worktree_status(worktree, pe->path, fileindex, repo,
5776 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
5781 if (TAILQ_EMPTY(&commitable_paths)) {
5782 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5786 TAILQ_FOREACH(pe, paths, entry) {
5787 err = check_path_is_commitable(pe->path, &commitable_paths);
5792 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5793 struct got_commitable *ct = pe->data;
5794 const char *ct_path = ct->in_repo_path;
5796 while (ct_path[0] == '/')
5798 err = check_out_of_date(ct_path, ct->status,
5799 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5800 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5806 err = commit_worktree(new_commit_id, &commitable_paths,
5807 head_commit_id, NULL, worktree, author, committer,
5808 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5812 err = update_fileindex_after_commit(worktree, &commitable_paths,
5813 *new_commit_id, fileindex, have_staged_files);
5814 sync_err = sync_fileindex(fileindex, fileindex_path);
5815 if (sync_err && err == NULL)
5819 got_fileindex_free(fileindex);
5820 free(fileindex_path);
5821 unlockerr = lock_worktree(worktree, LOCK_SH);
5822 if (unlockerr && err == NULL)
5824 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5825 struct got_commitable *ct = pe->data;
5826 free_commitable(ct);
5828 got_pathlist_free(&commitable_paths);
5833 got_commitable_get_path(struct got_commitable *ct)
5839 got_commitable_get_status(struct got_commitable *ct)
5844 struct check_rebase_ok_arg {
5845 struct got_worktree *worktree;
5846 struct got_repository *repo;
5849 static const struct got_error *
5850 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5852 const struct got_error *err = NULL;
5853 struct check_rebase_ok_arg *a = arg;
5854 unsigned char status;
5858 /* Reject rebase of a work tree with mixed base commits. */
5859 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5860 SHA1_DIGEST_LENGTH))
5861 return got_error(GOT_ERR_MIXED_COMMITS);
5863 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5865 return got_error_from_errno("asprintf");
5867 /* Reject rebase of a work tree with modified or staged files. */
5868 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5873 if (status != GOT_STATUS_NO_CHANGE)
5874 return got_error(GOT_ERR_MODIFIED);
5875 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5876 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5881 const struct got_error *
5882 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5883 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5884 struct got_worktree *worktree, struct got_reference *branch,
5885 struct got_repository *repo)
5887 const struct got_error *err = NULL;
5888 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5889 char *branch_ref_name = NULL;
5890 char *fileindex_path = NULL;
5891 struct check_rebase_ok_arg ok_arg;
5892 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5893 struct got_object_id *wt_branch_tip = NULL;
5895 *new_base_branch_ref = NULL;
5899 err = lock_worktree(worktree, LOCK_EX);
5903 err = open_fileindex(fileindex, &fileindex_path, worktree);
5907 ok_arg.worktree = worktree;
5909 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5914 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5918 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5922 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5926 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5931 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5934 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5935 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5939 err = got_ref_alloc_symref(new_base_branch_ref,
5940 new_base_branch_ref_name, wt_branch);
5943 err = got_ref_write(*new_base_branch_ref, repo);
5947 /* TODO Lock original branch's ref while rebasing? */
5949 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5953 err = got_ref_write(branch_ref, repo);
5957 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5958 worktree->base_commit_id);
5961 err = got_ref_write(*tmp_branch, repo);
5965 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5969 free(fileindex_path);
5970 free(tmp_branch_name);
5971 free(new_base_branch_ref_name);
5972 free(branch_ref_name);
5974 got_ref_close(branch_ref);
5976 got_ref_close(wt_branch);
5977 free(wt_branch_tip);
5979 if (*new_base_branch_ref) {
5980 got_ref_close(*new_base_branch_ref);
5981 *new_base_branch_ref = NULL;
5984 got_ref_close(*tmp_branch);
5988 got_fileindex_free(*fileindex);
5991 lock_worktree(worktree, LOCK_SH);
5996 const struct got_error *
5997 got_worktree_rebase_continue(struct got_object_id **commit_id,
5998 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5999 struct got_reference **branch, struct got_fileindex **fileindex,
6000 struct got_worktree *worktree, struct got_repository *repo)
6002 const struct got_error *err;
6003 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6004 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6005 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6006 char *fileindex_path = NULL;
6007 int have_staged_files = 0;
6010 *new_base_branch = NULL;
6015 err = lock_worktree(worktree, LOCK_EX);
6019 err = open_fileindex(fileindex, &fileindex_path, worktree);
6023 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6024 &have_staged_files);
6025 if (err && err->code != GOT_ERR_CANCELLED)
6027 if (have_staged_files) {
6028 err = got_error(GOT_ERR_STAGED_PATHS);
6032 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6036 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6040 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6044 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6048 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6052 err = got_ref_open(branch, repo,
6053 got_ref_get_symref_target(branch_ref), 0);
6057 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6061 err = got_ref_resolve(commit_id, repo, commit_ref);
6065 err = got_ref_open(new_base_branch, repo,
6066 new_base_branch_ref_name, 0);
6070 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6074 free(commit_ref_name);
6075 free(branch_ref_name);
6076 free(fileindex_path);
6078 got_ref_close(commit_ref);
6080 got_ref_close(branch_ref);
6085 got_ref_close(*tmp_branch);
6088 if (*new_base_branch) {
6089 got_ref_close(*new_base_branch);
6090 *new_base_branch = NULL;
6093 got_ref_close(*branch);
6097 got_fileindex_free(*fileindex);
6100 lock_worktree(worktree, LOCK_SH);
6105 const struct got_error *
6106 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6108 const struct got_error *err;
6109 char *tmp_branch_name = NULL;
6111 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6115 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6116 free(tmp_branch_name);
6120 static const struct got_error *
6121 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6122 char **logmsg, void *arg)
6128 static const struct got_error *
6129 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6130 const char *path, struct got_object_id *blob_id,
6131 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6132 int dirfd, const char *de_name)
6137 struct collect_merged_paths_arg {
6138 got_worktree_checkout_cb progress_cb;
6140 struct got_pathlist_head *merged_paths;
6143 static const struct got_error *
6144 collect_merged_paths(void *arg, unsigned char status, const char *path)
6146 const struct got_error *err;
6147 struct collect_merged_paths_arg *a = arg;
6149 struct got_pathlist_entry *new;
6151 err = (*a->progress_cb)(a->progress_arg, status, path);
6155 if (status != GOT_STATUS_MERGE &&
6156 status != GOT_STATUS_ADD &&
6157 status != GOT_STATUS_DELETE &&
6158 status != GOT_STATUS_CONFLICT)
6163 return got_error_from_errno("strdup");
6165 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6166 if (err || new == NULL)
6172 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6174 struct got_pathlist_entry *pe;
6176 TAILQ_FOREACH(pe, merged_paths, entry)
6177 free((char *)pe->path);
6179 got_pathlist_free(merged_paths);
6182 static const struct got_error *
6183 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6184 int is_rebase, struct got_repository *repo)
6186 const struct got_error *err;
6187 struct got_reference *commit_ref = NULL;
6189 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6191 if (err->code != GOT_ERR_NOT_REF)
6193 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6196 err = got_ref_write(commit_ref, repo);
6199 } else if (is_rebase) {
6200 struct got_object_id *stored_id;
6203 err = got_ref_resolve(&stored_id, repo, commit_ref);
6206 cmp = got_object_id_cmp(commit_id, stored_id);
6209 err = got_error(GOT_ERR_REBASE_COMMITID);
6215 got_ref_close(commit_ref);
6219 static const struct got_error *
6220 rebase_merge_files(struct got_pathlist_head *merged_paths,
6221 const char *commit_ref_name, struct got_worktree *worktree,
6222 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6223 struct got_object_id *commit_id, struct got_repository *repo,
6224 got_worktree_checkout_cb progress_cb, void *progress_arg,
6225 got_cancel_cb cancel_cb, void *cancel_arg)
6227 const struct got_error *err;
6228 struct got_reference *commit_ref = NULL;
6229 struct collect_merged_paths_arg cmp_arg;
6230 char *fileindex_path;
6232 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6234 err = get_fileindex_path(&fileindex_path, worktree);
6238 cmp_arg.progress_cb = progress_cb;
6239 cmp_arg.progress_arg = progress_arg;
6240 cmp_arg.merged_paths = merged_paths;
6241 err = merge_files(worktree, fileindex, fileindex_path,
6242 parent_commit_id, commit_id, repo, collect_merged_paths,
6243 &cmp_arg, cancel_cb, cancel_arg);
6245 got_ref_close(commit_ref);
6249 const struct got_error *
6250 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6251 struct got_worktree *worktree, struct got_fileindex *fileindex,
6252 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6253 struct got_repository *repo,
6254 got_worktree_checkout_cb progress_cb, void *progress_arg,
6255 got_cancel_cb cancel_cb, void *cancel_arg)
6257 const struct got_error *err;
6258 char *commit_ref_name;
6260 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6264 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6268 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6269 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6270 progress_arg, cancel_cb, cancel_arg);
6272 free(commit_ref_name);
6276 const struct got_error *
6277 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6278 struct got_worktree *worktree, struct got_fileindex *fileindex,
6279 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6280 struct got_repository *repo,
6281 got_worktree_checkout_cb progress_cb, void *progress_arg,
6282 got_cancel_cb cancel_cb, void *cancel_arg)
6284 const struct got_error *err;
6285 char *commit_ref_name;
6287 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6291 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6295 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6296 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6297 progress_arg, cancel_cb, cancel_arg);
6299 free(commit_ref_name);
6303 static const struct got_error *
6304 rebase_commit(struct got_object_id **new_commit_id,
6305 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6306 struct got_worktree *worktree, struct got_fileindex *fileindex,
6307 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6308 const char *new_logmsg, struct got_repository *repo)
6310 const struct got_error *err, *sync_err;
6311 struct got_pathlist_head commitable_paths;
6312 struct collect_commitables_arg cc_arg;
6313 char *fileindex_path = NULL;
6314 struct got_reference *head_ref = NULL;
6315 struct got_object_id *head_commit_id = NULL;
6316 char *logmsg = NULL;
6318 TAILQ_INIT(&commitable_paths);
6319 *new_commit_id = NULL;
6321 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6323 err = get_fileindex_path(&fileindex_path, worktree);
6327 cc_arg.commitable_paths = &commitable_paths;
6328 cc_arg.worktree = worktree;
6330 cc_arg.have_staged_files = 0;
6332 * If possible get the status of individual files directly to
6333 * avoid crawling the entire work tree once per rebased commit.
6335 * Ideally, merged_paths would contain a list of commitables
6336 * we could use so we could skip worktree_status() entirely.
6337 * However, we would then need carefully keep track of cumulative
6338 * effects of operations such as file additions and deletions
6339 * in 'got histedit -f' (folding multiple commits into one),
6340 * and this extra complexity is not really worth it.
6343 struct got_pathlist_entry *pe;
6344 TAILQ_FOREACH(pe, merged_paths, entry) {
6345 err = worktree_status(worktree, pe->path, fileindex,
6346 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6352 err = worktree_status(worktree, "", fileindex, repo,
6353 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6358 if (TAILQ_EMPTY(&commitable_paths)) {
6359 /* No-op change; commit will be elided. */
6360 err = got_ref_delete(commit_ref, repo);
6363 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6367 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6371 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6376 logmsg = strdup(new_logmsg);
6377 if (logmsg == NULL) {
6378 err = got_error_from_errno("strdup");
6382 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6387 /* NB: commit_worktree will call free(logmsg) */
6388 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6389 NULL, worktree, got_object_commit_get_author(orig_commit),
6390 got_object_commit_get_committer(orig_commit),
6391 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6395 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6399 err = got_ref_delete(commit_ref, repo);
6403 err = update_fileindex_after_commit(worktree, &commitable_paths,
6404 *new_commit_id, fileindex, 0);
6405 sync_err = sync_fileindex(fileindex, fileindex_path);
6406 if (sync_err && err == NULL)
6409 free(fileindex_path);
6410 free(head_commit_id);
6412 got_ref_close(head_ref);
6414 free(*new_commit_id);
6415 *new_commit_id = NULL;
6420 const struct got_error *
6421 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6422 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6423 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6424 struct got_commit_object *orig_commit,
6425 struct got_object_id *orig_commit_id, struct got_repository *repo)
6427 const struct got_error *err;
6428 char *commit_ref_name;
6429 struct got_reference *commit_ref = NULL;
6430 struct got_object_id *commit_id = NULL;
6432 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6436 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6439 err = got_ref_resolve(&commit_id, repo, commit_ref);
6442 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6443 err = got_error(GOT_ERR_REBASE_COMMITID);
6447 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6448 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6451 got_ref_close(commit_ref);
6452 free(commit_ref_name);
6457 const struct got_error *
6458 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6459 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6460 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6461 struct got_commit_object *orig_commit,
6462 struct got_object_id *orig_commit_id, const char *new_logmsg,
6463 struct got_repository *repo)
6465 const struct got_error *err;
6466 char *commit_ref_name;
6467 struct got_reference *commit_ref = NULL;
6469 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6473 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6477 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6478 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6481 got_ref_close(commit_ref);
6482 free(commit_ref_name);
6486 const struct got_error *
6487 got_worktree_rebase_postpone(struct got_worktree *worktree,
6488 struct got_fileindex *fileindex)
6491 got_fileindex_free(fileindex);
6492 return lock_worktree(worktree, LOCK_SH);
6495 static const struct got_error *
6496 delete_ref(const char *name, struct got_repository *repo)
6498 const struct got_error *err;
6499 struct got_reference *ref;
6501 err = got_ref_open(&ref, repo, name, 0);
6503 if (err->code == GOT_ERR_NOT_REF)
6508 err = got_ref_delete(ref, repo);
6513 static const struct got_error *
6514 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6516 const struct got_error *err;
6517 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6518 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6520 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6523 err = delete_ref(tmp_branch_name, repo);
6527 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6530 err = delete_ref(new_base_branch_ref_name, repo);
6534 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6537 err = delete_ref(branch_ref_name, repo);
6541 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6544 err = delete_ref(commit_ref_name, repo);
6549 free(tmp_branch_name);
6550 free(new_base_branch_ref_name);
6551 free(branch_ref_name);
6552 free(commit_ref_name);
6556 const struct got_error *
6557 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6558 struct got_object_id *new_commit_id, struct got_repository *repo)
6560 const struct got_error *err;
6561 struct got_reference *ref = NULL;
6562 struct got_object_id *old_commit_id = NULL;
6563 const char *branch_name = NULL;
6564 char *new_id_str = NULL;
6565 char *refname = NULL;
6567 branch_name = got_ref_get_name(branch);
6568 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6569 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6572 err = got_object_id_str(&new_id_str, new_commit_id);
6576 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6577 new_id_str) == -1) {
6578 err = got_error_from_errno("asprintf");
6582 err = got_ref_resolve(&old_commit_id, repo, branch);
6586 err = got_ref_alloc(&ref, refname, old_commit_id);
6590 err = got_ref_write(ref, repo);
6594 free(old_commit_id);
6600 const struct got_error *
6601 got_worktree_rebase_complete(struct got_worktree *worktree,
6602 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6603 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6604 struct got_repository *repo, int create_backup)
6606 const struct got_error *err, *unlockerr, *sync_err;
6607 struct got_object_id *new_head_commit_id = NULL;
6608 char *fileindex_path = NULL;
6610 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6614 if (create_backup) {
6615 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6616 rebased_branch, new_head_commit_id, repo);
6621 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6625 err = got_ref_write(rebased_branch, repo);
6629 err = got_worktree_set_head_ref(worktree, rebased_branch);
6633 err = delete_rebase_refs(worktree, repo);
6637 err = get_fileindex_path(&fileindex_path, worktree);
6640 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6641 sync_err = sync_fileindex(fileindex, fileindex_path);
6642 if (sync_err && err == NULL)
6645 got_fileindex_free(fileindex);
6646 free(fileindex_path);
6647 free(new_head_commit_id);
6648 unlockerr = lock_worktree(worktree, LOCK_SH);
6649 if (unlockerr && err == NULL)
6654 const struct got_error *
6655 got_worktree_rebase_abort(struct got_worktree *worktree,
6656 struct got_fileindex *fileindex, struct got_repository *repo,
6657 struct got_reference *new_base_branch,
6658 got_worktree_checkout_cb progress_cb, void *progress_arg)
6660 const struct got_error *err, *unlockerr, *sync_err;
6661 struct got_reference *resolved = NULL;
6662 struct got_object_id *commit_id = NULL;
6663 char *fileindex_path = NULL;
6664 struct revert_file_args rfa;
6665 struct got_object_id *tree_id = NULL;
6667 err = lock_worktree(worktree, LOCK_EX);
6671 err = got_ref_open(&resolved, repo,
6672 got_ref_get_symref_target(new_base_branch), 0);
6676 err = got_worktree_set_head_ref(worktree, resolved);
6681 * XXX commits to the base branch could have happened while
6682 * we were busy rebasing; should we store the original commit ID
6683 * when rebase begins and read it back here?
6685 err = got_ref_resolve(&commit_id, repo, resolved);
6689 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6693 err = got_object_id_by_path(&tree_id, repo,
6694 worktree->base_commit_id, worktree->path_prefix);
6698 err = delete_rebase_refs(worktree, repo);
6702 err = get_fileindex_path(&fileindex_path, worktree);
6706 rfa.worktree = worktree;
6707 rfa.fileindex = fileindex;
6708 rfa.progress_cb = progress_cb;
6709 rfa.progress_arg = progress_arg;
6710 rfa.patch_cb = NULL;
6711 rfa.patch_arg = NULL;
6713 rfa.unlink_added_files = 0;
6714 err = worktree_status(worktree, "", fileindex, repo,
6715 revert_file, &rfa, NULL, NULL, 1, 0);
6719 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6720 repo, progress_cb, progress_arg, NULL, NULL);
6722 sync_err = sync_fileindex(fileindex, fileindex_path);
6723 if (sync_err && err == NULL)
6726 got_ref_close(resolved);
6730 got_fileindex_free(fileindex);
6731 free(fileindex_path);
6733 unlockerr = lock_worktree(worktree, LOCK_SH);
6734 if (unlockerr && err == NULL)
6739 const struct got_error *
6740 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6741 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6742 struct got_fileindex **fileindex, struct got_worktree *worktree,
6743 struct got_repository *repo)
6745 const struct got_error *err = NULL;
6746 char *tmp_branch_name = NULL;
6747 char *branch_ref_name = NULL;
6748 char *base_commit_ref_name = NULL;
6749 char *fileindex_path = NULL;
6750 struct check_rebase_ok_arg ok_arg;
6751 struct got_reference *wt_branch = NULL;
6752 struct got_reference *base_commit_ref = NULL;
6756 *base_commit_id = NULL;
6759 err = lock_worktree(worktree, LOCK_EX);
6763 err = open_fileindex(fileindex, &fileindex_path, worktree);
6767 ok_arg.worktree = worktree;
6769 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6774 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6778 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6782 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6787 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6792 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6796 err = got_ref_write(*branch_ref, repo);
6800 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6801 worktree->base_commit_id);
6804 err = got_ref_write(base_commit_ref, repo);
6807 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6808 if (*base_commit_id == NULL) {
6809 err = got_error_from_errno("got_object_id_dup");
6813 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6814 worktree->base_commit_id);
6817 err = got_ref_write(*tmp_branch, repo);
6821 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6825 free(fileindex_path);
6826 free(tmp_branch_name);
6827 free(branch_ref_name);
6828 free(base_commit_ref_name);
6830 got_ref_close(wt_branch);
6833 got_ref_close(*branch_ref);
6837 got_ref_close(*tmp_branch);
6840 free(*base_commit_id);
6842 got_fileindex_free(*fileindex);
6845 lock_worktree(worktree, LOCK_SH);
6850 const struct got_error *
6851 got_worktree_histedit_postpone(struct got_worktree *worktree,
6852 struct got_fileindex *fileindex)
6855 got_fileindex_free(fileindex);
6856 return lock_worktree(worktree, LOCK_SH);
6859 const struct got_error *
6860 got_worktree_histedit_in_progress(int *in_progress,
6861 struct got_worktree *worktree)
6863 const struct got_error *err;
6864 char *tmp_branch_name = NULL;
6866 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6870 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6871 free(tmp_branch_name);
6875 const struct got_error *
6876 got_worktree_histedit_continue(struct got_object_id **commit_id,
6877 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6878 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6879 struct got_worktree *worktree, struct got_repository *repo)
6881 const struct got_error *err;
6882 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6883 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6884 struct got_reference *commit_ref = NULL;
6885 struct got_reference *base_commit_ref = NULL;
6886 char *fileindex_path = NULL;
6887 int have_staged_files = 0;
6891 *base_commit_id = NULL;
6894 err = lock_worktree(worktree, LOCK_EX);
6898 err = open_fileindex(fileindex, &fileindex_path, worktree);
6902 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6903 &have_staged_files);
6904 if (err && err->code != GOT_ERR_CANCELLED)
6906 if (have_staged_files) {
6907 err = got_error(GOT_ERR_STAGED_PATHS);
6911 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6915 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6919 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6923 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6928 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6932 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6935 err = got_ref_resolve(commit_id, repo, commit_ref);
6939 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6942 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6946 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6950 free(commit_ref_name);
6951 free(branch_ref_name);
6952 free(fileindex_path);
6954 got_ref_close(commit_ref);
6955 if (base_commit_ref)
6956 got_ref_close(base_commit_ref);
6960 free(*base_commit_id);
6961 *base_commit_id = NULL;
6963 got_ref_close(*tmp_branch);
6967 got_fileindex_free(*fileindex);
6970 lock_worktree(worktree, LOCK_EX);
6975 static const struct got_error *
6976 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6978 const struct got_error *err;
6979 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6980 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6982 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6985 err = delete_ref(tmp_branch_name, repo);
6989 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6993 err = delete_ref(base_commit_ref_name, repo);
6997 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7000 err = delete_ref(branch_ref_name, repo);
7004 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7007 err = delete_ref(commit_ref_name, repo);
7011 free(tmp_branch_name);
7012 free(base_commit_ref_name);
7013 free(branch_ref_name);
7014 free(commit_ref_name);
7018 const struct got_error *
7019 got_worktree_histedit_abort(struct got_worktree *worktree,
7020 struct got_fileindex *fileindex, struct got_repository *repo,
7021 struct got_reference *branch, struct got_object_id *base_commit_id,
7022 got_worktree_checkout_cb progress_cb, void *progress_arg)
7024 const struct got_error *err, *unlockerr, *sync_err;
7025 struct got_reference *resolved = NULL;
7026 char *fileindex_path = NULL;
7027 struct got_object_id *tree_id = NULL;
7028 struct revert_file_args rfa;
7030 err = lock_worktree(worktree, LOCK_EX);
7034 err = got_ref_open(&resolved, repo,
7035 got_ref_get_symref_target(branch), 0);
7039 err = got_worktree_set_head_ref(worktree, resolved);
7043 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7047 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
7048 worktree->path_prefix);
7052 err = delete_histedit_refs(worktree, repo);
7056 err = get_fileindex_path(&fileindex_path, worktree);
7060 rfa.worktree = worktree;
7061 rfa.fileindex = fileindex;
7062 rfa.progress_cb = progress_cb;
7063 rfa.progress_arg = progress_arg;
7064 rfa.patch_cb = NULL;
7065 rfa.patch_arg = NULL;
7067 rfa.unlink_added_files = 0;
7068 err = worktree_status(worktree, "", fileindex, repo,
7069 revert_file, &rfa, NULL, NULL, 1, 0);
7073 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7074 repo, progress_cb, progress_arg, NULL, NULL);
7076 sync_err = sync_fileindex(fileindex, fileindex_path);
7077 if (sync_err && err == NULL)
7080 got_ref_close(resolved);
7082 free(fileindex_path);
7084 unlockerr = lock_worktree(worktree, LOCK_SH);
7085 if (unlockerr && err == NULL)
7090 const struct got_error *
7091 got_worktree_histedit_complete(struct got_worktree *worktree,
7092 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7093 struct got_reference *edited_branch, struct got_repository *repo)
7095 const struct got_error *err, *unlockerr, *sync_err;
7096 struct got_object_id *new_head_commit_id = NULL;
7097 struct got_reference *resolved = NULL;
7098 char *fileindex_path = NULL;
7100 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7104 err = got_ref_open(&resolved, repo,
7105 got_ref_get_symref_target(edited_branch), 0);
7109 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7110 resolved, new_head_commit_id, repo);
7114 err = got_ref_change_ref(resolved, new_head_commit_id);
7118 err = got_ref_write(resolved, repo);
7122 err = got_worktree_set_head_ref(worktree, resolved);
7126 err = delete_histedit_refs(worktree, repo);
7130 err = get_fileindex_path(&fileindex_path, worktree);
7133 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7134 sync_err = sync_fileindex(fileindex, fileindex_path);
7135 if (sync_err && err == NULL)
7138 got_fileindex_free(fileindex);
7139 free(fileindex_path);
7140 free(new_head_commit_id);
7141 unlockerr = lock_worktree(worktree, LOCK_SH);
7142 if (unlockerr && err == NULL)
7147 const struct got_error *
7148 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7149 struct got_object_id *commit_id, struct got_repository *repo)
7151 const struct got_error *err;
7152 char *commit_ref_name;
7154 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7158 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7162 err = delete_ref(commit_ref_name, repo);
7164 free(commit_ref_name);
7168 const struct got_error *
7169 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7170 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7171 struct got_worktree *worktree, const char *refname,
7172 struct got_repository *repo)
7174 const struct got_error *err = NULL;
7175 char *fileindex_path = NULL;
7176 struct check_rebase_ok_arg ok_arg;
7180 *base_branch_ref = NULL;
7182 err = lock_worktree(worktree, LOCK_EX);
7186 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7187 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7188 "cannot integrate a branch into itself; "
7189 "update -b or different branch name required");
7193 err = open_fileindex(fileindex, &fileindex_path, worktree);
7197 /* Preconditions are the same as for rebase. */
7198 ok_arg.worktree = worktree;
7200 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7205 err = got_ref_open(branch_ref, repo, refname, 1);
7209 err = got_ref_open(base_branch_ref, repo,
7210 got_worktree_get_head_ref_name(worktree), 1);
7214 got_ref_close(*branch_ref);
7217 if (*base_branch_ref) {
7218 got_ref_close(*base_branch_ref);
7219 *base_branch_ref = NULL;
7222 got_fileindex_free(*fileindex);
7225 lock_worktree(worktree, LOCK_SH);
7230 const struct got_error *
7231 got_worktree_integrate_continue(struct got_worktree *worktree,
7232 struct got_fileindex *fileindex, struct got_repository *repo,
7233 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7234 got_worktree_checkout_cb progress_cb, void *progress_arg,
7235 got_cancel_cb cancel_cb, void *cancel_arg)
7237 const struct got_error *err = NULL, *sync_err, *unlockerr;
7238 char *fileindex_path = NULL;
7239 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7241 err = get_fileindex_path(&fileindex_path, worktree);
7245 err = got_ref_resolve(&commit_id, repo, branch_ref);
7249 err = got_object_id_by_path(&tree_id, repo, commit_id,
7250 worktree->path_prefix);
7254 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7258 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7259 progress_cb, progress_arg, cancel_cb, cancel_arg);
7263 err = got_ref_change_ref(base_branch_ref, commit_id);
7267 err = got_ref_write(base_branch_ref, repo);
7271 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7273 sync_err = sync_fileindex(fileindex, fileindex_path);
7274 if (sync_err && err == NULL)
7278 unlockerr = got_ref_unlock(branch_ref);
7279 if (unlockerr && err == NULL)
7281 got_ref_close(branch_ref);
7283 unlockerr = got_ref_unlock(base_branch_ref);
7284 if (unlockerr && err == NULL)
7286 got_ref_close(base_branch_ref);
7288 got_fileindex_free(fileindex);
7289 free(fileindex_path);
7292 unlockerr = lock_worktree(worktree, LOCK_SH);
7293 if (unlockerr && err == NULL)
7298 const struct got_error *
7299 got_worktree_integrate_abort(struct got_worktree *worktree,
7300 struct got_fileindex *fileindex, struct got_repository *repo,
7301 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7303 const struct got_error *err = NULL, *unlockerr = NULL;
7305 got_fileindex_free(fileindex);
7307 err = lock_worktree(worktree, LOCK_SH);
7309 unlockerr = got_ref_unlock(branch_ref);
7310 if (unlockerr && err == NULL)
7312 got_ref_close(branch_ref);
7314 unlockerr = got_ref_unlock(base_branch_ref);
7315 if (unlockerr && err == NULL)
7317 got_ref_close(base_branch_ref);
7322 const struct got_error *
7323 got_worktree_merge_postpone(struct got_worktree *worktree,
7324 struct got_fileindex *fileindex)
7326 const struct got_error *err, *sync_err;
7327 char *fileindex_path = NULL;
7329 err = get_fileindex_path(&fileindex_path, worktree);
7333 sync_err = sync_fileindex(fileindex, fileindex_path);
7335 err = lock_worktree(worktree, LOCK_SH);
7336 if (sync_err && err == NULL)
7339 got_fileindex_free(fileindex);
7340 free(fileindex_path);
7344 static const struct got_error *
7345 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7347 const struct got_error *err;
7348 char *branch_refname = NULL, *commit_refname = NULL;
7350 err = get_merge_branch_ref_name(&branch_refname, worktree);
7353 err = delete_ref(branch_refname, repo);
7357 err = get_merge_commit_ref_name(&commit_refname, worktree);
7360 err = delete_ref(commit_refname, repo);
7365 free(branch_refname);
7366 free(commit_refname);
7370 struct merge_commit_msg_arg {
7371 struct got_worktree *worktree;
7372 const char *branch_name;
7375 static const struct got_error *
7376 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths, char **logmsg,
7379 struct merge_commit_msg_arg *a = arg;
7381 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7382 got_worktree_get_head_ref_name(a->worktree)) == -1)
7383 return got_error_from_errno("asprintf");
7389 const struct got_error *
7390 got_worktree_merge_branch(struct got_worktree *worktree,
7391 struct got_fileindex *fileindex,
7392 struct got_object_id *yca_commit_id,
7393 struct got_object_id *branch_tip,
7394 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7395 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7397 const struct got_error *err;
7398 char *fileindex_path = NULL;
7400 err = get_fileindex_path(&fileindex_path, worktree);
7404 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7409 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7410 branch_tip, repo, progress_cb, progress_arg,
7411 cancel_cb, cancel_arg);
7413 free(fileindex_path);
7417 const struct got_error *
7418 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7419 struct got_worktree *worktree, struct got_fileindex *fileindex,
7420 const char *author, const char *committer, int allow_bad_symlinks,
7421 struct got_object_id *branch_tip, const char *branch_name,
7422 struct got_repository *repo,
7423 got_worktree_status_cb status_cb, void *status_arg)
7426 const struct got_error *err = NULL, *sync_err;
7427 struct got_pathlist_head commitable_paths;
7428 struct collect_commitables_arg cc_arg;
7429 struct got_pathlist_entry *pe;
7430 struct got_reference *head_ref = NULL;
7431 struct got_object_id *head_commit_id = NULL;
7432 int have_staged_files = 0;
7433 struct merge_commit_msg_arg mcm_arg;
7434 char *fileindex_path = NULL;
7436 *new_commit_id = NULL;
7438 TAILQ_INIT(&commitable_paths);
7440 err = get_fileindex_path(&fileindex_path, worktree);
7444 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7448 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7452 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7453 &have_staged_files);
7454 if (err && err->code != GOT_ERR_CANCELLED)
7456 if (have_staged_files) {
7457 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7461 cc_arg.commitable_paths = &commitable_paths;
7462 cc_arg.worktree = worktree;
7463 cc_arg.fileindex = fileindex;
7465 cc_arg.have_staged_files = have_staged_files;
7466 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7467 err = worktree_status(worktree, "", fileindex, repo,
7468 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7472 if (TAILQ_EMPTY(&commitable_paths)) {
7473 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7474 "merge of %s cannot proceed", branch_name);
7478 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7479 struct got_commitable *ct = pe->data;
7480 const char *ct_path = ct->in_repo_path;
7482 while (ct_path[0] == '/')
7484 err = check_out_of_date(ct_path, ct->status,
7485 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7486 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7492 mcm_arg.worktree = worktree;
7493 mcm_arg.branch_name = branch_name;
7494 err = commit_worktree(new_commit_id, &commitable_paths,
7495 head_commit_id, branch_tip, worktree, author, committer,
7496 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7500 err = update_fileindex_after_commit(worktree, &commitable_paths,
7501 *new_commit_id, fileindex, have_staged_files);
7502 sync_err = sync_fileindex(fileindex, fileindex_path);
7503 if (sync_err && err == NULL)
7506 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7507 struct got_commitable *ct = pe->data;
7508 free_commitable(ct);
7510 got_pathlist_free(&commitable_paths);
7511 free(fileindex_path);
7515 const struct got_error *
7516 got_worktree_merge_complete(struct got_worktree *worktree,
7517 struct got_fileindex *fileindex, struct got_repository *repo)
7519 const struct got_error *err, *unlockerr, *sync_err;
7520 char *fileindex_path = NULL;
7522 err = delete_merge_refs(worktree, repo);
7526 err = get_fileindex_path(&fileindex_path, worktree);
7529 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7530 sync_err = sync_fileindex(fileindex, fileindex_path);
7531 if (sync_err && err == NULL)
7534 got_fileindex_free(fileindex);
7535 free(fileindex_path);
7536 unlockerr = lock_worktree(worktree, LOCK_SH);
7537 if (unlockerr && err == NULL)
7542 const struct got_error *
7543 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7544 struct got_repository *repo)
7546 const struct got_error *err;
7547 char *branch_refname = NULL;
7548 struct got_reference *branch_ref = NULL;
7552 err = get_merge_branch_ref_name(&branch_refname, worktree);
7555 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7556 free(branch_refname);
7558 if (err->code != GOT_ERR_NOT_REF)
7566 const struct got_error *got_worktree_merge_prepare(
7567 struct got_fileindex **fileindex, struct got_worktree *worktree,
7568 struct got_reference *branch, struct got_repository *repo)
7570 const struct got_error *err = NULL;
7571 char *fileindex_path = NULL;
7572 char *branch_refname = NULL, *commit_refname = NULL;
7573 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
7574 struct got_reference *commit_ref = NULL;
7575 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
7576 struct check_rebase_ok_arg ok_arg;
7580 err = lock_worktree(worktree, LOCK_EX);
7584 err = open_fileindex(fileindex, &fileindex_path, worktree);
7588 /* Preconditions are the same as for rebase. */
7589 ok_arg.worktree = worktree;
7591 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7596 err = get_merge_branch_ref_name(&branch_refname, worktree);
7600 err = get_merge_commit_ref_name(&commit_refname, worktree);
7604 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7609 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
7613 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
7614 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
7618 err = got_ref_resolve(&branch_tip, repo, branch);
7622 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
7625 err = got_ref_write(branch_ref, repo);
7629 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
7632 err = got_ref_write(commit_ref, repo);
7637 free(branch_refname);
7638 free(commit_refname);
7639 free(fileindex_path);
7641 got_ref_close(branch_ref);
7643 got_ref_close(commit_ref);
7645 got_ref_close(wt_branch);
7646 free(wt_branch_tip);
7649 got_fileindex_free(*fileindex);
7652 lock_worktree(worktree, LOCK_SH);
7657 const struct got_error *
7658 got_worktree_merge_continue(char **branch_name,
7659 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
7660 struct got_worktree *worktree, struct got_repository *repo)
7662 const struct got_error *err;
7663 char *commit_refname = NULL, *branch_refname = NULL;
7664 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
7665 char *fileindex_path = NULL;
7666 int have_staged_files = 0;
7668 *branch_name = NULL;
7672 err = lock_worktree(worktree, LOCK_EX);
7676 err = open_fileindex(fileindex, &fileindex_path, worktree);
7680 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7681 &have_staged_files);
7682 if (err && err->code != GOT_ERR_CANCELLED)
7684 if (have_staged_files) {
7685 err = got_error(GOT_ERR_STAGED_PATHS);
7689 err = get_merge_branch_ref_name(&branch_refname, worktree);
7693 err = get_merge_commit_ref_name(&commit_refname, worktree);
7697 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7701 if (!got_ref_is_symbolic(branch_ref)) {
7702 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7703 "%s is not a symbolic reference",
7704 got_ref_get_name(branch_ref));
7707 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
7708 if (*branch_name == NULL) {
7709 err = got_error_from_errno("strdup");
7713 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
7717 err = got_ref_resolve(branch_tip, repo, commit_ref);
7721 free(commit_refname);
7722 free(branch_refname);
7723 free(fileindex_path);
7725 got_ref_close(commit_ref);
7727 got_ref_close(branch_ref);
7731 *branch_name = NULL;
7736 got_fileindex_free(*fileindex);
7739 lock_worktree(worktree, LOCK_SH);
7744 const struct got_error *
7745 got_worktree_merge_abort(struct got_worktree *worktree,
7746 struct got_fileindex *fileindex, struct got_repository *repo,
7747 got_worktree_checkout_cb progress_cb, void *progress_arg)
7749 const struct got_error *err, *unlockerr, *sync_err;
7750 struct got_object_id *commit_id = NULL;
7751 char *fileindex_path = NULL;
7752 struct revert_file_args rfa;
7753 struct got_object_id *tree_id = NULL;
7755 err = got_object_id_by_path(&tree_id, repo,
7756 worktree->base_commit_id, worktree->path_prefix);
7760 err = delete_merge_refs(worktree, repo);
7764 err = get_fileindex_path(&fileindex_path, worktree);
7768 rfa.worktree = worktree;
7769 rfa.fileindex = fileindex;
7770 rfa.progress_cb = progress_cb;
7771 rfa.progress_arg = progress_arg;
7772 rfa.patch_cb = NULL;
7773 rfa.patch_arg = NULL;
7775 rfa.unlink_added_files = 1;
7776 err = worktree_status(worktree, "", fileindex, repo,
7777 revert_file, &rfa, NULL, NULL, 1, 0);
7781 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7782 repo, progress_cb, progress_arg, NULL, NULL);
7784 sync_err = sync_fileindex(fileindex, fileindex_path);
7785 if (sync_err && err == NULL)
7791 got_fileindex_free(fileindex);
7792 free(fileindex_path);
7794 unlockerr = lock_worktree(worktree, LOCK_SH);
7795 if (unlockerr && err == NULL)
7800 struct check_stage_ok_arg {
7801 struct got_object_id *head_commit_id;
7802 struct got_worktree *worktree;
7803 struct got_fileindex *fileindex;
7804 struct got_repository *repo;
7808 const struct got_error *
7809 check_stage_ok(void *arg, unsigned char status,
7810 unsigned char staged_status, const char *relpath,
7811 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7812 struct got_object_id *commit_id, int dirfd, const char *de_name)
7814 struct check_stage_ok_arg *a = arg;
7815 const struct got_error *err = NULL;
7816 struct got_fileindex_entry *ie;
7817 struct got_object_id base_commit_id;
7818 struct got_object_id *base_commit_idp = NULL;
7819 char *in_repo_path = NULL, *p;
7821 if (status == GOT_STATUS_UNVERSIONED ||
7822 status == GOT_STATUS_NO_CHANGE)
7824 if (status == GOT_STATUS_NONEXISTENT)
7825 return got_error_set_errno(ENOENT, relpath);
7827 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7829 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7831 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7832 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7834 return got_error_from_errno("asprintf");
7836 if (got_fileindex_entry_has_commit(ie)) {
7837 memcpy(base_commit_id.sha1, ie->commit_sha1,
7838 SHA1_DIGEST_LENGTH);
7839 base_commit_idp = &base_commit_id;
7842 if (status == GOT_STATUS_CONFLICT) {
7843 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7845 } else if (status != GOT_STATUS_ADD &&
7846 status != GOT_STATUS_MODIFY &&
7847 status != GOT_STATUS_DELETE) {
7848 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7852 a->have_changes = 1;
7857 err = check_out_of_date(p, status, staged_status,
7858 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7859 GOT_ERR_STAGE_OUT_OF_DATE);
7865 struct stage_path_arg {
7866 struct got_worktree *worktree;
7867 struct got_fileindex *fileindex;
7868 struct got_repository *repo;
7869 got_worktree_status_cb status_cb;
7871 got_worktree_patch_cb patch_cb;
7873 int staged_something;
7874 int allow_bad_symlinks;
7877 static const struct got_error *
7878 stage_path(void *arg, unsigned char status,
7879 unsigned char staged_status, const char *relpath,
7880 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7881 struct got_object_id *commit_id, int dirfd, const char *de_name)
7883 struct stage_path_arg *a = arg;
7884 const struct got_error *err = NULL;
7885 struct got_fileindex_entry *ie;
7886 char *ondisk_path = NULL, *path_content = NULL;
7888 struct got_object_id *new_staged_blob_id = NULL;
7891 if (status == GOT_STATUS_UNVERSIONED)
7894 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7896 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7898 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7900 return got_error_from_errno("asprintf");
7903 case GOT_STATUS_ADD:
7904 case GOT_STATUS_MODIFY:
7905 /* XXX could sb.st_mode be passed in by our caller? */
7906 if (lstat(ondisk_path, &sb) == -1) {
7907 err = got_error_from_errno2("lstat", ondisk_path);
7911 if (status == GOT_STATUS_ADD) {
7912 int choice = GOT_PATCH_CHOICE_NONE;
7913 err = (*a->patch_cb)(&choice, a->patch_arg,
7914 status, ie->path, NULL, 1, 1);
7917 if (choice != GOT_PATCH_CHOICE_YES)
7920 err = create_patched_content(&path_content, 0,
7921 staged_blob_id ? staged_blob_id : blob_id,
7922 ondisk_path, dirfd, de_name, ie->path,
7923 a->repo, a->patch_cb, a->patch_arg);
7924 if (err || path_content == NULL)
7928 err = got_object_blob_create(&new_staged_blob_id,
7929 path_content ? path_content : ondisk_path, a->repo);
7932 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7933 SHA1_DIGEST_LENGTH);
7934 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7935 stage = GOT_FILEIDX_STAGE_ADD;
7937 stage = GOT_FILEIDX_STAGE_MODIFY;
7938 got_fileindex_entry_stage_set(ie, stage);
7939 if (S_ISLNK(sb.st_mode)) {
7940 int is_bad_symlink = 0;
7941 if (!a->allow_bad_symlinks) {
7942 char target_path[PATH_MAX];
7944 target_len = readlink(ondisk_path, target_path,
7945 sizeof(target_path));
7946 if (target_len == -1) {
7947 err = got_error_from_errno2("readlink",
7951 err = is_bad_symlink_target(&is_bad_symlink,
7952 target_path, target_len, ondisk_path,
7953 a->worktree->root_path);
7956 if (is_bad_symlink) {
7957 err = got_error_path(ondisk_path,
7958 GOT_ERR_BAD_SYMLINK);
7963 got_fileindex_entry_staged_filetype_set(ie,
7964 GOT_FILEIDX_MODE_BAD_SYMLINK);
7966 got_fileindex_entry_staged_filetype_set(ie,
7967 GOT_FILEIDX_MODE_SYMLINK);
7969 got_fileindex_entry_staged_filetype_set(ie,
7970 GOT_FILEIDX_MODE_REGULAR_FILE);
7972 a->staged_something = 1;
7973 if (a->status_cb == NULL)
7975 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7976 get_staged_status(ie), relpath, blob_id,
7977 new_staged_blob_id, NULL, dirfd, de_name);
7979 case GOT_STATUS_DELETE:
7980 if (staged_status == GOT_STATUS_DELETE)
7983 int choice = GOT_PATCH_CHOICE_NONE;
7984 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7985 ie->path, NULL, 1, 1);
7988 if (choice == GOT_PATCH_CHOICE_NO)
7990 if (choice != GOT_PATCH_CHOICE_YES) {
7991 err = got_error(GOT_ERR_PATCH_CHOICE);
7995 stage = GOT_FILEIDX_STAGE_DELETE;
7996 got_fileindex_entry_stage_set(ie, stage);
7997 a->staged_something = 1;
7998 if (a->status_cb == NULL)
8000 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8001 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8004 case GOT_STATUS_NO_CHANGE:
8006 case GOT_STATUS_CONFLICT:
8007 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8009 case GOT_STATUS_NONEXISTENT:
8010 err = got_error_set_errno(ENOENT, relpath);
8013 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8017 if (path_content && unlink(path_content) == -1 && err == NULL)
8018 err = got_error_from_errno2("unlink", path_content);
8021 free(new_staged_blob_id);
8025 const struct got_error *
8026 got_worktree_stage(struct got_worktree *worktree,
8027 struct got_pathlist_head *paths,
8028 got_worktree_status_cb status_cb, void *status_arg,
8029 got_worktree_patch_cb patch_cb, void *patch_arg,
8030 int allow_bad_symlinks, struct got_repository *repo)
8032 const struct got_error *err = NULL, *sync_err, *unlockerr;
8033 struct got_pathlist_entry *pe;
8034 struct got_fileindex *fileindex = NULL;
8035 char *fileindex_path = NULL;
8036 struct got_reference *head_ref = NULL;
8037 struct got_object_id *head_commit_id = NULL;
8038 struct check_stage_ok_arg oka;
8039 struct stage_path_arg spa;
8041 err = lock_worktree(worktree, LOCK_EX);
8045 err = got_ref_open(&head_ref, repo,
8046 got_worktree_get_head_ref_name(worktree), 0);
8049 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8052 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8056 /* Check pre-conditions before staging anything. */
8057 oka.head_commit_id = head_commit_id;
8058 oka.worktree = worktree;
8059 oka.fileindex = fileindex;
8061 oka.have_changes = 0;
8062 TAILQ_FOREACH(pe, paths, entry) {
8063 err = worktree_status(worktree, pe->path, fileindex, repo,
8064 check_stage_ok, &oka, NULL, NULL, 1, 0);
8068 if (!oka.have_changes) {
8069 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8073 spa.worktree = worktree;
8074 spa.fileindex = fileindex;
8076 spa.patch_cb = patch_cb;
8077 spa.patch_arg = patch_arg;
8078 spa.status_cb = status_cb;
8079 spa.status_arg = status_arg;
8080 spa.staged_something = 0;
8081 spa.allow_bad_symlinks = allow_bad_symlinks;
8082 TAILQ_FOREACH(pe, paths, entry) {
8083 err = worktree_status(worktree, pe->path, fileindex, repo,
8084 stage_path, &spa, NULL, NULL, 1, 0);
8088 if (!spa.staged_something) {
8089 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8093 sync_err = sync_fileindex(fileindex, fileindex_path);
8094 if (sync_err && err == NULL)
8098 got_ref_close(head_ref);
8099 free(head_commit_id);
8100 free(fileindex_path);
8102 got_fileindex_free(fileindex);
8103 unlockerr = lock_worktree(worktree, LOCK_SH);
8104 if (unlockerr && err == NULL)
8109 struct unstage_path_arg {
8110 struct got_worktree *worktree;
8111 struct got_fileindex *fileindex;
8112 struct got_repository *repo;
8113 got_worktree_checkout_cb progress_cb;
8115 got_worktree_patch_cb patch_cb;
8119 static const struct got_error *
8120 create_unstaged_content(char **path_unstaged_content,
8121 char **path_new_staged_content, struct got_object_id *blob_id,
8122 struct got_object_id *staged_blob_id, const char *relpath,
8123 struct got_repository *repo,
8124 got_worktree_patch_cb patch_cb, void *patch_arg)
8126 const struct got_error *err, *free_err;
8127 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8128 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8129 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8130 struct got_diffreg_result *diffreg_result = NULL;
8131 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8132 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8134 *path_unstaged_content = NULL;
8135 *path_new_staged_content = NULL;
8137 err = got_object_id_str(&label1, blob_id);
8140 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
8144 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
8148 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8152 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
8156 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
8160 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8164 err = got_diff_files(&diffreg_result, f1, label1, f2,
8165 path2, 3, 0, 1, NULL);
8169 err = got_opentemp_named(path_unstaged_content, &outfile,
8170 "got-unstaged-content");
8173 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8174 "got-new-staged-content");
8178 if (fseek(f1, 0L, SEEK_SET) == -1) {
8179 err = got_ferror(f1, GOT_ERR_IO);
8182 if (fseek(f2, 0L, SEEK_SET) == -1) {
8183 err = got_ferror(f2, GOT_ERR_IO);
8186 /* Count the number of actual changes in the diff result. */
8187 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8188 struct diff_chunk_context cc = {};
8189 diff_chunk_context_load_change(&cc, &nchunks_used,
8190 diffreg_result->result, n, 0);
8193 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8195 err = apply_or_reject_change(&choice, &nchunks_used,
8196 diffreg_result->result, n, relpath, f1, f2,
8197 &line_cur1, &line_cur2,
8198 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8201 if (choice == GOT_PATCH_CHOICE_YES)
8204 have_rejected_content = 1;
8205 if (choice == GOT_PATCH_CHOICE_QUIT)
8208 if (have_content || have_rejected_content)
8209 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8210 outfile, rejectfile);
8214 got_object_blob_close(blob);
8216 got_object_blob_close(staged_blob);
8217 free_err = got_diffreg_result_free(diffreg_result);
8218 if (free_err && err == NULL)
8220 if (f1 && fclose(f1) == EOF && err == NULL)
8221 err = got_error_from_errno2("fclose", path1);
8222 if (f2 && fclose(f2) == EOF && err == NULL)
8223 err = got_error_from_errno2("fclose", path2);
8224 if (outfile && fclose(outfile) == EOF && err == NULL)
8225 err = got_error_from_errno2("fclose", *path_unstaged_content);
8226 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8227 err = got_error_from_errno2("fclose", *path_new_staged_content);
8228 if (path1 && unlink(path1) == -1 && err == NULL)
8229 err = got_error_from_errno2("unlink", path1);
8230 if (path2 && unlink(path2) == -1 && err == NULL)
8231 err = got_error_from_errno2("unlink", path2);
8232 if (err || !have_content) {
8233 if (*path_unstaged_content &&
8234 unlink(*path_unstaged_content) == -1 && err == NULL)
8235 err = got_error_from_errno2("unlink",
8236 *path_unstaged_content);
8237 free(*path_unstaged_content);
8238 *path_unstaged_content = NULL;
8240 if (err || !have_content || !have_rejected_content) {
8241 if (*path_new_staged_content &&
8242 unlink(*path_new_staged_content) == -1 && err == NULL)
8243 err = got_error_from_errno2("unlink",
8244 *path_new_staged_content);
8245 free(*path_new_staged_content);
8246 *path_new_staged_content = NULL;
8253 static const struct got_error *
8254 unstage_hunks(struct got_object_id *staged_blob_id,
8255 struct got_blob_object *blob_base,
8256 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8257 const char *ondisk_path, const char *label_orig,
8258 struct got_worktree *worktree, struct got_repository *repo,
8259 got_worktree_patch_cb patch_cb, void *patch_arg,
8260 got_worktree_checkout_cb progress_cb, void *progress_arg)
8262 const struct got_error *err = NULL;
8263 char *path_unstaged_content = NULL;
8264 char *path_new_staged_content = NULL;
8265 char *parent = NULL, *base_path = NULL;
8266 char *blob_base_path = NULL;
8267 struct got_object_id *new_staged_blob_id = NULL;
8268 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8271 err = create_unstaged_content(&path_unstaged_content,
8272 &path_new_staged_content, blob_id, staged_blob_id,
8273 ie->path, repo, patch_cb, patch_arg);
8277 if (path_unstaged_content == NULL)
8280 if (path_new_staged_content) {
8281 err = got_object_blob_create(&new_staged_blob_id,
8282 path_new_staged_content, repo);
8287 f = fopen(path_unstaged_content, "r");
8289 err = got_error_from_errno2("fopen",
8290 path_unstaged_content);
8293 if (fstat(fileno(f), &sb) == -1) {
8294 err = got_error_from_errno2("fstat", path_unstaged_content);
8297 if (got_fileindex_entry_staged_filetype_get(ie) ==
8298 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8299 char link_target[PATH_MAX];
8301 r = fread(link_target, 1, sizeof(link_target), f);
8302 if (r == 0 && ferror(f)) {
8303 err = got_error_from_errno("fread");
8306 if (r >= sizeof(link_target)) { /* should not happen */
8307 err = got_error(GOT_ERR_NO_SPACE);
8310 link_target[r] = '\0';
8311 err = merge_symlink(worktree, blob_base,
8312 ondisk_path, ie->path, label_orig, link_target,
8313 worktree->base_commit_id, repo, progress_cb,
8316 int local_changes_subsumed;
8318 err = got_path_dirname(&parent, ondisk_path);
8322 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8324 err = got_error_from_errno("asprintf");
8329 err = got_opentemp_named(&blob_base_path, &f_base, base_path);
8332 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8338 * In order the run a 3-way merge with a symlink we copy the symlink's
8339 * target path into a temporary file and use that file with diff3.
8341 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8342 err = dump_symlink_target_path_to_file(&f_deriv2,
8348 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
8350 err = got_error_from_errno2("open", ondisk_path);
8353 f_deriv2 = fdopen(fd, "r");
8354 if (f_deriv2 == NULL) {
8355 err = got_error_from_errno2("fdopen", ondisk_path);
8361 err = merge_file(&local_changes_subsumed, worktree,
8362 f_base, f, f_deriv2, ondisk_path, ie->path,
8363 got_fileindex_perms_to_st(ie),
8364 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8365 repo, progress_cb, progress_arg);
8370 if (new_staged_blob_id) {
8371 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8372 SHA1_DIGEST_LENGTH);
8374 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8375 got_fileindex_entry_staged_filetype_set(ie, 0);
8378 free(new_staged_blob_id);
8379 if (path_unstaged_content &&
8380 unlink(path_unstaged_content) == -1 && err == NULL)
8381 err = got_error_from_errno2("unlink", path_unstaged_content);
8382 if (path_new_staged_content &&
8383 unlink(path_new_staged_content) == -1 && err == NULL)
8384 err = got_error_from_errno2("unlink", path_new_staged_content);
8385 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8386 err = got_error_from_errno2("unlink", blob_base_path);
8387 if (f_base && fclose(f_base) == EOF && err == NULL)
8388 err = got_error_from_errno2("fclose", path_unstaged_content);
8389 if (f && fclose(f) == EOF && err == NULL)
8390 err = got_error_from_errno2("fclose", path_unstaged_content);
8391 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8392 err = got_error_from_errno2("fclose", ondisk_path);
8393 free(path_unstaged_content);
8394 free(path_new_staged_content);
8395 free(blob_base_path);
8401 static const struct got_error *
8402 unstage_path(void *arg, unsigned char status,
8403 unsigned char staged_status, const char *relpath,
8404 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8405 struct got_object_id *commit_id, int dirfd, const char *de_name)
8407 const struct got_error *err = NULL;
8408 struct unstage_path_arg *a = arg;
8409 struct got_fileindex_entry *ie;
8410 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8411 char *ondisk_path = NULL;
8412 char *id_str = NULL, *label_orig = NULL;
8413 int local_changes_subsumed;
8416 if (staged_status != GOT_STATUS_ADD &&
8417 staged_status != GOT_STATUS_MODIFY &&
8418 staged_status != GOT_STATUS_DELETE)
8421 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8423 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8425 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8427 return got_error_from_errno("asprintf");
8429 err = got_object_id_str(&id_str,
8430 commit_id ? commit_id : a->worktree->base_commit_id);
8433 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8435 err = got_error_from_errno("asprintf");
8439 switch (staged_status) {
8440 case GOT_STATUS_MODIFY:
8441 err = got_object_open_as_blob(&blob_base, a->repo,
8446 case GOT_STATUS_ADD:
8448 if (staged_status == GOT_STATUS_ADD) {
8449 int choice = GOT_PATCH_CHOICE_NONE;
8450 err = (*a->patch_cb)(&choice, a->patch_arg,
8451 staged_status, ie->path, NULL, 1, 1);
8454 if (choice != GOT_PATCH_CHOICE_YES)
8457 err = unstage_hunks(staged_blob_id,
8458 blob_base, blob_id, ie, ondisk_path,
8459 label_orig, a->worktree, a->repo,
8460 a->patch_cb, a->patch_arg,
8461 a->progress_cb, a->progress_arg);
8462 break; /* Done with this file. */
8465 err = got_object_open_as_blob(&blob_staged, a->repo,
8466 staged_blob_id, 8192);
8469 switch (got_fileindex_entry_staged_filetype_get(ie)) {
8470 case GOT_FILEIDX_MODE_BAD_SYMLINK:
8471 case GOT_FILEIDX_MODE_REGULAR_FILE:
8472 err = merge_blob(&local_changes_subsumed, a->worktree,
8473 blob_base, ondisk_path, relpath,
8474 got_fileindex_perms_to_st(ie), label_orig,
8475 blob_staged, commit_id ? commit_id :
8476 a->worktree->base_commit_id, a->repo,
8477 a->progress_cb, a->progress_arg);
8479 case GOT_FILEIDX_MODE_SYMLINK:
8480 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8481 char *staged_target;
8482 err = got_object_blob_read_to_str(
8483 &staged_target, blob_staged);
8486 err = merge_symlink(a->worktree, blob_base,
8487 ondisk_path, relpath, label_orig,
8488 staged_target, commit_id ? commit_id :
8489 a->worktree->base_commit_id,
8490 a->repo, a->progress_cb, a->progress_arg);
8491 free(staged_target);
8493 err = merge_blob(&local_changes_subsumed,
8494 a->worktree, blob_base, ondisk_path,
8495 relpath, got_fileindex_perms_to_st(ie),
8496 label_orig, blob_staged,
8497 commit_id ? commit_id :
8498 a->worktree->base_commit_id, a->repo,
8499 a->progress_cb, a->progress_arg);
8503 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8507 got_fileindex_entry_stage_set(ie,
8508 GOT_FILEIDX_STAGE_NONE);
8509 got_fileindex_entry_staged_filetype_set(ie, 0);
8512 case GOT_STATUS_DELETE:
8514 int choice = GOT_PATCH_CHOICE_NONE;
8515 err = (*a->patch_cb)(&choice, a->patch_arg,
8516 staged_status, ie->path, NULL, 1, 1);
8519 if (choice == GOT_PATCH_CHOICE_NO)
8521 if (choice != GOT_PATCH_CHOICE_YES) {
8522 err = got_error(GOT_ERR_PATCH_CHOICE);
8526 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8527 got_fileindex_entry_staged_filetype_set(ie, 0);
8528 err = get_file_status(&status, &sb, ie, ondisk_path,
8529 dirfd, de_name, a->repo);
8532 err = (*a->progress_cb)(a->progress_arg, status, relpath);
8538 got_object_blob_close(blob_base);
8540 got_object_blob_close(blob_staged);
8546 const struct got_error *
8547 got_worktree_unstage(struct got_worktree *worktree,
8548 struct got_pathlist_head *paths,
8549 got_worktree_checkout_cb progress_cb, void *progress_arg,
8550 got_worktree_patch_cb patch_cb, void *patch_arg,
8551 struct got_repository *repo)
8553 const struct got_error *err = NULL, *sync_err, *unlockerr;
8554 struct got_pathlist_entry *pe;
8555 struct got_fileindex *fileindex = NULL;
8556 char *fileindex_path = NULL;
8557 struct unstage_path_arg upa;
8559 err = lock_worktree(worktree, LOCK_EX);
8563 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8567 upa.worktree = worktree;
8568 upa.fileindex = fileindex;
8570 upa.progress_cb = progress_cb;
8571 upa.progress_arg = progress_arg;
8572 upa.patch_cb = patch_cb;
8573 upa.patch_arg = patch_arg;
8574 TAILQ_FOREACH(pe, paths, entry) {
8575 err = worktree_status(worktree, pe->path, fileindex, repo,
8576 unstage_path, &upa, NULL, NULL, 1, 0);
8581 sync_err = sync_fileindex(fileindex, fileindex_path);
8582 if (sync_err && err == NULL)
8585 free(fileindex_path);
8587 got_fileindex_free(fileindex);
8588 unlockerr = lock_worktree(worktree, LOCK_SH);
8589 if (unlockerr && err == NULL)
8594 struct report_file_info_arg {
8595 struct got_worktree *worktree;
8596 got_worktree_path_info_cb info_cb;
8598 struct got_pathlist_head *paths;
8599 got_cancel_cb cancel_cb;
8603 static const struct got_error *
8604 report_file_info(void *arg, struct got_fileindex_entry *ie)
8606 struct report_file_info_arg *a = arg;
8607 struct got_pathlist_entry *pe;
8608 struct got_object_id blob_id, staged_blob_id, commit_id;
8609 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8610 struct got_object_id *commit_idp = NULL;
8613 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8614 return got_error(GOT_ERR_CANCELLED);
8616 TAILQ_FOREACH(pe, a->paths, entry) {
8617 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8618 got_path_is_child(ie->path, pe->path, pe->path_len))
8621 if (pe == NULL) /* not found */
8624 if (got_fileindex_entry_has_blob(ie)) {
8625 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8626 blob_idp = &blob_id;
8628 stage = got_fileindex_entry_stage_get(ie);
8629 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8630 stage == GOT_FILEIDX_STAGE_ADD) {
8631 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8632 SHA1_DIGEST_LENGTH);
8633 staged_blob_idp = &staged_blob_id;
8636 if (got_fileindex_entry_has_commit(ie)) {
8637 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8638 commit_idp = &commit_id;
8641 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8642 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8645 const struct got_error *
8646 got_worktree_path_info(struct got_worktree *worktree,
8647 struct got_pathlist_head *paths,
8648 got_worktree_path_info_cb info_cb, void *info_arg,
8649 got_cancel_cb cancel_cb, void *cancel_arg)
8652 const struct got_error *err = NULL, *unlockerr;
8653 struct got_fileindex *fileindex = NULL;
8654 char *fileindex_path = NULL;
8655 struct report_file_info_arg arg;
8657 err = lock_worktree(worktree, LOCK_SH);
8661 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8665 arg.worktree = worktree;
8666 arg.info_cb = info_cb;
8667 arg.info_arg = info_arg;
8669 arg.cancel_cb = cancel_cb;
8670 arg.cancel_arg = cancel_arg;
8671 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8674 free(fileindex_path);
8676 got_fileindex_free(fileindex);
8677 unlockerr = lock_worktree(worktree, LOCK_UN);
8678 if (unlockerr && err == NULL)