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, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2821 err = got_error_from_errno2("open",
2825 f_deriv = fdopen(fd, "r");
2826 if (f_deriv == NULL) {
2827 err = got_error_from_errno2("fdopen",
2832 err = got_object_id_str(&id_str, a->commit_id2);
2835 if (asprintf(&label_deriv2, "%s: commit %s",
2836 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2837 err = got_error_from_errno("asprintf");
2840 err = merge_file(&local_changes_subsumed, a->worktree,
2841 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2842 sb.st_mode, a->label_orig, NULL, label_deriv2,
2843 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2844 a->progress_cb, a->progress_arg);
2847 ie = got_fileindex_entry_get(a->fileindex, path1,
2850 return (*a->progress_cb)(a->progress_arg,
2851 GOT_STATUS_MISSING, path1);
2853 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2855 return got_error_from_errno("asprintf");
2857 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2863 case GOT_STATUS_NO_CHANGE:
2864 err = (*a->progress_cb)(a->progress_arg,
2865 GOT_STATUS_DELETE, path1);
2868 err = remove_ondisk_file(a->worktree->root_path, path1);
2872 got_fileindex_entry_mark_deleted_from_disk(ie);
2874 case GOT_STATUS_DELETE:
2875 case GOT_STATUS_MISSING:
2876 err = (*a->progress_cb)(a->progress_arg,
2877 GOT_STATUS_DELETE, path1);
2881 got_fileindex_entry_mark_deleted_from_disk(ie);
2883 case GOT_STATUS_ADD: {
2884 struct got_object_id *id;
2887 * Delete the added file only if its content already
2888 * exists in the repository.
2890 err = got_object_blob_file_create(&id, &blob1_f, path1);
2893 if (got_object_id_cmp(id, id1) == 0) {
2894 err = (*a->progress_cb)(a->progress_arg,
2895 GOT_STATUS_DELETE, path1);
2898 err = remove_ondisk_file(a->worktree->root_path,
2903 got_fileindex_entry_remove(a->fileindex,
2906 err = (*a->progress_cb)(a->progress_arg,
2907 GOT_STATUS_CANNOT_DELETE, path1);
2909 if (fclose(blob1_f) == EOF && err == NULL)
2910 err = got_error_from_errno("fclose");
2916 case GOT_STATUS_MODIFY:
2917 case GOT_STATUS_CONFLICT:
2918 err = (*a->progress_cb)(a->progress_arg,
2919 GOT_STATUS_CANNOT_DELETE, path1);
2923 case GOT_STATUS_OBSTRUCTED:
2924 err = (*a->progress_cb)(a->progress_arg, status, path1);
2932 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2934 return got_error_from_errno("asprintf");
2935 ie = got_fileindex_entry_get(a->fileindex, path2,
2938 err = get_file_status(&status, &sb, ie, ondisk_path,
2942 if (status != GOT_STATUS_NO_CHANGE &&
2943 status != GOT_STATUS_MODIFY &&
2944 status != GOT_STATUS_CONFLICT &&
2945 status != GOT_STATUS_ADD) {
2946 err = (*a->progress_cb)(a->progress_arg,
2950 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2952 err = got_object_blob_read_to_str(&link_target2,
2956 err = merge_symlink(a->worktree, NULL,
2957 ondisk_path, path2, a->label_orig,
2958 link_target2, a->commit_id2, repo,
2959 a->progress_cb, a->progress_arg);
2961 } else if (S_ISREG(sb.st_mode)) {
2962 err = merge_blob(&local_changes_subsumed,
2963 a->worktree, NULL, ondisk_path, path2,
2964 sb.st_mode, a->label_orig, blob2,
2965 a->commit_id2, repo, a->progress_cb,
2968 err = got_error_path(ondisk_path,
2969 GOT_ERR_FILE_OBSTRUCTED);
2973 if (status == GOT_STATUS_DELETE) {
2974 err = got_fileindex_entry_update(ie,
2975 a->worktree->root_fd, path2, blob2->id.sha1,
2976 a->worktree->base_commit_id->sha1, 0);
2981 int is_bad_symlink = 0;
2982 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2983 if (S_ISLNK(mode2)) {
2984 err = install_symlink(&is_bad_symlink,
2985 a->worktree, ondisk_path, path2, blob2, 0,
2986 0, 1, a->allow_bad_symlinks, repo,
2987 a->progress_cb, a->progress_arg);
2989 err = install_blob(a->worktree, ondisk_path, path2,
2990 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2991 a->progress_cb, a->progress_arg);
2995 err = got_fileindex_entry_alloc(&ie, path2);
2998 err = got_fileindex_entry_update(ie,
2999 a->worktree->root_fd, path2, NULL, NULL, 1);
3001 got_fileindex_entry_free(ie);
3004 err = got_fileindex_entry_add(a->fileindex, ie);
3006 got_fileindex_entry_free(ie);
3009 if (is_bad_symlink) {
3010 got_fileindex_entry_filetype_set(ie,
3011 GOT_FILEIDX_MODE_BAD_SYMLINK);
3016 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3017 err = got_error_from_errno("fclose");
3018 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3019 err = got_error_from_errno("fclose");
3020 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3021 err = got_error_from_errno("fclose");
3028 static const struct got_error *
3029 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3031 struct got_worktree *worktree = arg;
3033 /* Reject merges into a work tree with mixed base commits. */
3034 if (got_fileindex_entry_has_commit(ie) &&
3035 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3036 SHA1_DIGEST_LENGTH) != 0)
3037 return got_error(GOT_ERR_MIXED_COMMITS);
3042 struct check_merge_conflicts_arg {
3043 struct got_worktree *worktree;
3044 struct got_fileindex *fileindex;
3045 struct got_repository *repo;
3048 static const struct got_error *
3049 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3050 struct got_blob_object *blob2, struct got_object_id *id1,
3051 struct got_object_id *id2, const char *path1, const char *path2,
3052 mode_t mode1, mode_t mode2, struct got_repository *repo)
3054 const struct got_error *err = NULL;
3055 struct check_merge_conflicts_arg *a = arg;
3056 unsigned char status;
3058 struct got_fileindex_entry *ie;
3059 const char *path = path2 ? path2 : path1;
3060 struct got_object_id *id = id2 ? id2 : id1;
3066 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3070 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3072 return got_error_from_errno("asprintf");
3074 /* Reject merges into a work tree with conflicted files. */
3075 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3079 if (status == GOT_STATUS_CONFLICT)
3080 return got_error(GOT_ERR_CONFLICTS);
3085 static const struct got_error *
3086 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3087 const char *fileindex_path, struct got_object_id *commit_id1,
3088 struct got_object_id *commit_id2, struct got_repository *repo,
3089 got_worktree_checkout_cb progress_cb, void *progress_arg,
3090 got_cancel_cb cancel_cb, void *cancel_arg)
3092 const struct got_error *err = NULL, *sync_err;
3093 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3094 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3095 struct check_merge_conflicts_arg cmc_arg;
3096 struct merge_file_cb_arg arg;
3097 char *label_orig = NULL;
3100 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3101 worktree->path_prefix);
3102 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3108 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3112 err = got_object_id_str(&id_str, commit_id1);
3116 if (asprintf(&label_orig, "%s: commit %s",
3117 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3118 err = got_error_from_errno("asprintf");
3125 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3126 worktree->path_prefix);
3130 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3134 cmc_arg.worktree = worktree;
3135 cmc_arg.fileindex = fileindex;
3136 cmc_arg.repo = repo;
3137 err = got_diff_tree(tree1, tree2, "", "", repo,
3138 check_merge_conflicts, &cmc_arg, 0);
3142 arg.worktree = worktree;
3143 arg.fileindex = fileindex;
3144 arg.progress_cb = progress_cb;
3145 arg.progress_arg = progress_arg;
3146 arg.cancel_cb = cancel_cb;
3147 arg.cancel_arg = cancel_arg;
3148 arg.label_orig = label_orig;
3149 arg.commit_id2 = commit_id2;
3150 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3151 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3152 sync_err = sync_fileindex(fileindex, fileindex_path);
3153 if (sync_err && err == NULL)
3157 got_object_tree_close(tree1);
3159 got_object_tree_close(tree2);
3164 const struct got_error *
3165 got_worktree_merge_files(struct got_worktree *worktree,
3166 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3167 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3168 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3170 const struct got_error *err, *unlockerr;
3171 char *fileindex_path = NULL;
3172 struct got_fileindex *fileindex = NULL;
3174 err = lock_worktree(worktree, LOCK_EX);
3178 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3182 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3187 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3188 commit_id2, repo, progress_cb, progress_arg,
3189 cancel_cb, cancel_arg);
3192 got_fileindex_free(fileindex);
3193 free(fileindex_path);
3194 unlockerr = lock_worktree(worktree, LOCK_SH);
3195 if (unlockerr && err == NULL)
3200 struct diff_dir_cb_arg {
3201 struct got_fileindex *fileindex;
3202 struct got_worktree *worktree;
3203 const char *status_path;
3204 size_t status_path_len;
3205 struct got_repository *repo;
3206 got_worktree_status_cb status_cb;
3208 got_cancel_cb cancel_cb;
3210 /* A pathlist containing per-directory pathlists of ignore patterns. */
3211 struct got_pathlist_head *ignores;
3212 int report_unchanged;
3216 static const struct got_error *
3217 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3218 int dirfd, const char *de_name,
3219 got_worktree_status_cb status_cb, void *status_arg,
3220 struct got_repository *repo, int report_unchanged)
3222 const struct got_error *err = NULL;
3223 unsigned char status = GOT_STATUS_NO_CHANGE;
3224 unsigned char staged_status = get_staged_status(ie);
3226 struct got_object_id blob_id, commit_id, staged_blob_id;
3227 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3228 struct got_object_id *staged_blob_idp = NULL;
3230 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3234 if (status == GOT_STATUS_NO_CHANGE &&
3235 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3238 if (got_fileindex_entry_has_blob(ie)) {
3239 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3240 blob_idp = &blob_id;
3242 if (got_fileindex_entry_has_commit(ie)) {
3243 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3244 commit_idp = &commit_id;
3246 if (staged_status == GOT_STATUS_ADD ||
3247 staged_status == GOT_STATUS_MODIFY) {
3248 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3249 SHA1_DIGEST_LENGTH);
3250 staged_blob_idp = &staged_blob_id;
3253 return (*status_cb)(status_arg, status, staged_status,
3254 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3257 static const struct got_error *
3258 status_old_new(void *arg, struct got_fileindex_entry *ie,
3259 struct dirent *de, const char *parent_path, int dirfd)
3261 const struct got_error *err = NULL;
3262 struct diff_dir_cb_arg *a = arg;
3265 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3266 return got_error(GOT_ERR_CANCELLED);
3268 if (got_path_cmp(parent_path, a->status_path,
3269 strlen(parent_path), a->status_path_len) != 0 &&
3270 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3273 if (parent_path[0]) {
3274 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3275 parent_path, de->d_name) == -1)
3276 return got_error_from_errno("asprintf");
3278 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3280 return got_error_from_errno("asprintf");
3283 err = report_file_status(ie, abspath, dirfd, de->d_name,
3284 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3289 static const struct got_error *
3290 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3292 struct diff_dir_cb_arg *a = arg;
3293 struct got_object_id blob_id, commit_id;
3294 unsigned char status;
3296 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3297 return got_error(GOT_ERR_CANCELLED);
3299 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3302 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3303 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3304 if (got_fileindex_entry_has_file_on_disk(ie))
3305 status = GOT_STATUS_MISSING;
3307 status = GOT_STATUS_DELETE;
3308 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3309 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3313 free_ignorelist(struct got_pathlist_head *ignorelist)
3315 struct got_pathlist_entry *pe;
3317 TAILQ_FOREACH(pe, ignorelist, entry)
3318 free((char *)pe->path);
3319 got_pathlist_free(ignorelist);
3323 free_ignores(struct got_pathlist_head *ignores)
3325 struct got_pathlist_entry *pe;
3327 TAILQ_FOREACH(pe, ignores, entry) {
3328 struct got_pathlist_head *ignorelist = pe->data;
3329 free_ignorelist(ignorelist);
3330 free((char *)pe->path);
3332 got_pathlist_free(ignores);
3335 static const struct got_error *
3336 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3338 const struct got_error *err = NULL;
3339 struct got_pathlist_entry *pe = NULL;
3340 struct got_pathlist_head *ignorelist;
3341 char *line = NULL, *pattern, *dirpath = NULL;
3342 size_t linesize = 0;
3345 ignorelist = calloc(1, sizeof(*ignorelist));
3346 if (ignorelist == NULL)
3347 return got_error_from_errno("calloc");
3348 TAILQ_INIT(ignorelist);
3350 while ((linelen = getline(&line, &linesize, f)) != -1) {
3351 if (linelen > 0 && line[linelen - 1] == '\n')
3352 line[linelen - 1] = '\0';
3354 /* Git's ignores may contain comments. */
3358 /* Git's negated patterns are not (yet?) supported. */
3362 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3364 err = got_error_from_errno("asprintf");
3367 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3372 err = got_error_from_errno("getline");
3376 dirpath = strdup(path);
3377 if (dirpath == NULL) {
3378 err = got_error_from_errno("strdup");
3381 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3384 if (err || pe == NULL) {
3386 free_ignorelist(ignorelist);
3392 match_ignores(struct got_pathlist_head *ignores, const char *path)
3394 struct got_pathlist_entry *pe;
3396 /* Handle patterns which match in all directories. */
3397 TAILQ_FOREACH(pe, ignores, entry) {
3398 struct got_pathlist_head *ignorelist = pe->data;
3399 struct got_pathlist_entry *pi;
3401 TAILQ_FOREACH(pi, ignorelist, entry) {
3402 const char *p, *pattern = pi->path;
3404 if (strncmp(pattern, "**/", 3) != 0)
3409 if (fnmatch(pattern, p,
3410 FNM_PATHNAME | FNM_LEADING_DIR)) {
3411 /* Retry in next directory. */
3412 while (*p && *p != '/')
3424 * The ignores pathlist contains ignore lists from children before
3425 * parents, so we can find the most specific ignorelist by walking
3426 * ignores backwards.
3428 pe = TAILQ_LAST(ignores, got_pathlist_head);
3430 if (got_path_is_child(path, pe->path, pe->path_len)) {
3431 struct got_pathlist_head *ignorelist = pe->data;
3432 struct got_pathlist_entry *pi;
3433 TAILQ_FOREACH(pi, ignorelist, entry) {
3434 const char *pattern = pi->path;
3435 int flags = FNM_LEADING_DIR;
3436 if (strstr(pattern, "/**/") == NULL)
3437 flags |= FNM_PATHNAME;
3438 if (fnmatch(pattern, path, flags))
3443 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3449 static const struct got_error *
3450 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3451 const char *path, int dirfd, const char *ignores_filename)
3453 const struct got_error *err = NULL;
3456 FILE *ignoresfile = NULL;
3458 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3459 path[0] ? "/" : "", ignores_filename) == -1)
3460 return got_error_from_errno("asprintf");
3463 fd = openat(dirfd, ignores_filename,
3464 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3466 if (errno != ENOENT && errno != EACCES)
3467 err = got_error_from_errno2("openat",
3470 ignoresfile = fdopen(fd, "r");
3471 if (ignoresfile == NULL)
3472 err = got_error_from_errno2("fdopen",
3476 err = read_ignores(ignores, path, ignoresfile);
3480 ignoresfile = fopen(ignorespath, "re");
3481 if (ignoresfile == NULL) {
3482 if (errno != ENOENT && errno != EACCES)
3483 err = got_error_from_errno2("fopen",
3486 err = read_ignores(ignores, path, ignoresfile);
3489 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3490 err = got_error_from_errno2("fclose", path);
3491 if (fd != -1 && close(fd) == -1 && err == NULL)
3492 err = got_error_from_errno2("close", path);
3497 static const struct got_error *
3498 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3501 const struct got_error *err = NULL;
3502 struct diff_dir_cb_arg *a = arg;
3508 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3509 return got_error(GOT_ERR_CANCELLED);
3511 if (parent_path[0]) {
3512 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3513 return got_error_from_errno("asprintf");
3518 if (de->d_type == DT_DIR) {
3519 if (!a->no_ignores && ignore != NULL &&
3520 match_ignores(a->ignores, path))
3522 } else if (!match_ignores(a->ignores, path) &&
3523 got_path_is_child(path, a->status_path, a->status_path_len))
3524 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3525 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3531 static const struct got_error *
3532 status_traverse(void *arg, const char *path, int dirfd)
3534 const struct got_error *err = NULL;
3535 struct diff_dir_cb_arg *a = arg;
3540 err = add_ignores(a->ignores, a->worktree->root_path,
3541 path, dirfd, ".cvsignore");
3545 err = add_ignores(a->ignores, a->worktree->root_path, path,
3546 dirfd, ".gitignore");
3551 static const struct got_error *
3552 report_single_file_status(const char *path, const char *ondisk_path,
3553 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3554 void *status_arg, struct got_repository *repo, int report_unchanged,
3555 struct got_pathlist_head *ignores, int no_ignores)
3557 struct got_fileindex_entry *ie;
3560 if (!no_ignores && match_ignores(ignores, path))
3563 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3565 return report_file_status(ie, ondisk_path, -1, NULL,
3566 status_cb, status_arg, repo, report_unchanged);
3568 if (lstat(ondisk_path, &sb) == -1) {
3569 if (errno != ENOENT)
3570 return got_error_from_errno2("lstat", ondisk_path);
3571 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3572 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3576 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3577 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3578 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3583 static const struct got_error *
3584 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3585 const char *root_path, const char *path)
3587 const struct got_error *err;
3588 char *parent_path, *next_parent_path = NULL;
3590 err = add_ignores(ignores, root_path, "", -1,
3595 err = add_ignores(ignores, root_path, "", -1,
3600 err = got_path_dirname(&parent_path, path);
3602 if (err->code == GOT_ERR_BAD_PATH)
3603 return NULL; /* cannot traverse parent */
3607 err = add_ignores(ignores, root_path, parent_path, -1,
3611 err = add_ignores(ignores, root_path, parent_path, -1,
3615 err = got_path_dirname(&next_parent_path, parent_path);
3617 if (err->code == GOT_ERR_BAD_PATH)
3618 err = NULL; /* traversed everything */
3621 if (got_path_is_root_dir(parent_path))
3624 parent_path = next_parent_path;
3625 next_parent_path = NULL;
3629 free(next_parent_path);
3633 static const struct got_error *
3634 worktree_status(struct got_worktree *worktree, const char *path,
3635 struct got_fileindex *fileindex, struct got_repository *repo,
3636 got_worktree_status_cb status_cb, void *status_arg,
3637 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3638 int report_unchanged)
3640 const struct got_error *err = NULL;
3642 struct got_fileindex_diff_dir_cb fdiff_cb;
3643 struct diff_dir_cb_arg arg;
3644 char *ondisk_path = NULL;
3645 struct got_pathlist_head ignores;
3647 TAILQ_INIT(&ignores);
3649 if (asprintf(&ondisk_path, "%s%s%s",
3650 worktree->root_path, path[0] ? "/" : "", path) == -1)
3651 return got_error_from_errno("asprintf");
3653 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3655 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3656 !got_err_open_nofollow_on_symlink())
3657 err = got_error_from_errno2("open", ondisk_path);
3660 err = add_ignores_from_parent_paths(&ignores,
3661 worktree->root_path, ondisk_path);
3665 err = report_single_file_status(path, ondisk_path,
3666 fileindex, status_cb, status_arg, repo,
3667 report_unchanged, &ignores, no_ignores);
3670 fdiff_cb.diff_old_new = status_old_new;
3671 fdiff_cb.diff_old = status_old;
3672 fdiff_cb.diff_new = status_new;
3673 fdiff_cb.diff_traverse = status_traverse;
3674 arg.fileindex = fileindex;
3675 arg.worktree = worktree;
3676 arg.status_path = path;
3677 arg.status_path_len = strlen(path);
3679 arg.status_cb = status_cb;
3680 arg.status_arg = status_arg;
3681 arg.cancel_cb = cancel_cb;
3682 arg.cancel_arg = cancel_arg;
3683 arg.report_unchanged = report_unchanged;
3684 arg.no_ignores = no_ignores;
3686 err = add_ignores_from_parent_paths(&ignores,
3687 worktree->root_path, path);
3691 arg.ignores = &ignores;
3692 err = got_fileindex_diff_dir(fileindex, fd,
3693 worktree->root_path, path, repo, &fdiff_cb, &arg);
3696 free_ignores(&ignores);
3697 if (fd != -1 && close(fd) == -1 && err == NULL)
3698 err = got_error_from_errno("close");
3703 const struct got_error *
3704 got_worktree_status(struct got_worktree *worktree,
3705 struct got_pathlist_head *paths, struct got_repository *repo,
3706 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3707 got_cancel_cb cancel_cb, void *cancel_arg)
3709 const struct got_error *err = NULL;
3710 char *fileindex_path = NULL;
3711 struct got_fileindex *fileindex = NULL;
3712 struct got_pathlist_entry *pe;
3714 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3718 TAILQ_FOREACH(pe, paths, entry) {
3719 err = worktree_status(worktree, pe->path, fileindex, repo,
3720 status_cb, status_arg, cancel_cb, cancel_arg,
3725 free(fileindex_path);
3726 got_fileindex_free(fileindex);
3730 const struct got_error *
3731 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3734 const struct got_error *err = NULL;
3735 char *resolved = NULL, *cwd = NULL, *path = NULL;
3738 char *abspath = NULL;
3739 char canonpath[PATH_MAX];
3743 cwd = getcwd(NULL, 0);
3745 return got_error_from_errno("getcwd");
3747 if (lstat(arg, &sb) == -1) {
3748 if (errno != ENOENT) {
3749 err = got_error_from_errno2("lstat", arg);
3754 if (S_ISLNK(sb.st_mode)) {
3756 * We cannot use realpath(3) with symlinks since we want to
3757 * operate on the symlink itself.
3758 * But we can make the path absolute, assuming it is relative
3759 * to the current working directory, and then canonicalize it.
3761 if (!got_path_is_absolute(arg)) {
3762 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3763 err = got_error_from_errno("asprintf");
3768 err = got_canonpath(abspath ? abspath : arg, canonpath,
3772 resolved = strdup(canonpath);
3773 if (resolved == NULL) {
3774 err = got_error_from_errno("strdup");
3778 resolved = realpath(arg, NULL);
3779 if (resolved == NULL) {
3780 if (errno != ENOENT) {
3781 err = got_error_from_errno2("realpath", arg);
3784 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3785 err = got_error_from_errno("asprintf");
3788 err = got_canonpath(abspath, canonpath,
3792 resolved = strdup(canonpath);
3793 if (resolved == NULL) {
3794 err = got_error_from_errno("strdup");
3800 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3801 strlen(got_worktree_get_root_path(worktree)))) {
3802 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3806 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3807 err = got_path_skip_common_ancestor(&path,
3808 got_worktree_get_root_path(worktree), resolved);
3814 err = got_error_from_errno("strdup");
3819 /* XXX status walk can't deal with trailing slash! */
3821 while (len > 0 && path[len - 1] == '/') {
3822 path[len - 1] = '\0';
3836 struct schedule_addition_args {
3837 struct got_worktree *worktree;
3838 struct got_fileindex *fileindex;
3839 got_worktree_checkout_cb progress_cb;
3841 struct got_repository *repo;
3844 static const struct got_error *
3845 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3846 const char *relpath, struct got_object_id *blob_id,
3847 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3848 int dirfd, const char *de_name)
3850 struct schedule_addition_args *a = arg;
3851 const struct got_error *err = NULL;
3852 struct got_fileindex_entry *ie;
3856 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3858 return got_error_from_errno("asprintf");
3860 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3862 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3866 /* Re-adding an existing entry is a no-op. */
3867 if (status == GOT_STATUS_ADD)
3869 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3874 if (status != GOT_STATUS_UNVERSIONED) {
3875 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3879 err = got_fileindex_entry_alloc(&ie, relpath);
3882 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3883 relpath, NULL, NULL, 1);
3885 got_fileindex_entry_free(ie);
3888 err = got_fileindex_entry_add(a->fileindex, ie);
3890 got_fileindex_entry_free(ie);
3897 if (status == GOT_STATUS_ADD)
3899 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3902 const struct got_error *
3903 got_worktree_schedule_add(struct got_worktree *worktree,
3904 struct got_pathlist_head *paths,
3905 got_worktree_checkout_cb progress_cb, void *progress_arg,
3906 struct got_repository *repo, int no_ignores)
3908 struct got_fileindex *fileindex = NULL;
3909 char *fileindex_path = NULL;
3910 const struct got_error *err = NULL, *sync_err, *unlockerr;
3911 struct got_pathlist_entry *pe;
3912 struct schedule_addition_args saa;
3914 err = lock_worktree(worktree, LOCK_EX);
3918 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3922 saa.worktree = worktree;
3923 saa.fileindex = fileindex;
3924 saa.progress_cb = progress_cb;
3925 saa.progress_arg = progress_arg;
3928 TAILQ_FOREACH(pe, paths, entry) {
3929 err = worktree_status(worktree, pe->path, fileindex, repo,
3930 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3934 sync_err = sync_fileindex(fileindex, fileindex_path);
3935 if (sync_err && err == NULL)
3938 free(fileindex_path);
3940 got_fileindex_free(fileindex);
3941 unlockerr = lock_worktree(worktree, LOCK_SH);
3942 if (unlockerr && err == NULL)
3947 struct schedule_deletion_args {
3948 struct got_worktree *worktree;
3949 struct got_fileindex *fileindex;
3950 got_worktree_delete_cb progress_cb;
3952 struct got_repository *repo;
3953 int delete_local_mods;
3955 const char *status_codes;
3958 static const struct got_error *
3959 schedule_for_deletion(void *arg, unsigned char status,
3960 unsigned char staged_status, const char *relpath,
3961 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3962 struct got_object_id *commit_id, int dirfd, const char *de_name)
3964 struct schedule_deletion_args *a = arg;
3965 const struct got_error *err = NULL;
3966 struct got_fileindex_entry *ie = NULL;
3970 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3972 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3974 staged_status = get_staged_status(ie);
3975 if (staged_status != GOT_STATUS_NO_CHANGE) {
3976 if (staged_status == GOT_STATUS_DELETE)
3978 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3981 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3983 return got_error_from_errno("asprintf");
3985 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3990 if (a->status_codes) {
3991 size_t ncodes = strlen(a->status_codes);
3993 for (i = 0; i < ncodes ; i++) {
3994 if (status == a->status_codes[i])
3998 /* Do not delete files in non-matching status. */
4002 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4003 a->status_codes[i] != GOT_STATUS_MISSING) {
4004 static char msg[64];
4005 snprintf(msg, sizeof(msg),
4006 "invalid status code '%c'", a->status_codes[i]);
4007 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4012 if (status != GOT_STATUS_NO_CHANGE) {
4013 if (status == GOT_STATUS_DELETE)
4015 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4016 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4019 if (status != GOT_STATUS_MODIFY &&
4020 status != GOT_STATUS_MISSING) {
4021 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4026 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4030 if (unlinkat(dirfd, de_name, 0) != 0) {
4031 err = got_error_from_errno2("unlinkat",
4035 } else if (unlink(ondisk_path) != 0) {
4036 err = got_error_from_errno2("unlink", ondisk_path);
4040 root_len = strlen(a->worktree->root_path);
4043 err = got_path_dirname(&parent, ondisk_path);
4047 ondisk_path = parent;
4048 if (rmdir(ondisk_path) == -1) {
4049 if (errno != ENOTEMPTY)
4050 err = got_error_from_errno2("rmdir",
4054 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4055 strlen(ondisk_path), root_len) != 0);
4058 got_fileindex_entry_mark_deleted_from_disk(ie);
4063 if (status == GOT_STATUS_DELETE)
4065 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4066 staged_status, relpath);
4069 const struct got_error *
4070 got_worktree_schedule_delete(struct got_worktree *worktree,
4071 struct got_pathlist_head *paths, int delete_local_mods,
4072 const char *status_codes,
4073 got_worktree_delete_cb progress_cb, void *progress_arg,
4074 struct got_repository *repo, int keep_on_disk)
4076 struct got_fileindex *fileindex = NULL;
4077 char *fileindex_path = NULL;
4078 const struct got_error *err = NULL, *sync_err, *unlockerr;
4079 struct got_pathlist_entry *pe;
4080 struct schedule_deletion_args sda;
4082 err = lock_worktree(worktree, LOCK_EX);
4086 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4090 sda.worktree = worktree;
4091 sda.fileindex = fileindex;
4092 sda.progress_cb = progress_cb;
4093 sda.progress_arg = progress_arg;
4095 sda.delete_local_mods = delete_local_mods;
4096 sda.keep_on_disk = keep_on_disk;
4097 sda.status_codes = status_codes;
4099 TAILQ_FOREACH(pe, paths, entry) {
4100 err = worktree_status(worktree, pe->path, fileindex, repo,
4101 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4105 sync_err = sync_fileindex(fileindex, fileindex_path);
4106 if (sync_err && err == NULL)
4109 free(fileindex_path);
4111 got_fileindex_free(fileindex);
4112 unlockerr = lock_worktree(worktree, LOCK_SH);
4113 if (unlockerr && err == NULL)
4118 static const struct got_error *
4119 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4121 const struct got_error *err = NULL;
4123 size_t linesize = 0, n;
4126 linelen = getline(&line, &linesize, infile);
4127 if (linelen == -1) {
4128 if (ferror(infile)) {
4129 err = got_error_from_errno("getline");
4135 n = fwrite(line, 1, linelen, outfile);
4137 err = got_ferror(outfile, GOT_ERR_IO);
4142 n = fwrite(line, 1, linelen, rejectfile);
4144 err = got_ferror(outfile, GOT_ERR_IO);
4151 static const struct got_error *
4152 skip_one_line(FILE *f)
4155 size_t linesize = 0;
4158 linelen = getline(&line, &linesize, f);
4159 if (linelen == -1) {
4161 return got_error_from_errno("getline");
4168 static const struct got_error *
4169 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4170 int start_old, int end_old, int start_new, int end_new,
4171 FILE *outfile, FILE *rejectfile)
4173 const struct got_error *err;
4175 /* Copy old file's lines leading up to patch. */
4176 while (!feof(f1) && *line_cur1 < start_old) {
4177 err = copy_one_line(f1, outfile, NULL);
4182 /* Skip new file's lines leading up to patch. */
4183 while (!feof(f2) && *line_cur2 < start_new) {
4185 err = copy_one_line(f2, NULL, rejectfile);
4187 err = skip_one_line(f2);
4192 /* Copy patched lines. */
4193 while (!feof(f2) && *line_cur2 <= end_new) {
4194 err = copy_one_line(f2, outfile, NULL);
4199 /* Skip over old file's replaced lines. */
4200 while (!feof(f1) && *line_cur1 <= end_old) {
4202 err = copy_one_line(f1, NULL, rejectfile);
4204 err = skip_one_line(f1);
4213 static const struct got_error *
4214 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4215 FILE *outfile, FILE *rejectfile)
4217 const struct got_error *err;
4220 /* Copy old file's lines until EOF. */
4222 err = copy_one_line(f1, outfile, NULL);
4229 /* Copy new file's lines until EOF. */
4231 err = copy_one_line(f2, NULL, rejectfile);
4241 static const struct got_error *
4242 apply_or_reject_change(int *choice, int *nchunks_used,
4243 struct diff_result *diff_result, int n,
4244 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4245 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4246 got_worktree_patch_cb patch_cb, void *patch_arg)
4248 const struct got_error *err = NULL;
4249 struct diff_chunk_context cc = {};
4250 int start_old, end_old, start_new, end_new;
4252 struct diff_output_unidiff_state *diff_state;
4253 struct diff_input_info diff_info;
4256 *choice = GOT_PATCH_CHOICE_NONE;
4258 /* Get changed line numbers without context lines for copy_change(). */
4259 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4260 start_old = cc.left.start;
4261 end_old = cc.left.end;
4262 start_new = cc.right.start;
4263 end_new = cc.right.end;
4265 /* Get the same change with context lines for display. */
4266 memset(&cc, 0, sizeof(cc));
4267 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4269 memset(&diff_info, 0, sizeof(diff_info));
4270 diff_info.left_path = relpath;
4271 diff_info.right_path = relpath;
4273 diff_state = diff_output_unidiff_state_alloc();
4274 if (diff_state == NULL)
4275 return got_error_set_errno(ENOMEM,
4276 "diff_output_unidiff_state_alloc");
4278 hunkfile = got_opentemp();
4279 if (hunkfile == NULL) {
4280 err = got_error_from_errno("got_opentemp");
4284 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4286 if (rc != DIFF_RC_OK) {
4287 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4291 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4292 err = got_ferror(hunkfile, GOT_ERR_IO);
4296 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4297 hunkfile, changeno, nchanges);
4302 case GOT_PATCH_CHOICE_YES:
4303 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4304 end_old, start_new, end_new, outfile, rejectfile);
4306 case GOT_PATCH_CHOICE_NO:
4307 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4308 end_old, start_new, end_new, rejectfile, outfile);
4310 case GOT_PATCH_CHOICE_QUIT:
4313 err = got_error(GOT_ERR_PATCH_CHOICE);
4317 diff_output_unidiff_state_free(diff_state);
4318 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4319 err = got_error_from_errno("fclose");
4323 struct revert_file_args {
4324 struct got_worktree *worktree;
4325 struct got_fileindex *fileindex;
4326 got_worktree_checkout_cb progress_cb;
4328 got_worktree_patch_cb patch_cb;
4330 struct got_repository *repo;
4331 int unlink_added_files;
4334 static const struct got_error *
4335 create_patched_content(char **path_outfile, int reverse_patch,
4336 struct got_object_id *blob_id, const char *path2,
4337 int dirfd2, const char *de_name2,
4338 const char *relpath, struct got_repository *repo,
4339 got_worktree_patch_cb patch_cb, void *patch_arg)
4341 const struct got_error *err, *free_err;
4342 struct got_blob_object *blob = NULL;
4343 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4345 char link_target[PATH_MAX];
4346 ssize_t link_len = 0;
4347 char *path1 = NULL, *id_str = NULL;
4349 struct got_diffreg_result *diffreg_result = NULL;
4350 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4351 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4353 *path_outfile = NULL;
4355 err = got_object_id_str(&id_str, blob_id);
4360 fd2 = openat(dirfd2, de_name2,
4361 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4363 if (!got_err_open_nofollow_on_symlink()) {
4364 err = got_error_from_errno2("openat", path2);
4367 link_len = readlinkat(dirfd2, de_name2,
4368 link_target, sizeof(link_target));
4370 return got_error_from_errno2("readlinkat", path2);
4371 sb2.st_mode = S_IFLNK;
4372 sb2.st_size = link_len;
4375 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4377 if (!got_err_open_nofollow_on_symlink()) {
4378 err = got_error_from_errno2("open", path2);
4381 link_len = readlink(path2, link_target,
4382 sizeof(link_target));
4384 return got_error_from_errno2("readlink", path2);
4385 sb2.st_mode = S_IFLNK;
4386 sb2.st_size = link_len;
4390 if (fstat(fd2, &sb2) == -1) {
4391 err = got_error_from_errno2("fstat", path2);
4395 f2 = fdopen(fd2, "r");
4397 err = got_error_from_errno2("fdopen", path2);
4403 f2 = got_opentemp();
4405 err = got_error_from_errno2("got_opentemp", path2);
4408 n = fwrite(link_target, 1, link_len, f2);
4409 if (n != link_len) {
4410 err = got_ferror(f2, GOT_ERR_IO);
4413 if (fflush(f2) == EOF) {
4414 err = got_error_from_errno("fflush");
4420 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4424 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4428 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4432 err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4437 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4441 if (fseek(f1, 0L, SEEK_SET) == -1)
4442 return got_ferror(f1, GOT_ERR_IO);
4443 if (fseek(f2, 0L, SEEK_SET) == -1)
4444 return got_ferror(f2, GOT_ERR_IO);
4446 /* Count the number of actual changes in the diff result. */
4447 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4448 struct diff_chunk_context cc = {};
4449 diff_chunk_context_load_change(&cc, &nchunks_used,
4450 diffreg_result->result, n, 0);
4453 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4455 err = apply_or_reject_change(&choice, &nchunks_used,
4456 diffreg_result->result, n, relpath, f1, f2,
4457 &line_cur1, &line_cur2,
4458 reverse_patch ? NULL : outfile,
4459 reverse_patch ? outfile : NULL,
4460 ++i, nchanges, patch_cb, patch_arg);
4463 if (choice == GOT_PATCH_CHOICE_YES)
4465 else if (choice == GOT_PATCH_CHOICE_QUIT)
4469 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4470 reverse_patch ? NULL : outfile,
4471 reverse_patch ? outfile : NULL);
4475 if (!S_ISLNK(sb2.st_mode)) {
4476 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4477 err = got_error_from_errno2("fchmod", path2);
4485 got_object_blob_close(blob);
4486 free_err = got_diffreg_result_free(diffreg_result);
4489 if (f1 && fclose(f1) == EOF && err == NULL)
4490 err = got_error_from_errno2("fclose", path1);
4491 if (f2 && fclose(f2) == EOF && err == NULL)
4492 err = got_error_from_errno2("fclose", path2);
4493 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4494 err = got_error_from_errno2("close", path2);
4495 if (outfile && fclose(outfile) == EOF && err == NULL)
4496 err = got_error_from_errno2("fclose", *path_outfile);
4497 if (path1 && unlink(path1) == -1 && err == NULL)
4498 err = got_error_from_errno2("unlink", path1);
4499 if (err || !have_content) {
4500 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4501 err = got_error_from_errno2("unlink", *path_outfile);
4502 free(*path_outfile);
4503 *path_outfile = NULL;
4509 static const struct got_error *
4510 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4511 const char *relpath, struct got_object_id *blob_id,
4512 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4513 int dirfd, const char *de_name)
4515 struct revert_file_args *a = arg;
4516 const struct got_error *err = NULL;
4517 char *parent_path = NULL;
4518 struct got_fileindex_entry *ie;
4519 struct got_tree_object *tree = NULL;
4520 struct got_object_id *tree_id = NULL;
4521 const struct got_tree_entry *te = NULL;
4522 char *tree_path = NULL, *te_name;
4523 char *ondisk_path = NULL, *path_content = NULL;
4524 struct got_blob_object *blob = NULL;
4526 /* Reverting a staged deletion is a no-op. */
4527 if (status == GOT_STATUS_DELETE &&
4528 staged_status != GOT_STATUS_NO_CHANGE)
4531 if (status == GOT_STATUS_UNVERSIONED)
4532 return (*a->progress_cb)(a->progress_arg,
4533 GOT_STATUS_UNVERSIONED, relpath);
4535 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4537 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4539 /* Construct in-repository path of tree which contains this blob. */
4540 err = got_path_dirname(&parent_path, ie->path);
4542 if (err->code != GOT_ERR_BAD_PATH)
4544 parent_path = strdup("/");
4545 if (parent_path == NULL) {
4546 err = got_error_from_errno("strdup");
4550 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4551 tree_path = strdup(parent_path);
4552 if (tree_path == NULL) {
4553 err = got_error_from_errno("strdup");
4557 if (got_path_is_root_dir(parent_path)) {
4558 tree_path = strdup(a->worktree->path_prefix);
4559 if (tree_path == NULL) {
4560 err = got_error_from_errno("strdup");
4564 if (asprintf(&tree_path, "%s/%s",
4565 a->worktree->path_prefix, parent_path) == -1) {
4566 err = got_error_from_errno("asprintf");
4572 err = got_object_id_by_path(&tree_id, a->repo,
4573 a->worktree->base_commit_id, tree_path);
4575 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4576 (status == GOT_STATUS_ADD ||
4577 staged_status == GOT_STATUS_ADD)))
4580 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4584 err = got_path_basename(&te_name, ie->path);
4588 te = got_object_tree_find_entry(tree, te_name);
4590 if (te == NULL && status != GOT_STATUS_ADD &&
4591 staged_status != GOT_STATUS_ADD) {
4592 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4598 case GOT_STATUS_ADD:
4600 int choice = GOT_PATCH_CHOICE_NONE;
4601 err = (*a->patch_cb)(&choice, a->patch_arg,
4602 status, ie->path, NULL, 1, 1);
4605 if (choice != GOT_PATCH_CHOICE_YES)
4608 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4612 got_fileindex_entry_remove(a->fileindex, ie);
4613 if (a->unlink_added_files) {
4614 if (asprintf(&ondisk_path, "%s/%s",
4615 got_worktree_get_root_path(a->worktree),
4617 err = got_error_from_errno("asprintf");
4620 if (unlink(ondisk_path) == -1) {
4621 err = got_error_from_errno2("unlink",
4627 case GOT_STATUS_DELETE:
4629 int choice = GOT_PATCH_CHOICE_NONE;
4630 err = (*a->patch_cb)(&choice, a->patch_arg,
4631 status, ie->path, NULL, 1, 1);
4634 if (choice != GOT_PATCH_CHOICE_YES)
4638 case GOT_STATUS_MODIFY:
4639 case GOT_STATUS_MODE_CHANGE:
4640 case GOT_STATUS_CONFLICT:
4641 case GOT_STATUS_MISSING: {
4642 struct got_object_id id;
4643 if (staged_status == GOT_STATUS_ADD ||
4644 staged_status == GOT_STATUS_MODIFY) {
4645 memcpy(id.sha1, ie->staged_blob_sha1,
4646 SHA1_DIGEST_LENGTH);
4648 memcpy(id.sha1, ie->blob_sha1,
4649 SHA1_DIGEST_LENGTH);
4650 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4654 if (asprintf(&ondisk_path, "%s/%s",
4655 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4656 err = got_error_from_errno("asprintf");
4660 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4661 status == GOT_STATUS_CONFLICT)) {
4662 int is_bad_symlink = 0;
4663 err = create_patched_content(&path_content, 1, &id,
4664 ondisk_path, dirfd, de_name, ie->path, a->repo,
4665 a->patch_cb, a->patch_arg);
4666 if (err || path_content == NULL)
4668 if (te && S_ISLNK(te->mode)) {
4669 if (unlink(path_content) == -1) {
4670 err = got_error_from_errno2("unlink",
4674 err = install_symlink(&is_bad_symlink,
4675 a->worktree, ondisk_path, ie->path,
4676 blob, 0, 1, 0, 0, a->repo,
4677 a->progress_cb, a->progress_arg);
4679 if (rename(path_content, ondisk_path) == -1) {
4680 err = got_error_from_errno3("rename",
4681 path_content, ondisk_path);
4686 int is_bad_symlink = 0;
4687 if (te && S_ISLNK(te->mode)) {
4688 err = install_symlink(&is_bad_symlink,
4689 a->worktree, ondisk_path, ie->path,
4690 blob, 0, 1, 0, 0, a->repo,
4691 a->progress_cb, a->progress_arg);
4693 err = install_blob(a->worktree, ondisk_path,
4695 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4696 got_fileindex_perms_to_st(ie), blob,
4697 0, 1, 0, 0, a->repo,
4698 a->progress_cb, a->progress_arg);
4702 if (status == GOT_STATUS_DELETE ||
4703 status == GOT_STATUS_MODE_CHANGE) {
4704 err = got_fileindex_entry_update(ie,
4705 a->worktree->root_fd, relpath,
4707 a->worktree->base_commit_id->sha1, 1);
4711 if (is_bad_symlink) {
4712 got_fileindex_entry_filetype_set(ie,
4713 GOT_FILEIDX_MODE_BAD_SYMLINK);
4727 got_object_blob_close(blob);
4729 got_object_tree_close(tree);
4734 const struct got_error *
4735 got_worktree_revert(struct got_worktree *worktree,
4736 struct got_pathlist_head *paths,
4737 got_worktree_checkout_cb progress_cb, void *progress_arg,
4738 got_worktree_patch_cb patch_cb, void *patch_arg,
4739 struct got_repository *repo)
4741 struct got_fileindex *fileindex = NULL;
4742 char *fileindex_path = NULL;
4743 const struct got_error *err = NULL, *unlockerr = NULL;
4744 const struct got_error *sync_err = NULL;
4745 struct got_pathlist_entry *pe;
4746 struct revert_file_args rfa;
4748 err = lock_worktree(worktree, LOCK_EX);
4752 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4756 rfa.worktree = worktree;
4757 rfa.fileindex = fileindex;
4758 rfa.progress_cb = progress_cb;
4759 rfa.progress_arg = progress_arg;
4760 rfa.patch_cb = patch_cb;
4761 rfa.patch_arg = patch_arg;
4763 rfa.unlink_added_files = 0;
4764 TAILQ_FOREACH(pe, paths, entry) {
4765 err = worktree_status(worktree, pe->path, fileindex, repo,
4766 revert_file, &rfa, NULL, NULL, 1, 0);
4770 sync_err = sync_fileindex(fileindex, fileindex_path);
4771 if (sync_err && err == NULL)
4774 free(fileindex_path);
4776 got_fileindex_free(fileindex);
4777 unlockerr = lock_worktree(worktree, LOCK_SH);
4778 if (unlockerr && err == NULL)
4784 free_commitable(struct got_commitable *ct)
4787 free(ct->in_repo_path);
4788 free(ct->ondisk_path);
4790 free(ct->base_blob_id);
4791 free(ct->staged_blob_id);
4792 free(ct->base_commit_id);
4796 struct collect_commitables_arg {
4797 struct got_pathlist_head *commitable_paths;
4798 struct got_repository *repo;
4799 struct got_worktree *worktree;
4800 struct got_fileindex *fileindex;
4801 int have_staged_files;
4802 int allow_bad_symlinks;
4805 static const struct got_error *
4806 collect_commitables(void *arg, unsigned char status,
4807 unsigned char staged_status, const char *relpath,
4808 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4809 struct got_object_id *commit_id, int dirfd, const char *de_name)
4811 struct collect_commitables_arg *a = arg;
4812 const struct got_error *err = NULL;
4813 struct got_commitable *ct = NULL;
4814 struct got_pathlist_entry *new = NULL;
4815 char *parent_path = NULL, *path = NULL;
4818 if (a->have_staged_files) {
4819 if (staged_status != GOT_STATUS_MODIFY &&
4820 staged_status != GOT_STATUS_ADD &&
4821 staged_status != GOT_STATUS_DELETE)
4824 if (status == GOT_STATUS_CONFLICT)
4825 return got_error(GOT_ERR_COMMIT_CONFLICT);
4827 if (status != GOT_STATUS_MODIFY &&
4828 status != GOT_STATUS_MODE_CHANGE &&
4829 status != GOT_STATUS_ADD &&
4830 status != GOT_STATUS_DELETE)
4834 if (asprintf(&path, "/%s", relpath) == -1) {
4835 err = got_error_from_errno("asprintf");
4838 if (strcmp(path, "/") == 0) {
4839 parent_path = strdup("");
4840 if (parent_path == NULL)
4841 return got_error_from_errno("strdup");
4843 err = got_path_dirname(&parent_path, path);
4848 ct = calloc(1, sizeof(*ct));
4850 err = got_error_from_errno("calloc");
4854 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4856 err = got_error_from_errno("asprintf");
4860 if (staged_status == GOT_STATUS_ADD ||
4861 staged_status == GOT_STATUS_MODIFY) {
4862 struct got_fileindex_entry *ie;
4863 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4864 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4865 case GOT_FILEIDX_MODE_REGULAR_FILE:
4866 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4869 case GOT_FILEIDX_MODE_SYMLINK:
4873 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4876 ct->mode |= got_fileindex_entry_perms_get(ie);
4877 } else if (status != GOT_STATUS_DELETE &&
4878 staged_status != GOT_STATUS_DELETE) {
4880 if (fstatat(dirfd, de_name, &sb,
4881 AT_SYMLINK_NOFOLLOW) == -1) {
4882 err = got_error_from_errno2("fstatat",
4886 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4887 err = got_error_from_errno2("lstat", ct->ondisk_path);
4890 ct->mode = sb.st_mode;
4893 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4894 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4896 err = got_error_from_errno("asprintf");
4900 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4901 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4903 char target_path[PATH_MAX];
4905 target_len = readlink(ct->ondisk_path, target_path,
4906 sizeof(target_path));
4907 if (target_len == -1) {
4908 err = got_error_from_errno2("readlink",
4912 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4913 target_len, ct->ondisk_path, a->worktree->root_path);
4916 if (is_bad_symlink) {
4917 err = got_error_path(ct->ondisk_path,
4918 GOT_ERR_BAD_SYMLINK);
4924 ct->status = status;
4925 ct->staged_status = staged_status;
4926 ct->blob_id = NULL; /* will be filled in when blob gets created */
4927 if (ct->status != GOT_STATUS_ADD &&
4928 ct->staged_status != GOT_STATUS_ADD) {
4929 ct->base_blob_id = got_object_id_dup(blob_id);
4930 if (ct->base_blob_id == NULL) {
4931 err = got_error_from_errno("got_object_id_dup");
4934 ct->base_commit_id = got_object_id_dup(commit_id);
4935 if (ct->base_commit_id == NULL) {
4936 err = got_error_from_errno("got_object_id_dup");
4940 if (ct->staged_status == GOT_STATUS_ADD ||
4941 ct->staged_status == GOT_STATUS_MODIFY) {
4942 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4943 if (ct->staged_blob_id == NULL) {
4944 err = got_error_from_errno("got_object_id_dup");
4948 ct->path = strdup(path);
4949 if (ct->path == NULL) {
4950 err = got_error_from_errno("strdup");
4953 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4955 if (ct && (err || new == NULL))
4956 free_commitable(ct);
4962 static const struct got_error *write_tree(struct got_object_id **, int *,
4963 struct got_tree_object *, const char *, struct got_pathlist_head *,
4964 got_worktree_status_cb status_cb, void *status_arg,
4965 struct got_repository *);
4967 static const struct got_error *
4968 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4969 struct got_tree_entry *te, const char *parent_path,
4970 struct got_pathlist_head *commitable_paths,
4971 got_worktree_status_cb status_cb, void *status_arg,
4972 struct got_repository *repo)
4974 const struct got_error *err = NULL;
4975 struct got_tree_object *subtree;
4978 if (asprintf(&subpath, "%s%s%s", parent_path,
4979 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4980 return got_error_from_errno("asprintf");
4982 err = got_object_open_as_tree(&subtree, repo, &te->id);
4986 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4987 commitable_paths, status_cb, status_arg, repo);
4988 got_object_tree_close(subtree);
4993 static const struct got_error *
4994 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4996 const struct got_error *err = NULL;
4997 char *ct_parent_path = NULL;
5001 if (strchr(ct->in_repo_path, '/') == NULL) {
5002 *match = got_path_is_root_dir(path);
5006 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5009 *match = (strcmp(path, ct_parent_path) == 0);
5010 free(ct_parent_path);
5015 get_ct_file_mode(struct got_commitable *ct)
5017 if (S_ISLNK(ct->mode))
5020 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5023 static const struct got_error *
5024 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5025 struct got_tree_entry *te, struct got_commitable *ct)
5027 const struct got_error *err = NULL;
5031 err = got_object_tree_entry_dup(new_te, te);
5035 (*new_te)->mode = get_ct_file_mode(ct);
5037 if (ct->staged_status == GOT_STATUS_MODIFY)
5038 memcpy(&(*new_te)->id, ct->staged_blob_id,
5039 sizeof((*new_te)->id));
5041 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5043 if (err && *new_te) {
5050 static const struct got_error *
5051 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5052 struct got_commitable *ct)
5054 const struct got_error *err = NULL;
5055 char *ct_name = NULL;
5059 *new_te = calloc(1, sizeof(**new_te));
5060 if (*new_te == NULL)
5061 return got_error_from_errno("calloc");
5063 err = got_path_basename(&ct_name, ct->path);
5066 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5067 sizeof((*new_te)->name)) {
5068 err = got_error(GOT_ERR_NO_SPACE);
5072 (*new_te)->mode = get_ct_file_mode(ct);
5074 if (ct->staged_status == GOT_STATUS_ADD)
5075 memcpy(&(*new_te)->id, ct->staged_blob_id,
5076 sizeof((*new_te)->id));
5078 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5081 if (err && *new_te) {
5088 static const struct got_error *
5089 insert_tree_entry(struct got_tree_entry *new_te,
5090 struct got_pathlist_head *paths)
5092 const struct got_error *err = NULL;
5093 struct got_pathlist_entry *new_pe;
5095 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5099 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5103 static const struct got_error *
5104 report_ct_status(struct got_commitable *ct,
5105 got_worktree_status_cb status_cb, void *status_arg)
5107 const char *ct_path = ct->path;
5108 unsigned char status;
5110 if (status_cb == NULL) /* no commit progress output desired */
5113 while (ct_path[0] == '/')
5116 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5117 status = ct->staged_status;
5119 status = ct->status;
5121 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5122 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5125 static const struct got_error *
5126 match_modified_subtree(int *modified, struct got_tree_entry *te,
5127 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5129 const struct got_error *err = NULL;
5130 struct got_pathlist_entry *pe;
5135 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5136 got_path_is_root_dir(base_tree_path) ? "" : "/",
5138 return got_error_from_errno("asprintf");
5140 TAILQ_FOREACH(pe, commitable_paths, entry) {
5141 struct got_commitable *ct = pe->data;
5142 *modified = got_path_is_child(ct->in_repo_path, te_path,
5152 static const struct got_error *
5153 match_deleted_or_modified_ct(struct got_commitable **ctp,
5154 struct got_tree_entry *te, const char *base_tree_path,
5155 struct got_pathlist_head *commitable_paths)
5157 const struct got_error *err = NULL;
5158 struct got_pathlist_entry *pe;
5162 TAILQ_FOREACH(pe, commitable_paths, entry) {
5163 struct got_commitable *ct = pe->data;
5164 char *ct_name = NULL;
5167 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5168 if (ct->status != GOT_STATUS_MODIFY &&
5169 ct->status != GOT_STATUS_MODE_CHANGE &&
5170 ct->status != GOT_STATUS_DELETE)
5173 if (ct->staged_status != GOT_STATUS_MODIFY &&
5174 ct->staged_status != GOT_STATUS_DELETE)
5178 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5181 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5187 err = got_path_basename(&ct_name, pe->path);
5191 if (strcmp(te->name, ct_name) != 0) {
5204 static const struct got_error *
5205 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5206 const char *child_path, const char *path_base_tree,
5207 struct got_pathlist_head *commitable_paths,
5208 got_worktree_status_cb status_cb, void *status_arg,
5209 struct got_repository *repo)
5211 const struct got_error *err = NULL;
5212 struct got_tree_entry *new_te;
5214 struct got_object_id *id = NULL;
5219 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5220 got_path_is_root_dir(path_base_tree) ? "" : "/",
5222 return got_error_from_errno("asprintf");
5224 new_te = calloc(1, sizeof(*new_te));
5226 return got_error_from_errno("calloc");
5227 new_te->mode = S_IFDIR;
5229 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5230 sizeof(new_te->name)) {
5231 err = got_error(GOT_ERR_NO_SPACE);
5234 err = write_tree(&id, &nentries, NULL, subtree_path,
5235 commitable_paths, status_cb, status_arg, repo);
5240 memcpy(&new_te->id, id, sizeof(new_te->id));
5249 static const struct got_error *
5250 write_tree(struct got_object_id **new_tree_id, int *nentries,
5251 struct got_tree_object *base_tree, const char *path_base_tree,
5252 struct got_pathlist_head *commitable_paths,
5253 got_worktree_status_cb status_cb, void *status_arg,
5254 struct got_repository *repo)
5256 const struct got_error *err = NULL;
5257 struct got_pathlist_head paths;
5258 struct got_tree_entry *te, *new_te = NULL;
5259 struct got_pathlist_entry *pe;
5264 /* Insert, and recurse into, newly added entries first. */
5265 TAILQ_FOREACH(pe, commitable_paths, entry) {
5266 struct got_commitable *ct = pe->data;
5267 char *child_path = NULL, *slash;
5269 if ((ct->status != GOT_STATUS_ADD &&
5270 ct->staged_status != GOT_STATUS_ADD) ||
5271 (ct->flags & GOT_COMMITABLE_ADDED))
5274 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5275 strlen(path_base_tree)))
5278 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5283 slash = strchr(child_path, '/');
5284 if (slash == NULL) {
5285 err = alloc_added_blob_tree_entry(&new_te, ct);
5288 err = report_ct_status(ct, status_cb, status_arg);
5291 ct->flags |= GOT_COMMITABLE_ADDED;
5292 err = insert_tree_entry(new_te, &paths);
5297 *slash = '\0'; /* trim trailing path components */
5298 if (base_tree == NULL ||
5299 got_object_tree_find_entry(base_tree, child_path)
5301 err = make_subtree_for_added_blob(&new_te,
5302 child_path, path_base_tree,
5303 commitable_paths, status_cb, status_arg,
5307 err = insert_tree_entry(new_te, &paths);
5316 int i, nbase_entries;
5317 /* Handle modified and deleted entries. */
5318 nbase_entries = got_object_tree_get_nentries(base_tree);
5319 for (i = 0; i < nbase_entries; i++) {
5320 struct got_commitable *ct = NULL;
5322 te = got_object_tree_get_entry(base_tree, i);
5323 if (got_object_tree_entry_is_submodule(te)) {
5324 /* Entry is a submodule; just copy it. */
5325 err = got_object_tree_entry_dup(&new_te, te);
5328 err = insert_tree_entry(new_te, &paths);
5335 if (S_ISDIR(te->mode)) {
5337 err = got_object_tree_entry_dup(&new_te, te);
5340 err = match_modified_subtree(&modified, te,
5341 path_base_tree, commitable_paths);
5344 /* Avoid recursion into unmodified subtrees. */
5346 struct got_object_id *new_id;
5348 err = write_subtree(&new_id,
5350 path_base_tree, commitable_paths,
5351 status_cb, status_arg, repo);
5354 if (nsubentries == 0) {
5355 /* All entries were deleted. */
5359 memcpy(&new_te->id, new_id,
5360 sizeof(new_te->id));
5363 err = insert_tree_entry(new_te, &paths);
5370 err = match_deleted_or_modified_ct(&ct, te,
5371 path_base_tree, commitable_paths);
5375 /* NB: Deleted entries get dropped here. */
5376 if (ct->status == GOT_STATUS_MODIFY ||
5377 ct->status == GOT_STATUS_MODE_CHANGE ||
5378 ct->staged_status == GOT_STATUS_MODIFY) {
5379 err = alloc_modified_blob_tree_entry(
5383 err = insert_tree_entry(new_te, &paths);
5388 err = report_ct_status(ct, status_cb,
5393 /* Entry is unchanged; just copy it. */
5394 err = got_object_tree_entry_dup(&new_te, te);
5397 err = insert_tree_entry(new_te, &paths);
5405 /* Write new list of entries; deleted entries have been dropped. */
5406 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5408 got_pathlist_free(&paths);
5412 static const struct got_error *
5413 update_fileindex_after_commit(struct got_worktree *worktree,
5414 struct got_pathlist_head *commitable_paths,
5415 struct got_object_id *new_base_commit_id,
5416 struct got_fileindex *fileindex, int have_staged_files)
5418 const struct got_error *err = NULL;
5419 struct got_pathlist_entry *pe;
5420 char *relpath = NULL;
5422 TAILQ_FOREACH(pe, commitable_paths, entry) {
5423 struct got_fileindex_entry *ie;
5424 struct got_commitable *ct = pe->data;
5426 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5428 err = got_path_skip_common_ancestor(&relpath,
5429 worktree->root_path, ct->ondisk_path);
5434 if (ct->status == GOT_STATUS_DELETE ||
5435 ct->staged_status == GOT_STATUS_DELETE) {
5436 got_fileindex_entry_remove(fileindex, ie);
5437 } else if (ct->staged_status == GOT_STATUS_ADD ||
5438 ct->staged_status == GOT_STATUS_MODIFY) {
5439 got_fileindex_entry_stage_set(ie,
5440 GOT_FILEIDX_STAGE_NONE);
5441 got_fileindex_entry_staged_filetype_set(ie, 0);
5443 err = got_fileindex_entry_update(ie,
5444 worktree->root_fd, relpath,
5445 ct->staged_blob_id->sha1,
5446 new_base_commit_id->sha1,
5447 !have_staged_files);
5449 err = got_fileindex_entry_update(ie,
5450 worktree->root_fd, relpath,
5452 new_base_commit_id->sha1,
5453 !have_staged_files);
5455 err = got_fileindex_entry_alloc(&ie, pe->path);
5458 err = got_fileindex_entry_update(ie,
5459 worktree->root_fd, relpath, ct->blob_id->sha1,
5460 new_base_commit_id->sha1, 1);
5462 got_fileindex_entry_free(ie);
5465 err = got_fileindex_entry_add(fileindex, ie);
5467 got_fileindex_entry_free(ie);
5480 static const struct got_error *
5481 check_out_of_date(const char *in_repo_path, unsigned char status,
5482 unsigned char staged_status, struct got_object_id *base_blob_id,
5483 struct got_object_id *base_commit_id,
5484 struct got_object_id *head_commit_id, struct got_repository *repo,
5487 const struct got_error *err = NULL;
5488 struct got_object_id *id = NULL;
5490 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5491 /* Trivial case: base commit == head commit */
5492 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5495 * Ensure file content which local changes were based
5496 * on matches file content in the branch head.
5498 err = got_object_id_by_path(&id, repo, head_commit_id,
5501 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5502 err = got_error(ood_errcode);
5504 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5505 err = got_error(ood_errcode);
5507 /* Require that added files don't exist in the branch head. */
5508 err = got_object_id_by_path(&id, repo, head_commit_id,
5510 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5512 err = id ? got_error(ood_errcode) : NULL;
5519 const struct got_error *
5520 commit_worktree(struct got_object_id **new_commit_id,
5521 struct got_pathlist_head *commitable_paths,
5522 struct got_object_id *head_commit_id,
5523 struct got_object_id *parent_id2,
5524 struct got_worktree *worktree,
5525 const char *author, const char *committer,
5526 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5527 got_worktree_status_cb status_cb, void *status_arg,
5528 struct got_repository *repo)
5530 const struct got_error *err = NULL, *unlockerr = NULL;
5531 struct got_pathlist_entry *pe;
5532 const char *head_ref_name = NULL;
5533 struct got_commit_object *head_commit = NULL;
5534 struct got_reference *head_ref2 = NULL;
5535 struct got_object_id *head_commit_id2 = NULL;
5536 struct got_tree_object *head_tree = NULL;
5537 struct got_object_id *new_tree_id = NULL;
5538 int nentries, nparents = 0;
5539 struct got_object_id_queue parent_ids;
5540 struct got_object_qid *pid = NULL;
5541 char *logmsg = NULL;
5543 *new_commit_id = NULL;
5545 STAILQ_INIT(&parent_ids);
5547 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5551 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5555 if (commit_msg_cb != NULL) {
5556 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5561 if (logmsg == NULL || strlen(logmsg) == 0) {
5562 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5566 /* Create blobs from added and modified files and record their IDs. */
5567 TAILQ_FOREACH(pe, commitable_paths, entry) {
5568 struct got_commitable *ct = pe->data;
5571 /* Blobs for staged files already exist. */
5572 if (ct->staged_status == GOT_STATUS_ADD ||
5573 ct->staged_status == GOT_STATUS_MODIFY)
5576 if (ct->status != GOT_STATUS_ADD &&
5577 ct->status != GOT_STATUS_MODIFY &&
5578 ct->status != GOT_STATUS_MODE_CHANGE)
5581 if (asprintf(&ondisk_path, "%s/%s",
5582 worktree->root_path, pe->path) == -1) {
5583 err = got_error_from_errno("asprintf");
5586 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5592 /* Recursively write new tree objects. */
5593 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5594 commitable_paths, status_cb, status_arg, repo);
5598 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5601 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5604 err = got_object_qid_alloc(&pid, parent_id2);
5607 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5610 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5611 nparents, author, time(NULL), committer, time(NULL), logmsg, repo);
5617 /* Check if a concurrent commit to our branch has occurred. */
5618 head_ref_name = got_worktree_get_head_ref_name(worktree);
5619 if (head_ref_name == NULL) {
5620 err = got_error_from_errno("got_worktree_get_head_ref_name");
5623 /* Lock the reference here to prevent concurrent modification. */
5624 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5627 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5630 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5631 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5634 /* Update branch head in repository. */
5635 err = got_ref_change_ref(head_ref2, *new_commit_id);
5638 err = got_ref_write(head_ref2, repo);
5642 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5646 err = ref_base_commit(worktree, repo);
5650 got_object_id_queue_free(&parent_ids);
5652 got_object_tree_close(head_tree);
5654 got_object_commit_close(head_commit);
5655 free(head_commit_id2);
5657 unlockerr = got_ref_unlock(head_ref2);
5658 if (unlockerr && err == NULL)
5660 got_ref_close(head_ref2);
5665 static const struct got_error *
5666 check_path_is_commitable(const char *path,
5667 struct got_pathlist_head *commitable_paths)
5669 struct got_pathlist_entry *cpe = NULL;
5670 size_t path_len = strlen(path);
5672 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5673 struct got_commitable *ct = cpe->data;
5674 const char *ct_path = ct->path;
5676 while (ct_path[0] == '/')
5679 if (strcmp(path, ct_path) == 0 ||
5680 got_path_is_child(ct_path, path, path_len))
5685 return got_error_path(path, GOT_ERR_BAD_PATH);
5690 static const struct got_error *
5691 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5693 int *have_staged_files = arg;
5695 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5696 *have_staged_files = 1;
5697 return got_error(GOT_ERR_CANCELLED);
5703 static const struct got_error *
5704 check_non_staged_files(struct got_fileindex *fileindex,
5705 struct got_pathlist_head *paths)
5707 struct got_pathlist_entry *pe;
5708 struct got_fileindex_entry *ie;
5710 TAILQ_FOREACH(pe, paths, entry) {
5711 if (pe->path[0] == '\0')
5713 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5715 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5716 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5717 return got_error_path(pe->path,
5718 GOT_ERR_FILE_NOT_STAGED);
5724 const struct got_error *
5725 got_worktree_commit(struct got_object_id **new_commit_id,
5726 struct got_worktree *worktree, struct got_pathlist_head *paths,
5727 const char *author, const char *committer, int allow_bad_symlinks,
5728 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5729 got_worktree_status_cb status_cb, void *status_arg,
5730 struct got_repository *repo)
5732 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5733 struct got_fileindex *fileindex = NULL;
5734 char *fileindex_path = NULL;
5735 struct got_pathlist_head commitable_paths;
5736 struct collect_commitables_arg cc_arg;
5737 struct got_pathlist_entry *pe;
5738 struct got_reference *head_ref = NULL;
5739 struct got_object_id *head_commit_id = NULL;
5740 int have_staged_files = 0;
5742 *new_commit_id = NULL;
5744 TAILQ_INIT(&commitable_paths);
5746 err = lock_worktree(worktree, LOCK_EX);
5750 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5754 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5758 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5762 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5763 &have_staged_files);
5764 if (err && err->code != GOT_ERR_CANCELLED)
5766 if (have_staged_files) {
5767 err = check_non_staged_files(fileindex, paths);
5772 cc_arg.commitable_paths = &commitable_paths;
5773 cc_arg.worktree = worktree;
5774 cc_arg.fileindex = fileindex;
5776 cc_arg.have_staged_files = have_staged_files;
5777 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5778 TAILQ_FOREACH(pe, paths, entry) {
5779 err = worktree_status(worktree, pe->path, fileindex, repo,
5780 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
5785 if (TAILQ_EMPTY(&commitable_paths)) {
5786 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5790 TAILQ_FOREACH(pe, paths, entry) {
5791 err = check_path_is_commitable(pe->path, &commitable_paths);
5796 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5797 struct got_commitable *ct = pe->data;
5798 const char *ct_path = ct->in_repo_path;
5800 while (ct_path[0] == '/')
5802 err = check_out_of_date(ct_path, ct->status,
5803 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5804 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5810 err = commit_worktree(new_commit_id, &commitable_paths,
5811 head_commit_id, NULL, worktree, author, committer,
5812 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5816 err = update_fileindex_after_commit(worktree, &commitable_paths,
5817 *new_commit_id, fileindex, have_staged_files);
5818 sync_err = sync_fileindex(fileindex, fileindex_path);
5819 if (sync_err && err == NULL)
5823 got_fileindex_free(fileindex);
5824 free(fileindex_path);
5825 unlockerr = lock_worktree(worktree, LOCK_SH);
5826 if (unlockerr && err == NULL)
5828 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5829 struct got_commitable *ct = pe->data;
5830 free_commitable(ct);
5832 got_pathlist_free(&commitable_paths);
5837 got_commitable_get_path(struct got_commitable *ct)
5843 got_commitable_get_status(struct got_commitable *ct)
5848 struct check_rebase_ok_arg {
5849 struct got_worktree *worktree;
5850 struct got_repository *repo;
5853 static const struct got_error *
5854 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5856 const struct got_error *err = NULL;
5857 struct check_rebase_ok_arg *a = arg;
5858 unsigned char status;
5862 /* Reject rebase of a work tree with mixed base commits. */
5863 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5864 SHA1_DIGEST_LENGTH))
5865 return got_error(GOT_ERR_MIXED_COMMITS);
5867 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5869 return got_error_from_errno("asprintf");
5871 /* Reject rebase of a work tree with modified or staged files. */
5872 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5877 if (status != GOT_STATUS_NO_CHANGE)
5878 return got_error(GOT_ERR_MODIFIED);
5879 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5880 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5885 const struct got_error *
5886 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5887 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5888 struct got_worktree *worktree, struct got_reference *branch,
5889 struct got_repository *repo)
5891 const struct got_error *err = NULL;
5892 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5893 char *branch_ref_name = NULL;
5894 char *fileindex_path = NULL;
5895 struct check_rebase_ok_arg ok_arg;
5896 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5897 struct got_object_id *wt_branch_tip = NULL;
5899 *new_base_branch_ref = NULL;
5903 err = lock_worktree(worktree, LOCK_EX);
5907 err = open_fileindex(fileindex, &fileindex_path, worktree);
5911 ok_arg.worktree = worktree;
5913 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5918 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5922 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5926 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5930 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5935 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5938 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5939 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5943 err = got_ref_alloc_symref(new_base_branch_ref,
5944 new_base_branch_ref_name, wt_branch);
5947 err = got_ref_write(*new_base_branch_ref, repo);
5951 /* TODO Lock original branch's ref while rebasing? */
5953 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5957 err = got_ref_write(branch_ref, repo);
5961 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5962 worktree->base_commit_id);
5965 err = got_ref_write(*tmp_branch, repo);
5969 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5973 free(fileindex_path);
5974 free(tmp_branch_name);
5975 free(new_base_branch_ref_name);
5976 free(branch_ref_name);
5978 got_ref_close(branch_ref);
5980 got_ref_close(wt_branch);
5981 free(wt_branch_tip);
5983 if (*new_base_branch_ref) {
5984 got_ref_close(*new_base_branch_ref);
5985 *new_base_branch_ref = NULL;
5988 got_ref_close(*tmp_branch);
5992 got_fileindex_free(*fileindex);
5995 lock_worktree(worktree, LOCK_SH);
6000 const struct got_error *
6001 got_worktree_rebase_continue(struct got_object_id **commit_id,
6002 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6003 struct got_reference **branch, struct got_fileindex **fileindex,
6004 struct got_worktree *worktree, struct got_repository *repo)
6006 const struct got_error *err;
6007 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6008 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6009 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6010 char *fileindex_path = NULL;
6011 int have_staged_files = 0;
6014 *new_base_branch = NULL;
6019 err = lock_worktree(worktree, LOCK_EX);
6023 err = open_fileindex(fileindex, &fileindex_path, worktree);
6027 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6028 &have_staged_files);
6029 if (err && err->code != GOT_ERR_CANCELLED)
6031 if (have_staged_files) {
6032 err = got_error(GOT_ERR_STAGED_PATHS);
6036 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6040 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6044 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6048 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6052 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6056 err = got_ref_open(branch, repo,
6057 got_ref_get_symref_target(branch_ref), 0);
6061 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6065 err = got_ref_resolve(commit_id, repo, commit_ref);
6069 err = got_ref_open(new_base_branch, repo,
6070 new_base_branch_ref_name, 0);
6074 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6078 free(commit_ref_name);
6079 free(branch_ref_name);
6080 free(fileindex_path);
6082 got_ref_close(commit_ref);
6084 got_ref_close(branch_ref);
6089 got_ref_close(*tmp_branch);
6092 if (*new_base_branch) {
6093 got_ref_close(*new_base_branch);
6094 *new_base_branch = NULL;
6097 got_ref_close(*branch);
6101 got_fileindex_free(*fileindex);
6104 lock_worktree(worktree, LOCK_SH);
6109 const struct got_error *
6110 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6112 const struct got_error *err;
6113 char *tmp_branch_name = NULL;
6115 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6119 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6120 free(tmp_branch_name);
6124 static const struct got_error *
6125 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6126 char **logmsg, void *arg)
6132 static const struct got_error *
6133 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6134 const char *path, struct got_object_id *blob_id,
6135 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6136 int dirfd, const char *de_name)
6141 struct collect_merged_paths_arg {
6142 got_worktree_checkout_cb progress_cb;
6144 struct got_pathlist_head *merged_paths;
6147 static const struct got_error *
6148 collect_merged_paths(void *arg, unsigned char status, const char *path)
6150 const struct got_error *err;
6151 struct collect_merged_paths_arg *a = arg;
6153 struct got_pathlist_entry *new;
6155 err = (*a->progress_cb)(a->progress_arg, status, path);
6159 if (status != GOT_STATUS_MERGE &&
6160 status != GOT_STATUS_ADD &&
6161 status != GOT_STATUS_DELETE &&
6162 status != GOT_STATUS_CONFLICT)
6167 return got_error_from_errno("strdup");
6169 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6170 if (err || new == NULL)
6176 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6178 struct got_pathlist_entry *pe;
6180 TAILQ_FOREACH(pe, merged_paths, entry)
6181 free((char *)pe->path);
6183 got_pathlist_free(merged_paths);
6186 static const struct got_error *
6187 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6188 int is_rebase, struct got_repository *repo)
6190 const struct got_error *err;
6191 struct got_reference *commit_ref = NULL;
6193 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6195 if (err->code != GOT_ERR_NOT_REF)
6197 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6200 err = got_ref_write(commit_ref, repo);
6203 } else if (is_rebase) {
6204 struct got_object_id *stored_id;
6207 err = got_ref_resolve(&stored_id, repo, commit_ref);
6210 cmp = got_object_id_cmp(commit_id, stored_id);
6213 err = got_error(GOT_ERR_REBASE_COMMITID);
6219 got_ref_close(commit_ref);
6223 static const struct got_error *
6224 rebase_merge_files(struct got_pathlist_head *merged_paths,
6225 const char *commit_ref_name, struct got_worktree *worktree,
6226 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6227 struct got_object_id *commit_id, struct got_repository *repo,
6228 got_worktree_checkout_cb progress_cb, void *progress_arg,
6229 got_cancel_cb cancel_cb, void *cancel_arg)
6231 const struct got_error *err;
6232 struct got_reference *commit_ref = NULL;
6233 struct collect_merged_paths_arg cmp_arg;
6234 char *fileindex_path;
6236 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6238 err = get_fileindex_path(&fileindex_path, worktree);
6242 cmp_arg.progress_cb = progress_cb;
6243 cmp_arg.progress_arg = progress_arg;
6244 cmp_arg.merged_paths = merged_paths;
6245 err = merge_files(worktree, fileindex, fileindex_path,
6246 parent_commit_id, commit_id, repo, collect_merged_paths,
6247 &cmp_arg, cancel_cb, cancel_arg);
6249 got_ref_close(commit_ref);
6253 const struct got_error *
6254 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6255 struct got_worktree *worktree, struct got_fileindex *fileindex,
6256 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6257 struct got_repository *repo,
6258 got_worktree_checkout_cb progress_cb, void *progress_arg,
6259 got_cancel_cb cancel_cb, void *cancel_arg)
6261 const struct got_error *err;
6262 char *commit_ref_name;
6264 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6268 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6272 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6273 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6274 progress_arg, cancel_cb, cancel_arg);
6276 free(commit_ref_name);
6280 const struct got_error *
6281 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6282 struct got_worktree *worktree, struct got_fileindex *fileindex,
6283 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6284 struct got_repository *repo,
6285 got_worktree_checkout_cb progress_cb, void *progress_arg,
6286 got_cancel_cb cancel_cb, void *cancel_arg)
6288 const struct got_error *err;
6289 char *commit_ref_name;
6291 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6295 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6299 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6300 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6301 progress_arg, cancel_cb, cancel_arg);
6303 free(commit_ref_name);
6307 static const struct got_error *
6308 rebase_commit(struct got_object_id **new_commit_id,
6309 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6310 struct got_worktree *worktree, struct got_fileindex *fileindex,
6311 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6312 const char *new_logmsg, struct got_repository *repo)
6314 const struct got_error *err, *sync_err;
6315 struct got_pathlist_head commitable_paths;
6316 struct collect_commitables_arg cc_arg;
6317 char *fileindex_path = NULL;
6318 struct got_reference *head_ref = NULL;
6319 struct got_object_id *head_commit_id = NULL;
6320 char *logmsg = NULL;
6322 TAILQ_INIT(&commitable_paths);
6323 *new_commit_id = NULL;
6325 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6327 err = get_fileindex_path(&fileindex_path, worktree);
6331 cc_arg.commitable_paths = &commitable_paths;
6332 cc_arg.worktree = worktree;
6334 cc_arg.have_staged_files = 0;
6336 * If possible get the status of individual files directly to
6337 * avoid crawling the entire work tree once per rebased commit.
6339 * Ideally, merged_paths would contain a list of commitables
6340 * we could use so we could skip worktree_status() entirely.
6341 * However, we would then need carefully keep track of cumulative
6342 * effects of operations such as file additions and deletions
6343 * in 'got histedit -f' (folding multiple commits into one),
6344 * and this extra complexity is not really worth it.
6347 struct got_pathlist_entry *pe;
6348 TAILQ_FOREACH(pe, merged_paths, entry) {
6349 err = worktree_status(worktree, pe->path, fileindex,
6350 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6356 err = worktree_status(worktree, "", fileindex, repo,
6357 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6362 if (TAILQ_EMPTY(&commitable_paths)) {
6363 /* No-op change; commit will be elided. */
6364 err = got_ref_delete(commit_ref, repo);
6367 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6371 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6375 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6380 logmsg = strdup(new_logmsg);
6381 if (logmsg == NULL) {
6382 err = got_error_from_errno("strdup");
6386 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6391 /* NB: commit_worktree will call free(logmsg) */
6392 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6393 NULL, worktree, got_object_commit_get_author(orig_commit),
6394 got_object_commit_get_committer(orig_commit),
6395 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6399 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6403 err = got_ref_delete(commit_ref, repo);
6407 err = update_fileindex_after_commit(worktree, &commitable_paths,
6408 *new_commit_id, fileindex, 0);
6409 sync_err = sync_fileindex(fileindex, fileindex_path);
6410 if (sync_err && err == NULL)
6413 free(fileindex_path);
6414 free(head_commit_id);
6416 got_ref_close(head_ref);
6418 free(*new_commit_id);
6419 *new_commit_id = NULL;
6424 const struct got_error *
6425 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6426 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6427 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6428 struct got_commit_object *orig_commit,
6429 struct got_object_id *orig_commit_id, struct got_repository *repo)
6431 const struct got_error *err;
6432 char *commit_ref_name;
6433 struct got_reference *commit_ref = NULL;
6434 struct got_object_id *commit_id = NULL;
6436 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6440 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6443 err = got_ref_resolve(&commit_id, repo, commit_ref);
6446 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6447 err = got_error(GOT_ERR_REBASE_COMMITID);
6451 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6452 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6455 got_ref_close(commit_ref);
6456 free(commit_ref_name);
6461 const struct got_error *
6462 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6463 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6464 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6465 struct got_commit_object *orig_commit,
6466 struct got_object_id *orig_commit_id, const char *new_logmsg,
6467 struct got_repository *repo)
6469 const struct got_error *err;
6470 char *commit_ref_name;
6471 struct got_reference *commit_ref = NULL;
6473 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6477 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6481 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6482 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6485 got_ref_close(commit_ref);
6486 free(commit_ref_name);
6490 const struct got_error *
6491 got_worktree_rebase_postpone(struct got_worktree *worktree,
6492 struct got_fileindex *fileindex)
6495 got_fileindex_free(fileindex);
6496 return lock_worktree(worktree, LOCK_SH);
6499 static const struct got_error *
6500 delete_ref(const char *name, struct got_repository *repo)
6502 const struct got_error *err;
6503 struct got_reference *ref;
6505 err = got_ref_open(&ref, repo, name, 0);
6507 if (err->code == GOT_ERR_NOT_REF)
6512 err = got_ref_delete(ref, repo);
6517 static const struct got_error *
6518 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6520 const struct got_error *err;
6521 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6522 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6524 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6527 err = delete_ref(tmp_branch_name, repo);
6531 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6534 err = delete_ref(new_base_branch_ref_name, repo);
6538 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6541 err = delete_ref(branch_ref_name, repo);
6545 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6548 err = delete_ref(commit_ref_name, repo);
6553 free(tmp_branch_name);
6554 free(new_base_branch_ref_name);
6555 free(branch_ref_name);
6556 free(commit_ref_name);
6560 const struct got_error *
6561 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6562 struct got_object_id *new_commit_id, struct got_repository *repo)
6564 const struct got_error *err;
6565 struct got_reference *ref = NULL;
6566 struct got_object_id *old_commit_id = NULL;
6567 const char *branch_name = NULL;
6568 char *new_id_str = NULL;
6569 char *refname = NULL;
6571 branch_name = got_ref_get_name(branch);
6572 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6573 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6576 err = got_object_id_str(&new_id_str, new_commit_id);
6580 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6581 new_id_str) == -1) {
6582 err = got_error_from_errno("asprintf");
6586 err = got_ref_resolve(&old_commit_id, repo, branch);
6590 err = got_ref_alloc(&ref, refname, old_commit_id);
6594 err = got_ref_write(ref, repo);
6598 free(old_commit_id);
6604 const struct got_error *
6605 got_worktree_rebase_complete(struct got_worktree *worktree,
6606 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6607 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6608 struct got_repository *repo, int create_backup)
6610 const struct got_error *err, *unlockerr, *sync_err;
6611 struct got_object_id *new_head_commit_id = NULL;
6612 char *fileindex_path = NULL;
6614 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6618 if (create_backup) {
6619 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6620 rebased_branch, new_head_commit_id, repo);
6625 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6629 err = got_ref_write(rebased_branch, repo);
6633 err = got_worktree_set_head_ref(worktree, rebased_branch);
6637 err = delete_rebase_refs(worktree, repo);
6641 err = get_fileindex_path(&fileindex_path, worktree);
6644 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6645 sync_err = sync_fileindex(fileindex, fileindex_path);
6646 if (sync_err && err == NULL)
6649 got_fileindex_free(fileindex);
6650 free(fileindex_path);
6651 free(new_head_commit_id);
6652 unlockerr = lock_worktree(worktree, LOCK_SH);
6653 if (unlockerr && err == NULL)
6658 const struct got_error *
6659 got_worktree_rebase_abort(struct got_worktree *worktree,
6660 struct got_fileindex *fileindex, struct got_repository *repo,
6661 struct got_reference *new_base_branch,
6662 got_worktree_checkout_cb progress_cb, void *progress_arg)
6664 const struct got_error *err, *unlockerr, *sync_err;
6665 struct got_reference *resolved = NULL;
6666 struct got_object_id *commit_id = NULL;
6667 char *fileindex_path = NULL;
6668 struct revert_file_args rfa;
6669 struct got_object_id *tree_id = NULL;
6671 err = lock_worktree(worktree, LOCK_EX);
6675 err = got_ref_open(&resolved, repo,
6676 got_ref_get_symref_target(new_base_branch), 0);
6680 err = got_worktree_set_head_ref(worktree, resolved);
6685 * XXX commits to the base branch could have happened while
6686 * we were busy rebasing; should we store the original commit ID
6687 * when rebase begins and read it back here?
6689 err = got_ref_resolve(&commit_id, repo, resolved);
6693 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6697 err = got_object_id_by_path(&tree_id, repo,
6698 worktree->base_commit_id, worktree->path_prefix);
6702 err = delete_rebase_refs(worktree, repo);
6706 err = get_fileindex_path(&fileindex_path, worktree);
6710 rfa.worktree = worktree;
6711 rfa.fileindex = fileindex;
6712 rfa.progress_cb = progress_cb;
6713 rfa.progress_arg = progress_arg;
6714 rfa.patch_cb = NULL;
6715 rfa.patch_arg = NULL;
6717 rfa.unlink_added_files = 0;
6718 err = worktree_status(worktree, "", fileindex, repo,
6719 revert_file, &rfa, NULL, NULL, 1, 0);
6723 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6724 repo, progress_cb, progress_arg, NULL, NULL);
6726 sync_err = sync_fileindex(fileindex, fileindex_path);
6727 if (sync_err && err == NULL)
6730 got_ref_close(resolved);
6734 got_fileindex_free(fileindex);
6735 free(fileindex_path);
6737 unlockerr = lock_worktree(worktree, LOCK_SH);
6738 if (unlockerr && err == NULL)
6743 const struct got_error *
6744 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6745 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6746 struct got_fileindex **fileindex, struct got_worktree *worktree,
6747 struct got_repository *repo)
6749 const struct got_error *err = NULL;
6750 char *tmp_branch_name = NULL;
6751 char *branch_ref_name = NULL;
6752 char *base_commit_ref_name = NULL;
6753 char *fileindex_path = NULL;
6754 struct check_rebase_ok_arg ok_arg;
6755 struct got_reference *wt_branch = NULL;
6756 struct got_reference *base_commit_ref = NULL;
6760 *base_commit_id = NULL;
6763 err = lock_worktree(worktree, LOCK_EX);
6767 err = open_fileindex(fileindex, &fileindex_path, worktree);
6771 ok_arg.worktree = worktree;
6773 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6778 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6782 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6786 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6791 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6796 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6800 err = got_ref_write(*branch_ref, repo);
6804 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6805 worktree->base_commit_id);
6808 err = got_ref_write(base_commit_ref, repo);
6811 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6812 if (*base_commit_id == NULL) {
6813 err = got_error_from_errno("got_object_id_dup");
6817 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6818 worktree->base_commit_id);
6821 err = got_ref_write(*tmp_branch, repo);
6825 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6829 free(fileindex_path);
6830 free(tmp_branch_name);
6831 free(branch_ref_name);
6832 free(base_commit_ref_name);
6834 got_ref_close(wt_branch);
6837 got_ref_close(*branch_ref);
6841 got_ref_close(*tmp_branch);
6844 free(*base_commit_id);
6846 got_fileindex_free(*fileindex);
6849 lock_worktree(worktree, LOCK_SH);
6854 const struct got_error *
6855 got_worktree_histedit_postpone(struct got_worktree *worktree,
6856 struct got_fileindex *fileindex)
6859 got_fileindex_free(fileindex);
6860 return lock_worktree(worktree, LOCK_SH);
6863 const struct got_error *
6864 got_worktree_histedit_in_progress(int *in_progress,
6865 struct got_worktree *worktree)
6867 const struct got_error *err;
6868 char *tmp_branch_name = NULL;
6870 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6874 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6875 free(tmp_branch_name);
6879 const struct got_error *
6880 got_worktree_histedit_continue(struct got_object_id **commit_id,
6881 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6882 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6883 struct got_worktree *worktree, struct got_repository *repo)
6885 const struct got_error *err;
6886 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6887 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6888 struct got_reference *commit_ref = NULL;
6889 struct got_reference *base_commit_ref = NULL;
6890 char *fileindex_path = NULL;
6891 int have_staged_files = 0;
6895 *base_commit_id = NULL;
6898 err = lock_worktree(worktree, LOCK_EX);
6902 err = open_fileindex(fileindex, &fileindex_path, worktree);
6906 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6907 &have_staged_files);
6908 if (err && err->code != GOT_ERR_CANCELLED)
6910 if (have_staged_files) {
6911 err = got_error(GOT_ERR_STAGED_PATHS);
6915 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6919 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6923 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6927 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6932 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6936 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6939 err = got_ref_resolve(commit_id, repo, commit_ref);
6943 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6946 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6950 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6954 free(commit_ref_name);
6955 free(branch_ref_name);
6956 free(fileindex_path);
6958 got_ref_close(commit_ref);
6959 if (base_commit_ref)
6960 got_ref_close(base_commit_ref);
6964 free(*base_commit_id);
6965 *base_commit_id = NULL;
6967 got_ref_close(*tmp_branch);
6971 got_fileindex_free(*fileindex);
6974 lock_worktree(worktree, LOCK_EX);
6979 static const struct got_error *
6980 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6982 const struct got_error *err;
6983 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6984 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6986 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6989 err = delete_ref(tmp_branch_name, repo);
6993 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6997 err = delete_ref(base_commit_ref_name, repo);
7001 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7004 err = delete_ref(branch_ref_name, repo);
7008 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7011 err = delete_ref(commit_ref_name, repo);
7015 free(tmp_branch_name);
7016 free(base_commit_ref_name);
7017 free(branch_ref_name);
7018 free(commit_ref_name);
7022 const struct got_error *
7023 got_worktree_histedit_abort(struct got_worktree *worktree,
7024 struct got_fileindex *fileindex, struct got_repository *repo,
7025 struct got_reference *branch, struct got_object_id *base_commit_id,
7026 got_worktree_checkout_cb progress_cb, void *progress_arg)
7028 const struct got_error *err, *unlockerr, *sync_err;
7029 struct got_reference *resolved = NULL;
7030 char *fileindex_path = NULL;
7031 struct got_object_id *tree_id = NULL;
7032 struct revert_file_args rfa;
7034 err = lock_worktree(worktree, LOCK_EX);
7038 err = got_ref_open(&resolved, repo,
7039 got_ref_get_symref_target(branch), 0);
7043 err = got_worktree_set_head_ref(worktree, resolved);
7047 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7051 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
7052 worktree->path_prefix);
7056 err = delete_histedit_refs(worktree, repo);
7060 err = get_fileindex_path(&fileindex_path, worktree);
7064 rfa.worktree = worktree;
7065 rfa.fileindex = fileindex;
7066 rfa.progress_cb = progress_cb;
7067 rfa.progress_arg = progress_arg;
7068 rfa.patch_cb = NULL;
7069 rfa.patch_arg = NULL;
7071 rfa.unlink_added_files = 0;
7072 err = worktree_status(worktree, "", fileindex, repo,
7073 revert_file, &rfa, NULL, NULL, 1, 0);
7077 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7078 repo, progress_cb, progress_arg, NULL, NULL);
7080 sync_err = sync_fileindex(fileindex, fileindex_path);
7081 if (sync_err && err == NULL)
7084 got_ref_close(resolved);
7086 free(fileindex_path);
7088 unlockerr = lock_worktree(worktree, LOCK_SH);
7089 if (unlockerr && err == NULL)
7094 const struct got_error *
7095 got_worktree_histedit_complete(struct got_worktree *worktree,
7096 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7097 struct got_reference *edited_branch, struct got_repository *repo)
7099 const struct got_error *err, *unlockerr, *sync_err;
7100 struct got_object_id *new_head_commit_id = NULL;
7101 struct got_reference *resolved = NULL;
7102 char *fileindex_path = NULL;
7104 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7108 err = got_ref_open(&resolved, repo,
7109 got_ref_get_symref_target(edited_branch), 0);
7113 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7114 resolved, new_head_commit_id, repo);
7118 err = got_ref_change_ref(resolved, new_head_commit_id);
7122 err = got_ref_write(resolved, repo);
7126 err = got_worktree_set_head_ref(worktree, resolved);
7130 err = delete_histedit_refs(worktree, repo);
7134 err = get_fileindex_path(&fileindex_path, worktree);
7137 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7138 sync_err = sync_fileindex(fileindex, fileindex_path);
7139 if (sync_err && err == NULL)
7142 got_fileindex_free(fileindex);
7143 free(fileindex_path);
7144 free(new_head_commit_id);
7145 unlockerr = lock_worktree(worktree, LOCK_SH);
7146 if (unlockerr && err == NULL)
7151 const struct got_error *
7152 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7153 struct got_object_id *commit_id, struct got_repository *repo)
7155 const struct got_error *err;
7156 char *commit_ref_name;
7158 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7162 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7166 err = delete_ref(commit_ref_name, repo);
7168 free(commit_ref_name);
7172 const struct got_error *
7173 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7174 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7175 struct got_worktree *worktree, const char *refname,
7176 struct got_repository *repo)
7178 const struct got_error *err = NULL;
7179 char *fileindex_path = NULL;
7180 struct check_rebase_ok_arg ok_arg;
7184 *base_branch_ref = NULL;
7186 err = lock_worktree(worktree, LOCK_EX);
7190 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7191 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7192 "cannot integrate a branch into itself; "
7193 "update -b or different branch name required");
7197 err = open_fileindex(fileindex, &fileindex_path, worktree);
7201 /* Preconditions are the same as for rebase. */
7202 ok_arg.worktree = worktree;
7204 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7209 err = got_ref_open(branch_ref, repo, refname, 1);
7213 err = got_ref_open(base_branch_ref, repo,
7214 got_worktree_get_head_ref_name(worktree), 1);
7218 got_ref_close(*branch_ref);
7221 if (*base_branch_ref) {
7222 got_ref_close(*base_branch_ref);
7223 *base_branch_ref = NULL;
7226 got_fileindex_free(*fileindex);
7229 lock_worktree(worktree, LOCK_SH);
7234 const struct got_error *
7235 got_worktree_integrate_continue(struct got_worktree *worktree,
7236 struct got_fileindex *fileindex, struct got_repository *repo,
7237 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7238 got_worktree_checkout_cb progress_cb, void *progress_arg,
7239 got_cancel_cb cancel_cb, void *cancel_arg)
7241 const struct got_error *err = NULL, *sync_err, *unlockerr;
7242 char *fileindex_path = NULL;
7243 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7245 err = get_fileindex_path(&fileindex_path, worktree);
7249 err = got_ref_resolve(&commit_id, repo, branch_ref);
7253 err = got_object_id_by_path(&tree_id, repo, commit_id,
7254 worktree->path_prefix);
7258 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7262 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7263 progress_cb, progress_arg, cancel_cb, cancel_arg);
7267 err = got_ref_change_ref(base_branch_ref, commit_id);
7271 err = got_ref_write(base_branch_ref, repo);
7275 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7277 sync_err = sync_fileindex(fileindex, fileindex_path);
7278 if (sync_err && err == NULL)
7282 unlockerr = got_ref_unlock(branch_ref);
7283 if (unlockerr && err == NULL)
7285 got_ref_close(branch_ref);
7287 unlockerr = got_ref_unlock(base_branch_ref);
7288 if (unlockerr && err == NULL)
7290 got_ref_close(base_branch_ref);
7292 got_fileindex_free(fileindex);
7293 free(fileindex_path);
7296 unlockerr = lock_worktree(worktree, LOCK_SH);
7297 if (unlockerr && err == NULL)
7302 const struct got_error *
7303 got_worktree_integrate_abort(struct got_worktree *worktree,
7304 struct got_fileindex *fileindex, struct got_repository *repo,
7305 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7307 const struct got_error *err = NULL, *unlockerr = NULL;
7309 got_fileindex_free(fileindex);
7311 err = lock_worktree(worktree, LOCK_SH);
7313 unlockerr = got_ref_unlock(branch_ref);
7314 if (unlockerr && err == NULL)
7316 got_ref_close(branch_ref);
7318 unlockerr = got_ref_unlock(base_branch_ref);
7319 if (unlockerr && err == NULL)
7321 got_ref_close(base_branch_ref);
7326 const struct got_error *
7327 got_worktree_merge_postpone(struct got_worktree *worktree,
7328 struct got_fileindex *fileindex)
7330 const struct got_error *err, *sync_err;
7331 char *fileindex_path = NULL;
7333 err = get_fileindex_path(&fileindex_path, worktree);
7337 sync_err = sync_fileindex(fileindex, fileindex_path);
7339 err = lock_worktree(worktree, LOCK_SH);
7340 if (sync_err && err == NULL)
7343 got_fileindex_free(fileindex);
7344 free(fileindex_path);
7348 static const struct got_error *
7349 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7351 const struct got_error *err;
7352 char *branch_refname = NULL, *commit_refname = NULL;
7354 err = get_merge_branch_ref_name(&branch_refname, worktree);
7357 err = delete_ref(branch_refname, repo);
7361 err = get_merge_commit_ref_name(&commit_refname, worktree);
7364 err = delete_ref(commit_refname, repo);
7369 free(branch_refname);
7370 free(commit_refname);
7374 struct merge_commit_msg_arg {
7375 struct got_worktree *worktree;
7376 const char *branch_name;
7379 static const struct got_error *
7380 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths, char **logmsg,
7383 struct merge_commit_msg_arg *a = arg;
7385 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7386 got_worktree_get_head_ref_name(a->worktree)) == -1)
7387 return got_error_from_errno("asprintf");
7393 const struct got_error *
7394 got_worktree_merge_branch(struct got_worktree *worktree,
7395 struct got_fileindex *fileindex,
7396 struct got_object_id *yca_commit_id,
7397 struct got_object_id *branch_tip,
7398 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7399 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7401 const struct got_error *err;
7402 char *fileindex_path = NULL;
7404 err = get_fileindex_path(&fileindex_path, worktree);
7408 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7413 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7414 branch_tip, repo, progress_cb, progress_arg,
7415 cancel_cb, cancel_arg);
7417 free(fileindex_path);
7421 const struct got_error *
7422 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7423 struct got_worktree *worktree, struct got_fileindex *fileindex,
7424 const char *author, const char *committer, int allow_bad_symlinks,
7425 struct got_object_id *branch_tip, const char *branch_name,
7426 struct got_repository *repo,
7427 got_worktree_status_cb status_cb, void *status_arg)
7430 const struct got_error *err = NULL, *sync_err;
7431 struct got_pathlist_head commitable_paths;
7432 struct collect_commitables_arg cc_arg;
7433 struct got_pathlist_entry *pe;
7434 struct got_reference *head_ref = NULL;
7435 struct got_object_id *head_commit_id = NULL;
7436 int have_staged_files = 0;
7437 struct merge_commit_msg_arg mcm_arg;
7438 char *fileindex_path = NULL;
7440 *new_commit_id = NULL;
7442 TAILQ_INIT(&commitable_paths);
7444 err = get_fileindex_path(&fileindex_path, worktree);
7448 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7452 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7456 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7457 &have_staged_files);
7458 if (err && err->code != GOT_ERR_CANCELLED)
7460 if (have_staged_files) {
7461 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7465 cc_arg.commitable_paths = &commitable_paths;
7466 cc_arg.worktree = worktree;
7467 cc_arg.fileindex = fileindex;
7469 cc_arg.have_staged_files = have_staged_files;
7470 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7471 err = worktree_status(worktree, "", fileindex, repo,
7472 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7476 if (TAILQ_EMPTY(&commitable_paths)) {
7477 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7478 "merge of %s cannot proceed", branch_name);
7482 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7483 struct got_commitable *ct = pe->data;
7484 const char *ct_path = ct->in_repo_path;
7486 while (ct_path[0] == '/')
7488 err = check_out_of_date(ct_path, ct->status,
7489 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7490 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7496 mcm_arg.worktree = worktree;
7497 mcm_arg.branch_name = branch_name;
7498 err = commit_worktree(new_commit_id, &commitable_paths,
7499 head_commit_id, branch_tip, worktree, author, committer,
7500 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7504 err = update_fileindex_after_commit(worktree, &commitable_paths,
7505 *new_commit_id, fileindex, have_staged_files);
7506 sync_err = sync_fileindex(fileindex, fileindex_path);
7507 if (sync_err && err == NULL)
7510 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7511 struct got_commitable *ct = pe->data;
7512 free_commitable(ct);
7514 got_pathlist_free(&commitable_paths);
7515 free(fileindex_path);
7519 const struct got_error *
7520 got_worktree_merge_complete(struct got_worktree *worktree,
7521 struct got_fileindex *fileindex, struct got_repository *repo)
7523 const struct got_error *err, *unlockerr, *sync_err;
7524 char *fileindex_path = NULL;
7526 err = delete_merge_refs(worktree, repo);
7530 err = get_fileindex_path(&fileindex_path, worktree);
7533 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7534 sync_err = sync_fileindex(fileindex, fileindex_path);
7535 if (sync_err && err == NULL)
7538 got_fileindex_free(fileindex);
7539 free(fileindex_path);
7540 unlockerr = lock_worktree(worktree, LOCK_SH);
7541 if (unlockerr && err == NULL)
7546 const struct got_error *
7547 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7548 struct got_repository *repo)
7550 const struct got_error *err;
7551 char *branch_refname = NULL;
7552 struct got_reference *branch_ref = NULL;
7556 err = get_merge_branch_ref_name(&branch_refname, worktree);
7559 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7560 free(branch_refname);
7562 if (err->code != GOT_ERR_NOT_REF)
7570 const struct got_error *got_worktree_merge_prepare(
7571 struct got_fileindex **fileindex, struct got_worktree *worktree,
7572 struct got_reference *branch, struct got_repository *repo)
7574 const struct got_error *err = NULL;
7575 char *fileindex_path = NULL;
7576 char *branch_refname = NULL, *commit_refname = NULL;
7577 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
7578 struct got_reference *commit_ref = NULL;
7579 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
7580 struct check_rebase_ok_arg ok_arg;
7584 err = lock_worktree(worktree, LOCK_EX);
7588 err = open_fileindex(fileindex, &fileindex_path, worktree);
7592 /* Preconditions are the same as for rebase. */
7593 ok_arg.worktree = worktree;
7595 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7600 err = get_merge_branch_ref_name(&branch_refname, worktree);
7604 err = get_merge_commit_ref_name(&commit_refname, worktree);
7608 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7613 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
7617 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
7618 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
7622 err = got_ref_resolve(&branch_tip, repo, branch);
7626 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
7629 err = got_ref_write(branch_ref, repo);
7633 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
7636 err = got_ref_write(commit_ref, repo);
7641 free(branch_refname);
7642 free(commit_refname);
7643 free(fileindex_path);
7645 got_ref_close(branch_ref);
7647 got_ref_close(commit_ref);
7649 got_ref_close(wt_branch);
7650 free(wt_branch_tip);
7653 got_fileindex_free(*fileindex);
7656 lock_worktree(worktree, LOCK_SH);
7661 const struct got_error *
7662 got_worktree_merge_continue(char **branch_name,
7663 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
7664 struct got_worktree *worktree, struct got_repository *repo)
7666 const struct got_error *err;
7667 char *commit_refname = NULL, *branch_refname = NULL;
7668 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
7669 char *fileindex_path = NULL;
7670 int have_staged_files = 0;
7672 *branch_name = NULL;
7676 err = lock_worktree(worktree, LOCK_EX);
7680 err = open_fileindex(fileindex, &fileindex_path, worktree);
7684 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7685 &have_staged_files);
7686 if (err && err->code != GOT_ERR_CANCELLED)
7688 if (have_staged_files) {
7689 err = got_error(GOT_ERR_STAGED_PATHS);
7693 err = get_merge_branch_ref_name(&branch_refname, worktree);
7697 err = get_merge_commit_ref_name(&commit_refname, worktree);
7701 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7705 if (!got_ref_is_symbolic(branch_ref)) {
7706 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7707 "%s is not a symbolic reference",
7708 got_ref_get_name(branch_ref));
7711 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
7712 if (*branch_name == NULL) {
7713 err = got_error_from_errno("strdup");
7717 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
7721 err = got_ref_resolve(branch_tip, repo, commit_ref);
7725 free(commit_refname);
7726 free(branch_refname);
7727 free(fileindex_path);
7729 got_ref_close(commit_ref);
7731 got_ref_close(branch_ref);
7735 *branch_name = NULL;
7740 got_fileindex_free(*fileindex);
7743 lock_worktree(worktree, LOCK_SH);
7748 const struct got_error *
7749 got_worktree_merge_abort(struct got_worktree *worktree,
7750 struct got_fileindex *fileindex, struct got_repository *repo,
7751 got_worktree_checkout_cb progress_cb, void *progress_arg)
7753 const struct got_error *err, *unlockerr, *sync_err;
7754 struct got_object_id *commit_id = NULL;
7755 char *fileindex_path = NULL;
7756 struct revert_file_args rfa;
7757 struct got_object_id *tree_id = NULL;
7759 err = got_object_id_by_path(&tree_id, repo,
7760 worktree->base_commit_id, worktree->path_prefix);
7764 err = delete_merge_refs(worktree, repo);
7768 err = get_fileindex_path(&fileindex_path, worktree);
7772 rfa.worktree = worktree;
7773 rfa.fileindex = fileindex;
7774 rfa.progress_cb = progress_cb;
7775 rfa.progress_arg = progress_arg;
7776 rfa.patch_cb = NULL;
7777 rfa.patch_arg = NULL;
7779 rfa.unlink_added_files = 1;
7780 err = worktree_status(worktree, "", fileindex, repo,
7781 revert_file, &rfa, NULL, NULL, 1, 0);
7785 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7786 repo, progress_cb, progress_arg, NULL, NULL);
7788 sync_err = sync_fileindex(fileindex, fileindex_path);
7789 if (sync_err && err == NULL)
7795 got_fileindex_free(fileindex);
7796 free(fileindex_path);
7798 unlockerr = lock_worktree(worktree, LOCK_SH);
7799 if (unlockerr && err == NULL)
7804 struct check_stage_ok_arg {
7805 struct got_object_id *head_commit_id;
7806 struct got_worktree *worktree;
7807 struct got_fileindex *fileindex;
7808 struct got_repository *repo;
7812 const struct got_error *
7813 check_stage_ok(void *arg, unsigned char status,
7814 unsigned char staged_status, const char *relpath,
7815 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7816 struct got_object_id *commit_id, int dirfd, const char *de_name)
7818 struct check_stage_ok_arg *a = arg;
7819 const struct got_error *err = NULL;
7820 struct got_fileindex_entry *ie;
7821 struct got_object_id base_commit_id;
7822 struct got_object_id *base_commit_idp = NULL;
7823 char *in_repo_path = NULL, *p;
7825 if (status == GOT_STATUS_UNVERSIONED ||
7826 status == GOT_STATUS_NO_CHANGE)
7828 if (status == GOT_STATUS_NONEXISTENT)
7829 return got_error_set_errno(ENOENT, relpath);
7831 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7833 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7835 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7836 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7838 return got_error_from_errno("asprintf");
7840 if (got_fileindex_entry_has_commit(ie)) {
7841 memcpy(base_commit_id.sha1, ie->commit_sha1,
7842 SHA1_DIGEST_LENGTH);
7843 base_commit_idp = &base_commit_id;
7846 if (status == GOT_STATUS_CONFLICT) {
7847 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7849 } else if (status != GOT_STATUS_ADD &&
7850 status != GOT_STATUS_MODIFY &&
7851 status != GOT_STATUS_DELETE) {
7852 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7856 a->have_changes = 1;
7861 err = check_out_of_date(p, status, staged_status,
7862 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7863 GOT_ERR_STAGE_OUT_OF_DATE);
7869 struct stage_path_arg {
7870 struct got_worktree *worktree;
7871 struct got_fileindex *fileindex;
7872 struct got_repository *repo;
7873 got_worktree_status_cb status_cb;
7875 got_worktree_patch_cb patch_cb;
7877 int staged_something;
7878 int allow_bad_symlinks;
7881 static const struct got_error *
7882 stage_path(void *arg, unsigned char status,
7883 unsigned char staged_status, const char *relpath,
7884 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7885 struct got_object_id *commit_id, int dirfd, const char *de_name)
7887 struct stage_path_arg *a = arg;
7888 const struct got_error *err = NULL;
7889 struct got_fileindex_entry *ie;
7890 char *ondisk_path = NULL, *path_content = NULL;
7892 struct got_object_id *new_staged_blob_id = NULL;
7895 if (status == GOT_STATUS_UNVERSIONED)
7898 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7900 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7902 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7904 return got_error_from_errno("asprintf");
7907 case GOT_STATUS_ADD:
7908 case GOT_STATUS_MODIFY:
7909 /* XXX could sb.st_mode be passed in by our caller? */
7910 if (lstat(ondisk_path, &sb) == -1) {
7911 err = got_error_from_errno2("lstat", ondisk_path);
7915 if (status == GOT_STATUS_ADD) {
7916 int choice = GOT_PATCH_CHOICE_NONE;
7917 err = (*a->patch_cb)(&choice, a->patch_arg,
7918 status, ie->path, NULL, 1, 1);
7921 if (choice != GOT_PATCH_CHOICE_YES)
7924 err = create_patched_content(&path_content, 0,
7925 staged_blob_id ? staged_blob_id : blob_id,
7926 ondisk_path, dirfd, de_name, ie->path,
7927 a->repo, a->patch_cb, a->patch_arg);
7928 if (err || path_content == NULL)
7932 err = got_object_blob_create(&new_staged_blob_id,
7933 path_content ? path_content : ondisk_path, a->repo);
7936 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7937 SHA1_DIGEST_LENGTH);
7938 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7939 stage = GOT_FILEIDX_STAGE_ADD;
7941 stage = GOT_FILEIDX_STAGE_MODIFY;
7942 got_fileindex_entry_stage_set(ie, stage);
7943 if (S_ISLNK(sb.st_mode)) {
7944 int is_bad_symlink = 0;
7945 if (!a->allow_bad_symlinks) {
7946 char target_path[PATH_MAX];
7948 target_len = readlink(ondisk_path, target_path,
7949 sizeof(target_path));
7950 if (target_len == -1) {
7951 err = got_error_from_errno2("readlink",
7955 err = is_bad_symlink_target(&is_bad_symlink,
7956 target_path, target_len, ondisk_path,
7957 a->worktree->root_path);
7960 if (is_bad_symlink) {
7961 err = got_error_path(ondisk_path,
7962 GOT_ERR_BAD_SYMLINK);
7967 got_fileindex_entry_staged_filetype_set(ie,
7968 GOT_FILEIDX_MODE_BAD_SYMLINK);
7970 got_fileindex_entry_staged_filetype_set(ie,
7971 GOT_FILEIDX_MODE_SYMLINK);
7973 got_fileindex_entry_staged_filetype_set(ie,
7974 GOT_FILEIDX_MODE_REGULAR_FILE);
7976 a->staged_something = 1;
7977 if (a->status_cb == NULL)
7979 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7980 get_staged_status(ie), relpath, blob_id,
7981 new_staged_blob_id, NULL, dirfd, de_name);
7983 case GOT_STATUS_DELETE:
7984 if (staged_status == GOT_STATUS_DELETE)
7987 int choice = GOT_PATCH_CHOICE_NONE;
7988 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7989 ie->path, NULL, 1, 1);
7992 if (choice == GOT_PATCH_CHOICE_NO)
7994 if (choice != GOT_PATCH_CHOICE_YES) {
7995 err = got_error(GOT_ERR_PATCH_CHOICE);
7999 stage = GOT_FILEIDX_STAGE_DELETE;
8000 got_fileindex_entry_stage_set(ie, stage);
8001 a->staged_something = 1;
8002 if (a->status_cb == NULL)
8004 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8005 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8008 case GOT_STATUS_NO_CHANGE:
8010 case GOT_STATUS_CONFLICT:
8011 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8013 case GOT_STATUS_NONEXISTENT:
8014 err = got_error_set_errno(ENOENT, relpath);
8017 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8021 if (path_content && unlink(path_content) == -1 && err == NULL)
8022 err = got_error_from_errno2("unlink", path_content);
8025 free(new_staged_blob_id);
8029 const struct got_error *
8030 got_worktree_stage(struct got_worktree *worktree,
8031 struct got_pathlist_head *paths,
8032 got_worktree_status_cb status_cb, void *status_arg,
8033 got_worktree_patch_cb patch_cb, void *patch_arg,
8034 int allow_bad_symlinks, struct got_repository *repo)
8036 const struct got_error *err = NULL, *sync_err, *unlockerr;
8037 struct got_pathlist_entry *pe;
8038 struct got_fileindex *fileindex = NULL;
8039 char *fileindex_path = NULL;
8040 struct got_reference *head_ref = NULL;
8041 struct got_object_id *head_commit_id = NULL;
8042 struct check_stage_ok_arg oka;
8043 struct stage_path_arg spa;
8045 err = lock_worktree(worktree, LOCK_EX);
8049 err = got_ref_open(&head_ref, repo,
8050 got_worktree_get_head_ref_name(worktree), 0);
8053 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8056 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8060 /* Check pre-conditions before staging anything. */
8061 oka.head_commit_id = head_commit_id;
8062 oka.worktree = worktree;
8063 oka.fileindex = fileindex;
8065 oka.have_changes = 0;
8066 TAILQ_FOREACH(pe, paths, entry) {
8067 err = worktree_status(worktree, pe->path, fileindex, repo,
8068 check_stage_ok, &oka, NULL, NULL, 1, 0);
8072 if (!oka.have_changes) {
8073 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8077 spa.worktree = worktree;
8078 spa.fileindex = fileindex;
8080 spa.patch_cb = patch_cb;
8081 spa.patch_arg = patch_arg;
8082 spa.status_cb = status_cb;
8083 spa.status_arg = status_arg;
8084 spa.staged_something = 0;
8085 spa.allow_bad_symlinks = allow_bad_symlinks;
8086 TAILQ_FOREACH(pe, paths, entry) {
8087 err = worktree_status(worktree, pe->path, fileindex, repo,
8088 stage_path, &spa, NULL, NULL, 1, 0);
8092 if (!spa.staged_something) {
8093 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8097 sync_err = sync_fileindex(fileindex, fileindex_path);
8098 if (sync_err && err == NULL)
8102 got_ref_close(head_ref);
8103 free(head_commit_id);
8104 free(fileindex_path);
8106 got_fileindex_free(fileindex);
8107 unlockerr = lock_worktree(worktree, LOCK_SH);
8108 if (unlockerr && err == NULL)
8113 struct unstage_path_arg {
8114 struct got_worktree *worktree;
8115 struct got_fileindex *fileindex;
8116 struct got_repository *repo;
8117 got_worktree_checkout_cb progress_cb;
8119 got_worktree_patch_cb patch_cb;
8123 static const struct got_error *
8124 create_unstaged_content(char **path_unstaged_content,
8125 char **path_new_staged_content, struct got_object_id *blob_id,
8126 struct got_object_id *staged_blob_id, const char *relpath,
8127 struct got_repository *repo,
8128 got_worktree_patch_cb patch_cb, void *patch_arg)
8130 const struct got_error *err, *free_err;
8131 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8132 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8133 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8134 struct got_diffreg_result *diffreg_result = NULL;
8135 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8136 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8138 *path_unstaged_content = NULL;
8139 *path_new_staged_content = NULL;
8141 err = got_object_id_str(&label1, blob_id);
8144 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
8148 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
8152 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8156 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
8160 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
8164 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8168 err = got_diff_files(&diffreg_result, f1, label1, f2,
8169 path2, 3, 0, 1, NULL);
8173 err = got_opentemp_named(path_unstaged_content, &outfile,
8174 "got-unstaged-content");
8177 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8178 "got-new-staged-content");
8182 if (fseek(f1, 0L, SEEK_SET) == -1) {
8183 err = got_ferror(f1, GOT_ERR_IO);
8186 if (fseek(f2, 0L, SEEK_SET) == -1) {
8187 err = got_ferror(f2, GOT_ERR_IO);
8190 /* Count the number of actual changes in the diff result. */
8191 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8192 struct diff_chunk_context cc = {};
8193 diff_chunk_context_load_change(&cc, &nchunks_used,
8194 diffreg_result->result, n, 0);
8197 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8199 err = apply_or_reject_change(&choice, &nchunks_used,
8200 diffreg_result->result, n, relpath, f1, f2,
8201 &line_cur1, &line_cur2,
8202 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8205 if (choice == GOT_PATCH_CHOICE_YES)
8208 have_rejected_content = 1;
8209 if (choice == GOT_PATCH_CHOICE_QUIT)
8212 if (have_content || have_rejected_content)
8213 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8214 outfile, rejectfile);
8218 got_object_blob_close(blob);
8220 got_object_blob_close(staged_blob);
8221 free_err = got_diffreg_result_free(diffreg_result);
8222 if (free_err && err == NULL)
8224 if (f1 && fclose(f1) == EOF && err == NULL)
8225 err = got_error_from_errno2("fclose", path1);
8226 if (f2 && fclose(f2) == EOF && err == NULL)
8227 err = got_error_from_errno2("fclose", path2);
8228 if (outfile && fclose(outfile) == EOF && err == NULL)
8229 err = got_error_from_errno2("fclose", *path_unstaged_content);
8230 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8231 err = got_error_from_errno2("fclose", *path_new_staged_content);
8232 if (path1 && unlink(path1) == -1 && err == NULL)
8233 err = got_error_from_errno2("unlink", path1);
8234 if (path2 && unlink(path2) == -1 && err == NULL)
8235 err = got_error_from_errno2("unlink", path2);
8236 if (err || !have_content) {
8237 if (*path_unstaged_content &&
8238 unlink(*path_unstaged_content) == -1 && err == NULL)
8239 err = got_error_from_errno2("unlink",
8240 *path_unstaged_content);
8241 free(*path_unstaged_content);
8242 *path_unstaged_content = NULL;
8244 if (err || !have_content || !have_rejected_content) {
8245 if (*path_new_staged_content &&
8246 unlink(*path_new_staged_content) == -1 && err == NULL)
8247 err = got_error_from_errno2("unlink",
8248 *path_new_staged_content);
8249 free(*path_new_staged_content);
8250 *path_new_staged_content = NULL;
8257 static const struct got_error *
8258 unstage_hunks(struct got_object_id *staged_blob_id,
8259 struct got_blob_object *blob_base,
8260 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8261 const char *ondisk_path, const char *label_orig,
8262 struct got_worktree *worktree, struct got_repository *repo,
8263 got_worktree_patch_cb patch_cb, void *patch_arg,
8264 got_worktree_checkout_cb progress_cb, void *progress_arg)
8266 const struct got_error *err = NULL;
8267 char *path_unstaged_content = NULL;
8268 char *path_new_staged_content = NULL;
8269 char *parent = NULL, *base_path = NULL;
8270 char *blob_base_path = NULL;
8271 struct got_object_id *new_staged_blob_id = NULL;
8272 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8275 err = create_unstaged_content(&path_unstaged_content,
8276 &path_new_staged_content, blob_id, staged_blob_id,
8277 ie->path, repo, patch_cb, patch_arg);
8281 if (path_unstaged_content == NULL)
8284 if (path_new_staged_content) {
8285 err = got_object_blob_create(&new_staged_blob_id,
8286 path_new_staged_content, repo);
8291 f = fopen(path_unstaged_content, "re");
8293 err = got_error_from_errno2("fopen",
8294 path_unstaged_content);
8297 if (fstat(fileno(f), &sb) == -1) {
8298 err = got_error_from_errno2("fstat", path_unstaged_content);
8301 if (got_fileindex_entry_staged_filetype_get(ie) ==
8302 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8303 char link_target[PATH_MAX];
8305 r = fread(link_target, 1, sizeof(link_target), f);
8306 if (r == 0 && ferror(f)) {
8307 err = got_error_from_errno("fread");
8310 if (r >= sizeof(link_target)) { /* should not happen */
8311 err = got_error(GOT_ERR_NO_SPACE);
8314 link_target[r] = '\0';
8315 err = merge_symlink(worktree, blob_base,
8316 ondisk_path, ie->path, label_orig, link_target,
8317 worktree->base_commit_id, repo, progress_cb,
8320 int local_changes_subsumed;
8322 err = got_path_dirname(&parent, ondisk_path);
8326 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8328 err = got_error_from_errno("asprintf");
8333 err = got_opentemp_named(&blob_base_path, &f_base, base_path);
8336 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8342 * In order the run a 3-way merge with a symlink we copy the symlink's
8343 * target path into a temporary file and use that file with diff3.
8345 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8346 err = dump_symlink_target_path_to_file(&f_deriv2,
8352 fd = open(ondisk_path,
8353 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8355 err = got_error_from_errno2("open", ondisk_path);
8358 f_deriv2 = fdopen(fd, "r");
8359 if (f_deriv2 == NULL) {
8360 err = got_error_from_errno2("fdopen", ondisk_path);
8366 err = merge_file(&local_changes_subsumed, worktree,
8367 f_base, f, f_deriv2, ondisk_path, ie->path,
8368 got_fileindex_perms_to_st(ie),
8369 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8370 repo, progress_cb, progress_arg);
8375 if (new_staged_blob_id) {
8376 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8377 SHA1_DIGEST_LENGTH);
8379 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8380 got_fileindex_entry_staged_filetype_set(ie, 0);
8383 free(new_staged_blob_id);
8384 if (path_unstaged_content &&
8385 unlink(path_unstaged_content) == -1 && err == NULL)
8386 err = got_error_from_errno2("unlink", path_unstaged_content);
8387 if (path_new_staged_content &&
8388 unlink(path_new_staged_content) == -1 && err == NULL)
8389 err = got_error_from_errno2("unlink", path_new_staged_content);
8390 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8391 err = got_error_from_errno2("unlink", blob_base_path);
8392 if (f_base && fclose(f_base) == EOF && err == NULL)
8393 err = got_error_from_errno2("fclose", path_unstaged_content);
8394 if (f && fclose(f) == EOF && err == NULL)
8395 err = got_error_from_errno2("fclose", path_unstaged_content);
8396 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8397 err = got_error_from_errno2("fclose", ondisk_path);
8398 free(path_unstaged_content);
8399 free(path_new_staged_content);
8400 free(blob_base_path);
8406 static const struct got_error *
8407 unstage_path(void *arg, unsigned char status,
8408 unsigned char staged_status, const char *relpath,
8409 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8410 struct got_object_id *commit_id, int dirfd, const char *de_name)
8412 const struct got_error *err = NULL;
8413 struct unstage_path_arg *a = arg;
8414 struct got_fileindex_entry *ie;
8415 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8416 char *ondisk_path = NULL;
8417 char *id_str = NULL, *label_orig = NULL;
8418 int local_changes_subsumed;
8421 if (staged_status != GOT_STATUS_ADD &&
8422 staged_status != GOT_STATUS_MODIFY &&
8423 staged_status != GOT_STATUS_DELETE)
8426 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8428 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8430 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8432 return got_error_from_errno("asprintf");
8434 err = got_object_id_str(&id_str,
8435 commit_id ? commit_id : a->worktree->base_commit_id);
8438 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8440 err = got_error_from_errno("asprintf");
8444 switch (staged_status) {
8445 case GOT_STATUS_MODIFY:
8446 err = got_object_open_as_blob(&blob_base, a->repo,
8451 case GOT_STATUS_ADD:
8453 if (staged_status == GOT_STATUS_ADD) {
8454 int choice = GOT_PATCH_CHOICE_NONE;
8455 err = (*a->patch_cb)(&choice, a->patch_arg,
8456 staged_status, ie->path, NULL, 1, 1);
8459 if (choice != GOT_PATCH_CHOICE_YES)
8462 err = unstage_hunks(staged_blob_id,
8463 blob_base, blob_id, ie, ondisk_path,
8464 label_orig, a->worktree, a->repo,
8465 a->patch_cb, a->patch_arg,
8466 a->progress_cb, a->progress_arg);
8467 break; /* Done with this file. */
8470 err = got_object_open_as_blob(&blob_staged, a->repo,
8471 staged_blob_id, 8192);
8474 switch (got_fileindex_entry_staged_filetype_get(ie)) {
8475 case GOT_FILEIDX_MODE_BAD_SYMLINK:
8476 case GOT_FILEIDX_MODE_REGULAR_FILE:
8477 err = merge_blob(&local_changes_subsumed, a->worktree,
8478 blob_base, ondisk_path, relpath,
8479 got_fileindex_perms_to_st(ie), label_orig,
8480 blob_staged, commit_id ? commit_id :
8481 a->worktree->base_commit_id, a->repo,
8482 a->progress_cb, a->progress_arg);
8484 case GOT_FILEIDX_MODE_SYMLINK:
8485 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8486 char *staged_target;
8487 err = got_object_blob_read_to_str(
8488 &staged_target, blob_staged);
8491 err = merge_symlink(a->worktree, blob_base,
8492 ondisk_path, relpath, label_orig,
8493 staged_target, commit_id ? commit_id :
8494 a->worktree->base_commit_id,
8495 a->repo, a->progress_cb, a->progress_arg);
8496 free(staged_target);
8498 err = merge_blob(&local_changes_subsumed,
8499 a->worktree, blob_base, ondisk_path,
8500 relpath, got_fileindex_perms_to_st(ie),
8501 label_orig, blob_staged,
8502 commit_id ? commit_id :
8503 a->worktree->base_commit_id, a->repo,
8504 a->progress_cb, a->progress_arg);
8508 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8512 got_fileindex_entry_stage_set(ie,
8513 GOT_FILEIDX_STAGE_NONE);
8514 got_fileindex_entry_staged_filetype_set(ie, 0);
8517 case GOT_STATUS_DELETE:
8519 int choice = GOT_PATCH_CHOICE_NONE;
8520 err = (*a->patch_cb)(&choice, a->patch_arg,
8521 staged_status, ie->path, NULL, 1, 1);
8524 if (choice == GOT_PATCH_CHOICE_NO)
8526 if (choice != GOT_PATCH_CHOICE_YES) {
8527 err = got_error(GOT_ERR_PATCH_CHOICE);
8531 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8532 got_fileindex_entry_staged_filetype_set(ie, 0);
8533 err = get_file_status(&status, &sb, ie, ondisk_path,
8534 dirfd, de_name, a->repo);
8537 err = (*a->progress_cb)(a->progress_arg, status, relpath);
8543 got_object_blob_close(blob_base);
8545 got_object_blob_close(blob_staged);
8551 const struct got_error *
8552 got_worktree_unstage(struct got_worktree *worktree,
8553 struct got_pathlist_head *paths,
8554 got_worktree_checkout_cb progress_cb, void *progress_arg,
8555 got_worktree_patch_cb patch_cb, void *patch_arg,
8556 struct got_repository *repo)
8558 const struct got_error *err = NULL, *sync_err, *unlockerr;
8559 struct got_pathlist_entry *pe;
8560 struct got_fileindex *fileindex = NULL;
8561 char *fileindex_path = NULL;
8562 struct unstage_path_arg upa;
8564 err = lock_worktree(worktree, LOCK_EX);
8568 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8572 upa.worktree = worktree;
8573 upa.fileindex = fileindex;
8575 upa.progress_cb = progress_cb;
8576 upa.progress_arg = progress_arg;
8577 upa.patch_cb = patch_cb;
8578 upa.patch_arg = patch_arg;
8579 TAILQ_FOREACH(pe, paths, entry) {
8580 err = worktree_status(worktree, pe->path, fileindex, repo,
8581 unstage_path, &upa, NULL, NULL, 1, 0);
8586 sync_err = sync_fileindex(fileindex, fileindex_path);
8587 if (sync_err && err == NULL)
8590 free(fileindex_path);
8592 got_fileindex_free(fileindex);
8593 unlockerr = lock_worktree(worktree, LOCK_SH);
8594 if (unlockerr && err == NULL)
8599 struct report_file_info_arg {
8600 struct got_worktree *worktree;
8601 got_worktree_path_info_cb info_cb;
8603 struct got_pathlist_head *paths;
8604 got_cancel_cb cancel_cb;
8608 static const struct got_error *
8609 report_file_info(void *arg, struct got_fileindex_entry *ie)
8611 struct report_file_info_arg *a = arg;
8612 struct got_pathlist_entry *pe;
8613 struct got_object_id blob_id, staged_blob_id, commit_id;
8614 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8615 struct got_object_id *commit_idp = NULL;
8618 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8619 return got_error(GOT_ERR_CANCELLED);
8621 TAILQ_FOREACH(pe, a->paths, entry) {
8622 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8623 got_path_is_child(ie->path, pe->path, pe->path_len))
8626 if (pe == NULL) /* not found */
8629 if (got_fileindex_entry_has_blob(ie)) {
8630 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8631 blob_idp = &blob_id;
8633 stage = got_fileindex_entry_stage_get(ie);
8634 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8635 stage == GOT_FILEIDX_STAGE_ADD) {
8636 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8637 SHA1_DIGEST_LENGTH);
8638 staged_blob_idp = &staged_blob_id;
8641 if (got_fileindex_entry_has_commit(ie)) {
8642 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8643 commit_idp = &commit_id;
8646 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8647 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8650 const struct got_error *
8651 got_worktree_path_info(struct got_worktree *worktree,
8652 struct got_pathlist_head *paths,
8653 got_worktree_path_info_cb info_cb, void *info_arg,
8654 got_cancel_cb cancel_cb, void *cancel_arg)
8657 const struct got_error *err = NULL, *unlockerr;
8658 struct got_fileindex *fileindex = NULL;
8659 char *fileindex_path = NULL;
8660 struct report_file_info_arg arg;
8662 err = lock_worktree(worktree, LOCK_SH);
8666 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8670 arg.worktree = worktree;
8671 arg.info_cb = info_cb;
8672 arg.info_arg = info_arg;
8674 arg.cancel_cb = cancel_cb;
8675 arg.cancel_arg = cancel_arg;
8676 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8679 free(fileindex_path);
8681 got_fileindex_free(fileindex);
8682 unlockerr = lock_worktree(worktree, LOCK_UN);
8683 if (unlockerr && err == NULL)