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,
1108 int update_timestamps)
1110 const struct got_error *err = NULL;
1111 struct got_fileindex_entry *new_ie;
1115 err = got_fileindex_entry_alloc(&new_ie, path);
1119 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1120 blob_id->sha1, base_commit_id->sha1, update_timestamps);
1124 err = got_fileindex_entry_add(fileindex, new_ie);
1127 got_fileindex_entry_free(new_ie);
1134 get_ondisk_perms(int executable, mode_t st_mode)
1136 mode_t xbits = S_IXUSR;
1139 /* Map read bits to execute bits. */
1140 if (st_mode & S_IRGRP)
1142 if (st_mode & S_IROTH)
1144 return st_mode | xbits;
1150 /* forward declaration */
1151 static const struct got_error *
1152 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1153 const char *path, mode_t te_mode, mode_t st_mode,
1154 struct got_blob_object *blob, int restoring_missing_file,
1155 int reverting_versioned_file, int installing_bad_symlink,
1156 int path_is_unversioned, int *update_timestamps,
1157 struct got_repository *repo,
1158 got_worktree_checkout_cb progress_cb, void *progress_arg);
1161 * This function assumes that the provided symlink target points at a
1162 * safe location in the work tree!
1164 static const struct got_error *
1165 replace_existing_symlink(int *did_something, const char *ondisk_path,
1166 const char *target_path, size_t target_len)
1168 const struct got_error *err = NULL;
1170 char etarget[PATH_MAX];
1176 * "Bad" symlinks (those pointing outside the work tree or into the
1177 * .got directory) are installed in the work tree as a regular file
1178 * which contains the bad symlink target path.
1179 * The new symlink target has already been checked for safety by our
1180 * caller. If we can successfully open a regular file then we simply
1181 * replace this file with a symlink below.
1183 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1185 if (!got_err_open_nofollow_on_symlink())
1186 return got_error_from_errno2("open", ondisk_path);
1188 /* We are updating an existing on-disk symlink. */
1189 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1191 return got_error_from_errno2("readlink", ondisk_path);
1193 if (elen == target_len &&
1194 memcmp(etarget, target_path, target_len) == 0)
1195 return NULL; /* nothing to do */
1199 err = update_symlink(ondisk_path, target_path, target_len);
1200 if (fd != -1 && close(fd) == -1 && err == NULL)
1201 err = got_error_from_errno2("close", ondisk_path);
1205 static const struct got_error *
1206 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1207 size_t target_len, const char *ondisk_path, const char *wtroot_path,
1208 const char *meta_dir)
1210 const struct got_error *err = NULL;
1211 char canonpath[PATH_MAX];
1212 char *path_got = NULL;
1214 *is_bad_symlink = 0;
1216 if (target_len >= sizeof(canonpath)) {
1217 *is_bad_symlink = 1;
1222 * We do not use realpath(3) to resolve the symlink's target
1223 * path because we don't want to resolve symlinks recursively.
1224 * Instead we make the path absolute and then canonicalize it.
1225 * Relative symlink target lookup should begin at the directory
1226 * in which the blob object is being installed.
1228 if (!got_path_is_absolute(target_path)) {
1229 char *abspath, *parent;
1230 err = got_path_dirname(&parent, ondisk_path);
1233 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1235 return got_error_from_errno("asprintf");
1238 if (strlen(abspath) >= sizeof(canonpath)) {
1239 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1243 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1248 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1253 /* Only allow symlinks pointing at paths within the work tree. */
1254 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1255 *is_bad_symlink = 1;
1259 /* Do not allow symlinks pointing into the meta directory. */
1260 if (asprintf(&path_got, "%s/%s", wtroot_path, meta_dir) == -1)
1261 return got_error_from_errno("asprintf");
1262 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1263 *is_bad_symlink = 1;
1269 static const struct got_error *
1270 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1271 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1272 int restoring_missing_file, int reverting_versioned_file,
1273 int path_is_unversioned, int allow_bad_symlinks,
1274 struct got_repository *repo,
1275 got_worktree_checkout_cb progress_cb, void *progress_arg)
1277 const struct got_error *err = NULL;
1278 char target_path[PATH_MAX];
1279 size_t len, target_len = 0;
1280 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1281 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1283 *is_bad_symlink = 0;
1286 * Blob object content specifies the target path of the link.
1287 * If a symbolic link cannot be installed we instead create
1288 * a regular file which contains the link target path stored
1289 * in the blob object.
1292 err = got_object_blob_read_block(&len, blob);
1296 if (len + target_len >= sizeof(target_path)) {
1297 /* Path too long; install as a regular file. */
1298 *is_bad_symlink = 1;
1299 got_object_blob_rewind(blob);
1300 return install_blob(worktree, ondisk_path, path,
1301 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1302 restoring_missing_file, reverting_versioned_file,
1303 1, path_is_unversioned, NULL, repo, progress_cb,
1307 /* Skip blob object header first time around. */
1308 memcpy(target_path + target_len, buf + hdrlen,
1310 target_len += len - hdrlen;
1314 target_path[target_len] = '\0';
1316 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1317 ondisk_path, worktree->root_path, worktree->meta_dir);
1321 if (*is_bad_symlink && !allow_bad_symlinks) {
1322 /* install as a regular file */
1323 got_object_blob_rewind(blob);
1324 err = install_blob(worktree, ondisk_path, path,
1325 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1326 restoring_missing_file, reverting_versioned_file, 1,
1327 path_is_unversioned, NULL, repo,
1328 progress_cb, progress_arg);
1332 if (symlink(target_path, ondisk_path) == -1) {
1333 if (errno == EEXIST) {
1334 int symlink_replaced;
1335 if (path_is_unversioned) {
1336 err = (*progress_cb)(progress_arg,
1337 GOT_STATUS_UNVERSIONED, path);
1340 err = replace_existing_symlink(&symlink_replaced,
1341 ondisk_path, target_path, target_len);
1345 if (symlink_replaced) {
1346 err = (*progress_cb)(progress_arg,
1347 reverting_versioned_file ?
1349 GOT_STATUS_UPDATE, path);
1351 err = (*progress_cb)(progress_arg,
1352 GOT_STATUS_EXISTS, path);
1355 return err; /* Nothing else to do. */
1358 if (errno == ENOENT) {
1360 err = got_path_dirname(&parent, ondisk_path);
1363 err = got_path_mkdir(parent);
1368 * Retry, and fall through to error handling
1369 * below if this second attempt fails.
1371 if (symlink(target_path, ondisk_path) != -1) {
1372 err = NULL; /* success */
1374 err = (*progress_cb)(progress_arg,
1375 reverting_versioned_file ?
1376 GOT_STATUS_REVERT : GOT_STATUS_ADD,
1383 /* Handle errors from first or second creation attempt. */
1384 if (errno == ENAMETOOLONG) {
1385 /* bad target path; install as a regular file */
1386 *is_bad_symlink = 1;
1387 got_object_blob_rewind(blob);
1388 err = install_blob(worktree, ondisk_path, path,
1389 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1390 restoring_missing_file, reverting_versioned_file, 1,
1391 path_is_unversioned, NULL, repo,
1392 progress_cb, progress_arg);
1393 } else if (errno == ENOTDIR) {
1394 err = got_error_path(ondisk_path,
1395 GOT_ERR_FILE_OBSTRUCTED);
1397 err = got_error_from_errno3("symlink",
1398 target_path, ondisk_path);
1400 } else if (progress_cb)
1401 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1402 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1406 static const struct got_error *
1407 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1408 const char *path, mode_t te_mode, mode_t st_mode,
1409 struct got_blob_object *blob, int restoring_missing_file,
1410 int reverting_versioned_file, int installing_bad_symlink,
1411 int path_is_unversioned, int *update_timestamps,
1412 struct got_repository *repo,
1413 got_worktree_checkout_cb progress_cb, void *progress_arg)
1415 const struct got_error *err = NULL;
1419 char *tmppath = NULL;
1422 if (update_timestamps)
1423 *update_timestamps = 1;
1425 mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1426 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1429 if (errno == ENOENT || errno == ENOTDIR) {
1431 err = got_path_dirname(&parent, path);
1434 err = add_dir_on_disk(worktree, parent);
1435 if (err && err->code == GOT_ERR_FILE_OBSTRUCTED)
1436 err = got_error_path(path, err->code);
1440 fd = open(ondisk_path,
1441 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1444 return got_error_from_errno2("open",
1446 } else if (errno == EEXIST) {
1447 if (path_is_unversioned) {
1448 if (update_timestamps)
1449 *update_timestamps = 0;
1450 err = (*progress_cb)(progress_arg,
1451 GOT_STATUS_EXISTS, path);
1454 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1455 !S_ISREG(st_mode) && !installing_bad_symlink) {
1456 /* TODO file is obstructed; do something */
1457 err = got_error_path(ondisk_path,
1458 GOT_ERR_FILE_OBSTRUCTED);
1461 err = got_opentemp_named_fd(&tmppath, &fd,
1467 if (fchmod(fd, apply_umask(mode)) == -1) {
1468 err = got_error_from_errno2("fchmod",
1474 return got_error_from_errno2("open", ondisk_path);
1478 if (restoring_missing_file)
1479 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1481 else if (reverting_versioned_file)
1482 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1485 err = (*progress_cb)(progress_arg,
1486 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1491 hdrlen = got_object_blob_get_hdrlen(blob);
1493 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1494 err = got_object_blob_read_block(&len, blob);
1498 /* Skip blob object header first time around. */
1499 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1501 err = got_error_from_errno("write");
1503 } else if (outlen != len - hdrlen) {
1504 err = got_error(GOT_ERR_IO);
1511 if (fsync(fd) != 0) {
1512 err = got_error_from_errno("fsync");
1517 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1518 err = got_error_from_errno2("unlink", ondisk_path);
1521 if (rename(tmppath, ondisk_path) != 0) {
1522 err = got_error_from_errno3("rename", tmppath,
1531 if (fd != -1 && close(fd) == -1 && err == NULL)
1532 err = got_error_from_errno("close");
1533 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1534 err = got_error_from_errno2("unlink", tmppath);
1540 * Upgrade STATUS_MODIFY to STATUS_CONFLICT if a
1541 * conflict marker is found in newly added lines only.
1543 static const struct got_error *
1544 get_modified_file_content_status(unsigned char *status,
1545 struct got_blob_object *blob, const char *path, struct stat *sb,
1548 const struct got_error *err, *free_err;
1549 const char *markers[3] = {
1550 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1551 GOT_DIFF_CONFLICT_MARKER_SEP,
1552 GOT_DIFF_CONFLICT_MARKER_END
1555 struct got_diffreg_result *diffreg_result = NULL;
1556 struct diff_result *r;
1557 int nchunks_parsed, n, i = 0, ln = 0;
1559 size_t linesize = 0;
1562 if (*status != GOT_STATUS_MODIFY)
1565 f1 = got_opentemp();
1567 return got_error_from_errno("got_opentemp");
1570 got_object_blob_rewind(blob);
1571 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
1576 err = got_diff_files(&diffreg_result, f1, 1, NULL, ondisk_file,
1577 1, NULL, 0, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
1581 r = diffreg_result->result;
1583 for (n = 0; n < r->chunks.len; n += nchunks_parsed) {
1584 struct diff_chunk *c;
1585 struct diff_chunk_context cc = {};
1589 * We can optimise a little by advancing straight
1590 * to the next chunk if this one has no added lines.
1592 c = diff_chunk_get(r, n);
1594 if (diff_chunk_type(c) != CHUNK_PLUS) {
1596 continue; /* removed or unchanged lines */
1599 pos = diff_chunk_get_right_start_pos(c);
1600 if (fseek(ondisk_file, pos, SEEK_SET) == -1) {
1601 err = got_ferror(ondisk_file, GOT_ERR_IO);
1605 diff_chunk_context_load_change(&cc, &nchunks_parsed, r, n, 0);
1606 ln = cc.right.start;
1608 while (ln < cc.right.end) {
1609 linelen = getline(&line, &linesize, ondisk_file);
1610 if (linelen == -1) {
1611 if (feof(ondisk_file))
1613 err = got_ferror(ondisk_file, GOT_ERR_IO);
1617 if (line && strncmp(line, markers[i],
1618 strlen(markers[i])) == 0) {
1619 if (strcmp(markers[i],
1620 GOT_DIFF_CONFLICT_MARKER_END) == 0) {
1621 *status = GOT_STATUS_CONFLICT;
1632 if (f1 != NULL && fclose(f1) == EOF && err == NULL)
1633 err = got_error_from_errno("fclose");
1634 free_err = got_diffreg_result_free(diffreg_result);
1642 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1644 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1645 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1649 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1651 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1652 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1653 ie->mtime_sec == sb->st_mtim.tv_sec &&
1654 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1655 ie->size == (sb->st_size & 0xffffffff) &&
1656 !xbit_differs(ie, sb->st_mode));
1659 static unsigned char
1660 get_staged_status(struct got_fileindex_entry *ie)
1662 switch (got_fileindex_entry_stage_get(ie)) {
1663 case GOT_FILEIDX_STAGE_ADD:
1664 return GOT_STATUS_ADD;
1665 case GOT_FILEIDX_STAGE_DELETE:
1666 return GOT_STATUS_DELETE;
1667 case GOT_FILEIDX_STAGE_MODIFY:
1668 return GOT_STATUS_MODIFY;
1670 return GOT_STATUS_NO_CHANGE;
1674 static const struct got_error *
1675 get_symlink_modification_status(unsigned char *status,
1676 struct got_fileindex_entry *ie, const char *abspath,
1677 int dirfd, const char *de_name, struct got_blob_object *blob)
1679 const struct got_error *err = NULL;
1680 char target_path[PATH_MAX];
1681 char etarget[PATH_MAX];
1683 size_t len, target_len = 0;
1684 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1685 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1687 *status = GOT_STATUS_NO_CHANGE;
1689 /* Blob object content specifies the target path of the link. */
1691 err = got_object_blob_read_block(&len, blob);
1694 if (len + target_len >= sizeof(target_path)) {
1696 * Should not happen. The blob contents were OK
1697 * when this symlink was installed.
1699 return got_error(GOT_ERR_NO_SPACE);
1702 /* Skip blob object header first time around. */
1703 memcpy(target_path + target_len, buf + hdrlen,
1705 target_len += len - hdrlen;
1709 target_path[target_len] = '\0';
1712 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1714 return got_error_from_errno2("readlinkat", abspath);
1716 elen = readlink(abspath, etarget, sizeof(etarget));
1718 return got_error_from_errno2("readlink", abspath);
1721 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1722 *status = GOT_STATUS_MODIFY;
1727 static const struct got_error *
1728 get_file_status(unsigned char *status, struct stat *sb,
1729 struct got_fileindex_entry *ie, const char *abspath,
1730 int dirfd, const char *de_name, struct got_repository *repo)
1732 const struct got_error *err = NULL;
1733 struct got_object_id id;
1735 int fd = -1, fd1 = -1;
1738 struct got_blob_object *blob = NULL;
1740 unsigned char staged_status;
1742 staged_status = get_staged_status(ie);
1743 *status = GOT_STATUS_NO_CHANGE;
1744 memset(sb, 0, sizeof(*sb));
1747 * Whenever the caller provides a directory descriptor and a
1748 * directory entry name for the file, use them! This prevents
1749 * race conditions if filesystem paths change beneath our feet.
1752 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1753 if (errno == ENOENT) {
1754 if (got_fileindex_entry_has_file_on_disk(ie))
1755 *status = GOT_STATUS_MISSING;
1757 *status = GOT_STATUS_DELETE;
1760 err = got_error_from_errno2("fstatat", abspath);
1764 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1765 if (fd == -1 && errno != ENOENT &&
1766 !got_err_open_nofollow_on_symlink())
1767 return got_error_from_errno2("open", abspath);
1768 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1769 if (lstat(abspath, sb) == -1)
1770 return got_error_from_errno2("lstat", abspath);
1771 } else if (fd == -1 || fstat(fd, sb) == -1) {
1772 if (errno == ENOENT) {
1773 if (got_fileindex_entry_has_file_on_disk(ie))
1774 *status = GOT_STATUS_MISSING;
1776 *status = GOT_STATUS_DELETE;
1779 err = got_error_from_errno2("fstat", abspath);
1784 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1785 *status = GOT_STATUS_OBSTRUCTED;
1789 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1790 *status = GOT_STATUS_DELETE;
1792 } else if (!got_fileindex_entry_has_blob(ie) &&
1793 staged_status != GOT_STATUS_ADD) {
1794 *status = GOT_STATUS_ADD;
1798 if (!stat_info_differs(ie, sb))
1801 if (S_ISLNK(sb->st_mode) &&
1802 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1803 *status = GOT_STATUS_MODIFY;
1807 if (staged_status == GOT_STATUS_MODIFY ||
1808 staged_status == GOT_STATUS_ADD)
1809 got_fileindex_entry_get_staged_blob_id(&id, ie);
1811 got_fileindex_entry_get_blob_id(&id, ie);
1813 fd1 = got_opentempfd();
1815 err = got_error_from_errno("got_opentempfd");
1818 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1822 if (S_ISLNK(sb->st_mode)) {
1823 err = get_symlink_modification_status(status, ie,
1824 abspath, dirfd, de_name, blob);
1829 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1831 err = got_error_from_errno2("openat", abspath);
1836 f = fdopen(fd, "r");
1838 err = got_error_from_errno2("fdopen", abspath);
1842 hdrlen = got_object_blob_get_hdrlen(blob);
1844 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1845 err = got_object_blob_read_block(&blen, blob);
1848 /* Skip length of blob object header first time around. */
1849 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1850 if (flen == 0 && ferror(f)) {
1851 err = got_error_from_errno("fread");
1854 if (blen - hdrlen == 0) {
1856 *status = GOT_STATUS_MODIFY;
1858 } else if (flen == 0) {
1859 if (blen - hdrlen != 0)
1860 *status = GOT_STATUS_MODIFY;
1862 } else if (blen - hdrlen == flen) {
1863 /* Skip blob object header first time around. */
1864 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1865 *status = GOT_STATUS_MODIFY;
1869 *status = GOT_STATUS_MODIFY;
1875 if (*status == GOT_STATUS_MODIFY) {
1877 err = get_modified_file_content_status(status, blob, ie->path,
1879 } else if (xbit_differs(ie, sb->st_mode))
1880 *status = GOT_STATUS_MODE_CHANGE;
1882 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1883 err = got_error_from_errno("close");
1885 got_object_blob_close(blob);
1886 if (f != NULL && fclose(f) == EOF && err == NULL)
1887 err = got_error_from_errno2("fclose", abspath);
1888 if (fd != -1 && close(fd) == -1 && err == NULL)
1889 err = got_error_from_errno2("close", abspath);
1894 * Update timestamps in the file index if a file is unmodified and
1895 * we had to run a full content comparison to find out.
1897 static const struct got_error *
1898 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1899 struct got_fileindex_entry *ie, struct stat *sb)
1901 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1902 return got_fileindex_entry_update(ie, wt_fd, path,
1903 ie->blob_sha1, ie->commit_sha1, 1);
1908 static const struct got_error *remove_ondisk_file(const char *, const char *);
1910 static const struct got_error *
1911 update_blob(struct got_worktree *worktree,
1912 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1913 struct got_tree_entry *te, const char *path,
1914 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1917 const struct got_error *err = NULL;
1918 struct got_blob_object *blob = NULL;
1919 char *ondisk_path = NULL;
1920 unsigned char status = GOT_STATUS_NO_CHANGE;
1922 int fd1 = -1, fd2 = -1;
1923 int update_timestamps;
1925 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1926 return got_error_from_errno("asprintf");
1929 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1930 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1933 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1937 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1938 sb.st_mode = got_fileindex_perms_to_st(ie);
1940 if (stat(ondisk_path, &sb) == -1) {
1941 if (errno != ENOENT && errno != ENOTDIR) {
1942 err = got_error_from_errno2("stat",
1946 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1947 status = GOT_STATUS_UNVERSIONED;
1949 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1950 status = GOT_STATUS_UNVERSIONED;
1952 status = GOT_STATUS_OBSTRUCTED;
1956 if (status == GOT_STATUS_OBSTRUCTED) {
1958 got_fileindex_entry_mark_skipped(ie);
1959 err = (*progress_cb)(progress_arg, status, path);
1962 if (status == GOT_STATUS_CONFLICT) {
1964 got_fileindex_entry_mark_skipped(ie);
1965 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1970 if (S_ISDIR(te->mode)) { /* file changing into a directory */
1971 if (status == GOT_STATUS_UNVERSIONED) {
1972 err = (*progress_cb)(progress_arg, status, path);
1973 } else if (status != GOT_STATUS_NO_CHANGE &&
1974 status != GOT_STATUS_DELETE &&
1975 status != GOT_STATUS_NONEXISTENT &&
1976 status != GOT_STATUS_MISSING) {
1977 err = (*progress_cb)(progress_arg,
1978 GOT_STATUS_CANNOT_DELETE, path);
1980 if (status != GOT_STATUS_DELETE &&
1981 status != GOT_STATUS_NONEXISTENT &&
1982 status != GOT_STATUS_MISSING) {
1983 err = remove_ondisk_file(worktree->root_path,
1985 if (err && !(err->code == GOT_ERR_ERRNO &&
1989 got_fileindex_entry_remove(fileindex, ie);
1990 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE,
1993 goto done; /* nothing else to do */
1996 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1997 (S_ISLNK(te->mode) ||
1998 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
2000 * This is a regular file or an installed bad symlink.
2001 * If the file index indicates that this file is already
2002 * up-to-date with respect to the repository we can skip
2003 * updating contents of this file.
2005 if (got_fileindex_entry_has_commit(ie) &&
2006 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
2007 SHA1_DIGEST_LENGTH) == 0) {
2009 err = sync_timestamps(worktree->root_fd,
2010 path, status, ie, &sb);
2013 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2017 if (got_fileindex_entry_has_blob(ie) &&
2018 memcmp(ie->blob_sha1, te->id.sha1,
2019 SHA1_DIGEST_LENGTH) == 0) {
2020 /* Different commit but the same blob. */
2021 if (got_fileindex_entry_has_commit(ie)) {
2022 /* Update the base commit ID of this file. */
2023 memcpy(ie->commit_sha1,
2024 worktree->base_commit_id->sha1,
2025 sizeof(ie->commit_sha1));
2027 err = sync_timestamps(worktree->root_fd,
2028 path, status, ie, &sb);
2031 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2037 fd1 = got_opentempfd();
2039 err = got_error_from_errno("got_opentempfd");
2042 err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
2046 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
2047 int update_timestamps;
2048 struct got_blob_object *blob2 = NULL;
2049 char *label_orig = NULL;
2050 if (got_fileindex_entry_has_blob(ie)) {
2051 fd2 = got_opentempfd();
2053 err = got_error_from_errno("got_opentempfd");
2056 struct got_object_id id2;
2057 got_fileindex_entry_get_blob_id(&id2, ie);
2058 err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
2063 if (got_fileindex_entry_has_commit(ie)) {
2064 char id_str[SHA1_DIGEST_STRING_LENGTH];
2065 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2066 sizeof(id_str)) == NULL) {
2067 err = got_error_path(id_str,
2068 GOT_ERR_BAD_OBJ_ID_STR);
2071 if (asprintf(&label_orig, "%s: commit %s",
2072 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2073 err = got_error_from_errno("asprintf");
2077 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2079 err = got_object_blob_read_to_str(&link_target, blob);
2082 err = merge_symlink(worktree, blob2, ondisk_path, path,
2083 label_orig, link_target, worktree->base_commit_id,
2084 repo, progress_cb, progress_arg);
2087 err = merge_blob(&update_timestamps, worktree, blob2,
2088 ondisk_path, path, sb.st_mode, label_orig, blob,
2089 worktree->base_commit_id, repo,
2090 progress_cb, progress_arg);
2093 if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2094 err = got_error_from_errno("close");
2098 got_object_blob_close(blob2);
2102 * Do not update timestamps of files with local changes.
2103 * Otherwise, a future status walk would treat them as
2104 * unmodified files again.
2106 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2107 blob->id.sha1, worktree->base_commit_id->sha1,
2109 } else if (status == GOT_STATUS_MODE_CHANGE) {
2110 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2111 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2112 } else if (status == GOT_STATUS_DELETE) {
2113 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2116 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2117 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2121 int is_bad_symlink = 0;
2122 if (S_ISLNK(te->mode)) {
2123 err = install_symlink(&is_bad_symlink, worktree,
2124 ondisk_path, path, blob,
2125 status == GOT_STATUS_MISSING, 0,
2126 status == GOT_STATUS_UNVERSIONED, 0,
2127 repo, progress_cb, progress_arg);
2129 err = install_blob(worktree, ondisk_path, path,
2130 te->mode, sb.st_mode, blob,
2131 status == GOT_STATUS_MISSING, 0, 0,
2132 status == GOT_STATUS_UNVERSIONED,
2134 repo, progress_cb, progress_arg);
2140 err = got_fileindex_entry_update(ie,
2141 worktree->root_fd, path, blob->id.sha1,
2142 worktree->base_commit_id->sha1, 1);
2144 err = create_fileindex_entry(&ie, fileindex,
2145 worktree->base_commit_id, worktree->root_fd, path,
2146 &blob->id, update_timestamps);
2151 if (is_bad_symlink) {
2152 got_fileindex_entry_filetype_set(ie,
2153 GOT_FILEIDX_MODE_BAD_SYMLINK);
2157 if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2158 err = got_error_from_errno("close");
2161 got_object_blob_close(blob);
2167 static const struct got_error *
2168 remove_ondisk_file(const char *root_path, const char *path)
2170 const struct got_error *err = NULL;
2171 char *ondisk_path = NULL, *parent = NULL;
2173 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2174 return got_error_from_errno("asprintf");
2176 if (unlink(ondisk_path) == -1) {
2177 if (errno != ENOENT)
2178 err = got_error_from_errno2("unlink", ondisk_path);
2180 size_t root_len = strlen(root_path);
2181 err = got_path_dirname(&parent, ondisk_path);
2184 while (got_path_cmp(parent, root_path,
2185 strlen(parent), root_len) != 0) {
2187 ondisk_path = parent;
2189 if (rmdir(ondisk_path) == -1) {
2190 if (errno != ENOTEMPTY)
2191 err = got_error_from_errno2("rmdir",
2195 err = got_path_dirname(&parent, ondisk_path);
2206 static const struct got_error *
2207 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2208 struct got_fileindex_entry *ie, struct got_repository *repo,
2209 got_worktree_checkout_cb progress_cb, void *progress_arg)
2211 const struct got_error *err = NULL;
2212 unsigned char status;
2216 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2217 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2219 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2221 return got_error_from_errno("asprintf");
2223 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2227 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2228 char ondisk_target[PATH_MAX];
2229 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2230 sizeof(ondisk_target));
2231 if (ondisk_len == -1) {
2232 err = got_error_from_errno2("readlink", ondisk_path);
2235 ondisk_target[ondisk_len] = '\0';
2236 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2237 NULL, NULL, /* XXX pass common ancestor info? */
2238 ondisk_target, ondisk_path);
2241 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2246 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2247 status == GOT_STATUS_ADD) {
2248 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2252 * Preserve the working file and change the deleted blob's
2253 * entry into a schedule-add entry.
2255 err = got_fileindex_entry_update(ie, worktree->root_fd,
2256 ie->path, NULL, NULL, 0);
2258 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2261 if (status == GOT_STATUS_NO_CHANGE) {
2262 err = remove_ondisk_file(worktree->root_path, ie->path);
2266 got_fileindex_entry_remove(fileindex, ie);
2273 struct diff_cb_arg {
2274 struct got_fileindex *fileindex;
2275 struct got_worktree *worktree;
2276 struct got_repository *repo;
2277 got_worktree_checkout_cb progress_cb;
2279 got_cancel_cb cancel_cb;
2283 static const struct got_error *
2284 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2285 struct got_tree_entry *te, const char *parent_path)
2287 const struct got_error *err = NULL;
2288 struct diff_cb_arg *a = arg;
2291 err = a->cancel_cb(a->cancel_arg);
2296 return update_blob(a->worktree, a->fileindex, ie, te,
2297 ie->path, a->repo, a->progress_cb, a->progress_arg);
2300 static const struct got_error *
2301 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2303 const struct got_error *err = NULL;
2304 struct diff_cb_arg *a = arg;
2307 err = a->cancel_cb(a->cancel_arg);
2312 return delete_blob(a->worktree, a->fileindex, ie,
2313 a->repo, a->progress_cb, a->progress_arg);
2316 static const struct got_error *
2317 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2319 const struct got_error *err = NULL;
2320 struct diff_cb_arg *a = arg;
2324 err = a->cancel_cb(a->cancel_arg);
2329 if (got_object_tree_entry_is_submodule(te))
2332 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2335 if (asprintf(&path, "%s%s%s", parent_path,
2336 parent_path[0] ? "/" : "", te->name)
2338 return got_error_from_errno("asprintf");
2340 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2341 a->repo, a->progress_cb, a->progress_arg);
2347 const struct got_error *
2348 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2350 uint32_t uuid_status;
2352 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2353 if (uuid_status != uuid_s_ok) {
2355 return got_error_uuid(uuid_status, "uuid_to_string");
2361 static const struct got_error *
2362 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2364 const struct got_error *err = NULL;
2365 char *uuidstr = NULL;
2369 err = got_worktree_get_uuid(&uuidstr, worktree);
2373 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2374 err = got_error_from_errno("asprintf");
2381 const struct got_error *
2382 got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2385 return get_ref_name(refname, worktree, prefix);
2388 static const struct got_error *
2389 get_base_ref_name(char **refname, struct got_worktree *worktree)
2391 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2394 static const struct got_error *
2395 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2397 return get_ref_name(refname, worktree,
2398 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2401 static const struct got_error *
2402 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2404 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2407 static const struct got_error *
2408 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2410 return get_ref_name(refname, worktree,
2411 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2414 static const struct got_error *
2415 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2417 return get_ref_name(refname, worktree,
2418 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2421 static const struct got_error *
2422 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2424 return get_ref_name(refname, worktree,
2425 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2428 static const struct got_error *
2429 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2431 return get_ref_name(refname, worktree,
2432 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2435 static const struct got_error *
2436 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2438 return get_ref_name(refname, worktree,
2439 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2442 static const struct got_error *
2443 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2445 return get_ref_name(refname, worktree,
2446 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2449 const struct got_error *
2450 got_worktree_get_histedit_script_path(char **path,
2451 struct got_worktree *worktree)
2453 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2454 worktree->meta_dir, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2456 return got_error_from_errno("asprintf");
2461 static const struct got_error *
2462 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2464 return get_ref_name(refname, worktree,
2465 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2468 static const struct got_error *
2469 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2471 return get_ref_name(refname, worktree,
2472 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2476 * Prevent Git's garbage collector from deleting our base commit by
2477 * setting a reference to our base commit's ID.
2479 static const struct got_error *
2480 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2482 const struct got_error *err = NULL;
2483 struct got_reference *ref = NULL;
2486 err = get_base_ref_name(&refname, worktree);
2490 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2494 err = got_ref_write(ref, repo);
2502 static const struct got_error *
2503 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2505 const struct got_error *err = NULL;
2507 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2508 worktree->meta_dir, GOT_WORKTREE_FILE_INDEX) == -1) {
2509 err = got_error_from_errno("asprintf");
2510 *fileindex_path = NULL;
2516 static const struct got_error *
2517 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2518 struct got_worktree *worktree)
2520 const struct got_error *err = NULL;
2523 *fileindex_path = NULL;
2524 *fileindex = got_fileindex_alloc();
2525 if (*fileindex == NULL)
2526 return got_error_from_errno("got_fileindex_alloc");
2528 err = get_fileindex_path(fileindex_path, worktree);
2532 index = fopen(*fileindex_path, "rbe");
2533 if (index == NULL) {
2534 if (errno != ENOENT)
2535 err = got_error_from_errno2("fopen", *fileindex_path);
2537 err = got_fileindex_read(*fileindex, index);
2538 if (fclose(index) == EOF && err == NULL)
2539 err = got_error_from_errno("fclose");
2543 free(*fileindex_path);
2544 *fileindex_path = NULL;
2545 got_fileindex_free(*fileindex);
2551 struct bump_base_commit_id_arg {
2552 struct got_object_id *base_commit_id;
2555 const char *entry_name;
2556 got_worktree_checkout_cb progress_cb;
2560 static const struct got_error *
2561 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2563 const struct got_error *err;
2564 struct bump_base_commit_id_arg *a = arg;
2566 if (a->entry_name) {
2567 if (strcmp(ie->path, a->path) != 0)
2569 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2572 if (got_fileindex_entry_was_skipped(ie))
2575 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2576 SHA1_DIGEST_LENGTH) == 0)
2579 if (a->progress_cb) {
2580 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2585 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2589 /* Bump base commit ID of all files within an updated part of the work tree. */
2590 static const struct got_error *
2591 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2592 struct got_fileindex *fileindex,
2593 got_worktree_checkout_cb progress_cb, void *progress_arg)
2595 struct bump_base_commit_id_arg bbc_arg;
2597 bbc_arg.base_commit_id = worktree->base_commit_id;
2598 bbc_arg.entry_name = NULL;
2600 bbc_arg.path_len = 0;
2601 bbc_arg.progress_cb = progress_cb;
2602 bbc_arg.progress_arg = progress_arg;
2604 return got_fileindex_for_each_entry_safe(fileindex,
2605 bump_base_commit_id, &bbc_arg);
2608 static const struct got_error *
2609 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2611 const struct got_error *err = NULL;
2612 char *new_fileindex_path = NULL;
2613 FILE *new_index = NULL;
2614 struct timespec timeout;
2616 err = got_opentemp_named(&new_fileindex_path, &new_index,
2617 fileindex_path, "");
2621 err = got_fileindex_write(fileindex, new_index);
2625 if (rename(new_fileindex_path, fileindex_path) != 0) {
2626 err = got_error_from_errno3("rename", new_fileindex_path,
2628 unlink(new_fileindex_path);
2632 * Sleep for a short amount of time to ensure that files modified after
2633 * this program exits have a different time stamp from the one which
2634 * was recorded in the file index.
2637 timeout.tv_nsec = 1;
2638 nanosleep(&timeout, NULL);
2642 free(new_fileindex_path);
2646 static const struct got_error *
2647 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2648 struct got_object_id **tree_id, const char *wt_relpath,
2649 struct got_commit_object *base_commit, struct got_worktree *worktree,
2650 struct got_repository *repo)
2652 const struct got_error *err = NULL;
2653 struct got_object_id *id = NULL;
2654 char *in_repo_path = NULL;
2655 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2657 *entry_type = GOT_OBJ_TYPE_ANY;
2658 *tree_relpath = NULL;
2661 if (wt_relpath[0] == '\0') {
2662 /* Check out all files within the work tree. */
2663 *entry_type = GOT_OBJ_TYPE_TREE;
2664 *tree_relpath = strdup("");
2665 if (*tree_relpath == NULL) {
2666 err = got_error_from_errno("strdup");
2669 err = got_object_id_by_path(tree_id, repo, base_commit,
2670 worktree->path_prefix);
2676 /* Check out a subset of files in the work tree. */
2678 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2679 is_root_wt ? "" : "/", wt_relpath) == -1) {
2680 err = got_error_from_errno("asprintf");
2684 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2689 in_repo_path = NULL;
2691 err = got_object_get_type(entry_type, repo, id);
2695 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2696 /* Check out a single file. */
2697 if (strchr(wt_relpath, '/') == NULL) {
2698 /* Check out a single file in work tree's root dir. */
2699 in_repo_path = strdup(worktree->path_prefix);
2700 if (in_repo_path == NULL) {
2701 err = got_error_from_errno("strdup");
2704 *tree_relpath = strdup("");
2705 if (*tree_relpath == NULL) {
2706 err = got_error_from_errno("strdup");
2710 /* Check out a single file in a subdirectory. */
2711 err = got_path_dirname(tree_relpath, wt_relpath);
2714 if (asprintf(&in_repo_path, "%s%s%s",
2715 worktree->path_prefix, is_root_wt ? "" : "/",
2716 *tree_relpath) == -1) {
2717 err = got_error_from_errno("asprintf");
2721 err = got_object_id_by_path(tree_id, repo,
2722 base_commit, in_repo_path);
2724 /* Check out all files within a subdirectory. */
2725 *tree_id = got_object_id_dup(id);
2726 if (*tree_id == NULL) {
2727 err = got_error_from_errno("got_object_id_dup");
2730 *tree_relpath = strdup(wt_relpath);
2731 if (*tree_relpath == NULL) {
2732 err = got_error_from_errno("strdup");
2740 *entry_type = GOT_OBJ_TYPE_ANY;
2741 free(*tree_relpath);
2742 *tree_relpath = NULL;
2749 static const struct got_error *
2750 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2751 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2752 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2753 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2755 const struct got_error *err = NULL;
2756 struct got_commit_object *commit = NULL;
2757 struct got_tree_object *tree = NULL;
2758 struct got_fileindex_diff_tree_cb diff_cb;
2759 struct diff_cb_arg arg;
2761 err = ref_base_commit(worktree, repo);
2763 if (!(err->code == GOT_ERR_ERRNO &&
2764 (errno == EACCES || errno == EROFS)))
2766 err = (*progress_cb)(progress_arg,
2767 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2772 err = got_object_open_as_commit(&commit, repo,
2773 worktree->base_commit_id);
2777 err = got_object_open_as_tree(&tree, repo, tree_id);
2782 got_object_tree_find_entry(tree, entry_name) == NULL) {
2783 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2787 diff_cb.diff_old_new = diff_old_new;
2788 diff_cb.diff_old = diff_old;
2789 diff_cb.diff_new = diff_new;
2790 arg.fileindex = fileindex;
2791 arg.worktree = worktree;
2793 arg.progress_cb = progress_cb;
2794 arg.progress_arg = progress_arg;
2795 arg.cancel_cb = cancel_cb;
2796 arg.cancel_arg = cancel_arg;
2797 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2798 entry_name, repo, &diff_cb, &arg);
2801 got_object_tree_close(tree);
2803 got_object_commit_close(commit);
2807 const struct got_error *
2808 got_worktree_checkout_files(struct got_worktree *worktree,
2809 struct got_pathlist_head *paths, struct got_repository *repo,
2810 got_worktree_checkout_cb progress_cb, void *progress_arg,
2811 got_cancel_cb cancel_cb, void *cancel_arg)
2813 const struct got_error *err = NULL, *sync_err, *unlockerr;
2814 struct got_commit_object *commit = NULL;
2815 struct got_tree_object *tree = NULL;
2816 struct got_fileindex *fileindex = NULL;
2817 char *fileindex_path = NULL;
2818 struct got_pathlist_entry *pe;
2819 struct tree_path_data {
2820 STAILQ_ENTRY(tree_path_data) entry;
2821 struct got_object_id *tree_id;
2826 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2828 STAILQ_INIT(&tree_paths);
2830 err = lock_worktree(worktree, LOCK_EX);
2834 err = got_object_open_as_commit(&commit, repo,
2835 worktree->base_commit_id);
2839 /* Map all specified paths to in-repository trees. */
2840 TAILQ_FOREACH(pe, paths, entry) {
2841 tpd = malloc(sizeof(*tpd));
2843 err = got_error_from_errno("malloc");
2847 err = find_tree_entry_for_checkout(&tpd->entry_type,
2848 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2855 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2856 err = got_path_basename(&tpd->entry_name, pe->path);
2864 tpd->entry_name = NULL;
2866 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2870 * Read the file index.
2871 * Checking out files is supposed to be an idempotent operation.
2872 * If the on-disk file index is incomplete we will try to complete it.
2874 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2878 tpd = STAILQ_FIRST(&tree_paths);
2879 TAILQ_FOREACH(pe, paths, entry) {
2880 struct bump_base_commit_id_arg bbc_arg;
2882 err = checkout_files(worktree, fileindex, tpd->relpath,
2883 tpd->tree_id, tpd->entry_name, repo,
2884 progress_cb, progress_arg, cancel_cb, cancel_arg);
2888 bbc_arg.base_commit_id = worktree->base_commit_id;
2889 bbc_arg.entry_name = tpd->entry_name;
2890 bbc_arg.path = pe->path;
2891 bbc_arg.path_len = pe->path_len;
2892 bbc_arg.progress_cb = progress_cb;
2893 bbc_arg.progress_arg = progress_arg;
2894 err = got_fileindex_for_each_entry_safe(fileindex,
2895 bump_base_commit_id, &bbc_arg);
2899 tpd = STAILQ_NEXT(tpd, entry);
2901 sync_err = sync_fileindex(fileindex, fileindex_path);
2902 if (sync_err && err == NULL)
2905 free(fileindex_path);
2907 got_object_tree_close(tree);
2909 got_object_commit_close(commit);
2911 got_fileindex_free(fileindex);
2912 while (!STAILQ_EMPTY(&tree_paths)) {
2913 tpd = STAILQ_FIRST(&tree_paths);
2914 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2919 unlockerr = lock_worktree(worktree, LOCK_SH);
2920 if (unlockerr && err == NULL)
2925 static const struct got_error *
2926 add_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2927 struct got_fileindex_entry *ie, const char *ondisk_path,
2928 const char *path2, struct got_blob_object *blob2, mode_t mode2,
2929 int restoring_missing_file, int reverting_versioned_file,
2930 int path_is_unversioned, int allow_bad_symlinks,
2931 struct got_repository *repo,
2932 got_worktree_checkout_cb progress_cb, void *progress_arg)
2934 const struct got_error *err = NULL;
2935 int is_bad_symlink = 0;
2937 if (S_ISLNK(mode2)) {
2938 err = install_symlink(&is_bad_symlink,
2939 worktree, ondisk_path, path2, blob2,
2940 restoring_missing_file,
2941 reverting_versioned_file,
2942 path_is_unversioned, allow_bad_symlinks,
2943 repo, progress_cb, progress_arg);
2945 err = install_blob(worktree, ondisk_path, path2,
2946 mode2, GOT_DEFAULT_FILE_MODE, blob2,
2947 restoring_missing_file, reverting_versioned_file, 0,
2948 path_is_unversioned, NULL, repo,
2949 progress_cb, progress_arg);
2954 /* Adding an unversioned file. */
2955 err = got_fileindex_entry_alloc(&ie, path2);
2958 err = got_fileindex_entry_update(ie,
2959 worktree->root_fd, path2, NULL, NULL, 1);
2961 got_fileindex_entry_free(ie);
2964 err = got_fileindex_entry_add(fileindex, ie);
2966 got_fileindex_entry_free(ie);
2970 /* Re-adding a locally deleted file. */
2971 err = got_fileindex_entry_update(ie,
2972 worktree->root_fd, path2, ie->blob_sha1,
2973 worktree->base_commit_id->sha1, 0);
2978 if (is_bad_symlink) {
2979 got_fileindex_entry_filetype_set(ie,
2980 GOT_FILEIDX_MODE_BAD_SYMLINK);
2986 struct merge_file_cb_arg {
2987 struct got_worktree *worktree;
2988 struct got_fileindex *fileindex;
2989 got_worktree_checkout_cb progress_cb;
2991 got_cancel_cb cancel_cb;
2993 const char *label_orig;
2994 struct got_object_id *commit_id2;
2995 int allow_bad_symlinks;
2998 static const struct got_error *
2999 merge_file_cb(void *arg, struct got_blob_object *blob1,
3000 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3001 struct got_object_id *id1, struct got_object_id *id2,
3002 const char *path1, const char *path2,
3003 mode_t mode1, mode_t mode2, struct got_repository *repo)
3005 static const struct got_error *err = NULL;
3006 struct merge_file_cb_arg *a = arg;
3007 struct got_fileindex_entry *ie;
3008 char *ondisk_path = NULL;
3010 unsigned char status;
3011 int local_changes_subsumed;
3012 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
3013 char *id_str = NULL, *label_deriv2 = NULL;
3014 struct got_object_id *id = NULL;
3016 if (blob1 && blob2) {
3017 ie = got_fileindex_entry_get(a->fileindex, path2,
3020 return (*a->progress_cb)(a->progress_arg,
3021 GOT_STATUS_MISSING, path2);
3023 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3025 return got_error_from_errno("asprintf");
3027 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3032 if (status == GOT_STATUS_DELETE) {
3033 err = (*a->progress_cb)(a->progress_arg,
3034 GOT_STATUS_MERGE, path2);
3037 if (status != GOT_STATUS_NO_CHANGE &&
3038 status != GOT_STATUS_MODIFY &&
3039 status != GOT_STATUS_CONFLICT &&
3040 status != GOT_STATUS_ADD) {
3041 err = (*a->progress_cb)(a->progress_arg, status, path2);
3045 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
3047 err = got_object_blob_read_to_str(&link_target2, blob2);
3050 err = merge_symlink(a->worktree, blob1, ondisk_path,
3051 path2, a->label_orig, link_target2, a->commit_id2,
3052 repo, a->progress_cb, a->progress_arg);
3057 f_orig = got_opentemp();
3058 if (f_orig == NULL) {
3059 err = got_error_from_errno("got_opentemp");
3062 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3067 f_deriv2 = got_opentemp();
3068 if (f_deriv2 == NULL)
3070 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3075 fd = open(ondisk_path,
3076 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3078 err = got_error_from_errno2("open",
3082 f_deriv = fdopen(fd, "r");
3083 if (f_deriv == NULL) {
3084 err = got_error_from_errno2("fdopen",
3089 err = got_object_id_str(&id_str, a->commit_id2);
3092 if (asprintf(&label_deriv2, "%s: commit %s",
3093 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
3094 err = got_error_from_errno("asprintf");
3097 err = merge_file(&local_changes_subsumed, a->worktree,
3098 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
3099 mode2, a->label_orig, NULL, label_deriv2,
3100 GOT_DIFF_ALGORITHM_PATIENCE, repo,
3101 a->progress_cb, a->progress_arg);
3104 ie = got_fileindex_entry_get(a->fileindex, path1,
3107 return (*a->progress_cb)(a->progress_arg,
3108 GOT_STATUS_MISSING, path1);
3110 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3112 return got_error_from_errno("asprintf");
3114 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3120 case GOT_STATUS_NO_CHANGE:
3121 err = (*a->progress_cb)(a->progress_arg,
3122 GOT_STATUS_DELETE, path1);
3125 err = remove_ondisk_file(a->worktree->root_path, path1);
3129 got_fileindex_entry_mark_deleted_from_disk(ie);
3131 case GOT_STATUS_DELETE:
3132 case GOT_STATUS_MISSING:
3133 err = (*a->progress_cb)(a->progress_arg,
3134 GOT_STATUS_DELETE, path1);
3138 got_fileindex_entry_mark_deleted_from_disk(ie);
3140 case GOT_STATUS_MODIFY: {
3145 * Delete the file only if its content already
3146 * exists in the repository.
3148 err = got_object_blob_file_create(&id, &blob1_f,
3149 &blob1_size, path1);
3152 if (fclose(blob1_f) == EOF) {
3153 err = got_error_from_errno("fclose");
3157 /* Implied existence check. */
3158 err = got_object_get_type(&obj_type, repo, id);
3160 if (err->code != GOT_ERR_NO_OBJ)
3162 err = (*a->progress_cb)(a->progress_arg,
3163 GOT_STATUS_CANNOT_DELETE, path1);
3165 } else if (obj_type != GOT_OBJ_TYPE_BLOB) {
3166 err = (*a->progress_cb)(a->progress_arg,
3167 GOT_STATUS_CANNOT_DELETE, path1);
3170 err = (*a->progress_cb)(a->progress_arg,
3171 GOT_STATUS_DELETE, path1);
3174 err = remove_ondisk_file(a->worktree->root_path,
3179 got_fileindex_entry_mark_deleted_from_disk(ie);
3182 case GOT_STATUS_ADD: {
3186 * Delete the file only if its content already
3187 * exists in the repository.
3189 err = got_object_blob_file_create(&id, &blob1_f,
3190 &blob1_size, path1);
3193 if (fclose(blob1_f) == EOF) {
3194 err = got_error_from_errno("fclose");
3197 if (got_object_id_cmp(id, id1) == 0) {
3198 err = (*a->progress_cb)(a->progress_arg,
3199 GOT_STATUS_DELETE, path1);
3202 err = remove_ondisk_file(a->worktree->root_path,
3207 got_fileindex_entry_remove(a->fileindex,
3210 err = (*a->progress_cb)(a->progress_arg,
3211 GOT_STATUS_CANNOT_DELETE, path1);
3217 case GOT_STATUS_CONFLICT:
3218 err = (*a->progress_cb)(a->progress_arg,
3219 GOT_STATUS_CANNOT_DELETE, path1);
3223 case GOT_STATUS_OBSTRUCTED:
3224 err = (*a->progress_cb)(a->progress_arg, status, path1);
3232 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3234 return got_error_from_errno("asprintf");
3235 ie = got_fileindex_entry_get(a->fileindex, path2,
3238 err = get_file_status(&status, &sb, ie, ondisk_path,
3242 if (status != GOT_STATUS_NO_CHANGE &&
3243 status != GOT_STATUS_MODIFY &&
3244 status != GOT_STATUS_CONFLICT &&
3245 status != GOT_STATUS_ADD &&
3246 status != GOT_STATUS_DELETE) {
3247 err = (*a->progress_cb)(a->progress_arg,
3251 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3253 err = got_object_blob_read_to_str(&link_target2,
3257 err = merge_symlink(a->worktree, NULL,
3258 ondisk_path, path2, a->label_orig,
3259 link_target2, a->commit_id2, repo,
3260 a->progress_cb, a->progress_arg);
3262 } else if (S_ISREG(sb.st_mode)) {
3263 err = merge_blob(&local_changes_subsumed,
3264 a->worktree, NULL, ondisk_path, path2,
3265 sb.st_mode, a->label_orig, blob2,
3266 a->commit_id2, repo, a->progress_cb,
3268 } else if (status != GOT_STATUS_DELETE) {
3269 err = got_error_path(ondisk_path,
3270 GOT_ERR_FILE_OBSTRUCTED);
3274 if (status == GOT_STATUS_DELETE) {
3275 /* Re-add file with content from new blob. */
3276 err = add_file(a->worktree, a->fileindex, ie,
3277 ondisk_path, path2, blob2, mode2,
3278 0, 0, 0, a->allow_bad_symlinks,
3279 repo, a->progress_cb, a->progress_arg);
3284 err = add_file(a->worktree, a->fileindex, NULL,
3285 ondisk_path, path2, blob2, mode2,
3286 0, 0, 1, a->allow_bad_symlinks,
3287 repo, a->progress_cb, a->progress_arg);
3293 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3294 err = got_error_from_errno("fclose");
3295 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3296 err = got_error_from_errno("fclose");
3297 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3298 err = got_error_from_errno("fclose");
3306 struct check_mixed_commits_args {
3307 struct got_worktree *worktree;
3308 got_cancel_cb cancel_cb;
3312 static const struct got_error *
3313 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3315 const struct got_error *err = NULL;
3316 struct check_mixed_commits_args *a = arg;
3319 err = a->cancel_cb(a->cancel_arg);
3324 /* Reject merges into a work tree with mixed base commits. */
3325 if (got_fileindex_entry_has_commit(ie) &&
3326 memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3327 SHA1_DIGEST_LENGTH) != 0)
3328 return got_error(GOT_ERR_MIXED_COMMITS);
3333 const struct got_error *
3334 got_worktree_get_state(char *state, struct got_repository *repo,
3335 struct got_worktree *worktree,
3336 got_cancel_cb cancel_cb, void *cancel_arg)
3338 const struct got_error *err;
3339 struct got_object_id *base_id, *head_id = NULL;
3340 struct got_reference *head_ref;
3341 struct got_fileindex *fileindex = NULL;
3342 char *fileindex_path = NULL;
3343 struct check_mixed_commits_args cma;
3345 if (worktree == NULL)
3346 return got_error(GOT_ERR_NOT_WORKTREE);
3348 err = got_ref_open(&head_ref, repo,
3349 got_worktree_get_head_ref_name(worktree), 0);
3353 err = got_ref_resolve(&head_id, repo, head_ref);
3357 *state = GOT_WORKTREE_STATE_UNKNOWN;
3358 base_id = got_worktree_get_base_commit_id(worktree);
3360 cma.worktree = worktree;
3361 cma.cancel_cb = cancel_cb;
3362 cma.cancel_arg = cancel_arg;
3364 if (got_object_id_cmp(base_id, head_id) == 0) {
3365 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3369 err = got_fileindex_for_each_entry_safe(fileindex,
3370 check_mixed_commits, &cma);
3372 *state = GOT_WORKTREE_STATE_UPTODATE;
3373 else if (err->code == GOT_ERR_MIXED_COMMITS) {
3374 *state = GOT_WORKTREE_STATE_OUTOFDATE;
3378 *state = GOT_WORKTREE_STATE_OUTOFDATE;
3382 free(fileindex_path);
3383 got_ref_close(head_ref);
3384 if (fileindex != NULL)
3385 got_fileindex_free(fileindex);
3389 struct check_merge_conflicts_arg {
3390 struct got_worktree *worktree;
3391 struct got_fileindex *fileindex;
3392 struct got_repository *repo;
3395 static const struct got_error *
3396 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3397 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3398 struct got_object_id *id1, struct got_object_id *id2,
3399 const char *path1, const char *path2,
3400 mode_t mode1, mode_t mode2, struct got_repository *repo)
3402 const struct got_error *err = NULL;
3403 struct check_merge_conflicts_arg *a = arg;
3404 unsigned char status;
3406 struct got_fileindex_entry *ie;
3407 const char *path = path2 ? path2 : path1;
3408 struct got_object_id *id = id2 ? id2 : id1;
3414 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3418 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3420 return got_error_from_errno("asprintf");
3422 /* Reject merges into a work tree with conflicted files. */
3423 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3427 if (status == GOT_STATUS_CONFLICT)
3428 return got_error(GOT_ERR_CONFLICTS);
3433 static const struct got_error *
3434 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3435 const char *fileindex_path, struct got_object_id *commit_id1,
3436 struct got_object_id *commit_id2, struct got_repository *repo,
3437 got_worktree_checkout_cb progress_cb, void *progress_arg,
3438 got_cancel_cb cancel_cb, void *cancel_arg)
3440 const struct got_error *err = NULL, *sync_err;
3441 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3442 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3443 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3444 struct check_merge_conflicts_arg cmc_arg;
3445 struct merge_file_cb_arg arg;
3446 char *label_orig = NULL;
3447 FILE *f1 = NULL, *f2 = NULL;
3448 int fd1 = -1, fd2 = -1;
3451 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3454 err = got_object_id_by_path(&tree_id1, repo, commit1,
3455 worktree->path_prefix);
3456 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3462 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3466 err = got_object_id_str(&id_str, commit_id1);
3470 if (asprintf(&label_orig, "%s: commit %s",
3471 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3472 err = got_error_from_errno("asprintf");
3478 f1 = got_opentemp();
3480 err = got_error_from_errno("got_opentemp");
3485 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3489 err = got_object_id_by_path(&tree_id2, repo, commit2,
3490 worktree->path_prefix);
3494 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3498 f2 = got_opentemp();
3500 err = got_error_from_errno("got_opentemp");
3504 fd1 = got_opentempfd();
3506 err = got_error_from_errno("got_opentempfd");
3510 fd2 = got_opentempfd();
3512 err = got_error_from_errno("got_opentempfd");
3516 cmc_arg.worktree = worktree;
3517 cmc_arg.fileindex = fileindex;
3518 cmc_arg.repo = repo;
3519 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3520 check_merge_conflicts, &cmc_arg, 0);
3524 arg.worktree = worktree;
3525 arg.fileindex = fileindex;
3526 arg.progress_cb = progress_cb;
3527 arg.progress_arg = progress_arg;
3528 arg.cancel_cb = cancel_cb;
3529 arg.cancel_arg = cancel_arg;
3530 arg.label_orig = label_orig;
3531 arg.commit_id2 = commit_id2;
3532 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3533 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3534 merge_file_cb, &arg, 1);
3535 sync_err = sync_fileindex(fileindex, fileindex_path);
3536 if (sync_err && err == NULL)
3540 got_object_commit_close(commit1);
3542 got_object_commit_close(commit2);
3544 got_object_tree_close(tree1);
3546 got_object_tree_close(tree2);
3547 if (f1 && fclose(f1) == EOF && err == NULL)
3548 err = got_error_from_errno("fclose");
3549 if (f2 && fclose(f2) == EOF && err == NULL)
3550 err = got_error_from_errno("fclose");
3551 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3552 err = got_error_from_errno("close");
3553 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3554 err = got_error_from_errno("close");
3559 const struct got_error *
3560 got_worktree_merge_files(struct got_worktree *worktree,
3561 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3562 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3563 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3565 const struct got_error *err, *unlockerr;
3566 char *fileindex_path = NULL;
3567 struct got_fileindex *fileindex = NULL;
3568 struct check_mixed_commits_args cma;
3570 err = lock_worktree(worktree, LOCK_EX);
3574 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3578 cma.worktree = worktree;
3579 cma.cancel_cb = cancel_cb;
3580 cma.cancel_arg = cancel_arg;
3582 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3587 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3588 commit_id2, repo, progress_cb, progress_arg,
3589 cancel_cb, cancel_arg);
3592 got_fileindex_free(fileindex);
3593 free(fileindex_path);
3594 unlockerr = lock_worktree(worktree, LOCK_SH);
3595 if (unlockerr && err == NULL)
3600 struct diff_dir_cb_arg {
3601 struct got_fileindex *fileindex;
3602 struct got_worktree *worktree;
3603 const char *status_path;
3604 size_t status_path_len;
3605 struct got_repository *repo;
3606 got_worktree_status_cb status_cb;
3608 got_cancel_cb cancel_cb;
3610 /* A pathlist containing per-directory pathlists of ignore patterns. */
3611 struct got_pathlist_head *ignores;
3612 int report_unchanged;
3616 static const struct got_error *
3617 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3618 int dirfd, const char *de_name,
3619 got_worktree_status_cb status_cb, void *status_arg,
3620 struct got_repository *repo, int report_unchanged)
3622 const struct got_error *err = NULL;
3623 unsigned char status = GOT_STATUS_NO_CHANGE;
3624 unsigned char staged_status;
3626 struct got_object_id blob_id, commit_id, staged_blob_id;
3627 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3628 struct got_object_id *staged_blob_idp = NULL;
3630 staged_status = get_staged_status(ie);
3631 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3635 if (status == GOT_STATUS_NO_CHANGE &&
3636 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3639 if (got_fileindex_entry_has_blob(ie))
3640 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3641 if (got_fileindex_entry_has_commit(ie))
3642 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3643 if (staged_status == GOT_STATUS_ADD ||
3644 staged_status == GOT_STATUS_MODIFY) {
3645 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3646 &staged_blob_id, ie);
3649 return (*status_cb)(status_arg, status, staged_status,
3650 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3653 static const struct got_error *
3654 status_old_new(void *arg, struct got_fileindex_entry *ie,
3655 struct dirent *de, const char *parent_path, int dirfd)
3657 const struct got_error *err = NULL;
3658 struct diff_dir_cb_arg *a = arg;
3662 err = a->cancel_cb(a->cancel_arg);
3667 if (got_path_cmp(parent_path, a->status_path,
3668 strlen(parent_path), a->status_path_len) != 0 &&
3669 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3672 if (parent_path[0]) {
3673 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3674 parent_path, de->d_name) == -1)
3675 return got_error_from_errno("asprintf");
3677 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3679 return got_error_from_errno("asprintf");
3682 err = report_file_status(ie, abspath, dirfd, de->d_name,
3683 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3688 static const struct got_error *
3689 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3691 const struct got_error *err;
3692 struct diff_dir_cb_arg *a = arg;
3693 struct got_object_id blob_id, commit_id;
3694 unsigned char status;
3697 err = a->cancel_cb(a->cancel_arg);
3702 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3705 got_fileindex_entry_get_blob_id(&blob_id, ie);
3706 got_fileindex_entry_get_commit_id(&commit_id, ie);
3707 if (got_fileindex_entry_has_file_on_disk(ie))
3708 status = GOT_STATUS_MISSING;
3710 status = GOT_STATUS_DELETE;
3711 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3712 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3716 free_ignores(struct got_pathlist_head *ignores)
3718 struct got_pathlist_entry *pe;
3720 TAILQ_FOREACH(pe, ignores, entry) {
3721 struct got_pathlist_head *ignorelist = pe->data;
3723 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3725 got_pathlist_free(ignores, GOT_PATHLIST_FREE_ALL);
3728 static const struct got_error *
3729 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3731 const struct got_error *err = NULL;
3732 struct got_pathlist_entry *pe = NULL;
3733 struct got_pathlist_head *ignorelist;
3734 char *line = NULL, *pattern, *dirpath = NULL;
3735 size_t linesize = 0;
3738 ignorelist = calloc(1, sizeof(*ignorelist));
3739 if (ignorelist == NULL)
3740 return got_error_from_errno("calloc");
3741 TAILQ_INIT(ignorelist);
3743 while ((linelen = getline(&line, &linesize, f)) != -1) {
3744 if (linelen > 0 && line[linelen - 1] == '\n')
3745 line[linelen - 1] = '\0';
3747 /* Git's ignores may contain comments. */
3751 /* Git's negated patterns are not (yet?) supported. */
3755 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3757 err = got_error_from_errno("asprintf");
3760 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3765 err = got_error_from_errno("getline");
3769 dirpath = strdup(path);
3770 if (dirpath == NULL) {
3771 err = got_error_from_errno("strdup");
3774 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3777 if (err || pe == NULL) {
3779 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3786 match_path(const char *pattern, size_t pattern_len, const char *path,
3792 * Trailing slashes signify directories.
3793 * Append a * to make such patterns conform to fnmatch rules.
3795 if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
3796 if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
3797 return FNM_NOMATCH; /* XXX */
3799 return fnmatch(buf, path, flags);
3802 return fnmatch(pattern, path, flags);
3806 match_ignores(struct got_pathlist_head *ignores, const char *path)
3808 struct got_pathlist_entry *pe;
3810 /* Handle patterns which match in all directories. */
3811 TAILQ_FOREACH(pe, ignores, entry) {
3812 struct got_pathlist_head *ignorelist = pe->data;
3813 struct got_pathlist_entry *pi;
3815 TAILQ_FOREACH(pi, ignorelist, entry) {
3818 if (pi->path_len < 3 ||
3819 strncmp(pi->path, "**/", 3) != 0)
3823 if (match_path(pi->path + 3,
3824 pi->path_len - 3, p,
3825 FNM_PATHNAME | FNM_LEADING_DIR)) {
3826 /* Retry in next directory. */
3827 while (*p && *p != '/')
3839 * The ignores pathlist contains ignore lists from children before
3840 * parents, so we can find the most specific ignorelist by walking
3841 * ignores backwards.
3843 pe = TAILQ_LAST(ignores, got_pathlist_head);
3845 if (got_path_is_child(path, pe->path, pe->path_len)) {
3846 struct got_pathlist_head *ignorelist = pe->data;
3847 struct got_pathlist_entry *pi;
3848 TAILQ_FOREACH(pi, ignorelist, entry) {
3849 int flags = FNM_LEADING_DIR;
3850 if (strstr(pi->path, "/**/") == NULL)
3851 flags |= FNM_PATHNAME;
3852 if (match_path(pi->path, pi->path_len,
3858 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3864 static const struct got_error *
3865 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3866 const char *path, int dirfd, const char *ignores_filename)
3868 const struct got_error *err = NULL;
3871 FILE *ignoresfile = NULL;
3873 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3874 path[0] ? "/" : "", ignores_filename) == -1)
3875 return got_error_from_errno("asprintf");
3878 fd = openat(dirfd, ignores_filename,
3879 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3881 if (errno != ENOENT && errno != EACCES)
3882 err = got_error_from_errno2("openat",
3885 ignoresfile = fdopen(fd, "r");
3886 if (ignoresfile == NULL)
3887 err = got_error_from_errno2("fdopen",
3891 err = read_ignores(ignores, path, ignoresfile);
3895 ignoresfile = fopen(ignorespath, "re");
3896 if (ignoresfile == NULL) {
3897 if (errno != ENOENT && errno != EACCES)
3898 err = got_error_from_errno2("fopen",
3901 err = read_ignores(ignores, path, ignoresfile);
3904 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3905 err = got_error_from_errno2("fclose", path);
3906 if (fd != -1 && close(fd) == -1 && err == NULL)
3907 err = got_error_from_errno2("close", path);
3912 static const struct got_error *
3913 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3916 const struct got_error *err = NULL;
3917 struct diff_dir_cb_arg *a = arg;
3924 err = a->cancel_cb(a->cancel_arg);
3929 if (parent_path[0]) {
3930 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3931 return got_error_from_errno("asprintf");
3936 if (de->d_type == DT_DIR) {
3937 if (!a->no_ignores && ignore != NULL &&
3938 match_ignores(a->ignores, path))
3940 } else if (!match_ignores(a->ignores, path) &&
3941 got_path_is_child(path, a->status_path, a->status_path_len))
3942 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3943 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3949 static const struct got_error *
3950 status_traverse(void *arg, const char *path, int dirfd)
3952 const struct got_error *err = NULL;
3953 struct diff_dir_cb_arg *a = arg;
3958 err = add_ignores(a->ignores, a->worktree->root_path,
3959 path, dirfd, ".cvsignore");
3963 err = add_ignores(a->ignores, a->worktree->root_path, path,
3964 dirfd, ".gitignore");
3969 static const struct got_error *
3970 report_single_file_status(const char *path, const char *ondisk_path,
3971 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3972 void *status_arg, struct got_repository *repo, int report_unchanged,
3973 struct got_pathlist_head *ignores, int no_ignores)
3975 struct got_fileindex_entry *ie;
3978 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3980 return report_file_status(ie, ondisk_path, -1, NULL,
3981 status_cb, status_arg, repo, report_unchanged);
3983 if (lstat(ondisk_path, &sb) == -1) {
3984 if (errno != ENOENT)
3985 return got_error_from_errno2("lstat", ondisk_path);
3986 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3987 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3990 if (!no_ignores && match_ignores(ignores, path))
3993 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3994 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3995 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
4000 static const struct got_error *
4001 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
4002 const char *root_path, const char *path)
4004 const struct got_error *err;
4005 char *parent_path, *next_parent_path = NULL;
4007 err = add_ignores(ignores, root_path, "", -1,
4012 err = add_ignores(ignores, root_path, "", -1,
4017 err = got_path_dirname(&parent_path, path);
4019 if (err->code == GOT_ERR_BAD_PATH)
4020 return NULL; /* cannot traverse parent */
4024 err = add_ignores(ignores, root_path, parent_path, -1,
4028 err = add_ignores(ignores, root_path, parent_path, -1,
4032 err = got_path_dirname(&next_parent_path, parent_path);
4034 if (err->code == GOT_ERR_BAD_PATH)
4035 err = NULL; /* traversed everything */
4038 if (got_path_is_root_dir(parent_path))
4041 parent_path = next_parent_path;
4042 next_parent_path = NULL;
4046 free(next_parent_path);
4050 struct find_missing_children_args {
4051 const char *parent_path;
4053 struct got_pathlist_head *children;
4054 got_cancel_cb cancel_cb;
4058 static const struct got_error *
4059 find_missing_children(void *arg, struct got_fileindex_entry *ie)
4061 const struct got_error *err = NULL;
4062 struct find_missing_children_args *a = arg;
4065 err = a->cancel_cb(a->cancel_arg);
4070 if (got_path_is_child(ie->path, a->parent_path, a->parent_len))
4071 err = got_pathlist_append(a->children, ie->path, NULL);
4076 static const struct got_error *
4077 report_children(struct got_pathlist_head *children,
4078 struct got_worktree *worktree, struct got_fileindex *fileindex,
4079 struct got_repository *repo, int is_root_dir, int report_unchanged,
4080 struct got_pathlist_head *ignores, int no_ignores,
4081 got_worktree_status_cb status_cb, void *status_arg,
4082 got_cancel_cb cancel_cb, void *cancel_arg)
4084 const struct got_error *err = NULL;
4085 struct got_pathlist_entry *pe;
4086 char *ondisk_path = NULL;
4088 TAILQ_FOREACH(pe, children, entry) {
4090 err = cancel_cb(cancel_arg);
4095 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
4096 !is_root_dir ? "/" : "", pe->path) == -1) {
4097 err = got_error_from_errno("asprintf");
4102 err = report_single_file_status(pe->path, ondisk_path,
4103 fileindex, status_cb, status_arg, repo, report_unchanged,
4104 ignores, no_ignores);
4116 static const struct got_error *
4117 worktree_status(struct got_worktree *worktree, const char *path,
4118 struct got_fileindex *fileindex, struct got_repository *repo,
4119 got_worktree_status_cb status_cb, void *status_arg,
4120 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
4121 int report_unchanged)
4123 const struct got_error *err = NULL;
4125 struct got_fileindex_diff_dir_cb fdiff_cb;
4126 struct diff_dir_cb_arg arg;
4127 char *ondisk_path = NULL;
4128 struct got_pathlist_head ignores, missing_children;
4129 struct got_fileindex_entry *ie;
4131 TAILQ_INIT(&ignores);
4132 TAILQ_INIT(&missing_children);
4134 if (asprintf(&ondisk_path, "%s%s%s",
4135 worktree->root_path, path[0] ? "/" : "", path) == -1)
4136 return got_error_from_errno("asprintf");
4138 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
4140 err = report_single_file_status(path, ondisk_path,
4141 fileindex, status_cb, status_arg, repo,
4142 report_unchanged, &ignores, no_ignores);
4145 struct find_missing_children_args fmca;
4146 fmca.parent_path = path;
4147 fmca.parent_len = strlen(path);
4148 fmca.children = &missing_children;
4149 fmca.cancel_cb = cancel_cb;
4150 fmca.cancel_arg = cancel_arg;
4151 err = got_fileindex_for_each_entry_safe(fileindex,
4152 find_missing_children, &fmca);
4157 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
4159 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
4160 !got_err_open_nofollow_on_symlink())
4161 err = got_error_from_errno2("open", ondisk_path);
4164 err = add_ignores_from_parent_paths(&ignores,
4165 worktree->root_path, ondisk_path);
4169 if (TAILQ_EMPTY(&missing_children)) {
4170 err = report_single_file_status(path,
4171 ondisk_path, fileindex,
4172 status_cb, status_arg, repo,
4173 report_unchanged, &ignores, no_ignores);
4177 err = report_children(&missing_children,
4178 worktree, fileindex, repo,
4179 (path[0] == '\0'), report_unchanged,
4180 &ignores, no_ignores,
4181 status_cb, status_arg,
4182 cancel_cb, cancel_arg);
4188 fdiff_cb.diff_old_new = status_old_new;
4189 fdiff_cb.diff_old = status_old;
4190 fdiff_cb.diff_new = status_new;
4191 fdiff_cb.diff_traverse = status_traverse;
4192 arg.fileindex = fileindex;
4193 arg.worktree = worktree;
4194 arg.status_path = path;
4195 arg.status_path_len = strlen(path);
4197 arg.status_cb = status_cb;
4198 arg.status_arg = status_arg;
4199 arg.cancel_cb = cancel_cb;
4200 arg.cancel_arg = cancel_arg;
4201 arg.report_unchanged = report_unchanged;
4202 arg.no_ignores = no_ignores;
4204 err = add_ignores_from_parent_paths(&ignores,
4205 worktree->root_path, path);
4209 arg.ignores = &ignores;
4210 err = got_fileindex_diff_dir(fileindex, fd,
4211 worktree->root_path, path, repo, &fdiff_cb, &arg);
4214 free_ignores(&ignores);
4215 got_pathlist_free(&missing_children, GOT_PATHLIST_FREE_NONE);
4216 if (fd != -1 && close(fd) == -1 && err == NULL)
4217 err = got_error_from_errno("close");
4222 const struct got_error *
4223 got_worktree_status(struct got_worktree *worktree,
4224 struct got_pathlist_head *paths, struct got_repository *repo,
4225 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
4226 got_cancel_cb cancel_cb, void *cancel_arg)
4228 const struct got_error *err = NULL;
4229 char *fileindex_path = NULL;
4230 struct got_fileindex *fileindex = NULL;
4231 struct got_pathlist_entry *pe;
4233 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4237 TAILQ_FOREACH(pe, paths, entry) {
4238 err = worktree_status(worktree, pe->path, fileindex, repo,
4239 status_cb, status_arg, cancel_cb, cancel_arg,
4244 free(fileindex_path);
4245 got_fileindex_free(fileindex);
4249 const struct got_error *
4250 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
4253 const struct got_error *err = NULL;
4254 char *resolved = NULL, *cwd = NULL, *path = NULL;
4257 char *abspath = NULL;
4258 char canonpath[PATH_MAX];
4262 cwd = getcwd(NULL, 0);
4264 return got_error_from_errno("getcwd");
4266 if (lstat(arg, &sb) == -1) {
4267 if (errno != ENOENT) {
4268 err = got_error_from_errno2("lstat", arg);
4273 if (S_ISLNK(sb.st_mode)) {
4275 * We cannot use realpath(3) with symlinks since we want to
4276 * operate on the symlink itself.
4277 * But we can make the path absolute, assuming it is relative
4278 * to the current working directory, and then canonicalize it.
4280 if (!got_path_is_absolute(arg)) {
4281 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4282 err = got_error_from_errno("asprintf");
4287 err = got_canonpath(abspath ? abspath : arg, canonpath,
4291 resolved = strdup(canonpath);
4292 if (resolved == NULL) {
4293 err = got_error_from_errno("strdup");
4297 resolved = realpath(arg, NULL);
4298 if (resolved == NULL) {
4299 if (errno != ENOENT) {
4300 err = got_error_from_errno2("realpath", arg);
4303 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4304 err = got_error_from_errno("asprintf");
4307 err = got_canonpath(abspath, canonpath,
4311 resolved = strdup(canonpath);
4312 if (resolved == NULL) {
4313 err = got_error_from_errno("strdup");
4319 if (strncmp(got_worktree_get_root_path(worktree), resolved,
4320 strlen(got_worktree_get_root_path(worktree)))) {
4321 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
4325 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
4326 err = got_path_skip_common_ancestor(&path,
4327 got_worktree_get_root_path(worktree), resolved);
4333 err = got_error_from_errno("strdup");
4338 /* XXX status walk can't deal with trailing slash! */
4340 while (len > 0 && path[len - 1] == '/') {
4341 path[len - 1] = '\0';
4355 struct schedule_addition_args {
4356 struct got_worktree *worktree;
4357 struct got_fileindex *fileindex;
4358 got_worktree_checkout_cb progress_cb;
4360 struct got_repository *repo;
4364 add_noop_status(unsigned char status)
4366 return (status == GOT_STATUS_ADD ||
4367 status == GOT_STATUS_MODIFY ||
4368 status == GOT_STATUS_CONFLICT ||
4369 status == GOT_STATUS_MODE_CHANGE ||
4370 status == GOT_STATUS_NO_CHANGE);
4373 static const struct got_error *
4374 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4375 const char *relpath, struct got_object_id *blob_id,
4376 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4377 int dirfd, const char *de_name)
4379 struct schedule_addition_args *a = arg;
4380 const struct got_error *err = NULL;
4381 struct got_fileindex_entry *ie;
4385 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4387 return got_error_from_errno("asprintf");
4389 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4391 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4395 /* Re-adding an existing entry is a no-op. */
4396 if (staged_status == GOT_STATUS_NO_CHANGE &&
4397 add_noop_status(status))
4399 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4404 if (status != GOT_STATUS_UNVERSIONED) {
4405 if (status == GOT_STATUS_NONEXISTENT)
4406 err = got_error_set_errno(ENOENT, ondisk_path);
4408 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4412 err = got_fileindex_entry_alloc(&ie, relpath);
4415 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4416 relpath, NULL, NULL, 1);
4418 got_fileindex_entry_free(ie);
4421 err = got_fileindex_entry_add(a->fileindex, ie);
4423 got_fileindex_entry_free(ie);
4430 if (staged_status == GOT_STATUS_NO_CHANGE && add_noop_status(status))
4432 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4435 const struct got_error *
4436 got_worktree_schedule_add(struct got_worktree *worktree,
4437 struct got_pathlist_head *paths,
4438 got_worktree_checkout_cb progress_cb, void *progress_arg,
4439 struct got_repository *repo, int no_ignores)
4441 struct got_fileindex *fileindex = NULL;
4442 char *fileindex_path = NULL;
4443 const struct got_error *err = NULL, *sync_err, *unlockerr;
4444 struct got_pathlist_entry *pe;
4445 struct schedule_addition_args saa;
4447 err = lock_worktree(worktree, LOCK_EX);
4451 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4455 saa.worktree = worktree;
4456 saa.fileindex = fileindex;
4457 saa.progress_cb = progress_cb;
4458 saa.progress_arg = progress_arg;
4461 TAILQ_FOREACH(pe, paths, entry) {
4462 err = worktree_status(worktree, pe->path, fileindex, repo,
4463 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4467 sync_err = sync_fileindex(fileindex, fileindex_path);
4468 if (sync_err && err == NULL)
4471 free(fileindex_path);
4473 got_fileindex_free(fileindex);
4474 unlockerr = lock_worktree(worktree, LOCK_SH);
4475 if (unlockerr && err == NULL)
4480 struct schedule_deletion_args {
4481 struct got_worktree *worktree;
4482 struct got_fileindex *fileindex;
4483 got_worktree_delete_cb progress_cb;
4485 struct got_repository *repo;
4486 int delete_local_mods;
4488 int ignore_missing_paths;
4489 const char *status_path;
4490 size_t status_path_len;
4491 const char *status_codes;
4494 static const struct got_error *
4495 schedule_for_deletion(void *arg, unsigned char status,
4496 unsigned char staged_status, const char *relpath,
4497 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4498 struct got_object_id *commit_id, int dirfd, const char *de_name)
4500 struct schedule_deletion_args *a = arg;
4501 const struct got_error *err = NULL;
4502 struct got_fileindex_entry *ie = NULL;
4506 if (status == GOT_STATUS_NONEXISTENT) {
4507 if (a->ignore_missing_paths)
4509 return got_error_set_errno(ENOENT, relpath);
4512 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4514 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4516 staged_status = get_staged_status(ie);
4517 if (staged_status != GOT_STATUS_NO_CHANGE) {
4518 if (staged_status == GOT_STATUS_DELETE)
4520 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4523 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4525 return got_error_from_errno("asprintf");
4527 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4532 if (a->status_codes) {
4533 size_t ncodes = strlen(a->status_codes);
4535 for (i = 0; i < ncodes ; i++) {
4536 if (status == a->status_codes[i])
4540 /* Do not delete files in non-matching status. */
4544 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4545 a->status_codes[i] != GOT_STATUS_MISSING) {
4546 static char msg[64];
4547 snprintf(msg, sizeof(msg),
4548 "invalid status code '%c'", a->status_codes[i]);
4549 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4554 if (status != GOT_STATUS_NO_CHANGE) {
4555 if (status == GOT_STATUS_DELETE)
4557 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4558 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4561 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4562 err = got_error_set_errno(ENOENT, relpath);
4565 if (status != GOT_STATUS_MODIFY &&
4566 status != GOT_STATUS_MISSING) {
4567 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4572 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4576 if (unlinkat(dirfd, de_name, 0) == -1) {
4577 err = got_error_from_errno2("unlinkat",
4581 } else if (unlink(ondisk_path) == -1) {
4582 err = got_error_from_errno2("unlink", ondisk_path);
4586 root_len = strlen(a->worktree->root_path);
4590 err = got_path_dirname(&parent, ondisk_path);
4594 ondisk_path = parent;
4595 if (got_path_cmp(ondisk_path, a->status_path,
4596 strlen(ondisk_path), a->status_path_len) != 0 &&
4597 !got_path_is_child(ondisk_path, a->status_path,
4598 a->status_path_len))
4600 if (rmdir(ondisk_path) == -1) {
4601 if (errno != ENOTEMPTY)
4602 err = got_error_from_errno2("rmdir",
4606 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4607 strlen(ondisk_path), root_len) != 0);
4610 if (got_fileindex_entry_has_blob(ie))
4611 got_fileindex_entry_mark_deleted_from_disk(ie);
4613 got_fileindex_entry_remove(a->fileindex, ie);
4618 if (status == GOT_STATUS_DELETE)
4620 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4621 staged_status, relpath);
4624 const struct got_error *
4625 got_worktree_schedule_delete(struct got_worktree *worktree,
4626 struct got_pathlist_head *paths, int delete_local_mods,
4627 const char *status_codes,
4628 got_worktree_delete_cb progress_cb, void *progress_arg,
4629 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4631 struct got_fileindex *fileindex = NULL;
4632 char *fileindex_path = NULL;
4633 const struct got_error *err = NULL, *sync_err, *unlockerr;
4634 struct got_pathlist_entry *pe;
4635 struct schedule_deletion_args sda;
4637 err = lock_worktree(worktree, LOCK_EX);
4641 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4645 sda.worktree = worktree;
4646 sda.fileindex = fileindex;
4647 sda.progress_cb = progress_cb;
4648 sda.progress_arg = progress_arg;
4650 sda.delete_local_mods = delete_local_mods;
4651 sda.keep_on_disk = keep_on_disk;
4652 sda.ignore_missing_paths = ignore_missing_paths;
4653 sda.status_codes = status_codes;
4655 TAILQ_FOREACH(pe, paths, entry) {
4656 char *ondisk_status_path;
4658 if (asprintf(&ondisk_status_path, "%s%s%s",
4659 got_worktree_get_root_path(worktree),
4660 pe->path[0] == '\0' ? "" : "/", pe->path) == -1) {
4661 err = got_error_from_errno("asprintf");
4664 sda.status_path = ondisk_status_path;
4665 sda.status_path_len = strlen(ondisk_status_path);
4666 err = worktree_status(worktree, pe->path, fileindex, repo,
4667 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4668 free(ondisk_status_path);
4672 sync_err = sync_fileindex(fileindex, fileindex_path);
4673 if (sync_err && err == NULL)
4676 free(fileindex_path);
4678 got_fileindex_free(fileindex);
4679 unlockerr = lock_worktree(worktree, LOCK_SH);
4680 if (unlockerr && err == NULL)
4685 static const struct got_error *
4686 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4688 const struct got_error *err = NULL;
4690 size_t linesize = 0, n;
4693 linelen = getline(&line, &linesize, infile);
4694 if (linelen == -1) {
4695 if (ferror(infile)) {
4696 err = got_error_from_errno("getline");
4702 n = fwrite(line, 1, linelen, outfile);
4704 err = got_ferror(outfile, GOT_ERR_IO);
4709 n = fwrite(line, 1, linelen, rejectfile);
4711 err = got_ferror(rejectfile, GOT_ERR_IO);
4718 static const struct got_error *
4719 skip_one_line(FILE *f)
4722 size_t linesize = 0;
4725 linelen = getline(&line, &linesize, f);
4726 if (linelen == -1) {
4728 return got_error_from_errno("getline");
4735 static const struct got_error *
4736 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4737 int start_old, int end_old, int start_new, int end_new,
4738 FILE *outfile, FILE *rejectfile)
4740 const struct got_error *err;
4742 /* Copy old file's lines leading up to patch. */
4743 while (!feof(f1) && *line_cur1 < start_old) {
4744 err = copy_one_line(f1, outfile, NULL);
4749 /* Skip new file's lines leading up to patch. */
4750 while (!feof(f2) && *line_cur2 < start_new) {
4752 err = copy_one_line(f2, NULL, rejectfile);
4754 err = skip_one_line(f2);
4759 /* Copy patched lines. */
4760 while (!feof(f2) && *line_cur2 <= end_new) {
4761 err = copy_one_line(f2, outfile, NULL);
4766 /* Skip over old file's replaced lines. */
4767 while (!feof(f1) && *line_cur1 <= end_old) {
4769 err = copy_one_line(f1, NULL, rejectfile);
4771 err = skip_one_line(f1);
4780 static const struct got_error *
4781 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4782 FILE *outfile, FILE *rejectfile)
4784 const struct got_error *err;
4787 /* Copy old file's lines until EOF. */
4789 err = copy_one_line(f1, outfile, NULL);
4796 /* Copy new file's lines until EOF. */
4798 err = copy_one_line(f2, NULL, rejectfile);
4808 static const struct got_error *
4809 apply_or_reject_change(int *choice, int *nchunks_used,
4810 struct diff_result *diff_result, int n,
4811 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4812 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4813 got_worktree_patch_cb patch_cb, void *patch_arg)
4815 const struct got_error *err = NULL;
4816 struct diff_chunk_context cc = {};
4817 int start_old, end_old, start_new, end_new;
4819 struct diff_output_unidiff_state *diff_state;
4820 struct diff_input_info diff_info;
4823 *choice = GOT_PATCH_CHOICE_NONE;
4825 /* Get changed line numbers without context lines for copy_change(). */
4826 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4827 start_old = cc.left.start;
4828 end_old = cc.left.end;
4829 start_new = cc.right.start;
4830 end_new = cc.right.end;
4832 /* Get the same change with context lines for display. */
4833 memset(&cc, 0, sizeof(cc));
4834 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4836 memset(&diff_info, 0, sizeof(diff_info));
4837 diff_info.left_path = relpath;
4838 diff_info.right_path = relpath;
4840 diff_state = diff_output_unidiff_state_alloc();
4841 if (diff_state == NULL)
4842 return got_error_set_errno(ENOMEM,
4843 "diff_output_unidiff_state_alloc");
4845 hunkfile = got_opentemp();
4846 if (hunkfile == NULL) {
4847 err = got_error_from_errno("got_opentemp");
4851 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4853 if (rc != DIFF_RC_OK) {
4854 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4858 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4859 err = got_ferror(hunkfile, GOT_ERR_IO);
4863 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4864 hunkfile, changeno, nchanges);
4869 case GOT_PATCH_CHOICE_YES:
4870 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4871 end_old, start_new, end_new, outfile, rejectfile);
4873 case GOT_PATCH_CHOICE_NO:
4874 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4875 end_old, start_new, end_new, rejectfile, outfile);
4877 case GOT_PATCH_CHOICE_QUIT:
4880 err = got_error(GOT_ERR_PATCH_CHOICE);
4884 diff_output_unidiff_state_free(diff_state);
4885 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4886 err = got_error_from_errno("fclose");
4890 struct revert_file_args {
4891 struct got_worktree *worktree;
4892 struct got_fileindex *fileindex;
4893 got_worktree_checkout_cb progress_cb;
4895 got_worktree_patch_cb patch_cb;
4897 struct got_repository *repo;
4898 int unlink_added_files;
4899 struct got_pathlist_head *added_files_to_unlink;
4902 static const struct got_error *
4903 create_patched_content(char **path_outfile, int reverse_patch,
4904 struct got_object_id *blob_id, const char *path2,
4905 int dirfd2, const char *de_name2,
4906 const char *relpath, struct got_repository *repo,
4907 got_worktree_patch_cb patch_cb, void *patch_arg)
4909 const struct got_error *err, *free_err;
4910 struct got_blob_object *blob = NULL;
4911 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4912 int fd = -1, fd2 = -1;
4913 char link_target[PATH_MAX];
4914 ssize_t link_len = 0;
4915 char *path1 = NULL, *id_str = NULL;
4917 struct got_diffreg_result *diffreg_result = NULL;
4918 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4919 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4921 *path_outfile = NULL;
4923 err = got_object_id_str(&id_str, blob_id);
4928 fd2 = openat(dirfd2, de_name2,
4929 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4931 if (!got_err_open_nofollow_on_symlink()) {
4932 err = got_error_from_errno2("openat", path2);
4935 link_len = readlinkat(dirfd2, de_name2,
4936 link_target, sizeof(link_target));
4937 if (link_len == -1) {
4938 return got_error_from_errno2("readlinkat",
4941 sb2.st_mode = S_IFLNK;
4942 sb2.st_size = link_len;
4945 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4947 if (!got_err_open_nofollow_on_symlink()) {
4948 err = got_error_from_errno2("open", path2);
4951 link_len = readlink(path2, link_target,
4952 sizeof(link_target));
4954 return got_error_from_errno2("readlink", path2);
4955 sb2.st_mode = S_IFLNK;
4956 sb2.st_size = link_len;
4960 if (fstat(fd2, &sb2) == -1) {
4961 err = got_error_from_errno2("fstat", path2);
4965 f2 = fdopen(fd2, "r");
4967 err = got_error_from_errno2("fdopen", path2);
4973 f2 = got_opentemp();
4975 err = got_error_from_errno2("got_opentemp", path2);
4978 n = fwrite(link_target, 1, link_len, f2);
4979 if (n != link_len) {
4980 err = got_ferror(f2, GOT_ERR_IO);
4983 if (fflush(f2) == EOF) {
4984 err = got_error_from_errno("fflush");
4990 fd = got_opentempfd();
4992 err = got_error_from_errno("got_opentempfd");
4996 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
5000 err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
5004 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
5008 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
5009 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
5013 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
5018 if (fseek(f1, 0L, SEEK_SET) == -1)
5019 return got_ferror(f1, GOT_ERR_IO);
5020 if (fseek(f2, 0L, SEEK_SET) == -1)
5021 return got_ferror(f2, GOT_ERR_IO);
5023 /* Count the number of actual changes in the diff result. */
5024 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
5025 struct diff_chunk_context cc = {};
5026 diff_chunk_context_load_change(&cc, &nchunks_used,
5027 diffreg_result->result, n, 0);
5030 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
5032 err = apply_or_reject_change(&choice, &nchunks_used,
5033 diffreg_result->result, n, relpath, f1, f2,
5034 &line_cur1, &line_cur2,
5035 reverse_patch ? NULL : outfile,
5036 reverse_patch ? outfile : NULL,
5037 ++i, nchanges, patch_cb, patch_arg);
5040 if (choice == GOT_PATCH_CHOICE_YES)
5042 else if (choice == GOT_PATCH_CHOICE_QUIT)
5046 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
5047 reverse_patch ? NULL : outfile,
5048 reverse_patch ? outfile : NULL);
5052 if (!S_ISLNK(sb2.st_mode)) {
5055 mode = apply_umask(sb2.st_mode);
5056 if (fchmod(fileno(outfile), mode) == -1) {
5057 err = got_error_from_errno2("fchmod", path2);
5064 if (fd != -1 && close(fd) == -1 && err == NULL)
5065 err = got_error_from_errno("close");
5067 got_object_blob_close(blob);
5068 free_err = got_diffreg_result_free(diffreg_result);
5071 if (f1 && fclose(f1) == EOF && err == NULL)
5072 err = got_error_from_errno2("fclose", path1);
5073 if (f2 && fclose(f2) == EOF && err == NULL)
5074 err = got_error_from_errno2("fclose", path2);
5075 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5076 err = got_error_from_errno2("close", path2);
5077 if (outfile && fclose(outfile) == EOF && err == NULL)
5078 err = got_error_from_errno2("fclose", *path_outfile);
5079 if (path1 && unlink(path1) == -1 && err == NULL)
5080 err = got_error_from_errno2("unlink", path1);
5081 if (err || !have_content) {
5082 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
5083 err = got_error_from_errno2("unlink", *path_outfile);
5084 free(*path_outfile);
5085 *path_outfile = NULL;
5091 static const struct got_error *
5092 revert_file(void *arg, unsigned char status, unsigned char staged_status,
5093 const char *relpath, struct got_object_id *blob_id,
5094 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5095 int dirfd, const char *de_name)
5097 struct revert_file_args *a = arg;
5098 const struct got_error *err = NULL;
5099 char *parent_path = NULL;
5100 struct got_fileindex_entry *ie;
5101 struct got_commit_object *base_commit = NULL;
5102 struct got_tree_object *tree = NULL;
5103 struct got_object_id *tree_id = NULL;
5104 const struct got_tree_entry *te = NULL;
5105 char *tree_path = NULL, *te_name;
5106 char *ondisk_path = NULL, *path_content = NULL;
5107 struct got_blob_object *blob = NULL;
5110 /* Reverting a staged deletion is a no-op. */
5111 if (status == GOT_STATUS_DELETE &&
5112 staged_status != GOT_STATUS_NO_CHANGE)
5115 if (status == GOT_STATUS_UNVERSIONED)
5116 return (*a->progress_cb)(a->progress_arg,
5117 GOT_STATUS_UNVERSIONED, relpath);
5119 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5121 return got_error_path(relpath, GOT_ERR_BAD_PATH);
5123 /* Construct in-repository path of tree which contains this blob. */
5124 err = got_path_dirname(&parent_path, ie->path);
5126 if (err->code != GOT_ERR_BAD_PATH)
5128 parent_path = strdup("/");
5129 if (parent_path == NULL) {
5130 err = got_error_from_errno("strdup");
5134 if (got_path_is_root_dir(a->worktree->path_prefix)) {
5135 tree_path = strdup(parent_path);
5136 if (tree_path == NULL) {
5137 err = got_error_from_errno("strdup");
5141 if (got_path_is_root_dir(parent_path)) {
5142 tree_path = strdup(a->worktree->path_prefix);
5143 if (tree_path == NULL) {
5144 err = got_error_from_errno("strdup");
5148 if (asprintf(&tree_path, "%s/%s",
5149 a->worktree->path_prefix, parent_path) == -1) {
5150 err = got_error_from_errno("asprintf");
5156 err = got_object_open_as_commit(&base_commit, a->repo,
5157 a->worktree->base_commit_id);
5161 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
5163 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
5164 (status == GOT_STATUS_ADD ||
5165 staged_status == GOT_STATUS_ADD)))
5168 err = got_object_open_as_tree(&tree, a->repo, tree_id);
5172 err = got_path_basename(&te_name, ie->path);
5176 te = got_object_tree_find_entry(tree, te_name);
5178 if (te == NULL && status != GOT_STATUS_ADD &&
5179 staged_status != GOT_STATUS_ADD) {
5180 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
5186 case GOT_STATUS_ADD:
5188 int choice = GOT_PATCH_CHOICE_NONE;
5189 err = (*a->patch_cb)(&choice, a->patch_arg,
5190 status, ie->path, NULL, 1, 1);
5193 if (choice != GOT_PATCH_CHOICE_YES)
5196 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
5200 got_fileindex_entry_remove(a->fileindex, ie);
5201 if (a->unlink_added_files) {
5202 int do_unlink = a->added_files_to_unlink ? 0 : 1;
5204 if (a->added_files_to_unlink) {
5205 struct got_pathlist_entry *pe;
5207 TAILQ_FOREACH(pe, a->added_files_to_unlink,
5209 if (got_path_cmp(pe->path, relpath,
5210 pe->path_len, strlen(relpath)))
5218 if (asprintf(&ondisk_path, "%s/%s",
5219 got_worktree_get_root_path(a->worktree),
5221 err = got_error_from_errno("asprintf");
5224 if (unlink(ondisk_path) == -1) {
5225 err = got_error_from_errno2("unlink",
5232 case GOT_STATUS_DELETE:
5234 int choice = GOT_PATCH_CHOICE_NONE;
5235 err = (*a->patch_cb)(&choice, a->patch_arg,
5236 status, ie->path, NULL, 1, 1);
5239 if (choice != GOT_PATCH_CHOICE_YES)
5243 case GOT_STATUS_MODIFY:
5244 case GOT_STATUS_MODE_CHANGE:
5245 case GOT_STATUS_CONFLICT:
5246 case GOT_STATUS_MISSING: {
5247 struct got_object_id id;
5248 if (staged_status == GOT_STATUS_ADD ||
5249 staged_status == GOT_STATUS_MODIFY)
5250 got_fileindex_entry_get_staged_blob_id(&id, ie);
5252 got_fileindex_entry_get_blob_id(&id, ie);
5253 fd = got_opentempfd();
5255 err = got_error_from_errno("got_opentempfd");
5259 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
5263 if (asprintf(&ondisk_path, "%s/%s",
5264 got_worktree_get_root_path(a->worktree), relpath) == -1) {
5265 err = got_error_from_errno("asprintf");
5269 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
5270 status == GOT_STATUS_CONFLICT)) {
5271 int is_bad_symlink = 0;
5272 err = create_patched_content(&path_content, 1, &id,
5273 ondisk_path, dirfd, de_name, ie->path, a->repo,
5274 a->patch_cb, a->patch_arg);
5275 if (err || path_content == NULL)
5277 if (te && S_ISLNK(te->mode)) {
5278 if (unlink(path_content) == -1) {
5279 err = got_error_from_errno2("unlink",
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 if (rename(path_content, ondisk_path) == -1) {
5289 err = got_error_from_errno3("rename",
5290 path_content, ondisk_path);
5295 int is_bad_symlink = 0;
5296 if (te && S_ISLNK(te->mode)) {
5297 err = install_symlink(&is_bad_symlink,
5298 a->worktree, ondisk_path, ie->path,
5299 blob, 0, 1, 0, 0, a->repo,
5300 a->progress_cb, a->progress_arg);
5302 err = install_blob(a->worktree, ondisk_path,
5304 te ? te->mode : GOT_DEFAULT_FILE_MODE,
5305 got_fileindex_perms_to_st(ie), blob,
5306 0, 1, 0, 0, NULL, a->repo,
5307 a->progress_cb, a->progress_arg);
5311 if (status == GOT_STATUS_DELETE ||
5312 status == GOT_STATUS_MODE_CHANGE) {
5313 err = got_fileindex_entry_update(ie,
5314 a->worktree->root_fd, relpath,
5316 a->worktree->base_commit_id->sha1, 1);
5320 if (is_bad_symlink) {
5321 got_fileindex_entry_filetype_set(ie,
5322 GOT_FILEIDX_MODE_BAD_SYMLINK);
5335 if (fd != -1 && close(fd) == -1 && err == NULL)
5336 err = got_error_from_errno("close");
5338 got_object_blob_close(blob);
5340 got_object_tree_close(tree);
5343 got_object_commit_close(base_commit);
5347 const struct got_error *
5348 got_worktree_revert(struct got_worktree *worktree,
5349 struct got_pathlist_head *paths,
5350 got_worktree_checkout_cb progress_cb, void *progress_arg,
5351 got_worktree_patch_cb patch_cb, void *patch_arg,
5352 struct got_repository *repo)
5354 struct got_fileindex *fileindex = NULL;
5355 char *fileindex_path = NULL;
5356 const struct got_error *err = NULL, *unlockerr = NULL;
5357 const struct got_error *sync_err = NULL;
5358 struct got_pathlist_entry *pe;
5359 struct revert_file_args rfa;
5361 err = lock_worktree(worktree, LOCK_EX);
5365 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5369 rfa.worktree = worktree;
5370 rfa.fileindex = fileindex;
5371 rfa.progress_cb = progress_cb;
5372 rfa.progress_arg = progress_arg;
5373 rfa.patch_cb = patch_cb;
5374 rfa.patch_arg = patch_arg;
5376 rfa.unlink_added_files = 0;
5377 TAILQ_FOREACH(pe, paths, entry) {
5378 err = worktree_status(worktree, pe->path, fileindex, repo,
5379 revert_file, &rfa, NULL, NULL, 1, 0);
5383 sync_err = sync_fileindex(fileindex, fileindex_path);
5384 if (sync_err && err == NULL)
5387 free(fileindex_path);
5389 got_fileindex_free(fileindex);
5390 unlockerr = lock_worktree(worktree, LOCK_SH);
5391 if (unlockerr && err == NULL)
5397 free_commitable(struct got_commitable *ct)
5400 free(ct->in_repo_path);
5401 free(ct->ondisk_path);
5403 free(ct->base_blob_id);
5404 free(ct->staged_blob_id);
5405 free(ct->base_commit_id);
5409 struct collect_commitables_arg {
5410 struct got_pathlist_head *commitable_paths;
5411 struct got_repository *repo;
5412 struct got_worktree *worktree;
5413 struct got_fileindex *fileindex;
5414 int have_staged_files;
5415 int allow_bad_symlinks;
5416 int diff_header_shown;
5417 int commit_conflicts;
5424 * Create a file which contains the target path of a symlink so we can feed
5425 * it as content to the diff engine.
5427 static const struct got_error *
5428 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5429 const char *abspath)
5431 const struct got_error *err = NULL;
5432 char target_path[PATH_MAX];
5433 ssize_t target_len, outlen;
5438 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5439 if (target_len == -1)
5440 return got_error_from_errno2("readlinkat", abspath);
5442 target_len = readlink(abspath, target_path, PATH_MAX);
5443 if (target_len == -1)
5444 return got_error_from_errno2("readlink", abspath);
5447 *fd = got_opentempfd();
5449 return got_error_from_errno("got_opentempfd");
5451 outlen = write(*fd, target_path, target_len);
5453 err = got_error_from_errno("got_opentempfd");
5457 if (lseek(*fd, 0, SEEK_SET) == -1) {
5458 err = got_error_from_errno2("lseek", abspath);
5469 static const struct got_error *
5470 append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5471 FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5472 int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5474 const struct got_error *err = NULL;
5475 struct got_blob_object *blob1 = NULL;
5476 int fd = -1, fd1 = -1, fd2 = -1;
5477 FILE *ondisk_file = NULL;
5478 char *label1 = NULL;
5482 char *id_str = NULL;
5484 memset(&sb, 0, sizeof(sb));
5487 if (ct->staged_status != GOT_STATUS_MODIFY &&
5488 ct->staged_status != GOT_STATUS_ADD &&
5489 ct->staged_status != GOT_STATUS_DELETE)
5492 if (ct->status != GOT_STATUS_MODIFY &&
5493 ct->status != GOT_STATUS_ADD &&
5494 ct->status != GOT_STATUS_DELETE &&
5495 ct->status != GOT_STATUS_CONFLICT)
5499 err = got_opentemp_truncate(f1);
5501 return got_error_from_errno("got_opentemp_truncate");
5502 err = got_opentemp_truncate(f2);
5504 return got_error_from_errno("got_opentemp_truncate");
5506 if (!*diff_header_shown) {
5507 err = got_object_id_str(&id_str, worktree->base_commit_id);
5510 fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5511 got_worktree_get_root_path(worktree));
5512 fprintf(diff_outfile, "commit - %s\n", id_str);
5513 fprintf(diff_outfile, "path + %s%s\n",
5514 got_worktree_get_root_path(worktree),
5515 diff_staged ? " (staged changes)" : "");
5516 *diff_header_shown = 1;
5520 const char *label1 = NULL, *label2 = NULL;
5521 switch (ct->staged_status) {
5522 case GOT_STATUS_MODIFY:
5526 case GOT_STATUS_ADD:
5529 case GOT_STATUS_DELETE:
5533 return got_error(GOT_ERR_FILE_STATUS);
5535 fd1 = got_opentempfd();
5537 err = got_error_from_errno("got_opentempfd");
5540 fd2 = got_opentempfd();
5542 err = got_error_from_errno("got_opentempfd");
5545 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5546 fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5547 label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5548 NULL, repo, diff_outfile);
5552 fd1 = got_opentempfd();
5554 err = got_error_from_errno("got_opentempfd");
5558 if (ct->status != GOT_STATUS_ADD) {
5559 err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5565 if (ct->status != GOT_STATUS_DELETE) {
5567 fd = openat(dirfd, de_name,
5568 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5570 if (!got_err_open_nofollow_on_symlink()) {
5571 err = got_error_from_errno2("openat",
5575 err = get_symlink_target_file(&fd, dirfd,
5576 de_name, ct->ondisk_path);
5581 fd = open(ct->ondisk_path,
5582 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5584 if (!got_err_open_nofollow_on_symlink()) {
5585 err = got_error_from_errno2("open",
5589 err = get_symlink_target_file(&fd, dirfd,
5590 de_name, ct->ondisk_path);
5595 if (fstatat(fd, ct->ondisk_path, &sb,
5596 AT_SYMLINK_NOFOLLOW) == -1) {
5597 err = got_error_from_errno2("fstatat", ct->ondisk_path);
5600 ondisk_file = fdopen(fd, "r");
5601 if (ondisk_file == NULL) {
5602 err = got_error_from_errno2("fdopen", ct->ondisk_path);
5610 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5616 err = got_diff_blob_file(blob1, f1, size1, label1,
5617 ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5618 GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5620 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5621 err = got_error_from_errno("close");
5622 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5623 err = got_error_from_errno("close");
5625 got_object_blob_close(blob1);
5626 if (fd != -1 && close(fd) == -1 && err == NULL)
5627 err = got_error_from_errno("close");
5628 if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5629 err = got_error_from_errno("fclose");
5633 static const struct got_error *
5634 collect_commitables(void *arg, unsigned char status,
5635 unsigned char staged_status, const char *relpath,
5636 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5637 struct got_object_id *commit_id, int dirfd, const char *de_name)
5639 struct collect_commitables_arg *a = arg;
5640 const struct got_error *err = NULL;
5641 struct got_commitable *ct = NULL;
5642 struct got_pathlist_entry *new = NULL;
5643 char *parent_path = NULL, *path = NULL;
5646 if (a->have_staged_files) {
5647 if (staged_status != GOT_STATUS_MODIFY &&
5648 staged_status != GOT_STATUS_ADD &&
5649 staged_status != GOT_STATUS_DELETE)
5652 if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5653 printf("C %s\n", relpath);
5654 return got_error(GOT_ERR_COMMIT_CONFLICT);
5657 if (status != GOT_STATUS_MODIFY &&
5658 status != GOT_STATUS_MODE_CHANGE &&
5659 status != GOT_STATUS_ADD &&
5660 status != GOT_STATUS_DELETE &&
5661 status != GOT_STATUS_CONFLICT)
5665 if (asprintf(&path, "/%s", relpath) == -1) {
5666 err = got_error_from_errno("asprintf");
5669 if (strcmp(path, "/") == 0) {
5670 parent_path = strdup("");
5671 if (parent_path == NULL)
5672 return got_error_from_errno("strdup");
5674 err = got_path_dirname(&parent_path, path);
5679 ct = calloc(1, sizeof(*ct));
5681 err = got_error_from_errno("calloc");
5685 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5687 err = got_error_from_errno("asprintf");
5691 if (staged_status == GOT_STATUS_ADD ||
5692 staged_status == GOT_STATUS_MODIFY) {
5693 struct got_fileindex_entry *ie;
5694 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5695 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5696 case GOT_FILEIDX_MODE_REGULAR_FILE:
5697 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5700 case GOT_FILEIDX_MODE_SYMLINK:
5704 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5707 ct->mode |= got_fileindex_entry_perms_get(ie);
5708 } else if (status != GOT_STATUS_DELETE &&
5709 staged_status != GOT_STATUS_DELETE) {
5711 if (fstatat(dirfd, de_name, &sb,
5712 AT_SYMLINK_NOFOLLOW) == -1) {
5713 err = got_error_from_errno2("fstatat",
5717 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5718 err = got_error_from_errno2("lstat", ct->ondisk_path);
5721 ct->mode = sb.st_mode;
5724 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5725 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5727 err = got_error_from_errno("asprintf");
5731 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5732 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5734 char target_path[PATH_MAX];
5736 target_len = readlink(ct->ondisk_path, target_path,
5737 sizeof(target_path));
5738 if (target_len == -1) {
5739 err = got_error_from_errno2("readlink",
5743 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5744 target_len, ct->ondisk_path, a->worktree->root_path,
5745 a->worktree->meta_dir);
5748 if (is_bad_symlink) {
5749 err = got_error_path(ct->ondisk_path,
5750 GOT_ERR_BAD_SYMLINK);
5756 ct->status = status;
5757 ct->staged_status = staged_status;
5758 ct->blob_id = NULL; /* will be filled in when blob gets created */
5759 if (ct->status != GOT_STATUS_ADD &&
5760 ct->staged_status != GOT_STATUS_ADD) {
5761 ct->base_blob_id = got_object_id_dup(blob_id);
5762 if (ct->base_blob_id == NULL) {
5763 err = got_error_from_errno("got_object_id_dup");
5766 ct->base_commit_id = got_object_id_dup(commit_id);
5767 if (ct->base_commit_id == NULL) {
5768 err = got_error_from_errno("got_object_id_dup");
5772 if (ct->staged_status == GOT_STATUS_ADD ||
5773 ct->staged_status == GOT_STATUS_MODIFY) {
5774 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5775 if (ct->staged_blob_id == NULL) {
5776 err = got_error_from_errno("got_object_id_dup");
5780 ct->path = strdup(path);
5781 if (ct->path == NULL) {
5782 err = got_error_from_errno("strdup");
5786 if (a->diff_outfile) {
5787 err = append_ct_diff(ct, &a->diff_header_shown,
5788 a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5789 a->have_staged_files, a->repo, a->worktree);
5794 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5796 if (ct && (err || new == NULL))
5797 free_commitable(ct);
5803 static const struct got_error *write_tree(struct got_object_id **, int *,
5804 struct got_tree_object *, const char *, struct got_pathlist_head *,
5805 got_worktree_status_cb status_cb, void *status_arg,
5806 struct got_repository *);
5808 static const struct got_error *
5809 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5810 struct got_tree_entry *te, const char *parent_path,
5811 struct got_pathlist_head *commitable_paths,
5812 got_worktree_status_cb status_cb, void *status_arg,
5813 struct got_repository *repo)
5815 const struct got_error *err = NULL;
5816 struct got_tree_object *subtree;
5819 if (asprintf(&subpath, "%s%s%s", parent_path,
5820 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5821 return got_error_from_errno("asprintf");
5823 err = got_object_open_as_tree(&subtree, repo, &te->id);
5827 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5828 commitable_paths, status_cb, status_arg, repo);
5829 got_object_tree_close(subtree);
5834 static const struct got_error *
5835 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5837 const struct got_error *err = NULL;
5838 char *ct_parent_path = NULL;
5842 if (strchr(ct->in_repo_path, '/') == NULL) {
5843 *match = got_path_is_root_dir(path);
5847 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5850 *match = (strcmp(path, ct_parent_path) == 0);
5851 free(ct_parent_path);
5856 get_ct_file_mode(struct got_commitable *ct)
5858 if (S_ISLNK(ct->mode))
5861 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5864 static const struct got_error *
5865 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5866 struct got_tree_entry *te, struct got_commitable *ct)
5868 const struct got_error *err = NULL;
5872 err = got_object_tree_entry_dup(new_te, te);
5876 (*new_te)->mode = get_ct_file_mode(ct);
5878 if (ct->staged_status == GOT_STATUS_MODIFY)
5879 memcpy(&(*new_te)->id, ct->staged_blob_id,
5880 sizeof((*new_te)->id));
5882 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5884 if (err && *new_te) {
5891 static const struct got_error *
5892 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5893 struct got_commitable *ct)
5895 const struct got_error *err = NULL;
5896 char *ct_name = NULL;
5900 *new_te = calloc(1, sizeof(**new_te));
5901 if (*new_te == NULL)
5902 return got_error_from_errno("calloc");
5904 err = got_path_basename(&ct_name, ct->path);
5907 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5908 sizeof((*new_te)->name)) {
5909 err = got_error(GOT_ERR_NO_SPACE);
5913 (*new_te)->mode = get_ct_file_mode(ct);
5915 if (ct->staged_status == GOT_STATUS_ADD)
5916 memcpy(&(*new_te)->id, ct->staged_blob_id,
5917 sizeof((*new_te)->id));
5919 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5922 if (err && *new_te) {
5929 static const struct got_error *
5930 insert_tree_entry(struct got_tree_entry *new_te,
5931 struct got_pathlist_head *paths)
5933 const struct got_error *err = NULL;
5934 struct got_pathlist_entry *new_pe;
5936 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5940 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5944 static const struct got_error *
5945 report_ct_status(struct got_commitable *ct,
5946 got_worktree_status_cb status_cb, void *status_arg)
5948 const char *ct_path = ct->path;
5949 unsigned char status;
5951 if (status_cb == NULL) /* no commit progress output desired */
5954 while (ct_path[0] == '/')
5957 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5958 status = ct->staged_status;
5960 status = ct->status;
5962 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5963 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5966 static const struct got_error *
5967 match_modified_subtree(int *modified, struct got_tree_entry *te,
5968 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5970 const struct got_error *err = NULL;
5971 struct got_pathlist_entry *pe;
5976 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5977 got_path_is_root_dir(base_tree_path) ? "" : "/",
5979 return got_error_from_errno("asprintf");
5981 TAILQ_FOREACH(pe, commitable_paths, entry) {
5982 struct got_commitable *ct = pe->data;
5983 *modified = got_path_is_child(ct->in_repo_path, te_path,
5993 static const struct got_error *
5994 match_deleted_or_modified_ct(struct got_commitable **ctp,
5995 struct got_tree_entry *te, const char *base_tree_path,
5996 struct got_pathlist_head *commitable_paths)
5998 const struct got_error *err = NULL;
5999 struct got_pathlist_entry *pe;
6003 TAILQ_FOREACH(pe, commitable_paths, entry) {
6004 struct got_commitable *ct = pe->data;
6005 char *ct_name = NULL;
6008 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
6009 if (ct->status != GOT_STATUS_MODIFY &&
6010 ct->status != GOT_STATUS_MODE_CHANGE &&
6011 ct->status != GOT_STATUS_DELETE &&
6012 ct->status != GOT_STATUS_CONFLICT)
6015 if (ct->staged_status != GOT_STATUS_MODIFY &&
6016 ct->staged_status != GOT_STATUS_DELETE)
6020 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
6023 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
6029 err = got_path_basename(&ct_name, pe->path);
6033 if (strcmp(te->name, ct_name) != 0) {
6046 static const struct got_error *
6047 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
6048 const char *child_path, const char *path_base_tree,
6049 struct got_pathlist_head *commitable_paths,
6050 got_worktree_status_cb status_cb, void *status_arg,
6051 struct got_repository *repo)
6053 const struct got_error *err = NULL;
6054 struct got_tree_entry *new_te;
6056 struct got_object_id *id = NULL;
6061 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
6062 got_path_is_root_dir(path_base_tree) ? "" : "/",
6064 return got_error_from_errno("asprintf");
6066 new_te = calloc(1, sizeof(*new_te));
6068 return got_error_from_errno("calloc");
6069 new_te->mode = S_IFDIR;
6071 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
6072 sizeof(new_te->name)) {
6073 err = got_error(GOT_ERR_NO_SPACE);
6076 err = write_tree(&id, &nentries, NULL, subtree_path,
6077 commitable_paths, status_cb, status_arg, repo);
6082 memcpy(&new_te->id, id, sizeof(new_te->id));
6091 static const struct got_error *
6092 write_tree(struct got_object_id **new_tree_id, int *nentries,
6093 struct got_tree_object *base_tree, const char *path_base_tree,
6094 struct got_pathlist_head *commitable_paths,
6095 got_worktree_status_cb status_cb, void *status_arg,
6096 struct got_repository *repo)
6098 const struct got_error *err = NULL;
6099 struct got_pathlist_head paths;
6100 struct got_tree_entry *te, *new_te = NULL;
6101 struct got_pathlist_entry *pe;
6106 /* Insert, and recurse into, newly added entries first. */
6107 TAILQ_FOREACH(pe, commitable_paths, entry) {
6108 struct got_commitable *ct = pe->data;
6109 char *child_path = NULL, *slash;
6111 if ((ct->status != GOT_STATUS_ADD &&
6112 ct->staged_status != GOT_STATUS_ADD) ||
6113 (ct->flags & GOT_COMMITABLE_ADDED))
6116 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
6117 strlen(path_base_tree)))
6120 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
6125 slash = strchr(child_path, '/');
6126 if (slash == NULL) {
6127 err = alloc_added_blob_tree_entry(&new_te, ct);
6130 err = report_ct_status(ct, status_cb, status_arg);
6133 ct->flags |= GOT_COMMITABLE_ADDED;
6134 err = insert_tree_entry(new_te, &paths);
6139 *slash = '\0'; /* trim trailing path components */
6140 if (base_tree == NULL ||
6141 got_object_tree_find_entry(base_tree, child_path)
6143 err = make_subtree_for_added_blob(&new_te,
6144 child_path, path_base_tree,
6145 commitable_paths, status_cb, status_arg,
6149 err = insert_tree_entry(new_te, &paths);
6158 int i, nbase_entries;
6159 /* Handle modified and deleted entries. */
6160 nbase_entries = got_object_tree_get_nentries(base_tree);
6161 for (i = 0; i < nbase_entries; i++) {
6162 struct got_commitable *ct = NULL;
6164 te = got_object_tree_get_entry(base_tree, i);
6165 if (got_object_tree_entry_is_submodule(te)) {
6166 /* Entry is a submodule; just copy it. */
6167 err = got_object_tree_entry_dup(&new_te, te);
6170 err = insert_tree_entry(new_te, &paths);
6177 if (S_ISDIR(te->mode)) {
6179 err = got_object_tree_entry_dup(&new_te, te);
6182 err = match_modified_subtree(&modified, te,
6183 path_base_tree, commitable_paths);
6186 /* Avoid recursion into unmodified subtrees. */
6188 struct got_object_id *new_id;
6190 err = write_subtree(&new_id,
6192 path_base_tree, commitable_paths,
6193 status_cb, status_arg, repo);
6196 if (nsubentries == 0) {
6197 /* All entries were deleted. */
6201 memcpy(&new_te->id, new_id,
6202 sizeof(new_te->id));
6205 err = insert_tree_entry(new_te, &paths);
6212 err = match_deleted_or_modified_ct(&ct, te,
6213 path_base_tree, commitable_paths);
6217 /* NB: Deleted entries get dropped here. */
6218 if (ct->status == GOT_STATUS_MODIFY ||
6219 ct->status == GOT_STATUS_MODE_CHANGE ||
6220 ct->status == GOT_STATUS_CONFLICT ||
6221 ct->staged_status == GOT_STATUS_MODIFY) {
6222 err = alloc_modified_blob_tree_entry(
6226 err = insert_tree_entry(new_te, &paths);
6231 err = report_ct_status(ct, status_cb,
6236 /* Entry is unchanged; just copy it. */
6237 err = got_object_tree_entry_dup(&new_te, te);
6240 err = insert_tree_entry(new_te, &paths);
6248 /* Write new list of entries; deleted entries have been dropped. */
6249 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
6251 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
6255 static const struct got_error *
6256 update_fileindex_after_commit(struct got_worktree *worktree,
6257 struct got_pathlist_head *commitable_paths,
6258 struct got_object_id *new_base_commit_id,
6259 struct got_fileindex *fileindex, int have_staged_files)
6261 const struct got_error *err = NULL;
6262 struct got_pathlist_entry *pe;
6263 char *relpath = NULL;
6265 TAILQ_FOREACH(pe, commitable_paths, entry) {
6266 struct got_fileindex_entry *ie;
6267 struct got_commitable *ct = pe->data;
6269 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6271 err = got_path_skip_common_ancestor(&relpath,
6272 worktree->root_path, ct->ondisk_path);
6277 if (ct->status == GOT_STATUS_DELETE ||
6278 ct->staged_status == GOT_STATUS_DELETE) {
6279 got_fileindex_entry_remove(fileindex, ie);
6280 } else if (ct->staged_status == GOT_STATUS_ADD ||
6281 ct->staged_status == GOT_STATUS_MODIFY) {
6282 got_fileindex_entry_stage_set(ie,
6283 GOT_FILEIDX_STAGE_NONE);
6284 got_fileindex_entry_staged_filetype_set(ie, 0);
6286 err = got_fileindex_entry_update(ie,
6287 worktree->root_fd, relpath,
6288 ct->staged_blob_id->sha1,
6289 new_base_commit_id->sha1,
6290 !have_staged_files);
6292 err = got_fileindex_entry_update(ie,
6293 worktree->root_fd, relpath,
6295 new_base_commit_id->sha1,
6296 !have_staged_files);
6298 err = got_fileindex_entry_alloc(&ie, pe->path);
6301 err = got_fileindex_entry_update(ie,
6302 worktree->root_fd, relpath, ct->blob_id->sha1,
6303 new_base_commit_id->sha1, 1);
6305 got_fileindex_entry_free(ie);
6308 err = got_fileindex_entry_add(fileindex, ie);
6310 got_fileindex_entry_free(ie);
6323 static const struct got_error *
6324 check_out_of_date(const char *in_repo_path, unsigned char status,
6325 unsigned char staged_status, struct got_object_id *base_blob_id,
6326 struct got_object_id *base_commit_id,
6327 struct got_object_id *head_commit_id, struct got_repository *repo,
6330 const struct got_error *err = NULL;
6331 struct got_commit_object *commit = NULL;
6332 struct got_object_id *id = NULL;
6334 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
6335 /* Trivial case: base commit == head commit */
6336 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
6339 * Ensure file content which local changes were based
6340 * on matches file content in the branch head.
6342 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6345 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6347 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6348 err = got_error(ood_errcode);
6350 } else if (got_object_id_cmp(id, base_blob_id) != 0)
6351 err = got_error(ood_errcode);
6353 /* Require that added files don't exist in the branch head. */
6354 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6357 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6358 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
6360 err = id ? got_error(ood_errcode) : NULL;
6365 got_object_commit_close(commit);
6369 static const struct got_error *
6370 commit_worktree(struct got_object_id **new_commit_id,
6371 struct got_pathlist_head *commitable_paths,
6372 struct got_object_id *head_commit_id,
6373 struct got_object_id *parent_id2,
6374 struct got_worktree *worktree,
6375 const char *author, const char *committer, char *diff_path,
6376 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6377 got_worktree_status_cb status_cb, void *status_arg,
6378 struct got_repository *repo)
6380 const struct got_error *err = NULL, *unlockerr = NULL;
6381 struct got_pathlist_entry *pe;
6382 const char *head_ref_name = NULL;
6383 struct got_commit_object *head_commit = NULL;
6384 struct got_reference *head_ref2 = NULL;
6385 struct got_object_id *head_commit_id2 = NULL;
6386 struct got_tree_object *head_tree = NULL;
6387 struct got_object_id *new_tree_id = NULL;
6388 int nentries, nparents = 0;
6389 struct got_object_id_queue parent_ids;
6390 struct got_object_qid *pid = NULL;
6391 char *logmsg = NULL;
6394 *new_commit_id = NULL;
6396 STAILQ_INIT(&parent_ids);
6398 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6402 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6406 if (commit_msg_cb != NULL) {
6407 err = commit_msg_cb(commitable_paths, diff_path,
6408 &logmsg, commit_arg);
6413 if (logmsg == NULL || strlen(logmsg) == 0) {
6414 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6418 /* Create blobs from added and modified files and record their IDs. */
6419 TAILQ_FOREACH(pe, commitable_paths, entry) {
6420 struct got_commitable *ct = pe->data;
6423 /* Blobs for staged files already exist. */
6424 if (ct->staged_status == GOT_STATUS_ADD ||
6425 ct->staged_status == GOT_STATUS_MODIFY)
6428 if (ct->status != GOT_STATUS_ADD &&
6429 ct->status != GOT_STATUS_MODIFY &&
6430 ct->status != GOT_STATUS_MODE_CHANGE &&
6431 ct->status != GOT_STATUS_CONFLICT)
6434 if (asprintf(&ondisk_path, "%s/%s",
6435 worktree->root_path, pe->path) == -1) {
6436 err = got_error_from_errno("asprintf");
6439 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6445 /* Recursively write new tree objects. */
6446 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6447 commitable_paths, status_cb, status_arg, repo);
6451 err = got_object_qid_alloc(&pid, head_commit_id);
6454 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6457 err = got_object_qid_alloc(&pid, parent_id2);
6460 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6463 timestamp = time(NULL);
6464 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6465 nparents, author, timestamp, committer, timestamp, logmsg, repo);
6471 /* Check if a concurrent commit to our branch has occurred. */
6472 head_ref_name = got_worktree_get_head_ref_name(worktree);
6473 if (head_ref_name == NULL) {
6474 err = got_error_from_errno("got_worktree_get_head_ref_name");
6477 /* Lock the reference here to prevent concurrent modification. */
6478 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6481 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6484 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6485 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6488 /* Update branch head in repository. */
6489 err = got_ref_change_ref(head_ref2, *new_commit_id);
6492 err = got_ref_write(head_ref2, repo);
6496 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6500 err = ref_base_commit(worktree, repo);
6504 got_object_id_queue_free(&parent_ids);
6506 got_object_tree_close(head_tree);
6508 got_object_commit_close(head_commit);
6509 free(head_commit_id2);
6511 unlockerr = got_ref_unlock(head_ref2);
6512 if (unlockerr && err == NULL)
6514 got_ref_close(head_ref2);
6519 static const struct got_error *
6520 check_path_is_commitable(const char *path,
6521 struct got_pathlist_head *commitable_paths)
6523 struct got_pathlist_entry *cpe = NULL;
6524 size_t path_len = strlen(path);
6526 TAILQ_FOREACH(cpe, commitable_paths, entry) {
6527 struct got_commitable *ct = cpe->data;
6528 const char *ct_path = ct->path;
6530 while (ct_path[0] == '/')
6533 if (strcmp(path, ct_path) == 0 ||
6534 got_path_is_child(ct_path, path, path_len))
6539 return got_error_path(path, GOT_ERR_BAD_PATH);
6544 static const struct got_error *
6545 check_staged_file(void *arg, struct got_fileindex_entry *ie)
6547 int *have_staged_files = arg;
6549 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6550 *have_staged_files = 1;
6551 return got_error(GOT_ERR_CANCELLED);
6557 static const struct got_error *
6558 check_non_staged_files(struct got_fileindex *fileindex,
6559 struct got_pathlist_head *paths)
6561 struct got_pathlist_entry *pe;
6562 struct got_fileindex_entry *ie;
6564 TAILQ_FOREACH(pe, paths, entry) {
6565 if (pe->path[0] == '\0')
6567 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6569 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6570 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6571 return got_error_path(pe->path,
6572 GOT_ERR_FILE_NOT_STAGED);
6578 const struct got_error *
6579 got_worktree_commit(struct got_object_id **new_commit_id,
6580 struct got_worktree *worktree, struct got_pathlist_head *paths,
6581 const char *author, const char *committer, int allow_bad_symlinks,
6582 int show_diff, int commit_conflicts,
6583 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6584 got_worktree_status_cb status_cb, void *status_arg,
6585 struct got_repository *repo)
6587 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6588 struct got_fileindex *fileindex = NULL;
6589 char *fileindex_path = NULL;
6590 struct got_pathlist_head commitable_paths;
6591 struct collect_commitables_arg cc_arg;
6592 struct got_pathlist_entry *pe;
6593 struct got_reference *head_ref = NULL;
6594 struct got_object_id *head_commit_id = NULL;
6595 char *diff_path = NULL;
6596 int have_staged_files = 0;
6598 *new_commit_id = NULL;
6600 memset(&cc_arg, 0, sizeof(cc_arg));
6601 TAILQ_INIT(&commitable_paths);
6603 err = lock_worktree(worktree, LOCK_EX);
6607 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6611 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6615 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6619 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6620 &have_staged_files);
6621 if (err && err->code != GOT_ERR_CANCELLED)
6623 if (have_staged_files) {
6624 err = check_non_staged_files(fileindex, paths);
6629 cc_arg.commitable_paths = &commitable_paths;
6630 cc_arg.worktree = worktree;
6631 cc_arg.fileindex = fileindex;
6633 cc_arg.have_staged_files = have_staged_files;
6634 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6635 cc_arg.diff_header_shown = 0;
6636 cc_arg.commit_conflicts = commit_conflicts;
6638 err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6639 GOT_TMPDIR_STR "/got", ".diff");
6642 cc_arg.f1 = got_opentemp();
6643 if (cc_arg.f1 == NULL) {
6644 err = got_error_from_errno("got_opentemp");
6647 cc_arg.f2 = got_opentemp();
6648 if (cc_arg.f2 == NULL) {
6649 err = got_error_from_errno("got_opentemp");
6654 TAILQ_FOREACH(pe, paths, entry) {
6655 err = worktree_status(worktree, pe->path, fileindex, repo,
6656 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6662 if (fflush(cc_arg.diff_outfile) == EOF) {
6663 err = got_error_from_errno("fflush");
6668 if (TAILQ_EMPTY(&commitable_paths)) {
6669 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6673 TAILQ_FOREACH(pe, paths, entry) {
6674 err = check_path_is_commitable(pe->path, &commitable_paths);
6679 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6680 struct got_commitable *ct = pe->data;
6681 const char *ct_path = ct->in_repo_path;
6683 while (ct_path[0] == '/')
6685 err = check_out_of_date(ct_path, ct->status,
6686 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6687 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6693 err = commit_worktree(new_commit_id, &commitable_paths,
6694 head_commit_id, NULL, worktree, author, committer,
6695 (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6696 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6700 err = update_fileindex_after_commit(worktree, &commitable_paths,
6701 *new_commit_id, fileindex, have_staged_files);
6702 sync_err = sync_fileindex(fileindex, fileindex_path);
6703 if (sync_err && err == NULL)
6707 got_fileindex_free(fileindex);
6708 free(fileindex_path);
6709 unlockerr = lock_worktree(worktree, LOCK_SH);
6710 if (unlockerr && err == NULL)
6712 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6713 struct got_commitable *ct = pe->data;
6715 free_commitable(ct);
6717 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6718 if (diff_path && unlink(diff_path) == -1 && err == NULL)
6719 err = got_error_from_errno2("unlink", diff_path);
6721 if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6723 err = got_error_from_errno("fclose");
6728 got_commitable_get_path(struct got_commitable *ct)
6734 got_commitable_get_status(struct got_commitable *ct)
6739 struct check_rebase_ok_arg {
6740 struct got_worktree *worktree;
6741 struct got_repository *repo;
6744 static const struct got_error *
6745 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6747 const struct got_error *err = NULL;
6748 struct check_rebase_ok_arg *a = arg;
6749 unsigned char status;
6753 /* Reject rebase of a work tree with mixed base commits. */
6754 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6755 SHA1_DIGEST_LENGTH))
6756 return got_error(GOT_ERR_MIXED_COMMITS);
6758 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6760 return got_error_from_errno("asprintf");
6762 /* Reject rebase of a work tree with modified or staged files. */
6763 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6768 if (status != GOT_STATUS_NO_CHANGE)
6769 return got_error(GOT_ERR_MODIFIED);
6770 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6771 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6776 const struct got_error *
6777 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6778 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6779 struct got_worktree *worktree, struct got_reference *branch,
6780 struct got_repository *repo)
6782 const struct got_error *err = NULL;
6783 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6784 char *branch_ref_name = NULL;
6785 char *fileindex_path = NULL;
6786 struct check_rebase_ok_arg ok_arg;
6787 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6788 struct got_object_id *wt_branch_tip = NULL;
6790 *new_base_branch_ref = NULL;
6794 err = lock_worktree(worktree, LOCK_EX);
6798 err = open_fileindex(fileindex, &fileindex_path, worktree);
6802 ok_arg.worktree = worktree;
6804 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6809 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6813 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6817 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6821 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6826 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6829 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6830 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6834 err = got_ref_alloc_symref(new_base_branch_ref,
6835 new_base_branch_ref_name, wt_branch);
6838 err = got_ref_write(*new_base_branch_ref, repo);
6842 /* TODO Lock original branch's ref while rebasing? */
6844 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6848 err = got_ref_write(branch_ref, repo);
6852 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6853 worktree->base_commit_id);
6856 err = got_ref_write(*tmp_branch, repo);
6860 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6864 free(fileindex_path);
6865 free(tmp_branch_name);
6866 free(new_base_branch_ref_name);
6867 free(branch_ref_name);
6869 got_ref_close(branch_ref);
6871 got_ref_close(wt_branch);
6872 free(wt_branch_tip);
6874 if (*new_base_branch_ref) {
6875 got_ref_close(*new_base_branch_ref);
6876 *new_base_branch_ref = NULL;
6879 got_ref_close(*tmp_branch);
6883 got_fileindex_free(*fileindex);
6886 lock_worktree(worktree, LOCK_SH);
6891 const struct got_error *
6892 got_worktree_rebase_continue(struct got_object_id **commit_id,
6893 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6894 struct got_reference **branch, struct got_fileindex **fileindex,
6895 struct got_worktree *worktree, struct got_repository *repo)
6897 const struct got_error *err;
6898 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6899 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6900 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6901 char *fileindex_path = NULL;
6902 int have_staged_files = 0;
6905 *new_base_branch = NULL;
6910 err = lock_worktree(worktree, LOCK_EX);
6914 err = open_fileindex(fileindex, &fileindex_path, worktree);
6918 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6919 &have_staged_files);
6920 if (err && err->code != GOT_ERR_CANCELLED)
6922 if (have_staged_files) {
6923 err = got_error(GOT_ERR_STAGED_PATHS);
6927 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6931 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6935 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6939 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6943 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6947 err = got_ref_open(branch, repo,
6948 got_ref_get_symref_target(branch_ref), 0);
6952 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6956 err = got_ref_resolve(commit_id, repo, commit_ref);
6960 err = got_ref_open(new_base_branch, repo,
6961 new_base_branch_ref_name, 0);
6965 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6969 free(commit_ref_name);
6970 free(branch_ref_name);
6971 free(fileindex_path);
6973 got_ref_close(commit_ref);
6975 got_ref_close(branch_ref);
6980 got_ref_close(*tmp_branch);
6983 if (*new_base_branch) {
6984 got_ref_close(*new_base_branch);
6985 *new_base_branch = NULL;
6988 got_ref_close(*branch);
6992 got_fileindex_free(*fileindex);
6995 lock_worktree(worktree, LOCK_SH);
7000 const struct got_error *
7001 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
7003 const struct got_error *err;
7004 char *tmp_branch_name = NULL;
7006 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7010 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7011 free(tmp_branch_name);
7015 static const struct got_error *
7016 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
7017 const char *diff_path, char **logmsg, void *arg)
7023 static const struct got_error *
7024 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
7025 const char *path, struct got_object_id *blob_id,
7026 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7027 int dirfd, const char *de_name)
7032 struct collect_merged_paths_arg {
7033 got_worktree_checkout_cb progress_cb;
7035 struct got_pathlist_head *merged_paths;
7038 static const struct got_error *
7039 collect_merged_paths(void *arg, unsigned char status, const char *path)
7041 const struct got_error *err;
7042 struct collect_merged_paths_arg *a = arg;
7044 struct got_pathlist_entry *new;
7046 err = (*a->progress_cb)(a->progress_arg, status, path);
7050 if (status != GOT_STATUS_MERGE &&
7051 status != GOT_STATUS_ADD &&
7052 status != GOT_STATUS_DELETE &&
7053 status != GOT_STATUS_CONFLICT)
7058 return got_error_from_errno("strdup");
7060 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
7061 if (err || new == NULL)
7066 static const struct got_error *
7067 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
7068 int is_rebase, struct got_repository *repo)
7070 const struct got_error *err;
7071 struct got_reference *commit_ref = NULL;
7073 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7075 if (err->code != GOT_ERR_NOT_REF)
7077 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
7080 err = got_ref_write(commit_ref, repo);
7083 } else if (is_rebase) {
7084 struct got_object_id *stored_id;
7087 err = got_ref_resolve(&stored_id, repo, commit_ref);
7090 cmp = got_object_id_cmp(commit_id, stored_id);
7093 err = got_error(GOT_ERR_REBASE_COMMITID);
7099 got_ref_close(commit_ref);
7103 static const struct got_error *
7104 rebase_merge_files(struct got_pathlist_head *merged_paths,
7105 const char *commit_ref_name, struct got_worktree *worktree,
7106 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
7107 struct got_object_id *commit_id, struct got_repository *repo,
7108 got_worktree_checkout_cb progress_cb, void *progress_arg,
7109 got_cancel_cb cancel_cb, void *cancel_arg)
7111 const struct got_error *err;
7112 struct got_reference *commit_ref = NULL;
7113 struct collect_merged_paths_arg cmp_arg;
7114 char *fileindex_path;
7116 /* Work tree is locked/unlocked during rebase preparation/teardown. */
7118 err = get_fileindex_path(&fileindex_path, worktree);
7122 cmp_arg.progress_cb = progress_cb;
7123 cmp_arg.progress_arg = progress_arg;
7124 cmp_arg.merged_paths = merged_paths;
7125 err = merge_files(worktree, fileindex, fileindex_path,
7126 parent_commit_id, commit_id, repo, collect_merged_paths,
7127 &cmp_arg, cancel_cb, cancel_arg);
7129 got_ref_close(commit_ref);
7133 const struct got_error *
7134 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
7135 struct got_worktree *worktree, struct got_fileindex *fileindex,
7136 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
7137 struct got_repository *repo,
7138 got_worktree_checkout_cb progress_cb, void *progress_arg,
7139 got_cancel_cb cancel_cb, void *cancel_arg)
7141 const struct got_error *err;
7142 char *commit_ref_name;
7144 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7148 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
7152 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7153 fileindex, parent_commit_id, commit_id, repo, progress_cb,
7154 progress_arg, cancel_cb, cancel_arg);
7156 free(commit_ref_name);
7160 const struct got_error *
7161 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
7162 struct got_worktree *worktree, struct got_fileindex *fileindex,
7163 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
7164 struct got_repository *repo,
7165 got_worktree_checkout_cb progress_cb, void *progress_arg,
7166 got_cancel_cb cancel_cb, void *cancel_arg)
7168 const struct got_error *err;
7169 char *commit_ref_name;
7171 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7175 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7179 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7180 fileindex, parent_commit_id, commit_id, repo, progress_cb,
7181 progress_arg, cancel_cb, cancel_arg);
7183 free(commit_ref_name);
7187 static const struct got_error *
7188 rebase_commit(struct got_object_id **new_commit_id,
7189 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
7190 struct got_worktree *worktree, struct got_fileindex *fileindex,
7191 struct got_reference *tmp_branch, const char *committer,
7192 struct got_commit_object *orig_commit, const char *new_logmsg,
7193 int allow_conflict, struct got_repository *repo)
7195 const struct got_error *err, *sync_err;
7196 struct got_pathlist_head commitable_paths;
7197 struct collect_commitables_arg cc_arg;
7198 char *fileindex_path = NULL;
7199 struct got_reference *head_ref = NULL;
7200 struct got_object_id *head_commit_id = NULL;
7201 char *logmsg = NULL;
7203 memset(&cc_arg, 0, sizeof(cc_arg));
7204 TAILQ_INIT(&commitable_paths);
7205 *new_commit_id = NULL;
7207 /* Work tree is locked/unlocked during rebase preparation/teardown. */
7209 err = get_fileindex_path(&fileindex_path, worktree);
7213 cc_arg.commitable_paths = &commitable_paths;
7214 cc_arg.worktree = worktree;
7216 cc_arg.have_staged_files = 0;
7217 cc_arg.commit_conflicts = allow_conflict;
7219 * If possible get the status of individual files directly to
7220 * avoid crawling the entire work tree once per rebased commit.
7222 * Ideally, merged_paths would contain a list of commitables
7223 * we could use so we could skip worktree_status() entirely.
7224 * However, we would then need carefully keep track of cumulative
7225 * effects of operations such as file additions and deletions
7226 * in 'got histedit -f' (folding multiple commits into one),
7227 * and this extra complexity is not really worth it.
7230 struct got_pathlist_entry *pe;
7231 TAILQ_FOREACH(pe, merged_paths, entry) {
7232 err = worktree_status(worktree, pe->path, fileindex,
7233 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
7239 err = worktree_status(worktree, "", fileindex, repo,
7240 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7245 if (TAILQ_EMPTY(&commitable_paths)) {
7246 /* No-op change; commit will be elided. */
7247 err = got_ref_delete(commit_ref, repo);
7250 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
7254 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7258 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7263 logmsg = strdup(new_logmsg);
7264 if (logmsg == NULL) {
7265 err = got_error_from_errno("strdup");
7269 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
7274 /* NB: commit_worktree will call free(logmsg) */
7275 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
7276 NULL, worktree, got_object_commit_get_author(orig_commit),
7277 committer ? committer :
7278 got_object_commit_get_committer(orig_commit), NULL,
7279 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
7283 err = got_ref_change_ref(tmp_branch, *new_commit_id);
7287 err = got_ref_delete(commit_ref, repo);
7291 err = update_fileindex_after_commit(worktree, &commitable_paths,
7292 *new_commit_id, fileindex, 0);
7293 sync_err = sync_fileindex(fileindex, fileindex_path);
7294 if (sync_err && err == NULL)
7297 free(fileindex_path);
7298 free(head_commit_id);
7300 got_ref_close(head_ref);
7302 free(*new_commit_id);
7303 *new_commit_id = NULL;
7308 const struct got_error *
7309 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
7310 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7311 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7312 const char *committer, struct got_commit_object *orig_commit,
7313 struct got_object_id *orig_commit_id, int allow_conflict,
7314 struct got_repository *repo)
7316 const struct got_error *err;
7317 char *commit_ref_name;
7318 struct got_reference *commit_ref = NULL;
7319 struct got_object_id *commit_id = NULL;
7321 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7325 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7328 err = got_ref_resolve(&commit_id, repo, commit_ref);
7331 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
7332 err = got_error(GOT_ERR_REBASE_COMMITID);
7336 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7337 worktree, fileindex, tmp_branch, committer, orig_commit,
7338 NULL, allow_conflict, repo);
7341 got_ref_close(commit_ref);
7342 free(commit_ref_name);
7347 const struct got_error *
7348 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
7349 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7350 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7351 const char *committer, struct got_commit_object *orig_commit,
7352 struct got_object_id *orig_commit_id, const char *new_logmsg,
7353 int allow_conflict, struct got_repository *repo)
7355 const struct got_error *err;
7356 char *commit_ref_name;
7357 struct got_reference *commit_ref = NULL;
7359 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7363 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7367 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7368 worktree, fileindex, tmp_branch, committer, orig_commit,
7369 new_logmsg, allow_conflict, repo);
7372 got_ref_close(commit_ref);
7373 free(commit_ref_name);
7377 const struct got_error *
7378 got_worktree_rebase_postpone(struct got_worktree *worktree,
7379 struct got_fileindex *fileindex)
7382 got_fileindex_free(fileindex);
7383 return lock_worktree(worktree, LOCK_SH);
7386 static const struct got_error *
7387 delete_ref(const char *name, struct got_repository *repo)
7389 const struct got_error *err;
7390 struct got_reference *ref;
7392 err = got_ref_open(&ref, repo, name, 0);
7394 if (err->code == GOT_ERR_NOT_REF)
7399 err = got_ref_delete(ref, repo);
7404 static const struct got_error *
7405 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7407 const struct got_error *err;
7408 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7409 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7411 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7414 err = delete_ref(tmp_branch_name, repo);
7418 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7421 err = delete_ref(new_base_branch_ref_name, repo);
7425 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7428 err = delete_ref(branch_ref_name, repo);
7432 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7435 err = delete_ref(commit_ref_name, repo);
7440 free(tmp_branch_name);
7441 free(new_base_branch_ref_name);
7442 free(branch_ref_name);
7443 free(commit_ref_name);
7447 static const struct got_error *
7448 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7449 struct got_object_id *new_commit_id, struct got_repository *repo)
7451 const struct got_error *err;
7452 struct got_reference *ref = NULL;
7453 struct got_object_id *old_commit_id = NULL;
7454 const char *branch_name = NULL;
7455 char *new_id_str = NULL;
7456 char *refname = NULL;
7458 branch_name = got_ref_get_name(branch);
7459 if (strncmp(branch_name, "refs/heads/", 11) != 0)
7460 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7463 err = got_object_id_str(&new_id_str, new_commit_id);
7467 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7468 new_id_str) == -1) {
7469 err = got_error_from_errno("asprintf");
7473 err = got_ref_resolve(&old_commit_id, repo, branch);
7477 err = got_ref_alloc(&ref, refname, old_commit_id);
7481 err = got_ref_write(ref, repo);
7485 free(old_commit_id);
7491 const struct got_error *
7492 got_worktree_rebase_complete(struct got_worktree *worktree,
7493 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7494 struct got_reference *rebased_branch, struct got_repository *repo,
7497 const struct got_error *err, *unlockerr, *sync_err;
7498 struct got_object_id *new_head_commit_id = NULL;
7499 char *fileindex_path = NULL;
7501 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7505 if (create_backup) {
7506 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7507 rebased_branch, new_head_commit_id, repo);
7512 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7516 err = got_ref_write(rebased_branch, repo);
7520 err = got_worktree_set_head_ref(worktree, rebased_branch);
7524 err = delete_rebase_refs(worktree, repo);
7528 err = get_fileindex_path(&fileindex_path, worktree);
7531 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7532 sync_err = sync_fileindex(fileindex, fileindex_path);
7533 if (sync_err && err == NULL)
7536 got_fileindex_free(fileindex);
7537 free(fileindex_path);
7538 free(new_head_commit_id);
7539 unlockerr = lock_worktree(worktree, LOCK_SH);
7540 if (unlockerr && err == NULL)
7545 static const struct got_error *
7546 get_paths_changed_between_commits(struct got_pathlist_head *paths,
7547 struct got_object_id *id1, struct got_object_id *id2,
7548 struct got_repository *repo)
7550 const struct got_error *err;
7551 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
7552 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7555 err = got_object_open_as_commit(&commit1, repo, id1);
7559 err = got_object_open_as_tree(&tree1, repo,
7560 got_object_commit_get_tree_id(commit1));
7566 err = got_object_open_as_commit(&commit2, repo, id2);
7570 err = got_object_open_as_tree(&tree2, repo,
7571 got_object_commit_get_tree_id(commit2));
7576 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
7577 got_diff_tree_collect_changed_paths, paths, 0);
7582 got_object_commit_close(commit1);
7584 got_object_commit_close(commit2);
7586 got_object_tree_close(tree1);
7588 got_object_tree_close(tree2);
7592 static const struct got_error *
7593 get_paths_added_between_commits(struct got_pathlist_head *added_paths,
7594 struct got_object_id *id1, struct got_object_id *id2,
7595 const char *path_prefix, struct got_repository *repo)
7597 const struct got_error *err;
7598 struct got_pathlist_head merged_paths;
7599 struct got_pathlist_entry *pe;
7600 char *abspath = NULL, *wt_path = NULL;
7602 TAILQ_INIT(&merged_paths);
7604 err = get_paths_changed_between_commits(&merged_paths, id1, id2, repo);
7608 TAILQ_FOREACH(pe, &merged_paths, entry) {
7609 struct got_diff_changed_path *change = pe->data;
7611 if (change->status != GOT_STATUS_ADD)
7614 if (got_path_is_root_dir(path_prefix)) {
7615 wt_path = strdup(pe->path);
7616 if (wt_path == NULL) {
7617 err = got_error_from_errno("strdup");
7621 if (asprintf(&abspath, "/%s", pe->path) == -1) {
7622 err = got_error_from_errno("asprintf");
7626 err = got_path_skip_common_ancestor(&wt_path,
7627 path_prefix, abspath);
7634 err = got_pathlist_append(added_paths, wt_path, NULL);
7641 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_ALL);
7647 static const struct got_error *
7648 get_paths_added_in_commit(struct got_pathlist_head *added_paths,
7649 struct got_object_id *id, const char *path_prefix,
7650 struct got_repository *repo)
7652 const struct got_error *err;
7653 struct got_commit_object *commit = NULL;
7654 struct got_object_qid *pid;
7656 err = got_object_open_as_commit(&commit, repo, id);
7660 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7662 err = get_paths_added_between_commits(added_paths,
7663 pid ? &pid->id : NULL, id, path_prefix, repo);
7668 got_object_commit_close(commit);
7672 const struct got_error *
7673 got_worktree_rebase_abort(struct got_worktree *worktree,
7674 struct got_fileindex *fileindex, struct got_repository *repo,
7675 struct got_reference *new_base_branch,
7676 got_worktree_checkout_cb progress_cb, void *progress_arg)
7678 const struct got_error *err, *unlockerr, *sync_err;
7679 struct got_reference *resolved = NULL;
7680 struct got_object_id *commit_id = NULL;
7681 struct got_object_id *merged_commit_id = NULL;
7682 struct got_commit_object *commit = NULL;
7683 char *fileindex_path = NULL;
7684 char *commit_ref_name = NULL;
7685 struct got_reference *commit_ref = NULL;
7686 struct revert_file_args rfa;
7687 struct got_object_id *tree_id = NULL;
7688 struct got_pathlist_head added_paths;
7690 TAILQ_INIT(&added_paths);
7692 err = lock_worktree(worktree, LOCK_EX);
7696 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7700 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7704 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
7709 * Determine which files in added status can be safely removed
7710 * from disk while reverting changes in the work tree.
7711 * We want to avoid deleting unrelated files which were added by
7712 * the user for conflict resolution purposes.
7714 err = get_paths_added_in_commit(&added_paths, merged_commit_id,
7715 got_worktree_get_path_prefix(worktree), repo);
7719 err = got_ref_open(&resolved, repo,
7720 got_ref_get_symref_target(new_base_branch), 0);
7724 err = got_worktree_set_head_ref(worktree, resolved);
7729 * XXX commits to the base branch could have happened while
7730 * we were busy rebasing; should we store the original commit ID
7731 * when rebase begins and read it back here?
7733 err = got_ref_resolve(&commit_id, repo, resolved);
7737 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7741 err = got_object_open_as_commit(&commit, repo,
7742 worktree->base_commit_id);
7746 err = got_object_id_by_path(&tree_id, repo, commit,
7747 worktree->path_prefix);
7751 err = delete_rebase_refs(worktree, repo);
7755 err = get_fileindex_path(&fileindex_path, worktree);
7759 rfa.worktree = worktree;
7760 rfa.fileindex = fileindex;
7761 rfa.progress_cb = progress_cb;
7762 rfa.progress_arg = progress_arg;
7763 rfa.patch_cb = NULL;
7764 rfa.patch_arg = NULL;
7766 rfa.unlink_added_files = 1;
7767 rfa.added_files_to_unlink = &added_paths;
7768 err = worktree_status(worktree, "", fileindex, repo,
7769 revert_file, &rfa, NULL, NULL, 1, 0);
7773 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7774 repo, progress_cb, progress_arg, NULL, NULL);
7776 sync_err = sync_fileindex(fileindex, fileindex_path);
7777 if (sync_err && err == NULL)
7780 got_pathlist_free(&added_paths, GOT_PATHLIST_FREE_PATH);
7781 got_ref_close(resolved);
7784 free(merged_commit_id);
7786 got_object_commit_close(commit);
7788 got_fileindex_free(fileindex);
7789 free(fileindex_path);
7790 free(commit_ref_name);
7792 got_ref_close(commit_ref);
7794 unlockerr = lock_worktree(worktree, LOCK_SH);
7795 if (unlockerr && err == NULL)
7800 const struct got_error *
7801 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7802 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7803 struct got_fileindex **fileindex, struct got_worktree *worktree,
7804 struct got_repository *repo)
7806 const struct got_error *err = NULL;
7807 char *tmp_branch_name = NULL;
7808 char *branch_ref_name = NULL;
7809 char *base_commit_ref_name = NULL;
7810 char *fileindex_path = NULL;
7811 struct check_rebase_ok_arg ok_arg;
7812 struct got_reference *wt_branch = NULL;
7813 struct got_reference *base_commit_ref = NULL;
7817 *base_commit_id = NULL;
7820 err = lock_worktree(worktree, LOCK_EX);
7824 err = open_fileindex(fileindex, &fileindex_path, worktree);
7828 ok_arg.worktree = worktree;
7830 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7835 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7839 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7843 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7848 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7853 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7857 err = got_ref_write(*branch_ref, repo);
7861 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7862 worktree->base_commit_id);
7865 err = got_ref_write(base_commit_ref, repo);
7868 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7869 if (*base_commit_id == NULL) {
7870 err = got_error_from_errno("got_object_id_dup");
7874 err = got_ref_alloc(tmp_branch, tmp_branch_name,
7875 worktree->base_commit_id);
7878 err = got_ref_write(*tmp_branch, repo);
7882 err = got_worktree_set_head_ref(worktree, *tmp_branch);
7886 free(fileindex_path);
7887 free(tmp_branch_name);
7888 free(branch_ref_name);
7889 free(base_commit_ref_name);
7891 got_ref_close(wt_branch);
7894 got_ref_close(*branch_ref);
7898 got_ref_close(*tmp_branch);
7901 free(*base_commit_id);
7903 got_fileindex_free(*fileindex);
7906 lock_worktree(worktree, LOCK_SH);
7911 const struct got_error *
7912 got_worktree_histedit_postpone(struct got_worktree *worktree,
7913 struct got_fileindex *fileindex)
7916 got_fileindex_free(fileindex);
7917 return lock_worktree(worktree, LOCK_SH);
7920 const struct got_error *
7921 got_worktree_histedit_in_progress(int *in_progress,
7922 struct got_worktree *worktree)
7924 const struct got_error *err;
7925 char *tmp_branch_name = NULL;
7927 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7931 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7932 free(tmp_branch_name);
7936 const struct got_error *
7937 got_worktree_histedit_continue(struct got_object_id **commit_id,
7938 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7939 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7940 struct got_worktree *worktree, struct got_repository *repo)
7942 const struct got_error *err;
7943 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7944 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7945 struct got_reference *commit_ref = NULL;
7946 struct got_reference *base_commit_ref = NULL;
7947 char *fileindex_path = NULL;
7948 int have_staged_files = 0;
7952 *base_commit_id = NULL;
7955 err = lock_worktree(worktree, LOCK_EX);
7959 err = open_fileindex(fileindex, &fileindex_path, worktree);
7963 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7964 &have_staged_files);
7965 if (err && err->code != GOT_ERR_CANCELLED)
7967 if (have_staged_files) {
7968 err = got_error(GOT_ERR_STAGED_PATHS);
7972 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7976 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7980 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7984 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7989 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7993 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7996 err = got_ref_resolve(commit_id, repo, commit_ref);
8000 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
8003 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
8007 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
8011 free(commit_ref_name);
8012 free(branch_ref_name);
8013 free(fileindex_path);
8015 got_ref_close(commit_ref);
8016 if (base_commit_ref)
8017 got_ref_close(base_commit_ref);
8021 free(*base_commit_id);
8022 *base_commit_id = NULL;
8024 got_ref_close(*tmp_branch);
8028 got_fileindex_free(*fileindex);
8031 lock_worktree(worktree, LOCK_EX);
8036 static const struct got_error *
8037 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
8039 const struct got_error *err;
8040 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
8041 char *branch_ref_name = NULL, *commit_ref_name = NULL;
8043 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
8046 err = delete_ref(tmp_branch_name, repo);
8050 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
8054 err = delete_ref(base_commit_ref_name, repo);
8058 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
8061 err = delete_ref(branch_ref_name, repo);
8065 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8068 err = delete_ref(commit_ref_name, repo);
8072 free(tmp_branch_name);
8073 free(base_commit_ref_name);
8074 free(branch_ref_name);
8075 free(commit_ref_name);
8079 const struct got_error *
8080 got_worktree_histedit_abort(struct got_worktree *worktree,
8081 struct got_fileindex *fileindex, struct got_repository *repo,
8082 struct got_reference *branch, struct got_object_id *base_commit_id,
8083 got_worktree_checkout_cb progress_cb, void *progress_arg)
8085 const struct got_error *err, *unlockerr, *sync_err;
8086 struct got_reference *resolved = NULL;
8087 char *fileindex_path = NULL;
8088 struct got_object_id *merged_commit_id = NULL;
8089 struct got_commit_object *commit = NULL;
8090 char *commit_ref_name = NULL;
8091 struct got_reference *commit_ref = NULL;
8092 struct got_object_id *tree_id = NULL;
8093 struct revert_file_args rfa;
8094 struct got_pathlist_head added_paths;
8096 TAILQ_INIT(&added_paths);
8098 err = lock_worktree(worktree, LOCK_EX);
8102 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8106 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8108 if (err->code != GOT_ERR_NOT_REF)
8110 /* Can happen on early abort due to invalid histedit script. */
8115 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8120 * Determine which files in added status can be safely removed
8121 * from disk while reverting changes in the work tree.
8122 * We want to avoid deleting unrelated files added by the
8123 * user during conflict resolution or during histedit -e.
8125 err = get_paths_added_in_commit(&added_paths, merged_commit_id,
8126 got_worktree_get_path_prefix(worktree), repo);
8131 err = got_ref_open(&resolved, repo,
8132 got_ref_get_symref_target(branch), 0);
8136 err = got_worktree_set_head_ref(worktree, resolved);
8140 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
8144 err = got_object_open_as_commit(&commit, repo,
8145 worktree->base_commit_id);
8149 err = got_object_id_by_path(&tree_id, repo, commit,
8150 worktree->path_prefix);
8154 err = delete_histedit_refs(worktree, repo);
8158 err = get_fileindex_path(&fileindex_path, worktree);
8162 rfa.worktree = worktree;
8163 rfa.fileindex = fileindex;
8164 rfa.progress_cb = progress_cb;
8165 rfa.progress_arg = progress_arg;
8166 rfa.patch_cb = NULL;
8167 rfa.patch_arg = NULL;
8169 rfa.unlink_added_files = 1;
8170 rfa.added_files_to_unlink = &added_paths;
8171 err = worktree_status(worktree, "", fileindex, repo,
8172 revert_file, &rfa, NULL, NULL, 1, 0);
8176 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8177 repo, progress_cb, progress_arg, NULL, NULL);
8179 sync_err = sync_fileindex(fileindex, fileindex_path);
8180 if (sync_err && err == NULL)
8184 got_ref_close(resolved);
8186 got_ref_close(commit_ref);
8187 free(merged_commit_id);
8189 free(fileindex_path);
8190 free(commit_ref_name);
8192 unlockerr = lock_worktree(worktree, LOCK_SH);
8193 if (unlockerr && err == NULL)
8198 const struct got_error *
8199 got_worktree_histedit_complete(struct got_worktree *worktree,
8200 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8201 struct got_reference *edited_branch, struct got_repository *repo)
8203 const struct got_error *err, *unlockerr, *sync_err;
8204 struct got_object_id *new_head_commit_id = NULL;
8205 struct got_reference *resolved = NULL;
8206 char *fileindex_path = NULL;
8208 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
8212 err = got_ref_open(&resolved, repo,
8213 got_ref_get_symref_target(edited_branch), 0);
8217 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
8218 resolved, new_head_commit_id, repo);
8222 err = got_ref_change_ref(resolved, new_head_commit_id);
8226 err = got_ref_write(resolved, repo);
8230 err = got_worktree_set_head_ref(worktree, resolved);
8234 err = delete_histedit_refs(worktree, repo);
8238 err = get_fileindex_path(&fileindex_path, worktree);
8241 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8242 sync_err = sync_fileindex(fileindex, fileindex_path);
8243 if (sync_err && err == NULL)
8246 got_fileindex_free(fileindex);
8247 free(fileindex_path);
8248 free(new_head_commit_id);
8249 unlockerr = lock_worktree(worktree, LOCK_SH);
8250 if (unlockerr && err == NULL)
8255 const struct got_error *
8256 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
8257 struct got_object_id *commit_id, struct got_repository *repo)
8259 const struct got_error *err;
8260 char *commit_ref_name;
8262 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8266 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
8270 err = delete_ref(commit_ref_name, repo);
8272 free(commit_ref_name);
8276 const struct got_error *
8277 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
8278 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
8279 struct got_worktree *worktree, const char *refname,
8280 struct got_repository *repo)
8282 const struct got_error *err = NULL;
8283 char *fileindex_path = NULL;
8284 struct check_rebase_ok_arg ok_arg;
8288 *base_branch_ref = NULL;
8290 err = lock_worktree(worktree, LOCK_EX);
8294 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
8295 err = got_error_msg(GOT_ERR_SAME_BRANCH,
8296 "cannot integrate a branch into itself; "
8297 "update -b or different branch name required");
8301 err = open_fileindex(fileindex, &fileindex_path, worktree);
8305 /* Preconditions are the same as for rebase. */
8306 ok_arg.worktree = worktree;
8308 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8313 err = got_ref_open(branch_ref, repo, refname, 1);
8317 err = got_ref_open(base_branch_ref, repo,
8318 got_worktree_get_head_ref_name(worktree), 1);
8322 got_ref_close(*branch_ref);
8325 if (*base_branch_ref) {
8326 got_ref_close(*base_branch_ref);
8327 *base_branch_ref = NULL;
8330 got_fileindex_free(*fileindex);
8333 lock_worktree(worktree, LOCK_SH);
8338 const struct got_error *
8339 got_worktree_integrate_continue(struct got_worktree *worktree,
8340 struct got_fileindex *fileindex, struct got_repository *repo,
8341 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
8342 got_worktree_checkout_cb progress_cb, void *progress_arg,
8343 got_cancel_cb cancel_cb, void *cancel_arg)
8345 const struct got_error *err = NULL, *sync_err, *unlockerr;
8346 char *fileindex_path = NULL;
8347 struct got_object_id *tree_id = NULL, *commit_id = NULL;
8348 struct got_commit_object *commit = NULL;
8350 err = get_fileindex_path(&fileindex_path, worktree);
8354 err = got_ref_resolve(&commit_id, repo, branch_ref);
8358 err = got_object_open_as_commit(&commit, repo, commit_id);
8362 err = got_object_id_by_path(&tree_id, repo, commit,
8363 worktree->path_prefix);
8367 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
8371 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
8372 progress_cb, progress_arg, cancel_cb, cancel_arg);
8376 err = got_ref_change_ref(base_branch_ref, commit_id);
8380 err = got_ref_write(base_branch_ref, repo);
8384 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8386 sync_err = sync_fileindex(fileindex, fileindex_path);
8387 if (sync_err && err == NULL)
8391 unlockerr = got_ref_unlock(branch_ref);
8392 if (unlockerr && err == NULL)
8394 got_ref_close(branch_ref);
8396 unlockerr = got_ref_unlock(base_branch_ref);
8397 if (unlockerr && err == NULL)
8399 got_ref_close(base_branch_ref);
8401 got_fileindex_free(fileindex);
8402 free(fileindex_path);
8405 got_object_commit_close(commit);
8407 unlockerr = lock_worktree(worktree, LOCK_SH);
8408 if (unlockerr && err == NULL)
8413 const struct got_error *
8414 got_worktree_integrate_abort(struct got_worktree *worktree,
8415 struct got_fileindex *fileindex, struct got_repository *repo,
8416 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
8418 const struct got_error *err = NULL, *unlockerr = NULL;
8420 got_fileindex_free(fileindex);
8422 err = lock_worktree(worktree, LOCK_SH);
8424 unlockerr = got_ref_unlock(branch_ref);
8425 if (unlockerr && err == NULL)
8427 got_ref_close(branch_ref);
8429 unlockerr = got_ref_unlock(base_branch_ref);
8430 if (unlockerr && err == NULL)
8432 got_ref_close(base_branch_ref);
8437 const struct got_error *
8438 got_worktree_merge_postpone(struct got_worktree *worktree,
8439 struct got_fileindex *fileindex)
8441 const struct got_error *err, *sync_err;
8442 char *fileindex_path = NULL;
8444 err = get_fileindex_path(&fileindex_path, worktree);
8448 sync_err = sync_fileindex(fileindex, fileindex_path);
8450 err = lock_worktree(worktree, LOCK_SH);
8451 if (sync_err && err == NULL)
8454 got_fileindex_free(fileindex);
8455 free(fileindex_path);
8459 static const struct got_error *
8460 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
8462 const struct got_error *err;
8463 char *branch_refname = NULL, *commit_refname = NULL;
8465 err = get_merge_branch_ref_name(&branch_refname, worktree);
8468 err = delete_ref(branch_refname, repo);
8472 err = get_merge_commit_ref_name(&commit_refname, worktree);
8475 err = delete_ref(commit_refname, repo);
8480 free(branch_refname);
8481 free(commit_refname);
8485 struct merge_commit_msg_arg {
8486 struct got_worktree *worktree;
8487 const char *branch_name;
8490 static const struct got_error *
8491 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
8492 const char *diff_path, char **logmsg, void *arg)
8494 struct merge_commit_msg_arg *a = arg;
8496 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
8497 got_worktree_get_head_ref_name(a->worktree)) == -1)
8498 return got_error_from_errno("asprintf");
8504 const struct got_error *
8505 got_worktree_merge_branch(struct got_worktree *worktree,
8506 struct got_fileindex *fileindex,
8507 struct got_object_id *yca_commit_id,
8508 struct got_object_id *branch_tip,
8509 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
8510 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
8512 const struct got_error *err;
8513 char *fileindex_path = NULL;
8514 struct check_mixed_commits_args cma;
8516 err = get_fileindex_path(&fileindex_path, worktree);
8520 cma.worktree = worktree;
8521 cma.cancel_cb = cancel_cb;
8522 cma.cancel_arg = cancel_arg;
8524 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
8529 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
8530 branch_tip, repo, progress_cb, progress_arg,
8531 cancel_cb, cancel_arg);
8533 free(fileindex_path);
8537 const struct got_error *
8538 got_worktree_merge_commit(struct got_object_id **new_commit_id,
8539 struct got_worktree *worktree, struct got_fileindex *fileindex,
8540 const char *author, const char *committer, int allow_bad_symlinks,
8541 struct got_object_id *branch_tip, const char *branch_name,
8542 int allow_conflict, struct got_repository *repo,
8543 got_worktree_status_cb status_cb, void *status_arg)
8546 const struct got_error *err = NULL, *sync_err;
8547 struct got_pathlist_head commitable_paths;
8548 struct collect_commitables_arg cc_arg;
8549 struct got_pathlist_entry *pe;
8550 struct got_reference *head_ref = NULL;
8551 struct got_object_id *head_commit_id = NULL;
8552 int have_staged_files = 0;
8553 struct merge_commit_msg_arg mcm_arg;
8554 char *fileindex_path = NULL;
8556 memset(&cc_arg, 0, sizeof(cc_arg));
8557 *new_commit_id = NULL;
8559 TAILQ_INIT(&commitable_paths);
8561 err = get_fileindex_path(&fileindex_path, worktree);
8565 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
8569 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8573 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
8574 &have_staged_files);
8575 if (err && err->code != GOT_ERR_CANCELLED)
8577 if (have_staged_files) {
8578 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
8582 cc_arg.commitable_paths = &commitable_paths;
8583 cc_arg.worktree = worktree;
8584 cc_arg.fileindex = fileindex;
8586 cc_arg.have_staged_files = have_staged_files;
8587 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
8588 cc_arg.commit_conflicts = allow_conflict;
8589 err = worktree_status(worktree, "", fileindex, repo,
8590 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8594 mcm_arg.worktree = worktree;
8595 mcm_arg.branch_name = branch_name;
8596 err = commit_worktree(new_commit_id, &commitable_paths,
8597 head_commit_id, branch_tip, worktree, author, committer, NULL,
8598 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8602 err = update_fileindex_after_commit(worktree, &commitable_paths,
8603 *new_commit_id, fileindex, have_staged_files);
8604 sync_err = sync_fileindex(fileindex, fileindex_path);
8605 if (sync_err && err == NULL)
8608 TAILQ_FOREACH(pe, &commitable_paths, entry) {
8609 struct got_commitable *ct = pe->data;
8611 free_commitable(ct);
8613 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8614 free(fileindex_path);
8618 const struct got_error *
8619 got_worktree_merge_complete(struct got_worktree *worktree,
8620 struct got_fileindex *fileindex, struct got_repository *repo)
8622 const struct got_error *err, *unlockerr, *sync_err;
8623 char *fileindex_path = NULL;
8625 err = delete_merge_refs(worktree, repo);
8629 err = get_fileindex_path(&fileindex_path, worktree);
8632 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8633 sync_err = sync_fileindex(fileindex, fileindex_path);
8634 if (sync_err && err == NULL)
8637 got_fileindex_free(fileindex);
8638 free(fileindex_path);
8639 unlockerr = lock_worktree(worktree, LOCK_SH);
8640 if (unlockerr && err == NULL)
8645 const struct got_error *
8646 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8647 struct got_repository *repo)
8649 const struct got_error *err;
8650 char *branch_refname = NULL;
8651 struct got_reference *branch_ref = NULL;
8655 err = get_merge_branch_ref_name(&branch_refname, worktree);
8658 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8659 free(branch_refname);
8661 if (err->code != GOT_ERR_NOT_REF)
8669 const struct got_error *got_worktree_merge_prepare(
8670 struct got_fileindex **fileindex, struct got_worktree *worktree,
8671 struct got_repository *repo)
8673 const struct got_error *err = NULL;
8674 char *fileindex_path = NULL;
8675 struct got_reference *wt_branch = NULL;
8676 struct got_object_id *wt_branch_tip = NULL;
8677 struct check_rebase_ok_arg ok_arg;
8681 err = lock_worktree(worktree, LOCK_EX);
8685 err = open_fileindex(fileindex, &fileindex_path, worktree);
8689 /* Preconditions are the same as for rebase. */
8690 ok_arg.worktree = worktree;
8692 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8697 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8702 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8706 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8707 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8712 free(fileindex_path);
8714 got_ref_close(wt_branch);
8715 free(wt_branch_tip);
8718 got_fileindex_free(*fileindex);
8721 lock_worktree(worktree, LOCK_SH);
8726 const struct got_error *got_worktree_merge_write_refs(
8727 struct got_worktree *worktree, struct got_reference *branch,
8728 struct got_repository *repo)
8730 const struct got_error *err = NULL;
8731 char *branch_refname = NULL, *commit_refname = NULL;
8732 struct got_reference *branch_ref = NULL, *commit_ref = NULL;
8733 struct got_object_id *branch_tip = NULL;
8735 err = get_merge_branch_ref_name(&branch_refname, worktree);
8739 err = get_merge_commit_ref_name(&commit_refname, worktree);
8743 err = got_ref_resolve(&branch_tip, repo, branch);
8747 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8750 err = got_ref_write(branch_ref, repo);
8754 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8757 err = got_ref_write(commit_ref, repo);
8762 free(branch_refname);
8763 free(commit_refname);
8765 got_ref_close(branch_ref);
8767 got_ref_close(commit_ref);
8772 const struct got_error *
8773 got_worktree_merge_continue(char **branch_name,
8774 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8775 struct got_worktree *worktree, struct got_repository *repo)
8777 const struct got_error *err;
8778 char *commit_refname = NULL, *branch_refname = NULL;
8779 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8780 char *fileindex_path = NULL;
8781 int have_staged_files = 0;
8783 *branch_name = NULL;
8787 err = lock_worktree(worktree, LOCK_EX);
8791 err = open_fileindex(fileindex, &fileindex_path, worktree);
8795 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8796 &have_staged_files);
8797 if (err && err->code != GOT_ERR_CANCELLED)
8799 if (have_staged_files) {
8800 err = got_error(GOT_ERR_STAGED_PATHS);
8804 err = get_merge_branch_ref_name(&branch_refname, worktree);
8808 err = get_merge_commit_ref_name(&commit_refname, worktree);
8812 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8816 if (!got_ref_is_symbolic(branch_ref)) {
8817 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8818 "%s is not a symbolic reference",
8819 got_ref_get_name(branch_ref));
8822 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8823 if (*branch_name == NULL) {
8824 err = got_error_from_errno("strdup");
8828 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8832 err = got_ref_resolve(branch_tip, repo, commit_ref);
8836 free(commit_refname);
8837 free(branch_refname);
8838 free(fileindex_path);
8840 got_ref_close(commit_ref);
8842 got_ref_close(branch_ref);
8846 *branch_name = NULL;
8851 got_fileindex_free(*fileindex);
8854 lock_worktree(worktree, LOCK_SH);
8859 const struct got_error *
8860 got_worktree_merge_abort(struct got_worktree *worktree,
8861 struct got_fileindex *fileindex, struct got_repository *repo,
8862 got_worktree_checkout_cb progress_cb, void *progress_arg)
8864 const struct got_error *err, *unlockerr, *sync_err;
8865 struct got_commit_object *commit = NULL;
8866 char *fileindex_path = NULL;
8867 struct revert_file_args rfa;
8868 char *commit_ref_name = NULL;
8869 struct got_reference *commit_ref = NULL;
8870 struct got_object_id *merged_commit_id = NULL;
8871 struct got_object_id *tree_id = NULL;
8872 struct got_pathlist_head added_paths;
8874 TAILQ_INIT(&added_paths);
8876 err = get_merge_commit_ref_name(&commit_ref_name, worktree);
8880 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8884 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8889 * Determine which files in added status can be safely removed
8890 * from disk while reverting changes in the work tree.
8891 * We want to avoid deleting unrelated files which were added by
8892 * the user for conflict resolution purposes.
8894 err = get_paths_added_between_commits(&added_paths,
8895 got_worktree_get_base_commit_id(worktree), merged_commit_id,
8896 got_worktree_get_path_prefix(worktree), repo);
8901 err = got_object_open_as_commit(&commit, repo,
8902 worktree->base_commit_id);
8906 err = got_object_id_by_path(&tree_id, repo, commit,
8907 worktree->path_prefix);
8911 err = delete_merge_refs(worktree, repo);
8915 err = get_fileindex_path(&fileindex_path, worktree);
8919 rfa.worktree = worktree;
8920 rfa.fileindex = fileindex;
8921 rfa.progress_cb = progress_cb;
8922 rfa.progress_arg = progress_arg;
8923 rfa.patch_cb = NULL;
8924 rfa.patch_arg = NULL;
8926 rfa.unlink_added_files = 1;
8927 rfa.added_files_to_unlink = &added_paths;
8928 err = worktree_status(worktree, "", fileindex, repo,
8929 revert_file, &rfa, NULL, NULL, 1, 0);
8933 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8934 repo, progress_cb, progress_arg, NULL, NULL);
8936 sync_err = sync_fileindex(fileindex, fileindex_path);
8937 if (sync_err && err == NULL)
8941 free(merged_commit_id);
8943 got_object_commit_close(commit);
8945 got_fileindex_free(fileindex);
8946 free(fileindex_path);
8948 got_ref_close(commit_ref);
8949 free(commit_ref_name);
8951 unlockerr = lock_worktree(worktree, LOCK_SH);
8952 if (unlockerr && err == NULL)
8957 struct check_stage_ok_arg {
8958 struct got_object_id *head_commit_id;
8959 struct got_worktree *worktree;
8960 struct got_fileindex *fileindex;
8961 struct got_repository *repo;
8965 static const struct got_error *
8966 check_stage_ok(void *arg, unsigned char status,
8967 unsigned char staged_status, const char *relpath,
8968 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8969 struct got_object_id *commit_id, int dirfd, const char *de_name)
8971 struct check_stage_ok_arg *a = arg;
8972 const struct got_error *err = NULL;
8973 struct got_fileindex_entry *ie;
8974 struct got_object_id base_commit_id;
8975 struct got_object_id *base_commit_idp = NULL;
8976 char *in_repo_path = NULL, *p;
8978 if (status == GOT_STATUS_UNVERSIONED ||
8979 status == GOT_STATUS_NO_CHANGE)
8981 if (status == GOT_STATUS_NONEXISTENT)
8982 return got_error_set_errno(ENOENT, relpath);
8984 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8986 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8988 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8989 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8991 return got_error_from_errno("asprintf");
8993 if (got_fileindex_entry_has_commit(ie)) {
8994 base_commit_idp = got_fileindex_entry_get_commit_id(
8995 &base_commit_id, ie);
8998 if (status == GOT_STATUS_CONFLICT) {
8999 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
9001 } else if (status != GOT_STATUS_ADD &&
9002 status != GOT_STATUS_MODIFY &&
9003 status != GOT_STATUS_DELETE) {
9004 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
9008 a->have_changes = 1;
9013 err = check_out_of_date(p, status, staged_status,
9014 blob_id, base_commit_idp, a->head_commit_id, a->repo,
9015 GOT_ERR_STAGE_OUT_OF_DATE);
9021 struct stage_path_arg {
9022 struct got_worktree *worktree;
9023 struct got_fileindex *fileindex;
9024 struct got_repository *repo;
9025 got_worktree_status_cb status_cb;
9027 got_worktree_patch_cb patch_cb;
9029 int staged_something;
9030 int allow_bad_symlinks;
9033 static const struct got_error *
9034 stage_path(void *arg, unsigned char status,
9035 unsigned char staged_status, const char *relpath,
9036 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9037 struct got_object_id *commit_id, int dirfd, const char *de_name)
9039 struct stage_path_arg *a = arg;
9040 const struct got_error *err = NULL;
9041 struct got_fileindex_entry *ie;
9042 char *ondisk_path = NULL, *path_content = NULL;
9044 struct got_object_id *new_staged_blob_id = NULL;
9047 if (status == GOT_STATUS_UNVERSIONED)
9050 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9052 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9054 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
9056 return got_error_from_errno("asprintf");
9059 case GOT_STATUS_ADD:
9060 case GOT_STATUS_MODIFY:
9061 /* XXX could sb.st_mode be passed in by our caller? */
9062 if (lstat(ondisk_path, &sb) == -1) {
9063 err = got_error_from_errno2("lstat", ondisk_path);
9067 if (status == GOT_STATUS_ADD) {
9068 int choice = GOT_PATCH_CHOICE_NONE;
9069 err = (*a->patch_cb)(&choice, a->patch_arg,
9070 status, ie->path, NULL, 1, 1);
9073 if (choice != GOT_PATCH_CHOICE_YES)
9076 err = create_patched_content(&path_content, 0,
9077 staged_blob_id ? staged_blob_id : blob_id,
9078 ondisk_path, dirfd, de_name, ie->path,
9079 a->repo, a->patch_cb, a->patch_arg);
9080 if (err || path_content == NULL)
9084 err = got_object_blob_create(&new_staged_blob_id,
9085 path_content ? path_content : ondisk_path, a->repo);
9088 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9089 SHA1_DIGEST_LENGTH);
9090 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
9091 stage = GOT_FILEIDX_STAGE_ADD;
9093 stage = GOT_FILEIDX_STAGE_MODIFY;
9094 got_fileindex_entry_stage_set(ie, stage);
9095 if (S_ISLNK(sb.st_mode)) {
9096 int is_bad_symlink = 0;
9097 if (!a->allow_bad_symlinks) {
9098 char target_path[PATH_MAX];
9100 target_len = readlink(ondisk_path, target_path,
9101 sizeof(target_path));
9102 if (target_len == -1) {
9103 err = got_error_from_errno2("readlink",
9107 err = is_bad_symlink_target(&is_bad_symlink,
9108 target_path, target_len, ondisk_path,
9109 a->worktree->root_path,
9110 a->worktree->meta_dir);
9113 if (is_bad_symlink) {
9114 err = got_error_path(ondisk_path,
9115 GOT_ERR_BAD_SYMLINK);
9120 got_fileindex_entry_staged_filetype_set(ie,
9121 GOT_FILEIDX_MODE_BAD_SYMLINK);
9123 got_fileindex_entry_staged_filetype_set(ie,
9124 GOT_FILEIDX_MODE_SYMLINK);
9126 got_fileindex_entry_staged_filetype_set(ie,
9127 GOT_FILEIDX_MODE_REGULAR_FILE);
9129 a->staged_something = 1;
9130 if (a->status_cb == NULL)
9132 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
9133 get_staged_status(ie), relpath, blob_id,
9134 new_staged_blob_id, NULL, dirfd, de_name);
9138 * When staging the reverse of the staged diff,
9139 * implicitly unstage the file.
9141 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
9142 sizeof(ie->blob_sha1)) == 0) {
9143 got_fileindex_entry_stage_set(ie,
9144 GOT_FILEIDX_STAGE_NONE);
9147 case GOT_STATUS_DELETE:
9148 if (staged_status == GOT_STATUS_DELETE)
9151 int choice = GOT_PATCH_CHOICE_NONE;
9152 err = (*a->patch_cb)(&choice, a->patch_arg, status,
9153 ie->path, NULL, 1, 1);
9156 if (choice == GOT_PATCH_CHOICE_NO)
9158 if (choice != GOT_PATCH_CHOICE_YES) {
9159 err = got_error(GOT_ERR_PATCH_CHOICE);
9163 stage = GOT_FILEIDX_STAGE_DELETE;
9164 got_fileindex_entry_stage_set(ie, stage);
9165 a->staged_something = 1;
9166 if (a->status_cb == NULL)
9168 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
9169 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
9172 case GOT_STATUS_NO_CHANGE:
9174 case GOT_STATUS_CONFLICT:
9175 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
9177 case GOT_STATUS_NONEXISTENT:
9178 err = got_error_set_errno(ENOENT, relpath);
9181 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
9185 if (path_content && unlink(path_content) == -1 && err == NULL)
9186 err = got_error_from_errno2("unlink", path_content);
9189 free(new_staged_blob_id);
9193 const struct got_error *
9194 got_worktree_stage(struct got_worktree *worktree,
9195 struct got_pathlist_head *paths,
9196 got_worktree_status_cb status_cb, void *status_arg,
9197 got_worktree_patch_cb patch_cb, void *patch_arg,
9198 int allow_bad_symlinks, struct got_repository *repo)
9200 const struct got_error *err = NULL, *sync_err, *unlockerr;
9201 struct got_pathlist_entry *pe;
9202 struct got_fileindex *fileindex = NULL;
9203 char *fileindex_path = NULL;
9204 struct got_reference *head_ref = NULL;
9205 struct got_object_id *head_commit_id = NULL;
9206 struct check_stage_ok_arg oka;
9207 struct stage_path_arg spa;
9209 err = lock_worktree(worktree, LOCK_EX);
9213 err = got_ref_open(&head_ref, repo,
9214 got_worktree_get_head_ref_name(worktree), 0);
9217 err = got_ref_resolve(&head_commit_id, repo, head_ref);
9220 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9224 /* Check pre-conditions before staging anything. */
9225 oka.head_commit_id = head_commit_id;
9226 oka.worktree = worktree;
9227 oka.fileindex = fileindex;
9229 oka.have_changes = 0;
9230 TAILQ_FOREACH(pe, paths, entry) {
9231 err = worktree_status(worktree, pe->path, fileindex, repo,
9232 check_stage_ok, &oka, NULL, NULL, 1, 0);
9236 if (!oka.have_changes) {
9237 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9241 spa.worktree = worktree;
9242 spa.fileindex = fileindex;
9244 spa.patch_cb = patch_cb;
9245 spa.patch_arg = patch_arg;
9246 spa.status_cb = status_cb;
9247 spa.status_arg = status_arg;
9248 spa.staged_something = 0;
9249 spa.allow_bad_symlinks = allow_bad_symlinks;
9250 TAILQ_FOREACH(pe, paths, entry) {
9251 err = worktree_status(worktree, pe->path, fileindex, repo,
9252 stage_path, &spa, NULL, NULL, 1, 0);
9256 if (!spa.staged_something) {
9257 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9261 sync_err = sync_fileindex(fileindex, fileindex_path);
9262 if (sync_err && err == NULL)
9266 got_ref_close(head_ref);
9267 free(head_commit_id);
9268 free(fileindex_path);
9270 got_fileindex_free(fileindex);
9271 unlockerr = lock_worktree(worktree, LOCK_SH);
9272 if (unlockerr && err == NULL)
9277 struct unstage_path_arg {
9278 struct got_worktree *worktree;
9279 struct got_fileindex *fileindex;
9280 struct got_repository *repo;
9281 got_worktree_checkout_cb progress_cb;
9283 got_worktree_patch_cb patch_cb;
9287 static const struct got_error *
9288 create_unstaged_content(char **path_unstaged_content,
9289 char **path_new_staged_content, struct got_object_id *blob_id,
9290 struct got_object_id *staged_blob_id, const char *relpath,
9291 struct got_repository *repo,
9292 got_worktree_patch_cb patch_cb, void *patch_arg)
9294 const struct got_error *err, *free_err;
9295 struct got_blob_object *blob = NULL, *staged_blob = NULL;
9296 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
9297 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
9298 struct got_diffreg_result *diffreg_result = NULL;
9299 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
9300 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
9301 int fd1 = -1, fd2 = -1;
9303 *path_unstaged_content = NULL;
9304 *path_new_staged_content = NULL;
9306 err = got_object_id_str(&label1, blob_id);
9310 fd1 = got_opentempfd();
9312 err = got_error_from_errno("got_opentempfd");
9315 fd2 = got_opentempfd();
9317 err = got_error_from_errno("got_opentempfd");
9321 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
9325 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
9329 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
9333 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
9338 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
9342 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
9346 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
9347 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
9351 err = got_opentemp_named(path_unstaged_content, &outfile,
9352 "got-unstaged-content", "");
9355 err = got_opentemp_named(path_new_staged_content, &rejectfile,
9356 "got-new-staged-content", "");
9360 if (fseek(f1, 0L, SEEK_SET) == -1) {
9361 err = got_ferror(f1, GOT_ERR_IO);
9364 if (fseek(f2, 0L, SEEK_SET) == -1) {
9365 err = got_ferror(f2, GOT_ERR_IO);
9368 /* Count the number of actual changes in the diff result. */
9369 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9370 struct diff_chunk_context cc = {};
9371 diff_chunk_context_load_change(&cc, &nchunks_used,
9372 diffreg_result->result, n, 0);
9375 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9377 err = apply_or_reject_change(&choice, &nchunks_used,
9378 diffreg_result->result, n, relpath, f1, f2,
9379 &line_cur1, &line_cur2,
9380 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
9383 if (choice == GOT_PATCH_CHOICE_YES)
9386 have_rejected_content = 1;
9387 if (choice == GOT_PATCH_CHOICE_QUIT)
9390 if (have_content || have_rejected_content)
9391 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
9392 outfile, rejectfile);
9395 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9396 err = got_error_from_errno("close");
9398 got_object_blob_close(blob);
9399 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9400 err = got_error_from_errno("close");
9402 got_object_blob_close(staged_blob);
9403 free_err = got_diffreg_result_free(diffreg_result);
9404 if (free_err && err == NULL)
9406 if (f1 && fclose(f1) == EOF && err == NULL)
9407 err = got_error_from_errno2("fclose", path1);
9408 if (f2 && fclose(f2) == EOF && err == NULL)
9409 err = got_error_from_errno2("fclose", path2);
9410 if (outfile && fclose(outfile) == EOF && err == NULL)
9411 err = got_error_from_errno2("fclose", *path_unstaged_content);
9412 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
9413 err = got_error_from_errno2("fclose", *path_new_staged_content);
9414 if (path1 && unlink(path1) == -1 && err == NULL)
9415 err = got_error_from_errno2("unlink", path1);
9416 if (path2 && unlink(path2) == -1 && err == NULL)
9417 err = got_error_from_errno2("unlink", path2);
9418 if (err || !have_content) {
9419 if (*path_unstaged_content &&
9420 unlink(*path_unstaged_content) == -1 && err == NULL)
9421 err = got_error_from_errno2("unlink",
9422 *path_unstaged_content);
9423 free(*path_unstaged_content);
9424 *path_unstaged_content = NULL;
9426 if (err || !have_content || !have_rejected_content) {
9427 if (*path_new_staged_content &&
9428 unlink(*path_new_staged_content) == -1 && err == NULL)
9429 err = got_error_from_errno2("unlink",
9430 *path_new_staged_content);
9431 free(*path_new_staged_content);
9432 *path_new_staged_content = NULL;
9439 static const struct got_error *
9440 unstage_hunks(struct got_object_id *staged_blob_id,
9441 struct got_blob_object *blob_base,
9442 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
9443 const char *ondisk_path, const char *label_orig,
9444 struct got_worktree *worktree, struct got_repository *repo,
9445 got_worktree_patch_cb patch_cb, void *patch_arg,
9446 got_worktree_checkout_cb progress_cb, void *progress_arg)
9448 const struct got_error *err = NULL;
9449 char *path_unstaged_content = NULL;
9450 char *path_new_staged_content = NULL;
9451 char *parent = NULL, *base_path = NULL;
9452 char *blob_base_path = NULL;
9453 struct got_object_id *new_staged_blob_id = NULL;
9454 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
9457 err = create_unstaged_content(&path_unstaged_content,
9458 &path_new_staged_content, blob_id, staged_blob_id,
9459 ie->path, repo, patch_cb, patch_arg);
9463 if (path_unstaged_content == NULL)
9466 if (path_new_staged_content) {
9467 err = got_object_blob_create(&new_staged_blob_id,
9468 path_new_staged_content, repo);
9473 f = fopen(path_unstaged_content, "re");
9475 err = got_error_from_errno2("fopen",
9476 path_unstaged_content);
9479 if (fstat(fileno(f), &sb) == -1) {
9480 err = got_error_from_errno2("fstat", path_unstaged_content);
9483 if (got_fileindex_entry_staged_filetype_get(ie) ==
9484 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
9485 char link_target[PATH_MAX];
9487 r = fread(link_target, 1, sizeof(link_target), f);
9488 if (r == 0 && ferror(f)) {
9489 err = got_error_from_errno("fread");
9492 if (r >= sizeof(link_target)) { /* should not happen */
9493 err = got_error(GOT_ERR_NO_SPACE);
9496 link_target[r] = '\0';
9497 err = merge_symlink(worktree, blob_base,
9498 ondisk_path, ie->path, label_orig, link_target,
9499 worktree->base_commit_id, repo, progress_cb,
9502 int local_changes_subsumed;
9504 err = got_path_dirname(&parent, ondisk_path);
9508 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
9510 err = got_error_from_errno("asprintf");
9515 err = got_opentemp_named(&blob_base_path, &f_base,
9519 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
9525 * In order the run a 3-way merge with a symlink we copy the symlink's
9526 * target path into a temporary file and use that file with diff3.
9528 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9529 err = dump_symlink_target_path_to_file(&f_deriv2,
9535 fd = open(ondisk_path,
9536 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
9538 err = got_error_from_errno2("open", ondisk_path);
9541 f_deriv2 = fdopen(fd, "r");
9542 if (f_deriv2 == NULL) {
9543 err = got_error_from_errno2("fdopen", ondisk_path);
9549 err = merge_file(&local_changes_subsumed, worktree,
9550 f_base, f, f_deriv2, ondisk_path, ie->path,
9551 got_fileindex_perms_to_st(ie),
9552 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
9553 repo, progress_cb, progress_arg);
9558 if (new_staged_blob_id) {
9559 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9560 SHA1_DIGEST_LENGTH);
9562 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9563 got_fileindex_entry_staged_filetype_set(ie, 0);
9566 free(new_staged_blob_id);
9567 if (path_unstaged_content &&
9568 unlink(path_unstaged_content) == -1 && err == NULL)
9569 err = got_error_from_errno2("unlink", path_unstaged_content);
9570 if (path_new_staged_content &&
9571 unlink(path_new_staged_content) == -1 && err == NULL)
9572 err = got_error_from_errno2("unlink", path_new_staged_content);
9573 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
9574 err = got_error_from_errno2("unlink", blob_base_path);
9575 if (f_base && fclose(f_base) == EOF && err == NULL)
9576 err = got_error_from_errno2("fclose", path_unstaged_content);
9577 if (f && fclose(f) == EOF && err == NULL)
9578 err = got_error_from_errno2("fclose", path_unstaged_content);
9579 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
9580 err = got_error_from_errno2("fclose", ondisk_path);
9581 free(path_unstaged_content);
9582 free(path_new_staged_content);
9583 free(blob_base_path);
9589 static const struct got_error *
9590 unstage_path(void *arg, unsigned char status,
9591 unsigned char staged_status, const char *relpath,
9592 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9593 struct got_object_id *commit_id, int dirfd, const char *de_name)
9595 const struct got_error *err = NULL;
9596 struct unstage_path_arg *a = arg;
9597 struct got_fileindex_entry *ie;
9598 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
9599 char *ondisk_path = NULL;
9600 char *id_str = NULL, *label_orig = NULL;
9601 int local_changes_subsumed;
9603 int fd1 = -1, fd2 = -1;
9605 if (staged_status != GOT_STATUS_ADD &&
9606 staged_status != GOT_STATUS_MODIFY &&
9607 staged_status != GOT_STATUS_DELETE)
9610 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9612 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9614 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
9616 return got_error_from_errno("asprintf");
9618 err = got_object_id_str(&id_str,
9619 commit_id ? commit_id : a->worktree->base_commit_id);
9622 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9624 err = got_error_from_errno("asprintf");
9628 fd1 = got_opentempfd();
9630 err = got_error_from_errno("got_opentempfd");
9633 fd2 = got_opentempfd();
9635 err = got_error_from_errno("got_opentempfd");
9639 switch (staged_status) {
9640 case GOT_STATUS_MODIFY:
9641 err = got_object_open_as_blob(&blob_base, a->repo,
9642 blob_id, 8192, fd1);
9646 case GOT_STATUS_ADD:
9648 if (staged_status == GOT_STATUS_ADD) {
9649 int choice = GOT_PATCH_CHOICE_NONE;
9650 err = (*a->patch_cb)(&choice, a->patch_arg,
9651 staged_status, ie->path, NULL, 1, 1);
9654 if (choice != GOT_PATCH_CHOICE_YES)
9657 err = unstage_hunks(staged_blob_id,
9658 blob_base, blob_id, ie, ondisk_path,
9659 label_orig, a->worktree, a->repo,
9660 a->patch_cb, a->patch_arg,
9661 a->progress_cb, a->progress_arg);
9662 break; /* Done with this file. */
9665 err = got_object_open_as_blob(&blob_staged, a->repo,
9666 staged_blob_id, 8192, fd2);
9669 switch (got_fileindex_entry_staged_filetype_get(ie)) {
9670 case GOT_FILEIDX_MODE_BAD_SYMLINK:
9671 case GOT_FILEIDX_MODE_REGULAR_FILE:
9672 err = merge_blob(&local_changes_subsumed, a->worktree,
9673 blob_base, ondisk_path, relpath,
9674 got_fileindex_perms_to_st(ie), label_orig,
9675 blob_staged, commit_id ? commit_id :
9676 a->worktree->base_commit_id, a->repo,
9677 a->progress_cb, a->progress_arg);
9679 case GOT_FILEIDX_MODE_SYMLINK:
9680 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9681 char *staged_target;
9682 err = got_object_blob_read_to_str(
9683 &staged_target, blob_staged);
9686 err = merge_symlink(a->worktree, blob_base,
9687 ondisk_path, relpath, label_orig,
9688 staged_target, commit_id ? commit_id :
9689 a->worktree->base_commit_id,
9690 a->repo, a->progress_cb, a->progress_arg);
9691 free(staged_target);
9693 err = merge_blob(&local_changes_subsumed,
9694 a->worktree, blob_base, ondisk_path,
9695 relpath, got_fileindex_perms_to_st(ie),
9696 label_orig, blob_staged,
9697 commit_id ? commit_id :
9698 a->worktree->base_commit_id, a->repo,
9699 a->progress_cb, a->progress_arg);
9703 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9707 got_fileindex_entry_stage_set(ie,
9708 GOT_FILEIDX_STAGE_NONE);
9709 got_fileindex_entry_staged_filetype_set(ie, 0);
9712 case GOT_STATUS_DELETE:
9714 int choice = GOT_PATCH_CHOICE_NONE;
9715 err = (*a->patch_cb)(&choice, a->patch_arg,
9716 staged_status, ie->path, NULL, 1, 1);
9719 if (choice == GOT_PATCH_CHOICE_NO)
9721 if (choice != GOT_PATCH_CHOICE_YES) {
9722 err = got_error(GOT_ERR_PATCH_CHOICE);
9726 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9727 got_fileindex_entry_staged_filetype_set(ie, 0);
9728 err = get_file_status(&status, &sb, ie, ondisk_path,
9729 dirfd, de_name, a->repo);
9732 err = (*a->progress_cb)(a->progress_arg, status, relpath);
9737 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9738 err = got_error_from_errno("close");
9740 got_object_blob_close(blob_base);
9741 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9742 err = got_error_from_errno("close");
9744 got_object_blob_close(blob_staged);
9750 const struct got_error *
9751 got_worktree_unstage(struct got_worktree *worktree,
9752 struct got_pathlist_head *paths,
9753 got_worktree_checkout_cb progress_cb, void *progress_arg,
9754 got_worktree_patch_cb patch_cb, void *patch_arg,
9755 struct got_repository *repo)
9757 const struct got_error *err = NULL, *sync_err, *unlockerr;
9758 struct got_pathlist_entry *pe;
9759 struct got_fileindex *fileindex = NULL;
9760 char *fileindex_path = NULL;
9761 struct unstage_path_arg upa;
9763 err = lock_worktree(worktree, LOCK_EX);
9767 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9771 upa.worktree = worktree;
9772 upa.fileindex = fileindex;
9774 upa.progress_cb = progress_cb;
9775 upa.progress_arg = progress_arg;
9776 upa.patch_cb = patch_cb;
9777 upa.patch_arg = patch_arg;
9778 TAILQ_FOREACH(pe, paths, entry) {
9779 err = worktree_status(worktree, pe->path, fileindex, repo,
9780 unstage_path, &upa, NULL, NULL, 1, 0);
9785 sync_err = sync_fileindex(fileindex, fileindex_path);
9786 if (sync_err && err == NULL)
9789 free(fileindex_path);
9791 got_fileindex_free(fileindex);
9792 unlockerr = lock_worktree(worktree, LOCK_SH);
9793 if (unlockerr && err == NULL)
9798 struct report_file_info_arg {
9799 struct got_worktree *worktree;
9800 got_worktree_path_info_cb info_cb;
9802 struct got_pathlist_head *paths;
9803 got_cancel_cb cancel_cb;
9807 static const struct got_error *
9808 report_file_info(void *arg, struct got_fileindex_entry *ie)
9810 const struct got_error *err;
9811 struct report_file_info_arg *a = arg;
9812 struct got_pathlist_entry *pe;
9813 struct got_object_id blob_id, staged_blob_id, commit_id;
9814 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9815 struct got_object_id *commit_idp = NULL;
9819 err = a->cancel_cb(a->cancel_arg);
9824 TAILQ_FOREACH(pe, a->paths, entry) {
9825 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9826 got_path_is_child(ie->path, pe->path, pe->path_len))
9829 if (pe == NULL) /* not found */
9832 if (got_fileindex_entry_has_blob(ie))
9833 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9834 stage = got_fileindex_entry_stage_get(ie);
9835 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9836 stage == GOT_FILEIDX_STAGE_ADD) {
9837 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9838 &staged_blob_id, ie);
9841 if (got_fileindex_entry_has_commit(ie))
9842 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9844 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9845 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9848 const struct got_error *
9849 got_worktree_path_info(struct got_worktree *worktree,
9850 struct got_pathlist_head *paths,
9851 got_worktree_path_info_cb info_cb, void *info_arg,
9852 got_cancel_cb cancel_cb, void *cancel_arg)
9855 const struct got_error *err = NULL, *unlockerr;
9856 struct got_fileindex *fileindex = NULL;
9857 char *fileindex_path = NULL;
9858 struct report_file_info_arg arg;
9860 err = lock_worktree(worktree, LOCK_SH);
9864 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9868 arg.worktree = worktree;
9869 arg.info_cb = info_cb;
9870 arg.info_arg = info_arg;
9872 arg.cancel_cb = cancel_cb;
9873 arg.cancel_arg = cancel_arg;
9874 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9877 free(fileindex_path);
9879 got_fileindex_free(fileindex);
9880 unlockerr = lock_worktree(worktree, LOCK_UN);
9881 if (unlockerr && err == NULL)
9886 static const struct got_error *
9887 patch_check_path(const char *p, char **path, unsigned char *status,
9888 unsigned char *staged_status, struct got_fileindex *fileindex,
9889 struct got_worktree *worktree, struct got_repository *repo)
9891 const struct got_error *err;
9892 struct got_fileindex_entry *ie;
9894 char *ondisk_path = NULL;
9896 err = got_worktree_resolve_path(path, worktree, p);
9900 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9901 *path[0] ? "/" : "", *path) == -1)
9902 return got_error_from_errno("asprintf");
9904 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9906 *staged_status = get_staged_status(ie);
9907 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9912 *staged_status = GOT_STATUS_NO_CHANGE;
9913 *status = GOT_STATUS_UNVERSIONED;
9914 if (lstat(ondisk_path, &sb) == -1) {
9915 if (errno != ENOENT) {
9916 err = got_error_from_errno2("lstat",
9920 *status = GOT_STATUS_NONEXISTENT;
9929 static const struct got_error *
9930 patch_can_rm(const char *path, unsigned char status,
9931 unsigned char staged_status)
9933 if (status == GOT_STATUS_NONEXISTENT)
9934 return got_error_set_errno(ENOENT, path);
9935 if (status != GOT_STATUS_NO_CHANGE &&
9936 status != GOT_STATUS_ADD &&
9937 status != GOT_STATUS_MODIFY &&
9938 status != GOT_STATUS_MODE_CHANGE)
9939 return got_error_path(path, GOT_ERR_FILE_STATUS);
9940 if (staged_status == GOT_STATUS_DELETE)
9941 return got_error_path(path, GOT_ERR_FILE_STATUS);
9945 static const struct got_error *
9946 patch_can_add(const char *path, unsigned char status)
9948 if (status != GOT_STATUS_NONEXISTENT)
9949 return got_error_path(path, GOT_ERR_FILE_STATUS);
9953 static const struct got_error *
9954 patch_can_edit(const char *path, unsigned char status,
9955 unsigned char staged_status)
9957 if (status == GOT_STATUS_NONEXISTENT)
9958 return got_error_set_errno(ENOENT, path);
9959 if (status != GOT_STATUS_NO_CHANGE &&
9960 status != GOT_STATUS_ADD &&
9961 status != GOT_STATUS_MODIFY)
9962 return got_error_path(path, GOT_ERR_FILE_STATUS);
9963 if (staged_status == GOT_STATUS_DELETE)
9964 return got_error_path(path, GOT_ERR_FILE_STATUS);
9968 const struct got_error *
9969 got_worktree_patch_prepare(struct got_fileindex **fileindex,
9970 char **fileindex_path, struct got_worktree *worktree)
9972 return open_fileindex(fileindex, fileindex_path, worktree);
9975 const struct got_error *
9976 got_worktree_patch_check_path(const char *old, const char *new,
9977 char **oldpath, char **newpath, struct got_worktree *worktree,
9978 struct got_repository *repo, struct got_fileindex *fileindex)
9980 const struct got_error *err = NULL;
9981 int file_renamed = 0;
9982 unsigned char status_old, staged_status_old;
9983 unsigned char status_new, staged_status_new;
9988 err = patch_check_path(old != NULL ? old : new, oldpath,
9989 &status_old, &staged_status_old, fileindex, worktree, repo);
9993 err = patch_check_path(new != NULL ? new : old, newpath,
9994 &status_new, &staged_status_new, fileindex, worktree, repo);
9998 if (old != NULL && new != NULL && strcmp(old, new) != 0)
10001 if (old != NULL && new == NULL)
10002 err = patch_can_rm(*oldpath, status_old, staged_status_old);
10003 else if (file_renamed) {
10004 err = patch_can_rm(*oldpath, status_old, staged_status_old);
10006 err = patch_can_add(*newpath, status_new);
10007 } else if (old == NULL)
10008 err = patch_can_add(*newpath, status_new);
10010 err = patch_can_edit(*newpath, status_new, staged_status_new);
10022 const struct got_error *
10023 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
10024 struct got_worktree *worktree, struct got_fileindex *fileindex,
10025 got_worktree_checkout_cb progress_cb, void *progress_arg)
10027 struct schedule_addition_args saa;
10029 memset(&saa, 0, sizeof(saa));
10030 saa.worktree = worktree;
10031 saa.fileindex = fileindex;
10032 saa.progress_cb = progress_cb;
10033 saa.progress_arg = progress_arg;
10036 return worktree_status(worktree, path, fileindex, repo,
10037 schedule_addition, &saa, NULL, NULL, 1, 0);
10040 const struct got_error *
10041 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
10042 struct got_worktree *worktree, struct got_fileindex *fileindex,
10043 got_worktree_delete_cb progress_cb, void *progress_arg)
10045 const struct got_error *err;
10046 struct schedule_deletion_args sda;
10047 char *ondisk_status_path;
10049 memset(&sda, 0, sizeof(sda));
10050 sda.worktree = worktree;
10051 sda.fileindex = fileindex;
10052 sda.progress_cb = progress_cb;
10053 sda.progress_arg = progress_arg;
10055 sda.delete_local_mods = 0;
10056 sda.keep_on_disk = 0;
10057 sda.ignore_missing_paths = 0;
10058 sda.status_codes = NULL;
10059 if (asprintf(&ondisk_status_path, "%s/%s",
10060 got_worktree_get_root_path(worktree), path) == -1)
10061 return got_error_from_errno("asprintf");
10062 sda.status_path = ondisk_status_path;
10063 sda.status_path_len = strlen(ondisk_status_path);
10065 err = worktree_status(worktree, path, fileindex, repo,
10066 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
10067 free(ondisk_status_path);
10071 const struct got_error *
10072 got_worktree_patch_complete(struct got_fileindex *fileindex,
10073 const char *fileindex_path)
10075 const struct got_error *err = NULL;
10077 err = sync_fileindex(fileindex, fileindex_path);
10078 got_fileindex_free(fileindex);