2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/queue.h>
38 #include "got_error.h"
39 #include "got_repository.h"
40 #include "got_reference.h"
41 #include "got_object.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
45 #include "got_opentemp.h"
48 #include "got_lib_worktree.h"
49 #include "got_lib_sha1.h"
50 #include "got_lib_fileindex.h"
51 #include "got_lib_inflate.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_object_idset.h"
57 #include "got_lib_diff.h"
58 #include "got_lib_gotconfig.h"
61 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
64 #define GOT_MERGE_LABEL_MERGED "merged change"
65 #define GOT_MERGE_LABEL_BASE "3-way merge base"
67 static mode_t apply_umask(mode_t);
69 static const struct got_error *
70 create_meta_file(const char *path_got, const char *name, const char *content)
72 const struct got_error *err = NULL;
75 if (asprintf(&path, "%s/%s", path_got, name) == -1)
76 return got_error_from_errno("asprintf");
78 err = got_path_create_file(path, content);
83 static const struct got_error *
84 update_meta_file(const char *path_got, const char *name, const char *content)
86 const struct got_error *err = NULL;
91 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
92 err = got_error_from_errno("asprintf");
97 err = got_opentemp_named(&tmppath, &tmpfile, path, "");
102 int len = fprintf(tmpfile, "%s\n", content);
103 if (len != strlen(content) + 1) {
104 err = got_error_from_errno2("fprintf", tmppath);
109 if (rename(tmppath, path) != 0) {
110 err = got_error_from_errno3("rename", tmppath, path);
116 if (fclose(tmpfile) == EOF && err == NULL)
117 err = got_error_from_errno2("fclose", tmppath);
122 static const struct got_error *
123 write_head_ref(const char *path_got, struct got_reference *head_ref)
125 const struct got_error *err = NULL;
128 if (got_ref_is_symbolic(head_ref)) {
129 refstr = got_ref_to_str(head_ref);
131 return got_error_from_errno("got_ref_to_str");
133 refstr = strdup(got_ref_get_name(head_ref));
135 return got_error_from_errno("strdup");
137 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
142 const struct got_error *
143 got_worktree_init(const char *path, struct got_reference *head_ref,
144 const char *prefix, struct got_repository *repo)
146 const struct got_error *err = NULL;
147 struct got_object_id *commit_id = NULL;
149 uint32_t uuid_status;
151 char *path_got = NULL;
152 char *formatstr = NULL;
153 char *absprefix = NULL;
154 char *basestr = NULL;
155 char *uuidstr = NULL;
157 if (strcmp(path, got_repo_get_path(repo)) == 0) {
158 err = got_error(GOT_ERR_WORKTREE_REPO);
162 err = got_ref_resolve(&commit_id, repo, head_ref);
165 err = got_object_get_type(&obj_type, repo, commit_id);
168 if (obj_type != GOT_OBJ_TYPE_COMMIT)
169 return got_error(GOT_ERR_OBJ_TYPE);
171 if (!got_path_is_absolute(prefix)) {
172 if (asprintf(&absprefix, "/%s", prefix) == -1)
173 return got_error_from_errno("asprintf");
176 /* Create top-level directory (may already exist). */
177 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
178 err = got_error_from_errno2("mkdir", path);
182 /* Create .got directory (may already exist). */
183 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
184 err = got_error_from_errno("asprintf");
187 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
188 err = got_error_from_errno2("mkdir", path_got);
192 /* Create an empty lock file. */
193 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
197 /* Create an empty file index. */
198 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
202 /* Write the HEAD reference. */
203 err = write_head_ref(path_got, head_ref);
207 /* Record our base commit. */
208 err = got_object_id_str(&basestr, commit_id);
211 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
215 /* Store path to repository. */
216 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
217 got_repo_get_path(repo));
221 /* Store in-repository path prefix. */
222 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
223 absprefix ? absprefix : prefix);
228 uuid_create(&uuid, &uuid_status);
229 if (uuid_status != uuid_s_ok) {
230 err = got_error_uuid(uuid_status, "uuid_create");
233 uuid_to_string(&uuid, &uuidstr, &uuid_status);
234 if (uuid_status != uuid_s_ok) {
235 err = got_error_uuid(uuid_status, "uuid_to_string");
238 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
242 /* Stamp work tree with format file. */
243 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
244 err = got_error_from_errno("asprintf");
247 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
261 const struct got_error *
262 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
263 const char *path_prefix)
265 char *absprefix = NULL;
267 if (!got_path_is_absolute(path_prefix)) {
268 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
269 return got_error_from_errno("asprintf");
271 *match = (strcmp(absprefix ? absprefix : path_prefix,
272 worktree->path_prefix) == 0);
278 got_worktree_get_head_ref_name(struct got_worktree *worktree)
280 return worktree->head_ref_name;
283 const struct got_error *
284 got_worktree_set_head_ref(struct got_worktree *worktree,
285 struct got_reference *head_ref)
287 const struct got_error *err = NULL;
288 char *path_got = NULL, *head_ref_name = NULL;
290 if (asprintf(&path_got, "%s/%s", worktree->root_path,
291 GOT_WORKTREE_GOT_DIR) == -1) {
292 err = got_error_from_errno("asprintf");
297 head_ref_name = strdup(got_ref_get_name(head_ref));
298 if (head_ref_name == NULL) {
299 err = got_error_from_errno("strdup");
303 err = write_head_ref(path_got, head_ref);
307 free(worktree->head_ref_name);
308 worktree->head_ref_name = head_ref_name;
316 struct got_object_id *
317 got_worktree_get_base_commit_id(struct got_worktree *worktree)
319 return worktree->base_commit_id;
322 const struct got_error *
323 got_worktree_set_base_commit_id(struct got_worktree *worktree,
324 struct got_repository *repo, struct got_object_id *commit_id)
326 const struct got_error *err;
327 struct got_object *obj = NULL;
329 char *path_got = NULL;
331 if (asprintf(&path_got, "%s/%s", worktree->root_path,
332 GOT_WORKTREE_GOT_DIR) == -1) {
333 err = got_error_from_errno("asprintf");
338 err = got_object_open(&obj, repo, commit_id);
342 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
343 err = got_error(GOT_ERR_OBJ_TYPE);
347 /* Record our base commit. */
348 err = got_object_id_str(&id_str, commit_id);
351 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
355 free(worktree->base_commit_id);
356 worktree->base_commit_id = got_object_id_dup(commit_id);
357 if (worktree->base_commit_id == NULL) {
358 err = got_error_from_errno("got_object_id_dup");
363 got_object_close(obj);
369 const struct got_gotconfig *
370 got_worktree_get_gotconfig(struct got_worktree *worktree)
372 return worktree->gotconfig;
375 static const struct got_error *
376 lock_worktree(struct got_worktree *worktree, int operation)
378 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
379 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
380 : got_error_from_errno2("flock",
381 got_worktree_get_root_path(worktree)));
385 static const struct got_error *
386 add_dir_on_disk(struct got_worktree *worktree, const char *path)
388 const struct got_error *err = NULL;
391 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
392 return got_error_from_errno("asprintf");
394 err = got_path_mkdir(abspath);
395 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
398 if (lstat(abspath, &sb) == -1) {
399 err = got_error_from_errno2("lstat", abspath);
400 } else if (!S_ISDIR(sb.st_mode)) {
401 /* TODO directory is obstructed; do something */
402 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
409 static const struct got_error *
410 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
412 const struct got_error *err = NULL;
415 size_t flen1 = 0, flen2 = 0;
420 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
421 if (flen1 == 0 && ferror(f1)) {
422 err = got_error_from_errno("fread");
425 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
426 if (flen2 == 0 && ferror(f2)) {
427 err = got_error_from_errno("fread");
434 } else if (flen2 == 0) {
438 } else if (flen1 == flen2) {
439 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
452 static const struct got_error *
453 check_files_equal(int *same, FILE *f1, FILE *f2)
460 if (fstat(fileno(f1), &sb) != 0)
461 return got_error_from_errno("fstat");
464 if (fstat(fileno(f2), &sb) != 0)
465 return got_error_from_errno("fstat");
468 if (size1 != size2) {
473 if (fseek(f1, 0L, SEEK_SET) == -1)
474 return got_ferror(f1, GOT_ERR_IO);
475 if (fseek(f2, 0L, SEEK_SET) == -1)
476 return got_ferror(f2, GOT_ERR_IO);
478 return check_file_contents_equal(same, f1, f2);
481 static const struct got_error *
482 copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
490 if (fseek(f, 0L, SEEK_SET) == -1)
491 return got_ferror(f, GOT_ERR_IO);
494 flen = fread(fbuf, 1, sizeof(fbuf), f);
497 return got_error_from_errno("fread");
501 outlen = write(outfd, fbuf, flen);
503 return got_error_from_errno("write");
505 return got_error(GOT_ERR_IO);
512 static const struct got_error *
513 merge_binary_file(int *overlapcnt, int merged_fd,
514 FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
515 const char *label_deriv, const char *label_orig, const char *label_deriv2,
516 const char *ondisk_path)
518 const struct got_error *err = NULL;
519 int same_content, changed_deriv, changed_deriv2;
520 int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
521 off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
522 char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
523 char *base_path_orig = NULL, *base_path_deriv = NULL;
524 char *base_path_deriv2 = NULL;
528 err = check_files_equal(&same_content, f_deriv, f_deriv2);
533 return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
535 err = check_files_equal(&same_content, f_deriv, f_orig);
538 changed_deriv = !same_content;
539 err = check_files_equal(&same_content, f_deriv2, f_orig);
542 changed_deriv2 = !same_content;
544 if (changed_deriv && changed_deriv2) {
546 if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
547 err = got_error_from_errno("asprintf");
550 if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
551 err = got_error_from_errno("asprintf");
554 if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
555 err = got_error_from_errno("asprintf");
558 err = got_opentemp_named_fd(&path_orig, &fd_orig,
562 err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
563 base_path_deriv, "");
566 err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
567 base_path_deriv2, "");
570 err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
573 err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
576 err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
579 if (dprintf(merged_fd, "Binary files differ and cannot be "
580 "merged automatically:\n") < 0) {
581 err = got_error_from_errno("dprintf");
584 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
585 GOT_DIFF_CONFLICT_MARKER_BEGIN,
586 label_deriv ? " " : "",
587 label_deriv ? label_deriv : "",
589 err = got_error_from_errno("dprintf");
593 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
594 GOT_DIFF_CONFLICT_MARKER_ORIG,
595 label_orig ? " " : "",
596 label_orig ? label_orig : "",
598 err = got_error_from_errno("dprintf");
602 if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
603 GOT_DIFF_CONFLICT_MARKER_SEP,
605 GOT_DIFF_CONFLICT_MARKER_END,
606 label_deriv2 ? " " : "",
607 label_deriv2 ? label_deriv2 : "") < 0) {
608 err = got_error_from_errno("dprintf");
611 } else if (changed_deriv)
612 err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
613 else if (changed_deriv2)
614 err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
616 if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
618 err = got_error_from_errno2("unlink", path_orig);
619 if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
620 err = got_error_from_errno2("close", path_orig);
621 if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
622 err = got_error_from_errno2("close", path_deriv);
623 if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
624 err = got_error_from_errno2("close", path_deriv2);
628 free(base_path_orig);
629 free(base_path_deriv);
630 free(base_path_deriv2);
635 * Perform a 3-way merge where the file f_orig acts as the common
636 * ancestor, the file f_deriv acts as the first derived version,
637 * and the file f_deriv2 acts as the second derived version.
638 * The merge result will be written to a new file at ondisk_path; any
639 * existing file at this path will be replaced.
641 static const struct got_error *
642 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
643 FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
644 const char *path, uint16_t st_mode,
645 const char *label_orig, const char *label_deriv, const char *label_deriv2,
646 enum got_diff_algorithm diff_algo, struct got_repository *repo,
647 got_worktree_checkout_cb progress_cb, void *progress_arg)
649 const struct got_error *err = NULL;
651 FILE *f_merged = NULL;
652 char *merged_path = NULL, *base_path = NULL;
656 *local_changes_subsumed = 0;
658 err = got_path_dirname(&parent, ondisk_path);
662 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
663 err = got_error_from_errno("asprintf");
667 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path, "");
671 err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
672 f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
674 if (err->code != GOT_ERR_FILE_BINARY)
676 err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
677 f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
683 err = (*progress_cb)(progress_arg,
684 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
688 if (fsync(merged_fd) != 0) {
689 err = got_error_from_errno("fsync");
693 f_merged = fdopen(merged_fd, "r");
694 if (f_merged == NULL) {
695 err = got_error_from_errno("fdopen");
700 /* Check if a clean merge has subsumed all local changes. */
701 if (overlapcnt == 0) {
702 err = check_files_equal(local_changes_subsumed, f_deriv,
708 if (fchmod(fileno(f_merged), apply_umask(st_mode)) != 0) {
709 err = got_error_from_errno2("fchmod", merged_path);
713 if (rename(merged_path, ondisk_path) != 0) {
714 err = got_error_from_errno3("rename", merged_path,
723 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
724 err = got_error_from_errno("close");
725 if (f_merged && fclose(f_merged) == EOF && err == NULL)
726 err = got_error_from_errno("fclose");
733 static const struct got_error *
734 update_symlink(const char *ondisk_path, const char *target_path,
737 /* This is not atomic but matches what 'ln -sf' does. */
738 if (unlink(ondisk_path) == -1)
739 return got_error_from_errno2("unlink", ondisk_path);
740 if (symlink(target_path, ondisk_path) == -1)
741 return got_error_from_errno3("symlink", target_path,
747 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
748 * in the work tree with a file that contains conflict markers and the
749 * conflicting target paths of the original version, a "derived version"
750 * of a symlink from an incoming change, and a local version of the symlink.
752 * The original versions's target path can be NULL if it is not available,
753 * such as if both derived versions added a new symlink at the same path.
755 * The incoming derived symlink target is NULL in case the incoming change
756 * has deleted this symlink.
758 static const struct got_error *
759 install_symlink_conflict(const char *deriv_target,
760 struct got_object_id *deriv_base_commit_id, const char *orig_target,
761 const char *label_orig, const char *local_target, const char *ondisk_path)
763 const struct got_error *err;
764 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
767 err = got_object_id_str(&id_str, deriv_base_commit_id);
769 return got_error_from_errno("asprintf");
771 if (asprintf(&label_deriv, "%s: commit %s",
772 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
773 err = got_error_from_errno("asprintf");
777 err = got_opentemp_named(&path, &f, "got-symlink-conflict", "");
781 if (fchmod(fileno(f), apply_umask(GOT_DEFAULT_FILE_MODE)) == -1) {
782 err = got_error_from_errno2("fchmod", path);
786 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
787 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
788 deriv_target ? deriv_target : "(symlink was deleted)",
789 orig_target ? label_orig : "",
790 orig_target ? "\n" : "",
791 orig_target ? orig_target : "",
792 orig_target ? "\n" : "",
793 GOT_DIFF_CONFLICT_MARKER_SEP,
794 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
795 err = got_error_from_errno2("fprintf", path);
799 if (unlink(ondisk_path) == -1) {
800 err = got_error_from_errno2("unlink", ondisk_path);
803 if (rename(path, ondisk_path) == -1) {
804 err = got_error_from_errno3("rename", path, ondisk_path);
808 if (f != NULL && fclose(f) == EOF && err == NULL)
809 err = got_error_from_errno2("fclose", path);
816 /* forward declaration */
817 static const struct got_error *
818 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
819 const char *, const char *, uint16_t, const char *,
820 struct got_blob_object *, struct got_object_id *,
821 struct got_repository *, got_worktree_checkout_cb, void *);
824 * Merge a symlink into the work tree, where blob_orig acts as the common
825 * ancestor, deriv_target is the link target of the first derived version,
826 * and the symlink on disk acts as the second derived version.
827 * Assume that contents of both blobs represent symlinks.
829 static const struct got_error *
830 merge_symlink(struct got_worktree *worktree,
831 struct got_blob_object *blob_orig, const char *ondisk_path,
832 const char *path, const char *label_orig, const char *deriv_target,
833 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
834 got_worktree_checkout_cb progress_cb, void *progress_arg)
836 const struct got_error *err = NULL;
837 char *ancestor_target = NULL;
839 ssize_t ondisk_len, deriv_len;
840 char ondisk_target[PATH_MAX];
841 int have_local_change = 0;
842 int have_incoming_change = 0;
844 if (lstat(ondisk_path, &sb) == -1)
845 return got_error_from_errno2("lstat", ondisk_path);
847 ondisk_len = readlink(ondisk_path, ondisk_target,
848 sizeof(ondisk_target));
849 if (ondisk_len == -1) {
850 err = got_error_from_errno2("readlink",
854 ondisk_target[ondisk_len] = '\0';
857 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
862 if (ancestor_target == NULL ||
863 (ondisk_len != strlen(ancestor_target) ||
864 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
865 have_local_change = 1;
867 deriv_len = strlen(deriv_target);
868 if (ancestor_target == NULL ||
869 (deriv_len != strlen(ancestor_target) ||
870 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
871 have_incoming_change = 1;
873 if (!have_local_change && !have_incoming_change) {
874 if (ancestor_target) {
875 /* Both sides made the same change. */
876 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
878 } else if (deriv_len == ondisk_len &&
879 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
880 /* Both sides added the same symlink. */
881 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
884 /* Both sides added symlinks which don't match. */
885 err = install_symlink_conflict(deriv_target,
886 deriv_base_commit_id, ancestor_target,
887 label_orig, ondisk_target, ondisk_path);
890 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
893 } else if (!have_local_change && have_incoming_change) {
894 /* Apply the incoming change. */
895 err = update_symlink(ondisk_path, deriv_target,
896 strlen(deriv_target));
899 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
900 } else if (have_local_change && have_incoming_change) {
901 if (deriv_len == ondisk_len &&
902 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
903 /* Both sides made the same change. */
904 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
907 err = install_symlink_conflict(deriv_target,
908 deriv_base_commit_id, ancestor_target, label_orig,
909 ondisk_target, ondisk_path);
912 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
918 free(ancestor_target);
922 static const struct got_error *
923 dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
925 const struct got_error *err = NULL;
926 char target_path[PATH_MAX];
935 return got_error_from_errno("got_opentemp");
936 target_len = readlink(ondisk_path, target_path, sizeof(target_path));
937 if (target_len == -1) {
938 err = got_error_from_errno2("readlink", ondisk_path);
941 n = fwrite(target_path, 1, target_len, f);
942 if (n != target_len) {
943 err = got_ferror(f, GOT_ERR_IO);
946 if (fflush(f) == EOF) {
947 err = got_error_from_errno("fflush");
950 if (fseek(f, 0L, SEEK_SET) == -1) {
951 err = got_ferror(f, GOT_ERR_IO);
963 * Perform a 3-way merge where blob_orig acts as the common ancestor,
964 * blob_deriv acts as the first derived version, and the file on disk
965 * acts as the second derived version.
967 static const struct got_error *
968 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
969 struct got_blob_object *blob_orig, const char *ondisk_path,
970 const char *path, uint16_t st_mode, const char *label_orig,
971 struct got_blob_object *blob_deriv,
972 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
973 got_worktree_checkout_cb progress_cb, void *progress_arg)
975 const struct got_error *err = NULL;
976 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
977 char *blob_orig_path = NULL;
978 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
979 char *label_deriv = NULL, *parent = NULL;
981 *local_changes_subsumed = 0;
983 err = got_path_dirname(&parent, ondisk_path);
988 if (asprintf(&base_path, "%s/got-merge-blob-orig",
990 err = got_error_from_errno("asprintf");
995 err = got_opentemp_named(&blob_orig_path, &f_orig,
999 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1006 * No common ancestor exists. This is an "add vs add" conflict
1007 * and we simply use an empty ancestor file to make both files
1008 * appear in the merged result in their entirety.
1010 f_orig = got_opentemp();
1011 if (f_orig == NULL) {
1012 err = got_error_from_errno("got_opentemp");
1017 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1018 err = got_error_from_errno("asprintf");
1023 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path, "");
1026 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1031 err = got_object_id_str(&id_str, deriv_base_commit_id);
1034 if (asprintf(&label_deriv, "%s: commit %s",
1035 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1036 err = got_error_from_errno("asprintf");
1041 * In order the run a 3-way merge with a symlink we copy the symlink's
1042 * target path into a temporary file and use that file with diff3.
1044 if (S_ISLNK(st_mode)) {
1045 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1050 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1052 err = got_error_from_errno2("open", ondisk_path);
1055 f_deriv2 = fdopen(fd, "r");
1056 if (f_deriv2 == NULL) {
1057 err = got_error_from_errno2("fdopen", ondisk_path);
1063 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1064 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1065 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1067 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1068 err = got_error_from_errno("fclose");
1069 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1070 err = got_error_from_errno("fclose");
1071 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1072 err = got_error_from_errno("fclose");
1074 if (blob_orig_path) {
1075 unlink(blob_orig_path);
1076 free(blob_orig_path);
1078 if (blob_deriv_path) {
1079 unlink(blob_deriv_path);
1080 free(blob_deriv_path);
1088 static const struct got_error *
1089 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1090 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1091 int wt_fd, const char *path, struct got_object_id *blob_id)
1093 const struct got_error *err = NULL;
1094 struct got_fileindex_entry *new_ie;
1098 err = got_fileindex_entry_alloc(&new_ie, path);
1102 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1103 blob_id->sha1, base_commit_id->sha1, 1);
1107 err = got_fileindex_entry_add(fileindex, new_ie);
1110 got_fileindex_entry_free(new_ie);
1117 get_ondisk_perms(int executable, mode_t st_mode)
1119 mode_t xbits = S_IXUSR;
1122 /* Map read bits to execute bits. */
1123 if (st_mode & S_IRGRP)
1125 if (st_mode & S_IROTH)
1127 return st_mode | xbits;
1134 apply_umask(mode_t mode)
1143 /* forward declaration */
1144 static const struct got_error *
1145 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1146 const char *path, mode_t te_mode, mode_t st_mode,
1147 struct got_blob_object *blob, int restoring_missing_file,
1148 int reverting_versioned_file, int installing_bad_symlink,
1149 int path_is_unversioned, struct got_repository *repo,
1150 got_worktree_checkout_cb progress_cb, void *progress_arg);
1153 * This function assumes that the provided symlink target points at a
1154 * safe location in the work tree!
1156 static const struct got_error *
1157 replace_existing_symlink(int *did_something, const char *ondisk_path,
1158 const char *target_path, size_t target_len)
1160 const struct got_error *err = NULL;
1162 char etarget[PATH_MAX];
1168 * "Bad" symlinks (those pointing outside the work tree or into the
1169 * .got directory) are installed in the work tree as a regular file
1170 * which contains the bad symlink target path.
1171 * The new symlink target has already been checked for safety by our
1172 * caller. If we can successfully open a regular file then we simply
1173 * replace this file with a symlink below.
1175 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1177 if (!got_err_open_nofollow_on_symlink())
1178 return got_error_from_errno2("open", ondisk_path);
1180 /* We are updating an existing on-disk symlink. */
1181 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1183 return got_error_from_errno2("readlink", ondisk_path);
1185 if (elen == target_len &&
1186 memcmp(etarget, target_path, target_len) == 0)
1187 return NULL; /* nothing to do */
1191 err = update_symlink(ondisk_path, target_path, target_len);
1192 if (fd != -1 && close(fd) == -1 && err == NULL)
1193 err = got_error_from_errno2("close", ondisk_path);
1197 static const struct got_error *
1198 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1199 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1201 const struct got_error *err = NULL;
1202 char canonpath[PATH_MAX];
1203 char *path_got = NULL;
1205 *is_bad_symlink = 0;
1207 if (target_len >= sizeof(canonpath)) {
1208 *is_bad_symlink = 1;
1213 * We do not use realpath(3) to resolve the symlink's target
1214 * path because we don't want to resolve symlinks recursively.
1215 * Instead we make the path absolute and then canonicalize it.
1216 * Relative symlink target lookup should begin at the directory
1217 * in which the blob object is being installed.
1219 if (!got_path_is_absolute(target_path)) {
1220 char *abspath, *parent;
1221 err = got_path_dirname(&parent, ondisk_path);
1224 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1226 return got_error_from_errno("asprintf");
1229 if (strlen(abspath) >= sizeof(canonpath)) {
1230 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1234 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1239 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1244 /* Only allow symlinks pointing at paths within the work tree. */
1245 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1246 *is_bad_symlink = 1;
1250 /* Do not allow symlinks pointing into the .got directory. */
1251 if (asprintf(&path_got, "%s/%s", wtroot_path,
1252 GOT_WORKTREE_GOT_DIR) == -1)
1253 return got_error_from_errno("asprintf");
1254 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1255 *is_bad_symlink = 1;
1261 static const struct got_error *
1262 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1263 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1264 int restoring_missing_file, int reverting_versioned_file,
1265 int path_is_unversioned, int allow_bad_symlinks,
1266 struct got_repository *repo,
1267 got_worktree_checkout_cb progress_cb, void *progress_arg)
1269 const struct got_error *err = NULL;
1270 char target_path[PATH_MAX];
1271 size_t len, target_len = 0;
1272 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1273 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1275 *is_bad_symlink = 0;
1278 * Blob object content specifies the target path of the link.
1279 * If a symbolic link cannot be installed we instead create
1280 * a regular file which contains the link target path stored
1281 * in the blob object.
1284 err = got_object_blob_read_block(&len, blob);
1288 if (len + target_len >= sizeof(target_path)) {
1289 /* Path too long; install as a regular file. */
1290 *is_bad_symlink = 1;
1291 got_object_blob_rewind(blob);
1292 return install_blob(worktree, ondisk_path, path,
1293 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1294 restoring_missing_file, reverting_versioned_file,
1295 1, path_is_unversioned, repo, progress_cb,
1299 /* Skip blob object header first time around. */
1300 memcpy(target_path + target_len, buf + hdrlen,
1302 target_len += len - hdrlen;
1306 target_path[target_len] = '\0';
1308 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1309 ondisk_path, worktree->root_path);
1313 if (*is_bad_symlink && !allow_bad_symlinks) {
1314 /* install as a regular file */
1315 got_object_blob_rewind(blob);
1316 err = install_blob(worktree, ondisk_path, path,
1317 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1318 restoring_missing_file, reverting_versioned_file, 1,
1319 path_is_unversioned, repo, progress_cb, progress_arg);
1323 if (symlink(target_path, ondisk_path) == -1) {
1324 if (errno == EEXIST) {
1325 int symlink_replaced;
1326 if (path_is_unversioned) {
1327 err = (*progress_cb)(progress_arg,
1328 GOT_STATUS_UNVERSIONED, path);
1331 err = replace_existing_symlink(&symlink_replaced,
1332 ondisk_path, target_path, target_len);
1336 if (symlink_replaced) {
1337 err = (*progress_cb)(progress_arg,
1338 reverting_versioned_file ?
1340 GOT_STATUS_UPDATE, path);
1342 err = (*progress_cb)(progress_arg,
1343 GOT_STATUS_EXISTS, path);
1346 return err; /* Nothing else to do. */
1349 if (errno == ENOENT) {
1351 err = got_path_dirname(&parent, ondisk_path);
1354 err = add_dir_on_disk(worktree, parent);
1359 * Retry, and fall through to error handling
1360 * below if this second attempt fails.
1362 if (symlink(target_path, ondisk_path) != -1) {
1363 err = NULL; /* success */
1368 /* Handle errors from first or second creation attempt. */
1369 if (errno == ENAMETOOLONG) {
1370 /* bad target path; install as a regular file */
1371 *is_bad_symlink = 1;
1372 got_object_blob_rewind(blob);
1373 err = install_blob(worktree, ondisk_path, path,
1374 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1375 restoring_missing_file, reverting_versioned_file, 1,
1376 path_is_unversioned, repo,
1377 progress_cb, progress_arg);
1378 } else if (errno == ENOTDIR) {
1379 err = got_error_path(ondisk_path,
1380 GOT_ERR_FILE_OBSTRUCTED);
1382 err = got_error_from_errno3("symlink",
1383 target_path, ondisk_path);
1385 } else if (progress_cb)
1386 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1387 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1391 static const struct got_error *
1392 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1393 const char *path, mode_t te_mode, mode_t st_mode,
1394 struct got_blob_object *blob, int restoring_missing_file,
1395 int reverting_versioned_file, int installing_bad_symlink,
1396 int path_is_unversioned, struct got_repository *repo,
1397 got_worktree_checkout_cb progress_cb, void *progress_arg)
1399 const struct got_error *err = NULL;
1403 char *tmppath = NULL;
1406 mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1407 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1410 if (errno == ENOENT) {
1412 err = got_path_dirname(&parent, path);
1415 err = add_dir_on_disk(worktree, parent);
1419 fd = open(ondisk_path,
1420 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1423 return got_error_from_errno2("open",
1425 } else if (errno == EEXIST) {
1426 if (path_is_unversioned) {
1427 err = (*progress_cb)(progress_arg,
1428 GOT_STATUS_UNVERSIONED, path);
1431 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1432 !S_ISREG(st_mode) && !installing_bad_symlink) {
1433 /* TODO file is obstructed; do something */
1434 err = got_error_path(ondisk_path,
1435 GOT_ERR_FILE_OBSTRUCTED);
1438 err = got_opentemp_named_fd(&tmppath, &fd,
1444 if (fchmod(fd, apply_umask(mode)) == -1) {
1445 err = got_error_from_errno2("fchmod",
1451 return got_error_from_errno2("open", ondisk_path);
1455 if (restoring_missing_file)
1456 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1458 else if (reverting_versioned_file)
1459 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1462 err = (*progress_cb)(progress_arg,
1463 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1468 hdrlen = got_object_blob_get_hdrlen(blob);
1470 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1471 err = got_object_blob_read_block(&len, blob);
1475 /* Skip blob object header first time around. */
1476 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1478 err = got_error_from_errno("write");
1480 } else if (outlen != len - hdrlen) {
1481 err = got_error(GOT_ERR_IO);
1488 if (fsync(fd) != 0) {
1489 err = got_error_from_errno("fsync");
1494 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1495 err = got_error_from_errno2("unlink", ondisk_path);
1498 if (rename(tmppath, ondisk_path) != 0) {
1499 err = got_error_from_errno3("rename", tmppath,
1508 if (fd != -1 && close(fd) == -1 && err == NULL)
1509 err = got_error_from_errno("close");
1510 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1511 err = got_error_from_errno2("unlink", tmppath);
1516 static const struct got_error *skip_one_line(FILE *);
1519 * Upgrade STATUS_MODIFY to STATUS_CONFLICT if a
1520 * conflict marker is found in newly added lines only.
1522 static const struct got_error *
1523 get_modified_file_content_status(unsigned char *status,
1524 struct got_blob_object *blob, const char *path, struct stat *sb,
1527 const struct got_error *err, *free_err;
1528 const char *markers[3] = {
1529 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1530 GOT_DIFF_CONFLICT_MARKER_SEP,
1531 GOT_DIFF_CONFLICT_MARKER_END
1534 struct got_diffreg_result *diffreg_result = NULL;
1535 struct diff_result *r;
1536 int nchunks_parsed, n, i = 0, ln = 0;
1538 size_t linesize = 0;
1541 if (*status != GOT_STATUS_MODIFY)
1544 f1 = got_opentemp();
1546 return got_error_from_errno("got_opentemp");
1549 got_object_blob_rewind(blob);
1550 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
1555 err = got_diff_files(&diffreg_result, f1, 1, NULL, ondisk_file,
1556 1, NULL, 0, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
1560 r = diffreg_result->result;
1562 for (n = 0; n < r->chunks.len; n += nchunks_parsed) {
1563 struct diff_chunk *c;
1564 struct diff_chunk_context cc = {};
1569 * We can optimise a little by advancing straight
1570 * to the next chunk if this one has no added lines.
1572 c = diff_chunk_get(r, n);
1573 clc = diff_chunk_get_left_count(c);
1574 crc = diff_chunk_get_right_count(c);
1576 if (!crc || crc == clc) {
1578 continue; /* removed or unchanged lines */
1581 pos = diff_chunk_get_right_start_pos(c);
1582 if (fseek(ondisk_file, pos, SEEK_SET) == -1) {
1583 err = got_ferror(ondisk_file, GOT_ERR_IO);
1587 diff_chunk_context_load_change(&cc, &nchunks_parsed, r, n, 0);
1588 ln = cc.right.start;
1590 while (ln < cc.right.end) {
1591 linelen = getline(&line, &linesize, ondisk_file);
1592 if (linelen == -1) {
1593 if (feof(ondisk_file))
1595 err = got_ferror(ondisk_file, GOT_ERR_IO);
1599 if (line && strncmp(line, markers[i],
1600 strlen(markers[i])) == 0) {
1601 if (strcmp(markers[i],
1602 GOT_DIFF_CONFLICT_MARKER_END) == 0) {
1603 *status = GOT_STATUS_CONFLICT;
1614 if (f1 != NULL && fclose(f1) == EOF && err == NULL)
1615 err = got_error_from_errno("fclose");
1616 free_err = got_diffreg_result_free(diffreg_result);
1624 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1626 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1627 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1631 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1633 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1634 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1635 ie->mtime_sec == sb->st_mtim.tv_sec &&
1636 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1637 ie->size == (sb->st_size & 0xffffffff) &&
1638 !xbit_differs(ie, sb->st_mode));
1641 static unsigned char
1642 get_staged_status(struct got_fileindex_entry *ie)
1644 switch (got_fileindex_entry_stage_get(ie)) {
1645 case GOT_FILEIDX_STAGE_ADD:
1646 return GOT_STATUS_ADD;
1647 case GOT_FILEIDX_STAGE_DELETE:
1648 return GOT_STATUS_DELETE;
1649 case GOT_FILEIDX_STAGE_MODIFY:
1650 return GOT_STATUS_MODIFY;
1652 return GOT_STATUS_NO_CHANGE;
1656 static const struct got_error *
1657 get_symlink_modification_status(unsigned char *status,
1658 struct got_fileindex_entry *ie, const char *abspath,
1659 int dirfd, const char *de_name, struct got_blob_object *blob)
1661 const struct got_error *err = NULL;
1662 char target_path[PATH_MAX];
1663 char etarget[PATH_MAX];
1665 size_t len, target_len = 0;
1666 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1667 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1669 *status = GOT_STATUS_NO_CHANGE;
1671 /* Blob object content specifies the target path of the link. */
1673 err = got_object_blob_read_block(&len, blob);
1676 if (len + target_len >= sizeof(target_path)) {
1678 * Should not happen. The blob contents were OK
1679 * when this symlink was installed.
1681 return got_error(GOT_ERR_NO_SPACE);
1684 /* Skip blob object header first time around. */
1685 memcpy(target_path + target_len, buf + hdrlen,
1687 target_len += len - hdrlen;
1691 target_path[target_len] = '\0';
1694 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1696 return got_error_from_errno2("readlinkat", abspath);
1698 elen = readlink(abspath, etarget, sizeof(etarget));
1700 return got_error_from_errno2("readlink", abspath);
1703 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1704 *status = GOT_STATUS_MODIFY;
1709 static const struct got_error *
1710 get_file_status(unsigned char *status, struct stat *sb,
1711 struct got_fileindex_entry *ie, const char *abspath,
1712 int dirfd, const char *de_name, struct got_repository *repo)
1714 const struct got_error *err = NULL;
1715 struct got_object_id id;
1717 int fd = -1, fd1 = -1;
1720 struct got_blob_object *blob = NULL;
1722 unsigned char staged_status;
1724 staged_status = get_staged_status(ie);
1725 *status = GOT_STATUS_NO_CHANGE;
1726 memset(sb, 0, sizeof(*sb));
1729 * Whenever the caller provides a directory descriptor and a
1730 * directory entry name for the file, use them! This prevents
1731 * race conditions if filesystem paths change beneath our feet.
1734 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1735 if (errno == ENOENT) {
1736 if (got_fileindex_entry_has_file_on_disk(ie))
1737 *status = GOT_STATUS_MISSING;
1739 *status = GOT_STATUS_DELETE;
1742 err = got_error_from_errno2("fstatat", abspath);
1746 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1747 if (fd == -1 && errno != ENOENT &&
1748 !got_err_open_nofollow_on_symlink())
1749 return got_error_from_errno2("open", abspath);
1750 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1751 if (lstat(abspath, sb) == -1)
1752 return got_error_from_errno2("lstat", abspath);
1753 } else if (fd == -1 || fstat(fd, sb) == -1) {
1754 if (errno == ENOENT) {
1755 if (got_fileindex_entry_has_file_on_disk(ie))
1756 *status = GOT_STATUS_MISSING;
1758 *status = GOT_STATUS_DELETE;
1761 err = got_error_from_errno2("fstat", abspath);
1766 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1767 *status = GOT_STATUS_OBSTRUCTED;
1771 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1772 *status = GOT_STATUS_DELETE;
1774 } else if (!got_fileindex_entry_has_blob(ie) &&
1775 staged_status != GOT_STATUS_ADD) {
1776 *status = GOT_STATUS_ADD;
1780 if (!stat_info_differs(ie, sb))
1783 if (S_ISLNK(sb->st_mode) &&
1784 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1785 *status = GOT_STATUS_MODIFY;
1789 if (staged_status == GOT_STATUS_MODIFY ||
1790 staged_status == GOT_STATUS_ADD)
1791 got_fileindex_entry_get_staged_blob_id(&id, ie);
1793 got_fileindex_entry_get_blob_id(&id, ie);
1795 fd1 = got_opentempfd();
1797 err = got_error_from_errno("got_opentempfd");
1800 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1804 if (S_ISLNK(sb->st_mode)) {
1805 err = get_symlink_modification_status(status, ie,
1806 abspath, dirfd, de_name, blob);
1811 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1813 err = got_error_from_errno2("openat", abspath);
1818 f = fdopen(fd, "r");
1820 err = got_error_from_errno2("fdopen", abspath);
1824 hdrlen = got_object_blob_get_hdrlen(blob);
1826 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1827 err = got_object_blob_read_block(&blen, blob);
1830 /* Skip length of blob object header first time around. */
1831 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1832 if (flen == 0 && ferror(f)) {
1833 err = got_error_from_errno("fread");
1836 if (blen - hdrlen == 0) {
1838 *status = GOT_STATUS_MODIFY;
1840 } else if (flen == 0) {
1841 if (blen - hdrlen != 0)
1842 *status = GOT_STATUS_MODIFY;
1844 } else if (blen - hdrlen == flen) {
1845 /* Skip blob object header first time around. */
1846 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1847 *status = GOT_STATUS_MODIFY;
1851 *status = GOT_STATUS_MODIFY;
1857 if (*status == GOT_STATUS_MODIFY) {
1859 err = get_modified_file_content_status(status, blob, ie->path,
1861 } else if (xbit_differs(ie, sb->st_mode))
1862 *status = GOT_STATUS_MODE_CHANGE;
1864 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1865 err = got_error_from_errno("close");
1867 got_object_blob_close(blob);
1868 if (f != NULL && fclose(f) == EOF && err == NULL)
1869 err = got_error_from_errno2("fclose", abspath);
1870 if (fd != -1 && close(fd) == -1 && err == NULL)
1871 err = got_error_from_errno2("close", abspath);
1876 * Update timestamps in the file index if a file is unmodified and
1877 * we had to run a full content comparison to find out.
1879 static const struct got_error *
1880 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1881 struct got_fileindex_entry *ie, struct stat *sb)
1883 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1884 return got_fileindex_entry_update(ie, wt_fd, path,
1885 ie->blob_sha1, ie->commit_sha1, 1);
1890 static const struct got_error *
1891 update_blob(struct got_worktree *worktree,
1892 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1893 struct got_tree_entry *te, const char *path,
1894 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1897 const struct got_error *err = NULL;
1898 struct got_blob_object *blob = NULL;
1899 char *ondisk_path = NULL;
1900 unsigned char status = GOT_STATUS_NO_CHANGE;
1902 int fd1 = -1, fd2 = -1;
1904 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1905 return got_error_from_errno("asprintf");
1908 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1909 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1912 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1916 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1917 sb.st_mode = got_fileindex_perms_to_st(ie);
1919 if (stat(ondisk_path, &sb) == -1) {
1920 if (errno != ENOENT) {
1921 err = got_error_from_errno2("stat",
1925 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1926 status = GOT_STATUS_UNVERSIONED;
1928 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1929 status = GOT_STATUS_UNVERSIONED;
1931 status = GOT_STATUS_OBSTRUCTED;
1935 if (status == GOT_STATUS_OBSTRUCTED) {
1937 got_fileindex_entry_mark_skipped(ie);
1938 err = (*progress_cb)(progress_arg, status, path);
1941 if (status == GOT_STATUS_CONFLICT) {
1943 got_fileindex_entry_mark_skipped(ie);
1944 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1949 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1950 (S_ISLNK(te->mode) ||
1951 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1953 * This is a regular file or an installed bad symlink.
1954 * If the file index indicates that this file is already
1955 * up-to-date with respect to the repository we can skip
1956 * updating contents of this file.
1958 if (got_fileindex_entry_has_commit(ie) &&
1959 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1960 SHA1_DIGEST_LENGTH) == 0) {
1962 err = sync_timestamps(worktree->root_fd,
1963 path, status, ie, &sb);
1966 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1970 if (got_fileindex_entry_has_blob(ie) &&
1971 memcmp(ie->blob_sha1, te->id.sha1,
1972 SHA1_DIGEST_LENGTH) == 0) {
1973 /* Different commit but the same blob. */
1974 err = sync_timestamps(worktree->root_fd,
1975 path, status, ie, &sb);
1978 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1984 fd1 = got_opentempfd();
1986 err = got_error_from_errno("got_opentempfd");
1989 err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
1993 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1994 int update_timestamps;
1995 struct got_blob_object *blob2 = NULL;
1996 char *label_orig = NULL;
1997 if (got_fileindex_entry_has_blob(ie)) {
1998 fd2 = got_opentempfd();
2000 err = got_error_from_errno("got_opentempfd");
2003 struct got_object_id id2;
2004 got_fileindex_entry_get_blob_id(&id2, ie);
2005 err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
2010 if (got_fileindex_entry_has_commit(ie)) {
2011 char id_str[SHA1_DIGEST_STRING_LENGTH];
2012 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2013 sizeof(id_str)) == NULL) {
2014 err = got_error_path(id_str,
2015 GOT_ERR_BAD_OBJ_ID_STR);
2018 if (asprintf(&label_orig, "%s: commit %s",
2019 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2020 err = got_error_from_errno("asprintf");
2024 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2026 err = got_object_blob_read_to_str(&link_target, blob);
2029 err = merge_symlink(worktree, blob2, ondisk_path, path,
2030 label_orig, link_target, worktree->base_commit_id,
2031 repo, progress_cb, progress_arg);
2034 err = merge_blob(&update_timestamps, worktree, blob2,
2035 ondisk_path, path, sb.st_mode, label_orig, blob,
2036 worktree->base_commit_id, repo,
2037 progress_cb, progress_arg);
2040 if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2041 err = got_error_from_errno("close");
2045 got_object_blob_close(blob2);
2049 * Do not update timestamps of files with local changes.
2050 * Otherwise, a future status walk would treat them as
2051 * unmodified files again.
2053 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2054 blob->id.sha1, worktree->base_commit_id->sha1,
2056 } else if (status == GOT_STATUS_MODE_CHANGE) {
2057 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2058 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2059 } else if (status == GOT_STATUS_DELETE) {
2060 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2063 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2064 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2068 int is_bad_symlink = 0;
2069 if (S_ISLNK(te->mode)) {
2070 err = install_symlink(&is_bad_symlink, worktree,
2071 ondisk_path, path, blob,
2072 status == GOT_STATUS_MISSING, 0,
2073 status == GOT_STATUS_UNVERSIONED, 0,
2074 repo, progress_cb, progress_arg);
2076 err = install_blob(worktree, ondisk_path, path,
2077 te->mode, sb.st_mode, blob,
2078 status == GOT_STATUS_MISSING, 0, 0,
2079 status == GOT_STATUS_UNVERSIONED, repo,
2080 progress_cb, progress_arg);
2086 err = got_fileindex_entry_update(ie,
2087 worktree->root_fd, path, blob->id.sha1,
2088 worktree->base_commit_id->sha1, 1);
2090 err = create_fileindex_entry(&ie, fileindex,
2091 worktree->base_commit_id, worktree->root_fd, path,
2097 if (is_bad_symlink) {
2098 got_fileindex_entry_filetype_set(ie,
2099 GOT_FILEIDX_MODE_BAD_SYMLINK);
2103 if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2104 err = got_error_from_errno("close");
2107 got_object_blob_close(blob);
2113 static const struct got_error *
2114 remove_ondisk_file(const char *root_path, const char *path)
2116 const struct got_error *err = NULL;
2117 char *ondisk_path = NULL, *parent = NULL;
2119 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2120 return got_error_from_errno("asprintf");
2122 if (unlink(ondisk_path) == -1) {
2123 if (errno != ENOENT)
2124 err = got_error_from_errno2("unlink", ondisk_path);
2126 size_t root_len = strlen(root_path);
2127 err = got_path_dirname(&parent, ondisk_path);
2130 while (got_path_cmp(parent, root_path,
2131 strlen(parent), root_len) != 0) {
2133 ondisk_path = parent;
2135 if (rmdir(ondisk_path) == -1) {
2136 if (errno != ENOTEMPTY)
2137 err = got_error_from_errno2("rmdir",
2141 err = got_path_dirname(&parent, ondisk_path);
2152 static const struct got_error *
2153 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2154 struct got_fileindex_entry *ie, struct got_repository *repo,
2155 got_worktree_checkout_cb progress_cb, void *progress_arg)
2157 const struct got_error *err = NULL;
2158 unsigned char status;
2162 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2163 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2165 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2167 return got_error_from_errno("asprintf");
2169 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2173 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2174 char ondisk_target[PATH_MAX];
2175 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2176 sizeof(ondisk_target));
2177 if (ondisk_len == -1) {
2178 err = got_error_from_errno2("readlink", ondisk_path);
2181 ondisk_target[ondisk_len] = '\0';
2182 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2183 NULL, NULL, /* XXX pass common ancestor info? */
2184 ondisk_target, ondisk_path);
2187 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2192 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2193 status == GOT_STATUS_ADD) {
2194 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2198 * Preserve the working file and change the deleted blob's
2199 * entry into a schedule-add entry.
2201 err = got_fileindex_entry_update(ie, worktree->root_fd,
2202 ie->path, NULL, NULL, 0);
2204 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2207 if (status == GOT_STATUS_NO_CHANGE) {
2208 err = remove_ondisk_file(worktree->root_path, ie->path);
2212 got_fileindex_entry_remove(fileindex, ie);
2219 struct diff_cb_arg {
2220 struct got_fileindex *fileindex;
2221 struct got_worktree *worktree;
2222 struct got_repository *repo;
2223 got_worktree_checkout_cb progress_cb;
2225 got_cancel_cb cancel_cb;
2229 static const struct got_error *
2230 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2231 struct got_tree_entry *te, const char *parent_path)
2233 struct diff_cb_arg *a = arg;
2235 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2236 return got_error(GOT_ERR_CANCELLED);
2238 return update_blob(a->worktree, a->fileindex, ie, te,
2239 ie->path, a->repo, a->progress_cb, a->progress_arg);
2242 static const struct got_error *
2243 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2245 struct diff_cb_arg *a = arg;
2247 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2248 return got_error(GOT_ERR_CANCELLED);
2250 return delete_blob(a->worktree, a->fileindex, ie,
2251 a->repo, a->progress_cb, a->progress_arg);
2254 static const struct got_error *
2255 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2257 struct diff_cb_arg *a = arg;
2258 const struct got_error *err;
2261 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2262 return got_error(GOT_ERR_CANCELLED);
2264 if (got_object_tree_entry_is_submodule(te))
2267 if (asprintf(&path, "%s%s%s", parent_path,
2268 parent_path[0] ? "/" : "", te->name)
2270 return got_error_from_errno("asprintf");
2272 if (S_ISDIR(te->mode))
2273 err = add_dir_on_disk(a->worktree, path);
2275 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2276 a->repo, a->progress_cb, a->progress_arg);
2282 const struct got_error *
2283 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2285 uint32_t uuid_status;
2287 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2288 if (uuid_status != uuid_s_ok) {
2290 return got_error_uuid(uuid_status, "uuid_to_string");
2296 static const struct got_error *
2297 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2299 const struct got_error *err = NULL;
2300 char *uuidstr = NULL;
2304 err = got_worktree_get_uuid(&uuidstr, worktree);
2308 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2309 err = got_error_from_errno("asprintf");
2316 const struct got_error *
2317 got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2320 return get_ref_name(refname, worktree, prefix);
2323 const struct got_error *
2324 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2326 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2329 static const struct got_error *
2330 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2332 return get_ref_name(refname, worktree,
2333 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2336 static const struct got_error *
2337 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2339 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2342 static const struct got_error *
2343 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2345 return get_ref_name(refname, worktree,
2346 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2349 static const struct got_error *
2350 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2352 return get_ref_name(refname, worktree,
2353 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2356 static const struct got_error *
2357 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2359 return get_ref_name(refname, worktree,
2360 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2363 static const struct got_error *
2364 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2366 return get_ref_name(refname, worktree,
2367 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2370 static const struct got_error *
2371 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2373 return get_ref_name(refname, worktree,
2374 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2377 static const struct got_error *
2378 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2380 return get_ref_name(refname, worktree,
2381 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2384 const struct got_error *
2385 got_worktree_get_histedit_script_path(char **path,
2386 struct got_worktree *worktree)
2388 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2389 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2391 return got_error_from_errno("asprintf");
2396 static const struct got_error *
2397 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2399 return get_ref_name(refname, worktree,
2400 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2403 static const struct got_error *
2404 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2406 return get_ref_name(refname, worktree,
2407 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2411 * Prevent Git's garbage collector from deleting our base commit by
2412 * setting a reference to our base commit's ID.
2414 static const struct got_error *
2415 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2417 const struct got_error *err = NULL;
2418 struct got_reference *ref = NULL;
2421 err = got_worktree_get_base_ref_name(&refname, worktree);
2425 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2429 err = got_ref_write(ref, repo);
2437 static const struct got_error *
2438 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2440 const struct got_error *err = NULL;
2442 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2443 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2444 err = got_error_from_errno("asprintf");
2445 *fileindex_path = NULL;
2451 static const struct got_error *
2452 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2453 struct got_worktree *worktree)
2455 const struct got_error *err = NULL;
2458 *fileindex_path = NULL;
2459 *fileindex = got_fileindex_alloc();
2460 if (*fileindex == NULL)
2461 return got_error_from_errno("got_fileindex_alloc");
2463 err = get_fileindex_path(fileindex_path, worktree);
2467 index = fopen(*fileindex_path, "rbe");
2468 if (index == NULL) {
2469 if (errno != ENOENT)
2470 err = got_error_from_errno2("fopen", *fileindex_path);
2472 err = got_fileindex_read(*fileindex, index);
2473 if (fclose(index) == EOF && err == NULL)
2474 err = got_error_from_errno("fclose");
2478 free(*fileindex_path);
2479 *fileindex_path = NULL;
2480 got_fileindex_free(*fileindex);
2486 struct bump_base_commit_id_arg {
2487 struct got_object_id *base_commit_id;
2490 const char *entry_name;
2491 got_worktree_checkout_cb progress_cb;
2495 static const struct got_error *
2496 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2498 const struct got_error *err;
2499 struct bump_base_commit_id_arg *a = arg;
2501 if (a->entry_name) {
2502 if (strcmp(ie->path, a->path) != 0)
2504 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2507 if (got_fileindex_entry_was_skipped(ie))
2510 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2511 SHA1_DIGEST_LENGTH) == 0)
2514 if (a->progress_cb) {
2515 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2520 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2524 /* Bump base commit ID of all files within an updated part of the work tree. */
2525 static const struct got_error *
2526 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2527 struct got_fileindex *fileindex,
2528 got_worktree_checkout_cb progress_cb, void *progress_arg)
2530 struct bump_base_commit_id_arg bbc_arg;
2532 bbc_arg.base_commit_id = worktree->base_commit_id;
2533 bbc_arg.entry_name = NULL;
2535 bbc_arg.path_len = 0;
2536 bbc_arg.progress_cb = progress_cb;
2537 bbc_arg.progress_arg = progress_arg;
2539 return got_fileindex_for_each_entry_safe(fileindex,
2540 bump_base_commit_id, &bbc_arg);
2543 static const struct got_error *
2544 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2546 const struct got_error *err = NULL;
2547 char *new_fileindex_path = NULL;
2548 FILE *new_index = NULL;
2549 struct timespec timeout;
2551 err = got_opentemp_named(&new_fileindex_path, &new_index,
2552 fileindex_path, "");
2556 err = got_fileindex_write(fileindex, new_index);
2560 if (rename(new_fileindex_path, fileindex_path) != 0) {
2561 err = got_error_from_errno3("rename", new_fileindex_path,
2563 unlink(new_fileindex_path);
2567 * Sleep for a short amount of time to ensure that files modified after
2568 * this program exits have a different time stamp from the one which
2569 * was recorded in the file index.
2572 timeout.tv_nsec = 1;
2573 nanosleep(&timeout, NULL);
2577 free(new_fileindex_path);
2581 static const struct got_error *
2582 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2583 struct got_object_id **tree_id, const char *wt_relpath,
2584 struct got_commit_object *base_commit, struct got_worktree *worktree,
2585 struct got_repository *repo)
2587 const struct got_error *err = NULL;
2588 struct got_object_id *id = NULL;
2589 char *in_repo_path = NULL;
2590 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2592 *entry_type = GOT_OBJ_TYPE_ANY;
2593 *tree_relpath = NULL;
2596 if (wt_relpath[0] == '\0') {
2597 /* Check out all files within the work tree. */
2598 *entry_type = GOT_OBJ_TYPE_TREE;
2599 *tree_relpath = strdup("");
2600 if (*tree_relpath == NULL) {
2601 err = got_error_from_errno("strdup");
2604 err = got_object_id_by_path(tree_id, repo, base_commit,
2605 worktree->path_prefix);
2611 /* Check out a subset of files in the work tree. */
2613 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2614 is_root_wt ? "" : "/", wt_relpath) == -1) {
2615 err = got_error_from_errno("asprintf");
2619 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2624 in_repo_path = NULL;
2626 err = got_object_get_type(entry_type, repo, id);
2630 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2631 /* Check out a single file. */
2632 if (strchr(wt_relpath, '/') == NULL) {
2633 /* Check out a single file in work tree's root dir. */
2634 in_repo_path = strdup(worktree->path_prefix);
2635 if (in_repo_path == NULL) {
2636 err = got_error_from_errno("strdup");
2639 *tree_relpath = strdup("");
2640 if (*tree_relpath == NULL) {
2641 err = got_error_from_errno("strdup");
2645 /* Check out a single file in a subdirectory. */
2646 err = got_path_dirname(tree_relpath, wt_relpath);
2649 if (asprintf(&in_repo_path, "%s%s%s",
2650 worktree->path_prefix, is_root_wt ? "" : "/",
2651 *tree_relpath) == -1) {
2652 err = got_error_from_errno("asprintf");
2656 err = got_object_id_by_path(tree_id, repo,
2657 base_commit, in_repo_path);
2659 /* Check out all files within a subdirectory. */
2660 *tree_id = got_object_id_dup(id);
2661 if (*tree_id == NULL) {
2662 err = got_error_from_errno("got_object_id_dup");
2665 *tree_relpath = strdup(wt_relpath);
2666 if (*tree_relpath == NULL) {
2667 err = got_error_from_errno("strdup");
2675 *entry_type = GOT_OBJ_TYPE_ANY;
2676 free(*tree_relpath);
2677 *tree_relpath = NULL;
2684 static const struct got_error *
2685 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2686 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2687 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2688 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2690 const struct got_error *err = NULL;
2691 struct got_commit_object *commit = NULL;
2692 struct got_tree_object *tree = NULL;
2693 struct got_fileindex_diff_tree_cb diff_cb;
2694 struct diff_cb_arg arg;
2696 err = ref_base_commit(worktree, repo);
2698 if (!(err->code == GOT_ERR_ERRNO &&
2699 (errno == EACCES || errno == EROFS)))
2701 err = (*progress_cb)(progress_arg,
2702 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2707 err = got_object_open_as_commit(&commit, repo,
2708 worktree->base_commit_id);
2712 err = got_object_open_as_tree(&tree, repo, tree_id);
2717 got_object_tree_find_entry(tree, entry_name) == NULL) {
2718 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2722 diff_cb.diff_old_new = diff_old_new;
2723 diff_cb.diff_old = diff_old;
2724 diff_cb.diff_new = diff_new;
2725 arg.fileindex = fileindex;
2726 arg.worktree = worktree;
2728 arg.progress_cb = progress_cb;
2729 arg.progress_arg = progress_arg;
2730 arg.cancel_cb = cancel_cb;
2731 arg.cancel_arg = cancel_arg;
2732 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2733 entry_name, repo, &diff_cb, &arg);
2736 got_object_tree_close(tree);
2738 got_object_commit_close(commit);
2742 const struct got_error *
2743 got_worktree_checkout_files(struct got_worktree *worktree,
2744 struct got_pathlist_head *paths, struct got_repository *repo,
2745 got_worktree_checkout_cb progress_cb, void *progress_arg,
2746 got_cancel_cb cancel_cb, void *cancel_arg)
2748 const struct got_error *err = NULL, *sync_err, *unlockerr;
2749 struct got_commit_object *commit = NULL;
2750 struct got_tree_object *tree = NULL;
2751 struct got_fileindex *fileindex = NULL;
2752 char *fileindex_path = NULL;
2753 struct got_pathlist_entry *pe;
2754 struct tree_path_data {
2755 STAILQ_ENTRY(tree_path_data) entry;
2756 struct got_object_id *tree_id;
2761 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2763 STAILQ_INIT(&tree_paths);
2765 err = lock_worktree(worktree, LOCK_EX);
2769 err = got_object_open_as_commit(&commit, repo,
2770 worktree->base_commit_id);
2774 /* Map all specified paths to in-repository trees. */
2775 TAILQ_FOREACH(pe, paths, entry) {
2776 tpd = malloc(sizeof(*tpd));
2778 err = got_error_from_errno("malloc");
2782 err = find_tree_entry_for_checkout(&tpd->entry_type,
2783 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2790 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2791 err = got_path_basename(&tpd->entry_name, pe->path);
2799 tpd->entry_name = NULL;
2801 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2805 * Read the file index.
2806 * Checking out files is supposed to be an idempotent operation.
2807 * If the on-disk file index is incomplete we will try to complete it.
2809 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2813 tpd = STAILQ_FIRST(&tree_paths);
2814 TAILQ_FOREACH(pe, paths, entry) {
2815 struct bump_base_commit_id_arg bbc_arg;
2817 err = checkout_files(worktree, fileindex, tpd->relpath,
2818 tpd->tree_id, tpd->entry_name, repo,
2819 progress_cb, progress_arg, cancel_cb, cancel_arg);
2823 bbc_arg.base_commit_id = worktree->base_commit_id;
2824 bbc_arg.entry_name = tpd->entry_name;
2825 bbc_arg.path = pe->path;
2826 bbc_arg.path_len = pe->path_len;
2827 bbc_arg.progress_cb = progress_cb;
2828 bbc_arg.progress_arg = progress_arg;
2829 err = got_fileindex_for_each_entry_safe(fileindex,
2830 bump_base_commit_id, &bbc_arg);
2834 tpd = STAILQ_NEXT(tpd, entry);
2836 sync_err = sync_fileindex(fileindex, fileindex_path);
2837 if (sync_err && err == NULL)
2840 free(fileindex_path);
2842 got_object_tree_close(tree);
2844 got_object_commit_close(commit);
2846 got_fileindex_free(fileindex);
2847 while (!STAILQ_EMPTY(&tree_paths)) {
2848 tpd = STAILQ_FIRST(&tree_paths);
2849 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2854 unlockerr = lock_worktree(worktree, LOCK_SH);
2855 if (unlockerr && err == NULL)
2860 struct merge_file_cb_arg {
2861 struct got_worktree *worktree;
2862 struct got_fileindex *fileindex;
2863 got_worktree_checkout_cb progress_cb;
2865 got_cancel_cb cancel_cb;
2867 const char *label_orig;
2868 struct got_object_id *commit_id2;
2869 int allow_bad_symlinks;
2872 static const struct got_error *
2873 merge_file_cb(void *arg, struct got_blob_object *blob1,
2874 struct got_blob_object *blob2, FILE *f1, FILE *f2,
2875 struct got_object_id *id1, struct got_object_id *id2,
2876 const char *path1, const char *path2,
2877 mode_t mode1, mode_t mode2, struct got_repository *repo)
2879 static const struct got_error *err = NULL;
2880 struct merge_file_cb_arg *a = arg;
2881 struct got_fileindex_entry *ie;
2882 char *ondisk_path = NULL;
2884 unsigned char status;
2885 int local_changes_subsumed;
2886 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2887 char *id_str = NULL, *label_deriv2 = NULL;
2889 if (blob1 && blob2) {
2890 ie = got_fileindex_entry_get(a->fileindex, path2,
2893 return (*a->progress_cb)(a->progress_arg,
2894 GOT_STATUS_MISSING, path2);
2896 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2898 return got_error_from_errno("asprintf");
2900 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2905 if (status == GOT_STATUS_DELETE) {
2906 err = (*a->progress_cb)(a->progress_arg,
2907 GOT_STATUS_MERGE, path2);
2910 if (status != GOT_STATUS_NO_CHANGE &&
2911 status != GOT_STATUS_MODIFY &&
2912 status != GOT_STATUS_CONFLICT &&
2913 status != GOT_STATUS_ADD) {
2914 err = (*a->progress_cb)(a->progress_arg, status, path2);
2918 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2920 err = got_object_blob_read_to_str(&link_target2, blob2);
2923 err = merge_symlink(a->worktree, blob1, ondisk_path,
2924 path2, a->label_orig, link_target2, a->commit_id2,
2925 repo, a->progress_cb, a->progress_arg);
2930 f_orig = got_opentemp();
2931 if (f_orig == NULL) {
2932 err = got_error_from_errno("got_opentemp");
2935 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2940 f_deriv2 = got_opentemp();
2941 if (f_deriv2 == NULL)
2943 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2948 fd = open(ondisk_path,
2949 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2951 err = got_error_from_errno2("open",
2955 f_deriv = fdopen(fd, "r");
2956 if (f_deriv == NULL) {
2957 err = got_error_from_errno2("fdopen",
2962 err = got_object_id_str(&id_str, a->commit_id2);
2965 if (asprintf(&label_deriv2, "%s: commit %s",
2966 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2967 err = got_error_from_errno("asprintf");
2970 err = merge_file(&local_changes_subsumed, a->worktree,
2971 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2972 mode2, a->label_orig, NULL, label_deriv2,
2973 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2974 a->progress_cb, a->progress_arg);
2977 ie = got_fileindex_entry_get(a->fileindex, path1,
2980 return (*a->progress_cb)(a->progress_arg,
2981 GOT_STATUS_MISSING, path1);
2983 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2985 return got_error_from_errno("asprintf");
2987 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2993 case GOT_STATUS_NO_CHANGE:
2994 err = (*a->progress_cb)(a->progress_arg,
2995 GOT_STATUS_DELETE, path1);
2998 err = remove_ondisk_file(a->worktree->root_path, path1);
3002 got_fileindex_entry_mark_deleted_from_disk(ie);
3004 case GOT_STATUS_DELETE:
3005 case GOT_STATUS_MISSING:
3006 err = (*a->progress_cb)(a->progress_arg,
3007 GOT_STATUS_DELETE, path1);
3011 got_fileindex_entry_mark_deleted_from_disk(ie);
3013 case GOT_STATUS_ADD: {
3014 struct got_object_id *id;
3018 * Delete the added file only if its content already
3019 * exists in the repository.
3021 err = got_object_blob_file_create(&id, &blob1_f,
3022 &blob1_size, path1);
3025 if (got_object_id_cmp(id, id1) == 0) {
3026 err = (*a->progress_cb)(a->progress_arg,
3027 GOT_STATUS_DELETE, path1);
3030 err = remove_ondisk_file(a->worktree->root_path,
3035 got_fileindex_entry_remove(a->fileindex,
3038 err = (*a->progress_cb)(a->progress_arg,
3039 GOT_STATUS_CANNOT_DELETE, path1);
3041 if (fclose(blob1_f) == EOF && err == NULL)
3042 err = got_error_from_errno("fclose");
3048 case GOT_STATUS_MODIFY:
3049 case GOT_STATUS_CONFLICT:
3050 err = (*a->progress_cb)(a->progress_arg,
3051 GOT_STATUS_CANNOT_DELETE, path1);
3055 case GOT_STATUS_OBSTRUCTED:
3056 err = (*a->progress_cb)(a->progress_arg, status, path1);
3064 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3066 return got_error_from_errno("asprintf");
3067 ie = got_fileindex_entry_get(a->fileindex, path2,
3070 err = get_file_status(&status, &sb, ie, ondisk_path,
3074 if (status != GOT_STATUS_NO_CHANGE &&
3075 status != GOT_STATUS_MODIFY &&
3076 status != GOT_STATUS_CONFLICT &&
3077 status != GOT_STATUS_ADD) {
3078 err = (*a->progress_cb)(a->progress_arg,
3082 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3084 err = got_object_blob_read_to_str(&link_target2,
3088 err = merge_symlink(a->worktree, NULL,
3089 ondisk_path, path2, a->label_orig,
3090 link_target2, a->commit_id2, repo,
3091 a->progress_cb, a->progress_arg);
3093 } else if (S_ISREG(sb.st_mode)) {
3094 err = merge_blob(&local_changes_subsumed,
3095 a->worktree, NULL, ondisk_path, path2,
3096 sb.st_mode, a->label_orig, blob2,
3097 a->commit_id2, repo, a->progress_cb,
3100 err = got_error_path(ondisk_path,
3101 GOT_ERR_FILE_OBSTRUCTED);
3105 if (status == GOT_STATUS_DELETE) {
3106 err = got_fileindex_entry_update(ie,
3107 a->worktree->root_fd, path2, blob2->id.sha1,
3108 a->worktree->base_commit_id->sha1, 0);
3113 int is_bad_symlink = 0;
3114 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3115 if (S_ISLNK(mode2)) {
3116 err = install_symlink(&is_bad_symlink,
3117 a->worktree, ondisk_path, path2, blob2, 0,
3118 0, 1, a->allow_bad_symlinks, repo,
3119 a->progress_cb, a->progress_arg);
3121 err = install_blob(a->worktree, ondisk_path, path2,
3122 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3123 a->progress_cb, a->progress_arg);
3127 err = got_fileindex_entry_alloc(&ie, path2);
3130 err = got_fileindex_entry_update(ie,
3131 a->worktree->root_fd, path2, NULL, NULL, 1);
3133 got_fileindex_entry_free(ie);
3136 err = got_fileindex_entry_add(a->fileindex, ie);
3138 got_fileindex_entry_free(ie);
3141 if (is_bad_symlink) {
3142 got_fileindex_entry_filetype_set(ie,
3143 GOT_FILEIDX_MODE_BAD_SYMLINK);
3148 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3149 err = got_error_from_errno("fclose");
3150 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3151 err = got_error_from_errno("fclose");
3152 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3153 err = got_error_from_errno("fclose");
3160 static const struct got_error *
3161 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3163 struct got_worktree *worktree = arg;
3165 /* Reject merges into a work tree with mixed base commits. */
3166 if (got_fileindex_entry_has_commit(ie) &&
3167 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3168 SHA1_DIGEST_LENGTH) != 0)
3169 return got_error(GOT_ERR_MIXED_COMMITS);
3174 struct check_merge_conflicts_arg {
3175 struct got_worktree *worktree;
3176 struct got_fileindex *fileindex;
3177 struct got_repository *repo;
3180 static const struct got_error *
3181 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3182 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3183 struct got_object_id *id1, struct got_object_id *id2,
3184 const char *path1, const char *path2,
3185 mode_t mode1, mode_t mode2, struct got_repository *repo)
3187 const struct got_error *err = NULL;
3188 struct check_merge_conflicts_arg *a = arg;
3189 unsigned char status;
3191 struct got_fileindex_entry *ie;
3192 const char *path = path2 ? path2 : path1;
3193 struct got_object_id *id = id2 ? id2 : id1;
3199 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3203 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3205 return got_error_from_errno("asprintf");
3207 /* Reject merges into a work tree with conflicted files. */
3208 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3212 if (status == GOT_STATUS_CONFLICT)
3213 return got_error(GOT_ERR_CONFLICTS);
3218 static const struct got_error *
3219 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3220 const char *fileindex_path, struct got_object_id *commit_id1,
3221 struct got_object_id *commit_id2, struct got_repository *repo,
3222 got_worktree_checkout_cb progress_cb, void *progress_arg,
3223 got_cancel_cb cancel_cb, void *cancel_arg)
3225 const struct got_error *err = NULL, *sync_err;
3226 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3227 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3228 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3229 struct check_merge_conflicts_arg cmc_arg;
3230 struct merge_file_cb_arg arg;
3231 char *label_orig = NULL;
3232 FILE *f1 = NULL, *f2 = NULL;
3233 int fd1 = -1, fd2 = -1;
3236 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3239 err = got_object_id_by_path(&tree_id1, repo, commit1,
3240 worktree->path_prefix);
3241 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3247 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3251 err = got_object_id_str(&id_str, commit_id1);
3255 if (asprintf(&label_orig, "%s: commit %s",
3256 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3257 err = got_error_from_errno("asprintf");
3263 f1 = got_opentemp();
3265 err = got_error_from_errno("got_opentemp");
3270 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3274 err = got_object_id_by_path(&tree_id2, repo, commit2,
3275 worktree->path_prefix);
3279 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3283 f2 = got_opentemp();
3285 err = got_error_from_errno("got_opentemp");
3289 fd1 = got_opentempfd();
3291 err = got_error_from_errno("got_opentempfd");
3295 fd2 = got_opentempfd();
3297 err = got_error_from_errno("got_opentempfd");
3301 cmc_arg.worktree = worktree;
3302 cmc_arg.fileindex = fileindex;
3303 cmc_arg.repo = repo;
3304 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3305 check_merge_conflicts, &cmc_arg, 0);
3309 arg.worktree = worktree;
3310 arg.fileindex = fileindex;
3311 arg.progress_cb = progress_cb;
3312 arg.progress_arg = progress_arg;
3313 arg.cancel_cb = cancel_cb;
3314 arg.cancel_arg = cancel_arg;
3315 arg.label_orig = label_orig;
3316 arg.commit_id2 = commit_id2;
3317 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3318 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3319 merge_file_cb, &arg, 1);
3320 sync_err = sync_fileindex(fileindex, fileindex_path);
3321 if (sync_err && err == NULL)
3325 got_object_commit_close(commit1);
3327 got_object_commit_close(commit2);
3329 got_object_tree_close(tree1);
3331 got_object_tree_close(tree2);
3332 if (f1 && fclose(f1) == EOF && err == NULL)
3333 err = got_error_from_errno("fclose");
3334 if (f2 && fclose(f2) == EOF && err == NULL)
3335 err = got_error_from_errno("fclose");
3336 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3337 err = got_error_from_errno("close");
3338 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3339 err = got_error_from_errno("close");
3344 const struct got_error *
3345 got_worktree_merge_files(struct got_worktree *worktree,
3346 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3347 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3348 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3350 const struct got_error *err, *unlockerr;
3351 char *fileindex_path = NULL;
3352 struct got_fileindex *fileindex = NULL;
3354 err = lock_worktree(worktree, LOCK_EX);
3358 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3362 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3367 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3368 commit_id2, repo, progress_cb, progress_arg,
3369 cancel_cb, cancel_arg);
3372 got_fileindex_free(fileindex);
3373 free(fileindex_path);
3374 unlockerr = lock_worktree(worktree, LOCK_SH);
3375 if (unlockerr && err == NULL)
3380 struct diff_dir_cb_arg {
3381 struct got_fileindex *fileindex;
3382 struct got_worktree *worktree;
3383 const char *status_path;
3384 size_t status_path_len;
3385 struct got_repository *repo;
3386 got_worktree_status_cb status_cb;
3388 got_cancel_cb cancel_cb;
3390 /* A pathlist containing per-directory pathlists of ignore patterns. */
3391 struct got_pathlist_head *ignores;
3392 int report_unchanged;
3396 static const struct got_error *
3397 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3398 int dirfd, const char *de_name,
3399 got_worktree_status_cb status_cb, void *status_arg,
3400 struct got_repository *repo, int report_unchanged)
3402 const struct got_error *err = NULL;
3403 unsigned char status = GOT_STATUS_NO_CHANGE;
3404 unsigned char staged_status;
3406 struct got_object_id blob_id, commit_id, staged_blob_id;
3407 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3408 struct got_object_id *staged_blob_idp = NULL;
3410 staged_status = get_staged_status(ie);
3411 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3415 if (status == GOT_STATUS_NO_CHANGE &&
3416 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3419 if (got_fileindex_entry_has_blob(ie))
3420 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3421 if (got_fileindex_entry_has_commit(ie))
3422 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3423 if (staged_status == GOT_STATUS_ADD ||
3424 staged_status == GOT_STATUS_MODIFY) {
3425 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3426 &staged_blob_id, ie);
3429 return (*status_cb)(status_arg, status, staged_status,
3430 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3433 static const struct got_error *
3434 status_old_new(void *arg, struct got_fileindex_entry *ie,
3435 struct dirent *de, const char *parent_path, int dirfd)
3437 const struct got_error *err = NULL;
3438 struct diff_dir_cb_arg *a = arg;
3441 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3442 return got_error(GOT_ERR_CANCELLED);
3444 if (got_path_cmp(parent_path, a->status_path,
3445 strlen(parent_path), a->status_path_len) != 0 &&
3446 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3449 if (parent_path[0]) {
3450 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3451 parent_path, de->d_name) == -1)
3452 return got_error_from_errno("asprintf");
3454 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3456 return got_error_from_errno("asprintf");
3459 err = report_file_status(ie, abspath, dirfd, de->d_name,
3460 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3465 static const struct got_error *
3466 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3468 struct diff_dir_cb_arg *a = arg;
3469 struct got_object_id blob_id, commit_id;
3470 unsigned char status;
3472 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3473 return got_error(GOT_ERR_CANCELLED);
3475 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3478 got_fileindex_entry_get_blob_id(&blob_id, ie);
3479 got_fileindex_entry_get_commit_id(&commit_id, ie);
3480 if (got_fileindex_entry_has_file_on_disk(ie))
3481 status = GOT_STATUS_MISSING;
3483 status = GOT_STATUS_DELETE;
3484 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3485 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3489 free_ignores(struct got_pathlist_head *ignores)
3491 struct got_pathlist_entry *pe;
3493 TAILQ_FOREACH(pe, ignores, entry) {
3494 struct got_pathlist_head *ignorelist = pe->data;
3496 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3498 got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3501 static const struct got_error *
3502 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3504 const struct got_error *err = NULL;
3505 struct got_pathlist_entry *pe = NULL;
3506 struct got_pathlist_head *ignorelist;
3507 char *line = NULL, *pattern, *dirpath = NULL;
3508 size_t linesize = 0;
3511 ignorelist = calloc(1, sizeof(*ignorelist));
3512 if (ignorelist == NULL)
3513 return got_error_from_errno("calloc");
3514 TAILQ_INIT(ignorelist);
3516 while ((linelen = getline(&line, &linesize, f)) != -1) {
3517 if (linelen > 0 && line[linelen - 1] == '\n')
3518 line[linelen - 1] = '\0';
3520 /* Git's ignores may contain comments. */
3524 /* Git's negated patterns are not (yet?) supported. */
3528 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3530 err = got_error_from_errno("asprintf");
3533 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3538 err = got_error_from_errno("getline");
3542 dirpath = strdup(path);
3543 if (dirpath == NULL) {
3544 err = got_error_from_errno("strdup");
3547 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3550 if (err || pe == NULL) {
3552 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3558 match_ignores(struct got_pathlist_head *ignores, const char *path)
3560 struct got_pathlist_entry *pe;
3562 /* Handle patterns which match in all directories. */
3563 TAILQ_FOREACH(pe, ignores, entry) {
3564 struct got_pathlist_head *ignorelist = pe->data;
3565 struct got_pathlist_entry *pi;
3567 TAILQ_FOREACH(pi, ignorelist, entry) {
3568 const char *p, *pattern = pi->path;
3570 if (strncmp(pattern, "**/", 3) != 0)
3575 if (fnmatch(pattern, p,
3576 FNM_PATHNAME | FNM_LEADING_DIR)) {
3577 /* Retry in next directory. */
3578 while (*p && *p != '/')
3590 * The ignores pathlist contains ignore lists from children before
3591 * parents, so we can find the most specific ignorelist by walking
3592 * ignores backwards.
3594 pe = TAILQ_LAST(ignores, got_pathlist_head);
3596 if (got_path_is_child(path, pe->path, pe->path_len)) {
3597 struct got_pathlist_head *ignorelist = pe->data;
3598 struct got_pathlist_entry *pi;
3599 TAILQ_FOREACH(pi, ignorelist, entry) {
3600 const char *pattern = pi->path;
3601 int flags = FNM_LEADING_DIR;
3602 if (strstr(pattern, "/**/") == NULL)
3603 flags |= FNM_PATHNAME;
3604 if (fnmatch(pattern, path, flags))
3609 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3615 static const struct got_error *
3616 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3617 const char *path, int dirfd, const char *ignores_filename)
3619 const struct got_error *err = NULL;
3622 FILE *ignoresfile = NULL;
3624 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3625 path[0] ? "/" : "", ignores_filename) == -1)
3626 return got_error_from_errno("asprintf");
3629 fd = openat(dirfd, ignores_filename,
3630 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3632 if (errno != ENOENT && errno != EACCES)
3633 err = got_error_from_errno2("openat",
3636 ignoresfile = fdopen(fd, "r");
3637 if (ignoresfile == NULL)
3638 err = got_error_from_errno2("fdopen",
3642 err = read_ignores(ignores, path, ignoresfile);
3646 ignoresfile = fopen(ignorespath, "re");
3647 if (ignoresfile == NULL) {
3648 if (errno != ENOENT && errno != EACCES)
3649 err = got_error_from_errno2("fopen",
3652 err = read_ignores(ignores, path, ignoresfile);
3655 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3656 err = got_error_from_errno2("fclose", path);
3657 if (fd != -1 && close(fd) == -1 && err == NULL)
3658 err = got_error_from_errno2("close", path);
3663 static const struct got_error *
3664 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3667 const struct got_error *err = NULL;
3668 struct diff_dir_cb_arg *a = arg;
3674 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3675 return got_error(GOT_ERR_CANCELLED);
3677 if (parent_path[0]) {
3678 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3679 return got_error_from_errno("asprintf");
3684 if (de->d_type == DT_DIR) {
3685 if (!a->no_ignores && ignore != NULL &&
3686 match_ignores(a->ignores, path))
3688 } else if (!match_ignores(a->ignores, path) &&
3689 got_path_is_child(path, a->status_path, a->status_path_len))
3690 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3691 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3697 static const struct got_error *
3698 status_traverse(void *arg, const char *path, int dirfd)
3700 const struct got_error *err = NULL;
3701 struct diff_dir_cb_arg *a = arg;
3706 err = add_ignores(a->ignores, a->worktree->root_path,
3707 path, dirfd, ".cvsignore");
3711 err = add_ignores(a->ignores, a->worktree->root_path, path,
3712 dirfd, ".gitignore");
3717 static const struct got_error *
3718 report_single_file_status(const char *path, const char *ondisk_path,
3719 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3720 void *status_arg, struct got_repository *repo, int report_unchanged,
3721 struct got_pathlist_head *ignores, int no_ignores)
3723 struct got_fileindex_entry *ie;
3726 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3728 return report_file_status(ie, ondisk_path, -1, NULL,
3729 status_cb, status_arg, repo, report_unchanged);
3731 if (lstat(ondisk_path, &sb) == -1) {
3732 if (errno != ENOENT)
3733 return got_error_from_errno2("lstat", ondisk_path);
3734 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3735 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3738 if (!no_ignores && match_ignores(ignores, path))
3741 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3742 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3743 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3748 static const struct got_error *
3749 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3750 const char *root_path, const char *path)
3752 const struct got_error *err;
3753 char *parent_path, *next_parent_path = NULL;
3755 err = add_ignores(ignores, root_path, "", -1,
3760 err = add_ignores(ignores, root_path, "", -1,
3765 err = got_path_dirname(&parent_path, path);
3767 if (err->code == GOT_ERR_BAD_PATH)
3768 return NULL; /* cannot traverse parent */
3772 err = add_ignores(ignores, root_path, parent_path, -1,
3776 err = add_ignores(ignores, root_path, parent_path, -1,
3780 err = got_path_dirname(&next_parent_path, parent_path);
3782 if (err->code == GOT_ERR_BAD_PATH)
3783 err = NULL; /* traversed everything */
3786 if (got_path_is_root_dir(parent_path))
3789 parent_path = next_parent_path;
3790 next_parent_path = NULL;
3794 free(next_parent_path);
3798 static const struct got_error *
3799 worktree_status(struct got_worktree *worktree, const char *path,
3800 struct got_fileindex *fileindex, struct got_repository *repo,
3801 got_worktree_status_cb status_cb, void *status_arg,
3802 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3803 int report_unchanged)
3805 const struct got_error *err = NULL;
3807 struct got_fileindex_diff_dir_cb fdiff_cb;
3808 struct diff_dir_cb_arg arg;
3809 char *ondisk_path = NULL;
3810 struct got_pathlist_head ignores;
3811 struct got_fileindex_entry *ie;
3813 TAILQ_INIT(&ignores);
3815 if (asprintf(&ondisk_path, "%s%s%s",
3816 worktree->root_path, path[0] ? "/" : "", path) == -1)
3817 return got_error_from_errno("asprintf");
3819 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3821 err = report_single_file_status(path, ondisk_path,
3822 fileindex, status_cb, status_arg, repo,
3823 report_unchanged, &ignores, no_ignores);
3827 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3829 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3830 !got_err_open_nofollow_on_symlink())
3831 err = got_error_from_errno2("open", ondisk_path);
3834 err = add_ignores_from_parent_paths(&ignores,
3835 worktree->root_path, ondisk_path);
3839 err = report_single_file_status(path, ondisk_path,
3840 fileindex, status_cb, status_arg, repo,
3841 report_unchanged, &ignores, no_ignores);
3844 fdiff_cb.diff_old_new = status_old_new;
3845 fdiff_cb.diff_old = status_old;
3846 fdiff_cb.diff_new = status_new;
3847 fdiff_cb.diff_traverse = status_traverse;
3848 arg.fileindex = fileindex;
3849 arg.worktree = worktree;
3850 arg.status_path = path;
3851 arg.status_path_len = strlen(path);
3853 arg.status_cb = status_cb;
3854 arg.status_arg = status_arg;
3855 arg.cancel_cb = cancel_cb;
3856 arg.cancel_arg = cancel_arg;
3857 arg.report_unchanged = report_unchanged;
3858 arg.no_ignores = no_ignores;
3860 err = add_ignores_from_parent_paths(&ignores,
3861 worktree->root_path, path);
3865 arg.ignores = &ignores;
3866 err = got_fileindex_diff_dir(fileindex, fd,
3867 worktree->root_path, path, repo, &fdiff_cb, &arg);
3870 free_ignores(&ignores);
3871 if (fd != -1 && close(fd) == -1 && err == NULL)
3872 err = got_error_from_errno("close");
3877 const struct got_error *
3878 got_worktree_status(struct got_worktree *worktree,
3879 struct got_pathlist_head *paths, struct got_repository *repo,
3880 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3881 got_cancel_cb cancel_cb, void *cancel_arg)
3883 const struct got_error *err = NULL;
3884 char *fileindex_path = NULL;
3885 struct got_fileindex *fileindex = NULL;
3886 struct got_pathlist_entry *pe;
3888 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3892 TAILQ_FOREACH(pe, paths, entry) {
3893 err = worktree_status(worktree, pe->path, fileindex, repo,
3894 status_cb, status_arg, cancel_cb, cancel_arg,
3899 free(fileindex_path);
3900 got_fileindex_free(fileindex);
3904 const struct got_error *
3905 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3908 const struct got_error *err = NULL;
3909 char *resolved = NULL, *cwd = NULL, *path = NULL;
3912 char *abspath = NULL;
3913 char canonpath[PATH_MAX];
3917 cwd = getcwd(NULL, 0);
3919 return got_error_from_errno("getcwd");
3921 if (lstat(arg, &sb) == -1) {
3922 if (errno != ENOENT) {
3923 err = got_error_from_errno2("lstat", arg);
3928 if (S_ISLNK(sb.st_mode)) {
3930 * We cannot use realpath(3) with symlinks since we want to
3931 * operate on the symlink itself.
3932 * But we can make the path absolute, assuming it is relative
3933 * to the current working directory, and then canonicalize it.
3935 if (!got_path_is_absolute(arg)) {
3936 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3937 err = got_error_from_errno("asprintf");
3942 err = got_canonpath(abspath ? abspath : arg, canonpath,
3946 resolved = strdup(canonpath);
3947 if (resolved == NULL) {
3948 err = got_error_from_errno("strdup");
3952 resolved = realpath(arg, NULL);
3953 if (resolved == NULL) {
3954 if (errno != ENOENT) {
3955 err = got_error_from_errno2("realpath", arg);
3958 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3959 err = got_error_from_errno("asprintf");
3962 err = got_canonpath(abspath, canonpath,
3966 resolved = strdup(canonpath);
3967 if (resolved == NULL) {
3968 err = got_error_from_errno("strdup");
3974 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3975 strlen(got_worktree_get_root_path(worktree)))) {
3976 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3980 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3981 err = got_path_skip_common_ancestor(&path,
3982 got_worktree_get_root_path(worktree), resolved);
3988 err = got_error_from_errno("strdup");
3993 /* XXX status walk can't deal with trailing slash! */
3995 while (len > 0 && path[len - 1] == '/') {
3996 path[len - 1] = '\0';
4010 struct schedule_addition_args {
4011 struct got_worktree *worktree;
4012 struct got_fileindex *fileindex;
4013 got_worktree_checkout_cb progress_cb;
4015 struct got_repository *repo;
4018 static const struct got_error *
4019 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4020 const char *relpath, struct got_object_id *blob_id,
4021 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4022 int dirfd, const char *de_name)
4024 struct schedule_addition_args *a = arg;
4025 const struct got_error *err = NULL;
4026 struct got_fileindex_entry *ie;
4030 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4032 return got_error_from_errno("asprintf");
4034 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4036 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4040 /* Re-adding an existing entry is a no-op. */
4041 if (status == GOT_STATUS_ADD)
4043 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4048 if (status != GOT_STATUS_UNVERSIONED) {
4049 if (status == GOT_STATUS_NONEXISTENT)
4050 err = got_error_set_errno(ENOENT, ondisk_path);
4052 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4056 err = got_fileindex_entry_alloc(&ie, relpath);
4059 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4060 relpath, NULL, NULL, 1);
4062 got_fileindex_entry_free(ie);
4065 err = got_fileindex_entry_add(a->fileindex, ie);
4067 got_fileindex_entry_free(ie);
4074 if (status == GOT_STATUS_ADD)
4076 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4079 const struct got_error *
4080 got_worktree_schedule_add(struct got_worktree *worktree,
4081 struct got_pathlist_head *paths,
4082 got_worktree_checkout_cb progress_cb, void *progress_arg,
4083 struct got_repository *repo, int no_ignores)
4085 struct got_fileindex *fileindex = NULL;
4086 char *fileindex_path = NULL;
4087 const struct got_error *err = NULL, *sync_err, *unlockerr;
4088 struct got_pathlist_entry *pe;
4089 struct schedule_addition_args saa;
4091 err = lock_worktree(worktree, LOCK_EX);
4095 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4099 saa.worktree = worktree;
4100 saa.fileindex = fileindex;
4101 saa.progress_cb = progress_cb;
4102 saa.progress_arg = progress_arg;
4105 TAILQ_FOREACH(pe, paths, entry) {
4106 err = worktree_status(worktree, pe->path, fileindex, repo,
4107 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4111 sync_err = sync_fileindex(fileindex, fileindex_path);
4112 if (sync_err && err == NULL)
4115 free(fileindex_path);
4117 got_fileindex_free(fileindex);
4118 unlockerr = lock_worktree(worktree, LOCK_SH);
4119 if (unlockerr && err == NULL)
4124 struct schedule_deletion_args {
4125 struct got_worktree *worktree;
4126 struct got_fileindex *fileindex;
4127 got_worktree_delete_cb progress_cb;
4129 struct got_repository *repo;
4130 int delete_local_mods;
4132 int ignore_missing_paths;
4133 const char *status_codes;
4136 static const struct got_error *
4137 schedule_for_deletion(void *arg, unsigned char status,
4138 unsigned char staged_status, const char *relpath,
4139 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4140 struct got_object_id *commit_id, int dirfd, const char *de_name)
4142 struct schedule_deletion_args *a = arg;
4143 const struct got_error *err = NULL;
4144 struct got_fileindex_entry *ie = NULL;
4148 if (status == GOT_STATUS_NONEXISTENT) {
4149 if (a->ignore_missing_paths)
4151 return got_error_set_errno(ENOENT, relpath);
4154 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4156 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4158 staged_status = get_staged_status(ie);
4159 if (staged_status != GOT_STATUS_NO_CHANGE) {
4160 if (staged_status == GOT_STATUS_DELETE)
4162 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4165 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4167 return got_error_from_errno("asprintf");
4169 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4174 if (a->status_codes) {
4175 size_t ncodes = strlen(a->status_codes);
4177 for (i = 0; i < ncodes ; i++) {
4178 if (status == a->status_codes[i])
4182 /* Do not delete files in non-matching status. */
4186 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4187 a->status_codes[i] != GOT_STATUS_MISSING) {
4188 static char msg[64];
4189 snprintf(msg, sizeof(msg),
4190 "invalid status code '%c'", a->status_codes[i]);
4191 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4196 if (status != GOT_STATUS_NO_CHANGE) {
4197 if (status == GOT_STATUS_DELETE)
4199 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4200 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4203 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4204 err = got_error_set_errno(ENOENT, relpath);
4207 if (status != GOT_STATUS_MODIFY &&
4208 status != GOT_STATUS_MISSING) {
4209 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4214 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4218 if (unlinkat(dirfd, de_name, 0) == -1) {
4219 err = got_error_from_errno2("unlinkat",
4223 } else if (unlink(ondisk_path) == -1) {
4224 err = got_error_from_errno2("unlink", ondisk_path);
4228 root_len = strlen(a->worktree->root_path);
4231 err = got_path_dirname(&parent, ondisk_path);
4235 ondisk_path = parent;
4236 if (rmdir(ondisk_path) == -1) {
4237 if (errno != ENOTEMPTY)
4238 err = got_error_from_errno2("rmdir",
4242 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4243 strlen(ondisk_path), root_len) != 0);
4246 got_fileindex_entry_mark_deleted_from_disk(ie);
4251 if (status == GOT_STATUS_DELETE)
4253 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4254 staged_status, relpath);
4257 const struct got_error *
4258 got_worktree_schedule_delete(struct got_worktree *worktree,
4259 struct got_pathlist_head *paths, int delete_local_mods,
4260 const char *status_codes,
4261 got_worktree_delete_cb progress_cb, void *progress_arg,
4262 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4264 struct got_fileindex *fileindex = NULL;
4265 char *fileindex_path = NULL;
4266 const struct got_error *err = NULL, *sync_err, *unlockerr;
4267 struct got_pathlist_entry *pe;
4268 struct schedule_deletion_args sda;
4270 err = lock_worktree(worktree, LOCK_EX);
4274 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4278 sda.worktree = worktree;
4279 sda.fileindex = fileindex;
4280 sda.progress_cb = progress_cb;
4281 sda.progress_arg = progress_arg;
4283 sda.delete_local_mods = delete_local_mods;
4284 sda.keep_on_disk = keep_on_disk;
4285 sda.ignore_missing_paths = ignore_missing_paths;
4286 sda.status_codes = status_codes;
4288 TAILQ_FOREACH(pe, paths, entry) {
4289 err = worktree_status(worktree, pe->path, fileindex, repo,
4290 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4294 sync_err = sync_fileindex(fileindex, fileindex_path);
4295 if (sync_err && err == NULL)
4298 free(fileindex_path);
4300 got_fileindex_free(fileindex);
4301 unlockerr = lock_worktree(worktree, LOCK_SH);
4302 if (unlockerr && err == NULL)
4307 static const struct got_error *
4308 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4310 const struct got_error *err = NULL;
4312 size_t linesize = 0, n;
4315 linelen = getline(&line, &linesize, infile);
4316 if (linelen == -1) {
4317 if (ferror(infile)) {
4318 err = got_error_from_errno("getline");
4324 n = fwrite(line, 1, linelen, outfile);
4326 err = got_ferror(outfile, GOT_ERR_IO);
4331 n = fwrite(line, 1, linelen, rejectfile);
4333 err = got_ferror(rejectfile, GOT_ERR_IO);
4340 static const struct got_error *
4341 skip_one_line(FILE *f)
4344 size_t linesize = 0;
4347 linelen = getline(&line, &linesize, f);
4348 if (linelen == -1) {
4350 return got_error_from_errno("getline");
4357 static const struct got_error *
4358 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4359 int start_old, int end_old, int start_new, int end_new,
4360 FILE *outfile, FILE *rejectfile)
4362 const struct got_error *err;
4364 /* Copy old file's lines leading up to patch. */
4365 while (!feof(f1) && *line_cur1 < start_old) {
4366 err = copy_one_line(f1, outfile, NULL);
4371 /* Skip new file's lines leading up to patch. */
4372 while (!feof(f2) && *line_cur2 < start_new) {
4374 err = copy_one_line(f2, NULL, rejectfile);
4376 err = skip_one_line(f2);
4381 /* Copy patched lines. */
4382 while (!feof(f2) && *line_cur2 <= end_new) {
4383 err = copy_one_line(f2, outfile, NULL);
4388 /* Skip over old file's replaced lines. */
4389 while (!feof(f1) && *line_cur1 <= end_old) {
4391 err = copy_one_line(f1, NULL, rejectfile);
4393 err = skip_one_line(f1);
4402 static const struct got_error *
4403 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4404 FILE *outfile, FILE *rejectfile)
4406 const struct got_error *err;
4409 /* Copy old file's lines until EOF. */
4411 err = copy_one_line(f1, outfile, NULL);
4418 /* Copy new file's lines until EOF. */
4420 err = copy_one_line(f2, NULL, rejectfile);
4430 static const struct got_error *
4431 apply_or_reject_change(int *choice, int *nchunks_used,
4432 struct diff_result *diff_result, int n,
4433 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4434 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4435 got_worktree_patch_cb patch_cb, void *patch_arg)
4437 const struct got_error *err = NULL;
4438 struct diff_chunk_context cc = {};
4439 int start_old, end_old, start_new, end_new;
4441 struct diff_output_unidiff_state *diff_state;
4442 struct diff_input_info diff_info;
4445 *choice = GOT_PATCH_CHOICE_NONE;
4447 /* Get changed line numbers without context lines for copy_change(). */
4448 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4449 start_old = cc.left.start;
4450 end_old = cc.left.end;
4451 start_new = cc.right.start;
4452 end_new = cc.right.end;
4454 /* Get the same change with context lines for display. */
4455 memset(&cc, 0, sizeof(cc));
4456 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4458 memset(&diff_info, 0, sizeof(diff_info));
4459 diff_info.left_path = relpath;
4460 diff_info.right_path = relpath;
4462 diff_state = diff_output_unidiff_state_alloc();
4463 if (diff_state == NULL)
4464 return got_error_set_errno(ENOMEM,
4465 "diff_output_unidiff_state_alloc");
4467 hunkfile = got_opentemp();
4468 if (hunkfile == NULL) {
4469 err = got_error_from_errno("got_opentemp");
4473 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4475 if (rc != DIFF_RC_OK) {
4476 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4480 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4481 err = got_ferror(hunkfile, GOT_ERR_IO);
4485 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4486 hunkfile, changeno, nchanges);
4491 case GOT_PATCH_CHOICE_YES:
4492 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4493 end_old, start_new, end_new, outfile, rejectfile);
4495 case GOT_PATCH_CHOICE_NO:
4496 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4497 end_old, start_new, end_new, rejectfile, outfile);
4499 case GOT_PATCH_CHOICE_QUIT:
4502 err = got_error(GOT_ERR_PATCH_CHOICE);
4506 diff_output_unidiff_state_free(diff_state);
4507 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4508 err = got_error_from_errno("fclose");
4512 struct revert_file_args {
4513 struct got_worktree *worktree;
4514 struct got_fileindex *fileindex;
4515 got_worktree_checkout_cb progress_cb;
4517 got_worktree_patch_cb patch_cb;
4519 struct got_repository *repo;
4520 int unlink_added_files;
4523 static const struct got_error *
4524 create_patched_content(char **path_outfile, int reverse_patch,
4525 struct got_object_id *blob_id, const char *path2,
4526 int dirfd2, const char *de_name2,
4527 const char *relpath, struct got_repository *repo,
4528 got_worktree_patch_cb patch_cb, void *patch_arg)
4530 const struct got_error *err, *free_err;
4531 struct got_blob_object *blob = NULL;
4532 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4533 int fd = -1, fd2 = -1;
4534 char link_target[PATH_MAX];
4535 ssize_t link_len = 0;
4536 char *path1 = NULL, *id_str = NULL;
4538 struct got_diffreg_result *diffreg_result = NULL;
4539 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4540 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4542 *path_outfile = NULL;
4544 err = got_object_id_str(&id_str, blob_id);
4549 fd2 = openat(dirfd2, de_name2,
4550 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4552 if (!got_err_open_nofollow_on_symlink()) {
4553 err = got_error_from_errno2("openat", path2);
4556 link_len = readlinkat(dirfd2, de_name2,
4557 link_target, sizeof(link_target));
4558 if (link_len == -1) {
4559 return got_error_from_errno2("readlinkat",
4562 sb2.st_mode = S_IFLNK;
4563 sb2.st_size = link_len;
4566 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4568 if (!got_err_open_nofollow_on_symlink()) {
4569 err = got_error_from_errno2("open", path2);
4572 link_len = readlink(path2, link_target,
4573 sizeof(link_target));
4575 return got_error_from_errno2("readlink", path2);
4576 sb2.st_mode = S_IFLNK;
4577 sb2.st_size = link_len;
4581 if (fstat(fd2, &sb2) == -1) {
4582 err = got_error_from_errno2("fstat", path2);
4586 f2 = fdopen(fd2, "r");
4588 err = got_error_from_errno2("fdopen", path2);
4594 f2 = got_opentemp();
4596 err = got_error_from_errno2("got_opentemp", path2);
4599 n = fwrite(link_target, 1, link_len, f2);
4600 if (n != link_len) {
4601 err = got_ferror(f2, GOT_ERR_IO);
4604 if (fflush(f2) == EOF) {
4605 err = got_error_from_errno("fflush");
4611 fd = got_opentempfd();
4613 err = got_error_from_errno("got_opentempfd");
4617 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4621 err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4625 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4629 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4630 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4634 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4639 if (fseek(f1, 0L, SEEK_SET) == -1)
4640 return got_ferror(f1, GOT_ERR_IO);
4641 if (fseek(f2, 0L, SEEK_SET) == -1)
4642 return got_ferror(f2, GOT_ERR_IO);
4644 /* Count the number of actual changes in the diff result. */
4645 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4646 struct diff_chunk_context cc = {};
4647 diff_chunk_context_load_change(&cc, &nchunks_used,
4648 diffreg_result->result, n, 0);
4651 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4653 err = apply_or_reject_change(&choice, &nchunks_used,
4654 diffreg_result->result, n, relpath, f1, f2,
4655 &line_cur1, &line_cur2,
4656 reverse_patch ? NULL : outfile,
4657 reverse_patch ? outfile : NULL,
4658 ++i, nchanges, patch_cb, patch_arg);
4661 if (choice == GOT_PATCH_CHOICE_YES)
4663 else if (choice == GOT_PATCH_CHOICE_QUIT)
4667 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4668 reverse_patch ? NULL : outfile,
4669 reverse_patch ? outfile : NULL);
4673 if (!S_ISLNK(sb2.st_mode)) {
4676 mode = apply_umask(sb2.st_mode);
4677 if (fchmod(fileno(outfile), mode) == -1) {
4678 err = got_error_from_errno2("fchmod", path2);
4685 if (fd != -1 && close(fd) == -1 && err == NULL)
4686 err = got_error_from_errno("close");
4688 got_object_blob_close(blob);
4689 free_err = got_diffreg_result_free(diffreg_result);
4692 if (f1 && fclose(f1) == EOF && err == NULL)
4693 err = got_error_from_errno2("fclose", path1);
4694 if (f2 && fclose(f2) == EOF && err == NULL)
4695 err = got_error_from_errno2("fclose", path2);
4696 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4697 err = got_error_from_errno2("close", path2);
4698 if (outfile && fclose(outfile) == EOF && err == NULL)
4699 err = got_error_from_errno2("fclose", *path_outfile);
4700 if (path1 && unlink(path1) == -1 && err == NULL)
4701 err = got_error_from_errno2("unlink", path1);
4702 if (err || !have_content) {
4703 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4704 err = got_error_from_errno2("unlink", *path_outfile);
4705 free(*path_outfile);
4706 *path_outfile = NULL;
4712 static const struct got_error *
4713 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4714 const char *relpath, struct got_object_id *blob_id,
4715 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4716 int dirfd, const char *de_name)
4718 struct revert_file_args *a = arg;
4719 const struct got_error *err = NULL;
4720 char *parent_path = NULL;
4721 struct got_fileindex_entry *ie;
4722 struct got_commit_object *base_commit = NULL;
4723 struct got_tree_object *tree = NULL;
4724 struct got_object_id *tree_id = NULL;
4725 const struct got_tree_entry *te = NULL;
4726 char *tree_path = NULL, *te_name;
4727 char *ondisk_path = NULL, *path_content = NULL;
4728 struct got_blob_object *blob = NULL;
4731 /* Reverting a staged deletion is a no-op. */
4732 if (status == GOT_STATUS_DELETE &&
4733 staged_status != GOT_STATUS_NO_CHANGE)
4736 if (status == GOT_STATUS_UNVERSIONED)
4737 return (*a->progress_cb)(a->progress_arg,
4738 GOT_STATUS_UNVERSIONED, relpath);
4740 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4742 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4744 /* Construct in-repository path of tree which contains this blob. */
4745 err = got_path_dirname(&parent_path, ie->path);
4747 if (err->code != GOT_ERR_BAD_PATH)
4749 parent_path = strdup("/");
4750 if (parent_path == NULL) {
4751 err = got_error_from_errno("strdup");
4755 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4756 tree_path = strdup(parent_path);
4757 if (tree_path == NULL) {
4758 err = got_error_from_errno("strdup");
4762 if (got_path_is_root_dir(parent_path)) {
4763 tree_path = strdup(a->worktree->path_prefix);
4764 if (tree_path == NULL) {
4765 err = got_error_from_errno("strdup");
4769 if (asprintf(&tree_path, "%s/%s",
4770 a->worktree->path_prefix, parent_path) == -1) {
4771 err = got_error_from_errno("asprintf");
4777 err = got_object_open_as_commit(&base_commit, a->repo,
4778 a->worktree->base_commit_id);
4782 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4784 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4785 (status == GOT_STATUS_ADD ||
4786 staged_status == GOT_STATUS_ADD)))
4789 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4793 err = got_path_basename(&te_name, ie->path);
4797 te = got_object_tree_find_entry(tree, te_name);
4799 if (te == NULL && status != GOT_STATUS_ADD &&
4800 staged_status != GOT_STATUS_ADD) {
4801 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4807 case GOT_STATUS_ADD:
4809 int choice = GOT_PATCH_CHOICE_NONE;
4810 err = (*a->patch_cb)(&choice, a->patch_arg,
4811 status, ie->path, NULL, 1, 1);
4814 if (choice != GOT_PATCH_CHOICE_YES)
4817 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4821 got_fileindex_entry_remove(a->fileindex, ie);
4822 if (a->unlink_added_files) {
4823 if (asprintf(&ondisk_path, "%s/%s",
4824 got_worktree_get_root_path(a->worktree),
4826 err = got_error_from_errno("asprintf");
4829 if (unlink(ondisk_path) == -1) {
4830 err = got_error_from_errno2("unlink",
4836 case GOT_STATUS_DELETE:
4838 int choice = GOT_PATCH_CHOICE_NONE;
4839 err = (*a->patch_cb)(&choice, a->patch_arg,
4840 status, ie->path, NULL, 1, 1);
4843 if (choice != GOT_PATCH_CHOICE_YES)
4847 case GOT_STATUS_MODIFY:
4848 case GOT_STATUS_MODE_CHANGE:
4849 case GOT_STATUS_CONFLICT:
4850 case GOT_STATUS_MISSING: {
4851 struct got_object_id id;
4852 if (staged_status == GOT_STATUS_ADD ||
4853 staged_status == GOT_STATUS_MODIFY)
4854 got_fileindex_entry_get_staged_blob_id(&id, ie);
4856 got_fileindex_entry_get_blob_id(&id, ie);
4857 fd = got_opentempfd();
4859 err = got_error_from_errno("got_opentempfd");
4863 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4867 if (asprintf(&ondisk_path, "%s/%s",
4868 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4869 err = got_error_from_errno("asprintf");
4873 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4874 status == GOT_STATUS_CONFLICT)) {
4875 int is_bad_symlink = 0;
4876 err = create_patched_content(&path_content, 1, &id,
4877 ondisk_path, dirfd, de_name, ie->path, a->repo,
4878 a->patch_cb, a->patch_arg);
4879 if (err || path_content == NULL)
4881 if (te && S_ISLNK(te->mode)) {
4882 if (unlink(path_content) == -1) {
4883 err = got_error_from_errno2("unlink",
4887 err = install_symlink(&is_bad_symlink,
4888 a->worktree, ondisk_path, ie->path,
4889 blob, 0, 1, 0, 0, a->repo,
4890 a->progress_cb, a->progress_arg);
4892 if (rename(path_content, ondisk_path) == -1) {
4893 err = got_error_from_errno3("rename",
4894 path_content, ondisk_path);
4899 int is_bad_symlink = 0;
4900 if (te && S_ISLNK(te->mode)) {
4901 err = install_symlink(&is_bad_symlink,
4902 a->worktree, ondisk_path, ie->path,
4903 blob, 0, 1, 0, 0, a->repo,
4904 a->progress_cb, a->progress_arg);
4906 err = install_blob(a->worktree, ondisk_path,
4908 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4909 got_fileindex_perms_to_st(ie), blob,
4910 0, 1, 0, 0, a->repo,
4911 a->progress_cb, a->progress_arg);
4915 if (status == GOT_STATUS_DELETE ||
4916 status == GOT_STATUS_MODE_CHANGE) {
4917 err = got_fileindex_entry_update(ie,
4918 a->worktree->root_fd, relpath,
4920 a->worktree->base_commit_id->sha1, 1);
4924 if (is_bad_symlink) {
4925 got_fileindex_entry_filetype_set(ie,
4926 GOT_FILEIDX_MODE_BAD_SYMLINK);
4939 if (fd != -1 && close(fd) == -1 && err == NULL)
4940 err = got_error_from_errno("close");
4942 got_object_blob_close(blob);
4944 got_object_tree_close(tree);
4947 got_object_commit_close(base_commit);
4951 const struct got_error *
4952 got_worktree_revert(struct got_worktree *worktree,
4953 struct got_pathlist_head *paths,
4954 got_worktree_checkout_cb progress_cb, void *progress_arg,
4955 got_worktree_patch_cb patch_cb, void *patch_arg,
4956 struct got_repository *repo)
4958 struct got_fileindex *fileindex = NULL;
4959 char *fileindex_path = NULL;
4960 const struct got_error *err = NULL, *unlockerr = NULL;
4961 const struct got_error *sync_err = NULL;
4962 struct got_pathlist_entry *pe;
4963 struct revert_file_args rfa;
4965 err = lock_worktree(worktree, LOCK_EX);
4969 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4973 rfa.worktree = worktree;
4974 rfa.fileindex = fileindex;
4975 rfa.progress_cb = progress_cb;
4976 rfa.progress_arg = progress_arg;
4977 rfa.patch_cb = patch_cb;
4978 rfa.patch_arg = patch_arg;
4980 rfa.unlink_added_files = 0;
4981 TAILQ_FOREACH(pe, paths, entry) {
4982 err = worktree_status(worktree, pe->path, fileindex, repo,
4983 revert_file, &rfa, NULL, NULL, 1, 0);
4987 sync_err = sync_fileindex(fileindex, fileindex_path);
4988 if (sync_err && err == NULL)
4991 free(fileindex_path);
4993 got_fileindex_free(fileindex);
4994 unlockerr = lock_worktree(worktree, LOCK_SH);
4995 if (unlockerr && err == NULL)
5001 free_commitable(struct got_commitable *ct)
5004 free(ct->in_repo_path);
5005 free(ct->ondisk_path);
5007 free(ct->base_blob_id);
5008 free(ct->staged_blob_id);
5009 free(ct->base_commit_id);
5013 struct collect_commitables_arg {
5014 struct got_pathlist_head *commitable_paths;
5015 struct got_repository *repo;
5016 struct got_worktree *worktree;
5017 struct got_fileindex *fileindex;
5018 int have_staged_files;
5019 int allow_bad_symlinks;
5020 int diff_header_shown;
5021 int commit_conflicts;
5028 * Create a file which contains the target path of a symlink so we can feed
5029 * it as content to the diff engine.
5031 static const struct got_error *
5032 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5033 const char *abspath)
5035 const struct got_error *err = NULL;
5036 char target_path[PATH_MAX];
5037 ssize_t target_len, outlen;
5042 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5043 if (target_len == -1)
5044 return got_error_from_errno2("readlinkat", abspath);
5046 target_len = readlink(abspath, target_path, PATH_MAX);
5047 if (target_len == -1)
5048 return got_error_from_errno2("readlink", abspath);
5051 *fd = got_opentempfd();
5053 return got_error_from_errno("got_opentempfd");
5055 outlen = write(*fd, target_path, target_len);
5057 err = got_error_from_errno("got_opentempfd");
5061 if (lseek(*fd, 0, SEEK_SET) == -1) {
5062 err = got_error_from_errno2("lseek", abspath);
5073 static const struct got_error *
5074 append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5075 FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5076 int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5078 const struct got_error *err = NULL;
5079 struct got_blob_object *blob1 = NULL;
5080 int fd = -1, fd1 = -1, fd2 = -1;
5081 FILE *ondisk_file = NULL;
5082 char *label1 = NULL;
5086 char *id_str = NULL;
5088 memset(&sb, 0, sizeof(sb));
5091 if (ct->staged_status != GOT_STATUS_MODIFY &&
5092 ct->staged_status != GOT_STATUS_ADD &&
5093 ct->staged_status != GOT_STATUS_DELETE)
5096 if (ct->status != GOT_STATUS_MODIFY &&
5097 ct->status != GOT_STATUS_ADD &&
5098 ct->status != GOT_STATUS_DELETE &&
5099 ct->status != GOT_STATUS_CONFLICT)
5103 err = got_opentemp_truncate(f1);
5105 return got_error_from_errno("got_opentemp_truncate");
5106 err = got_opentemp_truncate(f2);
5108 return got_error_from_errno("got_opentemp_truncate");
5110 if (!*diff_header_shown) {
5111 err = got_object_id_str(&id_str, worktree->base_commit_id);
5114 fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5115 got_worktree_get_root_path(worktree));
5116 fprintf(diff_outfile, "commit - %s\n", id_str);
5117 fprintf(diff_outfile, "path + %s%s\n",
5118 got_worktree_get_root_path(worktree),
5119 diff_staged ? " (staged changes)" : "");
5120 *diff_header_shown = 1;
5124 const char *label1 = NULL, *label2 = NULL;
5125 switch (ct->staged_status) {
5126 case GOT_STATUS_MODIFY:
5130 case GOT_STATUS_ADD:
5133 case GOT_STATUS_DELETE:
5137 return got_error(GOT_ERR_FILE_STATUS);
5139 fd1 = got_opentempfd();
5141 err = got_error_from_errno("got_opentempfd");
5144 fd2 = got_opentempfd();
5146 err = got_error_from_errno("got_opentempfd");
5149 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5150 fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5151 label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5152 NULL, repo, diff_outfile);
5156 fd1 = got_opentempfd();
5158 err = got_error_from_errno("got_opentempfd");
5162 if (ct->status != GOT_STATUS_ADD) {
5163 err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5169 if (ct->status != GOT_STATUS_DELETE) {
5171 fd = openat(dirfd, de_name,
5172 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5174 if (!got_err_open_nofollow_on_symlink()) {
5175 err = got_error_from_errno2("openat",
5179 err = get_symlink_target_file(&fd, dirfd,
5180 de_name, ct->ondisk_path);
5185 fd = open(ct->ondisk_path,
5186 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5188 if (!got_err_open_nofollow_on_symlink()) {
5189 err = got_error_from_errno2("open",
5193 err = get_symlink_target_file(&fd, dirfd,
5194 de_name, ct->ondisk_path);
5199 if (fstatat(fd, ct->ondisk_path, &sb,
5200 AT_SYMLINK_NOFOLLOW) == -1) {
5201 err = got_error_from_errno2("fstatat", ct->ondisk_path);
5204 ondisk_file = fdopen(fd, "r");
5205 if (ondisk_file == NULL) {
5206 err = got_error_from_errno2("fdopen", ct->ondisk_path);
5214 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5220 err = got_diff_blob_file(blob1, f1, size1, label1,
5221 ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5222 GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5224 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5225 err = got_error_from_errno("close");
5226 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5227 err = got_error_from_errno("close");
5229 got_object_blob_close(blob1);
5230 if (fd != -1 && close(fd) == -1 && err == NULL)
5231 err = got_error_from_errno("close");
5232 if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5233 err = got_error_from_errno("fclose");
5237 static const struct got_error *
5238 collect_commitables(void *arg, unsigned char status,
5239 unsigned char staged_status, const char *relpath,
5240 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5241 struct got_object_id *commit_id, int dirfd, const char *de_name)
5243 struct collect_commitables_arg *a = arg;
5244 const struct got_error *err = NULL;
5245 struct got_commitable *ct = NULL;
5246 struct got_pathlist_entry *new = NULL;
5247 char *parent_path = NULL, *path = NULL;
5250 if (a->have_staged_files) {
5251 if (staged_status != GOT_STATUS_MODIFY &&
5252 staged_status != GOT_STATUS_ADD &&
5253 staged_status != GOT_STATUS_DELETE)
5256 if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5257 printf("C %s\n", relpath);
5258 return got_error(GOT_ERR_COMMIT_CONFLICT);
5261 if (status != GOT_STATUS_MODIFY &&
5262 status != GOT_STATUS_MODE_CHANGE &&
5263 status != GOT_STATUS_ADD &&
5264 status != GOT_STATUS_DELETE &&
5265 status != GOT_STATUS_CONFLICT)
5269 if (asprintf(&path, "/%s", relpath) == -1) {
5270 err = got_error_from_errno("asprintf");
5273 if (strcmp(path, "/") == 0) {
5274 parent_path = strdup("");
5275 if (parent_path == NULL)
5276 return got_error_from_errno("strdup");
5278 err = got_path_dirname(&parent_path, path);
5283 ct = calloc(1, sizeof(*ct));
5285 err = got_error_from_errno("calloc");
5289 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5291 err = got_error_from_errno("asprintf");
5295 if (staged_status == GOT_STATUS_ADD ||
5296 staged_status == GOT_STATUS_MODIFY) {
5297 struct got_fileindex_entry *ie;
5298 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5299 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5300 case GOT_FILEIDX_MODE_REGULAR_FILE:
5301 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5304 case GOT_FILEIDX_MODE_SYMLINK:
5308 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5311 ct->mode |= got_fileindex_entry_perms_get(ie);
5312 } else if (status != GOT_STATUS_DELETE &&
5313 staged_status != GOT_STATUS_DELETE) {
5315 if (fstatat(dirfd, de_name, &sb,
5316 AT_SYMLINK_NOFOLLOW) == -1) {
5317 err = got_error_from_errno2("fstatat",
5321 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5322 err = got_error_from_errno2("lstat", ct->ondisk_path);
5325 ct->mode = sb.st_mode;
5328 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5329 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5331 err = got_error_from_errno("asprintf");
5335 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5336 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5338 char target_path[PATH_MAX];
5340 target_len = readlink(ct->ondisk_path, target_path,
5341 sizeof(target_path));
5342 if (target_len == -1) {
5343 err = got_error_from_errno2("readlink",
5347 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5348 target_len, ct->ondisk_path, a->worktree->root_path);
5351 if (is_bad_symlink) {
5352 err = got_error_path(ct->ondisk_path,
5353 GOT_ERR_BAD_SYMLINK);
5359 ct->status = status;
5360 ct->staged_status = staged_status;
5361 ct->blob_id = NULL; /* will be filled in when blob gets created */
5362 if (ct->status != GOT_STATUS_ADD &&
5363 ct->staged_status != GOT_STATUS_ADD) {
5364 ct->base_blob_id = got_object_id_dup(blob_id);
5365 if (ct->base_blob_id == NULL) {
5366 err = got_error_from_errno("got_object_id_dup");
5369 ct->base_commit_id = got_object_id_dup(commit_id);
5370 if (ct->base_commit_id == NULL) {
5371 err = got_error_from_errno("got_object_id_dup");
5375 if (ct->staged_status == GOT_STATUS_ADD ||
5376 ct->staged_status == GOT_STATUS_MODIFY) {
5377 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5378 if (ct->staged_blob_id == NULL) {
5379 err = got_error_from_errno("got_object_id_dup");
5383 ct->path = strdup(path);
5384 if (ct->path == NULL) {
5385 err = got_error_from_errno("strdup");
5388 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5392 if (a->diff_outfile && ct && new != NULL) {
5393 err = append_ct_diff(ct, &a->diff_header_shown,
5394 a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5395 a->have_staged_files, a->repo, a->worktree);
5400 if (ct && (err || new == NULL))
5401 free_commitable(ct);
5407 static const struct got_error *write_tree(struct got_object_id **, int *,
5408 struct got_tree_object *, const char *, struct got_pathlist_head *,
5409 got_worktree_status_cb status_cb, void *status_arg,
5410 struct got_repository *);
5412 static const struct got_error *
5413 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5414 struct got_tree_entry *te, const char *parent_path,
5415 struct got_pathlist_head *commitable_paths,
5416 got_worktree_status_cb status_cb, void *status_arg,
5417 struct got_repository *repo)
5419 const struct got_error *err = NULL;
5420 struct got_tree_object *subtree;
5423 if (asprintf(&subpath, "%s%s%s", parent_path,
5424 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5425 return got_error_from_errno("asprintf");
5427 err = got_object_open_as_tree(&subtree, repo, &te->id);
5431 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5432 commitable_paths, status_cb, status_arg, repo);
5433 got_object_tree_close(subtree);
5438 static const struct got_error *
5439 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5441 const struct got_error *err = NULL;
5442 char *ct_parent_path = NULL;
5446 if (strchr(ct->in_repo_path, '/') == NULL) {
5447 *match = got_path_is_root_dir(path);
5451 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5454 *match = (strcmp(path, ct_parent_path) == 0);
5455 free(ct_parent_path);
5460 get_ct_file_mode(struct got_commitable *ct)
5462 if (S_ISLNK(ct->mode))
5465 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5468 static const struct got_error *
5469 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5470 struct got_tree_entry *te, struct got_commitable *ct)
5472 const struct got_error *err = NULL;
5476 err = got_object_tree_entry_dup(new_te, te);
5480 (*new_te)->mode = get_ct_file_mode(ct);
5482 if (ct->staged_status == GOT_STATUS_MODIFY)
5483 memcpy(&(*new_te)->id, ct->staged_blob_id,
5484 sizeof((*new_te)->id));
5486 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5488 if (err && *new_te) {
5495 static const struct got_error *
5496 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5497 struct got_commitable *ct)
5499 const struct got_error *err = NULL;
5500 char *ct_name = NULL;
5504 *new_te = calloc(1, sizeof(**new_te));
5505 if (*new_te == NULL)
5506 return got_error_from_errno("calloc");
5508 err = got_path_basename(&ct_name, ct->path);
5511 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5512 sizeof((*new_te)->name)) {
5513 err = got_error(GOT_ERR_NO_SPACE);
5517 (*new_te)->mode = get_ct_file_mode(ct);
5519 if (ct->staged_status == GOT_STATUS_ADD)
5520 memcpy(&(*new_te)->id, ct->staged_blob_id,
5521 sizeof((*new_te)->id));
5523 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5526 if (err && *new_te) {
5533 static const struct got_error *
5534 insert_tree_entry(struct got_tree_entry *new_te,
5535 struct got_pathlist_head *paths)
5537 const struct got_error *err = NULL;
5538 struct got_pathlist_entry *new_pe;
5540 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5544 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5548 static const struct got_error *
5549 report_ct_status(struct got_commitable *ct,
5550 got_worktree_status_cb status_cb, void *status_arg)
5552 const char *ct_path = ct->path;
5553 unsigned char status;
5555 if (status_cb == NULL) /* no commit progress output desired */
5558 while (ct_path[0] == '/')
5561 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5562 status = ct->staged_status;
5564 status = ct->status;
5566 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5567 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5570 static const struct got_error *
5571 match_modified_subtree(int *modified, struct got_tree_entry *te,
5572 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5574 const struct got_error *err = NULL;
5575 struct got_pathlist_entry *pe;
5580 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5581 got_path_is_root_dir(base_tree_path) ? "" : "/",
5583 return got_error_from_errno("asprintf");
5585 TAILQ_FOREACH(pe, commitable_paths, entry) {
5586 struct got_commitable *ct = pe->data;
5587 *modified = got_path_is_child(ct->in_repo_path, te_path,
5597 static const struct got_error *
5598 match_deleted_or_modified_ct(struct got_commitable **ctp,
5599 struct got_tree_entry *te, const char *base_tree_path,
5600 struct got_pathlist_head *commitable_paths)
5602 const struct got_error *err = NULL;
5603 struct got_pathlist_entry *pe;
5607 TAILQ_FOREACH(pe, commitable_paths, entry) {
5608 struct got_commitable *ct = pe->data;
5609 char *ct_name = NULL;
5612 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5613 if (ct->status != GOT_STATUS_MODIFY &&
5614 ct->status != GOT_STATUS_MODE_CHANGE &&
5615 ct->status != GOT_STATUS_DELETE &&
5616 ct->status != GOT_STATUS_CONFLICT)
5619 if (ct->staged_status != GOT_STATUS_MODIFY &&
5620 ct->staged_status != GOT_STATUS_DELETE)
5624 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5627 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5633 err = got_path_basename(&ct_name, pe->path);
5637 if (strcmp(te->name, ct_name) != 0) {
5650 static const struct got_error *
5651 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5652 const char *child_path, const char *path_base_tree,
5653 struct got_pathlist_head *commitable_paths,
5654 got_worktree_status_cb status_cb, void *status_arg,
5655 struct got_repository *repo)
5657 const struct got_error *err = NULL;
5658 struct got_tree_entry *new_te;
5660 struct got_object_id *id = NULL;
5665 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5666 got_path_is_root_dir(path_base_tree) ? "" : "/",
5668 return got_error_from_errno("asprintf");
5670 new_te = calloc(1, sizeof(*new_te));
5672 return got_error_from_errno("calloc");
5673 new_te->mode = S_IFDIR;
5675 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5676 sizeof(new_te->name)) {
5677 err = got_error(GOT_ERR_NO_SPACE);
5680 err = write_tree(&id, &nentries, NULL, subtree_path,
5681 commitable_paths, status_cb, status_arg, repo);
5686 memcpy(&new_te->id, id, sizeof(new_te->id));
5695 static const struct got_error *
5696 write_tree(struct got_object_id **new_tree_id, int *nentries,
5697 struct got_tree_object *base_tree, const char *path_base_tree,
5698 struct got_pathlist_head *commitable_paths,
5699 got_worktree_status_cb status_cb, void *status_arg,
5700 struct got_repository *repo)
5702 const struct got_error *err = NULL;
5703 struct got_pathlist_head paths;
5704 struct got_tree_entry *te, *new_te = NULL;
5705 struct got_pathlist_entry *pe;
5710 /* Insert, and recurse into, newly added entries first. */
5711 TAILQ_FOREACH(pe, commitable_paths, entry) {
5712 struct got_commitable *ct = pe->data;
5713 char *child_path = NULL, *slash;
5715 if ((ct->status != GOT_STATUS_ADD &&
5716 ct->staged_status != GOT_STATUS_ADD) ||
5717 (ct->flags & GOT_COMMITABLE_ADDED))
5720 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5721 strlen(path_base_tree)))
5724 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5729 slash = strchr(child_path, '/');
5730 if (slash == NULL) {
5731 err = alloc_added_blob_tree_entry(&new_te, ct);
5734 err = report_ct_status(ct, status_cb, status_arg);
5737 ct->flags |= GOT_COMMITABLE_ADDED;
5738 err = insert_tree_entry(new_te, &paths);
5743 *slash = '\0'; /* trim trailing path components */
5744 if (base_tree == NULL ||
5745 got_object_tree_find_entry(base_tree, child_path)
5747 err = make_subtree_for_added_blob(&new_te,
5748 child_path, path_base_tree,
5749 commitable_paths, status_cb, status_arg,
5753 err = insert_tree_entry(new_te, &paths);
5762 int i, nbase_entries;
5763 /* Handle modified and deleted entries. */
5764 nbase_entries = got_object_tree_get_nentries(base_tree);
5765 for (i = 0; i < nbase_entries; i++) {
5766 struct got_commitable *ct = NULL;
5768 te = got_object_tree_get_entry(base_tree, i);
5769 if (got_object_tree_entry_is_submodule(te)) {
5770 /* Entry is a submodule; just copy it. */
5771 err = got_object_tree_entry_dup(&new_te, te);
5774 err = insert_tree_entry(new_te, &paths);
5781 if (S_ISDIR(te->mode)) {
5783 err = got_object_tree_entry_dup(&new_te, te);
5786 err = match_modified_subtree(&modified, te,
5787 path_base_tree, commitable_paths);
5790 /* Avoid recursion into unmodified subtrees. */
5792 struct got_object_id *new_id;
5794 err = write_subtree(&new_id,
5796 path_base_tree, commitable_paths,
5797 status_cb, status_arg, repo);
5800 if (nsubentries == 0) {
5801 /* All entries were deleted. */
5805 memcpy(&new_te->id, new_id,
5806 sizeof(new_te->id));
5809 err = insert_tree_entry(new_te, &paths);
5816 err = match_deleted_or_modified_ct(&ct, te,
5817 path_base_tree, commitable_paths);
5821 /* NB: Deleted entries get dropped here. */
5822 if (ct->status == GOT_STATUS_MODIFY ||
5823 ct->status == GOT_STATUS_MODE_CHANGE ||
5824 ct->status == GOT_STATUS_CONFLICT ||
5825 ct->staged_status == GOT_STATUS_MODIFY) {
5826 err = alloc_modified_blob_tree_entry(
5830 err = insert_tree_entry(new_te, &paths);
5835 err = report_ct_status(ct, status_cb,
5840 /* Entry is unchanged; just copy it. */
5841 err = got_object_tree_entry_dup(&new_te, te);
5844 err = insert_tree_entry(new_te, &paths);
5852 /* Write new list of entries; deleted entries have been dropped. */
5853 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5855 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
5859 static const struct got_error *
5860 update_fileindex_after_commit(struct got_worktree *worktree,
5861 struct got_pathlist_head *commitable_paths,
5862 struct got_object_id *new_base_commit_id,
5863 struct got_fileindex *fileindex, int have_staged_files)
5865 const struct got_error *err = NULL;
5866 struct got_pathlist_entry *pe;
5867 char *relpath = NULL;
5869 TAILQ_FOREACH(pe, commitable_paths, entry) {
5870 struct got_fileindex_entry *ie;
5871 struct got_commitable *ct = pe->data;
5873 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5875 err = got_path_skip_common_ancestor(&relpath,
5876 worktree->root_path, ct->ondisk_path);
5881 if (ct->status == GOT_STATUS_DELETE ||
5882 ct->staged_status == GOT_STATUS_DELETE) {
5883 got_fileindex_entry_remove(fileindex, ie);
5884 } else if (ct->staged_status == GOT_STATUS_ADD ||
5885 ct->staged_status == GOT_STATUS_MODIFY) {
5886 got_fileindex_entry_stage_set(ie,
5887 GOT_FILEIDX_STAGE_NONE);
5888 got_fileindex_entry_staged_filetype_set(ie, 0);
5890 err = got_fileindex_entry_update(ie,
5891 worktree->root_fd, relpath,
5892 ct->staged_blob_id->sha1,
5893 new_base_commit_id->sha1,
5894 !have_staged_files);
5896 err = got_fileindex_entry_update(ie,
5897 worktree->root_fd, relpath,
5899 new_base_commit_id->sha1,
5900 !have_staged_files);
5902 err = got_fileindex_entry_alloc(&ie, pe->path);
5905 err = got_fileindex_entry_update(ie,
5906 worktree->root_fd, relpath, ct->blob_id->sha1,
5907 new_base_commit_id->sha1, 1);
5909 got_fileindex_entry_free(ie);
5912 err = got_fileindex_entry_add(fileindex, ie);
5914 got_fileindex_entry_free(ie);
5927 static const struct got_error *
5928 check_out_of_date(const char *in_repo_path, unsigned char status,
5929 unsigned char staged_status, struct got_object_id *base_blob_id,
5930 struct got_object_id *base_commit_id,
5931 struct got_object_id *head_commit_id, struct got_repository *repo,
5934 const struct got_error *err = NULL;
5935 struct got_commit_object *commit = NULL;
5936 struct got_object_id *id = NULL;
5938 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5939 /* Trivial case: base commit == head commit */
5940 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5943 * Ensure file content which local changes were based
5944 * on matches file content in the branch head.
5946 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5949 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5951 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5952 err = got_error(ood_errcode);
5954 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5955 err = got_error(ood_errcode);
5957 /* Require that added files don't exist in the branch head. */
5958 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5961 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5962 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5964 err = id ? got_error(ood_errcode) : NULL;
5969 got_object_commit_close(commit);
5973 static const struct got_error *
5974 commit_worktree(struct got_object_id **new_commit_id,
5975 struct got_pathlist_head *commitable_paths,
5976 struct got_object_id *head_commit_id,
5977 struct got_object_id *parent_id2,
5978 struct got_worktree *worktree,
5979 const char *author, const char *committer, char *diff_path,
5980 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5981 got_worktree_status_cb status_cb, void *status_arg,
5982 struct got_repository *repo)
5984 const struct got_error *err = NULL, *unlockerr = NULL;
5985 struct got_pathlist_entry *pe;
5986 const char *head_ref_name = NULL;
5987 struct got_commit_object *head_commit = NULL;
5988 struct got_reference *head_ref2 = NULL;
5989 struct got_object_id *head_commit_id2 = NULL;
5990 struct got_tree_object *head_tree = NULL;
5991 struct got_object_id *new_tree_id = NULL;
5992 int nentries, nparents = 0;
5993 struct got_object_id_queue parent_ids;
5994 struct got_object_qid *pid = NULL;
5995 char *logmsg = NULL;
5998 *new_commit_id = NULL;
6000 STAILQ_INIT(&parent_ids);
6002 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6006 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6010 if (commit_msg_cb != NULL) {
6011 err = commit_msg_cb(commitable_paths, diff_path,
6012 &logmsg, commit_arg);
6017 if (logmsg == NULL || strlen(logmsg) == 0) {
6018 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6022 /* Create blobs from added and modified files and record their IDs. */
6023 TAILQ_FOREACH(pe, commitable_paths, entry) {
6024 struct got_commitable *ct = pe->data;
6027 /* Blobs for staged files already exist. */
6028 if (ct->staged_status == GOT_STATUS_ADD ||
6029 ct->staged_status == GOT_STATUS_MODIFY)
6032 if (ct->status != GOT_STATUS_ADD &&
6033 ct->status != GOT_STATUS_MODIFY &&
6034 ct->status != GOT_STATUS_MODE_CHANGE &&
6035 ct->status != GOT_STATUS_CONFLICT)
6038 if (asprintf(&ondisk_path, "%s/%s",
6039 worktree->root_path, pe->path) == -1) {
6040 err = got_error_from_errno("asprintf");
6043 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6049 /* Recursively write new tree objects. */
6050 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6051 commitable_paths, status_cb, status_arg, repo);
6055 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
6058 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6061 err = got_object_qid_alloc(&pid, parent_id2);
6064 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6067 timestamp = time(NULL);
6068 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6069 nparents, author, timestamp, committer, timestamp, logmsg, repo);
6075 /* Check if a concurrent commit to our branch has occurred. */
6076 head_ref_name = got_worktree_get_head_ref_name(worktree);
6077 if (head_ref_name == NULL) {
6078 err = got_error_from_errno("got_worktree_get_head_ref_name");
6081 /* Lock the reference here to prevent concurrent modification. */
6082 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6085 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6088 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6089 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6092 /* Update branch head in repository. */
6093 err = got_ref_change_ref(head_ref2, *new_commit_id);
6096 err = got_ref_write(head_ref2, repo);
6100 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6104 err = ref_base_commit(worktree, repo);
6108 got_object_id_queue_free(&parent_ids);
6110 got_object_tree_close(head_tree);
6112 got_object_commit_close(head_commit);
6113 free(head_commit_id2);
6115 unlockerr = got_ref_unlock(head_ref2);
6116 if (unlockerr && err == NULL)
6118 got_ref_close(head_ref2);
6123 static const struct got_error *
6124 check_path_is_commitable(const char *path,
6125 struct got_pathlist_head *commitable_paths)
6127 struct got_pathlist_entry *cpe = NULL;
6128 size_t path_len = strlen(path);
6130 TAILQ_FOREACH(cpe, commitable_paths, entry) {
6131 struct got_commitable *ct = cpe->data;
6132 const char *ct_path = ct->path;
6134 while (ct_path[0] == '/')
6137 if (strcmp(path, ct_path) == 0 ||
6138 got_path_is_child(ct_path, path, path_len))
6143 return got_error_path(path, GOT_ERR_BAD_PATH);
6148 static const struct got_error *
6149 check_staged_file(void *arg, struct got_fileindex_entry *ie)
6151 int *have_staged_files = arg;
6153 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6154 *have_staged_files = 1;
6155 return got_error(GOT_ERR_CANCELLED);
6161 static const struct got_error *
6162 check_non_staged_files(struct got_fileindex *fileindex,
6163 struct got_pathlist_head *paths)
6165 struct got_pathlist_entry *pe;
6166 struct got_fileindex_entry *ie;
6168 TAILQ_FOREACH(pe, paths, entry) {
6169 if (pe->path[0] == '\0')
6171 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6173 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6174 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6175 return got_error_path(pe->path,
6176 GOT_ERR_FILE_NOT_STAGED);
6182 const struct got_error *
6183 got_worktree_commit(struct got_object_id **new_commit_id,
6184 struct got_worktree *worktree, struct got_pathlist_head *paths,
6185 const char *author, const char *committer, int allow_bad_symlinks,
6186 int show_diff, int commit_conflicts,
6187 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6188 got_worktree_status_cb status_cb, void *status_arg,
6189 struct got_repository *repo)
6191 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6192 struct got_fileindex *fileindex = NULL;
6193 char *fileindex_path = NULL;
6194 struct got_pathlist_head commitable_paths;
6195 struct collect_commitables_arg cc_arg;
6196 struct got_pathlist_entry *pe;
6197 struct got_reference *head_ref = NULL;
6198 struct got_object_id *head_commit_id = NULL;
6199 char *diff_path = NULL;
6200 int have_staged_files = 0;
6202 *new_commit_id = NULL;
6204 memset(&cc_arg, 0, sizeof(cc_arg));
6205 TAILQ_INIT(&commitable_paths);
6207 err = lock_worktree(worktree, LOCK_EX);
6211 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6215 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6219 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6223 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6224 &have_staged_files);
6225 if (err && err->code != GOT_ERR_CANCELLED)
6227 if (have_staged_files) {
6228 err = check_non_staged_files(fileindex, paths);
6233 cc_arg.commitable_paths = &commitable_paths;
6234 cc_arg.worktree = worktree;
6235 cc_arg.fileindex = fileindex;
6237 cc_arg.have_staged_files = have_staged_files;
6238 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6239 cc_arg.diff_header_shown = 0;
6240 cc_arg.commit_conflicts = commit_conflicts;
6242 err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6243 GOT_TMPDIR_STR "/got", ".diff");
6246 cc_arg.f1 = got_opentemp();
6247 if (cc_arg.f1 == NULL) {
6248 err = got_error_from_errno("got_opentemp");
6251 cc_arg.f2 = got_opentemp();
6252 if (cc_arg.f2 == NULL) {
6253 err = got_error_from_errno("got_opentemp");
6258 TAILQ_FOREACH(pe, paths, entry) {
6259 err = worktree_status(worktree, pe->path, fileindex, repo,
6260 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6266 if (fflush(cc_arg.diff_outfile) == EOF) {
6267 err = got_error_from_errno("fflush");
6272 if (TAILQ_EMPTY(&commitable_paths)) {
6273 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6277 TAILQ_FOREACH(pe, paths, entry) {
6278 err = check_path_is_commitable(pe->path, &commitable_paths);
6283 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6284 struct got_commitable *ct = pe->data;
6285 const char *ct_path = ct->in_repo_path;
6287 while (ct_path[0] == '/')
6289 err = check_out_of_date(ct_path, ct->status,
6290 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6291 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6297 err = commit_worktree(new_commit_id, &commitable_paths,
6298 head_commit_id, NULL, worktree, author, committer,
6299 (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6300 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6304 err = update_fileindex_after_commit(worktree, &commitable_paths,
6305 *new_commit_id, fileindex, have_staged_files);
6306 sync_err = sync_fileindex(fileindex, fileindex_path);
6307 if (sync_err && err == NULL)
6311 got_fileindex_free(fileindex);
6312 free(fileindex_path);
6313 unlockerr = lock_worktree(worktree, LOCK_SH);
6314 if (unlockerr && err == NULL)
6316 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6317 struct got_commitable *ct = pe->data;
6319 free_commitable(ct);
6321 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6322 if (diff_path && unlink(diff_path) == -1 && err == NULL)
6323 err = got_error_from_errno2("unlink", diff_path);
6325 if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6327 err = got_error_from_errno("fclose");
6332 got_commitable_get_path(struct got_commitable *ct)
6338 got_commitable_get_status(struct got_commitable *ct)
6343 struct check_rebase_ok_arg {
6344 struct got_worktree *worktree;
6345 struct got_repository *repo;
6348 static const struct got_error *
6349 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6351 const struct got_error *err = NULL;
6352 struct check_rebase_ok_arg *a = arg;
6353 unsigned char status;
6357 /* Reject rebase of a work tree with mixed base commits. */
6358 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6359 SHA1_DIGEST_LENGTH))
6360 return got_error(GOT_ERR_MIXED_COMMITS);
6362 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6364 return got_error_from_errno("asprintf");
6366 /* Reject rebase of a work tree with modified or staged files. */
6367 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6372 if (status != GOT_STATUS_NO_CHANGE)
6373 return got_error(GOT_ERR_MODIFIED);
6374 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6375 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6380 const struct got_error *
6381 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6382 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6383 struct got_worktree *worktree, struct got_reference *branch,
6384 struct got_repository *repo)
6386 const struct got_error *err = NULL;
6387 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6388 char *branch_ref_name = NULL;
6389 char *fileindex_path = NULL;
6390 struct check_rebase_ok_arg ok_arg;
6391 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6392 struct got_object_id *wt_branch_tip = NULL;
6394 *new_base_branch_ref = NULL;
6398 err = lock_worktree(worktree, LOCK_EX);
6402 err = open_fileindex(fileindex, &fileindex_path, worktree);
6406 ok_arg.worktree = worktree;
6408 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6413 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6417 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6421 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6425 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6430 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6433 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6434 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6438 err = got_ref_alloc_symref(new_base_branch_ref,
6439 new_base_branch_ref_name, wt_branch);
6442 err = got_ref_write(*new_base_branch_ref, repo);
6446 /* TODO Lock original branch's ref while rebasing? */
6448 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6452 err = got_ref_write(branch_ref, repo);
6456 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6457 worktree->base_commit_id);
6460 err = got_ref_write(*tmp_branch, repo);
6464 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6468 free(fileindex_path);
6469 free(tmp_branch_name);
6470 free(new_base_branch_ref_name);
6471 free(branch_ref_name);
6473 got_ref_close(branch_ref);
6475 got_ref_close(wt_branch);
6476 free(wt_branch_tip);
6478 if (*new_base_branch_ref) {
6479 got_ref_close(*new_base_branch_ref);
6480 *new_base_branch_ref = NULL;
6483 got_ref_close(*tmp_branch);
6487 got_fileindex_free(*fileindex);
6490 lock_worktree(worktree, LOCK_SH);
6495 const struct got_error *
6496 got_worktree_rebase_continue(struct got_object_id **commit_id,
6497 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6498 struct got_reference **branch, struct got_fileindex **fileindex,
6499 struct got_worktree *worktree, struct got_repository *repo)
6501 const struct got_error *err;
6502 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6503 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6504 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6505 char *fileindex_path = NULL;
6506 int have_staged_files = 0;
6509 *new_base_branch = NULL;
6514 err = lock_worktree(worktree, LOCK_EX);
6518 err = open_fileindex(fileindex, &fileindex_path, worktree);
6522 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6523 &have_staged_files);
6524 if (err && err->code != GOT_ERR_CANCELLED)
6526 if (have_staged_files) {
6527 err = got_error(GOT_ERR_STAGED_PATHS);
6531 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6535 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6539 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6543 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6547 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6551 err = got_ref_open(branch, repo,
6552 got_ref_get_symref_target(branch_ref), 0);
6556 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6560 err = got_ref_resolve(commit_id, repo, commit_ref);
6564 err = got_ref_open(new_base_branch, repo,
6565 new_base_branch_ref_name, 0);
6569 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6573 free(commit_ref_name);
6574 free(branch_ref_name);
6575 free(fileindex_path);
6577 got_ref_close(commit_ref);
6579 got_ref_close(branch_ref);
6584 got_ref_close(*tmp_branch);
6587 if (*new_base_branch) {
6588 got_ref_close(*new_base_branch);
6589 *new_base_branch = NULL;
6592 got_ref_close(*branch);
6596 got_fileindex_free(*fileindex);
6599 lock_worktree(worktree, LOCK_SH);
6604 const struct got_error *
6605 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6607 const struct got_error *err;
6608 char *tmp_branch_name = NULL;
6610 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6614 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6615 free(tmp_branch_name);
6619 static const struct got_error *
6620 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6621 const char *diff_path, char **logmsg, void *arg)
6627 static const struct got_error *
6628 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6629 const char *path, struct got_object_id *blob_id,
6630 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6631 int dirfd, const char *de_name)
6636 struct collect_merged_paths_arg {
6637 got_worktree_checkout_cb progress_cb;
6639 struct got_pathlist_head *merged_paths;
6642 static const struct got_error *
6643 collect_merged_paths(void *arg, unsigned char status, const char *path)
6645 const struct got_error *err;
6646 struct collect_merged_paths_arg *a = arg;
6648 struct got_pathlist_entry *new;
6650 err = (*a->progress_cb)(a->progress_arg, status, path);
6654 if (status != GOT_STATUS_MERGE &&
6655 status != GOT_STATUS_ADD &&
6656 status != GOT_STATUS_DELETE &&
6657 status != GOT_STATUS_CONFLICT)
6662 return got_error_from_errno("strdup");
6664 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6665 if (err || new == NULL)
6670 static const struct got_error *
6671 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6672 int is_rebase, struct got_repository *repo)
6674 const struct got_error *err;
6675 struct got_reference *commit_ref = NULL;
6677 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6679 if (err->code != GOT_ERR_NOT_REF)
6681 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6684 err = got_ref_write(commit_ref, repo);
6687 } else if (is_rebase) {
6688 struct got_object_id *stored_id;
6691 err = got_ref_resolve(&stored_id, repo, commit_ref);
6694 cmp = got_object_id_cmp(commit_id, stored_id);
6697 err = got_error(GOT_ERR_REBASE_COMMITID);
6703 got_ref_close(commit_ref);
6707 static const struct got_error *
6708 rebase_merge_files(struct got_pathlist_head *merged_paths,
6709 const char *commit_ref_name, struct got_worktree *worktree,
6710 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6711 struct got_object_id *commit_id, struct got_repository *repo,
6712 got_worktree_checkout_cb progress_cb, void *progress_arg,
6713 got_cancel_cb cancel_cb, void *cancel_arg)
6715 const struct got_error *err;
6716 struct got_reference *commit_ref = NULL;
6717 struct collect_merged_paths_arg cmp_arg;
6718 char *fileindex_path;
6720 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6722 err = get_fileindex_path(&fileindex_path, worktree);
6726 cmp_arg.progress_cb = progress_cb;
6727 cmp_arg.progress_arg = progress_arg;
6728 cmp_arg.merged_paths = merged_paths;
6729 err = merge_files(worktree, fileindex, fileindex_path,
6730 parent_commit_id, commit_id, repo, collect_merged_paths,
6731 &cmp_arg, cancel_cb, cancel_arg);
6733 got_ref_close(commit_ref);
6737 const struct got_error *
6738 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6739 struct got_worktree *worktree, struct got_fileindex *fileindex,
6740 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6741 struct got_repository *repo,
6742 got_worktree_checkout_cb progress_cb, void *progress_arg,
6743 got_cancel_cb cancel_cb, void *cancel_arg)
6745 const struct got_error *err;
6746 char *commit_ref_name;
6748 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6752 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6756 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6757 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6758 progress_arg, cancel_cb, cancel_arg);
6760 free(commit_ref_name);
6764 const struct got_error *
6765 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6766 struct got_worktree *worktree, struct got_fileindex *fileindex,
6767 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6768 struct got_repository *repo,
6769 got_worktree_checkout_cb progress_cb, void *progress_arg,
6770 got_cancel_cb cancel_cb, void *cancel_arg)
6772 const struct got_error *err;
6773 char *commit_ref_name;
6775 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6779 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6783 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6784 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6785 progress_arg, cancel_cb, cancel_arg);
6787 free(commit_ref_name);
6791 static const struct got_error *
6792 rebase_commit(struct got_object_id **new_commit_id,
6793 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6794 struct got_worktree *worktree, struct got_fileindex *fileindex,
6795 struct got_reference *tmp_branch, const char *committer,
6796 struct got_commit_object *orig_commit, const char *new_logmsg,
6797 int allow_conflict, struct got_repository *repo)
6799 const struct got_error *err, *sync_err;
6800 struct got_pathlist_head commitable_paths;
6801 struct collect_commitables_arg cc_arg;
6802 char *fileindex_path = NULL;
6803 struct got_reference *head_ref = NULL;
6804 struct got_object_id *head_commit_id = NULL;
6805 char *logmsg = NULL;
6807 memset(&cc_arg, 0, sizeof(cc_arg));
6808 TAILQ_INIT(&commitable_paths);
6809 *new_commit_id = NULL;
6811 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6813 err = get_fileindex_path(&fileindex_path, worktree);
6817 cc_arg.commitable_paths = &commitable_paths;
6818 cc_arg.worktree = worktree;
6820 cc_arg.have_staged_files = 0;
6821 cc_arg.commit_conflicts = allow_conflict;
6823 * If possible get the status of individual files directly to
6824 * avoid crawling the entire work tree once per rebased commit.
6826 * Ideally, merged_paths would contain a list of commitables
6827 * we could use so we could skip worktree_status() entirely.
6828 * However, we would then need carefully keep track of cumulative
6829 * effects of operations such as file additions and deletions
6830 * in 'got histedit -f' (folding multiple commits into one),
6831 * and this extra complexity is not really worth it.
6834 struct got_pathlist_entry *pe;
6835 TAILQ_FOREACH(pe, merged_paths, entry) {
6836 err = worktree_status(worktree, pe->path, fileindex,
6837 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6843 err = worktree_status(worktree, "", fileindex, repo,
6844 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6849 if (TAILQ_EMPTY(&commitable_paths)) {
6850 /* No-op change; commit will be elided. */
6851 err = got_ref_delete(commit_ref, repo);
6854 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6858 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6862 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6867 logmsg = strdup(new_logmsg);
6868 if (logmsg == NULL) {
6869 err = got_error_from_errno("strdup");
6873 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6878 /* NB: commit_worktree will call free(logmsg) */
6879 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6880 NULL, worktree, got_object_commit_get_author(orig_commit),
6881 committer ? committer :
6882 got_object_commit_get_committer(orig_commit), NULL,
6883 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6887 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6891 err = got_ref_delete(commit_ref, repo);
6895 err = update_fileindex_after_commit(worktree, &commitable_paths,
6896 *new_commit_id, fileindex, 0);
6897 sync_err = sync_fileindex(fileindex, fileindex_path);
6898 if (sync_err && err == NULL)
6901 free(fileindex_path);
6902 free(head_commit_id);
6904 got_ref_close(head_ref);
6906 free(*new_commit_id);
6907 *new_commit_id = NULL;
6912 const struct got_error *
6913 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6914 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6915 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6916 const char *committer, struct got_commit_object *orig_commit,
6917 struct got_object_id *orig_commit_id, int allow_conflict,
6918 struct got_repository *repo)
6920 const struct got_error *err;
6921 char *commit_ref_name;
6922 struct got_reference *commit_ref = NULL;
6923 struct got_object_id *commit_id = NULL;
6925 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6929 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6932 err = got_ref_resolve(&commit_id, repo, commit_ref);
6935 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6936 err = got_error(GOT_ERR_REBASE_COMMITID);
6940 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6941 worktree, fileindex, tmp_branch, committer, orig_commit,
6942 NULL, allow_conflict, repo);
6945 got_ref_close(commit_ref);
6946 free(commit_ref_name);
6951 const struct got_error *
6952 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6953 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6954 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6955 const char *committer, struct got_commit_object *orig_commit,
6956 struct got_object_id *orig_commit_id, const char *new_logmsg,
6957 int allow_conflict, struct got_repository *repo)
6959 const struct got_error *err;
6960 char *commit_ref_name;
6961 struct got_reference *commit_ref = NULL;
6963 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6967 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6971 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6972 worktree, fileindex, tmp_branch, committer, orig_commit,
6973 new_logmsg, allow_conflict, repo);
6976 got_ref_close(commit_ref);
6977 free(commit_ref_name);
6981 const struct got_error *
6982 got_worktree_rebase_postpone(struct got_worktree *worktree,
6983 struct got_fileindex *fileindex)
6986 got_fileindex_free(fileindex);
6987 return lock_worktree(worktree, LOCK_SH);
6990 static const struct got_error *
6991 delete_ref(const char *name, struct got_repository *repo)
6993 const struct got_error *err;
6994 struct got_reference *ref;
6996 err = got_ref_open(&ref, repo, name, 0);
6998 if (err->code == GOT_ERR_NOT_REF)
7003 err = got_ref_delete(ref, repo);
7008 static const struct got_error *
7009 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7011 const struct got_error *err;
7012 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7013 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7015 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7018 err = delete_ref(tmp_branch_name, repo);
7022 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7025 err = delete_ref(new_base_branch_ref_name, repo);
7029 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7032 err = delete_ref(branch_ref_name, repo);
7036 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7039 err = delete_ref(commit_ref_name, repo);
7044 free(tmp_branch_name);
7045 free(new_base_branch_ref_name);
7046 free(branch_ref_name);
7047 free(commit_ref_name);
7051 static const struct got_error *
7052 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7053 struct got_object_id *new_commit_id, struct got_repository *repo)
7055 const struct got_error *err;
7056 struct got_reference *ref = NULL;
7057 struct got_object_id *old_commit_id = NULL;
7058 const char *branch_name = NULL;
7059 char *new_id_str = NULL;
7060 char *refname = NULL;
7062 branch_name = got_ref_get_name(branch);
7063 if (strncmp(branch_name, "refs/heads/", 11) != 0)
7064 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7067 err = got_object_id_str(&new_id_str, new_commit_id);
7071 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7072 new_id_str) == -1) {
7073 err = got_error_from_errno("asprintf");
7077 err = got_ref_resolve(&old_commit_id, repo, branch);
7081 err = got_ref_alloc(&ref, refname, old_commit_id);
7085 err = got_ref_write(ref, repo);
7089 free(old_commit_id);
7095 const struct got_error *
7096 got_worktree_rebase_complete(struct got_worktree *worktree,
7097 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7098 struct got_reference *rebased_branch, struct got_repository *repo,
7101 const struct got_error *err, *unlockerr, *sync_err;
7102 struct got_object_id *new_head_commit_id = NULL;
7103 char *fileindex_path = NULL;
7105 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7109 if (create_backup) {
7110 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7111 rebased_branch, new_head_commit_id, repo);
7116 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7120 err = got_ref_write(rebased_branch, repo);
7124 err = got_worktree_set_head_ref(worktree, rebased_branch);
7128 err = delete_rebase_refs(worktree, repo);
7132 err = get_fileindex_path(&fileindex_path, worktree);
7135 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7136 sync_err = sync_fileindex(fileindex, fileindex_path);
7137 if (sync_err && err == NULL)
7140 got_fileindex_free(fileindex);
7141 free(fileindex_path);
7142 free(new_head_commit_id);
7143 unlockerr = lock_worktree(worktree, LOCK_SH);
7144 if (unlockerr && err == NULL)
7149 const struct got_error *
7150 got_worktree_rebase_abort(struct got_worktree *worktree,
7151 struct got_fileindex *fileindex, struct got_repository *repo,
7152 struct got_reference *new_base_branch,
7153 got_worktree_checkout_cb progress_cb, void *progress_arg)
7155 const struct got_error *err, *unlockerr, *sync_err;
7156 struct got_reference *resolved = NULL;
7157 struct got_object_id *commit_id = NULL;
7158 struct got_commit_object *commit = NULL;
7159 char *fileindex_path = NULL;
7160 struct revert_file_args rfa;
7161 struct got_object_id *tree_id = NULL;
7163 err = lock_worktree(worktree, LOCK_EX);
7167 err = got_object_open_as_commit(&commit, repo,
7168 worktree->base_commit_id);
7172 err = got_ref_open(&resolved, repo,
7173 got_ref_get_symref_target(new_base_branch), 0);
7177 err = got_worktree_set_head_ref(worktree, resolved);
7182 * XXX commits to the base branch could have happened while
7183 * we were busy rebasing; should we store the original commit ID
7184 * when rebase begins and read it back here?
7186 err = got_ref_resolve(&commit_id, repo, resolved);
7190 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7194 err = got_object_id_by_path(&tree_id, repo, commit,
7195 worktree->path_prefix);
7199 err = delete_rebase_refs(worktree, repo);
7203 err = get_fileindex_path(&fileindex_path, worktree);
7207 rfa.worktree = worktree;
7208 rfa.fileindex = fileindex;
7209 rfa.progress_cb = progress_cb;
7210 rfa.progress_arg = progress_arg;
7211 rfa.patch_cb = NULL;
7212 rfa.patch_arg = NULL;
7214 rfa.unlink_added_files = 0;
7215 err = worktree_status(worktree, "", fileindex, repo,
7216 revert_file, &rfa, NULL, NULL, 1, 0);
7220 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7221 repo, progress_cb, progress_arg, NULL, NULL);
7223 sync_err = sync_fileindex(fileindex, fileindex_path);
7224 if (sync_err && err == NULL)
7227 got_ref_close(resolved);
7231 got_object_commit_close(commit);
7233 got_fileindex_free(fileindex);
7234 free(fileindex_path);
7236 unlockerr = lock_worktree(worktree, LOCK_SH);
7237 if (unlockerr && err == NULL)
7242 const struct got_error *
7243 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7244 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7245 struct got_fileindex **fileindex, struct got_worktree *worktree,
7246 struct got_repository *repo)
7248 const struct got_error *err = NULL;
7249 char *tmp_branch_name = NULL;
7250 char *branch_ref_name = NULL;
7251 char *base_commit_ref_name = NULL;
7252 char *fileindex_path = NULL;
7253 struct check_rebase_ok_arg ok_arg;
7254 struct got_reference *wt_branch = NULL;
7255 struct got_reference *base_commit_ref = NULL;
7259 *base_commit_id = NULL;
7262 err = lock_worktree(worktree, LOCK_EX);
7266 err = open_fileindex(fileindex, &fileindex_path, worktree);
7270 ok_arg.worktree = worktree;
7272 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7277 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7281 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7285 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7290 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7295 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7299 err = got_ref_write(*branch_ref, repo);
7303 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7304 worktree->base_commit_id);
7307 err = got_ref_write(base_commit_ref, repo);
7310 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7311 if (*base_commit_id == NULL) {
7312 err = got_error_from_errno("got_object_id_dup");
7316 err = got_ref_alloc(tmp_branch, tmp_branch_name,
7317 worktree->base_commit_id);
7320 err = got_ref_write(*tmp_branch, repo);
7324 err = got_worktree_set_head_ref(worktree, *tmp_branch);
7328 free(fileindex_path);
7329 free(tmp_branch_name);
7330 free(branch_ref_name);
7331 free(base_commit_ref_name);
7333 got_ref_close(wt_branch);
7336 got_ref_close(*branch_ref);
7340 got_ref_close(*tmp_branch);
7343 free(*base_commit_id);
7345 got_fileindex_free(*fileindex);
7348 lock_worktree(worktree, LOCK_SH);
7353 const struct got_error *
7354 got_worktree_histedit_postpone(struct got_worktree *worktree,
7355 struct got_fileindex *fileindex)
7358 got_fileindex_free(fileindex);
7359 return lock_worktree(worktree, LOCK_SH);
7362 const struct got_error *
7363 got_worktree_histedit_in_progress(int *in_progress,
7364 struct got_worktree *worktree)
7366 const struct got_error *err;
7367 char *tmp_branch_name = NULL;
7369 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7373 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7374 free(tmp_branch_name);
7378 const struct got_error *
7379 got_worktree_histedit_continue(struct got_object_id **commit_id,
7380 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7381 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7382 struct got_worktree *worktree, struct got_repository *repo)
7384 const struct got_error *err;
7385 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7386 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7387 struct got_reference *commit_ref = NULL;
7388 struct got_reference *base_commit_ref = NULL;
7389 char *fileindex_path = NULL;
7390 int have_staged_files = 0;
7394 *base_commit_id = NULL;
7397 err = lock_worktree(worktree, LOCK_EX);
7401 err = open_fileindex(fileindex, &fileindex_path, worktree);
7405 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7406 &have_staged_files);
7407 if (err && err->code != GOT_ERR_CANCELLED)
7409 if (have_staged_files) {
7410 err = got_error(GOT_ERR_STAGED_PATHS);
7414 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7418 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7422 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7426 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7431 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7435 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7438 err = got_ref_resolve(commit_id, repo, commit_ref);
7442 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7445 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7449 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7453 free(commit_ref_name);
7454 free(branch_ref_name);
7455 free(fileindex_path);
7457 got_ref_close(commit_ref);
7458 if (base_commit_ref)
7459 got_ref_close(base_commit_ref);
7463 free(*base_commit_id);
7464 *base_commit_id = NULL;
7466 got_ref_close(*tmp_branch);
7470 got_fileindex_free(*fileindex);
7473 lock_worktree(worktree, LOCK_EX);
7478 static const struct got_error *
7479 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7481 const struct got_error *err;
7482 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7483 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7485 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7488 err = delete_ref(tmp_branch_name, repo);
7492 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7496 err = delete_ref(base_commit_ref_name, repo);
7500 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7503 err = delete_ref(branch_ref_name, repo);
7507 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7510 err = delete_ref(commit_ref_name, repo);
7514 free(tmp_branch_name);
7515 free(base_commit_ref_name);
7516 free(branch_ref_name);
7517 free(commit_ref_name);
7521 const struct got_error *
7522 got_worktree_histedit_abort(struct got_worktree *worktree,
7523 struct got_fileindex *fileindex, struct got_repository *repo,
7524 struct got_reference *branch, struct got_object_id *base_commit_id,
7525 got_worktree_checkout_cb progress_cb, void *progress_arg)
7527 const struct got_error *err, *unlockerr, *sync_err;
7528 struct got_reference *resolved = NULL;
7529 char *fileindex_path = NULL;
7530 struct got_commit_object *commit = NULL;
7531 struct got_object_id *tree_id = NULL;
7532 struct revert_file_args rfa;
7534 err = lock_worktree(worktree, LOCK_EX);
7538 err = got_object_open_as_commit(&commit, repo,
7539 worktree->base_commit_id);
7543 err = got_ref_open(&resolved, repo,
7544 got_ref_get_symref_target(branch), 0);
7548 err = got_worktree_set_head_ref(worktree, resolved);
7552 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7556 err = got_object_id_by_path(&tree_id, repo, commit,
7557 worktree->path_prefix);
7561 err = delete_histedit_refs(worktree, repo);
7565 err = get_fileindex_path(&fileindex_path, worktree);
7569 rfa.worktree = worktree;
7570 rfa.fileindex = fileindex;
7571 rfa.progress_cb = progress_cb;
7572 rfa.progress_arg = progress_arg;
7573 rfa.patch_cb = NULL;
7574 rfa.patch_arg = NULL;
7576 rfa.unlink_added_files = 0;
7577 err = worktree_status(worktree, "", fileindex, repo,
7578 revert_file, &rfa, NULL, NULL, 1, 0);
7582 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7583 repo, progress_cb, progress_arg, NULL, NULL);
7585 sync_err = sync_fileindex(fileindex, fileindex_path);
7586 if (sync_err && err == NULL)
7589 got_ref_close(resolved);
7591 free(fileindex_path);
7593 unlockerr = lock_worktree(worktree, LOCK_SH);
7594 if (unlockerr && err == NULL)
7599 const struct got_error *
7600 got_worktree_histedit_complete(struct got_worktree *worktree,
7601 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7602 struct got_reference *edited_branch, struct got_repository *repo)
7604 const struct got_error *err, *unlockerr, *sync_err;
7605 struct got_object_id *new_head_commit_id = NULL;
7606 struct got_reference *resolved = NULL;
7607 char *fileindex_path = NULL;
7609 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7613 err = got_ref_open(&resolved, repo,
7614 got_ref_get_symref_target(edited_branch), 0);
7618 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7619 resolved, new_head_commit_id, repo);
7623 err = got_ref_change_ref(resolved, new_head_commit_id);
7627 err = got_ref_write(resolved, repo);
7631 err = got_worktree_set_head_ref(worktree, resolved);
7635 err = delete_histedit_refs(worktree, repo);
7639 err = get_fileindex_path(&fileindex_path, worktree);
7642 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7643 sync_err = sync_fileindex(fileindex, fileindex_path);
7644 if (sync_err && err == NULL)
7647 got_fileindex_free(fileindex);
7648 free(fileindex_path);
7649 free(new_head_commit_id);
7650 unlockerr = lock_worktree(worktree, LOCK_SH);
7651 if (unlockerr && err == NULL)
7656 const struct got_error *
7657 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7658 struct got_object_id *commit_id, struct got_repository *repo)
7660 const struct got_error *err;
7661 char *commit_ref_name;
7663 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7667 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7671 err = delete_ref(commit_ref_name, repo);
7673 free(commit_ref_name);
7677 const struct got_error *
7678 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7679 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7680 struct got_worktree *worktree, const char *refname,
7681 struct got_repository *repo)
7683 const struct got_error *err = NULL;
7684 char *fileindex_path = NULL;
7685 struct check_rebase_ok_arg ok_arg;
7689 *base_branch_ref = NULL;
7691 err = lock_worktree(worktree, LOCK_EX);
7695 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7696 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7697 "cannot integrate a branch into itself; "
7698 "update -b or different branch name required");
7702 err = open_fileindex(fileindex, &fileindex_path, worktree);
7706 /* Preconditions are the same as for rebase. */
7707 ok_arg.worktree = worktree;
7709 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7714 err = got_ref_open(branch_ref, repo, refname, 1);
7718 err = got_ref_open(base_branch_ref, repo,
7719 got_worktree_get_head_ref_name(worktree), 1);
7723 got_ref_close(*branch_ref);
7726 if (*base_branch_ref) {
7727 got_ref_close(*base_branch_ref);
7728 *base_branch_ref = NULL;
7731 got_fileindex_free(*fileindex);
7734 lock_worktree(worktree, LOCK_SH);
7739 const struct got_error *
7740 got_worktree_integrate_continue(struct got_worktree *worktree,
7741 struct got_fileindex *fileindex, struct got_repository *repo,
7742 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7743 got_worktree_checkout_cb progress_cb, void *progress_arg,
7744 got_cancel_cb cancel_cb, void *cancel_arg)
7746 const struct got_error *err = NULL, *sync_err, *unlockerr;
7747 char *fileindex_path = NULL;
7748 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7749 struct got_commit_object *commit = NULL;
7751 err = get_fileindex_path(&fileindex_path, worktree);
7755 err = got_ref_resolve(&commit_id, repo, branch_ref);
7759 err = got_object_open_as_commit(&commit, repo, commit_id);
7763 err = got_object_id_by_path(&tree_id, repo, commit,
7764 worktree->path_prefix);
7768 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7772 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7773 progress_cb, progress_arg, cancel_cb, cancel_arg);
7777 err = got_ref_change_ref(base_branch_ref, commit_id);
7781 err = got_ref_write(base_branch_ref, repo);
7785 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7787 sync_err = sync_fileindex(fileindex, fileindex_path);
7788 if (sync_err && err == NULL)
7792 unlockerr = got_ref_unlock(branch_ref);
7793 if (unlockerr && err == NULL)
7795 got_ref_close(branch_ref);
7797 unlockerr = got_ref_unlock(base_branch_ref);
7798 if (unlockerr && err == NULL)
7800 got_ref_close(base_branch_ref);
7802 got_fileindex_free(fileindex);
7803 free(fileindex_path);
7806 got_object_commit_close(commit);
7808 unlockerr = lock_worktree(worktree, LOCK_SH);
7809 if (unlockerr && err == NULL)
7814 const struct got_error *
7815 got_worktree_integrate_abort(struct got_worktree *worktree,
7816 struct got_fileindex *fileindex, struct got_repository *repo,
7817 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7819 const struct got_error *err = NULL, *unlockerr = NULL;
7821 got_fileindex_free(fileindex);
7823 err = lock_worktree(worktree, LOCK_SH);
7825 unlockerr = got_ref_unlock(branch_ref);
7826 if (unlockerr && err == NULL)
7828 got_ref_close(branch_ref);
7830 unlockerr = got_ref_unlock(base_branch_ref);
7831 if (unlockerr && err == NULL)
7833 got_ref_close(base_branch_ref);
7838 const struct got_error *
7839 got_worktree_merge_postpone(struct got_worktree *worktree,
7840 struct got_fileindex *fileindex)
7842 const struct got_error *err, *sync_err;
7843 char *fileindex_path = NULL;
7845 err = get_fileindex_path(&fileindex_path, worktree);
7849 sync_err = sync_fileindex(fileindex, fileindex_path);
7851 err = lock_worktree(worktree, LOCK_SH);
7852 if (sync_err && err == NULL)
7855 got_fileindex_free(fileindex);
7856 free(fileindex_path);
7860 static const struct got_error *
7861 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7863 const struct got_error *err;
7864 char *branch_refname = NULL, *commit_refname = NULL;
7866 err = get_merge_branch_ref_name(&branch_refname, worktree);
7869 err = delete_ref(branch_refname, repo);
7873 err = get_merge_commit_ref_name(&commit_refname, worktree);
7876 err = delete_ref(commit_refname, repo);
7881 free(branch_refname);
7882 free(commit_refname);
7886 struct merge_commit_msg_arg {
7887 struct got_worktree *worktree;
7888 const char *branch_name;
7891 static const struct got_error *
7892 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
7893 const char *diff_path, char **logmsg, void *arg)
7895 struct merge_commit_msg_arg *a = arg;
7897 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7898 got_worktree_get_head_ref_name(a->worktree)) == -1)
7899 return got_error_from_errno("asprintf");
7905 const struct got_error *
7906 got_worktree_merge_branch(struct got_worktree *worktree,
7907 struct got_fileindex *fileindex,
7908 struct got_object_id *yca_commit_id,
7909 struct got_object_id *branch_tip,
7910 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7911 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7913 const struct got_error *err;
7914 char *fileindex_path = NULL;
7916 err = get_fileindex_path(&fileindex_path, worktree);
7920 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7925 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7926 branch_tip, repo, progress_cb, progress_arg,
7927 cancel_cb, cancel_arg);
7929 free(fileindex_path);
7933 const struct got_error *
7934 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7935 struct got_worktree *worktree, struct got_fileindex *fileindex,
7936 const char *author, const char *committer, int allow_bad_symlinks,
7937 struct got_object_id *branch_tip, const char *branch_name,
7938 int allow_conflict, struct got_repository *repo,
7939 got_worktree_status_cb status_cb, void *status_arg)
7942 const struct got_error *err = NULL, *sync_err;
7943 struct got_pathlist_head commitable_paths;
7944 struct collect_commitables_arg cc_arg;
7945 struct got_pathlist_entry *pe;
7946 struct got_reference *head_ref = NULL;
7947 struct got_object_id *head_commit_id = NULL;
7948 int have_staged_files = 0;
7949 struct merge_commit_msg_arg mcm_arg;
7950 char *fileindex_path = NULL;
7952 memset(&cc_arg, 0, sizeof(cc_arg));
7953 *new_commit_id = NULL;
7955 TAILQ_INIT(&commitable_paths);
7957 err = get_fileindex_path(&fileindex_path, worktree);
7961 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7965 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7969 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7970 &have_staged_files);
7971 if (err && err->code != GOT_ERR_CANCELLED)
7973 if (have_staged_files) {
7974 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7978 cc_arg.commitable_paths = &commitable_paths;
7979 cc_arg.worktree = worktree;
7980 cc_arg.fileindex = fileindex;
7982 cc_arg.have_staged_files = have_staged_files;
7983 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7984 cc_arg.commit_conflicts = allow_conflict;
7985 err = worktree_status(worktree, "", fileindex, repo,
7986 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7990 if (TAILQ_EMPTY(&commitable_paths)) {
7991 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7992 "merge of %s cannot proceed", branch_name);
7996 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7997 struct got_commitable *ct = pe->data;
7998 const char *ct_path = ct->in_repo_path;
8000 while (ct_path[0] == '/')
8002 err = check_out_of_date(ct_path, ct->status,
8003 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
8004 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
8010 mcm_arg.worktree = worktree;
8011 mcm_arg.branch_name = branch_name;
8012 err = commit_worktree(new_commit_id, &commitable_paths,
8013 head_commit_id, branch_tip, worktree, author, committer, NULL,
8014 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8018 err = update_fileindex_after_commit(worktree, &commitable_paths,
8019 *new_commit_id, fileindex, have_staged_files);
8020 sync_err = sync_fileindex(fileindex, fileindex_path);
8021 if (sync_err && err == NULL)
8024 TAILQ_FOREACH(pe, &commitable_paths, entry) {
8025 struct got_commitable *ct = pe->data;
8027 free_commitable(ct);
8029 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8030 free(fileindex_path);
8034 const struct got_error *
8035 got_worktree_merge_complete(struct got_worktree *worktree,
8036 struct got_fileindex *fileindex, struct got_repository *repo)
8038 const struct got_error *err, *unlockerr, *sync_err;
8039 char *fileindex_path = NULL;
8041 err = delete_merge_refs(worktree, repo);
8045 err = get_fileindex_path(&fileindex_path, worktree);
8048 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8049 sync_err = sync_fileindex(fileindex, fileindex_path);
8050 if (sync_err && err == NULL)
8053 got_fileindex_free(fileindex);
8054 free(fileindex_path);
8055 unlockerr = lock_worktree(worktree, LOCK_SH);
8056 if (unlockerr && err == NULL)
8061 const struct got_error *
8062 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8063 struct got_repository *repo)
8065 const struct got_error *err;
8066 char *branch_refname = NULL;
8067 struct got_reference *branch_ref = NULL;
8071 err = get_merge_branch_ref_name(&branch_refname, worktree);
8074 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8075 free(branch_refname);
8077 if (err->code != GOT_ERR_NOT_REF)
8085 const struct got_error *got_worktree_merge_prepare(
8086 struct got_fileindex **fileindex, struct got_worktree *worktree,
8087 struct got_reference *branch, struct got_repository *repo)
8089 const struct got_error *err = NULL;
8090 char *fileindex_path = NULL;
8091 char *branch_refname = NULL, *commit_refname = NULL;
8092 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
8093 struct got_reference *commit_ref = NULL;
8094 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
8095 struct check_rebase_ok_arg ok_arg;
8099 err = lock_worktree(worktree, LOCK_EX);
8103 err = open_fileindex(fileindex, &fileindex_path, worktree);
8107 /* Preconditions are the same as for rebase. */
8108 ok_arg.worktree = worktree;
8110 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8115 err = get_merge_branch_ref_name(&branch_refname, worktree);
8119 err = get_merge_commit_ref_name(&commit_refname, worktree);
8123 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8128 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8132 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8133 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8137 err = got_ref_resolve(&branch_tip, repo, branch);
8141 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8144 err = got_ref_write(branch_ref, repo);
8148 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8151 err = got_ref_write(commit_ref, repo);
8156 free(branch_refname);
8157 free(commit_refname);
8158 free(fileindex_path);
8160 got_ref_close(branch_ref);
8162 got_ref_close(commit_ref);
8164 got_ref_close(wt_branch);
8165 free(wt_branch_tip);
8168 got_fileindex_free(*fileindex);
8171 lock_worktree(worktree, LOCK_SH);
8176 const struct got_error *
8177 got_worktree_merge_continue(char **branch_name,
8178 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8179 struct got_worktree *worktree, struct got_repository *repo)
8181 const struct got_error *err;
8182 char *commit_refname = NULL, *branch_refname = NULL;
8183 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8184 char *fileindex_path = NULL;
8185 int have_staged_files = 0;
8187 *branch_name = NULL;
8191 err = lock_worktree(worktree, LOCK_EX);
8195 err = open_fileindex(fileindex, &fileindex_path, worktree);
8199 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8200 &have_staged_files);
8201 if (err && err->code != GOT_ERR_CANCELLED)
8203 if (have_staged_files) {
8204 err = got_error(GOT_ERR_STAGED_PATHS);
8208 err = get_merge_branch_ref_name(&branch_refname, worktree);
8212 err = get_merge_commit_ref_name(&commit_refname, worktree);
8216 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8220 if (!got_ref_is_symbolic(branch_ref)) {
8221 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8222 "%s is not a symbolic reference",
8223 got_ref_get_name(branch_ref));
8226 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8227 if (*branch_name == NULL) {
8228 err = got_error_from_errno("strdup");
8232 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8236 err = got_ref_resolve(branch_tip, repo, commit_ref);
8240 free(commit_refname);
8241 free(branch_refname);
8242 free(fileindex_path);
8244 got_ref_close(commit_ref);
8246 got_ref_close(branch_ref);
8250 *branch_name = NULL;
8255 got_fileindex_free(*fileindex);
8258 lock_worktree(worktree, LOCK_SH);
8263 const struct got_error *
8264 got_worktree_merge_abort(struct got_worktree *worktree,
8265 struct got_fileindex *fileindex, struct got_repository *repo,
8266 got_worktree_checkout_cb progress_cb, void *progress_arg)
8268 const struct got_error *err, *unlockerr, *sync_err;
8269 struct got_object_id *commit_id = NULL;
8270 struct got_commit_object *commit = NULL;
8271 char *fileindex_path = NULL;
8272 struct revert_file_args rfa;
8273 struct got_object_id *tree_id = NULL;
8275 err = got_object_open_as_commit(&commit, repo,
8276 worktree->base_commit_id);
8280 err = got_object_id_by_path(&tree_id, repo, commit,
8281 worktree->path_prefix);
8285 err = delete_merge_refs(worktree, repo);
8289 err = get_fileindex_path(&fileindex_path, worktree);
8293 rfa.worktree = worktree;
8294 rfa.fileindex = fileindex;
8295 rfa.progress_cb = progress_cb;
8296 rfa.progress_arg = progress_arg;
8297 rfa.patch_cb = NULL;
8298 rfa.patch_arg = NULL;
8300 rfa.unlink_added_files = 1;
8301 err = worktree_status(worktree, "", fileindex, repo,
8302 revert_file, &rfa, NULL, NULL, 1, 0);
8306 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8307 repo, progress_cb, progress_arg, NULL, NULL);
8309 sync_err = sync_fileindex(fileindex, fileindex_path);
8310 if (sync_err && err == NULL)
8316 got_object_commit_close(commit);
8318 got_fileindex_free(fileindex);
8319 free(fileindex_path);
8321 unlockerr = lock_worktree(worktree, LOCK_SH);
8322 if (unlockerr && err == NULL)
8327 struct check_stage_ok_arg {
8328 struct got_object_id *head_commit_id;
8329 struct got_worktree *worktree;
8330 struct got_fileindex *fileindex;
8331 struct got_repository *repo;
8335 static const struct got_error *
8336 check_stage_ok(void *arg, unsigned char status,
8337 unsigned char staged_status, const char *relpath,
8338 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8339 struct got_object_id *commit_id, int dirfd, const char *de_name)
8341 struct check_stage_ok_arg *a = arg;
8342 const struct got_error *err = NULL;
8343 struct got_fileindex_entry *ie;
8344 struct got_object_id base_commit_id;
8345 struct got_object_id *base_commit_idp = NULL;
8346 char *in_repo_path = NULL, *p;
8348 if (status == GOT_STATUS_UNVERSIONED ||
8349 status == GOT_STATUS_NO_CHANGE)
8351 if (status == GOT_STATUS_NONEXISTENT)
8352 return got_error_set_errno(ENOENT, relpath);
8354 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8356 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8358 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8359 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8361 return got_error_from_errno("asprintf");
8363 if (got_fileindex_entry_has_commit(ie)) {
8364 base_commit_idp = got_fileindex_entry_get_commit_id(
8365 &base_commit_id, ie);
8368 if (status == GOT_STATUS_CONFLICT) {
8369 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8371 } else if (status != GOT_STATUS_ADD &&
8372 status != GOT_STATUS_MODIFY &&
8373 status != GOT_STATUS_DELETE) {
8374 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8378 a->have_changes = 1;
8383 err = check_out_of_date(p, status, staged_status,
8384 blob_id, base_commit_idp, a->head_commit_id, a->repo,
8385 GOT_ERR_STAGE_OUT_OF_DATE);
8391 struct stage_path_arg {
8392 struct got_worktree *worktree;
8393 struct got_fileindex *fileindex;
8394 struct got_repository *repo;
8395 got_worktree_status_cb status_cb;
8397 got_worktree_patch_cb patch_cb;
8399 int staged_something;
8400 int allow_bad_symlinks;
8403 static const struct got_error *
8404 stage_path(void *arg, unsigned char status,
8405 unsigned char staged_status, const char *relpath,
8406 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8407 struct got_object_id *commit_id, int dirfd, const char *de_name)
8409 struct stage_path_arg *a = arg;
8410 const struct got_error *err = NULL;
8411 struct got_fileindex_entry *ie;
8412 char *ondisk_path = NULL, *path_content = NULL;
8414 struct got_object_id *new_staged_blob_id = NULL;
8417 if (status == GOT_STATUS_UNVERSIONED)
8420 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8422 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8424 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8426 return got_error_from_errno("asprintf");
8429 case GOT_STATUS_ADD:
8430 case GOT_STATUS_MODIFY:
8431 /* XXX could sb.st_mode be passed in by our caller? */
8432 if (lstat(ondisk_path, &sb) == -1) {
8433 err = got_error_from_errno2("lstat", ondisk_path);
8437 if (status == GOT_STATUS_ADD) {
8438 int choice = GOT_PATCH_CHOICE_NONE;
8439 err = (*a->patch_cb)(&choice, a->patch_arg,
8440 status, ie->path, NULL, 1, 1);
8443 if (choice != GOT_PATCH_CHOICE_YES)
8446 err = create_patched_content(&path_content, 0,
8447 staged_blob_id ? staged_blob_id : blob_id,
8448 ondisk_path, dirfd, de_name, ie->path,
8449 a->repo, a->patch_cb, a->patch_arg);
8450 if (err || path_content == NULL)
8454 err = got_object_blob_create(&new_staged_blob_id,
8455 path_content ? path_content : ondisk_path, a->repo);
8458 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8459 SHA1_DIGEST_LENGTH);
8460 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8461 stage = GOT_FILEIDX_STAGE_ADD;
8463 stage = GOT_FILEIDX_STAGE_MODIFY;
8464 got_fileindex_entry_stage_set(ie, stage);
8465 if (S_ISLNK(sb.st_mode)) {
8466 int is_bad_symlink = 0;
8467 if (!a->allow_bad_symlinks) {
8468 char target_path[PATH_MAX];
8470 target_len = readlink(ondisk_path, target_path,
8471 sizeof(target_path));
8472 if (target_len == -1) {
8473 err = got_error_from_errno2("readlink",
8477 err = is_bad_symlink_target(&is_bad_symlink,
8478 target_path, target_len, ondisk_path,
8479 a->worktree->root_path);
8482 if (is_bad_symlink) {
8483 err = got_error_path(ondisk_path,
8484 GOT_ERR_BAD_SYMLINK);
8489 got_fileindex_entry_staged_filetype_set(ie,
8490 GOT_FILEIDX_MODE_BAD_SYMLINK);
8492 got_fileindex_entry_staged_filetype_set(ie,
8493 GOT_FILEIDX_MODE_SYMLINK);
8495 got_fileindex_entry_staged_filetype_set(ie,
8496 GOT_FILEIDX_MODE_REGULAR_FILE);
8498 a->staged_something = 1;
8499 if (a->status_cb == NULL)
8501 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8502 get_staged_status(ie), relpath, blob_id,
8503 new_staged_blob_id, NULL, dirfd, de_name);
8507 * When staging the reverse of the staged diff,
8508 * implicitly unstage the file.
8510 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8511 sizeof(ie->blob_sha1)) == 0) {
8512 got_fileindex_entry_stage_set(ie,
8513 GOT_FILEIDX_STAGE_NONE);
8516 case GOT_STATUS_DELETE:
8517 if (staged_status == GOT_STATUS_DELETE)
8520 int choice = GOT_PATCH_CHOICE_NONE;
8521 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8522 ie->path, NULL, 1, 1);
8525 if (choice == GOT_PATCH_CHOICE_NO)
8527 if (choice != GOT_PATCH_CHOICE_YES) {
8528 err = got_error(GOT_ERR_PATCH_CHOICE);
8532 stage = GOT_FILEIDX_STAGE_DELETE;
8533 got_fileindex_entry_stage_set(ie, stage);
8534 a->staged_something = 1;
8535 if (a->status_cb == NULL)
8537 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8538 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8541 case GOT_STATUS_NO_CHANGE:
8543 case GOT_STATUS_CONFLICT:
8544 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8546 case GOT_STATUS_NONEXISTENT:
8547 err = got_error_set_errno(ENOENT, relpath);
8550 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8554 if (path_content && unlink(path_content) == -1 && err == NULL)
8555 err = got_error_from_errno2("unlink", path_content);
8558 free(new_staged_blob_id);
8562 const struct got_error *
8563 got_worktree_stage(struct got_worktree *worktree,
8564 struct got_pathlist_head *paths,
8565 got_worktree_status_cb status_cb, void *status_arg,
8566 got_worktree_patch_cb patch_cb, void *patch_arg,
8567 int allow_bad_symlinks, struct got_repository *repo)
8569 const struct got_error *err = NULL, *sync_err, *unlockerr;
8570 struct got_pathlist_entry *pe;
8571 struct got_fileindex *fileindex = NULL;
8572 char *fileindex_path = NULL;
8573 struct got_reference *head_ref = NULL;
8574 struct got_object_id *head_commit_id = NULL;
8575 struct check_stage_ok_arg oka;
8576 struct stage_path_arg spa;
8578 err = lock_worktree(worktree, LOCK_EX);
8582 err = got_ref_open(&head_ref, repo,
8583 got_worktree_get_head_ref_name(worktree), 0);
8586 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8589 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8593 /* Check pre-conditions before staging anything. */
8594 oka.head_commit_id = head_commit_id;
8595 oka.worktree = worktree;
8596 oka.fileindex = fileindex;
8598 oka.have_changes = 0;
8599 TAILQ_FOREACH(pe, paths, entry) {
8600 err = worktree_status(worktree, pe->path, fileindex, repo,
8601 check_stage_ok, &oka, NULL, NULL, 1, 0);
8605 if (!oka.have_changes) {
8606 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8610 spa.worktree = worktree;
8611 spa.fileindex = fileindex;
8613 spa.patch_cb = patch_cb;
8614 spa.patch_arg = patch_arg;
8615 spa.status_cb = status_cb;
8616 spa.status_arg = status_arg;
8617 spa.staged_something = 0;
8618 spa.allow_bad_symlinks = allow_bad_symlinks;
8619 TAILQ_FOREACH(pe, paths, entry) {
8620 err = worktree_status(worktree, pe->path, fileindex, repo,
8621 stage_path, &spa, NULL, NULL, 1, 0);
8625 if (!spa.staged_something) {
8626 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8630 sync_err = sync_fileindex(fileindex, fileindex_path);
8631 if (sync_err && err == NULL)
8635 got_ref_close(head_ref);
8636 free(head_commit_id);
8637 free(fileindex_path);
8639 got_fileindex_free(fileindex);
8640 unlockerr = lock_worktree(worktree, LOCK_SH);
8641 if (unlockerr && err == NULL)
8646 struct unstage_path_arg {
8647 struct got_worktree *worktree;
8648 struct got_fileindex *fileindex;
8649 struct got_repository *repo;
8650 got_worktree_checkout_cb progress_cb;
8652 got_worktree_patch_cb patch_cb;
8656 static const struct got_error *
8657 create_unstaged_content(char **path_unstaged_content,
8658 char **path_new_staged_content, struct got_object_id *blob_id,
8659 struct got_object_id *staged_blob_id, const char *relpath,
8660 struct got_repository *repo,
8661 got_worktree_patch_cb patch_cb, void *patch_arg)
8663 const struct got_error *err, *free_err;
8664 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8665 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8666 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8667 struct got_diffreg_result *diffreg_result = NULL;
8668 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8669 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8670 int fd1 = -1, fd2 = -1;
8672 *path_unstaged_content = NULL;
8673 *path_new_staged_content = NULL;
8675 err = got_object_id_str(&label1, blob_id);
8679 fd1 = got_opentempfd();
8681 err = got_error_from_errno("got_opentempfd");
8684 fd2 = got_opentempfd();
8686 err = got_error_from_errno("got_opentempfd");
8690 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8694 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
8698 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8702 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8707 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
8711 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8715 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8716 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8720 err = got_opentemp_named(path_unstaged_content, &outfile,
8721 "got-unstaged-content", "");
8724 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8725 "got-new-staged-content", "");
8729 if (fseek(f1, 0L, SEEK_SET) == -1) {
8730 err = got_ferror(f1, GOT_ERR_IO);
8733 if (fseek(f2, 0L, SEEK_SET) == -1) {
8734 err = got_ferror(f2, GOT_ERR_IO);
8737 /* Count the number of actual changes in the diff result. */
8738 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8739 struct diff_chunk_context cc = {};
8740 diff_chunk_context_load_change(&cc, &nchunks_used,
8741 diffreg_result->result, n, 0);
8744 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8746 err = apply_or_reject_change(&choice, &nchunks_used,
8747 diffreg_result->result, n, relpath, f1, f2,
8748 &line_cur1, &line_cur2,
8749 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8752 if (choice == GOT_PATCH_CHOICE_YES)
8755 have_rejected_content = 1;
8756 if (choice == GOT_PATCH_CHOICE_QUIT)
8759 if (have_content || have_rejected_content)
8760 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8761 outfile, rejectfile);
8764 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8765 err = got_error_from_errno("close");
8767 got_object_blob_close(blob);
8768 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8769 err = got_error_from_errno("close");
8771 got_object_blob_close(staged_blob);
8772 free_err = got_diffreg_result_free(diffreg_result);
8773 if (free_err && err == NULL)
8775 if (f1 && fclose(f1) == EOF && err == NULL)
8776 err = got_error_from_errno2("fclose", path1);
8777 if (f2 && fclose(f2) == EOF && err == NULL)
8778 err = got_error_from_errno2("fclose", path2);
8779 if (outfile && fclose(outfile) == EOF && err == NULL)
8780 err = got_error_from_errno2("fclose", *path_unstaged_content);
8781 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8782 err = got_error_from_errno2("fclose", *path_new_staged_content);
8783 if (path1 && unlink(path1) == -1 && err == NULL)
8784 err = got_error_from_errno2("unlink", path1);
8785 if (path2 && unlink(path2) == -1 && err == NULL)
8786 err = got_error_from_errno2("unlink", path2);
8787 if (err || !have_content) {
8788 if (*path_unstaged_content &&
8789 unlink(*path_unstaged_content) == -1 && err == NULL)
8790 err = got_error_from_errno2("unlink",
8791 *path_unstaged_content);
8792 free(*path_unstaged_content);
8793 *path_unstaged_content = NULL;
8795 if (err || !have_content || !have_rejected_content) {
8796 if (*path_new_staged_content &&
8797 unlink(*path_new_staged_content) == -1 && err == NULL)
8798 err = got_error_from_errno2("unlink",
8799 *path_new_staged_content);
8800 free(*path_new_staged_content);
8801 *path_new_staged_content = NULL;
8808 static const struct got_error *
8809 unstage_hunks(struct got_object_id *staged_blob_id,
8810 struct got_blob_object *blob_base,
8811 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8812 const char *ondisk_path, const char *label_orig,
8813 struct got_worktree *worktree, struct got_repository *repo,
8814 got_worktree_patch_cb patch_cb, void *patch_arg,
8815 got_worktree_checkout_cb progress_cb, void *progress_arg)
8817 const struct got_error *err = NULL;
8818 char *path_unstaged_content = NULL;
8819 char *path_new_staged_content = NULL;
8820 char *parent = NULL, *base_path = NULL;
8821 char *blob_base_path = NULL;
8822 struct got_object_id *new_staged_blob_id = NULL;
8823 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8826 err = create_unstaged_content(&path_unstaged_content,
8827 &path_new_staged_content, blob_id, staged_blob_id,
8828 ie->path, repo, patch_cb, patch_arg);
8832 if (path_unstaged_content == NULL)
8835 if (path_new_staged_content) {
8836 err = got_object_blob_create(&new_staged_blob_id,
8837 path_new_staged_content, repo);
8842 f = fopen(path_unstaged_content, "re");
8844 err = got_error_from_errno2("fopen",
8845 path_unstaged_content);
8848 if (fstat(fileno(f), &sb) == -1) {
8849 err = got_error_from_errno2("fstat", path_unstaged_content);
8852 if (got_fileindex_entry_staged_filetype_get(ie) ==
8853 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8854 char link_target[PATH_MAX];
8856 r = fread(link_target, 1, sizeof(link_target), f);
8857 if (r == 0 && ferror(f)) {
8858 err = got_error_from_errno("fread");
8861 if (r >= sizeof(link_target)) { /* should not happen */
8862 err = got_error(GOT_ERR_NO_SPACE);
8865 link_target[r] = '\0';
8866 err = merge_symlink(worktree, blob_base,
8867 ondisk_path, ie->path, label_orig, link_target,
8868 worktree->base_commit_id, repo, progress_cb,
8871 int local_changes_subsumed;
8873 err = got_path_dirname(&parent, ondisk_path);
8877 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8879 err = got_error_from_errno("asprintf");
8884 err = got_opentemp_named(&blob_base_path, &f_base,
8888 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8894 * In order the run a 3-way merge with a symlink we copy the symlink's
8895 * target path into a temporary file and use that file with diff3.
8897 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8898 err = dump_symlink_target_path_to_file(&f_deriv2,
8904 fd = open(ondisk_path,
8905 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8907 err = got_error_from_errno2("open", ondisk_path);
8910 f_deriv2 = fdopen(fd, "r");
8911 if (f_deriv2 == NULL) {
8912 err = got_error_from_errno2("fdopen", ondisk_path);
8918 err = merge_file(&local_changes_subsumed, worktree,
8919 f_base, f, f_deriv2, ondisk_path, ie->path,
8920 got_fileindex_perms_to_st(ie),
8921 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8922 repo, progress_cb, progress_arg);
8927 if (new_staged_blob_id) {
8928 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8929 SHA1_DIGEST_LENGTH);
8931 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8932 got_fileindex_entry_staged_filetype_set(ie, 0);
8935 free(new_staged_blob_id);
8936 if (path_unstaged_content &&
8937 unlink(path_unstaged_content) == -1 && err == NULL)
8938 err = got_error_from_errno2("unlink", path_unstaged_content);
8939 if (path_new_staged_content &&
8940 unlink(path_new_staged_content) == -1 && err == NULL)
8941 err = got_error_from_errno2("unlink", path_new_staged_content);
8942 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8943 err = got_error_from_errno2("unlink", blob_base_path);
8944 if (f_base && fclose(f_base) == EOF && err == NULL)
8945 err = got_error_from_errno2("fclose", path_unstaged_content);
8946 if (f && fclose(f) == EOF && err == NULL)
8947 err = got_error_from_errno2("fclose", path_unstaged_content);
8948 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8949 err = got_error_from_errno2("fclose", ondisk_path);
8950 free(path_unstaged_content);
8951 free(path_new_staged_content);
8952 free(blob_base_path);
8958 static const struct got_error *
8959 unstage_path(void *arg, unsigned char status,
8960 unsigned char staged_status, const char *relpath,
8961 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8962 struct got_object_id *commit_id, int dirfd, const char *de_name)
8964 const struct got_error *err = NULL;
8965 struct unstage_path_arg *a = arg;
8966 struct got_fileindex_entry *ie;
8967 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8968 char *ondisk_path = NULL;
8969 char *id_str = NULL, *label_orig = NULL;
8970 int local_changes_subsumed;
8972 int fd1 = -1, fd2 = -1;
8974 if (staged_status != GOT_STATUS_ADD &&
8975 staged_status != GOT_STATUS_MODIFY &&
8976 staged_status != GOT_STATUS_DELETE)
8979 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8981 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8983 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8985 return got_error_from_errno("asprintf");
8987 err = got_object_id_str(&id_str,
8988 commit_id ? commit_id : a->worktree->base_commit_id);
8991 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8993 err = got_error_from_errno("asprintf");
8997 fd1 = got_opentempfd();
8999 err = got_error_from_errno("got_opentempfd");
9002 fd2 = got_opentempfd();
9004 err = got_error_from_errno("got_opentempfd");
9008 switch (staged_status) {
9009 case GOT_STATUS_MODIFY:
9010 err = got_object_open_as_blob(&blob_base, a->repo,
9011 blob_id, 8192, fd1);
9015 case GOT_STATUS_ADD:
9017 if (staged_status == GOT_STATUS_ADD) {
9018 int choice = GOT_PATCH_CHOICE_NONE;
9019 err = (*a->patch_cb)(&choice, a->patch_arg,
9020 staged_status, ie->path, NULL, 1, 1);
9023 if (choice != GOT_PATCH_CHOICE_YES)
9026 err = unstage_hunks(staged_blob_id,
9027 blob_base, blob_id, ie, ondisk_path,
9028 label_orig, a->worktree, a->repo,
9029 a->patch_cb, a->patch_arg,
9030 a->progress_cb, a->progress_arg);
9031 break; /* Done with this file. */
9034 err = got_object_open_as_blob(&blob_staged, a->repo,
9035 staged_blob_id, 8192, fd2);
9038 switch (got_fileindex_entry_staged_filetype_get(ie)) {
9039 case GOT_FILEIDX_MODE_BAD_SYMLINK:
9040 case GOT_FILEIDX_MODE_REGULAR_FILE:
9041 err = merge_blob(&local_changes_subsumed, a->worktree,
9042 blob_base, ondisk_path, relpath,
9043 got_fileindex_perms_to_st(ie), label_orig,
9044 blob_staged, commit_id ? commit_id :
9045 a->worktree->base_commit_id, a->repo,
9046 a->progress_cb, a->progress_arg);
9048 case GOT_FILEIDX_MODE_SYMLINK:
9049 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9050 char *staged_target;
9051 err = got_object_blob_read_to_str(
9052 &staged_target, blob_staged);
9055 err = merge_symlink(a->worktree, blob_base,
9056 ondisk_path, relpath, label_orig,
9057 staged_target, commit_id ? commit_id :
9058 a->worktree->base_commit_id,
9059 a->repo, a->progress_cb, a->progress_arg);
9060 free(staged_target);
9062 err = merge_blob(&local_changes_subsumed,
9063 a->worktree, blob_base, ondisk_path,
9064 relpath, got_fileindex_perms_to_st(ie),
9065 label_orig, blob_staged,
9066 commit_id ? commit_id :
9067 a->worktree->base_commit_id, a->repo,
9068 a->progress_cb, a->progress_arg);
9072 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9076 got_fileindex_entry_stage_set(ie,
9077 GOT_FILEIDX_STAGE_NONE);
9078 got_fileindex_entry_staged_filetype_set(ie, 0);
9081 case GOT_STATUS_DELETE:
9083 int choice = GOT_PATCH_CHOICE_NONE;
9084 err = (*a->patch_cb)(&choice, a->patch_arg,
9085 staged_status, ie->path, NULL, 1, 1);
9088 if (choice == GOT_PATCH_CHOICE_NO)
9090 if (choice != GOT_PATCH_CHOICE_YES) {
9091 err = got_error(GOT_ERR_PATCH_CHOICE);
9095 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9096 got_fileindex_entry_staged_filetype_set(ie, 0);
9097 err = get_file_status(&status, &sb, ie, ondisk_path,
9098 dirfd, de_name, a->repo);
9101 err = (*a->progress_cb)(a->progress_arg, status, relpath);
9106 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9107 err = got_error_from_errno("close");
9109 got_object_blob_close(blob_base);
9110 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9111 err = got_error_from_errno("close");
9113 got_object_blob_close(blob_staged);
9119 const struct got_error *
9120 got_worktree_unstage(struct got_worktree *worktree,
9121 struct got_pathlist_head *paths,
9122 got_worktree_checkout_cb progress_cb, void *progress_arg,
9123 got_worktree_patch_cb patch_cb, void *patch_arg,
9124 struct got_repository *repo)
9126 const struct got_error *err = NULL, *sync_err, *unlockerr;
9127 struct got_pathlist_entry *pe;
9128 struct got_fileindex *fileindex = NULL;
9129 char *fileindex_path = NULL;
9130 struct unstage_path_arg upa;
9132 err = lock_worktree(worktree, LOCK_EX);
9136 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9140 upa.worktree = worktree;
9141 upa.fileindex = fileindex;
9143 upa.progress_cb = progress_cb;
9144 upa.progress_arg = progress_arg;
9145 upa.patch_cb = patch_cb;
9146 upa.patch_arg = patch_arg;
9147 TAILQ_FOREACH(pe, paths, entry) {
9148 err = worktree_status(worktree, pe->path, fileindex, repo,
9149 unstage_path, &upa, NULL, NULL, 1, 0);
9154 sync_err = sync_fileindex(fileindex, fileindex_path);
9155 if (sync_err && err == NULL)
9158 free(fileindex_path);
9160 got_fileindex_free(fileindex);
9161 unlockerr = lock_worktree(worktree, LOCK_SH);
9162 if (unlockerr && err == NULL)
9167 struct report_file_info_arg {
9168 struct got_worktree *worktree;
9169 got_worktree_path_info_cb info_cb;
9171 struct got_pathlist_head *paths;
9172 got_cancel_cb cancel_cb;
9176 static const struct got_error *
9177 report_file_info(void *arg, struct got_fileindex_entry *ie)
9179 struct report_file_info_arg *a = arg;
9180 struct got_pathlist_entry *pe;
9181 struct got_object_id blob_id, staged_blob_id, commit_id;
9182 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9183 struct got_object_id *commit_idp = NULL;
9186 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9187 return got_error(GOT_ERR_CANCELLED);
9189 TAILQ_FOREACH(pe, a->paths, entry) {
9190 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9191 got_path_is_child(ie->path, pe->path, pe->path_len))
9194 if (pe == NULL) /* not found */
9197 if (got_fileindex_entry_has_blob(ie))
9198 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9199 stage = got_fileindex_entry_stage_get(ie);
9200 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9201 stage == GOT_FILEIDX_STAGE_ADD) {
9202 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9203 &staged_blob_id, ie);
9206 if (got_fileindex_entry_has_commit(ie))
9207 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9209 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9210 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9213 const struct got_error *
9214 got_worktree_path_info(struct got_worktree *worktree,
9215 struct got_pathlist_head *paths,
9216 got_worktree_path_info_cb info_cb, void *info_arg,
9217 got_cancel_cb cancel_cb, void *cancel_arg)
9220 const struct got_error *err = NULL, *unlockerr;
9221 struct got_fileindex *fileindex = NULL;
9222 char *fileindex_path = NULL;
9223 struct report_file_info_arg arg;
9225 err = lock_worktree(worktree, LOCK_SH);
9229 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9233 arg.worktree = worktree;
9234 arg.info_cb = info_cb;
9235 arg.info_arg = info_arg;
9237 arg.cancel_cb = cancel_cb;
9238 arg.cancel_arg = cancel_arg;
9239 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9242 free(fileindex_path);
9244 got_fileindex_free(fileindex);
9245 unlockerr = lock_worktree(worktree, LOCK_UN);
9246 if (unlockerr && err == NULL)
9251 static const struct got_error *
9252 patch_check_path(const char *p, char **path, unsigned char *status,
9253 unsigned char *staged_status, struct got_fileindex *fileindex,
9254 struct got_worktree *worktree, struct got_repository *repo)
9256 const struct got_error *err;
9257 struct got_fileindex_entry *ie;
9259 char *ondisk_path = NULL;
9261 err = got_worktree_resolve_path(path, worktree, p);
9265 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9266 *path[0] ? "/" : "", *path) == -1)
9267 return got_error_from_errno("asprintf");
9269 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9271 *staged_status = get_staged_status(ie);
9272 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9277 *staged_status = GOT_STATUS_NO_CHANGE;
9278 *status = GOT_STATUS_UNVERSIONED;
9279 if (lstat(ondisk_path, &sb) == -1) {
9280 if (errno != ENOENT) {
9281 err = got_error_from_errno2("lstat",
9285 *status = GOT_STATUS_NONEXISTENT;
9294 static const struct got_error *
9295 patch_can_rm(const char *path, unsigned char status,
9296 unsigned char staged_status)
9298 if (status == GOT_STATUS_NONEXISTENT)
9299 return got_error_set_errno(ENOENT, path);
9300 if (status != GOT_STATUS_NO_CHANGE &&
9301 status != GOT_STATUS_ADD &&
9302 status != GOT_STATUS_MODIFY &&
9303 status != GOT_STATUS_MODE_CHANGE)
9304 return got_error_path(path, GOT_ERR_FILE_STATUS);
9305 if (staged_status == GOT_STATUS_DELETE)
9306 return got_error_path(path, GOT_ERR_FILE_STATUS);
9310 static const struct got_error *
9311 patch_can_add(const char *path, unsigned char status)
9313 if (status != GOT_STATUS_NONEXISTENT)
9314 return got_error_path(path, GOT_ERR_FILE_STATUS);
9318 static const struct got_error *
9319 patch_can_edit(const char *path, unsigned char status,
9320 unsigned char staged_status)
9322 if (status == GOT_STATUS_NONEXISTENT)
9323 return got_error_set_errno(ENOENT, path);
9324 if (status != GOT_STATUS_NO_CHANGE &&
9325 status != GOT_STATUS_ADD &&
9326 status != GOT_STATUS_MODIFY)
9327 return got_error_path(path, GOT_ERR_FILE_STATUS);
9328 if (staged_status == GOT_STATUS_DELETE)
9329 return got_error_path(path, GOT_ERR_FILE_STATUS);
9333 const struct got_error *
9334 got_worktree_patch_prepare(struct got_fileindex **fileindex,
9335 char **fileindex_path, struct got_worktree *worktree)
9337 return open_fileindex(fileindex, fileindex_path, worktree);
9340 const struct got_error *
9341 got_worktree_patch_check_path(const char *old, const char *new,
9342 char **oldpath, char **newpath, struct got_worktree *worktree,
9343 struct got_repository *repo, struct got_fileindex *fileindex)
9345 const struct got_error *err = NULL;
9346 int file_renamed = 0;
9347 unsigned char status_old, staged_status_old;
9348 unsigned char status_new, staged_status_new;
9353 err = patch_check_path(old != NULL ? old : new, oldpath,
9354 &status_old, &staged_status_old, fileindex, worktree, repo);
9358 err = patch_check_path(new != NULL ? new : old, newpath,
9359 &status_new, &staged_status_new, fileindex, worktree, repo);
9363 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9366 if (old != NULL && new == NULL)
9367 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9368 else if (file_renamed) {
9369 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9371 err = patch_can_add(*newpath, status_new);
9372 } else if (old == NULL)
9373 err = patch_can_add(*newpath, status_new);
9375 err = patch_can_edit(*newpath, status_new, staged_status_new);
9387 const struct got_error *
9388 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9389 struct got_worktree *worktree, struct got_fileindex *fileindex,
9390 got_worktree_checkout_cb progress_cb, void *progress_arg)
9392 struct schedule_addition_args saa;
9394 memset(&saa, 0, sizeof(saa));
9395 saa.worktree = worktree;
9396 saa.fileindex = fileindex;
9397 saa.progress_cb = progress_cb;
9398 saa.progress_arg = progress_arg;
9401 return worktree_status(worktree, path, fileindex, repo,
9402 schedule_addition, &saa, NULL, NULL, 1, 0);
9405 const struct got_error *
9406 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9407 struct got_worktree *worktree, struct got_fileindex *fileindex,
9408 got_worktree_delete_cb progress_cb, void *progress_arg)
9410 struct schedule_deletion_args sda;
9412 memset(&sda, 0, sizeof(sda));
9413 sda.worktree = worktree;
9414 sda.fileindex = fileindex;
9415 sda.progress_cb = progress_cb;
9416 sda.progress_arg = progress_arg;
9418 sda.delete_local_mods = 0;
9419 sda.keep_on_disk = 0;
9420 sda.ignore_missing_paths = 0;
9421 sda.status_codes = NULL;
9423 return worktree_status(worktree, path, fileindex, repo,
9424 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9427 const struct got_error *
9428 got_worktree_patch_complete(struct got_fileindex *fileindex,
9429 const char *fileindex_path)
9431 const struct got_error *err = NULL;
9433 err = sync_fileindex(fileindex, fileindex_path);
9434 got_fileindex_free(fileindex);