2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
34 #include "got_compat.h"
36 #include "got_error.h"
37 #include "got_repository.h"
38 #include "got_reference.h"
39 #include "got_object.h"
41 #include "got_cancel.h"
42 #include "got_worktree.h"
43 #include "got_opentemp.h"
46 #include "got_lib_worktree.h"
47 #include "got_lib_sha1.h"
48 #include "got_lib_fileindex.h"
49 #include "got_lib_inflate.h"
50 #include "got_lib_delta.h"
51 #include "got_lib_object.h"
52 #include "got_lib_object_parse.h"
53 #include "got_lib_object_create.h"
54 #include "got_lib_object_idset.h"
55 #include "got_lib_diff.h"
56 #include "got_lib_gotconfig.h"
59 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
62 #define GOT_MERGE_LABEL_MERGED "merged change"
63 #define GOT_MERGE_LABEL_BASE "3-way merge base"
65 static const struct got_error *
66 create_meta_file(const char *path_got, const char *name, const char *content)
68 const struct got_error *err = NULL;
71 if (asprintf(&path, "%s/%s", path_got, name) == -1)
72 return got_error_from_errno("asprintf");
74 err = got_path_create_file(path, content);
79 static const struct got_error *
80 update_meta_file(const char *path_got, const char *name, const char *content)
82 const struct got_error *err = NULL;
87 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
88 err = got_error_from_errno("asprintf");
93 err = got_opentemp_named(&tmppath, &tmpfile, path);
98 int len = fprintf(tmpfile, "%s\n", content);
99 if (len != strlen(content) + 1) {
100 err = got_error_from_errno2("fprintf", tmppath);
105 if (rename(tmppath, path) != 0) {
106 err = got_error_from_errno3("rename", tmppath, path);
112 if (fclose(tmpfile) == EOF && err == NULL)
113 err = got_error_from_errno2("fclose", tmppath);
118 static const struct got_error *
119 write_head_ref(const char *path_got, struct got_reference *head_ref)
121 const struct got_error *err = NULL;
124 if (got_ref_is_symbolic(head_ref)) {
125 refstr = got_ref_to_str(head_ref);
127 return got_error_from_errno("got_ref_to_str");
129 refstr = strdup(got_ref_get_name(head_ref));
131 return got_error_from_errno("strdup");
133 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
138 const struct got_error *
139 got_worktree_init(const char *path, struct got_reference *head_ref,
140 const char *prefix, struct got_repository *repo)
142 const struct got_error *err = NULL;
143 struct got_object_id *commit_id = NULL;
145 uint32_t uuid_status;
147 char *path_got = NULL;
148 char *formatstr = NULL;
149 char *absprefix = NULL;
150 char *basestr = NULL;
151 char *uuidstr = NULL;
153 if (strcmp(path, got_repo_get_path(repo)) == 0) {
154 err = got_error(GOT_ERR_WORKTREE_REPO);
158 err = got_ref_resolve(&commit_id, repo, head_ref);
161 err = got_object_get_type(&obj_type, repo, commit_id);
164 if (obj_type != GOT_OBJ_TYPE_COMMIT)
165 return got_error(GOT_ERR_OBJ_TYPE);
167 if (!got_path_is_absolute(prefix)) {
168 if (asprintf(&absprefix, "/%s", prefix) == -1)
169 return got_error_from_errno("asprintf");
172 /* Create top-level directory (may already exist). */
173 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
174 err = got_error_from_errno2("mkdir", path);
178 /* Create .got directory (may already exist). */
179 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
180 err = got_error_from_errno("asprintf");
183 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
184 err = got_error_from_errno2("mkdir", path_got);
188 /* Create an empty lock file. */
189 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
193 /* Create an empty file index. */
194 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
198 /* Write the HEAD reference. */
199 err = write_head_ref(path_got, head_ref);
203 /* Record our base commit. */
204 err = got_object_id_str(&basestr, commit_id);
207 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
211 /* Store path to repository. */
212 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
213 got_repo_get_path(repo));
217 /* Store in-repository path prefix. */
218 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
219 absprefix ? absprefix : prefix);
224 uuid_create(&uuid, &uuid_status);
225 if (uuid_status != uuid_s_ok) {
226 err = got_error_uuid(uuid_status, "uuid_create");
229 uuid_to_string(&uuid, &uuidstr, &uuid_status);
230 if (uuid_status != uuid_s_ok) {
231 err = got_error_uuid(uuid_status, "uuid_to_string");
234 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
238 /* Stamp work tree with format file. */
239 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
240 err = got_error_from_errno("asprintf");
243 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
257 const struct got_error *
258 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
259 const char *path_prefix)
261 char *absprefix = NULL;
263 if (!got_path_is_absolute(path_prefix)) {
264 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
265 return got_error_from_errno("asprintf");
267 *match = (strcmp(absprefix ? absprefix : path_prefix,
268 worktree->path_prefix) == 0);
274 got_worktree_get_head_ref_name(struct got_worktree *worktree)
276 return worktree->head_ref_name;
279 const struct got_error *
280 got_worktree_set_head_ref(struct got_worktree *worktree,
281 struct got_reference *head_ref)
283 const struct got_error *err = NULL;
284 char *path_got = NULL, *head_ref_name = NULL;
286 if (asprintf(&path_got, "%s/%s", worktree->root_path,
287 GOT_WORKTREE_GOT_DIR) == -1) {
288 err = got_error_from_errno("asprintf");
293 head_ref_name = strdup(got_ref_get_name(head_ref));
294 if (head_ref_name == NULL) {
295 err = got_error_from_errno("strdup");
299 err = write_head_ref(path_got, head_ref);
303 free(worktree->head_ref_name);
304 worktree->head_ref_name = head_ref_name;
312 struct got_object_id *
313 got_worktree_get_base_commit_id(struct got_worktree *worktree)
315 return worktree->base_commit_id;
318 const struct got_error *
319 got_worktree_set_base_commit_id(struct got_worktree *worktree,
320 struct got_repository *repo, struct got_object_id *commit_id)
322 const struct got_error *err;
323 struct got_object *obj = NULL;
325 char *path_got = NULL;
327 if (asprintf(&path_got, "%s/%s", worktree->root_path,
328 GOT_WORKTREE_GOT_DIR) == -1) {
329 err = got_error_from_errno("asprintf");
334 err = got_object_open(&obj, repo, commit_id);
338 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
339 err = got_error(GOT_ERR_OBJ_TYPE);
343 /* Record our base commit. */
344 err = got_object_id_str(&id_str, commit_id);
347 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
351 free(worktree->base_commit_id);
352 worktree->base_commit_id = got_object_id_dup(commit_id);
353 if (worktree->base_commit_id == NULL) {
354 err = got_error_from_errno("got_object_id_dup");
359 got_object_close(obj);
365 const struct got_gotconfig *
366 got_worktree_get_gotconfig(struct got_worktree *worktree)
368 return worktree->gotconfig;
371 static const struct got_error *
372 lock_worktree(struct got_worktree *worktree, int operation)
374 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
375 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
376 : got_error_from_errno2("flock",
377 got_worktree_get_root_path(worktree)));
381 static const struct got_error *
382 add_dir_on_disk(struct got_worktree *worktree, const char *path)
384 const struct got_error *err = NULL;
387 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
388 return got_error_from_errno("asprintf");
390 err = got_path_mkdir(abspath);
391 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
394 if (lstat(abspath, &sb) == -1) {
395 err = got_error_from_errno2("lstat", abspath);
396 } else if (!S_ISDIR(sb.st_mode)) {
397 /* TODO directory is obstructed; do something */
398 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
405 static const struct got_error *
406 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
408 const struct got_error *err = NULL;
411 size_t flen1 = 0, flen2 = 0;
416 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
417 if (flen1 == 0 && ferror(f1)) {
418 err = got_error_from_errno("fread");
421 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
422 if (flen2 == 0 && ferror(f2)) {
423 err = got_error_from_errno("fread");
430 } else if (flen2 == 0) {
434 } else if (flen1 == flen2) {
435 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
448 static const struct got_error *
449 check_files_equal(int *same, FILE *f1, FILE *f2)
456 if (fstat(fileno(f1), &sb) != 0)
457 return got_error_from_errno("fstat");
460 if (fstat(fileno(f2), &sb) != 0)
461 return got_error_from_errno("fstat");
464 if (size1 != size2) {
469 if (fseek(f1, 0L, SEEK_SET) == -1)
470 return got_ferror(f1, GOT_ERR_IO);
471 if (fseek(f2, 0L, SEEK_SET) == -1)
472 return got_ferror(f2, GOT_ERR_IO);
474 return check_file_contents_equal(same, f1, f2);
477 static const struct got_error *
478 copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
486 if (fseek(f, 0L, SEEK_SET) == -1)
487 return got_ferror(f, GOT_ERR_IO);
490 flen = fread(fbuf, 1, sizeof(fbuf), f);
493 return got_error_from_errno("fread");
497 outlen = write(outfd, fbuf, flen);
499 return got_error_from_errno("write");
501 return got_error(GOT_ERR_IO);
508 static const struct got_error *
509 merge_binary_file(int *overlapcnt, int merged_fd,
510 FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
511 const char *label_deriv, const char *label_orig, const char *label_deriv2,
512 const char *ondisk_path)
514 const struct got_error *err = NULL;
515 int same_content, changed_deriv, changed_deriv2;
516 int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
517 off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
518 char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
519 char *base_path_orig = NULL, *base_path_deriv = NULL;
520 char *base_path_deriv2 = NULL;
524 err = check_files_equal(&same_content, f_deriv, f_deriv2);
529 return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
531 err = check_files_equal(&same_content, f_deriv, f_orig);
534 changed_deriv = !same_content;
535 err = check_files_equal(&same_content, f_deriv2, f_orig);
538 changed_deriv2 = !same_content;
540 if (changed_deriv && changed_deriv2) {
542 if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
543 err = got_error_from_errno("asprintf");
546 if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
547 err = got_error_from_errno("asprintf");
550 if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
551 err = got_error_from_errno("asprintf");
554 err = got_opentemp_named_fd(&path_orig, &fd_orig,
558 err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
562 err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
566 err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
569 err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
572 err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
575 if (dprintf(merged_fd, "Binary files differ and cannot be "
576 "merged automatically:\n") < 0) {
577 err = got_error_from_errno("dprintf");
580 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
581 GOT_DIFF_CONFLICT_MARKER_BEGIN,
582 label_deriv ? " " : "",
583 label_deriv ? label_deriv : "",
585 err = got_error_from_errno("dprintf");
589 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
590 GOT_DIFF_CONFLICT_MARKER_ORIG,
591 label_orig ? " " : "",
592 label_orig ? label_orig : "",
594 err = got_error_from_errno("dprintf");
598 if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
599 GOT_DIFF_CONFLICT_MARKER_SEP,
601 GOT_DIFF_CONFLICT_MARKER_END,
602 label_deriv2 ? " " : "",
603 label_deriv2 ? label_deriv2 : "") < 0) {
604 err = got_error_from_errno("dprintf");
607 } else if (changed_deriv)
608 err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
609 else if (changed_deriv2)
610 err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
612 if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
614 err = got_error_from_errno2("unlink", path_orig);
615 if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
616 err = got_error_from_errno2("close", path_orig);
617 if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
618 err = got_error_from_errno2("close", path_deriv);
619 if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
620 err = got_error_from_errno2("close", path_deriv2);
624 free(base_path_orig);
625 free(base_path_deriv);
626 free(base_path_deriv2);
631 * Perform a 3-way merge where the file f_orig acts as the common
632 * ancestor, the file f_deriv acts as the first derived version,
633 * and the file f_deriv2 acts as the second derived version.
634 * The merge result will be written to a new file at ondisk_path; any
635 * existing file at this path will be replaced.
637 static const struct got_error *
638 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
639 FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
640 const char *path, uint16_t st_mode,
641 const char *label_orig, const char *label_deriv, const char *label_deriv2,
642 enum got_diff_algorithm diff_algo, struct got_repository *repo,
643 got_worktree_checkout_cb progress_cb, void *progress_arg)
645 const struct got_error *err = NULL;
647 FILE *f_merged = NULL;
648 char *merged_path = NULL, *base_path = NULL;
652 *local_changes_subsumed = 0;
654 err = got_path_dirname(&parent, ondisk_path);
658 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
659 err = got_error_from_errno("asprintf");
663 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
667 err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
668 f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
670 if (err->code != GOT_ERR_FILE_BINARY)
672 err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
673 f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
679 err = (*progress_cb)(progress_arg,
680 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
684 if (fsync(merged_fd) != 0) {
685 err = got_error_from_errno("fsync");
689 f_merged = fdopen(merged_fd, "r");
690 if (f_merged == NULL) {
691 err = got_error_from_errno("fdopen");
696 /* Check if a clean merge has subsumed all local changes. */
697 if (overlapcnt == 0) {
698 err = check_files_equal(local_changes_subsumed, f_deriv,
704 if (fchmod(fileno(f_merged), st_mode) != 0) {
705 err = got_error_from_errno2("fchmod", merged_path);
709 if (rename(merged_path, ondisk_path) != 0) {
710 err = got_error_from_errno3("rename", merged_path,
719 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
720 err = got_error_from_errno("close");
721 if (f_merged && fclose(f_merged) == EOF && err == NULL)
722 err = got_error_from_errno("fclose");
729 static const struct got_error *
730 update_symlink(const char *ondisk_path, const char *target_path,
733 /* This is not atomic but matches what 'ln -sf' does. */
734 if (unlink(ondisk_path) == -1)
735 return got_error_from_errno2("unlink", ondisk_path);
736 if (symlink(target_path, ondisk_path) == -1)
737 return got_error_from_errno3("symlink", target_path,
743 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
744 * in the work tree with a file that contains conflict markers and the
745 * conflicting target paths of the original version, a "derived version"
746 * of a symlink from an incoming change, and a local version of the symlink.
748 * The original versions's target path can be NULL if it is not available,
749 * such as if both derived versions added a new symlink at the same path.
751 * The incoming derived symlink target is NULL in case the incoming change
752 * has deleted this symlink.
754 static const struct got_error *
755 install_symlink_conflict(const char *deriv_target,
756 struct got_object_id *deriv_base_commit_id, const char *orig_target,
757 const char *label_orig, const char *local_target, const char *ondisk_path)
759 const struct got_error *err;
760 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
763 err = got_object_id_str(&id_str, deriv_base_commit_id);
765 return got_error_from_errno("asprintf");
767 if (asprintf(&label_deriv, "%s: commit %s",
768 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
769 err = got_error_from_errno("asprintf");
773 err = got_opentemp_named(&path, &f, "got-symlink-conflict");
777 if (fchmod(fileno(f), GOT_DEFAULT_FILE_MODE) == -1) {
778 err = got_error_from_errno2("fchmod", path);
782 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
783 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
784 deriv_target ? deriv_target : "(symlink was deleted)",
785 orig_target ? label_orig : "",
786 orig_target ? "\n" : "",
787 orig_target ? orig_target : "",
788 orig_target ? "\n" : "",
789 GOT_DIFF_CONFLICT_MARKER_SEP,
790 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
791 err = got_error_from_errno2("fprintf", path);
795 if (unlink(ondisk_path) == -1) {
796 err = got_error_from_errno2("unlink", ondisk_path);
799 if (rename(path, ondisk_path) == -1) {
800 err = got_error_from_errno3("rename", path, ondisk_path);
804 if (f != NULL && fclose(f) == EOF && err == NULL)
805 err = got_error_from_errno2("fclose", path);
812 /* forward declaration */
813 static const struct got_error *
814 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
815 const char *, const char *, uint16_t, const char *,
816 struct got_blob_object *, struct got_object_id *,
817 struct got_repository *, got_worktree_checkout_cb, void *);
820 * Merge a symlink into the work tree, where blob_orig acts as the common
821 * ancestor, deriv_target is the link target of the first derived version,
822 * and the symlink on disk acts as the second derived version.
823 * Assume that contents of both blobs represent symlinks.
825 static const struct got_error *
826 merge_symlink(struct got_worktree *worktree,
827 struct got_blob_object *blob_orig, const char *ondisk_path,
828 const char *path, const char *label_orig, const char *deriv_target,
829 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
830 got_worktree_checkout_cb progress_cb, void *progress_arg)
832 const struct got_error *err = NULL;
833 char *ancestor_target = NULL;
835 ssize_t ondisk_len, deriv_len;
836 char ondisk_target[PATH_MAX];
837 int have_local_change = 0;
838 int have_incoming_change = 0;
840 if (lstat(ondisk_path, &sb) == -1)
841 return got_error_from_errno2("lstat", ondisk_path);
843 ondisk_len = readlink(ondisk_path, ondisk_target,
844 sizeof(ondisk_target));
845 if (ondisk_len == -1) {
846 err = got_error_from_errno2("readlink",
850 ondisk_target[ondisk_len] = '\0';
853 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
858 if (ancestor_target == NULL ||
859 (ondisk_len != strlen(ancestor_target) ||
860 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
861 have_local_change = 1;
863 deriv_len = strlen(deriv_target);
864 if (ancestor_target == NULL ||
865 (deriv_len != strlen(ancestor_target) ||
866 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
867 have_incoming_change = 1;
869 if (!have_local_change && !have_incoming_change) {
870 if (ancestor_target) {
871 /* Both sides made the same change. */
872 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
874 } else if (deriv_len == ondisk_len &&
875 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
876 /* Both sides added the same symlink. */
877 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
880 /* Both sides added symlinks which don't match. */
881 err = install_symlink_conflict(deriv_target,
882 deriv_base_commit_id, ancestor_target,
883 label_orig, ondisk_target, ondisk_path);
886 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
889 } else if (!have_local_change && have_incoming_change) {
890 /* Apply the incoming change. */
891 err = update_symlink(ondisk_path, deriv_target,
892 strlen(deriv_target));
895 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
896 } else if (have_local_change && have_incoming_change) {
897 if (deriv_len == ondisk_len &&
898 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
899 /* Both sides made the same change. */
900 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
903 err = install_symlink_conflict(deriv_target,
904 deriv_base_commit_id, ancestor_target, label_orig,
905 ondisk_target, ondisk_path);
908 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
914 free(ancestor_target);
918 static const struct got_error *
919 dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
921 const struct got_error *err = NULL;
922 char target_path[PATH_MAX];
931 return got_error_from_errno("got_opentemp");
932 target_len = readlink(ondisk_path, target_path, sizeof(target_path));
933 if (target_len == -1) {
934 err = got_error_from_errno2("readlink", ondisk_path);
937 n = fwrite(target_path, 1, target_len, f);
938 if (n != target_len) {
939 err = got_ferror(f, GOT_ERR_IO);
942 if (fflush(f) == EOF) {
943 err = got_error_from_errno("fflush");
946 if (fseek(f, 0L, SEEK_SET) == -1) {
947 err = got_ferror(f, GOT_ERR_IO);
959 * Perform a 3-way merge where blob_orig acts as the common ancestor,
960 * blob_deriv acts as the first derived version, and the file on disk
961 * acts as the second derived version.
963 static const struct got_error *
964 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
965 struct got_blob_object *blob_orig, const char *ondisk_path,
966 const char *path, uint16_t st_mode, const char *label_orig,
967 struct got_blob_object *blob_deriv,
968 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
969 got_worktree_checkout_cb progress_cb, void *progress_arg)
971 const struct got_error *err = NULL;
972 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
973 char *blob_orig_path = NULL;
974 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
975 char *label_deriv = NULL, *parent = NULL;
977 *local_changes_subsumed = 0;
979 err = got_path_dirname(&parent, ondisk_path);
984 if (asprintf(&base_path, "%s/got-merge-blob-orig",
986 err = got_error_from_errno("asprintf");
991 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
994 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1001 * No common ancestor exists. This is an "add vs add" conflict
1002 * and we simply use an empty ancestor file to make both files
1003 * appear in the merged result in their entirety.
1005 f_orig = got_opentemp();
1006 if (f_orig == NULL) {
1007 err = got_error_from_errno("got_opentemp");
1012 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1013 err = got_error_from_errno("asprintf");
1018 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1021 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1026 err = got_object_id_str(&id_str, deriv_base_commit_id);
1029 if (asprintf(&label_deriv, "%s: commit %s",
1030 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1031 err = got_error_from_errno("asprintf");
1036 * In order the run a 3-way merge with a symlink we copy the symlink's
1037 * target path into a temporary file and use that file with diff3.
1039 if (S_ISLNK(st_mode)) {
1040 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1045 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1047 err = got_error_from_errno2("open", ondisk_path);
1050 f_deriv2 = fdopen(fd, "r");
1051 if (f_deriv2 == NULL) {
1052 err = got_error_from_errno2("fdopen", ondisk_path);
1058 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1059 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1060 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1062 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1063 err = got_error_from_errno("fclose");
1064 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1065 err = got_error_from_errno("fclose");
1066 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1067 err = got_error_from_errno("fclose");
1069 if (blob_orig_path) {
1070 unlink(blob_orig_path);
1071 free(blob_orig_path);
1073 if (blob_deriv_path) {
1074 unlink(blob_deriv_path);
1075 free(blob_deriv_path);
1083 static const struct got_error *
1084 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1085 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1086 int wt_fd, const char *path, struct got_object_id *blob_id)
1088 const struct got_error *err = NULL;
1089 struct got_fileindex_entry *new_ie;
1093 err = got_fileindex_entry_alloc(&new_ie, path);
1097 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1098 blob_id->sha1, base_commit_id->sha1, 1);
1102 err = got_fileindex_entry_add(fileindex, new_ie);
1105 got_fileindex_entry_free(new_ie);
1112 get_ondisk_perms(int executable, mode_t st_mode)
1114 mode_t xbits = S_IXUSR;
1117 /* Map read bits to execute bits. */
1118 if (st_mode & S_IRGRP)
1120 if (st_mode & S_IROTH)
1122 return st_mode | xbits;
1125 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1128 /* forward declaration */
1129 static const struct got_error *
1130 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1131 const char *path, mode_t te_mode, mode_t st_mode,
1132 struct got_blob_object *blob, int restoring_missing_file,
1133 int reverting_versioned_file, int installing_bad_symlink,
1134 int path_is_unversioned, struct got_repository *repo,
1135 got_worktree_checkout_cb progress_cb, void *progress_arg);
1138 * This function assumes that the provided symlink target points at a
1139 * safe location in the work tree!
1141 static const struct got_error *
1142 replace_existing_symlink(int *did_something, const char *ondisk_path,
1143 const char *target_path, size_t target_len)
1145 const struct got_error *err = NULL;
1147 char etarget[PATH_MAX];
1153 * "Bad" symlinks (those pointing outside the work tree or into the
1154 * .got directory) are installed in the work tree as a regular file
1155 * which contains the bad symlink target path.
1156 * The new symlink target has already been checked for safety by our
1157 * caller. If we can successfully open a regular file then we simply
1158 * replace this file with a symlink below.
1160 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1162 if (!got_err_open_nofollow_on_symlink())
1163 return got_error_from_errno2("open", ondisk_path);
1165 /* We are updating an existing on-disk symlink. */
1166 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1168 return got_error_from_errno2("readlink", ondisk_path);
1170 if (elen == target_len &&
1171 memcmp(etarget, target_path, target_len) == 0)
1172 return NULL; /* nothing to do */
1176 err = update_symlink(ondisk_path, target_path, target_len);
1177 if (fd != -1 && close(fd) == -1 && err == NULL)
1178 err = got_error_from_errno2("close", ondisk_path);
1182 static const struct got_error *
1183 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1184 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1186 const struct got_error *err = NULL;
1187 char canonpath[PATH_MAX];
1188 char *path_got = NULL;
1190 *is_bad_symlink = 0;
1192 if (target_len >= sizeof(canonpath)) {
1193 *is_bad_symlink = 1;
1198 * We do not use realpath(3) to resolve the symlink's target
1199 * path because we don't want to resolve symlinks recursively.
1200 * Instead we make the path absolute and then canonicalize it.
1201 * Relative symlink target lookup should begin at the directory
1202 * in which the blob object is being installed.
1204 if (!got_path_is_absolute(target_path)) {
1205 char *abspath, *parent;
1206 err = got_path_dirname(&parent, ondisk_path);
1209 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1211 return got_error_from_errno("asprintf");
1214 if (strlen(abspath) >= sizeof(canonpath)) {
1215 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1219 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1224 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1229 /* Only allow symlinks pointing at paths within the work tree. */
1230 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1231 *is_bad_symlink = 1;
1235 /* Do not allow symlinks pointing into the .got directory. */
1236 if (asprintf(&path_got, "%s/%s", wtroot_path,
1237 GOT_WORKTREE_GOT_DIR) == -1)
1238 return got_error_from_errno("asprintf");
1239 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1240 *is_bad_symlink = 1;
1246 static const struct got_error *
1247 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1248 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1249 int restoring_missing_file, int reverting_versioned_file,
1250 int path_is_unversioned, int allow_bad_symlinks,
1251 struct got_repository *repo,
1252 got_worktree_checkout_cb progress_cb, void *progress_arg)
1254 const struct got_error *err = NULL;
1255 char target_path[PATH_MAX];
1256 size_t len, target_len = 0;
1257 char *path_got = NULL;
1258 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1259 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1261 *is_bad_symlink = 0;
1264 * Blob object content specifies the target path of the link.
1265 * If a symbolic link cannot be installed we instead create
1266 * a regular file which contains the link target path stored
1267 * in the blob object.
1270 err = got_object_blob_read_block(&len, blob);
1271 if (len + target_len >= sizeof(target_path)) {
1272 /* Path too long; install as a regular file. */
1273 *is_bad_symlink = 1;
1274 got_object_blob_rewind(blob);
1275 return install_blob(worktree, ondisk_path, path,
1276 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1277 restoring_missing_file, reverting_versioned_file,
1278 1, path_is_unversioned, repo, progress_cb,
1282 /* Skip blob object header first time around. */
1283 memcpy(target_path + target_len, buf + hdrlen,
1285 target_len += len - hdrlen;
1289 target_path[target_len] = '\0';
1291 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1292 ondisk_path, worktree->root_path);
1296 if (*is_bad_symlink && !allow_bad_symlinks) {
1297 /* install as a regular file */
1298 got_object_blob_rewind(blob);
1299 err = install_blob(worktree, ondisk_path, path,
1300 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1301 restoring_missing_file, reverting_versioned_file, 1,
1302 path_is_unversioned, repo, progress_cb, progress_arg);
1306 if (symlink(target_path, ondisk_path) == -1) {
1307 if (errno == EEXIST) {
1308 int symlink_replaced;
1309 if (path_is_unversioned) {
1310 err = (*progress_cb)(progress_arg,
1311 GOT_STATUS_UNVERSIONED, path);
1314 err = replace_existing_symlink(&symlink_replaced,
1315 ondisk_path, target_path, target_len);
1319 if (symlink_replaced) {
1320 err = (*progress_cb)(progress_arg,
1321 reverting_versioned_file ?
1323 GOT_STATUS_UPDATE, path);
1325 err = (*progress_cb)(progress_arg,
1326 GOT_STATUS_EXISTS, path);
1329 goto done; /* Nothing else to do. */
1332 if (errno == ENOENT) {
1334 err = got_path_dirname(&parent, ondisk_path);
1337 err = add_dir_on_disk(worktree, parent);
1342 * Retry, and fall through to error handling
1343 * below if this second attempt fails.
1345 if (symlink(target_path, ondisk_path) != -1) {
1346 err = NULL; /* success */
1351 /* Handle errors from first or second creation attempt. */
1352 if (errno == ENAMETOOLONG) {
1353 /* bad target path; install as a regular file */
1354 *is_bad_symlink = 1;
1355 got_object_blob_rewind(blob);
1356 err = install_blob(worktree, ondisk_path, path,
1357 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1358 restoring_missing_file, reverting_versioned_file, 1,
1359 path_is_unversioned, repo,
1360 progress_cb, progress_arg);
1361 } else if (errno == ENOTDIR) {
1362 err = got_error_path(ondisk_path,
1363 GOT_ERR_FILE_OBSTRUCTED);
1365 err = got_error_from_errno3("symlink",
1366 target_path, ondisk_path);
1368 } else if (progress_cb)
1369 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1370 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1376 static const struct got_error *
1377 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1378 const char *path, mode_t te_mode, mode_t st_mode,
1379 struct got_blob_object *blob, int restoring_missing_file,
1380 int reverting_versioned_file, int installing_bad_symlink,
1381 int path_is_unversioned, struct got_repository *repo,
1382 got_worktree_checkout_cb progress_cb, void *progress_arg)
1384 const struct got_error *err = NULL;
1388 char *tmppath = NULL;
1390 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1391 O_CLOEXEC, GOT_DEFAULT_FILE_MODE);
1393 if (errno == ENOENT) {
1395 err = got_path_dirname(&parent, path);
1398 err = add_dir_on_disk(worktree, parent);
1402 fd = open(ondisk_path,
1403 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1404 GOT_DEFAULT_FILE_MODE);
1406 return got_error_from_errno2("open",
1408 } else if (errno == EEXIST) {
1409 if (path_is_unversioned) {
1410 err = (*progress_cb)(progress_arg,
1411 GOT_STATUS_UNVERSIONED, path);
1414 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1415 !S_ISREG(st_mode) && !installing_bad_symlink) {
1416 /* TODO file is obstructed; do something */
1417 err = got_error_path(ondisk_path,
1418 GOT_ERR_FILE_OBSTRUCTED);
1421 err = got_opentemp_named_fd(&tmppath, &fd,
1428 return got_error_from_errno2("open", ondisk_path);
1431 if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1432 err = got_error_from_errno2("fchmod",
1433 update ? tmppath : ondisk_path);
1438 if (restoring_missing_file)
1439 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1441 else if (reverting_versioned_file)
1442 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1445 err = (*progress_cb)(progress_arg,
1446 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1451 hdrlen = got_object_blob_get_hdrlen(blob);
1453 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1454 err = got_object_blob_read_block(&len, blob);
1458 /* Skip blob object header first time around. */
1459 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1461 err = got_error_from_errno("write");
1463 } else if (outlen != len - hdrlen) {
1464 err = got_error(GOT_ERR_IO);
1471 if (fsync(fd) != 0) {
1472 err = got_error_from_errno("fsync");
1477 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1478 err = got_error_from_errno2("unlink", ondisk_path);
1481 if (rename(tmppath, ondisk_path) != 0) {
1482 err = got_error_from_errno3("rename", tmppath,
1491 if (fd != -1 && close(fd) == -1 && err == NULL)
1492 err = got_error_from_errno("close");
1493 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1494 err = got_error_from_errno2("unlink", tmppath);
1499 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1500 static const struct got_error *
1501 get_modified_file_content_status(unsigned char *status, FILE *f)
1503 const struct got_error *err = NULL;
1504 const char *markers[3] = {
1505 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1506 GOT_DIFF_CONFLICT_MARKER_SEP,
1507 GOT_DIFF_CONFLICT_MARKER_END
1511 size_t linesize = 0;
1514 while (*status == GOT_STATUS_MODIFY) {
1515 linelen = getline(&line, &linesize, f);
1516 if (linelen == -1) {
1519 err = got_ferror(f, GOT_ERR_IO);
1523 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1524 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1526 *status = GOT_STATUS_CONFLICT;
1537 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1539 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1540 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1544 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1546 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1547 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1548 ie->mtime_sec == sb->st_mtim.tv_sec &&
1549 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1550 ie->size == (sb->st_size & 0xffffffff) &&
1551 !xbit_differs(ie, sb->st_mode));
1554 static unsigned char
1555 get_staged_status(struct got_fileindex_entry *ie)
1557 switch (got_fileindex_entry_stage_get(ie)) {
1558 case GOT_FILEIDX_STAGE_ADD:
1559 return GOT_STATUS_ADD;
1560 case GOT_FILEIDX_STAGE_DELETE:
1561 return GOT_STATUS_DELETE;
1562 case GOT_FILEIDX_STAGE_MODIFY:
1563 return GOT_STATUS_MODIFY;
1565 return GOT_STATUS_NO_CHANGE;
1569 static const struct got_error *
1570 get_symlink_modification_status(unsigned char *status,
1571 struct got_fileindex_entry *ie, const char *abspath,
1572 int dirfd, const char *de_name, struct got_blob_object *blob)
1574 const struct got_error *err = NULL;
1575 char target_path[PATH_MAX];
1576 char etarget[PATH_MAX];
1578 size_t len, target_len = 0;
1579 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1580 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1582 *status = GOT_STATUS_NO_CHANGE;
1584 /* Blob object content specifies the target path of the link. */
1586 err = got_object_blob_read_block(&len, blob);
1589 if (len + target_len >= sizeof(target_path)) {
1591 * Should not happen. The blob contents were OK
1592 * when this symlink was installed.
1594 return got_error(GOT_ERR_NO_SPACE);
1597 /* Skip blob object header first time around. */
1598 memcpy(target_path + target_len, buf + hdrlen,
1600 target_len += len - hdrlen;
1604 target_path[target_len] = '\0';
1607 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1609 return got_error_from_errno2("readlinkat", abspath);
1611 elen = readlink(abspath, etarget, sizeof(etarget));
1613 return got_error_from_errno2("readlink", abspath);
1616 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1617 *status = GOT_STATUS_MODIFY;
1622 static const struct got_error *
1623 get_file_status(unsigned char *status, struct stat *sb,
1624 struct got_fileindex_entry *ie, const char *abspath,
1625 int dirfd, const char *de_name, struct got_repository *repo)
1627 const struct got_error *err = NULL;
1628 struct got_object_id id;
1633 struct got_blob_object *blob = NULL;
1635 unsigned char staged_status = get_staged_status(ie);
1637 *status = GOT_STATUS_NO_CHANGE;
1638 memset(sb, 0, sizeof(*sb));
1641 * Whenever the caller provides a directory descriptor and a
1642 * directory entry name for the file, use them! This prevents
1643 * race conditions if filesystem paths change beneath our feet.
1646 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1647 if (errno == ENOENT) {
1648 if (got_fileindex_entry_has_file_on_disk(ie))
1649 *status = GOT_STATUS_MISSING;
1651 *status = GOT_STATUS_DELETE;
1654 err = got_error_from_errno2("fstatat", abspath);
1658 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1659 if (fd == -1 && errno != ENOENT &&
1660 !got_err_open_nofollow_on_symlink())
1661 return got_error_from_errno2("open", abspath);
1662 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1663 if (lstat(abspath, sb) == -1)
1664 return got_error_from_errno2("lstat", abspath);
1665 } else if (fd == -1 || fstat(fd, sb) == -1) {
1666 if (errno == ENOENT) {
1667 if (got_fileindex_entry_has_file_on_disk(ie))
1668 *status = GOT_STATUS_MISSING;
1670 *status = GOT_STATUS_DELETE;
1673 err = got_error_from_errno2("fstat", abspath);
1678 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1679 *status = GOT_STATUS_OBSTRUCTED;
1683 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1684 *status = GOT_STATUS_DELETE;
1686 } else if (!got_fileindex_entry_has_blob(ie) &&
1687 staged_status != GOT_STATUS_ADD) {
1688 *status = GOT_STATUS_ADD;
1692 if (!stat_info_differs(ie, sb))
1695 if (S_ISLNK(sb->st_mode) &&
1696 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1697 *status = GOT_STATUS_MODIFY;
1701 if (staged_status == GOT_STATUS_MODIFY ||
1702 staged_status == GOT_STATUS_ADD)
1703 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1705 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1707 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1711 if (S_ISLNK(sb->st_mode)) {
1712 err = get_symlink_modification_status(status, ie,
1713 abspath, dirfd, de_name, blob);
1718 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1720 err = got_error_from_errno2("openat", abspath);
1725 f = fdopen(fd, "r");
1727 err = got_error_from_errno2("fdopen", abspath);
1731 hdrlen = got_object_blob_get_hdrlen(blob);
1733 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1734 err = got_object_blob_read_block(&blen, blob);
1737 /* Skip length of blob object header first time around. */
1738 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1739 if (flen == 0 && ferror(f)) {
1740 err = got_error_from_errno("fread");
1743 if (blen - hdrlen == 0) {
1745 *status = GOT_STATUS_MODIFY;
1747 } else if (flen == 0) {
1748 if (blen - hdrlen != 0)
1749 *status = GOT_STATUS_MODIFY;
1751 } else if (blen - hdrlen == flen) {
1752 /* Skip blob object header first time around. */
1753 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1754 *status = GOT_STATUS_MODIFY;
1758 *status = GOT_STATUS_MODIFY;
1764 if (*status == GOT_STATUS_MODIFY) {
1766 err = get_modified_file_content_status(status, f);
1767 } else if (xbit_differs(ie, sb->st_mode))
1768 *status = GOT_STATUS_MODE_CHANGE;
1771 got_object_blob_close(blob);
1772 if (f != NULL && fclose(f) == EOF && err == NULL)
1773 err = got_error_from_errno2("fclose", abspath);
1774 if (fd != -1 && close(fd) == -1 && err == NULL)
1775 err = got_error_from_errno2("close", abspath);
1780 * Update timestamps in the file index if a file is unmodified and
1781 * we had to run a full content comparison to find out.
1783 static const struct got_error *
1784 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1785 struct got_fileindex_entry *ie, struct stat *sb)
1787 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1788 return got_fileindex_entry_update(ie, wt_fd, path,
1789 ie->blob_sha1, ie->commit_sha1, 1);
1794 static const struct got_error *
1795 update_blob(struct got_worktree *worktree,
1796 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1797 struct got_tree_entry *te, const char *path,
1798 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1801 const struct got_error *err = NULL;
1802 struct got_blob_object *blob = NULL;
1804 unsigned char status = GOT_STATUS_NO_CHANGE;
1807 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1808 return got_error_from_errno("asprintf");
1811 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1812 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1815 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1819 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1820 sb.st_mode = got_fileindex_perms_to_st(ie);
1822 if (stat(ondisk_path, &sb) == -1) {
1823 if (errno != ENOENT) {
1824 err = got_error_from_errno2("stat",
1828 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1829 status = GOT_STATUS_UNVERSIONED;
1831 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1832 status = GOT_STATUS_UNVERSIONED;
1834 status = GOT_STATUS_OBSTRUCTED;
1838 if (status == GOT_STATUS_OBSTRUCTED) {
1840 got_fileindex_entry_mark_skipped(ie);
1841 err = (*progress_cb)(progress_arg, status, path);
1844 if (status == GOT_STATUS_CONFLICT) {
1846 got_fileindex_entry_mark_skipped(ie);
1847 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1852 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1853 (S_ISLNK(te->mode) ||
1854 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1856 * This is a regular file or an installed bad symlink.
1857 * If the file index indicates that this file is already
1858 * up-to-date with respect to the repository we can skip
1859 * updating contents of this file.
1861 if (got_fileindex_entry_has_commit(ie) &&
1862 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1863 SHA1_DIGEST_LENGTH) == 0) {
1865 err = sync_timestamps(worktree->root_fd,
1866 path, status, ie, &sb);
1869 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1873 if (got_fileindex_entry_has_blob(ie) &&
1874 memcmp(ie->blob_sha1, te->id.sha1,
1875 SHA1_DIGEST_LENGTH) == 0) {
1876 /* Different commit but the same blob. */
1877 err = sync_timestamps(worktree->root_fd,
1878 path, status, ie, &sb);
1881 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1887 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1891 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1892 int update_timestamps;
1893 struct got_blob_object *blob2 = NULL;
1894 char *label_orig = NULL;
1895 if (got_fileindex_entry_has_blob(ie)) {
1896 struct got_object_id id2;
1897 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1898 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1902 if (got_fileindex_entry_has_commit(ie)) {
1903 char id_str[SHA1_DIGEST_STRING_LENGTH];
1904 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1905 sizeof(id_str)) == NULL) {
1906 err = got_error_path(id_str,
1907 GOT_ERR_BAD_OBJ_ID_STR);
1910 if (asprintf(&label_orig, "%s: commit %s",
1911 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1912 err = got_error_from_errno("asprintf");
1916 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1918 err = got_object_blob_read_to_str(&link_target, blob);
1921 err = merge_symlink(worktree, blob2, ondisk_path, path,
1922 label_orig, link_target, worktree->base_commit_id,
1923 repo, progress_cb, progress_arg);
1926 err = merge_blob(&update_timestamps, worktree, blob2,
1927 ondisk_path, path, sb.st_mode, label_orig, blob,
1928 worktree->base_commit_id, repo,
1929 progress_cb, progress_arg);
1933 got_object_blob_close(blob2);
1937 * Do not update timestamps of files with local changes.
1938 * Otherwise, a future status walk would treat them as
1939 * unmodified files again.
1941 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1942 blob->id.sha1, worktree->base_commit_id->sha1,
1944 } else if (status == GOT_STATUS_MODE_CHANGE) {
1945 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1946 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1947 } else if (status == GOT_STATUS_DELETE) {
1948 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1951 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1952 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1956 int is_bad_symlink = 0;
1957 if (S_ISLNK(te->mode)) {
1958 err = install_symlink(&is_bad_symlink, worktree,
1959 ondisk_path, path, blob,
1960 status == GOT_STATUS_MISSING, 0,
1961 status == GOT_STATUS_UNVERSIONED, 0,
1962 repo, progress_cb, progress_arg);
1964 err = install_blob(worktree, ondisk_path, path,
1965 te->mode, sb.st_mode, blob,
1966 status == GOT_STATUS_MISSING, 0, 0,
1967 status == GOT_STATUS_UNVERSIONED, repo,
1968 progress_cb, progress_arg);
1974 err = got_fileindex_entry_update(ie,
1975 worktree->root_fd, path, blob->id.sha1,
1976 worktree->base_commit_id->sha1, 1);
1978 err = create_fileindex_entry(&ie, fileindex,
1979 worktree->base_commit_id, worktree->root_fd, path,
1985 if (is_bad_symlink) {
1986 got_fileindex_entry_filetype_set(ie,
1987 GOT_FILEIDX_MODE_BAD_SYMLINK);
1990 got_object_blob_close(blob);
1996 static const struct got_error *
1997 remove_ondisk_file(const char *root_path, const char *path)
1999 const struct got_error *err = NULL;
2000 char *ondisk_path = NULL, *parent = NULL;
2002 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2003 return got_error_from_errno("asprintf");
2005 if (unlink(ondisk_path) == -1) {
2006 if (errno != ENOENT)
2007 err = got_error_from_errno2("unlink", ondisk_path);
2009 size_t root_len = strlen(root_path);
2010 err = got_path_dirname(&parent, ondisk_path);
2013 while (got_path_cmp(parent, root_path,
2014 strlen(parent), root_len) != 0) {
2016 ondisk_path = parent;
2018 if (rmdir(ondisk_path) == -1) {
2019 if (errno != ENOTEMPTY)
2020 err = got_error_from_errno2("rmdir",
2024 err = got_path_dirname(&parent, ondisk_path);
2035 static const struct got_error *
2036 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2037 struct got_fileindex_entry *ie, struct got_repository *repo,
2038 got_worktree_checkout_cb progress_cb, void *progress_arg)
2040 const struct got_error *err = NULL;
2041 unsigned char status;
2045 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2046 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2048 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2050 return got_error_from_errno("asprintf");
2052 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2056 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2057 char ondisk_target[PATH_MAX];
2058 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2059 sizeof(ondisk_target));
2060 if (ondisk_len == -1) {
2061 err = got_error_from_errno2("readlink", ondisk_path);
2064 ondisk_target[ondisk_len] = '\0';
2065 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2066 NULL, NULL, /* XXX pass common ancestor info? */
2067 ondisk_target, ondisk_path);
2070 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2075 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2076 status == GOT_STATUS_ADD) {
2077 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2081 * Preserve the working file and change the deleted blob's
2082 * entry into a schedule-add entry.
2084 err = got_fileindex_entry_update(ie, worktree->root_fd,
2085 ie->path, NULL, NULL, 0);
2087 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2090 if (status == GOT_STATUS_NO_CHANGE) {
2091 err = remove_ondisk_file(worktree->root_path, ie->path);
2095 got_fileindex_entry_remove(fileindex, ie);
2102 struct diff_cb_arg {
2103 struct got_fileindex *fileindex;
2104 struct got_worktree *worktree;
2105 struct got_repository *repo;
2106 got_worktree_checkout_cb progress_cb;
2108 got_cancel_cb cancel_cb;
2112 static const struct got_error *
2113 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2114 struct got_tree_entry *te, const char *parent_path)
2116 struct diff_cb_arg *a = arg;
2118 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2119 return got_error(GOT_ERR_CANCELLED);
2121 return update_blob(a->worktree, a->fileindex, ie, te,
2122 ie->path, a->repo, a->progress_cb, a->progress_arg);
2125 static const struct got_error *
2126 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2128 struct diff_cb_arg *a = arg;
2130 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2131 return got_error(GOT_ERR_CANCELLED);
2133 return delete_blob(a->worktree, a->fileindex, ie,
2134 a->repo, a->progress_cb, a->progress_arg);
2137 static const struct got_error *
2138 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2140 struct diff_cb_arg *a = arg;
2141 const struct got_error *err;
2144 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2145 return got_error(GOT_ERR_CANCELLED);
2147 if (got_object_tree_entry_is_submodule(te))
2150 if (asprintf(&path, "%s%s%s", parent_path,
2151 parent_path[0] ? "/" : "", te->name)
2153 return got_error_from_errno("asprintf");
2155 if (S_ISDIR(te->mode))
2156 err = add_dir_on_disk(a->worktree, path);
2158 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2159 a->repo, a->progress_cb, a->progress_arg);
2165 const struct got_error *
2166 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2168 uint32_t uuid_status;
2170 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2171 if (uuid_status != uuid_s_ok) {
2173 return got_error_uuid(uuid_status, "uuid_to_string");
2179 static const struct got_error *
2180 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2182 const struct got_error *err = NULL;
2183 char *uuidstr = NULL;
2187 err = got_worktree_get_uuid(&uuidstr, worktree);
2191 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2192 err = got_error_from_errno("asprintf");
2199 const struct got_error *
2200 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2202 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2205 static const struct got_error *
2206 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2208 return get_ref_name(refname, worktree,
2209 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2212 static const struct got_error *
2213 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2215 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2218 static const struct got_error *
2219 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2221 return get_ref_name(refname, worktree,
2222 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2225 static const struct got_error *
2226 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2228 return get_ref_name(refname, worktree,
2229 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2232 static const struct got_error *
2233 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2235 return get_ref_name(refname, worktree,
2236 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2239 static const struct got_error *
2240 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2242 return get_ref_name(refname, worktree,
2243 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2246 static const struct got_error *
2247 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2249 return get_ref_name(refname, worktree,
2250 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2253 static const struct got_error *
2254 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2256 return get_ref_name(refname, worktree,
2257 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2260 const struct got_error *
2261 got_worktree_get_histedit_script_path(char **path,
2262 struct got_worktree *worktree)
2264 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2265 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2267 return got_error_from_errno("asprintf");
2272 static const struct got_error *
2273 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2275 return get_ref_name(refname, worktree,
2276 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2279 static const struct got_error *
2280 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2282 return get_ref_name(refname, worktree,
2283 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2287 * Prevent Git's garbage collector from deleting our base commit by
2288 * setting a reference to our base commit's ID.
2290 static const struct got_error *
2291 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2293 const struct got_error *err = NULL;
2294 struct got_reference *ref = NULL;
2297 err = got_worktree_get_base_ref_name(&refname, worktree);
2301 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2305 err = got_ref_write(ref, repo);
2313 static const struct got_error *
2314 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2316 const struct got_error *err = NULL;
2318 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2319 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2320 err = got_error_from_errno("asprintf");
2321 *fileindex_path = NULL;
2327 static const struct got_error *
2328 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2329 struct got_worktree *worktree)
2331 const struct got_error *err = NULL;
2334 *fileindex_path = NULL;
2335 *fileindex = got_fileindex_alloc();
2336 if (*fileindex == NULL)
2337 return got_error_from_errno("got_fileindex_alloc");
2339 err = get_fileindex_path(fileindex_path, worktree);
2343 index = fopen(*fileindex_path, "rbe");
2344 if (index == NULL) {
2345 if (errno != ENOENT)
2346 err = got_error_from_errno2("fopen", *fileindex_path);
2348 err = got_fileindex_read(*fileindex, index);
2349 if (fclose(index) == EOF && err == NULL)
2350 err = got_error_from_errno("fclose");
2354 free(*fileindex_path);
2355 *fileindex_path = NULL;
2356 got_fileindex_free(*fileindex);
2362 struct bump_base_commit_id_arg {
2363 struct got_object_id *base_commit_id;
2366 const char *entry_name;
2367 got_worktree_checkout_cb progress_cb;
2371 /* Bump base commit ID of all files within an updated part of the work tree. */
2372 static const struct got_error *
2373 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2375 const struct got_error *err;
2376 struct bump_base_commit_id_arg *a = arg;
2378 if (a->entry_name) {
2379 if (strcmp(ie->path, a->path) != 0)
2381 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2384 if (got_fileindex_entry_was_skipped(ie))
2387 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2388 SHA1_DIGEST_LENGTH) == 0)
2391 if (a->progress_cb) {
2392 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2397 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2401 static const struct got_error *
2402 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2403 struct got_fileindex *fileindex,
2404 got_worktree_checkout_cb progress_cb, void *progress_arg)
2406 struct bump_base_commit_id_arg bbc_arg;
2408 bbc_arg.base_commit_id = worktree->base_commit_id;
2409 bbc_arg.entry_name = NULL;
2411 bbc_arg.path_len = 0;
2412 bbc_arg.progress_cb = progress_cb;
2413 bbc_arg.progress_arg = progress_arg;
2415 return got_fileindex_for_each_entry_safe(fileindex,
2416 bump_base_commit_id, &bbc_arg);
2419 static const struct got_error *
2420 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2422 const struct got_error *err = NULL;
2423 char *new_fileindex_path = NULL;
2424 FILE *new_index = NULL;
2425 struct timespec timeout;
2427 err = got_opentemp_named(&new_fileindex_path, &new_index,
2432 err = got_fileindex_write(fileindex, new_index);
2436 if (rename(new_fileindex_path, fileindex_path) != 0) {
2437 err = got_error_from_errno3("rename", new_fileindex_path,
2439 unlink(new_fileindex_path);
2443 * Sleep for a short amount of time to ensure that files modified after
2444 * this program exits have a different time stamp from the one which
2445 * was recorded in the file index.
2448 timeout.tv_nsec = 1;
2449 nanosleep(&timeout, NULL);
2453 free(new_fileindex_path);
2457 static const struct got_error *
2458 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2459 struct got_object_id **tree_id, const char *wt_relpath,
2460 struct got_worktree *worktree, struct got_repository *repo)
2462 const struct got_error *err = NULL;
2463 struct got_object_id *id = NULL;
2464 char *in_repo_path = NULL;
2465 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2467 *entry_type = GOT_OBJ_TYPE_ANY;
2468 *tree_relpath = NULL;
2471 if (wt_relpath[0] == '\0') {
2472 /* Check out all files within the work tree. */
2473 *entry_type = GOT_OBJ_TYPE_TREE;
2474 *tree_relpath = strdup("");
2475 if (*tree_relpath == NULL) {
2476 err = got_error_from_errno("strdup");
2479 err = got_object_id_by_path(tree_id, repo,
2480 worktree->base_commit_id, worktree->path_prefix);
2486 /* Check out a subset of files in the work tree. */
2488 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2489 is_root_wt ? "" : "/", wt_relpath) == -1) {
2490 err = got_error_from_errno("asprintf");
2494 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2500 in_repo_path = NULL;
2502 err = got_object_get_type(entry_type, repo, id);
2506 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2507 /* Check out a single file. */
2508 if (strchr(wt_relpath, '/') == NULL) {
2509 /* Check out a single file in work tree's root dir. */
2510 in_repo_path = strdup(worktree->path_prefix);
2511 if (in_repo_path == NULL) {
2512 err = got_error_from_errno("strdup");
2515 *tree_relpath = strdup("");
2516 if (*tree_relpath == NULL) {
2517 err = got_error_from_errno("strdup");
2521 /* Check out a single file in a subdirectory. */
2522 err = got_path_dirname(tree_relpath, wt_relpath);
2525 if (asprintf(&in_repo_path, "%s%s%s",
2526 worktree->path_prefix, is_root_wt ? "" : "/",
2527 *tree_relpath) == -1) {
2528 err = got_error_from_errno("asprintf");
2532 err = got_object_id_by_path(tree_id, repo,
2533 worktree->base_commit_id, in_repo_path);
2535 /* Check out all files within a subdirectory. */
2536 *tree_id = got_object_id_dup(id);
2537 if (*tree_id == NULL) {
2538 err = got_error_from_errno("got_object_id_dup");
2541 *tree_relpath = strdup(wt_relpath);
2542 if (*tree_relpath == NULL) {
2543 err = got_error_from_errno("strdup");
2551 *entry_type = GOT_OBJ_TYPE_ANY;
2552 free(*tree_relpath);
2553 *tree_relpath = NULL;
2560 static const struct got_error *
2561 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2562 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2563 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2564 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2566 const struct got_error *err = NULL;
2567 struct got_commit_object *commit = NULL;
2568 struct got_tree_object *tree = NULL;
2569 struct got_fileindex_diff_tree_cb diff_cb;
2570 struct diff_cb_arg arg;
2572 err = ref_base_commit(worktree, repo);
2574 if (!(err->code == GOT_ERR_ERRNO &&
2575 (errno == EACCES || errno == EROFS)))
2577 err = (*progress_cb)(progress_arg,
2578 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2583 err = got_object_open_as_commit(&commit, repo,
2584 worktree->base_commit_id);
2588 err = got_object_open_as_tree(&tree, repo, tree_id);
2593 got_object_tree_find_entry(tree, entry_name) == NULL) {
2594 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2598 diff_cb.diff_old_new = diff_old_new;
2599 diff_cb.diff_old = diff_old;
2600 diff_cb.diff_new = diff_new;
2601 arg.fileindex = fileindex;
2602 arg.worktree = worktree;
2604 arg.progress_cb = progress_cb;
2605 arg.progress_arg = progress_arg;
2606 arg.cancel_cb = cancel_cb;
2607 arg.cancel_arg = cancel_arg;
2608 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2609 entry_name, repo, &diff_cb, &arg);
2612 got_object_tree_close(tree);
2614 got_object_commit_close(commit);
2618 const struct got_error *
2619 got_worktree_checkout_files(struct got_worktree *worktree,
2620 struct got_pathlist_head *paths, struct got_repository *repo,
2621 got_worktree_checkout_cb progress_cb, void *progress_arg,
2622 got_cancel_cb cancel_cb, void *cancel_arg)
2624 const struct got_error *err = NULL, *sync_err, *unlockerr;
2625 struct got_commit_object *commit = NULL;
2626 struct got_tree_object *tree = NULL;
2627 struct got_fileindex *fileindex = NULL;
2628 char *fileindex_path = NULL;
2629 struct got_pathlist_entry *pe;
2630 struct tree_path_data {
2631 STAILQ_ENTRY(tree_path_data) entry;
2632 struct got_object_id *tree_id;
2637 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2639 STAILQ_INIT(&tree_paths);
2641 err = lock_worktree(worktree, LOCK_EX);
2645 /* Map all specified paths to in-repository trees. */
2646 TAILQ_FOREACH(pe, paths, entry) {
2647 tpd = malloc(sizeof(*tpd));
2649 err = got_error_from_errno("malloc");
2653 err = find_tree_entry_for_checkout(&tpd->entry_type,
2654 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2660 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2661 err = got_path_basename(&tpd->entry_name, pe->path);
2669 tpd->entry_name = NULL;
2671 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2675 * Read the file index.
2676 * Checking out files is supposed to be an idempotent operation.
2677 * If the on-disk file index is incomplete we will try to complete it.
2679 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2683 tpd = STAILQ_FIRST(&tree_paths);
2684 TAILQ_FOREACH(pe, paths, entry) {
2685 struct bump_base_commit_id_arg bbc_arg;
2687 err = checkout_files(worktree, fileindex, tpd->relpath,
2688 tpd->tree_id, tpd->entry_name, repo,
2689 progress_cb, progress_arg, cancel_cb, cancel_arg);
2693 bbc_arg.base_commit_id = worktree->base_commit_id;
2694 bbc_arg.entry_name = tpd->entry_name;
2695 bbc_arg.path = pe->path;
2696 bbc_arg.path_len = pe->path_len;
2697 bbc_arg.progress_cb = progress_cb;
2698 bbc_arg.progress_arg = progress_arg;
2699 err = got_fileindex_for_each_entry_safe(fileindex,
2700 bump_base_commit_id, &bbc_arg);
2704 tpd = STAILQ_NEXT(tpd, entry);
2706 sync_err = sync_fileindex(fileindex, fileindex_path);
2707 if (sync_err && err == NULL)
2710 free(fileindex_path);
2712 got_object_tree_close(tree);
2714 got_object_commit_close(commit);
2716 got_fileindex_free(fileindex);
2717 while (!STAILQ_EMPTY(&tree_paths)) {
2718 tpd = STAILQ_FIRST(&tree_paths);
2719 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2724 unlockerr = lock_worktree(worktree, LOCK_SH);
2725 if (unlockerr && err == NULL)
2730 struct merge_file_cb_arg {
2731 struct got_worktree *worktree;
2732 struct got_fileindex *fileindex;
2733 got_worktree_checkout_cb progress_cb;
2735 got_cancel_cb cancel_cb;
2737 const char *label_orig;
2738 struct got_object_id *commit_id2;
2739 int allow_bad_symlinks;
2742 static const struct got_error *
2743 merge_file_cb(void *arg, struct got_blob_object *blob1,
2744 struct got_blob_object *blob2, struct got_object_id *id1,
2745 struct got_object_id *id2, const char *path1, const char *path2,
2746 mode_t mode1, mode_t mode2, struct got_repository *repo)
2748 static const struct got_error *err = NULL;
2749 struct merge_file_cb_arg *a = arg;
2750 struct got_fileindex_entry *ie;
2751 char *ondisk_path = NULL;
2753 unsigned char status;
2754 int local_changes_subsumed;
2755 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2756 char *id_str = NULL, *label_deriv2 = NULL;
2758 if (blob1 && blob2) {
2759 ie = got_fileindex_entry_get(a->fileindex, path2,
2762 return (*a->progress_cb)(a->progress_arg,
2763 GOT_STATUS_MISSING, path2);
2765 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2767 return got_error_from_errno("asprintf");
2769 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2774 if (status == GOT_STATUS_DELETE) {
2775 err = (*a->progress_cb)(a->progress_arg,
2776 GOT_STATUS_MERGE, path2);
2779 if (status != GOT_STATUS_NO_CHANGE &&
2780 status != GOT_STATUS_MODIFY &&
2781 status != GOT_STATUS_CONFLICT &&
2782 status != GOT_STATUS_ADD) {
2783 err = (*a->progress_cb)(a->progress_arg, status, path2);
2787 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2789 err = got_object_blob_read_to_str(&link_target2, blob2);
2792 err = merge_symlink(a->worktree, blob1, ondisk_path,
2793 path2, a->label_orig, link_target2, a->commit_id2,
2794 repo, a->progress_cb, a->progress_arg);
2799 f_orig = got_opentemp();
2800 if (f_orig == NULL) {
2801 err = got_error_from_errno("got_opentemp");
2804 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2809 f_deriv2 = got_opentemp();
2810 if (f_deriv2 == NULL)
2812 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2817 fd = open(ondisk_path,
2818 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2820 err = got_error_from_errno2("open",
2824 f_deriv = fdopen(fd, "r");
2825 if (f_deriv == NULL) {
2826 err = got_error_from_errno2("fdopen",
2831 err = got_object_id_str(&id_str, a->commit_id2);
2834 if (asprintf(&label_deriv2, "%s: commit %s",
2835 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2836 err = got_error_from_errno("asprintf");
2839 err = merge_file(&local_changes_subsumed, a->worktree,
2840 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2841 sb.st_mode, a->label_orig, NULL, label_deriv2,
2842 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2843 a->progress_cb, a->progress_arg);
2846 ie = got_fileindex_entry_get(a->fileindex, path1,
2849 return (*a->progress_cb)(a->progress_arg,
2850 GOT_STATUS_MISSING, path1);
2852 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2854 return got_error_from_errno("asprintf");
2856 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2862 case GOT_STATUS_NO_CHANGE:
2863 err = (*a->progress_cb)(a->progress_arg,
2864 GOT_STATUS_DELETE, path1);
2867 err = remove_ondisk_file(a->worktree->root_path, path1);
2871 got_fileindex_entry_mark_deleted_from_disk(ie);
2873 case GOT_STATUS_DELETE:
2874 case GOT_STATUS_MISSING:
2875 err = (*a->progress_cb)(a->progress_arg,
2876 GOT_STATUS_DELETE, path1);
2880 got_fileindex_entry_mark_deleted_from_disk(ie);
2882 case GOT_STATUS_ADD: {
2883 struct got_object_id *id;
2887 * Delete the added file only if its content already
2888 * exists in the repository.
2890 err = got_object_blob_file_create(&id, &blob1_f,
2891 &blob1_size, path1);
2894 if (got_object_id_cmp(id, id1) == 0) {
2895 err = (*a->progress_cb)(a->progress_arg,
2896 GOT_STATUS_DELETE, path1);
2899 err = remove_ondisk_file(a->worktree->root_path,
2904 got_fileindex_entry_remove(a->fileindex,
2907 err = (*a->progress_cb)(a->progress_arg,
2908 GOT_STATUS_CANNOT_DELETE, path1);
2910 if (fclose(blob1_f) == EOF && err == NULL)
2911 err = got_error_from_errno("fclose");
2917 case GOT_STATUS_MODIFY:
2918 case GOT_STATUS_CONFLICT:
2919 err = (*a->progress_cb)(a->progress_arg,
2920 GOT_STATUS_CANNOT_DELETE, path1);
2924 case GOT_STATUS_OBSTRUCTED:
2925 err = (*a->progress_cb)(a->progress_arg, status, path1);
2933 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2935 return got_error_from_errno("asprintf");
2936 ie = got_fileindex_entry_get(a->fileindex, path2,
2939 err = get_file_status(&status, &sb, ie, ondisk_path,
2943 if (status != GOT_STATUS_NO_CHANGE &&
2944 status != GOT_STATUS_MODIFY &&
2945 status != GOT_STATUS_CONFLICT &&
2946 status != GOT_STATUS_ADD) {
2947 err = (*a->progress_cb)(a->progress_arg,
2951 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2953 err = got_object_blob_read_to_str(&link_target2,
2957 err = merge_symlink(a->worktree, NULL,
2958 ondisk_path, path2, a->label_orig,
2959 link_target2, a->commit_id2, repo,
2960 a->progress_cb, a->progress_arg);
2962 } else if (S_ISREG(sb.st_mode)) {
2963 err = merge_blob(&local_changes_subsumed,
2964 a->worktree, NULL, ondisk_path, path2,
2965 sb.st_mode, a->label_orig, blob2,
2966 a->commit_id2, repo, a->progress_cb,
2969 err = got_error_path(ondisk_path,
2970 GOT_ERR_FILE_OBSTRUCTED);
2974 if (status == GOT_STATUS_DELETE) {
2975 err = got_fileindex_entry_update(ie,
2976 a->worktree->root_fd, path2, blob2->id.sha1,
2977 a->worktree->base_commit_id->sha1, 0);
2982 int is_bad_symlink = 0;
2983 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2984 if (S_ISLNK(mode2)) {
2985 err = install_symlink(&is_bad_symlink,
2986 a->worktree, ondisk_path, path2, blob2, 0,
2987 0, 1, a->allow_bad_symlinks, repo,
2988 a->progress_cb, a->progress_arg);
2990 err = install_blob(a->worktree, ondisk_path, path2,
2991 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2992 a->progress_cb, a->progress_arg);
2996 err = got_fileindex_entry_alloc(&ie, path2);
2999 err = got_fileindex_entry_update(ie,
3000 a->worktree->root_fd, path2, NULL, NULL, 1);
3002 got_fileindex_entry_free(ie);
3005 err = got_fileindex_entry_add(a->fileindex, ie);
3007 got_fileindex_entry_free(ie);
3010 if (is_bad_symlink) {
3011 got_fileindex_entry_filetype_set(ie,
3012 GOT_FILEIDX_MODE_BAD_SYMLINK);
3017 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3018 err = got_error_from_errno("fclose");
3019 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3020 err = got_error_from_errno("fclose");
3021 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3022 err = got_error_from_errno("fclose");
3029 static const struct got_error *
3030 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3032 struct got_worktree *worktree = arg;
3034 /* Reject merges into a work tree with mixed base commits. */
3035 if (got_fileindex_entry_has_commit(ie) &&
3036 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3037 SHA1_DIGEST_LENGTH) != 0)
3038 return got_error(GOT_ERR_MIXED_COMMITS);
3043 struct check_merge_conflicts_arg {
3044 struct got_worktree *worktree;
3045 struct got_fileindex *fileindex;
3046 struct got_repository *repo;
3049 static const struct got_error *
3050 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3051 struct got_blob_object *blob2, struct got_object_id *id1,
3052 struct got_object_id *id2, const char *path1, const char *path2,
3053 mode_t mode1, mode_t mode2, struct got_repository *repo)
3055 const struct got_error *err = NULL;
3056 struct check_merge_conflicts_arg *a = arg;
3057 unsigned char status;
3059 struct got_fileindex_entry *ie;
3060 const char *path = path2 ? path2 : path1;
3061 struct got_object_id *id = id2 ? id2 : id1;
3067 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3071 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3073 return got_error_from_errno("asprintf");
3075 /* Reject merges into a work tree with conflicted files. */
3076 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3080 if (status == GOT_STATUS_CONFLICT)
3081 return got_error(GOT_ERR_CONFLICTS);
3086 static const struct got_error *
3087 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3088 const char *fileindex_path, struct got_object_id *commit_id1,
3089 struct got_object_id *commit_id2, struct got_repository *repo,
3090 got_worktree_checkout_cb progress_cb, void *progress_arg,
3091 got_cancel_cb cancel_cb, void *cancel_arg)
3093 const struct got_error *err = NULL, *sync_err;
3094 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3095 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3096 struct check_merge_conflicts_arg cmc_arg;
3097 struct merge_file_cb_arg arg;
3098 char *label_orig = NULL;
3101 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3102 worktree->path_prefix);
3103 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3109 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3113 err = got_object_id_str(&id_str, commit_id1);
3117 if (asprintf(&label_orig, "%s: commit %s",
3118 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3119 err = got_error_from_errno("asprintf");
3126 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3127 worktree->path_prefix);
3131 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3135 cmc_arg.worktree = worktree;
3136 cmc_arg.fileindex = fileindex;
3137 cmc_arg.repo = repo;
3138 err = got_diff_tree(tree1, tree2, "", "", repo,
3139 check_merge_conflicts, &cmc_arg, 0);
3143 arg.worktree = worktree;
3144 arg.fileindex = fileindex;
3145 arg.progress_cb = progress_cb;
3146 arg.progress_arg = progress_arg;
3147 arg.cancel_cb = cancel_cb;
3148 arg.cancel_arg = cancel_arg;
3149 arg.label_orig = label_orig;
3150 arg.commit_id2 = commit_id2;
3151 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3152 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3153 sync_err = sync_fileindex(fileindex, fileindex_path);
3154 if (sync_err && err == NULL)
3158 got_object_tree_close(tree1);
3160 got_object_tree_close(tree2);
3165 const struct got_error *
3166 got_worktree_merge_files(struct got_worktree *worktree,
3167 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3168 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3169 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3171 const struct got_error *err, *unlockerr;
3172 char *fileindex_path = NULL;
3173 struct got_fileindex *fileindex = NULL;
3175 err = lock_worktree(worktree, LOCK_EX);
3179 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3183 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3188 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3189 commit_id2, repo, progress_cb, progress_arg,
3190 cancel_cb, cancel_arg);
3193 got_fileindex_free(fileindex);
3194 free(fileindex_path);
3195 unlockerr = lock_worktree(worktree, LOCK_SH);
3196 if (unlockerr && err == NULL)
3201 struct diff_dir_cb_arg {
3202 struct got_fileindex *fileindex;
3203 struct got_worktree *worktree;
3204 const char *status_path;
3205 size_t status_path_len;
3206 struct got_repository *repo;
3207 got_worktree_status_cb status_cb;
3209 got_cancel_cb cancel_cb;
3211 /* A pathlist containing per-directory pathlists of ignore patterns. */
3212 struct got_pathlist_head *ignores;
3213 int report_unchanged;
3217 static const struct got_error *
3218 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3219 int dirfd, const char *de_name,
3220 got_worktree_status_cb status_cb, void *status_arg,
3221 struct got_repository *repo, int report_unchanged)
3223 const struct got_error *err = NULL;
3224 unsigned char status = GOT_STATUS_NO_CHANGE;
3225 unsigned char staged_status = get_staged_status(ie);
3227 struct got_object_id blob_id, commit_id, staged_blob_id;
3228 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3229 struct got_object_id *staged_blob_idp = NULL;
3231 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3235 if (status == GOT_STATUS_NO_CHANGE &&
3236 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3239 if (got_fileindex_entry_has_blob(ie)) {
3240 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3241 blob_idp = &blob_id;
3243 if (got_fileindex_entry_has_commit(ie)) {
3244 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3245 commit_idp = &commit_id;
3247 if (staged_status == GOT_STATUS_ADD ||
3248 staged_status == GOT_STATUS_MODIFY) {
3249 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3250 SHA1_DIGEST_LENGTH);
3251 staged_blob_idp = &staged_blob_id;
3254 return (*status_cb)(status_arg, status, staged_status,
3255 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3258 static const struct got_error *
3259 status_old_new(void *arg, struct got_fileindex_entry *ie,
3260 struct dirent *de, const char *parent_path, int dirfd)
3262 const struct got_error *err = NULL;
3263 struct diff_dir_cb_arg *a = arg;
3266 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3267 return got_error(GOT_ERR_CANCELLED);
3269 if (got_path_cmp(parent_path, a->status_path,
3270 strlen(parent_path), a->status_path_len) != 0 &&
3271 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3274 if (parent_path[0]) {
3275 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3276 parent_path, de->d_name) == -1)
3277 return got_error_from_errno("asprintf");
3279 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3281 return got_error_from_errno("asprintf");
3284 err = report_file_status(ie, abspath, dirfd, de->d_name,
3285 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3290 static const struct got_error *
3291 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3293 struct diff_dir_cb_arg *a = arg;
3294 struct got_object_id blob_id, commit_id;
3295 unsigned char status;
3297 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3298 return got_error(GOT_ERR_CANCELLED);
3300 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3303 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3304 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3305 if (got_fileindex_entry_has_file_on_disk(ie))
3306 status = GOT_STATUS_MISSING;
3308 status = GOT_STATUS_DELETE;
3309 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3310 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3314 free_ignorelist(struct got_pathlist_head *ignorelist)
3316 struct got_pathlist_entry *pe;
3318 TAILQ_FOREACH(pe, ignorelist, entry)
3319 free((char *)pe->path);
3320 got_pathlist_free(ignorelist);
3324 free_ignores(struct got_pathlist_head *ignores)
3326 struct got_pathlist_entry *pe;
3328 TAILQ_FOREACH(pe, ignores, entry) {
3329 struct got_pathlist_head *ignorelist = pe->data;
3330 free_ignorelist(ignorelist);
3331 free((char *)pe->path);
3333 got_pathlist_free(ignores);
3336 static const struct got_error *
3337 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3339 const struct got_error *err = NULL;
3340 struct got_pathlist_entry *pe = NULL;
3341 struct got_pathlist_head *ignorelist;
3342 char *line = NULL, *pattern, *dirpath = NULL;
3343 size_t linesize = 0;
3346 ignorelist = calloc(1, sizeof(*ignorelist));
3347 if (ignorelist == NULL)
3348 return got_error_from_errno("calloc");
3349 TAILQ_INIT(ignorelist);
3351 while ((linelen = getline(&line, &linesize, f)) != -1) {
3352 if (linelen > 0 && line[linelen - 1] == '\n')
3353 line[linelen - 1] = '\0';
3355 /* Git's ignores may contain comments. */
3359 /* Git's negated patterns are not (yet?) supported. */
3363 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3365 err = got_error_from_errno("asprintf");
3368 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3373 err = got_error_from_errno("getline");
3377 dirpath = strdup(path);
3378 if (dirpath == NULL) {
3379 err = got_error_from_errno("strdup");
3382 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3385 if (err || pe == NULL) {
3387 free_ignorelist(ignorelist);
3393 match_ignores(struct got_pathlist_head *ignores, const char *path)
3395 struct got_pathlist_entry *pe;
3397 /* Handle patterns which match in all directories. */
3398 TAILQ_FOREACH(pe, ignores, entry) {
3399 struct got_pathlist_head *ignorelist = pe->data;
3400 struct got_pathlist_entry *pi;
3402 TAILQ_FOREACH(pi, ignorelist, entry) {
3403 const char *p, *pattern = pi->path;
3405 if (strncmp(pattern, "**/", 3) != 0)
3410 if (fnmatch(pattern, p,
3411 FNM_PATHNAME | FNM_LEADING_DIR)) {
3412 /* Retry in next directory. */
3413 while (*p && *p != '/')
3425 * The ignores pathlist contains ignore lists from children before
3426 * parents, so we can find the most specific ignorelist by walking
3427 * ignores backwards.
3429 pe = TAILQ_LAST(ignores, got_pathlist_head);
3431 if (got_path_is_child(path, pe->path, pe->path_len)) {
3432 struct got_pathlist_head *ignorelist = pe->data;
3433 struct got_pathlist_entry *pi;
3434 TAILQ_FOREACH(pi, ignorelist, entry) {
3435 const char *pattern = pi->path;
3436 int flags = FNM_LEADING_DIR;
3437 if (strstr(pattern, "/**/") == NULL)
3438 flags |= FNM_PATHNAME;
3439 if (fnmatch(pattern, path, flags))
3444 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3450 static const struct got_error *
3451 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3452 const char *path, int dirfd, const char *ignores_filename)
3454 const struct got_error *err = NULL;
3457 FILE *ignoresfile = NULL;
3459 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3460 path[0] ? "/" : "", ignores_filename) == -1)
3461 return got_error_from_errno("asprintf");
3464 fd = openat(dirfd, ignores_filename,
3465 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3467 if (errno != ENOENT && errno != EACCES)
3468 err = got_error_from_errno2("openat",
3471 ignoresfile = fdopen(fd, "r");
3472 if (ignoresfile == NULL)
3473 err = got_error_from_errno2("fdopen",
3477 err = read_ignores(ignores, path, ignoresfile);
3481 ignoresfile = fopen(ignorespath, "re");
3482 if (ignoresfile == NULL) {
3483 if (errno != ENOENT && errno != EACCES)
3484 err = got_error_from_errno2("fopen",
3487 err = read_ignores(ignores, path, ignoresfile);
3490 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3491 err = got_error_from_errno2("fclose", path);
3492 if (fd != -1 && close(fd) == -1 && err == NULL)
3493 err = got_error_from_errno2("close", path);
3498 static const struct got_error *
3499 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3502 const struct got_error *err = NULL;
3503 struct diff_dir_cb_arg *a = arg;
3509 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3510 return got_error(GOT_ERR_CANCELLED);
3512 if (parent_path[0]) {
3513 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3514 return got_error_from_errno("asprintf");
3519 if (de->d_type == DT_DIR) {
3520 if (!a->no_ignores && ignore != NULL &&
3521 match_ignores(a->ignores, path))
3523 } else if (!match_ignores(a->ignores, path) &&
3524 got_path_is_child(path, a->status_path, a->status_path_len))
3525 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3526 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3532 static const struct got_error *
3533 status_traverse(void *arg, const char *path, int dirfd)
3535 const struct got_error *err = NULL;
3536 struct diff_dir_cb_arg *a = arg;
3541 err = add_ignores(a->ignores, a->worktree->root_path,
3542 path, dirfd, ".cvsignore");
3546 err = add_ignores(a->ignores, a->worktree->root_path, path,
3547 dirfd, ".gitignore");
3552 static const struct got_error *
3553 report_single_file_status(const char *path, const char *ondisk_path,
3554 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3555 void *status_arg, struct got_repository *repo, int report_unchanged,
3556 struct got_pathlist_head *ignores, int no_ignores)
3558 struct got_fileindex_entry *ie;
3561 if (!no_ignores && match_ignores(ignores, path))
3564 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3566 return report_file_status(ie, ondisk_path, -1, NULL,
3567 status_cb, status_arg, repo, report_unchanged);
3569 if (lstat(ondisk_path, &sb) == -1) {
3570 if (errno != ENOENT)
3571 return got_error_from_errno2("lstat", ondisk_path);
3572 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3573 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3576 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3577 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3578 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3583 static const struct got_error *
3584 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3585 const char *root_path, const char *path)
3587 const struct got_error *err;
3588 char *parent_path, *next_parent_path = NULL;
3590 err = add_ignores(ignores, root_path, "", -1,
3595 err = add_ignores(ignores, root_path, "", -1,
3600 err = got_path_dirname(&parent_path, path);
3602 if (err->code == GOT_ERR_BAD_PATH)
3603 return NULL; /* cannot traverse parent */
3607 err = add_ignores(ignores, root_path, parent_path, -1,
3611 err = add_ignores(ignores, root_path, parent_path, -1,
3615 err = got_path_dirname(&next_parent_path, parent_path);
3617 if (err->code == GOT_ERR_BAD_PATH)
3618 err = NULL; /* traversed everything */
3621 if (got_path_is_root_dir(parent_path))
3624 parent_path = next_parent_path;
3625 next_parent_path = NULL;
3629 free(next_parent_path);
3633 static const struct got_error *
3634 worktree_status(struct got_worktree *worktree, const char *path,
3635 struct got_fileindex *fileindex, struct got_repository *repo,
3636 got_worktree_status_cb status_cb, void *status_arg,
3637 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3638 int report_unchanged)
3640 const struct got_error *err = NULL;
3642 struct got_fileindex_diff_dir_cb fdiff_cb;
3643 struct diff_dir_cb_arg arg;
3644 char *ondisk_path = NULL;
3645 struct got_pathlist_head ignores;
3647 TAILQ_INIT(&ignores);
3649 if (asprintf(&ondisk_path, "%s%s%s",
3650 worktree->root_path, path[0] ? "/" : "", path) == -1)
3651 return got_error_from_errno("asprintf");
3653 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3655 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3656 !got_err_open_nofollow_on_symlink())
3657 err = got_error_from_errno2("open", ondisk_path);
3660 err = add_ignores_from_parent_paths(&ignores,
3661 worktree->root_path, ondisk_path);
3665 err = report_single_file_status(path, ondisk_path,
3666 fileindex, status_cb, status_arg, repo,
3667 report_unchanged, &ignores, no_ignores);
3670 fdiff_cb.diff_old_new = status_old_new;
3671 fdiff_cb.diff_old = status_old;
3672 fdiff_cb.diff_new = status_new;
3673 fdiff_cb.diff_traverse = status_traverse;
3674 arg.fileindex = fileindex;
3675 arg.worktree = worktree;
3676 arg.status_path = path;
3677 arg.status_path_len = strlen(path);
3679 arg.status_cb = status_cb;
3680 arg.status_arg = status_arg;
3681 arg.cancel_cb = cancel_cb;
3682 arg.cancel_arg = cancel_arg;
3683 arg.report_unchanged = report_unchanged;
3684 arg.no_ignores = no_ignores;
3686 err = add_ignores_from_parent_paths(&ignores,
3687 worktree->root_path, path);
3691 arg.ignores = &ignores;
3692 err = got_fileindex_diff_dir(fileindex, fd,
3693 worktree->root_path, path, repo, &fdiff_cb, &arg);
3696 free_ignores(&ignores);
3697 if (fd != -1 && close(fd) == -1 && err == NULL)
3698 err = got_error_from_errno("close");
3703 const struct got_error *
3704 got_worktree_status(struct got_worktree *worktree,
3705 struct got_pathlist_head *paths, struct got_repository *repo,
3706 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3707 got_cancel_cb cancel_cb, void *cancel_arg)
3709 const struct got_error *err = NULL;
3710 char *fileindex_path = NULL;
3711 struct got_fileindex *fileindex = NULL;
3712 struct got_pathlist_entry *pe;
3714 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3718 TAILQ_FOREACH(pe, paths, entry) {
3719 err = worktree_status(worktree, pe->path, fileindex, repo,
3720 status_cb, status_arg, cancel_cb, cancel_arg,
3725 free(fileindex_path);
3726 got_fileindex_free(fileindex);
3730 const struct got_error *
3731 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3734 const struct got_error *err = NULL;
3735 char *resolved = NULL, *cwd = NULL, *path = NULL;
3738 char *abspath = NULL;
3739 char canonpath[PATH_MAX];
3743 cwd = getcwd(NULL, 0);
3745 return got_error_from_errno("getcwd");
3747 if (lstat(arg, &sb) == -1) {
3748 if (errno != ENOENT) {
3749 err = got_error_from_errno2("lstat", arg);
3754 if (S_ISLNK(sb.st_mode)) {
3756 * We cannot use realpath(3) with symlinks since we want to
3757 * operate on the symlink itself.
3758 * But we can make the path absolute, assuming it is relative
3759 * to the current working directory, and then canonicalize it.
3761 if (!got_path_is_absolute(arg)) {
3762 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3763 err = got_error_from_errno("asprintf");
3768 err = got_canonpath(abspath ? abspath : arg, canonpath,
3772 resolved = strdup(canonpath);
3773 if (resolved == NULL) {
3774 err = got_error_from_errno("strdup");
3778 resolved = realpath(arg, NULL);
3779 if (resolved == NULL) {
3780 if (errno != ENOENT) {
3781 err = got_error_from_errno2("realpath", arg);
3784 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3785 err = got_error_from_errno("asprintf");
3788 err = got_canonpath(abspath, canonpath,
3792 resolved = strdup(canonpath);
3793 if (resolved == NULL) {
3794 err = got_error_from_errno("strdup");
3800 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3801 strlen(got_worktree_get_root_path(worktree)))) {
3802 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3806 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3807 err = got_path_skip_common_ancestor(&path,
3808 got_worktree_get_root_path(worktree), resolved);
3814 err = got_error_from_errno("strdup");
3819 /* XXX status walk can't deal with trailing slash! */
3821 while (len > 0 && path[len - 1] == '/') {
3822 path[len - 1] = '\0';
3836 struct schedule_addition_args {
3837 struct got_worktree *worktree;
3838 struct got_fileindex *fileindex;
3839 got_worktree_checkout_cb progress_cb;
3841 struct got_repository *repo;
3844 static const struct got_error *
3845 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3846 const char *relpath, struct got_object_id *blob_id,
3847 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3848 int dirfd, const char *de_name)
3850 struct schedule_addition_args *a = arg;
3851 const struct got_error *err = NULL;
3852 struct got_fileindex_entry *ie;
3856 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3858 return got_error_from_errno("asprintf");
3860 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3862 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3866 /* Re-adding an existing entry is a no-op. */
3867 if (status == GOT_STATUS_ADD)
3869 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3874 if (status != GOT_STATUS_UNVERSIONED) {
3875 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3879 err = got_fileindex_entry_alloc(&ie, relpath);
3882 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3883 relpath, NULL, NULL, 1);
3885 got_fileindex_entry_free(ie);
3888 err = got_fileindex_entry_add(a->fileindex, ie);
3890 got_fileindex_entry_free(ie);
3897 if (status == GOT_STATUS_ADD)
3899 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3902 const struct got_error *
3903 got_worktree_schedule_add(struct got_worktree *worktree,
3904 struct got_pathlist_head *paths,
3905 got_worktree_checkout_cb progress_cb, void *progress_arg,
3906 struct got_repository *repo, int no_ignores)
3908 struct got_fileindex *fileindex = NULL;
3909 char *fileindex_path = NULL;
3910 const struct got_error *err = NULL, *sync_err, *unlockerr;
3911 struct got_pathlist_entry *pe;
3912 struct schedule_addition_args saa;
3914 err = lock_worktree(worktree, LOCK_EX);
3918 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3922 saa.worktree = worktree;
3923 saa.fileindex = fileindex;
3924 saa.progress_cb = progress_cb;
3925 saa.progress_arg = progress_arg;
3928 TAILQ_FOREACH(pe, paths, entry) {
3929 err = worktree_status(worktree, pe->path, fileindex, repo,
3930 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3934 sync_err = sync_fileindex(fileindex, fileindex_path);
3935 if (sync_err && err == NULL)
3938 free(fileindex_path);
3940 got_fileindex_free(fileindex);
3941 unlockerr = lock_worktree(worktree, LOCK_SH);
3942 if (unlockerr && err == NULL)
3947 struct schedule_deletion_args {
3948 struct got_worktree *worktree;
3949 struct got_fileindex *fileindex;
3950 got_worktree_delete_cb progress_cb;
3952 struct got_repository *repo;
3953 int delete_local_mods;
3955 int ignore_missing_paths;
3956 const char *status_codes;
3959 static const struct got_error *
3960 schedule_for_deletion(void *arg, unsigned char status,
3961 unsigned char staged_status, const char *relpath,
3962 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3963 struct got_object_id *commit_id, int dirfd, const char *de_name)
3965 struct schedule_deletion_args *a = arg;
3966 const struct got_error *err = NULL;
3967 struct got_fileindex_entry *ie = NULL;
3971 if (status == GOT_STATUS_NONEXISTENT) {
3972 if (a->ignore_missing_paths)
3974 return got_error_set_errno(ENOENT, relpath);
3977 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3979 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
3981 staged_status = get_staged_status(ie);
3982 if (staged_status != GOT_STATUS_NO_CHANGE) {
3983 if (staged_status == GOT_STATUS_DELETE)
3985 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3988 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3990 return got_error_from_errno("asprintf");
3992 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3997 if (a->status_codes) {
3998 size_t ncodes = strlen(a->status_codes);
4000 for (i = 0; i < ncodes ; i++) {
4001 if (status == a->status_codes[i])
4005 /* Do not delete files in non-matching status. */
4009 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4010 a->status_codes[i] != GOT_STATUS_MISSING) {
4011 static char msg[64];
4012 snprintf(msg, sizeof(msg),
4013 "invalid status code '%c'", a->status_codes[i]);
4014 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4019 if (status != GOT_STATUS_NO_CHANGE) {
4020 if (status == GOT_STATUS_DELETE)
4022 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4023 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4026 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4027 err = got_error_set_errno(ENOENT, relpath);
4030 if (status != GOT_STATUS_MODIFY &&
4031 status != GOT_STATUS_MISSING) {
4032 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4037 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4041 if (unlinkat(dirfd, de_name, 0) != 0) {
4042 err = got_error_from_errno2("unlinkat",
4046 } else if (unlink(ondisk_path) != 0) {
4047 err = got_error_from_errno2("unlink", ondisk_path);
4051 root_len = strlen(a->worktree->root_path);
4054 err = got_path_dirname(&parent, ondisk_path);
4058 ondisk_path = parent;
4059 if (rmdir(ondisk_path) == -1) {
4060 if (errno != ENOTEMPTY)
4061 err = got_error_from_errno2("rmdir",
4065 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4066 strlen(ondisk_path), root_len) != 0);
4069 got_fileindex_entry_mark_deleted_from_disk(ie);
4074 if (status == GOT_STATUS_DELETE)
4076 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4077 staged_status, relpath);
4080 const struct got_error *
4081 got_worktree_schedule_delete(struct got_worktree *worktree,
4082 struct got_pathlist_head *paths, int delete_local_mods,
4083 const char *status_codes,
4084 got_worktree_delete_cb progress_cb, void *progress_arg,
4085 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4087 struct got_fileindex *fileindex = NULL;
4088 char *fileindex_path = NULL;
4089 const struct got_error *err = NULL, *sync_err, *unlockerr;
4090 struct got_pathlist_entry *pe;
4091 struct schedule_deletion_args sda;
4093 err = lock_worktree(worktree, LOCK_EX);
4097 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4101 sda.worktree = worktree;
4102 sda.fileindex = fileindex;
4103 sda.progress_cb = progress_cb;
4104 sda.progress_arg = progress_arg;
4106 sda.delete_local_mods = delete_local_mods;
4107 sda.keep_on_disk = keep_on_disk;
4108 sda.ignore_missing_paths = ignore_missing_paths;
4109 sda.status_codes = status_codes;
4111 TAILQ_FOREACH(pe, paths, entry) {
4112 err = worktree_status(worktree, pe->path, fileindex, repo,
4113 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4117 sync_err = sync_fileindex(fileindex, fileindex_path);
4118 if (sync_err && err == NULL)
4121 free(fileindex_path);
4123 got_fileindex_free(fileindex);
4124 unlockerr = lock_worktree(worktree, LOCK_SH);
4125 if (unlockerr && err == NULL)
4130 static const struct got_error *
4131 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4133 const struct got_error *err = NULL;
4135 size_t linesize = 0, n;
4138 linelen = getline(&line, &linesize, infile);
4139 if (linelen == -1) {
4140 if (ferror(infile)) {
4141 err = got_error_from_errno("getline");
4147 n = fwrite(line, 1, linelen, outfile);
4149 err = got_ferror(outfile, GOT_ERR_IO);
4154 n = fwrite(line, 1, linelen, rejectfile);
4156 err = got_ferror(outfile, GOT_ERR_IO);
4163 static const struct got_error *
4164 skip_one_line(FILE *f)
4167 size_t linesize = 0;
4170 linelen = getline(&line, &linesize, f);
4171 if (linelen == -1) {
4173 return got_error_from_errno("getline");
4180 static const struct got_error *
4181 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4182 int start_old, int end_old, int start_new, int end_new,
4183 FILE *outfile, FILE *rejectfile)
4185 const struct got_error *err;
4187 /* Copy old file's lines leading up to patch. */
4188 while (!feof(f1) && *line_cur1 < start_old) {
4189 err = copy_one_line(f1, outfile, NULL);
4194 /* Skip new file's lines leading up to patch. */
4195 while (!feof(f2) && *line_cur2 < start_new) {
4197 err = copy_one_line(f2, NULL, rejectfile);
4199 err = skip_one_line(f2);
4204 /* Copy patched lines. */
4205 while (!feof(f2) && *line_cur2 <= end_new) {
4206 err = copy_one_line(f2, outfile, NULL);
4211 /* Skip over old file's replaced lines. */
4212 while (!feof(f1) && *line_cur1 <= end_old) {
4214 err = copy_one_line(f1, NULL, rejectfile);
4216 err = skip_one_line(f1);
4225 static const struct got_error *
4226 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4227 FILE *outfile, FILE *rejectfile)
4229 const struct got_error *err;
4232 /* Copy old file's lines until EOF. */
4234 err = copy_one_line(f1, outfile, NULL);
4241 /* Copy new file's lines until EOF. */
4243 err = copy_one_line(f2, NULL, rejectfile);
4253 static const struct got_error *
4254 apply_or_reject_change(int *choice, int *nchunks_used,
4255 struct diff_result *diff_result, int n,
4256 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4257 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4258 got_worktree_patch_cb patch_cb, void *patch_arg)
4260 const struct got_error *err = NULL;
4261 struct diff_chunk_context cc = {};
4262 int start_old, end_old, start_new, end_new;
4264 struct diff_output_unidiff_state *diff_state;
4265 struct diff_input_info diff_info;
4268 *choice = GOT_PATCH_CHOICE_NONE;
4270 /* Get changed line numbers without context lines for copy_change(). */
4271 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4272 start_old = cc.left.start;
4273 end_old = cc.left.end;
4274 start_new = cc.right.start;
4275 end_new = cc.right.end;
4277 /* Get the same change with context lines for display. */
4278 memset(&cc, 0, sizeof(cc));
4279 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4281 memset(&diff_info, 0, sizeof(diff_info));
4282 diff_info.left_path = relpath;
4283 diff_info.right_path = relpath;
4285 diff_state = diff_output_unidiff_state_alloc();
4286 if (diff_state == NULL)
4287 return got_error_set_errno(ENOMEM,
4288 "diff_output_unidiff_state_alloc");
4290 hunkfile = got_opentemp();
4291 if (hunkfile == NULL) {
4292 err = got_error_from_errno("got_opentemp");
4296 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4298 if (rc != DIFF_RC_OK) {
4299 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4303 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4304 err = got_ferror(hunkfile, GOT_ERR_IO);
4308 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4309 hunkfile, changeno, nchanges);
4314 case GOT_PATCH_CHOICE_YES:
4315 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4316 end_old, start_new, end_new, outfile, rejectfile);
4318 case GOT_PATCH_CHOICE_NO:
4319 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4320 end_old, start_new, end_new, rejectfile, outfile);
4322 case GOT_PATCH_CHOICE_QUIT:
4325 err = got_error(GOT_ERR_PATCH_CHOICE);
4329 diff_output_unidiff_state_free(diff_state);
4330 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4331 err = got_error_from_errno("fclose");
4335 struct revert_file_args {
4336 struct got_worktree *worktree;
4337 struct got_fileindex *fileindex;
4338 got_worktree_checkout_cb progress_cb;
4340 got_worktree_patch_cb patch_cb;
4342 struct got_repository *repo;
4343 int unlink_added_files;
4346 static const struct got_error *
4347 create_patched_content(char **path_outfile, int reverse_patch,
4348 struct got_object_id *blob_id, const char *path2,
4349 int dirfd2, const char *de_name2,
4350 const char *relpath, struct got_repository *repo,
4351 got_worktree_patch_cb patch_cb, void *patch_arg)
4353 const struct got_error *err, *free_err;
4354 struct got_blob_object *blob = NULL;
4355 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4357 char link_target[PATH_MAX];
4358 ssize_t link_len = 0;
4359 char *path1 = NULL, *id_str = NULL;
4361 struct got_diffreg_result *diffreg_result = NULL;
4362 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4363 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4365 *path_outfile = NULL;
4367 err = got_object_id_str(&id_str, blob_id);
4372 fd2 = openat(dirfd2, de_name2,
4373 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4375 if (!got_err_open_nofollow_on_symlink()) {
4376 err = got_error_from_errno2("openat", path2);
4379 link_len = readlinkat(dirfd2, de_name2,
4380 link_target, sizeof(link_target));
4381 if (link_len == -1) {
4382 return got_error_from_errno2("readlinkat",
4385 sb2.st_mode = S_IFLNK;
4386 sb2.st_size = link_len;
4389 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4391 if (!got_err_open_nofollow_on_symlink()) {
4392 err = got_error_from_errno2("open", path2);
4395 link_len = readlink(path2, link_target,
4396 sizeof(link_target));
4398 return got_error_from_errno2("readlink", path2);
4399 sb2.st_mode = S_IFLNK;
4400 sb2.st_size = link_len;
4404 if (fstat(fd2, &sb2) == -1) {
4405 err = got_error_from_errno2("fstat", path2);
4409 f2 = fdopen(fd2, "r");
4411 err = got_error_from_errno2("fdopen", path2);
4417 f2 = got_opentemp();
4419 err = got_error_from_errno2("got_opentemp", path2);
4422 n = fwrite(link_target, 1, link_len, f2);
4423 if (n != link_len) {
4424 err = got_ferror(f2, GOT_ERR_IO);
4427 if (fflush(f2) == EOF) {
4428 err = got_error_from_errno("fflush");
4434 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4438 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4442 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4446 err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4451 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4455 if (fseek(f1, 0L, SEEK_SET) == -1)
4456 return got_ferror(f1, GOT_ERR_IO);
4457 if (fseek(f2, 0L, SEEK_SET) == -1)
4458 return got_ferror(f2, GOT_ERR_IO);
4460 /* Count the number of actual changes in the diff result. */
4461 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4462 struct diff_chunk_context cc = {};
4463 diff_chunk_context_load_change(&cc, &nchunks_used,
4464 diffreg_result->result, n, 0);
4467 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4469 err = apply_or_reject_change(&choice, &nchunks_used,
4470 diffreg_result->result, n, relpath, f1, f2,
4471 &line_cur1, &line_cur2,
4472 reverse_patch ? NULL : outfile,
4473 reverse_patch ? outfile : NULL,
4474 ++i, nchanges, patch_cb, patch_arg);
4477 if (choice == GOT_PATCH_CHOICE_YES)
4479 else if (choice == GOT_PATCH_CHOICE_QUIT)
4483 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4484 reverse_patch ? NULL : outfile,
4485 reverse_patch ? outfile : NULL);
4489 if (!S_ISLNK(sb2.st_mode)) {
4490 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4491 err = got_error_from_errno2("fchmod", path2);
4499 got_object_blob_close(blob);
4500 free_err = got_diffreg_result_free(diffreg_result);
4503 if (f1 && fclose(f1) == EOF && err == NULL)
4504 err = got_error_from_errno2("fclose", path1);
4505 if (f2 && fclose(f2) == EOF && err == NULL)
4506 err = got_error_from_errno2("fclose", path2);
4507 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4508 err = got_error_from_errno2("close", path2);
4509 if (outfile && fclose(outfile) == EOF && err == NULL)
4510 err = got_error_from_errno2("fclose", *path_outfile);
4511 if (path1 && unlink(path1) == -1 && err == NULL)
4512 err = got_error_from_errno2("unlink", path1);
4513 if (err || !have_content) {
4514 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4515 err = got_error_from_errno2("unlink", *path_outfile);
4516 free(*path_outfile);
4517 *path_outfile = NULL;
4523 static const struct got_error *
4524 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4525 const char *relpath, struct got_object_id *blob_id,
4526 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4527 int dirfd, const char *de_name)
4529 struct revert_file_args *a = arg;
4530 const struct got_error *err = NULL;
4531 char *parent_path = NULL;
4532 struct got_fileindex_entry *ie;
4533 struct got_tree_object *tree = NULL;
4534 struct got_object_id *tree_id = NULL;
4535 const struct got_tree_entry *te = NULL;
4536 char *tree_path = NULL, *te_name;
4537 char *ondisk_path = NULL, *path_content = NULL;
4538 struct got_blob_object *blob = NULL;
4540 /* Reverting a staged deletion is a no-op. */
4541 if (status == GOT_STATUS_DELETE &&
4542 staged_status != GOT_STATUS_NO_CHANGE)
4545 if (status == GOT_STATUS_UNVERSIONED)
4546 return (*a->progress_cb)(a->progress_arg,
4547 GOT_STATUS_UNVERSIONED, relpath);
4549 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4551 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4553 /* Construct in-repository path of tree which contains this blob. */
4554 err = got_path_dirname(&parent_path, ie->path);
4556 if (err->code != GOT_ERR_BAD_PATH)
4558 parent_path = strdup("/");
4559 if (parent_path == NULL) {
4560 err = got_error_from_errno("strdup");
4564 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4565 tree_path = strdup(parent_path);
4566 if (tree_path == NULL) {
4567 err = got_error_from_errno("strdup");
4571 if (got_path_is_root_dir(parent_path)) {
4572 tree_path = strdup(a->worktree->path_prefix);
4573 if (tree_path == NULL) {
4574 err = got_error_from_errno("strdup");
4578 if (asprintf(&tree_path, "%s/%s",
4579 a->worktree->path_prefix, parent_path) == -1) {
4580 err = got_error_from_errno("asprintf");
4586 err = got_object_id_by_path(&tree_id, a->repo,
4587 a->worktree->base_commit_id, tree_path);
4589 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4590 (status == GOT_STATUS_ADD ||
4591 staged_status == GOT_STATUS_ADD)))
4594 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4598 err = got_path_basename(&te_name, ie->path);
4602 te = got_object_tree_find_entry(tree, te_name);
4604 if (te == NULL && status != GOT_STATUS_ADD &&
4605 staged_status != GOT_STATUS_ADD) {
4606 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4612 case GOT_STATUS_ADD:
4614 int choice = GOT_PATCH_CHOICE_NONE;
4615 err = (*a->patch_cb)(&choice, a->patch_arg,
4616 status, ie->path, NULL, 1, 1);
4619 if (choice != GOT_PATCH_CHOICE_YES)
4622 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4626 got_fileindex_entry_remove(a->fileindex, ie);
4627 if (a->unlink_added_files) {
4628 if (asprintf(&ondisk_path, "%s/%s",
4629 got_worktree_get_root_path(a->worktree),
4631 err = got_error_from_errno("asprintf");
4634 if (unlink(ondisk_path) == -1) {
4635 err = got_error_from_errno2("unlink",
4641 case GOT_STATUS_DELETE:
4643 int choice = GOT_PATCH_CHOICE_NONE;
4644 err = (*a->patch_cb)(&choice, a->patch_arg,
4645 status, ie->path, NULL, 1, 1);
4648 if (choice != GOT_PATCH_CHOICE_YES)
4652 case GOT_STATUS_MODIFY:
4653 case GOT_STATUS_MODE_CHANGE:
4654 case GOT_STATUS_CONFLICT:
4655 case GOT_STATUS_MISSING: {
4656 struct got_object_id id;
4657 if (staged_status == GOT_STATUS_ADD ||
4658 staged_status == GOT_STATUS_MODIFY) {
4659 memcpy(id.sha1, ie->staged_blob_sha1,
4660 SHA1_DIGEST_LENGTH);
4662 memcpy(id.sha1, ie->blob_sha1,
4663 SHA1_DIGEST_LENGTH);
4664 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4668 if (asprintf(&ondisk_path, "%s/%s",
4669 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4670 err = got_error_from_errno("asprintf");
4674 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4675 status == GOT_STATUS_CONFLICT)) {
4676 int is_bad_symlink = 0;
4677 err = create_patched_content(&path_content, 1, &id,
4678 ondisk_path, dirfd, de_name, ie->path, a->repo,
4679 a->patch_cb, a->patch_arg);
4680 if (err || path_content == NULL)
4682 if (te && S_ISLNK(te->mode)) {
4683 if (unlink(path_content) == -1) {
4684 err = got_error_from_errno2("unlink",
4688 err = install_symlink(&is_bad_symlink,
4689 a->worktree, ondisk_path, ie->path,
4690 blob, 0, 1, 0, 0, a->repo,
4691 a->progress_cb, a->progress_arg);
4693 if (rename(path_content, ondisk_path) == -1) {
4694 err = got_error_from_errno3("rename",
4695 path_content, ondisk_path);
4700 int is_bad_symlink = 0;
4701 if (te && S_ISLNK(te->mode)) {
4702 err = install_symlink(&is_bad_symlink,
4703 a->worktree, ondisk_path, ie->path,
4704 blob, 0, 1, 0, 0, a->repo,
4705 a->progress_cb, a->progress_arg);
4707 err = install_blob(a->worktree, ondisk_path,
4709 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4710 got_fileindex_perms_to_st(ie), blob,
4711 0, 1, 0, 0, a->repo,
4712 a->progress_cb, a->progress_arg);
4716 if (status == GOT_STATUS_DELETE ||
4717 status == GOT_STATUS_MODE_CHANGE) {
4718 err = got_fileindex_entry_update(ie,
4719 a->worktree->root_fd, relpath,
4721 a->worktree->base_commit_id->sha1, 1);
4725 if (is_bad_symlink) {
4726 got_fileindex_entry_filetype_set(ie,
4727 GOT_FILEIDX_MODE_BAD_SYMLINK);
4741 got_object_blob_close(blob);
4743 got_object_tree_close(tree);
4748 const struct got_error *
4749 got_worktree_revert(struct got_worktree *worktree,
4750 struct got_pathlist_head *paths,
4751 got_worktree_checkout_cb progress_cb, void *progress_arg,
4752 got_worktree_patch_cb patch_cb, void *patch_arg,
4753 struct got_repository *repo)
4755 struct got_fileindex *fileindex = NULL;
4756 char *fileindex_path = NULL;
4757 const struct got_error *err = NULL, *unlockerr = NULL;
4758 const struct got_error *sync_err = NULL;
4759 struct got_pathlist_entry *pe;
4760 struct revert_file_args rfa;
4762 err = lock_worktree(worktree, LOCK_EX);
4766 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4770 rfa.worktree = worktree;
4771 rfa.fileindex = fileindex;
4772 rfa.progress_cb = progress_cb;
4773 rfa.progress_arg = progress_arg;
4774 rfa.patch_cb = patch_cb;
4775 rfa.patch_arg = patch_arg;
4777 rfa.unlink_added_files = 0;
4778 TAILQ_FOREACH(pe, paths, entry) {
4779 err = worktree_status(worktree, pe->path, fileindex, repo,
4780 revert_file, &rfa, NULL, NULL, 1, 0);
4784 sync_err = sync_fileindex(fileindex, fileindex_path);
4785 if (sync_err && err == NULL)
4788 free(fileindex_path);
4790 got_fileindex_free(fileindex);
4791 unlockerr = lock_worktree(worktree, LOCK_SH);
4792 if (unlockerr && err == NULL)
4798 free_commitable(struct got_commitable *ct)
4801 free(ct->in_repo_path);
4802 free(ct->ondisk_path);
4804 free(ct->base_blob_id);
4805 free(ct->staged_blob_id);
4806 free(ct->base_commit_id);
4810 struct collect_commitables_arg {
4811 struct got_pathlist_head *commitable_paths;
4812 struct got_repository *repo;
4813 struct got_worktree *worktree;
4814 struct got_fileindex *fileindex;
4815 int have_staged_files;
4816 int allow_bad_symlinks;
4819 static const struct got_error *
4820 collect_commitables(void *arg, unsigned char status,
4821 unsigned char staged_status, const char *relpath,
4822 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4823 struct got_object_id *commit_id, int dirfd, const char *de_name)
4825 struct collect_commitables_arg *a = arg;
4826 const struct got_error *err = NULL;
4827 struct got_commitable *ct = NULL;
4828 struct got_pathlist_entry *new = NULL;
4829 char *parent_path = NULL, *path = NULL;
4832 if (a->have_staged_files) {
4833 if (staged_status != GOT_STATUS_MODIFY &&
4834 staged_status != GOT_STATUS_ADD &&
4835 staged_status != GOT_STATUS_DELETE)
4838 if (status == GOT_STATUS_CONFLICT)
4839 return got_error(GOT_ERR_COMMIT_CONFLICT);
4841 if (status != GOT_STATUS_MODIFY &&
4842 status != GOT_STATUS_MODE_CHANGE &&
4843 status != GOT_STATUS_ADD &&
4844 status != GOT_STATUS_DELETE)
4848 if (asprintf(&path, "/%s", relpath) == -1) {
4849 err = got_error_from_errno("asprintf");
4852 if (strcmp(path, "/") == 0) {
4853 parent_path = strdup("");
4854 if (parent_path == NULL)
4855 return got_error_from_errno("strdup");
4857 err = got_path_dirname(&parent_path, path);
4862 ct = calloc(1, sizeof(*ct));
4864 err = got_error_from_errno("calloc");
4868 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4870 err = got_error_from_errno("asprintf");
4874 if (staged_status == GOT_STATUS_ADD ||
4875 staged_status == GOT_STATUS_MODIFY) {
4876 struct got_fileindex_entry *ie;
4877 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4878 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4879 case GOT_FILEIDX_MODE_REGULAR_FILE:
4880 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4883 case GOT_FILEIDX_MODE_SYMLINK:
4887 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4890 ct->mode |= got_fileindex_entry_perms_get(ie);
4891 } else if (status != GOT_STATUS_DELETE &&
4892 staged_status != GOT_STATUS_DELETE) {
4894 if (fstatat(dirfd, de_name, &sb,
4895 AT_SYMLINK_NOFOLLOW) == -1) {
4896 err = got_error_from_errno2("fstatat",
4900 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4901 err = got_error_from_errno2("lstat", ct->ondisk_path);
4904 ct->mode = sb.st_mode;
4907 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4908 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4910 err = got_error_from_errno("asprintf");
4914 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4915 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4917 char target_path[PATH_MAX];
4919 target_len = readlink(ct->ondisk_path, target_path,
4920 sizeof(target_path));
4921 if (target_len == -1) {
4922 err = got_error_from_errno2("readlink",
4926 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4927 target_len, ct->ondisk_path, a->worktree->root_path);
4930 if (is_bad_symlink) {
4931 err = got_error_path(ct->ondisk_path,
4932 GOT_ERR_BAD_SYMLINK);
4938 ct->status = status;
4939 ct->staged_status = staged_status;
4940 ct->blob_id = NULL; /* will be filled in when blob gets created */
4941 if (ct->status != GOT_STATUS_ADD &&
4942 ct->staged_status != GOT_STATUS_ADD) {
4943 ct->base_blob_id = got_object_id_dup(blob_id);
4944 if (ct->base_blob_id == NULL) {
4945 err = got_error_from_errno("got_object_id_dup");
4948 ct->base_commit_id = got_object_id_dup(commit_id);
4949 if (ct->base_commit_id == NULL) {
4950 err = got_error_from_errno("got_object_id_dup");
4954 if (ct->staged_status == GOT_STATUS_ADD ||
4955 ct->staged_status == GOT_STATUS_MODIFY) {
4956 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4957 if (ct->staged_blob_id == NULL) {
4958 err = got_error_from_errno("got_object_id_dup");
4962 ct->path = strdup(path);
4963 if (ct->path == NULL) {
4964 err = got_error_from_errno("strdup");
4967 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4969 if (ct && (err || new == NULL))
4970 free_commitable(ct);
4976 static const struct got_error *write_tree(struct got_object_id **, int *,
4977 struct got_tree_object *, const char *, struct got_pathlist_head *,
4978 got_worktree_status_cb status_cb, void *status_arg,
4979 struct got_repository *);
4981 static const struct got_error *
4982 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4983 struct got_tree_entry *te, const char *parent_path,
4984 struct got_pathlist_head *commitable_paths,
4985 got_worktree_status_cb status_cb, void *status_arg,
4986 struct got_repository *repo)
4988 const struct got_error *err = NULL;
4989 struct got_tree_object *subtree;
4992 if (asprintf(&subpath, "%s%s%s", parent_path,
4993 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4994 return got_error_from_errno("asprintf");
4996 err = got_object_open_as_tree(&subtree, repo, &te->id);
5000 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5001 commitable_paths, status_cb, status_arg, repo);
5002 got_object_tree_close(subtree);
5007 static const struct got_error *
5008 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5010 const struct got_error *err = NULL;
5011 char *ct_parent_path = NULL;
5015 if (strchr(ct->in_repo_path, '/') == NULL) {
5016 *match = got_path_is_root_dir(path);
5020 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5023 *match = (strcmp(path, ct_parent_path) == 0);
5024 free(ct_parent_path);
5029 get_ct_file_mode(struct got_commitable *ct)
5031 if (S_ISLNK(ct->mode))
5034 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5037 static const struct got_error *
5038 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5039 struct got_tree_entry *te, struct got_commitable *ct)
5041 const struct got_error *err = NULL;
5045 err = got_object_tree_entry_dup(new_te, te);
5049 (*new_te)->mode = get_ct_file_mode(ct);
5051 if (ct->staged_status == GOT_STATUS_MODIFY)
5052 memcpy(&(*new_te)->id, ct->staged_blob_id,
5053 sizeof((*new_te)->id));
5055 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5057 if (err && *new_te) {
5064 static const struct got_error *
5065 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5066 struct got_commitable *ct)
5068 const struct got_error *err = NULL;
5069 char *ct_name = NULL;
5073 *new_te = calloc(1, sizeof(**new_te));
5074 if (*new_te == NULL)
5075 return got_error_from_errno("calloc");
5077 err = got_path_basename(&ct_name, ct->path);
5080 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5081 sizeof((*new_te)->name)) {
5082 err = got_error(GOT_ERR_NO_SPACE);
5086 (*new_te)->mode = get_ct_file_mode(ct);
5088 if (ct->staged_status == GOT_STATUS_ADD)
5089 memcpy(&(*new_te)->id, ct->staged_blob_id,
5090 sizeof((*new_te)->id));
5092 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5095 if (err && *new_te) {
5102 static const struct got_error *
5103 insert_tree_entry(struct got_tree_entry *new_te,
5104 struct got_pathlist_head *paths)
5106 const struct got_error *err = NULL;
5107 struct got_pathlist_entry *new_pe;
5109 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5113 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5117 static const struct got_error *
5118 report_ct_status(struct got_commitable *ct,
5119 got_worktree_status_cb status_cb, void *status_arg)
5121 const char *ct_path = ct->path;
5122 unsigned char status;
5124 if (status_cb == NULL) /* no commit progress output desired */
5127 while (ct_path[0] == '/')
5130 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5131 status = ct->staged_status;
5133 status = ct->status;
5135 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5136 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5139 static const struct got_error *
5140 match_modified_subtree(int *modified, struct got_tree_entry *te,
5141 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5143 const struct got_error *err = NULL;
5144 struct got_pathlist_entry *pe;
5149 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5150 got_path_is_root_dir(base_tree_path) ? "" : "/",
5152 return got_error_from_errno("asprintf");
5154 TAILQ_FOREACH(pe, commitable_paths, entry) {
5155 struct got_commitable *ct = pe->data;
5156 *modified = got_path_is_child(ct->in_repo_path, te_path,
5166 static const struct got_error *
5167 match_deleted_or_modified_ct(struct got_commitable **ctp,
5168 struct got_tree_entry *te, const char *base_tree_path,
5169 struct got_pathlist_head *commitable_paths)
5171 const struct got_error *err = NULL;
5172 struct got_pathlist_entry *pe;
5176 TAILQ_FOREACH(pe, commitable_paths, entry) {
5177 struct got_commitable *ct = pe->data;
5178 char *ct_name = NULL;
5181 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5182 if (ct->status != GOT_STATUS_MODIFY &&
5183 ct->status != GOT_STATUS_MODE_CHANGE &&
5184 ct->status != GOT_STATUS_DELETE)
5187 if (ct->staged_status != GOT_STATUS_MODIFY &&
5188 ct->staged_status != GOT_STATUS_DELETE)
5192 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5195 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5201 err = got_path_basename(&ct_name, pe->path);
5205 if (strcmp(te->name, ct_name) != 0) {
5218 static const struct got_error *
5219 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5220 const char *child_path, const char *path_base_tree,
5221 struct got_pathlist_head *commitable_paths,
5222 got_worktree_status_cb status_cb, void *status_arg,
5223 struct got_repository *repo)
5225 const struct got_error *err = NULL;
5226 struct got_tree_entry *new_te;
5228 struct got_object_id *id = NULL;
5233 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5234 got_path_is_root_dir(path_base_tree) ? "" : "/",
5236 return got_error_from_errno("asprintf");
5238 new_te = calloc(1, sizeof(*new_te));
5240 return got_error_from_errno("calloc");
5241 new_te->mode = S_IFDIR;
5243 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5244 sizeof(new_te->name)) {
5245 err = got_error(GOT_ERR_NO_SPACE);
5248 err = write_tree(&id, &nentries, NULL, subtree_path,
5249 commitable_paths, status_cb, status_arg, repo);
5254 memcpy(&new_te->id, id, sizeof(new_te->id));
5263 static const struct got_error *
5264 write_tree(struct got_object_id **new_tree_id, int *nentries,
5265 struct got_tree_object *base_tree, const char *path_base_tree,
5266 struct got_pathlist_head *commitable_paths,
5267 got_worktree_status_cb status_cb, void *status_arg,
5268 struct got_repository *repo)
5270 const struct got_error *err = NULL;
5271 struct got_pathlist_head paths;
5272 struct got_tree_entry *te, *new_te = NULL;
5273 struct got_pathlist_entry *pe;
5278 /* Insert, and recurse into, newly added entries first. */
5279 TAILQ_FOREACH(pe, commitable_paths, entry) {
5280 struct got_commitable *ct = pe->data;
5281 char *child_path = NULL, *slash;
5283 if ((ct->status != GOT_STATUS_ADD &&
5284 ct->staged_status != GOT_STATUS_ADD) ||
5285 (ct->flags & GOT_COMMITABLE_ADDED))
5288 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5289 strlen(path_base_tree)))
5292 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5297 slash = strchr(child_path, '/');
5298 if (slash == NULL) {
5299 err = alloc_added_blob_tree_entry(&new_te, ct);
5302 err = report_ct_status(ct, status_cb, status_arg);
5305 ct->flags |= GOT_COMMITABLE_ADDED;
5306 err = insert_tree_entry(new_te, &paths);
5311 *slash = '\0'; /* trim trailing path components */
5312 if (base_tree == NULL ||
5313 got_object_tree_find_entry(base_tree, child_path)
5315 err = make_subtree_for_added_blob(&new_te,
5316 child_path, path_base_tree,
5317 commitable_paths, status_cb, status_arg,
5321 err = insert_tree_entry(new_te, &paths);
5330 int i, nbase_entries;
5331 /* Handle modified and deleted entries. */
5332 nbase_entries = got_object_tree_get_nentries(base_tree);
5333 for (i = 0; i < nbase_entries; i++) {
5334 struct got_commitable *ct = NULL;
5336 te = got_object_tree_get_entry(base_tree, i);
5337 if (got_object_tree_entry_is_submodule(te)) {
5338 /* Entry is a submodule; just copy it. */
5339 err = got_object_tree_entry_dup(&new_te, te);
5342 err = insert_tree_entry(new_te, &paths);
5349 if (S_ISDIR(te->mode)) {
5351 err = got_object_tree_entry_dup(&new_te, te);
5354 err = match_modified_subtree(&modified, te,
5355 path_base_tree, commitable_paths);
5358 /* Avoid recursion into unmodified subtrees. */
5360 struct got_object_id *new_id;
5362 err = write_subtree(&new_id,
5364 path_base_tree, commitable_paths,
5365 status_cb, status_arg, repo);
5368 if (nsubentries == 0) {
5369 /* All entries were deleted. */
5373 memcpy(&new_te->id, new_id,
5374 sizeof(new_te->id));
5377 err = insert_tree_entry(new_te, &paths);
5384 err = match_deleted_or_modified_ct(&ct, te,
5385 path_base_tree, commitable_paths);
5389 /* NB: Deleted entries get dropped here. */
5390 if (ct->status == GOT_STATUS_MODIFY ||
5391 ct->status == GOT_STATUS_MODE_CHANGE ||
5392 ct->staged_status == GOT_STATUS_MODIFY) {
5393 err = alloc_modified_blob_tree_entry(
5397 err = insert_tree_entry(new_te, &paths);
5402 err = report_ct_status(ct, status_cb,
5407 /* Entry is unchanged; just copy it. */
5408 err = got_object_tree_entry_dup(&new_te, te);
5411 err = insert_tree_entry(new_te, &paths);
5419 /* Write new list of entries; deleted entries have been dropped. */
5420 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5422 got_pathlist_free(&paths);
5426 static const struct got_error *
5427 update_fileindex_after_commit(struct got_worktree *worktree,
5428 struct got_pathlist_head *commitable_paths,
5429 struct got_object_id *new_base_commit_id,
5430 struct got_fileindex *fileindex, int have_staged_files)
5432 const struct got_error *err = NULL;
5433 struct got_pathlist_entry *pe;
5434 char *relpath = NULL;
5436 TAILQ_FOREACH(pe, commitable_paths, entry) {
5437 struct got_fileindex_entry *ie;
5438 struct got_commitable *ct = pe->data;
5440 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5442 err = got_path_skip_common_ancestor(&relpath,
5443 worktree->root_path, ct->ondisk_path);
5448 if (ct->status == GOT_STATUS_DELETE ||
5449 ct->staged_status == GOT_STATUS_DELETE) {
5450 got_fileindex_entry_remove(fileindex, ie);
5451 } else if (ct->staged_status == GOT_STATUS_ADD ||
5452 ct->staged_status == GOT_STATUS_MODIFY) {
5453 got_fileindex_entry_stage_set(ie,
5454 GOT_FILEIDX_STAGE_NONE);
5455 got_fileindex_entry_staged_filetype_set(ie, 0);
5457 err = got_fileindex_entry_update(ie,
5458 worktree->root_fd, relpath,
5459 ct->staged_blob_id->sha1,
5460 new_base_commit_id->sha1,
5461 !have_staged_files);
5463 err = got_fileindex_entry_update(ie,
5464 worktree->root_fd, relpath,
5466 new_base_commit_id->sha1,
5467 !have_staged_files);
5469 err = got_fileindex_entry_alloc(&ie, pe->path);
5472 err = got_fileindex_entry_update(ie,
5473 worktree->root_fd, relpath, ct->blob_id->sha1,
5474 new_base_commit_id->sha1, 1);
5476 got_fileindex_entry_free(ie);
5479 err = got_fileindex_entry_add(fileindex, ie);
5481 got_fileindex_entry_free(ie);
5494 static const struct got_error *
5495 check_out_of_date(const char *in_repo_path, unsigned char status,
5496 unsigned char staged_status, struct got_object_id *base_blob_id,
5497 struct got_object_id *base_commit_id,
5498 struct got_object_id *head_commit_id, struct got_repository *repo,
5501 const struct got_error *err = NULL;
5502 struct got_object_id *id = NULL;
5504 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5505 /* Trivial case: base commit == head commit */
5506 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5509 * Ensure file content which local changes were based
5510 * on matches file content in the branch head.
5512 err = got_object_id_by_path(&id, repo, head_commit_id,
5515 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5516 err = got_error(ood_errcode);
5518 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5519 err = got_error(ood_errcode);
5521 /* Require that added files don't exist in the branch head. */
5522 err = got_object_id_by_path(&id, repo, head_commit_id,
5524 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5526 err = id ? got_error(ood_errcode) : NULL;
5533 const struct got_error *
5534 commit_worktree(struct got_object_id **new_commit_id,
5535 struct got_pathlist_head *commitable_paths,
5536 struct got_object_id *head_commit_id,
5537 struct got_object_id *parent_id2,
5538 struct got_worktree *worktree,
5539 const char *author, const char *committer,
5540 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5541 got_worktree_status_cb status_cb, void *status_arg,
5542 struct got_repository *repo)
5544 const struct got_error *err = NULL, *unlockerr = NULL;
5545 struct got_pathlist_entry *pe;
5546 const char *head_ref_name = NULL;
5547 struct got_commit_object *head_commit = NULL;
5548 struct got_reference *head_ref2 = NULL;
5549 struct got_object_id *head_commit_id2 = NULL;
5550 struct got_tree_object *head_tree = NULL;
5551 struct got_object_id *new_tree_id = NULL;
5552 int nentries, nparents = 0;
5553 struct got_object_id_queue parent_ids;
5554 struct got_object_qid *pid = NULL;
5555 char *logmsg = NULL;
5557 *new_commit_id = NULL;
5559 STAILQ_INIT(&parent_ids);
5561 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5565 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5569 if (commit_msg_cb != NULL) {
5570 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5575 if (logmsg == NULL || strlen(logmsg) == 0) {
5576 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5580 /* Create blobs from added and modified files and record their IDs. */
5581 TAILQ_FOREACH(pe, commitable_paths, entry) {
5582 struct got_commitable *ct = pe->data;
5585 /* Blobs for staged files already exist. */
5586 if (ct->staged_status == GOT_STATUS_ADD ||
5587 ct->staged_status == GOT_STATUS_MODIFY)
5590 if (ct->status != GOT_STATUS_ADD &&
5591 ct->status != GOT_STATUS_MODIFY &&
5592 ct->status != GOT_STATUS_MODE_CHANGE)
5595 if (asprintf(&ondisk_path, "%s/%s",
5596 worktree->root_path, pe->path) == -1) {
5597 err = got_error_from_errno("asprintf");
5600 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5606 /* Recursively write new tree objects. */
5607 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5608 commitable_paths, status_cb, status_arg, repo);
5612 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5615 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5618 err = got_object_qid_alloc(&pid, parent_id2);
5621 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5624 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5625 nparents, author, time(NULL), committer, time(NULL), logmsg, repo);
5631 /* Check if a concurrent commit to our branch has occurred. */
5632 head_ref_name = got_worktree_get_head_ref_name(worktree);
5633 if (head_ref_name == NULL) {
5634 err = got_error_from_errno("got_worktree_get_head_ref_name");
5637 /* Lock the reference here to prevent concurrent modification. */
5638 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5641 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5644 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5645 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5648 /* Update branch head in repository. */
5649 err = got_ref_change_ref(head_ref2, *new_commit_id);
5652 err = got_ref_write(head_ref2, repo);
5656 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5660 err = ref_base_commit(worktree, repo);
5664 got_object_id_queue_free(&parent_ids);
5666 got_object_tree_close(head_tree);
5668 got_object_commit_close(head_commit);
5669 free(head_commit_id2);
5671 unlockerr = got_ref_unlock(head_ref2);
5672 if (unlockerr && err == NULL)
5674 got_ref_close(head_ref2);
5679 static const struct got_error *
5680 check_path_is_commitable(const char *path,
5681 struct got_pathlist_head *commitable_paths)
5683 struct got_pathlist_entry *cpe = NULL;
5684 size_t path_len = strlen(path);
5686 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5687 struct got_commitable *ct = cpe->data;
5688 const char *ct_path = ct->path;
5690 while (ct_path[0] == '/')
5693 if (strcmp(path, ct_path) == 0 ||
5694 got_path_is_child(ct_path, path, path_len))
5699 return got_error_path(path, GOT_ERR_BAD_PATH);
5704 static const struct got_error *
5705 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5707 int *have_staged_files = arg;
5709 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5710 *have_staged_files = 1;
5711 return got_error(GOT_ERR_CANCELLED);
5717 static const struct got_error *
5718 check_non_staged_files(struct got_fileindex *fileindex,
5719 struct got_pathlist_head *paths)
5721 struct got_pathlist_entry *pe;
5722 struct got_fileindex_entry *ie;
5724 TAILQ_FOREACH(pe, paths, entry) {
5725 if (pe->path[0] == '\0')
5727 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5729 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5730 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5731 return got_error_path(pe->path,
5732 GOT_ERR_FILE_NOT_STAGED);
5738 const struct got_error *
5739 got_worktree_commit(struct got_object_id **new_commit_id,
5740 struct got_worktree *worktree, struct got_pathlist_head *paths,
5741 const char *author, const char *committer, int allow_bad_symlinks,
5742 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5743 got_worktree_status_cb status_cb, void *status_arg,
5744 struct got_repository *repo)
5746 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5747 struct got_fileindex *fileindex = NULL;
5748 char *fileindex_path = NULL;
5749 struct got_pathlist_head commitable_paths;
5750 struct collect_commitables_arg cc_arg;
5751 struct got_pathlist_entry *pe;
5752 struct got_reference *head_ref = NULL;
5753 struct got_object_id *head_commit_id = NULL;
5754 int have_staged_files = 0;
5756 *new_commit_id = NULL;
5758 TAILQ_INIT(&commitable_paths);
5760 err = lock_worktree(worktree, LOCK_EX);
5764 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5768 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5772 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5776 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5777 &have_staged_files);
5778 if (err && err->code != GOT_ERR_CANCELLED)
5780 if (have_staged_files) {
5781 err = check_non_staged_files(fileindex, paths);
5786 cc_arg.commitable_paths = &commitable_paths;
5787 cc_arg.worktree = worktree;
5788 cc_arg.fileindex = fileindex;
5790 cc_arg.have_staged_files = have_staged_files;
5791 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5792 TAILQ_FOREACH(pe, paths, entry) {
5793 err = worktree_status(worktree, pe->path, fileindex, repo,
5794 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
5799 if (TAILQ_EMPTY(&commitable_paths)) {
5800 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5804 TAILQ_FOREACH(pe, paths, entry) {
5805 err = check_path_is_commitable(pe->path, &commitable_paths);
5810 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5811 struct got_commitable *ct = pe->data;
5812 const char *ct_path = ct->in_repo_path;
5814 while (ct_path[0] == '/')
5816 err = check_out_of_date(ct_path, ct->status,
5817 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5818 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5824 err = commit_worktree(new_commit_id, &commitable_paths,
5825 head_commit_id, NULL, worktree, author, committer,
5826 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5830 err = update_fileindex_after_commit(worktree, &commitable_paths,
5831 *new_commit_id, fileindex, have_staged_files);
5832 sync_err = sync_fileindex(fileindex, fileindex_path);
5833 if (sync_err && err == NULL)
5837 got_fileindex_free(fileindex);
5838 free(fileindex_path);
5839 unlockerr = lock_worktree(worktree, LOCK_SH);
5840 if (unlockerr && err == NULL)
5842 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5843 struct got_commitable *ct = pe->data;
5844 free_commitable(ct);
5846 got_pathlist_free(&commitable_paths);
5851 got_commitable_get_path(struct got_commitable *ct)
5857 got_commitable_get_status(struct got_commitable *ct)
5862 struct check_rebase_ok_arg {
5863 struct got_worktree *worktree;
5864 struct got_repository *repo;
5867 static const struct got_error *
5868 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5870 const struct got_error *err = NULL;
5871 struct check_rebase_ok_arg *a = arg;
5872 unsigned char status;
5876 /* Reject rebase of a work tree with mixed base commits. */
5877 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5878 SHA1_DIGEST_LENGTH))
5879 return got_error(GOT_ERR_MIXED_COMMITS);
5881 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5883 return got_error_from_errno("asprintf");
5885 /* Reject rebase of a work tree with modified or staged files. */
5886 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5891 if (status != GOT_STATUS_NO_CHANGE)
5892 return got_error(GOT_ERR_MODIFIED);
5893 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5894 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5899 const struct got_error *
5900 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5901 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5902 struct got_worktree *worktree, struct got_reference *branch,
5903 struct got_repository *repo)
5905 const struct got_error *err = NULL;
5906 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5907 char *branch_ref_name = NULL;
5908 char *fileindex_path = NULL;
5909 struct check_rebase_ok_arg ok_arg;
5910 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5911 struct got_object_id *wt_branch_tip = NULL;
5913 *new_base_branch_ref = NULL;
5917 err = lock_worktree(worktree, LOCK_EX);
5921 err = open_fileindex(fileindex, &fileindex_path, worktree);
5925 ok_arg.worktree = worktree;
5927 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5932 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5936 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5940 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5944 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5949 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5952 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5953 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5957 err = got_ref_alloc_symref(new_base_branch_ref,
5958 new_base_branch_ref_name, wt_branch);
5961 err = got_ref_write(*new_base_branch_ref, repo);
5965 /* TODO Lock original branch's ref while rebasing? */
5967 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5971 err = got_ref_write(branch_ref, repo);
5975 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5976 worktree->base_commit_id);
5979 err = got_ref_write(*tmp_branch, repo);
5983 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5987 free(fileindex_path);
5988 free(tmp_branch_name);
5989 free(new_base_branch_ref_name);
5990 free(branch_ref_name);
5992 got_ref_close(branch_ref);
5994 got_ref_close(wt_branch);
5995 free(wt_branch_tip);
5997 if (*new_base_branch_ref) {
5998 got_ref_close(*new_base_branch_ref);
5999 *new_base_branch_ref = NULL;
6002 got_ref_close(*tmp_branch);
6006 got_fileindex_free(*fileindex);
6009 lock_worktree(worktree, LOCK_SH);
6014 const struct got_error *
6015 got_worktree_rebase_continue(struct got_object_id **commit_id,
6016 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6017 struct got_reference **branch, struct got_fileindex **fileindex,
6018 struct got_worktree *worktree, struct got_repository *repo)
6020 const struct got_error *err;
6021 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6022 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6023 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6024 char *fileindex_path = NULL;
6025 int have_staged_files = 0;
6028 *new_base_branch = NULL;
6033 err = lock_worktree(worktree, LOCK_EX);
6037 err = open_fileindex(fileindex, &fileindex_path, worktree);
6041 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6042 &have_staged_files);
6043 if (err && err->code != GOT_ERR_CANCELLED)
6045 if (have_staged_files) {
6046 err = got_error(GOT_ERR_STAGED_PATHS);
6050 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6054 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6058 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6062 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6066 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6070 err = got_ref_open(branch, repo,
6071 got_ref_get_symref_target(branch_ref), 0);
6075 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6079 err = got_ref_resolve(commit_id, repo, commit_ref);
6083 err = got_ref_open(new_base_branch, repo,
6084 new_base_branch_ref_name, 0);
6088 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6092 free(commit_ref_name);
6093 free(branch_ref_name);
6094 free(fileindex_path);
6096 got_ref_close(commit_ref);
6098 got_ref_close(branch_ref);
6103 got_ref_close(*tmp_branch);
6106 if (*new_base_branch) {
6107 got_ref_close(*new_base_branch);
6108 *new_base_branch = NULL;
6111 got_ref_close(*branch);
6115 got_fileindex_free(*fileindex);
6118 lock_worktree(worktree, LOCK_SH);
6123 const struct got_error *
6124 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6126 const struct got_error *err;
6127 char *tmp_branch_name = NULL;
6129 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6133 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6134 free(tmp_branch_name);
6138 static const struct got_error *
6139 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6140 char **logmsg, void *arg)
6146 static const struct got_error *
6147 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6148 const char *path, struct got_object_id *blob_id,
6149 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6150 int dirfd, const char *de_name)
6155 struct collect_merged_paths_arg {
6156 got_worktree_checkout_cb progress_cb;
6158 struct got_pathlist_head *merged_paths;
6161 static const struct got_error *
6162 collect_merged_paths(void *arg, unsigned char status, const char *path)
6164 const struct got_error *err;
6165 struct collect_merged_paths_arg *a = arg;
6167 struct got_pathlist_entry *new;
6169 err = (*a->progress_cb)(a->progress_arg, status, path);
6173 if (status != GOT_STATUS_MERGE &&
6174 status != GOT_STATUS_ADD &&
6175 status != GOT_STATUS_DELETE &&
6176 status != GOT_STATUS_CONFLICT)
6181 return got_error_from_errno("strdup");
6183 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6184 if (err || new == NULL)
6190 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6192 struct got_pathlist_entry *pe;
6194 TAILQ_FOREACH(pe, merged_paths, entry)
6195 free((char *)pe->path);
6197 got_pathlist_free(merged_paths);
6200 static const struct got_error *
6201 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6202 int is_rebase, struct got_repository *repo)
6204 const struct got_error *err;
6205 struct got_reference *commit_ref = NULL;
6207 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6209 if (err->code != GOT_ERR_NOT_REF)
6211 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6214 err = got_ref_write(commit_ref, repo);
6217 } else if (is_rebase) {
6218 struct got_object_id *stored_id;
6221 err = got_ref_resolve(&stored_id, repo, commit_ref);
6224 cmp = got_object_id_cmp(commit_id, stored_id);
6227 err = got_error(GOT_ERR_REBASE_COMMITID);
6233 got_ref_close(commit_ref);
6237 static const struct got_error *
6238 rebase_merge_files(struct got_pathlist_head *merged_paths,
6239 const char *commit_ref_name, struct got_worktree *worktree,
6240 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6241 struct got_object_id *commit_id, struct got_repository *repo,
6242 got_worktree_checkout_cb progress_cb, void *progress_arg,
6243 got_cancel_cb cancel_cb, void *cancel_arg)
6245 const struct got_error *err;
6246 struct got_reference *commit_ref = NULL;
6247 struct collect_merged_paths_arg cmp_arg;
6248 char *fileindex_path;
6250 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6252 err = get_fileindex_path(&fileindex_path, worktree);
6256 cmp_arg.progress_cb = progress_cb;
6257 cmp_arg.progress_arg = progress_arg;
6258 cmp_arg.merged_paths = merged_paths;
6259 err = merge_files(worktree, fileindex, fileindex_path,
6260 parent_commit_id, commit_id, repo, collect_merged_paths,
6261 &cmp_arg, cancel_cb, cancel_arg);
6263 got_ref_close(commit_ref);
6267 const struct got_error *
6268 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6269 struct got_worktree *worktree, struct got_fileindex *fileindex,
6270 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6271 struct got_repository *repo,
6272 got_worktree_checkout_cb progress_cb, void *progress_arg,
6273 got_cancel_cb cancel_cb, void *cancel_arg)
6275 const struct got_error *err;
6276 char *commit_ref_name;
6278 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6282 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6286 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6287 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6288 progress_arg, cancel_cb, cancel_arg);
6290 free(commit_ref_name);
6294 const struct got_error *
6295 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6296 struct got_worktree *worktree, struct got_fileindex *fileindex,
6297 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6298 struct got_repository *repo,
6299 got_worktree_checkout_cb progress_cb, void *progress_arg,
6300 got_cancel_cb cancel_cb, void *cancel_arg)
6302 const struct got_error *err;
6303 char *commit_ref_name;
6305 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6309 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6313 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6314 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6315 progress_arg, cancel_cb, cancel_arg);
6317 free(commit_ref_name);
6321 static const struct got_error *
6322 rebase_commit(struct got_object_id **new_commit_id,
6323 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6324 struct got_worktree *worktree, struct got_fileindex *fileindex,
6325 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6326 const char *new_logmsg, struct got_repository *repo)
6328 const struct got_error *err, *sync_err;
6329 struct got_pathlist_head commitable_paths;
6330 struct collect_commitables_arg cc_arg;
6331 char *fileindex_path = NULL;
6332 struct got_reference *head_ref = NULL;
6333 struct got_object_id *head_commit_id = NULL;
6334 char *logmsg = NULL;
6336 TAILQ_INIT(&commitable_paths);
6337 *new_commit_id = NULL;
6339 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6341 err = get_fileindex_path(&fileindex_path, worktree);
6345 cc_arg.commitable_paths = &commitable_paths;
6346 cc_arg.worktree = worktree;
6348 cc_arg.have_staged_files = 0;
6350 * If possible get the status of individual files directly to
6351 * avoid crawling the entire work tree once per rebased commit.
6353 * Ideally, merged_paths would contain a list of commitables
6354 * we could use so we could skip worktree_status() entirely.
6355 * However, we would then need carefully keep track of cumulative
6356 * effects of operations such as file additions and deletions
6357 * in 'got histedit -f' (folding multiple commits into one),
6358 * and this extra complexity is not really worth it.
6361 struct got_pathlist_entry *pe;
6362 TAILQ_FOREACH(pe, merged_paths, entry) {
6363 err = worktree_status(worktree, pe->path, fileindex,
6364 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6370 err = worktree_status(worktree, "", fileindex, repo,
6371 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6376 if (TAILQ_EMPTY(&commitable_paths)) {
6377 /* No-op change; commit will be elided. */
6378 err = got_ref_delete(commit_ref, repo);
6381 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6385 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6389 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6394 logmsg = strdup(new_logmsg);
6395 if (logmsg == NULL) {
6396 err = got_error_from_errno("strdup");
6400 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6405 /* NB: commit_worktree will call free(logmsg) */
6406 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6407 NULL, worktree, got_object_commit_get_author(orig_commit),
6408 got_object_commit_get_committer(orig_commit),
6409 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6413 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6417 err = got_ref_delete(commit_ref, repo);
6421 err = update_fileindex_after_commit(worktree, &commitable_paths,
6422 *new_commit_id, fileindex, 0);
6423 sync_err = sync_fileindex(fileindex, fileindex_path);
6424 if (sync_err && err == NULL)
6427 free(fileindex_path);
6428 free(head_commit_id);
6430 got_ref_close(head_ref);
6432 free(*new_commit_id);
6433 *new_commit_id = NULL;
6438 const struct got_error *
6439 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6440 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6441 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6442 struct got_commit_object *orig_commit,
6443 struct got_object_id *orig_commit_id, struct got_repository *repo)
6445 const struct got_error *err;
6446 char *commit_ref_name;
6447 struct got_reference *commit_ref = NULL;
6448 struct got_object_id *commit_id = NULL;
6450 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6454 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6457 err = got_ref_resolve(&commit_id, repo, commit_ref);
6460 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6461 err = got_error(GOT_ERR_REBASE_COMMITID);
6465 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6466 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6469 got_ref_close(commit_ref);
6470 free(commit_ref_name);
6475 const struct got_error *
6476 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6477 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6478 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6479 struct got_commit_object *orig_commit,
6480 struct got_object_id *orig_commit_id, const char *new_logmsg,
6481 struct got_repository *repo)
6483 const struct got_error *err;
6484 char *commit_ref_name;
6485 struct got_reference *commit_ref = NULL;
6487 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6491 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6495 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6496 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6499 got_ref_close(commit_ref);
6500 free(commit_ref_name);
6504 const struct got_error *
6505 got_worktree_rebase_postpone(struct got_worktree *worktree,
6506 struct got_fileindex *fileindex)
6509 got_fileindex_free(fileindex);
6510 return lock_worktree(worktree, LOCK_SH);
6513 static const struct got_error *
6514 delete_ref(const char *name, struct got_repository *repo)
6516 const struct got_error *err;
6517 struct got_reference *ref;
6519 err = got_ref_open(&ref, repo, name, 0);
6521 if (err->code == GOT_ERR_NOT_REF)
6526 err = got_ref_delete(ref, repo);
6531 static const struct got_error *
6532 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6534 const struct got_error *err;
6535 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6536 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6538 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6541 err = delete_ref(tmp_branch_name, repo);
6545 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6548 err = delete_ref(new_base_branch_ref_name, repo);
6552 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6555 err = delete_ref(branch_ref_name, repo);
6559 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6562 err = delete_ref(commit_ref_name, repo);
6567 free(tmp_branch_name);
6568 free(new_base_branch_ref_name);
6569 free(branch_ref_name);
6570 free(commit_ref_name);
6574 const struct got_error *
6575 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6576 struct got_object_id *new_commit_id, struct got_repository *repo)
6578 const struct got_error *err;
6579 struct got_reference *ref = NULL;
6580 struct got_object_id *old_commit_id = NULL;
6581 const char *branch_name = NULL;
6582 char *new_id_str = NULL;
6583 char *refname = NULL;
6585 branch_name = got_ref_get_name(branch);
6586 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6587 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6590 err = got_object_id_str(&new_id_str, new_commit_id);
6594 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6595 new_id_str) == -1) {
6596 err = got_error_from_errno("asprintf");
6600 err = got_ref_resolve(&old_commit_id, repo, branch);
6604 err = got_ref_alloc(&ref, refname, old_commit_id);
6608 err = got_ref_write(ref, repo);
6612 free(old_commit_id);
6618 const struct got_error *
6619 got_worktree_rebase_complete(struct got_worktree *worktree,
6620 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6621 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6622 struct got_repository *repo, int create_backup)
6624 const struct got_error *err, *unlockerr, *sync_err;
6625 struct got_object_id *new_head_commit_id = NULL;
6626 char *fileindex_path = NULL;
6628 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6632 if (create_backup) {
6633 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6634 rebased_branch, new_head_commit_id, repo);
6639 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6643 err = got_ref_write(rebased_branch, repo);
6647 err = got_worktree_set_head_ref(worktree, rebased_branch);
6651 err = delete_rebase_refs(worktree, repo);
6655 err = get_fileindex_path(&fileindex_path, worktree);
6658 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6659 sync_err = sync_fileindex(fileindex, fileindex_path);
6660 if (sync_err && err == NULL)
6663 got_fileindex_free(fileindex);
6664 free(fileindex_path);
6665 free(new_head_commit_id);
6666 unlockerr = lock_worktree(worktree, LOCK_SH);
6667 if (unlockerr && err == NULL)
6672 const struct got_error *
6673 got_worktree_rebase_abort(struct got_worktree *worktree,
6674 struct got_fileindex *fileindex, struct got_repository *repo,
6675 struct got_reference *new_base_branch,
6676 got_worktree_checkout_cb progress_cb, void *progress_arg)
6678 const struct got_error *err, *unlockerr, *sync_err;
6679 struct got_reference *resolved = NULL;
6680 struct got_object_id *commit_id = NULL;
6681 char *fileindex_path = NULL;
6682 struct revert_file_args rfa;
6683 struct got_object_id *tree_id = NULL;
6685 err = lock_worktree(worktree, LOCK_EX);
6689 err = got_ref_open(&resolved, repo,
6690 got_ref_get_symref_target(new_base_branch), 0);
6694 err = got_worktree_set_head_ref(worktree, resolved);
6699 * XXX commits to the base branch could have happened while
6700 * we were busy rebasing; should we store the original commit ID
6701 * when rebase begins and read it back here?
6703 err = got_ref_resolve(&commit_id, repo, resolved);
6707 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6711 err = got_object_id_by_path(&tree_id, repo,
6712 worktree->base_commit_id, worktree->path_prefix);
6716 err = delete_rebase_refs(worktree, repo);
6720 err = get_fileindex_path(&fileindex_path, worktree);
6724 rfa.worktree = worktree;
6725 rfa.fileindex = fileindex;
6726 rfa.progress_cb = progress_cb;
6727 rfa.progress_arg = progress_arg;
6728 rfa.patch_cb = NULL;
6729 rfa.patch_arg = NULL;
6731 rfa.unlink_added_files = 0;
6732 err = worktree_status(worktree, "", fileindex, repo,
6733 revert_file, &rfa, NULL, NULL, 1, 0);
6737 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6738 repo, progress_cb, progress_arg, NULL, NULL);
6740 sync_err = sync_fileindex(fileindex, fileindex_path);
6741 if (sync_err && err == NULL)
6744 got_ref_close(resolved);
6748 got_fileindex_free(fileindex);
6749 free(fileindex_path);
6751 unlockerr = lock_worktree(worktree, LOCK_SH);
6752 if (unlockerr && err == NULL)
6757 const struct got_error *
6758 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6759 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6760 struct got_fileindex **fileindex, struct got_worktree *worktree,
6761 struct got_repository *repo)
6763 const struct got_error *err = NULL;
6764 char *tmp_branch_name = NULL;
6765 char *branch_ref_name = NULL;
6766 char *base_commit_ref_name = NULL;
6767 char *fileindex_path = NULL;
6768 struct check_rebase_ok_arg ok_arg;
6769 struct got_reference *wt_branch = NULL;
6770 struct got_reference *base_commit_ref = NULL;
6774 *base_commit_id = NULL;
6777 err = lock_worktree(worktree, LOCK_EX);
6781 err = open_fileindex(fileindex, &fileindex_path, worktree);
6785 ok_arg.worktree = worktree;
6787 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6792 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6796 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6800 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6805 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6810 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6814 err = got_ref_write(*branch_ref, repo);
6818 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6819 worktree->base_commit_id);
6822 err = got_ref_write(base_commit_ref, repo);
6825 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6826 if (*base_commit_id == NULL) {
6827 err = got_error_from_errno("got_object_id_dup");
6831 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6832 worktree->base_commit_id);
6835 err = got_ref_write(*tmp_branch, repo);
6839 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6843 free(fileindex_path);
6844 free(tmp_branch_name);
6845 free(branch_ref_name);
6846 free(base_commit_ref_name);
6848 got_ref_close(wt_branch);
6851 got_ref_close(*branch_ref);
6855 got_ref_close(*tmp_branch);
6858 free(*base_commit_id);
6860 got_fileindex_free(*fileindex);
6863 lock_worktree(worktree, LOCK_SH);
6868 const struct got_error *
6869 got_worktree_histedit_postpone(struct got_worktree *worktree,
6870 struct got_fileindex *fileindex)
6873 got_fileindex_free(fileindex);
6874 return lock_worktree(worktree, LOCK_SH);
6877 const struct got_error *
6878 got_worktree_histedit_in_progress(int *in_progress,
6879 struct got_worktree *worktree)
6881 const struct got_error *err;
6882 char *tmp_branch_name = NULL;
6884 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6888 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6889 free(tmp_branch_name);
6893 const struct got_error *
6894 got_worktree_histedit_continue(struct got_object_id **commit_id,
6895 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6896 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6897 struct got_worktree *worktree, struct got_repository *repo)
6899 const struct got_error *err;
6900 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6901 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6902 struct got_reference *commit_ref = NULL;
6903 struct got_reference *base_commit_ref = NULL;
6904 char *fileindex_path = NULL;
6905 int have_staged_files = 0;
6909 *base_commit_id = NULL;
6912 err = lock_worktree(worktree, LOCK_EX);
6916 err = open_fileindex(fileindex, &fileindex_path, worktree);
6920 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6921 &have_staged_files);
6922 if (err && err->code != GOT_ERR_CANCELLED)
6924 if (have_staged_files) {
6925 err = got_error(GOT_ERR_STAGED_PATHS);
6929 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6933 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6937 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6941 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6946 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6950 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6953 err = got_ref_resolve(commit_id, repo, commit_ref);
6957 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6960 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6964 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6968 free(commit_ref_name);
6969 free(branch_ref_name);
6970 free(fileindex_path);
6972 got_ref_close(commit_ref);
6973 if (base_commit_ref)
6974 got_ref_close(base_commit_ref);
6978 free(*base_commit_id);
6979 *base_commit_id = NULL;
6981 got_ref_close(*tmp_branch);
6985 got_fileindex_free(*fileindex);
6988 lock_worktree(worktree, LOCK_EX);
6993 static const struct got_error *
6994 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6996 const struct got_error *err;
6997 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6998 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7000 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7003 err = delete_ref(tmp_branch_name, repo);
7007 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7011 err = delete_ref(base_commit_ref_name, repo);
7015 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7018 err = delete_ref(branch_ref_name, repo);
7022 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7025 err = delete_ref(commit_ref_name, repo);
7029 free(tmp_branch_name);
7030 free(base_commit_ref_name);
7031 free(branch_ref_name);
7032 free(commit_ref_name);
7036 const struct got_error *
7037 got_worktree_histedit_abort(struct got_worktree *worktree,
7038 struct got_fileindex *fileindex, struct got_repository *repo,
7039 struct got_reference *branch, struct got_object_id *base_commit_id,
7040 got_worktree_checkout_cb progress_cb, void *progress_arg)
7042 const struct got_error *err, *unlockerr, *sync_err;
7043 struct got_reference *resolved = NULL;
7044 char *fileindex_path = NULL;
7045 struct got_object_id *tree_id = NULL;
7046 struct revert_file_args rfa;
7048 err = lock_worktree(worktree, LOCK_EX);
7052 err = got_ref_open(&resolved, repo,
7053 got_ref_get_symref_target(branch), 0);
7057 err = got_worktree_set_head_ref(worktree, resolved);
7061 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7065 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
7066 worktree->path_prefix);
7070 err = delete_histedit_refs(worktree, repo);
7074 err = get_fileindex_path(&fileindex_path, worktree);
7078 rfa.worktree = worktree;
7079 rfa.fileindex = fileindex;
7080 rfa.progress_cb = progress_cb;
7081 rfa.progress_arg = progress_arg;
7082 rfa.patch_cb = NULL;
7083 rfa.patch_arg = NULL;
7085 rfa.unlink_added_files = 0;
7086 err = worktree_status(worktree, "", fileindex, repo,
7087 revert_file, &rfa, NULL, NULL, 1, 0);
7091 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7092 repo, progress_cb, progress_arg, NULL, NULL);
7094 sync_err = sync_fileindex(fileindex, fileindex_path);
7095 if (sync_err && err == NULL)
7098 got_ref_close(resolved);
7100 free(fileindex_path);
7102 unlockerr = lock_worktree(worktree, LOCK_SH);
7103 if (unlockerr && err == NULL)
7108 const struct got_error *
7109 got_worktree_histedit_complete(struct got_worktree *worktree,
7110 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7111 struct got_reference *edited_branch, struct got_repository *repo)
7113 const struct got_error *err, *unlockerr, *sync_err;
7114 struct got_object_id *new_head_commit_id = NULL;
7115 struct got_reference *resolved = NULL;
7116 char *fileindex_path = NULL;
7118 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7122 err = got_ref_open(&resolved, repo,
7123 got_ref_get_symref_target(edited_branch), 0);
7127 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7128 resolved, new_head_commit_id, repo);
7132 err = got_ref_change_ref(resolved, new_head_commit_id);
7136 err = got_ref_write(resolved, repo);
7140 err = got_worktree_set_head_ref(worktree, resolved);
7144 err = delete_histedit_refs(worktree, repo);
7148 err = get_fileindex_path(&fileindex_path, worktree);
7151 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7152 sync_err = sync_fileindex(fileindex, fileindex_path);
7153 if (sync_err && err == NULL)
7156 got_fileindex_free(fileindex);
7157 free(fileindex_path);
7158 free(new_head_commit_id);
7159 unlockerr = lock_worktree(worktree, LOCK_SH);
7160 if (unlockerr && err == NULL)
7165 const struct got_error *
7166 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7167 struct got_object_id *commit_id, struct got_repository *repo)
7169 const struct got_error *err;
7170 char *commit_ref_name;
7172 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7176 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7180 err = delete_ref(commit_ref_name, repo);
7182 free(commit_ref_name);
7186 const struct got_error *
7187 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7188 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7189 struct got_worktree *worktree, const char *refname,
7190 struct got_repository *repo)
7192 const struct got_error *err = NULL;
7193 char *fileindex_path = NULL;
7194 struct check_rebase_ok_arg ok_arg;
7198 *base_branch_ref = NULL;
7200 err = lock_worktree(worktree, LOCK_EX);
7204 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7205 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7206 "cannot integrate a branch into itself; "
7207 "update -b or different branch name required");
7211 err = open_fileindex(fileindex, &fileindex_path, worktree);
7215 /* Preconditions are the same as for rebase. */
7216 ok_arg.worktree = worktree;
7218 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7223 err = got_ref_open(branch_ref, repo, refname, 1);
7227 err = got_ref_open(base_branch_ref, repo,
7228 got_worktree_get_head_ref_name(worktree), 1);
7232 got_ref_close(*branch_ref);
7235 if (*base_branch_ref) {
7236 got_ref_close(*base_branch_ref);
7237 *base_branch_ref = NULL;
7240 got_fileindex_free(*fileindex);
7243 lock_worktree(worktree, LOCK_SH);
7248 const struct got_error *
7249 got_worktree_integrate_continue(struct got_worktree *worktree,
7250 struct got_fileindex *fileindex, struct got_repository *repo,
7251 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7252 got_worktree_checkout_cb progress_cb, void *progress_arg,
7253 got_cancel_cb cancel_cb, void *cancel_arg)
7255 const struct got_error *err = NULL, *sync_err, *unlockerr;
7256 char *fileindex_path = NULL;
7257 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7259 err = get_fileindex_path(&fileindex_path, worktree);
7263 err = got_ref_resolve(&commit_id, repo, branch_ref);
7267 err = got_object_id_by_path(&tree_id, repo, commit_id,
7268 worktree->path_prefix);
7272 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7276 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7277 progress_cb, progress_arg, cancel_cb, cancel_arg);
7281 err = got_ref_change_ref(base_branch_ref, commit_id);
7285 err = got_ref_write(base_branch_ref, repo);
7289 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7291 sync_err = sync_fileindex(fileindex, fileindex_path);
7292 if (sync_err && err == NULL)
7296 unlockerr = got_ref_unlock(branch_ref);
7297 if (unlockerr && err == NULL)
7299 got_ref_close(branch_ref);
7301 unlockerr = got_ref_unlock(base_branch_ref);
7302 if (unlockerr && err == NULL)
7304 got_ref_close(base_branch_ref);
7306 got_fileindex_free(fileindex);
7307 free(fileindex_path);
7310 unlockerr = lock_worktree(worktree, LOCK_SH);
7311 if (unlockerr && err == NULL)
7316 const struct got_error *
7317 got_worktree_integrate_abort(struct got_worktree *worktree,
7318 struct got_fileindex *fileindex, struct got_repository *repo,
7319 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7321 const struct got_error *err = NULL, *unlockerr = NULL;
7323 got_fileindex_free(fileindex);
7325 err = lock_worktree(worktree, LOCK_SH);
7327 unlockerr = got_ref_unlock(branch_ref);
7328 if (unlockerr && err == NULL)
7330 got_ref_close(branch_ref);
7332 unlockerr = got_ref_unlock(base_branch_ref);
7333 if (unlockerr && err == NULL)
7335 got_ref_close(base_branch_ref);
7340 const struct got_error *
7341 got_worktree_merge_postpone(struct got_worktree *worktree,
7342 struct got_fileindex *fileindex)
7344 const struct got_error *err, *sync_err;
7345 char *fileindex_path = NULL;
7347 err = get_fileindex_path(&fileindex_path, worktree);
7351 sync_err = sync_fileindex(fileindex, fileindex_path);
7353 err = lock_worktree(worktree, LOCK_SH);
7354 if (sync_err && err == NULL)
7357 got_fileindex_free(fileindex);
7358 free(fileindex_path);
7362 static const struct got_error *
7363 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7365 const struct got_error *err;
7366 char *branch_refname = NULL, *commit_refname = NULL;
7368 err = get_merge_branch_ref_name(&branch_refname, worktree);
7371 err = delete_ref(branch_refname, repo);
7375 err = get_merge_commit_ref_name(&commit_refname, worktree);
7378 err = delete_ref(commit_refname, repo);
7383 free(branch_refname);
7384 free(commit_refname);
7388 struct merge_commit_msg_arg {
7389 struct got_worktree *worktree;
7390 const char *branch_name;
7393 static const struct got_error *
7394 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths, char **logmsg,
7397 struct merge_commit_msg_arg *a = arg;
7399 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7400 got_worktree_get_head_ref_name(a->worktree)) == -1)
7401 return got_error_from_errno("asprintf");
7407 const struct got_error *
7408 got_worktree_merge_branch(struct got_worktree *worktree,
7409 struct got_fileindex *fileindex,
7410 struct got_object_id *yca_commit_id,
7411 struct got_object_id *branch_tip,
7412 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7413 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7415 const struct got_error *err;
7416 char *fileindex_path = NULL;
7418 err = get_fileindex_path(&fileindex_path, worktree);
7422 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7427 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7428 branch_tip, repo, progress_cb, progress_arg,
7429 cancel_cb, cancel_arg);
7431 free(fileindex_path);
7435 const struct got_error *
7436 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7437 struct got_worktree *worktree, struct got_fileindex *fileindex,
7438 const char *author, const char *committer, int allow_bad_symlinks,
7439 struct got_object_id *branch_tip, const char *branch_name,
7440 struct got_repository *repo,
7441 got_worktree_status_cb status_cb, void *status_arg)
7444 const struct got_error *err = NULL, *sync_err;
7445 struct got_pathlist_head commitable_paths;
7446 struct collect_commitables_arg cc_arg;
7447 struct got_pathlist_entry *pe;
7448 struct got_reference *head_ref = NULL;
7449 struct got_object_id *head_commit_id = NULL;
7450 int have_staged_files = 0;
7451 struct merge_commit_msg_arg mcm_arg;
7452 char *fileindex_path = NULL;
7454 *new_commit_id = NULL;
7456 TAILQ_INIT(&commitable_paths);
7458 err = get_fileindex_path(&fileindex_path, worktree);
7462 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7466 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7470 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7471 &have_staged_files);
7472 if (err && err->code != GOT_ERR_CANCELLED)
7474 if (have_staged_files) {
7475 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7479 cc_arg.commitable_paths = &commitable_paths;
7480 cc_arg.worktree = worktree;
7481 cc_arg.fileindex = fileindex;
7483 cc_arg.have_staged_files = have_staged_files;
7484 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7485 err = worktree_status(worktree, "", fileindex, repo,
7486 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7490 if (TAILQ_EMPTY(&commitable_paths)) {
7491 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7492 "merge of %s cannot proceed", branch_name);
7496 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7497 struct got_commitable *ct = pe->data;
7498 const char *ct_path = ct->in_repo_path;
7500 while (ct_path[0] == '/')
7502 err = check_out_of_date(ct_path, ct->status,
7503 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7504 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7510 mcm_arg.worktree = worktree;
7511 mcm_arg.branch_name = branch_name;
7512 err = commit_worktree(new_commit_id, &commitable_paths,
7513 head_commit_id, branch_tip, worktree, author, committer,
7514 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7518 err = update_fileindex_after_commit(worktree, &commitable_paths,
7519 *new_commit_id, fileindex, have_staged_files);
7520 sync_err = sync_fileindex(fileindex, fileindex_path);
7521 if (sync_err && err == NULL)
7524 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7525 struct got_commitable *ct = pe->data;
7526 free_commitable(ct);
7528 got_pathlist_free(&commitable_paths);
7529 free(fileindex_path);
7533 const struct got_error *
7534 got_worktree_merge_complete(struct got_worktree *worktree,
7535 struct got_fileindex *fileindex, struct got_repository *repo)
7537 const struct got_error *err, *unlockerr, *sync_err;
7538 char *fileindex_path = NULL;
7540 err = delete_merge_refs(worktree, repo);
7544 err = get_fileindex_path(&fileindex_path, worktree);
7547 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7548 sync_err = sync_fileindex(fileindex, fileindex_path);
7549 if (sync_err && err == NULL)
7552 got_fileindex_free(fileindex);
7553 free(fileindex_path);
7554 unlockerr = lock_worktree(worktree, LOCK_SH);
7555 if (unlockerr && err == NULL)
7560 const struct got_error *
7561 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7562 struct got_repository *repo)
7564 const struct got_error *err;
7565 char *branch_refname = NULL;
7566 struct got_reference *branch_ref = NULL;
7570 err = get_merge_branch_ref_name(&branch_refname, worktree);
7573 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7574 free(branch_refname);
7576 if (err->code != GOT_ERR_NOT_REF)
7584 const struct got_error *got_worktree_merge_prepare(
7585 struct got_fileindex **fileindex, struct got_worktree *worktree,
7586 struct got_reference *branch, struct got_repository *repo)
7588 const struct got_error *err = NULL;
7589 char *fileindex_path = NULL;
7590 char *branch_refname = NULL, *commit_refname = NULL;
7591 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
7592 struct got_reference *commit_ref = NULL;
7593 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
7594 struct check_rebase_ok_arg ok_arg;
7598 err = lock_worktree(worktree, LOCK_EX);
7602 err = open_fileindex(fileindex, &fileindex_path, worktree);
7606 /* Preconditions are the same as for rebase. */
7607 ok_arg.worktree = worktree;
7609 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7614 err = get_merge_branch_ref_name(&branch_refname, worktree);
7618 err = get_merge_commit_ref_name(&commit_refname, worktree);
7622 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7627 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
7631 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
7632 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
7636 err = got_ref_resolve(&branch_tip, repo, branch);
7640 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
7643 err = got_ref_write(branch_ref, repo);
7647 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
7650 err = got_ref_write(commit_ref, repo);
7655 free(branch_refname);
7656 free(commit_refname);
7657 free(fileindex_path);
7659 got_ref_close(branch_ref);
7661 got_ref_close(commit_ref);
7663 got_ref_close(wt_branch);
7664 free(wt_branch_tip);
7667 got_fileindex_free(*fileindex);
7670 lock_worktree(worktree, LOCK_SH);
7675 const struct got_error *
7676 got_worktree_merge_continue(char **branch_name,
7677 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
7678 struct got_worktree *worktree, struct got_repository *repo)
7680 const struct got_error *err;
7681 char *commit_refname = NULL, *branch_refname = NULL;
7682 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
7683 char *fileindex_path = NULL;
7684 int have_staged_files = 0;
7686 *branch_name = NULL;
7690 err = lock_worktree(worktree, LOCK_EX);
7694 err = open_fileindex(fileindex, &fileindex_path, worktree);
7698 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7699 &have_staged_files);
7700 if (err && err->code != GOT_ERR_CANCELLED)
7702 if (have_staged_files) {
7703 err = got_error(GOT_ERR_STAGED_PATHS);
7707 err = get_merge_branch_ref_name(&branch_refname, worktree);
7711 err = get_merge_commit_ref_name(&commit_refname, worktree);
7715 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7719 if (!got_ref_is_symbolic(branch_ref)) {
7720 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7721 "%s is not a symbolic reference",
7722 got_ref_get_name(branch_ref));
7725 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
7726 if (*branch_name == NULL) {
7727 err = got_error_from_errno("strdup");
7731 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
7735 err = got_ref_resolve(branch_tip, repo, commit_ref);
7739 free(commit_refname);
7740 free(branch_refname);
7741 free(fileindex_path);
7743 got_ref_close(commit_ref);
7745 got_ref_close(branch_ref);
7749 *branch_name = NULL;
7754 got_fileindex_free(*fileindex);
7757 lock_worktree(worktree, LOCK_SH);
7762 const struct got_error *
7763 got_worktree_merge_abort(struct got_worktree *worktree,
7764 struct got_fileindex *fileindex, struct got_repository *repo,
7765 got_worktree_checkout_cb progress_cb, void *progress_arg)
7767 const struct got_error *err, *unlockerr, *sync_err;
7768 struct got_object_id *commit_id = NULL;
7769 char *fileindex_path = NULL;
7770 struct revert_file_args rfa;
7771 struct got_object_id *tree_id = NULL;
7773 err = got_object_id_by_path(&tree_id, repo,
7774 worktree->base_commit_id, worktree->path_prefix);
7778 err = delete_merge_refs(worktree, repo);
7782 err = get_fileindex_path(&fileindex_path, worktree);
7786 rfa.worktree = worktree;
7787 rfa.fileindex = fileindex;
7788 rfa.progress_cb = progress_cb;
7789 rfa.progress_arg = progress_arg;
7790 rfa.patch_cb = NULL;
7791 rfa.patch_arg = NULL;
7793 rfa.unlink_added_files = 1;
7794 err = worktree_status(worktree, "", fileindex, repo,
7795 revert_file, &rfa, NULL, NULL, 1, 0);
7799 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7800 repo, progress_cb, progress_arg, NULL, NULL);
7802 sync_err = sync_fileindex(fileindex, fileindex_path);
7803 if (sync_err && err == NULL)
7809 got_fileindex_free(fileindex);
7810 free(fileindex_path);
7812 unlockerr = lock_worktree(worktree, LOCK_SH);
7813 if (unlockerr && err == NULL)
7818 struct check_stage_ok_arg {
7819 struct got_object_id *head_commit_id;
7820 struct got_worktree *worktree;
7821 struct got_fileindex *fileindex;
7822 struct got_repository *repo;
7826 const struct got_error *
7827 check_stage_ok(void *arg, unsigned char status,
7828 unsigned char staged_status, const char *relpath,
7829 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7830 struct got_object_id *commit_id, int dirfd, const char *de_name)
7832 struct check_stage_ok_arg *a = arg;
7833 const struct got_error *err = NULL;
7834 struct got_fileindex_entry *ie;
7835 struct got_object_id base_commit_id;
7836 struct got_object_id *base_commit_idp = NULL;
7837 char *in_repo_path = NULL, *p;
7839 if (status == GOT_STATUS_UNVERSIONED ||
7840 status == GOT_STATUS_NO_CHANGE)
7842 if (status == GOT_STATUS_NONEXISTENT)
7843 return got_error_set_errno(ENOENT, relpath);
7845 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7847 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7849 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7850 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7852 return got_error_from_errno("asprintf");
7854 if (got_fileindex_entry_has_commit(ie)) {
7855 memcpy(base_commit_id.sha1, ie->commit_sha1,
7856 SHA1_DIGEST_LENGTH);
7857 base_commit_idp = &base_commit_id;
7860 if (status == GOT_STATUS_CONFLICT) {
7861 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7863 } else if (status != GOT_STATUS_ADD &&
7864 status != GOT_STATUS_MODIFY &&
7865 status != GOT_STATUS_DELETE) {
7866 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7870 a->have_changes = 1;
7875 err = check_out_of_date(p, status, staged_status,
7876 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7877 GOT_ERR_STAGE_OUT_OF_DATE);
7883 struct stage_path_arg {
7884 struct got_worktree *worktree;
7885 struct got_fileindex *fileindex;
7886 struct got_repository *repo;
7887 got_worktree_status_cb status_cb;
7889 got_worktree_patch_cb patch_cb;
7891 int staged_something;
7892 int allow_bad_symlinks;
7895 static const struct got_error *
7896 stage_path(void *arg, unsigned char status,
7897 unsigned char staged_status, const char *relpath,
7898 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7899 struct got_object_id *commit_id, int dirfd, const char *de_name)
7901 struct stage_path_arg *a = arg;
7902 const struct got_error *err = NULL;
7903 struct got_fileindex_entry *ie;
7904 char *ondisk_path = NULL, *path_content = NULL;
7906 struct got_object_id *new_staged_blob_id = NULL;
7909 if (status == GOT_STATUS_UNVERSIONED)
7912 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7914 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7916 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7918 return got_error_from_errno("asprintf");
7921 case GOT_STATUS_ADD:
7922 case GOT_STATUS_MODIFY:
7923 /* XXX could sb.st_mode be passed in by our caller? */
7924 if (lstat(ondisk_path, &sb) == -1) {
7925 err = got_error_from_errno2("lstat", ondisk_path);
7929 if (status == GOT_STATUS_ADD) {
7930 int choice = GOT_PATCH_CHOICE_NONE;
7931 err = (*a->patch_cb)(&choice, a->patch_arg,
7932 status, ie->path, NULL, 1, 1);
7935 if (choice != GOT_PATCH_CHOICE_YES)
7938 err = create_patched_content(&path_content, 0,
7939 staged_blob_id ? staged_blob_id : blob_id,
7940 ondisk_path, dirfd, de_name, ie->path,
7941 a->repo, a->patch_cb, a->patch_arg);
7942 if (err || path_content == NULL)
7946 err = got_object_blob_create(&new_staged_blob_id,
7947 path_content ? path_content : ondisk_path, a->repo);
7950 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7951 SHA1_DIGEST_LENGTH);
7952 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7953 stage = GOT_FILEIDX_STAGE_ADD;
7955 stage = GOT_FILEIDX_STAGE_MODIFY;
7956 got_fileindex_entry_stage_set(ie, stage);
7957 if (S_ISLNK(sb.st_mode)) {
7958 int is_bad_symlink = 0;
7959 if (!a->allow_bad_symlinks) {
7960 char target_path[PATH_MAX];
7962 target_len = readlink(ondisk_path, target_path,
7963 sizeof(target_path));
7964 if (target_len == -1) {
7965 err = got_error_from_errno2("readlink",
7969 err = is_bad_symlink_target(&is_bad_symlink,
7970 target_path, target_len, ondisk_path,
7971 a->worktree->root_path);
7974 if (is_bad_symlink) {
7975 err = got_error_path(ondisk_path,
7976 GOT_ERR_BAD_SYMLINK);
7981 got_fileindex_entry_staged_filetype_set(ie,
7982 GOT_FILEIDX_MODE_BAD_SYMLINK);
7984 got_fileindex_entry_staged_filetype_set(ie,
7985 GOT_FILEIDX_MODE_SYMLINK);
7987 got_fileindex_entry_staged_filetype_set(ie,
7988 GOT_FILEIDX_MODE_REGULAR_FILE);
7990 a->staged_something = 1;
7991 if (a->status_cb == NULL)
7993 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7994 get_staged_status(ie), relpath, blob_id,
7995 new_staged_blob_id, NULL, dirfd, de_name);
7997 case GOT_STATUS_DELETE:
7998 if (staged_status == GOT_STATUS_DELETE)
8001 int choice = GOT_PATCH_CHOICE_NONE;
8002 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8003 ie->path, NULL, 1, 1);
8006 if (choice == GOT_PATCH_CHOICE_NO)
8008 if (choice != GOT_PATCH_CHOICE_YES) {
8009 err = got_error(GOT_ERR_PATCH_CHOICE);
8013 stage = GOT_FILEIDX_STAGE_DELETE;
8014 got_fileindex_entry_stage_set(ie, stage);
8015 a->staged_something = 1;
8016 if (a->status_cb == NULL)
8018 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8019 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8022 case GOT_STATUS_NO_CHANGE:
8024 case GOT_STATUS_CONFLICT:
8025 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8027 case GOT_STATUS_NONEXISTENT:
8028 err = got_error_set_errno(ENOENT, relpath);
8031 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8035 if (path_content && unlink(path_content) == -1 && err == NULL)
8036 err = got_error_from_errno2("unlink", path_content);
8039 free(new_staged_blob_id);
8043 const struct got_error *
8044 got_worktree_stage(struct got_worktree *worktree,
8045 struct got_pathlist_head *paths,
8046 got_worktree_status_cb status_cb, void *status_arg,
8047 got_worktree_patch_cb patch_cb, void *patch_arg,
8048 int allow_bad_symlinks, struct got_repository *repo)
8050 const struct got_error *err = NULL, *sync_err, *unlockerr;
8051 struct got_pathlist_entry *pe;
8052 struct got_fileindex *fileindex = NULL;
8053 char *fileindex_path = NULL;
8054 struct got_reference *head_ref = NULL;
8055 struct got_object_id *head_commit_id = NULL;
8056 struct check_stage_ok_arg oka;
8057 struct stage_path_arg spa;
8059 err = lock_worktree(worktree, LOCK_EX);
8063 err = got_ref_open(&head_ref, repo,
8064 got_worktree_get_head_ref_name(worktree), 0);
8067 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8070 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8074 /* Check pre-conditions before staging anything. */
8075 oka.head_commit_id = head_commit_id;
8076 oka.worktree = worktree;
8077 oka.fileindex = fileindex;
8079 oka.have_changes = 0;
8080 TAILQ_FOREACH(pe, paths, entry) {
8081 err = worktree_status(worktree, pe->path, fileindex, repo,
8082 check_stage_ok, &oka, NULL, NULL, 1, 0);
8086 if (!oka.have_changes) {
8087 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8091 spa.worktree = worktree;
8092 spa.fileindex = fileindex;
8094 spa.patch_cb = patch_cb;
8095 spa.patch_arg = patch_arg;
8096 spa.status_cb = status_cb;
8097 spa.status_arg = status_arg;
8098 spa.staged_something = 0;
8099 spa.allow_bad_symlinks = allow_bad_symlinks;
8100 TAILQ_FOREACH(pe, paths, entry) {
8101 err = worktree_status(worktree, pe->path, fileindex, repo,
8102 stage_path, &spa, NULL, NULL, 1, 0);
8106 if (!spa.staged_something) {
8107 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8111 sync_err = sync_fileindex(fileindex, fileindex_path);
8112 if (sync_err && err == NULL)
8116 got_ref_close(head_ref);
8117 free(head_commit_id);
8118 free(fileindex_path);
8120 got_fileindex_free(fileindex);
8121 unlockerr = lock_worktree(worktree, LOCK_SH);
8122 if (unlockerr && err == NULL)
8127 struct unstage_path_arg {
8128 struct got_worktree *worktree;
8129 struct got_fileindex *fileindex;
8130 struct got_repository *repo;
8131 got_worktree_checkout_cb progress_cb;
8133 got_worktree_patch_cb patch_cb;
8137 static const struct got_error *
8138 create_unstaged_content(char **path_unstaged_content,
8139 char **path_new_staged_content, struct got_object_id *blob_id,
8140 struct got_object_id *staged_blob_id, const char *relpath,
8141 struct got_repository *repo,
8142 got_worktree_patch_cb patch_cb, void *patch_arg)
8144 const struct got_error *err, *free_err;
8145 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8146 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8147 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8148 struct got_diffreg_result *diffreg_result = NULL;
8149 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8150 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8152 *path_unstaged_content = NULL;
8153 *path_new_staged_content = NULL;
8155 err = got_object_id_str(&label1, blob_id);
8158 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
8162 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
8166 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8170 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
8174 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
8178 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8182 err = got_diff_files(&diffreg_result, f1, label1, f2,
8183 path2, 3, 0, 1, NULL);
8187 err = got_opentemp_named(path_unstaged_content, &outfile,
8188 "got-unstaged-content");
8191 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8192 "got-new-staged-content");
8196 if (fseek(f1, 0L, SEEK_SET) == -1) {
8197 err = got_ferror(f1, GOT_ERR_IO);
8200 if (fseek(f2, 0L, SEEK_SET) == -1) {
8201 err = got_ferror(f2, GOT_ERR_IO);
8204 /* Count the number of actual changes in the diff result. */
8205 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8206 struct diff_chunk_context cc = {};
8207 diff_chunk_context_load_change(&cc, &nchunks_used,
8208 diffreg_result->result, n, 0);
8211 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8213 err = apply_or_reject_change(&choice, &nchunks_used,
8214 diffreg_result->result, n, relpath, f1, f2,
8215 &line_cur1, &line_cur2,
8216 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8219 if (choice == GOT_PATCH_CHOICE_YES)
8222 have_rejected_content = 1;
8223 if (choice == GOT_PATCH_CHOICE_QUIT)
8226 if (have_content || have_rejected_content)
8227 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8228 outfile, rejectfile);
8232 got_object_blob_close(blob);
8234 got_object_blob_close(staged_blob);
8235 free_err = got_diffreg_result_free(diffreg_result);
8236 if (free_err && err == NULL)
8238 if (f1 && fclose(f1) == EOF && err == NULL)
8239 err = got_error_from_errno2("fclose", path1);
8240 if (f2 && fclose(f2) == EOF && err == NULL)
8241 err = got_error_from_errno2("fclose", path2);
8242 if (outfile && fclose(outfile) == EOF && err == NULL)
8243 err = got_error_from_errno2("fclose", *path_unstaged_content);
8244 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8245 err = got_error_from_errno2("fclose", *path_new_staged_content);
8246 if (path1 && unlink(path1) == -1 && err == NULL)
8247 err = got_error_from_errno2("unlink", path1);
8248 if (path2 && unlink(path2) == -1 && err == NULL)
8249 err = got_error_from_errno2("unlink", path2);
8250 if (err || !have_content) {
8251 if (*path_unstaged_content &&
8252 unlink(*path_unstaged_content) == -1 && err == NULL)
8253 err = got_error_from_errno2("unlink",
8254 *path_unstaged_content);
8255 free(*path_unstaged_content);
8256 *path_unstaged_content = NULL;
8258 if (err || !have_content || !have_rejected_content) {
8259 if (*path_new_staged_content &&
8260 unlink(*path_new_staged_content) == -1 && err == NULL)
8261 err = got_error_from_errno2("unlink",
8262 *path_new_staged_content);
8263 free(*path_new_staged_content);
8264 *path_new_staged_content = NULL;
8271 static const struct got_error *
8272 unstage_hunks(struct got_object_id *staged_blob_id,
8273 struct got_blob_object *blob_base,
8274 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8275 const char *ondisk_path, const char *label_orig,
8276 struct got_worktree *worktree, struct got_repository *repo,
8277 got_worktree_patch_cb patch_cb, void *patch_arg,
8278 got_worktree_checkout_cb progress_cb, void *progress_arg)
8280 const struct got_error *err = NULL;
8281 char *path_unstaged_content = NULL;
8282 char *path_new_staged_content = NULL;
8283 char *parent = NULL, *base_path = NULL;
8284 char *blob_base_path = NULL;
8285 struct got_object_id *new_staged_blob_id = NULL;
8286 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8289 err = create_unstaged_content(&path_unstaged_content,
8290 &path_new_staged_content, blob_id, staged_blob_id,
8291 ie->path, repo, patch_cb, patch_arg);
8295 if (path_unstaged_content == NULL)
8298 if (path_new_staged_content) {
8299 err = got_object_blob_create(&new_staged_blob_id,
8300 path_new_staged_content, repo);
8305 f = fopen(path_unstaged_content, "re");
8307 err = got_error_from_errno2("fopen",
8308 path_unstaged_content);
8311 if (fstat(fileno(f), &sb) == -1) {
8312 err = got_error_from_errno2("fstat", path_unstaged_content);
8315 if (got_fileindex_entry_staged_filetype_get(ie) ==
8316 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8317 char link_target[PATH_MAX];
8319 r = fread(link_target, 1, sizeof(link_target), f);
8320 if (r == 0 && ferror(f)) {
8321 err = got_error_from_errno("fread");
8324 if (r >= sizeof(link_target)) { /* should not happen */
8325 err = got_error(GOT_ERR_NO_SPACE);
8328 link_target[r] = '\0';
8329 err = merge_symlink(worktree, blob_base,
8330 ondisk_path, ie->path, label_orig, link_target,
8331 worktree->base_commit_id, repo, progress_cb,
8334 int local_changes_subsumed;
8336 err = got_path_dirname(&parent, ondisk_path);
8340 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8342 err = got_error_from_errno("asprintf");
8347 err = got_opentemp_named(&blob_base_path, &f_base, base_path);
8350 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8356 * In order the run a 3-way merge with a symlink we copy the symlink's
8357 * target path into a temporary file and use that file with diff3.
8359 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8360 err = dump_symlink_target_path_to_file(&f_deriv2,
8366 fd = open(ondisk_path,
8367 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8369 err = got_error_from_errno2("open", ondisk_path);
8372 f_deriv2 = fdopen(fd, "r");
8373 if (f_deriv2 == NULL) {
8374 err = got_error_from_errno2("fdopen", ondisk_path);
8380 err = merge_file(&local_changes_subsumed, worktree,
8381 f_base, f, f_deriv2, ondisk_path, ie->path,
8382 got_fileindex_perms_to_st(ie),
8383 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8384 repo, progress_cb, progress_arg);
8389 if (new_staged_blob_id) {
8390 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8391 SHA1_DIGEST_LENGTH);
8393 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8394 got_fileindex_entry_staged_filetype_set(ie, 0);
8397 free(new_staged_blob_id);
8398 if (path_unstaged_content &&
8399 unlink(path_unstaged_content) == -1 && err == NULL)
8400 err = got_error_from_errno2("unlink", path_unstaged_content);
8401 if (path_new_staged_content &&
8402 unlink(path_new_staged_content) == -1 && err == NULL)
8403 err = got_error_from_errno2("unlink", path_new_staged_content);
8404 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8405 err = got_error_from_errno2("unlink", blob_base_path);
8406 if (f_base && fclose(f_base) == EOF && err == NULL)
8407 err = got_error_from_errno2("fclose", path_unstaged_content);
8408 if (f && fclose(f) == EOF && err == NULL)
8409 err = got_error_from_errno2("fclose", path_unstaged_content);
8410 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8411 err = got_error_from_errno2("fclose", ondisk_path);
8412 free(path_unstaged_content);
8413 free(path_new_staged_content);
8414 free(blob_base_path);
8420 static const struct got_error *
8421 unstage_path(void *arg, unsigned char status,
8422 unsigned char staged_status, const char *relpath,
8423 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8424 struct got_object_id *commit_id, int dirfd, const char *de_name)
8426 const struct got_error *err = NULL;
8427 struct unstage_path_arg *a = arg;
8428 struct got_fileindex_entry *ie;
8429 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8430 char *ondisk_path = NULL;
8431 char *id_str = NULL, *label_orig = NULL;
8432 int local_changes_subsumed;
8435 if (staged_status != GOT_STATUS_ADD &&
8436 staged_status != GOT_STATUS_MODIFY &&
8437 staged_status != GOT_STATUS_DELETE)
8440 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8442 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8444 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8446 return got_error_from_errno("asprintf");
8448 err = got_object_id_str(&id_str,
8449 commit_id ? commit_id : a->worktree->base_commit_id);
8452 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8454 err = got_error_from_errno("asprintf");
8458 switch (staged_status) {
8459 case GOT_STATUS_MODIFY:
8460 err = got_object_open_as_blob(&blob_base, a->repo,
8465 case GOT_STATUS_ADD:
8467 if (staged_status == GOT_STATUS_ADD) {
8468 int choice = GOT_PATCH_CHOICE_NONE;
8469 err = (*a->patch_cb)(&choice, a->patch_arg,
8470 staged_status, ie->path, NULL, 1, 1);
8473 if (choice != GOT_PATCH_CHOICE_YES)
8476 err = unstage_hunks(staged_blob_id,
8477 blob_base, blob_id, ie, ondisk_path,
8478 label_orig, a->worktree, a->repo,
8479 a->patch_cb, a->patch_arg,
8480 a->progress_cb, a->progress_arg);
8481 break; /* Done with this file. */
8484 err = got_object_open_as_blob(&blob_staged, a->repo,
8485 staged_blob_id, 8192);
8488 switch (got_fileindex_entry_staged_filetype_get(ie)) {
8489 case GOT_FILEIDX_MODE_BAD_SYMLINK:
8490 case GOT_FILEIDX_MODE_REGULAR_FILE:
8491 err = merge_blob(&local_changes_subsumed, a->worktree,
8492 blob_base, ondisk_path, relpath,
8493 got_fileindex_perms_to_st(ie), label_orig,
8494 blob_staged, commit_id ? commit_id :
8495 a->worktree->base_commit_id, a->repo,
8496 a->progress_cb, a->progress_arg);
8498 case GOT_FILEIDX_MODE_SYMLINK:
8499 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8500 char *staged_target;
8501 err = got_object_blob_read_to_str(
8502 &staged_target, blob_staged);
8505 err = merge_symlink(a->worktree, blob_base,
8506 ondisk_path, relpath, label_orig,
8507 staged_target, commit_id ? commit_id :
8508 a->worktree->base_commit_id,
8509 a->repo, a->progress_cb, a->progress_arg);
8510 free(staged_target);
8512 err = merge_blob(&local_changes_subsumed,
8513 a->worktree, blob_base, ondisk_path,
8514 relpath, got_fileindex_perms_to_st(ie),
8515 label_orig, blob_staged,
8516 commit_id ? commit_id :
8517 a->worktree->base_commit_id, a->repo,
8518 a->progress_cb, a->progress_arg);
8522 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8526 got_fileindex_entry_stage_set(ie,
8527 GOT_FILEIDX_STAGE_NONE);
8528 got_fileindex_entry_staged_filetype_set(ie, 0);
8531 case GOT_STATUS_DELETE:
8533 int choice = GOT_PATCH_CHOICE_NONE;
8534 err = (*a->patch_cb)(&choice, a->patch_arg,
8535 staged_status, ie->path, NULL, 1, 1);
8538 if (choice == GOT_PATCH_CHOICE_NO)
8540 if (choice != GOT_PATCH_CHOICE_YES) {
8541 err = got_error(GOT_ERR_PATCH_CHOICE);
8545 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8546 got_fileindex_entry_staged_filetype_set(ie, 0);
8547 err = get_file_status(&status, &sb, ie, ondisk_path,
8548 dirfd, de_name, a->repo);
8551 err = (*a->progress_cb)(a->progress_arg, status, relpath);
8557 got_object_blob_close(blob_base);
8559 got_object_blob_close(blob_staged);
8565 const struct got_error *
8566 got_worktree_unstage(struct got_worktree *worktree,
8567 struct got_pathlist_head *paths,
8568 got_worktree_checkout_cb progress_cb, void *progress_arg,
8569 got_worktree_patch_cb patch_cb, void *patch_arg,
8570 struct got_repository *repo)
8572 const struct got_error *err = NULL, *sync_err, *unlockerr;
8573 struct got_pathlist_entry *pe;
8574 struct got_fileindex *fileindex = NULL;
8575 char *fileindex_path = NULL;
8576 struct unstage_path_arg upa;
8578 err = lock_worktree(worktree, LOCK_EX);
8582 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8586 upa.worktree = worktree;
8587 upa.fileindex = fileindex;
8589 upa.progress_cb = progress_cb;
8590 upa.progress_arg = progress_arg;
8591 upa.patch_cb = patch_cb;
8592 upa.patch_arg = patch_arg;
8593 TAILQ_FOREACH(pe, paths, entry) {
8594 err = worktree_status(worktree, pe->path, fileindex, repo,
8595 unstage_path, &upa, NULL, NULL, 1, 0);
8600 sync_err = sync_fileindex(fileindex, fileindex_path);
8601 if (sync_err && err == NULL)
8604 free(fileindex_path);
8606 got_fileindex_free(fileindex);
8607 unlockerr = lock_worktree(worktree, LOCK_SH);
8608 if (unlockerr && err == NULL)
8613 struct report_file_info_arg {
8614 struct got_worktree *worktree;
8615 got_worktree_path_info_cb info_cb;
8617 struct got_pathlist_head *paths;
8618 got_cancel_cb cancel_cb;
8622 static const struct got_error *
8623 report_file_info(void *arg, struct got_fileindex_entry *ie)
8625 struct report_file_info_arg *a = arg;
8626 struct got_pathlist_entry *pe;
8627 struct got_object_id blob_id, staged_blob_id, commit_id;
8628 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8629 struct got_object_id *commit_idp = NULL;
8632 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8633 return got_error(GOT_ERR_CANCELLED);
8635 TAILQ_FOREACH(pe, a->paths, entry) {
8636 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8637 got_path_is_child(ie->path, pe->path, pe->path_len))
8640 if (pe == NULL) /* not found */
8643 if (got_fileindex_entry_has_blob(ie)) {
8644 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8645 blob_idp = &blob_id;
8647 stage = got_fileindex_entry_stage_get(ie);
8648 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8649 stage == GOT_FILEIDX_STAGE_ADD) {
8650 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8651 SHA1_DIGEST_LENGTH);
8652 staged_blob_idp = &staged_blob_id;
8655 if (got_fileindex_entry_has_commit(ie)) {
8656 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8657 commit_idp = &commit_id;
8660 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8661 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8664 const struct got_error *
8665 got_worktree_path_info(struct got_worktree *worktree,
8666 struct got_pathlist_head *paths,
8667 got_worktree_path_info_cb info_cb, void *info_arg,
8668 got_cancel_cb cancel_cb, void *cancel_arg)
8671 const struct got_error *err = NULL, *unlockerr;
8672 struct got_fileindex *fileindex = NULL;
8673 char *fileindex_path = NULL;
8674 struct report_file_info_arg arg;
8676 err = lock_worktree(worktree, LOCK_SH);
8680 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8684 arg.worktree = worktree;
8685 arg.info_cb = info_cb;
8686 arg.info_arg = info_arg;
8688 arg.cancel_cb = cancel_cb;
8689 arg.cancel_arg = cancel_arg;
8690 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8693 free(fileindex_path);
8695 got_fileindex_free(fileindex);
8696 unlockerr = lock_worktree(worktree, LOCK_UN);
8697 if (unlockerr && err == NULL)