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,
566 err = got_opentemp_named_fd(&path_deriv2, &fd_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, base_path);
998 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1005 * No common ancestor exists. This is an "add vs add" conflict
1006 * and we simply use an empty ancestor file to make both files
1007 * appear in the merged result in their entirety.
1009 f_orig = got_opentemp();
1010 if (f_orig == NULL) {
1011 err = got_error_from_errno("got_opentemp");
1016 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1017 err = got_error_from_errno("asprintf");
1022 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1025 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1030 err = got_object_id_str(&id_str, deriv_base_commit_id);
1033 if (asprintf(&label_deriv, "%s: commit %s",
1034 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1035 err = got_error_from_errno("asprintf");
1040 * In order the run a 3-way merge with a symlink we copy the symlink's
1041 * target path into a temporary file and use that file with diff3.
1043 if (S_ISLNK(st_mode)) {
1044 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1049 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1051 err = got_error_from_errno2("open", ondisk_path);
1054 f_deriv2 = fdopen(fd, "r");
1055 if (f_deriv2 == NULL) {
1056 err = got_error_from_errno2("fdopen", ondisk_path);
1062 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1063 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1064 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1066 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1067 err = got_error_from_errno("fclose");
1068 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1069 err = got_error_from_errno("fclose");
1070 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1071 err = got_error_from_errno("fclose");
1073 if (blob_orig_path) {
1074 unlink(blob_orig_path);
1075 free(blob_orig_path);
1077 if (blob_deriv_path) {
1078 unlink(blob_deriv_path);
1079 free(blob_deriv_path);
1087 static const struct got_error *
1088 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1089 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1090 int wt_fd, const char *path, struct got_object_id *blob_id)
1092 const struct got_error *err = NULL;
1093 struct got_fileindex_entry *new_ie;
1097 err = got_fileindex_entry_alloc(&new_ie, path);
1101 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1102 blob_id->sha1, base_commit_id->sha1, 1);
1106 err = got_fileindex_entry_add(fileindex, new_ie);
1109 got_fileindex_entry_free(new_ie);
1116 get_ondisk_perms(int executable, mode_t st_mode)
1118 mode_t xbits = S_IXUSR;
1121 /* Map read bits to execute bits. */
1122 if (st_mode & S_IRGRP)
1124 if (st_mode & S_IROTH)
1126 return st_mode | xbits;
1133 apply_umask(mode_t mode)
1142 /* forward declaration */
1143 static const struct got_error *
1144 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1145 const char *path, mode_t te_mode, mode_t st_mode,
1146 struct got_blob_object *blob, int restoring_missing_file,
1147 int reverting_versioned_file, int installing_bad_symlink,
1148 int path_is_unversioned, struct got_repository *repo,
1149 got_worktree_checkout_cb progress_cb, void *progress_arg);
1152 * This function assumes that the provided symlink target points at a
1153 * safe location in the work tree!
1155 static const struct got_error *
1156 replace_existing_symlink(int *did_something, const char *ondisk_path,
1157 const char *target_path, size_t target_len)
1159 const struct got_error *err = NULL;
1161 char etarget[PATH_MAX];
1167 * "Bad" symlinks (those pointing outside the work tree or into the
1168 * .got directory) are installed in the work tree as a regular file
1169 * which contains the bad symlink target path.
1170 * The new symlink target has already been checked for safety by our
1171 * caller. If we can successfully open a regular file then we simply
1172 * replace this file with a symlink below.
1174 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1176 if (!got_err_open_nofollow_on_symlink())
1177 return got_error_from_errno2("open", ondisk_path);
1179 /* We are updating an existing on-disk symlink. */
1180 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1182 return got_error_from_errno2("readlink", ondisk_path);
1184 if (elen == target_len &&
1185 memcmp(etarget, target_path, target_len) == 0)
1186 return NULL; /* nothing to do */
1190 err = update_symlink(ondisk_path, target_path, target_len);
1191 if (fd != -1 && close(fd) == -1 && err == NULL)
1192 err = got_error_from_errno2("close", ondisk_path);
1196 static const struct got_error *
1197 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1198 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1200 const struct got_error *err = NULL;
1201 char canonpath[PATH_MAX];
1202 char *path_got = NULL;
1204 *is_bad_symlink = 0;
1206 if (target_len >= sizeof(canonpath)) {
1207 *is_bad_symlink = 1;
1212 * We do not use realpath(3) to resolve the symlink's target
1213 * path because we don't want to resolve symlinks recursively.
1214 * Instead we make the path absolute and then canonicalize it.
1215 * Relative symlink target lookup should begin at the directory
1216 * in which the blob object is being installed.
1218 if (!got_path_is_absolute(target_path)) {
1219 char *abspath, *parent;
1220 err = got_path_dirname(&parent, ondisk_path);
1223 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1225 return got_error_from_errno("asprintf");
1228 if (strlen(abspath) >= sizeof(canonpath)) {
1229 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1233 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1238 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1243 /* Only allow symlinks pointing at paths within the work tree. */
1244 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1245 *is_bad_symlink = 1;
1249 /* Do not allow symlinks pointing into the .got directory. */
1250 if (asprintf(&path_got, "%s/%s", wtroot_path,
1251 GOT_WORKTREE_GOT_DIR) == -1)
1252 return got_error_from_errno("asprintf");
1253 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1254 *is_bad_symlink = 1;
1260 static const struct got_error *
1261 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1262 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1263 int restoring_missing_file, int reverting_versioned_file,
1264 int path_is_unversioned, int allow_bad_symlinks,
1265 struct got_repository *repo,
1266 got_worktree_checkout_cb progress_cb, void *progress_arg)
1268 const struct got_error *err = NULL;
1269 char target_path[PATH_MAX];
1270 size_t len, target_len = 0;
1271 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1272 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1274 *is_bad_symlink = 0;
1277 * Blob object content specifies the target path of the link.
1278 * If a symbolic link cannot be installed we instead create
1279 * a regular file which contains the link target path stored
1280 * in the blob object.
1283 err = got_object_blob_read_block(&len, blob);
1287 if (len + target_len >= sizeof(target_path)) {
1288 /* Path too long; install as a regular file. */
1289 *is_bad_symlink = 1;
1290 got_object_blob_rewind(blob);
1291 return install_blob(worktree, ondisk_path, path,
1292 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1293 restoring_missing_file, reverting_versioned_file,
1294 1, path_is_unversioned, repo, progress_cb,
1298 /* Skip blob object header first time around. */
1299 memcpy(target_path + target_len, buf + hdrlen,
1301 target_len += len - hdrlen;
1305 target_path[target_len] = '\0';
1307 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1308 ondisk_path, worktree->root_path);
1312 if (*is_bad_symlink && !allow_bad_symlinks) {
1313 /* install as a regular file */
1314 got_object_blob_rewind(blob);
1315 err = install_blob(worktree, ondisk_path, path,
1316 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1317 restoring_missing_file, reverting_versioned_file, 1,
1318 path_is_unversioned, repo, progress_cb, progress_arg);
1322 if (symlink(target_path, ondisk_path) == -1) {
1323 if (errno == EEXIST) {
1324 int symlink_replaced;
1325 if (path_is_unversioned) {
1326 err = (*progress_cb)(progress_arg,
1327 GOT_STATUS_UNVERSIONED, path);
1330 err = replace_existing_symlink(&symlink_replaced,
1331 ondisk_path, target_path, target_len);
1335 if (symlink_replaced) {
1336 err = (*progress_cb)(progress_arg,
1337 reverting_versioned_file ?
1339 GOT_STATUS_UPDATE, path);
1341 err = (*progress_cb)(progress_arg,
1342 GOT_STATUS_EXISTS, path);
1345 return err; /* Nothing else to do. */
1348 if (errno == ENOENT) {
1350 err = got_path_dirname(&parent, ondisk_path);
1353 err = add_dir_on_disk(worktree, parent);
1358 * Retry, and fall through to error handling
1359 * below if this second attempt fails.
1361 if (symlink(target_path, ondisk_path) != -1) {
1362 err = NULL; /* success */
1367 /* Handle errors from first or second creation attempt. */
1368 if (errno == ENAMETOOLONG) {
1369 /* bad target path; install as a regular file */
1370 *is_bad_symlink = 1;
1371 got_object_blob_rewind(blob);
1372 err = install_blob(worktree, ondisk_path, path,
1373 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1374 restoring_missing_file, reverting_versioned_file, 1,
1375 path_is_unversioned, repo,
1376 progress_cb, progress_arg);
1377 } else if (errno == ENOTDIR) {
1378 err = got_error_path(ondisk_path,
1379 GOT_ERR_FILE_OBSTRUCTED);
1381 err = got_error_from_errno3("symlink",
1382 target_path, ondisk_path);
1384 } else if (progress_cb)
1385 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1386 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1390 static const struct got_error *
1391 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1392 const char *path, mode_t te_mode, mode_t st_mode,
1393 struct got_blob_object *blob, int restoring_missing_file,
1394 int reverting_versioned_file, int installing_bad_symlink,
1395 int path_is_unversioned, struct got_repository *repo,
1396 got_worktree_checkout_cb progress_cb, void *progress_arg)
1398 const struct got_error *err = NULL;
1402 char *tmppath = NULL;
1405 mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1406 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1409 if (errno == ENOENT) {
1411 err = got_path_dirname(&parent, path);
1414 err = add_dir_on_disk(worktree, parent);
1418 fd = open(ondisk_path,
1419 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1422 return got_error_from_errno2("open",
1424 } else if (errno == EEXIST) {
1425 if (path_is_unversioned) {
1426 err = (*progress_cb)(progress_arg,
1427 GOT_STATUS_UNVERSIONED, path);
1430 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1431 !S_ISREG(st_mode) && !installing_bad_symlink) {
1432 /* TODO file is obstructed; do something */
1433 err = got_error_path(ondisk_path,
1434 GOT_ERR_FILE_OBSTRUCTED);
1437 err = got_opentemp_named_fd(&tmppath, &fd,
1443 if (fchmod(fd, apply_umask(mode)) == -1) {
1444 err = got_error_from_errno2("fchmod",
1450 return got_error_from_errno2("open", ondisk_path);
1454 if (restoring_missing_file)
1455 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1457 else if (reverting_versioned_file)
1458 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1461 err = (*progress_cb)(progress_arg,
1462 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1467 hdrlen = got_object_blob_get_hdrlen(blob);
1469 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1470 err = got_object_blob_read_block(&len, blob);
1474 /* Skip blob object header first time around. */
1475 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1477 err = got_error_from_errno("write");
1479 } else if (outlen != len - hdrlen) {
1480 err = got_error(GOT_ERR_IO);
1487 if (fsync(fd) != 0) {
1488 err = got_error_from_errno("fsync");
1493 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1494 err = got_error_from_errno2("unlink", ondisk_path);
1497 if (rename(tmppath, ondisk_path) != 0) {
1498 err = got_error_from_errno3("rename", tmppath,
1507 if (fd != -1 && close(fd) == -1 && err == NULL)
1508 err = got_error_from_errno("close");
1509 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1510 err = got_error_from_errno2("unlink", tmppath);
1515 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1516 static const struct got_error *
1517 get_modified_file_content_status(unsigned char *status, FILE *f)
1519 const struct got_error *err = NULL;
1520 const char *markers[3] = {
1521 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1522 GOT_DIFF_CONFLICT_MARKER_SEP,
1523 GOT_DIFF_CONFLICT_MARKER_END
1527 size_t linesize = 0;
1530 while (*status == GOT_STATUS_MODIFY) {
1531 linelen = getline(&line, &linesize, f);
1532 if (linelen == -1) {
1535 err = got_ferror(f, GOT_ERR_IO);
1539 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1540 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1542 *status = GOT_STATUS_CONFLICT;
1553 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1555 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1556 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1560 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1562 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1563 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1564 ie->mtime_sec == sb->st_mtim.tv_sec &&
1565 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1566 ie->size == (sb->st_size & 0xffffffff) &&
1567 !xbit_differs(ie, sb->st_mode));
1570 static unsigned char
1571 get_staged_status(struct got_fileindex_entry *ie)
1573 switch (got_fileindex_entry_stage_get(ie)) {
1574 case GOT_FILEIDX_STAGE_ADD:
1575 return GOT_STATUS_ADD;
1576 case GOT_FILEIDX_STAGE_DELETE:
1577 return GOT_STATUS_DELETE;
1578 case GOT_FILEIDX_STAGE_MODIFY:
1579 return GOT_STATUS_MODIFY;
1581 return GOT_STATUS_NO_CHANGE;
1585 static const struct got_error *
1586 get_symlink_modification_status(unsigned char *status,
1587 struct got_fileindex_entry *ie, const char *abspath,
1588 int dirfd, const char *de_name, struct got_blob_object *blob)
1590 const struct got_error *err = NULL;
1591 char target_path[PATH_MAX];
1592 char etarget[PATH_MAX];
1594 size_t len, target_len = 0;
1595 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1596 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1598 *status = GOT_STATUS_NO_CHANGE;
1600 /* Blob object content specifies the target path of the link. */
1602 err = got_object_blob_read_block(&len, blob);
1605 if (len + target_len >= sizeof(target_path)) {
1607 * Should not happen. The blob contents were OK
1608 * when this symlink was installed.
1610 return got_error(GOT_ERR_NO_SPACE);
1613 /* Skip blob object header first time around. */
1614 memcpy(target_path + target_len, buf + hdrlen,
1616 target_len += len - hdrlen;
1620 target_path[target_len] = '\0';
1623 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1625 return got_error_from_errno2("readlinkat", abspath);
1627 elen = readlink(abspath, etarget, sizeof(etarget));
1629 return got_error_from_errno2("readlink", abspath);
1632 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1633 *status = GOT_STATUS_MODIFY;
1638 static const struct got_error *
1639 get_file_status(unsigned char *status, struct stat *sb,
1640 struct got_fileindex_entry *ie, const char *abspath,
1641 int dirfd, const char *de_name, struct got_repository *repo)
1643 const struct got_error *err = NULL;
1644 struct got_object_id id;
1646 int fd = -1, fd1 = -1;
1649 struct got_blob_object *blob = NULL;
1651 unsigned char staged_status = get_staged_status(ie);
1653 *status = GOT_STATUS_NO_CHANGE;
1654 memset(sb, 0, sizeof(*sb));
1657 * Whenever the caller provides a directory descriptor and a
1658 * directory entry name for the file, use them! This prevents
1659 * race conditions if filesystem paths change beneath our feet.
1662 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1663 if (errno == ENOENT) {
1664 if (got_fileindex_entry_has_file_on_disk(ie))
1665 *status = GOT_STATUS_MISSING;
1667 *status = GOT_STATUS_DELETE;
1670 err = got_error_from_errno2("fstatat", abspath);
1674 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1675 if (fd == -1 && errno != ENOENT &&
1676 !got_err_open_nofollow_on_symlink())
1677 return got_error_from_errno2("open", abspath);
1678 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1679 if (lstat(abspath, sb) == -1)
1680 return got_error_from_errno2("lstat", abspath);
1681 } else if (fd == -1 || fstat(fd, sb) == -1) {
1682 if (errno == ENOENT) {
1683 if (got_fileindex_entry_has_file_on_disk(ie))
1684 *status = GOT_STATUS_MISSING;
1686 *status = GOT_STATUS_DELETE;
1689 err = got_error_from_errno2("fstat", abspath);
1694 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1695 *status = GOT_STATUS_OBSTRUCTED;
1699 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1700 *status = GOT_STATUS_DELETE;
1702 } else if (!got_fileindex_entry_has_blob(ie) &&
1703 staged_status != GOT_STATUS_ADD) {
1704 *status = GOT_STATUS_ADD;
1708 if (!stat_info_differs(ie, sb))
1711 if (S_ISLNK(sb->st_mode) &&
1712 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1713 *status = GOT_STATUS_MODIFY;
1717 if (staged_status == GOT_STATUS_MODIFY ||
1718 staged_status == GOT_STATUS_ADD)
1719 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1721 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1723 fd1 = got_opentempfd();
1725 err = got_error_from_errno("got_opentempfd");
1728 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1732 if (S_ISLNK(sb->st_mode)) {
1733 err = get_symlink_modification_status(status, ie,
1734 abspath, dirfd, de_name, blob);
1739 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1741 err = got_error_from_errno2("openat", abspath);
1746 f = fdopen(fd, "r");
1748 err = got_error_from_errno2("fdopen", abspath);
1752 hdrlen = got_object_blob_get_hdrlen(blob);
1754 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1755 err = got_object_blob_read_block(&blen, blob);
1758 /* Skip length of blob object header first time around. */
1759 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1760 if (flen == 0 && ferror(f)) {
1761 err = got_error_from_errno("fread");
1764 if (blen - hdrlen == 0) {
1766 *status = GOT_STATUS_MODIFY;
1768 } else if (flen == 0) {
1769 if (blen - hdrlen != 0)
1770 *status = GOT_STATUS_MODIFY;
1772 } else if (blen - hdrlen == flen) {
1773 /* Skip blob object header first time around. */
1774 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1775 *status = GOT_STATUS_MODIFY;
1779 *status = GOT_STATUS_MODIFY;
1785 if (*status == GOT_STATUS_MODIFY) {
1787 err = get_modified_file_content_status(status, f);
1788 } else if (xbit_differs(ie, sb->st_mode))
1789 *status = GOT_STATUS_MODE_CHANGE;
1791 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1792 err = got_error_from_errno("close");
1794 got_object_blob_close(blob);
1795 if (f != NULL && fclose(f) == EOF && err == NULL)
1796 err = got_error_from_errno2("fclose", abspath);
1797 if (fd != -1 && close(fd) == -1 && err == NULL)
1798 err = got_error_from_errno2("close", abspath);
1803 * Update timestamps in the file index if a file is unmodified and
1804 * we had to run a full content comparison to find out.
1806 static const struct got_error *
1807 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1808 struct got_fileindex_entry *ie, struct stat *sb)
1810 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1811 return got_fileindex_entry_update(ie, wt_fd, path,
1812 ie->blob_sha1, ie->commit_sha1, 1);
1817 static const struct got_error *
1818 update_blob(struct got_worktree *worktree,
1819 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1820 struct got_tree_entry *te, const char *path,
1821 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1824 const struct got_error *err = NULL;
1825 struct got_blob_object *blob = NULL;
1826 char *ondisk_path = NULL;
1827 unsigned char status = GOT_STATUS_NO_CHANGE;
1829 int fd1 = -1, fd2 = -1;
1831 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1832 return got_error_from_errno("asprintf");
1835 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1836 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1839 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1843 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1844 sb.st_mode = got_fileindex_perms_to_st(ie);
1846 if (stat(ondisk_path, &sb) == -1) {
1847 if (errno != ENOENT) {
1848 err = got_error_from_errno2("stat",
1852 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1853 status = GOT_STATUS_UNVERSIONED;
1855 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1856 status = GOT_STATUS_UNVERSIONED;
1858 status = GOT_STATUS_OBSTRUCTED;
1862 if (status == GOT_STATUS_OBSTRUCTED) {
1864 got_fileindex_entry_mark_skipped(ie);
1865 err = (*progress_cb)(progress_arg, status, path);
1868 if (status == GOT_STATUS_CONFLICT) {
1870 got_fileindex_entry_mark_skipped(ie);
1871 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1876 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1877 (S_ISLNK(te->mode) ||
1878 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1880 * This is a regular file or an installed bad symlink.
1881 * If the file index indicates that this file is already
1882 * up-to-date with respect to the repository we can skip
1883 * updating contents of this file.
1885 if (got_fileindex_entry_has_commit(ie) &&
1886 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1887 SHA1_DIGEST_LENGTH) == 0) {
1889 err = sync_timestamps(worktree->root_fd,
1890 path, status, ie, &sb);
1893 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1897 if (got_fileindex_entry_has_blob(ie) &&
1898 memcmp(ie->blob_sha1, te->id.sha1,
1899 SHA1_DIGEST_LENGTH) == 0) {
1900 /* Different commit but the same blob. */
1901 err = sync_timestamps(worktree->root_fd,
1902 path, status, ie, &sb);
1905 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1911 fd1 = got_opentempfd();
1913 err = got_error_from_errno("got_opentempfd");
1916 err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
1920 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1921 int update_timestamps;
1922 struct got_blob_object *blob2 = NULL;
1923 char *label_orig = NULL;
1924 if (got_fileindex_entry_has_blob(ie)) {
1925 fd2 = got_opentempfd();
1927 err = got_error_from_errno("got_opentempfd");
1930 struct got_object_id id2;
1931 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1932 err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
1937 if (got_fileindex_entry_has_commit(ie)) {
1938 char id_str[SHA1_DIGEST_STRING_LENGTH];
1939 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1940 sizeof(id_str)) == NULL) {
1941 err = got_error_path(id_str,
1942 GOT_ERR_BAD_OBJ_ID_STR);
1945 if (asprintf(&label_orig, "%s: commit %s",
1946 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1947 err = got_error_from_errno("asprintf");
1951 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1953 err = got_object_blob_read_to_str(&link_target, blob);
1956 err = merge_symlink(worktree, blob2, ondisk_path, path,
1957 label_orig, link_target, worktree->base_commit_id,
1958 repo, progress_cb, progress_arg);
1961 err = merge_blob(&update_timestamps, worktree, blob2,
1962 ondisk_path, path, sb.st_mode, label_orig, blob,
1963 worktree->base_commit_id, repo,
1964 progress_cb, progress_arg);
1967 if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
1968 err = got_error_from_errno("close");
1972 got_object_blob_close(blob2);
1976 * Do not update timestamps of files with local changes.
1977 * Otherwise, a future status walk would treat them as
1978 * unmodified files again.
1980 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1981 blob->id.sha1, worktree->base_commit_id->sha1,
1983 } else if (status == GOT_STATUS_MODE_CHANGE) {
1984 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1985 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1986 } else if (status == GOT_STATUS_DELETE) {
1987 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1990 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1991 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1995 int is_bad_symlink = 0;
1996 if (S_ISLNK(te->mode)) {
1997 err = install_symlink(&is_bad_symlink, worktree,
1998 ondisk_path, path, blob,
1999 status == GOT_STATUS_MISSING, 0,
2000 status == GOT_STATUS_UNVERSIONED, 0,
2001 repo, progress_cb, progress_arg);
2003 err = install_blob(worktree, ondisk_path, path,
2004 te->mode, sb.st_mode, blob,
2005 status == GOT_STATUS_MISSING, 0, 0,
2006 status == GOT_STATUS_UNVERSIONED, repo,
2007 progress_cb, progress_arg);
2013 err = got_fileindex_entry_update(ie,
2014 worktree->root_fd, path, blob->id.sha1,
2015 worktree->base_commit_id->sha1, 1);
2017 err = create_fileindex_entry(&ie, fileindex,
2018 worktree->base_commit_id, worktree->root_fd, path,
2024 if (is_bad_symlink) {
2025 got_fileindex_entry_filetype_set(ie,
2026 GOT_FILEIDX_MODE_BAD_SYMLINK);
2030 if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2031 err = got_error_from_errno("close");
2034 got_object_blob_close(blob);
2040 static const struct got_error *
2041 remove_ondisk_file(const char *root_path, const char *path)
2043 const struct got_error *err = NULL;
2044 char *ondisk_path = NULL, *parent = NULL;
2046 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2047 return got_error_from_errno("asprintf");
2049 if (unlink(ondisk_path) == -1) {
2050 if (errno != ENOENT)
2051 err = got_error_from_errno2("unlink", ondisk_path);
2053 size_t root_len = strlen(root_path);
2054 err = got_path_dirname(&parent, ondisk_path);
2057 while (got_path_cmp(parent, root_path,
2058 strlen(parent), root_len) != 0) {
2060 ondisk_path = parent;
2062 if (rmdir(ondisk_path) == -1) {
2063 if (errno != ENOTEMPTY)
2064 err = got_error_from_errno2("rmdir",
2068 err = got_path_dirname(&parent, ondisk_path);
2079 static const struct got_error *
2080 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2081 struct got_fileindex_entry *ie, struct got_repository *repo,
2082 got_worktree_checkout_cb progress_cb, void *progress_arg)
2084 const struct got_error *err = NULL;
2085 unsigned char status;
2089 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2090 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2092 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2094 return got_error_from_errno("asprintf");
2096 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2100 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2101 char ondisk_target[PATH_MAX];
2102 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2103 sizeof(ondisk_target));
2104 if (ondisk_len == -1) {
2105 err = got_error_from_errno2("readlink", ondisk_path);
2108 ondisk_target[ondisk_len] = '\0';
2109 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2110 NULL, NULL, /* XXX pass common ancestor info? */
2111 ondisk_target, ondisk_path);
2114 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2119 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2120 status == GOT_STATUS_ADD) {
2121 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2125 * Preserve the working file and change the deleted blob's
2126 * entry into a schedule-add entry.
2128 err = got_fileindex_entry_update(ie, worktree->root_fd,
2129 ie->path, NULL, NULL, 0);
2131 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2134 if (status == GOT_STATUS_NO_CHANGE) {
2135 err = remove_ondisk_file(worktree->root_path, ie->path);
2139 got_fileindex_entry_remove(fileindex, ie);
2146 struct diff_cb_arg {
2147 struct got_fileindex *fileindex;
2148 struct got_worktree *worktree;
2149 struct got_repository *repo;
2150 got_worktree_checkout_cb progress_cb;
2152 got_cancel_cb cancel_cb;
2156 static const struct got_error *
2157 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2158 struct got_tree_entry *te, const char *parent_path)
2160 struct diff_cb_arg *a = arg;
2162 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2163 return got_error(GOT_ERR_CANCELLED);
2165 return update_blob(a->worktree, a->fileindex, ie, te,
2166 ie->path, a->repo, a->progress_cb, a->progress_arg);
2169 static const struct got_error *
2170 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2172 struct diff_cb_arg *a = arg;
2174 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2175 return got_error(GOT_ERR_CANCELLED);
2177 return delete_blob(a->worktree, a->fileindex, ie,
2178 a->repo, a->progress_cb, a->progress_arg);
2181 static const struct got_error *
2182 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2184 struct diff_cb_arg *a = arg;
2185 const struct got_error *err;
2188 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2189 return got_error(GOT_ERR_CANCELLED);
2191 if (got_object_tree_entry_is_submodule(te))
2194 if (asprintf(&path, "%s%s%s", parent_path,
2195 parent_path[0] ? "/" : "", te->name)
2197 return got_error_from_errno("asprintf");
2199 if (S_ISDIR(te->mode))
2200 err = add_dir_on_disk(a->worktree, path);
2202 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2203 a->repo, a->progress_cb, a->progress_arg);
2209 const struct got_error *
2210 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2212 uint32_t uuid_status;
2214 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2215 if (uuid_status != uuid_s_ok) {
2217 return got_error_uuid(uuid_status, "uuid_to_string");
2223 static const struct got_error *
2224 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2226 const struct got_error *err = NULL;
2227 char *uuidstr = NULL;
2231 err = got_worktree_get_uuid(&uuidstr, worktree);
2235 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2236 err = got_error_from_errno("asprintf");
2243 const struct got_error *
2244 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2246 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2249 static const struct got_error *
2250 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2252 return get_ref_name(refname, worktree,
2253 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2256 static const struct got_error *
2257 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2259 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2262 static const struct got_error *
2263 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2265 return get_ref_name(refname, worktree,
2266 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2269 static const struct got_error *
2270 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2272 return get_ref_name(refname, worktree,
2273 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2276 static const struct got_error *
2277 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2279 return get_ref_name(refname, worktree,
2280 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2283 static const struct got_error *
2284 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2286 return get_ref_name(refname, worktree,
2287 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2290 static const struct got_error *
2291 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2293 return get_ref_name(refname, worktree,
2294 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2297 static const struct got_error *
2298 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2300 return get_ref_name(refname, worktree,
2301 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2304 const struct got_error *
2305 got_worktree_get_histedit_script_path(char **path,
2306 struct got_worktree *worktree)
2308 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2309 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2311 return got_error_from_errno("asprintf");
2316 static const struct got_error *
2317 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2319 return get_ref_name(refname, worktree,
2320 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2323 static const struct got_error *
2324 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2326 return get_ref_name(refname, worktree,
2327 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2331 * Prevent Git's garbage collector from deleting our base commit by
2332 * setting a reference to our base commit's ID.
2334 static const struct got_error *
2335 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2337 const struct got_error *err = NULL;
2338 struct got_reference *ref = NULL;
2341 err = got_worktree_get_base_ref_name(&refname, worktree);
2345 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2349 err = got_ref_write(ref, repo);
2357 static const struct got_error *
2358 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2360 const struct got_error *err = NULL;
2362 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2363 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2364 err = got_error_from_errno("asprintf");
2365 *fileindex_path = NULL;
2371 static const struct got_error *
2372 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2373 struct got_worktree *worktree)
2375 const struct got_error *err = NULL;
2378 *fileindex_path = NULL;
2379 *fileindex = got_fileindex_alloc();
2380 if (*fileindex == NULL)
2381 return got_error_from_errno("got_fileindex_alloc");
2383 err = get_fileindex_path(fileindex_path, worktree);
2387 index = fopen(*fileindex_path, "rbe");
2388 if (index == NULL) {
2389 if (errno != ENOENT)
2390 err = got_error_from_errno2("fopen", *fileindex_path);
2392 err = got_fileindex_read(*fileindex, index);
2393 if (fclose(index) == EOF && err == NULL)
2394 err = got_error_from_errno("fclose");
2398 free(*fileindex_path);
2399 *fileindex_path = NULL;
2400 got_fileindex_free(*fileindex);
2406 struct bump_base_commit_id_arg {
2407 struct got_object_id *base_commit_id;
2410 const char *entry_name;
2411 got_worktree_checkout_cb progress_cb;
2415 /* Bump base commit ID of all files within an updated part of the work tree. */
2416 static const struct got_error *
2417 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2419 const struct got_error *err;
2420 struct bump_base_commit_id_arg *a = arg;
2422 if (a->entry_name) {
2423 if (strcmp(ie->path, a->path) != 0)
2425 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2428 if (got_fileindex_entry_was_skipped(ie))
2431 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2432 SHA1_DIGEST_LENGTH) == 0)
2435 if (a->progress_cb) {
2436 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2441 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2445 static const struct got_error *
2446 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2447 struct got_fileindex *fileindex,
2448 got_worktree_checkout_cb progress_cb, void *progress_arg)
2450 struct bump_base_commit_id_arg bbc_arg;
2452 bbc_arg.base_commit_id = worktree->base_commit_id;
2453 bbc_arg.entry_name = NULL;
2455 bbc_arg.path_len = 0;
2456 bbc_arg.progress_cb = progress_cb;
2457 bbc_arg.progress_arg = progress_arg;
2459 return got_fileindex_for_each_entry_safe(fileindex,
2460 bump_base_commit_id, &bbc_arg);
2463 static const struct got_error *
2464 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2466 const struct got_error *err = NULL;
2467 char *new_fileindex_path = NULL;
2468 FILE *new_index = NULL;
2469 struct timespec timeout;
2471 err = got_opentemp_named(&new_fileindex_path, &new_index,
2476 err = got_fileindex_write(fileindex, new_index);
2480 if (rename(new_fileindex_path, fileindex_path) != 0) {
2481 err = got_error_from_errno3("rename", new_fileindex_path,
2483 unlink(new_fileindex_path);
2487 * Sleep for a short amount of time to ensure that files modified after
2488 * this program exits have a different time stamp from the one which
2489 * was recorded in the file index.
2492 timeout.tv_nsec = 1;
2493 nanosleep(&timeout, NULL);
2497 free(new_fileindex_path);
2501 static const struct got_error *
2502 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2503 struct got_object_id **tree_id, const char *wt_relpath,
2504 struct got_commit_object *base_commit, struct got_worktree *worktree,
2505 struct got_repository *repo)
2507 const struct got_error *err = NULL;
2508 struct got_object_id *id = NULL;
2509 char *in_repo_path = NULL;
2510 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2512 *entry_type = GOT_OBJ_TYPE_ANY;
2513 *tree_relpath = NULL;
2516 if (wt_relpath[0] == '\0') {
2517 /* Check out all files within the work tree. */
2518 *entry_type = GOT_OBJ_TYPE_TREE;
2519 *tree_relpath = strdup("");
2520 if (*tree_relpath == NULL) {
2521 err = got_error_from_errno("strdup");
2524 err = got_object_id_by_path(tree_id, repo, base_commit,
2525 worktree->path_prefix);
2531 /* Check out a subset of files in the work tree. */
2533 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2534 is_root_wt ? "" : "/", wt_relpath) == -1) {
2535 err = got_error_from_errno("asprintf");
2539 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2544 in_repo_path = NULL;
2546 err = got_object_get_type(entry_type, repo, id);
2550 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2551 /* Check out a single file. */
2552 if (strchr(wt_relpath, '/') == NULL) {
2553 /* Check out a single file in work tree's root dir. */
2554 in_repo_path = strdup(worktree->path_prefix);
2555 if (in_repo_path == NULL) {
2556 err = got_error_from_errno("strdup");
2559 *tree_relpath = strdup("");
2560 if (*tree_relpath == NULL) {
2561 err = got_error_from_errno("strdup");
2565 /* Check out a single file in a subdirectory. */
2566 err = got_path_dirname(tree_relpath, wt_relpath);
2569 if (asprintf(&in_repo_path, "%s%s%s",
2570 worktree->path_prefix, is_root_wt ? "" : "/",
2571 *tree_relpath) == -1) {
2572 err = got_error_from_errno("asprintf");
2576 err = got_object_id_by_path(tree_id, repo,
2577 base_commit, in_repo_path);
2579 /* Check out all files within a subdirectory. */
2580 *tree_id = got_object_id_dup(id);
2581 if (*tree_id == NULL) {
2582 err = got_error_from_errno("got_object_id_dup");
2585 *tree_relpath = strdup(wt_relpath);
2586 if (*tree_relpath == NULL) {
2587 err = got_error_from_errno("strdup");
2595 *entry_type = GOT_OBJ_TYPE_ANY;
2596 free(*tree_relpath);
2597 *tree_relpath = NULL;
2604 static const struct got_error *
2605 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2606 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2607 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2608 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2610 const struct got_error *err = NULL;
2611 struct got_commit_object *commit = NULL;
2612 struct got_tree_object *tree = NULL;
2613 struct got_fileindex_diff_tree_cb diff_cb;
2614 struct diff_cb_arg arg;
2616 err = ref_base_commit(worktree, repo);
2618 if (!(err->code == GOT_ERR_ERRNO &&
2619 (errno == EACCES || errno == EROFS)))
2621 err = (*progress_cb)(progress_arg,
2622 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2627 err = got_object_open_as_commit(&commit, repo,
2628 worktree->base_commit_id);
2632 err = got_object_open_as_tree(&tree, repo, tree_id);
2637 got_object_tree_find_entry(tree, entry_name) == NULL) {
2638 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2642 diff_cb.diff_old_new = diff_old_new;
2643 diff_cb.diff_old = diff_old;
2644 diff_cb.diff_new = diff_new;
2645 arg.fileindex = fileindex;
2646 arg.worktree = worktree;
2648 arg.progress_cb = progress_cb;
2649 arg.progress_arg = progress_arg;
2650 arg.cancel_cb = cancel_cb;
2651 arg.cancel_arg = cancel_arg;
2652 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2653 entry_name, repo, &diff_cb, &arg);
2656 got_object_tree_close(tree);
2658 got_object_commit_close(commit);
2662 const struct got_error *
2663 got_worktree_checkout_files(struct got_worktree *worktree,
2664 struct got_pathlist_head *paths, struct got_repository *repo,
2665 got_worktree_checkout_cb progress_cb, void *progress_arg,
2666 got_cancel_cb cancel_cb, void *cancel_arg)
2668 const struct got_error *err = NULL, *sync_err, *unlockerr;
2669 struct got_commit_object *commit = NULL;
2670 struct got_tree_object *tree = NULL;
2671 struct got_fileindex *fileindex = NULL;
2672 char *fileindex_path = NULL;
2673 struct got_pathlist_entry *pe;
2674 struct tree_path_data {
2675 STAILQ_ENTRY(tree_path_data) entry;
2676 struct got_object_id *tree_id;
2681 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2683 STAILQ_INIT(&tree_paths);
2685 err = lock_worktree(worktree, LOCK_EX);
2689 err = got_object_open_as_commit(&commit, repo,
2690 worktree->base_commit_id);
2694 /* Map all specified paths to in-repository trees. */
2695 TAILQ_FOREACH(pe, paths, entry) {
2696 tpd = malloc(sizeof(*tpd));
2698 err = got_error_from_errno("malloc");
2702 err = find_tree_entry_for_checkout(&tpd->entry_type,
2703 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2710 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2711 err = got_path_basename(&tpd->entry_name, pe->path);
2719 tpd->entry_name = NULL;
2721 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2725 * Read the file index.
2726 * Checking out files is supposed to be an idempotent operation.
2727 * If the on-disk file index is incomplete we will try to complete it.
2729 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2733 tpd = STAILQ_FIRST(&tree_paths);
2734 TAILQ_FOREACH(pe, paths, entry) {
2735 struct bump_base_commit_id_arg bbc_arg;
2737 err = checkout_files(worktree, fileindex, tpd->relpath,
2738 tpd->tree_id, tpd->entry_name, repo,
2739 progress_cb, progress_arg, cancel_cb, cancel_arg);
2743 bbc_arg.base_commit_id = worktree->base_commit_id;
2744 bbc_arg.entry_name = tpd->entry_name;
2745 bbc_arg.path = pe->path;
2746 bbc_arg.path_len = pe->path_len;
2747 bbc_arg.progress_cb = progress_cb;
2748 bbc_arg.progress_arg = progress_arg;
2749 err = got_fileindex_for_each_entry_safe(fileindex,
2750 bump_base_commit_id, &bbc_arg);
2754 tpd = STAILQ_NEXT(tpd, entry);
2756 sync_err = sync_fileindex(fileindex, fileindex_path);
2757 if (sync_err && err == NULL)
2760 free(fileindex_path);
2762 got_object_tree_close(tree);
2764 got_object_commit_close(commit);
2766 got_fileindex_free(fileindex);
2767 while (!STAILQ_EMPTY(&tree_paths)) {
2768 tpd = STAILQ_FIRST(&tree_paths);
2769 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2774 unlockerr = lock_worktree(worktree, LOCK_SH);
2775 if (unlockerr && err == NULL)
2780 struct merge_file_cb_arg {
2781 struct got_worktree *worktree;
2782 struct got_fileindex *fileindex;
2783 got_worktree_checkout_cb progress_cb;
2785 got_cancel_cb cancel_cb;
2787 const char *label_orig;
2788 struct got_object_id *commit_id2;
2789 int allow_bad_symlinks;
2792 static const struct got_error *
2793 merge_file_cb(void *arg, struct got_blob_object *blob1,
2794 struct got_blob_object *blob2, FILE *f1, FILE *f2,
2795 struct got_object_id *id1, struct got_object_id *id2,
2796 const char *path1, const char *path2,
2797 mode_t mode1, mode_t mode2, struct got_repository *repo)
2799 static const struct got_error *err = NULL;
2800 struct merge_file_cb_arg *a = arg;
2801 struct got_fileindex_entry *ie;
2802 char *ondisk_path = NULL;
2804 unsigned char status;
2805 int local_changes_subsumed;
2806 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2807 char *id_str = NULL, *label_deriv2 = NULL;
2809 if (blob1 && blob2) {
2810 ie = got_fileindex_entry_get(a->fileindex, path2,
2813 return (*a->progress_cb)(a->progress_arg,
2814 GOT_STATUS_MISSING, path2);
2816 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2818 return got_error_from_errno("asprintf");
2820 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2825 if (status == GOT_STATUS_DELETE) {
2826 err = (*a->progress_cb)(a->progress_arg,
2827 GOT_STATUS_MERGE, path2);
2830 if (status != GOT_STATUS_NO_CHANGE &&
2831 status != GOT_STATUS_MODIFY &&
2832 status != GOT_STATUS_CONFLICT &&
2833 status != GOT_STATUS_ADD) {
2834 err = (*a->progress_cb)(a->progress_arg, status, path2);
2838 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2840 err = got_object_blob_read_to_str(&link_target2, blob2);
2843 err = merge_symlink(a->worktree, blob1, ondisk_path,
2844 path2, a->label_orig, link_target2, a->commit_id2,
2845 repo, a->progress_cb, a->progress_arg);
2850 f_orig = got_opentemp();
2851 if (f_orig == NULL) {
2852 err = got_error_from_errno("got_opentemp");
2855 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2860 f_deriv2 = got_opentemp();
2861 if (f_deriv2 == NULL)
2863 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2868 fd = open(ondisk_path,
2869 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2871 err = got_error_from_errno2("open",
2875 f_deriv = fdopen(fd, "r");
2876 if (f_deriv == NULL) {
2877 err = got_error_from_errno2("fdopen",
2882 err = got_object_id_str(&id_str, a->commit_id2);
2885 if (asprintf(&label_deriv2, "%s: commit %s",
2886 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2887 err = got_error_from_errno("asprintf");
2890 err = merge_file(&local_changes_subsumed, a->worktree,
2891 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2892 sb.st_mode, a->label_orig, NULL, label_deriv2,
2893 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2894 a->progress_cb, a->progress_arg);
2897 ie = got_fileindex_entry_get(a->fileindex, path1,
2900 return (*a->progress_cb)(a->progress_arg,
2901 GOT_STATUS_MISSING, path1);
2903 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2905 return got_error_from_errno("asprintf");
2907 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2913 case GOT_STATUS_NO_CHANGE:
2914 err = (*a->progress_cb)(a->progress_arg,
2915 GOT_STATUS_DELETE, path1);
2918 err = remove_ondisk_file(a->worktree->root_path, path1);
2922 got_fileindex_entry_mark_deleted_from_disk(ie);
2924 case GOT_STATUS_DELETE:
2925 case GOT_STATUS_MISSING:
2926 err = (*a->progress_cb)(a->progress_arg,
2927 GOT_STATUS_DELETE, path1);
2931 got_fileindex_entry_mark_deleted_from_disk(ie);
2933 case GOT_STATUS_ADD: {
2934 struct got_object_id *id;
2938 * Delete the added file only if its content already
2939 * exists in the repository.
2941 err = got_object_blob_file_create(&id, &blob1_f,
2942 &blob1_size, path1);
2945 if (got_object_id_cmp(id, id1) == 0) {
2946 err = (*a->progress_cb)(a->progress_arg,
2947 GOT_STATUS_DELETE, path1);
2950 err = remove_ondisk_file(a->worktree->root_path,
2955 got_fileindex_entry_remove(a->fileindex,
2958 err = (*a->progress_cb)(a->progress_arg,
2959 GOT_STATUS_CANNOT_DELETE, path1);
2961 if (fclose(blob1_f) == EOF && err == NULL)
2962 err = got_error_from_errno("fclose");
2968 case GOT_STATUS_MODIFY:
2969 case GOT_STATUS_CONFLICT:
2970 err = (*a->progress_cb)(a->progress_arg,
2971 GOT_STATUS_CANNOT_DELETE, path1);
2975 case GOT_STATUS_OBSTRUCTED:
2976 err = (*a->progress_cb)(a->progress_arg, status, path1);
2984 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2986 return got_error_from_errno("asprintf");
2987 ie = got_fileindex_entry_get(a->fileindex, path2,
2990 err = get_file_status(&status, &sb, ie, ondisk_path,
2994 if (status != GOT_STATUS_NO_CHANGE &&
2995 status != GOT_STATUS_MODIFY &&
2996 status != GOT_STATUS_CONFLICT &&
2997 status != GOT_STATUS_ADD) {
2998 err = (*a->progress_cb)(a->progress_arg,
3002 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3004 err = got_object_blob_read_to_str(&link_target2,
3008 err = merge_symlink(a->worktree, NULL,
3009 ondisk_path, path2, a->label_orig,
3010 link_target2, a->commit_id2, repo,
3011 a->progress_cb, a->progress_arg);
3013 } else if (S_ISREG(sb.st_mode)) {
3014 err = merge_blob(&local_changes_subsumed,
3015 a->worktree, NULL, ondisk_path, path2,
3016 sb.st_mode, a->label_orig, blob2,
3017 a->commit_id2, repo, a->progress_cb,
3020 err = got_error_path(ondisk_path,
3021 GOT_ERR_FILE_OBSTRUCTED);
3025 if (status == GOT_STATUS_DELETE) {
3026 err = got_fileindex_entry_update(ie,
3027 a->worktree->root_fd, path2, blob2->id.sha1,
3028 a->worktree->base_commit_id->sha1, 0);
3033 int is_bad_symlink = 0;
3034 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3035 if (S_ISLNK(mode2)) {
3036 err = install_symlink(&is_bad_symlink,
3037 a->worktree, ondisk_path, path2, blob2, 0,
3038 0, 1, a->allow_bad_symlinks, repo,
3039 a->progress_cb, a->progress_arg);
3041 err = install_blob(a->worktree, ondisk_path, path2,
3042 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3043 a->progress_cb, a->progress_arg);
3047 err = got_fileindex_entry_alloc(&ie, path2);
3050 err = got_fileindex_entry_update(ie,
3051 a->worktree->root_fd, path2, NULL, NULL, 1);
3053 got_fileindex_entry_free(ie);
3056 err = got_fileindex_entry_add(a->fileindex, ie);
3058 got_fileindex_entry_free(ie);
3061 if (is_bad_symlink) {
3062 got_fileindex_entry_filetype_set(ie,
3063 GOT_FILEIDX_MODE_BAD_SYMLINK);
3068 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3069 err = got_error_from_errno("fclose");
3070 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3071 err = got_error_from_errno("fclose");
3072 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3073 err = got_error_from_errno("fclose");
3080 static const struct got_error *
3081 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3083 struct got_worktree *worktree = arg;
3085 /* Reject merges into a work tree with mixed base commits. */
3086 if (got_fileindex_entry_has_commit(ie) &&
3087 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3088 SHA1_DIGEST_LENGTH) != 0)
3089 return got_error(GOT_ERR_MIXED_COMMITS);
3094 struct check_merge_conflicts_arg {
3095 struct got_worktree *worktree;
3096 struct got_fileindex *fileindex;
3097 struct got_repository *repo;
3100 static const struct got_error *
3101 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3102 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3103 struct got_object_id *id1, struct got_object_id *id2,
3104 const char *path1, const char *path2,
3105 mode_t mode1, mode_t mode2, struct got_repository *repo)
3107 const struct got_error *err = NULL;
3108 struct check_merge_conflicts_arg *a = arg;
3109 unsigned char status;
3111 struct got_fileindex_entry *ie;
3112 const char *path = path2 ? path2 : path1;
3113 struct got_object_id *id = id2 ? id2 : id1;
3119 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3123 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3125 return got_error_from_errno("asprintf");
3127 /* Reject merges into a work tree with conflicted files. */
3128 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3132 if (status == GOT_STATUS_CONFLICT)
3133 return got_error(GOT_ERR_CONFLICTS);
3138 static const struct got_error *
3139 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3140 const char *fileindex_path, struct got_object_id *commit_id1,
3141 struct got_object_id *commit_id2, struct got_repository *repo,
3142 got_worktree_checkout_cb progress_cb, void *progress_arg,
3143 got_cancel_cb cancel_cb, void *cancel_arg)
3145 const struct got_error *err = NULL, *sync_err;
3146 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3147 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3148 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3149 struct check_merge_conflicts_arg cmc_arg;
3150 struct merge_file_cb_arg arg;
3151 char *label_orig = NULL;
3152 FILE *f1 = NULL, *f2 = NULL;
3153 int fd1 = -1, fd2 = -1;
3156 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3159 err = got_object_id_by_path(&tree_id1, repo, commit1,
3160 worktree->path_prefix);
3161 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3167 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3171 err = got_object_id_str(&id_str, commit_id1);
3175 if (asprintf(&label_orig, "%s: commit %s",
3176 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3177 err = got_error_from_errno("asprintf");
3183 f1 = got_opentemp();
3185 err = got_error_from_errno("got_opentemp");
3190 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3194 err = got_object_id_by_path(&tree_id2, repo, commit2,
3195 worktree->path_prefix);
3199 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3203 f2 = got_opentemp();
3205 err = got_error_from_errno("got_opentemp");
3209 fd1 = got_opentempfd();
3211 err = got_error_from_errno("got_opentempfd");
3215 fd2 = got_opentempfd();
3217 err = got_error_from_errno("got_opentempfd");
3221 cmc_arg.worktree = worktree;
3222 cmc_arg.fileindex = fileindex;
3223 cmc_arg.repo = repo;
3224 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3225 check_merge_conflicts, &cmc_arg, 0);
3229 arg.worktree = worktree;
3230 arg.fileindex = fileindex;
3231 arg.progress_cb = progress_cb;
3232 arg.progress_arg = progress_arg;
3233 arg.cancel_cb = cancel_cb;
3234 arg.cancel_arg = cancel_arg;
3235 arg.label_orig = label_orig;
3236 arg.commit_id2 = commit_id2;
3237 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3238 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3239 merge_file_cb, &arg, 1);
3240 sync_err = sync_fileindex(fileindex, fileindex_path);
3241 if (sync_err && err == NULL)
3245 got_object_commit_close(commit1);
3247 got_object_commit_close(commit2);
3249 got_object_tree_close(tree1);
3251 got_object_tree_close(tree2);
3252 if (f1 && fclose(f1) == EOF && err == NULL)
3253 err = got_error_from_errno("fclose");
3254 if (f2 && fclose(f2) == EOF && err == NULL)
3255 err = got_error_from_errno("fclose");
3256 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3257 err = got_error_from_errno("close");
3258 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3259 err = got_error_from_errno("close");
3264 const struct got_error *
3265 got_worktree_merge_files(struct got_worktree *worktree,
3266 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3267 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3268 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3270 const struct got_error *err, *unlockerr;
3271 char *fileindex_path = NULL;
3272 struct got_fileindex *fileindex = NULL;
3274 err = lock_worktree(worktree, LOCK_EX);
3278 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3282 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3287 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3288 commit_id2, repo, progress_cb, progress_arg,
3289 cancel_cb, cancel_arg);
3292 got_fileindex_free(fileindex);
3293 free(fileindex_path);
3294 unlockerr = lock_worktree(worktree, LOCK_SH);
3295 if (unlockerr && err == NULL)
3300 struct diff_dir_cb_arg {
3301 struct got_fileindex *fileindex;
3302 struct got_worktree *worktree;
3303 const char *status_path;
3304 size_t status_path_len;
3305 struct got_repository *repo;
3306 got_worktree_status_cb status_cb;
3308 got_cancel_cb cancel_cb;
3310 /* A pathlist containing per-directory pathlists of ignore patterns. */
3311 struct got_pathlist_head *ignores;
3312 int report_unchanged;
3316 static const struct got_error *
3317 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3318 int dirfd, const char *de_name,
3319 got_worktree_status_cb status_cb, void *status_arg,
3320 struct got_repository *repo, int report_unchanged)
3322 const struct got_error *err = NULL;
3323 unsigned char status = GOT_STATUS_NO_CHANGE;
3324 unsigned char staged_status = get_staged_status(ie);
3326 struct got_object_id blob_id, commit_id, staged_blob_id;
3327 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3328 struct got_object_id *staged_blob_idp = NULL;
3330 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3334 if (status == GOT_STATUS_NO_CHANGE &&
3335 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3338 if (got_fileindex_entry_has_blob(ie)) {
3339 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3340 blob_idp = &blob_id;
3342 if (got_fileindex_entry_has_commit(ie)) {
3343 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3344 commit_idp = &commit_id;
3346 if (staged_status == GOT_STATUS_ADD ||
3347 staged_status == GOT_STATUS_MODIFY) {
3348 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3349 SHA1_DIGEST_LENGTH);
3350 staged_blob_idp = &staged_blob_id;
3353 return (*status_cb)(status_arg, status, staged_status,
3354 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3357 static const struct got_error *
3358 status_old_new(void *arg, struct got_fileindex_entry *ie,
3359 struct dirent *de, const char *parent_path, int dirfd)
3361 const struct got_error *err = NULL;
3362 struct diff_dir_cb_arg *a = arg;
3365 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3366 return got_error(GOT_ERR_CANCELLED);
3368 if (got_path_cmp(parent_path, a->status_path,
3369 strlen(parent_path), a->status_path_len) != 0 &&
3370 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3373 if (parent_path[0]) {
3374 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3375 parent_path, de->d_name) == -1)
3376 return got_error_from_errno("asprintf");
3378 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3380 return got_error_from_errno("asprintf");
3383 err = report_file_status(ie, abspath, dirfd, de->d_name,
3384 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3389 static const struct got_error *
3390 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3392 struct diff_dir_cb_arg *a = arg;
3393 struct got_object_id blob_id, commit_id;
3394 unsigned char status;
3396 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3397 return got_error(GOT_ERR_CANCELLED);
3399 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3402 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3403 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3404 if (got_fileindex_entry_has_file_on_disk(ie))
3405 status = GOT_STATUS_MISSING;
3407 status = GOT_STATUS_DELETE;
3408 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3409 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3413 free_ignorelist(struct got_pathlist_head *ignorelist)
3415 struct got_pathlist_entry *pe;
3417 TAILQ_FOREACH(pe, ignorelist, entry)
3418 free((char *)pe->path);
3419 got_pathlist_free(ignorelist);
3423 free_ignores(struct got_pathlist_head *ignores)
3425 struct got_pathlist_entry *pe;
3427 TAILQ_FOREACH(pe, ignores, entry) {
3428 struct got_pathlist_head *ignorelist = pe->data;
3429 free_ignorelist(ignorelist);
3430 free((char *)pe->path);
3432 got_pathlist_free(ignores);
3435 static const struct got_error *
3436 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3438 const struct got_error *err = NULL;
3439 struct got_pathlist_entry *pe = NULL;
3440 struct got_pathlist_head *ignorelist;
3441 char *line = NULL, *pattern, *dirpath = NULL;
3442 size_t linesize = 0;
3445 ignorelist = calloc(1, sizeof(*ignorelist));
3446 if (ignorelist == NULL)
3447 return got_error_from_errno("calloc");
3448 TAILQ_INIT(ignorelist);
3450 while ((linelen = getline(&line, &linesize, f)) != -1) {
3451 if (linelen > 0 && line[linelen - 1] == '\n')
3452 line[linelen - 1] = '\0';
3454 /* Git's ignores may contain comments. */
3458 /* Git's negated patterns are not (yet?) supported. */
3462 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3464 err = got_error_from_errno("asprintf");
3467 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3472 err = got_error_from_errno("getline");
3476 dirpath = strdup(path);
3477 if (dirpath == NULL) {
3478 err = got_error_from_errno("strdup");
3481 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3484 if (err || pe == NULL) {
3486 free_ignorelist(ignorelist);
3492 match_ignores(struct got_pathlist_head *ignores, const char *path)
3494 struct got_pathlist_entry *pe;
3496 /* Handle patterns which match in all directories. */
3497 TAILQ_FOREACH(pe, ignores, entry) {
3498 struct got_pathlist_head *ignorelist = pe->data;
3499 struct got_pathlist_entry *pi;
3501 TAILQ_FOREACH(pi, ignorelist, entry) {
3502 const char *p, *pattern = pi->path;
3504 if (strncmp(pattern, "**/", 3) != 0)
3509 if (fnmatch(pattern, p,
3510 FNM_PATHNAME | FNM_LEADING_DIR)) {
3511 /* Retry in next directory. */
3512 while (*p && *p != '/')
3524 * The ignores pathlist contains ignore lists from children before
3525 * parents, so we can find the most specific ignorelist by walking
3526 * ignores backwards.
3528 pe = TAILQ_LAST(ignores, got_pathlist_head);
3530 if (got_path_is_child(path, pe->path, pe->path_len)) {
3531 struct got_pathlist_head *ignorelist = pe->data;
3532 struct got_pathlist_entry *pi;
3533 TAILQ_FOREACH(pi, ignorelist, entry) {
3534 const char *pattern = pi->path;
3535 int flags = FNM_LEADING_DIR;
3536 if (strstr(pattern, "/**/") == NULL)
3537 flags |= FNM_PATHNAME;
3538 if (fnmatch(pattern, path, flags))
3543 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3549 static const struct got_error *
3550 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3551 const char *path, int dirfd, const char *ignores_filename)
3553 const struct got_error *err = NULL;
3556 FILE *ignoresfile = NULL;
3558 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3559 path[0] ? "/" : "", ignores_filename) == -1)
3560 return got_error_from_errno("asprintf");
3563 fd = openat(dirfd, ignores_filename,
3564 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3566 if (errno != ENOENT && errno != EACCES)
3567 err = got_error_from_errno2("openat",
3570 ignoresfile = fdopen(fd, "r");
3571 if (ignoresfile == NULL)
3572 err = got_error_from_errno2("fdopen",
3576 err = read_ignores(ignores, path, ignoresfile);
3580 ignoresfile = fopen(ignorespath, "re");
3581 if (ignoresfile == NULL) {
3582 if (errno != ENOENT && errno != EACCES)
3583 err = got_error_from_errno2("fopen",
3586 err = read_ignores(ignores, path, ignoresfile);
3589 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3590 err = got_error_from_errno2("fclose", path);
3591 if (fd != -1 && close(fd) == -1 && err == NULL)
3592 err = got_error_from_errno2("close", path);
3597 static const struct got_error *
3598 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3601 const struct got_error *err = NULL;
3602 struct diff_dir_cb_arg *a = arg;
3608 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3609 return got_error(GOT_ERR_CANCELLED);
3611 if (parent_path[0]) {
3612 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3613 return got_error_from_errno("asprintf");
3618 if (de->d_type == DT_DIR) {
3619 if (!a->no_ignores && ignore != NULL &&
3620 match_ignores(a->ignores, path))
3622 } else if (!match_ignores(a->ignores, path) &&
3623 got_path_is_child(path, a->status_path, a->status_path_len))
3624 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3625 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3631 static const struct got_error *
3632 status_traverse(void *arg, const char *path, int dirfd)
3634 const struct got_error *err = NULL;
3635 struct diff_dir_cb_arg *a = arg;
3640 err = add_ignores(a->ignores, a->worktree->root_path,
3641 path, dirfd, ".cvsignore");
3645 err = add_ignores(a->ignores, a->worktree->root_path, path,
3646 dirfd, ".gitignore");
3651 static const struct got_error *
3652 report_single_file_status(const char *path, const char *ondisk_path,
3653 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3654 void *status_arg, struct got_repository *repo, int report_unchanged,
3655 struct got_pathlist_head *ignores, int no_ignores)
3657 struct got_fileindex_entry *ie;
3660 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3662 return report_file_status(ie, ondisk_path, -1, NULL,
3663 status_cb, status_arg, repo, report_unchanged);
3665 if (lstat(ondisk_path, &sb) == -1) {
3666 if (errno != ENOENT)
3667 return got_error_from_errno2("lstat", ondisk_path);
3668 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3669 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3672 if (!no_ignores && match_ignores(ignores, path))
3675 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3676 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3677 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3682 static const struct got_error *
3683 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3684 const char *root_path, const char *path)
3686 const struct got_error *err;
3687 char *parent_path, *next_parent_path = NULL;
3689 err = add_ignores(ignores, root_path, "", -1,
3694 err = add_ignores(ignores, root_path, "", -1,
3699 err = got_path_dirname(&parent_path, path);
3701 if (err->code == GOT_ERR_BAD_PATH)
3702 return NULL; /* cannot traverse parent */
3706 err = add_ignores(ignores, root_path, parent_path, -1,
3710 err = add_ignores(ignores, root_path, parent_path, -1,
3714 err = got_path_dirname(&next_parent_path, parent_path);
3716 if (err->code == GOT_ERR_BAD_PATH)
3717 err = NULL; /* traversed everything */
3720 if (got_path_is_root_dir(parent_path))
3723 parent_path = next_parent_path;
3724 next_parent_path = NULL;
3728 free(next_parent_path);
3732 static const struct got_error *
3733 worktree_status(struct got_worktree *worktree, const char *path,
3734 struct got_fileindex *fileindex, struct got_repository *repo,
3735 got_worktree_status_cb status_cb, void *status_arg,
3736 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3737 int report_unchanged)
3739 const struct got_error *err = NULL;
3741 struct got_fileindex_diff_dir_cb fdiff_cb;
3742 struct diff_dir_cb_arg arg;
3743 char *ondisk_path = NULL;
3744 struct got_pathlist_head ignores;
3745 struct got_fileindex_entry *ie;
3747 TAILQ_INIT(&ignores);
3749 if (asprintf(&ondisk_path, "%s%s%s",
3750 worktree->root_path, path[0] ? "/" : "", path) == -1)
3751 return got_error_from_errno("asprintf");
3753 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3755 err = report_single_file_status(path, ondisk_path,
3756 fileindex, status_cb, status_arg, repo,
3757 report_unchanged, &ignores, no_ignores);
3761 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3763 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3764 !got_err_open_nofollow_on_symlink())
3765 err = got_error_from_errno2("open", ondisk_path);
3768 err = add_ignores_from_parent_paths(&ignores,
3769 worktree->root_path, ondisk_path);
3773 err = report_single_file_status(path, ondisk_path,
3774 fileindex, status_cb, status_arg, repo,
3775 report_unchanged, &ignores, no_ignores);
3778 fdiff_cb.diff_old_new = status_old_new;
3779 fdiff_cb.diff_old = status_old;
3780 fdiff_cb.diff_new = status_new;
3781 fdiff_cb.diff_traverse = status_traverse;
3782 arg.fileindex = fileindex;
3783 arg.worktree = worktree;
3784 arg.status_path = path;
3785 arg.status_path_len = strlen(path);
3787 arg.status_cb = status_cb;
3788 arg.status_arg = status_arg;
3789 arg.cancel_cb = cancel_cb;
3790 arg.cancel_arg = cancel_arg;
3791 arg.report_unchanged = report_unchanged;
3792 arg.no_ignores = no_ignores;
3794 err = add_ignores_from_parent_paths(&ignores,
3795 worktree->root_path, path);
3799 arg.ignores = &ignores;
3800 err = got_fileindex_diff_dir(fileindex, fd,
3801 worktree->root_path, path, repo, &fdiff_cb, &arg);
3804 free_ignores(&ignores);
3805 if (fd != -1 && close(fd) == -1 && err == NULL)
3806 err = got_error_from_errno("close");
3811 const struct got_error *
3812 got_worktree_status(struct got_worktree *worktree,
3813 struct got_pathlist_head *paths, struct got_repository *repo,
3814 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3815 got_cancel_cb cancel_cb, void *cancel_arg)
3817 const struct got_error *err = NULL;
3818 char *fileindex_path = NULL;
3819 struct got_fileindex *fileindex = NULL;
3820 struct got_pathlist_entry *pe;
3822 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3826 TAILQ_FOREACH(pe, paths, entry) {
3827 err = worktree_status(worktree, pe->path, fileindex, repo,
3828 status_cb, status_arg, cancel_cb, cancel_arg,
3833 free(fileindex_path);
3834 got_fileindex_free(fileindex);
3838 const struct got_error *
3839 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3842 const struct got_error *err = NULL;
3843 char *resolved = NULL, *cwd = NULL, *path = NULL;
3846 char *abspath = NULL;
3847 char canonpath[PATH_MAX];
3851 cwd = getcwd(NULL, 0);
3853 return got_error_from_errno("getcwd");
3855 if (lstat(arg, &sb) == -1) {
3856 if (errno != ENOENT) {
3857 err = got_error_from_errno2("lstat", arg);
3862 if (S_ISLNK(sb.st_mode)) {
3864 * We cannot use realpath(3) with symlinks since we want to
3865 * operate on the symlink itself.
3866 * But we can make the path absolute, assuming it is relative
3867 * to the current working directory, and then canonicalize it.
3869 if (!got_path_is_absolute(arg)) {
3870 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3871 err = got_error_from_errno("asprintf");
3876 err = got_canonpath(abspath ? abspath : arg, canonpath,
3880 resolved = strdup(canonpath);
3881 if (resolved == NULL) {
3882 err = got_error_from_errno("strdup");
3886 resolved = realpath(arg, NULL);
3887 if (resolved == NULL) {
3888 if (errno != ENOENT) {
3889 err = got_error_from_errno2("realpath", arg);
3892 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3893 err = got_error_from_errno("asprintf");
3896 err = got_canonpath(abspath, canonpath,
3900 resolved = strdup(canonpath);
3901 if (resolved == NULL) {
3902 err = got_error_from_errno("strdup");
3908 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3909 strlen(got_worktree_get_root_path(worktree)))) {
3910 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3914 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3915 err = got_path_skip_common_ancestor(&path,
3916 got_worktree_get_root_path(worktree), resolved);
3922 err = got_error_from_errno("strdup");
3927 /* XXX status walk can't deal with trailing slash! */
3929 while (len > 0 && path[len - 1] == '/') {
3930 path[len - 1] = '\0';
3944 struct schedule_addition_args {
3945 struct got_worktree *worktree;
3946 struct got_fileindex *fileindex;
3947 got_worktree_checkout_cb progress_cb;
3949 struct got_repository *repo;
3952 static const struct got_error *
3953 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3954 const char *relpath, struct got_object_id *blob_id,
3955 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3956 int dirfd, const char *de_name)
3958 struct schedule_addition_args *a = arg;
3959 const struct got_error *err = NULL;
3960 struct got_fileindex_entry *ie;
3964 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3966 return got_error_from_errno("asprintf");
3968 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3970 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3974 /* Re-adding an existing entry is a no-op. */
3975 if (status == GOT_STATUS_ADD)
3977 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3982 if (status != GOT_STATUS_UNVERSIONED) {
3983 if (status == GOT_STATUS_NONEXISTENT)
3984 err = got_error_set_errno(ENOENT, ondisk_path);
3986 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3990 err = got_fileindex_entry_alloc(&ie, relpath);
3993 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3994 relpath, NULL, NULL, 1);
3996 got_fileindex_entry_free(ie);
3999 err = got_fileindex_entry_add(a->fileindex, ie);
4001 got_fileindex_entry_free(ie);
4008 if (status == GOT_STATUS_ADD)
4010 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4013 const struct got_error *
4014 got_worktree_schedule_add(struct got_worktree *worktree,
4015 struct got_pathlist_head *paths,
4016 got_worktree_checkout_cb progress_cb, void *progress_arg,
4017 struct got_repository *repo, int no_ignores)
4019 struct got_fileindex *fileindex = NULL;
4020 char *fileindex_path = NULL;
4021 const struct got_error *err = NULL, *sync_err, *unlockerr;
4022 struct got_pathlist_entry *pe;
4023 struct schedule_addition_args saa;
4025 err = lock_worktree(worktree, LOCK_EX);
4029 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4033 saa.worktree = worktree;
4034 saa.fileindex = fileindex;
4035 saa.progress_cb = progress_cb;
4036 saa.progress_arg = progress_arg;
4039 TAILQ_FOREACH(pe, paths, entry) {
4040 err = worktree_status(worktree, pe->path, fileindex, repo,
4041 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4045 sync_err = sync_fileindex(fileindex, fileindex_path);
4046 if (sync_err && err == NULL)
4049 free(fileindex_path);
4051 got_fileindex_free(fileindex);
4052 unlockerr = lock_worktree(worktree, LOCK_SH);
4053 if (unlockerr && err == NULL)
4058 struct schedule_deletion_args {
4059 struct got_worktree *worktree;
4060 struct got_fileindex *fileindex;
4061 got_worktree_delete_cb progress_cb;
4063 struct got_repository *repo;
4064 int delete_local_mods;
4066 int ignore_missing_paths;
4067 const char *status_codes;
4070 static const struct got_error *
4071 schedule_for_deletion(void *arg, unsigned char status,
4072 unsigned char staged_status, const char *relpath,
4073 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4074 struct got_object_id *commit_id, int dirfd, const char *de_name)
4076 struct schedule_deletion_args *a = arg;
4077 const struct got_error *err = NULL;
4078 struct got_fileindex_entry *ie = NULL;
4082 if (status == GOT_STATUS_NONEXISTENT) {
4083 if (a->ignore_missing_paths)
4085 return got_error_set_errno(ENOENT, relpath);
4088 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4090 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4092 staged_status = get_staged_status(ie);
4093 if (staged_status != GOT_STATUS_NO_CHANGE) {
4094 if (staged_status == GOT_STATUS_DELETE)
4096 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4099 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4101 return got_error_from_errno("asprintf");
4103 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4108 if (a->status_codes) {
4109 size_t ncodes = strlen(a->status_codes);
4111 for (i = 0; i < ncodes ; i++) {
4112 if (status == a->status_codes[i])
4116 /* Do not delete files in non-matching status. */
4120 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4121 a->status_codes[i] != GOT_STATUS_MISSING) {
4122 static char msg[64];
4123 snprintf(msg, sizeof(msg),
4124 "invalid status code '%c'", a->status_codes[i]);
4125 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4130 if (status != GOT_STATUS_NO_CHANGE) {
4131 if (status == GOT_STATUS_DELETE)
4133 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4134 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4137 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4138 err = got_error_set_errno(ENOENT, relpath);
4141 if (status != GOT_STATUS_MODIFY &&
4142 status != GOT_STATUS_MISSING) {
4143 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4148 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4152 if (unlinkat(dirfd, de_name, 0) == -1) {
4153 err = got_error_from_errno2("unlinkat",
4157 } else if (unlink(ondisk_path) == -1) {
4158 err = got_error_from_errno2("unlink", ondisk_path);
4162 root_len = strlen(a->worktree->root_path);
4165 err = got_path_dirname(&parent, ondisk_path);
4169 ondisk_path = parent;
4170 if (rmdir(ondisk_path) == -1) {
4171 if (errno != ENOTEMPTY)
4172 err = got_error_from_errno2("rmdir",
4176 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4177 strlen(ondisk_path), root_len) != 0);
4180 got_fileindex_entry_mark_deleted_from_disk(ie);
4185 if (status == GOT_STATUS_DELETE)
4187 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4188 staged_status, relpath);
4191 const struct got_error *
4192 got_worktree_schedule_delete(struct got_worktree *worktree,
4193 struct got_pathlist_head *paths, int delete_local_mods,
4194 const char *status_codes,
4195 got_worktree_delete_cb progress_cb, void *progress_arg,
4196 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4198 struct got_fileindex *fileindex = NULL;
4199 char *fileindex_path = NULL;
4200 const struct got_error *err = NULL, *sync_err, *unlockerr;
4201 struct got_pathlist_entry *pe;
4202 struct schedule_deletion_args sda;
4204 err = lock_worktree(worktree, LOCK_EX);
4208 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4212 sda.worktree = worktree;
4213 sda.fileindex = fileindex;
4214 sda.progress_cb = progress_cb;
4215 sda.progress_arg = progress_arg;
4217 sda.delete_local_mods = delete_local_mods;
4218 sda.keep_on_disk = keep_on_disk;
4219 sda.ignore_missing_paths = ignore_missing_paths;
4220 sda.status_codes = status_codes;
4222 TAILQ_FOREACH(pe, paths, entry) {
4223 err = worktree_status(worktree, pe->path, fileindex, repo,
4224 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4228 sync_err = sync_fileindex(fileindex, fileindex_path);
4229 if (sync_err && err == NULL)
4232 free(fileindex_path);
4234 got_fileindex_free(fileindex);
4235 unlockerr = lock_worktree(worktree, LOCK_SH);
4236 if (unlockerr && err == NULL)
4241 static const struct got_error *
4242 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4244 const struct got_error *err = NULL;
4246 size_t linesize = 0, n;
4249 linelen = getline(&line, &linesize, infile);
4250 if (linelen == -1) {
4251 if (ferror(infile)) {
4252 err = got_error_from_errno("getline");
4258 n = fwrite(line, 1, linelen, outfile);
4260 err = got_ferror(outfile, GOT_ERR_IO);
4265 n = fwrite(line, 1, linelen, rejectfile);
4267 err = got_ferror(outfile, GOT_ERR_IO);
4274 static const struct got_error *
4275 skip_one_line(FILE *f)
4278 size_t linesize = 0;
4281 linelen = getline(&line, &linesize, f);
4282 if (linelen == -1) {
4284 return got_error_from_errno("getline");
4291 static const struct got_error *
4292 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4293 int start_old, int end_old, int start_new, int end_new,
4294 FILE *outfile, FILE *rejectfile)
4296 const struct got_error *err;
4298 /* Copy old file's lines leading up to patch. */
4299 while (!feof(f1) && *line_cur1 < start_old) {
4300 err = copy_one_line(f1, outfile, NULL);
4305 /* Skip new file's lines leading up to patch. */
4306 while (!feof(f2) && *line_cur2 < start_new) {
4308 err = copy_one_line(f2, NULL, rejectfile);
4310 err = skip_one_line(f2);
4315 /* Copy patched lines. */
4316 while (!feof(f2) && *line_cur2 <= end_new) {
4317 err = copy_one_line(f2, outfile, NULL);
4322 /* Skip over old file's replaced lines. */
4323 while (!feof(f1) && *line_cur1 <= end_old) {
4325 err = copy_one_line(f1, NULL, rejectfile);
4327 err = skip_one_line(f1);
4336 static const struct got_error *
4337 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4338 FILE *outfile, FILE *rejectfile)
4340 const struct got_error *err;
4343 /* Copy old file's lines until EOF. */
4345 err = copy_one_line(f1, outfile, NULL);
4352 /* Copy new file's lines until EOF. */
4354 err = copy_one_line(f2, NULL, rejectfile);
4364 static const struct got_error *
4365 apply_or_reject_change(int *choice, int *nchunks_used,
4366 struct diff_result *diff_result, int n,
4367 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4368 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4369 got_worktree_patch_cb patch_cb, void *patch_arg)
4371 const struct got_error *err = NULL;
4372 struct diff_chunk_context cc = {};
4373 int start_old, end_old, start_new, end_new;
4375 struct diff_output_unidiff_state *diff_state;
4376 struct diff_input_info diff_info;
4379 *choice = GOT_PATCH_CHOICE_NONE;
4381 /* Get changed line numbers without context lines for copy_change(). */
4382 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4383 start_old = cc.left.start;
4384 end_old = cc.left.end;
4385 start_new = cc.right.start;
4386 end_new = cc.right.end;
4388 /* Get the same change with context lines for display. */
4389 memset(&cc, 0, sizeof(cc));
4390 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4392 memset(&diff_info, 0, sizeof(diff_info));
4393 diff_info.left_path = relpath;
4394 diff_info.right_path = relpath;
4396 diff_state = diff_output_unidiff_state_alloc();
4397 if (diff_state == NULL)
4398 return got_error_set_errno(ENOMEM,
4399 "diff_output_unidiff_state_alloc");
4401 hunkfile = got_opentemp();
4402 if (hunkfile == NULL) {
4403 err = got_error_from_errno("got_opentemp");
4407 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4409 if (rc != DIFF_RC_OK) {
4410 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4414 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4415 err = got_ferror(hunkfile, GOT_ERR_IO);
4419 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4420 hunkfile, changeno, nchanges);
4425 case GOT_PATCH_CHOICE_YES:
4426 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4427 end_old, start_new, end_new, outfile, rejectfile);
4429 case GOT_PATCH_CHOICE_NO:
4430 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4431 end_old, start_new, end_new, rejectfile, outfile);
4433 case GOT_PATCH_CHOICE_QUIT:
4436 err = got_error(GOT_ERR_PATCH_CHOICE);
4440 diff_output_unidiff_state_free(diff_state);
4441 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4442 err = got_error_from_errno("fclose");
4446 struct revert_file_args {
4447 struct got_worktree *worktree;
4448 struct got_fileindex *fileindex;
4449 got_worktree_checkout_cb progress_cb;
4451 got_worktree_patch_cb patch_cb;
4453 struct got_repository *repo;
4454 int unlink_added_files;
4457 static const struct got_error *
4458 create_patched_content(char **path_outfile, int reverse_patch,
4459 struct got_object_id *blob_id, const char *path2,
4460 int dirfd2, const char *de_name2,
4461 const char *relpath, struct got_repository *repo,
4462 got_worktree_patch_cb patch_cb, void *patch_arg)
4464 const struct got_error *err, *free_err;
4465 struct got_blob_object *blob = NULL;
4466 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4467 int fd = -1, fd2 = -1;
4468 char link_target[PATH_MAX];
4469 ssize_t link_len = 0;
4470 char *path1 = NULL, *id_str = NULL;
4472 struct got_diffreg_result *diffreg_result = NULL;
4473 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4474 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4476 *path_outfile = NULL;
4478 err = got_object_id_str(&id_str, blob_id);
4483 fd2 = openat(dirfd2, de_name2,
4484 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4486 if (!got_err_open_nofollow_on_symlink()) {
4487 err = got_error_from_errno2("openat", path2);
4490 link_len = readlinkat(dirfd2, de_name2,
4491 link_target, sizeof(link_target));
4492 if (link_len == -1) {
4493 return got_error_from_errno2("readlinkat",
4496 sb2.st_mode = S_IFLNK;
4497 sb2.st_size = link_len;
4500 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4502 if (!got_err_open_nofollow_on_symlink()) {
4503 err = got_error_from_errno2("open", path2);
4506 link_len = readlink(path2, link_target,
4507 sizeof(link_target));
4509 return got_error_from_errno2("readlink", path2);
4510 sb2.st_mode = S_IFLNK;
4511 sb2.st_size = link_len;
4515 if (fstat(fd2, &sb2) == -1) {
4516 err = got_error_from_errno2("fstat", path2);
4520 f2 = fdopen(fd2, "r");
4522 err = got_error_from_errno2("fdopen", path2);
4528 f2 = got_opentemp();
4530 err = got_error_from_errno2("got_opentemp", path2);
4533 n = fwrite(link_target, 1, link_len, f2);
4534 if (n != link_len) {
4535 err = got_ferror(f2, GOT_ERR_IO);
4538 if (fflush(f2) == EOF) {
4539 err = got_error_from_errno("fflush");
4545 fd = got_opentempfd();
4547 err = got_error_from_errno("got_opentempfd");
4551 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4555 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4559 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4563 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4564 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4568 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4572 if (fseek(f1, 0L, SEEK_SET) == -1)
4573 return got_ferror(f1, GOT_ERR_IO);
4574 if (fseek(f2, 0L, SEEK_SET) == -1)
4575 return got_ferror(f2, GOT_ERR_IO);
4577 /* Count the number of actual changes in the diff result. */
4578 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4579 struct diff_chunk_context cc = {};
4580 diff_chunk_context_load_change(&cc, &nchunks_used,
4581 diffreg_result->result, n, 0);
4584 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4586 err = apply_or_reject_change(&choice, &nchunks_used,
4587 diffreg_result->result, n, relpath, f1, f2,
4588 &line_cur1, &line_cur2,
4589 reverse_patch ? NULL : outfile,
4590 reverse_patch ? outfile : NULL,
4591 ++i, nchanges, patch_cb, patch_arg);
4594 if (choice == GOT_PATCH_CHOICE_YES)
4596 else if (choice == GOT_PATCH_CHOICE_QUIT)
4600 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4601 reverse_patch ? NULL : outfile,
4602 reverse_patch ? outfile : NULL);
4606 if (!S_ISLNK(sb2.st_mode)) {
4609 mode = apply_umask(sb2.st_mode);
4610 if (fchmod(fileno(outfile), mode) == -1) {
4611 err = got_error_from_errno2("fchmod", path2);
4618 if (fd != -1 && close(fd) == -1 && err == NULL)
4619 err = got_error_from_errno("close");
4621 got_object_blob_close(blob);
4622 free_err = got_diffreg_result_free(diffreg_result);
4625 if (f1 && fclose(f1) == EOF && err == NULL)
4626 err = got_error_from_errno2("fclose", path1);
4627 if (f2 && fclose(f2) == EOF && err == NULL)
4628 err = got_error_from_errno2("fclose", path2);
4629 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4630 err = got_error_from_errno2("close", path2);
4631 if (outfile && fclose(outfile) == EOF && err == NULL)
4632 err = got_error_from_errno2("fclose", *path_outfile);
4633 if (path1 && unlink(path1) == -1 && err == NULL)
4634 err = got_error_from_errno2("unlink", path1);
4635 if (err || !have_content) {
4636 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4637 err = got_error_from_errno2("unlink", *path_outfile);
4638 free(*path_outfile);
4639 *path_outfile = NULL;
4645 static const struct got_error *
4646 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4647 const char *relpath, struct got_object_id *blob_id,
4648 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4649 int dirfd, const char *de_name)
4651 struct revert_file_args *a = arg;
4652 const struct got_error *err = NULL;
4653 char *parent_path = NULL;
4654 struct got_fileindex_entry *ie;
4655 struct got_commit_object *base_commit = NULL;
4656 struct got_tree_object *tree = NULL;
4657 struct got_object_id *tree_id = NULL;
4658 const struct got_tree_entry *te = NULL;
4659 char *tree_path = NULL, *te_name;
4660 char *ondisk_path = NULL, *path_content = NULL;
4661 struct got_blob_object *blob = NULL;
4664 /* Reverting a staged deletion is a no-op. */
4665 if (status == GOT_STATUS_DELETE &&
4666 staged_status != GOT_STATUS_NO_CHANGE)
4669 if (status == GOT_STATUS_UNVERSIONED)
4670 return (*a->progress_cb)(a->progress_arg,
4671 GOT_STATUS_UNVERSIONED, relpath);
4673 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4675 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4677 /* Construct in-repository path of tree which contains this blob. */
4678 err = got_path_dirname(&parent_path, ie->path);
4680 if (err->code != GOT_ERR_BAD_PATH)
4682 parent_path = strdup("/");
4683 if (parent_path == NULL) {
4684 err = got_error_from_errno("strdup");
4688 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4689 tree_path = strdup(parent_path);
4690 if (tree_path == NULL) {
4691 err = got_error_from_errno("strdup");
4695 if (got_path_is_root_dir(parent_path)) {
4696 tree_path = strdup(a->worktree->path_prefix);
4697 if (tree_path == NULL) {
4698 err = got_error_from_errno("strdup");
4702 if (asprintf(&tree_path, "%s/%s",
4703 a->worktree->path_prefix, parent_path) == -1) {
4704 err = got_error_from_errno("asprintf");
4710 err = got_object_open_as_commit(&base_commit, a->repo,
4711 a->worktree->base_commit_id);
4715 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4717 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4718 (status == GOT_STATUS_ADD ||
4719 staged_status == GOT_STATUS_ADD)))
4722 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4726 err = got_path_basename(&te_name, ie->path);
4730 te = got_object_tree_find_entry(tree, te_name);
4732 if (te == NULL && status != GOT_STATUS_ADD &&
4733 staged_status != GOT_STATUS_ADD) {
4734 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4740 case GOT_STATUS_ADD:
4742 int choice = GOT_PATCH_CHOICE_NONE;
4743 err = (*a->patch_cb)(&choice, a->patch_arg,
4744 status, ie->path, NULL, 1, 1);
4747 if (choice != GOT_PATCH_CHOICE_YES)
4750 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4754 got_fileindex_entry_remove(a->fileindex, ie);
4755 if (a->unlink_added_files) {
4756 if (asprintf(&ondisk_path, "%s/%s",
4757 got_worktree_get_root_path(a->worktree),
4759 err = got_error_from_errno("asprintf");
4762 if (unlink(ondisk_path) == -1) {
4763 err = got_error_from_errno2("unlink",
4769 case GOT_STATUS_DELETE:
4771 int choice = GOT_PATCH_CHOICE_NONE;
4772 err = (*a->patch_cb)(&choice, a->patch_arg,
4773 status, ie->path, NULL, 1, 1);
4776 if (choice != GOT_PATCH_CHOICE_YES)
4780 case GOT_STATUS_MODIFY:
4781 case GOT_STATUS_MODE_CHANGE:
4782 case GOT_STATUS_CONFLICT:
4783 case GOT_STATUS_MISSING: {
4784 struct got_object_id id;
4785 if (staged_status == GOT_STATUS_ADD ||
4786 staged_status == GOT_STATUS_MODIFY) {
4787 memcpy(id.sha1, ie->staged_blob_sha1,
4788 SHA1_DIGEST_LENGTH);
4790 memcpy(id.sha1, ie->blob_sha1,
4791 SHA1_DIGEST_LENGTH);
4792 fd = got_opentempfd();
4794 err = got_error_from_errno("got_opentempfd");
4798 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4802 if (asprintf(&ondisk_path, "%s/%s",
4803 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4804 err = got_error_from_errno("asprintf");
4808 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4809 status == GOT_STATUS_CONFLICT)) {
4810 int is_bad_symlink = 0;
4811 err = create_patched_content(&path_content, 1, &id,
4812 ondisk_path, dirfd, de_name, ie->path, a->repo,
4813 a->patch_cb, a->patch_arg);
4814 if (err || path_content == NULL)
4816 if (te && S_ISLNK(te->mode)) {
4817 if (unlink(path_content) == -1) {
4818 err = got_error_from_errno2("unlink",
4822 err = install_symlink(&is_bad_symlink,
4823 a->worktree, ondisk_path, ie->path,
4824 blob, 0, 1, 0, 0, a->repo,
4825 a->progress_cb, a->progress_arg);
4827 if (rename(path_content, ondisk_path) == -1) {
4828 err = got_error_from_errno3("rename",
4829 path_content, ondisk_path);
4834 int is_bad_symlink = 0;
4835 if (te && S_ISLNK(te->mode)) {
4836 err = install_symlink(&is_bad_symlink,
4837 a->worktree, ondisk_path, ie->path,
4838 blob, 0, 1, 0, 0, a->repo,
4839 a->progress_cb, a->progress_arg);
4841 err = install_blob(a->worktree, ondisk_path,
4843 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4844 got_fileindex_perms_to_st(ie), blob,
4845 0, 1, 0, 0, a->repo,
4846 a->progress_cb, a->progress_arg);
4850 if (status == GOT_STATUS_DELETE ||
4851 status == GOT_STATUS_MODE_CHANGE) {
4852 err = got_fileindex_entry_update(ie,
4853 a->worktree->root_fd, relpath,
4855 a->worktree->base_commit_id->sha1, 1);
4859 if (is_bad_symlink) {
4860 got_fileindex_entry_filetype_set(ie,
4861 GOT_FILEIDX_MODE_BAD_SYMLINK);
4874 if (fd != -1 && close(fd) == -1 && err == NULL)
4875 err = got_error_from_errno("close");
4877 got_object_blob_close(blob);
4879 got_object_tree_close(tree);
4882 got_object_commit_close(base_commit);
4886 const struct got_error *
4887 got_worktree_revert(struct got_worktree *worktree,
4888 struct got_pathlist_head *paths,
4889 got_worktree_checkout_cb progress_cb, void *progress_arg,
4890 got_worktree_patch_cb patch_cb, void *patch_arg,
4891 struct got_repository *repo)
4893 struct got_fileindex *fileindex = NULL;
4894 char *fileindex_path = NULL;
4895 const struct got_error *err = NULL, *unlockerr = NULL;
4896 const struct got_error *sync_err = NULL;
4897 struct got_pathlist_entry *pe;
4898 struct revert_file_args rfa;
4900 err = lock_worktree(worktree, LOCK_EX);
4904 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4908 rfa.worktree = worktree;
4909 rfa.fileindex = fileindex;
4910 rfa.progress_cb = progress_cb;
4911 rfa.progress_arg = progress_arg;
4912 rfa.patch_cb = patch_cb;
4913 rfa.patch_arg = patch_arg;
4915 rfa.unlink_added_files = 0;
4916 TAILQ_FOREACH(pe, paths, entry) {
4917 err = worktree_status(worktree, pe->path, fileindex, repo,
4918 revert_file, &rfa, NULL, NULL, 1, 0);
4922 sync_err = sync_fileindex(fileindex, fileindex_path);
4923 if (sync_err && err == NULL)
4926 free(fileindex_path);
4928 got_fileindex_free(fileindex);
4929 unlockerr = lock_worktree(worktree, LOCK_SH);
4930 if (unlockerr && err == NULL)
4936 free_commitable(struct got_commitable *ct)
4939 free(ct->in_repo_path);
4940 free(ct->ondisk_path);
4942 free(ct->base_blob_id);
4943 free(ct->staged_blob_id);
4944 free(ct->base_commit_id);
4948 struct collect_commitables_arg {
4949 struct got_pathlist_head *commitable_paths;
4950 struct got_repository *repo;
4951 struct got_worktree *worktree;
4952 struct got_fileindex *fileindex;
4953 int have_staged_files;
4954 int allow_bad_symlinks;
4957 static const struct got_error *
4958 collect_commitables(void *arg, unsigned char status,
4959 unsigned char staged_status, const char *relpath,
4960 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4961 struct got_object_id *commit_id, int dirfd, const char *de_name)
4963 struct collect_commitables_arg *a = arg;
4964 const struct got_error *err = NULL;
4965 struct got_commitable *ct = NULL;
4966 struct got_pathlist_entry *new = NULL;
4967 char *parent_path = NULL, *path = NULL;
4970 if (a->have_staged_files) {
4971 if (staged_status != GOT_STATUS_MODIFY &&
4972 staged_status != GOT_STATUS_ADD &&
4973 staged_status != GOT_STATUS_DELETE)
4976 if (status == GOT_STATUS_CONFLICT)
4977 return got_error(GOT_ERR_COMMIT_CONFLICT);
4979 if (status != GOT_STATUS_MODIFY &&
4980 status != GOT_STATUS_MODE_CHANGE &&
4981 status != GOT_STATUS_ADD &&
4982 status != GOT_STATUS_DELETE)
4986 if (asprintf(&path, "/%s", relpath) == -1) {
4987 err = got_error_from_errno("asprintf");
4990 if (strcmp(path, "/") == 0) {
4991 parent_path = strdup("");
4992 if (parent_path == NULL)
4993 return got_error_from_errno("strdup");
4995 err = got_path_dirname(&parent_path, path);
5000 ct = calloc(1, sizeof(*ct));
5002 err = got_error_from_errno("calloc");
5006 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5008 err = got_error_from_errno("asprintf");
5012 if (staged_status == GOT_STATUS_ADD ||
5013 staged_status == GOT_STATUS_MODIFY) {
5014 struct got_fileindex_entry *ie;
5015 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5016 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5017 case GOT_FILEIDX_MODE_REGULAR_FILE:
5018 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5021 case GOT_FILEIDX_MODE_SYMLINK:
5025 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5028 ct->mode |= got_fileindex_entry_perms_get(ie);
5029 } else if (status != GOT_STATUS_DELETE &&
5030 staged_status != GOT_STATUS_DELETE) {
5032 if (fstatat(dirfd, de_name, &sb,
5033 AT_SYMLINK_NOFOLLOW) == -1) {
5034 err = got_error_from_errno2("fstatat",
5038 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5039 err = got_error_from_errno2("lstat", ct->ondisk_path);
5042 ct->mode = sb.st_mode;
5045 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5046 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5048 err = got_error_from_errno("asprintf");
5052 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5053 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5055 char target_path[PATH_MAX];
5057 target_len = readlink(ct->ondisk_path, target_path,
5058 sizeof(target_path));
5059 if (target_len == -1) {
5060 err = got_error_from_errno2("readlink",
5064 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5065 target_len, ct->ondisk_path, a->worktree->root_path);
5068 if (is_bad_symlink) {
5069 err = got_error_path(ct->ondisk_path,
5070 GOT_ERR_BAD_SYMLINK);
5076 ct->status = status;
5077 ct->staged_status = staged_status;
5078 ct->blob_id = NULL; /* will be filled in when blob gets created */
5079 if (ct->status != GOT_STATUS_ADD &&
5080 ct->staged_status != GOT_STATUS_ADD) {
5081 ct->base_blob_id = got_object_id_dup(blob_id);
5082 if (ct->base_blob_id == NULL) {
5083 err = got_error_from_errno("got_object_id_dup");
5086 ct->base_commit_id = got_object_id_dup(commit_id);
5087 if (ct->base_commit_id == NULL) {
5088 err = got_error_from_errno("got_object_id_dup");
5092 if (ct->staged_status == GOT_STATUS_ADD ||
5093 ct->staged_status == GOT_STATUS_MODIFY) {
5094 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5095 if (ct->staged_blob_id == NULL) {
5096 err = got_error_from_errno("got_object_id_dup");
5100 ct->path = strdup(path);
5101 if (ct->path == NULL) {
5102 err = got_error_from_errno("strdup");
5105 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5107 if (ct && (err || new == NULL))
5108 free_commitable(ct);
5114 static const struct got_error *write_tree(struct got_object_id **, int *,
5115 struct got_tree_object *, const char *, struct got_pathlist_head *,
5116 got_worktree_status_cb status_cb, void *status_arg,
5117 struct got_repository *);
5119 static const struct got_error *
5120 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5121 struct got_tree_entry *te, const char *parent_path,
5122 struct got_pathlist_head *commitable_paths,
5123 got_worktree_status_cb status_cb, void *status_arg,
5124 struct got_repository *repo)
5126 const struct got_error *err = NULL;
5127 struct got_tree_object *subtree;
5130 if (asprintf(&subpath, "%s%s%s", parent_path,
5131 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5132 return got_error_from_errno("asprintf");
5134 err = got_object_open_as_tree(&subtree, repo, &te->id);
5138 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5139 commitable_paths, status_cb, status_arg, repo);
5140 got_object_tree_close(subtree);
5145 static const struct got_error *
5146 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5148 const struct got_error *err = NULL;
5149 char *ct_parent_path = NULL;
5153 if (strchr(ct->in_repo_path, '/') == NULL) {
5154 *match = got_path_is_root_dir(path);
5158 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5161 *match = (strcmp(path, ct_parent_path) == 0);
5162 free(ct_parent_path);
5167 get_ct_file_mode(struct got_commitable *ct)
5169 if (S_ISLNK(ct->mode))
5172 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5175 static const struct got_error *
5176 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5177 struct got_tree_entry *te, struct got_commitable *ct)
5179 const struct got_error *err = NULL;
5183 err = got_object_tree_entry_dup(new_te, te);
5187 (*new_te)->mode = get_ct_file_mode(ct);
5189 if (ct->staged_status == GOT_STATUS_MODIFY)
5190 memcpy(&(*new_te)->id, ct->staged_blob_id,
5191 sizeof((*new_te)->id));
5193 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5195 if (err && *new_te) {
5202 static const struct got_error *
5203 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5204 struct got_commitable *ct)
5206 const struct got_error *err = NULL;
5207 char *ct_name = NULL;
5211 *new_te = calloc(1, sizeof(**new_te));
5212 if (*new_te == NULL)
5213 return got_error_from_errno("calloc");
5215 err = got_path_basename(&ct_name, ct->path);
5218 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5219 sizeof((*new_te)->name)) {
5220 err = got_error(GOT_ERR_NO_SPACE);
5224 (*new_te)->mode = get_ct_file_mode(ct);
5226 if (ct->staged_status == GOT_STATUS_ADD)
5227 memcpy(&(*new_te)->id, ct->staged_blob_id,
5228 sizeof((*new_te)->id));
5230 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5233 if (err && *new_te) {
5240 static const struct got_error *
5241 insert_tree_entry(struct got_tree_entry *new_te,
5242 struct got_pathlist_head *paths)
5244 const struct got_error *err = NULL;
5245 struct got_pathlist_entry *new_pe;
5247 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5251 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5255 static const struct got_error *
5256 report_ct_status(struct got_commitable *ct,
5257 got_worktree_status_cb status_cb, void *status_arg)
5259 const char *ct_path = ct->path;
5260 unsigned char status;
5262 if (status_cb == NULL) /* no commit progress output desired */
5265 while (ct_path[0] == '/')
5268 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5269 status = ct->staged_status;
5271 status = ct->status;
5273 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5274 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5277 static const struct got_error *
5278 match_modified_subtree(int *modified, struct got_tree_entry *te,
5279 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5281 const struct got_error *err = NULL;
5282 struct got_pathlist_entry *pe;
5287 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5288 got_path_is_root_dir(base_tree_path) ? "" : "/",
5290 return got_error_from_errno("asprintf");
5292 TAILQ_FOREACH(pe, commitable_paths, entry) {
5293 struct got_commitable *ct = pe->data;
5294 *modified = got_path_is_child(ct->in_repo_path, te_path,
5304 static const struct got_error *
5305 match_deleted_or_modified_ct(struct got_commitable **ctp,
5306 struct got_tree_entry *te, const char *base_tree_path,
5307 struct got_pathlist_head *commitable_paths)
5309 const struct got_error *err = NULL;
5310 struct got_pathlist_entry *pe;
5314 TAILQ_FOREACH(pe, commitable_paths, entry) {
5315 struct got_commitable *ct = pe->data;
5316 char *ct_name = NULL;
5319 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5320 if (ct->status != GOT_STATUS_MODIFY &&
5321 ct->status != GOT_STATUS_MODE_CHANGE &&
5322 ct->status != GOT_STATUS_DELETE)
5325 if (ct->staged_status != GOT_STATUS_MODIFY &&
5326 ct->staged_status != GOT_STATUS_DELETE)
5330 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5333 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5339 err = got_path_basename(&ct_name, pe->path);
5343 if (strcmp(te->name, ct_name) != 0) {
5356 static const struct got_error *
5357 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5358 const char *child_path, const char *path_base_tree,
5359 struct got_pathlist_head *commitable_paths,
5360 got_worktree_status_cb status_cb, void *status_arg,
5361 struct got_repository *repo)
5363 const struct got_error *err = NULL;
5364 struct got_tree_entry *new_te;
5366 struct got_object_id *id = NULL;
5371 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5372 got_path_is_root_dir(path_base_tree) ? "" : "/",
5374 return got_error_from_errno("asprintf");
5376 new_te = calloc(1, sizeof(*new_te));
5378 return got_error_from_errno("calloc");
5379 new_te->mode = S_IFDIR;
5381 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5382 sizeof(new_te->name)) {
5383 err = got_error(GOT_ERR_NO_SPACE);
5386 err = write_tree(&id, &nentries, NULL, subtree_path,
5387 commitable_paths, status_cb, status_arg, repo);
5392 memcpy(&new_te->id, id, sizeof(new_te->id));
5401 static const struct got_error *
5402 write_tree(struct got_object_id **new_tree_id, int *nentries,
5403 struct got_tree_object *base_tree, const char *path_base_tree,
5404 struct got_pathlist_head *commitable_paths,
5405 got_worktree_status_cb status_cb, void *status_arg,
5406 struct got_repository *repo)
5408 const struct got_error *err = NULL;
5409 struct got_pathlist_head paths;
5410 struct got_tree_entry *te, *new_te = NULL;
5411 struct got_pathlist_entry *pe;
5416 /* Insert, and recurse into, newly added entries first. */
5417 TAILQ_FOREACH(pe, commitable_paths, entry) {
5418 struct got_commitable *ct = pe->data;
5419 char *child_path = NULL, *slash;
5421 if ((ct->status != GOT_STATUS_ADD &&
5422 ct->staged_status != GOT_STATUS_ADD) ||
5423 (ct->flags & GOT_COMMITABLE_ADDED))
5426 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5427 strlen(path_base_tree)))
5430 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5435 slash = strchr(child_path, '/');
5436 if (slash == NULL) {
5437 err = alloc_added_blob_tree_entry(&new_te, ct);
5440 err = report_ct_status(ct, status_cb, status_arg);
5443 ct->flags |= GOT_COMMITABLE_ADDED;
5444 err = insert_tree_entry(new_te, &paths);
5449 *slash = '\0'; /* trim trailing path components */
5450 if (base_tree == NULL ||
5451 got_object_tree_find_entry(base_tree, child_path)
5453 err = make_subtree_for_added_blob(&new_te,
5454 child_path, path_base_tree,
5455 commitable_paths, status_cb, status_arg,
5459 err = insert_tree_entry(new_te, &paths);
5468 int i, nbase_entries;
5469 /* Handle modified and deleted entries. */
5470 nbase_entries = got_object_tree_get_nentries(base_tree);
5471 for (i = 0; i < nbase_entries; i++) {
5472 struct got_commitable *ct = NULL;
5474 te = got_object_tree_get_entry(base_tree, i);
5475 if (got_object_tree_entry_is_submodule(te)) {
5476 /* Entry is a submodule; just copy it. */
5477 err = got_object_tree_entry_dup(&new_te, te);
5480 err = insert_tree_entry(new_te, &paths);
5487 if (S_ISDIR(te->mode)) {
5489 err = got_object_tree_entry_dup(&new_te, te);
5492 err = match_modified_subtree(&modified, te,
5493 path_base_tree, commitable_paths);
5496 /* Avoid recursion into unmodified subtrees. */
5498 struct got_object_id *new_id;
5500 err = write_subtree(&new_id,
5502 path_base_tree, commitable_paths,
5503 status_cb, status_arg, repo);
5506 if (nsubentries == 0) {
5507 /* All entries were deleted. */
5511 memcpy(&new_te->id, new_id,
5512 sizeof(new_te->id));
5515 err = insert_tree_entry(new_te, &paths);
5522 err = match_deleted_or_modified_ct(&ct, te,
5523 path_base_tree, commitable_paths);
5527 /* NB: Deleted entries get dropped here. */
5528 if (ct->status == GOT_STATUS_MODIFY ||
5529 ct->status == GOT_STATUS_MODE_CHANGE ||
5530 ct->staged_status == GOT_STATUS_MODIFY) {
5531 err = alloc_modified_blob_tree_entry(
5535 err = insert_tree_entry(new_te, &paths);
5540 err = report_ct_status(ct, status_cb,
5545 /* Entry is unchanged; just copy it. */
5546 err = got_object_tree_entry_dup(&new_te, te);
5549 err = insert_tree_entry(new_te, &paths);
5557 /* Write new list of entries; deleted entries have been dropped. */
5558 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5560 got_pathlist_free(&paths);
5564 static const struct got_error *
5565 update_fileindex_after_commit(struct got_worktree *worktree,
5566 struct got_pathlist_head *commitable_paths,
5567 struct got_object_id *new_base_commit_id,
5568 struct got_fileindex *fileindex, int have_staged_files)
5570 const struct got_error *err = NULL;
5571 struct got_pathlist_entry *pe;
5572 char *relpath = NULL;
5574 TAILQ_FOREACH(pe, commitable_paths, entry) {
5575 struct got_fileindex_entry *ie;
5576 struct got_commitable *ct = pe->data;
5578 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5580 err = got_path_skip_common_ancestor(&relpath,
5581 worktree->root_path, ct->ondisk_path);
5586 if (ct->status == GOT_STATUS_DELETE ||
5587 ct->staged_status == GOT_STATUS_DELETE) {
5588 got_fileindex_entry_remove(fileindex, ie);
5589 } else if (ct->staged_status == GOT_STATUS_ADD ||
5590 ct->staged_status == GOT_STATUS_MODIFY) {
5591 got_fileindex_entry_stage_set(ie,
5592 GOT_FILEIDX_STAGE_NONE);
5593 got_fileindex_entry_staged_filetype_set(ie, 0);
5595 err = got_fileindex_entry_update(ie,
5596 worktree->root_fd, relpath,
5597 ct->staged_blob_id->sha1,
5598 new_base_commit_id->sha1,
5599 !have_staged_files);
5601 err = got_fileindex_entry_update(ie,
5602 worktree->root_fd, relpath,
5604 new_base_commit_id->sha1,
5605 !have_staged_files);
5607 err = got_fileindex_entry_alloc(&ie, pe->path);
5610 err = got_fileindex_entry_update(ie,
5611 worktree->root_fd, relpath, ct->blob_id->sha1,
5612 new_base_commit_id->sha1, 1);
5614 got_fileindex_entry_free(ie);
5617 err = got_fileindex_entry_add(fileindex, ie);
5619 got_fileindex_entry_free(ie);
5632 static const struct got_error *
5633 check_out_of_date(const char *in_repo_path, unsigned char status,
5634 unsigned char staged_status, struct got_object_id *base_blob_id,
5635 struct got_object_id *base_commit_id,
5636 struct got_object_id *head_commit_id, struct got_repository *repo,
5639 const struct got_error *err = NULL;
5640 struct got_commit_object *commit = NULL;
5641 struct got_object_id *id = NULL;
5643 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5644 /* Trivial case: base commit == head commit */
5645 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5648 * Ensure file content which local changes were based
5649 * on matches file content in the branch head.
5651 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5654 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5656 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5657 err = got_error(ood_errcode);
5659 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5660 err = got_error(ood_errcode);
5662 /* Require that added files don't exist in the branch head. */
5663 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5666 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5667 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5669 err = id ? got_error(ood_errcode) : NULL;
5674 got_object_commit_close(commit);
5678 static const struct got_error *
5679 commit_worktree(struct got_object_id **new_commit_id,
5680 struct got_pathlist_head *commitable_paths,
5681 struct got_object_id *head_commit_id,
5682 struct got_object_id *parent_id2,
5683 struct got_worktree *worktree,
5684 const char *author, const char *committer,
5685 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5686 got_worktree_status_cb status_cb, void *status_arg,
5687 struct got_repository *repo)
5689 const struct got_error *err = NULL, *unlockerr = NULL;
5690 struct got_pathlist_entry *pe;
5691 const char *head_ref_name = NULL;
5692 struct got_commit_object *head_commit = NULL;
5693 struct got_reference *head_ref2 = NULL;
5694 struct got_object_id *head_commit_id2 = NULL;
5695 struct got_tree_object *head_tree = NULL;
5696 struct got_object_id *new_tree_id = NULL;
5697 int nentries, nparents = 0;
5698 struct got_object_id_queue parent_ids;
5699 struct got_object_qid *pid = NULL;
5700 char *logmsg = NULL;
5703 *new_commit_id = NULL;
5705 STAILQ_INIT(&parent_ids);
5707 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5711 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5715 if (commit_msg_cb != NULL) {
5716 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5721 if (logmsg == NULL || strlen(logmsg) == 0) {
5722 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5726 /* Create blobs from added and modified files and record their IDs. */
5727 TAILQ_FOREACH(pe, commitable_paths, entry) {
5728 struct got_commitable *ct = pe->data;
5731 /* Blobs for staged files already exist. */
5732 if (ct->staged_status == GOT_STATUS_ADD ||
5733 ct->staged_status == GOT_STATUS_MODIFY)
5736 if (ct->status != GOT_STATUS_ADD &&
5737 ct->status != GOT_STATUS_MODIFY &&
5738 ct->status != GOT_STATUS_MODE_CHANGE)
5741 if (asprintf(&ondisk_path, "%s/%s",
5742 worktree->root_path, pe->path) == -1) {
5743 err = got_error_from_errno("asprintf");
5746 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5752 /* Recursively write new tree objects. */
5753 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5754 commitable_paths, status_cb, status_arg, repo);
5758 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5761 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5764 err = got_object_qid_alloc(&pid, parent_id2);
5767 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5770 timestamp = time(NULL);
5771 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5772 nparents, author, timestamp, committer, timestamp, logmsg, repo);
5778 /* Check if a concurrent commit to our branch has occurred. */
5779 head_ref_name = got_worktree_get_head_ref_name(worktree);
5780 if (head_ref_name == NULL) {
5781 err = got_error_from_errno("got_worktree_get_head_ref_name");
5784 /* Lock the reference here to prevent concurrent modification. */
5785 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5788 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5791 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5792 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5795 /* Update branch head in repository. */
5796 err = got_ref_change_ref(head_ref2, *new_commit_id);
5799 err = got_ref_write(head_ref2, repo);
5803 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5807 err = ref_base_commit(worktree, repo);
5811 got_object_id_queue_free(&parent_ids);
5813 got_object_tree_close(head_tree);
5815 got_object_commit_close(head_commit);
5816 free(head_commit_id2);
5818 unlockerr = got_ref_unlock(head_ref2);
5819 if (unlockerr && err == NULL)
5821 got_ref_close(head_ref2);
5826 static const struct got_error *
5827 check_path_is_commitable(const char *path,
5828 struct got_pathlist_head *commitable_paths)
5830 struct got_pathlist_entry *cpe = NULL;
5831 size_t path_len = strlen(path);
5833 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5834 struct got_commitable *ct = cpe->data;
5835 const char *ct_path = ct->path;
5837 while (ct_path[0] == '/')
5840 if (strcmp(path, ct_path) == 0 ||
5841 got_path_is_child(ct_path, path, path_len))
5846 return got_error_path(path, GOT_ERR_BAD_PATH);
5851 static const struct got_error *
5852 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5854 int *have_staged_files = arg;
5856 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5857 *have_staged_files = 1;
5858 return got_error(GOT_ERR_CANCELLED);
5864 static const struct got_error *
5865 check_non_staged_files(struct got_fileindex *fileindex,
5866 struct got_pathlist_head *paths)
5868 struct got_pathlist_entry *pe;
5869 struct got_fileindex_entry *ie;
5871 TAILQ_FOREACH(pe, paths, entry) {
5872 if (pe->path[0] == '\0')
5874 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5876 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5877 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5878 return got_error_path(pe->path,
5879 GOT_ERR_FILE_NOT_STAGED);
5885 const struct got_error *
5886 got_worktree_commit(struct got_object_id **new_commit_id,
5887 struct got_worktree *worktree, struct got_pathlist_head *paths,
5888 const char *author, const char *committer, int allow_bad_symlinks,
5889 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5890 got_worktree_status_cb status_cb, void *status_arg,
5891 struct got_repository *repo)
5893 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5894 struct got_fileindex *fileindex = NULL;
5895 char *fileindex_path = NULL;
5896 struct got_pathlist_head commitable_paths;
5897 struct collect_commitables_arg cc_arg;
5898 struct got_pathlist_entry *pe;
5899 struct got_reference *head_ref = NULL;
5900 struct got_object_id *head_commit_id = NULL;
5901 int have_staged_files = 0;
5903 *new_commit_id = NULL;
5905 TAILQ_INIT(&commitable_paths);
5907 err = lock_worktree(worktree, LOCK_EX);
5911 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5915 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5919 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5923 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5924 &have_staged_files);
5925 if (err && err->code != GOT_ERR_CANCELLED)
5927 if (have_staged_files) {
5928 err = check_non_staged_files(fileindex, paths);
5933 cc_arg.commitable_paths = &commitable_paths;
5934 cc_arg.worktree = worktree;
5935 cc_arg.fileindex = fileindex;
5937 cc_arg.have_staged_files = have_staged_files;
5938 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5939 TAILQ_FOREACH(pe, paths, entry) {
5940 err = worktree_status(worktree, pe->path, fileindex, repo,
5941 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5946 if (TAILQ_EMPTY(&commitable_paths)) {
5947 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5951 TAILQ_FOREACH(pe, paths, entry) {
5952 err = check_path_is_commitable(pe->path, &commitable_paths);
5957 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5958 struct got_commitable *ct = pe->data;
5959 const char *ct_path = ct->in_repo_path;
5961 while (ct_path[0] == '/')
5963 err = check_out_of_date(ct_path, ct->status,
5964 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5965 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5971 err = commit_worktree(new_commit_id, &commitable_paths,
5972 head_commit_id, NULL, worktree, author, committer,
5973 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5977 err = update_fileindex_after_commit(worktree, &commitable_paths,
5978 *new_commit_id, fileindex, have_staged_files);
5979 sync_err = sync_fileindex(fileindex, fileindex_path);
5980 if (sync_err && err == NULL)
5984 got_fileindex_free(fileindex);
5985 free(fileindex_path);
5986 unlockerr = lock_worktree(worktree, LOCK_SH);
5987 if (unlockerr && err == NULL)
5989 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5990 struct got_commitable *ct = pe->data;
5991 free_commitable(ct);
5993 got_pathlist_free(&commitable_paths);
5998 got_commitable_get_path(struct got_commitable *ct)
6004 got_commitable_get_status(struct got_commitable *ct)
6009 struct check_rebase_ok_arg {
6010 struct got_worktree *worktree;
6011 struct got_repository *repo;
6014 static const struct got_error *
6015 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6017 const struct got_error *err = NULL;
6018 struct check_rebase_ok_arg *a = arg;
6019 unsigned char status;
6023 /* Reject rebase of a work tree with mixed base commits. */
6024 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6025 SHA1_DIGEST_LENGTH))
6026 return got_error(GOT_ERR_MIXED_COMMITS);
6028 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6030 return got_error_from_errno("asprintf");
6032 /* Reject rebase of a work tree with modified or staged files. */
6033 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6038 if (status != GOT_STATUS_NO_CHANGE)
6039 return got_error(GOT_ERR_MODIFIED);
6040 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6041 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6046 const struct got_error *
6047 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6048 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6049 struct got_worktree *worktree, struct got_reference *branch,
6050 struct got_repository *repo)
6052 const struct got_error *err = NULL;
6053 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6054 char *branch_ref_name = NULL;
6055 char *fileindex_path = NULL;
6056 struct check_rebase_ok_arg ok_arg;
6057 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6058 struct got_object_id *wt_branch_tip = NULL;
6060 *new_base_branch_ref = NULL;
6064 err = lock_worktree(worktree, LOCK_EX);
6068 err = open_fileindex(fileindex, &fileindex_path, worktree);
6072 ok_arg.worktree = worktree;
6074 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6079 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6083 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6087 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6091 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6096 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6099 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6100 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6104 err = got_ref_alloc_symref(new_base_branch_ref,
6105 new_base_branch_ref_name, wt_branch);
6108 err = got_ref_write(*new_base_branch_ref, repo);
6112 /* TODO Lock original branch's ref while rebasing? */
6114 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6118 err = got_ref_write(branch_ref, repo);
6122 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6123 worktree->base_commit_id);
6126 err = got_ref_write(*tmp_branch, repo);
6130 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6134 free(fileindex_path);
6135 free(tmp_branch_name);
6136 free(new_base_branch_ref_name);
6137 free(branch_ref_name);
6139 got_ref_close(branch_ref);
6141 got_ref_close(wt_branch);
6142 free(wt_branch_tip);
6144 if (*new_base_branch_ref) {
6145 got_ref_close(*new_base_branch_ref);
6146 *new_base_branch_ref = NULL;
6149 got_ref_close(*tmp_branch);
6153 got_fileindex_free(*fileindex);
6156 lock_worktree(worktree, LOCK_SH);
6161 const struct got_error *
6162 got_worktree_rebase_continue(struct got_object_id **commit_id,
6163 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6164 struct got_reference **branch, struct got_fileindex **fileindex,
6165 struct got_worktree *worktree, struct got_repository *repo)
6167 const struct got_error *err;
6168 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6169 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6170 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6171 char *fileindex_path = NULL;
6172 int have_staged_files = 0;
6175 *new_base_branch = NULL;
6180 err = lock_worktree(worktree, LOCK_EX);
6184 err = open_fileindex(fileindex, &fileindex_path, worktree);
6188 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6189 &have_staged_files);
6190 if (err && err->code != GOT_ERR_CANCELLED)
6192 if (have_staged_files) {
6193 err = got_error(GOT_ERR_STAGED_PATHS);
6197 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6201 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6205 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6209 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6213 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6217 err = got_ref_open(branch, repo,
6218 got_ref_get_symref_target(branch_ref), 0);
6222 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6226 err = got_ref_resolve(commit_id, repo, commit_ref);
6230 err = got_ref_open(new_base_branch, repo,
6231 new_base_branch_ref_name, 0);
6235 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6239 free(commit_ref_name);
6240 free(branch_ref_name);
6241 free(fileindex_path);
6243 got_ref_close(commit_ref);
6245 got_ref_close(branch_ref);
6250 got_ref_close(*tmp_branch);
6253 if (*new_base_branch) {
6254 got_ref_close(*new_base_branch);
6255 *new_base_branch = NULL;
6258 got_ref_close(*branch);
6262 got_fileindex_free(*fileindex);
6265 lock_worktree(worktree, LOCK_SH);
6270 const struct got_error *
6271 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6273 const struct got_error *err;
6274 char *tmp_branch_name = NULL;
6276 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6280 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6281 free(tmp_branch_name);
6285 static const struct got_error *
6286 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6287 char **logmsg, void *arg)
6293 static const struct got_error *
6294 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6295 const char *path, struct got_object_id *blob_id,
6296 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6297 int dirfd, const char *de_name)
6302 struct collect_merged_paths_arg {
6303 got_worktree_checkout_cb progress_cb;
6305 struct got_pathlist_head *merged_paths;
6308 static const struct got_error *
6309 collect_merged_paths(void *arg, unsigned char status, const char *path)
6311 const struct got_error *err;
6312 struct collect_merged_paths_arg *a = arg;
6314 struct got_pathlist_entry *new;
6316 err = (*a->progress_cb)(a->progress_arg, status, path);
6320 if (status != GOT_STATUS_MERGE &&
6321 status != GOT_STATUS_ADD &&
6322 status != GOT_STATUS_DELETE &&
6323 status != GOT_STATUS_CONFLICT)
6328 return got_error_from_errno("strdup");
6330 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6331 if (err || new == NULL)
6337 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6339 struct got_pathlist_entry *pe;
6341 TAILQ_FOREACH(pe, merged_paths, entry)
6342 free((char *)pe->path);
6344 got_pathlist_free(merged_paths);
6347 static const struct got_error *
6348 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6349 int is_rebase, struct got_repository *repo)
6351 const struct got_error *err;
6352 struct got_reference *commit_ref = NULL;
6354 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6356 if (err->code != GOT_ERR_NOT_REF)
6358 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6361 err = got_ref_write(commit_ref, repo);
6364 } else if (is_rebase) {
6365 struct got_object_id *stored_id;
6368 err = got_ref_resolve(&stored_id, repo, commit_ref);
6371 cmp = got_object_id_cmp(commit_id, stored_id);
6374 err = got_error(GOT_ERR_REBASE_COMMITID);
6380 got_ref_close(commit_ref);
6384 static const struct got_error *
6385 rebase_merge_files(struct got_pathlist_head *merged_paths,
6386 const char *commit_ref_name, struct got_worktree *worktree,
6387 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6388 struct got_object_id *commit_id, struct got_repository *repo,
6389 got_worktree_checkout_cb progress_cb, void *progress_arg,
6390 got_cancel_cb cancel_cb, void *cancel_arg)
6392 const struct got_error *err;
6393 struct got_reference *commit_ref = NULL;
6394 struct collect_merged_paths_arg cmp_arg;
6395 char *fileindex_path;
6397 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6399 err = get_fileindex_path(&fileindex_path, worktree);
6403 cmp_arg.progress_cb = progress_cb;
6404 cmp_arg.progress_arg = progress_arg;
6405 cmp_arg.merged_paths = merged_paths;
6406 err = merge_files(worktree, fileindex, fileindex_path,
6407 parent_commit_id, commit_id, repo, collect_merged_paths,
6408 &cmp_arg, cancel_cb, cancel_arg);
6410 got_ref_close(commit_ref);
6414 const struct got_error *
6415 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6416 struct got_worktree *worktree, struct got_fileindex *fileindex,
6417 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6418 struct got_repository *repo,
6419 got_worktree_checkout_cb progress_cb, void *progress_arg,
6420 got_cancel_cb cancel_cb, void *cancel_arg)
6422 const struct got_error *err;
6423 char *commit_ref_name;
6425 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6429 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6433 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6434 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6435 progress_arg, cancel_cb, cancel_arg);
6437 free(commit_ref_name);
6441 const struct got_error *
6442 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6443 struct got_worktree *worktree, struct got_fileindex *fileindex,
6444 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6445 struct got_repository *repo,
6446 got_worktree_checkout_cb progress_cb, void *progress_arg,
6447 got_cancel_cb cancel_cb, void *cancel_arg)
6449 const struct got_error *err;
6450 char *commit_ref_name;
6452 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6456 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6460 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6461 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6462 progress_arg, cancel_cb, cancel_arg);
6464 free(commit_ref_name);
6468 static const struct got_error *
6469 rebase_commit(struct got_object_id **new_commit_id,
6470 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6471 struct got_worktree *worktree, struct got_fileindex *fileindex,
6472 struct got_reference *tmp_branch, const char *committer,
6473 struct got_commit_object *orig_commit, const char *new_logmsg,
6474 struct got_repository *repo)
6476 const struct got_error *err, *sync_err;
6477 struct got_pathlist_head commitable_paths;
6478 struct collect_commitables_arg cc_arg;
6479 char *fileindex_path = NULL;
6480 struct got_reference *head_ref = NULL;
6481 struct got_object_id *head_commit_id = NULL;
6482 char *logmsg = NULL;
6484 TAILQ_INIT(&commitable_paths);
6485 *new_commit_id = NULL;
6487 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6489 err = get_fileindex_path(&fileindex_path, worktree);
6493 cc_arg.commitable_paths = &commitable_paths;
6494 cc_arg.worktree = worktree;
6496 cc_arg.have_staged_files = 0;
6498 * If possible get the status of individual files directly to
6499 * avoid crawling the entire work tree once per rebased commit.
6501 * Ideally, merged_paths would contain a list of commitables
6502 * we could use so we could skip worktree_status() entirely.
6503 * However, we would then need carefully keep track of cumulative
6504 * effects of operations such as file additions and deletions
6505 * in 'got histedit -f' (folding multiple commits into one),
6506 * and this extra complexity is not really worth it.
6509 struct got_pathlist_entry *pe;
6510 TAILQ_FOREACH(pe, merged_paths, entry) {
6511 err = worktree_status(worktree, pe->path, fileindex,
6512 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6518 err = worktree_status(worktree, "", fileindex, repo,
6519 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6524 if (TAILQ_EMPTY(&commitable_paths)) {
6525 /* No-op change; commit will be elided. */
6526 err = got_ref_delete(commit_ref, repo);
6529 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6533 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6537 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6542 logmsg = strdup(new_logmsg);
6543 if (logmsg == NULL) {
6544 err = got_error_from_errno("strdup");
6548 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6553 /* NB: commit_worktree will call free(logmsg) */
6554 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6555 NULL, worktree, got_object_commit_get_author(orig_commit),
6556 committer ? committer :
6557 got_object_commit_get_committer(orig_commit),
6558 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6562 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6566 err = got_ref_delete(commit_ref, repo);
6570 err = update_fileindex_after_commit(worktree, &commitable_paths,
6571 *new_commit_id, fileindex, 0);
6572 sync_err = sync_fileindex(fileindex, fileindex_path);
6573 if (sync_err && err == NULL)
6576 free(fileindex_path);
6577 free(head_commit_id);
6579 got_ref_close(head_ref);
6581 free(*new_commit_id);
6582 *new_commit_id = NULL;
6587 const struct got_error *
6588 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6589 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6590 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6591 const char *committer, struct got_commit_object *orig_commit,
6592 struct got_object_id *orig_commit_id, struct got_repository *repo)
6594 const struct got_error *err;
6595 char *commit_ref_name;
6596 struct got_reference *commit_ref = NULL;
6597 struct got_object_id *commit_id = NULL;
6599 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6603 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6606 err = got_ref_resolve(&commit_id, repo, commit_ref);
6609 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6610 err = got_error(GOT_ERR_REBASE_COMMITID);
6614 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6615 worktree, fileindex, tmp_branch, committer, orig_commit,
6619 got_ref_close(commit_ref);
6620 free(commit_ref_name);
6625 const struct got_error *
6626 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6627 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6628 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6629 const char *committer, struct got_commit_object *orig_commit,
6630 struct got_object_id *orig_commit_id, const char *new_logmsg,
6631 struct got_repository *repo)
6633 const struct got_error *err;
6634 char *commit_ref_name;
6635 struct got_reference *commit_ref = NULL;
6637 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6641 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6645 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6646 worktree, fileindex, tmp_branch, committer, orig_commit,
6650 got_ref_close(commit_ref);
6651 free(commit_ref_name);
6655 const struct got_error *
6656 got_worktree_rebase_postpone(struct got_worktree *worktree,
6657 struct got_fileindex *fileindex)
6660 got_fileindex_free(fileindex);
6661 return lock_worktree(worktree, LOCK_SH);
6664 static const struct got_error *
6665 delete_ref(const char *name, struct got_repository *repo)
6667 const struct got_error *err;
6668 struct got_reference *ref;
6670 err = got_ref_open(&ref, repo, name, 0);
6672 if (err->code == GOT_ERR_NOT_REF)
6677 err = got_ref_delete(ref, repo);
6682 static const struct got_error *
6683 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6685 const struct got_error *err;
6686 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6687 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6689 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6692 err = delete_ref(tmp_branch_name, repo);
6696 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6699 err = delete_ref(new_base_branch_ref_name, repo);
6703 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6706 err = delete_ref(branch_ref_name, repo);
6710 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6713 err = delete_ref(commit_ref_name, repo);
6718 free(tmp_branch_name);
6719 free(new_base_branch_ref_name);
6720 free(branch_ref_name);
6721 free(commit_ref_name);
6725 static const struct got_error *
6726 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6727 struct got_object_id *new_commit_id, struct got_repository *repo)
6729 const struct got_error *err;
6730 struct got_reference *ref = NULL;
6731 struct got_object_id *old_commit_id = NULL;
6732 const char *branch_name = NULL;
6733 char *new_id_str = NULL;
6734 char *refname = NULL;
6736 branch_name = got_ref_get_name(branch);
6737 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6738 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6741 err = got_object_id_str(&new_id_str, new_commit_id);
6745 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6746 new_id_str) == -1) {
6747 err = got_error_from_errno("asprintf");
6751 err = got_ref_resolve(&old_commit_id, repo, branch);
6755 err = got_ref_alloc(&ref, refname, old_commit_id);
6759 err = got_ref_write(ref, repo);
6763 free(old_commit_id);
6769 const struct got_error *
6770 got_worktree_rebase_complete(struct got_worktree *worktree,
6771 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6772 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6773 struct got_repository *repo, int create_backup)
6775 const struct got_error *err, *unlockerr, *sync_err;
6776 struct got_object_id *new_head_commit_id = NULL;
6777 char *fileindex_path = NULL;
6779 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6783 if (create_backup) {
6784 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6785 rebased_branch, new_head_commit_id, repo);
6790 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6794 err = got_ref_write(rebased_branch, repo);
6798 err = got_worktree_set_head_ref(worktree, rebased_branch);
6802 err = delete_rebase_refs(worktree, repo);
6806 err = get_fileindex_path(&fileindex_path, worktree);
6809 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6810 sync_err = sync_fileindex(fileindex, fileindex_path);
6811 if (sync_err && err == NULL)
6814 got_fileindex_free(fileindex);
6815 free(fileindex_path);
6816 free(new_head_commit_id);
6817 unlockerr = lock_worktree(worktree, LOCK_SH);
6818 if (unlockerr && err == NULL)
6823 const struct got_error *
6824 got_worktree_rebase_abort(struct got_worktree *worktree,
6825 struct got_fileindex *fileindex, struct got_repository *repo,
6826 struct got_reference *new_base_branch,
6827 got_worktree_checkout_cb progress_cb, void *progress_arg)
6829 const struct got_error *err, *unlockerr, *sync_err;
6830 struct got_reference *resolved = NULL;
6831 struct got_object_id *commit_id = NULL;
6832 struct got_commit_object *commit = NULL;
6833 char *fileindex_path = NULL;
6834 struct revert_file_args rfa;
6835 struct got_object_id *tree_id = NULL;
6837 err = lock_worktree(worktree, LOCK_EX);
6841 err = got_object_open_as_commit(&commit, repo,
6842 worktree->base_commit_id);
6846 err = got_ref_open(&resolved, repo,
6847 got_ref_get_symref_target(new_base_branch), 0);
6851 err = got_worktree_set_head_ref(worktree, resolved);
6856 * XXX commits to the base branch could have happened while
6857 * we were busy rebasing; should we store the original commit ID
6858 * when rebase begins and read it back here?
6860 err = got_ref_resolve(&commit_id, repo, resolved);
6864 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6868 err = got_object_id_by_path(&tree_id, repo, commit,
6869 worktree->path_prefix);
6873 err = delete_rebase_refs(worktree, repo);
6877 err = get_fileindex_path(&fileindex_path, worktree);
6881 rfa.worktree = worktree;
6882 rfa.fileindex = fileindex;
6883 rfa.progress_cb = progress_cb;
6884 rfa.progress_arg = progress_arg;
6885 rfa.patch_cb = NULL;
6886 rfa.patch_arg = NULL;
6888 rfa.unlink_added_files = 0;
6889 err = worktree_status(worktree, "", fileindex, repo,
6890 revert_file, &rfa, NULL, NULL, 1, 0);
6894 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6895 repo, progress_cb, progress_arg, NULL, NULL);
6897 sync_err = sync_fileindex(fileindex, fileindex_path);
6898 if (sync_err && err == NULL)
6901 got_ref_close(resolved);
6905 got_object_commit_close(commit);
6907 got_fileindex_free(fileindex);
6908 free(fileindex_path);
6910 unlockerr = lock_worktree(worktree, LOCK_SH);
6911 if (unlockerr && err == NULL)
6916 const struct got_error *
6917 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6918 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6919 struct got_fileindex **fileindex, struct got_worktree *worktree,
6920 struct got_repository *repo)
6922 const struct got_error *err = NULL;
6923 char *tmp_branch_name = NULL;
6924 char *branch_ref_name = NULL;
6925 char *base_commit_ref_name = NULL;
6926 char *fileindex_path = NULL;
6927 struct check_rebase_ok_arg ok_arg;
6928 struct got_reference *wt_branch = NULL;
6929 struct got_reference *base_commit_ref = NULL;
6933 *base_commit_id = NULL;
6936 err = lock_worktree(worktree, LOCK_EX);
6940 err = open_fileindex(fileindex, &fileindex_path, worktree);
6944 ok_arg.worktree = worktree;
6946 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6951 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6955 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6959 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6964 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6969 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6973 err = got_ref_write(*branch_ref, repo);
6977 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6978 worktree->base_commit_id);
6981 err = got_ref_write(base_commit_ref, repo);
6984 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6985 if (*base_commit_id == NULL) {
6986 err = got_error_from_errno("got_object_id_dup");
6990 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6991 worktree->base_commit_id);
6994 err = got_ref_write(*tmp_branch, repo);
6998 err = got_worktree_set_head_ref(worktree, *tmp_branch);
7002 free(fileindex_path);
7003 free(tmp_branch_name);
7004 free(branch_ref_name);
7005 free(base_commit_ref_name);
7007 got_ref_close(wt_branch);
7010 got_ref_close(*branch_ref);
7014 got_ref_close(*tmp_branch);
7017 free(*base_commit_id);
7019 got_fileindex_free(*fileindex);
7022 lock_worktree(worktree, LOCK_SH);
7027 const struct got_error *
7028 got_worktree_histedit_postpone(struct got_worktree *worktree,
7029 struct got_fileindex *fileindex)
7032 got_fileindex_free(fileindex);
7033 return lock_worktree(worktree, LOCK_SH);
7036 const struct got_error *
7037 got_worktree_histedit_in_progress(int *in_progress,
7038 struct got_worktree *worktree)
7040 const struct got_error *err;
7041 char *tmp_branch_name = NULL;
7043 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7047 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7048 free(tmp_branch_name);
7052 const struct got_error *
7053 got_worktree_histedit_continue(struct got_object_id **commit_id,
7054 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7055 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7056 struct got_worktree *worktree, struct got_repository *repo)
7058 const struct got_error *err;
7059 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7060 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7061 struct got_reference *commit_ref = NULL;
7062 struct got_reference *base_commit_ref = NULL;
7063 char *fileindex_path = NULL;
7064 int have_staged_files = 0;
7068 *base_commit_id = NULL;
7071 err = lock_worktree(worktree, LOCK_EX);
7075 err = open_fileindex(fileindex, &fileindex_path, worktree);
7079 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7080 &have_staged_files);
7081 if (err && err->code != GOT_ERR_CANCELLED)
7083 if (have_staged_files) {
7084 err = got_error(GOT_ERR_STAGED_PATHS);
7088 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7092 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7096 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7100 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7105 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7109 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7112 err = got_ref_resolve(commit_id, repo, commit_ref);
7116 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7119 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7123 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7127 free(commit_ref_name);
7128 free(branch_ref_name);
7129 free(fileindex_path);
7131 got_ref_close(commit_ref);
7132 if (base_commit_ref)
7133 got_ref_close(base_commit_ref);
7137 free(*base_commit_id);
7138 *base_commit_id = NULL;
7140 got_ref_close(*tmp_branch);
7144 got_fileindex_free(*fileindex);
7147 lock_worktree(worktree, LOCK_EX);
7152 static const struct got_error *
7153 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7155 const struct got_error *err;
7156 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7157 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7159 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7162 err = delete_ref(tmp_branch_name, repo);
7166 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7170 err = delete_ref(base_commit_ref_name, repo);
7174 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7177 err = delete_ref(branch_ref_name, repo);
7181 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7184 err = delete_ref(commit_ref_name, repo);
7188 free(tmp_branch_name);
7189 free(base_commit_ref_name);
7190 free(branch_ref_name);
7191 free(commit_ref_name);
7195 const struct got_error *
7196 got_worktree_histedit_abort(struct got_worktree *worktree,
7197 struct got_fileindex *fileindex, struct got_repository *repo,
7198 struct got_reference *branch, struct got_object_id *base_commit_id,
7199 got_worktree_checkout_cb progress_cb, void *progress_arg)
7201 const struct got_error *err, *unlockerr, *sync_err;
7202 struct got_reference *resolved = NULL;
7203 char *fileindex_path = NULL;
7204 struct got_commit_object *commit = NULL;
7205 struct got_object_id *tree_id = NULL;
7206 struct revert_file_args rfa;
7208 err = lock_worktree(worktree, LOCK_EX);
7212 err = got_object_open_as_commit(&commit, repo,
7213 worktree->base_commit_id);
7217 err = got_ref_open(&resolved, repo,
7218 got_ref_get_symref_target(branch), 0);
7222 err = got_worktree_set_head_ref(worktree, resolved);
7226 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7230 err = got_object_id_by_path(&tree_id, repo, commit,
7231 worktree->path_prefix);
7235 err = delete_histedit_refs(worktree, repo);
7239 err = get_fileindex_path(&fileindex_path, worktree);
7243 rfa.worktree = worktree;
7244 rfa.fileindex = fileindex;
7245 rfa.progress_cb = progress_cb;
7246 rfa.progress_arg = progress_arg;
7247 rfa.patch_cb = NULL;
7248 rfa.patch_arg = NULL;
7250 rfa.unlink_added_files = 0;
7251 err = worktree_status(worktree, "", fileindex, repo,
7252 revert_file, &rfa, NULL, NULL, 1, 0);
7256 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7257 repo, progress_cb, progress_arg, NULL, NULL);
7259 sync_err = sync_fileindex(fileindex, fileindex_path);
7260 if (sync_err && err == NULL)
7263 got_ref_close(resolved);
7265 free(fileindex_path);
7267 unlockerr = lock_worktree(worktree, LOCK_SH);
7268 if (unlockerr && err == NULL)
7273 const struct got_error *
7274 got_worktree_histedit_complete(struct got_worktree *worktree,
7275 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7276 struct got_reference *edited_branch, struct got_repository *repo)
7278 const struct got_error *err, *unlockerr, *sync_err;
7279 struct got_object_id *new_head_commit_id = NULL;
7280 struct got_reference *resolved = NULL;
7281 char *fileindex_path = NULL;
7283 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7287 err = got_ref_open(&resolved, repo,
7288 got_ref_get_symref_target(edited_branch), 0);
7292 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7293 resolved, new_head_commit_id, repo);
7297 err = got_ref_change_ref(resolved, new_head_commit_id);
7301 err = got_ref_write(resolved, repo);
7305 err = got_worktree_set_head_ref(worktree, resolved);
7309 err = delete_histedit_refs(worktree, repo);
7313 err = get_fileindex_path(&fileindex_path, worktree);
7316 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7317 sync_err = sync_fileindex(fileindex, fileindex_path);
7318 if (sync_err && err == NULL)
7321 got_fileindex_free(fileindex);
7322 free(fileindex_path);
7323 free(new_head_commit_id);
7324 unlockerr = lock_worktree(worktree, LOCK_SH);
7325 if (unlockerr && err == NULL)
7330 const struct got_error *
7331 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7332 struct got_object_id *commit_id, struct got_repository *repo)
7334 const struct got_error *err;
7335 char *commit_ref_name;
7337 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7341 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7345 err = delete_ref(commit_ref_name, repo);
7347 free(commit_ref_name);
7351 const struct got_error *
7352 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7353 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7354 struct got_worktree *worktree, const char *refname,
7355 struct got_repository *repo)
7357 const struct got_error *err = NULL;
7358 char *fileindex_path = NULL;
7359 struct check_rebase_ok_arg ok_arg;
7363 *base_branch_ref = NULL;
7365 err = lock_worktree(worktree, LOCK_EX);
7369 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7370 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7371 "cannot integrate a branch into itself; "
7372 "update -b or different branch name required");
7376 err = open_fileindex(fileindex, &fileindex_path, worktree);
7380 /* Preconditions are the same as for rebase. */
7381 ok_arg.worktree = worktree;
7383 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7388 err = got_ref_open(branch_ref, repo, refname, 1);
7392 err = got_ref_open(base_branch_ref, repo,
7393 got_worktree_get_head_ref_name(worktree), 1);
7397 got_ref_close(*branch_ref);
7400 if (*base_branch_ref) {
7401 got_ref_close(*base_branch_ref);
7402 *base_branch_ref = NULL;
7405 got_fileindex_free(*fileindex);
7408 lock_worktree(worktree, LOCK_SH);
7413 const struct got_error *
7414 got_worktree_integrate_continue(struct got_worktree *worktree,
7415 struct got_fileindex *fileindex, struct got_repository *repo,
7416 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7417 got_worktree_checkout_cb progress_cb, void *progress_arg,
7418 got_cancel_cb cancel_cb, void *cancel_arg)
7420 const struct got_error *err = NULL, *sync_err, *unlockerr;
7421 char *fileindex_path = NULL;
7422 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7423 struct got_commit_object *commit = NULL;
7425 err = get_fileindex_path(&fileindex_path, worktree);
7429 err = got_ref_resolve(&commit_id, repo, branch_ref);
7433 err = got_object_open_as_commit(&commit, repo, commit_id);
7437 err = got_object_id_by_path(&tree_id, repo, commit,
7438 worktree->path_prefix);
7442 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7446 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7447 progress_cb, progress_arg, cancel_cb, cancel_arg);
7451 err = got_ref_change_ref(base_branch_ref, commit_id);
7455 err = got_ref_write(base_branch_ref, repo);
7459 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7461 sync_err = sync_fileindex(fileindex, fileindex_path);
7462 if (sync_err && err == NULL)
7466 unlockerr = got_ref_unlock(branch_ref);
7467 if (unlockerr && err == NULL)
7469 got_ref_close(branch_ref);
7471 unlockerr = got_ref_unlock(base_branch_ref);
7472 if (unlockerr && err == NULL)
7474 got_ref_close(base_branch_ref);
7476 got_fileindex_free(fileindex);
7477 free(fileindex_path);
7480 got_object_commit_close(commit);
7482 unlockerr = lock_worktree(worktree, LOCK_SH);
7483 if (unlockerr && err == NULL)
7488 const struct got_error *
7489 got_worktree_integrate_abort(struct got_worktree *worktree,
7490 struct got_fileindex *fileindex, struct got_repository *repo,
7491 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7493 const struct got_error *err = NULL, *unlockerr = NULL;
7495 got_fileindex_free(fileindex);
7497 err = lock_worktree(worktree, LOCK_SH);
7499 unlockerr = got_ref_unlock(branch_ref);
7500 if (unlockerr && err == NULL)
7502 got_ref_close(branch_ref);
7504 unlockerr = got_ref_unlock(base_branch_ref);
7505 if (unlockerr && err == NULL)
7507 got_ref_close(base_branch_ref);
7512 const struct got_error *
7513 got_worktree_merge_postpone(struct got_worktree *worktree,
7514 struct got_fileindex *fileindex)
7516 const struct got_error *err, *sync_err;
7517 char *fileindex_path = NULL;
7519 err = get_fileindex_path(&fileindex_path, worktree);
7523 sync_err = sync_fileindex(fileindex, fileindex_path);
7525 err = lock_worktree(worktree, LOCK_SH);
7526 if (sync_err && err == NULL)
7529 got_fileindex_free(fileindex);
7530 free(fileindex_path);
7534 static const struct got_error *
7535 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7537 const struct got_error *err;
7538 char *branch_refname = NULL, *commit_refname = NULL;
7540 err = get_merge_branch_ref_name(&branch_refname, worktree);
7543 err = delete_ref(branch_refname, repo);
7547 err = get_merge_commit_ref_name(&commit_refname, worktree);
7550 err = delete_ref(commit_refname, repo);
7555 free(branch_refname);
7556 free(commit_refname);
7560 struct merge_commit_msg_arg {
7561 struct got_worktree *worktree;
7562 const char *branch_name;
7565 static const struct got_error *
7566 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths, char **logmsg,
7569 struct merge_commit_msg_arg *a = arg;
7571 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7572 got_worktree_get_head_ref_name(a->worktree)) == -1)
7573 return got_error_from_errno("asprintf");
7579 const struct got_error *
7580 got_worktree_merge_branch(struct got_worktree *worktree,
7581 struct got_fileindex *fileindex,
7582 struct got_object_id *yca_commit_id,
7583 struct got_object_id *branch_tip,
7584 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7585 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7587 const struct got_error *err;
7588 char *fileindex_path = NULL;
7590 err = get_fileindex_path(&fileindex_path, worktree);
7594 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7599 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7600 branch_tip, repo, progress_cb, progress_arg,
7601 cancel_cb, cancel_arg);
7603 free(fileindex_path);
7607 const struct got_error *
7608 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7609 struct got_worktree *worktree, struct got_fileindex *fileindex,
7610 const char *author, const char *committer, int allow_bad_symlinks,
7611 struct got_object_id *branch_tip, const char *branch_name,
7612 struct got_repository *repo,
7613 got_worktree_status_cb status_cb, void *status_arg)
7616 const struct got_error *err = NULL, *sync_err;
7617 struct got_pathlist_head commitable_paths;
7618 struct collect_commitables_arg cc_arg;
7619 struct got_pathlist_entry *pe;
7620 struct got_reference *head_ref = NULL;
7621 struct got_object_id *head_commit_id = NULL;
7622 int have_staged_files = 0;
7623 struct merge_commit_msg_arg mcm_arg;
7624 char *fileindex_path = NULL;
7626 *new_commit_id = NULL;
7628 TAILQ_INIT(&commitable_paths);
7630 err = get_fileindex_path(&fileindex_path, worktree);
7634 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7638 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7642 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7643 &have_staged_files);
7644 if (err && err->code != GOT_ERR_CANCELLED)
7646 if (have_staged_files) {
7647 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7651 cc_arg.commitable_paths = &commitable_paths;
7652 cc_arg.worktree = worktree;
7653 cc_arg.fileindex = fileindex;
7655 cc_arg.have_staged_files = have_staged_files;
7656 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7657 err = worktree_status(worktree, "", fileindex, repo,
7658 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7662 if (TAILQ_EMPTY(&commitable_paths)) {
7663 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7664 "merge of %s cannot proceed", branch_name);
7668 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7669 struct got_commitable *ct = pe->data;
7670 const char *ct_path = ct->in_repo_path;
7672 while (ct_path[0] == '/')
7674 err = check_out_of_date(ct_path, ct->status,
7675 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7676 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7682 mcm_arg.worktree = worktree;
7683 mcm_arg.branch_name = branch_name;
7684 err = commit_worktree(new_commit_id, &commitable_paths,
7685 head_commit_id, branch_tip, worktree, author, committer,
7686 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7690 err = update_fileindex_after_commit(worktree, &commitable_paths,
7691 *new_commit_id, fileindex, have_staged_files);
7692 sync_err = sync_fileindex(fileindex, fileindex_path);
7693 if (sync_err && err == NULL)
7696 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7697 struct got_commitable *ct = pe->data;
7698 free_commitable(ct);
7700 got_pathlist_free(&commitable_paths);
7701 free(fileindex_path);
7705 const struct got_error *
7706 got_worktree_merge_complete(struct got_worktree *worktree,
7707 struct got_fileindex *fileindex, struct got_repository *repo)
7709 const struct got_error *err, *unlockerr, *sync_err;
7710 char *fileindex_path = NULL;
7712 err = delete_merge_refs(worktree, repo);
7716 err = get_fileindex_path(&fileindex_path, worktree);
7719 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7720 sync_err = sync_fileindex(fileindex, fileindex_path);
7721 if (sync_err && err == NULL)
7724 got_fileindex_free(fileindex);
7725 free(fileindex_path);
7726 unlockerr = lock_worktree(worktree, LOCK_SH);
7727 if (unlockerr && err == NULL)
7732 const struct got_error *
7733 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7734 struct got_repository *repo)
7736 const struct got_error *err;
7737 char *branch_refname = NULL;
7738 struct got_reference *branch_ref = NULL;
7742 err = get_merge_branch_ref_name(&branch_refname, worktree);
7745 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7746 free(branch_refname);
7748 if (err->code != GOT_ERR_NOT_REF)
7756 const struct got_error *got_worktree_merge_prepare(
7757 struct got_fileindex **fileindex, struct got_worktree *worktree,
7758 struct got_reference *branch, struct got_repository *repo)
7760 const struct got_error *err = NULL;
7761 char *fileindex_path = NULL;
7762 char *branch_refname = NULL, *commit_refname = NULL;
7763 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
7764 struct got_reference *commit_ref = NULL;
7765 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
7766 struct check_rebase_ok_arg ok_arg;
7770 err = lock_worktree(worktree, LOCK_EX);
7774 err = open_fileindex(fileindex, &fileindex_path, worktree);
7778 /* Preconditions are the same as for rebase. */
7779 ok_arg.worktree = worktree;
7781 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7786 err = get_merge_branch_ref_name(&branch_refname, worktree);
7790 err = get_merge_commit_ref_name(&commit_refname, worktree);
7794 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7799 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
7803 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
7804 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
7808 err = got_ref_resolve(&branch_tip, repo, branch);
7812 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
7815 err = got_ref_write(branch_ref, repo);
7819 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
7822 err = got_ref_write(commit_ref, repo);
7827 free(branch_refname);
7828 free(commit_refname);
7829 free(fileindex_path);
7831 got_ref_close(branch_ref);
7833 got_ref_close(commit_ref);
7835 got_ref_close(wt_branch);
7836 free(wt_branch_tip);
7839 got_fileindex_free(*fileindex);
7842 lock_worktree(worktree, LOCK_SH);
7847 const struct got_error *
7848 got_worktree_merge_continue(char **branch_name,
7849 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
7850 struct got_worktree *worktree, struct got_repository *repo)
7852 const struct got_error *err;
7853 char *commit_refname = NULL, *branch_refname = NULL;
7854 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
7855 char *fileindex_path = NULL;
7856 int have_staged_files = 0;
7858 *branch_name = NULL;
7862 err = lock_worktree(worktree, LOCK_EX);
7866 err = open_fileindex(fileindex, &fileindex_path, worktree);
7870 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7871 &have_staged_files);
7872 if (err && err->code != GOT_ERR_CANCELLED)
7874 if (have_staged_files) {
7875 err = got_error(GOT_ERR_STAGED_PATHS);
7879 err = get_merge_branch_ref_name(&branch_refname, worktree);
7883 err = get_merge_commit_ref_name(&commit_refname, worktree);
7887 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7891 if (!got_ref_is_symbolic(branch_ref)) {
7892 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7893 "%s is not a symbolic reference",
7894 got_ref_get_name(branch_ref));
7897 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
7898 if (*branch_name == NULL) {
7899 err = got_error_from_errno("strdup");
7903 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
7907 err = got_ref_resolve(branch_tip, repo, commit_ref);
7911 free(commit_refname);
7912 free(branch_refname);
7913 free(fileindex_path);
7915 got_ref_close(commit_ref);
7917 got_ref_close(branch_ref);
7921 *branch_name = NULL;
7926 got_fileindex_free(*fileindex);
7929 lock_worktree(worktree, LOCK_SH);
7934 const struct got_error *
7935 got_worktree_merge_abort(struct got_worktree *worktree,
7936 struct got_fileindex *fileindex, struct got_repository *repo,
7937 got_worktree_checkout_cb progress_cb, void *progress_arg)
7939 const struct got_error *err, *unlockerr, *sync_err;
7940 struct got_object_id *commit_id = NULL;
7941 struct got_commit_object *commit = NULL;
7942 char *fileindex_path = NULL;
7943 struct revert_file_args rfa;
7944 struct got_object_id *tree_id = NULL;
7946 err = got_object_open_as_commit(&commit, repo,
7947 worktree->base_commit_id);
7951 err = got_object_id_by_path(&tree_id, repo, commit,
7952 worktree->path_prefix);
7956 err = delete_merge_refs(worktree, repo);
7960 err = get_fileindex_path(&fileindex_path, worktree);
7964 rfa.worktree = worktree;
7965 rfa.fileindex = fileindex;
7966 rfa.progress_cb = progress_cb;
7967 rfa.progress_arg = progress_arg;
7968 rfa.patch_cb = NULL;
7969 rfa.patch_arg = NULL;
7971 rfa.unlink_added_files = 1;
7972 err = worktree_status(worktree, "", fileindex, repo,
7973 revert_file, &rfa, NULL, NULL, 1, 0);
7977 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7978 repo, progress_cb, progress_arg, NULL, NULL);
7980 sync_err = sync_fileindex(fileindex, fileindex_path);
7981 if (sync_err && err == NULL)
7987 got_object_commit_close(commit);
7989 got_fileindex_free(fileindex);
7990 free(fileindex_path);
7992 unlockerr = lock_worktree(worktree, LOCK_SH);
7993 if (unlockerr && err == NULL)
7998 struct check_stage_ok_arg {
7999 struct got_object_id *head_commit_id;
8000 struct got_worktree *worktree;
8001 struct got_fileindex *fileindex;
8002 struct got_repository *repo;
8006 static const struct got_error *
8007 check_stage_ok(void *arg, unsigned char status,
8008 unsigned char staged_status, const char *relpath,
8009 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8010 struct got_object_id *commit_id, int dirfd, const char *de_name)
8012 struct check_stage_ok_arg *a = arg;
8013 const struct got_error *err = NULL;
8014 struct got_fileindex_entry *ie;
8015 struct got_object_id base_commit_id;
8016 struct got_object_id *base_commit_idp = NULL;
8017 char *in_repo_path = NULL, *p;
8019 if (status == GOT_STATUS_UNVERSIONED ||
8020 status == GOT_STATUS_NO_CHANGE)
8022 if (status == GOT_STATUS_NONEXISTENT)
8023 return got_error_set_errno(ENOENT, relpath);
8025 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8027 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8029 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8030 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8032 return got_error_from_errno("asprintf");
8034 if (got_fileindex_entry_has_commit(ie)) {
8035 memcpy(base_commit_id.sha1, ie->commit_sha1,
8036 SHA1_DIGEST_LENGTH);
8037 base_commit_idp = &base_commit_id;
8040 if (status == GOT_STATUS_CONFLICT) {
8041 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8043 } else if (status != GOT_STATUS_ADD &&
8044 status != GOT_STATUS_MODIFY &&
8045 status != GOT_STATUS_DELETE) {
8046 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8050 a->have_changes = 1;
8055 err = check_out_of_date(p, status, staged_status,
8056 blob_id, base_commit_idp, a->head_commit_id, a->repo,
8057 GOT_ERR_STAGE_OUT_OF_DATE);
8063 struct stage_path_arg {
8064 struct got_worktree *worktree;
8065 struct got_fileindex *fileindex;
8066 struct got_repository *repo;
8067 got_worktree_status_cb status_cb;
8069 got_worktree_patch_cb patch_cb;
8071 int staged_something;
8072 int allow_bad_symlinks;
8075 static const struct got_error *
8076 stage_path(void *arg, unsigned char status,
8077 unsigned char staged_status, const char *relpath,
8078 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8079 struct got_object_id *commit_id, int dirfd, const char *de_name)
8081 struct stage_path_arg *a = arg;
8082 const struct got_error *err = NULL;
8083 struct got_fileindex_entry *ie;
8084 char *ondisk_path = NULL, *path_content = NULL;
8086 struct got_object_id *new_staged_blob_id = NULL;
8089 if (status == GOT_STATUS_UNVERSIONED)
8092 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8094 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8096 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8098 return got_error_from_errno("asprintf");
8101 case GOT_STATUS_ADD:
8102 case GOT_STATUS_MODIFY:
8103 /* XXX could sb.st_mode be passed in by our caller? */
8104 if (lstat(ondisk_path, &sb) == -1) {
8105 err = got_error_from_errno2("lstat", ondisk_path);
8109 if (status == GOT_STATUS_ADD) {
8110 int choice = GOT_PATCH_CHOICE_NONE;
8111 err = (*a->patch_cb)(&choice, a->patch_arg,
8112 status, ie->path, NULL, 1, 1);
8115 if (choice != GOT_PATCH_CHOICE_YES)
8118 err = create_patched_content(&path_content, 0,
8119 staged_blob_id ? staged_blob_id : blob_id,
8120 ondisk_path, dirfd, de_name, ie->path,
8121 a->repo, a->patch_cb, a->patch_arg);
8122 if (err || path_content == NULL)
8126 err = got_object_blob_create(&new_staged_blob_id,
8127 path_content ? path_content : ondisk_path, a->repo);
8130 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8131 SHA1_DIGEST_LENGTH);
8132 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8133 stage = GOT_FILEIDX_STAGE_ADD;
8135 stage = GOT_FILEIDX_STAGE_MODIFY;
8136 got_fileindex_entry_stage_set(ie, stage);
8137 if (S_ISLNK(sb.st_mode)) {
8138 int is_bad_symlink = 0;
8139 if (!a->allow_bad_symlinks) {
8140 char target_path[PATH_MAX];
8142 target_len = readlink(ondisk_path, target_path,
8143 sizeof(target_path));
8144 if (target_len == -1) {
8145 err = got_error_from_errno2("readlink",
8149 err = is_bad_symlink_target(&is_bad_symlink,
8150 target_path, target_len, ondisk_path,
8151 a->worktree->root_path);
8154 if (is_bad_symlink) {
8155 err = got_error_path(ondisk_path,
8156 GOT_ERR_BAD_SYMLINK);
8161 got_fileindex_entry_staged_filetype_set(ie,
8162 GOT_FILEIDX_MODE_BAD_SYMLINK);
8164 got_fileindex_entry_staged_filetype_set(ie,
8165 GOT_FILEIDX_MODE_SYMLINK);
8167 got_fileindex_entry_staged_filetype_set(ie,
8168 GOT_FILEIDX_MODE_REGULAR_FILE);
8170 a->staged_something = 1;
8171 if (a->status_cb == NULL)
8173 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8174 get_staged_status(ie), relpath, blob_id,
8175 new_staged_blob_id, NULL, dirfd, de_name);
8179 * When staging the reverse of the staged diff,
8180 * implicitly unstage the file.
8182 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8183 sizeof(ie->blob_sha1)) == 0) {
8184 got_fileindex_entry_stage_set(ie,
8185 GOT_FILEIDX_STAGE_NONE);
8188 case GOT_STATUS_DELETE:
8189 if (staged_status == GOT_STATUS_DELETE)
8192 int choice = GOT_PATCH_CHOICE_NONE;
8193 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8194 ie->path, NULL, 1, 1);
8197 if (choice == GOT_PATCH_CHOICE_NO)
8199 if (choice != GOT_PATCH_CHOICE_YES) {
8200 err = got_error(GOT_ERR_PATCH_CHOICE);
8204 stage = GOT_FILEIDX_STAGE_DELETE;
8205 got_fileindex_entry_stage_set(ie, stage);
8206 a->staged_something = 1;
8207 if (a->status_cb == NULL)
8209 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8210 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8213 case GOT_STATUS_NO_CHANGE:
8215 case GOT_STATUS_CONFLICT:
8216 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8218 case GOT_STATUS_NONEXISTENT:
8219 err = got_error_set_errno(ENOENT, relpath);
8222 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8226 if (path_content && unlink(path_content) == -1 && err == NULL)
8227 err = got_error_from_errno2("unlink", path_content);
8230 free(new_staged_blob_id);
8234 const struct got_error *
8235 got_worktree_stage(struct got_worktree *worktree,
8236 struct got_pathlist_head *paths,
8237 got_worktree_status_cb status_cb, void *status_arg,
8238 got_worktree_patch_cb patch_cb, void *patch_arg,
8239 int allow_bad_symlinks, struct got_repository *repo)
8241 const struct got_error *err = NULL, *sync_err, *unlockerr;
8242 struct got_pathlist_entry *pe;
8243 struct got_fileindex *fileindex = NULL;
8244 char *fileindex_path = NULL;
8245 struct got_reference *head_ref = NULL;
8246 struct got_object_id *head_commit_id = NULL;
8247 struct check_stage_ok_arg oka;
8248 struct stage_path_arg spa;
8250 err = lock_worktree(worktree, LOCK_EX);
8254 err = got_ref_open(&head_ref, repo,
8255 got_worktree_get_head_ref_name(worktree), 0);
8258 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8261 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8265 /* Check pre-conditions before staging anything. */
8266 oka.head_commit_id = head_commit_id;
8267 oka.worktree = worktree;
8268 oka.fileindex = fileindex;
8270 oka.have_changes = 0;
8271 TAILQ_FOREACH(pe, paths, entry) {
8272 err = worktree_status(worktree, pe->path, fileindex, repo,
8273 check_stage_ok, &oka, NULL, NULL, 1, 0);
8277 if (!oka.have_changes) {
8278 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8282 spa.worktree = worktree;
8283 spa.fileindex = fileindex;
8285 spa.patch_cb = patch_cb;
8286 spa.patch_arg = patch_arg;
8287 spa.status_cb = status_cb;
8288 spa.status_arg = status_arg;
8289 spa.staged_something = 0;
8290 spa.allow_bad_symlinks = allow_bad_symlinks;
8291 TAILQ_FOREACH(pe, paths, entry) {
8292 err = worktree_status(worktree, pe->path, fileindex, repo,
8293 stage_path, &spa, NULL, NULL, 1, 0);
8297 if (!spa.staged_something) {
8298 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8302 sync_err = sync_fileindex(fileindex, fileindex_path);
8303 if (sync_err && err == NULL)
8307 got_ref_close(head_ref);
8308 free(head_commit_id);
8309 free(fileindex_path);
8311 got_fileindex_free(fileindex);
8312 unlockerr = lock_worktree(worktree, LOCK_SH);
8313 if (unlockerr && err == NULL)
8318 struct unstage_path_arg {
8319 struct got_worktree *worktree;
8320 struct got_fileindex *fileindex;
8321 struct got_repository *repo;
8322 got_worktree_checkout_cb progress_cb;
8324 got_worktree_patch_cb patch_cb;
8328 static const struct got_error *
8329 create_unstaged_content(char **path_unstaged_content,
8330 char **path_new_staged_content, struct got_object_id *blob_id,
8331 struct got_object_id *staged_blob_id, const char *relpath,
8332 struct got_repository *repo,
8333 got_worktree_patch_cb patch_cb, void *patch_arg)
8335 const struct got_error *err, *free_err;
8336 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8337 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8338 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8339 struct got_diffreg_result *diffreg_result = NULL;
8340 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8341 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8342 int fd1 = -1, fd2 = -1;
8344 *path_unstaged_content = NULL;
8345 *path_new_staged_content = NULL;
8347 err = got_object_id_str(&label1, blob_id);
8351 fd1 = got_opentempfd();
8353 err = got_error_from_errno("got_opentempfd");
8356 fd2 = got_opentempfd();
8358 err = got_error_from_errno("got_opentempfd");
8362 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8366 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
8370 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8374 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8379 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
8383 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8387 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8388 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8392 err = got_opentemp_named(path_unstaged_content, &outfile,
8393 "got-unstaged-content");
8396 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8397 "got-new-staged-content");
8401 if (fseek(f1, 0L, SEEK_SET) == -1) {
8402 err = got_ferror(f1, GOT_ERR_IO);
8405 if (fseek(f2, 0L, SEEK_SET) == -1) {
8406 err = got_ferror(f2, GOT_ERR_IO);
8409 /* Count the number of actual changes in the diff result. */
8410 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8411 struct diff_chunk_context cc = {};
8412 diff_chunk_context_load_change(&cc, &nchunks_used,
8413 diffreg_result->result, n, 0);
8416 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8418 err = apply_or_reject_change(&choice, &nchunks_used,
8419 diffreg_result->result, n, relpath, f1, f2,
8420 &line_cur1, &line_cur2,
8421 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8424 if (choice == GOT_PATCH_CHOICE_YES)
8427 have_rejected_content = 1;
8428 if (choice == GOT_PATCH_CHOICE_QUIT)
8431 if (have_content || have_rejected_content)
8432 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8433 outfile, rejectfile);
8436 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8437 err = got_error_from_errno("close");
8439 got_object_blob_close(blob);
8440 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8441 err = got_error_from_errno("close");
8443 got_object_blob_close(staged_blob);
8444 free_err = got_diffreg_result_free(diffreg_result);
8445 if (free_err && err == NULL)
8447 if (f1 && fclose(f1) == EOF && err == NULL)
8448 err = got_error_from_errno2("fclose", path1);
8449 if (f2 && fclose(f2) == EOF && err == NULL)
8450 err = got_error_from_errno2("fclose", path2);
8451 if (outfile && fclose(outfile) == EOF && err == NULL)
8452 err = got_error_from_errno2("fclose", *path_unstaged_content);
8453 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8454 err = got_error_from_errno2("fclose", *path_new_staged_content);
8455 if (path1 && unlink(path1) == -1 && err == NULL)
8456 err = got_error_from_errno2("unlink", path1);
8457 if (path2 && unlink(path2) == -1 && err == NULL)
8458 err = got_error_from_errno2("unlink", path2);
8459 if (err || !have_content) {
8460 if (*path_unstaged_content &&
8461 unlink(*path_unstaged_content) == -1 && err == NULL)
8462 err = got_error_from_errno2("unlink",
8463 *path_unstaged_content);
8464 free(*path_unstaged_content);
8465 *path_unstaged_content = NULL;
8467 if (err || !have_content || !have_rejected_content) {
8468 if (*path_new_staged_content &&
8469 unlink(*path_new_staged_content) == -1 && err == NULL)
8470 err = got_error_from_errno2("unlink",
8471 *path_new_staged_content);
8472 free(*path_new_staged_content);
8473 *path_new_staged_content = NULL;
8480 static const struct got_error *
8481 unstage_hunks(struct got_object_id *staged_blob_id,
8482 struct got_blob_object *blob_base,
8483 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8484 const char *ondisk_path, const char *label_orig,
8485 struct got_worktree *worktree, struct got_repository *repo,
8486 got_worktree_patch_cb patch_cb, void *patch_arg,
8487 got_worktree_checkout_cb progress_cb, void *progress_arg)
8489 const struct got_error *err = NULL;
8490 char *path_unstaged_content = NULL;
8491 char *path_new_staged_content = NULL;
8492 char *parent = NULL, *base_path = NULL;
8493 char *blob_base_path = NULL;
8494 struct got_object_id *new_staged_blob_id = NULL;
8495 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8498 err = create_unstaged_content(&path_unstaged_content,
8499 &path_new_staged_content, blob_id, staged_blob_id,
8500 ie->path, repo, patch_cb, patch_arg);
8504 if (path_unstaged_content == NULL)
8507 if (path_new_staged_content) {
8508 err = got_object_blob_create(&new_staged_blob_id,
8509 path_new_staged_content, repo);
8514 f = fopen(path_unstaged_content, "re");
8516 err = got_error_from_errno2("fopen",
8517 path_unstaged_content);
8520 if (fstat(fileno(f), &sb) == -1) {
8521 err = got_error_from_errno2("fstat", path_unstaged_content);
8524 if (got_fileindex_entry_staged_filetype_get(ie) ==
8525 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8526 char link_target[PATH_MAX];
8528 r = fread(link_target, 1, sizeof(link_target), f);
8529 if (r == 0 && ferror(f)) {
8530 err = got_error_from_errno("fread");
8533 if (r >= sizeof(link_target)) { /* should not happen */
8534 err = got_error(GOT_ERR_NO_SPACE);
8537 link_target[r] = '\0';
8538 err = merge_symlink(worktree, blob_base,
8539 ondisk_path, ie->path, label_orig, link_target,
8540 worktree->base_commit_id, repo, progress_cb,
8543 int local_changes_subsumed;
8545 err = got_path_dirname(&parent, ondisk_path);
8549 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8551 err = got_error_from_errno("asprintf");
8556 err = got_opentemp_named(&blob_base_path, &f_base, base_path);
8559 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8565 * In order the run a 3-way merge with a symlink we copy the symlink's
8566 * target path into a temporary file and use that file with diff3.
8568 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8569 err = dump_symlink_target_path_to_file(&f_deriv2,
8575 fd = open(ondisk_path,
8576 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8578 err = got_error_from_errno2("open", ondisk_path);
8581 f_deriv2 = fdopen(fd, "r");
8582 if (f_deriv2 == NULL) {
8583 err = got_error_from_errno2("fdopen", ondisk_path);
8589 err = merge_file(&local_changes_subsumed, worktree,
8590 f_base, f, f_deriv2, ondisk_path, ie->path,
8591 got_fileindex_perms_to_st(ie),
8592 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8593 repo, progress_cb, progress_arg);
8598 if (new_staged_blob_id) {
8599 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8600 SHA1_DIGEST_LENGTH);
8602 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8603 got_fileindex_entry_staged_filetype_set(ie, 0);
8606 free(new_staged_blob_id);
8607 if (path_unstaged_content &&
8608 unlink(path_unstaged_content) == -1 && err == NULL)
8609 err = got_error_from_errno2("unlink", path_unstaged_content);
8610 if (path_new_staged_content &&
8611 unlink(path_new_staged_content) == -1 && err == NULL)
8612 err = got_error_from_errno2("unlink", path_new_staged_content);
8613 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8614 err = got_error_from_errno2("unlink", blob_base_path);
8615 if (f_base && fclose(f_base) == EOF && err == NULL)
8616 err = got_error_from_errno2("fclose", path_unstaged_content);
8617 if (f && fclose(f) == EOF && err == NULL)
8618 err = got_error_from_errno2("fclose", path_unstaged_content);
8619 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8620 err = got_error_from_errno2("fclose", ondisk_path);
8621 free(path_unstaged_content);
8622 free(path_new_staged_content);
8623 free(blob_base_path);
8629 static const struct got_error *
8630 unstage_path(void *arg, unsigned char status,
8631 unsigned char staged_status, const char *relpath,
8632 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8633 struct got_object_id *commit_id, int dirfd, const char *de_name)
8635 const struct got_error *err = NULL;
8636 struct unstage_path_arg *a = arg;
8637 struct got_fileindex_entry *ie;
8638 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8639 char *ondisk_path = NULL;
8640 char *id_str = NULL, *label_orig = NULL;
8641 int local_changes_subsumed;
8643 int fd1 = -1, fd2 = -1;
8645 if (staged_status != GOT_STATUS_ADD &&
8646 staged_status != GOT_STATUS_MODIFY &&
8647 staged_status != GOT_STATUS_DELETE)
8650 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8652 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8654 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8656 return got_error_from_errno("asprintf");
8658 err = got_object_id_str(&id_str,
8659 commit_id ? commit_id : a->worktree->base_commit_id);
8662 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8664 err = got_error_from_errno("asprintf");
8668 fd1 = got_opentempfd();
8670 err = got_error_from_errno("got_opentempfd");
8673 fd2 = got_opentempfd();
8675 err = got_error_from_errno("got_opentempfd");
8679 switch (staged_status) {
8680 case GOT_STATUS_MODIFY:
8681 err = got_object_open_as_blob(&blob_base, a->repo,
8682 blob_id, 8192, fd1);
8686 case GOT_STATUS_ADD:
8688 if (staged_status == GOT_STATUS_ADD) {
8689 int choice = GOT_PATCH_CHOICE_NONE;
8690 err = (*a->patch_cb)(&choice, a->patch_arg,
8691 staged_status, ie->path, NULL, 1, 1);
8694 if (choice != GOT_PATCH_CHOICE_YES)
8697 err = unstage_hunks(staged_blob_id,
8698 blob_base, blob_id, ie, ondisk_path,
8699 label_orig, a->worktree, a->repo,
8700 a->patch_cb, a->patch_arg,
8701 a->progress_cb, a->progress_arg);
8702 break; /* Done with this file. */
8705 err = got_object_open_as_blob(&blob_staged, a->repo,
8706 staged_blob_id, 8192, fd2);
8709 switch (got_fileindex_entry_staged_filetype_get(ie)) {
8710 case GOT_FILEIDX_MODE_BAD_SYMLINK:
8711 case GOT_FILEIDX_MODE_REGULAR_FILE:
8712 err = merge_blob(&local_changes_subsumed, a->worktree,
8713 blob_base, ondisk_path, relpath,
8714 got_fileindex_perms_to_st(ie), label_orig,
8715 blob_staged, commit_id ? commit_id :
8716 a->worktree->base_commit_id, a->repo,
8717 a->progress_cb, a->progress_arg);
8719 case GOT_FILEIDX_MODE_SYMLINK:
8720 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8721 char *staged_target;
8722 err = got_object_blob_read_to_str(
8723 &staged_target, blob_staged);
8726 err = merge_symlink(a->worktree, blob_base,
8727 ondisk_path, relpath, label_orig,
8728 staged_target, commit_id ? commit_id :
8729 a->worktree->base_commit_id,
8730 a->repo, a->progress_cb, a->progress_arg);
8731 free(staged_target);
8733 err = merge_blob(&local_changes_subsumed,
8734 a->worktree, blob_base, ondisk_path,
8735 relpath, got_fileindex_perms_to_st(ie),
8736 label_orig, blob_staged,
8737 commit_id ? commit_id :
8738 a->worktree->base_commit_id, a->repo,
8739 a->progress_cb, a->progress_arg);
8743 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8747 got_fileindex_entry_stage_set(ie,
8748 GOT_FILEIDX_STAGE_NONE);
8749 got_fileindex_entry_staged_filetype_set(ie, 0);
8752 case GOT_STATUS_DELETE:
8754 int choice = GOT_PATCH_CHOICE_NONE;
8755 err = (*a->patch_cb)(&choice, a->patch_arg,
8756 staged_status, ie->path, NULL, 1, 1);
8759 if (choice == GOT_PATCH_CHOICE_NO)
8761 if (choice != GOT_PATCH_CHOICE_YES) {
8762 err = got_error(GOT_ERR_PATCH_CHOICE);
8766 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8767 got_fileindex_entry_staged_filetype_set(ie, 0);
8768 err = get_file_status(&status, &sb, ie, ondisk_path,
8769 dirfd, de_name, a->repo);
8772 err = (*a->progress_cb)(a->progress_arg, status, relpath);
8777 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8778 err = got_error_from_errno("close");
8780 got_object_blob_close(blob_base);
8781 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8782 err = got_error_from_errno("close");
8784 got_object_blob_close(blob_staged);
8790 const struct got_error *
8791 got_worktree_unstage(struct got_worktree *worktree,
8792 struct got_pathlist_head *paths,
8793 got_worktree_checkout_cb progress_cb, void *progress_arg,
8794 got_worktree_patch_cb patch_cb, void *patch_arg,
8795 struct got_repository *repo)
8797 const struct got_error *err = NULL, *sync_err, *unlockerr;
8798 struct got_pathlist_entry *pe;
8799 struct got_fileindex *fileindex = NULL;
8800 char *fileindex_path = NULL;
8801 struct unstage_path_arg upa;
8803 err = lock_worktree(worktree, LOCK_EX);
8807 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8811 upa.worktree = worktree;
8812 upa.fileindex = fileindex;
8814 upa.progress_cb = progress_cb;
8815 upa.progress_arg = progress_arg;
8816 upa.patch_cb = patch_cb;
8817 upa.patch_arg = patch_arg;
8818 TAILQ_FOREACH(pe, paths, entry) {
8819 err = worktree_status(worktree, pe->path, fileindex, repo,
8820 unstage_path, &upa, NULL, NULL, 1, 0);
8825 sync_err = sync_fileindex(fileindex, fileindex_path);
8826 if (sync_err && err == NULL)
8829 free(fileindex_path);
8831 got_fileindex_free(fileindex);
8832 unlockerr = lock_worktree(worktree, LOCK_SH);
8833 if (unlockerr && err == NULL)
8838 struct report_file_info_arg {
8839 struct got_worktree *worktree;
8840 got_worktree_path_info_cb info_cb;
8842 struct got_pathlist_head *paths;
8843 got_cancel_cb cancel_cb;
8847 static const struct got_error *
8848 report_file_info(void *arg, struct got_fileindex_entry *ie)
8850 struct report_file_info_arg *a = arg;
8851 struct got_pathlist_entry *pe;
8852 struct got_object_id blob_id, staged_blob_id, commit_id;
8853 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8854 struct got_object_id *commit_idp = NULL;
8857 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8858 return got_error(GOT_ERR_CANCELLED);
8860 TAILQ_FOREACH(pe, a->paths, entry) {
8861 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8862 got_path_is_child(ie->path, pe->path, pe->path_len))
8865 if (pe == NULL) /* not found */
8868 if (got_fileindex_entry_has_blob(ie)) {
8869 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8870 blob_idp = &blob_id;
8872 stage = got_fileindex_entry_stage_get(ie);
8873 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8874 stage == GOT_FILEIDX_STAGE_ADD) {
8875 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8876 SHA1_DIGEST_LENGTH);
8877 staged_blob_idp = &staged_blob_id;
8880 if (got_fileindex_entry_has_commit(ie)) {
8881 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8882 commit_idp = &commit_id;
8885 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8886 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8889 const struct got_error *
8890 got_worktree_path_info(struct got_worktree *worktree,
8891 struct got_pathlist_head *paths,
8892 got_worktree_path_info_cb info_cb, void *info_arg,
8893 got_cancel_cb cancel_cb, void *cancel_arg)
8896 const struct got_error *err = NULL, *unlockerr;
8897 struct got_fileindex *fileindex = NULL;
8898 char *fileindex_path = NULL;
8899 struct report_file_info_arg arg;
8901 err = lock_worktree(worktree, LOCK_SH);
8905 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8909 arg.worktree = worktree;
8910 arg.info_cb = info_cb;
8911 arg.info_arg = info_arg;
8913 arg.cancel_cb = cancel_cb;
8914 arg.cancel_arg = cancel_arg;
8915 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8918 free(fileindex_path);
8920 got_fileindex_free(fileindex);
8921 unlockerr = lock_worktree(worktree, LOCK_UN);
8922 if (unlockerr && err == NULL)
8927 static const struct got_error *
8928 patch_check_path(const char *p, char **path, unsigned char *status,
8929 unsigned char *staged_status, struct got_fileindex *fileindex,
8930 struct got_worktree *worktree, struct got_repository *repo)
8932 const struct got_error *err;
8933 struct got_fileindex_entry *ie;
8935 char *ondisk_path = NULL;
8937 err = got_worktree_resolve_path(path, worktree, p);
8941 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
8942 *path[0] ? "/" : "", *path) == -1)
8943 return got_error_from_errno("asprintf");
8945 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
8947 *staged_status = get_staged_status(ie);
8948 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
8953 *staged_status = GOT_STATUS_NO_CHANGE;
8954 *status = GOT_STATUS_UNVERSIONED;
8955 if (lstat(ondisk_path, &sb) == -1) {
8956 if (errno != ENOENT) {
8957 err = got_error_from_errno2("lstat",
8961 *status = GOT_STATUS_NONEXISTENT;
8970 static const struct got_error *
8971 patch_can_rm(const char *path, unsigned char status,
8972 unsigned char staged_status)
8974 if (status == GOT_STATUS_NONEXISTENT)
8975 return got_error_set_errno(ENOENT, path);
8976 if (status != GOT_STATUS_NO_CHANGE &&
8977 status != GOT_STATUS_ADD &&
8978 status != GOT_STATUS_MODIFY &&
8979 status != GOT_STATUS_MODE_CHANGE)
8980 return got_error_path(path, GOT_ERR_FILE_STATUS);
8981 if (staged_status == GOT_STATUS_DELETE)
8982 return got_error_path(path, GOT_ERR_FILE_STATUS);
8986 static const struct got_error *
8987 patch_can_add(const char *path, unsigned char status)
8989 if (status != GOT_STATUS_NONEXISTENT)
8990 return got_error_path(path, GOT_ERR_FILE_STATUS);
8994 static const struct got_error *
8995 patch_can_edit(const char *path, unsigned char status,
8996 unsigned char staged_status)
8998 if (status == GOT_STATUS_NONEXISTENT)
8999 return got_error_set_errno(ENOENT, path);
9000 if (status != GOT_STATUS_NO_CHANGE &&
9001 status != GOT_STATUS_ADD &&
9002 status != GOT_STATUS_MODIFY)
9003 return got_error_path(path, GOT_ERR_FILE_STATUS);
9004 if (staged_status == GOT_STATUS_DELETE)
9005 return got_error_path(path, GOT_ERR_FILE_STATUS);
9009 const struct got_error *
9010 got_worktree_patch_prepare(struct got_fileindex **fileindex,
9011 char **fileindex_path, struct got_worktree *worktree)
9013 return open_fileindex(fileindex, fileindex_path, worktree);
9016 const struct got_error *
9017 got_worktree_patch_check_path(const char *old, const char *new,
9018 char **oldpath, char **newpath, struct got_worktree *worktree,
9019 struct got_repository *repo, struct got_fileindex *fileindex)
9021 const struct got_error *err = NULL;
9022 int file_renamed = 0;
9023 unsigned char status_old, staged_status_old;
9024 unsigned char status_new, staged_status_new;
9029 err = patch_check_path(old != NULL ? old : new, oldpath,
9030 &status_old, &staged_status_old, fileindex, worktree, repo);
9034 err = patch_check_path(new != NULL ? new : old, newpath,
9035 &status_new, &staged_status_new, fileindex, worktree, repo);
9039 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9042 if (old != NULL && new == NULL)
9043 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9044 else if (file_renamed) {
9045 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9047 err = patch_can_add(*newpath, status_new);
9048 } else if (old == NULL)
9049 err = patch_can_add(*newpath, status_new);
9051 err = patch_can_edit(*newpath, status_new, staged_status_new);
9063 const struct got_error *
9064 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9065 struct got_worktree *worktree, struct got_fileindex *fileindex,
9066 got_worktree_checkout_cb progress_cb, void *progress_arg)
9068 struct schedule_addition_args saa;
9070 memset(&saa, 0, sizeof(saa));
9071 saa.worktree = worktree;
9072 saa.fileindex = fileindex;
9073 saa.progress_cb = progress_cb;
9074 saa.progress_arg = progress_arg;
9077 return worktree_status(worktree, path, fileindex, repo,
9078 schedule_addition, &saa, NULL, NULL, 1, 0);
9081 const struct got_error *
9082 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9083 struct got_worktree *worktree, struct got_fileindex *fileindex,
9084 got_worktree_delete_cb progress_cb, void *progress_arg)
9086 struct schedule_deletion_args sda;
9088 memset(&sda, 0, sizeof(sda));
9089 sda.worktree = worktree;
9090 sda.fileindex = fileindex;
9091 sda.progress_cb = progress_cb;
9092 sda.progress_arg = progress_arg;
9094 sda.delete_local_mods = 0;
9095 sda.keep_on_disk = 0;
9096 sda.ignore_missing_paths = 0;
9097 sda.status_codes = NULL;
9099 return worktree_status(worktree, path, fileindex, repo,
9100 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9103 const struct got_error *
9104 got_worktree_patch_complete(struct got_fileindex *fileindex,
9105 const char *fileindex_path)
9107 const struct got_error *err = NULL;
9109 err = sync_fileindex(fileindex, fileindex_path);
9110 got_fileindex_free(fileindex);