2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/queue.h>
38 #include "got_error.h"
39 #include "got_repository.h"
40 #include "got_reference.h"
41 #include "got_object.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
45 #include "got_opentemp.h"
48 #include "got_lib_worktree.h"
49 #include "got_lib_sha1.h"
50 #include "got_lib_fileindex.h"
51 #include "got_lib_inflate.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_object_idset.h"
57 #include "got_lib_diff.h"
58 #include "got_lib_gotconfig.h"
61 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
64 #define GOT_MERGE_LABEL_MERGED "merged change"
65 #define GOT_MERGE_LABEL_BASE "3-way merge base"
67 static mode_t apply_umask(mode_t);
69 static const struct got_error *
70 create_meta_file(const char *path_got, const char *name, const char *content)
72 const struct got_error *err = NULL;
75 if (asprintf(&path, "%s/%s", path_got, name) == -1)
76 return got_error_from_errno("asprintf");
78 err = got_path_create_file(path, content);
83 static const struct got_error *
84 update_meta_file(const char *path_got, const char *name, const char *content)
86 const struct got_error *err = NULL;
91 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
92 err = got_error_from_errno("asprintf");
97 err = got_opentemp_named(&tmppath, &tmpfile, path, "");
102 int len = fprintf(tmpfile, "%s\n", content);
103 if (len != strlen(content) + 1) {
104 err = got_error_from_errno2("fprintf", tmppath);
109 if (rename(tmppath, path) != 0) {
110 err = got_error_from_errno3("rename", tmppath, path);
116 if (fclose(tmpfile) == EOF && err == NULL)
117 err = got_error_from_errno2("fclose", tmppath);
122 static const struct got_error *
123 write_head_ref(const char *path_got, struct got_reference *head_ref)
125 const struct got_error *err = NULL;
128 if (got_ref_is_symbolic(head_ref)) {
129 refstr = got_ref_to_str(head_ref);
131 return got_error_from_errno("got_ref_to_str");
133 refstr = strdup(got_ref_get_name(head_ref));
135 return got_error_from_errno("strdup");
137 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
142 const struct got_error *
143 got_worktree_init(const char *path, struct got_reference *head_ref,
144 const char *prefix, struct got_repository *repo)
146 const struct got_error *err = NULL;
147 struct got_object_id *commit_id = NULL;
149 uint32_t uuid_status;
151 char *path_got = NULL;
152 char *formatstr = NULL;
153 char *absprefix = NULL;
154 char *basestr = NULL;
155 char *uuidstr = NULL;
157 if (strcmp(path, got_repo_get_path(repo)) == 0) {
158 err = got_error(GOT_ERR_WORKTREE_REPO);
162 err = got_ref_resolve(&commit_id, repo, head_ref);
165 err = got_object_get_type(&obj_type, repo, commit_id);
168 if (obj_type != GOT_OBJ_TYPE_COMMIT)
169 return got_error(GOT_ERR_OBJ_TYPE);
171 if (!got_path_is_absolute(prefix)) {
172 if (asprintf(&absprefix, "/%s", prefix) == -1)
173 return got_error_from_errno("asprintf");
176 /* Create top-level directory (may already exist). */
177 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
178 err = got_error_from_errno2("mkdir", path);
182 /* Create .got directory (may already exist). */
183 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
184 err = got_error_from_errno("asprintf");
187 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
188 err = got_error_from_errno2("mkdir", path_got);
192 /* Create an empty lock file. */
193 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
197 /* Create an empty file index. */
198 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
202 /* Write the HEAD reference. */
203 err = write_head_ref(path_got, head_ref);
207 /* Record our base commit. */
208 err = got_object_id_str(&basestr, commit_id);
211 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
215 /* Store path to repository. */
216 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
217 got_repo_get_path(repo));
221 /* Store in-repository path prefix. */
222 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
223 absprefix ? absprefix : prefix);
228 uuid_create(&uuid, &uuid_status);
229 if (uuid_status != uuid_s_ok) {
230 err = got_error_uuid(uuid_status, "uuid_create");
233 uuid_to_string(&uuid, &uuidstr, &uuid_status);
234 if (uuid_status != uuid_s_ok) {
235 err = got_error_uuid(uuid_status, "uuid_to_string");
238 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
242 /* Stamp work tree with format file. */
243 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
244 err = got_error_from_errno("asprintf");
247 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
261 const struct got_error *
262 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
263 const char *path_prefix)
265 char *absprefix = NULL;
267 if (!got_path_is_absolute(path_prefix)) {
268 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
269 return got_error_from_errno("asprintf");
271 *match = (strcmp(absprefix ? absprefix : path_prefix,
272 worktree->path_prefix) == 0);
278 got_worktree_get_head_ref_name(struct got_worktree *worktree)
280 return worktree->head_ref_name;
283 const struct got_error *
284 got_worktree_set_head_ref(struct got_worktree *worktree,
285 struct got_reference *head_ref)
287 const struct got_error *err = NULL;
288 char *path_got = NULL, *head_ref_name = NULL;
290 if (asprintf(&path_got, "%s/%s", worktree->root_path,
291 GOT_WORKTREE_GOT_DIR) == -1) {
292 err = got_error_from_errno("asprintf");
297 head_ref_name = strdup(got_ref_get_name(head_ref));
298 if (head_ref_name == NULL) {
299 err = got_error_from_errno("strdup");
303 err = write_head_ref(path_got, head_ref);
307 free(worktree->head_ref_name);
308 worktree->head_ref_name = head_ref_name;
316 struct got_object_id *
317 got_worktree_get_base_commit_id(struct got_worktree *worktree)
319 return worktree->base_commit_id;
322 const struct got_error *
323 got_worktree_set_base_commit_id(struct got_worktree *worktree,
324 struct got_repository *repo, struct got_object_id *commit_id)
326 const struct got_error *err;
327 struct got_object *obj = NULL;
329 char *path_got = NULL;
331 if (asprintf(&path_got, "%s/%s", worktree->root_path,
332 GOT_WORKTREE_GOT_DIR) == -1) {
333 err = got_error_from_errno("asprintf");
338 err = got_object_open(&obj, repo, commit_id);
342 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
343 err = got_error(GOT_ERR_OBJ_TYPE);
347 /* Record our base commit. */
348 err = got_object_id_str(&id_str, commit_id);
351 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
355 free(worktree->base_commit_id);
356 worktree->base_commit_id = got_object_id_dup(commit_id);
357 if (worktree->base_commit_id == NULL) {
358 err = got_error_from_errno("got_object_id_dup");
363 got_object_close(obj);
369 const struct got_gotconfig *
370 got_worktree_get_gotconfig(struct got_worktree *worktree)
372 return worktree->gotconfig;
375 static const struct got_error *
376 lock_worktree(struct got_worktree *worktree, int operation)
378 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
379 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
380 : got_error_from_errno2("flock",
381 got_worktree_get_root_path(worktree)));
385 static const struct got_error *
386 add_dir_on_disk(struct got_worktree *worktree, const char *path)
388 const struct got_error *err = NULL;
391 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
392 return got_error_from_errno("asprintf");
394 err = got_path_mkdir(abspath);
395 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
398 if (lstat(abspath, &sb) == -1) {
399 err = got_error_from_errno2("lstat", abspath);
400 } else if (!S_ISDIR(sb.st_mode)) {
401 /* TODO directory is obstructed; do something */
402 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
409 static const struct got_error *
410 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
412 const struct got_error *err = NULL;
415 size_t flen1 = 0, flen2 = 0;
420 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
421 if (flen1 == 0 && ferror(f1)) {
422 err = got_error_from_errno("fread");
425 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
426 if (flen2 == 0 && ferror(f2)) {
427 err = got_error_from_errno("fread");
434 } else if (flen2 == 0) {
438 } else if (flen1 == flen2) {
439 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
452 static const struct got_error *
453 check_files_equal(int *same, FILE *f1, FILE *f2)
460 if (fstat(fileno(f1), &sb) != 0)
461 return got_error_from_errno("fstat");
464 if (fstat(fileno(f2), &sb) != 0)
465 return got_error_from_errno("fstat");
468 if (size1 != size2) {
473 if (fseek(f1, 0L, SEEK_SET) == -1)
474 return got_ferror(f1, GOT_ERR_IO);
475 if (fseek(f2, 0L, SEEK_SET) == -1)
476 return got_ferror(f2, GOT_ERR_IO);
478 return check_file_contents_equal(same, f1, f2);
481 static const struct got_error *
482 copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
490 if (fseek(f, 0L, SEEK_SET) == -1)
491 return got_ferror(f, GOT_ERR_IO);
494 flen = fread(fbuf, 1, sizeof(fbuf), f);
497 return got_error_from_errno("fread");
501 outlen = write(outfd, fbuf, flen);
503 return got_error_from_errno("write");
505 return got_error(GOT_ERR_IO);
512 static const struct got_error *
513 merge_binary_file(int *overlapcnt, int merged_fd,
514 FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
515 const char *label_deriv, const char *label_orig, const char *label_deriv2,
516 const char *ondisk_path)
518 const struct got_error *err = NULL;
519 int same_content, changed_deriv, changed_deriv2;
520 int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
521 off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
522 char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
523 char *base_path_orig = NULL, *base_path_deriv = NULL;
524 char *base_path_deriv2 = NULL;
528 err = check_files_equal(&same_content, f_deriv, f_deriv2);
533 return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
535 err = check_files_equal(&same_content, f_deriv, f_orig);
538 changed_deriv = !same_content;
539 err = check_files_equal(&same_content, f_deriv2, f_orig);
542 changed_deriv2 = !same_content;
544 if (changed_deriv && changed_deriv2) {
546 if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
547 err = got_error_from_errno("asprintf");
550 if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
551 err = got_error_from_errno("asprintf");
554 if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
555 err = got_error_from_errno("asprintf");
558 err = got_opentemp_named_fd(&path_orig, &fd_orig,
562 err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
563 base_path_deriv, "");
566 err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
567 base_path_deriv2, "");
570 err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
573 err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
576 err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
579 if (dprintf(merged_fd, "Binary files differ and cannot be "
580 "merged automatically:\n") < 0) {
581 err = got_error_from_errno("dprintf");
584 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
585 GOT_DIFF_CONFLICT_MARKER_BEGIN,
586 label_deriv ? " " : "",
587 label_deriv ? label_deriv : "",
589 err = got_error_from_errno("dprintf");
593 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
594 GOT_DIFF_CONFLICT_MARKER_ORIG,
595 label_orig ? " " : "",
596 label_orig ? label_orig : "",
598 err = got_error_from_errno("dprintf");
602 if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
603 GOT_DIFF_CONFLICT_MARKER_SEP,
605 GOT_DIFF_CONFLICT_MARKER_END,
606 label_deriv2 ? " " : "",
607 label_deriv2 ? label_deriv2 : "") < 0) {
608 err = got_error_from_errno("dprintf");
611 } else if (changed_deriv)
612 err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
613 else if (changed_deriv2)
614 err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
616 if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
618 err = got_error_from_errno2("unlink", path_orig);
619 if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
620 err = got_error_from_errno2("close", path_orig);
621 if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
622 err = got_error_from_errno2("close", path_deriv);
623 if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
624 err = got_error_from_errno2("close", path_deriv2);
628 free(base_path_orig);
629 free(base_path_deriv);
630 free(base_path_deriv2);
635 * Perform a 3-way merge where the file f_orig acts as the common
636 * ancestor, the file f_deriv acts as the first derived version,
637 * and the file f_deriv2 acts as the second derived version.
638 * The merge result will be written to a new file at ondisk_path; any
639 * existing file at this path will be replaced.
641 static const struct got_error *
642 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
643 FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
644 const char *path, uint16_t st_mode,
645 const char *label_orig, const char *label_deriv, const char *label_deriv2,
646 enum got_diff_algorithm diff_algo, struct got_repository *repo,
647 got_worktree_checkout_cb progress_cb, void *progress_arg)
649 const struct got_error *err = NULL;
651 FILE *f_merged = NULL;
652 char *merged_path = NULL, *base_path = NULL;
656 *local_changes_subsumed = 0;
658 err = got_path_dirname(&parent, ondisk_path);
662 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
663 err = got_error_from_errno("asprintf");
667 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path, "");
671 err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
672 f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
674 if (err->code != GOT_ERR_FILE_BINARY)
676 err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
677 f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
683 err = (*progress_cb)(progress_arg,
684 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
688 if (fsync(merged_fd) != 0) {
689 err = got_error_from_errno("fsync");
693 f_merged = fdopen(merged_fd, "r");
694 if (f_merged == NULL) {
695 err = got_error_from_errno("fdopen");
700 /* Check if a clean merge has subsumed all local changes. */
701 if (overlapcnt == 0) {
702 err = check_files_equal(local_changes_subsumed, f_deriv,
708 if (fchmod(fileno(f_merged), apply_umask(st_mode)) != 0) {
709 err = got_error_from_errno2("fchmod", merged_path);
713 if (rename(merged_path, ondisk_path) != 0) {
714 err = got_error_from_errno3("rename", merged_path,
723 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
724 err = got_error_from_errno("close");
725 if (f_merged && fclose(f_merged) == EOF && err == NULL)
726 err = got_error_from_errno("fclose");
733 static const struct got_error *
734 update_symlink(const char *ondisk_path, const char *target_path,
737 /* This is not atomic but matches what 'ln -sf' does. */
738 if (unlink(ondisk_path) == -1)
739 return got_error_from_errno2("unlink", ondisk_path);
740 if (symlink(target_path, ondisk_path) == -1)
741 return got_error_from_errno3("symlink", target_path,
747 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
748 * in the work tree with a file that contains conflict markers and the
749 * conflicting target paths of the original version, a "derived version"
750 * of a symlink from an incoming change, and a local version of the symlink.
752 * The original versions's target path can be NULL if it is not available,
753 * such as if both derived versions added a new symlink at the same path.
755 * The incoming derived symlink target is NULL in case the incoming change
756 * has deleted this symlink.
758 static const struct got_error *
759 install_symlink_conflict(const char *deriv_target,
760 struct got_object_id *deriv_base_commit_id, const char *orig_target,
761 const char *label_orig, const char *local_target, const char *ondisk_path)
763 const struct got_error *err;
764 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
767 err = got_object_id_str(&id_str, deriv_base_commit_id);
769 return got_error_from_errno("asprintf");
771 if (asprintf(&label_deriv, "%s: commit %s",
772 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
773 err = got_error_from_errno("asprintf");
777 err = got_opentemp_named(&path, &f, "got-symlink-conflict", "");
781 if (fchmod(fileno(f), apply_umask(GOT_DEFAULT_FILE_MODE)) == -1) {
782 err = got_error_from_errno2("fchmod", path);
786 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
787 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
788 deriv_target ? deriv_target : "(symlink was deleted)",
789 orig_target ? label_orig : "",
790 orig_target ? "\n" : "",
791 orig_target ? orig_target : "",
792 orig_target ? "\n" : "",
793 GOT_DIFF_CONFLICT_MARKER_SEP,
794 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
795 err = got_error_from_errno2("fprintf", path);
799 if (unlink(ondisk_path) == -1) {
800 err = got_error_from_errno2("unlink", ondisk_path);
803 if (rename(path, ondisk_path) == -1) {
804 err = got_error_from_errno3("rename", path, ondisk_path);
808 if (f != NULL && fclose(f) == EOF && err == NULL)
809 err = got_error_from_errno2("fclose", path);
816 /* forward declaration */
817 static const struct got_error *
818 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
819 const char *, const char *, uint16_t, const char *,
820 struct got_blob_object *, struct got_object_id *,
821 struct got_repository *, got_worktree_checkout_cb, void *);
824 * Merge a symlink into the work tree, where blob_orig acts as the common
825 * ancestor, deriv_target is the link target of the first derived version,
826 * and the symlink on disk acts as the second derived version.
827 * Assume that contents of both blobs represent symlinks.
829 static const struct got_error *
830 merge_symlink(struct got_worktree *worktree,
831 struct got_blob_object *blob_orig, const char *ondisk_path,
832 const char *path, const char *label_orig, const char *deriv_target,
833 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
834 got_worktree_checkout_cb progress_cb, void *progress_arg)
836 const struct got_error *err = NULL;
837 char *ancestor_target = NULL;
839 ssize_t ondisk_len, deriv_len;
840 char ondisk_target[PATH_MAX];
841 int have_local_change = 0;
842 int have_incoming_change = 0;
844 if (lstat(ondisk_path, &sb) == -1)
845 return got_error_from_errno2("lstat", ondisk_path);
847 ondisk_len = readlink(ondisk_path, ondisk_target,
848 sizeof(ondisk_target));
849 if (ondisk_len == -1) {
850 err = got_error_from_errno2("readlink",
854 ondisk_target[ondisk_len] = '\0';
857 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
862 if (ancestor_target == NULL ||
863 (ondisk_len != strlen(ancestor_target) ||
864 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
865 have_local_change = 1;
867 deriv_len = strlen(deriv_target);
868 if (ancestor_target == NULL ||
869 (deriv_len != strlen(ancestor_target) ||
870 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
871 have_incoming_change = 1;
873 if (!have_local_change && !have_incoming_change) {
874 if (ancestor_target) {
875 /* Both sides made the same change. */
876 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
878 } else if (deriv_len == ondisk_len &&
879 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
880 /* Both sides added the same symlink. */
881 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
884 /* Both sides added symlinks which don't match. */
885 err = install_symlink_conflict(deriv_target,
886 deriv_base_commit_id, ancestor_target,
887 label_orig, ondisk_target, ondisk_path);
890 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
893 } else if (!have_local_change && have_incoming_change) {
894 /* Apply the incoming change. */
895 err = update_symlink(ondisk_path, deriv_target,
896 strlen(deriv_target));
899 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
900 } else if (have_local_change && have_incoming_change) {
901 if (deriv_len == ondisk_len &&
902 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
903 /* Both sides made the same change. */
904 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
907 err = install_symlink_conflict(deriv_target,
908 deriv_base_commit_id, ancestor_target, label_orig,
909 ondisk_target, ondisk_path);
912 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
918 free(ancestor_target);
922 static const struct got_error *
923 dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
925 const struct got_error *err = NULL;
926 char target_path[PATH_MAX];
935 return got_error_from_errno("got_opentemp");
936 target_len = readlink(ondisk_path, target_path, sizeof(target_path));
937 if (target_len == -1) {
938 err = got_error_from_errno2("readlink", ondisk_path);
941 n = fwrite(target_path, 1, target_len, f);
942 if (n != target_len) {
943 err = got_ferror(f, GOT_ERR_IO);
946 if (fflush(f) == EOF) {
947 err = got_error_from_errno("fflush");
950 if (fseek(f, 0L, SEEK_SET) == -1) {
951 err = got_ferror(f, GOT_ERR_IO);
963 * Perform a 3-way merge where blob_orig acts as the common ancestor,
964 * blob_deriv acts as the first derived version, and the file on disk
965 * acts as the second derived version.
967 static const struct got_error *
968 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
969 struct got_blob_object *blob_orig, const char *ondisk_path,
970 const char *path, uint16_t st_mode, const char *label_orig,
971 struct got_blob_object *blob_deriv,
972 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
973 got_worktree_checkout_cb progress_cb, void *progress_arg)
975 const struct got_error *err = NULL;
976 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
977 char *blob_orig_path = NULL;
978 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
979 char *label_deriv = NULL, *parent = NULL;
981 *local_changes_subsumed = 0;
983 err = got_path_dirname(&parent, ondisk_path);
988 if (asprintf(&base_path, "%s/got-merge-blob-orig",
990 err = got_error_from_errno("asprintf");
995 err = got_opentemp_named(&blob_orig_path, &f_orig,
999 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1006 * No common ancestor exists. This is an "add vs add" conflict
1007 * and we simply use an empty ancestor file to make both files
1008 * appear in the merged result in their entirety.
1010 f_orig = got_opentemp();
1011 if (f_orig == NULL) {
1012 err = got_error_from_errno("got_opentemp");
1017 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1018 err = got_error_from_errno("asprintf");
1023 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path, "");
1026 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1031 err = got_object_id_str(&id_str, deriv_base_commit_id);
1034 if (asprintf(&label_deriv, "%s: commit %s",
1035 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1036 err = got_error_from_errno("asprintf");
1041 * In order the run a 3-way merge with a symlink we copy the symlink's
1042 * target path into a temporary file and use that file with diff3.
1044 if (S_ISLNK(st_mode)) {
1045 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1050 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1052 err = got_error_from_errno2("open", ondisk_path);
1055 f_deriv2 = fdopen(fd, "r");
1056 if (f_deriv2 == NULL) {
1057 err = got_error_from_errno2("fdopen", ondisk_path);
1063 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1064 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1065 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1067 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1068 err = got_error_from_errno("fclose");
1069 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1070 err = got_error_from_errno("fclose");
1071 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1072 err = got_error_from_errno("fclose");
1074 if (blob_orig_path) {
1075 unlink(blob_orig_path);
1076 free(blob_orig_path);
1078 if (blob_deriv_path) {
1079 unlink(blob_deriv_path);
1080 free(blob_deriv_path);
1088 static const struct got_error *
1089 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1090 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1091 int wt_fd, const char *path, struct got_object_id *blob_id)
1093 const struct got_error *err = NULL;
1094 struct got_fileindex_entry *new_ie;
1098 err = got_fileindex_entry_alloc(&new_ie, path);
1102 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1103 blob_id->sha1, base_commit_id->sha1, 1);
1107 err = got_fileindex_entry_add(fileindex, new_ie);
1110 got_fileindex_entry_free(new_ie);
1117 get_ondisk_perms(int executable, mode_t st_mode)
1119 mode_t xbits = S_IXUSR;
1122 /* Map read bits to execute bits. */
1123 if (st_mode & S_IRGRP)
1125 if (st_mode & S_IROTH)
1127 return st_mode | xbits;
1134 apply_umask(mode_t mode)
1143 /* forward declaration */
1144 static const struct got_error *
1145 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1146 const char *path, mode_t te_mode, mode_t st_mode,
1147 struct got_blob_object *blob, int restoring_missing_file,
1148 int reverting_versioned_file, int installing_bad_symlink,
1149 int path_is_unversioned, struct got_repository *repo,
1150 got_worktree_checkout_cb progress_cb, void *progress_arg);
1153 * This function assumes that the provided symlink target points at a
1154 * safe location in the work tree!
1156 static const struct got_error *
1157 replace_existing_symlink(int *did_something, const char *ondisk_path,
1158 const char *target_path, size_t target_len)
1160 const struct got_error *err = NULL;
1162 char etarget[PATH_MAX];
1168 * "Bad" symlinks (those pointing outside the work tree or into the
1169 * .got directory) are installed in the work tree as a regular file
1170 * which contains the bad symlink target path.
1171 * The new symlink target has already been checked for safety by our
1172 * caller. If we can successfully open a regular file then we simply
1173 * replace this file with a symlink below.
1175 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1177 if (!got_err_open_nofollow_on_symlink())
1178 return got_error_from_errno2("open", ondisk_path);
1180 /* We are updating an existing on-disk symlink. */
1181 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1183 return got_error_from_errno2("readlink", ondisk_path);
1185 if (elen == target_len &&
1186 memcmp(etarget, target_path, target_len) == 0)
1187 return NULL; /* nothing to do */
1191 err = update_symlink(ondisk_path, target_path, target_len);
1192 if (fd != -1 && close(fd) == -1 && err == NULL)
1193 err = got_error_from_errno2("close", ondisk_path);
1197 static const struct got_error *
1198 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1199 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1201 const struct got_error *err = NULL;
1202 char canonpath[PATH_MAX];
1203 char *path_got = NULL;
1205 *is_bad_symlink = 0;
1207 if (target_len >= sizeof(canonpath)) {
1208 *is_bad_symlink = 1;
1213 * We do not use realpath(3) to resolve the symlink's target
1214 * path because we don't want to resolve symlinks recursively.
1215 * Instead we make the path absolute and then canonicalize it.
1216 * Relative symlink target lookup should begin at the directory
1217 * in which the blob object is being installed.
1219 if (!got_path_is_absolute(target_path)) {
1220 char *abspath, *parent;
1221 err = got_path_dirname(&parent, ondisk_path);
1224 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1226 return got_error_from_errno("asprintf");
1229 if (strlen(abspath) >= sizeof(canonpath)) {
1230 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1234 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1239 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1244 /* Only allow symlinks pointing at paths within the work tree. */
1245 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1246 *is_bad_symlink = 1;
1250 /* Do not allow symlinks pointing into the .got directory. */
1251 if (asprintf(&path_got, "%s/%s", wtroot_path,
1252 GOT_WORKTREE_GOT_DIR) == -1)
1253 return got_error_from_errno("asprintf");
1254 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1255 *is_bad_symlink = 1;
1261 static const struct got_error *
1262 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1263 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1264 int restoring_missing_file, int reverting_versioned_file,
1265 int path_is_unversioned, int allow_bad_symlinks,
1266 struct got_repository *repo,
1267 got_worktree_checkout_cb progress_cb, void *progress_arg)
1269 const struct got_error *err = NULL;
1270 char target_path[PATH_MAX];
1271 size_t len, target_len = 0;
1272 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1273 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1275 *is_bad_symlink = 0;
1278 * Blob object content specifies the target path of the link.
1279 * If a symbolic link cannot be installed we instead create
1280 * a regular file which contains the link target path stored
1281 * in the blob object.
1284 err = got_object_blob_read_block(&len, blob);
1288 if (len + target_len >= sizeof(target_path)) {
1289 /* Path too long; install as a regular file. */
1290 *is_bad_symlink = 1;
1291 got_object_blob_rewind(blob);
1292 return install_blob(worktree, ondisk_path, path,
1293 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1294 restoring_missing_file, reverting_versioned_file,
1295 1, path_is_unversioned, repo, progress_cb,
1299 /* Skip blob object header first time around. */
1300 memcpy(target_path + target_len, buf + hdrlen,
1302 target_len += len - hdrlen;
1306 target_path[target_len] = '\0';
1308 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1309 ondisk_path, worktree->root_path);
1313 if (*is_bad_symlink && !allow_bad_symlinks) {
1314 /* install as a regular file */
1315 got_object_blob_rewind(blob);
1316 err = install_blob(worktree, ondisk_path, path,
1317 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1318 restoring_missing_file, reverting_versioned_file, 1,
1319 path_is_unversioned, repo, progress_cb, progress_arg);
1323 if (symlink(target_path, ondisk_path) == -1) {
1324 if (errno == EEXIST) {
1325 int symlink_replaced;
1326 if (path_is_unversioned) {
1327 err = (*progress_cb)(progress_arg,
1328 GOT_STATUS_UNVERSIONED, path);
1331 err = replace_existing_symlink(&symlink_replaced,
1332 ondisk_path, target_path, target_len);
1336 if (symlink_replaced) {
1337 err = (*progress_cb)(progress_arg,
1338 reverting_versioned_file ?
1340 GOT_STATUS_UPDATE, path);
1342 err = (*progress_cb)(progress_arg,
1343 GOT_STATUS_EXISTS, path);
1346 return err; /* Nothing else to do. */
1349 if (errno == ENOENT) {
1351 err = got_path_dirname(&parent, ondisk_path);
1354 err = add_dir_on_disk(worktree, parent);
1359 * Retry, and fall through to error handling
1360 * below if this second attempt fails.
1362 if (symlink(target_path, ondisk_path) != -1) {
1363 err = NULL; /* success */
1368 /* Handle errors from first or second creation attempt. */
1369 if (errno == ENAMETOOLONG) {
1370 /* bad target path; install as a regular file */
1371 *is_bad_symlink = 1;
1372 got_object_blob_rewind(blob);
1373 err = install_blob(worktree, ondisk_path, path,
1374 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1375 restoring_missing_file, reverting_versioned_file, 1,
1376 path_is_unversioned, repo,
1377 progress_cb, progress_arg);
1378 } else if (errno == ENOTDIR) {
1379 err = got_error_path(ondisk_path,
1380 GOT_ERR_FILE_OBSTRUCTED);
1382 err = got_error_from_errno3("symlink",
1383 target_path, ondisk_path);
1385 } else if (progress_cb)
1386 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1387 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1391 static const struct got_error *
1392 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1393 const char *path, mode_t te_mode, mode_t st_mode,
1394 struct got_blob_object *blob, int restoring_missing_file,
1395 int reverting_versioned_file, int installing_bad_symlink,
1396 int path_is_unversioned, struct got_repository *repo,
1397 got_worktree_checkout_cb progress_cb, void *progress_arg)
1399 const struct got_error *err = NULL;
1403 char *tmppath = NULL;
1406 mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1407 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1410 if (errno == ENOENT) {
1412 err = got_path_dirname(&parent, path);
1415 err = add_dir_on_disk(worktree, parent);
1419 fd = open(ondisk_path,
1420 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1423 return got_error_from_errno2("open",
1425 } else if (errno == EEXIST) {
1426 if (path_is_unversioned) {
1427 err = (*progress_cb)(progress_arg,
1428 GOT_STATUS_UNVERSIONED, path);
1431 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1432 !S_ISREG(st_mode) && !installing_bad_symlink) {
1433 /* TODO file is obstructed; do something */
1434 err = got_error_path(ondisk_path,
1435 GOT_ERR_FILE_OBSTRUCTED);
1438 err = got_opentemp_named_fd(&tmppath, &fd,
1444 if (fchmod(fd, apply_umask(mode)) == -1) {
1445 err = got_error_from_errno2("fchmod",
1451 return got_error_from_errno2("open", ondisk_path);
1455 if (restoring_missing_file)
1456 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1458 else if (reverting_versioned_file)
1459 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1462 err = (*progress_cb)(progress_arg,
1463 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1468 hdrlen = got_object_blob_get_hdrlen(blob);
1470 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1471 err = got_object_blob_read_block(&len, blob);
1475 /* Skip blob object header first time around. */
1476 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1478 err = got_error_from_errno("write");
1480 } else if (outlen != len - hdrlen) {
1481 err = got_error(GOT_ERR_IO);
1488 if (fsync(fd) != 0) {
1489 err = got_error_from_errno("fsync");
1494 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1495 err = got_error_from_errno2("unlink", ondisk_path);
1498 if (rename(tmppath, ondisk_path) != 0) {
1499 err = got_error_from_errno3("rename", tmppath,
1508 if (fd != -1 && close(fd) == -1 && err == NULL)
1509 err = got_error_from_errno("close");
1510 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1511 err = got_error_from_errno2("unlink", tmppath);
1516 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1517 static const struct got_error *
1518 get_modified_file_content_status(unsigned char *status, FILE *f)
1520 const struct got_error *err = NULL;
1521 const char *markers[3] = {
1522 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1523 GOT_DIFF_CONFLICT_MARKER_SEP,
1524 GOT_DIFF_CONFLICT_MARKER_END
1528 size_t linesize = 0;
1531 while (*status == GOT_STATUS_MODIFY) {
1532 linelen = getline(&line, &linesize, f);
1533 if (linelen == -1) {
1536 err = got_ferror(f, GOT_ERR_IO);
1540 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1541 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1543 *status = GOT_STATUS_CONFLICT;
1554 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1556 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1557 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1561 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1563 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1564 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1565 ie->mtime_sec == sb->st_mtim.tv_sec &&
1566 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1567 ie->size == (sb->st_size & 0xffffffff) &&
1568 !xbit_differs(ie, sb->st_mode));
1571 static unsigned char
1572 get_staged_status(struct got_fileindex_entry *ie)
1574 switch (got_fileindex_entry_stage_get(ie)) {
1575 case GOT_FILEIDX_STAGE_ADD:
1576 return GOT_STATUS_ADD;
1577 case GOT_FILEIDX_STAGE_DELETE:
1578 return GOT_STATUS_DELETE;
1579 case GOT_FILEIDX_STAGE_MODIFY:
1580 return GOT_STATUS_MODIFY;
1582 return GOT_STATUS_NO_CHANGE;
1586 static const struct got_error *
1587 get_symlink_modification_status(unsigned char *status,
1588 struct got_fileindex_entry *ie, const char *abspath,
1589 int dirfd, const char *de_name, struct got_blob_object *blob)
1591 const struct got_error *err = NULL;
1592 char target_path[PATH_MAX];
1593 char etarget[PATH_MAX];
1595 size_t len, target_len = 0;
1596 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1597 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1599 *status = GOT_STATUS_NO_CHANGE;
1601 /* Blob object content specifies the target path of the link. */
1603 err = got_object_blob_read_block(&len, blob);
1606 if (len + target_len >= sizeof(target_path)) {
1608 * Should not happen. The blob contents were OK
1609 * when this symlink was installed.
1611 return got_error(GOT_ERR_NO_SPACE);
1614 /* Skip blob object header first time around. */
1615 memcpy(target_path + target_len, buf + hdrlen,
1617 target_len += len - hdrlen;
1621 target_path[target_len] = '\0';
1624 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1626 return got_error_from_errno2("readlinkat", abspath);
1628 elen = readlink(abspath, etarget, sizeof(etarget));
1630 return got_error_from_errno2("readlink", abspath);
1633 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1634 *status = GOT_STATUS_MODIFY;
1639 static const struct got_error *
1640 get_file_status(unsigned char *status, struct stat *sb,
1641 struct got_fileindex_entry *ie, const char *abspath,
1642 int dirfd, const char *de_name, struct got_repository *repo)
1644 const struct got_error *err = NULL;
1645 struct got_object_id id;
1647 int fd = -1, fd1 = -1;
1650 struct got_blob_object *blob = NULL;
1652 unsigned char staged_status;
1654 staged_status = get_staged_status(ie);
1655 *status = GOT_STATUS_NO_CHANGE;
1656 memset(sb, 0, sizeof(*sb));
1659 * Whenever the caller provides a directory descriptor and a
1660 * directory entry name for the file, use them! This prevents
1661 * race conditions if filesystem paths change beneath our feet.
1664 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1665 if (errno == ENOENT) {
1666 if (got_fileindex_entry_has_file_on_disk(ie))
1667 *status = GOT_STATUS_MISSING;
1669 *status = GOT_STATUS_DELETE;
1672 err = got_error_from_errno2("fstatat", abspath);
1676 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1677 if (fd == -1 && errno != ENOENT &&
1678 !got_err_open_nofollow_on_symlink())
1679 return got_error_from_errno2("open", abspath);
1680 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1681 if (lstat(abspath, sb) == -1)
1682 return got_error_from_errno2("lstat", abspath);
1683 } else if (fd == -1 || fstat(fd, sb) == -1) {
1684 if (errno == ENOENT) {
1685 if (got_fileindex_entry_has_file_on_disk(ie))
1686 *status = GOT_STATUS_MISSING;
1688 *status = GOT_STATUS_DELETE;
1691 err = got_error_from_errno2("fstat", abspath);
1696 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1697 *status = GOT_STATUS_OBSTRUCTED;
1701 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1702 *status = GOT_STATUS_DELETE;
1704 } else if (!got_fileindex_entry_has_blob(ie) &&
1705 staged_status != GOT_STATUS_ADD) {
1706 *status = GOT_STATUS_ADD;
1710 if (!stat_info_differs(ie, sb))
1713 if (S_ISLNK(sb->st_mode) &&
1714 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1715 *status = GOT_STATUS_MODIFY;
1719 if (staged_status == GOT_STATUS_MODIFY ||
1720 staged_status == GOT_STATUS_ADD)
1721 got_fileindex_entry_get_staged_blob_id(&id, ie);
1723 got_fileindex_entry_get_blob_id(&id, ie);
1725 fd1 = got_opentempfd();
1727 err = got_error_from_errno("got_opentempfd");
1730 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1734 if (S_ISLNK(sb->st_mode)) {
1735 err = get_symlink_modification_status(status, ie,
1736 abspath, dirfd, de_name, blob);
1741 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1743 err = got_error_from_errno2("openat", abspath);
1748 f = fdopen(fd, "r");
1750 err = got_error_from_errno2("fdopen", abspath);
1754 hdrlen = got_object_blob_get_hdrlen(blob);
1756 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1757 err = got_object_blob_read_block(&blen, blob);
1760 /* Skip length of blob object header first time around. */
1761 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1762 if (flen == 0 && ferror(f)) {
1763 err = got_error_from_errno("fread");
1766 if (blen - hdrlen == 0) {
1768 *status = GOT_STATUS_MODIFY;
1770 } else if (flen == 0) {
1771 if (blen - hdrlen != 0)
1772 *status = GOT_STATUS_MODIFY;
1774 } else if (blen - hdrlen == flen) {
1775 /* Skip blob object header first time around. */
1776 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1777 *status = GOT_STATUS_MODIFY;
1781 *status = GOT_STATUS_MODIFY;
1787 if (*status == GOT_STATUS_MODIFY) {
1789 err = get_modified_file_content_status(status, f);
1790 } else if (xbit_differs(ie, sb->st_mode))
1791 *status = GOT_STATUS_MODE_CHANGE;
1793 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1794 err = got_error_from_errno("close");
1796 got_object_blob_close(blob);
1797 if (f != NULL && fclose(f) == EOF && err == NULL)
1798 err = got_error_from_errno2("fclose", abspath);
1799 if (fd != -1 && close(fd) == -1 && err == NULL)
1800 err = got_error_from_errno2("close", abspath);
1805 * Update timestamps in the file index if a file is unmodified and
1806 * we had to run a full content comparison to find out.
1808 static const struct got_error *
1809 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1810 struct got_fileindex_entry *ie, struct stat *sb)
1812 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1813 return got_fileindex_entry_update(ie, wt_fd, path,
1814 ie->blob_sha1, ie->commit_sha1, 1);
1819 static const struct got_error *
1820 update_blob(struct got_worktree *worktree,
1821 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1822 struct got_tree_entry *te, const char *path,
1823 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1826 const struct got_error *err = NULL;
1827 struct got_blob_object *blob = NULL;
1828 char *ondisk_path = NULL;
1829 unsigned char status = GOT_STATUS_NO_CHANGE;
1831 int fd1 = -1, fd2 = -1;
1833 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1834 return got_error_from_errno("asprintf");
1837 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1838 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1841 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1845 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1846 sb.st_mode = got_fileindex_perms_to_st(ie);
1848 if (stat(ondisk_path, &sb) == -1) {
1849 if (errno != ENOENT) {
1850 err = got_error_from_errno2("stat",
1854 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1855 status = GOT_STATUS_UNVERSIONED;
1857 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1858 status = GOT_STATUS_UNVERSIONED;
1860 status = GOT_STATUS_OBSTRUCTED;
1864 if (status == GOT_STATUS_OBSTRUCTED) {
1866 got_fileindex_entry_mark_skipped(ie);
1867 err = (*progress_cb)(progress_arg, status, path);
1870 if (status == GOT_STATUS_CONFLICT) {
1872 got_fileindex_entry_mark_skipped(ie);
1873 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1878 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1879 (S_ISLNK(te->mode) ||
1880 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1882 * This is a regular file or an installed bad symlink.
1883 * If the file index indicates that this file is already
1884 * up-to-date with respect to the repository we can skip
1885 * updating contents of this file.
1887 if (got_fileindex_entry_has_commit(ie) &&
1888 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1889 SHA1_DIGEST_LENGTH) == 0) {
1891 err = sync_timestamps(worktree->root_fd,
1892 path, status, ie, &sb);
1895 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1899 if (got_fileindex_entry_has_blob(ie) &&
1900 memcmp(ie->blob_sha1, te->id.sha1,
1901 SHA1_DIGEST_LENGTH) == 0) {
1902 /* Different commit but the same blob. */
1903 err = sync_timestamps(worktree->root_fd,
1904 path, status, ie, &sb);
1907 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1913 fd1 = got_opentempfd();
1915 err = got_error_from_errno("got_opentempfd");
1918 err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
1922 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1923 int update_timestamps;
1924 struct got_blob_object *blob2 = NULL;
1925 char *label_orig = NULL;
1926 if (got_fileindex_entry_has_blob(ie)) {
1927 fd2 = got_opentempfd();
1929 err = got_error_from_errno("got_opentempfd");
1932 struct got_object_id id2;
1933 got_fileindex_entry_get_blob_id(&id2, ie);
1934 err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
1939 if (got_fileindex_entry_has_commit(ie)) {
1940 char id_str[SHA1_DIGEST_STRING_LENGTH];
1941 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1942 sizeof(id_str)) == NULL) {
1943 err = got_error_path(id_str,
1944 GOT_ERR_BAD_OBJ_ID_STR);
1947 if (asprintf(&label_orig, "%s: commit %s",
1948 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1949 err = got_error_from_errno("asprintf");
1953 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1955 err = got_object_blob_read_to_str(&link_target, blob);
1958 err = merge_symlink(worktree, blob2, ondisk_path, path,
1959 label_orig, link_target, worktree->base_commit_id,
1960 repo, progress_cb, progress_arg);
1963 err = merge_blob(&update_timestamps, worktree, blob2,
1964 ondisk_path, path, sb.st_mode, label_orig, blob,
1965 worktree->base_commit_id, repo,
1966 progress_cb, progress_arg);
1969 if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
1970 err = got_error_from_errno("close");
1974 got_object_blob_close(blob2);
1978 * Do not update timestamps of files with local changes.
1979 * Otherwise, a future status walk would treat them as
1980 * unmodified files again.
1982 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1983 blob->id.sha1, worktree->base_commit_id->sha1,
1985 } else if (status == GOT_STATUS_MODE_CHANGE) {
1986 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1987 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1988 } else if (status == GOT_STATUS_DELETE) {
1989 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1992 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1993 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1997 int is_bad_symlink = 0;
1998 if (S_ISLNK(te->mode)) {
1999 err = install_symlink(&is_bad_symlink, worktree,
2000 ondisk_path, path, blob,
2001 status == GOT_STATUS_MISSING, 0,
2002 status == GOT_STATUS_UNVERSIONED, 0,
2003 repo, progress_cb, progress_arg);
2005 err = install_blob(worktree, ondisk_path, path,
2006 te->mode, sb.st_mode, blob,
2007 status == GOT_STATUS_MISSING, 0, 0,
2008 status == GOT_STATUS_UNVERSIONED, repo,
2009 progress_cb, progress_arg);
2015 err = got_fileindex_entry_update(ie,
2016 worktree->root_fd, path, blob->id.sha1,
2017 worktree->base_commit_id->sha1, 1);
2019 err = create_fileindex_entry(&ie, fileindex,
2020 worktree->base_commit_id, worktree->root_fd, path,
2026 if (is_bad_symlink) {
2027 got_fileindex_entry_filetype_set(ie,
2028 GOT_FILEIDX_MODE_BAD_SYMLINK);
2032 if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2033 err = got_error_from_errno("close");
2036 got_object_blob_close(blob);
2042 static const struct got_error *
2043 remove_ondisk_file(const char *root_path, const char *path)
2045 const struct got_error *err = NULL;
2046 char *ondisk_path = NULL, *parent = NULL;
2048 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2049 return got_error_from_errno("asprintf");
2051 if (unlink(ondisk_path) == -1) {
2052 if (errno != ENOENT)
2053 err = got_error_from_errno2("unlink", ondisk_path);
2055 size_t root_len = strlen(root_path);
2056 err = got_path_dirname(&parent, ondisk_path);
2059 while (got_path_cmp(parent, root_path,
2060 strlen(parent), root_len) != 0) {
2062 ondisk_path = parent;
2064 if (rmdir(ondisk_path) == -1) {
2065 if (errno != ENOTEMPTY)
2066 err = got_error_from_errno2("rmdir",
2070 err = got_path_dirname(&parent, ondisk_path);
2081 static const struct got_error *
2082 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2083 struct got_fileindex_entry *ie, struct got_repository *repo,
2084 got_worktree_checkout_cb progress_cb, void *progress_arg)
2086 const struct got_error *err = NULL;
2087 unsigned char status;
2091 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2092 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2094 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2096 return got_error_from_errno("asprintf");
2098 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2102 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2103 char ondisk_target[PATH_MAX];
2104 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2105 sizeof(ondisk_target));
2106 if (ondisk_len == -1) {
2107 err = got_error_from_errno2("readlink", ondisk_path);
2110 ondisk_target[ondisk_len] = '\0';
2111 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2112 NULL, NULL, /* XXX pass common ancestor info? */
2113 ondisk_target, ondisk_path);
2116 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2121 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2122 status == GOT_STATUS_ADD) {
2123 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2127 * Preserve the working file and change the deleted blob's
2128 * entry into a schedule-add entry.
2130 err = got_fileindex_entry_update(ie, worktree->root_fd,
2131 ie->path, NULL, NULL, 0);
2133 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2136 if (status == GOT_STATUS_NO_CHANGE) {
2137 err = remove_ondisk_file(worktree->root_path, ie->path);
2141 got_fileindex_entry_remove(fileindex, ie);
2148 struct diff_cb_arg {
2149 struct got_fileindex *fileindex;
2150 struct got_worktree *worktree;
2151 struct got_repository *repo;
2152 got_worktree_checkout_cb progress_cb;
2154 got_cancel_cb cancel_cb;
2158 static const struct got_error *
2159 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2160 struct got_tree_entry *te, const char *parent_path)
2162 struct diff_cb_arg *a = arg;
2164 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2165 return got_error(GOT_ERR_CANCELLED);
2167 return update_blob(a->worktree, a->fileindex, ie, te,
2168 ie->path, a->repo, a->progress_cb, a->progress_arg);
2171 static const struct got_error *
2172 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2174 struct diff_cb_arg *a = arg;
2176 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2177 return got_error(GOT_ERR_CANCELLED);
2179 return delete_blob(a->worktree, a->fileindex, ie,
2180 a->repo, a->progress_cb, a->progress_arg);
2183 static const struct got_error *
2184 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2186 struct diff_cb_arg *a = arg;
2187 const struct got_error *err;
2190 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2191 return got_error(GOT_ERR_CANCELLED);
2193 if (got_object_tree_entry_is_submodule(te))
2196 if (asprintf(&path, "%s%s%s", parent_path,
2197 parent_path[0] ? "/" : "", te->name)
2199 return got_error_from_errno("asprintf");
2201 if (S_ISDIR(te->mode))
2202 err = add_dir_on_disk(a->worktree, path);
2204 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2205 a->repo, a->progress_cb, a->progress_arg);
2211 const struct got_error *
2212 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2214 uint32_t uuid_status;
2216 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2217 if (uuid_status != uuid_s_ok) {
2219 return got_error_uuid(uuid_status, "uuid_to_string");
2225 static const struct got_error *
2226 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2228 const struct got_error *err = NULL;
2229 char *uuidstr = NULL;
2233 err = got_worktree_get_uuid(&uuidstr, worktree);
2237 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2238 err = got_error_from_errno("asprintf");
2245 const struct got_error *
2246 got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2249 return get_ref_name(refname, worktree, prefix);
2252 const struct got_error *
2253 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2255 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2258 static const struct got_error *
2259 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2261 return get_ref_name(refname, worktree,
2262 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2265 static const struct got_error *
2266 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2268 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2271 static const struct got_error *
2272 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2274 return get_ref_name(refname, worktree,
2275 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2278 static const struct got_error *
2279 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2281 return get_ref_name(refname, worktree,
2282 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2285 static const struct got_error *
2286 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2288 return get_ref_name(refname, worktree,
2289 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2292 static const struct got_error *
2293 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2295 return get_ref_name(refname, worktree,
2296 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2299 static const struct got_error *
2300 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2302 return get_ref_name(refname, worktree,
2303 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2306 static const struct got_error *
2307 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2309 return get_ref_name(refname, worktree,
2310 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2313 const struct got_error *
2314 got_worktree_get_histedit_script_path(char **path,
2315 struct got_worktree *worktree)
2317 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2318 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2320 return got_error_from_errno("asprintf");
2325 static const struct got_error *
2326 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2328 return get_ref_name(refname, worktree,
2329 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2332 static const struct got_error *
2333 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2335 return get_ref_name(refname, worktree,
2336 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2340 * Prevent Git's garbage collector from deleting our base commit by
2341 * setting a reference to our base commit's ID.
2343 static const struct got_error *
2344 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2346 const struct got_error *err = NULL;
2347 struct got_reference *ref = NULL;
2350 err = got_worktree_get_base_ref_name(&refname, worktree);
2354 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2358 err = got_ref_write(ref, repo);
2366 static const struct got_error *
2367 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2369 const struct got_error *err = NULL;
2371 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2372 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2373 err = got_error_from_errno("asprintf");
2374 *fileindex_path = NULL;
2380 static const struct got_error *
2381 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2382 struct got_worktree *worktree)
2384 const struct got_error *err = NULL;
2387 *fileindex_path = NULL;
2388 *fileindex = got_fileindex_alloc();
2389 if (*fileindex == NULL)
2390 return got_error_from_errno("got_fileindex_alloc");
2392 err = get_fileindex_path(fileindex_path, worktree);
2396 index = fopen(*fileindex_path, "rbe");
2397 if (index == NULL) {
2398 if (errno != ENOENT)
2399 err = got_error_from_errno2("fopen", *fileindex_path);
2401 err = got_fileindex_read(*fileindex, index);
2402 if (fclose(index) == EOF && err == NULL)
2403 err = got_error_from_errno("fclose");
2407 free(*fileindex_path);
2408 *fileindex_path = NULL;
2409 got_fileindex_free(*fileindex);
2415 struct bump_base_commit_id_arg {
2416 struct got_object_id *base_commit_id;
2419 const char *entry_name;
2420 got_worktree_checkout_cb progress_cb;
2424 static const struct got_error *
2425 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2427 const struct got_error *err;
2428 struct bump_base_commit_id_arg *a = arg;
2430 if (a->entry_name) {
2431 if (strcmp(ie->path, a->path) != 0)
2433 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2436 if (got_fileindex_entry_was_skipped(ie))
2439 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2440 SHA1_DIGEST_LENGTH) == 0)
2443 if (a->progress_cb) {
2444 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2449 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2453 /* Bump base commit ID of all files within an updated part of the work tree. */
2454 static const struct got_error *
2455 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2456 struct got_fileindex *fileindex,
2457 got_worktree_checkout_cb progress_cb, void *progress_arg)
2459 struct bump_base_commit_id_arg bbc_arg;
2461 bbc_arg.base_commit_id = worktree->base_commit_id;
2462 bbc_arg.entry_name = NULL;
2464 bbc_arg.path_len = 0;
2465 bbc_arg.progress_cb = progress_cb;
2466 bbc_arg.progress_arg = progress_arg;
2468 return got_fileindex_for_each_entry_safe(fileindex,
2469 bump_base_commit_id, &bbc_arg);
2472 static const struct got_error *
2473 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2475 const struct got_error *err = NULL;
2476 char *new_fileindex_path = NULL;
2477 FILE *new_index = NULL;
2478 struct timespec timeout;
2480 err = got_opentemp_named(&new_fileindex_path, &new_index,
2481 fileindex_path, "");
2485 err = got_fileindex_write(fileindex, new_index);
2489 if (rename(new_fileindex_path, fileindex_path) != 0) {
2490 err = got_error_from_errno3("rename", new_fileindex_path,
2492 unlink(new_fileindex_path);
2496 * Sleep for a short amount of time to ensure that files modified after
2497 * this program exits have a different time stamp from the one which
2498 * was recorded in the file index.
2501 timeout.tv_nsec = 1;
2502 nanosleep(&timeout, NULL);
2506 free(new_fileindex_path);
2510 static const struct got_error *
2511 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2512 struct got_object_id **tree_id, const char *wt_relpath,
2513 struct got_commit_object *base_commit, struct got_worktree *worktree,
2514 struct got_repository *repo)
2516 const struct got_error *err = NULL;
2517 struct got_object_id *id = NULL;
2518 char *in_repo_path = NULL;
2519 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2521 *entry_type = GOT_OBJ_TYPE_ANY;
2522 *tree_relpath = NULL;
2525 if (wt_relpath[0] == '\0') {
2526 /* Check out all files within the work tree. */
2527 *entry_type = GOT_OBJ_TYPE_TREE;
2528 *tree_relpath = strdup("");
2529 if (*tree_relpath == NULL) {
2530 err = got_error_from_errno("strdup");
2533 err = got_object_id_by_path(tree_id, repo, base_commit,
2534 worktree->path_prefix);
2540 /* Check out a subset of files in the work tree. */
2542 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2543 is_root_wt ? "" : "/", wt_relpath) == -1) {
2544 err = got_error_from_errno("asprintf");
2548 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2553 in_repo_path = NULL;
2555 err = got_object_get_type(entry_type, repo, id);
2559 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2560 /* Check out a single file. */
2561 if (strchr(wt_relpath, '/') == NULL) {
2562 /* Check out a single file in work tree's root dir. */
2563 in_repo_path = strdup(worktree->path_prefix);
2564 if (in_repo_path == NULL) {
2565 err = got_error_from_errno("strdup");
2568 *tree_relpath = strdup("");
2569 if (*tree_relpath == NULL) {
2570 err = got_error_from_errno("strdup");
2574 /* Check out a single file in a subdirectory. */
2575 err = got_path_dirname(tree_relpath, wt_relpath);
2578 if (asprintf(&in_repo_path, "%s%s%s",
2579 worktree->path_prefix, is_root_wt ? "" : "/",
2580 *tree_relpath) == -1) {
2581 err = got_error_from_errno("asprintf");
2585 err = got_object_id_by_path(tree_id, repo,
2586 base_commit, in_repo_path);
2588 /* Check out all files within a subdirectory. */
2589 *tree_id = got_object_id_dup(id);
2590 if (*tree_id == NULL) {
2591 err = got_error_from_errno("got_object_id_dup");
2594 *tree_relpath = strdup(wt_relpath);
2595 if (*tree_relpath == NULL) {
2596 err = got_error_from_errno("strdup");
2604 *entry_type = GOT_OBJ_TYPE_ANY;
2605 free(*tree_relpath);
2606 *tree_relpath = NULL;
2613 static const struct got_error *
2614 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2615 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2616 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2617 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2619 const struct got_error *err = NULL;
2620 struct got_commit_object *commit = NULL;
2621 struct got_tree_object *tree = NULL;
2622 struct got_fileindex_diff_tree_cb diff_cb;
2623 struct diff_cb_arg arg;
2625 err = ref_base_commit(worktree, repo);
2627 if (!(err->code == GOT_ERR_ERRNO &&
2628 (errno == EACCES || errno == EROFS)))
2630 err = (*progress_cb)(progress_arg,
2631 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2636 err = got_object_open_as_commit(&commit, repo,
2637 worktree->base_commit_id);
2641 err = got_object_open_as_tree(&tree, repo, tree_id);
2646 got_object_tree_find_entry(tree, entry_name) == NULL) {
2647 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2651 diff_cb.diff_old_new = diff_old_new;
2652 diff_cb.diff_old = diff_old;
2653 diff_cb.diff_new = diff_new;
2654 arg.fileindex = fileindex;
2655 arg.worktree = worktree;
2657 arg.progress_cb = progress_cb;
2658 arg.progress_arg = progress_arg;
2659 arg.cancel_cb = cancel_cb;
2660 arg.cancel_arg = cancel_arg;
2661 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2662 entry_name, repo, &diff_cb, &arg);
2665 got_object_tree_close(tree);
2667 got_object_commit_close(commit);
2671 const struct got_error *
2672 got_worktree_checkout_files(struct got_worktree *worktree,
2673 struct got_pathlist_head *paths, struct got_repository *repo,
2674 got_worktree_checkout_cb progress_cb, void *progress_arg,
2675 got_cancel_cb cancel_cb, void *cancel_arg)
2677 const struct got_error *err = NULL, *sync_err, *unlockerr;
2678 struct got_commit_object *commit = NULL;
2679 struct got_tree_object *tree = NULL;
2680 struct got_fileindex *fileindex = NULL;
2681 char *fileindex_path = NULL;
2682 struct got_pathlist_entry *pe;
2683 struct tree_path_data {
2684 STAILQ_ENTRY(tree_path_data) entry;
2685 struct got_object_id *tree_id;
2690 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2692 STAILQ_INIT(&tree_paths);
2694 err = lock_worktree(worktree, LOCK_EX);
2698 err = got_object_open_as_commit(&commit, repo,
2699 worktree->base_commit_id);
2703 /* Map all specified paths to in-repository trees. */
2704 TAILQ_FOREACH(pe, paths, entry) {
2705 tpd = malloc(sizeof(*tpd));
2707 err = got_error_from_errno("malloc");
2711 err = find_tree_entry_for_checkout(&tpd->entry_type,
2712 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2719 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2720 err = got_path_basename(&tpd->entry_name, pe->path);
2728 tpd->entry_name = NULL;
2730 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2734 * Read the file index.
2735 * Checking out files is supposed to be an idempotent operation.
2736 * If the on-disk file index is incomplete we will try to complete it.
2738 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2742 tpd = STAILQ_FIRST(&tree_paths);
2743 TAILQ_FOREACH(pe, paths, entry) {
2744 struct bump_base_commit_id_arg bbc_arg;
2746 err = checkout_files(worktree, fileindex, tpd->relpath,
2747 tpd->tree_id, tpd->entry_name, repo,
2748 progress_cb, progress_arg, cancel_cb, cancel_arg);
2752 bbc_arg.base_commit_id = worktree->base_commit_id;
2753 bbc_arg.entry_name = tpd->entry_name;
2754 bbc_arg.path = pe->path;
2755 bbc_arg.path_len = pe->path_len;
2756 bbc_arg.progress_cb = progress_cb;
2757 bbc_arg.progress_arg = progress_arg;
2758 err = got_fileindex_for_each_entry_safe(fileindex,
2759 bump_base_commit_id, &bbc_arg);
2763 tpd = STAILQ_NEXT(tpd, entry);
2765 sync_err = sync_fileindex(fileindex, fileindex_path);
2766 if (sync_err && err == NULL)
2769 free(fileindex_path);
2771 got_object_tree_close(tree);
2773 got_object_commit_close(commit);
2775 got_fileindex_free(fileindex);
2776 while (!STAILQ_EMPTY(&tree_paths)) {
2777 tpd = STAILQ_FIRST(&tree_paths);
2778 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2783 unlockerr = lock_worktree(worktree, LOCK_SH);
2784 if (unlockerr && err == NULL)
2789 struct merge_file_cb_arg {
2790 struct got_worktree *worktree;
2791 struct got_fileindex *fileindex;
2792 got_worktree_checkout_cb progress_cb;
2794 got_cancel_cb cancel_cb;
2796 const char *label_orig;
2797 struct got_object_id *commit_id2;
2798 int allow_bad_symlinks;
2801 static const struct got_error *
2802 merge_file_cb(void *arg, struct got_blob_object *blob1,
2803 struct got_blob_object *blob2, FILE *f1, FILE *f2,
2804 struct got_object_id *id1, struct got_object_id *id2,
2805 const char *path1, const char *path2,
2806 mode_t mode1, mode_t mode2, struct got_repository *repo)
2808 static const struct got_error *err = NULL;
2809 struct merge_file_cb_arg *a = arg;
2810 struct got_fileindex_entry *ie;
2811 char *ondisk_path = NULL;
2813 unsigned char status;
2814 int local_changes_subsumed;
2815 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2816 char *id_str = NULL, *label_deriv2 = NULL;
2818 if (blob1 && blob2) {
2819 ie = got_fileindex_entry_get(a->fileindex, path2,
2822 return (*a->progress_cb)(a->progress_arg,
2823 GOT_STATUS_MISSING, path2);
2825 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2827 return got_error_from_errno("asprintf");
2829 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2834 if (status == GOT_STATUS_DELETE) {
2835 err = (*a->progress_cb)(a->progress_arg,
2836 GOT_STATUS_MERGE, path2);
2839 if (status != GOT_STATUS_NO_CHANGE &&
2840 status != GOT_STATUS_MODIFY &&
2841 status != GOT_STATUS_CONFLICT &&
2842 status != GOT_STATUS_ADD) {
2843 err = (*a->progress_cb)(a->progress_arg, status, path2);
2847 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2849 err = got_object_blob_read_to_str(&link_target2, blob2);
2852 err = merge_symlink(a->worktree, blob1, ondisk_path,
2853 path2, a->label_orig, link_target2, a->commit_id2,
2854 repo, a->progress_cb, a->progress_arg);
2859 f_orig = got_opentemp();
2860 if (f_orig == NULL) {
2861 err = got_error_from_errno("got_opentemp");
2864 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2869 f_deriv2 = got_opentemp();
2870 if (f_deriv2 == NULL)
2872 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2877 fd = open(ondisk_path,
2878 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2880 err = got_error_from_errno2("open",
2884 f_deriv = fdopen(fd, "r");
2885 if (f_deriv == NULL) {
2886 err = got_error_from_errno2("fdopen",
2891 err = got_object_id_str(&id_str, a->commit_id2);
2894 if (asprintf(&label_deriv2, "%s: commit %s",
2895 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2896 err = got_error_from_errno("asprintf");
2899 err = merge_file(&local_changes_subsumed, a->worktree,
2900 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2901 mode2, a->label_orig, NULL, label_deriv2,
2902 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2903 a->progress_cb, a->progress_arg);
2906 ie = got_fileindex_entry_get(a->fileindex, path1,
2909 return (*a->progress_cb)(a->progress_arg,
2910 GOT_STATUS_MISSING, path1);
2912 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2914 return got_error_from_errno("asprintf");
2916 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2922 case GOT_STATUS_NO_CHANGE:
2923 err = (*a->progress_cb)(a->progress_arg,
2924 GOT_STATUS_DELETE, path1);
2927 err = remove_ondisk_file(a->worktree->root_path, path1);
2931 got_fileindex_entry_mark_deleted_from_disk(ie);
2933 case GOT_STATUS_DELETE:
2934 case GOT_STATUS_MISSING:
2935 err = (*a->progress_cb)(a->progress_arg,
2936 GOT_STATUS_DELETE, path1);
2940 got_fileindex_entry_mark_deleted_from_disk(ie);
2942 case GOT_STATUS_ADD: {
2943 struct got_object_id *id;
2947 * Delete the added file only if its content already
2948 * exists in the repository.
2950 err = got_object_blob_file_create(&id, &blob1_f,
2951 &blob1_size, path1);
2954 if (got_object_id_cmp(id, id1) == 0) {
2955 err = (*a->progress_cb)(a->progress_arg,
2956 GOT_STATUS_DELETE, path1);
2959 err = remove_ondisk_file(a->worktree->root_path,
2964 got_fileindex_entry_remove(a->fileindex,
2967 err = (*a->progress_cb)(a->progress_arg,
2968 GOT_STATUS_CANNOT_DELETE, path1);
2970 if (fclose(blob1_f) == EOF && err == NULL)
2971 err = got_error_from_errno("fclose");
2977 case GOT_STATUS_MODIFY:
2978 case GOT_STATUS_CONFLICT:
2979 err = (*a->progress_cb)(a->progress_arg,
2980 GOT_STATUS_CANNOT_DELETE, path1);
2984 case GOT_STATUS_OBSTRUCTED:
2985 err = (*a->progress_cb)(a->progress_arg, status, path1);
2993 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2995 return got_error_from_errno("asprintf");
2996 ie = got_fileindex_entry_get(a->fileindex, path2,
2999 err = get_file_status(&status, &sb, ie, ondisk_path,
3003 if (status != GOT_STATUS_NO_CHANGE &&
3004 status != GOT_STATUS_MODIFY &&
3005 status != GOT_STATUS_CONFLICT &&
3006 status != GOT_STATUS_ADD) {
3007 err = (*a->progress_cb)(a->progress_arg,
3011 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3013 err = got_object_blob_read_to_str(&link_target2,
3017 err = merge_symlink(a->worktree, NULL,
3018 ondisk_path, path2, a->label_orig,
3019 link_target2, a->commit_id2, repo,
3020 a->progress_cb, a->progress_arg);
3022 } else if (S_ISREG(sb.st_mode)) {
3023 err = merge_blob(&local_changes_subsumed,
3024 a->worktree, NULL, ondisk_path, path2,
3025 sb.st_mode, a->label_orig, blob2,
3026 a->commit_id2, repo, a->progress_cb,
3029 err = got_error_path(ondisk_path,
3030 GOT_ERR_FILE_OBSTRUCTED);
3034 if (status == GOT_STATUS_DELETE) {
3035 err = got_fileindex_entry_update(ie,
3036 a->worktree->root_fd, path2, blob2->id.sha1,
3037 a->worktree->base_commit_id->sha1, 0);
3042 int is_bad_symlink = 0;
3043 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3044 if (S_ISLNK(mode2)) {
3045 err = install_symlink(&is_bad_symlink,
3046 a->worktree, ondisk_path, path2, blob2, 0,
3047 0, 1, a->allow_bad_symlinks, repo,
3048 a->progress_cb, a->progress_arg);
3050 err = install_blob(a->worktree, ondisk_path, path2,
3051 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3052 a->progress_cb, a->progress_arg);
3056 err = got_fileindex_entry_alloc(&ie, path2);
3059 err = got_fileindex_entry_update(ie,
3060 a->worktree->root_fd, path2, NULL, NULL, 1);
3062 got_fileindex_entry_free(ie);
3065 err = got_fileindex_entry_add(a->fileindex, ie);
3067 got_fileindex_entry_free(ie);
3070 if (is_bad_symlink) {
3071 got_fileindex_entry_filetype_set(ie,
3072 GOT_FILEIDX_MODE_BAD_SYMLINK);
3077 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3078 err = got_error_from_errno("fclose");
3079 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3080 err = got_error_from_errno("fclose");
3081 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3082 err = got_error_from_errno("fclose");
3089 static const struct got_error *
3090 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3092 struct got_worktree *worktree = arg;
3094 /* Reject merges into a work tree with mixed base commits. */
3095 if (got_fileindex_entry_has_commit(ie) &&
3096 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3097 SHA1_DIGEST_LENGTH) != 0)
3098 return got_error(GOT_ERR_MIXED_COMMITS);
3103 struct check_merge_conflicts_arg {
3104 struct got_worktree *worktree;
3105 struct got_fileindex *fileindex;
3106 struct got_repository *repo;
3109 static const struct got_error *
3110 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3111 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3112 struct got_object_id *id1, struct got_object_id *id2,
3113 const char *path1, const char *path2,
3114 mode_t mode1, mode_t mode2, struct got_repository *repo)
3116 const struct got_error *err = NULL;
3117 struct check_merge_conflicts_arg *a = arg;
3118 unsigned char status;
3120 struct got_fileindex_entry *ie;
3121 const char *path = path2 ? path2 : path1;
3122 struct got_object_id *id = id2 ? id2 : id1;
3128 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3132 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3134 return got_error_from_errno("asprintf");
3136 /* Reject merges into a work tree with conflicted files. */
3137 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3141 if (status == GOT_STATUS_CONFLICT)
3142 return got_error(GOT_ERR_CONFLICTS);
3147 static const struct got_error *
3148 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3149 const char *fileindex_path, struct got_object_id *commit_id1,
3150 struct got_object_id *commit_id2, struct got_repository *repo,
3151 got_worktree_checkout_cb progress_cb, void *progress_arg,
3152 got_cancel_cb cancel_cb, void *cancel_arg)
3154 const struct got_error *err = NULL, *sync_err;
3155 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3156 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3157 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3158 struct check_merge_conflicts_arg cmc_arg;
3159 struct merge_file_cb_arg arg;
3160 char *label_orig = NULL;
3161 FILE *f1 = NULL, *f2 = NULL;
3162 int fd1 = -1, fd2 = -1;
3165 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3168 err = got_object_id_by_path(&tree_id1, repo, commit1,
3169 worktree->path_prefix);
3170 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3176 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3180 err = got_object_id_str(&id_str, commit_id1);
3184 if (asprintf(&label_orig, "%s: commit %s",
3185 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3186 err = got_error_from_errno("asprintf");
3192 f1 = got_opentemp();
3194 err = got_error_from_errno("got_opentemp");
3199 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3203 err = got_object_id_by_path(&tree_id2, repo, commit2,
3204 worktree->path_prefix);
3208 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3212 f2 = got_opentemp();
3214 err = got_error_from_errno("got_opentemp");
3218 fd1 = got_opentempfd();
3220 err = got_error_from_errno("got_opentempfd");
3224 fd2 = got_opentempfd();
3226 err = got_error_from_errno("got_opentempfd");
3230 cmc_arg.worktree = worktree;
3231 cmc_arg.fileindex = fileindex;
3232 cmc_arg.repo = repo;
3233 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3234 check_merge_conflicts, &cmc_arg, 0);
3238 arg.worktree = worktree;
3239 arg.fileindex = fileindex;
3240 arg.progress_cb = progress_cb;
3241 arg.progress_arg = progress_arg;
3242 arg.cancel_cb = cancel_cb;
3243 arg.cancel_arg = cancel_arg;
3244 arg.label_orig = label_orig;
3245 arg.commit_id2 = commit_id2;
3246 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3247 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3248 merge_file_cb, &arg, 1);
3249 sync_err = sync_fileindex(fileindex, fileindex_path);
3250 if (sync_err && err == NULL)
3254 got_object_commit_close(commit1);
3256 got_object_commit_close(commit2);
3258 got_object_tree_close(tree1);
3260 got_object_tree_close(tree2);
3261 if (f1 && fclose(f1) == EOF && err == NULL)
3262 err = got_error_from_errno("fclose");
3263 if (f2 && fclose(f2) == EOF && err == NULL)
3264 err = got_error_from_errno("fclose");
3265 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3266 err = got_error_from_errno("close");
3267 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3268 err = got_error_from_errno("close");
3273 const struct got_error *
3274 got_worktree_merge_files(struct got_worktree *worktree,
3275 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3276 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3277 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3279 const struct got_error *err, *unlockerr;
3280 char *fileindex_path = NULL;
3281 struct got_fileindex *fileindex = NULL;
3283 err = lock_worktree(worktree, LOCK_EX);
3287 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3291 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3296 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3297 commit_id2, repo, progress_cb, progress_arg,
3298 cancel_cb, cancel_arg);
3301 got_fileindex_free(fileindex);
3302 free(fileindex_path);
3303 unlockerr = lock_worktree(worktree, LOCK_SH);
3304 if (unlockerr && err == NULL)
3309 struct diff_dir_cb_arg {
3310 struct got_fileindex *fileindex;
3311 struct got_worktree *worktree;
3312 const char *status_path;
3313 size_t status_path_len;
3314 struct got_repository *repo;
3315 got_worktree_status_cb status_cb;
3317 got_cancel_cb cancel_cb;
3319 /* A pathlist containing per-directory pathlists of ignore patterns. */
3320 struct got_pathlist_head *ignores;
3321 int report_unchanged;
3325 static const struct got_error *
3326 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3327 int dirfd, const char *de_name,
3328 got_worktree_status_cb status_cb, void *status_arg,
3329 struct got_repository *repo, int report_unchanged)
3331 const struct got_error *err = NULL;
3332 unsigned char status = GOT_STATUS_NO_CHANGE;
3333 unsigned char staged_status;
3335 struct got_object_id blob_id, commit_id, staged_blob_id;
3336 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3337 struct got_object_id *staged_blob_idp = NULL;
3339 staged_status = get_staged_status(ie);
3340 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3344 if (status == GOT_STATUS_NO_CHANGE &&
3345 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3348 if (got_fileindex_entry_has_blob(ie))
3349 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3350 if (got_fileindex_entry_has_commit(ie))
3351 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3352 if (staged_status == GOT_STATUS_ADD ||
3353 staged_status == GOT_STATUS_MODIFY) {
3354 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3355 &staged_blob_id, ie);
3358 return (*status_cb)(status_arg, status, staged_status,
3359 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3362 static const struct got_error *
3363 status_old_new(void *arg, struct got_fileindex_entry *ie,
3364 struct dirent *de, const char *parent_path, int dirfd)
3366 const struct got_error *err = NULL;
3367 struct diff_dir_cb_arg *a = arg;
3370 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3371 return got_error(GOT_ERR_CANCELLED);
3373 if (got_path_cmp(parent_path, a->status_path,
3374 strlen(parent_path), a->status_path_len) != 0 &&
3375 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3378 if (parent_path[0]) {
3379 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3380 parent_path, de->d_name) == -1)
3381 return got_error_from_errno("asprintf");
3383 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3385 return got_error_from_errno("asprintf");
3388 err = report_file_status(ie, abspath, dirfd, de->d_name,
3389 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3394 static const struct got_error *
3395 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3397 struct diff_dir_cb_arg *a = arg;
3398 struct got_object_id blob_id, commit_id;
3399 unsigned char status;
3401 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3402 return got_error(GOT_ERR_CANCELLED);
3404 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3407 got_fileindex_entry_get_blob_id(&blob_id, ie);
3408 got_fileindex_entry_get_commit_id(&commit_id, ie);
3409 if (got_fileindex_entry_has_file_on_disk(ie))
3410 status = GOT_STATUS_MISSING;
3412 status = GOT_STATUS_DELETE;
3413 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3414 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3418 free_ignores(struct got_pathlist_head *ignores)
3420 struct got_pathlist_entry *pe;
3422 TAILQ_FOREACH(pe, ignores, entry) {
3423 struct got_pathlist_head *ignorelist = pe->data;
3425 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3427 got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3430 static const struct got_error *
3431 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3433 const struct got_error *err = NULL;
3434 struct got_pathlist_entry *pe = NULL;
3435 struct got_pathlist_head *ignorelist;
3436 char *line = NULL, *pattern, *dirpath = NULL;
3437 size_t linesize = 0;
3440 ignorelist = calloc(1, sizeof(*ignorelist));
3441 if (ignorelist == NULL)
3442 return got_error_from_errno("calloc");
3443 TAILQ_INIT(ignorelist);
3445 while ((linelen = getline(&line, &linesize, f)) != -1) {
3446 if (linelen > 0 && line[linelen - 1] == '\n')
3447 line[linelen - 1] = '\0';
3449 /* Git's ignores may contain comments. */
3453 /* Git's negated patterns are not (yet?) supported. */
3457 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3459 err = got_error_from_errno("asprintf");
3462 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3467 err = got_error_from_errno("getline");
3471 dirpath = strdup(path);
3472 if (dirpath == NULL) {
3473 err = got_error_from_errno("strdup");
3476 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3479 if (err || pe == NULL) {
3481 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3487 match_ignores(struct got_pathlist_head *ignores, const char *path)
3489 struct got_pathlist_entry *pe;
3491 /* Handle patterns which match in all directories. */
3492 TAILQ_FOREACH(pe, ignores, entry) {
3493 struct got_pathlist_head *ignorelist = pe->data;
3494 struct got_pathlist_entry *pi;
3496 TAILQ_FOREACH(pi, ignorelist, entry) {
3497 const char *p, *pattern = pi->path;
3499 if (strncmp(pattern, "**/", 3) != 0)
3504 if (fnmatch(pattern, p,
3505 FNM_PATHNAME | FNM_LEADING_DIR)) {
3506 /* Retry in next directory. */
3507 while (*p && *p != '/')
3519 * The ignores pathlist contains ignore lists from children before
3520 * parents, so we can find the most specific ignorelist by walking
3521 * ignores backwards.
3523 pe = TAILQ_LAST(ignores, got_pathlist_head);
3525 if (got_path_is_child(path, pe->path, pe->path_len)) {
3526 struct got_pathlist_head *ignorelist = pe->data;
3527 struct got_pathlist_entry *pi;
3528 TAILQ_FOREACH(pi, ignorelist, entry) {
3529 const char *pattern = pi->path;
3530 int flags = FNM_LEADING_DIR;
3531 if (strstr(pattern, "/**/") == NULL)
3532 flags |= FNM_PATHNAME;
3533 if (fnmatch(pattern, path, flags))
3538 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3544 static const struct got_error *
3545 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3546 const char *path, int dirfd, const char *ignores_filename)
3548 const struct got_error *err = NULL;
3551 FILE *ignoresfile = NULL;
3553 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3554 path[0] ? "/" : "", ignores_filename) == -1)
3555 return got_error_from_errno("asprintf");
3558 fd = openat(dirfd, ignores_filename,
3559 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3561 if (errno != ENOENT && errno != EACCES)
3562 err = got_error_from_errno2("openat",
3565 ignoresfile = fdopen(fd, "r");
3566 if (ignoresfile == NULL)
3567 err = got_error_from_errno2("fdopen",
3571 err = read_ignores(ignores, path, ignoresfile);
3575 ignoresfile = fopen(ignorespath, "re");
3576 if (ignoresfile == NULL) {
3577 if (errno != ENOENT && errno != EACCES)
3578 err = got_error_from_errno2("fopen",
3581 err = read_ignores(ignores, path, ignoresfile);
3584 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3585 err = got_error_from_errno2("fclose", path);
3586 if (fd != -1 && close(fd) == -1 && err == NULL)
3587 err = got_error_from_errno2("close", path);
3592 static const struct got_error *
3593 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3596 const struct got_error *err = NULL;
3597 struct diff_dir_cb_arg *a = arg;
3603 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3604 return got_error(GOT_ERR_CANCELLED);
3606 if (parent_path[0]) {
3607 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3608 return got_error_from_errno("asprintf");
3613 if (de->d_type == DT_DIR) {
3614 if (!a->no_ignores && ignore != NULL &&
3615 match_ignores(a->ignores, path))
3617 } else if (!match_ignores(a->ignores, path) &&
3618 got_path_is_child(path, a->status_path, a->status_path_len))
3619 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3620 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3626 static const struct got_error *
3627 status_traverse(void *arg, const char *path, int dirfd)
3629 const struct got_error *err = NULL;
3630 struct diff_dir_cb_arg *a = arg;
3635 err = add_ignores(a->ignores, a->worktree->root_path,
3636 path, dirfd, ".cvsignore");
3640 err = add_ignores(a->ignores, a->worktree->root_path, path,
3641 dirfd, ".gitignore");
3646 static const struct got_error *
3647 report_single_file_status(const char *path, const char *ondisk_path,
3648 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3649 void *status_arg, struct got_repository *repo, int report_unchanged,
3650 struct got_pathlist_head *ignores, int no_ignores)
3652 struct got_fileindex_entry *ie;
3655 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3657 return report_file_status(ie, ondisk_path, -1, NULL,
3658 status_cb, status_arg, repo, report_unchanged);
3660 if (lstat(ondisk_path, &sb) == -1) {
3661 if (errno != ENOENT)
3662 return got_error_from_errno2("lstat", ondisk_path);
3663 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3664 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3667 if (!no_ignores && match_ignores(ignores, path))
3670 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3671 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3672 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3677 static const struct got_error *
3678 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3679 const char *root_path, const char *path)
3681 const struct got_error *err;
3682 char *parent_path, *next_parent_path = NULL;
3684 err = add_ignores(ignores, root_path, "", -1,
3689 err = add_ignores(ignores, root_path, "", -1,
3694 err = got_path_dirname(&parent_path, path);
3696 if (err->code == GOT_ERR_BAD_PATH)
3697 return NULL; /* cannot traverse parent */
3701 err = add_ignores(ignores, root_path, parent_path, -1,
3705 err = add_ignores(ignores, root_path, parent_path, -1,
3709 err = got_path_dirname(&next_parent_path, parent_path);
3711 if (err->code == GOT_ERR_BAD_PATH)
3712 err = NULL; /* traversed everything */
3715 if (got_path_is_root_dir(parent_path))
3718 parent_path = next_parent_path;
3719 next_parent_path = NULL;
3723 free(next_parent_path);
3727 static const struct got_error *
3728 worktree_status(struct got_worktree *worktree, const char *path,
3729 struct got_fileindex *fileindex, struct got_repository *repo,
3730 got_worktree_status_cb status_cb, void *status_arg,
3731 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3732 int report_unchanged)
3734 const struct got_error *err = NULL;
3736 struct got_fileindex_diff_dir_cb fdiff_cb;
3737 struct diff_dir_cb_arg arg;
3738 char *ondisk_path = NULL;
3739 struct got_pathlist_head ignores;
3740 struct got_fileindex_entry *ie;
3742 TAILQ_INIT(&ignores);
3744 if (asprintf(&ondisk_path, "%s%s%s",
3745 worktree->root_path, path[0] ? "/" : "", path) == -1)
3746 return got_error_from_errno("asprintf");
3748 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3750 err = report_single_file_status(path, ondisk_path,
3751 fileindex, status_cb, status_arg, repo,
3752 report_unchanged, &ignores, no_ignores);
3756 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3758 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3759 !got_err_open_nofollow_on_symlink())
3760 err = got_error_from_errno2("open", ondisk_path);
3763 err = add_ignores_from_parent_paths(&ignores,
3764 worktree->root_path, ondisk_path);
3768 err = report_single_file_status(path, ondisk_path,
3769 fileindex, status_cb, status_arg, repo,
3770 report_unchanged, &ignores, no_ignores);
3773 fdiff_cb.diff_old_new = status_old_new;
3774 fdiff_cb.diff_old = status_old;
3775 fdiff_cb.diff_new = status_new;
3776 fdiff_cb.diff_traverse = status_traverse;
3777 arg.fileindex = fileindex;
3778 arg.worktree = worktree;
3779 arg.status_path = path;
3780 arg.status_path_len = strlen(path);
3782 arg.status_cb = status_cb;
3783 arg.status_arg = status_arg;
3784 arg.cancel_cb = cancel_cb;
3785 arg.cancel_arg = cancel_arg;
3786 arg.report_unchanged = report_unchanged;
3787 arg.no_ignores = no_ignores;
3789 err = add_ignores_from_parent_paths(&ignores,
3790 worktree->root_path, path);
3794 arg.ignores = &ignores;
3795 err = got_fileindex_diff_dir(fileindex, fd,
3796 worktree->root_path, path, repo, &fdiff_cb, &arg);
3799 free_ignores(&ignores);
3800 if (fd != -1 && close(fd) == -1 && err == NULL)
3801 err = got_error_from_errno("close");
3806 const struct got_error *
3807 got_worktree_status(struct got_worktree *worktree,
3808 struct got_pathlist_head *paths, struct got_repository *repo,
3809 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3810 got_cancel_cb cancel_cb, void *cancel_arg)
3812 const struct got_error *err = NULL;
3813 char *fileindex_path = NULL;
3814 struct got_fileindex *fileindex = NULL;
3815 struct got_pathlist_entry *pe;
3817 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3821 TAILQ_FOREACH(pe, paths, entry) {
3822 err = worktree_status(worktree, pe->path, fileindex, repo,
3823 status_cb, status_arg, cancel_cb, cancel_arg,
3828 free(fileindex_path);
3829 got_fileindex_free(fileindex);
3833 const struct got_error *
3834 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3837 const struct got_error *err = NULL;
3838 char *resolved = NULL, *cwd = NULL, *path = NULL;
3841 char *abspath = NULL;
3842 char canonpath[PATH_MAX];
3846 cwd = getcwd(NULL, 0);
3848 return got_error_from_errno("getcwd");
3850 if (lstat(arg, &sb) == -1) {
3851 if (errno != ENOENT) {
3852 err = got_error_from_errno2("lstat", arg);
3857 if (S_ISLNK(sb.st_mode)) {
3859 * We cannot use realpath(3) with symlinks since we want to
3860 * operate on the symlink itself.
3861 * But we can make the path absolute, assuming it is relative
3862 * to the current working directory, and then canonicalize it.
3864 if (!got_path_is_absolute(arg)) {
3865 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3866 err = got_error_from_errno("asprintf");
3871 err = got_canonpath(abspath ? abspath : arg, canonpath,
3875 resolved = strdup(canonpath);
3876 if (resolved == NULL) {
3877 err = got_error_from_errno("strdup");
3881 resolved = realpath(arg, NULL);
3882 if (resolved == NULL) {
3883 if (errno != ENOENT) {
3884 err = got_error_from_errno2("realpath", arg);
3887 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3888 err = got_error_from_errno("asprintf");
3891 err = got_canonpath(abspath, canonpath,
3895 resolved = strdup(canonpath);
3896 if (resolved == NULL) {
3897 err = got_error_from_errno("strdup");
3903 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3904 strlen(got_worktree_get_root_path(worktree)))) {
3905 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3909 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3910 err = got_path_skip_common_ancestor(&path,
3911 got_worktree_get_root_path(worktree), resolved);
3917 err = got_error_from_errno("strdup");
3922 /* XXX status walk can't deal with trailing slash! */
3924 while (len > 0 && path[len - 1] == '/') {
3925 path[len - 1] = '\0';
3939 struct schedule_addition_args {
3940 struct got_worktree *worktree;
3941 struct got_fileindex *fileindex;
3942 got_worktree_checkout_cb progress_cb;
3944 struct got_repository *repo;
3947 static const struct got_error *
3948 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3949 const char *relpath, struct got_object_id *blob_id,
3950 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3951 int dirfd, const char *de_name)
3953 struct schedule_addition_args *a = arg;
3954 const struct got_error *err = NULL;
3955 struct got_fileindex_entry *ie;
3959 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3961 return got_error_from_errno("asprintf");
3963 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3965 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3969 /* Re-adding an existing entry is a no-op. */
3970 if (status == GOT_STATUS_ADD)
3972 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3977 if (status != GOT_STATUS_UNVERSIONED) {
3978 if (status == GOT_STATUS_NONEXISTENT)
3979 err = got_error_set_errno(ENOENT, ondisk_path);
3981 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3985 err = got_fileindex_entry_alloc(&ie, relpath);
3988 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3989 relpath, NULL, NULL, 1);
3991 got_fileindex_entry_free(ie);
3994 err = got_fileindex_entry_add(a->fileindex, ie);
3996 got_fileindex_entry_free(ie);
4003 if (status == GOT_STATUS_ADD)
4005 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4008 const struct got_error *
4009 got_worktree_schedule_add(struct got_worktree *worktree,
4010 struct got_pathlist_head *paths,
4011 got_worktree_checkout_cb progress_cb, void *progress_arg,
4012 struct got_repository *repo, int no_ignores)
4014 struct got_fileindex *fileindex = NULL;
4015 char *fileindex_path = NULL;
4016 const struct got_error *err = NULL, *sync_err, *unlockerr;
4017 struct got_pathlist_entry *pe;
4018 struct schedule_addition_args saa;
4020 err = lock_worktree(worktree, LOCK_EX);
4024 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4028 saa.worktree = worktree;
4029 saa.fileindex = fileindex;
4030 saa.progress_cb = progress_cb;
4031 saa.progress_arg = progress_arg;
4034 TAILQ_FOREACH(pe, paths, entry) {
4035 err = worktree_status(worktree, pe->path, fileindex, repo,
4036 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4040 sync_err = sync_fileindex(fileindex, fileindex_path);
4041 if (sync_err && err == NULL)
4044 free(fileindex_path);
4046 got_fileindex_free(fileindex);
4047 unlockerr = lock_worktree(worktree, LOCK_SH);
4048 if (unlockerr && err == NULL)
4053 struct schedule_deletion_args {
4054 struct got_worktree *worktree;
4055 struct got_fileindex *fileindex;
4056 got_worktree_delete_cb progress_cb;
4058 struct got_repository *repo;
4059 int delete_local_mods;
4061 int ignore_missing_paths;
4062 const char *status_codes;
4065 static const struct got_error *
4066 schedule_for_deletion(void *arg, unsigned char status,
4067 unsigned char staged_status, const char *relpath,
4068 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4069 struct got_object_id *commit_id, int dirfd, const char *de_name)
4071 struct schedule_deletion_args *a = arg;
4072 const struct got_error *err = NULL;
4073 struct got_fileindex_entry *ie = NULL;
4077 if (status == GOT_STATUS_NONEXISTENT) {
4078 if (a->ignore_missing_paths)
4080 return got_error_set_errno(ENOENT, relpath);
4083 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4085 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4087 staged_status = get_staged_status(ie);
4088 if (staged_status != GOT_STATUS_NO_CHANGE) {
4089 if (staged_status == GOT_STATUS_DELETE)
4091 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4094 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4096 return got_error_from_errno("asprintf");
4098 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4103 if (a->status_codes) {
4104 size_t ncodes = strlen(a->status_codes);
4106 for (i = 0; i < ncodes ; i++) {
4107 if (status == a->status_codes[i])
4111 /* Do not delete files in non-matching status. */
4115 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4116 a->status_codes[i] != GOT_STATUS_MISSING) {
4117 static char msg[64];
4118 snprintf(msg, sizeof(msg),
4119 "invalid status code '%c'", a->status_codes[i]);
4120 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4125 if (status != GOT_STATUS_NO_CHANGE) {
4126 if (status == GOT_STATUS_DELETE)
4128 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4129 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4132 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4133 err = got_error_set_errno(ENOENT, relpath);
4136 if (status != GOT_STATUS_MODIFY &&
4137 status != GOT_STATUS_MISSING) {
4138 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4143 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4147 if (unlinkat(dirfd, de_name, 0) == -1) {
4148 err = got_error_from_errno2("unlinkat",
4152 } else if (unlink(ondisk_path) == -1) {
4153 err = got_error_from_errno2("unlink", ondisk_path);
4157 root_len = strlen(a->worktree->root_path);
4160 err = got_path_dirname(&parent, ondisk_path);
4164 ondisk_path = parent;
4165 if (rmdir(ondisk_path) == -1) {
4166 if (errno != ENOTEMPTY)
4167 err = got_error_from_errno2("rmdir",
4171 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4172 strlen(ondisk_path), root_len) != 0);
4175 got_fileindex_entry_mark_deleted_from_disk(ie);
4180 if (status == GOT_STATUS_DELETE)
4182 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4183 staged_status, relpath);
4186 const struct got_error *
4187 got_worktree_schedule_delete(struct got_worktree *worktree,
4188 struct got_pathlist_head *paths, int delete_local_mods,
4189 const char *status_codes,
4190 got_worktree_delete_cb progress_cb, void *progress_arg,
4191 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4193 struct got_fileindex *fileindex = NULL;
4194 char *fileindex_path = NULL;
4195 const struct got_error *err = NULL, *sync_err, *unlockerr;
4196 struct got_pathlist_entry *pe;
4197 struct schedule_deletion_args sda;
4199 err = lock_worktree(worktree, LOCK_EX);
4203 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4207 sda.worktree = worktree;
4208 sda.fileindex = fileindex;
4209 sda.progress_cb = progress_cb;
4210 sda.progress_arg = progress_arg;
4212 sda.delete_local_mods = delete_local_mods;
4213 sda.keep_on_disk = keep_on_disk;
4214 sda.ignore_missing_paths = ignore_missing_paths;
4215 sda.status_codes = status_codes;
4217 TAILQ_FOREACH(pe, paths, entry) {
4218 err = worktree_status(worktree, pe->path, fileindex, repo,
4219 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4223 sync_err = sync_fileindex(fileindex, fileindex_path);
4224 if (sync_err && err == NULL)
4227 free(fileindex_path);
4229 got_fileindex_free(fileindex);
4230 unlockerr = lock_worktree(worktree, LOCK_SH);
4231 if (unlockerr && err == NULL)
4236 static const struct got_error *
4237 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4239 const struct got_error *err = NULL;
4241 size_t linesize = 0, n;
4244 linelen = getline(&line, &linesize, infile);
4245 if (linelen == -1) {
4246 if (ferror(infile)) {
4247 err = got_error_from_errno("getline");
4253 n = fwrite(line, 1, linelen, outfile);
4255 err = got_ferror(outfile, GOT_ERR_IO);
4260 n = fwrite(line, 1, linelen, rejectfile);
4262 err = got_ferror(rejectfile, GOT_ERR_IO);
4269 static const struct got_error *
4270 skip_one_line(FILE *f)
4273 size_t linesize = 0;
4276 linelen = getline(&line, &linesize, f);
4277 if (linelen == -1) {
4279 return got_error_from_errno("getline");
4286 static const struct got_error *
4287 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4288 int start_old, int end_old, int start_new, int end_new,
4289 FILE *outfile, FILE *rejectfile)
4291 const struct got_error *err;
4293 /* Copy old file's lines leading up to patch. */
4294 while (!feof(f1) && *line_cur1 < start_old) {
4295 err = copy_one_line(f1, outfile, NULL);
4300 /* Skip new file's lines leading up to patch. */
4301 while (!feof(f2) && *line_cur2 < start_new) {
4303 err = copy_one_line(f2, NULL, rejectfile);
4305 err = skip_one_line(f2);
4310 /* Copy patched lines. */
4311 while (!feof(f2) && *line_cur2 <= end_new) {
4312 err = copy_one_line(f2, outfile, NULL);
4317 /* Skip over old file's replaced lines. */
4318 while (!feof(f1) && *line_cur1 <= end_old) {
4320 err = copy_one_line(f1, NULL, rejectfile);
4322 err = skip_one_line(f1);
4331 static const struct got_error *
4332 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4333 FILE *outfile, FILE *rejectfile)
4335 const struct got_error *err;
4338 /* Copy old file's lines until EOF. */
4340 err = copy_one_line(f1, outfile, NULL);
4347 /* Copy new file's lines until EOF. */
4349 err = copy_one_line(f2, NULL, rejectfile);
4359 static const struct got_error *
4360 apply_or_reject_change(int *choice, int *nchunks_used,
4361 struct diff_result *diff_result, int n,
4362 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4363 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4364 got_worktree_patch_cb patch_cb, void *patch_arg)
4366 const struct got_error *err = NULL;
4367 struct diff_chunk_context cc = {};
4368 int start_old, end_old, start_new, end_new;
4370 struct diff_output_unidiff_state *diff_state;
4371 struct diff_input_info diff_info;
4374 *choice = GOT_PATCH_CHOICE_NONE;
4376 /* Get changed line numbers without context lines for copy_change(). */
4377 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4378 start_old = cc.left.start;
4379 end_old = cc.left.end;
4380 start_new = cc.right.start;
4381 end_new = cc.right.end;
4383 /* Get the same change with context lines for display. */
4384 memset(&cc, 0, sizeof(cc));
4385 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4387 memset(&diff_info, 0, sizeof(diff_info));
4388 diff_info.left_path = relpath;
4389 diff_info.right_path = relpath;
4391 diff_state = diff_output_unidiff_state_alloc();
4392 if (diff_state == NULL)
4393 return got_error_set_errno(ENOMEM,
4394 "diff_output_unidiff_state_alloc");
4396 hunkfile = got_opentemp();
4397 if (hunkfile == NULL) {
4398 err = got_error_from_errno("got_opentemp");
4402 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4404 if (rc != DIFF_RC_OK) {
4405 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4409 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4410 err = got_ferror(hunkfile, GOT_ERR_IO);
4414 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4415 hunkfile, changeno, nchanges);
4420 case GOT_PATCH_CHOICE_YES:
4421 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4422 end_old, start_new, end_new, outfile, rejectfile);
4424 case GOT_PATCH_CHOICE_NO:
4425 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4426 end_old, start_new, end_new, rejectfile, outfile);
4428 case GOT_PATCH_CHOICE_QUIT:
4431 err = got_error(GOT_ERR_PATCH_CHOICE);
4435 diff_output_unidiff_state_free(diff_state);
4436 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4437 err = got_error_from_errno("fclose");
4441 struct revert_file_args {
4442 struct got_worktree *worktree;
4443 struct got_fileindex *fileindex;
4444 got_worktree_checkout_cb progress_cb;
4446 got_worktree_patch_cb patch_cb;
4448 struct got_repository *repo;
4449 int unlink_added_files;
4452 static const struct got_error *
4453 create_patched_content(char **path_outfile, int reverse_patch,
4454 struct got_object_id *blob_id, const char *path2,
4455 int dirfd2, const char *de_name2,
4456 const char *relpath, struct got_repository *repo,
4457 got_worktree_patch_cb patch_cb, void *patch_arg)
4459 const struct got_error *err, *free_err;
4460 struct got_blob_object *blob = NULL;
4461 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4462 int fd = -1, fd2 = -1;
4463 char link_target[PATH_MAX];
4464 ssize_t link_len = 0;
4465 char *path1 = NULL, *id_str = NULL;
4467 struct got_diffreg_result *diffreg_result = NULL;
4468 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4469 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4471 *path_outfile = NULL;
4473 err = got_object_id_str(&id_str, blob_id);
4478 fd2 = openat(dirfd2, de_name2,
4479 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4481 if (!got_err_open_nofollow_on_symlink()) {
4482 err = got_error_from_errno2("openat", path2);
4485 link_len = readlinkat(dirfd2, de_name2,
4486 link_target, sizeof(link_target));
4487 if (link_len == -1) {
4488 return got_error_from_errno2("readlinkat",
4491 sb2.st_mode = S_IFLNK;
4492 sb2.st_size = link_len;
4495 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4497 if (!got_err_open_nofollow_on_symlink()) {
4498 err = got_error_from_errno2("open", path2);
4501 link_len = readlink(path2, link_target,
4502 sizeof(link_target));
4504 return got_error_from_errno2("readlink", path2);
4505 sb2.st_mode = S_IFLNK;
4506 sb2.st_size = link_len;
4510 if (fstat(fd2, &sb2) == -1) {
4511 err = got_error_from_errno2("fstat", path2);
4515 f2 = fdopen(fd2, "r");
4517 err = got_error_from_errno2("fdopen", path2);
4523 f2 = got_opentemp();
4525 err = got_error_from_errno2("got_opentemp", path2);
4528 n = fwrite(link_target, 1, link_len, f2);
4529 if (n != link_len) {
4530 err = got_ferror(f2, GOT_ERR_IO);
4533 if (fflush(f2) == EOF) {
4534 err = got_error_from_errno("fflush");
4540 fd = got_opentempfd();
4542 err = got_error_from_errno("got_opentempfd");
4546 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4550 err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4554 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4558 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4559 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4563 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4568 if (fseek(f1, 0L, SEEK_SET) == -1)
4569 return got_ferror(f1, GOT_ERR_IO);
4570 if (fseek(f2, 0L, SEEK_SET) == -1)
4571 return got_ferror(f2, GOT_ERR_IO);
4573 /* Count the number of actual changes in the diff result. */
4574 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4575 struct diff_chunk_context cc = {};
4576 diff_chunk_context_load_change(&cc, &nchunks_used,
4577 diffreg_result->result, n, 0);
4580 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4582 err = apply_or_reject_change(&choice, &nchunks_used,
4583 diffreg_result->result, n, relpath, f1, f2,
4584 &line_cur1, &line_cur2,
4585 reverse_patch ? NULL : outfile,
4586 reverse_patch ? outfile : NULL,
4587 ++i, nchanges, patch_cb, patch_arg);
4590 if (choice == GOT_PATCH_CHOICE_YES)
4592 else if (choice == GOT_PATCH_CHOICE_QUIT)
4596 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4597 reverse_patch ? NULL : outfile,
4598 reverse_patch ? outfile : NULL);
4602 if (!S_ISLNK(sb2.st_mode)) {
4605 mode = apply_umask(sb2.st_mode);
4606 if (fchmod(fileno(outfile), mode) == -1) {
4607 err = got_error_from_errno2("fchmod", path2);
4614 if (fd != -1 && close(fd) == -1 && err == NULL)
4615 err = got_error_from_errno("close");
4617 got_object_blob_close(blob);
4618 free_err = got_diffreg_result_free(diffreg_result);
4621 if (f1 && fclose(f1) == EOF && err == NULL)
4622 err = got_error_from_errno2("fclose", path1);
4623 if (f2 && fclose(f2) == EOF && err == NULL)
4624 err = got_error_from_errno2("fclose", path2);
4625 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4626 err = got_error_from_errno2("close", path2);
4627 if (outfile && fclose(outfile) == EOF && err == NULL)
4628 err = got_error_from_errno2("fclose", *path_outfile);
4629 if (path1 && unlink(path1) == -1 && err == NULL)
4630 err = got_error_from_errno2("unlink", path1);
4631 if (err || !have_content) {
4632 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4633 err = got_error_from_errno2("unlink", *path_outfile);
4634 free(*path_outfile);
4635 *path_outfile = NULL;
4641 static const struct got_error *
4642 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4643 const char *relpath, struct got_object_id *blob_id,
4644 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4645 int dirfd, const char *de_name)
4647 struct revert_file_args *a = arg;
4648 const struct got_error *err = NULL;
4649 char *parent_path = NULL;
4650 struct got_fileindex_entry *ie;
4651 struct got_commit_object *base_commit = NULL;
4652 struct got_tree_object *tree = NULL;
4653 struct got_object_id *tree_id = NULL;
4654 const struct got_tree_entry *te = NULL;
4655 char *tree_path = NULL, *te_name;
4656 char *ondisk_path = NULL, *path_content = NULL;
4657 struct got_blob_object *blob = NULL;
4660 /* Reverting a staged deletion is a no-op. */
4661 if (status == GOT_STATUS_DELETE &&
4662 staged_status != GOT_STATUS_NO_CHANGE)
4665 if (status == GOT_STATUS_UNVERSIONED)
4666 return (*a->progress_cb)(a->progress_arg,
4667 GOT_STATUS_UNVERSIONED, relpath);
4669 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4671 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4673 /* Construct in-repository path of tree which contains this blob. */
4674 err = got_path_dirname(&parent_path, ie->path);
4676 if (err->code != GOT_ERR_BAD_PATH)
4678 parent_path = strdup("/");
4679 if (parent_path == NULL) {
4680 err = got_error_from_errno("strdup");
4684 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4685 tree_path = strdup(parent_path);
4686 if (tree_path == NULL) {
4687 err = got_error_from_errno("strdup");
4691 if (got_path_is_root_dir(parent_path)) {
4692 tree_path = strdup(a->worktree->path_prefix);
4693 if (tree_path == NULL) {
4694 err = got_error_from_errno("strdup");
4698 if (asprintf(&tree_path, "%s/%s",
4699 a->worktree->path_prefix, parent_path) == -1) {
4700 err = got_error_from_errno("asprintf");
4706 err = got_object_open_as_commit(&base_commit, a->repo,
4707 a->worktree->base_commit_id);
4711 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4713 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4714 (status == GOT_STATUS_ADD ||
4715 staged_status == GOT_STATUS_ADD)))
4718 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4722 err = got_path_basename(&te_name, ie->path);
4726 te = got_object_tree_find_entry(tree, te_name);
4728 if (te == NULL && status != GOT_STATUS_ADD &&
4729 staged_status != GOT_STATUS_ADD) {
4730 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4736 case GOT_STATUS_ADD:
4738 int choice = GOT_PATCH_CHOICE_NONE;
4739 err = (*a->patch_cb)(&choice, a->patch_arg,
4740 status, ie->path, NULL, 1, 1);
4743 if (choice != GOT_PATCH_CHOICE_YES)
4746 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4750 got_fileindex_entry_remove(a->fileindex, ie);
4751 if (a->unlink_added_files) {
4752 if (asprintf(&ondisk_path, "%s/%s",
4753 got_worktree_get_root_path(a->worktree),
4755 err = got_error_from_errno("asprintf");
4758 if (unlink(ondisk_path) == -1) {
4759 err = got_error_from_errno2("unlink",
4765 case GOT_STATUS_DELETE:
4767 int choice = GOT_PATCH_CHOICE_NONE;
4768 err = (*a->patch_cb)(&choice, a->patch_arg,
4769 status, ie->path, NULL, 1, 1);
4772 if (choice != GOT_PATCH_CHOICE_YES)
4776 case GOT_STATUS_MODIFY:
4777 case GOT_STATUS_MODE_CHANGE:
4778 case GOT_STATUS_CONFLICT:
4779 case GOT_STATUS_MISSING: {
4780 struct got_object_id id;
4781 if (staged_status == GOT_STATUS_ADD ||
4782 staged_status == GOT_STATUS_MODIFY)
4783 got_fileindex_entry_get_staged_blob_id(&id, ie);
4785 got_fileindex_entry_get_blob_id(&id, ie);
4786 fd = got_opentempfd();
4788 err = got_error_from_errno("got_opentempfd");
4792 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4796 if (asprintf(&ondisk_path, "%s/%s",
4797 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4798 err = got_error_from_errno("asprintf");
4802 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4803 status == GOT_STATUS_CONFLICT)) {
4804 int is_bad_symlink = 0;
4805 err = create_patched_content(&path_content, 1, &id,
4806 ondisk_path, dirfd, de_name, ie->path, a->repo,
4807 a->patch_cb, a->patch_arg);
4808 if (err || path_content == NULL)
4810 if (te && S_ISLNK(te->mode)) {
4811 if (unlink(path_content) == -1) {
4812 err = got_error_from_errno2("unlink",
4816 err = install_symlink(&is_bad_symlink,
4817 a->worktree, ondisk_path, ie->path,
4818 blob, 0, 1, 0, 0, a->repo,
4819 a->progress_cb, a->progress_arg);
4821 if (rename(path_content, ondisk_path) == -1) {
4822 err = got_error_from_errno3("rename",
4823 path_content, ondisk_path);
4828 int is_bad_symlink = 0;
4829 if (te && S_ISLNK(te->mode)) {
4830 err = install_symlink(&is_bad_symlink,
4831 a->worktree, ondisk_path, ie->path,
4832 blob, 0, 1, 0, 0, a->repo,
4833 a->progress_cb, a->progress_arg);
4835 err = install_blob(a->worktree, ondisk_path,
4837 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4838 got_fileindex_perms_to_st(ie), blob,
4839 0, 1, 0, 0, a->repo,
4840 a->progress_cb, a->progress_arg);
4844 if (status == GOT_STATUS_DELETE ||
4845 status == GOT_STATUS_MODE_CHANGE) {
4846 err = got_fileindex_entry_update(ie,
4847 a->worktree->root_fd, relpath,
4849 a->worktree->base_commit_id->sha1, 1);
4853 if (is_bad_symlink) {
4854 got_fileindex_entry_filetype_set(ie,
4855 GOT_FILEIDX_MODE_BAD_SYMLINK);
4868 if (fd != -1 && close(fd) == -1 && err == NULL)
4869 err = got_error_from_errno("close");
4871 got_object_blob_close(blob);
4873 got_object_tree_close(tree);
4876 got_object_commit_close(base_commit);
4880 const struct got_error *
4881 got_worktree_revert(struct got_worktree *worktree,
4882 struct got_pathlist_head *paths,
4883 got_worktree_checkout_cb progress_cb, void *progress_arg,
4884 got_worktree_patch_cb patch_cb, void *patch_arg,
4885 struct got_repository *repo)
4887 struct got_fileindex *fileindex = NULL;
4888 char *fileindex_path = NULL;
4889 const struct got_error *err = NULL, *unlockerr = NULL;
4890 const struct got_error *sync_err = NULL;
4891 struct got_pathlist_entry *pe;
4892 struct revert_file_args rfa;
4894 err = lock_worktree(worktree, LOCK_EX);
4898 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4902 rfa.worktree = worktree;
4903 rfa.fileindex = fileindex;
4904 rfa.progress_cb = progress_cb;
4905 rfa.progress_arg = progress_arg;
4906 rfa.patch_cb = patch_cb;
4907 rfa.patch_arg = patch_arg;
4909 rfa.unlink_added_files = 0;
4910 TAILQ_FOREACH(pe, paths, entry) {
4911 err = worktree_status(worktree, pe->path, fileindex, repo,
4912 revert_file, &rfa, NULL, NULL, 1, 0);
4916 sync_err = sync_fileindex(fileindex, fileindex_path);
4917 if (sync_err && err == NULL)
4920 free(fileindex_path);
4922 got_fileindex_free(fileindex);
4923 unlockerr = lock_worktree(worktree, LOCK_SH);
4924 if (unlockerr && err == NULL)
4930 free_commitable(struct got_commitable *ct)
4933 free(ct->in_repo_path);
4934 free(ct->ondisk_path);
4936 free(ct->base_blob_id);
4937 free(ct->staged_blob_id);
4938 free(ct->base_commit_id);
4942 struct collect_commitables_arg {
4943 struct got_pathlist_head *commitable_paths;
4944 struct got_repository *repo;
4945 struct got_worktree *worktree;
4946 struct got_fileindex *fileindex;
4947 int have_staged_files;
4948 int allow_bad_symlinks;
4949 int diff_header_shown;
4956 * Create a file which contains the target path of a symlink so we can feed
4957 * it as content to the diff engine.
4959 static const struct got_error *
4960 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4961 const char *abspath)
4963 const struct got_error *err = NULL;
4964 char target_path[PATH_MAX];
4965 ssize_t target_len, outlen;
4970 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4971 if (target_len == -1)
4972 return got_error_from_errno2("readlinkat", abspath);
4974 target_len = readlink(abspath, target_path, PATH_MAX);
4975 if (target_len == -1)
4976 return got_error_from_errno2("readlink", abspath);
4979 *fd = got_opentempfd();
4981 return got_error_from_errno("got_opentempfd");
4983 outlen = write(*fd, target_path, target_len);
4985 err = got_error_from_errno("got_opentempfd");
4989 if (lseek(*fd, 0, SEEK_SET) == -1) {
4990 err = got_error_from_errno2("lseek", abspath);
5001 static const struct got_error *
5002 append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5003 FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5004 int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5006 const struct got_error *err = NULL;
5007 struct got_blob_object *blob1 = NULL;
5008 int fd = -1, fd1 = -1, fd2 = -1;
5009 FILE *ondisk_file = NULL;
5010 char *label1 = NULL;
5014 char *id_str = NULL;
5016 memset(&sb, 0, sizeof(sb));
5019 if (ct->staged_status != GOT_STATUS_MODIFY &&
5020 ct->staged_status != GOT_STATUS_ADD &&
5021 ct->staged_status != GOT_STATUS_DELETE)
5024 if (ct->status != GOT_STATUS_MODIFY &&
5025 ct->status != GOT_STATUS_ADD &&
5026 ct->status != GOT_STATUS_DELETE)
5030 err = got_opentemp_truncate(f1);
5032 return got_error_from_errno("got_opentemp_truncate");
5033 err = got_opentemp_truncate(f2);
5035 return got_error_from_errno("got_opentemp_truncate");
5037 if (!*diff_header_shown) {
5038 err = got_object_id_str(&id_str, worktree->base_commit_id);
5041 fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5042 got_worktree_get_root_path(worktree));
5043 fprintf(diff_outfile, "commit - %s\n", id_str);
5044 fprintf(diff_outfile, "path + %s%s\n",
5045 got_worktree_get_root_path(worktree),
5046 diff_staged ? " (staged changes)" : "");
5047 *diff_header_shown = 1;
5051 const char *label1 = NULL, *label2 = NULL;
5052 switch (ct->staged_status) {
5053 case GOT_STATUS_MODIFY:
5057 case GOT_STATUS_ADD:
5060 case GOT_STATUS_DELETE:
5064 return got_error(GOT_ERR_FILE_STATUS);
5066 fd1 = got_opentempfd();
5068 err = got_error_from_errno("got_opentempfd");
5071 fd2 = got_opentempfd();
5073 err = got_error_from_errno("got_opentempfd");
5076 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5077 fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5078 label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5079 NULL, repo, diff_outfile);
5083 fd1 = got_opentempfd();
5085 err = got_error_from_errno("got_opentempfd");
5089 if (ct->status != GOT_STATUS_ADD) {
5090 err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5096 if (ct->status != GOT_STATUS_DELETE) {
5098 fd = openat(dirfd, de_name,
5099 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5101 if (!got_err_open_nofollow_on_symlink()) {
5102 err = got_error_from_errno2("openat",
5106 err = get_symlink_target_file(&fd, dirfd,
5107 de_name, ct->ondisk_path);
5112 fd = open(ct->ondisk_path,
5113 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5115 if (!got_err_open_nofollow_on_symlink()) {
5116 err = got_error_from_errno2("open",
5120 err = get_symlink_target_file(&fd, dirfd,
5121 de_name, ct->ondisk_path);
5126 if (fstatat(fd, ct->ondisk_path, &sb,
5127 AT_SYMLINK_NOFOLLOW) == -1) {
5128 err = got_error_from_errno2("fstatat", ct->ondisk_path);
5131 ondisk_file = fdopen(fd, "r");
5132 if (ondisk_file == NULL) {
5133 err = got_error_from_errno2("fdopen", ct->ondisk_path);
5141 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5147 err = got_diff_blob_file(blob1, f1, size1, label1,
5148 ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5149 GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5151 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5152 err = got_error_from_errno("close");
5153 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5154 err = got_error_from_errno("close");
5156 got_object_blob_close(blob1);
5157 if (fd != -1 && close(fd) == -1 && err == NULL)
5158 err = got_error_from_errno("close");
5159 if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5160 err = got_error_from_errno("fclose");
5164 static const struct got_error *
5165 collect_commitables(void *arg, unsigned char status,
5166 unsigned char staged_status, const char *relpath,
5167 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5168 struct got_object_id *commit_id, int dirfd, const char *de_name)
5170 struct collect_commitables_arg *a = arg;
5171 const struct got_error *err = NULL;
5172 struct got_commitable *ct = NULL;
5173 struct got_pathlist_entry *new = NULL;
5174 char *parent_path = NULL, *path = NULL;
5177 if (a->have_staged_files) {
5178 if (staged_status != GOT_STATUS_MODIFY &&
5179 staged_status != GOT_STATUS_ADD &&
5180 staged_status != GOT_STATUS_DELETE)
5183 if (status == GOT_STATUS_CONFLICT)
5184 return got_error(GOT_ERR_COMMIT_CONFLICT);
5186 if (status != GOT_STATUS_MODIFY &&
5187 status != GOT_STATUS_MODE_CHANGE &&
5188 status != GOT_STATUS_ADD &&
5189 status != GOT_STATUS_DELETE)
5193 if (asprintf(&path, "/%s", relpath) == -1) {
5194 err = got_error_from_errno("asprintf");
5197 if (strcmp(path, "/") == 0) {
5198 parent_path = strdup("");
5199 if (parent_path == NULL)
5200 return got_error_from_errno("strdup");
5202 err = got_path_dirname(&parent_path, path);
5207 ct = calloc(1, sizeof(*ct));
5209 err = got_error_from_errno("calloc");
5213 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5215 err = got_error_from_errno("asprintf");
5219 if (staged_status == GOT_STATUS_ADD ||
5220 staged_status == GOT_STATUS_MODIFY) {
5221 struct got_fileindex_entry *ie;
5222 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5223 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5224 case GOT_FILEIDX_MODE_REGULAR_FILE:
5225 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5228 case GOT_FILEIDX_MODE_SYMLINK:
5232 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5235 ct->mode |= got_fileindex_entry_perms_get(ie);
5236 } else if (status != GOT_STATUS_DELETE &&
5237 staged_status != GOT_STATUS_DELETE) {
5239 if (fstatat(dirfd, de_name, &sb,
5240 AT_SYMLINK_NOFOLLOW) == -1) {
5241 err = got_error_from_errno2("fstatat",
5245 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5246 err = got_error_from_errno2("lstat", ct->ondisk_path);
5249 ct->mode = sb.st_mode;
5252 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5253 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5255 err = got_error_from_errno("asprintf");
5259 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5260 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5262 char target_path[PATH_MAX];
5264 target_len = readlink(ct->ondisk_path, target_path,
5265 sizeof(target_path));
5266 if (target_len == -1) {
5267 err = got_error_from_errno2("readlink",
5271 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5272 target_len, ct->ondisk_path, a->worktree->root_path);
5275 if (is_bad_symlink) {
5276 err = got_error_path(ct->ondisk_path,
5277 GOT_ERR_BAD_SYMLINK);
5283 ct->status = status;
5284 ct->staged_status = staged_status;
5285 ct->blob_id = NULL; /* will be filled in when blob gets created */
5286 if (ct->status != GOT_STATUS_ADD &&
5287 ct->staged_status != GOT_STATUS_ADD) {
5288 ct->base_blob_id = got_object_id_dup(blob_id);
5289 if (ct->base_blob_id == NULL) {
5290 err = got_error_from_errno("got_object_id_dup");
5293 ct->base_commit_id = got_object_id_dup(commit_id);
5294 if (ct->base_commit_id == NULL) {
5295 err = got_error_from_errno("got_object_id_dup");
5299 if (ct->staged_status == GOT_STATUS_ADD ||
5300 ct->staged_status == GOT_STATUS_MODIFY) {
5301 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5302 if (ct->staged_blob_id == NULL) {
5303 err = got_error_from_errno("got_object_id_dup");
5307 ct->path = strdup(path);
5308 if (ct->path == NULL) {
5309 err = got_error_from_errno("strdup");
5312 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5316 if (a->diff_outfile && ct && new != NULL) {
5317 err = append_ct_diff(ct, &a->diff_header_shown,
5318 a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5319 a->have_staged_files, a->repo, a->worktree);
5324 if (ct && (err || new == NULL))
5325 free_commitable(ct);
5331 static const struct got_error *write_tree(struct got_object_id **, int *,
5332 struct got_tree_object *, const char *, struct got_pathlist_head *,
5333 got_worktree_status_cb status_cb, void *status_arg,
5334 struct got_repository *);
5336 static const struct got_error *
5337 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5338 struct got_tree_entry *te, const char *parent_path,
5339 struct got_pathlist_head *commitable_paths,
5340 got_worktree_status_cb status_cb, void *status_arg,
5341 struct got_repository *repo)
5343 const struct got_error *err = NULL;
5344 struct got_tree_object *subtree;
5347 if (asprintf(&subpath, "%s%s%s", parent_path,
5348 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5349 return got_error_from_errno("asprintf");
5351 err = got_object_open_as_tree(&subtree, repo, &te->id);
5355 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5356 commitable_paths, status_cb, status_arg, repo);
5357 got_object_tree_close(subtree);
5362 static const struct got_error *
5363 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5365 const struct got_error *err = NULL;
5366 char *ct_parent_path = NULL;
5370 if (strchr(ct->in_repo_path, '/') == NULL) {
5371 *match = got_path_is_root_dir(path);
5375 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5378 *match = (strcmp(path, ct_parent_path) == 0);
5379 free(ct_parent_path);
5384 get_ct_file_mode(struct got_commitable *ct)
5386 if (S_ISLNK(ct->mode))
5389 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5392 static const struct got_error *
5393 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5394 struct got_tree_entry *te, struct got_commitable *ct)
5396 const struct got_error *err = NULL;
5400 err = got_object_tree_entry_dup(new_te, te);
5404 (*new_te)->mode = get_ct_file_mode(ct);
5406 if (ct->staged_status == GOT_STATUS_MODIFY)
5407 memcpy(&(*new_te)->id, ct->staged_blob_id,
5408 sizeof((*new_te)->id));
5410 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5412 if (err && *new_te) {
5419 static const struct got_error *
5420 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5421 struct got_commitable *ct)
5423 const struct got_error *err = NULL;
5424 char *ct_name = NULL;
5428 *new_te = calloc(1, sizeof(**new_te));
5429 if (*new_te == NULL)
5430 return got_error_from_errno("calloc");
5432 err = got_path_basename(&ct_name, ct->path);
5435 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5436 sizeof((*new_te)->name)) {
5437 err = got_error(GOT_ERR_NO_SPACE);
5441 (*new_te)->mode = get_ct_file_mode(ct);
5443 if (ct->staged_status == GOT_STATUS_ADD)
5444 memcpy(&(*new_te)->id, ct->staged_blob_id,
5445 sizeof((*new_te)->id));
5447 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5450 if (err && *new_te) {
5457 static const struct got_error *
5458 insert_tree_entry(struct got_tree_entry *new_te,
5459 struct got_pathlist_head *paths)
5461 const struct got_error *err = NULL;
5462 struct got_pathlist_entry *new_pe;
5464 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5468 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5472 static const struct got_error *
5473 report_ct_status(struct got_commitable *ct,
5474 got_worktree_status_cb status_cb, void *status_arg)
5476 const char *ct_path = ct->path;
5477 unsigned char status;
5479 if (status_cb == NULL) /* no commit progress output desired */
5482 while (ct_path[0] == '/')
5485 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5486 status = ct->staged_status;
5488 status = ct->status;
5490 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5491 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5494 static const struct got_error *
5495 match_modified_subtree(int *modified, struct got_tree_entry *te,
5496 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5498 const struct got_error *err = NULL;
5499 struct got_pathlist_entry *pe;
5504 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5505 got_path_is_root_dir(base_tree_path) ? "" : "/",
5507 return got_error_from_errno("asprintf");
5509 TAILQ_FOREACH(pe, commitable_paths, entry) {
5510 struct got_commitable *ct = pe->data;
5511 *modified = got_path_is_child(ct->in_repo_path, te_path,
5521 static const struct got_error *
5522 match_deleted_or_modified_ct(struct got_commitable **ctp,
5523 struct got_tree_entry *te, const char *base_tree_path,
5524 struct got_pathlist_head *commitable_paths)
5526 const struct got_error *err = NULL;
5527 struct got_pathlist_entry *pe;
5531 TAILQ_FOREACH(pe, commitable_paths, entry) {
5532 struct got_commitable *ct = pe->data;
5533 char *ct_name = NULL;
5536 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5537 if (ct->status != GOT_STATUS_MODIFY &&
5538 ct->status != GOT_STATUS_MODE_CHANGE &&
5539 ct->status != GOT_STATUS_DELETE)
5542 if (ct->staged_status != GOT_STATUS_MODIFY &&
5543 ct->staged_status != GOT_STATUS_DELETE)
5547 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5550 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5556 err = got_path_basename(&ct_name, pe->path);
5560 if (strcmp(te->name, ct_name) != 0) {
5573 static const struct got_error *
5574 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5575 const char *child_path, const char *path_base_tree,
5576 struct got_pathlist_head *commitable_paths,
5577 got_worktree_status_cb status_cb, void *status_arg,
5578 struct got_repository *repo)
5580 const struct got_error *err = NULL;
5581 struct got_tree_entry *new_te;
5583 struct got_object_id *id = NULL;
5588 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5589 got_path_is_root_dir(path_base_tree) ? "" : "/",
5591 return got_error_from_errno("asprintf");
5593 new_te = calloc(1, sizeof(*new_te));
5595 return got_error_from_errno("calloc");
5596 new_te->mode = S_IFDIR;
5598 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5599 sizeof(new_te->name)) {
5600 err = got_error(GOT_ERR_NO_SPACE);
5603 err = write_tree(&id, &nentries, NULL, subtree_path,
5604 commitable_paths, status_cb, status_arg, repo);
5609 memcpy(&new_te->id, id, sizeof(new_te->id));
5618 static const struct got_error *
5619 write_tree(struct got_object_id **new_tree_id, int *nentries,
5620 struct got_tree_object *base_tree, const char *path_base_tree,
5621 struct got_pathlist_head *commitable_paths,
5622 got_worktree_status_cb status_cb, void *status_arg,
5623 struct got_repository *repo)
5625 const struct got_error *err = NULL;
5626 struct got_pathlist_head paths;
5627 struct got_tree_entry *te, *new_te = NULL;
5628 struct got_pathlist_entry *pe;
5633 /* Insert, and recurse into, newly added entries first. */
5634 TAILQ_FOREACH(pe, commitable_paths, entry) {
5635 struct got_commitable *ct = pe->data;
5636 char *child_path = NULL, *slash;
5638 if ((ct->status != GOT_STATUS_ADD &&
5639 ct->staged_status != GOT_STATUS_ADD) ||
5640 (ct->flags & GOT_COMMITABLE_ADDED))
5643 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5644 strlen(path_base_tree)))
5647 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5652 slash = strchr(child_path, '/');
5653 if (slash == NULL) {
5654 err = alloc_added_blob_tree_entry(&new_te, ct);
5657 err = report_ct_status(ct, status_cb, status_arg);
5660 ct->flags |= GOT_COMMITABLE_ADDED;
5661 err = insert_tree_entry(new_te, &paths);
5666 *slash = '\0'; /* trim trailing path components */
5667 if (base_tree == NULL ||
5668 got_object_tree_find_entry(base_tree, child_path)
5670 err = make_subtree_for_added_blob(&new_te,
5671 child_path, path_base_tree,
5672 commitable_paths, status_cb, status_arg,
5676 err = insert_tree_entry(new_te, &paths);
5685 int i, nbase_entries;
5686 /* Handle modified and deleted entries. */
5687 nbase_entries = got_object_tree_get_nentries(base_tree);
5688 for (i = 0; i < nbase_entries; i++) {
5689 struct got_commitable *ct = NULL;
5691 te = got_object_tree_get_entry(base_tree, i);
5692 if (got_object_tree_entry_is_submodule(te)) {
5693 /* Entry is a submodule; just copy it. */
5694 err = got_object_tree_entry_dup(&new_te, te);
5697 err = insert_tree_entry(new_te, &paths);
5704 if (S_ISDIR(te->mode)) {
5706 err = got_object_tree_entry_dup(&new_te, te);
5709 err = match_modified_subtree(&modified, te,
5710 path_base_tree, commitable_paths);
5713 /* Avoid recursion into unmodified subtrees. */
5715 struct got_object_id *new_id;
5717 err = write_subtree(&new_id,
5719 path_base_tree, commitable_paths,
5720 status_cb, status_arg, repo);
5723 if (nsubentries == 0) {
5724 /* All entries were deleted. */
5728 memcpy(&new_te->id, new_id,
5729 sizeof(new_te->id));
5732 err = insert_tree_entry(new_te, &paths);
5739 err = match_deleted_or_modified_ct(&ct, te,
5740 path_base_tree, commitable_paths);
5744 /* NB: Deleted entries get dropped here. */
5745 if (ct->status == GOT_STATUS_MODIFY ||
5746 ct->status == GOT_STATUS_MODE_CHANGE ||
5747 ct->staged_status == GOT_STATUS_MODIFY) {
5748 err = alloc_modified_blob_tree_entry(
5752 err = insert_tree_entry(new_te, &paths);
5757 err = report_ct_status(ct, status_cb,
5762 /* Entry is unchanged; just copy it. */
5763 err = got_object_tree_entry_dup(&new_te, te);
5766 err = insert_tree_entry(new_te, &paths);
5774 /* Write new list of entries; deleted entries have been dropped. */
5775 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5777 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
5781 static const struct got_error *
5782 update_fileindex_after_commit(struct got_worktree *worktree,
5783 struct got_pathlist_head *commitable_paths,
5784 struct got_object_id *new_base_commit_id,
5785 struct got_fileindex *fileindex, int have_staged_files)
5787 const struct got_error *err = NULL;
5788 struct got_pathlist_entry *pe;
5789 char *relpath = NULL;
5791 TAILQ_FOREACH(pe, commitable_paths, entry) {
5792 struct got_fileindex_entry *ie;
5793 struct got_commitable *ct = pe->data;
5795 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5797 err = got_path_skip_common_ancestor(&relpath,
5798 worktree->root_path, ct->ondisk_path);
5803 if (ct->status == GOT_STATUS_DELETE ||
5804 ct->staged_status == GOT_STATUS_DELETE) {
5805 got_fileindex_entry_remove(fileindex, ie);
5806 } else if (ct->staged_status == GOT_STATUS_ADD ||
5807 ct->staged_status == GOT_STATUS_MODIFY) {
5808 got_fileindex_entry_stage_set(ie,
5809 GOT_FILEIDX_STAGE_NONE);
5810 got_fileindex_entry_staged_filetype_set(ie, 0);
5812 err = got_fileindex_entry_update(ie,
5813 worktree->root_fd, relpath,
5814 ct->staged_blob_id->sha1,
5815 new_base_commit_id->sha1,
5816 !have_staged_files);
5818 err = got_fileindex_entry_update(ie,
5819 worktree->root_fd, relpath,
5821 new_base_commit_id->sha1,
5822 !have_staged_files);
5824 err = got_fileindex_entry_alloc(&ie, pe->path);
5827 err = got_fileindex_entry_update(ie,
5828 worktree->root_fd, relpath, ct->blob_id->sha1,
5829 new_base_commit_id->sha1, 1);
5831 got_fileindex_entry_free(ie);
5834 err = got_fileindex_entry_add(fileindex, ie);
5836 got_fileindex_entry_free(ie);
5849 static const struct got_error *
5850 check_out_of_date(const char *in_repo_path, unsigned char status,
5851 unsigned char staged_status, struct got_object_id *base_blob_id,
5852 struct got_object_id *base_commit_id,
5853 struct got_object_id *head_commit_id, struct got_repository *repo,
5856 const struct got_error *err = NULL;
5857 struct got_commit_object *commit = NULL;
5858 struct got_object_id *id = NULL;
5860 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5861 /* Trivial case: base commit == head commit */
5862 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5865 * Ensure file content which local changes were based
5866 * on matches file content in the branch head.
5868 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5871 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5873 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5874 err = got_error(ood_errcode);
5876 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5877 err = got_error(ood_errcode);
5879 /* Require that added files don't exist in the branch head. */
5880 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5883 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5884 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5886 err = id ? got_error(ood_errcode) : NULL;
5891 got_object_commit_close(commit);
5895 static const struct got_error *
5896 commit_worktree(struct got_object_id **new_commit_id,
5897 struct got_pathlist_head *commitable_paths,
5898 struct got_object_id *head_commit_id,
5899 struct got_object_id *parent_id2,
5900 struct got_worktree *worktree,
5901 const char *author, const char *committer, char *diff_path,
5902 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5903 got_worktree_status_cb status_cb, void *status_arg,
5904 struct got_repository *repo)
5906 const struct got_error *err = NULL, *unlockerr = NULL;
5907 struct got_pathlist_entry *pe;
5908 const char *head_ref_name = NULL;
5909 struct got_commit_object *head_commit = NULL;
5910 struct got_reference *head_ref2 = NULL;
5911 struct got_object_id *head_commit_id2 = NULL;
5912 struct got_tree_object *head_tree = NULL;
5913 struct got_object_id *new_tree_id = NULL;
5914 int nentries, nparents = 0;
5915 struct got_object_id_queue parent_ids;
5916 struct got_object_qid *pid = NULL;
5917 char *logmsg = NULL;
5920 *new_commit_id = NULL;
5922 STAILQ_INIT(&parent_ids);
5924 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5928 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5932 if (commit_msg_cb != NULL) {
5933 err = commit_msg_cb(commitable_paths, diff_path,
5934 &logmsg, commit_arg);
5939 if (logmsg == NULL || strlen(logmsg) == 0) {
5940 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5944 /* Create blobs from added and modified files and record their IDs. */
5945 TAILQ_FOREACH(pe, commitable_paths, entry) {
5946 struct got_commitable *ct = pe->data;
5949 /* Blobs for staged files already exist. */
5950 if (ct->staged_status == GOT_STATUS_ADD ||
5951 ct->staged_status == GOT_STATUS_MODIFY)
5954 if (ct->status != GOT_STATUS_ADD &&
5955 ct->status != GOT_STATUS_MODIFY &&
5956 ct->status != GOT_STATUS_MODE_CHANGE)
5959 if (asprintf(&ondisk_path, "%s/%s",
5960 worktree->root_path, pe->path) == -1) {
5961 err = got_error_from_errno("asprintf");
5964 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5970 /* Recursively write new tree objects. */
5971 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5972 commitable_paths, status_cb, status_arg, repo);
5976 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5979 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5982 err = got_object_qid_alloc(&pid, parent_id2);
5985 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5988 timestamp = time(NULL);
5989 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5990 nparents, author, timestamp, committer, timestamp, logmsg, repo);
5996 /* Check if a concurrent commit to our branch has occurred. */
5997 head_ref_name = got_worktree_get_head_ref_name(worktree);
5998 if (head_ref_name == NULL) {
5999 err = got_error_from_errno("got_worktree_get_head_ref_name");
6002 /* Lock the reference here to prevent concurrent modification. */
6003 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6006 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6009 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6010 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6013 /* Update branch head in repository. */
6014 err = got_ref_change_ref(head_ref2, *new_commit_id);
6017 err = got_ref_write(head_ref2, repo);
6021 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6025 err = ref_base_commit(worktree, repo);
6029 got_object_id_queue_free(&parent_ids);
6031 got_object_tree_close(head_tree);
6033 got_object_commit_close(head_commit);
6034 free(head_commit_id2);
6036 unlockerr = got_ref_unlock(head_ref2);
6037 if (unlockerr && err == NULL)
6039 got_ref_close(head_ref2);
6044 static const struct got_error *
6045 check_path_is_commitable(const char *path,
6046 struct got_pathlist_head *commitable_paths)
6048 struct got_pathlist_entry *cpe = NULL;
6049 size_t path_len = strlen(path);
6051 TAILQ_FOREACH(cpe, commitable_paths, entry) {
6052 struct got_commitable *ct = cpe->data;
6053 const char *ct_path = ct->path;
6055 while (ct_path[0] == '/')
6058 if (strcmp(path, ct_path) == 0 ||
6059 got_path_is_child(ct_path, path, path_len))
6064 return got_error_path(path, GOT_ERR_BAD_PATH);
6069 static const struct got_error *
6070 check_staged_file(void *arg, struct got_fileindex_entry *ie)
6072 int *have_staged_files = arg;
6074 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6075 *have_staged_files = 1;
6076 return got_error(GOT_ERR_CANCELLED);
6082 static const struct got_error *
6083 check_non_staged_files(struct got_fileindex *fileindex,
6084 struct got_pathlist_head *paths)
6086 struct got_pathlist_entry *pe;
6087 struct got_fileindex_entry *ie;
6089 TAILQ_FOREACH(pe, paths, entry) {
6090 if (pe->path[0] == '\0')
6092 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6094 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6095 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6096 return got_error_path(pe->path,
6097 GOT_ERR_FILE_NOT_STAGED);
6103 const struct got_error *
6104 got_worktree_commit(struct got_object_id **new_commit_id,
6105 struct got_worktree *worktree, struct got_pathlist_head *paths,
6106 const char *author, const char *committer, int allow_bad_symlinks,
6107 int show_diff, got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6108 got_worktree_status_cb status_cb, void *status_arg,
6109 struct got_repository *repo)
6111 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6112 struct got_fileindex *fileindex = NULL;
6113 char *fileindex_path = NULL;
6114 struct got_pathlist_head commitable_paths;
6115 struct collect_commitables_arg cc_arg;
6116 struct got_pathlist_entry *pe;
6117 struct got_reference *head_ref = NULL;
6118 struct got_object_id *head_commit_id = NULL;
6119 char *diff_path = NULL;
6120 int have_staged_files = 0;
6122 *new_commit_id = NULL;
6124 memset(&cc_arg, 0, sizeof(cc_arg));
6125 TAILQ_INIT(&commitable_paths);
6127 err = lock_worktree(worktree, LOCK_EX);
6131 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6135 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6139 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6143 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6144 &have_staged_files);
6145 if (err && err->code != GOT_ERR_CANCELLED)
6147 if (have_staged_files) {
6148 err = check_non_staged_files(fileindex, paths);
6153 cc_arg.commitable_paths = &commitable_paths;
6154 cc_arg.worktree = worktree;
6155 cc_arg.fileindex = fileindex;
6157 cc_arg.have_staged_files = have_staged_files;
6158 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6159 cc_arg.diff_header_shown = 0;
6161 err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6162 GOT_TMPDIR_STR "/got", ".diff");
6165 cc_arg.f1 = got_opentemp();
6166 if (cc_arg.f1 == NULL) {
6167 err = got_error_from_errno("got_opentemp");
6170 cc_arg.f2 = got_opentemp();
6171 if (cc_arg.f2 == NULL) {
6172 err = got_error_from_errno("got_opentemp");
6177 TAILQ_FOREACH(pe, paths, entry) {
6178 err = worktree_status(worktree, pe->path, fileindex, repo,
6179 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6185 if (fflush(cc_arg.diff_outfile) == EOF) {
6186 err = got_error_from_errno("fflush");
6191 if (TAILQ_EMPTY(&commitable_paths)) {
6192 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6196 TAILQ_FOREACH(pe, paths, entry) {
6197 err = check_path_is_commitable(pe->path, &commitable_paths);
6202 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6203 struct got_commitable *ct = pe->data;
6204 const char *ct_path = ct->in_repo_path;
6206 while (ct_path[0] == '/')
6208 err = check_out_of_date(ct_path, ct->status,
6209 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6210 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6216 err = commit_worktree(new_commit_id, &commitable_paths,
6217 head_commit_id, NULL, worktree, author, committer,
6218 (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6219 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6223 err = update_fileindex_after_commit(worktree, &commitable_paths,
6224 *new_commit_id, fileindex, have_staged_files);
6225 sync_err = sync_fileindex(fileindex, fileindex_path);
6226 if (sync_err && err == NULL)
6230 got_fileindex_free(fileindex);
6231 free(fileindex_path);
6232 unlockerr = lock_worktree(worktree, LOCK_SH);
6233 if (unlockerr && err == NULL)
6235 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6236 struct got_commitable *ct = pe->data;
6238 free_commitable(ct);
6240 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6241 if (diff_path && unlink(diff_path) == -1 && err == NULL)
6242 err = got_error_from_errno2("unlink", diff_path);
6244 if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6246 err = got_error_from_errno("fclose");
6251 got_commitable_get_path(struct got_commitable *ct)
6257 got_commitable_get_status(struct got_commitable *ct)
6262 struct check_rebase_ok_arg {
6263 struct got_worktree *worktree;
6264 struct got_repository *repo;
6267 static const struct got_error *
6268 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6270 const struct got_error *err = NULL;
6271 struct check_rebase_ok_arg *a = arg;
6272 unsigned char status;
6276 /* Reject rebase of a work tree with mixed base commits. */
6277 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6278 SHA1_DIGEST_LENGTH))
6279 return got_error(GOT_ERR_MIXED_COMMITS);
6281 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6283 return got_error_from_errno("asprintf");
6285 /* Reject rebase of a work tree with modified or staged files. */
6286 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6291 if (status != GOT_STATUS_NO_CHANGE)
6292 return got_error(GOT_ERR_MODIFIED);
6293 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6294 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6299 const struct got_error *
6300 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6301 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6302 struct got_worktree *worktree, struct got_reference *branch,
6303 struct got_repository *repo)
6305 const struct got_error *err = NULL;
6306 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6307 char *branch_ref_name = NULL;
6308 char *fileindex_path = NULL;
6309 struct check_rebase_ok_arg ok_arg;
6310 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6311 struct got_object_id *wt_branch_tip = NULL;
6313 *new_base_branch_ref = NULL;
6317 err = lock_worktree(worktree, LOCK_EX);
6321 err = open_fileindex(fileindex, &fileindex_path, worktree);
6325 ok_arg.worktree = worktree;
6327 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6332 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6336 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6340 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6344 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6349 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6352 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6353 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6357 err = got_ref_alloc_symref(new_base_branch_ref,
6358 new_base_branch_ref_name, wt_branch);
6361 err = got_ref_write(*new_base_branch_ref, repo);
6365 /* TODO Lock original branch's ref while rebasing? */
6367 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6371 err = got_ref_write(branch_ref, repo);
6375 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6376 worktree->base_commit_id);
6379 err = got_ref_write(*tmp_branch, repo);
6383 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6387 free(fileindex_path);
6388 free(tmp_branch_name);
6389 free(new_base_branch_ref_name);
6390 free(branch_ref_name);
6392 got_ref_close(branch_ref);
6394 got_ref_close(wt_branch);
6395 free(wt_branch_tip);
6397 if (*new_base_branch_ref) {
6398 got_ref_close(*new_base_branch_ref);
6399 *new_base_branch_ref = NULL;
6402 got_ref_close(*tmp_branch);
6406 got_fileindex_free(*fileindex);
6409 lock_worktree(worktree, LOCK_SH);
6414 const struct got_error *
6415 got_worktree_rebase_continue(struct got_object_id **commit_id,
6416 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6417 struct got_reference **branch, struct got_fileindex **fileindex,
6418 struct got_worktree *worktree, struct got_repository *repo)
6420 const struct got_error *err;
6421 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6422 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6423 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6424 char *fileindex_path = NULL;
6425 int have_staged_files = 0;
6428 *new_base_branch = NULL;
6433 err = lock_worktree(worktree, LOCK_EX);
6437 err = open_fileindex(fileindex, &fileindex_path, worktree);
6441 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6442 &have_staged_files);
6443 if (err && err->code != GOT_ERR_CANCELLED)
6445 if (have_staged_files) {
6446 err = got_error(GOT_ERR_STAGED_PATHS);
6450 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6454 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6458 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6462 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6466 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6470 err = got_ref_open(branch, repo,
6471 got_ref_get_symref_target(branch_ref), 0);
6475 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6479 err = got_ref_resolve(commit_id, repo, commit_ref);
6483 err = got_ref_open(new_base_branch, repo,
6484 new_base_branch_ref_name, 0);
6488 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6492 free(commit_ref_name);
6493 free(branch_ref_name);
6494 free(fileindex_path);
6496 got_ref_close(commit_ref);
6498 got_ref_close(branch_ref);
6503 got_ref_close(*tmp_branch);
6506 if (*new_base_branch) {
6507 got_ref_close(*new_base_branch);
6508 *new_base_branch = NULL;
6511 got_ref_close(*branch);
6515 got_fileindex_free(*fileindex);
6518 lock_worktree(worktree, LOCK_SH);
6523 const struct got_error *
6524 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6526 const struct got_error *err;
6527 char *tmp_branch_name = NULL;
6529 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6533 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6534 free(tmp_branch_name);
6538 static const struct got_error *
6539 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6540 const char *diff_path, char **logmsg, void *arg)
6546 static const struct got_error *
6547 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6548 const char *path, struct got_object_id *blob_id,
6549 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6550 int dirfd, const char *de_name)
6555 struct collect_merged_paths_arg {
6556 got_worktree_checkout_cb progress_cb;
6558 struct got_pathlist_head *merged_paths;
6561 static const struct got_error *
6562 collect_merged_paths(void *arg, unsigned char status, const char *path)
6564 const struct got_error *err;
6565 struct collect_merged_paths_arg *a = arg;
6567 struct got_pathlist_entry *new;
6569 err = (*a->progress_cb)(a->progress_arg, status, path);
6573 if (status != GOT_STATUS_MERGE &&
6574 status != GOT_STATUS_ADD &&
6575 status != GOT_STATUS_DELETE &&
6576 status != GOT_STATUS_CONFLICT)
6581 return got_error_from_errno("strdup");
6583 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6584 if (err || new == NULL)
6589 static const struct got_error *
6590 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6591 int is_rebase, struct got_repository *repo)
6593 const struct got_error *err;
6594 struct got_reference *commit_ref = NULL;
6596 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6598 if (err->code != GOT_ERR_NOT_REF)
6600 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6603 err = got_ref_write(commit_ref, repo);
6606 } else if (is_rebase) {
6607 struct got_object_id *stored_id;
6610 err = got_ref_resolve(&stored_id, repo, commit_ref);
6613 cmp = got_object_id_cmp(commit_id, stored_id);
6616 err = got_error(GOT_ERR_REBASE_COMMITID);
6622 got_ref_close(commit_ref);
6626 static const struct got_error *
6627 rebase_merge_files(struct got_pathlist_head *merged_paths,
6628 const char *commit_ref_name, struct got_worktree *worktree,
6629 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6630 struct got_object_id *commit_id, struct got_repository *repo,
6631 got_worktree_checkout_cb progress_cb, void *progress_arg,
6632 got_cancel_cb cancel_cb, void *cancel_arg)
6634 const struct got_error *err;
6635 struct got_reference *commit_ref = NULL;
6636 struct collect_merged_paths_arg cmp_arg;
6637 char *fileindex_path;
6639 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6641 err = get_fileindex_path(&fileindex_path, worktree);
6645 cmp_arg.progress_cb = progress_cb;
6646 cmp_arg.progress_arg = progress_arg;
6647 cmp_arg.merged_paths = merged_paths;
6648 err = merge_files(worktree, fileindex, fileindex_path,
6649 parent_commit_id, commit_id, repo, collect_merged_paths,
6650 &cmp_arg, cancel_cb, cancel_arg);
6652 got_ref_close(commit_ref);
6656 const struct got_error *
6657 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6658 struct got_worktree *worktree, struct got_fileindex *fileindex,
6659 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6660 struct got_repository *repo,
6661 got_worktree_checkout_cb progress_cb, void *progress_arg,
6662 got_cancel_cb cancel_cb, void *cancel_arg)
6664 const struct got_error *err;
6665 char *commit_ref_name;
6667 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6671 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6675 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6676 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6677 progress_arg, cancel_cb, cancel_arg);
6679 free(commit_ref_name);
6683 const struct got_error *
6684 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6685 struct got_worktree *worktree, struct got_fileindex *fileindex,
6686 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6687 struct got_repository *repo,
6688 got_worktree_checkout_cb progress_cb, void *progress_arg,
6689 got_cancel_cb cancel_cb, void *cancel_arg)
6691 const struct got_error *err;
6692 char *commit_ref_name;
6694 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6698 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6702 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6703 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6704 progress_arg, cancel_cb, cancel_arg);
6706 free(commit_ref_name);
6710 static const struct got_error *
6711 rebase_commit(struct got_object_id **new_commit_id,
6712 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6713 struct got_worktree *worktree, struct got_fileindex *fileindex,
6714 struct got_reference *tmp_branch, const char *committer,
6715 struct got_commit_object *orig_commit, const char *new_logmsg,
6716 struct got_repository *repo)
6718 const struct got_error *err, *sync_err;
6719 struct got_pathlist_head commitable_paths;
6720 struct collect_commitables_arg cc_arg;
6721 char *fileindex_path = NULL;
6722 struct got_reference *head_ref = NULL;
6723 struct got_object_id *head_commit_id = NULL;
6724 char *logmsg = NULL;
6726 memset(&cc_arg, 0, sizeof(cc_arg));
6727 TAILQ_INIT(&commitable_paths);
6728 *new_commit_id = NULL;
6730 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6732 err = get_fileindex_path(&fileindex_path, worktree);
6736 cc_arg.commitable_paths = &commitable_paths;
6737 cc_arg.worktree = worktree;
6739 cc_arg.have_staged_files = 0;
6741 * If possible get the status of individual files directly to
6742 * avoid crawling the entire work tree once per rebased commit.
6744 * Ideally, merged_paths would contain a list of commitables
6745 * we could use so we could skip worktree_status() entirely.
6746 * However, we would then need carefully keep track of cumulative
6747 * effects of operations such as file additions and deletions
6748 * in 'got histedit -f' (folding multiple commits into one),
6749 * and this extra complexity is not really worth it.
6752 struct got_pathlist_entry *pe;
6753 TAILQ_FOREACH(pe, merged_paths, entry) {
6754 err = worktree_status(worktree, pe->path, fileindex,
6755 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6761 err = worktree_status(worktree, "", fileindex, repo,
6762 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6767 if (TAILQ_EMPTY(&commitable_paths)) {
6768 /* No-op change; commit will be elided. */
6769 err = got_ref_delete(commit_ref, repo);
6772 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6776 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6780 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6785 logmsg = strdup(new_logmsg);
6786 if (logmsg == NULL) {
6787 err = got_error_from_errno("strdup");
6791 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6796 /* NB: commit_worktree will call free(logmsg) */
6797 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6798 NULL, worktree, got_object_commit_get_author(orig_commit),
6799 committer ? committer :
6800 got_object_commit_get_committer(orig_commit), NULL,
6801 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6805 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6809 err = got_ref_delete(commit_ref, repo);
6813 err = update_fileindex_after_commit(worktree, &commitable_paths,
6814 *new_commit_id, fileindex, 0);
6815 sync_err = sync_fileindex(fileindex, fileindex_path);
6816 if (sync_err && err == NULL)
6819 free(fileindex_path);
6820 free(head_commit_id);
6822 got_ref_close(head_ref);
6824 free(*new_commit_id);
6825 *new_commit_id = NULL;
6830 const struct got_error *
6831 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6832 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6833 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6834 const char *committer, struct got_commit_object *orig_commit,
6835 struct got_object_id *orig_commit_id, struct got_repository *repo)
6837 const struct got_error *err;
6838 char *commit_ref_name;
6839 struct got_reference *commit_ref = NULL;
6840 struct got_object_id *commit_id = NULL;
6842 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6846 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6849 err = got_ref_resolve(&commit_id, repo, commit_ref);
6852 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6853 err = got_error(GOT_ERR_REBASE_COMMITID);
6857 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6858 worktree, fileindex, tmp_branch, committer, orig_commit,
6862 got_ref_close(commit_ref);
6863 free(commit_ref_name);
6868 const struct got_error *
6869 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6870 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6871 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6872 const char *committer, struct got_commit_object *orig_commit,
6873 struct got_object_id *orig_commit_id, const char *new_logmsg,
6874 struct got_repository *repo)
6876 const struct got_error *err;
6877 char *commit_ref_name;
6878 struct got_reference *commit_ref = NULL;
6880 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6884 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6888 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6889 worktree, fileindex, tmp_branch, committer, orig_commit,
6893 got_ref_close(commit_ref);
6894 free(commit_ref_name);
6898 const struct got_error *
6899 got_worktree_rebase_postpone(struct got_worktree *worktree,
6900 struct got_fileindex *fileindex)
6903 got_fileindex_free(fileindex);
6904 return lock_worktree(worktree, LOCK_SH);
6907 static const struct got_error *
6908 delete_ref(const char *name, struct got_repository *repo)
6910 const struct got_error *err;
6911 struct got_reference *ref;
6913 err = got_ref_open(&ref, repo, name, 0);
6915 if (err->code == GOT_ERR_NOT_REF)
6920 err = got_ref_delete(ref, repo);
6925 static const struct got_error *
6926 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6928 const struct got_error *err;
6929 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6930 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6932 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6935 err = delete_ref(tmp_branch_name, repo);
6939 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6942 err = delete_ref(new_base_branch_ref_name, repo);
6946 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6949 err = delete_ref(branch_ref_name, repo);
6953 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6956 err = delete_ref(commit_ref_name, repo);
6961 free(tmp_branch_name);
6962 free(new_base_branch_ref_name);
6963 free(branch_ref_name);
6964 free(commit_ref_name);
6968 static const struct got_error *
6969 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6970 struct got_object_id *new_commit_id, struct got_repository *repo)
6972 const struct got_error *err;
6973 struct got_reference *ref = NULL;
6974 struct got_object_id *old_commit_id = NULL;
6975 const char *branch_name = NULL;
6976 char *new_id_str = NULL;
6977 char *refname = NULL;
6979 branch_name = got_ref_get_name(branch);
6980 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6981 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6984 err = got_object_id_str(&new_id_str, new_commit_id);
6988 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6989 new_id_str) == -1) {
6990 err = got_error_from_errno("asprintf");
6994 err = got_ref_resolve(&old_commit_id, repo, branch);
6998 err = got_ref_alloc(&ref, refname, old_commit_id);
7002 err = got_ref_write(ref, repo);
7006 free(old_commit_id);
7012 const struct got_error *
7013 got_worktree_rebase_complete(struct got_worktree *worktree,
7014 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7015 struct got_reference *rebased_branch, struct got_repository *repo,
7018 const struct got_error *err, *unlockerr, *sync_err;
7019 struct got_object_id *new_head_commit_id = NULL;
7020 char *fileindex_path = NULL;
7022 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7026 if (create_backup) {
7027 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7028 rebased_branch, new_head_commit_id, repo);
7033 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7037 err = got_ref_write(rebased_branch, repo);
7041 err = got_worktree_set_head_ref(worktree, rebased_branch);
7045 err = delete_rebase_refs(worktree, repo);
7049 err = get_fileindex_path(&fileindex_path, worktree);
7052 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7053 sync_err = sync_fileindex(fileindex, fileindex_path);
7054 if (sync_err && err == NULL)
7057 got_fileindex_free(fileindex);
7058 free(fileindex_path);
7059 free(new_head_commit_id);
7060 unlockerr = lock_worktree(worktree, LOCK_SH);
7061 if (unlockerr && err == NULL)
7066 const struct got_error *
7067 got_worktree_rebase_abort(struct got_worktree *worktree,
7068 struct got_fileindex *fileindex, struct got_repository *repo,
7069 struct got_reference *new_base_branch,
7070 got_worktree_checkout_cb progress_cb, void *progress_arg)
7072 const struct got_error *err, *unlockerr, *sync_err;
7073 struct got_reference *resolved = NULL;
7074 struct got_object_id *commit_id = NULL;
7075 struct got_commit_object *commit = NULL;
7076 char *fileindex_path = NULL;
7077 struct revert_file_args rfa;
7078 struct got_object_id *tree_id = NULL;
7080 err = lock_worktree(worktree, LOCK_EX);
7084 err = got_object_open_as_commit(&commit, repo,
7085 worktree->base_commit_id);
7089 err = got_ref_open(&resolved, repo,
7090 got_ref_get_symref_target(new_base_branch), 0);
7094 err = got_worktree_set_head_ref(worktree, resolved);
7099 * XXX commits to the base branch could have happened while
7100 * we were busy rebasing; should we store the original commit ID
7101 * when rebase begins and read it back here?
7103 err = got_ref_resolve(&commit_id, repo, resolved);
7107 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7111 err = got_object_id_by_path(&tree_id, repo, commit,
7112 worktree->path_prefix);
7116 err = delete_rebase_refs(worktree, repo);
7120 err = get_fileindex_path(&fileindex_path, worktree);
7124 rfa.worktree = worktree;
7125 rfa.fileindex = fileindex;
7126 rfa.progress_cb = progress_cb;
7127 rfa.progress_arg = progress_arg;
7128 rfa.patch_cb = NULL;
7129 rfa.patch_arg = NULL;
7131 rfa.unlink_added_files = 0;
7132 err = worktree_status(worktree, "", fileindex, repo,
7133 revert_file, &rfa, NULL, NULL, 1, 0);
7137 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7138 repo, progress_cb, progress_arg, NULL, NULL);
7140 sync_err = sync_fileindex(fileindex, fileindex_path);
7141 if (sync_err && err == NULL)
7144 got_ref_close(resolved);
7148 got_object_commit_close(commit);
7150 got_fileindex_free(fileindex);
7151 free(fileindex_path);
7153 unlockerr = lock_worktree(worktree, LOCK_SH);
7154 if (unlockerr && err == NULL)
7159 const struct got_error *
7160 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7161 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7162 struct got_fileindex **fileindex, struct got_worktree *worktree,
7163 struct got_repository *repo)
7165 const struct got_error *err = NULL;
7166 char *tmp_branch_name = NULL;
7167 char *branch_ref_name = NULL;
7168 char *base_commit_ref_name = NULL;
7169 char *fileindex_path = NULL;
7170 struct check_rebase_ok_arg ok_arg;
7171 struct got_reference *wt_branch = NULL;
7172 struct got_reference *base_commit_ref = NULL;
7176 *base_commit_id = NULL;
7179 err = lock_worktree(worktree, LOCK_EX);
7183 err = open_fileindex(fileindex, &fileindex_path, worktree);
7187 ok_arg.worktree = worktree;
7189 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7194 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7198 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7202 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7207 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7212 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7216 err = got_ref_write(*branch_ref, repo);
7220 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7221 worktree->base_commit_id);
7224 err = got_ref_write(base_commit_ref, repo);
7227 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7228 if (*base_commit_id == NULL) {
7229 err = got_error_from_errno("got_object_id_dup");
7233 err = got_ref_alloc(tmp_branch, tmp_branch_name,
7234 worktree->base_commit_id);
7237 err = got_ref_write(*tmp_branch, repo);
7241 err = got_worktree_set_head_ref(worktree, *tmp_branch);
7245 free(fileindex_path);
7246 free(tmp_branch_name);
7247 free(branch_ref_name);
7248 free(base_commit_ref_name);
7250 got_ref_close(wt_branch);
7253 got_ref_close(*branch_ref);
7257 got_ref_close(*tmp_branch);
7260 free(*base_commit_id);
7262 got_fileindex_free(*fileindex);
7265 lock_worktree(worktree, LOCK_SH);
7270 const struct got_error *
7271 got_worktree_histedit_postpone(struct got_worktree *worktree,
7272 struct got_fileindex *fileindex)
7275 got_fileindex_free(fileindex);
7276 return lock_worktree(worktree, LOCK_SH);
7279 const struct got_error *
7280 got_worktree_histedit_in_progress(int *in_progress,
7281 struct got_worktree *worktree)
7283 const struct got_error *err;
7284 char *tmp_branch_name = NULL;
7286 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7290 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7291 free(tmp_branch_name);
7295 const struct got_error *
7296 got_worktree_histedit_continue(struct got_object_id **commit_id,
7297 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7298 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7299 struct got_worktree *worktree, struct got_repository *repo)
7301 const struct got_error *err;
7302 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7303 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7304 struct got_reference *commit_ref = NULL;
7305 struct got_reference *base_commit_ref = NULL;
7306 char *fileindex_path = NULL;
7307 int have_staged_files = 0;
7311 *base_commit_id = NULL;
7314 err = lock_worktree(worktree, LOCK_EX);
7318 err = open_fileindex(fileindex, &fileindex_path, worktree);
7322 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7323 &have_staged_files);
7324 if (err && err->code != GOT_ERR_CANCELLED)
7326 if (have_staged_files) {
7327 err = got_error(GOT_ERR_STAGED_PATHS);
7331 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7335 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7339 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7343 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7348 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7352 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7355 err = got_ref_resolve(commit_id, repo, commit_ref);
7359 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7362 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7366 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7370 free(commit_ref_name);
7371 free(branch_ref_name);
7372 free(fileindex_path);
7374 got_ref_close(commit_ref);
7375 if (base_commit_ref)
7376 got_ref_close(base_commit_ref);
7380 free(*base_commit_id);
7381 *base_commit_id = NULL;
7383 got_ref_close(*tmp_branch);
7387 got_fileindex_free(*fileindex);
7390 lock_worktree(worktree, LOCK_EX);
7395 static const struct got_error *
7396 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7398 const struct got_error *err;
7399 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7400 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7402 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7405 err = delete_ref(tmp_branch_name, repo);
7409 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7413 err = delete_ref(base_commit_ref_name, repo);
7417 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7420 err = delete_ref(branch_ref_name, repo);
7424 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7427 err = delete_ref(commit_ref_name, repo);
7431 free(tmp_branch_name);
7432 free(base_commit_ref_name);
7433 free(branch_ref_name);
7434 free(commit_ref_name);
7438 const struct got_error *
7439 got_worktree_histedit_abort(struct got_worktree *worktree,
7440 struct got_fileindex *fileindex, struct got_repository *repo,
7441 struct got_reference *branch, struct got_object_id *base_commit_id,
7442 got_worktree_checkout_cb progress_cb, void *progress_arg)
7444 const struct got_error *err, *unlockerr, *sync_err;
7445 struct got_reference *resolved = NULL;
7446 char *fileindex_path = NULL;
7447 struct got_commit_object *commit = NULL;
7448 struct got_object_id *tree_id = NULL;
7449 struct revert_file_args rfa;
7451 err = lock_worktree(worktree, LOCK_EX);
7455 err = got_object_open_as_commit(&commit, repo,
7456 worktree->base_commit_id);
7460 err = got_ref_open(&resolved, repo,
7461 got_ref_get_symref_target(branch), 0);
7465 err = got_worktree_set_head_ref(worktree, resolved);
7469 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7473 err = got_object_id_by_path(&tree_id, repo, commit,
7474 worktree->path_prefix);
7478 err = delete_histedit_refs(worktree, repo);
7482 err = get_fileindex_path(&fileindex_path, worktree);
7486 rfa.worktree = worktree;
7487 rfa.fileindex = fileindex;
7488 rfa.progress_cb = progress_cb;
7489 rfa.progress_arg = progress_arg;
7490 rfa.patch_cb = NULL;
7491 rfa.patch_arg = NULL;
7493 rfa.unlink_added_files = 0;
7494 err = worktree_status(worktree, "", fileindex, repo,
7495 revert_file, &rfa, NULL, NULL, 1, 0);
7499 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7500 repo, progress_cb, progress_arg, NULL, NULL);
7502 sync_err = sync_fileindex(fileindex, fileindex_path);
7503 if (sync_err && err == NULL)
7506 got_ref_close(resolved);
7508 free(fileindex_path);
7510 unlockerr = lock_worktree(worktree, LOCK_SH);
7511 if (unlockerr && err == NULL)
7516 const struct got_error *
7517 got_worktree_histedit_complete(struct got_worktree *worktree,
7518 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7519 struct got_reference *edited_branch, struct got_repository *repo)
7521 const struct got_error *err, *unlockerr, *sync_err;
7522 struct got_object_id *new_head_commit_id = NULL;
7523 struct got_reference *resolved = NULL;
7524 char *fileindex_path = NULL;
7526 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7530 err = got_ref_open(&resolved, repo,
7531 got_ref_get_symref_target(edited_branch), 0);
7535 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7536 resolved, new_head_commit_id, repo);
7540 err = got_ref_change_ref(resolved, new_head_commit_id);
7544 err = got_ref_write(resolved, repo);
7548 err = got_worktree_set_head_ref(worktree, resolved);
7552 err = delete_histedit_refs(worktree, repo);
7556 err = get_fileindex_path(&fileindex_path, worktree);
7559 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7560 sync_err = sync_fileindex(fileindex, fileindex_path);
7561 if (sync_err && err == NULL)
7564 got_fileindex_free(fileindex);
7565 free(fileindex_path);
7566 free(new_head_commit_id);
7567 unlockerr = lock_worktree(worktree, LOCK_SH);
7568 if (unlockerr && err == NULL)
7573 const struct got_error *
7574 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7575 struct got_object_id *commit_id, struct got_repository *repo)
7577 const struct got_error *err;
7578 char *commit_ref_name;
7580 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7584 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7588 err = delete_ref(commit_ref_name, repo);
7590 free(commit_ref_name);
7594 const struct got_error *
7595 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7596 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7597 struct got_worktree *worktree, const char *refname,
7598 struct got_repository *repo)
7600 const struct got_error *err = NULL;
7601 char *fileindex_path = NULL;
7602 struct check_rebase_ok_arg ok_arg;
7606 *base_branch_ref = NULL;
7608 err = lock_worktree(worktree, LOCK_EX);
7612 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7613 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7614 "cannot integrate a branch into itself; "
7615 "update -b or different branch name required");
7619 err = open_fileindex(fileindex, &fileindex_path, worktree);
7623 /* Preconditions are the same as for rebase. */
7624 ok_arg.worktree = worktree;
7626 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7631 err = got_ref_open(branch_ref, repo, refname, 1);
7635 err = got_ref_open(base_branch_ref, repo,
7636 got_worktree_get_head_ref_name(worktree), 1);
7640 got_ref_close(*branch_ref);
7643 if (*base_branch_ref) {
7644 got_ref_close(*base_branch_ref);
7645 *base_branch_ref = NULL;
7648 got_fileindex_free(*fileindex);
7651 lock_worktree(worktree, LOCK_SH);
7656 const struct got_error *
7657 got_worktree_integrate_continue(struct got_worktree *worktree,
7658 struct got_fileindex *fileindex, struct got_repository *repo,
7659 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7660 got_worktree_checkout_cb progress_cb, void *progress_arg,
7661 got_cancel_cb cancel_cb, void *cancel_arg)
7663 const struct got_error *err = NULL, *sync_err, *unlockerr;
7664 char *fileindex_path = NULL;
7665 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7666 struct got_commit_object *commit = NULL;
7668 err = get_fileindex_path(&fileindex_path, worktree);
7672 err = got_ref_resolve(&commit_id, repo, branch_ref);
7676 err = got_object_open_as_commit(&commit, repo, commit_id);
7680 err = got_object_id_by_path(&tree_id, repo, commit,
7681 worktree->path_prefix);
7685 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7689 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7690 progress_cb, progress_arg, cancel_cb, cancel_arg);
7694 err = got_ref_change_ref(base_branch_ref, commit_id);
7698 err = got_ref_write(base_branch_ref, repo);
7702 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7704 sync_err = sync_fileindex(fileindex, fileindex_path);
7705 if (sync_err && err == NULL)
7709 unlockerr = got_ref_unlock(branch_ref);
7710 if (unlockerr && err == NULL)
7712 got_ref_close(branch_ref);
7714 unlockerr = got_ref_unlock(base_branch_ref);
7715 if (unlockerr && err == NULL)
7717 got_ref_close(base_branch_ref);
7719 got_fileindex_free(fileindex);
7720 free(fileindex_path);
7723 got_object_commit_close(commit);
7725 unlockerr = lock_worktree(worktree, LOCK_SH);
7726 if (unlockerr && err == NULL)
7731 const struct got_error *
7732 got_worktree_integrate_abort(struct got_worktree *worktree,
7733 struct got_fileindex *fileindex, struct got_repository *repo,
7734 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7736 const struct got_error *err = NULL, *unlockerr = NULL;
7738 got_fileindex_free(fileindex);
7740 err = lock_worktree(worktree, LOCK_SH);
7742 unlockerr = got_ref_unlock(branch_ref);
7743 if (unlockerr && err == NULL)
7745 got_ref_close(branch_ref);
7747 unlockerr = got_ref_unlock(base_branch_ref);
7748 if (unlockerr && err == NULL)
7750 got_ref_close(base_branch_ref);
7755 const struct got_error *
7756 got_worktree_merge_postpone(struct got_worktree *worktree,
7757 struct got_fileindex *fileindex)
7759 const struct got_error *err, *sync_err;
7760 char *fileindex_path = NULL;
7762 err = get_fileindex_path(&fileindex_path, worktree);
7766 sync_err = sync_fileindex(fileindex, fileindex_path);
7768 err = lock_worktree(worktree, LOCK_SH);
7769 if (sync_err && err == NULL)
7772 got_fileindex_free(fileindex);
7773 free(fileindex_path);
7777 static const struct got_error *
7778 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7780 const struct got_error *err;
7781 char *branch_refname = NULL, *commit_refname = NULL;
7783 err = get_merge_branch_ref_name(&branch_refname, worktree);
7786 err = delete_ref(branch_refname, repo);
7790 err = get_merge_commit_ref_name(&commit_refname, worktree);
7793 err = delete_ref(commit_refname, repo);
7798 free(branch_refname);
7799 free(commit_refname);
7803 struct merge_commit_msg_arg {
7804 struct got_worktree *worktree;
7805 const char *branch_name;
7808 static const struct got_error *
7809 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
7810 const char *diff_path, char **logmsg, void *arg)
7812 struct merge_commit_msg_arg *a = arg;
7814 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7815 got_worktree_get_head_ref_name(a->worktree)) == -1)
7816 return got_error_from_errno("asprintf");
7822 const struct got_error *
7823 got_worktree_merge_branch(struct got_worktree *worktree,
7824 struct got_fileindex *fileindex,
7825 struct got_object_id *yca_commit_id,
7826 struct got_object_id *branch_tip,
7827 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7828 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7830 const struct got_error *err;
7831 char *fileindex_path = NULL;
7833 err = get_fileindex_path(&fileindex_path, worktree);
7837 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7842 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7843 branch_tip, repo, progress_cb, progress_arg,
7844 cancel_cb, cancel_arg);
7846 free(fileindex_path);
7850 const struct got_error *
7851 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7852 struct got_worktree *worktree, struct got_fileindex *fileindex,
7853 const char *author, const char *committer, int allow_bad_symlinks,
7854 struct got_object_id *branch_tip, const char *branch_name,
7855 struct got_repository *repo,
7856 got_worktree_status_cb status_cb, void *status_arg)
7859 const struct got_error *err = NULL, *sync_err;
7860 struct got_pathlist_head commitable_paths;
7861 struct collect_commitables_arg cc_arg;
7862 struct got_pathlist_entry *pe;
7863 struct got_reference *head_ref = NULL;
7864 struct got_object_id *head_commit_id = NULL;
7865 int have_staged_files = 0;
7866 struct merge_commit_msg_arg mcm_arg;
7867 char *fileindex_path = NULL;
7869 memset(&cc_arg, 0, sizeof(cc_arg));
7870 *new_commit_id = NULL;
7872 TAILQ_INIT(&commitable_paths);
7874 err = get_fileindex_path(&fileindex_path, worktree);
7878 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7882 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7886 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7887 &have_staged_files);
7888 if (err && err->code != GOT_ERR_CANCELLED)
7890 if (have_staged_files) {
7891 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7895 cc_arg.commitable_paths = &commitable_paths;
7896 cc_arg.worktree = worktree;
7897 cc_arg.fileindex = fileindex;
7899 cc_arg.have_staged_files = have_staged_files;
7900 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7901 err = worktree_status(worktree, "", fileindex, repo,
7902 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7906 if (TAILQ_EMPTY(&commitable_paths)) {
7907 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7908 "merge of %s cannot proceed", branch_name);
7912 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7913 struct got_commitable *ct = pe->data;
7914 const char *ct_path = ct->in_repo_path;
7916 while (ct_path[0] == '/')
7918 err = check_out_of_date(ct_path, ct->status,
7919 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7920 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7926 mcm_arg.worktree = worktree;
7927 mcm_arg.branch_name = branch_name;
7928 err = commit_worktree(new_commit_id, &commitable_paths,
7929 head_commit_id, branch_tip, worktree, author, committer, NULL,
7930 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7934 err = update_fileindex_after_commit(worktree, &commitable_paths,
7935 *new_commit_id, fileindex, have_staged_files);
7936 sync_err = sync_fileindex(fileindex, fileindex_path);
7937 if (sync_err && err == NULL)
7940 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7941 struct got_commitable *ct = pe->data;
7943 free_commitable(ct);
7945 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
7946 free(fileindex_path);
7950 const struct got_error *
7951 got_worktree_merge_complete(struct got_worktree *worktree,
7952 struct got_fileindex *fileindex, struct got_repository *repo)
7954 const struct got_error *err, *unlockerr, *sync_err;
7955 char *fileindex_path = NULL;
7957 err = delete_merge_refs(worktree, repo);
7961 err = get_fileindex_path(&fileindex_path, worktree);
7964 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7965 sync_err = sync_fileindex(fileindex, fileindex_path);
7966 if (sync_err && err == NULL)
7969 got_fileindex_free(fileindex);
7970 free(fileindex_path);
7971 unlockerr = lock_worktree(worktree, LOCK_SH);
7972 if (unlockerr && err == NULL)
7977 const struct got_error *
7978 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7979 struct got_repository *repo)
7981 const struct got_error *err;
7982 char *branch_refname = NULL;
7983 struct got_reference *branch_ref = NULL;
7987 err = get_merge_branch_ref_name(&branch_refname, worktree);
7990 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7991 free(branch_refname);
7993 if (err->code != GOT_ERR_NOT_REF)
8001 const struct got_error *got_worktree_merge_prepare(
8002 struct got_fileindex **fileindex, struct got_worktree *worktree,
8003 struct got_reference *branch, struct got_repository *repo)
8005 const struct got_error *err = NULL;
8006 char *fileindex_path = NULL;
8007 char *branch_refname = NULL, *commit_refname = NULL;
8008 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
8009 struct got_reference *commit_ref = NULL;
8010 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
8011 struct check_rebase_ok_arg ok_arg;
8015 err = lock_worktree(worktree, LOCK_EX);
8019 err = open_fileindex(fileindex, &fileindex_path, worktree);
8023 /* Preconditions are the same as for rebase. */
8024 ok_arg.worktree = worktree;
8026 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8031 err = get_merge_branch_ref_name(&branch_refname, worktree);
8035 err = get_merge_commit_ref_name(&commit_refname, worktree);
8039 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8044 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8048 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8049 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8053 err = got_ref_resolve(&branch_tip, repo, branch);
8057 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8060 err = got_ref_write(branch_ref, repo);
8064 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8067 err = got_ref_write(commit_ref, repo);
8072 free(branch_refname);
8073 free(commit_refname);
8074 free(fileindex_path);
8076 got_ref_close(branch_ref);
8078 got_ref_close(commit_ref);
8080 got_ref_close(wt_branch);
8081 free(wt_branch_tip);
8084 got_fileindex_free(*fileindex);
8087 lock_worktree(worktree, LOCK_SH);
8092 const struct got_error *
8093 got_worktree_merge_continue(char **branch_name,
8094 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8095 struct got_worktree *worktree, struct got_repository *repo)
8097 const struct got_error *err;
8098 char *commit_refname = NULL, *branch_refname = NULL;
8099 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8100 char *fileindex_path = NULL;
8101 int have_staged_files = 0;
8103 *branch_name = NULL;
8107 err = lock_worktree(worktree, LOCK_EX);
8111 err = open_fileindex(fileindex, &fileindex_path, worktree);
8115 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8116 &have_staged_files);
8117 if (err && err->code != GOT_ERR_CANCELLED)
8119 if (have_staged_files) {
8120 err = got_error(GOT_ERR_STAGED_PATHS);
8124 err = get_merge_branch_ref_name(&branch_refname, worktree);
8128 err = get_merge_commit_ref_name(&commit_refname, worktree);
8132 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8136 if (!got_ref_is_symbolic(branch_ref)) {
8137 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8138 "%s is not a symbolic reference",
8139 got_ref_get_name(branch_ref));
8142 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8143 if (*branch_name == NULL) {
8144 err = got_error_from_errno("strdup");
8148 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8152 err = got_ref_resolve(branch_tip, repo, commit_ref);
8156 free(commit_refname);
8157 free(branch_refname);
8158 free(fileindex_path);
8160 got_ref_close(commit_ref);
8162 got_ref_close(branch_ref);
8166 *branch_name = NULL;
8171 got_fileindex_free(*fileindex);
8174 lock_worktree(worktree, LOCK_SH);
8179 const struct got_error *
8180 got_worktree_merge_abort(struct got_worktree *worktree,
8181 struct got_fileindex *fileindex, struct got_repository *repo,
8182 got_worktree_checkout_cb progress_cb, void *progress_arg)
8184 const struct got_error *err, *unlockerr, *sync_err;
8185 struct got_object_id *commit_id = NULL;
8186 struct got_commit_object *commit = NULL;
8187 char *fileindex_path = NULL;
8188 struct revert_file_args rfa;
8189 struct got_object_id *tree_id = NULL;
8191 err = got_object_open_as_commit(&commit, repo,
8192 worktree->base_commit_id);
8196 err = got_object_id_by_path(&tree_id, repo, commit,
8197 worktree->path_prefix);
8201 err = delete_merge_refs(worktree, repo);
8205 err = get_fileindex_path(&fileindex_path, worktree);
8209 rfa.worktree = worktree;
8210 rfa.fileindex = fileindex;
8211 rfa.progress_cb = progress_cb;
8212 rfa.progress_arg = progress_arg;
8213 rfa.patch_cb = NULL;
8214 rfa.patch_arg = NULL;
8216 rfa.unlink_added_files = 1;
8217 err = worktree_status(worktree, "", fileindex, repo,
8218 revert_file, &rfa, NULL, NULL, 1, 0);
8222 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8223 repo, progress_cb, progress_arg, NULL, NULL);
8225 sync_err = sync_fileindex(fileindex, fileindex_path);
8226 if (sync_err && err == NULL)
8232 got_object_commit_close(commit);
8234 got_fileindex_free(fileindex);
8235 free(fileindex_path);
8237 unlockerr = lock_worktree(worktree, LOCK_SH);
8238 if (unlockerr && err == NULL)
8243 struct check_stage_ok_arg {
8244 struct got_object_id *head_commit_id;
8245 struct got_worktree *worktree;
8246 struct got_fileindex *fileindex;
8247 struct got_repository *repo;
8251 static const struct got_error *
8252 check_stage_ok(void *arg, unsigned char status,
8253 unsigned char staged_status, const char *relpath,
8254 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8255 struct got_object_id *commit_id, int dirfd, const char *de_name)
8257 struct check_stage_ok_arg *a = arg;
8258 const struct got_error *err = NULL;
8259 struct got_fileindex_entry *ie;
8260 struct got_object_id base_commit_id;
8261 struct got_object_id *base_commit_idp = NULL;
8262 char *in_repo_path = NULL, *p;
8264 if (status == GOT_STATUS_UNVERSIONED ||
8265 status == GOT_STATUS_NO_CHANGE)
8267 if (status == GOT_STATUS_NONEXISTENT)
8268 return got_error_set_errno(ENOENT, relpath);
8270 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8272 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8274 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8275 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8277 return got_error_from_errno("asprintf");
8279 if (got_fileindex_entry_has_commit(ie)) {
8280 base_commit_idp = got_fileindex_entry_get_commit_id(
8281 &base_commit_id, ie);
8284 if (status == GOT_STATUS_CONFLICT) {
8285 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8287 } else if (status != GOT_STATUS_ADD &&
8288 status != GOT_STATUS_MODIFY &&
8289 status != GOT_STATUS_DELETE) {
8290 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8294 a->have_changes = 1;
8299 err = check_out_of_date(p, status, staged_status,
8300 blob_id, base_commit_idp, a->head_commit_id, a->repo,
8301 GOT_ERR_STAGE_OUT_OF_DATE);
8307 struct stage_path_arg {
8308 struct got_worktree *worktree;
8309 struct got_fileindex *fileindex;
8310 struct got_repository *repo;
8311 got_worktree_status_cb status_cb;
8313 got_worktree_patch_cb patch_cb;
8315 int staged_something;
8316 int allow_bad_symlinks;
8319 static const struct got_error *
8320 stage_path(void *arg, unsigned char status,
8321 unsigned char staged_status, const char *relpath,
8322 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8323 struct got_object_id *commit_id, int dirfd, const char *de_name)
8325 struct stage_path_arg *a = arg;
8326 const struct got_error *err = NULL;
8327 struct got_fileindex_entry *ie;
8328 char *ondisk_path = NULL, *path_content = NULL;
8330 struct got_object_id *new_staged_blob_id = NULL;
8333 if (status == GOT_STATUS_UNVERSIONED)
8336 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8338 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8340 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8342 return got_error_from_errno("asprintf");
8345 case GOT_STATUS_ADD:
8346 case GOT_STATUS_MODIFY:
8347 /* XXX could sb.st_mode be passed in by our caller? */
8348 if (lstat(ondisk_path, &sb) == -1) {
8349 err = got_error_from_errno2("lstat", ondisk_path);
8353 if (status == GOT_STATUS_ADD) {
8354 int choice = GOT_PATCH_CHOICE_NONE;
8355 err = (*a->patch_cb)(&choice, a->patch_arg,
8356 status, ie->path, NULL, 1, 1);
8359 if (choice != GOT_PATCH_CHOICE_YES)
8362 err = create_patched_content(&path_content, 0,
8363 staged_blob_id ? staged_blob_id : blob_id,
8364 ondisk_path, dirfd, de_name, ie->path,
8365 a->repo, a->patch_cb, a->patch_arg);
8366 if (err || path_content == NULL)
8370 err = got_object_blob_create(&new_staged_blob_id,
8371 path_content ? path_content : ondisk_path, a->repo);
8374 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8375 SHA1_DIGEST_LENGTH);
8376 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8377 stage = GOT_FILEIDX_STAGE_ADD;
8379 stage = GOT_FILEIDX_STAGE_MODIFY;
8380 got_fileindex_entry_stage_set(ie, stage);
8381 if (S_ISLNK(sb.st_mode)) {
8382 int is_bad_symlink = 0;
8383 if (!a->allow_bad_symlinks) {
8384 char target_path[PATH_MAX];
8386 target_len = readlink(ondisk_path, target_path,
8387 sizeof(target_path));
8388 if (target_len == -1) {
8389 err = got_error_from_errno2("readlink",
8393 err = is_bad_symlink_target(&is_bad_symlink,
8394 target_path, target_len, ondisk_path,
8395 a->worktree->root_path);
8398 if (is_bad_symlink) {
8399 err = got_error_path(ondisk_path,
8400 GOT_ERR_BAD_SYMLINK);
8405 got_fileindex_entry_staged_filetype_set(ie,
8406 GOT_FILEIDX_MODE_BAD_SYMLINK);
8408 got_fileindex_entry_staged_filetype_set(ie,
8409 GOT_FILEIDX_MODE_SYMLINK);
8411 got_fileindex_entry_staged_filetype_set(ie,
8412 GOT_FILEIDX_MODE_REGULAR_FILE);
8414 a->staged_something = 1;
8415 if (a->status_cb == NULL)
8417 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8418 get_staged_status(ie), relpath, blob_id,
8419 new_staged_blob_id, NULL, dirfd, de_name);
8423 * When staging the reverse of the staged diff,
8424 * implicitly unstage the file.
8426 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8427 sizeof(ie->blob_sha1)) == 0) {
8428 got_fileindex_entry_stage_set(ie,
8429 GOT_FILEIDX_STAGE_NONE);
8432 case GOT_STATUS_DELETE:
8433 if (staged_status == GOT_STATUS_DELETE)
8436 int choice = GOT_PATCH_CHOICE_NONE;
8437 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8438 ie->path, NULL, 1, 1);
8441 if (choice == GOT_PATCH_CHOICE_NO)
8443 if (choice != GOT_PATCH_CHOICE_YES) {
8444 err = got_error(GOT_ERR_PATCH_CHOICE);
8448 stage = GOT_FILEIDX_STAGE_DELETE;
8449 got_fileindex_entry_stage_set(ie, stage);
8450 a->staged_something = 1;
8451 if (a->status_cb == NULL)
8453 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8454 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8457 case GOT_STATUS_NO_CHANGE:
8459 case GOT_STATUS_CONFLICT:
8460 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8462 case GOT_STATUS_NONEXISTENT:
8463 err = got_error_set_errno(ENOENT, relpath);
8466 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8470 if (path_content && unlink(path_content) == -1 && err == NULL)
8471 err = got_error_from_errno2("unlink", path_content);
8474 free(new_staged_blob_id);
8478 const struct got_error *
8479 got_worktree_stage(struct got_worktree *worktree,
8480 struct got_pathlist_head *paths,
8481 got_worktree_status_cb status_cb, void *status_arg,
8482 got_worktree_patch_cb patch_cb, void *patch_arg,
8483 int allow_bad_symlinks, struct got_repository *repo)
8485 const struct got_error *err = NULL, *sync_err, *unlockerr;
8486 struct got_pathlist_entry *pe;
8487 struct got_fileindex *fileindex = NULL;
8488 char *fileindex_path = NULL;
8489 struct got_reference *head_ref = NULL;
8490 struct got_object_id *head_commit_id = NULL;
8491 struct check_stage_ok_arg oka;
8492 struct stage_path_arg spa;
8494 err = lock_worktree(worktree, LOCK_EX);
8498 err = got_ref_open(&head_ref, repo,
8499 got_worktree_get_head_ref_name(worktree), 0);
8502 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8505 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8509 /* Check pre-conditions before staging anything. */
8510 oka.head_commit_id = head_commit_id;
8511 oka.worktree = worktree;
8512 oka.fileindex = fileindex;
8514 oka.have_changes = 0;
8515 TAILQ_FOREACH(pe, paths, entry) {
8516 err = worktree_status(worktree, pe->path, fileindex, repo,
8517 check_stage_ok, &oka, NULL, NULL, 1, 0);
8521 if (!oka.have_changes) {
8522 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8526 spa.worktree = worktree;
8527 spa.fileindex = fileindex;
8529 spa.patch_cb = patch_cb;
8530 spa.patch_arg = patch_arg;
8531 spa.status_cb = status_cb;
8532 spa.status_arg = status_arg;
8533 spa.staged_something = 0;
8534 spa.allow_bad_symlinks = allow_bad_symlinks;
8535 TAILQ_FOREACH(pe, paths, entry) {
8536 err = worktree_status(worktree, pe->path, fileindex, repo,
8537 stage_path, &spa, NULL, NULL, 1, 0);
8541 if (!spa.staged_something) {
8542 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8546 sync_err = sync_fileindex(fileindex, fileindex_path);
8547 if (sync_err && err == NULL)
8551 got_ref_close(head_ref);
8552 free(head_commit_id);
8553 free(fileindex_path);
8555 got_fileindex_free(fileindex);
8556 unlockerr = lock_worktree(worktree, LOCK_SH);
8557 if (unlockerr && err == NULL)
8562 struct unstage_path_arg {
8563 struct got_worktree *worktree;
8564 struct got_fileindex *fileindex;
8565 struct got_repository *repo;
8566 got_worktree_checkout_cb progress_cb;
8568 got_worktree_patch_cb patch_cb;
8572 static const struct got_error *
8573 create_unstaged_content(char **path_unstaged_content,
8574 char **path_new_staged_content, struct got_object_id *blob_id,
8575 struct got_object_id *staged_blob_id, const char *relpath,
8576 struct got_repository *repo,
8577 got_worktree_patch_cb patch_cb, void *patch_arg)
8579 const struct got_error *err, *free_err;
8580 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8581 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8582 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8583 struct got_diffreg_result *diffreg_result = NULL;
8584 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8585 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8586 int fd1 = -1, fd2 = -1;
8588 *path_unstaged_content = NULL;
8589 *path_new_staged_content = NULL;
8591 err = got_object_id_str(&label1, blob_id);
8595 fd1 = got_opentempfd();
8597 err = got_error_from_errno("got_opentempfd");
8600 fd2 = got_opentempfd();
8602 err = got_error_from_errno("got_opentempfd");
8606 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8610 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
8614 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8618 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8623 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
8627 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8631 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8632 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8636 err = got_opentemp_named(path_unstaged_content, &outfile,
8637 "got-unstaged-content", "");
8640 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8641 "got-new-staged-content", "");
8645 if (fseek(f1, 0L, SEEK_SET) == -1) {
8646 err = got_ferror(f1, GOT_ERR_IO);
8649 if (fseek(f2, 0L, SEEK_SET) == -1) {
8650 err = got_ferror(f2, GOT_ERR_IO);
8653 /* Count the number of actual changes in the diff result. */
8654 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8655 struct diff_chunk_context cc = {};
8656 diff_chunk_context_load_change(&cc, &nchunks_used,
8657 diffreg_result->result, n, 0);
8660 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8662 err = apply_or_reject_change(&choice, &nchunks_used,
8663 diffreg_result->result, n, relpath, f1, f2,
8664 &line_cur1, &line_cur2,
8665 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8668 if (choice == GOT_PATCH_CHOICE_YES)
8671 have_rejected_content = 1;
8672 if (choice == GOT_PATCH_CHOICE_QUIT)
8675 if (have_content || have_rejected_content)
8676 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8677 outfile, rejectfile);
8680 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8681 err = got_error_from_errno("close");
8683 got_object_blob_close(blob);
8684 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8685 err = got_error_from_errno("close");
8687 got_object_blob_close(staged_blob);
8688 free_err = got_diffreg_result_free(diffreg_result);
8689 if (free_err && err == NULL)
8691 if (f1 && fclose(f1) == EOF && err == NULL)
8692 err = got_error_from_errno2("fclose", path1);
8693 if (f2 && fclose(f2) == EOF && err == NULL)
8694 err = got_error_from_errno2("fclose", path2);
8695 if (outfile && fclose(outfile) == EOF && err == NULL)
8696 err = got_error_from_errno2("fclose", *path_unstaged_content);
8697 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8698 err = got_error_from_errno2("fclose", *path_new_staged_content);
8699 if (path1 && unlink(path1) == -1 && err == NULL)
8700 err = got_error_from_errno2("unlink", path1);
8701 if (path2 && unlink(path2) == -1 && err == NULL)
8702 err = got_error_from_errno2("unlink", path2);
8703 if (err || !have_content) {
8704 if (*path_unstaged_content &&
8705 unlink(*path_unstaged_content) == -1 && err == NULL)
8706 err = got_error_from_errno2("unlink",
8707 *path_unstaged_content);
8708 free(*path_unstaged_content);
8709 *path_unstaged_content = NULL;
8711 if (err || !have_content || !have_rejected_content) {
8712 if (*path_new_staged_content &&
8713 unlink(*path_new_staged_content) == -1 && err == NULL)
8714 err = got_error_from_errno2("unlink",
8715 *path_new_staged_content);
8716 free(*path_new_staged_content);
8717 *path_new_staged_content = NULL;
8724 static const struct got_error *
8725 unstage_hunks(struct got_object_id *staged_blob_id,
8726 struct got_blob_object *blob_base,
8727 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8728 const char *ondisk_path, const char *label_orig,
8729 struct got_worktree *worktree, struct got_repository *repo,
8730 got_worktree_patch_cb patch_cb, void *patch_arg,
8731 got_worktree_checkout_cb progress_cb, void *progress_arg)
8733 const struct got_error *err = NULL;
8734 char *path_unstaged_content = NULL;
8735 char *path_new_staged_content = NULL;
8736 char *parent = NULL, *base_path = NULL;
8737 char *blob_base_path = NULL;
8738 struct got_object_id *new_staged_blob_id = NULL;
8739 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8742 err = create_unstaged_content(&path_unstaged_content,
8743 &path_new_staged_content, blob_id, staged_blob_id,
8744 ie->path, repo, patch_cb, patch_arg);
8748 if (path_unstaged_content == NULL)
8751 if (path_new_staged_content) {
8752 err = got_object_blob_create(&new_staged_blob_id,
8753 path_new_staged_content, repo);
8758 f = fopen(path_unstaged_content, "re");
8760 err = got_error_from_errno2("fopen",
8761 path_unstaged_content);
8764 if (fstat(fileno(f), &sb) == -1) {
8765 err = got_error_from_errno2("fstat", path_unstaged_content);
8768 if (got_fileindex_entry_staged_filetype_get(ie) ==
8769 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8770 char link_target[PATH_MAX];
8772 r = fread(link_target, 1, sizeof(link_target), f);
8773 if (r == 0 && ferror(f)) {
8774 err = got_error_from_errno("fread");
8777 if (r >= sizeof(link_target)) { /* should not happen */
8778 err = got_error(GOT_ERR_NO_SPACE);
8781 link_target[r] = '\0';
8782 err = merge_symlink(worktree, blob_base,
8783 ondisk_path, ie->path, label_orig, link_target,
8784 worktree->base_commit_id, repo, progress_cb,
8787 int local_changes_subsumed;
8789 err = got_path_dirname(&parent, ondisk_path);
8793 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8795 err = got_error_from_errno("asprintf");
8800 err = got_opentemp_named(&blob_base_path, &f_base,
8804 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8810 * In order the run a 3-way merge with a symlink we copy the symlink's
8811 * target path into a temporary file and use that file with diff3.
8813 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8814 err = dump_symlink_target_path_to_file(&f_deriv2,
8820 fd = open(ondisk_path,
8821 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8823 err = got_error_from_errno2("open", ondisk_path);
8826 f_deriv2 = fdopen(fd, "r");
8827 if (f_deriv2 == NULL) {
8828 err = got_error_from_errno2("fdopen", ondisk_path);
8834 err = merge_file(&local_changes_subsumed, worktree,
8835 f_base, f, f_deriv2, ondisk_path, ie->path,
8836 got_fileindex_perms_to_st(ie),
8837 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8838 repo, progress_cb, progress_arg);
8843 if (new_staged_blob_id) {
8844 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8845 SHA1_DIGEST_LENGTH);
8847 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8848 got_fileindex_entry_staged_filetype_set(ie, 0);
8851 free(new_staged_blob_id);
8852 if (path_unstaged_content &&
8853 unlink(path_unstaged_content) == -1 && err == NULL)
8854 err = got_error_from_errno2("unlink", path_unstaged_content);
8855 if (path_new_staged_content &&
8856 unlink(path_new_staged_content) == -1 && err == NULL)
8857 err = got_error_from_errno2("unlink", path_new_staged_content);
8858 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8859 err = got_error_from_errno2("unlink", blob_base_path);
8860 if (f_base && fclose(f_base) == EOF && err == NULL)
8861 err = got_error_from_errno2("fclose", path_unstaged_content);
8862 if (f && fclose(f) == EOF && err == NULL)
8863 err = got_error_from_errno2("fclose", path_unstaged_content);
8864 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8865 err = got_error_from_errno2("fclose", ondisk_path);
8866 free(path_unstaged_content);
8867 free(path_new_staged_content);
8868 free(blob_base_path);
8874 static const struct got_error *
8875 unstage_path(void *arg, unsigned char status,
8876 unsigned char staged_status, const char *relpath,
8877 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8878 struct got_object_id *commit_id, int dirfd, const char *de_name)
8880 const struct got_error *err = NULL;
8881 struct unstage_path_arg *a = arg;
8882 struct got_fileindex_entry *ie;
8883 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8884 char *ondisk_path = NULL;
8885 char *id_str = NULL, *label_orig = NULL;
8886 int local_changes_subsumed;
8888 int fd1 = -1, fd2 = -1;
8890 if (staged_status != GOT_STATUS_ADD &&
8891 staged_status != GOT_STATUS_MODIFY &&
8892 staged_status != GOT_STATUS_DELETE)
8895 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8897 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8899 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8901 return got_error_from_errno("asprintf");
8903 err = got_object_id_str(&id_str,
8904 commit_id ? commit_id : a->worktree->base_commit_id);
8907 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8909 err = got_error_from_errno("asprintf");
8913 fd1 = got_opentempfd();
8915 err = got_error_from_errno("got_opentempfd");
8918 fd2 = got_opentempfd();
8920 err = got_error_from_errno("got_opentempfd");
8924 switch (staged_status) {
8925 case GOT_STATUS_MODIFY:
8926 err = got_object_open_as_blob(&blob_base, a->repo,
8927 blob_id, 8192, fd1);
8931 case GOT_STATUS_ADD:
8933 if (staged_status == GOT_STATUS_ADD) {
8934 int choice = GOT_PATCH_CHOICE_NONE;
8935 err = (*a->patch_cb)(&choice, a->patch_arg,
8936 staged_status, ie->path, NULL, 1, 1);
8939 if (choice != GOT_PATCH_CHOICE_YES)
8942 err = unstage_hunks(staged_blob_id,
8943 blob_base, blob_id, ie, ondisk_path,
8944 label_orig, a->worktree, a->repo,
8945 a->patch_cb, a->patch_arg,
8946 a->progress_cb, a->progress_arg);
8947 break; /* Done with this file. */
8950 err = got_object_open_as_blob(&blob_staged, a->repo,
8951 staged_blob_id, 8192, fd2);
8954 switch (got_fileindex_entry_staged_filetype_get(ie)) {
8955 case GOT_FILEIDX_MODE_BAD_SYMLINK:
8956 case GOT_FILEIDX_MODE_REGULAR_FILE:
8957 err = merge_blob(&local_changes_subsumed, a->worktree,
8958 blob_base, ondisk_path, relpath,
8959 got_fileindex_perms_to_st(ie), label_orig,
8960 blob_staged, commit_id ? commit_id :
8961 a->worktree->base_commit_id, a->repo,
8962 a->progress_cb, a->progress_arg);
8964 case GOT_FILEIDX_MODE_SYMLINK:
8965 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8966 char *staged_target;
8967 err = got_object_blob_read_to_str(
8968 &staged_target, blob_staged);
8971 err = merge_symlink(a->worktree, blob_base,
8972 ondisk_path, relpath, label_orig,
8973 staged_target, commit_id ? commit_id :
8974 a->worktree->base_commit_id,
8975 a->repo, a->progress_cb, a->progress_arg);
8976 free(staged_target);
8978 err = merge_blob(&local_changes_subsumed,
8979 a->worktree, blob_base, ondisk_path,
8980 relpath, got_fileindex_perms_to_st(ie),
8981 label_orig, blob_staged,
8982 commit_id ? commit_id :
8983 a->worktree->base_commit_id, a->repo,
8984 a->progress_cb, a->progress_arg);
8988 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8992 got_fileindex_entry_stage_set(ie,
8993 GOT_FILEIDX_STAGE_NONE);
8994 got_fileindex_entry_staged_filetype_set(ie, 0);
8997 case GOT_STATUS_DELETE:
8999 int choice = GOT_PATCH_CHOICE_NONE;
9000 err = (*a->patch_cb)(&choice, a->patch_arg,
9001 staged_status, ie->path, NULL, 1, 1);
9004 if (choice == GOT_PATCH_CHOICE_NO)
9006 if (choice != GOT_PATCH_CHOICE_YES) {
9007 err = got_error(GOT_ERR_PATCH_CHOICE);
9011 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9012 got_fileindex_entry_staged_filetype_set(ie, 0);
9013 err = get_file_status(&status, &sb, ie, ondisk_path,
9014 dirfd, de_name, a->repo);
9017 err = (*a->progress_cb)(a->progress_arg, status, relpath);
9022 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9023 err = got_error_from_errno("close");
9025 got_object_blob_close(blob_base);
9026 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9027 err = got_error_from_errno("close");
9029 got_object_blob_close(blob_staged);
9035 const struct got_error *
9036 got_worktree_unstage(struct got_worktree *worktree,
9037 struct got_pathlist_head *paths,
9038 got_worktree_checkout_cb progress_cb, void *progress_arg,
9039 got_worktree_patch_cb patch_cb, void *patch_arg,
9040 struct got_repository *repo)
9042 const struct got_error *err = NULL, *sync_err, *unlockerr;
9043 struct got_pathlist_entry *pe;
9044 struct got_fileindex *fileindex = NULL;
9045 char *fileindex_path = NULL;
9046 struct unstage_path_arg upa;
9048 err = lock_worktree(worktree, LOCK_EX);
9052 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9056 upa.worktree = worktree;
9057 upa.fileindex = fileindex;
9059 upa.progress_cb = progress_cb;
9060 upa.progress_arg = progress_arg;
9061 upa.patch_cb = patch_cb;
9062 upa.patch_arg = patch_arg;
9063 TAILQ_FOREACH(pe, paths, entry) {
9064 err = worktree_status(worktree, pe->path, fileindex, repo,
9065 unstage_path, &upa, NULL, NULL, 1, 0);
9070 sync_err = sync_fileindex(fileindex, fileindex_path);
9071 if (sync_err && err == NULL)
9074 free(fileindex_path);
9076 got_fileindex_free(fileindex);
9077 unlockerr = lock_worktree(worktree, LOCK_SH);
9078 if (unlockerr && err == NULL)
9083 struct report_file_info_arg {
9084 struct got_worktree *worktree;
9085 got_worktree_path_info_cb info_cb;
9087 struct got_pathlist_head *paths;
9088 got_cancel_cb cancel_cb;
9092 static const struct got_error *
9093 report_file_info(void *arg, struct got_fileindex_entry *ie)
9095 struct report_file_info_arg *a = arg;
9096 struct got_pathlist_entry *pe;
9097 struct got_object_id blob_id, staged_blob_id, commit_id;
9098 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9099 struct got_object_id *commit_idp = NULL;
9102 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9103 return got_error(GOT_ERR_CANCELLED);
9105 TAILQ_FOREACH(pe, a->paths, entry) {
9106 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9107 got_path_is_child(ie->path, pe->path, pe->path_len))
9110 if (pe == NULL) /* not found */
9113 if (got_fileindex_entry_has_blob(ie))
9114 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9115 stage = got_fileindex_entry_stage_get(ie);
9116 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9117 stage == GOT_FILEIDX_STAGE_ADD) {
9118 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9119 &staged_blob_id, ie);
9122 if (got_fileindex_entry_has_commit(ie))
9123 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9125 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9126 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9129 const struct got_error *
9130 got_worktree_path_info(struct got_worktree *worktree,
9131 struct got_pathlist_head *paths,
9132 got_worktree_path_info_cb info_cb, void *info_arg,
9133 got_cancel_cb cancel_cb, void *cancel_arg)
9136 const struct got_error *err = NULL, *unlockerr;
9137 struct got_fileindex *fileindex = NULL;
9138 char *fileindex_path = NULL;
9139 struct report_file_info_arg arg;
9141 err = lock_worktree(worktree, LOCK_SH);
9145 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9149 arg.worktree = worktree;
9150 arg.info_cb = info_cb;
9151 arg.info_arg = info_arg;
9153 arg.cancel_cb = cancel_cb;
9154 arg.cancel_arg = cancel_arg;
9155 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9158 free(fileindex_path);
9160 got_fileindex_free(fileindex);
9161 unlockerr = lock_worktree(worktree, LOCK_UN);
9162 if (unlockerr && err == NULL)
9167 static const struct got_error *
9168 patch_check_path(const char *p, char **path, unsigned char *status,
9169 unsigned char *staged_status, struct got_fileindex *fileindex,
9170 struct got_worktree *worktree, struct got_repository *repo)
9172 const struct got_error *err;
9173 struct got_fileindex_entry *ie;
9175 char *ondisk_path = NULL;
9177 err = got_worktree_resolve_path(path, worktree, p);
9181 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9182 *path[0] ? "/" : "", *path) == -1)
9183 return got_error_from_errno("asprintf");
9185 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9187 *staged_status = get_staged_status(ie);
9188 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9193 *staged_status = GOT_STATUS_NO_CHANGE;
9194 *status = GOT_STATUS_UNVERSIONED;
9195 if (lstat(ondisk_path, &sb) == -1) {
9196 if (errno != ENOENT) {
9197 err = got_error_from_errno2("lstat",
9201 *status = GOT_STATUS_NONEXISTENT;
9210 static const struct got_error *
9211 patch_can_rm(const char *path, unsigned char status,
9212 unsigned char staged_status)
9214 if (status == GOT_STATUS_NONEXISTENT)
9215 return got_error_set_errno(ENOENT, path);
9216 if (status != GOT_STATUS_NO_CHANGE &&
9217 status != GOT_STATUS_ADD &&
9218 status != GOT_STATUS_MODIFY &&
9219 status != GOT_STATUS_MODE_CHANGE)
9220 return got_error_path(path, GOT_ERR_FILE_STATUS);
9221 if (staged_status == GOT_STATUS_DELETE)
9222 return got_error_path(path, GOT_ERR_FILE_STATUS);
9226 static const struct got_error *
9227 patch_can_add(const char *path, unsigned char status)
9229 if (status != GOT_STATUS_NONEXISTENT)
9230 return got_error_path(path, GOT_ERR_FILE_STATUS);
9234 static const struct got_error *
9235 patch_can_edit(const char *path, unsigned char status,
9236 unsigned char staged_status)
9238 if (status == GOT_STATUS_NONEXISTENT)
9239 return got_error_set_errno(ENOENT, path);
9240 if (status != GOT_STATUS_NO_CHANGE &&
9241 status != GOT_STATUS_ADD &&
9242 status != GOT_STATUS_MODIFY)
9243 return got_error_path(path, GOT_ERR_FILE_STATUS);
9244 if (staged_status == GOT_STATUS_DELETE)
9245 return got_error_path(path, GOT_ERR_FILE_STATUS);
9249 const struct got_error *
9250 got_worktree_patch_prepare(struct got_fileindex **fileindex,
9251 char **fileindex_path, struct got_worktree *worktree)
9253 return open_fileindex(fileindex, fileindex_path, worktree);
9256 const struct got_error *
9257 got_worktree_patch_check_path(const char *old, const char *new,
9258 char **oldpath, char **newpath, struct got_worktree *worktree,
9259 struct got_repository *repo, struct got_fileindex *fileindex)
9261 const struct got_error *err = NULL;
9262 int file_renamed = 0;
9263 unsigned char status_old, staged_status_old;
9264 unsigned char status_new, staged_status_new;
9269 err = patch_check_path(old != NULL ? old : new, oldpath,
9270 &status_old, &staged_status_old, fileindex, worktree, repo);
9274 err = patch_check_path(new != NULL ? new : old, newpath,
9275 &status_new, &staged_status_new, fileindex, worktree, repo);
9279 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9282 if (old != NULL && new == NULL)
9283 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9284 else if (file_renamed) {
9285 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9287 err = patch_can_add(*newpath, status_new);
9288 } else if (old == NULL)
9289 err = patch_can_add(*newpath, status_new);
9291 err = patch_can_edit(*newpath, status_new, staged_status_new);
9303 const struct got_error *
9304 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9305 struct got_worktree *worktree, struct got_fileindex *fileindex,
9306 got_worktree_checkout_cb progress_cb, void *progress_arg)
9308 struct schedule_addition_args saa;
9310 memset(&saa, 0, sizeof(saa));
9311 saa.worktree = worktree;
9312 saa.fileindex = fileindex;
9313 saa.progress_cb = progress_cb;
9314 saa.progress_arg = progress_arg;
9317 return worktree_status(worktree, path, fileindex, repo,
9318 schedule_addition, &saa, NULL, NULL, 1, 0);
9321 const struct got_error *
9322 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9323 struct got_worktree *worktree, struct got_fileindex *fileindex,
9324 got_worktree_delete_cb progress_cb, void *progress_arg)
9326 struct schedule_deletion_args sda;
9328 memset(&sda, 0, sizeof(sda));
9329 sda.worktree = worktree;
9330 sda.fileindex = fileindex;
9331 sda.progress_cb = progress_cb;
9332 sda.progress_arg = progress_arg;
9334 sda.delete_local_mods = 0;
9335 sda.keep_on_disk = 0;
9336 sda.ignore_missing_paths = 0;
9337 sda.status_codes = NULL;
9339 return worktree_status(worktree, path, fileindex, repo,
9340 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9343 const struct got_error *
9344 got_worktree_patch_complete(struct got_fileindex *fileindex,
9345 const char *fileindex_path)
9347 const struct got_error *err = NULL;
9349 err = sync_fileindex(fileindex, fileindex_path);
9350 got_fileindex_free(fileindex);