2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/queue.h>
39 #include "got_error.h"
40 #include "got_repository.h"
41 #include "got_reference.h"
42 #include "got_object.h"
44 #include "got_cancel.h"
45 #include "got_worktree.h"
46 #include "got_opentemp.h"
49 #include "got_lib_worktree.h"
50 #include "got_lib_hash.h"
51 #include "got_lib_fileindex.h"
52 #include "got_lib_inflate.h"
53 #include "got_lib_delta.h"
54 #include "got_lib_object.h"
55 #include "got_lib_object_parse.h"
56 #include "got_lib_object_create.h"
57 #include "got_lib_object_idset.h"
58 #include "got_lib_diff.h"
59 #include "got_lib_gotconfig.h"
62 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
65 #define GOT_MERGE_LABEL_MERGED "merged change"
66 #define GOT_MERGE_LABEL_BASE "3-way merge base"
69 apply_umask(mode_t mode)
78 static const struct got_error *
79 create_meta_file(const char *path_got, const char *name, const char *content)
81 const struct got_error *err = NULL;
84 if (asprintf(&path, "%s/%s", path_got, name) == -1)
85 return got_error_from_errno("asprintf");
87 err = got_path_create_file(path, content);
92 static const struct got_error *
93 update_meta_file(const char *path_got, const char *name, const char *content)
95 const struct got_error *err = NULL;
100 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
101 err = got_error_from_errno("asprintf");
106 err = got_opentemp_named(&tmppath, &tmpfile, path, "");
111 int len = fprintf(tmpfile, "%s\n", content);
112 if (len != strlen(content) + 1) {
113 err = got_error_from_errno2("fprintf", tmppath);
118 if (rename(tmppath, path) != 0) {
119 err = got_error_from_errno3("rename", tmppath, path);
125 if (fclose(tmpfile) == EOF && err == NULL)
126 err = got_error_from_errno2("fclose", tmppath);
131 static const struct got_error *
132 write_head_ref(const char *path_got, struct got_reference *head_ref)
134 const struct got_error *err = NULL;
137 if (got_ref_is_symbolic(head_ref)) {
138 refstr = got_ref_to_str(head_ref);
140 return got_error_from_errno("got_ref_to_str");
142 refstr = strdup(got_ref_get_name(head_ref));
144 return got_error_from_errno("strdup");
146 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
151 const struct got_error *
152 got_worktree_init(const char *path, struct got_reference *head_ref,
153 const char *prefix, const char *meta_dir, struct got_repository *repo)
155 const struct got_error *err = NULL;
156 struct got_object_id *commit_id = NULL;
158 uint32_t uuid_status;
160 char *path_got = NULL;
161 char *formatstr = NULL;
162 char *absprefix = NULL;
163 char *basestr = NULL;
164 char *uuidstr = NULL;
166 if (strcmp(path, got_repo_get_path(repo)) == 0) {
167 err = got_error(GOT_ERR_WORKTREE_REPO);
171 err = got_ref_resolve(&commit_id, repo, head_ref);
174 err = got_object_get_type(&obj_type, repo, commit_id);
177 if (obj_type != GOT_OBJ_TYPE_COMMIT)
178 return got_error(GOT_ERR_OBJ_TYPE);
180 if (!got_path_is_absolute(prefix)) {
181 if (asprintf(&absprefix, "/%s", prefix) == -1)
182 return got_error_from_errno("asprintf");
185 /* Create top-level directory (may already exist). */
186 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
187 err = got_error_from_errno2("mkdir", path);
191 /* Create .got/.cvg directory (may already exist). */
192 if (asprintf(&path_got, "%s/%s", path, meta_dir) == -1) {
193 err = got_error_from_errno("asprintf");
196 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
197 err = got_error_from_errno2("mkdir", path_got);
201 /* Create an empty lock file. */
202 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
206 /* Create an empty file index. */
207 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
211 /* Write the HEAD reference. */
212 err = write_head_ref(path_got, head_ref);
216 /* Record our base commit. */
217 err = got_object_id_str(&basestr, commit_id);
220 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
224 /* Store path to repository. */
225 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
226 got_repo_get_path(repo));
230 /* Store in-repository path prefix. */
231 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
232 absprefix ? absprefix : prefix);
237 uuid_create(&uuid, &uuid_status);
238 if (uuid_status != uuid_s_ok) {
239 err = got_error_uuid(uuid_status, "uuid_create");
242 uuid_to_string(&uuid, &uuidstr, &uuid_status);
243 if (uuid_status != uuid_s_ok) {
244 err = got_error_uuid(uuid_status, "uuid_to_string");
247 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
251 /* Stamp work tree with format file. */
252 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
253 err = got_error_from_errno("asprintf");
256 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
270 const struct got_error *
271 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
272 const char *path_prefix)
274 char *absprefix = NULL;
276 if (!got_path_is_absolute(path_prefix)) {
277 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
278 return got_error_from_errno("asprintf");
280 *match = (strcmp(absprefix ? absprefix : path_prefix,
281 worktree->path_prefix) == 0);
287 got_worktree_get_head_ref_name(struct got_worktree *worktree)
289 return worktree->head_ref_name;
292 const struct got_error *
293 got_worktree_set_head_ref(struct got_worktree *worktree,
294 struct got_reference *head_ref)
296 const struct got_error *err = NULL;
297 char *path_got = NULL, *head_ref_name = NULL;
299 if (asprintf(&path_got, "%s/%s", worktree->root_path,
300 worktree->meta_dir) == -1) {
301 err = got_error_from_errno("asprintf");
306 head_ref_name = strdup(got_ref_get_name(head_ref));
307 if (head_ref_name == NULL) {
308 err = got_error_from_errno("strdup");
312 err = write_head_ref(path_got, head_ref);
316 free(worktree->head_ref_name);
317 worktree->head_ref_name = head_ref_name;
325 struct got_object_id *
326 got_worktree_get_base_commit_id(struct got_worktree *worktree)
328 return worktree->base_commit_id;
331 const struct got_error *
332 got_worktree_set_base_commit_id(struct got_worktree *worktree,
333 struct got_repository *repo, struct got_object_id *commit_id)
335 const struct got_error *err;
336 struct got_object *obj = NULL;
338 char *path_got = NULL;
340 if (asprintf(&path_got, "%s/%s", worktree->root_path,
341 worktree->meta_dir) == -1) {
342 err = got_error_from_errno("asprintf");
347 err = got_object_open(&obj, repo, commit_id);
351 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
352 err = got_error(GOT_ERR_OBJ_TYPE);
356 /* Record our base commit. */
357 err = got_object_id_str(&id_str, commit_id);
360 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
364 free(worktree->base_commit_id);
365 worktree->base_commit_id = got_object_id_dup(commit_id);
366 if (worktree->base_commit_id == NULL) {
367 err = got_error_from_errno("got_object_id_dup");
372 got_object_close(obj);
378 const struct got_gotconfig *
379 got_worktree_get_gotconfig(struct got_worktree *worktree)
381 return worktree->gotconfig;
384 static const struct got_error *
385 lock_worktree(struct got_worktree *worktree, int operation)
387 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
388 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
389 : got_error_from_errno2("flock",
390 got_worktree_get_root_path(worktree)));
394 static const struct got_error *
395 add_dir_on_disk(struct got_worktree *worktree, const char *path)
397 const struct got_error *err = NULL;
400 /* We only accept worktree-relative paths here. */
401 if (got_path_is_absolute(path)) {
402 return got_error_fmt(GOT_ERR_BAD_PATH,
403 "%s does not accept absolute paths: %s",
407 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
408 return got_error_from_errno("asprintf");
410 err = got_path_mkdir(abspath);
411 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
414 if (lstat(abspath, &sb) == -1) {
415 err = got_error_from_errno2("lstat", abspath);
416 } else if (!S_ISDIR(sb.st_mode)) {
417 /* TODO directory is obstructed; do something */
418 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
425 static const struct got_error *
426 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
428 const struct got_error *err = NULL;
431 size_t flen1 = 0, flen2 = 0;
436 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
437 if (flen1 == 0 && ferror(f1)) {
438 err = got_error_from_errno("fread");
441 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
442 if (flen2 == 0 && ferror(f2)) {
443 err = got_error_from_errno("fread");
450 } else if (flen2 == 0) {
454 } else if (flen1 == flen2) {
455 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
468 static const struct got_error *
469 check_files_equal(int *same, FILE *f1, FILE *f2)
476 if (fstat(fileno(f1), &sb) != 0)
477 return got_error_from_errno("fstat");
480 if (fstat(fileno(f2), &sb) != 0)
481 return got_error_from_errno("fstat");
484 if (size1 != size2) {
489 if (fseek(f1, 0L, SEEK_SET) == -1)
490 return got_ferror(f1, GOT_ERR_IO);
491 if (fseek(f2, 0L, SEEK_SET) == -1)
492 return got_ferror(f2, GOT_ERR_IO);
494 return check_file_contents_equal(same, f1, f2);
497 static const struct got_error *
498 copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
506 if (fseek(f, 0L, SEEK_SET) == -1)
507 return got_ferror(f, GOT_ERR_IO);
510 flen = fread(fbuf, 1, sizeof(fbuf), f);
513 return got_error_from_errno("fread");
517 outlen = write(outfd, fbuf, flen);
519 return got_error_from_errno("write");
521 return got_error(GOT_ERR_IO);
528 static const struct got_error *
529 merge_binary_file(int *overlapcnt, int merged_fd,
530 FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
531 const char *label_deriv, const char *label_orig, const char *label_deriv2,
532 const char *ondisk_path)
534 const struct got_error *err = NULL;
535 int same_content, changed_deriv, changed_deriv2;
536 int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
537 off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
538 char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
539 char *base_path_orig = NULL, *base_path_deriv = NULL;
540 char *base_path_deriv2 = NULL;
544 err = check_files_equal(&same_content, f_deriv, f_deriv2);
549 return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
551 err = check_files_equal(&same_content, f_deriv, f_orig);
554 changed_deriv = !same_content;
555 err = check_files_equal(&same_content, f_deriv2, f_orig);
558 changed_deriv2 = !same_content;
560 if (changed_deriv && changed_deriv2) {
562 if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
563 err = got_error_from_errno("asprintf");
566 if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
567 err = got_error_from_errno("asprintf");
570 if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
571 err = got_error_from_errno("asprintf");
574 err = got_opentemp_named_fd(&path_orig, &fd_orig,
578 err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
579 base_path_deriv, "");
582 err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
583 base_path_deriv2, "");
586 err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
589 err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
592 err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
595 if (dprintf(merged_fd, "Binary files differ and cannot be "
596 "merged automatically:\n") < 0) {
597 err = got_error_from_errno("dprintf");
600 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
601 GOT_DIFF_CONFLICT_MARKER_BEGIN,
602 label_deriv ? " " : "",
603 label_deriv ? label_deriv : "",
605 err = got_error_from_errno("dprintf");
609 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
610 GOT_DIFF_CONFLICT_MARKER_ORIG,
611 label_orig ? " " : "",
612 label_orig ? label_orig : "",
614 err = got_error_from_errno("dprintf");
618 if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
619 GOT_DIFF_CONFLICT_MARKER_SEP,
621 GOT_DIFF_CONFLICT_MARKER_END,
622 label_deriv2 ? " " : "",
623 label_deriv2 ? label_deriv2 : "") < 0) {
624 err = got_error_from_errno("dprintf");
627 } else if (changed_deriv)
628 err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
629 else if (changed_deriv2)
630 err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
632 if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
634 err = got_error_from_errno2("unlink", path_orig);
635 if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
636 err = got_error_from_errno2("close", path_orig);
637 if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
638 err = got_error_from_errno2("close", path_deriv);
639 if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
640 err = got_error_from_errno2("close", path_deriv2);
644 free(base_path_orig);
645 free(base_path_deriv);
646 free(base_path_deriv2);
651 * Perform a 3-way merge where the file f_orig acts as the common
652 * ancestor, the file f_deriv acts as the first derived version,
653 * and the file f_deriv2 acts as the second derived version.
654 * The merge result will be written to a new file at ondisk_path; any
655 * existing file at this path will be replaced.
657 static const struct got_error *
658 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
659 FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
660 const char *path, uint16_t st_mode,
661 const char *label_orig, const char *label_deriv, const char *label_deriv2,
662 enum got_diff_algorithm diff_algo, struct got_repository *repo,
663 got_worktree_checkout_cb progress_cb, void *progress_arg)
665 const struct got_error *err = NULL;
667 FILE *f_merged = NULL;
668 char *merged_path = NULL, *base_path = NULL;
672 *local_changes_subsumed = 0;
674 err = got_path_dirname(&parent, ondisk_path);
678 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
679 err = got_error_from_errno("asprintf");
683 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path, "");
687 err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
688 f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
690 if (err->code != GOT_ERR_FILE_BINARY)
692 err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
693 f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
699 err = (*progress_cb)(progress_arg,
700 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
704 if (fsync(merged_fd) != 0) {
705 err = got_error_from_errno("fsync");
709 f_merged = fdopen(merged_fd, "r");
710 if (f_merged == NULL) {
711 err = got_error_from_errno("fdopen");
716 /* Check if a clean merge has subsumed all local changes. */
717 if (overlapcnt == 0) {
718 err = check_files_equal(local_changes_subsumed, f_deriv,
724 if (fchmod(fileno(f_merged), apply_umask(st_mode)) != 0) {
725 err = got_error_from_errno2("fchmod", merged_path);
729 if (rename(merged_path, ondisk_path) != 0) {
730 err = got_error_from_errno3("rename", merged_path,
739 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
740 err = got_error_from_errno("close");
741 if (f_merged && fclose(f_merged) == EOF && err == NULL)
742 err = got_error_from_errno("fclose");
749 static const struct got_error *
750 update_symlink(const char *ondisk_path, const char *target_path,
753 /* This is not atomic but matches what 'ln -sf' does. */
754 if (unlink(ondisk_path) == -1)
755 return got_error_from_errno2("unlink", ondisk_path);
756 if (symlink(target_path, ondisk_path) == -1)
757 return got_error_from_errno3("symlink", target_path,
763 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
764 * in the work tree with a file that contains conflict markers and the
765 * conflicting target paths of the original version, a "derived version"
766 * of a symlink from an incoming change, and a local version of the symlink.
768 * The original versions's target path can be NULL if it is not available,
769 * such as if both derived versions added a new symlink at the same path.
771 * The incoming derived symlink target is NULL in case the incoming change
772 * has deleted this symlink.
774 static const struct got_error *
775 install_symlink_conflict(const char *deriv_target,
776 struct got_object_id *deriv_base_commit_id, const char *orig_target,
777 const char *label_orig, const char *local_target, const char *ondisk_path)
779 const struct got_error *err;
780 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
783 err = got_object_id_str(&id_str, deriv_base_commit_id);
785 return got_error_from_errno("asprintf");
787 if (asprintf(&label_deriv, "%s: commit %s",
788 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
789 err = got_error_from_errno("asprintf");
793 err = got_opentemp_named(&path, &f, "got-symlink-conflict", "");
797 if (fchmod(fileno(f), apply_umask(GOT_DEFAULT_FILE_MODE)) == -1) {
798 err = got_error_from_errno2("fchmod", path);
802 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
803 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
804 deriv_target ? deriv_target : "(symlink was deleted)",
805 orig_target ? label_orig : "",
806 orig_target ? "\n" : "",
807 orig_target ? orig_target : "",
808 orig_target ? "\n" : "",
809 GOT_DIFF_CONFLICT_MARKER_SEP,
810 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
811 err = got_error_from_errno2("fprintf", path);
815 if (unlink(ondisk_path) == -1) {
816 err = got_error_from_errno2("unlink", ondisk_path);
819 if (rename(path, ondisk_path) == -1) {
820 err = got_error_from_errno3("rename", path, ondisk_path);
824 if (f != NULL && fclose(f) == EOF && err == NULL)
825 err = got_error_from_errno2("fclose", path);
832 /* forward declaration */
833 static const struct got_error *
834 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
835 const char *, const char *, uint16_t, const char *,
836 struct got_blob_object *, struct got_object_id *,
837 struct got_repository *, got_worktree_checkout_cb, void *);
840 * Merge a symlink into the work tree, where blob_orig acts as the common
841 * ancestor, deriv_target is the link target of the first derived version,
842 * and the symlink on disk acts as the second derived version.
843 * Assume that contents of both blobs represent symlinks.
845 static const struct got_error *
846 merge_symlink(struct got_worktree *worktree,
847 struct got_blob_object *blob_orig, const char *ondisk_path,
848 const char *path, const char *label_orig, const char *deriv_target,
849 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
850 got_worktree_checkout_cb progress_cb, void *progress_arg)
852 const struct got_error *err = NULL;
853 char *ancestor_target = NULL;
855 ssize_t ondisk_len, deriv_len;
856 char ondisk_target[PATH_MAX];
857 int have_local_change = 0;
858 int have_incoming_change = 0;
860 if (lstat(ondisk_path, &sb) == -1)
861 return got_error_from_errno2("lstat", ondisk_path);
863 ondisk_len = readlink(ondisk_path, ondisk_target,
864 sizeof(ondisk_target));
865 if (ondisk_len == -1) {
866 err = got_error_from_errno2("readlink",
870 ondisk_target[ondisk_len] = '\0';
873 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
878 if (ancestor_target == NULL ||
879 (ondisk_len != strlen(ancestor_target) ||
880 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
881 have_local_change = 1;
883 deriv_len = strlen(deriv_target);
884 if (ancestor_target == NULL ||
885 (deriv_len != strlen(ancestor_target) ||
886 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
887 have_incoming_change = 1;
889 if (!have_local_change && !have_incoming_change) {
890 if (ancestor_target) {
891 /* Both sides made the same change. */
892 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
894 } else if (deriv_len == ondisk_len &&
895 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
896 /* Both sides added the same symlink. */
897 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
900 /* Both sides added symlinks which don't match. */
901 err = install_symlink_conflict(deriv_target,
902 deriv_base_commit_id, ancestor_target,
903 label_orig, ondisk_target, ondisk_path);
906 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
909 } else if (!have_local_change && have_incoming_change) {
910 /* Apply the incoming change. */
911 err = update_symlink(ondisk_path, deriv_target,
912 strlen(deriv_target));
915 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
916 } else if (have_local_change && have_incoming_change) {
917 if (deriv_len == ondisk_len &&
918 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
919 /* Both sides made the same change. */
920 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
923 err = install_symlink_conflict(deriv_target,
924 deriv_base_commit_id, ancestor_target, label_orig,
925 ondisk_target, ondisk_path);
928 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
934 free(ancestor_target);
938 static const struct got_error *
939 dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
941 const struct got_error *err = NULL;
942 char target_path[PATH_MAX];
951 return got_error_from_errno("got_opentemp");
952 target_len = readlink(ondisk_path, target_path, sizeof(target_path));
953 if (target_len == -1) {
954 err = got_error_from_errno2("readlink", ondisk_path);
957 n = fwrite(target_path, 1, target_len, f);
958 if (n != target_len) {
959 err = got_ferror(f, GOT_ERR_IO);
962 if (fflush(f) == EOF) {
963 err = got_error_from_errno("fflush");
966 if (fseek(f, 0L, SEEK_SET) == -1) {
967 err = got_ferror(f, GOT_ERR_IO);
979 * Perform a 3-way merge where blob_orig acts as the common ancestor,
980 * blob_deriv acts as the first derived version, and the file on disk
981 * acts as the second derived version.
983 static const struct got_error *
984 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
985 struct got_blob_object *blob_orig, const char *ondisk_path,
986 const char *path, uint16_t st_mode, const char *label_orig,
987 struct got_blob_object *blob_deriv,
988 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
989 got_worktree_checkout_cb progress_cb, void *progress_arg)
991 const struct got_error *err = NULL;
992 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
993 char *blob_orig_path = NULL;
994 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
995 char *label_deriv = NULL, *parent = NULL;
997 *local_changes_subsumed = 0;
999 err = got_path_dirname(&parent, ondisk_path);
1004 if (asprintf(&base_path, "%s/got-merge-blob-orig",
1006 err = got_error_from_errno("asprintf");
1011 err = got_opentemp_named(&blob_orig_path, &f_orig,
1015 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1022 * No common ancestor exists. This is an "add vs add" conflict
1023 * and we simply use an empty ancestor file to make both files
1024 * appear in the merged result in their entirety.
1026 f_orig = got_opentemp();
1027 if (f_orig == NULL) {
1028 err = got_error_from_errno("got_opentemp");
1033 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1034 err = got_error_from_errno("asprintf");
1039 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path, "");
1042 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1047 err = got_object_id_str(&id_str, deriv_base_commit_id);
1050 if (asprintf(&label_deriv, "%s: commit %s",
1051 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1052 err = got_error_from_errno("asprintf");
1057 * In order the run a 3-way merge with a symlink we copy the symlink's
1058 * target path into a temporary file and use that file with diff3.
1060 if (S_ISLNK(st_mode)) {
1061 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1066 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1068 err = got_error_from_errno2("open", ondisk_path);
1071 f_deriv2 = fdopen(fd, "r");
1072 if (f_deriv2 == NULL) {
1073 err = got_error_from_errno2("fdopen", ondisk_path);
1079 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1080 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1081 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1083 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1084 err = got_error_from_errno("fclose");
1085 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1086 err = got_error_from_errno("fclose");
1087 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1088 err = got_error_from_errno("fclose");
1090 if (blob_orig_path) {
1091 unlink(blob_orig_path);
1092 free(blob_orig_path);
1094 if (blob_deriv_path) {
1095 unlink(blob_deriv_path);
1096 free(blob_deriv_path);
1104 static const struct got_error *
1105 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1106 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1107 int wt_fd, const char *path, struct got_object_id *blob_id)
1109 const struct got_error *err = NULL;
1110 struct got_fileindex_entry *new_ie;
1114 err = got_fileindex_entry_alloc(&new_ie, path);
1118 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1119 blob_id->sha1, base_commit_id->sha1, 1);
1123 err = got_fileindex_entry_add(fileindex, new_ie);
1126 got_fileindex_entry_free(new_ie);
1133 get_ondisk_perms(int executable, mode_t st_mode)
1135 mode_t xbits = S_IXUSR;
1138 /* Map read bits to execute bits. */
1139 if (st_mode & S_IRGRP)
1141 if (st_mode & S_IROTH)
1143 return st_mode | xbits;
1149 /* forward declaration */
1150 static const struct got_error *
1151 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1152 const char *path, mode_t te_mode, mode_t st_mode,
1153 struct got_blob_object *blob, int restoring_missing_file,
1154 int reverting_versioned_file, int installing_bad_symlink,
1155 int path_is_unversioned, struct got_repository *repo,
1156 got_worktree_checkout_cb progress_cb, void *progress_arg);
1159 * This function assumes that the provided symlink target points at a
1160 * safe location in the work tree!
1162 static const struct got_error *
1163 replace_existing_symlink(int *did_something, const char *ondisk_path,
1164 const char *target_path, size_t target_len)
1166 const struct got_error *err = NULL;
1168 char etarget[PATH_MAX];
1174 * "Bad" symlinks (those pointing outside the work tree or into the
1175 * .got directory) are installed in the work tree as a regular file
1176 * which contains the bad symlink target path.
1177 * The new symlink target has already been checked for safety by our
1178 * caller. If we can successfully open a regular file then we simply
1179 * replace this file with a symlink below.
1181 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1183 if (!got_err_open_nofollow_on_symlink())
1184 return got_error_from_errno2("open", ondisk_path);
1186 /* We are updating an existing on-disk symlink. */
1187 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1189 return got_error_from_errno2("readlink", ondisk_path);
1191 if (elen == target_len &&
1192 memcmp(etarget, target_path, target_len) == 0)
1193 return NULL; /* nothing to do */
1197 err = update_symlink(ondisk_path, target_path, target_len);
1198 if (fd != -1 && close(fd) == -1 && err == NULL)
1199 err = got_error_from_errno2("close", ondisk_path);
1203 static const struct got_error *
1204 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1205 size_t target_len, const char *ondisk_path, const char *wtroot_path,
1206 const char *meta_dir)
1208 const struct got_error *err = NULL;
1209 char canonpath[PATH_MAX];
1210 char *path_got = NULL;
1212 *is_bad_symlink = 0;
1214 if (target_len >= sizeof(canonpath)) {
1215 *is_bad_symlink = 1;
1220 * We do not use realpath(3) to resolve the symlink's target
1221 * path because we don't want to resolve symlinks recursively.
1222 * Instead we make the path absolute and then canonicalize it.
1223 * Relative symlink target lookup should begin at the directory
1224 * in which the blob object is being installed.
1226 if (!got_path_is_absolute(target_path)) {
1227 char *abspath, *parent;
1228 err = got_path_dirname(&parent, ondisk_path);
1231 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1233 return got_error_from_errno("asprintf");
1236 if (strlen(abspath) >= sizeof(canonpath)) {
1237 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1241 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1246 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1251 /* Only allow symlinks pointing at paths within the work tree. */
1252 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1253 *is_bad_symlink = 1;
1257 /* Do not allow symlinks pointing into the meta directory. */
1258 if (asprintf(&path_got, "%s/%s", wtroot_path, meta_dir) == -1)
1259 return got_error_from_errno("asprintf");
1260 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1261 *is_bad_symlink = 1;
1267 static const struct got_error *
1268 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1269 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1270 int restoring_missing_file, int reverting_versioned_file,
1271 int path_is_unversioned, int allow_bad_symlinks,
1272 struct got_repository *repo,
1273 got_worktree_checkout_cb progress_cb, void *progress_arg)
1275 const struct got_error *err = NULL;
1276 char target_path[PATH_MAX];
1277 size_t len, target_len = 0;
1278 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1279 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1281 *is_bad_symlink = 0;
1284 * Blob object content specifies the target path of the link.
1285 * If a symbolic link cannot be installed we instead create
1286 * a regular file which contains the link target path stored
1287 * in the blob object.
1290 err = got_object_blob_read_block(&len, blob);
1294 if (len + target_len >= sizeof(target_path)) {
1295 /* Path too long; install as a regular file. */
1296 *is_bad_symlink = 1;
1297 got_object_blob_rewind(blob);
1298 return install_blob(worktree, ondisk_path, path,
1299 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1300 restoring_missing_file, reverting_versioned_file,
1301 1, path_is_unversioned, repo, progress_cb,
1305 /* Skip blob object header first time around. */
1306 memcpy(target_path + target_len, buf + hdrlen,
1308 target_len += len - hdrlen;
1312 target_path[target_len] = '\0';
1314 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1315 ondisk_path, worktree->root_path, worktree->meta_dir);
1319 if (*is_bad_symlink && !allow_bad_symlinks) {
1320 /* install as a regular file */
1321 got_object_blob_rewind(blob);
1322 err = install_blob(worktree, ondisk_path, path,
1323 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1324 restoring_missing_file, reverting_versioned_file, 1,
1325 path_is_unversioned, repo, progress_cb, progress_arg);
1329 if (symlink(target_path, ondisk_path) == -1) {
1330 if (errno == EEXIST) {
1331 int symlink_replaced;
1332 if (path_is_unversioned) {
1333 err = (*progress_cb)(progress_arg,
1334 GOT_STATUS_UNVERSIONED, path);
1337 err = replace_existing_symlink(&symlink_replaced,
1338 ondisk_path, target_path, target_len);
1342 if (symlink_replaced) {
1343 err = (*progress_cb)(progress_arg,
1344 reverting_versioned_file ?
1346 GOT_STATUS_UPDATE, path);
1348 err = (*progress_cb)(progress_arg,
1349 GOT_STATUS_EXISTS, path);
1352 return err; /* Nothing else to do. */
1355 if (errno == ENOENT) {
1357 err = got_path_dirname(&parent, ondisk_path);
1360 err = got_path_mkdir(parent);
1365 * Retry, and fall through to error handling
1366 * below if this second attempt fails.
1368 if (symlink(target_path, ondisk_path) != -1) {
1369 err = NULL; /* success */
1371 err = (*progress_cb)(progress_arg,
1372 reverting_versioned_file ?
1373 GOT_STATUS_REVERT : GOT_STATUS_ADD,
1380 /* Handle errors from first or second creation attempt. */
1381 if (errno == ENAMETOOLONG) {
1382 /* bad target path; install as a regular file */
1383 *is_bad_symlink = 1;
1384 got_object_blob_rewind(blob);
1385 err = install_blob(worktree, ondisk_path, path,
1386 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1387 restoring_missing_file, reverting_versioned_file, 1,
1388 path_is_unversioned, repo,
1389 progress_cb, progress_arg);
1390 } else if (errno == ENOTDIR) {
1391 err = got_error_path(ondisk_path,
1392 GOT_ERR_FILE_OBSTRUCTED);
1394 err = got_error_from_errno3("symlink",
1395 target_path, ondisk_path);
1397 } else if (progress_cb)
1398 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1399 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1403 static const struct got_error *
1404 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1405 const char *path, mode_t te_mode, mode_t st_mode,
1406 struct got_blob_object *blob, int restoring_missing_file,
1407 int reverting_versioned_file, int installing_bad_symlink,
1408 int path_is_unversioned, struct got_repository *repo,
1409 got_worktree_checkout_cb progress_cb, void *progress_arg)
1411 const struct got_error *err = NULL;
1415 char *tmppath = NULL;
1418 mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1419 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1422 if (errno == ENOENT || errno == ENOTDIR) {
1424 err = got_path_dirname(&parent, path);
1427 err = add_dir_on_disk(worktree, parent);
1428 if (err && err->code == GOT_ERR_FILE_OBSTRUCTED)
1429 err = got_error_path(path, err->code);
1433 fd = open(ondisk_path,
1434 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1437 return got_error_from_errno2("open",
1439 } else if (errno == EEXIST) {
1440 if (path_is_unversioned) {
1441 err = (*progress_cb)(progress_arg,
1442 GOT_STATUS_UNVERSIONED, path);
1445 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1446 !S_ISREG(st_mode) && !installing_bad_symlink) {
1447 /* TODO file is obstructed; do something */
1448 err = got_error_path(ondisk_path,
1449 GOT_ERR_FILE_OBSTRUCTED);
1452 err = got_opentemp_named_fd(&tmppath, &fd,
1458 if (fchmod(fd, apply_umask(mode)) == -1) {
1459 err = got_error_from_errno2("fchmod",
1465 return got_error_from_errno2("open", ondisk_path);
1469 if (restoring_missing_file)
1470 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1472 else if (reverting_versioned_file)
1473 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1476 err = (*progress_cb)(progress_arg,
1477 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1482 hdrlen = got_object_blob_get_hdrlen(blob);
1484 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1485 err = got_object_blob_read_block(&len, blob);
1489 /* Skip blob object header first time around. */
1490 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1492 err = got_error_from_errno("write");
1494 } else if (outlen != len - hdrlen) {
1495 err = got_error(GOT_ERR_IO);
1502 if (fsync(fd) != 0) {
1503 err = got_error_from_errno("fsync");
1508 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1509 err = got_error_from_errno2("unlink", ondisk_path);
1512 if (rename(tmppath, ondisk_path) != 0) {
1513 err = got_error_from_errno3("rename", tmppath,
1522 if (fd != -1 && close(fd) == -1 && err == NULL)
1523 err = got_error_from_errno("close");
1524 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1525 err = got_error_from_errno2("unlink", tmppath);
1531 * Upgrade STATUS_MODIFY to STATUS_CONFLICT if a
1532 * conflict marker is found in newly added lines only.
1534 static const struct got_error *
1535 get_modified_file_content_status(unsigned char *status,
1536 struct got_blob_object *blob, const char *path, struct stat *sb,
1539 const struct got_error *err, *free_err;
1540 const char *markers[3] = {
1541 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1542 GOT_DIFF_CONFLICT_MARKER_SEP,
1543 GOT_DIFF_CONFLICT_MARKER_END
1546 struct got_diffreg_result *diffreg_result = NULL;
1547 struct diff_result *r;
1548 int nchunks_parsed, n, i = 0, ln = 0;
1550 size_t linesize = 0;
1553 if (*status != GOT_STATUS_MODIFY)
1556 f1 = got_opentemp();
1558 return got_error_from_errno("got_opentemp");
1561 got_object_blob_rewind(blob);
1562 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
1567 err = got_diff_files(&diffreg_result, f1, 1, NULL, ondisk_file,
1568 1, NULL, 0, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
1572 r = diffreg_result->result;
1574 for (n = 0; n < r->chunks.len; n += nchunks_parsed) {
1575 struct diff_chunk *c;
1576 struct diff_chunk_context cc = {};
1580 * We can optimise a little by advancing straight
1581 * to the next chunk if this one has no added lines.
1583 c = diff_chunk_get(r, n);
1585 if (diff_chunk_type(c) != CHUNK_PLUS) {
1587 continue; /* removed or unchanged lines */
1590 pos = diff_chunk_get_right_start_pos(c);
1591 if (fseek(ondisk_file, pos, SEEK_SET) == -1) {
1592 err = got_ferror(ondisk_file, GOT_ERR_IO);
1596 diff_chunk_context_load_change(&cc, &nchunks_parsed, r, n, 0);
1597 ln = cc.right.start;
1599 while (ln < cc.right.end) {
1600 linelen = getline(&line, &linesize, ondisk_file);
1601 if (linelen == -1) {
1602 if (feof(ondisk_file))
1604 err = got_ferror(ondisk_file, GOT_ERR_IO);
1608 if (line && strncmp(line, markers[i],
1609 strlen(markers[i])) == 0) {
1610 if (strcmp(markers[i],
1611 GOT_DIFF_CONFLICT_MARKER_END) == 0) {
1612 *status = GOT_STATUS_CONFLICT;
1623 if (f1 != NULL && fclose(f1) == EOF && err == NULL)
1624 err = got_error_from_errno("fclose");
1625 free_err = got_diffreg_result_free(diffreg_result);
1633 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1635 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1636 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1640 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1642 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1643 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1644 ie->mtime_sec == sb->st_mtim.tv_sec &&
1645 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1646 ie->size == (sb->st_size & 0xffffffff) &&
1647 !xbit_differs(ie, sb->st_mode));
1650 static unsigned char
1651 get_staged_status(struct got_fileindex_entry *ie)
1653 switch (got_fileindex_entry_stage_get(ie)) {
1654 case GOT_FILEIDX_STAGE_ADD:
1655 return GOT_STATUS_ADD;
1656 case GOT_FILEIDX_STAGE_DELETE:
1657 return GOT_STATUS_DELETE;
1658 case GOT_FILEIDX_STAGE_MODIFY:
1659 return GOT_STATUS_MODIFY;
1661 return GOT_STATUS_NO_CHANGE;
1665 static const struct got_error *
1666 get_symlink_modification_status(unsigned char *status,
1667 struct got_fileindex_entry *ie, const char *abspath,
1668 int dirfd, const char *de_name, struct got_blob_object *blob)
1670 const struct got_error *err = NULL;
1671 char target_path[PATH_MAX];
1672 char etarget[PATH_MAX];
1674 size_t len, target_len = 0;
1675 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1676 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1678 *status = GOT_STATUS_NO_CHANGE;
1680 /* Blob object content specifies the target path of the link. */
1682 err = got_object_blob_read_block(&len, blob);
1685 if (len + target_len >= sizeof(target_path)) {
1687 * Should not happen. The blob contents were OK
1688 * when this symlink was installed.
1690 return got_error(GOT_ERR_NO_SPACE);
1693 /* Skip blob object header first time around. */
1694 memcpy(target_path + target_len, buf + hdrlen,
1696 target_len += len - hdrlen;
1700 target_path[target_len] = '\0';
1703 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1705 return got_error_from_errno2("readlinkat", abspath);
1707 elen = readlink(abspath, etarget, sizeof(etarget));
1709 return got_error_from_errno2("readlink", abspath);
1712 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1713 *status = GOT_STATUS_MODIFY;
1718 static const struct got_error *
1719 get_file_status(unsigned char *status, struct stat *sb,
1720 struct got_fileindex_entry *ie, const char *abspath,
1721 int dirfd, const char *de_name, struct got_repository *repo)
1723 const struct got_error *err = NULL;
1724 struct got_object_id id;
1726 int fd = -1, fd1 = -1;
1729 struct got_blob_object *blob = NULL;
1731 unsigned char staged_status;
1733 staged_status = get_staged_status(ie);
1734 *status = GOT_STATUS_NO_CHANGE;
1735 memset(sb, 0, sizeof(*sb));
1738 * Whenever the caller provides a directory descriptor and a
1739 * directory entry name for the file, use them! This prevents
1740 * race conditions if filesystem paths change beneath our feet.
1743 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1744 if (errno == ENOENT) {
1745 if (got_fileindex_entry_has_file_on_disk(ie))
1746 *status = GOT_STATUS_MISSING;
1748 *status = GOT_STATUS_DELETE;
1751 err = got_error_from_errno2("fstatat", abspath);
1755 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1756 if (fd == -1 && errno != ENOENT &&
1757 !got_err_open_nofollow_on_symlink())
1758 return got_error_from_errno2("open", abspath);
1759 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1760 if (lstat(abspath, sb) == -1)
1761 return got_error_from_errno2("lstat", abspath);
1762 } else if (fd == -1 || fstat(fd, sb) == -1) {
1763 if (errno == ENOENT) {
1764 if (got_fileindex_entry_has_file_on_disk(ie))
1765 *status = GOT_STATUS_MISSING;
1767 *status = GOT_STATUS_DELETE;
1770 err = got_error_from_errno2("fstat", abspath);
1775 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1776 *status = GOT_STATUS_OBSTRUCTED;
1780 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1781 *status = GOT_STATUS_DELETE;
1783 } else if (!got_fileindex_entry_has_blob(ie) &&
1784 staged_status != GOT_STATUS_ADD) {
1785 *status = GOT_STATUS_ADD;
1789 if (!stat_info_differs(ie, sb))
1792 if (S_ISLNK(sb->st_mode) &&
1793 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1794 *status = GOT_STATUS_MODIFY;
1798 if (staged_status == GOT_STATUS_MODIFY ||
1799 staged_status == GOT_STATUS_ADD)
1800 got_fileindex_entry_get_staged_blob_id(&id, ie);
1802 got_fileindex_entry_get_blob_id(&id, ie);
1804 fd1 = got_opentempfd();
1806 err = got_error_from_errno("got_opentempfd");
1809 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1813 if (S_ISLNK(sb->st_mode)) {
1814 err = get_symlink_modification_status(status, ie,
1815 abspath, dirfd, de_name, blob);
1820 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1822 err = got_error_from_errno2("openat", abspath);
1827 f = fdopen(fd, "r");
1829 err = got_error_from_errno2("fdopen", abspath);
1833 hdrlen = got_object_blob_get_hdrlen(blob);
1835 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1836 err = got_object_blob_read_block(&blen, blob);
1839 /* Skip length of blob object header first time around. */
1840 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1841 if (flen == 0 && ferror(f)) {
1842 err = got_error_from_errno("fread");
1845 if (blen - hdrlen == 0) {
1847 *status = GOT_STATUS_MODIFY;
1849 } else if (flen == 0) {
1850 if (blen - hdrlen != 0)
1851 *status = GOT_STATUS_MODIFY;
1853 } else if (blen - hdrlen == flen) {
1854 /* Skip blob object header first time around. */
1855 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1856 *status = GOT_STATUS_MODIFY;
1860 *status = GOT_STATUS_MODIFY;
1866 if (*status == GOT_STATUS_MODIFY) {
1868 err = get_modified_file_content_status(status, blob, ie->path,
1870 } else if (xbit_differs(ie, sb->st_mode))
1871 *status = GOT_STATUS_MODE_CHANGE;
1873 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1874 err = got_error_from_errno("close");
1876 got_object_blob_close(blob);
1877 if (f != NULL && fclose(f) == EOF && err == NULL)
1878 err = got_error_from_errno2("fclose", abspath);
1879 if (fd != -1 && close(fd) == -1 && err == NULL)
1880 err = got_error_from_errno2("close", abspath);
1885 * Update timestamps in the file index if a file is unmodified and
1886 * we had to run a full content comparison to find out.
1888 static const struct got_error *
1889 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1890 struct got_fileindex_entry *ie, struct stat *sb)
1892 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1893 return got_fileindex_entry_update(ie, wt_fd, path,
1894 ie->blob_sha1, ie->commit_sha1, 1);
1899 static const struct got_error *remove_ondisk_file(const char *, const char *);
1901 static const struct got_error *
1902 update_blob(struct got_worktree *worktree,
1903 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1904 struct got_tree_entry *te, const char *path,
1905 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1908 const struct got_error *err = NULL;
1909 struct got_blob_object *blob = NULL;
1910 char *ondisk_path = NULL;
1911 unsigned char status = GOT_STATUS_NO_CHANGE;
1913 int fd1 = -1, fd2 = -1;
1915 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1916 return got_error_from_errno("asprintf");
1919 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1920 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1923 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1927 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1928 sb.st_mode = got_fileindex_perms_to_st(ie);
1930 if (stat(ondisk_path, &sb) == -1) {
1931 if (errno != ENOENT && errno != ENOTDIR) {
1932 err = got_error_from_errno2("stat",
1936 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1937 status = GOT_STATUS_UNVERSIONED;
1939 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1940 status = GOT_STATUS_UNVERSIONED;
1942 status = GOT_STATUS_OBSTRUCTED;
1946 if (status == GOT_STATUS_OBSTRUCTED) {
1948 got_fileindex_entry_mark_skipped(ie);
1949 err = (*progress_cb)(progress_arg, status, path);
1952 if (status == GOT_STATUS_CONFLICT) {
1954 got_fileindex_entry_mark_skipped(ie);
1955 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1960 if (S_ISDIR(te->mode)) { /* file changing into a directory */
1961 if (status == GOT_STATUS_UNVERSIONED) {
1962 err = (*progress_cb)(progress_arg, status, path);
1963 } else if (status != GOT_STATUS_NO_CHANGE &&
1964 status != GOT_STATUS_DELETE &&
1965 status != GOT_STATUS_NONEXISTENT &&
1966 status != GOT_STATUS_MISSING) {
1967 err = (*progress_cb)(progress_arg,
1968 GOT_STATUS_CANNOT_DELETE, path);
1970 if (status != GOT_STATUS_DELETE &&
1971 status != GOT_STATUS_NONEXISTENT &&
1972 status != GOT_STATUS_MISSING) {
1973 err = remove_ondisk_file(worktree->root_path,
1975 if (err && !(err->code == GOT_ERR_ERRNO &&
1979 got_fileindex_entry_remove(fileindex, ie);
1980 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE,
1983 goto done; /* nothing else to do */
1986 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1987 (S_ISLNK(te->mode) ||
1988 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1990 * This is a regular file or an installed bad symlink.
1991 * If the file index indicates that this file is already
1992 * up-to-date with respect to the repository we can skip
1993 * updating contents of this file.
1995 if (got_fileindex_entry_has_commit(ie) &&
1996 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1997 SHA1_DIGEST_LENGTH) == 0) {
1999 err = sync_timestamps(worktree->root_fd,
2000 path, status, ie, &sb);
2003 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2007 if (got_fileindex_entry_has_blob(ie) &&
2008 memcmp(ie->blob_sha1, te->id.sha1,
2009 SHA1_DIGEST_LENGTH) == 0) {
2010 /* Different commit but the same blob. */
2011 if (got_fileindex_entry_has_commit(ie)) {
2012 /* Update the base commit ID of this file. */
2013 memcpy(ie->commit_sha1,
2014 worktree->base_commit_id->sha1,
2015 sizeof(ie->commit_sha1));
2017 err = sync_timestamps(worktree->root_fd,
2018 path, status, ie, &sb);
2021 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2027 fd1 = got_opentempfd();
2029 err = got_error_from_errno("got_opentempfd");
2032 err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
2036 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
2037 int update_timestamps;
2038 struct got_blob_object *blob2 = NULL;
2039 char *label_orig = NULL;
2040 if (got_fileindex_entry_has_blob(ie)) {
2041 fd2 = got_opentempfd();
2043 err = got_error_from_errno("got_opentempfd");
2046 struct got_object_id id2;
2047 got_fileindex_entry_get_blob_id(&id2, ie);
2048 err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
2053 if (got_fileindex_entry_has_commit(ie)) {
2054 char id_str[SHA1_DIGEST_STRING_LENGTH];
2055 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2056 sizeof(id_str)) == NULL) {
2057 err = got_error_path(id_str,
2058 GOT_ERR_BAD_OBJ_ID_STR);
2061 if (asprintf(&label_orig, "%s: commit %s",
2062 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2063 err = got_error_from_errno("asprintf");
2067 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2069 err = got_object_blob_read_to_str(&link_target, blob);
2072 err = merge_symlink(worktree, blob2, ondisk_path, path,
2073 label_orig, link_target, worktree->base_commit_id,
2074 repo, progress_cb, progress_arg);
2077 err = merge_blob(&update_timestamps, worktree, blob2,
2078 ondisk_path, path, sb.st_mode, label_orig, blob,
2079 worktree->base_commit_id, repo,
2080 progress_cb, progress_arg);
2083 if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2084 err = got_error_from_errno("close");
2088 got_object_blob_close(blob2);
2092 * Do not update timestamps of files with local changes.
2093 * Otherwise, a future status walk would treat them as
2094 * unmodified files again.
2096 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2097 blob->id.sha1, worktree->base_commit_id->sha1,
2099 } else if (status == GOT_STATUS_MODE_CHANGE) {
2100 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2101 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2102 } else if (status == GOT_STATUS_DELETE) {
2103 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2106 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2107 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2111 int is_bad_symlink = 0;
2112 if (S_ISLNK(te->mode)) {
2113 err = install_symlink(&is_bad_symlink, worktree,
2114 ondisk_path, path, blob,
2115 status == GOT_STATUS_MISSING, 0,
2116 status == GOT_STATUS_UNVERSIONED, 0,
2117 repo, progress_cb, progress_arg);
2119 err = install_blob(worktree, ondisk_path, path,
2120 te->mode, sb.st_mode, blob,
2121 status == GOT_STATUS_MISSING, 0, 0,
2122 status == GOT_STATUS_UNVERSIONED, repo,
2123 progress_cb, progress_arg);
2129 err = got_fileindex_entry_update(ie,
2130 worktree->root_fd, path, blob->id.sha1,
2131 worktree->base_commit_id->sha1, 1);
2133 err = create_fileindex_entry(&ie, fileindex,
2134 worktree->base_commit_id, worktree->root_fd, path,
2140 if (is_bad_symlink) {
2141 got_fileindex_entry_filetype_set(ie,
2142 GOT_FILEIDX_MODE_BAD_SYMLINK);
2146 if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2147 err = got_error_from_errno("close");
2150 got_object_blob_close(blob);
2156 static const struct got_error *
2157 remove_ondisk_file(const char *root_path, const char *path)
2159 const struct got_error *err = NULL;
2160 char *ondisk_path = NULL, *parent = NULL;
2162 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2163 return got_error_from_errno("asprintf");
2165 if (unlink(ondisk_path) == -1) {
2166 if (errno != ENOENT)
2167 err = got_error_from_errno2("unlink", ondisk_path);
2169 size_t root_len = strlen(root_path);
2170 err = got_path_dirname(&parent, ondisk_path);
2173 while (got_path_cmp(parent, root_path,
2174 strlen(parent), root_len) != 0) {
2176 ondisk_path = parent;
2178 if (rmdir(ondisk_path) == -1) {
2179 if (errno != ENOTEMPTY)
2180 err = got_error_from_errno2("rmdir",
2184 err = got_path_dirname(&parent, ondisk_path);
2195 static const struct got_error *
2196 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2197 struct got_fileindex_entry *ie, struct got_repository *repo,
2198 got_worktree_checkout_cb progress_cb, void *progress_arg)
2200 const struct got_error *err = NULL;
2201 unsigned char status;
2205 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2206 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2208 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2210 return got_error_from_errno("asprintf");
2212 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2216 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2217 char ondisk_target[PATH_MAX];
2218 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2219 sizeof(ondisk_target));
2220 if (ondisk_len == -1) {
2221 err = got_error_from_errno2("readlink", ondisk_path);
2224 ondisk_target[ondisk_len] = '\0';
2225 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2226 NULL, NULL, /* XXX pass common ancestor info? */
2227 ondisk_target, ondisk_path);
2230 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2235 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2236 status == GOT_STATUS_ADD) {
2237 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2241 * Preserve the working file and change the deleted blob's
2242 * entry into a schedule-add entry.
2244 err = got_fileindex_entry_update(ie, worktree->root_fd,
2245 ie->path, NULL, NULL, 0);
2247 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2250 if (status == GOT_STATUS_NO_CHANGE) {
2251 err = remove_ondisk_file(worktree->root_path, ie->path);
2255 got_fileindex_entry_remove(fileindex, ie);
2262 struct diff_cb_arg {
2263 struct got_fileindex *fileindex;
2264 struct got_worktree *worktree;
2265 struct got_repository *repo;
2266 got_worktree_checkout_cb progress_cb;
2268 got_cancel_cb cancel_cb;
2272 static const struct got_error *
2273 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2274 struct got_tree_entry *te, const char *parent_path)
2276 struct diff_cb_arg *a = arg;
2278 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2279 return got_error(GOT_ERR_CANCELLED);
2281 return update_blob(a->worktree, a->fileindex, ie, te,
2282 ie->path, a->repo, a->progress_cb, a->progress_arg);
2285 static const struct got_error *
2286 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2288 struct diff_cb_arg *a = arg;
2290 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2291 return got_error(GOT_ERR_CANCELLED);
2293 return delete_blob(a->worktree, a->fileindex, ie,
2294 a->repo, a->progress_cb, a->progress_arg);
2297 static const struct got_error *
2298 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2300 struct diff_cb_arg *a = arg;
2301 const struct got_error *err;
2304 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2305 return got_error(GOT_ERR_CANCELLED);
2307 if (got_object_tree_entry_is_submodule(te))
2310 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2313 if (asprintf(&path, "%s%s%s", parent_path,
2314 parent_path[0] ? "/" : "", te->name)
2316 return got_error_from_errno("asprintf");
2318 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2319 a->repo, a->progress_cb, a->progress_arg);
2325 const struct got_error *
2326 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2328 uint32_t uuid_status;
2330 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2331 if (uuid_status != uuid_s_ok) {
2333 return got_error_uuid(uuid_status, "uuid_to_string");
2339 static const struct got_error *
2340 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2342 const struct got_error *err = NULL;
2343 char *uuidstr = NULL;
2347 err = got_worktree_get_uuid(&uuidstr, worktree);
2351 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2352 err = got_error_from_errno("asprintf");
2359 const struct got_error *
2360 got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2363 return get_ref_name(refname, worktree, prefix);
2366 static const struct got_error *
2367 get_base_ref_name(char **refname, struct got_worktree *worktree)
2369 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2372 static const struct got_error *
2373 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2375 return get_ref_name(refname, worktree,
2376 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2379 static const struct got_error *
2380 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2382 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2385 static const struct got_error *
2386 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2388 return get_ref_name(refname, worktree,
2389 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2392 static const struct got_error *
2393 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2395 return get_ref_name(refname, worktree,
2396 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2399 static const struct got_error *
2400 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2402 return get_ref_name(refname, worktree,
2403 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2406 static const struct got_error *
2407 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2409 return get_ref_name(refname, worktree,
2410 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2413 static const struct got_error *
2414 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2416 return get_ref_name(refname, worktree,
2417 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2420 static const struct got_error *
2421 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2423 return get_ref_name(refname, worktree,
2424 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2427 const struct got_error *
2428 got_worktree_get_histedit_script_path(char **path,
2429 struct got_worktree *worktree)
2431 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2432 worktree->meta_dir, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2434 return got_error_from_errno("asprintf");
2439 static const struct got_error *
2440 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2442 return get_ref_name(refname, worktree,
2443 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2446 static const struct got_error *
2447 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2449 return get_ref_name(refname, worktree,
2450 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2454 * Prevent Git's garbage collector from deleting our base commit by
2455 * setting a reference to our base commit's ID.
2457 static const struct got_error *
2458 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2460 const struct got_error *err = NULL;
2461 struct got_reference *ref = NULL;
2464 err = get_base_ref_name(&refname, worktree);
2468 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2472 err = got_ref_write(ref, repo);
2480 static const struct got_error *
2481 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2483 const struct got_error *err = NULL;
2485 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2486 worktree->meta_dir, GOT_WORKTREE_FILE_INDEX) == -1) {
2487 err = got_error_from_errno("asprintf");
2488 *fileindex_path = NULL;
2494 static const struct got_error *
2495 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2496 struct got_worktree *worktree)
2498 const struct got_error *err = NULL;
2501 *fileindex_path = NULL;
2502 *fileindex = got_fileindex_alloc();
2503 if (*fileindex == NULL)
2504 return got_error_from_errno("got_fileindex_alloc");
2506 err = get_fileindex_path(fileindex_path, worktree);
2510 index = fopen(*fileindex_path, "rbe");
2511 if (index == NULL) {
2512 if (errno != ENOENT)
2513 err = got_error_from_errno2("fopen", *fileindex_path);
2515 err = got_fileindex_read(*fileindex, index);
2516 if (fclose(index) == EOF && err == NULL)
2517 err = got_error_from_errno("fclose");
2521 free(*fileindex_path);
2522 *fileindex_path = NULL;
2523 got_fileindex_free(*fileindex);
2529 struct bump_base_commit_id_arg {
2530 struct got_object_id *base_commit_id;
2533 const char *entry_name;
2534 got_worktree_checkout_cb progress_cb;
2538 static const struct got_error *
2539 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2541 const struct got_error *err;
2542 struct bump_base_commit_id_arg *a = arg;
2544 if (a->entry_name) {
2545 if (strcmp(ie->path, a->path) != 0)
2547 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2550 if (got_fileindex_entry_was_skipped(ie))
2553 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2554 SHA1_DIGEST_LENGTH) == 0)
2557 if (a->progress_cb) {
2558 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2563 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2567 /* Bump base commit ID of all files within an updated part of the work tree. */
2568 static const struct got_error *
2569 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2570 struct got_fileindex *fileindex,
2571 got_worktree_checkout_cb progress_cb, void *progress_arg)
2573 struct bump_base_commit_id_arg bbc_arg;
2575 bbc_arg.base_commit_id = worktree->base_commit_id;
2576 bbc_arg.entry_name = NULL;
2578 bbc_arg.path_len = 0;
2579 bbc_arg.progress_cb = progress_cb;
2580 bbc_arg.progress_arg = progress_arg;
2582 return got_fileindex_for_each_entry_safe(fileindex,
2583 bump_base_commit_id, &bbc_arg);
2586 static const struct got_error *
2587 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2589 const struct got_error *err = NULL;
2590 char *new_fileindex_path = NULL;
2591 FILE *new_index = NULL;
2592 struct timespec timeout;
2594 err = got_opentemp_named(&new_fileindex_path, &new_index,
2595 fileindex_path, "");
2599 err = got_fileindex_write(fileindex, new_index);
2603 if (rename(new_fileindex_path, fileindex_path) != 0) {
2604 err = got_error_from_errno3("rename", new_fileindex_path,
2606 unlink(new_fileindex_path);
2610 * Sleep for a short amount of time to ensure that files modified after
2611 * this program exits have a different time stamp from the one which
2612 * was recorded in the file index.
2615 timeout.tv_nsec = 1;
2616 nanosleep(&timeout, NULL);
2620 free(new_fileindex_path);
2624 static const struct got_error *
2625 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2626 struct got_object_id **tree_id, const char *wt_relpath,
2627 struct got_commit_object *base_commit, struct got_worktree *worktree,
2628 struct got_repository *repo)
2630 const struct got_error *err = NULL;
2631 struct got_object_id *id = NULL;
2632 char *in_repo_path = NULL;
2633 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2635 *entry_type = GOT_OBJ_TYPE_ANY;
2636 *tree_relpath = NULL;
2639 if (wt_relpath[0] == '\0') {
2640 /* Check out all files within the work tree. */
2641 *entry_type = GOT_OBJ_TYPE_TREE;
2642 *tree_relpath = strdup("");
2643 if (*tree_relpath == NULL) {
2644 err = got_error_from_errno("strdup");
2647 err = got_object_id_by_path(tree_id, repo, base_commit,
2648 worktree->path_prefix);
2654 /* Check out a subset of files in the work tree. */
2656 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2657 is_root_wt ? "" : "/", wt_relpath) == -1) {
2658 err = got_error_from_errno("asprintf");
2662 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2667 in_repo_path = NULL;
2669 err = got_object_get_type(entry_type, repo, id);
2673 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2674 /* Check out a single file. */
2675 if (strchr(wt_relpath, '/') == NULL) {
2676 /* Check out a single file in work tree's root dir. */
2677 in_repo_path = strdup(worktree->path_prefix);
2678 if (in_repo_path == NULL) {
2679 err = got_error_from_errno("strdup");
2682 *tree_relpath = strdup("");
2683 if (*tree_relpath == NULL) {
2684 err = got_error_from_errno("strdup");
2688 /* Check out a single file in a subdirectory. */
2689 err = got_path_dirname(tree_relpath, wt_relpath);
2692 if (asprintf(&in_repo_path, "%s%s%s",
2693 worktree->path_prefix, is_root_wt ? "" : "/",
2694 *tree_relpath) == -1) {
2695 err = got_error_from_errno("asprintf");
2699 err = got_object_id_by_path(tree_id, repo,
2700 base_commit, in_repo_path);
2702 /* Check out all files within a subdirectory. */
2703 *tree_id = got_object_id_dup(id);
2704 if (*tree_id == NULL) {
2705 err = got_error_from_errno("got_object_id_dup");
2708 *tree_relpath = strdup(wt_relpath);
2709 if (*tree_relpath == NULL) {
2710 err = got_error_from_errno("strdup");
2718 *entry_type = GOT_OBJ_TYPE_ANY;
2719 free(*tree_relpath);
2720 *tree_relpath = NULL;
2727 static const struct got_error *
2728 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2729 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2730 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2731 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2733 const struct got_error *err = NULL;
2734 struct got_commit_object *commit = NULL;
2735 struct got_tree_object *tree = NULL;
2736 struct got_fileindex_diff_tree_cb diff_cb;
2737 struct diff_cb_arg arg;
2739 err = ref_base_commit(worktree, repo);
2741 if (!(err->code == GOT_ERR_ERRNO &&
2742 (errno == EACCES || errno == EROFS)))
2744 err = (*progress_cb)(progress_arg,
2745 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2750 err = got_object_open_as_commit(&commit, repo,
2751 worktree->base_commit_id);
2755 err = got_object_open_as_tree(&tree, repo, tree_id);
2760 got_object_tree_find_entry(tree, entry_name) == NULL) {
2761 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2765 diff_cb.diff_old_new = diff_old_new;
2766 diff_cb.diff_old = diff_old;
2767 diff_cb.diff_new = diff_new;
2768 arg.fileindex = fileindex;
2769 arg.worktree = worktree;
2771 arg.progress_cb = progress_cb;
2772 arg.progress_arg = progress_arg;
2773 arg.cancel_cb = cancel_cb;
2774 arg.cancel_arg = cancel_arg;
2775 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2776 entry_name, repo, &diff_cb, &arg);
2779 got_object_tree_close(tree);
2781 got_object_commit_close(commit);
2785 const struct got_error *
2786 got_worktree_checkout_files(struct got_worktree *worktree,
2787 struct got_pathlist_head *paths, struct got_repository *repo,
2788 got_worktree_checkout_cb progress_cb, void *progress_arg,
2789 got_cancel_cb cancel_cb, void *cancel_arg)
2791 const struct got_error *err = NULL, *sync_err, *unlockerr;
2792 struct got_commit_object *commit = NULL;
2793 struct got_tree_object *tree = NULL;
2794 struct got_fileindex *fileindex = NULL;
2795 char *fileindex_path = NULL;
2796 struct got_pathlist_entry *pe;
2797 struct tree_path_data {
2798 STAILQ_ENTRY(tree_path_data) entry;
2799 struct got_object_id *tree_id;
2804 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2806 STAILQ_INIT(&tree_paths);
2808 err = lock_worktree(worktree, LOCK_EX);
2812 err = got_object_open_as_commit(&commit, repo,
2813 worktree->base_commit_id);
2817 /* Map all specified paths to in-repository trees. */
2818 TAILQ_FOREACH(pe, paths, entry) {
2819 tpd = malloc(sizeof(*tpd));
2821 err = got_error_from_errno("malloc");
2825 err = find_tree_entry_for_checkout(&tpd->entry_type,
2826 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2833 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2834 err = got_path_basename(&tpd->entry_name, pe->path);
2842 tpd->entry_name = NULL;
2844 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2848 * Read the file index.
2849 * Checking out files is supposed to be an idempotent operation.
2850 * If the on-disk file index is incomplete we will try to complete it.
2852 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2856 tpd = STAILQ_FIRST(&tree_paths);
2857 TAILQ_FOREACH(pe, paths, entry) {
2858 struct bump_base_commit_id_arg bbc_arg;
2860 err = checkout_files(worktree, fileindex, tpd->relpath,
2861 tpd->tree_id, tpd->entry_name, repo,
2862 progress_cb, progress_arg, cancel_cb, cancel_arg);
2866 bbc_arg.base_commit_id = worktree->base_commit_id;
2867 bbc_arg.entry_name = tpd->entry_name;
2868 bbc_arg.path = pe->path;
2869 bbc_arg.path_len = pe->path_len;
2870 bbc_arg.progress_cb = progress_cb;
2871 bbc_arg.progress_arg = progress_arg;
2872 err = got_fileindex_for_each_entry_safe(fileindex,
2873 bump_base_commit_id, &bbc_arg);
2877 tpd = STAILQ_NEXT(tpd, entry);
2879 sync_err = sync_fileindex(fileindex, fileindex_path);
2880 if (sync_err && err == NULL)
2883 free(fileindex_path);
2885 got_object_tree_close(tree);
2887 got_object_commit_close(commit);
2889 got_fileindex_free(fileindex);
2890 while (!STAILQ_EMPTY(&tree_paths)) {
2891 tpd = STAILQ_FIRST(&tree_paths);
2892 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2897 unlockerr = lock_worktree(worktree, LOCK_SH);
2898 if (unlockerr && err == NULL)
2903 static const struct got_error *
2904 add_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2905 struct got_fileindex_entry *ie, const char *ondisk_path,
2906 const char *path2, struct got_blob_object *blob2, mode_t mode2,
2907 int restoring_missing_file, int reverting_versioned_file,
2908 int path_is_unversioned, int allow_bad_symlinks,
2909 struct got_repository *repo,
2910 got_worktree_checkout_cb progress_cb, void *progress_arg)
2912 const struct got_error *err = NULL;
2913 int is_bad_symlink = 0;
2915 if (S_ISLNK(mode2)) {
2916 err = install_symlink(&is_bad_symlink,
2917 worktree, ondisk_path, path2, blob2,
2918 restoring_missing_file,
2919 reverting_versioned_file,
2920 path_is_unversioned, allow_bad_symlinks,
2921 repo, progress_cb, progress_arg);
2923 err = install_blob(worktree, ondisk_path, path2,
2924 mode2, GOT_DEFAULT_FILE_MODE, blob2,
2925 restoring_missing_file, reverting_versioned_file, 0,
2926 path_is_unversioned, repo, progress_cb, progress_arg);
2931 /* Adding an unversioned file. */
2932 err = got_fileindex_entry_alloc(&ie, path2);
2935 err = got_fileindex_entry_update(ie,
2936 worktree->root_fd, path2, NULL, NULL, 1);
2938 got_fileindex_entry_free(ie);
2941 err = got_fileindex_entry_add(fileindex, ie);
2943 got_fileindex_entry_free(ie);
2947 /* Re-adding a locally deleted file. */
2948 err = got_fileindex_entry_update(ie,
2949 worktree->root_fd, path2, ie->blob_sha1,
2950 worktree->base_commit_id->sha1, 0);
2955 if (is_bad_symlink) {
2956 got_fileindex_entry_filetype_set(ie,
2957 GOT_FILEIDX_MODE_BAD_SYMLINK);
2963 struct merge_file_cb_arg {
2964 struct got_worktree *worktree;
2965 struct got_fileindex *fileindex;
2966 got_worktree_checkout_cb progress_cb;
2968 got_cancel_cb cancel_cb;
2970 const char *label_orig;
2971 struct got_object_id *commit_id2;
2972 int allow_bad_symlinks;
2975 static const struct got_error *
2976 merge_file_cb(void *arg, struct got_blob_object *blob1,
2977 struct got_blob_object *blob2, FILE *f1, FILE *f2,
2978 struct got_object_id *id1, struct got_object_id *id2,
2979 const char *path1, const char *path2,
2980 mode_t mode1, mode_t mode2, struct got_repository *repo)
2982 static const struct got_error *err = NULL;
2983 struct merge_file_cb_arg *a = arg;
2984 struct got_fileindex_entry *ie;
2985 char *ondisk_path = NULL;
2987 unsigned char status;
2988 int local_changes_subsumed;
2989 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2990 char *id_str = NULL, *label_deriv2 = NULL;
2992 if (blob1 && blob2) {
2993 ie = got_fileindex_entry_get(a->fileindex, path2,
2996 return (*a->progress_cb)(a->progress_arg,
2997 GOT_STATUS_MISSING, path2);
2999 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3001 return got_error_from_errno("asprintf");
3003 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3008 if (status == GOT_STATUS_DELETE) {
3009 err = (*a->progress_cb)(a->progress_arg,
3010 GOT_STATUS_MERGE, path2);
3013 if (status != GOT_STATUS_NO_CHANGE &&
3014 status != GOT_STATUS_MODIFY &&
3015 status != GOT_STATUS_CONFLICT &&
3016 status != GOT_STATUS_ADD) {
3017 err = (*a->progress_cb)(a->progress_arg, status, path2);
3021 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
3023 err = got_object_blob_read_to_str(&link_target2, blob2);
3026 err = merge_symlink(a->worktree, blob1, ondisk_path,
3027 path2, a->label_orig, link_target2, a->commit_id2,
3028 repo, a->progress_cb, a->progress_arg);
3033 f_orig = got_opentemp();
3034 if (f_orig == NULL) {
3035 err = got_error_from_errno("got_opentemp");
3038 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3043 f_deriv2 = got_opentemp();
3044 if (f_deriv2 == NULL)
3046 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3051 fd = open(ondisk_path,
3052 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3054 err = got_error_from_errno2("open",
3058 f_deriv = fdopen(fd, "r");
3059 if (f_deriv == NULL) {
3060 err = got_error_from_errno2("fdopen",
3065 err = got_object_id_str(&id_str, a->commit_id2);
3068 if (asprintf(&label_deriv2, "%s: commit %s",
3069 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
3070 err = got_error_from_errno("asprintf");
3073 err = merge_file(&local_changes_subsumed, a->worktree,
3074 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
3075 mode2, a->label_orig, NULL, label_deriv2,
3076 GOT_DIFF_ALGORITHM_PATIENCE, repo,
3077 a->progress_cb, a->progress_arg);
3080 ie = got_fileindex_entry_get(a->fileindex, path1,
3083 return (*a->progress_cb)(a->progress_arg,
3084 GOT_STATUS_MISSING, path1);
3086 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3088 return got_error_from_errno("asprintf");
3090 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3096 case GOT_STATUS_NO_CHANGE:
3097 err = (*a->progress_cb)(a->progress_arg,
3098 GOT_STATUS_DELETE, path1);
3101 err = remove_ondisk_file(a->worktree->root_path, path1);
3105 got_fileindex_entry_mark_deleted_from_disk(ie);
3107 case GOT_STATUS_DELETE:
3108 case GOT_STATUS_MISSING:
3109 err = (*a->progress_cb)(a->progress_arg,
3110 GOT_STATUS_DELETE, path1);
3114 got_fileindex_entry_mark_deleted_from_disk(ie);
3116 case GOT_STATUS_ADD: {
3117 struct got_object_id *id;
3121 * Delete the added file only if its content already
3122 * exists in the repository.
3124 err = got_object_blob_file_create(&id, &blob1_f,
3125 &blob1_size, path1);
3128 if (got_object_id_cmp(id, id1) == 0) {
3129 err = (*a->progress_cb)(a->progress_arg,
3130 GOT_STATUS_DELETE, path1);
3133 err = remove_ondisk_file(a->worktree->root_path,
3138 got_fileindex_entry_remove(a->fileindex,
3141 err = (*a->progress_cb)(a->progress_arg,
3142 GOT_STATUS_CANNOT_DELETE, path1);
3144 if (fclose(blob1_f) == EOF && err == NULL)
3145 err = got_error_from_errno("fclose");
3151 case GOT_STATUS_MODIFY:
3152 case GOT_STATUS_CONFLICT:
3153 err = (*a->progress_cb)(a->progress_arg,
3154 GOT_STATUS_CANNOT_DELETE, path1);
3158 case GOT_STATUS_OBSTRUCTED:
3159 err = (*a->progress_cb)(a->progress_arg, status, path1);
3167 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3169 return got_error_from_errno("asprintf");
3170 ie = got_fileindex_entry_get(a->fileindex, path2,
3173 err = get_file_status(&status, &sb, ie, ondisk_path,
3177 if (status != GOT_STATUS_NO_CHANGE &&
3178 status != GOT_STATUS_MODIFY &&
3179 status != GOT_STATUS_CONFLICT &&
3180 status != GOT_STATUS_ADD &&
3181 status != GOT_STATUS_DELETE) {
3182 err = (*a->progress_cb)(a->progress_arg,
3186 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3188 err = got_object_blob_read_to_str(&link_target2,
3192 err = merge_symlink(a->worktree, NULL,
3193 ondisk_path, path2, a->label_orig,
3194 link_target2, a->commit_id2, repo,
3195 a->progress_cb, a->progress_arg);
3197 } else if (S_ISREG(sb.st_mode)) {
3198 err = merge_blob(&local_changes_subsumed,
3199 a->worktree, NULL, ondisk_path, path2,
3200 sb.st_mode, a->label_orig, blob2,
3201 a->commit_id2, repo, a->progress_cb,
3203 } else if (status != GOT_STATUS_DELETE) {
3204 err = got_error_path(ondisk_path,
3205 GOT_ERR_FILE_OBSTRUCTED);
3209 if (status == GOT_STATUS_DELETE) {
3210 /* Re-add file with content from new blob. */
3211 err = add_file(a->worktree, a->fileindex, ie,
3212 ondisk_path, path2, blob2, mode2,
3213 0, 0, 0, a->allow_bad_symlinks,
3214 repo, a->progress_cb, a->progress_arg);
3219 err = add_file(a->worktree, a->fileindex, NULL,
3220 ondisk_path, path2, blob2, mode2,
3221 0, 0, 1, a->allow_bad_symlinks,
3222 repo, a->progress_cb, a->progress_arg);
3228 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3229 err = got_error_from_errno("fclose");
3230 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3231 err = got_error_from_errno("fclose");
3232 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3233 err = got_error_from_errno("fclose");
3240 static const struct got_error *
3241 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3243 struct got_worktree *worktree = arg;
3245 /* Reject merges into a work tree with mixed base commits. */
3246 if (got_fileindex_entry_has_commit(ie) &&
3247 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3248 SHA1_DIGEST_LENGTH) != 0)
3249 return got_error(GOT_ERR_MIXED_COMMITS);
3254 const struct got_error *
3255 got_worktree_get_state(char *state, struct got_repository *repo,
3256 struct got_worktree *worktree)
3258 const struct got_error *err;
3259 struct got_object_id *base_id, *head_id = NULL;
3260 struct got_reference *head_ref;
3261 struct got_fileindex *fileindex = NULL;
3262 char *fileindex_path = NULL;
3264 if (worktree == NULL)
3265 return got_error(GOT_ERR_NOT_WORKTREE);
3267 err = got_ref_open(&head_ref, repo,
3268 got_worktree_get_head_ref_name(worktree), 0);
3272 err = got_ref_resolve(&head_id, repo, head_ref);
3276 *state = GOT_WORKTREE_STATE_UNKNOWN;
3277 base_id = got_worktree_get_base_commit_id(worktree);
3279 if (got_object_id_cmp(base_id, head_id) == 0) {
3280 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3284 err = got_fileindex_for_each_entry_safe(fileindex,
3285 check_mixed_commits, worktree);
3287 *state = GOT_WORKTREE_STATE_UPTODATE;
3288 else if (err->code == GOT_ERR_MIXED_COMMITS) {
3289 *state = GOT_WORKTREE_STATE_OUTOFDATE;
3293 *state = GOT_WORKTREE_STATE_OUTOFDATE;
3297 free(fileindex_path);
3298 got_ref_close(head_ref);
3299 if (fileindex != NULL)
3300 got_fileindex_free(fileindex);
3304 struct check_merge_conflicts_arg {
3305 struct got_worktree *worktree;
3306 struct got_fileindex *fileindex;
3307 struct got_repository *repo;
3310 static const struct got_error *
3311 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3312 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3313 struct got_object_id *id1, struct got_object_id *id2,
3314 const char *path1, const char *path2,
3315 mode_t mode1, mode_t mode2, struct got_repository *repo)
3317 const struct got_error *err = NULL;
3318 struct check_merge_conflicts_arg *a = arg;
3319 unsigned char status;
3321 struct got_fileindex_entry *ie;
3322 const char *path = path2 ? path2 : path1;
3323 struct got_object_id *id = id2 ? id2 : id1;
3329 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3333 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3335 return got_error_from_errno("asprintf");
3337 /* Reject merges into a work tree with conflicted files. */
3338 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3342 if (status == GOT_STATUS_CONFLICT)
3343 return got_error(GOT_ERR_CONFLICTS);
3348 static const struct got_error *
3349 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3350 const char *fileindex_path, struct got_object_id *commit_id1,
3351 struct got_object_id *commit_id2, struct got_repository *repo,
3352 got_worktree_checkout_cb progress_cb, void *progress_arg,
3353 got_cancel_cb cancel_cb, void *cancel_arg)
3355 const struct got_error *err = NULL, *sync_err;
3356 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3357 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3358 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3359 struct check_merge_conflicts_arg cmc_arg;
3360 struct merge_file_cb_arg arg;
3361 char *label_orig = NULL;
3362 FILE *f1 = NULL, *f2 = NULL;
3363 int fd1 = -1, fd2 = -1;
3366 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3369 err = got_object_id_by_path(&tree_id1, repo, commit1,
3370 worktree->path_prefix);
3371 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3377 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3381 err = got_object_id_str(&id_str, commit_id1);
3385 if (asprintf(&label_orig, "%s: commit %s",
3386 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3387 err = got_error_from_errno("asprintf");
3393 f1 = got_opentemp();
3395 err = got_error_from_errno("got_opentemp");
3400 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3404 err = got_object_id_by_path(&tree_id2, repo, commit2,
3405 worktree->path_prefix);
3409 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3413 f2 = got_opentemp();
3415 err = got_error_from_errno("got_opentemp");
3419 fd1 = got_opentempfd();
3421 err = got_error_from_errno("got_opentempfd");
3425 fd2 = got_opentempfd();
3427 err = got_error_from_errno("got_opentempfd");
3431 cmc_arg.worktree = worktree;
3432 cmc_arg.fileindex = fileindex;
3433 cmc_arg.repo = repo;
3434 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3435 check_merge_conflicts, &cmc_arg, 0);
3439 arg.worktree = worktree;
3440 arg.fileindex = fileindex;
3441 arg.progress_cb = progress_cb;
3442 arg.progress_arg = progress_arg;
3443 arg.cancel_cb = cancel_cb;
3444 arg.cancel_arg = cancel_arg;
3445 arg.label_orig = label_orig;
3446 arg.commit_id2 = commit_id2;
3447 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3448 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3449 merge_file_cb, &arg, 1);
3450 sync_err = sync_fileindex(fileindex, fileindex_path);
3451 if (sync_err && err == NULL)
3455 got_object_commit_close(commit1);
3457 got_object_commit_close(commit2);
3459 got_object_tree_close(tree1);
3461 got_object_tree_close(tree2);
3462 if (f1 && fclose(f1) == EOF && err == NULL)
3463 err = got_error_from_errno("fclose");
3464 if (f2 && fclose(f2) == EOF && err == NULL)
3465 err = got_error_from_errno("fclose");
3466 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3467 err = got_error_from_errno("close");
3468 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3469 err = got_error_from_errno("close");
3474 const struct got_error *
3475 got_worktree_merge_files(struct got_worktree *worktree,
3476 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3477 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3478 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3480 const struct got_error *err, *unlockerr;
3481 char *fileindex_path = NULL;
3482 struct got_fileindex *fileindex = NULL;
3484 err = lock_worktree(worktree, LOCK_EX);
3488 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3492 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3497 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3498 commit_id2, repo, progress_cb, progress_arg,
3499 cancel_cb, cancel_arg);
3502 got_fileindex_free(fileindex);
3503 free(fileindex_path);
3504 unlockerr = lock_worktree(worktree, LOCK_SH);
3505 if (unlockerr && err == NULL)
3510 struct diff_dir_cb_arg {
3511 struct got_fileindex *fileindex;
3512 struct got_worktree *worktree;
3513 const char *status_path;
3514 size_t status_path_len;
3515 struct got_repository *repo;
3516 got_worktree_status_cb status_cb;
3518 got_cancel_cb cancel_cb;
3520 /* A pathlist containing per-directory pathlists of ignore patterns. */
3521 struct got_pathlist_head *ignores;
3522 int report_unchanged;
3526 static const struct got_error *
3527 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3528 int dirfd, const char *de_name,
3529 got_worktree_status_cb status_cb, void *status_arg,
3530 struct got_repository *repo, int report_unchanged)
3532 const struct got_error *err = NULL;
3533 unsigned char status = GOT_STATUS_NO_CHANGE;
3534 unsigned char staged_status;
3536 struct got_object_id blob_id, commit_id, staged_blob_id;
3537 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3538 struct got_object_id *staged_blob_idp = NULL;
3540 staged_status = get_staged_status(ie);
3541 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3545 if (status == GOT_STATUS_NO_CHANGE &&
3546 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3549 if (got_fileindex_entry_has_blob(ie))
3550 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3551 if (got_fileindex_entry_has_commit(ie))
3552 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3553 if (staged_status == GOT_STATUS_ADD ||
3554 staged_status == GOT_STATUS_MODIFY) {
3555 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3556 &staged_blob_id, ie);
3559 return (*status_cb)(status_arg, status, staged_status,
3560 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3563 static const struct got_error *
3564 status_old_new(void *arg, struct got_fileindex_entry *ie,
3565 struct dirent *de, const char *parent_path, int dirfd)
3567 const struct got_error *err = NULL;
3568 struct diff_dir_cb_arg *a = arg;
3571 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3572 return got_error(GOT_ERR_CANCELLED);
3574 if (got_path_cmp(parent_path, a->status_path,
3575 strlen(parent_path), a->status_path_len) != 0 &&
3576 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3579 if (parent_path[0]) {
3580 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3581 parent_path, de->d_name) == -1)
3582 return got_error_from_errno("asprintf");
3584 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3586 return got_error_from_errno("asprintf");
3589 err = report_file_status(ie, abspath, dirfd, de->d_name,
3590 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3595 static const struct got_error *
3596 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3598 struct diff_dir_cb_arg *a = arg;
3599 struct got_object_id blob_id, commit_id;
3600 unsigned char status;
3602 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3603 return got_error(GOT_ERR_CANCELLED);
3605 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3608 got_fileindex_entry_get_blob_id(&blob_id, ie);
3609 got_fileindex_entry_get_commit_id(&commit_id, ie);
3610 if (got_fileindex_entry_has_file_on_disk(ie))
3611 status = GOT_STATUS_MISSING;
3613 status = GOT_STATUS_DELETE;
3614 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3615 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3619 free_ignores(struct got_pathlist_head *ignores)
3621 struct got_pathlist_entry *pe;
3623 TAILQ_FOREACH(pe, ignores, entry) {
3624 struct got_pathlist_head *ignorelist = pe->data;
3626 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3628 got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3631 static const struct got_error *
3632 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3634 const struct got_error *err = NULL;
3635 struct got_pathlist_entry *pe = NULL;
3636 struct got_pathlist_head *ignorelist;
3637 char *line = NULL, *pattern, *dirpath = NULL;
3638 size_t linesize = 0;
3641 ignorelist = calloc(1, sizeof(*ignorelist));
3642 if (ignorelist == NULL)
3643 return got_error_from_errno("calloc");
3644 TAILQ_INIT(ignorelist);
3646 while ((linelen = getline(&line, &linesize, f)) != -1) {
3647 if (linelen > 0 && line[linelen - 1] == '\n')
3648 line[linelen - 1] = '\0';
3650 /* Git's ignores may contain comments. */
3654 /* Git's negated patterns are not (yet?) supported. */
3658 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3660 err = got_error_from_errno("asprintf");
3663 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3668 err = got_error_from_errno("getline");
3672 dirpath = strdup(path);
3673 if (dirpath == NULL) {
3674 err = got_error_from_errno("strdup");
3677 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3680 if (err || pe == NULL) {
3682 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3688 match_path(const char *pattern, size_t pattern_len, const char *path,
3694 * Trailing slashes signify directories.
3695 * Append a * to make such patterns conform to fnmatch rules.
3697 if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
3698 if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
3699 return FNM_NOMATCH; /* XXX */
3701 return fnmatch(buf, path, flags);
3704 return fnmatch(pattern, path, flags);
3708 match_ignores(struct got_pathlist_head *ignores, const char *path)
3710 struct got_pathlist_entry *pe;
3712 /* Handle patterns which match in all directories. */
3713 TAILQ_FOREACH(pe, ignores, entry) {
3714 struct got_pathlist_head *ignorelist = pe->data;
3715 struct got_pathlist_entry *pi;
3717 TAILQ_FOREACH(pi, ignorelist, entry) {
3720 if (pi->path_len < 3 ||
3721 strncmp(pi->path, "**/", 3) != 0)
3725 if (match_path(pi->path + 3,
3726 pi->path_len - 3, p,
3727 FNM_PATHNAME | FNM_LEADING_DIR)) {
3728 /* Retry in next directory. */
3729 while (*p && *p != '/')
3741 * The ignores pathlist contains ignore lists from children before
3742 * parents, so we can find the most specific ignorelist by walking
3743 * ignores backwards.
3745 pe = TAILQ_LAST(ignores, got_pathlist_head);
3747 if (got_path_is_child(path, pe->path, pe->path_len)) {
3748 struct got_pathlist_head *ignorelist = pe->data;
3749 struct got_pathlist_entry *pi;
3750 TAILQ_FOREACH(pi, ignorelist, entry) {
3751 int flags = FNM_LEADING_DIR;
3752 if (strstr(pi->path, "/**/") == NULL)
3753 flags |= FNM_PATHNAME;
3754 if (match_path(pi->path, pi->path_len,
3760 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3766 static const struct got_error *
3767 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3768 const char *path, int dirfd, const char *ignores_filename)
3770 const struct got_error *err = NULL;
3773 FILE *ignoresfile = NULL;
3775 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3776 path[0] ? "/" : "", ignores_filename) == -1)
3777 return got_error_from_errno("asprintf");
3780 fd = openat(dirfd, ignores_filename,
3781 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3783 if (errno != ENOENT && errno != EACCES)
3784 err = got_error_from_errno2("openat",
3787 ignoresfile = fdopen(fd, "r");
3788 if (ignoresfile == NULL)
3789 err = got_error_from_errno2("fdopen",
3793 err = read_ignores(ignores, path, ignoresfile);
3797 ignoresfile = fopen(ignorespath, "re");
3798 if (ignoresfile == NULL) {
3799 if (errno != ENOENT && errno != EACCES)
3800 err = got_error_from_errno2("fopen",
3803 err = read_ignores(ignores, path, ignoresfile);
3806 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3807 err = got_error_from_errno2("fclose", path);
3808 if (fd != -1 && close(fd) == -1 && err == NULL)
3809 err = got_error_from_errno2("close", path);
3814 static const struct got_error *
3815 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3818 const struct got_error *err = NULL;
3819 struct diff_dir_cb_arg *a = arg;
3825 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3826 return got_error(GOT_ERR_CANCELLED);
3828 if (parent_path[0]) {
3829 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3830 return got_error_from_errno("asprintf");
3835 if (de->d_type == DT_DIR) {
3836 if (!a->no_ignores && ignore != NULL &&
3837 match_ignores(a->ignores, path))
3839 } else if (!match_ignores(a->ignores, path) &&
3840 got_path_is_child(path, a->status_path, a->status_path_len))
3841 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3842 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3848 static const struct got_error *
3849 status_traverse(void *arg, const char *path, int dirfd)
3851 const struct got_error *err = NULL;
3852 struct diff_dir_cb_arg *a = arg;
3857 err = add_ignores(a->ignores, a->worktree->root_path,
3858 path, dirfd, ".cvsignore");
3862 err = add_ignores(a->ignores, a->worktree->root_path, path,
3863 dirfd, ".gitignore");
3868 static const struct got_error *
3869 report_single_file_status(const char *path, const char *ondisk_path,
3870 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3871 void *status_arg, struct got_repository *repo, int report_unchanged,
3872 struct got_pathlist_head *ignores, int no_ignores)
3874 struct got_fileindex_entry *ie;
3877 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3879 return report_file_status(ie, ondisk_path, -1, NULL,
3880 status_cb, status_arg, repo, report_unchanged);
3882 if (lstat(ondisk_path, &sb) == -1) {
3883 if (errno != ENOENT)
3884 return got_error_from_errno2("lstat", ondisk_path);
3885 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3886 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3889 if (!no_ignores && match_ignores(ignores, path))
3892 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3893 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3894 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3899 static const struct got_error *
3900 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3901 const char *root_path, const char *path)
3903 const struct got_error *err;
3904 char *parent_path, *next_parent_path = NULL;
3906 err = add_ignores(ignores, root_path, "", -1,
3911 err = add_ignores(ignores, root_path, "", -1,
3916 err = got_path_dirname(&parent_path, path);
3918 if (err->code == GOT_ERR_BAD_PATH)
3919 return NULL; /* cannot traverse parent */
3923 err = add_ignores(ignores, root_path, parent_path, -1,
3927 err = add_ignores(ignores, root_path, parent_path, -1,
3931 err = got_path_dirname(&next_parent_path, parent_path);
3933 if (err->code == GOT_ERR_BAD_PATH)
3934 err = NULL; /* traversed everything */
3937 if (got_path_is_root_dir(parent_path))
3940 parent_path = next_parent_path;
3941 next_parent_path = NULL;
3945 free(next_parent_path);
3949 struct find_missing_children_args {
3950 const char *parent_path;
3952 struct got_pathlist_head *children;
3953 got_cancel_cb cancel_cb;
3957 static const struct got_error *
3958 find_missing_children(void *arg, struct got_fileindex_entry *ie)
3960 const struct got_error *err = NULL;
3961 struct find_missing_children_args *a = arg;
3964 err = a->cancel_cb(a->cancel_arg);
3969 if (got_path_is_child(ie->path, a->parent_path, a->parent_len))
3970 err = got_pathlist_append(a->children, ie->path, NULL);
3975 static const struct got_error *
3976 report_children(struct got_pathlist_head *children,
3977 struct got_worktree *worktree, struct got_fileindex *fileindex,
3978 struct got_repository *repo, int is_root_dir, int report_unchanged,
3979 struct got_pathlist_head *ignores, int no_ignores,
3980 got_worktree_status_cb status_cb, void *status_arg,
3981 got_cancel_cb cancel_cb, void *cancel_arg)
3983 const struct got_error *err = NULL;
3984 struct got_pathlist_entry *pe;
3985 char *ondisk_path = NULL;
3987 TAILQ_FOREACH(pe, children, entry) {
3989 err = cancel_cb(cancel_arg);
3994 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
3995 !is_root_dir ? "/" : "", pe->path) == -1) {
3996 err = got_error_from_errno("asprintf");
4001 err = report_single_file_status(pe->path, ondisk_path,
4002 fileindex, status_cb, status_arg, repo, report_unchanged,
4003 ignores, no_ignores);
4015 static const struct got_error *
4016 worktree_status(struct got_worktree *worktree, const char *path,
4017 struct got_fileindex *fileindex, struct got_repository *repo,
4018 got_worktree_status_cb status_cb, void *status_arg,
4019 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
4020 int report_unchanged)
4022 const struct got_error *err = NULL;
4024 struct got_fileindex_diff_dir_cb fdiff_cb;
4025 struct diff_dir_cb_arg arg;
4026 char *ondisk_path = NULL;
4027 struct got_pathlist_head ignores, missing_children;
4028 struct got_fileindex_entry *ie;
4030 TAILQ_INIT(&ignores);
4031 TAILQ_INIT(&missing_children);
4033 if (asprintf(&ondisk_path, "%s%s%s",
4034 worktree->root_path, path[0] ? "/" : "", path) == -1)
4035 return got_error_from_errno("asprintf");
4037 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
4039 err = report_single_file_status(path, ondisk_path,
4040 fileindex, status_cb, status_arg, repo,
4041 report_unchanged, &ignores, no_ignores);
4044 struct find_missing_children_args fmca;
4045 fmca.parent_path = path;
4046 fmca.parent_len = strlen(path);
4047 fmca.children = &missing_children;
4048 fmca.cancel_cb = cancel_cb;
4049 fmca.cancel_arg = cancel_arg;
4050 err = got_fileindex_for_each_entry_safe(fileindex,
4051 find_missing_children, &fmca);
4056 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
4058 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
4059 !got_err_open_nofollow_on_symlink())
4060 err = got_error_from_errno2("open", ondisk_path);
4063 err = add_ignores_from_parent_paths(&ignores,
4064 worktree->root_path, ondisk_path);
4068 if (TAILQ_EMPTY(&missing_children)) {
4069 err = report_single_file_status(path,
4070 ondisk_path, fileindex,
4071 status_cb, status_arg, repo,
4072 report_unchanged, &ignores, no_ignores);
4076 err = report_children(&missing_children,
4077 worktree, fileindex, repo,
4078 (path[0] == '\0'), report_unchanged,
4079 &ignores, no_ignores,
4080 status_cb, status_arg,
4081 cancel_cb, cancel_arg);
4087 fdiff_cb.diff_old_new = status_old_new;
4088 fdiff_cb.diff_old = status_old;
4089 fdiff_cb.diff_new = status_new;
4090 fdiff_cb.diff_traverse = status_traverse;
4091 arg.fileindex = fileindex;
4092 arg.worktree = worktree;
4093 arg.status_path = path;
4094 arg.status_path_len = strlen(path);
4096 arg.status_cb = status_cb;
4097 arg.status_arg = status_arg;
4098 arg.cancel_cb = cancel_cb;
4099 arg.cancel_arg = cancel_arg;
4100 arg.report_unchanged = report_unchanged;
4101 arg.no_ignores = no_ignores;
4103 err = add_ignores_from_parent_paths(&ignores,
4104 worktree->root_path, path);
4108 arg.ignores = &ignores;
4109 err = got_fileindex_diff_dir(fileindex, fd,
4110 worktree->root_path, path, repo, &fdiff_cb, &arg);
4113 free_ignores(&ignores);
4114 if (fd != -1 && close(fd) == -1 && err == NULL)
4115 err = got_error_from_errno("close");
4120 const struct got_error *
4121 got_worktree_status(struct got_worktree *worktree,
4122 struct got_pathlist_head *paths, struct got_repository *repo,
4123 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
4124 got_cancel_cb cancel_cb, void *cancel_arg)
4126 const struct got_error *err = NULL;
4127 char *fileindex_path = NULL;
4128 struct got_fileindex *fileindex = NULL;
4129 struct got_pathlist_entry *pe;
4131 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4135 TAILQ_FOREACH(pe, paths, entry) {
4136 err = worktree_status(worktree, pe->path, fileindex, repo,
4137 status_cb, status_arg, cancel_cb, cancel_arg,
4142 free(fileindex_path);
4143 got_fileindex_free(fileindex);
4147 const struct got_error *
4148 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
4151 const struct got_error *err = NULL;
4152 char *resolved = NULL, *cwd = NULL, *path = NULL;
4155 char *abspath = NULL;
4156 char canonpath[PATH_MAX];
4160 cwd = getcwd(NULL, 0);
4162 return got_error_from_errno("getcwd");
4164 if (lstat(arg, &sb) == -1) {
4165 if (errno != ENOENT) {
4166 err = got_error_from_errno2("lstat", arg);
4171 if (S_ISLNK(sb.st_mode)) {
4173 * We cannot use realpath(3) with symlinks since we want to
4174 * operate on the symlink itself.
4175 * But we can make the path absolute, assuming it is relative
4176 * to the current working directory, and then canonicalize it.
4178 if (!got_path_is_absolute(arg)) {
4179 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4180 err = got_error_from_errno("asprintf");
4185 err = got_canonpath(abspath ? abspath : arg, canonpath,
4189 resolved = strdup(canonpath);
4190 if (resolved == NULL) {
4191 err = got_error_from_errno("strdup");
4195 resolved = realpath(arg, NULL);
4196 if (resolved == NULL) {
4197 if (errno != ENOENT) {
4198 err = got_error_from_errno2("realpath", arg);
4201 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4202 err = got_error_from_errno("asprintf");
4205 err = got_canonpath(abspath, canonpath,
4209 resolved = strdup(canonpath);
4210 if (resolved == NULL) {
4211 err = got_error_from_errno("strdup");
4217 if (strncmp(got_worktree_get_root_path(worktree), resolved,
4218 strlen(got_worktree_get_root_path(worktree)))) {
4219 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
4223 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
4224 err = got_path_skip_common_ancestor(&path,
4225 got_worktree_get_root_path(worktree), resolved);
4231 err = got_error_from_errno("strdup");
4236 /* XXX status walk can't deal with trailing slash! */
4238 while (len > 0 && path[len - 1] == '/') {
4239 path[len - 1] = '\0';
4253 struct schedule_addition_args {
4254 struct got_worktree *worktree;
4255 struct got_fileindex *fileindex;
4256 got_worktree_checkout_cb progress_cb;
4258 struct got_repository *repo;
4262 add_noop_status(unsigned char status)
4264 return (status == GOT_STATUS_ADD ||
4265 status == GOT_STATUS_MODIFY ||
4266 status == GOT_STATUS_CONFLICT ||
4267 status == GOT_STATUS_MODE_CHANGE ||
4268 status == GOT_STATUS_NO_CHANGE);
4271 static const struct got_error *
4272 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4273 const char *relpath, struct got_object_id *blob_id,
4274 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4275 int dirfd, const char *de_name)
4277 struct schedule_addition_args *a = arg;
4278 const struct got_error *err = NULL;
4279 struct got_fileindex_entry *ie;
4283 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4285 return got_error_from_errno("asprintf");
4287 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4289 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4293 /* Re-adding an existing entry is a no-op. */
4294 if (staged_status == GOT_STATUS_NO_CHANGE &&
4295 add_noop_status(status))
4297 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4302 if (status != GOT_STATUS_UNVERSIONED) {
4303 if (status == GOT_STATUS_NONEXISTENT)
4304 err = got_error_set_errno(ENOENT, ondisk_path);
4306 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4310 err = got_fileindex_entry_alloc(&ie, relpath);
4313 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4314 relpath, NULL, NULL, 1);
4316 got_fileindex_entry_free(ie);
4319 err = got_fileindex_entry_add(a->fileindex, ie);
4321 got_fileindex_entry_free(ie);
4328 if (staged_status == GOT_STATUS_NO_CHANGE && add_noop_status(status))
4330 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4333 const struct got_error *
4334 got_worktree_schedule_add(struct got_worktree *worktree,
4335 struct got_pathlist_head *paths,
4336 got_worktree_checkout_cb progress_cb, void *progress_arg,
4337 struct got_repository *repo, int no_ignores)
4339 struct got_fileindex *fileindex = NULL;
4340 char *fileindex_path = NULL;
4341 const struct got_error *err = NULL, *sync_err, *unlockerr;
4342 struct got_pathlist_entry *pe;
4343 struct schedule_addition_args saa;
4345 err = lock_worktree(worktree, LOCK_EX);
4349 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4353 saa.worktree = worktree;
4354 saa.fileindex = fileindex;
4355 saa.progress_cb = progress_cb;
4356 saa.progress_arg = progress_arg;
4359 TAILQ_FOREACH(pe, paths, entry) {
4360 err = worktree_status(worktree, pe->path, fileindex, repo,
4361 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4365 sync_err = sync_fileindex(fileindex, fileindex_path);
4366 if (sync_err && err == NULL)
4369 free(fileindex_path);
4371 got_fileindex_free(fileindex);
4372 unlockerr = lock_worktree(worktree, LOCK_SH);
4373 if (unlockerr && err == NULL)
4378 struct schedule_deletion_args {
4379 struct got_worktree *worktree;
4380 struct got_fileindex *fileindex;
4381 got_worktree_delete_cb progress_cb;
4383 struct got_repository *repo;
4384 int delete_local_mods;
4386 int ignore_missing_paths;
4387 const char *status_path;
4388 size_t status_path_len;
4389 const char *status_codes;
4392 static const struct got_error *
4393 schedule_for_deletion(void *arg, unsigned char status,
4394 unsigned char staged_status, const char *relpath,
4395 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4396 struct got_object_id *commit_id, int dirfd, const char *de_name)
4398 struct schedule_deletion_args *a = arg;
4399 const struct got_error *err = NULL;
4400 struct got_fileindex_entry *ie = NULL;
4404 if (status == GOT_STATUS_NONEXISTENT) {
4405 if (a->ignore_missing_paths)
4407 return got_error_set_errno(ENOENT, relpath);
4410 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4412 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4414 staged_status = get_staged_status(ie);
4415 if (staged_status != GOT_STATUS_NO_CHANGE) {
4416 if (staged_status == GOT_STATUS_DELETE)
4418 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4421 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4423 return got_error_from_errno("asprintf");
4425 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4430 if (a->status_codes) {
4431 size_t ncodes = strlen(a->status_codes);
4433 for (i = 0; i < ncodes ; i++) {
4434 if (status == a->status_codes[i])
4438 /* Do not delete files in non-matching status. */
4442 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4443 a->status_codes[i] != GOT_STATUS_MISSING) {
4444 static char msg[64];
4445 snprintf(msg, sizeof(msg),
4446 "invalid status code '%c'", a->status_codes[i]);
4447 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4452 if (status != GOT_STATUS_NO_CHANGE) {
4453 if (status == GOT_STATUS_DELETE)
4455 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4456 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4459 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4460 err = got_error_set_errno(ENOENT, relpath);
4463 if (status != GOT_STATUS_MODIFY &&
4464 status != GOT_STATUS_MISSING) {
4465 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4470 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4474 if (unlinkat(dirfd, de_name, 0) == -1) {
4475 err = got_error_from_errno2("unlinkat",
4479 } else if (unlink(ondisk_path) == -1) {
4480 err = got_error_from_errno2("unlink", ondisk_path);
4484 root_len = strlen(a->worktree->root_path);
4488 err = got_path_dirname(&parent, ondisk_path);
4492 ondisk_path = parent;
4493 if (got_path_cmp(ondisk_path, a->status_path,
4494 strlen(ondisk_path), a->status_path_len) != 0 &&
4495 !got_path_is_child(ondisk_path, a->status_path,
4496 a->status_path_len))
4498 if (rmdir(ondisk_path) == -1) {
4499 if (errno != ENOTEMPTY)
4500 err = got_error_from_errno2("rmdir",
4504 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4505 strlen(ondisk_path), root_len) != 0);
4508 got_fileindex_entry_mark_deleted_from_disk(ie);
4513 if (status == GOT_STATUS_DELETE)
4515 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4516 staged_status, relpath);
4519 const struct got_error *
4520 got_worktree_schedule_delete(struct got_worktree *worktree,
4521 struct got_pathlist_head *paths, int delete_local_mods,
4522 const char *status_codes,
4523 got_worktree_delete_cb progress_cb, void *progress_arg,
4524 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4526 struct got_fileindex *fileindex = NULL;
4527 char *fileindex_path = NULL;
4528 const struct got_error *err = NULL, *sync_err, *unlockerr;
4529 struct got_pathlist_entry *pe;
4530 struct schedule_deletion_args sda;
4532 err = lock_worktree(worktree, LOCK_EX);
4536 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4540 sda.worktree = worktree;
4541 sda.fileindex = fileindex;
4542 sda.progress_cb = progress_cb;
4543 sda.progress_arg = progress_arg;
4545 sda.delete_local_mods = delete_local_mods;
4546 sda.keep_on_disk = keep_on_disk;
4547 sda.ignore_missing_paths = ignore_missing_paths;
4548 sda.status_codes = status_codes;
4550 TAILQ_FOREACH(pe, paths, entry) {
4551 char *ondisk_status_path;
4553 if (asprintf(&ondisk_status_path, "%s%s%s",
4554 got_worktree_get_root_path(worktree),
4555 pe->path[0] == '\0' ? "" : "/", pe->path) == -1) {
4556 err = got_error_from_errno("asprintf");
4559 sda.status_path = ondisk_status_path;
4560 sda.status_path_len = strlen(ondisk_status_path);
4561 err = worktree_status(worktree, pe->path, fileindex, repo,
4562 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4563 free(ondisk_status_path);
4567 sync_err = sync_fileindex(fileindex, fileindex_path);
4568 if (sync_err && err == NULL)
4571 free(fileindex_path);
4573 got_fileindex_free(fileindex);
4574 unlockerr = lock_worktree(worktree, LOCK_SH);
4575 if (unlockerr && err == NULL)
4580 static const struct got_error *
4581 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4583 const struct got_error *err = NULL;
4585 size_t linesize = 0, n;
4588 linelen = getline(&line, &linesize, infile);
4589 if (linelen == -1) {
4590 if (ferror(infile)) {
4591 err = got_error_from_errno("getline");
4597 n = fwrite(line, 1, linelen, outfile);
4599 err = got_ferror(outfile, GOT_ERR_IO);
4604 n = fwrite(line, 1, linelen, rejectfile);
4606 err = got_ferror(rejectfile, GOT_ERR_IO);
4613 static const struct got_error *
4614 skip_one_line(FILE *f)
4617 size_t linesize = 0;
4620 linelen = getline(&line, &linesize, f);
4621 if (linelen == -1) {
4623 return got_error_from_errno("getline");
4630 static const struct got_error *
4631 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4632 int start_old, int end_old, int start_new, int end_new,
4633 FILE *outfile, FILE *rejectfile)
4635 const struct got_error *err;
4637 /* Copy old file's lines leading up to patch. */
4638 while (!feof(f1) && *line_cur1 < start_old) {
4639 err = copy_one_line(f1, outfile, NULL);
4644 /* Skip new file's lines leading up to patch. */
4645 while (!feof(f2) && *line_cur2 < start_new) {
4647 err = copy_one_line(f2, NULL, rejectfile);
4649 err = skip_one_line(f2);
4654 /* Copy patched lines. */
4655 while (!feof(f2) && *line_cur2 <= end_new) {
4656 err = copy_one_line(f2, outfile, NULL);
4661 /* Skip over old file's replaced lines. */
4662 while (!feof(f1) && *line_cur1 <= end_old) {
4664 err = copy_one_line(f1, NULL, rejectfile);
4666 err = skip_one_line(f1);
4675 static const struct got_error *
4676 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4677 FILE *outfile, FILE *rejectfile)
4679 const struct got_error *err;
4682 /* Copy old file's lines until EOF. */
4684 err = copy_one_line(f1, outfile, NULL);
4691 /* Copy new file's lines until EOF. */
4693 err = copy_one_line(f2, NULL, rejectfile);
4703 static const struct got_error *
4704 apply_or_reject_change(int *choice, int *nchunks_used,
4705 struct diff_result *diff_result, int n,
4706 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4707 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4708 got_worktree_patch_cb patch_cb, void *patch_arg)
4710 const struct got_error *err = NULL;
4711 struct diff_chunk_context cc = {};
4712 int start_old, end_old, start_new, end_new;
4714 struct diff_output_unidiff_state *diff_state;
4715 struct diff_input_info diff_info;
4718 *choice = GOT_PATCH_CHOICE_NONE;
4720 /* Get changed line numbers without context lines for copy_change(). */
4721 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4722 start_old = cc.left.start;
4723 end_old = cc.left.end;
4724 start_new = cc.right.start;
4725 end_new = cc.right.end;
4727 /* Get the same change with context lines for display. */
4728 memset(&cc, 0, sizeof(cc));
4729 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4731 memset(&diff_info, 0, sizeof(diff_info));
4732 diff_info.left_path = relpath;
4733 diff_info.right_path = relpath;
4735 diff_state = diff_output_unidiff_state_alloc();
4736 if (diff_state == NULL)
4737 return got_error_set_errno(ENOMEM,
4738 "diff_output_unidiff_state_alloc");
4740 hunkfile = got_opentemp();
4741 if (hunkfile == NULL) {
4742 err = got_error_from_errno("got_opentemp");
4746 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4748 if (rc != DIFF_RC_OK) {
4749 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4753 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4754 err = got_ferror(hunkfile, GOT_ERR_IO);
4758 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4759 hunkfile, changeno, nchanges);
4764 case GOT_PATCH_CHOICE_YES:
4765 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4766 end_old, start_new, end_new, outfile, rejectfile);
4768 case GOT_PATCH_CHOICE_NO:
4769 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4770 end_old, start_new, end_new, rejectfile, outfile);
4772 case GOT_PATCH_CHOICE_QUIT:
4775 err = got_error(GOT_ERR_PATCH_CHOICE);
4779 diff_output_unidiff_state_free(diff_state);
4780 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4781 err = got_error_from_errno("fclose");
4785 struct revert_file_args {
4786 struct got_worktree *worktree;
4787 struct got_fileindex *fileindex;
4788 got_worktree_checkout_cb progress_cb;
4790 got_worktree_patch_cb patch_cb;
4792 struct got_repository *repo;
4793 int unlink_added_files;
4794 struct got_pathlist_head *added_files_to_unlink;
4797 static const struct got_error *
4798 create_patched_content(char **path_outfile, int reverse_patch,
4799 struct got_object_id *blob_id, const char *path2,
4800 int dirfd2, const char *de_name2,
4801 const char *relpath, struct got_repository *repo,
4802 got_worktree_patch_cb patch_cb, void *patch_arg)
4804 const struct got_error *err, *free_err;
4805 struct got_blob_object *blob = NULL;
4806 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4807 int fd = -1, fd2 = -1;
4808 char link_target[PATH_MAX];
4809 ssize_t link_len = 0;
4810 char *path1 = NULL, *id_str = NULL;
4812 struct got_diffreg_result *diffreg_result = NULL;
4813 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4814 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4816 *path_outfile = NULL;
4818 err = got_object_id_str(&id_str, blob_id);
4823 fd2 = openat(dirfd2, de_name2,
4824 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4826 if (!got_err_open_nofollow_on_symlink()) {
4827 err = got_error_from_errno2("openat", path2);
4830 link_len = readlinkat(dirfd2, de_name2,
4831 link_target, sizeof(link_target));
4832 if (link_len == -1) {
4833 return got_error_from_errno2("readlinkat",
4836 sb2.st_mode = S_IFLNK;
4837 sb2.st_size = link_len;
4840 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4842 if (!got_err_open_nofollow_on_symlink()) {
4843 err = got_error_from_errno2("open", path2);
4846 link_len = readlink(path2, link_target,
4847 sizeof(link_target));
4849 return got_error_from_errno2("readlink", path2);
4850 sb2.st_mode = S_IFLNK;
4851 sb2.st_size = link_len;
4855 if (fstat(fd2, &sb2) == -1) {
4856 err = got_error_from_errno2("fstat", path2);
4860 f2 = fdopen(fd2, "r");
4862 err = got_error_from_errno2("fdopen", path2);
4868 f2 = got_opentemp();
4870 err = got_error_from_errno2("got_opentemp", path2);
4873 n = fwrite(link_target, 1, link_len, f2);
4874 if (n != link_len) {
4875 err = got_ferror(f2, GOT_ERR_IO);
4878 if (fflush(f2) == EOF) {
4879 err = got_error_from_errno("fflush");
4885 fd = got_opentempfd();
4887 err = got_error_from_errno("got_opentempfd");
4891 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4895 err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4899 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4903 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4904 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4908 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4913 if (fseek(f1, 0L, SEEK_SET) == -1)
4914 return got_ferror(f1, GOT_ERR_IO);
4915 if (fseek(f2, 0L, SEEK_SET) == -1)
4916 return got_ferror(f2, GOT_ERR_IO);
4918 /* Count the number of actual changes in the diff result. */
4919 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4920 struct diff_chunk_context cc = {};
4921 diff_chunk_context_load_change(&cc, &nchunks_used,
4922 diffreg_result->result, n, 0);
4925 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4927 err = apply_or_reject_change(&choice, &nchunks_used,
4928 diffreg_result->result, n, relpath, f1, f2,
4929 &line_cur1, &line_cur2,
4930 reverse_patch ? NULL : outfile,
4931 reverse_patch ? outfile : NULL,
4932 ++i, nchanges, patch_cb, patch_arg);
4935 if (choice == GOT_PATCH_CHOICE_YES)
4937 else if (choice == GOT_PATCH_CHOICE_QUIT)
4941 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4942 reverse_patch ? NULL : outfile,
4943 reverse_patch ? outfile : NULL);
4947 if (!S_ISLNK(sb2.st_mode)) {
4950 mode = apply_umask(sb2.st_mode);
4951 if (fchmod(fileno(outfile), mode) == -1) {
4952 err = got_error_from_errno2("fchmod", path2);
4959 if (fd != -1 && close(fd) == -1 && err == NULL)
4960 err = got_error_from_errno("close");
4962 got_object_blob_close(blob);
4963 free_err = got_diffreg_result_free(diffreg_result);
4966 if (f1 && fclose(f1) == EOF && err == NULL)
4967 err = got_error_from_errno2("fclose", path1);
4968 if (f2 && fclose(f2) == EOF && err == NULL)
4969 err = got_error_from_errno2("fclose", path2);
4970 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4971 err = got_error_from_errno2("close", path2);
4972 if (outfile && fclose(outfile) == EOF && err == NULL)
4973 err = got_error_from_errno2("fclose", *path_outfile);
4974 if (path1 && unlink(path1) == -1 && err == NULL)
4975 err = got_error_from_errno2("unlink", path1);
4976 if (err || !have_content) {
4977 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4978 err = got_error_from_errno2("unlink", *path_outfile);
4979 free(*path_outfile);
4980 *path_outfile = NULL;
4986 static const struct got_error *
4987 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4988 const char *relpath, struct got_object_id *blob_id,
4989 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4990 int dirfd, const char *de_name)
4992 struct revert_file_args *a = arg;
4993 const struct got_error *err = NULL;
4994 char *parent_path = NULL;
4995 struct got_fileindex_entry *ie;
4996 struct got_commit_object *base_commit = NULL;
4997 struct got_tree_object *tree = NULL;
4998 struct got_object_id *tree_id = NULL;
4999 const struct got_tree_entry *te = NULL;
5000 char *tree_path = NULL, *te_name;
5001 char *ondisk_path = NULL, *path_content = NULL;
5002 struct got_blob_object *blob = NULL;
5005 /* Reverting a staged deletion is a no-op. */
5006 if (status == GOT_STATUS_DELETE &&
5007 staged_status != GOT_STATUS_NO_CHANGE)
5010 if (status == GOT_STATUS_UNVERSIONED)
5011 return (*a->progress_cb)(a->progress_arg,
5012 GOT_STATUS_UNVERSIONED, relpath);
5014 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5016 return got_error_path(relpath, GOT_ERR_BAD_PATH);
5018 /* Construct in-repository path of tree which contains this blob. */
5019 err = got_path_dirname(&parent_path, ie->path);
5021 if (err->code != GOT_ERR_BAD_PATH)
5023 parent_path = strdup("/");
5024 if (parent_path == NULL) {
5025 err = got_error_from_errno("strdup");
5029 if (got_path_is_root_dir(a->worktree->path_prefix)) {
5030 tree_path = strdup(parent_path);
5031 if (tree_path == NULL) {
5032 err = got_error_from_errno("strdup");
5036 if (got_path_is_root_dir(parent_path)) {
5037 tree_path = strdup(a->worktree->path_prefix);
5038 if (tree_path == NULL) {
5039 err = got_error_from_errno("strdup");
5043 if (asprintf(&tree_path, "%s/%s",
5044 a->worktree->path_prefix, parent_path) == -1) {
5045 err = got_error_from_errno("asprintf");
5051 err = got_object_open_as_commit(&base_commit, a->repo,
5052 a->worktree->base_commit_id);
5056 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
5058 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
5059 (status == GOT_STATUS_ADD ||
5060 staged_status == GOT_STATUS_ADD)))
5063 err = got_object_open_as_tree(&tree, a->repo, tree_id);
5067 err = got_path_basename(&te_name, ie->path);
5071 te = got_object_tree_find_entry(tree, te_name);
5073 if (te == NULL && status != GOT_STATUS_ADD &&
5074 staged_status != GOT_STATUS_ADD) {
5075 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
5081 case GOT_STATUS_ADD:
5083 int choice = GOT_PATCH_CHOICE_NONE;
5084 err = (*a->patch_cb)(&choice, a->patch_arg,
5085 status, ie->path, NULL, 1, 1);
5088 if (choice != GOT_PATCH_CHOICE_YES)
5091 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
5095 got_fileindex_entry_remove(a->fileindex, ie);
5096 if (a->unlink_added_files) {
5097 int do_unlink = a->added_files_to_unlink ? 0 : 1;
5099 if (a->added_files_to_unlink) {
5100 struct got_pathlist_entry *pe;
5102 TAILQ_FOREACH(pe, a->added_files_to_unlink,
5104 if (got_path_cmp(pe->path, relpath,
5105 pe->path_len, strlen(relpath)))
5113 if (asprintf(&ondisk_path, "%s/%s",
5114 got_worktree_get_root_path(a->worktree),
5116 err = got_error_from_errno("asprintf");
5119 if (unlink(ondisk_path) == -1) {
5120 err = got_error_from_errno2("unlink",
5127 case GOT_STATUS_DELETE:
5129 int choice = GOT_PATCH_CHOICE_NONE;
5130 err = (*a->patch_cb)(&choice, a->patch_arg,
5131 status, ie->path, NULL, 1, 1);
5134 if (choice != GOT_PATCH_CHOICE_YES)
5138 case GOT_STATUS_MODIFY:
5139 case GOT_STATUS_MODE_CHANGE:
5140 case GOT_STATUS_CONFLICT:
5141 case GOT_STATUS_MISSING: {
5142 struct got_object_id id;
5143 if (staged_status == GOT_STATUS_ADD ||
5144 staged_status == GOT_STATUS_MODIFY)
5145 got_fileindex_entry_get_staged_blob_id(&id, ie);
5147 got_fileindex_entry_get_blob_id(&id, ie);
5148 fd = got_opentempfd();
5150 err = got_error_from_errno("got_opentempfd");
5154 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
5158 if (asprintf(&ondisk_path, "%s/%s",
5159 got_worktree_get_root_path(a->worktree), relpath) == -1) {
5160 err = got_error_from_errno("asprintf");
5164 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
5165 status == GOT_STATUS_CONFLICT)) {
5166 int is_bad_symlink = 0;
5167 err = create_patched_content(&path_content, 1, &id,
5168 ondisk_path, dirfd, de_name, ie->path, a->repo,
5169 a->patch_cb, a->patch_arg);
5170 if (err || path_content == NULL)
5172 if (te && S_ISLNK(te->mode)) {
5173 if (unlink(path_content) == -1) {
5174 err = got_error_from_errno2("unlink",
5178 err = install_symlink(&is_bad_symlink,
5179 a->worktree, ondisk_path, ie->path,
5180 blob, 0, 1, 0, 0, a->repo,
5181 a->progress_cb, a->progress_arg);
5183 if (rename(path_content, ondisk_path) == -1) {
5184 err = got_error_from_errno3("rename",
5185 path_content, ondisk_path);
5190 int is_bad_symlink = 0;
5191 if (te && S_ISLNK(te->mode)) {
5192 err = install_symlink(&is_bad_symlink,
5193 a->worktree, ondisk_path, ie->path,
5194 blob, 0, 1, 0, 0, a->repo,
5195 a->progress_cb, a->progress_arg);
5197 err = install_blob(a->worktree, ondisk_path,
5199 te ? te->mode : GOT_DEFAULT_FILE_MODE,
5200 got_fileindex_perms_to_st(ie), blob,
5201 0, 1, 0, 0, a->repo,
5202 a->progress_cb, a->progress_arg);
5206 if (status == GOT_STATUS_DELETE ||
5207 status == GOT_STATUS_MODE_CHANGE) {
5208 err = got_fileindex_entry_update(ie,
5209 a->worktree->root_fd, relpath,
5211 a->worktree->base_commit_id->sha1, 1);
5215 if (is_bad_symlink) {
5216 got_fileindex_entry_filetype_set(ie,
5217 GOT_FILEIDX_MODE_BAD_SYMLINK);
5230 if (fd != -1 && close(fd) == -1 && err == NULL)
5231 err = got_error_from_errno("close");
5233 got_object_blob_close(blob);
5235 got_object_tree_close(tree);
5238 got_object_commit_close(base_commit);
5242 const struct got_error *
5243 got_worktree_revert(struct got_worktree *worktree,
5244 struct got_pathlist_head *paths,
5245 got_worktree_checkout_cb progress_cb, void *progress_arg,
5246 got_worktree_patch_cb patch_cb, void *patch_arg,
5247 struct got_repository *repo)
5249 struct got_fileindex *fileindex = NULL;
5250 char *fileindex_path = NULL;
5251 const struct got_error *err = NULL, *unlockerr = NULL;
5252 const struct got_error *sync_err = NULL;
5253 struct got_pathlist_entry *pe;
5254 struct revert_file_args rfa;
5256 err = lock_worktree(worktree, LOCK_EX);
5260 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5264 rfa.worktree = worktree;
5265 rfa.fileindex = fileindex;
5266 rfa.progress_cb = progress_cb;
5267 rfa.progress_arg = progress_arg;
5268 rfa.patch_cb = patch_cb;
5269 rfa.patch_arg = patch_arg;
5271 rfa.unlink_added_files = 0;
5272 TAILQ_FOREACH(pe, paths, entry) {
5273 err = worktree_status(worktree, pe->path, fileindex, repo,
5274 revert_file, &rfa, NULL, NULL, 1, 0);
5278 sync_err = sync_fileindex(fileindex, fileindex_path);
5279 if (sync_err && err == NULL)
5282 free(fileindex_path);
5284 got_fileindex_free(fileindex);
5285 unlockerr = lock_worktree(worktree, LOCK_SH);
5286 if (unlockerr && err == NULL)
5292 free_commitable(struct got_commitable *ct)
5295 free(ct->in_repo_path);
5296 free(ct->ondisk_path);
5298 free(ct->base_blob_id);
5299 free(ct->staged_blob_id);
5300 free(ct->base_commit_id);
5304 struct collect_commitables_arg {
5305 struct got_pathlist_head *commitable_paths;
5306 struct got_repository *repo;
5307 struct got_worktree *worktree;
5308 struct got_fileindex *fileindex;
5309 int have_staged_files;
5310 int allow_bad_symlinks;
5311 int diff_header_shown;
5312 int commit_conflicts;
5319 * Create a file which contains the target path of a symlink so we can feed
5320 * it as content to the diff engine.
5322 static const struct got_error *
5323 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5324 const char *abspath)
5326 const struct got_error *err = NULL;
5327 char target_path[PATH_MAX];
5328 ssize_t target_len, outlen;
5333 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5334 if (target_len == -1)
5335 return got_error_from_errno2("readlinkat", abspath);
5337 target_len = readlink(abspath, target_path, PATH_MAX);
5338 if (target_len == -1)
5339 return got_error_from_errno2("readlink", abspath);
5342 *fd = got_opentempfd();
5344 return got_error_from_errno("got_opentempfd");
5346 outlen = write(*fd, target_path, target_len);
5348 err = got_error_from_errno("got_opentempfd");
5352 if (lseek(*fd, 0, SEEK_SET) == -1) {
5353 err = got_error_from_errno2("lseek", abspath);
5364 static const struct got_error *
5365 append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5366 FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5367 int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5369 const struct got_error *err = NULL;
5370 struct got_blob_object *blob1 = NULL;
5371 int fd = -1, fd1 = -1, fd2 = -1;
5372 FILE *ondisk_file = NULL;
5373 char *label1 = NULL;
5377 char *id_str = NULL;
5379 memset(&sb, 0, sizeof(sb));
5382 if (ct->staged_status != GOT_STATUS_MODIFY &&
5383 ct->staged_status != GOT_STATUS_ADD &&
5384 ct->staged_status != GOT_STATUS_DELETE)
5387 if (ct->status != GOT_STATUS_MODIFY &&
5388 ct->status != GOT_STATUS_ADD &&
5389 ct->status != GOT_STATUS_DELETE &&
5390 ct->status != GOT_STATUS_CONFLICT)
5394 err = got_opentemp_truncate(f1);
5396 return got_error_from_errno("got_opentemp_truncate");
5397 err = got_opentemp_truncate(f2);
5399 return got_error_from_errno("got_opentemp_truncate");
5401 if (!*diff_header_shown) {
5402 err = got_object_id_str(&id_str, worktree->base_commit_id);
5405 fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5406 got_worktree_get_root_path(worktree));
5407 fprintf(diff_outfile, "commit - %s\n", id_str);
5408 fprintf(diff_outfile, "path + %s%s\n",
5409 got_worktree_get_root_path(worktree),
5410 diff_staged ? " (staged changes)" : "");
5411 *diff_header_shown = 1;
5415 const char *label1 = NULL, *label2 = NULL;
5416 switch (ct->staged_status) {
5417 case GOT_STATUS_MODIFY:
5421 case GOT_STATUS_ADD:
5424 case GOT_STATUS_DELETE:
5428 return got_error(GOT_ERR_FILE_STATUS);
5430 fd1 = got_opentempfd();
5432 err = got_error_from_errno("got_opentempfd");
5435 fd2 = got_opentempfd();
5437 err = got_error_from_errno("got_opentempfd");
5440 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5441 fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5442 label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5443 NULL, repo, diff_outfile);
5447 fd1 = got_opentempfd();
5449 err = got_error_from_errno("got_opentempfd");
5453 if (ct->status != GOT_STATUS_ADD) {
5454 err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5460 if (ct->status != GOT_STATUS_DELETE) {
5462 fd = openat(dirfd, de_name,
5463 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5465 if (!got_err_open_nofollow_on_symlink()) {
5466 err = got_error_from_errno2("openat",
5470 err = get_symlink_target_file(&fd, dirfd,
5471 de_name, ct->ondisk_path);
5476 fd = open(ct->ondisk_path,
5477 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5479 if (!got_err_open_nofollow_on_symlink()) {
5480 err = got_error_from_errno2("open",
5484 err = get_symlink_target_file(&fd, dirfd,
5485 de_name, ct->ondisk_path);
5490 if (fstatat(fd, ct->ondisk_path, &sb,
5491 AT_SYMLINK_NOFOLLOW) == -1) {
5492 err = got_error_from_errno2("fstatat", ct->ondisk_path);
5495 ondisk_file = fdopen(fd, "r");
5496 if (ondisk_file == NULL) {
5497 err = got_error_from_errno2("fdopen", ct->ondisk_path);
5505 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5511 err = got_diff_blob_file(blob1, f1, size1, label1,
5512 ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5513 GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5515 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5516 err = got_error_from_errno("close");
5517 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5518 err = got_error_from_errno("close");
5520 got_object_blob_close(blob1);
5521 if (fd != -1 && close(fd) == -1 && err == NULL)
5522 err = got_error_from_errno("close");
5523 if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5524 err = got_error_from_errno("fclose");
5528 static const struct got_error *
5529 collect_commitables(void *arg, unsigned char status,
5530 unsigned char staged_status, const char *relpath,
5531 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5532 struct got_object_id *commit_id, int dirfd, const char *de_name)
5534 struct collect_commitables_arg *a = arg;
5535 const struct got_error *err = NULL;
5536 struct got_commitable *ct = NULL;
5537 struct got_pathlist_entry *new = NULL;
5538 char *parent_path = NULL, *path = NULL;
5541 if (a->have_staged_files) {
5542 if (staged_status != GOT_STATUS_MODIFY &&
5543 staged_status != GOT_STATUS_ADD &&
5544 staged_status != GOT_STATUS_DELETE)
5547 if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5548 printf("C %s\n", relpath);
5549 return got_error(GOT_ERR_COMMIT_CONFLICT);
5552 if (status != GOT_STATUS_MODIFY &&
5553 status != GOT_STATUS_MODE_CHANGE &&
5554 status != GOT_STATUS_ADD &&
5555 status != GOT_STATUS_DELETE &&
5556 status != GOT_STATUS_CONFLICT)
5560 if (asprintf(&path, "/%s", relpath) == -1) {
5561 err = got_error_from_errno("asprintf");
5564 if (strcmp(path, "/") == 0) {
5565 parent_path = strdup("");
5566 if (parent_path == NULL)
5567 return got_error_from_errno("strdup");
5569 err = got_path_dirname(&parent_path, path);
5574 ct = calloc(1, sizeof(*ct));
5576 err = got_error_from_errno("calloc");
5580 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5582 err = got_error_from_errno("asprintf");
5586 if (staged_status == GOT_STATUS_ADD ||
5587 staged_status == GOT_STATUS_MODIFY) {
5588 struct got_fileindex_entry *ie;
5589 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5590 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5591 case GOT_FILEIDX_MODE_REGULAR_FILE:
5592 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5595 case GOT_FILEIDX_MODE_SYMLINK:
5599 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5602 ct->mode |= got_fileindex_entry_perms_get(ie);
5603 } else if (status != GOT_STATUS_DELETE &&
5604 staged_status != GOT_STATUS_DELETE) {
5606 if (fstatat(dirfd, de_name, &sb,
5607 AT_SYMLINK_NOFOLLOW) == -1) {
5608 err = got_error_from_errno2("fstatat",
5612 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5613 err = got_error_from_errno2("lstat", ct->ondisk_path);
5616 ct->mode = sb.st_mode;
5619 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5620 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5622 err = got_error_from_errno("asprintf");
5626 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5627 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5629 char target_path[PATH_MAX];
5631 target_len = readlink(ct->ondisk_path, target_path,
5632 sizeof(target_path));
5633 if (target_len == -1) {
5634 err = got_error_from_errno2("readlink",
5638 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5639 target_len, ct->ondisk_path, a->worktree->root_path,
5640 a->worktree->meta_dir);
5643 if (is_bad_symlink) {
5644 err = got_error_path(ct->ondisk_path,
5645 GOT_ERR_BAD_SYMLINK);
5651 ct->status = status;
5652 ct->staged_status = staged_status;
5653 ct->blob_id = NULL; /* will be filled in when blob gets created */
5654 if (ct->status != GOT_STATUS_ADD &&
5655 ct->staged_status != GOT_STATUS_ADD) {
5656 ct->base_blob_id = got_object_id_dup(blob_id);
5657 if (ct->base_blob_id == NULL) {
5658 err = got_error_from_errno("got_object_id_dup");
5661 ct->base_commit_id = got_object_id_dup(commit_id);
5662 if (ct->base_commit_id == NULL) {
5663 err = got_error_from_errno("got_object_id_dup");
5667 if (ct->staged_status == GOT_STATUS_ADD ||
5668 ct->staged_status == GOT_STATUS_MODIFY) {
5669 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5670 if (ct->staged_blob_id == NULL) {
5671 err = got_error_from_errno("got_object_id_dup");
5675 ct->path = strdup(path);
5676 if (ct->path == NULL) {
5677 err = got_error_from_errno("strdup");
5680 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5684 if (a->diff_outfile && ct && new != NULL) {
5685 err = append_ct_diff(ct, &a->diff_header_shown,
5686 a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5687 a->have_staged_files, a->repo, a->worktree);
5692 if (ct && (err || new == NULL))
5693 free_commitable(ct);
5699 static const struct got_error *write_tree(struct got_object_id **, int *,
5700 struct got_tree_object *, const char *, struct got_pathlist_head *,
5701 got_worktree_status_cb status_cb, void *status_arg,
5702 struct got_repository *);
5704 static const struct got_error *
5705 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5706 struct got_tree_entry *te, const char *parent_path,
5707 struct got_pathlist_head *commitable_paths,
5708 got_worktree_status_cb status_cb, void *status_arg,
5709 struct got_repository *repo)
5711 const struct got_error *err = NULL;
5712 struct got_tree_object *subtree;
5715 if (asprintf(&subpath, "%s%s%s", parent_path,
5716 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5717 return got_error_from_errno("asprintf");
5719 err = got_object_open_as_tree(&subtree, repo, &te->id);
5723 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5724 commitable_paths, status_cb, status_arg, repo);
5725 got_object_tree_close(subtree);
5730 static const struct got_error *
5731 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5733 const struct got_error *err = NULL;
5734 char *ct_parent_path = NULL;
5738 if (strchr(ct->in_repo_path, '/') == NULL) {
5739 *match = got_path_is_root_dir(path);
5743 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5746 *match = (strcmp(path, ct_parent_path) == 0);
5747 free(ct_parent_path);
5752 get_ct_file_mode(struct got_commitable *ct)
5754 if (S_ISLNK(ct->mode))
5757 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5760 static const struct got_error *
5761 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5762 struct got_tree_entry *te, struct got_commitable *ct)
5764 const struct got_error *err = NULL;
5768 err = got_object_tree_entry_dup(new_te, te);
5772 (*new_te)->mode = get_ct_file_mode(ct);
5774 if (ct->staged_status == GOT_STATUS_MODIFY)
5775 memcpy(&(*new_te)->id, ct->staged_blob_id,
5776 sizeof((*new_te)->id));
5778 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5780 if (err && *new_te) {
5787 static const struct got_error *
5788 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5789 struct got_commitable *ct)
5791 const struct got_error *err = NULL;
5792 char *ct_name = NULL;
5796 *new_te = calloc(1, sizeof(**new_te));
5797 if (*new_te == NULL)
5798 return got_error_from_errno("calloc");
5800 err = got_path_basename(&ct_name, ct->path);
5803 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5804 sizeof((*new_te)->name)) {
5805 err = got_error(GOT_ERR_NO_SPACE);
5809 (*new_te)->mode = get_ct_file_mode(ct);
5811 if (ct->staged_status == GOT_STATUS_ADD)
5812 memcpy(&(*new_te)->id, ct->staged_blob_id,
5813 sizeof((*new_te)->id));
5815 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5818 if (err && *new_te) {
5825 static const struct got_error *
5826 insert_tree_entry(struct got_tree_entry *new_te,
5827 struct got_pathlist_head *paths)
5829 const struct got_error *err = NULL;
5830 struct got_pathlist_entry *new_pe;
5832 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5836 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5840 static const struct got_error *
5841 report_ct_status(struct got_commitable *ct,
5842 got_worktree_status_cb status_cb, void *status_arg)
5844 const char *ct_path = ct->path;
5845 unsigned char status;
5847 if (status_cb == NULL) /* no commit progress output desired */
5850 while (ct_path[0] == '/')
5853 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5854 status = ct->staged_status;
5856 status = ct->status;
5858 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5859 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5862 static const struct got_error *
5863 match_modified_subtree(int *modified, struct got_tree_entry *te,
5864 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5866 const struct got_error *err = NULL;
5867 struct got_pathlist_entry *pe;
5872 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5873 got_path_is_root_dir(base_tree_path) ? "" : "/",
5875 return got_error_from_errno("asprintf");
5877 TAILQ_FOREACH(pe, commitable_paths, entry) {
5878 struct got_commitable *ct = pe->data;
5879 *modified = got_path_is_child(ct->in_repo_path, te_path,
5889 static const struct got_error *
5890 match_deleted_or_modified_ct(struct got_commitable **ctp,
5891 struct got_tree_entry *te, const char *base_tree_path,
5892 struct got_pathlist_head *commitable_paths)
5894 const struct got_error *err = NULL;
5895 struct got_pathlist_entry *pe;
5899 TAILQ_FOREACH(pe, commitable_paths, entry) {
5900 struct got_commitable *ct = pe->data;
5901 char *ct_name = NULL;
5904 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5905 if (ct->status != GOT_STATUS_MODIFY &&
5906 ct->status != GOT_STATUS_MODE_CHANGE &&
5907 ct->status != GOT_STATUS_DELETE &&
5908 ct->status != GOT_STATUS_CONFLICT)
5911 if (ct->staged_status != GOT_STATUS_MODIFY &&
5912 ct->staged_status != GOT_STATUS_DELETE)
5916 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5919 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5925 err = got_path_basename(&ct_name, pe->path);
5929 if (strcmp(te->name, ct_name) != 0) {
5942 static const struct got_error *
5943 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5944 const char *child_path, const char *path_base_tree,
5945 struct got_pathlist_head *commitable_paths,
5946 got_worktree_status_cb status_cb, void *status_arg,
5947 struct got_repository *repo)
5949 const struct got_error *err = NULL;
5950 struct got_tree_entry *new_te;
5952 struct got_object_id *id = NULL;
5957 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5958 got_path_is_root_dir(path_base_tree) ? "" : "/",
5960 return got_error_from_errno("asprintf");
5962 new_te = calloc(1, sizeof(*new_te));
5964 return got_error_from_errno("calloc");
5965 new_te->mode = S_IFDIR;
5967 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5968 sizeof(new_te->name)) {
5969 err = got_error(GOT_ERR_NO_SPACE);
5972 err = write_tree(&id, &nentries, NULL, subtree_path,
5973 commitable_paths, status_cb, status_arg, repo);
5978 memcpy(&new_te->id, id, sizeof(new_te->id));
5987 static const struct got_error *
5988 write_tree(struct got_object_id **new_tree_id, int *nentries,
5989 struct got_tree_object *base_tree, const char *path_base_tree,
5990 struct got_pathlist_head *commitable_paths,
5991 got_worktree_status_cb status_cb, void *status_arg,
5992 struct got_repository *repo)
5994 const struct got_error *err = NULL;
5995 struct got_pathlist_head paths;
5996 struct got_tree_entry *te, *new_te = NULL;
5997 struct got_pathlist_entry *pe;
6002 /* Insert, and recurse into, newly added entries first. */
6003 TAILQ_FOREACH(pe, commitable_paths, entry) {
6004 struct got_commitable *ct = pe->data;
6005 char *child_path = NULL, *slash;
6007 if ((ct->status != GOT_STATUS_ADD &&
6008 ct->staged_status != GOT_STATUS_ADD) ||
6009 (ct->flags & GOT_COMMITABLE_ADDED))
6012 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
6013 strlen(path_base_tree)))
6016 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
6021 slash = strchr(child_path, '/');
6022 if (slash == NULL) {
6023 err = alloc_added_blob_tree_entry(&new_te, ct);
6026 err = report_ct_status(ct, status_cb, status_arg);
6029 ct->flags |= GOT_COMMITABLE_ADDED;
6030 err = insert_tree_entry(new_te, &paths);
6035 *slash = '\0'; /* trim trailing path components */
6036 if (base_tree == NULL ||
6037 got_object_tree_find_entry(base_tree, child_path)
6039 err = make_subtree_for_added_blob(&new_te,
6040 child_path, path_base_tree,
6041 commitable_paths, status_cb, status_arg,
6045 err = insert_tree_entry(new_te, &paths);
6054 int i, nbase_entries;
6055 /* Handle modified and deleted entries. */
6056 nbase_entries = got_object_tree_get_nentries(base_tree);
6057 for (i = 0; i < nbase_entries; i++) {
6058 struct got_commitable *ct = NULL;
6060 te = got_object_tree_get_entry(base_tree, i);
6061 if (got_object_tree_entry_is_submodule(te)) {
6062 /* Entry is a submodule; just copy it. */
6063 err = got_object_tree_entry_dup(&new_te, te);
6066 err = insert_tree_entry(new_te, &paths);
6073 if (S_ISDIR(te->mode)) {
6075 err = got_object_tree_entry_dup(&new_te, te);
6078 err = match_modified_subtree(&modified, te,
6079 path_base_tree, commitable_paths);
6082 /* Avoid recursion into unmodified subtrees. */
6084 struct got_object_id *new_id;
6086 err = write_subtree(&new_id,
6088 path_base_tree, commitable_paths,
6089 status_cb, status_arg, repo);
6092 if (nsubentries == 0) {
6093 /* All entries were deleted. */
6097 memcpy(&new_te->id, new_id,
6098 sizeof(new_te->id));
6101 err = insert_tree_entry(new_te, &paths);
6108 err = match_deleted_or_modified_ct(&ct, te,
6109 path_base_tree, commitable_paths);
6113 /* NB: Deleted entries get dropped here. */
6114 if (ct->status == GOT_STATUS_MODIFY ||
6115 ct->status == GOT_STATUS_MODE_CHANGE ||
6116 ct->status == GOT_STATUS_CONFLICT ||
6117 ct->staged_status == GOT_STATUS_MODIFY) {
6118 err = alloc_modified_blob_tree_entry(
6122 err = insert_tree_entry(new_te, &paths);
6127 err = report_ct_status(ct, status_cb,
6132 /* Entry is unchanged; just copy it. */
6133 err = got_object_tree_entry_dup(&new_te, te);
6136 err = insert_tree_entry(new_te, &paths);
6144 /* Write new list of entries; deleted entries have been dropped. */
6145 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
6147 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
6151 static const struct got_error *
6152 update_fileindex_after_commit(struct got_worktree *worktree,
6153 struct got_pathlist_head *commitable_paths,
6154 struct got_object_id *new_base_commit_id,
6155 struct got_fileindex *fileindex, int have_staged_files)
6157 const struct got_error *err = NULL;
6158 struct got_pathlist_entry *pe;
6159 char *relpath = NULL;
6161 TAILQ_FOREACH(pe, commitable_paths, entry) {
6162 struct got_fileindex_entry *ie;
6163 struct got_commitable *ct = pe->data;
6165 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6167 err = got_path_skip_common_ancestor(&relpath,
6168 worktree->root_path, ct->ondisk_path);
6173 if (ct->status == GOT_STATUS_DELETE ||
6174 ct->staged_status == GOT_STATUS_DELETE) {
6175 got_fileindex_entry_remove(fileindex, ie);
6176 } else if (ct->staged_status == GOT_STATUS_ADD ||
6177 ct->staged_status == GOT_STATUS_MODIFY) {
6178 got_fileindex_entry_stage_set(ie,
6179 GOT_FILEIDX_STAGE_NONE);
6180 got_fileindex_entry_staged_filetype_set(ie, 0);
6182 err = got_fileindex_entry_update(ie,
6183 worktree->root_fd, relpath,
6184 ct->staged_blob_id->sha1,
6185 new_base_commit_id->sha1,
6186 !have_staged_files);
6188 err = got_fileindex_entry_update(ie,
6189 worktree->root_fd, relpath,
6191 new_base_commit_id->sha1,
6192 !have_staged_files);
6194 err = got_fileindex_entry_alloc(&ie, pe->path);
6197 err = got_fileindex_entry_update(ie,
6198 worktree->root_fd, relpath, ct->blob_id->sha1,
6199 new_base_commit_id->sha1, 1);
6201 got_fileindex_entry_free(ie);
6204 err = got_fileindex_entry_add(fileindex, ie);
6206 got_fileindex_entry_free(ie);
6219 static const struct got_error *
6220 check_out_of_date(const char *in_repo_path, unsigned char status,
6221 unsigned char staged_status, struct got_object_id *base_blob_id,
6222 struct got_object_id *base_commit_id,
6223 struct got_object_id *head_commit_id, struct got_repository *repo,
6226 const struct got_error *err = NULL;
6227 struct got_commit_object *commit = NULL;
6228 struct got_object_id *id = NULL;
6230 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
6231 /* Trivial case: base commit == head commit */
6232 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
6235 * Ensure file content which local changes were based
6236 * on matches file content in the branch head.
6238 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6241 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6243 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6244 err = got_error(ood_errcode);
6246 } else if (got_object_id_cmp(id, base_blob_id) != 0)
6247 err = got_error(ood_errcode);
6249 /* Require that added files don't exist in the branch head. */
6250 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6253 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6254 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
6256 err = id ? got_error(ood_errcode) : NULL;
6261 got_object_commit_close(commit);
6265 static const struct got_error *
6266 commit_worktree(struct got_object_id **new_commit_id,
6267 struct got_pathlist_head *commitable_paths,
6268 struct got_object_id *head_commit_id,
6269 struct got_object_id *parent_id2,
6270 struct got_worktree *worktree,
6271 const char *author, const char *committer, char *diff_path,
6272 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6273 got_worktree_status_cb status_cb, void *status_arg,
6274 struct got_repository *repo)
6276 const struct got_error *err = NULL, *unlockerr = NULL;
6277 struct got_pathlist_entry *pe;
6278 const char *head_ref_name = NULL;
6279 struct got_commit_object *head_commit = NULL;
6280 struct got_reference *head_ref2 = NULL;
6281 struct got_object_id *head_commit_id2 = NULL;
6282 struct got_tree_object *head_tree = NULL;
6283 struct got_object_id *new_tree_id = NULL;
6284 int nentries, nparents = 0;
6285 struct got_object_id_queue parent_ids;
6286 struct got_object_qid *pid = NULL;
6287 char *logmsg = NULL;
6290 *new_commit_id = NULL;
6292 STAILQ_INIT(&parent_ids);
6294 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6298 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6302 if (commit_msg_cb != NULL) {
6303 err = commit_msg_cb(commitable_paths, diff_path,
6304 &logmsg, commit_arg);
6309 if (logmsg == NULL || strlen(logmsg) == 0) {
6310 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6314 /* Create blobs from added and modified files and record their IDs. */
6315 TAILQ_FOREACH(pe, commitable_paths, entry) {
6316 struct got_commitable *ct = pe->data;
6319 /* Blobs for staged files already exist. */
6320 if (ct->staged_status == GOT_STATUS_ADD ||
6321 ct->staged_status == GOT_STATUS_MODIFY)
6324 if (ct->status != GOT_STATUS_ADD &&
6325 ct->status != GOT_STATUS_MODIFY &&
6326 ct->status != GOT_STATUS_MODE_CHANGE &&
6327 ct->status != GOT_STATUS_CONFLICT)
6330 if (asprintf(&ondisk_path, "%s/%s",
6331 worktree->root_path, pe->path) == -1) {
6332 err = got_error_from_errno("asprintf");
6335 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6341 /* Recursively write new tree objects. */
6342 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6343 commitable_paths, status_cb, status_arg, repo);
6347 err = got_object_qid_alloc(&pid, head_commit_id);
6350 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6353 err = got_object_qid_alloc(&pid, parent_id2);
6356 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6359 timestamp = time(NULL);
6360 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6361 nparents, author, timestamp, committer, timestamp, logmsg, repo);
6367 /* Check if a concurrent commit to our branch has occurred. */
6368 head_ref_name = got_worktree_get_head_ref_name(worktree);
6369 if (head_ref_name == NULL) {
6370 err = got_error_from_errno("got_worktree_get_head_ref_name");
6373 /* Lock the reference here to prevent concurrent modification. */
6374 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6377 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6380 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6381 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6384 /* Update branch head in repository. */
6385 err = got_ref_change_ref(head_ref2, *new_commit_id);
6388 err = got_ref_write(head_ref2, repo);
6392 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6396 err = ref_base_commit(worktree, repo);
6400 got_object_id_queue_free(&parent_ids);
6402 got_object_tree_close(head_tree);
6404 got_object_commit_close(head_commit);
6405 free(head_commit_id2);
6407 unlockerr = got_ref_unlock(head_ref2);
6408 if (unlockerr && err == NULL)
6410 got_ref_close(head_ref2);
6415 static const struct got_error *
6416 check_path_is_commitable(const char *path,
6417 struct got_pathlist_head *commitable_paths)
6419 struct got_pathlist_entry *cpe = NULL;
6420 size_t path_len = strlen(path);
6422 TAILQ_FOREACH(cpe, commitable_paths, entry) {
6423 struct got_commitable *ct = cpe->data;
6424 const char *ct_path = ct->path;
6426 while (ct_path[0] == '/')
6429 if (strcmp(path, ct_path) == 0 ||
6430 got_path_is_child(ct_path, path, path_len))
6435 return got_error_path(path, GOT_ERR_BAD_PATH);
6440 static const struct got_error *
6441 check_staged_file(void *arg, struct got_fileindex_entry *ie)
6443 int *have_staged_files = arg;
6445 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6446 *have_staged_files = 1;
6447 return got_error(GOT_ERR_CANCELLED);
6453 static const struct got_error *
6454 check_non_staged_files(struct got_fileindex *fileindex,
6455 struct got_pathlist_head *paths)
6457 struct got_pathlist_entry *pe;
6458 struct got_fileindex_entry *ie;
6460 TAILQ_FOREACH(pe, paths, entry) {
6461 if (pe->path[0] == '\0')
6463 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6465 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6466 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6467 return got_error_path(pe->path,
6468 GOT_ERR_FILE_NOT_STAGED);
6474 const struct got_error *
6475 got_worktree_commit(struct got_object_id **new_commit_id,
6476 struct got_worktree *worktree, struct got_pathlist_head *paths,
6477 const char *author, const char *committer, int allow_bad_symlinks,
6478 int show_diff, int commit_conflicts,
6479 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6480 got_worktree_status_cb status_cb, void *status_arg,
6481 struct got_repository *repo)
6483 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6484 struct got_fileindex *fileindex = NULL;
6485 char *fileindex_path = NULL;
6486 struct got_pathlist_head commitable_paths;
6487 struct collect_commitables_arg cc_arg;
6488 struct got_pathlist_entry *pe;
6489 struct got_reference *head_ref = NULL;
6490 struct got_object_id *head_commit_id = NULL;
6491 char *diff_path = NULL;
6492 int have_staged_files = 0;
6494 *new_commit_id = NULL;
6496 memset(&cc_arg, 0, sizeof(cc_arg));
6497 TAILQ_INIT(&commitable_paths);
6499 err = lock_worktree(worktree, LOCK_EX);
6503 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6507 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6511 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6515 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6516 &have_staged_files);
6517 if (err && err->code != GOT_ERR_CANCELLED)
6519 if (have_staged_files) {
6520 err = check_non_staged_files(fileindex, paths);
6525 cc_arg.commitable_paths = &commitable_paths;
6526 cc_arg.worktree = worktree;
6527 cc_arg.fileindex = fileindex;
6529 cc_arg.have_staged_files = have_staged_files;
6530 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6531 cc_arg.diff_header_shown = 0;
6532 cc_arg.commit_conflicts = commit_conflicts;
6534 err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6535 GOT_TMPDIR_STR "/got", ".diff");
6538 cc_arg.f1 = got_opentemp();
6539 if (cc_arg.f1 == NULL) {
6540 err = got_error_from_errno("got_opentemp");
6543 cc_arg.f2 = got_opentemp();
6544 if (cc_arg.f2 == NULL) {
6545 err = got_error_from_errno("got_opentemp");
6550 TAILQ_FOREACH(pe, paths, entry) {
6551 err = worktree_status(worktree, pe->path, fileindex, repo,
6552 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6558 if (fflush(cc_arg.diff_outfile) == EOF) {
6559 err = got_error_from_errno("fflush");
6564 if (TAILQ_EMPTY(&commitable_paths)) {
6565 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6569 TAILQ_FOREACH(pe, paths, entry) {
6570 err = check_path_is_commitable(pe->path, &commitable_paths);
6575 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6576 struct got_commitable *ct = pe->data;
6577 const char *ct_path = ct->in_repo_path;
6579 while (ct_path[0] == '/')
6581 err = check_out_of_date(ct_path, ct->status,
6582 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6583 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6589 err = commit_worktree(new_commit_id, &commitable_paths,
6590 head_commit_id, NULL, worktree, author, committer,
6591 (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6592 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6596 err = update_fileindex_after_commit(worktree, &commitable_paths,
6597 *new_commit_id, fileindex, have_staged_files);
6598 sync_err = sync_fileindex(fileindex, fileindex_path);
6599 if (sync_err && err == NULL)
6603 got_fileindex_free(fileindex);
6604 free(fileindex_path);
6605 unlockerr = lock_worktree(worktree, LOCK_SH);
6606 if (unlockerr && err == NULL)
6608 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6609 struct got_commitable *ct = pe->data;
6611 free_commitable(ct);
6613 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6614 if (diff_path && unlink(diff_path) == -1 && err == NULL)
6615 err = got_error_from_errno2("unlink", diff_path);
6617 if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6619 err = got_error_from_errno("fclose");
6624 got_commitable_get_path(struct got_commitable *ct)
6630 got_commitable_get_status(struct got_commitable *ct)
6635 struct check_rebase_ok_arg {
6636 struct got_worktree *worktree;
6637 struct got_repository *repo;
6640 static const struct got_error *
6641 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6643 const struct got_error *err = NULL;
6644 struct check_rebase_ok_arg *a = arg;
6645 unsigned char status;
6649 /* Reject rebase of a work tree with mixed base commits. */
6650 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6651 SHA1_DIGEST_LENGTH))
6652 return got_error(GOT_ERR_MIXED_COMMITS);
6654 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6656 return got_error_from_errno("asprintf");
6658 /* Reject rebase of a work tree with modified or staged files. */
6659 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6664 if (status != GOT_STATUS_NO_CHANGE)
6665 return got_error(GOT_ERR_MODIFIED);
6666 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6667 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6672 const struct got_error *
6673 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6674 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6675 struct got_worktree *worktree, struct got_reference *branch,
6676 struct got_repository *repo)
6678 const struct got_error *err = NULL;
6679 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6680 char *branch_ref_name = NULL;
6681 char *fileindex_path = NULL;
6682 struct check_rebase_ok_arg ok_arg;
6683 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6684 struct got_object_id *wt_branch_tip = NULL;
6686 *new_base_branch_ref = NULL;
6690 err = lock_worktree(worktree, LOCK_EX);
6694 err = open_fileindex(fileindex, &fileindex_path, worktree);
6698 ok_arg.worktree = worktree;
6700 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6705 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6709 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6713 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6717 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6722 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6725 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6726 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6730 err = got_ref_alloc_symref(new_base_branch_ref,
6731 new_base_branch_ref_name, wt_branch);
6734 err = got_ref_write(*new_base_branch_ref, repo);
6738 /* TODO Lock original branch's ref while rebasing? */
6740 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6744 err = got_ref_write(branch_ref, repo);
6748 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6749 worktree->base_commit_id);
6752 err = got_ref_write(*tmp_branch, repo);
6756 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6760 free(fileindex_path);
6761 free(tmp_branch_name);
6762 free(new_base_branch_ref_name);
6763 free(branch_ref_name);
6765 got_ref_close(branch_ref);
6767 got_ref_close(wt_branch);
6768 free(wt_branch_tip);
6770 if (*new_base_branch_ref) {
6771 got_ref_close(*new_base_branch_ref);
6772 *new_base_branch_ref = NULL;
6775 got_ref_close(*tmp_branch);
6779 got_fileindex_free(*fileindex);
6782 lock_worktree(worktree, LOCK_SH);
6787 const struct got_error *
6788 got_worktree_rebase_continue(struct got_object_id **commit_id,
6789 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6790 struct got_reference **branch, struct got_fileindex **fileindex,
6791 struct got_worktree *worktree, struct got_repository *repo)
6793 const struct got_error *err;
6794 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6795 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6796 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6797 char *fileindex_path = NULL;
6798 int have_staged_files = 0;
6801 *new_base_branch = NULL;
6806 err = lock_worktree(worktree, LOCK_EX);
6810 err = open_fileindex(fileindex, &fileindex_path, worktree);
6814 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6815 &have_staged_files);
6816 if (err && err->code != GOT_ERR_CANCELLED)
6818 if (have_staged_files) {
6819 err = got_error(GOT_ERR_STAGED_PATHS);
6823 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6827 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6831 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6835 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6839 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6843 err = got_ref_open(branch, repo,
6844 got_ref_get_symref_target(branch_ref), 0);
6848 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6852 err = got_ref_resolve(commit_id, repo, commit_ref);
6856 err = got_ref_open(new_base_branch, repo,
6857 new_base_branch_ref_name, 0);
6861 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6865 free(commit_ref_name);
6866 free(branch_ref_name);
6867 free(fileindex_path);
6869 got_ref_close(commit_ref);
6871 got_ref_close(branch_ref);
6876 got_ref_close(*tmp_branch);
6879 if (*new_base_branch) {
6880 got_ref_close(*new_base_branch);
6881 *new_base_branch = NULL;
6884 got_ref_close(*branch);
6888 got_fileindex_free(*fileindex);
6891 lock_worktree(worktree, LOCK_SH);
6896 const struct got_error *
6897 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6899 const struct got_error *err;
6900 char *tmp_branch_name = NULL;
6902 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6906 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6907 free(tmp_branch_name);
6911 static const struct got_error *
6912 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6913 const char *diff_path, char **logmsg, void *arg)
6919 static const struct got_error *
6920 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6921 const char *path, struct got_object_id *blob_id,
6922 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6923 int dirfd, const char *de_name)
6928 struct collect_merged_paths_arg {
6929 got_worktree_checkout_cb progress_cb;
6931 struct got_pathlist_head *merged_paths;
6934 static const struct got_error *
6935 collect_merged_paths(void *arg, unsigned char status, const char *path)
6937 const struct got_error *err;
6938 struct collect_merged_paths_arg *a = arg;
6940 struct got_pathlist_entry *new;
6942 err = (*a->progress_cb)(a->progress_arg, status, path);
6946 if (status != GOT_STATUS_MERGE &&
6947 status != GOT_STATUS_ADD &&
6948 status != GOT_STATUS_DELETE &&
6949 status != GOT_STATUS_CONFLICT)
6954 return got_error_from_errno("strdup");
6956 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6957 if (err || new == NULL)
6962 static const struct got_error *
6963 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6964 int is_rebase, struct got_repository *repo)
6966 const struct got_error *err;
6967 struct got_reference *commit_ref = NULL;
6969 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6971 if (err->code != GOT_ERR_NOT_REF)
6973 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6976 err = got_ref_write(commit_ref, repo);
6979 } else if (is_rebase) {
6980 struct got_object_id *stored_id;
6983 err = got_ref_resolve(&stored_id, repo, commit_ref);
6986 cmp = got_object_id_cmp(commit_id, stored_id);
6989 err = got_error(GOT_ERR_REBASE_COMMITID);
6995 got_ref_close(commit_ref);
6999 static const struct got_error *
7000 rebase_merge_files(struct got_pathlist_head *merged_paths,
7001 const char *commit_ref_name, struct got_worktree *worktree,
7002 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
7003 struct got_object_id *commit_id, struct got_repository *repo,
7004 got_worktree_checkout_cb progress_cb, void *progress_arg,
7005 got_cancel_cb cancel_cb, void *cancel_arg)
7007 const struct got_error *err;
7008 struct got_reference *commit_ref = NULL;
7009 struct collect_merged_paths_arg cmp_arg;
7010 char *fileindex_path;
7012 /* Work tree is locked/unlocked during rebase preparation/teardown. */
7014 err = get_fileindex_path(&fileindex_path, worktree);
7018 cmp_arg.progress_cb = progress_cb;
7019 cmp_arg.progress_arg = progress_arg;
7020 cmp_arg.merged_paths = merged_paths;
7021 err = merge_files(worktree, fileindex, fileindex_path,
7022 parent_commit_id, commit_id, repo, collect_merged_paths,
7023 &cmp_arg, cancel_cb, cancel_arg);
7025 got_ref_close(commit_ref);
7029 const struct got_error *
7030 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
7031 struct got_worktree *worktree, struct got_fileindex *fileindex,
7032 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
7033 struct got_repository *repo,
7034 got_worktree_checkout_cb progress_cb, void *progress_arg,
7035 got_cancel_cb cancel_cb, void *cancel_arg)
7037 const struct got_error *err;
7038 char *commit_ref_name;
7040 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7044 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
7048 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7049 fileindex, parent_commit_id, commit_id, repo, progress_cb,
7050 progress_arg, cancel_cb, cancel_arg);
7052 free(commit_ref_name);
7056 const struct got_error *
7057 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
7058 struct got_worktree *worktree, struct got_fileindex *fileindex,
7059 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
7060 struct got_repository *repo,
7061 got_worktree_checkout_cb progress_cb, void *progress_arg,
7062 got_cancel_cb cancel_cb, void *cancel_arg)
7064 const struct got_error *err;
7065 char *commit_ref_name;
7067 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7071 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7075 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7076 fileindex, parent_commit_id, commit_id, repo, progress_cb,
7077 progress_arg, cancel_cb, cancel_arg);
7079 free(commit_ref_name);
7083 static const struct got_error *
7084 rebase_commit(struct got_object_id **new_commit_id,
7085 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
7086 struct got_worktree *worktree, struct got_fileindex *fileindex,
7087 struct got_reference *tmp_branch, const char *committer,
7088 struct got_commit_object *orig_commit, const char *new_logmsg,
7089 int allow_conflict, struct got_repository *repo)
7091 const struct got_error *err, *sync_err;
7092 struct got_pathlist_head commitable_paths;
7093 struct collect_commitables_arg cc_arg;
7094 char *fileindex_path = NULL;
7095 struct got_reference *head_ref = NULL;
7096 struct got_object_id *head_commit_id = NULL;
7097 char *logmsg = NULL;
7099 memset(&cc_arg, 0, sizeof(cc_arg));
7100 TAILQ_INIT(&commitable_paths);
7101 *new_commit_id = NULL;
7103 /* Work tree is locked/unlocked during rebase preparation/teardown. */
7105 err = get_fileindex_path(&fileindex_path, worktree);
7109 cc_arg.commitable_paths = &commitable_paths;
7110 cc_arg.worktree = worktree;
7112 cc_arg.have_staged_files = 0;
7113 cc_arg.commit_conflicts = allow_conflict;
7115 * If possible get the status of individual files directly to
7116 * avoid crawling the entire work tree once per rebased commit.
7118 * Ideally, merged_paths would contain a list of commitables
7119 * we could use so we could skip worktree_status() entirely.
7120 * However, we would then need carefully keep track of cumulative
7121 * effects of operations such as file additions and deletions
7122 * in 'got histedit -f' (folding multiple commits into one),
7123 * and this extra complexity is not really worth it.
7126 struct got_pathlist_entry *pe;
7127 TAILQ_FOREACH(pe, merged_paths, entry) {
7128 err = worktree_status(worktree, pe->path, fileindex,
7129 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
7135 err = worktree_status(worktree, "", fileindex, repo,
7136 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7141 if (TAILQ_EMPTY(&commitable_paths)) {
7142 /* No-op change; commit will be elided. */
7143 err = got_ref_delete(commit_ref, repo);
7146 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
7150 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7154 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7159 logmsg = strdup(new_logmsg);
7160 if (logmsg == NULL) {
7161 err = got_error_from_errno("strdup");
7165 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
7170 /* NB: commit_worktree will call free(logmsg) */
7171 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
7172 NULL, worktree, got_object_commit_get_author(orig_commit),
7173 committer ? committer :
7174 got_object_commit_get_committer(orig_commit), NULL,
7175 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
7179 err = got_ref_change_ref(tmp_branch, *new_commit_id);
7183 err = got_ref_delete(commit_ref, repo);
7187 err = update_fileindex_after_commit(worktree, &commitable_paths,
7188 *new_commit_id, fileindex, 0);
7189 sync_err = sync_fileindex(fileindex, fileindex_path);
7190 if (sync_err && err == NULL)
7193 free(fileindex_path);
7194 free(head_commit_id);
7196 got_ref_close(head_ref);
7198 free(*new_commit_id);
7199 *new_commit_id = NULL;
7204 const struct got_error *
7205 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
7206 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7207 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7208 const char *committer, struct got_commit_object *orig_commit,
7209 struct got_object_id *orig_commit_id, int allow_conflict,
7210 struct got_repository *repo)
7212 const struct got_error *err;
7213 char *commit_ref_name;
7214 struct got_reference *commit_ref = NULL;
7215 struct got_object_id *commit_id = NULL;
7217 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7221 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7224 err = got_ref_resolve(&commit_id, repo, commit_ref);
7227 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
7228 err = got_error(GOT_ERR_REBASE_COMMITID);
7232 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7233 worktree, fileindex, tmp_branch, committer, orig_commit,
7234 NULL, allow_conflict, repo);
7237 got_ref_close(commit_ref);
7238 free(commit_ref_name);
7243 const struct got_error *
7244 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
7245 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7246 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7247 const char *committer, struct got_commit_object *orig_commit,
7248 struct got_object_id *orig_commit_id, const char *new_logmsg,
7249 int allow_conflict, struct got_repository *repo)
7251 const struct got_error *err;
7252 char *commit_ref_name;
7253 struct got_reference *commit_ref = NULL;
7255 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7259 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7263 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7264 worktree, fileindex, tmp_branch, committer, orig_commit,
7265 new_logmsg, allow_conflict, repo);
7268 got_ref_close(commit_ref);
7269 free(commit_ref_name);
7273 const struct got_error *
7274 got_worktree_rebase_postpone(struct got_worktree *worktree,
7275 struct got_fileindex *fileindex)
7278 got_fileindex_free(fileindex);
7279 return lock_worktree(worktree, LOCK_SH);
7282 static const struct got_error *
7283 delete_ref(const char *name, struct got_repository *repo)
7285 const struct got_error *err;
7286 struct got_reference *ref;
7288 err = got_ref_open(&ref, repo, name, 0);
7290 if (err->code == GOT_ERR_NOT_REF)
7295 err = got_ref_delete(ref, repo);
7300 static const struct got_error *
7301 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7303 const struct got_error *err;
7304 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7305 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7307 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7310 err = delete_ref(tmp_branch_name, repo);
7314 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7317 err = delete_ref(new_base_branch_ref_name, repo);
7321 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7324 err = delete_ref(branch_ref_name, repo);
7328 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7331 err = delete_ref(commit_ref_name, repo);
7336 free(tmp_branch_name);
7337 free(new_base_branch_ref_name);
7338 free(branch_ref_name);
7339 free(commit_ref_name);
7343 static const struct got_error *
7344 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7345 struct got_object_id *new_commit_id, struct got_repository *repo)
7347 const struct got_error *err;
7348 struct got_reference *ref = NULL;
7349 struct got_object_id *old_commit_id = NULL;
7350 const char *branch_name = NULL;
7351 char *new_id_str = NULL;
7352 char *refname = NULL;
7354 branch_name = got_ref_get_name(branch);
7355 if (strncmp(branch_name, "refs/heads/", 11) != 0)
7356 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7359 err = got_object_id_str(&new_id_str, new_commit_id);
7363 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7364 new_id_str) == -1) {
7365 err = got_error_from_errno("asprintf");
7369 err = got_ref_resolve(&old_commit_id, repo, branch);
7373 err = got_ref_alloc(&ref, refname, old_commit_id);
7377 err = got_ref_write(ref, repo);
7381 free(old_commit_id);
7387 const struct got_error *
7388 got_worktree_rebase_complete(struct got_worktree *worktree,
7389 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7390 struct got_reference *rebased_branch, struct got_repository *repo,
7393 const struct got_error *err, *unlockerr, *sync_err;
7394 struct got_object_id *new_head_commit_id = NULL;
7395 char *fileindex_path = NULL;
7397 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7401 if (create_backup) {
7402 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7403 rebased_branch, new_head_commit_id, repo);
7408 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7412 err = got_ref_write(rebased_branch, repo);
7416 err = got_worktree_set_head_ref(worktree, rebased_branch);
7420 err = delete_rebase_refs(worktree, repo);
7424 err = get_fileindex_path(&fileindex_path, worktree);
7427 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7428 sync_err = sync_fileindex(fileindex, fileindex_path);
7429 if (sync_err && err == NULL)
7432 got_fileindex_free(fileindex);
7433 free(fileindex_path);
7434 free(new_head_commit_id);
7435 unlockerr = lock_worktree(worktree, LOCK_SH);
7436 if (unlockerr && err == NULL)
7441 static const struct got_error *
7442 get_paths_changed_between_commits(struct got_pathlist_head *paths,
7443 struct got_object_id *id1, struct got_object_id *id2,
7444 struct got_repository *repo)
7446 const struct got_error *err;
7447 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
7448 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7451 err = got_object_open_as_commit(&commit1, repo, id1);
7455 err = got_object_open_as_tree(&tree1, repo,
7456 got_object_commit_get_tree_id(commit1));
7462 err = got_object_open_as_commit(&commit2, repo, id2);
7466 err = got_object_open_as_tree(&tree2, repo,
7467 got_object_commit_get_tree_id(commit2));
7472 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
7473 got_diff_tree_collect_changed_paths, paths, 0);
7478 got_object_commit_close(commit1);
7480 got_object_commit_close(commit2);
7482 got_object_tree_close(tree1);
7484 got_object_tree_close(tree2);
7488 static const struct got_error *
7489 get_paths_added_between_commits(struct got_pathlist_head *added_paths,
7490 struct got_object_id *id1, struct got_object_id *id2,
7491 const char *path_prefix, struct got_repository *repo)
7493 const struct got_error *err;
7494 struct got_pathlist_head merged_paths;
7495 struct got_pathlist_entry *pe;
7496 char *abspath = NULL, *wt_path = NULL;
7498 TAILQ_INIT(&merged_paths);
7500 err = get_paths_changed_between_commits(&merged_paths, id1, id2, repo);
7504 TAILQ_FOREACH(pe, &merged_paths, entry) {
7505 struct got_diff_changed_path *change = pe->data;
7507 if (change->status != GOT_STATUS_ADD)
7510 if (got_path_is_root_dir(path_prefix)) {
7511 wt_path = strdup(pe->path);
7512 if (wt_path == NULL) {
7513 err = got_error_from_errno("strdup");
7517 if (asprintf(&abspath, "/%s", pe->path) == -1) {
7518 err = got_error_from_errno("asprintf");
7522 err = got_path_skip_common_ancestor(&wt_path,
7523 path_prefix, abspath);
7530 err = got_pathlist_append(added_paths, wt_path, NULL);
7537 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_ALL);
7543 static const struct got_error *
7544 get_paths_added_in_commit(struct got_pathlist_head *added_paths,
7545 struct got_object_id *id, const char *path_prefix,
7546 struct got_repository *repo)
7548 const struct got_error *err;
7549 struct got_commit_object *commit = NULL;
7550 struct got_object_qid *pid;
7552 err = got_object_open_as_commit(&commit, repo, id);
7556 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7558 err = get_paths_added_between_commits(added_paths,
7559 pid ? &pid->id : NULL, id, path_prefix, repo);
7564 got_object_commit_close(commit);
7568 const struct got_error *
7569 got_worktree_rebase_abort(struct got_worktree *worktree,
7570 struct got_fileindex *fileindex, struct got_repository *repo,
7571 struct got_reference *new_base_branch,
7572 got_worktree_checkout_cb progress_cb, void *progress_arg)
7574 const struct got_error *err, *unlockerr, *sync_err;
7575 struct got_reference *resolved = NULL;
7576 struct got_object_id *commit_id = NULL;
7577 struct got_object_id *merged_commit_id = NULL;
7578 struct got_commit_object *commit = NULL;
7579 char *fileindex_path = NULL;
7580 char *commit_ref_name = NULL;
7581 struct got_reference *commit_ref = NULL;
7582 struct revert_file_args rfa;
7583 struct got_object_id *tree_id = NULL;
7584 struct got_pathlist_head added_paths;
7586 TAILQ_INIT(&added_paths);
7588 err = lock_worktree(worktree, LOCK_EX);
7592 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7596 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7600 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
7605 * Determine which files in added status can be safely removed
7606 * from disk while reverting changes in the work tree.
7607 * We want to avoid deleting unrelated files which were added by
7608 * the user for conflict resolution purposes.
7610 err = get_paths_added_in_commit(&added_paths, merged_commit_id,
7611 got_worktree_get_path_prefix(worktree), repo);
7615 err = got_ref_open(&resolved, repo,
7616 got_ref_get_symref_target(new_base_branch), 0);
7620 err = got_worktree_set_head_ref(worktree, resolved);
7625 * XXX commits to the base branch could have happened while
7626 * we were busy rebasing; should we store the original commit ID
7627 * when rebase begins and read it back here?
7629 err = got_ref_resolve(&commit_id, repo, resolved);
7633 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7637 err = got_object_open_as_commit(&commit, repo,
7638 worktree->base_commit_id);
7642 err = got_object_id_by_path(&tree_id, repo, commit,
7643 worktree->path_prefix);
7647 err = delete_rebase_refs(worktree, repo);
7651 err = get_fileindex_path(&fileindex_path, worktree);
7655 rfa.worktree = worktree;
7656 rfa.fileindex = fileindex;
7657 rfa.progress_cb = progress_cb;
7658 rfa.progress_arg = progress_arg;
7659 rfa.patch_cb = NULL;
7660 rfa.patch_arg = NULL;
7662 rfa.unlink_added_files = 1;
7663 rfa.added_files_to_unlink = &added_paths;
7664 err = worktree_status(worktree, "", fileindex, repo,
7665 revert_file, &rfa, NULL, NULL, 1, 0);
7669 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7670 repo, progress_cb, progress_arg, NULL, NULL);
7672 sync_err = sync_fileindex(fileindex, fileindex_path);
7673 if (sync_err && err == NULL)
7676 got_pathlist_free(&added_paths, GOT_PATHLIST_FREE_PATH);
7677 got_ref_close(resolved);
7680 free(merged_commit_id);
7682 got_object_commit_close(commit);
7684 got_fileindex_free(fileindex);
7685 free(fileindex_path);
7686 free(commit_ref_name);
7688 got_ref_close(commit_ref);
7690 unlockerr = lock_worktree(worktree, LOCK_SH);
7691 if (unlockerr && err == NULL)
7696 const struct got_error *
7697 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7698 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7699 struct got_fileindex **fileindex, struct got_worktree *worktree,
7700 struct got_repository *repo)
7702 const struct got_error *err = NULL;
7703 char *tmp_branch_name = NULL;
7704 char *branch_ref_name = NULL;
7705 char *base_commit_ref_name = NULL;
7706 char *fileindex_path = NULL;
7707 struct check_rebase_ok_arg ok_arg;
7708 struct got_reference *wt_branch = NULL;
7709 struct got_reference *base_commit_ref = NULL;
7713 *base_commit_id = NULL;
7716 err = lock_worktree(worktree, LOCK_EX);
7720 err = open_fileindex(fileindex, &fileindex_path, worktree);
7724 ok_arg.worktree = worktree;
7726 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7731 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7735 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7739 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7744 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7749 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7753 err = got_ref_write(*branch_ref, repo);
7757 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7758 worktree->base_commit_id);
7761 err = got_ref_write(base_commit_ref, repo);
7764 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7765 if (*base_commit_id == NULL) {
7766 err = got_error_from_errno("got_object_id_dup");
7770 err = got_ref_alloc(tmp_branch, tmp_branch_name,
7771 worktree->base_commit_id);
7774 err = got_ref_write(*tmp_branch, repo);
7778 err = got_worktree_set_head_ref(worktree, *tmp_branch);
7782 free(fileindex_path);
7783 free(tmp_branch_name);
7784 free(branch_ref_name);
7785 free(base_commit_ref_name);
7787 got_ref_close(wt_branch);
7790 got_ref_close(*branch_ref);
7794 got_ref_close(*tmp_branch);
7797 free(*base_commit_id);
7799 got_fileindex_free(*fileindex);
7802 lock_worktree(worktree, LOCK_SH);
7807 const struct got_error *
7808 got_worktree_histedit_postpone(struct got_worktree *worktree,
7809 struct got_fileindex *fileindex)
7812 got_fileindex_free(fileindex);
7813 return lock_worktree(worktree, LOCK_SH);
7816 const struct got_error *
7817 got_worktree_histedit_in_progress(int *in_progress,
7818 struct got_worktree *worktree)
7820 const struct got_error *err;
7821 char *tmp_branch_name = NULL;
7823 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7827 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7828 free(tmp_branch_name);
7832 const struct got_error *
7833 got_worktree_histedit_continue(struct got_object_id **commit_id,
7834 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7835 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7836 struct got_worktree *worktree, struct got_repository *repo)
7838 const struct got_error *err;
7839 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7840 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7841 struct got_reference *commit_ref = NULL;
7842 struct got_reference *base_commit_ref = NULL;
7843 char *fileindex_path = NULL;
7844 int have_staged_files = 0;
7848 *base_commit_id = NULL;
7851 err = lock_worktree(worktree, LOCK_EX);
7855 err = open_fileindex(fileindex, &fileindex_path, worktree);
7859 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7860 &have_staged_files);
7861 if (err && err->code != GOT_ERR_CANCELLED)
7863 if (have_staged_files) {
7864 err = got_error(GOT_ERR_STAGED_PATHS);
7868 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7872 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7876 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7880 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7885 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7889 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7892 err = got_ref_resolve(commit_id, repo, commit_ref);
7896 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7899 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7903 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7907 free(commit_ref_name);
7908 free(branch_ref_name);
7909 free(fileindex_path);
7911 got_ref_close(commit_ref);
7912 if (base_commit_ref)
7913 got_ref_close(base_commit_ref);
7917 free(*base_commit_id);
7918 *base_commit_id = NULL;
7920 got_ref_close(*tmp_branch);
7924 got_fileindex_free(*fileindex);
7927 lock_worktree(worktree, LOCK_EX);
7932 static const struct got_error *
7933 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7935 const struct got_error *err;
7936 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7937 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7939 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7942 err = delete_ref(tmp_branch_name, repo);
7946 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7950 err = delete_ref(base_commit_ref_name, repo);
7954 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7957 err = delete_ref(branch_ref_name, repo);
7961 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7964 err = delete_ref(commit_ref_name, repo);
7968 free(tmp_branch_name);
7969 free(base_commit_ref_name);
7970 free(branch_ref_name);
7971 free(commit_ref_name);
7975 const struct got_error *
7976 got_worktree_histedit_abort(struct got_worktree *worktree,
7977 struct got_fileindex *fileindex, struct got_repository *repo,
7978 struct got_reference *branch, struct got_object_id *base_commit_id,
7979 got_worktree_checkout_cb progress_cb, void *progress_arg)
7981 const struct got_error *err, *unlockerr, *sync_err;
7982 struct got_reference *resolved = NULL;
7983 char *fileindex_path = NULL;
7984 struct got_object_id *merged_commit_id = NULL;
7985 struct got_commit_object *commit = NULL;
7986 char *commit_ref_name = NULL;
7987 struct got_reference *commit_ref = NULL;
7988 struct got_object_id *tree_id = NULL;
7989 struct revert_file_args rfa;
7990 struct got_pathlist_head added_paths;
7992 TAILQ_INIT(&added_paths);
7994 err = lock_worktree(worktree, LOCK_EX);
7998 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8002 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8004 if (err->code != GOT_ERR_NOT_REF)
8006 /* Can happen on early abort due to invalid histedit script. */
8011 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8016 * Determine which files in added status can be safely removed
8017 * from disk while reverting changes in the work tree.
8018 * We want to avoid deleting unrelated files added by the
8019 * user during conflict resolution or during histedit -e.
8021 err = get_paths_added_in_commit(&added_paths, merged_commit_id,
8022 got_worktree_get_path_prefix(worktree), repo);
8027 err = got_ref_open(&resolved, repo,
8028 got_ref_get_symref_target(branch), 0);
8032 err = got_worktree_set_head_ref(worktree, resolved);
8036 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
8040 err = got_object_open_as_commit(&commit, repo,
8041 worktree->base_commit_id);
8045 err = got_object_id_by_path(&tree_id, repo, commit,
8046 worktree->path_prefix);
8050 err = delete_histedit_refs(worktree, repo);
8054 err = get_fileindex_path(&fileindex_path, worktree);
8058 rfa.worktree = worktree;
8059 rfa.fileindex = fileindex;
8060 rfa.progress_cb = progress_cb;
8061 rfa.progress_arg = progress_arg;
8062 rfa.patch_cb = NULL;
8063 rfa.patch_arg = NULL;
8065 rfa.unlink_added_files = 1;
8066 rfa.added_files_to_unlink = &added_paths;
8067 err = worktree_status(worktree, "", fileindex, repo,
8068 revert_file, &rfa, NULL, NULL, 1, 0);
8072 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8073 repo, progress_cb, progress_arg, NULL, NULL);
8075 sync_err = sync_fileindex(fileindex, fileindex_path);
8076 if (sync_err && err == NULL)
8080 got_ref_close(resolved);
8082 got_ref_close(commit_ref);
8083 free(merged_commit_id);
8085 free(fileindex_path);
8086 free(commit_ref_name);
8088 unlockerr = lock_worktree(worktree, LOCK_SH);
8089 if (unlockerr && err == NULL)
8094 const struct got_error *
8095 got_worktree_histedit_complete(struct got_worktree *worktree,
8096 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8097 struct got_reference *edited_branch, struct got_repository *repo)
8099 const struct got_error *err, *unlockerr, *sync_err;
8100 struct got_object_id *new_head_commit_id = NULL;
8101 struct got_reference *resolved = NULL;
8102 char *fileindex_path = NULL;
8104 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
8108 err = got_ref_open(&resolved, repo,
8109 got_ref_get_symref_target(edited_branch), 0);
8113 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
8114 resolved, new_head_commit_id, repo);
8118 err = got_ref_change_ref(resolved, new_head_commit_id);
8122 err = got_ref_write(resolved, repo);
8126 err = got_worktree_set_head_ref(worktree, resolved);
8130 err = delete_histedit_refs(worktree, repo);
8134 err = get_fileindex_path(&fileindex_path, worktree);
8137 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8138 sync_err = sync_fileindex(fileindex, fileindex_path);
8139 if (sync_err && err == NULL)
8142 got_fileindex_free(fileindex);
8143 free(fileindex_path);
8144 free(new_head_commit_id);
8145 unlockerr = lock_worktree(worktree, LOCK_SH);
8146 if (unlockerr && err == NULL)
8151 const struct got_error *
8152 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
8153 struct got_object_id *commit_id, struct got_repository *repo)
8155 const struct got_error *err;
8156 char *commit_ref_name;
8158 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8162 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
8166 err = delete_ref(commit_ref_name, repo);
8168 free(commit_ref_name);
8172 const struct got_error *
8173 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
8174 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
8175 struct got_worktree *worktree, const char *refname,
8176 struct got_repository *repo)
8178 const struct got_error *err = NULL;
8179 char *fileindex_path = NULL;
8180 struct check_rebase_ok_arg ok_arg;
8184 *base_branch_ref = NULL;
8186 err = lock_worktree(worktree, LOCK_EX);
8190 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
8191 err = got_error_msg(GOT_ERR_SAME_BRANCH,
8192 "cannot integrate a branch into itself; "
8193 "update -b or different branch name required");
8197 err = open_fileindex(fileindex, &fileindex_path, worktree);
8201 /* Preconditions are the same as for rebase. */
8202 ok_arg.worktree = worktree;
8204 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8209 err = got_ref_open(branch_ref, repo, refname, 1);
8213 err = got_ref_open(base_branch_ref, repo,
8214 got_worktree_get_head_ref_name(worktree), 1);
8218 got_ref_close(*branch_ref);
8221 if (*base_branch_ref) {
8222 got_ref_close(*base_branch_ref);
8223 *base_branch_ref = NULL;
8226 got_fileindex_free(*fileindex);
8229 lock_worktree(worktree, LOCK_SH);
8234 const struct got_error *
8235 got_worktree_integrate_continue(struct got_worktree *worktree,
8236 struct got_fileindex *fileindex, struct got_repository *repo,
8237 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
8238 got_worktree_checkout_cb progress_cb, void *progress_arg,
8239 got_cancel_cb cancel_cb, void *cancel_arg)
8241 const struct got_error *err = NULL, *sync_err, *unlockerr;
8242 char *fileindex_path = NULL;
8243 struct got_object_id *tree_id = NULL, *commit_id = NULL;
8244 struct got_commit_object *commit = NULL;
8246 err = get_fileindex_path(&fileindex_path, worktree);
8250 err = got_ref_resolve(&commit_id, repo, branch_ref);
8254 err = got_object_open_as_commit(&commit, repo, commit_id);
8258 err = got_object_id_by_path(&tree_id, repo, commit,
8259 worktree->path_prefix);
8263 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
8267 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
8268 progress_cb, progress_arg, cancel_cb, cancel_arg);
8272 err = got_ref_change_ref(base_branch_ref, commit_id);
8276 err = got_ref_write(base_branch_ref, repo);
8280 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8282 sync_err = sync_fileindex(fileindex, fileindex_path);
8283 if (sync_err && err == NULL)
8287 unlockerr = got_ref_unlock(branch_ref);
8288 if (unlockerr && err == NULL)
8290 got_ref_close(branch_ref);
8292 unlockerr = got_ref_unlock(base_branch_ref);
8293 if (unlockerr && err == NULL)
8295 got_ref_close(base_branch_ref);
8297 got_fileindex_free(fileindex);
8298 free(fileindex_path);
8301 got_object_commit_close(commit);
8303 unlockerr = lock_worktree(worktree, LOCK_SH);
8304 if (unlockerr && err == NULL)
8309 const struct got_error *
8310 got_worktree_integrate_abort(struct got_worktree *worktree,
8311 struct got_fileindex *fileindex, struct got_repository *repo,
8312 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
8314 const struct got_error *err = NULL, *unlockerr = NULL;
8316 got_fileindex_free(fileindex);
8318 err = lock_worktree(worktree, LOCK_SH);
8320 unlockerr = got_ref_unlock(branch_ref);
8321 if (unlockerr && err == NULL)
8323 got_ref_close(branch_ref);
8325 unlockerr = got_ref_unlock(base_branch_ref);
8326 if (unlockerr && err == NULL)
8328 got_ref_close(base_branch_ref);
8333 const struct got_error *
8334 got_worktree_merge_postpone(struct got_worktree *worktree,
8335 struct got_fileindex *fileindex)
8337 const struct got_error *err, *sync_err;
8338 char *fileindex_path = NULL;
8340 err = get_fileindex_path(&fileindex_path, worktree);
8344 sync_err = sync_fileindex(fileindex, fileindex_path);
8346 err = lock_worktree(worktree, LOCK_SH);
8347 if (sync_err && err == NULL)
8350 got_fileindex_free(fileindex);
8351 free(fileindex_path);
8355 static const struct got_error *
8356 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
8358 const struct got_error *err;
8359 char *branch_refname = NULL, *commit_refname = NULL;
8361 err = get_merge_branch_ref_name(&branch_refname, worktree);
8364 err = delete_ref(branch_refname, repo);
8368 err = get_merge_commit_ref_name(&commit_refname, worktree);
8371 err = delete_ref(commit_refname, repo);
8376 free(branch_refname);
8377 free(commit_refname);
8381 struct merge_commit_msg_arg {
8382 struct got_worktree *worktree;
8383 const char *branch_name;
8386 static const struct got_error *
8387 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
8388 const char *diff_path, char **logmsg, void *arg)
8390 struct merge_commit_msg_arg *a = arg;
8392 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
8393 got_worktree_get_head_ref_name(a->worktree)) == -1)
8394 return got_error_from_errno("asprintf");
8400 const struct got_error *
8401 got_worktree_merge_branch(struct got_worktree *worktree,
8402 struct got_fileindex *fileindex,
8403 struct got_object_id *yca_commit_id,
8404 struct got_object_id *branch_tip,
8405 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
8406 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
8408 const struct got_error *err;
8409 char *fileindex_path = NULL;
8411 err = get_fileindex_path(&fileindex_path, worktree);
8415 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
8420 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
8421 branch_tip, repo, progress_cb, progress_arg,
8422 cancel_cb, cancel_arg);
8424 free(fileindex_path);
8428 const struct got_error *
8429 got_worktree_merge_commit(struct got_object_id **new_commit_id,
8430 struct got_worktree *worktree, struct got_fileindex *fileindex,
8431 const char *author, const char *committer, int allow_bad_symlinks,
8432 struct got_object_id *branch_tip, const char *branch_name,
8433 int allow_conflict, struct got_repository *repo,
8434 got_worktree_status_cb status_cb, void *status_arg)
8437 const struct got_error *err = NULL, *sync_err;
8438 struct got_pathlist_head commitable_paths;
8439 struct collect_commitables_arg cc_arg;
8440 struct got_pathlist_entry *pe;
8441 struct got_reference *head_ref = NULL;
8442 struct got_object_id *head_commit_id = NULL;
8443 int have_staged_files = 0;
8444 struct merge_commit_msg_arg mcm_arg;
8445 char *fileindex_path = NULL;
8447 memset(&cc_arg, 0, sizeof(cc_arg));
8448 *new_commit_id = NULL;
8450 TAILQ_INIT(&commitable_paths);
8452 err = get_fileindex_path(&fileindex_path, worktree);
8456 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
8460 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8464 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
8465 &have_staged_files);
8466 if (err && err->code != GOT_ERR_CANCELLED)
8468 if (have_staged_files) {
8469 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
8473 cc_arg.commitable_paths = &commitable_paths;
8474 cc_arg.worktree = worktree;
8475 cc_arg.fileindex = fileindex;
8477 cc_arg.have_staged_files = have_staged_files;
8478 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
8479 cc_arg.commit_conflicts = allow_conflict;
8480 err = worktree_status(worktree, "", fileindex, repo,
8481 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8485 mcm_arg.worktree = worktree;
8486 mcm_arg.branch_name = branch_name;
8487 err = commit_worktree(new_commit_id, &commitable_paths,
8488 head_commit_id, branch_tip, worktree, author, committer, NULL,
8489 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8493 err = update_fileindex_after_commit(worktree, &commitable_paths,
8494 *new_commit_id, fileindex, have_staged_files);
8495 sync_err = sync_fileindex(fileindex, fileindex_path);
8496 if (sync_err && err == NULL)
8499 TAILQ_FOREACH(pe, &commitable_paths, entry) {
8500 struct got_commitable *ct = pe->data;
8502 free_commitable(ct);
8504 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8505 free(fileindex_path);
8509 const struct got_error *
8510 got_worktree_merge_complete(struct got_worktree *worktree,
8511 struct got_fileindex *fileindex, struct got_repository *repo)
8513 const struct got_error *err, *unlockerr, *sync_err;
8514 char *fileindex_path = NULL;
8516 err = delete_merge_refs(worktree, repo);
8520 err = get_fileindex_path(&fileindex_path, worktree);
8523 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8524 sync_err = sync_fileindex(fileindex, fileindex_path);
8525 if (sync_err && err == NULL)
8528 got_fileindex_free(fileindex);
8529 free(fileindex_path);
8530 unlockerr = lock_worktree(worktree, LOCK_SH);
8531 if (unlockerr && err == NULL)
8536 const struct got_error *
8537 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8538 struct got_repository *repo)
8540 const struct got_error *err;
8541 char *branch_refname = NULL;
8542 struct got_reference *branch_ref = NULL;
8546 err = get_merge_branch_ref_name(&branch_refname, worktree);
8549 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8550 free(branch_refname);
8552 if (err->code != GOT_ERR_NOT_REF)
8560 const struct got_error *got_worktree_merge_prepare(
8561 struct got_fileindex **fileindex, struct got_worktree *worktree,
8562 struct got_repository *repo)
8564 const struct got_error *err = NULL;
8565 char *fileindex_path = NULL;
8566 struct got_reference *wt_branch = NULL;
8567 struct got_object_id *wt_branch_tip = NULL;
8568 struct check_rebase_ok_arg ok_arg;
8572 err = lock_worktree(worktree, LOCK_EX);
8576 err = open_fileindex(fileindex, &fileindex_path, worktree);
8580 /* Preconditions are the same as for rebase. */
8581 ok_arg.worktree = worktree;
8583 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8588 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8593 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8597 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8598 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8603 free(fileindex_path);
8605 got_ref_close(wt_branch);
8606 free(wt_branch_tip);
8609 got_fileindex_free(*fileindex);
8612 lock_worktree(worktree, LOCK_SH);
8617 const struct got_error *got_worktree_merge_write_refs(
8618 struct got_worktree *worktree, struct got_reference *branch,
8619 struct got_repository *repo)
8621 const struct got_error *err = NULL;
8622 char *branch_refname = NULL, *commit_refname = NULL;
8623 struct got_reference *branch_ref = NULL, *commit_ref = NULL;
8624 struct got_object_id *branch_tip = NULL;
8626 err = get_merge_branch_ref_name(&branch_refname, worktree);
8630 err = get_merge_commit_ref_name(&commit_refname, worktree);
8634 err = got_ref_resolve(&branch_tip, repo, branch);
8638 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8641 err = got_ref_write(branch_ref, repo);
8645 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8648 err = got_ref_write(commit_ref, repo);
8653 free(branch_refname);
8654 free(commit_refname);
8656 got_ref_close(branch_ref);
8658 got_ref_close(commit_ref);
8663 const struct got_error *
8664 got_worktree_merge_continue(char **branch_name,
8665 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8666 struct got_worktree *worktree, struct got_repository *repo)
8668 const struct got_error *err;
8669 char *commit_refname = NULL, *branch_refname = NULL;
8670 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8671 char *fileindex_path = NULL;
8672 int have_staged_files = 0;
8674 *branch_name = NULL;
8678 err = lock_worktree(worktree, LOCK_EX);
8682 err = open_fileindex(fileindex, &fileindex_path, worktree);
8686 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8687 &have_staged_files);
8688 if (err && err->code != GOT_ERR_CANCELLED)
8690 if (have_staged_files) {
8691 err = got_error(GOT_ERR_STAGED_PATHS);
8695 err = get_merge_branch_ref_name(&branch_refname, worktree);
8699 err = get_merge_commit_ref_name(&commit_refname, worktree);
8703 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8707 if (!got_ref_is_symbolic(branch_ref)) {
8708 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8709 "%s is not a symbolic reference",
8710 got_ref_get_name(branch_ref));
8713 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8714 if (*branch_name == NULL) {
8715 err = got_error_from_errno("strdup");
8719 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8723 err = got_ref_resolve(branch_tip, repo, commit_ref);
8727 free(commit_refname);
8728 free(branch_refname);
8729 free(fileindex_path);
8731 got_ref_close(commit_ref);
8733 got_ref_close(branch_ref);
8737 *branch_name = NULL;
8742 got_fileindex_free(*fileindex);
8745 lock_worktree(worktree, LOCK_SH);
8750 const struct got_error *
8751 got_worktree_merge_abort(struct got_worktree *worktree,
8752 struct got_fileindex *fileindex, struct got_repository *repo,
8753 got_worktree_checkout_cb progress_cb, void *progress_arg)
8755 const struct got_error *err, *unlockerr, *sync_err;
8756 struct got_commit_object *commit = NULL;
8757 char *fileindex_path = NULL;
8758 struct revert_file_args rfa;
8759 char *commit_ref_name = NULL;
8760 struct got_reference *commit_ref = NULL;
8761 struct got_object_id *merged_commit_id = NULL;
8762 struct got_object_id *tree_id = NULL;
8763 struct got_pathlist_head added_paths;
8765 TAILQ_INIT(&added_paths);
8767 err = get_merge_commit_ref_name(&commit_ref_name, worktree);
8771 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8775 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8780 * Determine which files in added status can be safely removed
8781 * from disk while reverting changes in the work tree.
8782 * We want to avoid deleting unrelated files which were added by
8783 * the user for conflict resolution purposes.
8785 err = get_paths_added_between_commits(&added_paths,
8786 got_worktree_get_base_commit_id(worktree), merged_commit_id,
8787 got_worktree_get_path_prefix(worktree), repo);
8792 err = got_object_open_as_commit(&commit, repo,
8793 worktree->base_commit_id);
8797 err = got_object_id_by_path(&tree_id, repo, commit,
8798 worktree->path_prefix);
8802 err = delete_merge_refs(worktree, repo);
8806 err = get_fileindex_path(&fileindex_path, worktree);
8810 rfa.worktree = worktree;
8811 rfa.fileindex = fileindex;
8812 rfa.progress_cb = progress_cb;
8813 rfa.progress_arg = progress_arg;
8814 rfa.patch_cb = NULL;
8815 rfa.patch_arg = NULL;
8817 rfa.unlink_added_files = 1;
8818 rfa.added_files_to_unlink = &added_paths;
8819 err = worktree_status(worktree, "", fileindex, repo,
8820 revert_file, &rfa, NULL, NULL, 1, 0);
8824 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8825 repo, progress_cb, progress_arg, NULL, NULL);
8827 sync_err = sync_fileindex(fileindex, fileindex_path);
8828 if (sync_err && err == NULL)
8832 free(merged_commit_id);
8834 got_object_commit_close(commit);
8836 got_fileindex_free(fileindex);
8837 free(fileindex_path);
8839 got_ref_close(commit_ref);
8840 free(commit_ref_name);
8842 unlockerr = lock_worktree(worktree, LOCK_SH);
8843 if (unlockerr && err == NULL)
8848 struct check_stage_ok_arg {
8849 struct got_object_id *head_commit_id;
8850 struct got_worktree *worktree;
8851 struct got_fileindex *fileindex;
8852 struct got_repository *repo;
8856 static const struct got_error *
8857 check_stage_ok(void *arg, unsigned char status,
8858 unsigned char staged_status, const char *relpath,
8859 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8860 struct got_object_id *commit_id, int dirfd, const char *de_name)
8862 struct check_stage_ok_arg *a = arg;
8863 const struct got_error *err = NULL;
8864 struct got_fileindex_entry *ie;
8865 struct got_object_id base_commit_id;
8866 struct got_object_id *base_commit_idp = NULL;
8867 char *in_repo_path = NULL, *p;
8869 if (status == GOT_STATUS_UNVERSIONED ||
8870 status == GOT_STATUS_NO_CHANGE)
8872 if (status == GOT_STATUS_NONEXISTENT)
8873 return got_error_set_errno(ENOENT, relpath);
8875 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8877 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8879 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8880 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8882 return got_error_from_errno("asprintf");
8884 if (got_fileindex_entry_has_commit(ie)) {
8885 base_commit_idp = got_fileindex_entry_get_commit_id(
8886 &base_commit_id, ie);
8889 if (status == GOT_STATUS_CONFLICT) {
8890 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8892 } else if (status != GOT_STATUS_ADD &&
8893 status != GOT_STATUS_MODIFY &&
8894 status != GOT_STATUS_DELETE) {
8895 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8899 a->have_changes = 1;
8904 err = check_out_of_date(p, status, staged_status,
8905 blob_id, base_commit_idp, a->head_commit_id, a->repo,
8906 GOT_ERR_STAGE_OUT_OF_DATE);
8912 struct stage_path_arg {
8913 struct got_worktree *worktree;
8914 struct got_fileindex *fileindex;
8915 struct got_repository *repo;
8916 got_worktree_status_cb status_cb;
8918 got_worktree_patch_cb patch_cb;
8920 int staged_something;
8921 int allow_bad_symlinks;
8924 static const struct got_error *
8925 stage_path(void *arg, unsigned char status,
8926 unsigned char staged_status, const char *relpath,
8927 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8928 struct got_object_id *commit_id, int dirfd, const char *de_name)
8930 struct stage_path_arg *a = arg;
8931 const struct got_error *err = NULL;
8932 struct got_fileindex_entry *ie;
8933 char *ondisk_path = NULL, *path_content = NULL;
8935 struct got_object_id *new_staged_blob_id = NULL;
8938 if (status == GOT_STATUS_UNVERSIONED)
8941 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8943 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8945 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8947 return got_error_from_errno("asprintf");
8950 case GOT_STATUS_ADD:
8951 case GOT_STATUS_MODIFY:
8952 /* XXX could sb.st_mode be passed in by our caller? */
8953 if (lstat(ondisk_path, &sb) == -1) {
8954 err = got_error_from_errno2("lstat", ondisk_path);
8958 if (status == GOT_STATUS_ADD) {
8959 int choice = GOT_PATCH_CHOICE_NONE;
8960 err = (*a->patch_cb)(&choice, a->patch_arg,
8961 status, ie->path, NULL, 1, 1);
8964 if (choice != GOT_PATCH_CHOICE_YES)
8967 err = create_patched_content(&path_content, 0,
8968 staged_blob_id ? staged_blob_id : blob_id,
8969 ondisk_path, dirfd, de_name, ie->path,
8970 a->repo, a->patch_cb, a->patch_arg);
8971 if (err || path_content == NULL)
8975 err = got_object_blob_create(&new_staged_blob_id,
8976 path_content ? path_content : ondisk_path, a->repo);
8979 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8980 SHA1_DIGEST_LENGTH);
8981 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8982 stage = GOT_FILEIDX_STAGE_ADD;
8984 stage = GOT_FILEIDX_STAGE_MODIFY;
8985 got_fileindex_entry_stage_set(ie, stage);
8986 if (S_ISLNK(sb.st_mode)) {
8987 int is_bad_symlink = 0;
8988 if (!a->allow_bad_symlinks) {
8989 char target_path[PATH_MAX];
8991 target_len = readlink(ondisk_path, target_path,
8992 sizeof(target_path));
8993 if (target_len == -1) {
8994 err = got_error_from_errno2("readlink",
8998 err = is_bad_symlink_target(&is_bad_symlink,
8999 target_path, target_len, ondisk_path,
9000 a->worktree->root_path,
9001 a->worktree->meta_dir);
9004 if (is_bad_symlink) {
9005 err = got_error_path(ondisk_path,
9006 GOT_ERR_BAD_SYMLINK);
9011 got_fileindex_entry_staged_filetype_set(ie,
9012 GOT_FILEIDX_MODE_BAD_SYMLINK);
9014 got_fileindex_entry_staged_filetype_set(ie,
9015 GOT_FILEIDX_MODE_SYMLINK);
9017 got_fileindex_entry_staged_filetype_set(ie,
9018 GOT_FILEIDX_MODE_REGULAR_FILE);
9020 a->staged_something = 1;
9021 if (a->status_cb == NULL)
9023 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
9024 get_staged_status(ie), relpath, blob_id,
9025 new_staged_blob_id, NULL, dirfd, de_name);
9029 * When staging the reverse of the staged diff,
9030 * implicitly unstage the file.
9032 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
9033 sizeof(ie->blob_sha1)) == 0) {
9034 got_fileindex_entry_stage_set(ie,
9035 GOT_FILEIDX_STAGE_NONE);
9038 case GOT_STATUS_DELETE:
9039 if (staged_status == GOT_STATUS_DELETE)
9042 int choice = GOT_PATCH_CHOICE_NONE;
9043 err = (*a->patch_cb)(&choice, a->patch_arg, status,
9044 ie->path, NULL, 1, 1);
9047 if (choice == GOT_PATCH_CHOICE_NO)
9049 if (choice != GOT_PATCH_CHOICE_YES) {
9050 err = got_error(GOT_ERR_PATCH_CHOICE);
9054 stage = GOT_FILEIDX_STAGE_DELETE;
9055 got_fileindex_entry_stage_set(ie, stage);
9056 a->staged_something = 1;
9057 if (a->status_cb == NULL)
9059 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
9060 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
9063 case GOT_STATUS_NO_CHANGE:
9065 case GOT_STATUS_CONFLICT:
9066 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
9068 case GOT_STATUS_NONEXISTENT:
9069 err = got_error_set_errno(ENOENT, relpath);
9072 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
9076 if (path_content && unlink(path_content) == -1 && err == NULL)
9077 err = got_error_from_errno2("unlink", path_content);
9080 free(new_staged_blob_id);
9084 const struct got_error *
9085 got_worktree_stage(struct got_worktree *worktree,
9086 struct got_pathlist_head *paths,
9087 got_worktree_status_cb status_cb, void *status_arg,
9088 got_worktree_patch_cb patch_cb, void *patch_arg,
9089 int allow_bad_symlinks, struct got_repository *repo)
9091 const struct got_error *err = NULL, *sync_err, *unlockerr;
9092 struct got_pathlist_entry *pe;
9093 struct got_fileindex *fileindex = NULL;
9094 char *fileindex_path = NULL;
9095 struct got_reference *head_ref = NULL;
9096 struct got_object_id *head_commit_id = NULL;
9097 struct check_stage_ok_arg oka;
9098 struct stage_path_arg spa;
9100 err = lock_worktree(worktree, LOCK_EX);
9104 err = got_ref_open(&head_ref, repo,
9105 got_worktree_get_head_ref_name(worktree), 0);
9108 err = got_ref_resolve(&head_commit_id, repo, head_ref);
9111 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9115 /* Check pre-conditions before staging anything. */
9116 oka.head_commit_id = head_commit_id;
9117 oka.worktree = worktree;
9118 oka.fileindex = fileindex;
9120 oka.have_changes = 0;
9121 TAILQ_FOREACH(pe, paths, entry) {
9122 err = worktree_status(worktree, pe->path, fileindex, repo,
9123 check_stage_ok, &oka, NULL, NULL, 1, 0);
9127 if (!oka.have_changes) {
9128 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9132 spa.worktree = worktree;
9133 spa.fileindex = fileindex;
9135 spa.patch_cb = patch_cb;
9136 spa.patch_arg = patch_arg;
9137 spa.status_cb = status_cb;
9138 spa.status_arg = status_arg;
9139 spa.staged_something = 0;
9140 spa.allow_bad_symlinks = allow_bad_symlinks;
9141 TAILQ_FOREACH(pe, paths, entry) {
9142 err = worktree_status(worktree, pe->path, fileindex, repo,
9143 stage_path, &spa, NULL, NULL, 1, 0);
9147 if (!spa.staged_something) {
9148 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9152 sync_err = sync_fileindex(fileindex, fileindex_path);
9153 if (sync_err && err == NULL)
9157 got_ref_close(head_ref);
9158 free(head_commit_id);
9159 free(fileindex_path);
9161 got_fileindex_free(fileindex);
9162 unlockerr = lock_worktree(worktree, LOCK_SH);
9163 if (unlockerr && err == NULL)
9168 struct unstage_path_arg {
9169 struct got_worktree *worktree;
9170 struct got_fileindex *fileindex;
9171 struct got_repository *repo;
9172 got_worktree_checkout_cb progress_cb;
9174 got_worktree_patch_cb patch_cb;
9178 static const struct got_error *
9179 create_unstaged_content(char **path_unstaged_content,
9180 char **path_new_staged_content, struct got_object_id *blob_id,
9181 struct got_object_id *staged_blob_id, const char *relpath,
9182 struct got_repository *repo,
9183 got_worktree_patch_cb patch_cb, void *patch_arg)
9185 const struct got_error *err, *free_err;
9186 struct got_blob_object *blob = NULL, *staged_blob = NULL;
9187 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
9188 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
9189 struct got_diffreg_result *diffreg_result = NULL;
9190 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
9191 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
9192 int fd1 = -1, fd2 = -1;
9194 *path_unstaged_content = NULL;
9195 *path_new_staged_content = NULL;
9197 err = got_object_id_str(&label1, blob_id);
9201 fd1 = got_opentempfd();
9203 err = got_error_from_errno("got_opentempfd");
9206 fd2 = got_opentempfd();
9208 err = got_error_from_errno("got_opentempfd");
9212 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
9216 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
9220 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
9224 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
9229 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
9233 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
9237 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
9238 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
9242 err = got_opentemp_named(path_unstaged_content, &outfile,
9243 "got-unstaged-content", "");
9246 err = got_opentemp_named(path_new_staged_content, &rejectfile,
9247 "got-new-staged-content", "");
9251 if (fseek(f1, 0L, SEEK_SET) == -1) {
9252 err = got_ferror(f1, GOT_ERR_IO);
9255 if (fseek(f2, 0L, SEEK_SET) == -1) {
9256 err = got_ferror(f2, GOT_ERR_IO);
9259 /* Count the number of actual changes in the diff result. */
9260 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9261 struct diff_chunk_context cc = {};
9262 diff_chunk_context_load_change(&cc, &nchunks_used,
9263 diffreg_result->result, n, 0);
9266 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9268 err = apply_or_reject_change(&choice, &nchunks_used,
9269 diffreg_result->result, n, relpath, f1, f2,
9270 &line_cur1, &line_cur2,
9271 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
9274 if (choice == GOT_PATCH_CHOICE_YES)
9277 have_rejected_content = 1;
9278 if (choice == GOT_PATCH_CHOICE_QUIT)
9281 if (have_content || have_rejected_content)
9282 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
9283 outfile, rejectfile);
9286 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9287 err = got_error_from_errno("close");
9289 got_object_blob_close(blob);
9290 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9291 err = got_error_from_errno("close");
9293 got_object_blob_close(staged_blob);
9294 free_err = got_diffreg_result_free(diffreg_result);
9295 if (free_err && err == NULL)
9297 if (f1 && fclose(f1) == EOF && err == NULL)
9298 err = got_error_from_errno2("fclose", path1);
9299 if (f2 && fclose(f2) == EOF && err == NULL)
9300 err = got_error_from_errno2("fclose", path2);
9301 if (outfile && fclose(outfile) == EOF && err == NULL)
9302 err = got_error_from_errno2("fclose", *path_unstaged_content);
9303 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
9304 err = got_error_from_errno2("fclose", *path_new_staged_content);
9305 if (path1 && unlink(path1) == -1 && err == NULL)
9306 err = got_error_from_errno2("unlink", path1);
9307 if (path2 && unlink(path2) == -1 && err == NULL)
9308 err = got_error_from_errno2("unlink", path2);
9309 if (err || !have_content) {
9310 if (*path_unstaged_content &&
9311 unlink(*path_unstaged_content) == -1 && err == NULL)
9312 err = got_error_from_errno2("unlink",
9313 *path_unstaged_content);
9314 free(*path_unstaged_content);
9315 *path_unstaged_content = NULL;
9317 if (err || !have_content || !have_rejected_content) {
9318 if (*path_new_staged_content &&
9319 unlink(*path_new_staged_content) == -1 && err == NULL)
9320 err = got_error_from_errno2("unlink",
9321 *path_new_staged_content);
9322 free(*path_new_staged_content);
9323 *path_new_staged_content = NULL;
9330 static const struct got_error *
9331 unstage_hunks(struct got_object_id *staged_blob_id,
9332 struct got_blob_object *blob_base,
9333 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
9334 const char *ondisk_path, const char *label_orig,
9335 struct got_worktree *worktree, struct got_repository *repo,
9336 got_worktree_patch_cb patch_cb, void *patch_arg,
9337 got_worktree_checkout_cb progress_cb, void *progress_arg)
9339 const struct got_error *err = NULL;
9340 char *path_unstaged_content = NULL;
9341 char *path_new_staged_content = NULL;
9342 char *parent = NULL, *base_path = NULL;
9343 char *blob_base_path = NULL;
9344 struct got_object_id *new_staged_blob_id = NULL;
9345 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
9348 err = create_unstaged_content(&path_unstaged_content,
9349 &path_new_staged_content, blob_id, staged_blob_id,
9350 ie->path, repo, patch_cb, patch_arg);
9354 if (path_unstaged_content == NULL)
9357 if (path_new_staged_content) {
9358 err = got_object_blob_create(&new_staged_blob_id,
9359 path_new_staged_content, repo);
9364 f = fopen(path_unstaged_content, "re");
9366 err = got_error_from_errno2("fopen",
9367 path_unstaged_content);
9370 if (fstat(fileno(f), &sb) == -1) {
9371 err = got_error_from_errno2("fstat", path_unstaged_content);
9374 if (got_fileindex_entry_staged_filetype_get(ie) ==
9375 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
9376 char link_target[PATH_MAX];
9378 r = fread(link_target, 1, sizeof(link_target), f);
9379 if (r == 0 && ferror(f)) {
9380 err = got_error_from_errno("fread");
9383 if (r >= sizeof(link_target)) { /* should not happen */
9384 err = got_error(GOT_ERR_NO_SPACE);
9387 link_target[r] = '\0';
9388 err = merge_symlink(worktree, blob_base,
9389 ondisk_path, ie->path, label_orig, link_target,
9390 worktree->base_commit_id, repo, progress_cb,
9393 int local_changes_subsumed;
9395 err = got_path_dirname(&parent, ondisk_path);
9399 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
9401 err = got_error_from_errno("asprintf");
9406 err = got_opentemp_named(&blob_base_path, &f_base,
9410 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
9416 * In order the run a 3-way merge with a symlink we copy the symlink's
9417 * target path into a temporary file and use that file with diff3.
9419 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9420 err = dump_symlink_target_path_to_file(&f_deriv2,
9426 fd = open(ondisk_path,
9427 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
9429 err = got_error_from_errno2("open", ondisk_path);
9432 f_deriv2 = fdopen(fd, "r");
9433 if (f_deriv2 == NULL) {
9434 err = got_error_from_errno2("fdopen", ondisk_path);
9440 err = merge_file(&local_changes_subsumed, worktree,
9441 f_base, f, f_deriv2, ondisk_path, ie->path,
9442 got_fileindex_perms_to_st(ie),
9443 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
9444 repo, progress_cb, progress_arg);
9449 if (new_staged_blob_id) {
9450 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9451 SHA1_DIGEST_LENGTH);
9453 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9454 got_fileindex_entry_staged_filetype_set(ie, 0);
9457 free(new_staged_blob_id);
9458 if (path_unstaged_content &&
9459 unlink(path_unstaged_content) == -1 && err == NULL)
9460 err = got_error_from_errno2("unlink", path_unstaged_content);
9461 if (path_new_staged_content &&
9462 unlink(path_new_staged_content) == -1 && err == NULL)
9463 err = got_error_from_errno2("unlink", path_new_staged_content);
9464 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
9465 err = got_error_from_errno2("unlink", blob_base_path);
9466 if (f_base && fclose(f_base) == EOF && err == NULL)
9467 err = got_error_from_errno2("fclose", path_unstaged_content);
9468 if (f && fclose(f) == EOF && err == NULL)
9469 err = got_error_from_errno2("fclose", path_unstaged_content);
9470 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
9471 err = got_error_from_errno2("fclose", ondisk_path);
9472 free(path_unstaged_content);
9473 free(path_new_staged_content);
9474 free(blob_base_path);
9480 static const struct got_error *
9481 unstage_path(void *arg, unsigned char status,
9482 unsigned char staged_status, const char *relpath,
9483 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9484 struct got_object_id *commit_id, int dirfd, const char *de_name)
9486 const struct got_error *err = NULL;
9487 struct unstage_path_arg *a = arg;
9488 struct got_fileindex_entry *ie;
9489 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
9490 char *ondisk_path = NULL;
9491 char *id_str = NULL, *label_orig = NULL;
9492 int local_changes_subsumed;
9494 int fd1 = -1, fd2 = -1;
9496 if (staged_status != GOT_STATUS_ADD &&
9497 staged_status != GOT_STATUS_MODIFY &&
9498 staged_status != GOT_STATUS_DELETE)
9501 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9503 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9505 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
9507 return got_error_from_errno("asprintf");
9509 err = got_object_id_str(&id_str,
9510 commit_id ? commit_id : a->worktree->base_commit_id);
9513 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9515 err = got_error_from_errno("asprintf");
9519 fd1 = got_opentempfd();
9521 err = got_error_from_errno("got_opentempfd");
9524 fd2 = got_opentempfd();
9526 err = got_error_from_errno("got_opentempfd");
9530 switch (staged_status) {
9531 case GOT_STATUS_MODIFY:
9532 err = got_object_open_as_blob(&blob_base, a->repo,
9533 blob_id, 8192, fd1);
9537 case GOT_STATUS_ADD:
9539 if (staged_status == GOT_STATUS_ADD) {
9540 int choice = GOT_PATCH_CHOICE_NONE;
9541 err = (*a->patch_cb)(&choice, a->patch_arg,
9542 staged_status, ie->path, NULL, 1, 1);
9545 if (choice != GOT_PATCH_CHOICE_YES)
9548 err = unstage_hunks(staged_blob_id,
9549 blob_base, blob_id, ie, ondisk_path,
9550 label_orig, a->worktree, a->repo,
9551 a->patch_cb, a->patch_arg,
9552 a->progress_cb, a->progress_arg);
9553 break; /* Done with this file. */
9556 err = got_object_open_as_blob(&blob_staged, a->repo,
9557 staged_blob_id, 8192, fd2);
9560 switch (got_fileindex_entry_staged_filetype_get(ie)) {
9561 case GOT_FILEIDX_MODE_BAD_SYMLINK:
9562 case GOT_FILEIDX_MODE_REGULAR_FILE:
9563 err = merge_blob(&local_changes_subsumed, a->worktree,
9564 blob_base, ondisk_path, relpath,
9565 got_fileindex_perms_to_st(ie), label_orig,
9566 blob_staged, commit_id ? commit_id :
9567 a->worktree->base_commit_id, a->repo,
9568 a->progress_cb, a->progress_arg);
9570 case GOT_FILEIDX_MODE_SYMLINK:
9571 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9572 char *staged_target;
9573 err = got_object_blob_read_to_str(
9574 &staged_target, blob_staged);
9577 err = merge_symlink(a->worktree, blob_base,
9578 ondisk_path, relpath, label_orig,
9579 staged_target, commit_id ? commit_id :
9580 a->worktree->base_commit_id,
9581 a->repo, a->progress_cb, a->progress_arg);
9582 free(staged_target);
9584 err = merge_blob(&local_changes_subsumed,
9585 a->worktree, blob_base, ondisk_path,
9586 relpath, got_fileindex_perms_to_st(ie),
9587 label_orig, blob_staged,
9588 commit_id ? commit_id :
9589 a->worktree->base_commit_id, a->repo,
9590 a->progress_cb, a->progress_arg);
9594 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9598 got_fileindex_entry_stage_set(ie,
9599 GOT_FILEIDX_STAGE_NONE);
9600 got_fileindex_entry_staged_filetype_set(ie, 0);
9603 case GOT_STATUS_DELETE:
9605 int choice = GOT_PATCH_CHOICE_NONE;
9606 err = (*a->patch_cb)(&choice, a->patch_arg,
9607 staged_status, ie->path, NULL, 1, 1);
9610 if (choice == GOT_PATCH_CHOICE_NO)
9612 if (choice != GOT_PATCH_CHOICE_YES) {
9613 err = got_error(GOT_ERR_PATCH_CHOICE);
9617 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9618 got_fileindex_entry_staged_filetype_set(ie, 0);
9619 err = get_file_status(&status, &sb, ie, ondisk_path,
9620 dirfd, de_name, a->repo);
9623 err = (*a->progress_cb)(a->progress_arg, status, relpath);
9628 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9629 err = got_error_from_errno("close");
9631 got_object_blob_close(blob_base);
9632 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9633 err = got_error_from_errno("close");
9635 got_object_blob_close(blob_staged);
9641 const struct got_error *
9642 got_worktree_unstage(struct got_worktree *worktree,
9643 struct got_pathlist_head *paths,
9644 got_worktree_checkout_cb progress_cb, void *progress_arg,
9645 got_worktree_patch_cb patch_cb, void *patch_arg,
9646 struct got_repository *repo)
9648 const struct got_error *err = NULL, *sync_err, *unlockerr;
9649 struct got_pathlist_entry *pe;
9650 struct got_fileindex *fileindex = NULL;
9651 char *fileindex_path = NULL;
9652 struct unstage_path_arg upa;
9654 err = lock_worktree(worktree, LOCK_EX);
9658 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9662 upa.worktree = worktree;
9663 upa.fileindex = fileindex;
9665 upa.progress_cb = progress_cb;
9666 upa.progress_arg = progress_arg;
9667 upa.patch_cb = patch_cb;
9668 upa.patch_arg = patch_arg;
9669 TAILQ_FOREACH(pe, paths, entry) {
9670 err = worktree_status(worktree, pe->path, fileindex, repo,
9671 unstage_path, &upa, NULL, NULL, 1, 0);
9676 sync_err = sync_fileindex(fileindex, fileindex_path);
9677 if (sync_err && err == NULL)
9680 free(fileindex_path);
9682 got_fileindex_free(fileindex);
9683 unlockerr = lock_worktree(worktree, LOCK_SH);
9684 if (unlockerr && err == NULL)
9689 struct report_file_info_arg {
9690 struct got_worktree *worktree;
9691 got_worktree_path_info_cb info_cb;
9693 struct got_pathlist_head *paths;
9694 got_cancel_cb cancel_cb;
9698 static const struct got_error *
9699 report_file_info(void *arg, struct got_fileindex_entry *ie)
9701 struct report_file_info_arg *a = arg;
9702 struct got_pathlist_entry *pe;
9703 struct got_object_id blob_id, staged_blob_id, commit_id;
9704 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9705 struct got_object_id *commit_idp = NULL;
9708 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9709 return got_error(GOT_ERR_CANCELLED);
9711 TAILQ_FOREACH(pe, a->paths, entry) {
9712 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9713 got_path_is_child(ie->path, pe->path, pe->path_len))
9716 if (pe == NULL) /* not found */
9719 if (got_fileindex_entry_has_blob(ie))
9720 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9721 stage = got_fileindex_entry_stage_get(ie);
9722 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9723 stage == GOT_FILEIDX_STAGE_ADD) {
9724 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9725 &staged_blob_id, ie);
9728 if (got_fileindex_entry_has_commit(ie))
9729 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9731 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9732 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9735 const struct got_error *
9736 got_worktree_path_info(struct got_worktree *worktree,
9737 struct got_pathlist_head *paths,
9738 got_worktree_path_info_cb info_cb, void *info_arg,
9739 got_cancel_cb cancel_cb, void *cancel_arg)
9742 const struct got_error *err = NULL, *unlockerr;
9743 struct got_fileindex *fileindex = NULL;
9744 char *fileindex_path = NULL;
9745 struct report_file_info_arg arg;
9747 err = lock_worktree(worktree, LOCK_SH);
9751 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9755 arg.worktree = worktree;
9756 arg.info_cb = info_cb;
9757 arg.info_arg = info_arg;
9759 arg.cancel_cb = cancel_cb;
9760 arg.cancel_arg = cancel_arg;
9761 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9764 free(fileindex_path);
9766 got_fileindex_free(fileindex);
9767 unlockerr = lock_worktree(worktree, LOCK_UN);
9768 if (unlockerr && err == NULL)
9773 static const struct got_error *
9774 patch_check_path(const char *p, char **path, unsigned char *status,
9775 unsigned char *staged_status, struct got_fileindex *fileindex,
9776 struct got_worktree *worktree, struct got_repository *repo)
9778 const struct got_error *err;
9779 struct got_fileindex_entry *ie;
9781 char *ondisk_path = NULL;
9783 err = got_worktree_resolve_path(path, worktree, p);
9787 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9788 *path[0] ? "/" : "", *path) == -1)
9789 return got_error_from_errno("asprintf");
9791 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9793 *staged_status = get_staged_status(ie);
9794 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9799 *staged_status = GOT_STATUS_NO_CHANGE;
9800 *status = GOT_STATUS_UNVERSIONED;
9801 if (lstat(ondisk_path, &sb) == -1) {
9802 if (errno != ENOENT) {
9803 err = got_error_from_errno2("lstat",
9807 *status = GOT_STATUS_NONEXISTENT;
9816 static const struct got_error *
9817 patch_can_rm(const char *path, unsigned char status,
9818 unsigned char staged_status)
9820 if (status == GOT_STATUS_NONEXISTENT)
9821 return got_error_set_errno(ENOENT, path);
9822 if (status != GOT_STATUS_NO_CHANGE &&
9823 status != GOT_STATUS_ADD &&
9824 status != GOT_STATUS_MODIFY &&
9825 status != GOT_STATUS_MODE_CHANGE)
9826 return got_error_path(path, GOT_ERR_FILE_STATUS);
9827 if (staged_status == GOT_STATUS_DELETE)
9828 return got_error_path(path, GOT_ERR_FILE_STATUS);
9832 static const struct got_error *
9833 patch_can_add(const char *path, unsigned char status)
9835 if (status != GOT_STATUS_NONEXISTENT)
9836 return got_error_path(path, GOT_ERR_FILE_STATUS);
9840 static const struct got_error *
9841 patch_can_edit(const char *path, unsigned char status,
9842 unsigned char staged_status)
9844 if (status == GOT_STATUS_NONEXISTENT)
9845 return got_error_set_errno(ENOENT, path);
9846 if (status != GOT_STATUS_NO_CHANGE &&
9847 status != GOT_STATUS_ADD &&
9848 status != GOT_STATUS_MODIFY)
9849 return got_error_path(path, GOT_ERR_FILE_STATUS);
9850 if (staged_status == GOT_STATUS_DELETE)
9851 return got_error_path(path, GOT_ERR_FILE_STATUS);
9855 const struct got_error *
9856 got_worktree_patch_prepare(struct got_fileindex **fileindex,
9857 char **fileindex_path, struct got_worktree *worktree)
9859 return open_fileindex(fileindex, fileindex_path, worktree);
9862 const struct got_error *
9863 got_worktree_patch_check_path(const char *old, const char *new,
9864 char **oldpath, char **newpath, struct got_worktree *worktree,
9865 struct got_repository *repo, struct got_fileindex *fileindex)
9867 const struct got_error *err = NULL;
9868 int file_renamed = 0;
9869 unsigned char status_old, staged_status_old;
9870 unsigned char status_new, staged_status_new;
9875 err = patch_check_path(old != NULL ? old : new, oldpath,
9876 &status_old, &staged_status_old, fileindex, worktree, repo);
9880 err = patch_check_path(new != NULL ? new : old, newpath,
9881 &status_new, &staged_status_new, fileindex, worktree, repo);
9885 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9888 if (old != NULL && new == NULL)
9889 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9890 else if (file_renamed) {
9891 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9893 err = patch_can_add(*newpath, status_new);
9894 } else if (old == NULL)
9895 err = patch_can_add(*newpath, status_new);
9897 err = patch_can_edit(*newpath, status_new, staged_status_new);
9909 const struct got_error *
9910 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9911 struct got_worktree *worktree, struct got_fileindex *fileindex,
9912 got_worktree_checkout_cb progress_cb, void *progress_arg)
9914 struct schedule_addition_args saa;
9916 memset(&saa, 0, sizeof(saa));
9917 saa.worktree = worktree;
9918 saa.fileindex = fileindex;
9919 saa.progress_cb = progress_cb;
9920 saa.progress_arg = progress_arg;
9923 return worktree_status(worktree, path, fileindex, repo,
9924 schedule_addition, &saa, NULL, NULL, 1, 0);
9927 const struct got_error *
9928 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9929 struct got_worktree *worktree, struct got_fileindex *fileindex,
9930 got_worktree_delete_cb progress_cb, void *progress_arg)
9932 const struct got_error *err;
9933 struct schedule_deletion_args sda;
9934 char *ondisk_status_path;
9936 memset(&sda, 0, sizeof(sda));
9937 sda.worktree = worktree;
9938 sda.fileindex = fileindex;
9939 sda.progress_cb = progress_cb;
9940 sda.progress_arg = progress_arg;
9942 sda.delete_local_mods = 0;
9943 sda.keep_on_disk = 0;
9944 sda.ignore_missing_paths = 0;
9945 sda.status_codes = NULL;
9946 if (asprintf(&ondisk_status_path, "%s/%s",
9947 got_worktree_get_root_path(worktree), path) == -1)
9948 return got_error_from_errno("asprintf");
9949 sda.status_path = ondisk_status_path;
9950 sda.status_path_len = strlen(ondisk_status_path);
9952 err = worktree_status(worktree, path, fileindex, repo,
9953 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9954 free(ondisk_status_path);
9958 const struct got_error *
9959 got_worktree_patch_complete(struct got_fileindex *fileindex,
9960 const char *fileindex_path)
9962 const struct got_error *err = NULL;
9964 err = sync_fileindex(fileindex, fileindex_path);
9965 got_fileindex_free(fileindex);