2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/queue.h>
38 #include "got_error.h"
39 #include "got_repository.h"
40 #include "got_reference.h"
41 #include "got_object.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
45 #include "got_opentemp.h"
48 #include "got_lib_worktree.h"
49 #include "got_lib_sha1.h"
50 #include "got_lib_fileindex.h"
51 #include "got_lib_inflate.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_object_idset.h"
57 #include "got_lib_diff.h"
58 #include "got_lib_gotconfig.h"
61 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
64 #define GOT_MERGE_LABEL_MERGED "merged change"
65 #define GOT_MERGE_LABEL_BASE "3-way merge base"
67 static mode_t apply_umask(mode_t);
69 static const struct got_error *
70 create_meta_file(const char *path_got, const char *name, const char *content)
72 const struct got_error *err = NULL;
75 if (asprintf(&path, "%s/%s", path_got, name) == -1)
76 return got_error_from_errno("asprintf");
78 err = got_path_create_file(path, content);
83 static const struct got_error *
84 update_meta_file(const char *path_got, const char *name, const char *content)
86 const struct got_error *err = NULL;
91 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
92 err = got_error_from_errno("asprintf");
97 err = got_opentemp_named(&tmppath, &tmpfile, path, "");
102 int len = fprintf(tmpfile, "%s\n", content);
103 if (len != strlen(content) + 1) {
104 err = got_error_from_errno2("fprintf", tmppath);
109 if (rename(tmppath, path) != 0) {
110 err = got_error_from_errno3("rename", tmppath, path);
116 if (fclose(tmpfile) == EOF && err == NULL)
117 err = got_error_from_errno2("fclose", tmppath);
122 static const struct got_error *
123 write_head_ref(const char *path_got, struct got_reference *head_ref)
125 const struct got_error *err = NULL;
128 if (got_ref_is_symbolic(head_ref)) {
129 refstr = got_ref_to_str(head_ref);
131 return got_error_from_errno("got_ref_to_str");
133 refstr = strdup(got_ref_get_name(head_ref));
135 return got_error_from_errno("strdup");
137 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
142 const struct got_error *
143 got_worktree_init(const char *path, struct got_reference *head_ref,
144 const char *prefix, struct got_repository *repo)
146 const struct got_error *err = NULL;
147 struct got_object_id *commit_id = NULL;
149 uint32_t uuid_status;
151 char *path_got = NULL;
152 char *formatstr = NULL;
153 char *absprefix = NULL;
154 char *basestr = NULL;
155 char *uuidstr = NULL;
157 if (strcmp(path, got_repo_get_path(repo)) == 0) {
158 err = got_error(GOT_ERR_WORKTREE_REPO);
162 err = got_ref_resolve(&commit_id, repo, head_ref);
165 err = got_object_get_type(&obj_type, repo, commit_id);
168 if (obj_type != GOT_OBJ_TYPE_COMMIT)
169 return got_error(GOT_ERR_OBJ_TYPE);
171 if (!got_path_is_absolute(prefix)) {
172 if (asprintf(&absprefix, "/%s", prefix) == -1)
173 return got_error_from_errno("asprintf");
176 /* Create top-level directory (may already exist). */
177 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
178 err = got_error_from_errno2("mkdir", path);
182 /* Create .got directory (may already exist). */
183 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
184 err = got_error_from_errno("asprintf");
187 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
188 err = got_error_from_errno2("mkdir", path_got);
192 /* Create an empty lock file. */
193 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
197 /* Create an empty file index. */
198 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
202 /* Write the HEAD reference. */
203 err = write_head_ref(path_got, head_ref);
207 /* Record our base commit. */
208 err = got_object_id_str(&basestr, commit_id);
211 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
215 /* Store path to repository. */
216 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
217 got_repo_get_path(repo));
221 /* Store in-repository path prefix. */
222 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
223 absprefix ? absprefix : prefix);
228 uuid_create(&uuid, &uuid_status);
229 if (uuid_status != uuid_s_ok) {
230 err = got_error_uuid(uuid_status, "uuid_create");
233 uuid_to_string(&uuid, &uuidstr, &uuid_status);
234 if (uuid_status != uuid_s_ok) {
235 err = got_error_uuid(uuid_status, "uuid_to_string");
238 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
242 /* Stamp work tree with format file. */
243 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
244 err = got_error_from_errno("asprintf");
247 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
261 const struct got_error *
262 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
263 const char *path_prefix)
265 char *absprefix = NULL;
267 if (!got_path_is_absolute(path_prefix)) {
268 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
269 return got_error_from_errno("asprintf");
271 *match = (strcmp(absprefix ? absprefix : path_prefix,
272 worktree->path_prefix) == 0);
278 got_worktree_get_head_ref_name(struct got_worktree *worktree)
280 return worktree->head_ref_name;
283 const struct got_error *
284 got_worktree_set_head_ref(struct got_worktree *worktree,
285 struct got_reference *head_ref)
287 const struct got_error *err = NULL;
288 char *path_got = NULL, *head_ref_name = NULL;
290 if (asprintf(&path_got, "%s/%s", worktree->root_path,
291 GOT_WORKTREE_GOT_DIR) == -1) {
292 err = got_error_from_errno("asprintf");
297 head_ref_name = strdup(got_ref_get_name(head_ref));
298 if (head_ref_name == NULL) {
299 err = got_error_from_errno("strdup");
303 err = write_head_ref(path_got, head_ref);
307 free(worktree->head_ref_name);
308 worktree->head_ref_name = head_ref_name;
316 struct got_object_id *
317 got_worktree_get_base_commit_id(struct got_worktree *worktree)
319 return worktree->base_commit_id;
322 const struct got_error *
323 got_worktree_set_base_commit_id(struct got_worktree *worktree,
324 struct got_repository *repo, struct got_object_id *commit_id)
326 const struct got_error *err;
327 struct got_object *obj = NULL;
329 char *path_got = NULL;
331 if (asprintf(&path_got, "%s/%s", worktree->root_path,
332 GOT_WORKTREE_GOT_DIR) == -1) {
333 err = got_error_from_errno("asprintf");
338 err = got_object_open(&obj, repo, commit_id);
342 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
343 err = got_error(GOT_ERR_OBJ_TYPE);
347 /* Record our base commit. */
348 err = got_object_id_str(&id_str, commit_id);
351 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
355 free(worktree->base_commit_id);
356 worktree->base_commit_id = got_object_id_dup(commit_id);
357 if (worktree->base_commit_id == NULL) {
358 err = got_error_from_errno("got_object_id_dup");
363 got_object_close(obj);
369 const struct got_gotconfig *
370 got_worktree_get_gotconfig(struct got_worktree *worktree)
372 return worktree->gotconfig;
375 static const struct got_error *
376 lock_worktree(struct got_worktree *worktree, int operation)
378 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
379 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
380 : got_error_from_errno2("flock",
381 got_worktree_get_root_path(worktree)));
385 static const struct got_error *
386 add_dir_on_disk(struct got_worktree *worktree, const char *path)
388 const struct got_error *err = NULL;
391 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
392 return got_error_from_errno("asprintf");
394 err = got_path_mkdir(abspath);
395 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
398 if (lstat(abspath, &sb) == -1) {
399 err = got_error_from_errno2("lstat", abspath);
400 } else if (!S_ISDIR(sb.st_mode)) {
401 /* TODO directory is obstructed; do something */
402 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
409 static const struct got_error *
410 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
412 const struct got_error *err = NULL;
415 size_t flen1 = 0, flen2 = 0;
420 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
421 if (flen1 == 0 && ferror(f1)) {
422 err = got_error_from_errno("fread");
425 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
426 if (flen2 == 0 && ferror(f2)) {
427 err = got_error_from_errno("fread");
434 } else if (flen2 == 0) {
438 } else if (flen1 == flen2) {
439 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
452 static const struct got_error *
453 check_files_equal(int *same, FILE *f1, FILE *f2)
460 if (fstat(fileno(f1), &sb) != 0)
461 return got_error_from_errno("fstat");
464 if (fstat(fileno(f2), &sb) != 0)
465 return got_error_from_errno("fstat");
468 if (size1 != size2) {
473 if (fseek(f1, 0L, SEEK_SET) == -1)
474 return got_ferror(f1, GOT_ERR_IO);
475 if (fseek(f2, 0L, SEEK_SET) == -1)
476 return got_ferror(f2, GOT_ERR_IO);
478 return check_file_contents_equal(same, f1, f2);
481 static const struct got_error *
482 copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
490 if (fseek(f, 0L, SEEK_SET) == -1)
491 return got_ferror(f, GOT_ERR_IO);
494 flen = fread(fbuf, 1, sizeof(fbuf), f);
497 return got_error_from_errno("fread");
501 outlen = write(outfd, fbuf, flen);
503 return got_error_from_errno("write");
505 return got_error(GOT_ERR_IO);
512 static const struct got_error *
513 merge_binary_file(int *overlapcnt, int merged_fd,
514 FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
515 const char *label_deriv, const char *label_orig, const char *label_deriv2,
516 const char *ondisk_path)
518 const struct got_error *err = NULL;
519 int same_content, changed_deriv, changed_deriv2;
520 int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
521 off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
522 char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
523 char *base_path_orig = NULL, *base_path_deriv = NULL;
524 char *base_path_deriv2 = NULL;
528 err = check_files_equal(&same_content, f_deriv, f_deriv2);
533 return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
535 err = check_files_equal(&same_content, f_deriv, f_orig);
538 changed_deriv = !same_content;
539 err = check_files_equal(&same_content, f_deriv2, f_orig);
542 changed_deriv2 = !same_content;
544 if (changed_deriv && changed_deriv2) {
546 if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
547 err = got_error_from_errno("asprintf");
550 if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
551 err = got_error_from_errno("asprintf");
554 if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
555 err = got_error_from_errno("asprintf");
558 err = got_opentemp_named_fd(&path_orig, &fd_orig,
562 err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
563 base_path_deriv, "");
566 err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
567 base_path_deriv2, "");
570 err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
573 err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
576 err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
579 if (dprintf(merged_fd, "Binary files differ and cannot be "
580 "merged automatically:\n") < 0) {
581 err = got_error_from_errno("dprintf");
584 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
585 GOT_DIFF_CONFLICT_MARKER_BEGIN,
586 label_deriv ? " " : "",
587 label_deriv ? label_deriv : "",
589 err = got_error_from_errno("dprintf");
593 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
594 GOT_DIFF_CONFLICT_MARKER_ORIG,
595 label_orig ? " " : "",
596 label_orig ? label_orig : "",
598 err = got_error_from_errno("dprintf");
602 if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
603 GOT_DIFF_CONFLICT_MARKER_SEP,
605 GOT_DIFF_CONFLICT_MARKER_END,
606 label_deriv2 ? " " : "",
607 label_deriv2 ? label_deriv2 : "") < 0) {
608 err = got_error_from_errno("dprintf");
611 } else if (changed_deriv)
612 err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
613 else if (changed_deriv2)
614 err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
616 if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
618 err = got_error_from_errno2("unlink", path_orig);
619 if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
620 err = got_error_from_errno2("close", path_orig);
621 if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
622 err = got_error_from_errno2("close", path_deriv);
623 if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
624 err = got_error_from_errno2("close", path_deriv2);
628 free(base_path_orig);
629 free(base_path_deriv);
630 free(base_path_deriv2);
635 * Perform a 3-way merge where the file f_orig acts as the common
636 * ancestor, the file f_deriv acts as the first derived version,
637 * and the file f_deriv2 acts as the second derived version.
638 * The merge result will be written to a new file at ondisk_path; any
639 * existing file at this path will be replaced.
641 static const struct got_error *
642 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
643 FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
644 const char *path, uint16_t st_mode,
645 const char *label_orig, const char *label_deriv, const char *label_deriv2,
646 enum got_diff_algorithm diff_algo, struct got_repository *repo,
647 got_worktree_checkout_cb progress_cb, void *progress_arg)
649 const struct got_error *err = NULL;
651 FILE *f_merged = NULL;
652 char *merged_path = NULL, *base_path = NULL;
656 *local_changes_subsumed = 0;
658 err = got_path_dirname(&parent, ondisk_path);
662 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
663 err = got_error_from_errno("asprintf");
667 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path, "");
671 err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
672 f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
674 if (err->code != GOT_ERR_FILE_BINARY)
676 err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
677 f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
683 err = (*progress_cb)(progress_arg,
684 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
688 if (fsync(merged_fd) != 0) {
689 err = got_error_from_errno("fsync");
693 f_merged = fdopen(merged_fd, "r");
694 if (f_merged == NULL) {
695 err = got_error_from_errno("fdopen");
700 /* Check if a clean merge has subsumed all local changes. */
701 if (overlapcnt == 0) {
702 err = check_files_equal(local_changes_subsumed, f_deriv,
708 if (fchmod(fileno(f_merged), apply_umask(st_mode)) != 0) {
709 err = got_error_from_errno2("fchmod", merged_path);
713 if (rename(merged_path, ondisk_path) != 0) {
714 err = got_error_from_errno3("rename", merged_path,
723 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
724 err = got_error_from_errno("close");
725 if (f_merged && fclose(f_merged) == EOF && err == NULL)
726 err = got_error_from_errno("fclose");
733 static const struct got_error *
734 update_symlink(const char *ondisk_path, const char *target_path,
737 /* This is not atomic but matches what 'ln -sf' does. */
738 if (unlink(ondisk_path) == -1)
739 return got_error_from_errno2("unlink", ondisk_path);
740 if (symlink(target_path, ondisk_path) == -1)
741 return got_error_from_errno3("symlink", target_path,
747 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
748 * in the work tree with a file that contains conflict markers and the
749 * conflicting target paths of the original version, a "derived version"
750 * of a symlink from an incoming change, and a local version of the symlink.
752 * The original versions's target path can be NULL if it is not available,
753 * such as if both derived versions added a new symlink at the same path.
755 * The incoming derived symlink target is NULL in case the incoming change
756 * has deleted this symlink.
758 static const struct got_error *
759 install_symlink_conflict(const char *deriv_target,
760 struct got_object_id *deriv_base_commit_id, const char *orig_target,
761 const char *label_orig, const char *local_target, const char *ondisk_path)
763 const struct got_error *err;
764 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
767 err = got_object_id_str(&id_str, deriv_base_commit_id);
769 return got_error_from_errno("asprintf");
771 if (asprintf(&label_deriv, "%s: commit %s",
772 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
773 err = got_error_from_errno("asprintf");
777 err = got_opentemp_named(&path, &f, "got-symlink-conflict", "");
781 if (fchmod(fileno(f), apply_umask(GOT_DEFAULT_FILE_MODE)) == -1) {
782 err = got_error_from_errno2("fchmod", path);
786 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
787 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
788 deriv_target ? deriv_target : "(symlink was deleted)",
789 orig_target ? label_orig : "",
790 orig_target ? "\n" : "",
791 orig_target ? orig_target : "",
792 orig_target ? "\n" : "",
793 GOT_DIFF_CONFLICT_MARKER_SEP,
794 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
795 err = got_error_from_errno2("fprintf", path);
799 if (unlink(ondisk_path) == -1) {
800 err = got_error_from_errno2("unlink", ondisk_path);
803 if (rename(path, ondisk_path) == -1) {
804 err = got_error_from_errno3("rename", path, ondisk_path);
808 if (f != NULL && fclose(f) == EOF && err == NULL)
809 err = got_error_from_errno2("fclose", path);
816 /* forward declaration */
817 static const struct got_error *
818 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
819 const char *, const char *, uint16_t, const char *,
820 struct got_blob_object *, struct got_object_id *,
821 struct got_repository *, got_worktree_checkout_cb, void *);
824 * Merge a symlink into the work tree, where blob_orig acts as the common
825 * ancestor, deriv_target is the link target of the first derived version,
826 * and the symlink on disk acts as the second derived version.
827 * Assume that contents of both blobs represent symlinks.
829 static const struct got_error *
830 merge_symlink(struct got_worktree *worktree,
831 struct got_blob_object *blob_orig, const char *ondisk_path,
832 const char *path, const char *label_orig, const char *deriv_target,
833 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
834 got_worktree_checkout_cb progress_cb, void *progress_arg)
836 const struct got_error *err = NULL;
837 char *ancestor_target = NULL;
839 ssize_t ondisk_len, deriv_len;
840 char ondisk_target[PATH_MAX];
841 int have_local_change = 0;
842 int have_incoming_change = 0;
844 if (lstat(ondisk_path, &sb) == -1)
845 return got_error_from_errno2("lstat", ondisk_path);
847 ondisk_len = readlink(ondisk_path, ondisk_target,
848 sizeof(ondisk_target));
849 if (ondisk_len == -1) {
850 err = got_error_from_errno2("readlink",
854 ondisk_target[ondisk_len] = '\0';
857 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
862 if (ancestor_target == NULL ||
863 (ondisk_len != strlen(ancestor_target) ||
864 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
865 have_local_change = 1;
867 deriv_len = strlen(deriv_target);
868 if (ancestor_target == NULL ||
869 (deriv_len != strlen(ancestor_target) ||
870 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
871 have_incoming_change = 1;
873 if (!have_local_change && !have_incoming_change) {
874 if (ancestor_target) {
875 /* Both sides made the same change. */
876 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
878 } else if (deriv_len == ondisk_len &&
879 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
880 /* Both sides added the same symlink. */
881 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
884 /* Both sides added symlinks which don't match. */
885 err = install_symlink_conflict(deriv_target,
886 deriv_base_commit_id, ancestor_target,
887 label_orig, ondisk_target, ondisk_path);
890 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
893 } else if (!have_local_change && have_incoming_change) {
894 /* Apply the incoming change. */
895 err = update_symlink(ondisk_path, deriv_target,
896 strlen(deriv_target));
899 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
900 } else if (have_local_change && have_incoming_change) {
901 if (deriv_len == ondisk_len &&
902 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
903 /* Both sides made the same change. */
904 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
907 err = install_symlink_conflict(deriv_target,
908 deriv_base_commit_id, ancestor_target, label_orig,
909 ondisk_target, ondisk_path);
912 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
918 free(ancestor_target);
922 static const struct got_error *
923 dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
925 const struct got_error *err = NULL;
926 char target_path[PATH_MAX];
935 return got_error_from_errno("got_opentemp");
936 target_len = readlink(ondisk_path, target_path, sizeof(target_path));
937 if (target_len == -1) {
938 err = got_error_from_errno2("readlink", ondisk_path);
941 n = fwrite(target_path, 1, target_len, f);
942 if (n != target_len) {
943 err = got_ferror(f, GOT_ERR_IO);
946 if (fflush(f) == EOF) {
947 err = got_error_from_errno("fflush");
950 if (fseek(f, 0L, SEEK_SET) == -1) {
951 err = got_ferror(f, GOT_ERR_IO);
963 * Perform a 3-way merge where blob_orig acts as the common ancestor,
964 * blob_deriv acts as the first derived version, and the file on disk
965 * acts as the second derived version.
967 static const struct got_error *
968 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
969 struct got_blob_object *blob_orig, const char *ondisk_path,
970 const char *path, uint16_t st_mode, const char *label_orig,
971 struct got_blob_object *blob_deriv,
972 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
973 got_worktree_checkout_cb progress_cb, void *progress_arg)
975 const struct got_error *err = NULL;
976 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
977 char *blob_orig_path = NULL;
978 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
979 char *label_deriv = NULL, *parent = NULL;
981 *local_changes_subsumed = 0;
983 err = got_path_dirname(&parent, ondisk_path);
988 if (asprintf(&base_path, "%s/got-merge-blob-orig",
990 err = got_error_from_errno("asprintf");
995 err = got_opentemp_named(&blob_orig_path, &f_orig,
999 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1006 * No common ancestor exists. This is an "add vs add" conflict
1007 * and we simply use an empty ancestor file to make both files
1008 * appear in the merged result in their entirety.
1010 f_orig = got_opentemp();
1011 if (f_orig == NULL) {
1012 err = got_error_from_errno("got_opentemp");
1017 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1018 err = got_error_from_errno("asprintf");
1023 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path, "");
1026 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1031 err = got_object_id_str(&id_str, deriv_base_commit_id);
1034 if (asprintf(&label_deriv, "%s: commit %s",
1035 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1036 err = got_error_from_errno("asprintf");
1041 * In order the run a 3-way merge with a symlink we copy the symlink's
1042 * target path into a temporary file and use that file with diff3.
1044 if (S_ISLNK(st_mode)) {
1045 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1050 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1052 err = got_error_from_errno2("open", ondisk_path);
1055 f_deriv2 = fdopen(fd, "r");
1056 if (f_deriv2 == NULL) {
1057 err = got_error_from_errno2("fdopen", ondisk_path);
1063 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1064 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1065 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1067 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1068 err = got_error_from_errno("fclose");
1069 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1070 err = got_error_from_errno("fclose");
1071 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1072 err = got_error_from_errno("fclose");
1074 if (blob_orig_path) {
1075 unlink(blob_orig_path);
1076 free(blob_orig_path);
1078 if (blob_deriv_path) {
1079 unlink(blob_deriv_path);
1080 free(blob_deriv_path);
1088 static const struct got_error *
1089 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1090 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1091 int wt_fd, const char *path, struct got_object_id *blob_id)
1093 const struct got_error *err = NULL;
1094 struct got_fileindex_entry *new_ie;
1098 err = got_fileindex_entry_alloc(&new_ie, path);
1102 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1103 blob_id->sha1, base_commit_id->sha1, 1);
1107 err = got_fileindex_entry_add(fileindex, new_ie);
1110 got_fileindex_entry_free(new_ie);
1117 get_ondisk_perms(int executable, mode_t st_mode)
1119 mode_t xbits = S_IXUSR;
1122 /* Map read bits to execute bits. */
1123 if (st_mode & S_IRGRP)
1125 if (st_mode & S_IROTH)
1127 return st_mode | xbits;
1134 apply_umask(mode_t mode)
1143 /* forward declaration */
1144 static const struct got_error *
1145 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1146 const char *path, mode_t te_mode, mode_t st_mode,
1147 struct got_blob_object *blob, int restoring_missing_file,
1148 int reverting_versioned_file, int installing_bad_symlink,
1149 int path_is_unversioned, struct got_repository *repo,
1150 got_worktree_checkout_cb progress_cb, void *progress_arg);
1153 * This function assumes that the provided symlink target points at a
1154 * safe location in the work tree!
1156 static const struct got_error *
1157 replace_existing_symlink(int *did_something, const char *ondisk_path,
1158 const char *target_path, size_t target_len)
1160 const struct got_error *err = NULL;
1162 char etarget[PATH_MAX];
1168 * "Bad" symlinks (those pointing outside the work tree or into the
1169 * .got directory) are installed in the work tree as a regular file
1170 * which contains the bad symlink target path.
1171 * The new symlink target has already been checked for safety by our
1172 * caller. If we can successfully open a regular file then we simply
1173 * replace this file with a symlink below.
1175 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1177 if (!got_err_open_nofollow_on_symlink())
1178 return got_error_from_errno2("open", ondisk_path);
1180 /* We are updating an existing on-disk symlink. */
1181 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1183 return got_error_from_errno2("readlink", ondisk_path);
1185 if (elen == target_len &&
1186 memcmp(etarget, target_path, target_len) == 0)
1187 return NULL; /* nothing to do */
1191 err = update_symlink(ondisk_path, target_path, target_len);
1192 if (fd != -1 && close(fd) == -1 && err == NULL)
1193 err = got_error_from_errno2("close", ondisk_path);
1197 static const struct got_error *
1198 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1199 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1201 const struct got_error *err = NULL;
1202 char canonpath[PATH_MAX];
1203 char *path_got = NULL;
1205 *is_bad_symlink = 0;
1207 if (target_len >= sizeof(canonpath)) {
1208 *is_bad_symlink = 1;
1213 * We do not use realpath(3) to resolve the symlink's target
1214 * path because we don't want to resolve symlinks recursively.
1215 * Instead we make the path absolute and then canonicalize it.
1216 * Relative symlink target lookup should begin at the directory
1217 * in which the blob object is being installed.
1219 if (!got_path_is_absolute(target_path)) {
1220 char *abspath, *parent;
1221 err = got_path_dirname(&parent, ondisk_path);
1224 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1226 return got_error_from_errno("asprintf");
1229 if (strlen(abspath) >= sizeof(canonpath)) {
1230 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1234 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1239 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1244 /* Only allow symlinks pointing at paths within the work tree. */
1245 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1246 *is_bad_symlink = 1;
1250 /* Do not allow symlinks pointing into the .got directory. */
1251 if (asprintf(&path_got, "%s/%s", wtroot_path,
1252 GOT_WORKTREE_GOT_DIR) == -1)
1253 return got_error_from_errno("asprintf");
1254 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1255 *is_bad_symlink = 1;
1261 static const struct got_error *
1262 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1263 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1264 int restoring_missing_file, int reverting_versioned_file,
1265 int path_is_unversioned, int allow_bad_symlinks,
1266 struct got_repository *repo,
1267 got_worktree_checkout_cb progress_cb, void *progress_arg)
1269 const struct got_error *err = NULL;
1270 char target_path[PATH_MAX];
1271 size_t len, target_len = 0;
1272 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1273 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1275 *is_bad_symlink = 0;
1278 * Blob object content specifies the target path of the link.
1279 * If a symbolic link cannot be installed we instead create
1280 * a regular file which contains the link target path stored
1281 * in the blob object.
1284 err = got_object_blob_read_block(&len, blob);
1288 if (len + target_len >= sizeof(target_path)) {
1289 /* Path too long; install as a regular file. */
1290 *is_bad_symlink = 1;
1291 got_object_blob_rewind(blob);
1292 return install_blob(worktree, ondisk_path, path,
1293 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1294 restoring_missing_file, reverting_versioned_file,
1295 1, path_is_unversioned, repo, progress_cb,
1299 /* Skip blob object header first time around. */
1300 memcpy(target_path + target_len, buf + hdrlen,
1302 target_len += len - hdrlen;
1306 target_path[target_len] = '\0';
1308 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1309 ondisk_path, worktree->root_path);
1313 if (*is_bad_symlink && !allow_bad_symlinks) {
1314 /* install as a regular file */
1315 got_object_blob_rewind(blob);
1316 err = install_blob(worktree, ondisk_path, path,
1317 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1318 restoring_missing_file, reverting_versioned_file, 1,
1319 path_is_unversioned, repo, progress_cb, progress_arg);
1323 if (symlink(target_path, ondisk_path) == -1) {
1324 if (errno == EEXIST) {
1325 int symlink_replaced;
1326 if (path_is_unversioned) {
1327 err = (*progress_cb)(progress_arg,
1328 GOT_STATUS_UNVERSIONED, path);
1331 err = replace_existing_symlink(&symlink_replaced,
1332 ondisk_path, target_path, target_len);
1336 if (symlink_replaced) {
1337 err = (*progress_cb)(progress_arg,
1338 reverting_versioned_file ?
1340 GOT_STATUS_UPDATE, path);
1342 err = (*progress_cb)(progress_arg,
1343 GOT_STATUS_EXISTS, path);
1346 return err; /* Nothing else to do. */
1349 if (errno == ENOENT) {
1351 err = got_path_dirname(&parent, ondisk_path);
1354 err = add_dir_on_disk(worktree, parent);
1359 * Retry, and fall through to error handling
1360 * below if this second attempt fails.
1362 if (symlink(target_path, ondisk_path) != -1) {
1363 err = NULL; /* success */
1368 /* Handle errors from first or second creation attempt. */
1369 if (errno == ENAMETOOLONG) {
1370 /* bad target path; install as a regular file */
1371 *is_bad_symlink = 1;
1372 got_object_blob_rewind(blob);
1373 err = install_blob(worktree, ondisk_path, path,
1374 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1375 restoring_missing_file, reverting_versioned_file, 1,
1376 path_is_unversioned, repo,
1377 progress_cb, progress_arg);
1378 } else if (errno == ENOTDIR) {
1379 err = got_error_path(ondisk_path,
1380 GOT_ERR_FILE_OBSTRUCTED);
1382 err = got_error_from_errno3("symlink",
1383 target_path, ondisk_path);
1385 } else if (progress_cb)
1386 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1387 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1391 static const struct got_error *
1392 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1393 const char *path, mode_t te_mode, mode_t st_mode,
1394 struct got_blob_object *blob, int restoring_missing_file,
1395 int reverting_versioned_file, int installing_bad_symlink,
1396 int path_is_unversioned, struct got_repository *repo,
1397 got_worktree_checkout_cb progress_cb, void *progress_arg)
1399 const struct got_error *err = NULL;
1403 char *tmppath = NULL;
1406 mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1407 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1410 if (errno == ENOENT) {
1412 err = got_path_dirname(&parent, path);
1415 err = add_dir_on_disk(worktree, parent);
1419 fd = open(ondisk_path,
1420 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1423 return got_error_from_errno2("open",
1425 } else if (errno == EEXIST) {
1426 if (path_is_unversioned) {
1427 err = (*progress_cb)(progress_arg,
1428 GOT_STATUS_UNVERSIONED, path);
1431 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1432 !S_ISREG(st_mode) && !installing_bad_symlink) {
1433 /* TODO file is obstructed; do something */
1434 err = got_error_path(ondisk_path,
1435 GOT_ERR_FILE_OBSTRUCTED);
1438 err = got_opentemp_named_fd(&tmppath, &fd,
1444 if (fchmod(fd, apply_umask(mode)) == -1) {
1445 err = got_error_from_errno2("fchmod",
1451 return got_error_from_errno2("open", ondisk_path);
1455 if (restoring_missing_file)
1456 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1458 else if (reverting_versioned_file)
1459 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1462 err = (*progress_cb)(progress_arg,
1463 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1468 hdrlen = got_object_blob_get_hdrlen(blob);
1470 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1471 err = got_object_blob_read_block(&len, blob);
1475 /* Skip blob object header first time around. */
1476 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1478 err = got_error_from_errno("write");
1480 } else if (outlen != len - hdrlen) {
1481 err = got_error(GOT_ERR_IO);
1488 if (fsync(fd) != 0) {
1489 err = got_error_from_errno("fsync");
1494 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1495 err = got_error_from_errno2("unlink", ondisk_path);
1498 if (rename(tmppath, ondisk_path) != 0) {
1499 err = got_error_from_errno3("rename", tmppath,
1508 if (fd != -1 && close(fd) == -1 && err == NULL)
1509 err = got_error_from_errno("close");
1510 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1511 err = got_error_from_errno2("unlink", tmppath);
1517 * Upgrade STATUS_MODIFY to STATUS_CONFLICT if a
1518 * conflict marker is found in newly added lines only.
1520 static const struct got_error *
1521 get_modified_file_content_status(unsigned char *status,
1522 struct got_blob_object *blob, const char *path, struct stat *sb,
1525 const struct got_error *err, *free_err;
1526 const char *markers[3] = {
1527 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1528 GOT_DIFF_CONFLICT_MARKER_SEP,
1529 GOT_DIFF_CONFLICT_MARKER_END
1532 struct got_diffreg_result *diffreg_result = NULL;
1533 struct diff_result *r;
1534 int nchunks_parsed, n, i = 0, ln = 0;
1536 size_t linesize = 0;
1539 if (*status != GOT_STATUS_MODIFY)
1542 f1 = got_opentemp();
1544 return got_error_from_errno("got_opentemp");
1547 got_object_blob_rewind(blob);
1548 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
1553 err = got_diff_files(&diffreg_result, f1, 1, NULL, ondisk_file,
1554 1, NULL, 0, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
1558 r = diffreg_result->result;
1560 for (n = 0; n < r->chunks.len; n += nchunks_parsed) {
1561 struct diff_chunk *c;
1562 struct diff_chunk_context cc = {};
1567 * We can optimise a little by advancing straight
1568 * to the next chunk if this one has no added lines.
1570 c = diff_chunk_get(r, n);
1571 clc = diff_chunk_get_left_count(c);
1572 crc = diff_chunk_get_right_count(c);
1574 if (!crc || crc == clc) {
1576 continue; /* removed or unchanged lines */
1579 pos = diff_chunk_get_right_start_pos(c);
1580 if (fseek(ondisk_file, pos, SEEK_SET) == -1) {
1581 err = got_ferror(ondisk_file, GOT_ERR_IO);
1585 diff_chunk_context_load_change(&cc, &nchunks_parsed, r, n, 0);
1586 ln = cc.right.start;
1588 while (ln < cc.right.end) {
1589 linelen = getline(&line, &linesize, ondisk_file);
1590 if (linelen == -1) {
1591 if (feof(ondisk_file))
1593 err = got_ferror(ondisk_file, GOT_ERR_IO);
1597 if (line && strncmp(line, markers[i],
1598 strlen(markers[i])) == 0) {
1599 if (strcmp(markers[i],
1600 GOT_DIFF_CONFLICT_MARKER_END) == 0) {
1601 *status = GOT_STATUS_CONFLICT;
1612 if (f1 != NULL && fclose(f1) == EOF && err == NULL)
1613 err = got_error_from_errno("fclose");
1614 free_err = got_diffreg_result_free(diffreg_result);
1622 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1624 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1625 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1629 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1631 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1632 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1633 ie->mtime_sec == sb->st_mtim.tv_sec &&
1634 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1635 ie->size == (sb->st_size & 0xffffffff) &&
1636 !xbit_differs(ie, sb->st_mode));
1639 static unsigned char
1640 get_staged_status(struct got_fileindex_entry *ie)
1642 switch (got_fileindex_entry_stage_get(ie)) {
1643 case GOT_FILEIDX_STAGE_ADD:
1644 return GOT_STATUS_ADD;
1645 case GOT_FILEIDX_STAGE_DELETE:
1646 return GOT_STATUS_DELETE;
1647 case GOT_FILEIDX_STAGE_MODIFY:
1648 return GOT_STATUS_MODIFY;
1650 return GOT_STATUS_NO_CHANGE;
1654 static const struct got_error *
1655 get_symlink_modification_status(unsigned char *status,
1656 struct got_fileindex_entry *ie, const char *abspath,
1657 int dirfd, const char *de_name, struct got_blob_object *blob)
1659 const struct got_error *err = NULL;
1660 char target_path[PATH_MAX];
1661 char etarget[PATH_MAX];
1663 size_t len, target_len = 0;
1664 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1665 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1667 *status = GOT_STATUS_NO_CHANGE;
1669 /* Blob object content specifies the target path of the link. */
1671 err = got_object_blob_read_block(&len, blob);
1674 if (len + target_len >= sizeof(target_path)) {
1676 * Should not happen. The blob contents were OK
1677 * when this symlink was installed.
1679 return got_error(GOT_ERR_NO_SPACE);
1682 /* Skip blob object header first time around. */
1683 memcpy(target_path + target_len, buf + hdrlen,
1685 target_len += len - hdrlen;
1689 target_path[target_len] = '\0';
1692 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1694 return got_error_from_errno2("readlinkat", abspath);
1696 elen = readlink(abspath, etarget, sizeof(etarget));
1698 return got_error_from_errno2("readlink", abspath);
1701 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1702 *status = GOT_STATUS_MODIFY;
1707 static const struct got_error *
1708 get_file_status(unsigned char *status, struct stat *sb,
1709 struct got_fileindex_entry *ie, const char *abspath,
1710 int dirfd, const char *de_name, struct got_repository *repo)
1712 const struct got_error *err = NULL;
1713 struct got_object_id id;
1715 int fd = -1, fd1 = -1;
1718 struct got_blob_object *blob = NULL;
1720 unsigned char staged_status;
1722 staged_status = get_staged_status(ie);
1723 *status = GOT_STATUS_NO_CHANGE;
1724 memset(sb, 0, sizeof(*sb));
1727 * Whenever the caller provides a directory descriptor and a
1728 * directory entry name for the file, use them! This prevents
1729 * race conditions if filesystem paths change beneath our feet.
1732 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1733 if (errno == ENOENT) {
1734 if (got_fileindex_entry_has_file_on_disk(ie))
1735 *status = GOT_STATUS_MISSING;
1737 *status = GOT_STATUS_DELETE;
1740 err = got_error_from_errno2("fstatat", abspath);
1744 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1745 if (fd == -1 && errno != ENOENT &&
1746 !got_err_open_nofollow_on_symlink())
1747 return got_error_from_errno2("open", abspath);
1748 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1749 if (lstat(abspath, sb) == -1)
1750 return got_error_from_errno2("lstat", abspath);
1751 } else if (fd == -1 || fstat(fd, sb) == -1) {
1752 if (errno == ENOENT) {
1753 if (got_fileindex_entry_has_file_on_disk(ie))
1754 *status = GOT_STATUS_MISSING;
1756 *status = GOT_STATUS_DELETE;
1759 err = got_error_from_errno2("fstat", abspath);
1764 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1765 *status = GOT_STATUS_OBSTRUCTED;
1769 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1770 *status = GOT_STATUS_DELETE;
1772 } else if (!got_fileindex_entry_has_blob(ie) &&
1773 staged_status != GOT_STATUS_ADD) {
1774 *status = GOT_STATUS_ADD;
1778 if (!stat_info_differs(ie, sb))
1781 if (S_ISLNK(sb->st_mode) &&
1782 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1783 *status = GOT_STATUS_MODIFY;
1787 if (staged_status == GOT_STATUS_MODIFY ||
1788 staged_status == GOT_STATUS_ADD)
1789 got_fileindex_entry_get_staged_blob_id(&id, ie);
1791 got_fileindex_entry_get_blob_id(&id, ie);
1793 fd1 = got_opentempfd();
1795 err = got_error_from_errno("got_opentempfd");
1798 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1802 if (S_ISLNK(sb->st_mode)) {
1803 err = get_symlink_modification_status(status, ie,
1804 abspath, dirfd, de_name, blob);
1809 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1811 err = got_error_from_errno2("openat", abspath);
1816 f = fdopen(fd, "r");
1818 err = got_error_from_errno2("fdopen", abspath);
1822 hdrlen = got_object_blob_get_hdrlen(blob);
1824 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1825 err = got_object_blob_read_block(&blen, blob);
1828 /* Skip length of blob object header first time around. */
1829 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1830 if (flen == 0 && ferror(f)) {
1831 err = got_error_from_errno("fread");
1834 if (blen - hdrlen == 0) {
1836 *status = GOT_STATUS_MODIFY;
1838 } else if (flen == 0) {
1839 if (blen - hdrlen != 0)
1840 *status = GOT_STATUS_MODIFY;
1842 } else if (blen - hdrlen == flen) {
1843 /* Skip blob object header first time around. */
1844 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1845 *status = GOT_STATUS_MODIFY;
1849 *status = GOT_STATUS_MODIFY;
1855 if (*status == GOT_STATUS_MODIFY) {
1857 err = get_modified_file_content_status(status, blob, ie->path,
1859 } else if (xbit_differs(ie, sb->st_mode))
1860 *status = GOT_STATUS_MODE_CHANGE;
1862 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1863 err = got_error_from_errno("close");
1865 got_object_blob_close(blob);
1866 if (f != NULL && fclose(f) == EOF && err == NULL)
1867 err = got_error_from_errno2("fclose", abspath);
1868 if (fd != -1 && close(fd) == -1 && err == NULL)
1869 err = got_error_from_errno2("close", abspath);
1874 * Update timestamps in the file index if a file is unmodified and
1875 * we had to run a full content comparison to find out.
1877 static const struct got_error *
1878 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1879 struct got_fileindex_entry *ie, struct stat *sb)
1881 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1882 return got_fileindex_entry_update(ie, wt_fd, path,
1883 ie->blob_sha1, ie->commit_sha1, 1);
1888 static const struct got_error *
1889 update_blob(struct got_worktree *worktree,
1890 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1891 struct got_tree_entry *te, const char *path,
1892 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1895 const struct got_error *err = NULL;
1896 struct got_blob_object *blob = NULL;
1897 char *ondisk_path = NULL;
1898 unsigned char status = GOT_STATUS_NO_CHANGE;
1900 int fd1 = -1, fd2 = -1;
1902 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1903 return got_error_from_errno("asprintf");
1906 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1907 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1910 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1914 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1915 sb.st_mode = got_fileindex_perms_to_st(ie);
1917 if (stat(ondisk_path, &sb) == -1) {
1918 if (errno != ENOENT) {
1919 err = got_error_from_errno2("stat",
1923 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1924 status = GOT_STATUS_UNVERSIONED;
1926 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1927 status = GOT_STATUS_UNVERSIONED;
1929 status = GOT_STATUS_OBSTRUCTED;
1933 if (status == GOT_STATUS_OBSTRUCTED) {
1935 got_fileindex_entry_mark_skipped(ie);
1936 err = (*progress_cb)(progress_arg, status, path);
1939 if (status == GOT_STATUS_CONFLICT) {
1941 got_fileindex_entry_mark_skipped(ie);
1942 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1947 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1948 (S_ISLNK(te->mode) ||
1949 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1951 * This is a regular file or an installed bad symlink.
1952 * If the file index indicates that this file is already
1953 * up-to-date with respect to the repository we can skip
1954 * updating contents of this file.
1956 if (got_fileindex_entry_has_commit(ie) &&
1957 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1958 SHA1_DIGEST_LENGTH) == 0) {
1960 err = sync_timestamps(worktree->root_fd,
1961 path, status, ie, &sb);
1964 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1968 if (got_fileindex_entry_has_blob(ie) &&
1969 memcmp(ie->blob_sha1, te->id.sha1,
1970 SHA1_DIGEST_LENGTH) == 0) {
1971 /* Different commit but the same blob. */
1972 err = sync_timestamps(worktree->root_fd,
1973 path, status, ie, &sb);
1976 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1982 fd1 = got_opentempfd();
1984 err = got_error_from_errno("got_opentempfd");
1987 err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
1991 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1992 int update_timestamps;
1993 struct got_blob_object *blob2 = NULL;
1994 char *label_orig = NULL;
1995 if (got_fileindex_entry_has_blob(ie)) {
1996 fd2 = got_opentempfd();
1998 err = got_error_from_errno("got_opentempfd");
2001 struct got_object_id id2;
2002 got_fileindex_entry_get_blob_id(&id2, ie);
2003 err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
2008 if (got_fileindex_entry_has_commit(ie)) {
2009 char id_str[SHA1_DIGEST_STRING_LENGTH];
2010 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2011 sizeof(id_str)) == NULL) {
2012 err = got_error_path(id_str,
2013 GOT_ERR_BAD_OBJ_ID_STR);
2016 if (asprintf(&label_orig, "%s: commit %s",
2017 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2018 err = got_error_from_errno("asprintf");
2022 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2024 err = got_object_blob_read_to_str(&link_target, blob);
2027 err = merge_symlink(worktree, blob2, ondisk_path, path,
2028 label_orig, link_target, worktree->base_commit_id,
2029 repo, progress_cb, progress_arg);
2032 err = merge_blob(&update_timestamps, worktree, blob2,
2033 ondisk_path, path, sb.st_mode, label_orig, blob,
2034 worktree->base_commit_id, repo,
2035 progress_cb, progress_arg);
2038 if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2039 err = got_error_from_errno("close");
2043 got_object_blob_close(blob2);
2047 * Do not update timestamps of files with local changes.
2048 * Otherwise, a future status walk would treat them as
2049 * unmodified files again.
2051 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2052 blob->id.sha1, worktree->base_commit_id->sha1,
2054 } else if (status == GOT_STATUS_MODE_CHANGE) {
2055 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2056 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2057 } else if (status == GOT_STATUS_DELETE) {
2058 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2061 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2062 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2066 int is_bad_symlink = 0;
2067 if (S_ISLNK(te->mode)) {
2068 err = install_symlink(&is_bad_symlink, worktree,
2069 ondisk_path, path, blob,
2070 status == GOT_STATUS_MISSING, 0,
2071 status == GOT_STATUS_UNVERSIONED, 0,
2072 repo, progress_cb, progress_arg);
2074 err = install_blob(worktree, ondisk_path, path,
2075 te->mode, sb.st_mode, blob,
2076 status == GOT_STATUS_MISSING, 0, 0,
2077 status == GOT_STATUS_UNVERSIONED, repo,
2078 progress_cb, progress_arg);
2084 err = got_fileindex_entry_update(ie,
2085 worktree->root_fd, path, blob->id.sha1,
2086 worktree->base_commit_id->sha1, 1);
2088 err = create_fileindex_entry(&ie, fileindex,
2089 worktree->base_commit_id, worktree->root_fd, path,
2095 if (is_bad_symlink) {
2096 got_fileindex_entry_filetype_set(ie,
2097 GOT_FILEIDX_MODE_BAD_SYMLINK);
2101 if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2102 err = got_error_from_errno("close");
2105 got_object_blob_close(blob);
2111 static const struct got_error *
2112 remove_ondisk_file(const char *root_path, const char *path)
2114 const struct got_error *err = NULL;
2115 char *ondisk_path = NULL, *parent = NULL;
2117 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2118 return got_error_from_errno("asprintf");
2120 if (unlink(ondisk_path) == -1) {
2121 if (errno != ENOENT)
2122 err = got_error_from_errno2("unlink", ondisk_path);
2124 size_t root_len = strlen(root_path);
2125 err = got_path_dirname(&parent, ondisk_path);
2128 while (got_path_cmp(parent, root_path,
2129 strlen(parent), root_len) != 0) {
2131 ondisk_path = parent;
2133 if (rmdir(ondisk_path) == -1) {
2134 if (errno != ENOTEMPTY)
2135 err = got_error_from_errno2("rmdir",
2139 err = got_path_dirname(&parent, ondisk_path);
2150 static const struct got_error *
2151 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2152 struct got_fileindex_entry *ie, struct got_repository *repo,
2153 got_worktree_checkout_cb progress_cb, void *progress_arg)
2155 const struct got_error *err = NULL;
2156 unsigned char status;
2160 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2161 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2163 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2165 return got_error_from_errno("asprintf");
2167 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2171 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2172 char ondisk_target[PATH_MAX];
2173 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2174 sizeof(ondisk_target));
2175 if (ondisk_len == -1) {
2176 err = got_error_from_errno2("readlink", ondisk_path);
2179 ondisk_target[ondisk_len] = '\0';
2180 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2181 NULL, NULL, /* XXX pass common ancestor info? */
2182 ondisk_target, ondisk_path);
2185 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2190 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2191 status == GOT_STATUS_ADD) {
2192 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2196 * Preserve the working file and change the deleted blob's
2197 * entry into a schedule-add entry.
2199 err = got_fileindex_entry_update(ie, worktree->root_fd,
2200 ie->path, NULL, NULL, 0);
2202 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2205 if (status == GOT_STATUS_NO_CHANGE) {
2206 err = remove_ondisk_file(worktree->root_path, ie->path);
2210 got_fileindex_entry_remove(fileindex, ie);
2217 struct diff_cb_arg {
2218 struct got_fileindex *fileindex;
2219 struct got_worktree *worktree;
2220 struct got_repository *repo;
2221 got_worktree_checkout_cb progress_cb;
2223 got_cancel_cb cancel_cb;
2227 static const struct got_error *
2228 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2229 struct got_tree_entry *te, const char *parent_path)
2231 struct diff_cb_arg *a = arg;
2233 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2234 return got_error(GOT_ERR_CANCELLED);
2236 return update_blob(a->worktree, a->fileindex, ie, te,
2237 ie->path, a->repo, a->progress_cb, a->progress_arg);
2240 static const struct got_error *
2241 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2243 struct diff_cb_arg *a = arg;
2245 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2246 return got_error(GOT_ERR_CANCELLED);
2248 return delete_blob(a->worktree, a->fileindex, ie,
2249 a->repo, a->progress_cb, a->progress_arg);
2252 static const struct got_error *
2253 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2255 struct diff_cb_arg *a = arg;
2256 const struct got_error *err;
2259 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2260 return got_error(GOT_ERR_CANCELLED);
2262 if (got_object_tree_entry_is_submodule(te))
2265 if (asprintf(&path, "%s%s%s", parent_path,
2266 parent_path[0] ? "/" : "", te->name)
2268 return got_error_from_errno("asprintf");
2270 if (S_ISDIR(te->mode))
2271 err = add_dir_on_disk(a->worktree, path);
2273 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2274 a->repo, a->progress_cb, a->progress_arg);
2280 const struct got_error *
2281 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2283 uint32_t uuid_status;
2285 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2286 if (uuid_status != uuid_s_ok) {
2288 return got_error_uuid(uuid_status, "uuid_to_string");
2294 static const struct got_error *
2295 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2297 const struct got_error *err = NULL;
2298 char *uuidstr = NULL;
2302 err = got_worktree_get_uuid(&uuidstr, worktree);
2306 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2307 err = got_error_from_errno("asprintf");
2314 const struct got_error *
2315 got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2318 return get_ref_name(refname, worktree, prefix);
2321 const struct got_error *
2322 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2324 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2327 static const struct got_error *
2328 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2330 return get_ref_name(refname, worktree,
2331 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2334 static const struct got_error *
2335 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2337 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2340 static const struct got_error *
2341 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2343 return get_ref_name(refname, worktree,
2344 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2347 static const struct got_error *
2348 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2350 return get_ref_name(refname, worktree,
2351 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2354 static const struct got_error *
2355 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2357 return get_ref_name(refname, worktree,
2358 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2361 static const struct got_error *
2362 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2364 return get_ref_name(refname, worktree,
2365 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2368 static const struct got_error *
2369 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2371 return get_ref_name(refname, worktree,
2372 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2375 static const struct got_error *
2376 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2378 return get_ref_name(refname, worktree,
2379 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2382 const struct got_error *
2383 got_worktree_get_histedit_script_path(char **path,
2384 struct got_worktree *worktree)
2386 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2387 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2389 return got_error_from_errno("asprintf");
2394 static const struct got_error *
2395 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2397 return get_ref_name(refname, worktree,
2398 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2401 static const struct got_error *
2402 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2404 return get_ref_name(refname, worktree,
2405 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2409 * Prevent Git's garbage collector from deleting our base commit by
2410 * setting a reference to our base commit's ID.
2412 static const struct got_error *
2413 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2415 const struct got_error *err = NULL;
2416 struct got_reference *ref = NULL;
2419 err = got_worktree_get_base_ref_name(&refname, worktree);
2423 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2427 err = got_ref_write(ref, repo);
2435 static const struct got_error *
2436 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2438 const struct got_error *err = NULL;
2440 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2441 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2442 err = got_error_from_errno("asprintf");
2443 *fileindex_path = NULL;
2449 static const struct got_error *
2450 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2451 struct got_worktree *worktree)
2453 const struct got_error *err = NULL;
2456 *fileindex_path = NULL;
2457 *fileindex = got_fileindex_alloc();
2458 if (*fileindex == NULL)
2459 return got_error_from_errno("got_fileindex_alloc");
2461 err = get_fileindex_path(fileindex_path, worktree);
2465 index = fopen(*fileindex_path, "rbe");
2466 if (index == NULL) {
2467 if (errno != ENOENT)
2468 err = got_error_from_errno2("fopen", *fileindex_path);
2470 err = got_fileindex_read(*fileindex, index);
2471 if (fclose(index) == EOF && err == NULL)
2472 err = got_error_from_errno("fclose");
2476 free(*fileindex_path);
2477 *fileindex_path = NULL;
2478 got_fileindex_free(*fileindex);
2484 struct bump_base_commit_id_arg {
2485 struct got_object_id *base_commit_id;
2488 const char *entry_name;
2489 got_worktree_checkout_cb progress_cb;
2493 static const struct got_error *
2494 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2496 const struct got_error *err;
2497 struct bump_base_commit_id_arg *a = arg;
2499 if (a->entry_name) {
2500 if (strcmp(ie->path, a->path) != 0)
2502 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2505 if (got_fileindex_entry_was_skipped(ie))
2508 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2509 SHA1_DIGEST_LENGTH) == 0)
2512 if (a->progress_cb) {
2513 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2518 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2522 /* Bump base commit ID of all files within an updated part of the work tree. */
2523 static const struct got_error *
2524 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2525 struct got_fileindex *fileindex,
2526 got_worktree_checkout_cb progress_cb, void *progress_arg)
2528 struct bump_base_commit_id_arg bbc_arg;
2530 bbc_arg.base_commit_id = worktree->base_commit_id;
2531 bbc_arg.entry_name = NULL;
2533 bbc_arg.path_len = 0;
2534 bbc_arg.progress_cb = progress_cb;
2535 bbc_arg.progress_arg = progress_arg;
2537 return got_fileindex_for_each_entry_safe(fileindex,
2538 bump_base_commit_id, &bbc_arg);
2541 static const struct got_error *
2542 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2544 const struct got_error *err = NULL;
2545 char *new_fileindex_path = NULL;
2546 FILE *new_index = NULL;
2547 struct timespec timeout;
2549 err = got_opentemp_named(&new_fileindex_path, &new_index,
2550 fileindex_path, "");
2554 err = got_fileindex_write(fileindex, new_index);
2558 if (rename(new_fileindex_path, fileindex_path) != 0) {
2559 err = got_error_from_errno3("rename", new_fileindex_path,
2561 unlink(new_fileindex_path);
2565 * Sleep for a short amount of time to ensure that files modified after
2566 * this program exits have a different time stamp from the one which
2567 * was recorded in the file index.
2570 timeout.tv_nsec = 1;
2571 nanosleep(&timeout, NULL);
2575 free(new_fileindex_path);
2579 static const struct got_error *
2580 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2581 struct got_object_id **tree_id, const char *wt_relpath,
2582 struct got_commit_object *base_commit, struct got_worktree *worktree,
2583 struct got_repository *repo)
2585 const struct got_error *err = NULL;
2586 struct got_object_id *id = NULL;
2587 char *in_repo_path = NULL;
2588 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2590 *entry_type = GOT_OBJ_TYPE_ANY;
2591 *tree_relpath = NULL;
2594 if (wt_relpath[0] == '\0') {
2595 /* Check out all files within the work tree. */
2596 *entry_type = GOT_OBJ_TYPE_TREE;
2597 *tree_relpath = strdup("");
2598 if (*tree_relpath == NULL) {
2599 err = got_error_from_errno("strdup");
2602 err = got_object_id_by_path(tree_id, repo, base_commit,
2603 worktree->path_prefix);
2609 /* Check out a subset of files in the work tree. */
2611 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2612 is_root_wt ? "" : "/", wt_relpath) == -1) {
2613 err = got_error_from_errno("asprintf");
2617 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2622 in_repo_path = NULL;
2624 err = got_object_get_type(entry_type, repo, id);
2628 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2629 /* Check out a single file. */
2630 if (strchr(wt_relpath, '/') == NULL) {
2631 /* Check out a single file in work tree's root dir. */
2632 in_repo_path = strdup(worktree->path_prefix);
2633 if (in_repo_path == NULL) {
2634 err = got_error_from_errno("strdup");
2637 *tree_relpath = strdup("");
2638 if (*tree_relpath == NULL) {
2639 err = got_error_from_errno("strdup");
2643 /* Check out a single file in a subdirectory. */
2644 err = got_path_dirname(tree_relpath, wt_relpath);
2647 if (asprintf(&in_repo_path, "%s%s%s",
2648 worktree->path_prefix, is_root_wt ? "" : "/",
2649 *tree_relpath) == -1) {
2650 err = got_error_from_errno("asprintf");
2654 err = got_object_id_by_path(tree_id, repo,
2655 base_commit, in_repo_path);
2657 /* Check out all files within a subdirectory. */
2658 *tree_id = got_object_id_dup(id);
2659 if (*tree_id == NULL) {
2660 err = got_error_from_errno("got_object_id_dup");
2663 *tree_relpath = strdup(wt_relpath);
2664 if (*tree_relpath == NULL) {
2665 err = got_error_from_errno("strdup");
2673 *entry_type = GOT_OBJ_TYPE_ANY;
2674 free(*tree_relpath);
2675 *tree_relpath = NULL;
2682 static const struct got_error *
2683 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2684 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2685 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2686 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2688 const struct got_error *err = NULL;
2689 struct got_commit_object *commit = NULL;
2690 struct got_tree_object *tree = NULL;
2691 struct got_fileindex_diff_tree_cb diff_cb;
2692 struct diff_cb_arg arg;
2694 err = ref_base_commit(worktree, repo);
2696 if (!(err->code == GOT_ERR_ERRNO &&
2697 (errno == EACCES || errno == EROFS)))
2699 err = (*progress_cb)(progress_arg,
2700 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2705 err = got_object_open_as_commit(&commit, repo,
2706 worktree->base_commit_id);
2710 err = got_object_open_as_tree(&tree, repo, tree_id);
2715 got_object_tree_find_entry(tree, entry_name) == NULL) {
2716 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2720 diff_cb.diff_old_new = diff_old_new;
2721 diff_cb.diff_old = diff_old;
2722 diff_cb.diff_new = diff_new;
2723 arg.fileindex = fileindex;
2724 arg.worktree = worktree;
2726 arg.progress_cb = progress_cb;
2727 arg.progress_arg = progress_arg;
2728 arg.cancel_cb = cancel_cb;
2729 arg.cancel_arg = cancel_arg;
2730 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2731 entry_name, repo, &diff_cb, &arg);
2734 got_object_tree_close(tree);
2736 got_object_commit_close(commit);
2740 const struct got_error *
2741 got_worktree_checkout_files(struct got_worktree *worktree,
2742 struct got_pathlist_head *paths, struct got_repository *repo,
2743 got_worktree_checkout_cb progress_cb, void *progress_arg,
2744 got_cancel_cb cancel_cb, void *cancel_arg)
2746 const struct got_error *err = NULL, *sync_err, *unlockerr;
2747 struct got_commit_object *commit = NULL;
2748 struct got_tree_object *tree = NULL;
2749 struct got_fileindex *fileindex = NULL;
2750 char *fileindex_path = NULL;
2751 struct got_pathlist_entry *pe;
2752 struct tree_path_data {
2753 STAILQ_ENTRY(tree_path_data) entry;
2754 struct got_object_id *tree_id;
2759 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2761 STAILQ_INIT(&tree_paths);
2763 err = lock_worktree(worktree, LOCK_EX);
2767 err = got_object_open_as_commit(&commit, repo,
2768 worktree->base_commit_id);
2772 /* Map all specified paths to in-repository trees. */
2773 TAILQ_FOREACH(pe, paths, entry) {
2774 tpd = malloc(sizeof(*tpd));
2776 err = got_error_from_errno("malloc");
2780 err = find_tree_entry_for_checkout(&tpd->entry_type,
2781 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2788 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2789 err = got_path_basename(&tpd->entry_name, pe->path);
2797 tpd->entry_name = NULL;
2799 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2803 * Read the file index.
2804 * Checking out files is supposed to be an idempotent operation.
2805 * If the on-disk file index is incomplete we will try to complete it.
2807 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2811 tpd = STAILQ_FIRST(&tree_paths);
2812 TAILQ_FOREACH(pe, paths, entry) {
2813 struct bump_base_commit_id_arg bbc_arg;
2815 err = checkout_files(worktree, fileindex, tpd->relpath,
2816 tpd->tree_id, tpd->entry_name, repo,
2817 progress_cb, progress_arg, cancel_cb, cancel_arg);
2821 bbc_arg.base_commit_id = worktree->base_commit_id;
2822 bbc_arg.entry_name = tpd->entry_name;
2823 bbc_arg.path = pe->path;
2824 bbc_arg.path_len = pe->path_len;
2825 bbc_arg.progress_cb = progress_cb;
2826 bbc_arg.progress_arg = progress_arg;
2827 err = got_fileindex_for_each_entry_safe(fileindex,
2828 bump_base_commit_id, &bbc_arg);
2832 tpd = STAILQ_NEXT(tpd, entry);
2834 sync_err = sync_fileindex(fileindex, fileindex_path);
2835 if (sync_err && err == NULL)
2838 free(fileindex_path);
2840 got_object_tree_close(tree);
2842 got_object_commit_close(commit);
2844 got_fileindex_free(fileindex);
2845 while (!STAILQ_EMPTY(&tree_paths)) {
2846 tpd = STAILQ_FIRST(&tree_paths);
2847 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2852 unlockerr = lock_worktree(worktree, LOCK_SH);
2853 if (unlockerr && err == NULL)
2858 struct merge_file_cb_arg {
2859 struct got_worktree *worktree;
2860 struct got_fileindex *fileindex;
2861 got_worktree_checkout_cb progress_cb;
2863 got_cancel_cb cancel_cb;
2865 const char *label_orig;
2866 struct got_object_id *commit_id2;
2867 int allow_bad_symlinks;
2870 static const struct got_error *
2871 merge_file_cb(void *arg, struct got_blob_object *blob1,
2872 struct got_blob_object *blob2, FILE *f1, FILE *f2,
2873 struct got_object_id *id1, struct got_object_id *id2,
2874 const char *path1, const char *path2,
2875 mode_t mode1, mode_t mode2, struct got_repository *repo)
2877 static const struct got_error *err = NULL;
2878 struct merge_file_cb_arg *a = arg;
2879 struct got_fileindex_entry *ie;
2880 char *ondisk_path = NULL;
2882 unsigned char status;
2883 int local_changes_subsumed;
2884 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2885 char *id_str = NULL, *label_deriv2 = NULL;
2887 if (blob1 && blob2) {
2888 ie = got_fileindex_entry_get(a->fileindex, path2,
2891 return (*a->progress_cb)(a->progress_arg,
2892 GOT_STATUS_MISSING, path2);
2894 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2896 return got_error_from_errno("asprintf");
2898 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2903 if (status == GOT_STATUS_DELETE) {
2904 err = (*a->progress_cb)(a->progress_arg,
2905 GOT_STATUS_MERGE, path2);
2908 if (status != GOT_STATUS_NO_CHANGE &&
2909 status != GOT_STATUS_MODIFY &&
2910 status != GOT_STATUS_CONFLICT &&
2911 status != GOT_STATUS_ADD) {
2912 err = (*a->progress_cb)(a->progress_arg, status, path2);
2916 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2918 err = got_object_blob_read_to_str(&link_target2, blob2);
2921 err = merge_symlink(a->worktree, blob1, ondisk_path,
2922 path2, a->label_orig, link_target2, a->commit_id2,
2923 repo, a->progress_cb, a->progress_arg);
2928 f_orig = got_opentemp();
2929 if (f_orig == NULL) {
2930 err = got_error_from_errno("got_opentemp");
2933 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2938 f_deriv2 = got_opentemp();
2939 if (f_deriv2 == NULL)
2941 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2946 fd = open(ondisk_path,
2947 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2949 err = got_error_from_errno2("open",
2953 f_deriv = fdopen(fd, "r");
2954 if (f_deriv == NULL) {
2955 err = got_error_from_errno2("fdopen",
2960 err = got_object_id_str(&id_str, a->commit_id2);
2963 if (asprintf(&label_deriv2, "%s: commit %s",
2964 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2965 err = got_error_from_errno("asprintf");
2968 err = merge_file(&local_changes_subsumed, a->worktree,
2969 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2970 mode2, a->label_orig, NULL, label_deriv2,
2971 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2972 a->progress_cb, a->progress_arg);
2975 ie = got_fileindex_entry_get(a->fileindex, path1,
2978 return (*a->progress_cb)(a->progress_arg,
2979 GOT_STATUS_MISSING, path1);
2981 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2983 return got_error_from_errno("asprintf");
2985 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2991 case GOT_STATUS_NO_CHANGE:
2992 err = (*a->progress_cb)(a->progress_arg,
2993 GOT_STATUS_DELETE, path1);
2996 err = remove_ondisk_file(a->worktree->root_path, path1);
3000 got_fileindex_entry_mark_deleted_from_disk(ie);
3002 case GOT_STATUS_DELETE:
3003 case GOT_STATUS_MISSING:
3004 err = (*a->progress_cb)(a->progress_arg,
3005 GOT_STATUS_DELETE, path1);
3009 got_fileindex_entry_mark_deleted_from_disk(ie);
3011 case GOT_STATUS_ADD: {
3012 struct got_object_id *id;
3016 * Delete the added file only if its content already
3017 * exists in the repository.
3019 err = got_object_blob_file_create(&id, &blob1_f,
3020 &blob1_size, path1);
3023 if (got_object_id_cmp(id, id1) == 0) {
3024 err = (*a->progress_cb)(a->progress_arg,
3025 GOT_STATUS_DELETE, path1);
3028 err = remove_ondisk_file(a->worktree->root_path,
3033 got_fileindex_entry_remove(a->fileindex,
3036 err = (*a->progress_cb)(a->progress_arg,
3037 GOT_STATUS_CANNOT_DELETE, path1);
3039 if (fclose(blob1_f) == EOF && err == NULL)
3040 err = got_error_from_errno("fclose");
3046 case GOT_STATUS_MODIFY:
3047 case GOT_STATUS_CONFLICT:
3048 err = (*a->progress_cb)(a->progress_arg,
3049 GOT_STATUS_CANNOT_DELETE, path1);
3053 case GOT_STATUS_OBSTRUCTED:
3054 err = (*a->progress_cb)(a->progress_arg, status, path1);
3062 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3064 return got_error_from_errno("asprintf");
3065 ie = got_fileindex_entry_get(a->fileindex, path2,
3068 err = get_file_status(&status, &sb, ie, ondisk_path,
3072 if (status != GOT_STATUS_NO_CHANGE &&
3073 status != GOT_STATUS_MODIFY &&
3074 status != GOT_STATUS_CONFLICT &&
3075 status != GOT_STATUS_ADD) {
3076 err = (*a->progress_cb)(a->progress_arg,
3080 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3082 err = got_object_blob_read_to_str(&link_target2,
3086 err = merge_symlink(a->worktree, NULL,
3087 ondisk_path, path2, a->label_orig,
3088 link_target2, a->commit_id2, repo,
3089 a->progress_cb, a->progress_arg);
3091 } else if (S_ISREG(sb.st_mode)) {
3092 err = merge_blob(&local_changes_subsumed,
3093 a->worktree, NULL, ondisk_path, path2,
3094 sb.st_mode, a->label_orig, blob2,
3095 a->commit_id2, repo, a->progress_cb,
3098 err = got_error_path(ondisk_path,
3099 GOT_ERR_FILE_OBSTRUCTED);
3103 if (status == GOT_STATUS_DELETE) {
3104 err = got_fileindex_entry_update(ie,
3105 a->worktree->root_fd, path2, blob2->id.sha1,
3106 a->worktree->base_commit_id->sha1, 0);
3111 int is_bad_symlink = 0;
3112 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3113 if (S_ISLNK(mode2)) {
3114 err = install_symlink(&is_bad_symlink,
3115 a->worktree, ondisk_path, path2, blob2, 0,
3116 0, 1, a->allow_bad_symlinks, repo,
3117 a->progress_cb, a->progress_arg);
3119 err = install_blob(a->worktree, ondisk_path, path2,
3120 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3121 a->progress_cb, a->progress_arg);
3125 err = got_fileindex_entry_alloc(&ie, path2);
3128 err = got_fileindex_entry_update(ie,
3129 a->worktree->root_fd, path2, NULL, NULL, 1);
3131 got_fileindex_entry_free(ie);
3134 err = got_fileindex_entry_add(a->fileindex, ie);
3136 got_fileindex_entry_free(ie);
3139 if (is_bad_symlink) {
3140 got_fileindex_entry_filetype_set(ie,
3141 GOT_FILEIDX_MODE_BAD_SYMLINK);
3146 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3147 err = got_error_from_errno("fclose");
3148 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3149 err = got_error_from_errno("fclose");
3150 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3151 err = got_error_from_errno("fclose");
3158 static const struct got_error *
3159 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3161 struct got_worktree *worktree = arg;
3163 /* Reject merges into a work tree with mixed base commits. */
3164 if (got_fileindex_entry_has_commit(ie) &&
3165 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3166 SHA1_DIGEST_LENGTH) != 0)
3167 return got_error(GOT_ERR_MIXED_COMMITS);
3172 struct check_merge_conflicts_arg {
3173 struct got_worktree *worktree;
3174 struct got_fileindex *fileindex;
3175 struct got_repository *repo;
3178 static const struct got_error *
3179 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3180 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3181 struct got_object_id *id1, struct got_object_id *id2,
3182 const char *path1, const char *path2,
3183 mode_t mode1, mode_t mode2, struct got_repository *repo)
3185 const struct got_error *err = NULL;
3186 struct check_merge_conflicts_arg *a = arg;
3187 unsigned char status;
3189 struct got_fileindex_entry *ie;
3190 const char *path = path2 ? path2 : path1;
3191 struct got_object_id *id = id2 ? id2 : id1;
3197 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3201 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3203 return got_error_from_errno("asprintf");
3205 /* Reject merges into a work tree with conflicted files. */
3206 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3210 if (status == GOT_STATUS_CONFLICT)
3211 return got_error(GOT_ERR_CONFLICTS);
3216 static const struct got_error *
3217 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3218 const char *fileindex_path, struct got_object_id *commit_id1,
3219 struct got_object_id *commit_id2, struct got_repository *repo,
3220 got_worktree_checkout_cb progress_cb, void *progress_arg,
3221 got_cancel_cb cancel_cb, void *cancel_arg)
3223 const struct got_error *err = NULL, *sync_err;
3224 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3225 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3226 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3227 struct check_merge_conflicts_arg cmc_arg;
3228 struct merge_file_cb_arg arg;
3229 char *label_orig = NULL;
3230 FILE *f1 = NULL, *f2 = NULL;
3231 int fd1 = -1, fd2 = -1;
3234 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3237 err = got_object_id_by_path(&tree_id1, repo, commit1,
3238 worktree->path_prefix);
3239 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3245 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3249 err = got_object_id_str(&id_str, commit_id1);
3253 if (asprintf(&label_orig, "%s: commit %s",
3254 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3255 err = got_error_from_errno("asprintf");
3261 f1 = got_opentemp();
3263 err = got_error_from_errno("got_opentemp");
3268 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3272 err = got_object_id_by_path(&tree_id2, repo, commit2,
3273 worktree->path_prefix);
3277 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3281 f2 = got_opentemp();
3283 err = got_error_from_errno("got_opentemp");
3287 fd1 = got_opentempfd();
3289 err = got_error_from_errno("got_opentempfd");
3293 fd2 = got_opentempfd();
3295 err = got_error_from_errno("got_opentempfd");
3299 cmc_arg.worktree = worktree;
3300 cmc_arg.fileindex = fileindex;
3301 cmc_arg.repo = repo;
3302 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3303 check_merge_conflicts, &cmc_arg, 0);
3307 arg.worktree = worktree;
3308 arg.fileindex = fileindex;
3309 arg.progress_cb = progress_cb;
3310 arg.progress_arg = progress_arg;
3311 arg.cancel_cb = cancel_cb;
3312 arg.cancel_arg = cancel_arg;
3313 arg.label_orig = label_orig;
3314 arg.commit_id2 = commit_id2;
3315 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3316 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3317 merge_file_cb, &arg, 1);
3318 sync_err = sync_fileindex(fileindex, fileindex_path);
3319 if (sync_err && err == NULL)
3323 got_object_commit_close(commit1);
3325 got_object_commit_close(commit2);
3327 got_object_tree_close(tree1);
3329 got_object_tree_close(tree2);
3330 if (f1 && fclose(f1) == EOF && err == NULL)
3331 err = got_error_from_errno("fclose");
3332 if (f2 && fclose(f2) == EOF && err == NULL)
3333 err = got_error_from_errno("fclose");
3334 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3335 err = got_error_from_errno("close");
3336 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3337 err = got_error_from_errno("close");
3342 const struct got_error *
3343 got_worktree_merge_files(struct got_worktree *worktree,
3344 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3345 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3346 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3348 const struct got_error *err, *unlockerr;
3349 char *fileindex_path = NULL;
3350 struct got_fileindex *fileindex = NULL;
3352 err = lock_worktree(worktree, LOCK_EX);
3356 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3360 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3365 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3366 commit_id2, repo, progress_cb, progress_arg,
3367 cancel_cb, cancel_arg);
3370 got_fileindex_free(fileindex);
3371 free(fileindex_path);
3372 unlockerr = lock_worktree(worktree, LOCK_SH);
3373 if (unlockerr && err == NULL)
3378 struct diff_dir_cb_arg {
3379 struct got_fileindex *fileindex;
3380 struct got_worktree *worktree;
3381 const char *status_path;
3382 size_t status_path_len;
3383 struct got_repository *repo;
3384 got_worktree_status_cb status_cb;
3386 got_cancel_cb cancel_cb;
3388 /* A pathlist containing per-directory pathlists of ignore patterns. */
3389 struct got_pathlist_head *ignores;
3390 int report_unchanged;
3394 static const struct got_error *
3395 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3396 int dirfd, const char *de_name,
3397 got_worktree_status_cb status_cb, void *status_arg,
3398 struct got_repository *repo, int report_unchanged)
3400 const struct got_error *err = NULL;
3401 unsigned char status = GOT_STATUS_NO_CHANGE;
3402 unsigned char staged_status;
3404 struct got_object_id blob_id, commit_id, staged_blob_id;
3405 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3406 struct got_object_id *staged_blob_idp = NULL;
3408 staged_status = get_staged_status(ie);
3409 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3413 if (status == GOT_STATUS_NO_CHANGE &&
3414 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3417 if (got_fileindex_entry_has_blob(ie))
3418 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3419 if (got_fileindex_entry_has_commit(ie))
3420 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3421 if (staged_status == GOT_STATUS_ADD ||
3422 staged_status == GOT_STATUS_MODIFY) {
3423 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3424 &staged_blob_id, ie);
3427 return (*status_cb)(status_arg, status, staged_status,
3428 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3431 static const struct got_error *
3432 status_old_new(void *arg, struct got_fileindex_entry *ie,
3433 struct dirent *de, const char *parent_path, int dirfd)
3435 const struct got_error *err = NULL;
3436 struct diff_dir_cb_arg *a = arg;
3439 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3440 return got_error(GOT_ERR_CANCELLED);
3442 if (got_path_cmp(parent_path, a->status_path,
3443 strlen(parent_path), a->status_path_len) != 0 &&
3444 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3447 if (parent_path[0]) {
3448 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3449 parent_path, de->d_name) == -1)
3450 return got_error_from_errno("asprintf");
3452 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3454 return got_error_from_errno("asprintf");
3457 err = report_file_status(ie, abspath, dirfd, de->d_name,
3458 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3463 static const struct got_error *
3464 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3466 struct diff_dir_cb_arg *a = arg;
3467 struct got_object_id blob_id, commit_id;
3468 unsigned char status;
3470 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3471 return got_error(GOT_ERR_CANCELLED);
3473 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3476 got_fileindex_entry_get_blob_id(&blob_id, ie);
3477 got_fileindex_entry_get_commit_id(&commit_id, ie);
3478 if (got_fileindex_entry_has_file_on_disk(ie))
3479 status = GOT_STATUS_MISSING;
3481 status = GOT_STATUS_DELETE;
3482 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3483 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3487 free_ignores(struct got_pathlist_head *ignores)
3489 struct got_pathlist_entry *pe;
3491 TAILQ_FOREACH(pe, ignores, entry) {
3492 struct got_pathlist_head *ignorelist = pe->data;
3494 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3496 got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3499 static const struct got_error *
3500 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3502 const struct got_error *err = NULL;
3503 struct got_pathlist_entry *pe = NULL;
3504 struct got_pathlist_head *ignorelist;
3505 char *line = NULL, *pattern, *dirpath = NULL;
3506 size_t linesize = 0;
3509 ignorelist = calloc(1, sizeof(*ignorelist));
3510 if (ignorelist == NULL)
3511 return got_error_from_errno("calloc");
3512 TAILQ_INIT(ignorelist);
3514 while ((linelen = getline(&line, &linesize, f)) != -1) {
3515 if (linelen > 0 && line[linelen - 1] == '\n')
3516 line[linelen - 1] = '\0';
3518 /* Git's ignores may contain comments. */
3522 /* Git's negated patterns are not (yet?) supported. */
3526 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3528 err = got_error_from_errno("asprintf");
3531 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3536 err = got_error_from_errno("getline");
3540 dirpath = strdup(path);
3541 if (dirpath == NULL) {
3542 err = got_error_from_errno("strdup");
3545 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3548 if (err || pe == NULL) {
3550 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3556 match_path(const char *pattern, size_t pattern_len, const char *path,
3562 * Trailing slashes signify directories.
3563 * Append a * to make such patterns conform to fnmatch rules.
3565 if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
3566 if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
3567 return FNM_NOMATCH; /* XXX */
3569 return fnmatch(buf, path, flags);
3572 return fnmatch(pattern, path, flags);
3576 match_ignores(struct got_pathlist_head *ignores, const char *path)
3578 struct got_pathlist_entry *pe;
3580 /* Handle patterns which match in all directories. */
3581 TAILQ_FOREACH(pe, ignores, entry) {
3582 struct got_pathlist_head *ignorelist = pe->data;
3583 struct got_pathlist_entry *pi;
3585 TAILQ_FOREACH(pi, ignorelist, entry) {
3588 if (pi->path_len < 3 ||
3589 strncmp(pi->path, "**/", 3) != 0)
3593 if (match_path(pi->path + 3,
3594 pi->path_len - 3, p,
3595 FNM_PATHNAME | FNM_LEADING_DIR)) {
3596 /* Retry in next directory. */
3597 while (*p && *p != '/')
3609 * The ignores pathlist contains ignore lists from children before
3610 * parents, so we can find the most specific ignorelist by walking
3611 * ignores backwards.
3613 pe = TAILQ_LAST(ignores, got_pathlist_head);
3615 if (got_path_is_child(path, pe->path, pe->path_len)) {
3616 struct got_pathlist_head *ignorelist = pe->data;
3617 struct got_pathlist_entry *pi;
3618 TAILQ_FOREACH(pi, ignorelist, entry) {
3619 int flags = FNM_LEADING_DIR;
3620 if (strstr(pi->path, "/**/") == NULL)
3621 flags |= FNM_PATHNAME;
3622 if (match_path(pi->path, pi->path_len,
3628 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3634 static const struct got_error *
3635 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3636 const char *path, int dirfd, const char *ignores_filename)
3638 const struct got_error *err = NULL;
3641 FILE *ignoresfile = NULL;
3643 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3644 path[0] ? "/" : "", ignores_filename) == -1)
3645 return got_error_from_errno("asprintf");
3648 fd = openat(dirfd, ignores_filename,
3649 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3651 if (errno != ENOENT && errno != EACCES)
3652 err = got_error_from_errno2("openat",
3655 ignoresfile = fdopen(fd, "r");
3656 if (ignoresfile == NULL)
3657 err = got_error_from_errno2("fdopen",
3661 err = read_ignores(ignores, path, ignoresfile);
3665 ignoresfile = fopen(ignorespath, "re");
3666 if (ignoresfile == NULL) {
3667 if (errno != ENOENT && errno != EACCES)
3668 err = got_error_from_errno2("fopen",
3671 err = read_ignores(ignores, path, ignoresfile);
3674 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3675 err = got_error_from_errno2("fclose", path);
3676 if (fd != -1 && close(fd) == -1 && err == NULL)
3677 err = got_error_from_errno2("close", path);
3682 static const struct got_error *
3683 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3686 const struct got_error *err = NULL;
3687 struct diff_dir_cb_arg *a = arg;
3693 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3694 return got_error(GOT_ERR_CANCELLED);
3696 if (parent_path[0]) {
3697 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3698 return got_error_from_errno("asprintf");
3703 if (de->d_type == DT_DIR) {
3704 if (!a->no_ignores && ignore != NULL &&
3705 match_ignores(a->ignores, path))
3707 } else if (!match_ignores(a->ignores, path) &&
3708 got_path_is_child(path, a->status_path, a->status_path_len))
3709 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3710 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3716 static const struct got_error *
3717 status_traverse(void *arg, const char *path, int dirfd)
3719 const struct got_error *err = NULL;
3720 struct diff_dir_cb_arg *a = arg;
3725 err = add_ignores(a->ignores, a->worktree->root_path,
3726 path, dirfd, ".cvsignore");
3730 err = add_ignores(a->ignores, a->worktree->root_path, path,
3731 dirfd, ".gitignore");
3736 static const struct got_error *
3737 report_single_file_status(const char *path, const char *ondisk_path,
3738 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3739 void *status_arg, struct got_repository *repo, int report_unchanged,
3740 struct got_pathlist_head *ignores, int no_ignores)
3742 struct got_fileindex_entry *ie;
3745 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3747 return report_file_status(ie, ondisk_path, -1, NULL,
3748 status_cb, status_arg, repo, report_unchanged);
3750 if (lstat(ondisk_path, &sb) == -1) {
3751 if (errno != ENOENT)
3752 return got_error_from_errno2("lstat", ondisk_path);
3753 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3754 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3757 if (!no_ignores && match_ignores(ignores, path))
3760 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3761 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3762 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3767 static const struct got_error *
3768 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3769 const char *root_path, const char *path)
3771 const struct got_error *err;
3772 char *parent_path, *next_parent_path = NULL;
3774 err = add_ignores(ignores, root_path, "", -1,
3779 err = add_ignores(ignores, root_path, "", -1,
3784 err = got_path_dirname(&parent_path, path);
3786 if (err->code == GOT_ERR_BAD_PATH)
3787 return NULL; /* cannot traverse parent */
3791 err = add_ignores(ignores, root_path, parent_path, -1,
3795 err = add_ignores(ignores, root_path, parent_path, -1,
3799 err = got_path_dirname(&next_parent_path, parent_path);
3801 if (err->code == GOT_ERR_BAD_PATH)
3802 err = NULL; /* traversed everything */
3805 if (got_path_is_root_dir(parent_path))
3808 parent_path = next_parent_path;
3809 next_parent_path = NULL;
3813 free(next_parent_path);
3817 static const struct got_error *
3818 worktree_status(struct got_worktree *worktree, const char *path,
3819 struct got_fileindex *fileindex, struct got_repository *repo,
3820 got_worktree_status_cb status_cb, void *status_arg,
3821 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3822 int report_unchanged)
3824 const struct got_error *err = NULL;
3826 struct got_fileindex_diff_dir_cb fdiff_cb;
3827 struct diff_dir_cb_arg arg;
3828 char *ondisk_path = NULL;
3829 struct got_pathlist_head ignores;
3830 struct got_fileindex_entry *ie;
3832 TAILQ_INIT(&ignores);
3834 if (asprintf(&ondisk_path, "%s%s%s",
3835 worktree->root_path, path[0] ? "/" : "", path) == -1)
3836 return got_error_from_errno("asprintf");
3838 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3840 err = report_single_file_status(path, ondisk_path,
3841 fileindex, status_cb, status_arg, repo,
3842 report_unchanged, &ignores, no_ignores);
3846 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3848 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3849 !got_err_open_nofollow_on_symlink())
3850 err = got_error_from_errno2("open", ondisk_path);
3853 err = add_ignores_from_parent_paths(&ignores,
3854 worktree->root_path, ondisk_path);
3858 err = report_single_file_status(path, ondisk_path,
3859 fileindex, status_cb, status_arg, repo,
3860 report_unchanged, &ignores, no_ignores);
3863 fdiff_cb.diff_old_new = status_old_new;
3864 fdiff_cb.diff_old = status_old;
3865 fdiff_cb.diff_new = status_new;
3866 fdiff_cb.diff_traverse = status_traverse;
3867 arg.fileindex = fileindex;
3868 arg.worktree = worktree;
3869 arg.status_path = path;
3870 arg.status_path_len = strlen(path);
3872 arg.status_cb = status_cb;
3873 arg.status_arg = status_arg;
3874 arg.cancel_cb = cancel_cb;
3875 arg.cancel_arg = cancel_arg;
3876 arg.report_unchanged = report_unchanged;
3877 arg.no_ignores = no_ignores;
3879 err = add_ignores_from_parent_paths(&ignores,
3880 worktree->root_path, path);
3884 arg.ignores = &ignores;
3885 err = got_fileindex_diff_dir(fileindex, fd,
3886 worktree->root_path, path, repo, &fdiff_cb, &arg);
3889 free_ignores(&ignores);
3890 if (fd != -1 && close(fd) == -1 && err == NULL)
3891 err = got_error_from_errno("close");
3896 const struct got_error *
3897 got_worktree_status(struct got_worktree *worktree,
3898 struct got_pathlist_head *paths, struct got_repository *repo,
3899 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3900 got_cancel_cb cancel_cb, void *cancel_arg)
3902 const struct got_error *err = NULL;
3903 char *fileindex_path = NULL;
3904 struct got_fileindex *fileindex = NULL;
3905 struct got_pathlist_entry *pe;
3907 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3911 TAILQ_FOREACH(pe, paths, entry) {
3912 err = worktree_status(worktree, pe->path, fileindex, repo,
3913 status_cb, status_arg, cancel_cb, cancel_arg,
3918 free(fileindex_path);
3919 got_fileindex_free(fileindex);
3923 const struct got_error *
3924 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3927 const struct got_error *err = NULL;
3928 char *resolved = NULL, *cwd = NULL, *path = NULL;
3931 char *abspath = NULL;
3932 char canonpath[PATH_MAX];
3936 cwd = getcwd(NULL, 0);
3938 return got_error_from_errno("getcwd");
3940 if (lstat(arg, &sb) == -1) {
3941 if (errno != ENOENT) {
3942 err = got_error_from_errno2("lstat", arg);
3947 if (S_ISLNK(sb.st_mode)) {
3949 * We cannot use realpath(3) with symlinks since we want to
3950 * operate on the symlink itself.
3951 * But we can make the path absolute, assuming it is relative
3952 * to the current working directory, and then canonicalize it.
3954 if (!got_path_is_absolute(arg)) {
3955 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3956 err = got_error_from_errno("asprintf");
3961 err = got_canonpath(abspath ? abspath : arg, canonpath,
3965 resolved = strdup(canonpath);
3966 if (resolved == NULL) {
3967 err = got_error_from_errno("strdup");
3971 resolved = realpath(arg, NULL);
3972 if (resolved == NULL) {
3973 if (errno != ENOENT) {
3974 err = got_error_from_errno2("realpath", arg);
3977 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3978 err = got_error_from_errno("asprintf");
3981 err = got_canonpath(abspath, canonpath,
3985 resolved = strdup(canonpath);
3986 if (resolved == NULL) {
3987 err = got_error_from_errno("strdup");
3993 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3994 strlen(got_worktree_get_root_path(worktree)))) {
3995 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3999 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
4000 err = got_path_skip_common_ancestor(&path,
4001 got_worktree_get_root_path(worktree), resolved);
4007 err = got_error_from_errno("strdup");
4012 /* XXX status walk can't deal with trailing slash! */
4014 while (len > 0 && path[len - 1] == '/') {
4015 path[len - 1] = '\0';
4029 struct schedule_addition_args {
4030 struct got_worktree *worktree;
4031 struct got_fileindex *fileindex;
4032 got_worktree_checkout_cb progress_cb;
4034 struct got_repository *repo;
4037 static const struct got_error *
4038 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4039 const char *relpath, struct got_object_id *blob_id,
4040 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4041 int dirfd, const char *de_name)
4043 struct schedule_addition_args *a = arg;
4044 const struct got_error *err = NULL;
4045 struct got_fileindex_entry *ie;
4049 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4051 return got_error_from_errno("asprintf");
4053 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4055 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4059 /* Re-adding an existing entry is a no-op. */
4060 if (status == GOT_STATUS_ADD)
4062 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4067 if (status != GOT_STATUS_UNVERSIONED) {
4068 if (status == GOT_STATUS_NONEXISTENT)
4069 err = got_error_set_errno(ENOENT, ondisk_path);
4071 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4075 err = got_fileindex_entry_alloc(&ie, relpath);
4078 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4079 relpath, NULL, NULL, 1);
4081 got_fileindex_entry_free(ie);
4084 err = got_fileindex_entry_add(a->fileindex, ie);
4086 got_fileindex_entry_free(ie);
4093 if (status == GOT_STATUS_ADD)
4095 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4098 const struct got_error *
4099 got_worktree_schedule_add(struct got_worktree *worktree,
4100 struct got_pathlist_head *paths,
4101 got_worktree_checkout_cb progress_cb, void *progress_arg,
4102 struct got_repository *repo, int no_ignores)
4104 struct got_fileindex *fileindex = NULL;
4105 char *fileindex_path = NULL;
4106 const struct got_error *err = NULL, *sync_err, *unlockerr;
4107 struct got_pathlist_entry *pe;
4108 struct schedule_addition_args saa;
4110 err = lock_worktree(worktree, LOCK_EX);
4114 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4118 saa.worktree = worktree;
4119 saa.fileindex = fileindex;
4120 saa.progress_cb = progress_cb;
4121 saa.progress_arg = progress_arg;
4124 TAILQ_FOREACH(pe, paths, entry) {
4125 err = worktree_status(worktree, pe->path, fileindex, repo,
4126 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4130 sync_err = sync_fileindex(fileindex, fileindex_path);
4131 if (sync_err && err == NULL)
4134 free(fileindex_path);
4136 got_fileindex_free(fileindex);
4137 unlockerr = lock_worktree(worktree, LOCK_SH);
4138 if (unlockerr && err == NULL)
4143 struct schedule_deletion_args {
4144 struct got_worktree *worktree;
4145 struct got_fileindex *fileindex;
4146 got_worktree_delete_cb progress_cb;
4148 struct got_repository *repo;
4149 int delete_local_mods;
4151 int ignore_missing_paths;
4152 const char *status_codes;
4155 static const struct got_error *
4156 schedule_for_deletion(void *arg, unsigned char status,
4157 unsigned char staged_status, const char *relpath,
4158 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4159 struct got_object_id *commit_id, int dirfd, const char *de_name)
4161 struct schedule_deletion_args *a = arg;
4162 const struct got_error *err = NULL;
4163 struct got_fileindex_entry *ie = NULL;
4167 if (status == GOT_STATUS_NONEXISTENT) {
4168 if (a->ignore_missing_paths)
4170 return got_error_set_errno(ENOENT, relpath);
4173 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4175 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4177 staged_status = get_staged_status(ie);
4178 if (staged_status != GOT_STATUS_NO_CHANGE) {
4179 if (staged_status == GOT_STATUS_DELETE)
4181 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4184 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4186 return got_error_from_errno("asprintf");
4188 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4193 if (a->status_codes) {
4194 size_t ncodes = strlen(a->status_codes);
4196 for (i = 0; i < ncodes ; i++) {
4197 if (status == a->status_codes[i])
4201 /* Do not delete files in non-matching status. */
4205 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4206 a->status_codes[i] != GOT_STATUS_MISSING) {
4207 static char msg[64];
4208 snprintf(msg, sizeof(msg),
4209 "invalid status code '%c'", a->status_codes[i]);
4210 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4215 if (status != GOT_STATUS_NO_CHANGE) {
4216 if (status == GOT_STATUS_DELETE)
4218 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4219 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4222 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4223 err = got_error_set_errno(ENOENT, relpath);
4226 if (status != GOT_STATUS_MODIFY &&
4227 status != GOT_STATUS_MISSING) {
4228 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4233 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4237 if (unlinkat(dirfd, de_name, 0) == -1) {
4238 err = got_error_from_errno2("unlinkat",
4242 } else if (unlink(ondisk_path) == -1) {
4243 err = got_error_from_errno2("unlink", ondisk_path);
4247 root_len = strlen(a->worktree->root_path);
4250 err = got_path_dirname(&parent, ondisk_path);
4254 ondisk_path = parent;
4255 if (rmdir(ondisk_path) == -1) {
4256 if (errno != ENOTEMPTY)
4257 err = got_error_from_errno2("rmdir",
4261 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4262 strlen(ondisk_path), root_len) != 0);
4265 got_fileindex_entry_mark_deleted_from_disk(ie);
4270 if (status == GOT_STATUS_DELETE)
4272 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4273 staged_status, relpath);
4276 const struct got_error *
4277 got_worktree_schedule_delete(struct got_worktree *worktree,
4278 struct got_pathlist_head *paths, int delete_local_mods,
4279 const char *status_codes,
4280 got_worktree_delete_cb progress_cb, void *progress_arg,
4281 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4283 struct got_fileindex *fileindex = NULL;
4284 char *fileindex_path = NULL;
4285 const struct got_error *err = NULL, *sync_err, *unlockerr;
4286 struct got_pathlist_entry *pe;
4287 struct schedule_deletion_args sda;
4289 err = lock_worktree(worktree, LOCK_EX);
4293 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4297 sda.worktree = worktree;
4298 sda.fileindex = fileindex;
4299 sda.progress_cb = progress_cb;
4300 sda.progress_arg = progress_arg;
4302 sda.delete_local_mods = delete_local_mods;
4303 sda.keep_on_disk = keep_on_disk;
4304 sda.ignore_missing_paths = ignore_missing_paths;
4305 sda.status_codes = status_codes;
4307 TAILQ_FOREACH(pe, paths, entry) {
4308 err = worktree_status(worktree, pe->path, fileindex, repo,
4309 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4313 sync_err = sync_fileindex(fileindex, fileindex_path);
4314 if (sync_err && err == NULL)
4317 free(fileindex_path);
4319 got_fileindex_free(fileindex);
4320 unlockerr = lock_worktree(worktree, LOCK_SH);
4321 if (unlockerr && err == NULL)
4326 static const struct got_error *
4327 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4329 const struct got_error *err = NULL;
4331 size_t linesize = 0, n;
4334 linelen = getline(&line, &linesize, infile);
4335 if (linelen == -1) {
4336 if (ferror(infile)) {
4337 err = got_error_from_errno("getline");
4343 n = fwrite(line, 1, linelen, outfile);
4345 err = got_ferror(outfile, GOT_ERR_IO);
4350 n = fwrite(line, 1, linelen, rejectfile);
4352 err = got_ferror(rejectfile, GOT_ERR_IO);
4359 static const struct got_error *
4360 skip_one_line(FILE *f)
4363 size_t linesize = 0;
4366 linelen = getline(&line, &linesize, f);
4367 if (linelen == -1) {
4369 return got_error_from_errno("getline");
4376 static const struct got_error *
4377 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4378 int start_old, int end_old, int start_new, int end_new,
4379 FILE *outfile, FILE *rejectfile)
4381 const struct got_error *err;
4383 /* Copy old file's lines leading up to patch. */
4384 while (!feof(f1) && *line_cur1 < start_old) {
4385 err = copy_one_line(f1, outfile, NULL);
4390 /* Skip new file's lines leading up to patch. */
4391 while (!feof(f2) && *line_cur2 < start_new) {
4393 err = copy_one_line(f2, NULL, rejectfile);
4395 err = skip_one_line(f2);
4400 /* Copy patched lines. */
4401 while (!feof(f2) && *line_cur2 <= end_new) {
4402 err = copy_one_line(f2, outfile, NULL);
4407 /* Skip over old file's replaced lines. */
4408 while (!feof(f1) && *line_cur1 <= end_old) {
4410 err = copy_one_line(f1, NULL, rejectfile);
4412 err = skip_one_line(f1);
4421 static const struct got_error *
4422 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4423 FILE *outfile, FILE *rejectfile)
4425 const struct got_error *err;
4428 /* Copy old file's lines until EOF. */
4430 err = copy_one_line(f1, outfile, NULL);
4437 /* Copy new file's lines until EOF. */
4439 err = copy_one_line(f2, NULL, rejectfile);
4449 static const struct got_error *
4450 apply_or_reject_change(int *choice, int *nchunks_used,
4451 struct diff_result *diff_result, int n,
4452 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4453 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4454 got_worktree_patch_cb patch_cb, void *patch_arg)
4456 const struct got_error *err = NULL;
4457 struct diff_chunk_context cc = {};
4458 int start_old, end_old, start_new, end_new;
4460 struct diff_output_unidiff_state *diff_state;
4461 struct diff_input_info diff_info;
4464 *choice = GOT_PATCH_CHOICE_NONE;
4466 /* Get changed line numbers without context lines for copy_change(). */
4467 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4468 start_old = cc.left.start;
4469 end_old = cc.left.end;
4470 start_new = cc.right.start;
4471 end_new = cc.right.end;
4473 /* Get the same change with context lines for display. */
4474 memset(&cc, 0, sizeof(cc));
4475 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4477 memset(&diff_info, 0, sizeof(diff_info));
4478 diff_info.left_path = relpath;
4479 diff_info.right_path = relpath;
4481 diff_state = diff_output_unidiff_state_alloc();
4482 if (diff_state == NULL)
4483 return got_error_set_errno(ENOMEM,
4484 "diff_output_unidiff_state_alloc");
4486 hunkfile = got_opentemp();
4487 if (hunkfile == NULL) {
4488 err = got_error_from_errno("got_opentemp");
4492 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4494 if (rc != DIFF_RC_OK) {
4495 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4499 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4500 err = got_ferror(hunkfile, GOT_ERR_IO);
4504 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4505 hunkfile, changeno, nchanges);
4510 case GOT_PATCH_CHOICE_YES:
4511 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4512 end_old, start_new, end_new, outfile, rejectfile);
4514 case GOT_PATCH_CHOICE_NO:
4515 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4516 end_old, start_new, end_new, rejectfile, outfile);
4518 case GOT_PATCH_CHOICE_QUIT:
4521 err = got_error(GOT_ERR_PATCH_CHOICE);
4525 diff_output_unidiff_state_free(diff_state);
4526 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4527 err = got_error_from_errno("fclose");
4531 struct revert_file_args {
4532 struct got_worktree *worktree;
4533 struct got_fileindex *fileindex;
4534 got_worktree_checkout_cb progress_cb;
4536 got_worktree_patch_cb patch_cb;
4538 struct got_repository *repo;
4539 int unlink_added_files;
4542 static const struct got_error *
4543 create_patched_content(char **path_outfile, int reverse_patch,
4544 struct got_object_id *blob_id, const char *path2,
4545 int dirfd2, const char *de_name2,
4546 const char *relpath, struct got_repository *repo,
4547 got_worktree_patch_cb patch_cb, void *patch_arg)
4549 const struct got_error *err, *free_err;
4550 struct got_blob_object *blob = NULL;
4551 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4552 int fd = -1, fd2 = -1;
4553 char link_target[PATH_MAX];
4554 ssize_t link_len = 0;
4555 char *path1 = NULL, *id_str = NULL;
4557 struct got_diffreg_result *diffreg_result = NULL;
4558 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4559 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4561 *path_outfile = NULL;
4563 err = got_object_id_str(&id_str, blob_id);
4568 fd2 = openat(dirfd2, de_name2,
4569 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4571 if (!got_err_open_nofollow_on_symlink()) {
4572 err = got_error_from_errno2("openat", path2);
4575 link_len = readlinkat(dirfd2, de_name2,
4576 link_target, sizeof(link_target));
4577 if (link_len == -1) {
4578 return got_error_from_errno2("readlinkat",
4581 sb2.st_mode = S_IFLNK;
4582 sb2.st_size = link_len;
4585 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4587 if (!got_err_open_nofollow_on_symlink()) {
4588 err = got_error_from_errno2("open", path2);
4591 link_len = readlink(path2, link_target,
4592 sizeof(link_target));
4594 return got_error_from_errno2("readlink", path2);
4595 sb2.st_mode = S_IFLNK;
4596 sb2.st_size = link_len;
4600 if (fstat(fd2, &sb2) == -1) {
4601 err = got_error_from_errno2("fstat", path2);
4605 f2 = fdopen(fd2, "r");
4607 err = got_error_from_errno2("fdopen", path2);
4613 f2 = got_opentemp();
4615 err = got_error_from_errno2("got_opentemp", path2);
4618 n = fwrite(link_target, 1, link_len, f2);
4619 if (n != link_len) {
4620 err = got_ferror(f2, GOT_ERR_IO);
4623 if (fflush(f2) == EOF) {
4624 err = got_error_from_errno("fflush");
4630 fd = got_opentempfd();
4632 err = got_error_from_errno("got_opentempfd");
4636 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4640 err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4644 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4648 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4649 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4653 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4658 if (fseek(f1, 0L, SEEK_SET) == -1)
4659 return got_ferror(f1, GOT_ERR_IO);
4660 if (fseek(f2, 0L, SEEK_SET) == -1)
4661 return got_ferror(f2, GOT_ERR_IO);
4663 /* Count the number of actual changes in the diff result. */
4664 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4665 struct diff_chunk_context cc = {};
4666 diff_chunk_context_load_change(&cc, &nchunks_used,
4667 diffreg_result->result, n, 0);
4670 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4672 err = apply_or_reject_change(&choice, &nchunks_used,
4673 diffreg_result->result, n, relpath, f1, f2,
4674 &line_cur1, &line_cur2,
4675 reverse_patch ? NULL : outfile,
4676 reverse_patch ? outfile : NULL,
4677 ++i, nchanges, patch_cb, patch_arg);
4680 if (choice == GOT_PATCH_CHOICE_YES)
4682 else if (choice == GOT_PATCH_CHOICE_QUIT)
4686 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4687 reverse_patch ? NULL : outfile,
4688 reverse_patch ? outfile : NULL);
4692 if (!S_ISLNK(sb2.st_mode)) {
4695 mode = apply_umask(sb2.st_mode);
4696 if (fchmod(fileno(outfile), mode) == -1) {
4697 err = got_error_from_errno2("fchmod", path2);
4704 if (fd != -1 && close(fd) == -1 && err == NULL)
4705 err = got_error_from_errno("close");
4707 got_object_blob_close(blob);
4708 free_err = got_diffreg_result_free(diffreg_result);
4711 if (f1 && fclose(f1) == EOF && err == NULL)
4712 err = got_error_from_errno2("fclose", path1);
4713 if (f2 && fclose(f2) == EOF && err == NULL)
4714 err = got_error_from_errno2("fclose", path2);
4715 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4716 err = got_error_from_errno2("close", path2);
4717 if (outfile && fclose(outfile) == EOF && err == NULL)
4718 err = got_error_from_errno2("fclose", *path_outfile);
4719 if (path1 && unlink(path1) == -1 && err == NULL)
4720 err = got_error_from_errno2("unlink", path1);
4721 if (err || !have_content) {
4722 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4723 err = got_error_from_errno2("unlink", *path_outfile);
4724 free(*path_outfile);
4725 *path_outfile = NULL;
4731 static const struct got_error *
4732 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4733 const char *relpath, struct got_object_id *blob_id,
4734 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4735 int dirfd, const char *de_name)
4737 struct revert_file_args *a = arg;
4738 const struct got_error *err = NULL;
4739 char *parent_path = NULL;
4740 struct got_fileindex_entry *ie;
4741 struct got_commit_object *base_commit = NULL;
4742 struct got_tree_object *tree = NULL;
4743 struct got_object_id *tree_id = NULL;
4744 const struct got_tree_entry *te = NULL;
4745 char *tree_path = NULL, *te_name;
4746 char *ondisk_path = NULL, *path_content = NULL;
4747 struct got_blob_object *blob = NULL;
4750 /* Reverting a staged deletion is a no-op. */
4751 if (status == GOT_STATUS_DELETE &&
4752 staged_status != GOT_STATUS_NO_CHANGE)
4755 if (status == GOT_STATUS_UNVERSIONED)
4756 return (*a->progress_cb)(a->progress_arg,
4757 GOT_STATUS_UNVERSIONED, relpath);
4759 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4761 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4763 /* Construct in-repository path of tree which contains this blob. */
4764 err = got_path_dirname(&parent_path, ie->path);
4766 if (err->code != GOT_ERR_BAD_PATH)
4768 parent_path = strdup("/");
4769 if (parent_path == NULL) {
4770 err = got_error_from_errno("strdup");
4774 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4775 tree_path = strdup(parent_path);
4776 if (tree_path == NULL) {
4777 err = got_error_from_errno("strdup");
4781 if (got_path_is_root_dir(parent_path)) {
4782 tree_path = strdup(a->worktree->path_prefix);
4783 if (tree_path == NULL) {
4784 err = got_error_from_errno("strdup");
4788 if (asprintf(&tree_path, "%s/%s",
4789 a->worktree->path_prefix, parent_path) == -1) {
4790 err = got_error_from_errno("asprintf");
4796 err = got_object_open_as_commit(&base_commit, a->repo,
4797 a->worktree->base_commit_id);
4801 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4803 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4804 (status == GOT_STATUS_ADD ||
4805 staged_status == GOT_STATUS_ADD)))
4808 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4812 err = got_path_basename(&te_name, ie->path);
4816 te = got_object_tree_find_entry(tree, te_name);
4818 if (te == NULL && status != GOT_STATUS_ADD &&
4819 staged_status != GOT_STATUS_ADD) {
4820 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4826 case GOT_STATUS_ADD:
4828 int choice = GOT_PATCH_CHOICE_NONE;
4829 err = (*a->patch_cb)(&choice, a->patch_arg,
4830 status, ie->path, NULL, 1, 1);
4833 if (choice != GOT_PATCH_CHOICE_YES)
4836 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4840 got_fileindex_entry_remove(a->fileindex, ie);
4841 if (a->unlink_added_files) {
4842 if (asprintf(&ondisk_path, "%s/%s",
4843 got_worktree_get_root_path(a->worktree),
4845 err = got_error_from_errno("asprintf");
4848 if (unlink(ondisk_path) == -1) {
4849 err = got_error_from_errno2("unlink",
4855 case GOT_STATUS_DELETE:
4857 int choice = GOT_PATCH_CHOICE_NONE;
4858 err = (*a->patch_cb)(&choice, a->patch_arg,
4859 status, ie->path, NULL, 1, 1);
4862 if (choice != GOT_PATCH_CHOICE_YES)
4866 case GOT_STATUS_MODIFY:
4867 case GOT_STATUS_MODE_CHANGE:
4868 case GOT_STATUS_CONFLICT:
4869 case GOT_STATUS_MISSING: {
4870 struct got_object_id id;
4871 if (staged_status == GOT_STATUS_ADD ||
4872 staged_status == GOT_STATUS_MODIFY)
4873 got_fileindex_entry_get_staged_blob_id(&id, ie);
4875 got_fileindex_entry_get_blob_id(&id, ie);
4876 fd = got_opentempfd();
4878 err = got_error_from_errno("got_opentempfd");
4882 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4886 if (asprintf(&ondisk_path, "%s/%s",
4887 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4888 err = got_error_from_errno("asprintf");
4892 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4893 status == GOT_STATUS_CONFLICT)) {
4894 int is_bad_symlink = 0;
4895 err = create_patched_content(&path_content, 1, &id,
4896 ondisk_path, dirfd, de_name, ie->path, a->repo,
4897 a->patch_cb, a->patch_arg);
4898 if (err || path_content == NULL)
4900 if (te && S_ISLNK(te->mode)) {
4901 if (unlink(path_content) == -1) {
4902 err = got_error_from_errno2("unlink",
4906 err = install_symlink(&is_bad_symlink,
4907 a->worktree, ondisk_path, ie->path,
4908 blob, 0, 1, 0, 0, a->repo,
4909 a->progress_cb, a->progress_arg);
4911 if (rename(path_content, ondisk_path) == -1) {
4912 err = got_error_from_errno3("rename",
4913 path_content, ondisk_path);
4918 int is_bad_symlink = 0;
4919 if (te && S_ISLNK(te->mode)) {
4920 err = install_symlink(&is_bad_symlink,
4921 a->worktree, ondisk_path, ie->path,
4922 blob, 0, 1, 0, 0, a->repo,
4923 a->progress_cb, a->progress_arg);
4925 err = install_blob(a->worktree, ondisk_path,
4927 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4928 got_fileindex_perms_to_st(ie), blob,
4929 0, 1, 0, 0, a->repo,
4930 a->progress_cb, a->progress_arg);
4934 if (status == GOT_STATUS_DELETE ||
4935 status == GOT_STATUS_MODE_CHANGE) {
4936 err = got_fileindex_entry_update(ie,
4937 a->worktree->root_fd, relpath,
4939 a->worktree->base_commit_id->sha1, 1);
4943 if (is_bad_symlink) {
4944 got_fileindex_entry_filetype_set(ie,
4945 GOT_FILEIDX_MODE_BAD_SYMLINK);
4958 if (fd != -1 && close(fd) == -1 && err == NULL)
4959 err = got_error_from_errno("close");
4961 got_object_blob_close(blob);
4963 got_object_tree_close(tree);
4966 got_object_commit_close(base_commit);
4970 const struct got_error *
4971 got_worktree_revert(struct got_worktree *worktree,
4972 struct got_pathlist_head *paths,
4973 got_worktree_checkout_cb progress_cb, void *progress_arg,
4974 got_worktree_patch_cb patch_cb, void *patch_arg,
4975 struct got_repository *repo)
4977 struct got_fileindex *fileindex = NULL;
4978 char *fileindex_path = NULL;
4979 const struct got_error *err = NULL, *unlockerr = NULL;
4980 const struct got_error *sync_err = NULL;
4981 struct got_pathlist_entry *pe;
4982 struct revert_file_args rfa;
4984 err = lock_worktree(worktree, LOCK_EX);
4988 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4992 rfa.worktree = worktree;
4993 rfa.fileindex = fileindex;
4994 rfa.progress_cb = progress_cb;
4995 rfa.progress_arg = progress_arg;
4996 rfa.patch_cb = patch_cb;
4997 rfa.patch_arg = patch_arg;
4999 rfa.unlink_added_files = 0;
5000 TAILQ_FOREACH(pe, paths, entry) {
5001 err = worktree_status(worktree, pe->path, fileindex, repo,
5002 revert_file, &rfa, NULL, NULL, 1, 0);
5006 sync_err = sync_fileindex(fileindex, fileindex_path);
5007 if (sync_err && err == NULL)
5010 free(fileindex_path);
5012 got_fileindex_free(fileindex);
5013 unlockerr = lock_worktree(worktree, LOCK_SH);
5014 if (unlockerr && err == NULL)
5020 free_commitable(struct got_commitable *ct)
5023 free(ct->in_repo_path);
5024 free(ct->ondisk_path);
5026 free(ct->base_blob_id);
5027 free(ct->staged_blob_id);
5028 free(ct->base_commit_id);
5032 struct collect_commitables_arg {
5033 struct got_pathlist_head *commitable_paths;
5034 struct got_repository *repo;
5035 struct got_worktree *worktree;
5036 struct got_fileindex *fileindex;
5037 int have_staged_files;
5038 int allow_bad_symlinks;
5039 int diff_header_shown;
5040 int commit_conflicts;
5047 * Create a file which contains the target path of a symlink so we can feed
5048 * it as content to the diff engine.
5050 static const struct got_error *
5051 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5052 const char *abspath)
5054 const struct got_error *err = NULL;
5055 char target_path[PATH_MAX];
5056 ssize_t target_len, outlen;
5061 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5062 if (target_len == -1)
5063 return got_error_from_errno2("readlinkat", abspath);
5065 target_len = readlink(abspath, target_path, PATH_MAX);
5066 if (target_len == -1)
5067 return got_error_from_errno2("readlink", abspath);
5070 *fd = got_opentempfd();
5072 return got_error_from_errno("got_opentempfd");
5074 outlen = write(*fd, target_path, target_len);
5076 err = got_error_from_errno("got_opentempfd");
5080 if (lseek(*fd, 0, SEEK_SET) == -1) {
5081 err = got_error_from_errno2("lseek", abspath);
5092 static const struct got_error *
5093 append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5094 FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5095 int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5097 const struct got_error *err = NULL;
5098 struct got_blob_object *blob1 = NULL;
5099 int fd = -1, fd1 = -1, fd2 = -1;
5100 FILE *ondisk_file = NULL;
5101 char *label1 = NULL;
5105 char *id_str = NULL;
5107 memset(&sb, 0, sizeof(sb));
5110 if (ct->staged_status != GOT_STATUS_MODIFY &&
5111 ct->staged_status != GOT_STATUS_ADD &&
5112 ct->staged_status != GOT_STATUS_DELETE)
5115 if (ct->status != GOT_STATUS_MODIFY &&
5116 ct->status != GOT_STATUS_ADD &&
5117 ct->status != GOT_STATUS_DELETE &&
5118 ct->status != GOT_STATUS_CONFLICT)
5122 err = got_opentemp_truncate(f1);
5124 return got_error_from_errno("got_opentemp_truncate");
5125 err = got_opentemp_truncate(f2);
5127 return got_error_from_errno("got_opentemp_truncate");
5129 if (!*diff_header_shown) {
5130 err = got_object_id_str(&id_str, worktree->base_commit_id);
5133 fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5134 got_worktree_get_root_path(worktree));
5135 fprintf(diff_outfile, "commit - %s\n", id_str);
5136 fprintf(diff_outfile, "path + %s%s\n",
5137 got_worktree_get_root_path(worktree),
5138 diff_staged ? " (staged changes)" : "");
5139 *diff_header_shown = 1;
5143 const char *label1 = NULL, *label2 = NULL;
5144 switch (ct->staged_status) {
5145 case GOT_STATUS_MODIFY:
5149 case GOT_STATUS_ADD:
5152 case GOT_STATUS_DELETE:
5156 return got_error(GOT_ERR_FILE_STATUS);
5158 fd1 = got_opentempfd();
5160 err = got_error_from_errno("got_opentempfd");
5163 fd2 = got_opentempfd();
5165 err = got_error_from_errno("got_opentempfd");
5168 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5169 fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5170 label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5171 NULL, repo, diff_outfile);
5175 fd1 = got_opentempfd();
5177 err = got_error_from_errno("got_opentempfd");
5181 if (ct->status != GOT_STATUS_ADD) {
5182 err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5188 if (ct->status != GOT_STATUS_DELETE) {
5190 fd = openat(dirfd, de_name,
5191 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5193 if (!got_err_open_nofollow_on_symlink()) {
5194 err = got_error_from_errno2("openat",
5198 err = get_symlink_target_file(&fd, dirfd,
5199 de_name, ct->ondisk_path);
5204 fd = open(ct->ondisk_path,
5205 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5207 if (!got_err_open_nofollow_on_symlink()) {
5208 err = got_error_from_errno2("open",
5212 err = get_symlink_target_file(&fd, dirfd,
5213 de_name, ct->ondisk_path);
5218 if (fstatat(fd, ct->ondisk_path, &sb,
5219 AT_SYMLINK_NOFOLLOW) == -1) {
5220 err = got_error_from_errno2("fstatat", ct->ondisk_path);
5223 ondisk_file = fdopen(fd, "r");
5224 if (ondisk_file == NULL) {
5225 err = got_error_from_errno2("fdopen", ct->ondisk_path);
5233 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5239 err = got_diff_blob_file(blob1, f1, size1, label1,
5240 ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5241 GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5243 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5244 err = got_error_from_errno("close");
5245 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5246 err = got_error_from_errno("close");
5248 got_object_blob_close(blob1);
5249 if (fd != -1 && close(fd) == -1 && err == NULL)
5250 err = got_error_from_errno("close");
5251 if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5252 err = got_error_from_errno("fclose");
5256 static const struct got_error *
5257 collect_commitables(void *arg, unsigned char status,
5258 unsigned char staged_status, const char *relpath,
5259 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5260 struct got_object_id *commit_id, int dirfd, const char *de_name)
5262 struct collect_commitables_arg *a = arg;
5263 const struct got_error *err = NULL;
5264 struct got_commitable *ct = NULL;
5265 struct got_pathlist_entry *new = NULL;
5266 char *parent_path = NULL, *path = NULL;
5269 if (a->have_staged_files) {
5270 if (staged_status != GOT_STATUS_MODIFY &&
5271 staged_status != GOT_STATUS_ADD &&
5272 staged_status != GOT_STATUS_DELETE)
5275 if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5276 printf("C %s\n", relpath);
5277 return got_error(GOT_ERR_COMMIT_CONFLICT);
5280 if (status != GOT_STATUS_MODIFY &&
5281 status != GOT_STATUS_MODE_CHANGE &&
5282 status != GOT_STATUS_ADD &&
5283 status != GOT_STATUS_DELETE &&
5284 status != GOT_STATUS_CONFLICT)
5288 if (asprintf(&path, "/%s", relpath) == -1) {
5289 err = got_error_from_errno("asprintf");
5292 if (strcmp(path, "/") == 0) {
5293 parent_path = strdup("");
5294 if (parent_path == NULL)
5295 return got_error_from_errno("strdup");
5297 err = got_path_dirname(&parent_path, path);
5302 ct = calloc(1, sizeof(*ct));
5304 err = got_error_from_errno("calloc");
5308 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5310 err = got_error_from_errno("asprintf");
5314 if (staged_status == GOT_STATUS_ADD ||
5315 staged_status == GOT_STATUS_MODIFY) {
5316 struct got_fileindex_entry *ie;
5317 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5318 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5319 case GOT_FILEIDX_MODE_REGULAR_FILE:
5320 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5323 case GOT_FILEIDX_MODE_SYMLINK:
5327 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5330 ct->mode |= got_fileindex_entry_perms_get(ie);
5331 } else if (status != GOT_STATUS_DELETE &&
5332 staged_status != GOT_STATUS_DELETE) {
5334 if (fstatat(dirfd, de_name, &sb,
5335 AT_SYMLINK_NOFOLLOW) == -1) {
5336 err = got_error_from_errno2("fstatat",
5340 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5341 err = got_error_from_errno2("lstat", ct->ondisk_path);
5344 ct->mode = sb.st_mode;
5347 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5348 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5350 err = got_error_from_errno("asprintf");
5354 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5355 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5357 char target_path[PATH_MAX];
5359 target_len = readlink(ct->ondisk_path, target_path,
5360 sizeof(target_path));
5361 if (target_len == -1) {
5362 err = got_error_from_errno2("readlink",
5366 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5367 target_len, ct->ondisk_path, a->worktree->root_path);
5370 if (is_bad_symlink) {
5371 err = got_error_path(ct->ondisk_path,
5372 GOT_ERR_BAD_SYMLINK);
5378 ct->status = status;
5379 ct->staged_status = staged_status;
5380 ct->blob_id = NULL; /* will be filled in when blob gets created */
5381 if (ct->status != GOT_STATUS_ADD &&
5382 ct->staged_status != GOT_STATUS_ADD) {
5383 ct->base_blob_id = got_object_id_dup(blob_id);
5384 if (ct->base_blob_id == NULL) {
5385 err = got_error_from_errno("got_object_id_dup");
5388 ct->base_commit_id = got_object_id_dup(commit_id);
5389 if (ct->base_commit_id == NULL) {
5390 err = got_error_from_errno("got_object_id_dup");
5394 if (ct->staged_status == GOT_STATUS_ADD ||
5395 ct->staged_status == GOT_STATUS_MODIFY) {
5396 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5397 if (ct->staged_blob_id == NULL) {
5398 err = got_error_from_errno("got_object_id_dup");
5402 ct->path = strdup(path);
5403 if (ct->path == NULL) {
5404 err = got_error_from_errno("strdup");
5407 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5411 if (a->diff_outfile && ct && new != NULL) {
5412 err = append_ct_diff(ct, &a->diff_header_shown,
5413 a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5414 a->have_staged_files, a->repo, a->worktree);
5419 if (ct && (err || new == NULL))
5420 free_commitable(ct);
5426 static const struct got_error *write_tree(struct got_object_id **, int *,
5427 struct got_tree_object *, const char *, struct got_pathlist_head *,
5428 got_worktree_status_cb status_cb, void *status_arg,
5429 struct got_repository *);
5431 static const struct got_error *
5432 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5433 struct got_tree_entry *te, const char *parent_path,
5434 struct got_pathlist_head *commitable_paths,
5435 got_worktree_status_cb status_cb, void *status_arg,
5436 struct got_repository *repo)
5438 const struct got_error *err = NULL;
5439 struct got_tree_object *subtree;
5442 if (asprintf(&subpath, "%s%s%s", parent_path,
5443 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5444 return got_error_from_errno("asprintf");
5446 err = got_object_open_as_tree(&subtree, repo, &te->id);
5450 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5451 commitable_paths, status_cb, status_arg, repo);
5452 got_object_tree_close(subtree);
5457 static const struct got_error *
5458 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5460 const struct got_error *err = NULL;
5461 char *ct_parent_path = NULL;
5465 if (strchr(ct->in_repo_path, '/') == NULL) {
5466 *match = got_path_is_root_dir(path);
5470 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5473 *match = (strcmp(path, ct_parent_path) == 0);
5474 free(ct_parent_path);
5479 get_ct_file_mode(struct got_commitable *ct)
5481 if (S_ISLNK(ct->mode))
5484 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5487 static const struct got_error *
5488 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5489 struct got_tree_entry *te, struct got_commitable *ct)
5491 const struct got_error *err = NULL;
5495 err = got_object_tree_entry_dup(new_te, te);
5499 (*new_te)->mode = get_ct_file_mode(ct);
5501 if (ct->staged_status == GOT_STATUS_MODIFY)
5502 memcpy(&(*new_te)->id, ct->staged_blob_id,
5503 sizeof((*new_te)->id));
5505 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5507 if (err && *new_te) {
5514 static const struct got_error *
5515 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5516 struct got_commitable *ct)
5518 const struct got_error *err = NULL;
5519 char *ct_name = NULL;
5523 *new_te = calloc(1, sizeof(**new_te));
5524 if (*new_te == NULL)
5525 return got_error_from_errno("calloc");
5527 err = got_path_basename(&ct_name, ct->path);
5530 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5531 sizeof((*new_te)->name)) {
5532 err = got_error(GOT_ERR_NO_SPACE);
5536 (*new_te)->mode = get_ct_file_mode(ct);
5538 if (ct->staged_status == GOT_STATUS_ADD)
5539 memcpy(&(*new_te)->id, ct->staged_blob_id,
5540 sizeof((*new_te)->id));
5542 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5545 if (err && *new_te) {
5552 static const struct got_error *
5553 insert_tree_entry(struct got_tree_entry *new_te,
5554 struct got_pathlist_head *paths)
5556 const struct got_error *err = NULL;
5557 struct got_pathlist_entry *new_pe;
5559 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5563 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5567 static const struct got_error *
5568 report_ct_status(struct got_commitable *ct,
5569 got_worktree_status_cb status_cb, void *status_arg)
5571 const char *ct_path = ct->path;
5572 unsigned char status;
5574 if (status_cb == NULL) /* no commit progress output desired */
5577 while (ct_path[0] == '/')
5580 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5581 status = ct->staged_status;
5583 status = ct->status;
5585 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5586 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5589 static const struct got_error *
5590 match_modified_subtree(int *modified, struct got_tree_entry *te,
5591 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5593 const struct got_error *err = NULL;
5594 struct got_pathlist_entry *pe;
5599 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5600 got_path_is_root_dir(base_tree_path) ? "" : "/",
5602 return got_error_from_errno("asprintf");
5604 TAILQ_FOREACH(pe, commitable_paths, entry) {
5605 struct got_commitable *ct = pe->data;
5606 *modified = got_path_is_child(ct->in_repo_path, te_path,
5616 static const struct got_error *
5617 match_deleted_or_modified_ct(struct got_commitable **ctp,
5618 struct got_tree_entry *te, const char *base_tree_path,
5619 struct got_pathlist_head *commitable_paths)
5621 const struct got_error *err = NULL;
5622 struct got_pathlist_entry *pe;
5626 TAILQ_FOREACH(pe, commitable_paths, entry) {
5627 struct got_commitable *ct = pe->data;
5628 char *ct_name = NULL;
5631 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5632 if (ct->status != GOT_STATUS_MODIFY &&
5633 ct->status != GOT_STATUS_MODE_CHANGE &&
5634 ct->status != GOT_STATUS_DELETE &&
5635 ct->status != GOT_STATUS_CONFLICT)
5638 if (ct->staged_status != GOT_STATUS_MODIFY &&
5639 ct->staged_status != GOT_STATUS_DELETE)
5643 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5646 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5652 err = got_path_basename(&ct_name, pe->path);
5656 if (strcmp(te->name, ct_name) != 0) {
5669 static const struct got_error *
5670 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5671 const char *child_path, const char *path_base_tree,
5672 struct got_pathlist_head *commitable_paths,
5673 got_worktree_status_cb status_cb, void *status_arg,
5674 struct got_repository *repo)
5676 const struct got_error *err = NULL;
5677 struct got_tree_entry *new_te;
5679 struct got_object_id *id = NULL;
5684 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5685 got_path_is_root_dir(path_base_tree) ? "" : "/",
5687 return got_error_from_errno("asprintf");
5689 new_te = calloc(1, sizeof(*new_te));
5691 return got_error_from_errno("calloc");
5692 new_te->mode = S_IFDIR;
5694 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5695 sizeof(new_te->name)) {
5696 err = got_error(GOT_ERR_NO_SPACE);
5699 err = write_tree(&id, &nentries, NULL, subtree_path,
5700 commitable_paths, status_cb, status_arg, repo);
5705 memcpy(&new_te->id, id, sizeof(new_te->id));
5714 static const struct got_error *
5715 write_tree(struct got_object_id **new_tree_id, int *nentries,
5716 struct got_tree_object *base_tree, const char *path_base_tree,
5717 struct got_pathlist_head *commitable_paths,
5718 got_worktree_status_cb status_cb, void *status_arg,
5719 struct got_repository *repo)
5721 const struct got_error *err = NULL;
5722 struct got_pathlist_head paths;
5723 struct got_tree_entry *te, *new_te = NULL;
5724 struct got_pathlist_entry *pe;
5729 /* Insert, and recurse into, newly added entries first. */
5730 TAILQ_FOREACH(pe, commitable_paths, entry) {
5731 struct got_commitable *ct = pe->data;
5732 char *child_path = NULL, *slash;
5734 if ((ct->status != GOT_STATUS_ADD &&
5735 ct->staged_status != GOT_STATUS_ADD) ||
5736 (ct->flags & GOT_COMMITABLE_ADDED))
5739 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5740 strlen(path_base_tree)))
5743 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5748 slash = strchr(child_path, '/');
5749 if (slash == NULL) {
5750 err = alloc_added_blob_tree_entry(&new_te, ct);
5753 err = report_ct_status(ct, status_cb, status_arg);
5756 ct->flags |= GOT_COMMITABLE_ADDED;
5757 err = insert_tree_entry(new_te, &paths);
5762 *slash = '\0'; /* trim trailing path components */
5763 if (base_tree == NULL ||
5764 got_object_tree_find_entry(base_tree, child_path)
5766 err = make_subtree_for_added_blob(&new_te,
5767 child_path, path_base_tree,
5768 commitable_paths, status_cb, status_arg,
5772 err = insert_tree_entry(new_te, &paths);
5781 int i, nbase_entries;
5782 /* Handle modified and deleted entries. */
5783 nbase_entries = got_object_tree_get_nentries(base_tree);
5784 for (i = 0; i < nbase_entries; i++) {
5785 struct got_commitable *ct = NULL;
5787 te = got_object_tree_get_entry(base_tree, i);
5788 if (got_object_tree_entry_is_submodule(te)) {
5789 /* Entry is a submodule; just copy it. */
5790 err = got_object_tree_entry_dup(&new_te, te);
5793 err = insert_tree_entry(new_te, &paths);
5800 if (S_ISDIR(te->mode)) {
5802 err = got_object_tree_entry_dup(&new_te, te);
5805 err = match_modified_subtree(&modified, te,
5806 path_base_tree, commitable_paths);
5809 /* Avoid recursion into unmodified subtrees. */
5811 struct got_object_id *new_id;
5813 err = write_subtree(&new_id,
5815 path_base_tree, commitable_paths,
5816 status_cb, status_arg, repo);
5819 if (nsubentries == 0) {
5820 /* All entries were deleted. */
5824 memcpy(&new_te->id, new_id,
5825 sizeof(new_te->id));
5828 err = insert_tree_entry(new_te, &paths);
5835 err = match_deleted_or_modified_ct(&ct, te,
5836 path_base_tree, commitable_paths);
5840 /* NB: Deleted entries get dropped here. */
5841 if (ct->status == GOT_STATUS_MODIFY ||
5842 ct->status == GOT_STATUS_MODE_CHANGE ||
5843 ct->status == GOT_STATUS_CONFLICT ||
5844 ct->staged_status == GOT_STATUS_MODIFY) {
5845 err = alloc_modified_blob_tree_entry(
5849 err = insert_tree_entry(new_te, &paths);
5854 err = report_ct_status(ct, status_cb,
5859 /* Entry is unchanged; just copy it. */
5860 err = got_object_tree_entry_dup(&new_te, te);
5863 err = insert_tree_entry(new_te, &paths);
5871 /* Write new list of entries; deleted entries have been dropped. */
5872 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5874 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
5878 static const struct got_error *
5879 update_fileindex_after_commit(struct got_worktree *worktree,
5880 struct got_pathlist_head *commitable_paths,
5881 struct got_object_id *new_base_commit_id,
5882 struct got_fileindex *fileindex, int have_staged_files)
5884 const struct got_error *err = NULL;
5885 struct got_pathlist_entry *pe;
5886 char *relpath = NULL;
5888 TAILQ_FOREACH(pe, commitable_paths, entry) {
5889 struct got_fileindex_entry *ie;
5890 struct got_commitable *ct = pe->data;
5892 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5894 err = got_path_skip_common_ancestor(&relpath,
5895 worktree->root_path, ct->ondisk_path);
5900 if (ct->status == GOT_STATUS_DELETE ||
5901 ct->staged_status == GOT_STATUS_DELETE) {
5902 got_fileindex_entry_remove(fileindex, ie);
5903 } else if (ct->staged_status == GOT_STATUS_ADD ||
5904 ct->staged_status == GOT_STATUS_MODIFY) {
5905 got_fileindex_entry_stage_set(ie,
5906 GOT_FILEIDX_STAGE_NONE);
5907 got_fileindex_entry_staged_filetype_set(ie, 0);
5909 err = got_fileindex_entry_update(ie,
5910 worktree->root_fd, relpath,
5911 ct->staged_blob_id->sha1,
5912 new_base_commit_id->sha1,
5913 !have_staged_files);
5915 err = got_fileindex_entry_update(ie,
5916 worktree->root_fd, relpath,
5918 new_base_commit_id->sha1,
5919 !have_staged_files);
5921 err = got_fileindex_entry_alloc(&ie, pe->path);
5924 err = got_fileindex_entry_update(ie,
5925 worktree->root_fd, relpath, ct->blob_id->sha1,
5926 new_base_commit_id->sha1, 1);
5928 got_fileindex_entry_free(ie);
5931 err = got_fileindex_entry_add(fileindex, ie);
5933 got_fileindex_entry_free(ie);
5946 static const struct got_error *
5947 check_out_of_date(const char *in_repo_path, unsigned char status,
5948 unsigned char staged_status, struct got_object_id *base_blob_id,
5949 struct got_object_id *base_commit_id,
5950 struct got_object_id *head_commit_id, struct got_repository *repo,
5953 const struct got_error *err = NULL;
5954 struct got_commit_object *commit = NULL;
5955 struct got_object_id *id = NULL;
5957 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5958 /* Trivial case: base commit == head commit */
5959 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5962 * Ensure file content which local changes were based
5963 * on matches file content in the branch head.
5965 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5968 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5970 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5971 err = got_error(ood_errcode);
5973 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5974 err = got_error(ood_errcode);
5976 /* Require that added files don't exist in the branch head. */
5977 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5980 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5981 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5983 err = id ? got_error(ood_errcode) : NULL;
5988 got_object_commit_close(commit);
5992 static const struct got_error *
5993 commit_worktree(struct got_object_id **new_commit_id,
5994 struct got_pathlist_head *commitable_paths,
5995 struct got_object_id *head_commit_id,
5996 struct got_object_id *parent_id2,
5997 struct got_worktree *worktree,
5998 const char *author, const char *committer, char *diff_path,
5999 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6000 got_worktree_status_cb status_cb, void *status_arg,
6001 struct got_repository *repo)
6003 const struct got_error *err = NULL, *unlockerr = NULL;
6004 struct got_pathlist_entry *pe;
6005 const char *head_ref_name = NULL;
6006 struct got_commit_object *head_commit = NULL;
6007 struct got_reference *head_ref2 = NULL;
6008 struct got_object_id *head_commit_id2 = NULL;
6009 struct got_tree_object *head_tree = NULL;
6010 struct got_object_id *new_tree_id = NULL;
6011 int nentries, nparents = 0;
6012 struct got_object_id_queue parent_ids;
6013 struct got_object_qid *pid = NULL;
6014 char *logmsg = NULL;
6017 *new_commit_id = NULL;
6019 STAILQ_INIT(&parent_ids);
6021 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6025 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6029 if (commit_msg_cb != NULL) {
6030 err = commit_msg_cb(commitable_paths, diff_path,
6031 &logmsg, commit_arg);
6036 if (logmsg == NULL || strlen(logmsg) == 0) {
6037 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6041 /* Create blobs from added and modified files and record their IDs. */
6042 TAILQ_FOREACH(pe, commitable_paths, entry) {
6043 struct got_commitable *ct = pe->data;
6046 /* Blobs for staged files already exist. */
6047 if (ct->staged_status == GOT_STATUS_ADD ||
6048 ct->staged_status == GOT_STATUS_MODIFY)
6051 if (ct->status != GOT_STATUS_ADD &&
6052 ct->status != GOT_STATUS_MODIFY &&
6053 ct->status != GOT_STATUS_MODE_CHANGE &&
6054 ct->status != GOT_STATUS_CONFLICT)
6057 if (asprintf(&ondisk_path, "%s/%s",
6058 worktree->root_path, pe->path) == -1) {
6059 err = got_error_from_errno("asprintf");
6062 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6068 /* Recursively write new tree objects. */
6069 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6070 commitable_paths, status_cb, status_arg, repo);
6074 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
6077 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6080 err = got_object_qid_alloc(&pid, parent_id2);
6083 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6086 timestamp = time(NULL);
6087 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6088 nparents, author, timestamp, committer, timestamp, logmsg, repo);
6094 /* Check if a concurrent commit to our branch has occurred. */
6095 head_ref_name = got_worktree_get_head_ref_name(worktree);
6096 if (head_ref_name == NULL) {
6097 err = got_error_from_errno("got_worktree_get_head_ref_name");
6100 /* Lock the reference here to prevent concurrent modification. */
6101 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6104 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6107 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6108 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6111 /* Update branch head in repository. */
6112 err = got_ref_change_ref(head_ref2, *new_commit_id);
6115 err = got_ref_write(head_ref2, repo);
6119 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6123 err = ref_base_commit(worktree, repo);
6127 got_object_id_queue_free(&parent_ids);
6129 got_object_tree_close(head_tree);
6131 got_object_commit_close(head_commit);
6132 free(head_commit_id2);
6134 unlockerr = got_ref_unlock(head_ref2);
6135 if (unlockerr && err == NULL)
6137 got_ref_close(head_ref2);
6142 static const struct got_error *
6143 check_path_is_commitable(const char *path,
6144 struct got_pathlist_head *commitable_paths)
6146 struct got_pathlist_entry *cpe = NULL;
6147 size_t path_len = strlen(path);
6149 TAILQ_FOREACH(cpe, commitable_paths, entry) {
6150 struct got_commitable *ct = cpe->data;
6151 const char *ct_path = ct->path;
6153 while (ct_path[0] == '/')
6156 if (strcmp(path, ct_path) == 0 ||
6157 got_path_is_child(ct_path, path, path_len))
6162 return got_error_path(path, GOT_ERR_BAD_PATH);
6167 static const struct got_error *
6168 check_staged_file(void *arg, struct got_fileindex_entry *ie)
6170 int *have_staged_files = arg;
6172 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6173 *have_staged_files = 1;
6174 return got_error(GOT_ERR_CANCELLED);
6180 static const struct got_error *
6181 check_non_staged_files(struct got_fileindex *fileindex,
6182 struct got_pathlist_head *paths)
6184 struct got_pathlist_entry *pe;
6185 struct got_fileindex_entry *ie;
6187 TAILQ_FOREACH(pe, paths, entry) {
6188 if (pe->path[0] == '\0')
6190 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6192 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6193 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6194 return got_error_path(pe->path,
6195 GOT_ERR_FILE_NOT_STAGED);
6201 const struct got_error *
6202 got_worktree_commit(struct got_object_id **new_commit_id,
6203 struct got_worktree *worktree, struct got_pathlist_head *paths,
6204 const char *author, const char *committer, int allow_bad_symlinks,
6205 int show_diff, int commit_conflicts,
6206 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6207 got_worktree_status_cb status_cb, void *status_arg,
6208 struct got_repository *repo)
6210 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6211 struct got_fileindex *fileindex = NULL;
6212 char *fileindex_path = NULL;
6213 struct got_pathlist_head commitable_paths;
6214 struct collect_commitables_arg cc_arg;
6215 struct got_pathlist_entry *pe;
6216 struct got_reference *head_ref = NULL;
6217 struct got_object_id *head_commit_id = NULL;
6218 char *diff_path = NULL;
6219 int have_staged_files = 0;
6221 *new_commit_id = NULL;
6223 memset(&cc_arg, 0, sizeof(cc_arg));
6224 TAILQ_INIT(&commitable_paths);
6226 err = lock_worktree(worktree, LOCK_EX);
6230 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6234 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6238 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6242 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6243 &have_staged_files);
6244 if (err && err->code != GOT_ERR_CANCELLED)
6246 if (have_staged_files) {
6247 err = check_non_staged_files(fileindex, paths);
6252 cc_arg.commitable_paths = &commitable_paths;
6253 cc_arg.worktree = worktree;
6254 cc_arg.fileindex = fileindex;
6256 cc_arg.have_staged_files = have_staged_files;
6257 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6258 cc_arg.diff_header_shown = 0;
6259 cc_arg.commit_conflicts = commit_conflicts;
6261 err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6262 GOT_TMPDIR_STR "/got", ".diff");
6265 cc_arg.f1 = got_opentemp();
6266 if (cc_arg.f1 == NULL) {
6267 err = got_error_from_errno("got_opentemp");
6270 cc_arg.f2 = got_opentemp();
6271 if (cc_arg.f2 == NULL) {
6272 err = got_error_from_errno("got_opentemp");
6277 TAILQ_FOREACH(pe, paths, entry) {
6278 err = worktree_status(worktree, pe->path, fileindex, repo,
6279 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6285 if (fflush(cc_arg.diff_outfile) == EOF) {
6286 err = got_error_from_errno("fflush");
6291 if (TAILQ_EMPTY(&commitable_paths)) {
6292 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6296 TAILQ_FOREACH(pe, paths, entry) {
6297 err = check_path_is_commitable(pe->path, &commitable_paths);
6302 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6303 struct got_commitable *ct = pe->data;
6304 const char *ct_path = ct->in_repo_path;
6306 while (ct_path[0] == '/')
6308 err = check_out_of_date(ct_path, ct->status,
6309 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6310 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6316 err = commit_worktree(new_commit_id, &commitable_paths,
6317 head_commit_id, NULL, worktree, author, committer,
6318 (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6319 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6323 err = update_fileindex_after_commit(worktree, &commitable_paths,
6324 *new_commit_id, fileindex, have_staged_files);
6325 sync_err = sync_fileindex(fileindex, fileindex_path);
6326 if (sync_err && err == NULL)
6330 got_fileindex_free(fileindex);
6331 free(fileindex_path);
6332 unlockerr = lock_worktree(worktree, LOCK_SH);
6333 if (unlockerr && err == NULL)
6335 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6336 struct got_commitable *ct = pe->data;
6338 free_commitable(ct);
6340 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6341 if (diff_path && unlink(diff_path) == -1 && err == NULL)
6342 err = got_error_from_errno2("unlink", diff_path);
6344 if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6346 err = got_error_from_errno("fclose");
6351 got_commitable_get_path(struct got_commitable *ct)
6357 got_commitable_get_status(struct got_commitable *ct)
6362 struct check_rebase_ok_arg {
6363 struct got_worktree *worktree;
6364 struct got_repository *repo;
6367 static const struct got_error *
6368 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6370 const struct got_error *err = NULL;
6371 struct check_rebase_ok_arg *a = arg;
6372 unsigned char status;
6376 /* Reject rebase of a work tree with mixed base commits. */
6377 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6378 SHA1_DIGEST_LENGTH))
6379 return got_error(GOT_ERR_MIXED_COMMITS);
6381 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6383 return got_error_from_errno("asprintf");
6385 /* Reject rebase of a work tree with modified or staged files. */
6386 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6391 if (status != GOT_STATUS_NO_CHANGE)
6392 return got_error(GOT_ERR_MODIFIED);
6393 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6394 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6399 const struct got_error *
6400 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6401 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6402 struct got_worktree *worktree, struct got_reference *branch,
6403 struct got_repository *repo)
6405 const struct got_error *err = NULL;
6406 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6407 char *branch_ref_name = NULL;
6408 char *fileindex_path = NULL;
6409 struct check_rebase_ok_arg ok_arg;
6410 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6411 struct got_object_id *wt_branch_tip = NULL;
6413 *new_base_branch_ref = NULL;
6417 err = lock_worktree(worktree, LOCK_EX);
6421 err = open_fileindex(fileindex, &fileindex_path, worktree);
6425 ok_arg.worktree = worktree;
6427 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6432 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6436 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6440 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6444 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6449 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6452 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6453 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6457 err = got_ref_alloc_symref(new_base_branch_ref,
6458 new_base_branch_ref_name, wt_branch);
6461 err = got_ref_write(*new_base_branch_ref, repo);
6465 /* TODO Lock original branch's ref while rebasing? */
6467 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6471 err = got_ref_write(branch_ref, repo);
6475 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6476 worktree->base_commit_id);
6479 err = got_ref_write(*tmp_branch, repo);
6483 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6487 free(fileindex_path);
6488 free(tmp_branch_name);
6489 free(new_base_branch_ref_name);
6490 free(branch_ref_name);
6492 got_ref_close(branch_ref);
6494 got_ref_close(wt_branch);
6495 free(wt_branch_tip);
6497 if (*new_base_branch_ref) {
6498 got_ref_close(*new_base_branch_ref);
6499 *new_base_branch_ref = NULL;
6502 got_ref_close(*tmp_branch);
6506 got_fileindex_free(*fileindex);
6509 lock_worktree(worktree, LOCK_SH);
6514 const struct got_error *
6515 got_worktree_rebase_continue(struct got_object_id **commit_id,
6516 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6517 struct got_reference **branch, struct got_fileindex **fileindex,
6518 struct got_worktree *worktree, struct got_repository *repo)
6520 const struct got_error *err;
6521 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6522 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6523 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6524 char *fileindex_path = NULL;
6525 int have_staged_files = 0;
6528 *new_base_branch = NULL;
6533 err = lock_worktree(worktree, LOCK_EX);
6537 err = open_fileindex(fileindex, &fileindex_path, worktree);
6541 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6542 &have_staged_files);
6543 if (err && err->code != GOT_ERR_CANCELLED)
6545 if (have_staged_files) {
6546 err = got_error(GOT_ERR_STAGED_PATHS);
6550 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6554 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6558 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6562 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6566 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6570 err = got_ref_open(branch, repo,
6571 got_ref_get_symref_target(branch_ref), 0);
6575 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6579 err = got_ref_resolve(commit_id, repo, commit_ref);
6583 err = got_ref_open(new_base_branch, repo,
6584 new_base_branch_ref_name, 0);
6588 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6592 free(commit_ref_name);
6593 free(branch_ref_name);
6594 free(fileindex_path);
6596 got_ref_close(commit_ref);
6598 got_ref_close(branch_ref);
6603 got_ref_close(*tmp_branch);
6606 if (*new_base_branch) {
6607 got_ref_close(*new_base_branch);
6608 *new_base_branch = NULL;
6611 got_ref_close(*branch);
6615 got_fileindex_free(*fileindex);
6618 lock_worktree(worktree, LOCK_SH);
6623 const struct got_error *
6624 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6626 const struct got_error *err;
6627 char *tmp_branch_name = NULL;
6629 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6633 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6634 free(tmp_branch_name);
6638 static const struct got_error *
6639 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6640 const char *diff_path, char **logmsg, void *arg)
6646 static const struct got_error *
6647 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6648 const char *path, struct got_object_id *blob_id,
6649 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6650 int dirfd, const char *de_name)
6655 struct collect_merged_paths_arg {
6656 got_worktree_checkout_cb progress_cb;
6658 struct got_pathlist_head *merged_paths;
6661 static const struct got_error *
6662 collect_merged_paths(void *arg, unsigned char status, const char *path)
6664 const struct got_error *err;
6665 struct collect_merged_paths_arg *a = arg;
6667 struct got_pathlist_entry *new;
6669 err = (*a->progress_cb)(a->progress_arg, status, path);
6673 if (status != GOT_STATUS_MERGE &&
6674 status != GOT_STATUS_ADD &&
6675 status != GOT_STATUS_DELETE &&
6676 status != GOT_STATUS_CONFLICT)
6681 return got_error_from_errno("strdup");
6683 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6684 if (err || new == NULL)
6689 static const struct got_error *
6690 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6691 int is_rebase, struct got_repository *repo)
6693 const struct got_error *err;
6694 struct got_reference *commit_ref = NULL;
6696 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6698 if (err->code != GOT_ERR_NOT_REF)
6700 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6703 err = got_ref_write(commit_ref, repo);
6706 } else if (is_rebase) {
6707 struct got_object_id *stored_id;
6710 err = got_ref_resolve(&stored_id, repo, commit_ref);
6713 cmp = got_object_id_cmp(commit_id, stored_id);
6716 err = got_error(GOT_ERR_REBASE_COMMITID);
6722 got_ref_close(commit_ref);
6726 static const struct got_error *
6727 rebase_merge_files(struct got_pathlist_head *merged_paths,
6728 const char *commit_ref_name, struct got_worktree *worktree,
6729 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6730 struct got_object_id *commit_id, struct got_repository *repo,
6731 got_worktree_checkout_cb progress_cb, void *progress_arg,
6732 got_cancel_cb cancel_cb, void *cancel_arg)
6734 const struct got_error *err;
6735 struct got_reference *commit_ref = NULL;
6736 struct collect_merged_paths_arg cmp_arg;
6737 char *fileindex_path;
6739 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6741 err = get_fileindex_path(&fileindex_path, worktree);
6745 cmp_arg.progress_cb = progress_cb;
6746 cmp_arg.progress_arg = progress_arg;
6747 cmp_arg.merged_paths = merged_paths;
6748 err = merge_files(worktree, fileindex, fileindex_path,
6749 parent_commit_id, commit_id, repo, collect_merged_paths,
6750 &cmp_arg, cancel_cb, cancel_arg);
6752 got_ref_close(commit_ref);
6756 const struct got_error *
6757 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6758 struct got_worktree *worktree, struct got_fileindex *fileindex,
6759 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6760 struct got_repository *repo,
6761 got_worktree_checkout_cb progress_cb, void *progress_arg,
6762 got_cancel_cb cancel_cb, void *cancel_arg)
6764 const struct got_error *err;
6765 char *commit_ref_name;
6767 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6771 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6775 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6776 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6777 progress_arg, cancel_cb, cancel_arg);
6779 free(commit_ref_name);
6783 const struct got_error *
6784 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6785 struct got_worktree *worktree, struct got_fileindex *fileindex,
6786 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6787 struct got_repository *repo,
6788 got_worktree_checkout_cb progress_cb, void *progress_arg,
6789 got_cancel_cb cancel_cb, void *cancel_arg)
6791 const struct got_error *err;
6792 char *commit_ref_name;
6794 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6798 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6802 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6803 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6804 progress_arg, cancel_cb, cancel_arg);
6806 free(commit_ref_name);
6810 static const struct got_error *
6811 rebase_commit(struct got_object_id **new_commit_id,
6812 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6813 struct got_worktree *worktree, struct got_fileindex *fileindex,
6814 struct got_reference *tmp_branch, const char *committer,
6815 struct got_commit_object *orig_commit, const char *new_logmsg,
6816 int allow_conflict, struct got_repository *repo)
6818 const struct got_error *err, *sync_err;
6819 struct got_pathlist_head commitable_paths;
6820 struct collect_commitables_arg cc_arg;
6821 char *fileindex_path = NULL;
6822 struct got_reference *head_ref = NULL;
6823 struct got_object_id *head_commit_id = NULL;
6824 char *logmsg = NULL;
6826 memset(&cc_arg, 0, sizeof(cc_arg));
6827 TAILQ_INIT(&commitable_paths);
6828 *new_commit_id = NULL;
6830 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6832 err = get_fileindex_path(&fileindex_path, worktree);
6836 cc_arg.commitable_paths = &commitable_paths;
6837 cc_arg.worktree = worktree;
6839 cc_arg.have_staged_files = 0;
6840 cc_arg.commit_conflicts = allow_conflict;
6842 * If possible get the status of individual files directly to
6843 * avoid crawling the entire work tree once per rebased commit.
6845 * Ideally, merged_paths would contain a list of commitables
6846 * we could use so we could skip worktree_status() entirely.
6847 * However, we would then need carefully keep track of cumulative
6848 * effects of operations such as file additions and deletions
6849 * in 'got histedit -f' (folding multiple commits into one),
6850 * and this extra complexity is not really worth it.
6853 struct got_pathlist_entry *pe;
6854 TAILQ_FOREACH(pe, merged_paths, entry) {
6855 err = worktree_status(worktree, pe->path, fileindex,
6856 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6862 err = worktree_status(worktree, "", fileindex, repo,
6863 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6868 if (TAILQ_EMPTY(&commitable_paths)) {
6869 /* No-op change; commit will be elided. */
6870 err = got_ref_delete(commit_ref, repo);
6873 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6877 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6881 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6886 logmsg = strdup(new_logmsg);
6887 if (logmsg == NULL) {
6888 err = got_error_from_errno("strdup");
6892 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6897 /* NB: commit_worktree will call free(logmsg) */
6898 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6899 NULL, worktree, got_object_commit_get_author(orig_commit),
6900 committer ? committer :
6901 got_object_commit_get_committer(orig_commit), NULL,
6902 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6906 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6910 err = got_ref_delete(commit_ref, repo);
6914 err = update_fileindex_after_commit(worktree, &commitable_paths,
6915 *new_commit_id, fileindex, 0);
6916 sync_err = sync_fileindex(fileindex, fileindex_path);
6917 if (sync_err && err == NULL)
6920 free(fileindex_path);
6921 free(head_commit_id);
6923 got_ref_close(head_ref);
6925 free(*new_commit_id);
6926 *new_commit_id = NULL;
6931 const struct got_error *
6932 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6933 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6934 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6935 const char *committer, struct got_commit_object *orig_commit,
6936 struct got_object_id *orig_commit_id, int allow_conflict,
6937 struct got_repository *repo)
6939 const struct got_error *err;
6940 char *commit_ref_name;
6941 struct got_reference *commit_ref = NULL;
6942 struct got_object_id *commit_id = NULL;
6944 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6948 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6951 err = got_ref_resolve(&commit_id, repo, commit_ref);
6954 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6955 err = got_error(GOT_ERR_REBASE_COMMITID);
6959 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6960 worktree, fileindex, tmp_branch, committer, orig_commit,
6961 NULL, allow_conflict, repo);
6964 got_ref_close(commit_ref);
6965 free(commit_ref_name);
6970 const struct got_error *
6971 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6972 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6973 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6974 const char *committer, struct got_commit_object *orig_commit,
6975 struct got_object_id *orig_commit_id, const char *new_logmsg,
6976 int allow_conflict, struct got_repository *repo)
6978 const struct got_error *err;
6979 char *commit_ref_name;
6980 struct got_reference *commit_ref = NULL;
6982 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6986 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6990 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6991 worktree, fileindex, tmp_branch, committer, orig_commit,
6992 new_logmsg, allow_conflict, repo);
6995 got_ref_close(commit_ref);
6996 free(commit_ref_name);
7000 const struct got_error *
7001 got_worktree_rebase_postpone(struct got_worktree *worktree,
7002 struct got_fileindex *fileindex)
7005 got_fileindex_free(fileindex);
7006 return lock_worktree(worktree, LOCK_SH);
7009 static const struct got_error *
7010 delete_ref(const char *name, struct got_repository *repo)
7012 const struct got_error *err;
7013 struct got_reference *ref;
7015 err = got_ref_open(&ref, repo, name, 0);
7017 if (err->code == GOT_ERR_NOT_REF)
7022 err = got_ref_delete(ref, repo);
7027 static const struct got_error *
7028 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7030 const struct got_error *err;
7031 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7032 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7034 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7037 err = delete_ref(tmp_branch_name, repo);
7041 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7044 err = delete_ref(new_base_branch_ref_name, repo);
7048 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7051 err = delete_ref(branch_ref_name, repo);
7055 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7058 err = delete_ref(commit_ref_name, repo);
7063 free(tmp_branch_name);
7064 free(new_base_branch_ref_name);
7065 free(branch_ref_name);
7066 free(commit_ref_name);
7070 static const struct got_error *
7071 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7072 struct got_object_id *new_commit_id, struct got_repository *repo)
7074 const struct got_error *err;
7075 struct got_reference *ref = NULL;
7076 struct got_object_id *old_commit_id = NULL;
7077 const char *branch_name = NULL;
7078 char *new_id_str = NULL;
7079 char *refname = NULL;
7081 branch_name = got_ref_get_name(branch);
7082 if (strncmp(branch_name, "refs/heads/", 11) != 0)
7083 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7086 err = got_object_id_str(&new_id_str, new_commit_id);
7090 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7091 new_id_str) == -1) {
7092 err = got_error_from_errno("asprintf");
7096 err = got_ref_resolve(&old_commit_id, repo, branch);
7100 err = got_ref_alloc(&ref, refname, old_commit_id);
7104 err = got_ref_write(ref, repo);
7108 free(old_commit_id);
7114 const struct got_error *
7115 got_worktree_rebase_complete(struct got_worktree *worktree,
7116 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7117 struct got_reference *rebased_branch, struct got_repository *repo,
7120 const struct got_error *err, *unlockerr, *sync_err;
7121 struct got_object_id *new_head_commit_id = NULL;
7122 char *fileindex_path = NULL;
7124 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7128 if (create_backup) {
7129 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7130 rebased_branch, new_head_commit_id, repo);
7135 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7139 err = got_ref_write(rebased_branch, repo);
7143 err = got_worktree_set_head_ref(worktree, rebased_branch);
7147 err = delete_rebase_refs(worktree, repo);
7151 err = get_fileindex_path(&fileindex_path, worktree);
7154 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7155 sync_err = sync_fileindex(fileindex, fileindex_path);
7156 if (sync_err && err == NULL)
7159 got_fileindex_free(fileindex);
7160 free(fileindex_path);
7161 free(new_head_commit_id);
7162 unlockerr = lock_worktree(worktree, LOCK_SH);
7163 if (unlockerr && err == NULL)
7168 const struct got_error *
7169 got_worktree_rebase_abort(struct got_worktree *worktree,
7170 struct got_fileindex *fileindex, struct got_repository *repo,
7171 struct got_reference *new_base_branch,
7172 got_worktree_checkout_cb progress_cb, void *progress_arg)
7174 const struct got_error *err, *unlockerr, *sync_err;
7175 struct got_reference *resolved = NULL;
7176 struct got_object_id *commit_id = NULL;
7177 struct got_commit_object *commit = NULL;
7178 char *fileindex_path = NULL;
7179 struct revert_file_args rfa;
7180 struct got_object_id *tree_id = NULL;
7182 err = lock_worktree(worktree, LOCK_EX);
7186 err = got_object_open_as_commit(&commit, repo,
7187 worktree->base_commit_id);
7191 err = got_ref_open(&resolved, repo,
7192 got_ref_get_symref_target(new_base_branch), 0);
7196 err = got_worktree_set_head_ref(worktree, resolved);
7201 * XXX commits to the base branch could have happened while
7202 * we were busy rebasing; should we store the original commit ID
7203 * when rebase begins and read it back here?
7205 err = got_ref_resolve(&commit_id, repo, resolved);
7209 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7213 err = got_object_id_by_path(&tree_id, repo, commit,
7214 worktree->path_prefix);
7218 err = delete_rebase_refs(worktree, repo);
7222 err = get_fileindex_path(&fileindex_path, worktree);
7226 rfa.worktree = worktree;
7227 rfa.fileindex = fileindex;
7228 rfa.progress_cb = progress_cb;
7229 rfa.progress_arg = progress_arg;
7230 rfa.patch_cb = NULL;
7231 rfa.patch_arg = NULL;
7233 rfa.unlink_added_files = 0;
7234 err = worktree_status(worktree, "", fileindex, repo,
7235 revert_file, &rfa, NULL, NULL, 1, 0);
7239 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7240 repo, progress_cb, progress_arg, NULL, NULL);
7242 sync_err = sync_fileindex(fileindex, fileindex_path);
7243 if (sync_err && err == NULL)
7246 got_ref_close(resolved);
7250 got_object_commit_close(commit);
7252 got_fileindex_free(fileindex);
7253 free(fileindex_path);
7255 unlockerr = lock_worktree(worktree, LOCK_SH);
7256 if (unlockerr && err == NULL)
7261 const struct got_error *
7262 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7263 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7264 struct got_fileindex **fileindex, struct got_worktree *worktree,
7265 struct got_repository *repo)
7267 const struct got_error *err = NULL;
7268 char *tmp_branch_name = NULL;
7269 char *branch_ref_name = NULL;
7270 char *base_commit_ref_name = NULL;
7271 char *fileindex_path = NULL;
7272 struct check_rebase_ok_arg ok_arg;
7273 struct got_reference *wt_branch = NULL;
7274 struct got_reference *base_commit_ref = NULL;
7278 *base_commit_id = NULL;
7281 err = lock_worktree(worktree, LOCK_EX);
7285 err = open_fileindex(fileindex, &fileindex_path, worktree);
7289 ok_arg.worktree = worktree;
7291 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7296 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7300 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7304 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7309 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7314 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7318 err = got_ref_write(*branch_ref, repo);
7322 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7323 worktree->base_commit_id);
7326 err = got_ref_write(base_commit_ref, repo);
7329 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7330 if (*base_commit_id == NULL) {
7331 err = got_error_from_errno("got_object_id_dup");
7335 err = got_ref_alloc(tmp_branch, tmp_branch_name,
7336 worktree->base_commit_id);
7339 err = got_ref_write(*tmp_branch, repo);
7343 err = got_worktree_set_head_ref(worktree, *tmp_branch);
7347 free(fileindex_path);
7348 free(tmp_branch_name);
7349 free(branch_ref_name);
7350 free(base_commit_ref_name);
7352 got_ref_close(wt_branch);
7355 got_ref_close(*branch_ref);
7359 got_ref_close(*tmp_branch);
7362 free(*base_commit_id);
7364 got_fileindex_free(*fileindex);
7367 lock_worktree(worktree, LOCK_SH);
7372 const struct got_error *
7373 got_worktree_histedit_postpone(struct got_worktree *worktree,
7374 struct got_fileindex *fileindex)
7377 got_fileindex_free(fileindex);
7378 return lock_worktree(worktree, LOCK_SH);
7381 const struct got_error *
7382 got_worktree_histedit_in_progress(int *in_progress,
7383 struct got_worktree *worktree)
7385 const struct got_error *err;
7386 char *tmp_branch_name = NULL;
7388 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7392 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7393 free(tmp_branch_name);
7397 const struct got_error *
7398 got_worktree_histedit_continue(struct got_object_id **commit_id,
7399 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7400 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7401 struct got_worktree *worktree, struct got_repository *repo)
7403 const struct got_error *err;
7404 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7405 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7406 struct got_reference *commit_ref = NULL;
7407 struct got_reference *base_commit_ref = NULL;
7408 char *fileindex_path = NULL;
7409 int have_staged_files = 0;
7413 *base_commit_id = NULL;
7416 err = lock_worktree(worktree, LOCK_EX);
7420 err = open_fileindex(fileindex, &fileindex_path, worktree);
7424 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7425 &have_staged_files);
7426 if (err && err->code != GOT_ERR_CANCELLED)
7428 if (have_staged_files) {
7429 err = got_error(GOT_ERR_STAGED_PATHS);
7433 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7437 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7441 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7445 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7450 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7454 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7457 err = got_ref_resolve(commit_id, repo, commit_ref);
7461 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7464 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7468 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7472 free(commit_ref_name);
7473 free(branch_ref_name);
7474 free(fileindex_path);
7476 got_ref_close(commit_ref);
7477 if (base_commit_ref)
7478 got_ref_close(base_commit_ref);
7482 free(*base_commit_id);
7483 *base_commit_id = NULL;
7485 got_ref_close(*tmp_branch);
7489 got_fileindex_free(*fileindex);
7492 lock_worktree(worktree, LOCK_EX);
7497 static const struct got_error *
7498 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7500 const struct got_error *err;
7501 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7502 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7504 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7507 err = delete_ref(tmp_branch_name, repo);
7511 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7515 err = delete_ref(base_commit_ref_name, repo);
7519 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7522 err = delete_ref(branch_ref_name, repo);
7526 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7529 err = delete_ref(commit_ref_name, repo);
7533 free(tmp_branch_name);
7534 free(base_commit_ref_name);
7535 free(branch_ref_name);
7536 free(commit_ref_name);
7540 const struct got_error *
7541 got_worktree_histedit_abort(struct got_worktree *worktree,
7542 struct got_fileindex *fileindex, struct got_repository *repo,
7543 struct got_reference *branch, struct got_object_id *base_commit_id,
7544 got_worktree_checkout_cb progress_cb, void *progress_arg)
7546 const struct got_error *err, *unlockerr, *sync_err;
7547 struct got_reference *resolved = NULL;
7548 char *fileindex_path = NULL;
7549 struct got_commit_object *commit = NULL;
7550 struct got_object_id *tree_id = NULL;
7551 struct revert_file_args rfa;
7553 err = lock_worktree(worktree, LOCK_EX);
7557 err = got_object_open_as_commit(&commit, repo,
7558 worktree->base_commit_id);
7562 err = got_ref_open(&resolved, repo,
7563 got_ref_get_symref_target(branch), 0);
7567 err = got_worktree_set_head_ref(worktree, resolved);
7571 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7575 err = got_object_id_by_path(&tree_id, repo, commit,
7576 worktree->path_prefix);
7580 err = delete_histedit_refs(worktree, repo);
7584 err = get_fileindex_path(&fileindex_path, worktree);
7588 rfa.worktree = worktree;
7589 rfa.fileindex = fileindex;
7590 rfa.progress_cb = progress_cb;
7591 rfa.progress_arg = progress_arg;
7592 rfa.patch_cb = NULL;
7593 rfa.patch_arg = NULL;
7595 rfa.unlink_added_files = 0;
7596 err = worktree_status(worktree, "", fileindex, repo,
7597 revert_file, &rfa, NULL, NULL, 1, 0);
7601 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7602 repo, progress_cb, progress_arg, NULL, NULL);
7604 sync_err = sync_fileindex(fileindex, fileindex_path);
7605 if (sync_err && err == NULL)
7608 got_ref_close(resolved);
7610 free(fileindex_path);
7612 unlockerr = lock_worktree(worktree, LOCK_SH);
7613 if (unlockerr && err == NULL)
7618 const struct got_error *
7619 got_worktree_histedit_complete(struct got_worktree *worktree,
7620 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7621 struct got_reference *edited_branch, struct got_repository *repo)
7623 const struct got_error *err, *unlockerr, *sync_err;
7624 struct got_object_id *new_head_commit_id = NULL;
7625 struct got_reference *resolved = NULL;
7626 char *fileindex_path = NULL;
7628 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7632 err = got_ref_open(&resolved, repo,
7633 got_ref_get_symref_target(edited_branch), 0);
7637 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7638 resolved, new_head_commit_id, repo);
7642 err = got_ref_change_ref(resolved, new_head_commit_id);
7646 err = got_ref_write(resolved, repo);
7650 err = got_worktree_set_head_ref(worktree, resolved);
7654 err = delete_histedit_refs(worktree, repo);
7658 err = get_fileindex_path(&fileindex_path, worktree);
7661 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7662 sync_err = sync_fileindex(fileindex, fileindex_path);
7663 if (sync_err && err == NULL)
7666 got_fileindex_free(fileindex);
7667 free(fileindex_path);
7668 free(new_head_commit_id);
7669 unlockerr = lock_worktree(worktree, LOCK_SH);
7670 if (unlockerr && err == NULL)
7675 const struct got_error *
7676 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7677 struct got_object_id *commit_id, struct got_repository *repo)
7679 const struct got_error *err;
7680 char *commit_ref_name;
7682 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7686 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7690 err = delete_ref(commit_ref_name, repo);
7692 free(commit_ref_name);
7696 const struct got_error *
7697 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7698 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7699 struct got_worktree *worktree, const char *refname,
7700 struct got_repository *repo)
7702 const struct got_error *err = NULL;
7703 char *fileindex_path = NULL;
7704 struct check_rebase_ok_arg ok_arg;
7708 *base_branch_ref = NULL;
7710 err = lock_worktree(worktree, LOCK_EX);
7714 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7715 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7716 "cannot integrate a branch into itself; "
7717 "update -b or different branch name required");
7721 err = open_fileindex(fileindex, &fileindex_path, worktree);
7725 /* Preconditions are the same as for rebase. */
7726 ok_arg.worktree = worktree;
7728 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7733 err = got_ref_open(branch_ref, repo, refname, 1);
7737 err = got_ref_open(base_branch_ref, repo,
7738 got_worktree_get_head_ref_name(worktree), 1);
7742 got_ref_close(*branch_ref);
7745 if (*base_branch_ref) {
7746 got_ref_close(*base_branch_ref);
7747 *base_branch_ref = NULL;
7750 got_fileindex_free(*fileindex);
7753 lock_worktree(worktree, LOCK_SH);
7758 const struct got_error *
7759 got_worktree_integrate_continue(struct got_worktree *worktree,
7760 struct got_fileindex *fileindex, struct got_repository *repo,
7761 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7762 got_worktree_checkout_cb progress_cb, void *progress_arg,
7763 got_cancel_cb cancel_cb, void *cancel_arg)
7765 const struct got_error *err = NULL, *sync_err, *unlockerr;
7766 char *fileindex_path = NULL;
7767 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7768 struct got_commit_object *commit = NULL;
7770 err = get_fileindex_path(&fileindex_path, worktree);
7774 err = got_ref_resolve(&commit_id, repo, branch_ref);
7778 err = got_object_open_as_commit(&commit, repo, commit_id);
7782 err = got_object_id_by_path(&tree_id, repo, commit,
7783 worktree->path_prefix);
7787 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7791 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7792 progress_cb, progress_arg, cancel_cb, cancel_arg);
7796 err = got_ref_change_ref(base_branch_ref, commit_id);
7800 err = got_ref_write(base_branch_ref, repo);
7804 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7806 sync_err = sync_fileindex(fileindex, fileindex_path);
7807 if (sync_err && err == NULL)
7811 unlockerr = got_ref_unlock(branch_ref);
7812 if (unlockerr && err == NULL)
7814 got_ref_close(branch_ref);
7816 unlockerr = got_ref_unlock(base_branch_ref);
7817 if (unlockerr && err == NULL)
7819 got_ref_close(base_branch_ref);
7821 got_fileindex_free(fileindex);
7822 free(fileindex_path);
7825 got_object_commit_close(commit);
7827 unlockerr = lock_worktree(worktree, LOCK_SH);
7828 if (unlockerr && err == NULL)
7833 const struct got_error *
7834 got_worktree_integrate_abort(struct got_worktree *worktree,
7835 struct got_fileindex *fileindex, struct got_repository *repo,
7836 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7838 const struct got_error *err = NULL, *unlockerr = NULL;
7840 got_fileindex_free(fileindex);
7842 err = lock_worktree(worktree, LOCK_SH);
7844 unlockerr = got_ref_unlock(branch_ref);
7845 if (unlockerr && err == NULL)
7847 got_ref_close(branch_ref);
7849 unlockerr = got_ref_unlock(base_branch_ref);
7850 if (unlockerr && err == NULL)
7852 got_ref_close(base_branch_ref);
7857 const struct got_error *
7858 got_worktree_merge_postpone(struct got_worktree *worktree,
7859 struct got_fileindex *fileindex)
7861 const struct got_error *err, *sync_err;
7862 char *fileindex_path = NULL;
7864 err = get_fileindex_path(&fileindex_path, worktree);
7868 sync_err = sync_fileindex(fileindex, fileindex_path);
7870 err = lock_worktree(worktree, LOCK_SH);
7871 if (sync_err && err == NULL)
7874 got_fileindex_free(fileindex);
7875 free(fileindex_path);
7879 static const struct got_error *
7880 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7882 const struct got_error *err;
7883 char *branch_refname = NULL, *commit_refname = NULL;
7885 err = get_merge_branch_ref_name(&branch_refname, worktree);
7888 err = delete_ref(branch_refname, repo);
7892 err = get_merge_commit_ref_name(&commit_refname, worktree);
7895 err = delete_ref(commit_refname, repo);
7900 free(branch_refname);
7901 free(commit_refname);
7905 struct merge_commit_msg_arg {
7906 struct got_worktree *worktree;
7907 const char *branch_name;
7910 static const struct got_error *
7911 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
7912 const char *diff_path, char **logmsg, void *arg)
7914 struct merge_commit_msg_arg *a = arg;
7916 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7917 got_worktree_get_head_ref_name(a->worktree)) == -1)
7918 return got_error_from_errno("asprintf");
7924 const struct got_error *
7925 got_worktree_merge_branch(struct got_worktree *worktree,
7926 struct got_fileindex *fileindex,
7927 struct got_object_id *yca_commit_id,
7928 struct got_object_id *branch_tip,
7929 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7930 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7932 const struct got_error *err;
7933 char *fileindex_path = NULL;
7935 err = get_fileindex_path(&fileindex_path, worktree);
7939 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7944 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7945 branch_tip, repo, progress_cb, progress_arg,
7946 cancel_cb, cancel_arg);
7948 free(fileindex_path);
7952 const struct got_error *
7953 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7954 struct got_worktree *worktree, struct got_fileindex *fileindex,
7955 const char *author, const char *committer, int allow_bad_symlinks,
7956 struct got_object_id *branch_tip, const char *branch_name,
7957 int allow_conflict, struct got_repository *repo,
7958 got_worktree_status_cb status_cb, void *status_arg)
7961 const struct got_error *err = NULL, *sync_err;
7962 struct got_pathlist_head commitable_paths;
7963 struct collect_commitables_arg cc_arg;
7964 struct got_pathlist_entry *pe;
7965 struct got_reference *head_ref = NULL;
7966 struct got_object_id *head_commit_id = NULL;
7967 int have_staged_files = 0;
7968 struct merge_commit_msg_arg mcm_arg;
7969 char *fileindex_path = NULL;
7971 memset(&cc_arg, 0, sizeof(cc_arg));
7972 *new_commit_id = NULL;
7974 TAILQ_INIT(&commitable_paths);
7976 err = get_fileindex_path(&fileindex_path, worktree);
7980 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7984 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7988 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7989 &have_staged_files);
7990 if (err && err->code != GOT_ERR_CANCELLED)
7992 if (have_staged_files) {
7993 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7997 cc_arg.commitable_paths = &commitable_paths;
7998 cc_arg.worktree = worktree;
7999 cc_arg.fileindex = fileindex;
8001 cc_arg.have_staged_files = have_staged_files;
8002 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
8003 cc_arg.commit_conflicts = allow_conflict;
8004 err = worktree_status(worktree, "", fileindex, repo,
8005 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8009 if (TAILQ_EMPTY(&commitable_paths)) {
8010 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
8011 "merge of %s cannot proceed", branch_name);
8015 TAILQ_FOREACH(pe, &commitable_paths, entry) {
8016 struct got_commitable *ct = pe->data;
8017 const char *ct_path = ct->in_repo_path;
8019 while (ct_path[0] == '/')
8021 err = check_out_of_date(ct_path, ct->status,
8022 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
8023 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
8029 mcm_arg.worktree = worktree;
8030 mcm_arg.branch_name = branch_name;
8031 err = commit_worktree(new_commit_id, &commitable_paths,
8032 head_commit_id, branch_tip, worktree, author, committer, NULL,
8033 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8037 err = update_fileindex_after_commit(worktree, &commitable_paths,
8038 *new_commit_id, fileindex, have_staged_files);
8039 sync_err = sync_fileindex(fileindex, fileindex_path);
8040 if (sync_err && err == NULL)
8043 TAILQ_FOREACH(pe, &commitable_paths, entry) {
8044 struct got_commitable *ct = pe->data;
8046 free_commitable(ct);
8048 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8049 free(fileindex_path);
8053 const struct got_error *
8054 got_worktree_merge_complete(struct got_worktree *worktree,
8055 struct got_fileindex *fileindex, struct got_repository *repo)
8057 const struct got_error *err, *unlockerr, *sync_err;
8058 char *fileindex_path = NULL;
8060 err = delete_merge_refs(worktree, repo);
8064 err = get_fileindex_path(&fileindex_path, worktree);
8067 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8068 sync_err = sync_fileindex(fileindex, fileindex_path);
8069 if (sync_err && err == NULL)
8072 got_fileindex_free(fileindex);
8073 free(fileindex_path);
8074 unlockerr = lock_worktree(worktree, LOCK_SH);
8075 if (unlockerr && err == NULL)
8080 const struct got_error *
8081 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8082 struct got_repository *repo)
8084 const struct got_error *err;
8085 char *branch_refname = NULL;
8086 struct got_reference *branch_ref = NULL;
8090 err = get_merge_branch_ref_name(&branch_refname, worktree);
8093 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8094 free(branch_refname);
8096 if (err->code != GOT_ERR_NOT_REF)
8104 const struct got_error *got_worktree_merge_prepare(
8105 struct got_fileindex **fileindex, struct got_worktree *worktree,
8106 struct got_reference *branch, struct got_repository *repo)
8108 const struct got_error *err = NULL;
8109 char *fileindex_path = NULL;
8110 char *branch_refname = NULL, *commit_refname = NULL;
8111 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
8112 struct got_reference *commit_ref = NULL;
8113 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
8114 struct check_rebase_ok_arg ok_arg;
8118 err = lock_worktree(worktree, LOCK_EX);
8122 err = open_fileindex(fileindex, &fileindex_path, worktree);
8126 /* Preconditions are the same as for rebase. */
8127 ok_arg.worktree = worktree;
8129 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8134 err = get_merge_branch_ref_name(&branch_refname, worktree);
8138 err = get_merge_commit_ref_name(&commit_refname, worktree);
8142 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8147 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8151 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8152 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8156 err = got_ref_resolve(&branch_tip, repo, branch);
8160 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8163 err = got_ref_write(branch_ref, repo);
8167 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8170 err = got_ref_write(commit_ref, repo);
8175 free(branch_refname);
8176 free(commit_refname);
8177 free(fileindex_path);
8179 got_ref_close(branch_ref);
8181 got_ref_close(commit_ref);
8183 got_ref_close(wt_branch);
8184 free(wt_branch_tip);
8187 got_fileindex_free(*fileindex);
8190 lock_worktree(worktree, LOCK_SH);
8195 const struct got_error *
8196 got_worktree_merge_continue(char **branch_name,
8197 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8198 struct got_worktree *worktree, struct got_repository *repo)
8200 const struct got_error *err;
8201 char *commit_refname = NULL, *branch_refname = NULL;
8202 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8203 char *fileindex_path = NULL;
8204 int have_staged_files = 0;
8206 *branch_name = NULL;
8210 err = lock_worktree(worktree, LOCK_EX);
8214 err = open_fileindex(fileindex, &fileindex_path, worktree);
8218 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8219 &have_staged_files);
8220 if (err && err->code != GOT_ERR_CANCELLED)
8222 if (have_staged_files) {
8223 err = got_error(GOT_ERR_STAGED_PATHS);
8227 err = get_merge_branch_ref_name(&branch_refname, worktree);
8231 err = get_merge_commit_ref_name(&commit_refname, worktree);
8235 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8239 if (!got_ref_is_symbolic(branch_ref)) {
8240 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8241 "%s is not a symbolic reference",
8242 got_ref_get_name(branch_ref));
8245 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8246 if (*branch_name == NULL) {
8247 err = got_error_from_errno("strdup");
8251 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8255 err = got_ref_resolve(branch_tip, repo, commit_ref);
8259 free(commit_refname);
8260 free(branch_refname);
8261 free(fileindex_path);
8263 got_ref_close(commit_ref);
8265 got_ref_close(branch_ref);
8269 *branch_name = NULL;
8274 got_fileindex_free(*fileindex);
8277 lock_worktree(worktree, LOCK_SH);
8282 const struct got_error *
8283 got_worktree_merge_abort(struct got_worktree *worktree,
8284 struct got_fileindex *fileindex, struct got_repository *repo,
8285 got_worktree_checkout_cb progress_cb, void *progress_arg)
8287 const struct got_error *err, *unlockerr, *sync_err;
8288 struct got_object_id *commit_id = NULL;
8289 struct got_commit_object *commit = NULL;
8290 char *fileindex_path = NULL;
8291 struct revert_file_args rfa;
8292 struct got_object_id *tree_id = NULL;
8294 err = got_object_open_as_commit(&commit, repo,
8295 worktree->base_commit_id);
8299 err = got_object_id_by_path(&tree_id, repo, commit,
8300 worktree->path_prefix);
8304 err = delete_merge_refs(worktree, repo);
8308 err = get_fileindex_path(&fileindex_path, worktree);
8312 rfa.worktree = worktree;
8313 rfa.fileindex = fileindex;
8314 rfa.progress_cb = progress_cb;
8315 rfa.progress_arg = progress_arg;
8316 rfa.patch_cb = NULL;
8317 rfa.patch_arg = NULL;
8319 rfa.unlink_added_files = 1;
8320 err = worktree_status(worktree, "", fileindex, repo,
8321 revert_file, &rfa, NULL, NULL, 1, 0);
8325 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8326 repo, progress_cb, progress_arg, NULL, NULL);
8328 sync_err = sync_fileindex(fileindex, fileindex_path);
8329 if (sync_err && err == NULL)
8335 got_object_commit_close(commit);
8337 got_fileindex_free(fileindex);
8338 free(fileindex_path);
8340 unlockerr = lock_worktree(worktree, LOCK_SH);
8341 if (unlockerr && err == NULL)
8346 struct check_stage_ok_arg {
8347 struct got_object_id *head_commit_id;
8348 struct got_worktree *worktree;
8349 struct got_fileindex *fileindex;
8350 struct got_repository *repo;
8354 static const struct got_error *
8355 check_stage_ok(void *arg, unsigned char status,
8356 unsigned char staged_status, const char *relpath,
8357 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8358 struct got_object_id *commit_id, int dirfd, const char *de_name)
8360 struct check_stage_ok_arg *a = arg;
8361 const struct got_error *err = NULL;
8362 struct got_fileindex_entry *ie;
8363 struct got_object_id base_commit_id;
8364 struct got_object_id *base_commit_idp = NULL;
8365 char *in_repo_path = NULL, *p;
8367 if (status == GOT_STATUS_UNVERSIONED ||
8368 status == GOT_STATUS_NO_CHANGE)
8370 if (status == GOT_STATUS_NONEXISTENT)
8371 return got_error_set_errno(ENOENT, relpath);
8373 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8375 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8377 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8378 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8380 return got_error_from_errno("asprintf");
8382 if (got_fileindex_entry_has_commit(ie)) {
8383 base_commit_idp = got_fileindex_entry_get_commit_id(
8384 &base_commit_id, ie);
8387 if (status == GOT_STATUS_CONFLICT) {
8388 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8390 } else if (status != GOT_STATUS_ADD &&
8391 status != GOT_STATUS_MODIFY &&
8392 status != GOT_STATUS_DELETE) {
8393 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8397 a->have_changes = 1;
8402 err = check_out_of_date(p, status, staged_status,
8403 blob_id, base_commit_idp, a->head_commit_id, a->repo,
8404 GOT_ERR_STAGE_OUT_OF_DATE);
8410 struct stage_path_arg {
8411 struct got_worktree *worktree;
8412 struct got_fileindex *fileindex;
8413 struct got_repository *repo;
8414 got_worktree_status_cb status_cb;
8416 got_worktree_patch_cb patch_cb;
8418 int staged_something;
8419 int allow_bad_symlinks;
8422 static const struct got_error *
8423 stage_path(void *arg, unsigned char status,
8424 unsigned char staged_status, const char *relpath,
8425 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8426 struct got_object_id *commit_id, int dirfd, const char *de_name)
8428 struct stage_path_arg *a = arg;
8429 const struct got_error *err = NULL;
8430 struct got_fileindex_entry *ie;
8431 char *ondisk_path = NULL, *path_content = NULL;
8433 struct got_object_id *new_staged_blob_id = NULL;
8436 if (status == GOT_STATUS_UNVERSIONED)
8439 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8441 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8443 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8445 return got_error_from_errno("asprintf");
8448 case GOT_STATUS_ADD:
8449 case GOT_STATUS_MODIFY:
8450 /* XXX could sb.st_mode be passed in by our caller? */
8451 if (lstat(ondisk_path, &sb) == -1) {
8452 err = got_error_from_errno2("lstat", ondisk_path);
8456 if (status == GOT_STATUS_ADD) {
8457 int choice = GOT_PATCH_CHOICE_NONE;
8458 err = (*a->patch_cb)(&choice, a->patch_arg,
8459 status, ie->path, NULL, 1, 1);
8462 if (choice != GOT_PATCH_CHOICE_YES)
8465 err = create_patched_content(&path_content, 0,
8466 staged_blob_id ? staged_blob_id : blob_id,
8467 ondisk_path, dirfd, de_name, ie->path,
8468 a->repo, a->patch_cb, a->patch_arg);
8469 if (err || path_content == NULL)
8473 err = got_object_blob_create(&new_staged_blob_id,
8474 path_content ? path_content : ondisk_path, a->repo);
8477 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8478 SHA1_DIGEST_LENGTH);
8479 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8480 stage = GOT_FILEIDX_STAGE_ADD;
8482 stage = GOT_FILEIDX_STAGE_MODIFY;
8483 got_fileindex_entry_stage_set(ie, stage);
8484 if (S_ISLNK(sb.st_mode)) {
8485 int is_bad_symlink = 0;
8486 if (!a->allow_bad_symlinks) {
8487 char target_path[PATH_MAX];
8489 target_len = readlink(ondisk_path, target_path,
8490 sizeof(target_path));
8491 if (target_len == -1) {
8492 err = got_error_from_errno2("readlink",
8496 err = is_bad_symlink_target(&is_bad_symlink,
8497 target_path, target_len, ondisk_path,
8498 a->worktree->root_path);
8501 if (is_bad_symlink) {
8502 err = got_error_path(ondisk_path,
8503 GOT_ERR_BAD_SYMLINK);
8508 got_fileindex_entry_staged_filetype_set(ie,
8509 GOT_FILEIDX_MODE_BAD_SYMLINK);
8511 got_fileindex_entry_staged_filetype_set(ie,
8512 GOT_FILEIDX_MODE_SYMLINK);
8514 got_fileindex_entry_staged_filetype_set(ie,
8515 GOT_FILEIDX_MODE_REGULAR_FILE);
8517 a->staged_something = 1;
8518 if (a->status_cb == NULL)
8520 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8521 get_staged_status(ie), relpath, blob_id,
8522 new_staged_blob_id, NULL, dirfd, de_name);
8526 * When staging the reverse of the staged diff,
8527 * implicitly unstage the file.
8529 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8530 sizeof(ie->blob_sha1)) == 0) {
8531 got_fileindex_entry_stage_set(ie,
8532 GOT_FILEIDX_STAGE_NONE);
8535 case GOT_STATUS_DELETE:
8536 if (staged_status == GOT_STATUS_DELETE)
8539 int choice = GOT_PATCH_CHOICE_NONE;
8540 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8541 ie->path, NULL, 1, 1);
8544 if (choice == GOT_PATCH_CHOICE_NO)
8546 if (choice != GOT_PATCH_CHOICE_YES) {
8547 err = got_error(GOT_ERR_PATCH_CHOICE);
8551 stage = GOT_FILEIDX_STAGE_DELETE;
8552 got_fileindex_entry_stage_set(ie, stage);
8553 a->staged_something = 1;
8554 if (a->status_cb == NULL)
8556 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8557 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8560 case GOT_STATUS_NO_CHANGE:
8562 case GOT_STATUS_CONFLICT:
8563 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8565 case GOT_STATUS_NONEXISTENT:
8566 err = got_error_set_errno(ENOENT, relpath);
8569 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8573 if (path_content && unlink(path_content) == -1 && err == NULL)
8574 err = got_error_from_errno2("unlink", path_content);
8577 free(new_staged_blob_id);
8581 const struct got_error *
8582 got_worktree_stage(struct got_worktree *worktree,
8583 struct got_pathlist_head *paths,
8584 got_worktree_status_cb status_cb, void *status_arg,
8585 got_worktree_patch_cb patch_cb, void *patch_arg,
8586 int allow_bad_symlinks, struct got_repository *repo)
8588 const struct got_error *err = NULL, *sync_err, *unlockerr;
8589 struct got_pathlist_entry *pe;
8590 struct got_fileindex *fileindex = NULL;
8591 char *fileindex_path = NULL;
8592 struct got_reference *head_ref = NULL;
8593 struct got_object_id *head_commit_id = NULL;
8594 struct check_stage_ok_arg oka;
8595 struct stage_path_arg spa;
8597 err = lock_worktree(worktree, LOCK_EX);
8601 err = got_ref_open(&head_ref, repo,
8602 got_worktree_get_head_ref_name(worktree), 0);
8605 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8608 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8612 /* Check pre-conditions before staging anything. */
8613 oka.head_commit_id = head_commit_id;
8614 oka.worktree = worktree;
8615 oka.fileindex = fileindex;
8617 oka.have_changes = 0;
8618 TAILQ_FOREACH(pe, paths, entry) {
8619 err = worktree_status(worktree, pe->path, fileindex, repo,
8620 check_stage_ok, &oka, NULL, NULL, 1, 0);
8624 if (!oka.have_changes) {
8625 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8629 spa.worktree = worktree;
8630 spa.fileindex = fileindex;
8632 spa.patch_cb = patch_cb;
8633 spa.patch_arg = patch_arg;
8634 spa.status_cb = status_cb;
8635 spa.status_arg = status_arg;
8636 spa.staged_something = 0;
8637 spa.allow_bad_symlinks = allow_bad_symlinks;
8638 TAILQ_FOREACH(pe, paths, entry) {
8639 err = worktree_status(worktree, pe->path, fileindex, repo,
8640 stage_path, &spa, NULL, NULL, 1, 0);
8644 if (!spa.staged_something) {
8645 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8649 sync_err = sync_fileindex(fileindex, fileindex_path);
8650 if (sync_err && err == NULL)
8654 got_ref_close(head_ref);
8655 free(head_commit_id);
8656 free(fileindex_path);
8658 got_fileindex_free(fileindex);
8659 unlockerr = lock_worktree(worktree, LOCK_SH);
8660 if (unlockerr && err == NULL)
8665 struct unstage_path_arg {
8666 struct got_worktree *worktree;
8667 struct got_fileindex *fileindex;
8668 struct got_repository *repo;
8669 got_worktree_checkout_cb progress_cb;
8671 got_worktree_patch_cb patch_cb;
8675 static const struct got_error *
8676 create_unstaged_content(char **path_unstaged_content,
8677 char **path_new_staged_content, struct got_object_id *blob_id,
8678 struct got_object_id *staged_blob_id, const char *relpath,
8679 struct got_repository *repo,
8680 got_worktree_patch_cb patch_cb, void *patch_arg)
8682 const struct got_error *err, *free_err;
8683 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8684 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8685 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8686 struct got_diffreg_result *diffreg_result = NULL;
8687 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8688 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8689 int fd1 = -1, fd2 = -1;
8691 *path_unstaged_content = NULL;
8692 *path_new_staged_content = NULL;
8694 err = got_object_id_str(&label1, blob_id);
8698 fd1 = got_opentempfd();
8700 err = got_error_from_errno("got_opentempfd");
8703 fd2 = got_opentempfd();
8705 err = got_error_from_errno("got_opentempfd");
8709 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8713 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
8717 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8721 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8726 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
8730 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8734 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8735 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8739 err = got_opentemp_named(path_unstaged_content, &outfile,
8740 "got-unstaged-content", "");
8743 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8744 "got-new-staged-content", "");
8748 if (fseek(f1, 0L, SEEK_SET) == -1) {
8749 err = got_ferror(f1, GOT_ERR_IO);
8752 if (fseek(f2, 0L, SEEK_SET) == -1) {
8753 err = got_ferror(f2, GOT_ERR_IO);
8756 /* Count the number of actual changes in the diff result. */
8757 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8758 struct diff_chunk_context cc = {};
8759 diff_chunk_context_load_change(&cc, &nchunks_used,
8760 diffreg_result->result, n, 0);
8763 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8765 err = apply_or_reject_change(&choice, &nchunks_used,
8766 diffreg_result->result, n, relpath, f1, f2,
8767 &line_cur1, &line_cur2,
8768 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8771 if (choice == GOT_PATCH_CHOICE_YES)
8774 have_rejected_content = 1;
8775 if (choice == GOT_PATCH_CHOICE_QUIT)
8778 if (have_content || have_rejected_content)
8779 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8780 outfile, rejectfile);
8783 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8784 err = got_error_from_errno("close");
8786 got_object_blob_close(blob);
8787 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8788 err = got_error_from_errno("close");
8790 got_object_blob_close(staged_blob);
8791 free_err = got_diffreg_result_free(diffreg_result);
8792 if (free_err && err == NULL)
8794 if (f1 && fclose(f1) == EOF && err == NULL)
8795 err = got_error_from_errno2("fclose", path1);
8796 if (f2 && fclose(f2) == EOF && err == NULL)
8797 err = got_error_from_errno2("fclose", path2);
8798 if (outfile && fclose(outfile) == EOF && err == NULL)
8799 err = got_error_from_errno2("fclose", *path_unstaged_content);
8800 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8801 err = got_error_from_errno2("fclose", *path_new_staged_content);
8802 if (path1 && unlink(path1) == -1 && err == NULL)
8803 err = got_error_from_errno2("unlink", path1);
8804 if (path2 && unlink(path2) == -1 && err == NULL)
8805 err = got_error_from_errno2("unlink", path2);
8806 if (err || !have_content) {
8807 if (*path_unstaged_content &&
8808 unlink(*path_unstaged_content) == -1 && err == NULL)
8809 err = got_error_from_errno2("unlink",
8810 *path_unstaged_content);
8811 free(*path_unstaged_content);
8812 *path_unstaged_content = NULL;
8814 if (err || !have_content || !have_rejected_content) {
8815 if (*path_new_staged_content &&
8816 unlink(*path_new_staged_content) == -1 && err == NULL)
8817 err = got_error_from_errno2("unlink",
8818 *path_new_staged_content);
8819 free(*path_new_staged_content);
8820 *path_new_staged_content = NULL;
8827 static const struct got_error *
8828 unstage_hunks(struct got_object_id *staged_blob_id,
8829 struct got_blob_object *blob_base,
8830 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8831 const char *ondisk_path, const char *label_orig,
8832 struct got_worktree *worktree, struct got_repository *repo,
8833 got_worktree_patch_cb patch_cb, void *patch_arg,
8834 got_worktree_checkout_cb progress_cb, void *progress_arg)
8836 const struct got_error *err = NULL;
8837 char *path_unstaged_content = NULL;
8838 char *path_new_staged_content = NULL;
8839 char *parent = NULL, *base_path = NULL;
8840 char *blob_base_path = NULL;
8841 struct got_object_id *new_staged_blob_id = NULL;
8842 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8845 err = create_unstaged_content(&path_unstaged_content,
8846 &path_new_staged_content, blob_id, staged_blob_id,
8847 ie->path, repo, patch_cb, patch_arg);
8851 if (path_unstaged_content == NULL)
8854 if (path_new_staged_content) {
8855 err = got_object_blob_create(&new_staged_blob_id,
8856 path_new_staged_content, repo);
8861 f = fopen(path_unstaged_content, "re");
8863 err = got_error_from_errno2("fopen",
8864 path_unstaged_content);
8867 if (fstat(fileno(f), &sb) == -1) {
8868 err = got_error_from_errno2("fstat", path_unstaged_content);
8871 if (got_fileindex_entry_staged_filetype_get(ie) ==
8872 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8873 char link_target[PATH_MAX];
8875 r = fread(link_target, 1, sizeof(link_target), f);
8876 if (r == 0 && ferror(f)) {
8877 err = got_error_from_errno("fread");
8880 if (r >= sizeof(link_target)) { /* should not happen */
8881 err = got_error(GOT_ERR_NO_SPACE);
8884 link_target[r] = '\0';
8885 err = merge_symlink(worktree, blob_base,
8886 ondisk_path, ie->path, label_orig, link_target,
8887 worktree->base_commit_id, repo, progress_cb,
8890 int local_changes_subsumed;
8892 err = got_path_dirname(&parent, ondisk_path);
8896 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8898 err = got_error_from_errno("asprintf");
8903 err = got_opentemp_named(&blob_base_path, &f_base,
8907 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8913 * In order the run a 3-way merge with a symlink we copy the symlink's
8914 * target path into a temporary file and use that file with diff3.
8916 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8917 err = dump_symlink_target_path_to_file(&f_deriv2,
8923 fd = open(ondisk_path,
8924 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8926 err = got_error_from_errno2("open", ondisk_path);
8929 f_deriv2 = fdopen(fd, "r");
8930 if (f_deriv2 == NULL) {
8931 err = got_error_from_errno2("fdopen", ondisk_path);
8937 err = merge_file(&local_changes_subsumed, worktree,
8938 f_base, f, f_deriv2, ondisk_path, ie->path,
8939 got_fileindex_perms_to_st(ie),
8940 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8941 repo, progress_cb, progress_arg);
8946 if (new_staged_blob_id) {
8947 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8948 SHA1_DIGEST_LENGTH);
8950 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8951 got_fileindex_entry_staged_filetype_set(ie, 0);
8954 free(new_staged_blob_id);
8955 if (path_unstaged_content &&
8956 unlink(path_unstaged_content) == -1 && err == NULL)
8957 err = got_error_from_errno2("unlink", path_unstaged_content);
8958 if (path_new_staged_content &&
8959 unlink(path_new_staged_content) == -1 && err == NULL)
8960 err = got_error_from_errno2("unlink", path_new_staged_content);
8961 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8962 err = got_error_from_errno2("unlink", blob_base_path);
8963 if (f_base && fclose(f_base) == EOF && err == NULL)
8964 err = got_error_from_errno2("fclose", path_unstaged_content);
8965 if (f && fclose(f) == EOF && err == NULL)
8966 err = got_error_from_errno2("fclose", path_unstaged_content);
8967 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8968 err = got_error_from_errno2("fclose", ondisk_path);
8969 free(path_unstaged_content);
8970 free(path_new_staged_content);
8971 free(blob_base_path);
8977 static const struct got_error *
8978 unstage_path(void *arg, unsigned char status,
8979 unsigned char staged_status, const char *relpath,
8980 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8981 struct got_object_id *commit_id, int dirfd, const char *de_name)
8983 const struct got_error *err = NULL;
8984 struct unstage_path_arg *a = arg;
8985 struct got_fileindex_entry *ie;
8986 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8987 char *ondisk_path = NULL;
8988 char *id_str = NULL, *label_orig = NULL;
8989 int local_changes_subsumed;
8991 int fd1 = -1, fd2 = -1;
8993 if (staged_status != GOT_STATUS_ADD &&
8994 staged_status != GOT_STATUS_MODIFY &&
8995 staged_status != GOT_STATUS_DELETE)
8998 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9000 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9002 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
9004 return got_error_from_errno("asprintf");
9006 err = got_object_id_str(&id_str,
9007 commit_id ? commit_id : a->worktree->base_commit_id);
9010 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9012 err = got_error_from_errno("asprintf");
9016 fd1 = got_opentempfd();
9018 err = got_error_from_errno("got_opentempfd");
9021 fd2 = got_opentempfd();
9023 err = got_error_from_errno("got_opentempfd");
9027 switch (staged_status) {
9028 case GOT_STATUS_MODIFY:
9029 err = got_object_open_as_blob(&blob_base, a->repo,
9030 blob_id, 8192, fd1);
9034 case GOT_STATUS_ADD:
9036 if (staged_status == GOT_STATUS_ADD) {
9037 int choice = GOT_PATCH_CHOICE_NONE;
9038 err = (*a->patch_cb)(&choice, a->patch_arg,
9039 staged_status, ie->path, NULL, 1, 1);
9042 if (choice != GOT_PATCH_CHOICE_YES)
9045 err = unstage_hunks(staged_blob_id,
9046 blob_base, blob_id, ie, ondisk_path,
9047 label_orig, a->worktree, a->repo,
9048 a->patch_cb, a->patch_arg,
9049 a->progress_cb, a->progress_arg);
9050 break; /* Done with this file. */
9053 err = got_object_open_as_blob(&blob_staged, a->repo,
9054 staged_blob_id, 8192, fd2);
9057 switch (got_fileindex_entry_staged_filetype_get(ie)) {
9058 case GOT_FILEIDX_MODE_BAD_SYMLINK:
9059 case GOT_FILEIDX_MODE_REGULAR_FILE:
9060 err = merge_blob(&local_changes_subsumed, a->worktree,
9061 blob_base, ondisk_path, relpath,
9062 got_fileindex_perms_to_st(ie), label_orig,
9063 blob_staged, commit_id ? commit_id :
9064 a->worktree->base_commit_id, a->repo,
9065 a->progress_cb, a->progress_arg);
9067 case GOT_FILEIDX_MODE_SYMLINK:
9068 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9069 char *staged_target;
9070 err = got_object_blob_read_to_str(
9071 &staged_target, blob_staged);
9074 err = merge_symlink(a->worktree, blob_base,
9075 ondisk_path, relpath, label_orig,
9076 staged_target, commit_id ? commit_id :
9077 a->worktree->base_commit_id,
9078 a->repo, a->progress_cb, a->progress_arg);
9079 free(staged_target);
9081 err = merge_blob(&local_changes_subsumed,
9082 a->worktree, blob_base, ondisk_path,
9083 relpath, got_fileindex_perms_to_st(ie),
9084 label_orig, blob_staged,
9085 commit_id ? commit_id :
9086 a->worktree->base_commit_id, a->repo,
9087 a->progress_cb, a->progress_arg);
9091 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9095 got_fileindex_entry_stage_set(ie,
9096 GOT_FILEIDX_STAGE_NONE);
9097 got_fileindex_entry_staged_filetype_set(ie, 0);
9100 case GOT_STATUS_DELETE:
9102 int choice = GOT_PATCH_CHOICE_NONE;
9103 err = (*a->patch_cb)(&choice, a->patch_arg,
9104 staged_status, ie->path, NULL, 1, 1);
9107 if (choice == GOT_PATCH_CHOICE_NO)
9109 if (choice != GOT_PATCH_CHOICE_YES) {
9110 err = got_error(GOT_ERR_PATCH_CHOICE);
9114 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9115 got_fileindex_entry_staged_filetype_set(ie, 0);
9116 err = get_file_status(&status, &sb, ie, ondisk_path,
9117 dirfd, de_name, a->repo);
9120 err = (*a->progress_cb)(a->progress_arg, status, relpath);
9125 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9126 err = got_error_from_errno("close");
9128 got_object_blob_close(blob_base);
9129 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9130 err = got_error_from_errno("close");
9132 got_object_blob_close(blob_staged);
9138 const struct got_error *
9139 got_worktree_unstage(struct got_worktree *worktree,
9140 struct got_pathlist_head *paths,
9141 got_worktree_checkout_cb progress_cb, void *progress_arg,
9142 got_worktree_patch_cb patch_cb, void *patch_arg,
9143 struct got_repository *repo)
9145 const struct got_error *err = NULL, *sync_err, *unlockerr;
9146 struct got_pathlist_entry *pe;
9147 struct got_fileindex *fileindex = NULL;
9148 char *fileindex_path = NULL;
9149 struct unstage_path_arg upa;
9151 err = lock_worktree(worktree, LOCK_EX);
9155 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9159 upa.worktree = worktree;
9160 upa.fileindex = fileindex;
9162 upa.progress_cb = progress_cb;
9163 upa.progress_arg = progress_arg;
9164 upa.patch_cb = patch_cb;
9165 upa.patch_arg = patch_arg;
9166 TAILQ_FOREACH(pe, paths, entry) {
9167 err = worktree_status(worktree, pe->path, fileindex, repo,
9168 unstage_path, &upa, NULL, NULL, 1, 0);
9173 sync_err = sync_fileindex(fileindex, fileindex_path);
9174 if (sync_err && err == NULL)
9177 free(fileindex_path);
9179 got_fileindex_free(fileindex);
9180 unlockerr = lock_worktree(worktree, LOCK_SH);
9181 if (unlockerr && err == NULL)
9186 struct report_file_info_arg {
9187 struct got_worktree *worktree;
9188 got_worktree_path_info_cb info_cb;
9190 struct got_pathlist_head *paths;
9191 got_cancel_cb cancel_cb;
9195 static const struct got_error *
9196 report_file_info(void *arg, struct got_fileindex_entry *ie)
9198 struct report_file_info_arg *a = arg;
9199 struct got_pathlist_entry *pe;
9200 struct got_object_id blob_id, staged_blob_id, commit_id;
9201 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9202 struct got_object_id *commit_idp = NULL;
9205 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9206 return got_error(GOT_ERR_CANCELLED);
9208 TAILQ_FOREACH(pe, a->paths, entry) {
9209 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9210 got_path_is_child(ie->path, pe->path, pe->path_len))
9213 if (pe == NULL) /* not found */
9216 if (got_fileindex_entry_has_blob(ie))
9217 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9218 stage = got_fileindex_entry_stage_get(ie);
9219 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9220 stage == GOT_FILEIDX_STAGE_ADD) {
9221 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9222 &staged_blob_id, ie);
9225 if (got_fileindex_entry_has_commit(ie))
9226 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9228 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9229 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9232 const struct got_error *
9233 got_worktree_path_info(struct got_worktree *worktree,
9234 struct got_pathlist_head *paths,
9235 got_worktree_path_info_cb info_cb, void *info_arg,
9236 got_cancel_cb cancel_cb, void *cancel_arg)
9239 const struct got_error *err = NULL, *unlockerr;
9240 struct got_fileindex *fileindex = NULL;
9241 char *fileindex_path = NULL;
9242 struct report_file_info_arg arg;
9244 err = lock_worktree(worktree, LOCK_SH);
9248 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9252 arg.worktree = worktree;
9253 arg.info_cb = info_cb;
9254 arg.info_arg = info_arg;
9256 arg.cancel_cb = cancel_cb;
9257 arg.cancel_arg = cancel_arg;
9258 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9261 free(fileindex_path);
9263 got_fileindex_free(fileindex);
9264 unlockerr = lock_worktree(worktree, LOCK_UN);
9265 if (unlockerr && err == NULL)
9270 static const struct got_error *
9271 patch_check_path(const char *p, char **path, unsigned char *status,
9272 unsigned char *staged_status, struct got_fileindex *fileindex,
9273 struct got_worktree *worktree, struct got_repository *repo)
9275 const struct got_error *err;
9276 struct got_fileindex_entry *ie;
9278 char *ondisk_path = NULL;
9280 err = got_worktree_resolve_path(path, worktree, p);
9284 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9285 *path[0] ? "/" : "", *path) == -1)
9286 return got_error_from_errno("asprintf");
9288 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9290 *staged_status = get_staged_status(ie);
9291 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9296 *staged_status = GOT_STATUS_NO_CHANGE;
9297 *status = GOT_STATUS_UNVERSIONED;
9298 if (lstat(ondisk_path, &sb) == -1) {
9299 if (errno != ENOENT) {
9300 err = got_error_from_errno2("lstat",
9304 *status = GOT_STATUS_NONEXISTENT;
9313 static const struct got_error *
9314 patch_can_rm(const char *path, unsigned char status,
9315 unsigned char staged_status)
9317 if (status == GOT_STATUS_NONEXISTENT)
9318 return got_error_set_errno(ENOENT, path);
9319 if (status != GOT_STATUS_NO_CHANGE &&
9320 status != GOT_STATUS_ADD &&
9321 status != GOT_STATUS_MODIFY &&
9322 status != GOT_STATUS_MODE_CHANGE)
9323 return got_error_path(path, GOT_ERR_FILE_STATUS);
9324 if (staged_status == GOT_STATUS_DELETE)
9325 return got_error_path(path, GOT_ERR_FILE_STATUS);
9329 static const struct got_error *
9330 patch_can_add(const char *path, unsigned char status)
9332 if (status != GOT_STATUS_NONEXISTENT)
9333 return got_error_path(path, GOT_ERR_FILE_STATUS);
9337 static const struct got_error *
9338 patch_can_edit(const char *path, unsigned char status,
9339 unsigned char staged_status)
9341 if (status == GOT_STATUS_NONEXISTENT)
9342 return got_error_set_errno(ENOENT, path);
9343 if (status != GOT_STATUS_NO_CHANGE &&
9344 status != GOT_STATUS_ADD &&
9345 status != GOT_STATUS_MODIFY)
9346 return got_error_path(path, GOT_ERR_FILE_STATUS);
9347 if (staged_status == GOT_STATUS_DELETE)
9348 return got_error_path(path, GOT_ERR_FILE_STATUS);
9352 const struct got_error *
9353 got_worktree_patch_prepare(struct got_fileindex **fileindex,
9354 char **fileindex_path, struct got_worktree *worktree)
9356 return open_fileindex(fileindex, fileindex_path, worktree);
9359 const struct got_error *
9360 got_worktree_patch_check_path(const char *old, const char *new,
9361 char **oldpath, char **newpath, struct got_worktree *worktree,
9362 struct got_repository *repo, struct got_fileindex *fileindex)
9364 const struct got_error *err = NULL;
9365 int file_renamed = 0;
9366 unsigned char status_old, staged_status_old;
9367 unsigned char status_new, staged_status_new;
9372 err = patch_check_path(old != NULL ? old : new, oldpath,
9373 &status_old, &staged_status_old, fileindex, worktree, repo);
9377 err = patch_check_path(new != NULL ? new : old, newpath,
9378 &status_new, &staged_status_new, fileindex, worktree, repo);
9382 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9385 if (old != NULL && new == NULL)
9386 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9387 else if (file_renamed) {
9388 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9390 err = patch_can_add(*newpath, status_new);
9391 } else if (old == NULL)
9392 err = patch_can_add(*newpath, status_new);
9394 err = patch_can_edit(*newpath, status_new, staged_status_new);
9406 const struct got_error *
9407 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9408 struct got_worktree *worktree, struct got_fileindex *fileindex,
9409 got_worktree_checkout_cb progress_cb, void *progress_arg)
9411 struct schedule_addition_args saa;
9413 memset(&saa, 0, sizeof(saa));
9414 saa.worktree = worktree;
9415 saa.fileindex = fileindex;
9416 saa.progress_cb = progress_cb;
9417 saa.progress_arg = progress_arg;
9420 return worktree_status(worktree, path, fileindex, repo,
9421 schedule_addition, &saa, NULL, NULL, 1, 0);
9424 const struct got_error *
9425 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9426 struct got_worktree *worktree, struct got_fileindex *fileindex,
9427 got_worktree_delete_cb progress_cb, void *progress_arg)
9429 struct schedule_deletion_args sda;
9431 memset(&sda, 0, sizeof(sda));
9432 sda.worktree = worktree;
9433 sda.fileindex = fileindex;
9434 sda.progress_cb = progress_cb;
9435 sda.progress_arg = progress_arg;
9437 sda.delete_local_mods = 0;
9438 sda.keep_on_disk = 0;
9439 sda.ignore_missing_paths = 0;
9440 sda.status_codes = NULL;
9442 return worktree_status(worktree, path, fileindex, repo,
9443 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9446 const struct got_error *
9447 got_worktree_patch_complete(struct got_fileindex *fileindex,
9448 const char *fileindex_path)
9450 const struct got_error *err = NULL;
9452 err = sync_fileindex(fileindex, fileindex_path);
9453 got_fileindex_free(fileindex);