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);
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, O_RDONLY | O_NOFOLLOW);
3465 if (errno != ENOENT && errno != EACCES)
3466 err = got_error_from_errno2("openat",
3469 ignoresfile = fdopen(fd, "r");
3470 if (ignoresfile == NULL)
3471 err = got_error_from_errno2("fdopen",
3475 err = read_ignores(ignores, path, ignoresfile);
3479 ignoresfile = fopen(ignorespath, "re");
3480 if (ignoresfile == NULL) {
3481 if (errno != ENOENT && errno != EACCES)
3482 err = got_error_from_errno2("fopen",
3485 err = read_ignores(ignores, path, ignoresfile);
3488 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3489 err = got_error_from_errno2("fclose", path);
3490 if (fd != -1 && close(fd) == -1 && err == NULL)
3491 err = got_error_from_errno2("close", path);
3496 static const struct got_error *
3497 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3500 const struct got_error *err = NULL;
3501 struct diff_dir_cb_arg *a = arg;
3507 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3508 return got_error(GOT_ERR_CANCELLED);
3510 if (parent_path[0]) {
3511 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3512 return got_error_from_errno("asprintf");
3517 if (de->d_type == DT_DIR) {
3518 if (!a->no_ignores && ignore != NULL &&
3519 match_ignores(a->ignores, path))
3521 } else if (!match_ignores(a->ignores, path) &&
3522 got_path_is_child(path, a->status_path, a->status_path_len))
3523 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3524 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3530 static const struct got_error *
3531 status_traverse(void *arg, const char *path, int dirfd)
3533 const struct got_error *err = NULL;
3534 struct diff_dir_cb_arg *a = arg;
3539 err = add_ignores(a->ignores, a->worktree->root_path,
3540 path, dirfd, ".cvsignore");
3544 err = add_ignores(a->ignores, a->worktree->root_path, path,
3545 dirfd, ".gitignore");
3550 static const struct got_error *
3551 report_single_file_status(const char *path, const char *ondisk_path,
3552 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3553 void *status_arg, struct got_repository *repo, int report_unchanged,
3554 struct got_pathlist_head *ignores, int no_ignores)
3556 struct got_fileindex_entry *ie;
3559 if (!no_ignores && match_ignores(ignores, path))
3562 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3564 return report_file_status(ie, ondisk_path, -1, NULL,
3565 status_cb, status_arg, repo, report_unchanged);
3567 if (lstat(ondisk_path, &sb) == -1) {
3568 if (errno != ENOENT)
3569 return got_error_from_errno2("lstat", ondisk_path);
3570 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3571 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3575 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3576 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3577 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3582 static const struct got_error *
3583 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3584 const char *root_path, const char *path)
3586 const struct got_error *err;
3587 char *parent_path, *next_parent_path = NULL;
3589 err = add_ignores(ignores, root_path, "", -1,
3594 err = add_ignores(ignores, root_path, "", -1,
3599 err = got_path_dirname(&parent_path, path);
3601 if (err->code == GOT_ERR_BAD_PATH)
3602 return NULL; /* cannot traverse parent */
3606 err = add_ignores(ignores, root_path, parent_path, -1,
3610 err = add_ignores(ignores, root_path, parent_path, -1,
3614 err = got_path_dirname(&next_parent_path, parent_path);
3616 if (err->code == GOT_ERR_BAD_PATH)
3617 err = NULL; /* traversed everything */
3620 if (got_path_is_root_dir(parent_path))
3623 parent_path = next_parent_path;
3624 next_parent_path = NULL;
3628 free(next_parent_path);
3632 static const struct got_error *
3633 worktree_status(struct got_worktree *worktree, const char *path,
3634 struct got_fileindex *fileindex, struct got_repository *repo,
3635 got_worktree_status_cb status_cb, void *status_arg,
3636 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3637 int report_unchanged)
3639 const struct got_error *err = NULL;
3641 struct got_fileindex_diff_dir_cb fdiff_cb;
3642 struct diff_dir_cb_arg arg;
3643 char *ondisk_path = NULL;
3644 struct got_pathlist_head ignores;
3646 TAILQ_INIT(&ignores);
3648 if (asprintf(&ondisk_path, "%s%s%s",
3649 worktree->root_path, path[0] ? "/" : "", path) == -1)
3650 return got_error_from_errno("asprintf");
3652 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3654 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3655 !got_err_open_nofollow_on_symlink())
3656 err = got_error_from_errno2("open", ondisk_path);
3659 err = add_ignores_from_parent_paths(&ignores,
3660 worktree->root_path, ondisk_path);
3664 err = report_single_file_status(path, ondisk_path,
3665 fileindex, status_cb, status_arg, repo,
3666 report_unchanged, &ignores, no_ignores);
3669 fdiff_cb.diff_old_new = status_old_new;
3670 fdiff_cb.diff_old = status_old;
3671 fdiff_cb.diff_new = status_new;
3672 fdiff_cb.diff_traverse = status_traverse;
3673 arg.fileindex = fileindex;
3674 arg.worktree = worktree;
3675 arg.status_path = path;
3676 arg.status_path_len = strlen(path);
3678 arg.status_cb = status_cb;
3679 arg.status_arg = status_arg;
3680 arg.cancel_cb = cancel_cb;
3681 arg.cancel_arg = cancel_arg;
3682 arg.report_unchanged = report_unchanged;
3683 arg.no_ignores = no_ignores;
3685 err = add_ignores_from_parent_paths(&ignores,
3686 worktree->root_path, path);
3690 arg.ignores = &ignores;
3691 err = got_fileindex_diff_dir(fileindex, fd,
3692 worktree->root_path, path, repo, &fdiff_cb, &arg);
3695 free_ignores(&ignores);
3696 if (fd != -1 && close(fd) == -1 && err == NULL)
3697 err = got_error_from_errno("close");
3702 const struct got_error *
3703 got_worktree_status(struct got_worktree *worktree,
3704 struct got_pathlist_head *paths, struct got_repository *repo,
3705 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3706 got_cancel_cb cancel_cb, void *cancel_arg)
3708 const struct got_error *err = NULL;
3709 char *fileindex_path = NULL;
3710 struct got_fileindex *fileindex = NULL;
3711 struct got_pathlist_entry *pe;
3713 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3717 TAILQ_FOREACH(pe, paths, entry) {
3718 err = worktree_status(worktree, pe->path, fileindex, repo,
3719 status_cb, status_arg, cancel_cb, cancel_arg,
3724 free(fileindex_path);
3725 got_fileindex_free(fileindex);
3729 const struct got_error *
3730 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3733 const struct got_error *err = NULL;
3734 char *resolved = NULL, *cwd = NULL, *path = NULL;
3737 char *abspath = NULL;
3738 char canonpath[PATH_MAX];
3742 cwd = getcwd(NULL, 0);
3744 return got_error_from_errno("getcwd");
3746 if (lstat(arg, &sb) == -1) {
3747 if (errno != ENOENT) {
3748 err = got_error_from_errno2("lstat", arg);
3753 if (S_ISLNK(sb.st_mode)) {
3755 * We cannot use realpath(3) with symlinks since we want to
3756 * operate on the symlink itself.
3757 * But we can make the path absolute, assuming it is relative
3758 * to the current working directory, and then canonicalize it.
3760 if (!got_path_is_absolute(arg)) {
3761 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3762 err = got_error_from_errno("asprintf");
3767 err = got_canonpath(abspath ? abspath : arg, canonpath,
3771 resolved = strdup(canonpath);
3772 if (resolved == NULL) {
3773 err = got_error_from_errno("strdup");
3777 resolved = realpath(arg, NULL);
3778 if (resolved == NULL) {
3779 if (errno != ENOENT) {
3780 err = got_error_from_errno2("realpath", arg);
3783 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3784 err = got_error_from_errno("asprintf");
3787 err = got_canonpath(abspath, canonpath,
3791 resolved = strdup(canonpath);
3792 if (resolved == NULL) {
3793 err = got_error_from_errno("strdup");
3799 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3800 strlen(got_worktree_get_root_path(worktree)))) {
3801 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3805 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3806 err = got_path_skip_common_ancestor(&path,
3807 got_worktree_get_root_path(worktree), resolved);
3813 err = got_error_from_errno("strdup");
3818 /* XXX status walk can't deal with trailing slash! */
3820 while (len > 0 && path[len - 1] == '/') {
3821 path[len - 1] = '\0';
3835 struct schedule_addition_args {
3836 struct got_worktree *worktree;
3837 struct got_fileindex *fileindex;
3838 got_worktree_checkout_cb progress_cb;
3840 struct got_repository *repo;
3843 static const struct got_error *
3844 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3845 const char *relpath, struct got_object_id *blob_id,
3846 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3847 int dirfd, const char *de_name)
3849 struct schedule_addition_args *a = arg;
3850 const struct got_error *err = NULL;
3851 struct got_fileindex_entry *ie;
3855 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3857 return got_error_from_errno("asprintf");
3859 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3861 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3865 /* Re-adding an existing entry is a no-op. */
3866 if (status == GOT_STATUS_ADD)
3868 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3873 if (status != GOT_STATUS_UNVERSIONED) {
3874 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3878 err = got_fileindex_entry_alloc(&ie, relpath);
3881 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3882 relpath, NULL, NULL, 1);
3884 got_fileindex_entry_free(ie);
3887 err = got_fileindex_entry_add(a->fileindex, ie);
3889 got_fileindex_entry_free(ie);
3896 if (status == GOT_STATUS_ADD)
3898 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3901 const struct got_error *
3902 got_worktree_schedule_add(struct got_worktree *worktree,
3903 struct got_pathlist_head *paths,
3904 got_worktree_checkout_cb progress_cb, void *progress_arg,
3905 struct got_repository *repo, int no_ignores)
3907 struct got_fileindex *fileindex = NULL;
3908 char *fileindex_path = NULL;
3909 const struct got_error *err = NULL, *sync_err, *unlockerr;
3910 struct got_pathlist_entry *pe;
3911 struct schedule_addition_args saa;
3913 err = lock_worktree(worktree, LOCK_EX);
3917 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3921 saa.worktree = worktree;
3922 saa.fileindex = fileindex;
3923 saa.progress_cb = progress_cb;
3924 saa.progress_arg = progress_arg;
3927 TAILQ_FOREACH(pe, paths, entry) {
3928 err = worktree_status(worktree, pe->path, fileindex, repo,
3929 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3933 sync_err = sync_fileindex(fileindex, fileindex_path);
3934 if (sync_err && err == NULL)
3937 free(fileindex_path);
3939 got_fileindex_free(fileindex);
3940 unlockerr = lock_worktree(worktree, LOCK_SH);
3941 if (unlockerr && err == NULL)
3946 struct schedule_deletion_args {
3947 struct got_worktree *worktree;
3948 struct got_fileindex *fileindex;
3949 got_worktree_delete_cb progress_cb;
3951 struct got_repository *repo;
3952 int delete_local_mods;
3954 const char *status_codes;
3957 static const struct got_error *
3958 schedule_for_deletion(void *arg, unsigned char status,
3959 unsigned char staged_status, const char *relpath,
3960 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3961 struct got_object_id *commit_id, int dirfd, const char *de_name)
3963 struct schedule_deletion_args *a = arg;
3964 const struct got_error *err = NULL;
3965 struct got_fileindex_entry *ie = NULL;
3969 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3971 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3973 staged_status = get_staged_status(ie);
3974 if (staged_status != GOT_STATUS_NO_CHANGE) {
3975 if (staged_status == GOT_STATUS_DELETE)
3977 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3980 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3982 return got_error_from_errno("asprintf");
3984 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3989 if (a->status_codes) {
3990 size_t ncodes = strlen(a->status_codes);
3992 for (i = 0; i < ncodes ; i++) {
3993 if (status == a->status_codes[i])
3997 /* Do not delete files in non-matching status. */
4001 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4002 a->status_codes[i] != GOT_STATUS_MISSING) {
4003 static char msg[64];
4004 snprintf(msg, sizeof(msg),
4005 "invalid status code '%c'", a->status_codes[i]);
4006 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4011 if (status != GOT_STATUS_NO_CHANGE) {
4012 if (status == GOT_STATUS_DELETE)
4014 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4015 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4018 if (status != GOT_STATUS_MODIFY &&
4019 status != GOT_STATUS_MISSING) {
4020 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4025 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4029 if (unlinkat(dirfd, de_name, 0) != 0) {
4030 err = got_error_from_errno2("unlinkat",
4034 } else if (unlink(ondisk_path) != 0) {
4035 err = got_error_from_errno2("unlink", ondisk_path);
4039 root_len = strlen(a->worktree->root_path);
4042 err = got_path_dirname(&parent, ondisk_path);
4046 ondisk_path = parent;
4047 if (rmdir(ondisk_path) == -1) {
4048 if (errno != ENOTEMPTY)
4049 err = got_error_from_errno2("rmdir",
4053 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4054 strlen(ondisk_path), root_len) != 0);
4057 got_fileindex_entry_mark_deleted_from_disk(ie);
4062 if (status == GOT_STATUS_DELETE)
4064 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4065 staged_status, relpath);
4068 const struct got_error *
4069 got_worktree_schedule_delete(struct got_worktree *worktree,
4070 struct got_pathlist_head *paths, int delete_local_mods,
4071 const char *status_codes,
4072 got_worktree_delete_cb progress_cb, void *progress_arg,
4073 struct got_repository *repo, int keep_on_disk)
4075 struct got_fileindex *fileindex = NULL;
4076 char *fileindex_path = NULL;
4077 const struct got_error *err = NULL, *sync_err, *unlockerr;
4078 struct got_pathlist_entry *pe;
4079 struct schedule_deletion_args sda;
4081 err = lock_worktree(worktree, LOCK_EX);
4085 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4089 sda.worktree = worktree;
4090 sda.fileindex = fileindex;
4091 sda.progress_cb = progress_cb;
4092 sda.progress_arg = progress_arg;
4094 sda.delete_local_mods = delete_local_mods;
4095 sda.keep_on_disk = keep_on_disk;
4096 sda.status_codes = status_codes;
4098 TAILQ_FOREACH(pe, paths, entry) {
4099 err = worktree_status(worktree, pe->path, fileindex, repo,
4100 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4104 sync_err = sync_fileindex(fileindex, fileindex_path);
4105 if (sync_err && err == NULL)
4108 free(fileindex_path);
4110 got_fileindex_free(fileindex);
4111 unlockerr = lock_worktree(worktree, LOCK_SH);
4112 if (unlockerr && err == NULL)
4117 static const struct got_error *
4118 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4120 const struct got_error *err = NULL;
4122 size_t linesize = 0, n;
4125 linelen = getline(&line, &linesize, infile);
4126 if (linelen == -1) {
4127 if (ferror(infile)) {
4128 err = got_error_from_errno("getline");
4134 n = fwrite(line, 1, linelen, outfile);
4136 err = got_ferror(outfile, GOT_ERR_IO);
4141 n = fwrite(line, 1, linelen, rejectfile);
4143 err = got_ferror(outfile, GOT_ERR_IO);
4150 static const struct got_error *
4151 skip_one_line(FILE *f)
4154 size_t linesize = 0;
4157 linelen = getline(&line, &linesize, f);
4158 if (linelen == -1) {
4160 return got_error_from_errno("getline");
4167 static const struct got_error *
4168 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4169 int start_old, int end_old, int start_new, int end_new,
4170 FILE *outfile, FILE *rejectfile)
4172 const struct got_error *err;
4174 /* Copy old file's lines leading up to patch. */
4175 while (!feof(f1) && *line_cur1 < start_old) {
4176 err = copy_one_line(f1, outfile, NULL);
4181 /* Skip new file's lines leading up to patch. */
4182 while (!feof(f2) && *line_cur2 < start_new) {
4184 err = copy_one_line(f2, NULL, rejectfile);
4186 err = skip_one_line(f2);
4191 /* Copy patched lines. */
4192 while (!feof(f2) && *line_cur2 <= end_new) {
4193 err = copy_one_line(f2, outfile, NULL);
4198 /* Skip over old file's replaced lines. */
4199 while (!feof(f1) && *line_cur1 <= end_old) {
4201 err = copy_one_line(f1, NULL, rejectfile);
4203 err = skip_one_line(f1);
4212 static const struct got_error *
4213 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4214 FILE *outfile, FILE *rejectfile)
4216 const struct got_error *err;
4219 /* Copy old file's lines until EOF. */
4221 err = copy_one_line(f1, outfile, NULL);
4228 /* Copy new file's lines until EOF. */
4230 err = copy_one_line(f2, NULL, rejectfile);
4240 static const struct got_error *
4241 apply_or_reject_change(int *choice, int *nchunks_used,
4242 struct diff_result *diff_result, int n,
4243 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4244 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4245 got_worktree_patch_cb patch_cb, void *patch_arg)
4247 const struct got_error *err = NULL;
4248 struct diff_chunk_context cc = {};
4249 int start_old, end_old, start_new, end_new;
4251 struct diff_output_unidiff_state *diff_state;
4252 struct diff_input_info diff_info;
4255 *choice = GOT_PATCH_CHOICE_NONE;
4257 /* Get changed line numbers without context lines for copy_change(). */
4258 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4259 start_old = cc.left.start;
4260 end_old = cc.left.end;
4261 start_new = cc.right.start;
4262 end_new = cc.right.end;
4264 /* Get the same change with context lines for display. */
4265 memset(&cc, 0, sizeof(cc));
4266 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4268 memset(&diff_info, 0, sizeof(diff_info));
4269 diff_info.left_path = relpath;
4270 diff_info.right_path = relpath;
4272 diff_state = diff_output_unidiff_state_alloc();
4273 if (diff_state == NULL)
4274 return got_error_set_errno(ENOMEM,
4275 "diff_output_unidiff_state_alloc");
4277 hunkfile = got_opentemp();
4278 if (hunkfile == NULL) {
4279 err = got_error_from_errno("got_opentemp");
4283 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4285 if (rc != DIFF_RC_OK) {
4286 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4290 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4291 err = got_ferror(hunkfile, GOT_ERR_IO);
4295 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4296 hunkfile, changeno, nchanges);
4301 case GOT_PATCH_CHOICE_YES:
4302 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4303 end_old, start_new, end_new, outfile, rejectfile);
4305 case GOT_PATCH_CHOICE_NO:
4306 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4307 end_old, start_new, end_new, rejectfile, outfile);
4309 case GOT_PATCH_CHOICE_QUIT:
4312 err = got_error(GOT_ERR_PATCH_CHOICE);
4316 diff_output_unidiff_state_free(diff_state);
4317 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4318 err = got_error_from_errno("fclose");
4322 struct revert_file_args {
4323 struct got_worktree *worktree;
4324 struct got_fileindex *fileindex;
4325 got_worktree_checkout_cb progress_cb;
4327 got_worktree_patch_cb patch_cb;
4329 struct got_repository *repo;
4330 int unlink_added_files;
4333 static const struct got_error *
4334 create_patched_content(char **path_outfile, int reverse_patch,
4335 struct got_object_id *blob_id, const char *path2,
4336 int dirfd2, const char *de_name2,
4337 const char *relpath, struct got_repository *repo,
4338 got_worktree_patch_cb patch_cb, void *patch_arg)
4340 const struct got_error *err, *free_err;
4341 struct got_blob_object *blob = NULL;
4342 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4344 char link_target[PATH_MAX];
4345 ssize_t link_len = 0;
4346 char *path1 = NULL, *id_str = NULL;
4348 struct got_diffreg_result *diffreg_result = NULL;
4349 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4350 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4352 *path_outfile = NULL;
4354 err = got_object_id_str(&id_str, blob_id);
4359 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4361 if (!got_err_open_nofollow_on_symlink()) {
4362 err = got_error_from_errno2("openat", path2);
4365 link_len = readlinkat(dirfd2, de_name2,
4366 link_target, sizeof(link_target));
4368 return got_error_from_errno2("readlinkat", path2);
4369 sb2.st_mode = S_IFLNK;
4370 sb2.st_size = link_len;
4373 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4375 if (!got_err_open_nofollow_on_symlink()) {
4376 err = got_error_from_errno2("open", path2);
4379 link_len = readlink(path2, link_target,
4380 sizeof(link_target));
4382 return got_error_from_errno2("readlink", path2);
4383 sb2.st_mode = S_IFLNK;
4384 sb2.st_size = link_len;
4388 if (fstat(fd2, &sb2) == -1) {
4389 err = got_error_from_errno2("fstat", path2);
4393 f2 = fdopen(fd2, "r");
4395 err = got_error_from_errno2("fdopen", path2);
4401 f2 = got_opentemp();
4403 err = got_error_from_errno2("got_opentemp", path2);
4406 n = fwrite(link_target, 1, link_len, f2);
4407 if (n != link_len) {
4408 err = got_ferror(f2, GOT_ERR_IO);
4411 if (fflush(f2) == EOF) {
4412 err = got_error_from_errno("fflush");
4418 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4422 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4426 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4430 err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4435 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4439 if (fseek(f1, 0L, SEEK_SET) == -1)
4440 return got_ferror(f1, GOT_ERR_IO);
4441 if (fseek(f2, 0L, SEEK_SET) == -1)
4442 return got_ferror(f2, GOT_ERR_IO);
4444 /* Count the number of actual changes in the diff result. */
4445 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4446 struct diff_chunk_context cc = {};
4447 diff_chunk_context_load_change(&cc, &nchunks_used,
4448 diffreg_result->result, n, 0);
4451 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4453 err = apply_or_reject_change(&choice, &nchunks_used,
4454 diffreg_result->result, n, relpath, f1, f2,
4455 &line_cur1, &line_cur2,
4456 reverse_patch ? NULL : outfile,
4457 reverse_patch ? outfile : NULL,
4458 ++i, nchanges, patch_cb, patch_arg);
4461 if (choice == GOT_PATCH_CHOICE_YES)
4463 else if (choice == GOT_PATCH_CHOICE_QUIT)
4467 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4468 reverse_patch ? NULL : outfile,
4469 reverse_patch ? outfile : NULL);
4473 if (!S_ISLNK(sb2.st_mode)) {
4474 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4475 err = got_error_from_errno2("fchmod", path2);
4483 got_object_blob_close(blob);
4484 free_err = got_diffreg_result_free(diffreg_result);
4487 if (f1 && fclose(f1) == EOF && err == NULL)
4488 err = got_error_from_errno2("fclose", path1);
4489 if (f2 && fclose(f2) == EOF && err == NULL)
4490 err = got_error_from_errno2("fclose", path2);
4491 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4492 err = got_error_from_errno2("close", path2);
4493 if (outfile && fclose(outfile) == EOF && err == NULL)
4494 err = got_error_from_errno2("fclose", *path_outfile);
4495 if (path1 && unlink(path1) == -1 && err == NULL)
4496 err = got_error_from_errno2("unlink", path1);
4497 if (err || !have_content) {
4498 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4499 err = got_error_from_errno2("unlink", *path_outfile);
4500 free(*path_outfile);
4501 *path_outfile = NULL;
4507 static const struct got_error *
4508 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4509 const char *relpath, struct got_object_id *blob_id,
4510 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4511 int dirfd, const char *de_name)
4513 struct revert_file_args *a = arg;
4514 const struct got_error *err = NULL;
4515 char *parent_path = NULL;
4516 struct got_fileindex_entry *ie;
4517 struct got_tree_object *tree = NULL;
4518 struct got_object_id *tree_id = NULL;
4519 const struct got_tree_entry *te = NULL;
4520 char *tree_path = NULL, *te_name;
4521 char *ondisk_path = NULL, *path_content = NULL;
4522 struct got_blob_object *blob = NULL;
4524 /* Reverting a staged deletion is a no-op. */
4525 if (status == GOT_STATUS_DELETE &&
4526 staged_status != GOT_STATUS_NO_CHANGE)
4529 if (status == GOT_STATUS_UNVERSIONED)
4530 return (*a->progress_cb)(a->progress_arg,
4531 GOT_STATUS_UNVERSIONED, relpath);
4533 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4535 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4537 /* Construct in-repository path of tree which contains this blob. */
4538 err = got_path_dirname(&parent_path, ie->path);
4540 if (err->code != GOT_ERR_BAD_PATH)
4542 parent_path = strdup("/");
4543 if (parent_path == NULL) {
4544 err = got_error_from_errno("strdup");
4548 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4549 tree_path = strdup(parent_path);
4550 if (tree_path == NULL) {
4551 err = got_error_from_errno("strdup");
4555 if (got_path_is_root_dir(parent_path)) {
4556 tree_path = strdup(a->worktree->path_prefix);
4557 if (tree_path == NULL) {
4558 err = got_error_from_errno("strdup");
4562 if (asprintf(&tree_path, "%s/%s",
4563 a->worktree->path_prefix, parent_path) == -1) {
4564 err = got_error_from_errno("asprintf");
4570 err = got_object_id_by_path(&tree_id, a->repo,
4571 a->worktree->base_commit_id, tree_path);
4573 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4574 (status == GOT_STATUS_ADD ||
4575 staged_status == GOT_STATUS_ADD)))
4578 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4582 err = got_path_basename(&te_name, ie->path);
4586 te = got_object_tree_find_entry(tree, te_name);
4588 if (te == NULL && status != GOT_STATUS_ADD &&
4589 staged_status != GOT_STATUS_ADD) {
4590 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4596 case GOT_STATUS_ADD:
4598 int choice = GOT_PATCH_CHOICE_NONE;
4599 err = (*a->patch_cb)(&choice, a->patch_arg,
4600 status, ie->path, NULL, 1, 1);
4603 if (choice != GOT_PATCH_CHOICE_YES)
4606 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4610 got_fileindex_entry_remove(a->fileindex, ie);
4611 if (a->unlink_added_files) {
4612 if (asprintf(&ondisk_path, "%s/%s",
4613 got_worktree_get_root_path(a->worktree),
4615 err = got_error_from_errno("asprintf");
4618 if (unlink(ondisk_path) == -1) {
4619 err = got_error_from_errno2("unlink",
4625 case GOT_STATUS_DELETE:
4627 int choice = GOT_PATCH_CHOICE_NONE;
4628 err = (*a->patch_cb)(&choice, a->patch_arg,
4629 status, ie->path, NULL, 1, 1);
4632 if (choice != GOT_PATCH_CHOICE_YES)
4636 case GOT_STATUS_MODIFY:
4637 case GOT_STATUS_MODE_CHANGE:
4638 case GOT_STATUS_CONFLICT:
4639 case GOT_STATUS_MISSING: {
4640 struct got_object_id id;
4641 if (staged_status == GOT_STATUS_ADD ||
4642 staged_status == GOT_STATUS_MODIFY) {
4643 memcpy(id.sha1, ie->staged_blob_sha1,
4644 SHA1_DIGEST_LENGTH);
4646 memcpy(id.sha1, ie->blob_sha1,
4647 SHA1_DIGEST_LENGTH);
4648 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4652 if (asprintf(&ondisk_path, "%s/%s",
4653 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4654 err = got_error_from_errno("asprintf");
4658 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4659 status == GOT_STATUS_CONFLICT)) {
4660 int is_bad_symlink = 0;
4661 err = create_patched_content(&path_content, 1, &id,
4662 ondisk_path, dirfd, de_name, ie->path, a->repo,
4663 a->patch_cb, a->patch_arg);
4664 if (err || path_content == NULL)
4666 if (te && S_ISLNK(te->mode)) {
4667 if (unlink(path_content) == -1) {
4668 err = got_error_from_errno2("unlink",
4672 err = install_symlink(&is_bad_symlink,
4673 a->worktree, ondisk_path, ie->path,
4674 blob, 0, 1, 0, 0, a->repo,
4675 a->progress_cb, a->progress_arg);
4677 if (rename(path_content, ondisk_path) == -1) {
4678 err = got_error_from_errno3("rename",
4679 path_content, ondisk_path);
4684 int is_bad_symlink = 0;
4685 if (te && S_ISLNK(te->mode)) {
4686 err = install_symlink(&is_bad_symlink,
4687 a->worktree, ondisk_path, ie->path,
4688 blob, 0, 1, 0, 0, a->repo,
4689 a->progress_cb, a->progress_arg);
4691 err = install_blob(a->worktree, ondisk_path,
4693 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4694 got_fileindex_perms_to_st(ie), blob,
4695 0, 1, 0, 0, a->repo,
4696 a->progress_cb, a->progress_arg);
4700 if (status == GOT_STATUS_DELETE ||
4701 status == GOT_STATUS_MODE_CHANGE) {
4702 err = got_fileindex_entry_update(ie,
4703 a->worktree->root_fd, relpath,
4705 a->worktree->base_commit_id->sha1, 1);
4709 if (is_bad_symlink) {
4710 got_fileindex_entry_filetype_set(ie,
4711 GOT_FILEIDX_MODE_BAD_SYMLINK);
4725 got_object_blob_close(blob);
4727 got_object_tree_close(tree);
4732 const struct got_error *
4733 got_worktree_revert(struct got_worktree *worktree,
4734 struct got_pathlist_head *paths,
4735 got_worktree_checkout_cb progress_cb, void *progress_arg,
4736 got_worktree_patch_cb patch_cb, void *patch_arg,
4737 struct got_repository *repo)
4739 struct got_fileindex *fileindex = NULL;
4740 char *fileindex_path = NULL;
4741 const struct got_error *err = NULL, *unlockerr = NULL;
4742 const struct got_error *sync_err = NULL;
4743 struct got_pathlist_entry *pe;
4744 struct revert_file_args rfa;
4746 err = lock_worktree(worktree, LOCK_EX);
4750 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4754 rfa.worktree = worktree;
4755 rfa.fileindex = fileindex;
4756 rfa.progress_cb = progress_cb;
4757 rfa.progress_arg = progress_arg;
4758 rfa.patch_cb = patch_cb;
4759 rfa.patch_arg = patch_arg;
4761 rfa.unlink_added_files = 0;
4762 TAILQ_FOREACH(pe, paths, entry) {
4763 err = worktree_status(worktree, pe->path, fileindex, repo,
4764 revert_file, &rfa, NULL, NULL, 1, 0);
4768 sync_err = sync_fileindex(fileindex, fileindex_path);
4769 if (sync_err && err == NULL)
4772 free(fileindex_path);
4774 got_fileindex_free(fileindex);
4775 unlockerr = lock_worktree(worktree, LOCK_SH);
4776 if (unlockerr && err == NULL)
4782 free_commitable(struct got_commitable *ct)
4785 free(ct->in_repo_path);
4786 free(ct->ondisk_path);
4788 free(ct->base_blob_id);
4789 free(ct->staged_blob_id);
4790 free(ct->base_commit_id);
4794 struct collect_commitables_arg {
4795 struct got_pathlist_head *commitable_paths;
4796 struct got_repository *repo;
4797 struct got_worktree *worktree;
4798 struct got_fileindex *fileindex;
4799 int have_staged_files;
4800 int allow_bad_symlinks;
4803 static const struct got_error *
4804 collect_commitables(void *arg, unsigned char status,
4805 unsigned char staged_status, const char *relpath,
4806 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4807 struct got_object_id *commit_id, int dirfd, const char *de_name)
4809 struct collect_commitables_arg *a = arg;
4810 const struct got_error *err = NULL;
4811 struct got_commitable *ct = NULL;
4812 struct got_pathlist_entry *new = NULL;
4813 char *parent_path = NULL, *path = NULL;
4816 if (a->have_staged_files) {
4817 if (staged_status != GOT_STATUS_MODIFY &&
4818 staged_status != GOT_STATUS_ADD &&
4819 staged_status != GOT_STATUS_DELETE)
4822 if (status == GOT_STATUS_CONFLICT)
4823 return got_error(GOT_ERR_COMMIT_CONFLICT);
4825 if (status != GOT_STATUS_MODIFY &&
4826 status != GOT_STATUS_MODE_CHANGE &&
4827 status != GOT_STATUS_ADD &&
4828 status != GOT_STATUS_DELETE)
4832 if (asprintf(&path, "/%s", relpath) == -1) {
4833 err = got_error_from_errno("asprintf");
4836 if (strcmp(path, "/") == 0) {
4837 parent_path = strdup("");
4838 if (parent_path == NULL)
4839 return got_error_from_errno("strdup");
4841 err = got_path_dirname(&parent_path, path);
4846 ct = calloc(1, sizeof(*ct));
4848 err = got_error_from_errno("calloc");
4852 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4854 err = got_error_from_errno("asprintf");
4858 if (staged_status == GOT_STATUS_ADD ||
4859 staged_status == GOT_STATUS_MODIFY) {
4860 struct got_fileindex_entry *ie;
4861 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4862 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4863 case GOT_FILEIDX_MODE_REGULAR_FILE:
4864 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4867 case GOT_FILEIDX_MODE_SYMLINK:
4871 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4874 ct->mode |= got_fileindex_entry_perms_get(ie);
4875 } else if (status != GOT_STATUS_DELETE &&
4876 staged_status != GOT_STATUS_DELETE) {
4878 if (fstatat(dirfd, de_name, &sb,
4879 AT_SYMLINK_NOFOLLOW) == -1) {
4880 err = got_error_from_errno2("fstatat",
4884 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4885 err = got_error_from_errno2("lstat", ct->ondisk_path);
4888 ct->mode = sb.st_mode;
4891 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4892 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4894 err = got_error_from_errno("asprintf");
4898 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4899 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4901 char target_path[PATH_MAX];
4903 target_len = readlink(ct->ondisk_path, target_path,
4904 sizeof(target_path));
4905 if (target_len == -1) {
4906 err = got_error_from_errno2("readlink",
4910 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4911 target_len, ct->ondisk_path, a->worktree->root_path);
4914 if (is_bad_symlink) {
4915 err = got_error_path(ct->ondisk_path,
4916 GOT_ERR_BAD_SYMLINK);
4922 ct->status = status;
4923 ct->staged_status = staged_status;
4924 ct->blob_id = NULL; /* will be filled in when blob gets created */
4925 if (ct->status != GOT_STATUS_ADD &&
4926 ct->staged_status != GOT_STATUS_ADD) {
4927 ct->base_blob_id = got_object_id_dup(blob_id);
4928 if (ct->base_blob_id == NULL) {
4929 err = got_error_from_errno("got_object_id_dup");
4932 ct->base_commit_id = got_object_id_dup(commit_id);
4933 if (ct->base_commit_id == NULL) {
4934 err = got_error_from_errno("got_object_id_dup");
4938 if (ct->staged_status == GOT_STATUS_ADD ||
4939 ct->staged_status == GOT_STATUS_MODIFY) {
4940 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4941 if (ct->staged_blob_id == NULL) {
4942 err = got_error_from_errno("got_object_id_dup");
4946 ct->path = strdup(path);
4947 if (ct->path == NULL) {
4948 err = got_error_from_errno("strdup");
4951 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4953 if (ct && (err || new == NULL))
4954 free_commitable(ct);
4960 static const struct got_error *write_tree(struct got_object_id **, int *,
4961 struct got_tree_object *, const char *, struct got_pathlist_head *,
4962 got_worktree_status_cb status_cb, void *status_arg,
4963 struct got_repository *);
4965 static const struct got_error *
4966 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4967 struct got_tree_entry *te, const char *parent_path,
4968 struct got_pathlist_head *commitable_paths,
4969 got_worktree_status_cb status_cb, void *status_arg,
4970 struct got_repository *repo)
4972 const struct got_error *err = NULL;
4973 struct got_tree_object *subtree;
4976 if (asprintf(&subpath, "%s%s%s", parent_path,
4977 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4978 return got_error_from_errno("asprintf");
4980 err = got_object_open_as_tree(&subtree, repo, &te->id);
4984 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4985 commitable_paths, status_cb, status_arg, repo);
4986 got_object_tree_close(subtree);
4991 static const struct got_error *
4992 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4994 const struct got_error *err = NULL;
4995 char *ct_parent_path = NULL;
4999 if (strchr(ct->in_repo_path, '/') == NULL) {
5000 *match = got_path_is_root_dir(path);
5004 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5007 *match = (strcmp(path, ct_parent_path) == 0);
5008 free(ct_parent_path);
5013 get_ct_file_mode(struct got_commitable *ct)
5015 if (S_ISLNK(ct->mode))
5018 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5021 static const struct got_error *
5022 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5023 struct got_tree_entry *te, struct got_commitable *ct)
5025 const struct got_error *err = NULL;
5029 err = got_object_tree_entry_dup(new_te, te);
5033 (*new_te)->mode = get_ct_file_mode(ct);
5035 if (ct->staged_status == GOT_STATUS_MODIFY)
5036 memcpy(&(*new_te)->id, ct->staged_blob_id,
5037 sizeof((*new_te)->id));
5039 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5041 if (err && *new_te) {
5048 static const struct got_error *
5049 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5050 struct got_commitable *ct)
5052 const struct got_error *err = NULL;
5053 char *ct_name = NULL;
5057 *new_te = calloc(1, sizeof(**new_te));
5058 if (*new_te == NULL)
5059 return got_error_from_errno("calloc");
5061 err = got_path_basename(&ct_name, ct->path);
5064 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5065 sizeof((*new_te)->name)) {
5066 err = got_error(GOT_ERR_NO_SPACE);
5070 (*new_te)->mode = get_ct_file_mode(ct);
5072 if (ct->staged_status == GOT_STATUS_ADD)
5073 memcpy(&(*new_te)->id, ct->staged_blob_id,
5074 sizeof((*new_te)->id));
5076 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5079 if (err && *new_te) {
5086 static const struct got_error *
5087 insert_tree_entry(struct got_tree_entry *new_te,
5088 struct got_pathlist_head *paths)
5090 const struct got_error *err = NULL;
5091 struct got_pathlist_entry *new_pe;
5093 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5097 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5101 static const struct got_error *
5102 report_ct_status(struct got_commitable *ct,
5103 got_worktree_status_cb status_cb, void *status_arg)
5105 const char *ct_path = ct->path;
5106 unsigned char status;
5108 if (status_cb == NULL) /* no commit progress output desired */
5111 while (ct_path[0] == '/')
5114 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5115 status = ct->staged_status;
5117 status = ct->status;
5119 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5120 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5123 static const struct got_error *
5124 match_modified_subtree(int *modified, struct got_tree_entry *te,
5125 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5127 const struct got_error *err = NULL;
5128 struct got_pathlist_entry *pe;
5133 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5134 got_path_is_root_dir(base_tree_path) ? "" : "/",
5136 return got_error_from_errno("asprintf");
5138 TAILQ_FOREACH(pe, commitable_paths, entry) {
5139 struct got_commitable *ct = pe->data;
5140 *modified = got_path_is_child(ct->in_repo_path, te_path,
5150 static const struct got_error *
5151 match_deleted_or_modified_ct(struct got_commitable **ctp,
5152 struct got_tree_entry *te, const char *base_tree_path,
5153 struct got_pathlist_head *commitable_paths)
5155 const struct got_error *err = NULL;
5156 struct got_pathlist_entry *pe;
5160 TAILQ_FOREACH(pe, commitable_paths, entry) {
5161 struct got_commitable *ct = pe->data;
5162 char *ct_name = NULL;
5165 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5166 if (ct->status != GOT_STATUS_MODIFY &&
5167 ct->status != GOT_STATUS_MODE_CHANGE &&
5168 ct->status != GOT_STATUS_DELETE)
5171 if (ct->staged_status != GOT_STATUS_MODIFY &&
5172 ct->staged_status != GOT_STATUS_DELETE)
5176 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5179 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5185 err = got_path_basename(&ct_name, pe->path);
5189 if (strcmp(te->name, ct_name) != 0) {
5202 static const struct got_error *
5203 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5204 const char *child_path, const char *path_base_tree,
5205 struct got_pathlist_head *commitable_paths,
5206 got_worktree_status_cb status_cb, void *status_arg,
5207 struct got_repository *repo)
5209 const struct got_error *err = NULL;
5210 struct got_tree_entry *new_te;
5212 struct got_object_id *id = NULL;
5217 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5218 got_path_is_root_dir(path_base_tree) ? "" : "/",
5220 return got_error_from_errno("asprintf");
5222 new_te = calloc(1, sizeof(*new_te));
5224 return got_error_from_errno("calloc");
5225 new_te->mode = S_IFDIR;
5227 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5228 sizeof(new_te->name)) {
5229 err = got_error(GOT_ERR_NO_SPACE);
5232 err = write_tree(&id, &nentries, NULL, subtree_path,
5233 commitable_paths, status_cb, status_arg, repo);
5238 memcpy(&new_te->id, id, sizeof(new_te->id));
5247 static const struct got_error *
5248 write_tree(struct got_object_id **new_tree_id, int *nentries,
5249 struct got_tree_object *base_tree, const char *path_base_tree,
5250 struct got_pathlist_head *commitable_paths,
5251 got_worktree_status_cb status_cb, void *status_arg,
5252 struct got_repository *repo)
5254 const struct got_error *err = NULL;
5255 struct got_pathlist_head paths;
5256 struct got_tree_entry *te, *new_te = NULL;
5257 struct got_pathlist_entry *pe;
5262 /* Insert, and recurse into, newly added entries first. */
5263 TAILQ_FOREACH(pe, commitable_paths, entry) {
5264 struct got_commitable *ct = pe->data;
5265 char *child_path = NULL, *slash;
5267 if ((ct->status != GOT_STATUS_ADD &&
5268 ct->staged_status != GOT_STATUS_ADD) ||
5269 (ct->flags & GOT_COMMITABLE_ADDED))
5272 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5273 strlen(path_base_tree)))
5276 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5281 slash = strchr(child_path, '/');
5282 if (slash == NULL) {
5283 err = alloc_added_blob_tree_entry(&new_te, ct);
5286 err = report_ct_status(ct, status_cb, status_arg);
5289 ct->flags |= GOT_COMMITABLE_ADDED;
5290 err = insert_tree_entry(new_te, &paths);
5295 *slash = '\0'; /* trim trailing path components */
5296 if (base_tree == NULL ||
5297 got_object_tree_find_entry(base_tree, child_path)
5299 err = make_subtree_for_added_blob(&new_te,
5300 child_path, path_base_tree,
5301 commitable_paths, status_cb, status_arg,
5305 err = insert_tree_entry(new_te, &paths);
5314 int i, nbase_entries;
5315 /* Handle modified and deleted entries. */
5316 nbase_entries = got_object_tree_get_nentries(base_tree);
5317 for (i = 0; i < nbase_entries; i++) {
5318 struct got_commitable *ct = NULL;
5320 te = got_object_tree_get_entry(base_tree, i);
5321 if (got_object_tree_entry_is_submodule(te)) {
5322 /* Entry is a submodule; just copy it. */
5323 err = got_object_tree_entry_dup(&new_te, te);
5326 err = insert_tree_entry(new_te, &paths);
5333 if (S_ISDIR(te->mode)) {
5335 err = got_object_tree_entry_dup(&new_te, te);
5338 err = match_modified_subtree(&modified, te,
5339 path_base_tree, commitable_paths);
5342 /* Avoid recursion into unmodified subtrees. */
5344 struct got_object_id *new_id;
5346 err = write_subtree(&new_id,
5348 path_base_tree, commitable_paths,
5349 status_cb, status_arg, repo);
5352 if (nsubentries == 0) {
5353 /* All entries were deleted. */
5357 memcpy(&new_te->id, new_id,
5358 sizeof(new_te->id));
5361 err = insert_tree_entry(new_te, &paths);
5368 err = match_deleted_or_modified_ct(&ct, te,
5369 path_base_tree, commitable_paths);
5373 /* NB: Deleted entries get dropped here. */
5374 if (ct->status == GOT_STATUS_MODIFY ||
5375 ct->status == GOT_STATUS_MODE_CHANGE ||
5376 ct->staged_status == GOT_STATUS_MODIFY) {
5377 err = alloc_modified_blob_tree_entry(
5381 err = insert_tree_entry(new_te, &paths);
5386 err = report_ct_status(ct, status_cb,
5391 /* Entry is unchanged; just copy it. */
5392 err = got_object_tree_entry_dup(&new_te, te);
5395 err = insert_tree_entry(new_te, &paths);
5403 /* Write new list of entries; deleted entries have been dropped. */
5404 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5406 got_pathlist_free(&paths);
5410 static const struct got_error *
5411 update_fileindex_after_commit(struct got_worktree *worktree,
5412 struct got_pathlist_head *commitable_paths,
5413 struct got_object_id *new_base_commit_id,
5414 struct got_fileindex *fileindex, int have_staged_files)
5416 const struct got_error *err = NULL;
5417 struct got_pathlist_entry *pe;
5418 char *relpath = NULL;
5420 TAILQ_FOREACH(pe, commitable_paths, entry) {
5421 struct got_fileindex_entry *ie;
5422 struct got_commitable *ct = pe->data;
5424 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5426 err = got_path_skip_common_ancestor(&relpath,
5427 worktree->root_path, ct->ondisk_path);
5432 if (ct->status == GOT_STATUS_DELETE ||
5433 ct->staged_status == GOT_STATUS_DELETE) {
5434 got_fileindex_entry_remove(fileindex, ie);
5435 } else if (ct->staged_status == GOT_STATUS_ADD ||
5436 ct->staged_status == GOT_STATUS_MODIFY) {
5437 got_fileindex_entry_stage_set(ie,
5438 GOT_FILEIDX_STAGE_NONE);
5439 got_fileindex_entry_staged_filetype_set(ie, 0);
5441 err = got_fileindex_entry_update(ie,
5442 worktree->root_fd, relpath,
5443 ct->staged_blob_id->sha1,
5444 new_base_commit_id->sha1,
5445 !have_staged_files);
5447 err = got_fileindex_entry_update(ie,
5448 worktree->root_fd, relpath,
5450 new_base_commit_id->sha1,
5451 !have_staged_files);
5453 err = got_fileindex_entry_alloc(&ie, pe->path);
5456 err = got_fileindex_entry_update(ie,
5457 worktree->root_fd, relpath, ct->blob_id->sha1,
5458 new_base_commit_id->sha1, 1);
5460 got_fileindex_entry_free(ie);
5463 err = got_fileindex_entry_add(fileindex, ie);
5465 got_fileindex_entry_free(ie);
5478 static const struct got_error *
5479 check_out_of_date(const char *in_repo_path, unsigned char status,
5480 unsigned char staged_status, struct got_object_id *base_blob_id,
5481 struct got_object_id *base_commit_id,
5482 struct got_object_id *head_commit_id, struct got_repository *repo,
5485 const struct got_error *err = NULL;
5486 struct got_object_id *id = NULL;
5488 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5489 /* Trivial case: base commit == head commit */
5490 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5493 * Ensure file content which local changes were based
5494 * on matches file content in the branch head.
5496 err = got_object_id_by_path(&id, repo, head_commit_id,
5499 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5500 err = got_error(ood_errcode);
5502 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5503 err = got_error(ood_errcode);
5505 /* Require that added files don't exist in the branch head. */
5506 err = got_object_id_by_path(&id, repo, head_commit_id,
5508 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5510 err = id ? got_error(ood_errcode) : NULL;
5517 const struct got_error *
5518 commit_worktree(struct got_object_id **new_commit_id,
5519 struct got_pathlist_head *commitable_paths,
5520 struct got_object_id *head_commit_id,
5521 struct got_object_id *parent_id2,
5522 struct got_worktree *worktree,
5523 const char *author, const char *committer,
5524 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5525 got_worktree_status_cb status_cb, void *status_arg,
5526 struct got_repository *repo)
5528 const struct got_error *err = NULL, *unlockerr = NULL;
5529 struct got_pathlist_entry *pe;
5530 const char *head_ref_name = NULL;
5531 struct got_commit_object *head_commit = NULL;
5532 struct got_reference *head_ref2 = NULL;
5533 struct got_object_id *head_commit_id2 = NULL;
5534 struct got_tree_object *head_tree = NULL;
5535 struct got_object_id *new_tree_id = NULL;
5536 int nentries, nparents = 0;
5537 struct got_object_id_queue parent_ids;
5538 struct got_object_qid *pid = NULL;
5539 char *logmsg = NULL;
5541 *new_commit_id = NULL;
5543 STAILQ_INIT(&parent_ids);
5545 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5549 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5553 if (commit_msg_cb != NULL) {
5554 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5559 if (logmsg == NULL || strlen(logmsg) == 0) {
5560 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5564 /* Create blobs from added and modified files and record their IDs. */
5565 TAILQ_FOREACH(pe, commitable_paths, entry) {
5566 struct got_commitable *ct = pe->data;
5569 /* Blobs for staged files already exist. */
5570 if (ct->staged_status == GOT_STATUS_ADD ||
5571 ct->staged_status == GOT_STATUS_MODIFY)
5574 if (ct->status != GOT_STATUS_ADD &&
5575 ct->status != GOT_STATUS_MODIFY &&
5576 ct->status != GOT_STATUS_MODE_CHANGE)
5579 if (asprintf(&ondisk_path, "%s/%s",
5580 worktree->root_path, pe->path) == -1) {
5581 err = got_error_from_errno("asprintf");
5584 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5590 /* Recursively write new tree objects. */
5591 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5592 commitable_paths, status_cb, status_arg, repo);
5596 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5599 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5602 err = got_object_qid_alloc(&pid, parent_id2);
5605 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5608 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5609 nparents, author, time(NULL), committer, time(NULL), logmsg, repo);
5615 /* Check if a concurrent commit to our branch has occurred. */
5616 head_ref_name = got_worktree_get_head_ref_name(worktree);
5617 if (head_ref_name == NULL) {
5618 err = got_error_from_errno("got_worktree_get_head_ref_name");
5621 /* Lock the reference here to prevent concurrent modification. */
5622 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5625 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5628 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5629 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5632 /* Update branch head in repository. */
5633 err = got_ref_change_ref(head_ref2, *new_commit_id);
5636 err = got_ref_write(head_ref2, repo);
5640 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5644 err = ref_base_commit(worktree, repo);
5648 got_object_id_queue_free(&parent_ids);
5650 got_object_tree_close(head_tree);
5652 got_object_commit_close(head_commit);
5653 free(head_commit_id2);
5655 unlockerr = got_ref_unlock(head_ref2);
5656 if (unlockerr && err == NULL)
5658 got_ref_close(head_ref2);
5663 static const struct got_error *
5664 check_path_is_commitable(const char *path,
5665 struct got_pathlist_head *commitable_paths)
5667 struct got_pathlist_entry *cpe = NULL;
5668 size_t path_len = strlen(path);
5670 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5671 struct got_commitable *ct = cpe->data;
5672 const char *ct_path = ct->path;
5674 while (ct_path[0] == '/')
5677 if (strcmp(path, ct_path) == 0 ||
5678 got_path_is_child(ct_path, path, path_len))
5683 return got_error_path(path, GOT_ERR_BAD_PATH);
5688 static const struct got_error *
5689 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5691 int *have_staged_files = arg;
5693 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5694 *have_staged_files = 1;
5695 return got_error(GOT_ERR_CANCELLED);
5701 static const struct got_error *
5702 check_non_staged_files(struct got_fileindex *fileindex,
5703 struct got_pathlist_head *paths)
5705 struct got_pathlist_entry *pe;
5706 struct got_fileindex_entry *ie;
5708 TAILQ_FOREACH(pe, paths, entry) {
5709 if (pe->path[0] == '\0')
5711 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5713 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5714 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5715 return got_error_path(pe->path,
5716 GOT_ERR_FILE_NOT_STAGED);
5722 const struct got_error *
5723 got_worktree_commit(struct got_object_id **new_commit_id,
5724 struct got_worktree *worktree, struct got_pathlist_head *paths,
5725 const char *author, const char *committer, int allow_bad_symlinks,
5726 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5727 got_worktree_status_cb status_cb, void *status_arg,
5728 struct got_repository *repo)
5730 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5731 struct got_fileindex *fileindex = NULL;
5732 char *fileindex_path = NULL;
5733 struct got_pathlist_head commitable_paths;
5734 struct collect_commitables_arg cc_arg;
5735 struct got_pathlist_entry *pe;
5736 struct got_reference *head_ref = NULL;
5737 struct got_object_id *head_commit_id = NULL;
5738 int have_staged_files = 0;
5740 *new_commit_id = NULL;
5742 TAILQ_INIT(&commitable_paths);
5744 err = lock_worktree(worktree, LOCK_EX);
5748 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5752 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5756 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5760 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5761 &have_staged_files);
5762 if (err && err->code != GOT_ERR_CANCELLED)
5764 if (have_staged_files) {
5765 err = check_non_staged_files(fileindex, paths);
5770 cc_arg.commitable_paths = &commitable_paths;
5771 cc_arg.worktree = worktree;
5772 cc_arg.fileindex = fileindex;
5774 cc_arg.have_staged_files = have_staged_files;
5775 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5776 TAILQ_FOREACH(pe, paths, entry) {
5777 err = worktree_status(worktree, pe->path, fileindex, repo,
5778 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
5783 if (TAILQ_EMPTY(&commitable_paths)) {
5784 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5788 TAILQ_FOREACH(pe, paths, entry) {
5789 err = check_path_is_commitable(pe->path, &commitable_paths);
5794 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5795 struct got_commitable *ct = pe->data;
5796 const char *ct_path = ct->in_repo_path;
5798 while (ct_path[0] == '/')
5800 err = check_out_of_date(ct_path, ct->status,
5801 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5802 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5808 err = commit_worktree(new_commit_id, &commitable_paths,
5809 head_commit_id, NULL, worktree, author, committer,
5810 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5814 err = update_fileindex_after_commit(worktree, &commitable_paths,
5815 *new_commit_id, fileindex, have_staged_files);
5816 sync_err = sync_fileindex(fileindex, fileindex_path);
5817 if (sync_err && err == NULL)
5821 got_fileindex_free(fileindex);
5822 free(fileindex_path);
5823 unlockerr = lock_worktree(worktree, LOCK_SH);
5824 if (unlockerr && err == NULL)
5826 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5827 struct got_commitable *ct = pe->data;
5828 free_commitable(ct);
5830 got_pathlist_free(&commitable_paths);
5835 got_commitable_get_path(struct got_commitable *ct)
5841 got_commitable_get_status(struct got_commitable *ct)
5846 struct check_rebase_ok_arg {
5847 struct got_worktree *worktree;
5848 struct got_repository *repo;
5851 static const struct got_error *
5852 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5854 const struct got_error *err = NULL;
5855 struct check_rebase_ok_arg *a = arg;
5856 unsigned char status;
5860 /* Reject rebase of a work tree with mixed base commits. */
5861 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5862 SHA1_DIGEST_LENGTH))
5863 return got_error(GOT_ERR_MIXED_COMMITS);
5865 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5867 return got_error_from_errno("asprintf");
5869 /* Reject rebase of a work tree with modified or staged files. */
5870 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5875 if (status != GOT_STATUS_NO_CHANGE)
5876 return got_error(GOT_ERR_MODIFIED);
5877 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5878 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5883 const struct got_error *
5884 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5885 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5886 struct got_worktree *worktree, struct got_reference *branch,
5887 struct got_repository *repo)
5889 const struct got_error *err = NULL;
5890 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5891 char *branch_ref_name = NULL;
5892 char *fileindex_path = NULL;
5893 struct check_rebase_ok_arg ok_arg;
5894 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5895 struct got_object_id *wt_branch_tip = NULL;
5897 *new_base_branch_ref = NULL;
5901 err = lock_worktree(worktree, LOCK_EX);
5905 err = open_fileindex(fileindex, &fileindex_path, worktree);
5909 ok_arg.worktree = worktree;
5911 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5916 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5920 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5924 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5928 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5933 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5936 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5937 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5941 err = got_ref_alloc_symref(new_base_branch_ref,
5942 new_base_branch_ref_name, wt_branch);
5945 err = got_ref_write(*new_base_branch_ref, repo);
5949 /* TODO Lock original branch's ref while rebasing? */
5951 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5955 err = got_ref_write(branch_ref, repo);
5959 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5960 worktree->base_commit_id);
5963 err = got_ref_write(*tmp_branch, repo);
5967 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5971 free(fileindex_path);
5972 free(tmp_branch_name);
5973 free(new_base_branch_ref_name);
5974 free(branch_ref_name);
5976 got_ref_close(branch_ref);
5978 got_ref_close(wt_branch);
5979 free(wt_branch_tip);
5981 if (*new_base_branch_ref) {
5982 got_ref_close(*new_base_branch_ref);
5983 *new_base_branch_ref = NULL;
5986 got_ref_close(*tmp_branch);
5990 got_fileindex_free(*fileindex);
5993 lock_worktree(worktree, LOCK_SH);
5998 const struct got_error *
5999 got_worktree_rebase_continue(struct got_object_id **commit_id,
6000 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6001 struct got_reference **branch, struct got_fileindex **fileindex,
6002 struct got_worktree *worktree, struct got_repository *repo)
6004 const struct got_error *err;
6005 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6006 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6007 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6008 char *fileindex_path = NULL;
6009 int have_staged_files = 0;
6012 *new_base_branch = NULL;
6017 err = lock_worktree(worktree, LOCK_EX);
6021 err = open_fileindex(fileindex, &fileindex_path, worktree);
6025 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6026 &have_staged_files);
6027 if (err && err->code != GOT_ERR_CANCELLED)
6029 if (have_staged_files) {
6030 err = got_error(GOT_ERR_STAGED_PATHS);
6034 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6038 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6042 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6046 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6050 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6054 err = got_ref_open(branch, repo,
6055 got_ref_get_symref_target(branch_ref), 0);
6059 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6063 err = got_ref_resolve(commit_id, repo, commit_ref);
6067 err = got_ref_open(new_base_branch, repo,
6068 new_base_branch_ref_name, 0);
6072 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6076 free(commit_ref_name);
6077 free(branch_ref_name);
6078 free(fileindex_path);
6080 got_ref_close(commit_ref);
6082 got_ref_close(branch_ref);
6087 got_ref_close(*tmp_branch);
6090 if (*new_base_branch) {
6091 got_ref_close(*new_base_branch);
6092 *new_base_branch = NULL;
6095 got_ref_close(*branch);
6099 got_fileindex_free(*fileindex);
6102 lock_worktree(worktree, LOCK_SH);
6107 const struct got_error *
6108 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6110 const struct got_error *err;
6111 char *tmp_branch_name = NULL;
6113 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6117 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6118 free(tmp_branch_name);
6122 static const struct got_error *
6123 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6124 char **logmsg, void *arg)
6130 static const struct got_error *
6131 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6132 const char *path, struct got_object_id *blob_id,
6133 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6134 int dirfd, const char *de_name)
6139 struct collect_merged_paths_arg {
6140 got_worktree_checkout_cb progress_cb;
6142 struct got_pathlist_head *merged_paths;
6145 static const struct got_error *
6146 collect_merged_paths(void *arg, unsigned char status, const char *path)
6148 const struct got_error *err;
6149 struct collect_merged_paths_arg *a = arg;
6151 struct got_pathlist_entry *new;
6153 err = (*a->progress_cb)(a->progress_arg, status, path);
6157 if (status != GOT_STATUS_MERGE &&
6158 status != GOT_STATUS_ADD &&
6159 status != GOT_STATUS_DELETE &&
6160 status != GOT_STATUS_CONFLICT)
6165 return got_error_from_errno("strdup");
6167 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6168 if (err || new == NULL)
6174 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6176 struct got_pathlist_entry *pe;
6178 TAILQ_FOREACH(pe, merged_paths, entry)
6179 free((char *)pe->path);
6181 got_pathlist_free(merged_paths);
6184 static const struct got_error *
6185 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6186 int is_rebase, struct got_repository *repo)
6188 const struct got_error *err;
6189 struct got_reference *commit_ref = NULL;
6191 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6193 if (err->code != GOT_ERR_NOT_REF)
6195 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6198 err = got_ref_write(commit_ref, repo);
6201 } else if (is_rebase) {
6202 struct got_object_id *stored_id;
6205 err = got_ref_resolve(&stored_id, repo, commit_ref);
6208 cmp = got_object_id_cmp(commit_id, stored_id);
6211 err = got_error(GOT_ERR_REBASE_COMMITID);
6217 got_ref_close(commit_ref);
6221 static const struct got_error *
6222 rebase_merge_files(struct got_pathlist_head *merged_paths,
6223 const char *commit_ref_name, struct got_worktree *worktree,
6224 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6225 struct got_object_id *commit_id, struct got_repository *repo,
6226 got_worktree_checkout_cb progress_cb, void *progress_arg,
6227 got_cancel_cb cancel_cb, void *cancel_arg)
6229 const struct got_error *err;
6230 struct got_reference *commit_ref = NULL;
6231 struct collect_merged_paths_arg cmp_arg;
6232 char *fileindex_path;
6234 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6236 err = get_fileindex_path(&fileindex_path, worktree);
6240 cmp_arg.progress_cb = progress_cb;
6241 cmp_arg.progress_arg = progress_arg;
6242 cmp_arg.merged_paths = merged_paths;
6243 err = merge_files(worktree, fileindex, fileindex_path,
6244 parent_commit_id, commit_id, repo, collect_merged_paths,
6245 &cmp_arg, cancel_cb, cancel_arg);
6247 got_ref_close(commit_ref);
6251 const struct got_error *
6252 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6253 struct got_worktree *worktree, struct got_fileindex *fileindex,
6254 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6255 struct got_repository *repo,
6256 got_worktree_checkout_cb progress_cb, void *progress_arg,
6257 got_cancel_cb cancel_cb, void *cancel_arg)
6259 const struct got_error *err;
6260 char *commit_ref_name;
6262 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6266 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6270 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6271 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6272 progress_arg, cancel_cb, cancel_arg);
6274 free(commit_ref_name);
6278 const struct got_error *
6279 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6280 struct got_worktree *worktree, struct got_fileindex *fileindex,
6281 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6282 struct got_repository *repo,
6283 got_worktree_checkout_cb progress_cb, void *progress_arg,
6284 got_cancel_cb cancel_cb, void *cancel_arg)
6286 const struct got_error *err;
6287 char *commit_ref_name;
6289 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6293 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6297 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6298 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6299 progress_arg, cancel_cb, cancel_arg);
6301 free(commit_ref_name);
6305 static const struct got_error *
6306 rebase_commit(struct got_object_id **new_commit_id,
6307 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6308 struct got_worktree *worktree, struct got_fileindex *fileindex,
6309 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6310 const char *new_logmsg, struct got_repository *repo)
6312 const struct got_error *err, *sync_err;
6313 struct got_pathlist_head commitable_paths;
6314 struct collect_commitables_arg cc_arg;
6315 char *fileindex_path = NULL;
6316 struct got_reference *head_ref = NULL;
6317 struct got_object_id *head_commit_id = NULL;
6318 char *logmsg = NULL;
6320 TAILQ_INIT(&commitable_paths);
6321 *new_commit_id = NULL;
6323 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6325 err = get_fileindex_path(&fileindex_path, worktree);
6329 cc_arg.commitable_paths = &commitable_paths;
6330 cc_arg.worktree = worktree;
6332 cc_arg.have_staged_files = 0;
6334 * If possible get the status of individual files directly to
6335 * avoid crawling the entire work tree once per rebased commit.
6337 * Ideally, merged_paths would contain a list of commitables
6338 * we could use so we could skip worktree_status() entirely.
6339 * However, we would then need carefully keep track of cumulative
6340 * effects of operations such as file additions and deletions
6341 * in 'got histedit -f' (folding multiple commits into one),
6342 * and this extra complexity is not really worth it.
6345 struct got_pathlist_entry *pe;
6346 TAILQ_FOREACH(pe, merged_paths, entry) {
6347 err = worktree_status(worktree, pe->path, fileindex,
6348 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6354 err = worktree_status(worktree, "", fileindex, repo,
6355 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6360 if (TAILQ_EMPTY(&commitable_paths)) {
6361 /* No-op change; commit will be elided. */
6362 err = got_ref_delete(commit_ref, repo);
6365 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6369 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6373 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6378 logmsg = strdup(new_logmsg);
6379 if (logmsg == NULL) {
6380 err = got_error_from_errno("strdup");
6384 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6389 /* NB: commit_worktree will call free(logmsg) */
6390 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6391 NULL, worktree, got_object_commit_get_author(orig_commit),
6392 got_object_commit_get_committer(orig_commit),
6393 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6397 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6401 err = got_ref_delete(commit_ref, repo);
6405 err = update_fileindex_after_commit(worktree, &commitable_paths,
6406 *new_commit_id, fileindex, 0);
6407 sync_err = sync_fileindex(fileindex, fileindex_path);
6408 if (sync_err && err == NULL)
6411 free(fileindex_path);
6412 free(head_commit_id);
6414 got_ref_close(head_ref);
6416 free(*new_commit_id);
6417 *new_commit_id = NULL;
6422 const struct got_error *
6423 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6424 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6425 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6426 struct got_commit_object *orig_commit,
6427 struct got_object_id *orig_commit_id, struct got_repository *repo)
6429 const struct got_error *err;
6430 char *commit_ref_name;
6431 struct got_reference *commit_ref = NULL;
6432 struct got_object_id *commit_id = NULL;
6434 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6438 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6441 err = got_ref_resolve(&commit_id, repo, commit_ref);
6444 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6445 err = got_error(GOT_ERR_REBASE_COMMITID);
6449 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6450 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6453 got_ref_close(commit_ref);
6454 free(commit_ref_name);
6459 const struct got_error *
6460 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6461 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6462 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6463 struct got_commit_object *orig_commit,
6464 struct got_object_id *orig_commit_id, const char *new_logmsg,
6465 struct got_repository *repo)
6467 const struct got_error *err;
6468 char *commit_ref_name;
6469 struct got_reference *commit_ref = NULL;
6471 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6475 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6479 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6480 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6483 got_ref_close(commit_ref);
6484 free(commit_ref_name);
6488 const struct got_error *
6489 got_worktree_rebase_postpone(struct got_worktree *worktree,
6490 struct got_fileindex *fileindex)
6493 got_fileindex_free(fileindex);
6494 return lock_worktree(worktree, LOCK_SH);
6497 static const struct got_error *
6498 delete_ref(const char *name, struct got_repository *repo)
6500 const struct got_error *err;
6501 struct got_reference *ref;
6503 err = got_ref_open(&ref, repo, name, 0);
6505 if (err->code == GOT_ERR_NOT_REF)
6510 err = got_ref_delete(ref, repo);
6515 static const struct got_error *
6516 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6518 const struct got_error *err;
6519 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6520 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6522 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6525 err = delete_ref(tmp_branch_name, repo);
6529 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6532 err = delete_ref(new_base_branch_ref_name, repo);
6536 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6539 err = delete_ref(branch_ref_name, repo);
6543 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6546 err = delete_ref(commit_ref_name, repo);
6551 free(tmp_branch_name);
6552 free(new_base_branch_ref_name);
6553 free(branch_ref_name);
6554 free(commit_ref_name);
6558 const struct got_error *
6559 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6560 struct got_object_id *new_commit_id, struct got_repository *repo)
6562 const struct got_error *err;
6563 struct got_reference *ref = NULL;
6564 struct got_object_id *old_commit_id = NULL;
6565 const char *branch_name = NULL;
6566 char *new_id_str = NULL;
6567 char *refname = NULL;
6569 branch_name = got_ref_get_name(branch);
6570 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6571 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6574 err = got_object_id_str(&new_id_str, new_commit_id);
6578 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6579 new_id_str) == -1) {
6580 err = got_error_from_errno("asprintf");
6584 err = got_ref_resolve(&old_commit_id, repo, branch);
6588 err = got_ref_alloc(&ref, refname, old_commit_id);
6592 err = got_ref_write(ref, repo);
6596 free(old_commit_id);
6602 const struct got_error *
6603 got_worktree_rebase_complete(struct got_worktree *worktree,
6604 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6605 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6606 struct got_repository *repo, int create_backup)
6608 const struct got_error *err, *unlockerr, *sync_err;
6609 struct got_object_id *new_head_commit_id = NULL;
6610 char *fileindex_path = NULL;
6612 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6616 if (create_backup) {
6617 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6618 rebased_branch, new_head_commit_id, repo);
6623 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6627 err = got_ref_write(rebased_branch, repo);
6631 err = got_worktree_set_head_ref(worktree, rebased_branch);
6635 err = delete_rebase_refs(worktree, repo);
6639 err = get_fileindex_path(&fileindex_path, worktree);
6642 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6643 sync_err = sync_fileindex(fileindex, fileindex_path);
6644 if (sync_err && err == NULL)
6647 got_fileindex_free(fileindex);
6648 free(fileindex_path);
6649 free(new_head_commit_id);
6650 unlockerr = lock_worktree(worktree, LOCK_SH);
6651 if (unlockerr && err == NULL)
6656 const struct got_error *
6657 got_worktree_rebase_abort(struct got_worktree *worktree,
6658 struct got_fileindex *fileindex, struct got_repository *repo,
6659 struct got_reference *new_base_branch,
6660 got_worktree_checkout_cb progress_cb, void *progress_arg)
6662 const struct got_error *err, *unlockerr, *sync_err;
6663 struct got_reference *resolved = NULL;
6664 struct got_object_id *commit_id = NULL;
6665 char *fileindex_path = NULL;
6666 struct revert_file_args rfa;
6667 struct got_object_id *tree_id = NULL;
6669 err = lock_worktree(worktree, LOCK_EX);
6673 err = got_ref_open(&resolved, repo,
6674 got_ref_get_symref_target(new_base_branch), 0);
6678 err = got_worktree_set_head_ref(worktree, resolved);
6683 * XXX commits to the base branch could have happened while
6684 * we were busy rebasing; should we store the original commit ID
6685 * when rebase begins and read it back here?
6687 err = got_ref_resolve(&commit_id, repo, resolved);
6691 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6695 err = got_object_id_by_path(&tree_id, repo,
6696 worktree->base_commit_id, worktree->path_prefix);
6700 err = delete_rebase_refs(worktree, repo);
6704 err = get_fileindex_path(&fileindex_path, worktree);
6708 rfa.worktree = worktree;
6709 rfa.fileindex = fileindex;
6710 rfa.progress_cb = progress_cb;
6711 rfa.progress_arg = progress_arg;
6712 rfa.patch_cb = NULL;
6713 rfa.patch_arg = NULL;
6715 rfa.unlink_added_files = 0;
6716 err = worktree_status(worktree, "", fileindex, repo,
6717 revert_file, &rfa, NULL, NULL, 1, 0);
6721 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6722 repo, progress_cb, progress_arg, NULL, NULL);
6724 sync_err = sync_fileindex(fileindex, fileindex_path);
6725 if (sync_err && err == NULL)
6728 got_ref_close(resolved);
6732 got_fileindex_free(fileindex);
6733 free(fileindex_path);
6735 unlockerr = lock_worktree(worktree, LOCK_SH);
6736 if (unlockerr && err == NULL)
6741 const struct got_error *
6742 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6743 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6744 struct got_fileindex **fileindex, struct got_worktree *worktree,
6745 struct got_repository *repo)
6747 const struct got_error *err = NULL;
6748 char *tmp_branch_name = NULL;
6749 char *branch_ref_name = NULL;
6750 char *base_commit_ref_name = NULL;
6751 char *fileindex_path = NULL;
6752 struct check_rebase_ok_arg ok_arg;
6753 struct got_reference *wt_branch = NULL;
6754 struct got_reference *base_commit_ref = NULL;
6758 *base_commit_id = NULL;
6761 err = lock_worktree(worktree, LOCK_EX);
6765 err = open_fileindex(fileindex, &fileindex_path, worktree);
6769 ok_arg.worktree = worktree;
6771 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6776 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6780 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6784 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6789 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6794 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6798 err = got_ref_write(*branch_ref, repo);
6802 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6803 worktree->base_commit_id);
6806 err = got_ref_write(base_commit_ref, repo);
6809 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6810 if (*base_commit_id == NULL) {
6811 err = got_error_from_errno("got_object_id_dup");
6815 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6816 worktree->base_commit_id);
6819 err = got_ref_write(*tmp_branch, repo);
6823 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6827 free(fileindex_path);
6828 free(tmp_branch_name);
6829 free(branch_ref_name);
6830 free(base_commit_ref_name);
6832 got_ref_close(wt_branch);
6835 got_ref_close(*branch_ref);
6839 got_ref_close(*tmp_branch);
6842 free(*base_commit_id);
6844 got_fileindex_free(*fileindex);
6847 lock_worktree(worktree, LOCK_SH);
6852 const struct got_error *
6853 got_worktree_histedit_postpone(struct got_worktree *worktree,
6854 struct got_fileindex *fileindex)
6857 got_fileindex_free(fileindex);
6858 return lock_worktree(worktree, LOCK_SH);
6861 const struct got_error *
6862 got_worktree_histedit_in_progress(int *in_progress,
6863 struct got_worktree *worktree)
6865 const struct got_error *err;
6866 char *tmp_branch_name = NULL;
6868 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6872 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6873 free(tmp_branch_name);
6877 const struct got_error *
6878 got_worktree_histedit_continue(struct got_object_id **commit_id,
6879 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6880 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6881 struct got_worktree *worktree, struct got_repository *repo)
6883 const struct got_error *err;
6884 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6885 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6886 struct got_reference *commit_ref = NULL;
6887 struct got_reference *base_commit_ref = NULL;
6888 char *fileindex_path = NULL;
6889 int have_staged_files = 0;
6893 *base_commit_id = NULL;
6896 err = lock_worktree(worktree, LOCK_EX);
6900 err = open_fileindex(fileindex, &fileindex_path, worktree);
6904 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6905 &have_staged_files);
6906 if (err && err->code != GOT_ERR_CANCELLED)
6908 if (have_staged_files) {
6909 err = got_error(GOT_ERR_STAGED_PATHS);
6913 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6917 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6921 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6925 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6930 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6934 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6937 err = got_ref_resolve(commit_id, repo, commit_ref);
6941 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6944 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6948 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6952 free(commit_ref_name);
6953 free(branch_ref_name);
6954 free(fileindex_path);
6956 got_ref_close(commit_ref);
6957 if (base_commit_ref)
6958 got_ref_close(base_commit_ref);
6962 free(*base_commit_id);
6963 *base_commit_id = NULL;
6965 got_ref_close(*tmp_branch);
6969 got_fileindex_free(*fileindex);
6972 lock_worktree(worktree, LOCK_EX);
6977 static const struct got_error *
6978 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6980 const struct got_error *err;
6981 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6982 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6984 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6987 err = delete_ref(tmp_branch_name, repo);
6991 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6995 err = delete_ref(base_commit_ref_name, repo);
6999 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7002 err = delete_ref(branch_ref_name, repo);
7006 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7009 err = delete_ref(commit_ref_name, repo);
7013 free(tmp_branch_name);
7014 free(base_commit_ref_name);
7015 free(branch_ref_name);
7016 free(commit_ref_name);
7020 const struct got_error *
7021 got_worktree_histedit_abort(struct got_worktree *worktree,
7022 struct got_fileindex *fileindex, struct got_repository *repo,
7023 struct got_reference *branch, struct got_object_id *base_commit_id,
7024 got_worktree_checkout_cb progress_cb, void *progress_arg)
7026 const struct got_error *err, *unlockerr, *sync_err;
7027 struct got_reference *resolved = NULL;
7028 char *fileindex_path = NULL;
7029 struct got_object_id *tree_id = NULL;
7030 struct revert_file_args rfa;
7032 err = lock_worktree(worktree, LOCK_EX);
7036 err = got_ref_open(&resolved, repo,
7037 got_ref_get_symref_target(branch), 0);
7041 err = got_worktree_set_head_ref(worktree, resolved);
7045 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7049 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
7050 worktree->path_prefix);
7054 err = delete_histedit_refs(worktree, repo);
7058 err = get_fileindex_path(&fileindex_path, worktree);
7062 rfa.worktree = worktree;
7063 rfa.fileindex = fileindex;
7064 rfa.progress_cb = progress_cb;
7065 rfa.progress_arg = progress_arg;
7066 rfa.patch_cb = NULL;
7067 rfa.patch_arg = NULL;
7069 rfa.unlink_added_files = 0;
7070 err = worktree_status(worktree, "", fileindex, repo,
7071 revert_file, &rfa, NULL, NULL, 1, 0);
7075 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7076 repo, progress_cb, progress_arg, NULL, NULL);
7078 sync_err = sync_fileindex(fileindex, fileindex_path);
7079 if (sync_err && err == NULL)
7082 got_ref_close(resolved);
7084 free(fileindex_path);
7086 unlockerr = lock_worktree(worktree, LOCK_SH);
7087 if (unlockerr && err == NULL)
7092 const struct got_error *
7093 got_worktree_histedit_complete(struct got_worktree *worktree,
7094 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7095 struct got_reference *edited_branch, struct got_repository *repo)
7097 const struct got_error *err, *unlockerr, *sync_err;
7098 struct got_object_id *new_head_commit_id = NULL;
7099 struct got_reference *resolved = NULL;
7100 char *fileindex_path = NULL;
7102 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7106 err = got_ref_open(&resolved, repo,
7107 got_ref_get_symref_target(edited_branch), 0);
7111 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7112 resolved, new_head_commit_id, repo);
7116 err = got_ref_change_ref(resolved, new_head_commit_id);
7120 err = got_ref_write(resolved, repo);
7124 err = got_worktree_set_head_ref(worktree, resolved);
7128 err = delete_histedit_refs(worktree, repo);
7132 err = get_fileindex_path(&fileindex_path, worktree);
7135 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7136 sync_err = sync_fileindex(fileindex, fileindex_path);
7137 if (sync_err && err == NULL)
7140 got_fileindex_free(fileindex);
7141 free(fileindex_path);
7142 free(new_head_commit_id);
7143 unlockerr = lock_worktree(worktree, LOCK_SH);
7144 if (unlockerr && err == NULL)
7149 const struct got_error *
7150 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7151 struct got_object_id *commit_id, struct got_repository *repo)
7153 const struct got_error *err;
7154 char *commit_ref_name;
7156 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7160 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7164 err = delete_ref(commit_ref_name, repo);
7166 free(commit_ref_name);
7170 const struct got_error *
7171 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7172 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7173 struct got_worktree *worktree, const char *refname,
7174 struct got_repository *repo)
7176 const struct got_error *err = NULL;
7177 char *fileindex_path = NULL;
7178 struct check_rebase_ok_arg ok_arg;
7182 *base_branch_ref = NULL;
7184 err = lock_worktree(worktree, LOCK_EX);
7188 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7189 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7190 "cannot integrate a branch into itself; "
7191 "update -b or different branch name required");
7195 err = open_fileindex(fileindex, &fileindex_path, worktree);
7199 /* Preconditions are the same as for rebase. */
7200 ok_arg.worktree = worktree;
7202 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7207 err = got_ref_open(branch_ref, repo, refname, 1);
7211 err = got_ref_open(base_branch_ref, repo,
7212 got_worktree_get_head_ref_name(worktree), 1);
7216 got_ref_close(*branch_ref);
7219 if (*base_branch_ref) {
7220 got_ref_close(*base_branch_ref);
7221 *base_branch_ref = NULL;
7224 got_fileindex_free(*fileindex);
7227 lock_worktree(worktree, LOCK_SH);
7232 const struct got_error *
7233 got_worktree_integrate_continue(struct got_worktree *worktree,
7234 struct got_fileindex *fileindex, struct got_repository *repo,
7235 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7236 got_worktree_checkout_cb progress_cb, void *progress_arg,
7237 got_cancel_cb cancel_cb, void *cancel_arg)
7239 const struct got_error *err = NULL, *sync_err, *unlockerr;
7240 char *fileindex_path = NULL;
7241 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7243 err = get_fileindex_path(&fileindex_path, worktree);
7247 err = got_ref_resolve(&commit_id, repo, branch_ref);
7251 err = got_object_id_by_path(&tree_id, repo, commit_id,
7252 worktree->path_prefix);
7256 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7260 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7261 progress_cb, progress_arg, cancel_cb, cancel_arg);
7265 err = got_ref_change_ref(base_branch_ref, commit_id);
7269 err = got_ref_write(base_branch_ref, repo);
7273 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7275 sync_err = sync_fileindex(fileindex, fileindex_path);
7276 if (sync_err && err == NULL)
7280 unlockerr = got_ref_unlock(branch_ref);
7281 if (unlockerr && err == NULL)
7283 got_ref_close(branch_ref);
7285 unlockerr = got_ref_unlock(base_branch_ref);
7286 if (unlockerr && err == NULL)
7288 got_ref_close(base_branch_ref);
7290 got_fileindex_free(fileindex);
7291 free(fileindex_path);
7294 unlockerr = lock_worktree(worktree, LOCK_SH);
7295 if (unlockerr && err == NULL)
7300 const struct got_error *
7301 got_worktree_integrate_abort(struct got_worktree *worktree,
7302 struct got_fileindex *fileindex, struct got_repository *repo,
7303 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7305 const struct got_error *err = NULL, *unlockerr = NULL;
7307 got_fileindex_free(fileindex);
7309 err = lock_worktree(worktree, LOCK_SH);
7311 unlockerr = got_ref_unlock(branch_ref);
7312 if (unlockerr && err == NULL)
7314 got_ref_close(branch_ref);
7316 unlockerr = got_ref_unlock(base_branch_ref);
7317 if (unlockerr && err == NULL)
7319 got_ref_close(base_branch_ref);
7324 const struct got_error *
7325 got_worktree_merge_postpone(struct got_worktree *worktree,
7326 struct got_fileindex *fileindex)
7328 const struct got_error *err, *sync_err;
7329 char *fileindex_path = NULL;
7331 err = get_fileindex_path(&fileindex_path, worktree);
7335 sync_err = sync_fileindex(fileindex, fileindex_path);
7337 err = lock_worktree(worktree, LOCK_SH);
7338 if (sync_err && err == NULL)
7341 got_fileindex_free(fileindex);
7342 free(fileindex_path);
7346 static const struct got_error *
7347 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7349 const struct got_error *err;
7350 char *branch_refname = NULL, *commit_refname = NULL;
7352 err = get_merge_branch_ref_name(&branch_refname, worktree);
7355 err = delete_ref(branch_refname, repo);
7359 err = get_merge_commit_ref_name(&commit_refname, worktree);
7362 err = delete_ref(commit_refname, repo);
7367 free(branch_refname);
7368 free(commit_refname);
7372 struct merge_commit_msg_arg {
7373 struct got_worktree *worktree;
7374 const char *branch_name;
7377 static const struct got_error *
7378 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths, char **logmsg,
7381 struct merge_commit_msg_arg *a = arg;
7383 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7384 got_worktree_get_head_ref_name(a->worktree)) == -1)
7385 return got_error_from_errno("asprintf");
7391 const struct got_error *
7392 got_worktree_merge_branch(struct got_worktree *worktree,
7393 struct got_fileindex *fileindex,
7394 struct got_object_id *yca_commit_id,
7395 struct got_object_id *branch_tip,
7396 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7397 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7399 const struct got_error *err;
7400 char *fileindex_path = NULL;
7402 err = get_fileindex_path(&fileindex_path, worktree);
7406 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7411 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7412 branch_tip, repo, progress_cb, progress_arg,
7413 cancel_cb, cancel_arg);
7415 free(fileindex_path);
7419 const struct got_error *
7420 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7421 struct got_worktree *worktree, struct got_fileindex *fileindex,
7422 const char *author, const char *committer, int allow_bad_symlinks,
7423 struct got_object_id *branch_tip, const char *branch_name,
7424 struct got_repository *repo,
7425 got_worktree_status_cb status_cb, void *status_arg)
7428 const struct got_error *err = NULL, *sync_err;
7429 struct got_pathlist_head commitable_paths;
7430 struct collect_commitables_arg cc_arg;
7431 struct got_pathlist_entry *pe;
7432 struct got_reference *head_ref = NULL;
7433 struct got_object_id *head_commit_id = NULL;
7434 int have_staged_files = 0;
7435 struct merge_commit_msg_arg mcm_arg;
7436 char *fileindex_path = NULL;
7438 *new_commit_id = NULL;
7440 TAILQ_INIT(&commitable_paths);
7442 err = get_fileindex_path(&fileindex_path, worktree);
7446 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7450 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7454 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7455 &have_staged_files);
7456 if (err && err->code != GOT_ERR_CANCELLED)
7458 if (have_staged_files) {
7459 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7463 cc_arg.commitable_paths = &commitable_paths;
7464 cc_arg.worktree = worktree;
7465 cc_arg.fileindex = fileindex;
7467 cc_arg.have_staged_files = have_staged_files;
7468 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7469 err = worktree_status(worktree, "", fileindex, repo,
7470 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7474 if (TAILQ_EMPTY(&commitable_paths)) {
7475 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7476 "merge of %s cannot proceed", branch_name);
7480 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7481 struct got_commitable *ct = pe->data;
7482 const char *ct_path = ct->in_repo_path;
7484 while (ct_path[0] == '/')
7486 err = check_out_of_date(ct_path, ct->status,
7487 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7488 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7494 mcm_arg.worktree = worktree;
7495 mcm_arg.branch_name = branch_name;
7496 err = commit_worktree(new_commit_id, &commitable_paths,
7497 head_commit_id, branch_tip, worktree, author, committer,
7498 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7502 err = update_fileindex_after_commit(worktree, &commitable_paths,
7503 *new_commit_id, fileindex, have_staged_files);
7504 sync_err = sync_fileindex(fileindex, fileindex_path);
7505 if (sync_err && err == NULL)
7508 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7509 struct got_commitable *ct = pe->data;
7510 free_commitable(ct);
7512 got_pathlist_free(&commitable_paths);
7513 free(fileindex_path);
7517 const struct got_error *
7518 got_worktree_merge_complete(struct got_worktree *worktree,
7519 struct got_fileindex *fileindex, struct got_repository *repo)
7521 const struct got_error *err, *unlockerr, *sync_err;
7522 char *fileindex_path = NULL;
7524 err = delete_merge_refs(worktree, repo);
7528 err = get_fileindex_path(&fileindex_path, worktree);
7531 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7532 sync_err = sync_fileindex(fileindex, fileindex_path);
7533 if (sync_err && err == NULL)
7536 got_fileindex_free(fileindex);
7537 free(fileindex_path);
7538 unlockerr = lock_worktree(worktree, LOCK_SH);
7539 if (unlockerr && err == NULL)
7544 const struct got_error *
7545 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7546 struct got_repository *repo)
7548 const struct got_error *err;
7549 char *branch_refname = NULL;
7550 struct got_reference *branch_ref = NULL;
7554 err = get_merge_branch_ref_name(&branch_refname, worktree);
7557 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7558 free(branch_refname);
7560 if (err->code != GOT_ERR_NOT_REF)
7568 const struct got_error *got_worktree_merge_prepare(
7569 struct got_fileindex **fileindex, struct got_worktree *worktree,
7570 struct got_reference *branch, struct got_repository *repo)
7572 const struct got_error *err = NULL;
7573 char *fileindex_path = NULL;
7574 char *branch_refname = NULL, *commit_refname = NULL;
7575 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
7576 struct got_reference *commit_ref = NULL;
7577 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
7578 struct check_rebase_ok_arg ok_arg;
7582 err = lock_worktree(worktree, LOCK_EX);
7586 err = open_fileindex(fileindex, &fileindex_path, worktree);
7590 /* Preconditions are the same as for rebase. */
7591 ok_arg.worktree = worktree;
7593 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7598 err = get_merge_branch_ref_name(&branch_refname, worktree);
7602 err = get_merge_commit_ref_name(&commit_refname, worktree);
7606 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7611 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
7615 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
7616 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
7620 err = got_ref_resolve(&branch_tip, repo, branch);
7624 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
7627 err = got_ref_write(branch_ref, repo);
7631 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
7634 err = got_ref_write(commit_ref, repo);
7639 free(branch_refname);
7640 free(commit_refname);
7641 free(fileindex_path);
7643 got_ref_close(branch_ref);
7645 got_ref_close(commit_ref);
7647 got_ref_close(wt_branch);
7648 free(wt_branch_tip);
7651 got_fileindex_free(*fileindex);
7654 lock_worktree(worktree, LOCK_SH);
7659 const struct got_error *
7660 got_worktree_merge_continue(char **branch_name,
7661 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
7662 struct got_worktree *worktree, struct got_repository *repo)
7664 const struct got_error *err;
7665 char *commit_refname = NULL, *branch_refname = NULL;
7666 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
7667 char *fileindex_path = NULL;
7668 int have_staged_files = 0;
7670 *branch_name = NULL;
7674 err = lock_worktree(worktree, LOCK_EX);
7678 err = open_fileindex(fileindex, &fileindex_path, worktree);
7682 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7683 &have_staged_files);
7684 if (err && err->code != GOT_ERR_CANCELLED)
7686 if (have_staged_files) {
7687 err = got_error(GOT_ERR_STAGED_PATHS);
7691 err = get_merge_branch_ref_name(&branch_refname, worktree);
7695 err = get_merge_commit_ref_name(&commit_refname, worktree);
7699 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7703 if (!got_ref_is_symbolic(branch_ref)) {
7704 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7705 "%s is not a symbolic reference",
7706 got_ref_get_name(branch_ref));
7709 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
7710 if (*branch_name == NULL) {
7711 err = got_error_from_errno("strdup");
7715 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
7719 err = got_ref_resolve(branch_tip, repo, commit_ref);
7723 free(commit_refname);
7724 free(branch_refname);
7725 free(fileindex_path);
7727 got_ref_close(commit_ref);
7729 got_ref_close(branch_ref);
7733 *branch_name = NULL;
7738 got_fileindex_free(*fileindex);
7741 lock_worktree(worktree, LOCK_SH);
7746 const struct got_error *
7747 got_worktree_merge_abort(struct got_worktree *worktree,
7748 struct got_fileindex *fileindex, struct got_repository *repo,
7749 got_worktree_checkout_cb progress_cb, void *progress_arg)
7751 const struct got_error *err, *unlockerr, *sync_err;
7752 struct got_object_id *commit_id = NULL;
7753 char *fileindex_path = NULL;
7754 struct revert_file_args rfa;
7755 struct got_object_id *tree_id = NULL;
7757 err = got_object_id_by_path(&tree_id, repo,
7758 worktree->base_commit_id, worktree->path_prefix);
7762 err = delete_merge_refs(worktree, repo);
7766 err = get_fileindex_path(&fileindex_path, worktree);
7770 rfa.worktree = worktree;
7771 rfa.fileindex = fileindex;
7772 rfa.progress_cb = progress_cb;
7773 rfa.progress_arg = progress_arg;
7774 rfa.patch_cb = NULL;
7775 rfa.patch_arg = NULL;
7777 rfa.unlink_added_files = 1;
7778 err = worktree_status(worktree, "", fileindex, repo,
7779 revert_file, &rfa, NULL, NULL, 1, 0);
7783 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7784 repo, progress_cb, progress_arg, NULL, NULL);
7786 sync_err = sync_fileindex(fileindex, fileindex_path);
7787 if (sync_err && err == NULL)
7793 got_fileindex_free(fileindex);
7794 free(fileindex_path);
7796 unlockerr = lock_worktree(worktree, LOCK_SH);
7797 if (unlockerr && err == NULL)
7802 struct check_stage_ok_arg {
7803 struct got_object_id *head_commit_id;
7804 struct got_worktree *worktree;
7805 struct got_fileindex *fileindex;
7806 struct got_repository *repo;
7810 const struct got_error *
7811 check_stage_ok(void *arg, unsigned char status,
7812 unsigned char staged_status, const char *relpath,
7813 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7814 struct got_object_id *commit_id, int dirfd, const char *de_name)
7816 struct check_stage_ok_arg *a = arg;
7817 const struct got_error *err = NULL;
7818 struct got_fileindex_entry *ie;
7819 struct got_object_id base_commit_id;
7820 struct got_object_id *base_commit_idp = NULL;
7821 char *in_repo_path = NULL, *p;
7823 if (status == GOT_STATUS_UNVERSIONED ||
7824 status == GOT_STATUS_NO_CHANGE)
7826 if (status == GOT_STATUS_NONEXISTENT)
7827 return got_error_set_errno(ENOENT, relpath);
7829 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7831 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7833 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7834 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7836 return got_error_from_errno("asprintf");
7838 if (got_fileindex_entry_has_commit(ie)) {
7839 memcpy(base_commit_id.sha1, ie->commit_sha1,
7840 SHA1_DIGEST_LENGTH);
7841 base_commit_idp = &base_commit_id;
7844 if (status == GOT_STATUS_CONFLICT) {
7845 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7847 } else if (status != GOT_STATUS_ADD &&
7848 status != GOT_STATUS_MODIFY &&
7849 status != GOT_STATUS_DELETE) {
7850 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7854 a->have_changes = 1;
7859 err = check_out_of_date(p, status, staged_status,
7860 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7861 GOT_ERR_STAGE_OUT_OF_DATE);
7867 struct stage_path_arg {
7868 struct got_worktree *worktree;
7869 struct got_fileindex *fileindex;
7870 struct got_repository *repo;
7871 got_worktree_status_cb status_cb;
7873 got_worktree_patch_cb patch_cb;
7875 int staged_something;
7876 int allow_bad_symlinks;
7879 static const struct got_error *
7880 stage_path(void *arg, unsigned char status,
7881 unsigned char staged_status, const char *relpath,
7882 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7883 struct got_object_id *commit_id, int dirfd, const char *de_name)
7885 struct stage_path_arg *a = arg;
7886 const struct got_error *err = NULL;
7887 struct got_fileindex_entry *ie;
7888 char *ondisk_path = NULL, *path_content = NULL;
7890 struct got_object_id *new_staged_blob_id = NULL;
7893 if (status == GOT_STATUS_UNVERSIONED)
7896 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7898 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7900 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7902 return got_error_from_errno("asprintf");
7905 case GOT_STATUS_ADD:
7906 case GOT_STATUS_MODIFY:
7907 /* XXX could sb.st_mode be passed in by our caller? */
7908 if (lstat(ondisk_path, &sb) == -1) {
7909 err = got_error_from_errno2("lstat", ondisk_path);
7913 if (status == GOT_STATUS_ADD) {
7914 int choice = GOT_PATCH_CHOICE_NONE;
7915 err = (*a->patch_cb)(&choice, a->patch_arg,
7916 status, ie->path, NULL, 1, 1);
7919 if (choice != GOT_PATCH_CHOICE_YES)
7922 err = create_patched_content(&path_content, 0,
7923 staged_blob_id ? staged_blob_id : blob_id,
7924 ondisk_path, dirfd, de_name, ie->path,
7925 a->repo, a->patch_cb, a->patch_arg);
7926 if (err || path_content == NULL)
7930 err = got_object_blob_create(&new_staged_blob_id,
7931 path_content ? path_content : ondisk_path, a->repo);
7934 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7935 SHA1_DIGEST_LENGTH);
7936 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7937 stage = GOT_FILEIDX_STAGE_ADD;
7939 stage = GOT_FILEIDX_STAGE_MODIFY;
7940 got_fileindex_entry_stage_set(ie, stage);
7941 if (S_ISLNK(sb.st_mode)) {
7942 int is_bad_symlink = 0;
7943 if (!a->allow_bad_symlinks) {
7944 char target_path[PATH_MAX];
7946 target_len = readlink(ondisk_path, target_path,
7947 sizeof(target_path));
7948 if (target_len == -1) {
7949 err = got_error_from_errno2("readlink",
7953 err = is_bad_symlink_target(&is_bad_symlink,
7954 target_path, target_len, ondisk_path,
7955 a->worktree->root_path);
7958 if (is_bad_symlink) {
7959 err = got_error_path(ondisk_path,
7960 GOT_ERR_BAD_SYMLINK);
7965 got_fileindex_entry_staged_filetype_set(ie,
7966 GOT_FILEIDX_MODE_BAD_SYMLINK);
7968 got_fileindex_entry_staged_filetype_set(ie,
7969 GOT_FILEIDX_MODE_SYMLINK);
7971 got_fileindex_entry_staged_filetype_set(ie,
7972 GOT_FILEIDX_MODE_REGULAR_FILE);
7974 a->staged_something = 1;
7975 if (a->status_cb == NULL)
7977 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7978 get_staged_status(ie), relpath, blob_id,
7979 new_staged_blob_id, NULL, dirfd, de_name);
7981 case GOT_STATUS_DELETE:
7982 if (staged_status == GOT_STATUS_DELETE)
7985 int choice = GOT_PATCH_CHOICE_NONE;
7986 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7987 ie->path, NULL, 1, 1);
7990 if (choice == GOT_PATCH_CHOICE_NO)
7992 if (choice != GOT_PATCH_CHOICE_YES) {
7993 err = got_error(GOT_ERR_PATCH_CHOICE);
7997 stage = GOT_FILEIDX_STAGE_DELETE;
7998 got_fileindex_entry_stage_set(ie, stage);
7999 a->staged_something = 1;
8000 if (a->status_cb == NULL)
8002 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8003 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8006 case GOT_STATUS_NO_CHANGE:
8008 case GOT_STATUS_CONFLICT:
8009 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8011 case GOT_STATUS_NONEXISTENT:
8012 err = got_error_set_errno(ENOENT, relpath);
8015 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8019 if (path_content && unlink(path_content) == -1 && err == NULL)
8020 err = got_error_from_errno2("unlink", path_content);
8023 free(new_staged_blob_id);
8027 const struct got_error *
8028 got_worktree_stage(struct got_worktree *worktree,
8029 struct got_pathlist_head *paths,
8030 got_worktree_status_cb status_cb, void *status_arg,
8031 got_worktree_patch_cb patch_cb, void *patch_arg,
8032 int allow_bad_symlinks, struct got_repository *repo)
8034 const struct got_error *err = NULL, *sync_err, *unlockerr;
8035 struct got_pathlist_entry *pe;
8036 struct got_fileindex *fileindex = NULL;
8037 char *fileindex_path = NULL;
8038 struct got_reference *head_ref = NULL;
8039 struct got_object_id *head_commit_id = NULL;
8040 struct check_stage_ok_arg oka;
8041 struct stage_path_arg spa;
8043 err = lock_worktree(worktree, LOCK_EX);
8047 err = got_ref_open(&head_ref, repo,
8048 got_worktree_get_head_ref_name(worktree), 0);
8051 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8054 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8058 /* Check pre-conditions before staging anything. */
8059 oka.head_commit_id = head_commit_id;
8060 oka.worktree = worktree;
8061 oka.fileindex = fileindex;
8063 oka.have_changes = 0;
8064 TAILQ_FOREACH(pe, paths, entry) {
8065 err = worktree_status(worktree, pe->path, fileindex, repo,
8066 check_stage_ok, &oka, NULL, NULL, 1, 0);
8070 if (!oka.have_changes) {
8071 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8075 spa.worktree = worktree;
8076 spa.fileindex = fileindex;
8078 spa.patch_cb = patch_cb;
8079 spa.patch_arg = patch_arg;
8080 spa.status_cb = status_cb;
8081 spa.status_arg = status_arg;
8082 spa.staged_something = 0;
8083 spa.allow_bad_symlinks = allow_bad_symlinks;
8084 TAILQ_FOREACH(pe, paths, entry) {
8085 err = worktree_status(worktree, pe->path, fileindex, repo,
8086 stage_path, &spa, NULL, NULL, 1, 0);
8090 if (!spa.staged_something) {
8091 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8095 sync_err = sync_fileindex(fileindex, fileindex_path);
8096 if (sync_err && err == NULL)
8100 got_ref_close(head_ref);
8101 free(head_commit_id);
8102 free(fileindex_path);
8104 got_fileindex_free(fileindex);
8105 unlockerr = lock_worktree(worktree, LOCK_SH);
8106 if (unlockerr && err == NULL)
8111 struct unstage_path_arg {
8112 struct got_worktree *worktree;
8113 struct got_fileindex *fileindex;
8114 struct got_repository *repo;
8115 got_worktree_checkout_cb progress_cb;
8117 got_worktree_patch_cb patch_cb;
8121 static const struct got_error *
8122 create_unstaged_content(char **path_unstaged_content,
8123 char **path_new_staged_content, struct got_object_id *blob_id,
8124 struct got_object_id *staged_blob_id, const char *relpath,
8125 struct got_repository *repo,
8126 got_worktree_patch_cb patch_cb, void *patch_arg)
8128 const struct got_error *err, *free_err;
8129 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8130 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8131 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8132 struct got_diffreg_result *diffreg_result = NULL;
8133 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8134 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8136 *path_unstaged_content = NULL;
8137 *path_new_staged_content = NULL;
8139 err = got_object_id_str(&label1, blob_id);
8142 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
8146 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
8150 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8154 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
8158 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
8162 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8166 err = got_diff_files(&diffreg_result, f1, label1, f2,
8167 path2, 3, 0, 1, NULL);
8171 err = got_opentemp_named(path_unstaged_content, &outfile,
8172 "got-unstaged-content");
8175 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8176 "got-new-staged-content");
8180 if (fseek(f1, 0L, SEEK_SET) == -1) {
8181 err = got_ferror(f1, GOT_ERR_IO);
8184 if (fseek(f2, 0L, SEEK_SET) == -1) {
8185 err = got_ferror(f2, GOT_ERR_IO);
8188 /* Count the number of actual changes in the diff result. */
8189 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8190 struct diff_chunk_context cc = {};
8191 diff_chunk_context_load_change(&cc, &nchunks_used,
8192 diffreg_result->result, n, 0);
8195 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8197 err = apply_or_reject_change(&choice, &nchunks_used,
8198 diffreg_result->result, n, relpath, f1, f2,
8199 &line_cur1, &line_cur2,
8200 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8203 if (choice == GOT_PATCH_CHOICE_YES)
8206 have_rejected_content = 1;
8207 if (choice == GOT_PATCH_CHOICE_QUIT)
8210 if (have_content || have_rejected_content)
8211 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8212 outfile, rejectfile);
8216 got_object_blob_close(blob);
8218 got_object_blob_close(staged_blob);
8219 free_err = got_diffreg_result_free(diffreg_result);
8220 if (free_err && err == NULL)
8222 if (f1 && fclose(f1) == EOF && err == NULL)
8223 err = got_error_from_errno2("fclose", path1);
8224 if (f2 && fclose(f2) == EOF && err == NULL)
8225 err = got_error_from_errno2("fclose", path2);
8226 if (outfile && fclose(outfile) == EOF && err == NULL)
8227 err = got_error_from_errno2("fclose", *path_unstaged_content);
8228 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8229 err = got_error_from_errno2("fclose", *path_new_staged_content);
8230 if (path1 && unlink(path1) == -1 && err == NULL)
8231 err = got_error_from_errno2("unlink", path1);
8232 if (path2 && unlink(path2) == -1 && err == NULL)
8233 err = got_error_from_errno2("unlink", path2);
8234 if (err || !have_content) {
8235 if (*path_unstaged_content &&
8236 unlink(*path_unstaged_content) == -1 && err == NULL)
8237 err = got_error_from_errno2("unlink",
8238 *path_unstaged_content);
8239 free(*path_unstaged_content);
8240 *path_unstaged_content = NULL;
8242 if (err || !have_content || !have_rejected_content) {
8243 if (*path_new_staged_content &&
8244 unlink(*path_new_staged_content) == -1 && err == NULL)
8245 err = got_error_from_errno2("unlink",
8246 *path_new_staged_content);
8247 free(*path_new_staged_content);
8248 *path_new_staged_content = NULL;
8255 static const struct got_error *
8256 unstage_hunks(struct got_object_id *staged_blob_id,
8257 struct got_blob_object *blob_base,
8258 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8259 const char *ondisk_path, const char *label_orig,
8260 struct got_worktree *worktree, struct got_repository *repo,
8261 got_worktree_patch_cb patch_cb, void *patch_arg,
8262 got_worktree_checkout_cb progress_cb, void *progress_arg)
8264 const struct got_error *err = NULL;
8265 char *path_unstaged_content = NULL;
8266 char *path_new_staged_content = NULL;
8267 char *parent = NULL, *base_path = NULL;
8268 char *blob_base_path = NULL;
8269 struct got_object_id *new_staged_blob_id = NULL;
8270 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8273 err = create_unstaged_content(&path_unstaged_content,
8274 &path_new_staged_content, blob_id, staged_blob_id,
8275 ie->path, repo, patch_cb, patch_arg);
8279 if (path_unstaged_content == NULL)
8282 if (path_new_staged_content) {
8283 err = got_object_blob_create(&new_staged_blob_id,
8284 path_new_staged_content, repo);
8289 f = fopen(path_unstaged_content, "re");
8291 err = got_error_from_errno2("fopen",
8292 path_unstaged_content);
8295 if (fstat(fileno(f), &sb) == -1) {
8296 err = got_error_from_errno2("fstat", path_unstaged_content);
8299 if (got_fileindex_entry_staged_filetype_get(ie) ==
8300 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8301 char link_target[PATH_MAX];
8303 r = fread(link_target, 1, sizeof(link_target), f);
8304 if (r == 0 && ferror(f)) {
8305 err = got_error_from_errno("fread");
8308 if (r >= sizeof(link_target)) { /* should not happen */
8309 err = got_error(GOT_ERR_NO_SPACE);
8312 link_target[r] = '\0';
8313 err = merge_symlink(worktree, blob_base,
8314 ondisk_path, ie->path, label_orig, link_target,
8315 worktree->base_commit_id, repo, progress_cb,
8318 int local_changes_subsumed;
8320 err = got_path_dirname(&parent, ondisk_path);
8324 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8326 err = got_error_from_errno("asprintf");
8331 err = got_opentemp_named(&blob_base_path, &f_base, base_path);
8334 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8340 * In order the run a 3-way merge with a symlink we copy the symlink's
8341 * target path into a temporary file and use that file with diff3.
8343 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8344 err = dump_symlink_target_path_to_file(&f_deriv2,
8350 fd = open(ondisk_path,
8351 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8353 err = got_error_from_errno2("open", ondisk_path);
8356 f_deriv2 = fdopen(fd, "r");
8357 if (f_deriv2 == NULL) {
8358 err = got_error_from_errno2("fdopen", ondisk_path);
8364 err = merge_file(&local_changes_subsumed, worktree,
8365 f_base, f, f_deriv2, ondisk_path, ie->path,
8366 got_fileindex_perms_to_st(ie),
8367 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8368 repo, progress_cb, progress_arg);
8373 if (new_staged_blob_id) {
8374 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8375 SHA1_DIGEST_LENGTH);
8377 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8378 got_fileindex_entry_staged_filetype_set(ie, 0);
8381 free(new_staged_blob_id);
8382 if (path_unstaged_content &&
8383 unlink(path_unstaged_content) == -1 && err == NULL)
8384 err = got_error_from_errno2("unlink", path_unstaged_content);
8385 if (path_new_staged_content &&
8386 unlink(path_new_staged_content) == -1 && err == NULL)
8387 err = got_error_from_errno2("unlink", path_new_staged_content);
8388 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8389 err = got_error_from_errno2("unlink", blob_base_path);
8390 if (f_base && fclose(f_base) == EOF && err == NULL)
8391 err = got_error_from_errno2("fclose", path_unstaged_content);
8392 if (f && fclose(f) == EOF && err == NULL)
8393 err = got_error_from_errno2("fclose", path_unstaged_content);
8394 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8395 err = got_error_from_errno2("fclose", ondisk_path);
8396 free(path_unstaged_content);
8397 free(path_new_staged_content);
8398 free(blob_base_path);
8404 static const struct got_error *
8405 unstage_path(void *arg, unsigned char status,
8406 unsigned char staged_status, const char *relpath,
8407 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8408 struct got_object_id *commit_id, int dirfd, const char *de_name)
8410 const struct got_error *err = NULL;
8411 struct unstage_path_arg *a = arg;
8412 struct got_fileindex_entry *ie;
8413 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8414 char *ondisk_path = NULL;
8415 char *id_str = NULL, *label_orig = NULL;
8416 int local_changes_subsumed;
8419 if (staged_status != GOT_STATUS_ADD &&
8420 staged_status != GOT_STATUS_MODIFY &&
8421 staged_status != GOT_STATUS_DELETE)
8424 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8426 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8428 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8430 return got_error_from_errno("asprintf");
8432 err = got_object_id_str(&id_str,
8433 commit_id ? commit_id : a->worktree->base_commit_id);
8436 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8438 err = got_error_from_errno("asprintf");
8442 switch (staged_status) {
8443 case GOT_STATUS_MODIFY:
8444 err = got_object_open_as_blob(&blob_base, a->repo,
8449 case GOT_STATUS_ADD:
8451 if (staged_status == GOT_STATUS_ADD) {
8452 int choice = GOT_PATCH_CHOICE_NONE;
8453 err = (*a->patch_cb)(&choice, a->patch_arg,
8454 staged_status, ie->path, NULL, 1, 1);
8457 if (choice != GOT_PATCH_CHOICE_YES)
8460 err = unstage_hunks(staged_blob_id,
8461 blob_base, blob_id, ie, ondisk_path,
8462 label_orig, a->worktree, a->repo,
8463 a->patch_cb, a->patch_arg,
8464 a->progress_cb, a->progress_arg);
8465 break; /* Done with this file. */
8468 err = got_object_open_as_blob(&blob_staged, a->repo,
8469 staged_blob_id, 8192);
8472 switch (got_fileindex_entry_staged_filetype_get(ie)) {
8473 case GOT_FILEIDX_MODE_BAD_SYMLINK:
8474 case GOT_FILEIDX_MODE_REGULAR_FILE:
8475 err = merge_blob(&local_changes_subsumed, a->worktree,
8476 blob_base, ondisk_path, relpath,
8477 got_fileindex_perms_to_st(ie), label_orig,
8478 blob_staged, commit_id ? commit_id :
8479 a->worktree->base_commit_id, a->repo,
8480 a->progress_cb, a->progress_arg);
8482 case GOT_FILEIDX_MODE_SYMLINK:
8483 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8484 char *staged_target;
8485 err = got_object_blob_read_to_str(
8486 &staged_target, blob_staged);
8489 err = merge_symlink(a->worktree, blob_base,
8490 ondisk_path, relpath, label_orig,
8491 staged_target, commit_id ? commit_id :
8492 a->worktree->base_commit_id,
8493 a->repo, a->progress_cb, a->progress_arg);
8494 free(staged_target);
8496 err = merge_blob(&local_changes_subsumed,
8497 a->worktree, blob_base, ondisk_path,
8498 relpath, got_fileindex_perms_to_st(ie),
8499 label_orig, blob_staged,
8500 commit_id ? commit_id :
8501 a->worktree->base_commit_id, a->repo,
8502 a->progress_cb, a->progress_arg);
8506 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8510 got_fileindex_entry_stage_set(ie,
8511 GOT_FILEIDX_STAGE_NONE);
8512 got_fileindex_entry_staged_filetype_set(ie, 0);
8515 case GOT_STATUS_DELETE:
8517 int choice = GOT_PATCH_CHOICE_NONE;
8518 err = (*a->patch_cb)(&choice, a->patch_arg,
8519 staged_status, ie->path, NULL, 1, 1);
8522 if (choice == GOT_PATCH_CHOICE_NO)
8524 if (choice != GOT_PATCH_CHOICE_YES) {
8525 err = got_error(GOT_ERR_PATCH_CHOICE);
8529 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8530 got_fileindex_entry_staged_filetype_set(ie, 0);
8531 err = get_file_status(&status, &sb, ie, ondisk_path,
8532 dirfd, de_name, a->repo);
8535 err = (*a->progress_cb)(a->progress_arg, status, relpath);
8541 got_object_blob_close(blob_base);
8543 got_object_blob_close(blob_staged);
8549 const struct got_error *
8550 got_worktree_unstage(struct got_worktree *worktree,
8551 struct got_pathlist_head *paths,
8552 got_worktree_checkout_cb progress_cb, void *progress_arg,
8553 got_worktree_patch_cb patch_cb, void *patch_arg,
8554 struct got_repository *repo)
8556 const struct got_error *err = NULL, *sync_err, *unlockerr;
8557 struct got_pathlist_entry *pe;
8558 struct got_fileindex *fileindex = NULL;
8559 char *fileindex_path = NULL;
8560 struct unstage_path_arg upa;
8562 err = lock_worktree(worktree, LOCK_EX);
8566 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8570 upa.worktree = worktree;
8571 upa.fileindex = fileindex;
8573 upa.progress_cb = progress_cb;
8574 upa.progress_arg = progress_arg;
8575 upa.patch_cb = patch_cb;
8576 upa.patch_arg = patch_arg;
8577 TAILQ_FOREACH(pe, paths, entry) {
8578 err = worktree_status(worktree, pe->path, fileindex, repo,
8579 unstage_path, &upa, NULL, NULL, 1, 0);
8584 sync_err = sync_fileindex(fileindex, fileindex_path);
8585 if (sync_err && err == NULL)
8588 free(fileindex_path);
8590 got_fileindex_free(fileindex);
8591 unlockerr = lock_worktree(worktree, LOCK_SH);
8592 if (unlockerr && err == NULL)
8597 struct report_file_info_arg {
8598 struct got_worktree *worktree;
8599 got_worktree_path_info_cb info_cb;
8601 struct got_pathlist_head *paths;
8602 got_cancel_cb cancel_cb;
8606 static const struct got_error *
8607 report_file_info(void *arg, struct got_fileindex_entry *ie)
8609 struct report_file_info_arg *a = arg;
8610 struct got_pathlist_entry *pe;
8611 struct got_object_id blob_id, staged_blob_id, commit_id;
8612 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8613 struct got_object_id *commit_idp = NULL;
8616 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8617 return got_error(GOT_ERR_CANCELLED);
8619 TAILQ_FOREACH(pe, a->paths, entry) {
8620 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8621 got_path_is_child(ie->path, pe->path, pe->path_len))
8624 if (pe == NULL) /* not found */
8627 if (got_fileindex_entry_has_blob(ie)) {
8628 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8629 blob_idp = &blob_id;
8631 stage = got_fileindex_entry_stage_get(ie);
8632 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8633 stage == GOT_FILEIDX_STAGE_ADD) {
8634 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8635 SHA1_DIGEST_LENGTH);
8636 staged_blob_idp = &staged_blob_id;
8639 if (got_fileindex_entry_has_commit(ie)) {
8640 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8641 commit_idp = &commit_id;
8644 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8645 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8648 const struct got_error *
8649 got_worktree_path_info(struct got_worktree *worktree,
8650 struct got_pathlist_head *paths,
8651 got_worktree_path_info_cb info_cb, void *info_arg,
8652 got_cancel_cb cancel_cb, void *cancel_arg)
8655 const struct got_error *err = NULL, *unlockerr;
8656 struct got_fileindex *fileindex = NULL;
8657 char *fileindex_path = NULL;
8658 struct report_file_info_arg arg;
8660 err = lock_worktree(worktree, LOCK_SH);
8664 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8668 arg.worktree = worktree;
8669 arg.info_cb = info_cb;
8670 arg.info_arg = info_arg;
8672 arg.cancel_cb = cancel_cb;
8673 arg.cancel_arg = cancel_arg;
8674 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8677 free(fileindex_path);
8679 got_fileindex_free(fileindex);
8680 unlockerr = lock_worktree(worktree, LOCK_UN);
8681 if (unlockerr && err == NULL)