2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/queue.h>
38 #include "got_error.h"
39 #include "got_repository.h"
40 #include "got_reference.h"
41 #include "got_object.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
45 #include "got_opentemp.h"
48 #include "got_lib_worktree.h"
49 #include "got_lib_sha1.h"
50 #include "got_lib_fileindex.h"
51 #include "got_lib_inflate.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_object_idset.h"
57 #include "got_lib_diff.h"
58 #include "got_lib_gotconfig.h"
61 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
64 #define GOT_MERGE_LABEL_MERGED "merged change"
65 #define GOT_MERGE_LABEL_BASE "3-way merge base"
67 static mode_t apply_umask(mode_t);
69 static const struct got_error *
70 create_meta_file(const char *path_got, const char *name, const char *content)
72 const struct got_error *err = NULL;
75 if (asprintf(&path, "%s/%s", path_got, name) == -1)
76 return got_error_from_errno("asprintf");
78 err = got_path_create_file(path, content);
83 static const struct got_error *
84 update_meta_file(const char *path_got, const char *name, const char *content)
86 const struct got_error *err = NULL;
91 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
92 err = got_error_from_errno("asprintf");
97 err = got_opentemp_named(&tmppath, &tmpfile, path, "");
102 int len = fprintf(tmpfile, "%s\n", content);
103 if (len != strlen(content) + 1) {
104 err = got_error_from_errno2("fprintf", tmppath);
109 if (rename(tmppath, path) != 0) {
110 err = got_error_from_errno3("rename", tmppath, path);
116 if (fclose(tmpfile) == EOF && err == NULL)
117 err = got_error_from_errno2("fclose", tmppath);
122 static const struct got_error *
123 write_head_ref(const char *path_got, struct got_reference *head_ref)
125 const struct got_error *err = NULL;
128 if (got_ref_is_symbolic(head_ref)) {
129 refstr = got_ref_to_str(head_ref);
131 return got_error_from_errno("got_ref_to_str");
133 refstr = strdup(got_ref_get_name(head_ref));
135 return got_error_from_errno("strdup");
137 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
142 const struct got_error *
143 got_worktree_init(const char *path, struct got_reference *head_ref,
144 const char *prefix, struct got_repository *repo)
146 const struct got_error *err = NULL;
147 struct got_object_id *commit_id = NULL;
149 uint32_t uuid_status;
151 char *path_got = NULL;
152 char *formatstr = NULL;
153 char *absprefix = NULL;
154 char *basestr = NULL;
155 char *uuidstr = NULL;
157 if (strcmp(path, got_repo_get_path(repo)) == 0) {
158 err = got_error(GOT_ERR_WORKTREE_REPO);
162 err = got_ref_resolve(&commit_id, repo, head_ref);
165 err = got_object_get_type(&obj_type, repo, commit_id);
168 if (obj_type != GOT_OBJ_TYPE_COMMIT)
169 return got_error(GOT_ERR_OBJ_TYPE);
171 if (!got_path_is_absolute(prefix)) {
172 if (asprintf(&absprefix, "/%s", prefix) == -1)
173 return got_error_from_errno("asprintf");
176 /* Create top-level directory (may already exist). */
177 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
178 err = got_error_from_errno2("mkdir", path);
182 /* Create .got directory (may already exist). */
183 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
184 err = got_error_from_errno("asprintf");
187 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
188 err = got_error_from_errno2("mkdir", path_got);
192 /* Create an empty lock file. */
193 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
197 /* Create an empty file index. */
198 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
202 /* Write the HEAD reference. */
203 err = write_head_ref(path_got, head_ref);
207 /* Record our base commit. */
208 err = got_object_id_str(&basestr, commit_id);
211 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
215 /* Store path to repository. */
216 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
217 got_repo_get_path(repo));
221 /* Store in-repository path prefix. */
222 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
223 absprefix ? absprefix : prefix);
228 uuid_create(&uuid, &uuid_status);
229 if (uuid_status != uuid_s_ok) {
230 err = got_error_uuid(uuid_status, "uuid_create");
233 uuid_to_string(&uuid, &uuidstr, &uuid_status);
234 if (uuid_status != uuid_s_ok) {
235 err = got_error_uuid(uuid_status, "uuid_to_string");
238 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
242 /* Stamp work tree with format file. */
243 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
244 err = got_error_from_errno("asprintf");
247 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
261 const struct got_error *
262 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
263 const char *path_prefix)
265 char *absprefix = NULL;
267 if (!got_path_is_absolute(path_prefix)) {
268 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
269 return got_error_from_errno("asprintf");
271 *match = (strcmp(absprefix ? absprefix : path_prefix,
272 worktree->path_prefix) == 0);
278 got_worktree_get_head_ref_name(struct got_worktree *worktree)
280 return worktree->head_ref_name;
283 const struct got_error *
284 got_worktree_set_head_ref(struct got_worktree *worktree,
285 struct got_reference *head_ref)
287 const struct got_error *err = NULL;
288 char *path_got = NULL, *head_ref_name = NULL;
290 if (asprintf(&path_got, "%s/%s", worktree->root_path,
291 GOT_WORKTREE_GOT_DIR) == -1) {
292 err = got_error_from_errno("asprintf");
297 head_ref_name = strdup(got_ref_get_name(head_ref));
298 if (head_ref_name == NULL) {
299 err = got_error_from_errno("strdup");
303 err = write_head_ref(path_got, head_ref);
307 free(worktree->head_ref_name);
308 worktree->head_ref_name = head_ref_name;
316 struct got_object_id *
317 got_worktree_get_base_commit_id(struct got_worktree *worktree)
319 return worktree->base_commit_id;
322 const struct got_error *
323 got_worktree_set_base_commit_id(struct got_worktree *worktree,
324 struct got_repository *repo, struct got_object_id *commit_id)
326 const struct got_error *err;
327 struct got_object *obj = NULL;
329 char *path_got = NULL;
331 if (asprintf(&path_got, "%s/%s", worktree->root_path,
332 GOT_WORKTREE_GOT_DIR) == -1) {
333 err = got_error_from_errno("asprintf");
338 err = got_object_open(&obj, repo, commit_id);
342 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
343 err = got_error(GOT_ERR_OBJ_TYPE);
347 /* Record our base commit. */
348 err = got_object_id_str(&id_str, commit_id);
351 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
355 free(worktree->base_commit_id);
356 worktree->base_commit_id = got_object_id_dup(commit_id);
357 if (worktree->base_commit_id == NULL) {
358 err = got_error_from_errno("got_object_id_dup");
363 got_object_close(obj);
369 const struct got_gotconfig *
370 got_worktree_get_gotconfig(struct got_worktree *worktree)
372 return worktree->gotconfig;
375 static const struct got_error *
376 lock_worktree(struct got_worktree *worktree, int operation)
378 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
379 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
380 : got_error_from_errno2("flock",
381 got_worktree_get_root_path(worktree)));
385 static const struct got_error *
386 add_dir_on_disk(struct got_worktree *worktree, const char *path)
388 const struct got_error *err = NULL;
391 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
392 return got_error_from_errno("asprintf");
394 err = got_path_mkdir(abspath);
395 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
398 if (lstat(abspath, &sb) == -1) {
399 err = got_error_from_errno2("lstat", abspath);
400 } else if (!S_ISDIR(sb.st_mode)) {
401 /* TODO directory is obstructed; do something */
402 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
409 static const struct got_error *
410 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
412 const struct got_error *err = NULL;
415 size_t flen1 = 0, flen2 = 0;
420 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
421 if (flen1 == 0 && ferror(f1)) {
422 err = got_error_from_errno("fread");
425 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
426 if (flen2 == 0 && ferror(f2)) {
427 err = got_error_from_errno("fread");
434 } else if (flen2 == 0) {
438 } else if (flen1 == flen2) {
439 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
452 static const struct got_error *
453 check_files_equal(int *same, FILE *f1, FILE *f2)
460 if (fstat(fileno(f1), &sb) != 0)
461 return got_error_from_errno("fstat");
464 if (fstat(fileno(f2), &sb) != 0)
465 return got_error_from_errno("fstat");
468 if (size1 != size2) {
473 if (fseek(f1, 0L, SEEK_SET) == -1)
474 return got_ferror(f1, GOT_ERR_IO);
475 if (fseek(f2, 0L, SEEK_SET) == -1)
476 return got_ferror(f2, GOT_ERR_IO);
478 return check_file_contents_equal(same, f1, f2);
481 static const struct got_error *
482 copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
490 if (fseek(f, 0L, SEEK_SET) == -1)
491 return got_ferror(f, GOT_ERR_IO);
494 flen = fread(fbuf, 1, sizeof(fbuf), f);
497 return got_error_from_errno("fread");
501 outlen = write(outfd, fbuf, flen);
503 return got_error_from_errno("write");
505 return got_error(GOT_ERR_IO);
512 static const struct got_error *
513 merge_binary_file(int *overlapcnt, int merged_fd,
514 FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
515 const char *label_deriv, const char *label_orig, const char *label_deriv2,
516 const char *ondisk_path)
518 const struct got_error *err = NULL;
519 int same_content, changed_deriv, changed_deriv2;
520 int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
521 off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
522 char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
523 char *base_path_orig = NULL, *base_path_deriv = NULL;
524 char *base_path_deriv2 = NULL;
528 err = check_files_equal(&same_content, f_deriv, f_deriv2);
533 return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
535 err = check_files_equal(&same_content, f_deriv, f_orig);
538 changed_deriv = !same_content;
539 err = check_files_equal(&same_content, f_deriv2, f_orig);
542 changed_deriv2 = !same_content;
544 if (changed_deriv && changed_deriv2) {
546 if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
547 err = got_error_from_errno("asprintf");
550 if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
551 err = got_error_from_errno("asprintf");
554 if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
555 err = got_error_from_errno("asprintf");
558 err = got_opentemp_named_fd(&path_orig, &fd_orig,
562 err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
563 base_path_deriv, "");
566 err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
567 base_path_deriv2, "");
570 err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
573 err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
576 err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
579 if (dprintf(merged_fd, "Binary files differ and cannot be "
580 "merged automatically:\n") < 0) {
581 err = got_error_from_errno("dprintf");
584 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
585 GOT_DIFF_CONFLICT_MARKER_BEGIN,
586 label_deriv ? " " : "",
587 label_deriv ? label_deriv : "",
589 err = got_error_from_errno("dprintf");
593 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
594 GOT_DIFF_CONFLICT_MARKER_ORIG,
595 label_orig ? " " : "",
596 label_orig ? label_orig : "",
598 err = got_error_from_errno("dprintf");
602 if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
603 GOT_DIFF_CONFLICT_MARKER_SEP,
605 GOT_DIFF_CONFLICT_MARKER_END,
606 label_deriv2 ? " " : "",
607 label_deriv2 ? label_deriv2 : "") < 0) {
608 err = got_error_from_errno("dprintf");
611 } else if (changed_deriv)
612 err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
613 else if (changed_deriv2)
614 err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
616 if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
618 err = got_error_from_errno2("unlink", path_orig);
619 if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
620 err = got_error_from_errno2("close", path_orig);
621 if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
622 err = got_error_from_errno2("close", path_deriv);
623 if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
624 err = got_error_from_errno2("close", path_deriv2);
628 free(base_path_orig);
629 free(base_path_deriv);
630 free(base_path_deriv2);
635 * Perform a 3-way merge where the file f_orig acts as the common
636 * ancestor, the file f_deriv acts as the first derived version,
637 * and the file f_deriv2 acts as the second derived version.
638 * The merge result will be written to a new file at ondisk_path; any
639 * existing file at this path will be replaced.
641 static const struct got_error *
642 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
643 FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
644 const char *path, uint16_t st_mode,
645 const char *label_orig, const char *label_deriv, const char *label_deriv2,
646 enum got_diff_algorithm diff_algo, struct got_repository *repo,
647 got_worktree_checkout_cb progress_cb, void *progress_arg)
649 const struct got_error *err = NULL;
651 FILE *f_merged = NULL;
652 char *merged_path = NULL, *base_path = NULL;
656 *local_changes_subsumed = 0;
658 err = got_path_dirname(&parent, ondisk_path);
662 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
663 err = got_error_from_errno("asprintf");
667 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path, "");
671 err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
672 f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
674 if (err->code != GOT_ERR_FILE_BINARY)
676 err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
677 f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
683 err = (*progress_cb)(progress_arg,
684 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
688 if (fsync(merged_fd) != 0) {
689 err = got_error_from_errno("fsync");
693 f_merged = fdopen(merged_fd, "r");
694 if (f_merged == NULL) {
695 err = got_error_from_errno("fdopen");
700 /* Check if a clean merge has subsumed all local changes. */
701 if (overlapcnt == 0) {
702 err = check_files_equal(local_changes_subsumed, f_deriv,
708 if (fchmod(fileno(f_merged), apply_umask(st_mode)) != 0) {
709 err = got_error_from_errno2("fchmod", merged_path);
713 if (rename(merged_path, ondisk_path) != 0) {
714 err = got_error_from_errno3("rename", merged_path,
723 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
724 err = got_error_from_errno("close");
725 if (f_merged && fclose(f_merged) == EOF && err == NULL)
726 err = got_error_from_errno("fclose");
733 static const struct got_error *
734 update_symlink(const char *ondisk_path, const char *target_path,
737 /* This is not atomic but matches what 'ln -sf' does. */
738 if (unlink(ondisk_path) == -1)
739 return got_error_from_errno2("unlink", ondisk_path);
740 if (symlink(target_path, ondisk_path) == -1)
741 return got_error_from_errno3("symlink", target_path,
747 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
748 * in the work tree with a file that contains conflict markers and the
749 * conflicting target paths of the original version, a "derived version"
750 * of a symlink from an incoming change, and a local version of the symlink.
752 * The original versions's target path can be NULL if it is not available,
753 * such as if both derived versions added a new symlink at the same path.
755 * The incoming derived symlink target is NULL in case the incoming change
756 * has deleted this symlink.
758 static const struct got_error *
759 install_symlink_conflict(const char *deriv_target,
760 struct got_object_id *deriv_base_commit_id, const char *orig_target,
761 const char *label_orig, const char *local_target, const char *ondisk_path)
763 const struct got_error *err;
764 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
767 err = got_object_id_str(&id_str, deriv_base_commit_id);
769 return got_error_from_errno("asprintf");
771 if (asprintf(&label_deriv, "%s: commit %s",
772 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
773 err = got_error_from_errno("asprintf");
777 err = got_opentemp_named(&path, &f, "got-symlink-conflict", "");
781 if (fchmod(fileno(f), apply_umask(GOT_DEFAULT_FILE_MODE)) == -1) {
782 err = got_error_from_errno2("fchmod", path);
786 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
787 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
788 deriv_target ? deriv_target : "(symlink was deleted)",
789 orig_target ? label_orig : "",
790 orig_target ? "\n" : "",
791 orig_target ? orig_target : "",
792 orig_target ? "\n" : "",
793 GOT_DIFF_CONFLICT_MARKER_SEP,
794 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
795 err = got_error_from_errno2("fprintf", path);
799 if (unlink(ondisk_path) == -1) {
800 err = got_error_from_errno2("unlink", ondisk_path);
803 if (rename(path, ondisk_path) == -1) {
804 err = got_error_from_errno3("rename", path, ondisk_path);
808 if (f != NULL && fclose(f) == EOF && err == NULL)
809 err = got_error_from_errno2("fclose", path);
816 /* forward declaration */
817 static const struct got_error *
818 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
819 const char *, const char *, uint16_t, const char *,
820 struct got_blob_object *, struct got_object_id *,
821 struct got_repository *, got_worktree_checkout_cb, void *);
824 * Merge a symlink into the work tree, where blob_orig acts as the common
825 * ancestor, deriv_target is the link target of the first derived version,
826 * and the symlink on disk acts as the second derived version.
827 * Assume that contents of both blobs represent symlinks.
829 static const struct got_error *
830 merge_symlink(struct got_worktree *worktree,
831 struct got_blob_object *blob_orig, const char *ondisk_path,
832 const char *path, const char *label_orig, const char *deriv_target,
833 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
834 got_worktree_checkout_cb progress_cb, void *progress_arg)
836 const struct got_error *err = NULL;
837 char *ancestor_target = NULL;
839 ssize_t ondisk_len, deriv_len;
840 char ondisk_target[PATH_MAX];
841 int have_local_change = 0;
842 int have_incoming_change = 0;
844 if (lstat(ondisk_path, &sb) == -1)
845 return got_error_from_errno2("lstat", ondisk_path);
847 ondisk_len = readlink(ondisk_path, ondisk_target,
848 sizeof(ondisk_target));
849 if (ondisk_len == -1) {
850 err = got_error_from_errno2("readlink",
854 ondisk_target[ondisk_len] = '\0';
857 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
862 if (ancestor_target == NULL ||
863 (ondisk_len != strlen(ancestor_target) ||
864 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
865 have_local_change = 1;
867 deriv_len = strlen(deriv_target);
868 if (ancestor_target == NULL ||
869 (deriv_len != strlen(ancestor_target) ||
870 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
871 have_incoming_change = 1;
873 if (!have_local_change && !have_incoming_change) {
874 if (ancestor_target) {
875 /* Both sides made the same change. */
876 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
878 } else if (deriv_len == ondisk_len &&
879 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
880 /* Both sides added the same symlink. */
881 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
884 /* Both sides added symlinks which don't match. */
885 err = install_symlink_conflict(deriv_target,
886 deriv_base_commit_id, ancestor_target,
887 label_orig, ondisk_target, ondisk_path);
890 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
893 } else if (!have_local_change && have_incoming_change) {
894 /* Apply the incoming change. */
895 err = update_symlink(ondisk_path, deriv_target,
896 strlen(deriv_target));
899 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
900 } else if (have_local_change && have_incoming_change) {
901 if (deriv_len == ondisk_len &&
902 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
903 /* Both sides made the same change. */
904 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
907 err = install_symlink_conflict(deriv_target,
908 deriv_base_commit_id, ancestor_target, label_orig,
909 ondisk_target, ondisk_path);
912 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
918 free(ancestor_target);
922 static const struct got_error *
923 dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
925 const struct got_error *err = NULL;
926 char target_path[PATH_MAX];
935 return got_error_from_errno("got_opentemp");
936 target_len = readlink(ondisk_path, target_path, sizeof(target_path));
937 if (target_len == -1) {
938 err = got_error_from_errno2("readlink", ondisk_path);
941 n = fwrite(target_path, 1, target_len, f);
942 if (n != target_len) {
943 err = got_ferror(f, GOT_ERR_IO);
946 if (fflush(f) == EOF) {
947 err = got_error_from_errno("fflush");
950 if (fseek(f, 0L, SEEK_SET) == -1) {
951 err = got_ferror(f, GOT_ERR_IO);
963 * Perform a 3-way merge where blob_orig acts as the common ancestor,
964 * blob_deriv acts as the first derived version, and the file on disk
965 * acts as the second derived version.
967 static const struct got_error *
968 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
969 struct got_blob_object *blob_orig, const char *ondisk_path,
970 const char *path, uint16_t st_mode, const char *label_orig,
971 struct got_blob_object *blob_deriv,
972 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
973 got_worktree_checkout_cb progress_cb, void *progress_arg)
975 const struct got_error *err = NULL;
976 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
977 char *blob_orig_path = NULL;
978 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
979 char *label_deriv = NULL, *parent = NULL;
981 *local_changes_subsumed = 0;
983 err = got_path_dirname(&parent, ondisk_path);
988 if (asprintf(&base_path, "%s/got-merge-blob-orig",
990 err = got_error_from_errno("asprintf");
995 err = got_opentemp_named(&blob_orig_path, &f_orig,
999 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1006 * No common ancestor exists. This is an "add vs add" conflict
1007 * and we simply use an empty ancestor file to make both files
1008 * appear in the merged result in their entirety.
1010 f_orig = got_opentemp();
1011 if (f_orig == NULL) {
1012 err = got_error_from_errno("got_opentemp");
1017 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1018 err = got_error_from_errno("asprintf");
1023 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path, "");
1026 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1031 err = got_object_id_str(&id_str, deriv_base_commit_id);
1034 if (asprintf(&label_deriv, "%s: commit %s",
1035 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1036 err = got_error_from_errno("asprintf");
1041 * In order the run a 3-way merge with a symlink we copy the symlink's
1042 * target path into a temporary file and use that file with diff3.
1044 if (S_ISLNK(st_mode)) {
1045 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1050 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1052 err = got_error_from_errno2("open", ondisk_path);
1055 f_deriv2 = fdopen(fd, "r");
1056 if (f_deriv2 == NULL) {
1057 err = got_error_from_errno2("fdopen", ondisk_path);
1063 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1064 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1065 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1067 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1068 err = got_error_from_errno("fclose");
1069 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1070 err = got_error_from_errno("fclose");
1071 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1072 err = got_error_from_errno("fclose");
1074 if (blob_orig_path) {
1075 unlink(blob_orig_path);
1076 free(blob_orig_path);
1078 if (blob_deriv_path) {
1079 unlink(blob_deriv_path);
1080 free(blob_deriv_path);
1088 static const struct got_error *
1089 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1090 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1091 int wt_fd, const char *path, struct got_object_id *blob_id)
1093 const struct got_error *err = NULL;
1094 struct got_fileindex_entry *new_ie;
1098 err = got_fileindex_entry_alloc(&new_ie, path);
1102 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1103 blob_id->sha1, base_commit_id->sha1, 1);
1107 err = got_fileindex_entry_add(fileindex, new_ie);
1110 got_fileindex_entry_free(new_ie);
1117 get_ondisk_perms(int executable, mode_t st_mode)
1119 mode_t xbits = S_IXUSR;
1122 /* Map read bits to execute bits. */
1123 if (st_mode & S_IRGRP)
1125 if (st_mode & S_IROTH)
1127 return st_mode | xbits;
1134 apply_umask(mode_t mode)
1143 /* forward declaration */
1144 static const struct got_error *
1145 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1146 const char *path, mode_t te_mode, mode_t st_mode,
1147 struct got_blob_object *blob, int restoring_missing_file,
1148 int reverting_versioned_file, int installing_bad_symlink,
1149 int path_is_unversioned, struct got_repository *repo,
1150 got_worktree_checkout_cb progress_cb, void *progress_arg);
1153 * This function assumes that the provided symlink target points at a
1154 * safe location in the work tree!
1156 static const struct got_error *
1157 replace_existing_symlink(int *did_something, const char *ondisk_path,
1158 const char *target_path, size_t target_len)
1160 const struct got_error *err = NULL;
1162 char etarget[PATH_MAX];
1168 * "Bad" symlinks (those pointing outside the work tree or into the
1169 * .got directory) are installed in the work tree as a regular file
1170 * which contains the bad symlink target path.
1171 * The new symlink target has already been checked for safety by our
1172 * caller. If we can successfully open a regular file then we simply
1173 * replace this file with a symlink below.
1175 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1177 if (!got_err_open_nofollow_on_symlink())
1178 return got_error_from_errno2("open", ondisk_path);
1180 /* We are updating an existing on-disk symlink. */
1181 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1183 return got_error_from_errno2("readlink", ondisk_path);
1185 if (elen == target_len &&
1186 memcmp(etarget, target_path, target_len) == 0)
1187 return NULL; /* nothing to do */
1191 err = update_symlink(ondisk_path, target_path, target_len);
1192 if (fd != -1 && close(fd) == -1 && err == NULL)
1193 err = got_error_from_errno2("close", ondisk_path);
1197 static const struct got_error *
1198 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1199 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1201 const struct got_error *err = NULL;
1202 char canonpath[PATH_MAX];
1203 char *path_got = NULL;
1205 *is_bad_symlink = 0;
1207 if (target_len >= sizeof(canonpath)) {
1208 *is_bad_symlink = 1;
1213 * We do not use realpath(3) to resolve the symlink's target
1214 * path because we don't want to resolve symlinks recursively.
1215 * Instead we make the path absolute and then canonicalize it.
1216 * Relative symlink target lookup should begin at the directory
1217 * in which the blob object is being installed.
1219 if (!got_path_is_absolute(target_path)) {
1220 char *abspath, *parent;
1221 err = got_path_dirname(&parent, ondisk_path);
1224 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1226 return got_error_from_errno("asprintf");
1229 if (strlen(abspath) >= sizeof(canonpath)) {
1230 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1234 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1239 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1244 /* Only allow symlinks pointing at paths within the work tree. */
1245 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1246 *is_bad_symlink = 1;
1250 /* Do not allow symlinks pointing into the .got directory. */
1251 if (asprintf(&path_got, "%s/%s", wtroot_path,
1252 GOT_WORKTREE_GOT_DIR) == -1)
1253 return got_error_from_errno("asprintf");
1254 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1255 *is_bad_symlink = 1;
1261 static const struct got_error *
1262 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1263 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1264 int restoring_missing_file, int reverting_versioned_file,
1265 int path_is_unversioned, int allow_bad_symlinks,
1266 struct got_repository *repo,
1267 got_worktree_checkout_cb progress_cb, void *progress_arg)
1269 const struct got_error *err = NULL;
1270 char target_path[PATH_MAX];
1271 size_t len, target_len = 0;
1272 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1273 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1275 *is_bad_symlink = 0;
1278 * Blob object content specifies the target path of the link.
1279 * If a symbolic link cannot be installed we instead create
1280 * a regular file which contains the link target path stored
1281 * in the blob object.
1284 err = got_object_blob_read_block(&len, blob);
1288 if (len + target_len >= sizeof(target_path)) {
1289 /* Path too long; install as a regular file. */
1290 *is_bad_symlink = 1;
1291 got_object_blob_rewind(blob);
1292 return install_blob(worktree, ondisk_path, path,
1293 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1294 restoring_missing_file, reverting_versioned_file,
1295 1, path_is_unversioned, repo, progress_cb,
1299 /* Skip blob object header first time around. */
1300 memcpy(target_path + target_len, buf + hdrlen,
1302 target_len += len - hdrlen;
1306 target_path[target_len] = '\0';
1308 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1309 ondisk_path, worktree->root_path);
1313 if (*is_bad_symlink && !allow_bad_symlinks) {
1314 /* install as a regular file */
1315 got_object_blob_rewind(blob);
1316 err = install_blob(worktree, ondisk_path, path,
1317 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1318 restoring_missing_file, reverting_versioned_file, 1,
1319 path_is_unversioned, repo, progress_cb, progress_arg);
1323 if (symlink(target_path, ondisk_path) == -1) {
1324 if (errno == EEXIST) {
1325 int symlink_replaced;
1326 if (path_is_unversioned) {
1327 err = (*progress_cb)(progress_arg,
1328 GOT_STATUS_UNVERSIONED, path);
1331 err = replace_existing_symlink(&symlink_replaced,
1332 ondisk_path, target_path, target_len);
1336 if (symlink_replaced) {
1337 err = (*progress_cb)(progress_arg,
1338 reverting_versioned_file ?
1340 GOT_STATUS_UPDATE, path);
1342 err = (*progress_cb)(progress_arg,
1343 GOT_STATUS_EXISTS, path);
1346 return err; /* Nothing else to do. */
1349 if (errno == ENOENT) {
1351 err = got_path_dirname(&parent, ondisk_path);
1354 err = add_dir_on_disk(worktree, parent);
1359 * Retry, and fall through to error handling
1360 * below if this second attempt fails.
1362 if (symlink(target_path, ondisk_path) != -1) {
1363 err = NULL; /* success */
1368 /* Handle errors from first or second creation attempt. */
1369 if (errno == ENAMETOOLONG) {
1370 /* bad target path; install as a regular file */
1371 *is_bad_symlink = 1;
1372 got_object_blob_rewind(blob);
1373 err = install_blob(worktree, ondisk_path, path,
1374 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1375 restoring_missing_file, reverting_versioned_file, 1,
1376 path_is_unversioned, repo,
1377 progress_cb, progress_arg);
1378 } else if (errno == ENOTDIR) {
1379 err = got_error_path(ondisk_path,
1380 GOT_ERR_FILE_OBSTRUCTED);
1382 err = got_error_from_errno3("symlink",
1383 target_path, ondisk_path);
1385 } else if (progress_cb)
1386 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1387 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1391 static const struct got_error *
1392 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1393 const char *path, mode_t te_mode, mode_t st_mode,
1394 struct got_blob_object *blob, int restoring_missing_file,
1395 int reverting_versioned_file, int installing_bad_symlink,
1396 int path_is_unversioned, struct got_repository *repo,
1397 got_worktree_checkout_cb progress_cb, void *progress_arg)
1399 const struct got_error *err = NULL;
1403 char *tmppath = NULL;
1406 mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1407 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1410 if (errno == ENOENT) {
1412 err = got_path_dirname(&parent, path);
1415 err = add_dir_on_disk(worktree, parent);
1419 fd = open(ondisk_path,
1420 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1423 return got_error_from_errno2("open",
1425 } else if (errno == EEXIST) {
1426 if (path_is_unversioned) {
1427 err = (*progress_cb)(progress_arg,
1428 GOT_STATUS_UNVERSIONED, path);
1431 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1432 !S_ISREG(st_mode) && !installing_bad_symlink) {
1433 /* TODO file is obstructed; do something */
1434 err = got_error_path(ondisk_path,
1435 GOT_ERR_FILE_OBSTRUCTED);
1438 err = got_opentemp_named_fd(&tmppath, &fd,
1444 if (fchmod(fd, apply_umask(mode)) == -1) {
1445 err = got_error_from_errno2("fchmod",
1451 return got_error_from_errno2("open", ondisk_path);
1455 if (restoring_missing_file)
1456 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1458 else if (reverting_versioned_file)
1459 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1462 err = (*progress_cb)(progress_arg,
1463 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1468 hdrlen = got_object_blob_get_hdrlen(blob);
1470 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1471 err = got_object_blob_read_block(&len, blob);
1475 /* Skip blob object header first time around. */
1476 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1478 err = got_error_from_errno("write");
1480 } else if (outlen != len - hdrlen) {
1481 err = got_error(GOT_ERR_IO);
1488 if (fsync(fd) != 0) {
1489 err = got_error_from_errno("fsync");
1494 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1495 err = got_error_from_errno2("unlink", ondisk_path);
1498 if (rename(tmppath, ondisk_path) != 0) {
1499 err = got_error_from_errno3("rename", tmppath,
1508 if (fd != -1 && close(fd) == -1 && err == NULL)
1509 err = got_error_from_errno("close");
1510 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1511 err = got_error_from_errno2("unlink", tmppath);
1517 * Upgrade STATUS_MODIFY to STATUS_CONFLICT if a
1518 * conflict marker is found in newly added lines only.
1520 static const struct got_error *
1521 get_modified_file_content_status(unsigned char *status,
1522 struct got_blob_object *blob, const char *path, struct stat *sb,
1525 const struct got_error *err, *free_err;
1526 const char *markers[3] = {
1527 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1528 GOT_DIFF_CONFLICT_MARKER_SEP,
1529 GOT_DIFF_CONFLICT_MARKER_END
1532 struct got_diffreg_result *diffreg_result = NULL;
1533 struct diff_result *r;
1534 int nchunks_parsed, n, i = 0, ln = 0;
1536 size_t linesize = 0;
1539 if (*status != GOT_STATUS_MODIFY)
1542 f1 = got_opentemp();
1544 return got_error_from_errno("got_opentemp");
1547 got_object_blob_rewind(blob);
1548 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
1553 err = got_diff_files(&diffreg_result, f1, 1, NULL, ondisk_file,
1554 1, NULL, 0, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
1558 r = diffreg_result->result;
1560 for (n = 0; n < r->chunks.len; n += nchunks_parsed) {
1561 struct diff_chunk *c;
1562 struct diff_chunk_context cc = {};
1566 * We can optimise a little by advancing straight
1567 * to the next chunk if this one has no added lines.
1569 c = diff_chunk_get(r, n);
1571 if (diff_chunk_type(c) != CHUNK_PLUS) {
1573 continue; /* removed or unchanged lines */
1576 pos = diff_chunk_get_right_start_pos(c);
1577 if (fseek(ondisk_file, pos, SEEK_SET) == -1) {
1578 err = got_ferror(ondisk_file, GOT_ERR_IO);
1582 diff_chunk_context_load_change(&cc, &nchunks_parsed, r, n, 0);
1583 ln = cc.right.start;
1585 while (ln < cc.right.end) {
1586 linelen = getline(&line, &linesize, ondisk_file);
1587 if (linelen == -1) {
1588 if (feof(ondisk_file))
1590 err = got_ferror(ondisk_file, GOT_ERR_IO);
1594 if (line && strncmp(line, markers[i],
1595 strlen(markers[i])) == 0) {
1596 if (strcmp(markers[i],
1597 GOT_DIFF_CONFLICT_MARKER_END) == 0) {
1598 *status = GOT_STATUS_CONFLICT;
1609 if (f1 != NULL && fclose(f1) == EOF && err == NULL)
1610 err = got_error_from_errno("fclose");
1611 free_err = got_diffreg_result_free(diffreg_result);
1619 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1621 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1622 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1626 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1628 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1629 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1630 ie->mtime_sec == sb->st_mtim.tv_sec &&
1631 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1632 ie->size == (sb->st_size & 0xffffffff) &&
1633 !xbit_differs(ie, sb->st_mode));
1636 static unsigned char
1637 get_staged_status(struct got_fileindex_entry *ie)
1639 switch (got_fileindex_entry_stage_get(ie)) {
1640 case GOT_FILEIDX_STAGE_ADD:
1641 return GOT_STATUS_ADD;
1642 case GOT_FILEIDX_STAGE_DELETE:
1643 return GOT_STATUS_DELETE;
1644 case GOT_FILEIDX_STAGE_MODIFY:
1645 return GOT_STATUS_MODIFY;
1647 return GOT_STATUS_NO_CHANGE;
1651 static const struct got_error *
1652 get_symlink_modification_status(unsigned char *status,
1653 struct got_fileindex_entry *ie, const char *abspath,
1654 int dirfd, const char *de_name, struct got_blob_object *blob)
1656 const struct got_error *err = NULL;
1657 char target_path[PATH_MAX];
1658 char etarget[PATH_MAX];
1660 size_t len, target_len = 0;
1661 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1662 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1664 *status = GOT_STATUS_NO_CHANGE;
1666 /* Blob object content specifies the target path of the link. */
1668 err = got_object_blob_read_block(&len, blob);
1671 if (len + target_len >= sizeof(target_path)) {
1673 * Should not happen. The blob contents were OK
1674 * when this symlink was installed.
1676 return got_error(GOT_ERR_NO_SPACE);
1679 /* Skip blob object header first time around. */
1680 memcpy(target_path + target_len, buf + hdrlen,
1682 target_len += len - hdrlen;
1686 target_path[target_len] = '\0';
1689 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1691 return got_error_from_errno2("readlinkat", abspath);
1693 elen = readlink(abspath, etarget, sizeof(etarget));
1695 return got_error_from_errno2("readlink", abspath);
1698 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1699 *status = GOT_STATUS_MODIFY;
1704 static const struct got_error *
1705 get_file_status(unsigned char *status, struct stat *sb,
1706 struct got_fileindex_entry *ie, const char *abspath,
1707 int dirfd, const char *de_name, struct got_repository *repo)
1709 const struct got_error *err = NULL;
1710 struct got_object_id id;
1712 int fd = -1, fd1 = -1;
1715 struct got_blob_object *blob = NULL;
1717 unsigned char staged_status;
1719 staged_status = get_staged_status(ie);
1720 *status = GOT_STATUS_NO_CHANGE;
1721 memset(sb, 0, sizeof(*sb));
1724 * Whenever the caller provides a directory descriptor and a
1725 * directory entry name for the file, use them! This prevents
1726 * race conditions if filesystem paths change beneath our feet.
1729 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1730 if (errno == ENOENT) {
1731 if (got_fileindex_entry_has_file_on_disk(ie))
1732 *status = GOT_STATUS_MISSING;
1734 *status = GOT_STATUS_DELETE;
1737 err = got_error_from_errno2("fstatat", abspath);
1741 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1742 if (fd == -1 && errno != ENOENT &&
1743 !got_err_open_nofollow_on_symlink())
1744 return got_error_from_errno2("open", abspath);
1745 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1746 if (lstat(abspath, sb) == -1)
1747 return got_error_from_errno2("lstat", abspath);
1748 } else if (fd == -1 || fstat(fd, sb) == -1) {
1749 if (errno == ENOENT) {
1750 if (got_fileindex_entry_has_file_on_disk(ie))
1751 *status = GOT_STATUS_MISSING;
1753 *status = GOT_STATUS_DELETE;
1756 err = got_error_from_errno2("fstat", abspath);
1761 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1762 *status = GOT_STATUS_OBSTRUCTED;
1766 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1767 *status = GOT_STATUS_DELETE;
1769 } else if (!got_fileindex_entry_has_blob(ie) &&
1770 staged_status != GOT_STATUS_ADD) {
1771 *status = GOT_STATUS_ADD;
1775 if (!stat_info_differs(ie, sb))
1778 if (S_ISLNK(sb->st_mode) &&
1779 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1780 *status = GOT_STATUS_MODIFY;
1784 if (staged_status == GOT_STATUS_MODIFY ||
1785 staged_status == GOT_STATUS_ADD)
1786 got_fileindex_entry_get_staged_blob_id(&id, ie);
1788 got_fileindex_entry_get_blob_id(&id, ie);
1790 fd1 = got_opentempfd();
1792 err = got_error_from_errno("got_opentempfd");
1795 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1799 if (S_ISLNK(sb->st_mode)) {
1800 err = get_symlink_modification_status(status, ie,
1801 abspath, dirfd, de_name, blob);
1806 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1808 err = got_error_from_errno2("openat", abspath);
1813 f = fdopen(fd, "r");
1815 err = got_error_from_errno2("fdopen", abspath);
1819 hdrlen = got_object_blob_get_hdrlen(blob);
1821 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1822 err = got_object_blob_read_block(&blen, blob);
1825 /* Skip length of blob object header first time around. */
1826 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1827 if (flen == 0 && ferror(f)) {
1828 err = got_error_from_errno("fread");
1831 if (blen - hdrlen == 0) {
1833 *status = GOT_STATUS_MODIFY;
1835 } else if (flen == 0) {
1836 if (blen - hdrlen != 0)
1837 *status = GOT_STATUS_MODIFY;
1839 } else if (blen - hdrlen == flen) {
1840 /* Skip blob object header first time around. */
1841 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1842 *status = GOT_STATUS_MODIFY;
1846 *status = GOT_STATUS_MODIFY;
1852 if (*status == GOT_STATUS_MODIFY) {
1854 err = get_modified_file_content_status(status, blob, ie->path,
1856 } else if (xbit_differs(ie, sb->st_mode))
1857 *status = GOT_STATUS_MODE_CHANGE;
1859 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1860 err = got_error_from_errno("close");
1862 got_object_blob_close(blob);
1863 if (f != NULL && fclose(f) == EOF && err == NULL)
1864 err = got_error_from_errno2("fclose", abspath);
1865 if (fd != -1 && close(fd) == -1 && err == NULL)
1866 err = got_error_from_errno2("close", abspath);
1871 * Update timestamps in the file index if a file is unmodified and
1872 * we had to run a full content comparison to find out.
1874 static const struct got_error *
1875 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1876 struct got_fileindex_entry *ie, struct stat *sb)
1878 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1879 return got_fileindex_entry_update(ie, wt_fd, path,
1880 ie->blob_sha1, ie->commit_sha1, 1);
1885 static const struct got_error *
1886 update_blob(struct got_worktree *worktree,
1887 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1888 struct got_tree_entry *te, const char *path,
1889 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1892 const struct got_error *err = NULL;
1893 struct got_blob_object *blob = NULL;
1894 char *ondisk_path = NULL;
1895 unsigned char status = GOT_STATUS_NO_CHANGE;
1897 int fd1 = -1, fd2 = -1;
1899 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1900 return got_error_from_errno("asprintf");
1903 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1904 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1907 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1911 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1912 sb.st_mode = got_fileindex_perms_to_st(ie);
1914 if (stat(ondisk_path, &sb) == -1) {
1915 if (errno != ENOENT) {
1916 err = got_error_from_errno2("stat",
1920 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1921 status = GOT_STATUS_UNVERSIONED;
1923 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1924 status = GOT_STATUS_UNVERSIONED;
1926 status = GOT_STATUS_OBSTRUCTED;
1930 if (status == GOT_STATUS_OBSTRUCTED) {
1932 got_fileindex_entry_mark_skipped(ie);
1933 err = (*progress_cb)(progress_arg, status, path);
1936 if (status == GOT_STATUS_CONFLICT) {
1938 got_fileindex_entry_mark_skipped(ie);
1939 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1944 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1945 (S_ISLNK(te->mode) ||
1946 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1948 * This is a regular file or an installed bad symlink.
1949 * If the file index indicates that this file is already
1950 * up-to-date with respect to the repository we can skip
1951 * updating contents of this file.
1953 if (got_fileindex_entry_has_commit(ie) &&
1954 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1955 SHA1_DIGEST_LENGTH) == 0) {
1957 err = sync_timestamps(worktree->root_fd,
1958 path, status, ie, &sb);
1961 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1965 if (got_fileindex_entry_has_blob(ie) &&
1966 memcmp(ie->blob_sha1, te->id.sha1,
1967 SHA1_DIGEST_LENGTH) == 0) {
1968 /* Different commit but the same blob. */
1969 err = sync_timestamps(worktree->root_fd,
1970 path, status, ie, &sb);
1973 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1979 fd1 = got_opentempfd();
1981 err = got_error_from_errno("got_opentempfd");
1984 err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
1988 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1989 int update_timestamps;
1990 struct got_blob_object *blob2 = NULL;
1991 char *label_orig = NULL;
1992 if (got_fileindex_entry_has_blob(ie)) {
1993 fd2 = got_opentempfd();
1995 err = got_error_from_errno("got_opentempfd");
1998 struct got_object_id id2;
1999 got_fileindex_entry_get_blob_id(&id2, ie);
2000 err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
2005 if (got_fileindex_entry_has_commit(ie)) {
2006 char id_str[SHA1_DIGEST_STRING_LENGTH];
2007 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2008 sizeof(id_str)) == NULL) {
2009 err = got_error_path(id_str,
2010 GOT_ERR_BAD_OBJ_ID_STR);
2013 if (asprintf(&label_orig, "%s: commit %s",
2014 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2015 err = got_error_from_errno("asprintf");
2019 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2021 err = got_object_blob_read_to_str(&link_target, blob);
2024 err = merge_symlink(worktree, blob2, ondisk_path, path,
2025 label_orig, link_target, worktree->base_commit_id,
2026 repo, progress_cb, progress_arg);
2029 err = merge_blob(&update_timestamps, worktree, blob2,
2030 ondisk_path, path, sb.st_mode, label_orig, blob,
2031 worktree->base_commit_id, repo,
2032 progress_cb, progress_arg);
2035 if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2036 err = got_error_from_errno("close");
2040 got_object_blob_close(blob2);
2044 * Do not update timestamps of files with local changes.
2045 * Otherwise, a future status walk would treat them as
2046 * unmodified files again.
2048 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2049 blob->id.sha1, worktree->base_commit_id->sha1,
2051 } else if (status == GOT_STATUS_MODE_CHANGE) {
2052 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2053 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2054 } else if (status == GOT_STATUS_DELETE) {
2055 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2058 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2059 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2063 int is_bad_symlink = 0;
2064 if (S_ISLNK(te->mode)) {
2065 err = install_symlink(&is_bad_symlink, worktree,
2066 ondisk_path, path, blob,
2067 status == GOT_STATUS_MISSING, 0,
2068 status == GOT_STATUS_UNVERSIONED, 0,
2069 repo, progress_cb, progress_arg);
2071 err = install_blob(worktree, ondisk_path, path,
2072 te->mode, sb.st_mode, blob,
2073 status == GOT_STATUS_MISSING, 0, 0,
2074 status == GOT_STATUS_UNVERSIONED, repo,
2075 progress_cb, progress_arg);
2081 err = got_fileindex_entry_update(ie,
2082 worktree->root_fd, path, blob->id.sha1,
2083 worktree->base_commit_id->sha1, 1);
2085 err = create_fileindex_entry(&ie, fileindex,
2086 worktree->base_commit_id, worktree->root_fd, path,
2092 if (is_bad_symlink) {
2093 got_fileindex_entry_filetype_set(ie,
2094 GOT_FILEIDX_MODE_BAD_SYMLINK);
2098 if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2099 err = got_error_from_errno("close");
2102 got_object_blob_close(blob);
2108 static const struct got_error *
2109 remove_ondisk_file(const char *root_path, const char *path)
2111 const struct got_error *err = NULL;
2112 char *ondisk_path = NULL, *parent = NULL;
2114 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2115 return got_error_from_errno("asprintf");
2117 if (unlink(ondisk_path) == -1) {
2118 if (errno != ENOENT)
2119 err = got_error_from_errno2("unlink", ondisk_path);
2121 size_t root_len = strlen(root_path);
2122 err = got_path_dirname(&parent, ondisk_path);
2125 while (got_path_cmp(parent, root_path,
2126 strlen(parent), root_len) != 0) {
2128 ondisk_path = parent;
2130 if (rmdir(ondisk_path) == -1) {
2131 if (errno != ENOTEMPTY)
2132 err = got_error_from_errno2("rmdir",
2136 err = got_path_dirname(&parent, ondisk_path);
2147 static const struct got_error *
2148 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2149 struct got_fileindex_entry *ie, struct got_repository *repo,
2150 got_worktree_checkout_cb progress_cb, void *progress_arg)
2152 const struct got_error *err = NULL;
2153 unsigned char status;
2157 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2158 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2160 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2162 return got_error_from_errno("asprintf");
2164 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2168 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2169 char ondisk_target[PATH_MAX];
2170 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2171 sizeof(ondisk_target));
2172 if (ondisk_len == -1) {
2173 err = got_error_from_errno2("readlink", ondisk_path);
2176 ondisk_target[ondisk_len] = '\0';
2177 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2178 NULL, NULL, /* XXX pass common ancestor info? */
2179 ondisk_target, ondisk_path);
2182 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2187 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2188 status == GOT_STATUS_ADD) {
2189 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2193 * Preserve the working file and change the deleted blob's
2194 * entry into a schedule-add entry.
2196 err = got_fileindex_entry_update(ie, worktree->root_fd,
2197 ie->path, NULL, NULL, 0);
2199 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2202 if (status == GOT_STATUS_NO_CHANGE) {
2203 err = remove_ondisk_file(worktree->root_path, ie->path);
2207 got_fileindex_entry_remove(fileindex, ie);
2214 struct diff_cb_arg {
2215 struct got_fileindex *fileindex;
2216 struct got_worktree *worktree;
2217 struct got_repository *repo;
2218 got_worktree_checkout_cb progress_cb;
2220 got_cancel_cb cancel_cb;
2224 static const struct got_error *
2225 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2226 struct got_tree_entry *te, const char *parent_path)
2228 struct diff_cb_arg *a = arg;
2230 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2231 return got_error(GOT_ERR_CANCELLED);
2233 return update_blob(a->worktree, a->fileindex, ie, te,
2234 ie->path, a->repo, a->progress_cb, a->progress_arg);
2237 static const struct got_error *
2238 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2240 struct diff_cb_arg *a = arg;
2242 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2243 return got_error(GOT_ERR_CANCELLED);
2245 return delete_blob(a->worktree, a->fileindex, ie,
2246 a->repo, a->progress_cb, a->progress_arg);
2249 static const struct got_error *
2250 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2252 struct diff_cb_arg *a = arg;
2253 const struct got_error *err;
2256 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2257 return got_error(GOT_ERR_CANCELLED);
2259 if (got_object_tree_entry_is_submodule(te))
2262 if (asprintf(&path, "%s%s%s", parent_path,
2263 parent_path[0] ? "/" : "", te->name)
2265 return got_error_from_errno("asprintf");
2267 if (S_ISDIR(te->mode))
2268 err = add_dir_on_disk(a->worktree, path);
2270 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2271 a->repo, a->progress_cb, a->progress_arg);
2277 const struct got_error *
2278 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2280 uint32_t uuid_status;
2282 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2283 if (uuid_status != uuid_s_ok) {
2285 return got_error_uuid(uuid_status, "uuid_to_string");
2291 static const struct got_error *
2292 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2294 const struct got_error *err = NULL;
2295 char *uuidstr = NULL;
2299 err = got_worktree_get_uuid(&uuidstr, worktree);
2303 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2304 err = got_error_from_errno("asprintf");
2311 const struct got_error *
2312 got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2315 return get_ref_name(refname, worktree, prefix);
2318 const struct got_error *
2319 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2321 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2324 static const struct got_error *
2325 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2327 return get_ref_name(refname, worktree,
2328 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2331 static const struct got_error *
2332 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2334 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2337 static const struct got_error *
2338 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2340 return get_ref_name(refname, worktree,
2341 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2344 static const struct got_error *
2345 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2347 return get_ref_name(refname, worktree,
2348 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2351 static const struct got_error *
2352 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2354 return get_ref_name(refname, worktree,
2355 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2358 static const struct got_error *
2359 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2361 return get_ref_name(refname, worktree,
2362 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2365 static const struct got_error *
2366 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2368 return get_ref_name(refname, worktree,
2369 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2372 static const struct got_error *
2373 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2375 return get_ref_name(refname, worktree,
2376 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2379 const struct got_error *
2380 got_worktree_get_histedit_script_path(char **path,
2381 struct got_worktree *worktree)
2383 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2384 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2386 return got_error_from_errno("asprintf");
2391 static const struct got_error *
2392 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2394 return get_ref_name(refname, worktree,
2395 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2398 static const struct got_error *
2399 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2401 return get_ref_name(refname, worktree,
2402 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2406 * Prevent Git's garbage collector from deleting our base commit by
2407 * setting a reference to our base commit's ID.
2409 static const struct got_error *
2410 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2412 const struct got_error *err = NULL;
2413 struct got_reference *ref = NULL;
2416 err = got_worktree_get_base_ref_name(&refname, worktree);
2420 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2424 err = got_ref_write(ref, repo);
2432 static const struct got_error *
2433 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2435 const struct got_error *err = NULL;
2437 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2438 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2439 err = got_error_from_errno("asprintf");
2440 *fileindex_path = NULL;
2446 static const struct got_error *
2447 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2448 struct got_worktree *worktree)
2450 const struct got_error *err = NULL;
2453 *fileindex_path = NULL;
2454 *fileindex = got_fileindex_alloc();
2455 if (*fileindex == NULL)
2456 return got_error_from_errno("got_fileindex_alloc");
2458 err = get_fileindex_path(fileindex_path, worktree);
2462 index = fopen(*fileindex_path, "rbe");
2463 if (index == NULL) {
2464 if (errno != ENOENT)
2465 err = got_error_from_errno2("fopen", *fileindex_path);
2467 err = got_fileindex_read(*fileindex, index);
2468 if (fclose(index) == EOF && err == NULL)
2469 err = got_error_from_errno("fclose");
2473 free(*fileindex_path);
2474 *fileindex_path = NULL;
2475 got_fileindex_free(*fileindex);
2481 struct bump_base_commit_id_arg {
2482 struct got_object_id *base_commit_id;
2485 const char *entry_name;
2486 got_worktree_checkout_cb progress_cb;
2490 static const struct got_error *
2491 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2493 const struct got_error *err;
2494 struct bump_base_commit_id_arg *a = arg;
2496 if (a->entry_name) {
2497 if (strcmp(ie->path, a->path) != 0)
2499 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2502 if (got_fileindex_entry_was_skipped(ie))
2505 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2506 SHA1_DIGEST_LENGTH) == 0)
2509 if (a->progress_cb) {
2510 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2515 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2519 /* Bump base commit ID of all files within an updated part of the work tree. */
2520 static const struct got_error *
2521 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2522 struct got_fileindex *fileindex,
2523 got_worktree_checkout_cb progress_cb, void *progress_arg)
2525 struct bump_base_commit_id_arg bbc_arg;
2527 bbc_arg.base_commit_id = worktree->base_commit_id;
2528 bbc_arg.entry_name = NULL;
2530 bbc_arg.path_len = 0;
2531 bbc_arg.progress_cb = progress_cb;
2532 bbc_arg.progress_arg = progress_arg;
2534 return got_fileindex_for_each_entry_safe(fileindex,
2535 bump_base_commit_id, &bbc_arg);
2538 static const struct got_error *
2539 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2541 const struct got_error *err = NULL;
2542 char *new_fileindex_path = NULL;
2543 FILE *new_index = NULL;
2544 struct timespec timeout;
2546 err = got_opentemp_named(&new_fileindex_path, &new_index,
2547 fileindex_path, "");
2551 err = got_fileindex_write(fileindex, new_index);
2555 if (rename(new_fileindex_path, fileindex_path) != 0) {
2556 err = got_error_from_errno3("rename", new_fileindex_path,
2558 unlink(new_fileindex_path);
2562 * Sleep for a short amount of time to ensure that files modified after
2563 * this program exits have a different time stamp from the one which
2564 * was recorded in the file index.
2567 timeout.tv_nsec = 1;
2568 nanosleep(&timeout, NULL);
2572 free(new_fileindex_path);
2576 static const struct got_error *
2577 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2578 struct got_object_id **tree_id, const char *wt_relpath,
2579 struct got_commit_object *base_commit, struct got_worktree *worktree,
2580 struct got_repository *repo)
2582 const struct got_error *err = NULL;
2583 struct got_object_id *id = NULL;
2584 char *in_repo_path = NULL;
2585 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2587 *entry_type = GOT_OBJ_TYPE_ANY;
2588 *tree_relpath = NULL;
2591 if (wt_relpath[0] == '\0') {
2592 /* Check out all files within the work tree. */
2593 *entry_type = GOT_OBJ_TYPE_TREE;
2594 *tree_relpath = strdup("");
2595 if (*tree_relpath == NULL) {
2596 err = got_error_from_errno("strdup");
2599 err = got_object_id_by_path(tree_id, repo, base_commit,
2600 worktree->path_prefix);
2606 /* Check out a subset of files in the work tree. */
2608 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2609 is_root_wt ? "" : "/", wt_relpath) == -1) {
2610 err = got_error_from_errno("asprintf");
2614 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2619 in_repo_path = NULL;
2621 err = got_object_get_type(entry_type, repo, id);
2625 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2626 /* Check out a single file. */
2627 if (strchr(wt_relpath, '/') == NULL) {
2628 /* Check out a single file in work tree's root dir. */
2629 in_repo_path = strdup(worktree->path_prefix);
2630 if (in_repo_path == NULL) {
2631 err = got_error_from_errno("strdup");
2634 *tree_relpath = strdup("");
2635 if (*tree_relpath == NULL) {
2636 err = got_error_from_errno("strdup");
2640 /* Check out a single file in a subdirectory. */
2641 err = got_path_dirname(tree_relpath, wt_relpath);
2644 if (asprintf(&in_repo_path, "%s%s%s",
2645 worktree->path_prefix, is_root_wt ? "" : "/",
2646 *tree_relpath) == -1) {
2647 err = got_error_from_errno("asprintf");
2651 err = got_object_id_by_path(tree_id, repo,
2652 base_commit, in_repo_path);
2654 /* Check out all files within a subdirectory. */
2655 *tree_id = got_object_id_dup(id);
2656 if (*tree_id == NULL) {
2657 err = got_error_from_errno("got_object_id_dup");
2660 *tree_relpath = strdup(wt_relpath);
2661 if (*tree_relpath == NULL) {
2662 err = got_error_from_errno("strdup");
2670 *entry_type = GOT_OBJ_TYPE_ANY;
2671 free(*tree_relpath);
2672 *tree_relpath = NULL;
2679 static const struct got_error *
2680 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2681 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2682 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2683 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2685 const struct got_error *err = NULL;
2686 struct got_commit_object *commit = NULL;
2687 struct got_tree_object *tree = NULL;
2688 struct got_fileindex_diff_tree_cb diff_cb;
2689 struct diff_cb_arg arg;
2691 err = ref_base_commit(worktree, repo);
2693 if (!(err->code == GOT_ERR_ERRNO &&
2694 (errno == EACCES || errno == EROFS)))
2696 err = (*progress_cb)(progress_arg,
2697 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2702 err = got_object_open_as_commit(&commit, repo,
2703 worktree->base_commit_id);
2707 err = got_object_open_as_tree(&tree, repo, tree_id);
2712 got_object_tree_find_entry(tree, entry_name) == NULL) {
2713 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2717 diff_cb.diff_old_new = diff_old_new;
2718 diff_cb.diff_old = diff_old;
2719 diff_cb.diff_new = diff_new;
2720 arg.fileindex = fileindex;
2721 arg.worktree = worktree;
2723 arg.progress_cb = progress_cb;
2724 arg.progress_arg = progress_arg;
2725 arg.cancel_cb = cancel_cb;
2726 arg.cancel_arg = cancel_arg;
2727 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2728 entry_name, repo, &diff_cb, &arg);
2731 got_object_tree_close(tree);
2733 got_object_commit_close(commit);
2737 const struct got_error *
2738 got_worktree_checkout_files(struct got_worktree *worktree,
2739 struct got_pathlist_head *paths, struct got_repository *repo,
2740 got_worktree_checkout_cb progress_cb, void *progress_arg,
2741 got_cancel_cb cancel_cb, void *cancel_arg)
2743 const struct got_error *err = NULL, *sync_err, *unlockerr;
2744 struct got_commit_object *commit = NULL;
2745 struct got_tree_object *tree = NULL;
2746 struct got_fileindex *fileindex = NULL;
2747 char *fileindex_path = NULL;
2748 struct got_pathlist_entry *pe;
2749 struct tree_path_data {
2750 STAILQ_ENTRY(tree_path_data) entry;
2751 struct got_object_id *tree_id;
2756 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2758 STAILQ_INIT(&tree_paths);
2760 err = lock_worktree(worktree, LOCK_EX);
2764 err = got_object_open_as_commit(&commit, repo,
2765 worktree->base_commit_id);
2769 /* Map all specified paths to in-repository trees. */
2770 TAILQ_FOREACH(pe, paths, entry) {
2771 tpd = malloc(sizeof(*tpd));
2773 err = got_error_from_errno("malloc");
2777 err = find_tree_entry_for_checkout(&tpd->entry_type,
2778 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2785 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2786 err = got_path_basename(&tpd->entry_name, pe->path);
2794 tpd->entry_name = NULL;
2796 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2800 * Read the file index.
2801 * Checking out files is supposed to be an idempotent operation.
2802 * If the on-disk file index is incomplete we will try to complete it.
2804 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2808 tpd = STAILQ_FIRST(&tree_paths);
2809 TAILQ_FOREACH(pe, paths, entry) {
2810 struct bump_base_commit_id_arg bbc_arg;
2812 err = checkout_files(worktree, fileindex, tpd->relpath,
2813 tpd->tree_id, tpd->entry_name, repo,
2814 progress_cb, progress_arg, cancel_cb, cancel_arg);
2818 bbc_arg.base_commit_id = worktree->base_commit_id;
2819 bbc_arg.entry_name = tpd->entry_name;
2820 bbc_arg.path = pe->path;
2821 bbc_arg.path_len = pe->path_len;
2822 bbc_arg.progress_cb = progress_cb;
2823 bbc_arg.progress_arg = progress_arg;
2824 err = got_fileindex_for_each_entry_safe(fileindex,
2825 bump_base_commit_id, &bbc_arg);
2829 tpd = STAILQ_NEXT(tpd, entry);
2831 sync_err = sync_fileindex(fileindex, fileindex_path);
2832 if (sync_err && err == NULL)
2835 free(fileindex_path);
2837 got_object_tree_close(tree);
2839 got_object_commit_close(commit);
2841 got_fileindex_free(fileindex);
2842 while (!STAILQ_EMPTY(&tree_paths)) {
2843 tpd = STAILQ_FIRST(&tree_paths);
2844 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2849 unlockerr = lock_worktree(worktree, LOCK_SH);
2850 if (unlockerr && err == NULL)
2855 struct merge_file_cb_arg {
2856 struct got_worktree *worktree;
2857 struct got_fileindex *fileindex;
2858 got_worktree_checkout_cb progress_cb;
2860 got_cancel_cb cancel_cb;
2862 const char *label_orig;
2863 struct got_object_id *commit_id2;
2864 int allow_bad_symlinks;
2867 static const struct got_error *
2868 merge_file_cb(void *arg, struct got_blob_object *blob1,
2869 struct got_blob_object *blob2, FILE *f1, FILE *f2,
2870 struct got_object_id *id1, struct got_object_id *id2,
2871 const char *path1, const char *path2,
2872 mode_t mode1, mode_t mode2, struct got_repository *repo)
2874 static const struct got_error *err = NULL;
2875 struct merge_file_cb_arg *a = arg;
2876 struct got_fileindex_entry *ie;
2877 char *ondisk_path = NULL;
2879 unsigned char status;
2880 int local_changes_subsumed;
2881 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2882 char *id_str = NULL, *label_deriv2 = NULL;
2884 if (blob1 && blob2) {
2885 ie = got_fileindex_entry_get(a->fileindex, path2,
2888 return (*a->progress_cb)(a->progress_arg,
2889 GOT_STATUS_MISSING, path2);
2891 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2893 return got_error_from_errno("asprintf");
2895 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2900 if (status == GOT_STATUS_DELETE) {
2901 err = (*a->progress_cb)(a->progress_arg,
2902 GOT_STATUS_MERGE, path2);
2905 if (status != GOT_STATUS_NO_CHANGE &&
2906 status != GOT_STATUS_MODIFY &&
2907 status != GOT_STATUS_CONFLICT &&
2908 status != GOT_STATUS_ADD) {
2909 err = (*a->progress_cb)(a->progress_arg, status, path2);
2913 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2915 err = got_object_blob_read_to_str(&link_target2, blob2);
2918 err = merge_symlink(a->worktree, blob1, ondisk_path,
2919 path2, a->label_orig, link_target2, a->commit_id2,
2920 repo, a->progress_cb, a->progress_arg);
2925 f_orig = got_opentemp();
2926 if (f_orig == NULL) {
2927 err = got_error_from_errno("got_opentemp");
2930 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2935 f_deriv2 = got_opentemp();
2936 if (f_deriv2 == NULL)
2938 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2943 fd = open(ondisk_path,
2944 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2946 err = got_error_from_errno2("open",
2950 f_deriv = fdopen(fd, "r");
2951 if (f_deriv == NULL) {
2952 err = got_error_from_errno2("fdopen",
2957 err = got_object_id_str(&id_str, a->commit_id2);
2960 if (asprintf(&label_deriv2, "%s: commit %s",
2961 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2962 err = got_error_from_errno("asprintf");
2965 err = merge_file(&local_changes_subsumed, a->worktree,
2966 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2967 mode2, a->label_orig, NULL, label_deriv2,
2968 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2969 a->progress_cb, a->progress_arg);
2972 ie = got_fileindex_entry_get(a->fileindex, path1,
2975 return (*a->progress_cb)(a->progress_arg,
2976 GOT_STATUS_MISSING, path1);
2978 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2980 return got_error_from_errno("asprintf");
2982 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2988 case GOT_STATUS_NO_CHANGE:
2989 err = (*a->progress_cb)(a->progress_arg,
2990 GOT_STATUS_DELETE, path1);
2993 err = remove_ondisk_file(a->worktree->root_path, path1);
2997 got_fileindex_entry_mark_deleted_from_disk(ie);
2999 case GOT_STATUS_DELETE:
3000 case GOT_STATUS_MISSING:
3001 err = (*a->progress_cb)(a->progress_arg,
3002 GOT_STATUS_DELETE, path1);
3006 got_fileindex_entry_mark_deleted_from_disk(ie);
3008 case GOT_STATUS_ADD: {
3009 struct got_object_id *id;
3013 * Delete the added file only if its content already
3014 * exists in the repository.
3016 err = got_object_blob_file_create(&id, &blob1_f,
3017 &blob1_size, path1);
3020 if (got_object_id_cmp(id, id1) == 0) {
3021 err = (*a->progress_cb)(a->progress_arg,
3022 GOT_STATUS_DELETE, path1);
3025 err = remove_ondisk_file(a->worktree->root_path,
3030 got_fileindex_entry_remove(a->fileindex,
3033 err = (*a->progress_cb)(a->progress_arg,
3034 GOT_STATUS_CANNOT_DELETE, path1);
3036 if (fclose(blob1_f) == EOF && err == NULL)
3037 err = got_error_from_errno("fclose");
3043 case GOT_STATUS_MODIFY:
3044 case GOT_STATUS_CONFLICT:
3045 err = (*a->progress_cb)(a->progress_arg,
3046 GOT_STATUS_CANNOT_DELETE, path1);
3050 case GOT_STATUS_OBSTRUCTED:
3051 err = (*a->progress_cb)(a->progress_arg, status, path1);
3059 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3061 return got_error_from_errno("asprintf");
3062 ie = got_fileindex_entry_get(a->fileindex, path2,
3065 err = get_file_status(&status, &sb, ie, ondisk_path,
3069 if (status != GOT_STATUS_NO_CHANGE &&
3070 status != GOT_STATUS_MODIFY &&
3071 status != GOT_STATUS_CONFLICT &&
3072 status != GOT_STATUS_ADD) {
3073 err = (*a->progress_cb)(a->progress_arg,
3077 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3079 err = got_object_blob_read_to_str(&link_target2,
3083 err = merge_symlink(a->worktree, NULL,
3084 ondisk_path, path2, a->label_orig,
3085 link_target2, a->commit_id2, repo,
3086 a->progress_cb, a->progress_arg);
3088 } else if (S_ISREG(sb.st_mode)) {
3089 err = merge_blob(&local_changes_subsumed,
3090 a->worktree, NULL, ondisk_path, path2,
3091 sb.st_mode, a->label_orig, blob2,
3092 a->commit_id2, repo, a->progress_cb,
3095 err = got_error_path(ondisk_path,
3096 GOT_ERR_FILE_OBSTRUCTED);
3100 if (status == GOT_STATUS_DELETE) {
3101 err = got_fileindex_entry_update(ie,
3102 a->worktree->root_fd, path2, blob2->id.sha1,
3103 a->worktree->base_commit_id->sha1, 0);
3108 int is_bad_symlink = 0;
3109 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3110 if (S_ISLNK(mode2)) {
3111 err = install_symlink(&is_bad_symlink,
3112 a->worktree, ondisk_path, path2, blob2, 0,
3113 0, 1, a->allow_bad_symlinks, repo,
3114 a->progress_cb, a->progress_arg);
3116 err = install_blob(a->worktree, ondisk_path, path2,
3117 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3118 a->progress_cb, a->progress_arg);
3122 err = got_fileindex_entry_alloc(&ie, path2);
3125 err = got_fileindex_entry_update(ie,
3126 a->worktree->root_fd, path2, NULL, NULL, 1);
3128 got_fileindex_entry_free(ie);
3131 err = got_fileindex_entry_add(a->fileindex, ie);
3133 got_fileindex_entry_free(ie);
3136 if (is_bad_symlink) {
3137 got_fileindex_entry_filetype_set(ie,
3138 GOT_FILEIDX_MODE_BAD_SYMLINK);
3143 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3144 err = got_error_from_errno("fclose");
3145 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3146 err = got_error_from_errno("fclose");
3147 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3148 err = got_error_from_errno("fclose");
3155 static const struct got_error *
3156 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3158 struct got_worktree *worktree = arg;
3160 /* Reject merges into a work tree with mixed base commits. */
3161 if (got_fileindex_entry_has_commit(ie) &&
3162 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3163 SHA1_DIGEST_LENGTH) != 0)
3164 return got_error(GOT_ERR_MIXED_COMMITS);
3169 struct check_merge_conflicts_arg {
3170 struct got_worktree *worktree;
3171 struct got_fileindex *fileindex;
3172 struct got_repository *repo;
3175 static const struct got_error *
3176 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3177 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3178 struct got_object_id *id1, struct got_object_id *id2,
3179 const char *path1, const char *path2,
3180 mode_t mode1, mode_t mode2, struct got_repository *repo)
3182 const struct got_error *err = NULL;
3183 struct check_merge_conflicts_arg *a = arg;
3184 unsigned char status;
3186 struct got_fileindex_entry *ie;
3187 const char *path = path2 ? path2 : path1;
3188 struct got_object_id *id = id2 ? id2 : id1;
3194 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3198 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3200 return got_error_from_errno("asprintf");
3202 /* Reject merges into a work tree with conflicted files. */
3203 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3207 if (status == GOT_STATUS_CONFLICT)
3208 return got_error(GOT_ERR_CONFLICTS);
3213 static const struct got_error *
3214 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3215 const char *fileindex_path, struct got_object_id *commit_id1,
3216 struct got_object_id *commit_id2, struct got_repository *repo,
3217 got_worktree_checkout_cb progress_cb, void *progress_arg,
3218 got_cancel_cb cancel_cb, void *cancel_arg)
3220 const struct got_error *err = NULL, *sync_err;
3221 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3222 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3223 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3224 struct check_merge_conflicts_arg cmc_arg;
3225 struct merge_file_cb_arg arg;
3226 char *label_orig = NULL;
3227 FILE *f1 = NULL, *f2 = NULL;
3228 int fd1 = -1, fd2 = -1;
3231 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3234 err = got_object_id_by_path(&tree_id1, repo, commit1,
3235 worktree->path_prefix);
3236 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3242 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3246 err = got_object_id_str(&id_str, commit_id1);
3250 if (asprintf(&label_orig, "%s: commit %s",
3251 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3252 err = got_error_from_errno("asprintf");
3258 f1 = got_opentemp();
3260 err = got_error_from_errno("got_opentemp");
3265 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3269 err = got_object_id_by_path(&tree_id2, repo, commit2,
3270 worktree->path_prefix);
3274 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3278 f2 = got_opentemp();
3280 err = got_error_from_errno("got_opentemp");
3284 fd1 = got_opentempfd();
3286 err = got_error_from_errno("got_opentempfd");
3290 fd2 = got_opentempfd();
3292 err = got_error_from_errno("got_opentempfd");
3296 cmc_arg.worktree = worktree;
3297 cmc_arg.fileindex = fileindex;
3298 cmc_arg.repo = repo;
3299 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3300 check_merge_conflicts, &cmc_arg, 0);
3304 arg.worktree = worktree;
3305 arg.fileindex = fileindex;
3306 arg.progress_cb = progress_cb;
3307 arg.progress_arg = progress_arg;
3308 arg.cancel_cb = cancel_cb;
3309 arg.cancel_arg = cancel_arg;
3310 arg.label_orig = label_orig;
3311 arg.commit_id2 = commit_id2;
3312 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3313 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3314 merge_file_cb, &arg, 1);
3315 sync_err = sync_fileindex(fileindex, fileindex_path);
3316 if (sync_err && err == NULL)
3320 got_object_commit_close(commit1);
3322 got_object_commit_close(commit2);
3324 got_object_tree_close(tree1);
3326 got_object_tree_close(tree2);
3327 if (f1 && fclose(f1) == EOF && err == NULL)
3328 err = got_error_from_errno("fclose");
3329 if (f2 && fclose(f2) == EOF && err == NULL)
3330 err = got_error_from_errno("fclose");
3331 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3332 err = got_error_from_errno("close");
3333 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3334 err = got_error_from_errno("close");
3339 const struct got_error *
3340 got_worktree_merge_files(struct got_worktree *worktree,
3341 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3342 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3343 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3345 const struct got_error *err, *unlockerr;
3346 char *fileindex_path = NULL;
3347 struct got_fileindex *fileindex = NULL;
3349 err = lock_worktree(worktree, LOCK_EX);
3353 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3357 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3362 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3363 commit_id2, repo, progress_cb, progress_arg,
3364 cancel_cb, cancel_arg);
3367 got_fileindex_free(fileindex);
3368 free(fileindex_path);
3369 unlockerr = lock_worktree(worktree, LOCK_SH);
3370 if (unlockerr && err == NULL)
3375 struct diff_dir_cb_arg {
3376 struct got_fileindex *fileindex;
3377 struct got_worktree *worktree;
3378 const char *status_path;
3379 size_t status_path_len;
3380 struct got_repository *repo;
3381 got_worktree_status_cb status_cb;
3383 got_cancel_cb cancel_cb;
3385 /* A pathlist containing per-directory pathlists of ignore patterns. */
3386 struct got_pathlist_head *ignores;
3387 int report_unchanged;
3391 static const struct got_error *
3392 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3393 int dirfd, const char *de_name,
3394 got_worktree_status_cb status_cb, void *status_arg,
3395 struct got_repository *repo, int report_unchanged)
3397 const struct got_error *err = NULL;
3398 unsigned char status = GOT_STATUS_NO_CHANGE;
3399 unsigned char staged_status;
3401 struct got_object_id blob_id, commit_id, staged_blob_id;
3402 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3403 struct got_object_id *staged_blob_idp = NULL;
3405 staged_status = get_staged_status(ie);
3406 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3410 if (status == GOT_STATUS_NO_CHANGE &&
3411 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3414 if (got_fileindex_entry_has_blob(ie))
3415 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3416 if (got_fileindex_entry_has_commit(ie))
3417 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3418 if (staged_status == GOT_STATUS_ADD ||
3419 staged_status == GOT_STATUS_MODIFY) {
3420 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3421 &staged_blob_id, ie);
3424 return (*status_cb)(status_arg, status, staged_status,
3425 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3428 static const struct got_error *
3429 status_old_new(void *arg, struct got_fileindex_entry *ie,
3430 struct dirent *de, const char *parent_path, int dirfd)
3432 const struct got_error *err = NULL;
3433 struct diff_dir_cb_arg *a = arg;
3436 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3437 return got_error(GOT_ERR_CANCELLED);
3439 if (got_path_cmp(parent_path, a->status_path,
3440 strlen(parent_path), a->status_path_len) != 0 &&
3441 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3444 if (parent_path[0]) {
3445 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3446 parent_path, de->d_name) == -1)
3447 return got_error_from_errno("asprintf");
3449 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3451 return got_error_from_errno("asprintf");
3454 err = report_file_status(ie, abspath, dirfd, de->d_name,
3455 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3460 static const struct got_error *
3461 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3463 struct diff_dir_cb_arg *a = arg;
3464 struct got_object_id blob_id, commit_id;
3465 unsigned char status;
3467 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3468 return got_error(GOT_ERR_CANCELLED);
3470 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3473 got_fileindex_entry_get_blob_id(&blob_id, ie);
3474 got_fileindex_entry_get_commit_id(&commit_id, ie);
3475 if (got_fileindex_entry_has_file_on_disk(ie))
3476 status = GOT_STATUS_MISSING;
3478 status = GOT_STATUS_DELETE;
3479 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3480 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3484 free_ignores(struct got_pathlist_head *ignores)
3486 struct got_pathlist_entry *pe;
3488 TAILQ_FOREACH(pe, ignores, entry) {
3489 struct got_pathlist_head *ignorelist = pe->data;
3491 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3493 got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3496 static const struct got_error *
3497 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3499 const struct got_error *err = NULL;
3500 struct got_pathlist_entry *pe = NULL;
3501 struct got_pathlist_head *ignorelist;
3502 char *line = NULL, *pattern, *dirpath = NULL;
3503 size_t linesize = 0;
3506 ignorelist = calloc(1, sizeof(*ignorelist));
3507 if (ignorelist == NULL)
3508 return got_error_from_errno("calloc");
3509 TAILQ_INIT(ignorelist);
3511 while ((linelen = getline(&line, &linesize, f)) != -1) {
3512 if (linelen > 0 && line[linelen - 1] == '\n')
3513 line[linelen - 1] = '\0';
3515 /* Git's ignores may contain comments. */
3519 /* Git's negated patterns are not (yet?) supported. */
3523 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3525 err = got_error_from_errno("asprintf");
3528 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3533 err = got_error_from_errno("getline");
3537 dirpath = strdup(path);
3538 if (dirpath == NULL) {
3539 err = got_error_from_errno("strdup");
3542 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3545 if (err || pe == NULL) {
3547 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3553 match_path(const char *pattern, size_t pattern_len, const char *path,
3559 * Trailing slashes signify directories.
3560 * Append a * to make such patterns conform to fnmatch rules.
3562 if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
3563 if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
3564 return FNM_NOMATCH; /* XXX */
3566 return fnmatch(buf, path, flags);
3569 return fnmatch(pattern, path, flags);
3573 match_ignores(struct got_pathlist_head *ignores, const char *path)
3575 struct got_pathlist_entry *pe;
3577 /* Handle patterns which match in all directories. */
3578 TAILQ_FOREACH(pe, ignores, entry) {
3579 struct got_pathlist_head *ignorelist = pe->data;
3580 struct got_pathlist_entry *pi;
3582 TAILQ_FOREACH(pi, ignorelist, entry) {
3585 if (pi->path_len < 3 ||
3586 strncmp(pi->path, "**/", 3) != 0)
3590 if (match_path(pi->path + 3,
3591 pi->path_len - 3, p,
3592 FNM_PATHNAME | FNM_LEADING_DIR)) {
3593 /* Retry in next directory. */
3594 while (*p && *p != '/')
3606 * The ignores pathlist contains ignore lists from children before
3607 * parents, so we can find the most specific ignorelist by walking
3608 * ignores backwards.
3610 pe = TAILQ_LAST(ignores, got_pathlist_head);
3612 if (got_path_is_child(path, pe->path, pe->path_len)) {
3613 struct got_pathlist_head *ignorelist = pe->data;
3614 struct got_pathlist_entry *pi;
3615 TAILQ_FOREACH(pi, ignorelist, entry) {
3616 int flags = FNM_LEADING_DIR;
3617 if (strstr(pi->path, "/**/") == NULL)
3618 flags |= FNM_PATHNAME;
3619 if (match_path(pi->path, pi->path_len,
3625 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3631 static const struct got_error *
3632 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3633 const char *path, int dirfd, const char *ignores_filename)
3635 const struct got_error *err = NULL;
3638 FILE *ignoresfile = NULL;
3640 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3641 path[0] ? "/" : "", ignores_filename) == -1)
3642 return got_error_from_errno("asprintf");
3645 fd = openat(dirfd, ignores_filename,
3646 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3648 if (errno != ENOENT && errno != EACCES)
3649 err = got_error_from_errno2("openat",
3652 ignoresfile = fdopen(fd, "r");
3653 if (ignoresfile == NULL)
3654 err = got_error_from_errno2("fdopen",
3658 err = read_ignores(ignores, path, ignoresfile);
3662 ignoresfile = fopen(ignorespath, "re");
3663 if (ignoresfile == NULL) {
3664 if (errno != ENOENT && errno != EACCES)
3665 err = got_error_from_errno2("fopen",
3668 err = read_ignores(ignores, path, ignoresfile);
3671 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3672 err = got_error_from_errno2("fclose", path);
3673 if (fd != -1 && close(fd) == -1 && err == NULL)
3674 err = got_error_from_errno2("close", path);
3679 static const struct got_error *
3680 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3683 const struct got_error *err = NULL;
3684 struct diff_dir_cb_arg *a = arg;
3690 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3691 return got_error(GOT_ERR_CANCELLED);
3693 if (parent_path[0]) {
3694 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3695 return got_error_from_errno("asprintf");
3700 if (de->d_type == DT_DIR) {
3701 if (!a->no_ignores && ignore != NULL &&
3702 match_ignores(a->ignores, path))
3704 } else if (!match_ignores(a->ignores, path) &&
3705 got_path_is_child(path, a->status_path, a->status_path_len))
3706 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3707 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3713 static const struct got_error *
3714 status_traverse(void *arg, const char *path, int dirfd)
3716 const struct got_error *err = NULL;
3717 struct diff_dir_cb_arg *a = arg;
3722 err = add_ignores(a->ignores, a->worktree->root_path,
3723 path, dirfd, ".cvsignore");
3727 err = add_ignores(a->ignores, a->worktree->root_path, path,
3728 dirfd, ".gitignore");
3733 static const struct got_error *
3734 report_single_file_status(const char *path, const char *ondisk_path,
3735 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3736 void *status_arg, struct got_repository *repo, int report_unchanged,
3737 struct got_pathlist_head *ignores, int no_ignores)
3739 struct got_fileindex_entry *ie;
3742 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3744 return report_file_status(ie, ondisk_path, -1, NULL,
3745 status_cb, status_arg, repo, report_unchanged);
3747 if (lstat(ondisk_path, &sb) == -1) {
3748 if (errno != ENOENT)
3749 return got_error_from_errno2("lstat", ondisk_path);
3750 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3751 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3754 if (!no_ignores && match_ignores(ignores, path))
3757 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3758 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3759 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3764 static const struct got_error *
3765 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3766 const char *root_path, const char *path)
3768 const struct got_error *err;
3769 char *parent_path, *next_parent_path = NULL;
3771 err = add_ignores(ignores, root_path, "", -1,
3776 err = add_ignores(ignores, root_path, "", -1,
3781 err = got_path_dirname(&parent_path, path);
3783 if (err->code == GOT_ERR_BAD_PATH)
3784 return NULL; /* cannot traverse parent */
3788 err = add_ignores(ignores, root_path, parent_path, -1,
3792 err = add_ignores(ignores, root_path, parent_path, -1,
3796 err = got_path_dirname(&next_parent_path, parent_path);
3798 if (err->code == GOT_ERR_BAD_PATH)
3799 err = NULL; /* traversed everything */
3802 if (got_path_is_root_dir(parent_path))
3805 parent_path = next_parent_path;
3806 next_parent_path = NULL;
3810 free(next_parent_path);
3814 static const struct got_error *
3815 worktree_status(struct got_worktree *worktree, const char *path,
3816 struct got_fileindex *fileindex, struct got_repository *repo,
3817 got_worktree_status_cb status_cb, void *status_arg,
3818 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3819 int report_unchanged)
3821 const struct got_error *err = NULL;
3823 struct got_fileindex_diff_dir_cb fdiff_cb;
3824 struct diff_dir_cb_arg arg;
3825 char *ondisk_path = NULL;
3826 struct got_pathlist_head ignores;
3827 struct got_fileindex_entry *ie;
3829 TAILQ_INIT(&ignores);
3831 if (asprintf(&ondisk_path, "%s%s%s",
3832 worktree->root_path, path[0] ? "/" : "", path) == -1)
3833 return got_error_from_errno("asprintf");
3835 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3837 err = report_single_file_status(path, ondisk_path,
3838 fileindex, status_cb, status_arg, repo,
3839 report_unchanged, &ignores, no_ignores);
3843 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3845 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3846 !got_err_open_nofollow_on_symlink())
3847 err = got_error_from_errno2("open", ondisk_path);
3850 err = add_ignores_from_parent_paths(&ignores,
3851 worktree->root_path, ondisk_path);
3855 err = report_single_file_status(path, ondisk_path,
3856 fileindex, status_cb, status_arg, repo,
3857 report_unchanged, &ignores, no_ignores);
3860 fdiff_cb.diff_old_new = status_old_new;
3861 fdiff_cb.diff_old = status_old;
3862 fdiff_cb.diff_new = status_new;
3863 fdiff_cb.diff_traverse = status_traverse;
3864 arg.fileindex = fileindex;
3865 arg.worktree = worktree;
3866 arg.status_path = path;
3867 arg.status_path_len = strlen(path);
3869 arg.status_cb = status_cb;
3870 arg.status_arg = status_arg;
3871 arg.cancel_cb = cancel_cb;
3872 arg.cancel_arg = cancel_arg;
3873 arg.report_unchanged = report_unchanged;
3874 arg.no_ignores = no_ignores;
3876 err = add_ignores_from_parent_paths(&ignores,
3877 worktree->root_path, path);
3881 arg.ignores = &ignores;
3882 err = got_fileindex_diff_dir(fileindex, fd,
3883 worktree->root_path, path, repo, &fdiff_cb, &arg);
3886 free_ignores(&ignores);
3887 if (fd != -1 && close(fd) == -1 && err == NULL)
3888 err = got_error_from_errno("close");
3893 const struct got_error *
3894 got_worktree_status(struct got_worktree *worktree,
3895 struct got_pathlist_head *paths, struct got_repository *repo,
3896 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3897 got_cancel_cb cancel_cb, void *cancel_arg)
3899 const struct got_error *err = NULL;
3900 char *fileindex_path = NULL;
3901 struct got_fileindex *fileindex = NULL;
3902 struct got_pathlist_entry *pe;
3904 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3908 TAILQ_FOREACH(pe, paths, entry) {
3909 err = worktree_status(worktree, pe->path, fileindex, repo,
3910 status_cb, status_arg, cancel_cb, cancel_arg,
3915 free(fileindex_path);
3916 got_fileindex_free(fileindex);
3920 const struct got_error *
3921 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3924 const struct got_error *err = NULL;
3925 char *resolved = NULL, *cwd = NULL, *path = NULL;
3928 char *abspath = NULL;
3929 char canonpath[PATH_MAX];
3933 cwd = getcwd(NULL, 0);
3935 return got_error_from_errno("getcwd");
3937 if (lstat(arg, &sb) == -1) {
3938 if (errno != ENOENT) {
3939 err = got_error_from_errno2("lstat", arg);
3944 if (S_ISLNK(sb.st_mode)) {
3946 * We cannot use realpath(3) with symlinks since we want to
3947 * operate on the symlink itself.
3948 * But we can make the path absolute, assuming it is relative
3949 * to the current working directory, and then canonicalize it.
3951 if (!got_path_is_absolute(arg)) {
3952 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3953 err = got_error_from_errno("asprintf");
3958 err = got_canonpath(abspath ? abspath : arg, canonpath,
3962 resolved = strdup(canonpath);
3963 if (resolved == NULL) {
3964 err = got_error_from_errno("strdup");
3968 resolved = realpath(arg, NULL);
3969 if (resolved == NULL) {
3970 if (errno != ENOENT) {
3971 err = got_error_from_errno2("realpath", arg);
3974 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3975 err = got_error_from_errno("asprintf");
3978 err = got_canonpath(abspath, canonpath,
3982 resolved = strdup(canonpath);
3983 if (resolved == NULL) {
3984 err = got_error_from_errno("strdup");
3990 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3991 strlen(got_worktree_get_root_path(worktree)))) {
3992 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3996 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3997 err = got_path_skip_common_ancestor(&path,
3998 got_worktree_get_root_path(worktree), resolved);
4004 err = got_error_from_errno("strdup");
4009 /* XXX status walk can't deal with trailing slash! */
4011 while (len > 0 && path[len - 1] == '/') {
4012 path[len - 1] = '\0';
4026 struct schedule_addition_args {
4027 struct got_worktree *worktree;
4028 struct got_fileindex *fileindex;
4029 got_worktree_checkout_cb progress_cb;
4031 struct got_repository *repo;
4034 static const struct got_error *
4035 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4036 const char *relpath, struct got_object_id *blob_id,
4037 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4038 int dirfd, const char *de_name)
4040 struct schedule_addition_args *a = arg;
4041 const struct got_error *err = NULL;
4042 struct got_fileindex_entry *ie;
4046 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4048 return got_error_from_errno("asprintf");
4050 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4052 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4056 /* Re-adding an existing entry is a no-op. */
4057 if (status == GOT_STATUS_ADD)
4059 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4064 if (status != GOT_STATUS_UNVERSIONED) {
4065 if (status == GOT_STATUS_NONEXISTENT)
4066 err = got_error_set_errno(ENOENT, ondisk_path);
4068 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4072 err = got_fileindex_entry_alloc(&ie, relpath);
4075 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4076 relpath, NULL, NULL, 1);
4078 got_fileindex_entry_free(ie);
4081 err = got_fileindex_entry_add(a->fileindex, ie);
4083 got_fileindex_entry_free(ie);
4090 if (status == GOT_STATUS_ADD)
4092 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4095 const struct got_error *
4096 got_worktree_schedule_add(struct got_worktree *worktree,
4097 struct got_pathlist_head *paths,
4098 got_worktree_checkout_cb progress_cb, void *progress_arg,
4099 struct got_repository *repo, int no_ignores)
4101 struct got_fileindex *fileindex = NULL;
4102 char *fileindex_path = NULL;
4103 const struct got_error *err = NULL, *sync_err, *unlockerr;
4104 struct got_pathlist_entry *pe;
4105 struct schedule_addition_args saa;
4107 err = lock_worktree(worktree, LOCK_EX);
4111 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4115 saa.worktree = worktree;
4116 saa.fileindex = fileindex;
4117 saa.progress_cb = progress_cb;
4118 saa.progress_arg = progress_arg;
4121 TAILQ_FOREACH(pe, paths, entry) {
4122 err = worktree_status(worktree, pe->path, fileindex, repo,
4123 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4127 sync_err = sync_fileindex(fileindex, fileindex_path);
4128 if (sync_err && err == NULL)
4131 free(fileindex_path);
4133 got_fileindex_free(fileindex);
4134 unlockerr = lock_worktree(worktree, LOCK_SH);
4135 if (unlockerr && err == NULL)
4140 struct schedule_deletion_args {
4141 struct got_worktree *worktree;
4142 struct got_fileindex *fileindex;
4143 got_worktree_delete_cb progress_cb;
4145 struct got_repository *repo;
4146 int delete_local_mods;
4148 int ignore_missing_paths;
4149 const char *status_codes;
4152 static const struct got_error *
4153 schedule_for_deletion(void *arg, unsigned char status,
4154 unsigned char staged_status, const char *relpath,
4155 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4156 struct got_object_id *commit_id, int dirfd, const char *de_name)
4158 struct schedule_deletion_args *a = arg;
4159 const struct got_error *err = NULL;
4160 struct got_fileindex_entry *ie = NULL;
4164 if (status == GOT_STATUS_NONEXISTENT) {
4165 if (a->ignore_missing_paths)
4167 return got_error_set_errno(ENOENT, relpath);
4170 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4172 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4174 staged_status = get_staged_status(ie);
4175 if (staged_status != GOT_STATUS_NO_CHANGE) {
4176 if (staged_status == GOT_STATUS_DELETE)
4178 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4181 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4183 return got_error_from_errno("asprintf");
4185 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4190 if (a->status_codes) {
4191 size_t ncodes = strlen(a->status_codes);
4193 for (i = 0; i < ncodes ; i++) {
4194 if (status == a->status_codes[i])
4198 /* Do not delete files in non-matching status. */
4202 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4203 a->status_codes[i] != GOT_STATUS_MISSING) {
4204 static char msg[64];
4205 snprintf(msg, sizeof(msg),
4206 "invalid status code '%c'", a->status_codes[i]);
4207 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4212 if (status != GOT_STATUS_NO_CHANGE) {
4213 if (status == GOT_STATUS_DELETE)
4215 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4216 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4219 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4220 err = got_error_set_errno(ENOENT, relpath);
4223 if (status != GOT_STATUS_MODIFY &&
4224 status != GOT_STATUS_MISSING) {
4225 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4230 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4234 if (unlinkat(dirfd, de_name, 0) == -1) {
4235 err = got_error_from_errno2("unlinkat",
4239 } else if (unlink(ondisk_path) == -1) {
4240 err = got_error_from_errno2("unlink", ondisk_path);
4244 root_len = strlen(a->worktree->root_path);
4247 err = got_path_dirname(&parent, ondisk_path);
4251 ondisk_path = parent;
4252 if (rmdir(ondisk_path) == -1) {
4253 if (errno != ENOTEMPTY)
4254 err = got_error_from_errno2("rmdir",
4258 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4259 strlen(ondisk_path), root_len) != 0);
4262 got_fileindex_entry_mark_deleted_from_disk(ie);
4267 if (status == GOT_STATUS_DELETE)
4269 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4270 staged_status, relpath);
4273 const struct got_error *
4274 got_worktree_schedule_delete(struct got_worktree *worktree,
4275 struct got_pathlist_head *paths, int delete_local_mods,
4276 const char *status_codes,
4277 got_worktree_delete_cb progress_cb, void *progress_arg,
4278 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4280 struct got_fileindex *fileindex = NULL;
4281 char *fileindex_path = NULL;
4282 const struct got_error *err = NULL, *sync_err, *unlockerr;
4283 struct got_pathlist_entry *pe;
4284 struct schedule_deletion_args sda;
4286 err = lock_worktree(worktree, LOCK_EX);
4290 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4294 sda.worktree = worktree;
4295 sda.fileindex = fileindex;
4296 sda.progress_cb = progress_cb;
4297 sda.progress_arg = progress_arg;
4299 sda.delete_local_mods = delete_local_mods;
4300 sda.keep_on_disk = keep_on_disk;
4301 sda.ignore_missing_paths = ignore_missing_paths;
4302 sda.status_codes = status_codes;
4304 TAILQ_FOREACH(pe, paths, entry) {
4305 err = worktree_status(worktree, pe->path, fileindex, repo,
4306 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4310 sync_err = sync_fileindex(fileindex, fileindex_path);
4311 if (sync_err && err == NULL)
4314 free(fileindex_path);
4316 got_fileindex_free(fileindex);
4317 unlockerr = lock_worktree(worktree, LOCK_SH);
4318 if (unlockerr && err == NULL)
4323 static const struct got_error *
4324 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4326 const struct got_error *err = NULL;
4328 size_t linesize = 0, n;
4331 linelen = getline(&line, &linesize, infile);
4332 if (linelen == -1) {
4333 if (ferror(infile)) {
4334 err = got_error_from_errno("getline");
4340 n = fwrite(line, 1, linelen, outfile);
4342 err = got_ferror(outfile, GOT_ERR_IO);
4347 n = fwrite(line, 1, linelen, rejectfile);
4349 err = got_ferror(rejectfile, GOT_ERR_IO);
4356 static const struct got_error *
4357 skip_one_line(FILE *f)
4360 size_t linesize = 0;
4363 linelen = getline(&line, &linesize, f);
4364 if (linelen == -1) {
4366 return got_error_from_errno("getline");
4373 static const struct got_error *
4374 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4375 int start_old, int end_old, int start_new, int end_new,
4376 FILE *outfile, FILE *rejectfile)
4378 const struct got_error *err;
4380 /* Copy old file's lines leading up to patch. */
4381 while (!feof(f1) && *line_cur1 < start_old) {
4382 err = copy_one_line(f1, outfile, NULL);
4387 /* Skip new file's lines leading up to patch. */
4388 while (!feof(f2) && *line_cur2 < start_new) {
4390 err = copy_one_line(f2, NULL, rejectfile);
4392 err = skip_one_line(f2);
4397 /* Copy patched lines. */
4398 while (!feof(f2) && *line_cur2 <= end_new) {
4399 err = copy_one_line(f2, outfile, NULL);
4404 /* Skip over old file's replaced lines. */
4405 while (!feof(f1) && *line_cur1 <= end_old) {
4407 err = copy_one_line(f1, NULL, rejectfile);
4409 err = skip_one_line(f1);
4418 static const struct got_error *
4419 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4420 FILE *outfile, FILE *rejectfile)
4422 const struct got_error *err;
4425 /* Copy old file's lines until EOF. */
4427 err = copy_one_line(f1, outfile, NULL);
4434 /* Copy new file's lines until EOF. */
4436 err = copy_one_line(f2, NULL, rejectfile);
4446 static const struct got_error *
4447 apply_or_reject_change(int *choice, int *nchunks_used,
4448 struct diff_result *diff_result, int n,
4449 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4450 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4451 got_worktree_patch_cb patch_cb, void *patch_arg)
4453 const struct got_error *err = NULL;
4454 struct diff_chunk_context cc = {};
4455 int start_old, end_old, start_new, end_new;
4457 struct diff_output_unidiff_state *diff_state;
4458 struct diff_input_info diff_info;
4461 *choice = GOT_PATCH_CHOICE_NONE;
4463 /* Get changed line numbers without context lines for copy_change(). */
4464 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4465 start_old = cc.left.start;
4466 end_old = cc.left.end;
4467 start_new = cc.right.start;
4468 end_new = cc.right.end;
4470 /* Get the same change with context lines for display. */
4471 memset(&cc, 0, sizeof(cc));
4472 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4474 memset(&diff_info, 0, sizeof(diff_info));
4475 diff_info.left_path = relpath;
4476 diff_info.right_path = relpath;
4478 diff_state = diff_output_unidiff_state_alloc();
4479 if (diff_state == NULL)
4480 return got_error_set_errno(ENOMEM,
4481 "diff_output_unidiff_state_alloc");
4483 hunkfile = got_opentemp();
4484 if (hunkfile == NULL) {
4485 err = got_error_from_errno("got_opentemp");
4489 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4491 if (rc != DIFF_RC_OK) {
4492 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4496 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4497 err = got_ferror(hunkfile, GOT_ERR_IO);
4501 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4502 hunkfile, changeno, nchanges);
4507 case GOT_PATCH_CHOICE_YES:
4508 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4509 end_old, start_new, end_new, outfile, rejectfile);
4511 case GOT_PATCH_CHOICE_NO:
4512 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4513 end_old, start_new, end_new, rejectfile, outfile);
4515 case GOT_PATCH_CHOICE_QUIT:
4518 err = got_error(GOT_ERR_PATCH_CHOICE);
4522 diff_output_unidiff_state_free(diff_state);
4523 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4524 err = got_error_from_errno("fclose");
4528 struct revert_file_args {
4529 struct got_worktree *worktree;
4530 struct got_fileindex *fileindex;
4531 got_worktree_checkout_cb progress_cb;
4533 got_worktree_patch_cb patch_cb;
4535 struct got_repository *repo;
4536 int unlink_added_files;
4539 static const struct got_error *
4540 create_patched_content(char **path_outfile, int reverse_patch,
4541 struct got_object_id *blob_id, const char *path2,
4542 int dirfd2, const char *de_name2,
4543 const char *relpath, struct got_repository *repo,
4544 got_worktree_patch_cb patch_cb, void *patch_arg)
4546 const struct got_error *err, *free_err;
4547 struct got_blob_object *blob = NULL;
4548 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4549 int fd = -1, fd2 = -1;
4550 char link_target[PATH_MAX];
4551 ssize_t link_len = 0;
4552 char *path1 = NULL, *id_str = NULL;
4554 struct got_diffreg_result *diffreg_result = NULL;
4555 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4556 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4558 *path_outfile = NULL;
4560 err = got_object_id_str(&id_str, blob_id);
4565 fd2 = openat(dirfd2, de_name2,
4566 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4568 if (!got_err_open_nofollow_on_symlink()) {
4569 err = got_error_from_errno2("openat", path2);
4572 link_len = readlinkat(dirfd2, de_name2,
4573 link_target, sizeof(link_target));
4574 if (link_len == -1) {
4575 return got_error_from_errno2("readlinkat",
4578 sb2.st_mode = S_IFLNK;
4579 sb2.st_size = link_len;
4582 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4584 if (!got_err_open_nofollow_on_symlink()) {
4585 err = got_error_from_errno2("open", path2);
4588 link_len = readlink(path2, link_target,
4589 sizeof(link_target));
4591 return got_error_from_errno2("readlink", path2);
4592 sb2.st_mode = S_IFLNK;
4593 sb2.st_size = link_len;
4597 if (fstat(fd2, &sb2) == -1) {
4598 err = got_error_from_errno2("fstat", path2);
4602 f2 = fdopen(fd2, "r");
4604 err = got_error_from_errno2("fdopen", path2);
4610 f2 = got_opentemp();
4612 err = got_error_from_errno2("got_opentemp", path2);
4615 n = fwrite(link_target, 1, link_len, f2);
4616 if (n != link_len) {
4617 err = got_ferror(f2, GOT_ERR_IO);
4620 if (fflush(f2) == EOF) {
4621 err = got_error_from_errno("fflush");
4627 fd = got_opentempfd();
4629 err = got_error_from_errno("got_opentempfd");
4633 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4637 err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4641 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4645 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4646 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4650 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4655 if (fseek(f1, 0L, SEEK_SET) == -1)
4656 return got_ferror(f1, GOT_ERR_IO);
4657 if (fseek(f2, 0L, SEEK_SET) == -1)
4658 return got_ferror(f2, GOT_ERR_IO);
4660 /* Count the number of actual changes in the diff result. */
4661 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4662 struct diff_chunk_context cc = {};
4663 diff_chunk_context_load_change(&cc, &nchunks_used,
4664 diffreg_result->result, n, 0);
4667 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4669 err = apply_or_reject_change(&choice, &nchunks_used,
4670 diffreg_result->result, n, relpath, f1, f2,
4671 &line_cur1, &line_cur2,
4672 reverse_patch ? NULL : outfile,
4673 reverse_patch ? outfile : NULL,
4674 ++i, nchanges, patch_cb, patch_arg);
4677 if (choice == GOT_PATCH_CHOICE_YES)
4679 else if (choice == GOT_PATCH_CHOICE_QUIT)
4683 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4684 reverse_patch ? NULL : outfile,
4685 reverse_patch ? outfile : NULL);
4689 if (!S_ISLNK(sb2.st_mode)) {
4692 mode = apply_umask(sb2.st_mode);
4693 if (fchmod(fileno(outfile), mode) == -1) {
4694 err = got_error_from_errno2("fchmod", path2);
4701 if (fd != -1 && close(fd) == -1 && err == NULL)
4702 err = got_error_from_errno("close");
4704 got_object_blob_close(blob);
4705 free_err = got_diffreg_result_free(diffreg_result);
4708 if (f1 && fclose(f1) == EOF && err == NULL)
4709 err = got_error_from_errno2("fclose", path1);
4710 if (f2 && fclose(f2) == EOF && err == NULL)
4711 err = got_error_from_errno2("fclose", path2);
4712 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4713 err = got_error_from_errno2("close", path2);
4714 if (outfile && fclose(outfile) == EOF && err == NULL)
4715 err = got_error_from_errno2("fclose", *path_outfile);
4716 if (path1 && unlink(path1) == -1 && err == NULL)
4717 err = got_error_from_errno2("unlink", path1);
4718 if (err || !have_content) {
4719 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4720 err = got_error_from_errno2("unlink", *path_outfile);
4721 free(*path_outfile);
4722 *path_outfile = NULL;
4728 static const struct got_error *
4729 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4730 const char *relpath, struct got_object_id *blob_id,
4731 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4732 int dirfd, const char *de_name)
4734 struct revert_file_args *a = arg;
4735 const struct got_error *err = NULL;
4736 char *parent_path = NULL;
4737 struct got_fileindex_entry *ie;
4738 struct got_commit_object *base_commit = NULL;
4739 struct got_tree_object *tree = NULL;
4740 struct got_object_id *tree_id = NULL;
4741 const struct got_tree_entry *te = NULL;
4742 char *tree_path = NULL, *te_name;
4743 char *ondisk_path = NULL, *path_content = NULL;
4744 struct got_blob_object *blob = NULL;
4747 /* Reverting a staged deletion is a no-op. */
4748 if (status == GOT_STATUS_DELETE &&
4749 staged_status != GOT_STATUS_NO_CHANGE)
4752 if (status == GOT_STATUS_UNVERSIONED)
4753 return (*a->progress_cb)(a->progress_arg,
4754 GOT_STATUS_UNVERSIONED, relpath);
4756 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4758 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4760 /* Construct in-repository path of tree which contains this blob. */
4761 err = got_path_dirname(&parent_path, ie->path);
4763 if (err->code != GOT_ERR_BAD_PATH)
4765 parent_path = strdup("/");
4766 if (parent_path == NULL) {
4767 err = got_error_from_errno("strdup");
4771 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4772 tree_path = strdup(parent_path);
4773 if (tree_path == NULL) {
4774 err = got_error_from_errno("strdup");
4778 if (got_path_is_root_dir(parent_path)) {
4779 tree_path = strdup(a->worktree->path_prefix);
4780 if (tree_path == NULL) {
4781 err = got_error_from_errno("strdup");
4785 if (asprintf(&tree_path, "%s/%s",
4786 a->worktree->path_prefix, parent_path) == -1) {
4787 err = got_error_from_errno("asprintf");
4793 err = got_object_open_as_commit(&base_commit, a->repo,
4794 a->worktree->base_commit_id);
4798 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4800 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4801 (status == GOT_STATUS_ADD ||
4802 staged_status == GOT_STATUS_ADD)))
4805 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4809 err = got_path_basename(&te_name, ie->path);
4813 te = got_object_tree_find_entry(tree, te_name);
4815 if (te == NULL && status != GOT_STATUS_ADD &&
4816 staged_status != GOT_STATUS_ADD) {
4817 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4823 case GOT_STATUS_ADD:
4825 int choice = GOT_PATCH_CHOICE_NONE;
4826 err = (*a->patch_cb)(&choice, a->patch_arg,
4827 status, ie->path, NULL, 1, 1);
4830 if (choice != GOT_PATCH_CHOICE_YES)
4833 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4837 got_fileindex_entry_remove(a->fileindex, ie);
4838 if (a->unlink_added_files) {
4839 if (asprintf(&ondisk_path, "%s/%s",
4840 got_worktree_get_root_path(a->worktree),
4842 err = got_error_from_errno("asprintf");
4845 if (unlink(ondisk_path) == -1) {
4846 err = got_error_from_errno2("unlink",
4852 case GOT_STATUS_DELETE:
4854 int choice = GOT_PATCH_CHOICE_NONE;
4855 err = (*a->patch_cb)(&choice, a->patch_arg,
4856 status, ie->path, NULL, 1, 1);
4859 if (choice != GOT_PATCH_CHOICE_YES)
4863 case GOT_STATUS_MODIFY:
4864 case GOT_STATUS_MODE_CHANGE:
4865 case GOT_STATUS_CONFLICT:
4866 case GOT_STATUS_MISSING: {
4867 struct got_object_id id;
4868 if (staged_status == GOT_STATUS_ADD ||
4869 staged_status == GOT_STATUS_MODIFY)
4870 got_fileindex_entry_get_staged_blob_id(&id, ie);
4872 got_fileindex_entry_get_blob_id(&id, ie);
4873 fd = got_opentempfd();
4875 err = got_error_from_errno("got_opentempfd");
4879 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4883 if (asprintf(&ondisk_path, "%s/%s",
4884 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4885 err = got_error_from_errno("asprintf");
4889 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4890 status == GOT_STATUS_CONFLICT)) {
4891 int is_bad_symlink = 0;
4892 err = create_patched_content(&path_content, 1, &id,
4893 ondisk_path, dirfd, de_name, ie->path, a->repo,
4894 a->patch_cb, a->patch_arg);
4895 if (err || path_content == NULL)
4897 if (te && S_ISLNK(te->mode)) {
4898 if (unlink(path_content) == -1) {
4899 err = got_error_from_errno2("unlink",
4903 err = install_symlink(&is_bad_symlink,
4904 a->worktree, ondisk_path, ie->path,
4905 blob, 0, 1, 0, 0, a->repo,
4906 a->progress_cb, a->progress_arg);
4908 if (rename(path_content, ondisk_path) == -1) {
4909 err = got_error_from_errno3("rename",
4910 path_content, ondisk_path);
4915 int is_bad_symlink = 0;
4916 if (te && S_ISLNK(te->mode)) {
4917 err = install_symlink(&is_bad_symlink,
4918 a->worktree, ondisk_path, ie->path,
4919 blob, 0, 1, 0, 0, a->repo,
4920 a->progress_cb, a->progress_arg);
4922 err = install_blob(a->worktree, ondisk_path,
4924 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4925 got_fileindex_perms_to_st(ie), blob,
4926 0, 1, 0, 0, a->repo,
4927 a->progress_cb, a->progress_arg);
4931 if (status == GOT_STATUS_DELETE ||
4932 status == GOT_STATUS_MODE_CHANGE) {
4933 err = got_fileindex_entry_update(ie,
4934 a->worktree->root_fd, relpath,
4936 a->worktree->base_commit_id->sha1, 1);
4940 if (is_bad_symlink) {
4941 got_fileindex_entry_filetype_set(ie,
4942 GOT_FILEIDX_MODE_BAD_SYMLINK);
4955 if (fd != -1 && close(fd) == -1 && err == NULL)
4956 err = got_error_from_errno("close");
4958 got_object_blob_close(blob);
4960 got_object_tree_close(tree);
4963 got_object_commit_close(base_commit);
4967 const struct got_error *
4968 got_worktree_revert(struct got_worktree *worktree,
4969 struct got_pathlist_head *paths,
4970 got_worktree_checkout_cb progress_cb, void *progress_arg,
4971 got_worktree_patch_cb patch_cb, void *patch_arg,
4972 struct got_repository *repo)
4974 struct got_fileindex *fileindex = NULL;
4975 char *fileindex_path = NULL;
4976 const struct got_error *err = NULL, *unlockerr = NULL;
4977 const struct got_error *sync_err = NULL;
4978 struct got_pathlist_entry *pe;
4979 struct revert_file_args rfa;
4981 err = lock_worktree(worktree, LOCK_EX);
4985 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4989 rfa.worktree = worktree;
4990 rfa.fileindex = fileindex;
4991 rfa.progress_cb = progress_cb;
4992 rfa.progress_arg = progress_arg;
4993 rfa.patch_cb = patch_cb;
4994 rfa.patch_arg = patch_arg;
4996 rfa.unlink_added_files = 0;
4997 TAILQ_FOREACH(pe, paths, entry) {
4998 err = worktree_status(worktree, pe->path, fileindex, repo,
4999 revert_file, &rfa, NULL, NULL, 1, 0);
5003 sync_err = sync_fileindex(fileindex, fileindex_path);
5004 if (sync_err && err == NULL)
5007 free(fileindex_path);
5009 got_fileindex_free(fileindex);
5010 unlockerr = lock_worktree(worktree, LOCK_SH);
5011 if (unlockerr && err == NULL)
5017 free_commitable(struct got_commitable *ct)
5020 free(ct->in_repo_path);
5021 free(ct->ondisk_path);
5023 free(ct->base_blob_id);
5024 free(ct->staged_blob_id);
5025 free(ct->base_commit_id);
5029 struct collect_commitables_arg {
5030 struct got_pathlist_head *commitable_paths;
5031 struct got_repository *repo;
5032 struct got_worktree *worktree;
5033 struct got_fileindex *fileindex;
5034 int have_staged_files;
5035 int allow_bad_symlinks;
5036 int diff_header_shown;
5037 int commit_conflicts;
5044 * Create a file which contains the target path of a symlink so we can feed
5045 * it as content to the diff engine.
5047 static const struct got_error *
5048 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5049 const char *abspath)
5051 const struct got_error *err = NULL;
5052 char target_path[PATH_MAX];
5053 ssize_t target_len, outlen;
5058 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5059 if (target_len == -1)
5060 return got_error_from_errno2("readlinkat", abspath);
5062 target_len = readlink(abspath, target_path, PATH_MAX);
5063 if (target_len == -1)
5064 return got_error_from_errno2("readlink", abspath);
5067 *fd = got_opentempfd();
5069 return got_error_from_errno("got_opentempfd");
5071 outlen = write(*fd, target_path, target_len);
5073 err = got_error_from_errno("got_opentempfd");
5077 if (lseek(*fd, 0, SEEK_SET) == -1) {
5078 err = got_error_from_errno2("lseek", abspath);
5089 static const struct got_error *
5090 append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5091 FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5092 int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5094 const struct got_error *err = NULL;
5095 struct got_blob_object *blob1 = NULL;
5096 int fd = -1, fd1 = -1, fd2 = -1;
5097 FILE *ondisk_file = NULL;
5098 char *label1 = NULL;
5102 char *id_str = NULL;
5104 memset(&sb, 0, sizeof(sb));
5107 if (ct->staged_status != GOT_STATUS_MODIFY &&
5108 ct->staged_status != GOT_STATUS_ADD &&
5109 ct->staged_status != GOT_STATUS_DELETE)
5112 if (ct->status != GOT_STATUS_MODIFY &&
5113 ct->status != GOT_STATUS_ADD &&
5114 ct->status != GOT_STATUS_DELETE &&
5115 ct->status != GOT_STATUS_CONFLICT)
5119 err = got_opentemp_truncate(f1);
5121 return got_error_from_errno("got_opentemp_truncate");
5122 err = got_opentemp_truncate(f2);
5124 return got_error_from_errno("got_opentemp_truncate");
5126 if (!*diff_header_shown) {
5127 err = got_object_id_str(&id_str, worktree->base_commit_id);
5130 fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5131 got_worktree_get_root_path(worktree));
5132 fprintf(diff_outfile, "commit - %s\n", id_str);
5133 fprintf(diff_outfile, "path + %s%s\n",
5134 got_worktree_get_root_path(worktree),
5135 diff_staged ? " (staged changes)" : "");
5136 *diff_header_shown = 1;
5140 const char *label1 = NULL, *label2 = NULL;
5141 switch (ct->staged_status) {
5142 case GOT_STATUS_MODIFY:
5146 case GOT_STATUS_ADD:
5149 case GOT_STATUS_DELETE:
5153 return got_error(GOT_ERR_FILE_STATUS);
5155 fd1 = got_opentempfd();
5157 err = got_error_from_errno("got_opentempfd");
5160 fd2 = got_opentempfd();
5162 err = got_error_from_errno("got_opentempfd");
5165 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5166 fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5167 label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5168 NULL, repo, diff_outfile);
5172 fd1 = got_opentempfd();
5174 err = got_error_from_errno("got_opentempfd");
5178 if (ct->status != GOT_STATUS_ADD) {
5179 err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5185 if (ct->status != GOT_STATUS_DELETE) {
5187 fd = openat(dirfd, de_name,
5188 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5190 if (!got_err_open_nofollow_on_symlink()) {
5191 err = got_error_from_errno2("openat",
5195 err = get_symlink_target_file(&fd, dirfd,
5196 de_name, ct->ondisk_path);
5201 fd = open(ct->ondisk_path,
5202 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5204 if (!got_err_open_nofollow_on_symlink()) {
5205 err = got_error_from_errno2("open",
5209 err = get_symlink_target_file(&fd, dirfd,
5210 de_name, ct->ondisk_path);
5215 if (fstatat(fd, ct->ondisk_path, &sb,
5216 AT_SYMLINK_NOFOLLOW) == -1) {
5217 err = got_error_from_errno2("fstatat", ct->ondisk_path);
5220 ondisk_file = fdopen(fd, "r");
5221 if (ondisk_file == NULL) {
5222 err = got_error_from_errno2("fdopen", ct->ondisk_path);
5230 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5236 err = got_diff_blob_file(blob1, f1, size1, label1,
5237 ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5238 GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5240 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5241 err = got_error_from_errno("close");
5242 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5243 err = got_error_from_errno("close");
5245 got_object_blob_close(blob1);
5246 if (fd != -1 && close(fd) == -1 && err == NULL)
5247 err = got_error_from_errno("close");
5248 if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5249 err = got_error_from_errno("fclose");
5253 static const struct got_error *
5254 collect_commitables(void *arg, unsigned char status,
5255 unsigned char staged_status, const char *relpath,
5256 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5257 struct got_object_id *commit_id, int dirfd, const char *de_name)
5259 struct collect_commitables_arg *a = arg;
5260 const struct got_error *err = NULL;
5261 struct got_commitable *ct = NULL;
5262 struct got_pathlist_entry *new = NULL;
5263 char *parent_path = NULL, *path = NULL;
5266 if (a->have_staged_files) {
5267 if (staged_status != GOT_STATUS_MODIFY &&
5268 staged_status != GOT_STATUS_ADD &&
5269 staged_status != GOT_STATUS_DELETE)
5272 if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5273 printf("C %s\n", relpath);
5274 return got_error(GOT_ERR_COMMIT_CONFLICT);
5277 if (status != GOT_STATUS_MODIFY &&
5278 status != GOT_STATUS_MODE_CHANGE &&
5279 status != GOT_STATUS_ADD &&
5280 status != GOT_STATUS_DELETE &&
5281 status != GOT_STATUS_CONFLICT)
5285 if (asprintf(&path, "/%s", relpath) == -1) {
5286 err = got_error_from_errno("asprintf");
5289 if (strcmp(path, "/") == 0) {
5290 parent_path = strdup("");
5291 if (parent_path == NULL)
5292 return got_error_from_errno("strdup");
5294 err = got_path_dirname(&parent_path, path);
5299 ct = calloc(1, sizeof(*ct));
5301 err = got_error_from_errno("calloc");
5305 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5307 err = got_error_from_errno("asprintf");
5311 if (staged_status == GOT_STATUS_ADD ||
5312 staged_status == GOT_STATUS_MODIFY) {
5313 struct got_fileindex_entry *ie;
5314 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5315 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5316 case GOT_FILEIDX_MODE_REGULAR_FILE:
5317 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5320 case GOT_FILEIDX_MODE_SYMLINK:
5324 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5327 ct->mode |= got_fileindex_entry_perms_get(ie);
5328 } else if (status != GOT_STATUS_DELETE &&
5329 staged_status != GOT_STATUS_DELETE) {
5331 if (fstatat(dirfd, de_name, &sb,
5332 AT_SYMLINK_NOFOLLOW) == -1) {
5333 err = got_error_from_errno2("fstatat",
5337 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5338 err = got_error_from_errno2("lstat", ct->ondisk_path);
5341 ct->mode = sb.st_mode;
5344 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5345 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5347 err = got_error_from_errno("asprintf");
5351 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5352 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5354 char target_path[PATH_MAX];
5356 target_len = readlink(ct->ondisk_path, target_path,
5357 sizeof(target_path));
5358 if (target_len == -1) {
5359 err = got_error_from_errno2("readlink",
5363 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5364 target_len, ct->ondisk_path, a->worktree->root_path);
5367 if (is_bad_symlink) {
5368 err = got_error_path(ct->ondisk_path,
5369 GOT_ERR_BAD_SYMLINK);
5375 ct->status = status;
5376 ct->staged_status = staged_status;
5377 ct->blob_id = NULL; /* will be filled in when blob gets created */
5378 if (ct->status != GOT_STATUS_ADD &&
5379 ct->staged_status != GOT_STATUS_ADD) {
5380 ct->base_blob_id = got_object_id_dup(blob_id);
5381 if (ct->base_blob_id == NULL) {
5382 err = got_error_from_errno("got_object_id_dup");
5385 ct->base_commit_id = got_object_id_dup(commit_id);
5386 if (ct->base_commit_id == NULL) {
5387 err = got_error_from_errno("got_object_id_dup");
5391 if (ct->staged_status == GOT_STATUS_ADD ||
5392 ct->staged_status == GOT_STATUS_MODIFY) {
5393 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5394 if (ct->staged_blob_id == NULL) {
5395 err = got_error_from_errno("got_object_id_dup");
5399 ct->path = strdup(path);
5400 if (ct->path == NULL) {
5401 err = got_error_from_errno("strdup");
5404 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5408 if (a->diff_outfile && ct && new != NULL) {
5409 err = append_ct_diff(ct, &a->diff_header_shown,
5410 a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5411 a->have_staged_files, a->repo, a->worktree);
5416 if (ct && (err || new == NULL))
5417 free_commitable(ct);
5423 static const struct got_error *write_tree(struct got_object_id **, int *,
5424 struct got_tree_object *, const char *, struct got_pathlist_head *,
5425 got_worktree_status_cb status_cb, void *status_arg,
5426 struct got_repository *);
5428 static const struct got_error *
5429 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5430 struct got_tree_entry *te, const char *parent_path,
5431 struct got_pathlist_head *commitable_paths,
5432 got_worktree_status_cb status_cb, void *status_arg,
5433 struct got_repository *repo)
5435 const struct got_error *err = NULL;
5436 struct got_tree_object *subtree;
5439 if (asprintf(&subpath, "%s%s%s", parent_path,
5440 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5441 return got_error_from_errno("asprintf");
5443 err = got_object_open_as_tree(&subtree, repo, &te->id);
5447 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5448 commitable_paths, status_cb, status_arg, repo);
5449 got_object_tree_close(subtree);
5454 static const struct got_error *
5455 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5457 const struct got_error *err = NULL;
5458 char *ct_parent_path = NULL;
5462 if (strchr(ct->in_repo_path, '/') == NULL) {
5463 *match = got_path_is_root_dir(path);
5467 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5470 *match = (strcmp(path, ct_parent_path) == 0);
5471 free(ct_parent_path);
5476 get_ct_file_mode(struct got_commitable *ct)
5478 if (S_ISLNK(ct->mode))
5481 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5484 static const struct got_error *
5485 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5486 struct got_tree_entry *te, struct got_commitable *ct)
5488 const struct got_error *err = NULL;
5492 err = got_object_tree_entry_dup(new_te, te);
5496 (*new_te)->mode = get_ct_file_mode(ct);
5498 if (ct->staged_status == GOT_STATUS_MODIFY)
5499 memcpy(&(*new_te)->id, ct->staged_blob_id,
5500 sizeof((*new_te)->id));
5502 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5504 if (err && *new_te) {
5511 static const struct got_error *
5512 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5513 struct got_commitable *ct)
5515 const struct got_error *err = NULL;
5516 char *ct_name = NULL;
5520 *new_te = calloc(1, sizeof(**new_te));
5521 if (*new_te == NULL)
5522 return got_error_from_errno("calloc");
5524 err = got_path_basename(&ct_name, ct->path);
5527 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5528 sizeof((*new_te)->name)) {
5529 err = got_error(GOT_ERR_NO_SPACE);
5533 (*new_te)->mode = get_ct_file_mode(ct);
5535 if (ct->staged_status == GOT_STATUS_ADD)
5536 memcpy(&(*new_te)->id, ct->staged_blob_id,
5537 sizeof((*new_te)->id));
5539 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5542 if (err && *new_te) {
5549 static const struct got_error *
5550 insert_tree_entry(struct got_tree_entry *new_te,
5551 struct got_pathlist_head *paths)
5553 const struct got_error *err = NULL;
5554 struct got_pathlist_entry *new_pe;
5556 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5560 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5564 static const struct got_error *
5565 report_ct_status(struct got_commitable *ct,
5566 got_worktree_status_cb status_cb, void *status_arg)
5568 const char *ct_path = ct->path;
5569 unsigned char status;
5571 if (status_cb == NULL) /* no commit progress output desired */
5574 while (ct_path[0] == '/')
5577 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5578 status = ct->staged_status;
5580 status = ct->status;
5582 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5583 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5586 static const struct got_error *
5587 match_modified_subtree(int *modified, struct got_tree_entry *te,
5588 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5590 const struct got_error *err = NULL;
5591 struct got_pathlist_entry *pe;
5596 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5597 got_path_is_root_dir(base_tree_path) ? "" : "/",
5599 return got_error_from_errno("asprintf");
5601 TAILQ_FOREACH(pe, commitable_paths, entry) {
5602 struct got_commitable *ct = pe->data;
5603 *modified = got_path_is_child(ct->in_repo_path, te_path,
5613 static const struct got_error *
5614 match_deleted_or_modified_ct(struct got_commitable **ctp,
5615 struct got_tree_entry *te, const char *base_tree_path,
5616 struct got_pathlist_head *commitable_paths)
5618 const struct got_error *err = NULL;
5619 struct got_pathlist_entry *pe;
5623 TAILQ_FOREACH(pe, commitable_paths, entry) {
5624 struct got_commitable *ct = pe->data;
5625 char *ct_name = NULL;
5628 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5629 if (ct->status != GOT_STATUS_MODIFY &&
5630 ct->status != GOT_STATUS_MODE_CHANGE &&
5631 ct->status != GOT_STATUS_DELETE &&
5632 ct->status != GOT_STATUS_CONFLICT)
5635 if (ct->staged_status != GOT_STATUS_MODIFY &&
5636 ct->staged_status != GOT_STATUS_DELETE)
5640 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5643 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5649 err = got_path_basename(&ct_name, pe->path);
5653 if (strcmp(te->name, ct_name) != 0) {
5666 static const struct got_error *
5667 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5668 const char *child_path, const char *path_base_tree,
5669 struct got_pathlist_head *commitable_paths,
5670 got_worktree_status_cb status_cb, void *status_arg,
5671 struct got_repository *repo)
5673 const struct got_error *err = NULL;
5674 struct got_tree_entry *new_te;
5676 struct got_object_id *id = NULL;
5681 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5682 got_path_is_root_dir(path_base_tree) ? "" : "/",
5684 return got_error_from_errno("asprintf");
5686 new_te = calloc(1, sizeof(*new_te));
5688 return got_error_from_errno("calloc");
5689 new_te->mode = S_IFDIR;
5691 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5692 sizeof(new_te->name)) {
5693 err = got_error(GOT_ERR_NO_SPACE);
5696 err = write_tree(&id, &nentries, NULL, subtree_path,
5697 commitable_paths, status_cb, status_arg, repo);
5702 memcpy(&new_te->id, id, sizeof(new_te->id));
5711 static const struct got_error *
5712 write_tree(struct got_object_id **new_tree_id, int *nentries,
5713 struct got_tree_object *base_tree, const char *path_base_tree,
5714 struct got_pathlist_head *commitable_paths,
5715 got_worktree_status_cb status_cb, void *status_arg,
5716 struct got_repository *repo)
5718 const struct got_error *err = NULL;
5719 struct got_pathlist_head paths;
5720 struct got_tree_entry *te, *new_te = NULL;
5721 struct got_pathlist_entry *pe;
5726 /* Insert, and recurse into, newly added entries first. */
5727 TAILQ_FOREACH(pe, commitable_paths, entry) {
5728 struct got_commitable *ct = pe->data;
5729 char *child_path = NULL, *slash;
5731 if ((ct->status != GOT_STATUS_ADD &&
5732 ct->staged_status != GOT_STATUS_ADD) ||
5733 (ct->flags & GOT_COMMITABLE_ADDED))
5736 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5737 strlen(path_base_tree)))
5740 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5745 slash = strchr(child_path, '/');
5746 if (slash == NULL) {
5747 err = alloc_added_blob_tree_entry(&new_te, ct);
5750 err = report_ct_status(ct, status_cb, status_arg);
5753 ct->flags |= GOT_COMMITABLE_ADDED;
5754 err = insert_tree_entry(new_te, &paths);
5759 *slash = '\0'; /* trim trailing path components */
5760 if (base_tree == NULL ||
5761 got_object_tree_find_entry(base_tree, child_path)
5763 err = make_subtree_for_added_blob(&new_te,
5764 child_path, path_base_tree,
5765 commitable_paths, status_cb, status_arg,
5769 err = insert_tree_entry(new_te, &paths);
5778 int i, nbase_entries;
5779 /* Handle modified and deleted entries. */
5780 nbase_entries = got_object_tree_get_nentries(base_tree);
5781 for (i = 0; i < nbase_entries; i++) {
5782 struct got_commitable *ct = NULL;
5784 te = got_object_tree_get_entry(base_tree, i);
5785 if (got_object_tree_entry_is_submodule(te)) {
5786 /* Entry is a submodule; just copy it. */
5787 err = got_object_tree_entry_dup(&new_te, te);
5790 err = insert_tree_entry(new_te, &paths);
5797 if (S_ISDIR(te->mode)) {
5799 err = got_object_tree_entry_dup(&new_te, te);
5802 err = match_modified_subtree(&modified, te,
5803 path_base_tree, commitable_paths);
5806 /* Avoid recursion into unmodified subtrees. */
5808 struct got_object_id *new_id;
5810 err = write_subtree(&new_id,
5812 path_base_tree, commitable_paths,
5813 status_cb, status_arg, repo);
5816 if (nsubentries == 0) {
5817 /* All entries were deleted. */
5821 memcpy(&new_te->id, new_id,
5822 sizeof(new_te->id));
5825 err = insert_tree_entry(new_te, &paths);
5832 err = match_deleted_or_modified_ct(&ct, te,
5833 path_base_tree, commitable_paths);
5837 /* NB: Deleted entries get dropped here. */
5838 if (ct->status == GOT_STATUS_MODIFY ||
5839 ct->status == GOT_STATUS_MODE_CHANGE ||
5840 ct->status == GOT_STATUS_CONFLICT ||
5841 ct->staged_status == GOT_STATUS_MODIFY) {
5842 err = alloc_modified_blob_tree_entry(
5846 err = insert_tree_entry(new_te, &paths);
5851 err = report_ct_status(ct, status_cb,
5856 /* Entry is unchanged; just copy it. */
5857 err = got_object_tree_entry_dup(&new_te, te);
5860 err = insert_tree_entry(new_te, &paths);
5868 /* Write new list of entries; deleted entries have been dropped. */
5869 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5871 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
5875 static const struct got_error *
5876 update_fileindex_after_commit(struct got_worktree *worktree,
5877 struct got_pathlist_head *commitable_paths,
5878 struct got_object_id *new_base_commit_id,
5879 struct got_fileindex *fileindex, int have_staged_files)
5881 const struct got_error *err = NULL;
5882 struct got_pathlist_entry *pe;
5883 char *relpath = NULL;
5885 TAILQ_FOREACH(pe, commitable_paths, entry) {
5886 struct got_fileindex_entry *ie;
5887 struct got_commitable *ct = pe->data;
5889 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5891 err = got_path_skip_common_ancestor(&relpath,
5892 worktree->root_path, ct->ondisk_path);
5897 if (ct->status == GOT_STATUS_DELETE ||
5898 ct->staged_status == GOT_STATUS_DELETE) {
5899 got_fileindex_entry_remove(fileindex, ie);
5900 } else if (ct->staged_status == GOT_STATUS_ADD ||
5901 ct->staged_status == GOT_STATUS_MODIFY) {
5902 got_fileindex_entry_stage_set(ie,
5903 GOT_FILEIDX_STAGE_NONE);
5904 got_fileindex_entry_staged_filetype_set(ie, 0);
5906 err = got_fileindex_entry_update(ie,
5907 worktree->root_fd, relpath,
5908 ct->staged_blob_id->sha1,
5909 new_base_commit_id->sha1,
5910 !have_staged_files);
5912 err = got_fileindex_entry_update(ie,
5913 worktree->root_fd, relpath,
5915 new_base_commit_id->sha1,
5916 !have_staged_files);
5918 err = got_fileindex_entry_alloc(&ie, pe->path);
5921 err = got_fileindex_entry_update(ie,
5922 worktree->root_fd, relpath, ct->blob_id->sha1,
5923 new_base_commit_id->sha1, 1);
5925 got_fileindex_entry_free(ie);
5928 err = got_fileindex_entry_add(fileindex, ie);
5930 got_fileindex_entry_free(ie);
5943 static const struct got_error *
5944 check_out_of_date(const char *in_repo_path, unsigned char status,
5945 unsigned char staged_status, struct got_object_id *base_blob_id,
5946 struct got_object_id *base_commit_id,
5947 struct got_object_id *head_commit_id, struct got_repository *repo,
5950 const struct got_error *err = NULL;
5951 struct got_commit_object *commit = NULL;
5952 struct got_object_id *id = NULL;
5954 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5955 /* Trivial case: base commit == head commit */
5956 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5959 * Ensure file content which local changes were based
5960 * on matches file content in the branch head.
5962 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5965 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5967 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5968 err = got_error(ood_errcode);
5970 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5971 err = got_error(ood_errcode);
5973 /* Require that added files don't exist in the branch head. */
5974 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5977 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5978 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5980 err = id ? got_error(ood_errcode) : NULL;
5985 got_object_commit_close(commit);
5989 static const struct got_error *
5990 commit_worktree(struct got_object_id **new_commit_id,
5991 struct got_pathlist_head *commitable_paths,
5992 struct got_object_id *head_commit_id,
5993 struct got_object_id *parent_id2,
5994 struct got_worktree *worktree,
5995 const char *author, const char *committer, char *diff_path,
5996 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5997 got_worktree_status_cb status_cb, void *status_arg,
5998 struct got_repository *repo)
6000 const struct got_error *err = NULL, *unlockerr = NULL;
6001 struct got_pathlist_entry *pe;
6002 const char *head_ref_name = NULL;
6003 struct got_commit_object *head_commit = NULL;
6004 struct got_reference *head_ref2 = NULL;
6005 struct got_object_id *head_commit_id2 = NULL;
6006 struct got_tree_object *head_tree = NULL;
6007 struct got_object_id *new_tree_id = NULL;
6008 int nentries, nparents = 0;
6009 struct got_object_id_queue parent_ids;
6010 struct got_object_qid *pid = NULL;
6011 char *logmsg = NULL;
6014 *new_commit_id = NULL;
6016 STAILQ_INIT(&parent_ids);
6018 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6022 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6026 if (commit_msg_cb != NULL) {
6027 err = commit_msg_cb(commitable_paths, diff_path,
6028 &logmsg, commit_arg);
6033 if (logmsg == NULL || strlen(logmsg) == 0) {
6034 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6038 /* Create blobs from added and modified files and record their IDs. */
6039 TAILQ_FOREACH(pe, commitable_paths, entry) {
6040 struct got_commitable *ct = pe->data;
6043 /* Blobs for staged files already exist. */
6044 if (ct->staged_status == GOT_STATUS_ADD ||
6045 ct->staged_status == GOT_STATUS_MODIFY)
6048 if (ct->status != GOT_STATUS_ADD &&
6049 ct->status != GOT_STATUS_MODIFY &&
6050 ct->status != GOT_STATUS_MODE_CHANGE &&
6051 ct->status != GOT_STATUS_CONFLICT)
6054 if (asprintf(&ondisk_path, "%s/%s",
6055 worktree->root_path, pe->path) == -1) {
6056 err = got_error_from_errno("asprintf");
6059 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6065 /* Recursively write new tree objects. */
6066 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6067 commitable_paths, status_cb, status_arg, repo);
6071 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
6074 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6077 err = got_object_qid_alloc(&pid, parent_id2);
6080 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6083 timestamp = time(NULL);
6084 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6085 nparents, author, timestamp, committer, timestamp, logmsg, repo);
6091 /* Check if a concurrent commit to our branch has occurred. */
6092 head_ref_name = got_worktree_get_head_ref_name(worktree);
6093 if (head_ref_name == NULL) {
6094 err = got_error_from_errno("got_worktree_get_head_ref_name");
6097 /* Lock the reference here to prevent concurrent modification. */
6098 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6101 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6104 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6105 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6108 /* Update branch head in repository. */
6109 err = got_ref_change_ref(head_ref2, *new_commit_id);
6112 err = got_ref_write(head_ref2, repo);
6116 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6120 err = ref_base_commit(worktree, repo);
6124 got_object_id_queue_free(&parent_ids);
6126 got_object_tree_close(head_tree);
6128 got_object_commit_close(head_commit);
6129 free(head_commit_id2);
6131 unlockerr = got_ref_unlock(head_ref2);
6132 if (unlockerr && err == NULL)
6134 got_ref_close(head_ref2);
6139 static const struct got_error *
6140 check_path_is_commitable(const char *path,
6141 struct got_pathlist_head *commitable_paths)
6143 struct got_pathlist_entry *cpe = NULL;
6144 size_t path_len = strlen(path);
6146 TAILQ_FOREACH(cpe, commitable_paths, entry) {
6147 struct got_commitable *ct = cpe->data;
6148 const char *ct_path = ct->path;
6150 while (ct_path[0] == '/')
6153 if (strcmp(path, ct_path) == 0 ||
6154 got_path_is_child(ct_path, path, path_len))
6159 return got_error_path(path, GOT_ERR_BAD_PATH);
6164 static const struct got_error *
6165 check_staged_file(void *arg, struct got_fileindex_entry *ie)
6167 int *have_staged_files = arg;
6169 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6170 *have_staged_files = 1;
6171 return got_error(GOT_ERR_CANCELLED);
6177 static const struct got_error *
6178 check_non_staged_files(struct got_fileindex *fileindex,
6179 struct got_pathlist_head *paths)
6181 struct got_pathlist_entry *pe;
6182 struct got_fileindex_entry *ie;
6184 TAILQ_FOREACH(pe, paths, entry) {
6185 if (pe->path[0] == '\0')
6187 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6189 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6190 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6191 return got_error_path(pe->path,
6192 GOT_ERR_FILE_NOT_STAGED);
6198 const struct got_error *
6199 got_worktree_commit(struct got_object_id **new_commit_id,
6200 struct got_worktree *worktree, struct got_pathlist_head *paths,
6201 const char *author, const char *committer, int allow_bad_symlinks,
6202 int show_diff, int commit_conflicts,
6203 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6204 got_worktree_status_cb status_cb, void *status_arg,
6205 struct got_repository *repo)
6207 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6208 struct got_fileindex *fileindex = NULL;
6209 char *fileindex_path = NULL;
6210 struct got_pathlist_head commitable_paths;
6211 struct collect_commitables_arg cc_arg;
6212 struct got_pathlist_entry *pe;
6213 struct got_reference *head_ref = NULL;
6214 struct got_object_id *head_commit_id = NULL;
6215 char *diff_path = NULL;
6216 int have_staged_files = 0;
6218 *new_commit_id = NULL;
6220 memset(&cc_arg, 0, sizeof(cc_arg));
6221 TAILQ_INIT(&commitable_paths);
6223 err = lock_worktree(worktree, LOCK_EX);
6227 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6231 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6235 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6239 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6240 &have_staged_files);
6241 if (err && err->code != GOT_ERR_CANCELLED)
6243 if (have_staged_files) {
6244 err = check_non_staged_files(fileindex, paths);
6249 cc_arg.commitable_paths = &commitable_paths;
6250 cc_arg.worktree = worktree;
6251 cc_arg.fileindex = fileindex;
6253 cc_arg.have_staged_files = have_staged_files;
6254 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6255 cc_arg.diff_header_shown = 0;
6256 cc_arg.commit_conflicts = commit_conflicts;
6258 err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6259 GOT_TMPDIR_STR "/got", ".diff");
6262 cc_arg.f1 = got_opentemp();
6263 if (cc_arg.f1 == NULL) {
6264 err = got_error_from_errno("got_opentemp");
6267 cc_arg.f2 = got_opentemp();
6268 if (cc_arg.f2 == NULL) {
6269 err = got_error_from_errno("got_opentemp");
6274 TAILQ_FOREACH(pe, paths, entry) {
6275 err = worktree_status(worktree, pe->path, fileindex, repo,
6276 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6282 if (fflush(cc_arg.diff_outfile) == EOF) {
6283 err = got_error_from_errno("fflush");
6288 if (TAILQ_EMPTY(&commitable_paths)) {
6289 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6293 TAILQ_FOREACH(pe, paths, entry) {
6294 err = check_path_is_commitable(pe->path, &commitable_paths);
6299 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6300 struct got_commitable *ct = pe->data;
6301 const char *ct_path = ct->in_repo_path;
6303 while (ct_path[0] == '/')
6305 err = check_out_of_date(ct_path, ct->status,
6306 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6307 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6313 err = commit_worktree(new_commit_id, &commitable_paths,
6314 head_commit_id, NULL, worktree, author, committer,
6315 (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6316 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6320 err = update_fileindex_after_commit(worktree, &commitable_paths,
6321 *new_commit_id, fileindex, have_staged_files);
6322 sync_err = sync_fileindex(fileindex, fileindex_path);
6323 if (sync_err && err == NULL)
6327 got_fileindex_free(fileindex);
6328 free(fileindex_path);
6329 unlockerr = lock_worktree(worktree, LOCK_SH);
6330 if (unlockerr && err == NULL)
6332 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6333 struct got_commitable *ct = pe->data;
6335 free_commitable(ct);
6337 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6338 if (diff_path && unlink(diff_path) == -1 && err == NULL)
6339 err = got_error_from_errno2("unlink", diff_path);
6341 if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6343 err = got_error_from_errno("fclose");
6348 got_commitable_get_path(struct got_commitable *ct)
6354 got_commitable_get_status(struct got_commitable *ct)
6359 struct check_rebase_ok_arg {
6360 struct got_worktree *worktree;
6361 struct got_repository *repo;
6364 static const struct got_error *
6365 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6367 const struct got_error *err = NULL;
6368 struct check_rebase_ok_arg *a = arg;
6369 unsigned char status;
6373 /* Reject rebase of a work tree with mixed base commits. */
6374 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6375 SHA1_DIGEST_LENGTH))
6376 return got_error(GOT_ERR_MIXED_COMMITS);
6378 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6380 return got_error_from_errno("asprintf");
6382 /* Reject rebase of a work tree with modified or staged files. */
6383 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6388 if (status != GOT_STATUS_NO_CHANGE)
6389 return got_error(GOT_ERR_MODIFIED);
6390 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6391 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6396 const struct got_error *
6397 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6398 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6399 struct got_worktree *worktree, struct got_reference *branch,
6400 struct got_repository *repo)
6402 const struct got_error *err = NULL;
6403 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6404 char *branch_ref_name = NULL;
6405 char *fileindex_path = NULL;
6406 struct check_rebase_ok_arg ok_arg;
6407 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6408 struct got_object_id *wt_branch_tip = NULL;
6410 *new_base_branch_ref = NULL;
6414 err = lock_worktree(worktree, LOCK_EX);
6418 err = open_fileindex(fileindex, &fileindex_path, worktree);
6422 ok_arg.worktree = worktree;
6424 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6429 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6433 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6437 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6441 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6446 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6449 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6450 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6454 err = got_ref_alloc_symref(new_base_branch_ref,
6455 new_base_branch_ref_name, wt_branch);
6458 err = got_ref_write(*new_base_branch_ref, repo);
6462 /* TODO Lock original branch's ref while rebasing? */
6464 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6468 err = got_ref_write(branch_ref, repo);
6472 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6473 worktree->base_commit_id);
6476 err = got_ref_write(*tmp_branch, repo);
6480 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6484 free(fileindex_path);
6485 free(tmp_branch_name);
6486 free(new_base_branch_ref_name);
6487 free(branch_ref_name);
6489 got_ref_close(branch_ref);
6491 got_ref_close(wt_branch);
6492 free(wt_branch_tip);
6494 if (*new_base_branch_ref) {
6495 got_ref_close(*new_base_branch_ref);
6496 *new_base_branch_ref = NULL;
6499 got_ref_close(*tmp_branch);
6503 got_fileindex_free(*fileindex);
6506 lock_worktree(worktree, LOCK_SH);
6511 const struct got_error *
6512 got_worktree_rebase_continue(struct got_object_id **commit_id,
6513 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6514 struct got_reference **branch, struct got_fileindex **fileindex,
6515 struct got_worktree *worktree, struct got_repository *repo)
6517 const struct got_error *err;
6518 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6519 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6520 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6521 char *fileindex_path = NULL;
6522 int have_staged_files = 0;
6525 *new_base_branch = NULL;
6530 err = lock_worktree(worktree, LOCK_EX);
6534 err = open_fileindex(fileindex, &fileindex_path, worktree);
6538 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6539 &have_staged_files);
6540 if (err && err->code != GOT_ERR_CANCELLED)
6542 if (have_staged_files) {
6543 err = got_error(GOT_ERR_STAGED_PATHS);
6547 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6551 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6555 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6559 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6563 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6567 err = got_ref_open(branch, repo,
6568 got_ref_get_symref_target(branch_ref), 0);
6572 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6576 err = got_ref_resolve(commit_id, repo, commit_ref);
6580 err = got_ref_open(new_base_branch, repo,
6581 new_base_branch_ref_name, 0);
6585 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6589 free(commit_ref_name);
6590 free(branch_ref_name);
6591 free(fileindex_path);
6593 got_ref_close(commit_ref);
6595 got_ref_close(branch_ref);
6600 got_ref_close(*tmp_branch);
6603 if (*new_base_branch) {
6604 got_ref_close(*new_base_branch);
6605 *new_base_branch = NULL;
6608 got_ref_close(*branch);
6612 got_fileindex_free(*fileindex);
6615 lock_worktree(worktree, LOCK_SH);
6620 const struct got_error *
6621 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6623 const struct got_error *err;
6624 char *tmp_branch_name = NULL;
6626 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6630 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6631 free(tmp_branch_name);
6635 static const struct got_error *
6636 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6637 const char *diff_path, char **logmsg, void *arg)
6643 static const struct got_error *
6644 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6645 const char *path, struct got_object_id *blob_id,
6646 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6647 int dirfd, const char *de_name)
6652 struct collect_merged_paths_arg {
6653 got_worktree_checkout_cb progress_cb;
6655 struct got_pathlist_head *merged_paths;
6658 static const struct got_error *
6659 collect_merged_paths(void *arg, unsigned char status, const char *path)
6661 const struct got_error *err;
6662 struct collect_merged_paths_arg *a = arg;
6664 struct got_pathlist_entry *new;
6666 err = (*a->progress_cb)(a->progress_arg, status, path);
6670 if (status != GOT_STATUS_MERGE &&
6671 status != GOT_STATUS_ADD &&
6672 status != GOT_STATUS_DELETE &&
6673 status != GOT_STATUS_CONFLICT)
6678 return got_error_from_errno("strdup");
6680 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6681 if (err || new == NULL)
6686 static const struct got_error *
6687 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6688 int is_rebase, struct got_repository *repo)
6690 const struct got_error *err;
6691 struct got_reference *commit_ref = NULL;
6693 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6695 if (err->code != GOT_ERR_NOT_REF)
6697 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6700 err = got_ref_write(commit_ref, repo);
6703 } else if (is_rebase) {
6704 struct got_object_id *stored_id;
6707 err = got_ref_resolve(&stored_id, repo, commit_ref);
6710 cmp = got_object_id_cmp(commit_id, stored_id);
6713 err = got_error(GOT_ERR_REBASE_COMMITID);
6719 got_ref_close(commit_ref);
6723 static const struct got_error *
6724 rebase_merge_files(struct got_pathlist_head *merged_paths,
6725 const char *commit_ref_name, struct got_worktree *worktree,
6726 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6727 struct got_object_id *commit_id, struct got_repository *repo,
6728 got_worktree_checkout_cb progress_cb, void *progress_arg,
6729 got_cancel_cb cancel_cb, void *cancel_arg)
6731 const struct got_error *err;
6732 struct got_reference *commit_ref = NULL;
6733 struct collect_merged_paths_arg cmp_arg;
6734 char *fileindex_path;
6736 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6738 err = get_fileindex_path(&fileindex_path, worktree);
6742 cmp_arg.progress_cb = progress_cb;
6743 cmp_arg.progress_arg = progress_arg;
6744 cmp_arg.merged_paths = merged_paths;
6745 err = merge_files(worktree, fileindex, fileindex_path,
6746 parent_commit_id, commit_id, repo, collect_merged_paths,
6747 &cmp_arg, cancel_cb, cancel_arg);
6749 got_ref_close(commit_ref);
6753 const struct got_error *
6754 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6755 struct got_worktree *worktree, struct got_fileindex *fileindex,
6756 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6757 struct got_repository *repo,
6758 got_worktree_checkout_cb progress_cb, void *progress_arg,
6759 got_cancel_cb cancel_cb, void *cancel_arg)
6761 const struct got_error *err;
6762 char *commit_ref_name;
6764 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6768 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6772 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6773 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6774 progress_arg, cancel_cb, cancel_arg);
6776 free(commit_ref_name);
6780 const struct got_error *
6781 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6782 struct got_worktree *worktree, struct got_fileindex *fileindex,
6783 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6784 struct got_repository *repo,
6785 got_worktree_checkout_cb progress_cb, void *progress_arg,
6786 got_cancel_cb cancel_cb, void *cancel_arg)
6788 const struct got_error *err;
6789 char *commit_ref_name;
6791 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6795 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6799 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6800 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6801 progress_arg, cancel_cb, cancel_arg);
6803 free(commit_ref_name);
6807 static const struct got_error *
6808 rebase_commit(struct got_object_id **new_commit_id,
6809 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6810 struct got_worktree *worktree, struct got_fileindex *fileindex,
6811 struct got_reference *tmp_branch, const char *committer,
6812 struct got_commit_object *orig_commit, const char *new_logmsg,
6813 int allow_conflict, struct got_repository *repo)
6815 const struct got_error *err, *sync_err;
6816 struct got_pathlist_head commitable_paths;
6817 struct collect_commitables_arg cc_arg;
6818 char *fileindex_path = NULL;
6819 struct got_reference *head_ref = NULL;
6820 struct got_object_id *head_commit_id = NULL;
6821 char *logmsg = NULL;
6823 memset(&cc_arg, 0, sizeof(cc_arg));
6824 TAILQ_INIT(&commitable_paths);
6825 *new_commit_id = NULL;
6827 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6829 err = get_fileindex_path(&fileindex_path, worktree);
6833 cc_arg.commitable_paths = &commitable_paths;
6834 cc_arg.worktree = worktree;
6836 cc_arg.have_staged_files = 0;
6837 cc_arg.commit_conflicts = allow_conflict;
6839 * If possible get the status of individual files directly to
6840 * avoid crawling the entire work tree once per rebased commit.
6842 * Ideally, merged_paths would contain a list of commitables
6843 * we could use so we could skip worktree_status() entirely.
6844 * However, we would then need carefully keep track of cumulative
6845 * effects of operations such as file additions and deletions
6846 * in 'got histedit -f' (folding multiple commits into one),
6847 * and this extra complexity is not really worth it.
6850 struct got_pathlist_entry *pe;
6851 TAILQ_FOREACH(pe, merged_paths, entry) {
6852 err = worktree_status(worktree, pe->path, fileindex,
6853 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6859 err = worktree_status(worktree, "", fileindex, repo,
6860 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6865 if (TAILQ_EMPTY(&commitable_paths)) {
6866 /* No-op change; commit will be elided. */
6867 err = got_ref_delete(commit_ref, repo);
6870 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6874 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6878 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6883 logmsg = strdup(new_logmsg);
6884 if (logmsg == NULL) {
6885 err = got_error_from_errno("strdup");
6889 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6894 /* NB: commit_worktree will call free(logmsg) */
6895 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6896 NULL, worktree, got_object_commit_get_author(orig_commit),
6897 committer ? committer :
6898 got_object_commit_get_committer(orig_commit), NULL,
6899 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6903 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6907 err = got_ref_delete(commit_ref, repo);
6911 err = update_fileindex_after_commit(worktree, &commitable_paths,
6912 *new_commit_id, fileindex, 0);
6913 sync_err = sync_fileindex(fileindex, fileindex_path);
6914 if (sync_err && err == NULL)
6917 free(fileindex_path);
6918 free(head_commit_id);
6920 got_ref_close(head_ref);
6922 free(*new_commit_id);
6923 *new_commit_id = NULL;
6928 const struct got_error *
6929 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6930 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6931 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6932 const char *committer, struct got_commit_object *orig_commit,
6933 struct got_object_id *orig_commit_id, int allow_conflict,
6934 struct got_repository *repo)
6936 const struct got_error *err;
6937 char *commit_ref_name;
6938 struct got_reference *commit_ref = NULL;
6939 struct got_object_id *commit_id = NULL;
6941 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6945 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6948 err = got_ref_resolve(&commit_id, repo, commit_ref);
6951 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6952 err = got_error(GOT_ERR_REBASE_COMMITID);
6956 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6957 worktree, fileindex, tmp_branch, committer, orig_commit,
6958 NULL, allow_conflict, repo);
6961 got_ref_close(commit_ref);
6962 free(commit_ref_name);
6967 const struct got_error *
6968 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6969 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6970 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6971 const char *committer, struct got_commit_object *orig_commit,
6972 struct got_object_id *orig_commit_id, const char *new_logmsg,
6973 int allow_conflict, struct got_repository *repo)
6975 const struct got_error *err;
6976 char *commit_ref_name;
6977 struct got_reference *commit_ref = NULL;
6979 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6983 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6987 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6988 worktree, fileindex, tmp_branch, committer, orig_commit,
6989 new_logmsg, allow_conflict, repo);
6992 got_ref_close(commit_ref);
6993 free(commit_ref_name);
6997 const struct got_error *
6998 got_worktree_rebase_postpone(struct got_worktree *worktree,
6999 struct got_fileindex *fileindex)
7002 got_fileindex_free(fileindex);
7003 return lock_worktree(worktree, LOCK_SH);
7006 static const struct got_error *
7007 delete_ref(const char *name, struct got_repository *repo)
7009 const struct got_error *err;
7010 struct got_reference *ref;
7012 err = got_ref_open(&ref, repo, name, 0);
7014 if (err->code == GOT_ERR_NOT_REF)
7019 err = got_ref_delete(ref, repo);
7024 static const struct got_error *
7025 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7027 const struct got_error *err;
7028 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7029 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7031 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7034 err = delete_ref(tmp_branch_name, repo);
7038 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7041 err = delete_ref(new_base_branch_ref_name, repo);
7045 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7048 err = delete_ref(branch_ref_name, repo);
7052 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7055 err = delete_ref(commit_ref_name, repo);
7060 free(tmp_branch_name);
7061 free(new_base_branch_ref_name);
7062 free(branch_ref_name);
7063 free(commit_ref_name);
7067 static const struct got_error *
7068 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7069 struct got_object_id *new_commit_id, struct got_repository *repo)
7071 const struct got_error *err;
7072 struct got_reference *ref = NULL;
7073 struct got_object_id *old_commit_id = NULL;
7074 const char *branch_name = NULL;
7075 char *new_id_str = NULL;
7076 char *refname = NULL;
7078 branch_name = got_ref_get_name(branch);
7079 if (strncmp(branch_name, "refs/heads/", 11) != 0)
7080 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7083 err = got_object_id_str(&new_id_str, new_commit_id);
7087 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7088 new_id_str) == -1) {
7089 err = got_error_from_errno("asprintf");
7093 err = got_ref_resolve(&old_commit_id, repo, branch);
7097 err = got_ref_alloc(&ref, refname, old_commit_id);
7101 err = got_ref_write(ref, repo);
7105 free(old_commit_id);
7111 const struct got_error *
7112 got_worktree_rebase_complete(struct got_worktree *worktree,
7113 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7114 struct got_reference *rebased_branch, struct got_repository *repo,
7117 const struct got_error *err, *unlockerr, *sync_err;
7118 struct got_object_id *new_head_commit_id = NULL;
7119 char *fileindex_path = NULL;
7121 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7125 if (create_backup) {
7126 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7127 rebased_branch, new_head_commit_id, repo);
7132 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7136 err = got_ref_write(rebased_branch, repo);
7140 err = got_worktree_set_head_ref(worktree, rebased_branch);
7144 err = delete_rebase_refs(worktree, repo);
7148 err = get_fileindex_path(&fileindex_path, worktree);
7151 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7152 sync_err = sync_fileindex(fileindex, fileindex_path);
7153 if (sync_err && err == NULL)
7156 got_fileindex_free(fileindex);
7157 free(fileindex_path);
7158 free(new_head_commit_id);
7159 unlockerr = lock_worktree(worktree, LOCK_SH);
7160 if (unlockerr && err == NULL)
7165 const struct got_error *
7166 got_worktree_rebase_abort(struct got_worktree *worktree,
7167 struct got_fileindex *fileindex, struct got_repository *repo,
7168 struct got_reference *new_base_branch,
7169 got_worktree_checkout_cb progress_cb, void *progress_arg)
7171 const struct got_error *err, *unlockerr, *sync_err;
7172 struct got_reference *resolved = NULL;
7173 struct got_object_id *commit_id = NULL;
7174 struct got_commit_object *commit = NULL;
7175 char *fileindex_path = NULL;
7176 struct revert_file_args rfa;
7177 struct got_object_id *tree_id = NULL;
7179 err = lock_worktree(worktree, LOCK_EX);
7183 err = got_object_open_as_commit(&commit, repo,
7184 worktree->base_commit_id);
7188 err = got_ref_open(&resolved, repo,
7189 got_ref_get_symref_target(new_base_branch), 0);
7193 err = got_worktree_set_head_ref(worktree, resolved);
7198 * XXX commits to the base branch could have happened while
7199 * we were busy rebasing; should we store the original commit ID
7200 * when rebase begins and read it back here?
7202 err = got_ref_resolve(&commit_id, repo, resolved);
7206 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7210 err = got_object_id_by_path(&tree_id, repo, commit,
7211 worktree->path_prefix);
7215 err = delete_rebase_refs(worktree, repo);
7219 err = get_fileindex_path(&fileindex_path, worktree);
7223 rfa.worktree = worktree;
7224 rfa.fileindex = fileindex;
7225 rfa.progress_cb = progress_cb;
7226 rfa.progress_arg = progress_arg;
7227 rfa.patch_cb = NULL;
7228 rfa.patch_arg = NULL;
7230 rfa.unlink_added_files = 0;
7231 err = worktree_status(worktree, "", fileindex, repo,
7232 revert_file, &rfa, NULL, NULL, 1, 0);
7236 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7237 repo, progress_cb, progress_arg, NULL, NULL);
7239 sync_err = sync_fileindex(fileindex, fileindex_path);
7240 if (sync_err && err == NULL)
7243 got_ref_close(resolved);
7247 got_object_commit_close(commit);
7249 got_fileindex_free(fileindex);
7250 free(fileindex_path);
7252 unlockerr = lock_worktree(worktree, LOCK_SH);
7253 if (unlockerr && err == NULL)
7258 const struct got_error *
7259 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7260 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7261 struct got_fileindex **fileindex, struct got_worktree *worktree,
7262 struct got_repository *repo)
7264 const struct got_error *err = NULL;
7265 char *tmp_branch_name = NULL;
7266 char *branch_ref_name = NULL;
7267 char *base_commit_ref_name = NULL;
7268 char *fileindex_path = NULL;
7269 struct check_rebase_ok_arg ok_arg;
7270 struct got_reference *wt_branch = NULL;
7271 struct got_reference *base_commit_ref = NULL;
7275 *base_commit_id = NULL;
7278 err = lock_worktree(worktree, LOCK_EX);
7282 err = open_fileindex(fileindex, &fileindex_path, worktree);
7286 ok_arg.worktree = worktree;
7288 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7293 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7297 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7301 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7306 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7311 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7315 err = got_ref_write(*branch_ref, repo);
7319 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7320 worktree->base_commit_id);
7323 err = got_ref_write(base_commit_ref, repo);
7326 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7327 if (*base_commit_id == NULL) {
7328 err = got_error_from_errno("got_object_id_dup");
7332 err = got_ref_alloc(tmp_branch, tmp_branch_name,
7333 worktree->base_commit_id);
7336 err = got_ref_write(*tmp_branch, repo);
7340 err = got_worktree_set_head_ref(worktree, *tmp_branch);
7344 free(fileindex_path);
7345 free(tmp_branch_name);
7346 free(branch_ref_name);
7347 free(base_commit_ref_name);
7349 got_ref_close(wt_branch);
7352 got_ref_close(*branch_ref);
7356 got_ref_close(*tmp_branch);
7359 free(*base_commit_id);
7361 got_fileindex_free(*fileindex);
7364 lock_worktree(worktree, LOCK_SH);
7369 const struct got_error *
7370 got_worktree_histedit_postpone(struct got_worktree *worktree,
7371 struct got_fileindex *fileindex)
7374 got_fileindex_free(fileindex);
7375 return lock_worktree(worktree, LOCK_SH);
7378 const struct got_error *
7379 got_worktree_histedit_in_progress(int *in_progress,
7380 struct got_worktree *worktree)
7382 const struct got_error *err;
7383 char *tmp_branch_name = NULL;
7385 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7389 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7390 free(tmp_branch_name);
7394 const struct got_error *
7395 got_worktree_histedit_continue(struct got_object_id **commit_id,
7396 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7397 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7398 struct got_worktree *worktree, struct got_repository *repo)
7400 const struct got_error *err;
7401 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7402 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7403 struct got_reference *commit_ref = NULL;
7404 struct got_reference *base_commit_ref = NULL;
7405 char *fileindex_path = NULL;
7406 int have_staged_files = 0;
7410 *base_commit_id = NULL;
7413 err = lock_worktree(worktree, LOCK_EX);
7417 err = open_fileindex(fileindex, &fileindex_path, worktree);
7421 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7422 &have_staged_files);
7423 if (err && err->code != GOT_ERR_CANCELLED)
7425 if (have_staged_files) {
7426 err = got_error(GOT_ERR_STAGED_PATHS);
7430 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7434 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7438 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7442 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7447 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7451 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7454 err = got_ref_resolve(commit_id, repo, commit_ref);
7458 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7461 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7465 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7469 free(commit_ref_name);
7470 free(branch_ref_name);
7471 free(fileindex_path);
7473 got_ref_close(commit_ref);
7474 if (base_commit_ref)
7475 got_ref_close(base_commit_ref);
7479 free(*base_commit_id);
7480 *base_commit_id = NULL;
7482 got_ref_close(*tmp_branch);
7486 got_fileindex_free(*fileindex);
7489 lock_worktree(worktree, LOCK_EX);
7494 static const struct got_error *
7495 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7497 const struct got_error *err;
7498 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7499 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7501 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7504 err = delete_ref(tmp_branch_name, repo);
7508 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7512 err = delete_ref(base_commit_ref_name, repo);
7516 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7519 err = delete_ref(branch_ref_name, repo);
7523 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7526 err = delete_ref(commit_ref_name, repo);
7530 free(tmp_branch_name);
7531 free(base_commit_ref_name);
7532 free(branch_ref_name);
7533 free(commit_ref_name);
7537 const struct got_error *
7538 got_worktree_histedit_abort(struct got_worktree *worktree,
7539 struct got_fileindex *fileindex, struct got_repository *repo,
7540 struct got_reference *branch, struct got_object_id *base_commit_id,
7541 got_worktree_checkout_cb progress_cb, void *progress_arg)
7543 const struct got_error *err, *unlockerr, *sync_err;
7544 struct got_reference *resolved = NULL;
7545 char *fileindex_path = NULL;
7546 struct got_commit_object *commit = NULL;
7547 struct got_object_id *tree_id = NULL;
7548 struct revert_file_args rfa;
7550 err = lock_worktree(worktree, LOCK_EX);
7554 err = got_object_open_as_commit(&commit, repo,
7555 worktree->base_commit_id);
7559 err = got_ref_open(&resolved, repo,
7560 got_ref_get_symref_target(branch), 0);
7564 err = got_worktree_set_head_ref(worktree, resolved);
7568 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7572 err = got_object_id_by_path(&tree_id, repo, commit,
7573 worktree->path_prefix);
7577 err = delete_histedit_refs(worktree, repo);
7581 err = get_fileindex_path(&fileindex_path, worktree);
7585 rfa.worktree = worktree;
7586 rfa.fileindex = fileindex;
7587 rfa.progress_cb = progress_cb;
7588 rfa.progress_arg = progress_arg;
7589 rfa.patch_cb = NULL;
7590 rfa.patch_arg = NULL;
7592 rfa.unlink_added_files = 0;
7593 err = worktree_status(worktree, "", fileindex, repo,
7594 revert_file, &rfa, NULL, NULL, 1, 0);
7598 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7599 repo, progress_cb, progress_arg, NULL, NULL);
7601 sync_err = sync_fileindex(fileindex, fileindex_path);
7602 if (sync_err && err == NULL)
7605 got_ref_close(resolved);
7607 free(fileindex_path);
7609 unlockerr = lock_worktree(worktree, LOCK_SH);
7610 if (unlockerr && err == NULL)
7615 const struct got_error *
7616 got_worktree_histedit_complete(struct got_worktree *worktree,
7617 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7618 struct got_reference *edited_branch, struct got_repository *repo)
7620 const struct got_error *err, *unlockerr, *sync_err;
7621 struct got_object_id *new_head_commit_id = NULL;
7622 struct got_reference *resolved = NULL;
7623 char *fileindex_path = NULL;
7625 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7629 err = got_ref_open(&resolved, repo,
7630 got_ref_get_symref_target(edited_branch), 0);
7634 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7635 resolved, new_head_commit_id, repo);
7639 err = got_ref_change_ref(resolved, new_head_commit_id);
7643 err = got_ref_write(resolved, repo);
7647 err = got_worktree_set_head_ref(worktree, resolved);
7651 err = delete_histedit_refs(worktree, repo);
7655 err = get_fileindex_path(&fileindex_path, worktree);
7658 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7659 sync_err = sync_fileindex(fileindex, fileindex_path);
7660 if (sync_err && err == NULL)
7663 got_fileindex_free(fileindex);
7664 free(fileindex_path);
7665 free(new_head_commit_id);
7666 unlockerr = lock_worktree(worktree, LOCK_SH);
7667 if (unlockerr && err == NULL)
7672 const struct got_error *
7673 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7674 struct got_object_id *commit_id, struct got_repository *repo)
7676 const struct got_error *err;
7677 char *commit_ref_name;
7679 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7683 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7687 err = delete_ref(commit_ref_name, repo);
7689 free(commit_ref_name);
7693 const struct got_error *
7694 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7695 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7696 struct got_worktree *worktree, const char *refname,
7697 struct got_repository *repo)
7699 const struct got_error *err = NULL;
7700 char *fileindex_path = NULL;
7701 struct check_rebase_ok_arg ok_arg;
7705 *base_branch_ref = NULL;
7707 err = lock_worktree(worktree, LOCK_EX);
7711 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7712 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7713 "cannot integrate a branch into itself; "
7714 "update -b or different branch name required");
7718 err = open_fileindex(fileindex, &fileindex_path, worktree);
7722 /* Preconditions are the same as for rebase. */
7723 ok_arg.worktree = worktree;
7725 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7730 err = got_ref_open(branch_ref, repo, refname, 1);
7734 err = got_ref_open(base_branch_ref, repo,
7735 got_worktree_get_head_ref_name(worktree), 1);
7739 got_ref_close(*branch_ref);
7742 if (*base_branch_ref) {
7743 got_ref_close(*base_branch_ref);
7744 *base_branch_ref = NULL;
7747 got_fileindex_free(*fileindex);
7750 lock_worktree(worktree, LOCK_SH);
7755 const struct got_error *
7756 got_worktree_integrate_continue(struct got_worktree *worktree,
7757 struct got_fileindex *fileindex, struct got_repository *repo,
7758 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7759 got_worktree_checkout_cb progress_cb, void *progress_arg,
7760 got_cancel_cb cancel_cb, void *cancel_arg)
7762 const struct got_error *err = NULL, *sync_err, *unlockerr;
7763 char *fileindex_path = NULL;
7764 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7765 struct got_commit_object *commit = NULL;
7767 err = get_fileindex_path(&fileindex_path, worktree);
7771 err = got_ref_resolve(&commit_id, repo, branch_ref);
7775 err = got_object_open_as_commit(&commit, repo, commit_id);
7779 err = got_object_id_by_path(&tree_id, repo, commit,
7780 worktree->path_prefix);
7784 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7788 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7789 progress_cb, progress_arg, cancel_cb, cancel_arg);
7793 err = got_ref_change_ref(base_branch_ref, commit_id);
7797 err = got_ref_write(base_branch_ref, repo);
7801 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7803 sync_err = sync_fileindex(fileindex, fileindex_path);
7804 if (sync_err && err == NULL)
7808 unlockerr = got_ref_unlock(branch_ref);
7809 if (unlockerr && err == NULL)
7811 got_ref_close(branch_ref);
7813 unlockerr = got_ref_unlock(base_branch_ref);
7814 if (unlockerr && err == NULL)
7816 got_ref_close(base_branch_ref);
7818 got_fileindex_free(fileindex);
7819 free(fileindex_path);
7822 got_object_commit_close(commit);
7824 unlockerr = lock_worktree(worktree, LOCK_SH);
7825 if (unlockerr && err == NULL)
7830 const struct got_error *
7831 got_worktree_integrate_abort(struct got_worktree *worktree,
7832 struct got_fileindex *fileindex, struct got_repository *repo,
7833 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7835 const struct got_error *err = NULL, *unlockerr = NULL;
7837 got_fileindex_free(fileindex);
7839 err = lock_worktree(worktree, LOCK_SH);
7841 unlockerr = got_ref_unlock(branch_ref);
7842 if (unlockerr && err == NULL)
7844 got_ref_close(branch_ref);
7846 unlockerr = got_ref_unlock(base_branch_ref);
7847 if (unlockerr && err == NULL)
7849 got_ref_close(base_branch_ref);
7854 const struct got_error *
7855 got_worktree_merge_postpone(struct got_worktree *worktree,
7856 struct got_fileindex *fileindex)
7858 const struct got_error *err, *sync_err;
7859 char *fileindex_path = NULL;
7861 err = get_fileindex_path(&fileindex_path, worktree);
7865 sync_err = sync_fileindex(fileindex, fileindex_path);
7867 err = lock_worktree(worktree, LOCK_SH);
7868 if (sync_err && err == NULL)
7871 got_fileindex_free(fileindex);
7872 free(fileindex_path);
7876 static const struct got_error *
7877 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7879 const struct got_error *err;
7880 char *branch_refname = NULL, *commit_refname = NULL;
7882 err = get_merge_branch_ref_name(&branch_refname, worktree);
7885 err = delete_ref(branch_refname, repo);
7889 err = get_merge_commit_ref_name(&commit_refname, worktree);
7892 err = delete_ref(commit_refname, repo);
7897 free(branch_refname);
7898 free(commit_refname);
7902 struct merge_commit_msg_arg {
7903 struct got_worktree *worktree;
7904 const char *branch_name;
7907 static const struct got_error *
7908 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
7909 const char *diff_path, char **logmsg, void *arg)
7911 struct merge_commit_msg_arg *a = arg;
7913 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7914 got_worktree_get_head_ref_name(a->worktree)) == -1)
7915 return got_error_from_errno("asprintf");
7921 const struct got_error *
7922 got_worktree_merge_branch(struct got_worktree *worktree,
7923 struct got_fileindex *fileindex,
7924 struct got_object_id *yca_commit_id,
7925 struct got_object_id *branch_tip,
7926 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7927 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7929 const struct got_error *err;
7930 char *fileindex_path = NULL;
7932 err = get_fileindex_path(&fileindex_path, worktree);
7936 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7941 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7942 branch_tip, repo, progress_cb, progress_arg,
7943 cancel_cb, cancel_arg);
7945 free(fileindex_path);
7949 const struct got_error *
7950 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7951 struct got_worktree *worktree, struct got_fileindex *fileindex,
7952 const char *author, const char *committer, int allow_bad_symlinks,
7953 struct got_object_id *branch_tip, const char *branch_name,
7954 int allow_conflict, struct got_repository *repo,
7955 got_worktree_status_cb status_cb, void *status_arg)
7958 const struct got_error *err = NULL, *sync_err;
7959 struct got_pathlist_head commitable_paths;
7960 struct collect_commitables_arg cc_arg;
7961 struct got_pathlist_entry *pe;
7962 struct got_reference *head_ref = NULL;
7963 struct got_object_id *head_commit_id = NULL;
7964 int have_staged_files = 0;
7965 struct merge_commit_msg_arg mcm_arg;
7966 char *fileindex_path = NULL;
7968 memset(&cc_arg, 0, sizeof(cc_arg));
7969 *new_commit_id = NULL;
7971 TAILQ_INIT(&commitable_paths);
7973 err = get_fileindex_path(&fileindex_path, worktree);
7977 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7981 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7985 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7986 &have_staged_files);
7987 if (err && err->code != GOT_ERR_CANCELLED)
7989 if (have_staged_files) {
7990 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7994 cc_arg.commitable_paths = &commitable_paths;
7995 cc_arg.worktree = worktree;
7996 cc_arg.fileindex = fileindex;
7998 cc_arg.have_staged_files = have_staged_files;
7999 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
8000 cc_arg.commit_conflicts = allow_conflict;
8001 err = worktree_status(worktree, "", fileindex, repo,
8002 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8006 if (TAILQ_EMPTY(&commitable_paths)) {
8007 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
8008 "merge of %s cannot proceed", branch_name);
8012 TAILQ_FOREACH(pe, &commitable_paths, entry) {
8013 struct got_commitable *ct = pe->data;
8014 const char *ct_path = ct->in_repo_path;
8016 while (ct_path[0] == '/')
8018 err = check_out_of_date(ct_path, ct->status,
8019 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
8020 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
8026 mcm_arg.worktree = worktree;
8027 mcm_arg.branch_name = branch_name;
8028 err = commit_worktree(new_commit_id, &commitable_paths,
8029 head_commit_id, branch_tip, worktree, author, committer, NULL,
8030 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8034 err = update_fileindex_after_commit(worktree, &commitable_paths,
8035 *new_commit_id, fileindex, have_staged_files);
8036 sync_err = sync_fileindex(fileindex, fileindex_path);
8037 if (sync_err && err == NULL)
8040 TAILQ_FOREACH(pe, &commitable_paths, entry) {
8041 struct got_commitable *ct = pe->data;
8043 free_commitable(ct);
8045 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8046 free(fileindex_path);
8050 const struct got_error *
8051 got_worktree_merge_complete(struct got_worktree *worktree,
8052 struct got_fileindex *fileindex, struct got_repository *repo)
8054 const struct got_error *err, *unlockerr, *sync_err;
8055 char *fileindex_path = NULL;
8057 err = delete_merge_refs(worktree, repo);
8061 err = get_fileindex_path(&fileindex_path, worktree);
8064 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8065 sync_err = sync_fileindex(fileindex, fileindex_path);
8066 if (sync_err && err == NULL)
8069 got_fileindex_free(fileindex);
8070 free(fileindex_path);
8071 unlockerr = lock_worktree(worktree, LOCK_SH);
8072 if (unlockerr && err == NULL)
8077 const struct got_error *
8078 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8079 struct got_repository *repo)
8081 const struct got_error *err;
8082 char *branch_refname = NULL;
8083 struct got_reference *branch_ref = NULL;
8087 err = get_merge_branch_ref_name(&branch_refname, worktree);
8090 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8091 free(branch_refname);
8093 if (err->code != GOT_ERR_NOT_REF)
8101 const struct got_error *got_worktree_merge_prepare(
8102 struct got_fileindex **fileindex, struct got_worktree *worktree,
8103 struct got_reference *branch, struct got_repository *repo)
8105 const struct got_error *err = NULL;
8106 char *fileindex_path = NULL;
8107 char *branch_refname = NULL, *commit_refname = NULL;
8108 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
8109 struct got_reference *commit_ref = NULL;
8110 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
8111 struct check_rebase_ok_arg ok_arg;
8115 err = lock_worktree(worktree, LOCK_EX);
8119 err = open_fileindex(fileindex, &fileindex_path, worktree);
8123 /* Preconditions are the same as for rebase. */
8124 ok_arg.worktree = worktree;
8126 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8131 err = get_merge_branch_ref_name(&branch_refname, worktree);
8135 err = get_merge_commit_ref_name(&commit_refname, worktree);
8139 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8144 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8148 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8149 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8153 err = got_ref_resolve(&branch_tip, repo, branch);
8157 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8160 err = got_ref_write(branch_ref, repo);
8164 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8167 err = got_ref_write(commit_ref, repo);
8172 free(branch_refname);
8173 free(commit_refname);
8174 free(fileindex_path);
8176 got_ref_close(branch_ref);
8178 got_ref_close(commit_ref);
8180 got_ref_close(wt_branch);
8181 free(wt_branch_tip);
8184 got_fileindex_free(*fileindex);
8187 lock_worktree(worktree, LOCK_SH);
8192 const struct got_error *
8193 got_worktree_merge_continue(char **branch_name,
8194 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8195 struct got_worktree *worktree, struct got_repository *repo)
8197 const struct got_error *err;
8198 char *commit_refname = NULL, *branch_refname = NULL;
8199 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8200 char *fileindex_path = NULL;
8201 int have_staged_files = 0;
8203 *branch_name = NULL;
8207 err = lock_worktree(worktree, LOCK_EX);
8211 err = open_fileindex(fileindex, &fileindex_path, worktree);
8215 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8216 &have_staged_files);
8217 if (err && err->code != GOT_ERR_CANCELLED)
8219 if (have_staged_files) {
8220 err = got_error(GOT_ERR_STAGED_PATHS);
8224 err = get_merge_branch_ref_name(&branch_refname, worktree);
8228 err = get_merge_commit_ref_name(&commit_refname, worktree);
8232 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8236 if (!got_ref_is_symbolic(branch_ref)) {
8237 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8238 "%s is not a symbolic reference",
8239 got_ref_get_name(branch_ref));
8242 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8243 if (*branch_name == NULL) {
8244 err = got_error_from_errno("strdup");
8248 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8252 err = got_ref_resolve(branch_tip, repo, commit_ref);
8256 free(commit_refname);
8257 free(branch_refname);
8258 free(fileindex_path);
8260 got_ref_close(commit_ref);
8262 got_ref_close(branch_ref);
8266 *branch_name = NULL;
8271 got_fileindex_free(*fileindex);
8274 lock_worktree(worktree, LOCK_SH);
8279 const struct got_error *
8280 got_worktree_merge_abort(struct got_worktree *worktree,
8281 struct got_fileindex *fileindex, struct got_repository *repo,
8282 got_worktree_checkout_cb progress_cb, void *progress_arg)
8284 const struct got_error *err, *unlockerr, *sync_err;
8285 struct got_object_id *commit_id = NULL;
8286 struct got_commit_object *commit = NULL;
8287 char *fileindex_path = NULL;
8288 struct revert_file_args rfa;
8289 struct got_object_id *tree_id = NULL;
8291 err = got_object_open_as_commit(&commit, repo,
8292 worktree->base_commit_id);
8296 err = got_object_id_by_path(&tree_id, repo, commit,
8297 worktree->path_prefix);
8301 err = delete_merge_refs(worktree, repo);
8305 err = get_fileindex_path(&fileindex_path, worktree);
8309 rfa.worktree = worktree;
8310 rfa.fileindex = fileindex;
8311 rfa.progress_cb = progress_cb;
8312 rfa.progress_arg = progress_arg;
8313 rfa.patch_cb = NULL;
8314 rfa.patch_arg = NULL;
8316 rfa.unlink_added_files = 1;
8317 err = worktree_status(worktree, "", fileindex, repo,
8318 revert_file, &rfa, NULL, NULL, 1, 0);
8322 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8323 repo, progress_cb, progress_arg, NULL, NULL);
8325 sync_err = sync_fileindex(fileindex, fileindex_path);
8326 if (sync_err && err == NULL)
8332 got_object_commit_close(commit);
8334 got_fileindex_free(fileindex);
8335 free(fileindex_path);
8337 unlockerr = lock_worktree(worktree, LOCK_SH);
8338 if (unlockerr && err == NULL)
8343 struct check_stage_ok_arg {
8344 struct got_object_id *head_commit_id;
8345 struct got_worktree *worktree;
8346 struct got_fileindex *fileindex;
8347 struct got_repository *repo;
8351 static const struct got_error *
8352 check_stage_ok(void *arg, unsigned char status,
8353 unsigned char staged_status, const char *relpath,
8354 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8355 struct got_object_id *commit_id, int dirfd, const char *de_name)
8357 struct check_stage_ok_arg *a = arg;
8358 const struct got_error *err = NULL;
8359 struct got_fileindex_entry *ie;
8360 struct got_object_id base_commit_id;
8361 struct got_object_id *base_commit_idp = NULL;
8362 char *in_repo_path = NULL, *p;
8364 if (status == GOT_STATUS_UNVERSIONED ||
8365 status == GOT_STATUS_NO_CHANGE)
8367 if (status == GOT_STATUS_NONEXISTENT)
8368 return got_error_set_errno(ENOENT, relpath);
8370 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8372 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8374 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8375 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8377 return got_error_from_errno("asprintf");
8379 if (got_fileindex_entry_has_commit(ie)) {
8380 base_commit_idp = got_fileindex_entry_get_commit_id(
8381 &base_commit_id, ie);
8384 if (status == GOT_STATUS_CONFLICT) {
8385 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8387 } else if (status != GOT_STATUS_ADD &&
8388 status != GOT_STATUS_MODIFY &&
8389 status != GOT_STATUS_DELETE) {
8390 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8394 a->have_changes = 1;
8399 err = check_out_of_date(p, status, staged_status,
8400 blob_id, base_commit_idp, a->head_commit_id, a->repo,
8401 GOT_ERR_STAGE_OUT_OF_DATE);
8407 struct stage_path_arg {
8408 struct got_worktree *worktree;
8409 struct got_fileindex *fileindex;
8410 struct got_repository *repo;
8411 got_worktree_status_cb status_cb;
8413 got_worktree_patch_cb patch_cb;
8415 int staged_something;
8416 int allow_bad_symlinks;
8419 static const struct got_error *
8420 stage_path(void *arg, unsigned char status,
8421 unsigned char staged_status, const char *relpath,
8422 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8423 struct got_object_id *commit_id, int dirfd, const char *de_name)
8425 struct stage_path_arg *a = arg;
8426 const struct got_error *err = NULL;
8427 struct got_fileindex_entry *ie;
8428 char *ondisk_path = NULL, *path_content = NULL;
8430 struct got_object_id *new_staged_blob_id = NULL;
8433 if (status == GOT_STATUS_UNVERSIONED)
8436 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8438 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8440 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8442 return got_error_from_errno("asprintf");
8445 case GOT_STATUS_ADD:
8446 case GOT_STATUS_MODIFY:
8447 /* XXX could sb.st_mode be passed in by our caller? */
8448 if (lstat(ondisk_path, &sb) == -1) {
8449 err = got_error_from_errno2("lstat", ondisk_path);
8453 if (status == GOT_STATUS_ADD) {
8454 int choice = GOT_PATCH_CHOICE_NONE;
8455 err = (*a->patch_cb)(&choice, a->patch_arg,
8456 status, ie->path, NULL, 1, 1);
8459 if (choice != GOT_PATCH_CHOICE_YES)
8462 err = create_patched_content(&path_content, 0,
8463 staged_blob_id ? staged_blob_id : blob_id,
8464 ondisk_path, dirfd, de_name, ie->path,
8465 a->repo, a->patch_cb, a->patch_arg);
8466 if (err || path_content == NULL)
8470 err = got_object_blob_create(&new_staged_blob_id,
8471 path_content ? path_content : ondisk_path, a->repo);
8474 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8475 SHA1_DIGEST_LENGTH);
8476 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8477 stage = GOT_FILEIDX_STAGE_ADD;
8479 stage = GOT_FILEIDX_STAGE_MODIFY;
8480 got_fileindex_entry_stage_set(ie, stage);
8481 if (S_ISLNK(sb.st_mode)) {
8482 int is_bad_symlink = 0;
8483 if (!a->allow_bad_symlinks) {
8484 char target_path[PATH_MAX];
8486 target_len = readlink(ondisk_path, target_path,
8487 sizeof(target_path));
8488 if (target_len == -1) {
8489 err = got_error_from_errno2("readlink",
8493 err = is_bad_symlink_target(&is_bad_symlink,
8494 target_path, target_len, ondisk_path,
8495 a->worktree->root_path);
8498 if (is_bad_symlink) {
8499 err = got_error_path(ondisk_path,
8500 GOT_ERR_BAD_SYMLINK);
8505 got_fileindex_entry_staged_filetype_set(ie,
8506 GOT_FILEIDX_MODE_BAD_SYMLINK);
8508 got_fileindex_entry_staged_filetype_set(ie,
8509 GOT_FILEIDX_MODE_SYMLINK);
8511 got_fileindex_entry_staged_filetype_set(ie,
8512 GOT_FILEIDX_MODE_REGULAR_FILE);
8514 a->staged_something = 1;
8515 if (a->status_cb == NULL)
8517 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8518 get_staged_status(ie), relpath, blob_id,
8519 new_staged_blob_id, NULL, dirfd, de_name);
8523 * When staging the reverse of the staged diff,
8524 * implicitly unstage the file.
8526 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8527 sizeof(ie->blob_sha1)) == 0) {
8528 got_fileindex_entry_stage_set(ie,
8529 GOT_FILEIDX_STAGE_NONE);
8532 case GOT_STATUS_DELETE:
8533 if (staged_status == GOT_STATUS_DELETE)
8536 int choice = GOT_PATCH_CHOICE_NONE;
8537 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8538 ie->path, NULL, 1, 1);
8541 if (choice == GOT_PATCH_CHOICE_NO)
8543 if (choice != GOT_PATCH_CHOICE_YES) {
8544 err = got_error(GOT_ERR_PATCH_CHOICE);
8548 stage = GOT_FILEIDX_STAGE_DELETE;
8549 got_fileindex_entry_stage_set(ie, stage);
8550 a->staged_something = 1;
8551 if (a->status_cb == NULL)
8553 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8554 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8557 case GOT_STATUS_NO_CHANGE:
8559 case GOT_STATUS_CONFLICT:
8560 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8562 case GOT_STATUS_NONEXISTENT:
8563 err = got_error_set_errno(ENOENT, relpath);
8566 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8570 if (path_content && unlink(path_content) == -1 && err == NULL)
8571 err = got_error_from_errno2("unlink", path_content);
8574 free(new_staged_blob_id);
8578 const struct got_error *
8579 got_worktree_stage(struct got_worktree *worktree,
8580 struct got_pathlist_head *paths,
8581 got_worktree_status_cb status_cb, void *status_arg,
8582 got_worktree_patch_cb patch_cb, void *patch_arg,
8583 int allow_bad_symlinks, struct got_repository *repo)
8585 const struct got_error *err = NULL, *sync_err, *unlockerr;
8586 struct got_pathlist_entry *pe;
8587 struct got_fileindex *fileindex = NULL;
8588 char *fileindex_path = NULL;
8589 struct got_reference *head_ref = NULL;
8590 struct got_object_id *head_commit_id = NULL;
8591 struct check_stage_ok_arg oka;
8592 struct stage_path_arg spa;
8594 err = lock_worktree(worktree, LOCK_EX);
8598 err = got_ref_open(&head_ref, repo,
8599 got_worktree_get_head_ref_name(worktree), 0);
8602 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8605 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8609 /* Check pre-conditions before staging anything. */
8610 oka.head_commit_id = head_commit_id;
8611 oka.worktree = worktree;
8612 oka.fileindex = fileindex;
8614 oka.have_changes = 0;
8615 TAILQ_FOREACH(pe, paths, entry) {
8616 err = worktree_status(worktree, pe->path, fileindex, repo,
8617 check_stage_ok, &oka, NULL, NULL, 1, 0);
8621 if (!oka.have_changes) {
8622 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8626 spa.worktree = worktree;
8627 spa.fileindex = fileindex;
8629 spa.patch_cb = patch_cb;
8630 spa.patch_arg = patch_arg;
8631 spa.status_cb = status_cb;
8632 spa.status_arg = status_arg;
8633 spa.staged_something = 0;
8634 spa.allow_bad_symlinks = allow_bad_symlinks;
8635 TAILQ_FOREACH(pe, paths, entry) {
8636 err = worktree_status(worktree, pe->path, fileindex, repo,
8637 stage_path, &spa, NULL, NULL, 1, 0);
8641 if (!spa.staged_something) {
8642 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8646 sync_err = sync_fileindex(fileindex, fileindex_path);
8647 if (sync_err && err == NULL)
8651 got_ref_close(head_ref);
8652 free(head_commit_id);
8653 free(fileindex_path);
8655 got_fileindex_free(fileindex);
8656 unlockerr = lock_worktree(worktree, LOCK_SH);
8657 if (unlockerr && err == NULL)
8662 struct unstage_path_arg {
8663 struct got_worktree *worktree;
8664 struct got_fileindex *fileindex;
8665 struct got_repository *repo;
8666 got_worktree_checkout_cb progress_cb;
8668 got_worktree_patch_cb patch_cb;
8672 static const struct got_error *
8673 create_unstaged_content(char **path_unstaged_content,
8674 char **path_new_staged_content, struct got_object_id *blob_id,
8675 struct got_object_id *staged_blob_id, const char *relpath,
8676 struct got_repository *repo,
8677 got_worktree_patch_cb patch_cb, void *patch_arg)
8679 const struct got_error *err, *free_err;
8680 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8681 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8682 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8683 struct got_diffreg_result *diffreg_result = NULL;
8684 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8685 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8686 int fd1 = -1, fd2 = -1;
8688 *path_unstaged_content = NULL;
8689 *path_new_staged_content = NULL;
8691 err = got_object_id_str(&label1, blob_id);
8695 fd1 = got_opentempfd();
8697 err = got_error_from_errno("got_opentempfd");
8700 fd2 = got_opentempfd();
8702 err = got_error_from_errno("got_opentempfd");
8706 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8710 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
8714 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8718 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8723 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
8727 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8731 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8732 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8736 err = got_opentemp_named(path_unstaged_content, &outfile,
8737 "got-unstaged-content", "");
8740 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8741 "got-new-staged-content", "");
8745 if (fseek(f1, 0L, SEEK_SET) == -1) {
8746 err = got_ferror(f1, GOT_ERR_IO);
8749 if (fseek(f2, 0L, SEEK_SET) == -1) {
8750 err = got_ferror(f2, GOT_ERR_IO);
8753 /* Count the number of actual changes in the diff result. */
8754 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8755 struct diff_chunk_context cc = {};
8756 diff_chunk_context_load_change(&cc, &nchunks_used,
8757 diffreg_result->result, n, 0);
8760 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8762 err = apply_or_reject_change(&choice, &nchunks_used,
8763 diffreg_result->result, n, relpath, f1, f2,
8764 &line_cur1, &line_cur2,
8765 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8768 if (choice == GOT_PATCH_CHOICE_YES)
8771 have_rejected_content = 1;
8772 if (choice == GOT_PATCH_CHOICE_QUIT)
8775 if (have_content || have_rejected_content)
8776 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8777 outfile, rejectfile);
8780 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8781 err = got_error_from_errno("close");
8783 got_object_blob_close(blob);
8784 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8785 err = got_error_from_errno("close");
8787 got_object_blob_close(staged_blob);
8788 free_err = got_diffreg_result_free(diffreg_result);
8789 if (free_err && err == NULL)
8791 if (f1 && fclose(f1) == EOF && err == NULL)
8792 err = got_error_from_errno2("fclose", path1);
8793 if (f2 && fclose(f2) == EOF && err == NULL)
8794 err = got_error_from_errno2("fclose", path2);
8795 if (outfile && fclose(outfile) == EOF && err == NULL)
8796 err = got_error_from_errno2("fclose", *path_unstaged_content);
8797 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8798 err = got_error_from_errno2("fclose", *path_new_staged_content);
8799 if (path1 && unlink(path1) == -1 && err == NULL)
8800 err = got_error_from_errno2("unlink", path1);
8801 if (path2 && unlink(path2) == -1 && err == NULL)
8802 err = got_error_from_errno2("unlink", path2);
8803 if (err || !have_content) {
8804 if (*path_unstaged_content &&
8805 unlink(*path_unstaged_content) == -1 && err == NULL)
8806 err = got_error_from_errno2("unlink",
8807 *path_unstaged_content);
8808 free(*path_unstaged_content);
8809 *path_unstaged_content = NULL;
8811 if (err || !have_content || !have_rejected_content) {
8812 if (*path_new_staged_content &&
8813 unlink(*path_new_staged_content) == -1 && err == NULL)
8814 err = got_error_from_errno2("unlink",
8815 *path_new_staged_content);
8816 free(*path_new_staged_content);
8817 *path_new_staged_content = NULL;
8824 static const struct got_error *
8825 unstage_hunks(struct got_object_id *staged_blob_id,
8826 struct got_blob_object *blob_base,
8827 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8828 const char *ondisk_path, const char *label_orig,
8829 struct got_worktree *worktree, struct got_repository *repo,
8830 got_worktree_patch_cb patch_cb, void *patch_arg,
8831 got_worktree_checkout_cb progress_cb, void *progress_arg)
8833 const struct got_error *err = NULL;
8834 char *path_unstaged_content = NULL;
8835 char *path_new_staged_content = NULL;
8836 char *parent = NULL, *base_path = NULL;
8837 char *blob_base_path = NULL;
8838 struct got_object_id *new_staged_blob_id = NULL;
8839 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8842 err = create_unstaged_content(&path_unstaged_content,
8843 &path_new_staged_content, blob_id, staged_blob_id,
8844 ie->path, repo, patch_cb, patch_arg);
8848 if (path_unstaged_content == NULL)
8851 if (path_new_staged_content) {
8852 err = got_object_blob_create(&new_staged_blob_id,
8853 path_new_staged_content, repo);
8858 f = fopen(path_unstaged_content, "re");
8860 err = got_error_from_errno2("fopen",
8861 path_unstaged_content);
8864 if (fstat(fileno(f), &sb) == -1) {
8865 err = got_error_from_errno2("fstat", path_unstaged_content);
8868 if (got_fileindex_entry_staged_filetype_get(ie) ==
8869 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8870 char link_target[PATH_MAX];
8872 r = fread(link_target, 1, sizeof(link_target), f);
8873 if (r == 0 && ferror(f)) {
8874 err = got_error_from_errno("fread");
8877 if (r >= sizeof(link_target)) { /* should not happen */
8878 err = got_error(GOT_ERR_NO_SPACE);
8881 link_target[r] = '\0';
8882 err = merge_symlink(worktree, blob_base,
8883 ondisk_path, ie->path, label_orig, link_target,
8884 worktree->base_commit_id, repo, progress_cb,
8887 int local_changes_subsumed;
8889 err = got_path_dirname(&parent, ondisk_path);
8893 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8895 err = got_error_from_errno("asprintf");
8900 err = got_opentemp_named(&blob_base_path, &f_base,
8904 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8910 * In order the run a 3-way merge with a symlink we copy the symlink's
8911 * target path into a temporary file and use that file with diff3.
8913 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8914 err = dump_symlink_target_path_to_file(&f_deriv2,
8920 fd = open(ondisk_path,
8921 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8923 err = got_error_from_errno2("open", ondisk_path);
8926 f_deriv2 = fdopen(fd, "r");
8927 if (f_deriv2 == NULL) {
8928 err = got_error_from_errno2("fdopen", ondisk_path);
8934 err = merge_file(&local_changes_subsumed, worktree,
8935 f_base, f, f_deriv2, ondisk_path, ie->path,
8936 got_fileindex_perms_to_st(ie),
8937 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8938 repo, progress_cb, progress_arg);
8943 if (new_staged_blob_id) {
8944 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8945 SHA1_DIGEST_LENGTH);
8947 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8948 got_fileindex_entry_staged_filetype_set(ie, 0);
8951 free(new_staged_blob_id);
8952 if (path_unstaged_content &&
8953 unlink(path_unstaged_content) == -1 && err == NULL)
8954 err = got_error_from_errno2("unlink", path_unstaged_content);
8955 if (path_new_staged_content &&
8956 unlink(path_new_staged_content) == -1 && err == NULL)
8957 err = got_error_from_errno2("unlink", path_new_staged_content);
8958 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8959 err = got_error_from_errno2("unlink", blob_base_path);
8960 if (f_base && fclose(f_base) == EOF && err == NULL)
8961 err = got_error_from_errno2("fclose", path_unstaged_content);
8962 if (f && fclose(f) == EOF && err == NULL)
8963 err = got_error_from_errno2("fclose", path_unstaged_content);
8964 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8965 err = got_error_from_errno2("fclose", ondisk_path);
8966 free(path_unstaged_content);
8967 free(path_new_staged_content);
8968 free(blob_base_path);
8974 static const struct got_error *
8975 unstage_path(void *arg, unsigned char status,
8976 unsigned char staged_status, const char *relpath,
8977 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8978 struct got_object_id *commit_id, int dirfd, const char *de_name)
8980 const struct got_error *err = NULL;
8981 struct unstage_path_arg *a = arg;
8982 struct got_fileindex_entry *ie;
8983 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8984 char *ondisk_path = NULL;
8985 char *id_str = NULL, *label_orig = NULL;
8986 int local_changes_subsumed;
8988 int fd1 = -1, fd2 = -1;
8990 if (staged_status != GOT_STATUS_ADD &&
8991 staged_status != GOT_STATUS_MODIFY &&
8992 staged_status != GOT_STATUS_DELETE)
8995 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8997 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8999 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
9001 return got_error_from_errno("asprintf");
9003 err = got_object_id_str(&id_str,
9004 commit_id ? commit_id : a->worktree->base_commit_id);
9007 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9009 err = got_error_from_errno("asprintf");
9013 fd1 = got_opentempfd();
9015 err = got_error_from_errno("got_opentempfd");
9018 fd2 = got_opentempfd();
9020 err = got_error_from_errno("got_opentempfd");
9024 switch (staged_status) {
9025 case GOT_STATUS_MODIFY:
9026 err = got_object_open_as_blob(&blob_base, a->repo,
9027 blob_id, 8192, fd1);
9031 case GOT_STATUS_ADD:
9033 if (staged_status == GOT_STATUS_ADD) {
9034 int choice = GOT_PATCH_CHOICE_NONE;
9035 err = (*a->patch_cb)(&choice, a->patch_arg,
9036 staged_status, ie->path, NULL, 1, 1);
9039 if (choice != GOT_PATCH_CHOICE_YES)
9042 err = unstage_hunks(staged_blob_id,
9043 blob_base, blob_id, ie, ondisk_path,
9044 label_orig, a->worktree, a->repo,
9045 a->patch_cb, a->patch_arg,
9046 a->progress_cb, a->progress_arg);
9047 break; /* Done with this file. */
9050 err = got_object_open_as_blob(&blob_staged, a->repo,
9051 staged_blob_id, 8192, fd2);
9054 switch (got_fileindex_entry_staged_filetype_get(ie)) {
9055 case GOT_FILEIDX_MODE_BAD_SYMLINK:
9056 case GOT_FILEIDX_MODE_REGULAR_FILE:
9057 err = merge_blob(&local_changes_subsumed, a->worktree,
9058 blob_base, ondisk_path, relpath,
9059 got_fileindex_perms_to_st(ie), label_orig,
9060 blob_staged, commit_id ? commit_id :
9061 a->worktree->base_commit_id, a->repo,
9062 a->progress_cb, a->progress_arg);
9064 case GOT_FILEIDX_MODE_SYMLINK:
9065 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9066 char *staged_target;
9067 err = got_object_blob_read_to_str(
9068 &staged_target, blob_staged);
9071 err = merge_symlink(a->worktree, blob_base,
9072 ondisk_path, relpath, label_orig,
9073 staged_target, commit_id ? commit_id :
9074 a->worktree->base_commit_id,
9075 a->repo, a->progress_cb, a->progress_arg);
9076 free(staged_target);
9078 err = merge_blob(&local_changes_subsumed,
9079 a->worktree, blob_base, ondisk_path,
9080 relpath, got_fileindex_perms_to_st(ie),
9081 label_orig, blob_staged,
9082 commit_id ? commit_id :
9083 a->worktree->base_commit_id, a->repo,
9084 a->progress_cb, a->progress_arg);
9088 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9092 got_fileindex_entry_stage_set(ie,
9093 GOT_FILEIDX_STAGE_NONE);
9094 got_fileindex_entry_staged_filetype_set(ie, 0);
9097 case GOT_STATUS_DELETE:
9099 int choice = GOT_PATCH_CHOICE_NONE;
9100 err = (*a->patch_cb)(&choice, a->patch_arg,
9101 staged_status, ie->path, NULL, 1, 1);
9104 if (choice == GOT_PATCH_CHOICE_NO)
9106 if (choice != GOT_PATCH_CHOICE_YES) {
9107 err = got_error(GOT_ERR_PATCH_CHOICE);
9111 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9112 got_fileindex_entry_staged_filetype_set(ie, 0);
9113 err = get_file_status(&status, &sb, ie, ondisk_path,
9114 dirfd, de_name, a->repo);
9117 err = (*a->progress_cb)(a->progress_arg, status, relpath);
9122 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9123 err = got_error_from_errno("close");
9125 got_object_blob_close(blob_base);
9126 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9127 err = got_error_from_errno("close");
9129 got_object_blob_close(blob_staged);
9135 const struct got_error *
9136 got_worktree_unstage(struct got_worktree *worktree,
9137 struct got_pathlist_head *paths,
9138 got_worktree_checkout_cb progress_cb, void *progress_arg,
9139 got_worktree_patch_cb patch_cb, void *patch_arg,
9140 struct got_repository *repo)
9142 const struct got_error *err = NULL, *sync_err, *unlockerr;
9143 struct got_pathlist_entry *pe;
9144 struct got_fileindex *fileindex = NULL;
9145 char *fileindex_path = NULL;
9146 struct unstage_path_arg upa;
9148 err = lock_worktree(worktree, LOCK_EX);
9152 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9156 upa.worktree = worktree;
9157 upa.fileindex = fileindex;
9159 upa.progress_cb = progress_cb;
9160 upa.progress_arg = progress_arg;
9161 upa.patch_cb = patch_cb;
9162 upa.patch_arg = patch_arg;
9163 TAILQ_FOREACH(pe, paths, entry) {
9164 err = worktree_status(worktree, pe->path, fileindex, repo,
9165 unstage_path, &upa, NULL, NULL, 1, 0);
9170 sync_err = sync_fileindex(fileindex, fileindex_path);
9171 if (sync_err && err == NULL)
9174 free(fileindex_path);
9176 got_fileindex_free(fileindex);
9177 unlockerr = lock_worktree(worktree, LOCK_SH);
9178 if (unlockerr && err == NULL)
9183 struct report_file_info_arg {
9184 struct got_worktree *worktree;
9185 got_worktree_path_info_cb info_cb;
9187 struct got_pathlist_head *paths;
9188 got_cancel_cb cancel_cb;
9192 static const struct got_error *
9193 report_file_info(void *arg, struct got_fileindex_entry *ie)
9195 struct report_file_info_arg *a = arg;
9196 struct got_pathlist_entry *pe;
9197 struct got_object_id blob_id, staged_blob_id, commit_id;
9198 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9199 struct got_object_id *commit_idp = NULL;
9202 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9203 return got_error(GOT_ERR_CANCELLED);
9205 TAILQ_FOREACH(pe, a->paths, entry) {
9206 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9207 got_path_is_child(ie->path, pe->path, pe->path_len))
9210 if (pe == NULL) /* not found */
9213 if (got_fileindex_entry_has_blob(ie))
9214 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9215 stage = got_fileindex_entry_stage_get(ie);
9216 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9217 stage == GOT_FILEIDX_STAGE_ADD) {
9218 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9219 &staged_blob_id, ie);
9222 if (got_fileindex_entry_has_commit(ie))
9223 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9225 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9226 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9229 const struct got_error *
9230 got_worktree_path_info(struct got_worktree *worktree,
9231 struct got_pathlist_head *paths,
9232 got_worktree_path_info_cb info_cb, void *info_arg,
9233 got_cancel_cb cancel_cb, void *cancel_arg)
9236 const struct got_error *err = NULL, *unlockerr;
9237 struct got_fileindex *fileindex = NULL;
9238 char *fileindex_path = NULL;
9239 struct report_file_info_arg arg;
9241 err = lock_worktree(worktree, LOCK_SH);
9245 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9249 arg.worktree = worktree;
9250 arg.info_cb = info_cb;
9251 arg.info_arg = info_arg;
9253 arg.cancel_cb = cancel_cb;
9254 arg.cancel_arg = cancel_arg;
9255 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9258 free(fileindex_path);
9260 got_fileindex_free(fileindex);
9261 unlockerr = lock_worktree(worktree, LOCK_UN);
9262 if (unlockerr && err == NULL)
9267 static const struct got_error *
9268 patch_check_path(const char *p, char **path, unsigned char *status,
9269 unsigned char *staged_status, struct got_fileindex *fileindex,
9270 struct got_worktree *worktree, struct got_repository *repo)
9272 const struct got_error *err;
9273 struct got_fileindex_entry *ie;
9275 char *ondisk_path = NULL;
9277 err = got_worktree_resolve_path(path, worktree, p);
9281 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9282 *path[0] ? "/" : "", *path) == -1)
9283 return got_error_from_errno("asprintf");
9285 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9287 *staged_status = get_staged_status(ie);
9288 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9293 *staged_status = GOT_STATUS_NO_CHANGE;
9294 *status = GOT_STATUS_UNVERSIONED;
9295 if (lstat(ondisk_path, &sb) == -1) {
9296 if (errno != ENOENT) {
9297 err = got_error_from_errno2("lstat",
9301 *status = GOT_STATUS_NONEXISTENT;
9310 static const struct got_error *
9311 patch_can_rm(const char *path, unsigned char status,
9312 unsigned char staged_status)
9314 if (status == GOT_STATUS_NONEXISTENT)
9315 return got_error_set_errno(ENOENT, path);
9316 if (status != GOT_STATUS_NO_CHANGE &&
9317 status != GOT_STATUS_ADD &&
9318 status != GOT_STATUS_MODIFY &&
9319 status != GOT_STATUS_MODE_CHANGE)
9320 return got_error_path(path, GOT_ERR_FILE_STATUS);
9321 if (staged_status == GOT_STATUS_DELETE)
9322 return got_error_path(path, GOT_ERR_FILE_STATUS);
9326 static const struct got_error *
9327 patch_can_add(const char *path, unsigned char status)
9329 if (status != GOT_STATUS_NONEXISTENT)
9330 return got_error_path(path, GOT_ERR_FILE_STATUS);
9334 static const struct got_error *
9335 patch_can_edit(const char *path, unsigned char status,
9336 unsigned char staged_status)
9338 if (status == GOT_STATUS_NONEXISTENT)
9339 return got_error_set_errno(ENOENT, path);
9340 if (status != GOT_STATUS_NO_CHANGE &&
9341 status != GOT_STATUS_ADD &&
9342 status != GOT_STATUS_MODIFY)
9343 return got_error_path(path, GOT_ERR_FILE_STATUS);
9344 if (staged_status == GOT_STATUS_DELETE)
9345 return got_error_path(path, GOT_ERR_FILE_STATUS);
9349 const struct got_error *
9350 got_worktree_patch_prepare(struct got_fileindex **fileindex,
9351 char **fileindex_path, struct got_worktree *worktree)
9353 return open_fileindex(fileindex, fileindex_path, worktree);
9356 const struct got_error *
9357 got_worktree_patch_check_path(const char *old, const char *new,
9358 char **oldpath, char **newpath, struct got_worktree *worktree,
9359 struct got_repository *repo, struct got_fileindex *fileindex)
9361 const struct got_error *err = NULL;
9362 int file_renamed = 0;
9363 unsigned char status_old, staged_status_old;
9364 unsigned char status_new, staged_status_new;
9369 err = patch_check_path(old != NULL ? old : new, oldpath,
9370 &status_old, &staged_status_old, fileindex, worktree, repo);
9374 err = patch_check_path(new != NULL ? new : old, newpath,
9375 &status_new, &staged_status_new, fileindex, worktree, repo);
9379 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9382 if (old != NULL && new == NULL)
9383 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9384 else if (file_renamed) {
9385 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9387 err = patch_can_add(*newpath, status_new);
9388 } else if (old == NULL)
9389 err = patch_can_add(*newpath, status_new);
9391 err = patch_can_edit(*newpath, status_new, staged_status_new);
9403 const struct got_error *
9404 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9405 struct got_worktree *worktree, struct got_fileindex *fileindex,
9406 got_worktree_checkout_cb progress_cb, void *progress_arg)
9408 struct schedule_addition_args saa;
9410 memset(&saa, 0, sizeof(saa));
9411 saa.worktree = worktree;
9412 saa.fileindex = fileindex;
9413 saa.progress_cb = progress_cb;
9414 saa.progress_arg = progress_arg;
9417 return worktree_status(worktree, path, fileindex, repo,
9418 schedule_addition, &saa, NULL, NULL, 1, 0);
9421 const struct got_error *
9422 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9423 struct got_worktree *worktree, struct got_fileindex *fileindex,
9424 got_worktree_delete_cb progress_cb, void *progress_arg)
9426 struct schedule_deletion_args sda;
9428 memset(&sda, 0, sizeof(sda));
9429 sda.worktree = worktree;
9430 sda.fileindex = fileindex;
9431 sda.progress_cb = progress_cb;
9432 sda.progress_arg = progress_arg;
9434 sda.delete_local_mods = 0;
9435 sda.keep_on_disk = 0;
9436 sda.ignore_missing_paths = 0;
9437 sda.status_codes = NULL;
9439 return worktree_status(worktree, path, fileindex, repo,
9440 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9443 const struct got_error *
9444 got_worktree_patch_complete(struct got_fileindex *fileindex,
9445 const char *fileindex_path)
9447 const struct got_error *err = NULL;
9449 err = sync_fileindex(fileindex, fileindex_path);
9450 got_fileindex_free(fileindex);