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->sha1, base_commit_id->sha1, 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 || errno == ENOTDIR) {
1413 err = got_path_dirname(&parent, path);
1416 err = add_dir_on_disk(worktree, parent);
1417 if (err && err->code == GOT_ERR_FILE_OBSTRUCTED)
1418 err = got_error_path(path, err->code);
1422 fd = open(ondisk_path,
1423 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1426 return got_error_from_errno2("open",
1428 } else if (errno == EEXIST) {
1429 if (path_is_unversioned) {
1430 err = (*progress_cb)(progress_arg,
1431 GOT_STATUS_UNVERSIONED, path);
1434 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1435 !S_ISREG(st_mode) && !installing_bad_symlink) {
1436 /* TODO file is obstructed; do something */
1437 err = got_error_path(ondisk_path,
1438 GOT_ERR_FILE_OBSTRUCTED);
1441 err = got_opentemp_named_fd(&tmppath, &fd,
1447 if (fchmod(fd, apply_umask(mode)) == -1) {
1448 err = got_error_from_errno2("fchmod",
1454 return got_error_from_errno2("open", ondisk_path);
1458 if (restoring_missing_file)
1459 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1461 else if (reverting_versioned_file)
1462 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1465 err = (*progress_cb)(progress_arg,
1466 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1471 hdrlen = got_object_blob_get_hdrlen(blob);
1473 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1474 err = got_object_blob_read_block(&len, blob);
1478 /* Skip blob object header first time around. */
1479 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1481 err = got_error_from_errno("write");
1483 } else if (outlen != len - hdrlen) {
1484 err = got_error(GOT_ERR_IO);
1491 if (fsync(fd) != 0) {
1492 err = got_error_from_errno("fsync");
1497 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1498 err = got_error_from_errno2("unlink", ondisk_path);
1501 if (rename(tmppath, ondisk_path) != 0) {
1502 err = got_error_from_errno3("rename", tmppath,
1511 if (fd != -1 && close(fd) == -1 && err == NULL)
1512 err = got_error_from_errno("close");
1513 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1514 err = got_error_from_errno2("unlink", tmppath);
1520 * Upgrade STATUS_MODIFY to STATUS_CONFLICT if a
1521 * conflict marker is found in newly added lines only.
1523 static const struct got_error *
1524 get_modified_file_content_status(unsigned char *status,
1525 struct got_blob_object *blob, const char *path, struct stat *sb,
1528 const struct got_error *err, *free_err;
1529 const char *markers[3] = {
1530 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1531 GOT_DIFF_CONFLICT_MARKER_SEP,
1532 GOT_DIFF_CONFLICT_MARKER_END
1535 struct got_diffreg_result *diffreg_result = NULL;
1536 struct diff_result *r;
1537 int nchunks_parsed, n, i = 0, ln = 0;
1539 size_t linesize = 0;
1542 if (*status != GOT_STATUS_MODIFY)
1545 f1 = got_opentemp();
1547 return got_error_from_errno("got_opentemp");
1550 got_object_blob_rewind(blob);
1551 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
1556 err = got_diff_files(&diffreg_result, f1, 1, NULL, ondisk_file,
1557 1, NULL, 0, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
1561 r = diffreg_result->result;
1563 for (n = 0; n < r->chunks.len; n += nchunks_parsed) {
1564 struct diff_chunk *c;
1565 struct diff_chunk_context cc = {};
1569 * We can optimise a little by advancing straight
1570 * to the next chunk if this one has no added lines.
1572 c = diff_chunk_get(r, n);
1574 if (diff_chunk_type(c) != CHUNK_PLUS) {
1576 continue; /* removed or unchanged lines */
1579 pos = diff_chunk_get_right_start_pos(c);
1580 if (fseek(ondisk_file, pos, SEEK_SET) == -1) {
1581 err = got_ferror(ondisk_file, GOT_ERR_IO);
1585 diff_chunk_context_load_change(&cc, &nchunks_parsed, r, n, 0);
1586 ln = cc.right.start;
1588 while (ln < cc.right.end) {
1589 linelen = getline(&line, &linesize, ondisk_file);
1590 if (linelen == -1) {
1591 if (feof(ondisk_file))
1593 err = got_ferror(ondisk_file, GOT_ERR_IO);
1597 if (line && strncmp(line, markers[i],
1598 strlen(markers[i])) == 0) {
1599 if (strcmp(markers[i],
1600 GOT_DIFF_CONFLICT_MARKER_END) == 0) {
1601 *status = GOT_STATUS_CONFLICT;
1612 if (f1 != NULL && fclose(f1) == EOF && err == NULL)
1613 err = got_error_from_errno("fclose");
1614 free_err = got_diffreg_result_free(diffreg_result);
1622 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1624 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1625 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1629 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1631 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1632 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1633 ie->mtime_sec == sb->st_mtim.tv_sec &&
1634 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1635 ie->size == (sb->st_size & 0xffffffff) &&
1636 !xbit_differs(ie, sb->st_mode));
1639 static unsigned char
1640 get_staged_status(struct got_fileindex_entry *ie)
1642 switch (got_fileindex_entry_stage_get(ie)) {
1643 case GOT_FILEIDX_STAGE_ADD:
1644 return GOT_STATUS_ADD;
1645 case GOT_FILEIDX_STAGE_DELETE:
1646 return GOT_STATUS_DELETE;
1647 case GOT_FILEIDX_STAGE_MODIFY:
1648 return GOT_STATUS_MODIFY;
1650 return GOT_STATUS_NO_CHANGE;
1654 static const struct got_error *
1655 get_symlink_modification_status(unsigned char *status,
1656 struct got_fileindex_entry *ie, const char *abspath,
1657 int dirfd, const char *de_name, struct got_blob_object *blob)
1659 const struct got_error *err = NULL;
1660 char target_path[PATH_MAX];
1661 char etarget[PATH_MAX];
1663 size_t len, target_len = 0;
1664 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1665 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1667 *status = GOT_STATUS_NO_CHANGE;
1669 /* Blob object content specifies the target path of the link. */
1671 err = got_object_blob_read_block(&len, blob);
1674 if (len + target_len >= sizeof(target_path)) {
1676 * Should not happen. The blob contents were OK
1677 * when this symlink was installed.
1679 return got_error(GOT_ERR_NO_SPACE);
1682 /* Skip blob object header first time around. */
1683 memcpy(target_path + target_len, buf + hdrlen,
1685 target_len += len - hdrlen;
1689 target_path[target_len] = '\0';
1692 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1694 return got_error_from_errno2("readlinkat", abspath);
1696 elen = readlink(abspath, etarget, sizeof(etarget));
1698 return got_error_from_errno2("readlink", abspath);
1701 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1702 *status = GOT_STATUS_MODIFY;
1707 static const struct got_error *
1708 get_file_status(unsigned char *status, struct stat *sb,
1709 struct got_fileindex_entry *ie, const char *abspath,
1710 int dirfd, const char *de_name, struct got_repository *repo)
1712 const struct got_error *err = NULL;
1713 struct got_object_id id;
1715 int fd = -1, fd1 = -1;
1718 struct got_blob_object *blob = NULL;
1720 unsigned char staged_status;
1722 staged_status = get_staged_status(ie);
1723 *status = GOT_STATUS_NO_CHANGE;
1724 memset(sb, 0, sizeof(*sb));
1727 * Whenever the caller provides a directory descriptor and a
1728 * directory entry name for the file, use them! This prevents
1729 * race conditions if filesystem paths change beneath our feet.
1732 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1733 if (errno == ENOENT) {
1734 if (got_fileindex_entry_has_file_on_disk(ie))
1735 *status = GOT_STATUS_MISSING;
1737 *status = GOT_STATUS_DELETE;
1740 err = got_error_from_errno2("fstatat", abspath);
1744 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1745 if (fd == -1 && errno != ENOENT &&
1746 !got_err_open_nofollow_on_symlink())
1747 return got_error_from_errno2("open", abspath);
1748 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1749 if (lstat(abspath, sb) == -1)
1750 return got_error_from_errno2("lstat", abspath);
1751 } else if (fd == -1 || fstat(fd, sb) == -1) {
1752 if (errno == ENOENT) {
1753 if (got_fileindex_entry_has_file_on_disk(ie))
1754 *status = GOT_STATUS_MISSING;
1756 *status = GOT_STATUS_DELETE;
1759 err = got_error_from_errno2("fstat", abspath);
1764 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1765 *status = GOT_STATUS_OBSTRUCTED;
1769 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1770 *status = GOT_STATUS_DELETE;
1772 } else if (!got_fileindex_entry_has_blob(ie) &&
1773 staged_status != GOT_STATUS_ADD) {
1774 *status = GOT_STATUS_ADD;
1778 if (!stat_info_differs(ie, sb))
1781 if (S_ISLNK(sb->st_mode) &&
1782 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1783 *status = GOT_STATUS_MODIFY;
1787 if (staged_status == GOT_STATUS_MODIFY ||
1788 staged_status == GOT_STATUS_ADD)
1789 got_fileindex_entry_get_staged_blob_id(&id, ie);
1791 got_fileindex_entry_get_blob_id(&id, ie);
1793 fd1 = got_opentempfd();
1795 err = got_error_from_errno("got_opentempfd");
1798 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1802 if (S_ISLNK(sb->st_mode)) {
1803 err = get_symlink_modification_status(status, ie,
1804 abspath, dirfd, de_name, blob);
1809 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1811 err = got_error_from_errno2("openat", abspath);
1816 f = fdopen(fd, "r");
1818 err = got_error_from_errno2("fdopen", abspath);
1822 hdrlen = got_object_blob_get_hdrlen(blob);
1824 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1825 err = got_object_blob_read_block(&blen, blob);
1828 /* Skip length of blob object header first time around. */
1829 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1830 if (flen == 0 && ferror(f)) {
1831 err = got_error_from_errno("fread");
1834 if (blen - hdrlen == 0) {
1836 *status = GOT_STATUS_MODIFY;
1838 } else if (flen == 0) {
1839 if (blen - hdrlen != 0)
1840 *status = GOT_STATUS_MODIFY;
1842 } else if (blen - hdrlen == flen) {
1843 /* Skip blob object header first time around. */
1844 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1845 *status = GOT_STATUS_MODIFY;
1849 *status = GOT_STATUS_MODIFY;
1855 if (*status == GOT_STATUS_MODIFY) {
1857 err = get_modified_file_content_status(status, blob, ie->path,
1859 } else if (xbit_differs(ie, sb->st_mode))
1860 *status = GOT_STATUS_MODE_CHANGE;
1862 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1863 err = got_error_from_errno("close");
1865 got_object_blob_close(blob);
1866 if (f != NULL && fclose(f) == EOF && err == NULL)
1867 err = got_error_from_errno2("fclose", abspath);
1868 if (fd != -1 && close(fd) == -1 && err == NULL)
1869 err = got_error_from_errno2("close", abspath);
1874 * Update timestamps in the file index if a file is unmodified and
1875 * we had to run a full content comparison to find out.
1877 static const struct got_error *
1878 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1879 struct got_fileindex_entry *ie, struct stat *sb)
1881 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1882 return got_fileindex_entry_update(ie, wt_fd, path,
1883 ie->blob_sha1, ie->commit_sha1, 1);
1888 static const struct got_error *remove_ondisk_file(const char *, const char *);
1890 static const struct got_error *
1891 update_blob(struct got_worktree *worktree,
1892 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1893 struct got_tree_entry *te, const char *path,
1894 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1897 const struct got_error *err = NULL;
1898 struct got_blob_object *blob = NULL;
1899 char *ondisk_path = NULL;
1900 unsigned char status = GOT_STATUS_NO_CHANGE;
1902 int fd1 = -1, fd2 = -1;
1904 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1905 return got_error_from_errno("asprintf");
1908 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1909 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1912 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1916 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1917 sb.st_mode = got_fileindex_perms_to_st(ie);
1919 if (stat(ondisk_path, &sb) == -1) {
1920 if (errno != ENOENT && errno != ENOTDIR) {
1921 err = got_error_from_errno2("stat",
1925 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1926 status = GOT_STATUS_UNVERSIONED;
1928 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1929 status = GOT_STATUS_UNVERSIONED;
1931 status = GOT_STATUS_OBSTRUCTED;
1935 if (status == GOT_STATUS_OBSTRUCTED) {
1937 got_fileindex_entry_mark_skipped(ie);
1938 err = (*progress_cb)(progress_arg, status, path);
1941 if (status == GOT_STATUS_CONFLICT) {
1943 got_fileindex_entry_mark_skipped(ie);
1944 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1949 if (S_ISDIR(te->mode)) { /* file changing into a directory */
1950 if (status == GOT_STATUS_UNVERSIONED) {
1951 err = (*progress_cb)(progress_arg, status, path);
1952 } else if (status != GOT_STATUS_NO_CHANGE &&
1953 status != GOT_STATUS_DELETE &&
1954 status != GOT_STATUS_NONEXISTENT &&
1955 status != GOT_STATUS_MISSING) {
1956 err = (*progress_cb)(progress_arg,
1957 GOT_STATUS_CANNOT_DELETE, path);
1959 if (status != GOT_STATUS_DELETE &&
1960 status != GOT_STATUS_NONEXISTENT &&
1961 status != GOT_STATUS_MISSING) {
1962 err = remove_ondisk_file(worktree->root_path,
1964 if (err && !(err->code == GOT_ERR_ERRNO &&
1968 got_fileindex_entry_remove(fileindex, ie);
1969 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE,
1972 goto done; /* nothing else to do */
1975 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1976 (S_ISLNK(te->mode) ||
1977 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1979 * This is a regular file or an installed bad symlink.
1980 * If the file index indicates that this file is already
1981 * up-to-date with respect to the repository we can skip
1982 * updating contents of this file.
1984 if (got_fileindex_entry_has_commit(ie) &&
1985 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1986 SHA1_DIGEST_LENGTH) == 0) {
1988 err = sync_timestamps(worktree->root_fd,
1989 path, status, ie, &sb);
1992 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1996 if (got_fileindex_entry_has_blob(ie) &&
1997 memcmp(ie->blob_sha1, te->id.sha1,
1998 SHA1_DIGEST_LENGTH) == 0) {
1999 /* Different commit but the same blob. */
2000 if (got_fileindex_entry_has_commit(ie)) {
2001 /* Update the base commit ID of this file. */
2002 memcpy(ie->commit_sha1,
2003 worktree->base_commit_id->sha1,
2004 sizeof(ie->commit_sha1));
2006 err = sync_timestamps(worktree->root_fd,
2007 path, status, ie, &sb);
2010 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2016 fd1 = got_opentempfd();
2018 err = got_error_from_errno("got_opentempfd");
2021 err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
2025 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
2026 int update_timestamps;
2027 struct got_blob_object *blob2 = NULL;
2028 char *label_orig = NULL;
2029 if (got_fileindex_entry_has_blob(ie)) {
2030 fd2 = got_opentempfd();
2032 err = got_error_from_errno("got_opentempfd");
2035 struct got_object_id id2;
2036 got_fileindex_entry_get_blob_id(&id2, ie);
2037 err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
2042 if (got_fileindex_entry_has_commit(ie)) {
2043 char id_str[SHA1_DIGEST_STRING_LENGTH];
2044 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2045 sizeof(id_str)) == NULL) {
2046 err = got_error_path(id_str,
2047 GOT_ERR_BAD_OBJ_ID_STR);
2050 if (asprintf(&label_orig, "%s: commit %s",
2051 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2052 err = got_error_from_errno("asprintf");
2056 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2058 err = got_object_blob_read_to_str(&link_target, blob);
2061 err = merge_symlink(worktree, blob2, ondisk_path, path,
2062 label_orig, link_target, worktree->base_commit_id,
2063 repo, progress_cb, progress_arg);
2066 err = merge_blob(&update_timestamps, worktree, blob2,
2067 ondisk_path, path, sb.st_mode, label_orig, blob,
2068 worktree->base_commit_id, repo,
2069 progress_cb, progress_arg);
2072 if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2073 err = got_error_from_errno("close");
2077 got_object_blob_close(blob2);
2081 * Do not update timestamps of files with local changes.
2082 * Otherwise, a future status walk would treat them as
2083 * unmodified files again.
2085 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2086 blob->id.sha1, worktree->base_commit_id->sha1,
2088 } else if (status == GOT_STATUS_MODE_CHANGE) {
2089 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2090 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2091 } else if (status == GOT_STATUS_DELETE) {
2092 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2095 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2096 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2100 int is_bad_symlink = 0;
2101 if (S_ISLNK(te->mode)) {
2102 err = install_symlink(&is_bad_symlink, worktree,
2103 ondisk_path, path, blob,
2104 status == GOT_STATUS_MISSING, 0,
2105 status == GOT_STATUS_UNVERSIONED, 0,
2106 repo, progress_cb, progress_arg);
2108 err = install_blob(worktree, ondisk_path, path,
2109 te->mode, sb.st_mode, blob,
2110 status == GOT_STATUS_MISSING, 0, 0,
2111 status == GOT_STATUS_UNVERSIONED, repo,
2112 progress_cb, progress_arg);
2118 err = got_fileindex_entry_update(ie,
2119 worktree->root_fd, path, blob->id.sha1,
2120 worktree->base_commit_id->sha1, 1);
2122 err = create_fileindex_entry(&ie, fileindex,
2123 worktree->base_commit_id, worktree->root_fd, path,
2129 if (is_bad_symlink) {
2130 got_fileindex_entry_filetype_set(ie,
2131 GOT_FILEIDX_MODE_BAD_SYMLINK);
2135 if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2136 err = got_error_from_errno("close");
2139 got_object_blob_close(blob);
2145 static const struct got_error *
2146 remove_ondisk_file(const char *root_path, const char *path)
2148 const struct got_error *err = NULL;
2149 char *ondisk_path = NULL, *parent = NULL;
2151 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2152 return got_error_from_errno("asprintf");
2154 if (unlink(ondisk_path) == -1) {
2155 if (errno != ENOENT)
2156 err = got_error_from_errno2("unlink", ondisk_path);
2158 size_t root_len = strlen(root_path);
2159 err = got_path_dirname(&parent, ondisk_path);
2162 while (got_path_cmp(parent, root_path,
2163 strlen(parent), root_len) != 0) {
2165 ondisk_path = parent;
2167 if (rmdir(ondisk_path) == -1) {
2168 if (errno != ENOTEMPTY)
2169 err = got_error_from_errno2("rmdir",
2173 err = got_path_dirname(&parent, ondisk_path);
2184 static const struct got_error *
2185 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2186 struct got_fileindex_entry *ie, struct got_repository *repo,
2187 got_worktree_checkout_cb progress_cb, void *progress_arg)
2189 const struct got_error *err = NULL;
2190 unsigned char status;
2194 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2195 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2197 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2199 return got_error_from_errno("asprintf");
2201 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2205 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2206 char ondisk_target[PATH_MAX];
2207 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2208 sizeof(ondisk_target));
2209 if (ondisk_len == -1) {
2210 err = got_error_from_errno2("readlink", ondisk_path);
2213 ondisk_target[ondisk_len] = '\0';
2214 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2215 NULL, NULL, /* XXX pass common ancestor info? */
2216 ondisk_target, ondisk_path);
2219 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2224 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2225 status == GOT_STATUS_ADD) {
2226 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2230 * Preserve the working file and change the deleted blob's
2231 * entry into a schedule-add entry.
2233 err = got_fileindex_entry_update(ie, worktree->root_fd,
2234 ie->path, NULL, NULL, 0);
2236 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2239 if (status == GOT_STATUS_NO_CHANGE) {
2240 err = remove_ondisk_file(worktree->root_path, ie->path);
2244 got_fileindex_entry_remove(fileindex, ie);
2251 struct diff_cb_arg {
2252 struct got_fileindex *fileindex;
2253 struct got_worktree *worktree;
2254 struct got_repository *repo;
2255 got_worktree_checkout_cb progress_cb;
2257 got_cancel_cb cancel_cb;
2261 static const struct got_error *
2262 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2263 struct got_tree_entry *te, const char *parent_path)
2265 struct diff_cb_arg *a = arg;
2267 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2268 return got_error(GOT_ERR_CANCELLED);
2270 return update_blob(a->worktree, a->fileindex, ie, te,
2271 ie->path, a->repo, a->progress_cb, a->progress_arg);
2274 static const struct got_error *
2275 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2277 struct diff_cb_arg *a = arg;
2279 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2280 return got_error(GOT_ERR_CANCELLED);
2282 return delete_blob(a->worktree, a->fileindex, ie,
2283 a->repo, a->progress_cb, a->progress_arg);
2286 static const struct got_error *
2287 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2289 struct diff_cb_arg *a = arg;
2290 const struct got_error *err;
2293 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2294 return got_error(GOT_ERR_CANCELLED);
2296 if (got_object_tree_entry_is_submodule(te))
2299 if (asprintf(&path, "%s%s%s", parent_path,
2300 parent_path[0] ? "/" : "", te->name)
2302 return got_error_from_errno("asprintf");
2304 if (S_ISDIR(te->mode))
2305 err = add_dir_on_disk(a->worktree, path);
2307 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2308 a->repo, a->progress_cb, a->progress_arg);
2314 const struct got_error *
2315 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2317 uint32_t uuid_status;
2319 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2320 if (uuid_status != uuid_s_ok) {
2322 return got_error_uuid(uuid_status, "uuid_to_string");
2328 static const struct got_error *
2329 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2331 const struct got_error *err = NULL;
2332 char *uuidstr = NULL;
2336 err = got_worktree_get_uuid(&uuidstr, worktree);
2340 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2341 err = got_error_from_errno("asprintf");
2348 const struct got_error *
2349 got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2352 return get_ref_name(refname, worktree, prefix);
2355 const struct got_error *
2356 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2358 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2361 static const struct got_error *
2362 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2364 return get_ref_name(refname, worktree,
2365 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2368 static const struct got_error *
2369 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2371 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2374 static const struct got_error *
2375 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2377 return get_ref_name(refname, worktree,
2378 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2381 static const struct got_error *
2382 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2384 return get_ref_name(refname, worktree,
2385 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2388 static const struct got_error *
2389 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2391 return get_ref_name(refname, worktree,
2392 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2395 static const struct got_error *
2396 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2398 return get_ref_name(refname, worktree,
2399 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2402 static const struct got_error *
2403 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2405 return get_ref_name(refname, worktree,
2406 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2409 static const struct got_error *
2410 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2412 return get_ref_name(refname, worktree,
2413 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2416 const struct got_error *
2417 got_worktree_get_histedit_script_path(char **path,
2418 struct got_worktree *worktree)
2420 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2421 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2423 return got_error_from_errno("asprintf");
2428 static const struct got_error *
2429 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2431 return get_ref_name(refname, worktree,
2432 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2435 static const struct got_error *
2436 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2438 return get_ref_name(refname, worktree,
2439 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2443 * Prevent Git's garbage collector from deleting our base commit by
2444 * setting a reference to our base commit's ID.
2446 static const struct got_error *
2447 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2449 const struct got_error *err = NULL;
2450 struct got_reference *ref = NULL;
2453 err = got_worktree_get_base_ref_name(&refname, worktree);
2457 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2461 err = got_ref_write(ref, repo);
2469 static const struct got_error *
2470 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2472 const struct got_error *err = NULL;
2474 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2475 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2476 err = got_error_from_errno("asprintf");
2477 *fileindex_path = NULL;
2483 static const struct got_error *
2484 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2485 struct got_worktree *worktree)
2487 const struct got_error *err = NULL;
2490 *fileindex_path = NULL;
2491 *fileindex = got_fileindex_alloc();
2492 if (*fileindex == NULL)
2493 return got_error_from_errno("got_fileindex_alloc");
2495 err = get_fileindex_path(fileindex_path, worktree);
2499 index = fopen(*fileindex_path, "rbe");
2500 if (index == NULL) {
2501 if (errno != ENOENT)
2502 err = got_error_from_errno2("fopen", *fileindex_path);
2504 err = got_fileindex_read(*fileindex, index);
2505 if (fclose(index) == EOF && err == NULL)
2506 err = got_error_from_errno("fclose");
2510 free(*fileindex_path);
2511 *fileindex_path = NULL;
2512 got_fileindex_free(*fileindex);
2518 struct bump_base_commit_id_arg {
2519 struct got_object_id *base_commit_id;
2522 const char *entry_name;
2523 got_worktree_checkout_cb progress_cb;
2527 static const struct got_error *
2528 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2530 const struct got_error *err;
2531 struct bump_base_commit_id_arg *a = arg;
2533 if (a->entry_name) {
2534 if (strcmp(ie->path, a->path) != 0)
2536 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2539 if (got_fileindex_entry_was_skipped(ie))
2542 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2543 SHA1_DIGEST_LENGTH) == 0)
2546 if (a->progress_cb) {
2547 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2552 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2556 /* Bump base commit ID of all files within an updated part of the work tree. */
2557 static const struct got_error *
2558 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2559 struct got_fileindex *fileindex,
2560 got_worktree_checkout_cb progress_cb, void *progress_arg)
2562 struct bump_base_commit_id_arg bbc_arg;
2564 bbc_arg.base_commit_id = worktree->base_commit_id;
2565 bbc_arg.entry_name = NULL;
2567 bbc_arg.path_len = 0;
2568 bbc_arg.progress_cb = progress_cb;
2569 bbc_arg.progress_arg = progress_arg;
2571 return got_fileindex_for_each_entry_safe(fileindex,
2572 bump_base_commit_id, &bbc_arg);
2575 static const struct got_error *
2576 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2578 const struct got_error *err = NULL;
2579 char *new_fileindex_path = NULL;
2580 FILE *new_index = NULL;
2581 struct timespec timeout;
2583 err = got_opentemp_named(&new_fileindex_path, &new_index,
2584 fileindex_path, "");
2588 err = got_fileindex_write(fileindex, new_index);
2592 if (rename(new_fileindex_path, fileindex_path) != 0) {
2593 err = got_error_from_errno3("rename", new_fileindex_path,
2595 unlink(new_fileindex_path);
2599 * Sleep for a short amount of time to ensure that files modified after
2600 * this program exits have a different time stamp from the one which
2601 * was recorded in the file index.
2604 timeout.tv_nsec = 1;
2605 nanosleep(&timeout, NULL);
2609 free(new_fileindex_path);
2613 static const struct got_error *
2614 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2615 struct got_object_id **tree_id, const char *wt_relpath,
2616 struct got_commit_object *base_commit, struct got_worktree *worktree,
2617 struct got_repository *repo)
2619 const struct got_error *err = NULL;
2620 struct got_object_id *id = NULL;
2621 char *in_repo_path = NULL;
2622 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2624 *entry_type = GOT_OBJ_TYPE_ANY;
2625 *tree_relpath = NULL;
2628 if (wt_relpath[0] == '\0') {
2629 /* Check out all files within the work tree. */
2630 *entry_type = GOT_OBJ_TYPE_TREE;
2631 *tree_relpath = strdup("");
2632 if (*tree_relpath == NULL) {
2633 err = got_error_from_errno("strdup");
2636 err = got_object_id_by_path(tree_id, repo, base_commit,
2637 worktree->path_prefix);
2643 /* Check out a subset of files in the work tree. */
2645 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2646 is_root_wt ? "" : "/", wt_relpath) == -1) {
2647 err = got_error_from_errno("asprintf");
2651 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2656 in_repo_path = NULL;
2658 err = got_object_get_type(entry_type, repo, id);
2662 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2663 /* Check out a single file. */
2664 if (strchr(wt_relpath, '/') == NULL) {
2665 /* Check out a single file in work tree's root dir. */
2666 in_repo_path = strdup(worktree->path_prefix);
2667 if (in_repo_path == NULL) {
2668 err = got_error_from_errno("strdup");
2671 *tree_relpath = strdup("");
2672 if (*tree_relpath == NULL) {
2673 err = got_error_from_errno("strdup");
2677 /* Check out a single file in a subdirectory. */
2678 err = got_path_dirname(tree_relpath, wt_relpath);
2681 if (asprintf(&in_repo_path, "%s%s%s",
2682 worktree->path_prefix, is_root_wt ? "" : "/",
2683 *tree_relpath) == -1) {
2684 err = got_error_from_errno("asprintf");
2688 err = got_object_id_by_path(tree_id, repo,
2689 base_commit, in_repo_path);
2691 /* Check out all files within a subdirectory. */
2692 *tree_id = got_object_id_dup(id);
2693 if (*tree_id == NULL) {
2694 err = got_error_from_errno("got_object_id_dup");
2697 *tree_relpath = strdup(wt_relpath);
2698 if (*tree_relpath == NULL) {
2699 err = got_error_from_errno("strdup");
2707 *entry_type = GOT_OBJ_TYPE_ANY;
2708 free(*tree_relpath);
2709 *tree_relpath = NULL;
2716 static const struct got_error *
2717 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2718 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2719 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2720 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2722 const struct got_error *err = NULL;
2723 struct got_commit_object *commit = NULL;
2724 struct got_tree_object *tree = NULL;
2725 struct got_fileindex_diff_tree_cb diff_cb;
2726 struct diff_cb_arg arg;
2728 err = ref_base_commit(worktree, repo);
2730 if (!(err->code == GOT_ERR_ERRNO &&
2731 (errno == EACCES || errno == EROFS)))
2733 err = (*progress_cb)(progress_arg,
2734 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2739 err = got_object_open_as_commit(&commit, repo,
2740 worktree->base_commit_id);
2744 err = got_object_open_as_tree(&tree, repo, tree_id);
2749 got_object_tree_find_entry(tree, entry_name) == NULL) {
2750 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2754 diff_cb.diff_old_new = diff_old_new;
2755 diff_cb.diff_old = diff_old;
2756 diff_cb.diff_new = diff_new;
2757 arg.fileindex = fileindex;
2758 arg.worktree = worktree;
2760 arg.progress_cb = progress_cb;
2761 arg.progress_arg = progress_arg;
2762 arg.cancel_cb = cancel_cb;
2763 arg.cancel_arg = cancel_arg;
2764 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2765 entry_name, repo, &diff_cb, &arg);
2768 got_object_tree_close(tree);
2770 got_object_commit_close(commit);
2774 const struct got_error *
2775 got_worktree_checkout_files(struct got_worktree *worktree,
2776 struct got_pathlist_head *paths, struct got_repository *repo,
2777 got_worktree_checkout_cb progress_cb, void *progress_arg,
2778 got_cancel_cb cancel_cb, void *cancel_arg)
2780 const struct got_error *err = NULL, *sync_err, *unlockerr;
2781 struct got_commit_object *commit = NULL;
2782 struct got_tree_object *tree = NULL;
2783 struct got_fileindex *fileindex = NULL;
2784 char *fileindex_path = NULL;
2785 struct got_pathlist_entry *pe;
2786 struct tree_path_data {
2787 STAILQ_ENTRY(tree_path_data) entry;
2788 struct got_object_id *tree_id;
2793 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2795 STAILQ_INIT(&tree_paths);
2797 err = lock_worktree(worktree, LOCK_EX);
2801 err = got_object_open_as_commit(&commit, repo,
2802 worktree->base_commit_id);
2806 /* Map all specified paths to in-repository trees. */
2807 TAILQ_FOREACH(pe, paths, entry) {
2808 tpd = malloc(sizeof(*tpd));
2810 err = got_error_from_errno("malloc");
2814 err = find_tree_entry_for_checkout(&tpd->entry_type,
2815 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2822 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2823 err = got_path_basename(&tpd->entry_name, pe->path);
2831 tpd->entry_name = NULL;
2833 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2837 * Read the file index.
2838 * Checking out files is supposed to be an idempotent operation.
2839 * If the on-disk file index is incomplete we will try to complete it.
2841 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2845 tpd = STAILQ_FIRST(&tree_paths);
2846 TAILQ_FOREACH(pe, paths, entry) {
2847 struct bump_base_commit_id_arg bbc_arg;
2849 err = checkout_files(worktree, fileindex, tpd->relpath,
2850 tpd->tree_id, tpd->entry_name, repo,
2851 progress_cb, progress_arg, cancel_cb, cancel_arg);
2855 bbc_arg.base_commit_id = worktree->base_commit_id;
2856 bbc_arg.entry_name = tpd->entry_name;
2857 bbc_arg.path = pe->path;
2858 bbc_arg.path_len = pe->path_len;
2859 bbc_arg.progress_cb = progress_cb;
2860 bbc_arg.progress_arg = progress_arg;
2861 err = got_fileindex_for_each_entry_safe(fileindex,
2862 bump_base_commit_id, &bbc_arg);
2866 tpd = STAILQ_NEXT(tpd, entry);
2868 sync_err = sync_fileindex(fileindex, fileindex_path);
2869 if (sync_err && err == NULL)
2872 free(fileindex_path);
2874 got_object_tree_close(tree);
2876 got_object_commit_close(commit);
2878 got_fileindex_free(fileindex);
2879 while (!STAILQ_EMPTY(&tree_paths)) {
2880 tpd = STAILQ_FIRST(&tree_paths);
2881 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2886 unlockerr = lock_worktree(worktree, LOCK_SH);
2887 if (unlockerr && err == NULL)
2892 static const struct got_error *
2893 add_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2894 struct got_fileindex_entry *ie, const char *ondisk_path,
2895 const char *path2, struct got_blob_object *blob2, mode_t mode2,
2896 int restoring_missing_file, int reverting_versioned_file,
2897 int path_is_unversioned, int allow_bad_symlinks,
2898 struct got_repository *repo,
2899 got_worktree_checkout_cb progress_cb, void *progress_arg)
2901 const struct got_error *err = NULL;
2902 int is_bad_symlink = 0;
2904 if (S_ISLNK(mode2)) {
2905 err = install_symlink(&is_bad_symlink,
2906 worktree, ondisk_path, path2, blob2,
2907 restoring_missing_file,
2908 reverting_versioned_file,
2909 path_is_unversioned, allow_bad_symlinks,
2910 repo, progress_cb, progress_arg);
2912 err = install_blob(worktree, ondisk_path, path2,
2913 mode2, GOT_DEFAULT_FILE_MODE, blob2,
2914 restoring_missing_file, reverting_versioned_file, 0,
2915 path_is_unversioned, repo, progress_cb, progress_arg);
2920 /* Adding an unversioned file. */
2921 err = got_fileindex_entry_alloc(&ie, path2);
2924 err = got_fileindex_entry_update(ie,
2925 worktree->root_fd, path2, NULL, NULL, 1);
2927 got_fileindex_entry_free(ie);
2930 err = got_fileindex_entry_add(fileindex, ie);
2932 got_fileindex_entry_free(ie);
2936 /* Re-adding a locally deleted file. */
2937 err = got_fileindex_entry_update(ie,
2938 worktree->root_fd, path2, ie->blob_sha1,
2939 worktree->base_commit_id->sha1, 0);
2944 if (is_bad_symlink) {
2945 got_fileindex_entry_filetype_set(ie,
2946 GOT_FILEIDX_MODE_BAD_SYMLINK);
2952 struct merge_file_cb_arg {
2953 struct got_worktree *worktree;
2954 struct got_fileindex *fileindex;
2955 got_worktree_checkout_cb progress_cb;
2957 got_cancel_cb cancel_cb;
2959 const char *label_orig;
2960 struct got_object_id *commit_id2;
2961 int allow_bad_symlinks;
2964 static const struct got_error *
2965 merge_file_cb(void *arg, struct got_blob_object *blob1,
2966 struct got_blob_object *blob2, FILE *f1, FILE *f2,
2967 struct got_object_id *id1, struct got_object_id *id2,
2968 const char *path1, const char *path2,
2969 mode_t mode1, mode_t mode2, struct got_repository *repo)
2971 static const struct got_error *err = NULL;
2972 struct merge_file_cb_arg *a = arg;
2973 struct got_fileindex_entry *ie;
2974 char *ondisk_path = NULL;
2976 unsigned char status;
2977 int local_changes_subsumed;
2978 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2979 char *id_str = NULL, *label_deriv2 = NULL;
2981 if (blob1 && blob2) {
2982 ie = got_fileindex_entry_get(a->fileindex, path2,
2985 return (*a->progress_cb)(a->progress_arg,
2986 GOT_STATUS_MISSING, path2);
2988 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2990 return got_error_from_errno("asprintf");
2992 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2997 if (status == GOT_STATUS_DELETE) {
2998 err = (*a->progress_cb)(a->progress_arg,
2999 GOT_STATUS_MERGE, path2);
3002 if (status != GOT_STATUS_NO_CHANGE &&
3003 status != GOT_STATUS_MODIFY &&
3004 status != GOT_STATUS_CONFLICT &&
3005 status != GOT_STATUS_ADD) {
3006 err = (*a->progress_cb)(a->progress_arg, status, path2);
3010 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
3012 err = got_object_blob_read_to_str(&link_target2, blob2);
3015 err = merge_symlink(a->worktree, blob1, ondisk_path,
3016 path2, a->label_orig, link_target2, a->commit_id2,
3017 repo, a->progress_cb, a->progress_arg);
3022 f_orig = got_opentemp();
3023 if (f_orig == NULL) {
3024 err = got_error_from_errno("got_opentemp");
3027 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3032 f_deriv2 = got_opentemp();
3033 if (f_deriv2 == NULL)
3035 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3040 fd = open(ondisk_path,
3041 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3043 err = got_error_from_errno2("open",
3047 f_deriv = fdopen(fd, "r");
3048 if (f_deriv == NULL) {
3049 err = got_error_from_errno2("fdopen",
3054 err = got_object_id_str(&id_str, a->commit_id2);
3057 if (asprintf(&label_deriv2, "%s: commit %s",
3058 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
3059 err = got_error_from_errno("asprintf");
3062 err = merge_file(&local_changes_subsumed, a->worktree,
3063 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
3064 mode2, a->label_orig, NULL, label_deriv2,
3065 GOT_DIFF_ALGORITHM_PATIENCE, repo,
3066 a->progress_cb, a->progress_arg);
3069 ie = got_fileindex_entry_get(a->fileindex, path1,
3072 return (*a->progress_cb)(a->progress_arg,
3073 GOT_STATUS_MISSING, path1);
3075 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3077 return got_error_from_errno("asprintf");
3079 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3085 case GOT_STATUS_NO_CHANGE:
3086 err = (*a->progress_cb)(a->progress_arg,
3087 GOT_STATUS_DELETE, path1);
3090 err = remove_ondisk_file(a->worktree->root_path, path1);
3094 got_fileindex_entry_mark_deleted_from_disk(ie);
3096 case GOT_STATUS_DELETE:
3097 case GOT_STATUS_MISSING:
3098 err = (*a->progress_cb)(a->progress_arg,
3099 GOT_STATUS_DELETE, path1);
3103 got_fileindex_entry_mark_deleted_from_disk(ie);
3105 case GOT_STATUS_ADD: {
3106 struct got_object_id *id;
3110 * Delete the added file only if its content already
3111 * exists in the repository.
3113 err = got_object_blob_file_create(&id, &blob1_f,
3114 &blob1_size, path1);
3117 if (got_object_id_cmp(id, id1) == 0) {
3118 err = (*a->progress_cb)(a->progress_arg,
3119 GOT_STATUS_DELETE, path1);
3122 err = remove_ondisk_file(a->worktree->root_path,
3127 got_fileindex_entry_remove(a->fileindex,
3130 err = (*a->progress_cb)(a->progress_arg,
3131 GOT_STATUS_CANNOT_DELETE, path1);
3133 if (fclose(blob1_f) == EOF && err == NULL)
3134 err = got_error_from_errno("fclose");
3140 case GOT_STATUS_MODIFY:
3141 case GOT_STATUS_CONFLICT:
3142 err = (*a->progress_cb)(a->progress_arg,
3143 GOT_STATUS_CANNOT_DELETE, path1);
3147 case GOT_STATUS_OBSTRUCTED:
3148 err = (*a->progress_cb)(a->progress_arg, status, path1);
3156 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3158 return got_error_from_errno("asprintf");
3159 ie = got_fileindex_entry_get(a->fileindex, path2,
3162 err = get_file_status(&status, &sb, ie, ondisk_path,
3166 if (status != GOT_STATUS_NO_CHANGE &&
3167 status != GOT_STATUS_MODIFY &&
3168 status != GOT_STATUS_CONFLICT &&
3169 status != GOT_STATUS_ADD &&
3170 status != GOT_STATUS_DELETE) {
3171 err = (*a->progress_cb)(a->progress_arg,
3175 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3177 err = got_object_blob_read_to_str(&link_target2,
3181 err = merge_symlink(a->worktree, NULL,
3182 ondisk_path, path2, a->label_orig,
3183 link_target2, a->commit_id2, repo,
3184 a->progress_cb, a->progress_arg);
3186 } else if (S_ISREG(sb.st_mode)) {
3187 err = merge_blob(&local_changes_subsumed,
3188 a->worktree, NULL, ondisk_path, path2,
3189 sb.st_mode, a->label_orig, blob2,
3190 a->commit_id2, repo, a->progress_cb,
3192 } else if (status != GOT_STATUS_DELETE) {
3193 err = got_error_path(ondisk_path,
3194 GOT_ERR_FILE_OBSTRUCTED);
3198 if (status == GOT_STATUS_DELETE) {
3199 /* Re-add file with content from new blob. */
3200 err = add_file(a->worktree, a->fileindex, ie,
3201 ondisk_path, path2, blob2, mode2,
3202 0, 0, 0, a->allow_bad_symlinks,
3203 repo, a->progress_cb, a->progress_arg);
3208 err = add_file(a->worktree, a->fileindex, NULL,
3209 ondisk_path, path2, blob2, mode2,
3210 0, 0, 1, a->allow_bad_symlinks,
3211 repo, a->progress_cb, a->progress_arg);
3217 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3218 err = got_error_from_errno("fclose");
3219 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3220 err = got_error_from_errno("fclose");
3221 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3222 err = got_error_from_errno("fclose");
3229 static const struct got_error *
3230 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3232 struct got_worktree *worktree = arg;
3234 /* Reject merges into a work tree with mixed base commits. */
3235 if (got_fileindex_entry_has_commit(ie) &&
3236 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3237 SHA1_DIGEST_LENGTH) != 0)
3238 return got_error(GOT_ERR_MIXED_COMMITS);
3243 struct check_merge_conflicts_arg {
3244 struct got_worktree *worktree;
3245 struct got_fileindex *fileindex;
3246 struct got_repository *repo;
3249 static const struct got_error *
3250 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3251 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3252 struct got_object_id *id1, struct got_object_id *id2,
3253 const char *path1, const char *path2,
3254 mode_t mode1, mode_t mode2, struct got_repository *repo)
3256 const struct got_error *err = NULL;
3257 struct check_merge_conflicts_arg *a = arg;
3258 unsigned char status;
3260 struct got_fileindex_entry *ie;
3261 const char *path = path2 ? path2 : path1;
3262 struct got_object_id *id = id2 ? id2 : id1;
3268 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3272 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3274 return got_error_from_errno("asprintf");
3276 /* Reject merges into a work tree with conflicted files. */
3277 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3281 if (status == GOT_STATUS_CONFLICT)
3282 return got_error(GOT_ERR_CONFLICTS);
3287 static const struct got_error *
3288 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3289 const char *fileindex_path, struct got_object_id *commit_id1,
3290 struct got_object_id *commit_id2, struct got_repository *repo,
3291 got_worktree_checkout_cb progress_cb, void *progress_arg,
3292 got_cancel_cb cancel_cb, void *cancel_arg)
3294 const struct got_error *err = NULL, *sync_err;
3295 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3296 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3297 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3298 struct check_merge_conflicts_arg cmc_arg;
3299 struct merge_file_cb_arg arg;
3300 char *label_orig = NULL;
3301 FILE *f1 = NULL, *f2 = NULL;
3302 int fd1 = -1, fd2 = -1;
3305 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3308 err = got_object_id_by_path(&tree_id1, repo, commit1,
3309 worktree->path_prefix);
3310 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3316 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3320 err = got_object_id_str(&id_str, commit_id1);
3324 if (asprintf(&label_orig, "%s: commit %s",
3325 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3326 err = got_error_from_errno("asprintf");
3332 f1 = got_opentemp();
3334 err = got_error_from_errno("got_opentemp");
3339 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3343 err = got_object_id_by_path(&tree_id2, repo, commit2,
3344 worktree->path_prefix);
3348 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3352 f2 = got_opentemp();
3354 err = got_error_from_errno("got_opentemp");
3358 fd1 = got_opentempfd();
3360 err = got_error_from_errno("got_opentempfd");
3364 fd2 = got_opentempfd();
3366 err = got_error_from_errno("got_opentempfd");
3370 cmc_arg.worktree = worktree;
3371 cmc_arg.fileindex = fileindex;
3372 cmc_arg.repo = repo;
3373 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3374 check_merge_conflicts, &cmc_arg, 0);
3378 arg.worktree = worktree;
3379 arg.fileindex = fileindex;
3380 arg.progress_cb = progress_cb;
3381 arg.progress_arg = progress_arg;
3382 arg.cancel_cb = cancel_cb;
3383 arg.cancel_arg = cancel_arg;
3384 arg.label_orig = label_orig;
3385 arg.commit_id2 = commit_id2;
3386 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3387 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3388 merge_file_cb, &arg, 1);
3389 sync_err = sync_fileindex(fileindex, fileindex_path);
3390 if (sync_err && err == NULL)
3394 got_object_commit_close(commit1);
3396 got_object_commit_close(commit2);
3398 got_object_tree_close(tree1);
3400 got_object_tree_close(tree2);
3401 if (f1 && fclose(f1) == EOF && err == NULL)
3402 err = got_error_from_errno("fclose");
3403 if (f2 && fclose(f2) == EOF && err == NULL)
3404 err = got_error_from_errno("fclose");
3405 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3406 err = got_error_from_errno("close");
3407 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3408 err = got_error_from_errno("close");
3413 const struct got_error *
3414 got_worktree_merge_files(struct got_worktree *worktree,
3415 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3416 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3417 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3419 const struct got_error *err, *unlockerr;
3420 char *fileindex_path = NULL;
3421 struct got_fileindex *fileindex = NULL;
3423 err = lock_worktree(worktree, LOCK_EX);
3427 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3431 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3436 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3437 commit_id2, repo, progress_cb, progress_arg,
3438 cancel_cb, cancel_arg);
3441 got_fileindex_free(fileindex);
3442 free(fileindex_path);
3443 unlockerr = lock_worktree(worktree, LOCK_SH);
3444 if (unlockerr && err == NULL)
3449 struct diff_dir_cb_arg {
3450 struct got_fileindex *fileindex;
3451 struct got_worktree *worktree;
3452 const char *status_path;
3453 size_t status_path_len;
3454 struct got_repository *repo;
3455 got_worktree_status_cb status_cb;
3457 got_cancel_cb cancel_cb;
3459 /* A pathlist containing per-directory pathlists of ignore patterns. */
3460 struct got_pathlist_head *ignores;
3461 int report_unchanged;
3465 static const struct got_error *
3466 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3467 int dirfd, const char *de_name,
3468 got_worktree_status_cb status_cb, void *status_arg,
3469 struct got_repository *repo, int report_unchanged)
3471 const struct got_error *err = NULL;
3472 unsigned char status = GOT_STATUS_NO_CHANGE;
3473 unsigned char staged_status;
3475 struct got_object_id blob_id, commit_id, staged_blob_id;
3476 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3477 struct got_object_id *staged_blob_idp = NULL;
3479 staged_status = get_staged_status(ie);
3480 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3484 if (status == GOT_STATUS_NO_CHANGE &&
3485 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3488 if (got_fileindex_entry_has_blob(ie))
3489 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3490 if (got_fileindex_entry_has_commit(ie))
3491 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3492 if (staged_status == GOT_STATUS_ADD ||
3493 staged_status == GOT_STATUS_MODIFY) {
3494 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3495 &staged_blob_id, ie);
3498 return (*status_cb)(status_arg, status, staged_status,
3499 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3502 static const struct got_error *
3503 status_old_new(void *arg, struct got_fileindex_entry *ie,
3504 struct dirent *de, const char *parent_path, int dirfd)
3506 const struct got_error *err = NULL;
3507 struct diff_dir_cb_arg *a = arg;
3510 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3511 return got_error(GOT_ERR_CANCELLED);
3513 if (got_path_cmp(parent_path, a->status_path,
3514 strlen(parent_path), a->status_path_len) != 0 &&
3515 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3518 if (parent_path[0]) {
3519 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3520 parent_path, de->d_name) == -1)
3521 return got_error_from_errno("asprintf");
3523 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3525 return got_error_from_errno("asprintf");
3528 err = report_file_status(ie, abspath, dirfd, de->d_name,
3529 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3534 static const struct got_error *
3535 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3537 struct diff_dir_cb_arg *a = arg;
3538 struct got_object_id blob_id, commit_id;
3539 unsigned char status;
3541 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3542 return got_error(GOT_ERR_CANCELLED);
3544 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3547 got_fileindex_entry_get_blob_id(&blob_id, ie);
3548 got_fileindex_entry_get_commit_id(&commit_id, ie);
3549 if (got_fileindex_entry_has_file_on_disk(ie))
3550 status = GOT_STATUS_MISSING;
3552 status = GOT_STATUS_DELETE;
3553 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3554 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3558 free_ignores(struct got_pathlist_head *ignores)
3560 struct got_pathlist_entry *pe;
3562 TAILQ_FOREACH(pe, ignores, entry) {
3563 struct got_pathlist_head *ignorelist = pe->data;
3565 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3567 got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3570 static const struct got_error *
3571 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3573 const struct got_error *err = NULL;
3574 struct got_pathlist_entry *pe = NULL;
3575 struct got_pathlist_head *ignorelist;
3576 char *line = NULL, *pattern, *dirpath = NULL;
3577 size_t linesize = 0;
3580 ignorelist = calloc(1, sizeof(*ignorelist));
3581 if (ignorelist == NULL)
3582 return got_error_from_errno("calloc");
3583 TAILQ_INIT(ignorelist);
3585 while ((linelen = getline(&line, &linesize, f)) != -1) {
3586 if (linelen > 0 && line[linelen - 1] == '\n')
3587 line[linelen - 1] = '\0';
3589 /* Git's ignores may contain comments. */
3593 /* Git's negated patterns are not (yet?) supported. */
3597 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3599 err = got_error_from_errno("asprintf");
3602 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3607 err = got_error_from_errno("getline");
3611 dirpath = strdup(path);
3612 if (dirpath == NULL) {
3613 err = got_error_from_errno("strdup");
3616 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3619 if (err || pe == NULL) {
3621 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3627 match_path(const char *pattern, size_t pattern_len, const char *path,
3633 * Trailing slashes signify directories.
3634 * Append a * to make such patterns conform to fnmatch rules.
3636 if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
3637 if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
3638 return FNM_NOMATCH; /* XXX */
3640 return fnmatch(buf, path, flags);
3643 return fnmatch(pattern, path, flags);
3647 match_ignores(struct got_pathlist_head *ignores, const char *path)
3649 struct got_pathlist_entry *pe;
3651 /* Handle patterns which match in all directories. */
3652 TAILQ_FOREACH(pe, ignores, entry) {
3653 struct got_pathlist_head *ignorelist = pe->data;
3654 struct got_pathlist_entry *pi;
3656 TAILQ_FOREACH(pi, ignorelist, entry) {
3659 if (pi->path_len < 3 ||
3660 strncmp(pi->path, "**/", 3) != 0)
3664 if (match_path(pi->path + 3,
3665 pi->path_len - 3, p,
3666 FNM_PATHNAME | FNM_LEADING_DIR)) {
3667 /* Retry in next directory. */
3668 while (*p && *p != '/')
3680 * The ignores pathlist contains ignore lists from children before
3681 * parents, so we can find the most specific ignorelist by walking
3682 * ignores backwards.
3684 pe = TAILQ_LAST(ignores, got_pathlist_head);
3686 if (got_path_is_child(path, pe->path, pe->path_len)) {
3687 struct got_pathlist_head *ignorelist = pe->data;
3688 struct got_pathlist_entry *pi;
3689 TAILQ_FOREACH(pi, ignorelist, entry) {
3690 int flags = FNM_LEADING_DIR;
3691 if (strstr(pi->path, "/**/") == NULL)
3692 flags |= FNM_PATHNAME;
3693 if (match_path(pi->path, pi->path_len,
3699 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3705 static const struct got_error *
3706 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3707 const char *path, int dirfd, const char *ignores_filename)
3709 const struct got_error *err = NULL;
3712 FILE *ignoresfile = NULL;
3714 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3715 path[0] ? "/" : "", ignores_filename) == -1)
3716 return got_error_from_errno("asprintf");
3719 fd = openat(dirfd, ignores_filename,
3720 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3722 if (errno != ENOENT && errno != EACCES)
3723 err = got_error_from_errno2("openat",
3726 ignoresfile = fdopen(fd, "r");
3727 if (ignoresfile == NULL)
3728 err = got_error_from_errno2("fdopen",
3732 err = read_ignores(ignores, path, ignoresfile);
3736 ignoresfile = fopen(ignorespath, "re");
3737 if (ignoresfile == NULL) {
3738 if (errno != ENOENT && errno != EACCES)
3739 err = got_error_from_errno2("fopen",
3742 err = read_ignores(ignores, path, ignoresfile);
3745 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3746 err = got_error_from_errno2("fclose", path);
3747 if (fd != -1 && close(fd) == -1 && err == NULL)
3748 err = got_error_from_errno2("close", path);
3753 static const struct got_error *
3754 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3757 const struct got_error *err = NULL;
3758 struct diff_dir_cb_arg *a = arg;
3764 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3765 return got_error(GOT_ERR_CANCELLED);
3767 if (parent_path[0]) {
3768 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3769 return got_error_from_errno("asprintf");
3774 if (de->d_type == DT_DIR) {
3775 if (!a->no_ignores && ignore != NULL &&
3776 match_ignores(a->ignores, path))
3778 } else if (!match_ignores(a->ignores, path) &&
3779 got_path_is_child(path, a->status_path, a->status_path_len))
3780 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3781 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3787 static const struct got_error *
3788 status_traverse(void *arg, const char *path, int dirfd)
3790 const struct got_error *err = NULL;
3791 struct diff_dir_cb_arg *a = arg;
3796 err = add_ignores(a->ignores, a->worktree->root_path,
3797 path, dirfd, ".cvsignore");
3801 err = add_ignores(a->ignores, a->worktree->root_path, path,
3802 dirfd, ".gitignore");
3807 static const struct got_error *
3808 report_single_file_status(const char *path, const char *ondisk_path,
3809 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3810 void *status_arg, struct got_repository *repo, int report_unchanged,
3811 struct got_pathlist_head *ignores, int no_ignores)
3813 struct got_fileindex_entry *ie;
3816 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3818 return report_file_status(ie, ondisk_path, -1, NULL,
3819 status_cb, status_arg, repo, report_unchanged);
3821 if (lstat(ondisk_path, &sb) == -1) {
3822 if (errno != ENOENT)
3823 return got_error_from_errno2("lstat", ondisk_path);
3824 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3825 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3828 if (!no_ignores && match_ignores(ignores, path))
3831 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3832 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3833 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3838 static const struct got_error *
3839 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3840 const char *root_path, const char *path)
3842 const struct got_error *err;
3843 char *parent_path, *next_parent_path = NULL;
3845 err = add_ignores(ignores, root_path, "", -1,
3850 err = add_ignores(ignores, root_path, "", -1,
3855 err = got_path_dirname(&parent_path, path);
3857 if (err->code == GOT_ERR_BAD_PATH)
3858 return NULL; /* cannot traverse parent */
3862 err = add_ignores(ignores, root_path, parent_path, -1,
3866 err = add_ignores(ignores, root_path, parent_path, -1,
3870 err = got_path_dirname(&next_parent_path, parent_path);
3872 if (err->code == GOT_ERR_BAD_PATH)
3873 err = NULL; /* traversed everything */
3876 if (got_path_is_root_dir(parent_path))
3879 parent_path = next_parent_path;
3880 next_parent_path = NULL;
3884 free(next_parent_path);
3888 struct find_missing_children_args {
3889 const char *parent_path;
3891 struct got_pathlist_head *children;
3892 got_cancel_cb cancel_cb;
3896 static const struct got_error *
3897 find_missing_children(void *arg, struct got_fileindex_entry *ie)
3899 const struct got_error *err = NULL;
3900 struct find_missing_children_args *a = arg;
3903 err = a->cancel_cb(a->cancel_arg);
3908 if (got_path_is_child(ie->path, a->parent_path, a->parent_len))
3909 err = got_pathlist_append(a->children, ie->path, NULL);
3914 static const struct got_error *
3915 report_children(struct got_pathlist_head *children,
3916 struct got_worktree *worktree, struct got_fileindex *fileindex,
3917 struct got_repository *repo, int is_root_dir, int report_unchanged,
3918 struct got_pathlist_head *ignores, int no_ignores,
3919 got_worktree_status_cb status_cb, void *status_arg,
3920 got_cancel_cb cancel_cb, void *cancel_arg)
3922 const struct got_error *err = NULL;
3923 struct got_pathlist_entry *pe;
3924 char *ondisk_path = NULL;
3926 TAILQ_FOREACH(pe, children, entry) {
3928 err = cancel_cb(cancel_arg);
3933 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
3934 !is_root_dir ? "/" : "", pe->path) == -1) {
3935 err = got_error_from_errno("asprintf");
3940 err = report_single_file_status(pe->path, ondisk_path,
3941 fileindex, status_cb, status_arg, repo, report_unchanged,
3942 ignores, no_ignores);
3954 static const struct got_error *
3955 worktree_status(struct got_worktree *worktree, const char *path,
3956 struct got_fileindex *fileindex, struct got_repository *repo,
3957 got_worktree_status_cb status_cb, void *status_arg,
3958 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3959 int report_unchanged)
3961 const struct got_error *err = NULL;
3963 struct got_fileindex_diff_dir_cb fdiff_cb;
3964 struct diff_dir_cb_arg arg;
3965 char *ondisk_path = NULL;
3966 struct got_pathlist_head ignores, missing_children;
3967 struct got_fileindex_entry *ie;
3969 TAILQ_INIT(&ignores);
3970 TAILQ_INIT(&missing_children);
3972 if (asprintf(&ondisk_path, "%s%s%s",
3973 worktree->root_path, path[0] ? "/" : "", path) == -1)
3974 return got_error_from_errno("asprintf");
3976 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3978 err = report_single_file_status(path, ondisk_path,
3979 fileindex, status_cb, status_arg, repo,
3980 report_unchanged, &ignores, no_ignores);
3983 struct find_missing_children_args fmca;
3984 fmca.parent_path = path;
3985 fmca.parent_len = strlen(path);
3986 fmca.children = &missing_children;
3987 fmca.cancel_cb = cancel_cb;
3988 fmca.cancel_arg = cancel_arg;
3989 err = got_fileindex_for_each_entry_safe(fileindex,
3990 find_missing_children, &fmca);
3995 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3997 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3998 !got_err_open_nofollow_on_symlink())
3999 err = got_error_from_errno2("open", ondisk_path);
4002 err = add_ignores_from_parent_paths(&ignores,
4003 worktree->root_path, ondisk_path);
4007 if (TAILQ_EMPTY(&missing_children)) {
4008 err = report_single_file_status(path,
4009 ondisk_path, fileindex,
4010 status_cb, status_arg, repo,
4011 report_unchanged, &ignores, no_ignores);
4015 err = report_children(&missing_children,
4016 worktree, fileindex, repo,
4017 (path[0] == '\0'), report_unchanged,
4018 &ignores, no_ignores,
4019 status_cb, status_arg,
4020 cancel_cb, cancel_arg);
4026 fdiff_cb.diff_old_new = status_old_new;
4027 fdiff_cb.diff_old = status_old;
4028 fdiff_cb.diff_new = status_new;
4029 fdiff_cb.diff_traverse = status_traverse;
4030 arg.fileindex = fileindex;
4031 arg.worktree = worktree;
4032 arg.status_path = path;
4033 arg.status_path_len = strlen(path);
4035 arg.status_cb = status_cb;
4036 arg.status_arg = status_arg;
4037 arg.cancel_cb = cancel_cb;
4038 arg.cancel_arg = cancel_arg;
4039 arg.report_unchanged = report_unchanged;
4040 arg.no_ignores = no_ignores;
4042 err = add_ignores_from_parent_paths(&ignores,
4043 worktree->root_path, path);
4047 arg.ignores = &ignores;
4048 err = got_fileindex_diff_dir(fileindex, fd,
4049 worktree->root_path, path, repo, &fdiff_cb, &arg);
4052 free_ignores(&ignores);
4053 if (fd != -1 && close(fd) == -1 && err == NULL)
4054 err = got_error_from_errno("close");
4059 const struct got_error *
4060 got_worktree_status(struct got_worktree *worktree,
4061 struct got_pathlist_head *paths, struct got_repository *repo,
4062 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
4063 got_cancel_cb cancel_cb, void *cancel_arg)
4065 const struct got_error *err = NULL;
4066 char *fileindex_path = NULL;
4067 struct got_fileindex *fileindex = NULL;
4068 struct got_pathlist_entry *pe;
4070 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4074 TAILQ_FOREACH(pe, paths, entry) {
4075 err = worktree_status(worktree, pe->path, fileindex, repo,
4076 status_cb, status_arg, cancel_cb, cancel_arg,
4081 free(fileindex_path);
4082 got_fileindex_free(fileindex);
4086 const struct got_error *
4087 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
4090 const struct got_error *err = NULL;
4091 char *resolved = NULL, *cwd = NULL, *path = NULL;
4094 char *abspath = NULL;
4095 char canonpath[PATH_MAX];
4099 cwd = getcwd(NULL, 0);
4101 return got_error_from_errno("getcwd");
4103 if (lstat(arg, &sb) == -1) {
4104 if (errno != ENOENT) {
4105 err = got_error_from_errno2("lstat", arg);
4110 if (S_ISLNK(sb.st_mode)) {
4112 * We cannot use realpath(3) with symlinks since we want to
4113 * operate on the symlink itself.
4114 * But we can make the path absolute, assuming it is relative
4115 * to the current working directory, and then canonicalize it.
4117 if (!got_path_is_absolute(arg)) {
4118 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4119 err = got_error_from_errno("asprintf");
4124 err = got_canonpath(abspath ? abspath : arg, canonpath,
4128 resolved = strdup(canonpath);
4129 if (resolved == NULL) {
4130 err = got_error_from_errno("strdup");
4134 resolved = realpath(arg, NULL);
4135 if (resolved == NULL) {
4136 if (errno != ENOENT) {
4137 err = got_error_from_errno2("realpath", arg);
4140 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4141 err = got_error_from_errno("asprintf");
4144 err = got_canonpath(abspath, canonpath,
4148 resolved = strdup(canonpath);
4149 if (resolved == NULL) {
4150 err = got_error_from_errno("strdup");
4156 if (strncmp(got_worktree_get_root_path(worktree), resolved,
4157 strlen(got_worktree_get_root_path(worktree)))) {
4158 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
4162 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
4163 err = got_path_skip_common_ancestor(&path,
4164 got_worktree_get_root_path(worktree), resolved);
4170 err = got_error_from_errno("strdup");
4175 /* XXX status walk can't deal with trailing slash! */
4177 while (len > 0 && path[len - 1] == '/') {
4178 path[len - 1] = '\0';
4192 struct schedule_addition_args {
4193 struct got_worktree *worktree;
4194 struct got_fileindex *fileindex;
4195 got_worktree_checkout_cb progress_cb;
4197 struct got_repository *repo;
4201 add_noop_status(unsigned char status)
4203 return (status == GOT_STATUS_ADD ||
4204 status == GOT_STATUS_MODIFY ||
4205 status == GOT_STATUS_CONFLICT ||
4206 status == GOT_STATUS_MODE_CHANGE ||
4207 status == GOT_STATUS_NO_CHANGE);
4210 static const struct got_error *
4211 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4212 const char *relpath, struct got_object_id *blob_id,
4213 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4214 int dirfd, const char *de_name)
4216 struct schedule_addition_args *a = arg;
4217 const struct got_error *err = NULL;
4218 struct got_fileindex_entry *ie;
4222 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4224 return got_error_from_errno("asprintf");
4226 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4228 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4232 /* Re-adding an existing entry is a no-op. */
4233 if (staged_status == GOT_STATUS_NO_CHANGE &&
4234 add_noop_status(status))
4236 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4241 if (status != GOT_STATUS_UNVERSIONED) {
4242 if (status == GOT_STATUS_NONEXISTENT)
4243 err = got_error_set_errno(ENOENT, ondisk_path);
4245 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4249 err = got_fileindex_entry_alloc(&ie, relpath);
4252 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4253 relpath, NULL, NULL, 1);
4255 got_fileindex_entry_free(ie);
4258 err = got_fileindex_entry_add(a->fileindex, ie);
4260 got_fileindex_entry_free(ie);
4267 if (staged_status == GOT_STATUS_NO_CHANGE && add_noop_status(status))
4269 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4272 const struct got_error *
4273 got_worktree_schedule_add(struct got_worktree *worktree,
4274 struct got_pathlist_head *paths,
4275 got_worktree_checkout_cb progress_cb, void *progress_arg,
4276 struct got_repository *repo, int no_ignores)
4278 struct got_fileindex *fileindex = NULL;
4279 char *fileindex_path = NULL;
4280 const struct got_error *err = NULL, *sync_err, *unlockerr;
4281 struct got_pathlist_entry *pe;
4282 struct schedule_addition_args saa;
4284 err = lock_worktree(worktree, LOCK_EX);
4288 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4292 saa.worktree = worktree;
4293 saa.fileindex = fileindex;
4294 saa.progress_cb = progress_cb;
4295 saa.progress_arg = progress_arg;
4298 TAILQ_FOREACH(pe, paths, entry) {
4299 err = worktree_status(worktree, pe->path, fileindex, repo,
4300 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4304 sync_err = sync_fileindex(fileindex, fileindex_path);
4305 if (sync_err && err == NULL)
4308 free(fileindex_path);
4310 got_fileindex_free(fileindex);
4311 unlockerr = lock_worktree(worktree, LOCK_SH);
4312 if (unlockerr && err == NULL)
4317 struct schedule_deletion_args {
4318 struct got_worktree *worktree;
4319 struct got_fileindex *fileindex;
4320 got_worktree_delete_cb progress_cb;
4322 struct got_repository *repo;
4323 int delete_local_mods;
4325 int ignore_missing_paths;
4326 const char *status_path;
4327 size_t status_path_len;
4328 const char *status_codes;
4331 static const struct got_error *
4332 schedule_for_deletion(void *arg, unsigned char status,
4333 unsigned char staged_status, const char *relpath,
4334 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4335 struct got_object_id *commit_id, int dirfd, const char *de_name)
4337 struct schedule_deletion_args *a = arg;
4338 const struct got_error *err = NULL;
4339 struct got_fileindex_entry *ie = NULL;
4343 if (status == GOT_STATUS_NONEXISTENT) {
4344 if (a->ignore_missing_paths)
4346 return got_error_set_errno(ENOENT, relpath);
4349 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4351 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4353 staged_status = get_staged_status(ie);
4354 if (staged_status != GOT_STATUS_NO_CHANGE) {
4355 if (staged_status == GOT_STATUS_DELETE)
4357 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4360 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4362 return got_error_from_errno("asprintf");
4364 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4369 if (a->status_codes) {
4370 size_t ncodes = strlen(a->status_codes);
4372 for (i = 0; i < ncodes ; i++) {
4373 if (status == a->status_codes[i])
4377 /* Do not delete files in non-matching status. */
4381 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4382 a->status_codes[i] != GOT_STATUS_MISSING) {
4383 static char msg[64];
4384 snprintf(msg, sizeof(msg),
4385 "invalid status code '%c'", a->status_codes[i]);
4386 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4391 if (status != GOT_STATUS_NO_CHANGE) {
4392 if (status == GOT_STATUS_DELETE)
4394 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4395 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4398 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4399 err = got_error_set_errno(ENOENT, relpath);
4402 if (status != GOT_STATUS_MODIFY &&
4403 status != GOT_STATUS_MISSING) {
4404 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4409 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4413 if (unlinkat(dirfd, de_name, 0) == -1) {
4414 err = got_error_from_errno2("unlinkat",
4418 } else if (unlink(ondisk_path) == -1) {
4419 err = got_error_from_errno2("unlink", ondisk_path);
4423 root_len = strlen(a->worktree->root_path);
4427 err = got_path_dirname(&parent, ondisk_path);
4431 ondisk_path = parent;
4432 if (got_path_cmp(ondisk_path, a->status_path,
4433 strlen(ondisk_path), a->status_path_len) != 0 &&
4434 !got_path_is_child(ondisk_path, a->status_path,
4435 a->status_path_len))
4437 if (rmdir(ondisk_path) == -1) {
4438 if (errno != ENOTEMPTY)
4439 err = got_error_from_errno2("rmdir",
4443 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4444 strlen(ondisk_path), root_len) != 0);
4447 got_fileindex_entry_mark_deleted_from_disk(ie);
4452 if (status == GOT_STATUS_DELETE)
4454 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4455 staged_status, relpath);
4458 const struct got_error *
4459 got_worktree_schedule_delete(struct got_worktree *worktree,
4460 struct got_pathlist_head *paths, int delete_local_mods,
4461 const char *status_codes,
4462 got_worktree_delete_cb progress_cb, void *progress_arg,
4463 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4465 struct got_fileindex *fileindex = NULL;
4466 char *fileindex_path = NULL;
4467 const struct got_error *err = NULL, *sync_err, *unlockerr;
4468 struct got_pathlist_entry *pe;
4469 struct schedule_deletion_args sda;
4471 err = lock_worktree(worktree, LOCK_EX);
4475 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4479 sda.worktree = worktree;
4480 sda.fileindex = fileindex;
4481 sda.progress_cb = progress_cb;
4482 sda.progress_arg = progress_arg;
4484 sda.delete_local_mods = delete_local_mods;
4485 sda.keep_on_disk = keep_on_disk;
4486 sda.ignore_missing_paths = ignore_missing_paths;
4487 sda.status_codes = status_codes;
4489 TAILQ_FOREACH(pe, paths, entry) {
4490 char *ondisk_status_path;
4492 if (asprintf(&ondisk_status_path, "%s%s%s",
4493 got_worktree_get_root_path(worktree),
4494 pe->path[0] == '\0' ? "" : "/", pe->path) == -1) {
4495 err = got_error_from_errno("asprintf");
4498 sda.status_path = ondisk_status_path;
4499 sda.status_path_len = strlen(ondisk_status_path);
4500 err = worktree_status(worktree, pe->path, fileindex, repo,
4501 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4502 free(ondisk_status_path);
4506 sync_err = sync_fileindex(fileindex, fileindex_path);
4507 if (sync_err && err == NULL)
4510 free(fileindex_path);
4512 got_fileindex_free(fileindex);
4513 unlockerr = lock_worktree(worktree, LOCK_SH);
4514 if (unlockerr && err == NULL)
4519 static const struct got_error *
4520 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4522 const struct got_error *err = NULL;
4524 size_t linesize = 0, n;
4527 linelen = getline(&line, &linesize, infile);
4528 if (linelen == -1) {
4529 if (ferror(infile)) {
4530 err = got_error_from_errno("getline");
4536 n = fwrite(line, 1, linelen, outfile);
4538 err = got_ferror(outfile, GOT_ERR_IO);
4543 n = fwrite(line, 1, linelen, rejectfile);
4545 err = got_ferror(rejectfile, GOT_ERR_IO);
4552 static const struct got_error *
4553 skip_one_line(FILE *f)
4556 size_t linesize = 0;
4559 linelen = getline(&line, &linesize, f);
4560 if (linelen == -1) {
4562 return got_error_from_errno("getline");
4569 static const struct got_error *
4570 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4571 int start_old, int end_old, int start_new, int end_new,
4572 FILE *outfile, FILE *rejectfile)
4574 const struct got_error *err;
4576 /* Copy old file's lines leading up to patch. */
4577 while (!feof(f1) && *line_cur1 < start_old) {
4578 err = copy_one_line(f1, outfile, NULL);
4583 /* Skip new file's lines leading up to patch. */
4584 while (!feof(f2) && *line_cur2 < start_new) {
4586 err = copy_one_line(f2, NULL, rejectfile);
4588 err = skip_one_line(f2);
4593 /* Copy patched lines. */
4594 while (!feof(f2) && *line_cur2 <= end_new) {
4595 err = copy_one_line(f2, outfile, NULL);
4600 /* Skip over old file's replaced lines. */
4601 while (!feof(f1) && *line_cur1 <= end_old) {
4603 err = copy_one_line(f1, NULL, rejectfile);
4605 err = skip_one_line(f1);
4614 static const struct got_error *
4615 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4616 FILE *outfile, FILE *rejectfile)
4618 const struct got_error *err;
4621 /* Copy old file's lines until EOF. */
4623 err = copy_one_line(f1, outfile, NULL);
4630 /* Copy new file's lines until EOF. */
4632 err = copy_one_line(f2, NULL, rejectfile);
4642 static const struct got_error *
4643 apply_or_reject_change(int *choice, int *nchunks_used,
4644 struct diff_result *diff_result, int n,
4645 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4646 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4647 got_worktree_patch_cb patch_cb, void *patch_arg)
4649 const struct got_error *err = NULL;
4650 struct diff_chunk_context cc = {};
4651 int start_old, end_old, start_new, end_new;
4653 struct diff_output_unidiff_state *diff_state;
4654 struct diff_input_info diff_info;
4657 *choice = GOT_PATCH_CHOICE_NONE;
4659 /* Get changed line numbers without context lines for copy_change(). */
4660 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4661 start_old = cc.left.start;
4662 end_old = cc.left.end;
4663 start_new = cc.right.start;
4664 end_new = cc.right.end;
4666 /* Get the same change with context lines for display. */
4667 memset(&cc, 0, sizeof(cc));
4668 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4670 memset(&diff_info, 0, sizeof(diff_info));
4671 diff_info.left_path = relpath;
4672 diff_info.right_path = relpath;
4674 diff_state = diff_output_unidiff_state_alloc();
4675 if (diff_state == NULL)
4676 return got_error_set_errno(ENOMEM,
4677 "diff_output_unidiff_state_alloc");
4679 hunkfile = got_opentemp();
4680 if (hunkfile == NULL) {
4681 err = got_error_from_errno("got_opentemp");
4685 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4687 if (rc != DIFF_RC_OK) {
4688 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4692 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4693 err = got_ferror(hunkfile, GOT_ERR_IO);
4697 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4698 hunkfile, changeno, nchanges);
4703 case GOT_PATCH_CHOICE_YES:
4704 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4705 end_old, start_new, end_new, outfile, rejectfile);
4707 case GOT_PATCH_CHOICE_NO:
4708 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4709 end_old, start_new, end_new, rejectfile, outfile);
4711 case GOT_PATCH_CHOICE_QUIT:
4714 err = got_error(GOT_ERR_PATCH_CHOICE);
4718 diff_output_unidiff_state_free(diff_state);
4719 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4720 err = got_error_from_errno("fclose");
4724 struct revert_file_args {
4725 struct got_worktree *worktree;
4726 struct got_fileindex *fileindex;
4727 got_worktree_checkout_cb progress_cb;
4729 got_worktree_patch_cb patch_cb;
4731 struct got_repository *repo;
4732 int unlink_added_files;
4733 struct got_pathlist_head *added_files_to_unlink;
4736 static const struct got_error *
4737 create_patched_content(char **path_outfile, int reverse_patch,
4738 struct got_object_id *blob_id, const char *path2,
4739 int dirfd2, const char *de_name2,
4740 const char *relpath, struct got_repository *repo,
4741 got_worktree_patch_cb patch_cb, void *patch_arg)
4743 const struct got_error *err, *free_err;
4744 struct got_blob_object *blob = NULL;
4745 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4746 int fd = -1, fd2 = -1;
4747 char link_target[PATH_MAX];
4748 ssize_t link_len = 0;
4749 char *path1 = NULL, *id_str = NULL;
4751 struct got_diffreg_result *diffreg_result = NULL;
4752 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4753 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4755 *path_outfile = NULL;
4757 err = got_object_id_str(&id_str, blob_id);
4762 fd2 = openat(dirfd2, de_name2,
4763 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4765 if (!got_err_open_nofollow_on_symlink()) {
4766 err = got_error_from_errno2("openat", path2);
4769 link_len = readlinkat(dirfd2, de_name2,
4770 link_target, sizeof(link_target));
4771 if (link_len == -1) {
4772 return got_error_from_errno2("readlinkat",
4775 sb2.st_mode = S_IFLNK;
4776 sb2.st_size = link_len;
4779 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4781 if (!got_err_open_nofollow_on_symlink()) {
4782 err = got_error_from_errno2("open", path2);
4785 link_len = readlink(path2, link_target,
4786 sizeof(link_target));
4788 return got_error_from_errno2("readlink", path2);
4789 sb2.st_mode = S_IFLNK;
4790 sb2.st_size = link_len;
4794 if (fstat(fd2, &sb2) == -1) {
4795 err = got_error_from_errno2("fstat", path2);
4799 f2 = fdopen(fd2, "r");
4801 err = got_error_from_errno2("fdopen", path2);
4807 f2 = got_opentemp();
4809 err = got_error_from_errno2("got_opentemp", path2);
4812 n = fwrite(link_target, 1, link_len, f2);
4813 if (n != link_len) {
4814 err = got_ferror(f2, GOT_ERR_IO);
4817 if (fflush(f2) == EOF) {
4818 err = got_error_from_errno("fflush");
4824 fd = got_opentempfd();
4826 err = got_error_from_errno("got_opentempfd");
4830 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4834 err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4838 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4842 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4843 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4847 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4852 if (fseek(f1, 0L, SEEK_SET) == -1)
4853 return got_ferror(f1, GOT_ERR_IO);
4854 if (fseek(f2, 0L, SEEK_SET) == -1)
4855 return got_ferror(f2, GOT_ERR_IO);
4857 /* Count the number of actual changes in the diff result. */
4858 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4859 struct diff_chunk_context cc = {};
4860 diff_chunk_context_load_change(&cc, &nchunks_used,
4861 diffreg_result->result, n, 0);
4864 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4866 err = apply_or_reject_change(&choice, &nchunks_used,
4867 diffreg_result->result, n, relpath, f1, f2,
4868 &line_cur1, &line_cur2,
4869 reverse_patch ? NULL : outfile,
4870 reverse_patch ? outfile : NULL,
4871 ++i, nchanges, patch_cb, patch_arg);
4874 if (choice == GOT_PATCH_CHOICE_YES)
4876 else if (choice == GOT_PATCH_CHOICE_QUIT)
4880 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4881 reverse_patch ? NULL : outfile,
4882 reverse_patch ? outfile : NULL);
4886 if (!S_ISLNK(sb2.st_mode)) {
4889 mode = apply_umask(sb2.st_mode);
4890 if (fchmod(fileno(outfile), mode) == -1) {
4891 err = got_error_from_errno2("fchmod", path2);
4898 if (fd != -1 && close(fd) == -1 && err == NULL)
4899 err = got_error_from_errno("close");
4901 got_object_blob_close(blob);
4902 free_err = got_diffreg_result_free(diffreg_result);
4905 if (f1 && fclose(f1) == EOF && err == NULL)
4906 err = got_error_from_errno2("fclose", path1);
4907 if (f2 && fclose(f2) == EOF && err == NULL)
4908 err = got_error_from_errno2("fclose", path2);
4909 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4910 err = got_error_from_errno2("close", path2);
4911 if (outfile && fclose(outfile) == EOF && err == NULL)
4912 err = got_error_from_errno2("fclose", *path_outfile);
4913 if (path1 && unlink(path1) == -1 && err == NULL)
4914 err = got_error_from_errno2("unlink", path1);
4915 if (err || !have_content) {
4916 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4917 err = got_error_from_errno2("unlink", *path_outfile);
4918 free(*path_outfile);
4919 *path_outfile = NULL;
4925 static const struct got_error *
4926 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4927 const char *relpath, struct got_object_id *blob_id,
4928 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4929 int dirfd, const char *de_name)
4931 struct revert_file_args *a = arg;
4932 const struct got_error *err = NULL;
4933 char *parent_path = NULL;
4934 struct got_fileindex_entry *ie;
4935 struct got_commit_object *base_commit = NULL;
4936 struct got_tree_object *tree = NULL;
4937 struct got_object_id *tree_id = NULL;
4938 const struct got_tree_entry *te = NULL;
4939 char *tree_path = NULL, *te_name;
4940 char *ondisk_path = NULL, *path_content = NULL;
4941 struct got_blob_object *blob = NULL;
4944 /* Reverting a staged deletion is a no-op. */
4945 if (status == GOT_STATUS_DELETE &&
4946 staged_status != GOT_STATUS_NO_CHANGE)
4949 if (status == GOT_STATUS_UNVERSIONED)
4950 return (*a->progress_cb)(a->progress_arg,
4951 GOT_STATUS_UNVERSIONED, relpath);
4953 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4955 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4957 /* Construct in-repository path of tree which contains this blob. */
4958 err = got_path_dirname(&parent_path, ie->path);
4960 if (err->code != GOT_ERR_BAD_PATH)
4962 parent_path = strdup("/");
4963 if (parent_path == NULL) {
4964 err = got_error_from_errno("strdup");
4968 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4969 tree_path = strdup(parent_path);
4970 if (tree_path == NULL) {
4971 err = got_error_from_errno("strdup");
4975 if (got_path_is_root_dir(parent_path)) {
4976 tree_path = strdup(a->worktree->path_prefix);
4977 if (tree_path == NULL) {
4978 err = got_error_from_errno("strdup");
4982 if (asprintf(&tree_path, "%s/%s",
4983 a->worktree->path_prefix, parent_path) == -1) {
4984 err = got_error_from_errno("asprintf");
4990 err = got_object_open_as_commit(&base_commit, a->repo,
4991 a->worktree->base_commit_id);
4995 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4997 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4998 (status == GOT_STATUS_ADD ||
4999 staged_status == GOT_STATUS_ADD)))
5002 err = got_object_open_as_tree(&tree, a->repo, tree_id);
5006 err = got_path_basename(&te_name, ie->path);
5010 te = got_object_tree_find_entry(tree, te_name);
5012 if (te == NULL && status != GOT_STATUS_ADD &&
5013 staged_status != GOT_STATUS_ADD) {
5014 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
5020 case GOT_STATUS_ADD:
5022 int choice = GOT_PATCH_CHOICE_NONE;
5023 err = (*a->patch_cb)(&choice, a->patch_arg,
5024 status, ie->path, NULL, 1, 1);
5027 if (choice != GOT_PATCH_CHOICE_YES)
5030 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
5034 got_fileindex_entry_remove(a->fileindex, ie);
5035 if (a->unlink_added_files) {
5036 int do_unlink = a->added_files_to_unlink ? 0 : 1;
5038 if (a->added_files_to_unlink) {
5039 struct got_pathlist_entry *pe;
5041 TAILQ_FOREACH(pe, a->added_files_to_unlink,
5043 if (got_path_cmp(pe->path, relpath,
5044 pe->path_len, strlen(relpath)))
5052 if (asprintf(&ondisk_path, "%s/%s",
5053 got_worktree_get_root_path(a->worktree),
5055 err = got_error_from_errno("asprintf");
5058 if (unlink(ondisk_path) == -1) {
5059 err = got_error_from_errno2("unlink",
5066 case GOT_STATUS_DELETE:
5068 int choice = GOT_PATCH_CHOICE_NONE;
5069 err = (*a->patch_cb)(&choice, a->patch_arg,
5070 status, ie->path, NULL, 1, 1);
5073 if (choice != GOT_PATCH_CHOICE_YES)
5077 case GOT_STATUS_MODIFY:
5078 case GOT_STATUS_MODE_CHANGE:
5079 case GOT_STATUS_CONFLICT:
5080 case GOT_STATUS_MISSING: {
5081 struct got_object_id id;
5082 if (staged_status == GOT_STATUS_ADD ||
5083 staged_status == GOT_STATUS_MODIFY)
5084 got_fileindex_entry_get_staged_blob_id(&id, ie);
5086 got_fileindex_entry_get_blob_id(&id, ie);
5087 fd = got_opentempfd();
5089 err = got_error_from_errno("got_opentempfd");
5093 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
5097 if (asprintf(&ondisk_path, "%s/%s",
5098 got_worktree_get_root_path(a->worktree), relpath) == -1) {
5099 err = got_error_from_errno("asprintf");
5103 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
5104 status == GOT_STATUS_CONFLICT)) {
5105 int is_bad_symlink = 0;
5106 err = create_patched_content(&path_content, 1, &id,
5107 ondisk_path, dirfd, de_name, ie->path, a->repo,
5108 a->patch_cb, a->patch_arg);
5109 if (err || path_content == NULL)
5111 if (te && S_ISLNK(te->mode)) {
5112 if (unlink(path_content) == -1) {
5113 err = got_error_from_errno2("unlink",
5117 err = install_symlink(&is_bad_symlink,
5118 a->worktree, ondisk_path, ie->path,
5119 blob, 0, 1, 0, 0, a->repo,
5120 a->progress_cb, a->progress_arg);
5122 if (rename(path_content, ondisk_path) == -1) {
5123 err = got_error_from_errno3("rename",
5124 path_content, ondisk_path);
5129 int is_bad_symlink = 0;
5130 if (te && S_ISLNK(te->mode)) {
5131 err = install_symlink(&is_bad_symlink,
5132 a->worktree, ondisk_path, ie->path,
5133 blob, 0, 1, 0, 0, a->repo,
5134 a->progress_cb, a->progress_arg);
5136 err = install_blob(a->worktree, ondisk_path,
5138 te ? te->mode : GOT_DEFAULT_FILE_MODE,
5139 got_fileindex_perms_to_st(ie), blob,
5140 0, 1, 0, 0, a->repo,
5141 a->progress_cb, a->progress_arg);
5145 if (status == GOT_STATUS_DELETE ||
5146 status == GOT_STATUS_MODE_CHANGE) {
5147 err = got_fileindex_entry_update(ie,
5148 a->worktree->root_fd, relpath,
5150 a->worktree->base_commit_id->sha1, 1);
5154 if (is_bad_symlink) {
5155 got_fileindex_entry_filetype_set(ie,
5156 GOT_FILEIDX_MODE_BAD_SYMLINK);
5169 if (fd != -1 && close(fd) == -1 && err == NULL)
5170 err = got_error_from_errno("close");
5172 got_object_blob_close(blob);
5174 got_object_tree_close(tree);
5177 got_object_commit_close(base_commit);
5181 const struct got_error *
5182 got_worktree_revert(struct got_worktree *worktree,
5183 struct got_pathlist_head *paths,
5184 got_worktree_checkout_cb progress_cb, void *progress_arg,
5185 got_worktree_patch_cb patch_cb, void *patch_arg,
5186 struct got_repository *repo)
5188 struct got_fileindex *fileindex = NULL;
5189 char *fileindex_path = NULL;
5190 const struct got_error *err = NULL, *unlockerr = NULL;
5191 const struct got_error *sync_err = NULL;
5192 struct got_pathlist_entry *pe;
5193 struct revert_file_args rfa;
5195 err = lock_worktree(worktree, LOCK_EX);
5199 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5203 rfa.worktree = worktree;
5204 rfa.fileindex = fileindex;
5205 rfa.progress_cb = progress_cb;
5206 rfa.progress_arg = progress_arg;
5207 rfa.patch_cb = patch_cb;
5208 rfa.patch_arg = patch_arg;
5210 rfa.unlink_added_files = 0;
5211 TAILQ_FOREACH(pe, paths, entry) {
5212 err = worktree_status(worktree, pe->path, fileindex, repo,
5213 revert_file, &rfa, NULL, NULL, 1, 0);
5217 sync_err = sync_fileindex(fileindex, fileindex_path);
5218 if (sync_err && err == NULL)
5221 free(fileindex_path);
5223 got_fileindex_free(fileindex);
5224 unlockerr = lock_worktree(worktree, LOCK_SH);
5225 if (unlockerr && err == NULL)
5231 free_commitable(struct got_commitable *ct)
5234 free(ct->in_repo_path);
5235 free(ct->ondisk_path);
5237 free(ct->base_blob_id);
5238 free(ct->staged_blob_id);
5239 free(ct->base_commit_id);
5243 struct collect_commitables_arg {
5244 struct got_pathlist_head *commitable_paths;
5245 struct got_repository *repo;
5246 struct got_worktree *worktree;
5247 struct got_fileindex *fileindex;
5248 int have_staged_files;
5249 int allow_bad_symlinks;
5250 int diff_header_shown;
5251 int commit_conflicts;
5258 * Create a file which contains the target path of a symlink so we can feed
5259 * it as content to the diff engine.
5261 static const struct got_error *
5262 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5263 const char *abspath)
5265 const struct got_error *err = NULL;
5266 char target_path[PATH_MAX];
5267 ssize_t target_len, outlen;
5272 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5273 if (target_len == -1)
5274 return got_error_from_errno2("readlinkat", abspath);
5276 target_len = readlink(abspath, target_path, PATH_MAX);
5277 if (target_len == -1)
5278 return got_error_from_errno2("readlink", abspath);
5281 *fd = got_opentempfd();
5283 return got_error_from_errno("got_opentempfd");
5285 outlen = write(*fd, target_path, target_len);
5287 err = got_error_from_errno("got_opentempfd");
5291 if (lseek(*fd, 0, SEEK_SET) == -1) {
5292 err = got_error_from_errno2("lseek", abspath);
5303 static const struct got_error *
5304 append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5305 FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5306 int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5308 const struct got_error *err = NULL;
5309 struct got_blob_object *blob1 = NULL;
5310 int fd = -1, fd1 = -1, fd2 = -1;
5311 FILE *ondisk_file = NULL;
5312 char *label1 = NULL;
5316 char *id_str = NULL;
5318 memset(&sb, 0, sizeof(sb));
5321 if (ct->staged_status != GOT_STATUS_MODIFY &&
5322 ct->staged_status != GOT_STATUS_ADD &&
5323 ct->staged_status != GOT_STATUS_DELETE)
5326 if (ct->status != GOT_STATUS_MODIFY &&
5327 ct->status != GOT_STATUS_ADD &&
5328 ct->status != GOT_STATUS_DELETE &&
5329 ct->status != GOT_STATUS_CONFLICT)
5333 err = got_opentemp_truncate(f1);
5335 return got_error_from_errno("got_opentemp_truncate");
5336 err = got_opentemp_truncate(f2);
5338 return got_error_from_errno("got_opentemp_truncate");
5340 if (!*diff_header_shown) {
5341 err = got_object_id_str(&id_str, worktree->base_commit_id);
5344 fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5345 got_worktree_get_root_path(worktree));
5346 fprintf(diff_outfile, "commit - %s\n", id_str);
5347 fprintf(diff_outfile, "path + %s%s\n",
5348 got_worktree_get_root_path(worktree),
5349 diff_staged ? " (staged changes)" : "");
5350 *diff_header_shown = 1;
5354 const char *label1 = NULL, *label2 = NULL;
5355 switch (ct->staged_status) {
5356 case GOT_STATUS_MODIFY:
5360 case GOT_STATUS_ADD:
5363 case GOT_STATUS_DELETE:
5367 return got_error(GOT_ERR_FILE_STATUS);
5369 fd1 = got_opentempfd();
5371 err = got_error_from_errno("got_opentempfd");
5374 fd2 = got_opentempfd();
5376 err = got_error_from_errno("got_opentempfd");
5379 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5380 fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5381 label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5382 NULL, repo, diff_outfile);
5386 fd1 = got_opentempfd();
5388 err = got_error_from_errno("got_opentempfd");
5392 if (ct->status != GOT_STATUS_ADD) {
5393 err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5399 if (ct->status != GOT_STATUS_DELETE) {
5401 fd = openat(dirfd, de_name,
5402 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5404 if (!got_err_open_nofollow_on_symlink()) {
5405 err = got_error_from_errno2("openat",
5409 err = get_symlink_target_file(&fd, dirfd,
5410 de_name, ct->ondisk_path);
5415 fd = open(ct->ondisk_path,
5416 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5418 if (!got_err_open_nofollow_on_symlink()) {
5419 err = got_error_from_errno2("open",
5423 err = get_symlink_target_file(&fd, dirfd,
5424 de_name, ct->ondisk_path);
5429 if (fstatat(fd, ct->ondisk_path, &sb,
5430 AT_SYMLINK_NOFOLLOW) == -1) {
5431 err = got_error_from_errno2("fstatat", ct->ondisk_path);
5434 ondisk_file = fdopen(fd, "r");
5435 if (ondisk_file == NULL) {
5436 err = got_error_from_errno2("fdopen", ct->ondisk_path);
5444 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5450 err = got_diff_blob_file(blob1, f1, size1, label1,
5451 ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5452 GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5454 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5455 err = got_error_from_errno("close");
5456 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5457 err = got_error_from_errno("close");
5459 got_object_blob_close(blob1);
5460 if (fd != -1 && close(fd) == -1 && err == NULL)
5461 err = got_error_from_errno("close");
5462 if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5463 err = got_error_from_errno("fclose");
5467 static const struct got_error *
5468 collect_commitables(void *arg, unsigned char status,
5469 unsigned char staged_status, const char *relpath,
5470 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5471 struct got_object_id *commit_id, int dirfd, const char *de_name)
5473 struct collect_commitables_arg *a = arg;
5474 const struct got_error *err = NULL;
5475 struct got_commitable *ct = NULL;
5476 struct got_pathlist_entry *new = NULL;
5477 char *parent_path = NULL, *path = NULL;
5480 if (a->have_staged_files) {
5481 if (staged_status != GOT_STATUS_MODIFY &&
5482 staged_status != GOT_STATUS_ADD &&
5483 staged_status != GOT_STATUS_DELETE)
5486 if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5487 printf("C %s\n", relpath);
5488 return got_error(GOT_ERR_COMMIT_CONFLICT);
5491 if (status != GOT_STATUS_MODIFY &&
5492 status != GOT_STATUS_MODE_CHANGE &&
5493 status != GOT_STATUS_ADD &&
5494 status != GOT_STATUS_DELETE &&
5495 status != GOT_STATUS_CONFLICT)
5499 if (asprintf(&path, "/%s", relpath) == -1) {
5500 err = got_error_from_errno("asprintf");
5503 if (strcmp(path, "/") == 0) {
5504 parent_path = strdup("");
5505 if (parent_path == NULL)
5506 return got_error_from_errno("strdup");
5508 err = got_path_dirname(&parent_path, path);
5513 ct = calloc(1, sizeof(*ct));
5515 err = got_error_from_errno("calloc");
5519 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5521 err = got_error_from_errno("asprintf");
5525 if (staged_status == GOT_STATUS_ADD ||
5526 staged_status == GOT_STATUS_MODIFY) {
5527 struct got_fileindex_entry *ie;
5528 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5529 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5530 case GOT_FILEIDX_MODE_REGULAR_FILE:
5531 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5534 case GOT_FILEIDX_MODE_SYMLINK:
5538 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5541 ct->mode |= got_fileindex_entry_perms_get(ie);
5542 } else if (status != GOT_STATUS_DELETE &&
5543 staged_status != GOT_STATUS_DELETE) {
5545 if (fstatat(dirfd, de_name, &sb,
5546 AT_SYMLINK_NOFOLLOW) == -1) {
5547 err = got_error_from_errno2("fstatat",
5551 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5552 err = got_error_from_errno2("lstat", ct->ondisk_path);
5555 ct->mode = sb.st_mode;
5558 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5559 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5561 err = got_error_from_errno("asprintf");
5565 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5566 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5568 char target_path[PATH_MAX];
5570 target_len = readlink(ct->ondisk_path, target_path,
5571 sizeof(target_path));
5572 if (target_len == -1) {
5573 err = got_error_from_errno2("readlink",
5577 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5578 target_len, ct->ondisk_path, a->worktree->root_path);
5581 if (is_bad_symlink) {
5582 err = got_error_path(ct->ondisk_path,
5583 GOT_ERR_BAD_SYMLINK);
5589 ct->status = status;
5590 ct->staged_status = staged_status;
5591 ct->blob_id = NULL; /* will be filled in when blob gets created */
5592 if (ct->status != GOT_STATUS_ADD &&
5593 ct->staged_status != GOT_STATUS_ADD) {
5594 ct->base_blob_id = got_object_id_dup(blob_id);
5595 if (ct->base_blob_id == NULL) {
5596 err = got_error_from_errno("got_object_id_dup");
5599 ct->base_commit_id = got_object_id_dup(commit_id);
5600 if (ct->base_commit_id == NULL) {
5601 err = got_error_from_errno("got_object_id_dup");
5605 if (ct->staged_status == GOT_STATUS_ADD ||
5606 ct->staged_status == GOT_STATUS_MODIFY) {
5607 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5608 if (ct->staged_blob_id == NULL) {
5609 err = got_error_from_errno("got_object_id_dup");
5613 ct->path = strdup(path);
5614 if (ct->path == NULL) {
5615 err = got_error_from_errno("strdup");
5618 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5622 if (a->diff_outfile && ct && new != NULL) {
5623 err = append_ct_diff(ct, &a->diff_header_shown,
5624 a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5625 a->have_staged_files, a->repo, a->worktree);
5630 if (ct && (err || new == NULL))
5631 free_commitable(ct);
5637 static const struct got_error *write_tree(struct got_object_id **, int *,
5638 struct got_tree_object *, const char *, struct got_pathlist_head *,
5639 got_worktree_status_cb status_cb, void *status_arg,
5640 struct got_repository *);
5642 static const struct got_error *
5643 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5644 struct got_tree_entry *te, const char *parent_path,
5645 struct got_pathlist_head *commitable_paths,
5646 got_worktree_status_cb status_cb, void *status_arg,
5647 struct got_repository *repo)
5649 const struct got_error *err = NULL;
5650 struct got_tree_object *subtree;
5653 if (asprintf(&subpath, "%s%s%s", parent_path,
5654 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5655 return got_error_from_errno("asprintf");
5657 err = got_object_open_as_tree(&subtree, repo, &te->id);
5661 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5662 commitable_paths, status_cb, status_arg, repo);
5663 got_object_tree_close(subtree);
5668 static const struct got_error *
5669 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5671 const struct got_error *err = NULL;
5672 char *ct_parent_path = NULL;
5676 if (strchr(ct->in_repo_path, '/') == NULL) {
5677 *match = got_path_is_root_dir(path);
5681 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5684 *match = (strcmp(path, ct_parent_path) == 0);
5685 free(ct_parent_path);
5690 get_ct_file_mode(struct got_commitable *ct)
5692 if (S_ISLNK(ct->mode))
5695 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5698 static const struct got_error *
5699 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5700 struct got_tree_entry *te, struct got_commitable *ct)
5702 const struct got_error *err = NULL;
5706 err = got_object_tree_entry_dup(new_te, te);
5710 (*new_te)->mode = get_ct_file_mode(ct);
5712 if (ct->staged_status == GOT_STATUS_MODIFY)
5713 memcpy(&(*new_te)->id, ct->staged_blob_id,
5714 sizeof((*new_te)->id));
5716 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5718 if (err && *new_te) {
5725 static const struct got_error *
5726 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5727 struct got_commitable *ct)
5729 const struct got_error *err = NULL;
5730 char *ct_name = NULL;
5734 *new_te = calloc(1, sizeof(**new_te));
5735 if (*new_te == NULL)
5736 return got_error_from_errno("calloc");
5738 err = got_path_basename(&ct_name, ct->path);
5741 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5742 sizeof((*new_te)->name)) {
5743 err = got_error(GOT_ERR_NO_SPACE);
5747 (*new_te)->mode = get_ct_file_mode(ct);
5749 if (ct->staged_status == GOT_STATUS_ADD)
5750 memcpy(&(*new_te)->id, ct->staged_blob_id,
5751 sizeof((*new_te)->id));
5753 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5756 if (err && *new_te) {
5763 static const struct got_error *
5764 insert_tree_entry(struct got_tree_entry *new_te,
5765 struct got_pathlist_head *paths)
5767 const struct got_error *err = NULL;
5768 struct got_pathlist_entry *new_pe;
5770 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5774 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5778 static const struct got_error *
5779 report_ct_status(struct got_commitable *ct,
5780 got_worktree_status_cb status_cb, void *status_arg)
5782 const char *ct_path = ct->path;
5783 unsigned char status;
5785 if (status_cb == NULL) /* no commit progress output desired */
5788 while (ct_path[0] == '/')
5791 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5792 status = ct->staged_status;
5794 status = ct->status;
5796 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5797 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5800 static const struct got_error *
5801 match_modified_subtree(int *modified, struct got_tree_entry *te,
5802 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5804 const struct got_error *err = NULL;
5805 struct got_pathlist_entry *pe;
5810 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5811 got_path_is_root_dir(base_tree_path) ? "" : "/",
5813 return got_error_from_errno("asprintf");
5815 TAILQ_FOREACH(pe, commitable_paths, entry) {
5816 struct got_commitable *ct = pe->data;
5817 *modified = got_path_is_child(ct->in_repo_path, te_path,
5827 static const struct got_error *
5828 match_deleted_or_modified_ct(struct got_commitable **ctp,
5829 struct got_tree_entry *te, const char *base_tree_path,
5830 struct got_pathlist_head *commitable_paths)
5832 const struct got_error *err = NULL;
5833 struct got_pathlist_entry *pe;
5837 TAILQ_FOREACH(pe, commitable_paths, entry) {
5838 struct got_commitable *ct = pe->data;
5839 char *ct_name = NULL;
5842 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5843 if (ct->status != GOT_STATUS_MODIFY &&
5844 ct->status != GOT_STATUS_MODE_CHANGE &&
5845 ct->status != GOT_STATUS_DELETE &&
5846 ct->status != GOT_STATUS_CONFLICT)
5849 if (ct->staged_status != GOT_STATUS_MODIFY &&
5850 ct->staged_status != GOT_STATUS_DELETE)
5854 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5857 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5863 err = got_path_basename(&ct_name, pe->path);
5867 if (strcmp(te->name, ct_name) != 0) {
5880 static const struct got_error *
5881 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5882 const char *child_path, const char *path_base_tree,
5883 struct got_pathlist_head *commitable_paths,
5884 got_worktree_status_cb status_cb, void *status_arg,
5885 struct got_repository *repo)
5887 const struct got_error *err = NULL;
5888 struct got_tree_entry *new_te;
5890 struct got_object_id *id = NULL;
5895 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5896 got_path_is_root_dir(path_base_tree) ? "" : "/",
5898 return got_error_from_errno("asprintf");
5900 new_te = calloc(1, sizeof(*new_te));
5902 return got_error_from_errno("calloc");
5903 new_te->mode = S_IFDIR;
5905 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5906 sizeof(new_te->name)) {
5907 err = got_error(GOT_ERR_NO_SPACE);
5910 err = write_tree(&id, &nentries, NULL, subtree_path,
5911 commitable_paths, status_cb, status_arg, repo);
5916 memcpy(&new_te->id, id, sizeof(new_te->id));
5925 static const struct got_error *
5926 write_tree(struct got_object_id **new_tree_id, int *nentries,
5927 struct got_tree_object *base_tree, const char *path_base_tree,
5928 struct got_pathlist_head *commitable_paths,
5929 got_worktree_status_cb status_cb, void *status_arg,
5930 struct got_repository *repo)
5932 const struct got_error *err = NULL;
5933 struct got_pathlist_head paths;
5934 struct got_tree_entry *te, *new_te = NULL;
5935 struct got_pathlist_entry *pe;
5940 /* Insert, and recurse into, newly added entries first. */
5941 TAILQ_FOREACH(pe, commitable_paths, entry) {
5942 struct got_commitable *ct = pe->data;
5943 char *child_path = NULL, *slash;
5945 if ((ct->status != GOT_STATUS_ADD &&
5946 ct->staged_status != GOT_STATUS_ADD) ||
5947 (ct->flags & GOT_COMMITABLE_ADDED))
5950 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5951 strlen(path_base_tree)))
5954 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5959 slash = strchr(child_path, '/');
5960 if (slash == NULL) {
5961 err = alloc_added_blob_tree_entry(&new_te, ct);
5964 err = report_ct_status(ct, status_cb, status_arg);
5967 ct->flags |= GOT_COMMITABLE_ADDED;
5968 err = insert_tree_entry(new_te, &paths);
5973 *slash = '\0'; /* trim trailing path components */
5974 if (base_tree == NULL ||
5975 got_object_tree_find_entry(base_tree, child_path)
5977 err = make_subtree_for_added_blob(&new_te,
5978 child_path, path_base_tree,
5979 commitable_paths, status_cb, status_arg,
5983 err = insert_tree_entry(new_te, &paths);
5992 int i, nbase_entries;
5993 /* Handle modified and deleted entries. */
5994 nbase_entries = got_object_tree_get_nentries(base_tree);
5995 for (i = 0; i < nbase_entries; i++) {
5996 struct got_commitable *ct = NULL;
5998 te = got_object_tree_get_entry(base_tree, i);
5999 if (got_object_tree_entry_is_submodule(te)) {
6000 /* Entry is a submodule; just copy it. */
6001 err = got_object_tree_entry_dup(&new_te, te);
6004 err = insert_tree_entry(new_te, &paths);
6011 if (S_ISDIR(te->mode)) {
6013 err = got_object_tree_entry_dup(&new_te, te);
6016 err = match_modified_subtree(&modified, te,
6017 path_base_tree, commitable_paths);
6020 /* Avoid recursion into unmodified subtrees. */
6022 struct got_object_id *new_id;
6024 err = write_subtree(&new_id,
6026 path_base_tree, commitable_paths,
6027 status_cb, status_arg, repo);
6030 if (nsubentries == 0) {
6031 /* All entries were deleted. */
6035 memcpy(&new_te->id, new_id,
6036 sizeof(new_te->id));
6039 err = insert_tree_entry(new_te, &paths);
6046 err = match_deleted_or_modified_ct(&ct, te,
6047 path_base_tree, commitable_paths);
6051 /* NB: Deleted entries get dropped here. */
6052 if (ct->status == GOT_STATUS_MODIFY ||
6053 ct->status == GOT_STATUS_MODE_CHANGE ||
6054 ct->status == GOT_STATUS_CONFLICT ||
6055 ct->staged_status == GOT_STATUS_MODIFY) {
6056 err = alloc_modified_blob_tree_entry(
6060 err = insert_tree_entry(new_te, &paths);
6065 err = report_ct_status(ct, status_cb,
6070 /* Entry is unchanged; just copy it. */
6071 err = got_object_tree_entry_dup(&new_te, te);
6074 err = insert_tree_entry(new_te, &paths);
6082 /* Write new list of entries; deleted entries have been dropped. */
6083 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
6085 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
6089 static const struct got_error *
6090 update_fileindex_after_commit(struct got_worktree *worktree,
6091 struct got_pathlist_head *commitable_paths,
6092 struct got_object_id *new_base_commit_id,
6093 struct got_fileindex *fileindex, int have_staged_files)
6095 const struct got_error *err = NULL;
6096 struct got_pathlist_entry *pe;
6097 char *relpath = NULL;
6099 TAILQ_FOREACH(pe, commitable_paths, entry) {
6100 struct got_fileindex_entry *ie;
6101 struct got_commitable *ct = pe->data;
6103 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6105 err = got_path_skip_common_ancestor(&relpath,
6106 worktree->root_path, ct->ondisk_path);
6111 if (ct->status == GOT_STATUS_DELETE ||
6112 ct->staged_status == GOT_STATUS_DELETE) {
6113 got_fileindex_entry_remove(fileindex, ie);
6114 } else if (ct->staged_status == GOT_STATUS_ADD ||
6115 ct->staged_status == GOT_STATUS_MODIFY) {
6116 got_fileindex_entry_stage_set(ie,
6117 GOT_FILEIDX_STAGE_NONE);
6118 got_fileindex_entry_staged_filetype_set(ie, 0);
6120 err = got_fileindex_entry_update(ie,
6121 worktree->root_fd, relpath,
6122 ct->staged_blob_id->sha1,
6123 new_base_commit_id->sha1,
6124 !have_staged_files);
6126 err = got_fileindex_entry_update(ie,
6127 worktree->root_fd, relpath,
6129 new_base_commit_id->sha1,
6130 !have_staged_files);
6132 err = got_fileindex_entry_alloc(&ie, pe->path);
6135 err = got_fileindex_entry_update(ie,
6136 worktree->root_fd, relpath, ct->blob_id->sha1,
6137 new_base_commit_id->sha1, 1);
6139 got_fileindex_entry_free(ie);
6142 err = got_fileindex_entry_add(fileindex, ie);
6144 got_fileindex_entry_free(ie);
6157 static const struct got_error *
6158 check_out_of_date(const char *in_repo_path, unsigned char status,
6159 unsigned char staged_status, struct got_object_id *base_blob_id,
6160 struct got_object_id *base_commit_id,
6161 struct got_object_id *head_commit_id, struct got_repository *repo,
6164 const struct got_error *err = NULL;
6165 struct got_commit_object *commit = NULL;
6166 struct got_object_id *id = NULL;
6168 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
6169 /* Trivial case: base commit == head commit */
6170 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
6173 * Ensure file content which local changes were based
6174 * on matches file content in the branch head.
6176 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6179 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6181 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6182 err = got_error(ood_errcode);
6184 } else if (got_object_id_cmp(id, base_blob_id) != 0)
6185 err = got_error(ood_errcode);
6187 /* Require that added files don't exist in the branch head. */
6188 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6191 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6192 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
6194 err = id ? got_error(ood_errcode) : NULL;
6199 got_object_commit_close(commit);
6203 static const struct got_error *
6204 commit_worktree(struct got_object_id **new_commit_id,
6205 struct got_pathlist_head *commitable_paths,
6206 struct got_object_id *head_commit_id,
6207 struct got_object_id *parent_id2,
6208 struct got_worktree *worktree,
6209 const char *author, const char *committer, char *diff_path,
6210 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6211 got_worktree_status_cb status_cb, void *status_arg,
6212 struct got_repository *repo)
6214 const struct got_error *err = NULL, *unlockerr = NULL;
6215 struct got_pathlist_entry *pe;
6216 const char *head_ref_name = NULL;
6217 struct got_commit_object *head_commit = NULL;
6218 struct got_reference *head_ref2 = NULL;
6219 struct got_object_id *head_commit_id2 = NULL;
6220 struct got_tree_object *head_tree = NULL;
6221 struct got_object_id *new_tree_id = NULL;
6222 int nentries, nparents = 0;
6223 struct got_object_id_queue parent_ids;
6224 struct got_object_qid *pid = NULL;
6225 char *logmsg = NULL;
6228 *new_commit_id = NULL;
6230 STAILQ_INIT(&parent_ids);
6232 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6236 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6240 if (commit_msg_cb != NULL) {
6241 err = commit_msg_cb(commitable_paths, diff_path,
6242 &logmsg, commit_arg);
6247 if (logmsg == NULL || strlen(logmsg) == 0) {
6248 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6252 /* Create blobs from added and modified files and record their IDs. */
6253 TAILQ_FOREACH(pe, commitable_paths, entry) {
6254 struct got_commitable *ct = pe->data;
6257 /* Blobs for staged files already exist. */
6258 if (ct->staged_status == GOT_STATUS_ADD ||
6259 ct->staged_status == GOT_STATUS_MODIFY)
6262 if (ct->status != GOT_STATUS_ADD &&
6263 ct->status != GOT_STATUS_MODIFY &&
6264 ct->status != GOT_STATUS_MODE_CHANGE &&
6265 ct->status != GOT_STATUS_CONFLICT)
6268 if (asprintf(&ondisk_path, "%s/%s",
6269 worktree->root_path, pe->path) == -1) {
6270 err = got_error_from_errno("asprintf");
6273 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6279 /* Recursively write new tree objects. */
6280 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6281 commitable_paths, status_cb, status_arg, repo);
6285 err = got_object_qid_alloc(&pid, head_commit_id);
6288 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6291 err = got_object_qid_alloc(&pid, parent_id2);
6294 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6297 timestamp = time(NULL);
6298 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6299 nparents, author, timestamp, committer, timestamp, logmsg, repo);
6305 /* Check if a concurrent commit to our branch has occurred. */
6306 head_ref_name = got_worktree_get_head_ref_name(worktree);
6307 if (head_ref_name == NULL) {
6308 err = got_error_from_errno("got_worktree_get_head_ref_name");
6311 /* Lock the reference here to prevent concurrent modification. */
6312 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6315 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6318 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6319 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6322 /* Update branch head in repository. */
6323 err = got_ref_change_ref(head_ref2, *new_commit_id);
6326 err = got_ref_write(head_ref2, repo);
6330 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6334 err = ref_base_commit(worktree, repo);
6338 got_object_id_queue_free(&parent_ids);
6340 got_object_tree_close(head_tree);
6342 got_object_commit_close(head_commit);
6343 free(head_commit_id2);
6345 unlockerr = got_ref_unlock(head_ref2);
6346 if (unlockerr && err == NULL)
6348 got_ref_close(head_ref2);
6353 static const struct got_error *
6354 check_path_is_commitable(const char *path,
6355 struct got_pathlist_head *commitable_paths)
6357 struct got_pathlist_entry *cpe = NULL;
6358 size_t path_len = strlen(path);
6360 TAILQ_FOREACH(cpe, commitable_paths, entry) {
6361 struct got_commitable *ct = cpe->data;
6362 const char *ct_path = ct->path;
6364 while (ct_path[0] == '/')
6367 if (strcmp(path, ct_path) == 0 ||
6368 got_path_is_child(ct_path, path, path_len))
6373 return got_error_path(path, GOT_ERR_BAD_PATH);
6378 static const struct got_error *
6379 check_staged_file(void *arg, struct got_fileindex_entry *ie)
6381 int *have_staged_files = arg;
6383 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6384 *have_staged_files = 1;
6385 return got_error(GOT_ERR_CANCELLED);
6391 static const struct got_error *
6392 check_non_staged_files(struct got_fileindex *fileindex,
6393 struct got_pathlist_head *paths)
6395 struct got_pathlist_entry *pe;
6396 struct got_fileindex_entry *ie;
6398 TAILQ_FOREACH(pe, paths, entry) {
6399 if (pe->path[0] == '\0')
6401 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6403 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6404 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6405 return got_error_path(pe->path,
6406 GOT_ERR_FILE_NOT_STAGED);
6412 const struct got_error *
6413 got_worktree_commit(struct got_object_id **new_commit_id,
6414 struct got_worktree *worktree, struct got_pathlist_head *paths,
6415 const char *author, const char *committer, int allow_bad_symlinks,
6416 int show_diff, int commit_conflicts,
6417 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6418 got_worktree_status_cb status_cb, void *status_arg,
6419 struct got_repository *repo)
6421 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6422 struct got_fileindex *fileindex = NULL;
6423 char *fileindex_path = NULL;
6424 struct got_pathlist_head commitable_paths;
6425 struct collect_commitables_arg cc_arg;
6426 struct got_pathlist_entry *pe;
6427 struct got_reference *head_ref = NULL;
6428 struct got_object_id *head_commit_id = NULL;
6429 char *diff_path = NULL;
6430 int have_staged_files = 0;
6432 *new_commit_id = NULL;
6434 memset(&cc_arg, 0, sizeof(cc_arg));
6435 TAILQ_INIT(&commitable_paths);
6437 err = lock_worktree(worktree, LOCK_EX);
6441 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6445 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6449 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6453 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6454 &have_staged_files);
6455 if (err && err->code != GOT_ERR_CANCELLED)
6457 if (have_staged_files) {
6458 err = check_non_staged_files(fileindex, paths);
6463 cc_arg.commitable_paths = &commitable_paths;
6464 cc_arg.worktree = worktree;
6465 cc_arg.fileindex = fileindex;
6467 cc_arg.have_staged_files = have_staged_files;
6468 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6469 cc_arg.diff_header_shown = 0;
6470 cc_arg.commit_conflicts = commit_conflicts;
6472 err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6473 GOT_TMPDIR_STR "/got", ".diff");
6476 cc_arg.f1 = got_opentemp();
6477 if (cc_arg.f1 == NULL) {
6478 err = got_error_from_errno("got_opentemp");
6481 cc_arg.f2 = got_opentemp();
6482 if (cc_arg.f2 == NULL) {
6483 err = got_error_from_errno("got_opentemp");
6488 TAILQ_FOREACH(pe, paths, entry) {
6489 err = worktree_status(worktree, pe->path, fileindex, repo,
6490 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6496 if (fflush(cc_arg.diff_outfile) == EOF) {
6497 err = got_error_from_errno("fflush");
6502 if (TAILQ_EMPTY(&commitable_paths)) {
6503 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6507 TAILQ_FOREACH(pe, paths, entry) {
6508 err = check_path_is_commitable(pe->path, &commitable_paths);
6513 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6514 struct got_commitable *ct = pe->data;
6515 const char *ct_path = ct->in_repo_path;
6517 while (ct_path[0] == '/')
6519 err = check_out_of_date(ct_path, ct->status,
6520 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6521 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6527 err = commit_worktree(new_commit_id, &commitable_paths,
6528 head_commit_id, NULL, worktree, author, committer,
6529 (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6530 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6534 err = update_fileindex_after_commit(worktree, &commitable_paths,
6535 *new_commit_id, fileindex, have_staged_files);
6536 sync_err = sync_fileindex(fileindex, fileindex_path);
6537 if (sync_err && err == NULL)
6541 got_fileindex_free(fileindex);
6542 free(fileindex_path);
6543 unlockerr = lock_worktree(worktree, LOCK_SH);
6544 if (unlockerr && err == NULL)
6546 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6547 struct got_commitable *ct = pe->data;
6549 free_commitable(ct);
6551 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6552 if (diff_path && unlink(diff_path) == -1 && err == NULL)
6553 err = got_error_from_errno2("unlink", diff_path);
6555 if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6557 err = got_error_from_errno("fclose");
6562 got_commitable_get_path(struct got_commitable *ct)
6568 got_commitable_get_status(struct got_commitable *ct)
6573 struct check_rebase_ok_arg {
6574 struct got_worktree *worktree;
6575 struct got_repository *repo;
6578 static const struct got_error *
6579 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6581 const struct got_error *err = NULL;
6582 struct check_rebase_ok_arg *a = arg;
6583 unsigned char status;
6587 /* Reject rebase of a work tree with mixed base commits. */
6588 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6589 SHA1_DIGEST_LENGTH))
6590 return got_error(GOT_ERR_MIXED_COMMITS);
6592 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6594 return got_error_from_errno("asprintf");
6596 /* Reject rebase of a work tree with modified or staged files. */
6597 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6602 if (status != GOT_STATUS_NO_CHANGE)
6603 return got_error(GOT_ERR_MODIFIED);
6604 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6605 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6610 const struct got_error *
6611 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6612 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6613 struct got_worktree *worktree, struct got_reference *branch,
6614 struct got_repository *repo)
6616 const struct got_error *err = NULL;
6617 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6618 char *branch_ref_name = NULL;
6619 char *fileindex_path = NULL;
6620 struct check_rebase_ok_arg ok_arg;
6621 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6622 struct got_object_id *wt_branch_tip = NULL;
6624 *new_base_branch_ref = NULL;
6628 err = lock_worktree(worktree, LOCK_EX);
6632 err = open_fileindex(fileindex, &fileindex_path, worktree);
6636 ok_arg.worktree = worktree;
6638 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6643 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6647 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6651 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6655 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6660 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6663 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6664 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6668 err = got_ref_alloc_symref(new_base_branch_ref,
6669 new_base_branch_ref_name, wt_branch);
6672 err = got_ref_write(*new_base_branch_ref, repo);
6676 /* TODO Lock original branch's ref while rebasing? */
6678 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6682 err = got_ref_write(branch_ref, repo);
6686 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6687 worktree->base_commit_id);
6690 err = got_ref_write(*tmp_branch, repo);
6694 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6698 free(fileindex_path);
6699 free(tmp_branch_name);
6700 free(new_base_branch_ref_name);
6701 free(branch_ref_name);
6703 got_ref_close(branch_ref);
6705 got_ref_close(wt_branch);
6706 free(wt_branch_tip);
6708 if (*new_base_branch_ref) {
6709 got_ref_close(*new_base_branch_ref);
6710 *new_base_branch_ref = NULL;
6713 got_ref_close(*tmp_branch);
6717 got_fileindex_free(*fileindex);
6720 lock_worktree(worktree, LOCK_SH);
6725 const struct got_error *
6726 got_worktree_rebase_continue(struct got_object_id **commit_id,
6727 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6728 struct got_reference **branch, struct got_fileindex **fileindex,
6729 struct got_worktree *worktree, struct got_repository *repo)
6731 const struct got_error *err;
6732 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6733 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6734 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6735 char *fileindex_path = NULL;
6736 int have_staged_files = 0;
6739 *new_base_branch = NULL;
6744 err = lock_worktree(worktree, LOCK_EX);
6748 err = open_fileindex(fileindex, &fileindex_path, worktree);
6752 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6753 &have_staged_files);
6754 if (err && err->code != GOT_ERR_CANCELLED)
6756 if (have_staged_files) {
6757 err = got_error(GOT_ERR_STAGED_PATHS);
6761 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6765 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6769 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6773 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6777 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6781 err = got_ref_open(branch, repo,
6782 got_ref_get_symref_target(branch_ref), 0);
6786 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6790 err = got_ref_resolve(commit_id, repo, commit_ref);
6794 err = got_ref_open(new_base_branch, repo,
6795 new_base_branch_ref_name, 0);
6799 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6803 free(commit_ref_name);
6804 free(branch_ref_name);
6805 free(fileindex_path);
6807 got_ref_close(commit_ref);
6809 got_ref_close(branch_ref);
6814 got_ref_close(*tmp_branch);
6817 if (*new_base_branch) {
6818 got_ref_close(*new_base_branch);
6819 *new_base_branch = NULL;
6822 got_ref_close(*branch);
6826 got_fileindex_free(*fileindex);
6829 lock_worktree(worktree, LOCK_SH);
6834 const struct got_error *
6835 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6837 const struct got_error *err;
6838 char *tmp_branch_name = NULL;
6840 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6844 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6845 free(tmp_branch_name);
6849 static const struct got_error *
6850 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6851 const char *diff_path, char **logmsg, void *arg)
6857 static const struct got_error *
6858 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6859 const char *path, struct got_object_id *blob_id,
6860 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6861 int dirfd, const char *de_name)
6866 struct collect_merged_paths_arg {
6867 got_worktree_checkout_cb progress_cb;
6869 struct got_pathlist_head *merged_paths;
6872 static const struct got_error *
6873 collect_merged_paths(void *arg, unsigned char status, const char *path)
6875 const struct got_error *err;
6876 struct collect_merged_paths_arg *a = arg;
6878 struct got_pathlist_entry *new;
6880 err = (*a->progress_cb)(a->progress_arg, status, path);
6884 if (status != GOT_STATUS_MERGE &&
6885 status != GOT_STATUS_ADD &&
6886 status != GOT_STATUS_DELETE &&
6887 status != GOT_STATUS_CONFLICT)
6892 return got_error_from_errno("strdup");
6894 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6895 if (err || new == NULL)
6900 static const struct got_error *
6901 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6902 int is_rebase, struct got_repository *repo)
6904 const struct got_error *err;
6905 struct got_reference *commit_ref = NULL;
6907 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6909 if (err->code != GOT_ERR_NOT_REF)
6911 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6914 err = got_ref_write(commit_ref, repo);
6917 } else if (is_rebase) {
6918 struct got_object_id *stored_id;
6921 err = got_ref_resolve(&stored_id, repo, commit_ref);
6924 cmp = got_object_id_cmp(commit_id, stored_id);
6927 err = got_error(GOT_ERR_REBASE_COMMITID);
6933 got_ref_close(commit_ref);
6937 static const struct got_error *
6938 rebase_merge_files(struct got_pathlist_head *merged_paths,
6939 const char *commit_ref_name, struct got_worktree *worktree,
6940 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6941 struct got_object_id *commit_id, struct got_repository *repo,
6942 got_worktree_checkout_cb progress_cb, void *progress_arg,
6943 got_cancel_cb cancel_cb, void *cancel_arg)
6945 const struct got_error *err;
6946 struct got_reference *commit_ref = NULL;
6947 struct collect_merged_paths_arg cmp_arg;
6948 char *fileindex_path;
6950 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6952 err = get_fileindex_path(&fileindex_path, worktree);
6956 cmp_arg.progress_cb = progress_cb;
6957 cmp_arg.progress_arg = progress_arg;
6958 cmp_arg.merged_paths = merged_paths;
6959 err = merge_files(worktree, fileindex, fileindex_path,
6960 parent_commit_id, commit_id, repo, collect_merged_paths,
6961 &cmp_arg, cancel_cb, cancel_arg);
6963 got_ref_close(commit_ref);
6967 const struct got_error *
6968 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6969 struct got_worktree *worktree, struct got_fileindex *fileindex,
6970 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6971 struct got_repository *repo,
6972 got_worktree_checkout_cb progress_cb, void *progress_arg,
6973 got_cancel_cb cancel_cb, void *cancel_arg)
6975 const struct got_error *err;
6976 char *commit_ref_name;
6978 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6982 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6986 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6987 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6988 progress_arg, cancel_cb, cancel_arg);
6990 free(commit_ref_name);
6994 const struct got_error *
6995 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6996 struct got_worktree *worktree, struct got_fileindex *fileindex,
6997 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6998 struct got_repository *repo,
6999 got_worktree_checkout_cb progress_cb, void *progress_arg,
7000 got_cancel_cb cancel_cb, void *cancel_arg)
7002 const struct got_error *err;
7003 char *commit_ref_name;
7005 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7009 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7013 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7014 fileindex, parent_commit_id, commit_id, repo, progress_cb,
7015 progress_arg, cancel_cb, cancel_arg);
7017 free(commit_ref_name);
7021 static const struct got_error *
7022 rebase_commit(struct got_object_id **new_commit_id,
7023 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
7024 struct got_worktree *worktree, struct got_fileindex *fileindex,
7025 struct got_reference *tmp_branch, const char *committer,
7026 struct got_commit_object *orig_commit, const char *new_logmsg,
7027 int allow_conflict, struct got_repository *repo)
7029 const struct got_error *err, *sync_err;
7030 struct got_pathlist_head commitable_paths;
7031 struct collect_commitables_arg cc_arg;
7032 char *fileindex_path = NULL;
7033 struct got_reference *head_ref = NULL;
7034 struct got_object_id *head_commit_id = NULL;
7035 char *logmsg = NULL;
7037 memset(&cc_arg, 0, sizeof(cc_arg));
7038 TAILQ_INIT(&commitable_paths);
7039 *new_commit_id = NULL;
7041 /* Work tree is locked/unlocked during rebase preparation/teardown. */
7043 err = get_fileindex_path(&fileindex_path, worktree);
7047 cc_arg.commitable_paths = &commitable_paths;
7048 cc_arg.worktree = worktree;
7050 cc_arg.have_staged_files = 0;
7051 cc_arg.commit_conflicts = allow_conflict;
7053 * If possible get the status of individual files directly to
7054 * avoid crawling the entire work tree once per rebased commit.
7056 * Ideally, merged_paths would contain a list of commitables
7057 * we could use so we could skip worktree_status() entirely.
7058 * However, we would then need carefully keep track of cumulative
7059 * effects of operations such as file additions and deletions
7060 * in 'got histedit -f' (folding multiple commits into one),
7061 * and this extra complexity is not really worth it.
7064 struct got_pathlist_entry *pe;
7065 TAILQ_FOREACH(pe, merged_paths, entry) {
7066 err = worktree_status(worktree, pe->path, fileindex,
7067 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
7073 err = worktree_status(worktree, "", fileindex, repo,
7074 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7079 if (TAILQ_EMPTY(&commitable_paths)) {
7080 /* No-op change; commit will be elided. */
7081 err = got_ref_delete(commit_ref, repo);
7084 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
7088 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7092 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7097 logmsg = strdup(new_logmsg);
7098 if (logmsg == NULL) {
7099 err = got_error_from_errno("strdup");
7103 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
7108 /* NB: commit_worktree will call free(logmsg) */
7109 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
7110 NULL, worktree, got_object_commit_get_author(orig_commit),
7111 committer ? committer :
7112 got_object_commit_get_committer(orig_commit), NULL,
7113 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
7117 err = got_ref_change_ref(tmp_branch, *new_commit_id);
7121 err = got_ref_delete(commit_ref, repo);
7125 err = update_fileindex_after_commit(worktree, &commitable_paths,
7126 *new_commit_id, fileindex, 0);
7127 sync_err = sync_fileindex(fileindex, fileindex_path);
7128 if (sync_err && err == NULL)
7131 free(fileindex_path);
7132 free(head_commit_id);
7134 got_ref_close(head_ref);
7136 free(*new_commit_id);
7137 *new_commit_id = NULL;
7142 const struct got_error *
7143 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
7144 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7145 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7146 const char *committer, struct got_commit_object *orig_commit,
7147 struct got_object_id *orig_commit_id, int allow_conflict,
7148 struct got_repository *repo)
7150 const struct got_error *err;
7151 char *commit_ref_name;
7152 struct got_reference *commit_ref = NULL;
7153 struct got_object_id *commit_id = NULL;
7155 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7159 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7162 err = got_ref_resolve(&commit_id, repo, commit_ref);
7165 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
7166 err = got_error(GOT_ERR_REBASE_COMMITID);
7170 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7171 worktree, fileindex, tmp_branch, committer, orig_commit,
7172 NULL, allow_conflict, repo);
7175 got_ref_close(commit_ref);
7176 free(commit_ref_name);
7181 const struct got_error *
7182 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
7183 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7184 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7185 const char *committer, struct got_commit_object *orig_commit,
7186 struct got_object_id *orig_commit_id, const char *new_logmsg,
7187 int allow_conflict, struct got_repository *repo)
7189 const struct got_error *err;
7190 char *commit_ref_name;
7191 struct got_reference *commit_ref = NULL;
7193 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7197 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7201 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7202 worktree, fileindex, tmp_branch, committer, orig_commit,
7203 new_logmsg, allow_conflict, repo);
7206 got_ref_close(commit_ref);
7207 free(commit_ref_name);
7211 const struct got_error *
7212 got_worktree_rebase_postpone(struct got_worktree *worktree,
7213 struct got_fileindex *fileindex)
7216 got_fileindex_free(fileindex);
7217 return lock_worktree(worktree, LOCK_SH);
7220 static const struct got_error *
7221 delete_ref(const char *name, struct got_repository *repo)
7223 const struct got_error *err;
7224 struct got_reference *ref;
7226 err = got_ref_open(&ref, repo, name, 0);
7228 if (err->code == GOT_ERR_NOT_REF)
7233 err = got_ref_delete(ref, repo);
7238 static const struct got_error *
7239 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7241 const struct got_error *err;
7242 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7243 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7245 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7248 err = delete_ref(tmp_branch_name, repo);
7252 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7255 err = delete_ref(new_base_branch_ref_name, repo);
7259 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7262 err = delete_ref(branch_ref_name, repo);
7266 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7269 err = delete_ref(commit_ref_name, repo);
7274 free(tmp_branch_name);
7275 free(new_base_branch_ref_name);
7276 free(branch_ref_name);
7277 free(commit_ref_name);
7281 static const struct got_error *
7282 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7283 struct got_object_id *new_commit_id, struct got_repository *repo)
7285 const struct got_error *err;
7286 struct got_reference *ref = NULL;
7287 struct got_object_id *old_commit_id = NULL;
7288 const char *branch_name = NULL;
7289 char *new_id_str = NULL;
7290 char *refname = NULL;
7292 branch_name = got_ref_get_name(branch);
7293 if (strncmp(branch_name, "refs/heads/", 11) != 0)
7294 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7297 err = got_object_id_str(&new_id_str, new_commit_id);
7301 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7302 new_id_str) == -1) {
7303 err = got_error_from_errno("asprintf");
7307 err = got_ref_resolve(&old_commit_id, repo, branch);
7311 err = got_ref_alloc(&ref, refname, old_commit_id);
7315 err = got_ref_write(ref, repo);
7319 free(old_commit_id);
7325 const struct got_error *
7326 got_worktree_rebase_complete(struct got_worktree *worktree,
7327 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7328 struct got_reference *rebased_branch, struct got_repository *repo,
7331 const struct got_error *err, *unlockerr, *sync_err;
7332 struct got_object_id *new_head_commit_id = NULL;
7333 char *fileindex_path = NULL;
7335 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7339 if (create_backup) {
7340 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7341 rebased_branch, new_head_commit_id, repo);
7346 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7350 err = got_ref_write(rebased_branch, repo);
7354 err = got_worktree_set_head_ref(worktree, rebased_branch);
7358 err = delete_rebase_refs(worktree, repo);
7362 err = get_fileindex_path(&fileindex_path, worktree);
7365 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7366 sync_err = sync_fileindex(fileindex, fileindex_path);
7367 if (sync_err && err == NULL)
7370 got_fileindex_free(fileindex);
7371 free(fileindex_path);
7372 free(new_head_commit_id);
7373 unlockerr = lock_worktree(worktree, LOCK_SH);
7374 if (unlockerr && err == NULL)
7379 static const struct got_error *
7380 get_paths_changed_between_commits(struct got_pathlist_head *paths,
7381 struct got_object_id *id1, struct got_object_id *id2,
7382 struct got_repository *repo)
7384 const struct got_error *err;
7385 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
7386 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7389 err = got_object_open_as_commit(&commit1, repo, id1);
7393 err = got_object_open_as_tree(&tree1, repo,
7394 got_object_commit_get_tree_id(commit1));
7400 err = got_object_open_as_commit(&commit2, repo, id2);
7404 err = got_object_open_as_tree(&tree2, repo,
7405 got_object_commit_get_tree_id(commit2));
7410 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
7411 got_diff_tree_collect_changed_paths, paths, 0);
7416 got_object_commit_close(commit1);
7418 got_object_commit_close(commit2);
7420 got_object_tree_close(tree1);
7422 got_object_tree_close(tree2);
7426 static const struct got_error *
7427 get_paths_added_between_commits(struct got_pathlist_head *added_paths,
7428 struct got_object_id *id1, struct got_object_id *id2,
7429 const char *path_prefix, struct got_repository *repo)
7431 const struct got_error *err;
7432 struct got_pathlist_head merged_paths;
7433 struct got_pathlist_entry *pe;
7434 char *abspath = NULL, *wt_path = NULL;
7436 TAILQ_INIT(&merged_paths);
7438 err = get_paths_changed_between_commits(&merged_paths, id1, id2, repo);
7442 TAILQ_FOREACH(pe, &merged_paths, entry) {
7443 struct got_diff_changed_path *change = pe->data;
7445 if (change->status != GOT_STATUS_ADD)
7448 if (got_path_is_root_dir(path_prefix)) {
7449 wt_path = strdup(pe->path);
7450 if (wt_path == NULL) {
7451 err = got_error_from_errno("strdup");
7455 if (asprintf(&abspath, "/%s", pe->path) == -1) {
7456 err = got_error_from_errno("asprintf");
7460 err = got_path_skip_common_ancestor(&wt_path,
7461 path_prefix, abspath);
7468 err = got_pathlist_append(added_paths, wt_path, NULL);
7475 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_ALL);
7481 static const struct got_error *
7482 get_paths_added_in_commit(struct got_pathlist_head *added_paths,
7483 struct got_object_id *id, const char *path_prefix,
7484 struct got_repository *repo)
7486 const struct got_error *err;
7487 struct got_commit_object *commit = NULL;
7488 struct got_object_qid *pid;
7490 err = got_object_open_as_commit(&commit, repo, id);
7494 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7496 err = get_paths_added_between_commits(added_paths,
7497 pid ? &pid->id : NULL, id, path_prefix, repo);
7502 got_object_commit_close(commit);
7506 const struct got_error *
7507 got_worktree_rebase_abort(struct got_worktree *worktree,
7508 struct got_fileindex *fileindex, struct got_repository *repo,
7509 struct got_reference *new_base_branch,
7510 got_worktree_checkout_cb progress_cb, void *progress_arg)
7512 const struct got_error *err, *unlockerr, *sync_err;
7513 struct got_reference *resolved = NULL;
7514 struct got_object_id *commit_id = NULL;
7515 struct got_object_id *merged_commit_id = NULL;
7516 struct got_commit_object *commit = NULL;
7517 char *fileindex_path = NULL;
7518 char *commit_ref_name = NULL;
7519 struct got_reference *commit_ref = NULL;
7520 struct revert_file_args rfa;
7521 struct got_object_id *tree_id = NULL;
7522 struct got_pathlist_head added_paths;
7524 TAILQ_INIT(&added_paths);
7526 err = lock_worktree(worktree, LOCK_EX);
7530 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7534 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7538 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
7543 * Determine which files in added status can be safely removed
7544 * from disk while reverting changes in the work tree.
7545 * We want to avoid deleting unrelated files which were added by
7546 * the user for conflict resolution purposes.
7548 err = get_paths_added_in_commit(&added_paths, merged_commit_id,
7549 got_worktree_get_path_prefix(worktree), repo);
7553 err = got_ref_open(&resolved, repo,
7554 got_ref_get_symref_target(new_base_branch), 0);
7558 err = got_worktree_set_head_ref(worktree, resolved);
7563 * XXX commits to the base branch could have happened while
7564 * we were busy rebasing; should we store the original commit ID
7565 * when rebase begins and read it back here?
7567 err = got_ref_resolve(&commit_id, repo, resolved);
7571 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7575 err = got_object_open_as_commit(&commit, repo,
7576 worktree->base_commit_id);
7580 err = got_object_id_by_path(&tree_id, repo, commit,
7581 worktree->path_prefix);
7585 err = delete_rebase_refs(worktree, repo);
7589 err = get_fileindex_path(&fileindex_path, worktree);
7593 rfa.worktree = worktree;
7594 rfa.fileindex = fileindex;
7595 rfa.progress_cb = progress_cb;
7596 rfa.progress_arg = progress_arg;
7597 rfa.patch_cb = NULL;
7598 rfa.patch_arg = NULL;
7600 rfa.unlink_added_files = 1;
7601 rfa.added_files_to_unlink = &added_paths;
7602 err = worktree_status(worktree, "", fileindex, repo,
7603 revert_file, &rfa, NULL, NULL, 1, 0);
7607 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7608 repo, progress_cb, progress_arg, NULL, NULL);
7610 sync_err = sync_fileindex(fileindex, fileindex_path);
7611 if (sync_err && err == NULL)
7614 got_pathlist_free(&added_paths, GOT_PATHLIST_FREE_PATH);
7615 got_ref_close(resolved);
7618 free(merged_commit_id);
7620 got_object_commit_close(commit);
7622 got_fileindex_free(fileindex);
7623 free(fileindex_path);
7624 free(commit_ref_name);
7626 got_ref_close(commit_ref);
7628 unlockerr = lock_worktree(worktree, LOCK_SH);
7629 if (unlockerr && err == NULL)
7634 const struct got_error *
7635 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7636 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7637 struct got_fileindex **fileindex, struct got_worktree *worktree,
7638 struct got_repository *repo)
7640 const struct got_error *err = NULL;
7641 char *tmp_branch_name = NULL;
7642 char *branch_ref_name = NULL;
7643 char *base_commit_ref_name = NULL;
7644 char *fileindex_path = NULL;
7645 struct check_rebase_ok_arg ok_arg;
7646 struct got_reference *wt_branch = NULL;
7647 struct got_reference *base_commit_ref = NULL;
7651 *base_commit_id = NULL;
7654 err = lock_worktree(worktree, LOCK_EX);
7658 err = open_fileindex(fileindex, &fileindex_path, worktree);
7662 ok_arg.worktree = worktree;
7664 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7669 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7673 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7677 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7682 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7687 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7691 err = got_ref_write(*branch_ref, repo);
7695 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7696 worktree->base_commit_id);
7699 err = got_ref_write(base_commit_ref, repo);
7702 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7703 if (*base_commit_id == NULL) {
7704 err = got_error_from_errno("got_object_id_dup");
7708 err = got_ref_alloc(tmp_branch, tmp_branch_name,
7709 worktree->base_commit_id);
7712 err = got_ref_write(*tmp_branch, repo);
7716 err = got_worktree_set_head_ref(worktree, *tmp_branch);
7720 free(fileindex_path);
7721 free(tmp_branch_name);
7722 free(branch_ref_name);
7723 free(base_commit_ref_name);
7725 got_ref_close(wt_branch);
7728 got_ref_close(*branch_ref);
7732 got_ref_close(*tmp_branch);
7735 free(*base_commit_id);
7737 got_fileindex_free(*fileindex);
7740 lock_worktree(worktree, LOCK_SH);
7745 const struct got_error *
7746 got_worktree_histedit_postpone(struct got_worktree *worktree,
7747 struct got_fileindex *fileindex)
7750 got_fileindex_free(fileindex);
7751 return lock_worktree(worktree, LOCK_SH);
7754 const struct got_error *
7755 got_worktree_histedit_in_progress(int *in_progress,
7756 struct got_worktree *worktree)
7758 const struct got_error *err;
7759 char *tmp_branch_name = NULL;
7761 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7765 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7766 free(tmp_branch_name);
7770 const struct got_error *
7771 got_worktree_histedit_continue(struct got_object_id **commit_id,
7772 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7773 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7774 struct got_worktree *worktree, struct got_repository *repo)
7776 const struct got_error *err;
7777 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7778 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7779 struct got_reference *commit_ref = NULL;
7780 struct got_reference *base_commit_ref = NULL;
7781 char *fileindex_path = NULL;
7782 int have_staged_files = 0;
7786 *base_commit_id = NULL;
7789 err = lock_worktree(worktree, LOCK_EX);
7793 err = open_fileindex(fileindex, &fileindex_path, worktree);
7797 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7798 &have_staged_files);
7799 if (err && err->code != GOT_ERR_CANCELLED)
7801 if (have_staged_files) {
7802 err = got_error(GOT_ERR_STAGED_PATHS);
7806 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7810 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7814 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7818 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7823 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7827 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7830 err = got_ref_resolve(commit_id, repo, commit_ref);
7834 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7837 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7841 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7845 free(commit_ref_name);
7846 free(branch_ref_name);
7847 free(fileindex_path);
7849 got_ref_close(commit_ref);
7850 if (base_commit_ref)
7851 got_ref_close(base_commit_ref);
7855 free(*base_commit_id);
7856 *base_commit_id = NULL;
7858 got_ref_close(*tmp_branch);
7862 got_fileindex_free(*fileindex);
7865 lock_worktree(worktree, LOCK_EX);
7870 static const struct got_error *
7871 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7873 const struct got_error *err;
7874 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7875 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7877 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7880 err = delete_ref(tmp_branch_name, repo);
7884 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7888 err = delete_ref(base_commit_ref_name, repo);
7892 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7895 err = delete_ref(branch_ref_name, repo);
7899 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7902 err = delete_ref(commit_ref_name, repo);
7906 free(tmp_branch_name);
7907 free(base_commit_ref_name);
7908 free(branch_ref_name);
7909 free(commit_ref_name);
7913 const struct got_error *
7914 got_worktree_histedit_abort(struct got_worktree *worktree,
7915 struct got_fileindex *fileindex, struct got_repository *repo,
7916 struct got_reference *branch, struct got_object_id *base_commit_id,
7917 got_worktree_checkout_cb progress_cb, void *progress_arg)
7919 const struct got_error *err, *unlockerr, *sync_err;
7920 struct got_reference *resolved = NULL;
7921 char *fileindex_path = NULL;
7922 struct got_object_id *merged_commit_id = NULL;
7923 struct got_commit_object *commit = NULL;
7924 char *commit_ref_name = NULL;
7925 struct got_reference *commit_ref = NULL;
7926 struct got_object_id *tree_id = NULL;
7927 struct revert_file_args rfa;
7928 struct got_pathlist_head added_paths;
7930 TAILQ_INIT(&added_paths);
7932 err = lock_worktree(worktree, LOCK_EX);
7936 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7940 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7942 if (err->code != GOT_ERR_NOT_REF)
7944 /* Can happen on early abort due to invalid histedit script. */
7949 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
7954 * Determine which files in added status can be safely removed
7955 * from disk while reverting changes in the work tree.
7956 * We want to avoid deleting unrelated files added by the
7957 * user during conflict resolution or during histedit -e.
7959 err = get_paths_added_in_commit(&added_paths, merged_commit_id,
7960 got_worktree_get_path_prefix(worktree), repo);
7965 err = got_ref_open(&resolved, repo,
7966 got_ref_get_symref_target(branch), 0);
7970 err = got_worktree_set_head_ref(worktree, resolved);
7974 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7978 err = got_object_open_as_commit(&commit, repo,
7979 worktree->base_commit_id);
7983 err = got_object_id_by_path(&tree_id, repo, commit,
7984 worktree->path_prefix);
7988 err = delete_histedit_refs(worktree, repo);
7992 err = get_fileindex_path(&fileindex_path, worktree);
7996 rfa.worktree = worktree;
7997 rfa.fileindex = fileindex;
7998 rfa.progress_cb = progress_cb;
7999 rfa.progress_arg = progress_arg;
8000 rfa.patch_cb = NULL;
8001 rfa.patch_arg = NULL;
8003 rfa.unlink_added_files = 1;
8004 rfa.added_files_to_unlink = &added_paths;
8005 err = worktree_status(worktree, "", fileindex, repo,
8006 revert_file, &rfa, NULL, NULL, 1, 0);
8010 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8011 repo, progress_cb, progress_arg, NULL, NULL);
8013 sync_err = sync_fileindex(fileindex, fileindex_path);
8014 if (sync_err && err == NULL)
8018 got_ref_close(resolved);
8020 got_ref_close(commit_ref);
8021 free(merged_commit_id);
8023 free(fileindex_path);
8024 free(commit_ref_name);
8026 unlockerr = lock_worktree(worktree, LOCK_SH);
8027 if (unlockerr && err == NULL)
8032 const struct got_error *
8033 got_worktree_histedit_complete(struct got_worktree *worktree,
8034 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8035 struct got_reference *edited_branch, struct got_repository *repo)
8037 const struct got_error *err, *unlockerr, *sync_err;
8038 struct got_object_id *new_head_commit_id = NULL;
8039 struct got_reference *resolved = NULL;
8040 char *fileindex_path = NULL;
8042 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
8046 err = got_ref_open(&resolved, repo,
8047 got_ref_get_symref_target(edited_branch), 0);
8051 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
8052 resolved, new_head_commit_id, repo);
8056 err = got_ref_change_ref(resolved, new_head_commit_id);
8060 err = got_ref_write(resolved, repo);
8064 err = got_worktree_set_head_ref(worktree, resolved);
8068 err = delete_histedit_refs(worktree, repo);
8072 err = get_fileindex_path(&fileindex_path, worktree);
8075 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8076 sync_err = sync_fileindex(fileindex, fileindex_path);
8077 if (sync_err && err == NULL)
8080 got_fileindex_free(fileindex);
8081 free(fileindex_path);
8082 free(new_head_commit_id);
8083 unlockerr = lock_worktree(worktree, LOCK_SH);
8084 if (unlockerr && err == NULL)
8089 const struct got_error *
8090 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
8091 struct got_object_id *commit_id, struct got_repository *repo)
8093 const struct got_error *err;
8094 char *commit_ref_name;
8096 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8100 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
8104 err = delete_ref(commit_ref_name, repo);
8106 free(commit_ref_name);
8110 const struct got_error *
8111 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
8112 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
8113 struct got_worktree *worktree, const char *refname,
8114 struct got_repository *repo)
8116 const struct got_error *err = NULL;
8117 char *fileindex_path = NULL;
8118 struct check_rebase_ok_arg ok_arg;
8122 *base_branch_ref = NULL;
8124 err = lock_worktree(worktree, LOCK_EX);
8128 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
8129 err = got_error_msg(GOT_ERR_SAME_BRANCH,
8130 "cannot integrate a branch into itself; "
8131 "update -b or different branch name required");
8135 err = open_fileindex(fileindex, &fileindex_path, worktree);
8139 /* Preconditions are the same as for rebase. */
8140 ok_arg.worktree = worktree;
8142 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8147 err = got_ref_open(branch_ref, repo, refname, 1);
8151 err = got_ref_open(base_branch_ref, repo,
8152 got_worktree_get_head_ref_name(worktree), 1);
8156 got_ref_close(*branch_ref);
8159 if (*base_branch_ref) {
8160 got_ref_close(*base_branch_ref);
8161 *base_branch_ref = NULL;
8164 got_fileindex_free(*fileindex);
8167 lock_worktree(worktree, LOCK_SH);
8172 const struct got_error *
8173 got_worktree_integrate_continue(struct got_worktree *worktree,
8174 struct got_fileindex *fileindex, struct got_repository *repo,
8175 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
8176 got_worktree_checkout_cb progress_cb, void *progress_arg,
8177 got_cancel_cb cancel_cb, void *cancel_arg)
8179 const struct got_error *err = NULL, *sync_err, *unlockerr;
8180 char *fileindex_path = NULL;
8181 struct got_object_id *tree_id = NULL, *commit_id = NULL;
8182 struct got_commit_object *commit = NULL;
8184 err = get_fileindex_path(&fileindex_path, worktree);
8188 err = got_ref_resolve(&commit_id, repo, branch_ref);
8192 err = got_object_open_as_commit(&commit, repo, commit_id);
8196 err = got_object_id_by_path(&tree_id, repo, commit,
8197 worktree->path_prefix);
8201 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
8205 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
8206 progress_cb, progress_arg, cancel_cb, cancel_arg);
8210 err = got_ref_change_ref(base_branch_ref, commit_id);
8214 err = got_ref_write(base_branch_ref, repo);
8218 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8220 sync_err = sync_fileindex(fileindex, fileindex_path);
8221 if (sync_err && err == NULL)
8225 unlockerr = got_ref_unlock(branch_ref);
8226 if (unlockerr && err == NULL)
8228 got_ref_close(branch_ref);
8230 unlockerr = got_ref_unlock(base_branch_ref);
8231 if (unlockerr && err == NULL)
8233 got_ref_close(base_branch_ref);
8235 got_fileindex_free(fileindex);
8236 free(fileindex_path);
8239 got_object_commit_close(commit);
8241 unlockerr = lock_worktree(worktree, LOCK_SH);
8242 if (unlockerr && err == NULL)
8247 const struct got_error *
8248 got_worktree_integrate_abort(struct got_worktree *worktree,
8249 struct got_fileindex *fileindex, struct got_repository *repo,
8250 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
8252 const struct got_error *err = NULL, *unlockerr = NULL;
8254 got_fileindex_free(fileindex);
8256 err = lock_worktree(worktree, LOCK_SH);
8258 unlockerr = got_ref_unlock(branch_ref);
8259 if (unlockerr && err == NULL)
8261 got_ref_close(branch_ref);
8263 unlockerr = got_ref_unlock(base_branch_ref);
8264 if (unlockerr && err == NULL)
8266 got_ref_close(base_branch_ref);
8271 const struct got_error *
8272 got_worktree_merge_postpone(struct got_worktree *worktree,
8273 struct got_fileindex *fileindex)
8275 const struct got_error *err, *sync_err;
8276 char *fileindex_path = NULL;
8278 err = get_fileindex_path(&fileindex_path, worktree);
8282 sync_err = sync_fileindex(fileindex, fileindex_path);
8284 err = lock_worktree(worktree, LOCK_SH);
8285 if (sync_err && err == NULL)
8288 got_fileindex_free(fileindex);
8289 free(fileindex_path);
8293 static const struct got_error *
8294 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
8296 const struct got_error *err;
8297 char *branch_refname = NULL, *commit_refname = NULL;
8299 err = get_merge_branch_ref_name(&branch_refname, worktree);
8302 err = delete_ref(branch_refname, repo);
8306 err = get_merge_commit_ref_name(&commit_refname, worktree);
8309 err = delete_ref(commit_refname, repo);
8314 free(branch_refname);
8315 free(commit_refname);
8319 struct merge_commit_msg_arg {
8320 struct got_worktree *worktree;
8321 const char *branch_name;
8324 static const struct got_error *
8325 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
8326 const char *diff_path, char **logmsg, void *arg)
8328 struct merge_commit_msg_arg *a = arg;
8330 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
8331 got_worktree_get_head_ref_name(a->worktree)) == -1)
8332 return got_error_from_errno("asprintf");
8338 const struct got_error *
8339 got_worktree_merge_branch(struct got_worktree *worktree,
8340 struct got_fileindex *fileindex,
8341 struct got_object_id *yca_commit_id,
8342 struct got_object_id *branch_tip,
8343 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
8344 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
8346 const struct got_error *err;
8347 char *fileindex_path = NULL;
8349 err = get_fileindex_path(&fileindex_path, worktree);
8353 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
8358 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
8359 branch_tip, repo, progress_cb, progress_arg,
8360 cancel_cb, cancel_arg);
8362 free(fileindex_path);
8366 const struct got_error *
8367 got_worktree_merge_commit(struct got_object_id **new_commit_id,
8368 struct got_worktree *worktree, struct got_fileindex *fileindex,
8369 const char *author, const char *committer, int allow_bad_symlinks,
8370 struct got_object_id *branch_tip, const char *branch_name,
8371 int allow_conflict, struct got_repository *repo,
8372 got_worktree_status_cb status_cb, void *status_arg)
8375 const struct got_error *err = NULL, *sync_err;
8376 struct got_pathlist_head commitable_paths;
8377 struct collect_commitables_arg cc_arg;
8378 struct got_pathlist_entry *pe;
8379 struct got_reference *head_ref = NULL;
8380 struct got_object_id *head_commit_id = NULL;
8381 int have_staged_files = 0;
8382 struct merge_commit_msg_arg mcm_arg;
8383 char *fileindex_path = NULL;
8385 memset(&cc_arg, 0, sizeof(cc_arg));
8386 *new_commit_id = NULL;
8388 TAILQ_INIT(&commitable_paths);
8390 err = get_fileindex_path(&fileindex_path, worktree);
8394 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
8398 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8402 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
8403 &have_staged_files);
8404 if (err && err->code != GOT_ERR_CANCELLED)
8406 if (have_staged_files) {
8407 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
8411 cc_arg.commitable_paths = &commitable_paths;
8412 cc_arg.worktree = worktree;
8413 cc_arg.fileindex = fileindex;
8415 cc_arg.have_staged_files = have_staged_files;
8416 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
8417 cc_arg.commit_conflicts = allow_conflict;
8418 err = worktree_status(worktree, "", fileindex, repo,
8419 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8423 mcm_arg.worktree = worktree;
8424 mcm_arg.branch_name = branch_name;
8425 err = commit_worktree(new_commit_id, &commitable_paths,
8426 head_commit_id, branch_tip, worktree, author, committer, NULL,
8427 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8431 err = update_fileindex_after_commit(worktree, &commitable_paths,
8432 *new_commit_id, fileindex, have_staged_files);
8433 sync_err = sync_fileindex(fileindex, fileindex_path);
8434 if (sync_err && err == NULL)
8437 TAILQ_FOREACH(pe, &commitable_paths, entry) {
8438 struct got_commitable *ct = pe->data;
8440 free_commitable(ct);
8442 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8443 free(fileindex_path);
8447 const struct got_error *
8448 got_worktree_merge_complete(struct got_worktree *worktree,
8449 struct got_fileindex *fileindex, struct got_repository *repo)
8451 const struct got_error *err, *unlockerr, *sync_err;
8452 char *fileindex_path = NULL;
8454 err = delete_merge_refs(worktree, repo);
8458 err = get_fileindex_path(&fileindex_path, worktree);
8461 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8462 sync_err = sync_fileindex(fileindex, fileindex_path);
8463 if (sync_err && err == NULL)
8466 got_fileindex_free(fileindex);
8467 free(fileindex_path);
8468 unlockerr = lock_worktree(worktree, LOCK_SH);
8469 if (unlockerr && err == NULL)
8474 const struct got_error *
8475 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8476 struct got_repository *repo)
8478 const struct got_error *err;
8479 char *branch_refname = NULL;
8480 struct got_reference *branch_ref = NULL;
8484 err = get_merge_branch_ref_name(&branch_refname, worktree);
8487 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8488 free(branch_refname);
8490 if (err->code != GOT_ERR_NOT_REF)
8498 const struct got_error *got_worktree_merge_prepare(
8499 struct got_fileindex **fileindex, struct got_worktree *worktree,
8500 struct got_repository *repo)
8502 const struct got_error *err = NULL;
8503 char *fileindex_path = NULL;
8504 struct got_reference *wt_branch = NULL;
8505 struct got_object_id *wt_branch_tip = NULL;
8506 struct check_rebase_ok_arg ok_arg;
8510 err = lock_worktree(worktree, LOCK_EX);
8514 err = open_fileindex(fileindex, &fileindex_path, worktree);
8518 /* Preconditions are the same as for rebase. */
8519 ok_arg.worktree = worktree;
8521 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8526 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8531 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8535 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8536 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8541 free(fileindex_path);
8543 got_ref_close(wt_branch);
8544 free(wt_branch_tip);
8547 got_fileindex_free(*fileindex);
8550 lock_worktree(worktree, LOCK_SH);
8555 const struct got_error *got_worktree_merge_write_refs(
8556 struct got_worktree *worktree, struct got_reference *branch,
8557 struct got_repository *repo)
8559 const struct got_error *err = NULL;
8560 char *branch_refname = NULL, *commit_refname = NULL;
8561 struct got_reference *branch_ref = NULL, *commit_ref = NULL;
8562 struct got_object_id *branch_tip = NULL;
8564 err = get_merge_branch_ref_name(&branch_refname, worktree);
8568 err = get_merge_commit_ref_name(&commit_refname, worktree);
8572 err = got_ref_resolve(&branch_tip, repo, branch);
8576 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8579 err = got_ref_write(branch_ref, repo);
8583 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8586 err = got_ref_write(commit_ref, repo);
8591 free(branch_refname);
8592 free(commit_refname);
8594 got_ref_close(branch_ref);
8596 got_ref_close(commit_ref);
8601 const struct got_error *
8602 got_worktree_merge_continue(char **branch_name,
8603 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8604 struct got_worktree *worktree, struct got_repository *repo)
8606 const struct got_error *err;
8607 char *commit_refname = NULL, *branch_refname = NULL;
8608 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8609 char *fileindex_path = NULL;
8610 int have_staged_files = 0;
8612 *branch_name = NULL;
8616 err = lock_worktree(worktree, LOCK_EX);
8620 err = open_fileindex(fileindex, &fileindex_path, worktree);
8624 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8625 &have_staged_files);
8626 if (err && err->code != GOT_ERR_CANCELLED)
8628 if (have_staged_files) {
8629 err = got_error(GOT_ERR_STAGED_PATHS);
8633 err = get_merge_branch_ref_name(&branch_refname, worktree);
8637 err = get_merge_commit_ref_name(&commit_refname, worktree);
8641 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8645 if (!got_ref_is_symbolic(branch_ref)) {
8646 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8647 "%s is not a symbolic reference",
8648 got_ref_get_name(branch_ref));
8651 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8652 if (*branch_name == NULL) {
8653 err = got_error_from_errno("strdup");
8657 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8661 err = got_ref_resolve(branch_tip, repo, commit_ref);
8665 free(commit_refname);
8666 free(branch_refname);
8667 free(fileindex_path);
8669 got_ref_close(commit_ref);
8671 got_ref_close(branch_ref);
8675 *branch_name = NULL;
8680 got_fileindex_free(*fileindex);
8683 lock_worktree(worktree, LOCK_SH);
8688 const struct got_error *
8689 got_worktree_merge_abort(struct got_worktree *worktree,
8690 struct got_fileindex *fileindex, struct got_repository *repo,
8691 got_worktree_checkout_cb progress_cb, void *progress_arg)
8693 const struct got_error *err, *unlockerr, *sync_err;
8694 struct got_commit_object *commit = NULL;
8695 char *fileindex_path = NULL;
8696 struct revert_file_args rfa;
8697 char *commit_ref_name = NULL;
8698 struct got_reference *commit_ref = NULL;
8699 struct got_object_id *merged_commit_id = NULL;
8700 struct got_object_id *tree_id = NULL;
8701 struct got_pathlist_head added_paths;
8703 TAILQ_INIT(&added_paths);
8705 err = get_merge_commit_ref_name(&commit_ref_name, worktree);
8709 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8713 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8718 * Determine which files in added status can be safely removed
8719 * from disk while reverting changes in the work tree.
8720 * We want to avoid deleting unrelated files which were added by
8721 * the user for conflict resolution purposes.
8723 err = get_paths_added_between_commits(&added_paths,
8724 got_worktree_get_base_commit_id(worktree), merged_commit_id,
8725 got_worktree_get_path_prefix(worktree), repo);
8730 err = got_object_open_as_commit(&commit, repo,
8731 worktree->base_commit_id);
8735 err = got_object_id_by_path(&tree_id, repo, commit,
8736 worktree->path_prefix);
8740 err = delete_merge_refs(worktree, repo);
8744 err = get_fileindex_path(&fileindex_path, worktree);
8748 rfa.worktree = worktree;
8749 rfa.fileindex = fileindex;
8750 rfa.progress_cb = progress_cb;
8751 rfa.progress_arg = progress_arg;
8752 rfa.patch_cb = NULL;
8753 rfa.patch_arg = NULL;
8755 rfa.unlink_added_files = 1;
8756 rfa.added_files_to_unlink = &added_paths;
8757 err = worktree_status(worktree, "", fileindex, repo,
8758 revert_file, &rfa, NULL, NULL, 1, 0);
8762 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8763 repo, progress_cb, progress_arg, NULL, NULL);
8765 sync_err = sync_fileindex(fileindex, fileindex_path);
8766 if (sync_err && err == NULL)
8770 free(merged_commit_id);
8772 got_object_commit_close(commit);
8774 got_fileindex_free(fileindex);
8775 free(fileindex_path);
8777 got_ref_close(commit_ref);
8778 free(commit_ref_name);
8780 unlockerr = lock_worktree(worktree, LOCK_SH);
8781 if (unlockerr && err == NULL)
8786 struct check_stage_ok_arg {
8787 struct got_object_id *head_commit_id;
8788 struct got_worktree *worktree;
8789 struct got_fileindex *fileindex;
8790 struct got_repository *repo;
8794 static const struct got_error *
8795 check_stage_ok(void *arg, unsigned char status,
8796 unsigned char staged_status, const char *relpath,
8797 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8798 struct got_object_id *commit_id, int dirfd, const char *de_name)
8800 struct check_stage_ok_arg *a = arg;
8801 const struct got_error *err = NULL;
8802 struct got_fileindex_entry *ie;
8803 struct got_object_id base_commit_id;
8804 struct got_object_id *base_commit_idp = NULL;
8805 char *in_repo_path = NULL, *p;
8807 if (status == GOT_STATUS_UNVERSIONED ||
8808 status == GOT_STATUS_NO_CHANGE)
8810 if (status == GOT_STATUS_NONEXISTENT)
8811 return got_error_set_errno(ENOENT, relpath);
8813 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8815 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8817 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8818 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8820 return got_error_from_errno("asprintf");
8822 if (got_fileindex_entry_has_commit(ie)) {
8823 base_commit_idp = got_fileindex_entry_get_commit_id(
8824 &base_commit_id, ie);
8827 if (status == GOT_STATUS_CONFLICT) {
8828 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8830 } else if (status != GOT_STATUS_ADD &&
8831 status != GOT_STATUS_MODIFY &&
8832 status != GOT_STATUS_DELETE) {
8833 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8837 a->have_changes = 1;
8842 err = check_out_of_date(p, status, staged_status,
8843 blob_id, base_commit_idp, a->head_commit_id, a->repo,
8844 GOT_ERR_STAGE_OUT_OF_DATE);
8850 struct stage_path_arg {
8851 struct got_worktree *worktree;
8852 struct got_fileindex *fileindex;
8853 struct got_repository *repo;
8854 got_worktree_status_cb status_cb;
8856 got_worktree_patch_cb patch_cb;
8858 int staged_something;
8859 int allow_bad_symlinks;
8862 static const struct got_error *
8863 stage_path(void *arg, unsigned char status,
8864 unsigned char staged_status, const char *relpath,
8865 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8866 struct got_object_id *commit_id, int dirfd, const char *de_name)
8868 struct stage_path_arg *a = arg;
8869 const struct got_error *err = NULL;
8870 struct got_fileindex_entry *ie;
8871 char *ondisk_path = NULL, *path_content = NULL;
8873 struct got_object_id *new_staged_blob_id = NULL;
8876 if (status == GOT_STATUS_UNVERSIONED)
8879 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8881 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8883 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8885 return got_error_from_errno("asprintf");
8888 case GOT_STATUS_ADD:
8889 case GOT_STATUS_MODIFY:
8890 /* XXX could sb.st_mode be passed in by our caller? */
8891 if (lstat(ondisk_path, &sb) == -1) {
8892 err = got_error_from_errno2("lstat", ondisk_path);
8896 if (status == GOT_STATUS_ADD) {
8897 int choice = GOT_PATCH_CHOICE_NONE;
8898 err = (*a->patch_cb)(&choice, a->patch_arg,
8899 status, ie->path, NULL, 1, 1);
8902 if (choice != GOT_PATCH_CHOICE_YES)
8905 err = create_patched_content(&path_content, 0,
8906 staged_blob_id ? staged_blob_id : blob_id,
8907 ondisk_path, dirfd, de_name, ie->path,
8908 a->repo, a->patch_cb, a->patch_arg);
8909 if (err || path_content == NULL)
8913 err = got_object_blob_create(&new_staged_blob_id,
8914 path_content ? path_content : ondisk_path, a->repo);
8917 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8918 SHA1_DIGEST_LENGTH);
8919 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8920 stage = GOT_FILEIDX_STAGE_ADD;
8922 stage = GOT_FILEIDX_STAGE_MODIFY;
8923 got_fileindex_entry_stage_set(ie, stage);
8924 if (S_ISLNK(sb.st_mode)) {
8925 int is_bad_symlink = 0;
8926 if (!a->allow_bad_symlinks) {
8927 char target_path[PATH_MAX];
8929 target_len = readlink(ondisk_path, target_path,
8930 sizeof(target_path));
8931 if (target_len == -1) {
8932 err = got_error_from_errno2("readlink",
8936 err = is_bad_symlink_target(&is_bad_symlink,
8937 target_path, target_len, ondisk_path,
8938 a->worktree->root_path);
8941 if (is_bad_symlink) {
8942 err = got_error_path(ondisk_path,
8943 GOT_ERR_BAD_SYMLINK);
8948 got_fileindex_entry_staged_filetype_set(ie,
8949 GOT_FILEIDX_MODE_BAD_SYMLINK);
8951 got_fileindex_entry_staged_filetype_set(ie,
8952 GOT_FILEIDX_MODE_SYMLINK);
8954 got_fileindex_entry_staged_filetype_set(ie,
8955 GOT_FILEIDX_MODE_REGULAR_FILE);
8957 a->staged_something = 1;
8958 if (a->status_cb == NULL)
8960 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8961 get_staged_status(ie), relpath, blob_id,
8962 new_staged_blob_id, NULL, dirfd, de_name);
8966 * When staging the reverse of the staged diff,
8967 * implicitly unstage the file.
8969 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8970 sizeof(ie->blob_sha1)) == 0) {
8971 got_fileindex_entry_stage_set(ie,
8972 GOT_FILEIDX_STAGE_NONE);
8975 case GOT_STATUS_DELETE:
8976 if (staged_status == GOT_STATUS_DELETE)
8979 int choice = GOT_PATCH_CHOICE_NONE;
8980 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8981 ie->path, NULL, 1, 1);
8984 if (choice == GOT_PATCH_CHOICE_NO)
8986 if (choice != GOT_PATCH_CHOICE_YES) {
8987 err = got_error(GOT_ERR_PATCH_CHOICE);
8991 stage = GOT_FILEIDX_STAGE_DELETE;
8992 got_fileindex_entry_stage_set(ie, stage);
8993 a->staged_something = 1;
8994 if (a->status_cb == NULL)
8996 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8997 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
9000 case GOT_STATUS_NO_CHANGE:
9002 case GOT_STATUS_CONFLICT:
9003 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
9005 case GOT_STATUS_NONEXISTENT:
9006 err = got_error_set_errno(ENOENT, relpath);
9009 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
9013 if (path_content && unlink(path_content) == -1 && err == NULL)
9014 err = got_error_from_errno2("unlink", path_content);
9017 free(new_staged_blob_id);
9021 const struct got_error *
9022 got_worktree_stage(struct got_worktree *worktree,
9023 struct got_pathlist_head *paths,
9024 got_worktree_status_cb status_cb, void *status_arg,
9025 got_worktree_patch_cb patch_cb, void *patch_arg,
9026 int allow_bad_symlinks, struct got_repository *repo)
9028 const struct got_error *err = NULL, *sync_err, *unlockerr;
9029 struct got_pathlist_entry *pe;
9030 struct got_fileindex *fileindex = NULL;
9031 char *fileindex_path = NULL;
9032 struct got_reference *head_ref = NULL;
9033 struct got_object_id *head_commit_id = NULL;
9034 struct check_stage_ok_arg oka;
9035 struct stage_path_arg spa;
9037 err = lock_worktree(worktree, LOCK_EX);
9041 err = got_ref_open(&head_ref, repo,
9042 got_worktree_get_head_ref_name(worktree), 0);
9045 err = got_ref_resolve(&head_commit_id, repo, head_ref);
9048 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9052 /* Check pre-conditions before staging anything. */
9053 oka.head_commit_id = head_commit_id;
9054 oka.worktree = worktree;
9055 oka.fileindex = fileindex;
9057 oka.have_changes = 0;
9058 TAILQ_FOREACH(pe, paths, entry) {
9059 err = worktree_status(worktree, pe->path, fileindex, repo,
9060 check_stage_ok, &oka, NULL, NULL, 1, 0);
9064 if (!oka.have_changes) {
9065 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9069 spa.worktree = worktree;
9070 spa.fileindex = fileindex;
9072 spa.patch_cb = patch_cb;
9073 spa.patch_arg = patch_arg;
9074 spa.status_cb = status_cb;
9075 spa.status_arg = status_arg;
9076 spa.staged_something = 0;
9077 spa.allow_bad_symlinks = allow_bad_symlinks;
9078 TAILQ_FOREACH(pe, paths, entry) {
9079 err = worktree_status(worktree, pe->path, fileindex, repo,
9080 stage_path, &spa, NULL, NULL, 1, 0);
9084 if (!spa.staged_something) {
9085 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9089 sync_err = sync_fileindex(fileindex, fileindex_path);
9090 if (sync_err && err == NULL)
9094 got_ref_close(head_ref);
9095 free(head_commit_id);
9096 free(fileindex_path);
9098 got_fileindex_free(fileindex);
9099 unlockerr = lock_worktree(worktree, LOCK_SH);
9100 if (unlockerr && err == NULL)
9105 struct unstage_path_arg {
9106 struct got_worktree *worktree;
9107 struct got_fileindex *fileindex;
9108 struct got_repository *repo;
9109 got_worktree_checkout_cb progress_cb;
9111 got_worktree_patch_cb patch_cb;
9115 static const struct got_error *
9116 create_unstaged_content(char **path_unstaged_content,
9117 char **path_new_staged_content, struct got_object_id *blob_id,
9118 struct got_object_id *staged_blob_id, const char *relpath,
9119 struct got_repository *repo,
9120 got_worktree_patch_cb patch_cb, void *patch_arg)
9122 const struct got_error *err, *free_err;
9123 struct got_blob_object *blob = NULL, *staged_blob = NULL;
9124 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
9125 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
9126 struct got_diffreg_result *diffreg_result = NULL;
9127 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
9128 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
9129 int fd1 = -1, fd2 = -1;
9131 *path_unstaged_content = NULL;
9132 *path_new_staged_content = NULL;
9134 err = got_object_id_str(&label1, blob_id);
9138 fd1 = got_opentempfd();
9140 err = got_error_from_errno("got_opentempfd");
9143 fd2 = got_opentempfd();
9145 err = got_error_from_errno("got_opentempfd");
9149 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
9153 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
9157 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
9161 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
9166 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
9170 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
9174 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
9175 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
9179 err = got_opentemp_named(path_unstaged_content, &outfile,
9180 "got-unstaged-content", "");
9183 err = got_opentemp_named(path_new_staged_content, &rejectfile,
9184 "got-new-staged-content", "");
9188 if (fseek(f1, 0L, SEEK_SET) == -1) {
9189 err = got_ferror(f1, GOT_ERR_IO);
9192 if (fseek(f2, 0L, SEEK_SET) == -1) {
9193 err = got_ferror(f2, GOT_ERR_IO);
9196 /* Count the number of actual changes in the diff result. */
9197 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9198 struct diff_chunk_context cc = {};
9199 diff_chunk_context_load_change(&cc, &nchunks_used,
9200 diffreg_result->result, n, 0);
9203 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9205 err = apply_or_reject_change(&choice, &nchunks_used,
9206 diffreg_result->result, n, relpath, f1, f2,
9207 &line_cur1, &line_cur2,
9208 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
9211 if (choice == GOT_PATCH_CHOICE_YES)
9214 have_rejected_content = 1;
9215 if (choice == GOT_PATCH_CHOICE_QUIT)
9218 if (have_content || have_rejected_content)
9219 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
9220 outfile, rejectfile);
9223 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9224 err = got_error_from_errno("close");
9226 got_object_blob_close(blob);
9227 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9228 err = got_error_from_errno("close");
9230 got_object_blob_close(staged_blob);
9231 free_err = got_diffreg_result_free(diffreg_result);
9232 if (free_err && err == NULL)
9234 if (f1 && fclose(f1) == EOF && err == NULL)
9235 err = got_error_from_errno2("fclose", path1);
9236 if (f2 && fclose(f2) == EOF && err == NULL)
9237 err = got_error_from_errno2("fclose", path2);
9238 if (outfile && fclose(outfile) == EOF && err == NULL)
9239 err = got_error_from_errno2("fclose", *path_unstaged_content);
9240 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
9241 err = got_error_from_errno2("fclose", *path_new_staged_content);
9242 if (path1 && unlink(path1) == -1 && err == NULL)
9243 err = got_error_from_errno2("unlink", path1);
9244 if (path2 && unlink(path2) == -1 && err == NULL)
9245 err = got_error_from_errno2("unlink", path2);
9246 if (err || !have_content) {
9247 if (*path_unstaged_content &&
9248 unlink(*path_unstaged_content) == -1 && err == NULL)
9249 err = got_error_from_errno2("unlink",
9250 *path_unstaged_content);
9251 free(*path_unstaged_content);
9252 *path_unstaged_content = NULL;
9254 if (err || !have_content || !have_rejected_content) {
9255 if (*path_new_staged_content &&
9256 unlink(*path_new_staged_content) == -1 && err == NULL)
9257 err = got_error_from_errno2("unlink",
9258 *path_new_staged_content);
9259 free(*path_new_staged_content);
9260 *path_new_staged_content = NULL;
9267 static const struct got_error *
9268 unstage_hunks(struct got_object_id *staged_blob_id,
9269 struct got_blob_object *blob_base,
9270 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
9271 const char *ondisk_path, const char *label_orig,
9272 struct got_worktree *worktree, struct got_repository *repo,
9273 got_worktree_patch_cb patch_cb, void *patch_arg,
9274 got_worktree_checkout_cb progress_cb, void *progress_arg)
9276 const struct got_error *err = NULL;
9277 char *path_unstaged_content = NULL;
9278 char *path_new_staged_content = NULL;
9279 char *parent = NULL, *base_path = NULL;
9280 char *blob_base_path = NULL;
9281 struct got_object_id *new_staged_blob_id = NULL;
9282 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
9285 err = create_unstaged_content(&path_unstaged_content,
9286 &path_new_staged_content, blob_id, staged_blob_id,
9287 ie->path, repo, patch_cb, patch_arg);
9291 if (path_unstaged_content == NULL)
9294 if (path_new_staged_content) {
9295 err = got_object_blob_create(&new_staged_blob_id,
9296 path_new_staged_content, repo);
9301 f = fopen(path_unstaged_content, "re");
9303 err = got_error_from_errno2("fopen",
9304 path_unstaged_content);
9307 if (fstat(fileno(f), &sb) == -1) {
9308 err = got_error_from_errno2("fstat", path_unstaged_content);
9311 if (got_fileindex_entry_staged_filetype_get(ie) ==
9312 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
9313 char link_target[PATH_MAX];
9315 r = fread(link_target, 1, sizeof(link_target), f);
9316 if (r == 0 && ferror(f)) {
9317 err = got_error_from_errno("fread");
9320 if (r >= sizeof(link_target)) { /* should not happen */
9321 err = got_error(GOT_ERR_NO_SPACE);
9324 link_target[r] = '\0';
9325 err = merge_symlink(worktree, blob_base,
9326 ondisk_path, ie->path, label_orig, link_target,
9327 worktree->base_commit_id, repo, progress_cb,
9330 int local_changes_subsumed;
9332 err = got_path_dirname(&parent, ondisk_path);
9336 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
9338 err = got_error_from_errno("asprintf");
9343 err = got_opentemp_named(&blob_base_path, &f_base,
9347 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
9353 * In order the run a 3-way merge with a symlink we copy the symlink's
9354 * target path into a temporary file and use that file with diff3.
9356 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9357 err = dump_symlink_target_path_to_file(&f_deriv2,
9363 fd = open(ondisk_path,
9364 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
9366 err = got_error_from_errno2("open", ondisk_path);
9369 f_deriv2 = fdopen(fd, "r");
9370 if (f_deriv2 == NULL) {
9371 err = got_error_from_errno2("fdopen", ondisk_path);
9377 err = merge_file(&local_changes_subsumed, worktree,
9378 f_base, f, f_deriv2, ondisk_path, ie->path,
9379 got_fileindex_perms_to_st(ie),
9380 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
9381 repo, progress_cb, progress_arg);
9386 if (new_staged_blob_id) {
9387 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9388 SHA1_DIGEST_LENGTH);
9390 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9391 got_fileindex_entry_staged_filetype_set(ie, 0);
9394 free(new_staged_blob_id);
9395 if (path_unstaged_content &&
9396 unlink(path_unstaged_content) == -1 && err == NULL)
9397 err = got_error_from_errno2("unlink", path_unstaged_content);
9398 if (path_new_staged_content &&
9399 unlink(path_new_staged_content) == -1 && err == NULL)
9400 err = got_error_from_errno2("unlink", path_new_staged_content);
9401 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
9402 err = got_error_from_errno2("unlink", blob_base_path);
9403 if (f_base && fclose(f_base) == EOF && err == NULL)
9404 err = got_error_from_errno2("fclose", path_unstaged_content);
9405 if (f && fclose(f) == EOF && err == NULL)
9406 err = got_error_from_errno2("fclose", path_unstaged_content);
9407 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
9408 err = got_error_from_errno2("fclose", ondisk_path);
9409 free(path_unstaged_content);
9410 free(path_new_staged_content);
9411 free(blob_base_path);
9417 static const struct got_error *
9418 unstage_path(void *arg, unsigned char status,
9419 unsigned char staged_status, const char *relpath,
9420 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9421 struct got_object_id *commit_id, int dirfd, const char *de_name)
9423 const struct got_error *err = NULL;
9424 struct unstage_path_arg *a = arg;
9425 struct got_fileindex_entry *ie;
9426 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
9427 char *ondisk_path = NULL;
9428 char *id_str = NULL, *label_orig = NULL;
9429 int local_changes_subsumed;
9431 int fd1 = -1, fd2 = -1;
9433 if (staged_status != GOT_STATUS_ADD &&
9434 staged_status != GOT_STATUS_MODIFY &&
9435 staged_status != GOT_STATUS_DELETE)
9438 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9440 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9442 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
9444 return got_error_from_errno("asprintf");
9446 err = got_object_id_str(&id_str,
9447 commit_id ? commit_id : a->worktree->base_commit_id);
9450 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9452 err = got_error_from_errno("asprintf");
9456 fd1 = got_opentempfd();
9458 err = got_error_from_errno("got_opentempfd");
9461 fd2 = got_opentempfd();
9463 err = got_error_from_errno("got_opentempfd");
9467 switch (staged_status) {
9468 case GOT_STATUS_MODIFY:
9469 err = got_object_open_as_blob(&blob_base, a->repo,
9470 blob_id, 8192, fd1);
9474 case GOT_STATUS_ADD:
9476 if (staged_status == GOT_STATUS_ADD) {
9477 int choice = GOT_PATCH_CHOICE_NONE;
9478 err = (*a->patch_cb)(&choice, a->patch_arg,
9479 staged_status, ie->path, NULL, 1, 1);
9482 if (choice != GOT_PATCH_CHOICE_YES)
9485 err = unstage_hunks(staged_blob_id,
9486 blob_base, blob_id, ie, ondisk_path,
9487 label_orig, a->worktree, a->repo,
9488 a->patch_cb, a->patch_arg,
9489 a->progress_cb, a->progress_arg);
9490 break; /* Done with this file. */
9493 err = got_object_open_as_blob(&blob_staged, a->repo,
9494 staged_blob_id, 8192, fd2);
9497 switch (got_fileindex_entry_staged_filetype_get(ie)) {
9498 case GOT_FILEIDX_MODE_BAD_SYMLINK:
9499 case GOT_FILEIDX_MODE_REGULAR_FILE:
9500 err = merge_blob(&local_changes_subsumed, a->worktree,
9501 blob_base, ondisk_path, relpath,
9502 got_fileindex_perms_to_st(ie), label_orig,
9503 blob_staged, commit_id ? commit_id :
9504 a->worktree->base_commit_id, a->repo,
9505 a->progress_cb, a->progress_arg);
9507 case GOT_FILEIDX_MODE_SYMLINK:
9508 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9509 char *staged_target;
9510 err = got_object_blob_read_to_str(
9511 &staged_target, blob_staged);
9514 err = merge_symlink(a->worktree, blob_base,
9515 ondisk_path, relpath, label_orig,
9516 staged_target, commit_id ? commit_id :
9517 a->worktree->base_commit_id,
9518 a->repo, a->progress_cb, a->progress_arg);
9519 free(staged_target);
9521 err = merge_blob(&local_changes_subsumed,
9522 a->worktree, blob_base, ondisk_path,
9523 relpath, got_fileindex_perms_to_st(ie),
9524 label_orig, blob_staged,
9525 commit_id ? commit_id :
9526 a->worktree->base_commit_id, a->repo,
9527 a->progress_cb, a->progress_arg);
9531 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9535 got_fileindex_entry_stage_set(ie,
9536 GOT_FILEIDX_STAGE_NONE);
9537 got_fileindex_entry_staged_filetype_set(ie, 0);
9540 case GOT_STATUS_DELETE:
9542 int choice = GOT_PATCH_CHOICE_NONE;
9543 err = (*a->patch_cb)(&choice, a->patch_arg,
9544 staged_status, ie->path, NULL, 1, 1);
9547 if (choice == GOT_PATCH_CHOICE_NO)
9549 if (choice != GOT_PATCH_CHOICE_YES) {
9550 err = got_error(GOT_ERR_PATCH_CHOICE);
9554 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9555 got_fileindex_entry_staged_filetype_set(ie, 0);
9556 err = get_file_status(&status, &sb, ie, ondisk_path,
9557 dirfd, de_name, a->repo);
9560 err = (*a->progress_cb)(a->progress_arg, status, relpath);
9565 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9566 err = got_error_from_errno("close");
9568 got_object_blob_close(blob_base);
9569 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9570 err = got_error_from_errno("close");
9572 got_object_blob_close(blob_staged);
9578 const struct got_error *
9579 got_worktree_unstage(struct got_worktree *worktree,
9580 struct got_pathlist_head *paths,
9581 got_worktree_checkout_cb progress_cb, void *progress_arg,
9582 got_worktree_patch_cb patch_cb, void *patch_arg,
9583 struct got_repository *repo)
9585 const struct got_error *err = NULL, *sync_err, *unlockerr;
9586 struct got_pathlist_entry *pe;
9587 struct got_fileindex *fileindex = NULL;
9588 char *fileindex_path = NULL;
9589 struct unstage_path_arg upa;
9591 err = lock_worktree(worktree, LOCK_EX);
9595 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9599 upa.worktree = worktree;
9600 upa.fileindex = fileindex;
9602 upa.progress_cb = progress_cb;
9603 upa.progress_arg = progress_arg;
9604 upa.patch_cb = patch_cb;
9605 upa.patch_arg = patch_arg;
9606 TAILQ_FOREACH(pe, paths, entry) {
9607 err = worktree_status(worktree, pe->path, fileindex, repo,
9608 unstage_path, &upa, NULL, NULL, 1, 0);
9613 sync_err = sync_fileindex(fileindex, fileindex_path);
9614 if (sync_err && err == NULL)
9617 free(fileindex_path);
9619 got_fileindex_free(fileindex);
9620 unlockerr = lock_worktree(worktree, LOCK_SH);
9621 if (unlockerr && err == NULL)
9626 struct report_file_info_arg {
9627 struct got_worktree *worktree;
9628 got_worktree_path_info_cb info_cb;
9630 struct got_pathlist_head *paths;
9631 got_cancel_cb cancel_cb;
9635 static const struct got_error *
9636 report_file_info(void *arg, struct got_fileindex_entry *ie)
9638 struct report_file_info_arg *a = arg;
9639 struct got_pathlist_entry *pe;
9640 struct got_object_id blob_id, staged_blob_id, commit_id;
9641 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9642 struct got_object_id *commit_idp = NULL;
9645 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9646 return got_error(GOT_ERR_CANCELLED);
9648 TAILQ_FOREACH(pe, a->paths, entry) {
9649 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9650 got_path_is_child(ie->path, pe->path, pe->path_len))
9653 if (pe == NULL) /* not found */
9656 if (got_fileindex_entry_has_blob(ie))
9657 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9658 stage = got_fileindex_entry_stage_get(ie);
9659 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9660 stage == GOT_FILEIDX_STAGE_ADD) {
9661 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9662 &staged_blob_id, ie);
9665 if (got_fileindex_entry_has_commit(ie))
9666 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9668 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9669 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9672 const struct got_error *
9673 got_worktree_path_info(struct got_worktree *worktree,
9674 struct got_pathlist_head *paths,
9675 got_worktree_path_info_cb info_cb, void *info_arg,
9676 got_cancel_cb cancel_cb, void *cancel_arg)
9679 const struct got_error *err = NULL, *unlockerr;
9680 struct got_fileindex *fileindex = NULL;
9681 char *fileindex_path = NULL;
9682 struct report_file_info_arg arg;
9684 err = lock_worktree(worktree, LOCK_SH);
9688 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9692 arg.worktree = worktree;
9693 arg.info_cb = info_cb;
9694 arg.info_arg = info_arg;
9696 arg.cancel_cb = cancel_cb;
9697 arg.cancel_arg = cancel_arg;
9698 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9701 free(fileindex_path);
9703 got_fileindex_free(fileindex);
9704 unlockerr = lock_worktree(worktree, LOCK_UN);
9705 if (unlockerr && err == NULL)
9710 static const struct got_error *
9711 patch_check_path(const char *p, char **path, unsigned char *status,
9712 unsigned char *staged_status, struct got_fileindex *fileindex,
9713 struct got_worktree *worktree, struct got_repository *repo)
9715 const struct got_error *err;
9716 struct got_fileindex_entry *ie;
9718 char *ondisk_path = NULL;
9720 err = got_worktree_resolve_path(path, worktree, p);
9724 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9725 *path[0] ? "/" : "", *path) == -1)
9726 return got_error_from_errno("asprintf");
9728 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9730 *staged_status = get_staged_status(ie);
9731 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9736 *staged_status = GOT_STATUS_NO_CHANGE;
9737 *status = GOT_STATUS_UNVERSIONED;
9738 if (lstat(ondisk_path, &sb) == -1) {
9739 if (errno != ENOENT) {
9740 err = got_error_from_errno2("lstat",
9744 *status = GOT_STATUS_NONEXISTENT;
9753 static const struct got_error *
9754 patch_can_rm(const char *path, unsigned char status,
9755 unsigned char staged_status)
9757 if (status == GOT_STATUS_NONEXISTENT)
9758 return got_error_set_errno(ENOENT, path);
9759 if (status != GOT_STATUS_NO_CHANGE &&
9760 status != GOT_STATUS_ADD &&
9761 status != GOT_STATUS_MODIFY &&
9762 status != GOT_STATUS_MODE_CHANGE)
9763 return got_error_path(path, GOT_ERR_FILE_STATUS);
9764 if (staged_status == GOT_STATUS_DELETE)
9765 return got_error_path(path, GOT_ERR_FILE_STATUS);
9769 static const struct got_error *
9770 patch_can_add(const char *path, unsigned char status)
9772 if (status != GOT_STATUS_NONEXISTENT)
9773 return got_error_path(path, GOT_ERR_FILE_STATUS);
9777 static const struct got_error *
9778 patch_can_edit(const char *path, unsigned char status,
9779 unsigned char staged_status)
9781 if (status == GOT_STATUS_NONEXISTENT)
9782 return got_error_set_errno(ENOENT, path);
9783 if (status != GOT_STATUS_NO_CHANGE &&
9784 status != GOT_STATUS_ADD &&
9785 status != GOT_STATUS_MODIFY)
9786 return got_error_path(path, GOT_ERR_FILE_STATUS);
9787 if (staged_status == GOT_STATUS_DELETE)
9788 return got_error_path(path, GOT_ERR_FILE_STATUS);
9792 const struct got_error *
9793 got_worktree_patch_prepare(struct got_fileindex **fileindex,
9794 char **fileindex_path, struct got_worktree *worktree)
9796 return open_fileindex(fileindex, fileindex_path, worktree);
9799 const struct got_error *
9800 got_worktree_patch_check_path(const char *old, const char *new,
9801 char **oldpath, char **newpath, struct got_worktree *worktree,
9802 struct got_repository *repo, struct got_fileindex *fileindex)
9804 const struct got_error *err = NULL;
9805 int file_renamed = 0;
9806 unsigned char status_old, staged_status_old;
9807 unsigned char status_new, staged_status_new;
9812 err = patch_check_path(old != NULL ? old : new, oldpath,
9813 &status_old, &staged_status_old, fileindex, worktree, repo);
9817 err = patch_check_path(new != NULL ? new : old, newpath,
9818 &status_new, &staged_status_new, fileindex, worktree, repo);
9822 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9825 if (old != NULL && new == NULL)
9826 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9827 else if (file_renamed) {
9828 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9830 err = patch_can_add(*newpath, status_new);
9831 } else if (old == NULL)
9832 err = patch_can_add(*newpath, status_new);
9834 err = patch_can_edit(*newpath, status_new, staged_status_new);
9846 const struct got_error *
9847 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9848 struct got_worktree *worktree, struct got_fileindex *fileindex,
9849 got_worktree_checkout_cb progress_cb, void *progress_arg)
9851 struct schedule_addition_args saa;
9853 memset(&saa, 0, sizeof(saa));
9854 saa.worktree = worktree;
9855 saa.fileindex = fileindex;
9856 saa.progress_cb = progress_cb;
9857 saa.progress_arg = progress_arg;
9860 return worktree_status(worktree, path, fileindex, repo,
9861 schedule_addition, &saa, NULL, NULL, 1, 0);
9864 const struct got_error *
9865 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9866 struct got_worktree *worktree, struct got_fileindex *fileindex,
9867 got_worktree_delete_cb progress_cb, void *progress_arg)
9869 const struct got_error *err;
9870 struct schedule_deletion_args sda;
9871 char *ondisk_status_path;
9873 memset(&sda, 0, sizeof(sda));
9874 sda.worktree = worktree;
9875 sda.fileindex = fileindex;
9876 sda.progress_cb = progress_cb;
9877 sda.progress_arg = progress_arg;
9879 sda.delete_local_mods = 0;
9880 sda.keep_on_disk = 0;
9881 sda.ignore_missing_paths = 0;
9882 sda.status_codes = NULL;
9883 if (asprintf(&ondisk_status_path, "%s/%s",
9884 got_worktree_get_root_path(worktree), path) == -1)
9885 return got_error_from_errno("asprintf");
9886 sda.status_path = ondisk_status_path;
9887 sda.status_path_len = strlen(ondisk_status_path);
9889 err = worktree_status(worktree, path, fileindex, repo,
9890 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9891 free(ondisk_status_path);
9895 const struct got_error *
9896 got_worktree_patch_complete(struct got_fileindex *fileindex,
9897 const char *fileindex_path)
9899 const struct got_error *err = NULL;
9901 err = sync_fileindex(fileindex, fileindex_path);
9902 got_fileindex_free(fileindex);