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.
18 #include <sys/queue.h>
38 #include "got_error.h"
39 #include "got_repository.h"
40 #include "got_reference.h"
41 #include "got_object.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
45 #include "got_opentemp.h"
48 #include "got_lib_worktree.h"
49 #include "got_lib_sha1.h"
50 #include "got_lib_fileindex.h"
51 #include "got_lib_inflate.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_object_idset.h"
57 #include "got_lib_diff.h"
58 #include "got_lib_gotconfig.h"
61 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
64 #define GOT_MERGE_LABEL_MERGED "merged change"
65 #define GOT_MERGE_LABEL_BASE "3-way merge base"
67 static const struct got_error *
68 create_meta_file(const char *path_got, const char *name, const char *content)
70 const struct got_error *err = NULL;
73 if (asprintf(&path, "%s/%s", path_got, name) == -1)
74 return got_error_from_errno("asprintf");
76 err = got_path_create_file(path, content);
81 static const struct got_error *
82 update_meta_file(const char *path_got, const char *name, const char *content)
84 const struct got_error *err = NULL;
89 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
90 err = got_error_from_errno("asprintf");
95 err = got_opentemp_named(&tmppath, &tmpfile, path);
100 int len = fprintf(tmpfile, "%s\n", content);
101 if (len != strlen(content) + 1) {
102 err = got_error_from_errno2("fprintf", tmppath);
107 if (rename(tmppath, path) != 0) {
108 err = got_error_from_errno3("rename", tmppath, path);
114 if (fclose(tmpfile) == EOF && err == NULL)
115 err = got_error_from_errno2("fclose", tmppath);
120 static const struct got_error *
121 write_head_ref(const char *path_got, struct got_reference *head_ref)
123 const struct got_error *err = NULL;
126 if (got_ref_is_symbolic(head_ref)) {
127 refstr = got_ref_to_str(head_ref);
129 return got_error_from_errno("got_ref_to_str");
131 refstr = strdup(got_ref_get_name(head_ref));
133 return got_error_from_errno("strdup");
135 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
140 const struct got_error *
141 got_worktree_init(const char *path, struct got_reference *head_ref,
142 const char *prefix, struct got_repository *repo)
144 const struct got_error *err = NULL;
145 struct got_object_id *commit_id = NULL;
147 uint32_t uuid_status;
149 char *path_got = NULL;
150 char *formatstr = NULL;
151 char *absprefix = NULL;
152 char *basestr = NULL;
153 char *uuidstr = NULL;
155 if (strcmp(path, got_repo_get_path(repo)) == 0) {
156 err = got_error(GOT_ERR_WORKTREE_REPO);
160 err = got_ref_resolve(&commit_id, repo, head_ref);
163 err = got_object_get_type(&obj_type, repo, commit_id);
166 if (obj_type != GOT_OBJ_TYPE_COMMIT)
167 return got_error(GOT_ERR_OBJ_TYPE);
169 if (!got_path_is_absolute(prefix)) {
170 if (asprintf(&absprefix, "/%s", prefix) == -1)
171 return got_error_from_errno("asprintf");
174 /* Create top-level directory (may already exist). */
175 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
176 err = got_error_from_errno2("mkdir", path);
180 /* Create .got directory (may already exist). */
181 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
182 err = got_error_from_errno("asprintf");
185 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
186 err = got_error_from_errno2("mkdir", path_got);
190 /* Create an empty lock file. */
191 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
195 /* Create an empty file index. */
196 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
200 /* Write the HEAD reference. */
201 err = write_head_ref(path_got, head_ref);
205 /* Record our base commit. */
206 err = got_object_id_str(&basestr, commit_id);
209 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
213 /* Store path to repository. */
214 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
215 got_repo_get_path(repo));
219 /* Store in-repository path prefix. */
220 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
221 absprefix ? absprefix : prefix);
226 uuid_create(&uuid, &uuid_status);
227 if (uuid_status != uuid_s_ok) {
228 err = got_error_uuid(uuid_status, "uuid_create");
231 uuid_to_string(&uuid, &uuidstr, &uuid_status);
232 if (uuid_status != uuid_s_ok) {
233 err = got_error_uuid(uuid_status, "uuid_to_string");
236 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
240 /* Stamp work tree with format file. */
241 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
242 err = got_error_from_errno("asprintf");
245 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
259 const struct got_error *
260 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
261 const char *path_prefix)
263 char *absprefix = NULL;
265 if (!got_path_is_absolute(path_prefix)) {
266 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
267 return got_error_from_errno("asprintf");
269 *match = (strcmp(absprefix ? absprefix : path_prefix,
270 worktree->path_prefix) == 0);
276 got_worktree_get_head_ref_name(struct got_worktree *worktree)
278 return worktree->head_ref_name;
281 const struct got_error *
282 got_worktree_set_head_ref(struct got_worktree *worktree,
283 struct got_reference *head_ref)
285 const struct got_error *err = NULL;
286 char *path_got = NULL, *head_ref_name = NULL;
288 if (asprintf(&path_got, "%s/%s", worktree->root_path,
289 GOT_WORKTREE_GOT_DIR) == -1) {
290 err = got_error_from_errno("asprintf");
295 head_ref_name = strdup(got_ref_get_name(head_ref));
296 if (head_ref_name == NULL) {
297 err = got_error_from_errno("strdup");
301 err = write_head_ref(path_got, head_ref);
305 free(worktree->head_ref_name);
306 worktree->head_ref_name = head_ref_name;
314 struct got_object_id *
315 got_worktree_get_base_commit_id(struct got_worktree *worktree)
317 return worktree->base_commit_id;
320 const struct got_error *
321 got_worktree_set_base_commit_id(struct got_worktree *worktree,
322 struct got_repository *repo, struct got_object_id *commit_id)
324 const struct got_error *err;
325 struct got_object *obj = NULL;
327 char *path_got = NULL;
329 if (asprintf(&path_got, "%s/%s", worktree->root_path,
330 GOT_WORKTREE_GOT_DIR) == -1) {
331 err = got_error_from_errno("asprintf");
336 err = got_object_open(&obj, repo, commit_id);
340 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
341 err = got_error(GOT_ERR_OBJ_TYPE);
345 /* Record our base commit. */
346 err = got_object_id_str(&id_str, commit_id);
349 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
353 free(worktree->base_commit_id);
354 worktree->base_commit_id = got_object_id_dup(commit_id);
355 if (worktree->base_commit_id == NULL) {
356 err = got_error_from_errno("got_object_id_dup");
361 got_object_close(obj);
367 const struct got_gotconfig *
368 got_worktree_get_gotconfig(struct got_worktree *worktree)
370 return worktree->gotconfig;
373 static const struct got_error *
374 lock_worktree(struct got_worktree *worktree, int operation)
376 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
377 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
378 : got_error_from_errno2("flock",
379 got_worktree_get_root_path(worktree)));
383 static const struct got_error *
384 add_dir_on_disk(struct got_worktree *worktree, const char *path)
386 const struct got_error *err = NULL;
389 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
390 return got_error_from_errno("asprintf");
392 err = got_path_mkdir(abspath);
393 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
396 if (lstat(abspath, &sb) == -1) {
397 err = got_error_from_errno2("lstat", abspath);
398 } else if (!S_ISDIR(sb.st_mode)) {
399 /* TODO directory is obstructed; do something */
400 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
407 static const struct got_error *
408 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
410 const struct got_error *err = NULL;
413 size_t flen1 = 0, flen2 = 0;
418 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
419 if (flen1 == 0 && ferror(f1)) {
420 err = got_error_from_errno("fread");
423 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
424 if (flen2 == 0 && ferror(f2)) {
425 err = got_error_from_errno("fread");
432 } else if (flen2 == 0) {
436 } else if (flen1 == flen2) {
437 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
450 static const struct got_error *
451 check_files_equal(int *same, FILE *f1, FILE *f2)
458 if (fstat(fileno(f1), &sb) != 0)
459 return got_error_from_errno("fstat");
462 if (fstat(fileno(f2), &sb) != 0)
463 return got_error_from_errno("fstat");
466 if (size1 != size2) {
471 if (fseek(f1, 0L, SEEK_SET) == -1)
472 return got_ferror(f1, GOT_ERR_IO);
473 if (fseek(f2, 0L, SEEK_SET) == -1)
474 return got_ferror(f2, GOT_ERR_IO);
476 return check_file_contents_equal(same, f1, f2);
479 static const struct got_error *
480 copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
488 if (fseek(f, 0L, SEEK_SET) == -1)
489 return got_ferror(f, GOT_ERR_IO);
492 flen = fread(fbuf, 1, sizeof(fbuf), f);
495 return got_error_from_errno("fread");
499 outlen = write(outfd, fbuf, flen);
501 return got_error_from_errno("write");
503 return got_error(GOT_ERR_IO);
510 static const struct got_error *
511 merge_binary_file(int *overlapcnt, int merged_fd,
512 FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
513 const char *label_deriv, const char *label_orig, const char *label_deriv2,
514 const char *ondisk_path)
516 const struct got_error *err = NULL;
517 int same_content, changed_deriv, changed_deriv2;
518 int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
519 off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
520 char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
521 char *base_path_orig = NULL, *base_path_deriv = NULL;
522 char *base_path_deriv2 = NULL;
526 err = check_files_equal(&same_content, f_deriv, f_deriv2);
531 return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
533 err = check_files_equal(&same_content, f_deriv, f_orig);
536 changed_deriv = !same_content;
537 err = check_files_equal(&same_content, f_deriv2, f_orig);
540 changed_deriv2 = !same_content;
542 if (changed_deriv && changed_deriv2) {
544 if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
545 err = got_error_from_errno("asprintf");
548 if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
549 err = got_error_from_errno("asprintf");
552 if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
553 err = got_error_from_errno("asprintf");
556 err = got_opentemp_named_fd(&path_orig, &fd_orig,
560 err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
564 err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
568 err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
571 err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
574 err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
577 if (dprintf(merged_fd, "Binary files differ and cannot be "
578 "merged automatically:\n") < 0) {
579 err = got_error_from_errno("dprintf");
582 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
583 GOT_DIFF_CONFLICT_MARKER_BEGIN,
584 label_deriv ? " " : "",
585 label_deriv ? label_deriv : "",
587 err = got_error_from_errno("dprintf");
591 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
592 GOT_DIFF_CONFLICT_MARKER_ORIG,
593 label_orig ? " " : "",
594 label_orig ? label_orig : "",
596 err = got_error_from_errno("dprintf");
600 if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
601 GOT_DIFF_CONFLICT_MARKER_SEP,
603 GOT_DIFF_CONFLICT_MARKER_END,
604 label_deriv2 ? " " : "",
605 label_deriv2 ? label_deriv2 : "") < 0) {
606 err = got_error_from_errno("dprintf");
609 } else if (changed_deriv)
610 err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
611 else if (changed_deriv2)
612 err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
614 if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
616 err = got_error_from_errno2("unlink", path_orig);
617 if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
618 err = got_error_from_errno2("close", path_orig);
619 if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
620 err = got_error_from_errno2("close", path_deriv);
621 if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
622 err = got_error_from_errno2("close", path_deriv2);
626 free(base_path_orig);
627 free(base_path_deriv);
628 free(base_path_deriv2);
633 * Perform a 3-way merge where the file f_orig acts as the common
634 * ancestor, the file f_deriv acts as the first derived version,
635 * and the file f_deriv2 acts as the second derived version.
636 * The merge result will be written to a new file at ondisk_path; any
637 * existing file at this path will be replaced.
639 static const struct got_error *
640 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
641 FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
642 const char *path, uint16_t st_mode,
643 const char *label_orig, const char *label_deriv, const char *label_deriv2,
644 enum got_diff_algorithm diff_algo, struct got_repository *repo,
645 got_worktree_checkout_cb progress_cb, void *progress_arg)
647 const struct got_error *err = NULL;
649 FILE *f_merged = NULL;
650 char *merged_path = NULL, *base_path = NULL;
654 *local_changes_subsumed = 0;
656 err = got_path_dirname(&parent, ondisk_path);
660 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
661 err = got_error_from_errno("asprintf");
665 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
669 err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
670 f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
672 if (err->code != GOT_ERR_FILE_BINARY)
674 err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
675 f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
681 err = (*progress_cb)(progress_arg,
682 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
686 if (fsync(merged_fd) != 0) {
687 err = got_error_from_errno("fsync");
691 f_merged = fdopen(merged_fd, "r");
692 if (f_merged == NULL) {
693 err = got_error_from_errno("fdopen");
698 /* Check if a clean merge has subsumed all local changes. */
699 if (overlapcnt == 0) {
700 err = check_files_equal(local_changes_subsumed, f_deriv,
706 if (fchmod(fileno(f_merged), st_mode) != 0) {
707 err = got_error_from_errno2("fchmod", merged_path);
711 if (rename(merged_path, ondisk_path) != 0) {
712 err = got_error_from_errno3("rename", merged_path,
721 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
722 err = got_error_from_errno("close");
723 if (f_merged && fclose(f_merged) == EOF && err == NULL)
724 err = got_error_from_errno("fclose");
731 static const struct got_error *
732 update_symlink(const char *ondisk_path, const char *target_path,
735 /* This is not atomic but matches what 'ln -sf' does. */
736 if (unlink(ondisk_path) == -1)
737 return got_error_from_errno2("unlink", ondisk_path);
738 if (symlink(target_path, ondisk_path) == -1)
739 return got_error_from_errno3("symlink", target_path,
745 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
746 * in the work tree with a file that contains conflict markers and the
747 * conflicting target paths of the original version, a "derived version"
748 * of a symlink from an incoming change, and a local version of the symlink.
750 * The original versions's target path can be NULL if it is not available,
751 * such as if both derived versions added a new symlink at the same path.
753 * The incoming derived symlink target is NULL in case the incoming change
754 * has deleted this symlink.
756 static const struct got_error *
757 install_symlink_conflict(const char *deriv_target,
758 struct got_object_id *deriv_base_commit_id, const char *orig_target,
759 const char *label_orig, const char *local_target, const char *ondisk_path)
761 const struct got_error *err;
762 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
765 err = got_object_id_str(&id_str, deriv_base_commit_id);
767 return got_error_from_errno("asprintf");
769 if (asprintf(&label_deriv, "%s: commit %s",
770 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
771 err = got_error_from_errno("asprintf");
775 err = got_opentemp_named(&path, &f, "got-symlink-conflict");
779 if (fchmod(fileno(f), GOT_DEFAULT_FILE_MODE) == -1) {
780 err = got_error_from_errno2("fchmod", path);
784 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
785 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
786 deriv_target ? deriv_target : "(symlink was deleted)",
787 orig_target ? label_orig : "",
788 orig_target ? "\n" : "",
789 orig_target ? orig_target : "",
790 orig_target ? "\n" : "",
791 GOT_DIFF_CONFLICT_MARKER_SEP,
792 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
793 err = got_error_from_errno2("fprintf", path);
797 if (unlink(ondisk_path) == -1) {
798 err = got_error_from_errno2("unlink", ondisk_path);
801 if (rename(path, ondisk_path) == -1) {
802 err = got_error_from_errno3("rename", path, ondisk_path);
806 if (f != NULL && fclose(f) == EOF && err == NULL)
807 err = got_error_from_errno2("fclose", path);
814 /* forward declaration */
815 static const struct got_error *
816 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
817 const char *, const char *, uint16_t, const char *,
818 struct got_blob_object *, struct got_object_id *,
819 struct got_repository *, got_worktree_checkout_cb, void *);
822 * Merge a symlink into the work tree, where blob_orig acts as the common
823 * ancestor, deriv_target is the link target of the first derived version,
824 * and the symlink on disk acts as the second derived version.
825 * Assume that contents of both blobs represent symlinks.
827 static const struct got_error *
828 merge_symlink(struct got_worktree *worktree,
829 struct got_blob_object *blob_orig, const char *ondisk_path,
830 const char *path, const char *label_orig, const char *deriv_target,
831 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
832 got_worktree_checkout_cb progress_cb, void *progress_arg)
834 const struct got_error *err = NULL;
835 char *ancestor_target = NULL;
837 ssize_t ondisk_len, deriv_len;
838 char ondisk_target[PATH_MAX];
839 int have_local_change = 0;
840 int have_incoming_change = 0;
842 if (lstat(ondisk_path, &sb) == -1)
843 return got_error_from_errno2("lstat", ondisk_path);
845 ondisk_len = readlink(ondisk_path, ondisk_target,
846 sizeof(ondisk_target));
847 if (ondisk_len == -1) {
848 err = got_error_from_errno2("readlink",
852 ondisk_target[ondisk_len] = '\0';
855 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
860 if (ancestor_target == NULL ||
861 (ondisk_len != strlen(ancestor_target) ||
862 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
863 have_local_change = 1;
865 deriv_len = strlen(deriv_target);
866 if (ancestor_target == NULL ||
867 (deriv_len != strlen(ancestor_target) ||
868 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
869 have_incoming_change = 1;
871 if (!have_local_change && !have_incoming_change) {
872 if (ancestor_target) {
873 /* Both sides made the same change. */
874 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
876 } else if (deriv_len == ondisk_len &&
877 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
878 /* Both sides added the same symlink. */
879 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
882 /* Both sides added symlinks which don't match. */
883 err = install_symlink_conflict(deriv_target,
884 deriv_base_commit_id, ancestor_target,
885 label_orig, ondisk_target, ondisk_path);
888 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
891 } else if (!have_local_change && have_incoming_change) {
892 /* Apply the incoming change. */
893 err = update_symlink(ondisk_path, deriv_target,
894 strlen(deriv_target));
897 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
898 } else if (have_local_change && have_incoming_change) {
899 if (deriv_len == ondisk_len &&
900 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
901 /* Both sides made the same change. */
902 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
905 err = install_symlink_conflict(deriv_target,
906 deriv_base_commit_id, ancestor_target, label_orig,
907 ondisk_target, ondisk_path);
910 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
916 free(ancestor_target);
920 static const struct got_error *
921 dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
923 const struct got_error *err = NULL;
924 char target_path[PATH_MAX];
933 return got_error_from_errno("got_opentemp");
934 target_len = readlink(ondisk_path, target_path, sizeof(target_path));
935 if (target_len == -1) {
936 err = got_error_from_errno2("readlink", ondisk_path);
939 n = fwrite(target_path, 1, target_len, f);
940 if (n != target_len) {
941 err = got_ferror(f, GOT_ERR_IO);
944 if (fflush(f) == EOF) {
945 err = got_error_from_errno("fflush");
948 if (fseek(f, 0L, SEEK_SET) == -1) {
949 err = got_ferror(f, GOT_ERR_IO);
961 * Perform a 3-way merge where blob_orig acts as the common ancestor,
962 * blob_deriv acts as the first derived version, and the file on disk
963 * acts as the second derived version.
965 static const struct got_error *
966 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
967 struct got_blob_object *blob_orig, const char *ondisk_path,
968 const char *path, uint16_t st_mode, const char *label_orig,
969 struct got_blob_object *blob_deriv,
970 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
971 got_worktree_checkout_cb progress_cb, void *progress_arg)
973 const struct got_error *err = NULL;
974 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
975 char *blob_orig_path = NULL;
976 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
977 char *label_deriv = NULL, *parent = NULL;
979 *local_changes_subsumed = 0;
981 err = got_path_dirname(&parent, ondisk_path);
986 if (asprintf(&base_path, "%s/got-merge-blob-orig",
988 err = got_error_from_errno("asprintf");
993 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
996 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1003 * No common ancestor exists. This is an "add vs add" conflict
1004 * and we simply use an empty ancestor file to make both files
1005 * appear in the merged result in their entirety.
1007 f_orig = got_opentemp();
1008 if (f_orig == NULL) {
1009 err = got_error_from_errno("got_opentemp");
1014 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1015 err = got_error_from_errno("asprintf");
1020 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1023 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1028 err = got_object_id_str(&id_str, deriv_base_commit_id);
1031 if (asprintf(&label_deriv, "%s: commit %s",
1032 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1033 err = got_error_from_errno("asprintf");
1038 * In order the run a 3-way merge with a symlink we copy the symlink's
1039 * target path into a temporary file and use that file with diff3.
1041 if (S_ISLNK(st_mode)) {
1042 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1047 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1049 err = got_error_from_errno2("open", ondisk_path);
1052 f_deriv2 = fdopen(fd, "r");
1053 if (f_deriv2 == NULL) {
1054 err = got_error_from_errno2("fdopen", ondisk_path);
1060 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1061 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1062 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1064 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1065 err = got_error_from_errno("fclose");
1066 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1067 err = got_error_from_errno("fclose");
1068 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1069 err = got_error_from_errno("fclose");
1071 if (blob_orig_path) {
1072 unlink(blob_orig_path);
1073 free(blob_orig_path);
1075 if (blob_deriv_path) {
1076 unlink(blob_deriv_path);
1077 free(blob_deriv_path);
1085 static const struct got_error *
1086 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1087 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1088 int wt_fd, const char *path, struct got_object_id *blob_id)
1090 const struct got_error *err = NULL;
1091 struct got_fileindex_entry *new_ie;
1095 err = got_fileindex_entry_alloc(&new_ie, path);
1099 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1100 blob_id->sha1, base_commit_id->sha1, 1);
1104 err = got_fileindex_entry_add(fileindex, new_ie);
1107 got_fileindex_entry_free(new_ie);
1114 get_ondisk_perms(int executable, mode_t st_mode)
1116 mode_t xbits = S_IXUSR;
1119 /* Map read bits to execute bits. */
1120 if (st_mode & S_IRGRP)
1122 if (st_mode & S_IROTH)
1124 return st_mode | xbits;
1127 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1130 /* forward declaration */
1131 static const struct got_error *
1132 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1133 const char *path, mode_t te_mode, mode_t st_mode,
1134 struct got_blob_object *blob, int restoring_missing_file,
1135 int reverting_versioned_file, int installing_bad_symlink,
1136 int path_is_unversioned, struct got_repository *repo,
1137 got_worktree_checkout_cb progress_cb, void *progress_arg);
1140 * This function assumes that the provided symlink target points at a
1141 * safe location in the work tree!
1143 static const struct got_error *
1144 replace_existing_symlink(int *did_something, const char *ondisk_path,
1145 const char *target_path, size_t target_len)
1147 const struct got_error *err = NULL;
1149 char etarget[PATH_MAX];
1155 * "Bad" symlinks (those pointing outside the work tree or into the
1156 * .got directory) are installed in the work tree as a regular file
1157 * which contains the bad symlink target path.
1158 * The new symlink target has already been checked for safety by our
1159 * caller. If we can successfully open a regular file then we simply
1160 * replace this file with a symlink below.
1162 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1164 if (!got_err_open_nofollow_on_symlink())
1165 return got_error_from_errno2("open", ondisk_path);
1167 /* We are updating an existing on-disk symlink. */
1168 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1170 return got_error_from_errno2("readlink", ondisk_path);
1172 if (elen == target_len &&
1173 memcmp(etarget, target_path, target_len) == 0)
1174 return NULL; /* nothing to do */
1178 err = update_symlink(ondisk_path, target_path, target_len);
1179 if (fd != -1 && close(fd) == -1 && err == NULL)
1180 err = got_error_from_errno2("close", ondisk_path);
1184 static const struct got_error *
1185 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1186 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1188 const struct got_error *err = NULL;
1189 char canonpath[PATH_MAX];
1190 char *path_got = NULL;
1192 *is_bad_symlink = 0;
1194 if (target_len >= sizeof(canonpath)) {
1195 *is_bad_symlink = 1;
1200 * We do not use realpath(3) to resolve the symlink's target
1201 * path because we don't want to resolve symlinks recursively.
1202 * Instead we make the path absolute and then canonicalize it.
1203 * Relative symlink target lookup should begin at the directory
1204 * in which the blob object is being installed.
1206 if (!got_path_is_absolute(target_path)) {
1207 char *abspath, *parent;
1208 err = got_path_dirname(&parent, ondisk_path);
1211 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1213 return got_error_from_errno("asprintf");
1216 if (strlen(abspath) >= sizeof(canonpath)) {
1217 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1221 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1226 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1231 /* Only allow symlinks pointing at paths within the work tree. */
1232 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1233 *is_bad_symlink = 1;
1237 /* Do not allow symlinks pointing into the .got directory. */
1238 if (asprintf(&path_got, "%s/%s", wtroot_path,
1239 GOT_WORKTREE_GOT_DIR) == -1)
1240 return got_error_from_errno("asprintf");
1241 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1242 *is_bad_symlink = 1;
1248 static const struct got_error *
1249 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1250 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1251 int restoring_missing_file, int reverting_versioned_file,
1252 int path_is_unversioned, int allow_bad_symlinks,
1253 struct got_repository *repo,
1254 got_worktree_checkout_cb progress_cb, void *progress_arg)
1256 const struct got_error *err = NULL;
1257 char target_path[PATH_MAX];
1258 size_t len, target_len = 0;
1259 char *path_got = NULL;
1260 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1261 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1263 *is_bad_symlink = 0;
1266 * Blob object content specifies the target path of the link.
1267 * If a symbolic link cannot be installed we instead create
1268 * a regular file which contains the link target path stored
1269 * in the blob object.
1272 err = got_object_blob_read_block(&len, blob);
1273 if (len + target_len >= sizeof(target_path)) {
1274 /* Path too long; install as a regular file. */
1275 *is_bad_symlink = 1;
1276 got_object_blob_rewind(blob);
1277 return install_blob(worktree, ondisk_path, path,
1278 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1279 restoring_missing_file, reverting_versioned_file,
1280 1, path_is_unversioned, repo, progress_cb,
1284 /* Skip blob object header first time around. */
1285 memcpy(target_path + target_len, buf + hdrlen,
1287 target_len += len - hdrlen;
1291 target_path[target_len] = '\0';
1293 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1294 ondisk_path, worktree->root_path);
1298 if (*is_bad_symlink && !allow_bad_symlinks) {
1299 /* install as a regular file */
1300 got_object_blob_rewind(blob);
1301 err = install_blob(worktree, ondisk_path, path,
1302 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1303 restoring_missing_file, reverting_versioned_file, 1,
1304 path_is_unversioned, repo, progress_cb, progress_arg);
1308 if (symlink(target_path, ondisk_path) == -1) {
1309 if (errno == EEXIST) {
1310 int symlink_replaced;
1311 if (path_is_unversioned) {
1312 err = (*progress_cb)(progress_arg,
1313 GOT_STATUS_UNVERSIONED, path);
1316 err = replace_existing_symlink(&symlink_replaced,
1317 ondisk_path, target_path, target_len);
1321 if (symlink_replaced) {
1322 err = (*progress_cb)(progress_arg,
1323 reverting_versioned_file ?
1325 GOT_STATUS_UPDATE, path);
1327 err = (*progress_cb)(progress_arg,
1328 GOT_STATUS_EXISTS, path);
1331 goto done; /* Nothing else to do. */
1334 if (errno == ENOENT) {
1336 err = got_path_dirname(&parent, ondisk_path);
1339 err = add_dir_on_disk(worktree, parent);
1344 * Retry, and fall through to error handling
1345 * below if this second attempt fails.
1347 if (symlink(target_path, ondisk_path) != -1) {
1348 err = NULL; /* success */
1353 /* Handle errors from first or second creation attempt. */
1354 if (errno == ENAMETOOLONG) {
1355 /* bad target path; install as a regular file */
1356 *is_bad_symlink = 1;
1357 got_object_blob_rewind(blob);
1358 err = install_blob(worktree, ondisk_path, path,
1359 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1360 restoring_missing_file, reverting_versioned_file, 1,
1361 path_is_unversioned, repo,
1362 progress_cb, progress_arg);
1363 } else if (errno == ENOTDIR) {
1364 err = got_error_path(ondisk_path,
1365 GOT_ERR_FILE_OBSTRUCTED);
1367 err = got_error_from_errno3("symlink",
1368 target_path, ondisk_path);
1370 } else if (progress_cb)
1371 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1372 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1378 static const struct got_error *
1379 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1380 const char *path, mode_t te_mode, mode_t st_mode,
1381 struct got_blob_object *blob, int restoring_missing_file,
1382 int reverting_versioned_file, int installing_bad_symlink,
1383 int path_is_unversioned, struct got_repository *repo,
1384 got_worktree_checkout_cb progress_cb, void *progress_arg)
1386 const struct got_error *err = NULL;
1390 char *tmppath = NULL;
1392 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1393 O_CLOEXEC, GOT_DEFAULT_FILE_MODE);
1395 if (errno == ENOENT) {
1397 err = got_path_dirname(&parent, path);
1400 err = add_dir_on_disk(worktree, parent);
1404 fd = open(ondisk_path,
1405 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1406 GOT_DEFAULT_FILE_MODE);
1408 return got_error_from_errno2("open",
1410 } else if (errno == EEXIST) {
1411 if (path_is_unversioned) {
1412 err = (*progress_cb)(progress_arg,
1413 GOT_STATUS_UNVERSIONED, path);
1416 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1417 !S_ISREG(st_mode) && !installing_bad_symlink) {
1418 /* TODO file is obstructed; do something */
1419 err = got_error_path(ondisk_path,
1420 GOT_ERR_FILE_OBSTRUCTED);
1423 err = got_opentemp_named_fd(&tmppath, &fd,
1430 return got_error_from_errno2("open", ondisk_path);
1433 if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1434 err = got_error_from_errno2("fchmod",
1435 update ? tmppath : ondisk_path);
1440 if (restoring_missing_file)
1441 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1443 else if (reverting_versioned_file)
1444 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1447 err = (*progress_cb)(progress_arg,
1448 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1453 hdrlen = got_object_blob_get_hdrlen(blob);
1455 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1456 err = got_object_blob_read_block(&len, blob);
1460 /* Skip blob object header first time around. */
1461 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1463 err = got_error_from_errno("write");
1465 } else if (outlen != len - hdrlen) {
1466 err = got_error(GOT_ERR_IO);
1473 if (fsync(fd) != 0) {
1474 err = got_error_from_errno("fsync");
1479 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1480 err = got_error_from_errno2("unlink", ondisk_path);
1483 if (rename(tmppath, ondisk_path) != 0) {
1484 err = got_error_from_errno3("rename", tmppath,
1493 if (fd != -1 && close(fd) == -1 && err == NULL)
1494 err = got_error_from_errno("close");
1495 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1496 err = got_error_from_errno2("unlink", tmppath);
1501 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1502 static const struct got_error *
1503 get_modified_file_content_status(unsigned char *status, FILE *f)
1505 const struct got_error *err = NULL;
1506 const char *markers[3] = {
1507 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1508 GOT_DIFF_CONFLICT_MARKER_SEP,
1509 GOT_DIFF_CONFLICT_MARKER_END
1513 size_t linesize = 0;
1516 while (*status == GOT_STATUS_MODIFY) {
1517 linelen = getline(&line, &linesize, f);
1518 if (linelen == -1) {
1521 err = got_ferror(f, GOT_ERR_IO);
1525 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1526 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1528 *status = GOT_STATUS_CONFLICT;
1539 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1541 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1542 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1546 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1548 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1549 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1550 ie->mtime_sec == sb->st_mtim.tv_sec &&
1551 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1552 ie->size == (sb->st_size & 0xffffffff) &&
1553 !xbit_differs(ie, sb->st_mode));
1556 static unsigned char
1557 get_staged_status(struct got_fileindex_entry *ie)
1559 switch (got_fileindex_entry_stage_get(ie)) {
1560 case GOT_FILEIDX_STAGE_ADD:
1561 return GOT_STATUS_ADD;
1562 case GOT_FILEIDX_STAGE_DELETE:
1563 return GOT_STATUS_DELETE;
1564 case GOT_FILEIDX_STAGE_MODIFY:
1565 return GOT_STATUS_MODIFY;
1567 return GOT_STATUS_NO_CHANGE;
1571 static const struct got_error *
1572 get_symlink_modification_status(unsigned char *status,
1573 struct got_fileindex_entry *ie, const char *abspath,
1574 int dirfd, const char *de_name, struct got_blob_object *blob)
1576 const struct got_error *err = NULL;
1577 char target_path[PATH_MAX];
1578 char etarget[PATH_MAX];
1580 size_t len, target_len = 0;
1581 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1582 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1584 *status = GOT_STATUS_NO_CHANGE;
1586 /* Blob object content specifies the target path of the link. */
1588 err = got_object_blob_read_block(&len, blob);
1591 if (len + target_len >= sizeof(target_path)) {
1593 * Should not happen. The blob contents were OK
1594 * when this symlink was installed.
1596 return got_error(GOT_ERR_NO_SPACE);
1599 /* Skip blob object header first time around. */
1600 memcpy(target_path + target_len, buf + hdrlen,
1602 target_len += len - hdrlen;
1606 target_path[target_len] = '\0';
1609 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1611 return got_error_from_errno2("readlinkat", abspath);
1613 elen = readlink(abspath, etarget, sizeof(etarget));
1615 return got_error_from_errno2("readlink", abspath);
1618 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1619 *status = GOT_STATUS_MODIFY;
1624 static const struct got_error *
1625 get_file_status(unsigned char *status, struct stat *sb,
1626 struct got_fileindex_entry *ie, const char *abspath,
1627 int dirfd, const char *de_name, struct got_repository *repo)
1629 const struct got_error *err = NULL;
1630 struct got_object_id id;
1635 struct got_blob_object *blob = NULL;
1637 unsigned char staged_status = get_staged_status(ie);
1639 *status = GOT_STATUS_NO_CHANGE;
1640 memset(sb, 0, sizeof(*sb));
1643 * Whenever the caller provides a directory descriptor and a
1644 * directory entry name for the file, use them! This prevents
1645 * race conditions if filesystem paths change beneath our feet.
1648 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1649 if (errno == ENOENT) {
1650 if (got_fileindex_entry_has_file_on_disk(ie))
1651 *status = GOT_STATUS_MISSING;
1653 *status = GOT_STATUS_DELETE;
1656 err = got_error_from_errno2("fstatat", abspath);
1660 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1661 if (fd == -1 && errno != ENOENT &&
1662 !got_err_open_nofollow_on_symlink())
1663 return got_error_from_errno2("open", abspath);
1664 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1665 if (lstat(abspath, sb) == -1)
1666 return got_error_from_errno2("lstat", abspath);
1667 } else if (fd == -1 || fstat(fd, sb) == -1) {
1668 if (errno == ENOENT) {
1669 if (got_fileindex_entry_has_file_on_disk(ie))
1670 *status = GOT_STATUS_MISSING;
1672 *status = GOT_STATUS_DELETE;
1675 err = got_error_from_errno2("fstat", abspath);
1680 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1681 *status = GOT_STATUS_OBSTRUCTED;
1685 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1686 *status = GOT_STATUS_DELETE;
1688 } else if (!got_fileindex_entry_has_blob(ie) &&
1689 staged_status != GOT_STATUS_ADD) {
1690 *status = GOT_STATUS_ADD;
1694 if (!stat_info_differs(ie, sb))
1697 if (S_ISLNK(sb->st_mode) &&
1698 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1699 *status = GOT_STATUS_MODIFY;
1703 if (staged_status == GOT_STATUS_MODIFY ||
1704 staged_status == GOT_STATUS_ADD)
1705 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1707 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1709 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1713 if (S_ISLNK(sb->st_mode)) {
1714 err = get_symlink_modification_status(status, ie,
1715 abspath, dirfd, de_name, blob);
1720 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1722 err = got_error_from_errno2("openat", abspath);
1727 f = fdopen(fd, "r");
1729 err = got_error_from_errno2("fdopen", abspath);
1733 hdrlen = got_object_blob_get_hdrlen(blob);
1735 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1736 err = got_object_blob_read_block(&blen, blob);
1739 /* Skip length of blob object header first time around. */
1740 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1741 if (flen == 0 && ferror(f)) {
1742 err = got_error_from_errno("fread");
1745 if (blen - hdrlen == 0) {
1747 *status = GOT_STATUS_MODIFY;
1749 } else if (flen == 0) {
1750 if (blen - hdrlen != 0)
1751 *status = GOT_STATUS_MODIFY;
1753 } else if (blen - hdrlen == flen) {
1754 /* Skip blob object header first time around. */
1755 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1756 *status = GOT_STATUS_MODIFY;
1760 *status = GOT_STATUS_MODIFY;
1766 if (*status == GOT_STATUS_MODIFY) {
1768 err = get_modified_file_content_status(status, f);
1769 } else if (xbit_differs(ie, sb->st_mode))
1770 *status = GOT_STATUS_MODE_CHANGE;
1773 got_object_blob_close(blob);
1774 if (f != NULL && fclose(f) == EOF && err == NULL)
1775 err = got_error_from_errno2("fclose", abspath);
1776 if (fd != -1 && close(fd) == -1 && err == NULL)
1777 err = got_error_from_errno2("close", abspath);
1782 * Update timestamps in the file index if a file is unmodified and
1783 * we had to run a full content comparison to find out.
1785 static const struct got_error *
1786 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1787 struct got_fileindex_entry *ie, struct stat *sb)
1789 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1790 return got_fileindex_entry_update(ie, wt_fd, path,
1791 ie->blob_sha1, ie->commit_sha1, 1);
1796 static const struct got_error *
1797 update_blob(struct got_worktree *worktree,
1798 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1799 struct got_tree_entry *te, const char *path,
1800 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1803 const struct got_error *err = NULL;
1804 struct got_blob_object *blob = NULL;
1806 unsigned char status = GOT_STATUS_NO_CHANGE;
1809 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1810 return got_error_from_errno("asprintf");
1813 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1814 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1817 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1821 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1822 sb.st_mode = got_fileindex_perms_to_st(ie);
1824 if (stat(ondisk_path, &sb) == -1) {
1825 if (errno != ENOENT) {
1826 err = got_error_from_errno2("stat",
1830 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1831 status = GOT_STATUS_UNVERSIONED;
1833 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1834 status = GOT_STATUS_UNVERSIONED;
1836 status = GOT_STATUS_OBSTRUCTED;
1840 if (status == GOT_STATUS_OBSTRUCTED) {
1842 got_fileindex_entry_mark_skipped(ie);
1843 err = (*progress_cb)(progress_arg, status, path);
1846 if (status == GOT_STATUS_CONFLICT) {
1848 got_fileindex_entry_mark_skipped(ie);
1849 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1854 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1855 (S_ISLNK(te->mode) ||
1856 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1858 * This is a regular file or an installed bad symlink.
1859 * If the file index indicates that this file is already
1860 * up-to-date with respect to the repository we can skip
1861 * updating contents of this file.
1863 if (got_fileindex_entry_has_commit(ie) &&
1864 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1865 SHA1_DIGEST_LENGTH) == 0) {
1867 err = sync_timestamps(worktree->root_fd,
1868 path, status, ie, &sb);
1871 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1875 if (got_fileindex_entry_has_blob(ie) &&
1876 memcmp(ie->blob_sha1, te->id.sha1,
1877 SHA1_DIGEST_LENGTH) == 0) {
1878 /* Different commit but the same blob. */
1879 err = sync_timestamps(worktree->root_fd,
1880 path, status, ie, &sb);
1883 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1889 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1893 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1894 int update_timestamps;
1895 struct got_blob_object *blob2 = NULL;
1896 char *label_orig = NULL;
1897 if (got_fileindex_entry_has_blob(ie)) {
1898 struct got_object_id id2;
1899 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1900 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1904 if (got_fileindex_entry_has_commit(ie)) {
1905 char id_str[SHA1_DIGEST_STRING_LENGTH];
1906 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1907 sizeof(id_str)) == NULL) {
1908 err = got_error_path(id_str,
1909 GOT_ERR_BAD_OBJ_ID_STR);
1912 if (asprintf(&label_orig, "%s: commit %s",
1913 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1914 err = got_error_from_errno("asprintf");
1918 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1920 err = got_object_blob_read_to_str(&link_target, blob);
1923 err = merge_symlink(worktree, blob2, ondisk_path, path,
1924 label_orig, link_target, worktree->base_commit_id,
1925 repo, progress_cb, progress_arg);
1928 err = merge_blob(&update_timestamps, worktree, blob2,
1929 ondisk_path, path, sb.st_mode, label_orig, blob,
1930 worktree->base_commit_id, repo,
1931 progress_cb, progress_arg);
1935 got_object_blob_close(blob2);
1939 * Do not update timestamps of files with local changes.
1940 * Otherwise, a future status walk would treat them as
1941 * unmodified files again.
1943 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1944 blob->id.sha1, worktree->base_commit_id->sha1,
1946 } else if (status == GOT_STATUS_MODE_CHANGE) {
1947 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1948 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1949 } else if (status == GOT_STATUS_DELETE) {
1950 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1953 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1954 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1958 int is_bad_symlink = 0;
1959 if (S_ISLNK(te->mode)) {
1960 err = install_symlink(&is_bad_symlink, worktree,
1961 ondisk_path, path, blob,
1962 status == GOT_STATUS_MISSING, 0,
1963 status == GOT_STATUS_UNVERSIONED, 0,
1964 repo, progress_cb, progress_arg);
1966 err = install_blob(worktree, ondisk_path, path,
1967 te->mode, sb.st_mode, blob,
1968 status == GOT_STATUS_MISSING, 0, 0,
1969 status == GOT_STATUS_UNVERSIONED, repo,
1970 progress_cb, progress_arg);
1976 err = got_fileindex_entry_update(ie,
1977 worktree->root_fd, path, blob->id.sha1,
1978 worktree->base_commit_id->sha1, 1);
1980 err = create_fileindex_entry(&ie, fileindex,
1981 worktree->base_commit_id, worktree->root_fd, path,
1987 if (is_bad_symlink) {
1988 got_fileindex_entry_filetype_set(ie,
1989 GOT_FILEIDX_MODE_BAD_SYMLINK);
1992 got_object_blob_close(blob);
1998 static const struct got_error *
1999 remove_ondisk_file(const char *root_path, const char *path)
2001 const struct got_error *err = NULL;
2002 char *ondisk_path = NULL, *parent = NULL;
2004 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2005 return got_error_from_errno("asprintf");
2007 if (unlink(ondisk_path) == -1) {
2008 if (errno != ENOENT)
2009 err = got_error_from_errno2("unlink", ondisk_path);
2011 size_t root_len = strlen(root_path);
2012 err = got_path_dirname(&parent, ondisk_path);
2015 while (got_path_cmp(parent, root_path,
2016 strlen(parent), root_len) != 0) {
2018 ondisk_path = parent;
2020 if (rmdir(ondisk_path) == -1) {
2021 if (errno != ENOTEMPTY)
2022 err = got_error_from_errno2("rmdir",
2026 err = got_path_dirname(&parent, ondisk_path);
2037 static const struct got_error *
2038 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2039 struct got_fileindex_entry *ie, struct got_repository *repo,
2040 got_worktree_checkout_cb progress_cb, void *progress_arg)
2042 const struct got_error *err = NULL;
2043 unsigned char status;
2047 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2048 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2050 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2052 return got_error_from_errno("asprintf");
2054 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2058 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2059 char ondisk_target[PATH_MAX];
2060 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2061 sizeof(ondisk_target));
2062 if (ondisk_len == -1) {
2063 err = got_error_from_errno2("readlink", ondisk_path);
2066 ondisk_target[ondisk_len] = '\0';
2067 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2068 NULL, NULL, /* XXX pass common ancestor info? */
2069 ondisk_target, ondisk_path);
2072 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2077 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2078 status == GOT_STATUS_ADD) {
2079 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2083 * Preserve the working file and change the deleted blob's
2084 * entry into a schedule-add entry.
2086 err = got_fileindex_entry_update(ie, worktree->root_fd,
2087 ie->path, NULL, NULL, 0);
2089 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2092 if (status == GOT_STATUS_NO_CHANGE) {
2093 err = remove_ondisk_file(worktree->root_path, ie->path);
2097 got_fileindex_entry_remove(fileindex, ie);
2104 struct diff_cb_arg {
2105 struct got_fileindex *fileindex;
2106 struct got_worktree *worktree;
2107 struct got_repository *repo;
2108 got_worktree_checkout_cb progress_cb;
2110 got_cancel_cb cancel_cb;
2114 static const struct got_error *
2115 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2116 struct got_tree_entry *te, const char *parent_path)
2118 struct diff_cb_arg *a = arg;
2120 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2121 return got_error(GOT_ERR_CANCELLED);
2123 return update_blob(a->worktree, a->fileindex, ie, te,
2124 ie->path, a->repo, a->progress_cb, a->progress_arg);
2127 static const struct got_error *
2128 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2130 struct diff_cb_arg *a = arg;
2132 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2133 return got_error(GOT_ERR_CANCELLED);
2135 return delete_blob(a->worktree, a->fileindex, ie,
2136 a->repo, a->progress_cb, a->progress_arg);
2139 static const struct got_error *
2140 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2142 struct diff_cb_arg *a = arg;
2143 const struct got_error *err;
2146 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2147 return got_error(GOT_ERR_CANCELLED);
2149 if (got_object_tree_entry_is_submodule(te))
2152 if (asprintf(&path, "%s%s%s", parent_path,
2153 parent_path[0] ? "/" : "", te->name)
2155 return got_error_from_errno("asprintf");
2157 if (S_ISDIR(te->mode))
2158 err = add_dir_on_disk(a->worktree, path);
2160 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2161 a->repo, a->progress_cb, a->progress_arg);
2167 const struct got_error *
2168 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2170 uint32_t uuid_status;
2172 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2173 if (uuid_status != uuid_s_ok) {
2175 return got_error_uuid(uuid_status, "uuid_to_string");
2181 static const struct got_error *
2182 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2184 const struct got_error *err = NULL;
2185 char *uuidstr = NULL;
2189 err = got_worktree_get_uuid(&uuidstr, worktree);
2193 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2194 err = got_error_from_errno("asprintf");
2201 const struct got_error *
2202 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2204 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2207 static const struct got_error *
2208 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2210 return get_ref_name(refname, worktree,
2211 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2214 static const struct got_error *
2215 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2217 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2220 static const struct got_error *
2221 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2223 return get_ref_name(refname, worktree,
2224 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2227 static const struct got_error *
2228 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2230 return get_ref_name(refname, worktree,
2231 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2234 static const struct got_error *
2235 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2237 return get_ref_name(refname, worktree,
2238 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2241 static const struct got_error *
2242 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2244 return get_ref_name(refname, worktree,
2245 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2248 static const struct got_error *
2249 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2251 return get_ref_name(refname, worktree,
2252 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2255 static const struct got_error *
2256 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2258 return get_ref_name(refname, worktree,
2259 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2262 const struct got_error *
2263 got_worktree_get_histedit_script_path(char **path,
2264 struct got_worktree *worktree)
2266 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2267 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2269 return got_error_from_errno("asprintf");
2274 static const struct got_error *
2275 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2277 return get_ref_name(refname, worktree,
2278 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2281 static const struct got_error *
2282 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2284 return get_ref_name(refname, worktree,
2285 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2289 * Prevent Git's garbage collector from deleting our base commit by
2290 * setting a reference to our base commit's ID.
2292 static const struct got_error *
2293 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2295 const struct got_error *err = NULL;
2296 struct got_reference *ref = NULL;
2299 err = got_worktree_get_base_ref_name(&refname, worktree);
2303 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2307 err = got_ref_write(ref, repo);
2315 static const struct got_error *
2316 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2318 const struct got_error *err = NULL;
2320 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2321 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2322 err = got_error_from_errno("asprintf");
2323 *fileindex_path = NULL;
2329 static const struct got_error *
2330 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2331 struct got_worktree *worktree)
2333 const struct got_error *err = NULL;
2336 *fileindex_path = NULL;
2337 *fileindex = got_fileindex_alloc();
2338 if (*fileindex == NULL)
2339 return got_error_from_errno("got_fileindex_alloc");
2341 err = get_fileindex_path(fileindex_path, worktree);
2345 index = fopen(*fileindex_path, "rbe");
2346 if (index == NULL) {
2347 if (errno != ENOENT)
2348 err = got_error_from_errno2("fopen", *fileindex_path);
2350 err = got_fileindex_read(*fileindex, index);
2351 if (fclose(index) == EOF && err == NULL)
2352 err = got_error_from_errno("fclose");
2356 free(*fileindex_path);
2357 *fileindex_path = NULL;
2358 got_fileindex_free(*fileindex);
2364 struct bump_base_commit_id_arg {
2365 struct got_object_id *base_commit_id;
2368 const char *entry_name;
2369 got_worktree_checkout_cb progress_cb;
2373 /* Bump base commit ID of all files within an updated part of the work tree. */
2374 static const struct got_error *
2375 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2377 const struct got_error *err;
2378 struct bump_base_commit_id_arg *a = arg;
2380 if (a->entry_name) {
2381 if (strcmp(ie->path, a->path) != 0)
2383 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2386 if (got_fileindex_entry_was_skipped(ie))
2389 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2390 SHA1_DIGEST_LENGTH) == 0)
2393 if (a->progress_cb) {
2394 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2399 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2403 static const struct got_error *
2404 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2405 struct got_fileindex *fileindex,
2406 got_worktree_checkout_cb progress_cb, void *progress_arg)
2408 struct bump_base_commit_id_arg bbc_arg;
2410 bbc_arg.base_commit_id = worktree->base_commit_id;
2411 bbc_arg.entry_name = NULL;
2413 bbc_arg.path_len = 0;
2414 bbc_arg.progress_cb = progress_cb;
2415 bbc_arg.progress_arg = progress_arg;
2417 return got_fileindex_for_each_entry_safe(fileindex,
2418 bump_base_commit_id, &bbc_arg);
2421 static const struct got_error *
2422 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2424 const struct got_error *err = NULL;
2425 char *new_fileindex_path = NULL;
2426 FILE *new_index = NULL;
2427 struct timespec timeout;
2429 err = got_opentemp_named(&new_fileindex_path, &new_index,
2434 err = got_fileindex_write(fileindex, new_index);
2438 if (rename(new_fileindex_path, fileindex_path) != 0) {
2439 err = got_error_from_errno3("rename", new_fileindex_path,
2441 unlink(new_fileindex_path);
2445 * Sleep for a short amount of time to ensure that files modified after
2446 * this program exits have a different time stamp from the one which
2447 * was recorded in the file index.
2450 timeout.tv_nsec = 1;
2451 nanosleep(&timeout, NULL);
2455 free(new_fileindex_path);
2459 static const struct got_error *
2460 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2461 struct got_object_id **tree_id, const char *wt_relpath,
2462 struct got_worktree *worktree, struct got_repository *repo)
2464 const struct got_error *err = NULL;
2465 struct got_object_id *id = NULL;
2466 char *in_repo_path = NULL;
2467 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2469 *entry_type = GOT_OBJ_TYPE_ANY;
2470 *tree_relpath = NULL;
2473 if (wt_relpath[0] == '\0') {
2474 /* Check out all files within the work tree. */
2475 *entry_type = GOT_OBJ_TYPE_TREE;
2476 *tree_relpath = strdup("");
2477 if (*tree_relpath == NULL) {
2478 err = got_error_from_errno("strdup");
2481 err = got_object_id_by_path(tree_id, repo,
2482 worktree->base_commit_id, worktree->path_prefix);
2488 /* Check out a subset of files in the work tree. */
2490 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2491 is_root_wt ? "" : "/", wt_relpath) == -1) {
2492 err = got_error_from_errno("asprintf");
2496 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2502 in_repo_path = NULL;
2504 err = got_object_get_type(entry_type, repo, id);
2508 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2509 /* Check out a single file. */
2510 if (strchr(wt_relpath, '/') == NULL) {
2511 /* Check out a single file in work tree's root dir. */
2512 in_repo_path = strdup(worktree->path_prefix);
2513 if (in_repo_path == NULL) {
2514 err = got_error_from_errno("strdup");
2517 *tree_relpath = strdup("");
2518 if (*tree_relpath == NULL) {
2519 err = got_error_from_errno("strdup");
2523 /* Check out a single file in a subdirectory. */
2524 err = got_path_dirname(tree_relpath, wt_relpath);
2527 if (asprintf(&in_repo_path, "%s%s%s",
2528 worktree->path_prefix, is_root_wt ? "" : "/",
2529 *tree_relpath) == -1) {
2530 err = got_error_from_errno("asprintf");
2534 err = got_object_id_by_path(tree_id, repo,
2535 worktree->base_commit_id, in_repo_path);
2537 /* Check out all files within a subdirectory. */
2538 *tree_id = got_object_id_dup(id);
2539 if (*tree_id == NULL) {
2540 err = got_error_from_errno("got_object_id_dup");
2543 *tree_relpath = strdup(wt_relpath);
2544 if (*tree_relpath == NULL) {
2545 err = got_error_from_errno("strdup");
2553 *entry_type = GOT_OBJ_TYPE_ANY;
2554 free(*tree_relpath);
2555 *tree_relpath = NULL;
2562 static const struct got_error *
2563 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2564 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2565 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2566 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2568 const struct got_error *err = NULL;
2569 struct got_commit_object *commit = NULL;
2570 struct got_tree_object *tree = NULL;
2571 struct got_fileindex_diff_tree_cb diff_cb;
2572 struct diff_cb_arg arg;
2574 err = ref_base_commit(worktree, repo);
2576 if (!(err->code == GOT_ERR_ERRNO &&
2577 (errno == EACCES || errno == EROFS)))
2579 err = (*progress_cb)(progress_arg,
2580 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2585 err = got_object_open_as_commit(&commit, repo,
2586 worktree->base_commit_id);
2590 err = got_object_open_as_tree(&tree, repo, tree_id);
2595 got_object_tree_find_entry(tree, entry_name) == NULL) {
2596 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2600 diff_cb.diff_old_new = diff_old_new;
2601 diff_cb.diff_old = diff_old;
2602 diff_cb.diff_new = diff_new;
2603 arg.fileindex = fileindex;
2604 arg.worktree = worktree;
2606 arg.progress_cb = progress_cb;
2607 arg.progress_arg = progress_arg;
2608 arg.cancel_cb = cancel_cb;
2609 arg.cancel_arg = cancel_arg;
2610 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2611 entry_name, repo, &diff_cb, &arg);
2614 got_object_tree_close(tree);
2616 got_object_commit_close(commit);
2620 const struct got_error *
2621 got_worktree_checkout_files(struct got_worktree *worktree,
2622 struct got_pathlist_head *paths, struct got_repository *repo,
2623 got_worktree_checkout_cb progress_cb, void *progress_arg,
2624 got_cancel_cb cancel_cb, void *cancel_arg)
2626 const struct got_error *err = NULL, *sync_err, *unlockerr;
2627 struct got_commit_object *commit = NULL;
2628 struct got_tree_object *tree = NULL;
2629 struct got_fileindex *fileindex = NULL;
2630 char *fileindex_path = NULL;
2631 struct got_pathlist_entry *pe;
2632 struct tree_path_data {
2633 STAILQ_ENTRY(tree_path_data) entry;
2634 struct got_object_id *tree_id;
2639 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2641 STAILQ_INIT(&tree_paths);
2643 err = lock_worktree(worktree, LOCK_EX);
2647 /* Map all specified paths to in-repository trees. */
2648 TAILQ_FOREACH(pe, paths, entry) {
2649 tpd = malloc(sizeof(*tpd));
2651 err = got_error_from_errno("malloc");
2655 err = find_tree_entry_for_checkout(&tpd->entry_type,
2656 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2662 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2663 err = got_path_basename(&tpd->entry_name, pe->path);
2671 tpd->entry_name = NULL;
2673 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2677 * Read the file index.
2678 * Checking out files is supposed to be an idempotent operation.
2679 * If the on-disk file index is incomplete we will try to complete it.
2681 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2685 tpd = STAILQ_FIRST(&tree_paths);
2686 TAILQ_FOREACH(pe, paths, entry) {
2687 struct bump_base_commit_id_arg bbc_arg;
2689 err = checkout_files(worktree, fileindex, tpd->relpath,
2690 tpd->tree_id, tpd->entry_name, repo,
2691 progress_cb, progress_arg, cancel_cb, cancel_arg);
2695 bbc_arg.base_commit_id = worktree->base_commit_id;
2696 bbc_arg.entry_name = tpd->entry_name;
2697 bbc_arg.path = pe->path;
2698 bbc_arg.path_len = pe->path_len;
2699 bbc_arg.progress_cb = progress_cb;
2700 bbc_arg.progress_arg = progress_arg;
2701 err = got_fileindex_for_each_entry_safe(fileindex,
2702 bump_base_commit_id, &bbc_arg);
2706 tpd = STAILQ_NEXT(tpd, entry);
2708 sync_err = sync_fileindex(fileindex, fileindex_path);
2709 if (sync_err && err == NULL)
2712 free(fileindex_path);
2714 got_object_tree_close(tree);
2716 got_object_commit_close(commit);
2718 got_fileindex_free(fileindex);
2719 while (!STAILQ_EMPTY(&tree_paths)) {
2720 tpd = STAILQ_FIRST(&tree_paths);
2721 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2726 unlockerr = lock_worktree(worktree, LOCK_SH);
2727 if (unlockerr && err == NULL)
2732 struct merge_file_cb_arg {
2733 struct got_worktree *worktree;
2734 struct got_fileindex *fileindex;
2735 got_worktree_checkout_cb progress_cb;
2737 got_cancel_cb cancel_cb;
2739 const char *label_orig;
2740 struct got_object_id *commit_id2;
2741 int allow_bad_symlinks;
2744 static const struct got_error *
2745 merge_file_cb(void *arg, struct got_blob_object *blob1,
2746 struct got_blob_object *blob2, struct got_object_id *id1,
2747 struct got_object_id *id2, const char *path1, const char *path2,
2748 mode_t mode1, mode_t mode2, struct got_repository *repo)
2750 static const struct got_error *err = NULL;
2751 struct merge_file_cb_arg *a = arg;
2752 struct got_fileindex_entry *ie;
2753 char *ondisk_path = NULL;
2755 unsigned char status;
2756 int local_changes_subsumed;
2757 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2758 char *id_str = NULL, *label_deriv2 = NULL;
2760 if (blob1 && blob2) {
2761 ie = got_fileindex_entry_get(a->fileindex, path2,
2764 return (*a->progress_cb)(a->progress_arg,
2765 GOT_STATUS_MISSING, path2);
2767 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2769 return got_error_from_errno("asprintf");
2771 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2776 if (status == GOT_STATUS_DELETE) {
2777 err = (*a->progress_cb)(a->progress_arg,
2778 GOT_STATUS_MERGE, path2);
2781 if (status != GOT_STATUS_NO_CHANGE &&
2782 status != GOT_STATUS_MODIFY &&
2783 status != GOT_STATUS_CONFLICT &&
2784 status != GOT_STATUS_ADD) {
2785 err = (*a->progress_cb)(a->progress_arg, status, path2);
2789 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2791 err = got_object_blob_read_to_str(&link_target2, blob2);
2794 err = merge_symlink(a->worktree, blob1, ondisk_path,
2795 path2, a->label_orig, link_target2, a->commit_id2,
2796 repo, a->progress_cb, a->progress_arg);
2801 f_orig = got_opentemp();
2802 if (f_orig == NULL) {
2803 err = got_error_from_errno("got_opentemp");
2806 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2811 f_deriv2 = got_opentemp();
2812 if (f_deriv2 == NULL)
2814 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2819 fd = open(ondisk_path,
2820 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2822 err = got_error_from_errno2("open",
2826 f_deriv = fdopen(fd, "r");
2827 if (f_deriv == NULL) {
2828 err = got_error_from_errno2("fdopen",
2833 err = got_object_id_str(&id_str, a->commit_id2);
2836 if (asprintf(&label_deriv2, "%s: commit %s",
2837 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2838 err = got_error_from_errno("asprintf");
2841 err = merge_file(&local_changes_subsumed, a->worktree,
2842 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2843 sb.st_mode, a->label_orig, NULL, label_deriv2,
2844 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2845 a->progress_cb, a->progress_arg);
2848 ie = got_fileindex_entry_get(a->fileindex, path1,
2851 return (*a->progress_cb)(a->progress_arg,
2852 GOT_STATUS_MISSING, path1);
2854 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2856 return got_error_from_errno("asprintf");
2858 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2864 case GOT_STATUS_NO_CHANGE:
2865 err = (*a->progress_cb)(a->progress_arg,
2866 GOT_STATUS_DELETE, path1);
2869 err = remove_ondisk_file(a->worktree->root_path, path1);
2873 got_fileindex_entry_mark_deleted_from_disk(ie);
2875 case GOT_STATUS_DELETE:
2876 case GOT_STATUS_MISSING:
2877 err = (*a->progress_cb)(a->progress_arg,
2878 GOT_STATUS_DELETE, path1);
2882 got_fileindex_entry_mark_deleted_from_disk(ie);
2884 case GOT_STATUS_ADD: {
2885 struct got_object_id *id;
2889 * Delete the added file only if its content already
2890 * exists in the repository.
2892 err = got_object_blob_file_create(&id, &blob1_f,
2893 &blob1_size, path1);
2896 if (got_object_id_cmp(id, id1) == 0) {
2897 err = (*a->progress_cb)(a->progress_arg,
2898 GOT_STATUS_DELETE, path1);
2901 err = remove_ondisk_file(a->worktree->root_path,
2906 got_fileindex_entry_remove(a->fileindex,
2909 err = (*a->progress_cb)(a->progress_arg,
2910 GOT_STATUS_CANNOT_DELETE, path1);
2912 if (fclose(blob1_f) == EOF && err == NULL)
2913 err = got_error_from_errno("fclose");
2919 case GOT_STATUS_MODIFY:
2920 case GOT_STATUS_CONFLICT:
2921 err = (*a->progress_cb)(a->progress_arg,
2922 GOT_STATUS_CANNOT_DELETE, path1);
2926 case GOT_STATUS_OBSTRUCTED:
2927 err = (*a->progress_cb)(a->progress_arg, status, path1);
2935 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2937 return got_error_from_errno("asprintf");
2938 ie = got_fileindex_entry_get(a->fileindex, path2,
2941 err = get_file_status(&status, &sb, ie, ondisk_path,
2945 if (status != GOT_STATUS_NO_CHANGE &&
2946 status != GOT_STATUS_MODIFY &&
2947 status != GOT_STATUS_CONFLICT &&
2948 status != GOT_STATUS_ADD) {
2949 err = (*a->progress_cb)(a->progress_arg,
2953 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2955 err = got_object_blob_read_to_str(&link_target2,
2959 err = merge_symlink(a->worktree, NULL,
2960 ondisk_path, path2, a->label_orig,
2961 link_target2, a->commit_id2, repo,
2962 a->progress_cb, a->progress_arg);
2964 } else if (S_ISREG(sb.st_mode)) {
2965 err = merge_blob(&local_changes_subsumed,
2966 a->worktree, NULL, ondisk_path, path2,
2967 sb.st_mode, a->label_orig, blob2,
2968 a->commit_id2, repo, a->progress_cb,
2971 err = got_error_path(ondisk_path,
2972 GOT_ERR_FILE_OBSTRUCTED);
2976 if (status == GOT_STATUS_DELETE) {
2977 err = got_fileindex_entry_update(ie,
2978 a->worktree->root_fd, path2, blob2->id.sha1,
2979 a->worktree->base_commit_id->sha1, 0);
2984 int is_bad_symlink = 0;
2985 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2986 if (S_ISLNK(mode2)) {
2987 err = install_symlink(&is_bad_symlink,
2988 a->worktree, ondisk_path, path2, blob2, 0,
2989 0, 1, a->allow_bad_symlinks, repo,
2990 a->progress_cb, a->progress_arg);
2992 err = install_blob(a->worktree, ondisk_path, path2,
2993 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2994 a->progress_cb, a->progress_arg);
2998 err = got_fileindex_entry_alloc(&ie, path2);
3001 err = got_fileindex_entry_update(ie,
3002 a->worktree->root_fd, path2, NULL, NULL, 1);
3004 got_fileindex_entry_free(ie);
3007 err = got_fileindex_entry_add(a->fileindex, ie);
3009 got_fileindex_entry_free(ie);
3012 if (is_bad_symlink) {
3013 got_fileindex_entry_filetype_set(ie,
3014 GOT_FILEIDX_MODE_BAD_SYMLINK);
3019 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3020 err = got_error_from_errno("fclose");
3021 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3022 err = got_error_from_errno("fclose");
3023 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3024 err = got_error_from_errno("fclose");
3031 static const struct got_error *
3032 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3034 struct got_worktree *worktree = arg;
3036 /* Reject merges into a work tree with mixed base commits. */
3037 if (got_fileindex_entry_has_commit(ie) &&
3038 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3039 SHA1_DIGEST_LENGTH) != 0)
3040 return got_error(GOT_ERR_MIXED_COMMITS);
3045 struct check_merge_conflicts_arg {
3046 struct got_worktree *worktree;
3047 struct got_fileindex *fileindex;
3048 struct got_repository *repo;
3051 static const struct got_error *
3052 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3053 struct got_blob_object *blob2, struct got_object_id *id1,
3054 struct got_object_id *id2, const char *path1, const char *path2,
3055 mode_t mode1, mode_t mode2, struct got_repository *repo)
3057 const struct got_error *err = NULL;
3058 struct check_merge_conflicts_arg *a = arg;
3059 unsigned char status;
3061 struct got_fileindex_entry *ie;
3062 const char *path = path2 ? path2 : path1;
3063 struct got_object_id *id = id2 ? id2 : id1;
3069 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3073 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3075 return got_error_from_errno("asprintf");
3077 /* Reject merges into a work tree with conflicted files. */
3078 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3082 if (status == GOT_STATUS_CONFLICT)
3083 return got_error(GOT_ERR_CONFLICTS);
3088 static const struct got_error *
3089 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3090 const char *fileindex_path, struct got_object_id *commit_id1,
3091 struct got_object_id *commit_id2, struct got_repository *repo,
3092 got_worktree_checkout_cb progress_cb, void *progress_arg,
3093 got_cancel_cb cancel_cb, void *cancel_arg)
3095 const struct got_error *err = NULL, *sync_err;
3096 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3097 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3098 struct check_merge_conflicts_arg cmc_arg;
3099 struct merge_file_cb_arg arg;
3100 char *label_orig = NULL;
3103 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3104 worktree->path_prefix);
3105 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3111 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3115 err = got_object_id_str(&id_str, commit_id1);
3119 if (asprintf(&label_orig, "%s: commit %s",
3120 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3121 err = got_error_from_errno("asprintf");
3128 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3129 worktree->path_prefix);
3133 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3137 cmc_arg.worktree = worktree;
3138 cmc_arg.fileindex = fileindex;
3139 cmc_arg.repo = repo;
3140 err = got_diff_tree(tree1, tree2, "", "", repo,
3141 check_merge_conflicts, &cmc_arg, 0);
3145 arg.worktree = worktree;
3146 arg.fileindex = fileindex;
3147 arg.progress_cb = progress_cb;
3148 arg.progress_arg = progress_arg;
3149 arg.cancel_cb = cancel_cb;
3150 arg.cancel_arg = cancel_arg;
3151 arg.label_orig = label_orig;
3152 arg.commit_id2 = commit_id2;
3153 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3154 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3155 sync_err = sync_fileindex(fileindex, fileindex_path);
3156 if (sync_err && err == NULL)
3160 got_object_tree_close(tree1);
3162 got_object_tree_close(tree2);
3167 const struct got_error *
3168 got_worktree_merge_files(struct got_worktree *worktree,
3169 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3170 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3171 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3173 const struct got_error *err, *unlockerr;
3174 char *fileindex_path = NULL;
3175 struct got_fileindex *fileindex = NULL;
3177 err = lock_worktree(worktree, LOCK_EX);
3181 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3185 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3190 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3191 commit_id2, repo, progress_cb, progress_arg,
3192 cancel_cb, cancel_arg);
3195 got_fileindex_free(fileindex);
3196 free(fileindex_path);
3197 unlockerr = lock_worktree(worktree, LOCK_SH);
3198 if (unlockerr && err == NULL)
3203 struct diff_dir_cb_arg {
3204 struct got_fileindex *fileindex;
3205 struct got_worktree *worktree;
3206 const char *status_path;
3207 size_t status_path_len;
3208 struct got_repository *repo;
3209 got_worktree_status_cb status_cb;
3211 got_cancel_cb cancel_cb;
3213 /* A pathlist containing per-directory pathlists of ignore patterns. */
3214 struct got_pathlist_head *ignores;
3215 int report_unchanged;
3219 static const struct got_error *
3220 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3221 int dirfd, const char *de_name,
3222 got_worktree_status_cb status_cb, void *status_arg,
3223 struct got_repository *repo, int report_unchanged)
3225 const struct got_error *err = NULL;
3226 unsigned char status = GOT_STATUS_NO_CHANGE;
3227 unsigned char staged_status = get_staged_status(ie);
3229 struct got_object_id blob_id, commit_id, staged_blob_id;
3230 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3231 struct got_object_id *staged_blob_idp = NULL;
3233 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3237 if (status == GOT_STATUS_NO_CHANGE &&
3238 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3241 if (got_fileindex_entry_has_blob(ie)) {
3242 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3243 blob_idp = &blob_id;
3245 if (got_fileindex_entry_has_commit(ie)) {
3246 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3247 commit_idp = &commit_id;
3249 if (staged_status == GOT_STATUS_ADD ||
3250 staged_status == GOT_STATUS_MODIFY) {
3251 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3252 SHA1_DIGEST_LENGTH);
3253 staged_blob_idp = &staged_blob_id;
3256 return (*status_cb)(status_arg, status, staged_status,
3257 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3260 static const struct got_error *
3261 status_old_new(void *arg, struct got_fileindex_entry *ie,
3262 struct dirent *de, const char *parent_path, int dirfd)
3264 const struct got_error *err = NULL;
3265 struct diff_dir_cb_arg *a = arg;
3268 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3269 return got_error(GOT_ERR_CANCELLED);
3271 if (got_path_cmp(parent_path, a->status_path,
3272 strlen(parent_path), a->status_path_len) != 0 &&
3273 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3276 if (parent_path[0]) {
3277 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3278 parent_path, de->d_name) == -1)
3279 return got_error_from_errno("asprintf");
3281 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3283 return got_error_from_errno("asprintf");
3286 err = report_file_status(ie, abspath, dirfd, de->d_name,
3287 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3292 static const struct got_error *
3293 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3295 struct diff_dir_cb_arg *a = arg;
3296 struct got_object_id blob_id, commit_id;
3297 unsigned char status;
3299 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3300 return got_error(GOT_ERR_CANCELLED);
3302 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3305 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3306 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3307 if (got_fileindex_entry_has_file_on_disk(ie))
3308 status = GOT_STATUS_MISSING;
3310 status = GOT_STATUS_DELETE;
3311 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3312 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3316 free_ignorelist(struct got_pathlist_head *ignorelist)
3318 struct got_pathlist_entry *pe;
3320 TAILQ_FOREACH(pe, ignorelist, entry)
3321 free((char *)pe->path);
3322 got_pathlist_free(ignorelist);
3326 free_ignores(struct got_pathlist_head *ignores)
3328 struct got_pathlist_entry *pe;
3330 TAILQ_FOREACH(pe, ignores, entry) {
3331 struct got_pathlist_head *ignorelist = pe->data;
3332 free_ignorelist(ignorelist);
3333 free((char *)pe->path);
3335 got_pathlist_free(ignores);
3338 static const struct got_error *
3339 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3341 const struct got_error *err = NULL;
3342 struct got_pathlist_entry *pe = NULL;
3343 struct got_pathlist_head *ignorelist;
3344 char *line = NULL, *pattern, *dirpath = NULL;
3345 size_t linesize = 0;
3348 ignorelist = calloc(1, sizeof(*ignorelist));
3349 if (ignorelist == NULL)
3350 return got_error_from_errno("calloc");
3351 TAILQ_INIT(ignorelist);
3353 while ((linelen = getline(&line, &linesize, f)) != -1) {
3354 if (linelen > 0 && line[linelen - 1] == '\n')
3355 line[linelen - 1] = '\0';
3357 /* Git's ignores may contain comments. */
3361 /* Git's negated patterns are not (yet?) supported. */
3365 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3367 err = got_error_from_errno("asprintf");
3370 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3375 err = got_error_from_errno("getline");
3379 dirpath = strdup(path);
3380 if (dirpath == NULL) {
3381 err = got_error_from_errno("strdup");
3384 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3387 if (err || pe == NULL) {
3389 free_ignorelist(ignorelist);
3395 match_ignores(struct got_pathlist_head *ignores, const char *path)
3397 struct got_pathlist_entry *pe;
3399 /* Handle patterns which match in all directories. */
3400 TAILQ_FOREACH(pe, ignores, entry) {
3401 struct got_pathlist_head *ignorelist = pe->data;
3402 struct got_pathlist_entry *pi;
3404 TAILQ_FOREACH(pi, ignorelist, entry) {
3405 const char *p, *pattern = pi->path;
3407 if (strncmp(pattern, "**/", 3) != 0)
3412 if (fnmatch(pattern, p,
3413 FNM_PATHNAME | FNM_LEADING_DIR)) {
3414 /* Retry in next directory. */
3415 while (*p && *p != '/')
3427 * The ignores pathlist contains ignore lists from children before
3428 * parents, so we can find the most specific ignorelist by walking
3429 * ignores backwards.
3431 pe = TAILQ_LAST(ignores, got_pathlist_head);
3433 if (got_path_is_child(path, pe->path, pe->path_len)) {
3434 struct got_pathlist_head *ignorelist = pe->data;
3435 struct got_pathlist_entry *pi;
3436 TAILQ_FOREACH(pi, ignorelist, entry) {
3437 const char *pattern = pi->path;
3438 int flags = FNM_LEADING_DIR;
3439 if (strstr(pattern, "/**/") == NULL)
3440 flags |= FNM_PATHNAME;
3441 if (fnmatch(pattern, path, flags))
3446 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3452 static const struct got_error *
3453 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3454 const char *path, int dirfd, const char *ignores_filename)
3456 const struct got_error *err = NULL;
3459 FILE *ignoresfile = NULL;
3461 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3462 path[0] ? "/" : "", ignores_filename) == -1)
3463 return got_error_from_errno("asprintf");
3466 fd = openat(dirfd, ignores_filename,
3467 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3469 if (errno != ENOENT && errno != EACCES)
3470 err = got_error_from_errno2("openat",
3473 ignoresfile = fdopen(fd, "r");
3474 if (ignoresfile == NULL)
3475 err = got_error_from_errno2("fdopen",
3479 err = read_ignores(ignores, path, ignoresfile);
3483 ignoresfile = fopen(ignorespath, "re");
3484 if (ignoresfile == NULL) {
3485 if (errno != ENOENT && errno != EACCES)
3486 err = got_error_from_errno2("fopen",
3489 err = read_ignores(ignores, path, ignoresfile);
3492 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3493 err = got_error_from_errno2("fclose", path);
3494 if (fd != -1 && close(fd) == -1 && err == NULL)
3495 err = got_error_from_errno2("close", path);
3500 static const struct got_error *
3501 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3504 const struct got_error *err = NULL;
3505 struct diff_dir_cb_arg *a = arg;
3511 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3512 return got_error(GOT_ERR_CANCELLED);
3514 if (parent_path[0]) {
3515 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3516 return got_error_from_errno("asprintf");
3521 if (de->d_type == DT_DIR) {
3522 if (!a->no_ignores && ignore != NULL &&
3523 match_ignores(a->ignores, path))
3525 } else if (!match_ignores(a->ignores, path) &&
3526 got_path_is_child(path, a->status_path, a->status_path_len))
3527 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3528 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3534 static const struct got_error *
3535 status_traverse(void *arg, const char *path, int dirfd)
3537 const struct got_error *err = NULL;
3538 struct diff_dir_cb_arg *a = arg;
3543 err = add_ignores(a->ignores, a->worktree->root_path,
3544 path, dirfd, ".cvsignore");
3548 err = add_ignores(a->ignores, a->worktree->root_path, path,
3549 dirfd, ".gitignore");
3554 static const struct got_error *
3555 report_single_file_status(const char *path, const char *ondisk_path,
3556 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3557 void *status_arg, struct got_repository *repo, int report_unchanged,
3558 struct got_pathlist_head *ignores, int no_ignores)
3560 struct got_fileindex_entry *ie;
3563 if (!no_ignores && match_ignores(ignores, path))
3566 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3568 return report_file_status(ie, ondisk_path, -1, NULL,
3569 status_cb, status_arg, repo, report_unchanged);
3571 if (lstat(ondisk_path, &sb) == -1) {
3572 if (errno != ENOENT)
3573 return got_error_from_errno2("lstat", ondisk_path);
3574 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3575 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3578 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3579 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3580 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3585 static const struct got_error *
3586 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3587 const char *root_path, const char *path)
3589 const struct got_error *err;
3590 char *parent_path, *next_parent_path = NULL;
3592 err = add_ignores(ignores, root_path, "", -1,
3597 err = add_ignores(ignores, root_path, "", -1,
3602 err = got_path_dirname(&parent_path, path);
3604 if (err->code == GOT_ERR_BAD_PATH)
3605 return NULL; /* cannot traverse parent */
3609 err = add_ignores(ignores, root_path, parent_path, -1,
3613 err = add_ignores(ignores, root_path, parent_path, -1,
3617 err = got_path_dirname(&next_parent_path, parent_path);
3619 if (err->code == GOT_ERR_BAD_PATH)
3620 err = NULL; /* traversed everything */
3623 if (got_path_is_root_dir(parent_path))
3626 parent_path = next_parent_path;
3627 next_parent_path = NULL;
3631 free(next_parent_path);
3635 static const struct got_error *
3636 worktree_status(struct got_worktree *worktree, const char *path,
3637 struct got_fileindex *fileindex, struct got_repository *repo,
3638 got_worktree_status_cb status_cb, void *status_arg,
3639 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3640 int report_unchanged)
3642 const struct got_error *err = NULL;
3644 struct got_fileindex_diff_dir_cb fdiff_cb;
3645 struct diff_dir_cb_arg arg;
3646 char *ondisk_path = NULL;
3647 struct got_pathlist_head ignores;
3649 TAILQ_INIT(&ignores);
3651 if (asprintf(&ondisk_path, "%s%s%s",
3652 worktree->root_path, path[0] ? "/" : "", path) == -1)
3653 return got_error_from_errno("asprintf");
3655 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3657 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3658 !got_err_open_nofollow_on_symlink())
3659 err = got_error_from_errno2("open", ondisk_path);
3662 err = add_ignores_from_parent_paths(&ignores,
3663 worktree->root_path, ondisk_path);
3667 err = report_single_file_status(path, ondisk_path,
3668 fileindex, status_cb, status_arg, repo,
3669 report_unchanged, &ignores, no_ignores);
3672 fdiff_cb.diff_old_new = status_old_new;
3673 fdiff_cb.diff_old = status_old;
3674 fdiff_cb.diff_new = status_new;
3675 fdiff_cb.diff_traverse = status_traverse;
3676 arg.fileindex = fileindex;
3677 arg.worktree = worktree;
3678 arg.status_path = path;
3679 arg.status_path_len = strlen(path);
3681 arg.status_cb = status_cb;
3682 arg.status_arg = status_arg;
3683 arg.cancel_cb = cancel_cb;
3684 arg.cancel_arg = cancel_arg;
3685 arg.report_unchanged = report_unchanged;
3686 arg.no_ignores = no_ignores;
3688 err = add_ignores_from_parent_paths(&ignores,
3689 worktree->root_path, path);
3693 arg.ignores = &ignores;
3694 err = got_fileindex_diff_dir(fileindex, fd,
3695 worktree->root_path, path, repo, &fdiff_cb, &arg);
3698 free_ignores(&ignores);
3699 if (fd != -1 && close(fd) == -1 && err == NULL)
3700 err = got_error_from_errno("close");
3705 const struct got_error *
3706 got_worktree_status(struct got_worktree *worktree,
3707 struct got_pathlist_head *paths, struct got_repository *repo,
3708 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3709 got_cancel_cb cancel_cb, void *cancel_arg)
3711 const struct got_error *err = NULL;
3712 char *fileindex_path = NULL;
3713 struct got_fileindex *fileindex = NULL;
3714 struct got_pathlist_entry *pe;
3716 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3720 TAILQ_FOREACH(pe, paths, entry) {
3721 err = worktree_status(worktree, pe->path, fileindex, repo,
3722 status_cb, status_arg, cancel_cb, cancel_arg,
3727 free(fileindex_path);
3728 got_fileindex_free(fileindex);
3732 const struct got_error *
3733 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3736 const struct got_error *err = NULL;
3737 char *resolved = NULL, *cwd = NULL, *path = NULL;
3740 char *abspath = NULL;
3741 char canonpath[PATH_MAX];
3745 cwd = getcwd(NULL, 0);
3747 return got_error_from_errno("getcwd");
3749 if (lstat(arg, &sb) == -1) {
3750 if (errno != ENOENT) {
3751 err = got_error_from_errno2("lstat", arg);
3756 if (S_ISLNK(sb.st_mode)) {
3758 * We cannot use realpath(3) with symlinks since we want to
3759 * operate on the symlink itself.
3760 * But we can make the path absolute, assuming it is relative
3761 * to the current working directory, and then canonicalize it.
3763 if (!got_path_is_absolute(arg)) {
3764 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3765 err = got_error_from_errno("asprintf");
3770 err = got_canonpath(abspath ? abspath : arg, canonpath,
3774 resolved = strdup(canonpath);
3775 if (resolved == NULL) {
3776 err = got_error_from_errno("strdup");
3780 resolved = realpath(arg, NULL);
3781 if (resolved == NULL) {
3782 if (errno != ENOENT) {
3783 err = got_error_from_errno2("realpath", arg);
3786 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3787 err = got_error_from_errno("asprintf");
3790 err = got_canonpath(abspath, canonpath,
3794 resolved = strdup(canonpath);
3795 if (resolved == NULL) {
3796 err = got_error_from_errno("strdup");
3802 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3803 strlen(got_worktree_get_root_path(worktree)))) {
3804 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3808 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3809 err = got_path_skip_common_ancestor(&path,
3810 got_worktree_get_root_path(worktree), resolved);
3816 err = got_error_from_errno("strdup");
3821 /* XXX status walk can't deal with trailing slash! */
3823 while (len > 0 && path[len - 1] == '/') {
3824 path[len - 1] = '\0';
3838 struct schedule_addition_args {
3839 struct got_worktree *worktree;
3840 struct got_fileindex *fileindex;
3841 got_worktree_checkout_cb progress_cb;
3843 struct got_repository *repo;
3846 static const struct got_error *
3847 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3848 const char *relpath, struct got_object_id *blob_id,
3849 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3850 int dirfd, const char *de_name)
3852 struct schedule_addition_args *a = arg;
3853 const struct got_error *err = NULL;
3854 struct got_fileindex_entry *ie;
3858 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3860 return got_error_from_errno("asprintf");
3862 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3864 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3868 /* Re-adding an existing entry is a no-op. */
3869 if (status == GOT_STATUS_ADD)
3871 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3876 if (status != GOT_STATUS_UNVERSIONED) {
3877 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3881 err = got_fileindex_entry_alloc(&ie, relpath);
3884 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3885 relpath, NULL, NULL, 1);
3887 got_fileindex_entry_free(ie);
3890 err = got_fileindex_entry_add(a->fileindex, ie);
3892 got_fileindex_entry_free(ie);
3899 if (status == GOT_STATUS_ADD)
3901 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3904 const struct got_error *
3905 got_worktree_schedule_add(struct got_worktree *worktree,
3906 struct got_pathlist_head *paths,
3907 got_worktree_checkout_cb progress_cb, void *progress_arg,
3908 struct got_repository *repo, int no_ignores)
3910 struct got_fileindex *fileindex = NULL;
3911 char *fileindex_path = NULL;
3912 const struct got_error *err = NULL, *sync_err, *unlockerr;
3913 struct got_pathlist_entry *pe;
3914 struct schedule_addition_args saa;
3916 err = lock_worktree(worktree, LOCK_EX);
3920 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3924 saa.worktree = worktree;
3925 saa.fileindex = fileindex;
3926 saa.progress_cb = progress_cb;
3927 saa.progress_arg = progress_arg;
3930 TAILQ_FOREACH(pe, paths, entry) {
3931 err = worktree_status(worktree, pe->path, fileindex, repo,
3932 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3936 sync_err = sync_fileindex(fileindex, fileindex_path);
3937 if (sync_err && err == NULL)
3940 free(fileindex_path);
3942 got_fileindex_free(fileindex);
3943 unlockerr = lock_worktree(worktree, LOCK_SH);
3944 if (unlockerr && err == NULL)
3949 struct schedule_deletion_args {
3950 struct got_worktree *worktree;
3951 struct got_fileindex *fileindex;
3952 got_worktree_delete_cb progress_cb;
3954 struct got_repository *repo;
3955 int delete_local_mods;
3957 const char *status_codes;
3960 static const struct got_error *
3961 schedule_for_deletion(void *arg, unsigned char status,
3962 unsigned char staged_status, const char *relpath,
3963 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3964 struct got_object_id *commit_id, int dirfd, const char *de_name)
3966 struct schedule_deletion_args *a = arg;
3967 const struct got_error *err = NULL;
3968 struct got_fileindex_entry *ie = NULL;
3972 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3974 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3976 staged_status = get_staged_status(ie);
3977 if (staged_status != GOT_STATUS_NO_CHANGE) {
3978 if (staged_status == GOT_STATUS_DELETE)
3980 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3983 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3985 return got_error_from_errno("asprintf");
3987 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3992 if (a->status_codes) {
3993 size_t ncodes = strlen(a->status_codes);
3995 for (i = 0; i < ncodes ; i++) {
3996 if (status == a->status_codes[i])
4000 /* Do not delete files in non-matching status. */
4004 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4005 a->status_codes[i] != GOT_STATUS_MISSING) {
4006 static char msg[64];
4007 snprintf(msg, sizeof(msg),
4008 "invalid status code '%c'", a->status_codes[i]);
4009 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4014 if (status != GOT_STATUS_NO_CHANGE) {
4015 if (status == GOT_STATUS_DELETE)
4017 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4018 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4021 if (status != GOT_STATUS_MODIFY &&
4022 status != GOT_STATUS_MISSING) {
4023 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4028 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4032 if (unlinkat(dirfd, de_name, 0) != 0) {
4033 err = got_error_from_errno2("unlinkat",
4037 } else if (unlink(ondisk_path) != 0) {
4038 err = got_error_from_errno2("unlink", ondisk_path);
4042 root_len = strlen(a->worktree->root_path);
4045 err = got_path_dirname(&parent, ondisk_path);
4049 ondisk_path = parent;
4050 if (rmdir(ondisk_path) == -1) {
4051 if (errno != ENOTEMPTY)
4052 err = got_error_from_errno2("rmdir",
4056 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4057 strlen(ondisk_path), root_len) != 0);
4060 got_fileindex_entry_mark_deleted_from_disk(ie);
4065 if (status == GOT_STATUS_DELETE)
4067 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4068 staged_status, relpath);
4071 const struct got_error *
4072 got_worktree_schedule_delete(struct got_worktree *worktree,
4073 struct got_pathlist_head *paths, int delete_local_mods,
4074 const char *status_codes,
4075 got_worktree_delete_cb progress_cb, void *progress_arg,
4076 struct got_repository *repo, int keep_on_disk)
4078 struct got_fileindex *fileindex = NULL;
4079 char *fileindex_path = NULL;
4080 const struct got_error *err = NULL, *sync_err, *unlockerr;
4081 struct got_pathlist_entry *pe;
4082 struct schedule_deletion_args sda;
4084 err = lock_worktree(worktree, LOCK_EX);
4088 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4092 sda.worktree = worktree;
4093 sda.fileindex = fileindex;
4094 sda.progress_cb = progress_cb;
4095 sda.progress_arg = progress_arg;
4097 sda.delete_local_mods = delete_local_mods;
4098 sda.keep_on_disk = keep_on_disk;
4099 sda.status_codes = status_codes;
4101 TAILQ_FOREACH(pe, paths, entry) {
4102 err = worktree_status(worktree, pe->path, fileindex, repo,
4103 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4107 sync_err = sync_fileindex(fileindex, fileindex_path);
4108 if (sync_err && err == NULL)
4111 free(fileindex_path);
4113 got_fileindex_free(fileindex);
4114 unlockerr = lock_worktree(worktree, LOCK_SH);
4115 if (unlockerr && err == NULL)
4120 static const struct got_error *
4121 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4123 const struct got_error *err = NULL;
4125 size_t linesize = 0, n;
4128 linelen = getline(&line, &linesize, infile);
4129 if (linelen == -1) {
4130 if (ferror(infile)) {
4131 err = got_error_from_errno("getline");
4137 n = fwrite(line, 1, linelen, outfile);
4139 err = got_ferror(outfile, GOT_ERR_IO);
4144 n = fwrite(line, 1, linelen, rejectfile);
4146 err = got_ferror(outfile, GOT_ERR_IO);
4153 static const struct got_error *
4154 skip_one_line(FILE *f)
4157 size_t linesize = 0;
4160 linelen = getline(&line, &linesize, f);
4161 if (linelen == -1) {
4163 return got_error_from_errno("getline");
4170 static const struct got_error *
4171 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4172 int start_old, int end_old, int start_new, int end_new,
4173 FILE *outfile, FILE *rejectfile)
4175 const struct got_error *err;
4177 /* Copy old file's lines leading up to patch. */
4178 while (!feof(f1) && *line_cur1 < start_old) {
4179 err = copy_one_line(f1, outfile, NULL);
4184 /* Skip new file's lines leading up to patch. */
4185 while (!feof(f2) && *line_cur2 < start_new) {
4187 err = copy_one_line(f2, NULL, rejectfile);
4189 err = skip_one_line(f2);
4194 /* Copy patched lines. */
4195 while (!feof(f2) && *line_cur2 <= end_new) {
4196 err = copy_one_line(f2, outfile, NULL);
4201 /* Skip over old file's replaced lines. */
4202 while (!feof(f1) && *line_cur1 <= end_old) {
4204 err = copy_one_line(f1, NULL, rejectfile);
4206 err = skip_one_line(f1);
4215 static const struct got_error *
4216 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4217 FILE *outfile, FILE *rejectfile)
4219 const struct got_error *err;
4222 /* Copy old file's lines until EOF. */
4224 err = copy_one_line(f1, outfile, NULL);
4231 /* Copy new file's lines until EOF. */
4233 err = copy_one_line(f2, NULL, rejectfile);
4243 static const struct got_error *
4244 apply_or_reject_change(int *choice, int *nchunks_used,
4245 struct diff_result *diff_result, int n,
4246 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4247 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4248 got_worktree_patch_cb patch_cb, void *patch_arg)
4250 const struct got_error *err = NULL;
4251 struct diff_chunk_context cc = {};
4252 int start_old, end_old, start_new, end_new;
4254 struct diff_output_unidiff_state *diff_state;
4255 struct diff_input_info diff_info;
4258 *choice = GOT_PATCH_CHOICE_NONE;
4260 /* Get changed line numbers without context lines for copy_change(). */
4261 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4262 start_old = cc.left.start;
4263 end_old = cc.left.end;
4264 start_new = cc.right.start;
4265 end_new = cc.right.end;
4267 /* Get the same change with context lines for display. */
4268 memset(&cc, 0, sizeof(cc));
4269 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4271 memset(&diff_info, 0, sizeof(diff_info));
4272 diff_info.left_path = relpath;
4273 diff_info.right_path = relpath;
4275 diff_state = diff_output_unidiff_state_alloc();
4276 if (diff_state == NULL)
4277 return got_error_set_errno(ENOMEM,
4278 "diff_output_unidiff_state_alloc");
4280 hunkfile = got_opentemp();
4281 if (hunkfile == NULL) {
4282 err = got_error_from_errno("got_opentemp");
4286 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4288 if (rc != DIFF_RC_OK) {
4289 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4293 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4294 err = got_ferror(hunkfile, GOT_ERR_IO);
4298 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4299 hunkfile, changeno, nchanges);
4304 case GOT_PATCH_CHOICE_YES:
4305 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4306 end_old, start_new, end_new, outfile, rejectfile);
4308 case GOT_PATCH_CHOICE_NO:
4309 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4310 end_old, start_new, end_new, rejectfile, outfile);
4312 case GOT_PATCH_CHOICE_QUIT:
4315 err = got_error(GOT_ERR_PATCH_CHOICE);
4319 diff_output_unidiff_state_free(diff_state);
4320 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4321 err = got_error_from_errno("fclose");
4325 struct revert_file_args {
4326 struct got_worktree *worktree;
4327 struct got_fileindex *fileindex;
4328 got_worktree_checkout_cb progress_cb;
4330 got_worktree_patch_cb patch_cb;
4332 struct got_repository *repo;
4333 int unlink_added_files;
4336 static const struct got_error *
4337 create_patched_content(char **path_outfile, int reverse_patch,
4338 struct got_object_id *blob_id, const char *path2,
4339 int dirfd2, const char *de_name2,
4340 const char *relpath, struct got_repository *repo,
4341 got_worktree_patch_cb patch_cb, void *patch_arg)
4343 const struct got_error *err, *free_err;
4344 struct got_blob_object *blob = NULL;
4345 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4347 char link_target[PATH_MAX];
4348 ssize_t link_len = 0;
4349 char *path1 = NULL, *id_str = NULL;
4351 struct got_diffreg_result *diffreg_result = NULL;
4352 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4353 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4355 *path_outfile = NULL;
4357 err = got_object_id_str(&id_str, blob_id);
4362 fd2 = openat(dirfd2, de_name2,
4363 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4365 if (!got_err_open_nofollow_on_symlink()) {
4366 err = got_error_from_errno2("openat", path2);
4369 link_len = readlinkat(dirfd2, de_name2,
4370 link_target, sizeof(link_target));
4371 if (link_len == -1) {
4372 return got_error_from_errno2("readlinkat",
4375 sb2.st_mode = S_IFLNK;
4376 sb2.st_size = link_len;
4379 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4381 if (!got_err_open_nofollow_on_symlink()) {
4382 err = got_error_from_errno2("open", path2);
4385 link_len = readlink(path2, link_target,
4386 sizeof(link_target));
4388 return got_error_from_errno2("readlink", path2);
4389 sb2.st_mode = S_IFLNK;
4390 sb2.st_size = link_len;
4394 if (fstat(fd2, &sb2) == -1) {
4395 err = got_error_from_errno2("fstat", path2);
4399 f2 = fdopen(fd2, "r");
4401 err = got_error_from_errno2("fdopen", path2);
4407 f2 = got_opentemp();
4409 err = got_error_from_errno2("got_opentemp", path2);
4412 n = fwrite(link_target, 1, link_len, f2);
4413 if (n != link_len) {
4414 err = got_ferror(f2, GOT_ERR_IO);
4417 if (fflush(f2) == EOF) {
4418 err = got_error_from_errno("fflush");
4424 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4428 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4432 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4436 err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4441 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4445 if (fseek(f1, 0L, SEEK_SET) == -1)
4446 return got_ferror(f1, GOT_ERR_IO);
4447 if (fseek(f2, 0L, SEEK_SET) == -1)
4448 return got_ferror(f2, GOT_ERR_IO);
4450 /* Count the number of actual changes in the diff result. */
4451 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4452 struct diff_chunk_context cc = {};
4453 diff_chunk_context_load_change(&cc, &nchunks_used,
4454 diffreg_result->result, n, 0);
4457 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4459 err = apply_or_reject_change(&choice, &nchunks_used,
4460 diffreg_result->result, n, relpath, f1, f2,
4461 &line_cur1, &line_cur2,
4462 reverse_patch ? NULL : outfile,
4463 reverse_patch ? outfile : NULL,
4464 ++i, nchanges, patch_cb, patch_arg);
4467 if (choice == GOT_PATCH_CHOICE_YES)
4469 else if (choice == GOT_PATCH_CHOICE_QUIT)
4473 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4474 reverse_patch ? NULL : outfile,
4475 reverse_patch ? outfile : NULL);
4479 if (!S_ISLNK(sb2.st_mode)) {
4480 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4481 err = got_error_from_errno2("fchmod", path2);
4489 got_object_blob_close(blob);
4490 free_err = got_diffreg_result_free(diffreg_result);
4493 if (f1 && fclose(f1) == EOF && err == NULL)
4494 err = got_error_from_errno2("fclose", path1);
4495 if (f2 && fclose(f2) == EOF && err == NULL)
4496 err = got_error_from_errno2("fclose", path2);
4497 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4498 err = got_error_from_errno2("close", path2);
4499 if (outfile && fclose(outfile) == EOF && err == NULL)
4500 err = got_error_from_errno2("fclose", *path_outfile);
4501 if (path1 && unlink(path1) == -1 && err == NULL)
4502 err = got_error_from_errno2("unlink", path1);
4503 if (err || !have_content) {
4504 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4505 err = got_error_from_errno2("unlink", *path_outfile);
4506 free(*path_outfile);
4507 *path_outfile = NULL;
4513 static const struct got_error *
4514 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4515 const char *relpath, struct got_object_id *blob_id,
4516 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4517 int dirfd, const char *de_name)
4519 struct revert_file_args *a = arg;
4520 const struct got_error *err = NULL;
4521 char *parent_path = NULL;
4522 struct got_fileindex_entry *ie;
4523 struct got_tree_object *tree = NULL;
4524 struct got_object_id *tree_id = NULL;
4525 const struct got_tree_entry *te = NULL;
4526 char *tree_path = NULL, *te_name;
4527 char *ondisk_path = NULL, *path_content = NULL;
4528 struct got_blob_object *blob = NULL;
4530 /* Reverting a staged deletion is a no-op. */
4531 if (status == GOT_STATUS_DELETE &&
4532 staged_status != GOT_STATUS_NO_CHANGE)
4535 if (status == GOT_STATUS_UNVERSIONED)
4536 return (*a->progress_cb)(a->progress_arg,
4537 GOT_STATUS_UNVERSIONED, relpath);
4539 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4541 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4543 /* Construct in-repository path of tree which contains this blob. */
4544 err = got_path_dirname(&parent_path, ie->path);
4546 if (err->code != GOT_ERR_BAD_PATH)
4548 parent_path = strdup("/");
4549 if (parent_path == NULL) {
4550 err = got_error_from_errno("strdup");
4554 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4555 tree_path = strdup(parent_path);
4556 if (tree_path == NULL) {
4557 err = got_error_from_errno("strdup");
4561 if (got_path_is_root_dir(parent_path)) {
4562 tree_path = strdup(a->worktree->path_prefix);
4563 if (tree_path == NULL) {
4564 err = got_error_from_errno("strdup");
4568 if (asprintf(&tree_path, "%s/%s",
4569 a->worktree->path_prefix, parent_path) == -1) {
4570 err = got_error_from_errno("asprintf");
4576 err = got_object_id_by_path(&tree_id, a->repo,
4577 a->worktree->base_commit_id, tree_path);
4579 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4580 (status == GOT_STATUS_ADD ||
4581 staged_status == GOT_STATUS_ADD)))
4584 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4588 err = got_path_basename(&te_name, ie->path);
4592 te = got_object_tree_find_entry(tree, te_name);
4594 if (te == NULL && status != GOT_STATUS_ADD &&
4595 staged_status != GOT_STATUS_ADD) {
4596 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4602 case GOT_STATUS_ADD:
4604 int choice = GOT_PATCH_CHOICE_NONE;
4605 err = (*a->patch_cb)(&choice, a->patch_arg,
4606 status, ie->path, NULL, 1, 1);
4609 if (choice != GOT_PATCH_CHOICE_YES)
4612 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4616 got_fileindex_entry_remove(a->fileindex, ie);
4617 if (a->unlink_added_files) {
4618 if (asprintf(&ondisk_path, "%s/%s",
4619 got_worktree_get_root_path(a->worktree),
4621 err = got_error_from_errno("asprintf");
4624 if (unlink(ondisk_path) == -1) {
4625 err = got_error_from_errno2("unlink",
4631 case GOT_STATUS_DELETE:
4633 int choice = GOT_PATCH_CHOICE_NONE;
4634 err = (*a->patch_cb)(&choice, a->patch_arg,
4635 status, ie->path, NULL, 1, 1);
4638 if (choice != GOT_PATCH_CHOICE_YES)
4642 case GOT_STATUS_MODIFY:
4643 case GOT_STATUS_MODE_CHANGE:
4644 case GOT_STATUS_CONFLICT:
4645 case GOT_STATUS_MISSING: {
4646 struct got_object_id id;
4647 if (staged_status == GOT_STATUS_ADD ||
4648 staged_status == GOT_STATUS_MODIFY) {
4649 memcpy(id.sha1, ie->staged_blob_sha1,
4650 SHA1_DIGEST_LENGTH);
4652 memcpy(id.sha1, ie->blob_sha1,
4653 SHA1_DIGEST_LENGTH);
4654 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4658 if (asprintf(&ondisk_path, "%s/%s",
4659 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4660 err = got_error_from_errno("asprintf");
4664 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4665 status == GOT_STATUS_CONFLICT)) {
4666 int is_bad_symlink = 0;
4667 err = create_patched_content(&path_content, 1, &id,
4668 ondisk_path, dirfd, de_name, ie->path, a->repo,
4669 a->patch_cb, a->patch_arg);
4670 if (err || path_content == NULL)
4672 if (te && S_ISLNK(te->mode)) {
4673 if (unlink(path_content) == -1) {
4674 err = got_error_from_errno2("unlink",
4678 err = install_symlink(&is_bad_symlink,
4679 a->worktree, ondisk_path, ie->path,
4680 blob, 0, 1, 0, 0, a->repo,
4681 a->progress_cb, a->progress_arg);
4683 if (rename(path_content, ondisk_path) == -1) {
4684 err = got_error_from_errno3("rename",
4685 path_content, ondisk_path);
4690 int is_bad_symlink = 0;
4691 if (te && S_ISLNK(te->mode)) {
4692 err = install_symlink(&is_bad_symlink,
4693 a->worktree, ondisk_path, ie->path,
4694 blob, 0, 1, 0, 0, a->repo,
4695 a->progress_cb, a->progress_arg);
4697 err = install_blob(a->worktree, ondisk_path,
4699 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4700 got_fileindex_perms_to_st(ie), blob,
4701 0, 1, 0, 0, a->repo,
4702 a->progress_cb, a->progress_arg);
4706 if (status == GOT_STATUS_DELETE ||
4707 status == GOT_STATUS_MODE_CHANGE) {
4708 err = got_fileindex_entry_update(ie,
4709 a->worktree->root_fd, relpath,
4711 a->worktree->base_commit_id->sha1, 1);
4715 if (is_bad_symlink) {
4716 got_fileindex_entry_filetype_set(ie,
4717 GOT_FILEIDX_MODE_BAD_SYMLINK);
4731 got_object_blob_close(blob);
4733 got_object_tree_close(tree);
4738 const struct got_error *
4739 got_worktree_revert(struct got_worktree *worktree,
4740 struct got_pathlist_head *paths,
4741 got_worktree_checkout_cb progress_cb, void *progress_arg,
4742 got_worktree_patch_cb patch_cb, void *patch_arg,
4743 struct got_repository *repo)
4745 struct got_fileindex *fileindex = NULL;
4746 char *fileindex_path = NULL;
4747 const struct got_error *err = NULL, *unlockerr = NULL;
4748 const struct got_error *sync_err = NULL;
4749 struct got_pathlist_entry *pe;
4750 struct revert_file_args rfa;
4752 err = lock_worktree(worktree, LOCK_EX);
4756 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4760 rfa.worktree = worktree;
4761 rfa.fileindex = fileindex;
4762 rfa.progress_cb = progress_cb;
4763 rfa.progress_arg = progress_arg;
4764 rfa.patch_cb = patch_cb;
4765 rfa.patch_arg = patch_arg;
4767 rfa.unlink_added_files = 0;
4768 TAILQ_FOREACH(pe, paths, entry) {
4769 err = worktree_status(worktree, pe->path, fileindex, repo,
4770 revert_file, &rfa, NULL, NULL, 1, 0);
4774 sync_err = sync_fileindex(fileindex, fileindex_path);
4775 if (sync_err && err == NULL)
4778 free(fileindex_path);
4780 got_fileindex_free(fileindex);
4781 unlockerr = lock_worktree(worktree, LOCK_SH);
4782 if (unlockerr && err == NULL)
4788 free_commitable(struct got_commitable *ct)
4791 free(ct->in_repo_path);
4792 free(ct->ondisk_path);
4794 free(ct->base_blob_id);
4795 free(ct->staged_blob_id);
4796 free(ct->base_commit_id);
4800 struct collect_commitables_arg {
4801 struct got_pathlist_head *commitable_paths;
4802 struct got_repository *repo;
4803 struct got_worktree *worktree;
4804 struct got_fileindex *fileindex;
4805 int have_staged_files;
4806 int allow_bad_symlinks;
4809 static const struct got_error *
4810 collect_commitables(void *arg, unsigned char status,
4811 unsigned char staged_status, const char *relpath,
4812 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4813 struct got_object_id *commit_id, int dirfd, const char *de_name)
4815 struct collect_commitables_arg *a = arg;
4816 const struct got_error *err = NULL;
4817 struct got_commitable *ct = NULL;
4818 struct got_pathlist_entry *new = NULL;
4819 char *parent_path = NULL, *path = NULL;
4822 if (a->have_staged_files) {
4823 if (staged_status != GOT_STATUS_MODIFY &&
4824 staged_status != GOT_STATUS_ADD &&
4825 staged_status != GOT_STATUS_DELETE)
4828 if (status == GOT_STATUS_CONFLICT)
4829 return got_error(GOT_ERR_COMMIT_CONFLICT);
4831 if (status != GOT_STATUS_MODIFY &&
4832 status != GOT_STATUS_MODE_CHANGE &&
4833 status != GOT_STATUS_ADD &&
4834 status != GOT_STATUS_DELETE)
4838 if (asprintf(&path, "/%s", relpath) == -1) {
4839 err = got_error_from_errno("asprintf");
4842 if (strcmp(path, "/") == 0) {
4843 parent_path = strdup("");
4844 if (parent_path == NULL)
4845 return got_error_from_errno("strdup");
4847 err = got_path_dirname(&parent_path, path);
4852 ct = calloc(1, sizeof(*ct));
4854 err = got_error_from_errno("calloc");
4858 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4860 err = got_error_from_errno("asprintf");
4864 if (staged_status == GOT_STATUS_ADD ||
4865 staged_status == GOT_STATUS_MODIFY) {
4866 struct got_fileindex_entry *ie;
4867 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4868 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4869 case GOT_FILEIDX_MODE_REGULAR_FILE:
4870 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4873 case GOT_FILEIDX_MODE_SYMLINK:
4877 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4880 ct->mode |= got_fileindex_entry_perms_get(ie);
4881 } else if (status != GOT_STATUS_DELETE &&
4882 staged_status != GOT_STATUS_DELETE) {
4884 if (fstatat(dirfd, de_name, &sb,
4885 AT_SYMLINK_NOFOLLOW) == -1) {
4886 err = got_error_from_errno2("fstatat",
4890 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4891 err = got_error_from_errno2("lstat", ct->ondisk_path);
4894 ct->mode = sb.st_mode;
4897 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4898 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4900 err = got_error_from_errno("asprintf");
4904 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4905 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4907 char target_path[PATH_MAX];
4909 target_len = readlink(ct->ondisk_path, target_path,
4910 sizeof(target_path));
4911 if (target_len == -1) {
4912 err = got_error_from_errno2("readlink",
4916 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4917 target_len, ct->ondisk_path, a->worktree->root_path);
4920 if (is_bad_symlink) {
4921 err = got_error_path(ct->ondisk_path,
4922 GOT_ERR_BAD_SYMLINK);
4928 ct->status = status;
4929 ct->staged_status = staged_status;
4930 ct->blob_id = NULL; /* will be filled in when blob gets created */
4931 if (ct->status != GOT_STATUS_ADD &&
4932 ct->staged_status != GOT_STATUS_ADD) {
4933 ct->base_blob_id = got_object_id_dup(blob_id);
4934 if (ct->base_blob_id == NULL) {
4935 err = got_error_from_errno("got_object_id_dup");
4938 ct->base_commit_id = got_object_id_dup(commit_id);
4939 if (ct->base_commit_id == NULL) {
4940 err = got_error_from_errno("got_object_id_dup");
4944 if (ct->staged_status == GOT_STATUS_ADD ||
4945 ct->staged_status == GOT_STATUS_MODIFY) {
4946 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4947 if (ct->staged_blob_id == NULL) {
4948 err = got_error_from_errno("got_object_id_dup");
4952 ct->path = strdup(path);
4953 if (ct->path == NULL) {
4954 err = got_error_from_errno("strdup");
4957 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4959 if (ct && (err || new == NULL))
4960 free_commitable(ct);
4966 static const struct got_error *write_tree(struct got_object_id **, int *,
4967 struct got_tree_object *, const char *, struct got_pathlist_head *,
4968 got_worktree_status_cb status_cb, void *status_arg,
4969 struct got_repository *);
4971 static const struct got_error *
4972 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4973 struct got_tree_entry *te, const char *parent_path,
4974 struct got_pathlist_head *commitable_paths,
4975 got_worktree_status_cb status_cb, void *status_arg,
4976 struct got_repository *repo)
4978 const struct got_error *err = NULL;
4979 struct got_tree_object *subtree;
4982 if (asprintf(&subpath, "%s%s%s", parent_path,
4983 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4984 return got_error_from_errno("asprintf");
4986 err = got_object_open_as_tree(&subtree, repo, &te->id);
4990 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4991 commitable_paths, status_cb, status_arg, repo);
4992 got_object_tree_close(subtree);
4997 static const struct got_error *
4998 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5000 const struct got_error *err = NULL;
5001 char *ct_parent_path = NULL;
5005 if (strchr(ct->in_repo_path, '/') == NULL) {
5006 *match = got_path_is_root_dir(path);
5010 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5013 *match = (strcmp(path, ct_parent_path) == 0);
5014 free(ct_parent_path);
5019 get_ct_file_mode(struct got_commitable *ct)
5021 if (S_ISLNK(ct->mode))
5024 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5027 static const struct got_error *
5028 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5029 struct got_tree_entry *te, struct got_commitable *ct)
5031 const struct got_error *err = NULL;
5035 err = got_object_tree_entry_dup(new_te, te);
5039 (*new_te)->mode = get_ct_file_mode(ct);
5041 if (ct->staged_status == GOT_STATUS_MODIFY)
5042 memcpy(&(*new_te)->id, ct->staged_blob_id,
5043 sizeof((*new_te)->id));
5045 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5047 if (err && *new_te) {
5054 static const struct got_error *
5055 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5056 struct got_commitable *ct)
5058 const struct got_error *err = NULL;
5059 char *ct_name = NULL;
5063 *new_te = calloc(1, sizeof(**new_te));
5064 if (*new_te == NULL)
5065 return got_error_from_errno("calloc");
5067 err = got_path_basename(&ct_name, ct->path);
5070 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5071 sizeof((*new_te)->name)) {
5072 err = got_error(GOT_ERR_NO_SPACE);
5076 (*new_te)->mode = get_ct_file_mode(ct);
5078 if (ct->staged_status == GOT_STATUS_ADD)
5079 memcpy(&(*new_te)->id, ct->staged_blob_id,
5080 sizeof((*new_te)->id));
5082 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5085 if (err && *new_te) {
5092 static const struct got_error *
5093 insert_tree_entry(struct got_tree_entry *new_te,
5094 struct got_pathlist_head *paths)
5096 const struct got_error *err = NULL;
5097 struct got_pathlist_entry *new_pe;
5099 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5103 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5107 static const struct got_error *
5108 report_ct_status(struct got_commitable *ct,
5109 got_worktree_status_cb status_cb, void *status_arg)
5111 const char *ct_path = ct->path;
5112 unsigned char status;
5114 if (status_cb == NULL) /* no commit progress output desired */
5117 while (ct_path[0] == '/')
5120 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5121 status = ct->staged_status;
5123 status = ct->status;
5125 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5126 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5129 static const struct got_error *
5130 match_modified_subtree(int *modified, struct got_tree_entry *te,
5131 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5133 const struct got_error *err = NULL;
5134 struct got_pathlist_entry *pe;
5139 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5140 got_path_is_root_dir(base_tree_path) ? "" : "/",
5142 return got_error_from_errno("asprintf");
5144 TAILQ_FOREACH(pe, commitable_paths, entry) {
5145 struct got_commitable *ct = pe->data;
5146 *modified = got_path_is_child(ct->in_repo_path, te_path,
5156 static const struct got_error *
5157 match_deleted_or_modified_ct(struct got_commitable **ctp,
5158 struct got_tree_entry *te, const char *base_tree_path,
5159 struct got_pathlist_head *commitable_paths)
5161 const struct got_error *err = NULL;
5162 struct got_pathlist_entry *pe;
5166 TAILQ_FOREACH(pe, commitable_paths, entry) {
5167 struct got_commitable *ct = pe->data;
5168 char *ct_name = NULL;
5171 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5172 if (ct->status != GOT_STATUS_MODIFY &&
5173 ct->status != GOT_STATUS_MODE_CHANGE &&
5174 ct->status != GOT_STATUS_DELETE)
5177 if (ct->staged_status != GOT_STATUS_MODIFY &&
5178 ct->staged_status != GOT_STATUS_DELETE)
5182 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5185 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5191 err = got_path_basename(&ct_name, pe->path);
5195 if (strcmp(te->name, ct_name) != 0) {
5208 static const struct got_error *
5209 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5210 const char *child_path, const char *path_base_tree,
5211 struct got_pathlist_head *commitable_paths,
5212 got_worktree_status_cb status_cb, void *status_arg,
5213 struct got_repository *repo)
5215 const struct got_error *err = NULL;
5216 struct got_tree_entry *new_te;
5218 struct got_object_id *id = NULL;
5223 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5224 got_path_is_root_dir(path_base_tree) ? "" : "/",
5226 return got_error_from_errno("asprintf");
5228 new_te = calloc(1, sizeof(*new_te));
5230 return got_error_from_errno("calloc");
5231 new_te->mode = S_IFDIR;
5233 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5234 sizeof(new_te->name)) {
5235 err = got_error(GOT_ERR_NO_SPACE);
5238 err = write_tree(&id, &nentries, NULL, subtree_path,
5239 commitable_paths, status_cb, status_arg, repo);
5244 memcpy(&new_te->id, id, sizeof(new_te->id));
5253 static const struct got_error *
5254 write_tree(struct got_object_id **new_tree_id, int *nentries,
5255 struct got_tree_object *base_tree, const char *path_base_tree,
5256 struct got_pathlist_head *commitable_paths,
5257 got_worktree_status_cb status_cb, void *status_arg,
5258 struct got_repository *repo)
5260 const struct got_error *err = NULL;
5261 struct got_pathlist_head paths;
5262 struct got_tree_entry *te, *new_te = NULL;
5263 struct got_pathlist_entry *pe;
5268 /* Insert, and recurse into, newly added entries first. */
5269 TAILQ_FOREACH(pe, commitable_paths, entry) {
5270 struct got_commitable *ct = pe->data;
5271 char *child_path = NULL, *slash;
5273 if ((ct->status != GOT_STATUS_ADD &&
5274 ct->staged_status != GOT_STATUS_ADD) ||
5275 (ct->flags & GOT_COMMITABLE_ADDED))
5278 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5279 strlen(path_base_tree)))
5282 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5287 slash = strchr(child_path, '/');
5288 if (slash == NULL) {
5289 err = alloc_added_blob_tree_entry(&new_te, ct);
5292 err = report_ct_status(ct, status_cb, status_arg);
5295 ct->flags |= GOT_COMMITABLE_ADDED;
5296 err = insert_tree_entry(new_te, &paths);
5301 *slash = '\0'; /* trim trailing path components */
5302 if (base_tree == NULL ||
5303 got_object_tree_find_entry(base_tree, child_path)
5305 err = make_subtree_for_added_blob(&new_te,
5306 child_path, path_base_tree,
5307 commitable_paths, status_cb, status_arg,
5311 err = insert_tree_entry(new_te, &paths);
5320 int i, nbase_entries;
5321 /* Handle modified and deleted entries. */
5322 nbase_entries = got_object_tree_get_nentries(base_tree);
5323 for (i = 0; i < nbase_entries; i++) {
5324 struct got_commitable *ct = NULL;
5326 te = got_object_tree_get_entry(base_tree, i);
5327 if (got_object_tree_entry_is_submodule(te)) {
5328 /* Entry is a submodule; just copy it. */
5329 err = got_object_tree_entry_dup(&new_te, te);
5332 err = insert_tree_entry(new_te, &paths);
5339 if (S_ISDIR(te->mode)) {
5341 err = got_object_tree_entry_dup(&new_te, te);
5344 err = match_modified_subtree(&modified, te,
5345 path_base_tree, commitable_paths);
5348 /* Avoid recursion into unmodified subtrees. */
5350 struct got_object_id *new_id;
5352 err = write_subtree(&new_id,
5354 path_base_tree, commitable_paths,
5355 status_cb, status_arg, repo);
5358 if (nsubentries == 0) {
5359 /* All entries were deleted. */
5363 memcpy(&new_te->id, new_id,
5364 sizeof(new_te->id));
5367 err = insert_tree_entry(new_te, &paths);
5374 err = match_deleted_or_modified_ct(&ct, te,
5375 path_base_tree, commitable_paths);
5379 /* NB: Deleted entries get dropped here. */
5380 if (ct->status == GOT_STATUS_MODIFY ||
5381 ct->status == GOT_STATUS_MODE_CHANGE ||
5382 ct->staged_status == GOT_STATUS_MODIFY) {
5383 err = alloc_modified_blob_tree_entry(
5387 err = insert_tree_entry(new_te, &paths);
5392 err = report_ct_status(ct, status_cb,
5397 /* Entry is unchanged; just copy it. */
5398 err = got_object_tree_entry_dup(&new_te, te);
5401 err = insert_tree_entry(new_te, &paths);
5409 /* Write new list of entries; deleted entries have been dropped. */
5410 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5412 got_pathlist_free(&paths);
5416 static const struct got_error *
5417 update_fileindex_after_commit(struct got_worktree *worktree,
5418 struct got_pathlist_head *commitable_paths,
5419 struct got_object_id *new_base_commit_id,
5420 struct got_fileindex *fileindex, int have_staged_files)
5422 const struct got_error *err = NULL;
5423 struct got_pathlist_entry *pe;
5424 char *relpath = NULL;
5426 TAILQ_FOREACH(pe, commitable_paths, entry) {
5427 struct got_fileindex_entry *ie;
5428 struct got_commitable *ct = pe->data;
5430 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5432 err = got_path_skip_common_ancestor(&relpath,
5433 worktree->root_path, ct->ondisk_path);
5438 if (ct->status == GOT_STATUS_DELETE ||
5439 ct->staged_status == GOT_STATUS_DELETE) {
5440 got_fileindex_entry_remove(fileindex, ie);
5441 } else if (ct->staged_status == GOT_STATUS_ADD ||
5442 ct->staged_status == GOT_STATUS_MODIFY) {
5443 got_fileindex_entry_stage_set(ie,
5444 GOT_FILEIDX_STAGE_NONE);
5445 got_fileindex_entry_staged_filetype_set(ie, 0);
5447 err = got_fileindex_entry_update(ie,
5448 worktree->root_fd, relpath,
5449 ct->staged_blob_id->sha1,
5450 new_base_commit_id->sha1,
5451 !have_staged_files);
5453 err = got_fileindex_entry_update(ie,
5454 worktree->root_fd, relpath,
5456 new_base_commit_id->sha1,
5457 !have_staged_files);
5459 err = got_fileindex_entry_alloc(&ie, pe->path);
5462 err = got_fileindex_entry_update(ie,
5463 worktree->root_fd, relpath, ct->blob_id->sha1,
5464 new_base_commit_id->sha1, 1);
5466 got_fileindex_entry_free(ie);
5469 err = got_fileindex_entry_add(fileindex, ie);
5471 got_fileindex_entry_free(ie);
5484 static const struct got_error *
5485 check_out_of_date(const char *in_repo_path, unsigned char status,
5486 unsigned char staged_status, struct got_object_id *base_blob_id,
5487 struct got_object_id *base_commit_id,
5488 struct got_object_id *head_commit_id, struct got_repository *repo,
5491 const struct got_error *err = NULL;
5492 struct got_object_id *id = NULL;
5494 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5495 /* Trivial case: base commit == head commit */
5496 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5499 * Ensure file content which local changes were based
5500 * on matches file content in the branch head.
5502 err = got_object_id_by_path(&id, repo, head_commit_id,
5505 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5506 err = got_error(ood_errcode);
5508 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5509 err = got_error(ood_errcode);
5511 /* Require that added files don't exist in the branch head. */
5512 err = got_object_id_by_path(&id, repo, head_commit_id,
5514 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5516 err = id ? got_error(ood_errcode) : NULL;
5523 const struct got_error *
5524 commit_worktree(struct got_object_id **new_commit_id,
5525 struct got_pathlist_head *commitable_paths,
5526 struct got_object_id *head_commit_id,
5527 struct got_object_id *parent_id2,
5528 struct got_worktree *worktree,
5529 const char *author, const char *committer,
5530 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5531 got_worktree_status_cb status_cb, void *status_arg,
5532 struct got_repository *repo)
5534 const struct got_error *err = NULL, *unlockerr = NULL;
5535 struct got_pathlist_entry *pe;
5536 const char *head_ref_name = NULL;
5537 struct got_commit_object *head_commit = NULL;
5538 struct got_reference *head_ref2 = NULL;
5539 struct got_object_id *head_commit_id2 = NULL;
5540 struct got_tree_object *head_tree = NULL;
5541 struct got_object_id *new_tree_id = NULL;
5542 int nentries, nparents = 0;
5543 struct got_object_id_queue parent_ids;
5544 struct got_object_qid *pid = NULL;
5545 char *logmsg = NULL;
5547 *new_commit_id = NULL;
5549 STAILQ_INIT(&parent_ids);
5551 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5555 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5559 if (commit_msg_cb != NULL) {
5560 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5565 if (logmsg == NULL || strlen(logmsg) == 0) {
5566 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5570 /* Create blobs from added and modified files and record their IDs. */
5571 TAILQ_FOREACH(pe, commitable_paths, entry) {
5572 struct got_commitable *ct = pe->data;
5575 /* Blobs for staged files already exist. */
5576 if (ct->staged_status == GOT_STATUS_ADD ||
5577 ct->staged_status == GOT_STATUS_MODIFY)
5580 if (ct->status != GOT_STATUS_ADD &&
5581 ct->status != GOT_STATUS_MODIFY &&
5582 ct->status != GOT_STATUS_MODE_CHANGE)
5585 if (asprintf(&ondisk_path, "%s/%s",
5586 worktree->root_path, pe->path) == -1) {
5587 err = got_error_from_errno("asprintf");
5590 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5596 /* Recursively write new tree objects. */
5597 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5598 commitable_paths, status_cb, status_arg, repo);
5602 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5605 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5608 err = got_object_qid_alloc(&pid, parent_id2);
5611 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5614 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5615 nparents, author, time(NULL), committer, time(NULL), logmsg, repo);
5621 /* Check if a concurrent commit to our branch has occurred. */
5622 head_ref_name = got_worktree_get_head_ref_name(worktree);
5623 if (head_ref_name == NULL) {
5624 err = got_error_from_errno("got_worktree_get_head_ref_name");
5627 /* Lock the reference here to prevent concurrent modification. */
5628 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5631 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5634 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5635 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5638 /* Update branch head in repository. */
5639 err = got_ref_change_ref(head_ref2, *new_commit_id);
5642 err = got_ref_write(head_ref2, repo);
5646 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5650 err = ref_base_commit(worktree, repo);
5654 got_object_id_queue_free(&parent_ids);
5656 got_object_tree_close(head_tree);
5658 got_object_commit_close(head_commit);
5659 free(head_commit_id2);
5661 unlockerr = got_ref_unlock(head_ref2);
5662 if (unlockerr && err == NULL)
5664 got_ref_close(head_ref2);
5669 static const struct got_error *
5670 check_path_is_commitable(const char *path,
5671 struct got_pathlist_head *commitable_paths)
5673 struct got_pathlist_entry *cpe = NULL;
5674 size_t path_len = strlen(path);
5676 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5677 struct got_commitable *ct = cpe->data;
5678 const char *ct_path = ct->path;
5680 while (ct_path[0] == '/')
5683 if (strcmp(path, ct_path) == 0 ||
5684 got_path_is_child(ct_path, path, path_len))
5689 return got_error_path(path, GOT_ERR_BAD_PATH);
5694 static const struct got_error *
5695 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5697 int *have_staged_files = arg;
5699 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5700 *have_staged_files = 1;
5701 return got_error(GOT_ERR_CANCELLED);
5707 static const struct got_error *
5708 check_non_staged_files(struct got_fileindex *fileindex,
5709 struct got_pathlist_head *paths)
5711 struct got_pathlist_entry *pe;
5712 struct got_fileindex_entry *ie;
5714 TAILQ_FOREACH(pe, paths, entry) {
5715 if (pe->path[0] == '\0')
5717 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5719 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5720 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5721 return got_error_path(pe->path,
5722 GOT_ERR_FILE_NOT_STAGED);
5728 const struct got_error *
5729 got_worktree_commit(struct got_object_id **new_commit_id,
5730 struct got_worktree *worktree, struct got_pathlist_head *paths,
5731 const char *author, const char *committer, int allow_bad_symlinks,
5732 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5733 got_worktree_status_cb status_cb, void *status_arg,
5734 struct got_repository *repo)
5736 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5737 struct got_fileindex *fileindex = NULL;
5738 char *fileindex_path = NULL;
5739 struct got_pathlist_head commitable_paths;
5740 struct collect_commitables_arg cc_arg;
5741 struct got_pathlist_entry *pe;
5742 struct got_reference *head_ref = NULL;
5743 struct got_object_id *head_commit_id = NULL;
5744 int have_staged_files = 0;
5746 *new_commit_id = NULL;
5748 TAILQ_INIT(&commitable_paths);
5750 err = lock_worktree(worktree, LOCK_EX);
5754 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5758 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5762 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5766 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5767 &have_staged_files);
5768 if (err && err->code != GOT_ERR_CANCELLED)
5770 if (have_staged_files) {
5771 err = check_non_staged_files(fileindex, paths);
5776 cc_arg.commitable_paths = &commitable_paths;
5777 cc_arg.worktree = worktree;
5778 cc_arg.fileindex = fileindex;
5780 cc_arg.have_staged_files = have_staged_files;
5781 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5782 TAILQ_FOREACH(pe, paths, entry) {
5783 err = worktree_status(worktree, pe->path, fileindex, repo,
5784 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
5789 if (TAILQ_EMPTY(&commitable_paths)) {
5790 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5794 TAILQ_FOREACH(pe, paths, entry) {
5795 err = check_path_is_commitable(pe->path, &commitable_paths);
5800 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5801 struct got_commitable *ct = pe->data;
5802 const char *ct_path = ct->in_repo_path;
5804 while (ct_path[0] == '/')
5806 err = check_out_of_date(ct_path, ct->status,
5807 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5808 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5814 err = commit_worktree(new_commit_id, &commitable_paths,
5815 head_commit_id, NULL, worktree, author, committer,
5816 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5820 err = update_fileindex_after_commit(worktree, &commitable_paths,
5821 *new_commit_id, fileindex, have_staged_files);
5822 sync_err = sync_fileindex(fileindex, fileindex_path);
5823 if (sync_err && err == NULL)
5827 got_fileindex_free(fileindex);
5828 free(fileindex_path);
5829 unlockerr = lock_worktree(worktree, LOCK_SH);
5830 if (unlockerr && err == NULL)
5832 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5833 struct got_commitable *ct = pe->data;
5834 free_commitable(ct);
5836 got_pathlist_free(&commitable_paths);
5841 got_commitable_get_path(struct got_commitable *ct)
5847 got_commitable_get_status(struct got_commitable *ct)
5852 struct check_rebase_ok_arg {
5853 struct got_worktree *worktree;
5854 struct got_repository *repo;
5857 static const struct got_error *
5858 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5860 const struct got_error *err = NULL;
5861 struct check_rebase_ok_arg *a = arg;
5862 unsigned char status;
5866 /* Reject rebase of a work tree with mixed base commits. */
5867 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5868 SHA1_DIGEST_LENGTH))
5869 return got_error(GOT_ERR_MIXED_COMMITS);
5871 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5873 return got_error_from_errno("asprintf");
5875 /* Reject rebase of a work tree with modified or staged files. */
5876 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5881 if (status != GOT_STATUS_NO_CHANGE)
5882 return got_error(GOT_ERR_MODIFIED);
5883 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5884 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5889 const struct got_error *
5890 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5891 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5892 struct got_worktree *worktree, struct got_reference *branch,
5893 struct got_repository *repo)
5895 const struct got_error *err = NULL;
5896 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5897 char *branch_ref_name = NULL;
5898 char *fileindex_path = NULL;
5899 struct check_rebase_ok_arg ok_arg;
5900 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5901 struct got_object_id *wt_branch_tip = NULL;
5903 *new_base_branch_ref = NULL;
5907 err = lock_worktree(worktree, LOCK_EX);
5911 err = open_fileindex(fileindex, &fileindex_path, worktree);
5915 ok_arg.worktree = worktree;
5917 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5922 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5926 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5930 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5934 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5939 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5942 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5943 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5947 err = got_ref_alloc_symref(new_base_branch_ref,
5948 new_base_branch_ref_name, wt_branch);
5951 err = got_ref_write(*new_base_branch_ref, repo);
5955 /* TODO Lock original branch's ref while rebasing? */
5957 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5961 err = got_ref_write(branch_ref, repo);
5965 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5966 worktree->base_commit_id);
5969 err = got_ref_write(*tmp_branch, repo);
5973 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5977 free(fileindex_path);
5978 free(tmp_branch_name);
5979 free(new_base_branch_ref_name);
5980 free(branch_ref_name);
5982 got_ref_close(branch_ref);
5984 got_ref_close(wt_branch);
5985 free(wt_branch_tip);
5987 if (*new_base_branch_ref) {
5988 got_ref_close(*new_base_branch_ref);
5989 *new_base_branch_ref = NULL;
5992 got_ref_close(*tmp_branch);
5996 got_fileindex_free(*fileindex);
5999 lock_worktree(worktree, LOCK_SH);
6004 const struct got_error *
6005 got_worktree_rebase_continue(struct got_object_id **commit_id,
6006 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6007 struct got_reference **branch, struct got_fileindex **fileindex,
6008 struct got_worktree *worktree, struct got_repository *repo)
6010 const struct got_error *err;
6011 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6012 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6013 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6014 char *fileindex_path = NULL;
6015 int have_staged_files = 0;
6018 *new_base_branch = NULL;
6023 err = lock_worktree(worktree, LOCK_EX);
6027 err = open_fileindex(fileindex, &fileindex_path, worktree);
6031 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6032 &have_staged_files);
6033 if (err && err->code != GOT_ERR_CANCELLED)
6035 if (have_staged_files) {
6036 err = got_error(GOT_ERR_STAGED_PATHS);
6040 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6044 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6048 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6052 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6056 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6060 err = got_ref_open(branch, repo,
6061 got_ref_get_symref_target(branch_ref), 0);
6065 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6069 err = got_ref_resolve(commit_id, repo, commit_ref);
6073 err = got_ref_open(new_base_branch, repo,
6074 new_base_branch_ref_name, 0);
6078 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6082 free(commit_ref_name);
6083 free(branch_ref_name);
6084 free(fileindex_path);
6086 got_ref_close(commit_ref);
6088 got_ref_close(branch_ref);
6093 got_ref_close(*tmp_branch);
6096 if (*new_base_branch) {
6097 got_ref_close(*new_base_branch);
6098 *new_base_branch = NULL;
6101 got_ref_close(*branch);
6105 got_fileindex_free(*fileindex);
6108 lock_worktree(worktree, LOCK_SH);
6113 const struct got_error *
6114 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6116 const struct got_error *err;
6117 char *tmp_branch_name = NULL;
6119 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6123 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6124 free(tmp_branch_name);
6128 static const struct got_error *
6129 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6130 char **logmsg, void *arg)
6136 static const struct got_error *
6137 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6138 const char *path, struct got_object_id *blob_id,
6139 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6140 int dirfd, const char *de_name)
6145 struct collect_merged_paths_arg {
6146 got_worktree_checkout_cb progress_cb;
6148 struct got_pathlist_head *merged_paths;
6151 static const struct got_error *
6152 collect_merged_paths(void *arg, unsigned char status, const char *path)
6154 const struct got_error *err;
6155 struct collect_merged_paths_arg *a = arg;
6157 struct got_pathlist_entry *new;
6159 err = (*a->progress_cb)(a->progress_arg, status, path);
6163 if (status != GOT_STATUS_MERGE &&
6164 status != GOT_STATUS_ADD &&
6165 status != GOT_STATUS_DELETE &&
6166 status != GOT_STATUS_CONFLICT)
6171 return got_error_from_errno("strdup");
6173 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6174 if (err || new == NULL)
6180 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6182 struct got_pathlist_entry *pe;
6184 TAILQ_FOREACH(pe, merged_paths, entry)
6185 free((char *)pe->path);
6187 got_pathlist_free(merged_paths);
6190 static const struct got_error *
6191 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6192 int is_rebase, struct got_repository *repo)
6194 const struct got_error *err;
6195 struct got_reference *commit_ref = NULL;
6197 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6199 if (err->code != GOT_ERR_NOT_REF)
6201 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6204 err = got_ref_write(commit_ref, repo);
6207 } else if (is_rebase) {
6208 struct got_object_id *stored_id;
6211 err = got_ref_resolve(&stored_id, repo, commit_ref);
6214 cmp = got_object_id_cmp(commit_id, stored_id);
6217 err = got_error(GOT_ERR_REBASE_COMMITID);
6223 got_ref_close(commit_ref);
6227 static const struct got_error *
6228 rebase_merge_files(struct got_pathlist_head *merged_paths,
6229 const char *commit_ref_name, struct got_worktree *worktree,
6230 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6231 struct got_object_id *commit_id, struct got_repository *repo,
6232 got_worktree_checkout_cb progress_cb, void *progress_arg,
6233 got_cancel_cb cancel_cb, void *cancel_arg)
6235 const struct got_error *err;
6236 struct got_reference *commit_ref = NULL;
6237 struct collect_merged_paths_arg cmp_arg;
6238 char *fileindex_path;
6240 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6242 err = get_fileindex_path(&fileindex_path, worktree);
6246 cmp_arg.progress_cb = progress_cb;
6247 cmp_arg.progress_arg = progress_arg;
6248 cmp_arg.merged_paths = merged_paths;
6249 err = merge_files(worktree, fileindex, fileindex_path,
6250 parent_commit_id, commit_id, repo, collect_merged_paths,
6251 &cmp_arg, cancel_cb, cancel_arg);
6253 got_ref_close(commit_ref);
6257 const struct got_error *
6258 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6259 struct got_worktree *worktree, struct got_fileindex *fileindex,
6260 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6261 struct got_repository *repo,
6262 got_worktree_checkout_cb progress_cb, void *progress_arg,
6263 got_cancel_cb cancel_cb, void *cancel_arg)
6265 const struct got_error *err;
6266 char *commit_ref_name;
6268 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6272 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6276 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6277 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6278 progress_arg, cancel_cb, cancel_arg);
6280 free(commit_ref_name);
6284 const struct got_error *
6285 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6286 struct got_worktree *worktree, struct got_fileindex *fileindex,
6287 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6288 struct got_repository *repo,
6289 got_worktree_checkout_cb progress_cb, void *progress_arg,
6290 got_cancel_cb cancel_cb, void *cancel_arg)
6292 const struct got_error *err;
6293 char *commit_ref_name;
6295 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6299 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6303 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6304 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6305 progress_arg, cancel_cb, cancel_arg);
6307 free(commit_ref_name);
6311 static const struct got_error *
6312 rebase_commit(struct got_object_id **new_commit_id,
6313 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6314 struct got_worktree *worktree, struct got_fileindex *fileindex,
6315 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6316 const char *new_logmsg, struct got_repository *repo)
6318 const struct got_error *err, *sync_err;
6319 struct got_pathlist_head commitable_paths;
6320 struct collect_commitables_arg cc_arg;
6321 char *fileindex_path = NULL;
6322 struct got_reference *head_ref = NULL;
6323 struct got_object_id *head_commit_id = NULL;
6324 char *logmsg = NULL;
6326 TAILQ_INIT(&commitable_paths);
6327 *new_commit_id = NULL;
6329 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6331 err = get_fileindex_path(&fileindex_path, worktree);
6335 cc_arg.commitable_paths = &commitable_paths;
6336 cc_arg.worktree = worktree;
6338 cc_arg.have_staged_files = 0;
6340 * If possible get the status of individual files directly to
6341 * avoid crawling the entire work tree once per rebased commit.
6343 * Ideally, merged_paths would contain a list of commitables
6344 * we could use so we could skip worktree_status() entirely.
6345 * However, we would then need carefully keep track of cumulative
6346 * effects of operations such as file additions and deletions
6347 * in 'got histedit -f' (folding multiple commits into one),
6348 * and this extra complexity is not really worth it.
6351 struct got_pathlist_entry *pe;
6352 TAILQ_FOREACH(pe, merged_paths, entry) {
6353 err = worktree_status(worktree, pe->path, fileindex,
6354 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6360 err = worktree_status(worktree, "", fileindex, repo,
6361 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6366 if (TAILQ_EMPTY(&commitable_paths)) {
6367 /* No-op change; commit will be elided. */
6368 err = got_ref_delete(commit_ref, repo);
6371 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6375 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6379 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6384 logmsg = strdup(new_logmsg);
6385 if (logmsg == NULL) {
6386 err = got_error_from_errno("strdup");
6390 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6395 /* NB: commit_worktree will call free(logmsg) */
6396 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6397 NULL, worktree, got_object_commit_get_author(orig_commit),
6398 got_object_commit_get_committer(orig_commit),
6399 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6403 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6407 err = got_ref_delete(commit_ref, repo);
6411 err = update_fileindex_after_commit(worktree, &commitable_paths,
6412 *new_commit_id, fileindex, 0);
6413 sync_err = sync_fileindex(fileindex, fileindex_path);
6414 if (sync_err && err == NULL)
6417 free(fileindex_path);
6418 free(head_commit_id);
6420 got_ref_close(head_ref);
6422 free(*new_commit_id);
6423 *new_commit_id = NULL;
6428 const struct got_error *
6429 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6430 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6431 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6432 struct got_commit_object *orig_commit,
6433 struct got_object_id *orig_commit_id, struct got_repository *repo)
6435 const struct got_error *err;
6436 char *commit_ref_name;
6437 struct got_reference *commit_ref = NULL;
6438 struct got_object_id *commit_id = NULL;
6440 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6444 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6447 err = got_ref_resolve(&commit_id, repo, commit_ref);
6450 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6451 err = got_error(GOT_ERR_REBASE_COMMITID);
6455 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6456 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6459 got_ref_close(commit_ref);
6460 free(commit_ref_name);
6465 const struct got_error *
6466 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6467 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6468 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6469 struct got_commit_object *orig_commit,
6470 struct got_object_id *orig_commit_id, const char *new_logmsg,
6471 struct got_repository *repo)
6473 const struct got_error *err;
6474 char *commit_ref_name;
6475 struct got_reference *commit_ref = NULL;
6477 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6481 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6485 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6486 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6489 got_ref_close(commit_ref);
6490 free(commit_ref_name);
6494 const struct got_error *
6495 got_worktree_rebase_postpone(struct got_worktree *worktree,
6496 struct got_fileindex *fileindex)
6499 got_fileindex_free(fileindex);
6500 return lock_worktree(worktree, LOCK_SH);
6503 static const struct got_error *
6504 delete_ref(const char *name, struct got_repository *repo)
6506 const struct got_error *err;
6507 struct got_reference *ref;
6509 err = got_ref_open(&ref, repo, name, 0);
6511 if (err->code == GOT_ERR_NOT_REF)
6516 err = got_ref_delete(ref, repo);
6521 static const struct got_error *
6522 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6524 const struct got_error *err;
6525 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6526 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6528 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6531 err = delete_ref(tmp_branch_name, repo);
6535 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6538 err = delete_ref(new_base_branch_ref_name, repo);
6542 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6545 err = delete_ref(branch_ref_name, repo);
6549 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6552 err = delete_ref(commit_ref_name, repo);
6557 free(tmp_branch_name);
6558 free(new_base_branch_ref_name);
6559 free(branch_ref_name);
6560 free(commit_ref_name);
6564 const struct got_error *
6565 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6566 struct got_object_id *new_commit_id, struct got_repository *repo)
6568 const struct got_error *err;
6569 struct got_reference *ref = NULL;
6570 struct got_object_id *old_commit_id = NULL;
6571 const char *branch_name = NULL;
6572 char *new_id_str = NULL;
6573 char *refname = NULL;
6575 branch_name = got_ref_get_name(branch);
6576 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6577 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6580 err = got_object_id_str(&new_id_str, new_commit_id);
6584 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6585 new_id_str) == -1) {
6586 err = got_error_from_errno("asprintf");
6590 err = got_ref_resolve(&old_commit_id, repo, branch);
6594 err = got_ref_alloc(&ref, refname, old_commit_id);
6598 err = got_ref_write(ref, repo);
6602 free(old_commit_id);
6608 const struct got_error *
6609 got_worktree_rebase_complete(struct got_worktree *worktree,
6610 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6611 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6612 struct got_repository *repo, int create_backup)
6614 const struct got_error *err, *unlockerr, *sync_err;
6615 struct got_object_id *new_head_commit_id = NULL;
6616 char *fileindex_path = NULL;
6618 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6622 if (create_backup) {
6623 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6624 rebased_branch, new_head_commit_id, repo);
6629 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6633 err = got_ref_write(rebased_branch, repo);
6637 err = got_worktree_set_head_ref(worktree, rebased_branch);
6641 err = delete_rebase_refs(worktree, repo);
6645 err = get_fileindex_path(&fileindex_path, worktree);
6648 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6649 sync_err = sync_fileindex(fileindex, fileindex_path);
6650 if (sync_err && err == NULL)
6653 got_fileindex_free(fileindex);
6654 free(fileindex_path);
6655 free(new_head_commit_id);
6656 unlockerr = lock_worktree(worktree, LOCK_SH);
6657 if (unlockerr && err == NULL)
6662 const struct got_error *
6663 got_worktree_rebase_abort(struct got_worktree *worktree,
6664 struct got_fileindex *fileindex, struct got_repository *repo,
6665 struct got_reference *new_base_branch,
6666 got_worktree_checkout_cb progress_cb, void *progress_arg)
6668 const struct got_error *err, *unlockerr, *sync_err;
6669 struct got_reference *resolved = NULL;
6670 struct got_object_id *commit_id = NULL;
6671 char *fileindex_path = NULL;
6672 struct revert_file_args rfa;
6673 struct got_object_id *tree_id = NULL;
6675 err = lock_worktree(worktree, LOCK_EX);
6679 err = got_ref_open(&resolved, repo,
6680 got_ref_get_symref_target(new_base_branch), 0);
6684 err = got_worktree_set_head_ref(worktree, resolved);
6689 * XXX commits to the base branch could have happened while
6690 * we were busy rebasing; should we store the original commit ID
6691 * when rebase begins and read it back here?
6693 err = got_ref_resolve(&commit_id, repo, resolved);
6697 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6701 err = got_object_id_by_path(&tree_id, repo,
6702 worktree->base_commit_id, worktree->path_prefix);
6706 err = delete_rebase_refs(worktree, repo);
6710 err = get_fileindex_path(&fileindex_path, worktree);
6714 rfa.worktree = worktree;
6715 rfa.fileindex = fileindex;
6716 rfa.progress_cb = progress_cb;
6717 rfa.progress_arg = progress_arg;
6718 rfa.patch_cb = NULL;
6719 rfa.patch_arg = NULL;
6721 rfa.unlink_added_files = 0;
6722 err = worktree_status(worktree, "", fileindex, repo,
6723 revert_file, &rfa, NULL, NULL, 1, 0);
6727 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6728 repo, progress_cb, progress_arg, NULL, NULL);
6730 sync_err = sync_fileindex(fileindex, fileindex_path);
6731 if (sync_err && err == NULL)
6734 got_ref_close(resolved);
6738 got_fileindex_free(fileindex);
6739 free(fileindex_path);
6741 unlockerr = lock_worktree(worktree, LOCK_SH);
6742 if (unlockerr && err == NULL)
6747 const struct got_error *
6748 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6749 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6750 struct got_fileindex **fileindex, struct got_worktree *worktree,
6751 struct got_repository *repo)
6753 const struct got_error *err = NULL;
6754 char *tmp_branch_name = NULL;
6755 char *branch_ref_name = NULL;
6756 char *base_commit_ref_name = NULL;
6757 char *fileindex_path = NULL;
6758 struct check_rebase_ok_arg ok_arg;
6759 struct got_reference *wt_branch = NULL;
6760 struct got_reference *base_commit_ref = NULL;
6764 *base_commit_id = NULL;
6767 err = lock_worktree(worktree, LOCK_EX);
6771 err = open_fileindex(fileindex, &fileindex_path, worktree);
6775 ok_arg.worktree = worktree;
6777 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6782 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6786 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6790 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6795 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6800 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6804 err = got_ref_write(*branch_ref, repo);
6808 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6809 worktree->base_commit_id);
6812 err = got_ref_write(base_commit_ref, repo);
6815 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6816 if (*base_commit_id == NULL) {
6817 err = got_error_from_errno("got_object_id_dup");
6821 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6822 worktree->base_commit_id);
6825 err = got_ref_write(*tmp_branch, repo);
6829 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6833 free(fileindex_path);
6834 free(tmp_branch_name);
6835 free(branch_ref_name);
6836 free(base_commit_ref_name);
6838 got_ref_close(wt_branch);
6841 got_ref_close(*branch_ref);
6845 got_ref_close(*tmp_branch);
6848 free(*base_commit_id);
6850 got_fileindex_free(*fileindex);
6853 lock_worktree(worktree, LOCK_SH);
6858 const struct got_error *
6859 got_worktree_histedit_postpone(struct got_worktree *worktree,
6860 struct got_fileindex *fileindex)
6863 got_fileindex_free(fileindex);
6864 return lock_worktree(worktree, LOCK_SH);
6867 const struct got_error *
6868 got_worktree_histedit_in_progress(int *in_progress,
6869 struct got_worktree *worktree)
6871 const struct got_error *err;
6872 char *tmp_branch_name = NULL;
6874 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6878 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6879 free(tmp_branch_name);
6883 const struct got_error *
6884 got_worktree_histedit_continue(struct got_object_id **commit_id,
6885 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6886 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6887 struct got_worktree *worktree, struct got_repository *repo)
6889 const struct got_error *err;
6890 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6891 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6892 struct got_reference *commit_ref = NULL;
6893 struct got_reference *base_commit_ref = NULL;
6894 char *fileindex_path = NULL;
6895 int have_staged_files = 0;
6899 *base_commit_id = NULL;
6902 err = lock_worktree(worktree, LOCK_EX);
6906 err = open_fileindex(fileindex, &fileindex_path, worktree);
6910 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6911 &have_staged_files);
6912 if (err && err->code != GOT_ERR_CANCELLED)
6914 if (have_staged_files) {
6915 err = got_error(GOT_ERR_STAGED_PATHS);
6919 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6923 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6927 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6931 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6936 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6940 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6943 err = got_ref_resolve(commit_id, repo, commit_ref);
6947 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6950 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6954 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6958 free(commit_ref_name);
6959 free(branch_ref_name);
6960 free(fileindex_path);
6962 got_ref_close(commit_ref);
6963 if (base_commit_ref)
6964 got_ref_close(base_commit_ref);
6968 free(*base_commit_id);
6969 *base_commit_id = NULL;
6971 got_ref_close(*tmp_branch);
6975 got_fileindex_free(*fileindex);
6978 lock_worktree(worktree, LOCK_EX);
6983 static const struct got_error *
6984 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6986 const struct got_error *err;
6987 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6988 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6990 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6993 err = delete_ref(tmp_branch_name, repo);
6997 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7001 err = delete_ref(base_commit_ref_name, repo);
7005 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7008 err = delete_ref(branch_ref_name, repo);
7012 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7015 err = delete_ref(commit_ref_name, repo);
7019 free(tmp_branch_name);
7020 free(base_commit_ref_name);
7021 free(branch_ref_name);
7022 free(commit_ref_name);
7026 const struct got_error *
7027 got_worktree_histedit_abort(struct got_worktree *worktree,
7028 struct got_fileindex *fileindex, struct got_repository *repo,
7029 struct got_reference *branch, struct got_object_id *base_commit_id,
7030 got_worktree_checkout_cb progress_cb, void *progress_arg)
7032 const struct got_error *err, *unlockerr, *sync_err;
7033 struct got_reference *resolved = NULL;
7034 char *fileindex_path = NULL;
7035 struct got_object_id *tree_id = NULL;
7036 struct revert_file_args rfa;
7038 err = lock_worktree(worktree, LOCK_EX);
7042 err = got_ref_open(&resolved, repo,
7043 got_ref_get_symref_target(branch), 0);
7047 err = got_worktree_set_head_ref(worktree, resolved);
7051 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7055 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
7056 worktree->path_prefix);
7060 err = delete_histedit_refs(worktree, repo);
7064 err = get_fileindex_path(&fileindex_path, worktree);
7068 rfa.worktree = worktree;
7069 rfa.fileindex = fileindex;
7070 rfa.progress_cb = progress_cb;
7071 rfa.progress_arg = progress_arg;
7072 rfa.patch_cb = NULL;
7073 rfa.patch_arg = NULL;
7075 rfa.unlink_added_files = 0;
7076 err = worktree_status(worktree, "", fileindex, repo,
7077 revert_file, &rfa, NULL, NULL, 1, 0);
7081 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7082 repo, progress_cb, progress_arg, NULL, NULL);
7084 sync_err = sync_fileindex(fileindex, fileindex_path);
7085 if (sync_err && err == NULL)
7088 got_ref_close(resolved);
7090 free(fileindex_path);
7092 unlockerr = lock_worktree(worktree, LOCK_SH);
7093 if (unlockerr && err == NULL)
7098 const struct got_error *
7099 got_worktree_histedit_complete(struct got_worktree *worktree,
7100 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7101 struct got_reference *edited_branch, struct got_repository *repo)
7103 const struct got_error *err, *unlockerr, *sync_err;
7104 struct got_object_id *new_head_commit_id = NULL;
7105 struct got_reference *resolved = NULL;
7106 char *fileindex_path = NULL;
7108 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7112 err = got_ref_open(&resolved, repo,
7113 got_ref_get_symref_target(edited_branch), 0);
7117 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7118 resolved, new_head_commit_id, repo);
7122 err = got_ref_change_ref(resolved, new_head_commit_id);
7126 err = got_ref_write(resolved, repo);
7130 err = got_worktree_set_head_ref(worktree, resolved);
7134 err = delete_histedit_refs(worktree, repo);
7138 err = get_fileindex_path(&fileindex_path, worktree);
7141 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7142 sync_err = sync_fileindex(fileindex, fileindex_path);
7143 if (sync_err && err == NULL)
7146 got_fileindex_free(fileindex);
7147 free(fileindex_path);
7148 free(new_head_commit_id);
7149 unlockerr = lock_worktree(worktree, LOCK_SH);
7150 if (unlockerr && err == NULL)
7155 const struct got_error *
7156 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7157 struct got_object_id *commit_id, struct got_repository *repo)
7159 const struct got_error *err;
7160 char *commit_ref_name;
7162 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7166 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7170 err = delete_ref(commit_ref_name, repo);
7172 free(commit_ref_name);
7176 const struct got_error *
7177 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7178 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7179 struct got_worktree *worktree, const char *refname,
7180 struct got_repository *repo)
7182 const struct got_error *err = NULL;
7183 char *fileindex_path = NULL;
7184 struct check_rebase_ok_arg ok_arg;
7188 *base_branch_ref = NULL;
7190 err = lock_worktree(worktree, LOCK_EX);
7194 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7195 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7196 "cannot integrate a branch into itself; "
7197 "update -b or different branch name required");
7201 err = open_fileindex(fileindex, &fileindex_path, worktree);
7205 /* Preconditions are the same as for rebase. */
7206 ok_arg.worktree = worktree;
7208 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7213 err = got_ref_open(branch_ref, repo, refname, 1);
7217 err = got_ref_open(base_branch_ref, repo,
7218 got_worktree_get_head_ref_name(worktree), 1);
7222 got_ref_close(*branch_ref);
7225 if (*base_branch_ref) {
7226 got_ref_close(*base_branch_ref);
7227 *base_branch_ref = NULL;
7230 got_fileindex_free(*fileindex);
7233 lock_worktree(worktree, LOCK_SH);
7238 const struct got_error *
7239 got_worktree_integrate_continue(struct got_worktree *worktree,
7240 struct got_fileindex *fileindex, struct got_repository *repo,
7241 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7242 got_worktree_checkout_cb progress_cb, void *progress_arg,
7243 got_cancel_cb cancel_cb, void *cancel_arg)
7245 const struct got_error *err = NULL, *sync_err, *unlockerr;
7246 char *fileindex_path = NULL;
7247 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7249 err = get_fileindex_path(&fileindex_path, worktree);
7253 err = got_ref_resolve(&commit_id, repo, branch_ref);
7257 err = got_object_id_by_path(&tree_id, repo, commit_id,
7258 worktree->path_prefix);
7262 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7266 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7267 progress_cb, progress_arg, cancel_cb, cancel_arg);
7271 err = got_ref_change_ref(base_branch_ref, commit_id);
7275 err = got_ref_write(base_branch_ref, repo);
7279 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7281 sync_err = sync_fileindex(fileindex, fileindex_path);
7282 if (sync_err && err == NULL)
7286 unlockerr = got_ref_unlock(branch_ref);
7287 if (unlockerr && err == NULL)
7289 got_ref_close(branch_ref);
7291 unlockerr = got_ref_unlock(base_branch_ref);
7292 if (unlockerr && err == NULL)
7294 got_ref_close(base_branch_ref);
7296 got_fileindex_free(fileindex);
7297 free(fileindex_path);
7300 unlockerr = lock_worktree(worktree, LOCK_SH);
7301 if (unlockerr && err == NULL)
7306 const struct got_error *
7307 got_worktree_integrate_abort(struct got_worktree *worktree,
7308 struct got_fileindex *fileindex, struct got_repository *repo,
7309 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7311 const struct got_error *err = NULL, *unlockerr = NULL;
7313 got_fileindex_free(fileindex);
7315 err = lock_worktree(worktree, LOCK_SH);
7317 unlockerr = got_ref_unlock(branch_ref);
7318 if (unlockerr && err == NULL)
7320 got_ref_close(branch_ref);
7322 unlockerr = got_ref_unlock(base_branch_ref);
7323 if (unlockerr && err == NULL)
7325 got_ref_close(base_branch_ref);
7330 const struct got_error *
7331 got_worktree_merge_postpone(struct got_worktree *worktree,
7332 struct got_fileindex *fileindex)
7334 const struct got_error *err, *sync_err;
7335 char *fileindex_path = NULL;
7337 err = get_fileindex_path(&fileindex_path, worktree);
7341 sync_err = sync_fileindex(fileindex, fileindex_path);
7343 err = lock_worktree(worktree, LOCK_SH);
7344 if (sync_err && err == NULL)
7347 got_fileindex_free(fileindex);
7348 free(fileindex_path);
7352 static const struct got_error *
7353 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7355 const struct got_error *err;
7356 char *branch_refname = NULL, *commit_refname = NULL;
7358 err = get_merge_branch_ref_name(&branch_refname, worktree);
7361 err = delete_ref(branch_refname, repo);
7365 err = get_merge_commit_ref_name(&commit_refname, worktree);
7368 err = delete_ref(commit_refname, repo);
7373 free(branch_refname);
7374 free(commit_refname);
7378 struct merge_commit_msg_arg {
7379 struct got_worktree *worktree;
7380 const char *branch_name;
7383 static const struct got_error *
7384 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths, char **logmsg,
7387 struct merge_commit_msg_arg *a = arg;
7389 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7390 got_worktree_get_head_ref_name(a->worktree)) == -1)
7391 return got_error_from_errno("asprintf");
7397 const struct got_error *
7398 got_worktree_merge_branch(struct got_worktree *worktree,
7399 struct got_fileindex *fileindex,
7400 struct got_object_id *yca_commit_id,
7401 struct got_object_id *branch_tip,
7402 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7403 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7405 const struct got_error *err;
7406 char *fileindex_path = NULL;
7408 err = get_fileindex_path(&fileindex_path, worktree);
7412 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7417 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7418 branch_tip, repo, progress_cb, progress_arg,
7419 cancel_cb, cancel_arg);
7421 free(fileindex_path);
7425 const struct got_error *
7426 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7427 struct got_worktree *worktree, struct got_fileindex *fileindex,
7428 const char *author, const char *committer, int allow_bad_symlinks,
7429 struct got_object_id *branch_tip, const char *branch_name,
7430 struct got_repository *repo,
7431 got_worktree_status_cb status_cb, void *status_arg)
7434 const struct got_error *err = NULL, *sync_err;
7435 struct got_pathlist_head commitable_paths;
7436 struct collect_commitables_arg cc_arg;
7437 struct got_pathlist_entry *pe;
7438 struct got_reference *head_ref = NULL;
7439 struct got_object_id *head_commit_id = NULL;
7440 int have_staged_files = 0;
7441 struct merge_commit_msg_arg mcm_arg;
7442 char *fileindex_path = NULL;
7444 *new_commit_id = NULL;
7446 TAILQ_INIT(&commitable_paths);
7448 err = get_fileindex_path(&fileindex_path, worktree);
7452 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7456 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7460 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7461 &have_staged_files);
7462 if (err && err->code != GOT_ERR_CANCELLED)
7464 if (have_staged_files) {
7465 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7469 cc_arg.commitable_paths = &commitable_paths;
7470 cc_arg.worktree = worktree;
7471 cc_arg.fileindex = fileindex;
7473 cc_arg.have_staged_files = have_staged_files;
7474 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7475 err = worktree_status(worktree, "", fileindex, repo,
7476 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7480 if (TAILQ_EMPTY(&commitable_paths)) {
7481 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7482 "merge of %s cannot proceed", branch_name);
7486 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7487 struct got_commitable *ct = pe->data;
7488 const char *ct_path = ct->in_repo_path;
7490 while (ct_path[0] == '/')
7492 err = check_out_of_date(ct_path, ct->status,
7493 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7494 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7500 mcm_arg.worktree = worktree;
7501 mcm_arg.branch_name = branch_name;
7502 err = commit_worktree(new_commit_id, &commitable_paths,
7503 head_commit_id, branch_tip, worktree, author, committer,
7504 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7508 err = update_fileindex_after_commit(worktree, &commitable_paths,
7509 *new_commit_id, fileindex, have_staged_files);
7510 sync_err = sync_fileindex(fileindex, fileindex_path);
7511 if (sync_err && err == NULL)
7514 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7515 struct got_commitable *ct = pe->data;
7516 free_commitable(ct);
7518 got_pathlist_free(&commitable_paths);
7519 free(fileindex_path);
7523 const struct got_error *
7524 got_worktree_merge_complete(struct got_worktree *worktree,
7525 struct got_fileindex *fileindex, struct got_repository *repo)
7527 const struct got_error *err, *unlockerr, *sync_err;
7528 char *fileindex_path = NULL;
7530 err = delete_merge_refs(worktree, repo);
7534 err = get_fileindex_path(&fileindex_path, worktree);
7537 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7538 sync_err = sync_fileindex(fileindex, fileindex_path);
7539 if (sync_err && err == NULL)
7542 got_fileindex_free(fileindex);
7543 free(fileindex_path);
7544 unlockerr = lock_worktree(worktree, LOCK_SH);
7545 if (unlockerr && err == NULL)
7550 const struct got_error *
7551 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7552 struct got_repository *repo)
7554 const struct got_error *err;
7555 char *branch_refname = NULL;
7556 struct got_reference *branch_ref = NULL;
7560 err = get_merge_branch_ref_name(&branch_refname, worktree);
7563 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7564 free(branch_refname);
7566 if (err->code != GOT_ERR_NOT_REF)
7574 const struct got_error *got_worktree_merge_prepare(
7575 struct got_fileindex **fileindex, struct got_worktree *worktree,
7576 struct got_reference *branch, struct got_repository *repo)
7578 const struct got_error *err = NULL;
7579 char *fileindex_path = NULL;
7580 char *branch_refname = NULL, *commit_refname = NULL;
7581 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
7582 struct got_reference *commit_ref = NULL;
7583 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
7584 struct check_rebase_ok_arg ok_arg;
7588 err = lock_worktree(worktree, LOCK_EX);
7592 err = open_fileindex(fileindex, &fileindex_path, worktree);
7596 /* Preconditions are the same as for rebase. */
7597 ok_arg.worktree = worktree;
7599 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7604 err = get_merge_branch_ref_name(&branch_refname, worktree);
7608 err = get_merge_commit_ref_name(&commit_refname, worktree);
7612 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7617 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
7621 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
7622 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
7626 err = got_ref_resolve(&branch_tip, repo, branch);
7630 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
7633 err = got_ref_write(branch_ref, repo);
7637 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
7640 err = got_ref_write(commit_ref, repo);
7645 free(branch_refname);
7646 free(commit_refname);
7647 free(fileindex_path);
7649 got_ref_close(branch_ref);
7651 got_ref_close(commit_ref);
7653 got_ref_close(wt_branch);
7654 free(wt_branch_tip);
7657 got_fileindex_free(*fileindex);
7660 lock_worktree(worktree, LOCK_SH);
7665 const struct got_error *
7666 got_worktree_merge_continue(char **branch_name,
7667 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
7668 struct got_worktree *worktree, struct got_repository *repo)
7670 const struct got_error *err;
7671 char *commit_refname = NULL, *branch_refname = NULL;
7672 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
7673 char *fileindex_path = NULL;
7674 int have_staged_files = 0;
7676 *branch_name = NULL;
7680 err = lock_worktree(worktree, LOCK_EX);
7684 err = open_fileindex(fileindex, &fileindex_path, worktree);
7688 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7689 &have_staged_files);
7690 if (err && err->code != GOT_ERR_CANCELLED)
7692 if (have_staged_files) {
7693 err = got_error(GOT_ERR_STAGED_PATHS);
7697 err = get_merge_branch_ref_name(&branch_refname, worktree);
7701 err = get_merge_commit_ref_name(&commit_refname, worktree);
7705 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7709 if (!got_ref_is_symbolic(branch_ref)) {
7710 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7711 "%s is not a symbolic reference",
7712 got_ref_get_name(branch_ref));
7715 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
7716 if (*branch_name == NULL) {
7717 err = got_error_from_errno("strdup");
7721 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
7725 err = got_ref_resolve(branch_tip, repo, commit_ref);
7729 free(commit_refname);
7730 free(branch_refname);
7731 free(fileindex_path);
7733 got_ref_close(commit_ref);
7735 got_ref_close(branch_ref);
7739 *branch_name = NULL;
7744 got_fileindex_free(*fileindex);
7747 lock_worktree(worktree, LOCK_SH);
7752 const struct got_error *
7753 got_worktree_merge_abort(struct got_worktree *worktree,
7754 struct got_fileindex *fileindex, struct got_repository *repo,
7755 got_worktree_checkout_cb progress_cb, void *progress_arg)
7757 const struct got_error *err, *unlockerr, *sync_err;
7758 struct got_object_id *commit_id = NULL;
7759 char *fileindex_path = NULL;
7760 struct revert_file_args rfa;
7761 struct got_object_id *tree_id = NULL;
7763 err = got_object_id_by_path(&tree_id, repo,
7764 worktree->base_commit_id, worktree->path_prefix);
7768 err = delete_merge_refs(worktree, repo);
7772 err = get_fileindex_path(&fileindex_path, worktree);
7776 rfa.worktree = worktree;
7777 rfa.fileindex = fileindex;
7778 rfa.progress_cb = progress_cb;
7779 rfa.progress_arg = progress_arg;
7780 rfa.patch_cb = NULL;
7781 rfa.patch_arg = NULL;
7783 rfa.unlink_added_files = 1;
7784 err = worktree_status(worktree, "", fileindex, repo,
7785 revert_file, &rfa, NULL, NULL, 1, 0);
7789 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7790 repo, progress_cb, progress_arg, NULL, NULL);
7792 sync_err = sync_fileindex(fileindex, fileindex_path);
7793 if (sync_err && err == NULL)
7799 got_fileindex_free(fileindex);
7800 free(fileindex_path);
7802 unlockerr = lock_worktree(worktree, LOCK_SH);
7803 if (unlockerr && err == NULL)
7808 struct check_stage_ok_arg {
7809 struct got_object_id *head_commit_id;
7810 struct got_worktree *worktree;
7811 struct got_fileindex *fileindex;
7812 struct got_repository *repo;
7816 const struct got_error *
7817 check_stage_ok(void *arg, unsigned char status,
7818 unsigned char staged_status, const char *relpath,
7819 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7820 struct got_object_id *commit_id, int dirfd, const char *de_name)
7822 struct check_stage_ok_arg *a = arg;
7823 const struct got_error *err = NULL;
7824 struct got_fileindex_entry *ie;
7825 struct got_object_id base_commit_id;
7826 struct got_object_id *base_commit_idp = NULL;
7827 char *in_repo_path = NULL, *p;
7829 if (status == GOT_STATUS_UNVERSIONED ||
7830 status == GOT_STATUS_NO_CHANGE)
7832 if (status == GOT_STATUS_NONEXISTENT)
7833 return got_error_set_errno(ENOENT, relpath);
7835 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7837 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7839 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7840 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7842 return got_error_from_errno("asprintf");
7844 if (got_fileindex_entry_has_commit(ie)) {
7845 memcpy(base_commit_id.sha1, ie->commit_sha1,
7846 SHA1_DIGEST_LENGTH);
7847 base_commit_idp = &base_commit_id;
7850 if (status == GOT_STATUS_CONFLICT) {
7851 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7853 } else if (status != GOT_STATUS_ADD &&
7854 status != GOT_STATUS_MODIFY &&
7855 status != GOT_STATUS_DELETE) {
7856 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7860 a->have_changes = 1;
7865 err = check_out_of_date(p, status, staged_status,
7866 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7867 GOT_ERR_STAGE_OUT_OF_DATE);
7873 struct stage_path_arg {
7874 struct got_worktree *worktree;
7875 struct got_fileindex *fileindex;
7876 struct got_repository *repo;
7877 got_worktree_status_cb status_cb;
7879 got_worktree_patch_cb patch_cb;
7881 int staged_something;
7882 int allow_bad_symlinks;
7885 static const struct got_error *
7886 stage_path(void *arg, unsigned char status,
7887 unsigned char staged_status, const char *relpath,
7888 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7889 struct got_object_id *commit_id, int dirfd, const char *de_name)
7891 struct stage_path_arg *a = arg;
7892 const struct got_error *err = NULL;
7893 struct got_fileindex_entry *ie;
7894 char *ondisk_path = NULL, *path_content = NULL;
7896 struct got_object_id *new_staged_blob_id = NULL;
7899 if (status == GOT_STATUS_UNVERSIONED)
7902 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7904 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7906 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7908 return got_error_from_errno("asprintf");
7911 case GOT_STATUS_ADD:
7912 case GOT_STATUS_MODIFY:
7913 /* XXX could sb.st_mode be passed in by our caller? */
7914 if (lstat(ondisk_path, &sb) == -1) {
7915 err = got_error_from_errno2("lstat", ondisk_path);
7919 if (status == GOT_STATUS_ADD) {
7920 int choice = GOT_PATCH_CHOICE_NONE;
7921 err = (*a->patch_cb)(&choice, a->patch_arg,
7922 status, ie->path, NULL, 1, 1);
7925 if (choice != GOT_PATCH_CHOICE_YES)
7928 err = create_patched_content(&path_content, 0,
7929 staged_blob_id ? staged_blob_id : blob_id,
7930 ondisk_path, dirfd, de_name, ie->path,
7931 a->repo, a->patch_cb, a->patch_arg);
7932 if (err || path_content == NULL)
7936 err = got_object_blob_create(&new_staged_blob_id,
7937 path_content ? path_content : ondisk_path, a->repo);
7940 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7941 SHA1_DIGEST_LENGTH);
7942 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7943 stage = GOT_FILEIDX_STAGE_ADD;
7945 stage = GOT_FILEIDX_STAGE_MODIFY;
7946 got_fileindex_entry_stage_set(ie, stage);
7947 if (S_ISLNK(sb.st_mode)) {
7948 int is_bad_symlink = 0;
7949 if (!a->allow_bad_symlinks) {
7950 char target_path[PATH_MAX];
7952 target_len = readlink(ondisk_path, target_path,
7953 sizeof(target_path));
7954 if (target_len == -1) {
7955 err = got_error_from_errno2("readlink",
7959 err = is_bad_symlink_target(&is_bad_symlink,
7960 target_path, target_len, ondisk_path,
7961 a->worktree->root_path);
7964 if (is_bad_symlink) {
7965 err = got_error_path(ondisk_path,
7966 GOT_ERR_BAD_SYMLINK);
7971 got_fileindex_entry_staged_filetype_set(ie,
7972 GOT_FILEIDX_MODE_BAD_SYMLINK);
7974 got_fileindex_entry_staged_filetype_set(ie,
7975 GOT_FILEIDX_MODE_SYMLINK);
7977 got_fileindex_entry_staged_filetype_set(ie,
7978 GOT_FILEIDX_MODE_REGULAR_FILE);
7980 a->staged_something = 1;
7981 if (a->status_cb == NULL)
7983 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7984 get_staged_status(ie), relpath, blob_id,
7985 new_staged_blob_id, NULL, dirfd, de_name);
7987 case GOT_STATUS_DELETE:
7988 if (staged_status == GOT_STATUS_DELETE)
7991 int choice = GOT_PATCH_CHOICE_NONE;
7992 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7993 ie->path, NULL, 1, 1);
7996 if (choice == GOT_PATCH_CHOICE_NO)
7998 if (choice != GOT_PATCH_CHOICE_YES) {
7999 err = got_error(GOT_ERR_PATCH_CHOICE);
8003 stage = GOT_FILEIDX_STAGE_DELETE;
8004 got_fileindex_entry_stage_set(ie, stage);
8005 a->staged_something = 1;
8006 if (a->status_cb == NULL)
8008 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8009 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8012 case GOT_STATUS_NO_CHANGE:
8014 case GOT_STATUS_CONFLICT:
8015 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8017 case GOT_STATUS_NONEXISTENT:
8018 err = got_error_set_errno(ENOENT, relpath);
8021 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8025 if (path_content && unlink(path_content) == -1 && err == NULL)
8026 err = got_error_from_errno2("unlink", path_content);
8029 free(new_staged_blob_id);
8033 const struct got_error *
8034 got_worktree_stage(struct got_worktree *worktree,
8035 struct got_pathlist_head *paths,
8036 got_worktree_status_cb status_cb, void *status_arg,
8037 got_worktree_patch_cb patch_cb, void *patch_arg,
8038 int allow_bad_symlinks, struct got_repository *repo)
8040 const struct got_error *err = NULL, *sync_err, *unlockerr;
8041 struct got_pathlist_entry *pe;
8042 struct got_fileindex *fileindex = NULL;
8043 char *fileindex_path = NULL;
8044 struct got_reference *head_ref = NULL;
8045 struct got_object_id *head_commit_id = NULL;
8046 struct check_stage_ok_arg oka;
8047 struct stage_path_arg spa;
8049 err = lock_worktree(worktree, LOCK_EX);
8053 err = got_ref_open(&head_ref, repo,
8054 got_worktree_get_head_ref_name(worktree), 0);
8057 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8060 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8064 /* Check pre-conditions before staging anything. */
8065 oka.head_commit_id = head_commit_id;
8066 oka.worktree = worktree;
8067 oka.fileindex = fileindex;
8069 oka.have_changes = 0;
8070 TAILQ_FOREACH(pe, paths, entry) {
8071 err = worktree_status(worktree, pe->path, fileindex, repo,
8072 check_stage_ok, &oka, NULL, NULL, 1, 0);
8076 if (!oka.have_changes) {
8077 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8081 spa.worktree = worktree;
8082 spa.fileindex = fileindex;
8084 spa.patch_cb = patch_cb;
8085 spa.patch_arg = patch_arg;
8086 spa.status_cb = status_cb;
8087 spa.status_arg = status_arg;
8088 spa.staged_something = 0;
8089 spa.allow_bad_symlinks = allow_bad_symlinks;
8090 TAILQ_FOREACH(pe, paths, entry) {
8091 err = worktree_status(worktree, pe->path, fileindex, repo,
8092 stage_path, &spa, NULL, NULL, 1, 0);
8096 if (!spa.staged_something) {
8097 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8101 sync_err = sync_fileindex(fileindex, fileindex_path);
8102 if (sync_err && err == NULL)
8106 got_ref_close(head_ref);
8107 free(head_commit_id);
8108 free(fileindex_path);
8110 got_fileindex_free(fileindex);
8111 unlockerr = lock_worktree(worktree, LOCK_SH);
8112 if (unlockerr && err == NULL)
8117 struct unstage_path_arg {
8118 struct got_worktree *worktree;
8119 struct got_fileindex *fileindex;
8120 struct got_repository *repo;
8121 got_worktree_checkout_cb progress_cb;
8123 got_worktree_patch_cb patch_cb;
8127 static const struct got_error *
8128 create_unstaged_content(char **path_unstaged_content,
8129 char **path_new_staged_content, struct got_object_id *blob_id,
8130 struct got_object_id *staged_blob_id, const char *relpath,
8131 struct got_repository *repo,
8132 got_worktree_patch_cb patch_cb, void *patch_arg)
8134 const struct got_error *err, *free_err;
8135 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8136 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8137 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8138 struct got_diffreg_result *diffreg_result = NULL;
8139 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8140 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8142 *path_unstaged_content = NULL;
8143 *path_new_staged_content = NULL;
8145 err = got_object_id_str(&label1, blob_id);
8148 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
8152 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
8156 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8160 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
8164 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
8168 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8172 err = got_diff_files(&diffreg_result, f1, label1, f2,
8173 path2, 3, 0, 1, NULL);
8177 err = got_opentemp_named(path_unstaged_content, &outfile,
8178 "got-unstaged-content");
8181 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8182 "got-new-staged-content");
8186 if (fseek(f1, 0L, SEEK_SET) == -1) {
8187 err = got_ferror(f1, GOT_ERR_IO);
8190 if (fseek(f2, 0L, SEEK_SET) == -1) {
8191 err = got_ferror(f2, GOT_ERR_IO);
8194 /* Count the number of actual changes in the diff result. */
8195 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8196 struct diff_chunk_context cc = {};
8197 diff_chunk_context_load_change(&cc, &nchunks_used,
8198 diffreg_result->result, n, 0);
8201 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8203 err = apply_or_reject_change(&choice, &nchunks_used,
8204 diffreg_result->result, n, relpath, f1, f2,
8205 &line_cur1, &line_cur2,
8206 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8209 if (choice == GOT_PATCH_CHOICE_YES)
8212 have_rejected_content = 1;
8213 if (choice == GOT_PATCH_CHOICE_QUIT)
8216 if (have_content || have_rejected_content)
8217 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8218 outfile, rejectfile);
8222 got_object_blob_close(blob);
8224 got_object_blob_close(staged_blob);
8225 free_err = got_diffreg_result_free(diffreg_result);
8226 if (free_err && err == NULL)
8228 if (f1 && fclose(f1) == EOF && err == NULL)
8229 err = got_error_from_errno2("fclose", path1);
8230 if (f2 && fclose(f2) == EOF && err == NULL)
8231 err = got_error_from_errno2("fclose", path2);
8232 if (outfile && fclose(outfile) == EOF && err == NULL)
8233 err = got_error_from_errno2("fclose", *path_unstaged_content);
8234 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8235 err = got_error_from_errno2("fclose", *path_new_staged_content);
8236 if (path1 && unlink(path1) == -1 && err == NULL)
8237 err = got_error_from_errno2("unlink", path1);
8238 if (path2 && unlink(path2) == -1 && err == NULL)
8239 err = got_error_from_errno2("unlink", path2);
8240 if (err || !have_content) {
8241 if (*path_unstaged_content &&
8242 unlink(*path_unstaged_content) == -1 && err == NULL)
8243 err = got_error_from_errno2("unlink",
8244 *path_unstaged_content);
8245 free(*path_unstaged_content);
8246 *path_unstaged_content = NULL;
8248 if (err || !have_content || !have_rejected_content) {
8249 if (*path_new_staged_content &&
8250 unlink(*path_new_staged_content) == -1 && err == NULL)
8251 err = got_error_from_errno2("unlink",
8252 *path_new_staged_content);
8253 free(*path_new_staged_content);
8254 *path_new_staged_content = NULL;
8261 static const struct got_error *
8262 unstage_hunks(struct got_object_id *staged_blob_id,
8263 struct got_blob_object *blob_base,
8264 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8265 const char *ondisk_path, const char *label_orig,
8266 struct got_worktree *worktree, struct got_repository *repo,
8267 got_worktree_patch_cb patch_cb, void *patch_arg,
8268 got_worktree_checkout_cb progress_cb, void *progress_arg)
8270 const struct got_error *err = NULL;
8271 char *path_unstaged_content = NULL;
8272 char *path_new_staged_content = NULL;
8273 char *parent = NULL, *base_path = NULL;
8274 char *blob_base_path = NULL;
8275 struct got_object_id *new_staged_blob_id = NULL;
8276 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8279 err = create_unstaged_content(&path_unstaged_content,
8280 &path_new_staged_content, blob_id, staged_blob_id,
8281 ie->path, repo, patch_cb, patch_arg);
8285 if (path_unstaged_content == NULL)
8288 if (path_new_staged_content) {
8289 err = got_object_blob_create(&new_staged_blob_id,
8290 path_new_staged_content, repo);
8295 f = fopen(path_unstaged_content, "re");
8297 err = got_error_from_errno2("fopen",
8298 path_unstaged_content);
8301 if (fstat(fileno(f), &sb) == -1) {
8302 err = got_error_from_errno2("fstat", path_unstaged_content);
8305 if (got_fileindex_entry_staged_filetype_get(ie) ==
8306 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8307 char link_target[PATH_MAX];
8309 r = fread(link_target, 1, sizeof(link_target), f);
8310 if (r == 0 && ferror(f)) {
8311 err = got_error_from_errno("fread");
8314 if (r >= sizeof(link_target)) { /* should not happen */
8315 err = got_error(GOT_ERR_NO_SPACE);
8318 link_target[r] = '\0';
8319 err = merge_symlink(worktree, blob_base,
8320 ondisk_path, ie->path, label_orig, link_target,
8321 worktree->base_commit_id, repo, progress_cb,
8324 int local_changes_subsumed;
8326 err = got_path_dirname(&parent, ondisk_path);
8330 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8332 err = got_error_from_errno("asprintf");
8337 err = got_opentemp_named(&blob_base_path, &f_base, base_path);
8340 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8346 * In order the run a 3-way merge with a symlink we copy the symlink's
8347 * target path into a temporary file and use that file with diff3.
8349 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8350 err = dump_symlink_target_path_to_file(&f_deriv2,
8356 fd = open(ondisk_path,
8357 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8359 err = got_error_from_errno2("open", ondisk_path);
8362 f_deriv2 = fdopen(fd, "r");
8363 if (f_deriv2 == NULL) {
8364 err = got_error_from_errno2("fdopen", ondisk_path);
8370 err = merge_file(&local_changes_subsumed, worktree,
8371 f_base, f, f_deriv2, ondisk_path, ie->path,
8372 got_fileindex_perms_to_st(ie),
8373 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8374 repo, progress_cb, progress_arg);
8379 if (new_staged_blob_id) {
8380 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8381 SHA1_DIGEST_LENGTH);
8383 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8384 got_fileindex_entry_staged_filetype_set(ie, 0);
8387 free(new_staged_blob_id);
8388 if (path_unstaged_content &&
8389 unlink(path_unstaged_content) == -1 && err == NULL)
8390 err = got_error_from_errno2("unlink", path_unstaged_content);
8391 if (path_new_staged_content &&
8392 unlink(path_new_staged_content) == -1 && err == NULL)
8393 err = got_error_from_errno2("unlink", path_new_staged_content);
8394 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8395 err = got_error_from_errno2("unlink", blob_base_path);
8396 if (f_base && fclose(f_base) == EOF && err == NULL)
8397 err = got_error_from_errno2("fclose", path_unstaged_content);
8398 if (f && fclose(f) == EOF && err == NULL)
8399 err = got_error_from_errno2("fclose", path_unstaged_content);
8400 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8401 err = got_error_from_errno2("fclose", ondisk_path);
8402 free(path_unstaged_content);
8403 free(path_new_staged_content);
8404 free(blob_base_path);
8410 static const struct got_error *
8411 unstage_path(void *arg, unsigned char status,
8412 unsigned char staged_status, const char *relpath,
8413 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8414 struct got_object_id *commit_id, int dirfd, const char *de_name)
8416 const struct got_error *err = NULL;
8417 struct unstage_path_arg *a = arg;
8418 struct got_fileindex_entry *ie;
8419 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8420 char *ondisk_path = NULL;
8421 char *id_str = NULL, *label_orig = NULL;
8422 int local_changes_subsumed;
8425 if (staged_status != GOT_STATUS_ADD &&
8426 staged_status != GOT_STATUS_MODIFY &&
8427 staged_status != GOT_STATUS_DELETE)
8430 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8432 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8434 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8436 return got_error_from_errno("asprintf");
8438 err = got_object_id_str(&id_str,
8439 commit_id ? commit_id : a->worktree->base_commit_id);
8442 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8444 err = got_error_from_errno("asprintf");
8448 switch (staged_status) {
8449 case GOT_STATUS_MODIFY:
8450 err = got_object_open_as_blob(&blob_base, a->repo,
8455 case GOT_STATUS_ADD:
8457 if (staged_status == GOT_STATUS_ADD) {
8458 int choice = GOT_PATCH_CHOICE_NONE;
8459 err = (*a->patch_cb)(&choice, a->patch_arg,
8460 staged_status, ie->path, NULL, 1, 1);
8463 if (choice != GOT_PATCH_CHOICE_YES)
8466 err = unstage_hunks(staged_blob_id,
8467 blob_base, blob_id, ie, ondisk_path,
8468 label_orig, a->worktree, a->repo,
8469 a->patch_cb, a->patch_arg,
8470 a->progress_cb, a->progress_arg);
8471 break; /* Done with this file. */
8474 err = got_object_open_as_blob(&blob_staged, a->repo,
8475 staged_blob_id, 8192);
8478 switch (got_fileindex_entry_staged_filetype_get(ie)) {
8479 case GOT_FILEIDX_MODE_BAD_SYMLINK:
8480 case GOT_FILEIDX_MODE_REGULAR_FILE:
8481 err = merge_blob(&local_changes_subsumed, a->worktree,
8482 blob_base, ondisk_path, relpath,
8483 got_fileindex_perms_to_st(ie), label_orig,
8484 blob_staged, commit_id ? commit_id :
8485 a->worktree->base_commit_id, a->repo,
8486 a->progress_cb, a->progress_arg);
8488 case GOT_FILEIDX_MODE_SYMLINK:
8489 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8490 char *staged_target;
8491 err = got_object_blob_read_to_str(
8492 &staged_target, blob_staged);
8495 err = merge_symlink(a->worktree, blob_base,
8496 ondisk_path, relpath, label_orig,
8497 staged_target, commit_id ? commit_id :
8498 a->worktree->base_commit_id,
8499 a->repo, a->progress_cb, a->progress_arg);
8500 free(staged_target);
8502 err = merge_blob(&local_changes_subsumed,
8503 a->worktree, blob_base, ondisk_path,
8504 relpath, got_fileindex_perms_to_st(ie),
8505 label_orig, blob_staged,
8506 commit_id ? commit_id :
8507 a->worktree->base_commit_id, a->repo,
8508 a->progress_cb, a->progress_arg);
8512 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8516 got_fileindex_entry_stage_set(ie,
8517 GOT_FILEIDX_STAGE_NONE);
8518 got_fileindex_entry_staged_filetype_set(ie, 0);
8521 case GOT_STATUS_DELETE:
8523 int choice = GOT_PATCH_CHOICE_NONE;
8524 err = (*a->patch_cb)(&choice, a->patch_arg,
8525 staged_status, ie->path, NULL, 1, 1);
8528 if (choice == GOT_PATCH_CHOICE_NO)
8530 if (choice != GOT_PATCH_CHOICE_YES) {
8531 err = got_error(GOT_ERR_PATCH_CHOICE);
8535 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8536 got_fileindex_entry_staged_filetype_set(ie, 0);
8537 err = get_file_status(&status, &sb, ie, ondisk_path,
8538 dirfd, de_name, a->repo);
8541 err = (*a->progress_cb)(a->progress_arg, status, relpath);
8547 got_object_blob_close(blob_base);
8549 got_object_blob_close(blob_staged);
8555 const struct got_error *
8556 got_worktree_unstage(struct got_worktree *worktree,
8557 struct got_pathlist_head *paths,
8558 got_worktree_checkout_cb progress_cb, void *progress_arg,
8559 got_worktree_patch_cb patch_cb, void *patch_arg,
8560 struct got_repository *repo)
8562 const struct got_error *err = NULL, *sync_err, *unlockerr;
8563 struct got_pathlist_entry *pe;
8564 struct got_fileindex *fileindex = NULL;
8565 char *fileindex_path = NULL;
8566 struct unstage_path_arg upa;
8568 err = lock_worktree(worktree, LOCK_EX);
8572 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8576 upa.worktree = worktree;
8577 upa.fileindex = fileindex;
8579 upa.progress_cb = progress_cb;
8580 upa.progress_arg = progress_arg;
8581 upa.patch_cb = patch_cb;
8582 upa.patch_arg = patch_arg;
8583 TAILQ_FOREACH(pe, paths, entry) {
8584 err = worktree_status(worktree, pe->path, fileindex, repo,
8585 unstage_path, &upa, NULL, NULL, 1, 0);
8590 sync_err = sync_fileindex(fileindex, fileindex_path);
8591 if (sync_err && err == NULL)
8594 free(fileindex_path);
8596 got_fileindex_free(fileindex);
8597 unlockerr = lock_worktree(worktree, LOCK_SH);
8598 if (unlockerr && err == NULL)
8603 struct report_file_info_arg {
8604 struct got_worktree *worktree;
8605 got_worktree_path_info_cb info_cb;
8607 struct got_pathlist_head *paths;
8608 got_cancel_cb cancel_cb;
8612 static const struct got_error *
8613 report_file_info(void *arg, struct got_fileindex_entry *ie)
8615 struct report_file_info_arg *a = arg;
8616 struct got_pathlist_entry *pe;
8617 struct got_object_id blob_id, staged_blob_id, commit_id;
8618 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8619 struct got_object_id *commit_idp = NULL;
8622 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8623 return got_error(GOT_ERR_CANCELLED);
8625 TAILQ_FOREACH(pe, a->paths, entry) {
8626 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8627 got_path_is_child(ie->path, pe->path, pe->path_len))
8630 if (pe == NULL) /* not found */
8633 if (got_fileindex_entry_has_blob(ie)) {
8634 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8635 blob_idp = &blob_id;
8637 stage = got_fileindex_entry_stage_get(ie);
8638 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8639 stage == GOT_FILEIDX_STAGE_ADD) {
8640 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8641 SHA1_DIGEST_LENGTH);
8642 staged_blob_idp = &staged_blob_id;
8645 if (got_fileindex_entry_has_commit(ie)) {
8646 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8647 commit_idp = &commit_id;
8650 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8651 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8654 const struct got_error *
8655 got_worktree_path_info(struct got_worktree *worktree,
8656 struct got_pathlist_head *paths,
8657 got_worktree_path_info_cb info_cb, void *info_arg,
8658 got_cancel_cb cancel_cb, void *cancel_arg)
8661 const struct got_error *err = NULL, *unlockerr;
8662 struct got_fileindex *fileindex = NULL;
8663 char *fileindex_path = NULL;
8664 struct report_file_info_arg arg;
8666 err = lock_worktree(worktree, LOCK_SH);
8670 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8674 arg.worktree = worktree;
8675 arg.info_cb = info_cb;
8676 arg.info_arg = info_arg;
8678 arg.cancel_cb = cancel_cb;
8679 arg.cancel_arg = cancel_arg;
8680 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8683 free(fileindex_path);
8685 got_fileindex_free(fileindex);
8686 unlockerr = lock_worktree(worktree, LOCK_UN);
8687 if (unlockerr && err == NULL)