2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/queue.h>
38 #include "got_error.h"
39 #include "got_repository.h"
40 #include "got_reference.h"
41 #include "got_object.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
45 #include "got_opentemp.h"
48 #include "got_lib_worktree.h"
49 #include "got_lib_sha1.h"
50 #include "got_lib_fileindex.h"
51 #include "got_lib_inflate.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_object_idset.h"
57 #include "got_lib_diff.h"
58 #include "got_lib_gotconfig.h"
61 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
64 #define GOT_MERGE_LABEL_MERGED "merged change"
65 #define GOT_MERGE_LABEL_BASE "3-way merge base"
67 static mode_t apply_umask(mode_t);
69 static const struct got_error *
70 create_meta_file(const char *path_got, const char *name, const char *content)
72 const struct got_error *err = NULL;
75 if (asprintf(&path, "%s/%s", path_got, name) == -1)
76 return got_error_from_errno("asprintf");
78 err = got_path_create_file(path, content);
83 static const struct got_error *
84 update_meta_file(const char *path_got, const char *name, const char *content)
86 const struct got_error *err = NULL;
91 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
92 err = got_error_from_errno("asprintf");
97 err = got_opentemp_named(&tmppath, &tmpfile, path);
102 int len = fprintf(tmpfile, "%s\n", content);
103 if (len != strlen(content) + 1) {
104 err = got_error_from_errno2("fprintf", tmppath);
109 if (rename(tmppath, path) != 0) {
110 err = got_error_from_errno3("rename", tmppath, path);
116 if (fclose(tmpfile) == EOF && err == NULL)
117 err = got_error_from_errno2("fclose", tmppath);
122 static const struct got_error *
123 write_head_ref(const char *path_got, struct got_reference *head_ref)
125 const struct got_error *err = NULL;
128 if (got_ref_is_symbolic(head_ref)) {
129 refstr = got_ref_to_str(head_ref);
131 return got_error_from_errno("got_ref_to_str");
133 refstr = strdup(got_ref_get_name(head_ref));
135 return got_error_from_errno("strdup");
137 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
142 const struct got_error *
143 got_worktree_init(const char *path, struct got_reference *head_ref,
144 const char *prefix, struct got_repository *repo)
146 const struct got_error *err = NULL;
147 struct got_object_id *commit_id = NULL;
149 uint32_t uuid_status;
151 char *path_got = NULL;
152 char *formatstr = NULL;
153 char *absprefix = NULL;
154 char *basestr = NULL;
155 char *uuidstr = NULL;
157 if (strcmp(path, got_repo_get_path(repo)) == 0) {
158 err = got_error(GOT_ERR_WORKTREE_REPO);
162 err = got_ref_resolve(&commit_id, repo, head_ref);
165 err = got_object_get_type(&obj_type, repo, commit_id);
168 if (obj_type != GOT_OBJ_TYPE_COMMIT)
169 return got_error(GOT_ERR_OBJ_TYPE);
171 if (!got_path_is_absolute(prefix)) {
172 if (asprintf(&absprefix, "/%s", prefix) == -1)
173 return got_error_from_errno("asprintf");
176 /* Create top-level directory (may already exist). */
177 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
178 err = got_error_from_errno2("mkdir", path);
182 /* Create .got directory (may already exist). */
183 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
184 err = got_error_from_errno("asprintf");
187 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
188 err = got_error_from_errno2("mkdir", path_got);
192 /* Create an empty lock file. */
193 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
197 /* Create an empty file index. */
198 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
202 /* Write the HEAD reference. */
203 err = write_head_ref(path_got, head_ref);
207 /* Record our base commit. */
208 err = got_object_id_str(&basestr, commit_id);
211 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
215 /* Store path to repository. */
216 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
217 got_repo_get_path(repo));
221 /* Store in-repository path prefix. */
222 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
223 absprefix ? absprefix : prefix);
228 uuid_create(&uuid, &uuid_status);
229 if (uuid_status != uuid_s_ok) {
230 err = got_error_uuid(uuid_status, "uuid_create");
233 uuid_to_string(&uuid, &uuidstr, &uuid_status);
234 if (uuid_status != uuid_s_ok) {
235 err = got_error_uuid(uuid_status, "uuid_to_string");
238 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
242 /* Stamp work tree with format file. */
243 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
244 err = got_error_from_errno("asprintf");
247 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
261 const struct got_error *
262 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
263 const char *path_prefix)
265 char *absprefix = NULL;
267 if (!got_path_is_absolute(path_prefix)) {
268 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
269 return got_error_from_errno("asprintf");
271 *match = (strcmp(absprefix ? absprefix : path_prefix,
272 worktree->path_prefix) == 0);
278 got_worktree_get_head_ref_name(struct got_worktree *worktree)
280 return worktree->head_ref_name;
283 const struct got_error *
284 got_worktree_set_head_ref(struct got_worktree *worktree,
285 struct got_reference *head_ref)
287 const struct got_error *err = NULL;
288 char *path_got = NULL, *head_ref_name = NULL;
290 if (asprintf(&path_got, "%s/%s", worktree->root_path,
291 GOT_WORKTREE_GOT_DIR) == -1) {
292 err = got_error_from_errno("asprintf");
297 head_ref_name = strdup(got_ref_get_name(head_ref));
298 if (head_ref_name == NULL) {
299 err = got_error_from_errno("strdup");
303 err = write_head_ref(path_got, head_ref);
307 free(worktree->head_ref_name);
308 worktree->head_ref_name = head_ref_name;
316 struct got_object_id *
317 got_worktree_get_base_commit_id(struct got_worktree *worktree)
319 return worktree->base_commit_id;
322 const struct got_error *
323 got_worktree_set_base_commit_id(struct got_worktree *worktree,
324 struct got_repository *repo, struct got_object_id *commit_id)
326 const struct got_error *err;
327 struct got_object *obj = NULL;
329 char *path_got = NULL;
331 if (asprintf(&path_got, "%s/%s", worktree->root_path,
332 GOT_WORKTREE_GOT_DIR) == -1) {
333 err = got_error_from_errno("asprintf");
338 err = got_object_open(&obj, repo, commit_id);
342 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
343 err = got_error(GOT_ERR_OBJ_TYPE);
347 /* Record our base commit. */
348 err = got_object_id_str(&id_str, commit_id);
351 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
355 free(worktree->base_commit_id);
356 worktree->base_commit_id = got_object_id_dup(commit_id);
357 if (worktree->base_commit_id == NULL) {
358 err = got_error_from_errno("got_object_id_dup");
363 got_object_close(obj);
369 const struct got_gotconfig *
370 got_worktree_get_gotconfig(struct got_worktree *worktree)
372 return worktree->gotconfig;
375 static const struct got_error *
376 lock_worktree(struct got_worktree *worktree, int operation)
378 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
379 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
380 : got_error_from_errno2("flock",
381 got_worktree_get_root_path(worktree)));
385 static const struct got_error *
386 add_dir_on_disk(struct got_worktree *worktree, const char *path)
388 const struct got_error *err = NULL;
391 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
392 return got_error_from_errno("asprintf");
394 err = got_path_mkdir(abspath);
395 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
398 if (lstat(abspath, &sb) == -1) {
399 err = got_error_from_errno2("lstat", abspath);
400 } else if (!S_ISDIR(sb.st_mode)) {
401 /* TODO directory is obstructed; do something */
402 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
409 static const struct got_error *
410 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
412 const struct got_error *err = NULL;
415 size_t flen1 = 0, flen2 = 0;
420 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
421 if (flen1 == 0 && ferror(f1)) {
422 err = got_error_from_errno("fread");
425 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
426 if (flen2 == 0 && ferror(f2)) {
427 err = got_error_from_errno("fread");
434 } else if (flen2 == 0) {
438 } else if (flen1 == flen2) {
439 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
452 static const struct got_error *
453 check_files_equal(int *same, FILE *f1, FILE *f2)
460 if (fstat(fileno(f1), &sb) != 0)
461 return got_error_from_errno("fstat");
464 if (fstat(fileno(f2), &sb) != 0)
465 return got_error_from_errno("fstat");
468 if (size1 != size2) {
473 if (fseek(f1, 0L, SEEK_SET) == -1)
474 return got_ferror(f1, GOT_ERR_IO);
475 if (fseek(f2, 0L, SEEK_SET) == -1)
476 return got_ferror(f2, GOT_ERR_IO);
478 return check_file_contents_equal(same, f1, f2);
481 static const struct got_error *
482 copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
490 if (fseek(f, 0L, SEEK_SET) == -1)
491 return got_ferror(f, GOT_ERR_IO);
494 flen = fread(fbuf, 1, sizeof(fbuf), f);
497 return got_error_from_errno("fread");
501 outlen = write(outfd, fbuf, flen);
503 return got_error_from_errno("write");
505 return got_error(GOT_ERR_IO);
512 static const struct got_error *
513 merge_binary_file(int *overlapcnt, int merged_fd,
514 FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
515 const char *label_deriv, const char *label_orig, const char *label_deriv2,
516 const char *ondisk_path)
518 const struct got_error *err = NULL;
519 int same_content, changed_deriv, changed_deriv2;
520 int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
521 off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
522 char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
523 char *base_path_orig = NULL, *base_path_deriv = NULL;
524 char *base_path_deriv2 = NULL;
528 err = check_files_equal(&same_content, f_deriv, f_deriv2);
533 return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
535 err = check_files_equal(&same_content, f_deriv, f_orig);
538 changed_deriv = !same_content;
539 err = check_files_equal(&same_content, f_deriv2, f_orig);
542 changed_deriv2 = !same_content;
544 if (changed_deriv && changed_deriv2) {
546 if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
547 err = got_error_from_errno("asprintf");
550 if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
551 err = got_error_from_errno("asprintf");
554 if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
555 err = got_error_from_errno("asprintf");
558 err = got_opentemp_named_fd(&path_orig, &fd_orig,
562 err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
566 err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
570 err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
573 err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
576 err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
579 if (dprintf(merged_fd, "Binary files differ and cannot be "
580 "merged automatically:\n") < 0) {
581 err = got_error_from_errno("dprintf");
584 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
585 GOT_DIFF_CONFLICT_MARKER_BEGIN,
586 label_deriv ? " " : "",
587 label_deriv ? label_deriv : "",
589 err = got_error_from_errno("dprintf");
593 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
594 GOT_DIFF_CONFLICT_MARKER_ORIG,
595 label_orig ? " " : "",
596 label_orig ? label_orig : "",
598 err = got_error_from_errno("dprintf");
602 if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
603 GOT_DIFF_CONFLICT_MARKER_SEP,
605 GOT_DIFF_CONFLICT_MARKER_END,
606 label_deriv2 ? " " : "",
607 label_deriv2 ? label_deriv2 : "") < 0) {
608 err = got_error_from_errno("dprintf");
611 } else if (changed_deriv)
612 err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
613 else if (changed_deriv2)
614 err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
616 if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
618 err = got_error_from_errno2("unlink", path_orig);
619 if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
620 err = got_error_from_errno2("close", path_orig);
621 if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
622 err = got_error_from_errno2("close", path_deriv);
623 if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
624 err = got_error_from_errno2("close", path_deriv2);
628 free(base_path_orig);
629 free(base_path_deriv);
630 free(base_path_deriv2);
635 * Perform a 3-way merge where the file f_orig acts as the common
636 * ancestor, the file f_deriv acts as the first derived version,
637 * and the file f_deriv2 acts as the second derived version.
638 * The merge result will be written to a new file at ondisk_path; any
639 * existing file at this path will be replaced.
641 static const struct got_error *
642 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
643 FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
644 const char *path, uint16_t st_mode,
645 const char *label_orig, const char *label_deriv, const char *label_deriv2,
646 enum got_diff_algorithm diff_algo, struct got_repository *repo,
647 got_worktree_checkout_cb progress_cb, void *progress_arg)
649 const struct got_error *err = NULL;
651 FILE *f_merged = NULL;
652 char *merged_path = NULL, *base_path = NULL;
656 *local_changes_subsumed = 0;
658 err = got_path_dirname(&parent, ondisk_path);
662 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
663 err = got_error_from_errno("asprintf");
667 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
671 err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
672 f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
674 if (err->code != GOT_ERR_FILE_BINARY)
676 err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
677 f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
683 err = (*progress_cb)(progress_arg,
684 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
688 if (fsync(merged_fd) != 0) {
689 err = got_error_from_errno("fsync");
693 f_merged = fdopen(merged_fd, "r");
694 if (f_merged == NULL) {
695 err = got_error_from_errno("fdopen");
700 /* Check if a clean merge has subsumed all local changes. */
701 if (overlapcnt == 0) {
702 err = check_files_equal(local_changes_subsumed, f_deriv,
708 if (fchmod(fileno(f_merged), apply_umask(st_mode)) != 0) {
709 err = got_error_from_errno2("fchmod", merged_path);
713 if (rename(merged_path, ondisk_path) != 0) {
714 err = got_error_from_errno3("rename", merged_path,
723 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
724 err = got_error_from_errno("close");
725 if (f_merged && fclose(f_merged) == EOF && err == NULL)
726 err = got_error_from_errno("fclose");
733 static const struct got_error *
734 update_symlink(const char *ondisk_path, const char *target_path,
737 /* This is not atomic but matches what 'ln -sf' does. */
738 if (unlink(ondisk_path) == -1)
739 return got_error_from_errno2("unlink", ondisk_path);
740 if (symlink(target_path, ondisk_path) == -1)
741 return got_error_from_errno3("symlink", target_path,
747 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
748 * in the work tree with a file that contains conflict markers and the
749 * conflicting target paths of the original version, a "derived version"
750 * of a symlink from an incoming change, and a local version of the symlink.
752 * The original versions's target path can be NULL if it is not available,
753 * such as if both derived versions added a new symlink at the same path.
755 * The incoming derived symlink target is NULL in case the incoming change
756 * has deleted this symlink.
758 static const struct got_error *
759 install_symlink_conflict(const char *deriv_target,
760 struct got_object_id *deriv_base_commit_id, const char *orig_target,
761 const char *label_orig, const char *local_target, const char *ondisk_path)
763 const struct got_error *err;
764 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
767 err = got_object_id_str(&id_str, deriv_base_commit_id);
769 return got_error_from_errno("asprintf");
771 if (asprintf(&label_deriv, "%s: commit %s",
772 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
773 err = got_error_from_errno("asprintf");
777 err = got_opentemp_named(&path, &f, "got-symlink-conflict");
781 if (fchmod(fileno(f), apply_umask(GOT_DEFAULT_FILE_MODE)) == -1) {
782 err = got_error_from_errno2("fchmod", path);
786 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
787 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
788 deriv_target ? deriv_target : "(symlink was deleted)",
789 orig_target ? label_orig : "",
790 orig_target ? "\n" : "",
791 orig_target ? orig_target : "",
792 orig_target ? "\n" : "",
793 GOT_DIFF_CONFLICT_MARKER_SEP,
794 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
795 err = got_error_from_errno2("fprintf", path);
799 if (unlink(ondisk_path) == -1) {
800 err = got_error_from_errno2("unlink", ondisk_path);
803 if (rename(path, ondisk_path) == -1) {
804 err = got_error_from_errno3("rename", path, ondisk_path);
808 if (f != NULL && fclose(f) == EOF && err == NULL)
809 err = got_error_from_errno2("fclose", path);
816 /* forward declaration */
817 static const struct got_error *
818 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
819 const char *, const char *, uint16_t, const char *,
820 struct got_blob_object *, struct got_object_id *,
821 struct got_repository *, got_worktree_checkout_cb, void *);
824 * Merge a symlink into the work tree, where blob_orig acts as the common
825 * ancestor, deriv_target is the link target of the first derived version,
826 * and the symlink on disk acts as the second derived version.
827 * Assume that contents of both blobs represent symlinks.
829 static const struct got_error *
830 merge_symlink(struct got_worktree *worktree,
831 struct got_blob_object *blob_orig, const char *ondisk_path,
832 const char *path, const char *label_orig, const char *deriv_target,
833 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
834 got_worktree_checkout_cb progress_cb, void *progress_arg)
836 const struct got_error *err = NULL;
837 char *ancestor_target = NULL;
839 ssize_t ondisk_len, deriv_len;
840 char ondisk_target[PATH_MAX];
841 int have_local_change = 0;
842 int have_incoming_change = 0;
844 if (lstat(ondisk_path, &sb) == -1)
845 return got_error_from_errno2("lstat", ondisk_path);
847 ondisk_len = readlink(ondisk_path, ondisk_target,
848 sizeof(ondisk_target));
849 if (ondisk_len == -1) {
850 err = got_error_from_errno2("readlink",
854 ondisk_target[ondisk_len] = '\0';
857 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
862 if (ancestor_target == NULL ||
863 (ondisk_len != strlen(ancestor_target) ||
864 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
865 have_local_change = 1;
867 deriv_len = strlen(deriv_target);
868 if (ancestor_target == NULL ||
869 (deriv_len != strlen(ancestor_target) ||
870 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
871 have_incoming_change = 1;
873 if (!have_local_change && !have_incoming_change) {
874 if (ancestor_target) {
875 /* Both sides made the same change. */
876 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
878 } else if (deriv_len == ondisk_len &&
879 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
880 /* Both sides added the same symlink. */
881 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
884 /* Both sides added symlinks which don't match. */
885 err = install_symlink_conflict(deriv_target,
886 deriv_base_commit_id, ancestor_target,
887 label_orig, ondisk_target, ondisk_path);
890 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
893 } else if (!have_local_change && have_incoming_change) {
894 /* Apply the incoming change. */
895 err = update_symlink(ondisk_path, deriv_target,
896 strlen(deriv_target));
899 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
900 } else if (have_local_change && have_incoming_change) {
901 if (deriv_len == ondisk_len &&
902 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
903 /* Both sides made the same change. */
904 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
907 err = install_symlink_conflict(deriv_target,
908 deriv_base_commit_id, ancestor_target, label_orig,
909 ondisk_target, ondisk_path);
912 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
918 free(ancestor_target);
922 static const struct got_error *
923 dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
925 const struct got_error *err = NULL;
926 char target_path[PATH_MAX];
935 return got_error_from_errno("got_opentemp");
936 target_len = readlink(ondisk_path, target_path, sizeof(target_path));
937 if (target_len == -1) {
938 err = got_error_from_errno2("readlink", ondisk_path);
941 n = fwrite(target_path, 1, target_len, f);
942 if (n != target_len) {
943 err = got_ferror(f, GOT_ERR_IO);
946 if (fflush(f) == EOF) {
947 err = got_error_from_errno("fflush");
950 if (fseek(f, 0L, SEEK_SET) == -1) {
951 err = got_ferror(f, GOT_ERR_IO);
963 * Perform a 3-way merge where blob_orig acts as the common ancestor,
964 * blob_deriv acts as the first derived version, and the file on disk
965 * acts as the second derived version.
967 static const struct got_error *
968 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
969 struct got_blob_object *blob_orig, const char *ondisk_path,
970 const char *path, uint16_t st_mode, const char *label_orig,
971 struct got_blob_object *blob_deriv,
972 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
973 got_worktree_checkout_cb progress_cb, void *progress_arg)
975 const struct got_error *err = NULL;
976 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
977 char *blob_orig_path = NULL;
978 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
979 char *label_deriv = NULL, *parent = NULL;
981 *local_changes_subsumed = 0;
983 err = got_path_dirname(&parent, ondisk_path);
988 if (asprintf(&base_path, "%s/got-merge-blob-orig",
990 err = got_error_from_errno("asprintf");
995 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
998 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1005 * No common ancestor exists. This is an "add vs add" conflict
1006 * and we simply use an empty ancestor file to make both files
1007 * appear in the merged result in their entirety.
1009 f_orig = got_opentemp();
1010 if (f_orig == NULL) {
1011 err = got_error_from_errno("got_opentemp");
1016 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1017 err = got_error_from_errno("asprintf");
1022 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1025 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1030 err = got_object_id_str(&id_str, deriv_base_commit_id);
1033 if (asprintf(&label_deriv, "%s: commit %s",
1034 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1035 err = got_error_from_errno("asprintf");
1040 * In order the run a 3-way merge with a symlink we copy the symlink's
1041 * target path into a temporary file and use that file with diff3.
1043 if (S_ISLNK(st_mode)) {
1044 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1049 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1051 err = got_error_from_errno2("open", ondisk_path);
1054 f_deriv2 = fdopen(fd, "r");
1055 if (f_deriv2 == NULL) {
1056 err = got_error_from_errno2("fdopen", ondisk_path);
1062 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1063 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1064 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1066 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1067 err = got_error_from_errno("fclose");
1068 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1069 err = got_error_from_errno("fclose");
1070 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1071 err = got_error_from_errno("fclose");
1073 if (blob_orig_path) {
1074 unlink(blob_orig_path);
1075 free(blob_orig_path);
1077 if (blob_deriv_path) {
1078 unlink(blob_deriv_path);
1079 free(blob_deriv_path);
1087 static const struct got_error *
1088 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1089 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1090 int wt_fd, const char *path, struct got_object_id *blob_id)
1092 const struct got_error *err = NULL;
1093 struct got_fileindex_entry *new_ie;
1097 err = got_fileindex_entry_alloc(&new_ie, path);
1101 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1102 blob_id->sha1, base_commit_id->sha1, 1);
1106 err = got_fileindex_entry_add(fileindex, new_ie);
1109 got_fileindex_entry_free(new_ie);
1116 get_ondisk_perms(int executable, mode_t st_mode)
1118 mode_t xbits = S_IXUSR;
1121 /* Map read bits to execute bits. */
1122 if (st_mode & S_IRGRP)
1124 if (st_mode & S_IROTH)
1126 return st_mode | xbits;
1133 apply_umask(mode_t mode)
1142 /* forward declaration */
1143 static const struct got_error *
1144 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1145 const char *path, mode_t te_mode, mode_t st_mode,
1146 struct got_blob_object *blob, int restoring_missing_file,
1147 int reverting_versioned_file, int installing_bad_symlink,
1148 int path_is_unversioned, struct got_repository *repo,
1149 got_worktree_checkout_cb progress_cb, void *progress_arg);
1152 * This function assumes that the provided symlink target points at a
1153 * safe location in the work tree!
1155 static const struct got_error *
1156 replace_existing_symlink(int *did_something, const char *ondisk_path,
1157 const char *target_path, size_t target_len)
1159 const struct got_error *err = NULL;
1161 char etarget[PATH_MAX];
1167 * "Bad" symlinks (those pointing outside the work tree or into the
1168 * .got directory) are installed in the work tree as a regular file
1169 * which contains the bad symlink target path.
1170 * The new symlink target has already been checked for safety by our
1171 * caller. If we can successfully open a regular file then we simply
1172 * replace this file with a symlink below.
1174 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1176 if (!got_err_open_nofollow_on_symlink())
1177 return got_error_from_errno2("open", ondisk_path);
1179 /* We are updating an existing on-disk symlink. */
1180 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1182 return got_error_from_errno2("readlink", ondisk_path);
1184 if (elen == target_len &&
1185 memcmp(etarget, target_path, target_len) == 0)
1186 return NULL; /* nothing to do */
1190 err = update_symlink(ondisk_path, target_path, target_len);
1191 if (fd != -1 && close(fd) == -1 && err == NULL)
1192 err = got_error_from_errno2("close", ondisk_path);
1196 static const struct got_error *
1197 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1198 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1200 const struct got_error *err = NULL;
1201 char canonpath[PATH_MAX];
1202 char *path_got = NULL;
1204 *is_bad_symlink = 0;
1206 if (target_len >= sizeof(canonpath)) {
1207 *is_bad_symlink = 1;
1212 * We do not use realpath(3) to resolve the symlink's target
1213 * path because we don't want to resolve symlinks recursively.
1214 * Instead we make the path absolute and then canonicalize it.
1215 * Relative symlink target lookup should begin at the directory
1216 * in which the blob object is being installed.
1218 if (!got_path_is_absolute(target_path)) {
1219 char *abspath, *parent;
1220 err = got_path_dirname(&parent, ondisk_path);
1223 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1225 return got_error_from_errno("asprintf");
1228 if (strlen(abspath) >= sizeof(canonpath)) {
1229 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1233 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1238 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1243 /* Only allow symlinks pointing at paths within the work tree. */
1244 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1245 *is_bad_symlink = 1;
1249 /* Do not allow symlinks pointing into the .got directory. */
1250 if (asprintf(&path_got, "%s/%s", wtroot_path,
1251 GOT_WORKTREE_GOT_DIR) == -1)
1252 return got_error_from_errno("asprintf");
1253 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1254 *is_bad_symlink = 1;
1260 static const struct got_error *
1261 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1262 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1263 int restoring_missing_file, int reverting_versioned_file,
1264 int path_is_unversioned, int allow_bad_symlinks,
1265 struct got_repository *repo,
1266 got_worktree_checkout_cb progress_cb, void *progress_arg)
1268 const struct got_error *err = NULL;
1269 char target_path[PATH_MAX];
1270 size_t len, target_len = 0;
1271 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1272 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1274 *is_bad_symlink = 0;
1277 * Blob object content specifies the target path of the link.
1278 * If a symbolic link cannot be installed we instead create
1279 * a regular file which contains the link target path stored
1280 * in the blob object.
1283 err = got_object_blob_read_block(&len, blob);
1287 if (len + target_len >= sizeof(target_path)) {
1288 /* Path too long; install as a regular file. */
1289 *is_bad_symlink = 1;
1290 got_object_blob_rewind(blob);
1291 return install_blob(worktree, ondisk_path, path,
1292 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1293 restoring_missing_file, reverting_versioned_file,
1294 1, path_is_unversioned, repo, progress_cb,
1298 /* Skip blob object header first time around. */
1299 memcpy(target_path + target_len, buf + hdrlen,
1301 target_len += len - hdrlen;
1305 target_path[target_len] = '\0';
1307 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1308 ondisk_path, worktree->root_path);
1312 if (*is_bad_symlink && !allow_bad_symlinks) {
1313 /* install as a regular file */
1314 got_object_blob_rewind(blob);
1315 err = install_blob(worktree, ondisk_path, path,
1316 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1317 restoring_missing_file, reverting_versioned_file, 1,
1318 path_is_unversioned, repo, progress_cb, progress_arg);
1322 if (symlink(target_path, ondisk_path) == -1) {
1323 if (errno == EEXIST) {
1324 int symlink_replaced;
1325 if (path_is_unversioned) {
1326 err = (*progress_cb)(progress_arg,
1327 GOT_STATUS_UNVERSIONED, path);
1330 err = replace_existing_symlink(&symlink_replaced,
1331 ondisk_path, target_path, target_len);
1335 if (symlink_replaced) {
1336 err = (*progress_cb)(progress_arg,
1337 reverting_versioned_file ?
1339 GOT_STATUS_UPDATE, path);
1341 err = (*progress_cb)(progress_arg,
1342 GOT_STATUS_EXISTS, path);
1345 return err; /* Nothing else to do. */
1348 if (errno == ENOENT) {
1350 err = got_path_dirname(&parent, ondisk_path);
1353 err = add_dir_on_disk(worktree, parent);
1358 * Retry, and fall through to error handling
1359 * below if this second attempt fails.
1361 if (symlink(target_path, ondisk_path) != -1) {
1362 err = NULL; /* success */
1367 /* Handle errors from first or second creation attempt. */
1368 if (errno == ENAMETOOLONG) {
1369 /* bad target path; install as a regular file */
1370 *is_bad_symlink = 1;
1371 got_object_blob_rewind(blob);
1372 err = install_blob(worktree, ondisk_path, path,
1373 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1374 restoring_missing_file, reverting_versioned_file, 1,
1375 path_is_unversioned, repo,
1376 progress_cb, progress_arg);
1377 } else if (errno == ENOTDIR) {
1378 err = got_error_path(ondisk_path,
1379 GOT_ERR_FILE_OBSTRUCTED);
1381 err = got_error_from_errno3("symlink",
1382 target_path, ondisk_path);
1384 } else if (progress_cb)
1385 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1386 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1390 static const struct got_error *
1391 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1392 const char *path, mode_t te_mode, mode_t st_mode,
1393 struct got_blob_object *blob, int restoring_missing_file,
1394 int reverting_versioned_file, int installing_bad_symlink,
1395 int path_is_unversioned, struct got_repository *repo,
1396 got_worktree_checkout_cb progress_cb, void *progress_arg)
1398 const struct got_error *err = NULL;
1402 char *tmppath = NULL;
1405 mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1406 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1409 if (errno == ENOENT) {
1411 err = got_path_dirname(&parent, path);
1414 err = add_dir_on_disk(worktree, parent);
1418 fd = open(ondisk_path,
1419 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1422 return got_error_from_errno2("open",
1424 } else if (errno == EEXIST) {
1425 if (path_is_unversioned) {
1426 err = (*progress_cb)(progress_arg,
1427 GOT_STATUS_UNVERSIONED, path);
1430 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1431 !S_ISREG(st_mode) && !installing_bad_symlink) {
1432 /* TODO file is obstructed; do something */
1433 err = got_error_path(ondisk_path,
1434 GOT_ERR_FILE_OBSTRUCTED);
1437 err = got_opentemp_named_fd(&tmppath, &fd,
1443 if (fchmod(fd, apply_umask(mode)) == -1) {
1444 err = got_error_from_errno2("fchmod",
1450 return got_error_from_errno2("open", ondisk_path);
1454 if (restoring_missing_file)
1455 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1457 else if (reverting_versioned_file)
1458 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1461 err = (*progress_cb)(progress_arg,
1462 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1467 hdrlen = got_object_blob_get_hdrlen(blob);
1469 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1470 err = got_object_blob_read_block(&len, blob);
1474 /* Skip blob object header first time around. */
1475 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1477 err = got_error_from_errno("write");
1479 } else if (outlen != len - hdrlen) {
1480 err = got_error(GOT_ERR_IO);
1487 if (fsync(fd) != 0) {
1488 err = got_error_from_errno("fsync");
1493 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1494 err = got_error_from_errno2("unlink", ondisk_path);
1497 if (rename(tmppath, ondisk_path) != 0) {
1498 err = got_error_from_errno3("rename", tmppath,
1507 if (fd != -1 && close(fd) == -1 && err == NULL)
1508 err = got_error_from_errno("close");
1509 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1510 err = got_error_from_errno2("unlink", tmppath);
1515 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1516 static const struct got_error *
1517 get_modified_file_content_status(unsigned char *status, FILE *f)
1519 const struct got_error *err = NULL;
1520 const char *markers[3] = {
1521 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1522 GOT_DIFF_CONFLICT_MARKER_SEP,
1523 GOT_DIFF_CONFLICT_MARKER_END
1527 size_t linesize = 0;
1530 while (*status == GOT_STATUS_MODIFY) {
1531 linelen = getline(&line, &linesize, f);
1532 if (linelen == -1) {
1535 err = got_ferror(f, GOT_ERR_IO);
1539 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1540 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1542 *status = GOT_STATUS_CONFLICT;
1553 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1555 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1556 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1560 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1562 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1563 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1564 ie->mtime_sec == sb->st_mtim.tv_sec &&
1565 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1566 ie->size == (sb->st_size & 0xffffffff) &&
1567 !xbit_differs(ie, sb->st_mode));
1570 static unsigned char
1571 get_staged_status(struct got_fileindex_entry *ie)
1573 switch (got_fileindex_entry_stage_get(ie)) {
1574 case GOT_FILEIDX_STAGE_ADD:
1575 return GOT_STATUS_ADD;
1576 case GOT_FILEIDX_STAGE_DELETE:
1577 return GOT_STATUS_DELETE;
1578 case GOT_FILEIDX_STAGE_MODIFY:
1579 return GOT_STATUS_MODIFY;
1581 return GOT_STATUS_NO_CHANGE;
1585 static const struct got_error *
1586 get_symlink_modification_status(unsigned char *status,
1587 struct got_fileindex_entry *ie, const char *abspath,
1588 int dirfd, const char *de_name, struct got_blob_object *blob)
1590 const struct got_error *err = NULL;
1591 char target_path[PATH_MAX];
1592 char etarget[PATH_MAX];
1594 size_t len, target_len = 0;
1595 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1596 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1598 *status = GOT_STATUS_NO_CHANGE;
1600 /* Blob object content specifies the target path of the link. */
1602 err = got_object_blob_read_block(&len, blob);
1605 if (len + target_len >= sizeof(target_path)) {
1607 * Should not happen. The blob contents were OK
1608 * when this symlink was installed.
1610 return got_error(GOT_ERR_NO_SPACE);
1613 /* Skip blob object header first time around. */
1614 memcpy(target_path + target_len, buf + hdrlen,
1616 target_len += len - hdrlen;
1620 target_path[target_len] = '\0';
1623 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1625 return got_error_from_errno2("readlinkat", abspath);
1627 elen = readlink(abspath, etarget, sizeof(etarget));
1629 return got_error_from_errno2("readlink", abspath);
1632 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1633 *status = GOT_STATUS_MODIFY;
1638 static const struct got_error *
1639 get_file_status(unsigned char *status, struct stat *sb,
1640 struct got_fileindex_entry *ie, const char *abspath,
1641 int dirfd, const char *de_name, struct got_repository *repo)
1643 const struct got_error *err = NULL;
1644 struct got_object_id id;
1646 int fd = -1, fd1 = -1;
1649 struct got_blob_object *blob = NULL;
1651 unsigned char staged_status = get_staged_status(ie);
1653 *status = GOT_STATUS_NO_CHANGE;
1654 memset(sb, 0, sizeof(*sb));
1657 * Whenever the caller provides a directory descriptor and a
1658 * directory entry name for the file, use them! This prevents
1659 * race conditions if filesystem paths change beneath our feet.
1662 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1663 if (errno == ENOENT) {
1664 if (got_fileindex_entry_has_file_on_disk(ie))
1665 *status = GOT_STATUS_MISSING;
1667 *status = GOT_STATUS_DELETE;
1670 err = got_error_from_errno2("fstatat", abspath);
1674 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1675 if (fd == -1 && errno != ENOENT &&
1676 !got_err_open_nofollow_on_symlink())
1677 return got_error_from_errno2("open", abspath);
1678 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1679 if (lstat(abspath, sb) == -1)
1680 return got_error_from_errno2("lstat", abspath);
1681 } else if (fd == -1 || fstat(fd, sb) == -1) {
1682 if (errno == ENOENT) {
1683 if (got_fileindex_entry_has_file_on_disk(ie))
1684 *status = GOT_STATUS_MISSING;
1686 *status = GOT_STATUS_DELETE;
1689 err = got_error_from_errno2("fstat", abspath);
1694 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1695 *status = GOT_STATUS_OBSTRUCTED;
1699 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1700 *status = GOT_STATUS_DELETE;
1702 } else if (!got_fileindex_entry_has_blob(ie) &&
1703 staged_status != GOT_STATUS_ADD) {
1704 *status = GOT_STATUS_ADD;
1708 if (!stat_info_differs(ie, sb))
1711 if (S_ISLNK(sb->st_mode) &&
1712 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1713 *status = GOT_STATUS_MODIFY;
1717 if (staged_status == GOT_STATUS_MODIFY ||
1718 staged_status == GOT_STATUS_ADD)
1719 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1721 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1723 fd1 = got_opentempfd();
1725 err = got_error_from_errno("got_opentempfd");
1728 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1732 if (S_ISLNK(sb->st_mode)) {
1733 err = get_symlink_modification_status(status, ie,
1734 abspath, dirfd, de_name, blob);
1739 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1741 err = got_error_from_errno2("openat", abspath);
1746 f = fdopen(fd, "r");
1748 err = got_error_from_errno2("fdopen", abspath);
1752 hdrlen = got_object_blob_get_hdrlen(blob);
1754 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1755 err = got_object_blob_read_block(&blen, blob);
1758 /* Skip length of blob object header first time around. */
1759 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1760 if (flen == 0 && ferror(f)) {
1761 err = got_error_from_errno("fread");
1764 if (blen - hdrlen == 0) {
1766 *status = GOT_STATUS_MODIFY;
1768 } else if (flen == 0) {
1769 if (blen - hdrlen != 0)
1770 *status = GOT_STATUS_MODIFY;
1772 } else if (blen - hdrlen == flen) {
1773 /* Skip blob object header first time around. */
1774 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1775 *status = GOT_STATUS_MODIFY;
1779 *status = GOT_STATUS_MODIFY;
1785 if (*status == GOT_STATUS_MODIFY) {
1787 err = get_modified_file_content_status(status, f);
1788 } else if (xbit_differs(ie, sb->st_mode))
1789 *status = GOT_STATUS_MODE_CHANGE;
1791 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1792 err = got_error_from_errno("close");
1794 got_object_blob_close(blob);
1795 if (f != NULL && fclose(f) == EOF && err == NULL)
1796 err = got_error_from_errno2("fclose", abspath);
1797 if (fd != -1 && close(fd) == -1 && err == NULL)
1798 err = got_error_from_errno2("close", abspath);
1803 * Update timestamps in the file index if a file is unmodified and
1804 * we had to run a full content comparison to find out.
1806 static const struct got_error *
1807 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1808 struct got_fileindex_entry *ie, struct stat *sb)
1810 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1811 return got_fileindex_entry_update(ie, wt_fd, path,
1812 ie->blob_sha1, ie->commit_sha1, 1);
1817 static const struct got_error *
1818 update_blob(struct got_worktree *worktree,
1819 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1820 struct got_tree_entry *te, const char *path,
1821 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1824 const struct got_error *err = NULL;
1825 struct got_blob_object *blob = NULL;
1826 char *ondisk_path = NULL;
1827 unsigned char status = GOT_STATUS_NO_CHANGE;
1829 int fd1 = -1, fd2 = -1;
1831 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1832 return got_error_from_errno("asprintf");
1835 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1836 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1839 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1843 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1844 sb.st_mode = got_fileindex_perms_to_st(ie);
1846 if (stat(ondisk_path, &sb) == -1) {
1847 if (errno != ENOENT) {
1848 err = got_error_from_errno2("stat",
1852 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1853 status = GOT_STATUS_UNVERSIONED;
1855 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1856 status = GOT_STATUS_UNVERSIONED;
1858 status = GOT_STATUS_OBSTRUCTED;
1862 if (status == GOT_STATUS_OBSTRUCTED) {
1864 got_fileindex_entry_mark_skipped(ie);
1865 err = (*progress_cb)(progress_arg, status, path);
1868 if (status == GOT_STATUS_CONFLICT) {
1870 got_fileindex_entry_mark_skipped(ie);
1871 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1876 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1877 (S_ISLNK(te->mode) ||
1878 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1880 * This is a regular file or an installed bad symlink.
1881 * If the file index indicates that this file is already
1882 * up-to-date with respect to the repository we can skip
1883 * updating contents of this file.
1885 if (got_fileindex_entry_has_commit(ie) &&
1886 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1887 SHA1_DIGEST_LENGTH) == 0) {
1889 err = sync_timestamps(worktree->root_fd,
1890 path, status, ie, &sb);
1893 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1897 if (got_fileindex_entry_has_blob(ie) &&
1898 memcmp(ie->blob_sha1, te->id.sha1,
1899 SHA1_DIGEST_LENGTH) == 0) {
1900 /* Different commit but the same blob. */
1901 err = sync_timestamps(worktree->root_fd,
1902 path, status, ie, &sb);
1905 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1911 fd1 = got_opentempfd();
1913 err = got_error_from_errno("got_opentempfd");
1916 err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
1920 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1921 int update_timestamps;
1922 struct got_blob_object *blob2 = NULL;
1923 char *label_orig = NULL;
1924 if (got_fileindex_entry_has_blob(ie)) {
1925 fd2 = got_opentempfd();
1927 err = got_error_from_errno("got_opentempfd");
1930 struct got_object_id id2;
1931 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1932 err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
1937 if (got_fileindex_entry_has_commit(ie)) {
1938 char id_str[SHA1_DIGEST_STRING_LENGTH];
1939 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1940 sizeof(id_str)) == NULL) {
1941 err = got_error_path(id_str,
1942 GOT_ERR_BAD_OBJ_ID_STR);
1945 if (asprintf(&label_orig, "%s: commit %s",
1946 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1947 err = got_error_from_errno("asprintf");
1951 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1953 err = got_object_blob_read_to_str(&link_target, blob);
1956 err = merge_symlink(worktree, blob2, ondisk_path, path,
1957 label_orig, link_target, worktree->base_commit_id,
1958 repo, progress_cb, progress_arg);
1961 err = merge_blob(&update_timestamps, worktree, blob2,
1962 ondisk_path, path, sb.st_mode, label_orig, blob,
1963 worktree->base_commit_id, repo,
1964 progress_cb, progress_arg);
1967 if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
1968 err = got_error_from_errno("close");
1972 got_object_blob_close(blob2);
1976 * Do not update timestamps of files with local changes.
1977 * Otherwise, a future status walk would treat them as
1978 * unmodified files again.
1980 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1981 blob->id.sha1, worktree->base_commit_id->sha1,
1983 } else if (status == GOT_STATUS_MODE_CHANGE) {
1984 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1985 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1986 } else if (status == GOT_STATUS_DELETE) {
1987 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1990 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1991 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1995 int is_bad_symlink = 0;
1996 if (S_ISLNK(te->mode)) {
1997 err = install_symlink(&is_bad_symlink, worktree,
1998 ondisk_path, path, blob,
1999 status == GOT_STATUS_MISSING, 0,
2000 status == GOT_STATUS_UNVERSIONED, 0,
2001 repo, progress_cb, progress_arg);
2003 err = install_blob(worktree, ondisk_path, path,
2004 te->mode, sb.st_mode, blob,
2005 status == GOT_STATUS_MISSING, 0, 0,
2006 status == GOT_STATUS_UNVERSIONED, repo,
2007 progress_cb, progress_arg);
2013 err = got_fileindex_entry_update(ie,
2014 worktree->root_fd, path, blob->id.sha1,
2015 worktree->base_commit_id->sha1, 1);
2017 err = create_fileindex_entry(&ie, fileindex,
2018 worktree->base_commit_id, worktree->root_fd, path,
2024 if (is_bad_symlink) {
2025 got_fileindex_entry_filetype_set(ie,
2026 GOT_FILEIDX_MODE_BAD_SYMLINK);
2030 if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2031 err = got_error_from_errno("close");
2034 got_object_blob_close(blob);
2040 static const struct got_error *
2041 remove_ondisk_file(const char *root_path, const char *path)
2043 const struct got_error *err = NULL;
2044 char *ondisk_path = NULL, *parent = NULL;
2046 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2047 return got_error_from_errno("asprintf");
2049 if (unlink(ondisk_path) == -1) {
2050 if (errno != ENOENT)
2051 err = got_error_from_errno2("unlink", ondisk_path);
2053 size_t root_len = strlen(root_path);
2054 err = got_path_dirname(&parent, ondisk_path);
2057 while (got_path_cmp(parent, root_path,
2058 strlen(parent), root_len) != 0) {
2060 ondisk_path = parent;
2062 if (rmdir(ondisk_path) == -1) {
2063 if (errno != ENOTEMPTY)
2064 err = got_error_from_errno2("rmdir",
2068 err = got_path_dirname(&parent, ondisk_path);
2079 static const struct got_error *
2080 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2081 struct got_fileindex_entry *ie, struct got_repository *repo,
2082 got_worktree_checkout_cb progress_cb, void *progress_arg)
2084 const struct got_error *err = NULL;
2085 unsigned char status;
2089 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2090 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2092 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2094 return got_error_from_errno("asprintf");
2096 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2100 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2101 char ondisk_target[PATH_MAX];
2102 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2103 sizeof(ondisk_target));
2104 if (ondisk_len == -1) {
2105 err = got_error_from_errno2("readlink", ondisk_path);
2108 ondisk_target[ondisk_len] = '\0';
2109 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2110 NULL, NULL, /* XXX pass common ancestor info? */
2111 ondisk_target, ondisk_path);
2114 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2119 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2120 status == GOT_STATUS_ADD) {
2121 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2125 * Preserve the working file and change the deleted blob's
2126 * entry into a schedule-add entry.
2128 err = got_fileindex_entry_update(ie, worktree->root_fd,
2129 ie->path, NULL, NULL, 0);
2131 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2134 if (status == GOT_STATUS_NO_CHANGE) {
2135 err = remove_ondisk_file(worktree->root_path, ie->path);
2139 got_fileindex_entry_remove(fileindex, ie);
2146 struct diff_cb_arg {
2147 struct got_fileindex *fileindex;
2148 struct got_worktree *worktree;
2149 struct got_repository *repo;
2150 got_worktree_checkout_cb progress_cb;
2152 got_cancel_cb cancel_cb;
2156 static const struct got_error *
2157 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2158 struct got_tree_entry *te, const char *parent_path)
2160 struct diff_cb_arg *a = arg;
2162 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2163 return got_error(GOT_ERR_CANCELLED);
2165 return update_blob(a->worktree, a->fileindex, ie, te,
2166 ie->path, a->repo, a->progress_cb, a->progress_arg);
2169 static const struct got_error *
2170 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2172 struct diff_cb_arg *a = arg;
2174 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2175 return got_error(GOT_ERR_CANCELLED);
2177 return delete_blob(a->worktree, a->fileindex, ie,
2178 a->repo, a->progress_cb, a->progress_arg);
2181 static const struct got_error *
2182 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2184 struct diff_cb_arg *a = arg;
2185 const struct got_error *err;
2188 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2189 return got_error(GOT_ERR_CANCELLED);
2191 if (got_object_tree_entry_is_submodule(te))
2194 if (asprintf(&path, "%s%s%s", parent_path,
2195 parent_path[0] ? "/" : "", te->name)
2197 return got_error_from_errno("asprintf");
2199 if (S_ISDIR(te->mode))
2200 err = add_dir_on_disk(a->worktree, path);
2202 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2203 a->repo, a->progress_cb, a->progress_arg);
2209 const struct got_error *
2210 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2212 uint32_t uuid_status;
2214 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2215 if (uuid_status != uuid_s_ok) {
2217 return got_error_uuid(uuid_status, "uuid_to_string");
2223 static const struct got_error *
2224 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2226 const struct got_error *err = NULL;
2227 char *uuidstr = NULL;
2231 err = got_worktree_get_uuid(&uuidstr, worktree);
2235 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2236 err = got_error_from_errno("asprintf");
2243 const struct got_error *
2244 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2246 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2249 static const struct got_error *
2250 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2252 return get_ref_name(refname, worktree,
2253 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2256 static const struct got_error *
2257 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2259 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2262 static const struct got_error *
2263 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2265 return get_ref_name(refname, worktree,
2266 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2269 static const struct got_error *
2270 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2272 return get_ref_name(refname, worktree,
2273 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2276 static const struct got_error *
2277 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2279 return get_ref_name(refname, worktree,
2280 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2283 static const struct got_error *
2284 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2286 return get_ref_name(refname, worktree,
2287 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2290 static const struct got_error *
2291 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2293 return get_ref_name(refname, worktree,
2294 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2297 static const struct got_error *
2298 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2300 return get_ref_name(refname, worktree,
2301 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2304 const struct got_error *
2305 got_worktree_get_histedit_script_path(char **path,
2306 struct got_worktree *worktree)
2308 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2309 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2311 return got_error_from_errno("asprintf");
2316 static const struct got_error *
2317 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2319 return get_ref_name(refname, worktree,
2320 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2323 static const struct got_error *
2324 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2326 return get_ref_name(refname, worktree,
2327 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2331 * Prevent Git's garbage collector from deleting our base commit by
2332 * setting a reference to our base commit's ID.
2334 static const struct got_error *
2335 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2337 const struct got_error *err = NULL;
2338 struct got_reference *ref = NULL;
2341 err = got_worktree_get_base_ref_name(&refname, worktree);
2345 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2349 err = got_ref_write(ref, repo);
2357 static const struct got_error *
2358 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2360 const struct got_error *err = NULL;
2362 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2363 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2364 err = got_error_from_errno("asprintf");
2365 *fileindex_path = NULL;
2371 static const struct got_error *
2372 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2373 struct got_worktree *worktree)
2375 const struct got_error *err = NULL;
2378 *fileindex_path = NULL;
2379 *fileindex = got_fileindex_alloc();
2380 if (*fileindex == NULL)
2381 return got_error_from_errno("got_fileindex_alloc");
2383 err = get_fileindex_path(fileindex_path, worktree);
2387 index = fopen(*fileindex_path, "rbe");
2388 if (index == NULL) {
2389 if (errno != ENOENT)
2390 err = got_error_from_errno2("fopen", *fileindex_path);
2392 err = got_fileindex_read(*fileindex, index);
2393 if (fclose(index) == EOF && err == NULL)
2394 err = got_error_from_errno("fclose");
2398 free(*fileindex_path);
2399 *fileindex_path = NULL;
2400 got_fileindex_free(*fileindex);
2406 struct bump_base_commit_id_arg {
2407 struct got_object_id *base_commit_id;
2410 const char *entry_name;
2411 got_worktree_checkout_cb progress_cb;
2415 /* Bump base commit ID of all files within an updated part of the work tree. */
2416 static const struct got_error *
2417 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2419 const struct got_error *err;
2420 struct bump_base_commit_id_arg *a = arg;
2422 if (a->entry_name) {
2423 if (strcmp(ie->path, a->path) != 0)
2425 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2428 if (got_fileindex_entry_was_skipped(ie))
2431 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2432 SHA1_DIGEST_LENGTH) == 0)
2435 if (a->progress_cb) {
2436 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2441 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2445 static const struct got_error *
2446 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2447 struct got_fileindex *fileindex,
2448 got_worktree_checkout_cb progress_cb, void *progress_arg)
2450 struct bump_base_commit_id_arg bbc_arg;
2452 bbc_arg.base_commit_id = worktree->base_commit_id;
2453 bbc_arg.entry_name = NULL;
2455 bbc_arg.path_len = 0;
2456 bbc_arg.progress_cb = progress_cb;
2457 bbc_arg.progress_arg = progress_arg;
2459 return got_fileindex_for_each_entry_safe(fileindex,
2460 bump_base_commit_id, &bbc_arg);
2463 static const struct got_error *
2464 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2466 const struct got_error *err = NULL;
2467 char *new_fileindex_path = NULL;
2468 FILE *new_index = NULL;
2469 struct timespec timeout;
2471 err = got_opentemp_named(&new_fileindex_path, &new_index,
2476 err = got_fileindex_write(fileindex, new_index);
2480 if (rename(new_fileindex_path, fileindex_path) != 0) {
2481 err = got_error_from_errno3("rename", new_fileindex_path,
2483 unlink(new_fileindex_path);
2487 * Sleep for a short amount of time to ensure that files modified after
2488 * this program exits have a different time stamp from the one which
2489 * was recorded in the file index.
2492 timeout.tv_nsec = 1;
2493 nanosleep(&timeout, NULL);
2497 free(new_fileindex_path);
2501 static const struct got_error *
2502 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2503 struct got_object_id **tree_id, const char *wt_relpath,
2504 struct got_commit_object *base_commit, struct got_worktree *worktree,
2505 struct got_repository *repo)
2507 const struct got_error *err = NULL;
2508 struct got_object_id *id = NULL;
2509 char *in_repo_path = NULL;
2510 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2512 *entry_type = GOT_OBJ_TYPE_ANY;
2513 *tree_relpath = NULL;
2516 if (wt_relpath[0] == '\0') {
2517 /* Check out all files within the work tree. */
2518 *entry_type = GOT_OBJ_TYPE_TREE;
2519 *tree_relpath = strdup("");
2520 if (*tree_relpath == NULL) {
2521 err = got_error_from_errno("strdup");
2524 err = got_object_id_by_path(tree_id, repo, base_commit,
2525 worktree->path_prefix);
2531 /* Check out a subset of files in the work tree. */
2533 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2534 is_root_wt ? "" : "/", wt_relpath) == -1) {
2535 err = got_error_from_errno("asprintf");
2539 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2544 in_repo_path = NULL;
2546 err = got_object_get_type(entry_type, repo, id);
2550 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2551 /* Check out a single file. */
2552 if (strchr(wt_relpath, '/') == NULL) {
2553 /* Check out a single file in work tree's root dir. */
2554 in_repo_path = strdup(worktree->path_prefix);
2555 if (in_repo_path == NULL) {
2556 err = got_error_from_errno("strdup");
2559 *tree_relpath = strdup("");
2560 if (*tree_relpath == NULL) {
2561 err = got_error_from_errno("strdup");
2565 /* Check out a single file in a subdirectory. */
2566 err = got_path_dirname(tree_relpath, wt_relpath);
2569 if (asprintf(&in_repo_path, "%s%s%s",
2570 worktree->path_prefix, is_root_wt ? "" : "/",
2571 *tree_relpath) == -1) {
2572 err = got_error_from_errno("asprintf");
2576 err = got_object_id_by_path(tree_id, repo,
2577 base_commit, in_repo_path);
2579 /* Check out all files within a subdirectory. */
2580 *tree_id = got_object_id_dup(id);
2581 if (*tree_id == NULL) {
2582 err = got_error_from_errno("got_object_id_dup");
2585 *tree_relpath = strdup(wt_relpath);
2586 if (*tree_relpath == NULL) {
2587 err = got_error_from_errno("strdup");
2595 *entry_type = GOT_OBJ_TYPE_ANY;
2596 free(*tree_relpath);
2597 *tree_relpath = NULL;
2604 static const struct got_error *
2605 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2606 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2607 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2608 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2610 const struct got_error *err = NULL;
2611 struct got_commit_object *commit = NULL;
2612 struct got_tree_object *tree = NULL;
2613 struct got_fileindex_diff_tree_cb diff_cb;
2614 struct diff_cb_arg arg;
2616 err = ref_base_commit(worktree, repo);
2618 if (!(err->code == GOT_ERR_ERRNO &&
2619 (errno == EACCES || errno == EROFS)))
2621 err = (*progress_cb)(progress_arg,
2622 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2627 err = got_object_open_as_commit(&commit, repo,
2628 worktree->base_commit_id);
2632 err = got_object_open_as_tree(&tree, repo, tree_id);
2637 got_object_tree_find_entry(tree, entry_name) == NULL) {
2638 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2642 diff_cb.diff_old_new = diff_old_new;
2643 diff_cb.diff_old = diff_old;
2644 diff_cb.diff_new = diff_new;
2645 arg.fileindex = fileindex;
2646 arg.worktree = worktree;
2648 arg.progress_cb = progress_cb;
2649 arg.progress_arg = progress_arg;
2650 arg.cancel_cb = cancel_cb;
2651 arg.cancel_arg = cancel_arg;
2652 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2653 entry_name, repo, &diff_cb, &arg);
2656 got_object_tree_close(tree);
2658 got_object_commit_close(commit);
2662 const struct got_error *
2663 got_worktree_checkout_files(struct got_worktree *worktree,
2664 struct got_pathlist_head *paths, struct got_repository *repo,
2665 got_worktree_checkout_cb progress_cb, void *progress_arg,
2666 got_cancel_cb cancel_cb, void *cancel_arg)
2668 const struct got_error *err = NULL, *sync_err, *unlockerr;
2669 struct got_commit_object *commit = NULL;
2670 struct got_tree_object *tree = NULL;
2671 struct got_fileindex *fileindex = NULL;
2672 char *fileindex_path = NULL;
2673 struct got_pathlist_entry *pe;
2674 struct tree_path_data {
2675 STAILQ_ENTRY(tree_path_data) entry;
2676 struct got_object_id *tree_id;
2681 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2683 STAILQ_INIT(&tree_paths);
2685 err = lock_worktree(worktree, LOCK_EX);
2689 err = got_object_open_as_commit(&commit, repo,
2690 worktree->base_commit_id);
2694 /* Map all specified paths to in-repository trees. */
2695 TAILQ_FOREACH(pe, paths, entry) {
2696 tpd = malloc(sizeof(*tpd));
2698 err = got_error_from_errno("malloc");
2702 err = find_tree_entry_for_checkout(&tpd->entry_type,
2703 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2710 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2711 err = got_path_basename(&tpd->entry_name, pe->path);
2719 tpd->entry_name = NULL;
2721 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2725 * Read the file index.
2726 * Checking out files is supposed to be an idempotent operation.
2727 * If the on-disk file index is incomplete we will try to complete it.
2729 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2733 tpd = STAILQ_FIRST(&tree_paths);
2734 TAILQ_FOREACH(pe, paths, entry) {
2735 struct bump_base_commit_id_arg bbc_arg;
2737 err = checkout_files(worktree, fileindex, tpd->relpath,
2738 tpd->tree_id, tpd->entry_name, repo,
2739 progress_cb, progress_arg, cancel_cb, cancel_arg);
2743 bbc_arg.base_commit_id = worktree->base_commit_id;
2744 bbc_arg.entry_name = tpd->entry_name;
2745 bbc_arg.path = pe->path;
2746 bbc_arg.path_len = pe->path_len;
2747 bbc_arg.progress_cb = progress_cb;
2748 bbc_arg.progress_arg = progress_arg;
2749 err = got_fileindex_for_each_entry_safe(fileindex,
2750 bump_base_commit_id, &bbc_arg);
2754 tpd = STAILQ_NEXT(tpd, entry);
2756 sync_err = sync_fileindex(fileindex, fileindex_path);
2757 if (sync_err && err == NULL)
2760 free(fileindex_path);
2762 got_object_tree_close(tree);
2764 got_object_commit_close(commit);
2766 got_fileindex_free(fileindex);
2767 while (!STAILQ_EMPTY(&tree_paths)) {
2768 tpd = STAILQ_FIRST(&tree_paths);
2769 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2774 unlockerr = lock_worktree(worktree, LOCK_SH);
2775 if (unlockerr && err == NULL)
2780 struct merge_file_cb_arg {
2781 struct got_worktree *worktree;
2782 struct got_fileindex *fileindex;
2783 got_worktree_checkout_cb progress_cb;
2785 got_cancel_cb cancel_cb;
2787 const char *label_orig;
2788 struct got_object_id *commit_id2;
2789 int allow_bad_symlinks;
2792 static const struct got_error *
2793 merge_file_cb(void *arg, struct got_blob_object *blob1,
2794 struct got_blob_object *blob2, FILE *f1, FILE *f2,
2795 struct got_object_id *id1, struct got_object_id *id2,
2796 const char *path1, const char *path2,
2797 mode_t mode1, mode_t mode2, struct got_repository *repo)
2799 static const struct got_error *err = NULL;
2800 struct merge_file_cb_arg *a = arg;
2801 struct got_fileindex_entry *ie;
2802 char *ondisk_path = NULL;
2804 unsigned char status;
2805 int local_changes_subsumed;
2806 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2807 char *id_str = NULL, *label_deriv2 = NULL;
2809 if (blob1 && blob2) {
2810 ie = got_fileindex_entry_get(a->fileindex, path2,
2813 return (*a->progress_cb)(a->progress_arg,
2814 GOT_STATUS_MISSING, path2);
2816 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2818 return got_error_from_errno("asprintf");
2820 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2825 if (status == GOT_STATUS_DELETE) {
2826 err = (*a->progress_cb)(a->progress_arg,
2827 GOT_STATUS_MERGE, path2);
2830 if (status != GOT_STATUS_NO_CHANGE &&
2831 status != GOT_STATUS_MODIFY &&
2832 status != GOT_STATUS_CONFLICT &&
2833 status != GOT_STATUS_ADD) {
2834 err = (*a->progress_cb)(a->progress_arg, status, path2);
2838 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2840 err = got_object_blob_read_to_str(&link_target2, blob2);
2843 err = merge_symlink(a->worktree, blob1, ondisk_path,
2844 path2, a->label_orig, link_target2, a->commit_id2,
2845 repo, a->progress_cb, a->progress_arg);
2850 f_orig = got_opentemp();
2851 if (f_orig == NULL) {
2852 err = got_error_from_errno("got_opentemp");
2855 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2860 f_deriv2 = got_opentemp();
2861 if (f_deriv2 == NULL)
2863 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2868 fd = open(ondisk_path,
2869 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2871 err = got_error_from_errno2("open",
2875 f_deriv = fdopen(fd, "r");
2876 if (f_deriv == NULL) {
2877 err = got_error_from_errno2("fdopen",
2882 err = got_object_id_str(&id_str, a->commit_id2);
2885 if (asprintf(&label_deriv2, "%s: commit %s",
2886 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2887 err = got_error_from_errno("asprintf");
2890 err = merge_file(&local_changes_subsumed, a->worktree,
2891 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2892 sb.st_mode, a->label_orig, NULL, label_deriv2,
2893 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2894 a->progress_cb, a->progress_arg);
2897 ie = got_fileindex_entry_get(a->fileindex, path1,
2900 return (*a->progress_cb)(a->progress_arg,
2901 GOT_STATUS_MISSING, path1);
2903 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2905 return got_error_from_errno("asprintf");
2907 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2913 case GOT_STATUS_NO_CHANGE:
2914 err = (*a->progress_cb)(a->progress_arg,
2915 GOT_STATUS_DELETE, path1);
2918 err = remove_ondisk_file(a->worktree->root_path, path1);
2922 got_fileindex_entry_mark_deleted_from_disk(ie);
2924 case GOT_STATUS_DELETE:
2925 case GOT_STATUS_MISSING:
2926 err = (*a->progress_cb)(a->progress_arg,
2927 GOT_STATUS_DELETE, path1);
2931 got_fileindex_entry_mark_deleted_from_disk(ie);
2933 case GOT_STATUS_ADD: {
2934 struct got_object_id *id;
2938 * Delete the added file only if its content already
2939 * exists in the repository.
2941 err = got_object_blob_file_create(&id, &blob1_f,
2942 &blob1_size, path1);
2945 if (got_object_id_cmp(id, id1) == 0) {
2946 err = (*a->progress_cb)(a->progress_arg,
2947 GOT_STATUS_DELETE, path1);
2950 err = remove_ondisk_file(a->worktree->root_path,
2955 got_fileindex_entry_remove(a->fileindex,
2958 err = (*a->progress_cb)(a->progress_arg,
2959 GOT_STATUS_CANNOT_DELETE, path1);
2961 if (fclose(blob1_f) == EOF && err == NULL)
2962 err = got_error_from_errno("fclose");
2968 case GOT_STATUS_MODIFY:
2969 case GOT_STATUS_CONFLICT:
2970 err = (*a->progress_cb)(a->progress_arg,
2971 GOT_STATUS_CANNOT_DELETE, path1);
2975 case GOT_STATUS_OBSTRUCTED:
2976 err = (*a->progress_cb)(a->progress_arg, status, path1);
2984 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2986 return got_error_from_errno("asprintf");
2987 ie = got_fileindex_entry_get(a->fileindex, path2,
2990 err = get_file_status(&status, &sb, ie, ondisk_path,
2994 if (status != GOT_STATUS_NO_CHANGE &&
2995 status != GOT_STATUS_MODIFY &&
2996 status != GOT_STATUS_CONFLICT &&
2997 status != GOT_STATUS_ADD) {
2998 err = (*a->progress_cb)(a->progress_arg,
3002 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3004 err = got_object_blob_read_to_str(&link_target2,
3008 err = merge_symlink(a->worktree, NULL,
3009 ondisk_path, path2, a->label_orig,
3010 link_target2, a->commit_id2, repo,
3011 a->progress_cb, a->progress_arg);
3013 } else if (S_ISREG(sb.st_mode)) {
3014 err = merge_blob(&local_changes_subsumed,
3015 a->worktree, NULL, ondisk_path, path2,
3016 sb.st_mode, a->label_orig, blob2,
3017 a->commit_id2, repo, a->progress_cb,
3020 err = got_error_path(ondisk_path,
3021 GOT_ERR_FILE_OBSTRUCTED);
3025 if (status == GOT_STATUS_DELETE) {
3026 err = got_fileindex_entry_update(ie,
3027 a->worktree->root_fd, path2, blob2->id.sha1,
3028 a->worktree->base_commit_id->sha1, 0);
3033 int is_bad_symlink = 0;
3034 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3035 if (S_ISLNK(mode2)) {
3036 err = install_symlink(&is_bad_symlink,
3037 a->worktree, ondisk_path, path2, blob2, 0,
3038 0, 1, a->allow_bad_symlinks, repo,
3039 a->progress_cb, a->progress_arg);
3041 err = install_blob(a->worktree, ondisk_path, path2,
3042 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3043 a->progress_cb, a->progress_arg);
3047 err = got_fileindex_entry_alloc(&ie, path2);
3050 err = got_fileindex_entry_update(ie,
3051 a->worktree->root_fd, path2, NULL, NULL, 1);
3053 got_fileindex_entry_free(ie);
3056 err = got_fileindex_entry_add(a->fileindex, ie);
3058 got_fileindex_entry_free(ie);
3061 if (is_bad_symlink) {
3062 got_fileindex_entry_filetype_set(ie,
3063 GOT_FILEIDX_MODE_BAD_SYMLINK);
3068 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3069 err = got_error_from_errno("fclose");
3070 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3071 err = got_error_from_errno("fclose");
3072 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3073 err = got_error_from_errno("fclose");
3080 static const struct got_error *
3081 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3083 struct got_worktree *worktree = arg;
3085 /* Reject merges into a work tree with mixed base commits. */
3086 if (got_fileindex_entry_has_commit(ie) &&
3087 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3088 SHA1_DIGEST_LENGTH) != 0)
3089 return got_error(GOT_ERR_MIXED_COMMITS);
3094 struct check_merge_conflicts_arg {
3095 struct got_worktree *worktree;
3096 struct got_fileindex *fileindex;
3097 struct got_repository *repo;
3100 static const struct got_error *
3101 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3102 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3103 struct got_object_id *id1, struct got_object_id *id2,
3104 const char *path1, const char *path2,
3105 mode_t mode1, mode_t mode2, struct got_repository *repo)
3107 const struct got_error *err = NULL;
3108 struct check_merge_conflicts_arg *a = arg;
3109 unsigned char status;
3111 struct got_fileindex_entry *ie;
3112 const char *path = path2 ? path2 : path1;
3113 struct got_object_id *id = id2 ? id2 : id1;
3119 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3123 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3125 return got_error_from_errno("asprintf");
3127 /* Reject merges into a work tree with conflicted files. */
3128 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3132 if (status == GOT_STATUS_CONFLICT)
3133 return got_error(GOT_ERR_CONFLICTS);
3138 static const struct got_error *
3139 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3140 const char *fileindex_path, struct got_object_id *commit_id1,
3141 struct got_object_id *commit_id2, struct got_repository *repo,
3142 got_worktree_checkout_cb progress_cb, void *progress_arg,
3143 got_cancel_cb cancel_cb, void *cancel_arg)
3145 const struct got_error *err = NULL, *sync_err;
3146 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3147 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3148 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3149 struct check_merge_conflicts_arg cmc_arg;
3150 struct merge_file_cb_arg arg;
3151 char *label_orig = NULL;
3152 FILE *f1 = NULL, *f2 = NULL;
3153 int fd1 = -1, fd2 = -1;
3156 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3159 err = got_object_id_by_path(&tree_id1, repo, commit1,
3160 worktree->path_prefix);
3161 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3167 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3171 err = got_object_id_str(&id_str, commit_id1);
3175 if (asprintf(&label_orig, "%s: commit %s",
3176 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3177 err = got_error_from_errno("asprintf");
3183 f1 = got_opentemp();
3185 err = got_error_from_errno("got_opentemp");
3190 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3194 err = got_object_id_by_path(&tree_id2, repo, commit2,
3195 worktree->path_prefix);
3199 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3203 f2 = got_opentemp();
3205 err = got_error_from_errno("got_opentemp");
3209 fd1 = got_opentempfd();
3211 err = got_error_from_errno("got_opentempfd");
3215 fd2 = got_opentempfd();
3217 err = got_error_from_errno("got_opentempfd");
3221 cmc_arg.worktree = worktree;
3222 cmc_arg.fileindex = fileindex;
3223 cmc_arg.repo = repo;
3224 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3225 check_merge_conflicts, &cmc_arg, 0);
3229 arg.worktree = worktree;
3230 arg.fileindex = fileindex;
3231 arg.progress_cb = progress_cb;
3232 arg.progress_arg = progress_arg;
3233 arg.cancel_cb = cancel_cb;
3234 arg.cancel_arg = cancel_arg;
3235 arg.label_orig = label_orig;
3236 arg.commit_id2 = commit_id2;
3237 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3238 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3239 merge_file_cb, &arg, 1);
3240 sync_err = sync_fileindex(fileindex, fileindex_path);
3241 if (sync_err && err == NULL)
3245 got_object_commit_close(commit1);
3247 got_object_commit_close(commit2);
3249 got_object_tree_close(tree1);
3251 got_object_tree_close(tree2);
3252 if (f1 && fclose(f1) == EOF && err == NULL)
3253 err = got_error_from_errno("fclose");
3254 if (f2 && fclose(f2) == EOF && err == NULL)
3255 err = got_error_from_errno("fclose");
3256 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3257 err = got_error_from_errno("close");
3258 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3259 err = got_error_from_errno("close");
3264 const struct got_error *
3265 got_worktree_merge_files(struct got_worktree *worktree,
3266 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3267 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3268 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3270 const struct got_error *err, *unlockerr;
3271 char *fileindex_path = NULL;
3272 struct got_fileindex *fileindex = NULL;
3274 err = lock_worktree(worktree, LOCK_EX);
3278 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3282 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3287 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3288 commit_id2, repo, progress_cb, progress_arg,
3289 cancel_cb, cancel_arg);
3292 got_fileindex_free(fileindex);
3293 free(fileindex_path);
3294 unlockerr = lock_worktree(worktree, LOCK_SH);
3295 if (unlockerr && err == NULL)
3300 struct diff_dir_cb_arg {
3301 struct got_fileindex *fileindex;
3302 struct got_worktree *worktree;
3303 const char *status_path;
3304 size_t status_path_len;
3305 struct got_repository *repo;
3306 got_worktree_status_cb status_cb;
3308 got_cancel_cb cancel_cb;
3310 /* A pathlist containing per-directory pathlists of ignore patterns. */
3311 struct got_pathlist_head *ignores;
3312 int report_unchanged;
3316 static const struct got_error *
3317 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3318 int dirfd, const char *de_name,
3319 got_worktree_status_cb status_cb, void *status_arg,
3320 struct got_repository *repo, int report_unchanged)
3322 const struct got_error *err = NULL;
3323 unsigned char status = GOT_STATUS_NO_CHANGE;
3324 unsigned char staged_status = get_staged_status(ie);
3326 struct got_object_id blob_id, commit_id, staged_blob_id;
3327 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3328 struct got_object_id *staged_blob_idp = NULL;
3330 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3334 if (status == GOT_STATUS_NO_CHANGE &&
3335 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3338 if (got_fileindex_entry_has_blob(ie)) {
3339 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3340 blob_idp = &blob_id;
3342 if (got_fileindex_entry_has_commit(ie)) {
3343 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3344 commit_idp = &commit_id;
3346 if (staged_status == GOT_STATUS_ADD ||
3347 staged_status == GOT_STATUS_MODIFY) {
3348 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3349 SHA1_DIGEST_LENGTH);
3350 staged_blob_idp = &staged_blob_id;
3353 return (*status_cb)(status_arg, status, staged_status,
3354 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3357 static const struct got_error *
3358 status_old_new(void *arg, struct got_fileindex_entry *ie,
3359 struct dirent *de, const char *parent_path, int dirfd)
3361 const struct got_error *err = NULL;
3362 struct diff_dir_cb_arg *a = arg;
3365 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3366 return got_error(GOT_ERR_CANCELLED);
3368 if (got_path_cmp(parent_path, a->status_path,
3369 strlen(parent_path), a->status_path_len) != 0 &&
3370 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3373 if (parent_path[0]) {
3374 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3375 parent_path, de->d_name) == -1)
3376 return got_error_from_errno("asprintf");
3378 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3380 return got_error_from_errno("asprintf");
3383 err = report_file_status(ie, abspath, dirfd, de->d_name,
3384 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3389 static const struct got_error *
3390 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3392 struct diff_dir_cb_arg *a = arg;
3393 struct got_object_id blob_id, commit_id;
3394 unsigned char status;
3396 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3397 return got_error(GOT_ERR_CANCELLED);
3399 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3402 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3403 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3404 if (got_fileindex_entry_has_file_on_disk(ie))
3405 status = GOT_STATUS_MISSING;
3407 status = GOT_STATUS_DELETE;
3408 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3409 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3413 free_ignorelist(struct got_pathlist_head *ignorelist)
3415 struct got_pathlist_entry *pe;
3417 TAILQ_FOREACH(pe, ignorelist, entry)
3418 free((char *)pe->path);
3419 got_pathlist_free(ignorelist);
3423 free_ignores(struct got_pathlist_head *ignores)
3425 struct got_pathlist_entry *pe;
3427 TAILQ_FOREACH(pe, ignores, entry) {
3428 struct got_pathlist_head *ignorelist = pe->data;
3429 free_ignorelist(ignorelist);
3430 free((char *)pe->path);
3432 got_pathlist_free(ignores);
3435 static const struct got_error *
3436 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3438 const struct got_error *err = NULL;
3439 struct got_pathlist_entry *pe = NULL;
3440 struct got_pathlist_head *ignorelist;
3441 char *line = NULL, *pattern, *dirpath = NULL;
3442 size_t linesize = 0;
3445 ignorelist = calloc(1, sizeof(*ignorelist));
3446 if (ignorelist == NULL)
3447 return got_error_from_errno("calloc");
3448 TAILQ_INIT(ignorelist);
3450 while ((linelen = getline(&line, &linesize, f)) != -1) {
3451 if (linelen > 0 && line[linelen - 1] == '\n')
3452 line[linelen - 1] = '\0';
3454 /* Git's ignores may contain comments. */
3458 /* Git's negated patterns are not (yet?) supported. */
3462 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3464 err = got_error_from_errno("asprintf");
3467 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3472 err = got_error_from_errno("getline");
3476 dirpath = strdup(path);
3477 if (dirpath == NULL) {
3478 err = got_error_from_errno("strdup");
3481 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3484 if (err || pe == NULL) {
3486 free_ignorelist(ignorelist);
3492 match_ignores(struct got_pathlist_head *ignores, const char *path)
3494 struct got_pathlist_entry *pe;
3496 /* Handle patterns which match in all directories. */
3497 TAILQ_FOREACH(pe, ignores, entry) {
3498 struct got_pathlist_head *ignorelist = pe->data;
3499 struct got_pathlist_entry *pi;
3501 TAILQ_FOREACH(pi, ignorelist, entry) {
3502 const char *p, *pattern = pi->path;
3504 if (strncmp(pattern, "**/", 3) != 0)
3509 if (fnmatch(pattern, p,
3510 FNM_PATHNAME | FNM_LEADING_DIR)) {
3511 /* Retry in next directory. */
3512 while (*p && *p != '/')
3524 * The ignores pathlist contains ignore lists from children before
3525 * parents, so we can find the most specific ignorelist by walking
3526 * ignores backwards.
3528 pe = TAILQ_LAST(ignores, got_pathlist_head);
3530 if (got_path_is_child(path, pe->path, pe->path_len)) {
3531 struct got_pathlist_head *ignorelist = pe->data;
3532 struct got_pathlist_entry *pi;
3533 TAILQ_FOREACH(pi, ignorelist, entry) {
3534 const char *pattern = pi->path;
3535 int flags = FNM_LEADING_DIR;
3536 if (strstr(pattern, "/**/") == NULL)
3537 flags |= FNM_PATHNAME;
3538 if (fnmatch(pattern, path, flags))
3543 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3549 static const struct got_error *
3550 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3551 const char *path, int dirfd, const char *ignores_filename)
3553 const struct got_error *err = NULL;
3556 FILE *ignoresfile = NULL;
3558 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3559 path[0] ? "/" : "", ignores_filename) == -1)
3560 return got_error_from_errno("asprintf");
3563 fd = openat(dirfd, ignores_filename,
3564 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3566 if (errno != ENOENT && errno != EACCES)
3567 err = got_error_from_errno2("openat",
3570 ignoresfile = fdopen(fd, "r");
3571 if (ignoresfile == NULL)
3572 err = got_error_from_errno2("fdopen",
3576 err = read_ignores(ignores, path, ignoresfile);
3580 ignoresfile = fopen(ignorespath, "re");
3581 if (ignoresfile == NULL) {
3582 if (errno != ENOENT && errno != EACCES)
3583 err = got_error_from_errno2("fopen",
3586 err = read_ignores(ignores, path, ignoresfile);
3589 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3590 err = got_error_from_errno2("fclose", path);
3591 if (fd != -1 && close(fd) == -1 && err == NULL)
3592 err = got_error_from_errno2("close", path);
3597 static const struct got_error *
3598 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3601 const struct got_error *err = NULL;
3602 struct diff_dir_cb_arg *a = arg;
3608 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3609 return got_error(GOT_ERR_CANCELLED);
3611 if (parent_path[0]) {
3612 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3613 return got_error_from_errno("asprintf");
3618 if (de->d_type == DT_DIR) {
3619 if (!a->no_ignores && ignore != NULL &&
3620 match_ignores(a->ignores, path))
3622 } else if (!match_ignores(a->ignores, path) &&
3623 got_path_is_child(path, a->status_path, a->status_path_len))
3624 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3625 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3631 static const struct got_error *
3632 status_traverse(void *arg, const char *path, int dirfd)
3634 const struct got_error *err = NULL;
3635 struct diff_dir_cb_arg *a = arg;
3640 err = add_ignores(a->ignores, a->worktree->root_path,
3641 path, dirfd, ".cvsignore");
3645 err = add_ignores(a->ignores, a->worktree->root_path, path,
3646 dirfd, ".gitignore");
3651 static const struct got_error *
3652 report_single_file_status(const char *path, const char *ondisk_path,
3653 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3654 void *status_arg, struct got_repository *repo, int report_unchanged,
3655 struct got_pathlist_head *ignores, int no_ignores)
3657 struct got_fileindex_entry *ie;
3660 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3662 return report_file_status(ie, ondisk_path, -1, NULL,
3663 status_cb, status_arg, repo, report_unchanged);
3665 if (lstat(ondisk_path, &sb) == -1) {
3666 if (errno != ENOENT)
3667 return got_error_from_errno2("lstat", ondisk_path);
3668 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3669 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3672 if (!no_ignores && match_ignores(ignores, path))
3675 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3676 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3677 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3682 static const struct got_error *
3683 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3684 const char *root_path, const char *path)
3686 const struct got_error *err;
3687 char *parent_path, *next_parent_path = NULL;
3689 err = add_ignores(ignores, root_path, "", -1,
3694 err = add_ignores(ignores, root_path, "", -1,
3699 err = got_path_dirname(&parent_path, path);
3701 if (err->code == GOT_ERR_BAD_PATH)
3702 return NULL; /* cannot traverse parent */
3706 err = add_ignores(ignores, root_path, parent_path, -1,
3710 err = add_ignores(ignores, root_path, parent_path, -1,
3714 err = got_path_dirname(&next_parent_path, parent_path);
3716 if (err->code == GOT_ERR_BAD_PATH)
3717 err = NULL; /* traversed everything */
3720 if (got_path_is_root_dir(parent_path))
3723 parent_path = next_parent_path;
3724 next_parent_path = NULL;
3728 free(next_parent_path);
3732 static const struct got_error *
3733 worktree_status(struct got_worktree *worktree, const char *path,
3734 struct got_fileindex *fileindex, struct got_repository *repo,
3735 got_worktree_status_cb status_cb, void *status_arg,
3736 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3737 int report_unchanged)
3739 const struct got_error *err = NULL;
3741 struct got_fileindex_diff_dir_cb fdiff_cb;
3742 struct diff_dir_cb_arg arg;
3743 char *ondisk_path = NULL;
3744 struct got_pathlist_head ignores;
3745 struct got_fileindex_entry *ie;
3747 TAILQ_INIT(&ignores);
3749 if (asprintf(&ondisk_path, "%s%s%s",
3750 worktree->root_path, path[0] ? "/" : "", path) == -1)
3751 return got_error_from_errno("asprintf");
3753 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3755 err = report_single_file_status(path, ondisk_path,
3756 fileindex, status_cb, status_arg, repo,
3757 report_unchanged, &ignores, no_ignores);
3761 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3763 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3764 !got_err_open_nofollow_on_symlink())
3765 err = got_error_from_errno2("open", ondisk_path);
3768 err = add_ignores_from_parent_paths(&ignores,
3769 worktree->root_path, ondisk_path);
3773 err = report_single_file_status(path, ondisk_path,
3774 fileindex, status_cb, status_arg, repo,
3775 report_unchanged, &ignores, no_ignores);
3778 fdiff_cb.diff_old_new = status_old_new;
3779 fdiff_cb.diff_old = status_old;
3780 fdiff_cb.diff_new = status_new;
3781 fdiff_cb.diff_traverse = status_traverse;
3782 arg.fileindex = fileindex;
3783 arg.worktree = worktree;
3784 arg.status_path = path;
3785 arg.status_path_len = strlen(path);
3787 arg.status_cb = status_cb;
3788 arg.status_arg = status_arg;
3789 arg.cancel_cb = cancel_cb;
3790 arg.cancel_arg = cancel_arg;
3791 arg.report_unchanged = report_unchanged;
3792 arg.no_ignores = no_ignores;
3794 err = add_ignores_from_parent_paths(&ignores,
3795 worktree->root_path, path);
3799 arg.ignores = &ignores;
3800 err = got_fileindex_diff_dir(fileindex, fd,
3801 worktree->root_path, path, repo, &fdiff_cb, &arg);
3804 free_ignores(&ignores);
3805 if (fd != -1 && close(fd) == -1 && err == NULL)
3806 err = got_error_from_errno("close");
3811 const struct got_error *
3812 got_worktree_status(struct got_worktree *worktree,
3813 struct got_pathlist_head *paths, struct got_repository *repo,
3814 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3815 got_cancel_cb cancel_cb, void *cancel_arg)
3817 const struct got_error *err = NULL;
3818 char *fileindex_path = NULL;
3819 struct got_fileindex *fileindex = NULL;
3820 struct got_pathlist_entry *pe;
3822 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3826 TAILQ_FOREACH(pe, paths, entry) {
3827 err = worktree_status(worktree, pe->path, fileindex, repo,
3828 status_cb, status_arg, cancel_cb, cancel_arg,
3833 free(fileindex_path);
3834 got_fileindex_free(fileindex);
3838 const struct got_error *
3839 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3842 const struct got_error *err = NULL;
3843 char *resolved = NULL, *cwd = NULL, *path = NULL;
3846 char *abspath = NULL;
3847 char canonpath[PATH_MAX];
3851 cwd = getcwd(NULL, 0);
3853 return got_error_from_errno("getcwd");
3855 if (lstat(arg, &sb) == -1) {
3856 if (errno != ENOENT) {
3857 err = got_error_from_errno2("lstat", arg);
3862 if (S_ISLNK(sb.st_mode)) {
3864 * We cannot use realpath(3) with symlinks since we want to
3865 * operate on the symlink itself.
3866 * But we can make the path absolute, assuming it is relative
3867 * to the current working directory, and then canonicalize it.
3869 if (!got_path_is_absolute(arg)) {
3870 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3871 err = got_error_from_errno("asprintf");
3876 err = got_canonpath(abspath ? abspath : arg, canonpath,
3880 resolved = strdup(canonpath);
3881 if (resolved == NULL) {
3882 err = got_error_from_errno("strdup");
3886 resolved = realpath(arg, NULL);
3887 if (resolved == NULL) {
3888 if (errno != ENOENT) {
3889 err = got_error_from_errno2("realpath", arg);
3892 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3893 err = got_error_from_errno("asprintf");
3896 err = got_canonpath(abspath, canonpath,
3900 resolved = strdup(canonpath);
3901 if (resolved == NULL) {
3902 err = got_error_from_errno("strdup");
3908 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3909 strlen(got_worktree_get_root_path(worktree)))) {
3910 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3914 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3915 err = got_path_skip_common_ancestor(&path,
3916 got_worktree_get_root_path(worktree), resolved);
3922 err = got_error_from_errno("strdup");
3927 /* XXX status walk can't deal with trailing slash! */
3929 while (len > 0 && path[len - 1] == '/') {
3930 path[len - 1] = '\0';
3944 struct schedule_addition_args {
3945 struct got_worktree *worktree;
3946 struct got_fileindex *fileindex;
3947 got_worktree_checkout_cb progress_cb;
3949 struct got_repository *repo;
3952 static const struct got_error *
3953 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3954 const char *relpath, struct got_object_id *blob_id,
3955 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3956 int dirfd, const char *de_name)
3958 struct schedule_addition_args *a = arg;
3959 const struct got_error *err = NULL;
3960 struct got_fileindex_entry *ie;
3964 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3966 return got_error_from_errno("asprintf");
3968 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3970 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3974 /* Re-adding an existing entry is a no-op. */
3975 if (status == GOT_STATUS_ADD)
3977 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3982 if (status != GOT_STATUS_UNVERSIONED) {
3983 if (status == GOT_STATUS_NONEXISTENT)
3984 err = got_error_set_errno(ENOENT, ondisk_path);
3986 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3990 err = got_fileindex_entry_alloc(&ie, relpath);
3993 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3994 relpath, NULL, NULL, 1);
3996 got_fileindex_entry_free(ie);
3999 err = got_fileindex_entry_add(a->fileindex, ie);
4001 got_fileindex_entry_free(ie);
4008 if (status == GOT_STATUS_ADD)
4010 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4013 const struct got_error *
4014 got_worktree_schedule_add(struct got_worktree *worktree,
4015 struct got_pathlist_head *paths,
4016 got_worktree_checkout_cb progress_cb, void *progress_arg,
4017 struct got_repository *repo, int no_ignores)
4019 struct got_fileindex *fileindex = NULL;
4020 char *fileindex_path = NULL;
4021 const struct got_error *err = NULL, *sync_err, *unlockerr;
4022 struct got_pathlist_entry *pe;
4023 struct schedule_addition_args saa;
4025 err = lock_worktree(worktree, LOCK_EX);
4029 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4033 saa.worktree = worktree;
4034 saa.fileindex = fileindex;
4035 saa.progress_cb = progress_cb;
4036 saa.progress_arg = progress_arg;
4039 TAILQ_FOREACH(pe, paths, entry) {
4040 err = worktree_status(worktree, pe->path, fileindex, repo,
4041 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4045 sync_err = sync_fileindex(fileindex, fileindex_path);
4046 if (sync_err && err == NULL)
4049 free(fileindex_path);
4051 got_fileindex_free(fileindex);
4052 unlockerr = lock_worktree(worktree, LOCK_SH);
4053 if (unlockerr && err == NULL)
4058 struct schedule_deletion_args {
4059 struct got_worktree *worktree;
4060 struct got_fileindex *fileindex;
4061 got_worktree_delete_cb progress_cb;
4063 struct got_repository *repo;
4064 int delete_local_mods;
4066 int ignore_missing_paths;
4067 const char *status_codes;
4070 static const struct got_error *
4071 schedule_for_deletion(void *arg, unsigned char status,
4072 unsigned char staged_status, const char *relpath,
4073 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4074 struct got_object_id *commit_id, int dirfd, const char *de_name)
4076 struct schedule_deletion_args *a = arg;
4077 const struct got_error *err = NULL;
4078 struct got_fileindex_entry *ie = NULL;
4082 if (status == GOT_STATUS_NONEXISTENT) {
4083 if (a->ignore_missing_paths)
4085 return got_error_set_errno(ENOENT, relpath);
4088 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4090 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4092 staged_status = get_staged_status(ie);
4093 if (staged_status != GOT_STATUS_NO_CHANGE) {
4094 if (staged_status == GOT_STATUS_DELETE)
4096 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4099 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4101 return got_error_from_errno("asprintf");
4103 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4108 if (a->status_codes) {
4109 size_t ncodes = strlen(a->status_codes);
4111 for (i = 0; i < ncodes ; i++) {
4112 if (status == a->status_codes[i])
4116 /* Do not delete files in non-matching status. */
4120 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4121 a->status_codes[i] != GOT_STATUS_MISSING) {
4122 static char msg[64];
4123 snprintf(msg, sizeof(msg),
4124 "invalid status code '%c'", a->status_codes[i]);
4125 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4130 if (status != GOT_STATUS_NO_CHANGE) {
4131 if (status == GOT_STATUS_DELETE)
4133 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4134 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4137 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4138 err = got_error_set_errno(ENOENT, relpath);
4141 if (status != GOT_STATUS_MODIFY &&
4142 status != GOT_STATUS_MISSING) {
4143 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4148 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4152 if (unlinkat(dirfd, de_name, 0) == -1) {
4153 err = got_error_from_errno2("unlinkat",
4157 } else if (unlink(ondisk_path) == -1) {
4158 err = got_error_from_errno2("unlink", ondisk_path);
4162 root_len = strlen(a->worktree->root_path);
4165 err = got_path_dirname(&parent, ondisk_path);
4169 ondisk_path = parent;
4170 if (rmdir(ondisk_path) == -1) {
4171 if (errno != ENOTEMPTY)
4172 err = got_error_from_errno2("rmdir",
4176 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4177 strlen(ondisk_path), root_len) != 0);
4180 got_fileindex_entry_mark_deleted_from_disk(ie);
4185 if (status == GOT_STATUS_DELETE)
4187 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4188 staged_status, relpath);
4191 const struct got_error *
4192 got_worktree_schedule_delete(struct got_worktree *worktree,
4193 struct got_pathlist_head *paths, int delete_local_mods,
4194 const char *status_codes,
4195 got_worktree_delete_cb progress_cb, void *progress_arg,
4196 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4198 struct got_fileindex *fileindex = NULL;
4199 char *fileindex_path = NULL;
4200 const struct got_error *err = NULL, *sync_err, *unlockerr;
4201 struct got_pathlist_entry *pe;
4202 struct schedule_deletion_args sda;
4204 err = lock_worktree(worktree, LOCK_EX);
4208 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4212 sda.worktree = worktree;
4213 sda.fileindex = fileindex;
4214 sda.progress_cb = progress_cb;
4215 sda.progress_arg = progress_arg;
4217 sda.delete_local_mods = delete_local_mods;
4218 sda.keep_on_disk = keep_on_disk;
4219 sda.ignore_missing_paths = ignore_missing_paths;
4220 sda.status_codes = status_codes;
4222 TAILQ_FOREACH(pe, paths, entry) {
4223 err = worktree_status(worktree, pe->path, fileindex, repo,
4224 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4228 sync_err = sync_fileindex(fileindex, fileindex_path);
4229 if (sync_err && err == NULL)
4232 free(fileindex_path);
4234 got_fileindex_free(fileindex);
4235 unlockerr = lock_worktree(worktree, LOCK_SH);
4236 if (unlockerr && err == NULL)
4241 static const struct got_error *
4242 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4244 const struct got_error *err = NULL;
4246 size_t linesize = 0, n;
4249 linelen = getline(&line, &linesize, infile);
4250 if (linelen == -1) {
4251 if (ferror(infile)) {
4252 err = got_error_from_errno("getline");
4258 n = fwrite(line, 1, linelen, outfile);
4260 err = got_ferror(outfile, GOT_ERR_IO);
4265 n = fwrite(line, 1, linelen, rejectfile);
4267 err = got_ferror(outfile, GOT_ERR_IO);
4274 static const struct got_error *
4275 skip_one_line(FILE *f)
4278 size_t linesize = 0;
4281 linelen = getline(&line, &linesize, f);
4282 if (linelen == -1) {
4284 return got_error_from_errno("getline");
4291 static const struct got_error *
4292 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4293 int start_old, int end_old, int start_new, int end_new,
4294 FILE *outfile, FILE *rejectfile)
4296 const struct got_error *err;
4298 /* Copy old file's lines leading up to patch. */
4299 while (!feof(f1) && *line_cur1 < start_old) {
4300 err = copy_one_line(f1, outfile, NULL);
4305 /* Skip new file's lines leading up to patch. */
4306 while (!feof(f2) && *line_cur2 < start_new) {
4308 err = copy_one_line(f2, NULL, rejectfile);
4310 err = skip_one_line(f2);
4315 /* Copy patched lines. */
4316 while (!feof(f2) && *line_cur2 <= end_new) {
4317 err = copy_one_line(f2, outfile, NULL);
4322 /* Skip over old file's replaced lines. */
4323 while (!feof(f1) && *line_cur1 <= end_old) {
4325 err = copy_one_line(f1, NULL, rejectfile);
4327 err = skip_one_line(f1);
4336 static const struct got_error *
4337 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4338 FILE *outfile, FILE *rejectfile)
4340 const struct got_error *err;
4343 /* Copy old file's lines until EOF. */
4345 err = copy_one_line(f1, outfile, NULL);
4352 /* Copy new file's lines until EOF. */
4354 err = copy_one_line(f2, NULL, rejectfile);
4364 static const struct got_error *
4365 apply_or_reject_change(int *choice, int *nchunks_used,
4366 struct diff_result *diff_result, int n,
4367 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4368 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4369 got_worktree_patch_cb patch_cb, void *patch_arg)
4371 const struct got_error *err = NULL;
4372 struct diff_chunk_context cc = {};
4373 int start_old, end_old, start_new, end_new;
4375 struct diff_output_unidiff_state *diff_state;
4376 struct diff_input_info diff_info;
4379 *choice = GOT_PATCH_CHOICE_NONE;
4381 /* Get changed line numbers without context lines for copy_change(). */
4382 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4383 start_old = cc.left.start;
4384 end_old = cc.left.end;
4385 start_new = cc.right.start;
4386 end_new = cc.right.end;
4388 /* Get the same change with context lines for display. */
4389 memset(&cc, 0, sizeof(cc));
4390 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4392 memset(&diff_info, 0, sizeof(diff_info));
4393 diff_info.left_path = relpath;
4394 diff_info.right_path = relpath;
4396 diff_state = diff_output_unidiff_state_alloc();
4397 if (diff_state == NULL)
4398 return got_error_set_errno(ENOMEM,
4399 "diff_output_unidiff_state_alloc");
4401 hunkfile = got_opentemp();
4402 if (hunkfile == NULL) {
4403 err = got_error_from_errno("got_opentemp");
4407 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4409 if (rc != DIFF_RC_OK) {
4410 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4414 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4415 err = got_ferror(hunkfile, GOT_ERR_IO);
4419 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4420 hunkfile, changeno, nchanges);
4425 case GOT_PATCH_CHOICE_YES:
4426 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4427 end_old, start_new, end_new, outfile, rejectfile);
4429 case GOT_PATCH_CHOICE_NO:
4430 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4431 end_old, start_new, end_new, rejectfile, outfile);
4433 case GOT_PATCH_CHOICE_QUIT:
4436 err = got_error(GOT_ERR_PATCH_CHOICE);
4440 diff_output_unidiff_state_free(diff_state);
4441 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4442 err = got_error_from_errno("fclose");
4446 struct revert_file_args {
4447 struct got_worktree *worktree;
4448 struct got_fileindex *fileindex;
4449 got_worktree_checkout_cb progress_cb;
4451 got_worktree_patch_cb patch_cb;
4453 struct got_repository *repo;
4454 int unlink_added_files;
4457 static const struct got_error *
4458 create_patched_content(char **path_outfile, int reverse_patch,
4459 struct got_object_id *blob_id, const char *path2,
4460 int dirfd2, const char *de_name2,
4461 const char *relpath, struct got_repository *repo,
4462 got_worktree_patch_cb patch_cb, void *patch_arg)
4464 const struct got_error *err, *free_err;
4465 struct got_blob_object *blob = NULL;
4466 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4467 int fd = -1, fd2 = -1;
4468 char link_target[PATH_MAX];
4469 ssize_t link_len = 0;
4470 char *path1 = NULL, *id_str = NULL;
4472 struct got_diffreg_result *diffreg_result = NULL;
4473 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4474 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4476 *path_outfile = NULL;
4478 err = got_object_id_str(&id_str, blob_id);
4483 fd2 = openat(dirfd2, de_name2,
4484 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4486 if (!got_err_open_nofollow_on_symlink()) {
4487 err = got_error_from_errno2("openat", path2);
4490 link_len = readlinkat(dirfd2, de_name2,
4491 link_target, sizeof(link_target));
4492 if (link_len == -1) {
4493 return got_error_from_errno2("readlinkat",
4496 sb2.st_mode = S_IFLNK;
4497 sb2.st_size = link_len;
4500 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4502 if (!got_err_open_nofollow_on_symlink()) {
4503 err = got_error_from_errno2("open", path2);
4506 link_len = readlink(path2, link_target,
4507 sizeof(link_target));
4509 return got_error_from_errno2("readlink", path2);
4510 sb2.st_mode = S_IFLNK;
4511 sb2.st_size = link_len;
4515 if (fstat(fd2, &sb2) == -1) {
4516 err = got_error_from_errno2("fstat", path2);
4520 f2 = fdopen(fd2, "r");
4522 err = got_error_from_errno2("fdopen", path2);
4528 f2 = got_opentemp();
4530 err = got_error_from_errno2("got_opentemp", path2);
4533 n = fwrite(link_target, 1, link_len, f2);
4534 if (n != link_len) {
4535 err = got_ferror(f2, GOT_ERR_IO);
4538 if (fflush(f2) == EOF) {
4539 err = got_error_from_errno("fflush");
4545 fd = got_opentempfd();
4547 err = got_error_from_errno("got_opentempfd");
4551 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4555 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4559 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4563 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4564 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4568 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4572 if (fseek(f1, 0L, SEEK_SET) == -1)
4573 return got_ferror(f1, GOT_ERR_IO);
4574 if (fseek(f2, 0L, SEEK_SET) == -1)
4575 return got_ferror(f2, GOT_ERR_IO);
4577 /* Count the number of actual changes in the diff result. */
4578 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4579 struct diff_chunk_context cc = {};
4580 diff_chunk_context_load_change(&cc, &nchunks_used,
4581 diffreg_result->result, n, 0);
4584 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4586 err = apply_or_reject_change(&choice, &nchunks_used,
4587 diffreg_result->result, n, relpath, f1, f2,
4588 &line_cur1, &line_cur2,
4589 reverse_patch ? NULL : outfile,
4590 reverse_patch ? outfile : NULL,
4591 ++i, nchanges, patch_cb, patch_arg);
4594 if (choice == GOT_PATCH_CHOICE_YES)
4596 else if (choice == GOT_PATCH_CHOICE_QUIT)
4600 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4601 reverse_patch ? NULL : outfile,
4602 reverse_patch ? outfile : NULL);
4606 if (!S_ISLNK(sb2.st_mode)) {
4609 mode = apply_umask(sb2.st_mode);
4610 if (fchmod(fileno(outfile), mode) == -1) {
4611 err = got_error_from_errno2("fchmod", path2);
4618 if (fd != -1 && close(fd) == -1 && err == NULL)
4619 err = got_error_from_errno("close");
4621 got_object_blob_close(blob);
4622 free_err = got_diffreg_result_free(diffreg_result);
4625 if (f1 && fclose(f1) == EOF && err == NULL)
4626 err = got_error_from_errno2("fclose", path1);
4627 if (f2 && fclose(f2) == EOF && err == NULL)
4628 err = got_error_from_errno2("fclose", path2);
4629 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4630 err = got_error_from_errno2("close", path2);
4631 if (outfile && fclose(outfile) == EOF && err == NULL)
4632 err = got_error_from_errno2("fclose", *path_outfile);
4633 if (path1 && unlink(path1) == -1 && err == NULL)
4634 err = got_error_from_errno2("unlink", path1);
4635 if (err || !have_content) {
4636 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4637 err = got_error_from_errno2("unlink", *path_outfile);
4638 free(*path_outfile);
4639 *path_outfile = NULL;
4645 static const struct got_error *
4646 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4647 const char *relpath, struct got_object_id *blob_id,
4648 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4649 int dirfd, const char *de_name)
4651 struct revert_file_args *a = arg;
4652 const struct got_error *err = NULL;
4653 char *parent_path = NULL;
4654 struct got_fileindex_entry *ie;
4655 struct got_commit_object *base_commit = NULL;
4656 struct got_tree_object *tree = NULL;
4657 struct got_object_id *tree_id = NULL;
4658 const struct got_tree_entry *te = NULL;
4659 char *tree_path = NULL, *te_name;
4660 char *ondisk_path = NULL, *path_content = NULL;
4661 struct got_blob_object *blob = NULL;
4664 /* Reverting a staged deletion is a no-op. */
4665 if (status == GOT_STATUS_DELETE &&
4666 staged_status != GOT_STATUS_NO_CHANGE)
4669 if (status == GOT_STATUS_UNVERSIONED)
4670 return (*a->progress_cb)(a->progress_arg,
4671 GOT_STATUS_UNVERSIONED, relpath);
4673 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4675 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4677 /* Construct in-repository path of tree which contains this blob. */
4678 err = got_path_dirname(&parent_path, ie->path);
4680 if (err->code != GOT_ERR_BAD_PATH)
4682 parent_path = strdup("/");
4683 if (parent_path == NULL) {
4684 err = got_error_from_errno("strdup");
4688 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4689 tree_path = strdup(parent_path);
4690 if (tree_path == NULL) {
4691 err = got_error_from_errno("strdup");
4695 if (got_path_is_root_dir(parent_path)) {
4696 tree_path = strdup(a->worktree->path_prefix);
4697 if (tree_path == NULL) {
4698 err = got_error_from_errno("strdup");
4702 if (asprintf(&tree_path, "%s/%s",
4703 a->worktree->path_prefix, parent_path) == -1) {
4704 err = got_error_from_errno("asprintf");
4710 err = got_object_open_as_commit(&base_commit, a->repo,
4711 a->worktree->base_commit_id);
4715 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4717 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4718 (status == GOT_STATUS_ADD ||
4719 staged_status == GOT_STATUS_ADD)))
4722 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4726 err = got_path_basename(&te_name, ie->path);
4730 te = got_object_tree_find_entry(tree, te_name);
4732 if (te == NULL && status != GOT_STATUS_ADD &&
4733 staged_status != GOT_STATUS_ADD) {
4734 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4740 case GOT_STATUS_ADD:
4742 int choice = GOT_PATCH_CHOICE_NONE;
4743 err = (*a->patch_cb)(&choice, a->patch_arg,
4744 status, ie->path, NULL, 1, 1);
4747 if (choice != GOT_PATCH_CHOICE_YES)
4750 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4754 got_fileindex_entry_remove(a->fileindex, ie);
4755 if (a->unlink_added_files) {
4756 if (asprintf(&ondisk_path, "%s/%s",
4757 got_worktree_get_root_path(a->worktree),
4759 err = got_error_from_errno("asprintf");
4762 if (unlink(ondisk_path) == -1) {
4763 err = got_error_from_errno2("unlink",
4769 case GOT_STATUS_DELETE:
4771 int choice = GOT_PATCH_CHOICE_NONE;
4772 err = (*a->patch_cb)(&choice, a->patch_arg,
4773 status, ie->path, NULL, 1, 1);
4776 if (choice != GOT_PATCH_CHOICE_YES)
4780 case GOT_STATUS_MODIFY:
4781 case GOT_STATUS_MODE_CHANGE:
4782 case GOT_STATUS_CONFLICT:
4783 case GOT_STATUS_MISSING: {
4784 struct got_object_id id;
4785 if (staged_status == GOT_STATUS_ADD ||
4786 staged_status == GOT_STATUS_MODIFY) {
4787 memcpy(id.sha1, ie->staged_blob_sha1,
4788 SHA1_DIGEST_LENGTH);
4790 memcpy(id.sha1, ie->blob_sha1,
4791 SHA1_DIGEST_LENGTH);
4792 fd = got_opentempfd();
4794 err = got_error_from_errno("got_opentempfd");
4798 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4802 if (asprintf(&ondisk_path, "%s/%s",
4803 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4804 err = got_error_from_errno("asprintf");
4808 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4809 status == GOT_STATUS_CONFLICT)) {
4810 int is_bad_symlink = 0;
4811 err = create_patched_content(&path_content, 1, &id,
4812 ondisk_path, dirfd, de_name, ie->path, a->repo,
4813 a->patch_cb, a->patch_arg);
4814 if (err || path_content == NULL)
4816 if (te && S_ISLNK(te->mode)) {
4817 if (unlink(path_content) == -1) {
4818 err = got_error_from_errno2("unlink",
4822 err = install_symlink(&is_bad_symlink,
4823 a->worktree, ondisk_path, ie->path,
4824 blob, 0, 1, 0, 0, a->repo,
4825 a->progress_cb, a->progress_arg);
4827 if (rename(path_content, ondisk_path) == -1) {
4828 err = got_error_from_errno3("rename",
4829 path_content, ondisk_path);
4834 int is_bad_symlink = 0;
4835 if (te && S_ISLNK(te->mode)) {
4836 err = install_symlink(&is_bad_symlink,
4837 a->worktree, ondisk_path, ie->path,
4838 blob, 0, 1, 0, 0, a->repo,
4839 a->progress_cb, a->progress_arg);
4841 err = install_blob(a->worktree, ondisk_path,
4843 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4844 got_fileindex_perms_to_st(ie), blob,
4845 0, 1, 0, 0, a->repo,
4846 a->progress_cb, a->progress_arg);
4850 if (status == GOT_STATUS_DELETE ||
4851 status == GOT_STATUS_MODE_CHANGE) {
4852 err = got_fileindex_entry_update(ie,
4853 a->worktree->root_fd, relpath,
4855 a->worktree->base_commit_id->sha1, 1);
4859 if (is_bad_symlink) {
4860 got_fileindex_entry_filetype_set(ie,
4861 GOT_FILEIDX_MODE_BAD_SYMLINK);
4874 if (fd != -1 && close(fd) == -1 && err == NULL)
4875 err = got_error_from_errno("close");
4877 got_object_blob_close(blob);
4879 got_object_tree_close(tree);
4882 got_object_commit_close(base_commit);
4886 const struct got_error *
4887 got_worktree_revert(struct got_worktree *worktree,
4888 struct got_pathlist_head *paths,
4889 got_worktree_checkout_cb progress_cb, void *progress_arg,
4890 got_worktree_patch_cb patch_cb, void *patch_arg,
4891 struct got_repository *repo)
4893 struct got_fileindex *fileindex = NULL;
4894 char *fileindex_path = NULL;
4895 const struct got_error *err = NULL, *unlockerr = NULL;
4896 const struct got_error *sync_err = NULL;
4897 struct got_pathlist_entry *pe;
4898 struct revert_file_args rfa;
4900 err = lock_worktree(worktree, LOCK_EX);
4904 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4908 rfa.worktree = worktree;
4909 rfa.fileindex = fileindex;
4910 rfa.progress_cb = progress_cb;
4911 rfa.progress_arg = progress_arg;
4912 rfa.patch_cb = patch_cb;
4913 rfa.patch_arg = patch_arg;
4915 rfa.unlink_added_files = 0;
4916 TAILQ_FOREACH(pe, paths, entry) {
4917 err = worktree_status(worktree, pe->path, fileindex, repo,
4918 revert_file, &rfa, NULL, NULL, 1, 0);
4922 sync_err = sync_fileindex(fileindex, fileindex_path);
4923 if (sync_err && err == NULL)
4926 free(fileindex_path);
4928 got_fileindex_free(fileindex);
4929 unlockerr = lock_worktree(worktree, LOCK_SH);
4930 if (unlockerr && err == NULL)
4936 free_commitable(struct got_commitable *ct)
4939 free(ct->in_repo_path);
4940 free(ct->ondisk_path);
4942 free(ct->base_blob_id);
4943 free(ct->staged_blob_id);
4944 free(ct->base_commit_id);
4948 struct collect_commitables_arg {
4949 struct got_pathlist_head *commitable_paths;
4950 struct got_repository *repo;
4951 struct got_worktree *worktree;
4952 struct got_fileindex *fileindex;
4953 int have_staged_files;
4954 int allow_bad_symlinks;
4955 int diff_header_shown;
4962 * Create a file which contains the target path of a symlink so we can feed
4963 * it as content to the diff engine.
4965 static const struct got_error *
4966 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4967 const char *abspath)
4969 const struct got_error *err = NULL;
4970 char target_path[PATH_MAX];
4971 ssize_t target_len, outlen;
4976 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4977 if (target_len == -1)
4978 return got_error_from_errno2("readlinkat", abspath);
4980 target_len = readlink(abspath, target_path, PATH_MAX);
4981 if (target_len == -1)
4982 return got_error_from_errno2("readlink", abspath);
4985 *fd = got_opentempfd();
4987 return got_error_from_errno("got_opentempfd");
4989 outlen = write(*fd, target_path, target_len);
4991 err = got_error_from_errno("got_opentempfd");
4995 if (lseek(*fd, 0, SEEK_SET) == -1) {
4996 err = got_error_from_errno2("lseek", abspath);
5007 static const struct got_error *
5008 append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5009 FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5010 struct got_repository *repo, struct got_worktree *worktree)
5012 const struct got_error *err = NULL;
5013 struct got_blob_object *blob1 = NULL;
5014 int fd = -1, fd1 = -1, fd2 = -1;
5015 FILE *ondisk_file = NULL;
5016 char *label1 = NULL;
5020 int diff_staged = (ct->staged_status != GOT_STATUS_NO_CHANGE);
5021 char *id_str = NULL;
5023 memset(&sb, 0, sizeof(sb));
5025 err = got_opentemp_truncate(f1);
5027 return got_error_from_errno("got_opentemp_truncate");
5028 err = got_opentemp_truncate(f2);
5030 return got_error_from_errno("got_opentemp_truncate");
5032 if (!*diff_header_shown) {
5033 err = got_object_id_str(&id_str, worktree->base_commit_id);
5036 fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5037 got_worktree_get_root_path(worktree));
5038 fprintf(diff_outfile, "commit - %s\n", id_str);
5039 fprintf(diff_outfile, "path + %s%s\n",
5040 got_worktree_get_root_path(worktree),
5041 diff_staged ? " (staged changes)" : "");
5042 *diff_header_shown = 1;
5046 const char *label1 = NULL, *label2 = NULL;
5047 switch (ct->staged_status) {
5048 case GOT_STATUS_MODIFY:
5052 case GOT_STATUS_ADD:
5055 case GOT_STATUS_DELETE:
5059 return got_error(GOT_ERR_FILE_STATUS);
5061 fd1 = got_opentempfd();
5063 err = got_error_from_errno("got_opentempfd");
5066 fd2 = got_opentempfd();
5068 err = got_error_from_errno("got_opentempfd");
5071 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5072 fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5073 label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5074 repo, diff_outfile);
5078 fd1 = got_opentempfd();
5080 err = got_error_from_errno("got_opentempfd");
5084 if (ct->status != GOT_STATUS_ADD) {
5085 err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5091 if (ct->status != GOT_STATUS_DELETE) {
5093 fd = openat(dirfd, de_name,
5094 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5096 if (!got_err_open_nofollow_on_symlink()) {
5097 err = got_error_from_errno2("openat",
5101 err = get_symlink_target_file(&fd, dirfd,
5102 de_name, ct->ondisk_path);
5107 fd = open(ct->ondisk_path,
5108 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5110 if (!got_err_open_nofollow_on_symlink()) {
5111 err = got_error_from_errno2("open",
5115 err = get_symlink_target_file(&fd, dirfd,
5116 de_name, ct->ondisk_path);
5121 if (fstatat(fd, ct->ondisk_path, &sb,
5122 AT_SYMLINK_NOFOLLOW) == -1) {
5123 err = got_error_from_errno2("fstatat", ct->ondisk_path);
5126 ondisk_file = fdopen(fd, "r");
5127 if (ondisk_file == NULL) {
5128 err = got_error_from_errno2("fdopen", ct->ondisk_path);
5136 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5142 err = got_diff_blob_file(blob1, f1, size1, label1,
5143 ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5144 GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, diff_outfile);
5146 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5147 err = got_error_from_errno("close");
5148 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5149 err = got_error_from_errno("close");
5151 got_object_blob_close(blob1);
5152 if (fd != -1 && close(fd) == -1 && err == NULL)
5153 err = got_error_from_errno("close");
5154 if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5155 err = got_error_from_errno("fclose");
5159 static const struct got_error *
5160 collect_commitables(void *arg, unsigned char status,
5161 unsigned char staged_status, const char *relpath,
5162 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5163 struct got_object_id *commit_id, int dirfd, const char *de_name)
5165 struct collect_commitables_arg *a = arg;
5166 const struct got_error *err = NULL;
5167 struct got_commitable *ct = NULL;
5168 struct got_pathlist_entry *new = NULL;
5169 char *parent_path = NULL, *path = NULL;
5172 if (a->have_staged_files) {
5173 if (staged_status != GOT_STATUS_MODIFY &&
5174 staged_status != GOT_STATUS_ADD &&
5175 staged_status != GOT_STATUS_DELETE)
5178 if (status == GOT_STATUS_CONFLICT)
5179 return got_error(GOT_ERR_COMMIT_CONFLICT);
5181 if (status != GOT_STATUS_MODIFY &&
5182 status != GOT_STATUS_MODE_CHANGE &&
5183 status != GOT_STATUS_ADD &&
5184 status != GOT_STATUS_DELETE)
5188 if (asprintf(&path, "/%s", relpath) == -1) {
5189 err = got_error_from_errno("asprintf");
5192 if (strcmp(path, "/") == 0) {
5193 parent_path = strdup("");
5194 if (parent_path == NULL)
5195 return got_error_from_errno("strdup");
5197 err = got_path_dirname(&parent_path, path);
5202 ct = calloc(1, sizeof(*ct));
5204 err = got_error_from_errno("calloc");
5208 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5210 err = got_error_from_errno("asprintf");
5214 if (staged_status == GOT_STATUS_ADD ||
5215 staged_status == GOT_STATUS_MODIFY) {
5216 struct got_fileindex_entry *ie;
5217 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5218 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5219 case GOT_FILEIDX_MODE_REGULAR_FILE:
5220 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5223 case GOT_FILEIDX_MODE_SYMLINK:
5227 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5230 ct->mode |= got_fileindex_entry_perms_get(ie);
5231 } else if (status != GOT_STATUS_DELETE &&
5232 staged_status != GOT_STATUS_DELETE) {
5234 if (fstatat(dirfd, de_name, &sb,
5235 AT_SYMLINK_NOFOLLOW) == -1) {
5236 err = got_error_from_errno2("fstatat",
5240 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5241 err = got_error_from_errno2("lstat", ct->ondisk_path);
5244 ct->mode = sb.st_mode;
5247 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5248 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5250 err = got_error_from_errno("asprintf");
5254 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5255 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5257 char target_path[PATH_MAX];
5259 target_len = readlink(ct->ondisk_path, target_path,
5260 sizeof(target_path));
5261 if (target_len == -1) {
5262 err = got_error_from_errno2("readlink",
5266 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5267 target_len, ct->ondisk_path, a->worktree->root_path);
5270 if (is_bad_symlink) {
5271 err = got_error_path(ct->ondisk_path,
5272 GOT_ERR_BAD_SYMLINK);
5278 ct->status = status;
5279 ct->staged_status = staged_status;
5280 ct->blob_id = NULL; /* will be filled in when blob gets created */
5281 if (ct->status != GOT_STATUS_ADD &&
5282 ct->staged_status != GOT_STATUS_ADD) {
5283 ct->base_blob_id = got_object_id_dup(blob_id);
5284 if (ct->base_blob_id == NULL) {
5285 err = got_error_from_errno("got_object_id_dup");
5288 ct->base_commit_id = got_object_id_dup(commit_id);
5289 if (ct->base_commit_id == NULL) {
5290 err = got_error_from_errno("got_object_id_dup");
5294 if (ct->staged_status == GOT_STATUS_ADD ||
5295 ct->staged_status == GOT_STATUS_MODIFY) {
5296 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5297 if (ct->staged_blob_id == NULL) {
5298 err = got_error_from_errno("got_object_id_dup");
5302 ct->path = strdup(path);
5303 if (ct->path == NULL) {
5304 err = got_error_from_errno("strdup");
5307 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5311 if (a->diff_outfile && ct && new != NULL) {
5312 err = append_ct_diff(ct, &a->diff_header_shown,
5313 a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5314 a->repo, a->worktree);
5319 if (ct && (err || new == NULL))
5320 free_commitable(ct);
5326 static const struct got_error *write_tree(struct got_object_id **, int *,
5327 struct got_tree_object *, const char *, struct got_pathlist_head *,
5328 got_worktree_status_cb status_cb, void *status_arg,
5329 struct got_repository *);
5331 static const struct got_error *
5332 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5333 struct got_tree_entry *te, const char *parent_path,
5334 struct got_pathlist_head *commitable_paths,
5335 got_worktree_status_cb status_cb, void *status_arg,
5336 struct got_repository *repo)
5338 const struct got_error *err = NULL;
5339 struct got_tree_object *subtree;
5342 if (asprintf(&subpath, "%s%s%s", parent_path,
5343 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5344 return got_error_from_errno("asprintf");
5346 err = got_object_open_as_tree(&subtree, repo, &te->id);
5350 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5351 commitable_paths, status_cb, status_arg, repo);
5352 got_object_tree_close(subtree);
5357 static const struct got_error *
5358 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5360 const struct got_error *err = NULL;
5361 char *ct_parent_path = NULL;
5365 if (strchr(ct->in_repo_path, '/') == NULL) {
5366 *match = got_path_is_root_dir(path);
5370 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5373 *match = (strcmp(path, ct_parent_path) == 0);
5374 free(ct_parent_path);
5379 get_ct_file_mode(struct got_commitable *ct)
5381 if (S_ISLNK(ct->mode))
5384 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5387 static const struct got_error *
5388 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5389 struct got_tree_entry *te, struct got_commitable *ct)
5391 const struct got_error *err = NULL;
5395 err = got_object_tree_entry_dup(new_te, te);
5399 (*new_te)->mode = get_ct_file_mode(ct);
5401 if (ct->staged_status == GOT_STATUS_MODIFY)
5402 memcpy(&(*new_te)->id, ct->staged_blob_id,
5403 sizeof((*new_te)->id));
5405 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5407 if (err && *new_te) {
5414 static const struct got_error *
5415 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5416 struct got_commitable *ct)
5418 const struct got_error *err = NULL;
5419 char *ct_name = NULL;
5423 *new_te = calloc(1, sizeof(**new_te));
5424 if (*new_te == NULL)
5425 return got_error_from_errno("calloc");
5427 err = got_path_basename(&ct_name, ct->path);
5430 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5431 sizeof((*new_te)->name)) {
5432 err = got_error(GOT_ERR_NO_SPACE);
5436 (*new_te)->mode = get_ct_file_mode(ct);
5438 if (ct->staged_status == GOT_STATUS_ADD)
5439 memcpy(&(*new_te)->id, ct->staged_blob_id,
5440 sizeof((*new_te)->id));
5442 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5445 if (err && *new_te) {
5452 static const struct got_error *
5453 insert_tree_entry(struct got_tree_entry *new_te,
5454 struct got_pathlist_head *paths)
5456 const struct got_error *err = NULL;
5457 struct got_pathlist_entry *new_pe;
5459 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5463 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5467 static const struct got_error *
5468 report_ct_status(struct got_commitable *ct,
5469 got_worktree_status_cb status_cb, void *status_arg)
5471 const char *ct_path = ct->path;
5472 unsigned char status;
5474 if (status_cb == NULL) /* no commit progress output desired */
5477 while (ct_path[0] == '/')
5480 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5481 status = ct->staged_status;
5483 status = ct->status;
5485 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5486 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5489 static const struct got_error *
5490 match_modified_subtree(int *modified, struct got_tree_entry *te,
5491 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5493 const struct got_error *err = NULL;
5494 struct got_pathlist_entry *pe;
5499 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5500 got_path_is_root_dir(base_tree_path) ? "" : "/",
5502 return got_error_from_errno("asprintf");
5504 TAILQ_FOREACH(pe, commitable_paths, entry) {
5505 struct got_commitable *ct = pe->data;
5506 *modified = got_path_is_child(ct->in_repo_path, te_path,
5516 static const struct got_error *
5517 match_deleted_or_modified_ct(struct got_commitable **ctp,
5518 struct got_tree_entry *te, const char *base_tree_path,
5519 struct got_pathlist_head *commitable_paths)
5521 const struct got_error *err = NULL;
5522 struct got_pathlist_entry *pe;
5526 TAILQ_FOREACH(pe, commitable_paths, entry) {
5527 struct got_commitable *ct = pe->data;
5528 char *ct_name = NULL;
5531 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5532 if (ct->status != GOT_STATUS_MODIFY &&
5533 ct->status != GOT_STATUS_MODE_CHANGE &&
5534 ct->status != GOT_STATUS_DELETE)
5537 if (ct->staged_status != GOT_STATUS_MODIFY &&
5538 ct->staged_status != GOT_STATUS_DELETE)
5542 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5545 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5551 err = got_path_basename(&ct_name, pe->path);
5555 if (strcmp(te->name, ct_name) != 0) {
5568 static const struct got_error *
5569 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5570 const char *child_path, const char *path_base_tree,
5571 struct got_pathlist_head *commitable_paths,
5572 got_worktree_status_cb status_cb, void *status_arg,
5573 struct got_repository *repo)
5575 const struct got_error *err = NULL;
5576 struct got_tree_entry *new_te;
5578 struct got_object_id *id = NULL;
5583 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5584 got_path_is_root_dir(path_base_tree) ? "" : "/",
5586 return got_error_from_errno("asprintf");
5588 new_te = calloc(1, sizeof(*new_te));
5590 return got_error_from_errno("calloc");
5591 new_te->mode = S_IFDIR;
5593 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5594 sizeof(new_te->name)) {
5595 err = got_error(GOT_ERR_NO_SPACE);
5598 err = write_tree(&id, &nentries, NULL, subtree_path,
5599 commitable_paths, status_cb, status_arg, repo);
5604 memcpy(&new_te->id, id, sizeof(new_te->id));
5613 static const struct got_error *
5614 write_tree(struct got_object_id **new_tree_id, int *nentries,
5615 struct got_tree_object *base_tree, const char *path_base_tree,
5616 struct got_pathlist_head *commitable_paths,
5617 got_worktree_status_cb status_cb, void *status_arg,
5618 struct got_repository *repo)
5620 const struct got_error *err = NULL;
5621 struct got_pathlist_head paths;
5622 struct got_tree_entry *te, *new_te = NULL;
5623 struct got_pathlist_entry *pe;
5628 /* Insert, and recurse into, newly added entries first. */
5629 TAILQ_FOREACH(pe, commitable_paths, entry) {
5630 struct got_commitable *ct = pe->data;
5631 char *child_path = NULL, *slash;
5633 if ((ct->status != GOT_STATUS_ADD &&
5634 ct->staged_status != GOT_STATUS_ADD) ||
5635 (ct->flags & GOT_COMMITABLE_ADDED))
5638 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5639 strlen(path_base_tree)))
5642 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5647 slash = strchr(child_path, '/');
5648 if (slash == NULL) {
5649 err = alloc_added_blob_tree_entry(&new_te, ct);
5652 err = report_ct_status(ct, status_cb, status_arg);
5655 ct->flags |= GOT_COMMITABLE_ADDED;
5656 err = insert_tree_entry(new_te, &paths);
5661 *slash = '\0'; /* trim trailing path components */
5662 if (base_tree == NULL ||
5663 got_object_tree_find_entry(base_tree, child_path)
5665 err = make_subtree_for_added_blob(&new_te,
5666 child_path, path_base_tree,
5667 commitable_paths, status_cb, status_arg,
5671 err = insert_tree_entry(new_te, &paths);
5680 int i, nbase_entries;
5681 /* Handle modified and deleted entries. */
5682 nbase_entries = got_object_tree_get_nentries(base_tree);
5683 for (i = 0; i < nbase_entries; i++) {
5684 struct got_commitable *ct = NULL;
5686 te = got_object_tree_get_entry(base_tree, i);
5687 if (got_object_tree_entry_is_submodule(te)) {
5688 /* Entry is a submodule; just copy it. */
5689 err = got_object_tree_entry_dup(&new_te, te);
5692 err = insert_tree_entry(new_te, &paths);
5699 if (S_ISDIR(te->mode)) {
5701 err = got_object_tree_entry_dup(&new_te, te);
5704 err = match_modified_subtree(&modified, te,
5705 path_base_tree, commitable_paths);
5708 /* Avoid recursion into unmodified subtrees. */
5710 struct got_object_id *new_id;
5712 err = write_subtree(&new_id,
5714 path_base_tree, commitable_paths,
5715 status_cb, status_arg, repo);
5718 if (nsubentries == 0) {
5719 /* All entries were deleted. */
5723 memcpy(&new_te->id, new_id,
5724 sizeof(new_te->id));
5727 err = insert_tree_entry(new_te, &paths);
5734 err = match_deleted_or_modified_ct(&ct, te,
5735 path_base_tree, commitable_paths);
5739 /* NB: Deleted entries get dropped here. */
5740 if (ct->status == GOT_STATUS_MODIFY ||
5741 ct->status == GOT_STATUS_MODE_CHANGE ||
5742 ct->staged_status == GOT_STATUS_MODIFY) {
5743 err = alloc_modified_blob_tree_entry(
5747 err = insert_tree_entry(new_te, &paths);
5752 err = report_ct_status(ct, status_cb,
5757 /* Entry is unchanged; just copy it. */
5758 err = got_object_tree_entry_dup(&new_te, te);
5761 err = insert_tree_entry(new_te, &paths);
5769 /* Write new list of entries; deleted entries have been dropped. */
5770 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5772 got_pathlist_free(&paths);
5776 static const struct got_error *
5777 update_fileindex_after_commit(struct got_worktree *worktree,
5778 struct got_pathlist_head *commitable_paths,
5779 struct got_object_id *new_base_commit_id,
5780 struct got_fileindex *fileindex, int have_staged_files)
5782 const struct got_error *err = NULL;
5783 struct got_pathlist_entry *pe;
5784 char *relpath = NULL;
5786 TAILQ_FOREACH(pe, commitable_paths, entry) {
5787 struct got_fileindex_entry *ie;
5788 struct got_commitable *ct = pe->data;
5790 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5792 err = got_path_skip_common_ancestor(&relpath,
5793 worktree->root_path, ct->ondisk_path);
5798 if (ct->status == GOT_STATUS_DELETE ||
5799 ct->staged_status == GOT_STATUS_DELETE) {
5800 got_fileindex_entry_remove(fileindex, ie);
5801 } else if (ct->staged_status == GOT_STATUS_ADD ||
5802 ct->staged_status == GOT_STATUS_MODIFY) {
5803 got_fileindex_entry_stage_set(ie,
5804 GOT_FILEIDX_STAGE_NONE);
5805 got_fileindex_entry_staged_filetype_set(ie, 0);
5807 err = got_fileindex_entry_update(ie,
5808 worktree->root_fd, relpath,
5809 ct->staged_blob_id->sha1,
5810 new_base_commit_id->sha1,
5811 !have_staged_files);
5813 err = got_fileindex_entry_update(ie,
5814 worktree->root_fd, relpath,
5816 new_base_commit_id->sha1,
5817 !have_staged_files);
5819 err = got_fileindex_entry_alloc(&ie, pe->path);
5822 err = got_fileindex_entry_update(ie,
5823 worktree->root_fd, relpath, ct->blob_id->sha1,
5824 new_base_commit_id->sha1, 1);
5826 got_fileindex_entry_free(ie);
5829 err = got_fileindex_entry_add(fileindex, ie);
5831 got_fileindex_entry_free(ie);
5844 static const struct got_error *
5845 check_out_of_date(const char *in_repo_path, unsigned char status,
5846 unsigned char staged_status, struct got_object_id *base_blob_id,
5847 struct got_object_id *base_commit_id,
5848 struct got_object_id *head_commit_id, struct got_repository *repo,
5851 const struct got_error *err = NULL;
5852 struct got_commit_object *commit = NULL;
5853 struct got_object_id *id = NULL;
5855 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5856 /* Trivial case: base commit == head commit */
5857 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5860 * Ensure file content which local changes were based
5861 * on matches file content in the branch head.
5863 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5866 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5868 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5869 err = got_error(ood_errcode);
5871 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5872 err = got_error(ood_errcode);
5874 /* Require that added files don't exist in the branch head. */
5875 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5878 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5879 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5881 err = id ? got_error(ood_errcode) : NULL;
5886 got_object_commit_close(commit);
5890 static const struct got_error *
5891 commit_worktree(struct got_object_id **new_commit_id,
5892 struct got_pathlist_head *commitable_paths,
5893 struct got_object_id *head_commit_id,
5894 struct got_object_id *parent_id2,
5895 struct got_worktree *worktree,
5896 const char *author, const char *committer, char *diff_path,
5897 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5898 got_worktree_status_cb status_cb, void *status_arg,
5899 struct got_repository *repo)
5901 const struct got_error *err = NULL, *unlockerr = NULL;
5902 struct got_pathlist_entry *pe;
5903 const char *head_ref_name = NULL;
5904 struct got_commit_object *head_commit = NULL;
5905 struct got_reference *head_ref2 = NULL;
5906 struct got_object_id *head_commit_id2 = NULL;
5907 struct got_tree_object *head_tree = NULL;
5908 struct got_object_id *new_tree_id = NULL;
5909 int nentries, nparents = 0;
5910 struct got_object_id_queue parent_ids;
5911 struct got_object_qid *pid = NULL;
5912 char *logmsg = NULL;
5915 *new_commit_id = NULL;
5917 STAILQ_INIT(&parent_ids);
5919 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5923 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5927 if (commit_msg_cb != NULL) {
5928 err = commit_msg_cb(commitable_paths, diff_path,
5929 &logmsg, commit_arg);
5934 if (logmsg == NULL || strlen(logmsg) == 0) {
5935 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5939 /* Create blobs from added and modified files and record their IDs. */
5940 TAILQ_FOREACH(pe, commitable_paths, entry) {
5941 struct got_commitable *ct = pe->data;
5944 /* Blobs for staged files already exist. */
5945 if (ct->staged_status == GOT_STATUS_ADD ||
5946 ct->staged_status == GOT_STATUS_MODIFY)
5949 if (ct->status != GOT_STATUS_ADD &&
5950 ct->status != GOT_STATUS_MODIFY &&
5951 ct->status != GOT_STATUS_MODE_CHANGE)
5954 if (asprintf(&ondisk_path, "%s/%s",
5955 worktree->root_path, pe->path) == -1) {
5956 err = got_error_from_errno("asprintf");
5959 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5965 /* Recursively write new tree objects. */
5966 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5967 commitable_paths, status_cb, status_arg, repo);
5971 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5974 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5977 err = got_object_qid_alloc(&pid, parent_id2);
5980 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5983 timestamp = time(NULL);
5984 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5985 nparents, author, timestamp, committer, timestamp, logmsg, repo);
5991 /* Check if a concurrent commit to our branch has occurred. */
5992 head_ref_name = got_worktree_get_head_ref_name(worktree);
5993 if (head_ref_name == NULL) {
5994 err = got_error_from_errno("got_worktree_get_head_ref_name");
5997 /* Lock the reference here to prevent concurrent modification. */
5998 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6001 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6004 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6005 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6008 /* Update branch head in repository. */
6009 err = got_ref_change_ref(head_ref2, *new_commit_id);
6012 err = got_ref_write(head_ref2, repo);
6016 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6020 err = ref_base_commit(worktree, repo);
6024 got_object_id_queue_free(&parent_ids);
6026 got_object_tree_close(head_tree);
6028 got_object_commit_close(head_commit);
6029 free(head_commit_id2);
6031 unlockerr = got_ref_unlock(head_ref2);
6032 if (unlockerr && err == NULL)
6034 got_ref_close(head_ref2);
6039 static const struct got_error *
6040 check_path_is_commitable(const char *path,
6041 struct got_pathlist_head *commitable_paths)
6043 struct got_pathlist_entry *cpe = NULL;
6044 size_t path_len = strlen(path);
6046 TAILQ_FOREACH(cpe, commitable_paths, entry) {
6047 struct got_commitable *ct = cpe->data;
6048 const char *ct_path = ct->path;
6050 while (ct_path[0] == '/')
6053 if (strcmp(path, ct_path) == 0 ||
6054 got_path_is_child(ct_path, path, path_len))
6059 return got_error_path(path, GOT_ERR_BAD_PATH);
6064 static const struct got_error *
6065 check_staged_file(void *arg, struct got_fileindex_entry *ie)
6067 int *have_staged_files = arg;
6069 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6070 *have_staged_files = 1;
6071 return got_error(GOT_ERR_CANCELLED);
6077 static const struct got_error *
6078 check_non_staged_files(struct got_fileindex *fileindex,
6079 struct got_pathlist_head *paths)
6081 struct got_pathlist_entry *pe;
6082 struct got_fileindex_entry *ie;
6084 TAILQ_FOREACH(pe, paths, entry) {
6085 if (pe->path[0] == '\0')
6087 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6089 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6090 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6091 return got_error_path(pe->path,
6092 GOT_ERR_FILE_NOT_STAGED);
6098 const struct got_error *
6099 got_worktree_commit(struct got_object_id **new_commit_id,
6100 struct got_worktree *worktree, struct got_pathlist_head *paths,
6101 const char *author, const char *committer, int allow_bad_symlinks,
6102 int show_diff, got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6103 got_worktree_status_cb status_cb, void *status_arg,
6104 struct got_repository *repo)
6106 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6107 struct got_fileindex *fileindex = NULL;
6108 char *fileindex_path = NULL;
6109 struct got_pathlist_head commitable_paths;
6110 struct collect_commitables_arg cc_arg;
6111 struct got_pathlist_entry *pe;
6112 struct got_reference *head_ref = NULL;
6113 struct got_object_id *head_commit_id = NULL;
6114 char *diff_path = NULL;
6115 int have_staged_files = 0;
6117 *new_commit_id = NULL;
6119 memset(&cc_arg, 0, sizeof(cc_arg));
6120 TAILQ_INIT(&commitable_paths);
6122 err = lock_worktree(worktree, LOCK_EX);
6126 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6130 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6134 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6138 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6139 &have_staged_files);
6140 if (err && err->code != GOT_ERR_CANCELLED)
6142 if (have_staged_files) {
6143 err = check_non_staged_files(fileindex, paths);
6148 cc_arg.commitable_paths = &commitable_paths;
6149 cc_arg.worktree = worktree;
6150 cc_arg.fileindex = fileindex;
6152 cc_arg.have_staged_files = have_staged_files;
6153 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6154 cc_arg.diff_header_shown = 0;
6156 err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6157 GOT_TMPDIR_STR "/got-diff");
6160 cc_arg.f1 = got_opentemp();
6161 if (cc_arg.f1 == NULL) {
6162 err = got_error_from_errno("got_opentemp");
6165 cc_arg.f2 = got_opentemp();
6166 if (cc_arg.f2 == NULL) {
6167 err = got_error_from_errno("got_opentemp");
6172 TAILQ_FOREACH(pe, paths, entry) {
6173 err = worktree_status(worktree, pe->path, fileindex, repo,
6174 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6180 if (fflush(cc_arg.diff_outfile) == EOF) {
6181 err = got_error_from_errno("fflush");
6186 if (TAILQ_EMPTY(&commitable_paths)) {
6187 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6191 TAILQ_FOREACH(pe, paths, entry) {
6192 err = check_path_is_commitable(pe->path, &commitable_paths);
6197 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6198 struct got_commitable *ct = pe->data;
6199 const char *ct_path = ct->in_repo_path;
6201 while (ct_path[0] == '/')
6203 err = check_out_of_date(ct_path, ct->status,
6204 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6205 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6211 err = commit_worktree(new_commit_id, &commitable_paths,
6212 head_commit_id, NULL, worktree, author, committer, diff_path,
6213 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6217 err = update_fileindex_after_commit(worktree, &commitable_paths,
6218 *new_commit_id, fileindex, have_staged_files);
6219 sync_err = sync_fileindex(fileindex, fileindex_path);
6220 if (sync_err && err == NULL)
6224 got_fileindex_free(fileindex);
6225 free(fileindex_path);
6226 unlockerr = lock_worktree(worktree, LOCK_SH);
6227 if (unlockerr && err == NULL)
6229 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6230 struct got_commitable *ct = pe->data;
6231 free_commitable(ct);
6233 got_pathlist_free(&commitable_paths);
6234 if (diff_path && unlink(diff_path) == -1 && err == NULL)
6235 err = got_error_from_errno2("unlink", diff_path);
6237 if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6239 err = got_error_from_errno("fclose");
6244 got_commitable_get_path(struct got_commitable *ct)
6250 got_commitable_get_status(struct got_commitable *ct)
6255 struct check_rebase_ok_arg {
6256 struct got_worktree *worktree;
6257 struct got_repository *repo;
6260 static const struct got_error *
6261 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6263 const struct got_error *err = NULL;
6264 struct check_rebase_ok_arg *a = arg;
6265 unsigned char status;
6269 /* Reject rebase of a work tree with mixed base commits. */
6270 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6271 SHA1_DIGEST_LENGTH))
6272 return got_error(GOT_ERR_MIXED_COMMITS);
6274 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6276 return got_error_from_errno("asprintf");
6278 /* Reject rebase of a work tree with modified or staged files. */
6279 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6284 if (status != GOT_STATUS_NO_CHANGE)
6285 return got_error(GOT_ERR_MODIFIED);
6286 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6287 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6292 const struct got_error *
6293 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6294 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6295 struct got_worktree *worktree, struct got_reference *branch,
6296 struct got_repository *repo)
6298 const struct got_error *err = NULL;
6299 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6300 char *branch_ref_name = NULL;
6301 char *fileindex_path = NULL;
6302 struct check_rebase_ok_arg ok_arg;
6303 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6304 struct got_object_id *wt_branch_tip = NULL;
6306 *new_base_branch_ref = NULL;
6310 err = lock_worktree(worktree, LOCK_EX);
6314 err = open_fileindex(fileindex, &fileindex_path, worktree);
6318 ok_arg.worktree = worktree;
6320 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6325 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6329 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6333 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6337 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6342 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6345 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6346 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6350 err = got_ref_alloc_symref(new_base_branch_ref,
6351 new_base_branch_ref_name, wt_branch);
6354 err = got_ref_write(*new_base_branch_ref, repo);
6358 /* TODO Lock original branch's ref while rebasing? */
6360 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6364 err = got_ref_write(branch_ref, repo);
6368 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6369 worktree->base_commit_id);
6372 err = got_ref_write(*tmp_branch, repo);
6376 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6380 free(fileindex_path);
6381 free(tmp_branch_name);
6382 free(new_base_branch_ref_name);
6383 free(branch_ref_name);
6385 got_ref_close(branch_ref);
6387 got_ref_close(wt_branch);
6388 free(wt_branch_tip);
6390 if (*new_base_branch_ref) {
6391 got_ref_close(*new_base_branch_ref);
6392 *new_base_branch_ref = NULL;
6395 got_ref_close(*tmp_branch);
6399 got_fileindex_free(*fileindex);
6402 lock_worktree(worktree, LOCK_SH);
6407 const struct got_error *
6408 got_worktree_rebase_continue(struct got_object_id **commit_id,
6409 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6410 struct got_reference **branch, struct got_fileindex **fileindex,
6411 struct got_worktree *worktree, struct got_repository *repo)
6413 const struct got_error *err;
6414 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6415 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6416 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6417 char *fileindex_path = NULL;
6418 int have_staged_files = 0;
6421 *new_base_branch = NULL;
6426 err = lock_worktree(worktree, LOCK_EX);
6430 err = open_fileindex(fileindex, &fileindex_path, worktree);
6434 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6435 &have_staged_files);
6436 if (err && err->code != GOT_ERR_CANCELLED)
6438 if (have_staged_files) {
6439 err = got_error(GOT_ERR_STAGED_PATHS);
6443 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6447 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6451 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6455 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6459 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6463 err = got_ref_open(branch, repo,
6464 got_ref_get_symref_target(branch_ref), 0);
6468 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6472 err = got_ref_resolve(commit_id, repo, commit_ref);
6476 err = got_ref_open(new_base_branch, repo,
6477 new_base_branch_ref_name, 0);
6481 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6485 free(commit_ref_name);
6486 free(branch_ref_name);
6487 free(fileindex_path);
6489 got_ref_close(commit_ref);
6491 got_ref_close(branch_ref);
6496 got_ref_close(*tmp_branch);
6499 if (*new_base_branch) {
6500 got_ref_close(*new_base_branch);
6501 *new_base_branch = NULL;
6504 got_ref_close(*branch);
6508 got_fileindex_free(*fileindex);
6511 lock_worktree(worktree, LOCK_SH);
6516 const struct got_error *
6517 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6519 const struct got_error *err;
6520 char *tmp_branch_name = NULL;
6522 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6526 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6527 free(tmp_branch_name);
6531 static const struct got_error *
6532 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6533 const char *diff_path, char **logmsg, void *arg)
6539 static const struct got_error *
6540 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6541 const char *path, struct got_object_id *blob_id,
6542 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6543 int dirfd, const char *de_name)
6548 struct collect_merged_paths_arg {
6549 got_worktree_checkout_cb progress_cb;
6551 struct got_pathlist_head *merged_paths;
6554 static const struct got_error *
6555 collect_merged_paths(void *arg, unsigned char status, const char *path)
6557 const struct got_error *err;
6558 struct collect_merged_paths_arg *a = arg;
6560 struct got_pathlist_entry *new;
6562 err = (*a->progress_cb)(a->progress_arg, status, path);
6566 if (status != GOT_STATUS_MERGE &&
6567 status != GOT_STATUS_ADD &&
6568 status != GOT_STATUS_DELETE &&
6569 status != GOT_STATUS_CONFLICT)
6574 return got_error_from_errno("strdup");
6576 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6577 if (err || new == NULL)
6583 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6585 struct got_pathlist_entry *pe;
6587 TAILQ_FOREACH(pe, merged_paths, entry)
6588 free((char *)pe->path);
6590 got_pathlist_free(merged_paths);
6593 static const struct got_error *
6594 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6595 int is_rebase, struct got_repository *repo)
6597 const struct got_error *err;
6598 struct got_reference *commit_ref = NULL;
6600 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6602 if (err->code != GOT_ERR_NOT_REF)
6604 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6607 err = got_ref_write(commit_ref, repo);
6610 } else if (is_rebase) {
6611 struct got_object_id *stored_id;
6614 err = got_ref_resolve(&stored_id, repo, commit_ref);
6617 cmp = got_object_id_cmp(commit_id, stored_id);
6620 err = got_error(GOT_ERR_REBASE_COMMITID);
6626 got_ref_close(commit_ref);
6630 static const struct got_error *
6631 rebase_merge_files(struct got_pathlist_head *merged_paths,
6632 const char *commit_ref_name, struct got_worktree *worktree,
6633 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6634 struct got_object_id *commit_id, struct got_repository *repo,
6635 got_worktree_checkout_cb progress_cb, void *progress_arg,
6636 got_cancel_cb cancel_cb, void *cancel_arg)
6638 const struct got_error *err;
6639 struct got_reference *commit_ref = NULL;
6640 struct collect_merged_paths_arg cmp_arg;
6641 char *fileindex_path;
6643 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6645 err = get_fileindex_path(&fileindex_path, worktree);
6649 cmp_arg.progress_cb = progress_cb;
6650 cmp_arg.progress_arg = progress_arg;
6651 cmp_arg.merged_paths = merged_paths;
6652 err = merge_files(worktree, fileindex, fileindex_path,
6653 parent_commit_id, commit_id, repo, collect_merged_paths,
6654 &cmp_arg, cancel_cb, cancel_arg);
6656 got_ref_close(commit_ref);
6660 const struct got_error *
6661 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6662 struct got_worktree *worktree, struct got_fileindex *fileindex,
6663 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6664 struct got_repository *repo,
6665 got_worktree_checkout_cb progress_cb, void *progress_arg,
6666 got_cancel_cb cancel_cb, void *cancel_arg)
6668 const struct got_error *err;
6669 char *commit_ref_name;
6671 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6675 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6679 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6680 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6681 progress_arg, cancel_cb, cancel_arg);
6683 free(commit_ref_name);
6687 const struct got_error *
6688 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6689 struct got_worktree *worktree, struct got_fileindex *fileindex,
6690 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6691 struct got_repository *repo,
6692 got_worktree_checkout_cb progress_cb, void *progress_arg,
6693 got_cancel_cb cancel_cb, void *cancel_arg)
6695 const struct got_error *err;
6696 char *commit_ref_name;
6698 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6702 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6706 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6707 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6708 progress_arg, cancel_cb, cancel_arg);
6710 free(commit_ref_name);
6714 static const struct got_error *
6715 rebase_commit(struct got_object_id **new_commit_id,
6716 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6717 struct got_worktree *worktree, struct got_fileindex *fileindex,
6718 struct got_reference *tmp_branch, const char *committer,
6719 struct got_commit_object *orig_commit, const char *new_logmsg,
6720 struct got_repository *repo)
6722 const struct got_error *err, *sync_err;
6723 struct got_pathlist_head commitable_paths;
6724 struct collect_commitables_arg cc_arg;
6725 char *fileindex_path = NULL;
6726 struct got_reference *head_ref = NULL;
6727 struct got_object_id *head_commit_id = NULL;
6728 char *logmsg = NULL;
6730 memset(&cc_arg, 0, sizeof(cc_arg));
6731 TAILQ_INIT(&commitable_paths);
6732 *new_commit_id = NULL;
6734 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6736 err = get_fileindex_path(&fileindex_path, worktree);
6740 cc_arg.commitable_paths = &commitable_paths;
6741 cc_arg.worktree = worktree;
6743 cc_arg.have_staged_files = 0;
6745 * If possible get the status of individual files directly to
6746 * avoid crawling the entire work tree once per rebased commit.
6748 * Ideally, merged_paths would contain a list of commitables
6749 * we could use so we could skip worktree_status() entirely.
6750 * However, we would then need carefully keep track of cumulative
6751 * effects of operations such as file additions and deletions
6752 * in 'got histedit -f' (folding multiple commits into one),
6753 * and this extra complexity is not really worth it.
6756 struct got_pathlist_entry *pe;
6757 TAILQ_FOREACH(pe, merged_paths, entry) {
6758 err = worktree_status(worktree, pe->path, fileindex,
6759 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6765 err = worktree_status(worktree, "", fileindex, repo,
6766 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6771 if (TAILQ_EMPTY(&commitable_paths)) {
6772 /* No-op change; commit will be elided. */
6773 err = got_ref_delete(commit_ref, repo);
6776 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6780 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6784 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6789 logmsg = strdup(new_logmsg);
6790 if (logmsg == NULL) {
6791 err = got_error_from_errno("strdup");
6795 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6800 /* NB: commit_worktree will call free(logmsg) */
6801 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6802 NULL, worktree, got_object_commit_get_author(orig_commit),
6803 committer ? committer :
6804 got_object_commit_get_committer(orig_commit), NULL,
6805 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6809 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6813 err = got_ref_delete(commit_ref, repo);
6817 err = update_fileindex_after_commit(worktree, &commitable_paths,
6818 *new_commit_id, fileindex, 0);
6819 sync_err = sync_fileindex(fileindex, fileindex_path);
6820 if (sync_err && err == NULL)
6823 free(fileindex_path);
6824 free(head_commit_id);
6826 got_ref_close(head_ref);
6828 free(*new_commit_id);
6829 *new_commit_id = NULL;
6834 const struct got_error *
6835 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6836 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6837 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6838 const char *committer, struct got_commit_object *orig_commit,
6839 struct got_object_id *orig_commit_id, struct got_repository *repo)
6841 const struct got_error *err;
6842 char *commit_ref_name;
6843 struct got_reference *commit_ref = NULL;
6844 struct got_object_id *commit_id = NULL;
6846 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6850 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6853 err = got_ref_resolve(&commit_id, repo, commit_ref);
6856 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6857 err = got_error(GOT_ERR_REBASE_COMMITID);
6861 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6862 worktree, fileindex, tmp_branch, committer, orig_commit,
6866 got_ref_close(commit_ref);
6867 free(commit_ref_name);
6872 const struct got_error *
6873 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6874 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6875 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6876 const char *committer, struct got_commit_object *orig_commit,
6877 struct got_object_id *orig_commit_id, const char *new_logmsg,
6878 struct got_repository *repo)
6880 const struct got_error *err;
6881 char *commit_ref_name;
6882 struct got_reference *commit_ref = NULL;
6884 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6888 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6892 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6893 worktree, fileindex, tmp_branch, committer, orig_commit,
6897 got_ref_close(commit_ref);
6898 free(commit_ref_name);
6902 const struct got_error *
6903 got_worktree_rebase_postpone(struct got_worktree *worktree,
6904 struct got_fileindex *fileindex)
6907 got_fileindex_free(fileindex);
6908 return lock_worktree(worktree, LOCK_SH);
6911 static const struct got_error *
6912 delete_ref(const char *name, struct got_repository *repo)
6914 const struct got_error *err;
6915 struct got_reference *ref;
6917 err = got_ref_open(&ref, repo, name, 0);
6919 if (err->code == GOT_ERR_NOT_REF)
6924 err = got_ref_delete(ref, repo);
6929 static const struct got_error *
6930 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6932 const struct got_error *err;
6933 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6934 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6936 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6939 err = delete_ref(tmp_branch_name, repo);
6943 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6946 err = delete_ref(new_base_branch_ref_name, repo);
6950 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6953 err = delete_ref(branch_ref_name, repo);
6957 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6960 err = delete_ref(commit_ref_name, repo);
6965 free(tmp_branch_name);
6966 free(new_base_branch_ref_name);
6967 free(branch_ref_name);
6968 free(commit_ref_name);
6972 static const struct got_error *
6973 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6974 struct got_object_id *new_commit_id, struct got_repository *repo)
6976 const struct got_error *err;
6977 struct got_reference *ref = NULL;
6978 struct got_object_id *old_commit_id = NULL;
6979 const char *branch_name = NULL;
6980 char *new_id_str = NULL;
6981 char *refname = NULL;
6983 branch_name = got_ref_get_name(branch);
6984 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6985 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6988 err = got_object_id_str(&new_id_str, new_commit_id);
6992 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6993 new_id_str) == -1) {
6994 err = got_error_from_errno("asprintf");
6998 err = got_ref_resolve(&old_commit_id, repo, branch);
7002 err = got_ref_alloc(&ref, refname, old_commit_id);
7006 err = got_ref_write(ref, repo);
7010 free(old_commit_id);
7016 const struct got_error *
7017 got_worktree_rebase_complete(struct got_worktree *worktree,
7018 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
7019 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
7020 struct got_repository *repo, int create_backup)
7022 const struct got_error *err, *unlockerr, *sync_err;
7023 struct got_object_id *new_head_commit_id = NULL;
7024 char *fileindex_path = NULL;
7026 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7030 if (create_backup) {
7031 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7032 rebased_branch, new_head_commit_id, repo);
7037 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7041 err = got_ref_write(rebased_branch, repo);
7045 err = got_worktree_set_head_ref(worktree, rebased_branch);
7049 err = delete_rebase_refs(worktree, repo);
7053 err = get_fileindex_path(&fileindex_path, worktree);
7056 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7057 sync_err = sync_fileindex(fileindex, fileindex_path);
7058 if (sync_err && err == NULL)
7061 got_fileindex_free(fileindex);
7062 free(fileindex_path);
7063 free(new_head_commit_id);
7064 unlockerr = lock_worktree(worktree, LOCK_SH);
7065 if (unlockerr && err == NULL)
7070 const struct got_error *
7071 got_worktree_rebase_abort(struct got_worktree *worktree,
7072 struct got_fileindex *fileindex, struct got_repository *repo,
7073 struct got_reference *new_base_branch,
7074 got_worktree_checkout_cb progress_cb, void *progress_arg)
7076 const struct got_error *err, *unlockerr, *sync_err;
7077 struct got_reference *resolved = NULL;
7078 struct got_object_id *commit_id = NULL;
7079 struct got_commit_object *commit = NULL;
7080 char *fileindex_path = NULL;
7081 struct revert_file_args rfa;
7082 struct got_object_id *tree_id = NULL;
7084 err = lock_worktree(worktree, LOCK_EX);
7088 err = got_object_open_as_commit(&commit, repo,
7089 worktree->base_commit_id);
7093 err = got_ref_open(&resolved, repo,
7094 got_ref_get_symref_target(new_base_branch), 0);
7098 err = got_worktree_set_head_ref(worktree, resolved);
7103 * XXX commits to the base branch could have happened while
7104 * we were busy rebasing; should we store the original commit ID
7105 * when rebase begins and read it back here?
7107 err = got_ref_resolve(&commit_id, repo, resolved);
7111 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7115 err = got_object_id_by_path(&tree_id, repo, commit,
7116 worktree->path_prefix);
7120 err = delete_rebase_refs(worktree, repo);
7124 err = get_fileindex_path(&fileindex_path, worktree);
7128 rfa.worktree = worktree;
7129 rfa.fileindex = fileindex;
7130 rfa.progress_cb = progress_cb;
7131 rfa.progress_arg = progress_arg;
7132 rfa.patch_cb = NULL;
7133 rfa.patch_arg = NULL;
7135 rfa.unlink_added_files = 0;
7136 err = worktree_status(worktree, "", fileindex, repo,
7137 revert_file, &rfa, NULL, NULL, 1, 0);
7141 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7142 repo, progress_cb, progress_arg, NULL, NULL);
7144 sync_err = sync_fileindex(fileindex, fileindex_path);
7145 if (sync_err && err == NULL)
7148 got_ref_close(resolved);
7152 got_object_commit_close(commit);
7154 got_fileindex_free(fileindex);
7155 free(fileindex_path);
7157 unlockerr = lock_worktree(worktree, LOCK_SH);
7158 if (unlockerr && err == NULL)
7163 const struct got_error *
7164 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7165 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7166 struct got_fileindex **fileindex, struct got_worktree *worktree,
7167 struct got_repository *repo)
7169 const struct got_error *err = NULL;
7170 char *tmp_branch_name = NULL;
7171 char *branch_ref_name = NULL;
7172 char *base_commit_ref_name = NULL;
7173 char *fileindex_path = NULL;
7174 struct check_rebase_ok_arg ok_arg;
7175 struct got_reference *wt_branch = NULL;
7176 struct got_reference *base_commit_ref = NULL;
7180 *base_commit_id = NULL;
7183 err = lock_worktree(worktree, LOCK_EX);
7187 err = open_fileindex(fileindex, &fileindex_path, worktree);
7191 ok_arg.worktree = worktree;
7193 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7198 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7202 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7206 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7211 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7216 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7220 err = got_ref_write(*branch_ref, repo);
7224 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7225 worktree->base_commit_id);
7228 err = got_ref_write(base_commit_ref, repo);
7231 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7232 if (*base_commit_id == NULL) {
7233 err = got_error_from_errno("got_object_id_dup");
7237 err = got_ref_alloc(tmp_branch, tmp_branch_name,
7238 worktree->base_commit_id);
7241 err = got_ref_write(*tmp_branch, repo);
7245 err = got_worktree_set_head_ref(worktree, *tmp_branch);
7249 free(fileindex_path);
7250 free(tmp_branch_name);
7251 free(branch_ref_name);
7252 free(base_commit_ref_name);
7254 got_ref_close(wt_branch);
7257 got_ref_close(*branch_ref);
7261 got_ref_close(*tmp_branch);
7264 free(*base_commit_id);
7266 got_fileindex_free(*fileindex);
7269 lock_worktree(worktree, LOCK_SH);
7274 const struct got_error *
7275 got_worktree_histedit_postpone(struct got_worktree *worktree,
7276 struct got_fileindex *fileindex)
7279 got_fileindex_free(fileindex);
7280 return lock_worktree(worktree, LOCK_SH);
7283 const struct got_error *
7284 got_worktree_histedit_in_progress(int *in_progress,
7285 struct got_worktree *worktree)
7287 const struct got_error *err;
7288 char *tmp_branch_name = NULL;
7290 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7294 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7295 free(tmp_branch_name);
7299 const struct got_error *
7300 got_worktree_histedit_continue(struct got_object_id **commit_id,
7301 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7302 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7303 struct got_worktree *worktree, struct got_repository *repo)
7305 const struct got_error *err;
7306 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7307 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7308 struct got_reference *commit_ref = NULL;
7309 struct got_reference *base_commit_ref = NULL;
7310 char *fileindex_path = NULL;
7311 int have_staged_files = 0;
7315 *base_commit_id = NULL;
7318 err = lock_worktree(worktree, LOCK_EX);
7322 err = open_fileindex(fileindex, &fileindex_path, worktree);
7326 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7327 &have_staged_files);
7328 if (err && err->code != GOT_ERR_CANCELLED)
7330 if (have_staged_files) {
7331 err = got_error(GOT_ERR_STAGED_PATHS);
7335 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7339 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7343 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7347 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7352 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7356 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7359 err = got_ref_resolve(commit_id, repo, commit_ref);
7363 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7366 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7370 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7374 free(commit_ref_name);
7375 free(branch_ref_name);
7376 free(fileindex_path);
7378 got_ref_close(commit_ref);
7379 if (base_commit_ref)
7380 got_ref_close(base_commit_ref);
7384 free(*base_commit_id);
7385 *base_commit_id = NULL;
7387 got_ref_close(*tmp_branch);
7391 got_fileindex_free(*fileindex);
7394 lock_worktree(worktree, LOCK_EX);
7399 static const struct got_error *
7400 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7402 const struct got_error *err;
7403 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7404 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7406 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7409 err = delete_ref(tmp_branch_name, repo);
7413 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7417 err = delete_ref(base_commit_ref_name, repo);
7421 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7424 err = delete_ref(branch_ref_name, repo);
7428 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7431 err = delete_ref(commit_ref_name, repo);
7435 free(tmp_branch_name);
7436 free(base_commit_ref_name);
7437 free(branch_ref_name);
7438 free(commit_ref_name);
7442 const struct got_error *
7443 got_worktree_histedit_abort(struct got_worktree *worktree,
7444 struct got_fileindex *fileindex, struct got_repository *repo,
7445 struct got_reference *branch, struct got_object_id *base_commit_id,
7446 got_worktree_checkout_cb progress_cb, void *progress_arg)
7448 const struct got_error *err, *unlockerr, *sync_err;
7449 struct got_reference *resolved = NULL;
7450 char *fileindex_path = NULL;
7451 struct got_commit_object *commit = NULL;
7452 struct got_object_id *tree_id = NULL;
7453 struct revert_file_args rfa;
7455 err = lock_worktree(worktree, LOCK_EX);
7459 err = got_object_open_as_commit(&commit, repo,
7460 worktree->base_commit_id);
7464 err = got_ref_open(&resolved, repo,
7465 got_ref_get_symref_target(branch), 0);
7469 err = got_worktree_set_head_ref(worktree, resolved);
7473 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7477 err = got_object_id_by_path(&tree_id, repo, commit,
7478 worktree->path_prefix);
7482 err = delete_histedit_refs(worktree, repo);
7486 err = get_fileindex_path(&fileindex_path, worktree);
7490 rfa.worktree = worktree;
7491 rfa.fileindex = fileindex;
7492 rfa.progress_cb = progress_cb;
7493 rfa.progress_arg = progress_arg;
7494 rfa.patch_cb = NULL;
7495 rfa.patch_arg = NULL;
7497 rfa.unlink_added_files = 0;
7498 err = worktree_status(worktree, "", fileindex, repo,
7499 revert_file, &rfa, NULL, NULL, 1, 0);
7503 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7504 repo, progress_cb, progress_arg, NULL, NULL);
7506 sync_err = sync_fileindex(fileindex, fileindex_path);
7507 if (sync_err && err == NULL)
7510 got_ref_close(resolved);
7512 free(fileindex_path);
7514 unlockerr = lock_worktree(worktree, LOCK_SH);
7515 if (unlockerr && err == NULL)
7520 const struct got_error *
7521 got_worktree_histedit_complete(struct got_worktree *worktree,
7522 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7523 struct got_reference *edited_branch, struct got_repository *repo)
7525 const struct got_error *err, *unlockerr, *sync_err;
7526 struct got_object_id *new_head_commit_id = NULL;
7527 struct got_reference *resolved = NULL;
7528 char *fileindex_path = NULL;
7530 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7534 err = got_ref_open(&resolved, repo,
7535 got_ref_get_symref_target(edited_branch), 0);
7539 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7540 resolved, new_head_commit_id, repo);
7544 err = got_ref_change_ref(resolved, new_head_commit_id);
7548 err = got_ref_write(resolved, repo);
7552 err = got_worktree_set_head_ref(worktree, resolved);
7556 err = delete_histedit_refs(worktree, repo);
7560 err = get_fileindex_path(&fileindex_path, worktree);
7563 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7564 sync_err = sync_fileindex(fileindex, fileindex_path);
7565 if (sync_err && err == NULL)
7568 got_fileindex_free(fileindex);
7569 free(fileindex_path);
7570 free(new_head_commit_id);
7571 unlockerr = lock_worktree(worktree, LOCK_SH);
7572 if (unlockerr && err == NULL)
7577 const struct got_error *
7578 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7579 struct got_object_id *commit_id, struct got_repository *repo)
7581 const struct got_error *err;
7582 char *commit_ref_name;
7584 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7588 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7592 err = delete_ref(commit_ref_name, repo);
7594 free(commit_ref_name);
7598 const struct got_error *
7599 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7600 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7601 struct got_worktree *worktree, const char *refname,
7602 struct got_repository *repo)
7604 const struct got_error *err = NULL;
7605 char *fileindex_path = NULL;
7606 struct check_rebase_ok_arg ok_arg;
7610 *base_branch_ref = NULL;
7612 err = lock_worktree(worktree, LOCK_EX);
7616 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7617 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7618 "cannot integrate a branch into itself; "
7619 "update -b or different branch name required");
7623 err = open_fileindex(fileindex, &fileindex_path, worktree);
7627 /* Preconditions are the same as for rebase. */
7628 ok_arg.worktree = worktree;
7630 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7635 err = got_ref_open(branch_ref, repo, refname, 1);
7639 err = got_ref_open(base_branch_ref, repo,
7640 got_worktree_get_head_ref_name(worktree), 1);
7644 got_ref_close(*branch_ref);
7647 if (*base_branch_ref) {
7648 got_ref_close(*base_branch_ref);
7649 *base_branch_ref = NULL;
7652 got_fileindex_free(*fileindex);
7655 lock_worktree(worktree, LOCK_SH);
7660 const struct got_error *
7661 got_worktree_integrate_continue(struct got_worktree *worktree,
7662 struct got_fileindex *fileindex, struct got_repository *repo,
7663 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7664 got_worktree_checkout_cb progress_cb, void *progress_arg,
7665 got_cancel_cb cancel_cb, void *cancel_arg)
7667 const struct got_error *err = NULL, *sync_err, *unlockerr;
7668 char *fileindex_path = NULL;
7669 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7670 struct got_commit_object *commit = NULL;
7672 err = get_fileindex_path(&fileindex_path, worktree);
7676 err = got_ref_resolve(&commit_id, repo, branch_ref);
7680 err = got_object_open_as_commit(&commit, repo, commit_id);
7684 err = got_object_id_by_path(&tree_id, repo, commit,
7685 worktree->path_prefix);
7689 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7693 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7694 progress_cb, progress_arg, cancel_cb, cancel_arg);
7698 err = got_ref_change_ref(base_branch_ref, commit_id);
7702 err = got_ref_write(base_branch_ref, repo);
7706 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7708 sync_err = sync_fileindex(fileindex, fileindex_path);
7709 if (sync_err && err == NULL)
7713 unlockerr = got_ref_unlock(branch_ref);
7714 if (unlockerr && err == NULL)
7716 got_ref_close(branch_ref);
7718 unlockerr = got_ref_unlock(base_branch_ref);
7719 if (unlockerr && err == NULL)
7721 got_ref_close(base_branch_ref);
7723 got_fileindex_free(fileindex);
7724 free(fileindex_path);
7727 got_object_commit_close(commit);
7729 unlockerr = lock_worktree(worktree, LOCK_SH);
7730 if (unlockerr && err == NULL)
7735 const struct got_error *
7736 got_worktree_integrate_abort(struct got_worktree *worktree,
7737 struct got_fileindex *fileindex, struct got_repository *repo,
7738 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7740 const struct got_error *err = NULL, *unlockerr = NULL;
7742 got_fileindex_free(fileindex);
7744 err = lock_worktree(worktree, LOCK_SH);
7746 unlockerr = got_ref_unlock(branch_ref);
7747 if (unlockerr && err == NULL)
7749 got_ref_close(branch_ref);
7751 unlockerr = got_ref_unlock(base_branch_ref);
7752 if (unlockerr && err == NULL)
7754 got_ref_close(base_branch_ref);
7759 const struct got_error *
7760 got_worktree_merge_postpone(struct got_worktree *worktree,
7761 struct got_fileindex *fileindex)
7763 const struct got_error *err, *sync_err;
7764 char *fileindex_path = NULL;
7766 err = get_fileindex_path(&fileindex_path, worktree);
7770 sync_err = sync_fileindex(fileindex, fileindex_path);
7772 err = lock_worktree(worktree, LOCK_SH);
7773 if (sync_err && err == NULL)
7776 got_fileindex_free(fileindex);
7777 free(fileindex_path);
7781 static const struct got_error *
7782 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7784 const struct got_error *err;
7785 char *branch_refname = NULL, *commit_refname = NULL;
7787 err = get_merge_branch_ref_name(&branch_refname, worktree);
7790 err = delete_ref(branch_refname, repo);
7794 err = get_merge_commit_ref_name(&commit_refname, worktree);
7797 err = delete_ref(commit_refname, repo);
7802 free(branch_refname);
7803 free(commit_refname);
7807 struct merge_commit_msg_arg {
7808 struct got_worktree *worktree;
7809 const char *branch_name;
7812 static const struct got_error *
7813 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
7814 const char *diff_path, char **logmsg, void *arg)
7816 struct merge_commit_msg_arg *a = arg;
7818 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7819 got_worktree_get_head_ref_name(a->worktree)) == -1)
7820 return got_error_from_errno("asprintf");
7826 const struct got_error *
7827 got_worktree_merge_branch(struct got_worktree *worktree,
7828 struct got_fileindex *fileindex,
7829 struct got_object_id *yca_commit_id,
7830 struct got_object_id *branch_tip,
7831 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7832 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7834 const struct got_error *err;
7835 char *fileindex_path = NULL;
7837 err = get_fileindex_path(&fileindex_path, worktree);
7841 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7846 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7847 branch_tip, repo, progress_cb, progress_arg,
7848 cancel_cb, cancel_arg);
7850 free(fileindex_path);
7854 const struct got_error *
7855 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7856 struct got_worktree *worktree, struct got_fileindex *fileindex,
7857 const char *author, const char *committer, int allow_bad_symlinks,
7858 struct got_object_id *branch_tip, const char *branch_name,
7859 struct got_repository *repo,
7860 got_worktree_status_cb status_cb, void *status_arg)
7863 const struct got_error *err = NULL, *sync_err;
7864 struct got_pathlist_head commitable_paths;
7865 struct collect_commitables_arg cc_arg;
7866 struct got_pathlist_entry *pe;
7867 struct got_reference *head_ref = NULL;
7868 struct got_object_id *head_commit_id = NULL;
7869 int have_staged_files = 0;
7870 struct merge_commit_msg_arg mcm_arg;
7871 char *fileindex_path = NULL;
7873 memset(&cc_arg, 0, sizeof(cc_arg));
7874 *new_commit_id = NULL;
7876 TAILQ_INIT(&commitable_paths);
7878 err = get_fileindex_path(&fileindex_path, worktree);
7882 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7886 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7890 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7891 &have_staged_files);
7892 if (err && err->code != GOT_ERR_CANCELLED)
7894 if (have_staged_files) {
7895 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7899 cc_arg.commitable_paths = &commitable_paths;
7900 cc_arg.worktree = worktree;
7901 cc_arg.fileindex = fileindex;
7903 cc_arg.have_staged_files = have_staged_files;
7904 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7905 err = worktree_status(worktree, "", fileindex, repo,
7906 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7910 if (TAILQ_EMPTY(&commitable_paths)) {
7911 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7912 "merge of %s cannot proceed", branch_name);
7916 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7917 struct got_commitable *ct = pe->data;
7918 const char *ct_path = ct->in_repo_path;
7920 while (ct_path[0] == '/')
7922 err = check_out_of_date(ct_path, ct->status,
7923 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7924 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7930 mcm_arg.worktree = worktree;
7931 mcm_arg.branch_name = branch_name;
7932 err = commit_worktree(new_commit_id, &commitable_paths,
7933 head_commit_id, branch_tip, worktree, author, committer, NULL,
7934 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7938 err = update_fileindex_after_commit(worktree, &commitable_paths,
7939 *new_commit_id, fileindex, have_staged_files);
7940 sync_err = sync_fileindex(fileindex, fileindex_path);
7941 if (sync_err && err == NULL)
7944 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7945 struct got_commitable *ct = pe->data;
7946 free_commitable(ct);
7948 got_pathlist_free(&commitable_paths);
7949 free(fileindex_path);
7953 const struct got_error *
7954 got_worktree_merge_complete(struct got_worktree *worktree,
7955 struct got_fileindex *fileindex, struct got_repository *repo)
7957 const struct got_error *err, *unlockerr, *sync_err;
7958 char *fileindex_path = NULL;
7960 err = delete_merge_refs(worktree, repo);
7964 err = get_fileindex_path(&fileindex_path, worktree);
7967 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7968 sync_err = sync_fileindex(fileindex, fileindex_path);
7969 if (sync_err && err == NULL)
7972 got_fileindex_free(fileindex);
7973 free(fileindex_path);
7974 unlockerr = lock_worktree(worktree, LOCK_SH);
7975 if (unlockerr && err == NULL)
7980 const struct got_error *
7981 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7982 struct got_repository *repo)
7984 const struct got_error *err;
7985 char *branch_refname = NULL;
7986 struct got_reference *branch_ref = NULL;
7990 err = get_merge_branch_ref_name(&branch_refname, worktree);
7993 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7994 free(branch_refname);
7996 if (err->code != GOT_ERR_NOT_REF)
8004 const struct got_error *got_worktree_merge_prepare(
8005 struct got_fileindex **fileindex, struct got_worktree *worktree,
8006 struct got_reference *branch, struct got_repository *repo)
8008 const struct got_error *err = NULL;
8009 char *fileindex_path = NULL;
8010 char *branch_refname = NULL, *commit_refname = NULL;
8011 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
8012 struct got_reference *commit_ref = NULL;
8013 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
8014 struct check_rebase_ok_arg ok_arg;
8018 err = lock_worktree(worktree, LOCK_EX);
8022 err = open_fileindex(fileindex, &fileindex_path, worktree);
8026 /* Preconditions are the same as for rebase. */
8027 ok_arg.worktree = worktree;
8029 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8034 err = get_merge_branch_ref_name(&branch_refname, worktree);
8038 err = get_merge_commit_ref_name(&commit_refname, worktree);
8042 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8047 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8051 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8052 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8056 err = got_ref_resolve(&branch_tip, repo, branch);
8060 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8063 err = got_ref_write(branch_ref, repo);
8067 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8070 err = got_ref_write(commit_ref, repo);
8075 free(branch_refname);
8076 free(commit_refname);
8077 free(fileindex_path);
8079 got_ref_close(branch_ref);
8081 got_ref_close(commit_ref);
8083 got_ref_close(wt_branch);
8084 free(wt_branch_tip);
8087 got_fileindex_free(*fileindex);
8090 lock_worktree(worktree, LOCK_SH);
8095 const struct got_error *
8096 got_worktree_merge_continue(char **branch_name,
8097 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8098 struct got_worktree *worktree, struct got_repository *repo)
8100 const struct got_error *err;
8101 char *commit_refname = NULL, *branch_refname = NULL;
8102 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8103 char *fileindex_path = NULL;
8104 int have_staged_files = 0;
8106 *branch_name = NULL;
8110 err = lock_worktree(worktree, LOCK_EX);
8114 err = open_fileindex(fileindex, &fileindex_path, worktree);
8118 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8119 &have_staged_files);
8120 if (err && err->code != GOT_ERR_CANCELLED)
8122 if (have_staged_files) {
8123 err = got_error(GOT_ERR_STAGED_PATHS);
8127 err = get_merge_branch_ref_name(&branch_refname, worktree);
8131 err = get_merge_commit_ref_name(&commit_refname, worktree);
8135 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8139 if (!got_ref_is_symbolic(branch_ref)) {
8140 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8141 "%s is not a symbolic reference",
8142 got_ref_get_name(branch_ref));
8145 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8146 if (*branch_name == NULL) {
8147 err = got_error_from_errno("strdup");
8151 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8155 err = got_ref_resolve(branch_tip, repo, commit_ref);
8159 free(commit_refname);
8160 free(branch_refname);
8161 free(fileindex_path);
8163 got_ref_close(commit_ref);
8165 got_ref_close(branch_ref);
8169 *branch_name = NULL;
8174 got_fileindex_free(*fileindex);
8177 lock_worktree(worktree, LOCK_SH);
8182 const struct got_error *
8183 got_worktree_merge_abort(struct got_worktree *worktree,
8184 struct got_fileindex *fileindex, struct got_repository *repo,
8185 got_worktree_checkout_cb progress_cb, void *progress_arg)
8187 const struct got_error *err, *unlockerr, *sync_err;
8188 struct got_object_id *commit_id = NULL;
8189 struct got_commit_object *commit = NULL;
8190 char *fileindex_path = NULL;
8191 struct revert_file_args rfa;
8192 struct got_object_id *tree_id = NULL;
8194 err = got_object_open_as_commit(&commit, repo,
8195 worktree->base_commit_id);
8199 err = got_object_id_by_path(&tree_id, repo, commit,
8200 worktree->path_prefix);
8204 err = delete_merge_refs(worktree, repo);
8208 err = get_fileindex_path(&fileindex_path, worktree);
8212 rfa.worktree = worktree;
8213 rfa.fileindex = fileindex;
8214 rfa.progress_cb = progress_cb;
8215 rfa.progress_arg = progress_arg;
8216 rfa.patch_cb = NULL;
8217 rfa.patch_arg = NULL;
8219 rfa.unlink_added_files = 1;
8220 err = worktree_status(worktree, "", fileindex, repo,
8221 revert_file, &rfa, NULL, NULL, 1, 0);
8225 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8226 repo, progress_cb, progress_arg, NULL, NULL);
8228 sync_err = sync_fileindex(fileindex, fileindex_path);
8229 if (sync_err && err == NULL)
8235 got_object_commit_close(commit);
8237 got_fileindex_free(fileindex);
8238 free(fileindex_path);
8240 unlockerr = lock_worktree(worktree, LOCK_SH);
8241 if (unlockerr && err == NULL)
8246 struct check_stage_ok_arg {
8247 struct got_object_id *head_commit_id;
8248 struct got_worktree *worktree;
8249 struct got_fileindex *fileindex;
8250 struct got_repository *repo;
8254 static const struct got_error *
8255 check_stage_ok(void *arg, unsigned char status,
8256 unsigned char staged_status, const char *relpath,
8257 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8258 struct got_object_id *commit_id, int dirfd, const char *de_name)
8260 struct check_stage_ok_arg *a = arg;
8261 const struct got_error *err = NULL;
8262 struct got_fileindex_entry *ie;
8263 struct got_object_id base_commit_id;
8264 struct got_object_id *base_commit_idp = NULL;
8265 char *in_repo_path = NULL, *p;
8267 if (status == GOT_STATUS_UNVERSIONED ||
8268 status == GOT_STATUS_NO_CHANGE)
8270 if (status == GOT_STATUS_NONEXISTENT)
8271 return got_error_set_errno(ENOENT, relpath);
8273 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8275 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8277 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8278 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8280 return got_error_from_errno("asprintf");
8282 if (got_fileindex_entry_has_commit(ie)) {
8283 memcpy(base_commit_id.sha1, ie->commit_sha1,
8284 SHA1_DIGEST_LENGTH);
8285 base_commit_idp = &base_commit_id;
8288 if (status == GOT_STATUS_CONFLICT) {
8289 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8291 } else if (status != GOT_STATUS_ADD &&
8292 status != GOT_STATUS_MODIFY &&
8293 status != GOT_STATUS_DELETE) {
8294 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8298 a->have_changes = 1;
8303 err = check_out_of_date(p, status, staged_status,
8304 blob_id, base_commit_idp, a->head_commit_id, a->repo,
8305 GOT_ERR_STAGE_OUT_OF_DATE);
8311 struct stage_path_arg {
8312 struct got_worktree *worktree;
8313 struct got_fileindex *fileindex;
8314 struct got_repository *repo;
8315 got_worktree_status_cb status_cb;
8317 got_worktree_patch_cb patch_cb;
8319 int staged_something;
8320 int allow_bad_symlinks;
8323 static const struct got_error *
8324 stage_path(void *arg, unsigned char status,
8325 unsigned char staged_status, const char *relpath,
8326 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8327 struct got_object_id *commit_id, int dirfd, const char *de_name)
8329 struct stage_path_arg *a = arg;
8330 const struct got_error *err = NULL;
8331 struct got_fileindex_entry *ie;
8332 char *ondisk_path = NULL, *path_content = NULL;
8334 struct got_object_id *new_staged_blob_id = NULL;
8337 if (status == GOT_STATUS_UNVERSIONED)
8340 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8342 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8344 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8346 return got_error_from_errno("asprintf");
8349 case GOT_STATUS_ADD:
8350 case GOT_STATUS_MODIFY:
8351 /* XXX could sb.st_mode be passed in by our caller? */
8352 if (lstat(ondisk_path, &sb) == -1) {
8353 err = got_error_from_errno2("lstat", ondisk_path);
8357 if (status == GOT_STATUS_ADD) {
8358 int choice = GOT_PATCH_CHOICE_NONE;
8359 err = (*a->patch_cb)(&choice, a->patch_arg,
8360 status, ie->path, NULL, 1, 1);
8363 if (choice != GOT_PATCH_CHOICE_YES)
8366 err = create_patched_content(&path_content, 0,
8367 staged_blob_id ? staged_blob_id : blob_id,
8368 ondisk_path, dirfd, de_name, ie->path,
8369 a->repo, a->patch_cb, a->patch_arg);
8370 if (err || path_content == NULL)
8374 err = got_object_blob_create(&new_staged_blob_id,
8375 path_content ? path_content : ondisk_path, a->repo);
8378 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8379 SHA1_DIGEST_LENGTH);
8380 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8381 stage = GOT_FILEIDX_STAGE_ADD;
8383 stage = GOT_FILEIDX_STAGE_MODIFY;
8384 got_fileindex_entry_stage_set(ie, stage);
8385 if (S_ISLNK(sb.st_mode)) {
8386 int is_bad_symlink = 0;
8387 if (!a->allow_bad_symlinks) {
8388 char target_path[PATH_MAX];
8390 target_len = readlink(ondisk_path, target_path,
8391 sizeof(target_path));
8392 if (target_len == -1) {
8393 err = got_error_from_errno2("readlink",
8397 err = is_bad_symlink_target(&is_bad_symlink,
8398 target_path, target_len, ondisk_path,
8399 a->worktree->root_path);
8402 if (is_bad_symlink) {
8403 err = got_error_path(ondisk_path,
8404 GOT_ERR_BAD_SYMLINK);
8409 got_fileindex_entry_staged_filetype_set(ie,
8410 GOT_FILEIDX_MODE_BAD_SYMLINK);
8412 got_fileindex_entry_staged_filetype_set(ie,
8413 GOT_FILEIDX_MODE_SYMLINK);
8415 got_fileindex_entry_staged_filetype_set(ie,
8416 GOT_FILEIDX_MODE_REGULAR_FILE);
8418 a->staged_something = 1;
8419 if (a->status_cb == NULL)
8421 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8422 get_staged_status(ie), relpath, blob_id,
8423 new_staged_blob_id, NULL, dirfd, de_name);
8427 * When staging the reverse of the staged diff,
8428 * implicitly unstage the file.
8430 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8431 sizeof(ie->blob_sha1)) == 0) {
8432 got_fileindex_entry_stage_set(ie,
8433 GOT_FILEIDX_STAGE_NONE);
8436 case GOT_STATUS_DELETE:
8437 if (staged_status == GOT_STATUS_DELETE)
8440 int choice = GOT_PATCH_CHOICE_NONE;
8441 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8442 ie->path, NULL, 1, 1);
8445 if (choice == GOT_PATCH_CHOICE_NO)
8447 if (choice != GOT_PATCH_CHOICE_YES) {
8448 err = got_error(GOT_ERR_PATCH_CHOICE);
8452 stage = GOT_FILEIDX_STAGE_DELETE;
8453 got_fileindex_entry_stage_set(ie, stage);
8454 a->staged_something = 1;
8455 if (a->status_cb == NULL)
8457 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8458 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8461 case GOT_STATUS_NO_CHANGE:
8463 case GOT_STATUS_CONFLICT:
8464 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8466 case GOT_STATUS_NONEXISTENT:
8467 err = got_error_set_errno(ENOENT, relpath);
8470 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8474 if (path_content && unlink(path_content) == -1 && err == NULL)
8475 err = got_error_from_errno2("unlink", path_content);
8478 free(new_staged_blob_id);
8482 const struct got_error *
8483 got_worktree_stage(struct got_worktree *worktree,
8484 struct got_pathlist_head *paths,
8485 got_worktree_status_cb status_cb, void *status_arg,
8486 got_worktree_patch_cb patch_cb, void *patch_arg,
8487 int allow_bad_symlinks, struct got_repository *repo)
8489 const struct got_error *err = NULL, *sync_err, *unlockerr;
8490 struct got_pathlist_entry *pe;
8491 struct got_fileindex *fileindex = NULL;
8492 char *fileindex_path = NULL;
8493 struct got_reference *head_ref = NULL;
8494 struct got_object_id *head_commit_id = NULL;
8495 struct check_stage_ok_arg oka;
8496 struct stage_path_arg spa;
8498 err = lock_worktree(worktree, LOCK_EX);
8502 err = got_ref_open(&head_ref, repo,
8503 got_worktree_get_head_ref_name(worktree), 0);
8506 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8509 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8513 /* Check pre-conditions before staging anything. */
8514 oka.head_commit_id = head_commit_id;
8515 oka.worktree = worktree;
8516 oka.fileindex = fileindex;
8518 oka.have_changes = 0;
8519 TAILQ_FOREACH(pe, paths, entry) {
8520 err = worktree_status(worktree, pe->path, fileindex, repo,
8521 check_stage_ok, &oka, NULL, NULL, 1, 0);
8525 if (!oka.have_changes) {
8526 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8530 spa.worktree = worktree;
8531 spa.fileindex = fileindex;
8533 spa.patch_cb = patch_cb;
8534 spa.patch_arg = patch_arg;
8535 spa.status_cb = status_cb;
8536 spa.status_arg = status_arg;
8537 spa.staged_something = 0;
8538 spa.allow_bad_symlinks = allow_bad_symlinks;
8539 TAILQ_FOREACH(pe, paths, entry) {
8540 err = worktree_status(worktree, pe->path, fileindex, repo,
8541 stage_path, &spa, NULL, NULL, 1, 0);
8545 if (!spa.staged_something) {
8546 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8550 sync_err = sync_fileindex(fileindex, fileindex_path);
8551 if (sync_err && err == NULL)
8555 got_ref_close(head_ref);
8556 free(head_commit_id);
8557 free(fileindex_path);
8559 got_fileindex_free(fileindex);
8560 unlockerr = lock_worktree(worktree, LOCK_SH);
8561 if (unlockerr && err == NULL)
8566 struct unstage_path_arg {
8567 struct got_worktree *worktree;
8568 struct got_fileindex *fileindex;
8569 struct got_repository *repo;
8570 got_worktree_checkout_cb progress_cb;
8572 got_worktree_patch_cb patch_cb;
8576 static const struct got_error *
8577 create_unstaged_content(char **path_unstaged_content,
8578 char **path_new_staged_content, struct got_object_id *blob_id,
8579 struct got_object_id *staged_blob_id, const char *relpath,
8580 struct got_repository *repo,
8581 got_worktree_patch_cb patch_cb, void *patch_arg)
8583 const struct got_error *err, *free_err;
8584 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8585 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8586 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8587 struct got_diffreg_result *diffreg_result = NULL;
8588 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8589 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8590 int fd1 = -1, fd2 = -1;
8592 *path_unstaged_content = NULL;
8593 *path_new_staged_content = NULL;
8595 err = got_object_id_str(&label1, blob_id);
8599 fd1 = got_opentempfd();
8601 err = got_error_from_errno("got_opentempfd");
8604 fd2 = got_opentempfd();
8606 err = got_error_from_errno("got_opentempfd");
8610 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8614 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
8618 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8622 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8627 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
8631 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8635 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8636 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8640 err = got_opentemp_named(path_unstaged_content, &outfile,
8641 "got-unstaged-content");
8644 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8645 "got-new-staged-content");
8649 if (fseek(f1, 0L, SEEK_SET) == -1) {
8650 err = got_ferror(f1, GOT_ERR_IO);
8653 if (fseek(f2, 0L, SEEK_SET) == -1) {
8654 err = got_ferror(f2, GOT_ERR_IO);
8657 /* Count the number of actual changes in the diff result. */
8658 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8659 struct diff_chunk_context cc = {};
8660 diff_chunk_context_load_change(&cc, &nchunks_used,
8661 diffreg_result->result, n, 0);
8664 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8666 err = apply_or_reject_change(&choice, &nchunks_used,
8667 diffreg_result->result, n, relpath, f1, f2,
8668 &line_cur1, &line_cur2,
8669 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8672 if (choice == GOT_PATCH_CHOICE_YES)
8675 have_rejected_content = 1;
8676 if (choice == GOT_PATCH_CHOICE_QUIT)
8679 if (have_content || have_rejected_content)
8680 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8681 outfile, rejectfile);
8684 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8685 err = got_error_from_errno("close");
8687 got_object_blob_close(blob);
8688 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8689 err = got_error_from_errno("close");
8691 got_object_blob_close(staged_blob);
8692 free_err = got_diffreg_result_free(diffreg_result);
8693 if (free_err && err == NULL)
8695 if (f1 && fclose(f1) == EOF && err == NULL)
8696 err = got_error_from_errno2("fclose", path1);
8697 if (f2 && fclose(f2) == EOF && err == NULL)
8698 err = got_error_from_errno2("fclose", path2);
8699 if (outfile && fclose(outfile) == EOF && err == NULL)
8700 err = got_error_from_errno2("fclose", *path_unstaged_content);
8701 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8702 err = got_error_from_errno2("fclose", *path_new_staged_content);
8703 if (path1 && unlink(path1) == -1 && err == NULL)
8704 err = got_error_from_errno2("unlink", path1);
8705 if (path2 && unlink(path2) == -1 && err == NULL)
8706 err = got_error_from_errno2("unlink", path2);
8707 if (err || !have_content) {
8708 if (*path_unstaged_content &&
8709 unlink(*path_unstaged_content) == -1 && err == NULL)
8710 err = got_error_from_errno2("unlink",
8711 *path_unstaged_content);
8712 free(*path_unstaged_content);
8713 *path_unstaged_content = NULL;
8715 if (err || !have_content || !have_rejected_content) {
8716 if (*path_new_staged_content &&
8717 unlink(*path_new_staged_content) == -1 && err == NULL)
8718 err = got_error_from_errno2("unlink",
8719 *path_new_staged_content);
8720 free(*path_new_staged_content);
8721 *path_new_staged_content = NULL;
8728 static const struct got_error *
8729 unstage_hunks(struct got_object_id *staged_blob_id,
8730 struct got_blob_object *blob_base,
8731 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8732 const char *ondisk_path, const char *label_orig,
8733 struct got_worktree *worktree, struct got_repository *repo,
8734 got_worktree_patch_cb patch_cb, void *patch_arg,
8735 got_worktree_checkout_cb progress_cb, void *progress_arg)
8737 const struct got_error *err = NULL;
8738 char *path_unstaged_content = NULL;
8739 char *path_new_staged_content = NULL;
8740 char *parent = NULL, *base_path = NULL;
8741 char *blob_base_path = NULL;
8742 struct got_object_id *new_staged_blob_id = NULL;
8743 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8746 err = create_unstaged_content(&path_unstaged_content,
8747 &path_new_staged_content, blob_id, staged_blob_id,
8748 ie->path, repo, patch_cb, patch_arg);
8752 if (path_unstaged_content == NULL)
8755 if (path_new_staged_content) {
8756 err = got_object_blob_create(&new_staged_blob_id,
8757 path_new_staged_content, repo);
8762 f = fopen(path_unstaged_content, "re");
8764 err = got_error_from_errno2("fopen",
8765 path_unstaged_content);
8768 if (fstat(fileno(f), &sb) == -1) {
8769 err = got_error_from_errno2("fstat", path_unstaged_content);
8772 if (got_fileindex_entry_staged_filetype_get(ie) ==
8773 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8774 char link_target[PATH_MAX];
8776 r = fread(link_target, 1, sizeof(link_target), f);
8777 if (r == 0 && ferror(f)) {
8778 err = got_error_from_errno("fread");
8781 if (r >= sizeof(link_target)) { /* should not happen */
8782 err = got_error(GOT_ERR_NO_SPACE);
8785 link_target[r] = '\0';
8786 err = merge_symlink(worktree, blob_base,
8787 ondisk_path, ie->path, label_orig, link_target,
8788 worktree->base_commit_id, repo, progress_cb,
8791 int local_changes_subsumed;
8793 err = got_path_dirname(&parent, ondisk_path);
8797 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8799 err = got_error_from_errno("asprintf");
8804 err = got_opentemp_named(&blob_base_path, &f_base, base_path);
8807 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8813 * In order the run a 3-way merge with a symlink we copy the symlink's
8814 * target path into a temporary file and use that file with diff3.
8816 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8817 err = dump_symlink_target_path_to_file(&f_deriv2,
8823 fd = open(ondisk_path,
8824 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8826 err = got_error_from_errno2("open", ondisk_path);
8829 f_deriv2 = fdopen(fd, "r");
8830 if (f_deriv2 == NULL) {
8831 err = got_error_from_errno2("fdopen", ondisk_path);
8837 err = merge_file(&local_changes_subsumed, worktree,
8838 f_base, f, f_deriv2, ondisk_path, ie->path,
8839 got_fileindex_perms_to_st(ie),
8840 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8841 repo, progress_cb, progress_arg);
8846 if (new_staged_blob_id) {
8847 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8848 SHA1_DIGEST_LENGTH);
8850 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8851 got_fileindex_entry_staged_filetype_set(ie, 0);
8854 free(new_staged_blob_id);
8855 if (path_unstaged_content &&
8856 unlink(path_unstaged_content) == -1 && err == NULL)
8857 err = got_error_from_errno2("unlink", path_unstaged_content);
8858 if (path_new_staged_content &&
8859 unlink(path_new_staged_content) == -1 && err == NULL)
8860 err = got_error_from_errno2("unlink", path_new_staged_content);
8861 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8862 err = got_error_from_errno2("unlink", blob_base_path);
8863 if (f_base && fclose(f_base) == EOF && err == NULL)
8864 err = got_error_from_errno2("fclose", path_unstaged_content);
8865 if (f && fclose(f) == EOF && err == NULL)
8866 err = got_error_from_errno2("fclose", path_unstaged_content);
8867 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8868 err = got_error_from_errno2("fclose", ondisk_path);
8869 free(path_unstaged_content);
8870 free(path_new_staged_content);
8871 free(blob_base_path);
8877 static const struct got_error *
8878 unstage_path(void *arg, unsigned char status,
8879 unsigned char staged_status, const char *relpath,
8880 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8881 struct got_object_id *commit_id, int dirfd, const char *de_name)
8883 const struct got_error *err = NULL;
8884 struct unstage_path_arg *a = arg;
8885 struct got_fileindex_entry *ie;
8886 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8887 char *ondisk_path = NULL;
8888 char *id_str = NULL, *label_orig = NULL;
8889 int local_changes_subsumed;
8891 int fd1 = -1, fd2 = -1;
8893 if (staged_status != GOT_STATUS_ADD &&
8894 staged_status != GOT_STATUS_MODIFY &&
8895 staged_status != GOT_STATUS_DELETE)
8898 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8900 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8902 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8904 return got_error_from_errno("asprintf");
8906 err = got_object_id_str(&id_str,
8907 commit_id ? commit_id : a->worktree->base_commit_id);
8910 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8912 err = got_error_from_errno("asprintf");
8916 fd1 = got_opentempfd();
8918 err = got_error_from_errno("got_opentempfd");
8921 fd2 = got_opentempfd();
8923 err = got_error_from_errno("got_opentempfd");
8927 switch (staged_status) {
8928 case GOT_STATUS_MODIFY:
8929 err = got_object_open_as_blob(&blob_base, a->repo,
8930 blob_id, 8192, fd1);
8934 case GOT_STATUS_ADD:
8936 if (staged_status == GOT_STATUS_ADD) {
8937 int choice = GOT_PATCH_CHOICE_NONE;
8938 err = (*a->patch_cb)(&choice, a->patch_arg,
8939 staged_status, ie->path, NULL, 1, 1);
8942 if (choice != GOT_PATCH_CHOICE_YES)
8945 err = unstage_hunks(staged_blob_id,
8946 blob_base, blob_id, ie, ondisk_path,
8947 label_orig, a->worktree, a->repo,
8948 a->patch_cb, a->patch_arg,
8949 a->progress_cb, a->progress_arg);
8950 break; /* Done with this file. */
8953 err = got_object_open_as_blob(&blob_staged, a->repo,
8954 staged_blob_id, 8192, fd2);
8957 switch (got_fileindex_entry_staged_filetype_get(ie)) {
8958 case GOT_FILEIDX_MODE_BAD_SYMLINK:
8959 case GOT_FILEIDX_MODE_REGULAR_FILE:
8960 err = merge_blob(&local_changes_subsumed, a->worktree,
8961 blob_base, ondisk_path, relpath,
8962 got_fileindex_perms_to_st(ie), label_orig,
8963 blob_staged, commit_id ? commit_id :
8964 a->worktree->base_commit_id, a->repo,
8965 a->progress_cb, a->progress_arg);
8967 case GOT_FILEIDX_MODE_SYMLINK:
8968 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8969 char *staged_target;
8970 err = got_object_blob_read_to_str(
8971 &staged_target, blob_staged);
8974 err = merge_symlink(a->worktree, blob_base,
8975 ondisk_path, relpath, label_orig,
8976 staged_target, commit_id ? commit_id :
8977 a->worktree->base_commit_id,
8978 a->repo, a->progress_cb, a->progress_arg);
8979 free(staged_target);
8981 err = merge_blob(&local_changes_subsumed,
8982 a->worktree, blob_base, ondisk_path,
8983 relpath, got_fileindex_perms_to_st(ie),
8984 label_orig, blob_staged,
8985 commit_id ? commit_id :
8986 a->worktree->base_commit_id, a->repo,
8987 a->progress_cb, a->progress_arg);
8991 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8995 got_fileindex_entry_stage_set(ie,
8996 GOT_FILEIDX_STAGE_NONE);
8997 got_fileindex_entry_staged_filetype_set(ie, 0);
9000 case GOT_STATUS_DELETE:
9002 int choice = GOT_PATCH_CHOICE_NONE;
9003 err = (*a->patch_cb)(&choice, a->patch_arg,
9004 staged_status, ie->path, NULL, 1, 1);
9007 if (choice == GOT_PATCH_CHOICE_NO)
9009 if (choice != GOT_PATCH_CHOICE_YES) {
9010 err = got_error(GOT_ERR_PATCH_CHOICE);
9014 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9015 got_fileindex_entry_staged_filetype_set(ie, 0);
9016 err = get_file_status(&status, &sb, ie, ondisk_path,
9017 dirfd, de_name, a->repo);
9020 err = (*a->progress_cb)(a->progress_arg, status, relpath);
9025 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9026 err = got_error_from_errno("close");
9028 got_object_blob_close(blob_base);
9029 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9030 err = got_error_from_errno("close");
9032 got_object_blob_close(blob_staged);
9038 const struct got_error *
9039 got_worktree_unstage(struct got_worktree *worktree,
9040 struct got_pathlist_head *paths,
9041 got_worktree_checkout_cb progress_cb, void *progress_arg,
9042 got_worktree_patch_cb patch_cb, void *patch_arg,
9043 struct got_repository *repo)
9045 const struct got_error *err = NULL, *sync_err, *unlockerr;
9046 struct got_pathlist_entry *pe;
9047 struct got_fileindex *fileindex = NULL;
9048 char *fileindex_path = NULL;
9049 struct unstage_path_arg upa;
9051 err = lock_worktree(worktree, LOCK_EX);
9055 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9059 upa.worktree = worktree;
9060 upa.fileindex = fileindex;
9062 upa.progress_cb = progress_cb;
9063 upa.progress_arg = progress_arg;
9064 upa.patch_cb = patch_cb;
9065 upa.patch_arg = patch_arg;
9066 TAILQ_FOREACH(pe, paths, entry) {
9067 err = worktree_status(worktree, pe->path, fileindex, repo,
9068 unstage_path, &upa, NULL, NULL, 1, 0);
9073 sync_err = sync_fileindex(fileindex, fileindex_path);
9074 if (sync_err && err == NULL)
9077 free(fileindex_path);
9079 got_fileindex_free(fileindex);
9080 unlockerr = lock_worktree(worktree, LOCK_SH);
9081 if (unlockerr && err == NULL)
9086 struct report_file_info_arg {
9087 struct got_worktree *worktree;
9088 got_worktree_path_info_cb info_cb;
9090 struct got_pathlist_head *paths;
9091 got_cancel_cb cancel_cb;
9095 static const struct got_error *
9096 report_file_info(void *arg, struct got_fileindex_entry *ie)
9098 struct report_file_info_arg *a = arg;
9099 struct got_pathlist_entry *pe;
9100 struct got_object_id blob_id, staged_blob_id, commit_id;
9101 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9102 struct got_object_id *commit_idp = NULL;
9105 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9106 return got_error(GOT_ERR_CANCELLED);
9108 TAILQ_FOREACH(pe, a->paths, entry) {
9109 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9110 got_path_is_child(ie->path, pe->path, pe->path_len))
9113 if (pe == NULL) /* not found */
9116 if (got_fileindex_entry_has_blob(ie)) {
9117 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
9118 blob_idp = &blob_id;
9120 stage = got_fileindex_entry_stage_get(ie);
9121 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9122 stage == GOT_FILEIDX_STAGE_ADD) {
9123 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
9124 SHA1_DIGEST_LENGTH);
9125 staged_blob_idp = &staged_blob_id;
9128 if (got_fileindex_entry_has_commit(ie)) {
9129 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
9130 commit_idp = &commit_id;
9133 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9134 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9137 const struct got_error *
9138 got_worktree_path_info(struct got_worktree *worktree,
9139 struct got_pathlist_head *paths,
9140 got_worktree_path_info_cb info_cb, void *info_arg,
9141 got_cancel_cb cancel_cb, void *cancel_arg)
9144 const struct got_error *err = NULL, *unlockerr;
9145 struct got_fileindex *fileindex = NULL;
9146 char *fileindex_path = NULL;
9147 struct report_file_info_arg arg;
9149 err = lock_worktree(worktree, LOCK_SH);
9153 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9157 arg.worktree = worktree;
9158 arg.info_cb = info_cb;
9159 arg.info_arg = info_arg;
9161 arg.cancel_cb = cancel_cb;
9162 arg.cancel_arg = cancel_arg;
9163 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9166 free(fileindex_path);
9168 got_fileindex_free(fileindex);
9169 unlockerr = lock_worktree(worktree, LOCK_UN);
9170 if (unlockerr && err == NULL)
9175 static const struct got_error *
9176 patch_check_path(const char *p, char **path, unsigned char *status,
9177 unsigned char *staged_status, struct got_fileindex *fileindex,
9178 struct got_worktree *worktree, struct got_repository *repo)
9180 const struct got_error *err;
9181 struct got_fileindex_entry *ie;
9183 char *ondisk_path = NULL;
9185 err = got_worktree_resolve_path(path, worktree, p);
9189 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9190 *path[0] ? "/" : "", *path) == -1)
9191 return got_error_from_errno("asprintf");
9193 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9195 *staged_status = get_staged_status(ie);
9196 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9201 *staged_status = GOT_STATUS_NO_CHANGE;
9202 *status = GOT_STATUS_UNVERSIONED;
9203 if (lstat(ondisk_path, &sb) == -1) {
9204 if (errno != ENOENT) {
9205 err = got_error_from_errno2("lstat",
9209 *status = GOT_STATUS_NONEXISTENT;
9218 static const struct got_error *
9219 patch_can_rm(const char *path, unsigned char status,
9220 unsigned char staged_status)
9222 if (status == GOT_STATUS_NONEXISTENT)
9223 return got_error_set_errno(ENOENT, path);
9224 if (status != GOT_STATUS_NO_CHANGE &&
9225 status != GOT_STATUS_ADD &&
9226 status != GOT_STATUS_MODIFY &&
9227 status != GOT_STATUS_MODE_CHANGE)
9228 return got_error_path(path, GOT_ERR_FILE_STATUS);
9229 if (staged_status == GOT_STATUS_DELETE)
9230 return got_error_path(path, GOT_ERR_FILE_STATUS);
9234 static const struct got_error *
9235 patch_can_add(const char *path, unsigned char status)
9237 if (status != GOT_STATUS_NONEXISTENT)
9238 return got_error_path(path, GOT_ERR_FILE_STATUS);
9242 static const struct got_error *
9243 patch_can_edit(const char *path, unsigned char status,
9244 unsigned char staged_status)
9246 if (status == GOT_STATUS_NONEXISTENT)
9247 return got_error_set_errno(ENOENT, path);
9248 if (status != GOT_STATUS_NO_CHANGE &&
9249 status != GOT_STATUS_ADD &&
9250 status != GOT_STATUS_MODIFY)
9251 return got_error_path(path, GOT_ERR_FILE_STATUS);
9252 if (staged_status == GOT_STATUS_DELETE)
9253 return got_error_path(path, GOT_ERR_FILE_STATUS);
9257 const struct got_error *
9258 got_worktree_patch_prepare(struct got_fileindex **fileindex,
9259 char **fileindex_path, struct got_worktree *worktree)
9261 return open_fileindex(fileindex, fileindex_path, worktree);
9264 const struct got_error *
9265 got_worktree_patch_check_path(const char *old, const char *new,
9266 char **oldpath, char **newpath, struct got_worktree *worktree,
9267 struct got_repository *repo, struct got_fileindex *fileindex)
9269 const struct got_error *err = NULL;
9270 int file_renamed = 0;
9271 unsigned char status_old, staged_status_old;
9272 unsigned char status_new, staged_status_new;
9277 err = patch_check_path(old != NULL ? old : new, oldpath,
9278 &status_old, &staged_status_old, fileindex, worktree, repo);
9282 err = patch_check_path(new != NULL ? new : old, newpath,
9283 &status_new, &staged_status_new, fileindex, worktree, repo);
9287 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9290 if (old != NULL && new == NULL)
9291 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9292 else if (file_renamed) {
9293 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9295 err = patch_can_add(*newpath, status_new);
9296 } else if (old == NULL)
9297 err = patch_can_add(*newpath, status_new);
9299 err = patch_can_edit(*newpath, status_new, staged_status_new);
9311 const struct got_error *
9312 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9313 struct got_worktree *worktree, struct got_fileindex *fileindex,
9314 got_worktree_checkout_cb progress_cb, void *progress_arg)
9316 struct schedule_addition_args saa;
9318 memset(&saa, 0, sizeof(saa));
9319 saa.worktree = worktree;
9320 saa.fileindex = fileindex;
9321 saa.progress_cb = progress_cb;
9322 saa.progress_arg = progress_arg;
9325 return worktree_status(worktree, path, fileindex, repo,
9326 schedule_addition, &saa, NULL, NULL, 1, 0);
9329 const struct got_error *
9330 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9331 struct got_worktree *worktree, struct got_fileindex *fileindex,
9332 got_worktree_delete_cb progress_cb, void *progress_arg)
9334 struct schedule_deletion_args sda;
9336 memset(&sda, 0, sizeof(sda));
9337 sda.worktree = worktree;
9338 sda.fileindex = fileindex;
9339 sda.progress_cb = progress_cb;
9340 sda.progress_arg = progress_arg;
9342 sda.delete_local_mods = 0;
9343 sda.keep_on_disk = 0;
9344 sda.ignore_missing_paths = 0;
9345 sda.status_codes = NULL;
9347 return worktree_status(worktree, path, fileindex, repo,
9348 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9351 const struct got_error *
9352 got_worktree_patch_complete(struct got_fileindex *fileindex,
9353 const char *fileindex_path)
9355 const struct got_error *err = NULL;
9357 err = sync_fileindex(fileindex, fileindex_path);
9358 got_fileindex_free(fileindex);