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.
17 #include "got_compat.h"
20 #include <sys/queue.h>
36 #include "got_error.h"
37 #include "got_repository.h"
38 #include "got_reference.h"
39 #include "got_object.h"
41 #include "got_cancel.h"
42 #include "got_worktree.h"
43 #include "got_opentemp.h"
46 #include "got_lib_worktree.h"
47 #include "got_lib_hash.h"
48 #include "got_lib_fileindex.h"
49 #include "got_lib_inflate.h"
50 #include "got_lib_delta.h"
51 #include "got_lib_object.h"
52 #include "got_lib_object_parse.h"
53 #include "got_lib_object_create.h"
54 #include "got_lib_object_idset.h"
55 #include "got_lib_diff.h"
56 #include "got_lib_gotconfig.h"
59 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
62 #define GOT_MERGE_LABEL_MERGED "merged change"
63 #define GOT_MERGE_LABEL_BASE "3-way merge base"
66 apply_umask(mode_t mode)
75 static const struct got_error *
76 create_meta_file(const char *path_got, const char *name, const char *content)
78 const struct got_error *err = NULL;
81 if (asprintf(&path, "%s/%s", path_got, name) == -1)
82 return got_error_from_errno("asprintf");
84 err = got_path_create_file(path, content);
89 static const struct got_error *
90 update_meta_file(const char *path_got, const char *name, const char *content)
92 const struct got_error *err = NULL;
97 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
98 err = got_error_from_errno("asprintf");
103 err = got_opentemp_named(&tmppath, &tmpfile, path, "");
108 int len = fprintf(tmpfile, "%s\n", content);
109 if (len != strlen(content) + 1) {
110 err = got_error_from_errno2("fprintf", tmppath);
115 if (rename(tmppath, path) != 0) {
116 err = got_error_from_errno3("rename", tmppath, path);
122 if (fclose(tmpfile) == EOF && err == NULL)
123 err = got_error_from_errno2("fclose", tmppath);
128 static const struct got_error *
129 write_head_ref(const char *path_got, struct got_reference *head_ref)
131 const struct got_error *err = NULL;
134 if (got_ref_is_symbolic(head_ref)) {
135 refstr = got_ref_to_str(head_ref);
137 return got_error_from_errno("got_ref_to_str");
139 refstr = strdup(got_ref_get_name(head_ref));
141 return got_error_from_errno("strdup");
143 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
148 const struct got_error *
149 got_worktree_init(const char *path, struct got_reference *head_ref,
150 const char *prefix, struct got_repository *repo)
152 const struct got_error *err = NULL;
153 struct got_object_id *commit_id = NULL;
155 uint32_t uuid_status;
157 char *path_got = NULL;
158 char *formatstr = NULL;
159 char *absprefix = NULL;
160 char *basestr = NULL;
161 char *uuidstr = NULL;
163 if (strcmp(path, got_repo_get_path(repo)) == 0) {
164 err = got_error(GOT_ERR_WORKTREE_REPO);
168 err = got_ref_resolve(&commit_id, repo, head_ref);
171 err = got_object_get_type(&obj_type, repo, commit_id);
174 if (obj_type != GOT_OBJ_TYPE_COMMIT)
175 return got_error(GOT_ERR_OBJ_TYPE);
177 if (!got_path_is_absolute(prefix)) {
178 if (asprintf(&absprefix, "/%s", prefix) == -1)
179 return got_error_from_errno("asprintf");
182 /* Create top-level directory (may already exist). */
183 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
184 err = got_error_from_errno2("mkdir", path);
188 /* Create .got directory (may already exist). */
189 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
190 err = got_error_from_errno("asprintf");
193 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
194 err = got_error_from_errno2("mkdir", path_got);
198 /* Create an empty lock file. */
199 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
203 /* Create an empty file index. */
204 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
208 /* Write the HEAD reference. */
209 err = write_head_ref(path_got, head_ref);
213 /* Record our base commit. */
214 err = got_object_id_str(&basestr, commit_id);
217 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
221 /* Store path to repository. */
222 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
223 got_repo_get_path(repo));
227 /* Store in-repository path prefix. */
228 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
229 absprefix ? absprefix : prefix);
234 uuid_create(&uuid, &uuid_status);
235 if (uuid_status != uuid_s_ok) {
236 err = got_error_uuid(uuid_status, "uuid_create");
239 uuid_to_string(&uuid, &uuidstr, &uuid_status);
240 if (uuid_status != uuid_s_ok) {
241 err = got_error_uuid(uuid_status, "uuid_to_string");
244 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
248 /* Stamp work tree with format file. */
249 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
250 err = got_error_from_errno("asprintf");
253 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
267 const struct got_error *
268 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
269 const char *path_prefix)
271 char *absprefix = NULL;
273 if (!got_path_is_absolute(path_prefix)) {
274 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
275 return got_error_from_errno("asprintf");
277 *match = (strcmp(absprefix ? absprefix : path_prefix,
278 worktree->path_prefix) == 0);
284 got_worktree_get_head_ref_name(struct got_worktree *worktree)
286 return worktree->head_ref_name;
289 const struct got_error *
290 got_worktree_set_head_ref(struct got_worktree *worktree,
291 struct got_reference *head_ref)
293 const struct got_error *err = NULL;
294 char *path_got = NULL, *head_ref_name = NULL;
296 if (asprintf(&path_got, "%s/%s", worktree->root_path,
297 GOT_WORKTREE_GOT_DIR) == -1) {
298 err = got_error_from_errno("asprintf");
303 head_ref_name = strdup(got_ref_get_name(head_ref));
304 if (head_ref_name == NULL) {
305 err = got_error_from_errno("strdup");
309 err = write_head_ref(path_got, head_ref);
313 free(worktree->head_ref_name);
314 worktree->head_ref_name = head_ref_name;
322 struct got_object_id *
323 got_worktree_get_base_commit_id(struct got_worktree *worktree)
325 return worktree->base_commit_id;
328 const struct got_error *
329 got_worktree_set_base_commit_id(struct got_worktree *worktree,
330 struct got_repository *repo, struct got_object_id *commit_id)
332 const struct got_error *err;
333 struct got_object *obj = NULL;
335 char *path_got = NULL;
337 if (asprintf(&path_got, "%s/%s", worktree->root_path,
338 GOT_WORKTREE_GOT_DIR) == -1) {
339 err = got_error_from_errno("asprintf");
344 err = got_object_open(&obj, repo, commit_id);
348 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
349 err = got_error(GOT_ERR_OBJ_TYPE);
353 /* Record our base commit. */
354 err = got_object_id_str(&id_str, commit_id);
357 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
361 free(worktree->base_commit_id);
362 worktree->base_commit_id = got_object_id_dup(commit_id);
363 if (worktree->base_commit_id == NULL) {
364 err = got_error_from_errno("got_object_id_dup");
369 got_object_close(obj);
375 const struct got_gotconfig *
376 got_worktree_get_gotconfig(struct got_worktree *worktree)
378 return worktree->gotconfig;
381 static const struct got_error *
382 lock_worktree(struct got_worktree *worktree, int operation)
384 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
385 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
386 : got_error_from_errno2("flock",
387 got_worktree_get_root_path(worktree)));
391 static const struct got_error *
392 add_dir_on_disk(struct got_worktree *worktree, const char *path)
394 const struct got_error *err = NULL;
397 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
398 return got_error_from_errno("asprintf");
400 err = got_path_mkdir(abspath);
401 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
404 if (lstat(abspath, &sb) == -1) {
405 err = got_error_from_errno2("lstat", abspath);
406 } else if (!S_ISDIR(sb.st_mode)) {
407 /* TODO directory is obstructed; do something */
408 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
415 static const struct got_error *
416 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
418 const struct got_error *err = NULL;
421 size_t flen1 = 0, flen2 = 0;
426 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
427 if (flen1 == 0 && ferror(f1)) {
428 err = got_error_from_errno("fread");
431 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
432 if (flen2 == 0 && ferror(f2)) {
433 err = got_error_from_errno("fread");
440 } else if (flen2 == 0) {
444 } else if (flen1 == flen2) {
445 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
458 static const struct got_error *
459 check_files_equal(int *same, FILE *f1, FILE *f2)
466 if (fstat(fileno(f1), &sb) != 0)
467 return got_error_from_errno("fstat");
470 if (fstat(fileno(f2), &sb) != 0)
471 return got_error_from_errno("fstat");
474 if (size1 != size2) {
479 if (fseek(f1, 0L, SEEK_SET) == -1)
480 return got_ferror(f1, GOT_ERR_IO);
481 if (fseek(f2, 0L, SEEK_SET) == -1)
482 return got_ferror(f2, GOT_ERR_IO);
484 return check_file_contents_equal(same, f1, f2);
487 static const struct got_error *
488 copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
496 if (fseek(f, 0L, SEEK_SET) == -1)
497 return got_ferror(f, GOT_ERR_IO);
500 flen = fread(fbuf, 1, sizeof(fbuf), f);
503 return got_error_from_errno("fread");
507 outlen = write(outfd, fbuf, flen);
509 return got_error_from_errno("write");
511 return got_error(GOT_ERR_IO);
518 static const struct got_error *
519 merge_binary_file(int *overlapcnt, int merged_fd,
520 FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
521 const char *label_deriv, const char *label_orig, const char *label_deriv2,
522 const char *ondisk_path)
524 const struct got_error *err = NULL;
525 int same_content, changed_deriv, changed_deriv2;
526 int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
527 off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
528 char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
529 char *base_path_orig = NULL, *base_path_deriv = NULL;
530 char *base_path_deriv2 = NULL;
534 err = check_files_equal(&same_content, f_deriv, f_deriv2);
539 return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
541 err = check_files_equal(&same_content, f_deriv, f_orig);
544 changed_deriv = !same_content;
545 err = check_files_equal(&same_content, f_deriv2, f_orig);
548 changed_deriv2 = !same_content;
550 if (changed_deriv && changed_deriv2) {
552 if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
553 err = got_error_from_errno("asprintf");
556 if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
557 err = got_error_from_errno("asprintf");
560 if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
561 err = got_error_from_errno("asprintf");
564 err = got_opentemp_named_fd(&path_orig, &fd_orig,
568 err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
569 base_path_deriv, "");
572 err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
573 base_path_deriv2, "");
576 err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
579 err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
582 err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
585 if (dprintf(merged_fd, "Binary files differ and cannot be "
586 "merged automatically:\n") < 0) {
587 err = got_error_from_errno("dprintf");
590 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
591 GOT_DIFF_CONFLICT_MARKER_BEGIN,
592 label_deriv ? " " : "",
593 label_deriv ? label_deriv : "",
595 err = got_error_from_errno("dprintf");
599 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
600 GOT_DIFF_CONFLICT_MARKER_ORIG,
601 label_orig ? " " : "",
602 label_orig ? label_orig : "",
604 err = got_error_from_errno("dprintf");
608 if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
609 GOT_DIFF_CONFLICT_MARKER_SEP,
611 GOT_DIFF_CONFLICT_MARKER_END,
612 label_deriv2 ? " " : "",
613 label_deriv2 ? label_deriv2 : "") < 0) {
614 err = got_error_from_errno("dprintf");
617 } else if (changed_deriv)
618 err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
619 else if (changed_deriv2)
620 err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
622 if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
624 err = got_error_from_errno2("unlink", path_orig);
625 if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
626 err = got_error_from_errno2("close", path_orig);
627 if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
628 err = got_error_from_errno2("close", path_deriv);
629 if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
630 err = got_error_from_errno2("close", path_deriv2);
634 free(base_path_orig);
635 free(base_path_deriv);
636 free(base_path_deriv2);
641 * Perform a 3-way merge where the file f_orig acts as the common
642 * ancestor, the file f_deriv acts as the first derived version,
643 * and the file f_deriv2 acts as the second derived version.
644 * The merge result will be written to a new file at ondisk_path; any
645 * existing file at this path will be replaced.
647 static const struct got_error *
648 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
649 FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
650 const char *path, uint16_t st_mode,
651 const char *label_orig, const char *label_deriv, const char *label_deriv2,
652 enum got_diff_algorithm diff_algo, struct got_repository *repo,
653 got_worktree_checkout_cb progress_cb, void *progress_arg)
655 const struct got_error *err = NULL;
657 FILE *f_merged = NULL;
658 char *merged_path = NULL, *base_path = NULL;
662 *local_changes_subsumed = 0;
664 err = got_path_dirname(&parent, ondisk_path);
668 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
669 err = got_error_from_errno("asprintf");
673 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path, "");
677 err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
678 f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
680 if (err->code != GOT_ERR_FILE_BINARY)
682 err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
683 f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
689 err = (*progress_cb)(progress_arg,
690 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
694 if (fsync(merged_fd) != 0) {
695 err = got_error_from_errno("fsync");
699 f_merged = fdopen(merged_fd, "r");
700 if (f_merged == NULL) {
701 err = got_error_from_errno("fdopen");
706 /* Check if a clean merge has subsumed all local changes. */
707 if (overlapcnt == 0) {
708 err = check_files_equal(local_changes_subsumed, f_deriv,
714 if (fchmod(fileno(f_merged), apply_umask(st_mode)) != 0) {
715 err = got_error_from_errno2("fchmod", merged_path);
719 if (rename(merged_path, ondisk_path) != 0) {
720 err = got_error_from_errno3("rename", merged_path,
729 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
730 err = got_error_from_errno("close");
731 if (f_merged && fclose(f_merged) == EOF && err == NULL)
732 err = got_error_from_errno("fclose");
739 static const struct got_error *
740 update_symlink(const char *ondisk_path, const char *target_path,
743 /* This is not atomic but matches what 'ln -sf' does. */
744 if (unlink(ondisk_path) == -1)
745 return got_error_from_errno2("unlink", ondisk_path);
746 if (symlink(target_path, ondisk_path) == -1)
747 return got_error_from_errno3("symlink", target_path,
753 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
754 * in the work tree with a file that contains conflict markers and the
755 * conflicting target paths of the original version, a "derived version"
756 * of a symlink from an incoming change, and a local version of the symlink.
758 * The original versions's target path can be NULL if it is not available,
759 * such as if both derived versions added a new symlink at the same path.
761 * The incoming derived symlink target is NULL in case the incoming change
762 * has deleted this symlink.
764 static const struct got_error *
765 install_symlink_conflict(const char *deriv_target,
766 struct got_object_id *deriv_base_commit_id, const char *orig_target,
767 const char *label_orig, const char *local_target, const char *ondisk_path)
769 const struct got_error *err;
770 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
773 err = got_object_id_str(&id_str, deriv_base_commit_id);
775 return got_error_from_errno("asprintf");
777 if (asprintf(&label_deriv, "%s: commit %s",
778 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
779 err = got_error_from_errno("asprintf");
783 err = got_opentemp_named(&path, &f, "got-symlink-conflict", "");
787 if (fchmod(fileno(f), apply_umask(GOT_DEFAULT_FILE_MODE)) == -1) {
788 err = got_error_from_errno2("fchmod", path);
792 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
793 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
794 deriv_target ? deriv_target : "(symlink was deleted)",
795 orig_target ? label_orig : "",
796 orig_target ? "\n" : "",
797 orig_target ? orig_target : "",
798 orig_target ? "\n" : "",
799 GOT_DIFF_CONFLICT_MARKER_SEP,
800 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
801 err = got_error_from_errno2("fprintf", path);
805 if (unlink(ondisk_path) == -1) {
806 err = got_error_from_errno2("unlink", ondisk_path);
809 if (rename(path, ondisk_path) == -1) {
810 err = got_error_from_errno3("rename", path, ondisk_path);
814 if (f != NULL && fclose(f) == EOF && err == NULL)
815 err = got_error_from_errno2("fclose", path);
822 /* forward declaration */
823 static const struct got_error *
824 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
825 const char *, const char *, uint16_t, const char *,
826 struct got_blob_object *, struct got_object_id *,
827 struct got_repository *, got_worktree_checkout_cb, void *);
830 * Merge a symlink into the work tree, where blob_orig acts as the common
831 * ancestor, deriv_target is the link target of the first derived version,
832 * and the symlink on disk acts as the second derived version.
833 * Assume that contents of both blobs represent symlinks.
835 static const struct got_error *
836 merge_symlink(struct got_worktree *worktree,
837 struct got_blob_object *blob_orig, const char *ondisk_path,
838 const char *path, const char *label_orig, const char *deriv_target,
839 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
840 got_worktree_checkout_cb progress_cb, void *progress_arg)
842 const struct got_error *err = NULL;
843 char *ancestor_target = NULL;
845 ssize_t ondisk_len, deriv_len;
846 char ondisk_target[PATH_MAX];
847 int have_local_change = 0;
848 int have_incoming_change = 0;
850 if (lstat(ondisk_path, &sb) == -1)
851 return got_error_from_errno2("lstat", ondisk_path);
853 ondisk_len = readlink(ondisk_path, ondisk_target,
854 sizeof(ondisk_target));
855 if (ondisk_len == -1) {
856 err = got_error_from_errno2("readlink",
860 ondisk_target[ondisk_len] = '\0';
863 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
868 if (ancestor_target == NULL ||
869 (ondisk_len != strlen(ancestor_target) ||
870 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
871 have_local_change = 1;
873 deriv_len = strlen(deriv_target);
874 if (ancestor_target == NULL ||
875 (deriv_len != strlen(ancestor_target) ||
876 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
877 have_incoming_change = 1;
879 if (!have_local_change && !have_incoming_change) {
880 if (ancestor_target) {
881 /* Both sides made the same change. */
882 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
884 } else if (deriv_len == ondisk_len &&
885 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
886 /* Both sides added the same symlink. */
887 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
890 /* Both sides added symlinks which don't match. */
891 err = install_symlink_conflict(deriv_target,
892 deriv_base_commit_id, ancestor_target,
893 label_orig, ondisk_target, ondisk_path);
896 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
899 } else if (!have_local_change && have_incoming_change) {
900 /* Apply the incoming change. */
901 err = update_symlink(ondisk_path, deriv_target,
902 strlen(deriv_target));
905 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
906 } else if (have_local_change && have_incoming_change) {
907 if (deriv_len == ondisk_len &&
908 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
909 /* Both sides made the same change. */
910 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
913 err = install_symlink_conflict(deriv_target,
914 deriv_base_commit_id, ancestor_target, label_orig,
915 ondisk_target, ondisk_path);
918 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
924 free(ancestor_target);
928 static const struct got_error *
929 dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
931 const struct got_error *err = NULL;
932 char target_path[PATH_MAX];
941 return got_error_from_errno("got_opentemp");
942 target_len = readlink(ondisk_path, target_path, sizeof(target_path));
943 if (target_len == -1) {
944 err = got_error_from_errno2("readlink", ondisk_path);
947 n = fwrite(target_path, 1, target_len, f);
948 if (n != target_len) {
949 err = got_ferror(f, GOT_ERR_IO);
952 if (fflush(f) == EOF) {
953 err = got_error_from_errno("fflush");
956 if (fseek(f, 0L, SEEK_SET) == -1) {
957 err = got_ferror(f, GOT_ERR_IO);
969 * Perform a 3-way merge where blob_orig acts as the common ancestor,
970 * blob_deriv acts as the first derived version, and the file on disk
971 * acts as the second derived version.
973 static const struct got_error *
974 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
975 struct got_blob_object *blob_orig, const char *ondisk_path,
976 const char *path, uint16_t st_mode, const char *label_orig,
977 struct got_blob_object *blob_deriv,
978 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
979 got_worktree_checkout_cb progress_cb, void *progress_arg)
981 const struct got_error *err = NULL;
982 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
983 char *blob_orig_path = NULL;
984 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
985 char *label_deriv = NULL, *parent = NULL;
987 *local_changes_subsumed = 0;
989 err = got_path_dirname(&parent, ondisk_path);
994 if (asprintf(&base_path, "%s/got-merge-blob-orig",
996 err = got_error_from_errno("asprintf");
1001 err = got_opentemp_named(&blob_orig_path, &f_orig,
1005 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1012 * No common ancestor exists. This is an "add vs add" conflict
1013 * and we simply use an empty ancestor file to make both files
1014 * appear in the merged result in their entirety.
1016 f_orig = got_opentemp();
1017 if (f_orig == NULL) {
1018 err = got_error_from_errno("got_opentemp");
1023 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1024 err = got_error_from_errno("asprintf");
1029 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path, "");
1032 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1037 err = got_object_id_str(&id_str, deriv_base_commit_id);
1040 if (asprintf(&label_deriv, "%s: commit %s",
1041 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1042 err = got_error_from_errno("asprintf");
1047 * In order the run a 3-way merge with a symlink we copy the symlink's
1048 * target path into a temporary file and use that file with diff3.
1050 if (S_ISLNK(st_mode)) {
1051 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1056 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1058 err = got_error_from_errno2("open", ondisk_path);
1061 f_deriv2 = fdopen(fd, "r");
1062 if (f_deriv2 == NULL) {
1063 err = got_error_from_errno2("fdopen", ondisk_path);
1069 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1070 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1071 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1073 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1074 err = got_error_from_errno("fclose");
1075 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1076 err = got_error_from_errno("fclose");
1077 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1078 err = got_error_from_errno("fclose");
1080 if (blob_orig_path) {
1081 unlink(blob_orig_path);
1082 free(blob_orig_path);
1084 if (blob_deriv_path) {
1085 unlink(blob_deriv_path);
1086 free(blob_deriv_path);
1094 static const struct got_error *
1095 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1096 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1097 int wt_fd, const char *path, struct got_object_id *blob_id)
1099 const struct got_error *err = NULL;
1100 struct got_fileindex_entry *new_ie;
1104 err = got_fileindex_entry_alloc(&new_ie, path);
1108 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1109 blob_id->sha1, base_commit_id->sha1, 1);
1113 err = got_fileindex_entry_add(fileindex, new_ie);
1116 got_fileindex_entry_free(new_ie);
1123 get_ondisk_perms(int executable, mode_t st_mode)
1125 mode_t xbits = S_IXUSR;
1128 /* Map read bits to execute bits. */
1129 if (st_mode & S_IRGRP)
1131 if (st_mode & S_IROTH)
1133 return st_mode | xbits;
1139 /* forward declaration */
1140 static const struct got_error *
1141 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1142 const char *path, mode_t te_mode, mode_t st_mode,
1143 struct got_blob_object *blob, int restoring_missing_file,
1144 int reverting_versioned_file, int installing_bad_symlink,
1145 int path_is_unversioned, struct got_repository *repo,
1146 got_worktree_checkout_cb progress_cb, void *progress_arg);
1149 * This function assumes that the provided symlink target points at a
1150 * safe location in the work tree!
1152 static const struct got_error *
1153 replace_existing_symlink(int *did_something, const char *ondisk_path,
1154 const char *target_path, size_t target_len)
1156 const struct got_error *err = NULL;
1158 char etarget[PATH_MAX];
1164 * "Bad" symlinks (those pointing outside the work tree or into the
1165 * .got directory) are installed in the work tree as a regular file
1166 * which contains the bad symlink target path.
1167 * The new symlink target has already been checked for safety by our
1168 * caller. If we can successfully open a regular file then we simply
1169 * replace this file with a symlink below.
1171 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1173 if (!got_err_open_nofollow_on_symlink())
1174 return got_error_from_errno2("open", ondisk_path);
1176 /* We are updating an existing on-disk symlink. */
1177 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1179 return got_error_from_errno2("readlink", ondisk_path);
1181 if (elen == target_len &&
1182 memcmp(etarget, target_path, target_len) == 0)
1183 return NULL; /* nothing to do */
1187 err = update_symlink(ondisk_path, target_path, target_len);
1188 if (fd != -1 && close(fd) == -1 && err == NULL)
1189 err = got_error_from_errno2("close", ondisk_path);
1193 static const struct got_error *
1194 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1195 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1197 const struct got_error *err = NULL;
1198 char canonpath[PATH_MAX];
1199 char *path_got = NULL;
1201 *is_bad_symlink = 0;
1203 if (target_len >= sizeof(canonpath)) {
1204 *is_bad_symlink = 1;
1209 * We do not use realpath(3) to resolve the symlink's target
1210 * path because we don't want to resolve symlinks recursively.
1211 * Instead we make the path absolute and then canonicalize it.
1212 * Relative symlink target lookup should begin at the directory
1213 * in which the blob object is being installed.
1215 if (!got_path_is_absolute(target_path)) {
1216 char *abspath, *parent;
1217 err = got_path_dirname(&parent, ondisk_path);
1220 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1222 return got_error_from_errno("asprintf");
1225 if (strlen(abspath) >= sizeof(canonpath)) {
1226 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1230 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1235 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1240 /* Only allow symlinks pointing at paths within the work tree. */
1241 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1242 *is_bad_symlink = 1;
1246 /* Do not allow symlinks pointing into the .got directory. */
1247 if (asprintf(&path_got, "%s/%s", wtroot_path,
1248 GOT_WORKTREE_GOT_DIR) == -1)
1249 return got_error_from_errno("asprintf");
1250 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1251 *is_bad_symlink = 1;
1257 static const struct got_error *
1258 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1259 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1260 int restoring_missing_file, int reverting_versioned_file,
1261 int path_is_unversioned, int allow_bad_symlinks,
1262 struct got_repository *repo,
1263 got_worktree_checkout_cb progress_cb, void *progress_arg)
1265 const struct got_error *err = NULL;
1266 char target_path[PATH_MAX];
1267 size_t len, target_len = 0;
1268 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1269 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1271 *is_bad_symlink = 0;
1274 * Blob object content specifies the target path of the link.
1275 * If a symbolic link cannot be installed we instead create
1276 * a regular file which contains the link target path stored
1277 * in the blob object.
1280 err = got_object_blob_read_block(&len, blob);
1284 if (len + target_len >= sizeof(target_path)) {
1285 /* Path too long; install as a regular file. */
1286 *is_bad_symlink = 1;
1287 got_object_blob_rewind(blob);
1288 return install_blob(worktree, ondisk_path, path,
1289 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1290 restoring_missing_file, reverting_versioned_file,
1291 1, path_is_unversioned, repo, progress_cb,
1295 /* Skip blob object header first time around. */
1296 memcpy(target_path + target_len, buf + hdrlen,
1298 target_len += len - hdrlen;
1302 target_path[target_len] = '\0';
1304 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1305 ondisk_path, worktree->root_path);
1309 if (*is_bad_symlink && !allow_bad_symlinks) {
1310 /* install as a regular file */
1311 got_object_blob_rewind(blob);
1312 err = install_blob(worktree, ondisk_path, path,
1313 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1314 restoring_missing_file, reverting_versioned_file, 1,
1315 path_is_unversioned, repo, progress_cb, progress_arg);
1319 if (symlink(target_path, ondisk_path) == -1) {
1320 if (errno == EEXIST) {
1321 int symlink_replaced;
1322 if (path_is_unversioned) {
1323 err = (*progress_cb)(progress_arg,
1324 GOT_STATUS_UNVERSIONED, path);
1327 err = replace_existing_symlink(&symlink_replaced,
1328 ondisk_path, target_path, target_len);
1332 if (symlink_replaced) {
1333 err = (*progress_cb)(progress_arg,
1334 reverting_versioned_file ?
1336 GOT_STATUS_UPDATE, path);
1338 err = (*progress_cb)(progress_arg,
1339 GOT_STATUS_EXISTS, path);
1342 return err; /* Nothing else to do. */
1345 if (errno == ENOENT) {
1347 err = got_path_dirname(&parent, ondisk_path);
1350 err = add_dir_on_disk(worktree, parent);
1355 * Retry, and fall through to error handling
1356 * below if this second attempt fails.
1358 if (symlink(target_path, ondisk_path) != -1) {
1359 err = NULL; /* success */
1364 /* Handle errors from first or second creation attempt. */
1365 if (errno == ENAMETOOLONG) {
1366 /* bad target path; install as a regular file */
1367 *is_bad_symlink = 1;
1368 got_object_blob_rewind(blob);
1369 err = install_blob(worktree, ondisk_path, path,
1370 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1371 restoring_missing_file, reverting_versioned_file, 1,
1372 path_is_unversioned, repo,
1373 progress_cb, progress_arg);
1374 } else if (errno == ENOTDIR) {
1375 err = got_error_path(ondisk_path,
1376 GOT_ERR_FILE_OBSTRUCTED);
1378 err = got_error_from_errno3("symlink",
1379 target_path, ondisk_path);
1381 } else if (progress_cb)
1382 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1383 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1387 static const struct got_error *
1388 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1389 const char *path, mode_t te_mode, mode_t st_mode,
1390 struct got_blob_object *blob, int restoring_missing_file,
1391 int reverting_versioned_file, int installing_bad_symlink,
1392 int path_is_unversioned, struct got_repository *repo,
1393 got_worktree_checkout_cb progress_cb, void *progress_arg)
1395 const struct got_error *err = NULL;
1399 char *tmppath = NULL;
1402 mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1403 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1406 if (errno == ENOENT || errno == ENOTDIR) {
1408 err = got_path_dirname(&parent, path);
1411 err = add_dir_on_disk(worktree, parent);
1412 if (err && err->code == GOT_ERR_FILE_OBSTRUCTED)
1413 err = got_error_path(path, err->code);
1417 fd = open(ondisk_path,
1418 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1421 return got_error_from_errno2("open",
1423 } else if (errno == EEXIST) {
1424 if (path_is_unversioned) {
1425 err = (*progress_cb)(progress_arg,
1426 GOT_STATUS_UNVERSIONED, path);
1429 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1430 !S_ISREG(st_mode) && !installing_bad_symlink) {
1431 /* TODO file is obstructed; do something */
1432 err = got_error_path(ondisk_path,
1433 GOT_ERR_FILE_OBSTRUCTED);
1436 err = got_opentemp_named_fd(&tmppath, &fd,
1442 if (fchmod(fd, apply_umask(mode)) == -1) {
1443 err = got_error_from_errno2("fchmod",
1449 return got_error_from_errno2("open", ondisk_path);
1453 if (restoring_missing_file)
1454 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1456 else if (reverting_versioned_file)
1457 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1460 err = (*progress_cb)(progress_arg,
1461 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1466 hdrlen = got_object_blob_get_hdrlen(blob);
1468 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1469 err = got_object_blob_read_block(&len, blob);
1473 /* Skip blob object header first time around. */
1474 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1476 err = got_error_from_errno("write");
1478 } else if (outlen != len - hdrlen) {
1479 err = got_error(GOT_ERR_IO);
1486 if (fsync(fd) != 0) {
1487 err = got_error_from_errno("fsync");
1492 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1493 err = got_error_from_errno2("unlink", ondisk_path);
1496 if (rename(tmppath, ondisk_path) != 0) {
1497 err = got_error_from_errno3("rename", tmppath,
1506 if (fd != -1 && close(fd) == -1 && err == NULL)
1507 err = got_error_from_errno("close");
1508 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1509 err = got_error_from_errno2("unlink", tmppath);
1515 * Upgrade STATUS_MODIFY to STATUS_CONFLICT if a
1516 * conflict marker is found in newly added lines only.
1518 static const struct got_error *
1519 get_modified_file_content_status(unsigned char *status,
1520 struct got_blob_object *blob, const char *path, struct stat *sb,
1523 const struct got_error *err, *free_err;
1524 const char *markers[3] = {
1525 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1526 GOT_DIFF_CONFLICT_MARKER_SEP,
1527 GOT_DIFF_CONFLICT_MARKER_END
1530 struct got_diffreg_result *diffreg_result = NULL;
1531 struct diff_result *r;
1532 int nchunks_parsed, n, i = 0, ln = 0;
1534 size_t linesize = 0;
1537 if (*status != GOT_STATUS_MODIFY)
1540 f1 = got_opentemp();
1542 return got_error_from_errno("got_opentemp");
1545 got_object_blob_rewind(blob);
1546 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
1551 err = got_diff_files(&diffreg_result, f1, 1, NULL, ondisk_file,
1552 1, NULL, 0, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
1556 r = diffreg_result->result;
1558 for (n = 0; n < r->chunks.len; n += nchunks_parsed) {
1559 struct diff_chunk *c;
1560 struct diff_chunk_context cc = {};
1564 * We can optimise a little by advancing straight
1565 * to the next chunk if this one has no added lines.
1567 c = diff_chunk_get(r, n);
1569 if (diff_chunk_type(c) != CHUNK_PLUS) {
1571 continue; /* removed or unchanged lines */
1574 pos = diff_chunk_get_right_start_pos(c);
1575 if (fseek(ondisk_file, pos, SEEK_SET) == -1) {
1576 err = got_ferror(ondisk_file, GOT_ERR_IO);
1580 diff_chunk_context_load_change(&cc, &nchunks_parsed, r, n, 0);
1581 ln = cc.right.start;
1583 while (ln < cc.right.end) {
1584 linelen = getline(&line, &linesize, ondisk_file);
1585 if (linelen == -1) {
1586 if (feof(ondisk_file))
1588 err = got_ferror(ondisk_file, GOT_ERR_IO);
1592 if (line && strncmp(line, markers[i],
1593 strlen(markers[i])) == 0) {
1594 if (strcmp(markers[i],
1595 GOT_DIFF_CONFLICT_MARKER_END) == 0) {
1596 *status = GOT_STATUS_CONFLICT;
1607 if (f1 != NULL && fclose(f1) == EOF && err == NULL)
1608 err = got_error_from_errno("fclose");
1609 free_err = got_diffreg_result_free(diffreg_result);
1617 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1619 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1620 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1624 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1626 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1627 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1628 ie->mtime_sec == sb->st_mtim.tv_sec &&
1629 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1630 ie->size == (sb->st_size & 0xffffffff) &&
1631 !xbit_differs(ie, sb->st_mode));
1634 static unsigned char
1635 get_staged_status(struct got_fileindex_entry *ie)
1637 switch (got_fileindex_entry_stage_get(ie)) {
1638 case GOT_FILEIDX_STAGE_ADD:
1639 return GOT_STATUS_ADD;
1640 case GOT_FILEIDX_STAGE_DELETE:
1641 return GOT_STATUS_DELETE;
1642 case GOT_FILEIDX_STAGE_MODIFY:
1643 return GOT_STATUS_MODIFY;
1645 return GOT_STATUS_NO_CHANGE;
1649 static const struct got_error *
1650 get_symlink_modification_status(unsigned char *status,
1651 struct got_fileindex_entry *ie, const char *abspath,
1652 int dirfd, const char *de_name, struct got_blob_object *blob)
1654 const struct got_error *err = NULL;
1655 char target_path[PATH_MAX];
1656 char etarget[PATH_MAX];
1658 size_t len, target_len = 0;
1659 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1660 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1662 *status = GOT_STATUS_NO_CHANGE;
1664 /* Blob object content specifies the target path of the link. */
1666 err = got_object_blob_read_block(&len, blob);
1669 if (len + target_len >= sizeof(target_path)) {
1671 * Should not happen. The blob contents were OK
1672 * when this symlink was installed.
1674 return got_error(GOT_ERR_NO_SPACE);
1677 /* Skip blob object header first time around. */
1678 memcpy(target_path + target_len, buf + hdrlen,
1680 target_len += len - hdrlen;
1684 target_path[target_len] = '\0';
1687 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1689 return got_error_from_errno2("readlinkat", abspath);
1691 elen = readlink(abspath, etarget, sizeof(etarget));
1693 return got_error_from_errno2("readlink", abspath);
1696 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1697 *status = GOT_STATUS_MODIFY;
1702 static const struct got_error *
1703 get_file_status(unsigned char *status, struct stat *sb,
1704 struct got_fileindex_entry *ie, const char *abspath,
1705 int dirfd, const char *de_name, struct got_repository *repo)
1707 const struct got_error *err = NULL;
1708 struct got_object_id id;
1710 int fd = -1, fd1 = -1;
1713 struct got_blob_object *blob = NULL;
1715 unsigned char staged_status;
1717 staged_status = get_staged_status(ie);
1718 *status = GOT_STATUS_NO_CHANGE;
1719 memset(sb, 0, sizeof(*sb));
1722 * Whenever the caller provides a directory descriptor and a
1723 * directory entry name for the file, use them! This prevents
1724 * race conditions if filesystem paths change beneath our feet.
1727 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1728 if (errno == ENOENT) {
1729 if (got_fileindex_entry_has_file_on_disk(ie))
1730 *status = GOT_STATUS_MISSING;
1732 *status = GOT_STATUS_DELETE;
1735 err = got_error_from_errno2("fstatat", abspath);
1739 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1740 if (fd == -1 && errno != ENOENT &&
1741 !got_err_open_nofollow_on_symlink())
1742 return got_error_from_errno2("open", abspath);
1743 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1744 if (lstat(abspath, sb) == -1)
1745 return got_error_from_errno2("lstat", abspath);
1746 } else if (fd == -1 || fstat(fd, sb) == -1) {
1747 if (errno == ENOENT) {
1748 if (got_fileindex_entry_has_file_on_disk(ie))
1749 *status = GOT_STATUS_MISSING;
1751 *status = GOT_STATUS_DELETE;
1754 err = got_error_from_errno2("fstat", abspath);
1759 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1760 *status = GOT_STATUS_OBSTRUCTED;
1764 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1765 *status = GOT_STATUS_DELETE;
1767 } else if (!got_fileindex_entry_has_blob(ie) &&
1768 staged_status != GOT_STATUS_ADD) {
1769 *status = GOT_STATUS_ADD;
1773 if (!stat_info_differs(ie, sb))
1776 if (S_ISLNK(sb->st_mode) &&
1777 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1778 *status = GOT_STATUS_MODIFY;
1782 if (staged_status == GOT_STATUS_MODIFY ||
1783 staged_status == GOT_STATUS_ADD)
1784 got_fileindex_entry_get_staged_blob_id(&id, ie);
1786 got_fileindex_entry_get_blob_id(&id, ie);
1788 fd1 = got_opentempfd();
1790 err = got_error_from_errno("got_opentempfd");
1793 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1797 if (S_ISLNK(sb->st_mode)) {
1798 err = get_symlink_modification_status(status, ie,
1799 abspath, dirfd, de_name, blob);
1804 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1806 err = got_error_from_errno2("openat", abspath);
1811 f = fdopen(fd, "r");
1813 err = got_error_from_errno2("fdopen", abspath);
1817 hdrlen = got_object_blob_get_hdrlen(blob);
1819 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1820 err = got_object_blob_read_block(&blen, blob);
1823 /* Skip length of blob object header first time around. */
1824 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1825 if (flen == 0 && ferror(f)) {
1826 err = got_error_from_errno("fread");
1829 if (blen - hdrlen == 0) {
1831 *status = GOT_STATUS_MODIFY;
1833 } else if (flen == 0) {
1834 if (blen - hdrlen != 0)
1835 *status = GOT_STATUS_MODIFY;
1837 } else if (blen - hdrlen == flen) {
1838 /* Skip blob object header first time around. */
1839 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1840 *status = GOT_STATUS_MODIFY;
1844 *status = GOT_STATUS_MODIFY;
1850 if (*status == GOT_STATUS_MODIFY) {
1852 err = get_modified_file_content_status(status, blob, ie->path,
1854 } else if (xbit_differs(ie, sb->st_mode))
1855 *status = GOT_STATUS_MODE_CHANGE;
1857 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1858 err = got_error_from_errno("close");
1860 got_object_blob_close(blob);
1861 if (f != NULL && fclose(f) == EOF && err == NULL)
1862 err = got_error_from_errno2("fclose", abspath);
1863 if (fd != -1 && close(fd) == -1 && err == NULL)
1864 err = got_error_from_errno2("close", abspath);
1869 * Update timestamps in the file index if a file is unmodified and
1870 * we had to run a full content comparison to find out.
1872 static const struct got_error *
1873 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1874 struct got_fileindex_entry *ie, struct stat *sb)
1876 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1877 return got_fileindex_entry_update(ie, wt_fd, path,
1878 ie->blob_sha1, ie->commit_sha1, 1);
1883 static const struct got_error *remove_ondisk_file(const char *, const char *);
1885 static const struct got_error *
1886 update_blob(struct got_worktree *worktree,
1887 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1888 struct got_tree_entry *te, const char *path,
1889 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1892 const struct got_error *err = NULL;
1893 struct got_blob_object *blob = NULL;
1894 char *ondisk_path = NULL;
1895 unsigned char status = GOT_STATUS_NO_CHANGE;
1897 int fd1 = -1, fd2 = -1;
1899 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1900 return got_error_from_errno("asprintf");
1903 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1904 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1907 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1911 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1912 sb.st_mode = got_fileindex_perms_to_st(ie);
1914 if (stat(ondisk_path, &sb) == -1) {
1915 if (errno != ENOENT && errno != ENOTDIR) {
1916 err = got_error_from_errno2("stat",
1920 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1921 status = GOT_STATUS_UNVERSIONED;
1923 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1924 status = GOT_STATUS_UNVERSIONED;
1926 status = GOT_STATUS_OBSTRUCTED;
1930 if (status == GOT_STATUS_OBSTRUCTED) {
1932 got_fileindex_entry_mark_skipped(ie);
1933 err = (*progress_cb)(progress_arg, status, path);
1936 if (status == GOT_STATUS_CONFLICT) {
1938 got_fileindex_entry_mark_skipped(ie);
1939 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1944 if (S_ISDIR(te->mode)) { /* file changing into a directory */
1945 if (status == GOT_STATUS_UNVERSIONED) {
1946 err = (*progress_cb)(progress_arg, status, path);
1947 } else if (status != GOT_STATUS_NO_CHANGE &&
1948 status != GOT_STATUS_DELETE &&
1949 status != GOT_STATUS_NONEXISTENT &&
1950 status != GOT_STATUS_MISSING) {
1951 err = (*progress_cb)(progress_arg,
1952 GOT_STATUS_CANNOT_DELETE, path);
1954 if (status != GOT_STATUS_DELETE &&
1955 status != GOT_STATUS_NONEXISTENT &&
1956 status != GOT_STATUS_MISSING) {
1957 err = remove_ondisk_file(worktree->root_path,
1959 if (err && !(err->code == GOT_ERR_ERRNO &&
1963 got_fileindex_entry_remove(fileindex, ie);
1964 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE,
1967 goto done; /* nothing else to do */
1970 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1971 (S_ISLNK(te->mode) ||
1972 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1974 * This is a regular file or an installed bad symlink.
1975 * If the file index indicates that this file is already
1976 * up-to-date with respect to the repository we can skip
1977 * updating contents of this file.
1979 if (got_fileindex_entry_has_commit(ie) &&
1980 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1981 SHA1_DIGEST_LENGTH) == 0) {
1983 err = sync_timestamps(worktree->root_fd,
1984 path, status, ie, &sb);
1987 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1991 if (got_fileindex_entry_has_blob(ie) &&
1992 memcmp(ie->blob_sha1, te->id.sha1,
1993 SHA1_DIGEST_LENGTH) == 0) {
1994 /* Different commit but the same blob. */
1995 if (got_fileindex_entry_has_commit(ie)) {
1996 /* Update the base commit ID of this file. */
1997 memcpy(ie->commit_sha1,
1998 worktree->base_commit_id->sha1,
1999 sizeof(ie->commit_sha1));
2001 err = sync_timestamps(worktree->root_fd,
2002 path, status, ie, &sb);
2005 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2011 fd1 = got_opentempfd();
2013 err = got_error_from_errno("got_opentempfd");
2016 err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
2020 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
2021 int update_timestamps;
2022 struct got_blob_object *blob2 = NULL;
2023 char *label_orig = NULL;
2024 if (got_fileindex_entry_has_blob(ie)) {
2025 fd2 = got_opentempfd();
2027 err = got_error_from_errno("got_opentempfd");
2030 struct got_object_id id2;
2031 got_fileindex_entry_get_blob_id(&id2, ie);
2032 err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
2037 if (got_fileindex_entry_has_commit(ie)) {
2038 char id_str[SHA1_DIGEST_STRING_LENGTH];
2039 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2040 sizeof(id_str)) == NULL) {
2041 err = got_error_path(id_str,
2042 GOT_ERR_BAD_OBJ_ID_STR);
2045 if (asprintf(&label_orig, "%s: commit %s",
2046 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2047 err = got_error_from_errno("asprintf");
2051 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2053 err = got_object_blob_read_to_str(&link_target, blob);
2056 err = merge_symlink(worktree, blob2, ondisk_path, path,
2057 label_orig, link_target, worktree->base_commit_id,
2058 repo, progress_cb, progress_arg);
2061 err = merge_blob(&update_timestamps, worktree, blob2,
2062 ondisk_path, path, sb.st_mode, label_orig, blob,
2063 worktree->base_commit_id, repo,
2064 progress_cb, progress_arg);
2067 if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2068 err = got_error_from_errno("close");
2072 got_object_blob_close(blob2);
2076 * Do not update timestamps of files with local changes.
2077 * Otherwise, a future status walk would treat them as
2078 * unmodified files again.
2080 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2081 blob->id.sha1, worktree->base_commit_id->sha1,
2083 } else if (status == GOT_STATUS_MODE_CHANGE) {
2084 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2085 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2086 } else if (status == GOT_STATUS_DELETE) {
2087 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2090 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2091 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2095 int is_bad_symlink = 0;
2096 if (S_ISLNK(te->mode)) {
2097 err = install_symlink(&is_bad_symlink, worktree,
2098 ondisk_path, path, blob,
2099 status == GOT_STATUS_MISSING, 0,
2100 status == GOT_STATUS_UNVERSIONED, 0,
2101 repo, progress_cb, progress_arg);
2103 err = install_blob(worktree, ondisk_path, path,
2104 te->mode, sb.st_mode, blob,
2105 status == GOT_STATUS_MISSING, 0, 0,
2106 status == GOT_STATUS_UNVERSIONED, repo,
2107 progress_cb, progress_arg);
2113 err = got_fileindex_entry_update(ie,
2114 worktree->root_fd, path, blob->id.sha1,
2115 worktree->base_commit_id->sha1, 1);
2117 err = create_fileindex_entry(&ie, fileindex,
2118 worktree->base_commit_id, worktree->root_fd, path,
2124 if (is_bad_symlink) {
2125 got_fileindex_entry_filetype_set(ie,
2126 GOT_FILEIDX_MODE_BAD_SYMLINK);
2130 if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2131 err = got_error_from_errno("close");
2134 got_object_blob_close(blob);
2140 static const struct got_error *
2141 remove_ondisk_file(const char *root_path, const char *path)
2143 const struct got_error *err = NULL;
2144 char *ondisk_path = NULL, *parent = NULL;
2146 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2147 return got_error_from_errno("asprintf");
2149 if (unlink(ondisk_path) == -1) {
2150 if (errno != ENOENT)
2151 err = got_error_from_errno2("unlink", ondisk_path);
2153 size_t root_len = strlen(root_path);
2154 err = got_path_dirname(&parent, ondisk_path);
2157 while (got_path_cmp(parent, root_path,
2158 strlen(parent), root_len) != 0) {
2160 ondisk_path = parent;
2162 if (rmdir(ondisk_path) == -1) {
2163 if (errno != ENOTEMPTY)
2164 err = got_error_from_errno2("rmdir",
2168 err = got_path_dirname(&parent, ondisk_path);
2179 static const struct got_error *
2180 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2181 struct got_fileindex_entry *ie, struct got_repository *repo,
2182 got_worktree_checkout_cb progress_cb, void *progress_arg)
2184 const struct got_error *err = NULL;
2185 unsigned char status;
2189 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2190 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2192 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2194 return got_error_from_errno("asprintf");
2196 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2200 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2201 char ondisk_target[PATH_MAX];
2202 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2203 sizeof(ondisk_target));
2204 if (ondisk_len == -1) {
2205 err = got_error_from_errno2("readlink", ondisk_path);
2208 ondisk_target[ondisk_len] = '\0';
2209 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2210 NULL, NULL, /* XXX pass common ancestor info? */
2211 ondisk_target, ondisk_path);
2214 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2219 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2220 status == GOT_STATUS_ADD) {
2221 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2225 * Preserve the working file and change the deleted blob's
2226 * entry into a schedule-add entry.
2228 err = got_fileindex_entry_update(ie, worktree->root_fd,
2229 ie->path, NULL, NULL, 0);
2231 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2234 if (status == GOT_STATUS_NO_CHANGE) {
2235 err = remove_ondisk_file(worktree->root_path, ie->path);
2239 got_fileindex_entry_remove(fileindex, ie);
2246 struct diff_cb_arg {
2247 struct got_fileindex *fileindex;
2248 struct got_worktree *worktree;
2249 struct got_repository *repo;
2250 got_worktree_checkout_cb progress_cb;
2252 got_cancel_cb cancel_cb;
2256 static const struct got_error *
2257 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2258 struct got_tree_entry *te, const char *parent_path)
2260 struct diff_cb_arg *a = arg;
2262 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2263 return got_error(GOT_ERR_CANCELLED);
2265 return update_blob(a->worktree, a->fileindex, ie, te,
2266 ie->path, a->repo, a->progress_cb, a->progress_arg);
2269 static const struct got_error *
2270 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2272 struct diff_cb_arg *a = arg;
2274 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2275 return got_error(GOT_ERR_CANCELLED);
2277 return delete_blob(a->worktree, a->fileindex, ie,
2278 a->repo, a->progress_cb, a->progress_arg);
2281 static const struct got_error *
2282 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2284 struct diff_cb_arg *a = arg;
2285 const struct got_error *err;
2288 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2289 return got_error(GOT_ERR_CANCELLED);
2291 if (got_object_tree_entry_is_submodule(te))
2294 if (asprintf(&path, "%s%s%s", parent_path,
2295 parent_path[0] ? "/" : "", te->name)
2297 return got_error_from_errno("asprintf");
2299 if (S_ISDIR(te->mode))
2300 err = add_dir_on_disk(a->worktree, path);
2302 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2303 a->repo, a->progress_cb, a->progress_arg);
2309 const struct got_error *
2310 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2312 uint32_t uuid_status;
2314 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2315 if (uuid_status != uuid_s_ok) {
2317 return got_error_uuid(uuid_status, "uuid_to_string");
2323 static const struct got_error *
2324 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2326 const struct got_error *err = NULL;
2327 char *uuidstr = NULL;
2331 err = got_worktree_get_uuid(&uuidstr, worktree);
2335 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2336 err = got_error_from_errno("asprintf");
2343 const struct got_error *
2344 got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2347 return get_ref_name(refname, worktree, prefix);
2350 static const struct got_error *
2351 get_base_ref_name(char **refname, struct got_worktree *worktree)
2353 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2356 static const struct got_error *
2357 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2359 return get_ref_name(refname, worktree,
2360 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2363 static const struct got_error *
2364 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2366 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2369 static const struct got_error *
2370 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2372 return get_ref_name(refname, worktree,
2373 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2376 static const struct got_error *
2377 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2379 return get_ref_name(refname, worktree,
2380 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2383 static const struct got_error *
2384 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2386 return get_ref_name(refname, worktree,
2387 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2390 static const struct got_error *
2391 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2393 return get_ref_name(refname, worktree,
2394 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2397 static const struct got_error *
2398 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2400 return get_ref_name(refname, worktree,
2401 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2404 static const struct got_error *
2405 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2407 return get_ref_name(refname, worktree,
2408 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2411 const struct got_error *
2412 got_worktree_get_histedit_script_path(char **path,
2413 struct got_worktree *worktree)
2415 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2416 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2418 return got_error_from_errno("asprintf");
2423 static const struct got_error *
2424 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2426 return get_ref_name(refname, worktree,
2427 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2430 static const struct got_error *
2431 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2433 return get_ref_name(refname, worktree,
2434 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2438 * Prevent Git's garbage collector from deleting our base commit by
2439 * setting a reference to our base commit's ID.
2441 static const struct got_error *
2442 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2444 const struct got_error *err = NULL;
2445 struct got_reference *ref = NULL;
2448 err = get_base_ref_name(&refname, worktree);
2452 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2456 err = got_ref_write(ref, repo);
2464 static const struct got_error *
2465 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2467 const struct got_error *err = NULL;
2469 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2470 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2471 err = got_error_from_errno("asprintf");
2472 *fileindex_path = NULL;
2478 static const struct got_error *
2479 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2480 struct got_worktree *worktree)
2482 const struct got_error *err = NULL;
2485 *fileindex_path = NULL;
2486 *fileindex = got_fileindex_alloc();
2487 if (*fileindex == NULL)
2488 return got_error_from_errno("got_fileindex_alloc");
2490 err = get_fileindex_path(fileindex_path, worktree);
2494 index = fopen(*fileindex_path, "rbe");
2495 if (index == NULL) {
2496 if (errno != ENOENT)
2497 err = got_error_from_errno2("fopen", *fileindex_path);
2499 err = got_fileindex_read(*fileindex, index);
2500 if (fclose(index) == EOF && err == NULL)
2501 err = got_error_from_errno("fclose");
2505 free(*fileindex_path);
2506 *fileindex_path = NULL;
2507 got_fileindex_free(*fileindex);
2513 struct bump_base_commit_id_arg {
2514 struct got_object_id *base_commit_id;
2517 const char *entry_name;
2518 got_worktree_checkout_cb progress_cb;
2522 static const struct got_error *
2523 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2525 const struct got_error *err;
2526 struct bump_base_commit_id_arg *a = arg;
2528 if (a->entry_name) {
2529 if (strcmp(ie->path, a->path) != 0)
2531 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2534 if (got_fileindex_entry_was_skipped(ie))
2537 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2538 SHA1_DIGEST_LENGTH) == 0)
2541 if (a->progress_cb) {
2542 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2547 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2551 /* Bump base commit ID of all files within an updated part of the work tree. */
2552 static const struct got_error *
2553 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2554 struct got_fileindex *fileindex,
2555 got_worktree_checkout_cb progress_cb, void *progress_arg)
2557 struct bump_base_commit_id_arg bbc_arg;
2559 bbc_arg.base_commit_id = worktree->base_commit_id;
2560 bbc_arg.entry_name = NULL;
2562 bbc_arg.path_len = 0;
2563 bbc_arg.progress_cb = progress_cb;
2564 bbc_arg.progress_arg = progress_arg;
2566 return got_fileindex_for_each_entry_safe(fileindex,
2567 bump_base_commit_id, &bbc_arg);
2570 static const struct got_error *
2571 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2573 const struct got_error *err = NULL;
2574 char *new_fileindex_path = NULL;
2575 FILE *new_index = NULL;
2576 struct timespec timeout;
2578 err = got_opentemp_named(&new_fileindex_path, &new_index,
2579 fileindex_path, "");
2583 err = got_fileindex_write(fileindex, new_index);
2587 if (rename(new_fileindex_path, fileindex_path) != 0) {
2588 err = got_error_from_errno3("rename", new_fileindex_path,
2590 unlink(new_fileindex_path);
2594 * Sleep for a short amount of time to ensure that files modified after
2595 * this program exits have a different time stamp from the one which
2596 * was recorded in the file index.
2599 timeout.tv_nsec = 1;
2600 nanosleep(&timeout, NULL);
2604 free(new_fileindex_path);
2608 static const struct got_error *
2609 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2610 struct got_object_id **tree_id, const char *wt_relpath,
2611 struct got_commit_object *base_commit, struct got_worktree *worktree,
2612 struct got_repository *repo)
2614 const struct got_error *err = NULL;
2615 struct got_object_id *id = NULL;
2616 char *in_repo_path = NULL;
2617 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2619 *entry_type = GOT_OBJ_TYPE_ANY;
2620 *tree_relpath = NULL;
2623 if (wt_relpath[0] == '\0') {
2624 /* Check out all files within the work tree. */
2625 *entry_type = GOT_OBJ_TYPE_TREE;
2626 *tree_relpath = strdup("");
2627 if (*tree_relpath == NULL) {
2628 err = got_error_from_errno("strdup");
2631 err = got_object_id_by_path(tree_id, repo, base_commit,
2632 worktree->path_prefix);
2638 /* Check out a subset of files in the work tree. */
2640 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2641 is_root_wt ? "" : "/", wt_relpath) == -1) {
2642 err = got_error_from_errno("asprintf");
2646 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2651 in_repo_path = NULL;
2653 err = got_object_get_type(entry_type, repo, id);
2657 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2658 /* Check out a single file. */
2659 if (strchr(wt_relpath, '/') == NULL) {
2660 /* Check out a single file in work tree's root dir. */
2661 in_repo_path = strdup(worktree->path_prefix);
2662 if (in_repo_path == NULL) {
2663 err = got_error_from_errno("strdup");
2666 *tree_relpath = strdup("");
2667 if (*tree_relpath == NULL) {
2668 err = got_error_from_errno("strdup");
2672 /* Check out a single file in a subdirectory. */
2673 err = got_path_dirname(tree_relpath, wt_relpath);
2676 if (asprintf(&in_repo_path, "%s%s%s",
2677 worktree->path_prefix, is_root_wt ? "" : "/",
2678 *tree_relpath) == -1) {
2679 err = got_error_from_errno("asprintf");
2683 err = got_object_id_by_path(tree_id, repo,
2684 base_commit, in_repo_path);
2686 /* Check out all files within a subdirectory. */
2687 *tree_id = got_object_id_dup(id);
2688 if (*tree_id == NULL) {
2689 err = got_error_from_errno("got_object_id_dup");
2692 *tree_relpath = strdup(wt_relpath);
2693 if (*tree_relpath == NULL) {
2694 err = got_error_from_errno("strdup");
2702 *entry_type = GOT_OBJ_TYPE_ANY;
2703 free(*tree_relpath);
2704 *tree_relpath = NULL;
2711 static const struct got_error *
2712 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2713 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2714 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2715 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2717 const struct got_error *err = NULL;
2718 struct got_commit_object *commit = NULL;
2719 struct got_tree_object *tree = NULL;
2720 struct got_fileindex_diff_tree_cb diff_cb;
2721 struct diff_cb_arg arg;
2723 err = ref_base_commit(worktree, repo);
2725 if (!(err->code == GOT_ERR_ERRNO &&
2726 (errno == EACCES || errno == EROFS)))
2728 err = (*progress_cb)(progress_arg,
2729 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2734 err = got_object_open_as_commit(&commit, repo,
2735 worktree->base_commit_id);
2739 err = got_object_open_as_tree(&tree, repo, tree_id);
2744 got_object_tree_find_entry(tree, entry_name) == NULL) {
2745 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2749 diff_cb.diff_old_new = diff_old_new;
2750 diff_cb.diff_old = diff_old;
2751 diff_cb.diff_new = diff_new;
2752 arg.fileindex = fileindex;
2753 arg.worktree = worktree;
2755 arg.progress_cb = progress_cb;
2756 arg.progress_arg = progress_arg;
2757 arg.cancel_cb = cancel_cb;
2758 arg.cancel_arg = cancel_arg;
2759 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2760 entry_name, repo, &diff_cb, &arg);
2763 got_object_tree_close(tree);
2765 got_object_commit_close(commit);
2769 const struct got_error *
2770 got_worktree_checkout_files(struct got_worktree *worktree,
2771 struct got_pathlist_head *paths, struct got_repository *repo,
2772 got_worktree_checkout_cb progress_cb, void *progress_arg,
2773 got_cancel_cb cancel_cb, void *cancel_arg)
2775 const struct got_error *err = NULL, *sync_err, *unlockerr;
2776 struct got_commit_object *commit = NULL;
2777 struct got_tree_object *tree = NULL;
2778 struct got_fileindex *fileindex = NULL;
2779 char *fileindex_path = NULL;
2780 struct got_pathlist_entry *pe;
2781 struct tree_path_data {
2782 STAILQ_ENTRY(tree_path_data) entry;
2783 struct got_object_id *tree_id;
2788 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2790 STAILQ_INIT(&tree_paths);
2792 err = lock_worktree(worktree, LOCK_EX);
2796 err = got_object_open_as_commit(&commit, repo,
2797 worktree->base_commit_id);
2801 /* Map all specified paths to in-repository trees. */
2802 TAILQ_FOREACH(pe, paths, entry) {
2803 tpd = malloc(sizeof(*tpd));
2805 err = got_error_from_errno("malloc");
2809 err = find_tree_entry_for_checkout(&tpd->entry_type,
2810 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2817 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2818 err = got_path_basename(&tpd->entry_name, pe->path);
2826 tpd->entry_name = NULL;
2828 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2832 * Read the file index.
2833 * Checking out files is supposed to be an idempotent operation.
2834 * If the on-disk file index is incomplete we will try to complete it.
2836 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2840 tpd = STAILQ_FIRST(&tree_paths);
2841 TAILQ_FOREACH(pe, paths, entry) {
2842 struct bump_base_commit_id_arg bbc_arg;
2844 err = checkout_files(worktree, fileindex, tpd->relpath,
2845 tpd->tree_id, tpd->entry_name, repo,
2846 progress_cb, progress_arg, cancel_cb, cancel_arg);
2850 bbc_arg.base_commit_id = worktree->base_commit_id;
2851 bbc_arg.entry_name = tpd->entry_name;
2852 bbc_arg.path = pe->path;
2853 bbc_arg.path_len = pe->path_len;
2854 bbc_arg.progress_cb = progress_cb;
2855 bbc_arg.progress_arg = progress_arg;
2856 err = got_fileindex_for_each_entry_safe(fileindex,
2857 bump_base_commit_id, &bbc_arg);
2861 tpd = STAILQ_NEXT(tpd, entry);
2863 sync_err = sync_fileindex(fileindex, fileindex_path);
2864 if (sync_err && err == NULL)
2867 free(fileindex_path);
2869 got_object_tree_close(tree);
2871 got_object_commit_close(commit);
2873 got_fileindex_free(fileindex);
2874 while (!STAILQ_EMPTY(&tree_paths)) {
2875 tpd = STAILQ_FIRST(&tree_paths);
2876 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2881 unlockerr = lock_worktree(worktree, LOCK_SH);
2882 if (unlockerr && err == NULL)
2887 static const struct got_error *
2888 add_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2889 struct got_fileindex_entry *ie, const char *ondisk_path,
2890 const char *path2, struct got_blob_object *blob2, mode_t mode2,
2891 int restoring_missing_file, int reverting_versioned_file,
2892 int path_is_unversioned, int allow_bad_symlinks,
2893 struct got_repository *repo,
2894 got_worktree_checkout_cb progress_cb, void *progress_arg)
2896 const struct got_error *err = NULL;
2897 int is_bad_symlink = 0;
2899 if (S_ISLNK(mode2)) {
2900 err = install_symlink(&is_bad_symlink,
2901 worktree, ondisk_path, path2, blob2,
2902 restoring_missing_file,
2903 reverting_versioned_file,
2904 path_is_unversioned, allow_bad_symlinks,
2905 repo, progress_cb, progress_arg);
2907 err = install_blob(worktree, ondisk_path, path2,
2908 mode2, GOT_DEFAULT_FILE_MODE, blob2,
2909 restoring_missing_file, reverting_versioned_file, 0,
2910 path_is_unversioned, repo, progress_cb, progress_arg);
2915 /* Adding an unversioned file. */
2916 err = got_fileindex_entry_alloc(&ie, path2);
2919 err = got_fileindex_entry_update(ie,
2920 worktree->root_fd, path2, NULL, NULL, 1);
2922 got_fileindex_entry_free(ie);
2925 err = got_fileindex_entry_add(fileindex, ie);
2927 got_fileindex_entry_free(ie);
2931 /* Re-adding a locally deleted file. */
2932 err = got_fileindex_entry_update(ie,
2933 worktree->root_fd, path2, ie->blob_sha1,
2934 worktree->base_commit_id->sha1, 0);
2939 if (is_bad_symlink) {
2940 got_fileindex_entry_filetype_set(ie,
2941 GOT_FILEIDX_MODE_BAD_SYMLINK);
2947 struct merge_file_cb_arg {
2948 struct got_worktree *worktree;
2949 struct got_fileindex *fileindex;
2950 got_worktree_checkout_cb progress_cb;
2952 got_cancel_cb cancel_cb;
2954 const char *label_orig;
2955 struct got_object_id *commit_id2;
2956 int allow_bad_symlinks;
2959 static const struct got_error *
2960 merge_file_cb(void *arg, struct got_blob_object *blob1,
2961 struct got_blob_object *blob2, FILE *f1, FILE *f2,
2962 struct got_object_id *id1, struct got_object_id *id2,
2963 const char *path1, const char *path2,
2964 mode_t mode1, mode_t mode2, struct got_repository *repo)
2966 static const struct got_error *err = NULL;
2967 struct merge_file_cb_arg *a = arg;
2968 struct got_fileindex_entry *ie;
2969 char *ondisk_path = NULL;
2971 unsigned char status;
2972 int local_changes_subsumed;
2973 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2974 char *id_str = NULL, *label_deriv2 = NULL;
2976 if (blob1 && blob2) {
2977 ie = got_fileindex_entry_get(a->fileindex, path2,
2980 return (*a->progress_cb)(a->progress_arg,
2981 GOT_STATUS_MISSING, path2);
2983 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2985 return got_error_from_errno("asprintf");
2987 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2992 if (status == GOT_STATUS_DELETE) {
2993 err = (*a->progress_cb)(a->progress_arg,
2994 GOT_STATUS_MERGE, path2);
2997 if (status != GOT_STATUS_NO_CHANGE &&
2998 status != GOT_STATUS_MODIFY &&
2999 status != GOT_STATUS_CONFLICT &&
3000 status != GOT_STATUS_ADD) {
3001 err = (*a->progress_cb)(a->progress_arg, status, path2);
3005 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
3007 err = got_object_blob_read_to_str(&link_target2, blob2);
3010 err = merge_symlink(a->worktree, blob1, ondisk_path,
3011 path2, a->label_orig, link_target2, a->commit_id2,
3012 repo, a->progress_cb, a->progress_arg);
3017 f_orig = got_opentemp();
3018 if (f_orig == NULL) {
3019 err = got_error_from_errno("got_opentemp");
3022 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3027 f_deriv2 = got_opentemp();
3028 if (f_deriv2 == NULL)
3030 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3035 fd = open(ondisk_path,
3036 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3038 err = got_error_from_errno2("open",
3042 f_deriv = fdopen(fd, "r");
3043 if (f_deriv == NULL) {
3044 err = got_error_from_errno2("fdopen",
3049 err = got_object_id_str(&id_str, a->commit_id2);
3052 if (asprintf(&label_deriv2, "%s: commit %s",
3053 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
3054 err = got_error_from_errno("asprintf");
3057 err = merge_file(&local_changes_subsumed, a->worktree,
3058 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
3059 mode2, a->label_orig, NULL, label_deriv2,
3060 GOT_DIFF_ALGORITHM_PATIENCE, repo,
3061 a->progress_cb, a->progress_arg);
3064 ie = got_fileindex_entry_get(a->fileindex, path1,
3067 return (*a->progress_cb)(a->progress_arg,
3068 GOT_STATUS_MISSING, path1);
3070 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3072 return got_error_from_errno("asprintf");
3074 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3080 case GOT_STATUS_NO_CHANGE:
3081 err = (*a->progress_cb)(a->progress_arg,
3082 GOT_STATUS_DELETE, path1);
3085 err = remove_ondisk_file(a->worktree->root_path, path1);
3089 got_fileindex_entry_mark_deleted_from_disk(ie);
3091 case GOT_STATUS_DELETE:
3092 case GOT_STATUS_MISSING:
3093 err = (*a->progress_cb)(a->progress_arg,
3094 GOT_STATUS_DELETE, path1);
3098 got_fileindex_entry_mark_deleted_from_disk(ie);
3100 case GOT_STATUS_ADD: {
3101 struct got_object_id *id;
3105 * Delete the added file only if its content already
3106 * exists in the repository.
3108 err = got_object_blob_file_create(&id, &blob1_f,
3109 &blob1_size, path1);
3112 if (got_object_id_cmp(id, id1) == 0) {
3113 err = (*a->progress_cb)(a->progress_arg,
3114 GOT_STATUS_DELETE, path1);
3117 err = remove_ondisk_file(a->worktree->root_path,
3122 got_fileindex_entry_remove(a->fileindex,
3125 err = (*a->progress_cb)(a->progress_arg,
3126 GOT_STATUS_CANNOT_DELETE, path1);
3128 if (fclose(blob1_f) == EOF && err == NULL)
3129 err = got_error_from_errno("fclose");
3135 case GOT_STATUS_MODIFY:
3136 case GOT_STATUS_CONFLICT:
3137 err = (*a->progress_cb)(a->progress_arg,
3138 GOT_STATUS_CANNOT_DELETE, path1);
3142 case GOT_STATUS_OBSTRUCTED:
3143 err = (*a->progress_cb)(a->progress_arg, status, path1);
3151 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3153 return got_error_from_errno("asprintf");
3154 ie = got_fileindex_entry_get(a->fileindex, path2,
3157 err = get_file_status(&status, &sb, ie, ondisk_path,
3161 if (status != GOT_STATUS_NO_CHANGE &&
3162 status != GOT_STATUS_MODIFY &&
3163 status != GOT_STATUS_CONFLICT &&
3164 status != GOT_STATUS_ADD &&
3165 status != GOT_STATUS_DELETE) {
3166 err = (*a->progress_cb)(a->progress_arg,
3170 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3172 err = got_object_blob_read_to_str(&link_target2,
3176 err = merge_symlink(a->worktree, NULL,
3177 ondisk_path, path2, a->label_orig,
3178 link_target2, a->commit_id2, repo,
3179 a->progress_cb, a->progress_arg);
3181 } else if (S_ISREG(sb.st_mode)) {
3182 err = merge_blob(&local_changes_subsumed,
3183 a->worktree, NULL, ondisk_path, path2,
3184 sb.st_mode, a->label_orig, blob2,
3185 a->commit_id2, repo, a->progress_cb,
3187 } else if (status != GOT_STATUS_DELETE) {
3188 err = got_error_path(ondisk_path,
3189 GOT_ERR_FILE_OBSTRUCTED);
3193 if (status == GOT_STATUS_DELETE) {
3194 /* Re-add file with content from new blob. */
3195 err = add_file(a->worktree, a->fileindex, ie,
3196 ondisk_path, path2, blob2, mode2,
3197 0, 0, 0, a->allow_bad_symlinks,
3198 repo, a->progress_cb, a->progress_arg);
3203 err = add_file(a->worktree, a->fileindex, NULL,
3204 ondisk_path, path2, blob2, mode2,
3205 0, 0, 1, a->allow_bad_symlinks,
3206 repo, a->progress_cb, a->progress_arg);
3212 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3213 err = got_error_from_errno("fclose");
3214 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3215 err = got_error_from_errno("fclose");
3216 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3217 err = got_error_from_errno("fclose");
3224 static const struct got_error *
3225 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3227 struct got_worktree *worktree = arg;
3229 /* Reject merges into a work tree with mixed base commits. */
3230 if (got_fileindex_entry_has_commit(ie) &&
3231 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3232 SHA1_DIGEST_LENGTH) != 0)
3233 return got_error(GOT_ERR_MIXED_COMMITS);
3238 struct check_merge_conflicts_arg {
3239 struct got_worktree *worktree;
3240 struct got_fileindex *fileindex;
3241 struct got_repository *repo;
3244 static const struct got_error *
3245 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3246 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3247 struct got_object_id *id1, struct got_object_id *id2,
3248 const char *path1, const char *path2,
3249 mode_t mode1, mode_t mode2, struct got_repository *repo)
3251 const struct got_error *err = NULL;
3252 struct check_merge_conflicts_arg *a = arg;
3253 unsigned char status;
3255 struct got_fileindex_entry *ie;
3256 const char *path = path2 ? path2 : path1;
3257 struct got_object_id *id = id2 ? id2 : id1;
3263 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3267 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3269 return got_error_from_errno("asprintf");
3271 /* Reject merges into a work tree with conflicted files. */
3272 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3276 if (status == GOT_STATUS_CONFLICT)
3277 return got_error(GOT_ERR_CONFLICTS);
3282 static const struct got_error *
3283 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3284 const char *fileindex_path, struct got_object_id *commit_id1,
3285 struct got_object_id *commit_id2, struct got_repository *repo,
3286 got_worktree_checkout_cb progress_cb, void *progress_arg,
3287 got_cancel_cb cancel_cb, void *cancel_arg)
3289 const struct got_error *err = NULL, *sync_err;
3290 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3291 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3292 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3293 struct check_merge_conflicts_arg cmc_arg;
3294 struct merge_file_cb_arg arg;
3295 char *label_orig = NULL;
3296 FILE *f1 = NULL, *f2 = NULL;
3297 int fd1 = -1, fd2 = -1;
3300 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3303 err = got_object_id_by_path(&tree_id1, repo, commit1,
3304 worktree->path_prefix);
3305 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3311 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3315 err = got_object_id_str(&id_str, commit_id1);
3319 if (asprintf(&label_orig, "%s: commit %s",
3320 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3321 err = got_error_from_errno("asprintf");
3327 f1 = got_opentemp();
3329 err = got_error_from_errno("got_opentemp");
3334 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3338 err = got_object_id_by_path(&tree_id2, repo, commit2,
3339 worktree->path_prefix);
3343 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3347 f2 = got_opentemp();
3349 err = got_error_from_errno("got_opentemp");
3353 fd1 = got_opentempfd();
3355 err = got_error_from_errno("got_opentempfd");
3359 fd2 = got_opentempfd();
3361 err = got_error_from_errno("got_opentempfd");
3365 cmc_arg.worktree = worktree;
3366 cmc_arg.fileindex = fileindex;
3367 cmc_arg.repo = repo;
3368 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3369 check_merge_conflicts, &cmc_arg, 0);
3373 arg.worktree = worktree;
3374 arg.fileindex = fileindex;
3375 arg.progress_cb = progress_cb;
3376 arg.progress_arg = progress_arg;
3377 arg.cancel_cb = cancel_cb;
3378 arg.cancel_arg = cancel_arg;
3379 arg.label_orig = label_orig;
3380 arg.commit_id2 = commit_id2;
3381 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3382 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3383 merge_file_cb, &arg, 1);
3384 sync_err = sync_fileindex(fileindex, fileindex_path);
3385 if (sync_err && err == NULL)
3389 got_object_commit_close(commit1);
3391 got_object_commit_close(commit2);
3393 got_object_tree_close(tree1);
3395 got_object_tree_close(tree2);
3396 if (f1 && fclose(f1) == EOF && err == NULL)
3397 err = got_error_from_errno("fclose");
3398 if (f2 && fclose(f2) == EOF && err == NULL)
3399 err = got_error_from_errno("fclose");
3400 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3401 err = got_error_from_errno("close");
3402 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3403 err = got_error_from_errno("close");
3408 const struct got_error *
3409 got_worktree_merge_files(struct got_worktree *worktree,
3410 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3411 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3412 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3414 const struct got_error *err, *unlockerr;
3415 char *fileindex_path = NULL;
3416 struct got_fileindex *fileindex = NULL;
3418 err = lock_worktree(worktree, LOCK_EX);
3422 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3426 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3431 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3432 commit_id2, repo, progress_cb, progress_arg,
3433 cancel_cb, cancel_arg);
3436 got_fileindex_free(fileindex);
3437 free(fileindex_path);
3438 unlockerr = lock_worktree(worktree, LOCK_SH);
3439 if (unlockerr && err == NULL)
3444 struct diff_dir_cb_arg {
3445 struct got_fileindex *fileindex;
3446 struct got_worktree *worktree;
3447 const char *status_path;
3448 size_t status_path_len;
3449 struct got_repository *repo;
3450 got_worktree_status_cb status_cb;
3452 got_cancel_cb cancel_cb;
3454 /* A pathlist containing per-directory pathlists of ignore patterns. */
3455 struct got_pathlist_head *ignores;
3456 int report_unchanged;
3460 static const struct got_error *
3461 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3462 int dirfd, const char *de_name,
3463 got_worktree_status_cb status_cb, void *status_arg,
3464 struct got_repository *repo, int report_unchanged)
3466 const struct got_error *err = NULL;
3467 unsigned char status = GOT_STATUS_NO_CHANGE;
3468 unsigned char staged_status;
3470 struct got_object_id blob_id, commit_id, staged_blob_id;
3471 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3472 struct got_object_id *staged_blob_idp = NULL;
3474 staged_status = get_staged_status(ie);
3475 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3479 if (status == GOT_STATUS_NO_CHANGE &&
3480 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3483 if (got_fileindex_entry_has_blob(ie))
3484 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3485 if (got_fileindex_entry_has_commit(ie))
3486 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3487 if (staged_status == GOT_STATUS_ADD ||
3488 staged_status == GOT_STATUS_MODIFY) {
3489 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3490 &staged_blob_id, ie);
3493 return (*status_cb)(status_arg, status, staged_status,
3494 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3497 static const struct got_error *
3498 status_old_new(void *arg, struct got_fileindex_entry *ie,
3499 struct dirent *de, const char *parent_path, int dirfd)
3501 const struct got_error *err = NULL;
3502 struct diff_dir_cb_arg *a = arg;
3505 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3506 return got_error(GOT_ERR_CANCELLED);
3508 if (got_path_cmp(parent_path, a->status_path,
3509 strlen(parent_path), a->status_path_len) != 0 &&
3510 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3513 if (parent_path[0]) {
3514 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3515 parent_path, de->d_name) == -1)
3516 return got_error_from_errno("asprintf");
3518 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3520 return got_error_from_errno("asprintf");
3523 err = report_file_status(ie, abspath, dirfd, de->d_name,
3524 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3529 static const struct got_error *
3530 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3532 struct diff_dir_cb_arg *a = arg;
3533 struct got_object_id blob_id, commit_id;
3534 unsigned char status;
3536 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3537 return got_error(GOT_ERR_CANCELLED);
3539 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3542 got_fileindex_entry_get_blob_id(&blob_id, ie);
3543 got_fileindex_entry_get_commit_id(&commit_id, ie);
3544 if (got_fileindex_entry_has_file_on_disk(ie))
3545 status = GOT_STATUS_MISSING;
3547 status = GOT_STATUS_DELETE;
3548 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3549 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3553 free_ignores(struct got_pathlist_head *ignores)
3555 struct got_pathlist_entry *pe;
3557 TAILQ_FOREACH(pe, ignores, entry) {
3558 struct got_pathlist_head *ignorelist = pe->data;
3560 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3562 got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3565 static const struct got_error *
3566 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3568 const struct got_error *err = NULL;
3569 struct got_pathlist_entry *pe = NULL;
3570 struct got_pathlist_head *ignorelist;
3571 char *line = NULL, *pattern, *dirpath = NULL;
3572 size_t linesize = 0;
3575 ignorelist = calloc(1, sizeof(*ignorelist));
3576 if (ignorelist == NULL)
3577 return got_error_from_errno("calloc");
3578 TAILQ_INIT(ignorelist);
3580 while ((linelen = getline(&line, &linesize, f)) != -1) {
3581 if (linelen > 0 && line[linelen - 1] == '\n')
3582 line[linelen - 1] = '\0';
3584 /* Git's ignores may contain comments. */
3588 /* Git's negated patterns are not (yet?) supported. */
3592 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3594 err = got_error_from_errno("asprintf");
3597 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3602 err = got_error_from_errno("getline");
3606 dirpath = strdup(path);
3607 if (dirpath == NULL) {
3608 err = got_error_from_errno("strdup");
3611 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3614 if (err || pe == NULL) {
3616 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3622 match_path(const char *pattern, size_t pattern_len, const char *path,
3628 * Trailing slashes signify directories.
3629 * Append a * to make such patterns conform to fnmatch rules.
3631 if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
3632 if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
3633 return FNM_NOMATCH; /* XXX */
3635 return fnmatch(buf, path, flags);
3638 return fnmatch(pattern, path, flags);
3642 match_ignores(struct got_pathlist_head *ignores, const char *path)
3644 struct got_pathlist_entry *pe;
3646 /* Handle patterns which match in all directories. */
3647 TAILQ_FOREACH(pe, ignores, entry) {
3648 struct got_pathlist_head *ignorelist = pe->data;
3649 struct got_pathlist_entry *pi;
3651 TAILQ_FOREACH(pi, ignorelist, entry) {
3654 if (pi->path_len < 3 ||
3655 strncmp(pi->path, "**/", 3) != 0)
3659 if (match_path(pi->path + 3,
3660 pi->path_len - 3, p,
3661 FNM_PATHNAME | FNM_LEADING_DIR)) {
3662 /* Retry in next directory. */
3663 while (*p && *p != '/')
3675 * The ignores pathlist contains ignore lists from children before
3676 * parents, so we can find the most specific ignorelist by walking
3677 * ignores backwards.
3679 pe = TAILQ_LAST(ignores, got_pathlist_head);
3681 if (got_path_is_child(path, pe->path, pe->path_len)) {
3682 struct got_pathlist_head *ignorelist = pe->data;
3683 struct got_pathlist_entry *pi;
3684 TAILQ_FOREACH(pi, ignorelist, entry) {
3685 int flags = FNM_LEADING_DIR;
3686 if (strstr(pi->path, "/**/") == NULL)
3687 flags |= FNM_PATHNAME;
3688 if (match_path(pi->path, pi->path_len,
3694 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3700 static const struct got_error *
3701 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3702 const char *path, int dirfd, const char *ignores_filename)
3704 const struct got_error *err = NULL;
3707 FILE *ignoresfile = NULL;
3709 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3710 path[0] ? "/" : "", ignores_filename) == -1)
3711 return got_error_from_errno("asprintf");
3714 fd = openat(dirfd, ignores_filename,
3715 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3717 if (errno != ENOENT && errno != EACCES)
3718 err = got_error_from_errno2("openat",
3721 ignoresfile = fdopen(fd, "r");
3722 if (ignoresfile == NULL)
3723 err = got_error_from_errno2("fdopen",
3727 err = read_ignores(ignores, path, ignoresfile);
3731 ignoresfile = fopen(ignorespath, "re");
3732 if (ignoresfile == NULL) {
3733 if (errno != ENOENT && errno != EACCES)
3734 err = got_error_from_errno2("fopen",
3737 err = read_ignores(ignores, path, ignoresfile);
3740 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3741 err = got_error_from_errno2("fclose", path);
3742 if (fd != -1 && close(fd) == -1 && err == NULL)
3743 err = got_error_from_errno2("close", path);
3748 static const struct got_error *
3749 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3752 const struct got_error *err = NULL;
3753 struct diff_dir_cb_arg *a = arg;
3759 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3760 return got_error(GOT_ERR_CANCELLED);
3762 if (parent_path[0]) {
3763 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3764 return got_error_from_errno("asprintf");
3769 if (de->d_type == DT_DIR) {
3770 if (!a->no_ignores && ignore != NULL &&
3771 match_ignores(a->ignores, path))
3773 } else if (!match_ignores(a->ignores, path) &&
3774 got_path_is_child(path, a->status_path, a->status_path_len))
3775 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3776 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3782 static const struct got_error *
3783 status_traverse(void *arg, const char *path, int dirfd)
3785 const struct got_error *err = NULL;
3786 struct diff_dir_cb_arg *a = arg;
3791 err = add_ignores(a->ignores, a->worktree->root_path,
3792 path, dirfd, ".cvsignore");
3796 err = add_ignores(a->ignores, a->worktree->root_path, path,
3797 dirfd, ".gitignore");
3802 static const struct got_error *
3803 report_single_file_status(const char *path, const char *ondisk_path,
3804 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3805 void *status_arg, struct got_repository *repo, int report_unchanged,
3806 struct got_pathlist_head *ignores, int no_ignores)
3808 struct got_fileindex_entry *ie;
3811 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3813 return report_file_status(ie, ondisk_path, -1, NULL,
3814 status_cb, status_arg, repo, report_unchanged);
3816 if (lstat(ondisk_path, &sb) == -1) {
3817 if (errno != ENOENT)
3818 return got_error_from_errno2("lstat", ondisk_path);
3819 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3820 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3823 if (!no_ignores && match_ignores(ignores, path))
3826 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3827 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3828 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3833 static const struct got_error *
3834 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3835 const char *root_path, const char *path)
3837 const struct got_error *err;
3838 char *parent_path, *next_parent_path = NULL;
3840 err = add_ignores(ignores, root_path, "", -1,
3845 err = add_ignores(ignores, root_path, "", -1,
3850 err = got_path_dirname(&parent_path, path);
3852 if (err->code == GOT_ERR_BAD_PATH)
3853 return NULL; /* cannot traverse parent */
3857 err = add_ignores(ignores, root_path, parent_path, -1,
3861 err = add_ignores(ignores, root_path, parent_path, -1,
3865 err = got_path_dirname(&next_parent_path, parent_path);
3867 if (err->code == GOT_ERR_BAD_PATH)
3868 err = NULL; /* traversed everything */
3871 if (got_path_is_root_dir(parent_path))
3874 parent_path = next_parent_path;
3875 next_parent_path = NULL;
3879 free(next_parent_path);
3883 struct find_missing_children_args {
3884 const char *parent_path;
3886 struct got_pathlist_head *children;
3887 got_cancel_cb cancel_cb;
3891 static const struct got_error *
3892 find_missing_children(void *arg, struct got_fileindex_entry *ie)
3894 const struct got_error *err = NULL;
3895 struct find_missing_children_args *a = arg;
3898 err = a->cancel_cb(a->cancel_arg);
3903 if (got_path_is_child(ie->path, a->parent_path, a->parent_len))
3904 err = got_pathlist_append(a->children, ie->path, NULL);
3909 static const struct got_error *
3910 report_children(struct got_pathlist_head *children,
3911 struct got_worktree *worktree, struct got_fileindex *fileindex,
3912 struct got_repository *repo, int is_root_dir, int report_unchanged,
3913 struct got_pathlist_head *ignores, int no_ignores,
3914 got_worktree_status_cb status_cb, void *status_arg,
3915 got_cancel_cb cancel_cb, void *cancel_arg)
3917 const struct got_error *err = NULL;
3918 struct got_pathlist_entry *pe;
3919 char *ondisk_path = NULL;
3921 TAILQ_FOREACH(pe, children, entry) {
3923 err = cancel_cb(cancel_arg);
3928 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
3929 !is_root_dir ? "/" : "", pe->path) == -1) {
3930 err = got_error_from_errno("asprintf");
3935 err = report_single_file_status(pe->path, ondisk_path,
3936 fileindex, status_cb, status_arg, repo, report_unchanged,
3937 ignores, no_ignores);
3949 static const struct got_error *
3950 worktree_status(struct got_worktree *worktree, const char *path,
3951 struct got_fileindex *fileindex, struct got_repository *repo,
3952 got_worktree_status_cb status_cb, void *status_arg,
3953 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3954 int report_unchanged)
3956 const struct got_error *err = NULL;
3958 struct got_fileindex_diff_dir_cb fdiff_cb;
3959 struct diff_dir_cb_arg arg;
3960 char *ondisk_path = NULL;
3961 struct got_pathlist_head ignores, missing_children;
3962 struct got_fileindex_entry *ie;
3964 TAILQ_INIT(&ignores);
3965 TAILQ_INIT(&missing_children);
3967 if (asprintf(&ondisk_path, "%s%s%s",
3968 worktree->root_path, path[0] ? "/" : "", path) == -1)
3969 return got_error_from_errno("asprintf");
3971 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3973 err = report_single_file_status(path, ondisk_path,
3974 fileindex, status_cb, status_arg, repo,
3975 report_unchanged, &ignores, no_ignores);
3978 struct find_missing_children_args fmca;
3979 fmca.parent_path = path;
3980 fmca.parent_len = strlen(path);
3981 fmca.children = &missing_children;
3982 fmca.cancel_cb = cancel_cb;
3983 fmca.cancel_arg = cancel_arg;
3984 err = got_fileindex_for_each_entry_safe(fileindex,
3985 find_missing_children, &fmca);
3990 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3992 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3993 !got_err_open_nofollow_on_symlink())
3994 err = got_error_from_errno2("open", ondisk_path);
3997 err = add_ignores_from_parent_paths(&ignores,
3998 worktree->root_path, ondisk_path);
4002 if (TAILQ_EMPTY(&missing_children)) {
4003 err = report_single_file_status(path,
4004 ondisk_path, fileindex,
4005 status_cb, status_arg, repo,
4006 report_unchanged, &ignores, no_ignores);
4010 err = report_children(&missing_children,
4011 worktree, fileindex, repo,
4012 (path[0] == '\0'), report_unchanged,
4013 &ignores, no_ignores,
4014 status_cb, status_arg,
4015 cancel_cb, cancel_arg);
4021 fdiff_cb.diff_old_new = status_old_new;
4022 fdiff_cb.diff_old = status_old;
4023 fdiff_cb.diff_new = status_new;
4024 fdiff_cb.diff_traverse = status_traverse;
4025 arg.fileindex = fileindex;
4026 arg.worktree = worktree;
4027 arg.status_path = path;
4028 arg.status_path_len = strlen(path);
4030 arg.status_cb = status_cb;
4031 arg.status_arg = status_arg;
4032 arg.cancel_cb = cancel_cb;
4033 arg.cancel_arg = cancel_arg;
4034 arg.report_unchanged = report_unchanged;
4035 arg.no_ignores = no_ignores;
4037 err = add_ignores_from_parent_paths(&ignores,
4038 worktree->root_path, path);
4042 arg.ignores = &ignores;
4043 err = got_fileindex_diff_dir(fileindex, fd,
4044 worktree->root_path, path, repo, &fdiff_cb, &arg);
4047 free_ignores(&ignores);
4048 if (fd != -1 && close(fd) == -1 && err == NULL)
4049 err = got_error_from_errno("close");
4054 const struct got_error *
4055 got_worktree_status(struct got_worktree *worktree,
4056 struct got_pathlist_head *paths, struct got_repository *repo,
4057 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
4058 got_cancel_cb cancel_cb, void *cancel_arg)
4060 const struct got_error *err = NULL;
4061 char *fileindex_path = NULL;
4062 struct got_fileindex *fileindex = NULL;
4063 struct got_pathlist_entry *pe;
4065 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4069 TAILQ_FOREACH(pe, paths, entry) {
4070 err = worktree_status(worktree, pe->path, fileindex, repo,
4071 status_cb, status_arg, cancel_cb, cancel_arg,
4076 free(fileindex_path);
4077 got_fileindex_free(fileindex);
4081 const struct got_error *
4082 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
4085 const struct got_error *err = NULL;
4086 char *resolved = NULL, *cwd = NULL, *path = NULL;
4089 char *abspath = NULL;
4090 char canonpath[PATH_MAX];
4094 cwd = getcwd(NULL, 0);
4096 return got_error_from_errno("getcwd");
4098 if (lstat(arg, &sb) == -1) {
4099 if (errno != ENOENT) {
4100 err = got_error_from_errno2("lstat", arg);
4105 if (S_ISLNK(sb.st_mode)) {
4107 * We cannot use realpath(3) with symlinks since we want to
4108 * operate on the symlink itself.
4109 * But we can make the path absolute, assuming it is relative
4110 * to the current working directory, and then canonicalize it.
4112 if (!got_path_is_absolute(arg)) {
4113 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4114 err = got_error_from_errno("asprintf");
4119 err = got_canonpath(abspath ? abspath : arg, canonpath,
4123 resolved = strdup(canonpath);
4124 if (resolved == NULL) {
4125 err = got_error_from_errno("strdup");
4129 resolved = realpath(arg, NULL);
4130 if (resolved == NULL) {
4131 if (errno != ENOENT) {
4132 err = got_error_from_errno2("realpath", arg);
4135 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4136 err = got_error_from_errno("asprintf");
4139 err = got_canonpath(abspath, canonpath,
4143 resolved = strdup(canonpath);
4144 if (resolved == NULL) {
4145 err = got_error_from_errno("strdup");
4151 if (strncmp(got_worktree_get_root_path(worktree), resolved,
4152 strlen(got_worktree_get_root_path(worktree)))) {
4153 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
4157 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
4158 err = got_path_skip_common_ancestor(&path,
4159 got_worktree_get_root_path(worktree), resolved);
4165 err = got_error_from_errno("strdup");
4170 /* XXX status walk can't deal with trailing slash! */
4172 while (len > 0 && path[len - 1] == '/') {
4173 path[len - 1] = '\0';
4187 struct schedule_addition_args {
4188 struct got_worktree *worktree;
4189 struct got_fileindex *fileindex;
4190 got_worktree_checkout_cb progress_cb;
4192 struct got_repository *repo;
4196 add_noop_status(unsigned char status)
4198 return (status == GOT_STATUS_ADD ||
4199 status == GOT_STATUS_MODIFY ||
4200 status == GOT_STATUS_CONFLICT ||
4201 status == GOT_STATUS_MODE_CHANGE ||
4202 status == GOT_STATUS_NO_CHANGE);
4205 static const struct got_error *
4206 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4207 const char *relpath, struct got_object_id *blob_id,
4208 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4209 int dirfd, const char *de_name)
4211 struct schedule_addition_args *a = arg;
4212 const struct got_error *err = NULL;
4213 struct got_fileindex_entry *ie;
4217 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4219 return got_error_from_errno("asprintf");
4221 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4223 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4227 /* Re-adding an existing entry is a no-op. */
4228 if (staged_status == GOT_STATUS_NO_CHANGE &&
4229 add_noop_status(status))
4231 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4236 if (status != GOT_STATUS_UNVERSIONED) {
4237 if (status == GOT_STATUS_NONEXISTENT)
4238 err = got_error_set_errno(ENOENT, ondisk_path);
4240 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4244 err = got_fileindex_entry_alloc(&ie, relpath);
4247 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4248 relpath, NULL, NULL, 1);
4250 got_fileindex_entry_free(ie);
4253 err = got_fileindex_entry_add(a->fileindex, ie);
4255 got_fileindex_entry_free(ie);
4262 if (staged_status == GOT_STATUS_NO_CHANGE && add_noop_status(status))
4264 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4267 const struct got_error *
4268 got_worktree_schedule_add(struct got_worktree *worktree,
4269 struct got_pathlist_head *paths,
4270 got_worktree_checkout_cb progress_cb, void *progress_arg,
4271 struct got_repository *repo, int no_ignores)
4273 struct got_fileindex *fileindex = NULL;
4274 char *fileindex_path = NULL;
4275 const struct got_error *err = NULL, *sync_err, *unlockerr;
4276 struct got_pathlist_entry *pe;
4277 struct schedule_addition_args saa;
4279 err = lock_worktree(worktree, LOCK_EX);
4283 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4287 saa.worktree = worktree;
4288 saa.fileindex = fileindex;
4289 saa.progress_cb = progress_cb;
4290 saa.progress_arg = progress_arg;
4293 TAILQ_FOREACH(pe, paths, entry) {
4294 err = worktree_status(worktree, pe->path, fileindex, repo,
4295 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4299 sync_err = sync_fileindex(fileindex, fileindex_path);
4300 if (sync_err && err == NULL)
4303 free(fileindex_path);
4305 got_fileindex_free(fileindex);
4306 unlockerr = lock_worktree(worktree, LOCK_SH);
4307 if (unlockerr && err == NULL)
4312 struct schedule_deletion_args {
4313 struct got_worktree *worktree;
4314 struct got_fileindex *fileindex;
4315 got_worktree_delete_cb progress_cb;
4317 struct got_repository *repo;
4318 int delete_local_mods;
4320 int ignore_missing_paths;
4321 const char *status_path;
4322 size_t status_path_len;
4323 const char *status_codes;
4326 static const struct got_error *
4327 schedule_for_deletion(void *arg, unsigned char status,
4328 unsigned char staged_status, const char *relpath,
4329 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4330 struct got_object_id *commit_id, int dirfd, const char *de_name)
4332 struct schedule_deletion_args *a = arg;
4333 const struct got_error *err = NULL;
4334 struct got_fileindex_entry *ie = NULL;
4338 if (status == GOT_STATUS_NONEXISTENT) {
4339 if (a->ignore_missing_paths)
4341 return got_error_set_errno(ENOENT, relpath);
4344 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4346 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4348 staged_status = get_staged_status(ie);
4349 if (staged_status != GOT_STATUS_NO_CHANGE) {
4350 if (staged_status == GOT_STATUS_DELETE)
4352 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4355 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4357 return got_error_from_errno("asprintf");
4359 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4364 if (a->status_codes) {
4365 size_t ncodes = strlen(a->status_codes);
4367 for (i = 0; i < ncodes ; i++) {
4368 if (status == a->status_codes[i])
4372 /* Do not delete files in non-matching status. */
4376 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4377 a->status_codes[i] != GOT_STATUS_MISSING) {
4378 static char msg[64];
4379 snprintf(msg, sizeof(msg),
4380 "invalid status code '%c'", a->status_codes[i]);
4381 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4386 if (status != GOT_STATUS_NO_CHANGE) {
4387 if (status == GOT_STATUS_DELETE)
4389 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4390 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4393 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4394 err = got_error_set_errno(ENOENT, relpath);
4397 if (status != GOT_STATUS_MODIFY &&
4398 status != GOT_STATUS_MISSING) {
4399 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4404 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4408 if (unlinkat(dirfd, de_name, 0) == -1) {
4409 err = got_error_from_errno2("unlinkat",
4413 } else if (unlink(ondisk_path) == -1) {
4414 err = got_error_from_errno2("unlink", ondisk_path);
4418 root_len = strlen(a->worktree->root_path);
4422 err = got_path_dirname(&parent, ondisk_path);
4426 ondisk_path = parent;
4427 if (got_path_cmp(ondisk_path, a->status_path,
4428 strlen(ondisk_path), a->status_path_len) != 0 &&
4429 !got_path_is_child(ondisk_path, a->status_path,
4430 a->status_path_len))
4432 if (rmdir(ondisk_path) == -1) {
4433 if (errno != ENOTEMPTY)
4434 err = got_error_from_errno2("rmdir",
4438 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4439 strlen(ondisk_path), root_len) != 0);
4442 got_fileindex_entry_mark_deleted_from_disk(ie);
4447 if (status == GOT_STATUS_DELETE)
4449 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4450 staged_status, relpath);
4453 const struct got_error *
4454 got_worktree_schedule_delete(struct got_worktree *worktree,
4455 struct got_pathlist_head *paths, int delete_local_mods,
4456 const char *status_codes,
4457 got_worktree_delete_cb progress_cb, void *progress_arg,
4458 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4460 struct got_fileindex *fileindex = NULL;
4461 char *fileindex_path = NULL;
4462 const struct got_error *err = NULL, *sync_err, *unlockerr;
4463 struct got_pathlist_entry *pe;
4464 struct schedule_deletion_args sda;
4466 err = lock_worktree(worktree, LOCK_EX);
4470 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4474 sda.worktree = worktree;
4475 sda.fileindex = fileindex;
4476 sda.progress_cb = progress_cb;
4477 sda.progress_arg = progress_arg;
4479 sda.delete_local_mods = delete_local_mods;
4480 sda.keep_on_disk = keep_on_disk;
4481 sda.ignore_missing_paths = ignore_missing_paths;
4482 sda.status_codes = status_codes;
4484 TAILQ_FOREACH(pe, paths, entry) {
4485 char *ondisk_status_path;
4487 if (asprintf(&ondisk_status_path, "%s%s%s",
4488 got_worktree_get_root_path(worktree),
4489 pe->path[0] == '\0' ? "" : "/", pe->path) == -1) {
4490 err = got_error_from_errno("asprintf");
4493 sda.status_path = ondisk_status_path;
4494 sda.status_path_len = strlen(ondisk_status_path);
4495 err = worktree_status(worktree, pe->path, fileindex, repo,
4496 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4497 free(ondisk_status_path);
4501 sync_err = sync_fileindex(fileindex, fileindex_path);
4502 if (sync_err && err == NULL)
4505 free(fileindex_path);
4507 got_fileindex_free(fileindex);
4508 unlockerr = lock_worktree(worktree, LOCK_SH);
4509 if (unlockerr && err == NULL)
4514 static const struct got_error *
4515 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4517 const struct got_error *err = NULL;
4519 size_t linesize = 0, n;
4522 linelen = getline(&line, &linesize, infile);
4523 if (linelen == -1) {
4524 if (ferror(infile)) {
4525 err = got_error_from_errno("getline");
4531 n = fwrite(line, 1, linelen, outfile);
4533 err = got_ferror(outfile, GOT_ERR_IO);
4538 n = fwrite(line, 1, linelen, rejectfile);
4540 err = got_ferror(rejectfile, GOT_ERR_IO);
4547 static const struct got_error *
4548 skip_one_line(FILE *f)
4551 size_t linesize = 0;
4554 linelen = getline(&line, &linesize, f);
4555 if (linelen == -1) {
4557 return got_error_from_errno("getline");
4564 static const struct got_error *
4565 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4566 int start_old, int end_old, int start_new, int end_new,
4567 FILE *outfile, FILE *rejectfile)
4569 const struct got_error *err;
4571 /* Copy old file's lines leading up to patch. */
4572 while (!feof(f1) && *line_cur1 < start_old) {
4573 err = copy_one_line(f1, outfile, NULL);
4578 /* Skip new file's lines leading up to patch. */
4579 while (!feof(f2) && *line_cur2 < start_new) {
4581 err = copy_one_line(f2, NULL, rejectfile);
4583 err = skip_one_line(f2);
4588 /* Copy patched lines. */
4589 while (!feof(f2) && *line_cur2 <= end_new) {
4590 err = copy_one_line(f2, outfile, NULL);
4595 /* Skip over old file's replaced lines. */
4596 while (!feof(f1) && *line_cur1 <= end_old) {
4598 err = copy_one_line(f1, NULL, rejectfile);
4600 err = skip_one_line(f1);
4609 static const struct got_error *
4610 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4611 FILE *outfile, FILE *rejectfile)
4613 const struct got_error *err;
4616 /* Copy old file's lines until EOF. */
4618 err = copy_one_line(f1, outfile, NULL);
4625 /* Copy new file's lines until EOF. */
4627 err = copy_one_line(f2, NULL, rejectfile);
4637 static const struct got_error *
4638 apply_or_reject_change(int *choice, int *nchunks_used,
4639 struct diff_result *diff_result, int n,
4640 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4641 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4642 got_worktree_patch_cb patch_cb, void *patch_arg)
4644 const struct got_error *err = NULL;
4645 struct diff_chunk_context cc = {};
4646 int start_old, end_old, start_new, end_new;
4648 struct diff_output_unidiff_state *diff_state;
4649 struct diff_input_info diff_info;
4652 *choice = GOT_PATCH_CHOICE_NONE;
4654 /* Get changed line numbers without context lines for copy_change(). */
4655 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4656 start_old = cc.left.start;
4657 end_old = cc.left.end;
4658 start_new = cc.right.start;
4659 end_new = cc.right.end;
4661 /* Get the same change with context lines for display. */
4662 memset(&cc, 0, sizeof(cc));
4663 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4665 memset(&diff_info, 0, sizeof(diff_info));
4666 diff_info.left_path = relpath;
4667 diff_info.right_path = relpath;
4669 diff_state = diff_output_unidiff_state_alloc();
4670 if (diff_state == NULL)
4671 return got_error_set_errno(ENOMEM,
4672 "diff_output_unidiff_state_alloc");
4674 hunkfile = got_opentemp();
4675 if (hunkfile == NULL) {
4676 err = got_error_from_errno("got_opentemp");
4680 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4682 if (rc != DIFF_RC_OK) {
4683 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4687 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4688 err = got_ferror(hunkfile, GOT_ERR_IO);
4692 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4693 hunkfile, changeno, nchanges);
4698 case GOT_PATCH_CHOICE_YES:
4699 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4700 end_old, start_new, end_new, outfile, rejectfile);
4702 case GOT_PATCH_CHOICE_NO:
4703 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4704 end_old, start_new, end_new, rejectfile, outfile);
4706 case GOT_PATCH_CHOICE_QUIT:
4709 err = got_error(GOT_ERR_PATCH_CHOICE);
4713 diff_output_unidiff_state_free(diff_state);
4714 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4715 err = got_error_from_errno("fclose");
4719 struct revert_file_args {
4720 struct got_worktree *worktree;
4721 struct got_fileindex *fileindex;
4722 got_worktree_checkout_cb progress_cb;
4724 got_worktree_patch_cb patch_cb;
4726 struct got_repository *repo;
4727 int unlink_added_files;
4728 struct got_pathlist_head *added_files_to_unlink;
4731 static const struct got_error *
4732 create_patched_content(char **path_outfile, int reverse_patch,
4733 struct got_object_id *blob_id, const char *path2,
4734 int dirfd2, const char *de_name2,
4735 const char *relpath, struct got_repository *repo,
4736 got_worktree_patch_cb patch_cb, void *patch_arg)
4738 const struct got_error *err, *free_err;
4739 struct got_blob_object *blob = NULL;
4740 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4741 int fd = -1, fd2 = -1;
4742 char link_target[PATH_MAX];
4743 ssize_t link_len = 0;
4744 char *path1 = NULL, *id_str = NULL;
4746 struct got_diffreg_result *diffreg_result = NULL;
4747 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4748 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4750 *path_outfile = NULL;
4752 err = got_object_id_str(&id_str, blob_id);
4757 fd2 = openat(dirfd2, de_name2,
4758 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4760 if (!got_err_open_nofollow_on_symlink()) {
4761 err = got_error_from_errno2("openat", path2);
4764 link_len = readlinkat(dirfd2, de_name2,
4765 link_target, sizeof(link_target));
4766 if (link_len == -1) {
4767 return got_error_from_errno2("readlinkat",
4770 sb2.st_mode = S_IFLNK;
4771 sb2.st_size = link_len;
4774 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4776 if (!got_err_open_nofollow_on_symlink()) {
4777 err = got_error_from_errno2("open", path2);
4780 link_len = readlink(path2, link_target,
4781 sizeof(link_target));
4783 return got_error_from_errno2("readlink", path2);
4784 sb2.st_mode = S_IFLNK;
4785 sb2.st_size = link_len;
4789 if (fstat(fd2, &sb2) == -1) {
4790 err = got_error_from_errno2("fstat", path2);
4794 f2 = fdopen(fd2, "r");
4796 err = got_error_from_errno2("fdopen", path2);
4802 f2 = got_opentemp();
4804 err = got_error_from_errno2("got_opentemp", path2);
4807 n = fwrite(link_target, 1, link_len, f2);
4808 if (n != link_len) {
4809 err = got_ferror(f2, GOT_ERR_IO);
4812 if (fflush(f2) == EOF) {
4813 err = got_error_from_errno("fflush");
4819 fd = got_opentempfd();
4821 err = got_error_from_errno("got_opentempfd");
4825 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4829 err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4833 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4837 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4838 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4842 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4847 if (fseek(f1, 0L, SEEK_SET) == -1)
4848 return got_ferror(f1, GOT_ERR_IO);
4849 if (fseek(f2, 0L, SEEK_SET) == -1)
4850 return got_ferror(f2, GOT_ERR_IO);
4852 /* Count the number of actual changes in the diff result. */
4853 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4854 struct diff_chunk_context cc = {};
4855 diff_chunk_context_load_change(&cc, &nchunks_used,
4856 diffreg_result->result, n, 0);
4859 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4861 err = apply_or_reject_change(&choice, &nchunks_used,
4862 diffreg_result->result, n, relpath, f1, f2,
4863 &line_cur1, &line_cur2,
4864 reverse_patch ? NULL : outfile,
4865 reverse_patch ? outfile : NULL,
4866 ++i, nchanges, patch_cb, patch_arg);
4869 if (choice == GOT_PATCH_CHOICE_YES)
4871 else if (choice == GOT_PATCH_CHOICE_QUIT)
4875 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4876 reverse_patch ? NULL : outfile,
4877 reverse_patch ? outfile : NULL);
4881 if (!S_ISLNK(sb2.st_mode)) {
4884 mode = apply_umask(sb2.st_mode);
4885 if (fchmod(fileno(outfile), mode) == -1) {
4886 err = got_error_from_errno2("fchmod", path2);
4893 if (fd != -1 && close(fd) == -1 && err == NULL)
4894 err = got_error_from_errno("close");
4896 got_object_blob_close(blob);
4897 free_err = got_diffreg_result_free(diffreg_result);
4900 if (f1 && fclose(f1) == EOF && err == NULL)
4901 err = got_error_from_errno2("fclose", path1);
4902 if (f2 && fclose(f2) == EOF && err == NULL)
4903 err = got_error_from_errno2("fclose", path2);
4904 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4905 err = got_error_from_errno2("close", path2);
4906 if (outfile && fclose(outfile) == EOF && err == NULL)
4907 err = got_error_from_errno2("fclose", *path_outfile);
4908 if (path1 && unlink(path1) == -1 && err == NULL)
4909 err = got_error_from_errno2("unlink", path1);
4910 if (err || !have_content) {
4911 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4912 err = got_error_from_errno2("unlink", *path_outfile);
4913 free(*path_outfile);
4914 *path_outfile = NULL;
4920 static const struct got_error *
4921 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4922 const char *relpath, struct got_object_id *blob_id,
4923 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4924 int dirfd, const char *de_name)
4926 struct revert_file_args *a = arg;
4927 const struct got_error *err = NULL;
4928 char *parent_path = NULL;
4929 struct got_fileindex_entry *ie;
4930 struct got_commit_object *base_commit = NULL;
4931 struct got_tree_object *tree = NULL;
4932 struct got_object_id *tree_id = NULL;
4933 const struct got_tree_entry *te = NULL;
4934 char *tree_path = NULL, *te_name;
4935 char *ondisk_path = NULL, *path_content = NULL;
4936 struct got_blob_object *blob = NULL;
4939 /* Reverting a staged deletion is a no-op. */
4940 if (status == GOT_STATUS_DELETE &&
4941 staged_status != GOT_STATUS_NO_CHANGE)
4944 if (status == GOT_STATUS_UNVERSIONED)
4945 return (*a->progress_cb)(a->progress_arg,
4946 GOT_STATUS_UNVERSIONED, relpath);
4948 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4950 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4952 /* Construct in-repository path of tree which contains this blob. */
4953 err = got_path_dirname(&parent_path, ie->path);
4955 if (err->code != GOT_ERR_BAD_PATH)
4957 parent_path = strdup("/");
4958 if (parent_path == NULL) {
4959 err = got_error_from_errno("strdup");
4963 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4964 tree_path = strdup(parent_path);
4965 if (tree_path == NULL) {
4966 err = got_error_from_errno("strdup");
4970 if (got_path_is_root_dir(parent_path)) {
4971 tree_path = strdup(a->worktree->path_prefix);
4972 if (tree_path == NULL) {
4973 err = got_error_from_errno("strdup");
4977 if (asprintf(&tree_path, "%s/%s",
4978 a->worktree->path_prefix, parent_path) == -1) {
4979 err = got_error_from_errno("asprintf");
4985 err = got_object_open_as_commit(&base_commit, a->repo,
4986 a->worktree->base_commit_id);
4990 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4992 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4993 (status == GOT_STATUS_ADD ||
4994 staged_status == GOT_STATUS_ADD)))
4997 err = got_object_open_as_tree(&tree, a->repo, tree_id);
5001 err = got_path_basename(&te_name, ie->path);
5005 te = got_object_tree_find_entry(tree, te_name);
5007 if (te == NULL && status != GOT_STATUS_ADD &&
5008 staged_status != GOT_STATUS_ADD) {
5009 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
5015 case GOT_STATUS_ADD:
5017 int choice = GOT_PATCH_CHOICE_NONE;
5018 err = (*a->patch_cb)(&choice, a->patch_arg,
5019 status, ie->path, NULL, 1, 1);
5022 if (choice != GOT_PATCH_CHOICE_YES)
5025 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
5029 got_fileindex_entry_remove(a->fileindex, ie);
5030 if (a->unlink_added_files) {
5031 int do_unlink = a->added_files_to_unlink ? 0 : 1;
5033 if (a->added_files_to_unlink) {
5034 struct got_pathlist_entry *pe;
5036 TAILQ_FOREACH(pe, a->added_files_to_unlink,
5038 if (got_path_cmp(pe->path, relpath,
5039 pe->path_len, strlen(relpath)))
5047 if (asprintf(&ondisk_path, "%s/%s",
5048 got_worktree_get_root_path(a->worktree),
5050 err = got_error_from_errno("asprintf");
5053 if (unlink(ondisk_path) == -1) {
5054 err = got_error_from_errno2("unlink",
5061 case GOT_STATUS_DELETE:
5063 int choice = GOT_PATCH_CHOICE_NONE;
5064 err = (*a->patch_cb)(&choice, a->patch_arg,
5065 status, ie->path, NULL, 1, 1);
5068 if (choice != GOT_PATCH_CHOICE_YES)
5072 case GOT_STATUS_MODIFY:
5073 case GOT_STATUS_MODE_CHANGE:
5074 case GOT_STATUS_CONFLICT:
5075 case GOT_STATUS_MISSING: {
5076 struct got_object_id id;
5077 if (staged_status == GOT_STATUS_ADD ||
5078 staged_status == GOT_STATUS_MODIFY)
5079 got_fileindex_entry_get_staged_blob_id(&id, ie);
5081 got_fileindex_entry_get_blob_id(&id, ie);
5082 fd = got_opentempfd();
5084 err = got_error_from_errno("got_opentempfd");
5088 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
5092 if (asprintf(&ondisk_path, "%s/%s",
5093 got_worktree_get_root_path(a->worktree), relpath) == -1) {
5094 err = got_error_from_errno("asprintf");
5098 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
5099 status == GOT_STATUS_CONFLICT)) {
5100 int is_bad_symlink = 0;
5101 err = create_patched_content(&path_content, 1, &id,
5102 ondisk_path, dirfd, de_name, ie->path, a->repo,
5103 a->patch_cb, a->patch_arg);
5104 if (err || path_content == NULL)
5106 if (te && S_ISLNK(te->mode)) {
5107 if (unlink(path_content) == -1) {
5108 err = got_error_from_errno2("unlink",
5112 err = install_symlink(&is_bad_symlink,
5113 a->worktree, ondisk_path, ie->path,
5114 blob, 0, 1, 0, 0, a->repo,
5115 a->progress_cb, a->progress_arg);
5117 if (rename(path_content, ondisk_path) == -1) {
5118 err = got_error_from_errno3("rename",
5119 path_content, ondisk_path);
5124 int is_bad_symlink = 0;
5125 if (te && S_ISLNK(te->mode)) {
5126 err = install_symlink(&is_bad_symlink,
5127 a->worktree, ondisk_path, ie->path,
5128 blob, 0, 1, 0, 0, a->repo,
5129 a->progress_cb, a->progress_arg);
5131 err = install_blob(a->worktree, ondisk_path,
5133 te ? te->mode : GOT_DEFAULT_FILE_MODE,
5134 got_fileindex_perms_to_st(ie), blob,
5135 0, 1, 0, 0, a->repo,
5136 a->progress_cb, a->progress_arg);
5140 if (status == GOT_STATUS_DELETE ||
5141 status == GOT_STATUS_MODE_CHANGE) {
5142 err = got_fileindex_entry_update(ie,
5143 a->worktree->root_fd, relpath,
5145 a->worktree->base_commit_id->sha1, 1);
5149 if (is_bad_symlink) {
5150 got_fileindex_entry_filetype_set(ie,
5151 GOT_FILEIDX_MODE_BAD_SYMLINK);
5164 if (fd != -1 && close(fd) == -1 && err == NULL)
5165 err = got_error_from_errno("close");
5167 got_object_blob_close(blob);
5169 got_object_tree_close(tree);
5172 got_object_commit_close(base_commit);
5176 const struct got_error *
5177 got_worktree_revert(struct got_worktree *worktree,
5178 struct got_pathlist_head *paths,
5179 got_worktree_checkout_cb progress_cb, void *progress_arg,
5180 got_worktree_patch_cb patch_cb, void *patch_arg,
5181 struct got_repository *repo)
5183 struct got_fileindex *fileindex = NULL;
5184 char *fileindex_path = NULL;
5185 const struct got_error *err = NULL, *unlockerr = NULL;
5186 const struct got_error *sync_err = NULL;
5187 struct got_pathlist_entry *pe;
5188 struct revert_file_args rfa;
5190 err = lock_worktree(worktree, LOCK_EX);
5194 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5198 rfa.worktree = worktree;
5199 rfa.fileindex = fileindex;
5200 rfa.progress_cb = progress_cb;
5201 rfa.progress_arg = progress_arg;
5202 rfa.patch_cb = patch_cb;
5203 rfa.patch_arg = patch_arg;
5205 rfa.unlink_added_files = 0;
5206 TAILQ_FOREACH(pe, paths, entry) {
5207 err = worktree_status(worktree, pe->path, fileindex, repo,
5208 revert_file, &rfa, NULL, NULL, 1, 0);
5212 sync_err = sync_fileindex(fileindex, fileindex_path);
5213 if (sync_err && err == NULL)
5216 free(fileindex_path);
5218 got_fileindex_free(fileindex);
5219 unlockerr = lock_worktree(worktree, LOCK_SH);
5220 if (unlockerr && err == NULL)
5226 free_commitable(struct got_commitable *ct)
5229 free(ct->in_repo_path);
5230 free(ct->ondisk_path);
5232 free(ct->base_blob_id);
5233 free(ct->staged_blob_id);
5234 free(ct->base_commit_id);
5238 struct collect_commitables_arg {
5239 struct got_pathlist_head *commitable_paths;
5240 struct got_repository *repo;
5241 struct got_worktree *worktree;
5242 struct got_fileindex *fileindex;
5243 int have_staged_files;
5244 int allow_bad_symlinks;
5245 int diff_header_shown;
5246 int commit_conflicts;
5253 * Create a file which contains the target path of a symlink so we can feed
5254 * it as content to the diff engine.
5256 static const struct got_error *
5257 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5258 const char *abspath)
5260 const struct got_error *err = NULL;
5261 char target_path[PATH_MAX];
5262 ssize_t target_len, outlen;
5267 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5268 if (target_len == -1)
5269 return got_error_from_errno2("readlinkat", abspath);
5271 target_len = readlink(abspath, target_path, PATH_MAX);
5272 if (target_len == -1)
5273 return got_error_from_errno2("readlink", abspath);
5276 *fd = got_opentempfd();
5278 return got_error_from_errno("got_opentempfd");
5280 outlen = write(*fd, target_path, target_len);
5282 err = got_error_from_errno("got_opentempfd");
5286 if (lseek(*fd, 0, SEEK_SET) == -1) {
5287 err = got_error_from_errno2("lseek", abspath);
5298 static const struct got_error *
5299 append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5300 FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5301 int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5303 const struct got_error *err = NULL;
5304 struct got_blob_object *blob1 = NULL;
5305 int fd = -1, fd1 = -1, fd2 = -1;
5306 FILE *ondisk_file = NULL;
5307 char *label1 = NULL;
5311 char *id_str = NULL;
5313 memset(&sb, 0, sizeof(sb));
5316 if (ct->staged_status != GOT_STATUS_MODIFY &&
5317 ct->staged_status != GOT_STATUS_ADD &&
5318 ct->staged_status != GOT_STATUS_DELETE)
5321 if (ct->status != GOT_STATUS_MODIFY &&
5322 ct->status != GOT_STATUS_ADD &&
5323 ct->status != GOT_STATUS_DELETE &&
5324 ct->status != GOT_STATUS_CONFLICT)
5328 err = got_opentemp_truncate(f1);
5330 return got_error_from_errno("got_opentemp_truncate");
5331 err = got_opentemp_truncate(f2);
5333 return got_error_from_errno("got_opentemp_truncate");
5335 if (!*diff_header_shown) {
5336 err = got_object_id_str(&id_str, worktree->base_commit_id);
5339 fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5340 got_worktree_get_root_path(worktree));
5341 fprintf(diff_outfile, "commit - %s\n", id_str);
5342 fprintf(diff_outfile, "path + %s%s\n",
5343 got_worktree_get_root_path(worktree),
5344 diff_staged ? " (staged changes)" : "");
5345 *diff_header_shown = 1;
5349 const char *label1 = NULL, *label2 = NULL;
5350 switch (ct->staged_status) {
5351 case GOT_STATUS_MODIFY:
5355 case GOT_STATUS_ADD:
5358 case GOT_STATUS_DELETE:
5362 return got_error(GOT_ERR_FILE_STATUS);
5364 fd1 = got_opentempfd();
5366 err = got_error_from_errno("got_opentempfd");
5369 fd2 = got_opentempfd();
5371 err = got_error_from_errno("got_opentempfd");
5374 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5375 fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5376 label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5377 NULL, repo, diff_outfile);
5381 fd1 = got_opentempfd();
5383 err = got_error_from_errno("got_opentempfd");
5387 if (ct->status != GOT_STATUS_ADD) {
5388 err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5394 if (ct->status != GOT_STATUS_DELETE) {
5396 fd = openat(dirfd, de_name,
5397 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5399 if (!got_err_open_nofollow_on_symlink()) {
5400 err = got_error_from_errno2("openat",
5404 err = get_symlink_target_file(&fd, dirfd,
5405 de_name, ct->ondisk_path);
5410 fd = open(ct->ondisk_path,
5411 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5413 if (!got_err_open_nofollow_on_symlink()) {
5414 err = got_error_from_errno2("open",
5418 err = get_symlink_target_file(&fd, dirfd,
5419 de_name, ct->ondisk_path);
5424 if (fstatat(fd, ct->ondisk_path, &sb,
5425 AT_SYMLINK_NOFOLLOW) == -1) {
5426 err = got_error_from_errno2("fstatat", ct->ondisk_path);
5429 ondisk_file = fdopen(fd, "r");
5430 if (ondisk_file == NULL) {
5431 err = got_error_from_errno2("fdopen", ct->ondisk_path);
5439 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5445 err = got_diff_blob_file(blob1, f1, size1, label1,
5446 ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5447 GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5449 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5450 err = got_error_from_errno("close");
5451 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5452 err = got_error_from_errno("close");
5454 got_object_blob_close(blob1);
5455 if (fd != -1 && close(fd) == -1 && err == NULL)
5456 err = got_error_from_errno("close");
5457 if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5458 err = got_error_from_errno("fclose");
5462 static const struct got_error *
5463 collect_commitables(void *arg, unsigned char status,
5464 unsigned char staged_status, const char *relpath,
5465 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5466 struct got_object_id *commit_id, int dirfd, const char *de_name)
5468 struct collect_commitables_arg *a = arg;
5469 const struct got_error *err = NULL;
5470 struct got_commitable *ct = NULL;
5471 struct got_pathlist_entry *new = NULL;
5472 char *parent_path = NULL, *path = NULL;
5475 if (a->have_staged_files) {
5476 if (staged_status != GOT_STATUS_MODIFY &&
5477 staged_status != GOT_STATUS_ADD &&
5478 staged_status != GOT_STATUS_DELETE)
5481 if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5482 printf("C %s\n", relpath);
5483 return got_error(GOT_ERR_COMMIT_CONFLICT);
5486 if (status != GOT_STATUS_MODIFY &&
5487 status != GOT_STATUS_MODE_CHANGE &&
5488 status != GOT_STATUS_ADD &&
5489 status != GOT_STATUS_DELETE &&
5490 status != GOT_STATUS_CONFLICT)
5494 if (asprintf(&path, "/%s", relpath) == -1) {
5495 err = got_error_from_errno("asprintf");
5498 if (strcmp(path, "/") == 0) {
5499 parent_path = strdup("");
5500 if (parent_path == NULL)
5501 return got_error_from_errno("strdup");
5503 err = got_path_dirname(&parent_path, path);
5508 ct = calloc(1, sizeof(*ct));
5510 err = got_error_from_errno("calloc");
5514 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5516 err = got_error_from_errno("asprintf");
5520 if (staged_status == GOT_STATUS_ADD ||
5521 staged_status == GOT_STATUS_MODIFY) {
5522 struct got_fileindex_entry *ie;
5523 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5524 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5525 case GOT_FILEIDX_MODE_REGULAR_FILE:
5526 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5529 case GOT_FILEIDX_MODE_SYMLINK:
5533 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5536 ct->mode |= got_fileindex_entry_perms_get(ie);
5537 } else if (status != GOT_STATUS_DELETE &&
5538 staged_status != GOT_STATUS_DELETE) {
5540 if (fstatat(dirfd, de_name, &sb,
5541 AT_SYMLINK_NOFOLLOW) == -1) {
5542 err = got_error_from_errno2("fstatat",
5546 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5547 err = got_error_from_errno2("lstat", ct->ondisk_path);
5550 ct->mode = sb.st_mode;
5553 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5554 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5556 err = got_error_from_errno("asprintf");
5560 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5561 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5563 char target_path[PATH_MAX];
5565 target_len = readlink(ct->ondisk_path, target_path,
5566 sizeof(target_path));
5567 if (target_len == -1) {
5568 err = got_error_from_errno2("readlink",
5572 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5573 target_len, ct->ondisk_path, a->worktree->root_path);
5576 if (is_bad_symlink) {
5577 err = got_error_path(ct->ondisk_path,
5578 GOT_ERR_BAD_SYMLINK);
5584 ct->status = status;
5585 ct->staged_status = staged_status;
5586 ct->blob_id = NULL; /* will be filled in when blob gets created */
5587 if (ct->status != GOT_STATUS_ADD &&
5588 ct->staged_status != GOT_STATUS_ADD) {
5589 ct->base_blob_id = got_object_id_dup(blob_id);
5590 if (ct->base_blob_id == NULL) {
5591 err = got_error_from_errno("got_object_id_dup");
5594 ct->base_commit_id = got_object_id_dup(commit_id);
5595 if (ct->base_commit_id == NULL) {
5596 err = got_error_from_errno("got_object_id_dup");
5600 if (ct->staged_status == GOT_STATUS_ADD ||
5601 ct->staged_status == GOT_STATUS_MODIFY) {
5602 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5603 if (ct->staged_blob_id == NULL) {
5604 err = got_error_from_errno("got_object_id_dup");
5608 ct->path = strdup(path);
5609 if (ct->path == NULL) {
5610 err = got_error_from_errno("strdup");
5613 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5617 if (a->diff_outfile && ct && new != NULL) {
5618 err = append_ct_diff(ct, &a->diff_header_shown,
5619 a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5620 a->have_staged_files, a->repo, a->worktree);
5625 if (ct && (err || new == NULL))
5626 free_commitable(ct);
5632 static const struct got_error *write_tree(struct got_object_id **, int *,
5633 struct got_tree_object *, const char *, struct got_pathlist_head *,
5634 got_worktree_status_cb status_cb, void *status_arg,
5635 struct got_repository *);
5637 static const struct got_error *
5638 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5639 struct got_tree_entry *te, const char *parent_path,
5640 struct got_pathlist_head *commitable_paths,
5641 got_worktree_status_cb status_cb, void *status_arg,
5642 struct got_repository *repo)
5644 const struct got_error *err = NULL;
5645 struct got_tree_object *subtree;
5648 if (asprintf(&subpath, "%s%s%s", parent_path,
5649 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5650 return got_error_from_errno("asprintf");
5652 err = got_object_open_as_tree(&subtree, repo, &te->id);
5656 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5657 commitable_paths, status_cb, status_arg, repo);
5658 got_object_tree_close(subtree);
5663 static const struct got_error *
5664 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5666 const struct got_error *err = NULL;
5667 char *ct_parent_path = NULL;
5671 if (strchr(ct->in_repo_path, '/') == NULL) {
5672 *match = got_path_is_root_dir(path);
5676 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5679 *match = (strcmp(path, ct_parent_path) == 0);
5680 free(ct_parent_path);
5685 get_ct_file_mode(struct got_commitable *ct)
5687 if (S_ISLNK(ct->mode))
5690 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5693 static const struct got_error *
5694 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5695 struct got_tree_entry *te, struct got_commitable *ct)
5697 const struct got_error *err = NULL;
5701 err = got_object_tree_entry_dup(new_te, te);
5705 (*new_te)->mode = get_ct_file_mode(ct);
5707 if (ct->staged_status == GOT_STATUS_MODIFY)
5708 memcpy(&(*new_te)->id, ct->staged_blob_id,
5709 sizeof((*new_te)->id));
5711 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5713 if (err && *new_te) {
5720 static const struct got_error *
5721 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5722 struct got_commitable *ct)
5724 const struct got_error *err = NULL;
5725 char *ct_name = NULL;
5729 *new_te = calloc(1, sizeof(**new_te));
5730 if (*new_te == NULL)
5731 return got_error_from_errno("calloc");
5733 err = got_path_basename(&ct_name, ct->path);
5736 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5737 sizeof((*new_te)->name)) {
5738 err = got_error(GOT_ERR_NO_SPACE);
5742 (*new_te)->mode = get_ct_file_mode(ct);
5744 if (ct->staged_status == GOT_STATUS_ADD)
5745 memcpy(&(*new_te)->id, ct->staged_blob_id,
5746 sizeof((*new_te)->id));
5748 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5751 if (err && *new_te) {
5758 static const struct got_error *
5759 insert_tree_entry(struct got_tree_entry *new_te,
5760 struct got_pathlist_head *paths)
5762 const struct got_error *err = NULL;
5763 struct got_pathlist_entry *new_pe;
5765 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5769 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5773 static const struct got_error *
5774 report_ct_status(struct got_commitable *ct,
5775 got_worktree_status_cb status_cb, void *status_arg)
5777 const char *ct_path = ct->path;
5778 unsigned char status;
5780 if (status_cb == NULL) /* no commit progress output desired */
5783 while (ct_path[0] == '/')
5786 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5787 status = ct->staged_status;
5789 status = ct->status;
5791 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5792 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5795 static const struct got_error *
5796 match_modified_subtree(int *modified, struct got_tree_entry *te,
5797 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5799 const struct got_error *err = NULL;
5800 struct got_pathlist_entry *pe;
5805 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5806 got_path_is_root_dir(base_tree_path) ? "" : "/",
5808 return got_error_from_errno("asprintf");
5810 TAILQ_FOREACH(pe, commitable_paths, entry) {
5811 struct got_commitable *ct = pe->data;
5812 *modified = got_path_is_child(ct->in_repo_path, te_path,
5822 static const struct got_error *
5823 match_deleted_or_modified_ct(struct got_commitable **ctp,
5824 struct got_tree_entry *te, const char *base_tree_path,
5825 struct got_pathlist_head *commitable_paths)
5827 const struct got_error *err = NULL;
5828 struct got_pathlist_entry *pe;
5832 TAILQ_FOREACH(pe, commitable_paths, entry) {
5833 struct got_commitable *ct = pe->data;
5834 char *ct_name = NULL;
5837 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5838 if (ct->status != GOT_STATUS_MODIFY &&
5839 ct->status != GOT_STATUS_MODE_CHANGE &&
5840 ct->status != GOT_STATUS_DELETE &&
5841 ct->status != GOT_STATUS_CONFLICT)
5844 if (ct->staged_status != GOT_STATUS_MODIFY &&
5845 ct->staged_status != GOT_STATUS_DELETE)
5849 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5852 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5858 err = got_path_basename(&ct_name, pe->path);
5862 if (strcmp(te->name, ct_name) != 0) {
5875 static const struct got_error *
5876 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5877 const char *child_path, const char *path_base_tree,
5878 struct got_pathlist_head *commitable_paths,
5879 got_worktree_status_cb status_cb, void *status_arg,
5880 struct got_repository *repo)
5882 const struct got_error *err = NULL;
5883 struct got_tree_entry *new_te;
5885 struct got_object_id *id = NULL;
5890 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5891 got_path_is_root_dir(path_base_tree) ? "" : "/",
5893 return got_error_from_errno("asprintf");
5895 new_te = calloc(1, sizeof(*new_te));
5897 return got_error_from_errno("calloc");
5898 new_te->mode = S_IFDIR;
5900 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5901 sizeof(new_te->name)) {
5902 err = got_error(GOT_ERR_NO_SPACE);
5905 err = write_tree(&id, &nentries, NULL, subtree_path,
5906 commitable_paths, status_cb, status_arg, repo);
5911 memcpy(&new_te->id, id, sizeof(new_te->id));
5920 static const struct got_error *
5921 write_tree(struct got_object_id **new_tree_id, int *nentries,
5922 struct got_tree_object *base_tree, const char *path_base_tree,
5923 struct got_pathlist_head *commitable_paths,
5924 got_worktree_status_cb status_cb, void *status_arg,
5925 struct got_repository *repo)
5927 const struct got_error *err = NULL;
5928 struct got_pathlist_head paths;
5929 struct got_tree_entry *te, *new_te = NULL;
5930 struct got_pathlist_entry *pe;
5935 /* Insert, and recurse into, newly added entries first. */
5936 TAILQ_FOREACH(pe, commitable_paths, entry) {
5937 struct got_commitable *ct = pe->data;
5938 char *child_path = NULL, *slash;
5940 if ((ct->status != GOT_STATUS_ADD &&
5941 ct->staged_status != GOT_STATUS_ADD) ||
5942 (ct->flags & GOT_COMMITABLE_ADDED))
5945 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5946 strlen(path_base_tree)))
5949 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5954 slash = strchr(child_path, '/');
5955 if (slash == NULL) {
5956 err = alloc_added_blob_tree_entry(&new_te, ct);
5959 err = report_ct_status(ct, status_cb, status_arg);
5962 ct->flags |= GOT_COMMITABLE_ADDED;
5963 err = insert_tree_entry(new_te, &paths);
5968 *slash = '\0'; /* trim trailing path components */
5969 if (base_tree == NULL ||
5970 got_object_tree_find_entry(base_tree, child_path)
5972 err = make_subtree_for_added_blob(&new_te,
5973 child_path, path_base_tree,
5974 commitable_paths, status_cb, status_arg,
5978 err = insert_tree_entry(new_te, &paths);
5987 int i, nbase_entries;
5988 /* Handle modified and deleted entries. */
5989 nbase_entries = got_object_tree_get_nentries(base_tree);
5990 for (i = 0; i < nbase_entries; i++) {
5991 struct got_commitable *ct = NULL;
5993 te = got_object_tree_get_entry(base_tree, i);
5994 if (got_object_tree_entry_is_submodule(te)) {
5995 /* Entry is a submodule; just copy it. */
5996 err = got_object_tree_entry_dup(&new_te, te);
5999 err = insert_tree_entry(new_te, &paths);
6006 if (S_ISDIR(te->mode)) {
6008 err = got_object_tree_entry_dup(&new_te, te);
6011 err = match_modified_subtree(&modified, te,
6012 path_base_tree, commitable_paths);
6015 /* Avoid recursion into unmodified subtrees. */
6017 struct got_object_id *new_id;
6019 err = write_subtree(&new_id,
6021 path_base_tree, commitable_paths,
6022 status_cb, status_arg, repo);
6025 if (nsubentries == 0) {
6026 /* All entries were deleted. */
6030 memcpy(&new_te->id, new_id,
6031 sizeof(new_te->id));
6034 err = insert_tree_entry(new_te, &paths);
6041 err = match_deleted_or_modified_ct(&ct, te,
6042 path_base_tree, commitable_paths);
6046 /* NB: Deleted entries get dropped here. */
6047 if (ct->status == GOT_STATUS_MODIFY ||
6048 ct->status == GOT_STATUS_MODE_CHANGE ||
6049 ct->status == GOT_STATUS_CONFLICT ||
6050 ct->staged_status == GOT_STATUS_MODIFY) {
6051 err = alloc_modified_blob_tree_entry(
6055 err = insert_tree_entry(new_te, &paths);
6060 err = report_ct_status(ct, status_cb,
6065 /* Entry is unchanged; just copy it. */
6066 err = got_object_tree_entry_dup(&new_te, te);
6069 err = insert_tree_entry(new_te, &paths);
6077 /* Write new list of entries; deleted entries have been dropped. */
6078 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
6080 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
6084 static const struct got_error *
6085 update_fileindex_after_commit(struct got_worktree *worktree,
6086 struct got_pathlist_head *commitable_paths,
6087 struct got_object_id *new_base_commit_id,
6088 struct got_fileindex *fileindex, int have_staged_files)
6090 const struct got_error *err = NULL;
6091 struct got_pathlist_entry *pe;
6092 char *relpath = NULL;
6094 TAILQ_FOREACH(pe, commitable_paths, entry) {
6095 struct got_fileindex_entry *ie;
6096 struct got_commitable *ct = pe->data;
6098 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6100 err = got_path_skip_common_ancestor(&relpath,
6101 worktree->root_path, ct->ondisk_path);
6106 if (ct->status == GOT_STATUS_DELETE ||
6107 ct->staged_status == GOT_STATUS_DELETE) {
6108 got_fileindex_entry_remove(fileindex, ie);
6109 } else if (ct->staged_status == GOT_STATUS_ADD ||
6110 ct->staged_status == GOT_STATUS_MODIFY) {
6111 got_fileindex_entry_stage_set(ie,
6112 GOT_FILEIDX_STAGE_NONE);
6113 got_fileindex_entry_staged_filetype_set(ie, 0);
6115 err = got_fileindex_entry_update(ie,
6116 worktree->root_fd, relpath,
6117 ct->staged_blob_id->sha1,
6118 new_base_commit_id->sha1,
6119 !have_staged_files);
6121 err = got_fileindex_entry_update(ie,
6122 worktree->root_fd, relpath,
6124 new_base_commit_id->sha1,
6125 !have_staged_files);
6127 err = got_fileindex_entry_alloc(&ie, pe->path);
6130 err = got_fileindex_entry_update(ie,
6131 worktree->root_fd, relpath, ct->blob_id->sha1,
6132 new_base_commit_id->sha1, 1);
6134 got_fileindex_entry_free(ie);
6137 err = got_fileindex_entry_add(fileindex, ie);
6139 got_fileindex_entry_free(ie);
6152 static const struct got_error *
6153 check_out_of_date(const char *in_repo_path, unsigned char status,
6154 unsigned char staged_status, struct got_object_id *base_blob_id,
6155 struct got_object_id *base_commit_id,
6156 struct got_object_id *head_commit_id, struct got_repository *repo,
6159 const struct got_error *err = NULL;
6160 struct got_commit_object *commit = NULL;
6161 struct got_object_id *id = NULL;
6163 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
6164 /* Trivial case: base commit == head commit */
6165 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
6168 * Ensure file content which local changes were based
6169 * on matches file content in the branch head.
6171 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6174 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6176 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6177 err = got_error(ood_errcode);
6179 } else if (got_object_id_cmp(id, base_blob_id) != 0)
6180 err = got_error(ood_errcode);
6182 /* Require that added files don't exist in the branch head. */
6183 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6186 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6187 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
6189 err = id ? got_error(ood_errcode) : NULL;
6194 got_object_commit_close(commit);
6198 static const struct got_error *
6199 commit_worktree(struct got_object_id **new_commit_id,
6200 struct got_pathlist_head *commitable_paths,
6201 struct got_object_id *head_commit_id,
6202 struct got_object_id *parent_id2,
6203 struct got_worktree *worktree,
6204 const char *author, const char *committer, char *diff_path,
6205 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6206 got_worktree_status_cb status_cb, void *status_arg,
6207 struct got_repository *repo)
6209 const struct got_error *err = NULL, *unlockerr = NULL;
6210 struct got_pathlist_entry *pe;
6211 const char *head_ref_name = NULL;
6212 struct got_commit_object *head_commit = NULL;
6213 struct got_reference *head_ref2 = NULL;
6214 struct got_object_id *head_commit_id2 = NULL;
6215 struct got_tree_object *head_tree = NULL;
6216 struct got_object_id *new_tree_id = NULL;
6217 int nentries, nparents = 0;
6218 struct got_object_id_queue parent_ids;
6219 struct got_object_qid *pid = NULL;
6220 char *logmsg = NULL;
6223 *new_commit_id = NULL;
6225 STAILQ_INIT(&parent_ids);
6227 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6231 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6235 if (commit_msg_cb != NULL) {
6236 err = commit_msg_cb(commitable_paths, diff_path,
6237 &logmsg, commit_arg);
6242 if (logmsg == NULL || strlen(logmsg) == 0) {
6243 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6247 /* Create blobs from added and modified files and record their IDs. */
6248 TAILQ_FOREACH(pe, commitable_paths, entry) {
6249 struct got_commitable *ct = pe->data;
6252 /* Blobs for staged files already exist. */
6253 if (ct->staged_status == GOT_STATUS_ADD ||
6254 ct->staged_status == GOT_STATUS_MODIFY)
6257 if (ct->status != GOT_STATUS_ADD &&
6258 ct->status != GOT_STATUS_MODIFY &&
6259 ct->status != GOT_STATUS_MODE_CHANGE &&
6260 ct->status != GOT_STATUS_CONFLICT)
6263 if (asprintf(&ondisk_path, "%s/%s",
6264 worktree->root_path, pe->path) == -1) {
6265 err = got_error_from_errno("asprintf");
6268 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6274 /* Recursively write new tree objects. */
6275 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6276 commitable_paths, status_cb, status_arg, repo);
6280 err = got_object_qid_alloc(&pid, head_commit_id);
6283 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6286 err = got_object_qid_alloc(&pid, parent_id2);
6289 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6292 timestamp = time(NULL);
6293 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6294 nparents, author, timestamp, committer, timestamp, logmsg, repo);
6300 /* Check if a concurrent commit to our branch has occurred. */
6301 head_ref_name = got_worktree_get_head_ref_name(worktree);
6302 if (head_ref_name == NULL) {
6303 err = got_error_from_errno("got_worktree_get_head_ref_name");
6306 /* Lock the reference here to prevent concurrent modification. */
6307 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6310 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6313 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6314 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6317 /* Update branch head in repository. */
6318 err = got_ref_change_ref(head_ref2, *new_commit_id);
6321 err = got_ref_write(head_ref2, repo);
6325 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6329 err = ref_base_commit(worktree, repo);
6333 got_object_id_queue_free(&parent_ids);
6335 got_object_tree_close(head_tree);
6337 got_object_commit_close(head_commit);
6338 free(head_commit_id2);
6340 unlockerr = got_ref_unlock(head_ref2);
6341 if (unlockerr && err == NULL)
6343 got_ref_close(head_ref2);
6348 static const struct got_error *
6349 check_path_is_commitable(const char *path,
6350 struct got_pathlist_head *commitable_paths)
6352 struct got_pathlist_entry *cpe = NULL;
6353 size_t path_len = strlen(path);
6355 TAILQ_FOREACH(cpe, commitable_paths, entry) {
6356 struct got_commitable *ct = cpe->data;
6357 const char *ct_path = ct->path;
6359 while (ct_path[0] == '/')
6362 if (strcmp(path, ct_path) == 0 ||
6363 got_path_is_child(ct_path, path, path_len))
6368 return got_error_path(path, GOT_ERR_BAD_PATH);
6373 static const struct got_error *
6374 check_staged_file(void *arg, struct got_fileindex_entry *ie)
6376 int *have_staged_files = arg;
6378 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6379 *have_staged_files = 1;
6380 return got_error(GOT_ERR_CANCELLED);
6386 static const struct got_error *
6387 check_non_staged_files(struct got_fileindex *fileindex,
6388 struct got_pathlist_head *paths)
6390 struct got_pathlist_entry *pe;
6391 struct got_fileindex_entry *ie;
6393 TAILQ_FOREACH(pe, paths, entry) {
6394 if (pe->path[0] == '\0')
6396 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6398 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6399 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6400 return got_error_path(pe->path,
6401 GOT_ERR_FILE_NOT_STAGED);
6407 const struct got_error *
6408 got_worktree_commit(struct got_object_id **new_commit_id,
6409 struct got_worktree *worktree, struct got_pathlist_head *paths,
6410 const char *author, const char *committer, int allow_bad_symlinks,
6411 int show_diff, int commit_conflicts,
6412 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6413 got_worktree_status_cb status_cb, void *status_arg,
6414 struct got_repository *repo)
6416 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6417 struct got_fileindex *fileindex = NULL;
6418 char *fileindex_path = NULL;
6419 struct got_pathlist_head commitable_paths;
6420 struct collect_commitables_arg cc_arg;
6421 struct got_pathlist_entry *pe;
6422 struct got_reference *head_ref = NULL;
6423 struct got_object_id *head_commit_id = NULL;
6424 char *diff_path = NULL;
6425 int have_staged_files = 0;
6427 *new_commit_id = NULL;
6429 memset(&cc_arg, 0, sizeof(cc_arg));
6430 TAILQ_INIT(&commitable_paths);
6432 err = lock_worktree(worktree, LOCK_EX);
6436 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6440 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6444 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6448 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6449 &have_staged_files);
6450 if (err && err->code != GOT_ERR_CANCELLED)
6452 if (have_staged_files) {
6453 err = check_non_staged_files(fileindex, paths);
6458 cc_arg.commitable_paths = &commitable_paths;
6459 cc_arg.worktree = worktree;
6460 cc_arg.fileindex = fileindex;
6462 cc_arg.have_staged_files = have_staged_files;
6463 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6464 cc_arg.diff_header_shown = 0;
6465 cc_arg.commit_conflicts = commit_conflicts;
6467 err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6468 GOT_TMPDIR_STR "/got", ".diff");
6471 cc_arg.f1 = got_opentemp();
6472 if (cc_arg.f1 == NULL) {
6473 err = got_error_from_errno("got_opentemp");
6476 cc_arg.f2 = got_opentemp();
6477 if (cc_arg.f2 == NULL) {
6478 err = got_error_from_errno("got_opentemp");
6483 TAILQ_FOREACH(pe, paths, entry) {
6484 err = worktree_status(worktree, pe->path, fileindex, repo,
6485 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6491 if (fflush(cc_arg.diff_outfile) == EOF) {
6492 err = got_error_from_errno("fflush");
6497 if (TAILQ_EMPTY(&commitable_paths)) {
6498 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6502 TAILQ_FOREACH(pe, paths, entry) {
6503 err = check_path_is_commitable(pe->path, &commitable_paths);
6508 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6509 struct got_commitable *ct = pe->data;
6510 const char *ct_path = ct->in_repo_path;
6512 while (ct_path[0] == '/')
6514 err = check_out_of_date(ct_path, ct->status,
6515 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6516 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6522 err = commit_worktree(new_commit_id, &commitable_paths,
6523 head_commit_id, NULL, worktree, author, committer,
6524 (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6525 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6529 err = update_fileindex_after_commit(worktree, &commitable_paths,
6530 *new_commit_id, fileindex, have_staged_files);
6531 sync_err = sync_fileindex(fileindex, fileindex_path);
6532 if (sync_err && err == NULL)
6536 got_fileindex_free(fileindex);
6537 free(fileindex_path);
6538 unlockerr = lock_worktree(worktree, LOCK_SH);
6539 if (unlockerr && err == NULL)
6541 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6542 struct got_commitable *ct = pe->data;
6544 free_commitable(ct);
6546 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6547 if (diff_path && unlink(diff_path) == -1 && err == NULL)
6548 err = got_error_from_errno2("unlink", diff_path);
6550 if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6552 err = got_error_from_errno("fclose");
6557 got_commitable_get_path(struct got_commitable *ct)
6563 got_commitable_get_status(struct got_commitable *ct)
6568 struct check_rebase_ok_arg {
6569 struct got_worktree *worktree;
6570 struct got_repository *repo;
6573 static const struct got_error *
6574 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6576 const struct got_error *err = NULL;
6577 struct check_rebase_ok_arg *a = arg;
6578 unsigned char status;
6582 /* Reject rebase of a work tree with mixed base commits. */
6583 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6584 SHA1_DIGEST_LENGTH))
6585 return got_error(GOT_ERR_MIXED_COMMITS);
6587 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6589 return got_error_from_errno("asprintf");
6591 /* Reject rebase of a work tree with modified or staged files. */
6592 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6597 if (status != GOT_STATUS_NO_CHANGE)
6598 return got_error(GOT_ERR_MODIFIED);
6599 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6600 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6605 const struct got_error *
6606 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6607 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6608 struct got_worktree *worktree, struct got_reference *branch,
6609 struct got_repository *repo)
6611 const struct got_error *err = NULL;
6612 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6613 char *branch_ref_name = NULL;
6614 char *fileindex_path = NULL;
6615 struct check_rebase_ok_arg ok_arg;
6616 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6617 struct got_object_id *wt_branch_tip = NULL;
6619 *new_base_branch_ref = NULL;
6623 err = lock_worktree(worktree, LOCK_EX);
6627 err = open_fileindex(fileindex, &fileindex_path, worktree);
6631 ok_arg.worktree = worktree;
6633 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6638 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6642 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6646 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6650 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6655 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6658 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6659 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6663 err = got_ref_alloc_symref(new_base_branch_ref,
6664 new_base_branch_ref_name, wt_branch);
6667 err = got_ref_write(*new_base_branch_ref, repo);
6671 /* TODO Lock original branch's ref while rebasing? */
6673 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6677 err = got_ref_write(branch_ref, repo);
6681 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6682 worktree->base_commit_id);
6685 err = got_ref_write(*tmp_branch, repo);
6689 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6693 free(fileindex_path);
6694 free(tmp_branch_name);
6695 free(new_base_branch_ref_name);
6696 free(branch_ref_name);
6698 got_ref_close(branch_ref);
6700 got_ref_close(wt_branch);
6701 free(wt_branch_tip);
6703 if (*new_base_branch_ref) {
6704 got_ref_close(*new_base_branch_ref);
6705 *new_base_branch_ref = NULL;
6708 got_ref_close(*tmp_branch);
6712 got_fileindex_free(*fileindex);
6715 lock_worktree(worktree, LOCK_SH);
6720 const struct got_error *
6721 got_worktree_rebase_continue(struct got_object_id **commit_id,
6722 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6723 struct got_reference **branch, struct got_fileindex **fileindex,
6724 struct got_worktree *worktree, struct got_repository *repo)
6726 const struct got_error *err;
6727 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6728 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6729 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6730 char *fileindex_path = NULL;
6731 int have_staged_files = 0;
6734 *new_base_branch = NULL;
6739 err = lock_worktree(worktree, LOCK_EX);
6743 err = open_fileindex(fileindex, &fileindex_path, worktree);
6747 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6748 &have_staged_files);
6749 if (err && err->code != GOT_ERR_CANCELLED)
6751 if (have_staged_files) {
6752 err = got_error(GOT_ERR_STAGED_PATHS);
6756 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6760 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6764 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6768 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6772 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6776 err = got_ref_open(branch, repo,
6777 got_ref_get_symref_target(branch_ref), 0);
6781 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6785 err = got_ref_resolve(commit_id, repo, commit_ref);
6789 err = got_ref_open(new_base_branch, repo,
6790 new_base_branch_ref_name, 0);
6794 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6798 free(commit_ref_name);
6799 free(branch_ref_name);
6800 free(fileindex_path);
6802 got_ref_close(commit_ref);
6804 got_ref_close(branch_ref);
6809 got_ref_close(*tmp_branch);
6812 if (*new_base_branch) {
6813 got_ref_close(*new_base_branch);
6814 *new_base_branch = NULL;
6817 got_ref_close(*branch);
6821 got_fileindex_free(*fileindex);
6824 lock_worktree(worktree, LOCK_SH);
6829 const struct got_error *
6830 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6832 const struct got_error *err;
6833 char *tmp_branch_name = NULL;
6835 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6839 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6840 free(tmp_branch_name);
6844 static const struct got_error *
6845 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6846 const char *diff_path, char **logmsg, void *arg)
6852 static const struct got_error *
6853 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6854 const char *path, struct got_object_id *blob_id,
6855 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6856 int dirfd, const char *de_name)
6861 struct collect_merged_paths_arg {
6862 got_worktree_checkout_cb progress_cb;
6864 struct got_pathlist_head *merged_paths;
6867 static const struct got_error *
6868 collect_merged_paths(void *arg, unsigned char status, const char *path)
6870 const struct got_error *err;
6871 struct collect_merged_paths_arg *a = arg;
6873 struct got_pathlist_entry *new;
6875 err = (*a->progress_cb)(a->progress_arg, status, path);
6879 if (status != GOT_STATUS_MERGE &&
6880 status != GOT_STATUS_ADD &&
6881 status != GOT_STATUS_DELETE &&
6882 status != GOT_STATUS_CONFLICT)
6887 return got_error_from_errno("strdup");
6889 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6890 if (err || new == NULL)
6895 static const struct got_error *
6896 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6897 int is_rebase, struct got_repository *repo)
6899 const struct got_error *err;
6900 struct got_reference *commit_ref = NULL;
6902 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6904 if (err->code != GOT_ERR_NOT_REF)
6906 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6909 err = got_ref_write(commit_ref, repo);
6912 } else if (is_rebase) {
6913 struct got_object_id *stored_id;
6916 err = got_ref_resolve(&stored_id, repo, commit_ref);
6919 cmp = got_object_id_cmp(commit_id, stored_id);
6922 err = got_error(GOT_ERR_REBASE_COMMITID);
6928 got_ref_close(commit_ref);
6932 static const struct got_error *
6933 rebase_merge_files(struct got_pathlist_head *merged_paths,
6934 const char *commit_ref_name, struct got_worktree *worktree,
6935 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6936 struct got_object_id *commit_id, struct got_repository *repo,
6937 got_worktree_checkout_cb progress_cb, void *progress_arg,
6938 got_cancel_cb cancel_cb, void *cancel_arg)
6940 const struct got_error *err;
6941 struct got_reference *commit_ref = NULL;
6942 struct collect_merged_paths_arg cmp_arg;
6943 char *fileindex_path;
6945 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6947 err = get_fileindex_path(&fileindex_path, worktree);
6951 cmp_arg.progress_cb = progress_cb;
6952 cmp_arg.progress_arg = progress_arg;
6953 cmp_arg.merged_paths = merged_paths;
6954 err = merge_files(worktree, fileindex, fileindex_path,
6955 parent_commit_id, commit_id, repo, collect_merged_paths,
6956 &cmp_arg, cancel_cb, cancel_arg);
6958 got_ref_close(commit_ref);
6962 const struct got_error *
6963 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6964 struct got_worktree *worktree, struct got_fileindex *fileindex,
6965 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6966 struct got_repository *repo,
6967 got_worktree_checkout_cb progress_cb, void *progress_arg,
6968 got_cancel_cb cancel_cb, void *cancel_arg)
6970 const struct got_error *err;
6971 char *commit_ref_name;
6973 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6977 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6981 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6982 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6983 progress_arg, cancel_cb, cancel_arg);
6985 free(commit_ref_name);
6989 const struct got_error *
6990 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6991 struct got_worktree *worktree, struct got_fileindex *fileindex,
6992 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6993 struct got_repository *repo,
6994 got_worktree_checkout_cb progress_cb, void *progress_arg,
6995 got_cancel_cb cancel_cb, void *cancel_arg)
6997 const struct got_error *err;
6998 char *commit_ref_name;
7000 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7004 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7008 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7009 fileindex, parent_commit_id, commit_id, repo, progress_cb,
7010 progress_arg, cancel_cb, cancel_arg);
7012 free(commit_ref_name);
7016 static const struct got_error *
7017 rebase_commit(struct got_object_id **new_commit_id,
7018 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
7019 struct got_worktree *worktree, struct got_fileindex *fileindex,
7020 struct got_reference *tmp_branch, const char *committer,
7021 struct got_commit_object *orig_commit, const char *new_logmsg,
7022 int allow_conflict, struct got_repository *repo)
7024 const struct got_error *err, *sync_err;
7025 struct got_pathlist_head commitable_paths;
7026 struct collect_commitables_arg cc_arg;
7027 char *fileindex_path = NULL;
7028 struct got_reference *head_ref = NULL;
7029 struct got_object_id *head_commit_id = NULL;
7030 char *logmsg = NULL;
7032 memset(&cc_arg, 0, sizeof(cc_arg));
7033 TAILQ_INIT(&commitable_paths);
7034 *new_commit_id = NULL;
7036 /* Work tree is locked/unlocked during rebase preparation/teardown. */
7038 err = get_fileindex_path(&fileindex_path, worktree);
7042 cc_arg.commitable_paths = &commitable_paths;
7043 cc_arg.worktree = worktree;
7045 cc_arg.have_staged_files = 0;
7046 cc_arg.commit_conflicts = allow_conflict;
7048 * If possible get the status of individual files directly to
7049 * avoid crawling the entire work tree once per rebased commit.
7051 * Ideally, merged_paths would contain a list of commitables
7052 * we could use so we could skip worktree_status() entirely.
7053 * However, we would then need carefully keep track of cumulative
7054 * effects of operations such as file additions and deletions
7055 * in 'got histedit -f' (folding multiple commits into one),
7056 * and this extra complexity is not really worth it.
7059 struct got_pathlist_entry *pe;
7060 TAILQ_FOREACH(pe, merged_paths, entry) {
7061 err = worktree_status(worktree, pe->path, fileindex,
7062 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
7068 err = worktree_status(worktree, "", fileindex, repo,
7069 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7074 if (TAILQ_EMPTY(&commitable_paths)) {
7075 /* No-op change; commit will be elided. */
7076 err = got_ref_delete(commit_ref, repo);
7079 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
7083 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7087 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7092 logmsg = strdup(new_logmsg);
7093 if (logmsg == NULL) {
7094 err = got_error_from_errno("strdup");
7098 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
7103 /* NB: commit_worktree will call free(logmsg) */
7104 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
7105 NULL, worktree, got_object_commit_get_author(orig_commit),
7106 committer ? committer :
7107 got_object_commit_get_committer(orig_commit), NULL,
7108 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
7112 err = got_ref_change_ref(tmp_branch, *new_commit_id);
7116 err = got_ref_delete(commit_ref, repo);
7120 err = update_fileindex_after_commit(worktree, &commitable_paths,
7121 *new_commit_id, fileindex, 0);
7122 sync_err = sync_fileindex(fileindex, fileindex_path);
7123 if (sync_err && err == NULL)
7126 free(fileindex_path);
7127 free(head_commit_id);
7129 got_ref_close(head_ref);
7131 free(*new_commit_id);
7132 *new_commit_id = NULL;
7137 const struct got_error *
7138 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
7139 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7140 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7141 const char *committer, struct got_commit_object *orig_commit,
7142 struct got_object_id *orig_commit_id, int allow_conflict,
7143 struct got_repository *repo)
7145 const struct got_error *err;
7146 char *commit_ref_name;
7147 struct got_reference *commit_ref = NULL;
7148 struct got_object_id *commit_id = NULL;
7150 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7154 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7157 err = got_ref_resolve(&commit_id, repo, commit_ref);
7160 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
7161 err = got_error(GOT_ERR_REBASE_COMMITID);
7165 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7166 worktree, fileindex, tmp_branch, committer, orig_commit,
7167 NULL, allow_conflict, repo);
7170 got_ref_close(commit_ref);
7171 free(commit_ref_name);
7176 const struct got_error *
7177 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
7178 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7179 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7180 const char *committer, struct got_commit_object *orig_commit,
7181 struct got_object_id *orig_commit_id, const char *new_logmsg,
7182 int allow_conflict, struct got_repository *repo)
7184 const struct got_error *err;
7185 char *commit_ref_name;
7186 struct got_reference *commit_ref = NULL;
7188 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7192 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7196 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7197 worktree, fileindex, tmp_branch, committer, orig_commit,
7198 new_logmsg, allow_conflict, repo);
7201 got_ref_close(commit_ref);
7202 free(commit_ref_name);
7206 const struct got_error *
7207 got_worktree_rebase_postpone(struct got_worktree *worktree,
7208 struct got_fileindex *fileindex)
7211 got_fileindex_free(fileindex);
7212 return lock_worktree(worktree, LOCK_SH);
7215 static const struct got_error *
7216 delete_ref(const char *name, struct got_repository *repo)
7218 const struct got_error *err;
7219 struct got_reference *ref;
7221 err = got_ref_open(&ref, repo, name, 0);
7223 if (err->code == GOT_ERR_NOT_REF)
7228 err = got_ref_delete(ref, repo);
7233 static const struct got_error *
7234 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7236 const struct got_error *err;
7237 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7238 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7240 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7243 err = delete_ref(tmp_branch_name, repo);
7247 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7250 err = delete_ref(new_base_branch_ref_name, repo);
7254 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7257 err = delete_ref(branch_ref_name, repo);
7261 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7264 err = delete_ref(commit_ref_name, repo);
7269 free(tmp_branch_name);
7270 free(new_base_branch_ref_name);
7271 free(branch_ref_name);
7272 free(commit_ref_name);
7276 static const struct got_error *
7277 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7278 struct got_object_id *new_commit_id, struct got_repository *repo)
7280 const struct got_error *err;
7281 struct got_reference *ref = NULL;
7282 struct got_object_id *old_commit_id = NULL;
7283 const char *branch_name = NULL;
7284 char *new_id_str = NULL;
7285 char *refname = NULL;
7287 branch_name = got_ref_get_name(branch);
7288 if (strncmp(branch_name, "refs/heads/", 11) != 0)
7289 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7292 err = got_object_id_str(&new_id_str, new_commit_id);
7296 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7297 new_id_str) == -1) {
7298 err = got_error_from_errno("asprintf");
7302 err = got_ref_resolve(&old_commit_id, repo, branch);
7306 err = got_ref_alloc(&ref, refname, old_commit_id);
7310 err = got_ref_write(ref, repo);
7314 free(old_commit_id);
7320 const struct got_error *
7321 got_worktree_rebase_complete(struct got_worktree *worktree,
7322 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7323 struct got_reference *rebased_branch, struct got_repository *repo,
7326 const struct got_error *err, *unlockerr, *sync_err;
7327 struct got_object_id *new_head_commit_id = NULL;
7328 char *fileindex_path = NULL;
7330 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7334 if (create_backup) {
7335 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7336 rebased_branch, new_head_commit_id, repo);
7341 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7345 err = got_ref_write(rebased_branch, repo);
7349 err = got_worktree_set_head_ref(worktree, rebased_branch);
7353 err = delete_rebase_refs(worktree, repo);
7357 err = get_fileindex_path(&fileindex_path, worktree);
7360 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7361 sync_err = sync_fileindex(fileindex, fileindex_path);
7362 if (sync_err && err == NULL)
7365 got_fileindex_free(fileindex);
7366 free(fileindex_path);
7367 free(new_head_commit_id);
7368 unlockerr = lock_worktree(worktree, LOCK_SH);
7369 if (unlockerr && err == NULL)
7374 static const struct got_error *
7375 get_paths_changed_between_commits(struct got_pathlist_head *paths,
7376 struct got_object_id *id1, struct got_object_id *id2,
7377 struct got_repository *repo)
7379 const struct got_error *err;
7380 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
7381 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7384 err = got_object_open_as_commit(&commit1, repo, id1);
7388 err = got_object_open_as_tree(&tree1, repo,
7389 got_object_commit_get_tree_id(commit1));
7395 err = got_object_open_as_commit(&commit2, repo, id2);
7399 err = got_object_open_as_tree(&tree2, repo,
7400 got_object_commit_get_tree_id(commit2));
7405 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
7406 got_diff_tree_collect_changed_paths, paths, 0);
7411 got_object_commit_close(commit1);
7413 got_object_commit_close(commit2);
7415 got_object_tree_close(tree1);
7417 got_object_tree_close(tree2);
7421 static const struct got_error *
7422 get_paths_added_between_commits(struct got_pathlist_head *added_paths,
7423 struct got_object_id *id1, struct got_object_id *id2,
7424 const char *path_prefix, struct got_repository *repo)
7426 const struct got_error *err;
7427 struct got_pathlist_head merged_paths;
7428 struct got_pathlist_entry *pe;
7429 char *abspath = NULL, *wt_path = NULL;
7431 TAILQ_INIT(&merged_paths);
7433 err = get_paths_changed_between_commits(&merged_paths, id1, id2, repo);
7437 TAILQ_FOREACH(pe, &merged_paths, entry) {
7438 struct got_diff_changed_path *change = pe->data;
7440 if (change->status != GOT_STATUS_ADD)
7443 if (got_path_is_root_dir(path_prefix)) {
7444 wt_path = strdup(pe->path);
7445 if (wt_path == NULL) {
7446 err = got_error_from_errno("strdup");
7450 if (asprintf(&abspath, "/%s", pe->path) == -1) {
7451 err = got_error_from_errno("asprintf");
7455 err = got_path_skip_common_ancestor(&wt_path,
7456 path_prefix, abspath);
7463 err = got_pathlist_append(added_paths, wt_path, NULL);
7470 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_ALL);
7476 static const struct got_error *
7477 get_paths_added_in_commit(struct got_pathlist_head *added_paths,
7478 struct got_object_id *id, const char *path_prefix,
7479 struct got_repository *repo)
7481 const struct got_error *err;
7482 struct got_commit_object *commit = NULL;
7483 struct got_object_qid *pid;
7485 err = got_object_open_as_commit(&commit, repo, id);
7489 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7491 err = get_paths_added_between_commits(added_paths,
7492 pid ? &pid->id : NULL, id, path_prefix, repo);
7497 got_object_commit_close(commit);
7501 const struct got_error *
7502 got_worktree_rebase_abort(struct got_worktree *worktree,
7503 struct got_fileindex *fileindex, struct got_repository *repo,
7504 struct got_reference *new_base_branch,
7505 got_worktree_checkout_cb progress_cb, void *progress_arg)
7507 const struct got_error *err, *unlockerr, *sync_err;
7508 struct got_reference *resolved = NULL;
7509 struct got_object_id *commit_id = NULL;
7510 struct got_object_id *merged_commit_id = NULL;
7511 struct got_commit_object *commit = NULL;
7512 char *fileindex_path = NULL;
7513 char *commit_ref_name = NULL;
7514 struct got_reference *commit_ref = NULL;
7515 struct revert_file_args rfa;
7516 struct got_object_id *tree_id = NULL;
7517 struct got_pathlist_head added_paths;
7519 TAILQ_INIT(&added_paths);
7521 err = lock_worktree(worktree, LOCK_EX);
7525 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7529 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7533 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
7538 * Determine which files in added status can be safely removed
7539 * from disk while reverting changes in the work tree.
7540 * We want to avoid deleting unrelated files which were added by
7541 * the user for conflict resolution purposes.
7543 err = get_paths_added_in_commit(&added_paths, merged_commit_id,
7544 got_worktree_get_path_prefix(worktree), repo);
7548 err = got_ref_open(&resolved, repo,
7549 got_ref_get_symref_target(new_base_branch), 0);
7553 err = got_worktree_set_head_ref(worktree, resolved);
7558 * XXX commits to the base branch could have happened while
7559 * we were busy rebasing; should we store the original commit ID
7560 * when rebase begins and read it back here?
7562 err = got_ref_resolve(&commit_id, repo, resolved);
7566 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7570 err = got_object_open_as_commit(&commit, repo,
7571 worktree->base_commit_id);
7575 err = got_object_id_by_path(&tree_id, repo, commit,
7576 worktree->path_prefix);
7580 err = delete_rebase_refs(worktree, repo);
7584 err = get_fileindex_path(&fileindex_path, worktree);
7588 rfa.worktree = worktree;
7589 rfa.fileindex = fileindex;
7590 rfa.progress_cb = progress_cb;
7591 rfa.progress_arg = progress_arg;
7592 rfa.patch_cb = NULL;
7593 rfa.patch_arg = NULL;
7595 rfa.unlink_added_files = 1;
7596 rfa.added_files_to_unlink = &added_paths;
7597 err = worktree_status(worktree, "", fileindex, repo,
7598 revert_file, &rfa, NULL, NULL, 1, 0);
7602 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7603 repo, progress_cb, progress_arg, NULL, NULL);
7605 sync_err = sync_fileindex(fileindex, fileindex_path);
7606 if (sync_err && err == NULL)
7609 got_pathlist_free(&added_paths, GOT_PATHLIST_FREE_PATH);
7610 got_ref_close(resolved);
7613 free(merged_commit_id);
7615 got_object_commit_close(commit);
7617 got_fileindex_free(fileindex);
7618 free(fileindex_path);
7619 free(commit_ref_name);
7621 got_ref_close(commit_ref);
7623 unlockerr = lock_worktree(worktree, LOCK_SH);
7624 if (unlockerr && err == NULL)
7629 const struct got_error *
7630 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7631 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7632 struct got_fileindex **fileindex, struct got_worktree *worktree,
7633 struct got_repository *repo)
7635 const struct got_error *err = NULL;
7636 char *tmp_branch_name = NULL;
7637 char *branch_ref_name = NULL;
7638 char *base_commit_ref_name = NULL;
7639 char *fileindex_path = NULL;
7640 struct check_rebase_ok_arg ok_arg;
7641 struct got_reference *wt_branch = NULL;
7642 struct got_reference *base_commit_ref = NULL;
7646 *base_commit_id = NULL;
7649 err = lock_worktree(worktree, LOCK_EX);
7653 err = open_fileindex(fileindex, &fileindex_path, worktree);
7657 ok_arg.worktree = worktree;
7659 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7664 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7668 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7672 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7677 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7682 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7686 err = got_ref_write(*branch_ref, repo);
7690 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7691 worktree->base_commit_id);
7694 err = got_ref_write(base_commit_ref, repo);
7697 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7698 if (*base_commit_id == NULL) {
7699 err = got_error_from_errno("got_object_id_dup");
7703 err = got_ref_alloc(tmp_branch, tmp_branch_name,
7704 worktree->base_commit_id);
7707 err = got_ref_write(*tmp_branch, repo);
7711 err = got_worktree_set_head_ref(worktree, *tmp_branch);
7715 free(fileindex_path);
7716 free(tmp_branch_name);
7717 free(branch_ref_name);
7718 free(base_commit_ref_name);
7720 got_ref_close(wt_branch);
7723 got_ref_close(*branch_ref);
7727 got_ref_close(*tmp_branch);
7730 free(*base_commit_id);
7732 got_fileindex_free(*fileindex);
7735 lock_worktree(worktree, LOCK_SH);
7740 const struct got_error *
7741 got_worktree_histedit_postpone(struct got_worktree *worktree,
7742 struct got_fileindex *fileindex)
7745 got_fileindex_free(fileindex);
7746 return lock_worktree(worktree, LOCK_SH);
7749 const struct got_error *
7750 got_worktree_histedit_in_progress(int *in_progress,
7751 struct got_worktree *worktree)
7753 const struct got_error *err;
7754 char *tmp_branch_name = NULL;
7756 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7760 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7761 free(tmp_branch_name);
7765 const struct got_error *
7766 got_worktree_histedit_continue(struct got_object_id **commit_id,
7767 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7768 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7769 struct got_worktree *worktree, struct got_repository *repo)
7771 const struct got_error *err;
7772 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7773 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7774 struct got_reference *commit_ref = NULL;
7775 struct got_reference *base_commit_ref = NULL;
7776 char *fileindex_path = NULL;
7777 int have_staged_files = 0;
7781 *base_commit_id = NULL;
7784 err = lock_worktree(worktree, LOCK_EX);
7788 err = open_fileindex(fileindex, &fileindex_path, worktree);
7792 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7793 &have_staged_files);
7794 if (err && err->code != GOT_ERR_CANCELLED)
7796 if (have_staged_files) {
7797 err = got_error(GOT_ERR_STAGED_PATHS);
7801 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7805 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7809 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7813 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7818 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7822 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7825 err = got_ref_resolve(commit_id, repo, commit_ref);
7829 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7832 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7836 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7840 free(commit_ref_name);
7841 free(branch_ref_name);
7842 free(fileindex_path);
7844 got_ref_close(commit_ref);
7845 if (base_commit_ref)
7846 got_ref_close(base_commit_ref);
7850 free(*base_commit_id);
7851 *base_commit_id = NULL;
7853 got_ref_close(*tmp_branch);
7857 got_fileindex_free(*fileindex);
7860 lock_worktree(worktree, LOCK_EX);
7865 static const struct got_error *
7866 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7868 const struct got_error *err;
7869 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7870 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7872 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7875 err = delete_ref(tmp_branch_name, repo);
7879 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7883 err = delete_ref(base_commit_ref_name, repo);
7887 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7890 err = delete_ref(branch_ref_name, repo);
7894 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7897 err = delete_ref(commit_ref_name, repo);
7901 free(tmp_branch_name);
7902 free(base_commit_ref_name);
7903 free(branch_ref_name);
7904 free(commit_ref_name);
7908 const struct got_error *
7909 got_worktree_histedit_abort(struct got_worktree *worktree,
7910 struct got_fileindex *fileindex, struct got_repository *repo,
7911 struct got_reference *branch, struct got_object_id *base_commit_id,
7912 got_worktree_checkout_cb progress_cb, void *progress_arg)
7914 const struct got_error *err, *unlockerr, *sync_err;
7915 struct got_reference *resolved = NULL;
7916 char *fileindex_path = NULL;
7917 struct got_object_id *merged_commit_id = NULL;
7918 struct got_commit_object *commit = NULL;
7919 char *commit_ref_name = NULL;
7920 struct got_reference *commit_ref = NULL;
7921 struct got_object_id *tree_id = NULL;
7922 struct revert_file_args rfa;
7923 struct got_pathlist_head added_paths;
7925 TAILQ_INIT(&added_paths);
7927 err = lock_worktree(worktree, LOCK_EX);
7931 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7935 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7937 if (err->code != GOT_ERR_NOT_REF)
7939 /* Can happen on early abort due to invalid histedit script. */
7944 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
7949 * Determine which files in added status can be safely removed
7950 * from disk while reverting changes in the work tree.
7951 * We want to avoid deleting unrelated files added by the
7952 * user during conflict resolution or during histedit -e.
7954 err = get_paths_added_in_commit(&added_paths, merged_commit_id,
7955 got_worktree_get_path_prefix(worktree), repo);
7960 err = got_ref_open(&resolved, repo,
7961 got_ref_get_symref_target(branch), 0);
7965 err = got_worktree_set_head_ref(worktree, resolved);
7969 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7973 err = got_object_open_as_commit(&commit, repo,
7974 worktree->base_commit_id);
7978 err = got_object_id_by_path(&tree_id, repo, commit,
7979 worktree->path_prefix);
7983 err = delete_histedit_refs(worktree, repo);
7987 err = get_fileindex_path(&fileindex_path, worktree);
7991 rfa.worktree = worktree;
7992 rfa.fileindex = fileindex;
7993 rfa.progress_cb = progress_cb;
7994 rfa.progress_arg = progress_arg;
7995 rfa.patch_cb = NULL;
7996 rfa.patch_arg = NULL;
7998 rfa.unlink_added_files = 1;
7999 rfa.added_files_to_unlink = &added_paths;
8000 err = worktree_status(worktree, "", fileindex, repo,
8001 revert_file, &rfa, NULL, NULL, 1, 0);
8005 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8006 repo, progress_cb, progress_arg, NULL, NULL);
8008 sync_err = sync_fileindex(fileindex, fileindex_path);
8009 if (sync_err && err == NULL)
8013 got_ref_close(resolved);
8015 got_ref_close(commit_ref);
8016 free(merged_commit_id);
8018 free(fileindex_path);
8019 free(commit_ref_name);
8021 unlockerr = lock_worktree(worktree, LOCK_SH);
8022 if (unlockerr && err == NULL)
8027 const struct got_error *
8028 got_worktree_histedit_complete(struct got_worktree *worktree,
8029 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8030 struct got_reference *edited_branch, struct got_repository *repo)
8032 const struct got_error *err, *unlockerr, *sync_err;
8033 struct got_object_id *new_head_commit_id = NULL;
8034 struct got_reference *resolved = NULL;
8035 char *fileindex_path = NULL;
8037 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
8041 err = got_ref_open(&resolved, repo,
8042 got_ref_get_symref_target(edited_branch), 0);
8046 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
8047 resolved, new_head_commit_id, repo);
8051 err = got_ref_change_ref(resolved, new_head_commit_id);
8055 err = got_ref_write(resolved, repo);
8059 err = got_worktree_set_head_ref(worktree, resolved);
8063 err = delete_histedit_refs(worktree, repo);
8067 err = get_fileindex_path(&fileindex_path, worktree);
8070 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8071 sync_err = sync_fileindex(fileindex, fileindex_path);
8072 if (sync_err && err == NULL)
8075 got_fileindex_free(fileindex);
8076 free(fileindex_path);
8077 free(new_head_commit_id);
8078 unlockerr = lock_worktree(worktree, LOCK_SH);
8079 if (unlockerr && err == NULL)
8084 const struct got_error *
8085 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
8086 struct got_object_id *commit_id, struct got_repository *repo)
8088 const struct got_error *err;
8089 char *commit_ref_name;
8091 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8095 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
8099 err = delete_ref(commit_ref_name, repo);
8101 free(commit_ref_name);
8105 const struct got_error *
8106 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
8107 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
8108 struct got_worktree *worktree, const char *refname,
8109 struct got_repository *repo)
8111 const struct got_error *err = NULL;
8112 char *fileindex_path = NULL;
8113 struct check_rebase_ok_arg ok_arg;
8117 *base_branch_ref = NULL;
8119 err = lock_worktree(worktree, LOCK_EX);
8123 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
8124 err = got_error_msg(GOT_ERR_SAME_BRANCH,
8125 "cannot integrate a branch into itself; "
8126 "update -b or different branch name required");
8130 err = open_fileindex(fileindex, &fileindex_path, worktree);
8134 /* Preconditions are the same as for rebase. */
8135 ok_arg.worktree = worktree;
8137 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8142 err = got_ref_open(branch_ref, repo, refname, 1);
8146 err = got_ref_open(base_branch_ref, repo,
8147 got_worktree_get_head_ref_name(worktree), 1);
8151 got_ref_close(*branch_ref);
8154 if (*base_branch_ref) {
8155 got_ref_close(*base_branch_ref);
8156 *base_branch_ref = NULL;
8159 got_fileindex_free(*fileindex);
8162 lock_worktree(worktree, LOCK_SH);
8167 const struct got_error *
8168 got_worktree_integrate_continue(struct got_worktree *worktree,
8169 struct got_fileindex *fileindex, struct got_repository *repo,
8170 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
8171 got_worktree_checkout_cb progress_cb, void *progress_arg,
8172 got_cancel_cb cancel_cb, void *cancel_arg)
8174 const struct got_error *err = NULL, *sync_err, *unlockerr;
8175 char *fileindex_path = NULL;
8176 struct got_object_id *tree_id = NULL, *commit_id = NULL;
8177 struct got_commit_object *commit = NULL;
8179 err = get_fileindex_path(&fileindex_path, worktree);
8183 err = got_ref_resolve(&commit_id, repo, branch_ref);
8187 err = got_object_open_as_commit(&commit, repo, commit_id);
8191 err = got_object_id_by_path(&tree_id, repo, commit,
8192 worktree->path_prefix);
8196 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
8200 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
8201 progress_cb, progress_arg, cancel_cb, cancel_arg);
8205 err = got_ref_change_ref(base_branch_ref, commit_id);
8209 err = got_ref_write(base_branch_ref, repo);
8213 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8215 sync_err = sync_fileindex(fileindex, fileindex_path);
8216 if (sync_err && err == NULL)
8220 unlockerr = got_ref_unlock(branch_ref);
8221 if (unlockerr && err == NULL)
8223 got_ref_close(branch_ref);
8225 unlockerr = got_ref_unlock(base_branch_ref);
8226 if (unlockerr && err == NULL)
8228 got_ref_close(base_branch_ref);
8230 got_fileindex_free(fileindex);
8231 free(fileindex_path);
8234 got_object_commit_close(commit);
8236 unlockerr = lock_worktree(worktree, LOCK_SH);
8237 if (unlockerr && err == NULL)
8242 const struct got_error *
8243 got_worktree_integrate_abort(struct got_worktree *worktree,
8244 struct got_fileindex *fileindex, struct got_repository *repo,
8245 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
8247 const struct got_error *err = NULL, *unlockerr = NULL;
8249 got_fileindex_free(fileindex);
8251 err = lock_worktree(worktree, LOCK_SH);
8253 unlockerr = got_ref_unlock(branch_ref);
8254 if (unlockerr && err == NULL)
8256 got_ref_close(branch_ref);
8258 unlockerr = got_ref_unlock(base_branch_ref);
8259 if (unlockerr && err == NULL)
8261 got_ref_close(base_branch_ref);
8266 const struct got_error *
8267 got_worktree_merge_postpone(struct got_worktree *worktree,
8268 struct got_fileindex *fileindex)
8270 const struct got_error *err, *sync_err;
8271 char *fileindex_path = NULL;
8273 err = get_fileindex_path(&fileindex_path, worktree);
8277 sync_err = sync_fileindex(fileindex, fileindex_path);
8279 err = lock_worktree(worktree, LOCK_SH);
8280 if (sync_err && err == NULL)
8283 got_fileindex_free(fileindex);
8284 free(fileindex_path);
8288 static const struct got_error *
8289 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
8291 const struct got_error *err;
8292 char *branch_refname = NULL, *commit_refname = NULL;
8294 err = get_merge_branch_ref_name(&branch_refname, worktree);
8297 err = delete_ref(branch_refname, repo);
8301 err = get_merge_commit_ref_name(&commit_refname, worktree);
8304 err = delete_ref(commit_refname, repo);
8309 free(branch_refname);
8310 free(commit_refname);
8314 struct merge_commit_msg_arg {
8315 struct got_worktree *worktree;
8316 const char *branch_name;
8319 static const struct got_error *
8320 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
8321 const char *diff_path, char **logmsg, void *arg)
8323 struct merge_commit_msg_arg *a = arg;
8325 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
8326 got_worktree_get_head_ref_name(a->worktree)) == -1)
8327 return got_error_from_errno("asprintf");
8333 const struct got_error *
8334 got_worktree_merge_branch(struct got_worktree *worktree,
8335 struct got_fileindex *fileindex,
8336 struct got_object_id *yca_commit_id,
8337 struct got_object_id *branch_tip,
8338 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
8339 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
8341 const struct got_error *err;
8342 char *fileindex_path = NULL;
8344 err = get_fileindex_path(&fileindex_path, worktree);
8348 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
8353 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
8354 branch_tip, repo, progress_cb, progress_arg,
8355 cancel_cb, cancel_arg);
8357 free(fileindex_path);
8361 const struct got_error *
8362 got_worktree_merge_commit(struct got_object_id **new_commit_id,
8363 struct got_worktree *worktree, struct got_fileindex *fileindex,
8364 const char *author, const char *committer, int allow_bad_symlinks,
8365 struct got_object_id *branch_tip, const char *branch_name,
8366 int allow_conflict, struct got_repository *repo,
8367 got_worktree_status_cb status_cb, void *status_arg)
8370 const struct got_error *err = NULL, *sync_err;
8371 struct got_pathlist_head commitable_paths;
8372 struct collect_commitables_arg cc_arg;
8373 struct got_pathlist_entry *pe;
8374 struct got_reference *head_ref = NULL;
8375 struct got_object_id *head_commit_id = NULL;
8376 int have_staged_files = 0;
8377 struct merge_commit_msg_arg mcm_arg;
8378 char *fileindex_path = NULL;
8380 memset(&cc_arg, 0, sizeof(cc_arg));
8381 *new_commit_id = NULL;
8383 TAILQ_INIT(&commitable_paths);
8385 err = get_fileindex_path(&fileindex_path, worktree);
8389 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
8393 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8397 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
8398 &have_staged_files);
8399 if (err && err->code != GOT_ERR_CANCELLED)
8401 if (have_staged_files) {
8402 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
8406 cc_arg.commitable_paths = &commitable_paths;
8407 cc_arg.worktree = worktree;
8408 cc_arg.fileindex = fileindex;
8410 cc_arg.have_staged_files = have_staged_files;
8411 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
8412 cc_arg.commit_conflicts = allow_conflict;
8413 err = worktree_status(worktree, "", fileindex, repo,
8414 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8418 mcm_arg.worktree = worktree;
8419 mcm_arg.branch_name = branch_name;
8420 err = commit_worktree(new_commit_id, &commitable_paths,
8421 head_commit_id, branch_tip, worktree, author, committer, NULL,
8422 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8426 err = update_fileindex_after_commit(worktree, &commitable_paths,
8427 *new_commit_id, fileindex, have_staged_files);
8428 sync_err = sync_fileindex(fileindex, fileindex_path);
8429 if (sync_err && err == NULL)
8432 TAILQ_FOREACH(pe, &commitable_paths, entry) {
8433 struct got_commitable *ct = pe->data;
8435 free_commitable(ct);
8437 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8438 free(fileindex_path);
8442 const struct got_error *
8443 got_worktree_merge_complete(struct got_worktree *worktree,
8444 struct got_fileindex *fileindex, struct got_repository *repo)
8446 const struct got_error *err, *unlockerr, *sync_err;
8447 char *fileindex_path = NULL;
8449 err = delete_merge_refs(worktree, repo);
8453 err = get_fileindex_path(&fileindex_path, worktree);
8456 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8457 sync_err = sync_fileindex(fileindex, fileindex_path);
8458 if (sync_err && err == NULL)
8461 got_fileindex_free(fileindex);
8462 free(fileindex_path);
8463 unlockerr = lock_worktree(worktree, LOCK_SH);
8464 if (unlockerr && err == NULL)
8469 const struct got_error *
8470 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8471 struct got_repository *repo)
8473 const struct got_error *err;
8474 char *branch_refname = NULL;
8475 struct got_reference *branch_ref = NULL;
8479 err = get_merge_branch_ref_name(&branch_refname, worktree);
8482 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8483 free(branch_refname);
8485 if (err->code != GOT_ERR_NOT_REF)
8493 const struct got_error *got_worktree_merge_prepare(
8494 struct got_fileindex **fileindex, struct got_worktree *worktree,
8495 struct got_repository *repo)
8497 const struct got_error *err = NULL;
8498 char *fileindex_path = NULL;
8499 struct got_reference *wt_branch = NULL;
8500 struct got_object_id *wt_branch_tip = NULL;
8501 struct check_rebase_ok_arg ok_arg;
8505 err = lock_worktree(worktree, LOCK_EX);
8509 err = open_fileindex(fileindex, &fileindex_path, worktree);
8513 /* Preconditions are the same as for rebase. */
8514 ok_arg.worktree = worktree;
8516 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8521 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8526 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8530 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8531 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8536 free(fileindex_path);
8538 got_ref_close(wt_branch);
8539 free(wt_branch_tip);
8542 got_fileindex_free(*fileindex);
8545 lock_worktree(worktree, LOCK_SH);
8550 const struct got_error *got_worktree_merge_write_refs(
8551 struct got_worktree *worktree, struct got_reference *branch,
8552 struct got_repository *repo)
8554 const struct got_error *err = NULL;
8555 char *branch_refname = NULL, *commit_refname = NULL;
8556 struct got_reference *branch_ref = NULL, *commit_ref = NULL;
8557 struct got_object_id *branch_tip = NULL;
8559 err = get_merge_branch_ref_name(&branch_refname, worktree);
8563 err = get_merge_commit_ref_name(&commit_refname, worktree);
8567 err = got_ref_resolve(&branch_tip, repo, branch);
8571 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8574 err = got_ref_write(branch_ref, repo);
8578 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8581 err = got_ref_write(commit_ref, repo);
8586 free(branch_refname);
8587 free(commit_refname);
8589 got_ref_close(branch_ref);
8591 got_ref_close(commit_ref);
8596 const struct got_error *
8597 got_worktree_merge_continue(char **branch_name,
8598 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8599 struct got_worktree *worktree, struct got_repository *repo)
8601 const struct got_error *err;
8602 char *commit_refname = NULL, *branch_refname = NULL;
8603 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8604 char *fileindex_path = NULL;
8605 int have_staged_files = 0;
8607 *branch_name = NULL;
8611 err = lock_worktree(worktree, LOCK_EX);
8615 err = open_fileindex(fileindex, &fileindex_path, worktree);
8619 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8620 &have_staged_files);
8621 if (err && err->code != GOT_ERR_CANCELLED)
8623 if (have_staged_files) {
8624 err = got_error(GOT_ERR_STAGED_PATHS);
8628 err = get_merge_branch_ref_name(&branch_refname, worktree);
8632 err = get_merge_commit_ref_name(&commit_refname, worktree);
8636 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8640 if (!got_ref_is_symbolic(branch_ref)) {
8641 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8642 "%s is not a symbolic reference",
8643 got_ref_get_name(branch_ref));
8646 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8647 if (*branch_name == NULL) {
8648 err = got_error_from_errno("strdup");
8652 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8656 err = got_ref_resolve(branch_tip, repo, commit_ref);
8660 free(commit_refname);
8661 free(branch_refname);
8662 free(fileindex_path);
8664 got_ref_close(commit_ref);
8666 got_ref_close(branch_ref);
8670 *branch_name = NULL;
8675 got_fileindex_free(*fileindex);
8678 lock_worktree(worktree, LOCK_SH);
8683 const struct got_error *
8684 got_worktree_merge_abort(struct got_worktree *worktree,
8685 struct got_fileindex *fileindex, struct got_repository *repo,
8686 got_worktree_checkout_cb progress_cb, void *progress_arg)
8688 const struct got_error *err, *unlockerr, *sync_err;
8689 struct got_commit_object *commit = NULL;
8690 char *fileindex_path = NULL;
8691 struct revert_file_args rfa;
8692 char *commit_ref_name = NULL;
8693 struct got_reference *commit_ref = NULL;
8694 struct got_object_id *merged_commit_id = NULL;
8695 struct got_object_id *tree_id = NULL;
8696 struct got_pathlist_head added_paths;
8698 TAILQ_INIT(&added_paths);
8700 err = get_merge_commit_ref_name(&commit_ref_name, worktree);
8704 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8708 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8713 * Determine which files in added status can be safely removed
8714 * from disk while reverting changes in the work tree.
8715 * We want to avoid deleting unrelated files which were added by
8716 * the user for conflict resolution purposes.
8718 err = get_paths_added_between_commits(&added_paths,
8719 got_worktree_get_base_commit_id(worktree), merged_commit_id,
8720 got_worktree_get_path_prefix(worktree), repo);
8725 err = got_object_open_as_commit(&commit, repo,
8726 worktree->base_commit_id);
8730 err = got_object_id_by_path(&tree_id, repo, commit,
8731 worktree->path_prefix);
8735 err = delete_merge_refs(worktree, repo);
8739 err = get_fileindex_path(&fileindex_path, worktree);
8743 rfa.worktree = worktree;
8744 rfa.fileindex = fileindex;
8745 rfa.progress_cb = progress_cb;
8746 rfa.progress_arg = progress_arg;
8747 rfa.patch_cb = NULL;
8748 rfa.patch_arg = NULL;
8750 rfa.unlink_added_files = 1;
8751 rfa.added_files_to_unlink = &added_paths;
8752 err = worktree_status(worktree, "", fileindex, repo,
8753 revert_file, &rfa, NULL, NULL, 1, 0);
8757 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8758 repo, progress_cb, progress_arg, NULL, NULL);
8760 sync_err = sync_fileindex(fileindex, fileindex_path);
8761 if (sync_err && err == NULL)
8765 free(merged_commit_id);
8767 got_object_commit_close(commit);
8769 got_fileindex_free(fileindex);
8770 free(fileindex_path);
8772 got_ref_close(commit_ref);
8773 free(commit_ref_name);
8775 unlockerr = lock_worktree(worktree, LOCK_SH);
8776 if (unlockerr && err == NULL)
8781 struct check_stage_ok_arg {
8782 struct got_object_id *head_commit_id;
8783 struct got_worktree *worktree;
8784 struct got_fileindex *fileindex;
8785 struct got_repository *repo;
8789 static const struct got_error *
8790 check_stage_ok(void *arg, unsigned char status,
8791 unsigned char staged_status, const char *relpath,
8792 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8793 struct got_object_id *commit_id, int dirfd, const char *de_name)
8795 struct check_stage_ok_arg *a = arg;
8796 const struct got_error *err = NULL;
8797 struct got_fileindex_entry *ie;
8798 struct got_object_id base_commit_id;
8799 struct got_object_id *base_commit_idp = NULL;
8800 char *in_repo_path = NULL, *p;
8802 if (status == GOT_STATUS_UNVERSIONED ||
8803 status == GOT_STATUS_NO_CHANGE)
8805 if (status == GOT_STATUS_NONEXISTENT)
8806 return got_error_set_errno(ENOENT, relpath);
8808 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8810 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8812 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8813 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8815 return got_error_from_errno("asprintf");
8817 if (got_fileindex_entry_has_commit(ie)) {
8818 base_commit_idp = got_fileindex_entry_get_commit_id(
8819 &base_commit_id, ie);
8822 if (status == GOT_STATUS_CONFLICT) {
8823 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8825 } else if (status != GOT_STATUS_ADD &&
8826 status != GOT_STATUS_MODIFY &&
8827 status != GOT_STATUS_DELETE) {
8828 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8832 a->have_changes = 1;
8837 err = check_out_of_date(p, status, staged_status,
8838 blob_id, base_commit_idp, a->head_commit_id, a->repo,
8839 GOT_ERR_STAGE_OUT_OF_DATE);
8845 struct stage_path_arg {
8846 struct got_worktree *worktree;
8847 struct got_fileindex *fileindex;
8848 struct got_repository *repo;
8849 got_worktree_status_cb status_cb;
8851 got_worktree_patch_cb patch_cb;
8853 int staged_something;
8854 int allow_bad_symlinks;
8857 static const struct got_error *
8858 stage_path(void *arg, unsigned char status,
8859 unsigned char staged_status, const char *relpath,
8860 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8861 struct got_object_id *commit_id, int dirfd, const char *de_name)
8863 struct stage_path_arg *a = arg;
8864 const struct got_error *err = NULL;
8865 struct got_fileindex_entry *ie;
8866 char *ondisk_path = NULL, *path_content = NULL;
8868 struct got_object_id *new_staged_blob_id = NULL;
8871 if (status == GOT_STATUS_UNVERSIONED)
8874 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8876 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8878 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8880 return got_error_from_errno("asprintf");
8883 case GOT_STATUS_ADD:
8884 case GOT_STATUS_MODIFY:
8885 /* XXX could sb.st_mode be passed in by our caller? */
8886 if (lstat(ondisk_path, &sb) == -1) {
8887 err = got_error_from_errno2("lstat", ondisk_path);
8891 if (status == GOT_STATUS_ADD) {
8892 int choice = GOT_PATCH_CHOICE_NONE;
8893 err = (*a->patch_cb)(&choice, a->patch_arg,
8894 status, ie->path, NULL, 1, 1);
8897 if (choice != GOT_PATCH_CHOICE_YES)
8900 err = create_patched_content(&path_content, 0,
8901 staged_blob_id ? staged_blob_id : blob_id,
8902 ondisk_path, dirfd, de_name, ie->path,
8903 a->repo, a->patch_cb, a->patch_arg);
8904 if (err || path_content == NULL)
8908 err = got_object_blob_create(&new_staged_blob_id,
8909 path_content ? path_content : ondisk_path, a->repo);
8912 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8913 SHA1_DIGEST_LENGTH);
8914 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8915 stage = GOT_FILEIDX_STAGE_ADD;
8917 stage = GOT_FILEIDX_STAGE_MODIFY;
8918 got_fileindex_entry_stage_set(ie, stage);
8919 if (S_ISLNK(sb.st_mode)) {
8920 int is_bad_symlink = 0;
8921 if (!a->allow_bad_symlinks) {
8922 char target_path[PATH_MAX];
8924 target_len = readlink(ondisk_path, target_path,
8925 sizeof(target_path));
8926 if (target_len == -1) {
8927 err = got_error_from_errno2("readlink",
8931 err = is_bad_symlink_target(&is_bad_symlink,
8932 target_path, target_len, ondisk_path,
8933 a->worktree->root_path);
8936 if (is_bad_symlink) {
8937 err = got_error_path(ondisk_path,
8938 GOT_ERR_BAD_SYMLINK);
8943 got_fileindex_entry_staged_filetype_set(ie,
8944 GOT_FILEIDX_MODE_BAD_SYMLINK);
8946 got_fileindex_entry_staged_filetype_set(ie,
8947 GOT_FILEIDX_MODE_SYMLINK);
8949 got_fileindex_entry_staged_filetype_set(ie,
8950 GOT_FILEIDX_MODE_REGULAR_FILE);
8952 a->staged_something = 1;
8953 if (a->status_cb == NULL)
8955 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8956 get_staged_status(ie), relpath, blob_id,
8957 new_staged_blob_id, NULL, dirfd, de_name);
8961 * When staging the reverse of the staged diff,
8962 * implicitly unstage the file.
8964 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8965 sizeof(ie->blob_sha1)) == 0) {
8966 got_fileindex_entry_stage_set(ie,
8967 GOT_FILEIDX_STAGE_NONE);
8970 case GOT_STATUS_DELETE:
8971 if (staged_status == GOT_STATUS_DELETE)
8974 int choice = GOT_PATCH_CHOICE_NONE;
8975 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8976 ie->path, NULL, 1, 1);
8979 if (choice == GOT_PATCH_CHOICE_NO)
8981 if (choice != GOT_PATCH_CHOICE_YES) {
8982 err = got_error(GOT_ERR_PATCH_CHOICE);
8986 stage = GOT_FILEIDX_STAGE_DELETE;
8987 got_fileindex_entry_stage_set(ie, stage);
8988 a->staged_something = 1;
8989 if (a->status_cb == NULL)
8991 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8992 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8995 case GOT_STATUS_NO_CHANGE:
8997 case GOT_STATUS_CONFLICT:
8998 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
9000 case GOT_STATUS_NONEXISTENT:
9001 err = got_error_set_errno(ENOENT, relpath);
9004 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
9008 if (path_content && unlink(path_content) == -1 && err == NULL)
9009 err = got_error_from_errno2("unlink", path_content);
9012 free(new_staged_blob_id);
9016 const struct got_error *
9017 got_worktree_stage(struct got_worktree *worktree,
9018 struct got_pathlist_head *paths,
9019 got_worktree_status_cb status_cb, void *status_arg,
9020 got_worktree_patch_cb patch_cb, void *patch_arg,
9021 int allow_bad_symlinks, struct got_repository *repo)
9023 const struct got_error *err = NULL, *sync_err, *unlockerr;
9024 struct got_pathlist_entry *pe;
9025 struct got_fileindex *fileindex = NULL;
9026 char *fileindex_path = NULL;
9027 struct got_reference *head_ref = NULL;
9028 struct got_object_id *head_commit_id = NULL;
9029 struct check_stage_ok_arg oka;
9030 struct stage_path_arg spa;
9032 err = lock_worktree(worktree, LOCK_EX);
9036 err = got_ref_open(&head_ref, repo,
9037 got_worktree_get_head_ref_name(worktree), 0);
9040 err = got_ref_resolve(&head_commit_id, repo, head_ref);
9043 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9047 /* Check pre-conditions before staging anything. */
9048 oka.head_commit_id = head_commit_id;
9049 oka.worktree = worktree;
9050 oka.fileindex = fileindex;
9052 oka.have_changes = 0;
9053 TAILQ_FOREACH(pe, paths, entry) {
9054 err = worktree_status(worktree, pe->path, fileindex, repo,
9055 check_stage_ok, &oka, NULL, NULL, 1, 0);
9059 if (!oka.have_changes) {
9060 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9064 spa.worktree = worktree;
9065 spa.fileindex = fileindex;
9067 spa.patch_cb = patch_cb;
9068 spa.patch_arg = patch_arg;
9069 spa.status_cb = status_cb;
9070 spa.status_arg = status_arg;
9071 spa.staged_something = 0;
9072 spa.allow_bad_symlinks = allow_bad_symlinks;
9073 TAILQ_FOREACH(pe, paths, entry) {
9074 err = worktree_status(worktree, pe->path, fileindex, repo,
9075 stage_path, &spa, NULL, NULL, 1, 0);
9079 if (!spa.staged_something) {
9080 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9084 sync_err = sync_fileindex(fileindex, fileindex_path);
9085 if (sync_err && err == NULL)
9089 got_ref_close(head_ref);
9090 free(head_commit_id);
9091 free(fileindex_path);
9093 got_fileindex_free(fileindex);
9094 unlockerr = lock_worktree(worktree, LOCK_SH);
9095 if (unlockerr && err == NULL)
9100 struct unstage_path_arg {
9101 struct got_worktree *worktree;
9102 struct got_fileindex *fileindex;
9103 struct got_repository *repo;
9104 got_worktree_checkout_cb progress_cb;
9106 got_worktree_patch_cb patch_cb;
9110 static const struct got_error *
9111 create_unstaged_content(char **path_unstaged_content,
9112 char **path_new_staged_content, struct got_object_id *blob_id,
9113 struct got_object_id *staged_blob_id, const char *relpath,
9114 struct got_repository *repo,
9115 got_worktree_patch_cb patch_cb, void *patch_arg)
9117 const struct got_error *err, *free_err;
9118 struct got_blob_object *blob = NULL, *staged_blob = NULL;
9119 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
9120 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
9121 struct got_diffreg_result *diffreg_result = NULL;
9122 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
9123 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
9124 int fd1 = -1, fd2 = -1;
9126 *path_unstaged_content = NULL;
9127 *path_new_staged_content = NULL;
9129 err = got_object_id_str(&label1, blob_id);
9133 fd1 = got_opentempfd();
9135 err = got_error_from_errno("got_opentempfd");
9138 fd2 = got_opentempfd();
9140 err = got_error_from_errno("got_opentempfd");
9144 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
9148 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
9152 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
9156 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
9161 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
9165 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
9169 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
9170 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
9174 err = got_opentemp_named(path_unstaged_content, &outfile,
9175 "got-unstaged-content", "");
9178 err = got_opentemp_named(path_new_staged_content, &rejectfile,
9179 "got-new-staged-content", "");
9183 if (fseek(f1, 0L, SEEK_SET) == -1) {
9184 err = got_ferror(f1, GOT_ERR_IO);
9187 if (fseek(f2, 0L, SEEK_SET) == -1) {
9188 err = got_ferror(f2, GOT_ERR_IO);
9191 /* Count the number of actual changes in the diff result. */
9192 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9193 struct diff_chunk_context cc = {};
9194 diff_chunk_context_load_change(&cc, &nchunks_used,
9195 diffreg_result->result, n, 0);
9198 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9200 err = apply_or_reject_change(&choice, &nchunks_used,
9201 diffreg_result->result, n, relpath, f1, f2,
9202 &line_cur1, &line_cur2,
9203 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
9206 if (choice == GOT_PATCH_CHOICE_YES)
9209 have_rejected_content = 1;
9210 if (choice == GOT_PATCH_CHOICE_QUIT)
9213 if (have_content || have_rejected_content)
9214 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
9215 outfile, rejectfile);
9218 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9219 err = got_error_from_errno("close");
9221 got_object_blob_close(blob);
9222 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9223 err = got_error_from_errno("close");
9225 got_object_blob_close(staged_blob);
9226 free_err = got_diffreg_result_free(diffreg_result);
9227 if (free_err && err == NULL)
9229 if (f1 && fclose(f1) == EOF && err == NULL)
9230 err = got_error_from_errno2("fclose", path1);
9231 if (f2 && fclose(f2) == EOF && err == NULL)
9232 err = got_error_from_errno2("fclose", path2);
9233 if (outfile && fclose(outfile) == EOF && err == NULL)
9234 err = got_error_from_errno2("fclose", *path_unstaged_content);
9235 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
9236 err = got_error_from_errno2("fclose", *path_new_staged_content);
9237 if (path1 && unlink(path1) == -1 && err == NULL)
9238 err = got_error_from_errno2("unlink", path1);
9239 if (path2 && unlink(path2) == -1 && err == NULL)
9240 err = got_error_from_errno2("unlink", path2);
9241 if (err || !have_content) {
9242 if (*path_unstaged_content &&
9243 unlink(*path_unstaged_content) == -1 && err == NULL)
9244 err = got_error_from_errno2("unlink",
9245 *path_unstaged_content);
9246 free(*path_unstaged_content);
9247 *path_unstaged_content = NULL;
9249 if (err || !have_content || !have_rejected_content) {
9250 if (*path_new_staged_content &&
9251 unlink(*path_new_staged_content) == -1 && err == NULL)
9252 err = got_error_from_errno2("unlink",
9253 *path_new_staged_content);
9254 free(*path_new_staged_content);
9255 *path_new_staged_content = NULL;
9262 static const struct got_error *
9263 unstage_hunks(struct got_object_id *staged_blob_id,
9264 struct got_blob_object *blob_base,
9265 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
9266 const char *ondisk_path, const char *label_orig,
9267 struct got_worktree *worktree, struct got_repository *repo,
9268 got_worktree_patch_cb patch_cb, void *patch_arg,
9269 got_worktree_checkout_cb progress_cb, void *progress_arg)
9271 const struct got_error *err = NULL;
9272 char *path_unstaged_content = NULL;
9273 char *path_new_staged_content = NULL;
9274 char *parent = NULL, *base_path = NULL;
9275 char *blob_base_path = NULL;
9276 struct got_object_id *new_staged_blob_id = NULL;
9277 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
9280 err = create_unstaged_content(&path_unstaged_content,
9281 &path_new_staged_content, blob_id, staged_blob_id,
9282 ie->path, repo, patch_cb, patch_arg);
9286 if (path_unstaged_content == NULL)
9289 if (path_new_staged_content) {
9290 err = got_object_blob_create(&new_staged_blob_id,
9291 path_new_staged_content, repo);
9296 f = fopen(path_unstaged_content, "re");
9298 err = got_error_from_errno2("fopen",
9299 path_unstaged_content);
9302 if (fstat(fileno(f), &sb) == -1) {
9303 err = got_error_from_errno2("fstat", path_unstaged_content);
9306 if (got_fileindex_entry_staged_filetype_get(ie) ==
9307 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
9308 char link_target[PATH_MAX];
9310 r = fread(link_target, 1, sizeof(link_target), f);
9311 if (r == 0 && ferror(f)) {
9312 err = got_error_from_errno("fread");
9315 if (r >= sizeof(link_target)) { /* should not happen */
9316 err = got_error(GOT_ERR_NO_SPACE);
9319 link_target[r] = '\0';
9320 err = merge_symlink(worktree, blob_base,
9321 ondisk_path, ie->path, label_orig, link_target,
9322 worktree->base_commit_id, repo, progress_cb,
9325 int local_changes_subsumed;
9327 err = got_path_dirname(&parent, ondisk_path);
9331 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
9333 err = got_error_from_errno("asprintf");
9338 err = got_opentemp_named(&blob_base_path, &f_base,
9342 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
9348 * In order the run a 3-way merge with a symlink we copy the symlink's
9349 * target path into a temporary file and use that file with diff3.
9351 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9352 err = dump_symlink_target_path_to_file(&f_deriv2,
9358 fd = open(ondisk_path,
9359 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
9361 err = got_error_from_errno2("open", ondisk_path);
9364 f_deriv2 = fdopen(fd, "r");
9365 if (f_deriv2 == NULL) {
9366 err = got_error_from_errno2("fdopen", ondisk_path);
9372 err = merge_file(&local_changes_subsumed, worktree,
9373 f_base, f, f_deriv2, ondisk_path, ie->path,
9374 got_fileindex_perms_to_st(ie),
9375 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
9376 repo, progress_cb, progress_arg);
9381 if (new_staged_blob_id) {
9382 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9383 SHA1_DIGEST_LENGTH);
9385 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9386 got_fileindex_entry_staged_filetype_set(ie, 0);
9389 free(new_staged_blob_id);
9390 if (path_unstaged_content &&
9391 unlink(path_unstaged_content) == -1 && err == NULL)
9392 err = got_error_from_errno2("unlink", path_unstaged_content);
9393 if (path_new_staged_content &&
9394 unlink(path_new_staged_content) == -1 && err == NULL)
9395 err = got_error_from_errno2("unlink", path_new_staged_content);
9396 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
9397 err = got_error_from_errno2("unlink", blob_base_path);
9398 if (f_base && fclose(f_base) == EOF && err == NULL)
9399 err = got_error_from_errno2("fclose", path_unstaged_content);
9400 if (f && fclose(f) == EOF && err == NULL)
9401 err = got_error_from_errno2("fclose", path_unstaged_content);
9402 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
9403 err = got_error_from_errno2("fclose", ondisk_path);
9404 free(path_unstaged_content);
9405 free(path_new_staged_content);
9406 free(blob_base_path);
9412 static const struct got_error *
9413 unstage_path(void *arg, unsigned char status,
9414 unsigned char staged_status, const char *relpath,
9415 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9416 struct got_object_id *commit_id, int dirfd, const char *de_name)
9418 const struct got_error *err = NULL;
9419 struct unstage_path_arg *a = arg;
9420 struct got_fileindex_entry *ie;
9421 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
9422 char *ondisk_path = NULL;
9423 char *id_str = NULL, *label_orig = NULL;
9424 int local_changes_subsumed;
9426 int fd1 = -1, fd2 = -1;
9428 if (staged_status != GOT_STATUS_ADD &&
9429 staged_status != GOT_STATUS_MODIFY &&
9430 staged_status != GOT_STATUS_DELETE)
9433 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9435 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9437 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
9439 return got_error_from_errno("asprintf");
9441 err = got_object_id_str(&id_str,
9442 commit_id ? commit_id : a->worktree->base_commit_id);
9445 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9447 err = got_error_from_errno("asprintf");
9451 fd1 = got_opentempfd();
9453 err = got_error_from_errno("got_opentempfd");
9456 fd2 = got_opentempfd();
9458 err = got_error_from_errno("got_opentempfd");
9462 switch (staged_status) {
9463 case GOT_STATUS_MODIFY:
9464 err = got_object_open_as_blob(&blob_base, a->repo,
9465 blob_id, 8192, fd1);
9469 case GOT_STATUS_ADD:
9471 if (staged_status == GOT_STATUS_ADD) {
9472 int choice = GOT_PATCH_CHOICE_NONE;
9473 err = (*a->patch_cb)(&choice, a->patch_arg,
9474 staged_status, ie->path, NULL, 1, 1);
9477 if (choice != GOT_PATCH_CHOICE_YES)
9480 err = unstage_hunks(staged_blob_id,
9481 blob_base, blob_id, ie, ondisk_path,
9482 label_orig, a->worktree, a->repo,
9483 a->patch_cb, a->patch_arg,
9484 a->progress_cb, a->progress_arg);
9485 break; /* Done with this file. */
9488 err = got_object_open_as_blob(&blob_staged, a->repo,
9489 staged_blob_id, 8192, fd2);
9492 switch (got_fileindex_entry_staged_filetype_get(ie)) {
9493 case GOT_FILEIDX_MODE_BAD_SYMLINK:
9494 case GOT_FILEIDX_MODE_REGULAR_FILE:
9495 err = merge_blob(&local_changes_subsumed, a->worktree,
9496 blob_base, ondisk_path, relpath,
9497 got_fileindex_perms_to_st(ie), label_orig,
9498 blob_staged, commit_id ? commit_id :
9499 a->worktree->base_commit_id, a->repo,
9500 a->progress_cb, a->progress_arg);
9502 case GOT_FILEIDX_MODE_SYMLINK:
9503 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9504 char *staged_target;
9505 err = got_object_blob_read_to_str(
9506 &staged_target, blob_staged);
9509 err = merge_symlink(a->worktree, blob_base,
9510 ondisk_path, relpath, label_orig,
9511 staged_target, commit_id ? commit_id :
9512 a->worktree->base_commit_id,
9513 a->repo, a->progress_cb, a->progress_arg);
9514 free(staged_target);
9516 err = merge_blob(&local_changes_subsumed,
9517 a->worktree, blob_base, ondisk_path,
9518 relpath, got_fileindex_perms_to_st(ie),
9519 label_orig, blob_staged,
9520 commit_id ? commit_id :
9521 a->worktree->base_commit_id, a->repo,
9522 a->progress_cb, a->progress_arg);
9526 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9530 got_fileindex_entry_stage_set(ie,
9531 GOT_FILEIDX_STAGE_NONE);
9532 got_fileindex_entry_staged_filetype_set(ie, 0);
9535 case GOT_STATUS_DELETE:
9537 int choice = GOT_PATCH_CHOICE_NONE;
9538 err = (*a->patch_cb)(&choice, a->patch_arg,
9539 staged_status, ie->path, NULL, 1, 1);
9542 if (choice == GOT_PATCH_CHOICE_NO)
9544 if (choice != GOT_PATCH_CHOICE_YES) {
9545 err = got_error(GOT_ERR_PATCH_CHOICE);
9549 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9550 got_fileindex_entry_staged_filetype_set(ie, 0);
9551 err = get_file_status(&status, &sb, ie, ondisk_path,
9552 dirfd, de_name, a->repo);
9555 err = (*a->progress_cb)(a->progress_arg, status, relpath);
9560 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9561 err = got_error_from_errno("close");
9563 got_object_blob_close(blob_base);
9564 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9565 err = got_error_from_errno("close");
9567 got_object_blob_close(blob_staged);
9573 const struct got_error *
9574 got_worktree_unstage(struct got_worktree *worktree,
9575 struct got_pathlist_head *paths,
9576 got_worktree_checkout_cb progress_cb, void *progress_arg,
9577 got_worktree_patch_cb patch_cb, void *patch_arg,
9578 struct got_repository *repo)
9580 const struct got_error *err = NULL, *sync_err, *unlockerr;
9581 struct got_pathlist_entry *pe;
9582 struct got_fileindex *fileindex = NULL;
9583 char *fileindex_path = NULL;
9584 struct unstage_path_arg upa;
9586 err = lock_worktree(worktree, LOCK_EX);
9590 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9594 upa.worktree = worktree;
9595 upa.fileindex = fileindex;
9597 upa.progress_cb = progress_cb;
9598 upa.progress_arg = progress_arg;
9599 upa.patch_cb = patch_cb;
9600 upa.patch_arg = patch_arg;
9601 TAILQ_FOREACH(pe, paths, entry) {
9602 err = worktree_status(worktree, pe->path, fileindex, repo,
9603 unstage_path, &upa, NULL, NULL, 1, 0);
9608 sync_err = sync_fileindex(fileindex, fileindex_path);
9609 if (sync_err && err == NULL)
9612 free(fileindex_path);
9614 got_fileindex_free(fileindex);
9615 unlockerr = lock_worktree(worktree, LOCK_SH);
9616 if (unlockerr && err == NULL)
9621 struct report_file_info_arg {
9622 struct got_worktree *worktree;
9623 got_worktree_path_info_cb info_cb;
9625 struct got_pathlist_head *paths;
9626 got_cancel_cb cancel_cb;
9630 static const struct got_error *
9631 report_file_info(void *arg, struct got_fileindex_entry *ie)
9633 struct report_file_info_arg *a = arg;
9634 struct got_pathlist_entry *pe;
9635 struct got_object_id blob_id, staged_blob_id, commit_id;
9636 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9637 struct got_object_id *commit_idp = NULL;
9640 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9641 return got_error(GOT_ERR_CANCELLED);
9643 TAILQ_FOREACH(pe, a->paths, entry) {
9644 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9645 got_path_is_child(ie->path, pe->path, pe->path_len))
9648 if (pe == NULL) /* not found */
9651 if (got_fileindex_entry_has_blob(ie))
9652 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9653 stage = got_fileindex_entry_stage_get(ie);
9654 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9655 stage == GOT_FILEIDX_STAGE_ADD) {
9656 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9657 &staged_blob_id, ie);
9660 if (got_fileindex_entry_has_commit(ie))
9661 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9663 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9664 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9667 const struct got_error *
9668 got_worktree_path_info(struct got_worktree *worktree,
9669 struct got_pathlist_head *paths,
9670 got_worktree_path_info_cb info_cb, void *info_arg,
9671 got_cancel_cb cancel_cb, void *cancel_arg)
9674 const struct got_error *err = NULL, *unlockerr;
9675 struct got_fileindex *fileindex = NULL;
9676 char *fileindex_path = NULL;
9677 struct report_file_info_arg arg;
9679 err = lock_worktree(worktree, LOCK_SH);
9683 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9687 arg.worktree = worktree;
9688 arg.info_cb = info_cb;
9689 arg.info_arg = info_arg;
9691 arg.cancel_cb = cancel_cb;
9692 arg.cancel_arg = cancel_arg;
9693 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9696 free(fileindex_path);
9698 got_fileindex_free(fileindex);
9699 unlockerr = lock_worktree(worktree, LOCK_UN);
9700 if (unlockerr && err == NULL)
9705 static const struct got_error *
9706 patch_check_path(const char *p, char **path, unsigned char *status,
9707 unsigned char *staged_status, struct got_fileindex *fileindex,
9708 struct got_worktree *worktree, struct got_repository *repo)
9710 const struct got_error *err;
9711 struct got_fileindex_entry *ie;
9713 char *ondisk_path = NULL;
9715 err = got_worktree_resolve_path(path, worktree, p);
9719 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9720 *path[0] ? "/" : "", *path) == -1)
9721 return got_error_from_errno("asprintf");
9723 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9725 *staged_status = get_staged_status(ie);
9726 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9731 *staged_status = GOT_STATUS_NO_CHANGE;
9732 *status = GOT_STATUS_UNVERSIONED;
9733 if (lstat(ondisk_path, &sb) == -1) {
9734 if (errno != ENOENT) {
9735 err = got_error_from_errno2("lstat",
9739 *status = GOT_STATUS_NONEXISTENT;
9748 static const struct got_error *
9749 patch_can_rm(const char *path, unsigned char status,
9750 unsigned char staged_status)
9752 if (status == GOT_STATUS_NONEXISTENT)
9753 return got_error_set_errno(ENOENT, path);
9754 if (status != GOT_STATUS_NO_CHANGE &&
9755 status != GOT_STATUS_ADD &&
9756 status != GOT_STATUS_MODIFY &&
9757 status != GOT_STATUS_MODE_CHANGE)
9758 return got_error_path(path, GOT_ERR_FILE_STATUS);
9759 if (staged_status == GOT_STATUS_DELETE)
9760 return got_error_path(path, GOT_ERR_FILE_STATUS);
9764 static const struct got_error *
9765 patch_can_add(const char *path, unsigned char status)
9767 if (status != GOT_STATUS_NONEXISTENT)
9768 return got_error_path(path, GOT_ERR_FILE_STATUS);
9772 static const struct got_error *
9773 patch_can_edit(const char *path, unsigned char status,
9774 unsigned char staged_status)
9776 if (status == GOT_STATUS_NONEXISTENT)
9777 return got_error_set_errno(ENOENT, path);
9778 if (status != GOT_STATUS_NO_CHANGE &&
9779 status != GOT_STATUS_ADD &&
9780 status != GOT_STATUS_MODIFY)
9781 return got_error_path(path, GOT_ERR_FILE_STATUS);
9782 if (staged_status == GOT_STATUS_DELETE)
9783 return got_error_path(path, GOT_ERR_FILE_STATUS);
9787 const struct got_error *
9788 got_worktree_patch_prepare(struct got_fileindex **fileindex,
9789 char **fileindex_path, struct got_worktree *worktree)
9791 return open_fileindex(fileindex, fileindex_path, worktree);
9794 const struct got_error *
9795 got_worktree_patch_check_path(const char *old, const char *new,
9796 char **oldpath, char **newpath, struct got_worktree *worktree,
9797 struct got_repository *repo, struct got_fileindex *fileindex)
9799 const struct got_error *err = NULL;
9800 int file_renamed = 0;
9801 unsigned char status_old, staged_status_old;
9802 unsigned char status_new, staged_status_new;
9807 err = patch_check_path(old != NULL ? old : new, oldpath,
9808 &status_old, &staged_status_old, fileindex, worktree, repo);
9812 err = patch_check_path(new != NULL ? new : old, newpath,
9813 &status_new, &staged_status_new, fileindex, worktree, repo);
9817 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9820 if (old != NULL && new == NULL)
9821 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9822 else if (file_renamed) {
9823 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9825 err = patch_can_add(*newpath, status_new);
9826 } else if (old == NULL)
9827 err = patch_can_add(*newpath, status_new);
9829 err = patch_can_edit(*newpath, status_new, staged_status_new);
9841 const struct got_error *
9842 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9843 struct got_worktree *worktree, struct got_fileindex *fileindex,
9844 got_worktree_checkout_cb progress_cb, void *progress_arg)
9846 struct schedule_addition_args saa;
9848 memset(&saa, 0, sizeof(saa));
9849 saa.worktree = worktree;
9850 saa.fileindex = fileindex;
9851 saa.progress_cb = progress_cb;
9852 saa.progress_arg = progress_arg;
9855 return worktree_status(worktree, path, fileindex, repo,
9856 schedule_addition, &saa, NULL, NULL, 1, 0);
9859 const struct got_error *
9860 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9861 struct got_worktree *worktree, struct got_fileindex *fileindex,
9862 got_worktree_delete_cb progress_cb, void *progress_arg)
9864 const struct got_error *err;
9865 struct schedule_deletion_args sda;
9866 char *ondisk_status_path;
9868 memset(&sda, 0, sizeof(sda));
9869 sda.worktree = worktree;
9870 sda.fileindex = fileindex;
9871 sda.progress_cb = progress_cb;
9872 sda.progress_arg = progress_arg;
9874 sda.delete_local_mods = 0;
9875 sda.keep_on_disk = 0;
9876 sda.ignore_missing_paths = 0;
9877 sda.status_codes = NULL;
9878 if (asprintf(&ondisk_status_path, "%s/%s",
9879 got_worktree_get_root_path(worktree), path) == -1)
9880 return got_error_from_errno("asprintf");
9881 sda.status_path = ondisk_status_path;
9882 sda.status_path_len = strlen(ondisk_status_path);
9884 err = worktree_status(worktree, path, fileindex, repo,
9885 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9886 free(ondisk_status_path);
9890 const struct got_error *
9891 got_worktree_patch_complete(struct got_fileindex *fileindex,
9892 const char *fileindex_path)
9894 const struct got_error *err = NULL;
9896 err = sync_fileindex(fileindex, fileindex_path);
9897 got_fileindex_free(fileindex);