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 = {};
1567 * We can optimise a little by advancing straight
1568 * to the next chunk if this one has no added lines.
1570 c = diff_chunk_get(r, n);
1571 clc = diff_chunk_get_left_count(c);
1572 crc = diff_chunk_get_right_count(c);
1574 if (!crc || crc == clc) {
1576 continue; /* removed or unchanged lines */
1579 pos = diff_chunk_get_right_start_pos(c);
1580 if (fseek(ondisk_file, pos, SEEK_SET) == -1) {
1581 err = got_ferror(ondisk_file, GOT_ERR_IO);
1585 diff_chunk_context_load_change(&cc, &nchunks_parsed, r, n, 0);
1586 ln = cc.right.start;
1588 while (ln < cc.right.end) {
1589 linelen = getline(&line, &linesize, ondisk_file);
1590 if (linelen == -1) {
1591 if (feof(ondisk_file))
1593 err = got_ferror(ondisk_file, GOT_ERR_IO);
1597 if (line && strncmp(line, markers[i],
1598 strlen(markers[i])) == 0) {
1599 if (strcmp(markers[i],
1600 GOT_DIFF_CONFLICT_MARKER_END) == 0) {
1601 *status = GOT_STATUS_CONFLICT;
1612 if (f1 != NULL && fclose(f1) == EOF && err == NULL)
1613 err = got_error_from_errno("fclose");
1614 free_err = got_diffreg_result_free(diffreg_result);
1622 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1624 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1625 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1629 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1631 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1632 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1633 ie->mtime_sec == sb->st_mtim.tv_sec &&
1634 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1635 ie->size == (sb->st_size & 0xffffffff) &&
1636 !xbit_differs(ie, sb->st_mode));
1639 static unsigned char
1640 get_staged_status(struct got_fileindex_entry *ie)
1642 switch (got_fileindex_entry_stage_get(ie)) {
1643 case GOT_FILEIDX_STAGE_ADD:
1644 return GOT_STATUS_ADD;
1645 case GOT_FILEIDX_STAGE_DELETE:
1646 return GOT_STATUS_DELETE;
1647 case GOT_FILEIDX_STAGE_MODIFY:
1648 return GOT_STATUS_MODIFY;
1650 return GOT_STATUS_NO_CHANGE;
1654 static const struct got_error *
1655 get_symlink_modification_status(unsigned char *status,
1656 struct got_fileindex_entry *ie, const char *abspath,
1657 int dirfd, const char *de_name, struct got_blob_object *blob)
1659 const struct got_error *err = NULL;
1660 char target_path[PATH_MAX];
1661 char etarget[PATH_MAX];
1663 size_t len, target_len = 0;
1664 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1665 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1667 *status = GOT_STATUS_NO_CHANGE;
1669 /* Blob object content specifies the target path of the link. */
1671 err = got_object_blob_read_block(&len, blob);
1674 if (len + target_len >= sizeof(target_path)) {
1676 * Should not happen. The blob contents were OK
1677 * when this symlink was installed.
1679 return got_error(GOT_ERR_NO_SPACE);
1682 /* Skip blob object header first time around. */
1683 memcpy(target_path + target_len, buf + hdrlen,
1685 target_len += len - hdrlen;
1689 target_path[target_len] = '\0';
1692 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1694 return got_error_from_errno2("readlinkat", abspath);
1696 elen = readlink(abspath, etarget, sizeof(etarget));
1698 return got_error_from_errno2("readlink", abspath);
1701 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1702 *status = GOT_STATUS_MODIFY;
1707 static const struct got_error *
1708 get_file_status(unsigned char *status, struct stat *sb,
1709 struct got_fileindex_entry *ie, const char *abspath,
1710 int dirfd, const char *de_name, struct got_repository *repo)
1712 const struct got_error *err = NULL;
1713 struct got_object_id id;
1715 int fd = -1, fd1 = -1;
1718 struct got_blob_object *blob = NULL;
1720 unsigned char staged_status;
1722 staged_status = get_staged_status(ie);
1723 *status = GOT_STATUS_NO_CHANGE;
1724 memset(sb, 0, sizeof(*sb));
1727 * Whenever the caller provides a directory descriptor and a
1728 * directory entry name for the file, use them! This prevents
1729 * race conditions if filesystem paths change beneath our feet.
1732 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1733 if (errno == ENOENT) {
1734 if (got_fileindex_entry_has_file_on_disk(ie))
1735 *status = GOT_STATUS_MISSING;
1737 *status = GOT_STATUS_DELETE;
1740 err = got_error_from_errno2("fstatat", abspath);
1744 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1745 if (fd == -1 && errno != ENOENT &&
1746 !got_err_open_nofollow_on_symlink())
1747 return got_error_from_errno2("open", abspath);
1748 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1749 if (lstat(abspath, sb) == -1)
1750 return got_error_from_errno2("lstat", abspath);
1751 } else if (fd == -1 || fstat(fd, sb) == -1) {
1752 if (errno == ENOENT) {
1753 if (got_fileindex_entry_has_file_on_disk(ie))
1754 *status = GOT_STATUS_MISSING;
1756 *status = GOT_STATUS_DELETE;
1759 err = got_error_from_errno2("fstat", abspath);
1764 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1765 *status = GOT_STATUS_OBSTRUCTED;
1769 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1770 *status = GOT_STATUS_DELETE;
1772 } else if (!got_fileindex_entry_has_blob(ie) &&
1773 staged_status != GOT_STATUS_ADD) {
1774 *status = GOT_STATUS_ADD;
1778 if (!stat_info_differs(ie, sb))
1781 if (S_ISLNK(sb->st_mode) &&
1782 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1783 *status = GOT_STATUS_MODIFY;
1787 if (staged_status == GOT_STATUS_MODIFY ||
1788 staged_status == GOT_STATUS_ADD)
1789 got_fileindex_entry_get_staged_blob_id(&id, ie);
1791 got_fileindex_entry_get_blob_id(&id, ie);
1793 fd1 = got_opentempfd();
1795 err = got_error_from_errno("got_opentempfd");
1798 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1802 if (S_ISLNK(sb->st_mode)) {
1803 err = get_symlink_modification_status(status, ie,
1804 abspath, dirfd, de_name, blob);
1809 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1811 err = got_error_from_errno2("openat", abspath);
1816 f = fdopen(fd, "r");
1818 err = got_error_from_errno2("fdopen", abspath);
1822 hdrlen = got_object_blob_get_hdrlen(blob);
1824 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1825 err = got_object_blob_read_block(&blen, blob);
1828 /* Skip length of blob object header first time around. */
1829 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1830 if (flen == 0 && ferror(f)) {
1831 err = got_error_from_errno("fread");
1834 if (blen - hdrlen == 0) {
1836 *status = GOT_STATUS_MODIFY;
1838 } else if (flen == 0) {
1839 if (blen - hdrlen != 0)
1840 *status = GOT_STATUS_MODIFY;
1842 } else if (blen - hdrlen == flen) {
1843 /* Skip blob object header first time around. */
1844 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1845 *status = GOT_STATUS_MODIFY;
1849 *status = GOT_STATUS_MODIFY;
1855 if (*status == GOT_STATUS_MODIFY) {
1857 err = get_modified_file_content_status(status, blob, ie->path,
1859 } else if (xbit_differs(ie, sb->st_mode))
1860 *status = GOT_STATUS_MODE_CHANGE;
1862 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1863 err = got_error_from_errno("close");
1865 got_object_blob_close(blob);
1866 if (f != NULL && fclose(f) == EOF && err == NULL)
1867 err = got_error_from_errno2("fclose", abspath);
1868 if (fd != -1 && close(fd) == -1 && err == NULL)
1869 err = got_error_from_errno2("close", abspath);
1874 * Update timestamps in the file index if a file is unmodified and
1875 * we had to run a full content comparison to find out.
1877 static const struct got_error *
1878 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1879 struct got_fileindex_entry *ie, struct stat *sb)
1881 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1882 return got_fileindex_entry_update(ie, wt_fd, path,
1883 ie->blob_sha1, ie->commit_sha1, 1);
1888 static const struct got_error *
1889 update_blob(struct got_worktree *worktree,
1890 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1891 struct got_tree_entry *te, const char *path,
1892 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1895 const struct got_error *err = NULL;
1896 struct got_blob_object *blob = NULL;
1897 char *ondisk_path = NULL;
1898 unsigned char status = GOT_STATUS_NO_CHANGE;
1900 int fd1 = -1, fd2 = -1;
1902 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1903 return got_error_from_errno("asprintf");
1906 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1907 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1910 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1914 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1915 sb.st_mode = got_fileindex_perms_to_st(ie);
1917 if (stat(ondisk_path, &sb) == -1) {
1918 if (errno != ENOENT) {
1919 err = got_error_from_errno2("stat",
1923 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1924 status = GOT_STATUS_UNVERSIONED;
1926 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1927 status = GOT_STATUS_UNVERSIONED;
1929 status = GOT_STATUS_OBSTRUCTED;
1933 if (status == GOT_STATUS_OBSTRUCTED) {
1935 got_fileindex_entry_mark_skipped(ie);
1936 err = (*progress_cb)(progress_arg, status, path);
1939 if (status == GOT_STATUS_CONFLICT) {
1941 got_fileindex_entry_mark_skipped(ie);
1942 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1947 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1948 (S_ISLNK(te->mode) ||
1949 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1951 * This is a regular file or an installed bad symlink.
1952 * If the file index indicates that this file is already
1953 * up-to-date with respect to the repository we can skip
1954 * updating contents of this file.
1956 if (got_fileindex_entry_has_commit(ie) &&
1957 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1958 SHA1_DIGEST_LENGTH) == 0) {
1960 err = sync_timestamps(worktree->root_fd,
1961 path, status, ie, &sb);
1964 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1968 if (got_fileindex_entry_has_blob(ie) &&
1969 memcmp(ie->blob_sha1, te->id.sha1,
1970 SHA1_DIGEST_LENGTH) == 0) {
1971 /* Different commit but the same blob. */
1972 err = sync_timestamps(worktree->root_fd,
1973 path, status, ie, &sb);
1976 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1982 fd1 = got_opentempfd();
1984 err = got_error_from_errno("got_opentempfd");
1987 err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
1991 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1992 int update_timestamps;
1993 struct got_blob_object *blob2 = NULL;
1994 char *label_orig = NULL;
1995 if (got_fileindex_entry_has_blob(ie)) {
1996 fd2 = got_opentempfd();
1998 err = got_error_from_errno("got_opentempfd");
2001 struct got_object_id id2;
2002 got_fileindex_entry_get_blob_id(&id2, ie);
2003 err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
2008 if (got_fileindex_entry_has_commit(ie)) {
2009 char id_str[SHA1_DIGEST_STRING_LENGTH];
2010 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2011 sizeof(id_str)) == NULL) {
2012 err = got_error_path(id_str,
2013 GOT_ERR_BAD_OBJ_ID_STR);
2016 if (asprintf(&label_orig, "%s: commit %s",
2017 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2018 err = got_error_from_errno("asprintf");
2022 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2024 err = got_object_blob_read_to_str(&link_target, blob);
2027 err = merge_symlink(worktree, blob2, ondisk_path, path,
2028 label_orig, link_target, worktree->base_commit_id,
2029 repo, progress_cb, progress_arg);
2032 err = merge_blob(&update_timestamps, worktree, blob2,
2033 ondisk_path, path, sb.st_mode, label_orig, blob,
2034 worktree->base_commit_id, repo,
2035 progress_cb, progress_arg);
2038 if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2039 err = got_error_from_errno("close");
2043 got_object_blob_close(blob2);
2047 * Do not update timestamps of files with local changes.
2048 * Otherwise, a future status walk would treat them as
2049 * unmodified files again.
2051 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2052 blob->id.sha1, worktree->base_commit_id->sha1,
2054 } else if (status == GOT_STATUS_MODE_CHANGE) {
2055 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2056 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2057 } else if (status == GOT_STATUS_DELETE) {
2058 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2061 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2062 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2066 int is_bad_symlink = 0;
2067 if (S_ISLNK(te->mode)) {
2068 err = install_symlink(&is_bad_symlink, worktree,
2069 ondisk_path, path, blob,
2070 status == GOT_STATUS_MISSING, 0,
2071 status == GOT_STATUS_UNVERSIONED, 0,
2072 repo, progress_cb, progress_arg);
2074 err = install_blob(worktree, ondisk_path, path,
2075 te->mode, sb.st_mode, blob,
2076 status == GOT_STATUS_MISSING, 0, 0,
2077 status == GOT_STATUS_UNVERSIONED, repo,
2078 progress_cb, progress_arg);
2084 err = got_fileindex_entry_update(ie,
2085 worktree->root_fd, path, blob->id.sha1,
2086 worktree->base_commit_id->sha1, 1);
2088 err = create_fileindex_entry(&ie, fileindex,
2089 worktree->base_commit_id, worktree->root_fd, path,
2095 if (is_bad_symlink) {
2096 got_fileindex_entry_filetype_set(ie,
2097 GOT_FILEIDX_MODE_BAD_SYMLINK);
2101 if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2102 err = got_error_from_errno("close");
2105 got_object_blob_close(blob);
2111 static const struct got_error *
2112 remove_ondisk_file(const char *root_path, const char *path)
2114 const struct got_error *err = NULL;
2115 char *ondisk_path = NULL, *parent = NULL;
2117 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2118 return got_error_from_errno("asprintf");
2120 if (unlink(ondisk_path) == -1) {
2121 if (errno != ENOENT)
2122 err = got_error_from_errno2("unlink", ondisk_path);
2124 size_t root_len = strlen(root_path);
2125 err = got_path_dirname(&parent, ondisk_path);
2128 while (got_path_cmp(parent, root_path,
2129 strlen(parent), root_len) != 0) {
2131 ondisk_path = parent;
2133 if (rmdir(ondisk_path) == -1) {
2134 if (errno != ENOTEMPTY)
2135 err = got_error_from_errno2("rmdir",
2139 err = got_path_dirname(&parent, ondisk_path);
2150 static const struct got_error *
2151 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2152 struct got_fileindex_entry *ie, struct got_repository *repo,
2153 got_worktree_checkout_cb progress_cb, void *progress_arg)
2155 const struct got_error *err = NULL;
2156 unsigned char status;
2160 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2161 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2163 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2165 return got_error_from_errno("asprintf");
2167 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2171 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2172 char ondisk_target[PATH_MAX];
2173 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2174 sizeof(ondisk_target));
2175 if (ondisk_len == -1) {
2176 err = got_error_from_errno2("readlink", ondisk_path);
2179 ondisk_target[ondisk_len] = '\0';
2180 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2181 NULL, NULL, /* XXX pass common ancestor info? */
2182 ondisk_target, ondisk_path);
2185 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2190 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2191 status == GOT_STATUS_ADD) {
2192 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2196 * Preserve the working file and change the deleted blob's
2197 * entry into a schedule-add entry.
2199 err = got_fileindex_entry_update(ie, worktree->root_fd,
2200 ie->path, NULL, NULL, 0);
2202 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2205 if (status == GOT_STATUS_NO_CHANGE) {
2206 err = remove_ondisk_file(worktree->root_path, ie->path);
2210 got_fileindex_entry_remove(fileindex, ie);
2217 struct diff_cb_arg {
2218 struct got_fileindex *fileindex;
2219 struct got_worktree *worktree;
2220 struct got_repository *repo;
2221 got_worktree_checkout_cb progress_cb;
2223 got_cancel_cb cancel_cb;
2227 static const struct got_error *
2228 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2229 struct got_tree_entry *te, const char *parent_path)
2231 struct diff_cb_arg *a = arg;
2233 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2234 return got_error(GOT_ERR_CANCELLED);
2236 return update_blob(a->worktree, a->fileindex, ie, te,
2237 ie->path, a->repo, a->progress_cb, a->progress_arg);
2240 static const struct got_error *
2241 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2243 struct diff_cb_arg *a = arg;
2245 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2246 return got_error(GOT_ERR_CANCELLED);
2248 return delete_blob(a->worktree, a->fileindex, ie,
2249 a->repo, a->progress_cb, a->progress_arg);
2252 static const struct got_error *
2253 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2255 struct diff_cb_arg *a = arg;
2256 const struct got_error *err;
2259 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2260 return got_error(GOT_ERR_CANCELLED);
2262 if (got_object_tree_entry_is_submodule(te))
2265 if (asprintf(&path, "%s%s%s", parent_path,
2266 parent_path[0] ? "/" : "", te->name)
2268 return got_error_from_errno("asprintf");
2270 if (S_ISDIR(te->mode))
2271 err = add_dir_on_disk(a->worktree, path);
2273 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2274 a->repo, a->progress_cb, a->progress_arg);
2280 const struct got_error *
2281 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2283 uint32_t uuid_status;
2285 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2286 if (uuid_status != uuid_s_ok) {
2288 return got_error_uuid(uuid_status, "uuid_to_string");
2294 static const struct got_error *
2295 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2297 const struct got_error *err = NULL;
2298 char *uuidstr = NULL;
2302 err = got_worktree_get_uuid(&uuidstr, worktree);
2306 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2307 err = got_error_from_errno("asprintf");
2314 const struct got_error *
2315 got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2318 return get_ref_name(refname, worktree, prefix);
2321 const struct got_error *
2322 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2324 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2327 static const struct got_error *
2328 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2330 return get_ref_name(refname, worktree,
2331 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2334 static const struct got_error *
2335 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2337 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2340 static const struct got_error *
2341 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2343 return get_ref_name(refname, worktree,
2344 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2347 static const struct got_error *
2348 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2350 return get_ref_name(refname, worktree,
2351 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2354 static const struct got_error *
2355 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2357 return get_ref_name(refname, worktree,
2358 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2361 static const struct got_error *
2362 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2364 return get_ref_name(refname, worktree,
2365 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2368 static const struct got_error *
2369 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2371 return get_ref_name(refname, worktree,
2372 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2375 static const struct got_error *
2376 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2378 return get_ref_name(refname, worktree,
2379 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2382 const struct got_error *
2383 got_worktree_get_histedit_script_path(char **path,
2384 struct got_worktree *worktree)
2386 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2387 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2389 return got_error_from_errno("asprintf");
2394 static const struct got_error *
2395 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2397 return get_ref_name(refname, worktree,
2398 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2401 static const struct got_error *
2402 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2404 return get_ref_name(refname, worktree,
2405 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2409 * Prevent Git's garbage collector from deleting our base commit by
2410 * setting a reference to our base commit's ID.
2412 static const struct got_error *
2413 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2415 const struct got_error *err = NULL;
2416 struct got_reference *ref = NULL;
2419 err = got_worktree_get_base_ref_name(&refname, worktree);
2423 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2427 err = got_ref_write(ref, repo);
2435 static const struct got_error *
2436 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2438 const struct got_error *err = NULL;
2440 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2441 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2442 err = got_error_from_errno("asprintf");
2443 *fileindex_path = NULL;
2449 static const struct got_error *
2450 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2451 struct got_worktree *worktree)
2453 const struct got_error *err = NULL;
2456 *fileindex_path = NULL;
2457 *fileindex = got_fileindex_alloc();
2458 if (*fileindex == NULL)
2459 return got_error_from_errno("got_fileindex_alloc");
2461 err = get_fileindex_path(fileindex_path, worktree);
2465 index = fopen(*fileindex_path, "rbe");
2466 if (index == NULL) {
2467 if (errno != ENOENT)
2468 err = got_error_from_errno2("fopen", *fileindex_path);
2470 err = got_fileindex_read(*fileindex, index);
2471 if (fclose(index) == EOF && err == NULL)
2472 err = got_error_from_errno("fclose");
2476 free(*fileindex_path);
2477 *fileindex_path = NULL;
2478 got_fileindex_free(*fileindex);
2484 struct bump_base_commit_id_arg {
2485 struct got_object_id *base_commit_id;
2488 const char *entry_name;
2489 got_worktree_checkout_cb progress_cb;
2493 static const struct got_error *
2494 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2496 const struct got_error *err;
2497 struct bump_base_commit_id_arg *a = arg;
2499 if (a->entry_name) {
2500 if (strcmp(ie->path, a->path) != 0)
2502 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2505 if (got_fileindex_entry_was_skipped(ie))
2508 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2509 SHA1_DIGEST_LENGTH) == 0)
2512 if (a->progress_cb) {
2513 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2518 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2522 /* Bump base commit ID of all files within an updated part of the work tree. */
2523 static const struct got_error *
2524 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2525 struct got_fileindex *fileindex,
2526 got_worktree_checkout_cb progress_cb, void *progress_arg)
2528 struct bump_base_commit_id_arg bbc_arg;
2530 bbc_arg.base_commit_id = worktree->base_commit_id;
2531 bbc_arg.entry_name = NULL;
2533 bbc_arg.path_len = 0;
2534 bbc_arg.progress_cb = progress_cb;
2535 bbc_arg.progress_arg = progress_arg;
2537 return got_fileindex_for_each_entry_safe(fileindex,
2538 bump_base_commit_id, &bbc_arg);
2541 static const struct got_error *
2542 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2544 const struct got_error *err = NULL;
2545 char *new_fileindex_path = NULL;
2546 FILE *new_index = NULL;
2547 struct timespec timeout;
2549 err = got_opentemp_named(&new_fileindex_path, &new_index,
2550 fileindex_path, "");
2554 err = got_fileindex_write(fileindex, new_index);
2558 if (rename(new_fileindex_path, fileindex_path) != 0) {
2559 err = got_error_from_errno3("rename", new_fileindex_path,
2561 unlink(new_fileindex_path);
2565 * Sleep for a short amount of time to ensure that files modified after
2566 * this program exits have a different time stamp from the one which
2567 * was recorded in the file index.
2570 timeout.tv_nsec = 1;
2571 nanosleep(&timeout, NULL);
2575 free(new_fileindex_path);
2579 static const struct got_error *
2580 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2581 struct got_object_id **tree_id, const char *wt_relpath,
2582 struct got_commit_object *base_commit, struct got_worktree *worktree,
2583 struct got_repository *repo)
2585 const struct got_error *err = NULL;
2586 struct got_object_id *id = NULL;
2587 char *in_repo_path = NULL;
2588 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2590 *entry_type = GOT_OBJ_TYPE_ANY;
2591 *tree_relpath = NULL;
2594 if (wt_relpath[0] == '\0') {
2595 /* Check out all files within the work tree. */
2596 *entry_type = GOT_OBJ_TYPE_TREE;
2597 *tree_relpath = strdup("");
2598 if (*tree_relpath == NULL) {
2599 err = got_error_from_errno("strdup");
2602 err = got_object_id_by_path(tree_id, repo, base_commit,
2603 worktree->path_prefix);
2609 /* Check out a subset of files in the work tree. */
2611 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2612 is_root_wt ? "" : "/", wt_relpath) == -1) {
2613 err = got_error_from_errno("asprintf");
2617 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2622 in_repo_path = NULL;
2624 err = got_object_get_type(entry_type, repo, id);
2628 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2629 /* Check out a single file. */
2630 if (strchr(wt_relpath, '/') == NULL) {
2631 /* Check out a single file in work tree's root dir. */
2632 in_repo_path = strdup(worktree->path_prefix);
2633 if (in_repo_path == NULL) {
2634 err = got_error_from_errno("strdup");
2637 *tree_relpath = strdup("");
2638 if (*tree_relpath == NULL) {
2639 err = got_error_from_errno("strdup");
2643 /* Check out a single file in a subdirectory. */
2644 err = got_path_dirname(tree_relpath, wt_relpath);
2647 if (asprintf(&in_repo_path, "%s%s%s",
2648 worktree->path_prefix, is_root_wt ? "" : "/",
2649 *tree_relpath) == -1) {
2650 err = got_error_from_errno("asprintf");
2654 err = got_object_id_by_path(tree_id, repo,
2655 base_commit, in_repo_path);
2657 /* Check out all files within a subdirectory. */
2658 *tree_id = got_object_id_dup(id);
2659 if (*tree_id == NULL) {
2660 err = got_error_from_errno("got_object_id_dup");
2663 *tree_relpath = strdup(wt_relpath);
2664 if (*tree_relpath == NULL) {
2665 err = got_error_from_errno("strdup");
2673 *entry_type = GOT_OBJ_TYPE_ANY;
2674 free(*tree_relpath);
2675 *tree_relpath = NULL;
2682 static const struct got_error *
2683 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2684 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2685 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2686 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2688 const struct got_error *err = NULL;
2689 struct got_commit_object *commit = NULL;
2690 struct got_tree_object *tree = NULL;
2691 struct got_fileindex_diff_tree_cb diff_cb;
2692 struct diff_cb_arg arg;
2694 err = ref_base_commit(worktree, repo);
2696 if (!(err->code == GOT_ERR_ERRNO &&
2697 (errno == EACCES || errno == EROFS)))
2699 err = (*progress_cb)(progress_arg,
2700 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2705 err = got_object_open_as_commit(&commit, repo,
2706 worktree->base_commit_id);
2710 err = got_object_open_as_tree(&tree, repo, tree_id);
2715 got_object_tree_find_entry(tree, entry_name) == NULL) {
2716 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2720 diff_cb.diff_old_new = diff_old_new;
2721 diff_cb.diff_old = diff_old;
2722 diff_cb.diff_new = diff_new;
2723 arg.fileindex = fileindex;
2724 arg.worktree = worktree;
2726 arg.progress_cb = progress_cb;
2727 arg.progress_arg = progress_arg;
2728 arg.cancel_cb = cancel_cb;
2729 arg.cancel_arg = cancel_arg;
2730 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2731 entry_name, repo, &diff_cb, &arg);
2734 got_object_tree_close(tree);
2736 got_object_commit_close(commit);
2740 const struct got_error *
2741 got_worktree_checkout_files(struct got_worktree *worktree,
2742 struct got_pathlist_head *paths, struct got_repository *repo,
2743 got_worktree_checkout_cb progress_cb, void *progress_arg,
2744 got_cancel_cb cancel_cb, void *cancel_arg)
2746 const struct got_error *err = NULL, *sync_err, *unlockerr;
2747 struct got_commit_object *commit = NULL;
2748 struct got_tree_object *tree = NULL;
2749 struct got_fileindex *fileindex = NULL;
2750 char *fileindex_path = NULL;
2751 struct got_pathlist_entry *pe;
2752 struct tree_path_data {
2753 STAILQ_ENTRY(tree_path_data) entry;
2754 struct got_object_id *tree_id;
2759 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2761 STAILQ_INIT(&tree_paths);
2763 err = lock_worktree(worktree, LOCK_EX);
2767 err = got_object_open_as_commit(&commit, repo,
2768 worktree->base_commit_id);
2772 /* Map all specified paths to in-repository trees. */
2773 TAILQ_FOREACH(pe, paths, entry) {
2774 tpd = malloc(sizeof(*tpd));
2776 err = got_error_from_errno("malloc");
2780 err = find_tree_entry_for_checkout(&tpd->entry_type,
2781 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2788 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2789 err = got_path_basename(&tpd->entry_name, pe->path);
2797 tpd->entry_name = NULL;
2799 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2803 * Read the file index.
2804 * Checking out files is supposed to be an idempotent operation.
2805 * If the on-disk file index is incomplete we will try to complete it.
2807 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2811 tpd = STAILQ_FIRST(&tree_paths);
2812 TAILQ_FOREACH(pe, paths, entry) {
2813 struct bump_base_commit_id_arg bbc_arg;
2815 err = checkout_files(worktree, fileindex, tpd->relpath,
2816 tpd->tree_id, tpd->entry_name, repo,
2817 progress_cb, progress_arg, cancel_cb, cancel_arg);
2821 bbc_arg.base_commit_id = worktree->base_commit_id;
2822 bbc_arg.entry_name = tpd->entry_name;
2823 bbc_arg.path = pe->path;
2824 bbc_arg.path_len = pe->path_len;
2825 bbc_arg.progress_cb = progress_cb;
2826 bbc_arg.progress_arg = progress_arg;
2827 err = got_fileindex_for_each_entry_safe(fileindex,
2828 bump_base_commit_id, &bbc_arg);
2832 tpd = STAILQ_NEXT(tpd, entry);
2834 sync_err = sync_fileindex(fileindex, fileindex_path);
2835 if (sync_err && err == NULL)
2838 free(fileindex_path);
2840 got_object_tree_close(tree);
2842 got_object_commit_close(commit);
2844 got_fileindex_free(fileindex);
2845 while (!STAILQ_EMPTY(&tree_paths)) {
2846 tpd = STAILQ_FIRST(&tree_paths);
2847 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2852 unlockerr = lock_worktree(worktree, LOCK_SH);
2853 if (unlockerr && err == NULL)
2858 struct merge_file_cb_arg {
2859 struct got_worktree *worktree;
2860 struct got_fileindex *fileindex;
2861 got_worktree_checkout_cb progress_cb;
2863 got_cancel_cb cancel_cb;
2865 const char *label_orig;
2866 struct got_object_id *commit_id2;
2867 int allow_bad_symlinks;
2870 static const struct got_error *
2871 merge_file_cb(void *arg, struct got_blob_object *blob1,
2872 struct got_blob_object *blob2, FILE *f1, FILE *f2,
2873 struct got_object_id *id1, struct got_object_id *id2,
2874 const char *path1, const char *path2,
2875 mode_t mode1, mode_t mode2, struct got_repository *repo)
2877 static const struct got_error *err = NULL;
2878 struct merge_file_cb_arg *a = arg;
2879 struct got_fileindex_entry *ie;
2880 char *ondisk_path = NULL;
2882 unsigned char status;
2883 int local_changes_subsumed;
2884 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2885 char *id_str = NULL, *label_deriv2 = NULL;
2887 if (blob1 && blob2) {
2888 ie = got_fileindex_entry_get(a->fileindex, path2,
2891 return (*a->progress_cb)(a->progress_arg,
2892 GOT_STATUS_MISSING, path2);
2894 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2896 return got_error_from_errno("asprintf");
2898 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2903 if (status == GOT_STATUS_DELETE) {
2904 err = (*a->progress_cb)(a->progress_arg,
2905 GOT_STATUS_MERGE, path2);
2908 if (status != GOT_STATUS_NO_CHANGE &&
2909 status != GOT_STATUS_MODIFY &&
2910 status != GOT_STATUS_CONFLICT &&
2911 status != GOT_STATUS_ADD) {
2912 err = (*a->progress_cb)(a->progress_arg, status, path2);
2916 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2918 err = got_object_blob_read_to_str(&link_target2, blob2);
2921 err = merge_symlink(a->worktree, blob1, ondisk_path,
2922 path2, a->label_orig, link_target2, a->commit_id2,
2923 repo, a->progress_cb, a->progress_arg);
2928 f_orig = got_opentemp();
2929 if (f_orig == NULL) {
2930 err = got_error_from_errno("got_opentemp");
2933 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2938 f_deriv2 = got_opentemp();
2939 if (f_deriv2 == NULL)
2941 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2946 fd = open(ondisk_path,
2947 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2949 err = got_error_from_errno2("open",
2953 f_deriv = fdopen(fd, "r");
2954 if (f_deriv == NULL) {
2955 err = got_error_from_errno2("fdopen",
2960 err = got_object_id_str(&id_str, a->commit_id2);
2963 if (asprintf(&label_deriv2, "%s: commit %s",
2964 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2965 err = got_error_from_errno("asprintf");
2968 err = merge_file(&local_changes_subsumed, a->worktree,
2969 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2970 mode2, a->label_orig, NULL, label_deriv2,
2971 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2972 a->progress_cb, a->progress_arg);
2975 ie = got_fileindex_entry_get(a->fileindex, path1,
2978 return (*a->progress_cb)(a->progress_arg,
2979 GOT_STATUS_MISSING, path1);
2981 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2983 return got_error_from_errno("asprintf");
2985 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2991 case GOT_STATUS_NO_CHANGE:
2992 err = (*a->progress_cb)(a->progress_arg,
2993 GOT_STATUS_DELETE, path1);
2996 err = remove_ondisk_file(a->worktree->root_path, path1);
3000 got_fileindex_entry_mark_deleted_from_disk(ie);
3002 case GOT_STATUS_DELETE:
3003 case GOT_STATUS_MISSING:
3004 err = (*a->progress_cb)(a->progress_arg,
3005 GOT_STATUS_DELETE, path1);
3009 got_fileindex_entry_mark_deleted_from_disk(ie);
3011 case GOT_STATUS_ADD: {
3012 struct got_object_id *id;
3016 * Delete the added file only if its content already
3017 * exists in the repository.
3019 err = got_object_blob_file_create(&id, &blob1_f,
3020 &blob1_size, path1);
3023 if (got_object_id_cmp(id, id1) == 0) {
3024 err = (*a->progress_cb)(a->progress_arg,
3025 GOT_STATUS_DELETE, path1);
3028 err = remove_ondisk_file(a->worktree->root_path,
3033 got_fileindex_entry_remove(a->fileindex,
3036 err = (*a->progress_cb)(a->progress_arg,
3037 GOT_STATUS_CANNOT_DELETE, path1);
3039 if (fclose(blob1_f) == EOF && err == NULL)
3040 err = got_error_from_errno("fclose");
3046 case GOT_STATUS_MODIFY:
3047 case GOT_STATUS_CONFLICT:
3048 err = (*a->progress_cb)(a->progress_arg,
3049 GOT_STATUS_CANNOT_DELETE, path1);
3053 case GOT_STATUS_OBSTRUCTED:
3054 err = (*a->progress_cb)(a->progress_arg, status, path1);
3062 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3064 return got_error_from_errno("asprintf");
3065 ie = got_fileindex_entry_get(a->fileindex, path2,
3068 err = get_file_status(&status, &sb, ie, ondisk_path,
3072 if (status != GOT_STATUS_NO_CHANGE &&
3073 status != GOT_STATUS_MODIFY &&
3074 status != GOT_STATUS_CONFLICT &&
3075 status != GOT_STATUS_ADD) {
3076 err = (*a->progress_cb)(a->progress_arg,
3080 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3082 err = got_object_blob_read_to_str(&link_target2,
3086 err = merge_symlink(a->worktree, NULL,
3087 ondisk_path, path2, a->label_orig,
3088 link_target2, a->commit_id2, repo,
3089 a->progress_cb, a->progress_arg);
3091 } else if (S_ISREG(sb.st_mode)) {
3092 err = merge_blob(&local_changes_subsumed,
3093 a->worktree, NULL, ondisk_path, path2,
3094 sb.st_mode, a->label_orig, blob2,
3095 a->commit_id2, repo, a->progress_cb,
3098 err = got_error_path(ondisk_path,
3099 GOT_ERR_FILE_OBSTRUCTED);
3103 if (status == GOT_STATUS_DELETE) {
3104 err = got_fileindex_entry_update(ie,
3105 a->worktree->root_fd, path2, blob2->id.sha1,
3106 a->worktree->base_commit_id->sha1, 0);
3111 int is_bad_symlink = 0;
3112 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3113 if (S_ISLNK(mode2)) {
3114 err = install_symlink(&is_bad_symlink,
3115 a->worktree, ondisk_path, path2, blob2, 0,
3116 0, 1, a->allow_bad_symlinks, repo,
3117 a->progress_cb, a->progress_arg);
3119 err = install_blob(a->worktree, ondisk_path, path2,
3120 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3121 a->progress_cb, a->progress_arg);
3125 err = got_fileindex_entry_alloc(&ie, path2);
3128 err = got_fileindex_entry_update(ie,
3129 a->worktree->root_fd, path2, NULL, NULL, 1);
3131 got_fileindex_entry_free(ie);
3134 err = got_fileindex_entry_add(a->fileindex, ie);
3136 got_fileindex_entry_free(ie);
3139 if (is_bad_symlink) {
3140 got_fileindex_entry_filetype_set(ie,
3141 GOT_FILEIDX_MODE_BAD_SYMLINK);
3146 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3147 err = got_error_from_errno("fclose");
3148 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3149 err = got_error_from_errno("fclose");
3150 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3151 err = got_error_from_errno("fclose");
3158 static const struct got_error *
3159 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3161 struct got_worktree *worktree = arg;
3163 /* Reject merges into a work tree with mixed base commits. */
3164 if (got_fileindex_entry_has_commit(ie) &&
3165 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3166 SHA1_DIGEST_LENGTH) != 0)
3167 return got_error(GOT_ERR_MIXED_COMMITS);
3172 struct check_merge_conflicts_arg {
3173 struct got_worktree *worktree;
3174 struct got_fileindex *fileindex;
3175 struct got_repository *repo;
3178 static const struct got_error *
3179 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3180 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3181 struct got_object_id *id1, struct got_object_id *id2,
3182 const char *path1, const char *path2,
3183 mode_t mode1, mode_t mode2, struct got_repository *repo)
3185 const struct got_error *err = NULL;
3186 struct check_merge_conflicts_arg *a = arg;
3187 unsigned char status;
3189 struct got_fileindex_entry *ie;
3190 const char *path = path2 ? path2 : path1;
3191 struct got_object_id *id = id2 ? id2 : id1;
3197 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3201 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3203 return got_error_from_errno("asprintf");
3205 /* Reject merges into a work tree with conflicted files. */
3206 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3210 if (status == GOT_STATUS_CONFLICT)
3211 return got_error(GOT_ERR_CONFLICTS);
3216 static const struct got_error *
3217 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3218 const char *fileindex_path, struct got_object_id *commit_id1,
3219 struct got_object_id *commit_id2, struct got_repository *repo,
3220 got_worktree_checkout_cb progress_cb, void *progress_arg,
3221 got_cancel_cb cancel_cb, void *cancel_arg)
3223 const struct got_error *err = NULL, *sync_err;
3224 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3225 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3226 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3227 struct check_merge_conflicts_arg cmc_arg;
3228 struct merge_file_cb_arg arg;
3229 char *label_orig = NULL;
3230 FILE *f1 = NULL, *f2 = NULL;
3231 int fd1 = -1, fd2 = -1;
3234 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3237 err = got_object_id_by_path(&tree_id1, repo, commit1,
3238 worktree->path_prefix);
3239 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3245 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3249 err = got_object_id_str(&id_str, commit_id1);
3253 if (asprintf(&label_orig, "%s: commit %s",
3254 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3255 err = got_error_from_errno("asprintf");
3261 f1 = got_opentemp();
3263 err = got_error_from_errno("got_opentemp");
3268 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3272 err = got_object_id_by_path(&tree_id2, repo, commit2,
3273 worktree->path_prefix);
3277 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3281 f2 = got_opentemp();
3283 err = got_error_from_errno("got_opentemp");
3287 fd1 = got_opentempfd();
3289 err = got_error_from_errno("got_opentempfd");
3293 fd2 = got_opentempfd();
3295 err = got_error_from_errno("got_opentempfd");
3299 cmc_arg.worktree = worktree;
3300 cmc_arg.fileindex = fileindex;
3301 cmc_arg.repo = repo;
3302 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3303 check_merge_conflicts, &cmc_arg, 0);
3307 arg.worktree = worktree;
3308 arg.fileindex = fileindex;
3309 arg.progress_cb = progress_cb;
3310 arg.progress_arg = progress_arg;
3311 arg.cancel_cb = cancel_cb;
3312 arg.cancel_arg = cancel_arg;
3313 arg.label_orig = label_orig;
3314 arg.commit_id2 = commit_id2;
3315 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3316 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3317 merge_file_cb, &arg, 1);
3318 sync_err = sync_fileindex(fileindex, fileindex_path);
3319 if (sync_err && err == NULL)
3323 got_object_commit_close(commit1);
3325 got_object_commit_close(commit2);
3327 got_object_tree_close(tree1);
3329 got_object_tree_close(tree2);
3330 if (f1 && fclose(f1) == EOF && err == NULL)
3331 err = got_error_from_errno("fclose");
3332 if (f2 && fclose(f2) == EOF && err == NULL)
3333 err = got_error_from_errno("fclose");
3334 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3335 err = got_error_from_errno("close");
3336 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3337 err = got_error_from_errno("close");
3342 const struct got_error *
3343 got_worktree_merge_files(struct got_worktree *worktree,
3344 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3345 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3346 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3348 const struct got_error *err, *unlockerr;
3349 char *fileindex_path = NULL;
3350 struct got_fileindex *fileindex = NULL;
3352 err = lock_worktree(worktree, LOCK_EX);
3356 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3360 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3365 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3366 commit_id2, repo, progress_cb, progress_arg,
3367 cancel_cb, cancel_arg);
3370 got_fileindex_free(fileindex);
3371 free(fileindex_path);
3372 unlockerr = lock_worktree(worktree, LOCK_SH);
3373 if (unlockerr && err == NULL)
3378 struct diff_dir_cb_arg {
3379 struct got_fileindex *fileindex;
3380 struct got_worktree *worktree;
3381 const char *status_path;
3382 size_t status_path_len;
3383 struct got_repository *repo;
3384 got_worktree_status_cb status_cb;
3386 got_cancel_cb cancel_cb;
3388 /* A pathlist containing per-directory pathlists of ignore patterns. */
3389 struct got_pathlist_head *ignores;
3390 int report_unchanged;
3394 static const struct got_error *
3395 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3396 int dirfd, const char *de_name,
3397 got_worktree_status_cb status_cb, void *status_arg,
3398 struct got_repository *repo, int report_unchanged)
3400 const struct got_error *err = NULL;
3401 unsigned char status = GOT_STATUS_NO_CHANGE;
3402 unsigned char staged_status;
3404 struct got_object_id blob_id, commit_id, staged_blob_id;
3405 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3406 struct got_object_id *staged_blob_idp = NULL;
3408 staged_status = get_staged_status(ie);
3409 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3413 if (status == GOT_STATUS_NO_CHANGE &&
3414 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3417 if (got_fileindex_entry_has_blob(ie))
3418 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3419 if (got_fileindex_entry_has_commit(ie))
3420 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3421 if (staged_status == GOT_STATUS_ADD ||
3422 staged_status == GOT_STATUS_MODIFY) {
3423 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3424 &staged_blob_id, ie);
3427 return (*status_cb)(status_arg, status, staged_status,
3428 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3431 static const struct got_error *
3432 status_old_new(void *arg, struct got_fileindex_entry *ie,
3433 struct dirent *de, const char *parent_path, int dirfd)
3435 const struct got_error *err = NULL;
3436 struct diff_dir_cb_arg *a = arg;
3439 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3440 return got_error(GOT_ERR_CANCELLED);
3442 if (got_path_cmp(parent_path, a->status_path,
3443 strlen(parent_path), a->status_path_len) != 0 &&
3444 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3447 if (parent_path[0]) {
3448 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3449 parent_path, de->d_name) == -1)
3450 return got_error_from_errno("asprintf");
3452 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3454 return got_error_from_errno("asprintf");
3457 err = report_file_status(ie, abspath, dirfd, de->d_name,
3458 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3463 static const struct got_error *
3464 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3466 struct diff_dir_cb_arg *a = arg;
3467 struct got_object_id blob_id, commit_id;
3468 unsigned char status;
3470 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3471 return got_error(GOT_ERR_CANCELLED);
3473 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3476 got_fileindex_entry_get_blob_id(&blob_id, ie);
3477 got_fileindex_entry_get_commit_id(&commit_id, ie);
3478 if (got_fileindex_entry_has_file_on_disk(ie))
3479 status = GOT_STATUS_MISSING;
3481 status = GOT_STATUS_DELETE;
3482 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3483 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3487 free_ignores(struct got_pathlist_head *ignores)
3489 struct got_pathlist_entry *pe;
3491 TAILQ_FOREACH(pe, ignores, entry) {
3492 struct got_pathlist_head *ignorelist = pe->data;
3494 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3496 got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3499 static const struct got_error *
3500 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3502 const struct got_error *err = NULL;
3503 struct got_pathlist_entry *pe = NULL;
3504 struct got_pathlist_head *ignorelist;
3505 char *line = NULL, *pattern, *dirpath = NULL;
3506 size_t linesize = 0;
3509 ignorelist = calloc(1, sizeof(*ignorelist));
3510 if (ignorelist == NULL)
3511 return got_error_from_errno("calloc");
3512 TAILQ_INIT(ignorelist);
3514 while ((linelen = getline(&line, &linesize, f)) != -1) {
3515 if (linelen > 0 && line[linelen - 1] == '\n')
3516 line[linelen - 1] = '\0';
3518 /* Git's ignores may contain comments. */
3522 /* Git's negated patterns are not (yet?) supported. */
3526 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3528 err = got_error_from_errno("asprintf");
3531 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3536 err = got_error_from_errno("getline");
3540 dirpath = strdup(path);
3541 if (dirpath == NULL) {
3542 err = got_error_from_errno("strdup");
3545 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3548 if (err || pe == NULL) {
3550 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3556 match_ignores(struct got_pathlist_head *ignores, const char *path)
3558 struct got_pathlist_entry *pe;
3560 /* Handle patterns which match in all directories. */
3561 TAILQ_FOREACH(pe, ignores, entry) {
3562 struct got_pathlist_head *ignorelist = pe->data;
3563 struct got_pathlist_entry *pi;
3565 TAILQ_FOREACH(pi, ignorelist, entry) {
3566 const char *p, *pattern = pi->path;
3568 if (strncmp(pattern, "**/", 3) != 0)
3573 if (fnmatch(pattern, p,
3574 FNM_PATHNAME | FNM_LEADING_DIR)) {
3575 /* Retry in next directory. */
3576 while (*p && *p != '/')
3588 * The ignores pathlist contains ignore lists from children before
3589 * parents, so we can find the most specific ignorelist by walking
3590 * ignores backwards.
3592 pe = TAILQ_LAST(ignores, got_pathlist_head);
3594 if (got_path_is_child(path, pe->path, pe->path_len)) {
3595 struct got_pathlist_head *ignorelist = pe->data;
3596 struct got_pathlist_entry *pi;
3597 TAILQ_FOREACH(pi, ignorelist, entry) {
3598 const char *pattern = pi->path;
3599 int flags = FNM_LEADING_DIR;
3600 if (strstr(pattern, "/**/") == NULL)
3601 flags |= FNM_PATHNAME;
3602 if (fnmatch(pattern, path, flags))
3607 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3613 static const struct got_error *
3614 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3615 const char *path, int dirfd, const char *ignores_filename)
3617 const struct got_error *err = NULL;
3620 FILE *ignoresfile = NULL;
3622 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3623 path[0] ? "/" : "", ignores_filename) == -1)
3624 return got_error_from_errno("asprintf");
3627 fd = openat(dirfd, ignores_filename,
3628 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3630 if (errno != ENOENT && errno != EACCES)
3631 err = got_error_from_errno2("openat",
3634 ignoresfile = fdopen(fd, "r");
3635 if (ignoresfile == NULL)
3636 err = got_error_from_errno2("fdopen",
3640 err = read_ignores(ignores, path, ignoresfile);
3644 ignoresfile = fopen(ignorespath, "re");
3645 if (ignoresfile == NULL) {
3646 if (errno != ENOENT && errno != EACCES)
3647 err = got_error_from_errno2("fopen",
3650 err = read_ignores(ignores, path, ignoresfile);
3653 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3654 err = got_error_from_errno2("fclose", path);
3655 if (fd != -1 && close(fd) == -1 && err == NULL)
3656 err = got_error_from_errno2("close", path);
3661 static const struct got_error *
3662 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3665 const struct got_error *err = NULL;
3666 struct diff_dir_cb_arg *a = arg;
3672 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3673 return got_error(GOT_ERR_CANCELLED);
3675 if (parent_path[0]) {
3676 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3677 return got_error_from_errno("asprintf");
3682 if (de->d_type == DT_DIR) {
3683 if (!a->no_ignores && ignore != NULL &&
3684 match_ignores(a->ignores, path))
3686 } else if (!match_ignores(a->ignores, path) &&
3687 got_path_is_child(path, a->status_path, a->status_path_len))
3688 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3689 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3695 static const struct got_error *
3696 status_traverse(void *arg, const char *path, int dirfd)
3698 const struct got_error *err = NULL;
3699 struct diff_dir_cb_arg *a = arg;
3704 err = add_ignores(a->ignores, a->worktree->root_path,
3705 path, dirfd, ".cvsignore");
3709 err = add_ignores(a->ignores, a->worktree->root_path, path,
3710 dirfd, ".gitignore");
3715 static const struct got_error *
3716 report_single_file_status(const char *path, const char *ondisk_path,
3717 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3718 void *status_arg, struct got_repository *repo, int report_unchanged,
3719 struct got_pathlist_head *ignores, int no_ignores)
3721 struct got_fileindex_entry *ie;
3724 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3726 return report_file_status(ie, ondisk_path, -1, NULL,
3727 status_cb, status_arg, repo, report_unchanged);
3729 if (lstat(ondisk_path, &sb) == -1) {
3730 if (errno != ENOENT)
3731 return got_error_from_errno2("lstat", ondisk_path);
3732 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3733 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3736 if (!no_ignores && match_ignores(ignores, path))
3739 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3740 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3741 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3746 static const struct got_error *
3747 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3748 const char *root_path, const char *path)
3750 const struct got_error *err;
3751 char *parent_path, *next_parent_path = NULL;
3753 err = add_ignores(ignores, root_path, "", -1,
3758 err = add_ignores(ignores, root_path, "", -1,
3763 err = got_path_dirname(&parent_path, path);
3765 if (err->code == GOT_ERR_BAD_PATH)
3766 return NULL; /* cannot traverse parent */
3770 err = add_ignores(ignores, root_path, parent_path, -1,
3774 err = add_ignores(ignores, root_path, parent_path, -1,
3778 err = got_path_dirname(&next_parent_path, parent_path);
3780 if (err->code == GOT_ERR_BAD_PATH)
3781 err = NULL; /* traversed everything */
3784 if (got_path_is_root_dir(parent_path))
3787 parent_path = next_parent_path;
3788 next_parent_path = NULL;
3792 free(next_parent_path);
3796 static const struct got_error *
3797 worktree_status(struct got_worktree *worktree, const char *path,
3798 struct got_fileindex *fileindex, struct got_repository *repo,
3799 got_worktree_status_cb status_cb, void *status_arg,
3800 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3801 int report_unchanged)
3803 const struct got_error *err = NULL;
3805 struct got_fileindex_diff_dir_cb fdiff_cb;
3806 struct diff_dir_cb_arg arg;
3807 char *ondisk_path = NULL;
3808 struct got_pathlist_head ignores;
3809 struct got_fileindex_entry *ie;
3811 TAILQ_INIT(&ignores);
3813 if (asprintf(&ondisk_path, "%s%s%s",
3814 worktree->root_path, path[0] ? "/" : "", path) == -1)
3815 return got_error_from_errno("asprintf");
3817 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3819 err = report_single_file_status(path, ondisk_path,
3820 fileindex, status_cb, status_arg, repo,
3821 report_unchanged, &ignores, no_ignores);
3825 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3827 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3828 !got_err_open_nofollow_on_symlink())
3829 err = got_error_from_errno2("open", ondisk_path);
3832 err = add_ignores_from_parent_paths(&ignores,
3833 worktree->root_path, ondisk_path);
3837 err = report_single_file_status(path, ondisk_path,
3838 fileindex, status_cb, status_arg, repo,
3839 report_unchanged, &ignores, no_ignores);
3842 fdiff_cb.diff_old_new = status_old_new;
3843 fdiff_cb.diff_old = status_old;
3844 fdiff_cb.diff_new = status_new;
3845 fdiff_cb.diff_traverse = status_traverse;
3846 arg.fileindex = fileindex;
3847 arg.worktree = worktree;
3848 arg.status_path = path;
3849 arg.status_path_len = strlen(path);
3851 arg.status_cb = status_cb;
3852 arg.status_arg = status_arg;
3853 arg.cancel_cb = cancel_cb;
3854 arg.cancel_arg = cancel_arg;
3855 arg.report_unchanged = report_unchanged;
3856 arg.no_ignores = no_ignores;
3858 err = add_ignores_from_parent_paths(&ignores,
3859 worktree->root_path, path);
3863 arg.ignores = &ignores;
3864 err = got_fileindex_diff_dir(fileindex, fd,
3865 worktree->root_path, path, repo, &fdiff_cb, &arg);
3868 free_ignores(&ignores);
3869 if (fd != -1 && close(fd) == -1 && err == NULL)
3870 err = got_error_from_errno("close");
3875 const struct got_error *
3876 got_worktree_status(struct got_worktree *worktree,
3877 struct got_pathlist_head *paths, struct got_repository *repo,
3878 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3879 got_cancel_cb cancel_cb, void *cancel_arg)
3881 const struct got_error *err = NULL;
3882 char *fileindex_path = NULL;
3883 struct got_fileindex *fileindex = NULL;
3884 struct got_pathlist_entry *pe;
3886 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3890 TAILQ_FOREACH(pe, paths, entry) {
3891 err = worktree_status(worktree, pe->path, fileindex, repo,
3892 status_cb, status_arg, cancel_cb, cancel_arg,
3897 free(fileindex_path);
3898 got_fileindex_free(fileindex);
3902 const struct got_error *
3903 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3906 const struct got_error *err = NULL;
3907 char *resolved = NULL, *cwd = NULL, *path = NULL;
3910 char *abspath = NULL;
3911 char canonpath[PATH_MAX];
3915 cwd = getcwd(NULL, 0);
3917 return got_error_from_errno("getcwd");
3919 if (lstat(arg, &sb) == -1) {
3920 if (errno != ENOENT) {
3921 err = got_error_from_errno2("lstat", arg);
3926 if (S_ISLNK(sb.st_mode)) {
3928 * We cannot use realpath(3) with symlinks since we want to
3929 * operate on the symlink itself.
3930 * But we can make the path absolute, assuming it is relative
3931 * to the current working directory, and then canonicalize it.
3933 if (!got_path_is_absolute(arg)) {
3934 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3935 err = got_error_from_errno("asprintf");
3940 err = got_canonpath(abspath ? abspath : arg, canonpath,
3944 resolved = strdup(canonpath);
3945 if (resolved == NULL) {
3946 err = got_error_from_errno("strdup");
3950 resolved = realpath(arg, NULL);
3951 if (resolved == NULL) {
3952 if (errno != ENOENT) {
3953 err = got_error_from_errno2("realpath", arg);
3956 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3957 err = got_error_from_errno("asprintf");
3960 err = got_canonpath(abspath, canonpath,
3964 resolved = strdup(canonpath);
3965 if (resolved == NULL) {
3966 err = got_error_from_errno("strdup");
3972 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3973 strlen(got_worktree_get_root_path(worktree)))) {
3974 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3978 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3979 err = got_path_skip_common_ancestor(&path,
3980 got_worktree_get_root_path(worktree), resolved);
3986 err = got_error_from_errno("strdup");
3991 /* XXX status walk can't deal with trailing slash! */
3993 while (len > 0 && path[len - 1] == '/') {
3994 path[len - 1] = '\0';
4008 struct schedule_addition_args {
4009 struct got_worktree *worktree;
4010 struct got_fileindex *fileindex;
4011 got_worktree_checkout_cb progress_cb;
4013 struct got_repository *repo;
4016 static const struct got_error *
4017 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4018 const char *relpath, struct got_object_id *blob_id,
4019 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4020 int dirfd, const char *de_name)
4022 struct schedule_addition_args *a = arg;
4023 const struct got_error *err = NULL;
4024 struct got_fileindex_entry *ie;
4028 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4030 return got_error_from_errno("asprintf");
4032 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4034 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4038 /* Re-adding an existing entry is a no-op. */
4039 if (status == GOT_STATUS_ADD)
4041 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4046 if (status != GOT_STATUS_UNVERSIONED) {
4047 if (status == GOT_STATUS_NONEXISTENT)
4048 err = got_error_set_errno(ENOENT, ondisk_path);
4050 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4054 err = got_fileindex_entry_alloc(&ie, relpath);
4057 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4058 relpath, NULL, NULL, 1);
4060 got_fileindex_entry_free(ie);
4063 err = got_fileindex_entry_add(a->fileindex, ie);
4065 got_fileindex_entry_free(ie);
4072 if (status == GOT_STATUS_ADD)
4074 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4077 const struct got_error *
4078 got_worktree_schedule_add(struct got_worktree *worktree,
4079 struct got_pathlist_head *paths,
4080 got_worktree_checkout_cb progress_cb, void *progress_arg,
4081 struct got_repository *repo, int no_ignores)
4083 struct got_fileindex *fileindex = NULL;
4084 char *fileindex_path = NULL;
4085 const struct got_error *err = NULL, *sync_err, *unlockerr;
4086 struct got_pathlist_entry *pe;
4087 struct schedule_addition_args saa;
4089 err = lock_worktree(worktree, LOCK_EX);
4093 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4097 saa.worktree = worktree;
4098 saa.fileindex = fileindex;
4099 saa.progress_cb = progress_cb;
4100 saa.progress_arg = progress_arg;
4103 TAILQ_FOREACH(pe, paths, entry) {
4104 err = worktree_status(worktree, pe->path, fileindex, repo,
4105 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4109 sync_err = sync_fileindex(fileindex, fileindex_path);
4110 if (sync_err && err == NULL)
4113 free(fileindex_path);
4115 got_fileindex_free(fileindex);
4116 unlockerr = lock_worktree(worktree, LOCK_SH);
4117 if (unlockerr && err == NULL)
4122 struct schedule_deletion_args {
4123 struct got_worktree *worktree;
4124 struct got_fileindex *fileindex;
4125 got_worktree_delete_cb progress_cb;
4127 struct got_repository *repo;
4128 int delete_local_mods;
4130 int ignore_missing_paths;
4131 const char *status_codes;
4134 static const struct got_error *
4135 schedule_for_deletion(void *arg, unsigned char status,
4136 unsigned char staged_status, const char *relpath,
4137 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4138 struct got_object_id *commit_id, int dirfd, const char *de_name)
4140 struct schedule_deletion_args *a = arg;
4141 const struct got_error *err = NULL;
4142 struct got_fileindex_entry *ie = NULL;
4146 if (status == GOT_STATUS_NONEXISTENT) {
4147 if (a->ignore_missing_paths)
4149 return got_error_set_errno(ENOENT, relpath);
4152 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4154 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4156 staged_status = get_staged_status(ie);
4157 if (staged_status != GOT_STATUS_NO_CHANGE) {
4158 if (staged_status == GOT_STATUS_DELETE)
4160 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4163 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4165 return got_error_from_errno("asprintf");
4167 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4172 if (a->status_codes) {
4173 size_t ncodes = strlen(a->status_codes);
4175 for (i = 0; i < ncodes ; i++) {
4176 if (status == a->status_codes[i])
4180 /* Do not delete files in non-matching status. */
4184 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4185 a->status_codes[i] != GOT_STATUS_MISSING) {
4186 static char msg[64];
4187 snprintf(msg, sizeof(msg),
4188 "invalid status code '%c'", a->status_codes[i]);
4189 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4194 if (status != GOT_STATUS_NO_CHANGE) {
4195 if (status == GOT_STATUS_DELETE)
4197 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4198 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4201 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4202 err = got_error_set_errno(ENOENT, relpath);
4205 if (status != GOT_STATUS_MODIFY &&
4206 status != GOT_STATUS_MISSING) {
4207 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4212 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4216 if (unlinkat(dirfd, de_name, 0) == -1) {
4217 err = got_error_from_errno2("unlinkat",
4221 } else if (unlink(ondisk_path) == -1) {
4222 err = got_error_from_errno2("unlink", ondisk_path);
4226 root_len = strlen(a->worktree->root_path);
4229 err = got_path_dirname(&parent, ondisk_path);
4233 ondisk_path = parent;
4234 if (rmdir(ondisk_path) == -1) {
4235 if (errno != ENOTEMPTY)
4236 err = got_error_from_errno2("rmdir",
4240 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4241 strlen(ondisk_path), root_len) != 0);
4244 got_fileindex_entry_mark_deleted_from_disk(ie);
4249 if (status == GOT_STATUS_DELETE)
4251 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4252 staged_status, relpath);
4255 const struct got_error *
4256 got_worktree_schedule_delete(struct got_worktree *worktree,
4257 struct got_pathlist_head *paths, int delete_local_mods,
4258 const char *status_codes,
4259 got_worktree_delete_cb progress_cb, void *progress_arg,
4260 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4262 struct got_fileindex *fileindex = NULL;
4263 char *fileindex_path = NULL;
4264 const struct got_error *err = NULL, *sync_err, *unlockerr;
4265 struct got_pathlist_entry *pe;
4266 struct schedule_deletion_args sda;
4268 err = lock_worktree(worktree, LOCK_EX);
4272 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4276 sda.worktree = worktree;
4277 sda.fileindex = fileindex;
4278 sda.progress_cb = progress_cb;
4279 sda.progress_arg = progress_arg;
4281 sda.delete_local_mods = delete_local_mods;
4282 sda.keep_on_disk = keep_on_disk;
4283 sda.ignore_missing_paths = ignore_missing_paths;
4284 sda.status_codes = status_codes;
4286 TAILQ_FOREACH(pe, paths, entry) {
4287 err = worktree_status(worktree, pe->path, fileindex, repo,
4288 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4292 sync_err = sync_fileindex(fileindex, fileindex_path);
4293 if (sync_err && err == NULL)
4296 free(fileindex_path);
4298 got_fileindex_free(fileindex);
4299 unlockerr = lock_worktree(worktree, LOCK_SH);
4300 if (unlockerr && err == NULL)
4305 static const struct got_error *
4306 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4308 const struct got_error *err = NULL;
4310 size_t linesize = 0, n;
4313 linelen = getline(&line, &linesize, infile);
4314 if (linelen == -1) {
4315 if (ferror(infile)) {
4316 err = got_error_from_errno("getline");
4322 n = fwrite(line, 1, linelen, outfile);
4324 err = got_ferror(outfile, GOT_ERR_IO);
4329 n = fwrite(line, 1, linelen, rejectfile);
4331 err = got_ferror(rejectfile, GOT_ERR_IO);
4338 static const struct got_error *
4339 skip_one_line(FILE *f)
4342 size_t linesize = 0;
4345 linelen = getline(&line, &linesize, f);
4346 if (linelen == -1) {
4348 return got_error_from_errno("getline");
4355 static const struct got_error *
4356 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4357 int start_old, int end_old, int start_new, int end_new,
4358 FILE *outfile, FILE *rejectfile)
4360 const struct got_error *err;
4362 /* Copy old file's lines leading up to patch. */
4363 while (!feof(f1) && *line_cur1 < start_old) {
4364 err = copy_one_line(f1, outfile, NULL);
4369 /* Skip new file's lines leading up to patch. */
4370 while (!feof(f2) && *line_cur2 < start_new) {
4372 err = copy_one_line(f2, NULL, rejectfile);
4374 err = skip_one_line(f2);
4379 /* Copy patched lines. */
4380 while (!feof(f2) && *line_cur2 <= end_new) {
4381 err = copy_one_line(f2, outfile, NULL);
4386 /* Skip over old file's replaced lines. */
4387 while (!feof(f1) && *line_cur1 <= end_old) {
4389 err = copy_one_line(f1, NULL, rejectfile);
4391 err = skip_one_line(f1);
4400 static const struct got_error *
4401 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4402 FILE *outfile, FILE *rejectfile)
4404 const struct got_error *err;
4407 /* Copy old file's lines until EOF. */
4409 err = copy_one_line(f1, outfile, NULL);
4416 /* Copy new file's lines until EOF. */
4418 err = copy_one_line(f2, NULL, rejectfile);
4428 static const struct got_error *
4429 apply_or_reject_change(int *choice, int *nchunks_used,
4430 struct diff_result *diff_result, int n,
4431 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4432 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4433 got_worktree_patch_cb patch_cb, void *patch_arg)
4435 const struct got_error *err = NULL;
4436 struct diff_chunk_context cc = {};
4437 int start_old, end_old, start_new, end_new;
4439 struct diff_output_unidiff_state *diff_state;
4440 struct diff_input_info diff_info;
4443 *choice = GOT_PATCH_CHOICE_NONE;
4445 /* Get changed line numbers without context lines for copy_change(). */
4446 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4447 start_old = cc.left.start;
4448 end_old = cc.left.end;
4449 start_new = cc.right.start;
4450 end_new = cc.right.end;
4452 /* Get the same change with context lines for display. */
4453 memset(&cc, 0, sizeof(cc));
4454 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4456 memset(&diff_info, 0, sizeof(diff_info));
4457 diff_info.left_path = relpath;
4458 diff_info.right_path = relpath;
4460 diff_state = diff_output_unidiff_state_alloc();
4461 if (diff_state == NULL)
4462 return got_error_set_errno(ENOMEM,
4463 "diff_output_unidiff_state_alloc");
4465 hunkfile = got_opentemp();
4466 if (hunkfile == NULL) {
4467 err = got_error_from_errno("got_opentemp");
4471 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4473 if (rc != DIFF_RC_OK) {
4474 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4478 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4479 err = got_ferror(hunkfile, GOT_ERR_IO);
4483 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4484 hunkfile, changeno, nchanges);
4489 case GOT_PATCH_CHOICE_YES:
4490 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4491 end_old, start_new, end_new, outfile, rejectfile);
4493 case GOT_PATCH_CHOICE_NO:
4494 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4495 end_old, start_new, end_new, rejectfile, outfile);
4497 case GOT_PATCH_CHOICE_QUIT:
4500 err = got_error(GOT_ERR_PATCH_CHOICE);
4504 diff_output_unidiff_state_free(diff_state);
4505 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4506 err = got_error_from_errno("fclose");
4510 struct revert_file_args {
4511 struct got_worktree *worktree;
4512 struct got_fileindex *fileindex;
4513 got_worktree_checkout_cb progress_cb;
4515 got_worktree_patch_cb patch_cb;
4517 struct got_repository *repo;
4518 int unlink_added_files;
4521 static const struct got_error *
4522 create_patched_content(char **path_outfile, int reverse_patch,
4523 struct got_object_id *blob_id, const char *path2,
4524 int dirfd2, const char *de_name2,
4525 const char *relpath, struct got_repository *repo,
4526 got_worktree_patch_cb patch_cb, void *patch_arg)
4528 const struct got_error *err, *free_err;
4529 struct got_blob_object *blob = NULL;
4530 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4531 int fd = -1, fd2 = -1;
4532 char link_target[PATH_MAX];
4533 ssize_t link_len = 0;
4534 char *path1 = NULL, *id_str = NULL;
4536 struct got_diffreg_result *diffreg_result = NULL;
4537 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4538 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4540 *path_outfile = NULL;
4542 err = got_object_id_str(&id_str, blob_id);
4547 fd2 = openat(dirfd2, de_name2,
4548 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4550 if (!got_err_open_nofollow_on_symlink()) {
4551 err = got_error_from_errno2("openat", path2);
4554 link_len = readlinkat(dirfd2, de_name2,
4555 link_target, sizeof(link_target));
4556 if (link_len == -1) {
4557 return got_error_from_errno2("readlinkat",
4560 sb2.st_mode = S_IFLNK;
4561 sb2.st_size = link_len;
4564 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4566 if (!got_err_open_nofollow_on_symlink()) {
4567 err = got_error_from_errno2("open", path2);
4570 link_len = readlink(path2, link_target,
4571 sizeof(link_target));
4573 return got_error_from_errno2("readlink", path2);
4574 sb2.st_mode = S_IFLNK;
4575 sb2.st_size = link_len;
4579 if (fstat(fd2, &sb2) == -1) {
4580 err = got_error_from_errno2("fstat", path2);
4584 f2 = fdopen(fd2, "r");
4586 err = got_error_from_errno2("fdopen", path2);
4592 f2 = got_opentemp();
4594 err = got_error_from_errno2("got_opentemp", path2);
4597 n = fwrite(link_target, 1, link_len, f2);
4598 if (n != link_len) {
4599 err = got_ferror(f2, GOT_ERR_IO);
4602 if (fflush(f2) == EOF) {
4603 err = got_error_from_errno("fflush");
4609 fd = got_opentempfd();
4611 err = got_error_from_errno("got_opentempfd");
4615 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4619 err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4623 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4627 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4628 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4632 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4637 if (fseek(f1, 0L, SEEK_SET) == -1)
4638 return got_ferror(f1, GOT_ERR_IO);
4639 if (fseek(f2, 0L, SEEK_SET) == -1)
4640 return got_ferror(f2, GOT_ERR_IO);
4642 /* Count the number of actual changes in the diff result. */
4643 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4644 struct diff_chunk_context cc = {};
4645 diff_chunk_context_load_change(&cc, &nchunks_used,
4646 diffreg_result->result, n, 0);
4649 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4651 err = apply_or_reject_change(&choice, &nchunks_used,
4652 diffreg_result->result, n, relpath, f1, f2,
4653 &line_cur1, &line_cur2,
4654 reverse_patch ? NULL : outfile,
4655 reverse_patch ? outfile : NULL,
4656 ++i, nchanges, patch_cb, patch_arg);
4659 if (choice == GOT_PATCH_CHOICE_YES)
4661 else if (choice == GOT_PATCH_CHOICE_QUIT)
4665 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4666 reverse_patch ? NULL : outfile,
4667 reverse_patch ? outfile : NULL);
4671 if (!S_ISLNK(sb2.st_mode)) {
4674 mode = apply_umask(sb2.st_mode);
4675 if (fchmod(fileno(outfile), mode) == -1) {
4676 err = got_error_from_errno2("fchmod", path2);
4683 if (fd != -1 && close(fd) == -1 && err == NULL)
4684 err = got_error_from_errno("close");
4686 got_object_blob_close(blob);
4687 free_err = got_diffreg_result_free(diffreg_result);
4690 if (f1 && fclose(f1) == EOF && err == NULL)
4691 err = got_error_from_errno2("fclose", path1);
4692 if (f2 && fclose(f2) == EOF && err == NULL)
4693 err = got_error_from_errno2("fclose", path2);
4694 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4695 err = got_error_from_errno2("close", path2);
4696 if (outfile && fclose(outfile) == EOF && err == NULL)
4697 err = got_error_from_errno2("fclose", *path_outfile);
4698 if (path1 && unlink(path1) == -1 && err == NULL)
4699 err = got_error_from_errno2("unlink", path1);
4700 if (err || !have_content) {
4701 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4702 err = got_error_from_errno2("unlink", *path_outfile);
4703 free(*path_outfile);
4704 *path_outfile = NULL;
4710 static const struct got_error *
4711 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4712 const char *relpath, struct got_object_id *blob_id,
4713 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4714 int dirfd, const char *de_name)
4716 struct revert_file_args *a = arg;
4717 const struct got_error *err = NULL;
4718 char *parent_path = NULL;
4719 struct got_fileindex_entry *ie;
4720 struct got_commit_object *base_commit = NULL;
4721 struct got_tree_object *tree = NULL;
4722 struct got_object_id *tree_id = NULL;
4723 const struct got_tree_entry *te = NULL;
4724 char *tree_path = NULL, *te_name;
4725 char *ondisk_path = NULL, *path_content = NULL;
4726 struct got_blob_object *blob = NULL;
4729 /* Reverting a staged deletion is a no-op. */
4730 if (status == GOT_STATUS_DELETE &&
4731 staged_status != GOT_STATUS_NO_CHANGE)
4734 if (status == GOT_STATUS_UNVERSIONED)
4735 return (*a->progress_cb)(a->progress_arg,
4736 GOT_STATUS_UNVERSIONED, relpath);
4738 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4740 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4742 /* Construct in-repository path of tree which contains this blob. */
4743 err = got_path_dirname(&parent_path, ie->path);
4745 if (err->code != GOT_ERR_BAD_PATH)
4747 parent_path = strdup("/");
4748 if (parent_path == NULL) {
4749 err = got_error_from_errno("strdup");
4753 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4754 tree_path = strdup(parent_path);
4755 if (tree_path == NULL) {
4756 err = got_error_from_errno("strdup");
4760 if (got_path_is_root_dir(parent_path)) {
4761 tree_path = strdup(a->worktree->path_prefix);
4762 if (tree_path == NULL) {
4763 err = got_error_from_errno("strdup");
4767 if (asprintf(&tree_path, "%s/%s",
4768 a->worktree->path_prefix, parent_path) == -1) {
4769 err = got_error_from_errno("asprintf");
4775 err = got_object_open_as_commit(&base_commit, a->repo,
4776 a->worktree->base_commit_id);
4780 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4782 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4783 (status == GOT_STATUS_ADD ||
4784 staged_status == GOT_STATUS_ADD)))
4787 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4791 err = got_path_basename(&te_name, ie->path);
4795 te = got_object_tree_find_entry(tree, te_name);
4797 if (te == NULL && status != GOT_STATUS_ADD &&
4798 staged_status != GOT_STATUS_ADD) {
4799 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4805 case GOT_STATUS_ADD:
4807 int choice = GOT_PATCH_CHOICE_NONE;
4808 err = (*a->patch_cb)(&choice, a->patch_arg,
4809 status, ie->path, NULL, 1, 1);
4812 if (choice != GOT_PATCH_CHOICE_YES)
4815 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4819 got_fileindex_entry_remove(a->fileindex, ie);
4820 if (a->unlink_added_files) {
4821 if (asprintf(&ondisk_path, "%s/%s",
4822 got_worktree_get_root_path(a->worktree),
4824 err = got_error_from_errno("asprintf");
4827 if (unlink(ondisk_path) == -1) {
4828 err = got_error_from_errno2("unlink",
4834 case GOT_STATUS_DELETE:
4836 int choice = GOT_PATCH_CHOICE_NONE;
4837 err = (*a->patch_cb)(&choice, a->patch_arg,
4838 status, ie->path, NULL, 1, 1);
4841 if (choice != GOT_PATCH_CHOICE_YES)
4845 case GOT_STATUS_MODIFY:
4846 case GOT_STATUS_MODE_CHANGE:
4847 case GOT_STATUS_CONFLICT:
4848 case GOT_STATUS_MISSING: {
4849 struct got_object_id id;
4850 if (staged_status == GOT_STATUS_ADD ||
4851 staged_status == GOT_STATUS_MODIFY)
4852 got_fileindex_entry_get_staged_blob_id(&id, ie);
4854 got_fileindex_entry_get_blob_id(&id, ie);
4855 fd = got_opentempfd();
4857 err = got_error_from_errno("got_opentempfd");
4861 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4865 if (asprintf(&ondisk_path, "%s/%s",
4866 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4867 err = got_error_from_errno("asprintf");
4871 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4872 status == GOT_STATUS_CONFLICT)) {
4873 int is_bad_symlink = 0;
4874 err = create_patched_content(&path_content, 1, &id,
4875 ondisk_path, dirfd, de_name, ie->path, a->repo,
4876 a->patch_cb, a->patch_arg);
4877 if (err || path_content == NULL)
4879 if (te && S_ISLNK(te->mode)) {
4880 if (unlink(path_content) == -1) {
4881 err = got_error_from_errno2("unlink",
4885 err = install_symlink(&is_bad_symlink,
4886 a->worktree, ondisk_path, ie->path,
4887 blob, 0, 1, 0, 0, a->repo,
4888 a->progress_cb, a->progress_arg);
4890 if (rename(path_content, ondisk_path) == -1) {
4891 err = got_error_from_errno3("rename",
4892 path_content, ondisk_path);
4897 int is_bad_symlink = 0;
4898 if (te && S_ISLNK(te->mode)) {
4899 err = install_symlink(&is_bad_symlink,
4900 a->worktree, ondisk_path, ie->path,
4901 blob, 0, 1, 0, 0, a->repo,
4902 a->progress_cb, a->progress_arg);
4904 err = install_blob(a->worktree, ondisk_path,
4906 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4907 got_fileindex_perms_to_st(ie), blob,
4908 0, 1, 0, 0, a->repo,
4909 a->progress_cb, a->progress_arg);
4913 if (status == GOT_STATUS_DELETE ||
4914 status == GOT_STATUS_MODE_CHANGE) {
4915 err = got_fileindex_entry_update(ie,
4916 a->worktree->root_fd, relpath,
4918 a->worktree->base_commit_id->sha1, 1);
4922 if (is_bad_symlink) {
4923 got_fileindex_entry_filetype_set(ie,
4924 GOT_FILEIDX_MODE_BAD_SYMLINK);
4937 if (fd != -1 && close(fd) == -1 && err == NULL)
4938 err = got_error_from_errno("close");
4940 got_object_blob_close(blob);
4942 got_object_tree_close(tree);
4945 got_object_commit_close(base_commit);
4949 const struct got_error *
4950 got_worktree_revert(struct got_worktree *worktree,
4951 struct got_pathlist_head *paths,
4952 got_worktree_checkout_cb progress_cb, void *progress_arg,
4953 got_worktree_patch_cb patch_cb, void *patch_arg,
4954 struct got_repository *repo)
4956 struct got_fileindex *fileindex = NULL;
4957 char *fileindex_path = NULL;
4958 const struct got_error *err = NULL, *unlockerr = NULL;
4959 const struct got_error *sync_err = NULL;
4960 struct got_pathlist_entry *pe;
4961 struct revert_file_args rfa;
4963 err = lock_worktree(worktree, LOCK_EX);
4967 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4971 rfa.worktree = worktree;
4972 rfa.fileindex = fileindex;
4973 rfa.progress_cb = progress_cb;
4974 rfa.progress_arg = progress_arg;
4975 rfa.patch_cb = patch_cb;
4976 rfa.patch_arg = patch_arg;
4978 rfa.unlink_added_files = 0;
4979 TAILQ_FOREACH(pe, paths, entry) {
4980 err = worktree_status(worktree, pe->path, fileindex, repo,
4981 revert_file, &rfa, NULL, NULL, 1, 0);
4985 sync_err = sync_fileindex(fileindex, fileindex_path);
4986 if (sync_err && err == NULL)
4989 free(fileindex_path);
4991 got_fileindex_free(fileindex);
4992 unlockerr = lock_worktree(worktree, LOCK_SH);
4993 if (unlockerr && err == NULL)
4999 free_commitable(struct got_commitable *ct)
5002 free(ct->in_repo_path);
5003 free(ct->ondisk_path);
5005 free(ct->base_blob_id);
5006 free(ct->staged_blob_id);
5007 free(ct->base_commit_id);
5011 struct collect_commitables_arg {
5012 struct got_pathlist_head *commitable_paths;
5013 struct got_repository *repo;
5014 struct got_worktree *worktree;
5015 struct got_fileindex *fileindex;
5016 int have_staged_files;
5017 int allow_bad_symlinks;
5018 int diff_header_shown;
5019 int commit_conflicts;
5026 * Create a file which contains the target path of a symlink so we can feed
5027 * it as content to the diff engine.
5029 static const struct got_error *
5030 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5031 const char *abspath)
5033 const struct got_error *err = NULL;
5034 char target_path[PATH_MAX];
5035 ssize_t target_len, outlen;
5040 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5041 if (target_len == -1)
5042 return got_error_from_errno2("readlinkat", abspath);
5044 target_len = readlink(abspath, target_path, PATH_MAX);
5045 if (target_len == -1)
5046 return got_error_from_errno2("readlink", abspath);
5049 *fd = got_opentempfd();
5051 return got_error_from_errno("got_opentempfd");
5053 outlen = write(*fd, target_path, target_len);
5055 err = got_error_from_errno("got_opentempfd");
5059 if (lseek(*fd, 0, SEEK_SET) == -1) {
5060 err = got_error_from_errno2("lseek", abspath);
5071 static const struct got_error *
5072 append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5073 FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5074 int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5076 const struct got_error *err = NULL;
5077 struct got_blob_object *blob1 = NULL;
5078 int fd = -1, fd1 = -1, fd2 = -1;
5079 FILE *ondisk_file = NULL;
5080 char *label1 = NULL;
5084 char *id_str = NULL;
5086 memset(&sb, 0, sizeof(sb));
5089 if (ct->staged_status != GOT_STATUS_MODIFY &&
5090 ct->staged_status != GOT_STATUS_ADD &&
5091 ct->staged_status != GOT_STATUS_DELETE)
5094 if (ct->status != GOT_STATUS_MODIFY &&
5095 ct->status != GOT_STATUS_ADD &&
5096 ct->status != GOT_STATUS_DELETE &&
5097 ct->status != GOT_STATUS_CONFLICT)
5101 err = got_opentemp_truncate(f1);
5103 return got_error_from_errno("got_opentemp_truncate");
5104 err = got_opentemp_truncate(f2);
5106 return got_error_from_errno("got_opentemp_truncate");
5108 if (!*diff_header_shown) {
5109 err = got_object_id_str(&id_str, worktree->base_commit_id);
5112 fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5113 got_worktree_get_root_path(worktree));
5114 fprintf(diff_outfile, "commit - %s\n", id_str);
5115 fprintf(diff_outfile, "path + %s%s\n",
5116 got_worktree_get_root_path(worktree),
5117 diff_staged ? " (staged changes)" : "");
5118 *diff_header_shown = 1;
5122 const char *label1 = NULL, *label2 = NULL;
5123 switch (ct->staged_status) {
5124 case GOT_STATUS_MODIFY:
5128 case GOT_STATUS_ADD:
5131 case GOT_STATUS_DELETE:
5135 return got_error(GOT_ERR_FILE_STATUS);
5137 fd1 = got_opentempfd();
5139 err = got_error_from_errno("got_opentempfd");
5142 fd2 = got_opentempfd();
5144 err = got_error_from_errno("got_opentempfd");
5147 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5148 fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5149 label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5150 NULL, repo, diff_outfile);
5154 fd1 = got_opentempfd();
5156 err = got_error_from_errno("got_opentempfd");
5160 if (ct->status != GOT_STATUS_ADD) {
5161 err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5167 if (ct->status != GOT_STATUS_DELETE) {
5169 fd = openat(dirfd, de_name,
5170 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5172 if (!got_err_open_nofollow_on_symlink()) {
5173 err = got_error_from_errno2("openat",
5177 err = get_symlink_target_file(&fd, dirfd,
5178 de_name, ct->ondisk_path);
5183 fd = open(ct->ondisk_path,
5184 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5186 if (!got_err_open_nofollow_on_symlink()) {
5187 err = got_error_from_errno2("open",
5191 err = get_symlink_target_file(&fd, dirfd,
5192 de_name, ct->ondisk_path);
5197 if (fstatat(fd, ct->ondisk_path, &sb,
5198 AT_SYMLINK_NOFOLLOW) == -1) {
5199 err = got_error_from_errno2("fstatat", ct->ondisk_path);
5202 ondisk_file = fdopen(fd, "r");
5203 if (ondisk_file == NULL) {
5204 err = got_error_from_errno2("fdopen", ct->ondisk_path);
5212 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5218 err = got_diff_blob_file(blob1, f1, size1, label1,
5219 ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5220 GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5222 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5223 err = got_error_from_errno("close");
5224 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5225 err = got_error_from_errno("close");
5227 got_object_blob_close(blob1);
5228 if (fd != -1 && close(fd) == -1 && err == NULL)
5229 err = got_error_from_errno("close");
5230 if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5231 err = got_error_from_errno("fclose");
5235 static const struct got_error *
5236 collect_commitables(void *arg, unsigned char status,
5237 unsigned char staged_status, const char *relpath,
5238 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5239 struct got_object_id *commit_id, int dirfd, const char *de_name)
5241 struct collect_commitables_arg *a = arg;
5242 const struct got_error *err = NULL;
5243 struct got_commitable *ct = NULL;
5244 struct got_pathlist_entry *new = NULL;
5245 char *parent_path = NULL, *path = NULL;
5248 if (a->have_staged_files) {
5249 if (staged_status != GOT_STATUS_MODIFY &&
5250 staged_status != GOT_STATUS_ADD &&
5251 staged_status != GOT_STATUS_DELETE)
5254 if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5255 printf("C %s\n", relpath);
5256 return got_error(GOT_ERR_COMMIT_CONFLICT);
5259 if (status != GOT_STATUS_MODIFY &&
5260 status != GOT_STATUS_MODE_CHANGE &&
5261 status != GOT_STATUS_ADD &&
5262 status != GOT_STATUS_DELETE &&
5263 status != GOT_STATUS_CONFLICT)
5267 if (asprintf(&path, "/%s", relpath) == -1) {
5268 err = got_error_from_errno("asprintf");
5271 if (strcmp(path, "/") == 0) {
5272 parent_path = strdup("");
5273 if (parent_path == NULL)
5274 return got_error_from_errno("strdup");
5276 err = got_path_dirname(&parent_path, path);
5281 ct = calloc(1, sizeof(*ct));
5283 err = got_error_from_errno("calloc");
5287 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5289 err = got_error_from_errno("asprintf");
5293 if (staged_status == GOT_STATUS_ADD ||
5294 staged_status == GOT_STATUS_MODIFY) {
5295 struct got_fileindex_entry *ie;
5296 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5297 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5298 case GOT_FILEIDX_MODE_REGULAR_FILE:
5299 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5302 case GOT_FILEIDX_MODE_SYMLINK:
5306 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5309 ct->mode |= got_fileindex_entry_perms_get(ie);
5310 } else if (status != GOT_STATUS_DELETE &&
5311 staged_status != GOT_STATUS_DELETE) {
5313 if (fstatat(dirfd, de_name, &sb,
5314 AT_SYMLINK_NOFOLLOW) == -1) {
5315 err = got_error_from_errno2("fstatat",
5319 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5320 err = got_error_from_errno2("lstat", ct->ondisk_path);
5323 ct->mode = sb.st_mode;
5326 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5327 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5329 err = got_error_from_errno("asprintf");
5333 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5334 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5336 char target_path[PATH_MAX];
5338 target_len = readlink(ct->ondisk_path, target_path,
5339 sizeof(target_path));
5340 if (target_len == -1) {
5341 err = got_error_from_errno2("readlink",
5345 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5346 target_len, ct->ondisk_path, a->worktree->root_path);
5349 if (is_bad_symlink) {
5350 err = got_error_path(ct->ondisk_path,
5351 GOT_ERR_BAD_SYMLINK);
5357 ct->status = status;
5358 ct->staged_status = staged_status;
5359 ct->blob_id = NULL; /* will be filled in when blob gets created */
5360 if (ct->status != GOT_STATUS_ADD &&
5361 ct->staged_status != GOT_STATUS_ADD) {
5362 ct->base_blob_id = got_object_id_dup(blob_id);
5363 if (ct->base_blob_id == NULL) {
5364 err = got_error_from_errno("got_object_id_dup");
5367 ct->base_commit_id = got_object_id_dup(commit_id);
5368 if (ct->base_commit_id == NULL) {
5369 err = got_error_from_errno("got_object_id_dup");
5373 if (ct->staged_status == GOT_STATUS_ADD ||
5374 ct->staged_status == GOT_STATUS_MODIFY) {
5375 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5376 if (ct->staged_blob_id == NULL) {
5377 err = got_error_from_errno("got_object_id_dup");
5381 ct->path = strdup(path);
5382 if (ct->path == NULL) {
5383 err = got_error_from_errno("strdup");
5386 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5390 if (a->diff_outfile && ct && new != NULL) {
5391 err = append_ct_diff(ct, &a->diff_header_shown,
5392 a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5393 a->have_staged_files, a->repo, a->worktree);
5398 if (ct && (err || new == NULL))
5399 free_commitable(ct);
5405 static const struct got_error *write_tree(struct got_object_id **, int *,
5406 struct got_tree_object *, const char *, struct got_pathlist_head *,
5407 got_worktree_status_cb status_cb, void *status_arg,
5408 struct got_repository *);
5410 static const struct got_error *
5411 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5412 struct got_tree_entry *te, const char *parent_path,
5413 struct got_pathlist_head *commitable_paths,
5414 got_worktree_status_cb status_cb, void *status_arg,
5415 struct got_repository *repo)
5417 const struct got_error *err = NULL;
5418 struct got_tree_object *subtree;
5421 if (asprintf(&subpath, "%s%s%s", parent_path,
5422 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5423 return got_error_from_errno("asprintf");
5425 err = got_object_open_as_tree(&subtree, repo, &te->id);
5429 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5430 commitable_paths, status_cb, status_arg, repo);
5431 got_object_tree_close(subtree);
5436 static const struct got_error *
5437 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5439 const struct got_error *err = NULL;
5440 char *ct_parent_path = NULL;
5444 if (strchr(ct->in_repo_path, '/') == NULL) {
5445 *match = got_path_is_root_dir(path);
5449 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5452 *match = (strcmp(path, ct_parent_path) == 0);
5453 free(ct_parent_path);
5458 get_ct_file_mode(struct got_commitable *ct)
5460 if (S_ISLNK(ct->mode))
5463 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5466 static const struct got_error *
5467 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5468 struct got_tree_entry *te, struct got_commitable *ct)
5470 const struct got_error *err = NULL;
5474 err = got_object_tree_entry_dup(new_te, te);
5478 (*new_te)->mode = get_ct_file_mode(ct);
5480 if (ct->staged_status == GOT_STATUS_MODIFY)
5481 memcpy(&(*new_te)->id, ct->staged_blob_id,
5482 sizeof((*new_te)->id));
5484 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5486 if (err && *new_te) {
5493 static const struct got_error *
5494 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5495 struct got_commitable *ct)
5497 const struct got_error *err = NULL;
5498 char *ct_name = NULL;
5502 *new_te = calloc(1, sizeof(**new_te));
5503 if (*new_te == NULL)
5504 return got_error_from_errno("calloc");
5506 err = got_path_basename(&ct_name, ct->path);
5509 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5510 sizeof((*new_te)->name)) {
5511 err = got_error(GOT_ERR_NO_SPACE);
5515 (*new_te)->mode = get_ct_file_mode(ct);
5517 if (ct->staged_status == GOT_STATUS_ADD)
5518 memcpy(&(*new_te)->id, ct->staged_blob_id,
5519 sizeof((*new_te)->id));
5521 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5524 if (err && *new_te) {
5531 static const struct got_error *
5532 insert_tree_entry(struct got_tree_entry *new_te,
5533 struct got_pathlist_head *paths)
5535 const struct got_error *err = NULL;
5536 struct got_pathlist_entry *new_pe;
5538 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5542 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5546 static const struct got_error *
5547 report_ct_status(struct got_commitable *ct,
5548 got_worktree_status_cb status_cb, void *status_arg)
5550 const char *ct_path = ct->path;
5551 unsigned char status;
5553 if (status_cb == NULL) /* no commit progress output desired */
5556 while (ct_path[0] == '/')
5559 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5560 status = ct->staged_status;
5562 status = ct->status;
5564 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5565 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5568 static const struct got_error *
5569 match_modified_subtree(int *modified, struct got_tree_entry *te,
5570 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5572 const struct got_error *err = NULL;
5573 struct got_pathlist_entry *pe;
5578 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5579 got_path_is_root_dir(base_tree_path) ? "" : "/",
5581 return got_error_from_errno("asprintf");
5583 TAILQ_FOREACH(pe, commitable_paths, entry) {
5584 struct got_commitable *ct = pe->data;
5585 *modified = got_path_is_child(ct->in_repo_path, te_path,
5595 static const struct got_error *
5596 match_deleted_or_modified_ct(struct got_commitable **ctp,
5597 struct got_tree_entry *te, const char *base_tree_path,
5598 struct got_pathlist_head *commitable_paths)
5600 const struct got_error *err = NULL;
5601 struct got_pathlist_entry *pe;
5605 TAILQ_FOREACH(pe, commitable_paths, entry) {
5606 struct got_commitable *ct = pe->data;
5607 char *ct_name = NULL;
5610 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5611 if (ct->status != GOT_STATUS_MODIFY &&
5612 ct->status != GOT_STATUS_MODE_CHANGE &&
5613 ct->status != GOT_STATUS_DELETE &&
5614 ct->status != GOT_STATUS_CONFLICT)
5617 if (ct->staged_status != GOT_STATUS_MODIFY &&
5618 ct->staged_status != GOT_STATUS_DELETE)
5622 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5625 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5631 err = got_path_basename(&ct_name, pe->path);
5635 if (strcmp(te->name, ct_name) != 0) {
5648 static const struct got_error *
5649 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5650 const char *child_path, const char *path_base_tree,
5651 struct got_pathlist_head *commitable_paths,
5652 got_worktree_status_cb status_cb, void *status_arg,
5653 struct got_repository *repo)
5655 const struct got_error *err = NULL;
5656 struct got_tree_entry *new_te;
5658 struct got_object_id *id = NULL;
5663 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5664 got_path_is_root_dir(path_base_tree) ? "" : "/",
5666 return got_error_from_errno("asprintf");
5668 new_te = calloc(1, sizeof(*new_te));
5670 return got_error_from_errno("calloc");
5671 new_te->mode = S_IFDIR;
5673 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5674 sizeof(new_te->name)) {
5675 err = got_error(GOT_ERR_NO_SPACE);
5678 err = write_tree(&id, &nentries, NULL, subtree_path,
5679 commitable_paths, status_cb, status_arg, repo);
5684 memcpy(&new_te->id, id, sizeof(new_te->id));
5693 static const struct got_error *
5694 write_tree(struct got_object_id **new_tree_id, int *nentries,
5695 struct got_tree_object *base_tree, const char *path_base_tree,
5696 struct got_pathlist_head *commitable_paths,
5697 got_worktree_status_cb status_cb, void *status_arg,
5698 struct got_repository *repo)
5700 const struct got_error *err = NULL;
5701 struct got_pathlist_head paths;
5702 struct got_tree_entry *te, *new_te = NULL;
5703 struct got_pathlist_entry *pe;
5708 /* Insert, and recurse into, newly added entries first. */
5709 TAILQ_FOREACH(pe, commitable_paths, entry) {
5710 struct got_commitable *ct = pe->data;
5711 char *child_path = NULL, *slash;
5713 if ((ct->status != GOT_STATUS_ADD &&
5714 ct->staged_status != GOT_STATUS_ADD) ||
5715 (ct->flags & GOT_COMMITABLE_ADDED))
5718 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5719 strlen(path_base_tree)))
5722 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5727 slash = strchr(child_path, '/');
5728 if (slash == NULL) {
5729 err = alloc_added_blob_tree_entry(&new_te, ct);
5732 err = report_ct_status(ct, status_cb, status_arg);
5735 ct->flags |= GOT_COMMITABLE_ADDED;
5736 err = insert_tree_entry(new_te, &paths);
5741 *slash = '\0'; /* trim trailing path components */
5742 if (base_tree == NULL ||
5743 got_object_tree_find_entry(base_tree, child_path)
5745 err = make_subtree_for_added_blob(&new_te,
5746 child_path, path_base_tree,
5747 commitable_paths, status_cb, status_arg,
5751 err = insert_tree_entry(new_te, &paths);
5760 int i, nbase_entries;
5761 /* Handle modified and deleted entries. */
5762 nbase_entries = got_object_tree_get_nentries(base_tree);
5763 for (i = 0; i < nbase_entries; i++) {
5764 struct got_commitable *ct = NULL;
5766 te = got_object_tree_get_entry(base_tree, i);
5767 if (got_object_tree_entry_is_submodule(te)) {
5768 /* Entry is a submodule; just copy it. */
5769 err = got_object_tree_entry_dup(&new_te, te);
5772 err = insert_tree_entry(new_te, &paths);
5779 if (S_ISDIR(te->mode)) {
5781 err = got_object_tree_entry_dup(&new_te, te);
5784 err = match_modified_subtree(&modified, te,
5785 path_base_tree, commitable_paths);
5788 /* Avoid recursion into unmodified subtrees. */
5790 struct got_object_id *new_id;
5792 err = write_subtree(&new_id,
5794 path_base_tree, commitable_paths,
5795 status_cb, status_arg, repo);
5798 if (nsubentries == 0) {
5799 /* All entries were deleted. */
5803 memcpy(&new_te->id, new_id,
5804 sizeof(new_te->id));
5807 err = insert_tree_entry(new_te, &paths);
5814 err = match_deleted_or_modified_ct(&ct, te,
5815 path_base_tree, commitable_paths);
5819 /* NB: Deleted entries get dropped here. */
5820 if (ct->status == GOT_STATUS_MODIFY ||
5821 ct->status == GOT_STATUS_MODE_CHANGE ||
5822 ct->status == GOT_STATUS_CONFLICT ||
5823 ct->staged_status == GOT_STATUS_MODIFY) {
5824 err = alloc_modified_blob_tree_entry(
5828 err = insert_tree_entry(new_te, &paths);
5833 err = report_ct_status(ct, status_cb,
5838 /* Entry is unchanged; just copy it. */
5839 err = got_object_tree_entry_dup(&new_te, te);
5842 err = insert_tree_entry(new_te, &paths);
5850 /* Write new list of entries; deleted entries have been dropped. */
5851 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5853 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
5857 static const struct got_error *
5858 update_fileindex_after_commit(struct got_worktree *worktree,
5859 struct got_pathlist_head *commitable_paths,
5860 struct got_object_id *new_base_commit_id,
5861 struct got_fileindex *fileindex, int have_staged_files)
5863 const struct got_error *err = NULL;
5864 struct got_pathlist_entry *pe;
5865 char *relpath = NULL;
5867 TAILQ_FOREACH(pe, commitable_paths, entry) {
5868 struct got_fileindex_entry *ie;
5869 struct got_commitable *ct = pe->data;
5871 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5873 err = got_path_skip_common_ancestor(&relpath,
5874 worktree->root_path, ct->ondisk_path);
5879 if (ct->status == GOT_STATUS_DELETE ||
5880 ct->staged_status == GOT_STATUS_DELETE) {
5881 got_fileindex_entry_remove(fileindex, ie);
5882 } else if (ct->staged_status == GOT_STATUS_ADD ||
5883 ct->staged_status == GOT_STATUS_MODIFY) {
5884 got_fileindex_entry_stage_set(ie,
5885 GOT_FILEIDX_STAGE_NONE);
5886 got_fileindex_entry_staged_filetype_set(ie, 0);
5888 err = got_fileindex_entry_update(ie,
5889 worktree->root_fd, relpath,
5890 ct->staged_blob_id->sha1,
5891 new_base_commit_id->sha1,
5892 !have_staged_files);
5894 err = got_fileindex_entry_update(ie,
5895 worktree->root_fd, relpath,
5897 new_base_commit_id->sha1,
5898 !have_staged_files);
5900 err = got_fileindex_entry_alloc(&ie, pe->path);
5903 err = got_fileindex_entry_update(ie,
5904 worktree->root_fd, relpath, ct->blob_id->sha1,
5905 new_base_commit_id->sha1, 1);
5907 got_fileindex_entry_free(ie);
5910 err = got_fileindex_entry_add(fileindex, ie);
5912 got_fileindex_entry_free(ie);
5925 static const struct got_error *
5926 check_out_of_date(const char *in_repo_path, unsigned char status,
5927 unsigned char staged_status, struct got_object_id *base_blob_id,
5928 struct got_object_id *base_commit_id,
5929 struct got_object_id *head_commit_id, struct got_repository *repo,
5932 const struct got_error *err = NULL;
5933 struct got_commit_object *commit = NULL;
5934 struct got_object_id *id = NULL;
5936 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5937 /* Trivial case: base commit == head commit */
5938 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5941 * Ensure file content which local changes were based
5942 * on matches file content in the branch head.
5944 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5947 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5949 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5950 err = got_error(ood_errcode);
5952 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5953 err = got_error(ood_errcode);
5955 /* Require that added files don't exist in the branch head. */
5956 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5959 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5960 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5962 err = id ? got_error(ood_errcode) : NULL;
5967 got_object_commit_close(commit);
5971 static const struct got_error *
5972 commit_worktree(struct got_object_id **new_commit_id,
5973 struct got_pathlist_head *commitable_paths,
5974 struct got_object_id *head_commit_id,
5975 struct got_object_id *parent_id2,
5976 struct got_worktree *worktree,
5977 const char *author, const char *committer, char *diff_path,
5978 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5979 got_worktree_status_cb status_cb, void *status_arg,
5980 struct got_repository *repo)
5982 const struct got_error *err = NULL, *unlockerr = NULL;
5983 struct got_pathlist_entry *pe;
5984 const char *head_ref_name = NULL;
5985 struct got_commit_object *head_commit = NULL;
5986 struct got_reference *head_ref2 = NULL;
5987 struct got_object_id *head_commit_id2 = NULL;
5988 struct got_tree_object *head_tree = NULL;
5989 struct got_object_id *new_tree_id = NULL;
5990 int nentries, nparents = 0;
5991 struct got_object_id_queue parent_ids;
5992 struct got_object_qid *pid = NULL;
5993 char *logmsg = NULL;
5996 *new_commit_id = NULL;
5998 STAILQ_INIT(&parent_ids);
6000 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6004 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6008 if (commit_msg_cb != NULL) {
6009 err = commit_msg_cb(commitable_paths, diff_path,
6010 &logmsg, commit_arg);
6015 if (logmsg == NULL || strlen(logmsg) == 0) {
6016 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6020 /* Create blobs from added and modified files and record their IDs. */
6021 TAILQ_FOREACH(pe, commitable_paths, entry) {
6022 struct got_commitable *ct = pe->data;
6025 /* Blobs for staged files already exist. */
6026 if (ct->staged_status == GOT_STATUS_ADD ||
6027 ct->staged_status == GOT_STATUS_MODIFY)
6030 if (ct->status != GOT_STATUS_ADD &&
6031 ct->status != GOT_STATUS_MODIFY &&
6032 ct->status != GOT_STATUS_MODE_CHANGE &&
6033 ct->status != GOT_STATUS_CONFLICT)
6036 if (asprintf(&ondisk_path, "%s/%s",
6037 worktree->root_path, pe->path) == -1) {
6038 err = got_error_from_errno("asprintf");
6041 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6047 /* Recursively write new tree objects. */
6048 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6049 commitable_paths, status_cb, status_arg, repo);
6053 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
6056 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6059 err = got_object_qid_alloc(&pid, parent_id2);
6062 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6065 timestamp = time(NULL);
6066 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6067 nparents, author, timestamp, committer, timestamp, logmsg, repo);
6073 /* Check if a concurrent commit to our branch has occurred. */
6074 head_ref_name = got_worktree_get_head_ref_name(worktree);
6075 if (head_ref_name == NULL) {
6076 err = got_error_from_errno("got_worktree_get_head_ref_name");
6079 /* Lock the reference here to prevent concurrent modification. */
6080 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6083 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6086 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6087 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6090 /* Update branch head in repository. */
6091 err = got_ref_change_ref(head_ref2, *new_commit_id);
6094 err = got_ref_write(head_ref2, repo);
6098 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6102 err = ref_base_commit(worktree, repo);
6106 got_object_id_queue_free(&parent_ids);
6108 got_object_tree_close(head_tree);
6110 got_object_commit_close(head_commit);
6111 free(head_commit_id2);
6113 unlockerr = got_ref_unlock(head_ref2);
6114 if (unlockerr && err == NULL)
6116 got_ref_close(head_ref2);
6121 static const struct got_error *
6122 check_path_is_commitable(const char *path,
6123 struct got_pathlist_head *commitable_paths)
6125 struct got_pathlist_entry *cpe = NULL;
6126 size_t path_len = strlen(path);
6128 TAILQ_FOREACH(cpe, commitable_paths, entry) {
6129 struct got_commitable *ct = cpe->data;
6130 const char *ct_path = ct->path;
6132 while (ct_path[0] == '/')
6135 if (strcmp(path, ct_path) == 0 ||
6136 got_path_is_child(ct_path, path, path_len))
6141 return got_error_path(path, GOT_ERR_BAD_PATH);
6146 static const struct got_error *
6147 check_staged_file(void *arg, struct got_fileindex_entry *ie)
6149 int *have_staged_files = arg;
6151 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6152 *have_staged_files = 1;
6153 return got_error(GOT_ERR_CANCELLED);
6159 static const struct got_error *
6160 check_non_staged_files(struct got_fileindex *fileindex,
6161 struct got_pathlist_head *paths)
6163 struct got_pathlist_entry *pe;
6164 struct got_fileindex_entry *ie;
6166 TAILQ_FOREACH(pe, paths, entry) {
6167 if (pe->path[0] == '\0')
6169 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6171 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6172 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6173 return got_error_path(pe->path,
6174 GOT_ERR_FILE_NOT_STAGED);
6180 const struct got_error *
6181 got_worktree_commit(struct got_object_id **new_commit_id,
6182 struct got_worktree *worktree, struct got_pathlist_head *paths,
6183 const char *author, const char *committer, int allow_bad_symlinks,
6184 int show_diff, int commit_conflicts,
6185 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6186 got_worktree_status_cb status_cb, void *status_arg,
6187 struct got_repository *repo)
6189 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6190 struct got_fileindex *fileindex = NULL;
6191 char *fileindex_path = NULL;
6192 struct got_pathlist_head commitable_paths;
6193 struct collect_commitables_arg cc_arg;
6194 struct got_pathlist_entry *pe;
6195 struct got_reference *head_ref = NULL;
6196 struct got_object_id *head_commit_id = NULL;
6197 char *diff_path = NULL;
6198 int have_staged_files = 0;
6200 *new_commit_id = NULL;
6202 memset(&cc_arg, 0, sizeof(cc_arg));
6203 TAILQ_INIT(&commitable_paths);
6205 err = lock_worktree(worktree, LOCK_EX);
6209 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6213 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6217 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6221 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6222 &have_staged_files);
6223 if (err && err->code != GOT_ERR_CANCELLED)
6225 if (have_staged_files) {
6226 err = check_non_staged_files(fileindex, paths);
6231 cc_arg.commitable_paths = &commitable_paths;
6232 cc_arg.worktree = worktree;
6233 cc_arg.fileindex = fileindex;
6235 cc_arg.have_staged_files = have_staged_files;
6236 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6237 cc_arg.diff_header_shown = 0;
6238 cc_arg.commit_conflicts = commit_conflicts;
6240 err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6241 GOT_TMPDIR_STR "/got", ".diff");
6244 cc_arg.f1 = got_opentemp();
6245 if (cc_arg.f1 == NULL) {
6246 err = got_error_from_errno("got_opentemp");
6249 cc_arg.f2 = got_opentemp();
6250 if (cc_arg.f2 == NULL) {
6251 err = got_error_from_errno("got_opentemp");
6256 TAILQ_FOREACH(pe, paths, entry) {
6257 err = worktree_status(worktree, pe->path, fileindex, repo,
6258 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6264 if (fflush(cc_arg.diff_outfile) == EOF) {
6265 err = got_error_from_errno("fflush");
6270 if (TAILQ_EMPTY(&commitable_paths)) {
6271 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6275 TAILQ_FOREACH(pe, paths, entry) {
6276 err = check_path_is_commitable(pe->path, &commitable_paths);
6281 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6282 struct got_commitable *ct = pe->data;
6283 const char *ct_path = ct->in_repo_path;
6285 while (ct_path[0] == '/')
6287 err = check_out_of_date(ct_path, ct->status,
6288 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6289 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6295 err = commit_worktree(new_commit_id, &commitable_paths,
6296 head_commit_id, NULL, worktree, author, committer,
6297 (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6298 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6302 err = update_fileindex_after_commit(worktree, &commitable_paths,
6303 *new_commit_id, fileindex, have_staged_files);
6304 sync_err = sync_fileindex(fileindex, fileindex_path);
6305 if (sync_err && err == NULL)
6309 got_fileindex_free(fileindex);
6310 free(fileindex_path);
6311 unlockerr = lock_worktree(worktree, LOCK_SH);
6312 if (unlockerr && err == NULL)
6314 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6315 struct got_commitable *ct = pe->data;
6317 free_commitable(ct);
6319 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6320 if (diff_path && unlink(diff_path) == -1 && err == NULL)
6321 err = got_error_from_errno2("unlink", diff_path);
6323 if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6325 err = got_error_from_errno("fclose");
6330 got_commitable_get_path(struct got_commitable *ct)
6336 got_commitable_get_status(struct got_commitable *ct)
6341 struct check_rebase_ok_arg {
6342 struct got_worktree *worktree;
6343 struct got_repository *repo;
6346 static const struct got_error *
6347 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6349 const struct got_error *err = NULL;
6350 struct check_rebase_ok_arg *a = arg;
6351 unsigned char status;
6355 /* Reject rebase of a work tree with mixed base commits. */
6356 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6357 SHA1_DIGEST_LENGTH))
6358 return got_error(GOT_ERR_MIXED_COMMITS);
6360 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6362 return got_error_from_errno("asprintf");
6364 /* Reject rebase of a work tree with modified or staged files. */
6365 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6370 if (status != GOT_STATUS_NO_CHANGE)
6371 return got_error(GOT_ERR_MODIFIED);
6372 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6373 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6378 const struct got_error *
6379 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6380 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6381 struct got_worktree *worktree, struct got_reference *branch,
6382 struct got_repository *repo)
6384 const struct got_error *err = NULL;
6385 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6386 char *branch_ref_name = NULL;
6387 char *fileindex_path = NULL;
6388 struct check_rebase_ok_arg ok_arg;
6389 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6390 struct got_object_id *wt_branch_tip = NULL;
6392 *new_base_branch_ref = NULL;
6396 err = lock_worktree(worktree, LOCK_EX);
6400 err = open_fileindex(fileindex, &fileindex_path, worktree);
6404 ok_arg.worktree = worktree;
6406 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6411 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6415 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6419 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6423 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6428 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6431 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6432 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6436 err = got_ref_alloc_symref(new_base_branch_ref,
6437 new_base_branch_ref_name, wt_branch);
6440 err = got_ref_write(*new_base_branch_ref, repo);
6444 /* TODO Lock original branch's ref while rebasing? */
6446 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6450 err = got_ref_write(branch_ref, repo);
6454 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6455 worktree->base_commit_id);
6458 err = got_ref_write(*tmp_branch, repo);
6462 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6466 free(fileindex_path);
6467 free(tmp_branch_name);
6468 free(new_base_branch_ref_name);
6469 free(branch_ref_name);
6471 got_ref_close(branch_ref);
6473 got_ref_close(wt_branch);
6474 free(wt_branch_tip);
6476 if (*new_base_branch_ref) {
6477 got_ref_close(*new_base_branch_ref);
6478 *new_base_branch_ref = NULL;
6481 got_ref_close(*tmp_branch);
6485 got_fileindex_free(*fileindex);
6488 lock_worktree(worktree, LOCK_SH);
6493 const struct got_error *
6494 got_worktree_rebase_continue(struct got_object_id **commit_id,
6495 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6496 struct got_reference **branch, struct got_fileindex **fileindex,
6497 struct got_worktree *worktree, struct got_repository *repo)
6499 const struct got_error *err;
6500 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6501 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6502 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6503 char *fileindex_path = NULL;
6504 int have_staged_files = 0;
6507 *new_base_branch = NULL;
6512 err = lock_worktree(worktree, LOCK_EX);
6516 err = open_fileindex(fileindex, &fileindex_path, worktree);
6520 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6521 &have_staged_files);
6522 if (err && err->code != GOT_ERR_CANCELLED)
6524 if (have_staged_files) {
6525 err = got_error(GOT_ERR_STAGED_PATHS);
6529 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6533 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6537 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6541 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6545 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6549 err = got_ref_open(branch, repo,
6550 got_ref_get_symref_target(branch_ref), 0);
6554 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6558 err = got_ref_resolve(commit_id, repo, commit_ref);
6562 err = got_ref_open(new_base_branch, repo,
6563 new_base_branch_ref_name, 0);
6567 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6571 free(commit_ref_name);
6572 free(branch_ref_name);
6573 free(fileindex_path);
6575 got_ref_close(commit_ref);
6577 got_ref_close(branch_ref);
6582 got_ref_close(*tmp_branch);
6585 if (*new_base_branch) {
6586 got_ref_close(*new_base_branch);
6587 *new_base_branch = NULL;
6590 got_ref_close(*branch);
6594 got_fileindex_free(*fileindex);
6597 lock_worktree(worktree, LOCK_SH);
6602 const struct got_error *
6603 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6605 const struct got_error *err;
6606 char *tmp_branch_name = NULL;
6608 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6612 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6613 free(tmp_branch_name);
6617 static const struct got_error *
6618 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6619 const char *diff_path, char **logmsg, void *arg)
6625 static const struct got_error *
6626 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6627 const char *path, struct got_object_id *blob_id,
6628 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6629 int dirfd, const char *de_name)
6634 struct collect_merged_paths_arg {
6635 got_worktree_checkout_cb progress_cb;
6637 struct got_pathlist_head *merged_paths;
6640 static const struct got_error *
6641 collect_merged_paths(void *arg, unsigned char status, const char *path)
6643 const struct got_error *err;
6644 struct collect_merged_paths_arg *a = arg;
6646 struct got_pathlist_entry *new;
6648 err = (*a->progress_cb)(a->progress_arg, status, path);
6652 if (status != GOT_STATUS_MERGE &&
6653 status != GOT_STATUS_ADD &&
6654 status != GOT_STATUS_DELETE &&
6655 status != GOT_STATUS_CONFLICT)
6660 return got_error_from_errno("strdup");
6662 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6663 if (err || new == NULL)
6668 static const struct got_error *
6669 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6670 int is_rebase, struct got_repository *repo)
6672 const struct got_error *err;
6673 struct got_reference *commit_ref = NULL;
6675 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6677 if (err->code != GOT_ERR_NOT_REF)
6679 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6682 err = got_ref_write(commit_ref, repo);
6685 } else if (is_rebase) {
6686 struct got_object_id *stored_id;
6689 err = got_ref_resolve(&stored_id, repo, commit_ref);
6692 cmp = got_object_id_cmp(commit_id, stored_id);
6695 err = got_error(GOT_ERR_REBASE_COMMITID);
6701 got_ref_close(commit_ref);
6705 static const struct got_error *
6706 rebase_merge_files(struct got_pathlist_head *merged_paths,
6707 const char *commit_ref_name, struct got_worktree *worktree,
6708 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6709 struct got_object_id *commit_id, struct got_repository *repo,
6710 got_worktree_checkout_cb progress_cb, void *progress_arg,
6711 got_cancel_cb cancel_cb, void *cancel_arg)
6713 const struct got_error *err;
6714 struct got_reference *commit_ref = NULL;
6715 struct collect_merged_paths_arg cmp_arg;
6716 char *fileindex_path;
6718 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6720 err = get_fileindex_path(&fileindex_path, worktree);
6724 cmp_arg.progress_cb = progress_cb;
6725 cmp_arg.progress_arg = progress_arg;
6726 cmp_arg.merged_paths = merged_paths;
6727 err = merge_files(worktree, fileindex, fileindex_path,
6728 parent_commit_id, commit_id, repo, collect_merged_paths,
6729 &cmp_arg, cancel_cb, cancel_arg);
6731 got_ref_close(commit_ref);
6735 const struct got_error *
6736 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6737 struct got_worktree *worktree, struct got_fileindex *fileindex,
6738 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6739 struct got_repository *repo,
6740 got_worktree_checkout_cb progress_cb, void *progress_arg,
6741 got_cancel_cb cancel_cb, void *cancel_arg)
6743 const struct got_error *err;
6744 char *commit_ref_name;
6746 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6750 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6754 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6755 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6756 progress_arg, cancel_cb, cancel_arg);
6758 free(commit_ref_name);
6762 const struct got_error *
6763 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6764 struct got_worktree *worktree, struct got_fileindex *fileindex,
6765 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6766 struct got_repository *repo,
6767 got_worktree_checkout_cb progress_cb, void *progress_arg,
6768 got_cancel_cb cancel_cb, void *cancel_arg)
6770 const struct got_error *err;
6771 char *commit_ref_name;
6773 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6777 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6781 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6782 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6783 progress_arg, cancel_cb, cancel_arg);
6785 free(commit_ref_name);
6789 static const struct got_error *
6790 rebase_commit(struct got_object_id **new_commit_id,
6791 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6792 struct got_worktree *worktree, struct got_fileindex *fileindex,
6793 struct got_reference *tmp_branch, const char *committer,
6794 struct got_commit_object *orig_commit, const char *new_logmsg,
6795 int allow_conflict, struct got_repository *repo)
6797 const struct got_error *err, *sync_err;
6798 struct got_pathlist_head commitable_paths;
6799 struct collect_commitables_arg cc_arg;
6800 char *fileindex_path = NULL;
6801 struct got_reference *head_ref = NULL;
6802 struct got_object_id *head_commit_id = NULL;
6803 char *logmsg = NULL;
6805 memset(&cc_arg, 0, sizeof(cc_arg));
6806 TAILQ_INIT(&commitable_paths);
6807 *new_commit_id = NULL;
6809 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6811 err = get_fileindex_path(&fileindex_path, worktree);
6815 cc_arg.commitable_paths = &commitable_paths;
6816 cc_arg.worktree = worktree;
6818 cc_arg.have_staged_files = 0;
6819 cc_arg.commit_conflicts = allow_conflict;
6821 * If possible get the status of individual files directly to
6822 * avoid crawling the entire work tree once per rebased commit.
6824 * Ideally, merged_paths would contain a list of commitables
6825 * we could use so we could skip worktree_status() entirely.
6826 * However, we would then need carefully keep track of cumulative
6827 * effects of operations such as file additions and deletions
6828 * in 'got histedit -f' (folding multiple commits into one),
6829 * and this extra complexity is not really worth it.
6832 struct got_pathlist_entry *pe;
6833 TAILQ_FOREACH(pe, merged_paths, entry) {
6834 err = worktree_status(worktree, pe->path, fileindex,
6835 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6841 err = worktree_status(worktree, "", fileindex, repo,
6842 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6847 if (TAILQ_EMPTY(&commitable_paths)) {
6848 /* No-op change; commit will be elided. */
6849 err = got_ref_delete(commit_ref, repo);
6852 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6856 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6860 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6865 logmsg = strdup(new_logmsg);
6866 if (logmsg == NULL) {
6867 err = got_error_from_errno("strdup");
6871 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6876 /* NB: commit_worktree will call free(logmsg) */
6877 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6878 NULL, worktree, got_object_commit_get_author(orig_commit),
6879 committer ? committer :
6880 got_object_commit_get_committer(orig_commit), NULL,
6881 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6885 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6889 err = got_ref_delete(commit_ref, repo);
6893 err = update_fileindex_after_commit(worktree, &commitable_paths,
6894 *new_commit_id, fileindex, 0);
6895 sync_err = sync_fileindex(fileindex, fileindex_path);
6896 if (sync_err && err == NULL)
6899 free(fileindex_path);
6900 free(head_commit_id);
6902 got_ref_close(head_ref);
6904 free(*new_commit_id);
6905 *new_commit_id = NULL;
6910 const struct got_error *
6911 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6912 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6913 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6914 const char *committer, struct got_commit_object *orig_commit,
6915 struct got_object_id *orig_commit_id, int allow_conflict,
6916 struct got_repository *repo)
6918 const struct got_error *err;
6919 char *commit_ref_name;
6920 struct got_reference *commit_ref = NULL;
6921 struct got_object_id *commit_id = NULL;
6923 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6927 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6930 err = got_ref_resolve(&commit_id, repo, commit_ref);
6933 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6934 err = got_error(GOT_ERR_REBASE_COMMITID);
6938 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6939 worktree, fileindex, tmp_branch, committer, orig_commit,
6940 NULL, allow_conflict, repo);
6943 got_ref_close(commit_ref);
6944 free(commit_ref_name);
6949 const struct got_error *
6950 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6951 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6952 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6953 const char *committer, struct got_commit_object *orig_commit,
6954 struct got_object_id *orig_commit_id, const char *new_logmsg,
6955 int allow_conflict, struct got_repository *repo)
6957 const struct got_error *err;
6958 char *commit_ref_name;
6959 struct got_reference *commit_ref = NULL;
6961 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6965 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6969 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6970 worktree, fileindex, tmp_branch, committer, orig_commit,
6971 new_logmsg, allow_conflict, repo);
6974 got_ref_close(commit_ref);
6975 free(commit_ref_name);
6979 const struct got_error *
6980 got_worktree_rebase_postpone(struct got_worktree *worktree,
6981 struct got_fileindex *fileindex)
6984 got_fileindex_free(fileindex);
6985 return lock_worktree(worktree, LOCK_SH);
6988 static const struct got_error *
6989 delete_ref(const char *name, struct got_repository *repo)
6991 const struct got_error *err;
6992 struct got_reference *ref;
6994 err = got_ref_open(&ref, repo, name, 0);
6996 if (err->code == GOT_ERR_NOT_REF)
7001 err = got_ref_delete(ref, repo);
7006 static const struct got_error *
7007 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7009 const struct got_error *err;
7010 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7011 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7013 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7016 err = delete_ref(tmp_branch_name, repo);
7020 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7023 err = delete_ref(new_base_branch_ref_name, repo);
7027 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7030 err = delete_ref(branch_ref_name, repo);
7034 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7037 err = delete_ref(commit_ref_name, repo);
7042 free(tmp_branch_name);
7043 free(new_base_branch_ref_name);
7044 free(branch_ref_name);
7045 free(commit_ref_name);
7049 static const struct got_error *
7050 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7051 struct got_object_id *new_commit_id, struct got_repository *repo)
7053 const struct got_error *err;
7054 struct got_reference *ref = NULL;
7055 struct got_object_id *old_commit_id = NULL;
7056 const char *branch_name = NULL;
7057 char *new_id_str = NULL;
7058 char *refname = NULL;
7060 branch_name = got_ref_get_name(branch);
7061 if (strncmp(branch_name, "refs/heads/", 11) != 0)
7062 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7065 err = got_object_id_str(&new_id_str, new_commit_id);
7069 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7070 new_id_str) == -1) {
7071 err = got_error_from_errno("asprintf");
7075 err = got_ref_resolve(&old_commit_id, repo, branch);
7079 err = got_ref_alloc(&ref, refname, old_commit_id);
7083 err = got_ref_write(ref, repo);
7087 free(old_commit_id);
7093 const struct got_error *
7094 got_worktree_rebase_complete(struct got_worktree *worktree,
7095 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7096 struct got_reference *rebased_branch, struct got_repository *repo,
7099 const struct got_error *err, *unlockerr, *sync_err;
7100 struct got_object_id *new_head_commit_id = NULL;
7101 char *fileindex_path = NULL;
7103 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7107 if (create_backup) {
7108 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7109 rebased_branch, new_head_commit_id, repo);
7114 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7118 err = got_ref_write(rebased_branch, repo);
7122 err = got_worktree_set_head_ref(worktree, rebased_branch);
7126 err = delete_rebase_refs(worktree, repo);
7130 err = get_fileindex_path(&fileindex_path, worktree);
7133 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7134 sync_err = sync_fileindex(fileindex, fileindex_path);
7135 if (sync_err && err == NULL)
7138 got_fileindex_free(fileindex);
7139 free(fileindex_path);
7140 free(new_head_commit_id);
7141 unlockerr = lock_worktree(worktree, LOCK_SH);
7142 if (unlockerr && err == NULL)
7147 const struct got_error *
7148 got_worktree_rebase_abort(struct got_worktree *worktree,
7149 struct got_fileindex *fileindex, struct got_repository *repo,
7150 struct got_reference *new_base_branch,
7151 got_worktree_checkout_cb progress_cb, void *progress_arg)
7153 const struct got_error *err, *unlockerr, *sync_err;
7154 struct got_reference *resolved = NULL;
7155 struct got_object_id *commit_id = NULL;
7156 struct got_commit_object *commit = NULL;
7157 char *fileindex_path = NULL;
7158 struct revert_file_args rfa;
7159 struct got_object_id *tree_id = NULL;
7161 err = lock_worktree(worktree, LOCK_EX);
7165 err = got_object_open_as_commit(&commit, repo,
7166 worktree->base_commit_id);
7170 err = got_ref_open(&resolved, repo,
7171 got_ref_get_symref_target(new_base_branch), 0);
7175 err = got_worktree_set_head_ref(worktree, resolved);
7180 * XXX commits to the base branch could have happened while
7181 * we were busy rebasing; should we store the original commit ID
7182 * when rebase begins and read it back here?
7184 err = got_ref_resolve(&commit_id, repo, resolved);
7188 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7192 err = got_object_id_by_path(&tree_id, repo, commit,
7193 worktree->path_prefix);
7197 err = delete_rebase_refs(worktree, repo);
7201 err = get_fileindex_path(&fileindex_path, worktree);
7205 rfa.worktree = worktree;
7206 rfa.fileindex = fileindex;
7207 rfa.progress_cb = progress_cb;
7208 rfa.progress_arg = progress_arg;
7209 rfa.patch_cb = NULL;
7210 rfa.patch_arg = NULL;
7212 rfa.unlink_added_files = 0;
7213 err = worktree_status(worktree, "", fileindex, repo,
7214 revert_file, &rfa, NULL, NULL, 1, 0);
7218 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7219 repo, progress_cb, progress_arg, NULL, NULL);
7221 sync_err = sync_fileindex(fileindex, fileindex_path);
7222 if (sync_err && err == NULL)
7225 got_ref_close(resolved);
7229 got_object_commit_close(commit);
7231 got_fileindex_free(fileindex);
7232 free(fileindex_path);
7234 unlockerr = lock_worktree(worktree, LOCK_SH);
7235 if (unlockerr && err == NULL)
7240 const struct got_error *
7241 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7242 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7243 struct got_fileindex **fileindex, struct got_worktree *worktree,
7244 struct got_repository *repo)
7246 const struct got_error *err = NULL;
7247 char *tmp_branch_name = NULL;
7248 char *branch_ref_name = NULL;
7249 char *base_commit_ref_name = NULL;
7250 char *fileindex_path = NULL;
7251 struct check_rebase_ok_arg ok_arg;
7252 struct got_reference *wt_branch = NULL;
7253 struct got_reference *base_commit_ref = NULL;
7257 *base_commit_id = NULL;
7260 err = lock_worktree(worktree, LOCK_EX);
7264 err = open_fileindex(fileindex, &fileindex_path, worktree);
7268 ok_arg.worktree = worktree;
7270 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7275 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7279 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7283 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7288 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7293 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7297 err = got_ref_write(*branch_ref, repo);
7301 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7302 worktree->base_commit_id);
7305 err = got_ref_write(base_commit_ref, repo);
7308 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7309 if (*base_commit_id == NULL) {
7310 err = got_error_from_errno("got_object_id_dup");
7314 err = got_ref_alloc(tmp_branch, tmp_branch_name,
7315 worktree->base_commit_id);
7318 err = got_ref_write(*tmp_branch, repo);
7322 err = got_worktree_set_head_ref(worktree, *tmp_branch);
7326 free(fileindex_path);
7327 free(tmp_branch_name);
7328 free(branch_ref_name);
7329 free(base_commit_ref_name);
7331 got_ref_close(wt_branch);
7334 got_ref_close(*branch_ref);
7338 got_ref_close(*tmp_branch);
7341 free(*base_commit_id);
7343 got_fileindex_free(*fileindex);
7346 lock_worktree(worktree, LOCK_SH);
7351 const struct got_error *
7352 got_worktree_histedit_postpone(struct got_worktree *worktree,
7353 struct got_fileindex *fileindex)
7356 got_fileindex_free(fileindex);
7357 return lock_worktree(worktree, LOCK_SH);
7360 const struct got_error *
7361 got_worktree_histedit_in_progress(int *in_progress,
7362 struct got_worktree *worktree)
7364 const struct got_error *err;
7365 char *tmp_branch_name = NULL;
7367 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7371 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7372 free(tmp_branch_name);
7376 const struct got_error *
7377 got_worktree_histedit_continue(struct got_object_id **commit_id,
7378 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7379 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7380 struct got_worktree *worktree, struct got_repository *repo)
7382 const struct got_error *err;
7383 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7384 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7385 struct got_reference *commit_ref = NULL;
7386 struct got_reference *base_commit_ref = NULL;
7387 char *fileindex_path = NULL;
7388 int have_staged_files = 0;
7392 *base_commit_id = NULL;
7395 err = lock_worktree(worktree, LOCK_EX);
7399 err = open_fileindex(fileindex, &fileindex_path, worktree);
7403 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7404 &have_staged_files);
7405 if (err && err->code != GOT_ERR_CANCELLED)
7407 if (have_staged_files) {
7408 err = got_error(GOT_ERR_STAGED_PATHS);
7412 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7416 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7420 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7424 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7429 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7433 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7436 err = got_ref_resolve(commit_id, repo, commit_ref);
7440 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7443 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7447 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7451 free(commit_ref_name);
7452 free(branch_ref_name);
7453 free(fileindex_path);
7455 got_ref_close(commit_ref);
7456 if (base_commit_ref)
7457 got_ref_close(base_commit_ref);
7461 free(*base_commit_id);
7462 *base_commit_id = NULL;
7464 got_ref_close(*tmp_branch);
7468 got_fileindex_free(*fileindex);
7471 lock_worktree(worktree, LOCK_EX);
7476 static const struct got_error *
7477 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7479 const struct got_error *err;
7480 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7481 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7483 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7486 err = delete_ref(tmp_branch_name, repo);
7490 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7494 err = delete_ref(base_commit_ref_name, repo);
7498 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7501 err = delete_ref(branch_ref_name, repo);
7505 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7508 err = delete_ref(commit_ref_name, repo);
7512 free(tmp_branch_name);
7513 free(base_commit_ref_name);
7514 free(branch_ref_name);
7515 free(commit_ref_name);
7519 const struct got_error *
7520 got_worktree_histedit_abort(struct got_worktree *worktree,
7521 struct got_fileindex *fileindex, struct got_repository *repo,
7522 struct got_reference *branch, struct got_object_id *base_commit_id,
7523 got_worktree_checkout_cb progress_cb, void *progress_arg)
7525 const struct got_error *err, *unlockerr, *sync_err;
7526 struct got_reference *resolved = NULL;
7527 char *fileindex_path = NULL;
7528 struct got_commit_object *commit = NULL;
7529 struct got_object_id *tree_id = NULL;
7530 struct revert_file_args rfa;
7532 err = lock_worktree(worktree, LOCK_EX);
7536 err = got_object_open_as_commit(&commit, repo,
7537 worktree->base_commit_id);
7541 err = got_ref_open(&resolved, repo,
7542 got_ref_get_symref_target(branch), 0);
7546 err = got_worktree_set_head_ref(worktree, resolved);
7550 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7554 err = got_object_id_by_path(&tree_id, repo, commit,
7555 worktree->path_prefix);
7559 err = delete_histedit_refs(worktree, repo);
7563 err = get_fileindex_path(&fileindex_path, worktree);
7567 rfa.worktree = worktree;
7568 rfa.fileindex = fileindex;
7569 rfa.progress_cb = progress_cb;
7570 rfa.progress_arg = progress_arg;
7571 rfa.patch_cb = NULL;
7572 rfa.patch_arg = NULL;
7574 rfa.unlink_added_files = 0;
7575 err = worktree_status(worktree, "", fileindex, repo,
7576 revert_file, &rfa, NULL, NULL, 1, 0);
7580 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7581 repo, progress_cb, progress_arg, NULL, NULL);
7583 sync_err = sync_fileindex(fileindex, fileindex_path);
7584 if (sync_err && err == NULL)
7587 got_ref_close(resolved);
7589 free(fileindex_path);
7591 unlockerr = lock_worktree(worktree, LOCK_SH);
7592 if (unlockerr && err == NULL)
7597 const struct got_error *
7598 got_worktree_histedit_complete(struct got_worktree *worktree,
7599 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7600 struct got_reference *edited_branch, struct got_repository *repo)
7602 const struct got_error *err, *unlockerr, *sync_err;
7603 struct got_object_id *new_head_commit_id = NULL;
7604 struct got_reference *resolved = NULL;
7605 char *fileindex_path = NULL;
7607 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7611 err = got_ref_open(&resolved, repo,
7612 got_ref_get_symref_target(edited_branch), 0);
7616 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7617 resolved, new_head_commit_id, repo);
7621 err = got_ref_change_ref(resolved, new_head_commit_id);
7625 err = got_ref_write(resolved, repo);
7629 err = got_worktree_set_head_ref(worktree, resolved);
7633 err = delete_histedit_refs(worktree, repo);
7637 err = get_fileindex_path(&fileindex_path, worktree);
7640 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7641 sync_err = sync_fileindex(fileindex, fileindex_path);
7642 if (sync_err && err == NULL)
7645 got_fileindex_free(fileindex);
7646 free(fileindex_path);
7647 free(new_head_commit_id);
7648 unlockerr = lock_worktree(worktree, LOCK_SH);
7649 if (unlockerr && err == NULL)
7654 const struct got_error *
7655 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7656 struct got_object_id *commit_id, struct got_repository *repo)
7658 const struct got_error *err;
7659 char *commit_ref_name;
7661 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7665 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7669 err = delete_ref(commit_ref_name, repo);
7671 free(commit_ref_name);
7675 const struct got_error *
7676 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7677 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7678 struct got_worktree *worktree, const char *refname,
7679 struct got_repository *repo)
7681 const struct got_error *err = NULL;
7682 char *fileindex_path = NULL;
7683 struct check_rebase_ok_arg ok_arg;
7687 *base_branch_ref = NULL;
7689 err = lock_worktree(worktree, LOCK_EX);
7693 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7694 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7695 "cannot integrate a branch into itself; "
7696 "update -b or different branch name required");
7700 err = open_fileindex(fileindex, &fileindex_path, worktree);
7704 /* Preconditions are the same as for rebase. */
7705 ok_arg.worktree = worktree;
7707 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7712 err = got_ref_open(branch_ref, repo, refname, 1);
7716 err = got_ref_open(base_branch_ref, repo,
7717 got_worktree_get_head_ref_name(worktree), 1);
7721 got_ref_close(*branch_ref);
7724 if (*base_branch_ref) {
7725 got_ref_close(*base_branch_ref);
7726 *base_branch_ref = NULL;
7729 got_fileindex_free(*fileindex);
7732 lock_worktree(worktree, LOCK_SH);
7737 const struct got_error *
7738 got_worktree_integrate_continue(struct got_worktree *worktree,
7739 struct got_fileindex *fileindex, struct got_repository *repo,
7740 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7741 got_worktree_checkout_cb progress_cb, void *progress_arg,
7742 got_cancel_cb cancel_cb, void *cancel_arg)
7744 const struct got_error *err = NULL, *sync_err, *unlockerr;
7745 char *fileindex_path = NULL;
7746 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7747 struct got_commit_object *commit = NULL;
7749 err = get_fileindex_path(&fileindex_path, worktree);
7753 err = got_ref_resolve(&commit_id, repo, branch_ref);
7757 err = got_object_open_as_commit(&commit, repo, commit_id);
7761 err = got_object_id_by_path(&tree_id, repo, commit,
7762 worktree->path_prefix);
7766 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7770 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7771 progress_cb, progress_arg, cancel_cb, cancel_arg);
7775 err = got_ref_change_ref(base_branch_ref, commit_id);
7779 err = got_ref_write(base_branch_ref, repo);
7783 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7785 sync_err = sync_fileindex(fileindex, fileindex_path);
7786 if (sync_err && err == NULL)
7790 unlockerr = got_ref_unlock(branch_ref);
7791 if (unlockerr && err == NULL)
7793 got_ref_close(branch_ref);
7795 unlockerr = got_ref_unlock(base_branch_ref);
7796 if (unlockerr && err == NULL)
7798 got_ref_close(base_branch_ref);
7800 got_fileindex_free(fileindex);
7801 free(fileindex_path);
7804 got_object_commit_close(commit);
7806 unlockerr = lock_worktree(worktree, LOCK_SH);
7807 if (unlockerr && err == NULL)
7812 const struct got_error *
7813 got_worktree_integrate_abort(struct got_worktree *worktree,
7814 struct got_fileindex *fileindex, struct got_repository *repo,
7815 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7817 const struct got_error *err = NULL, *unlockerr = NULL;
7819 got_fileindex_free(fileindex);
7821 err = lock_worktree(worktree, LOCK_SH);
7823 unlockerr = got_ref_unlock(branch_ref);
7824 if (unlockerr && err == NULL)
7826 got_ref_close(branch_ref);
7828 unlockerr = got_ref_unlock(base_branch_ref);
7829 if (unlockerr && err == NULL)
7831 got_ref_close(base_branch_ref);
7836 const struct got_error *
7837 got_worktree_merge_postpone(struct got_worktree *worktree,
7838 struct got_fileindex *fileindex)
7840 const struct got_error *err, *sync_err;
7841 char *fileindex_path = NULL;
7843 err = get_fileindex_path(&fileindex_path, worktree);
7847 sync_err = sync_fileindex(fileindex, fileindex_path);
7849 err = lock_worktree(worktree, LOCK_SH);
7850 if (sync_err && err == NULL)
7853 got_fileindex_free(fileindex);
7854 free(fileindex_path);
7858 static const struct got_error *
7859 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7861 const struct got_error *err;
7862 char *branch_refname = NULL, *commit_refname = NULL;
7864 err = get_merge_branch_ref_name(&branch_refname, worktree);
7867 err = delete_ref(branch_refname, repo);
7871 err = get_merge_commit_ref_name(&commit_refname, worktree);
7874 err = delete_ref(commit_refname, repo);
7879 free(branch_refname);
7880 free(commit_refname);
7884 struct merge_commit_msg_arg {
7885 struct got_worktree *worktree;
7886 const char *branch_name;
7889 static const struct got_error *
7890 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
7891 const char *diff_path, char **logmsg, void *arg)
7893 struct merge_commit_msg_arg *a = arg;
7895 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7896 got_worktree_get_head_ref_name(a->worktree)) == -1)
7897 return got_error_from_errno("asprintf");
7903 const struct got_error *
7904 got_worktree_merge_branch(struct got_worktree *worktree,
7905 struct got_fileindex *fileindex,
7906 struct got_object_id *yca_commit_id,
7907 struct got_object_id *branch_tip,
7908 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7909 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7911 const struct got_error *err;
7912 char *fileindex_path = NULL;
7914 err = get_fileindex_path(&fileindex_path, worktree);
7918 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7923 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7924 branch_tip, repo, progress_cb, progress_arg,
7925 cancel_cb, cancel_arg);
7927 free(fileindex_path);
7931 const struct got_error *
7932 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7933 struct got_worktree *worktree, struct got_fileindex *fileindex,
7934 const char *author, const char *committer, int allow_bad_symlinks,
7935 struct got_object_id *branch_tip, const char *branch_name,
7936 int allow_conflict, struct got_repository *repo,
7937 got_worktree_status_cb status_cb, void *status_arg)
7940 const struct got_error *err = NULL, *sync_err;
7941 struct got_pathlist_head commitable_paths;
7942 struct collect_commitables_arg cc_arg;
7943 struct got_pathlist_entry *pe;
7944 struct got_reference *head_ref = NULL;
7945 struct got_object_id *head_commit_id = NULL;
7946 int have_staged_files = 0;
7947 struct merge_commit_msg_arg mcm_arg;
7948 char *fileindex_path = NULL;
7950 memset(&cc_arg, 0, sizeof(cc_arg));
7951 *new_commit_id = NULL;
7953 TAILQ_INIT(&commitable_paths);
7955 err = get_fileindex_path(&fileindex_path, worktree);
7959 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7963 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7967 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7968 &have_staged_files);
7969 if (err && err->code != GOT_ERR_CANCELLED)
7971 if (have_staged_files) {
7972 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7976 cc_arg.commitable_paths = &commitable_paths;
7977 cc_arg.worktree = worktree;
7978 cc_arg.fileindex = fileindex;
7980 cc_arg.have_staged_files = have_staged_files;
7981 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7982 cc_arg.commit_conflicts = allow_conflict;
7983 err = worktree_status(worktree, "", fileindex, repo,
7984 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7988 if (TAILQ_EMPTY(&commitable_paths)) {
7989 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7990 "merge of %s cannot proceed", branch_name);
7994 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7995 struct got_commitable *ct = pe->data;
7996 const char *ct_path = ct->in_repo_path;
7998 while (ct_path[0] == '/')
8000 err = check_out_of_date(ct_path, ct->status,
8001 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
8002 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
8008 mcm_arg.worktree = worktree;
8009 mcm_arg.branch_name = branch_name;
8010 err = commit_worktree(new_commit_id, &commitable_paths,
8011 head_commit_id, branch_tip, worktree, author, committer, NULL,
8012 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8016 err = update_fileindex_after_commit(worktree, &commitable_paths,
8017 *new_commit_id, fileindex, have_staged_files);
8018 sync_err = sync_fileindex(fileindex, fileindex_path);
8019 if (sync_err && err == NULL)
8022 TAILQ_FOREACH(pe, &commitable_paths, entry) {
8023 struct got_commitable *ct = pe->data;
8025 free_commitable(ct);
8027 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8028 free(fileindex_path);
8032 const struct got_error *
8033 got_worktree_merge_complete(struct got_worktree *worktree,
8034 struct got_fileindex *fileindex, struct got_repository *repo)
8036 const struct got_error *err, *unlockerr, *sync_err;
8037 char *fileindex_path = NULL;
8039 err = delete_merge_refs(worktree, repo);
8043 err = get_fileindex_path(&fileindex_path, worktree);
8046 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8047 sync_err = sync_fileindex(fileindex, fileindex_path);
8048 if (sync_err && err == NULL)
8051 got_fileindex_free(fileindex);
8052 free(fileindex_path);
8053 unlockerr = lock_worktree(worktree, LOCK_SH);
8054 if (unlockerr && err == NULL)
8059 const struct got_error *
8060 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8061 struct got_repository *repo)
8063 const struct got_error *err;
8064 char *branch_refname = NULL;
8065 struct got_reference *branch_ref = NULL;
8069 err = get_merge_branch_ref_name(&branch_refname, worktree);
8072 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8073 free(branch_refname);
8075 if (err->code != GOT_ERR_NOT_REF)
8083 const struct got_error *got_worktree_merge_prepare(
8084 struct got_fileindex **fileindex, struct got_worktree *worktree,
8085 struct got_reference *branch, struct got_repository *repo)
8087 const struct got_error *err = NULL;
8088 char *fileindex_path = NULL;
8089 char *branch_refname = NULL, *commit_refname = NULL;
8090 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
8091 struct got_reference *commit_ref = NULL;
8092 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
8093 struct check_rebase_ok_arg ok_arg;
8097 err = lock_worktree(worktree, LOCK_EX);
8101 err = open_fileindex(fileindex, &fileindex_path, worktree);
8105 /* Preconditions are the same as for rebase. */
8106 ok_arg.worktree = worktree;
8108 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8113 err = get_merge_branch_ref_name(&branch_refname, worktree);
8117 err = get_merge_commit_ref_name(&commit_refname, worktree);
8121 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8126 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8130 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8131 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8135 err = got_ref_resolve(&branch_tip, repo, branch);
8139 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8142 err = got_ref_write(branch_ref, repo);
8146 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8149 err = got_ref_write(commit_ref, repo);
8154 free(branch_refname);
8155 free(commit_refname);
8156 free(fileindex_path);
8158 got_ref_close(branch_ref);
8160 got_ref_close(commit_ref);
8162 got_ref_close(wt_branch);
8163 free(wt_branch_tip);
8166 got_fileindex_free(*fileindex);
8169 lock_worktree(worktree, LOCK_SH);
8174 const struct got_error *
8175 got_worktree_merge_continue(char **branch_name,
8176 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8177 struct got_worktree *worktree, struct got_repository *repo)
8179 const struct got_error *err;
8180 char *commit_refname = NULL, *branch_refname = NULL;
8181 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8182 char *fileindex_path = NULL;
8183 int have_staged_files = 0;
8185 *branch_name = NULL;
8189 err = lock_worktree(worktree, LOCK_EX);
8193 err = open_fileindex(fileindex, &fileindex_path, worktree);
8197 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8198 &have_staged_files);
8199 if (err && err->code != GOT_ERR_CANCELLED)
8201 if (have_staged_files) {
8202 err = got_error(GOT_ERR_STAGED_PATHS);
8206 err = get_merge_branch_ref_name(&branch_refname, worktree);
8210 err = get_merge_commit_ref_name(&commit_refname, worktree);
8214 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8218 if (!got_ref_is_symbolic(branch_ref)) {
8219 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8220 "%s is not a symbolic reference",
8221 got_ref_get_name(branch_ref));
8224 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8225 if (*branch_name == NULL) {
8226 err = got_error_from_errno("strdup");
8230 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8234 err = got_ref_resolve(branch_tip, repo, commit_ref);
8238 free(commit_refname);
8239 free(branch_refname);
8240 free(fileindex_path);
8242 got_ref_close(commit_ref);
8244 got_ref_close(branch_ref);
8248 *branch_name = NULL;
8253 got_fileindex_free(*fileindex);
8256 lock_worktree(worktree, LOCK_SH);
8261 const struct got_error *
8262 got_worktree_merge_abort(struct got_worktree *worktree,
8263 struct got_fileindex *fileindex, struct got_repository *repo,
8264 got_worktree_checkout_cb progress_cb, void *progress_arg)
8266 const struct got_error *err, *unlockerr, *sync_err;
8267 struct got_object_id *commit_id = NULL;
8268 struct got_commit_object *commit = NULL;
8269 char *fileindex_path = NULL;
8270 struct revert_file_args rfa;
8271 struct got_object_id *tree_id = NULL;
8273 err = got_object_open_as_commit(&commit, repo,
8274 worktree->base_commit_id);
8278 err = got_object_id_by_path(&tree_id, repo, commit,
8279 worktree->path_prefix);
8283 err = delete_merge_refs(worktree, repo);
8287 err = get_fileindex_path(&fileindex_path, worktree);
8291 rfa.worktree = worktree;
8292 rfa.fileindex = fileindex;
8293 rfa.progress_cb = progress_cb;
8294 rfa.progress_arg = progress_arg;
8295 rfa.patch_cb = NULL;
8296 rfa.patch_arg = NULL;
8298 rfa.unlink_added_files = 1;
8299 err = worktree_status(worktree, "", fileindex, repo,
8300 revert_file, &rfa, NULL, NULL, 1, 0);
8304 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8305 repo, progress_cb, progress_arg, NULL, NULL);
8307 sync_err = sync_fileindex(fileindex, fileindex_path);
8308 if (sync_err && err == NULL)
8314 got_object_commit_close(commit);
8316 got_fileindex_free(fileindex);
8317 free(fileindex_path);
8319 unlockerr = lock_worktree(worktree, LOCK_SH);
8320 if (unlockerr && err == NULL)
8325 struct check_stage_ok_arg {
8326 struct got_object_id *head_commit_id;
8327 struct got_worktree *worktree;
8328 struct got_fileindex *fileindex;
8329 struct got_repository *repo;
8333 static const struct got_error *
8334 check_stage_ok(void *arg, unsigned char status,
8335 unsigned char staged_status, const char *relpath,
8336 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8337 struct got_object_id *commit_id, int dirfd, const char *de_name)
8339 struct check_stage_ok_arg *a = arg;
8340 const struct got_error *err = NULL;
8341 struct got_fileindex_entry *ie;
8342 struct got_object_id base_commit_id;
8343 struct got_object_id *base_commit_idp = NULL;
8344 char *in_repo_path = NULL, *p;
8346 if (status == GOT_STATUS_UNVERSIONED ||
8347 status == GOT_STATUS_NO_CHANGE)
8349 if (status == GOT_STATUS_NONEXISTENT)
8350 return got_error_set_errno(ENOENT, relpath);
8352 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8354 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8356 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8357 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8359 return got_error_from_errno("asprintf");
8361 if (got_fileindex_entry_has_commit(ie)) {
8362 base_commit_idp = got_fileindex_entry_get_commit_id(
8363 &base_commit_id, ie);
8366 if (status == GOT_STATUS_CONFLICT) {
8367 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8369 } else if (status != GOT_STATUS_ADD &&
8370 status != GOT_STATUS_MODIFY &&
8371 status != GOT_STATUS_DELETE) {
8372 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8376 a->have_changes = 1;
8381 err = check_out_of_date(p, status, staged_status,
8382 blob_id, base_commit_idp, a->head_commit_id, a->repo,
8383 GOT_ERR_STAGE_OUT_OF_DATE);
8389 struct stage_path_arg {
8390 struct got_worktree *worktree;
8391 struct got_fileindex *fileindex;
8392 struct got_repository *repo;
8393 got_worktree_status_cb status_cb;
8395 got_worktree_patch_cb patch_cb;
8397 int staged_something;
8398 int allow_bad_symlinks;
8401 static const struct got_error *
8402 stage_path(void *arg, unsigned char status,
8403 unsigned char staged_status, const char *relpath,
8404 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8405 struct got_object_id *commit_id, int dirfd, const char *de_name)
8407 struct stage_path_arg *a = arg;
8408 const struct got_error *err = NULL;
8409 struct got_fileindex_entry *ie;
8410 char *ondisk_path = NULL, *path_content = NULL;
8412 struct got_object_id *new_staged_blob_id = NULL;
8415 if (status == GOT_STATUS_UNVERSIONED)
8418 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8420 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8422 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8424 return got_error_from_errno("asprintf");
8427 case GOT_STATUS_ADD:
8428 case GOT_STATUS_MODIFY:
8429 /* XXX could sb.st_mode be passed in by our caller? */
8430 if (lstat(ondisk_path, &sb) == -1) {
8431 err = got_error_from_errno2("lstat", ondisk_path);
8435 if (status == GOT_STATUS_ADD) {
8436 int choice = GOT_PATCH_CHOICE_NONE;
8437 err = (*a->patch_cb)(&choice, a->patch_arg,
8438 status, ie->path, NULL, 1, 1);
8441 if (choice != GOT_PATCH_CHOICE_YES)
8444 err = create_patched_content(&path_content, 0,
8445 staged_blob_id ? staged_blob_id : blob_id,
8446 ondisk_path, dirfd, de_name, ie->path,
8447 a->repo, a->patch_cb, a->patch_arg);
8448 if (err || path_content == NULL)
8452 err = got_object_blob_create(&new_staged_blob_id,
8453 path_content ? path_content : ondisk_path, a->repo);
8456 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8457 SHA1_DIGEST_LENGTH);
8458 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8459 stage = GOT_FILEIDX_STAGE_ADD;
8461 stage = GOT_FILEIDX_STAGE_MODIFY;
8462 got_fileindex_entry_stage_set(ie, stage);
8463 if (S_ISLNK(sb.st_mode)) {
8464 int is_bad_symlink = 0;
8465 if (!a->allow_bad_symlinks) {
8466 char target_path[PATH_MAX];
8468 target_len = readlink(ondisk_path, target_path,
8469 sizeof(target_path));
8470 if (target_len == -1) {
8471 err = got_error_from_errno2("readlink",
8475 err = is_bad_symlink_target(&is_bad_symlink,
8476 target_path, target_len, ondisk_path,
8477 a->worktree->root_path);
8480 if (is_bad_symlink) {
8481 err = got_error_path(ondisk_path,
8482 GOT_ERR_BAD_SYMLINK);
8487 got_fileindex_entry_staged_filetype_set(ie,
8488 GOT_FILEIDX_MODE_BAD_SYMLINK);
8490 got_fileindex_entry_staged_filetype_set(ie,
8491 GOT_FILEIDX_MODE_SYMLINK);
8493 got_fileindex_entry_staged_filetype_set(ie,
8494 GOT_FILEIDX_MODE_REGULAR_FILE);
8496 a->staged_something = 1;
8497 if (a->status_cb == NULL)
8499 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8500 get_staged_status(ie), relpath, blob_id,
8501 new_staged_blob_id, NULL, dirfd, de_name);
8505 * When staging the reverse of the staged diff,
8506 * implicitly unstage the file.
8508 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8509 sizeof(ie->blob_sha1)) == 0) {
8510 got_fileindex_entry_stage_set(ie,
8511 GOT_FILEIDX_STAGE_NONE);
8514 case GOT_STATUS_DELETE:
8515 if (staged_status == GOT_STATUS_DELETE)
8518 int choice = GOT_PATCH_CHOICE_NONE;
8519 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8520 ie->path, NULL, 1, 1);
8523 if (choice == GOT_PATCH_CHOICE_NO)
8525 if (choice != GOT_PATCH_CHOICE_YES) {
8526 err = got_error(GOT_ERR_PATCH_CHOICE);
8530 stage = GOT_FILEIDX_STAGE_DELETE;
8531 got_fileindex_entry_stage_set(ie, stage);
8532 a->staged_something = 1;
8533 if (a->status_cb == NULL)
8535 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8536 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8539 case GOT_STATUS_NO_CHANGE:
8541 case GOT_STATUS_CONFLICT:
8542 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8544 case GOT_STATUS_NONEXISTENT:
8545 err = got_error_set_errno(ENOENT, relpath);
8548 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8552 if (path_content && unlink(path_content) == -1 && err == NULL)
8553 err = got_error_from_errno2("unlink", path_content);
8556 free(new_staged_blob_id);
8560 const struct got_error *
8561 got_worktree_stage(struct got_worktree *worktree,
8562 struct got_pathlist_head *paths,
8563 got_worktree_status_cb status_cb, void *status_arg,
8564 got_worktree_patch_cb patch_cb, void *patch_arg,
8565 int allow_bad_symlinks, struct got_repository *repo)
8567 const struct got_error *err = NULL, *sync_err, *unlockerr;
8568 struct got_pathlist_entry *pe;
8569 struct got_fileindex *fileindex = NULL;
8570 char *fileindex_path = NULL;
8571 struct got_reference *head_ref = NULL;
8572 struct got_object_id *head_commit_id = NULL;
8573 struct check_stage_ok_arg oka;
8574 struct stage_path_arg spa;
8576 err = lock_worktree(worktree, LOCK_EX);
8580 err = got_ref_open(&head_ref, repo,
8581 got_worktree_get_head_ref_name(worktree), 0);
8584 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8587 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8591 /* Check pre-conditions before staging anything. */
8592 oka.head_commit_id = head_commit_id;
8593 oka.worktree = worktree;
8594 oka.fileindex = fileindex;
8596 oka.have_changes = 0;
8597 TAILQ_FOREACH(pe, paths, entry) {
8598 err = worktree_status(worktree, pe->path, fileindex, repo,
8599 check_stage_ok, &oka, NULL, NULL, 1, 0);
8603 if (!oka.have_changes) {
8604 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8608 spa.worktree = worktree;
8609 spa.fileindex = fileindex;
8611 spa.patch_cb = patch_cb;
8612 spa.patch_arg = patch_arg;
8613 spa.status_cb = status_cb;
8614 spa.status_arg = status_arg;
8615 spa.staged_something = 0;
8616 spa.allow_bad_symlinks = allow_bad_symlinks;
8617 TAILQ_FOREACH(pe, paths, entry) {
8618 err = worktree_status(worktree, pe->path, fileindex, repo,
8619 stage_path, &spa, NULL, NULL, 1, 0);
8623 if (!spa.staged_something) {
8624 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8628 sync_err = sync_fileindex(fileindex, fileindex_path);
8629 if (sync_err && err == NULL)
8633 got_ref_close(head_ref);
8634 free(head_commit_id);
8635 free(fileindex_path);
8637 got_fileindex_free(fileindex);
8638 unlockerr = lock_worktree(worktree, LOCK_SH);
8639 if (unlockerr && err == NULL)
8644 struct unstage_path_arg {
8645 struct got_worktree *worktree;
8646 struct got_fileindex *fileindex;
8647 struct got_repository *repo;
8648 got_worktree_checkout_cb progress_cb;
8650 got_worktree_patch_cb patch_cb;
8654 static const struct got_error *
8655 create_unstaged_content(char **path_unstaged_content,
8656 char **path_new_staged_content, struct got_object_id *blob_id,
8657 struct got_object_id *staged_blob_id, const char *relpath,
8658 struct got_repository *repo,
8659 got_worktree_patch_cb patch_cb, void *patch_arg)
8661 const struct got_error *err, *free_err;
8662 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8663 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8664 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8665 struct got_diffreg_result *diffreg_result = NULL;
8666 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8667 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8668 int fd1 = -1, fd2 = -1;
8670 *path_unstaged_content = NULL;
8671 *path_new_staged_content = NULL;
8673 err = got_object_id_str(&label1, blob_id);
8677 fd1 = got_opentempfd();
8679 err = got_error_from_errno("got_opentempfd");
8682 fd2 = got_opentempfd();
8684 err = got_error_from_errno("got_opentempfd");
8688 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8692 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
8696 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8700 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8705 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
8709 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8713 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8714 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8718 err = got_opentemp_named(path_unstaged_content, &outfile,
8719 "got-unstaged-content", "");
8722 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8723 "got-new-staged-content", "");
8727 if (fseek(f1, 0L, SEEK_SET) == -1) {
8728 err = got_ferror(f1, GOT_ERR_IO);
8731 if (fseek(f2, 0L, SEEK_SET) == -1) {
8732 err = got_ferror(f2, GOT_ERR_IO);
8735 /* Count the number of actual changes in the diff result. */
8736 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8737 struct diff_chunk_context cc = {};
8738 diff_chunk_context_load_change(&cc, &nchunks_used,
8739 diffreg_result->result, n, 0);
8742 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8744 err = apply_or_reject_change(&choice, &nchunks_used,
8745 diffreg_result->result, n, relpath, f1, f2,
8746 &line_cur1, &line_cur2,
8747 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8750 if (choice == GOT_PATCH_CHOICE_YES)
8753 have_rejected_content = 1;
8754 if (choice == GOT_PATCH_CHOICE_QUIT)
8757 if (have_content || have_rejected_content)
8758 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8759 outfile, rejectfile);
8762 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8763 err = got_error_from_errno("close");
8765 got_object_blob_close(blob);
8766 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8767 err = got_error_from_errno("close");
8769 got_object_blob_close(staged_blob);
8770 free_err = got_diffreg_result_free(diffreg_result);
8771 if (free_err && err == NULL)
8773 if (f1 && fclose(f1) == EOF && err == NULL)
8774 err = got_error_from_errno2("fclose", path1);
8775 if (f2 && fclose(f2) == EOF && err == NULL)
8776 err = got_error_from_errno2("fclose", path2);
8777 if (outfile && fclose(outfile) == EOF && err == NULL)
8778 err = got_error_from_errno2("fclose", *path_unstaged_content);
8779 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8780 err = got_error_from_errno2("fclose", *path_new_staged_content);
8781 if (path1 && unlink(path1) == -1 && err == NULL)
8782 err = got_error_from_errno2("unlink", path1);
8783 if (path2 && unlink(path2) == -1 && err == NULL)
8784 err = got_error_from_errno2("unlink", path2);
8785 if (err || !have_content) {
8786 if (*path_unstaged_content &&
8787 unlink(*path_unstaged_content) == -1 && err == NULL)
8788 err = got_error_from_errno2("unlink",
8789 *path_unstaged_content);
8790 free(*path_unstaged_content);
8791 *path_unstaged_content = NULL;
8793 if (err || !have_content || !have_rejected_content) {
8794 if (*path_new_staged_content &&
8795 unlink(*path_new_staged_content) == -1 && err == NULL)
8796 err = got_error_from_errno2("unlink",
8797 *path_new_staged_content);
8798 free(*path_new_staged_content);
8799 *path_new_staged_content = NULL;
8806 static const struct got_error *
8807 unstage_hunks(struct got_object_id *staged_blob_id,
8808 struct got_blob_object *blob_base,
8809 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8810 const char *ondisk_path, const char *label_orig,
8811 struct got_worktree *worktree, struct got_repository *repo,
8812 got_worktree_patch_cb patch_cb, void *patch_arg,
8813 got_worktree_checkout_cb progress_cb, void *progress_arg)
8815 const struct got_error *err = NULL;
8816 char *path_unstaged_content = NULL;
8817 char *path_new_staged_content = NULL;
8818 char *parent = NULL, *base_path = NULL;
8819 char *blob_base_path = NULL;
8820 struct got_object_id *new_staged_blob_id = NULL;
8821 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8824 err = create_unstaged_content(&path_unstaged_content,
8825 &path_new_staged_content, blob_id, staged_blob_id,
8826 ie->path, repo, patch_cb, patch_arg);
8830 if (path_unstaged_content == NULL)
8833 if (path_new_staged_content) {
8834 err = got_object_blob_create(&new_staged_blob_id,
8835 path_new_staged_content, repo);
8840 f = fopen(path_unstaged_content, "re");
8842 err = got_error_from_errno2("fopen",
8843 path_unstaged_content);
8846 if (fstat(fileno(f), &sb) == -1) {
8847 err = got_error_from_errno2("fstat", path_unstaged_content);
8850 if (got_fileindex_entry_staged_filetype_get(ie) ==
8851 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8852 char link_target[PATH_MAX];
8854 r = fread(link_target, 1, sizeof(link_target), f);
8855 if (r == 0 && ferror(f)) {
8856 err = got_error_from_errno("fread");
8859 if (r >= sizeof(link_target)) { /* should not happen */
8860 err = got_error(GOT_ERR_NO_SPACE);
8863 link_target[r] = '\0';
8864 err = merge_symlink(worktree, blob_base,
8865 ondisk_path, ie->path, label_orig, link_target,
8866 worktree->base_commit_id, repo, progress_cb,
8869 int local_changes_subsumed;
8871 err = got_path_dirname(&parent, ondisk_path);
8875 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8877 err = got_error_from_errno("asprintf");
8882 err = got_opentemp_named(&blob_base_path, &f_base,
8886 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8892 * In order the run a 3-way merge with a symlink we copy the symlink's
8893 * target path into a temporary file and use that file with diff3.
8895 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8896 err = dump_symlink_target_path_to_file(&f_deriv2,
8902 fd = open(ondisk_path,
8903 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8905 err = got_error_from_errno2("open", ondisk_path);
8908 f_deriv2 = fdopen(fd, "r");
8909 if (f_deriv2 == NULL) {
8910 err = got_error_from_errno2("fdopen", ondisk_path);
8916 err = merge_file(&local_changes_subsumed, worktree,
8917 f_base, f, f_deriv2, ondisk_path, ie->path,
8918 got_fileindex_perms_to_st(ie),
8919 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8920 repo, progress_cb, progress_arg);
8925 if (new_staged_blob_id) {
8926 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8927 SHA1_DIGEST_LENGTH);
8929 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8930 got_fileindex_entry_staged_filetype_set(ie, 0);
8933 free(new_staged_blob_id);
8934 if (path_unstaged_content &&
8935 unlink(path_unstaged_content) == -1 && err == NULL)
8936 err = got_error_from_errno2("unlink", path_unstaged_content);
8937 if (path_new_staged_content &&
8938 unlink(path_new_staged_content) == -1 && err == NULL)
8939 err = got_error_from_errno2("unlink", path_new_staged_content);
8940 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8941 err = got_error_from_errno2("unlink", blob_base_path);
8942 if (f_base && fclose(f_base) == EOF && err == NULL)
8943 err = got_error_from_errno2("fclose", path_unstaged_content);
8944 if (f && fclose(f) == EOF && err == NULL)
8945 err = got_error_from_errno2("fclose", path_unstaged_content);
8946 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8947 err = got_error_from_errno2("fclose", ondisk_path);
8948 free(path_unstaged_content);
8949 free(path_new_staged_content);
8950 free(blob_base_path);
8956 static const struct got_error *
8957 unstage_path(void *arg, unsigned char status,
8958 unsigned char staged_status, const char *relpath,
8959 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8960 struct got_object_id *commit_id, int dirfd, const char *de_name)
8962 const struct got_error *err = NULL;
8963 struct unstage_path_arg *a = arg;
8964 struct got_fileindex_entry *ie;
8965 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8966 char *ondisk_path = NULL;
8967 char *id_str = NULL, *label_orig = NULL;
8968 int local_changes_subsumed;
8970 int fd1 = -1, fd2 = -1;
8972 if (staged_status != GOT_STATUS_ADD &&
8973 staged_status != GOT_STATUS_MODIFY &&
8974 staged_status != GOT_STATUS_DELETE)
8977 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8979 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8981 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8983 return got_error_from_errno("asprintf");
8985 err = got_object_id_str(&id_str,
8986 commit_id ? commit_id : a->worktree->base_commit_id);
8989 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8991 err = got_error_from_errno("asprintf");
8995 fd1 = got_opentempfd();
8997 err = got_error_from_errno("got_opentempfd");
9000 fd2 = got_opentempfd();
9002 err = got_error_from_errno("got_opentempfd");
9006 switch (staged_status) {
9007 case GOT_STATUS_MODIFY:
9008 err = got_object_open_as_blob(&blob_base, a->repo,
9009 blob_id, 8192, fd1);
9013 case GOT_STATUS_ADD:
9015 if (staged_status == GOT_STATUS_ADD) {
9016 int choice = GOT_PATCH_CHOICE_NONE;
9017 err = (*a->patch_cb)(&choice, a->patch_arg,
9018 staged_status, ie->path, NULL, 1, 1);
9021 if (choice != GOT_PATCH_CHOICE_YES)
9024 err = unstage_hunks(staged_blob_id,
9025 blob_base, blob_id, ie, ondisk_path,
9026 label_orig, a->worktree, a->repo,
9027 a->patch_cb, a->patch_arg,
9028 a->progress_cb, a->progress_arg);
9029 break; /* Done with this file. */
9032 err = got_object_open_as_blob(&blob_staged, a->repo,
9033 staged_blob_id, 8192, fd2);
9036 switch (got_fileindex_entry_staged_filetype_get(ie)) {
9037 case GOT_FILEIDX_MODE_BAD_SYMLINK:
9038 case GOT_FILEIDX_MODE_REGULAR_FILE:
9039 err = merge_blob(&local_changes_subsumed, a->worktree,
9040 blob_base, ondisk_path, relpath,
9041 got_fileindex_perms_to_st(ie), label_orig,
9042 blob_staged, commit_id ? commit_id :
9043 a->worktree->base_commit_id, a->repo,
9044 a->progress_cb, a->progress_arg);
9046 case GOT_FILEIDX_MODE_SYMLINK:
9047 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9048 char *staged_target;
9049 err = got_object_blob_read_to_str(
9050 &staged_target, blob_staged);
9053 err = merge_symlink(a->worktree, blob_base,
9054 ondisk_path, relpath, label_orig,
9055 staged_target, commit_id ? commit_id :
9056 a->worktree->base_commit_id,
9057 a->repo, a->progress_cb, a->progress_arg);
9058 free(staged_target);
9060 err = merge_blob(&local_changes_subsumed,
9061 a->worktree, blob_base, ondisk_path,
9062 relpath, got_fileindex_perms_to_st(ie),
9063 label_orig, blob_staged,
9064 commit_id ? commit_id :
9065 a->worktree->base_commit_id, a->repo,
9066 a->progress_cb, a->progress_arg);
9070 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9074 got_fileindex_entry_stage_set(ie,
9075 GOT_FILEIDX_STAGE_NONE);
9076 got_fileindex_entry_staged_filetype_set(ie, 0);
9079 case GOT_STATUS_DELETE:
9081 int choice = GOT_PATCH_CHOICE_NONE;
9082 err = (*a->patch_cb)(&choice, a->patch_arg,
9083 staged_status, ie->path, NULL, 1, 1);
9086 if (choice == GOT_PATCH_CHOICE_NO)
9088 if (choice != GOT_PATCH_CHOICE_YES) {
9089 err = got_error(GOT_ERR_PATCH_CHOICE);
9093 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9094 got_fileindex_entry_staged_filetype_set(ie, 0);
9095 err = get_file_status(&status, &sb, ie, ondisk_path,
9096 dirfd, de_name, a->repo);
9099 err = (*a->progress_cb)(a->progress_arg, status, relpath);
9104 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9105 err = got_error_from_errno("close");
9107 got_object_blob_close(blob_base);
9108 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9109 err = got_error_from_errno("close");
9111 got_object_blob_close(blob_staged);
9117 const struct got_error *
9118 got_worktree_unstage(struct got_worktree *worktree,
9119 struct got_pathlist_head *paths,
9120 got_worktree_checkout_cb progress_cb, void *progress_arg,
9121 got_worktree_patch_cb patch_cb, void *patch_arg,
9122 struct got_repository *repo)
9124 const struct got_error *err = NULL, *sync_err, *unlockerr;
9125 struct got_pathlist_entry *pe;
9126 struct got_fileindex *fileindex = NULL;
9127 char *fileindex_path = NULL;
9128 struct unstage_path_arg upa;
9130 err = lock_worktree(worktree, LOCK_EX);
9134 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9138 upa.worktree = worktree;
9139 upa.fileindex = fileindex;
9141 upa.progress_cb = progress_cb;
9142 upa.progress_arg = progress_arg;
9143 upa.patch_cb = patch_cb;
9144 upa.patch_arg = patch_arg;
9145 TAILQ_FOREACH(pe, paths, entry) {
9146 err = worktree_status(worktree, pe->path, fileindex, repo,
9147 unstage_path, &upa, NULL, NULL, 1, 0);
9152 sync_err = sync_fileindex(fileindex, fileindex_path);
9153 if (sync_err && err == NULL)
9156 free(fileindex_path);
9158 got_fileindex_free(fileindex);
9159 unlockerr = lock_worktree(worktree, LOCK_SH);
9160 if (unlockerr && err == NULL)
9165 struct report_file_info_arg {
9166 struct got_worktree *worktree;
9167 got_worktree_path_info_cb info_cb;
9169 struct got_pathlist_head *paths;
9170 got_cancel_cb cancel_cb;
9174 static const struct got_error *
9175 report_file_info(void *arg, struct got_fileindex_entry *ie)
9177 struct report_file_info_arg *a = arg;
9178 struct got_pathlist_entry *pe;
9179 struct got_object_id blob_id, staged_blob_id, commit_id;
9180 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9181 struct got_object_id *commit_idp = NULL;
9184 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9185 return got_error(GOT_ERR_CANCELLED);
9187 TAILQ_FOREACH(pe, a->paths, entry) {
9188 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9189 got_path_is_child(ie->path, pe->path, pe->path_len))
9192 if (pe == NULL) /* not found */
9195 if (got_fileindex_entry_has_blob(ie))
9196 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9197 stage = got_fileindex_entry_stage_get(ie);
9198 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9199 stage == GOT_FILEIDX_STAGE_ADD) {
9200 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9201 &staged_blob_id, ie);
9204 if (got_fileindex_entry_has_commit(ie))
9205 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9207 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9208 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9211 const struct got_error *
9212 got_worktree_path_info(struct got_worktree *worktree,
9213 struct got_pathlist_head *paths,
9214 got_worktree_path_info_cb info_cb, void *info_arg,
9215 got_cancel_cb cancel_cb, void *cancel_arg)
9218 const struct got_error *err = NULL, *unlockerr;
9219 struct got_fileindex *fileindex = NULL;
9220 char *fileindex_path = NULL;
9221 struct report_file_info_arg arg;
9223 err = lock_worktree(worktree, LOCK_SH);
9227 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9231 arg.worktree = worktree;
9232 arg.info_cb = info_cb;
9233 arg.info_arg = info_arg;
9235 arg.cancel_cb = cancel_cb;
9236 arg.cancel_arg = cancel_arg;
9237 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9240 free(fileindex_path);
9242 got_fileindex_free(fileindex);
9243 unlockerr = lock_worktree(worktree, LOCK_UN);
9244 if (unlockerr && err == NULL)
9249 static const struct got_error *
9250 patch_check_path(const char *p, char **path, unsigned char *status,
9251 unsigned char *staged_status, struct got_fileindex *fileindex,
9252 struct got_worktree *worktree, struct got_repository *repo)
9254 const struct got_error *err;
9255 struct got_fileindex_entry *ie;
9257 char *ondisk_path = NULL;
9259 err = got_worktree_resolve_path(path, worktree, p);
9263 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9264 *path[0] ? "/" : "", *path) == -1)
9265 return got_error_from_errno("asprintf");
9267 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9269 *staged_status = get_staged_status(ie);
9270 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9275 *staged_status = GOT_STATUS_NO_CHANGE;
9276 *status = GOT_STATUS_UNVERSIONED;
9277 if (lstat(ondisk_path, &sb) == -1) {
9278 if (errno != ENOENT) {
9279 err = got_error_from_errno2("lstat",
9283 *status = GOT_STATUS_NONEXISTENT;
9292 static const struct got_error *
9293 patch_can_rm(const char *path, unsigned char status,
9294 unsigned char staged_status)
9296 if (status == GOT_STATUS_NONEXISTENT)
9297 return got_error_set_errno(ENOENT, path);
9298 if (status != GOT_STATUS_NO_CHANGE &&
9299 status != GOT_STATUS_ADD &&
9300 status != GOT_STATUS_MODIFY &&
9301 status != GOT_STATUS_MODE_CHANGE)
9302 return got_error_path(path, GOT_ERR_FILE_STATUS);
9303 if (staged_status == GOT_STATUS_DELETE)
9304 return got_error_path(path, GOT_ERR_FILE_STATUS);
9308 static const struct got_error *
9309 patch_can_add(const char *path, unsigned char status)
9311 if (status != GOT_STATUS_NONEXISTENT)
9312 return got_error_path(path, GOT_ERR_FILE_STATUS);
9316 static const struct got_error *
9317 patch_can_edit(const char *path, unsigned char status,
9318 unsigned char staged_status)
9320 if (status == GOT_STATUS_NONEXISTENT)
9321 return got_error_set_errno(ENOENT, path);
9322 if (status != GOT_STATUS_NO_CHANGE &&
9323 status != GOT_STATUS_ADD &&
9324 status != GOT_STATUS_MODIFY)
9325 return got_error_path(path, GOT_ERR_FILE_STATUS);
9326 if (staged_status == GOT_STATUS_DELETE)
9327 return got_error_path(path, GOT_ERR_FILE_STATUS);
9331 const struct got_error *
9332 got_worktree_patch_prepare(struct got_fileindex **fileindex,
9333 char **fileindex_path, struct got_worktree *worktree)
9335 return open_fileindex(fileindex, fileindex_path, worktree);
9338 const struct got_error *
9339 got_worktree_patch_check_path(const char *old, const char *new,
9340 char **oldpath, char **newpath, struct got_worktree *worktree,
9341 struct got_repository *repo, struct got_fileindex *fileindex)
9343 const struct got_error *err = NULL;
9344 int file_renamed = 0;
9345 unsigned char status_old, staged_status_old;
9346 unsigned char status_new, staged_status_new;
9351 err = patch_check_path(old != NULL ? old : new, oldpath,
9352 &status_old, &staged_status_old, fileindex, worktree, repo);
9356 err = patch_check_path(new != NULL ? new : old, newpath,
9357 &status_new, &staged_status_new, fileindex, worktree, repo);
9361 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9364 if (old != NULL && new == NULL)
9365 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9366 else if (file_renamed) {
9367 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9369 err = patch_can_add(*newpath, status_new);
9370 } else if (old == NULL)
9371 err = patch_can_add(*newpath, status_new);
9373 err = patch_can_edit(*newpath, status_new, staged_status_new);
9385 const struct got_error *
9386 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9387 struct got_worktree *worktree, struct got_fileindex *fileindex,
9388 got_worktree_checkout_cb progress_cb, void *progress_arg)
9390 struct schedule_addition_args saa;
9392 memset(&saa, 0, sizeof(saa));
9393 saa.worktree = worktree;
9394 saa.fileindex = fileindex;
9395 saa.progress_cb = progress_cb;
9396 saa.progress_arg = progress_arg;
9399 return worktree_status(worktree, path, fileindex, repo,
9400 schedule_addition, &saa, NULL, NULL, 1, 0);
9403 const struct got_error *
9404 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9405 struct got_worktree *worktree, struct got_fileindex *fileindex,
9406 got_worktree_delete_cb progress_cb, void *progress_arg)
9408 struct schedule_deletion_args sda;
9410 memset(&sda, 0, sizeof(sda));
9411 sda.worktree = worktree;
9412 sda.fileindex = fileindex;
9413 sda.progress_cb = progress_cb;
9414 sda.progress_arg = progress_arg;
9416 sda.delete_local_mods = 0;
9417 sda.keep_on_disk = 0;
9418 sda.ignore_missing_paths = 0;
9419 sda.status_codes = NULL;
9421 return worktree_status(worktree, path, fileindex, repo,
9422 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9425 const struct got_error *
9426 got_worktree_patch_complete(struct got_fileindex *fileindex,
9427 const char *fileindex_path)
9429 const struct got_error *err = NULL;
9431 err = sync_fileindex(fileindex, fileindex_path);
9432 got_fileindex_free(fileindex);