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>
39 #include "got_error.h"
40 #include "got_repository.h"
41 #include "got_reference.h"
42 #include "got_object.h"
44 #include "got_cancel.h"
45 #include "got_worktree.h"
46 #include "got_opentemp.h"
49 #include "got_lib_worktree.h"
50 #include "got_lib_hash.h"
51 #include "got_lib_fileindex.h"
52 #include "got_lib_inflate.h"
53 #include "got_lib_delta.h"
54 #include "got_lib_object.h"
55 #include "got_lib_object_parse.h"
56 #include "got_lib_object_create.h"
57 #include "got_lib_object_idset.h"
58 #include "got_lib_diff.h"
59 #include "got_lib_gotconfig.h"
62 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
65 #define GOT_MERGE_LABEL_MERGED "merged change"
66 #define GOT_MERGE_LABEL_BASE "3-way merge base"
68 static mode_t apply_umask(mode_t);
70 static const struct got_error *
71 create_meta_file(const char *path_got, const char *name, const char *content)
73 const struct got_error *err = NULL;
76 if (asprintf(&path, "%s/%s", path_got, name) == -1)
77 return got_error_from_errno("asprintf");
79 err = got_path_create_file(path, content);
84 static const struct got_error *
85 update_meta_file(const char *path_got, const char *name, const char *content)
87 const struct got_error *err = NULL;
92 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
93 err = got_error_from_errno("asprintf");
98 err = got_opentemp_named(&tmppath, &tmpfile, path, "");
103 int len = fprintf(tmpfile, "%s\n", content);
104 if (len != strlen(content) + 1) {
105 err = got_error_from_errno2("fprintf", tmppath);
110 if (rename(tmppath, path) != 0) {
111 err = got_error_from_errno3("rename", tmppath, path);
117 if (fclose(tmpfile) == EOF && err == NULL)
118 err = got_error_from_errno2("fclose", tmppath);
123 static const struct got_error *
124 write_head_ref(const char *path_got, struct got_reference *head_ref)
126 const struct got_error *err = NULL;
129 if (got_ref_is_symbolic(head_ref)) {
130 refstr = got_ref_to_str(head_ref);
132 return got_error_from_errno("got_ref_to_str");
134 refstr = strdup(got_ref_get_name(head_ref));
136 return got_error_from_errno("strdup");
138 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
143 const struct got_error *
144 got_worktree_init(const char *path, struct got_reference *head_ref,
145 const char *prefix, struct got_repository *repo)
147 const struct got_error *err = NULL;
148 struct got_object_id *commit_id = NULL;
150 uint32_t uuid_status;
152 char *path_got = NULL;
153 char *formatstr = NULL;
154 char *absprefix = NULL;
155 char *basestr = NULL;
156 char *uuidstr = NULL;
158 if (strcmp(path, got_repo_get_path(repo)) == 0) {
159 err = got_error(GOT_ERR_WORKTREE_REPO);
163 err = got_ref_resolve(&commit_id, repo, head_ref);
166 err = got_object_get_type(&obj_type, repo, commit_id);
169 if (obj_type != GOT_OBJ_TYPE_COMMIT)
170 return got_error(GOT_ERR_OBJ_TYPE);
172 if (!got_path_is_absolute(prefix)) {
173 if (asprintf(&absprefix, "/%s", prefix) == -1)
174 return got_error_from_errno("asprintf");
177 /* Create top-level directory (may already exist). */
178 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
179 err = got_error_from_errno2("mkdir", path);
183 /* Create .got directory (may already exist). */
184 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
185 err = got_error_from_errno("asprintf");
188 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
189 err = got_error_from_errno2("mkdir", path_got);
193 /* Create an empty lock file. */
194 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
198 /* Create an empty file index. */
199 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
203 /* Write the HEAD reference. */
204 err = write_head_ref(path_got, head_ref);
208 /* Record our base commit. */
209 err = got_object_id_str(&basestr, commit_id);
212 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
216 /* Store path to repository. */
217 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
218 got_repo_get_path(repo));
222 /* Store in-repository path prefix. */
223 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
224 absprefix ? absprefix : prefix);
229 uuid_create(&uuid, &uuid_status);
230 if (uuid_status != uuid_s_ok) {
231 err = got_error_uuid(uuid_status, "uuid_create");
234 uuid_to_string(&uuid, &uuidstr, &uuid_status);
235 if (uuid_status != uuid_s_ok) {
236 err = got_error_uuid(uuid_status, "uuid_to_string");
239 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
243 /* Stamp work tree with format file. */
244 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
245 err = got_error_from_errno("asprintf");
248 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
262 const struct got_error *
263 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
264 const char *path_prefix)
266 char *absprefix = NULL;
268 if (!got_path_is_absolute(path_prefix)) {
269 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
270 return got_error_from_errno("asprintf");
272 *match = (strcmp(absprefix ? absprefix : path_prefix,
273 worktree->path_prefix) == 0);
279 got_worktree_get_head_ref_name(struct got_worktree *worktree)
281 return worktree->head_ref_name;
284 const struct got_error *
285 got_worktree_set_head_ref(struct got_worktree *worktree,
286 struct got_reference *head_ref)
288 const struct got_error *err = NULL;
289 char *path_got = NULL, *head_ref_name = NULL;
291 if (asprintf(&path_got, "%s/%s", worktree->root_path,
292 GOT_WORKTREE_GOT_DIR) == -1) {
293 err = got_error_from_errno("asprintf");
298 head_ref_name = strdup(got_ref_get_name(head_ref));
299 if (head_ref_name == NULL) {
300 err = got_error_from_errno("strdup");
304 err = write_head_ref(path_got, head_ref);
308 free(worktree->head_ref_name);
309 worktree->head_ref_name = head_ref_name;
317 struct got_object_id *
318 got_worktree_get_base_commit_id(struct got_worktree *worktree)
320 return worktree->base_commit_id;
323 const struct got_error *
324 got_worktree_set_base_commit_id(struct got_worktree *worktree,
325 struct got_repository *repo, struct got_object_id *commit_id)
327 const struct got_error *err;
328 struct got_object *obj = NULL;
330 char *path_got = NULL;
332 if (asprintf(&path_got, "%s/%s", worktree->root_path,
333 GOT_WORKTREE_GOT_DIR) == -1) {
334 err = got_error_from_errno("asprintf");
339 err = got_object_open(&obj, repo, commit_id);
343 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
344 err = got_error(GOT_ERR_OBJ_TYPE);
348 /* Record our base commit. */
349 err = got_object_id_str(&id_str, commit_id);
352 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
356 free(worktree->base_commit_id);
357 worktree->base_commit_id = got_object_id_dup(commit_id);
358 if (worktree->base_commit_id == NULL) {
359 err = got_error_from_errno("got_object_id_dup");
364 got_object_close(obj);
370 const struct got_gotconfig *
371 got_worktree_get_gotconfig(struct got_worktree *worktree)
373 return worktree->gotconfig;
376 static const struct got_error *
377 lock_worktree(struct got_worktree *worktree, int operation)
379 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
380 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
381 : got_error_from_errno2("flock",
382 got_worktree_get_root_path(worktree)));
386 static const struct got_error *
387 add_dir_on_disk(struct got_worktree *worktree, const char *path)
389 const struct got_error *err = NULL;
392 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
393 return got_error_from_errno("asprintf");
395 err = got_path_mkdir(abspath);
396 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
399 if (lstat(abspath, &sb) == -1) {
400 err = got_error_from_errno2("lstat", abspath);
401 } else if (!S_ISDIR(sb.st_mode)) {
402 /* TODO directory is obstructed; do something */
403 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
410 static const struct got_error *
411 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
413 const struct got_error *err = NULL;
416 size_t flen1 = 0, flen2 = 0;
421 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
422 if (flen1 == 0 && ferror(f1)) {
423 err = got_error_from_errno("fread");
426 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
427 if (flen2 == 0 && ferror(f2)) {
428 err = got_error_from_errno("fread");
435 } else if (flen2 == 0) {
439 } else if (flen1 == flen2) {
440 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
453 static const struct got_error *
454 check_files_equal(int *same, FILE *f1, FILE *f2)
461 if (fstat(fileno(f1), &sb) != 0)
462 return got_error_from_errno("fstat");
465 if (fstat(fileno(f2), &sb) != 0)
466 return got_error_from_errno("fstat");
469 if (size1 != size2) {
474 if (fseek(f1, 0L, SEEK_SET) == -1)
475 return got_ferror(f1, GOT_ERR_IO);
476 if (fseek(f2, 0L, SEEK_SET) == -1)
477 return got_ferror(f2, GOT_ERR_IO);
479 return check_file_contents_equal(same, f1, f2);
482 static const struct got_error *
483 copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
491 if (fseek(f, 0L, SEEK_SET) == -1)
492 return got_ferror(f, GOT_ERR_IO);
495 flen = fread(fbuf, 1, sizeof(fbuf), f);
498 return got_error_from_errno("fread");
502 outlen = write(outfd, fbuf, flen);
504 return got_error_from_errno("write");
506 return got_error(GOT_ERR_IO);
513 static const struct got_error *
514 merge_binary_file(int *overlapcnt, int merged_fd,
515 FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
516 const char *label_deriv, const char *label_orig, const char *label_deriv2,
517 const char *ondisk_path)
519 const struct got_error *err = NULL;
520 int same_content, changed_deriv, changed_deriv2;
521 int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
522 off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
523 char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
524 char *base_path_orig = NULL, *base_path_deriv = NULL;
525 char *base_path_deriv2 = NULL;
529 err = check_files_equal(&same_content, f_deriv, f_deriv2);
534 return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
536 err = check_files_equal(&same_content, f_deriv, f_orig);
539 changed_deriv = !same_content;
540 err = check_files_equal(&same_content, f_deriv2, f_orig);
543 changed_deriv2 = !same_content;
545 if (changed_deriv && changed_deriv2) {
547 if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
548 err = got_error_from_errno("asprintf");
551 if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
552 err = got_error_from_errno("asprintf");
555 if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
556 err = got_error_from_errno("asprintf");
559 err = got_opentemp_named_fd(&path_orig, &fd_orig,
563 err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
564 base_path_deriv, "");
567 err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
568 base_path_deriv2, "");
571 err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
574 err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
577 err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
580 if (dprintf(merged_fd, "Binary files differ and cannot be "
581 "merged automatically:\n") < 0) {
582 err = got_error_from_errno("dprintf");
585 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
586 GOT_DIFF_CONFLICT_MARKER_BEGIN,
587 label_deriv ? " " : "",
588 label_deriv ? label_deriv : "",
590 err = got_error_from_errno("dprintf");
594 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
595 GOT_DIFF_CONFLICT_MARKER_ORIG,
596 label_orig ? " " : "",
597 label_orig ? label_orig : "",
599 err = got_error_from_errno("dprintf");
603 if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
604 GOT_DIFF_CONFLICT_MARKER_SEP,
606 GOT_DIFF_CONFLICT_MARKER_END,
607 label_deriv2 ? " " : "",
608 label_deriv2 ? label_deriv2 : "") < 0) {
609 err = got_error_from_errno("dprintf");
612 } else if (changed_deriv)
613 err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
614 else if (changed_deriv2)
615 err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
617 if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
619 err = got_error_from_errno2("unlink", path_orig);
620 if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
621 err = got_error_from_errno2("close", path_orig);
622 if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
623 err = got_error_from_errno2("close", path_deriv);
624 if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
625 err = got_error_from_errno2("close", path_deriv2);
629 free(base_path_orig);
630 free(base_path_deriv);
631 free(base_path_deriv2);
636 * Perform a 3-way merge where the file f_orig acts as the common
637 * ancestor, the file f_deriv acts as the first derived version,
638 * and the file f_deriv2 acts as the second derived version.
639 * The merge result will be written to a new file at ondisk_path; any
640 * existing file at this path will be replaced.
642 static const struct got_error *
643 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
644 FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
645 const char *path, uint16_t st_mode,
646 const char *label_orig, const char *label_deriv, const char *label_deriv2,
647 enum got_diff_algorithm diff_algo, struct got_repository *repo,
648 got_worktree_checkout_cb progress_cb, void *progress_arg)
650 const struct got_error *err = NULL;
652 FILE *f_merged = NULL;
653 char *merged_path = NULL, *base_path = NULL;
657 *local_changes_subsumed = 0;
659 err = got_path_dirname(&parent, ondisk_path);
663 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
664 err = got_error_from_errno("asprintf");
668 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path, "");
672 err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
673 f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
675 if (err->code != GOT_ERR_FILE_BINARY)
677 err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
678 f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
684 err = (*progress_cb)(progress_arg,
685 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
689 if (fsync(merged_fd) != 0) {
690 err = got_error_from_errno("fsync");
694 f_merged = fdopen(merged_fd, "r");
695 if (f_merged == NULL) {
696 err = got_error_from_errno("fdopen");
701 /* Check if a clean merge has subsumed all local changes. */
702 if (overlapcnt == 0) {
703 err = check_files_equal(local_changes_subsumed, f_deriv,
709 if (fchmod(fileno(f_merged), apply_umask(st_mode)) != 0) {
710 err = got_error_from_errno2("fchmod", merged_path);
714 if (rename(merged_path, ondisk_path) != 0) {
715 err = got_error_from_errno3("rename", merged_path,
724 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
725 err = got_error_from_errno("close");
726 if (f_merged && fclose(f_merged) == EOF && err == NULL)
727 err = got_error_from_errno("fclose");
734 static const struct got_error *
735 update_symlink(const char *ondisk_path, const char *target_path,
738 /* This is not atomic but matches what 'ln -sf' does. */
739 if (unlink(ondisk_path) == -1)
740 return got_error_from_errno2("unlink", ondisk_path);
741 if (symlink(target_path, ondisk_path) == -1)
742 return got_error_from_errno3("symlink", target_path,
748 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
749 * in the work tree with a file that contains conflict markers and the
750 * conflicting target paths of the original version, a "derived version"
751 * of a symlink from an incoming change, and a local version of the symlink.
753 * The original versions's target path can be NULL if it is not available,
754 * such as if both derived versions added a new symlink at the same path.
756 * The incoming derived symlink target is NULL in case the incoming change
757 * has deleted this symlink.
759 static const struct got_error *
760 install_symlink_conflict(const char *deriv_target,
761 struct got_object_id *deriv_base_commit_id, const char *orig_target,
762 const char *label_orig, const char *local_target, const char *ondisk_path)
764 const struct got_error *err;
765 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
768 err = got_object_id_str(&id_str, deriv_base_commit_id);
770 return got_error_from_errno("asprintf");
772 if (asprintf(&label_deriv, "%s: commit %s",
773 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
774 err = got_error_from_errno("asprintf");
778 err = got_opentemp_named(&path, &f, "got-symlink-conflict", "");
782 if (fchmod(fileno(f), apply_umask(GOT_DEFAULT_FILE_MODE)) == -1) {
783 err = got_error_from_errno2("fchmod", path);
787 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
788 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
789 deriv_target ? deriv_target : "(symlink was deleted)",
790 orig_target ? label_orig : "",
791 orig_target ? "\n" : "",
792 orig_target ? orig_target : "",
793 orig_target ? "\n" : "",
794 GOT_DIFF_CONFLICT_MARKER_SEP,
795 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
796 err = got_error_from_errno2("fprintf", path);
800 if (unlink(ondisk_path) == -1) {
801 err = got_error_from_errno2("unlink", ondisk_path);
804 if (rename(path, ondisk_path) == -1) {
805 err = got_error_from_errno3("rename", path, ondisk_path);
809 if (f != NULL && fclose(f) == EOF && err == NULL)
810 err = got_error_from_errno2("fclose", path);
817 /* forward declaration */
818 static const struct got_error *
819 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
820 const char *, const char *, uint16_t, const char *,
821 struct got_blob_object *, struct got_object_id *,
822 struct got_repository *, got_worktree_checkout_cb, void *);
825 * Merge a symlink into the work tree, where blob_orig acts as the common
826 * ancestor, deriv_target is the link target of the first derived version,
827 * and the symlink on disk acts as the second derived version.
828 * Assume that contents of both blobs represent symlinks.
830 static const struct got_error *
831 merge_symlink(struct got_worktree *worktree,
832 struct got_blob_object *blob_orig, const char *ondisk_path,
833 const char *path, const char *label_orig, const char *deriv_target,
834 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
835 got_worktree_checkout_cb progress_cb, void *progress_arg)
837 const struct got_error *err = NULL;
838 char *ancestor_target = NULL;
840 ssize_t ondisk_len, deriv_len;
841 char ondisk_target[PATH_MAX];
842 int have_local_change = 0;
843 int have_incoming_change = 0;
845 if (lstat(ondisk_path, &sb) == -1)
846 return got_error_from_errno2("lstat", ondisk_path);
848 ondisk_len = readlink(ondisk_path, ondisk_target,
849 sizeof(ondisk_target));
850 if (ondisk_len == -1) {
851 err = got_error_from_errno2("readlink",
855 ondisk_target[ondisk_len] = '\0';
858 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
863 if (ancestor_target == NULL ||
864 (ondisk_len != strlen(ancestor_target) ||
865 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
866 have_local_change = 1;
868 deriv_len = strlen(deriv_target);
869 if (ancestor_target == NULL ||
870 (deriv_len != strlen(ancestor_target) ||
871 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
872 have_incoming_change = 1;
874 if (!have_local_change && !have_incoming_change) {
875 if (ancestor_target) {
876 /* Both sides made the same change. */
877 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
879 } else if (deriv_len == ondisk_len &&
880 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
881 /* Both sides added the same symlink. */
882 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
885 /* Both sides added symlinks which don't match. */
886 err = install_symlink_conflict(deriv_target,
887 deriv_base_commit_id, ancestor_target,
888 label_orig, ondisk_target, ondisk_path);
891 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
894 } else if (!have_local_change && have_incoming_change) {
895 /* Apply the incoming change. */
896 err = update_symlink(ondisk_path, deriv_target,
897 strlen(deriv_target));
900 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
901 } else if (have_local_change && have_incoming_change) {
902 if (deriv_len == ondisk_len &&
903 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
904 /* Both sides made the same change. */
905 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
908 err = install_symlink_conflict(deriv_target,
909 deriv_base_commit_id, ancestor_target, label_orig,
910 ondisk_target, ondisk_path);
913 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
919 free(ancestor_target);
923 static const struct got_error *
924 dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
926 const struct got_error *err = NULL;
927 char target_path[PATH_MAX];
936 return got_error_from_errno("got_opentemp");
937 target_len = readlink(ondisk_path, target_path, sizeof(target_path));
938 if (target_len == -1) {
939 err = got_error_from_errno2("readlink", ondisk_path);
942 n = fwrite(target_path, 1, target_len, f);
943 if (n != target_len) {
944 err = got_ferror(f, GOT_ERR_IO);
947 if (fflush(f) == EOF) {
948 err = got_error_from_errno("fflush");
951 if (fseek(f, 0L, SEEK_SET) == -1) {
952 err = got_ferror(f, GOT_ERR_IO);
964 * Perform a 3-way merge where blob_orig acts as the common ancestor,
965 * blob_deriv acts as the first derived version, and the file on disk
966 * acts as the second derived version.
968 static const struct got_error *
969 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
970 struct got_blob_object *blob_orig, const char *ondisk_path,
971 const char *path, uint16_t st_mode, const char *label_orig,
972 struct got_blob_object *blob_deriv,
973 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
974 got_worktree_checkout_cb progress_cb, void *progress_arg)
976 const struct got_error *err = NULL;
977 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
978 char *blob_orig_path = NULL;
979 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
980 char *label_deriv = NULL, *parent = NULL;
982 *local_changes_subsumed = 0;
984 err = got_path_dirname(&parent, ondisk_path);
989 if (asprintf(&base_path, "%s/got-merge-blob-orig",
991 err = got_error_from_errno("asprintf");
996 err = got_opentemp_named(&blob_orig_path, &f_orig,
1000 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1007 * No common ancestor exists. This is an "add vs add" conflict
1008 * and we simply use an empty ancestor file to make both files
1009 * appear in the merged result in their entirety.
1011 f_orig = got_opentemp();
1012 if (f_orig == NULL) {
1013 err = got_error_from_errno("got_opentemp");
1018 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1019 err = got_error_from_errno("asprintf");
1024 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path, "");
1027 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1032 err = got_object_id_str(&id_str, deriv_base_commit_id);
1035 if (asprintf(&label_deriv, "%s: commit %s",
1036 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1037 err = got_error_from_errno("asprintf");
1042 * In order the run a 3-way merge with a symlink we copy the symlink's
1043 * target path into a temporary file and use that file with diff3.
1045 if (S_ISLNK(st_mode)) {
1046 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1051 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1053 err = got_error_from_errno2("open", ondisk_path);
1056 f_deriv2 = fdopen(fd, "r");
1057 if (f_deriv2 == NULL) {
1058 err = got_error_from_errno2("fdopen", ondisk_path);
1064 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1065 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1066 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1068 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1069 err = got_error_from_errno("fclose");
1070 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1071 err = got_error_from_errno("fclose");
1072 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1073 err = got_error_from_errno("fclose");
1075 if (blob_orig_path) {
1076 unlink(blob_orig_path);
1077 free(blob_orig_path);
1079 if (blob_deriv_path) {
1080 unlink(blob_deriv_path);
1081 free(blob_deriv_path);
1089 static const struct got_error *
1090 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1091 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1092 int wt_fd, const char *path, struct got_object_id *blob_id)
1094 const struct got_error *err = NULL;
1095 struct got_fileindex_entry *new_ie;
1099 err = got_fileindex_entry_alloc(&new_ie, path);
1103 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1104 blob_id->hash, base_commit_id->hash, 1);
1108 err = got_fileindex_entry_add(fileindex, new_ie);
1111 got_fileindex_entry_free(new_ie);
1118 get_ondisk_perms(int executable, mode_t st_mode)
1120 mode_t xbits = S_IXUSR;
1123 /* Map read bits to execute bits. */
1124 if (st_mode & S_IRGRP)
1126 if (st_mode & S_IROTH)
1128 return st_mode | xbits;
1135 apply_umask(mode_t mode)
1144 /* forward declaration */
1145 static const struct got_error *
1146 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1147 const char *path, mode_t te_mode, mode_t st_mode,
1148 struct got_blob_object *blob, int restoring_missing_file,
1149 int reverting_versioned_file, int installing_bad_symlink,
1150 int path_is_unversioned, struct got_repository *repo,
1151 got_worktree_checkout_cb progress_cb, void *progress_arg);
1154 * This function assumes that the provided symlink target points at a
1155 * safe location in the work tree!
1157 static const struct got_error *
1158 replace_existing_symlink(int *did_something, const char *ondisk_path,
1159 const char *target_path, size_t target_len)
1161 const struct got_error *err = NULL;
1163 char etarget[PATH_MAX];
1169 * "Bad" symlinks (those pointing outside the work tree or into the
1170 * .got directory) are installed in the work tree as a regular file
1171 * which contains the bad symlink target path.
1172 * The new symlink target has already been checked for safety by our
1173 * caller. If we can successfully open a regular file then we simply
1174 * replace this file with a symlink below.
1176 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1178 if (!got_err_open_nofollow_on_symlink())
1179 return got_error_from_errno2("open", ondisk_path);
1181 /* We are updating an existing on-disk symlink. */
1182 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1184 return got_error_from_errno2("readlink", ondisk_path);
1186 if (elen == target_len &&
1187 memcmp(etarget, target_path, target_len) == 0)
1188 return NULL; /* nothing to do */
1192 err = update_symlink(ondisk_path, target_path, target_len);
1193 if (fd != -1 && close(fd) == -1 && err == NULL)
1194 err = got_error_from_errno2("close", ondisk_path);
1198 static const struct got_error *
1199 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1200 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1202 const struct got_error *err = NULL;
1203 char canonpath[PATH_MAX];
1204 char *path_got = NULL;
1206 *is_bad_symlink = 0;
1208 if (target_len >= sizeof(canonpath)) {
1209 *is_bad_symlink = 1;
1214 * We do not use realpath(3) to resolve the symlink's target
1215 * path because we don't want to resolve symlinks recursively.
1216 * Instead we make the path absolute and then canonicalize it.
1217 * Relative symlink target lookup should begin at the directory
1218 * in which the blob object is being installed.
1220 if (!got_path_is_absolute(target_path)) {
1221 char *abspath, *parent;
1222 err = got_path_dirname(&parent, ondisk_path);
1225 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1227 return got_error_from_errno("asprintf");
1230 if (strlen(abspath) >= sizeof(canonpath)) {
1231 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1235 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1240 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1245 /* Only allow symlinks pointing at paths within the work tree. */
1246 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1247 *is_bad_symlink = 1;
1251 /* Do not allow symlinks pointing into the .got directory. */
1252 if (asprintf(&path_got, "%s/%s", wtroot_path,
1253 GOT_WORKTREE_GOT_DIR) == -1)
1254 return got_error_from_errno("asprintf");
1255 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1256 *is_bad_symlink = 1;
1262 static const struct got_error *
1263 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1264 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1265 int restoring_missing_file, int reverting_versioned_file,
1266 int path_is_unversioned, int allow_bad_symlinks,
1267 struct got_repository *repo,
1268 got_worktree_checkout_cb progress_cb, void *progress_arg)
1270 const struct got_error *err = NULL;
1271 char target_path[PATH_MAX];
1272 size_t len, target_len = 0;
1273 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1274 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1276 *is_bad_symlink = 0;
1279 * Blob object content specifies the target path of the link.
1280 * If a symbolic link cannot be installed we instead create
1281 * a regular file which contains the link target path stored
1282 * in the blob object.
1285 err = got_object_blob_read_block(&len, blob);
1289 if (len + target_len >= sizeof(target_path)) {
1290 /* Path too long; install as a regular file. */
1291 *is_bad_symlink = 1;
1292 got_object_blob_rewind(blob);
1293 return install_blob(worktree, ondisk_path, path,
1294 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1295 restoring_missing_file, reverting_versioned_file,
1296 1, path_is_unversioned, repo, progress_cb,
1300 /* Skip blob object header first time around. */
1301 memcpy(target_path + target_len, buf + hdrlen,
1303 target_len += len - hdrlen;
1307 target_path[target_len] = '\0';
1309 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1310 ondisk_path, worktree->root_path);
1314 if (*is_bad_symlink && !allow_bad_symlinks) {
1315 /* install as a regular file */
1316 got_object_blob_rewind(blob);
1317 err = install_blob(worktree, ondisk_path, path,
1318 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1319 restoring_missing_file, reverting_versioned_file, 1,
1320 path_is_unversioned, repo, progress_cb, progress_arg);
1324 if (symlink(target_path, ondisk_path) == -1) {
1325 if (errno == EEXIST) {
1326 int symlink_replaced;
1327 if (path_is_unversioned) {
1328 err = (*progress_cb)(progress_arg,
1329 GOT_STATUS_UNVERSIONED, path);
1332 err = replace_existing_symlink(&symlink_replaced,
1333 ondisk_path, target_path, target_len);
1337 if (symlink_replaced) {
1338 err = (*progress_cb)(progress_arg,
1339 reverting_versioned_file ?
1341 GOT_STATUS_UPDATE, path);
1343 err = (*progress_cb)(progress_arg,
1344 GOT_STATUS_EXISTS, path);
1347 return err; /* Nothing else to do. */
1350 if (errno == ENOENT) {
1352 err = got_path_dirname(&parent, ondisk_path);
1355 err = add_dir_on_disk(worktree, parent);
1360 * Retry, and fall through to error handling
1361 * below if this second attempt fails.
1363 if (symlink(target_path, ondisk_path) != -1) {
1364 err = NULL; /* success */
1369 /* Handle errors from first or second creation attempt. */
1370 if (errno == ENAMETOOLONG) {
1371 /* bad target path; install as a regular file */
1372 *is_bad_symlink = 1;
1373 got_object_blob_rewind(blob);
1374 err = install_blob(worktree, ondisk_path, path,
1375 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1376 restoring_missing_file, reverting_versioned_file, 1,
1377 path_is_unversioned, repo,
1378 progress_cb, progress_arg);
1379 } else if (errno == ENOTDIR) {
1380 err = got_error_path(ondisk_path,
1381 GOT_ERR_FILE_OBSTRUCTED);
1383 err = got_error_from_errno3("symlink",
1384 target_path, ondisk_path);
1386 } else if (progress_cb)
1387 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1388 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1392 static const struct got_error *
1393 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1394 const char *path, mode_t te_mode, mode_t st_mode,
1395 struct got_blob_object *blob, int restoring_missing_file,
1396 int reverting_versioned_file, int installing_bad_symlink,
1397 int path_is_unversioned, struct got_repository *repo,
1398 got_worktree_checkout_cb progress_cb, void *progress_arg)
1400 const struct got_error *err = NULL;
1404 char *tmppath = NULL;
1407 mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1408 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1411 if (errno == ENOENT) {
1413 err = got_path_dirname(&parent, path);
1416 err = add_dir_on_disk(worktree, parent);
1420 fd = open(ondisk_path,
1421 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1424 return got_error_from_errno2("open",
1426 } else if (errno == EEXIST) {
1427 if (path_is_unversioned) {
1428 err = (*progress_cb)(progress_arg,
1429 GOT_STATUS_UNVERSIONED, path);
1432 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1433 !S_ISREG(st_mode) && !installing_bad_symlink) {
1434 /* TODO file is obstructed; do something */
1435 err = got_error_path(ondisk_path,
1436 GOT_ERR_FILE_OBSTRUCTED);
1439 err = got_opentemp_named_fd(&tmppath, &fd,
1445 if (fchmod(fd, apply_umask(mode)) == -1) {
1446 err = got_error_from_errno2("fchmod",
1452 return got_error_from_errno2("open", ondisk_path);
1456 if (restoring_missing_file)
1457 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1459 else if (reverting_versioned_file)
1460 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1463 err = (*progress_cb)(progress_arg,
1464 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1469 hdrlen = got_object_blob_get_hdrlen(blob);
1471 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1472 err = got_object_blob_read_block(&len, blob);
1476 /* Skip blob object header first time around. */
1477 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1479 err = got_error_from_errno("write");
1481 } else if (outlen != len - hdrlen) {
1482 err = got_error(GOT_ERR_IO);
1489 if (fsync(fd) != 0) {
1490 err = got_error_from_errno("fsync");
1495 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1496 err = got_error_from_errno2("unlink", ondisk_path);
1499 if (rename(tmppath, ondisk_path) != 0) {
1500 err = got_error_from_errno3("rename", tmppath,
1509 if (fd != -1 && close(fd) == -1 && err == NULL)
1510 err = got_error_from_errno("close");
1511 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1512 err = got_error_from_errno2("unlink", tmppath);
1517 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1518 static const struct got_error *
1519 get_modified_file_content_status(unsigned char *status, FILE *f)
1521 const struct got_error *err = NULL;
1522 const char *markers[3] = {
1523 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1524 GOT_DIFF_CONFLICT_MARKER_SEP,
1525 GOT_DIFF_CONFLICT_MARKER_END
1529 size_t linesize = 0;
1532 while (*status == GOT_STATUS_MODIFY) {
1533 linelen = getline(&line, &linesize, f);
1534 if (linelen == -1) {
1537 err = got_ferror(f, GOT_ERR_IO);
1541 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1542 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1544 *status = GOT_STATUS_CONFLICT;
1555 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1557 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1558 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1562 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1564 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1565 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1566 ie->mtime_sec == sb->st_mtim.tv_sec &&
1567 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1568 ie->size == (sb->st_size & 0xffffffff) &&
1569 !xbit_differs(ie, sb->st_mode));
1572 static unsigned char
1573 get_staged_status(struct got_fileindex_entry *ie)
1575 switch (got_fileindex_entry_stage_get(ie)) {
1576 case GOT_FILEIDX_STAGE_ADD:
1577 return GOT_STATUS_ADD;
1578 case GOT_FILEIDX_STAGE_DELETE:
1579 return GOT_STATUS_DELETE;
1580 case GOT_FILEIDX_STAGE_MODIFY:
1581 return GOT_STATUS_MODIFY;
1583 return GOT_STATUS_NO_CHANGE;
1587 static const struct got_error *
1588 get_symlink_modification_status(unsigned char *status,
1589 struct got_fileindex_entry *ie, const char *abspath,
1590 int dirfd, const char *de_name, struct got_blob_object *blob)
1592 const struct got_error *err = NULL;
1593 char target_path[PATH_MAX];
1594 char etarget[PATH_MAX];
1596 size_t len, target_len = 0;
1597 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1598 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1600 *status = GOT_STATUS_NO_CHANGE;
1602 /* Blob object content specifies the target path of the link. */
1604 err = got_object_blob_read_block(&len, blob);
1607 if (len + target_len >= sizeof(target_path)) {
1609 * Should not happen. The blob contents were OK
1610 * when this symlink was installed.
1612 return got_error(GOT_ERR_NO_SPACE);
1615 /* Skip blob object header first time around. */
1616 memcpy(target_path + target_len, buf + hdrlen,
1618 target_len += len - hdrlen;
1622 target_path[target_len] = '\0';
1625 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1627 return got_error_from_errno2("readlinkat", abspath);
1629 elen = readlink(abspath, etarget, sizeof(etarget));
1631 return got_error_from_errno2("readlink", abspath);
1634 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1635 *status = GOT_STATUS_MODIFY;
1640 static const struct got_error *
1641 get_file_status(unsigned char *status, struct stat *sb,
1642 struct got_fileindex_entry *ie, const char *abspath,
1643 int dirfd, const char *de_name, struct got_repository *repo)
1645 const struct got_error *err = NULL;
1646 struct got_object_id id;
1648 int fd = -1, fd1 = -1;
1651 struct got_blob_object *blob = NULL;
1653 unsigned char staged_status = get_staged_status(ie);
1655 *status = GOT_STATUS_NO_CHANGE;
1656 memset(sb, 0, sizeof(*sb));
1659 * Whenever the caller provides a directory descriptor and a
1660 * directory entry name for the file, use them! This prevents
1661 * race conditions if filesystem paths change beneath our feet.
1664 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1665 if (errno == ENOENT) {
1666 if (got_fileindex_entry_has_file_on_disk(ie))
1667 *status = GOT_STATUS_MISSING;
1669 *status = GOT_STATUS_DELETE;
1672 err = got_error_from_errno2("fstatat", abspath);
1676 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1677 if (fd == -1 && errno != ENOENT &&
1678 !got_err_open_nofollow_on_symlink())
1679 return got_error_from_errno2("open", abspath);
1680 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1681 if (lstat(abspath, sb) == -1)
1682 return got_error_from_errno2("lstat", abspath);
1683 } else if (fd == -1 || fstat(fd, sb) == -1) {
1684 if (errno == ENOENT) {
1685 if (got_fileindex_entry_has_file_on_disk(ie))
1686 *status = GOT_STATUS_MISSING;
1688 *status = GOT_STATUS_DELETE;
1691 err = got_error_from_errno2("fstat", abspath);
1696 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1697 *status = GOT_STATUS_OBSTRUCTED;
1701 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1702 *status = GOT_STATUS_DELETE;
1704 } else if (!got_fileindex_entry_has_blob(ie) &&
1705 staged_status != GOT_STATUS_ADD) {
1706 *status = GOT_STATUS_ADD;
1710 if (!stat_info_differs(ie, sb))
1713 if (S_ISLNK(sb->st_mode) &&
1714 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1715 *status = GOT_STATUS_MODIFY;
1719 if (staged_status == GOT_STATUS_MODIFY ||
1720 staged_status == GOT_STATUS_ADD)
1721 memcpy(id.hash, ie->staged_blob_hash, sizeof(id.hash));
1723 memcpy(id.hash, ie->blob_hash, sizeof(id.hash));
1724 id.algo = got_repo_get_hash_algorithm(repo);
1726 fd1 = got_opentempfd();
1728 err = got_error_from_errno("got_opentempfd");
1731 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1735 if (S_ISLNK(sb->st_mode)) {
1736 err = get_symlink_modification_status(status, ie,
1737 abspath, dirfd, de_name, blob);
1742 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1744 err = got_error_from_errno2("openat", abspath);
1749 f = fdopen(fd, "r");
1751 err = got_error_from_errno2("fdopen", abspath);
1755 hdrlen = got_object_blob_get_hdrlen(blob);
1757 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1758 err = got_object_blob_read_block(&blen, blob);
1761 /* Skip length of blob object header first time around. */
1762 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1763 if (flen == 0 && ferror(f)) {
1764 err = got_error_from_errno("fread");
1767 if (blen - hdrlen == 0) {
1769 *status = GOT_STATUS_MODIFY;
1771 } else if (flen == 0) {
1772 if (blen - hdrlen != 0)
1773 *status = GOT_STATUS_MODIFY;
1775 } else if (blen - hdrlen == flen) {
1776 /* Skip blob object header first time around. */
1777 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1778 *status = GOT_STATUS_MODIFY;
1782 *status = GOT_STATUS_MODIFY;
1788 if (*status == GOT_STATUS_MODIFY) {
1790 err = get_modified_file_content_status(status, f);
1791 } else if (xbit_differs(ie, sb->st_mode))
1792 *status = GOT_STATUS_MODE_CHANGE;
1794 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1795 err = got_error_from_errno("close");
1797 got_object_blob_close(blob);
1798 if (f != NULL && fclose(f) == EOF && err == NULL)
1799 err = got_error_from_errno2("fclose", abspath);
1800 if (fd != -1 && close(fd) == -1 && err == NULL)
1801 err = got_error_from_errno2("close", abspath);
1806 * Update timestamps in the file index if a file is unmodified and
1807 * we had to run a full content comparison to find out.
1809 static const struct got_error *
1810 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1811 struct got_fileindex_entry *ie, struct stat *sb)
1813 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1814 return got_fileindex_entry_update(ie, wt_fd, path,
1815 ie->blob_hash, ie->commit_hash, 1);
1820 static const struct got_error *
1821 update_blob(struct got_worktree *worktree,
1822 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1823 struct got_tree_entry *te, const char *path,
1824 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1827 const struct got_error *err = NULL;
1828 struct got_blob_object *blob = NULL;
1829 char *ondisk_path = NULL;
1830 unsigned char status = GOT_STATUS_NO_CHANGE;
1832 int fd1 = -1, fd2 = -1;
1834 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1835 return got_error_from_errno("asprintf");
1838 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1839 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1842 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1846 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1847 sb.st_mode = got_fileindex_perms_to_st(ie);
1849 if (stat(ondisk_path, &sb) == -1) {
1850 if (errno != ENOENT) {
1851 err = got_error_from_errno2("stat",
1855 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1856 status = GOT_STATUS_UNVERSIONED;
1858 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1859 status = GOT_STATUS_UNVERSIONED;
1861 status = GOT_STATUS_OBSTRUCTED;
1865 if (status == GOT_STATUS_OBSTRUCTED) {
1867 got_fileindex_entry_mark_skipped(ie);
1868 err = (*progress_cb)(progress_arg, status, path);
1871 if (status == GOT_STATUS_CONFLICT) {
1873 got_fileindex_entry_mark_skipped(ie);
1874 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1879 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1880 (S_ISLNK(te->mode) ||
1881 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1883 * This is a regular file or an installed bad symlink.
1884 * If the file index indicates that this file is already
1885 * up-to-date with respect to the repository we can skip
1886 * updating contents of this file.
1888 if (got_fileindex_entry_has_commit(ie) &&
1889 memcmp(ie->commit_hash, worktree->base_commit_id->hash,
1890 SHA1_DIGEST_LENGTH) == 0) {
1892 err = sync_timestamps(worktree->root_fd,
1893 path, status, ie, &sb);
1896 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1900 if (got_fileindex_entry_has_blob(ie) &&
1901 memcmp(ie->blob_hash, te->id.hash,
1902 SHA1_DIGEST_LENGTH) == 0) {
1903 /* Different commit but the same blob. */
1904 err = sync_timestamps(worktree->root_fd,
1905 path, status, ie, &sb);
1908 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1914 fd1 = got_opentempfd();
1916 err = got_error_from_errno("got_opentempfd");
1919 err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
1923 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1924 int update_timestamps;
1925 struct got_blob_object *blob2 = NULL;
1926 char *label_orig = NULL;
1927 if (got_fileindex_entry_has_blob(ie)) {
1928 fd2 = got_opentempfd();
1930 err = got_error_from_errno("got_opentempfd");
1933 struct got_object_id id2;
1934 memcpy(id2.hash, ie->blob_hash, SHA1_DIGEST_LENGTH);
1935 err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
1940 if (got_fileindex_entry_has_commit(ie)) {
1941 char id_str[SHA1_DIGEST_STRING_LENGTH];
1942 if (got_sha1_digest_to_str(ie->commit_hash, id_str,
1943 sizeof(id_str)) == NULL) {
1944 err = got_error_path(id_str,
1945 GOT_ERR_BAD_OBJ_ID_STR);
1948 if (asprintf(&label_orig, "%s: commit %s",
1949 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1950 err = got_error_from_errno("asprintf");
1954 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1956 err = got_object_blob_read_to_str(&link_target, blob);
1959 err = merge_symlink(worktree, blob2, ondisk_path, path,
1960 label_orig, link_target, worktree->base_commit_id,
1961 repo, progress_cb, progress_arg);
1964 err = merge_blob(&update_timestamps, worktree, blob2,
1965 ondisk_path, path, sb.st_mode, label_orig, blob,
1966 worktree->base_commit_id, repo,
1967 progress_cb, progress_arg);
1970 if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
1971 err = got_error_from_errno("close");
1975 got_object_blob_close(blob2);
1979 * Do not update timestamps of files with local changes.
1980 * Otherwise, a future status walk would treat them as
1981 * unmodified files again.
1983 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1984 blob->id.hash, worktree->base_commit_id->hash,
1986 } else if (status == GOT_STATUS_MODE_CHANGE) {
1987 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1988 blob->id.hash, worktree->base_commit_id->hash, 0);
1989 } else if (status == GOT_STATUS_DELETE) {
1990 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1993 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1994 blob->id.hash, worktree->base_commit_id->hash, 0);
1998 int is_bad_symlink = 0;
1999 if (S_ISLNK(te->mode)) {
2000 err = install_symlink(&is_bad_symlink, worktree,
2001 ondisk_path, path, blob,
2002 status == GOT_STATUS_MISSING, 0,
2003 status == GOT_STATUS_UNVERSIONED, 0,
2004 repo, progress_cb, progress_arg);
2006 err = install_blob(worktree, ondisk_path, path,
2007 te->mode, sb.st_mode, blob,
2008 status == GOT_STATUS_MISSING, 0, 0,
2009 status == GOT_STATUS_UNVERSIONED, repo,
2010 progress_cb, progress_arg);
2016 err = got_fileindex_entry_update(ie,
2017 worktree->root_fd, path, blob->id.hash,
2018 worktree->base_commit_id->hash, 1);
2020 err = create_fileindex_entry(&ie, fileindex,
2021 worktree->base_commit_id, worktree->root_fd, path,
2027 if (is_bad_symlink) {
2028 got_fileindex_entry_filetype_set(ie,
2029 GOT_FILEIDX_MODE_BAD_SYMLINK);
2033 if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2034 err = got_error_from_errno("close");
2037 got_object_blob_close(blob);
2043 static const struct got_error *
2044 remove_ondisk_file(const char *root_path, const char *path)
2046 const struct got_error *err = NULL;
2047 char *ondisk_path = NULL, *parent = NULL;
2049 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2050 return got_error_from_errno("asprintf");
2052 if (unlink(ondisk_path) == -1) {
2053 if (errno != ENOENT)
2054 err = got_error_from_errno2("unlink", ondisk_path);
2056 size_t root_len = strlen(root_path);
2057 err = got_path_dirname(&parent, ondisk_path);
2060 while (got_path_cmp(parent, root_path,
2061 strlen(parent), root_len) != 0) {
2063 ondisk_path = parent;
2065 if (rmdir(ondisk_path) == -1) {
2066 if (errno != ENOTEMPTY)
2067 err = got_error_from_errno2("rmdir",
2071 err = got_path_dirname(&parent, ondisk_path);
2082 static const struct got_error *
2083 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2084 struct got_fileindex_entry *ie, struct got_repository *repo,
2085 got_worktree_checkout_cb progress_cb, void *progress_arg)
2087 const struct got_error *err = NULL;
2088 unsigned char status;
2092 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2093 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2095 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2097 return got_error_from_errno("asprintf");
2099 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2103 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2104 char ondisk_target[PATH_MAX];
2105 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2106 sizeof(ondisk_target));
2107 if (ondisk_len == -1) {
2108 err = got_error_from_errno2("readlink", ondisk_path);
2111 ondisk_target[ondisk_len] = '\0';
2112 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2113 NULL, NULL, /* XXX pass common ancestor info? */
2114 ondisk_target, ondisk_path);
2117 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2122 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2123 status == GOT_STATUS_ADD) {
2124 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2128 * Preserve the working file and change the deleted blob's
2129 * entry into a schedule-add entry.
2131 err = got_fileindex_entry_update(ie, worktree->root_fd,
2132 ie->path, NULL, NULL, 0);
2134 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2137 if (status == GOT_STATUS_NO_CHANGE) {
2138 err = remove_ondisk_file(worktree->root_path, ie->path);
2142 got_fileindex_entry_remove(fileindex, ie);
2149 struct diff_cb_arg {
2150 struct got_fileindex *fileindex;
2151 struct got_worktree *worktree;
2152 struct got_repository *repo;
2153 got_worktree_checkout_cb progress_cb;
2155 got_cancel_cb cancel_cb;
2159 static const struct got_error *
2160 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2161 struct got_tree_entry *te, const char *parent_path)
2163 struct diff_cb_arg *a = arg;
2165 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2166 return got_error(GOT_ERR_CANCELLED);
2168 return update_blob(a->worktree, a->fileindex, ie, te,
2169 ie->path, a->repo, a->progress_cb, a->progress_arg);
2172 static const struct got_error *
2173 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2175 struct diff_cb_arg *a = arg;
2177 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2178 return got_error(GOT_ERR_CANCELLED);
2180 return delete_blob(a->worktree, a->fileindex, ie,
2181 a->repo, a->progress_cb, a->progress_arg);
2184 static const struct got_error *
2185 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2187 struct diff_cb_arg *a = arg;
2188 const struct got_error *err;
2191 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2192 return got_error(GOT_ERR_CANCELLED);
2194 if (got_object_tree_entry_is_submodule(te))
2197 if (asprintf(&path, "%s%s%s", parent_path,
2198 parent_path[0] ? "/" : "", te->name)
2200 return got_error_from_errno("asprintf");
2202 if (S_ISDIR(te->mode))
2203 err = add_dir_on_disk(a->worktree, path);
2205 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2206 a->repo, a->progress_cb, a->progress_arg);
2212 const struct got_error *
2213 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2215 uint32_t uuid_status;
2217 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2218 if (uuid_status != uuid_s_ok) {
2220 return got_error_uuid(uuid_status, "uuid_to_string");
2226 static const struct got_error *
2227 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2229 const struct got_error *err = NULL;
2230 char *uuidstr = NULL;
2234 err = got_worktree_get_uuid(&uuidstr, worktree);
2238 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2239 err = got_error_from_errno("asprintf");
2246 const struct got_error *
2247 got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2250 return get_ref_name(refname, worktree, prefix);
2253 const struct got_error *
2254 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2256 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2259 static const struct got_error *
2260 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2262 return get_ref_name(refname, worktree,
2263 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2266 static const struct got_error *
2267 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2269 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2272 static const struct got_error *
2273 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2275 return get_ref_name(refname, worktree,
2276 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2279 static const struct got_error *
2280 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2282 return get_ref_name(refname, worktree,
2283 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2286 static const struct got_error *
2287 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2289 return get_ref_name(refname, worktree,
2290 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2293 static const struct got_error *
2294 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2296 return get_ref_name(refname, worktree,
2297 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2300 static const struct got_error *
2301 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2303 return get_ref_name(refname, worktree,
2304 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2307 static const struct got_error *
2308 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2310 return get_ref_name(refname, worktree,
2311 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2314 const struct got_error *
2315 got_worktree_get_histedit_script_path(char **path,
2316 struct got_worktree *worktree)
2318 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2319 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2321 return got_error_from_errno("asprintf");
2326 static const struct got_error *
2327 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2329 return get_ref_name(refname, worktree,
2330 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2333 static const struct got_error *
2334 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2336 return get_ref_name(refname, worktree,
2337 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2341 * Prevent Git's garbage collector from deleting our base commit by
2342 * setting a reference to our base commit's ID.
2344 static const struct got_error *
2345 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2347 const struct got_error *err = NULL;
2348 struct got_reference *ref = NULL;
2351 err = got_worktree_get_base_ref_name(&refname, worktree);
2355 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2359 err = got_ref_write(ref, repo);
2367 static const struct got_error *
2368 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2370 const struct got_error *err = NULL;
2372 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2373 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2374 err = got_error_from_errno("asprintf");
2375 *fileindex_path = NULL;
2381 static const struct got_error *
2382 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2383 struct got_worktree *worktree)
2385 const struct got_error *err = NULL;
2388 *fileindex_path = NULL;
2389 *fileindex = got_fileindex_alloc();
2390 if (*fileindex == NULL)
2391 return got_error_from_errno("got_fileindex_alloc");
2393 err = get_fileindex_path(fileindex_path, worktree);
2397 index = fopen(*fileindex_path, "rbe");
2398 if (index == NULL) {
2399 if (errno != ENOENT)
2400 err = got_error_from_errno2("fopen", *fileindex_path);
2402 err = got_fileindex_read(*fileindex, index);
2403 if (fclose(index) == EOF && err == NULL)
2404 err = got_error_from_errno("fclose");
2408 free(*fileindex_path);
2409 *fileindex_path = NULL;
2410 got_fileindex_free(*fileindex);
2416 struct bump_base_commit_id_arg {
2417 struct got_object_id *base_commit_id;
2420 const char *entry_name;
2421 got_worktree_checkout_cb progress_cb;
2425 /* Bump base commit ID of all files within an updated part of the work tree. */
2426 static const struct got_error *
2427 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2429 const struct got_error *err;
2430 struct bump_base_commit_id_arg *a = arg;
2432 if (a->entry_name) {
2433 if (strcmp(ie->path, a->path) != 0)
2435 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2438 if (got_fileindex_entry_was_skipped(ie))
2441 if (memcmp(ie->commit_hash, a->base_commit_id->hash,
2442 SHA1_DIGEST_LENGTH) == 0)
2445 if (a->progress_cb) {
2446 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2451 memcpy(ie->commit_hash, a->base_commit_id->hash, SHA1_DIGEST_LENGTH);
2455 static const struct got_error *
2456 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2457 struct got_fileindex *fileindex,
2458 got_worktree_checkout_cb progress_cb, void *progress_arg)
2460 struct bump_base_commit_id_arg bbc_arg;
2462 bbc_arg.base_commit_id = worktree->base_commit_id;
2463 bbc_arg.entry_name = NULL;
2465 bbc_arg.path_len = 0;
2466 bbc_arg.progress_cb = progress_cb;
2467 bbc_arg.progress_arg = progress_arg;
2469 return got_fileindex_for_each_entry_safe(fileindex,
2470 bump_base_commit_id, &bbc_arg);
2473 static const struct got_error *
2474 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2476 const struct got_error *err = NULL;
2477 char *new_fileindex_path = NULL;
2478 FILE *new_index = NULL;
2479 struct timespec timeout;
2481 err = got_opentemp_named(&new_fileindex_path, &new_index,
2482 fileindex_path, "");
2486 err = got_fileindex_write(fileindex, new_index);
2490 if (rename(new_fileindex_path, fileindex_path) != 0) {
2491 err = got_error_from_errno3("rename", new_fileindex_path,
2493 unlink(new_fileindex_path);
2497 * Sleep for a short amount of time to ensure that files modified after
2498 * this program exits have a different time stamp from the one which
2499 * was recorded in the file index.
2502 timeout.tv_nsec = 1;
2503 nanosleep(&timeout, NULL);
2507 free(new_fileindex_path);
2511 static const struct got_error *
2512 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2513 struct got_object_id **tree_id, const char *wt_relpath,
2514 struct got_commit_object *base_commit, struct got_worktree *worktree,
2515 struct got_repository *repo)
2517 const struct got_error *err = NULL;
2518 struct got_object_id *id = NULL;
2519 char *in_repo_path = NULL;
2520 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2522 *entry_type = GOT_OBJ_TYPE_ANY;
2523 *tree_relpath = NULL;
2526 if (wt_relpath[0] == '\0') {
2527 /* Check out all files within the work tree. */
2528 *entry_type = GOT_OBJ_TYPE_TREE;
2529 *tree_relpath = strdup("");
2530 if (*tree_relpath == NULL) {
2531 err = got_error_from_errno("strdup");
2534 err = got_object_id_by_path(tree_id, repo, base_commit,
2535 worktree->path_prefix);
2541 /* Check out a subset of files in the work tree. */
2543 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2544 is_root_wt ? "" : "/", wt_relpath) == -1) {
2545 err = got_error_from_errno("asprintf");
2549 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2554 in_repo_path = NULL;
2556 err = got_object_get_type(entry_type, repo, id);
2560 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2561 /* Check out a single file. */
2562 if (strchr(wt_relpath, '/') == NULL) {
2563 /* Check out a single file in work tree's root dir. */
2564 in_repo_path = strdup(worktree->path_prefix);
2565 if (in_repo_path == NULL) {
2566 err = got_error_from_errno("strdup");
2569 *tree_relpath = strdup("");
2570 if (*tree_relpath == NULL) {
2571 err = got_error_from_errno("strdup");
2575 /* Check out a single file in a subdirectory. */
2576 err = got_path_dirname(tree_relpath, wt_relpath);
2579 if (asprintf(&in_repo_path, "%s%s%s",
2580 worktree->path_prefix, is_root_wt ? "" : "/",
2581 *tree_relpath) == -1) {
2582 err = got_error_from_errno("asprintf");
2586 err = got_object_id_by_path(tree_id, repo,
2587 base_commit, in_repo_path);
2589 /* Check out all files within a subdirectory. */
2590 *tree_id = got_object_id_dup(id);
2591 if (*tree_id == NULL) {
2592 err = got_error_from_errno("got_object_id_dup");
2595 *tree_relpath = strdup(wt_relpath);
2596 if (*tree_relpath == NULL) {
2597 err = got_error_from_errno("strdup");
2605 *entry_type = GOT_OBJ_TYPE_ANY;
2606 free(*tree_relpath);
2607 *tree_relpath = NULL;
2614 static const struct got_error *
2615 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2616 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2617 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2618 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2620 const struct got_error *err = NULL;
2621 struct got_commit_object *commit = NULL;
2622 struct got_tree_object *tree = NULL;
2623 struct got_fileindex_diff_tree_cb diff_cb;
2624 struct diff_cb_arg arg;
2626 err = ref_base_commit(worktree, repo);
2628 if (!(err->code == GOT_ERR_ERRNO &&
2629 (errno == EACCES || errno == EROFS)))
2631 err = (*progress_cb)(progress_arg,
2632 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2637 err = got_object_open_as_commit(&commit, repo,
2638 worktree->base_commit_id);
2642 err = got_object_open_as_tree(&tree, repo, tree_id);
2647 got_object_tree_find_entry(tree, entry_name) == NULL) {
2648 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2652 diff_cb.diff_old_new = diff_old_new;
2653 diff_cb.diff_old = diff_old;
2654 diff_cb.diff_new = diff_new;
2655 arg.fileindex = fileindex;
2656 arg.worktree = worktree;
2658 arg.progress_cb = progress_cb;
2659 arg.progress_arg = progress_arg;
2660 arg.cancel_cb = cancel_cb;
2661 arg.cancel_arg = cancel_arg;
2662 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2663 entry_name, repo, &diff_cb, &arg);
2666 got_object_tree_close(tree);
2668 got_object_commit_close(commit);
2672 const struct got_error *
2673 got_worktree_checkout_files(struct got_worktree *worktree,
2674 struct got_pathlist_head *paths, struct got_repository *repo,
2675 got_worktree_checkout_cb progress_cb, void *progress_arg,
2676 got_cancel_cb cancel_cb, void *cancel_arg)
2678 const struct got_error *err = NULL, *sync_err, *unlockerr;
2679 struct got_commit_object *commit = NULL;
2680 struct got_tree_object *tree = NULL;
2681 struct got_fileindex *fileindex = NULL;
2682 char *fileindex_path = NULL;
2683 struct got_pathlist_entry *pe;
2684 struct tree_path_data {
2685 STAILQ_ENTRY(tree_path_data) entry;
2686 struct got_object_id *tree_id;
2691 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2693 STAILQ_INIT(&tree_paths);
2695 err = lock_worktree(worktree, LOCK_EX);
2699 err = got_object_open_as_commit(&commit, repo,
2700 worktree->base_commit_id);
2704 /* Map all specified paths to in-repository trees. */
2705 TAILQ_FOREACH(pe, paths, entry) {
2706 tpd = malloc(sizeof(*tpd));
2708 err = got_error_from_errno("malloc");
2712 err = find_tree_entry_for_checkout(&tpd->entry_type,
2713 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2720 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2721 err = got_path_basename(&tpd->entry_name, pe->path);
2729 tpd->entry_name = NULL;
2731 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2735 * Read the file index.
2736 * Checking out files is supposed to be an idempotent operation.
2737 * If the on-disk file index is incomplete we will try to complete it.
2739 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2743 tpd = STAILQ_FIRST(&tree_paths);
2744 TAILQ_FOREACH(pe, paths, entry) {
2745 struct bump_base_commit_id_arg bbc_arg;
2747 err = checkout_files(worktree, fileindex, tpd->relpath,
2748 tpd->tree_id, tpd->entry_name, repo,
2749 progress_cb, progress_arg, cancel_cb, cancel_arg);
2753 bbc_arg.base_commit_id = worktree->base_commit_id;
2754 bbc_arg.entry_name = tpd->entry_name;
2755 bbc_arg.path = pe->path;
2756 bbc_arg.path_len = pe->path_len;
2757 bbc_arg.progress_cb = progress_cb;
2758 bbc_arg.progress_arg = progress_arg;
2759 err = got_fileindex_for_each_entry_safe(fileindex,
2760 bump_base_commit_id, &bbc_arg);
2764 tpd = STAILQ_NEXT(tpd, entry);
2766 sync_err = sync_fileindex(fileindex, fileindex_path);
2767 if (sync_err && err == NULL)
2770 free(fileindex_path);
2772 got_object_tree_close(tree);
2774 got_object_commit_close(commit);
2776 got_fileindex_free(fileindex);
2777 while (!STAILQ_EMPTY(&tree_paths)) {
2778 tpd = STAILQ_FIRST(&tree_paths);
2779 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2784 unlockerr = lock_worktree(worktree, LOCK_SH);
2785 if (unlockerr && err == NULL)
2790 struct merge_file_cb_arg {
2791 struct got_worktree *worktree;
2792 struct got_fileindex *fileindex;
2793 got_worktree_checkout_cb progress_cb;
2795 got_cancel_cb cancel_cb;
2797 const char *label_orig;
2798 struct got_object_id *commit_id2;
2799 int allow_bad_symlinks;
2802 static const struct got_error *
2803 merge_file_cb(void *arg, struct got_blob_object *blob1,
2804 struct got_blob_object *blob2, FILE *f1, FILE *f2,
2805 struct got_object_id *id1, struct got_object_id *id2,
2806 const char *path1, const char *path2,
2807 mode_t mode1, mode_t mode2, struct got_repository *repo)
2809 static const struct got_error *err = NULL;
2810 struct merge_file_cb_arg *a = arg;
2811 struct got_fileindex_entry *ie;
2812 char *ondisk_path = NULL;
2814 unsigned char status;
2815 int local_changes_subsumed;
2816 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2817 char *id_str = NULL, *label_deriv2 = NULL;
2819 if (blob1 && blob2) {
2820 ie = got_fileindex_entry_get(a->fileindex, path2,
2823 return (*a->progress_cb)(a->progress_arg,
2824 GOT_STATUS_MISSING, path2);
2826 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2828 return got_error_from_errno("asprintf");
2830 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2835 if (status == GOT_STATUS_DELETE) {
2836 err = (*a->progress_cb)(a->progress_arg,
2837 GOT_STATUS_MERGE, path2);
2840 if (status != GOT_STATUS_NO_CHANGE &&
2841 status != GOT_STATUS_MODIFY &&
2842 status != GOT_STATUS_CONFLICT &&
2843 status != GOT_STATUS_ADD) {
2844 err = (*a->progress_cb)(a->progress_arg, status, path2);
2848 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2850 err = got_object_blob_read_to_str(&link_target2, blob2);
2853 err = merge_symlink(a->worktree, blob1, ondisk_path,
2854 path2, a->label_orig, link_target2, a->commit_id2,
2855 repo, a->progress_cb, a->progress_arg);
2860 f_orig = got_opentemp();
2861 if (f_orig == NULL) {
2862 err = got_error_from_errno("got_opentemp");
2865 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2870 f_deriv2 = got_opentemp();
2871 if (f_deriv2 == NULL)
2873 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2878 fd = open(ondisk_path,
2879 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2881 err = got_error_from_errno2("open",
2885 f_deriv = fdopen(fd, "r");
2886 if (f_deriv == NULL) {
2887 err = got_error_from_errno2("fdopen",
2892 err = got_object_id_str(&id_str, a->commit_id2);
2895 if (asprintf(&label_deriv2, "%s: commit %s",
2896 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2897 err = got_error_from_errno("asprintf");
2900 err = merge_file(&local_changes_subsumed, a->worktree,
2901 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2902 mode2, a->label_orig, NULL, label_deriv2,
2903 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2904 a->progress_cb, a->progress_arg);
2907 ie = got_fileindex_entry_get(a->fileindex, path1,
2910 return (*a->progress_cb)(a->progress_arg,
2911 GOT_STATUS_MISSING, path1);
2913 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2915 return got_error_from_errno("asprintf");
2917 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2923 case GOT_STATUS_NO_CHANGE:
2924 err = (*a->progress_cb)(a->progress_arg,
2925 GOT_STATUS_DELETE, path1);
2928 err = remove_ondisk_file(a->worktree->root_path, path1);
2932 got_fileindex_entry_mark_deleted_from_disk(ie);
2934 case GOT_STATUS_DELETE:
2935 case GOT_STATUS_MISSING:
2936 err = (*a->progress_cb)(a->progress_arg,
2937 GOT_STATUS_DELETE, path1);
2941 got_fileindex_entry_mark_deleted_from_disk(ie);
2943 case GOT_STATUS_ADD: {
2944 struct got_object_id *id;
2948 * Delete the added file only if its content already
2949 * exists in the repository.
2951 err = got_object_blob_file_create(&id, &blob1_f,
2952 &blob1_size, path1);
2955 if (got_object_id_cmp(id, id1) == 0) {
2956 err = (*a->progress_cb)(a->progress_arg,
2957 GOT_STATUS_DELETE, path1);
2960 err = remove_ondisk_file(a->worktree->root_path,
2965 got_fileindex_entry_remove(a->fileindex,
2968 err = (*a->progress_cb)(a->progress_arg,
2969 GOT_STATUS_CANNOT_DELETE, path1);
2971 if (fclose(blob1_f) == EOF && err == NULL)
2972 err = got_error_from_errno("fclose");
2978 case GOT_STATUS_MODIFY:
2979 case GOT_STATUS_CONFLICT:
2980 err = (*a->progress_cb)(a->progress_arg,
2981 GOT_STATUS_CANNOT_DELETE, path1);
2985 case GOT_STATUS_OBSTRUCTED:
2986 err = (*a->progress_cb)(a->progress_arg, status, path1);
2994 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2996 return got_error_from_errno("asprintf");
2997 ie = got_fileindex_entry_get(a->fileindex, path2,
3000 err = get_file_status(&status, &sb, ie, ondisk_path,
3004 if (status != GOT_STATUS_NO_CHANGE &&
3005 status != GOT_STATUS_MODIFY &&
3006 status != GOT_STATUS_CONFLICT &&
3007 status != GOT_STATUS_ADD) {
3008 err = (*a->progress_cb)(a->progress_arg,
3012 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3014 err = got_object_blob_read_to_str(&link_target2,
3018 err = merge_symlink(a->worktree, NULL,
3019 ondisk_path, path2, a->label_orig,
3020 link_target2, a->commit_id2, repo,
3021 a->progress_cb, a->progress_arg);
3023 } else if (S_ISREG(sb.st_mode)) {
3024 err = merge_blob(&local_changes_subsumed,
3025 a->worktree, NULL, ondisk_path, path2,
3026 sb.st_mode, a->label_orig, blob2,
3027 a->commit_id2, repo, a->progress_cb,
3030 err = got_error_path(ondisk_path,
3031 GOT_ERR_FILE_OBSTRUCTED);
3035 if (status == GOT_STATUS_DELETE) {
3036 err = got_fileindex_entry_update(ie,
3037 a->worktree->root_fd, path2, blob2->id.hash,
3038 a->worktree->base_commit_id->hash, 0);
3043 int is_bad_symlink = 0;
3044 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3045 if (S_ISLNK(mode2)) {
3046 err = install_symlink(&is_bad_symlink,
3047 a->worktree, ondisk_path, path2, blob2, 0,
3048 0, 1, a->allow_bad_symlinks, repo,
3049 a->progress_cb, a->progress_arg);
3051 err = install_blob(a->worktree, ondisk_path, path2,
3052 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3053 a->progress_cb, a->progress_arg);
3057 err = got_fileindex_entry_alloc(&ie, path2);
3060 err = got_fileindex_entry_update(ie,
3061 a->worktree->root_fd, path2, NULL, NULL, 1);
3063 got_fileindex_entry_free(ie);
3066 err = got_fileindex_entry_add(a->fileindex, ie);
3068 got_fileindex_entry_free(ie);
3071 if (is_bad_symlink) {
3072 got_fileindex_entry_filetype_set(ie,
3073 GOT_FILEIDX_MODE_BAD_SYMLINK);
3078 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3079 err = got_error_from_errno("fclose");
3080 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3081 err = got_error_from_errno("fclose");
3082 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3083 err = got_error_from_errno("fclose");
3090 static const struct got_error *
3091 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3093 struct got_worktree *worktree = arg;
3095 /* Reject merges into a work tree with mixed base commits. */
3096 if (got_fileindex_entry_has_commit(ie) &&
3097 memcmp(ie->commit_hash, worktree->base_commit_id->hash,
3098 SHA1_DIGEST_LENGTH) != 0)
3099 return got_error(GOT_ERR_MIXED_COMMITS);
3104 struct check_merge_conflicts_arg {
3105 struct got_worktree *worktree;
3106 struct got_fileindex *fileindex;
3107 struct got_repository *repo;
3110 static const struct got_error *
3111 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3112 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3113 struct got_object_id *id1, struct got_object_id *id2,
3114 const char *path1, const char *path2,
3115 mode_t mode1, mode_t mode2, struct got_repository *repo)
3117 const struct got_error *err = NULL;
3118 struct check_merge_conflicts_arg *a = arg;
3119 unsigned char status;
3121 struct got_fileindex_entry *ie;
3122 const char *path = path2 ? path2 : path1;
3123 struct got_object_id *id = id2 ? id2 : id1;
3129 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3133 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3135 return got_error_from_errno("asprintf");
3137 /* Reject merges into a work tree with conflicted files. */
3138 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3142 if (status == GOT_STATUS_CONFLICT)
3143 return got_error(GOT_ERR_CONFLICTS);
3148 static const struct got_error *
3149 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3150 const char *fileindex_path, struct got_object_id *commit_id1,
3151 struct got_object_id *commit_id2, struct got_repository *repo,
3152 got_worktree_checkout_cb progress_cb, void *progress_arg,
3153 got_cancel_cb cancel_cb, void *cancel_arg)
3155 const struct got_error *err = NULL, *sync_err;
3156 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3157 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3158 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3159 struct check_merge_conflicts_arg cmc_arg;
3160 struct merge_file_cb_arg arg;
3161 char *label_orig = NULL;
3162 FILE *f1 = NULL, *f2 = NULL;
3163 int fd1 = -1, fd2 = -1;
3166 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3169 err = got_object_id_by_path(&tree_id1, repo, commit1,
3170 worktree->path_prefix);
3171 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3177 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3181 err = got_object_id_str(&id_str, commit_id1);
3185 if (asprintf(&label_orig, "%s: commit %s",
3186 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3187 err = got_error_from_errno("asprintf");
3193 f1 = got_opentemp();
3195 err = got_error_from_errno("got_opentemp");
3200 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3204 err = got_object_id_by_path(&tree_id2, repo, commit2,
3205 worktree->path_prefix);
3209 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3213 f2 = got_opentemp();
3215 err = got_error_from_errno("got_opentemp");
3219 fd1 = got_opentempfd();
3221 err = got_error_from_errno("got_opentempfd");
3225 fd2 = got_opentempfd();
3227 err = got_error_from_errno("got_opentempfd");
3231 cmc_arg.worktree = worktree;
3232 cmc_arg.fileindex = fileindex;
3233 cmc_arg.repo = repo;
3234 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3235 check_merge_conflicts, &cmc_arg, 0);
3239 arg.worktree = worktree;
3240 arg.fileindex = fileindex;
3241 arg.progress_cb = progress_cb;
3242 arg.progress_arg = progress_arg;
3243 arg.cancel_cb = cancel_cb;
3244 arg.cancel_arg = cancel_arg;
3245 arg.label_orig = label_orig;
3246 arg.commit_id2 = commit_id2;
3247 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3248 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3249 merge_file_cb, &arg, 1);
3250 sync_err = sync_fileindex(fileindex, fileindex_path);
3251 if (sync_err && err == NULL)
3255 got_object_commit_close(commit1);
3257 got_object_commit_close(commit2);
3259 got_object_tree_close(tree1);
3261 got_object_tree_close(tree2);
3262 if (f1 && fclose(f1) == EOF && err == NULL)
3263 err = got_error_from_errno("fclose");
3264 if (f2 && fclose(f2) == EOF && err == NULL)
3265 err = got_error_from_errno("fclose");
3266 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3267 err = got_error_from_errno("close");
3268 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3269 err = got_error_from_errno("close");
3274 const struct got_error *
3275 got_worktree_merge_files(struct got_worktree *worktree,
3276 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3277 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3278 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3280 const struct got_error *err, *unlockerr;
3281 char *fileindex_path = NULL;
3282 struct got_fileindex *fileindex = NULL;
3284 err = lock_worktree(worktree, LOCK_EX);
3288 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3292 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3297 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3298 commit_id2, repo, progress_cb, progress_arg,
3299 cancel_cb, cancel_arg);
3302 got_fileindex_free(fileindex);
3303 free(fileindex_path);
3304 unlockerr = lock_worktree(worktree, LOCK_SH);
3305 if (unlockerr && err == NULL)
3310 struct diff_dir_cb_arg {
3311 struct got_fileindex *fileindex;
3312 struct got_worktree *worktree;
3313 const char *status_path;
3314 size_t status_path_len;
3315 struct got_repository *repo;
3316 got_worktree_status_cb status_cb;
3318 got_cancel_cb cancel_cb;
3320 /* A pathlist containing per-directory pathlists of ignore patterns. */
3321 struct got_pathlist_head *ignores;
3322 int report_unchanged;
3326 static const struct got_error *
3327 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3328 int dirfd, const char *de_name,
3329 got_worktree_status_cb status_cb, void *status_arg,
3330 struct got_repository *repo, int report_unchanged)
3332 const struct got_error *err = NULL;
3333 unsigned char status = GOT_STATUS_NO_CHANGE;
3334 unsigned char staged_status = get_staged_status(ie);
3336 struct got_object_id blob_id, commit_id, staged_blob_id;
3337 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3338 struct got_object_id *staged_blob_idp = NULL;
3340 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3344 if (status == GOT_STATUS_NO_CHANGE &&
3345 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3348 if (got_fileindex_entry_has_blob(ie)) {
3349 memcpy(blob_id.hash, ie->blob_hash, SHA1_DIGEST_LENGTH);
3350 blob_idp = &blob_id;
3352 if (got_fileindex_entry_has_commit(ie)) {
3353 memcpy(commit_id.hash, ie->commit_hash, SHA1_DIGEST_LENGTH);
3354 commit_idp = &commit_id;
3356 if (staged_status == GOT_STATUS_ADD ||
3357 staged_status == GOT_STATUS_MODIFY) {
3358 memcpy(staged_blob_id.hash, ie->staged_blob_hash,
3359 SHA1_DIGEST_LENGTH);
3360 staged_blob_idp = &staged_blob_id;
3363 return (*status_cb)(status_arg, status, staged_status,
3364 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3367 static const struct got_error *
3368 status_old_new(void *arg, struct got_fileindex_entry *ie,
3369 struct dirent *de, const char *parent_path, int dirfd)
3371 const struct got_error *err = NULL;
3372 struct diff_dir_cb_arg *a = arg;
3375 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3376 return got_error(GOT_ERR_CANCELLED);
3378 if (got_path_cmp(parent_path, a->status_path,
3379 strlen(parent_path), a->status_path_len) != 0 &&
3380 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3383 if (parent_path[0]) {
3384 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3385 parent_path, de->d_name) == -1)
3386 return got_error_from_errno("asprintf");
3388 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3390 return got_error_from_errno("asprintf");
3393 err = report_file_status(ie, abspath, dirfd, de->d_name,
3394 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3399 static const struct got_error *
3400 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3402 struct diff_dir_cb_arg *a = arg;
3403 struct got_object_id blob_id, commit_id;
3404 unsigned char status;
3406 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3407 return got_error(GOT_ERR_CANCELLED);
3409 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3412 memcpy(blob_id.hash, ie->blob_hash, SHA1_DIGEST_LENGTH);
3413 memcpy(commit_id.hash, ie->commit_hash, SHA1_DIGEST_LENGTH);
3414 if (got_fileindex_entry_has_file_on_disk(ie))
3415 status = GOT_STATUS_MISSING;
3417 status = GOT_STATUS_DELETE;
3418 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3419 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3423 free_ignores(struct got_pathlist_head *ignores)
3425 struct got_pathlist_entry *pe;
3427 TAILQ_FOREACH(pe, ignores, entry) {
3428 struct got_pathlist_head *ignorelist = pe->data;
3430 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3432 got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3435 static const struct got_error *
3436 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3438 const struct got_error *err = NULL;
3439 struct got_pathlist_entry *pe = NULL;
3440 struct got_pathlist_head *ignorelist;
3441 char *line = NULL, *pattern, *dirpath = NULL;
3442 size_t linesize = 0;
3445 ignorelist = calloc(1, sizeof(*ignorelist));
3446 if (ignorelist == NULL)
3447 return got_error_from_errno("calloc");
3448 TAILQ_INIT(ignorelist);
3450 while ((linelen = getline(&line, &linesize, f)) != -1) {
3451 if (linelen > 0 && line[linelen - 1] == '\n')
3452 line[linelen - 1] = '\0';
3454 /* Git's ignores may contain comments. */
3458 /* Git's negated patterns are not (yet?) supported. */
3462 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3464 err = got_error_from_errno("asprintf");
3467 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3472 err = got_error_from_errno("getline");
3476 dirpath = strdup(path);
3477 if (dirpath == NULL) {
3478 err = got_error_from_errno("strdup");
3481 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3484 if (err || pe == NULL) {
3486 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3492 match_ignores(struct got_pathlist_head *ignores, const char *path)
3494 struct got_pathlist_entry *pe;
3496 /* Handle patterns which match in all directories. */
3497 TAILQ_FOREACH(pe, ignores, entry) {
3498 struct got_pathlist_head *ignorelist = pe->data;
3499 struct got_pathlist_entry *pi;
3501 TAILQ_FOREACH(pi, ignorelist, entry) {
3502 const char *p, *pattern = pi->path;
3504 if (strncmp(pattern, "**/", 3) != 0)
3509 if (fnmatch(pattern, p,
3510 FNM_PATHNAME | FNM_LEADING_DIR)) {
3511 /* Retry in next directory. */
3512 while (*p && *p != '/')
3524 * The ignores pathlist contains ignore lists from children before
3525 * parents, so we can find the most specific ignorelist by walking
3526 * ignores backwards.
3528 pe = TAILQ_LAST(ignores, got_pathlist_head);
3530 if (got_path_is_child(path, pe->path, pe->path_len)) {
3531 struct got_pathlist_head *ignorelist = pe->data;
3532 struct got_pathlist_entry *pi;
3533 TAILQ_FOREACH(pi, ignorelist, entry) {
3534 const char *pattern = pi->path;
3535 int flags = FNM_LEADING_DIR;
3536 if (strstr(pattern, "/**/") == NULL)
3537 flags |= FNM_PATHNAME;
3538 if (fnmatch(pattern, path, flags))
3543 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3549 static const struct got_error *
3550 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3551 const char *path, int dirfd, const char *ignores_filename)
3553 const struct got_error *err = NULL;
3556 FILE *ignoresfile = NULL;
3558 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3559 path[0] ? "/" : "", ignores_filename) == -1)
3560 return got_error_from_errno("asprintf");
3563 fd = openat(dirfd, ignores_filename,
3564 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3566 if (errno != ENOENT && errno != EACCES)
3567 err = got_error_from_errno2("openat",
3570 ignoresfile = fdopen(fd, "r");
3571 if (ignoresfile == NULL)
3572 err = got_error_from_errno2("fdopen",
3576 err = read_ignores(ignores, path, ignoresfile);
3580 ignoresfile = fopen(ignorespath, "re");
3581 if (ignoresfile == NULL) {
3582 if (errno != ENOENT && errno != EACCES)
3583 err = got_error_from_errno2("fopen",
3586 err = read_ignores(ignores, path, ignoresfile);
3589 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3590 err = got_error_from_errno2("fclose", path);
3591 if (fd != -1 && close(fd) == -1 && err == NULL)
3592 err = got_error_from_errno2("close", path);
3597 static const struct got_error *
3598 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3601 const struct got_error *err = NULL;
3602 struct diff_dir_cb_arg *a = arg;
3608 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3609 return got_error(GOT_ERR_CANCELLED);
3611 if (parent_path[0]) {
3612 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3613 return got_error_from_errno("asprintf");
3618 if (de->d_type == DT_DIR) {
3619 if (!a->no_ignores && ignore != NULL &&
3620 match_ignores(a->ignores, path))
3622 } else if (!match_ignores(a->ignores, path) &&
3623 got_path_is_child(path, a->status_path, a->status_path_len))
3624 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3625 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3631 static const struct got_error *
3632 status_traverse(void *arg, const char *path, int dirfd)
3634 const struct got_error *err = NULL;
3635 struct diff_dir_cb_arg *a = arg;
3640 err = add_ignores(a->ignores, a->worktree->root_path,
3641 path, dirfd, ".cvsignore");
3645 err = add_ignores(a->ignores, a->worktree->root_path, path,
3646 dirfd, ".gitignore");
3651 static const struct got_error *
3652 report_single_file_status(const char *path, const char *ondisk_path,
3653 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3654 void *status_arg, struct got_repository *repo, int report_unchanged,
3655 struct got_pathlist_head *ignores, int no_ignores)
3657 struct got_fileindex_entry *ie;
3660 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3662 return report_file_status(ie, ondisk_path, -1, NULL,
3663 status_cb, status_arg, repo, report_unchanged);
3665 if (lstat(ondisk_path, &sb) == -1) {
3666 if (errno != ENOENT)
3667 return got_error_from_errno2("lstat", ondisk_path);
3668 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3669 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3672 if (!no_ignores && match_ignores(ignores, path))
3675 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3676 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3677 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3682 static const struct got_error *
3683 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3684 const char *root_path, const char *path)
3686 const struct got_error *err;
3687 char *parent_path, *next_parent_path = NULL;
3689 err = add_ignores(ignores, root_path, "", -1,
3694 err = add_ignores(ignores, root_path, "", -1,
3699 err = got_path_dirname(&parent_path, path);
3701 if (err->code == GOT_ERR_BAD_PATH)
3702 return NULL; /* cannot traverse parent */
3706 err = add_ignores(ignores, root_path, parent_path, -1,
3710 err = add_ignores(ignores, root_path, parent_path, -1,
3714 err = got_path_dirname(&next_parent_path, parent_path);
3716 if (err->code == GOT_ERR_BAD_PATH)
3717 err = NULL; /* traversed everything */
3720 if (got_path_is_root_dir(parent_path))
3723 parent_path = next_parent_path;
3724 next_parent_path = NULL;
3728 free(next_parent_path);
3732 static const struct got_error *
3733 worktree_status(struct got_worktree *worktree, const char *path,
3734 struct got_fileindex *fileindex, struct got_repository *repo,
3735 got_worktree_status_cb status_cb, void *status_arg,
3736 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3737 int report_unchanged)
3739 const struct got_error *err = NULL;
3741 struct got_fileindex_diff_dir_cb fdiff_cb;
3742 struct diff_dir_cb_arg arg;
3743 char *ondisk_path = NULL;
3744 struct got_pathlist_head ignores;
3745 struct got_fileindex_entry *ie;
3747 TAILQ_INIT(&ignores);
3749 if (asprintf(&ondisk_path, "%s%s%s",
3750 worktree->root_path, path[0] ? "/" : "", path) == -1)
3751 return got_error_from_errno("asprintf");
3753 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3755 err = report_single_file_status(path, ondisk_path,
3756 fileindex, status_cb, status_arg, repo,
3757 report_unchanged, &ignores, no_ignores);
3761 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3763 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3764 !got_err_open_nofollow_on_symlink())
3765 err = got_error_from_errno2("open", ondisk_path);
3768 err = add_ignores_from_parent_paths(&ignores,
3769 worktree->root_path, ondisk_path);
3773 err = report_single_file_status(path, ondisk_path,
3774 fileindex, status_cb, status_arg, repo,
3775 report_unchanged, &ignores, no_ignores);
3778 fdiff_cb.diff_old_new = status_old_new;
3779 fdiff_cb.diff_old = status_old;
3780 fdiff_cb.diff_new = status_new;
3781 fdiff_cb.diff_traverse = status_traverse;
3782 arg.fileindex = fileindex;
3783 arg.worktree = worktree;
3784 arg.status_path = path;
3785 arg.status_path_len = strlen(path);
3787 arg.status_cb = status_cb;
3788 arg.status_arg = status_arg;
3789 arg.cancel_cb = cancel_cb;
3790 arg.cancel_arg = cancel_arg;
3791 arg.report_unchanged = report_unchanged;
3792 arg.no_ignores = no_ignores;
3794 err = add_ignores_from_parent_paths(&ignores,
3795 worktree->root_path, path);
3799 arg.ignores = &ignores;
3800 err = got_fileindex_diff_dir(fileindex, fd,
3801 worktree->root_path, path, repo, &fdiff_cb, &arg);
3804 free_ignores(&ignores);
3805 if (fd != -1 && close(fd) == -1 && err == NULL)
3806 err = got_error_from_errno("close");
3811 const struct got_error *
3812 got_worktree_status(struct got_worktree *worktree,
3813 struct got_pathlist_head *paths, struct got_repository *repo,
3814 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3815 got_cancel_cb cancel_cb, void *cancel_arg)
3817 const struct got_error *err = NULL;
3818 char *fileindex_path = NULL;
3819 struct got_fileindex *fileindex = NULL;
3820 struct got_pathlist_entry *pe;
3822 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3826 TAILQ_FOREACH(pe, paths, entry) {
3827 err = worktree_status(worktree, pe->path, fileindex, repo,
3828 status_cb, status_arg, cancel_cb, cancel_arg,
3833 free(fileindex_path);
3834 got_fileindex_free(fileindex);
3838 const struct got_error *
3839 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3842 const struct got_error *err = NULL;
3843 char *resolved = NULL, *cwd = NULL, *path = NULL;
3846 char *abspath = NULL;
3847 char canonpath[PATH_MAX];
3851 cwd = getcwd(NULL, 0);
3853 return got_error_from_errno("getcwd");
3855 if (lstat(arg, &sb) == -1) {
3856 if (errno != ENOENT) {
3857 err = got_error_from_errno2("lstat", arg);
3862 if (S_ISLNK(sb.st_mode)) {
3864 * We cannot use realpath(3) with symlinks since we want to
3865 * operate on the symlink itself.
3866 * But we can make the path absolute, assuming it is relative
3867 * to the current working directory, and then canonicalize it.
3869 if (!got_path_is_absolute(arg)) {
3870 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3871 err = got_error_from_errno("asprintf");
3876 err = got_canonpath(abspath ? abspath : arg, canonpath,
3880 resolved = strdup(canonpath);
3881 if (resolved == NULL) {
3882 err = got_error_from_errno("strdup");
3886 resolved = realpath(arg, NULL);
3887 if (resolved == NULL) {
3888 if (errno != ENOENT) {
3889 err = got_error_from_errno2("realpath", arg);
3892 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3893 err = got_error_from_errno("asprintf");
3896 err = got_canonpath(abspath, canonpath,
3900 resolved = strdup(canonpath);
3901 if (resolved == NULL) {
3902 err = got_error_from_errno("strdup");
3908 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3909 strlen(got_worktree_get_root_path(worktree)))) {
3910 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3914 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3915 err = got_path_skip_common_ancestor(&path,
3916 got_worktree_get_root_path(worktree), resolved);
3922 err = got_error_from_errno("strdup");
3927 /* XXX status walk can't deal with trailing slash! */
3929 while (len > 0 && path[len - 1] == '/') {
3930 path[len - 1] = '\0';
3944 struct schedule_addition_args {
3945 struct got_worktree *worktree;
3946 struct got_fileindex *fileindex;
3947 got_worktree_checkout_cb progress_cb;
3949 struct got_repository *repo;
3952 static const struct got_error *
3953 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3954 const char *relpath, struct got_object_id *blob_id,
3955 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3956 int dirfd, const char *de_name)
3958 struct schedule_addition_args *a = arg;
3959 const struct got_error *err = NULL;
3960 struct got_fileindex_entry *ie;
3964 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3966 return got_error_from_errno("asprintf");
3968 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3970 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3974 /* Re-adding an existing entry is a no-op. */
3975 if (status == GOT_STATUS_ADD)
3977 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3982 if (status != GOT_STATUS_UNVERSIONED) {
3983 if (status == GOT_STATUS_NONEXISTENT)
3984 err = got_error_set_errno(ENOENT, ondisk_path);
3986 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3990 err = got_fileindex_entry_alloc(&ie, relpath);
3993 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3994 relpath, NULL, NULL, 1);
3996 got_fileindex_entry_free(ie);
3999 err = got_fileindex_entry_add(a->fileindex, ie);
4001 got_fileindex_entry_free(ie);
4008 if (status == GOT_STATUS_ADD)
4010 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4013 const struct got_error *
4014 got_worktree_schedule_add(struct got_worktree *worktree,
4015 struct got_pathlist_head *paths,
4016 got_worktree_checkout_cb progress_cb, void *progress_arg,
4017 struct got_repository *repo, int no_ignores)
4019 struct got_fileindex *fileindex = NULL;
4020 char *fileindex_path = NULL;
4021 const struct got_error *err = NULL, *sync_err, *unlockerr;
4022 struct got_pathlist_entry *pe;
4023 struct schedule_addition_args saa;
4025 err = lock_worktree(worktree, LOCK_EX);
4029 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4033 saa.worktree = worktree;
4034 saa.fileindex = fileindex;
4035 saa.progress_cb = progress_cb;
4036 saa.progress_arg = progress_arg;
4039 TAILQ_FOREACH(pe, paths, entry) {
4040 err = worktree_status(worktree, pe->path, fileindex, repo,
4041 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4045 sync_err = sync_fileindex(fileindex, fileindex_path);
4046 if (sync_err && err == NULL)
4049 free(fileindex_path);
4051 got_fileindex_free(fileindex);
4052 unlockerr = lock_worktree(worktree, LOCK_SH);
4053 if (unlockerr && err == NULL)
4058 struct schedule_deletion_args {
4059 struct got_worktree *worktree;
4060 struct got_fileindex *fileindex;
4061 got_worktree_delete_cb progress_cb;
4063 struct got_repository *repo;
4064 int delete_local_mods;
4066 int ignore_missing_paths;
4067 const char *status_codes;
4070 static const struct got_error *
4071 schedule_for_deletion(void *arg, unsigned char status,
4072 unsigned char staged_status, const char *relpath,
4073 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4074 struct got_object_id *commit_id, int dirfd, const char *de_name)
4076 struct schedule_deletion_args *a = arg;
4077 const struct got_error *err = NULL;
4078 struct got_fileindex_entry *ie = NULL;
4082 if (status == GOT_STATUS_NONEXISTENT) {
4083 if (a->ignore_missing_paths)
4085 return got_error_set_errno(ENOENT, relpath);
4088 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4090 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4092 staged_status = get_staged_status(ie);
4093 if (staged_status != GOT_STATUS_NO_CHANGE) {
4094 if (staged_status == GOT_STATUS_DELETE)
4096 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4099 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4101 return got_error_from_errno("asprintf");
4103 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4108 if (a->status_codes) {
4109 size_t ncodes = strlen(a->status_codes);
4111 for (i = 0; i < ncodes ; i++) {
4112 if (status == a->status_codes[i])
4116 /* Do not delete files in non-matching status. */
4120 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4121 a->status_codes[i] != GOT_STATUS_MISSING) {
4122 static char msg[64];
4123 snprintf(msg, sizeof(msg),
4124 "invalid status code '%c'", a->status_codes[i]);
4125 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4130 if (status != GOT_STATUS_NO_CHANGE) {
4131 if (status == GOT_STATUS_DELETE)
4133 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4134 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4137 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4138 err = got_error_set_errno(ENOENT, relpath);
4141 if (status != GOT_STATUS_MODIFY &&
4142 status != GOT_STATUS_MISSING) {
4143 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4148 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4152 if (unlinkat(dirfd, de_name, 0) == -1) {
4153 err = got_error_from_errno2("unlinkat",
4157 } else if (unlink(ondisk_path) == -1) {
4158 err = got_error_from_errno2("unlink", ondisk_path);
4162 root_len = strlen(a->worktree->root_path);
4165 err = got_path_dirname(&parent, ondisk_path);
4169 ondisk_path = parent;
4170 if (rmdir(ondisk_path) == -1) {
4171 if (errno != ENOTEMPTY)
4172 err = got_error_from_errno2("rmdir",
4176 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4177 strlen(ondisk_path), root_len) != 0);
4180 got_fileindex_entry_mark_deleted_from_disk(ie);
4185 if (status == GOT_STATUS_DELETE)
4187 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4188 staged_status, relpath);
4191 const struct got_error *
4192 got_worktree_schedule_delete(struct got_worktree *worktree,
4193 struct got_pathlist_head *paths, int delete_local_mods,
4194 const char *status_codes,
4195 got_worktree_delete_cb progress_cb, void *progress_arg,
4196 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4198 struct got_fileindex *fileindex = NULL;
4199 char *fileindex_path = NULL;
4200 const struct got_error *err = NULL, *sync_err, *unlockerr;
4201 struct got_pathlist_entry *pe;
4202 struct schedule_deletion_args sda;
4204 err = lock_worktree(worktree, LOCK_EX);
4208 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4212 sda.worktree = worktree;
4213 sda.fileindex = fileindex;
4214 sda.progress_cb = progress_cb;
4215 sda.progress_arg = progress_arg;
4217 sda.delete_local_mods = delete_local_mods;
4218 sda.keep_on_disk = keep_on_disk;
4219 sda.ignore_missing_paths = ignore_missing_paths;
4220 sda.status_codes = status_codes;
4222 TAILQ_FOREACH(pe, paths, entry) {
4223 err = worktree_status(worktree, pe->path, fileindex, repo,
4224 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4228 sync_err = sync_fileindex(fileindex, fileindex_path);
4229 if (sync_err && err == NULL)
4232 free(fileindex_path);
4234 got_fileindex_free(fileindex);
4235 unlockerr = lock_worktree(worktree, LOCK_SH);
4236 if (unlockerr && err == NULL)
4241 static const struct got_error *
4242 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4244 const struct got_error *err = NULL;
4246 size_t linesize = 0, n;
4249 linelen = getline(&line, &linesize, infile);
4250 if (linelen == -1) {
4251 if (ferror(infile)) {
4252 err = got_error_from_errno("getline");
4258 n = fwrite(line, 1, linelen, outfile);
4260 err = got_ferror(outfile, GOT_ERR_IO);
4265 n = fwrite(line, 1, linelen, rejectfile);
4267 err = got_ferror(rejectfile, GOT_ERR_IO);
4274 static const struct got_error *
4275 skip_one_line(FILE *f)
4278 size_t linesize = 0;
4281 linelen = getline(&line, &linesize, f);
4282 if (linelen == -1) {
4284 return got_error_from_errno("getline");
4291 static const struct got_error *
4292 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4293 int start_old, int end_old, int start_new, int end_new,
4294 FILE *outfile, FILE *rejectfile)
4296 const struct got_error *err;
4298 /* Copy old file's lines leading up to patch. */
4299 while (!feof(f1) && *line_cur1 < start_old) {
4300 err = copy_one_line(f1, outfile, NULL);
4305 /* Skip new file's lines leading up to patch. */
4306 while (!feof(f2) && *line_cur2 < start_new) {
4308 err = copy_one_line(f2, NULL, rejectfile);
4310 err = skip_one_line(f2);
4315 /* Copy patched lines. */
4316 while (!feof(f2) && *line_cur2 <= end_new) {
4317 err = copy_one_line(f2, outfile, NULL);
4322 /* Skip over old file's replaced lines. */
4323 while (!feof(f1) && *line_cur1 <= end_old) {
4325 err = copy_one_line(f1, NULL, rejectfile);
4327 err = skip_one_line(f1);
4336 static const struct got_error *
4337 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4338 FILE *outfile, FILE *rejectfile)
4340 const struct got_error *err;
4343 /* Copy old file's lines until EOF. */
4345 err = copy_one_line(f1, outfile, NULL);
4352 /* Copy new file's lines until EOF. */
4354 err = copy_one_line(f2, NULL, rejectfile);
4364 static const struct got_error *
4365 apply_or_reject_change(int *choice, int *nchunks_used,
4366 struct diff_result *diff_result, int n,
4367 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4368 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4369 got_worktree_patch_cb patch_cb, void *patch_arg)
4371 const struct got_error *err = NULL;
4372 struct diff_chunk_context cc = {};
4373 int start_old, end_old, start_new, end_new;
4375 struct diff_output_unidiff_state *diff_state;
4376 struct diff_input_info diff_info;
4379 *choice = GOT_PATCH_CHOICE_NONE;
4381 /* Get changed line numbers without context lines for copy_change(). */
4382 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4383 start_old = cc.left.start;
4384 end_old = cc.left.end;
4385 start_new = cc.right.start;
4386 end_new = cc.right.end;
4388 /* Get the same change with context lines for display. */
4389 memset(&cc, 0, sizeof(cc));
4390 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4392 memset(&diff_info, 0, sizeof(diff_info));
4393 diff_info.left_path = relpath;
4394 diff_info.right_path = relpath;
4396 diff_state = diff_output_unidiff_state_alloc();
4397 if (diff_state == NULL)
4398 return got_error_set_errno(ENOMEM,
4399 "diff_output_unidiff_state_alloc");
4401 hunkfile = got_opentemp();
4402 if (hunkfile == NULL) {
4403 err = got_error_from_errno("got_opentemp");
4407 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4409 if (rc != DIFF_RC_OK) {
4410 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4414 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4415 err = got_ferror(hunkfile, GOT_ERR_IO);
4419 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4420 hunkfile, changeno, nchanges);
4425 case GOT_PATCH_CHOICE_YES:
4426 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4427 end_old, start_new, end_new, outfile, rejectfile);
4429 case GOT_PATCH_CHOICE_NO:
4430 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4431 end_old, start_new, end_new, rejectfile, outfile);
4433 case GOT_PATCH_CHOICE_QUIT:
4436 err = got_error(GOT_ERR_PATCH_CHOICE);
4440 diff_output_unidiff_state_free(diff_state);
4441 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4442 err = got_error_from_errno("fclose");
4446 struct revert_file_args {
4447 struct got_worktree *worktree;
4448 struct got_fileindex *fileindex;
4449 got_worktree_checkout_cb progress_cb;
4451 got_worktree_patch_cb patch_cb;
4453 struct got_repository *repo;
4454 int unlink_added_files;
4457 static const struct got_error *
4458 create_patched_content(char **path_outfile, int reverse_patch,
4459 struct got_object_id *blob_id, const char *path2,
4460 int dirfd2, const char *de_name2,
4461 const char *relpath, struct got_repository *repo,
4462 got_worktree_patch_cb patch_cb, void *patch_arg)
4464 const struct got_error *err, *free_err;
4465 struct got_blob_object *blob = NULL;
4466 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4467 int fd = -1, fd2 = -1;
4468 char link_target[PATH_MAX];
4469 ssize_t link_len = 0;
4470 char *path1 = NULL, *id_str = NULL;
4472 struct got_diffreg_result *diffreg_result = NULL;
4473 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4474 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4476 *path_outfile = NULL;
4478 err = got_object_id_str(&id_str, blob_id);
4483 fd2 = openat(dirfd2, de_name2,
4484 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4486 if (!got_err_open_nofollow_on_symlink()) {
4487 err = got_error_from_errno2("openat", path2);
4490 link_len = readlinkat(dirfd2, de_name2,
4491 link_target, sizeof(link_target));
4492 if (link_len == -1) {
4493 return got_error_from_errno2("readlinkat",
4496 sb2.st_mode = S_IFLNK;
4497 sb2.st_size = link_len;
4500 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4502 if (!got_err_open_nofollow_on_symlink()) {
4503 err = got_error_from_errno2("open", path2);
4506 link_len = readlink(path2, link_target,
4507 sizeof(link_target));
4509 return got_error_from_errno2("readlink", path2);
4510 sb2.st_mode = S_IFLNK;
4511 sb2.st_size = link_len;
4515 if (fstat(fd2, &sb2) == -1) {
4516 err = got_error_from_errno2("fstat", path2);
4520 f2 = fdopen(fd2, "r");
4522 err = got_error_from_errno2("fdopen", path2);
4528 f2 = got_opentemp();
4530 err = got_error_from_errno2("got_opentemp", path2);
4533 n = fwrite(link_target, 1, link_len, f2);
4534 if (n != link_len) {
4535 err = got_ferror(f2, GOT_ERR_IO);
4538 if (fflush(f2) == EOF) {
4539 err = got_error_from_errno("fflush");
4545 fd = got_opentempfd();
4547 err = got_error_from_errno("got_opentempfd");
4551 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4555 err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4559 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4563 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4564 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4568 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4573 if (fseek(f1, 0L, SEEK_SET) == -1)
4574 return got_ferror(f1, GOT_ERR_IO);
4575 if (fseek(f2, 0L, SEEK_SET) == -1)
4576 return got_ferror(f2, GOT_ERR_IO);
4578 /* Count the number of actual changes in the diff result. */
4579 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4580 struct diff_chunk_context cc = {};
4581 diff_chunk_context_load_change(&cc, &nchunks_used,
4582 diffreg_result->result, n, 0);
4585 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4587 err = apply_or_reject_change(&choice, &nchunks_used,
4588 diffreg_result->result, n, relpath, f1, f2,
4589 &line_cur1, &line_cur2,
4590 reverse_patch ? NULL : outfile,
4591 reverse_patch ? outfile : NULL,
4592 ++i, nchanges, patch_cb, patch_arg);
4595 if (choice == GOT_PATCH_CHOICE_YES)
4597 else if (choice == GOT_PATCH_CHOICE_QUIT)
4601 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4602 reverse_patch ? NULL : outfile,
4603 reverse_patch ? outfile : NULL);
4607 if (!S_ISLNK(sb2.st_mode)) {
4610 mode = apply_umask(sb2.st_mode);
4611 if (fchmod(fileno(outfile), mode) == -1) {
4612 err = got_error_from_errno2("fchmod", path2);
4619 if (fd != -1 && close(fd) == -1 && err == NULL)
4620 err = got_error_from_errno("close");
4622 got_object_blob_close(blob);
4623 free_err = got_diffreg_result_free(diffreg_result);
4626 if (f1 && fclose(f1) == EOF && err == NULL)
4627 err = got_error_from_errno2("fclose", path1);
4628 if (f2 && fclose(f2) == EOF && err == NULL)
4629 err = got_error_from_errno2("fclose", path2);
4630 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4631 err = got_error_from_errno2("close", path2);
4632 if (outfile && fclose(outfile) == EOF && err == NULL)
4633 err = got_error_from_errno2("fclose", *path_outfile);
4634 if (path1 && unlink(path1) == -1 && err == NULL)
4635 err = got_error_from_errno2("unlink", path1);
4636 if (err || !have_content) {
4637 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4638 err = got_error_from_errno2("unlink", *path_outfile);
4639 free(*path_outfile);
4640 *path_outfile = NULL;
4646 static const struct got_error *
4647 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4648 const char *relpath, struct got_object_id *blob_id,
4649 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4650 int dirfd, const char *de_name)
4652 struct revert_file_args *a = arg;
4653 const struct got_error *err = NULL;
4654 char *parent_path = NULL;
4655 struct got_fileindex_entry *ie;
4656 struct got_commit_object *base_commit = NULL;
4657 struct got_tree_object *tree = NULL;
4658 struct got_object_id *tree_id = NULL;
4659 const struct got_tree_entry *te = NULL;
4660 char *tree_path = NULL, *te_name;
4661 char *ondisk_path = NULL, *path_content = NULL;
4662 struct got_blob_object *blob = NULL;
4665 /* Reverting a staged deletion is a no-op. */
4666 if (status == GOT_STATUS_DELETE &&
4667 staged_status != GOT_STATUS_NO_CHANGE)
4670 if (status == GOT_STATUS_UNVERSIONED)
4671 return (*a->progress_cb)(a->progress_arg,
4672 GOT_STATUS_UNVERSIONED, relpath);
4674 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4676 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4678 /* Construct in-repository path of tree which contains this blob. */
4679 err = got_path_dirname(&parent_path, ie->path);
4681 if (err->code != GOT_ERR_BAD_PATH)
4683 parent_path = strdup("/");
4684 if (parent_path == NULL) {
4685 err = got_error_from_errno("strdup");
4689 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4690 tree_path = strdup(parent_path);
4691 if (tree_path == NULL) {
4692 err = got_error_from_errno("strdup");
4696 if (got_path_is_root_dir(parent_path)) {
4697 tree_path = strdup(a->worktree->path_prefix);
4698 if (tree_path == NULL) {
4699 err = got_error_from_errno("strdup");
4703 if (asprintf(&tree_path, "%s/%s",
4704 a->worktree->path_prefix, parent_path) == -1) {
4705 err = got_error_from_errno("asprintf");
4711 err = got_object_open_as_commit(&base_commit, a->repo,
4712 a->worktree->base_commit_id);
4716 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4718 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4719 (status == GOT_STATUS_ADD ||
4720 staged_status == GOT_STATUS_ADD)))
4723 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4727 err = got_path_basename(&te_name, ie->path);
4731 te = got_object_tree_find_entry(tree, te_name);
4733 if (te == NULL && status != GOT_STATUS_ADD &&
4734 staged_status != GOT_STATUS_ADD) {
4735 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4741 case GOT_STATUS_ADD:
4743 int choice = GOT_PATCH_CHOICE_NONE;
4744 err = (*a->patch_cb)(&choice, a->patch_arg,
4745 status, ie->path, NULL, 1, 1);
4748 if (choice != GOT_PATCH_CHOICE_YES)
4751 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4755 got_fileindex_entry_remove(a->fileindex, ie);
4756 if (a->unlink_added_files) {
4757 if (asprintf(&ondisk_path, "%s/%s",
4758 got_worktree_get_root_path(a->worktree),
4760 err = got_error_from_errno("asprintf");
4763 if (unlink(ondisk_path) == -1) {
4764 err = got_error_from_errno2("unlink",
4770 case GOT_STATUS_DELETE:
4772 int choice = GOT_PATCH_CHOICE_NONE;
4773 err = (*a->patch_cb)(&choice, a->patch_arg,
4774 status, ie->path, NULL, 1, 1);
4777 if (choice != GOT_PATCH_CHOICE_YES)
4781 case GOT_STATUS_MODIFY:
4782 case GOT_STATUS_MODE_CHANGE:
4783 case GOT_STATUS_CONFLICT:
4784 case GOT_STATUS_MISSING: {
4785 struct got_object_id id;
4786 if (staged_status == GOT_STATUS_ADD ||
4787 staged_status == GOT_STATUS_MODIFY) {
4788 memcpy(id.hash, ie->staged_blob_hash,
4789 SHA1_DIGEST_LENGTH);
4791 memcpy(id.hash, ie->blob_hash,
4792 SHA1_DIGEST_LENGTH);
4793 fd = got_opentempfd();
4795 err = got_error_from_errno("got_opentempfd");
4799 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4803 if (asprintf(&ondisk_path, "%s/%s",
4804 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4805 err = got_error_from_errno("asprintf");
4809 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4810 status == GOT_STATUS_CONFLICT)) {
4811 int is_bad_symlink = 0;
4812 err = create_patched_content(&path_content, 1, &id,
4813 ondisk_path, dirfd, de_name, ie->path, a->repo,
4814 a->patch_cb, a->patch_arg);
4815 if (err || path_content == NULL)
4817 if (te && S_ISLNK(te->mode)) {
4818 if (unlink(path_content) == -1) {
4819 err = got_error_from_errno2("unlink",
4823 err = install_symlink(&is_bad_symlink,
4824 a->worktree, ondisk_path, ie->path,
4825 blob, 0, 1, 0, 0, a->repo,
4826 a->progress_cb, a->progress_arg);
4828 if (rename(path_content, ondisk_path) == -1) {
4829 err = got_error_from_errno3("rename",
4830 path_content, ondisk_path);
4835 int is_bad_symlink = 0;
4836 if (te && S_ISLNK(te->mode)) {
4837 err = install_symlink(&is_bad_symlink,
4838 a->worktree, ondisk_path, ie->path,
4839 blob, 0, 1, 0, 0, a->repo,
4840 a->progress_cb, a->progress_arg);
4842 err = install_blob(a->worktree, ondisk_path,
4844 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4845 got_fileindex_perms_to_st(ie), blob,
4846 0, 1, 0, 0, a->repo,
4847 a->progress_cb, a->progress_arg);
4851 if (status == GOT_STATUS_DELETE ||
4852 status == GOT_STATUS_MODE_CHANGE) {
4853 err = got_fileindex_entry_update(ie,
4854 a->worktree->root_fd, relpath,
4856 a->worktree->base_commit_id->hash, 1);
4860 if (is_bad_symlink) {
4861 got_fileindex_entry_filetype_set(ie,
4862 GOT_FILEIDX_MODE_BAD_SYMLINK);
4875 if (fd != -1 && close(fd) == -1 && err == NULL)
4876 err = got_error_from_errno("close");
4878 got_object_blob_close(blob);
4880 got_object_tree_close(tree);
4883 got_object_commit_close(base_commit);
4887 const struct got_error *
4888 got_worktree_revert(struct got_worktree *worktree,
4889 struct got_pathlist_head *paths,
4890 got_worktree_checkout_cb progress_cb, void *progress_arg,
4891 got_worktree_patch_cb patch_cb, void *patch_arg,
4892 struct got_repository *repo)
4894 struct got_fileindex *fileindex = NULL;
4895 char *fileindex_path = NULL;
4896 const struct got_error *err = NULL, *unlockerr = NULL;
4897 const struct got_error *sync_err = NULL;
4898 struct got_pathlist_entry *pe;
4899 struct revert_file_args rfa;
4901 err = lock_worktree(worktree, LOCK_EX);
4905 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4909 rfa.worktree = worktree;
4910 rfa.fileindex = fileindex;
4911 rfa.progress_cb = progress_cb;
4912 rfa.progress_arg = progress_arg;
4913 rfa.patch_cb = patch_cb;
4914 rfa.patch_arg = patch_arg;
4916 rfa.unlink_added_files = 0;
4917 TAILQ_FOREACH(pe, paths, entry) {
4918 err = worktree_status(worktree, pe->path, fileindex, repo,
4919 revert_file, &rfa, NULL, NULL, 1, 0);
4923 sync_err = sync_fileindex(fileindex, fileindex_path);
4924 if (sync_err && err == NULL)
4927 free(fileindex_path);
4929 got_fileindex_free(fileindex);
4930 unlockerr = lock_worktree(worktree, LOCK_SH);
4931 if (unlockerr && err == NULL)
4937 free_commitable(struct got_commitable *ct)
4940 free(ct->in_repo_path);
4941 free(ct->ondisk_path);
4943 free(ct->base_blob_id);
4944 free(ct->staged_blob_id);
4945 free(ct->base_commit_id);
4949 struct collect_commitables_arg {
4950 struct got_pathlist_head *commitable_paths;
4951 struct got_repository *repo;
4952 struct got_worktree *worktree;
4953 struct got_fileindex *fileindex;
4954 int have_staged_files;
4955 int allow_bad_symlinks;
4956 int diff_header_shown;
4963 * Create a file which contains the target path of a symlink so we can feed
4964 * it as content to the diff engine.
4966 static const struct got_error *
4967 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4968 const char *abspath)
4970 const struct got_error *err = NULL;
4971 char target_path[PATH_MAX];
4972 ssize_t target_len, outlen;
4977 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4978 if (target_len == -1)
4979 return got_error_from_errno2("readlinkat", abspath);
4981 target_len = readlink(abspath, target_path, PATH_MAX);
4982 if (target_len == -1)
4983 return got_error_from_errno2("readlink", abspath);
4986 *fd = got_opentempfd();
4988 return got_error_from_errno("got_opentempfd");
4990 outlen = write(*fd, target_path, target_len);
4992 err = got_error_from_errno("got_opentempfd");
4996 if (lseek(*fd, 0, SEEK_SET) == -1) {
4997 err = got_error_from_errno2("lseek", abspath);
5008 static const struct got_error *
5009 append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5010 FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5011 int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5013 const struct got_error *err = NULL;
5014 struct got_blob_object *blob1 = NULL;
5015 int fd = -1, fd1 = -1, fd2 = -1;
5016 FILE *ondisk_file = NULL;
5017 char *label1 = NULL;
5021 char *id_str = NULL;
5023 memset(&sb, 0, sizeof(sb));
5026 if (ct->staged_status != GOT_STATUS_MODIFY &&
5027 ct->staged_status != GOT_STATUS_ADD &&
5028 ct->staged_status != GOT_STATUS_DELETE)
5031 if (ct->status != GOT_STATUS_MODIFY &&
5032 ct->status != GOT_STATUS_ADD &&
5033 ct->status != GOT_STATUS_DELETE)
5037 err = got_opentemp_truncate(f1);
5039 return got_error_from_errno("got_opentemp_truncate");
5040 err = got_opentemp_truncate(f2);
5042 return got_error_from_errno("got_opentemp_truncate");
5044 if (!*diff_header_shown) {
5045 err = got_object_id_str(&id_str, worktree->base_commit_id);
5048 fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5049 got_worktree_get_root_path(worktree));
5050 fprintf(diff_outfile, "commit - %s\n", id_str);
5051 fprintf(diff_outfile, "path + %s%s\n",
5052 got_worktree_get_root_path(worktree),
5053 diff_staged ? " (staged changes)" : "");
5054 *diff_header_shown = 1;
5058 const char *label1 = NULL, *label2 = NULL;
5059 switch (ct->staged_status) {
5060 case GOT_STATUS_MODIFY:
5064 case GOT_STATUS_ADD:
5067 case GOT_STATUS_DELETE:
5071 return got_error(GOT_ERR_FILE_STATUS);
5073 fd1 = got_opentempfd();
5075 err = got_error_from_errno("got_opentempfd");
5078 fd2 = got_opentempfd();
5080 err = got_error_from_errno("got_opentempfd");
5083 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5084 fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5085 label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5086 NULL, repo, diff_outfile);
5090 fd1 = got_opentempfd();
5092 err = got_error_from_errno("got_opentempfd");
5096 if (ct->status != GOT_STATUS_ADD) {
5097 err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5103 if (ct->status != GOT_STATUS_DELETE) {
5105 fd = openat(dirfd, de_name,
5106 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5108 if (!got_err_open_nofollow_on_symlink()) {
5109 err = got_error_from_errno2("openat",
5113 err = get_symlink_target_file(&fd, dirfd,
5114 de_name, ct->ondisk_path);
5119 fd = open(ct->ondisk_path,
5120 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5122 if (!got_err_open_nofollow_on_symlink()) {
5123 err = got_error_from_errno2("open",
5127 err = get_symlink_target_file(&fd, dirfd,
5128 de_name, ct->ondisk_path);
5133 if (fstatat(fd, ct->ondisk_path, &sb,
5134 AT_SYMLINK_NOFOLLOW) == -1) {
5135 err = got_error_from_errno2("fstatat", ct->ondisk_path);
5138 ondisk_file = fdopen(fd, "r");
5139 if (ondisk_file == NULL) {
5140 err = got_error_from_errno2("fdopen", ct->ondisk_path);
5148 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5154 err = got_diff_blob_file(blob1, f1, size1, label1,
5155 ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5156 GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5158 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5159 err = got_error_from_errno("close");
5160 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5161 err = got_error_from_errno("close");
5163 got_object_blob_close(blob1);
5164 if (fd != -1 && close(fd) == -1 && err == NULL)
5165 err = got_error_from_errno("close");
5166 if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5167 err = got_error_from_errno("fclose");
5171 static const struct got_error *
5172 collect_commitables(void *arg, unsigned char status,
5173 unsigned char staged_status, const char *relpath,
5174 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5175 struct got_object_id *commit_id, int dirfd, const char *de_name)
5177 struct collect_commitables_arg *a = arg;
5178 const struct got_error *err = NULL;
5179 struct got_commitable *ct = NULL;
5180 struct got_pathlist_entry *new = NULL;
5181 char *parent_path = NULL, *path = NULL;
5184 if (a->have_staged_files) {
5185 if (staged_status != GOT_STATUS_MODIFY &&
5186 staged_status != GOT_STATUS_ADD &&
5187 staged_status != GOT_STATUS_DELETE)
5190 if (status == GOT_STATUS_CONFLICT)
5191 return got_error(GOT_ERR_COMMIT_CONFLICT);
5193 if (status != GOT_STATUS_MODIFY &&
5194 status != GOT_STATUS_MODE_CHANGE &&
5195 status != GOT_STATUS_ADD &&
5196 status != GOT_STATUS_DELETE)
5200 if (asprintf(&path, "/%s", relpath) == -1) {
5201 err = got_error_from_errno("asprintf");
5204 if (strcmp(path, "/") == 0) {
5205 parent_path = strdup("");
5206 if (parent_path == NULL)
5207 return got_error_from_errno("strdup");
5209 err = got_path_dirname(&parent_path, path);
5214 ct = calloc(1, sizeof(*ct));
5216 err = got_error_from_errno("calloc");
5220 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5222 err = got_error_from_errno("asprintf");
5226 if (staged_status == GOT_STATUS_ADD ||
5227 staged_status == GOT_STATUS_MODIFY) {
5228 struct got_fileindex_entry *ie;
5229 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5230 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5231 case GOT_FILEIDX_MODE_REGULAR_FILE:
5232 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5235 case GOT_FILEIDX_MODE_SYMLINK:
5239 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5242 ct->mode |= got_fileindex_entry_perms_get(ie);
5243 } else if (status != GOT_STATUS_DELETE &&
5244 staged_status != GOT_STATUS_DELETE) {
5246 if (fstatat(dirfd, de_name, &sb,
5247 AT_SYMLINK_NOFOLLOW) == -1) {
5248 err = got_error_from_errno2("fstatat",
5252 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5253 err = got_error_from_errno2("lstat", ct->ondisk_path);
5256 ct->mode = sb.st_mode;
5259 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5260 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5262 err = got_error_from_errno("asprintf");
5266 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5267 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5269 char target_path[PATH_MAX];
5271 target_len = readlink(ct->ondisk_path, target_path,
5272 sizeof(target_path));
5273 if (target_len == -1) {
5274 err = got_error_from_errno2("readlink",
5278 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5279 target_len, ct->ondisk_path, a->worktree->root_path);
5282 if (is_bad_symlink) {
5283 err = got_error_path(ct->ondisk_path,
5284 GOT_ERR_BAD_SYMLINK);
5290 ct->status = status;
5291 ct->staged_status = staged_status;
5292 ct->blob_id = NULL; /* will be filled in when blob gets created */
5293 if (ct->status != GOT_STATUS_ADD &&
5294 ct->staged_status != GOT_STATUS_ADD) {
5295 ct->base_blob_id = got_object_id_dup(blob_id);
5296 if (ct->base_blob_id == NULL) {
5297 err = got_error_from_errno("got_object_id_dup");
5300 ct->base_commit_id = got_object_id_dup(commit_id);
5301 if (ct->base_commit_id == NULL) {
5302 err = got_error_from_errno("got_object_id_dup");
5306 if (ct->staged_status == GOT_STATUS_ADD ||
5307 ct->staged_status == GOT_STATUS_MODIFY) {
5308 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5309 if (ct->staged_blob_id == NULL) {
5310 err = got_error_from_errno("got_object_id_dup");
5314 ct->path = strdup(path);
5315 if (ct->path == NULL) {
5316 err = got_error_from_errno("strdup");
5319 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5323 if (a->diff_outfile && ct && new != NULL) {
5324 err = append_ct_diff(ct, &a->diff_header_shown,
5325 a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5326 a->have_staged_files, a->repo, a->worktree);
5331 if (ct && (err || new == NULL))
5332 free_commitable(ct);
5338 static const struct got_error *write_tree(struct got_object_id **, int *,
5339 struct got_tree_object *, const char *, struct got_pathlist_head *,
5340 got_worktree_status_cb status_cb, void *status_arg,
5341 struct got_repository *);
5343 static const struct got_error *
5344 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5345 struct got_tree_entry *te, const char *parent_path,
5346 struct got_pathlist_head *commitable_paths,
5347 got_worktree_status_cb status_cb, void *status_arg,
5348 struct got_repository *repo)
5350 const struct got_error *err = NULL;
5351 struct got_tree_object *subtree;
5354 if (asprintf(&subpath, "%s%s%s", parent_path,
5355 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5356 return got_error_from_errno("asprintf");
5358 err = got_object_open_as_tree(&subtree, repo, &te->id);
5362 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5363 commitable_paths, status_cb, status_arg, repo);
5364 got_object_tree_close(subtree);
5369 static const struct got_error *
5370 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5372 const struct got_error *err = NULL;
5373 char *ct_parent_path = NULL;
5377 if (strchr(ct->in_repo_path, '/') == NULL) {
5378 *match = got_path_is_root_dir(path);
5382 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5385 *match = (strcmp(path, ct_parent_path) == 0);
5386 free(ct_parent_path);
5391 get_ct_file_mode(struct got_commitable *ct)
5393 if (S_ISLNK(ct->mode))
5396 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5399 static const struct got_error *
5400 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5401 struct got_tree_entry *te, struct got_commitable *ct)
5403 const struct got_error *err = NULL;
5407 err = got_object_tree_entry_dup(new_te, te);
5411 (*new_te)->mode = get_ct_file_mode(ct);
5413 if (ct->staged_status == GOT_STATUS_MODIFY)
5414 memcpy(&(*new_te)->id, ct->staged_blob_id,
5415 sizeof((*new_te)->id));
5417 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5419 if (err && *new_te) {
5426 static const struct got_error *
5427 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5428 struct got_commitable *ct)
5430 const struct got_error *err = NULL;
5431 char *ct_name = NULL;
5435 *new_te = calloc(1, sizeof(**new_te));
5436 if (*new_te == NULL)
5437 return got_error_from_errno("calloc");
5439 err = got_path_basename(&ct_name, ct->path);
5442 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5443 sizeof((*new_te)->name)) {
5444 err = got_error(GOT_ERR_NO_SPACE);
5448 (*new_te)->mode = get_ct_file_mode(ct);
5450 if (ct->staged_status == GOT_STATUS_ADD)
5451 memcpy(&(*new_te)->id, ct->staged_blob_id,
5452 sizeof((*new_te)->id));
5454 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5457 if (err && *new_te) {
5464 static const struct got_error *
5465 insert_tree_entry(struct got_tree_entry *new_te,
5466 struct got_pathlist_head *paths)
5468 const struct got_error *err = NULL;
5469 struct got_pathlist_entry *new_pe;
5471 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5475 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5479 static const struct got_error *
5480 report_ct_status(struct got_commitable *ct,
5481 got_worktree_status_cb status_cb, void *status_arg)
5483 const char *ct_path = ct->path;
5484 unsigned char status;
5486 if (status_cb == NULL) /* no commit progress output desired */
5489 while (ct_path[0] == '/')
5492 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5493 status = ct->staged_status;
5495 status = ct->status;
5497 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5498 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5501 static const struct got_error *
5502 match_modified_subtree(int *modified, struct got_tree_entry *te,
5503 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5505 const struct got_error *err = NULL;
5506 struct got_pathlist_entry *pe;
5511 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5512 got_path_is_root_dir(base_tree_path) ? "" : "/",
5514 return got_error_from_errno("asprintf");
5516 TAILQ_FOREACH(pe, commitable_paths, entry) {
5517 struct got_commitable *ct = pe->data;
5518 *modified = got_path_is_child(ct->in_repo_path, te_path,
5528 static const struct got_error *
5529 match_deleted_or_modified_ct(struct got_commitable **ctp,
5530 struct got_tree_entry *te, const char *base_tree_path,
5531 struct got_pathlist_head *commitable_paths)
5533 const struct got_error *err = NULL;
5534 struct got_pathlist_entry *pe;
5538 TAILQ_FOREACH(pe, commitable_paths, entry) {
5539 struct got_commitable *ct = pe->data;
5540 char *ct_name = NULL;
5543 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5544 if (ct->status != GOT_STATUS_MODIFY &&
5545 ct->status != GOT_STATUS_MODE_CHANGE &&
5546 ct->status != GOT_STATUS_DELETE)
5549 if (ct->staged_status != GOT_STATUS_MODIFY &&
5550 ct->staged_status != GOT_STATUS_DELETE)
5554 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5557 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5563 err = got_path_basename(&ct_name, pe->path);
5567 if (strcmp(te->name, ct_name) != 0) {
5580 static const struct got_error *
5581 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5582 const char *child_path, const char *path_base_tree,
5583 struct got_pathlist_head *commitable_paths,
5584 got_worktree_status_cb status_cb, void *status_arg,
5585 struct got_repository *repo)
5587 const struct got_error *err = NULL;
5588 struct got_tree_entry *new_te;
5590 struct got_object_id *id = NULL;
5595 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5596 got_path_is_root_dir(path_base_tree) ? "" : "/",
5598 return got_error_from_errno("asprintf");
5600 new_te = calloc(1, sizeof(*new_te));
5602 return got_error_from_errno("calloc");
5603 new_te->mode = S_IFDIR;
5605 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5606 sizeof(new_te->name)) {
5607 err = got_error(GOT_ERR_NO_SPACE);
5610 err = write_tree(&id, &nentries, NULL, subtree_path,
5611 commitable_paths, status_cb, status_arg, repo);
5616 memcpy(&new_te->id, id, sizeof(new_te->id));
5625 static const struct got_error *
5626 write_tree(struct got_object_id **new_tree_id, int *nentries,
5627 struct got_tree_object *base_tree, const char *path_base_tree,
5628 struct got_pathlist_head *commitable_paths,
5629 got_worktree_status_cb status_cb, void *status_arg,
5630 struct got_repository *repo)
5632 const struct got_error *err = NULL;
5633 struct got_pathlist_head paths;
5634 struct got_tree_entry *te, *new_te = NULL;
5635 struct got_pathlist_entry *pe;
5640 /* Insert, and recurse into, newly added entries first. */
5641 TAILQ_FOREACH(pe, commitable_paths, entry) {
5642 struct got_commitable *ct = pe->data;
5643 char *child_path = NULL, *slash;
5645 if ((ct->status != GOT_STATUS_ADD &&
5646 ct->staged_status != GOT_STATUS_ADD) ||
5647 (ct->flags & GOT_COMMITABLE_ADDED))
5650 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5651 strlen(path_base_tree)))
5654 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5659 slash = strchr(child_path, '/');
5660 if (slash == NULL) {
5661 err = alloc_added_blob_tree_entry(&new_te, ct);
5664 err = report_ct_status(ct, status_cb, status_arg);
5667 ct->flags |= GOT_COMMITABLE_ADDED;
5668 err = insert_tree_entry(new_te, &paths);
5673 *slash = '\0'; /* trim trailing path components */
5674 if (base_tree == NULL ||
5675 got_object_tree_find_entry(base_tree, child_path)
5677 err = make_subtree_for_added_blob(&new_te,
5678 child_path, path_base_tree,
5679 commitable_paths, status_cb, status_arg,
5683 err = insert_tree_entry(new_te, &paths);
5692 int i, nbase_entries;
5693 /* Handle modified and deleted entries. */
5694 nbase_entries = got_object_tree_get_nentries(base_tree);
5695 for (i = 0; i < nbase_entries; i++) {
5696 struct got_commitable *ct = NULL;
5698 te = got_object_tree_get_entry(base_tree, i);
5699 if (got_object_tree_entry_is_submodule(te)) {
5700 /* Entry is a submodule; just copy it. */
5701 err = got_object_tree_entry_dup(&new_te, te);
5704 err = insert_tree_entry(new_te, &paths);
5711 if (S_ISDIR(te->mode)) {
5713 err = got_object_tree_entry_dup(&new_te, te);
5716 err = match_modified_subtree(&modified, te,
5717 path_base_tree, commitable_paths);
5720 /* Avoid recursion into unmodified subtrees. */
5722 struct got_object_id *new_id;
5724 err = write_subtree(&new_id,
5726 path_base_tree, commitable_paths,
5727 status_cb, status_arg, repo);
5730 if (nsubentries == 0) {
5731 /* All entries were deleted. */
5735 memcpy(&new_te->id, new_id,
5736 sizeof(new_te->id));
5739 err = insert_tree_entry(new_te, &paths);
5746 err = match_deleted_or_modified_ct(&ct, te,
5747 path_base_tree, commitable_paths);
5751 /* NB: Deleted entries get dropped here. */
5752 if (ct->status == GOT_STATUS_MODIFY ||
5753 ct->status == GOT_STATUS_MODE_CHANGE ||
5754 ct->staged_status == GOT_STATUS_MODIFY) {
5755 err = alloc_modified_blob_tree_entry(
5759 err = insert_tree_entry(new_te, &paths);
5764 err = report_ct_status(ct, status_cb,
5769 /* Entry is unchanged; just copy it. */
5770 err = got_object_tree_entry_dup(&new_te, te);
5773 err = insert_tree_entry(new_te, &paths);
5781 /* Write new list of entries; deleted entries have been dropped. */
5782 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5784 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
5788 static const struct got_error *
5789 update_fileindex_after_commit(struct got_worktree *worktree,
5790 struct got_pathlist_head *commitable_paths,
5791 struct got_object_id *new_base_commit_id,
5792 struct got_fileindex *fileindex, int have_staged_files)
5794 const struct got_error *err = NULL;
5795 struct got_pathlist_entry *pe;
5796 char *relpath = NULL;
5798 TAILQ_FOREACH(pe, commitable_paths, entry) {
5799 struct got_fileindex_entry *ie;
5800 struct got_commitable *ct = pe->data;
5802 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5804 err = got_path_skip_common_ancestor(&relpath,
5805 worktree->root_path, ct->ondisk_path);
5810 if (ct->status == GOT_STATUS_DELETE ||
5811 ct->staged_status == GOT_STATUS_DELETE) {
5812 got_fileindex_entry_remove(fileindex, ie);
5813 } else if (ct->staged_status == GOT_STATUS_ADD ||
5814 ct->staged_status == GOT_STATUS_MODIFY) {
5815 got_fileindex_entry_stage_set(ie,
5816 GOT_FILEIDX_STAGE_NONE);
5817 got_fileindex_entry_staged_filetype_set(ie, 0);
5819 err = got_fileindex_entry_update(ie,
5820 worktree->root_fd, relpath,
5821 ct->staged_blob_id->hash,
5822 new_base_commit_id->hash,
5823 !have_staged_files);
5825 err = got_fileindex_entry_update(ie,
5826 worktree->root_fd, relpath,
5828 new_base_commit_id->hash,
5829 !have_staged_files);
5831 err = got_fileindex_entry_alloc(&ie, pe->path);
5834 err = got_fileindex_entry_update(ie,
5835 worktree->root_fd, relpath, ct->blob_id->hash,
5836 new_base_commit_id->hash, 1);
5838 got_fileindex_entry_free(ie);
5841 err = got_fileindex_entry_add(fileindex, ie);
5843 got_fileindex_entry_free(ie);
5856 static const struct got_error *
5857 check_out_of_date(const char *in_repo_path, unsigned char status,
5858 unsigned char staged_status, struct got_object_id *base_blob_id,
5859 struct got_object_id *base_commit_id,
5860 struct got_object_id *head_commit_id, struct got_repository *repo,
5863 const struct got_error *err = NULL;
5864 struct got_commit_object *commit = NULL;
5865 struct got_object_id *id = NULL;
5867 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5868 /* Trivial case: base commit == head commit */
5869 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5872 * Ensure file content which local changes were based
5873 * on matches file content in the branch head.
5875 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5878 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5880 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5881 err = got_error(ood_errcode);
5883 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5884 err = got_error(ood_errcode);
5886 /* Require that added files don't exist in the branch head. */
5887 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5890 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5891 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5893 err = id ? got_error(ood_errcode) : NULL;
5898 got_object_commit_close(commit);
5902 static const struct got_error *
5903 commit_worktree(struct got_object_id **new_commit_id,
5904 struct got_pathlist_head *commitable_paths,
5905 struct got_object_id *head_commit_id,
5906 struct got_object_id *parent_id2,
5907 struct got_worktree *worktree,
5908 const char *author, const char *committer, char *diff_path,
5909 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5910 got_worktree_status_cb status_cb, void *status_arg,
5911 struct got_repository *repo)
5913 const struct got_error *err = NULL, *unlockerr = NULL;
5914 struct got_pathlist_entry *pe;
5915 const char *head_ref_name = NULL;
5916 struct got_commit_object *head_commit = NULL;
5917 struct got_reference *head_ref2 = NULL;
5918 struct got_object_id *head_commit_id2 = NULL;
5919 struct got_tree_object *head_tree = NULL;
5920 struct got_object_id *new_tree_id = NULL;
5921 int nentries, nparents = 0;
5922 struct got_object_id_queue parent_ids;
5923 struct got_object_qid *pid = NULL;
5924 char *logmsg = NULL;
5927 *new_commit_id = NULL;
5929 STAILQ_INIT(&parent_ids);
5931 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5935 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5939 if (commit_msg_cb != NULL) {
5940 err = commit_msg_cb(commitable_paths, diff_path,
5941 &logmsg, commit_arg);
5946 if (logmsg == NULL || strlen(logmsg) == 0) {
5947 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5951 /* Create blobs from added and modified files and record their IDs. */
5952 TAILQ_FOREACH(pe, commitable_paths, entry) {
5953 struct got_commitable *ct = pe->data;
5956 /* Blobs for staged files already exist. */
5957 if (ct->staged_status == GOT_STATUS_ADD ||
5958 ct->staged_status == GOT_STATUS_MODIFY)
5961 if (ct->status != GOT_STATUS_ADD &&
5962 ct->status != GOT_STATUS_MODIFY &&
5963 ct->status != GOT_STATUS_MODE_CHANGE)
5966 if (asprintf(&ondisk_path, "%s/%s",
5967 worktree->root_path, pe->path) == -1) {
5968 err = got_error_from_errno("asprintf");
5971 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5977 /* Recursively write new tree objects. */
5978 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5979 commitable_paths, status_cb, status_arg, repo);
5983 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5986 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5989 err = got_object_qid_alloc(&pid, parent_id2);
5992 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5995 timestamp = time(NULL);
5996 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5997 nparents, author, timestamp, committer, timestamp, logmsg, repo);
6003 /* Check if a concurrent commit to our branch has occurred. */
6004 head_ref_name = got_worktree_get_head_ref_name(worktree);
6005 if (head_ref_name == NULL) {
6006 err = got_error_from_errno("got_worktree_get_head_ref_name");
6009 /* Lock the reference here to prevent concurrent modification. */
6010 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6013 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6016 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6017 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6020 /* Update branch head in repository. */
6021 err = got_ref_change_ref(head_ref2, *new_commit_id);
6024 err = got_ref_write(head_ref2, repo);
6028 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6032 err = ref_base_commit(worktree, repo);
6036 got_object_id_queue_free(&parent_ids);
6038 got_object_tree_close(head_tree);
6040 got_object_commit_close(head_commit);
6041 free(head_commit_id2);
6043 unlockerr = got_ref_unlock(head_ref2);
6044 if (unlockerr && err == NULL)
6046 got_ref_close(head_ref2);
6051 static const struct got_error *
6052 check_path_is_commitable(const char *path,
6053 struct got_pathlist_head *commitable_paths)
6055 struct got_pathlist_entry *cpe = NULL;
6056 size_t path_len = strlen(path);
6058 TAILQ_FOREACH(cpe, commitable_paths, entry) {
6059 struct got_commitable *ct = cpe->data;
6060 const char *ct_path = ct->path;
6062 while (ct_path[0] == '/')
6065 if (strcmp(path, ct_path) == 0 ||
6066 got_path_is_child(ct_path, path, path_len))
6071 return got_error_path(path, GOT_ERR_BAD_PATH);
6076 static const struct got_error *
6077 check_staged_file(void *arg, struct got_fileindex_entry *ie)
6079 int *have_staged_files = arg;
6081 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6082 *have_staged_files = 1;
6083 return got_error(GOT_ERR_CANCELLED);
6089 static const struct got_error *
6090 check_non_staged_files(struct got_fileindex *fileindex,
6091 struct got_pathlist_head *paths)
6093 struct got_pathlist_entry *pe;
6094 struct got_fileindex_entry *ie;
6096 TAILQ_FOREACH(pe, paths, entry) {
6097 if (pe->path[0] == '\0')
6099 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6101 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6102 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6103 return got_error_path(pe->path,
6104 GOT_ERR_FILE_NOT_STAGED);
6110 const struct got_error *
6111 got_worktree_commit(struct got_object_id **new_commit_id,
6112 struct got_worktree *worktree, struct got_pathlist_head *paths,
6113 const char *author, const char *committer, int allow_bad_symlinks,
6114 int show_diff, got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6115 got_worktree_status_cb status_cb, void *status_arg,
6116 struct got_repository *repo)
6118 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6119 struct got_fileindex *fileindex = NULL;
6120 char *fileindex_path = NULL;
6121 struct got_pathlist_head commitable_paths;
6122 struct collect_commitables_arg cc_arg;
6123 struct got_pathlist_entry *pe;
6124 struct got_reference *head_ref = NULL;
6125 struct got_object_id *head_commit_id = NULL;
6126 char *diff_path = NULL;
6127 int have_staged_files = 0;
6129 *new_commit_id = NULL;
6131 memset(&cc_arg, 0, sizeof(cc_arg));
6132 TAILQ_INIT(&commitable_paths);
6134 err = lock_worktree(worktree, LOCK_EX);
6138 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6142 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6146 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6150 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6151 &have_staged_files);
6152 if (err && err->code != GOT_ERR_CANCELLED)
6154 if (have_staged_files) {
6155 err = check_non_staged_files(fileindex, paths);
6160 cc_arg.commitable_paths = &commitable_paths;
6161 cc_arg.worktree = worktree;
6162 cc_arg.fileindex = fileindex;
6164 cc_arg.have_staged_files = have_staged_files;
6165 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6166 cc_arg.diff_header_shown = 0;
6168 err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6169 GOT_TMPDIR_STR "/got", ".diff");
6172 cc_arg.f1 = got_opentemp();
6173 if (cc_arg.f1 == NULL) {
6174 err = got_error_from_errno("got_opentemp");
6177 cc_arg.f2 = got_opentemp();
6178 if (cc_arg.f2 == NULL) {
6179 err = got_error_from_errno("got_opentemp");
6184 TAILQ_FOREACH(pe, paths, entry) {
6185 err = worktree_status(worktree, pe->path, fileindex, repo,
6186 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6192 if (fflush(cc_arg.diff_outfile) == EOF) {
6193 err = got_error_from_errno("fflush");
6198 if (TAILQ_EMPTY(&commitable_paths)) {
6199 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6203 TAILQ_FOREACH(pe, paths, entry) {
6204 err = check_path_is_commitable(pe->path, &commitable_paths);
6209 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6210 struct got_commitable *ct = pe->data;
6211 const char *ct_path = ct->in_repo_path;
6213 while (ct_path[0] == '/')
6215 err = check_out_of_date(ct_path, ct->status,
6216 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6217 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6223 err = commit_worktree(new_commit_id, &commitable_paths,
6224 head_commit_id, NULL, worktree, author, committer,
6225 (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6226 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6230 err = update_fileindex_after_commit(worktree, &commitable_paths,
6231 *new_commit_id, fileindex, have_staged_files);
6232 sync_err = sync_fileindex(fileindex, fileindex_path);
6233 if (sync_err && err == NULL)
6237 got_fileindex_free(fileindex);
6238 free(fileindex_path);
6239 unlockerr = lock_worktree(worktree, LOCK_SH);
6240 if (unlockerr && err == NULL)
6242 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6243 struct got_commitable *ct = pe->data;
6245 free_commitable(ct);
6247 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6248 if (diff_path && unlink(diff_path) == -1 && err == NULL)
6249 err = got_error_from_errno2("unlink", diff_path);
6251 if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6253 err = got_error_from_errno("fclose");
6258 got_commitable_get_path(struct got_commitable *ct)
6264 got_commitable_get_status(struct got_commitable *ct)
6269 struct check_rebase_ok_arg {
6270 struct got_worktree *worktree;
6271 struct got_repository *repo;
6274 static const struct got_error *
6275 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6277 const struct got_error *err = NULL;
6278 struct check_rebase_ok_arg *a = arg;
6279 unsigned char status;
6283 /* Reject rebase of a work tree with mixed base commits. */
6284 if (memcmp(ie->commit_hash, a->worktree->base_commit_id->hash,
6285 SHA1_DIGEST_LENGTH))
6286 return got_error(GOT_ERR_MIXED_COMMITS);
6288 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6290 return got_error_from_errno("asprintf");
6292 /* Reject rebase of a work tree with modified or staged files. */
6293 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6298 if (status != GOT_STATUS_NO_CHANGE)
6299 return got_error(GOT_ERR_MODIFIED);
6300 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6301 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6306 const struct got_error *
6307 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6308 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6309 struct got_worktree *worktree, struct got_reference *branch,
6310 struct got_repository *repo)
6312 const struct got_error *err = NULL;
6313 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6314 char *branch_ref_name = NULL;
6315 char *fileindex_path = NULL;
6316 struct check_rebase_ok_arg ok_arg;
6317 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6318 struct got_object_id *wt_branch_tip = NULL;
6320 *new_base_branch_ref = NULL;
6324 err = lock_worktree(worktree, LOCK_EX);
6328 err = open_fileindex(fileindex, &fileindex_path, worktree);
6332 ok_arg.worktree = worktree;
6334 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6339 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6343 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6347 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6351 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6356 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6359 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6360 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6364 err = got_ref_alloc_symref(new_base_branch_ref,
6365 new_base_branch_ref_name, wt_branch);
6368 err = got_ref_write(*new_base_branch_ref, repo);
6372 /* TODO Lock original branch's ref while rebasing? */
6374 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6378 err = got_ref_write(branch_ref, repo);
6382 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6383 worktree->base_commit_id);
6386 err = got_ref_write(*tmp_branch, repo);
6390 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6394 free(fileindex_path);
6395 free(tmp_branch_name);
6396 free(new_base_branch_ref_name);
6397 free(branch_ref_name);
6399 got_ref_close(branch_ref);
6401 got_ref_close(wt_branch);
6402 free(wt_branch_tip);
6404 if (*new_base_branch_ref) {
6405 got_ref_close(*new_base_branch_ref);
6406 *new_base_branch_ref = NULL;
6409 got_ref_close(*tmp_branch);
6413 got_fileindex_free(*fileindex);
6416 lock_worktree(worktree, LOCK_SH);
6421 const struct got_error *
6422 got_worktree_rebase_continue(struct got_object_id **commit_id,
6423 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6424 struct got_reference **branch, struct got_fileindex **fileindex,
6425 struct got_worktree *worktree, struct got_repository *repo)
6427 const struct got_error *err;
6428 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6429 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6430 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6431 char *fileindex_path = NULL;
6432 int have_staged_files = 0;
6435 *new_base_branch = NULL;
6440 err = lock_worktree(worktree, LOCK_EX);
6444 err = open_fileindex(fileindex, &fileindex_path, worktree);
6448 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6449 &have_staged_files);
6450 if (err && err->code != GOT_ERR_CANCELLED)
6452 if (have_staged_files) {
6453 err = got_error(GOT_ERR_STAGED_PATHS);
6457 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6461 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6465 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6469 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6473 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6477 err = got_ref_open(branch, repo,
6478 got_ref_get_symref_target(branch_ref), 0);
6482 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6486 err = got_ref_resolve(commit_id, repo, commit_ref);
6490 err = got_ref_open(new_base_branch, repo,
6491 new_base_branch_ref_name, 0);
6495 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6499 free(commit_ref_name);
6500 free(branch_ref_name);
6501 free(fileindex_path);
6503 got_ref_close(commit_ref);
6505 got_ref_close(branch_ref);
6510 got_ref_close(*tmp_branch);
6513 if (*new_base_branch) {
6514 got_ref_close(*new_base_branch);
6515 *new_base_branch = NULL;
6518 got_ref_close(*branch);
6522 got_fileindex_free(*fileindex);
6525 lock_worktree(worktree, LOCK_SH);
6530 const struct got_error *
6531 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6533 const struct got_error *err;
6534 char *tmp_branch_name = NULL;
6536 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6540 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6541 free(tmp_branch_name);
6545 static const struct got_error *
6546 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6547 const char *diff_path, char **logmsg, void *arg)
6553 static const struct got_error *
6554 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6555 const char *path, struct got_object_id *blob_id,
6556 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6557 int dirfd, const char *de_name)
6562 struct collect_merged_paths_arg {
6563 got_worktree_checkout_cb progress_cb;
6565 struct got_pathlist_head *merged_paths;
6568 static const struct got_error *
6569 collect_merged_paths(void *arg, unsigned char status, const char *path)
6571 const struct got_error *err;
6572 struct collect_merged_paths_arg *a = arg;
6574 struct got_pathlist_entry *new;
6576 err = (*a->progress_cb)(a->progress_arg, status, path);
6580 if (status != GOT_STATUS_MERGE &&
6581 status != GOT_STATUS_ADD &&
6582 status != GOT_STATUS_DELETE &&
6583 status != GOT_STATUS_CONFLICT)
6588 return got_error_from_errno("strdup");
6590 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6591 if (err || new == NULL)
6596 static const struct got_error *
6597 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6598 int is_rebase, struct got_repository *repo)
6600 const struct got_error *err;
6601 struct got_reference *commit_ref = NULL;
6603 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6605 if (err->code != GOT_ERR_NOT_REF)
6607 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6610 err = got_ref_write(commit_ref, repo);
6613 } else if (is_rebase) {
6614 struct got_object_id *stored_id;
6617 err = got_ref_resolve(&stored_id, repo, commit_ref);
6620 cmp = got_object_id_cmp(commit_id, stored_id);
6623 err = got_error(GOT_ERR_REBASE_COMMITID);
6629 got_ref_close(commit_ref);
6633 static const struct got_error *
6634 rebase_merge_files(struct got_pathlist_head *merged_paths,
6635 const char *commit_ref_name, struct got_worktree *worktree,
6636 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6637 struct got_object_id *commit_id, struct got_repository *repo,
6638 got_worktree_checkout_cb progress_cb, void *progress_arg,
6639 got_cancel_cb cancel_cb, void *cancel_arg)
6641 const struct got_error *err;
6642 struct got_reference *commit_ref = NULL;
6643 struct collect_merged_paths_arg cmp_arg;
6644 char *fileindex_path;
6646 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6648 err = get_fileindex_path(&fileindex_path, worktree);
6652 cmp_arg.progress_cb = progress_cb;
6653 cmp_arg.progress_arg = progress_arg;
6654 cmp_arg.merged_paths = merged_paths;
6655 err = merge_files(worktree, fileindex, fileindex_path,
6656 parent_commit_id, commit_id, repo, collect_merged_paths,
6657 &cmp_arg, cancel_cb, cancel_arg);
6659 got_ref_close(commit_ref);
6663 const struct got_error *
6664 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6665 struct got_worktree *worktree, struct got_fileindex *fileindex,
6666 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6667 struct got_repository *repo,
6668 got_worktree_checkout_cb progress_cb, void *progress_arg,
6669 got_cancel_cb cancel_cb, void *cancel_arg)
6671 const struct got_error *err;
6672 char *commit_ref_name;
6674 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6678 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6682 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6683 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6684 progress_arg, cancel_cb, cancel_arg);
6686 free(commit_ref_name);
6690 const struct got_error *
6691 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6692 struct got_worktree *worktree, struct got_fileindex *fileindex,
6693 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6694 struct got_repository *repo,
6695 got_worktree_checkout_cb progress_cb, void *progress_arg,
6696 got_cancel_cb cancel_cb, void *cancel_arg)
6698 const struct got_error *err;
6699 char *commit_ref_name;
6701 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6705 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6709 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6710 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6711 progress_arg, cancel_cb, cancel_arg);
6713 free(commit_ref_name);
6717 static const struct got_error *
6718 rebase_commit(struct got_object_id **new_commit_id,
6719 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6720 struct got_worktree *worktree, struct got_fileindex *fileindex,
6721 struct got_reference *tmp_branch, const char *committer,
6722 struct got_commit_object *orig_commit, const char *new_logmsg,
6723 struct got_repository *repo)
6725 const struct got_error *err, *sync_err;
6726 struct got_pathlist_head commitable_paths;
6727 struct collect_commitables_arg cc_arg;
6728 char *fileindex_path = NULL;
6729 struct got_reference *head_ref = NULL;
6730 struct got_object_id *head_commit_id = NULL;
6731 char *logmsg = NULL;
6733 memset(&cc_arg, 0, sizeof(cc_arg));
6734 TAILQ_INIT(&commitable_paths);
6735 *new_commit_id = NULL;
6737 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6739 err = get_fileindex_path(&fileindex_path, worktree);
6743 cc_arg.commitable_paths = &commitable_paths;
6744 cc_arg.worktree = worktree;
6746 cc_arg.have_staged_files = 0;
6748 * If possible get the status of individual files directly to
6749 * avoid crawling the entire work tree once per rebased commit.
6751 * Ideally, merged_paths would contain a list of commitables
6752 * we could use so we could skip worktree_status() entirely.
6753 * However, we would then need carefully keep track of cumulative
6754 * effects of operations such as file additions and deletions
6755 * in 'got histedit -f' (folding multiple commits into one),
6756 * and this extra complexity is not really worth it.
6759 struct got_pathlist_entry *pe;
6760 TAILQ_FOREACH(pe, merged_paths, entry) {
6761 err = worktree_status(worktree, pe->path, fileindex,
6762 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6768 err = worktree_status(worktree, "", fileindex, repo,
6769 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6774 if (TAILQ_EMPTY(&commitable_paths)) {
6775 /* No-op change; commit will be elided. */
6776 err = got_ref_delete(commit_ref, repo);
6779 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6783 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6787 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6792 logmsg = strdup(new_logmsg);
6793 if (logmsg == NULL) {
6794 err = got_error_from_errno("strdup");
6798 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6803 /* NB: commit_worktree will call free(logmsg) */
6804 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6805 NULL, worktree, got_object_commit_get_author(orig_commit),
6806 committer ? committer :
6807 got_object_commit_get_committer(orig_commit), NULL,
6808 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6812 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6816 err = got_ref_delete(commit_ref, repo);
6820 err = update_fileindex_after_commit(worktree, &commitable_paths,
6821 *new_commit_id, fileindex, 0);
6822 sync_err = sync_fileindex(fileindex, fileindex_path);
6823 if (sync_err && err == NULL)
6826 free(fileindex_path);
6827 free(head_commit_id);
6829 got_ref_close(head_ref);
6831 free(*new_commit_id);
6832 *new_commit_id = NULL;
6837 const struct got_error *
6838 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6839 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6840 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6841 const char *committer, struct got_commit_object *orig_commit,
6842 struct got_object_id *orig_commit_id, struct got_repository *repo)
6844 const struct got_error *err;
6845 char *commit_ref_name;
6846 struct got_reference *commit_ref = NULL;
6847 struct got_object_id *commit_id = NULL;
6849 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6853 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6856 err = got_ref_resolve(&commit_id, repo, commit_ref);
6859 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6860 err = got_error(GOT_ERR_REBASE_COMMITID);
6864 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6865 worktree, fileindex, tmp_branch, committer, orig_commit,
6869 got_ref_close(commit_ref);
6870 free(commit_ref_name);
6875 const struct got_error *
6876 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6877 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6878 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6879 const char *committer, struct got_commit_object *orig_commit,
6880 struct got_object_id *orig_commit_id, const char *new_logmsg,
6881 struct got_repository *repo)
6883 const struct got_error *err;
6884 char *commit_ref_name;
6885 struct got_reference *commit_ref = NULL;
6887 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6891 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6895 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6896 worktree, fileindex, tmp_branch, committer, orig_commit,
6900 got_ref_close(commit_ref);
6901 free(commit_ref_name);
6905 const struct got_error *
6906 got_worktree_rebase_postpone(struct got_worktree *worktree,
6907 struct got_fileindex *fileindex)
6910 got_fileindex_free(fileindex);
6911 return lock_worktree(worktree, LOCK_SH);
6914 static const struct got_error *
6915 delete_ref(const char *name, struct got_repository *repo)
6917 const struct got_error *err;
6918 struct got_reference *ref;
6920 err = got_ref_open(&ref, repo, name, 0);
6922 if (err->code == GOT_ERR_NOT_REF)
6927 err = got_ref_delete(ref, repo);
6932 static const struct got_error *
6933 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6935 const struct got_error *err;
6936 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6937 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6939 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6942 err = delete_ref(tmp_branch_name, repo);
6946 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6949 err = delete_ref(new_base_branch_ref_name, repo);
6953 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6956 err = delete_ref(branch_ref_name, repo);
6960 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6963 err = delete_ref(commit_ref_name, repo);
6968 free(tmp_branch_name);
6969 free(new_base_branch_ref_name);
6970 free(branch_ref_name);
6971 free(commit_ref_name);
6975 static const struct got_error *
6976 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6977 struct got_object_id *new_commit_id, struct got_repository *repo)
6979 const struct got_error *err;
6980 struct got_reference *ref = NULL;
6981 struct got_object_id *old_commit_id = NULL;
6982 const char *branch_name = NULL;
6983 char *new_id_str = NULL;
6984 char *refname = NULL;
6986 branch_name = got_ref_get_name(branch);
6987 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6988 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6991 err = got_object_id_str(&new_id_str, new_commit_id);
6995 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6996 new_id_str) == -1) {
6997 err = got_error_from_errno("asprintf");
7001 err = got_ref_resolve(&old_commit_id, repo, branch);
7005 err = got_ref_alloc(&ref, refname, old_commit_id);
7009 err = got_ref_write(ref, repo);
7013 free(old_commit_id);
7019 const struct got_error *
7020 got_worktree_rebase_complete(struct got_worktree *worktree,
7021 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7022 struct got_reference *rebased_branch, struct got_repository *repo,
7025 const struct got_error *err, *unlockerr, *sync_err;
7026 struct got_object_id *new_head_commit_id = NULL;
7027 char *fileindex_path = NULL;
7029 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7033 if (create_backup) {
7034 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7035 rebased_branch, new_head_commit_id, repo);
7040 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7044 err = got_ref_write(rebased_branch, repo);
7048 err = got_worktree_set_head_ref(worktree, rebased_branch);
7052 err = delete_rebase_refs(worktree, repo);
7056 err = get_fileindex_path(&fileindex_path, worktree);
7059 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7060 sync_err = sync_fileindex(fileindex, fileindex_path);
7061 if (sync_err && err == NULL)
7064 got_fileindex_free(fileindex);
7065 free(fileindex_path);
7066 free(new_head_commit_id);
7067 unlockerr = lock_worktree(worktree, LOCK_SH);
7068 if (unlockerr && err == NULL)
7073 const struct got_error *
7074 got_worktree_rebase_abort(struct got_worktree *worktree,
7075 struct got_fileindex *fileindex, struct got_repository *repo,
7076 struct got_reference *new_base_branch,
7077 got_worktree_checkout_cb progress_cb, void *progress_arg)
7079 const struct got_error *err, *unlockerr, *sync_err;
7080 struct got_reference *resolved = NULL;
7081 struct got_object_id *commit_id = NULL;
7082 struct got_commit_object *commit = NULL;
7083 char *fileindex_path = NULL;
7084 struct revert_file_args rfa;
7085 struct got_object_id *tree_id = NULL;
7087 err = lock_worktree(worktree, LOCK_EX);
7091 err = got_object_open_as_commit(&commit, repo,
7092 worktree->base_commit_id);
7096 err = got_ref_open(&resolved, repo,
7097 got_ref_get_symref_target(new_base_branch), 0);
7101 err = got_worktree_set_head_ref(worktree, resolved);
7106 * XXX commits to the base branch could have happened while
7107 * we were busy rebasing; should we store the original commit ID
7108 * when rebase begins and read it back here?
7110 err = got_ref_resolve(&commit_id, repo, resolved);
7114 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7118 err = got_object_id_by_path(&tree_id, repo, commit,
7119 worktree->path_prefix);
7123 err = delete_rebase_refs(worktree, repo);
7127 err = get_fileindex_path(&fileindex_path, worktree);
7131 rfa.worktree = worktree;
7132 rfa.fileindex = fileindex;
7133 rfa.progress_cb = progress_cb;
7134 rfa.progress_arg = progress_arg;
7135 rfa.patch_cb = NULL;
7136 rfa.patch_arg = NULL;
7138 rfa.unlink_added_files = 0;
7139 err = worktree_status(worktree, "", fileindex, repo,
7140 revert_file, &rfa, NULL, NULL, 1, 0);
7144 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7145 repo, progress_cb, progress_arg, NULL, NULL);
7147 sync_err = sync_fileindex(fileindex, fileindex_path);
7148 if (sync_err && err == NULL)
7151 got_ref_close(resolved);
7155 got_object_commit_close(commit);
7157 got_fileindex_free(fileindex);
7158 free(fileindex_path);
7160 unlockerr = lock_worktree(worktree, LOCK_SH);
7161 if (unlockerr && err == NULL)
7166 const struct got_error *
7167 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7168 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7169 struct got_fileindex **fileindex, struct got_worktree *worktree,
7170 struct got_repository *repo)
7172 const struct got_error *err = NULL;
7173 char *tmp_branch_name = NULL;
7174 char *branch_ref_name = NULL;
7175 char *base_commit_ref_name = NULL;
7176 char *fileindex_path = NULL;
7177 struct check_rebase_ok_arg ok_arg;
7178 struct got_reference *wt_branch = NULL;
7179 struct got_reference *base_commit_ref = NULL;
7183 *base_commit_id = NULL;
7186 err = lock_worktree(worktree, LOCK_EX);
7190 err = open_fileindex(fileindex, &fileindex_path, worktree);
7194 ok_arg.worktree = worktree;
7196 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7201 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7205 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7209 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7214 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7219 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7223 err = got_ref_write(*branch_ref, repo);
7227 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7228 worktree->base_commit_id);
7231 err = got_ref_write(base_commit_ref, repo);
7234 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7235 if (*base_commit_id == NULL) {
7236 err = got_error_from_errno("got_object_id_dup");
7240 err = got_ref_alloc(tmp_branch, tmp_branch_name,
7241 worktree->base_commit_id);
7244 err = got_ref_write(*tmp_branch, repo);
7248 err = got_worktree_set_head_ref(worktree, *tmp_branch);
7252 free(fileindex_path);
7253 free(tmp_branch_name);
7254 free(branch_ref_name);
7255 free(base_commit_ref_name);
7257 got_ref_close(wt_branch);
7260 got_ref_close(*branch_ref);
7264 got_ref_close(*tmp_branch);
7267 free(*base_commit_id);
7269 got_fileindex_free(*fileindex);
7272 lock_worktree(worktree, LOCK_SH);
7277 const struct got_error *
7278 got_worktree_histedit_postpone(struct got_worktree *worktree,
7279 struct got_fileindex *fileindex)
7282 got_fileindex_free(fileindex);
7283 return lock_worktree(worktree, LOCK_SH);
7286 const struct got_error *
7287 got_worktree_histedit_in_progress(int *in_progress,
7288 struct got_worktree *worktree)
7290 const struct got_error *err;
7291 char *tmp_branch_name = NULL;
7293 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7297 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7298 free(tmp_branch_name);
7302 const struct got_error *
7303 got_worktree_histedit_continue(struct got_object_id **commit_id,
7304 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7305 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7306 struct got_worktree *worktree, struct got_repository *repo)
7308 const struct got_error *err;
7309 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7310 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7311 struct got_reference *commit_ref = NULL;
7312 struct got_reference *base_commit_ref = NULL;
7313 char *fileindex_path = NULL;
7314 int have_staged_files = 0;
7318 *base_commit_id = NULL;
7321 err = lock_worktree(worktree, LOCK_EX);
7325 err = open_fileindex(fileindex, &fileindex_path, worktree);
7329 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7330 &have_staged_files);
7331 if (err && err->code != GOT_ERR_CANCELLED)
7333 if (have_staged_files) {
7334 err = got_error(GOT_ERR_STAGED_PATHS);
7338 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7342 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7346 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7350 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7355 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7359 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7362 err = got_ref_resolve(commit_id, repo, commit_ref);
7366 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7369 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7373 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7377 free(commit_ref_name);
7378 free(branch_ref_name);
7379 free(fileindex_path);
7381 got_ref_close(commit_ref);
7382 if (base_commit_ref)
7383 got_ref_close(base_commit_ref);
7387 free(*base_commit_id);
7388 *base_commit_id = NULL;
7390 got_ref_close(*tmp_branch);
7394 got_fileindex_free(*fileindex);
7397 lock_worktree(worktree, LOCK_EX);
7402 static const struct got_error *
7403 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7405 const struct got_error *err;
7406 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7407 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7409 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7412 err = delete_ref(tmp_branch_name, repo);
7416 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7420 err = delete_ref(base_commit_ref_name, repo);
7424 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7427 err = delete_ref(branch_ref_name, repo);
7431 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7434 err = delete_ref(commit_ref_name, repo);
7438 free(tmp_branch_name);
7439 free(base_commit_ref_name);
7440 free(branch_ref_name);
7441 free(commit_ref_name);
7445 const struct got_error *
7446 got_worktree_histedit_abort(struct got_worktree *worktree,
7447 struct got_fileindex *fileindex, struct got_repository *repo,
7448 struct got_reference *branch, struct got_object_id *base_commit_id,
7449 got_worktree_checkout_cb progress_cb, void *progress_arg)
7451 const struct got_error *err, *unlockerr, *sync_err;
7452 struct got_reference *resolved = NULL;
7453 char *fileindex_path = NULL;
7454 struct got_commit_object *commit = NULL;
7455 struct got_object_id *tree_id = NULL;
7456 struct revert_file_args rfa;
7458 err = lock_worktree(worktree, LOCK_EX);
7462 err = got_object_open_as_commit(&commit, repo,
7463 worktree->base_commit_id);
7467 err = got_ref_open(&resolved, repo,
7468 got_ref_get_symref_target(branch), 0);
7472 err = got_worktree_set_head_ref(worktree, resolved);
7476 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7480 err = got_object_id_by_path(&tree_id, repo, commit,
7481 worktree->path_prefix);
7485 err = delete_histedit_refs(worktree, repo);
7489 err = get_fileindex_path(&fileindex_path, worktree);
7493 rfa.worktree = worktree;
7494 rfa.fileindex = fileindex;
7495 rfa.progress_cb = progress_cb;
7496 rfa.progress_arg = progress_arg;
7497 rfa.patch_cb = NULL;
7498 rfa.patch_arg = NULL;
7500 rfa.unlink_added_files = 0;
7501 err = worktree_status(worktree, "", fileindex, repo,
7502 revert_file, &rfa, NULL, NULL, 1, 0);
7506 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7507 repo, progress_cb, progress_arg, NULL, NULL);
7509 sync_err = sync_fileindex(fileindex, fileindex_path);
7510 if (sync_err && err == NULL)
7513 got_ref_close(resolved);
7515 free(fileindex_path);
7517 unlockerr = lock_worktree(worktree, LOCK_SH);
7518 if (unlockerr && err == NULL)
7523 const struct got_error *
7524 got_worktree_histedit_complete(struct got_worktree *worktree,
7525 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7526 struct got_reference *edited_branch, struct got_repository *repo)
7528 const struct got_error *err, *unlockerr, *sync_err;
7529 struct got_object_id *new_head_commit_id = NULL;
7530 struct got_reference *resolved = NULL;
7531 char *fileindex_path = NULL;
7533 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7537 err = got_ref_open(&resolved, repo,
7538 got_ref_get_symref_target(edited_branch), 0);
7542 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7543 resolved, new_head_commit_id, repo);
7547 err = got_ref_change_ref(resolved, new_head_commit_id);
7551 err = got_ref_write(resolved, repo);
7555 err = got_worktree_set_head_ref(worktree, resolved);
7559 err = delete_histedit_refs(worktree, repo);
7563 err = get_fileindex_path(&fileindex_path, worktree);
7566 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7567 sync_err = sync_fileindex(fileindex, fileindex_path);
7568 if (sync_err && err == NULL)
7571 got_fileindex_free(fileindex);
7572 free(fileindex_path);
7573 free(new_head_commit_id);
7574 unlockerr = lock_worktree(worktree, LOCK_SH);
7575 if (unlockerr && err == NULL)
7580 const struct got_error *
7581 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7582 struct got_object_id *commit_id, struct got_repository *repo)
7584 const struct got_error *err;
7585 char *commit_ref_name;
7587 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7591 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7595 err = delete_ref(commit_ref_name, repo);
7597 free(commit_ref_name);
7601 const struct got_error *
7602 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7603 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7604 struct got_worktree *worktree, const char *refname,
7605 struct got_repository *repo)
7607 const struct got_error *err = NULL;
7608 char *fileindex_path = NULL;
7609 struct check_rebase_ok_arg ok_arg;
7613 *base_branch_ref = NULL;
7615 err = lock_worktree(worktree, LOCK_EX);
7619 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7620 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7621 "cannot integrate a branch into itself; "
7622 "update -b or different branch name required");
7626 err = open_fileindex(fileindex, &fileindex_path, worktree);
7630 /* Preconditions are the same as for rebase. */
7631 ok_arg.worktree = worktree;
7633 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7638 err = got_ref_open(branch_ref, repo, refname, 1);
7642 err = got_ref_open(base_branch_ref, repo,
7643 got_worktree_get_head_ref_name(worktree), 1);
7647 got_ref_close(*branch_ref);
7650 if (*base_branch_ref) {
7651 got_ref_close(*base_branch_ref);
7652 *base_branch_ref = NULL;
7655 got_fileindex_free(*fileindex);
7658 lock_worktree(worktree, LOCK_SH);
7663 const struct got_error *
7664 got_worktree_integrate_continue(struct got_worktree *worktree,
7665 struct got_fileindex *fileindex, struct got_repository *repo,
7666 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7667 got_worktree_checkout_cb progress_cb, void *progress_arg,
7668 got_cancel_cb cancel_cb, void *cancel_arg)
7670 const struct got_error *err = NULL, *sync_err, *unlockerr;
7671 char *fileindex_path = NULL;
7672 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7673 struct got_commit_object *commit = NULL;
7675 err = get_fileindex_path(&fileindex_path, worktree);
7679 err = got_ref_resolve(&commit_id, repo, branch_ref);
7683 err = got_object_open_as_commit(&commit, repo, commit_id);
7687 err = got_object_id_by_path(&tree_id, repo, commit,
7688 worktree->path_prefix);
7692 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7696 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7697 progress_cb, progress_arg, cancel_cb, cancel_arg);
7701 err = got_ref_change_ref(base_branch_ref, commit_id);
7705 err = got_ref_write(base_branch_ref, repo);
7709 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7711 sync_err = sync_fileindex(fileindex, fileindex_path);
7712 if (sync_err && err == NULL)
7716 unlockerr = got_ref_unlock(branch_ref);
7717 if (unlockerr && err == NULL)
7719 got_ref_close(branch_ref);
7721 unlockerr = got_ref_unlock(base_branch_ref);
7722 if (unlockerr && err == NULL)
7724 got_ref_close(base_branch_ref);
7726 got_fileindex_free(fileindex);
7727 free(fileindex_path);
7730 got_object_commit_close(commit);
7732 unlockerr = lock_worktree(worktree, LOCK_SH);
7733 if (unlockerr && err == NULL)
7738 const struct got_error *
7739 got_worktree_integrate_abort(struct got_worktree *worktree,
7740 struct got_fileindex *fileindex, struct got_repository *repo,
7741 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7743 const struct got_error *err = NULL, *unlockerr = NULL;
7745 got_fileindex_free(fileindex);
7747 err = lock_worktree(worktree, LOCK_SH);
7749 unlockerr = got_ref_unlock(branch_ref);
7750 if (unlockerr && err == NULL)
7752 got_ref_close(branch_ref);
7754 unlockerr = got_ref_unlock(base_branch_ref);
7755 if (unlockerr && err == NULL)
7757 got_ref_close(base_branch_ref);
7762 const struct got_error *
7763 got_worktree_merge_postpone(struct got_worktree *worktree,
7764 struct got_fileindex *fileindex)
7766 const struct got_error *err, *sync_err;
7767 char *fileindex_path = NULL;
7769 err = get_fileindex_path(&fileindex_path, worktree);
7773 sync_err = sync_fileindex(fileindex, fileindex_path);
7775 err = lock_worktree(worktree, LOCK_SH);
7776 if (sync_err && err == NULL)
7779 got_fileindex_free(fileindex);
7780 free(fileindex_path);
7784 static const struct got_error *
7785 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7787 const struct got_error *err;
7788 char *branch_refname = NULL, *commit_refname = NULL;
7790 err = get_merge_branch_ref_name(&branch_refname, worktree);
7793 err = delete_ref(branch_refname, repo);
7797 err = get_merge_commit_ref_name(&commit_refname, worktree);
7800 err = delete_ref(commit_refname, repo);
7805 free(branch_refname);
7806 free(commit_refname);
7810 struct merge_commit_msg_arg {
7811 struct got_worktree *worktree;
7812 const char *branch_name;
7815 static const struct got_error *
7816 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
7817 const char *diff_path, char **logmsg, void *arg)
7819 struct merge_commit_msg_arg *a = arg;
7821 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7822 got_worktree_get_head_ref_name(a->worktree)) == -1)
7823 return got_error_from_errno("asprintf");
7829 const struct got_error *
7830 got_worktree_merge_branch(struct got_worktree *worktree,
7831 struct got_fileindex *fileindex,
7832 struct got_object_id *yca_commit_id,
7833 struct got_object_id *branch_tip,
7834 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7835 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7837 const struct got_error *err;
7838 char *fileindex_path = NULL;
7840 err = get_fileindex_path(&fileindex_path, worktree);
7844 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7849 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7850 branch_tip, repo, progress_cb, progress_arg,
7851 cancel_cb, cancel_arg);
7853 free(fileindex_path);
7857 const struct got_error *
7858 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7859 struct got_worktree *worktree, struct got_fileindex *fileindex,
7860 const char *author, const char *committer, int allow_bad_symlinks,
7861 struct got_object_id *branch_tip, const char *branch_name,
7862 struct got_repository *repo,
7863 got_worktree_status_cb status_cb, void *status_arg)
7866 const struct got_error *err = NULL, *sync_err;
7867 struct got_pathlist_head commitable_paths;
7868 struct collect_commitables_arg cc_arg;
7869 struct got_pathlist_entry *pe;
7870 struct got_reference *head_ref = NULL;
7871 struct got_object_id *head_commit_id = NULL;
7872 int have_staged_files = 0;
7873 struct merge_commit_msg_arg mcm_arg;
7874 char *fileindex_path = NULL;
7876 memset(&cc_arg, 0, sizeof(cc_arg));
7877 *new_commit_id = NULL;
7879 TAILQ_INIT(&commitable_paths);
7881 err = get_fileindex_path(&fileindex_path, worktree);
7885 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7889 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7893 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7894 &have_staged_files);
7895 if (err && err->code != GOT_ERR_CANCELLED)
7897 if (have_staged_files) {
7898 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7902 cc_arg.commitable_paths = &commitable_paths;
7903 cc_arg.worktree = worktree;
7904 cc_arg.fileindex = fileindex;
7906 cc_arg.have_staged_files = have_staged_files;
7907 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7908 err = worktree_status(worktree, "", fileindex, repo,
7909 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7913 if (TAILQ_EMPTY(&commitable_paths)) {
7914 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7915 "merge of %s cannot proceed", branch_name);
7919 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7920 struct got_commitable *ct = pe->data;
7921 const char *ct_path = ct->in_repo_path;
7923 while (ct_path[0] == '/')
7925 err = check_out_of_date(ct_path, ct->status,
7926 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7927 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7933 mcm_arg.worktree = worktree;
7934 mcm_arg.branch_name = branch_name;
7935 err = commit_worktree(new_commit_id, &commitable_paths,
7936 head_commit_id, branch_tip, worktree, author, committer, NULL,
7937 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7941 err = update_fileindex_after_commit(worktree, &commitable_paths,
7942 *new_commit_id, fileindex, have_staged_files);
7943 sync_err = sync_fileindex(fileindex, fileindex_path);
7944 if (sync_err && err == NULL)
7947 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7948 struct got_commitable *ct = pe->data;
7950 free_commitable(ct);
7952 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
7953 free(fileindex_path);
7957 const struct got_error *
7958 got_worktree_merge_complete(struct got_worktree *worktree,
7959 struct got_fileindex *fileindex, struct got_repository *repo)
7961 const struct got_error *err, *unlockerr, *sync_err;
7962 char *fileindex_path = NULL;
7964 err = delete_merge_refs(worktree, repo);
7968 err = get_fileindex_path(&fileindex_path, worktree);
7971 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7972 sync_err = sync_fileindex(fileindex, fileindex_path);
7973 if (sync_err && err == NULL)
7976 got_fileindex_free(fileindex);
7977 free(fileindex_path);
7978 unlockerr = lock_worktree(worktree, LOCK_SH);
7979 if (unlockerr && err == NULL)
7984 const struct got_error *
7985 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7986 struct got_repository *repo)
7988 const struct got_error *err;
7989 char *branch_refname = NULL;
7990 struct got_reference *branch_ref = NULL;
7994 err = get_merge_branch_ref_name(&branch_refname, worktree);
7997 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7998 free(branch_refname);
8000 if (err->code != GOT_ERR_NOT_REF)
8008 const struct got_error *got_worktree_merge_prepare(
8009 struct got_fileindex **fileindex, struct got_worktree *worktree,
8010 struct got_reference *branch, struct got_repository *repo)
8012 const struct got_error *err = NULL;
8013 char *fileindex_path = NULL;
8014 char *branch_refname = NULL, *commit_refname = NULL;
8015 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
8016 struct got_reference *commit_ref = NULL;
8017 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
8018 struct check_rebase_ok_arg ok_arg;
8022 err = lock_worktree(worktree, LOCK_EX);
8026 err = open_fileindex(fileindex, &fileindex_path, worktree);
8030 /* Preconditions are the same as for rebase. */
8031 ok_arg.worktree = worktree;
8033 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8038 err = get_merge_branch_ref_name(&branch_refname, worktree);
8042 err = get_merge_commit_ref_name(&commit_refname, worktree);
8046 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8051 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8055 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8056 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8060 err = got_ref_resolve(&branch_tip, repo, branch);
8064 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8067 err = got_ref_write(branch_ref, repo);
8071 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8074 err = got_ref_write(commit_ref, repo);
8079 free(branch_refname);
8080 free(commit_refname);
8081 free(fileindex_path);
8083 got_ref_close(branch_ref);
8085 got_ref_close(commit_ref);
8087 got_ref_close(wt_branch);
8088 free(wt_branch_tip);
8091 got_fileindex_free(*fileindex);
8094 lock_worktree(worktree, LOCK_SH);
8099 const struct got_error *
8100 got_worktree_merge_continue(char **branch_name,
8101 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8102 struct got_worktree *worktree, struct got_repository *repo)
8104 const struct got_error *err;
8105 char *commit_refname = NULL, *branch_refname = NULL;
8106 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8107 char *fileindex_path = NULL;
8108 int have_staged_files = 0;
8110 *branch_name = NULL;
8114 err = lock_worktree(worktree, LOCK_EX);
8118 err = open_fileindex(fileindex, &fileindex_path, worktree);
8122 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8123 &have_staged_files);
8124 if (err && err->code != GOT_ERR_CANCELLED)
8126 if (have_staged_files) {
8127 err = got_error(GOT_ERR_STAGED_PATHS);
8131 err = get_merge_branch_ref_name(&branch_refname, worktree);
8135 err = get_merge_commit_ref_name(&commit_refname, worktree);
8139 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8143 if (!got_ref_is_symbolic(branch_ref)) {
8144 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8145 "%s is not a symbolic reference",
8146 got_ref_get_name(branch_ref));
8149 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8150 if (*branch_name == NULL) {
8151 err = got_error_from_errno("strdup");
8155 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8159 err = got_ref_resolve(branch_tip, repo, commit_ref);
8163 free(commit_refname);
8164 free(branch_refname);
8165 free(fileindex_path);
8167 got_ref_close(commit_ref);
8169 got_ref_close(branch_ref);
8173 *branch_name = NULL;
8178 got_fileindex_free(*fileindex);
8181 lock_worktree(worktree, LOCK_SH);
8186 const struct got_error *
8187 got_worktree_merge_abort(struct got_worktree *worktree,
8188 struct got_fileindex *fileindex, struct got_repository *repo,
8189 got_worktree_checkout_cb progress_cb, void *progress_arg)
8191 const struct got_error *err, *unlockerr, *sync_err;
8192 struct got_object_id *commit_id = NULL;
8193 struct got_commit_object *commit = NULL;
8194 char *fileindex_path = NULL;
8195 struct revert_file_args rfa;
8196 struct got_object_id *tree_id = NULL;
8198 err = got_object_open_as_commit(&commit, repo,
8199 worktree->base_commit_id);
8203 err = got_object_id_by_path(&tree_id, repo, commit,
8204 worktree->path_prefix);
8208 err = delete_merge_refs(worktree, repo);
8212 err = get_fileindex_path(&fileindex_path, worktree);
8216 rfa.worktree = worktree;
8217 rfa.fileindex = fileindex;
8218 rfa.progress_cb = progress_cb;
8219 rfa.progress_arg = progress_arg;
8220 rfa.patch_cb = NULL;
8221 rfa.patch_arg = NULL;
8223 rfa.unlink_added_files = 1;
8224 err = worktree_status(worktree, "", fileindex, repo,
8225 revert_file, &rfa, NULL, NULL, 1, 0);
8229 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8230 repo, progress_cb, progress_arg, NULL, NULL);
8232 sync_err = sync_fileindex(fileindex, fileindex_path);
8233 if (sync_err && err == NULL)
8239 got_object_commit_close(commit);
8241 got_fileindex_free(fileindex);
8242 free(fileindex_path);
8244 unlockerr = lock_worktree(worktree, LOCK_SH);
8245 if (unlockerr && err == NULL)
8250 struct check_stage_ok_arg {
8251 struct got_object_id *head_commit_id;
8252 struct got_worktree *worktree;
8253 struct got_fileindex *fileindex;
8254 struct got_repository *repo;
8258 static const struct got_error *
8259 check_stage_ok(void *arg, unsigned char status,
8260 unsigned char staged_status, const char *relpath,
8261 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8262 struct got_object_id *commit_id, int dirfd, const char *de_name)
8264 struct check_stage_ok_arg *a = arg;
8265 const struct got_error *err = NULL;
8266 struct got_fileindex_entry *ie;
8267 struct got_object_id base_commit_id;
8268 struct got_object_id *base_commit_idp = NULL;
8269 char *in_repo_path = NULL, *p;
8271 if (status == GOT_STATUS_UNVERSIONED ||
8272 status == GOT_STATUS_NO_CHANGE)
8274 if (status == GOT_STATUS_NONEXISTENT)
8275 return got_error_set_errno(ENOENT, relpath);
8277 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8279 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8281 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8282 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8284 return got_error_from_errno("asprintf");
8286 if (got_fileindex_entry_has_commit(ie)) {
8287 memcpy(base_commit_id.hash, ie->commit_hash,
8288 SHA1_DIGEST_LENGTH);
8289 base_commit_idp = &base_commit_id;
8292 if (status == GOT_STATUS_CONFLICT) {
8293 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8295 } else if (status != GOT_STATUS_ADD &&
8296 status != GOT_STATUS_MODIFY &&
8297 status != GOT_STATUS_DELETE) {
8298 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8302 a->have_changes = 1;
8307 err = check_out_of_date(p, status, staged_status,
8308 blob_id, base_commit_idp, a->head_commit_id, a->repo,
8309 GOT_ERR_STAGE_OUT_OF_DATE);
8315 struct stage_path_arg {
8316 struct got_worktree *worktree;
8317 struct got_fileindex *fileindex;
8318 struct got_repository *repo;
8319 got_worktree_status_cb status_cb;
8321 got_worktree_patch_cb patch_cb;
8323 int staged_something;
8324 int allow_bad_symlinks;
8327 static const struct got_error *
8328 stage_path(void *arg, unsigned char status,
8329 unsigned char staged_status, const char *relpath,
8330 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8331 struct got_object_id *commit_id, int dirfd, const char *de_name)
8333 struct stage_path_arg *a = arg;
8334 const struct got_error *err = NULL;
8335 struct got_fileindex_entry *ie;
8336 char *ondisk_path = NULL, *path_content = NULL;
8338 struct got_object_id *new_staged_blob_id = NULL;
8341 if (status == GOT_STATUS_UNVERSIONED)
8344 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8346 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8348 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8350 return got_error_from_errno("asprintf");
8353 case GOT_STATUS_ADD:
8354 case GOT_STATUS_MODIFY:
8355 /* XXX could sb.st_mode be passed in by our caller? */
8356 if (lstat(ondisk_path, &sb) == -1) {
8357 err = got_error_from_errno2("lstat", ondisk_path);
8361 if (status == GOT_STATUS_ADD) {
8362 int choice = GOT_PATCH_CHOICE_NONE;
8363 err = (*a->patch_cb)(&choice, a->patch_arg,
8364 status, ie->path, NULL, 1, 1);
8367 if (choice != GOT_PATCH_CHOICE_YES)
8370 err = create_patched_content(&path_content, 0,
8371 staged_blob_id ? staged_blob_id : blob_id,
8372 ondisk_path, dirfd, de_name, ie->path,
8373 a->repo, a->patch_cb, a->patch_arg);
8374 if (err || path_content == NULL)
8378 err = got_object_blob_create(&new_staged_blob_id,
8379 path_content ? path_content : ondisk_path, a->repo);
8382 memcpy(ie->staged_blob_hash, new_staged_blob_id->hash,
8383 SHA1_DIGEST_LENGTH);
8384 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8385 stage = GOT_FILEIDX_STAGE_ADD;
8387 stage = GOT_FILEIDX_STAGE_MODIFY;
8388 got_fileindex_entry_stage_set(ie, stage);
8389 if (S_ISLNK(sb.st_mode)) {
8390 int is_bad_symlink = 0;
8391 if (!a->allow_bad_symlinks) {
8392 char target_path[PATH_MAX];
8394 target_len = readlink(ondisk_path, target_path,
8395 sizeof(target_path));
8396 if (target_len == -1) {
8397 err = got_error_from_errno2("readlink",
8401 err = is_bad_symlink_target(&is_bad_symlink,
8402 target_path, target_len, ondisk_path,
8403 a->worktree->root_path);
8406 if (is_bad_symlink) {
8407 err = got_error_path(ondisk_path,
8408 GOT_ERR_BAD_SYMLINK);
8413 got_fileindex_entry_staged_filetype_set(ie,
8414 GOT_FILEIDX_MODE_BAD_SYMLINK);
8416 got_fileindex_entry_staged_filetype_set(ie,
8417 GOT_FILEIDX_MODE_SYMLINK);
8419 got_fileindex_entry_staged_filetype_set(ie,
8420 GOT_FILEIDX_MODE_REGULAR_FILE);
8422 a->staged_something = 1;
8423 if (a->status_cb == NULL)
8425 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8426 get_staged_status(ie), relpath, blob_id,
8427 new_staged_blob_id, NULL, dirfd, de_name);
8431 * When staging the reverse of the staged diff,
8432 * implicitly unstage the file.
8434 if (memcmp(ie->staged_blob_hash, ie->blob_hash,
8435 sizeof(ie->blob_hash)) == 0) {
8436 got_fileindex_entry_stage_set(ie,
8437 GOT_FILEIDX_STAGE_NONE);
8440 case GOT_STATUS_DELETE:
8441 if (staged_status == GOT_STATUS_DELETE)
8444 int choice = GOT_PATCH_CHOICE_NONE;
8445 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8446 ie->path, NULL, 1, 1);
8449 if (choice == GOT_PATCH_CHOICE_NO)
8451 if (choice != GOT_PATCH_CHOICE_YES) {
8452 err = got_error(GOT_ERR_PATCH_CHOICE);
8456 stage = GOT_FILEIDX_STAGE_DELETE;
8457 got_fileindex_entry_stage_set(ie, stage);
8458 a->staged_something = 1;
8459 if (a->status_cb == NULL)
8461 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8462 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8465 case GOT_STATUS_NO_CHANGE:
8467 case GOT_STATUS_CONFLICT:
8468 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8470 case GOT_STATUS_NONEXISTENT:
8471 err = got_error_set_errno(ENOENT, relpath);
8474 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8478 if (path_content && unlink(path_content) == -1 && err == NULL)
8479 err = got_error_from_errno2("unlink", path_content);
8482 free(new_staged_blob_id);
8486 const struct got_error *
8487 got_worktree_stage(struct got_worktree *worktree,
8488 struct got_pathlist_head *paths,
8489 got_worktree_status_cb status_cb, void *status_arg,
8490 got_worktree_patch_cb patch_cb, void *patch_arg,
8491 int allow_bad_symlinks, struct got_repository *repo)
8493 const struct got_error *err = NULL, *sync_err, *unlockerr;
8494 struct got_pathlist_entry *pe;
8495 struct got_fileindex *fileindex = NULL;
8496 char *fileindex_path = NULL;
8497 struct got_reference *head_ref = NULL;
8498 struct got_object_id *head_commit_id = NULL;
8499 struct check_stage_ok_arg oka;
8500 struct stage_path_arg spa;
8502 err = lock_worktree(worktree, LOCK_EX);
8506 err = got_ref_open(&head_ref, repo,
8507 got_worktree_get_head_ref_name(worktree), 0);
8510 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8513 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8517 /* Check pre-conditions before staging anything. */
8518 oka.head_commit_id = head_commit_id;
8519 oka.worktree = worktree;
8520 oka.fileindex = fileindex;
8522 oka.have_changes = 0;
8523 TAILQ_FOREACH(pe, paths, entry) {
8524 err = worktree_status(worktree, pe->path, fileindex, repo,
8525 check_stage_ok, &oka, NULL, NULL, 1, 0);
8529 if (!oka.have_changes) {
8530 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8534 spa.worktree = worktree;
8535 spa.fileindex = fileindex;
8537 spa.patch_cb = patch_cb;
8538 spa.patch_arg = patch_arg;
8539 spa.status_cb = status_cb;
8540 spa.status_arg = status_arg;
8541 spa.staged_something = 0;
8542 spa.allow_bad_symlinks = allow_bad_symlinks;
8543 TAILQ_FOREACH(pe, paths, entry) {
8544 err = worktree_status(worktree, pe->path, fileindex, repo,
8545 stage_path, &spa, NULL, NULL, 1, 0);
8549 if (!spa.staged_something) {
8550 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8554 sync_err = sync_fileindex(fileindex, fileindex_path);
8555 if (sync_err && err == NULL)
8559 got_ref_close(head_ref);
8560 free(head_commit_id);
8561 free(fileindex_path);
8563 got_fileindex_free(fileindex);
8564 unlockerr = lock_worktree(worktree, LOCK_SH);
8565 if (unlockerr && err == NULL)
8570 struct unstage_path_arg {
8571 struct got_worktree *worktree;
8572 struct got_fileindex *fileindex;
8573 struct got_repository *repo;
8574 got_worktree_checkout_cb progress_cb;
8576 got_worktree_patch_cb patch_cb;
8580 static const struct got_error *
8581 create_unstaged_content(char **path_unstaged_content,
8582 char **path_new_staged_content, struct got_object_id *blob_id,
8583 struct got_object_id *staged_blob_id, const char *relpath,
8584 struct got_repository *repo,
8585 got_worktree_patch_cb patch_cb, void *patch_arg)
8587 const struct got_error *err, *free_err;
8588 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8589 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8590 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8591 struct got_diffreg_result *diffreg_result = NULL;
8592 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8593 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8594 int fd1 = -1, fd2 = -1;
8596 *path_unstaged_content = NULL;
8597 *path_new_staged_content = NULL;
8599 err = got_object_id_str(&label1, blob_id);
8603 fd1 = got_opentempfd();
8605 err = got_error_from_errno("got_opentempfd");
8608 fd2 = got_opentempfd();
8610 err = got_error_from_errno("got_opentempfd");
8614 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8618 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
8622 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8626 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8631 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
8635 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8639 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8640 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8644 err = got_opentemp_named(path_unstaged_content, &outfile,
8645 "got-unstaged-content", "");
8648 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8649 "got-new-staged-content", "");
8653 if (fseek(f1, 0L, SEEK_SET) == -1) {
8654 err = got_ferror(f1, GOT_ERR_IO);
8657 if (fseek(f2, 0L, SEEK_SET) == -1) {
8658 err = got_ferror(f2, GOT_ERR_IO);
8661 /* Count the number of actual changes in the diff result. */
8662 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8663 struct diff_chunk_context cc = {};
8664 diff_chunk_context_load_change(&cc, &nchunks_used,
8665 diffreg_result->result, n, 0);
8668 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8670 err = apply_or_reject_change(&choice, &nchunks_used,
8671 diffreg_result->result, n, relpath, f1, f2,
8672 &line_cur1, &line_cur2,
8673 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8676 if (choice == GOT_PATCH_CHOICE_YES)
8679 have_rejected_content = 1;
8680 if (choice == GOT_PATCH_CHOICE_QUIT)
8683 if (have_content || have_rejected_content)
8684 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8685 outfile, rejectfile);
8688 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8689 err = got_error_from_errno("close");
8691 got_object_blob_close(blob);
8692 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8693 err = got_error_from_errno("close");
8695 got_object_blob_close(staged_blob);
8696 free_err = got_diffreg_result_free(diffreg_result);
8697 if (free_err && err == NULL)
8699 if (f1 && fclose(f1) == EOF && err == NULL)
8700 err = got_error_from_errno2("fclose", path1);
8701 if (f2 && fclose(f2) == EOF && err == NULL)
8702 err = got_error_from_errno2("fclose", path2);
8703 if (outfile && fclose(outfile) == EOF && err == NULL)
8704 err = got_error_from_errno2("fclose", *path_unstaged_content);
8705 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8706 err = got_error_from_errno2("fclose", *path_new_staged_content);
8707 if (path1 && unlink(path1) == -1 && err == NULL)
8708 err = got_error_from_errno2("unlink", path1);
8709 if (path2 && unlink(path2) == -1 && err == NULL)
8710 err = got_error_from_errno2("unlink", path2);
8711 if (err || !have_content) {
8712 if (*path_unstaged_content &&
8713 unlink(*path_unstaged_content) == -1 && err == NULL)
8714 err = got_error_from_errno2("unlink",
8715 *path_unstaged_content);
8716 free(*path_unstaged_content);
8717 *path_unstaged_content = NULL;
8719 if (err || !have_content || !have_rejected_content) {
8720 if (*path_new_staged_content &&
8721 unlink(*path_new_staged_content) == -1 && err == NULL)
8722 err = got_error_from_errno2("unlink",
8723 *path_new_staged_content);
8724 free(*path_new_staged_content);
8725 *path_new_staged_content = NULL;
8732 static const struct got_error *
8733 unstage_hunks(struct got_object_id *staged_blob_id,
8734 struct got_blob_object *blob_base,
8735 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8736 const char *ondisk_path, const char *label_orig,
8737 struct got_worktree *worktree, struct got_repository *repo,
8738 got_worktree_patch_cb patch_cb, void *patch_arg,
8739 got_worktree_checkout_cb progress_cb, void *progress_arg)
8741 const struct got_error *err = NULL;
8742 char *path_unstaged_content = NULL;
8743 char *path_new_staged_content = NULL;
8744 char *parent = NULL, *base_path = NULL;
8745 char *blob_base_path = NULL;
8746 struct got_object_id *new_staged_blob_id = NULL;
8747 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8750 err = create_unstaged_content(&path_unstaged_content,
8751 &path_new_staged_content, blob_id, staged_blob_id,
8752 ie->path, repo, patch_cb, patch_arg);
8756 if (path_unstaged_content == NULL)
8759 if (path_new_staged_content) {
8760 err = got_object_blob_create(&new_staged_blob_id,
8761 path_new_staged_content, repo);
8766 f = fopen(path_unstaged_content, "re");
8768 err = got_error_from_errno2("fopen",
8769 path_unstaged_content);
8772 if (fstat(fileno(f), &sb) == -1) {
8773 err = got_error_from_errno2("fstat", path_unstaged_content);
8776 if (got_fileindex_entry_staged_filetype_get(ie) ==
8777 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8778 char link_target[PATH_MAX];
8780 r = fread(link_target, 1, sizeof(link_target), f);
8781 if (r == 0 && ferror(f)) {
8782 err = got_error_from_errno("fread");
8785 if (r >= sizeof(link_target)) { /* should not happen */
8786 err = got_error(GOT_ERR_NO_SPACE);
8789 link_target[r] = '\0';
8790 err = merge_symlink(worktree, blob_base,
8791 ondisk_path, ie->path, label_orig, link_target,
8792 worktree->base_commit_id, repo, progress_cb,
8795 int local_changes_subsumed;
8797 err = got_path_dirname(&parent, ondisk_path);
8801 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8803 err = got_error_from_errno("asprintf");
8808 err = got_opentemp_named(&blob_base_path, &f_base,
8812 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8818 * In order the run a 3-way merge with a symlink we copy the symlink's
8819 * target path into a temporary file and use that file with diff3.
8821 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8822 err = dump_symlink_target_path_to_file(&f_deriv2,
8828 fd = open(ondisk_path,
8829 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8831 err = got_error_from_errno2("open", ondisk_path);
8834 f_deriv2 = fdopen(fd, "r");
8835 if (f_deriv2 == NULL) {
8836 err = got_error_from_errno2("fdopen", ondisk_path);
8842 err = merge_file(&local_changes_subsumed, worktree,
8843 f_base, f, f_deriv2, ondisk_path, ie->path,
8844 got_fileindex_perms_to_st(ie),
8845 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8846 repo, progress_cb, progress_arg);
8851 if (new_staged_blob_id) {
8852 memcpy(ie->staged_blob_hash, new_staged_blob_id->hash,
8853 SHA1_DIGEST_LENGTH);
8855 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8856 got_fileindex_entry_staged_filetype_set(ie, 0);
8859 free(new_staged_blob_id);
8860 if (path_unstaged_content &&
8861 unlink(path_unstaged_content) == -1 && err == NULL)
8862 err = got_error_from_errno2("unlink", path_unstaged_content);
8863 if (path_new_staged_content &&
8864 unlink(path_new_staged_content) == -1 && err == NULL)
8865 err = got_error_from_errno2("unlink", path_new_staged_content);
8866 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8867 err = got_error_from_errno2("unlink", blob_base_path);
8868 if (f_base && fclose(f_base) == EOF && err == NULL)
8869 err = got_error_from_errno2("fclose", path_unstaged_content);
8870 if (f && fclose(f) == EOF && err == NULL)
8871 err = got_error_from_errno2("fclose", path_unstaged_content);
8872 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8873 err = got_error_from_errno2("fclose", ondisk_path);
8874 free(path_unstaged_content);
8875 free(path_new_staged_content);
8876 free(blob_base_path);
8882 static const struct got_error *
8883 unstage_path(void *arg, unsigned char status,
8884 unsigned char staged_status, const char *relpath,
8885 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8886 struct got_object_id *commit_id, int dirfd, const char *de_name)
8888 const struct got_error *err = NULL;
8889 struct unstage_path_arg *a = arg;
8890 struct got_fileindex_entry *ie;
8891 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8892 char *ondisk_path = NULL;
8893 char *id_str = NULL, *label_orig = NULL;
8894 int local_changes_subsumed;
8896 int fd1 = -1, fd2 = -1;
8898 if (staged_status != GOT_STATUS_ADD &&
8899 staged_status != GOT_STATUS_MODIFY &&
8900 staged_status != GOT_STATUS_DELETE)
8903 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8905 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8907 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8909 return got_error_from_errno("asprintf");
8911 err = got_object_id_str(&id_str,
8912 commit_id ? commit_id : a->worktree->base_commit_id);
8915 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8917 err = got_error_from_errno("asprintf");
8921 fd1 = got_opentempfd();
8923 err = got_error_from_errno("got_opentempfd");
8926 fd2 = got_opentempfd();
8928 err = got_error_from_errno("got_opentempfd");
8932 switch (staged_status) {
8933 case GOT_STATUS_MODIFY:
8934 err = got_object_open_as_blob(&blob_base, a->repo,
8935 blob_id, 8192, fd1);
8939 case GOT_STATUS_ADD:
8941 if (staged_status == GOT_STATUS_ADD) {
8942 int choice = GOT_PATCH_CHOICE_NONE;
8943 err = (*a->patch_cb)(&choice, a->patch_arg,
8944 staged_status, ie->path, NULL, 1, 1);
8947 if (choice != GOT_PATCH_CHOICE_YES)
8950 err = unstage_hunks(staged_blob_id,
8951 blob_base, blob_id, ie, ondisk_path,
8952 label_orig, a->worktree, a->repo,
8953 a->patch_cb, a->patch_arg,
8954 a->progress_cb, a->progress_arg);
8955 break; /* Done with this file. */
8958 err = got_object_open_as_blob(&blob_staged, a->repo,
8959 staged_blob_id, 8192, fd2);
8962 switch (got_fileindex_entry_staged_filetype_get(ie)) {
8963 case GOT_FILEIDX_MODE_BAD_SYMLINK:
8964 case GOT_FILEIDX_MODE_REGULAR_FILE:
8965 err = merge_blob(&local_changes_subsumed, a->worktree,
8966 blob_base, ondisk_path, relpath,
8967 got_fileindex_perms_to_st(ie), label_orig,
8968 blob_staged, commit_id ? commit_id :
8969 a->worktree->base_commit_id, a->repo,
8970 a->progress_cb, a->progress_arg);
8972 case GOT_FILEIDX_MODE_SYMLINK:
8973 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8974 char *staged_target;
8975 err = got_object_blob_read_to_str(
8976 &staged_target, blob_staged);
8979 err = merge_symlink(a->worktree, blob_base,
8980 ondisk_path, relpath, label_orig,
8981 staged_target, commit_id ? commit_id :
8982 a->worktree->base_commit_id,
8983 a->repo, a->progress_cb, a->progress_arg);
8984 free(staged_target);
8986 err = merge_blob(&local_changes_subsumed,
8987 a->worktree, blob_base, ondisk_path,
8988 relpath, got_fileindex_perms_to_st(ie),
8989 label_orig, blob_staged,
8990 commit_id ? commit_id :
8991 a->worktree->base_commit_id, a->repo,
8992 a->progress_cb, a->progress_arg);
8996 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9000 got_fileindex_entry_stage_set(ie,
9001 GOT_FILEIDX_STAGE_NONE);
9002 got_fileindex_entry_staged_filetype_set(ie, 0);
9005 case GOT_STATUS_DELETE:
9007 int choice = GOT_PATCH_CHOICE_NONE;
9008 err = (*a->patch_cb)(&choice, a->patch_arg,
9009 staged_status, ie->path, NULL, 1, 1);
9012 if (choice == GOT_PATCH_CHOICE_NO)
9014 if (choice != GOT_PATCH_CHOICE_YES) {
9015 err = got_error(GOT_ERR_PATCH_CHOICE);
9019 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9020 got_fileindex_entry_staged_filetype_set(ie, 0);
9021 err = get_file_status(&status, &sb, ie, ondisk_path,
9022 dirfd, de_name, a->repo);
9025 err = (*a->progress_cb)(a->progress_arg, status, relpath);
9030 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9031 err = got_error_from_errno("close");
9033 got_object_blob_close(blob_base);
9034 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9035 err = got_error_from_errno("close");
9037 got_object_blob_close(blob_staged);
9043 const struct got_error *
9044 got_worktree_unstage(struct got_worktree *worktree,
9045 struct got_pathlist_head *paths,
9046 got_worktree_checkout_cb progress_cb, void *progress_arg,
9047 got_worktree_patch_cb patch_cb, void *patch_arg,
9048 struct got_repository *repo)
9050 const struct got_error *err = NULL, *sync_err, *unlockerr;
9051 struct got_pathlist_entry *pe;
9052 struct got_fileindex *fileindex = NULL;
9053 char *fileindex_path = NULL;
9054 struct unstage_path_arg upa;
9056 err = lock_worktree(worktree, LOCK_EX);
9060 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9064 upa.worktree = worktree;
9065 upa.fileindex = fileindex;
9067 upa.progress_cb = progress_cb;
9068 upa.progress_arg = progress_arg;
9069 upa.patch_cb = patch_cb;
9070 upa.patch_arg = patch_arg;
9071 TAILQ_FOREACH(pe, paths, entry) {
9072 err = worktree_status(worktree, pe->path, fileindex, repo,
9073 unstage_path, &upa, NULL, NULL, 1, 0);
9078 sync_err = sync_fileindex(fileindex, fileindex_path);
9079 if (sync_err && err == NULL)
9082 free(fileindex_path);
9084 got_fileindex_free(fileindex);
9085 unlockerr = lock_worktree(worktree, LOCK_SH);
9086 if (unlockerr && err == NULL)
9091 struct report_file_info_arg {
9092 struct got_worktree *worktree;
9093 got_worktree_path_info_cb info_cb;
9095 struct got_pathlist_head *paths;
9096 got_cancel_cb cancel_cb;
9100 static const struct got_error *
9101 report_file_info(void *arg, struct got_fileindex_entry *ie)
9103 struct report_file_info_arg *a = arg;
9104 struct got_pathlist_entry *pe;
9105 struct got_object_id blob_id, staged_blob_id, commit_id;
9106 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9107 struct got_object_id *commit_idp = NULL;
9110 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9111 return got_error(GOT_ERR_CANCELLED);
9113 TAILQ_FOREACH(pe, a->paths, entry) {
9114 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9115 got_path_is_child(ie->path, pe->path, pe->path_len))
9118 if (pe == NULL) /* not found */
9121 if (got_fileindex_entry_has_blob(ie)) {
9122 memcpy(blob_id.hash, ie->blob_hash, SHA1_DIGEST_LENGTH);
9123 blob_idp = &blob_id;
9125 stage = got_fileindex_entry_stage_get(ie);
9126 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9127 stage == GOT_FILEIDX_STAGE_ADD) {
9128 memcpy(staged_blob_id.hash, ie->staged_blob_hash,
9129 SHA1_DIGEST_LENGTH);
9130 staged_blob_idp = &staged_blob_id;
9133 if (got_fileindex_entry_has_commit(ie)) {
9134 memcpy(commit_id.hash, ie->commit_hash, SHA1_DIGEST_LENGTH);
9135 commit_idp = &commit_id;
9138 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9139 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9142 const struct got_error *
9143 got_worktree_path_info(struct got_worktree *worktree,
9144 struct got_pathlist_head *paths,
9145 got_worktree_path_info_cb info_cb, void *info_arg,
9146 got_cancel_cb cancel_cb, void *cancel_arg)
9149 const struct got_error *err = NULL, *unlockerr;
9150 struct got_fileindex *fileindex = NULL;
9151 char *fileindex_path = NULL;
9152 struct report_file_info_arg arg;
9154 err = lock_worktree(worktree, LOCK_SH);
9158 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9162 arg.worktree = worktree;
9163 arg.info_cb = info_cb;
9164 arg.info_arg = info_arg;
9166 arg.cancel_cb = cancel_cb;
9167 arg.cancel_arg = cancel_arg;
9168 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9171 free(fileindex_path);
9173 got_fileindex_free(fileindex);
9174 unlockerr = lock_worktree(worktree, LOCK_UN);
9175 if (unlockerr && err == NULL)
9180 static const struct got_error *
9181 patch_check_path(const char *p, char **path, unsigned char *status,
9182 unsigned char *staged_status, struct got_fileindex *fileindex,
9183 struct got_worktree *worktree, struct got_repository *repo)
9185 const struct got_error *err;
9186 struct got_fileindex_entry *ie;
9188 char *ondisk_path = NULL;
9190 err = got_worktree_resolve_path(path, worktree, p);
9194 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9195 *path[0] ? "/" : "", *path) == -1)
9196 return got_error_from_errno("asprintf");
9198 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9200 *staged_status = get_staged_status(ie);
9201 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9206 *staged_status = GOT_STATUS_NO_CHANGE;
9207 *status = GOT_STATUS_UNVERSIONED;
9208 if (lstat(ondisk_path, &sb) == -1) {
9209 if (errno != ENOENT) {
9210 err = got_error_from_errno2("lstat",
9214 *status = GOT_STATUS_NONEXISTENT;
9223 static const struct got_error *
9224 patch_can_rm(const char *path, unsigned char status,
9225 unsigned char staged_status)
9227 if (status == GOT_STATUS_NONEXISTENT)
9228 return got_error_set_errno(ENOENT, path);
9229 if (status != GOT_STATUS_NO_CHANGE &&
9230 status != GOT_STATUS_ADD &&
9231 status != GOT_STATUS_MODIFY &&
9232 status != GOT_STATUS_MODE_CHANGE)
9233 return got_error_path(path, GOT_ERR_FILE_STATUS);
9234 if (staged_status == GOT_STATUS_DELETE)
9235 return got_error_path(path, GOT_ERR_FILE_STATUS);
9239 static const struct got_error *
9240 patch_can_add(const char *path, unsigned char status)
9242 if (status != GOT_STATUS_NONEXISTENT)
9243 return got_error_path(path, GOT_ERR_FILE_STATUS);
9247 static const struct got_error *
9248 patch_can_edit(const char *path, unsigned char status,
9249 unsigned char staged_status)
9251 if (status == GOT_STATUS_NONEXISTENT)
9252 return got_error_set_errno(ENOENT, path);
9253 if (status != GOT_STATUS_NO_CHANGE &&
9254 status != GOT_STATUS_ADD &&
9255 status != GOT_STATUS_MODIFY)
9256 return got_error_path(path, GOT_ERR_FILE_STATUS);
9257 if (staged_status == GOT_STATUS_DELETE)
9258 return got_error_path(path, GOT_ERR_FILE_STATUS);
9262 const struct got_error *
9263 got_worktree_patch_prepare(struct got_fileindex **fileindex,
9264 char **fileindex_path, struct got_worktree *worktree)
9266 return open_fileindex(fileindex, fileindex_path, worktree);
9269 const struct got_error *
9270 got_worktree_patch_check_path(const char *old, const char *new,
9271 char **oldpath, char **newpath, struct got_worktree *worktree,
9272 struct got_repository *repo, struct got_fileindex *fileindex)
9274 const struct got_error *err = NULL;
9275 int file_renamed = 0;
9276 unsigned char status_old, staged_status_old;
9277 unsigned char status_new, staged_status_new;
9282 err = patch_check_path(old != NULL ? old : new, oldpath,
9283 &status_old, &staged_status_old, fileindex, worktree, repo);
9287 err = patch_check_path(new != NULL ? new : old, newpath,
9288 &status_new, &staged_status_new, fileindex, worktree, repo);
9292 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9295 if (old != NULL && new == NULL)
9296 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9297 else if (file_renamed) {
9298 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9300 err = patch_can_add(*newpath, status_new);
9301 } else if (old == NULL)
9302 err = patch_can_add(*newpath, status_new);
9304 err = patch_can_edit(*newpath, status_new, staged_status_new);
9316 const struct got_error *
9317 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9318 struct got_worktree *worktree, struct got_fileindex *fileindex,
9319 got_worktree_checkout_cb progress_cb, void *progress_arg)
9321 struct schedule_addition_args saa;
9323 memset(&saa, 0, sizeof(saa));
9324 saa.worktree = worktree;
9325 saa.fileindex = fileindex;
9326 saa.progress_cb = progress_cb;
9327 saa.progress_arg = progress_arg;
9330 return worktree_status(worktree, path, fileindex, repo,
9331 schedule_addition, &saa, NULL, NULL, 1, 0);
9334 const struct got_error *
9335 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9336 struct got_worktree *worktree, struct got_fileindex *fileindex,
9337 got_worktree_delete_cb progress_cb, void *progress_arg)
9339 struct schedule_deletion_args sda;
9341 memset(&sda, 0, sizeof(sda));
9342 sda.worktree = worktree;
9343 sda.fileindex = fileindex;
9344 sda.progress_cb = progress_cb;
9345 sda.progress_arg = progress_arg;
9347 sda.delete_local_mods = 0;
9348 sda.keep_on_disk = 0;
9349 sda.ignore_missing_paths = 0;
9350 sda.status_codes = NULL;
9352 return worktree_status(worktree, path, fileindex, repo,
9353 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9356 const struct got_error *
9357 got_worktree_patch_complete(struct got_fileindex *fileindex,
9358 const char *fileindex_path)
9360 const struct got_error *err = NULL;
9362 err = sync_fileindex(fileindex, fileindex_path);
9363 got_fileindex_free(fileindex);