2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/queue.h>
39 #include "got_error.h"
40 #include "got_repository.h"
41 #include "got_reference.h"
42 #include "got_object.h"
44 #include "got_cancel.h"
45 #include "got_worktree.h"
46 #include "got_opentemp.h"
49 #include "got_lib_worktree.h"
50 #include "got_lib_hash.h"
51 #include "got_lib_fileindex.h"
52 #include "got_lib_inflate.h"
53 #include "got_lib_delta.h"
54 #include "got_lib_object.h"
55 #include "got_lib_object_parse.h"
56 #include "got_lib_object_create.h"
57 #include "got_lib_object_idset.h"
58 #include "got_lib_diff.h"
59 #include "got_lib_gotconfig.h"
62 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
65 #define GOT_MERGE_LABEL_MERGED "merged change"
66 #define GOT_MERGE_LABEL_BASE "3-way merge base"
69 apply_umask(mode_t mode)
78 static const struct got_error *
79 create_meta_file(const char *path_got, const char *name, const char *content)
81 const struct got_error *err = NULL;
84 if (asprintf(&path, "%s/%s", path_got, name) == -1)
85 return got_error_from_errno("asprintf");
87 err = got_path_create_file(path, content);
92 static const struct got_error *
93 update_meta_file(const char *path_got, const char *name, const char *content)
95 const struct got_error *err = NULL;
100 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
101 err = got_error_from_errno("asprintf");
106 err = got_opentemp_named(&tmppath, &tmpfile, path, "");
111 int len = fprintf(tmpfile, "%s\n", content);
112 if (len != strlen(content) + 1) {
113 err = got_error_from_errno2("fprintf", tmppath);
118 if (rename(tmppath, path) != 0) {
119 err = got_error_from_errno3("rename", tmppath, path);
125 if (fclose(tmpfile) == EOF && err == NULL)
126 err = got_error_from_errno2("fclose", tmppath);
131 static const struct got_error *
132 write_head_ref(const char *path_got, struct got_reference *head_ref)
134 const struct got_error *err = NULL;
137 if (got_ref_is_symbolic(head_ref)) {
138 refstr = got_ref_to_str(head_ref);
140 return got_error_from_errno("got_ref_to_str");
142 refstr = strdup(got_ref_get_name(head_ref));
144 return got_error_from_errno("strdup");
146 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
151 const struct got_error *
152 got_worktree_init(const char *path, struct got_reference *head_ref,
153 const char *prefix, const char *meta_dir, struct got_repository *repo)
155 const struct got_error *err = NULL;
156 struct got_object_id *commit_id = NULL;
158 uint32_t uuid_status;
160 char *path_got = NULL;
161 char *formatstr = NULL;
162 char *absprefix = NULL;
163 char *basestr = NULL;
164 char *uuidstr = NULL;
166 if (strcmp(path, got_repo_get_path(repo)) == 0) {
167 err = got_error(GOT_ERR_WORKTREE_REPO);
171 err = got_ref_resolve(&commit_id, repo, head_ref);
174 err = got_object_get_type(&obj_type, repo, commit_id);
177 if (obj_type != GOT_OBJ_TYPE_COMMIT)
178 return got_error(GOT_ERR_OBJ_TYPE);
180 if (!got_path_is_absolute(prefix)) {
181 if (asprintf(&absprefix, "/%s", prefix) == -1)
182 return got_error_from_errno("asprintf");
185 /* Create top-level directory (may already exist). */
186 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
187 err = got_error_from_errno2("mkdir", path);
191 /* Create .got/.cvg directory (may already exist). */
192 if (asprintf(&path_got, "%s/%s", path, meta_dir) == -1) {
193 err = got_error_from_errno("asprintf");
196 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
197 err = got_error_from_errno2("mkdir", path_got);
201 /* Create an empty lock file. */
202 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
206 /* Create an empty file index. */
207 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
211 /* Write the HEAD reference. */
212 err = write_head_ref(path_got, head_ref);
216 /* Record our base commit. */
217 err = got_object_id_str(&basestr, commit_id);
220 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
224 /* Store path to repository. */
225 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
226 got_repo_get_path(repo));
230 /* Store in-repository path prefix. */
231 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
232 absprefix ? absprefix : prefix);
237 uuid_create(&uuid, &uuid_status);
238 if (uuid_status != uuid_s_ok) {
239 err = got_error_uuid(uuid_status, "uuid_create");
242 uuid_to_string(&uuid, &uuidstr, &uuid_status);
243 if (uuid_status != uuid_s_ok) {
244 err = got_error_uuid(uuid_status, "uuid_to_string");
247 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
251 /* Stamp work tree with format file. */
252 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
253 err = got_error_from_errno("asprintf");
256 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
270 const struct got_error *
271 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
272 const char *path_prefix)
274 char *absprefix = NULL;
276 if (!got_path_is_absolute(path_prefix)) {
277 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
278 return got_error_from_errno("asprintf");
280 *match = (strcmp(absprefix ? absprefix : path_prefix,
281 worktree->path_prefix) == 0);
287 got_worktree_get_head_ref_name(struct got_worktree *worktree)
289 return worktree->head_ref_name;
292 const struct got_error *
293 got_worktree_set_head_ref(struct got_worktree *worktree,
294 struct got_reference *head_ref)
296 const struct got_error *err = NULL;
297 char *path_got = NULL, *head_ref_name = NULL;
299 if (asprintf(&path_got, "%s/%s", worktree->root_path,
300 worktree->meta_dir) == -1) {
301 err = got_error_from_errno("asprintf");
306 head_ref_name = strdup(got_ref_get_name(head_ref));
307 if (head_ref_name == NULL) {
308 err = got_error_from_errno("strdup");
312 err = write_head_ref(path_got, head_ref);
316 free(worktree->head_ref_name);
317 worktree->head_ref_name = head_ref_name;
325 struct got_object_id *
326 got_worktree_get_base_commit_id(struct got_worktree *worktree)
328 return worktree->base_commit_id;
331 const struct got_error *
332 got_worktree_set_base_commit_id(struct got_worktree *worktree,
333 struct got_repository *repo, struct got_object_id *commit_id)
335 const struct got_error *err;
336 struct got_object *obj = NULL;
338 char *path_got = NULL;
340 if (asprintf(&path_got, "%s/%s", worktree->root_path,
341 worktree->meta_dir) == -1) {
342 err = got_error_from_errno("asprintf");
347 err = got_object_open(&obj, repo, commit_id);
351 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
352 err = got_error(GOT_ERR_OBJ_TYPE);
356 /* Record our base commit. */
357 err = got_object_id_str(&id_str, commit_id);
360 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
364 free(worktree->base_commit_id);
365 worktree->base_commit_id = got_object_id_dup(commit_id);
366 if (worktree->base_commit_id == NULL) {
367 err = got_error_from_errno("got_object_id_dup");
372 got_object_close(obj);
378 const struct got_gotconfig *
379 got_worktree_get_gotconfig(struct got_worktree *worktree)
381 return worktree->gotconfig;
384 static const struct got_error *
385 lock_worktree(struct got_worktree *worktree, int operation)
387 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
388 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
389 : got_error_from_errno2("flock",
390 got_worktree_get_root_path(worktree)));
394 static const struct got_error *
395 add_dir_on_disk(struct got_worktree *worktree, const char *path)
397 const struct got_error *err = NULL;
400 /* We only accept worktree-relative paths here. */
401 if (got_path_is_absolute(path)) {
402 return got_error_fmt(GOT_ERR_BAD_PATH,
403 "%s does not accept absolute paths: %s",
407 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
408 return got_error_from_errno("asprintf");
410 err = got_path_mkdir(abspath);
411 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
414 if (lstat(abspath, &sb) == -1) {
415 err = got_error_from_errno2("lstat", abspath);
416 } else if (!S_ISDIR(sb.st_mode)) {
417 /* TODO directory is obstructed; do something */
418 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
425 static const struct got_error *
426 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
428 const struct got_error *err = NULL;
431 size_t flen1 = 0, flen2 = 0;
436 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
437 if (flen1 == 0 && ferror(f1)) {
438 err = got_error_from_errno("fread");
441 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
442 if (flen2 == 0 && ferror(f2)) {
443 err = got_error_from_errno("fread");
450 } else if (flen2 == 0) {
454 } else if (flen1 == flen2) {
455 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
468 static const struct got_error *
469 check_files_equal(int *same, FILE *f1, FILE *f2)
476 if (fstat(fileno(f1), &sb) != 0)
477 return got_error_from_errno("fstat");
480 if (fstat(fileno(f2), &sb) != 0)
481 return got_error_from_errno("fstat");
484 if (size1 != size2) {
489 if (fseek(f1, 0L, SEEK_SET) == -1)
490 return got_ferror(f1, GOT_ERR_IO);
491 if (fseek(f2, 0L, SEEK_SET) == -1)
492 return got_ferror(f2, GOT_ERR_IO);
494 return check_file_contents_equal(same, f1, f2);
497 static const struct got_error *
498 copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
506 if (fseek(f, 0L, SEEK_SET) == -1)
507 return got_ferror(f, GOT_ERR_IO);
510 flen = fread(fbuf, 1, sizeof(fbuf), f);
513 return got_error_from_errno("fread");
517 outlen = write(outfd, fbuf, flen);
519 return got_error_from_errno("write");
521 return got_error(GOT_ERR_IO);
528 static const struct got_error *
529 merge_binary_file(int *overlapcnt, int merged_fd,
530 FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
531 const char *label_deriv, const char *label_orig, const char *label_deriv2,
532 const char *ondisk_path)
534 const struct got_error *err = NULL;
535 int same_content, changed_deriv, changed_deriv2;
536 int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
537 off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
538 char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
539 char *base_path_orig = NULL, *base_path_deriv = NULL;
540 char *base_path_deriv2 = NULL;
544 err = check_files_equal(&same_content, f_deriv, f_deriv2);
549 return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
551 err = check_files_equal(&same_content, f_deriv, f_orig);
554 changed_deriv = !same_content;
555 err = check_files_equal(&same_content, f_deriv2, f_orig);
558 changed_deriv2 = !same_content;
560 if (changed_deriv && changed_deriv2) {
562 if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
563 err = got_error_from_errno("asprintf");
566 if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
567 err = got_error_from_errno("asprintf");
570 if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
571 err = got_error_from_errno("asprintf");
574 err = got_opentemp_named_fd(&path_orig, &fd_orig,
578 err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
579 base_path_deriv, "");
582 err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
583 base_path_deriv2, "");
586 err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
589 err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
592 err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
595 if (dprintf(merged_fd, "Binary files differ and cannot be "
596 "merged automatically:\n") < 0) {
597 err = got_error_from_errno("dprintf");
600 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
601 GOT_DIFF_CONFLICT_MARKER_BEGIN,
602 label_deriv ? " " : "",
603 label_deriv ? label_deriv : "",
605 err = got_error_from_errno("dprintf");
609 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
610 GOT_DIFF_CONFLICT_MARKER_ORIG,
611 label_orig ? " " : "",
612 label_orig ? label_orig : "",
614 err = got_error_from_errno("dprintf");
618 if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
619 GOT_DIFF_CONFLICT_MARKER_SEP,
621 GOT_DIFF_CONFLICT_MARKER_END,
622 label_deriv2 ? " " : "",
623 label_deriv2 ? label_deriv2 : "") < 0) {
624 err = got_error_from_errno("dprintf");
627 } else if (changed_deriv)
628 err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
629 else if (changed_deriv2)
630 err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
632 if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
634 err = got_error_from_errno2("unlink", path_orig);
635 if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
636 err = got_error_from_errno2("close", path_orig);
637 if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
638 err = got_error_from_errno2("close", path_deriv);
639 if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
640 err = got_error_from_errno2("close", path_deriv2);
644 free(base_path_orig);
645 free(base_path_deriv);
646 free(base_path_deriv2);
651 * Perform a 3-way merge where the file f_orig acts as the common
652 * ancestor, the file f_deriv acts as the first derived version,
653 * and the file f_deriv2 acts as the second derived version.
654 * The merge result will be written to a new file at ondisk_path; any
655 * existing file at this path will be replaced.
657 static const struct got_error *
658 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
659 FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
660 const char *path, uint16_t st_mode,
661 const char *label_orig, const char *label_deriv, const char *label_deriv2,
662 enum got_diff_algorithm diff_algo, struct got_repository *repo,
663 got_worktree_checkout_cb progress_cb, void *progress_arg)
665 const struct got_error *err = NULL;
667 FILE *f_merged = NULL;
668 char *merged_path = NULL, *base_path = NULL;
672 *local_changes_subsumed = 0;
674 err = got_path_dirname(&parent, ondisk_path);
678 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
679 err = got_error_from_errno("asprintf");
683 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path, "");
687 err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
688 f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
690 if (err->code != GOT_ERR_FILE_BINARY)
692 err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
693 f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
699 err = (*progress_cb)(progress_arg,
700 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
704 if (fsync(merged_fd) != 0) {
705 err = got_error_from_errno("fsync");
709 f_merged = fdopen(merged_fd, "r");
710 if (f_merged == NULL) {
711 err = got_error_from_errno("fdopen");
716 /* Check if a clean merge has subsumed all local changes. */
717 if (overlapcnt == 0) {
718 err = check_files_equal(local_changes_subsumed, f_deriv,
724 if (fchmod(fileno(f_merged), apply_umask(st_mode)) != 0) {
725 err = got_error_from_errno2("fchmod", merged_path);
729 if (rename(merged_path, ondisk_path) != 0) {
730 err = got_error_from_errno3("rename", merged_path,
739 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
740 err = got_error_from_errno("close");
741 if (f_merged && fclose(f_merged) == EOF && err == NULL)
742 err = got_error_from_errno("fclose");
749 static const struct got_error *
750 update_symlink(const char *ondisk_path, const char *target_path,
753 /* This is not atomic but matches what 'ln -sf' does. */
754 if (unlink(ondisk_path) == -1)
755 return got_error_from_errno2("unlink", ondisk_path);
756 if (symlink(target_path, ondisk_path) == -1)
757 return got_error_from_errno3("symlink", target_path,
763 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
764 * in the work tree with a file that contains conflict markers and the
765 * conflicting target paths of the original version, a "derived version"
766 * of a symlink from an incoming change, and a local version of the symlink.
768 * The original versions's target path can be NULL if it is not available,
769 * such as if both derived versions added a new symlink at the same path.
771 * The incoming derived symlink target is NULL in case the incoming change
772 * has deleted this symlink.
774 static const struct got_error *
775 install_symlink_conflict(const char *deriv_target,
776 struct got_object_id *deriv_base_commit_id, const char *orig_target,
777 const char *label_orig, const char *local_target, const char *ondisk_path)
779 const struct got_error *err;
780 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
783 err = got_object_id_str(&id_str, deriv_base_commit_id);
785 return got_error_from_errno("asprintf");
787 if (asprintf(&label_deriv, "%s: commit %s",
788 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
789 err = got_error_from_errno("asprintf");
793 err = got_opentemp_named(&path, &f, "got-symlink-conflict", "");
797 if (fchmod(fileno(f), apply_umask(GOT_DEFAULT_FILE_MODE)) == -1) {
798 err = got_error_from_errno2("fchmod", path);
802 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
803 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
804 deriv_target ? deriv_target : "(symlink was deleted)",
805 orig_target ? label_orig : "",
806 orig_target ? "\n" : "",
807 orig_target ? orig_target : "",
808 orig_target ? "\n" : "",
809 GOT_DIFF_CONFLICT_MARKER_SEP,
810 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
811 err = got_error_from_errno2("fprintf", path);
815 if (unlink(ondisk_path) == -1) {
816 err = got_error_from_errno2("unlink", ondisk_path);
819 if (rename(path, ondisk_path) == -1) {
820 err = got_error_from_errno3("rename", path, ondisk_path);
824 if (f != NULL && fclose(f) == EOF && err == NULL)
825 err = got_error_from_errno2("fclose", path);
832 /* forward declaration */
833 static const struct got_error *
834 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
835 const char *, const char *, uint16_t, const char *,
836 struct got_blob_object *, struct got_object_id *,
837 struct got_repository *, got_worktree_checkout_cb, void *);
840 * Merge a symlink into the work tree, where blob_orig acts as the common
841 * ancestor, deriv_target is the link target of the first derived version,
842 * and the symlink on disk acts as the second derived version.
843 * Assume that contents of both blobs represent symlinks.
845 static const struct got_error *
846 merge_symlink(struct got_worktree *worktree,
847 struct got_blob_object *blob_orig, const char *ondisk_path,
848 const char *path, const char *label_orig, const char *deriv_target,
849 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
850 got_worktree_checkout_cb progress_cb, void *progress_arg)
852 const struct got_error *err = NULL;
853 char *ancestor_target = NULL;
855 ssize_t ondisk_len, deriv_len;
856 char ondisk_target[PATH_MAX];
857 int have_local_change = 0;
858 int have_incoming_change = 0;
860 if (lstat(ondisk_path, &sb) == -1)
861 return got_error_from_errno2("lstat", ondisk_path);
863 ondisk_len = readlink(ondisk_path, ondisk_target,
864 sizeof(ondisk_target));
865 if (ondisk_len == -1) {
866 err = got_error_from_errno2("readlink",
870 ondisk_target[ondisk_len] = '\0';
873 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
878 if (ancestor_target == NULL ||
879 (ondisk_len != strlen(ancestor_target) ||
880 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
881 have_local_change = 1;
883 deriv_len = strlen(deriv_target);
884 if (ancestor_target == NULL ||
885 (deriv_len != strlen(ancestor_target) ||
886 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
887 have_incoming_change = 1;
889 if (!have_local_change && !have_incoming_change) {
890 if (ancestor_target) {
891 /* Both sides made the same change. */
892 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
894 } else if (deriv_len == ondisk_len &&
895 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
896 /* Both sides added the same symlink. */
897 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
900 /* Both sides added symlinks which don't match. */
901 err = install_symlink_conflict(deriv_target,
902 deriv_base_commit_id, ancestor_target,
903 label_orig, ondisk_target, ondisk_path);
906 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
909 } else if (!have_local_change && have_incoming_change) {
910 /* Apply the incoming change. */
911 err = update_symlink(ondisk_path, deriv_target,
912 strlen(deriv_target));
915 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
916 } else if (have_local_change && have_incoming_change) {
917 if (deriv_len == ondisk_len &&
918 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
919 /* Both sides made the same change. */
920 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
923 err = install_symlink_conflict(deriv_target,
924 deriv_base_commit_id, ancestor_target, label_orig,
925 ondisk_target, ondisk_path);
928 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
934 free(ancestor_target);
938 static const struct got_error *
939 dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
941 const struct got_error *err = NULL;
942 char target_path[PATH_MAX];
951 return got_error_from_errno("got_opentemp");
952 target_len = readlink(ondisk_path, target_path, sizeof(target_path));
953 if (target_len == -1) {
954 err = got_error_from_errno2("readlink", ondisk_path);
957 n = fwrite(target_path, 1, target_len, f);
958 if (n != target_len) {
959 err = got_ferror(f, GOT_ERR_IO);
962 if (fflush(f) == EOF) {
963 err = got_error_from_errno("fflush");
966 if (fseek(f, 0L, SEEK_SET) == -1) {
967 err = got_ferror(f, GOT_ERR_IO);
979 * Perform a 3-way merge where blob_orig acts as the common ancestor,
980 * blob_deriv acts as the first derived version, and the file on disk
981 * acts as the second derived version.
983 static const struct got_error *
984 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
985 struct got_blob_object *blob_orig, const char *ondisk_path,
986 const char *path, uint16_t st_mode, const char *label_orig,
987 struct got_blob_object *blob_deriv,
988 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
989 got_worktree_checkout_cb progress_cb, void *progress_arg)
991 const struct got_error *err = NULL;
992 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
993 char *blob_orig_path = NULL;
994 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
995 char *label_deriv = NULL, *parent = NULL;
997 *local_changes_subsumed = 0;
999 err = got_path_dirname(&parent, ondisk_path);
1004 if (asprintf(&base_path, "%s/got-merge-blob-orig",
1006 err = got_error_from_errno("asprintf");
1011 err = got_opentemp_named(&blob_orig_path, &f_orig,
1015 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1022 * No common ancestor exists. This is an "add vs add" conflict
1023 * and we simply use an empty ancestor file to make both files
1024 * appear in the merged result in their entirety.
1026 f_orig = got_opentemp();
1027 if (f_orig == NULL) {
1028 err = got_error_from_errno("got_opentemp");
1033 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1034 err = got_error_from_errno("asprintf");
1039 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path, "");
1042 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1047 err = got_object_id_str(&id_str, deriv_base_commit_id);
1050 if (asprintf(&label_deriv, "%s: commit %s",
1051 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1052 err = got_error_from_errno("asprintf");
1057 * In order the run a 3-way merge with a symlink we copy the symlink's
1058 * target path into a temporary file and use that file with diff3.
1060 if (S_ISLNK(st_mode)) {
1061 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1066 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1068 err = got_error_from_errno2("open", ondisk_path);
1071 f_deriv2 = fdopen(fd, "r");
1072 if (f_deriv2 == NULL) {
1073 err = got_error_from_errno2("fdopen", ondisk_path);
1079 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1080 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1081 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1083 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1084 err = got_error_from_errno("fclose");
1085 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1086 err = got_error_from_errno("fclose");
1087 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1088 err = got_error_from_errno("fclose");
1090 if (blob_orig_path) {
1091 unlink(blob_orig_path);
1092 free(blob_orig_path);
1094 if (blob_deriv_path) {
1095 unlink(blob_deriv_path);
1096 free(blob_deriv_path);
1104 static const struct got_error *
1105 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1106 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1107 int wt_fd, const char *path, struct got_object_id *blob_id)
1109 const struct got_error *err = NULL;
1110 struct got_fileindex_entry *new_ie;
1114 err = got_fileindex_entry_alloc(&new_ie, path);
1118 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1119 blob_id->sha1, base_commit_id->sha1, 1);
1123 err = got_fileindex_entry_add(fileindex, new_ie);
1126 got_fileindex_entry_free(new_ie);
1133 get_ondisk_perms(int executable, mode_t st_mode)
1135 mode_t xbits = S_IXUSR;
1138 /* Map read bits to execute bits. */
1139 if (st_mode & S_IRGRP)
1141 if (st_mode & S_IROTH)
1143 return st_mode | xbits;
1149 /* forward declaration */
1150 static const struct got_error *
1151 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1152 const char *path, mode_t te_mode, mode_t st_mode,
1153 struct got_blob_object *blob, int restoring_missing_file,
1154 int reverting_versioned_file, int installing_bad_symlink,
1155 int path_is_unversioned, struct got_repository *repo,
1156 got_worktree_checkout_cb progress_cb, void *progress_arg);
1159 * This function assumes that the provided symlink target points at a
1160 * safe location in the work tree!
1162 static const struct got_error *
1163 replace_existing_symlink(int *did_something, const char *ondisk_path,
1164 const char *target_path, size_t target_len)
1166 const struct got_error *err = NULL;
1168 char etarget[PATH_MAX];
1174 * "Bad" symlinks (those pointing outside the work tree or into the
1175 * .got directory) are installed in the work tree as a regular file
1176 * which contains the bad symlink target path.
1177 * The new symlink target has already been checked for safety by our
1178 * caller. If we can successfully open a regular file then we simply
1179 * replace this file with a symlink below.
1181 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1183 if (!got_err_open_nofollow_on_symlink())
1184 return got_error_from_errno2("open", ondisk_path);
1186 /* We are updating an existing on-disk symlink. */
1187 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1189 return got_error_from_errno2("readlink", ondisk_path);
1191 if (elen == target_len &&
1192 memcmp(etarget, target_path, target_len) == 0)
1193 return NULL; /* nothing to do */
1197 err = update_symlink(ondisk_path, target_path, target_len);
1198 if (fd != -1 && close(fd) == -1 && err == NULL)
1199 err = got_error_from_errno2("close", ondisk_path);
1203 static const struct got_error *
1204 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1205 size_t target_len, const char *ondisk_path, const char *wtroot_path,
1206 const char *meta_dir)
1208 const struct got_error *err = NULL;
1209 char canonpath[PATH_MAX];
1210 char *path_got = NULL;
1212 *is_bad_symlink = 0;
1214 if (target_len >= sizeof(canonpath)) {
1215 *is_bad_symlink = 1;
1220 * We do not use realpath(3) to resolve the symlink's target
1221 * path because we don't want to resolve symlinks recursively.
1222 * Instead we make the path absolute and then canonicalize it.
1223 * Relative symlink target lookup should begin at the directory
1224 * in which the blob object is being installed.
1226 if (!got_path_is_absolute(target_path)) {
1227 char *abspath, *parent;
1228 err = got_path_dirname(&parent, ondisk_path);
1231 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1233 return got_error_from_errno("asprintf");
1236 if (strlen(abspath) >= sizeof(canonpath)) {
1237 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1241 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1246 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1251 /* Only allow symlinks pointing at paths within the work tree. */
1252 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1253 *is_bad_symlink = 1;
1257 /* Do not allow symlinks pointing into the meta directory. */
1258 if (asprintf(&path_got, "%s/%s", wtroot_path, meta_dir) == -1)
1259 return got_error_from_errno("asprintf");
1260 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1261 *is_bad_symlink = 1;
1267 static const struct got_error *
1268 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1269 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1270 int restoring_missing_file, int reverting_versioned_file,
1271 int path_is_unversioned, int allow_bad_symlinks,
1272 struct got_repository *repo,
1273 got_worktree_checkout_cb progress_cb, void *progress_arg)
1275 const struct got_error *err = NULL;
1276 char target_path[PATH_MAX];
1277 size_t len, target_len = 0;
1278 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1279 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1281 *is_bad_symlink = 0;
1284 * Blob object content specifies the target path of the link.
1285 * If a symbolic link cannot be installed we instead create
1286 * a regular file which contains the link target path stored
1287 * in the blob object.
1290 err = got_object_blob_read_block(&len, blob);
1294 if (len + target_len >= sizeof(target_path)) {
1295 /* Path too long; install as a regular file. */
1296 *is_bad_symlink = 1;
1297 got_object_blob_rewind(blob);
1298 return install_blob(worktree, ondisk_path, path,
1299 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1300 restoring_missing_file, reverting_versioned_file,
1301 1, path_is_unversioned, repo, progress_cb,
1305 /* Skip blob object header first time around. */
1306 memcpy(target_path + target_len, buf + hdrlen,
1308 target_len += len - hdrlen;
1312 target_path[target_len] = '\0';
1314 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1315 ondisk_path, worktree->root_path, worktree->meta_dir);
1319 if (*is_bad_symlink && !allow_bad_symlinks) {
1320 /* install as a regular file */
1321 got_object_blob_rewind(blob);
1322 err = install_blob(worktree, ondisk_path, path,
1323 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1324 restoring_missing_file, reverting_versioned_file, 1,
1325 path_is_unversioned, repo, progress_cb, progress_arg);
1329 if (symlink(target_path, ondisk_path) == -1) {
1330 if (errno == EEXIST) {
1331 int symlink_replaced;
1332 if (path_is_unversioned) {
1333 err = (*progress_cb)(progress_arg,
1334 GOT_STATUS_UNVERSIONED, path);
1337 err = replace_existing_symlink(&symlink_replaced,
1338 ondisk_path, target_path, target_len);
1342 if (symlink_replaced) {
1343 err = (*progress_cb)(progress_arg,
1344 reverting_versioned_file ?
1346 GOT_STATUS_UPDATE, path);
1348 err = (*progress_cb)(progress_arg,
1349 GOT_STATUS_EXISTS, path);
1352 return err; /* Nothing else to do. */
1355 if (errno == ENOENT) {
1357 err = got_path_dirname(&parent, ondisk_path);
1360 err = got_path_mkdir(parent);
1365 * Retry, and fall through to error handling
1366 * below if this second attempt fails.
1368 if (symlink(target_path, ondisk_path) != -1) {
1369 err = NULL; /* success */
1371 err = (*progress_cb)(progress_arg,
1372 reverting_versioned_file ?
1373 GOT_STATUS_REVERT : GOT_STATUS_ADD,
1380 /* Handle errors from first or second creation attempt. */
1381 if (errno == ENAMETOOLONG) {
1382 /* bad target path; install as a regular file */
1383 *is_bad_symlink = 1;
1384 got_object_blob_rewind(blob);
1385 err = install_blob(worktree, ondisk_path, path,
1386 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1387 restoring_missing_file, reverting_versioned_file, 1,
1388 path_is_unversioned, repo,
1389 progress_cb, progress_arg);
1390 } else if (errno == ENOTDIR) {
1391 err = got_error_path(ondisk_path,
1392 GOT_ERR_FILE_OBSTRUCTED);
1394 err = got_error_from_errno3("symlink",
1395 target_path, ondisk_path);
1397 } else if (progress_cb)
1398 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1399 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1403 static const struct got_error *
1404 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1405 const char *path, mode_t te_mode, mode_t st_mode,
1406 struct got_blob_object *blob, int restoring_missing_file,
1407 int reverting_versioned_file, int installing_bad_symlink,
1408 int path_is_unversioned, struct got_repository *repo,
1409 got_worktree_checkout_cb progress_cb, void *progress_arg)
1411 const struct got_error *err = NULL;
1415 char *tmppath = NULL;
1418 mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1419 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1422 if (errno == ENOENT || errno == ENOTDIR) {
1424 err = got_path_dirname(&parent, path);
1427 err = add_dir_on_disk(worktree, parent);
1428 if (err && err->code == GOT_ERR_FILE_OBSTRUCTED)
1429 err = got_error_path(path, err->code);
1433 fd = open(ondisk_path,
1434 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1437 return got_error_from_errno2("open",
1439 } else if (errno == EEXIST) {
1440 if (path_is_unversioned) {
1441 err = (*progress_cb)(progress_arg,
1442 GOT_STATUS_UNVERSIONED, path);
1445 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1446 !S_ISREG(st_mode) && !installing_bad_symlink) {
1447 /* TODO file is obstructed; do something */
1448 err = got_error_path(ondisk_path,
1449 GOT_ERR_FILE_OBSTRUCTED);
1452 err = got_opentemp_named_fd(&tmppath, &fd,
1458 if (fchmod(fd, apply_umask(mode)) == -1) {
1459 err = got_error_from_errno2("fchmod",
1465 return got_error_from_errno2("open", ondisk_path);
1469 if (restoring_missing_file)
1470 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1472 else if (reverting_versioned_file)
1473 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1476 err = (*progress_cb)(progress_arg,
1477 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1482 hdrlen = got_object_blob_get_hdrlen(blob);
1484 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1485 err = got_object_blob_read_block(&len, blob);
1489 /* Skip blob object header first time around. */
1490 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1492 err = got_error_from_errno("write");
1494 } else if (outlen != len - hdrlen) {
1495 err = got_error(GOT_ERR_IO);
1502 if (fsync(fd) != 0) {
1503 err = got_error_from_errno("fsync");
1508 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1509 err = got_error_from_errno2("unlink", ondisk_path);
1512 if (rename(tmppath, ondisk_path) != 0) {
1513 err = got_error_from_errno3("rename", tmppath,
1522 if (fd != -1 && close(fd) == -1 && err == NULL)
1523 err = got_error_from_errno("close");
1524 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1525 err = got_error_from_errno2("unlink", tmppath);
1531 * Upgrade STATUS_MODIFY to STATUS_CONFLICT if a
1532 * conflict marker is found in newly added lines only.
1534 static const struct got_error *
1535 get_modified_file_content_status(unsigned char *status,
1536 struct got_blob_object *blob, const char *path, struct stat *sb,
1539 const struct got_error *err, *free_err;
1540 const char *markers[3] = {
1541 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1542 GOT_DIFF_CONFLICT_MARKER_SEP,
1543 GOT_DIFF_CONFLICT_MARKER_END
1546 struct got_diffreg_result *diffreg_result = NULL;
1547 struct diff_result *r;
1548 int nchunks_parsed, n, i = 0, ln = 0;
1550 size_t linesize = 0;
1553 if (*status != GOT_STATUS_MODIFY)
1556 f1 = got_opentemp();
1558 return got_error_from_errno("got_opentemp");
1561 got_object_blob_rewind(blob);
1562 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
1567 err = got_diff_files(&diffreg_result, f1, 1, NULL, ondisk_file,
1568 1, NULL, 0, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
1572 r = diffreg_result->result;
1574 for (n = 0; n < r->chunks.len; n += nchunks_parsed) {
1575 struct diff_chunk *c;
1576 struct diff_chunk_context cc = {};
1580 * We can optimise a little by advancing straight
1581 * to the next chunk if this one has no added lines.
1583 c = diff_chunk_get(r, n);
1585 if (diff_chunk_type(c) != CHUNK_PLUS) {
1587 continue; /* removed or unchanged lines */
1590 pos = diff_chunk_get_right_start_pos(c);
1591 if (fseek(ondisk_file, pos, SEEK_SET) == -1) {
1592 err = got_ferror(ondisk_file, GOT_ERR_IO);
1596 diff_chunk_context_load_change(&cc, &nchunks_parsed, r, n, 0);
1597 ln = cc.right.start;
1599 while (ln < cc.right.end) {
1600 linelen = getline(&line, &linesize, ondisk_file);
1601 if (linelen == -1) {
1602 if (feof(ondisk_file))
1604 err = got_ferror(ondisk_file, GOT_ERR_IO);
1608 if (line && strncmp(line, markers[i],
1609 strlen(markers[i])) == 0) {
1610 if (strcmp(markers[i],
1611 GOT_DIFF_CONFLICT_MARKER_END) == 0) {
1612 *status = GOT_STATUS_CONFLICT;
1623 if (f1 != NULL && fclose(f1) == EOF && err == NULL)
1624 err = got_error_from_errno("fclose");
1625 free_err = got_diffreg_result_free(diffreg_result);
1633 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1635 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1636 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1640 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1642 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1643 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1644 ie->mtime_sec == sb->st_mtim.tv_sec &&
1645 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1646 ie->size == (sb->st_size & 0xffffffff) &&
1647 !xbit_differs(ie, sb->st_mode));
1650 static unsigned char
1651 get_staged_status(struct got_fileindex_entry *ie)
1653 switch (got_fileindex_entry_stage_get(ie)) {
1654 case GOT_FILEIDX_STAGE_ADD:
1655 return GOT_STATUS_ADD;
1656 case GOT_FILEIDX_STAGE_DELETE:
1657 return GOT_STATUS_DELETE;
1658 case GOT_FILEIDX_STAGE_MODIFY:
1659 return GOT_STATUS_MODIFY;
1661 return GOT_STATUS_NO_CHANGE;
1665 static const struct got_error *
1666 get_symlink_modification_status(unsigned char *status,
1667 struct got_fileindex_entry *ie, const char *abspath,
1668 int dirfd, const char *de_name, struct got_blob_object *blob)
1670 const struct got_error *err = NULL;
1671 char target_path[PATH_MAX];
1672 char etarget[PATH_MAX];
1674 size_t len, target_len = 0;
1675 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1676 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1678 *status = GOT_STATUS_NO_CHANGE;
1680 /* Blob object content specifies the target path of the link. */
1682 err = got_object_blob_read_block(&len, blob);
1685 if (len + target_len >= sizeof(target_path)) {
1687 * Should not happen. The blob contents were OK
1688 * when this symlink was installed.
1690 return got_error(GOT_ERR_NO_SPACE);
1693 /* Skip blob object header first time around. */
1694 memcpy(target_path + target_len, buf + hdrlen,
1696 target_len += len - hdrlen;
1700 target_path[target_len] = '\0';
1703 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1705 return got_error_from_errno2("readlinkat", abspath);
1707 elen = readlink(abspath, etarget, sizeof(etarget));
1709 return got_error_from_errno2("readlink", abspath);
1712 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1713 *status = GOT_STATUS_MODIFY;
1718 static const struct got_error *
1719 get_file_status(unsigned char *status, struct stat *sb,
1720 struct got_fileindex_entry *ie, const char *abspath,
1721 int dirfd, const char *de_name, struct got_repository *repo)
1723 const struct got_error *err = NULL;
1724 struct got_object_id id;
1726 int fd = -1, fd1 = -1;
1729 struct got_blob_object *blob = NULL;
1731 unsigned char staged_status;
1733 staged_status = get_staged_status(ie);
1734 *status = GOT_STATUS_NO_CHANGE;
1735 memset(sb, 0, sizeof(*sb));
1738 * Whenever the caller provides a directory descriptor and a
1739 * directory entry name for the file, use them! This prevents
1740 * race conditions if filesystem paths change beneath our feet.
1743 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1744 if (errno == ENOENT) {
1745 if (got_fileindex_entry_has_file_on_disk(ie))
1746 *status = GOT_STATUS_MISSING;
1748 *status = GOT_STATUS_DELETE;
1751 err = got_error_from_errno2("fstatat", abspath);
1755 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1756 if (fd == -1 && errno != ENOENT &&
1757 !got_err_open_nofollow_on_symlink())
1758 return got_error_from_errno2("open", abspath);
1759 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1760 if (lstat(abspath, sb) == -1)
1761 return got_error_from_errno2("lstat", abspath);
1762 } else if (fd == -1 || fstat(fd, sb) == -1) {
1763 if (errno == ENOENT) {
1764 if (got_fileindex_entry_has_file_on_disk(ie))
1765 *status = GOT_STATUS_MISSING;
1767 *status = GOT_STATUS_DELETE;
1770 err = got_error_from_errno2("fstat", abspath);
1775 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1776 *status = GOT_STATUS_OBSTRUCTED;
1780 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1781 *status = GOT_STATUS_DELETE;
1783 } else if (!got_fileindex_entry_has_blob(ie) &&
1784 staged_status != GOT_STATUS_ADD) {
1785 *status = GOT_STATUS_ADD;
1789 if (!stat_info_differs(ie, sb))
1792 if (S_ISLNK(sb->st_mode) &&
1793 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1794 *status = GOT_STATUS_MODIFY;
1798 if (staged_status == GOT_STATUS_MODIFY ||
1799 staged_status == GOT_STATUS_ADD)
1800 got_fileindex_entry_get_staged_blob_id(&id, ie);
1802 got_fileindex_entry_get_blob_id(&id, ie);
1804 fd1 = got_opentempfd();
1806 err = got_error_from_errno("got_opentempfd");
1809 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1813 if (S_ISLNK(sb->st_mode)) {
1814 err = get_symlink_modification_status(status, ie,
1815 abspath, dirfd, de_name, blob);
1820 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1822 err = got_error_from_errno2("openat", abspath);
1827 f = fdopen(fd, "r");
1829 err = got_error_from_errno2("fdopen", abspath);
1833 hdrlen = got_object_blob_get_hdrlen(blob);
1835 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1836 err = got_object_blob_read_block(&blen, blob);
1839 /* Skip length of blob object header first time around. */
1840 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1841 if (flen == 0 && ferror(f)) {
1842 err = got_error_from_errno("fread");
1845 if (blen - hdrlen == 0) {
1847 *status = GOT_STATUS_MODIFY;
1849 } else if (flen == 0) {
1850 if (blen - hdrlen != 0)
1851 *status = GOT_STATUS_MODIFY;
1853 } else if (blen - hdrlen == flen) {
1854 /* Skip blob object header first time around. */
1855 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1856 *status = GOT_STATUS_MODIFY;
1860 *status = GOT_STATUS_MODIFY;
1866 if (*status == GOT_STATUS_MODIFY) {
1868 err = get_modified_file_content_status(status, blob, ie->path,
1870 } else if (xbit_differs(ie, sb->st_mode))
1871 *status = GOT_STATUS_MODE_CHANGE;
1873 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1874 err = got_error_from_errno("close");
1876 got_object_blob_close(blob);
1877 if (f != NULL && fclose(f) == EOF && err == NULL)
1878 err = got_error_from_errno2("fclose", abspath);
1879 if (fd != -1 && close(fd) == -1 && err == NULL)
1880 err = got_error_from_errno2("close", abspath);
1885 * Update timestamps in the file index if a file is unmodified and
1886 * we had to run a full content comparison to find out.
1888 static const struct got_error *
1889 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1890 struct got_fileindex_entry *ie, struct stat *sb)
1892 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1893 return got_fileindex_entry_update(ie, wt_fd, path,
1894 ie->blob_sha1, ie->commit_sha1, 1);
1899 static const struct got_error *remove_ondisk_file(const char *, const char *);
1901 static const struct got_error *
1902 update_blob(struct got_worktree *worktree,
1903 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1904 struct got_tree_entry *te, const char *path,
1905 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1908 const struct got_error *err = NULL;
1909 struct got_blob_object *blob = NULL;
1910 char *ondisk_path = NULL;
1911 unsigned char status = GOT_STATUS_NO_CHANGE;
1913 int fd1 = -1, fd2 = -1;
1915 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1916 return got_error_from_errno("asprintf");
1919 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1920 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1923 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1927 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1928 sb.st_mode = got_fileindex_perms_to_st(ie);
1930 if (stat(ondisk_path, &sb) == -1) {
1931 if (errno != ENOENT && errno != ENOTDIR) {
1932 err = got_error_from_errno2("stat",
1936 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1937 status = GOT_STATUS_UNVERSIONED;
1939 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1940 status = GOT_STATUS_UNVERSIONED;
1942 status = GOT_STATUS_OBSTRUCTED;
1946 if (status == GOT_STATUS_OBSTRUCTED) {
1948 got_fileindex_entry_mark_skipped(ie);
1949 err = (*progress_cb)(progress_arg, status, path);
1952 if (status == GOT_STATUS_CONFLICT) {
1954 got_fileindex_entry_mark_skipped(ie);
1955 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1960 if (S_ISDIR(te->mode)) { /* file changing into a directory */
1961 if (status == GOT_STATUS_UNVERSIONED) {
1962 err = (*progress_cb)(progress_arg, status, path);
1963 } else if (status != GOT_STATUS_NO_CHANGE &&
1964 status != GOT_STATUS_DELETE &&
1965 status != GOT_STATUS_NONEXISTENT &&
1966 status != GOT_STATUS_MISSING) {
1967 err = (*progress_cb)(progress_arg,
1968 GOT_STATUS_CANNOT_DELETE, path);
1970 if (status != GOT_STATUS_DELETE &&
1971 status != GOT_STATUS_NONEXISTENT &&
1972 status != GOT_STATUS_MISSING) {
1973 err = remove_ondisk_file(worktree->root_path,
1975 if (err && !(err->code == GOT_ERR_ERRNO &&
1979 got_fileindex_entry_remove(fileindex, ie);
1980 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE,
1983 goto done; /* nothing else to do */
1986 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1987 (S_ISLNK(te->mode) ||
1988 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1990 * This is a regular file or an installed bad symlink.
1991 * If the file index indicates that this file is already
1992 * up-to-date with respect to the repository we can skip
1993 * updating contents of this file.
1995 if (got_fileindex_entry_has_commit(ie) &&
1996 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1997 SHA1_DIGEST_LENGTH) == 0) {
1999 err = sync_timestamps(worktree->root_fd,
2000 path, status, ie, &sb);
2003 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2007 if (got_fileindex_entry_has_blob(ie) &&
2008 memcmp(ie->blob_sha1, te->id.sha1,
2009 SHA1_DIGEST_LENGTH) == 0) {
2010 /* Different commit but the same blob. */
2011 if (got_fileindex_entry_has_commit(ie)) {
2012 /* Update the base commit ID of this file. */
2013 memcpy(ie->commit_sha1,
2014 worktree->base_commit_id->sha1,
2015 sizeof(ie->commit_sha1));
2017 err = sync_timestamps(worktree->root_fd,
2018 path, status, ie, &sb);
2021 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2027 fd1 = got_opentempfd();
2029 err = got_error_from_errno("got_opentempfd");
2032 err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
2036 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
2037 int update_timestamps;
2038 struct got_blob_object *blob2 = NULL;
2039 char *label_orig = NULL;
2040 if (got_fileindex_entry_has_blob(ie)) {
2041 fd2 = got_opentempfd();
2043 err = got_error_from_errno("got_opentempfd");
2046 struct got_object_id id2;
2047 got_fileindex_entry_get_blob_id(&id2, ie);
2048 err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
2053 if (got_fileindex_entry_has_commit(ie)) {
2054 char id_str[SHA1_DIGEST_STRING_LENGTH];
2055 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2056 sizeof(id_str)) == NULL) {
2057 err = got_error_path(id_str,
2058 GOT_ERR_BAD_OBJ_ID_STR);
2061 if (asprintf(&label_orig, "%s: commit %s",
2062 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2063 err = got_error_from_errno("asprintf");
2067 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2069 err = got_object_blob_read_to_str(&link_target, blob);
2072 err = merge_symlink(worktree, blob2, ondisk_path, path,
2073 label_orig, link_target, worktree->base_commit_id,
2074 repo, progress_cb, progress_arg);
2077 err = merge_blob(&update_timestamps, worktree, blob2,
2078 ondisk_path, path, sb.st_mode, label_orig, blob,
2079 worktree->base_commit_id, repo,
2080 progress_cb, progress_arg);
2083 if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2084 err = got_error_from_errno("close");
2088 got_object_blob_close(blob2);
2092 * Do not update timestamps of files with local changes.
2093 * Otherwise, a future status walk would treat them as
2094 * unmodified files again.
2096 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2097 blob->id.sha1, worktree->base_commit_id->sha1,
2099 } else if (status == GOT_STATUS_MODE_CHANGE) {
2100 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2101 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2102 } else if (status == GOT_STATUS_DELETE) {
2103 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2106 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2107 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2111 int is_bad_symlink = 0;
2112 if (S_ISLNK(te->mode)) {
2113 err = install_symlink(&is_bad_symlink, worktree,
2114 ondisk_path, path, blob,
2115 status == GOT_STATUS_MISSING, 0,
2116 status == GOT_STATUS_UNVERSIONED, 0,
2117 repo, progress_cb, progress_arg);
2119 err = install_blob(worktree, ondisk_path, path,
2120 te->mode, sb.st_mode, blob,
2121 status == GOT_STATUS_MISSING, 0, 0,
2122 status == GOT_STATUS_UNVERSIONED, repo,
2123 progress_cb, progress_arg);
2129 err = got_fileindex_entry_update(ie,
2130 worktree->root_fd, path, blob->id.sha1,
2131 worktree->base_commit_id->sha1, 1);
2133 err = create_fileindex_entry(&ie, fileindex,
2134 worktree->base_commit_id, worktree->root_fd, path,
2140 if (is_bad_symlink) {
2141 got_fileindex_entry_filetype_set(ie,
2142 GOT_FILEIDX_MODE_BAD_SYMLINK);
2146 if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2147 err = got_error_from_errno("close");
2150 got_object_blob_close(blob);
2156 static const struct got_error *
2157 remove_ondisk_file(const char *root_path, const char *path)
2159 const struct got_error *err = NULL;
2160 char *ondisk_path = NULL, *parent = NULL;
2162 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2163 return got_error_from_errno("asprintf");
2165 if (unlink(ondisk_path) == -1) {
2166 if (errno != ENOENT)
2167 err = got_error_from_errno2("unlink", ondisk_path);
2169 size_t root_len = strlen(root_path);
2170 err = got_path_dirname(&parent, ondisk_path);
2173 while (got_path_cmp(parent, root_path,
2174 strlen(parent), root_len) != 0) {
2176 ondisk_path = parent;
2178 if (rmdir(ondisk_path) == -1) {
2179 if (errno != ENOTEMPTY)
2180 err = got_error_from_errno2("rmdir",
2184 err = got_path_dirname(&parent, ondisk_path);
2195 static const struct got_error *
2196 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2197 struct got_fileindex_entry *ie, struct got_repository *repo,
2198 got_worktree_checkout_cb progress_cb, void *progress_arg)
2200 const struct got_error *err = NULL;
2201 unsigned char status;
2205 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2206 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2208 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2210 return got_error_from_errno("asprintf");
2212 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2216 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2217 char ondisk_target[PATH_MAX];
2218 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2219 sizeof(ondisk_target));
2220 if (ondisk_len == -1) {
2221 err = got_error_from_errno2("readlink", ondisk_path);
2224 ondisk_target[ondisk_len] = '\0';
2225 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2226 NULL, NULL, /* XXX pass common ancestor info? */
2227 ondisk_target, ondisk_path);
2230 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2235 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2236 status == GOT_STATUS_ADD) {
2237 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2241 * Preserve the working file and change the deleted blob's
2242 * entry into a schedule-add entry.
2244 err = got_fileindex_entry_update(ie, worktree->root_fd,
2245 ie->path, NULL, NULL, 0);
2247 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2250 if (status == GOT_STATUS_NO_CHANGE) {
2251 err = remove_ondisk_file(worktree->root_path, ie->path);
2255 got_fileindex_entry_remove(fileindex, ie);
2262 struct diff_cb_arg {
2263 struct got_fileindex *fileindex;
2264 struct got_worktree *worktree;
2265 struct got_repository *repo;
2266 got_worktree_checkout_cb progress_cb;
2268 got_cancel_cb cancel_cb;
2272 static const struct got_error *
2273 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2274 struct got_tree_entry *te, const char *parent_path)
2276 const struct got_error *err = NULL;
2277 struct diff_cb_arg *a = arg;
2280 err = a->cancel_cb(a->cancel_arg);
2285 return update_blob(a->worktree, a->fileindex, ie, te,
2286 ie->path, a->repo, a->progress_cb, a->progress_arg);
2289 static const struct got_error *
2290 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2292 const struct got_error *err = NULL;
2293 struct diff_cb_arg *a = arg;
2296 err = a->cancel_cb(a->cancel_arg);
2301 return delete_blob(a->worktree, a->fileindex, ie,
2302 a->repo, a->progress_cb, a->progress_arg);
2305 static const struct got_error *
2306 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2308 const struct got_error *err = NULL;
2309 struct diff_cb_arg *a = arg;
2313 err = a->cancel_cb(a->cancel_arg);
2318 if (got_object_tree_entry_is_submodule(te))
2321 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2324 if (asprintf(&path, "%s%s%s", parent_path,
2325 parent_path[0] ? "/" : "", te->name)
2327 return got_error_from_errno("asprintf");
2329 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2330 a->repo, a->progress_cb, a->progress_arg);
2336 const struct got_error *
2337 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2339 uint32_t uuid_status;
2341 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2342 if (uuid_status != uuid_s_ok) {
2344 return got_error_uuid(uuid_status, "uuid_to_string");
2350 static const struct got_error *
2351 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2353 const struct got_error *err = NULL;
2354 char *uuidstr = NULL;
2358 err = got_worktree_get_uuid(&uuidstr, worktree);
2362 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2363 err = got_error_from_errno("asprintf");
2370 const struct got_error *
2371 got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2374 return get_ref_name(refname, worktree, prefix);
2377 static const struct got_error *
2378 get_base_ref_name(char **refname, struct got_worktree *worktree)
2380 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2383 static const struct got_error *
2384 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2386 return get_ref_name(refname, worktree,
2387 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2390 static const struct got_error *
2391 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2393 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2396 static const struct got_error *
2397 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2399 return get_ref_name(refname, worktree,
2400 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2403 static const struct got_error *
2404 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2406 return get_ref_name(refname, worktree,
2407 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2410 static const struct got_error *
2411 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2413 return get_ref_name(refname, worktree,
2414 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2417 static const struct got_error *
2418 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2420 return get_ref_name(refname, worktree,
2421 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2424 static const struct got_error *
2425 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2427 return get_ref_name(refname, worktree,
2428 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2431 static const struct got_error *
2432 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2434 return get_ref_name(refname, worktree,
2435 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2438 const struct got_error *
2439 got_worktree_get_histedit_script_path(char **path,
2440 struct got_worktree *worktree)
2442 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2443 worktree->meta_dir, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2445 return got_error_from_errno("asprintf");
2450 static const struct got_error *
2451 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2453 return get_ref_name(refname, worktree,
2454 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2457 static const struct got_error *
2458 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2460 return get_ref_name(refname, worktree,
2461 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2465 * Prevent Git's garbage collector from deleting our base commit by
2466 * setting a reference to our base commit's ID.
2468 static const struct got_error *
2469 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2471 const struct got_error *err = NULL;
2472 struct got_reference *ref = NULL;
2475 err = get_base_ref_name(&refname, worktree);
2479 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2483 err = got_ref_write(ref, repo);
2491 static const struct got_error *
2492 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2494 const struct got_error *err = NULL;
2496 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2497 worktree->meta_dir, GOT_WORKTREE_FILE_INDEX) == -1) {
2498 err = got_error_from_errno("asprintf");
2499 *fileindex_path = NULL;
2505 static const struct got_error *
2506 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2507 struct got_worktree *worktree)
2509 const struct got_error *err = NULL;
2512 *fileindex_path = NULL;
2513 *fileindex = got_fileindex_alloc();
2514 if (*fileindex == NULL)
2515 return got_error_from_errno("got_fileindex_alloc");
2517 err = get_fileindex_path(fileindex_path, worktree);
2521 index = fopen(*fileindex_path, "rbe");
2522 if (index == NULL) {
2523 if (errno != ENOENT)
2524 err = got_error_from_errno2("fopen", *fileindex_path);
2526 err = got_fileindex_read(*fileindex, index);
2527 if (fclose(index) == EOF && err == NULL)
2528 err = got_error_from_errno("fclose");
2532 free(*fileindex_path);
2533 *fileindex_path = NULL;
2534 got_fileindex_free(*fileindex);
2540 struct bump_base_commit_id_arg {
2541 struct got_object_id *base_commit_id;
2544 const char *entry_name;
2545 got_worktree_checkout_cb progress_cb;
2549 static const struct got_error *
2550 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2552 const struct got_error *err;
2553 struct bump_base_commit_id_arg *a = arg;
2555 if (a->entry_name) {
2556 if (strcmp(ie->path, a->path) != 0)
2558 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2561 if (got_fileindex_entry_was_skipped(ie))
2564 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2565 SHA1_DIGEST_LENGTH) == 0)
2568 if (a->progress_cb) {
2569 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2574 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2578 /* Bump base commit ID of all files within an updated part of the work tree. */
2579 static const struct got_error *
2580 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2581 struct got_fileindex *fileindex,
2582 got_worktree_checkout_cb progress_cb, void *progress_arg)
2584 struct bump_base_commit_id_arg bbc_arg;
2586 bbc_arg.base_commit_id = worktree->base_commit_id;
2587 bbc_arg.entry_name = NULL;
2589 bbc_arg.path_len = 0;
2590 bbc_arg.progress_cb = progress_cb;
2591 bbc_arg.progress_arg = progress_arg;
2593 return got_fileindex_for_each_entry_safe(fileindex,
2594 bump_base_commit_id, &bbc_arg);
2597 static const struct got_error *
2598 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2600 const struct got_error *err = NULL;
2601 char *new_fileindex_path = NULL;
2602 FILE *new_index = NULL;
2603 struct timespec timeout;
2605 err = got_opentemp_named(&new_fileindex_path, &new_index,
2606 fileindex_path, "");
2610 err = got_fileindex_write(fileindex, new_index);
2614 if (rename(new_fileindex_path, fileindex_path) != 0) {
2615 err = got_error_from_errno3("rename", new_fileindex_path,
2617 unlink(new_fileindex_path);
2621 * Sleep for a short amount of time to ensure that files modified after
2622 * this program exits have a different time stamp from the one which
2623 * was recorded in the file index.
2626 timeout.tv_nsec = 1;
2627 nanosleep(&timeout, NULL);
2631 free(new_fileindex_path);
2635 static const struct got_error *
2636 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2637 struct got_object_id **tree_id, const char *wt_relpath,
2638 struct got_commit_object *base_commit, struct got_worktree *worktree,
2639 struct got_repository *repo)
2641 const struct got_error *err = NULL;
2642 struct got_object_id *id = NULL;
2643 char *in_repo_path = NULL;
2644 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2646 *entry_type = GOT_OBJ_TYPE_ANY;
2647 *tree_relpath = NULL;
2650 if (wt_relpath[0] == '\0') {
2651 /* Check out all files within the work tree. */
2652 *entry_type = GOT_OBJ_TYPE_TREE;
2653 *tree_relpath = strdup("");
2654 if (*tree_relpath == NULL) {
2655 err = got_error_from_errno("strdup");
2658 err = got_object_id_by_path(tree_id, repo, base_commit,
2659 worktree->path_prefix);
2665 /* Check out a subset of files in the work tree. */
2667 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2668 is_root_wt ? "" : "/", wt_relpath) == -1) {
2669 err = got_error_from_errno("asprintf");
2673 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2678 in_repo_path = NULL;
2680 err = got_object_get_type(entry_type, repo, id);
2684 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2685 /* Check out a single file. */
2686 if (strchr(wt_relpath, '/') == NULL) {
2687 /* Check out a single file in work tree's root dir. */
2688 in_repo_path = strdup(worktree->path_prefix);
2689 if (in_repo_path == NULL) {
2690 err = got_error_from_errno("strdup");
2693 *tree_relpath = strdup("");
2694 if (*tree_relpath == NULL) {
2695 err = got_error_from_errno("strdup");
2699 /* Check out a single file in a subdirectory. */
2700 err = got_path_dirname(tree_relpath, wt_relpath);
2703 if (asprintf(&in_repo_path, "%s%s%s",
2704 worktree->path_prefix, is_root_wt ? "" : "/",
2705 *tree_relpath) == -1) {
2706 err = got_error_from_errno("asprintf");
2710 err = got_object_id_by_path(tree_id, repo,
2711 base_commit, in_repo_path);
2713 /* Check out all files within a subdirectory. */
2714 *tree_id = got_object_id_dup(id);
2715 if (*tree_id == NULL) {
2716 err = got_error_from_errno("got_object_id_dup");
2719 *tree_relpath = strdup(wt_relpath);
2720 if (*tree_relpath == NULL) {
2721 err = got_error_from_errno("strdup");
2729 *entry_type = GOT_OBJ_TYPE_ANY;
2730 free(*tree_relpath);
2731 *tree_relpath = NULL;
2738 static const struct got_error *
2739 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2740 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2741 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2742 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2744 const struct got_error *err = NULL;
2745 struct got_commit_object *commit = NULL;
2746 struct got_tree_object *tree = NULL;
2747 struct got_fileindex_diff_tree_cb diff_cb;
2748 struct diff_cb_arg arg;
2750 err = ref_base_commit(worktree, repo);
2752 if (!(err->code == GOT_ERR_ERRNO &&
2753 (errno == EACCES || errno == EROFS)))
2755 err = (*progress_cb)(progress_arg,
2756 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2761 err = got_object_open_as_commit(&commit, repo,
2762 worktree->base_commit_id);
2766 err = got_object_open_as_tree(&tree, repo, tree_id);
2771 got_object_tree_find_entry(tree, entry_name) == NULL) {
2772 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2776 diff_cb.diff_old_new = diff_old_new;
2777 diff_cb.diff_old = diff_old;
2778 diff_cb.diff_new = diff_new;
2779 arg.fileindex = fileindex;
2780 arg.worktree = worktree;
2782 arg.progress_cb = progress_cb;
2783 arg.progress_arg = progress_arg;
2784 arg.cancel_cb = cancel_cb;
2785 arg.cancel_arg = cancel_arg;
2786 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2787 entry_name, repo, &diff_cb, &arg);
2790 got_object_tree_close(tree);
2792 got_object_commit_close(commit);
2796 const struct got_error *
2797 got_worktree_checkout_files(struct got_worktree *worktree,
2798 struct got_pathlist_head *paths, struct got_repository *repo,
2799 got_worktree_checkout_cb progress_cb, void *progress_arg,
2800 got_cancel_cb cancel_cb, void *cancel_arg)
2802 const struct got_error *err = NULL, *sync_err, *unlockerr;
2803 struct got_commit_object *commit = NULL;
2804 struct got_tree_object *tree = NULL;
2805 struct got_fileindex *fileindex = NULL;
2806 char *fileindex_path = NULL;
2807 struct got_pathlist_entry *pe;
2808 struct tree_path_data {
2809 STAILQ_ENTRY(tree_path_data) entry;
2810 struct got_object_id *tree_id;
2815 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2817 STAILQ_INIT(&tree_paths);
2819 err = lock_worktree(worktree, LOCK_EX);
2823 err = got_object_open_as_commit(&commit, repo,
2824 worktree->base_commit_id);
2828 /* Map all specified paths to in-repository trees. */
2829 TAILQ_FOREACH(pe, paths, entry) {
2830 tpd = malloc(sizeof(*tpd));
2832 err = got_error_from_errno("malloc");
2836 err = find_tree_entry_for_checkout(&tpd->entry_type,
2837 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2844 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2845 err = got_path_basename(&tpd->entry_name, pe->path);
2853 tpd->entry_name = NULL;
2855 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2859 * Read the file index.
2860 * Checking out files is supposed to be an idempotent operation.
2861 * If the on-disk file index is incomplete we will try to complete it.
2863 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2867 tpd = STAILQ_FIRST(&tree_paths);
2868 TAILQ_FOREACH(pe, paths, entry) {
2869 struct bump_base_commit_id_arg bbc_arg;
2871 err = checkout_files(worktree, fileindex, tpd->relpath,
2872 tpd->tree_id, tpd->entry_name, repo,
2873 progress_cb, progress_arg, cancel_cb, cancel_arg);
2877 bbc_arg.base_commit_id = worktree->base_commit_id;
2878 bbc_arg.entry_name = tpd->entry_name;
2879 bbc_arg.path = pe->path;
2880 bbc_arg.path_len = pe->path_len;
2881 bbc_arg.progress_cb = progress_cb;
2882 bbc_arg.progress_arg = progress_arg;
2883 err = got_fileindex_for_each_entry_safe(fileindex,
2884 bump_base_commit_id, &bbc_arg);
2888 tpd = STAILQ_NEXT(tpd, entry);
2890 sync_err = sync_fileindex(fileindex, fileindex_path);
2891 if (sync_err && err == NULL)
2894 free(fileindex_path);
2896 got_object_tree_close(tree);
2898 got_object_commit_close(commit);
2900 got_fileindex_free(fileindex);
2901 while (!STAILQ_EMPTY(&tree_paths)) {
2902 tpd = STAILQ_FIRST(&tree_paths);
2903 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2908 unlockerr = lock_worktree(worktree, LOCK_SH);
2909 if (unlockerr && err == NULL)
2914 static const struct got_error *
2915 add_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2916 struct got_fileindex_entry *ie, const char *ondisk_path,
2917 const char *path2, struct got_blob_object *blob2, mode_t mode2,
2918 int restoring_missing_file, int reverting_versioned_file,
2919 int path_is_unversioned, int allow_bad_symlinks,
2920 struct got_repository *repo,
2921 got_worktree_checkout_cb progress_cb, void *progress_arg)
2923 const struct got_error *err = NULL;
2924 int is_bad_symlink = 0;
2926 if (S_ISLNK(mode2)) {
2927 err = install_symlink(&is_bad_symlink,
2928 worktree, ondisk_path, path2, blob2,
2929 restoring_missing_file,
2930 reverting_versioned_file,
2931 path_is_unversioned, allow_bad_symlinks,
2932 repo, progress_cb, progress_arg);
2934 err = install_blob(worktree, ondisk_path, path2,
2935 mode2, GOT_DEFAULT_FILE_MODE, blob2,
2936 restoring_missing_file, reverting_versioned_file, 0,
2937 path_is_unversioned, repo, progress_cb, progress_arg);
2942 /* Adding an unversioned file. */
2943 err = got_fileindex_entry_alloc(&ie, path2);
2946 err = got_fileindex_entry_update(ie,
2947 worktree->root_fd, path2, NULL, NULL, 1);
2949 got_fileindex_entry_free(ie);
2952 err = got_fileindex_entry_add(fileindex, ie);
2954 got_fileindex_entry_free(ie);
2958 /* Re-adding a locally deleted file. */
2959 err = got_fileindex_entry_update(ie,
2960 worktree->root_fd, path2, ie->blob_sha1,
2961 worktree->base_commit_id->sha1, 0);
2966 if (is_bad_symlink) {
2967 got_fileindex_entry_filetype_set(ie,
2968 GOT_FILEIDX_MODE_BAD_SYMLINK);
2974 struct merge_file_cb_arg {
2975 struct got_worktree *worktree;
2976 struct got_fileindex *fileindex;
2977 got_worktree_checkout_cb progress_cb;
2979 got_cancel_cb cancel_cb;
2981 const char *label_orig;
2982 struct got_object_id *commit_id2;
2983 int allow_bad_symlinks;
2986 static const struct got_error *
2987 merge_file_cb(void *arg, struct got_blob_object *blob1,
2988 struct got_blob_object *blob2, FILE *f1, FILE *f2,
2989 struct got_object_id *id1, struct got_object_id *id2,
2990 const char *path1, const char *path2,
2991 mode_t mode1, mode_t mode2, struct got_repository *repo)
2993 static const struct got_error *err = NULL;
2994 struct merge_file_cb_arg *a = arg;
2995 struct got_fileindex_entry *ie;
2996 char *ondisk_path = NULL;
2998 unsigned char status;
2999 int local_changes_subsumed;
3000 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
3001 char *id_str = NULL, *label_deriv2 = NULL;
3002 struct got_object_id *id = NULL;
3004 if (blob1 && blob2) {
3005 ie = got_fileindex_entry_get(a->fileindex, path2,
3008 return (*a->progress_cb)(a->progress_arg,
3009 GOT_STATUS_MISSING, path2);
3011 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3013 return got_error_from_errno("asprintf");
3015 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3020 if (status == GOT_STATUS_DELETE) {
3021 err = (*a->progress_cb)(a->progress_arg,
3022 GOT_STATUS_MERGE, path2);
3025 if (status != GOT_STATUS_NO_CHANGE &&
3026 status != GOT_STATUS_MODIFY &&
3027 status != GOT_STATUS_CONFLICT &&
3028 status != GOT_STATUS_ADD) {
3029 err = (*a->progress_cb)(a->progress_arg, status, path2);
3033 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
3035 err = got_object_blob_read_to_str(&link_target2, blob2);
3038 err = merge_symlink(a->worktree, blob1, ondisk_path,
3039 path2, a->label_orig, link_target2, a->commit_id2,
3040 repo, a->progress_cb, a->progress_arg);
3045 f_orig = got_opentemp();
3046 if (f_orig == NULL) {
3047 err = got_error_from_errno("got_opentemp");
3050 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3055 f_deriv2 = got_opentemp();
3056 if (f_deriv2 == NULL)
3058 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3063 fd = open(ondisk_path,
3064 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3066 err = got_error_from_errno2("open",
3070 f_deriv = fdopen(fd, "r");
3071 if (f_deriv == NULL) {
3072 err = got_error_from_errno2("fdopen",
3077 err = got_object_id_str(&id_str, a->commit_id2);
3080 if (asprintf(&label_deriv2, "%s: commit %s",
3081 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
3082 err = got_error_from_errno("asprintf");
3085 err = merge_file(&local_changes_subsumed, a->worktree,
3086 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
3087 mode2, a->label_orig, NULL, label_deriv2,
3088 GOT_DIFF_ALGORITHM_PATIENCE, repo,
3089 a->progress_cb, a->progress_arg);
3092 ie = got_fileindex_entry_get(a->fileindex, path1,
3095 return (*a->progress_cb)(a->progress_arg,
3096 GOT_STATUS_MISSING, path1);
3098 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3100 return got_error_from_errno("asprintf");
3102 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3108 case GOT_STATUS_NO_CHANGE:
3109 err = (*a->progress_cb)(a->progress_arg,
3110 GOT_STATUS_DELETE, path1);
3113 err = remove_ondisk_file(a->worktree->root_path, path1);
3117 got_fileindex_entry_mark_deleted_from_disk(ie);
3119 case GOT_STATUS_DELETE:
3120 case GOT_STATUS_MISSING:
3121 err = (*a->progress_cb)(a->progress_arg,
3122 GOT_STATUS_DELETE, path1);
3126 got_fileindex_entry_mark_deleted_from_disk(ie);
3128 case GOT_STATUS_MODIFY: {
3133 * Delete the file only if its content already
3134 * exists in the repository.
3136 err = got_object_blob_file_create(&id, &blob1_f,
3137 &blob1_size, path1);
3140 if (fclose(blob1_f) == EOF) {
3141 err = got_error_from_errno("fclose");
3145 /* Implied existence check. */
3146 err = got_object_get_type(&obj_type, repo, id);
3148 if (err->code != GOT_ERR_NO_OBJ)
3150 err = (*a->progress_cb)(a->progress_arg,
3151 GOT_STATUS_CANNOT_DELETE, path1);
3153 } else if (obj_type != GOT_OBJ_TYPE_BLOB) {
3154 err = (*a->progress_cb)(a->progress_arg,
3155 GOT_STATUS_CANNOT_DELETE, path1);
3158 err = (*a->progress_cb)(a->progress_arg,
3159 GOT_STATUS_DELETE, path1);
3162 err = remove_ondisk_file(a->worktree->root_path,
3167 got_fileindex_entry_mark_deleted_from_disk(ie);
3170 case GOT_STATUS_ADD: {
3174 * Delete the file only if its content already
3175 * exists in the repository.
3177 err = got_object_blob_file_create(&id, &blob1_f,
3178 &blob1_size, path1);
3181 if (fclose(blob1_f) == EOF) {
3182 err = got_error_from_errno("fclose");
3185 if (got_object_id_cmp(id, id1) == 0) {
3186 err = (*a->progress_cb)(a->progress_arg,
3187 GOT_STATUS_DELETE, path1);
3190 err = remove_ondisk_file(a->worktree->root_path,
3195 got_fileindex_entry_remove(a->fileindex,
3198 err = (*a->progress_cb)(a->progress_arg,
3199 GOT_STATUS_CANNOT_DELETE, path1);
3205 case GOT_STATUS_CONFLICT:
3206 err = (*a->progress_cb)(a->progress_arg,
3207 GOT_STATUS_CANNOT_DELETE, path1);
3211 case GOT_STATUS_OBSTRUCTED:
3212 err = (*a->progress_cb)(a->progress_arg, status, path1);
3220 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3222 return got_error_from_errno("asprintf");
3223 ie = got_fileindex_entry_get(a->fileindex, path2,
3226 err = get_file_status(&status, &sb, ie, ondisk_path,
3230 if (status != GOT_STATUS_NO_CHANGE &&
3231 status != GOT_STATUS_MODIFY &&
3232 status != GOT_STATUS_CONFLICT &&
3233 status != GOT_STATUS_ADD &&
3234 status != GOT_STATUS_DELETE) {
3235 err = (*a->progress_cb)(a->progress_arg,
3239 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3241 err = got_object_blob_read_to_str(&link_target2,
3245 err = merge_symlink(a->worktree, NULL,
3246 ondisk_path, path2, a->label_orig,
3247 link_target2, a->commit_id2, repo,
3248 a->progress_cb, a->progress_arg);
3250 } else if (S_ISREG(sb.st_mode)) {
3251 err = merge_blob(&local_changes_subsumed,
3252 a->worktree, NULL, ondisk_path, path2,
3253 sb.st_mode, a->label_orig, blob2,
3254 a->commit_id2, repo, a->progress_cb,
3256 } else if (status != GOT_STATUS_DELETE) {
3257 err = got_error_path(ondisk_path,
3258 GOT_ERR_FILE_OBSTRUCTED);
3262 if (status == GOT_STATUS_DELETE) {
3263 /* Re-add file with content from new blob. */
3264 err = add_file(a->worktree, a->fileindex, ie,
3265 ondisk_path, path2, blob2, mode2,
3266 0, 0, 0, a->allow_bad_symlinks,
3267 repo, a->progress_cb, a->progress_arg);
3272 err = add_file(a->worktree, a->fileindex, NULL,
3273 ondisk_path, path2, blob2, mode2,
3274 0, 0, 1, a->allow_bad_symlinks,
3275 repo, a->progress_cb, a->progress_arg);
3281 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3282 err = got_error_from_errno("fclose");
3283 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3284 err = got_error_from_errno("fclose");
3285 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3286 err = got_error_from_errno("fclose");
3294 struct check_mixed_commits_args {
3295 struct got_worktree *worktree;
3296 got_cancel_cb cancel_cb;
3300 static const struct got_error *
3301 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3303 const struct got_error *err = NULL;
3304 struct check_mixed_commits_args *a = arg;
3307 err = a->cancel_cb(a->cancel_arg);
3312 /* Reject merges into a work tree with mixed base commits. */
3313 if (got_fileindex_entry_has_commit(ie) &&
3314 memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3315 SHA1_DIGEST_LENGTH) != 0)
3316 return got_error(GOT_ERR_MIXED_COMMITS);
3321 const struct got_error *
3322 got_worktree_get_state(char *state, struct got_repository *repo,
3323 struct got_worktree *worktree,
3324 got_cancel_cb cancel_cb, void *cancel_arg)
3326 const struct got_error *err;
3327 struct got_object_id *base_id, *head_id = NULL;
3328 struct got_reference *head_ref;
3329 struct got_fileindex *fileindex = NULL;
3330 char *fileindex_path = NULL;
3331 struct check_mixed_commits_args cma;
3333 if (worktree == NULL)
3334 return got_error(GOT_ERR_NOT_WORKTREE);
3336 err = got_ref_open(&head_ref, repo,
3337 got_worktree_get_head_ref_name(worktree), 0);
3341 err = got_ref_resolve(&head_id, repo, head_ref);
3345 *state = GOT_WORKTREE_STATE_UNKNOWN;
3346 base_id = got_worktree_get_base_commit_id(worktree);
3348 cma.worktree = worktree;
3349 cma.cancel_cb = cancel_cb;
3350 cma.cancel_arg = cancel_arg;
3352 if (got_object_id_cmp(base_id, head_id) == 0) {
3353 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3357 err = got_fileindex_for_each_entry_safe(fileindex,
3358 check_mixed_commits, &cma);
3360 *state = GOT_WORKTREE_STATE_UPTODATE;
3361 else if (err->code == GOT_ERR_MIXED_COMMITS) {
3362 *state = GOT_WORKTREE_STATE_OUTOFDATE;
3366 *state = GOT_WORKTREE_STATE_OUTOFDATE;
3370 free(fileindex_path);
3371 got_ref_close(head_ref);
3372 if (fileindex != NULL)
3373 got_fileindex_free(fileindex);
3377 struct check_merge_conflicts_arg {
3378 struct got_worktree *worktree;
3379 struct got_fileindex *fileindex;
3380 struct got_repository *repo;
3383 static const struct got_error *
3384 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3385 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3386 struct got_object_id *id1, struct got_object_id *id2,
3387 const char *path1, const char *path2,
3388 mode_t mode1, mode_t mode2, struct got_repository *repo)
3390 const struct got_error *err = NULL;
3391 struct check_merge_conflicts_arg *a = arg;
3392 unsigned char status;
3394 struct got_fileindex_entry *ie;
3395 const char *path = path2 ? path2 : path1;
3396 struct got_object_id *id = id2 ? id2 : id1;
3402 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3406 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3408 return got_error_from_errno("asprintf");
3410 /* Reject merges into a work tree with conflicted files. */
3411 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3415 if (status == GOT_STATUS_CONFLICT)
3416 return got_error(GOT_ERR_CONFLICTS);
3421 static const struct got_error *
3422 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3423 const char *fileindex_path, struct got_object_id *commit_id1,
3424 struct got_object_id *commit_id2, struct got_repository *repo,
3425 got_worktree_checkout_cb progress_cb, void *progress_arg,
3426 got_cancel_cb cancel_cb, void *cancel_arg)
3428 const struct got_error *err = NULL, *sync_err;
3429 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3430 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3431 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3432 struct check_merge_conflicts_arg cmc_arg;
3433 struct merge_file_cb_arg arg;
3434 char *label_orig = NULL;
3435 FILE *f1 = NULL, *f2 = NULL;
3436 int fd1 = -1, fd2 = -1;
3439 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3442 err = got_object_id_by_path(&tree_id1, repo, commit1,
3443 worktree->path_prefix);
3444 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3450 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3454 err = got_object_id_str(&id_str, commit_id1);
3458 if (asprintf(&label_orig, "%s: commit %s",
3459 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3460 err = got_error_from_errno("asprintf");
3466 f1 = got_opentemp();
3468 err = got_error_from_errno("got_opentemp");
3473 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3477 err = got_object_id_by_path(&tree_id2, repo, commit2,
3478 worktree->path_prefix);
3482 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3486 f2 = got_opentemp();
3488 err = got_error_from_errno("got_opentemp");
3492 fd1 = got_opentempfd();
3494 err = got_error_from_errno("got_opentempfd");
3498 fd2 = got_opentempfd();
3500 err = got_error_from_errno("got_opentempfd");
3504 cmc_arg.worktree = worktree;
3505 cmc_arg.fileindex = fileindex;
3506 cmc_arg.repo = repo;
3507 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3508 check_merge_conflicts, &cmc_arg, 0);
3512 arg.worktree = worktree;
3513 arg.fileindex = fileindex;
3514 arg.progress_cb = progress_cb;
3515 arg.progress_arg = progress_arg;
3516 arg.cancel_cb = cancel_cb;
3517 arg.cancel_arg = cancel_arg;
3518 arg.label_orig = label_orig;
3519 arg.commit_id2 = commit_id2;
3520 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3521 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3522 merge_file_cb, &arg, 1);
3523 sync_err = sync_fileindex(fileindex, fileindex_path);
3524 if (sync_err && err == NULL)
3528 got_object_commit_close(commit1);
3530 got_object_commit_close(commit2);
3532 got_object_tree_close(tree1);
3534 got_object_tree_close(tree2);
3535 if (f1 && fclose(f1) == EOF && err == NULL)
3536 err = got_error_from_errno("fclose");
3537 if (f2 && fclose(f2) == EOF && err == NULL)
3538 err = got_error_from_errno("fclose");
3539 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3540 err = got_error_from_errno("close");
3541 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3542 err = got_error_from_errno("close");
3547 const struct got_error *
3548 got_worktree_merge_files(struct got_worktree *worktree,
3549 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3550 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3551 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3553 const struct got_error *err, *unlockerr;
3554 char *fileindex_path = NULL;
3555 struct got_fileindex *fileindex = NULL;
3556 struct check_mixed_commits_args cma;
3558 err = lock_worktree(worktree, LOCK_EX);
3562 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3566 cma.worktree = worktree;
3567 cma.cancel_cb = cancel_cb;
3568 cma.cancel_arg = cancel_arg;
3570 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3575 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3576 commit_id2, repo, progress_cb, progress_arg,
3577 cancel_cb, cancel_arg);
3580 got_fileindex_free(fileindex);
3581 free(fileindex_path);
3582 unlockerr = lock_worktree(worktree, LOCK_SH);
3583 if (unlockerr && err == NULL)
3588 struct diff_dir_cb_arg {
3589 struct got_fileindex *fileindex;
3590 struct got_worktree *worktree;
3591 const char *status_path;
3592 size_t status_path_len;
3593 struct got_repository *repo;
3594 got_worktree_status_cb status_cb;
3596 got_cancel_cb cancel_cb;
3598 /* A pathlist containing per-directory pathlists of ignore patterns. */
3599 struct got_pathlist_head *ignores;
3600 int report_unchanged;
3604 static const struct got_error *
3605 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3606 int dirfd, const char *de_name,
3607 got_worktree_status_cb status_cb, void *status_arg,
3608 struct got_repository *repo, int report_unchanged)
3610 const struct got_error *err = NULL;
3611 unsigned char status = GOT_STATUS_NO_CHANGE;
3612 unsigned char staged_status;
3614 struct got_object_id blob_id, commit_id, staged_blob_id;
3615 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3616 struct got_object_id *staged_blob_idp = NULL;
3618 staged_status = get_staged_status(ie);
3619 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3623 if (status == GOT_STATUS_NO_CHANGE &&
3624 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3627 if (got_fileindex_entry_has_blob(ie))
3628 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3629 if (got_fileindex_entry_has_commit(ie))
3630 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3631 if (staged_status == GOT_STATUS_ADD ||
3632 staged_status == GOT_STATUS_MODIFY) {
3633 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3634 &staged_blob_id, ie);
3637 return (*status_cb)(status_arg, status, staged_status,
3638 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3641 static const struct got_error *
3642 status_old_new(void *arg, struct got_fileindex_entry *ie,
3643 struct dirent *de, const char *parent_path, int dirfd)
3645 const struct got_error *err = NULL;
3646 struct diff_dir_cb_arg *a = arg;
3650 err = a->cancel_cb(a->cancel_arg);
3655 if (got_path_cmp(parent_path, a->status_path,
3656 strlen(parent_path), a->status_path_len) != 0 &&
3657 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3660 if (parent_path[0]) {
3661 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3662 parent_path, de->d_name) == -1)
3663 return got_error_from_errno("asprintf");
3665 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3667 return got_error_from_errno("asprintf");
3670 err = report_file_status(ie, abspath, dirfd, de->d_name,
3671 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3676 static const struct got_error *
3677 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3679 const struct got_error *err;
3680 struct diff_dir_cb_arg *a = arg;
3681 struct got_object_id blob_id, commit_id;
3682 unsigned char status;
3685 err = a->cancel_cb(a->cancel_arg);
3690 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3693 got_fileindex_entry_get_blob_id(&blob_id, ie);
3694 got_fileindex_entry_get_commit_id(&commit_id, ie);
3695 if (got_fileindex_entry_has_file_on_disk(ie))
3696 status = GOT_STATUS_MISSING;
3698 status = GOT_STATUS_DELETE;
3699 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3700 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3704 free_ignores(struct got_pathlist_head *ignores)
3706 struct got_pathlist_entry *pe;
3708 TAILQ_FOREACH(pe, ignores, entry) {
3709 struct got_pathlist_head *ignorelist = pe->data;
3711 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3713 got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3716 static const struct got_error *
3717 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3719 const struct got_error *err = NULL;
3720 struct got_pathlist_entry *pe = NULL;
3721 struct got_pathlist_head *ignorelist;
3722 char *line = NULL, *pattern, *dirpath = NULL;
3723 size_t linesize = 0;
3726 ignorelist = calloc(1, sizeof(*ignorelist));
3727 if (ignorelist == NULL)
3728 return got_error_from_errno("calloc");
3729 TAILQ_INIT(ignorelist);
3731 while ((linelen = getline(&line, &linesize, f)) != -1) {
3732 if (linelen > 0 && line[linelen - 1] == '\n')
3733 line[linelen - 1] = '\0';
3735 /* Git's ignores may contain comments. */
3739 /* Git's negated patterns are not (yet?) supported. */
3743 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3745 err = got_error_from_errno("asprintf");
3748 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3753 err = got_error_from_errno("getline");
3757 dirpath = strdup(path);
3758 if (dirpath == NULL) {
3759 err = got_error_from_errno("strdup");
3762 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3765 if (err || pe == NULL) {
3767 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3773 match_path(const char *pattern, size_t pattern_len, const char *path,
3779 * Trailing slashes signify directories.
3780 * Append a * to make such patterns conform to fnmatch rules.
3782 if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
3783 if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
3784 return FNM_NOMATCH; /* XXX */
3786 return fnmatch(buf, path, flags);
3789 return fnmatch(pattern, path, flags);
3793 match_ignores(struct got_pathlist_head *ignores, const char *path)
3795 struct got_pathlist_entry *pe;
3797 /* Handle patterns which match in all directories. */
3798 TAILQ_FOREACH(pe, ignores, entry) {
3799 struct got_pathlist_head *ignorelist = pe->data;
3800 struct got_pathlist_entry *pi;
3802 TAILQ_FOREACH(pi, ignorelist, entry) {
3805 if (pi->path_len < 3 ||
3806 strncmp(pi->path, "**/", 3) != 0)
3810 if (match_path(pi->path + 3,
3811 pi->path_len - 3, p,
3812 FNM_PATHNAME | FNM_LEADING_DIR)) {
3813 /* Retry in next directory. */
3814 while (*p && *p != '/')
3826 * The ignores pathlist contains ignore lists from children before
3827 * parents, so we can find the most specific ignorelist by walking
3828 * ignores backwards.
3830 pe = TAILQ_LAST(ignores, got_pathlist_head);
3832 if (got_path_is_child(path, pe->path, pe->path_len)) {
3833 struct got_pathlist_head *ignorelist = pe->data;
3834 struct got_pathlist_entry *pi;
3835 TAILQ_FOREACH(pi, ignorelist, entry) {
3836 int flags = FNM_LEADING_DIR;
3837 if (strstr(pi->path, "/**/") == NULL)
3838 flags |= FNM_PATHNAME;
3839 if (match_path(pi->path, pi->path_len,
3845 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3851 static const struct got_error *
3852 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3853 const char *path, int dirfd, const char *ignores_filename)
3855 const struct got_error *err = NULL;
3858 FILE *ignoresfile = NULL;
3860 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3861 path[0] ? "/" : "", ignores_filename) == -1)
3862 return got_error_from_errno("asprintf");
3865 fd = openat(dirfd, ignores_filename,
3866 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3868 if (errno != ENOENT && errno != EACCES)
3869 err = got_error_from_errno2("openat",
3872 ignoresfile = fdopen(fd, "r");
3873 if (ignoresfile == NULL)
3874 err = got_error_from_errno2("fdopen",
3878 err = read_ignores(ignores, path, ignoresfile);
3882 ignoresfile = fopen(ignorespath, "re");
3883 if (ignoresfile == NULL) {
3884 if (errno != ENOENT && errno != EACCES)
3885 err = got_error_from_errno2("fopen",
3888 err = read_ignores(ignores, path, ignoresfile);
3891 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3892 err = got_error_from_errno2("fclose", path);
3893 if (fd != -1 && close(fd) == -1 && err == NULL)
3894 err = got_error_from_errno2("close", path);
3899 static const struct got_error *
3900 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3903 const struct got_error *err = NULL;
3904 struct diff_dir_cb_arg *a = arg;
3911 err = a->cancel_cb(a->cancel_arg);
3916 if (parent_path[0]) {
3917 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3918 return got_error_from_errno("asprintf");
3923 if (de->d_type == DT_DIR) {
3924 if (!a->no_ignores && ignore != NULL &&
3925 match_ignores(a->ignores, path))
3927 } else if (!match_ignores(a->ignores, path) &&
3928 got_path_is_child(path, a->status_path, a->status_path_len))
3929 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3930 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3936 static const struct got_error *
3937 status_traverse(void *arg, const char *path, int dirfd)
3939 const struct got_error *err = NULL;
3940 struct diff_dir_cb_arg *a = arg;
3945 err = add_ignores(a->ignores, a->worktree->root_path,
3946 path, dirfd, ".cvsignore");
3950 err = add_ignores(a->ignores, a->worktree->root_path, path,
3951 dirfd, ".gitignore");
3956 static const struct got_error *
3957 report_single_file_status(const char *path, const char *ondisk_path,
3958 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3959 void *status_arg, struct got_repository *repo, int report_unchanged,
3960 struct got_pathlist_head *ignores, int no_ignores)
3962 struct got_fileindex_entry *ie;
3965 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3967 return report_file_status(ie, ondisk_path, -1, NULL,
3968 status_cb, status_arg, repo, report_unchanged);
3970 if (lstat(ondisk_path, &sb) == -1) {
3971 if (errno != ENOENT)
3972 return got_error_from_errno2("lstat", ondisk_path);
3973 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3974 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3977 if (!no_ignores && match_ignores(ignores, path))
3980 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3981 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3982 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3987 static const struct got_error *
3988 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3989 const char *root_path, const char *path)
3991 const struct got_error *err;
3992 char *parent_path, *next_parent_path = NULL;
3994 err = add_ignores(ignores, root_path, "", -1,
3999 err = add_ignores(ignores, root_path, "", -1,
4004 err = got_path_dirname(&parent_path, path);
4006 if (err->code == GOT_ERR_BAD_PATH)
4007 return NULL; /* cannot traverse parent */
4011 err = add_ignores(ignores, root_path, parent_path, -1,
4015 err = add_ignores(ignores, root_path, parent_path, -1,
4019 err = got_path_dirname(&next_parent_path, parent_path);
4021 if (err->code == GOT_ERR_BAD_PATH)
4022 err = NULL; /* traversed everything */
4025 if (got_path_is_root_dir(parent_path))
4028 parent_path = next_parent_path;
4029 next_parent_path = NULL;
4033 free(next_parent_path);
4037 struct find_missing_children_args {
4038 const char *parent_path;
4040 struct got_pathlist_head *children;
4041 got_cancel_cb cancel_cb;
4045 static const struct got_error *
4046 find_missing_children(void *arg, struct got_fileindex_entry *ie)
4048 const struct got_error *err = NULL;
4049 struct find_missing_children_args *a = arg;
4052 err = a->cancel_cb(a->cancel_arg);
4057 if (got_path_is_child(ie->path, a->parent_path, a->parent_len))
4058 err = got_pathlist_append(a->children, ie->path, NULL);
4063 static const struct got_error *
4064 report_children(struct got_pathlist_head *children,
4065 struct got_worktree *worktree, struct got_fileindex *fileindex,
4066 struct got_repository *repo, int is_root_dir, int report_unchanged,
4067 struct got_pathlist_head *ignores, int no_ignores,
4068 got_worktree_status_cb status_cb, void *status_arg,
4069 got_cancel_cb cancel_cb, void *cancel_arg)
4071 const struct got_error *err = NULL;
4072 struct got_pathlist_entry *pe;
4073 char *ondisk_path = NULL;
4075 TAILQ_FOREACH(pe, children, entry) {
4077 err = cancel_cb(cancel_arg);
4082 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
4083 !is_root_dir ? "/" : "", pe->path) == -1) {
4084 err = got_error_from_errno("asprintf");
4089 err = report_single_file_status(pe->path, ondisk_path,
4090 fileindex, status_cb, status_arg, repo, report_unchanged,
4091 ignores, no_ignores);
4103 static const struct got_error *
4104 worktree_status(struct got_worktree *worktree, const char *path,
4105 struct got_fileindex *fileindex, struct got_repository *repo,
4106 got_worktree_status_cb status_cb, void *status_arg,
4107 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
4108 int report_unchanged)
4110 const struct got_error *err = NULL;
4112 struct got_fileindex_diff_dir_cb fdiff_cb;
4113 struct diff_dir_cb_arg arg;
4114 char *ondisk_path = NULL;
4115 struct got_pathlist_head ignores, missing_children;
4116 struct got_fileindex_entry *ie;
4118 TAILQ_INIT(&ignores);
4119 TAILQ_INIT(&missing_children);
4121 if (asprintf(&ondisk_path, "%s%s%s",
4122 worktree->root_path, path[0] ? "/" : "", path) == -1)
4123 return got_error_from_errno("asprintf");
4125 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
4127 err = report_single_file_status(path, ondisk_path,
4128 fileindex, status_cb, status_arg, repo,
4129 report_unchanged, &ignores, no_ignores);
4132 struct find_missing_children_args fmca;
4133 fmca.parent_path = path;
4134 fmca.parent_len = strlen(path);
4135 fmca.children = &missing_children;
4136 fmca.cancel_cb = cancel_cb;
4137 fmca.cancel_arg = cancel_arg;
4138 err = got_fileindex_for_each_entry_safe(fileindex,
4139 find_missing_children, &fmca);
4144 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
4146 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
4147 !got_err_open_nofollow_on_symlink())
4148 err = got_error_from_errno2("open", ondisk_path);
4151 err = add_ignores_from_parent_paths(&ignores,
4152 worktree->root_path, ondisk_path);
4156 if (TAILQ_EMPTY(&missing_children)) {
4157 err = report_single_file_status(path,
4158 ondisk_path, fileindex,
4159 status_cb, status_arg, repo,
4160 report_unchanged, &ignores, no_ignores);
4164 err = report_children(&missing_children,
4165 worktree, fileindex, repo,
4166 (path[0] == '\0'), report_unchanged,
4167 &ignores, no_ignores,
4168 status_cb, status_arg,
4169 cancel_cb, cancel_arg);
4175 fdiff_cb.diff_old_new = status_old_new;
4176 fdiff_cb.diff_old = status_old;
4177 fdiff_cb.diff_new = status_new;
4178 fdiff_cb.diff_traverse = status_traverse;
4179 arg.fileindex = fileindex;
4180 arg.worktree = worktree;
4181 arg.status_path = path;
4182 arg.status_path_len = strlen(path);
4184 arg.status_cb = status_cb;
4185 arg.status_arg = status_arg;
4186 arg.cancel_cb = cancel_cb;
4187 arg.cancel_arg = cancel_arg;
4188 arg.report_unchanged = report_unchanged;
4189 arg.no_ignores = no_ignores;
4191 err = add_ignores_from_parent_paths(&ignores,
4192 worktree->root_path, path);
4196 arg.ignores = &ignores;
4197 err = got_fileindex_diff_dir(fileindex, fd,
4198 worktree->root_path, path, repo, &fdiff_cb, &arg);
4201 free_ignores(&ignores);
4202 if (fd != -1 && close(fd) == -1 && err == NULL)
4203 err = got_error_from_errno("close");
4208 const struct got_error *
4209 got_worktree_status(struct got_worktree *worktree,
4210 struct got_pathlist_head *paths, struct got_repository *repo,
4211 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
4212 got_cancel_cb cancel_cb, void *cancel_arg)
4214 const struct got_error *err = NULL;
4215 char *fileindex_path = NULL;
4216 struct got_fileindex *fileindex = NULL;
4217 struct got_pathlist_entry *pe;
4219 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4223 TAILQ_FOREACH(pe, paths, entry) {
4224 err = worktree_status(worktree, pe->path, fileindex, repo,
4225 status_cb, status_arg, cancel_cb, cancel_arg,
4230 free(fileindex_path);
4231 got_fileindex_free(fileindex);
4235 const struct got_error *
4236 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
4239 const struct got_error *err = NULL;
4240 char *resolved = NULL, *cwd = NULL, *path = NULL;
4243 char *abspath = NULL;
4244 char canonpath[PATH_MAX];
4248 cwd = getcwd(NULL, 0);
4250 return got_error_from_errno("getcwd");
4252 if (lstat(arg, &sb) == -1) {
4253 if (errno != ENOENT) {
4254 err = got_error_from_errno2("lstat", arg);
4259 if (S_ISLNK(sb.st_mode)) {
4261 * We cannot use realpath(3) with symlinks since we want to
4262 * operate on the symlink itself.
4263 * But we can make the path absolute, assuming it is relative
4264 * to the current working directory, and then canonicalize it.
4266 if (!got_path_is_absolute(arg)) {
4267 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4268 err = got_error_from_errno("asprintf");
4273 err = got_canonpath(abspath ? abspath : arg, canonpath,
4277 resolved = strdup(canonpath);
4278 if (resolved == NULL) {
4279 err = got_error_from_errno("strdup");
4283 resolved = realpath(arg, NULL);
4284 if (resolved == NULL) {
4285 if (errno != ENOENT) {
4286 err = got_error_from_errno2("realpath", arg);
4289 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4290 err = got_error_from_errno("asprintf");
4293 err = got_canonpath(abspath, canonpath,
4297 resolved = strdup(canonpath);
4298 if (resolved == NULL) {
4299 err = got_error_from_errno("strdup");
4305 if (strncmp(got_worktree_get_root_path(worktree), resolved,
4306 strlen(got_worktree_get_root_path(worktree)))) {
4307 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
4311 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
4312 err = got_path_skip_common_ancestor(&path,
4313 got_worktree_get_root_path(worktree), resolved);
4319 err = got_error_from_errno("strdup");
4324 /* XXX status walk can't deal with trailing slash! */
4326 while (len > 0 && path[len - 1] == '/') {
4327 path[len - 1] = '\0';
4341 struct schedule_addition_args {
4342 struct got_worktree *worktree;
4343 struct got_fileindex *fileindex;
4344 got_worktree_checkout_cb progress_cb;
4346 struct got_repository *repo;
4350 add_noop_status(unsigned char status)
4352 return (status == GOT_STATUS_ADD ||
4353 status == GOT_STATUS_MODIFY ||
4354 status == GOT_STATUS_CONFLICT ||
4355 status == GOT_STATUS_MODE_CHANGE ||
4356 status == GOT_STATUS_NO_CHANGE);
4359 static const struct got_error *
4360 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4361 const char *relpath, struct got_object_id *blob_id,
4362 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4363 int dirfd, const char *de_name)
4365 struct schedule_addition_args *a = arg;
4366 const struct got_error *err = NULL;
4367 struct got_fileindex_entry *ie;
4371 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4373 return got_error_from_errno("asprintf");
4375 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4377 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4381 /* Re-adding an existing entry is a no-op. */
4382 if (staged_status == GOT_STATUS_NO_CHANGE &&
4383 add_noop_status(status))
4385 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4390 if (status != GOT_STATUS_UNVERSIONED) {
4391 if (status == GOT_STATUS_NONEXISTENT)
4392 err = got_error_set_errno(ENOENT, ondisk_path);
4394 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4398 err = got_fileindex_entry_alloc(&ie, relpath);
4401 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4402 relpath, NULL, NULL, 1);
4404 got_fileindex_entry_free(ie);
4407 err = got_fileindex_entry_add(a->fileindex, ie);
4409 got_fileindex_entry_free(ie);
4416 if (staged_status == GOT_STATUS_NO_CHANGE && add_noop_status(status))
4418 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4421 const struct got_error *
4422 got_worktree_schedule_add(struct got_worktree *worktree,
4423 struct got_pathlist_head *paths,
4424 got_worktree_checkout_cb progress_cb, void *progress_arg,
4425 struct got_repository *repo, int no_ignores)
4427 struct got_fileindex *fileindex = NULL;
4428 char *fileindex_path = NULL;
4429 const struct got_error *err = NULL, *sync_err, *unlockerr;
4430 struct got_pathlist_entry *pe;
4431 struct schedule_addition_args saa;
4433 err = lock_worktree(worktree, LOCK_EX);
4437 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4441 saa.worktree = worktree;
4442 saa.fileindex = fileindex;
4443 saa.progress_cb = progress_cb;
4444 saa.progress_arg = progress_arg;
4447 TAILQ_FOREACH(pe, paths, entry) {
4448 err = worktree_status(worktree, pe->path, fileindex, repo,
4449 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4453 sync_err = sync_fileindex(fileindex, fileindex_path);
4454 if (sync_err && err == NULL)
4457 free(fileindex_path);
4459 got_fileindex_free(fileindex);
4460 unlockerr = lock_worktree(worktree, LOCK_SH);
4461 if (unlockerr && err == NULL)
4466 struct schedule_deletion_args {
4467 struct got_worktree *worktree;
4468 struct got_fileindex *fileindex;
4469 got_worktree_delete_cb progress_cb;
4471 struct got_repository *repo;
4472 int delete_local_mods;
4474 int ignore_missing_paths;
4475 const char *status_path;
4476 size_t status_path_len;
4477 const char *status_codes;
4480 static const struct got_error *
4481 schedule_for_deletion(void *arg, unsigned char status,
4482 unsigned char staged_status, const char *relpath,
4483 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4484 struct got_object_id *commit_id, int dirfd, const char *de_name)
4486 struct schedule_deletion_args *a = arg;
4487 const struct got_error *err = NULL;
4488 struct got_fileindex_entry *ie = NULL;
4492 if (status == GOT_STATUS_NONEXISTENT) {
4493 if (a->ignore_missing_paths)
4495 return got_error_set_errno(ENOENT, relpath);
4498 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4500 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4502 staged_status = get_staged_status(ie);
4503 if (staged_status != GOT_STATUS_NO_CHANGE) {
4504 if (staged_status == GOT_STATUS_DELETE)
4506 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4509 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4511 return got_error_from_errno("asprintf");
4513 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4518 if (a->status_codes) {
4519 size_t ncodes = strlen(a->status_codes);
4521 for (i = 0; i < ncodes ; i++) {
4522 if (status == a->status_codes[i])
4526 /* Do not delete files in non-matching status. */
4530 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4531 a->status_codes[i] != GOT_STATUS_MISSING) {
4532 static char msg[64];
4533 snprintf(msg, sizeof(msg),
4534 "invalid status code '%c'", a->status_codes[i]);
4535 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4540 if (status != GOT_STATUS_NO_CHANGE) {
4541 if (status == GOT_STATUS_DELETE)
4543 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4544 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4547 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4548 err = got_error_set_errno(ENOENT, relpath);
4551 if (status != GOT_STATUS_MODIFY &&
4552 status != GOT_STATUS_MISSING) {
4553 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4558 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4562 if (unlinkat(dirfd, de_name, 0) == -1) {
4563 err = got_error_from_errno2("unlinkat",
4567 } else if (unlink(ondisk_path) == -1) {
4568 err = got_error_from_errno2("unlink", ondisk_path);
4572 root_len = strlen(a->worktree->root_path);
4576 err = got_path_dirname(&parent, ondisk_path);
4580 ondisk_path = parent;
4581 if (got_path_cmp(ondisk_path, a->status_path,
4582 strlen(ondisk_path), a->status_path_len) != 0 &&
4583 !got_path_is_child(ondisk_path, a->status_path,
4584 a->status_path_len))
4586 if (rmdir(ondisk_path) == -1) {
4587 if (errno != ENOTEMPTY)
4588 err = got_error_from_errno2("rmdir",
4592 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4593 strlen(ondisk_path), root_len) != 0);
4596 if (got_fileindex_entry_has_blob(ie))
4597 got_fileindex_entry_mark_deleted_from_disk(ie);
4599 got_fileindex_entry_remove(a->fileindex, ie);
4604 if (status == GOT_STATUS_DELETE)
4606 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4607 staged_status, relpath);
4610 const struct got_error *
4611 got_worktree_schedule_delete(struct got_worktree *worktree,
4612 struct got_pathlist_head *paths, int delete_local_mods,
4613 const char *status_codes,
4614 got_worktree_delete_cb progress_cb, void *progress_arg,
4615 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4617 struct got_fileindex *fileindex = NULL;
4618 char *fileindex_path = NULL;
4619 const struct got_error *err = NULL, *sync_err, *unlockerr;
4620 struct got_pathlist_entry *pe;
4621 struct schedule_deletion_args sda;
4623 err = lock_worktree(worktree, LOCK_EX);
4627 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4631 sda.worktree = worktree;
4632 sda.fileindex = fileindex;
4633 sda.progress_cb = progress_cb;
4634 sda.progress_arg = progress_arg;
4636 sda.delete_local_mods = delete_local_mods;
4637 sda.keep_on_disk = keep_on_disk;
4638 sda.ignore_missing_paths = ignore_missing_paths;
4639 sda.status_codes = status_codes;
4641 TAILQ_FOREACH(pe, paths, entry) {
4642 char *ondisk_status_path;
4644 if (asprintf(&ondisk_status_path, "%s%s%s",
4645 got_worktree_get_root_path(worktree),
4646 pe->path[0] == '\0' ? "" : "/", pe->path) == -1) {
4647 err = got_error_from_errno("asprintf");
4650 sda.status_path = ondisk_status_path;
4651 sda.status_path_len = strlen(ondisk_status_path);
4652 err = worktree_status(worktree, pe->path, fileindex, repo,
4653 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4654 free(ondisk_status_path);
4658 sync_err = sync_fileindex(fileindex, fileindex_path);
4659 if (sync_err && err == NULL)
4662 free(fileindex_path);
4664 got_fileindex_free(fileindex);
4665 unlockerr = lock_worktree(worktree, LOCK_SH);
4666 if (unlockerr && err == NULL)
4671 static const struct got_error *
4672 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4674 const struct got_error *err = NULL;
4676 size_t linesize = 0, n;
4679 linelen = getline(&line, &linesize, infile);
4680 if (linelen == -1) {
4681 if (ferror(infile)) {
4682 err = got_error_from_errno("getline");
4688 n = fwrite(line, 1, linelen, outfile);
4690 err = got_ferror(outfile, GOT_ERR_IO);
4695 n = fwrite(line, 1, linelen, rejectfile);
4697 err = got_ferror(rejectfile, GOT_ERR_IO);
4704 static const struct got_error *
4705 skip_one_line(FILE *f)
4708 size_t linesize = 0;
4711 linelen = getline(&line, &linesize, f);
4712 if (linelen == -1) {
4714 return got_error_from_errno("getline");
4721 static const struct got_error *
4722 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4723 int start_old, int end_old, int start_new, int end_new,
4724 FILE *outfile, FILE *rejectfile)
4726 const struct got_error *err;
4728 /* Copy old file's lines leading up to patch. */
4729 while (!feof(f1) && *line_cur1 < start_old) {
4730 err = copy_one_line(f1, outfile, NULL);
4735 /* Skip new file's lines leading up to patch. */
4736 while (!feof(f2) && *line_cur2 < start_new) {
4738 err = copy_one_line(f2, NULL, rejectfile);
4740 err = skip_one_line(f2);
4745 /* Copy patched lines. */
4746 while (!feof(f2) && *line_cur2 <= end_new) {
4747 err = copy_one_line(f2, outfile, NULL);
4752 /* Skip over old file's replaced lines. */
4753 while (!feof(f1) && *line_cur1 <= end_old) {
4755 err = copy_one_line(f1, NULL, rejectfile);
4757 err = skip_one_line(f1);
4766 static const struct got_error *
4767 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4768 FILE *outfile, FILE *rejectfile)
4770 const struct got_error *err;
4773 /* Copy old file's lines until EOF. */
4775 err = copy_one_line(f1, outfile, NULL);
4782 /* Copy new file's lines until EOF. */
4784 err = copy_one_line(f2, NULL, rejectfile);
4794 static const struct got_error *
4795 apply_or_reject_change(int *choice, int *nchunks_used,
4796 struct diff_result *diff_result, int n,
4797 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4798 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4799 got_worktree_patch_cb patch_cb, void *patch_arg)
4801 const struct got_error *err = NULL;
4802 struct diff_chunk_context cc = {};
4803 int start_old, end_old, start_new, end_new;
4805 struct diff_output_unidiff_state *diff_state;
4806 struct diff_input_info diff_info;
4809 *choice = GOT_PATCH_CHOICE_NONE;
4811 /* Get changed line numbers without context lines for copy_change(). */
4812 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4813 start_old = cc.left.start;
4814 end_old = cc.left.end;
4815 start_new = cc.right.start;
4816 end_new = cc.right.end;
4818 /* Get the same change with context lines for display. */
4819 memset(&cc, 0, sizeof(cc));
4820 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4822 memset(&diff_info, 0, sizeof(diff_info));
4823 diff_info.left_path = relpath;
4824 diff_info.right_path = relpath;
4826 diff_state = diff_output_unidiff_state_alloc();
4827 if (diff_state == NULL)
4828 return got_error_set_errno(ENOMEM,
4829 "diff_output_unidiff_state_alloc");
4831 hunkfile = got_opentemp();
4832 if (hunkfile == NULL) {
4833 err = got_error_from_errno("got_opentemp");
4837 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4839 if (rc != DIFF_RC_OK) {
4840 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4844 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4845 err = got_ferror(hunkfile, GOT_ERR_IO);
4849 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4850 hunkfile, changeno, nchanges);
4855 case GOT_PATCH_CHOICE_YES:
4856 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4857 end_old, start_new, end_new, outfile, rejectfile);
4859 case GOT_PATCH_CHOICE_NO:
4860 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4861 end_old, start_new, end_new, rejectfile, outfile);
4863 case GOT_PATCH_CHOICE_QUIT:
4866 err = got_error(GOT_ERR_PATCH_CHOICE);
4870 diff_output_unidiff_state_free(diff_state);
4871 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4872 err = got_error_from_errno("fclose");
4876 struct revert_file_args {
4877 struct got_worktree *worktree;
4878 struct got_fileindex *fileindex;
4879 got_worktree_checkout_cb progress_cb;
4881 got_worktree_patch_cb patch_cb;
4883 struct got_repository *repo;
4884 int unlink_added_files;
4885 struct got_pathlist_head *added_files_to_unlink;
4888 static const struct got_error *
4889 create_patched_content(char **path_outfile, int reverse_patch,
4890 struct got_object_id *blob_id, const char *path2,
4891 int dirfd2, const char *de_name2,
4892 const char *relpath, struct got_repository *repo,
4893 got_worktree_patch_cb patch_cb, void *patch_arg)
4895 const struct got_error *err, *free_err;
4896 struct got_blob_object *blob = NULL;
4897 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4898 int fd = -1, fd2 = -1;
4899 char link_target[PATH_MAX];
4900 ssize_t link_len = 0;
4901 char *path1 = NULL, *id_str = NULL;
4903 struct got_diffreg_result *diffreg_result = NULL;
4904 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4905 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4907 *path_outfile = NULL;
4909 err = got_object_id_str(&id_str, blob_id);
4914 fd2 = openat(dirfd2, de_name2,
4915 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4917 if (!got_err_open_nofollow_on_symlink()) {
4918 err = got_error_from_errno2("openat", path2);
4921 link_len = readlinkat(dirfd2, de_name2,
4922 link_target, sizeof(link_target));
4923 if (link_len == -1) {
4924 return got_error_from_errno2("readlinkat",
4927 sb2.st_mode = S_IFLNK;
4928 sb2.st_size = link_len;
4931 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4933 if (!got_err_open_nofollow_on_symlink()) {
4934 err = got_error_from_errno2("open", path2);
4937 link_len = readlink(path2, link_target,
4938 sizeof(link_target));
4940 return got_error_from_errno2("readlink", path2);
4941 sb2.st_mode = S_IFLNK;
4942 sb2.st_size = link_len;
4946 if (fstat(fd2, &sb2) == -1) {
4947 err = got_error_from_errno2("fstat", path2);
4951 f2 = fdopen(fd2, "r");
4953 err = got_error_from_errno2("fdopen", path2);
4959 f2 = got_opentemp();
4961 err = got_error_from_errno2("got_opentemp", path2);
4964 n = fwrite(link_target, 1, link_len, f2);
4965 if (n != link_len) {
4966 err = got_ferror(f2, GOT_ERR_IO);
4969 if (fflush(f2) == EOF) {
4970 err = got_error_from_errno("fflush");
4976 fd = got_opentempfd();
4978 err = got_error_from_errno("got_opentempfd");
4982 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4986 err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4990 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4994 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4995 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4999 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
5004 if (fseek(f1, 0L, SEEK_SET) == -1)
5005 return got_ferror(f1, GOT_ERR_IO);
5006 if (fseek(f2, 0L, SEEK_SET) == -1)
5007 return got_ferror(f2, GOT_ERR_IO);
5009 /* Count the number of actual changes in the diff result. */
5010 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
5011 struct diff_chunk_context cc = {};
5012 diff_chunk_context_load_change(&cc, &nchunks_used,
5013 diffreg_result->result, n, 0);
5016 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
5018 err = apply_or_reject_change(&choice, &nchunks_used,
5019 diffreg_result->result, n, relpath, f1, f2,
5020 &line_cur1, &line_cur2,
5021 reverse_patch ? NULL : outfile,
5022 reverse_patch ? outfile : NULL,
5023 ++i, nchanges, patch_cb, patch_arg);
5026 if (choice == GOT_PATCH_CHOICE_YES)
5028 else if (choice == GOT_PATCH_CHOICE_QUIT)
5032 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
5033 reverse_patch ? NULL : outfile,
5034 reverse_patch ? outfile : NULL);
5038 if (!S_ISLNK(sb2.st_mode)) {
5041 mode = apply_umask(sb2.st_mode);
5042 if (fchmod(fileno(outfile), mode) == -1) {
5043 err = got_error_from_errno2("fchmod", path2);
5050 if (fd != -1 && close(fd) == -1 && err == NULL)
5051 err = got_error_from_errno("close");
5053 got_object_blob_close(blob);
5054 free_err = got_diffreg_result_free(diffreg_result);
5057 if (f1 && fclose(f1) == EOF && err == NULL)
5058 err = got_error_from_errno2("fclose", path1);
5059 if (f2 && fclose(f2) == EOF && err == NULL)
5060 err = got_error_from_errno2("fclose", path2);
5061 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5062 err = got_error_from_errno2("close", path2);
5063 if (outfile && fclose(outfile) == EOF && err == NULL)
5064 err = got_error_from_errno2("fclose", *path_outfile);
5065 if (path1 && unlink(path1) == -1 && err == NULL)
5066 err = got_error_from_errno2("unlink", path1);
5067 if (err || !have_content) {
5068 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
5069 err = got_error_from_errno2("unlink", *path_outfile);
5070 free(*path_outfile);
5071 *path_outfile = NULL;
5077 static const struct got_error *
5078 revert_file(void *arg, unsigned char status, unsigned char staged_status,
5079 const char *relpath, struct got_object_id *blob_id,
5080 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5081 int dirfd, const char *de_name)
5083 struct revert_file_args *a = arg;
5084 const struct got_error *err = NULL;
5085 char *parent_path = NULL;
5086 struct got_fileindex_entry *ie;
5087 struct got_commit_object *base_commit = NULL;
5088 struct got_tree_object *tree = NULL;
5089 struct got_object_id *tree_id = NULL;
5090 const struct got_tree_entry *te = NULL;
5091 char *tree_path = NULL, *te_name;
5092 char *ondisk_path = NULL, *path_content = NULL;
5093 struct got_blob_object *blob = NULL;
5096 /* Reverting a staged deletion is a no-op. */
5097 if (status == GOT_STATUS_DELETE &&
5098 staged_status != GOT_STATUS_NO_CHANGE)
5101 if (status == GOT_STATUS_UNVERSIONED)
5102 return (*a->progress_cb)(a->progress_arg,
5103 GOT_STATUS_UNVERSIONED, relpath);
5105 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5107 return got_error_path(relpath, GOT_ERR_BAD_PATH);
5109 /* Construct in-repository path of tree which contains this blob. */
5110 err = got_path_dirname(&parent_path, ie->path);
5112 if (err->code != GOT_ERR_BAD_PATH)
5114 parent_path = strdup("/");
5115 if (parent_path == NULL) {
5116 err = got_error_from_errno("strdup");
5120 if (got_path_is_root_dir(a->worktree->path_prefix)) {
5121 tree_path = strdup(parent_path);
5122 if (tree_path == NULL) {
5123 err = got_error_from_errno("strdup");
5127 if (got_path_is_root_dir(parent_path)) {
5128 tree_path = strdup(a->worktree->path_prefix);
5129 if (tree_path == NULL) {
5130 err = got_error_from_errno("strdup");
5134 if (asprintf(&tree_path, "%s/%s",
5135 a->worktree->path_prefix, parent_path) == -1) {
5136 err = got_error_from_errno("asprintf");
5142 err = got_object_open_as_commit(&base_commit, a->repo,
5143 a->worktree->base_commit_id);
5147 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
5149 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
5150 (status == GOT_STATUS_ADD ||
5151 staged_status == GOT_STATUS_ADD)))
5154 err = got_object_open_as_tree(&tree, a->repo, tree_id);
5158 err = got_path_basename(&te_name, ie->path);
5162 te = got_object_tree_find_entry(tree, te_name);
5164 if (te == NULL && status != GOT_STATUS_ADD &&
5165 staged_status != GOT_STATUS_ADD) {
5166 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
5172 case GOT_STATUS_ADD:
5174 int choice = GOT_PATCH_CHOICE_NONE;
5175 err = (*a->patch_cb)(&choice, a->patch_arg,
5176 status, ie->path, NULL, 1, 1);
5179 if (choice != GOT_PATCH_CHOICE_YES)
5182 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
5186 got_fileindex_entry_remove(a->fileindex, ie);
5187 if (a->unlink_added_files) {
5188 int do_unlink = a->added_files_to_unlink ? 0 : 1;
5190 if (a->added_files_to_unlink) {
5191 struct got_pathlist_entry *pe;
5193 TAILQ_FOREACH(pe, a->added_files_to_unlink,
5195 if (got_path_cmp(pe->path, relpath,
5196 pe->path_len, strlen(relpath)))
5204 if (asprintf(&ondisk_path, "%s/%s",
5205 got_worktree_get_root_path(a->worktree),
5207 err = got_error_from_errno("asprintf");
5210 if (unlink(ondisk_path) == -1) {
5211 err = got_error_from_errno2("unlink",
5218 case GOT_STATUS_DELETE:
5220 int choice = GOT_PATCH_CHOICE_NONE;
5221 err = (*a->patch_cb)(&choice, a->patch_arg,
5222 status, ie->path, NULL, 1, 1);
5225 if (choice != GOT_PATCH_CHOICE_YES)
5229 case GOT_STATUS_MODIFY:
5230 case GOT_STATUS_MODE_CHANGE:
5231 case GOT_STATUS_CONFLICT:
5232 case GOT_STATUS_MISSING: {
5233 struct got_object_id id;
5234 if (staged_status == GOT_STATUS_ADD ||
5235 staged_status == GOT_STATUS_MODIFY)
5236 got_fileindex_entry_get_staged_blob_id(&id, ie);
5238 got_fileindex_entry_get_blob_id(&id, ie);
5239 fd = got_opentempfd();
5241 err = got_error_from_errno("got_opentempfd");
5245 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
5249 if (asprintf(&ondisk_path, "%s/%s",
5250 got_worktree_get_root_path(a->worktree), relpath) == -1) {
5251 err = got_error_from_errno("asprintf");
5255 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
5256 status == GOT_STATUS_CONFLICT)) {
5257 int is_bad_symlink = 0;
5258 err = create_patched_content(&path_content, 1, &id,
5259 ondisk_path, dirfd, de_name, ie->path, a->repo,
5260 a->patch_cb, a->patch_arg);
5261 if (err || path_content == NULL)
5263 if (te && S_ISLNK(te->mode)) {
5264 if (unlink(path_content) == -1) {
5265 err = got_error_from_errno2("unlink",
5269 err = install_symlink(&is_bad_symlink,
5270 a->worktree, ondisk_path, ie->path,
5271 blob, 0, 1, 0, 0, a->repo,
5272 a->progress_cb, a->progress_arg);
5274 if (rename(path_content, ondisk_path) == -1) {
5275 err = got_error_from_errno3("rename",
5276 path_content, ondisk_path);
5281 int is_bad_symlink = 0;
5282 if (te && S_ISLNK(te->mode)) {
5283 err = install_symlink(&is_bad_symlink,
5284 a->worktree, ondisk_path, ie->path,
5285 blob, 0, 1, 0, 0, a->repo,
5286 a->progress_cb, a->progress_arg);
5288 err = install_blob(a->worktree, ondisk_path,
5290 te ? te->mode : GOT_DEFAULT_FILE_MODE,
5291 got_fileindex_perms_to_st(ie), blob,
5292 0, 1, 0, 0, a->repo,
5293 a->progress_cb, a->progress_arg);
5297 if (status == GOT_STATUS_DELETE ||
5298 status == GOT_STATUS_MODE_CHANGE) {
5299 err = got_fileindex_entry_update(ie,
5300 a->worktree->root_fd, relpath,
5302 a->worktree->base_commit_id->sha1, 1);
5306 if (is_bad_symlink) {
5307 got_fileindex_entry_filetype_set(ie,
5308 GOT_FILEIDX_MODE_BAD_SYMLINK);
5321 if (fd != -1 && close(fd) == -1 && err == NULL)
5322 err = got_error_from_errno("close");
5324 got_object_blob_close(blob);
5326 got_object_tree_close(tree);
5329 got_object_commit_close(base_commit);
5333 const struct got_error *
5334 got_worktree_revert(struct got_worktree *worktree,
5335 struct got_pathlist_head *paths,
5336 got_worktree_checkout_cb progress_cb, void *progress_arg,
5337 got_worktree_patch_cb patch_cb, void *patch_arg,
5338 struct got_repository *repo)
5340 struct got_fileindex *fileindex = NULL;
5341 char *fileindex_path = NULL;
5342 const struct got_error *err = NULL, *unlockerr = NULL;
5343 const struct got_error *sync_err = NULL;
5344 struct got_pathlist_entry *pe;
5345 struct revert_file_args rfa;
5347 err = lock_worktree(worktree, LOCK_EX);
5351 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5355 rfa.worktree = worktree;
5356 rfa.fileindex = fileindex;
5357 rfa.progress_cb = progress_cb;
5358 rfa.progress_arg = progress_arg;
5359 rfa.patch_cb = patch_cb;
5360 rfa.patch_arg = patch_arg;
5362 rfa.unlink_added_files = 0;
5363 TAILQ_FOREACH(pe, paths, entry) {
5364 err = worktree_status(worktree, pe->path, fileindex, repo,
5365 revert_file, &rfa, NULL, NULL, 1, 0);
5369 sync_err = sync_fileindex(fileindex, fileindex_path);
5370 if (sync_err && err == NULL)
5373 free(fileindex_path);
5375 got_fileindex_free(fileindex);
5376 unlockerr = lock_worktree(worktree, LOCK_SH);
5377 if (unlockerr && err == NULL)
5383 free_commitable(struct got_commitable *ct)
5386 free(ct->in_repo_path);
5387 free(ct->ondisk_path);
5389 free(ct->base_blob_id);
5390 free(ct->staged_blob_id);
5391 free(ct->base_commit_id);
5395 struct collect_commitables_arg {
5396 struct got_pathlist_head *commitable_paths;
5397 struct got_repository *repo;
5398 struct got_worktree *worktree;
5399 struct got_fileindex *fileindex;
5400 int have_staged_files;
5401 int allow_bad_symlinks;
5402 int diff_header_shown;
5403 int commit_conflicts;
5410 * Create a file which contains the target path of a symlink so we can feed
5411 * it as content to the diff engine.
5413 static const struct got_error *
5414 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5415 const char *abspath)
5417 const struct got_error *err = NULL;
5418 char target_path[PATH_MAX];
5419 ssize_t target_len, outlen;
5424 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5425 if (target_len == -1)
5426 return got_error_from_errno2("readlinkat", abspath);
5428 target_len = readlink(abspath, target_path, PATH_MAX);
5429 if (target_len == -1)
5430 return got_error_from_errno2("readlink", abspath);
5433 *fd = got_opentempfd();
5435 return got_error_from_errno("got_opentempfd");
5437 outlen = write(*fd, target_path, target_len);
5439 err = got_error_from_errno("got_opentempfd");
5443 if (lseek(*fd, 0, SEEK_SET) == -1) {
5444 err = got_error_from_errno2("lseek", abspath);
5455 static const struct got_error *
5456 append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5457 FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5458 int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5460 const struct got_error *err = NULL;
5461 struct got_blob_object *blob1 = NULL;
5462 int fd = -1, fd1 = -1, fd2 = -1;
5463 FILE *ondisk_file = NULL;
5464 char *label1 = NULL;
5468 char *id_str = NULL;
5470 memset(&sb, 0, sizeof(sb));
5473 if (ct->staged_status != GOT_STATUS_MODIFY &&
5474 ct->staged_status != GOT_STATUS_ADD &&
5475 ct->staged_status != GOT_STATUS_DELETE)
5478 if (ct->status != GOT_STATUS_MODIFY &&
5479 ct->status != GOT_STATUS_ADD &&
5480 ct->status != GOT_STATUS_DELETE &&
5481 ct->status != GOT_STATUS_CONFLICT)
5485 err = got_opentemp_truncate(f1);
5487 return got_error_from_errno("got_opentemp_truncate");
5488 err = got_opentemp_truncate(f2);
5490 return got_error_from_errno("got_opentemp_truncate");
5492 if (!*diff_header_shown) {
5493 err = got_object_id_str(&id_str, worktree->base_commit_id);
5496 fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5497 got_worktree_get_root_path(worktree));
5498 fprintf(diff_outfile, "commit - %s\n", id_str);
5499 fprintf(diff_outfile, "path + %s%s\n",
5500 got_worktree_get_root_path(worktree),
5501 diff_staged ? " (staged changes)" : "");
5502 *diff_header_shown = 1;
5506 const char *label1 = NULL, *label2 = NULL;
5507 switch (ct->staged_status) {
5508 case GOT_STATUS_MODIFY:
5512 case GOT_STATUS_ADD:
5515 case GOT_STATUS_DELETE:
5519 return got_error(GOT_ERR_FILE_STATUS);
5521 fd1 = got_opentempfd();
5523 err = got_error_from_errno("got_opentempfd");
5526 fd2 = got_opentempfd();
5528 err = got_error_from_errno("got_opentempfd");
5531 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5532 fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5533 label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5534 NULL, repo, diff_outfile);
5538 fd1 = got_opentempfd();
5540 err = got_error_from_errno("got_opentempfd");
5544 if (ct->status != GOT_STATUS_ADD) {
5545 err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5551 if (ct->status != GOT_STATUS_DELETE) {
5553 fd = openat(dirfd, de_name,
5554 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5556 if (!got_err_open_nofollow_on_symlink()) {
5557 err = got_error_from_errno2("openat",
5561 err = get_symlink_target_file(&fd, dirfd,
5562 de_name, ct->ondisk_path);
5567 fd = open(ct->ondisk_path,
5568 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5570 if (!got_err_open_nofollow_on_symlink()) {
5571 err = got_error_from_errno2("open",
5575 err = get_symlink_target_file(&fd, dirfd,
5576 de_name, ct->ondisk_path);
5581 if (fstatat(fd, ct->ondisk_path, &sb,
5582 AT_SYMLINK_NOFOLLOW) == -1) {
5583 err = got_error_from_errno2("fstatat", ct->ondisk_path);
5586 ondisk_file = fdopen(fd, "r");
5587 if (ondisk_file == NULL) {
5588 err = got_error_from_errno2("fdopen", ct->ondisk_path);
5596 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5602 err = got_diff_blob_file(blob1, f1, size1, label1,
5603 ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5604 GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5606 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5607 err = got_error_from_errno("close");
5608 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5609 err = got_error_from_errno("close");
5611 got_object_blob_close(blob1);
5612 if (fd != -1 && close(fd) == -1 && err == NULL)
5613 err = got_error_from_errno("close");
5614 if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5615 err = got_error_from_errno("fclose");
5619 static const struct got_error *
5620 collect_commitables(void *arg, unsigned char status,
5621 unsigned char staged_status, const char *relpath,
5622 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5623 struct got_object_id *commit_id, int dirfd, const char *de_name)
5625 struct collect_commitables_arg *a = arg;
5626 const struct got_error *err = NULL;
5627 struct got_commitable *ct = NULL;
5628 struct got_pathlist_entry *new = NULL;
5629 char *parent_path = NULL, *path = NULL;
5632 if (a->have_staged_files) {
5633 if (staged_status != GOT_STATUS_MODIFY &&
5634 staged_status != GOT_STATUS_ADD &&
5635 staged_status != GOT_STATUS_DELETE)
5638 if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5639 printf("C %s\n", relpath);
5640 return got_error(GOT_ERR_COMMIT_CONFLICT);
5643 if (status != GOT_STATUS_MODIFY &&
5644 status != GOT_STATUS_MODE_CHANGE &&
5645 status != GOT_STATUS_ADD &&
5646 status != GOT_STATUS_DELETE &&
5647 status != GOT_STATUS_CONFLICT)
5651 if (asprintf(&path, "/%s", relpath) == -1) {
5652 err = got_error_from_errno("asprintf");
5655 if (strcmp(path, "/") == 0) {
5656 parent_path = strdup("");
5657 if (parent_path == NULL)
5658 return got_error_from_errno("strdup");
5660 err = got_path_dirname(&parent_path, path);
5665 ct = calloc(1, sizeof(*ct));
5667 err = got_error_from_errno("calloc");
5671 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5673 err = got_error_from_errno("asprintf");
5677 if (staged_status == GOT_STATUS_ADD ||
5678 staged_status == GOT_STATUS_MODIFY) {
5679 struct got_fileindex_entry *ie;
5680 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5681 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5682 case GOT_FILEIDX_MODE_REGULAR_FILE:
5683 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5686 case GOT_FILEIDX_MODE_SYMLINK:
5690 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5693 ct->mode |= got_fileindex_entry_perms_get(ie);
5694 } else if (status != GOT_STATUS_DELETE &&
5695 staged_status != GOT_STATUS_DELETE) {
5697 if (fstatat(dirfd, de_name, &sb,
5698 AT_SYMLINK_NOFOLLOW) == -1) {
5699 err = got_error_from_errno2("fstatat",
5703 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5704 err = got_error_from_errno2("lstat", ct->ondisk_path);
5707 ct->mode = sb.st_mode;
5710 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5711 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5713 err = got_error_from_errno("asprintf");
5717 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5718 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5720 char target_path[PATH_MAX];
5722 target_len = readlink(ct->ondisk_path, target_path,
5723 sizeof(target_path));
5724 if (target_len == -1) {
5725 err = got_error_from_errno2("readlink",
5729 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5730 target_len, ct->ondisk_path, a->worktree->root_path,
5731 a->worktree->meta_dir);
5734 if (is_bad_symlink) {
5735 err = got_error_path(ct->ondisk_path,
5736 GOT_ERR_BAD_SYMLINK);
5742 ct->status = status;
5743 ct->staged_status = staged_status;
5744 ct->blob_id = NULL; /* will be filled in when blob gets created */
5745 if (ct->status != GOT_STATUS_ADD &&
5746 ct->staged_status != GOT_STATUS_ADD) {
5747 ct->base_blob_id = got_object_id_dup(blob_id);
5748 if (ct->base_blob_id == NULL) {
5749 err = got_error_from_errno("got_object_id_dup");
5752 ct->base_commit_id = got_object_id_dup(commit_id);
5753 if (ct->base_commit_id == NULL) {
5754 err = got_error_from_errno("got_object_id_dup");
5758 if (ct->staged_status == GOT_STATUS_ADD ||
5759 ct->staged_status == GOT_STATUS_MODIFY) {
5760 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5761 if (ct->staged_blob_id == NULL) {
5762 err = got_error_from_errno("got_object_id_dup");
5766 ct->path = strdup(path);
5767 if (ct->path == NULL) {
5768 err = got_error_from_errno("strdup");
5772 if (a->diff_outfile) {
5773 err = append_ct_diff(ct, &a->diff_header_shown,
5774 a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5775 a->have_staged_files, a->repo, a->worktree);
5780 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5782 if (ct && (err || new == NULL))
5783 free_commitable(ct);
5789 static const struct got_error *write_tree(struct got_object_id **, int *,
5790 struct got_tree_object *, const char *, struct got_pathlist_head *,
5791 got_worktree_status_cb status_cb, void *status_arg,
5792 struct got_repository *);
5794 static const struct got_error *
5795 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5796 struct got_tree_entry *te, const char *parent_path,
5797 struct got_pathlist_head *commitable_paths,
5798 got_worktree_status_cb status_cb, void *status_arg,
5799 struct got_repository *repo)
5801 const struct got_error *err = NULL;
5802 struct got_tree_object *subtree;
5805 if (asprintf(&subpath, "%s%s%s", parent_path,
5806 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5807 return got_error_from_errno("asprintf");
5809 err = got_object_open_as_tree(&subtree, repo, &te->id);
5813 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5814 commitable_paths, status_cb, status_arg, repo);
5815 got_object_tree_close(subtree);
5820 static const struct got_error *
5821 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5823 const struct got_error *err = NULL;
5824 char *ct_parent_path = NULL;
5828 if (strchr(ct->in_repo_path, '/') == NULL) {
5829 *match = got_path_is_root_dir(path);
5833 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5836 *match = (strcmp(path, ct_parent_path) == 0);
5837 free(ct_parent_path);
5842 get_ct_file_mode(struct got_commitable *ct)
5844 if (S_ISLNK(ct->mode))
5847 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5850 static const struct got_error *
5851 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5852 struct got_tree_entry *te, struct got_commitable *ct)
5854 const struct got_error *err = NULL;
5858 err = got_object_tree_entry_dup(new_te, te);
5862 (*new_te)->mode = get_ct_file_mode(ct);
5864 if (ct->staged_status == GOT_STATUS_MODIFY)
5865 memcpy(&(*new_te)->id, ct->staged_blob_id,
5866 sizeof((*new_te)->id));
5868 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5870 if (err && *new_te) {
5877 static const struct got_error *
5878 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5879 struct got_commitable *ct)
5881 const struct got_error *err = NULL;
5882 char *ct_name = NULL;
5886 *new_te = calloc(1, sizeof(**new_te));
5887 if (*new_te == NULL)
5888 return got_error_from_errno("calloc");
5890 err = got_path_basename(&ct_name, ct->path);
5893 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5894 sizeof((*new_te)->name)) {
5895 err = got_error(GOT_ERR_NO_SPACE);
5899 (*new_te)->mode = get_ct_file_mode(ct);
5901 if (ct->staged_status == GOT_STATUS_ADD)
5902 memcpy(&(*new_te)->id, ct->staged_blob_id,
5903 sizeof((*new_te)->id));
5905 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5908 if (err && *new_te) {
5915 static const struct got_error *
5916 insert_tree_entry(struct got_tree_entry *new_te,
5917 struct got_pathlist_head *paths)
5919 const struct got_error *err = NULL;
5920 struct got_pathlist_entry *new_pe;
5922 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5926 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5930 static const struct got_error *
5931 report_ct_status(struct got_commitable *ct,
5932 got_worktree_status_cb status_cb, void *status_arg)
5934 const char *ct_path = ct->path;
5935 unsigned char status;
5937 if (status_cb == NULL) /* no commit progress output desired */
5940 while (ct_path[0] == '/')
5943 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5944 status = ct->staged_status;
5946 status = ct->status;
5948 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5949 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5952 static const struct got_error *
5953 match_modified_subtree(int *modified, struct got_tree_entry *te,
5954 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5956 const struct got_error *err = NULL;
5957 struct got_pathlist_entry *pe;
5962 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5963 got_path_is_root_dir(base_tree_path) ? "" : "/",
5965 return got_error_from_errno("asprintf");
5967 TAILQ_FOREACH(pe, commitable_paths, entry) {
5968 struct got_commitable *ct = pe->data;
5969 *modified = got_path_is_child(ct->in_repo_path, te_path,
5979 static const struct got_error *
5980 match_deleted_or_modified_ct(struct got_commitable **ctp,
5981 struct got_tree_entry *te, const char *base_tree_path,
5982 struct got_pathlist_head *commitable_paths)
5984 const struct got_error *err = NULL;
5985 struct got_pathlist_entry *pe;
5989 TAILQ_FOREACH(pe, commitable_paths, entry) {
5990 struct got_commitable *ct = pe->data;
5991 char *ct_name = NULL;
5994 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5995 if (ct->status != GOT_STATUS_MODIFY &&
5996 ct->status != GOT_STATUS_MODE_CHANGE &&
5997 ct->status != GOT_STATUS_DELETE &&
5998 ct->status != GOT_STATUS_CONFLICT)
6001 if (ct->staged_status != GOT_STATUS_MODIFY &&
6002 ct->staged_status != GOT_STATUS_DELETE)
6006 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
6009 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
6015 err = got_path_basename(&ct_name, pe->path);
6019 if (strcmp(te->name, ct_name) != 0) {
6032 static const struct got_error *
6033 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
6034 const char *child_path, const char *path_base_tree,
6035 struct got_pathlist_head *commitable_paths,
6036 got_worktree_status_cb status_cb, void *status_arg,
6037 struct got_repository *repo)
6039 const struct got_error *err = NULL;
6040 struct got_tree_entry *new_te;
6042 struct got_object_id *id = NULL;
6047 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
6048 got_path_is_root_dir(path_base_tree) ? "" : "/",
6050 return got_error_from_errno("asprintf");
6052 new_te = calloc(1, sizeof(*new_te));
6054 return got_error_from_errno("calloc");
6055 new_te->mode = S_IFDIR;
6057 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
6058 sizeof(new_te->name)) {
6059 err = got_error(GOT_ERR_NO_SPACE);
6062 err = write_tree(&id, &nentries, NULL, subtree_path,
6063 commitable_paths, status_cb, status_arg, repo);
6068 memcpy(&new_te->id, id, sizeof(new_te->id));
6077 static const struct got_error *
6078 write_tree(struct got_object_id **new_tree_id, int *nentries,
6079 struct got_tree_object *base_tree, const char *path_base_tree,
6080 struct got_pathlist_head *commitable_paths,
6081 got_worktree_status_cb status_cb, void *status_arg,
6082 struct got_repository *repo)
6084 const struct got_error *err = NULL;
6085 struct got_pathlist_head paths;
6086 struct got_tree_entry *te, *new_te = NULL;
6087 struct got_pathlist_entry *pe;
6092 /* Insert, and recurse into, newly added entries first. */
6093 TAILQ_FOREACH(pe, commitable_paths, entry) {
6094 struct got_commitable *ct = pe->data;
6095 char *child_path = NULL, *slash;
6097 if ((ct->status != GOT_STATUS_ADD &&
6098 ct->staged_status != GOT_STATUS_ADD) ||
6099 (ct->flags & GOT_COMMITABLE_ADDED))
6102 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
6103 strlen(path_base_tree)))
6106 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
6111 slash = strchr(child_path, '/');
6112 if (slash == NULL) {
6113 err = alloc_added_blob_tree_entry(&new_te, ct);
6116 err = report_ct_status(ct, status_cb, status_arg);
6119 ct->flags |= GOT_COMMITABLE_ADDED;
6120 err = insert_tree_entry(new_te, &paths);
6125 *slash = '\0'; /* trim trailing path components */
6126 if (base_tree == NULL ||
6127 got_object_tree_find_entry(base_tree, child_path)
6129 err = make_subtree_for_added_blob(&new_te,
6130 child_path, path_base_tree,
6131 commitable_paths, status_cb, status_arg,
6135 err = insert_tree_entry(new_te, &paths);
6144 int i, nbase_entries;
6145 /* Handle modified and deleted entries. */
6146 nbase_entries = got_object_tree_get_nentries(base_tree);
6147 for (i = 0; i < nbase_entries; i++) {
6148 struct got_commitable *ct = NULL;
6150 te = got_object_tree_get_entry(base_tree, i);
6151 if (got_object_tree_entry_is_submodule(te)) {
6152 /* Entry is a submodule; just copy it. */
6153 err = got_object_tree_entry_dup(&new_te, te);
6156 err = insert_tree_entry(new_te, &paths);
6163 if (S_ISDIR(te->mode)) {
6165 err = got_object_tree_entry_dup(&new_te, te);
6168 err = match_modified_subtree(&modified, te,
6169 path_base_tree, commitable_paths);
6172 /* Avoid recursion into unmodified subtrees. */
6174 struct got_object_id *new_id;
6176 err = write_subtree(&new_id,
6178 path_base_tree, commitable_paths,
6179 status_cb, status_arg, repo);
6182 if (nsubentries == 0) {
6183 /* All entries were deleted. */
6187 memcpy(&new_te->id, new_id,
6188 sizeof(new_te->id));
6191 err = insert_tree_entry(new_te, &paths);
6198 err = match_deleted_or_modified_ct(&ct, te,
6199 path_base_tree, commitable_paths);
6203 /* NB: Deleted entries get dropped here. */
6204 if (ct->status == GOT_STATUS_MODIFY ||
6205 ct->status == GOT_STATUS_MODE_CHANGE ||
6206 ct->status == GOT_STATUS_CONFLICT ||
6207 ct->staged_status == GOT_STATUS_MODIFY) {
6208 err = alloc_modified_blob_tree_entry(
6212 err = insert_tree_entry(new_te, &paths);
6217 err = report_ct_status(ct, status_cb,
6222 /* Entry is unchanged; just copy it. */
6223 err = got_object_tree_entry_dup(&new_te, te);
6226 err = insert_tree_entry(new_te, &paths);
6234 /* Write new list of entries; deleted entries have been dropped. */
6235 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
6237 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
6241 static const struct got_error *
6242 update_fileindex_after_commit(struct got_worktree *worktree,
6243 struct got_pathlist_head *commitable_paths,
6244 struct got_object_id *new_base_commit_id,
6245 struct got_fileindex *fileindex, int have_staged_files)
6247 const struct got_error *err = NULL;
6248 struct got_pathlist_entry *pe;
6249 char *relpath = NULL;
6251 TAILQ_FOREACH(pe, commitable_paths, entry) {
6252 struct got_fileindex_entry *ie;
6253 struct got_commitable *ct = pe->data;
6255 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6257 err = got_path_skip_common_ancestor(&relpath,
6258 worktree->root_path, ct->ondisk_path);
6263 if (ct->status == GOT_STATUS_DELETE ||
6264 ct->staged_status == GOT_STATUS_DELETE) {
6265 got_fileindex_entry_remove(fileindex, ie);
6266 } else if (ct->staged_status == GOT_STATUS_ADD ||
6267 ct->staged_status == GOT_STATUS_MODIFY) {
6268 got_fileindex_entry_stage_set(ie,
6269 GOT_FILEIDX_STAGE_NONE);
6270 got_fileindex_entry_staged_filetype_set(ie, 0);
6272 err = got_fileindex_entry_update(ie,
6273 worktree->root_fd, relpath,
6274 ct->staged_blob_id->sha1,
6275 new_base_commit_id->sha1,
6276 !have_staged_files);
6278 err = got_fileindex_entry_update(ie,
6279 worktree->root_fd, relpath,
6281 new_base_commit_id->sha1,
6282 !have_staged_files);
6284 err = got_fileindex_entry_alloc(&ie, pe->path);
6287 err = got_fileindex_entry_update(ie,
6288 worktree->root_fd, relpath, ct->blob_id->sha1,
6289 new_base_commit_id->sha1, 1);
6291 got_fileindex_entry_free(ie);
6294 err = got_fileindex_entry_add(fileindex, ie);
6296 got_fileindex_entry_free(ie);
6309 static const struct got_error *
6310 check_out_of_date(const char *in_repo_path, unsigned char status,
6311 unsigned char staged_status, struct got_object_id *base_blob_id,
6312 struct got_object_id *base_commit_id,
6313 struct got_object_id *head_commit_id, struct got_repository *repo,
6316 const struct got_error *err = NULL;
6317 struct got_commit_object *commit = NULL;
6318 struct got_object_id *id = NULL;
6320 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
6321 /* Trivial case: base commit == head commit */
6322 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
6325 * Ensure file content which local changes were based
6326 * on matches file content in the branch head.
6328 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6331 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6333 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6334 err = got_error(ood_errcode);
6336 } else if (got_object_id_cmp(id, base_blob_id) != 0)
6337 err = got_error(ood_errcode);
6339 /* Require that added files don't exist in the branch head. */
6340 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6343 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6344 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
6346 err = id ? got_error(ood_errcode) : NULL;
6351 got_object_commit_close(commit);
6355 static const struct got_error *
6356 commit_worktree(struct got_object_id **new_commit_id,
6357 struct got_pathlist_head *commitable_paths,
6358 struct got_object_id *head_commit_id,
6359 struct got_object_id *parent_id2,
6360 struct got_worktree *worktree,
6361 const char *author, const char *committer, char *diff_path,
6362 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6363 got_worktree_status_cb status_cb, void *status_arg,
6364 struct got_repository *repo)
6366 const struct got_error *err = NULL, *unlockerr = NULL;
6367 struct got_pathlist_entry *pe;
6368 const char *head_ref_name = NULL;
6369 struct got_commit_object *head_commit = NULL;
6370 struct got_reference *head_ref2 = NULL;
6371 struct got_object_id *head_commit_id2 = NULL;
6372 struct got_tree_object *head_tree = NULL;
6373 struct got_object_id *new_tree_id = NULL;
6374 int nentries, nparents = 0;
6375 struct got_object_id_queue parent_ids;
6376 struct got_object_qid *pid = NULL;
6377 char *logmsg = NULL;
6380 *new_commit_id = NULL;
6382 STAILQ_INIT(&parent_ids);
6384 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6388 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6392 if (commit_msg_cb != NULL) {
6393 err = commit_msg_cb(commitable_paths, diff_path,
6394 &logmsg, commit_arg);
6399 if (logmsg == NULL || strlen(logmsg) == 0) {
6400 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6404 /* Create blobs from added and modified files and record their IDs. */
6405 TAILQ_FOREACH(pe, commitable_paths, entry) {
6406 struct got_commitable *ct = pe->data;
6409 /* Blobs for staged files already exist. */
6410 if (ct->staged_status == GOT_STATUS_ADD ||
6411 ct->staged_status == GOT_STATUS_MODIFY)
6414 if (ct->status != GOT_STATUS_ADD &&
6415 ct->status != GOT_STATUS_MODIFY &&
6416 ct->status != GOT_STATUS_MODE_CHANGE &&
6417 ct->status != GOT_STATUS_CONFLICT)
6420 if (asprintf(&ondisk_path, "%s/%s",
6421 worktree->root_path, pe->path) == -1) {
6422 err = got_error_from_errno("asprintf");
6425 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6431 /* Recursively write new tree objects. */
6432 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6433 commitable_paths, status_cb, status_arg, repo);
6437 err = got_object_qid_alloc(&pid, head_commit_id);
6440 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6443 err = got_object_qid_alloc(&pid, parent_id2);
6446 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6449 timestamp = time(NULL);
6450 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6451 nparents, author, timestamp, committer, timestamp, logmsg, repo);
6457 /* Check if a concurrent commit to our branch has occurred. */
6458 head_ref_name = got_worktree_get_head_ref_name(worktree);
6459 if (head_ref_name == NULL) {
6460 err = got_error_from_errno("got_worktree_get_head_ref_name");
6463 /* Lock the reference here to prevent concurrent modification. */
6464 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6467 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6470 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6471 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6474 /* Update branch head in repository. */
6475 err = got_ref_change_ref(head_ref2, *new_commit_id);
6478 err = got_ref_write(head_ref2, repo);
6482 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6486 err = ref_base_commit(worktree, repo);
6490 got_object_id_queue_free(&parent_ids);
6492 got_object_tree_close(head_tree);
6494 got_object_commit_close(head_commit);
6495 free(head_commit_id2);
6497 unlockerr = got_ref_unlock(head_ref2);
6498 if (unlockerr && err == NULL)
6500 got_ref_close(head_ref2);
6505 static const struct got_error *
6506 check_path_is_commitable(const char *path,
6507 struct got_pathlist_head *commitable_paths)
6509 struct got_pathlist_entry *cpe = NULL;
6510 size_t path_len = strlen(path);
6512 TAILQ_FOREACH(cpe, commitable_paths, entry) {
6513 struct got_commitable *ct = cpe->data;
6514 const char *ct_path = ct->path;
6516 while (ct_path[0] == '/')
6519 if (strcmp(path, ct_path) == 0 ||
6520 got_path_is_child(ct_path, path, path_len))
6525 return got_error_path(path, GOT_ERR_BAD_PATH);
6530 static const struct got_error *
6531 check_staged_file(void *arg, struct got_fileindex_entry *ie)
6533 int *have_staged_files = arg;
6535 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6536 *have_staged_files = 1;
6537 return got_error(GOT_ERR_CANCELLED);
6543 static const struct got_error *
6544 check_non_staged_files(struct got_fileindex *fileindex,
6545 struct got_pathlist_head *paths)
6547 struct got_pathlist_entry *pe;
6548 struct got_fileindex_entry *ie;
6550 TAILQ_FOREACH(pe, paths, entry) {
6551 if (pe->path[0] == '\0')
6553 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6555 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6556 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6557 return got_error_path(pe->path,
6558 GOT_ERR_FILE_NOT_STAGED);
6564 const struct got_error *
6565 got_worktree_commit(struct got_object_id **new_commit_id,
6566 struct got_worktree *worktree, struct got_pathlist_head *paths,
6567 const char *author, const char *committer, int allow_bad_symlinks,
6568 int show_diff, int commit_conflicts,
6569 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6570 got_worktree_status_cb status_cb, void *status_arg,
6571 struct got_repository *repo)
6573 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6574 struct got_fileindex *fileindex = NULL;
6575 char *fileindex_path = NULL;
6576 struct got_pathlist_head commitable_paths;
6577 struct collect_commitables_arg cc_arg;
6578 struct got_pathlist_entry *pe;
6579 struct got_reference *head_ref = NULL;
6580 struct got_object_id *head_commit_id = NULL;
6581 char *diff_path = NULL;
6582 int have_staged_files = 0;
6584 *new_commit_id = NULL;
6586 memset(&cc_arg, 0, sizeof(cc_arg));
6587 TAILQ_INIT(&commitable_paths);
6589 err = lock_worktree(worktree, LOCK_EX);
6593 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6597 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6601 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6605 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6606 &have_staged_files);
6607 if (err && err->code != GOT_ERR_CANCELLED)
6609 if (have_staged_files) {
6610 err = check_non_staged_files(fileindex, paths);
6615 cc_arg.commitable_paths = &commitable_paths;
6616 cc_arg.worktree = worktree;
6617 cc_arg.fileindex = fileindex;
6619 cc_arg.have_staged_files = have_staged_files;
6620 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6621 cc_arg.diff_header_shown = 0;
6622 cc_arg.commit_conflicts = commit_conflicts;
6624 err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6625 GOT_TMPDIR_STR "/got", ".diff");
6628 cc_arg.f1 = got_opentemp();
6629 if (cc_arg.f1 == NULL) {
6630 err = got_error_from_errno("got_opentemp");
6633 cc_arg.f2 = got_opentemp();
6634 if (cc_arg.f2 == NULL) {
6635 err = got_error_from_errno("got_opentemp");
6640 TAILQ_FOREACH(pe, paths, entry) {
6641 err = worktree_status(worktree, pe->path, fileindex, repo,
6642 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6648 if (fflush(cc_arg.diff_outfile) == EOF) {
6649 err = got_error_from_errno("fflush");
6654 if (TAILQ_EMPTY(&commitable_paths)) {
6655 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6659 TAILQ_FOREACH(pe, paths, entry) {
6660 err = check_path_is_commitable(pe->path, &commitable_paths);
6665 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6666 struct got_commitable *ct = pe->data;
6667 const char *ct_path = ct->in_repo_path;
6669 while (ct_path[0] == '/')
6671 err = check_out_of_date(ct_path, ct->status,
6672 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6673 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6679 err = commit_worktree(new_commit_id, &commitable_paths,
6680 head_commit_id, NULL, worktree, author, committer,
6681 (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6682 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6686 err = update_fileindex_after_commit(worktree, &commitable_paths,
6687 *new_commit_id, fileindex, have_staged_files);
6688 sync_err = sync_fileindex(fileindex, fileindex_path);
6689 if (sync_err && err == NULL)
6693 got_fileindex_free(fileindex);
6694 free(fileindex_path);
6695 unlockerr = lock_worktree(worktree, LOCK_SH);
6696 if (unlockerr && err == NULL)
6698 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6699 struct got_commitable *ct = pe->data;
6701 free_commitable(ct);
6703 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6704 if (diff_path && unlink(diff_path) == -1 && err == NULL)
6705 err = got_error_from_errno2("unlink", diff_path);
6707 if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6709 err = got_error_from_errno("fclose");
6714 got_commitable_get_path(struct got_commitable *ct)
6720 got_commitable_get_status(struct got_commitable *ct)
6725 struct check_rebase_ok_arg {
6726 struct got_worktree *worktree;
6727 struct got_repository *repo;
6730 static const struct got_error *
6731 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6733 const struct got_error *err = NULL;
6734 struct check_rebase_ok_arg *a = arg;
6735 unsigned char status;
6739 /* Reject rebase of a work tree with mixed base commits. */
6740 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6741 SHA1_DIGEST_LENGTH))
6742 return got_error(GOT_ERR_MIXED_COMMITS);
6744 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6746 return got_error_from_errno("asprintf");
6748 /* Reject rebase of a work tree with modified or staged files. */
6749 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6754 if (status != GOT_STATUS_NO_CHANGE)
6755 return got_error(GOT_ERR_MODIFIED);
6756 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6757 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6762 const struct got_error *
6763 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6764 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6765 struct got_worktree *worktree, struct got_reference *branch,
6766 struct got_repository *repo)
6768 const struct got_error *err = NULL;
6769 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6770 char *branch_ref_name = NULL;
6771 char *fileindex_path = NULL;
6772 struct check_rebase_ok_arg ok_arg;
6773 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6774 struct got_object_id *wt_branch_tip = NULL;
6776 *new_base_branch_ref = NULL;
6780 err = lock_worktree(worktree, LOCK_EX);
6784 err = open_fileindex(fileindex, &fileindex_path, worktree);
6788 ok_arg.worktree = worktree;
6790 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6795 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6799 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6803 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6807 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6812 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6815 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6816 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6820 err = got_ref_alloc_symref(new_base_branch_ref,
6821 new_base_branch_ref_name, wt_branch);
6824 err = got_ref_write(*new_base_branch_ref, repo);
6828 /* TODO Lock original branch's ref while rebasing? */
6830 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6834 err = got_ref_write(branch_ref, repo);
6838 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6839 worktree->base_commit_id);
6842 err = got_ref_write(*tmp_branch, repo);
6846 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6850 free(fileindex_path);
6851 free(tmp_branch_name);
6852 free(new_base_branch_ref_name);
6853 free(branch_ref_name);
6855 got_ref_close(branch_ref);
6857 got_ref_close(wt_branch);
6858 free(wt_branch_tip);
6860 if (*new_base_branch_ref) {
6861 got_ref_close(*new_base_branch_ref);
6862 *new_base_branch_ref = NULL;
6865 got_ref_close(*tmp_branch);
6869 got_fileindex_free(*fileindex);
6872 lock_worktree(worktree, LOCK_SH);
6877 const struct got_error *
6878 got_worktree_rebase_continue(struct got_object_id **commit_id,
6879 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6880 struct got_reference **branch, struct got_fileindex **fileindex,
6881 struct got_worktree *worktree, struct got_repository *repo)
6883 const struct got_error *err;
6884 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6885 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6886 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6887 char *fileindex_path = NULL;
6888 int have_staged_files = 0;
6891 *new_base_branch = NULL;
6896 err = lock_worktree(worktree, LOCK_EX);
6900 err = open_fileindex(fileindex, &fileindex_path, worktree);
6904 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6905 &have_staged_files);
6906 if (err && err->code != GOT_ERR_CANCELLED)
6908 if (have_staged_files) {
6909 err = got_error(GOT_ERR_STAGED_PATHS);
6913 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6917 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6921 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6925 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6929 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6933 err = got_ref_open(branch, repo,
6934 got_ref_get_symref_target(branch_ref), 0);
6938 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6942 err = got_ref_resolve(commit_id, repo, commit_ref);
6946 err = got_ref_open(new_base_branch, repo,
6947 new_base_branch_ref_name, 0);
6951 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6955 free(commit_ref_name);
6956 free(branch_ref_name);
6957 free(fileindex_path);
6959 got_ref_close(commit_ref);
6961 got_ref_close(branch_ref);
6966 got_ref_close(*tmp_branch);
6969 if (*new_base_branch) {
6970 got_ref_close(*new_base_branch);
6971 *new_base_branch = NULL;
6974 got_ref_close(*branch);
6978 got_fileindex_free(*fileindex);
6981 lock_worktree(worktree, LOCK_SH);
6986 const struct got_error *
6987 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6989 const struct got_error *err;
6990 char *tmp_branch_name = NULL;
6992 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6996 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6997 free(tmp_branch_name);
7001 static const struct got_error *
7002 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
7003 const char *diff_path, char **logmsg, void *arg)
7009 static const struct got_error *
7010 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
7011 const char *path, struct got_object_id *blob_id,
7012 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7013 int dirfd, const char *de_name)
7018 struct collect_merged_paths_arg {
7019 got_worktree_checkout_cb progress_cb;
7021 struct got_pathlist_head *merged_paths;
7024 static const struct got_error *
7025 collect_merged_paths(void *arg, unsigned char status, const char *path)
7027 const struct got_error *err;
7028 struct collect_merged_paths_arg *a = arg;
7030 struct got_pathlist_entry *new;
7032 err = (*a->progress_cb)(a->progress_arg, status, path);
7036 if (status != GOT_STATUS_MERGE &&
7037 status != GOT_STATUS_ADD &&
7038 status != GOT_STATUS_DELETE &&
7039 status != GOT_STATUS_CONFLICT)
7044 return got_error_from_errno("strdup");
7046 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
7047 if (err || new == NULL)
7052 static const struct got_error *
7053 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
7054 int is_rebase, struct got_repository *repo)
7056 const struct got_error *err;
7057 struct got_reference *commit_ref = NULL;
7059 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7061 if (err->code != GOT_ERR_NOT_REF)
7063 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
7066 err = got_ref_write(commit_ref, repo);
7069 } else if (is_rebase) {
7070 struct got_object_id *stored_id;
7073 err = got_ref_resolve(&stored_id, repo, commit_ref);
7076 cmp = got_object_id_cmp(commit_id, stored_id);
7079 err = got_error(GOT_ERR_REBASE_COMMITID);
7085 got_ref_close(commit_ref);
7089 static const struct got_error *
7090 rebase_merge_files(struct got_pathlist_head *merged_paths,
7091 const char *commit_ref_name, struct got_worktree *worktree,
7092 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
7093 struct got_object_id *commit_id, struct got_repository *repo,
7094 got_worktree_checkout_cb progress_cb, void *progress_arg,
7095 got_cancel_cb cancel_cb, void *cancel_arg)
7097 const struct got_error *err;
7098 struct got_reference *commit_ref = NULL;
7099 struct collect_merged_paths_arg cmp_arg;
7100 char *fileindex_path;
7102 /* Work tree is locked/unlocked during rebase preparation/teardown. */
7104 err = get_fileindex_path(&fileindex_path, worktree);
7108 cmp_arg.progress_cb = progress_cb;
7109 cmp_arg.progress_arg = progress_arg;
7110 cmp_arg.merged_paths = merged_paths;
7111 err = merge_files(worktree, fileindex, fileindex_path,
7112 parent_commit_id, commit_id, repo, collect_merged_paths,
7113 &cmp_arg, cancel_cb, cancel_arg);
7115 got_ref_close(commit_ref);
7119 const struct got_error *
7120 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
7121 struct got_worktree *worktree, struct got_fileindex *fileindex,
7122 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
7123 struct got_repository *repo,
7124 got_worktree_checkout_cb progress_cb, void *progress_arg,
7125 got_cancel_cb cancel_cb, void *cancel_arg)
7127 const struct got_error *err;
7128 char *commit_ref_name;
7130 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7134 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
7138 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7139 fileindex, parent_commit_id, commit_id, repo, progress_cb,
7140 progress_arg, cancel_cb, cancel_arg);
7142 free(commit_ref_name);
7146 const struct got_error *
7147 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
7148 struct got_worktree *worktree, struct got_fileindex *fileindex,
7149 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
7150 struct got_repository *repo,
7151 got_worktree_checkout_cb progress_cb, void *progress_arg,
7152 got_cancel_cb cancel_cb, void *cancel_arg)
7154 const struct got_error *err;
7155 char *commit_ref_name;
7157 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7161 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7165 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7166 fileindex, parent_commit_id, commit_id, repo, progress_cb,
7167 progress_arg, cancel_cb, cancel_arg);
7169 free(commit_ref_name);
7173 static const struct got_error *
7174 rebase_commit(struct got_object_id **new_commit_id,
7175 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
7176 struct got_worktree *worktree, struct got_fileindex *fileindex,
7177 struct got_reference *tmp_branch, const char *committer,
7178 struct got_commit_object *orig_commit, const char *new_logmsg,
7179 int allow_conflict, struct got_repository *repo)
7181 const struct got_error *err, *sync_err;
7182 struct got_pathlist_head commitable_paths;
7183 struct collect_commitables_arg cc_arg;
7184 char *fileindex_path = NULL;
7185 struct got_reference *head_ref = NULL;
7186 struct got_object_id *head_commit_id = NULL;
7187 char *logmsg = NULL;
7189 memset(&cc_arg, 0, sizeof(cc_arg));
7190 TAILQ_INIT(&commitable_paths);
7191 *new_commit_id = NULL;
7193 /* Work tree is locked/unlocked during rebase preparation/teardown. */
7195 err = get_fileindex_path(&fileindex_path, worktree);
7199 cc_arg.commitable_paths = &commitable_paths;
7200 cc_arg.worktree = worktree;
7202 cc_arg.have_staged_files = 0;
7203 cc_arg.commit_conflicts = allow_conflict;
7205 * If possible get the status of individual files directly to
7206 * avoid crawling the entire work tree once per rebased commit.
7208 * Ideally, merged_paths would contain a list of commitables
7209 * we could use so we could skip worktree_status() entirely.
7210 * However, we would then need carefully keep track of cumulative
7211 * effects of operations such as file additions and deletions
7212 * in 'got histedit -f' (folding multiple commits into one),
7213 * and this extra complexity is not really worth it.
7216 struct got_pathlist_entry *pe;
7217 TAILQ_FOREACH(pe, merged_paths, entry) {
7218 err = worktree_status(worktree, pe->path, fileindex,
7219 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
7225 err = worktree_status(worktree, "", fileindex, repo,
7226 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7231 if (TAILQ_EMPTY(&commitable_paths)) {
7232 /* No-op change; commit will be elided. */
7233 err = got_ref_delete(commit_ref, repo);
7236 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
7240 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7244 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7249 logmsg = strdup(new_logmsg);
7250 if (logmsg == NULL) {
7251 err = got_error_from_errno("strdup");
7255 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
7260 /* NB: commit_worktree will call free(logmsg) */
7261 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
7262 NULL, worktree, got_object_commit_get_author(orig_commit),
7263 committer ? committer :
7264 got_object_commit_get_committer(orig_commit), NULL,
7265 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
7269 err = got_ref_change_ref(tmp_branch, *new_commit_id);
7273 err = got_ref_delete(commit_ref, repo);
7277 err = update_fileindex_after_commit(worktree, &commitable_paths,
7278 *new_commit_id, fileindex, 0);
7279 sync_err = sync_fileindex(fileindex, fileindex_path);
7280 if (sync_err && err == NULL)
7283 free(fileindex_path);
7284 free(head_commit_id);
7286 got_ref_close(head_ref);
7288 free(*new_commit_id);
7289 *new_commit_id = NULL;
7294 const struct got_error *
7295 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
7296 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7297 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7298 const char *committer, struct got_commit_object *orig_commit,
7299 struct got_object_id *orig_commit_id, int allow_conflict,
7300 struct got_repository *repo)
7302 const struct got_error *err;
7303 char *commit_ref_name;
7304 struct got_reference *commit_ref = NULL;
7305 struct got_object_id *commit_id = NULL;
7307 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7311 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7314 err = got_ref_resolve(&commit_id, repo, commit_ref);
7317 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
7318 err = got_error(GOT_ERR_REBASE_COMMITID);
7322 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7323 worktree, fileindex, tmp_branch, committer, orig_commit,
7324 NULL, allow_conflict, repo);
7327 got_ref_close(commit_ref);
7328 free(commit_ref_name);
7333 const struct got_error *
7334 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
7335 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7336 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7337 const char *committer, struct got_commit_object *orig_commit,
7338 struct got_object_id *orig_commit_id, const char *new_logmsg,
7339 int allow_conflict, struct got_repository *repo)
7341 const struct got_error *err;
7342 char *commit_ref_name;
7343 struct got_reference *commit_ref = NULL;
7345 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7349 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7353 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7354 worktree, fileindex, tmp_branch, committer, orig_commit,
7355 new_logmsg, allow_conflict, repo);
7358 got_ref_close(commit_ref);
7359 free(commit_ref_name);
7363 const struct got_error *
7364 got_worktree_rebase_postpone(struct got_worktree *worktree,
7365 struct got_fileindex *fileindex)
7368 got_fileindex_free(fileindex);
7369 return lock_worktree(worktree, LOCK_SH);
7372 static const struct got_error *
7373 delete_ref(const char *name, struct got_repository *repo)
7375 const struct got_error *err;
7376 struct got_reference *ref;
7378 err = got_ref_open(&ref, repo, name, 0);
7380 if (err->code == GOT_ERR_NOT_REF)
7385 err = got_ref_delete(ref, repo);
7390 static const struct got_error *
7391 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7393 const struct got_error *err;
7394 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7395 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7397 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7400 err = delete_ref(tmp_branch_name, repo);
7404 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7407 err = delete_ref(new_base_branch_ref_name, repo);
7411 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7414 err = delete_ref(branch_ref_name, repo);
7418 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7421 err = delete_ref(commit_ref_name, repo);
7426 free(tmp_branch_name);
7427 free(new_base_branch_ref_name);
7428 free(branch_ref_name);
7429 free(commit_ref_name);
7433 static const struct got_error *
7434 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7435 struct got_object_id *new_commit_id, struct got_repository *repo)
7437 const struct got_error *err;
7438 struct got_reference *ref = NULL;
7439 struct got_object_id *old_commit_id = NULL;
7440 const char *branch_name = NULL;
7441 char *new_id_str = NULL;
7442 char *refname = NULL;
7444 branch_name = got_ref_get_name(branch);
7445 if (strncmp(branch_name, "refs/heads/", 11) != 0)
7446 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7449 err = got_object_id_str(&new_id_str, new_commit_id);
7453 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7454 new_id_str) == -1) {
7455 err = got_error_from_errno("asprintf");
7459 err = got_ref_resolve(&old_commit_id, repo, branch);
7463 err = got_ref_alloc(&ref, refname, old_commit_id);
7467 err = got_ref_write(ref, repo);
7471 free(old_commit_id);
7477 const struct got_error *
7478 got_worktree_rebase_complete(struct got_worktree *worktree,
7479 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7480 struct got_reference *rebased_branch, struct got_repository *repo,
7483 const struct got_error *err, *unlockerr, *sync_err;
7484 struct got_object_id *new_head_commit_id = NULL;
7485 char *fileindex_path = NULL;
7487 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7491 if (create_backup) {
7492 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7493 rebased_branch, new_head_commit_id, repo);
7498 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7502 err = got_ref_write(rebased_branch, repo);
7506 err = got_worktree_set_head_ref(worktree, rebased_branch);
7510 err = delete_rebase_refs(worktree, repo);
7514 err = get_fileindex_path(&fileindex_path, worktree);
7517 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7518 sync_err = sync_fileindex(fileindex, fileindex_path);
7519 if (sync_err && err == NULL)
7522 got_fileindex_free(fileindex);
7523 free(fileindex_path);
7524 free(new_head_commit_id);
7525 unlockerr = lock_worktree(worktree, LOCK_SH);
7526 if (unlockerr && err == NULL)
7531 static const struct got_error *
7532 get_paths_changed_between_commits(struct got_pathlist_head *paths,
7533 struct got_object_id *id1, struct got_object_id *id2,
7534 struct got_repository *repo)
7536 const struct got_error *err;
7537 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
7538 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7541 err = got_object_open_as_commit(&commit1, repo, id1);
7545 err = got_object_open_as_tree(&tree1, repo,
7546 got_object_commit_get_tree_id(commit1));
7552 err = got_object_open_as_commit(&commit2, repo, id2);
7556 err = got_object_open_as_tree(&tree2, repo,
7557 got_object_commit_get_tree_id(commit2));
7562 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
7563 got_diff_tree_collect_changed_paths, paths, 0);
7568 got_object_commit_close(commit1);
7570 got_object_commit_close(commit2);
7572 got_object_tree_close(tree1);
7574 got_object_tree_close(tree2);
7578 static const struct got_error *
7579 get_paths_added_between_commits(struct got_pathlist_head *added_paths,
7580 struct got_object_id *id1, struct got_object_id *id2,
7581 const char *path_prefix, struct got_repository *repo)
7583 const struct got_error *err;
7584 struct got_pathlist_head merged_paths;
7585 struct got_pathlist_entry *pe;
7586 char *abspath = NULL, *wt_path = NULL;
7588 TAILQ_INIT(&merged_paths);
7590 err = get_paths_changed_between_commits(&merged_paths, id1, id2, repo);
7594 TAILQ_FOREACH(pe, &merged_paths, entry) {
7595 struct got_diff_changed_path *change = pe->data;
7597 if (change->status != GOT_STATUS_ADD)
7600 if (got_path_is_root_dir(path_prefix)) {
7601 wt_path = strdup(pe->path);
7602 if (wt_path == NULL) {
7603 err = got_error_from_errno("strdup");
7607 if (asprintf(&abspath, "/%s", pe->path) == -1) {
7608 err = got_error_from_errno("asprintf");
7612 err = got_path_skip_common_ancestor(&wt_path,
7613 path_prefix, abspath);
7620 err = got_pathlist_append(added_paths, wt_path, NULL);
7627 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_ALL);
7633 static const struct got_error *
7634 get_paths_added_in_commit(struct got_pathlist_head *added_paths,
7635 struct got_object_id *id, const char *path_prefix,
7636 struct got_repository *repo)
7638 const struct got_error *err;
7639 struct got_commit_object *commit = NULL;
7640 struct got_object_qid *pid;
7642 err = got_object_open_as_commit(&commit, repo, id);
7646 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7648 err = get_paths_added_between_commits(added_paths,
7649 pid ? &pid->id : NULL, id, path_prefix, repo);
7654 got_object_commit_close(commit);
7658 const struct got_error *
7659 got_worktree_rebase_abort(struct got_worktree *worktree,
7660 struct got_fileindex *fileindex, struct got_repository *repo,
7661 struct got_reference *new_base_branch,
7662 got_worktree_checkout_cb progress_cb, void *progress_arg)
7664 const struct got_error *err, *unlockerr, *sync_err;
7665 struct got_reference *resolved = NULL;
7666 struct got_object_id *commit_id = NULL;
7667 struct got_object_id *merged_commit_id = NULL;
7668 struct got_commit_object *commit = NULL;
7669 char *fileindex_path = NULL;
7670 char *commit_ref_name = NULL;
7671 struct got_reference *commit_ref = NULL;
7672 struct revert_file_args rfa;
7673 struct got_object_id *tree_id = NULL;
7674 struct got_pathlist_head added_paths;
7676 TAILQ_INIT(&added_paths);
7678 err = lock_worktree(worktree, LOCK_EX);
7682 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7686 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7690 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
7695 * Determine which files in added status can be safely removed
7696 * from disk while reverting changes in the work tree.
7697 * We want to avoid deleting unrelated files which were added by
7698 * the user for conflict resolution purposes.
7700 err = get_paths_added_in_commit(&added_paths, merged_commit_id,
7701 got_worktree_get_path_prefix(worktree), repo);
7705 err = got_ref_open(&resolved, repo,
7706 got_ref_get_symref_target(new_base_branch), 0);
7710 err = got_worktree_set_head_ref(worktree, resolved);
7715 * XXX commits to the base branch could have happened while
7716 * we were busy rebasing; should we store the original commit ID
7717 * when rebase begins and read it back here?
7719 err = got_ref_resolve(&commit_id, repo, resolved);
7723 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7727 err = got_object_open_as_commit(&commit, repo,
7728 worktree->base_commit_id);
7732 err = got_object_id_by_path(&tree_id, repo, commit,
7733 worktree->path_prefix);
7737 err = delete_rebase_refs(worktree, repo);
7741 err = get_fileindex_path(&fileindex_path, worktree);
7745 rfa.worktree = worktree;
7746 rfa.fileindex = fileindex;
7747 rfa.progress_cb = progress_cb;
7748 rfa.progress_arg = progress_arg;
7749 rfa.patch_cb = NULL;
7750 rfa.patch_arg = NULL;
7752 rfa.unlink_added_files = 1;
7753 rfa.added_files_to_unlink = &added_paths;
7754 err = worktree_status(worktree, "", fileindex, repo,
7755 revert_file, &rfa, NULL, NULL, 1, 0);
7759 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7760 repo, progress_cb, progress_arg, NULL, NULL);
7762 sync_err = sync_fileindex(fileindex, fileindex_path);
7763 if (sync_err && err == NULL)
7766 got_pathlist_free(&added_paths, GOT_PATHLIST_FREE_PATH);
7767 got_ref_close(resolved);
7770 free(merged_commit_id);
7772 got_object_commit_close(commit);
7774 got_fileindex_free(fileindex);
7775 free(fileindex_path);
7776 free(commit_ref_name);
7778 got_ref_close(commit_ref);
7780 unlockerr = lock_worktree(worktree, LOCK_SH);
7781 if (unlockerr && err == NULL)
7786 const struct got_error *
7787 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7788 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7789 struct got_fileindex **fileindex, struct got_worktree *worktree,
7790 struct got_repository *repo)
7792 const struct got_error *err = NULL;
7793 char *tmp_branch_name = NULL;
7794 char *branch_ref_name = NULL;
7795 char *base_commit_ref_name = NULL;
7796 char *fileindex_path = NULL;
7797 struct check_rebase_ok_arg ok_arg;
7798 struct got_reference *wt_branch = NULL;
7799 struct got_reference *base_commit_ref = NULL;
7803 *base_commit_id = NULL;
7806 err = lock_worktree(worktree, LOCK_EX);
7810 err = open_fileindex(fileindex, &fileindex_path, worktree);
7814 ok_arg.worktree = worktree;
7816 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7821 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7825 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7829 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7834 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7839 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7843 err = got_ref_write(*branch_ref, repo);
7847 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7848 worktree->base_commit_id);
7851 err = got_ref_write(base_commit_ref, repo);
7854 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7855 if (*base_commit_id == NULL) {
7856 err = got_error_from_errno("got_object_id_dup");
7860 err = got_ref_alloc(tmp_branch, tmp_branch_name,
7861 worktree->base_commit_id);
7864 err = got_ref_write(*tmp_branch, repo);
7868 err = got_worktree_set_head_ref(worktree, *tmp_branch);
7872 free(fileindex_path);
7873 free(tmp_branch_name);
7874 free(branch_ref_name);
7875 free(base_commit_ref_name);
7877 got_ref_close(wt_branch);
7880 got_ref_close(*branch_ref);
7884 got_ref_close(*tmp_branch);
7887 free(*base_commit_id);
7889 got_fileindex_free(*fileindex);
7892 lock_worktree(worktree, LOCK_SH);
7897 const struct got_error *
7898 got_worktree_histedit_postpone(struct got_worktree *worktree,
7899 struct got_fileindex *fileindex)
7902 got_fileindex_free(fileindex);
7903 return lock_worktree(worktree, LOCK_SH);
7906 const struct got_error *
7907 got_worktree_histedit_in_progress(int *in_progress,
7908 struct got_worktree *worktree)
7910 const struct got_error *err;
7911 char *tmp_branch_name = NULL;
7913 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7917 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7918 free(tmp_branch_name);
7922 const struct got_error *
7923 got_worktree_histedit_continue(struct got_object_id **commit_id,
7924 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7925 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7926 struct got_worktree *worktree, struct got_repository *repo)
7928 const struct got_error *err;
7929 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7930 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7931 struct got_reference *commit_ref = NULL;
7932 struct got_reference *base_commit_ref = NULL;
7933 char *fileindex_path = NULL;
7934 int have_staged_files = 0;
7938 *base_commit_id = NULL;
7941 err = lock_worktree(worktree, LOCK_EX);
7945 err = open_fileindex(fileindex, &fileindex_path, worktree);
7949 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7950 &have_staged_files);
7951 if (err && err->code != GOT_ERR_CANCELLED)
7953 if (have_staged_files) {
7954 err = got_error(GOT_ERR_STAGED_PATHS);
7958 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7962 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7966 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7970 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7975 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7979 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7982 err = got_ref_resolve(commit_id, repo, commit_ref);
7986 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7989 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7993 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7997 free(commit_ref_name);
7998 free(branch_ref_name);
7999 free(fileindex_path);
8001 got_ref_close(commit_ref);
8002 if (base_commit_ref)
8003 got_ref_close(base_commit_ref);
8007 free(*base_commit_id);
8008 *base_commit_id = NULL;
8010 got_ref_close(*tmp_branch);
8014 got_fileindex_free(*fileindex);
8017 lock_worktree(worktree, LOCK_EX);
8022 static const struct got_error *
8023 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
8025 const struct got_error *err;
8026 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
8027 char *branch_ref_name = NULL, *commit_ref_name = NULL;
8029 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
8032 err = delete_ref(tmp_branch_name, repo);
8036 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
8040 err = delete_ref(base_commit_ref_name, repo);
8044 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
8047 err = delete_ref(branch_ref_name, repo);
8051 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8054 err = delete_ref(commit_ref_name, repo);
8058 free(tmp_branch_name);
8059 free(base_commit_ref_name);
8060 free(branch_ref_name);
8061 free(commit_ref_name);
8065 const struct got_error *
8066 got_worktree_histedit_abort(struct got_worktree *worktree,
8067 struct got_fileindex *fileindex, struct got_repository *repo,
8068 struct got_reference *branch, struct got_object_id *base_commit_id,
8069 got_worktree_checkout_cb progress_cb, void *progress_arg)
8071 const struct got_error *err, *unlockerr, *sync_err;
8072 struct got_reference *resolved = NULL;
8073 char *fileindex_path = NULL;
8074 struct got_object_id *merged_commit_id = NULL;
8075 struct got_commit_object *commit = NULL;
8076 char *commit_ref_name = NULL;
8077 struct got_reference *commit_ref = NULL;
8078 struct got_object_id *tree_id = NULL;
8079 struct revert_file_args rfa;
8080 struct got_pathlist_head added_paths;
8082 TAILQ_INIT(&added_paths);
8084 err = lock_worktree(worktree, LOCK_EX);
8088 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8092 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8094 if (err->code != GOT_ERR_NOT_REF)
8096 /* Can happen on early abort due to invalid histedit script. */
8101 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8106 * Determine which files in added status can be safely removed
8107 * from disk while reverting changes in the work tree.
8108 * We want to avoid deleting unrelated files added by the
8109 * user during conflict resolution or during histedit -e.
8111 err = get_paths_added_in_commit(&added_paths, merged_commit_id,
8112 got_worktree_get_path_prefix(worktree), repo);
8117 err = got_ref_open(&resolved, repo,
8118 got_ref_get_symref_target(branch), 0);
8122 err = got_worktree_set_head_ref(worktree, resolved);
8126 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
8130 err = got_object_open_as_commit(&commit, repo,
8131 worktree->base_commit_id);
8135 err = got_object_id_by_path(&tree_id, repo, commit,
8136 worktree->path_prefix);
8140 err = delete_histedit_refs(worktree, repo);
8144 err = get_fileindex_path(&fileindex_path, worktree);
8148 rfa.worktree = worktree;
8149 rfa.fileindex = fileindex;
8150 rfa.progress_cb = progress_cb;
8151 rfa.progress_arg = progress_arg;
8152 rfa.patch_cb = NULL;
8153 rfa.patch_arg = NULL;
8155 rfa.unlink_added_files = 1;
8156 rfa.added_files_to_unlink = &added_paths;
8157 err = worktree_status(worktree, "", fileindex, repo,
8158 revert_file, &rfa, NULL, NULL, 1, 0);
8162 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8163 repo, progress_cb, progress_arg, NULL, NULL);
8165 sync_err = sync_fileindex(fileindex, fileindex_path);
8166 if (sync_err && err == NULL)
8170 got_ref_close(resolved);
8172 got_ref_close(commit_ref);
8173 free(merged_commit_id);
8175 free(fileindex_path);
8176 free(commit_ref_name);
8178 unlockerr = lock_worktree(worktree, LOCK_SH);
8179 if (unlockerr && err == NULL)
8184 const struct got_error *
8185 got_worktree_histedit_complete(struct got_worktree *worktree,
8186 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8187 struct got_reference *edited_branch, struct got_repository *repo)
8189 const struct got_error *err, *unlockerr, *sync_err;
8190 struct got_object_id *new_head_commit_id = NULL;
8191 struct got_reference *resolved = NULL;
8192 char *fileindex_path = NULL;
8194 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
8198 err = got_ref_open(&resolved, repo,
8199 got_ref_get_symref_target(edited_branch), 0);
8203 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
8204 resolved, new_head_commit_id, repo);
8208 err = got_ref_change_ref(resolved, new_head_commit_id);
8212 err = got_ref_write(resolved, repo);
8216 err = got_worktree_set_head_ref(worktree, resolved);
8220 err = delete_histedit_refs(worktree, repo);
8224 err = get_fileindex_path(&fileindex_path, worktree);
8227 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8228 sync_err = sync_fileindex(fileindex, fileindex_path);
8229 if (sync_err && err == NULL)
8232 got_fileindex_free(fileindex);
8233 free(fileindex_path);
8234 free(new_head_commit_id);
8235 unlockerr = lock_worktree(worktree, LOCK_SH);
8236 if (unlockerr && err == NULL)
8241 const struct got_error *
8242 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
8243 struct got_object_id *commit_id, struct got_repository *repo)
8245 const struct got_error *err;
8246 char *commit_ref_name;
8248 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8252 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
8256 err = delete_ref(commit_ref_name, repo);
8258 free(commit_ref_name);
8262 const struct got_error *
8263 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
8264 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
8265 struct got_worktree *worktree, const char *refname,
8266 struct got_repository *repo)
8268 const struct got_error *err = NULL;
8269 char *fileindex_path = NULL;
8270 struct check_rebase_ok_arg ok_arg;
8274 *base_branch_ref = NULL;
8276 err = lock_worktree(worktree, LOCK_EX);
8280 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
8281 err = got_error_msg(GOT_ERR_SAME_BRANCH,
8282 "cannot integrate a branch into itself; "
8283 "update -b or different branch name required");
8287 err = open_fileindex(fileindex, &fileindex_path, worktree);
8291 /* Preconditions are the same as for rebase. */
8292 ok_arg.worktree = worktree;
8294 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8299 err = got_ref_open(branch_ref, repo, refname, 1);
8303 err = got_ref_open(base_branch_ref, repo,
8304 got_worktree_get_head_ref_name(worktree), 1);
8308 got_ref_close(*branch_ref);
8311 if (*base_branch_ref) {
8312 got_ref_close(*base_branch_ref);
8313 *base_branch_ref = NULL;
8316 got_fileindex_free(*fileindex);
8319 lock_worktree(worktree, LOCK_SH);
8324 const struct got_error *
8325 got_worktree_integrate_continue(struct got_worktree *worktree,
8326 struct got_fileindex *fileindex, struct got_repository *repo,
8327 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
8328 got_worktree_checkout_cb progress_cb, void *progress_arg,
8329 got_cancel_cb cancel_cb, void *cancel_arg)
8331 const struct got_error *err = NULL, *sync_err, *unlockerr;
8332 char *fileindex_path = NULL;
8333 struct got_object_id *tree_id = NULL, *commit_id = NULL;
8334 struct got_commit_object *commit = NULL;
8336 err = get_fileindex_path(&fileindex_path, worktree);
8340 err = got_ref_resolve(&commit_id, repo, branch_ref);
8344 err = got_object_open_as_commit(&commit, repo, commit_id);
8348 err = got_object_id_by_path(&tree_id, repo, commit,
8349 worktree->path_prefix);
8353 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
8357 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
8358 progress_cb, progress_arg, cancel_cb, cancel_arg);
8362 err = got_ref_change_ref(base_branch_ref, commit_id);
8366 err = got_ref_write(base_branch_ref, repo);
8370 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8372 sync_err = sync_fileindex(fileindex, fileindex_path);
8373 if (sync_err && err == NULL)
8377 unlockerr = got_ref_unlock(branch_ref);
8378 if (unlockerr && err == NULL)
8380 got_ref_close(branch_ref);
8382 unlockerr = got_ref_unlock(base_branch_ref);
8383 if (unlockerr && err == NULL)
8385 got_ref_close(base_branch_ref);
8387 got_fileindex_free(fileindex);
8388 free(fileindex_path);
8391 got_object_commit_close(commit);
8393 unlockerr = lock_worktree(worktree, LOCK_SH);
8394 if (unlockerr && err == NULL)
8399 const struct got_error *
8400 got_worktree_integrate_abort(struct got_worktree *worktree,
8401 struct got_fileindex *fileindex, struct got_repository *repo,
8402 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
8404 const struct got_error *err = NULL, *unlockerr = NULL;
8406 got_fileindex_free(fileindex);
8408 err = lock_worktree(worktree, LOCK_SH);
8410 unlockerr = got_ref_unlock(branch_ref);
8411 if (unlockerr && err == NULL)
8413 got_ref_close(branch_ref);
8415 unlockerr = got_ref_unlock(base_branch_ref);
8416 if (unlockerr && err == NULL)
8418 got_ref_close(base_branch_ref);
8423 const struct got_error *
8424 got_worktree_merge_postpone(struct got_worktree *worktree,
8425 struct got_fileindex *fileindex)
8427 const struct got_error *err, *sync_err;
8428 char *fileindex_path = NULL;
8430 err = get_fileindex_path(&fileindex_path, worktree);
8434 sync_err = sync_fileindex(fileindex, fileindex_path);
8436 err = lock_worktree(worktree, LOCK_SH);
8437 if (sync_err && err == NULL)
8440 got_fileindex_free(fileindex);
8441 free(fileindex_path);
8445 static const struct got_error *
8446 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
8448 const struct got_error *err;
8449 char *branch_refname = NULL, *commit_refname = NULL;
8451 err = get_merge_branch_ref_name(&branch_refname, worktree);
8454 err = delete_ref(branch_refname, repo);
8458 err = get_merge_commit_ref_name(&commit_refname, worktree);
8461 err = delete_ref(commit_refname, repo);
8466 free(branch_refname);
8467 free(commit_refname);
8471 struct merge_commit_msg_arg {
8472 struct got_worktree *worktree;
8473 const char *branch_name;
8476 static const struct got_error *
8477 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
8478 const char *diff_path, char **logmsg, void *arg)
8480 struct merge_commit_msg_arg *a = arg;
8482 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
8483 got_worktree_get_head_ref_name(a->worktree)) == -1)
8484 return got_error_from_errno("asprintf");
8490 const struct got_error *
8491 got_worktree_merge_branch(struct got_worktree *worktree,
8492 struct got_fileindex *fileindex,
8493 struct got_object_id *yca_commit_id,
8494 struct got_object_id *branch_tip,
8495 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
8496 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
8498 const struct got_error *err;
8499 char *fileindex_path = NULL;
8500 struct check_mixed_commits_args cma;
8502 err = get_fileindex_path(&fileindex_path, worktree);
8506 cma.worktree = worktree;
8507 cma.cancel_cb = cancel_cb;
8508 cma.cancel_arg = cancel_arg;
8510 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
8515 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
8516 branch_tip, repo, progress_cb, progress_arg,
8517 cancel_cb, cancel_arg);
8519 free(fileindex_path);
8523 const struct got_error *
8524 got_worktree_merge_commit(struct got_object_id **new_commit_id,
8525 struct got_worktree *worktree, struct got_fileindex *fileindex,
8526 const char *author, const char *committer, int allow_bad_symlinks,
8527 struct got_object_id *branch_tip, const char *branch_name,
8528 int allow_conflict, struct got_repository *repo,
8529 got_worktree_status_cb status_cb, void *status_arg)
8532 const struct got_error *err = NULL, *sync_err;
8533 struct got_pathlist_head commitable_paths;
8534 struct collect_commitables_arg cc_arg;
8535 struct got_pathlist_entry *pe;
8536 struct got_reference *head_ref = NULL;
8537 struct got_object_id *head_commit_id = NULL;
8538 int have_staged_files = 0;
8539 struct merge_commit_msg_arg mcm_arg;
8540 char *fileindex_path = NULL;
8542 memset(&cc_arg, 0, sizeof(cc_arg));
8543 *new_commit_id = NULL;
8545 TAILQ_INIT(&commitable_paths);
8547 err = get_fileindex_path(&fileindex_path, worktree);
8551 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
8555 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8559 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
8560 &have_staged_files);
8561 if (err && err->code != GOT_ERR_CANCELLED)
8563 if (have_staged_files) {
8564 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
8568 cc_arg.commitable_paths = &commitable_paths;
8569 cc_arg.worktree = worktree;
8570 cc_arg.fileindex = fileindex;
8572 cc_arg.have_staged_files = have_staged_files;
8573 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
8574 cc_arg.commit_conflicts = allow_conflict;
8575 err = worktree_status(worktree, "", fileindex, repo,
8576 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8580 mcm_arg.worktree = worktree;
8581 mcm_arg.branch_name = branch_name;
8582 err = commit_worktree(new_commit_id, &commitable_paths,
8583 head_commit_id, branch_tip, worktree, author, committer, NULL,
8584 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8588 err = update_fileindex_after_commit(worktree, &commitable_paths,
8589 *new_commit_id, fileindex, have_staged_files);
8590 sync_err = sync_fileindex(fileindex, fileindex_path);
8591 if (sync_err && err == NULL)
8594 TAILQ_FOREACH(pe, &commitable_paths, entry) {
8595 struct got_commitable *ct = pe->data;
8597 free_commitable(ct);
8599 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8600 free(fileindex_path);
8604 const struct got_error *
8605 got_worktree_merge_complete(struct got_worktree *worktree,
8606 struct got_fileindex *fileindex, struct got_repository *repo)
8608 const struct got_error *err, *unlockerr, *sync_err;
8609 char *fileindex_path = NULL;
8611 err = delete_merge_refs(worktree, repo);
8615 err = get_fileindex_path(&fileindex_path, worktree);
8618 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8619 sync_err = sync_fileindex(fileindex, fileindex_path);
8620 if (sync_err && err == NULL)
8623 got_fileindex_free(fileindex);
8624 free(fileindex_path);
8625 unlockerr = lock_worktree(worktree, LOCK_SH);
8626 if (unlockerr && err == NULL)
8631 const struct got_error *
8632 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8633 struct got_repository *repo)
8635 const struct got_error *err;
8636 char *branch_refname = NULL;
8637 struct got_reference *branch_ref = NULL;
8641 err = get_merge_branch_ref_name(&branch_refname, worktree);
8644 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8645 free(branch_refname);
8647 if (err->code != GOT_ERR_NOT_REF)
8655 const struct got_error *got_worktree_merge_prepare(
8656 struct got_fileindex **fileindex, struct got_worktree *worktree,
8657 struct got_repository *repo)
8659 const struct got_error *err = NULL;
8660 char *fileindex_path = NULL;
8661 struct got_reference *wt_branch = NULL;
8662 struct got_object_id *wt_branch_tip = NULL;
8663 struct check_rebase_ok_arg ok_arg;
8667 err = lock_worktree(worktree, LOCK_EX);
8671 err = open_fileindex(fileindex, &fileindex_path, worktree);
8675 /* Preconditions are the same as for rebase. */
8676 ok_arg.worktree = worktree;
8678 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8683 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8688 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8692 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8693 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8698 free(fileindex_path);
8700 got_ref_close(wt_branch);
8701 free(wt_branch_tip);
8704 got_fileindex_free(*fileindex);
8707 lock_worktree(worktree, LOCK_SH);
8712 const struct got_error *got_worktree_merge_write_refs(
8713 struct got_worktree *worktree, struct got_reference *branch,
8714 struct got_repository *repo)
8716 const struct got_error *err = NULL;
8717 char *branch_refname = NULL, *commit_refname = NULL;
8718 struct got_reference *branch_ref = NULL, *commit_ref = NULL;
8719 struct got_object_id *branch_tip = NULL;
8721 err = get_merge_branch_ref_name(&branch_refname, worktree);
8725 err = get_merge_commit_ref_name(&commit_refname, worktree);
8729 err = got_ref_resolve(&branch_tip, repo, branch);
8733 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8736 err = got_ref_write(branch_ref, repo);
8740 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8743 err = got_ref_write(commit_ref, repo);
8748 free(branch_refname);
8749 free(commit_refname);
8751 got_ref_close(branch_ref);
8753 got_ref_close(commit_ref);
8758 const struct got_error *
8759 got_worktree_merge_continue(char **branch_name,
8760 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8761 struct got_worktree *worktree, struct got_repository *repo)
8763 const struct got_error *err;
8764 char *commit_refname = NULL, *branch_refname = NULL;
8765 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8766 char *fileindex_path = NULL;
8767 int have_staged_files = 0;
8769 *branch_name = NULL;
8773 err = lock_worktree(worktree, LOCK_EX);
8777 err = open_fileindex(fileindex, &fileindex_path, worktree);
8781 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8782 &have_staged_files);
8783 if (err && err->code != GOT_ERR_CANCELLED)
8785 if (have_staged_files) {
8786 err = got_error(GOT_ERR_STAGED_PATHS);
8790 err = get_merge_branch_ref_name(&branch_refname, worktree);
8794 err = get_merge_commit_ref_name(&commit_refname, worktree);
8798 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8802 if (!got_ref_is_symbolic(branch_ref)) {
8803 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8804 "%s is not a symbolic reference",
8805 got_ref_get_name(branch_ref));
8808 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8809 if (*branch_name == NULL) {
8810 err = got_error_from_errno("strdup");
8814 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8818 err = got_ref_resolve(branch_tip, repo, commit_ref);
8822 free(commit_refname);
8823 free(branch_refname);
8824 free(fileindex_path);
8826 got_ref_close(commit_ref);
8828 got_ref_close(branch_ref);
8832 *branch_name = NULL;
8837 got_fileindex_free(*fileindex);
8840 lock_worktree(worktree, LOCK_SH);
8845 const struct got_error *
8846 got_worktree_merge_abort(struct got_worktree *worktree,
8847 struct got_fileindex *fileindex, struct got_repository *repo,
8848 got_worktree_checkout_cb progress_cb, void *progress_arg)
8850 const struct got_error *err, *unlockerr, *sync_err;
8851 struct got_commit_object *commit = NULL;
8852 char *fileindex_path = NULL;
8853 struct revert_file_args rfa;
8854 char *commit_ref_name = NULL;
8855 struct got_reference *commit_ref = NULL;
8856 struct got_object_id *merged_commit_id = NULL;
8857 struct got_object_id *tree_id = NULL;
8858 struct got_pathlist_head added_paths;
8860 TAILQ_INIT(&added_paths);
8862 err = get_merge_commit_ref_name(&commit_ref_name, worktree);
8866 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8870 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8875 * Determine which files in added status can be safely removed
8876 * from disk while reverting changes in the work tree.
8877 * We want to avoid deleting unrelated files which were added by
8878 * the user for conflict resolution purposes.
8880 err = get_paths_added_between_commits(&added_paths,
8881 got_worktree_get_base_commit_id(worktree), merged_commit_id,
8882 got_worktree_get_path_prefix(worktree), repo);
8887 err = got_object_open_as_commit(&commit, repo,
8888 worktree->base_commit_id);
8892 err = got_object_id_by_path(&tree_id, repo, commit,
8893 worktree->path_prefix);
8897 err = delete_merge_refs(worktree, repo);
8901 err = get_fileindex_path(&fileindex_path, worktree);
8905 rfa.worktree = worktree;
8906 rfa.fileindex = fileindex;
8907 rfa.progress_cb = progress_cb;
8908 rfa.progress_arg = progress_arg;
8909 rfa.patch_cb = NULL;
8910 rfa.patch_arg = NULL;
8912 rfa.unlink_added_files = 1;
8913 rfa.added_files_to_unlink = &added_paths;
8914 err = worktree_status(worktree, "", fileindex, repo,
8915 revert_file, &rfa, NULL, NULL, 1, 0);
8919 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8920 repo, progress_cb, progress_arg, NULL, NULL);
8922 sync_err = sync_fileindex(fileindex, fileindex_path);
8923 if (sync_err && err == NULL)
8927 free(merged_commit_id);
8929 got_object_commit_close(commit);
8931 got_fileindex_free(fileindex);
8932 free(fileindex_path);
8934 got_ref_close(commit_ref);
8935 free(commit_ref_name);
8937 unlockerr = lock_worktree(worktree, LOCK_SH);
8938 if (unlockerr && err == NULL)
8943 struct check_stage_ok_arg {
8944 struct got_object_id *head_commit_id;
8945 struct got_worktree *worktree;
8946 struct got_fileindex *fileindex;
8947 struct got_repository *repo;
8951 static const struct got_error *
8952 check_stage_ok(void *arg, unsigned char status,
8953 unsigned char staged_status, const char *relpath,
8954 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8955 struct got_object_id *commit_id, int dirfd, const char *de_name)
8957 struct check_stage_ok_arg *a = arg;
8958 const struct got_error *err = NULL;
8959 struct got_fileindex_entry *ie;
8960 struct got_object_id base_commit_id;
8961 struct got_object_id *base_commit_idp = NULL;
8962 char *in_repo_path = NULL, *p;
8964 if (status == GOT_STATUS_UNVERSIONED ||
8965 status == GOT_STATUS_NO_CHANGE)
8967 if (status == GOT_STATUS_NONEXISTENT)
8968 return got_error_set_errno(ENOENT, relpath);
8970 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8972 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8974 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8975 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8977 return got_error_from_errno("asprintf");
8979 if (got_fileindex_entry_has_commit(ie)) {
8980 base_commit_idp = got_fileindex_entry_get_commit_id(
8981 &base_commit_id, ie);
8984 if (status == GOT_STATUS_CONFLICT) {
8985 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8987 } else if (status != GOT_STATUS_ADD &&
8988 status != GOT_STATUS_MODIFY &&
8989 status != GOT_STATUS_DELETE) {
8990 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8994 a->have_changes = 1;
8999 err = check_out_of_date(p, status, staged_status,
9000 blob_id, base_commit_idp, a->head_commit_id, a->repo,
9001 GOT_ERR_STAGE_OUT_OF_DATE);
9007 struct stage_path_arg {
9008 struct got_worktree *worktree;
9009 struct got_fileindex *fileindex;
9010 struct got_repository *repo;
9011 got_worktree_status_cb status_cb;
9013 got_worktree_patch_cb patch_cb;
9015 int staged_something;
9016 int allow_bad_symlinks;
9019 static const struct got_error *
9020 stage_path(void *arg, unsigned char status,
9021 unsigned char staged_status, const char *relpath,
9022 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9023 struct got_object_id *commit_id, int dirfd, const char *de_name)
9025 struct stage_path_arg *a = arg;
9026 const struct got_error *err = NULL;
9027 struct got_fileindex_entry *ie;
9028 char *ondisk_path = NULL, *path_content = NULL;
9030 struct got_object_id *new_staged_blob_id = NULL;
9033 if (status == GOT_STATUS_UNVERSIONED)
9036 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9038 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9040 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
9042 return got_error_from_errno("asprintf");
9045 case GOT_STATUS_ADD:
9046 case GOT_STATUS_MODIFY:
9047 /* XXX could sb.st_mode be passed in by our caller? */
9048 if (lstat(ondisk_path, &sb) == -1) {
9049 err = got_error_from_errno2("lstat", ondisk_path);
9053 if (status == GOT_STATUS_ADD) {
9054 int choice = GOT_PATCH_CHOICE_NONE;
9055 err = (*a->patch_cb)(&choice, a->patch_arg,
9056 status, ie->path, NULL, 1, 1);
9059 if (choice != GOT_PATCH_CHOICE_YES)
9062 err = create_patched_content(&path_content, 0,
9063 staged_blob_id ? staged_blob_id : blob_id,
9064 ondisk_path, dirfd, de_name, ie->path,
9065 a->repo, a->patch_cb, a->patch_arg);
9066 if (err || path_content == NULL)
9070 err = got_object_blob_create(&new_staged_blob_id,
9071 path_content ? path_content : ondisk_path, a->repo);
9074 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9075 SHA1_DIGEST_LENGTH);
9076 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
9077 stage = GOT_FILEIDX_STAGE_ADD;
9079 stage = GOT_FILEIDX_STAGE_MODIFY;
9080 got_fileindex_entry_stage_set(ie, stage);
9081 if (S_ISLNK(sb.st_mode)) {
9082 int is_bad_symlink = 0;
9083 if (!a->allow_bad_symlinks) {
9084 char target_path[PATH_MAX];
9086 target_len = readlink(ondisk_path, target_path,
9087 sizeof(target_path));
9088 if (target_len == -1) {
9089 err = got_error_from_errno2("readlink",
9093 err = is_bad_symlink_target(&is_bad_symlink,
9094 target_path, target_len, ondisk_path,
9095 a->worktree->root_path,
9096 a->worktree->meta_dir);
9099 if (is_bad_symlink) {
9100 err = got_error_path(ondisk_path,
9101 GOT_ERR_BAD_SYMLINK);
9106 got_fileindex_entry_staged_filetype_set(ie,
9107 GOT_FILEIDX_MODE_BAD_SYMLINK);
9109 got_fileindex_entry_staged_filetype_set(ie,
9110 GOT_FILEIDX_MODE_SYMLINK);
9112 got_fileindex_entry_staged_filetype_set(ie,
9113 GOT_FILEIDX_MODE_REGULAR_FILE);
9115 a->staged_something = 1;
9116 if (a->status_cb == NULL)
9118 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
9119 get_staged_status(ie), relpath, blob_id,
9120 new_staged_blob_id, NULL, dirfd, de_name);
9124 * When staging the reverse of the staged diff,
9125 * implicitly unstage the file.
9127 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
9128 sizeof(ie->blob_sha1)) == 0) {
9129 got_fileindex_entry_stage_set(ie,
9130 GOT_FILEIDX_STAGE_NONE);
9133 case GOT_STATUS_DELETE:
9134 if (staged_status == GOT_STATUS_DELETE)
9137 int choice = GOT_PATCH_CHOICE_NONE;
9138 err = (*a->patch_cb)(&choice, a->patch_arg, status,
9139 ie->path, NULL, 1, 1);
9142 if (choice == GOT_PATCH_CHOICE_NO)
9144 if (choice != GOT_PATCH_CHOICE_YES) {
9145 err = got_error(GOT_ERR_PATCH_CHOICE);
9149 stage = GOT_FILEIDX_STAGE_DELETE;
9150 got_fileindex_entry_stage_set(ie, stage);
9151 a->staged_something = 1;
9152 if (a->status_cb == NULL)
9154 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
9155 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
9158 case GOT_STATUS_NO_CHANGE:
9160 case GOT_STATUS_CONFLICT:
9161 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
9163 case GOT_STATUS_NONEXISTENT:
9164 err = got_error_set_errno(ENOENT, relpath);
9167 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
9171 if (path_content && unlink(path_content) == -1 && err == NULL)
9172 err = got_error_from_errno2("unlink", path_content);
9175 free(new_staged_blob_id);
9179 const struct got_error *
9180 got_worktree_stage(struct got_worktree *worktree,
9181 struct got_pathlist_head *paths,
9182 got_worktree_status_cb status_cb, void *status_arg,
9183 got_worktree_patch_cb patch_cb, void *patch_arg,
9184 int allow_bad_symlinks, struct got_repository *repo)
9186 const struct got_error *err = NULL, *sync_err, *unlockerr;
9187 struct got_pathlist_entry *pe;
9188 struct got_fileindex *fileindex = NULL;
9189 char *fileindex_path = NULL;
9190 struct got_reference *head_ref = NULL;
9191 struct got_object_id *head_commit_id = NULL;
9192 struct check_stage_ok_arg oka;
9193 struct stage_path_arg spa;
9195 err = lock_worktree(worktree, LOCK_EX);
9199 err = got_ref_open(&head_ref, repo,
9200 got_worktree_get_head_ref_name(worktree), 0);
9203 err = got_ref_resolve(&head_commit_id, repo, head_ref);
9206 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9210 /* Check pre-conditions before staging anything. */
9211 oka.head_commit_id = head_commit_id;
9212 oka.worktree = worktree;
9213 oka.fileindex = fileindex;
9215 oka.have_changes = 0;
9216 TAILQ_FOREACH(pe, paths, entry) {
9217 err = worktree_status(worktree, pe->path, fileindex, repo,
9218 check_stage_ok, &oka, NULL, NULL, 1, 0);
9222 if (!oka.have_changes) {
9223 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9227 spa.worktree = worktree;
9228 spa.fileindex = fileindex;
9230 spa.patch_cb = patch_cb;
9231 spa.patch_arg = patch_arg;
9232 spa.status_cb = status_cb;
9233 spa.status_arg = status_arg;
9234 spa.staged_something = 0;
9235 spa.allow_bad_symlinks = allow_bad_symlinks;
9236 TAILQ_FOREACH(pe, paths, entry) {
9237 err = worktree_status(worktree, pe->path, fileindex, repo,
9238 stage_path, &spa, NULL, NULL, 1, 0);
9242 if (!spa.staged_something) {
9243 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9247 sync_err = sync_fileindex(fileindex, fileindex_path);
9248 if (sync_err && err == NULL)
9252 got_ref_close(head_ref);
9253 free(head_commit_id);
9254 free(fileindex_path);
9256 got_fileindex_free(fileindex);
9257 unlockerr = lock_worktree(worktree, LOCK_SH);
9258 if (unlockerr && err == NULL)
9263 struct unstage_path_arg {
9264 struct got_worktree *worktree;
9265 struct got_fileindex *fileindex;
9266 struct got_repository *repo;
9267 got_worktree_checkout_cb progress_cb;
9269 got_worktree_patch_cb patch_cb;
9273 static const struct got_error *
9274 create_unstaged_content(char **path_unstaged_content,
9275 char **path_new_staged_content, struct got_object_id *blob_id,
9276 struct got_object_id *staged_blob_id, const char *relpath,
9277 struct got_repository *repo,
9278 got_worktree_patch_cb patch_cb, void *patch_arg)
9280 const struct got_error *err, *free_err;
9281 struct got_blob_object *blob = NULL, *staged_blob = NULL;
9282 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
9283 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
9284 struct got_diffreg_result *diffreg_result = NULL;
9285 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
9286 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
9287 int fd1 = -1, fd2 = -1;
9289 *path_unstaged_content = NULL;
9290 *path_new_staged_content = NULL;
9292 err = got_object_id_str(&label1, blob_id);
9296 fd1 = got_opentempfd();
9298 err = got_error_from_errno("got_opentempfd");
9301 fd2 = got_opentempfd();
9303 err = got_error_from_errno("got_opentempfd");
9307 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
9311 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
9315 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
9319 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
9324 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
9328 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
9332 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
9333 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
9337 err = got_opentemp_named(path_unstaged_content, &outfile,
9338 "got-unstaged-content", "");
9341 err = got_opentemp_named(path_new_staged_content, &rejectfile,
9342 "got-new-staged-content", "");
9346 if (fseek(f1, 0L, SEEK_SET) == -1) {
9347 err = got_ferror(f1, GOT_ERR_IO);
9350 if (fseek(f2, 0L, SEEK_SET) == -1) {
9351 err = got_ferror(f2, GOT_ERR_IO);
9354 /* Count the number of actual changes in the diff result. */
9355 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9356 struct diff_chunk_context cc = {};
9357 diff_chunk_context_load_change(&cc, &nchunks_used,
9358 diffreg_result->result, n, 0);
9361 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9363 err = apply_or_reject_change(&choice, &nchunks_used,
9364 diffreg_result->result, n, relpath, f1, f2,
9365 &line_cur1, &line_cur2,
9366 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
9369 if (choice == GOT_PATCH_CHOICE_YES)
9372 have_rejected_content = 1;
9373 if (choice == GOT_PATCH_CHOICE_QUIT)
9376 if (have_content || have_rejected_content)
9377 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
9378 outfile, rejectfile);
9381 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9382 err = got_error_from_errno("close");
9384 got_object_blob_close(blob);
9385 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9386 err = got_error_from_errno("close");
9388 got_object_blob_close(staged_blob);
9389 free_err = got_diffreg_result_free(diffreg_result);
9390 if (free_err && err == NULL)
9392 if (f1 && fclose(f1) == EOF && err == NULL)
9393 err = got_error_from_errno2("fclose", path1);
9394 if (f2 && fclose(f2) == EOF && err == NULL)
9395 err = got_error_from_errno2("fclose", path2);
9396 if (outfile && fclose(outfile) == EOF && err == NULL)
9397 err = got_error_from_errno2("fclose", *path_unstaged_content);
9398 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
9399 err = got_error_from_errno2("fclose", *path_new_staged_content);
9400 if (path1 && unlink(path1) == -1 && err == NULL)
9401 err = got_error_from_errno2("unlink", path1);
9402 if (path2 && unlink(path2) == -1 && err == NULL)
9403 err = got_error_from_errno2("unlink", path2);
9404 if (err || !have_content) {
9405 if (*path_unstaged_content &&
9406 unlink(*path_unstaged_content) == -1 && err == NULL)
9407 err = got_error_from_errno2("unlink",
9408 *path_unstaged_content);
9409 free(*path_unstaged_content);
9410 *path_unstaged_content = NULL;
9412 if (err || !have_content || !have_rejected_content) {
9413 if (*path_new_staged_content &&
9414 unlink(*path_new_staged_content) == -1 && err == NULL)
9415 err = got_error_from_errno2("unlink",
9416 *path_new_staged_content);
9417 free(*path_new_staged_content);
9418 *path_new_staged_content = NULL;
9425 static const struct got_error *
9426 unstage_hunks(struct got_object_id *staged_blob_id,
9427 struct got_blob_object *blob_base,
9428 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
9429 const char *ondisk_path, const char *label_orig,
9430 struct got_worktree *worktree, struct got_repository *repo,
9431 got_worktree_patch_cb patch_cb, void *patch_arg,
9432 got_worktree_checkout_cb progress_cb, void *progress_arg)
9434 const struct got_error *err = NULL;
9435 char *path_unstaged_content = NULL;
9436 char *path_new_staged_content = NULL;
9437 char *parent = NULL, *base_path = NULL;
9438 char *blob_base_path = NULL;
9439 struct got_object_id *new_staged_blob_id = NULL;
9440 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
9443 err = create_unstaged_content(&path_unstaged_content,
9444 &path_new_staged_content, blob_id, staged_blob_id,
9445 ie->path, repo, patch_cb, patch_arg);
9449 if (path_unstaged_content == NULL)
9452 if (path_new_staged_content) {
9453 err = got_object_blob_create(&new_staged_blob_id,
9454 path_new_staged_content, repo);
9459 f = fopen(path_unstaged_content, "re");
9461 err = got_error_from_errno2("fopen",
9462 path_unstaged_content);
9465 if (fstat(fileno(f), &sb) == -1) {
9466 err = got_error_from_errno2("fstat", path_unstaged_content);
9469 if (got_fileindex_entry_staged_filetype_get(ie) ==
9470 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
9471 char link_target[PATH_MAX];
9473 r = fread(link_target, 1, sizeof(link_target), f);
9474 if (r == 0 && ferror(f)) {
9475 err = got_error_from_errno("fread");
9478 if (r >= sizeof(link_target)) { /* should not happen */
9479 err = got_error(GOT_ERR_NO_SPACE);
9482 link_target[r] = '\0';
9483 err = merge_symlink(worktree, blob_base,
9484 ondisk_path, ie->path, label_orig, link_target,
9485 worktree->base_commit_id, repo, progress_cb,
9488 int local_changes_subsumed;
9490 err = got_path_dirname(&parent, ondisk_path);
9494 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
9496 err = got_error_from_errno("asprintf");
9501 err = got_opentemp_named(&blob_base_path, &f_base,
9505 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
9511 * In order the run a 3-way merge with a symlink we copy the symlink's
9512 * target path into a temporary file and use that file with diff3.
9514 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9515 err = dump_symlink_target_path_to_file(&f_deriv2,
9521 fd = open(ondisk_path,
9522 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
9524 err = got_error_from_errno2("open", ondisk_path);
9527 f_deriv2 = fdopen(fd, "r");
9528 if (f_deriv2 == NULL) {
9529 err = got_error_from_errno2("fdopen", ondisk_path);
9535 err = merge_file(&local_changes_subsumed, worktree,
9536 f_base, f, f_deriv2, ondisk_path, ie->path,
9537 got_fileindex_perms_to_st(ie),
9538 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
9539 repo, progress_cb, progress_arg);
9544 if (new_staged_blob_id) {
9545 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9546 SHA1_DIGEST_LENGTH);
9548 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9549 got_fileindex_entry_staged_filetype_set(ie, 0);
9552 free(new_staged_blob_id);
9553 if (path_unstaged_content &&
9554 unlink(path_unstaged_content) == -1 && err == NULL)
9555 err = got_error_from_errno2("unlink", path_unstaged_content);
9556 if (path_new_staged_content &&
9557 unlink(path_new_staged_content) == -1 && err == NULL)
9558 err = got_error_from_errno2("unlink", path_new_staged_content);
9559 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
9560 err = got_error_from_errno2("unlink", blob_base_path);
9561 if (f_base && fclose(f_base) == EOF && err == NULL)
9562 err = got_error_from_errno2("fclose", path_unstaged_content);
9563 if (f && fclose(f) == EOF && err == NULL)
9564 err = got_error_from_errno2("fclose", path_unstaged_content);
9565 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
9566 err = got_error_from_errno2("fclose", ondisk_path);
9567 free(path_unstaged_content);
9568 free(path_new_staged_content);
9569 free(blob_base_path);
9575 static const struct got_error *
9576 unstage_path(void *arg, unsigned char status,
9577 unsigned char staged_status, const char *relpath,
9578 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9579 struct got_object_id *commit_id, int dirfd, const char *de_name)
9581 const struct got_error *err = NULL;
9582 struct unstage_path_arg *a = arg;
9583 struct got_fileindex_entry *ie;
9584 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
9585 char *ondisk_path = NULL;
9586 char *id_str = NULL, *label_orig = NULL;
9587 int local_changes_subsumed;
9589 int fd1 = -1, fd2 = -1;
9591 if (staged_status != GOT_STATUS_ADD &&
9592 staged_status != GOT_STATUS_MODIFY &&
9593 staged_status != GOT_STATUS_DELETE)
9596 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9598 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9600 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
9602 return got_error_from_errno("asprintf");
9604 err = got_object_id_str(&id_str,
9605 commit_id ? commit_id : a->worktree->base_commit_id);
9608 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9610 err = got_error_from_errno("asprintf");
9614 fd1 = got_opentempfd();
9616 err = got_error_from_errno("got_opentempfd");
9619 fd2 = got_opentempfd();
9621 err = got_error_from_errno("got_opentempfd");
9625 switch (staged_status) {
9626 case GOT_STATUS_MODIFY:
9627 err = got_object_open_as_blob(&blob_base, a->repo,
9628 blob_id, 8192, fd1);
9632 case GOT_STATUS_ADD:
9634 if (staged_status == GOT_STATUS_ADD) {
9635 int choice = GOT_PATCH_CHOICE_NONE;
9636 err = (*a->patch_cb)(&choice, a->patch_arg,
9637 staged_status, ie->path, NULL, 1, 1);
9640 if (choice != GOT_PATCH_CHOICE_YES)
9643 err = unstage_hunks(staged_blob_id,
9644 blob_base, blob_id, ie, ondisk_path,
9645 label_orig, a->worktree, a->repo,
9646 a->patch_cb, a->patch_arg,
9647 a->progress_cb, a->progress_arg);
9648 break; /* Done with this file. */
9651 err = got_object_open_as_blob(&blob_staged, a->repo,
9652 staged_blob_id, 8192, fd2);
9655 switch (got_fileindex_entry_staged_filetype_get(ie)) {
9656 case GOT_FILEIDX_MODE_BAD_SYMLINK:
9657 case GOT_FILEIDX_MODE_REGULAR_FILE:
9658 err = merge_blob(&local_changes_subsumed, a->worktree,
9659 blob_base, ondisk_path, relpath,
9660 got_fileindex_perms_to_st(ie), label_orig,
9661 blob_staged, commit_id ? commit_id :
9662 a->worktree->base_commit_id, a->repo,
9663 a->progress_cb, a->progress_arg);
9665 case GOT_FILEIDX_MODE_SYMLINK:
9666 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9667 char *staged_target;
9668 err = got_object_blob_read_to_str(
9669 &staged_target, blob_staged);
9672 err = merge_symlink(a->worktree, blob_base,
9673 ondisk_path, relpath, label_orig,
9674 staged_target, commit_id ? commit_id :
9675 a->worktree->base_commit_id,
9676 a->repo, a->progress_cb, a->progress_arg);
9677 free(staged_target);
9679 err = merge_blob(&local_changes_subsumed,
9680 a->worktree, blob_base, ondisk_path,
9681 relpath, got_fileindex_perms_to_st(ie),
9682 label_orig, blob_staged,
9683 commit_id ? commit_id :
9684 a->worktree->base_commit_id, a->repo,
9685 a->progress_cb, a->progress_arg);
9689 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9693 got_fileindex_entry_stage_set(ie,
9694 GOT_FILEIDX_STAGE_NONE);
9695 got_fileindex_entry_staged_filetype_set(ie, 0);
9698 case GOT_STATUS_DELETE:
9700 int choice = GOT_PATCH_CHOICE_NONE;
9701 err = (*a->patch_cb)(&choice, a->patch_arg,
9702 staged_status, ie->path, NULL, 1, 1);
9705 if (choice == GOT_PATCH_CHOICE_NO)
9707 if (choice != GOT_PATCH_CHOICE_YES) {
9708 err = got_error(GOT_ERR_PATCH_CHOICE);
9712 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9713 got_fileindex_entry_staged_filetype_set(ie, 0);
9714 err = get_file_status(&status, &sb, ie, ondisk_path,
9715 dirfd, de_name, a->repo);
9718 err = (*a->progress_cb)(a->progress_arg, status, relpath);
9723 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9724 err = got_error_from_errno("close");
9726 got_object_blob_close(blob_base);
9727 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9728 err = got_error_from_errno("close");
9730 got_object_blob_close(blob_staged);
9736 const struct got_error *
9737 got_worktree_unstage(struct got_worktree *worktree,
9738 struct got_pathlist_head *paths,
9739 got_worktree_checkout_cb progress_cb, void *progress_arg,
9740 got_worktree_patch_cb patch_cb, void *patch_arg,
9741 struct got_repository *repo)
9743 const struct got_error *err = NULL, *sync_err, *unlockerr;
9744 struct got_pathlist_entry *pe;
9745 struct got_fileindex *fileindex = NULL;
9746 char *fileindex_path = NULL;
9747 struct unstage_path_arg upa;
9749 err = lock_worktree(worktree, LOCK_EX);
9753 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9757 upa.worktree = worktree;
9758 upa.fileindex = fileindex;
9760 upa.progress_cb = progress_cb;
9761 upa.progress_arg = progress_arg;
9762 upa.patch_cb = patch_cb;
9763 upa.patch_arg = patch_arg;
9764 TAILQ_FOREACH(pe, paths, entry) {
9765 err = worktree_status(worktree, pe->path, fileindex, repo,
9766 unstage_path, &upa, NULL, NULL, 1, 0);
9771 sync_err = sync_fileindex(fileindex, fileindex_path);
9772 if (sync_err && err == NULL)
9775 free(fileindex_path);
9777 got_fileindex_free(fileindex);
9778 unlockerr = lock_worktree(worktree, LOCK_SH);
9779 if (unlockerr && err == NULL)
9784 struct report_file_info_arg {
9785 struct got_worktree *worktree;
9786 got_worktree_path_info_cb info_cb;
9788 struct got_pathlist_head *paths;
9789 got_cancel_cb cancel_cb;
9793 static const struct got_error *
9794 report_file_info(void *arg, struct got_fileindex_entry *ie)
9796 const struct got_error *err;
9797 struct report_file_info_arg *a = arg;
9798 struct got_pathlist_entry *pe;
9799 struct got_object_id blob_id, staged_blob_id, commit_id;
9800 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9801 struct got_object_id *commit_idp = NULL;
9805 err = a->cancel_cb(a->cancel_arg);
9810 TAILQ_FOREACH(pe, a->paths, entry) {
9811 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9812 got_path_is_child(ie->path, pe->path, pe->path_len))
9815 if (pe == NULL) /* not found */
9818 if (got_fileindex_entry_has_blob(ie))
9819 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9820 stage = got_fileindex_entry_stage_get(ie);
9821 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9822 stage == GOT_FILEIDX_STAGE_ADD) {
9823 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9824 &staged_blob_id, ie);
9827 if (got_fileindex_entry_has_commit(ie))
9828 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9830 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9831 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9834 const struct got_error *
9835 got_worktree_path_info(struct got_worktree *worktree,
9836 struct got_pathlist_head *paths,
9837 got_worktree_path_info_cb info_cb, void *info_arg,
9838 got_cancel_cb cancel_cb, void *cancel_arg)
9841 const struct got_error *err = NULL, *unlockerr;
9842 struct got_fileindex *fileindex = NULL;
9843 char *fileindex_path = NULL;
9844 struct report_file_info_arg arg;
9846 err = lock_worktree(worktree, LOCK_SH);
9850 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9854 arg.worktree = worktree;
9855 arg.info_cb = info_cb;
9856 arg.info_arg = info_arg;
9858 arg.cancel_cb = cancel_cb;
9859 arg.cancel_arg = cancel_arg;
9860 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9863 free(fileindex_path);
9865 got_fileindex_free(fileindex);
9866 unlockerr = lock_worktree(worktree, LOCK_UN);
9867 if (unlockerr && err == NULL)
9872 static const struct got_error *
9873 patch_check_path(const char *p, char **path, unsigned char *status,
9874 unsigned char *staged_status, struct got_fileindex *fileindex,
9875 struct got_worktree *worktree, struct got_repository *repo)
9877 const struct got_error *err;
9878 struct got_fileindex_entry *ie;
9880 char *ondisk_path = NULL;
9882 err = got_worktree_resolve_path(path, worktree, p);
9886 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9887 *path[0] ? "/" : "", *path) == -1)
9888 return got_error_from_errno("asprintf");
9890 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9892 *staged_status = get_staged_status(ie);
9893 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9898 *staged_status = GOT_STATUS_NO_CHANGE;
9899 *status = GOT_STATUS_UNVERSIONED;
9900 if (lstat(ondisk_path, &sb) == -1) {
9901 if (errno != ENOENT) {
9902 err = got_error_from_errno2("lstat",
9906 *status = GOT_STATUS_NONEXISTENT;
9915 static const struct got_error *
9916 patch_can_rm(const char *path, unsigned char status,
9917 unsigned char staged_status)
9919 if (status == GOT_STATUS_NONEXISTENT)
9920 return got_error_set_errno(ENOENT, path);
9921 if (status != GOT_STATUS_NO_CHANGE &&
9922 status != GOT_STATUS_ADD &&
9923 status != GOT_STATUS_MODIFY &&
9924 status != GOT_STATUS_MODE_CHANGE)
9925 return got_error_path(path, GOT_ERR_FILE_STATUS);
9926 if (staged_status == GOT_STATUS_DELETE)
9927 return got_error_path(path, GOT_ERR_FILE_STATUS);
9931 static const struct got_error *
9932 patch_can_add(const char *path, unsigned char status)
9934 if (status != GOT_STATUS_NONEXISTENT)
9935 return got_error_path(path, GOT_ERR_FILE_STATUS);
9939 static const struct got_error *
9940 patch_can_edit(const char *path, unsigned char status,
9941 unsigned char staged_status)
9943 if (status == GOT_STATUS_NONEXISTENT)
9944 return got_error_set_errno(ENOENT, path);
9945 if (status != GOT_STATUS_NO_CHANGE &&
9946 status != GOT_STATUS_ADD &&
9947 status != GOT_STATUS_MODIFY)
9948 return got_error_path(path, GOT_ERR_FILE_STATUS);
9949 if (staged_status == GOT_STATUS_DELETE)
9950 return got_error_path(path, GOT_ERR_FILE_STATUS);
9954 const struct got_error *
9955 got_worktree_patch_prepare(struct got_fileindex **fileindex,
9956 char **fileindex_path, struct got_worktree *worktree)
9958 return open_fileindex(fileindex, fileindex_path, worktree);
9961 const struct got_error *
9962 got_worktree_patch_check_path(const char *old, const char *new,
9963 char **oldpath, char **newpath, struct got_worktree *worktree,
9964 struct got_repository *repo, struct got_fileindex *fileindex)
9966 const struct got_error *err = NULL;
9967 int file_renamed = 0;
9968 unsigned char status_old, staged_status_old;
9969 unsigned char status_new, staged_status_new;
9974 err = patch_check_path(old != NULL ? old : new, oldpath,
9975 &status_old, &staged_status_old, fileindex, worktree, repo);
9979 err = patch_check_path(new != NULL ? new : old, newpath,
9980 &status_new, &staged_status_new, fileindex, worktree, repo);
9984 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9987 if (old != NULL && new == NULL)
9988 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9989 else if (file_renamed) {
9990 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9992 err = patch_can_add(*newpath, status_new);
9993 } else if (old == NULL)
9994 err = patch_can_add(*newpath, status_new);
9996 err = patch_can_edit(*newpath, status_new, staged_status_new);
10008 const struct got_error *
10009 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
10010 struct got_worktree *worktree, struct got_fileindex *fileindex,
10011 got_worktree_checkout_cb progress_cb, void *progress_arg)
10013 struct schedule_addition_args saa;
10015 memset(&saa, 0, sizeof(saa));
10016 saa.worktree = worktree;
10017 saa.fileindex = fileindex;
10018 saa.progress_cb = progress_cb;
10019 saa.progress_arg = progress_arg;
10022 return worktree_status(worktree, path, fileindex, repo,
10023 schedule_addition, &saa, NULL, NULL, 1, 0);
10026 const struct got_error *
10027 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
10028 struct got_worktree *worktree, struct got_fileindex *fileindex,
10029 got_worktree_delete_cb progress_cb, void *progress_arg)
10031 const struct got_error *err;
10032 struct schedule_deletion_args sda;
10033 char *ondisk_status_path;
10035 memset(&sda, 0, sizeof(sda));
10036 sda.worktree = worktree;
10037 sda.fileindex = fileindex;
10038 sda.progress_cb = progress_cb;
10039 sda.progress_arg = progress_arg;
10041 sda.delete_local_mods = 0;
10042 sda.keep_on_disk = 0;
10043 sda.ignore_missing_paths = 0;
10044 sda.status_codes = NULL;
10045 if (asprintf(&ondisk_status_path, "%s/%s",
10046 got_worktree_get_root_path(worktree), path) == -1)
10047 return got_error_from_errno("asprintf");
10048 sda.status_path = ondisk_status_path;
10049 sda.status_path_len = strlen(ondisk_status_path);
10051 err = worktree_status(worktree, path, fileindex, repo,
10052 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
10053 free(ondisk_status_path);
10057 const struct got_error *
10058 got_worktree_patch_complete(struct got_fileindex *fileindex,
10059 const char *fileindex_path)
10061 const struct got_error *err = NULL;
10063 err = sync_fileindex(fileindex, fileindex_path);
10064 got_fileindex_free(fileindex);