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 err = sync_timestamps(worktree->root_fd,
2001 path, status, ie, &sb);
2004 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2010 fd1 = got_opentempfd();
2012 err = got_error_from_errno("got_opentempfd");
2015 err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
2019 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
2020 int update_timestamps;
2021 struct got_blob_object *blob2 = NULL;
2022 char *label_orig = NULL;
2023 if (got_fileindex_entry_has_blob(ie)) {
2024 fd2 = got_opentempfd();
2026 err = got_error_from_errno("got_opentempfd");
2029 struct got_object_id id2;
2030 got_fileindex_entry_get_blob_id(&id2, ie);
2031 err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
2036 if (got_fileindex_entry_has_commit(ie)) {
2037 char id_str[SHA1_DIGEST_STRING_LENGTH];
2038 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2039 sizeof(id_str)) == NULL) {
2040 err = got_error_path(id_str,
2041 GOT_ERR_BAD_OBJ_ID_STR);
2044 if (asprintf(&label_orig, "%s: commit %s",
2045 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2046 err = got_error_from_errno("asprintf");
2050 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2052 err = got_object_blob_read_to_str(&link_target, blob);
2055 err = merge_symlink(worktree, blob2, ondisk_path, path,
2056 label_orig, link_target, worktree->base_commit_id,
2057 repo, progress_cb, progress_arg);
2060 err = merge_blob(&update_timestamps, worktree, blob2,
2061 ondisk_path, path, sb.st_mode, label_orig, blob,
2062 worktree->base_commit_id, repo,
2063 progress_cb, progress_arg);
2066 if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2067 err = got_error_from_errno("close");
2071 got_object_blob_close(blob2);
2075 * Do not update timestamps of files with local changes.
2076 * Otherwise, a future status walk would treat them as
2077 * unmodified files again.
2079 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2080 blob->id.sha1, worktree->base_commit_id->sha1,
2082 } else if (status == GOT_STATUS_MODE_CHANGE) {
2083 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2084 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2085 } else if (status == GOT_STATUS_DELETE) {
2086 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2089 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2090 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2094 int is_bad_symlink = 0;
2095 if (S_ISLNK(te->mode)) {
2096 err = install_symlink(&is_bad_symlink, worktree,
2097 ondisk_path, path, blob,
2098 status == GOT_STATUS_MISSING, 0,
2099 status == GOT_STATUS_UNVERSIONED, 0,
2100 repo, progress_cb, progress_arg);
2102 err = install_blob(worktree, ondisk_path, path,
2103 te->mode, sb.st_mode, blob,
2104 status == GOT_STATUS_MISSING, 0, 0,
2105 status == GOT_STATUS_UNVERSIONED, repo,
2106 progress_cb, progress_arg);
2112 err = got_fileindex_entry_update(ie,
2113 worktree->root_fd, path, blob->id.sha1,
2114 worktree->base_commit_id->sha1, 1);
2116 err = create_fileindex_entry(&ie, fileindex,
2117 worktree->base_commit_id, worktree->root_fd, path,
2123 if (is_bad_symlink) {
2124 got_fileindex_entry_filetype_set(ie,
2125 GOT_FILEIDX_MODE_BAD_SYMLINK);
2129 if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2130 err = got_error_from_errno("close");
2133 got_object_blob_close(blob);
2139 static const struct got_error *
2140 remove_ondisk_file(const char *root_path, const char *path)
2142 const struct got_error *err = NULL;
2143 char *ondisk_path = NULL, *parent = NULL;
2145 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2146 return got_error_from_errno("asprintf");
2148 if (unlink(ondisk_path) == -1) {
2149 if (errno != ENOENT)
2150 err = got_error_from_errno2("unlink", ondisk_path);
2152 size_t root_len = strlen(root_path);
2153 err = got_path_dirname(&parent, ondisk_path);
2156 while (got_path_cmp(parent, root_path,
2157 strlen(parent), root_len) != 0) {
2159 ondisk_path = parent;
2161 if (rmdir(ondisk_path) == -1) {
2162 if (errno != ENOTEMPTY)
2163 err = got_error_from_errno2("rmdir",
2167 err = got_path_dirname(&parent, ondisk_path);
2178 static const struct got_error *
2179 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2180 struct got_fileindex_entry *ie, struct got_repository *repo,
2181 got_worktree_checkout_cb progress_cb, void *progress_arg)
2183 const struct got_error *err = NULL;
2184 unsigned char status;
2188 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2189 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2191 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2193 return got_error_from_errno("asprintf");
2195 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2199 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2200 char ondisk_target[PATH_MAX];
2201 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2202 sizeof(ondisk_target));
2203 if (ondisk_len == -1) {
2204 err = got_error_from_errno2("readlink", ondisk_path);
2207 ondisk_target[ondisk_len] = '\0';
2208 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2209 NULL, NULL, /* XXX pass common ancestor info? */
2210 ondisk_target, ondisk_path);
2213 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2218 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2219 status == GOT_STATUS_ADD) {
2220 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2224 * Preserve the working file and change the deleted blob's
2225 * entry into a schedule-add entry.
2227 err = got_fileindex_entry_update(ie, worktree->root_fd,
2228 ie->path, NULL, NULL, 0);
2230 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2233 if (status == GOT_STATUS_NO_CHANGE) {
2234 err = remove_ondisk_file(worktree->root_path, ie->path);
2238 got_fileindex_entry_remove(fileindex, ie);
2245 struct diff_cb_arg {
2246 struct got_fileindex *fileindex;
2247 struct got_worktree *worktree;
2248 struct got_repository *repo;
2249 got_worktree_checkout_cb progress_cb;
2251 got_cancel_cb cancel_cb;
2255 static const struct got_error *
2256 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2257 struct got_tree_entry *te, const char *parent_path)
2259 struct diff_cb_arg *a = arg;
2261 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2262 return got_error(GOT_ERR_CANCELLED);
2264 return update_blob(a->worktree, a->fileindex, ie, te,
2265 ie->path, a->repo, a->progress_cb, a->progress_arg);
2268 static const struct got_error *
2269 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2271 struct diff_cb_arg *a = arg;
2273 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2274 return got_error(GOT_ERR_CANCELLED);
2276 return delete_blob(a->worktree, a->fileindex, ie,
2277 a->repo, a->progress_cb, a->progress_arg);
2280 static const struct got_error *
2281 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2283 struct diff_cb_arg *a = arg;
2284 const struct got_error *err;
2287 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2288 return got_error(GOT_ERR_CANCELLED);
2290 if (got_object_tree_entry_is_submodule(te))
2293 if (asprintf(&path, "%s%s%s", parent_path,
2294 parent_path[0] ? "/" : "", te->name)
2296 return got_error_from_errno("asprintf");
2298 if (S_ISDIR(te->mode))
2299 err = add_dir_on_disk(a->worktree, path);
2301 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2302 a->repo, a->progress_cb, a->progress_arg);
2308 const struct got_error *
2309 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2311 uint32_t uuid_status;
2313 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2314 if (uuid_status != uuid_s_ok) {
2316 return got_error_uuid(uuid_status, "uuid_to_string");
2322 static const struct got_error *
2323 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2325 const struct got_error *err = NULL;
2326 char *uuidstr = NULL;
2330 err = got_worktree_get_uuid(&uuidstr, worktree);
2334 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2335 err = got_error_from_errno("asprintf");
2342 const struct got_error *
2343 got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2346 return get_ref_name(refname, worktree, prefix);
2349 const struct got_error *
2350 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2352 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2355 static const struct got_error *
2356 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2358 return get_ref_name(refname, worktree,
2359 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2362 static const struct got_error *
2363 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2365 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2368 static const struct got_error *
2369 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2371 return get_ref_name(refname, worktree,
2372 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2375 static const struct got_error *
2376 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2378 return get_ref_name(refname, worktree,
2379 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2382 static const struct got_error *
2383 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2385 return get_ref_name(refname, worktree,
2386 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2389 static const struct got_error *
2390 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2392 return get_ref_name(refname, worktree,
2393 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2396 static const struct got_error *
2397 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2399 return get_ref_name(refname, worktree,
2400 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2403 static const struct got_error *
2404 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2406 return get_ref_name(refname, worktree,
2407 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2410 const struct got_error *
2411 got_worktree_get_histedit_script_path(char **path,
2412 struct got_worktree *worktree)
2414 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2415 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2417 return got_error_from_errno("asprintf");
2422 static const struct got_error *
2423 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2425 return get_ref_name(refname, worktree,
2426 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2429 static const struct got_error *
2430 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2432 return get_ref_name(refname, worktree,
2433 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2437 * Prevent Git's garbage collector from deleting our base commit by
2438 * setting a reference to our base commit's ID.
2440 static const struct got_error *
2441 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2443 const struct got_error *err = NULL;
2444 struct got_reference *ref = NULL;
2447 err = got_worktree_get_base_ref_name(&refname, worktree);
2451 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2455 err = got_ref_write(ref, repo);
2463 static const struct got_error *
2464 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2466 const struct got_error *err = NULL;
2468 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2469 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2470 err = got_error_from_errno("asprintf");
2471 *fileindex_path = NULL;
2477 static const struct got_error *
2478 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2479 struct got_worktree *worktree)
2481 const struct got_error *err = NULL;
2484 *fileindex_path = NULL;
2485 *fileindex = got_fileindex_alloc();
2486 if (*fileindex == NULL)
2487 return got_error_from_errno("got_fileindex_alloc");
2489 err = get_fileindex_path(fileindex_path, worktree);
2493 index = fopen(*fileindex_path, "rbe");
2494 if (index == NULL) {
2495 if (errno != ENOENT)
2496 err = got_error_from_errno2("fopen", *fileindex_path);
2498 err = got_fileindex_read(*fileindex, index);
2499 if (fclose(index) == EOF && err == NULL)
2500 err = got_error_from_errno("fclose");
2504 free(*fileindex_path);
2505 *fileindex_path = NULL;
2506 got_fileindex_free(*fileindex);
2512 struct bump_base_commit_id_arg {
2513 struct got_object_id *base_commit_id;
2516 const char *entry_name;
2517 got_worktree_checkout_cb progress_cb;
2521 static const struct got_error *
2522 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2524 const struct got_error *err;
2525 struct bump_base_commit_id_arg *a = arg;
2527 if (a->entry_name) {
2528 if (strcmp(ie->path, a->path) != 0)
2530 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2533 if (got_fileindex_entry_was_skipped(ie))
2536 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2537 SHA1_DIGEST_LENGTH) == 0)
2540 if (a->progress_cb) {
2541 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2546 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2550 /* Bump base commit ID of all files within an updated part of the work tree. */
2551 static const struct got_error *
2552 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2553 struct got_fileindex *fileindex,
2554 got_worktree_checkout_cb progress_cb, void *progress_arg)
2556 struct bump_base_commit_id_arg bbc_arg;
2558 bbc_arg.base_commit_id = worktree->base_commit_id;
2559 bbc_arg.entry_name = NULL;
2561 bbc_arg.path_len = 0;
2562 bbc_arg.progress_cb = progress_cb;
2563 bbc_arg.progress_arg = progress_arg;
2565 return got_fileindex_for_each_entry_safe(fileindex,
2566 bump_base_commit_id, &bbc_arg);
2569 static const struct got_error *
2570 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2572 const struct got_error *err = NULL;
2573 char *new_fileindex_path = NULL;
2574 FILE *new_index = NULL;
2575 struct timespec timeout;
2577 err = got_opentemp_named(&new_fileindex_path, &new_index,
2578 fileindex_path, "");
2582 err = got_fileindex_write(fileindex, new_index);
2586 if (rename(new_fileindex_path, fileindex_path) != 0) {
2587 err = got_error_from_errno3("rename", new_fileindex_path,
2589 unlink(new_fileindex_path);
2593 * Sleep for a short amount of time to ensure that files modified after
2594 * this program exits have a different time stamp from the one which
2595 * was recorded in the file index.
2598 timeout.tv_nsec = 1;
2599 nanosleep(&timeout, NULL);
2603 free(new_fileindex_path);
2607 static const struct got_error *
2608 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2609 struct got_object_id **tree_id, const char *wt_relpath,
2610 struct got_commit_object *base_commit, struct got_worktree *worktree,
2611 struct got_repository *repo)
2613 const struct got_error *err = NULL;
2614 struct got_object_id *id = NULL;
2615 char *in_repo_path = NULL;
2616 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2618 *entry_type = GOT_OBJ_TYPE_ANY;
2619 *tree_relpath = NULL;
2622 if (wt_relpath[0] == '\0') {
2623 /* Check out all files within the work tree. */
2624 *entry_type = GOT_OBJ_TYPE_TREE;
2625 *tree_relpath = strdup("");
2626 if (*tree_relpath == NULL) {
2627 err = got_error_from_errno("strdup");
2630 err = got_object_id_by_path(tree_id, repo, base_commit,
2631 worktree->path_prefix);
2637 /* Check out a subset of files in the work tree. */
2639 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2640 is_root_wt ? "" : "/", wt_relpath) == -1) {
2641 err = got_error_from_errno("asprintf");
2645 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2650 in_repo_path = NULL;
2652 err = got_object_get_type(entry_type, repo, id);
2656 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2657 /* Check out a single file. */
2658 if (strchr(wt_relpath, '/') == NULL) {
2659 /* Check out a single file in work tree's root dir. */
2660 in_repo_path = strdup(worktree->path_prefix);
2661 if (in_repo_path == NULL) {
2662 err = got_error_from_errno("strdup");
2665 *tree_relpath = strdup("");
2666 if (*tree_relpath == NULL) {
2667 err = got_error_from_errno("strdup");
2671 /* Check out a single file in a subdirectory. */
2672 err = got_path_dirname(tree_relpath, wt_relpath);
2675 if (asprintf(&in_repo_path, "%s%s%s",
2676 worktree->path_prefix, is_root_wt ? "" : "/",
2677 *tree_relpath) == -1) {
2678 err = got_error_from_errno("asprintf");
2682 err = got_object_id_by_path(tree_id, repo,
2683 base_commit, in_repo_path);
2685 /* Check out all files within a subdirectory. */
2686 *tree_id = got_object_id_dup(id);
2687 if (*tree_id == NULL) {
2688 err = got_error_from_errno("got_object_id_dup");
2691 *tree_relpath = strdup(wt_relpath);
2692 if (*tree_relpath == NULL) {
2693 err = got_error_from_errno("strdup");
2701 *entry_type = GOT_OBJ_TYPE_ANY;
2702 free(*tree_relpath);
2703 *tree_relpath = NULL;
2710 static const struct got_error *
2711 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2712 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2713 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2714 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2716 const struct got_error *err = NULL;
2717 struct got_commit_object *commit = NULL;
2718 struct got_tree_object *tree = NULL;
2719 struct got_fileindex_diff_tree_cb diff_cb;
2720 struct diff_cb_arg arg;
2722 err = ref_base_commit(worktree, repo);
2724 if (!(err->code == GOT_ERR_ERRNO &&
2725 (errno == EACCES || errno == EROFS)))
2727 err = (*progress_cb)(progress_arg,
2728 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2733 err = got_object_open_as_commit(&commit, repo,
2734 worktree->base_commit_id);
2738 err = got_object_open_as_tree(&tree, repo, tree_id);
2743 got_object_tree_find_entry(tree, entry_name) == NULL) {
2744 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2748 diff_cb.diff_old_new = diff_old_new;
2749 diff_cb.diff_old = diff_old;
2750 diff_cb.diff_new = diff_new;
2751 arg.fileindex = fileindex;
2752 arg.worktree = worktree;
2754 arg.progress_cb = progress_cb;
2755 arg.progress_arg = progress_arg;
2756 arg.cancel_cb = cancel_cb;
2757 arg.cancel_arg = cancel_arg;
2758 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2759 entry_name, repo, &diff_cb, &arg);
2762 got_object_tree_close(tree);
2764 got_object_commit_close(commit);
2768 const struct got_error *
2769 got_worktree_checkout_files(struct got_worktree *worktree,
2770 struct got_pathlist_head *paths, struct got_repository *repo,
2771 got_worktree_checkout_cb progress_cb, void *progress_arg,
2772 got_cancel_cb cancel_cb, void *cancel_arg)
2774 const struct got_error *err = NULL, *sync_err, *unlockerr;
2775 struct got_commit_object *commit = NULL;
2776 struct got_tree_object *tree = NULL;
2777 struct got_fileindex *fileindex = NULL;
2778 char *fileindex_path = NULL;
2779 struct got_pathlist_entry *pe;
2780 struct tree_path_data {
2781 STAILQ_ENTRY(tree_path_data) entry;
2782 struct got_object_id *tree_id;
2787 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2789 STAILQ_INIT(&tree_paths);
2791 err = lock_worktree(worktree, LOCK_EX);
2795 err = got_object_open_as_commit(&commit, repo,
2796 worktree->base_commit_id);
2800 /* Map all specified paths to in-repository trees. */
2801 TAILQ_FOREACH(pe, paths, entry) {
2802 tpd = malloc(sizeof(*tpd));
2804 err = got_error_from_errno("malloc");
2808 err = find_tree_entry_for_checkout(&tpd->entry_type,
2809 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2816 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2817 err = got_path_basename(&tpd->entry_name, pe->path);
2825 tpd->entry_name = NULL;
2827 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2831 * Read the file index.
2832 * Checking out files is supposed to be an idempotent operation.
2833 * If the on-disk file index is incomplete we will try to complete it.
2835 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2839 tpd = STAILQ_FIRST(&tree_paths);
2840 TAILQ_FOREACH(pe, paths, entry) {
2841 struct bump_base_commit_id_arg bbc_arg;
2843 err = checkout_files(worktree, fileindex, tpd->relpath,
2844 tpd->tree_id, tpd->entry_name, repo,
2845 progress_cb, progress_arg, cancel_cb, cancel_arg);
2849 bbc_arg.base_commit_id = worktree->base_commit_id;
2850 bbc_arg.entry_name = tpd->entry_name;
2851 bbc_arg.path = pe->path;
2852 bbc_arg.path_len = pe->path_len;
2853 bbc_arg.progress_cb = progress_cb;
2854 bbc_arg.progress_arg = progress_arg;
2855 err = got_fileindex_for_each_entry_safe(fileindex,
2856 bump_base_commit_id, &bbc_arg);
2860 tpd = STAILQ_NEXT(tpd, entry);
2862 sync_err = sync_fileindex(fileindex, fileindex_path);
2863 if (sync_err && err == NULL)
2866 free(fileindex_path);
2868 got_object_tree_close(tree);
2870 got_object_commit_close(commit);
2872 got_fileindex_free(fileindex);
2873 while (!STAILQ_EMPTY(&tree_paths)) {
2874 tpd = STAILQ_FIRST(&tree_paths);
2875 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2880 unlockerr = lock_worktree(worktree, LOCK_SH);
2881 if (unlockerr && err == NULL)
2886 static const struct got_error *
2887 add_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2888 struct got_fileindex_entry *ie, const char *ondisk_path,
2889 const char *path2, struct got_blob_object *blob2, mode_t mode2,
2890 int restoring_missing_file, int reverting_versioned_file,
2891 int path_is_unversioned, int allow_bad_symlinks,
2892 struct got_repository *repo,
2893 got_worktree_checkout_cb progress_cb, void *progress_arg)
2895 const struct got_error *err = NULL;
2896 int is_bad_symlink = 0;
2898 if (S_ISLNK(mode2)) {
2899 err = install_symlink(&is_bad_symlink,
2900 worktree, ondisk_path, path2, blob2,
2901 restoring_missing_file,
2902 reverting_versioned_file,
2903 path_is_unversioned, allow_bad_symlinks,
2904 repo, progress_cb, progress_arg);
2906 err = install_blob(worktree, ondisk_path, path2,
2907 mode2, GOT_DEFAULT_FILE_MODE, blob2,
2908 restoring_missing_file, reverting_versioned_file, 0,
2909 path_is_unversioned, repo, progress_cb, progress_arg);
2914 /* Adding an unversioned file. */
2915 err = got_fileindex_entry_alloc(&ie, path2);
2918 err = got_fileindex_entry_update(ie,
2919 worktree->root_fd, path2, NULL, NULL, 1);
2921 got_fileindex_entry_free(ie);
2924 err = got_fileindex_entry_add(fileindex, ie);
2926 got_fileindex_entry_free(ie);
2930 /* Re-adding a locally deleted file. */
2931 err = got_fileindex_entry_update(ie,
2932 worktree->root_fd, path2, ie->blob_sha1,
2933 worktree->base_commit_id->sha1, 0);
2938 if (is_bad_symlink) {
2939 got_fileindex_entry_filetype_set(ie,
2940 GOT_FILEIDX_MODE_BAD_SYMLINK);
2946 struct merge_file_cb_arg {
2947 struct got_worktree *worktree;
2948 struct got_fileindex *fileindex;
2949 got_worktree_checkout_cb progress_cb;
2951 got_cancel_cb cancel_cb;
2953 const char *label_orig;
2954 struct got_object_id *commit_id2;
2955 int allow_bad_symlinks;
2958 static const struct got_error *
2959 merge_file_cb(void *arg, struct got_blob_object *blob1,
2960 struct got_blob_object *blob2, FILE *f1, FILE *f2,
2961 struct got_object_id *id1, struct got_object_id *id2,
2962 const char *path1, const char *path2,
2963 mode_t mode1, mode_t mode2, struct got_repository *repo)
2965 static const struct got_error *err = NULL;
2966 struct merge_file_cb_arg *a = arg;
2967 struct got_fileindex_entry *ie;
2968 char *ondisk_path = NULL;
2970 unsigned char status;
2971 int local_changes_subsumed;
2972 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2973 char *id_str = NULL, *label_deriv2 = NULL;
2975 if (blob1 && blob2) {
2976 ie = got_fileindex_entry_get(a->fileindex, path2,
2979 return (*a->progress_cb)(a->progress_arg,
2980 GOT_STATUS_MISSING, path2);
2982 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2984 return got_error_from_errno("asprintf");
2986 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2991 if (status == GOT_STATUS_DELETE) {
2992 err = (*a->progress_cb)(a->progress_arg,
2993 GOT_STATUS_MERGE, path2);
2996 if (status != GOT_STATUS_NO_CHANGE &&
2997 status != GOT_STATUS_MODIFY &&
2998 status != GOT_STATUS_CONFLICT &&
2999 status != GOT_STATUS_ADD) {
3000 err = (*a->progress_cb)(a->progress_arg, status, path2);
3004 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
3006 err = got_object_blob_read_to_str(&link_target2, blob2);
3009 err = merge_symlink(a->worktree, blob1, ondisk_path,
3010 path2, a->label_orig, link_target2, a->commit_id2,
3011 repo, a->progress_cb, a->progress_arg);
3016 f_orig = got_opentemp();
3017 if (f_orig == NULL) {
3018 err = got_error_from_errno("got_opentemp");
3021 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3026 f_deriv2 = got_opentemp();
3027 if (f_deriv2 == NULL)
3029 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3034 fd = open(ondisk_path,
3035 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3037 err = got_error_from_errno2("open",
3041 f_deriv = fdopen(fd, "r");
3042 if (f_deriv == NULL) {
3043 err = got_error_from_errno2("fdopen",
3048 err = got_object_id_str(&id_str, a->commit_id2);
3051 if (asprintf(&label_deriv2, "%s: commit %s",
3052 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
3053 err = got_error_from_errno("asprintf");
3056 err = merge_file(&local_changes_subsumed, a->worktree,
3057 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
3058 mode2, a->label_orig, NULL, label_deriv2,
3059 GOT_DIFF_ALGORITHM_PATIENCE, repo,
3060 a->progress_cb, a->progress_arg);
3063 ie = got_fileindex_entry_get(a->fileindex, path1,
3066 return (*a->progress_cb)(a->progress_arg,
3067 GOT_STATUS_MISSING, path1);
3069 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3071 return got_error_from_errno("asprintf");
3073 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3079 case GOT_STATUS_NO_CHANGE:
3080 err = (*a->progress_cb)(a->progress_arg,
3081 GOT_STATUS_DELETE, path1);
3084 err = remove_ondisk_file(a->worktree->root_path, path1);
3088 got_fileindex_entry_mark_deleted_from_disk(ie);
3090 case GOT_STATUS_DELETE:
3091 case GOT_STATUS_MISSING:
3092 err = (*a->progress_cb)(a->progress_arg,
3093 GOT_STATUS_DELETE, path1);
3097 got_fileindex_entry_mark_deleted_from_disk(ie);
3099 case GOT_STATUS_ADD: {
3100 struct got_object_id *id;
3104 * Delete the added file only if its content already
3105 * exists in the repository.
3107 err = got_object_blob_file_create(&id, &blob1_f,
3108 &blob1_size, path1);
3111 if (got_object_id_cmp(id, id1) == 0) {
3112 err = (*a->progress_cb)(a->progress_arg,
3113 GOT_STATUS_DELETE, path1);
3116 err = remove_ondisk_file(a->worktree->root_path,
3121 got_fileindex_entry_remove(a->fileindex,
3124 err = (*a->progress_cb)(a->progress_arg,
3125 GOT_STATUS_CANNOT_DELETE, path1);
3127 if (fclose(blob1_f) == EOF && err == NULL)
3128 err = got_error_from_errno("fclose");
3134 case GOT_STATUS_MODIFY:
3135 case GOT_STATUS_CONFLICT:
3136 err = (*a->progress_cb)(a->progress_arg,
3137 GOT_STATUS_CANNOT_DELETE, path1);
3141 case GOT_STATUS_OBSTRUCTED:
3142 err = (*a->progress_cb)(a->progress_arg, status, path1);
3150 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3152 return got_error_from_errno("asprintf");
3153 ie = got_fileindex_entry_get(a->fileindex, path2,
3156 err = get_file_status(&status, &sb, ie, ondisk_path,
3160 if (status != GOT_STATUS_NO_CHANGE &&
3161 status != GOT_STATUS_MODIFY &&
3162 status != GOT_STATUS_CONFLICT &&
3163 status != GOT_STATUS_ADD &&
3164 status != GOT_STATUS_DELETE) {
3165 err = (*a->progress_cb)(a->progress_arg,
3169 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3171 err = got_object_blob_read_to_str(&link_target2,
3175 err = merge_symlink(a->worktree, NULL,
3176 ondisk_path, path2, a->label_orig,
3177 link_target2, a->commit_id2, repo,
3178 a->progress_cb, a->progress_arg);
3180 } else if (S_ISREG(sb.st_mode)) {
3181 err = merge_blob(&local_changes_subsumed,
3182 a->worktree, NULL, ondisk_path, path2,
3183 sb.st_mode, a->label_orig, blob2,
3184 a->commit_id2, repo, a->progress_cb,
3186 } else if (status != GOT_STATUS_DELETE) {
3187 err = got_error_path(ondisk_path,
3188 GOT_ERR_FILE_OBSTRUCTED);
3192 if (status == GOT_STATUS_DELETE) {
3193 /* Re-add file with content from new blob. */
3194 err = add_file(a->worktree, a->fileindex, ie,
3195 ondisk_path, path2, blob2, mode2,
3196 0, 0, 0, a->allow_bad_symlinks,
3197 repo, a->progress_cb, a->progress_arg);
3202 err = add_file(a->worktree, a->fileindex, NULL,
3203 ondisk_path, path2, blob2, mode2,
3204 0, 0, 1, a->allow_bad_symlinks,
3205 repo, a->progress_cb, a->progress_arg);
3211 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3212 err = got_error_from_errno("fclose");
3213 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3214 err = got_error_from_errno("fclose");
3215 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3216 err = got_error_from_errno("fclose");
3223 static const struct got_error *
3224 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3226 struct got_worktree *worktree = arg;
3228 /* Reject merges into a work tree with mixed base commits. */
3229 if (got_fileindex_entry_has_commit(ie) &&
3230 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3231 SHA1_DIGEST_LENGTH) != 0)
3232 return got_error(GOT_ERR_MIXED_COMMITS);
3237 struct check_merge_conflicts_arg {
3238 struct got_worktree *worktree;
3239 struct got_fileindex *fileindex;
3240 struct got_repository *repo;
3243 static const struct got_error *
3244 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3245 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3246 struct got_object_id *id1, struct got_object_id *id2,
3247 const char *path1, const char *path2,
3248 mode_t mode1, mode_t mode2, struct got_repository *repo)
3250 const struct got_error *err = NULL;
3251 struct check_merge_conflicts_arg *a = arg;
3252 unsigned char status;
3254 struct got_fileindex_entry *ie;
3255 const char *path = path2 ? path2 : path1;
3256 struct got_object_id *id = id2 ? id2 : id1;
3262 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3266 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3268 return got_error_from_errno("asprintf");
3270 /* Reject merges into a work tree with conflicted files. */
3271 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3275 if (status == GOT_STATUS_CONFLICT)
3276 return got_error(GOT_ERR_CONFLICTS);
3281 static const struct got_error *
3282 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3283 const char *fileindex_path, struct got_object_id *commit_id1,
3284 struct got_object_id *commit_id2, struct got_repository *repo,
3285 got_worktree_checkout_cb progress_cb, void *progress_arg,
3286 got_cancel_cb cancel_cb, void *cancel_arg)
3288 const struct got_error *err = NULL, *sync_err;
3289 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3290 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3291 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3292 struct check_merge_conflicts_arg cmc_arg;
3293 struct merge_file_cb_arg arg;
3294 char *label_orig = NULL;
3295 FILE *f1 = NULL, *f2 = NULL;
3296 int fd1 = -1, fd2 = -1;
3299 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3302 err = got_object_id_by_path(&tree_id1, repo, commit1,
3303 worktree->path_prefix);
3304 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3310 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3314 err = got_object_id_str(&id_str, commit_id1);
3318 if (asprintf(&label_orig, "%s: commit %s",
3319 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3320 err = got_error_from_errno("asprintf");
3326 f1 = got_opentemp();
3328 err = got_error_from_errno("got_opentemp");
3333 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3337 err = got_object_id_by_path(&tree_id2, repo, commit2,
3338 worktree->path_prefix);
3342 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3346 f2 = got_opentemp();
3348 err = got_error_from_errno("got_opentemp");
3352 fd1 = got_opentempfd();
3354 err = got_error_from_errno("got_opentempfd");
3358 fd2 = got_opentempfd();
3360 err = got_error_from_errno("got_opentempfd");
3364 cmc_arg.worktree = worktree;
3365 cmc_arg.fileindex = fileindex;
3366 cmc_arg.repo = repo;
3367 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3368 check_merge_conflicts, &cmc_arg, 0);
3372 arg.worktree = worktree;
3373 arg.fileindex = fileindex;
3374 arg.progress_cb = progress_cb;
3375 arg.progress_arg = progress_arg;
3376 arg.cancel_cb = cancel_cb;
3377 arg.cancel_arg = cancel_arg;
3378 arg.label_orig = label_orig;
3379 arg.commit_id2 = commit_id2;
3380 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3381 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3382 merge_file_cb, &arg, 1);
3383 sync_err = sync_fileindex(fileindex, fileindex_path);
3384 if (sync_err && err == NULL)
3388 got_object_commit_close(commit1);
3390 got_object_commit_close(commit2);
3392 got_object_tree_close(tree1);
3394 got_object_tree_close(tree2);
3395 if (f1 && fclose(f1) == EOF && err == NULL)
3396 err = got_error_from_errno("fclose");
3397 if (f2 && fclose(f2) == EOF && err == NULL)
3398 err = got_error_from_errno("fclose");
3399 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3400 err = got_error_from_errno("close");
3401 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3402 err = got_error_from_errno("close");
3407 const struct got_error *
3408 got_worktree_merge_files(struct got_worktree *worktree,
3409 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3410 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3411 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3413 const struct got_error *err, *unlockerr;
3414 char *fileindex_path = NULL;
3415 struct got_fileindex *fileindex = NULL;
3417 err = lock_worktree(worktree, LOCK_EX);
3421 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3425 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3430 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3431 commit_id2, repo, progress_cb, progress_arg,
3432 cancel_cb, cancel_arg);
3435 got_fileindex_free(fileindex);
3436 free(fileindex_path);
3437 unlockerr = lock_worktree(worktree, LOCK_SH);
3438 if (unlockerr && err == NULL)
3443 struct diff_dir_cb_arg {
3444 struct got_fileindex *fileindex;
3445 struct got_worktree *worktree;
3446 const char *status_path;
3447 size_t status_path_len;
3448 struct got_repository *repo;
3449 got_worktree_status_cb status_cb;
3451 got_cancel_cb cancel_cb;
3453 /* A pathlist containing per-directory pathlists of ignore patterns. */
3454 struct got_pathlist_head *ignores;
3455 int report_unchanged;
3459 static const struct got_error *
3460 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3461 int dirfd, const char *de_name,
3462 got_worktree_status_cb status_cb, void *status_arg,
3463 struct got_repository *repo, int report_unchanged)
3465 const struct got_error *err = NULL;
3466 unsigned char status = GOT_STATUS_NO_CHANGE;
3467 unsigned char staged_status;
3469 struct got_object_id blob_id, commit_id, staged_blob_id;
3470 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3471 struct got_object_id *staged_blob_idp = NULL;
3473 staged_status = get_staged_status(ie);
3474 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3478 if (status == GOT_STATUS_NO_CHANGE &&
3479 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3482 if (got_fileindex_entry_has_blob(ie))
3483 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3484 if (got_fileindex_entry_has_commit(ie))
3485 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3486 if (staged_status == GOT_STATUS_ADD ||
3487 staged_status == GOT_STATUS_MODIFY) {
3488 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3489 &staged_blob_id, ie);
3492 return (*status_cb)(status_arg, status, staged_status,
3493 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3496 static const struct got_error *
3497 status_old_new(void *arg, struct got_fileindex_entry *ie,
3498 struct dirent *de, const char *parent_path, int dirfd)
3500 const struct got_error *err = NULL;
3501 struct diff_dir_cb_arg *a = arg;
3504 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3505 return got_error(GOT_ERR_CANCELLED);
3507 if (got_path_cmp(parent_path, a->status_path,
3508 strlen(parent_path), a->status_path_len) != 0 &&
3509 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3512 if (parent_path[0]) {
3513 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3514 parent_path, de->d_name) == -1)
3515 return got_error_from_errno("asprintf");
3517 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3519 return got_error_from_errno("asprintf");
3522 err = report_file_status(ie, abspath, dirfd, de->d_name,
3523 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3528 static const struct got_error *
3529 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3531 struct diff_dir_cb_arg *a = arg;
3532 struct got_object_id blob_id, commit_id;
3533 unsigned char status;
3535 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3536 return got_error(GOT_ERR_CANCELLED);
3538 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3541 got_fileindex_entry_get_blob_id(&blob_id, ie);
3542 got_fileindex_entry_get_commit_id(&commit_id, ie);
3543 if (got_fileindex_entry_has_file_on_disk(ie))
3544 status = GOT_STATUS_MISSING;
3546 status = GOT_STATUS_DELETE;
3547 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3548 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3552 free_ignores(struct got_pathlist_head *ignores)
3554 struct got_pathlist_entry *pe;
3556 TAILQ_FOREACH(pe, ignores, entry) {
3557 struct got_pathlist_head *ignorelist = pe->data;
3559 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3561 got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3564 static const struct got_error *
3565 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3567 const struct got_error *err = NULL;
3568 struct got_pathlist_entry *pe = NULL;
3569 struct got_pathlist_head *ignorelist;
3570 char *line = NULL, *pattern, *dirpath = NULL;
3571 size_t linesize = 0;
3574 ignorelist = calloc(1, sizeof(*ignorelist));
3575 if (ignorelist == NULL)
3576 return got_error_from_errno("calloc");
3577 TAILQ_INIT(ignorelist);
3579 while ((linelen = getline(&line, &linesize, f)) != -1) {
3580 if (linelen > 0 && line[linelen - 1] == '\n')
3581 line[linelen - 1] = '\0';
3583 /* Git's ignores may contain comments. */
3587 /* Git's negated patterns are not (yet?) supported. */
3591 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3593 err = got_error_from_errno("asprintf");
3596 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3601 err = got_error_from_errno("getline");
3605 dirpath = strdup(path);
3606 if (dirpath == NULL) {
3607 err = got_error_from_errno("strdup");
3610 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3613 if (err || pe == NULL) {
3615 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3621 match_path(const char *pattern, size_t pattern_len, const char *path,
3627 * Trailing slashes signify directories.
3628 * Append a * to make such patterns conform to fnmatch rules.
3630 if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
3631 if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
3632 return FNM_NOMATCH; /* XXX */
3634 return fnmatch(buf, path, flags);
3637 return fnmatch(pattern, path, flags);
3641 match_ignores(struct got_pathlist_head *ignores, const char *path)
3643 struct got_pathlist_entry *pe;
3645 /* Handle patterns which match in all directories. */
3646 TAILQ_FOREACH(pe, ignores, entry) {
3647 struct got_pathlist_head *ignorelist = pe->data;
3648 struct got_pathlist_entry *pi;
3650 TAILQ_FOREACH(pi, ignorelist, entry) {
3653 if (pi->path_len < 3 ||
3654 strncmp(pi->path, "**/", 3) != 0)
3658 if (match_path(pi->path + 3,
3659 pi->path_len - 3, p,
3660 FNM_PATHNAME | FNM_LEADING_DIR)) {
3661 /* Retry in next directory. */
3662 while (*p && *p != '/')
3674 * The ignores pathlist contains ignore lists from children before
3675 * parents, so we can find the most specific ignorelist by walking
3676 * ignores backwards.
3678 pe = TAILQ_LAST(ignores, got_pathlist_head);
3680 if (got_path_is_child(path, pe->path, pe->path_len)) {
3681 struct got_pathlist_head *ignorelist = pe->data;
3682 struct got_pathlist_entry *pi;
3683 TAILQ_FOREACH(pi, ignorelist, entry) {
3684 int flags = FNM_LEADING_DIR;
3685 if (strstr(pi->path, "/**/") == NULL)
3686 flags |= FNM_PATHNAME;
3687 if (match_path(pi->path, pi->path_len,
3693 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3699 static const struct got_error *
3700 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3701 const char *path, int dirfd, const char *ignores_filename)
3703 const struct got_error *err = NULL;
3706 FILE *ignoresfile = NULL;
3708 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3709 path[0] ? "/" : "", ignores_filename) == -1)
3710 return got_error_from_errno("asprintf");
3713 fd = openat(dirfd, ignores_filename,
3714 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3716 if (errno != ENOENT && errno != EACCES)
3717 err = got_error_from_errno2("openat",
3720 ignoresfile = fdopen(fd, "r");
3721 if (ignoresfile == NULL)
3722 err = got_error_from_errno2("fdopen",
3726 err = read_ignores(ignores, path, ignoresfile);
3730 ignoresfile = fopen(ignorespath, "re");
3731 if (ignoresfile == NULL) {
3732 if (errno != ENOENT && errno != EACCES)
3733 err = got_error_from_errno2("fopen",
3736 err = read_ignores(ignores, path, ignoresfile);
3739 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3740 err = got_error_from_errno2("fclose", path);
3741 if (fd != -1 && close(fd) == -1 && err == NULL)
3742 err = got_error_from_errno2("close", path);
3747 static const struct got_error *
3748 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3751 const struct got_error *err = NULL;
3752 struct diff_dir_cb_arg *a = arg;
3758 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3759 return got_error(GOT_ERR_CANCELLED);
3761 if (parent_path[0]) {
3762 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3763 return got_error_from_errno("asprintf");
3768 if (de->d_type == DT_DIR) {
3769 if (!a->no_ignores && ignore != NULL &&
3770 match_ignores(a->ignores, path))
3772 } else if (!match_ignores(a->ignores, path) &&
3773 got_path_is_child(path, a->status_path, a->status_path_len))
3774 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3775 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3781 static const struct got_error *
3782 status_traverse(void *arg, const char *path, int dirfd)
3784 const struct got_error *err = NULL;
3785 struct diff_dir_cb_arg *a = arg;
3790 err = add_ignores(a->ignores, a->worktree->root_path,
3791 path, dirfd, ".cvsignore");
3795 err = add_ignores(a->ignores, a->worktree->root_path, path,
3796 dirfd, ".gitignore");
3801 static const struct got_error *
3802 report_single_file_status(const char *path, const char *ondisk_path,
3803 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3804 void *status_arg, struct got_repository *repo, int report_unchanged,
3805 struct got_pathlist_head *ignores, int no_ignores)
3807 struct got_fileindex_entry *ie;
3810 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3812 return report_file_status(ie, ondisk_path, -1, NULL,
3813 status_cb, status_arg, repo, report_unchanged);
3815 if (lstat(ondisk_path, &sb) == -1) {
3816 if (errno != ENOENT)
3817 return got_error_from_errno2("lstat", ondisk_path);
3818 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3819 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3822 if (!no_ignores && match_ignores(ignores, path))
3825 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3826 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3827 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3832 static const struct got_error *
3833 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3834 const char *root_path, const char *path)
3836 const struct got_error *err;
3837 char *parent_path, *next_parent_path = NULL;
3839 err = add_ignores(ignores, root_path, "", -1,
3844 err = add_ignores(ignores, root_path, "", -1,
3849 err = got_path_dirname(&parent_path, path);
3851 if (err->code == GOT_ERR_BAD_PATH)
3852 return NULL; /* cannot traverse parent */
3856 err = add_ignores(ignores, root_path, parent_path, -1,
3860 err = add_ignores(ignores, root_path, parent_path, -1,
3864 err = got_path_dirname(&next_parent_path, parent_path);
3866 if (err->code == GOT_ERR_BAD_PATH)
3867 err = NULL; /* traversed everything */
3870 if (got_path_is_root_dir(parent_path))
3873 parent_path = next_parent_path;
3874 next_parent_path = NULL;
3878 free(next_parent_path);
3882 static const struct got_error *
3883 worktree_status(struct got_worktree *worktree, const char *path,
3884 struct got_fileindex *fileindex, struct got_repository *repo,
3885 got_worktree_status_cb status_cb, void *status_arg,
3886 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3887 int report_unchanged)
3889 const struct got_error *err = NULL;
3891 struct got_fileindex_diff_dir_cb fdiff_cb;
3892 struct diff_dir_cb_arg arg;
3893 char *ondisk_path = NULL;
3894 struct got_pathlist_head ignores;
3895 struct got_fileindex_entry *ie;
3897 TAILQ_INIT(&ignores);
3899 if (asprintf(&ondisk_path, "%s%s%s",
3900 worktree->root_path, path[0] ? "/" : "", path) == -1)
3901 return got_error_from_errno("asprintf");
3903 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3905 err = report_single_file_status(path, ondisk_path,
3906 fileindex, status_cb, status_arg, repo,
3907 report_unchanged, &ignores, no_ignores);
3911 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3913 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3914 !got_err_open_nofollow_on_symlink())
3915 err = got_error_from_errno2("open", ondisk_path);
3918 err = add_ignores_from_parent_paths(&ignores,
3919 worktree->root_path, ondisk_path);
3923 err = report_single_file_status(path, ondisk_path,
3924 fileindex, status_cb, status_arg, repo,
3925 report_unchanged, &ignores, no_ignores);
3928 fdiff_cb.diff_old_new = status_old_new;
3929 fdiff_cb.diff_old = status_old;
3930 fdiff_cb.diff_new = status_new;
3931 fdiff_cb.diff_traverse = status_traverse;
3932 arg.fileindex = fileindex;
3933 arg.worktree = worktree;
3934 arg.status_path = path;
3935 arg.status_path_len = strlen(path);
3937 arg.status_cb = status_cb;
3938 arg.status_arg = status_arg;
3939 arg.cancel_cb = cancel_cb;
3940 arg.cancel_arg = cancel_arg;
3941 arg.report_unchanged = report_unchanged;
3942 arg.no_ignores = no_ignores;
3944 err = add_ignores_from_parent_paths(&ignores,
3945 worktree->root_path, path);
3949 arg.ignores = &ignores;
3950 err = got_fileindex_diff_dir(fileindex, fd,
3951 worktree->root_path, path, repo, &fdiff_cb, &arg);
3954 free_ignores(&ignores);
3955 if (fd != -1 && close(fd) == -1 && err == NULL)
3956 err = got_error_from_errno("close");
3961 const struct got_error *
3962 got_worktree_status(struct got_worktree *worktree,
3963 struct got_pathlist_head *paths, struct got_repository *repo,
3964 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3965 got_cancel_cb cancel_cb, void *cancel_arg)
3967 const struct got_error *err = NULL;
3968 char *fileindex_path = NULL;
3969 struct got_fileindex *fileindex = NULL;
3970 struct got_pathlist_entry *pe;
3972 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3976 TAILQ_FOREACH(pe, paths, entry) {
3977 err = worktree_status(worktree, pe->path, fileindex, repo,
3978 status_cb, status_arg, cancel_cb, cancel_arg,
3983 free(fileindex_path);
3984 got_fileindex_free(fileindex);
3988 const struct got_error *
3989 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3992 const struct got_error *err = NULL;
3993 char *resolved = NULL, *cwd = NULL, *path = NULL;
3996 char *abspath = NULL;
3997 char canonpath[PATH_MAX];
4001 cwd = getcwd(NULL, 0);
4003 return got_error_from_errno("getcwd");
4005 if (lstat(arg, &sb) == -1) {
4006 if (errno != ENOENT) {
4007 err = got_error_from_errno2("lstat", arg);
4012 if (S_ISLNK(sb.st_mode)) {
4014 * We cannot use realpath(3) with symlinks since we want to
4015 * operate on the symlink itself.
4016 * But we can make the path absolute, assuming it is relative
4017 * to the current working directory, and then canonicalize it.
4019 if (!got_path_is_absolute(arg)) {
4020 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4021 err = got_error_from_errno("asprintf");
4026 err = got_canonpath(abspath ? abspath : arg, canonpath,
4030 resolved = strdup(canonpath);
4031 if (resolved == NULL) {
4032 err = got_error_from_errno("strdup");
4036 resolved = realpath(arg, NULL);
4037 if (resolved == NULL) {
4038 if (errno != ENOENT) {
4039 err = got_error_from_errno2("realpath", arg);
4042 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4043 err = got_error_from_errno("asprintf");
4046 err = got_canonpath(abspath, canonpath,
4050 resolved = strdup(canonpath);
4051 if (resolved == NULL) {
4052 err = got_error_from_errno("strdup");
4058 if (strncmp(got_worktree_get_root_path(worktree), resolved,
4059 strlen(got_worktree_get_root_path(worktree)))) {
4060 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
4064 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
4065 err = got_path_skip_common_ancestor(&path,
4066 got_worktree_get_root_path(worktree), resolved);
4072 err = got_error_from_errno("strdup");
4077 /* XXX status walk can't deal with trailing slash! */
4079 while (len > 0 && path[len - 1] == '/') {
4080 path[len - 1] = '\0';
4094 struct schedule_addition_args {
4095 struct got_worktree *worktree;
4096 struct got_fileindex *fileindex;
4097 got_worktree_checkout_cb progress_cb;
4099 struct got_repository *repo;
4102 static const struct got_error *
4103 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4104 const char *relpath, struct got_object_id *blob_id,
4105 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4106 int dirfd, const char *de_name)
4108 struct schedule_addition_args *a = arg;
4109 const struct got_error *err = NULL;
4110 struct got_fileindex_entry *ie;
4114 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4116 return got_error_from_errno("asprintf");
4118 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4120 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4124 /* Re-adding an existing entry is a no-op. */
4125 if (status == GOT_STATUS_ADD)
4127 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4132 if (status != GOT_STATUS_UNVERSIONED) {
4133 if (status == GOT_STATUS_NONEXISTENT)
4134 err = got_error_set_errno(ENOENT, ondisk_path);
4136 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4140 err = got_fileindex_entry_alloc(&ie, relpath);
4143 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4144 relpath, NULL, NULL, 1);
4146 got_fileindex_entry_free(ie);
4149 err = got_fileindex_entry_add(a->fileindex, ie);
4151 got_fileindex_entry_free(ie);
4158 if (status == GOT_STATUS_ADD)
4160 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4163 const struct got_error *
4164 got_worktree_schedule_add(struct got_worktree *worktree,
4165 struct got_pathlist_head *paths,
4166 got_worktree_checkout_cb progress_cb, void *progress_arg,
4167 struct got_repository *repo, int no_ignores)
4169 struct got_fileindex *fileindex = NULL;
4170 char *fileindex_path = NULL;
4171 const struct got_error *err = NULL, *sync_err, *unlockerr;
4172 struct got_pathlist_entry *pe;
4173 struct schedule_addition_args saa;
4175 err = lock_worktree(worktree, LOCK_EX);
4179 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4183 saa.worktree = worktree;
4184 saa.fileindex = fileindex;
4185 saa.progress_cb = progress_cb;
4186 saa.progress_arg = progress_arg;
4189 TAILQ_FOREACH(pe, paths, entry) {
4190 err = worktree_status(worktree, pe->path, fileindex, repo,
4191 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4195 sync_err = sync_fileindex(fileindex, fileindex_path);
4196 if (sync_err && err == NULL)
4199 free(fileindex_path);
4201 got_fileindex_free(fileindex);
4202 unlockerr = lock_worktree(worktree, LOCK_SH);
4203 if (unlockerr && err == NULL)
4208 struct schedule_deletion_args {
4209 struct got_worktree *worktree;
4210 struct got_fileindex *fileindex;
4211 got_worktree_delete_cb progress_cb;
4213 struct got_repository *repo;
4214 int delete_local_mods;
4216 int ignore_missing_paths;
4217 const char *status_codes;
4220 static const struct got_error *
4221 schedule_for_deletion(void *arg, unsigned char status,
4222 unsigned char staged_status, const char *relpath,
4223 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4224 struct got_object_id *commit_id, int dirfd, const char *de_name)
4226 struct schedule_deletion_args *a = arg;
4227 const struct got_error *err = NULL;
4228 struct got_fileindex_entry *ie = NULL;
4232 if (status == GOT_STATUS_NONEXISTENT) {
4233 if (a->ignore_missing_paths)
4235 return got_error_set_errno(ENOENT, relpath);
4238 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4240 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4242 staged_status = get_staged_status(ie);
4243 if (staged_status != GOT_STATUS_NO_CHANGE) {
4244 if (staged_status == GOT_STATUS_DELETE)
4246 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4249 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4251 return got_error_from_errno("asprintf");
4253 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4258 if (a->status_codes) {
4259 size_t ncodes = strlen(a->status_codes);
4261 for (i = 0; i < ncodes ; i++) {
4262 if (status == a->status_codes[i])
4266 /* Do not delete files in non-matching status. */
4270 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4271 a->status_codes[i] != GOT_STATUS_MISSING) {
4272 static char msg[64];
4273 snprintf(msg, sizeof(msg),
4274 "invalid status code '%c'", a->status_codes[i]);
4275 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4280 if (status != GOT_STATUS_NO_CHANGE) {
4281 if (status == GOT_STATUS_DELETE)
4283 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4284 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4287 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4288 err = got_error_set_errno(ENOENT, relpath);
4291 if (status != GOT_STATUS_MODIFY &&
4292 status != GOT_STATUS_MISSING) {
4293 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4298 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4302 if (unlinkat(dirfd, de_name, 0) == -1) {
4303 err = got_error_from_errno2("unlinkat",
4307 } else if (unlink(ondisk_path) == -1) {
4308 err = got_error_from_errno2("unlink", ondisk_path);
4312 root_len = strlen(a->worktree->root_path);
4315 err = got_path_dirname(&parent, ondisk_path);
4319 ondisk_path = parent;
4320 if (rmdir(ondisk_path) == -1) {
4321 if (errno != ENOTEMPTY)
4322 err = got_error_from_errno2("rmdir",
4326 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4327 strlen(ondisk_path), root_len) != 0);
4330 got_fileindex_entry_mark_deleted_from_disk(ie);
4335 if (status == GOT_STATUS_DELETE)
4337 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4338 staged_status, relpath);
4341 const struct got_error *
4342 got_worktree_schedule_delete(struct got_worktree *worktree,
4343 struct got_pathlist_head *paths, int delete_local_mods,
4344 const char *status_codes,
4345 got_worktree_delete_cb progress_cb, void *progress_arg,
4346 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4348 struct got_fileindex *fileindex = NULL;
4349 char *fileindex_path = NULL;
4350 const struct got_error *err = NULL, *sync_err, *unlockerr;
4351 struct got_pathlist_entry *pe;
4352 struct schedule_deletion_args sda;
4354 err = lock_worktree(worktree, LOCK_EX);
4358 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4362 sda.worktree = worktree;
4363 sda.fileindex = fileindex;
4364 sda.progress_cb = progress_cb;
4365 sda.progress_arg = progress_arg;
4367 sda.delete_local_mods = delete_local_mods;
4368 sda.keep_on_disk = keep_on_disk;
4369 sda.ignore_missing_paths = ignore_missing_paths;
4370 sda.status_codes = status_codes;
4372 TAILQ_FOREACH(pe, paths, entry) {
4373 err = worktree_status(worktree, pe->path, fileindex, repo,
4374 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4378 sync_err = sync_fileindex(fileindex, fileindex_path);
4379 if (sync_err && err == NULL)
4382 free(fileindex_path);
4384 got_fileindex_free(fileindex);
4385 unlockerr = lock_worktree(worktree, LOCK_SH);
4386 if (unlockerr && err == NULL)
4391 static const struct got_error *
4392 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4394 const struct got_error *err = NULL;
4396 size_t linesize = 0, n;
4399 linelen = getline(&line, &linesize, infile);
4400 if (linelen == -1) {
4401 if (ferror(infile)) {
4402 err = got_error_from_errno("getline");
4408 n = fwrite(line, 1, linelen, outfile);
4410 err = got_ferror(outfile, GOT_ERR_IO);
4415 n = fwrite(line, 1, linelen, rejectfile);
4417 err = got_ferror(rejectfile, GOT_ERR_IO);
4424 static const struct got_error *
4425 skip_one_line(FILE *f)
4428 size_t linesize = 0;
4431 linelen = getline(&line, &linesize, f);
4432 if (linelen == -1) {
4434 return got_error_from_errno("getline");
4441 static const struct got_error *
4442 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4443 int start_old, int end_old, int start_new, int end_new,
4444 FILE *outfile, FILE *rejectfile)
4446 const struct got_error *err;
4448 /* Copy old file's lines leading up to patch. */
4449 while (!feof(f1) && *line_cur1 < start_old) {
4450 err = copy_one_line(f1, outfile, NULL);
4455 /* Skip new file's lines leading up to patch. */
4456 while (!feof(f2) && *line_cur2 < start_new) {
4458 err = copy_one_line(f2, NULL, rejectfile);
4460 err = skip_one_line(f2);
4465 /* Copy patched lines. */
4466 while (!feof(f2) && *line_cur2 <= end_new) {
4467 err = copy_one_line(f2, outfile, NULL);
4472 /* Skip over old file's replaced lines. */
4473 while (!feof(f1) && *line_cur1 <= end_old) {
4475 err = copy_one_line(f1, NULL, rejectfile);
4477 err = skip_one_line(f1);
4486 static const struct got_error *
4487 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4488 FILE *outfile, FILE *rejectfile)
4490 const struct got_error *err;
4493 /* Copy old file's lines until EOF. */
4495 err = copy_one_line(f1, outfile, NULL);
4502 /* Copy new file's lines until EOF. */
4504 err = copy_one_line(f2, NULL, rejectfile);
4514 static const struct got_error *
4515 apply_or_reject_change(int *choice, int *nchunks_used,
4516 struct diff_result *diff_result, int n,
4517 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4518 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4519 got_worktree_patch_cb patch_cb, void *patch_arg)
4521 const struct got_error *err = NULL;
4522 struct diff_chunk_context cc = {};
4523 int start_old, end_old, start_new, end_new;
4525 struct diff_output_unidiff_state *diff_state;
4526 struct diff_input_info diff_info;
4529 *choice = GOT_PATCH_CHOICE_NONE;
4531 /* Get changed line numbers without context lines for copy_change(). */
4532 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4533 start_old = cc.left.start;
4534 end_old = cc.left.end;
4535 start_new = cc.right.start;
4536 end_new = cc.right.end;
4538 /* Get the same change with context lines for display. */
4539 memset(&cc, 0, sizeof(cc));
4540 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4542 memset(&diff_info, 0, sizeof(diff_info));
4543 diff_info.left_path = relpath;
4544 diff_info.right_path = relpath;
4546 diff_state = diff_output_unidiff_state_alloc();
4547 if (diff_state == NULL)
4548 return got_error_set_errno(ENOMEM,
4549 "diff_output_unidiff_state_alloc");
4551 hunkfile = got_opentemp();
4552 if (hunkfile == NULL) {
4553 err = got_error_from_errno("got_opentemp");
4557 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4559 if (rc != DIFF_RC_OK) {
4560 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4564 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4565 err = got_ferror(hunkfile, GOT_ERR_IO);
4569 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4570 hunkfile, changeno, nchanges);
4575 case GOT_PATCH_CHOICE_YES:
4576 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4577 end_old, start_new, end_new, outfile, rejectfile);
4579 case GOT_PATCH_CHOICE_NO:
4580 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4581 end_old, start_new, end_new, rejectfile, outfile);
4583 case GOT_PATCH_CHOICE_QUIT:
4586 err = got_error(GOT_ERR_PATCH_CHOICE);
4590 diff_output_unidiff_state_free(diff_state);
4591 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4592 err = got_error_from_errno("fclose");
4596 struct revert_file_args {
4597 struct got_worktree *worktree;
4598 struct got_fileindex *fileindex;
4599 got_worktree_checkout_cb progress_cb;
4601 got_worktree_patch_cb patch_cb;
4603 struct got_repository *repo;
4604 int unlink_added_files;
4607 static const struct got_error *
4608 create_patched_content(char **path_outfile, int reverse_patch,
4609 struct got_object_id *blob_id, const char *path2,
4610 int dirfd2, const char *de_name2,
4611 const char *relpath, struct got_repository *repo,
4612 got_worktree_patch_cb patch_cb, void *patch_arg)
4614 const struct got_error *err, *free_err;
4615 struct got_blob_object *blob = NULL;
4616 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4617 int fd = -1, fd2 = -1;
4618 char link_target[PATH_MAX];
4619 ssize_t link_len = 0;
4620 char *path1 = NULL, *id_str = NULL;
4622 struct got_diffreg_result *diffreg_result = NULL;
4623 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4624 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4626 *path_outfile = NULL;
4628 err = got_object_id_str(&id_str, blob_id);
4633 fd2 = openat(dirfd2, de_name2,
4634 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4636 if (!got_err_open_nofollow_on_symlink()) {
4637 err = got_error_from_errno2("openat", path2);
4640 link_len = readlinkat(dirfd2, de_name2,
4641 link_target, sizeof(link_target));
4642 if (link_len == -1) {
4643 return got_error_from_errno2("readlinkat",
4646 sb2.st_mode = S_IFLNK;
4647 sb2.st_size = link_len;
4650 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4652 if (!got_err_open_nofollow_on_symlink()) {
4653 err = got_error_from_errno2("open", path2);
4656 link_len = readlink(path2, link_target,
4657 sizeof(link_target));
4659 return got_error_from_errno2("readlink", path2);
4660 sb2.st_mode = S_IFLNK;
4661 sb2.st_size = link_len;
4665 if (fstat(fd2, &sb2) == -1) {
4666 err = got_error_from_errno2("fstat", path2);
4670 f2 = fdopen(fd2, "r");
4672 err = got_error_from_errno2("fdopen", path2);
4678 f2 = got_opentemp();
4680 err = got_error_from_errno2("got_opentemp", path2);
4683 n = fwrite(link_target, 1, link_len, f2);
4684 if (n != link_len) {
4685 err = got_ferror(f2, GOT_ERR_IO);
4688 if (fflush(f2) == EOF) {
4689 err = got_error_from_errno("fflush");
4695 fd = got_opentempfd();
4697 err = got_error_from_errno("got_opentempfd");
4701 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4705 err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4709 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4713 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4714 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4718 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4723 if (fseek(f1, 0L, SEEK_SET) == -1)
4724 return got_ferror(f1, GOT_ERR_IO);
4725 if (fseek(f2, 0L, SEEK_SET) == -1)
4726 return got_ferror(f2, GOT_ERR_IO);
4728 /* Count the number of actual changes in the diff result. */
4729 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4730 struct diff_chunk_context cc = {};
4731 diff_chunk_context_load_change(&cc, &nchunks_used,
4732 diffreg_result->result, n, 0);
4735 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4737 err = apply_or_reject_change(&choice, &nchunks_used,
4738 diffreg_result->result, n, relpath, f1, f2,
4739 &line_cur1, &line_cur2,
4740 reverse_patch ? NULL : outfile,
4741 reverse_patch ? outfile : NULL,
4742 ++i, nchanges, patch_cb, patch_arg);
4745 if (choice == GOT_PATCH_CHOICE_YES)
4747 else if (choice == GOT_PATCH_CHOICE_QUIT)
4751 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4752 reverse_patch ? NULL : outfile,
4753 reverse_patch ? outfile : NULL);
4757 if (!S_ISLNK(sb2.st_mode)) {
4760 mode = apply_umask(sb2.st_mode);
4761 if (fchmod(fileno(outfile), mode) == -1) {
4762 err = got_error_from_errno2("fchmod", path2);
4769 if (fd != -1 && close(fd) == -1 && err == NULL)
4770 err = got_error_from_errno("close");
4772 got_object_blob_close(blob);
4773 free_err = got_diffreg_result_free(diffreg_result);
4776 if (f1 && fclose(f1) == EOF && err == NULL)
4777 err = got_error_from_errno2("fclose", path1);
4778 if (f2 && fclose(f2) == EOF && err == NULL)
4779 err = got_error_from_errno2("fclose", path2);
4780 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4781 err = got_error_from_errno2("close", path2);
4782 if (outfile && fclose(outfile) == EOF && err == NULL)
4783 err = got_error_from_errno2("fclose", *path_outfile);
4784 if (path1 && unlink(path1) == -1 && err == NULL)
4785 err = got_error_from_errno2("unlink", path1);
4786 if (err || !have_content) {
4787 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4788 err = got_error_from_errno2("unlink", *path_outfile);
4789 free(*path_outfile);
4790 *path_outfile = NULL;
4796 static const struct got_error *
4797 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4798 const char *relpath, struct got_object_id *blob_id,
4799 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4800 int dirfd, const char *de_name)
4802 struct revert_file_args *a = arg;
4803 const struct got_error *err = NULL;
4804 char *parent_path = NULL;
4805 struct got_fileindex_entry *ie;
4806 struct got_commit_object *base_commit = NULL;
4807 struct got_tree_object *tree = NULL;
4808 struct got_object_id *tree_id = NULL;
4809 const struct got_tree_entry *te = NULL;
4810 char *tree_path = NULL, *te_name;
4811 char *ondisk_path = NULL, *path_content = NULL;
4812 struct got_blob_object *blob = NULL;
4815 /* Reverting a staged deletion is a no-op. */
4816 if (status == GOT_STATUS_DELETE &&
4817 staged_status != GOT_STATUS_NO_CHANGE)
4820 if (status == GOT_STATUS_UNVERSIONED)
4821 return (*a->progress_cb)(a->progress_arg,
4822 GOT_STATUS_UNVERSIONED, relpath);
4824 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4826 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4828 /* Construct in-repository path of tree which contains this blob. */
4829 err = got_path_dirname(&parent_path, ie->path);
4831 if (err->code != GOT_ERR_BAD_PATH)
4833 parent_path = strdup("/");
4834 if (parent_path == NULL) {
4835 err = got_error_from_errno("strdup");
4839 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4840 tree_path = strdup(parent_path);
4841 if (tree_path == NULL) {
4842 err = got_error_from_errno("strdup");
4846 if (got_path_is_root_dir(parent_path)) {
4847 tree_path = strdup(a->worktree->path_prefix);
4848 if (tree_path == NULL) {
4849 err = got_error_from_errno("strdup");
4853 if (asprintf(&tree_path, "%s/%s",
4854 a->worktree->path_prefix, parent_path) == -1) {
4855 err = got_error_from_errno("asprintf");
4861 err = got_object_open_as_commit(&base_commit, a->repo,
4862 a->worktree->base_commit_id);
4866 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4868 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4869 (status == GOT_STATUS_ADD ||
4870 staged_status == GOT_STATUS_ADD)))
4873 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4877 err = got_path_basename(&te_name, ie->path);
4881 te = got_object_tree_find_entry(tree, te_name);
4883 if (te == NULL && status != GOT_STATUS_ADD &&
4884 staged_status != GOT_STATUS_ADD) {
4885 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4891 case GOT_STATUS_ADD:
4893 int choice = GOT_PATCH_CHOICE_NONE;
4894 err = (*a->patch_cb)(&choice, a->patch_arg,
4895 status, ie->path, NULL, 1, 1);
4898 if (choice != GOT_PATCH_CHOICE_YES)
4901 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4905 got_fileindex_entry_remove(a->fileindex, ie);
4906 if (a->unlink_added_files) {
4907 if (asprintf(&ondisk_path, "%s/%s",
4908 got_worktree_get_root_path(a->worktree),
4910 err = got_error_from_errno("asprintf");
4913 if (unlink(ondisk_path) == -1) {
4914 err = got_error_from_errno2("unlink",
4920 case GOT_STATUS_DELETE:
4922 int choice = GOT_PATCH_CHOICE_NONE;
4923 err = (*a->patch_cb)(&choice, a->patch_arg,
4924 status, ie->path, NULL, 1, 1);
4927 if (choice != GOT_PATCH_CHOICE_YES)
4931 case GOT_STATUS_MODIFY:
4932 case GOT_STATUS_MODE_CHANGE:
4933 case GOT_STATUS_CONFLICT:
4934 case GOT_STATUS_MISSING: {
4935 struct got_object_id id;
4936 if (staged_status == GOT_STATUS_ADD ||
4937 staged_status == GOT_STATUS_MODIFY)
4938 got_fileindex_entry_get_staged_blob_id(&id, ie);
4940 got_fileindex_entry_get_blob_id(&id, ie);
4941 fd = got_opentempfd();
4943 err = got_error_from_errno("got_opentempfd");
4947 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4951 if (asprintf(&ondisk_path, "%s/%s",
4952 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4953 err = got_error_from_errno("asprintf");
4957 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4958 status == GOT_STATUS_CONFLICT)) {
4959 int is_bad_symlink = 0;
4960 err = create_patched_content(&path_content, 1, &id,
4961 ondisk_path, dirfd, de_name, ie->path, a->repo,
4962 a->patch_cb, a->patch_arg);
4963 if (err || path_content == NULL)
4965 if (te && S_ISLNK(te->mode)) {
4966 if (unlink(path_content) == -1) {
4967 err = got_error_from_errno2("unlink",
4971 err = install_symlink(&is_bad_symlink,
4972 a->worktree, ondisk_path, ie->path,
4973 blob, 0, 1, 0, 0, a->repo,
4974 a->progress_cb, a->progress_arg);
4976 if (rename(path_content, ondisk_path) == -1) {
4977 err = got_error_from_errno3("rename",
4978 path_content, ondisk_path);
4983 int is_bad_symlink = 0;
4984 if (te && S_ISLNK(te->mode)) {
4985 err = install_symlink(&is_bad_symlink,
4986 a->worktree, ondisk_path, ie->path,
4987 blob, 0, 1, 0, 0, a->repo,
4988 a->progress_cb, a->progress_arg);
4990 err = install_blob(a->worktree, ondisk_path,
4992 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4993 got_fileindex_perms_to_st(ie), blob,
4994 0, 1, 0, 0, a->repo,
4995 a->progress_cb, a->progress_arg);
4999 if (status == GOT_STATUS_DELETE ||
5000 status == GOT_STATUS_MODE_CHANGE) {
5001 err = got_fileindex_entry_update(ie,
5002 a->worktree->root_fd, relpath,
5004 a->worktree->base_commit_id->sha1, 1);
5008 if (is_bad_symlink) {
5009 got_fileindex_entry_filetype_set(ie,
5010 GOT_FILEIDX_MODE_BAD_SYMLINK);
5023 if (fd != -1 && close(fd) == -1 && err == NULL)
5024 err = got_error_from_errno("close");
5026 got_object_blob_close(blob);
5028 got_object_tree_close(tree);
5031 got_object_commit_close(base_commit);
5035 const struct got_error *
5036 got_worktree_revert(struct got_worktree *worktree,
5037 struct got_pathlist_head *paths,
5038 got_worktree_checkout_cb progress_cb, void *progress_arg,
5039 got_worktree_patch_cb patch_cb, void *patch_arg,
5040 struct got_repository *repo)
5042 struct got_fileindex *fileindex = NULL;
5043 char *fileindex_path = NULL;
5044 const struct got_error *err = NULL, *unlockerr = NULL;
5045 const struct got_error *sync_err = NULL;
5046 struct got_pathlist_entry *pe;
5047 struct revert_file_args rfa;
5049 err = lock_worktree(worktree, LOCK_EX);
5053 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5057 rfa.worktree = worktree;
5058 rfa.fileindex = fileindex;
5059 rfa.progress_cb = progress_cb;
5060 rfa.progress_arg = progress_arg;
5061 rfa.patch_cb = patch_cb;
5062 rfa.patch_arg = patch_arg;
5064 rfa.unlink_added_files = 0;
5065 TAILQ_FOREACH(pe, paths, entry) {
5066 err = worktree_status(worktree, pe->path, fileindex, repo,
5067 revert_file, &rfa, NULL, NULL, 1, 0);
5071 sync_err = sync_fileindex(fileindex, fileindex_path);
5072 if (sync_err && err == NULL)
5075 free(fileindex_path);
5077 got_fileindex_free(fileindex);
5078 unlockerr = lock_worktree(worktree, LOCK_SH);
5079 if (unlockerr && err == NULL)
5085 free_commitable(struct got_commitable *ct)
5088 free(ct->in_repo_path);
5089 free(ct->ondisk_path);
5091 free(ct->base_blob_id);
5092 free(ct->staged_blob_id);
5093 free(ct->base_commit_id);
5097 struct collect_commitables_arg {
5098 struct got_pathlist_head *commitable_paths;
5099 struct got_repository *repo;
5100 struct got_worktree *worktree;
5101 struct got_fileindex *fileindex;
5102 int have_staged_files;
5103 int allow_bad_symlinks;
5104 int diff_header_shown;
5105 int commit_conflicts;
5112 * Create a file which contains the target path of a symlink so we can feed
5113 * it as content to the diff engine.
5115 static const struct got_error *
5116 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5117 const char *abspath)
5119 const struct got_error *err = NULL;
5120 char target_path[PATH_MAX];
5121 ssize_t target_len, outlen;
5126 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5127 if (target_len == -1)
5128 return got_error_from_errno2("readlinkat", abspath);
5130 target_len = readlink(abspath, target_path, PATH_MAX);
5131 if (target_len == -1)
5132 return got_error_from_errno2("readlink", abspath);
5135 *fd = got_opentempfd();
5137 return got_error_from_errno("got_opentempfd");
5139 outlen = write(*fd, target_path, target_len);
5141 err = got_error_from_errno("got_opentempfd");
5145 if (lseek(*fd, 0, SEEK_SET) == -1) {
5146 err = got_error_from_errno2("lseek", abspath);
5157 static const struct got_error *
5158 append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5159 FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5160 int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5162 const struct got_error *err = NULL;
5163 struct got_blob_object *blob1 = NULL;
5164 int fd = -1, fd1 = -1, fd2 = -1;
5165 FILE *ondisk_file = NULL;
5166 char *label1 = NULL;
5170 char *id_str = NULL;
5172 memset(&sb, 0, sizeof(sb));
5175 if (ct->staged_status != GOT_STATUS_MODIFY &&
5176 ct->staged_status != GOT_STATUS_ADD &&
5177 ct->staged_status != GOT_STATUS_DELETE)
5180 if (ct->status != GOT_STATUS_MODIFY &&
5181 ct->status != GOT_STATUS_ADD &&
5182 ct->status != GOT_STATUS_DELETE &&
5183 ct->status != GOT_STATUS_CONFLICT)
5187 err = got_opentemp_truncate(f1);
5189 return got_error_from_errno("got_opentemp_truncate");
5190 err = got_opentemp_truncate(f2);
5192 return got_error_from_errno("got_opentemp_truncate");
5194 if (!*diff_header_shown) {
5195 err = got_object_id_str(&id_str, worktree->base_commit_id);
5198 fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5199 got_worktree_get_root_path(worktree));
5200 fprintf(diff_outfile, "commit - %s\n", id_str);
5201 fprintf(diff_outfile, "path + %s%s\n",
5202 got_worktree_get_root_path(worktree),
5203 diff_staged ? " (staged changes)" : "");
5204 *diff_header_shown = 1;
5208 const char *label1 = NULL, *label2 = NULL;
5209 switch (ct->staged_status) {
5210 case GOT_STATUS_MODIFY:
5214 case GOT_STATUS_ADD:
5217 case GOT_STATUS_DELETE:
5221 return got_error(GOT_ERR_FILE_STATUS);
5223 fd1 = got_opentempfd();
5225 err = got_error_from_errno("got_opentempfd");
5228 fd2 = got_opentempfd();
5230 err = got_error_from_errno("got_opentempfd");
5233 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5234 fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5235 label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5236 NULL, repo, diff_outfile);
5240 fd1 = got_opentempfd();
5242 err = got_error_from_errno("got_opentempfd");
5246 if (ct->status != GOT_STATUS_ADD) {
5247 err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5253 if (ct->status != GOT_STATUS_DELETE) {
5255 fd = openat(dirfd, de_name,
5256 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5258 if (!got_err_open_nofollow_on_symlink()) {
5259 err = got_error_from_errno2("openat",
5263 err = get_symlink_target_file(&fd, dirfd,
5264 de_name, ct->ondisk_path);
5269 fd = open(ct->ondisk_path,
5270 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5272 if (!got_err_open_nofollow_on_symlink()) {
5273 err = got_error_from_errno2("open",
5277 err = get_symlink_target_file(&fd, dirfd,
5278 de_name, ct->ondisk_path);
5283 if (fstatat(fd, ct->ondisk_path, &sb,
5284 AT_SYMLINK_NOFOLLOW) == -1) {
5285 err = got_error_from_errno2("fstatat", ct->ondisk_path);
5288 ondisk_file = fdopen(fd, "r");
5289 if (ondisk_file == NULL) {
5290 err = got_error_from_errno2("fdopen", ct->ondisk_path);
5298 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5304 err = got_diff_blob_file(blob1, f1, size1, label1,
5305 ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5306 GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5308 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5309 err = got_error_from_errno("close");
5310 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5311 err = got_error_from_errno("close");
5313 got_object_blob_close(blob1);
5314 if (fd != -1 && close(fd) == -1 && err == NULL)
5315 err = got_error_from_errno("close");
5316 if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5317 err = got_error_from_errno("fclose");
5321 static const struct got_error *
5322 collect_commitables(void *arg, unsigned char status,
5323 unsigned char staged_status, const char *relpath,
5324 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5325 struct got_object_id *commit_id, int dirfd, const char *de_name)
5327 struct collect_commitables_arg *a = arg;
5328 const struct got_error *err = NULL;
5329 struct got_commitable *ct = NULL;
5330 struct got_pathlist_entry *new = NULL;
5331 char *parent_path = NULL, *path = NULL;
5334 if (a->have_staged_files) {
5335 if (staged_status != GOT_STATUS_MODIFY &&
5336 staged_status != GOT_STATUS_ADD &&
5337 staged_status != GOT_STATUS_DELETE)
5340 if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5341 printf("C %s\n", relpath);
5342 return got_error(GOT_ERR_COMMIT_CONFLICT);
5345 if (status != GOT_STATUS_MODIFY &&
5346 status != GOT_STATUS_MODE_CHANGE &&
5347 status != GOT_STATUS_ADD &&
5348 status != GOT_STATUS_DELETE &&
5349 status != GOT_STATUS_CONFLICT)
5353 if (asprintf(&path, "/%s", relpath) == -1) {
5354 err = got_error_from_errno("asprintf");
5357 if (strcmp(path, "/") == 0) {
5358 parent_path = strdup("");
5359 if (parent_path == NULL)
5360 return got_error_from_errno("strdup");
5362 err = got_path_dirname(&parent_path, path);
5367 ct = calloc(1, sizeof(*ct));
5369 err = got_error_from_errno("calloc");
5373 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5375 err = got_error_from_errno("asprintf");
5379 if (staged_status == GOT_STATUS_ADD ||
5380 staged_status == GOT_STATUS_MODIFY) {
5381 struct got_fileindex_entry *ie;
5382 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5383 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5384 case GOT_FILEIDX_MODE_REGULAR_FILE:
5385 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5388 case GOT_FILEIDX_MODE_SYMLINK:
5392 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5395 ct->mode |= got_fileindex_entry_perms_get(ie);
5396 } else if (status != GOT_STATUS_DELETE &&
5397 staged_status != GOT_STATUS_DELETE) {
5399 if (fstatat(dirfd, de_name, &sb,
5400 AT_SYMLINK_NOFOLLOW) == -1) {
5401 err = got_error_from_errno2("fstatat",
5405 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5406 err = got_error_from_errno2("lstat", ct->ondisk_path);
5409 ct->mode = sb.st_mode;
5412 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5413 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5415 err = got_error_from_errno("asprintf");
5419 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5420 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5422 char target_path[PATH_MAX];
5424 target_len = readlink(ct->ondisk_path, target_path,
5425 sizeof(target_path));
5426 if (target_len == -1) {
5427 err = got_error_from_errno2("readlink",
5431 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5432 target_len, ct->ondisk_path, a->worktree->root_path);
5435 if (is_bad_symlink) {
5436 err = got_error_path(ct->ondisk_path,
5437 GOT_ERR_BAD_SYMLINK);
5443 ct->status = status;
5444 ct->staged_status = staged_status;
5445 ct->blob_id = NULL; /* will be filled in when blob gets created */
5446 if (ct->status != GOT_STATUS_ADD &&
5447 ct->staged_status != GOT_STATUS_ADD) {
5448 ct->base_blob_id = got_object_id_dup(blob_id);
5449 if (ct->base_blob_id == NULL) {
5450 err = got_error_from_errno("got_object_id_dup");
5453 ct->base_commit_id = got_object_id_dup(commit_id);
5454 if (ct->base_commit_id == NULL) {
5455 err = got_error_from_errno("got_object_id_dup");
5459 if (ct->staged_status == GOT_STATUS_ADD ||
5460 ct->staged_status == GOT_STATUS_MODIFY) {
5461 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5462 if (ct->staged_blob_id == NULL) {
5463 err = got_error_from_errno("got_object_id_dup");
5467 ct->path = strdup(path);
5468 if (ct->path == NULL) {
5469 err = got_error_from_errno("strdup");
5472 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5476 if (a->diff_outfile && ct && new != NULL) {
5477 err = append_ct_diff(ct, &a->diff_header_shown,
5478 a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5479 a->have_staged_files, a->repo, a->worktree);
5484 if (ct && (err || new == NULL))
5485 free_commitable(ct);
5491 static const struct got_error *write_tree(struct got_object_id **, int *,
5492 struct got_tree_object *, const char *, struct got_pathlist_head *,
5493 got_worktree_status_cb status_cb, void *status_arg,
5494 struct got_repository *);
5496 static const struct got_error *
5497 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5498 struct got_tree_entry *te, const char *parent_path,
5499 struct got_pathlist_head *commitable_paths,
5500 got_worktree_status_cb status_cb, void *status_arg,
5501 struct got_repository *repo)
5503 const struct got_error *err = NULL;
5504 struct got_tree_object *subtree;
5507 if (asprintf(&subpath, "%s%s%s", parent_path,
5508 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5509 return got_error_from_errno("asprintf");
5511 err = got_object_open_as_tree(&subtree, repo, &te->id);
5515 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5516 commitable_paths, status_cb, status_arg, repo);
5517 got_object_tree_close(subtree);
5522 static const struct got_error *
5523 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5525 const struct got_error *err = NULL;
5526 char *ct_parent_path = NULL;
5530 if (strchr(ct->in_repo_path, '/') == NULL) {
5531 *match = got_path_is_root_dir(path);
5535 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5538 *match = (strcmp(path, ct_parent_path) == 0);
5539 free(ct_parent_path);
5544 get_ct_file_mode(struct got_commitable *ct)
5546 if (S_ISLNK(ct->mode))
5549 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5552 static const struct got_error *
5553 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5554 struct got_tree_entry *te, struct got_commitable *ct)
5556 const struct got_error *err = NULL;
5560 err = got_object_tree_entry_dup(new_te, te);
5564 (*new_te)->mode = get_ct_file_mode(ct);
5566 if (ct->staged_status == GOT_STATUS_MODIFY)
5567 memcpy(&(*new_te)->id, ct->staged_blob_id,
5568 sizeof((*new_te)->id));
5570 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5572 if (err && *new_te) {
5579 static const struct got_error *
5580 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5581 struct got_commitable *ct)
5583 const struct got_error *err = NULL;
5584 char *ct_name = NULL;
5588 *new_te = calloc(1, sizeof(**new_te));
5589 if (*new_te == NULL)
5590 return got_error_from_errno("calloc");
5592 err = got_path_basename(&ct_name, ct->path);
5595 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5596 sizeof((*new_te)->name)) {
5597 err = got_error(GOT_ERR_NO_SPACE);
5601 (*new_te)->mode = get_ct_file_mode(ct);
5603 if (ct->staged_status == GOT_STATUS_ADD)
5604 memcpy(&(*new_te)->id, ct->staged_blob_id,
5605 sizeof((*new_te)->id));
5607 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5610 if (err && *new_te) {
5617 static const struct got_error *
5618 insert_tree_entry(struct got_tree_entry *new_te,
5619 struct got_pathlist_head *paths)
5621 const struct got_error *err = NULL;
5622 struct got_pathlist_entry *new_pe;
5624 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5628 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5632 static const struct got_error *
5633 report_ct_status(struct got_commitable *ct,
5634 got_worktree_status_cb status_cb, void *status_arg)
5636 const char *ct_path = ct->path;
5637 unsigned char status;
5639 if (status_cb == NULL) /* no commit progress output desired */
5642 while (ct_path[0] == '/')
5645 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5646 status = ct->staged_status;
5648 status = ct->status;
5650 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5651 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5654 static const struct got_error *
5655 match_modified_subtree(int *modified, struct got_tree_entry *te,
5656 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5658 const struct got_error *err = NULL;
5659 struct got_pathlist_entry *pe;
5664 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5665 got_path_is_root_dir(base_tree_path) ? "" : "/",
5667 return got_error_from_errno("asprintf");
5669 TAILQ_FOREACH(pe, commitable_paths, entry) {
5670 struct got_commitable *ct = pe->data;
5671 *modified = got_path_is_child(ct->in_repo_path, te_path,
5681 static const struct got_error *
5682 match_deleted_or_modified_ct(struct got_commitable **ctp,
5683 struct got_tree_entry *te, const char *base_tree_path,
5684 struct got_pathlist_head *commitable_paths)
5686 const struct got_error *err = NULL;
5687 struct got_pathlist_entry *pe;
5691 TAILQ_FOREACH(pe, commitable_paths, entry) {
5692 struct got_commitable *ct = pe->data;
5693 char *ct_name = NULL;
5696 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5697 if (ct->status != GOT_STATUS_MODIFY &&
5698 ct->status != GOT_STATUS_MODE_CHANGE &&
5699 ct->status != GOT_STATUS_DELETE &&
5700 ct->status != GOT_STATUS_CONFLICT)
5703 if (ct->staged_status != GOT_STATUS_MODIFY &&
5704 ct->staged_status != GOT_STATUS_DELETE)
5708 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5711 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5717 err = got_path_basename(&ct_name, pe->path);
5721 if (strcmp(te->name, ct_name) != 0) {
5734 static const struct got_error *
5735 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5736 const char *child_path, const char *path_base_tree,
5737 struct got_pathlist_head *commitable_paths,
5738 got_worktree_status_cb status_cb, void *status_arg,
5739 struct got_repository *repo)
5741 const struct got_error *err = NULL;
5742 struct got_tree_entry *new_te;
5744 struct got_object_id *id = NULL;
5749 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5750 got_path_is_root_dir(path_base_tree) ? "" : "/",
5752 return got_error_from_errno("asprintf");
5754 new_te = calloc(1, sizeof(*new_te));
5756 return got_error_from_errno("calloc");
5757 new_te->mode = S_IFDIR;
5759 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5760 sizeof(new_te->name)) {
5761 err = got_error(GOT_ERR_NO_SPACE);
5764 err = write_tree(&id, &nentries, NULL, subtree_path,
5765 commitable_paths, status_cb, status_arg, repo);
5770 memcpy(&new_te->id, id, sizeof(new_te->id));
5779 static const struct got_error *
5780 write_tree(struct got_object_id **new_tree_id, int *nentries,
5781 struct got_tree_object *base_tree, const char *path_base_tree,
5782 struct got_pathlist_head *commitable_paths,
5783 got_worktree_status_cb status_cb, void *status_arg,
5784 struct got_repository *repo)
5786 const struct got_error *err = NULL;
5787 struct got_pathlist_head paths;
5788 struct got_tree_entry *te, *new_te = NULL;
5789 struct got_pathlist_entry *pe;
5794 /* Insert, and recurse into, newly added entries first. */
5795 TAILQ_FOREACH(pe, commitable_paths, entry) {
5796 struct got_commitable *ct = pe->data;
5797 char *child_path = NULL, *slash;
5799 if ((ct->status != GOT_STATUS_ADD &&
5800 ct->staged_status != GOT_STATUS_ADD) ||
5801 (ct->flags & GOT_COMMITABLE_ADDED))
5804 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5805 strlen(path_base_tree)))
5808 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5813 slash = strchr(child_path, '/');
5814 if (slash == NULL) {
5815 err = alloc_added_blob_tree_entry(&new_te, ct);
5818 err = report_ct_status(ct, status_cb, status_arg);
5821 ct->flags |= GOT_COMMITABLE_ADDED;
5822 err = insert_tree_entry(new_te, &paths);
5827 *slash = '\0'; /* trim trailing path components */
5828 if (base_tree == NULL ||
5829 got_object_tree_find_entry(base_tree, child_path)
5831 err = make_subtree_for_added_blob(&new_te,
5832 child_path, path_base_tree,
5833 commitable_paths, status_cb, status_arg,
5837 err = insert_tree_entry(new_te, &paths);
5846 int i, nbase_entries;
5847 /* Handle modified and deleted entries. */
5848 nbase_entries = got_object_tree_get_nentries(base_tree);
5849 for (i = 0; i < nbase_entries; i++) {
5850 struct got_commitable *ct = NULL;
5852 te = got_object_tree_get_entry(base_tree, i);
5853 if (got_object_tree_entry_is_submodule(te)) {
5854 /* Entry is a submodule; just copy it. */
5855 err = got_object_tree_entry_dup(&new_te, te);
5858 err = insert_tree_entry(new_te, &paths);
5865 if (S_ISDIR(te->mode)) {
5867 err = got_object_tree_entry_dup(&new_te, te);
5870 err = match_modified_subtree(&modified, te,
5871 path_base_tree, commitable_paths);
5874 /* Avoid recursion into unmodified subtrees. */
5876 struct got_object_id *new_id;
5878 err = write_subtree(&new_id,
5880 path_base_tree, commitable_paths,
5881 status_cb, status_arg, repo);
5884 if (nsubentries == 0) {
5885 /* All entries were deleted. */
5889 memcpy(&new_te->id, new_id,
5890 sizeof(new_te->id));
5893 err = insert_tree_entry(new_te, &paths);
5900 err = match_deleted_or_modified_ct(&ct, te,
5901 path_base_tree, commitable_paths);
5905 /* NB: Deleted entries get dropped here. */
5906 if (ct->status == GOT_STATUS_MODIFY ||
5907 ct->status == GOT_STATUS_MODE_CHANGE ||
5908 ct->status == GOT_STATUS_CONFLICT ||
5909 ct->staged_status == GOT_STATUS_MODIFY) {
5910 err = alloc_modified_blob_tree_entry(
5914 err = insert_tree_entry(new_te, &paths);
5919 err = report_ct_status(ct, status_cb,
5924 /* Entry is unchanged; just copy it. */
5925 err = got_object_tree_entry_dup(&new_te, te);
5928 err = insert_tree_entry(new_te, &paths);
5936 /* Write new list of entries; deleted entries have been dropped. */
5937 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5939 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
5943 static const struct got_error *
5944 update_fileindex_after_commit(struct got_worktree *worktree,
5945 struct got_pathlist_head *commitable_paths,
5946 struct got_object_id *new_base_commit_id,
5947 struct got_fileindex *fileindex, int have_staged_files)
5949 const struct got_error *err = NULL;
5950 struct got_pathlist_entry *pe;
5951 char *relpath = NULL;
5953 TAILQ_FOREACH(pe, commitable_paths, entry) {
5954 struct got_fileindex_entry *ie;
5955 struct got_commitable *ct = pe->data;
5957 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5959 err = got_path_skip_common_ancestor(&relpath,
5960 worktree->root_path, ct->ondisk_path);
5965 if (ct->status == GOT_STATUS_DELETE ||
5966 ct->staged_status == GOT_STATUS_DELETE) {
5967 got_fileindex_entry_remove(fileindex, ie);
5968 } else if (ct->staged_status == GOT_STATUS_ADD ||
5969 ct->staged_status == GOT_STATUS_MODIFY) {
5970 got_fileindex_entry_stage_set(ie,
5971 GOT_FILEIDX_STAGE_NONE);
5972 got_fileindex_entry_staged_filetype_set(ie, 0);
5974 err = got_fileindex_entry_update(ie,
5975 worktree->root_fd, relpath,
5976 ct->staged_blob_id->sha1,
5977 new_base_commit_id->sha1,
5978 !have_staged_files);
5980 err = got_fileindex_entry_update(ie,
5981 worktree->root_fd, relpath,
5983 new_base_commit_id->sha1,
5984 !have_staged_files);
5986 err = got_fileindex_entry_alloc(&ie, pe->path);
5989 err = got_fileindex_entry_update(ie,
5990 worktree->root_fd, relpath, ct->blob_id->sha1,
5991 new_base_commit_id->sha1, 1);
5993 got_fileindex_entry_free(ie);
5996 err = got_fileindex_entry_add(fileindex, ie);
5998 got_fileindex_entry_free(ie);
6011 static const struct got_error *
6012 check_out_of_date(const char *in_repo_path, unsigned char status,
6013 unsigned char staged_status, struct got_object_id *base_blob_id,
6014 struct got_object_id *base_commit_id,
6015 struct got_object_id *head_commit_id, struct got_repository *repo,
6018 const struct got_error *err = NULL;
6019 struct got_commit_object *commit = NULL;
6020 struct got_object_id *id = NULL;
6022 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
6023 /* Trivial case: base commit == head commit */
6024 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
6027 * Ensure file content which local changes were based
6028 * on matches file content in the branch head.
6030 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6033 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6035 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6036 err = got_error(ood_errcode);
6038 } else if (got_object_id_cmp(id, base_blob_id) != 0)
6039 err = got_error(ood_errcode);
6041 /* Require that added files don't exist in the branch head. */
6042 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6045 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6046 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
6048 err = id ? got_error(ood_errcode) : NULL;
6053 got_object_commit_close(commit);
6057 static const struct got_error *
6058 commit_worktree(struct got_object_id **new_commit_id,
6059 struct got_pathlist_head *commitable_paths,
6060 struct got_object_id *head_commit_id,
6061 struct got_object_id *parent_id2,
6062 struct got_worktree *worktree,
6063 const char *author, const char *committer, char *diff_path,
6064 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6065 got_worktree_status_cb status_cb, void *status_arg,
6066 struct got_repository *repo)
6068 const struct got_error *err = NULL, *unlockerr = NULL;
6069 struct got_pathlist_entry *pe;
6070 const char *head_ref_name = NULL;
6071 struct got_commit_object *head_commit = NULL;
6072 struct got_reference *head_ref2 = NULL;
6073 struct got_object_id *head_commit_id2 = NULL;
6074 struct got_tree_object *head_tree = NULL;
6075 struct got_object_id *new_tree_id = NULL;
6076 int nentries, nparents = 0;
6077 struct got_object_id_queue parent_ids;
6078 struct got_object_qid *pid = NULL;
6079 char *logmsg = NULL;
6082 *new_commit_id = NULL;
6084 STAILQ_INIT(&parent_ids);
6086 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6090 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6094 if (commit_msg_cb != NULL) {
6095 err = commit_msg_cb(commitable_paths, diff_path,
6096 &logmsg, commit_arg);
6101 if (logmsg == NULL || strlen(logmsg) == 0) {
6102 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6106 /* Create blobs from added and modified files and record their IDs. */
6107 TAILQ_FOREACH(pe, commitable_paths, entry) {
6108 struct got_commitable *ct = pe->data;
6111 /* Blobs for staged files already exist. */
6112 if (ct->staged_status == GOT_STATUS_ADD ||
6113 ct->staged_status == GOT_STATUS_MODIFY)
6116 if (ct->status != GOT_STATUS_ADD &&
6117 ct->status != GOT_STATUS_MODIFY &&
6118 ct->status != GOT_STATUS_MODE_CHANGE &&
6119 ct->status != GOT_STATUS_CONFLICT)
6122 if (asprintf(&ondisk_path, "%s/%s",
6123 worktree->root_path, pe->path) == -1) {
6124 err = got_error_from_errno("asprintf");
6127 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6133 /* Recursively write new tree objects. */
6134 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6135 commitable_paths, status_cb, status_arg, repo);
6139 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
6142 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6145 err = got_object_qid_alloc(&pid, parent_id2);
6148 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6151 timestamp = time(NULL);
6152 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6153 nparents, author, timestamp, committer, timestamp, logmsg, repo);
6159 /* Check if a concurrent commit to our branch has occurred. */
6160 head_ref_name = got_worktree_get_head_ref_name(worktree);
6161 if (head_ref_name == NULL) {
6162 err = got_error_from_errno("got_worktree_get_head_ref_name");
6165 /* Lock the reference here to prevent concurrent modification. */
6166 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6169 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6172 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6173 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6176 /* Update branch head in repository. */
6177 err = got_ref_change_ref(head_ref2, *new_commit_id);
6180 err = got_ref_write(head_ref2, repo);
6184 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6188 err = ref_base_commit(worktree, repo);
6192 got_object_id_queue_free(&parent_ids);
6194 got_object_tree_close(head_tree);
6196 got_object_commit_close(head_commit);
6197 free(head_commit_id2);
6199 unlockerr = got_ref_unlock(head_ref2);
6200 if (unlockerr && err == NULL)
6202 got_ref_close(head_ref2);
6207 static const struct got_error *
6208 check_path_is_commitable(const char *path,
6209 struct got_pathlist_head *commitable_paths)
6211 struct got_pathlist_entry *cpe = NULL;
6212 size_t path_len = strlen(path);
6214 TAILQ_FOREACH(cpe, commitable_paths, entry) {
6215 struct got_commitable *ct = cpe->data;
6216 const char *ct_path = ct->path;
6218 while (ct_path[0] == '/')
6221 if (strcmp(path, ct_path) == 0 ||
6222 got_path_is_child(ct_path, path, path_len))
6227 return got_error_path(path, GOT_ERR_BAD_PATH);
6232 static const struct got_error *
6233 check_staged_file(void *arg, struct got_fileindex_entry *ie)
6235 int *have_staged_files = arg;
6237 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6238 *have_staged_files = 1;
6239 return got_error(GOT_ERR_CANCELLED);
6245 static const struct got_error *
6246 check_non_staged_files(struct got_fileindex *fileindex,
6247 struct got_pathlist_head *paths)
6249 struct got_pathlist_entry *pe;
6250 struct got_fileindex_entry *ie;
6252 TAILQ_FOREACH(pe, paths, entry) {
6253 if (pe->path[0] == '\0')
6255 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6257 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6258 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6259 return got_error_path(pe->path,
6260 GOT_ERR_FILE_NOT_STAGED);
6266 const struct got_error *
6267 got_worktree_commit(struct got_object_id **new_commit_id,
6268 struct got_worktree *worktree, struct got_pathlist_head *paths,
6269 const char *author, const char *committer, int allow_bad_symlinks,
6270 int show_diff, int commit_conflicts,
6271 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6272 got_worktree_status_cb status_cb, void *status_arg,
6273 struct got_repository *repo)
6275 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6276 struct got_fileindex *fileindex = NULL;
6277 char *fileindex_path = NULL;
6278 struct got_pathlist_head commitable_paths;
6279 struct collect_commitables_arg cc_arg;
6280 struct got_pathlist_entry *pe;
6281 struct got_reference *head_ref = NULL;
6282 struct got_object_id *head_commit_id = NULL;
6283 char *diff_path = NULL;
6284 int have_staged_files = 0;
6286 *new_commit_id = NULL;
6288 memset(&cc_arg, 0, sizeof(cc_arg));
6289 TAILQ_INIT(&commitable_paths);
6291 err = lock_worktree(worktree, LOCK_EX);
6295 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6299 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6303 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6307 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6308 &have_staged_files);
6309 if (err && err->code != GOT_ERR_CANCELLED)
6311 if (have_staged_files) {
6312 err = check_non_staged_files(fileindex, paths);
6317 cc_arg.commitable_paths = &commitable_paths;
6318 cc_arg.worktree = worktree;
6319 cc_arg.fileindex = fileindex;
6321 cc_arg.have_staged_files = have_staged_files;
6322 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6323 cc_arg.diff_header_shown = 0;
6324 cc_arg.commit_conflicts = commit_conflicts;
6326 err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6327 GOT_TMPDIR_STR "/got", ".diff");
6330 cc_arg.f1 = got_opentemp();
6331 if (cc_arg.f1 == NULL) {
6332 err = got_error_from_errno("got_opentemp");
6335 cc_arg.f2 = got_opentemp();
6336 if (cc_arg.f2 == NULL) {
6337 err = got_error_from_errno("got_opentemp");
6342 TAILQ_FOREACH(pe, paths, entry) {
6343 err = worktree_status(worktree, pe->path, fileindex, repo,
6344 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6350 if (fflush(cc_arg.diff_outfile) == EOF) {
6351 err = got_error_from_errno("fflush");
6356 if (TAILQ_EMPTY(&commitable_paths)) {
6357 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6361 TAILQ_FOREACH(pe, paths, entry) {
6362 err = check_path_is_commitable(pe->path, &commitable_paths);
6367 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6368 struct got_commitable *ct = pe->data;
6369 const char *ct_path = ct->in_repo_path;
6371 while (ct_path[0] == '/')
6373 err = check_out_of_date(ct_path, ct->status,
6374 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6375 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6381 err = commit_worktree(new_commit_id, &commitable_paths,
6382 head_commit_id, NULL, worktree, author, committer,
6383 (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6384 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6388 err = update_fileindex_after_commit(worktree, &commitable_paths,
6389 *new_commit_id, fileindex, have_staged_files);
6390 sync_err = sync_fileindex(fileindex, fileindex_path);
6391 if (sync_err && err == NULL)
6395 got_fileindex_free(fileindex);
6396 free(fileindex_path);
6397 unlockerr = lock_worktree(worktree, LOCK_SH);
6398 if (unlockerr && err == NULL)
6400 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6401 struct got_commitable *ct = pe->data;
6403 free_commitable(ct);
6405 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6406 if (diff_path && unlink(diff_path) == -1 && err == NULL)
6407 err = got_error_from_errno2("unlink", diff_path);
6409 if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6411 err = got_error_from_errno("fclose");
6416 got_commitable_get_path(struct got_commitable *ct)
6422 got_commitable_get_status(struct got_commitable *ct)
6427 struct check_rebase_ok_arg {
6428 struct got_worktree *worktree;
6429 struct got_repository *repo;
6432 static const struct got_error *
6433 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6435 const struct got_error *err = NULL;
6436 struct check_rebase_ok_arg *a = arg;
6437 unsigned char status;
6441 /* Reject rebase of a work tree with mixed base commits. */
6442 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6443 SHA1_DIGEST_LENGTH))
6444 return got_error(GOT_ERR_MIXED_COMMITS);
6446 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6448 return got_error_from_errno("asprintf");
6450 /* Reject rebase of a work tree with modified or staged files. */
6451 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6456 if (status != GOT_STATUS_NO_CHANGE)
6457 return got_error(GOT_ERR_MODIFIED);
6458 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6459 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6464 const struct got_error *
6465 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6466 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6467 struct got_worktree *worktree, struct got_reference *branch,
6468 struct got_repository *repo)
6470 const struct got_error *err = NULL;
6471 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6472 char *branch_ref_name = NULL;
6473 char *fileindex_path = NULL;
6474 struct check_rebase_ok_arg ok_arg;
6475 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6476 struct got_object_id *wt_branch_tip = NULL;
6478 *new_base_branch_ref = NULL;
6482 err = lock_worktree(worktree, LOCK_EX);
6486 err = open_fileindex(fileindex, &fileindex_path, worktree);
6490 ok_arg.worktree = worktree;
6492 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6497 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6501 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6505 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6509 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6514 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6517 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6518 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6522 err = got_ref_alloc_symref(new_base_branch_ref,
6523 new_base_branch_ref_name, wt_branch);
6526 err = got_ref_write(*new_base_branch_ref, repo);
6530 /* TODO Lock original branch's ref while rebasing? */
6532 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6536 err = got_ref_write(branch_ref, repo);
6540 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6541 worktree->base_commit_id);
6544 err = got_ref_write(*tmp_branch, repo);
6548 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6552 free(fileindex_path);
6553 free(tmp_branch_name);
6554 free(new_base_branch_ref_name);
6555 free(branch_ref_name);
6557 got_ref_close(branch_ref);
6559 got_ref_close(wt_branch);
6560 free(wt_branch_tip);
6562 if (*new_base_branch_ref) {
6563 got_ref_close(*new_base_branch_ref);
6564 *new_base_branch_ref = NULL;
6567 got_ref_close(*tmp_branch);
6571 got_fileindex_free(*fileindex);
6574 lock_worktree(worktree, LOCK_SH);
6579 const struct got_error *
6580 got_worktree_rebase_continue(struct got_object_id **commit_id,
6581 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6582 struct got_reference **branch, struct got_fileindex **fileindex,
6583 struct got_worktree *worktree, struct got_repository *repo)
6585 const struct got_error *err;
6586 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6587 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6588 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6589 char *fileindex_path = NULL;
6590 int have_staged_files = 0;
6593 *new_base_branch = NULL;
6598 err = lock_worktree(worktree, LOCK_EX);
6602 err = open_fileindex(fileindex, &fileindex_path, worktree);
6606 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6607 &have_staged_files);
6608 if (err && err->code != GOT_ERR_CANCELLED)
6610 if (have_staged_files) {
6611 err = got_error(GOT_ERR_STAGED_PATHS);
6615 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6619 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6623 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6627 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6631 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6635 err = got_ref_open(branch, repo,
6636 got_ref_get_symref_target(branch_ref), 0);
6640 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6644 err = got_ref_resolve(commit_id, repo, commit_ref);
6648 err = got_ref_open(new_base_branch, repo,
6649 new_base_branch_ref_name, 0);
6653 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6657 free(commit_ref_name);
6658 free(branch_ref_name);
6659 free(fileindex_path);
6661 got_ref_close(commit_ref);
6663 got_ref_close(branch_ref);
6668 got_ref_close(*tmp_branch);
6671 if (*new_base_branch) {
6672 got_ref_close(*new_base_branch);
6673 *new_base_branch = NULL;
6676 got_ref_close(*branch);
6680 got_fileindex_free(*fileindex);
6683 lock_worktree(worktree, LOCK_SH);
6688 const struct got_error *
6689 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6691 const struct got_error *err;
6692 char *tmp_branch_name = NULL;
6694 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6698 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6699 free(tmp_branch_name);
6703 static const struct got_error *
6704 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6705 const char *diff_path, char **logmsg, void *arg)
6711 static const struct got_error *
6712 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6713 const char *path, struct got_object_id *blob_id,
6714 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6715 int dirfd, const char *de_name)
6720 struct collect_merged_paths_arg {
6721 got_worktree_checkout_cb progress_cb;
6723 struct got_pathlist_head *merged_paths;
6726 static const struct got_error *
6727 collect_merged_paths(void *arg, unsigned char status, const char *path)
6729 const struct got_error *err;
6730 struct collect_merged_paths_arg *a = arg;
6732 struct got_pathlist_entry *new;
6734 err = (*a->progress_cb)(a->progress_arg, status, path);
6738 if (status != GOT_STATUS_MERGE &&
6739 status != GOT_STATUS_ADD &&
6740 status != GOT_STATUS_DELETE &&
6741 status != GOT_STATUS_CONFLICT)
6746 return got_error_from_errno("strdup");
6748 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6749 if (err || new == NULL)
6754 static const struct got_error *
6755 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6756 int is_rebase, struct got_repository *repo)
6758 const struct got_error *err;
6759 struct got_reference *commit_ref = NULL;
6761 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6763 if (err->code != GOT_ERR_NOT_REF)
6765 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6768 err = got_ref_write(commit_ref, repo);
6771 } else if (is_rebase) {
6772 struct got_object_id *stored_id;
6775 err = got_ref_resolve(&stored_id, repo, commit_ref);
6778 cmp = got_object_id_cmp(commit_id, stored_id);
6781 err = got_error(GOT_ERR_REBASE_COMMITID);
6787 got_ref_close(commit_ref);
6791 static const struct got_error *
6792 rebase_merge_files(struct got_pathlist_head *merged_paths,
6793 const char *commit_ref_name, struct got_worktree *worktree,
6794 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6795 struct got_object_id *commit_id, struct got_repository *repo,
6796 got_worktree_checkout_cb progress_cb, void *progress_arg,
6797 got_cancel_cb cancel_cb, void *cancel_arg)
6799 const struct got_error *err;
6800 struct got_reference *commit_ref = NULL;
6801 struct collect_merged_paths_arg cmp_arg;
6802 char *fileindex_path;
6804 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6806 err = get_fileindex_path(&fileindex_path, worktree);
6810 cmp_arg.progress_cb = progress_cb;
6811 cmp_arg.progress_arg = progress_arg;
6812 cmp_arg.merged_paths = merged_paths;
6813 err = merge_files(worktree, fileindex, fileindex_path,
6814 parent_commit_id, commit_id, repo, collect_merged_paths,
6815 &cmp_arg, cancel_cb, cancel_arg);
6817 got_ref_close(commit_ref);
6821 const struct got_error *
6822 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6823 struct got_worktree *worktree, struct got_fileindex *fileindex,
6824 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6825 struct got_repository *repo,
6826 got_worktree_checkout_cb progress_cb, void *progress_arg,
6827 got_cancel_cb cancel_cb, void *cancel_arg)
6829 const struct got_error *err;
6830 char *commit_ref_name;
6832 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6836 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6840 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6841 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6842 progress_arg, cancel_cb, cancel_arg);
6844 free(commit_ref_name);
6848 const struct got_error *
6849 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6850 struct got_worktree *worktree, struct got_fileindex *fileindex,
6851 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6852 struct got_repository *repo,
6853 got_worktree_checkout_cb progress_cb, void *progress_arg,
6854 got_cancel_cb cancel_cb, void *cancel_arg)
6856 const struct got_error *err;
6857 char *commit_ref_name;
6859 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6863 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6867 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6868 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6869 progress_arg, cancel_cb, cancel_arg);
6871 free(commit_ref_name);
6875 static const struct got_error *
6876 rebase_commit(struct got_object_id **new_commit_id,
6877 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6878 struct got_worktree *worktree, struct got_fileindex *fileindex,
6879 struct got_reference *tmp_branch, const char *committer,
6880 struct got_commit_object *orig_commit, const char *new_logmsg,
6881 int allow_conflict, struct got_repository *repo)
6883 const struct got_error *err, *sync_err;
6884 struct got_pathlist_head commitable_paths;
6885 struct collect_commitables_arg cc_arg;
6886 char *fileindex_path = NULL;
6887 struct got_reference *head_ref = NULL;
6888 struct got_object_id *head_commit_id = NULL;
6889 char *logmsg = NULL;
6891 memset(&cc_arg, 0, sizeof(cc_arg));
6892 TAILQ_INIT(&commitable_paths);
6893 *new_commit_id = NULL;
6895 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6897 err = get_fileindex_path(&fileindex_path, worktree);
6901 cc_arg.commitable_paths = &commitable_paths;
6902 cc_arg.worktree = worktree;
6904 cc_arg.have_staged_files = 0;
6905 cc_arg.commit_conflicts = allow_conflict;
6907 * If possible get the status of individual files directly to
6908 * avoid crawling the entire work tree once per rebased commit.
6910 * Ideally, merged_paths would contain a list of commitables
6911 * we could use so we could skip worktree_status() entirely.
6912 * However, we would then need carefully keep track of cumulative
6913 * effects of operations such as file additions and deletions
6914 * in 'got histedit -f' (folding multiple commits into one),
6915 * and this extra complexity is not really worth it.
6918 struct got_pathlist_entry *pe;
6919 TAILQ_FOREACH(pe, merged_paths, entry) {
6920 err = worktree_status(worktree, pe->path, fileindex,
6921 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6927 err = worktree_status(worktree, "", fileindex, repo,
6928 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6933 if (TAILQ_EMPTY(&commitable_paths)) {
6934 /* No-op change; commit will be elided. */
6935 err = got_ref_delete(commit_ref, repo);
6938 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6942 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6946 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6951 logmsg = strdup(new_logmsg);
6952 if (logmsg == NULL) {
6953 err = got_error_from_errno("strdup");
6957 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6962 /* NB: commit_worktree will call free(logmsg) */
6963 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6964 NULL, worktree, got_object_commit_get_author(orig_commit),
6965 committer ? committer :
6966 got_object_commit_get_committer(orig_commit), NULL,
6967 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6971 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6975 err = got_ref_delete(commit_ref, repo);
6979 err = update_fileindex_after_commit(worktree, &commitable_paths,
6980 *new_commit_id, fileindex, 0);
6981 sync_err = sync_fileindex(fileindex, fileindex_path);
6982 if (sync_err && err == NULL)
6985 free(fileindex_path);
6986 free(head_commit_id);
6988 got_ref_close(head_ref);
6990 free(*new_commit_id);
6991 *new_commit_id = NULL;
6996 const struct got_error *
6997 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6998 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6999 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7000 const char *committer, struct got_commit_object *orig_commit,
7001 struct got_object_id *orig_commit_id, int allow_conflict,
7002 struct got_repository *repo)
7004 const struct got_error *err;
7005 char *commit_ref_name;
7006 struct got_reference *commit_ref = NULL;
7007 struct got_object_id *commit_id = NULL;
7009 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7013 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7016 err = got_ref_resolve(&commit_id, repo, commit_ref);
7019 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
7020 err = got_error(GOT_ERR_REBASE_COMMITID);
7024 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7025 worktree, fileindex, tmp_branch, committer, orig_commit,
7026 NULL, allow_conflict, repo);
7029 got_ref_close(commit_ref);
7030 free(commit_ref_name);
7035 const struct got_error *
7036 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
7037 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7038 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7039 const char *committer, struct got_commit_object *orig_commit,
7040 struct got_object_id *orig_commit_id, const char *new_logmsg,
7041 int allow_conflict, struct got_repository *repo)
7043 const struct got_error *err;
7044 char *commit_ref_name;
7045 struct got_reference *commit_ref = NULL;
7047 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7051 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7055 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7056 worktree, fileindex, tmp_branch, committer, orig_commit,
7057 new_logmsg, allow_conflict, repo);
7060 got_ref_close(commit_ref);
7061 free(commit_ref_name);
7065 const struct got_error *
7066 got_worktree_rebase_postpone(struct got_worktree *worktree,
7067 struct got_fileindex *fileindex)
7070 got_fileindex_free(fileindex);
7071 return lock_worktree(worktree, LOCK_SH);
7074 static const struct got_error *
7075 delete_ref(const char *name, struct got_repository *repo)
7077 const struct got_error *err;
7078 struct got_reference *ref;
7080 err = got_ref_open(&ref, repo, name, 0);
7082 if (err->code == GOT_ERR_NOT_REF)
7087 err = got_ref_delete(ref, repo);
7092 static const struct got_error *
7093 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7095 const struct got_error *err;
7096 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7097 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7099 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7102 err = delete_ref(tmp_branch_name, repo);
7106 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7109 err = delete_ref(new_base_branch_ref_name, repo);
7113 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7116 err = delete_ref(branch_ref_name, repo);
7120 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7123 err = delete_ref(commit_ref_name, repo);
7128 free(tmp_branch_name);
7129 free(new_base_branch_ref_name);
7130 free(branch_ref_name);
7131 free(commit_ref_name);
7135 static const struct got_error *
7136 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7137 struct got_object_id *new_commit_id, struct got_repository *repo)
7139 const struct got_error *err;
7140 struct got_reference *ref = NULL;
7141 struct got_object_id *old_commit_id = NULL;
7142 const char *branch_name = NULL;
7143 char *new_id_str = NULL;
7144 char *refname = NULL;
7146 branch_name = got_ref_get_name(branch);
7147 if (strncmp(branch_name, "refs/heads/", 11) != 0)
7148 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7151 err = got_object_id_str(&new_id_str, new_commit_id);
7155 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7156 new_id_str) == -1) {
7157 err = got_error_from_errno("asprintf");
7161 err = got_ref_resolve(&old_commit_id, repo, branch);
7165 err = got_ref_alloc(&ref, refname, old_commit_id);
7169 err = got_ref_write(ref, repo);
7173 free(old_commit_id);
7179 const struct got_error *
7180 got_worktree_rebase_complete(struct got_worktree *worktree,
7181 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7182 struct got_reference *rebased_branch, struct got_repository *repo,
7185 const struct got_error *err, *unlockerr, *sync_err;
7186 struct got_object_id *new_head_commit_id = NULL;
7187 char *fileindex_path = NULL;
7189 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7193 if (create_backup) {
7194 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7195 rebased_branch, new_head_commit_id, repo);
7200 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7204 err = got_ref_write(rebased_branch, repo);
7208 err = got_worktree_set_head_ref(worktree, rebased_branch);
7212 err = delete_rebase_refs(worktree, repo);
7216 err = get_fileindex_path(&fileindex_path, worktree);
7219 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7220 sync_err = sync_fileindex(fileindex, fileindex_path);
7221 if (sync_err && err == NULL)
7224 got_fileindex_free(fileindex);
7225 free(fileindex_path);
7226 free(new_head_commit_id);
7227 unlockerr = lock_worktree(worktree, LOCK_SH);
7228 if (unlockerr && err == NULL)
7233 const struct got_error *
7234 got_worktree_rebase_abort(struct got_worktree *worktree,
7235 struct got_fileindex *fileindex, struct got_repository *repo,
7236 struct got_reference *new_base_branch,
7237 got_worktree_checkout_cb progress_cb, void *progress_arg)
7239 const struct got_error *err, *unlockerr, *sync_err;
7240 struct got_reference *resolved = NULL;
7241 struct got_object_id *commit_id = NULL;
7242 struct got_commit_object *commit = NULL;
7243 char *fileindex_path = NULL;
7244 struct revert_file_args rfa;
7245 struct got_object_id *tree_id = NULL;
7247 err = lock_worktree(worktree, LOCK_EX);
7251 err = got_object_open_as_commit(&commit, repo,
7252 worktree->base_commit_id);
7256 err = got_ref_open(&resolved, repo,
7257 got_ref_get_symref_target(new_base_branch), 0);
7261 err = got_worktree_set_head_ref(worktree, resolved);
7266 * XXX commits to the base branch could have happened while
7267 * we were busy rebasing; should we store the original commit ID
7268 * when rebase begins and read it back here?
7270 err = got_ref_resolve(&commit_id, repo, resolved);
7274 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7278 err = got_object_id_by_path(&tree_id, repo, commit,
7279 worktree->path_prefix);
7283 err = delete_rebase_refs(worktree, repo);
7287 err = get_fileindex_path(&fileindex_path, worktree);
7291 rfa.worktree = worktree;
7292 rfa.fileindex = fileindex;
7293 rfa.progress_cb = progress_cb;
7294 rfa.progress_arg = progress_arg;
7295 rfa.patch_cb = NULL;
7296 rfa.patch_arg = NULL;
7298 rfa.unlink_added_files = 0;
7299 err = worktree_status(worktree, "", fileindex, repo,
7300 revert_file, &rfa, NULL, NULL, 1, 0);
7304 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7305 repo, progress_cb, progress_arg, NULL, NULL);
7307 sync_err = sync_fileindex(fileindex, fileindex_path);
7308 if (sync_err && err == NULL)
7311 got_ref_close(resolved);
7315 got_object_commit_close(commit);
7317 got_fileindex_free(fileindex);
7318 free(fileindex_path);
7320 unlockerr = lock_worktree(worktree, LOCK_SH);
7321 if (unlockerr && err == NULL)
7326 const struct got_error *
7327 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7328 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7329 struct got_fileindex **fileindex, struct got_worktree *worktree,
7330 struct got_repository *repo)
7332 const struct got_error *err = NULL;
7333 char *tmp_branch_name = NULL;
7334 char *branch_ref_name = NULL;
7335 char *base_commit_ref_name = NULL;
7336 char *fileindex_path = NULL;
7337 struct check_rebase_ok_arg ok_arg;
7338 struct got_reference *wt_branch = NULL;
7339 struct got_reference *base_commit_ref = NULL;
7343 *base_commit_id = NULL;
7346 err = lock_worktree(worktree, LOCK_EX);
7350 err = open_fileindex(fileindex, &fileindex_path, worktree);
7354 ok_arg.worktree = worktree;
7356 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7361 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7365 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7369 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7374 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7379 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7383 err = got_ref_write(*branch_ref, repo);
7387 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7388 worktree->base_commit_id);
7391 err = got_ref_write(base_commit_ref, repo);
7394 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7395 if (*base_commit_id == NULL) {
7396 err = got_error_from_errno("got_object_id_dup");
7400 err = got_ref_alloc(tmp_branch, tmp_branch_name,
7401 worktree->base_commit_id);
7404 err = got_ref_write(*tmp_branch, repo);
7408 err = got_worktree_set_head_ref(worktree, *tmp_branch);
7412 free(fileindex_path);
7413 free(tmp_branch_name);
7414 free(branch_ref_name);
7415 free(base_commit_ref_name);
7417 got_ref_close(wt_branch);
7420 got_ref_close(*branch_ref);
7424 got_ref_close(*tmp_branch);
7427 free(*base_commit_id);
7429 got_fileindex_free(*fileindex);
7432 lock_worktree(worktree, LOCK_SH);
7437 const struct got_error *
7438 got_worktree_histedit_postpone(struct got_worktree *worktree,
7439 struct got_fileindex *fileindex)
7442 got_fileindex_free(fileindex);
7443 return lock_worktree(worktree, LOCK_SH);
7446 const struct got_error *
7447 got_worktree_histedit_in_progress(int *in_progress,
7448 struct got_worktree *worktree)
7450 const struct got_error *err;
7451 char *tmp_branch_name = NULL;
7453 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7457 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7458 free(tmp_branch_name);
7462 const struct got_error *
7463 got_worktree_histedit_continue(struct got_object_id **commit_id,
7464 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7465 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7466 struct got_worktree *worktree, struct got_repository *repo)
7468 const struct got_error *err;
7469 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7470 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7471 struct got_reference *commit_ref = NULL;
7472 struct got_reference *base_commit_ref = NULL;
7473 char *fileindex_path = NULL;
7474 int have_staged_files = 0;
7478 *base_commit_id = NULL;
7481 err = lock_worktree(worktree, LOCK_EX);
7485 err = open_fileindex(fileindex, &fileindex_path, worktree);
7489 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7490 &have_staged_files);
7491 if (err && err->code != GOT_ERR_CANCELLED)
7493 if (have_staged_files) {
7494 err = got_error(GOT_ERR_STAGED_PATHS);
7498 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7502 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7506 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7510 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7515 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7519 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7522 err = got_ref_resolve(commit_id, repo, commit_ref);
7526 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7529 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7533 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7537 free(commit_ref_name);
7538 free(branch_ref_name);
7539 free(fileindex_path);
7541 got_ref_close(commit_ref);
7542 if (base_commit_ref)
7543 got_ref_close(base_commit_ref);
7547 free(*base_commit_id);
7548 *base_commit_id = NULL;
7550 got_ref_close(*tmp_branch);
7554 got_fileindex_free(*fileindex);
7557 lock_worktree(worktree, LOCK_EX);
7562 static const struct got_error *
7563 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7565 const struct got_error *err;
7566 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7567 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7569 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7572 err = delete_ref(tmp_branch_name, repo);
7576 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7580 err = delete_ref(base_commit_ref_name, repo);
7584 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7587 err = delete_ref(branch_ref_name, repo);
7591 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7594 err = delete_ref(commit_ref_name, repo);
7598 free(tmp_branch_name);
7599 free(base_commit_ref_name);
7600 free(branch_ref_name);
7601 free(commit_ref_name);
7605 const struct got_error *
7606 got_worktree_histedit_abort(struct got_worktree *worktree,
7607 struct got_fileindex *fileindex, struct got_repository *repo,
7608 struct got_reference *branch, struct got_object_id *base_commit_id,
7609 got_worktree_checkout_cb progress_cb, void *progress_arg)
7611 const struct got_error *err, *unlockerr, *sync_err;
7612 struct got_reference *resolved = NULL;
7613 char *fileindex_path = NULL;
7614 struct got_commit_object *commit = NULL;
7615 struct got_object_id *tree_id = NULL;
7616 struct revert_file_args rfa;
7618 err = lock_worktree(worktree, LOCK_EX);
7622 err = got_object_open_as_commit(&commit, repo,
7623 worktree->base_commit_id);
7627 err = got_ref_open(&resolved, repo,
7628 got_ref_get_symref_target(branch), 0);
7632 err = got_worktree_set_head_ref(worktree, resolved);
7636 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7640 err = got_object_id_by_path(&tree_id, repo, commit,
7641 worktree->path_prefix);
7645 err = delete_histedit_refs(worktree, repo);
7649 err = get_fileindex_path(&fileindex_path, worktree);
7653 rfa.worktree = worktree;
7654 rfa.fileindex = fileindex;
7655 rfa.progress_cb = progress_cb;
7656 rfa.progress_arg = progress_arg;
7657 rfa.patch_cb = NULL;
7658 rfa.patch_arg = NULL;
7660 rfa.unlink_added_files = 0;
7661 err = worktree_status(worktree, "", fileindex, repo,
7662 revert_file, &rfa, NULL, NULL, 1, 0);
7666 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7667 repo, progress_cb, progress_arg, NULL, NULL);
7669 sync_err = sync_fileindex(fileindex, fileindex_path);
7670 if (sync_err && err == NULL)
7673 got_ref_close(resolved);
7675 free(fileindex_path);
7677 unlockerr = lock_worktree(worktree, LOCK_SH);
7678 if (unlockerr && err == NULL)
7683 const struct got_error *
7684 got_worktree_histedit_complete(struct got_worktree *worktree,
7685 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7686 struct got_reference *edited_branch, struct got_repository *repo)
7688 const struct got_error *err, *unlockerr, *sync_err;
7689 struct got_object_id *new_head_commit_id = NULL;
7690 struct got_reference *resolved = NULL;
7691 char *fileindex_path = NULL;
7693 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7697 err = got_ref_open(&resolved, repo,
7698 got_ref_get_symref_target(edited_branch), 0);
7702 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7703 resolved, new_head_commit_id, repo);
7707 err = got_ref_change_ref(resolved, new_head_commit_id);
7711 err = got_ref_write(resolved, repo);
7715 err = got_worktree_set_head_ref(worktree, resolved);
7719 err = delete_histedit_refs(worktree, repo);
7723 err = get_fileindex_path(&fileindex_path, worktree);
7726 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7727 sync_err = sync_fileindex(fileindex, fileindex_path);
7728 if (sync_err && err == NULL)
7731 got_fileindex_free(fileindex);
7732 free(fileindex_path);
7733 free(new_head_commit_id);
7734 unlockerr = lock_worktree(worktree, LOCK_SH);
7735 if (unlockerr && err == NULL)
7740 const struct got_error *
7741 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7742 struct got_object_id *commit_id, struct got_repository *repo)
7744 const struct got_error *err;
7745 char *commit_ref_name;
7747 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7751 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7755 err = delete_ref(commit_ref_name, repo);
7757 free(commit_ref_name);
7761 const struct got_error *
7762 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7763 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7764 struct got_worktree *worktree, const char *refname,
7765 struct got_repository *repo)
7767 const struct got_error *err = NULL;
7768 char *fileindex_path = NULL;
7769 struct check_rebase_ok_arg ok_arg;
7773 *base_branch_ref = NULL;
7775 err = lock_worktree(worktree, LOCK_EX);
7779 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7780 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7781 "cannot integrate a branch into itself; "
7782 "update -b or different branch name required");
7786 err = open_fileindex(fileindex, &fileindex_path, worktree);
7790 /* Preconditions are the same as for rebase. */
7791 ok_arg.worktree = worktree;
7793 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7798 err = got_ref_open(branch_ref, repo, refname, 1);
7802 err = got_ref_open(base_branch_ref, repo,
7803 got_worktree_get_head_ref_name(worktree), 1);
7807 got_ref_close(*branch_ref);
7810 if (*base_branch_ref) {
7811 got_ref_close(*base_branch_ref);
7812 *base_branch_ref = NULL;
7815 got_fileindex_free(*fileindex);
7818 lock_worktree(worktree, LOCK_SH);
7823 const struct got_error *
7824 got_worktree_integrate_continue(struct got_worktree *worktree,
7825 struct got_fileindex *fileindex, struct got_repository *repo,
7826 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7827 got_worktree_checkout_cb progress_cb, void *progress_arg,
7828 got_cancel_cb cancel_cb, void *cancel_arg)
7830 const struct got_error *err = NULL, *sync_err, *unlockerr;
7831 char *fileindex_path = NULL;
7832 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7833 struct got_commit_object *commit = NULL;
7835 err = get_fileindex_path(&fileindex_path, worktree);
7839 err = got_ref_resolve(&commit_id, repo, branch_ref);
7843 err = got_object_open_as_commit(&commit, repo, commit_id);
7847 err = got_object_id_by_path(&tree_id, repo, commit,
7848 worktree->path_prefix);
7852 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7856 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7857 progress_cb, progress_arg, cancel_cb, cancel_arg);
7861 err = got_ref_change_ref(base_branch_ref, commit_id);
7865 err = got_ref_write(base_branch_ref, repo);
7869 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7871 sync_err = sync_fileindex(fileindex, fileindex_path);
7872 if (sync_err && err == NULL)
7876 unlockerr = got_ref_unlock(branch_ref);
7877 if (unlockerr && err == NULL)
7879 got_ref_close(branch_ref);
7881 unlockerr = got_ref_unlock(base_branch_ref);
7882 if (unlockerr && err == NULL)
7884 got_ref_close(base_branch_ref);
7886 got_fileindex_free(fileindex);
7887 free(fileindex_path);
7890 got_object_commit_close(commit);
7892 unlockerr = lock_worktree(worktree, LOCK_SH);
7893 if (unlockerr && err == NULL)
7898 const struct got_error *
7899 got_worktree_integrate_abort(struct got_worktree *worktree,
7900 struct got_fileindex *fileindex, struct got_repository *repo,
7901 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7903 const struct got_error *err = NULL, *unlockerr = NULL;
7905 got_fileindex_free(fileindex);
7907 err = lock_worktree(worktree, LOCK_SH);
7909 unlockerr = got_ref_unlock(branch_ref);
7910 if (unlockerr && err == NULL)
7912 got_ref_close(branch_ref);
7914 unlockerr = got_ref_unlock(base_branch_ref);
7915 if (unlockerr && err == NULL)
7917 got_ref_close(base_branch_ref);
7922 const struct got_error *
7923 got_worktree_merge_postpone(struct got_worktree *worktree,
7924 struct got_fileindex *fileindex)
7926 const struct got_error *err, *sync_err;
7927 char *fileindex_path = NULL;
7929 err = get_fileindex_path(&fileindex_path, worktree);
7933 sync_err = sync_fileindex(fileindex, fileindex_path);
7935 err = lock_worktree(worktree, LOCK_SH);
7936 if (sync_err && err == NULL)
7939 got_fileindex_free(fileindex);
7940 free(fileindex_path);
7944 static const struct got_error *
7945 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7947 const struct got_error *err;
7948 char *branch_refname = NULL, *commit_refname = NULL;
7950 err = get_merge_branch_ref_name(&branch_refname, worktree);
7953 err = delete_ref(branch_refname, repo);
7957 err = get_merge_commit_ref_name(&commit_refname, worktree);
7960 err = delete_ref(commit_refname, repo);
7965 free(branch_refname);
7966 free(commit_refname);
7970 struct merge_commit_msg_arg {
7971 struct got_worktree *worktree;
7972 const char *branch_name;
7975 static const struct got_error *
7976 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
7977 const char *diff_path, char **logmsg, void *arg)
7979 struct merge_commit_msg_arg *a = arg;
7981 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7982 got_worktree_get_head_ref_name(a->worktree)) == -1)
7983 return got_error_from_errno("asprintf");
7989 const struct got_error *
7990 got_worktree_merge_branch(struct got_worktree *worktree,
7991 struct got_fileindex *fileindex,
7992 struct got_object_id *yca_commit_id,
7993 struct got_object_id *branch_tip,
7994 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7995 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7997 const struct got_error *err;
7998 char *fileindex_path = NULL;
8000 err = get_fileindex_path(&fileindex_path, worktree);
8004 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
8009 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
8010 branch_tip, repo, progress_cb, progress_arg,
8011 cancel_cb, cancel_arg);
8013 free(fileindex_path);
8017 const struct got_error *
8018 got_worktree_merge_commit(struct got_object_id **new_commit_id,
8019 struct got_worktree *worktree, struct got_fileindex *fileindex,
8020 const char *author, const char *committer, int allow_bad_symlinks,
8021 struct got_object_id *branch_tip, const char *branch_name,
8022 int allow_conflict, struct got_repository *repo,
8023 got_worktree_status_cb status_cb, void *status_arg)
8026 const struct got_error *err = NULL, *sync_err;
8027 struct got_pathlist_head commitable_paths;
8028 struct collect_commitables_arg cc_arg;
8029 struct got_pathlist_entry *pe;
8030 struct got_reference *head_ref = NULL;
8031 struct got_object_id *head_commit_id = NULL;
8032 int have_staged_files = 0;
8033 struct merge_commit_msg_arg mcm_arg;
8034 char *fileindex_path = NULL;
8036 memset(&cc_arg, 0, sizeof(cc_arg));
8037 *new_commit_id = NULL;
8039 TAILQ_INIT(&commitable_paths);
8041 err = get_fileindex_path(&fileindex_path, worktree);
8045 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
8049 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8053 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
8054 &have_staged_files);
8055 if (err && err->code != GOT_ERR_CANCELLED)
8057 if (have_staged_files) {
8058 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
8062 cc_arg.commitable_paths = &commitable_paths;
8063 cc_arg.worktree = worktree;
8064 cc_arg.fileindex = fileindex;
8066 cc_arg.have_staged_files = have_staged_files;
8067 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
8068 cc_arg.commit_conflicts = allow_conflict;
8069 err = worktree_status(worktree, "", fileindex, repo,
8070 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8074 if (TAILQ_EMPTY(&commitable_paths)) {
8075 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
8076 "merge of %s cannot proceed", branch_name);
8080 TAILQ_FOREACH(pe, &commitable_paths, entry) {
8081 struct got_commitable *ct = pe->data;
8082 const char *ct_path = ct->in_repo_path;
8084 while (ct_path[0] == '/')
8086 err = check_out_of_date(ct_path, ct->status,
8087 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
8088 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
8094 mcm_arg.worktree = worktree;
8095 mcm_arg.branch_name = branch_name;
8096 err = commit_worktree(new_commit_id, &commitable_paths,
8097 head_commit_id, branch_tip, worktree, author, committer, NULL,
8098 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8102 err = update_fileindex_after_commit(worktree, &commitable_paths,
8103 *new_commit_id, fileindex, have_staged_files);
8104 sync_err = sync_fileindex(fileindex, fileindex_path);
8105 if (sync_err && err == NULL)
8108 TAILQ_FOREACH(pe, &commitable_paths, entry) {
8109 struct got_commitable *ct = pe->data;
8111 free_commitable(ct);
8113 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8114 free(fileindex_path);
8118 const struct got_error *
8119 got_worktree_merge_complete(struct got_worktree *worktree,
8120 struct got_fileindex *fileindex, struct got_repository *repo)
8122 const struct got_error *err, *unlockerr, *sync_err;
8123 char *fileindex_path = NULL;
8125 err = delete_merge_refs(worktree, repo);
8129 err = get_fileindex_path(&fileindex_path, worktree);
8132 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8133 sync_err = sync_fileindex(fileindex, fileindex_path);
8134 if (sync_err && err == NULL)
8137 got_fileindex_free(fileindex);
8138 free(fileindex_path);
8139 unlockerr = lock_worktree(worktree, LOCK_SH);
8140 if (unlockerr && err == NULL)
8145 const struct got_error *
8146 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8147 struct got_repository *repo)
8149 const struct got_error *err;
8150 char *branch_refname = NULL;
8151 struct got_reference *branch_ref = NULL;
8155 err = get_merge_branch_ref_name(&branch_refname, worktree);
8158 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8159 free(branch_refname);
8161 if (err->code != GOT_ERR_NOT_REF)
8169 const struct got_error *got_worktree_merge_prepare(
8170 struct got_fileindex **fileindex, struct got_worktree *worktree,
8171 struct got_reference *branch, struct got_repository *repo)
8173 const struct got_error *err = NULL;
8174 char *fileindex_path = NULL;
8175 char *branch_refname = NULL, *commit_refname = NULL;
8176 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
8177 struct got_reference *commit_ref = NULL;
8178 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
8179 struct check_rebase_ok_arg ok_arg;
8183 err = lock_worktree(worktree, LOCK_EX);
8187 err = open_fileindex(fileindex, &fileindex_path, worktree);
8191 /* Preconditions are the same as for rebase. */
8192 ok_arg.worktree = worktree;
8194 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8199 err = get_merge_branch_ref_name(&branch_refname, worktree);
8203 err = get_merge_commit_ref_name(&commit_refname, worktree);
8207 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8212 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8216 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8217 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8221 err = got_ref_resolve(&branch_tip, repo, branch);
8225 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8228 err = got_ref_write(branch_ref, repo);
8232 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8235 err = got_ref_write(commit_ref, repo);
8240 free(branch_refname);
8241 free(commit_refname);
8242 free(fileindex_path);
8244 got_ref_close(branch_ref);
8246 got_ref_close(commit_ref);
8248 got_ref_close(wt_branch);
8249 free(wt_branch_tip);
8252 got_fileindex_free(*fileindex);
8255 lock_worktree(worktree, LOCK_SH);
8260 const struct got_error *
8261 got_worktree_merge_continue(char **branch_name,
8262 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8263 struct got_worktree *worktree, struct got_repository *repo)
8265 const struct got_error *err;
8266 char *commit_refname = NULL, *branch_refname = NULL;
8267 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8268 char *fileindex_path = NULL;
8269 int have_staged_files = 0;
8271 *branch_name = NULL;
8275 err = lock_worktree(worktree, LOCK_EX);
8279 err = open_fileindex(fileindex, &fileindex_path, worktree);
8283 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8284 &have_staged_files);
8285 if (err && err->code != GOT_ERR_CANCELLED)
8287 if (have_staged_files) {
8288 err = got_error(GOT_ERR_STAGED_PATHS);
8292 err = get_merge_branch_ref_name(&branch_refname, worktree);
8296 err = get_merge_commit_ref_name(&commit_refname, worktree);
8300 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8304 if (!got_ref_is_symbolic(branch_ref)) {
8305 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8306 "%s is not a symbolic reference",
8307 got_ref_get_name(branch_ref));
8310 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8311 if (*branch_name == NULL) {
8312 err = got_error_from_errno("strdup");
8316 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8320 err = got_ref_resolve(branch_tip, repo, commit_ref);
8324 free(commit_refname);
8325 free(branch_refname);
8326 free(fileindex_path);
8328 got_ref_close(commit_ref);
8330 got_ref_close(branch_ref);
8334 *branch_name = NULL;
8339 got_fileindex_free(*fileindex);
8342 lock_worktree(worktree, LOCK_SH);
8347 const struct got_error *
8348 got_worktree_merge_abort(struct got_worktree *worktree,
8349 struct got_fileindex *fileindex, struct got_repository *repo,
8350 got_worktree_checkout_cb progress_cb, void *progress_arg)
8352 const struct got_error *err, *unlockerr, *sync_err;
8353 struct got_object_id *commit_id = NULL;
8354 struct got_commit_object *commit = NULL;
8355 char *fileindex_path = NULL;
8356 struct revert_file_args rfa;
8357 struct got_object_id *tree_id = NULL;
8359 err = got_object_open_as_commit(&commit, repo,
8360 worktree->base_commit_id);
8364 err = got_object_id_by_path(&tree_id, repo, commit,
8365 worktree->path_prefix);
8369 err = delete_merge_refs(worktree, repo);
8373 err = get_fileindex_path(&fileindex_path, worktree);
8377 rfa.worktree = worktree;
8378 rfa.fileindex = fileindex;
8379 rfa.progress_cb = progress_cb;
8380 rfa.progress_arg = progress_arg;
8381 rfa.patch_cb = NULL;
8382 rfa.patch_arg = NULL;
8384 rfa.unlink_added_files = 1;
8385 err = worktree_status(worktree, "", fileindex, repo,
8386 revert_file, &rfa, NULL, NULL, 1, 0);
8390 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8391 repo, progress_cb, progress_arg, NULL, NULL);
8393 sync_err = sync_fileindex(fileindex, fileindex_path);
8394 if (sync_err && err == NULL)
8400 got_object_commit_close(commit);
8402 got_fileindex_free(fileindex);
8403 free(fileindex_path);
8405 unlockerr = lock_worktree(worktree, LOCK_SH);
8406 if (unlockerr && err == NULL)
8411 struct check_stage_ok_arg {
8412 struct got_object_id *head_commit_id;
8413 struct got_worktree *worktree;
8414 struct got_fileindex *fileindex;
8415 struct got_repository *repo;
8419 static const struct got_error *
8420 check_stage_ok(void *arg, unsigned char status,
8421 unsigned char staged_status, const char *relpath,
8422 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8423 struct got_object_id *commit_id, int dirfd, const char *de_name)
8425 struct check_stage_ok_arg *a = arg;
8426 const struct got_error *err = NULL;
8427 struct got_fileindex_entry *ie;
8428 struct got_object_id base_commit_id;
8429 struct got_object_id *base_commit_idp = NULL;
8430 char *in_repo_path = NULL, *p;
8432 if (status == GOT_STATUS_UNVERSIONED ||
8433 status == GOT_STATUS_NO_CHANGE)
8435 if (status == GOT_STATUS_NONEXISTENT)
8436 return got_error_set_errno(ENOENT, relpath);
8438 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8440 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8442 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8443 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8445 return got_error_from_errno("asprintf");
8447 if (got_fileindex_entry_has_commit(ie)) {
8448 base_commit_idp = got_fileindex_entry_get_commit_id(
8449 &base_commit_id, ie);
8452 if (status == GOT_STATUS_CONFLICT) {
8453 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8455 } else if (status != GOT_STATUS_ADD &&
8456 status != GOT_STATUS_MODIFY &&
8457 status != GOT_STATUS_DELETE) {
8458 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8462 a->have_changes = 1;
8467 err = check_out_of_date(p, status, staged_status,
8468 blob_id, base_commit_idp, a->head_commit_id, a->repo,
8469 GOT_ERR_STAGE_OUT_OF_DATE);
8475 struct stage_path_arg {
8476 struct got_worktree *worktree;
8477 struct got_fileindex *fileindex;
8478 struct got_repository *repo;
8479 got_worktree_status_cb status_cb;
8481 got_worktree_patch_cb patch_cb;
8483 int staged_something;
8484 int allow_bad_symlinks;
8487 static const struct got_error *
8488 stage_path(void *arg, unsigned char status,
8489 unsigned char staged_status, const char *relpath,
8490 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8491 struct got_object_id *commit_id, int dirfd, const char *de_name)
8493 struct stage_path_arg *a = arg;
8494 const struct got_error *err = NULL;
8495 struct got_fileindex_entry *ie;
8496 char *ondisk_path = NULL, *path_content = NULL;
8498 struct got_object_id *new_staged_blob_id = NULL;
8501 if (status == GOT_STATUS_UNVERSIONED)
8504 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8506 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8508 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8510 return got_error_from_errno("asprintf");
8513 case GOT_STATUS_ADD:
8514 case GOT_STATUS_MODIFY:
8515 /* XXX could sb.st_mode be passed in by our caller? */
8516 if (lstat(ondisk_path, &sb) == -1) {
8517 err = got_error_from_errno2("lstat", ondisk_path);
8521 if (status == GOT_STATUS_ADD) {
8522 int choice = GOT_PATCH_CHOICE_NONE;
8523 err = (*a->patch_cb)(&choice, a->patch_arg,
8524 status, ie->path, NULL, 1, 1);
8527 if (choice != GOT_PATCH_CHOICE_YES)
8530 err = create_patched_content(&path_content, 0,
8531 staged_blob_id ? staged_blob_id : blob_id,
8532 ondisk_path, dirfd, de_name, ie->path,
8533 a->repo, a->patch_cb, a->patch_arg);
8534 if (err || path_content == NULL)
8538 err = got_object_blob_create(&new_staged_blob_id,
8539 path_content ? path_content : ondisk_path, a->repo);
8542 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8543 SHA1_DIGEST_LENGTH);
8544 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8545 stage = GOT_FILEIDX_STAGE_ADD;
8547 stage = GOT_FILEIDX_STAGE_MODIFY;
8548 got_fileindex_entry_stage_set(ie, stage);
8549 if (S_ISLNK(sb.st_mode)) {
8550 int is_bad_symlink = 0;
8551 if (!a->allow_bad_symlinks) {
8552 char target_path[PATH_MAX];
8554 target_len = readlink(ondisk_path, target_path,
8555 sizeof(target_path));
8556 if (target_len == -1) {
8557 err = got_error_from_errno2("readlink",
8561 err = is_bad_symlink_target(&is_bad_symlink,
8562 target_path, target_len, ondisk_path,
8563 a->worktree->root_path);
8566 if (is_bad_symlink) {
8567 err = got_error_path(ondisk_path,
8568 GOT_ERR_BAD_SYMLINK);
8573 got_fileindex_entry_staged_filetype_set(ie,
8574 GOT_FILEIDX_MODE_BAD_SYMLINK);
8576 got_fileindex_entry_staged_filetype_set(ie,
8577 GOT_FILEIDX_MODE_SYMLINK);
8579 got_fileindex_entry_staged_filetype_set(ie,
8580 GOT_FILEIDX_MODE_REGULAR_FILE);
8582 a->staged_something = 1;
8583 if (a->status_cb == NULL)
8585 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8586 get_staged_status(ie), relpath, blob_id,
8587 new_staged_blob_id, NULL, dirfd, de_name);
8591 * When staging the reverse of the staged diff,
8592 * implicitly unstage the file.
8594 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8595 sizeof(ie->blob_sha1)) == 0) {
8596 got_fileindex_entry_stage_set(ie,
8597 GOT_FILEIDX_STAGE_NONE);
8600 case GOT_STATUS_DELETE:
8601 if (staged_status == GOT_STATUS_DELETE)
8604 int choice = GOT_PATCH_CHOICE_NONE;
8605 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8606 ie->path, NULL, 1, 1);
8609 if (choice == GOT_PATCH_CHOICE_NO)
8611 if (choice != GOT_PATCH_CHOICE_YES) {
8612 err = got_error(GOT_ERR_PATCH_CHOICE);
8616 stage = GOT_FILEIDX_STAGE_DELETE;
8617 got_fileindex_entry_stage_set(ie, stage);
8618 a->staged_something = 1;
8619 if (a->status_cb == NULL)
8621 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8622 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8625 case GOT_STATUS_NO_CHANGE:
8627 case GOT_STATUS_CONFLICT:
8628 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8630 case GOT_STATUS_NONEXISTENT:
8631 err = got_error_set_errno(ENOENT, relpath);
8634 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8638 if (path_content && unlink(path_content) == -1 && err == NULL)
8639 err = got_error_from_errno2("unlink", path_content);
8642 free(new_staged_blob_id);
8646 const struct got_error *
8647 got_worktree_stage(struct got_worktree *worktree,
8648 struct got_pathlist_head *paths,
8649 got_worktree_status_cb status_cb, void *status_arg,
8650 got_worktree_patch_cb patch_cb, void *patch_arg,
8651 int allow_bad_symlinks, struct got_repository *repo)
8653 const struct got_error *err = NULL, *sync_err, *unlockerr;
8654 struct got_pathlist_entry *pe;
8655 struct got_fileindex *fileindex = NULL;
8656 char *fileindex_path = NULL;
8657 struct got_reference *head_ref = NULL;
8658 struct got_object_id *head_commit_id = NULL;
8659 struct check_stage_ok_arg oka;
8660 struct stage_path_arg spa;
8662 err = lock_worktree(worktree, LOCK_EX);
8666 err = got_ref_open(&head_ref, repo,
8667 got_worktree_get_head_ref_name(worktree), 0);
8670 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8673 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8677 /* Check pre-conditions before staging anything. */
8678 oka.head_commit_id = head_commit_id;
8679 oka.worktree = worktree;
8680 oka.fileindex = fileindex;
8682 oka.have_changes = 0;
8683 TAILQ_FOREACH(pe, paths, entry) {
8684 err = worktree_status(worktree, pe->path, fileindex, repo,
8685 check_stage_ok, &oka, NULL, NULL, 1, 0);
8689 if (!oka.have_changes) {
8690 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8694 spa.worktree = worktree;
8695 spa.fileindex = fileindex;
8697 spa.patch_cb = patch_cb;
8698 spa.patch_arg = patch_arg;
8699 spa.status_cb = status_cb;
8700 spa.status_arg = status_arg;
8701 spa.staged_something = 0;
8702 spa.allow_bad_symlinks = allow_bad_symlinks;
8703 TAILQ_FOREACH(pe, paths, entry) {
8704 err = worktree_status(worktree, pe->path, fileindex, repo,
8705 stage_path, &spa, NULL, NULL, 1, 0);
8709 if (!spa.staged_something) {
8710 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8714 sync_err = sync_fileindex(fileindex, fileindex_path);
8715 if (sync_err && err == NULL)
8719 got_ref_close(head_ref);
8720 free(head_commit_id);
8721 free(fileindex_path);
8723 got_fileindex_free(fileindex);
8724 unlockerr = lock_worktree(worktree, LOCK_SH);
8725 if (unlockerr && err == NULL)
8730 struct unstage_path_arg {
8731 struct got_worktree *worktree;
8732 struct got_fileindex *fileindex;
8733 struct got_repository *repo;
8734 got_worktree_checkout_cb progress_cb;
8736 got_worktree_patch_cb patch_cb;
8740 static const struct got_error *
8741 create_unstaged_content(char **path_unstaged_content,
8742 char **path_new_staged_content, struct got_object_id *blob_id,
8743 struct got_object_id *staged_blob_id, const char *relpath,
8744 struct got_repository *repo,
8745 got_worktree_patch_cb patch_cb, void *patch_arg)
8747 const struct got_error *err, *free_err;
8748 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8749 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8750 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8751 struct got_diffreg_result *diffreg_result = NULL;
8752 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8753 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8754 int fd1 = -1, fd2 = -1;
8756 *path_unstaged_content = NULL;
8757 *path_new_staged_content = NULL;
8759 err = got_object_id_str(&label1, blob_id);
8763 fd1 = got_opentempfd();
8765 err = got_error_from_errno("got_opentempfd");
8768 fd2 = got_opentempfd();
8770 err = got_error_from_errno("got_opentempfd");
8774 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8778 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
8782 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8786 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8791 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
8795 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8799 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8800 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8804 err = got_opentemp_named(path_unstaged_content, &outfile,
8805 "got-unstaged-content", "");
8808 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8809 "got-new-staged-content", "");
8813 if (fseek(f1, 0L, SEEK_SET) == -1) {
8814 err = got_ferror(f1, GOT_ERR_IO);
8817 if (fseek(f2, 0L, SEEK_SET) == -1) {
8818 err = got_ferror(f2, GOT_ERR_IO);
8821 /* Count the number of actual changes in the diff result. */
8822 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8823 struct diff_chunk_context cc = {};
8824 diff_chunk_context_load_change(&cc, &nchunks_used,
8825 diffreg_result->result, n, 0);
8828 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8830 err = apply_or_reject_change(&choice, &nchunks_used,
8831 diffreg_result->result, n, relpath, f1, f2,
8832 &line_cur1, &line_cur2,
8833 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8836 if (choice == GOT_PATCH_CHOICE_YES)
8839 have_rejected_content = 1;
8840 if (choice == GOT_PATCH_CHOICE_QUIT)
8843 if (have_content || have_rejected_content)
8844 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8845 outfile, rejectfile);
8848 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8849 err = got_error_from_errno("close");
8851 got_object_blob_close(blob);
8852 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8853 err = got_error_from_errno("close");
8855 got_object_blob_close(staged_blob);
8856 free_err = got_diffreg_result_free(diffreg_result);
8857 if (free_err && err == NULL)
8859 if (f1 && fclose(f1) == EOF && err == NULL)
8860 err = got_error_from_errno2("fclose", path1);
8861 if (f2 && fclose(f2) == EOF && err == NULL)
8862 err = got_error_from_errno2("fclose", path2);
8863 if (outfile && fclose(outfile) == EOF && err == NULL)
8864 err = got_error_from_errno2("fclose", *path_unstaged_content);
8865 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8866 err = got_error_from_errno2("fclose", *path_new_staged_content);
8867 if (path1 && unlink(path1) == -1 && err == NULL)
8868 err = got_error_from_errno2("unlink", path1);
8869 if (path2 && unlink(path2) == -1 && err == NULL)
8870 err = got_error_from_errno2("unlink", path2);
8871 if (err || !have_content) {
8872 if (*path_unstaged_content &&
8873 unlink(*path_unstaged_content) == -1 && err == NULL)
8874 err = got_error_from_errno2("unlink",
8875 *path_unstaged_content);
8876 free(*path_unstaged_content);
8877 *path_unstaged_content = NULL;
8879 if (err || !have_content || !have_rejected_content) {
8880 if (*path_new_staged_content &&
8881 unlink(*path_new_staged_content) == -1 && err == NULL)
8882 err = got_error_from_errno2("unlink",
8883 *path_new_staged_content);
8884 free(*path_new_staged_content);
8885 *path_new_staged_content = NULL;
8892 static const struct got_error *
8893 unstage_hunks(struct got_object_id *staged_blob_id,
8894 struct got_blob_object *blob_base,
8895 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8896 const char *ondisk_path, const char *label_orig,
8897 struct got_worktree *worktree, struct got_repository *repo,
8898 got_worktree_patch_cb patch_cb, void *patch_arg,
8899 got_worktree_checkout_cb progress_cb, void *progress_arg)
8901 const struct got_error *err = NULL;
8902 char *path_unstaged_content = NULL;
8903 char *path_new_staged_content = NULL;
8904 char *parent = NULL, *base_path = NULL;
8905 char *blob_base_path = NULL;
8906 struct got_object_id *new_staged_blob_id = NULL;
8907 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8910 err = create_unstaged_content(&path_unstaged_content,
8911 &path_new_staged_content, blob_id, staged_blob_id,
8912 ie->path, repo, patch_cb, patch_arg);
8916 if (path_unstaged_content == NULL)
8919 if (path_new_staged_content) {
8920 err = got_object_blob_create(&new_staged_blob_id,
8921 path_new_staged_content, repo);
8926 f = fopen(path_unstaged_content, "re");
8928 err = got_error_from_errno2("fopen",
8929 path_unstaged_content);
8932 if (fstat(fileno(f), &sb) == -1) {
8933 err = got_error_from_errno2("fstat", path_unstaged_content);
8936 if (got_fileindex_entry_staged_filetype_get(ie) ==
8937 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8938 char link_target[PATH_MAX];
8940 r = fread(link_target, 1, sizeof(link_target), f);
8941 if (r == 0 && ferror(f)) {
8942 err = got_error_from_errno("fread");
8945 if (r >= sizeof(link_target)) { /* should not happen */
8946 err = got_error(GOT_ERR_NO_SPACE);
8949 link_target[r] = '\0';
8950 err = merge_symlink(worktree, blob_base,
8951 ondisk_path, ie->path, label_orig, link_target,
8952 worktree->base_commit_id, repo, progress_cb,
8955 int local_changes_subsumed;
8957 err = got_path_dirname(&parent, ondisk_path);
8961 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8963 err = got_error_from_errno("asprintf");
8968 err = got_opentemp_named(&blob_base_path, &f_base,
8972 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8978 * In order the run a 3-way merge with a symlink we copy the symlink's
8979 * target path into a temporary file and use that file with diff3.
8981 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8982 err = dump_symlink_target_path_to_file(&f_deriv2,
8988 fd = open(ondisk_path,
8989 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8991 err = got_error_from_errno2("open", ondisk_path);
8994 f_deriv2 = fdopen(fd, "r");
8995 if (f_deriv2 == NULL) {
8996 err = got_error_from_errno2("fdopen", ondisk_path);
9002 err = merge_file(&local_changes_subsumed, worktree,
9003 f_base, f, f_deriv2, ondisk_path, ie->path,
9004 got_fileindex_perms_to_st(ie),
9005 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
9006 repo, progress_cb, progress_arg);
9011 if (new_staged_blob_id) {
9012 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9013 SHA1_DIGEST_LENGTH);
9015 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9016 got_fileindex_entry_staged_filetype_set(ie, 0);
9019 free(new_staged_blob_id);
9020 if (path_unstaged_content &&
9021 unlink(path_unstaged_content) == -1 && err == NULL)
9022 err = got_error_from_errno2("unlink", path_unstaged_content);
9023 if (path_new_staged_content &&
9024 unlink(path_new_staged_content) == -1 && err == NULL)
9025 err = got_error_from_errno2("unlink", path_new_staged_content);
9026 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
9027 err = got_error_from_errno2("unlink", blob_base_path);
9028 if (f_base && fclose(f_base) == EOF && err == NULL)
9029 err = got_error_from_errno2("fclose", path_unstaged_content);
9030 if (f && fclose(f) == EOF && err == NULL)
9031 err = got_error_from_errno2("fclose", path_unstaged_content);
9032 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
9033 err = got_error_from_errno2("fclose", ondisk_path);
9034 free(path_unstaged_content);
9035 free(path_new_staged_content);
9036 free(blob_base_path);
9042 static const struct got_error *
9043 unstage_path(void *arg, unsigned char status,
9044 unsigned char staged_status, const char *relpath,
9045 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9046 struct got_object_id *commit_id, int dirfd, const char *de_name)
9048 const struct got_error *err = NULL;
9049 struct unstage_path_arg *a = arg;
9050 struct got_fileindex_entry *ie;
9051 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
9052 char *ondisk_path = NULL;
9053 char *id_str = NULL, *label_orig = NULL;
9054 int local_changes_subsumed;
9056 int fd1 = -1, fd2 = -1;
9058 if (staged_status != GOT_STATUS_ADD &&
9059 staged_status != GOT_STATUS_MODIFY &&
9060 staged_status != GOT_STATUS_DELETE)
9063 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9065 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9067 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
9069 return got_error_from_errno("asprintf");
9071 err = got_object_id_str(&id_str,
9072 commit_id ? commit_id : a->worktree->base_commit_id);
9075 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9077 err = got_error_from_errno("asprintf");
9081 fd1 = got_opentempfd();
9083 err = got_error_from_errno("got_opentempfd");
9086 fd2 = got_opentempfd();
9088 err = got_error_from_errno("got_opentempfd");
9092 switch (staged_status) {
9093 case GOT_STATUS_MODIFY:
9094 err = got_object_open_as_blob(&blob_base, a->repo,
9095 blob_id, 8192, fd1);
9099 case GOT_STATUS_ADD:
9101 if (staged_status == GOT_STATUS_ADD) {
9102 int choice = GOT_PATCH_CHOICE_NONE;
9103 err = (*a->patch_cb)(&choice, a->patch_arg,
9104 staged_status, ie->path, NULL, 1, 1);
9107 if (choice != GOT_PATCH_CHOICE_YES)
9110 err = unstage_hunks(staged_blob_id,
9111 blob_base, blob_id, ie, ondisk_path,
9112 label_orig, a->worktree, a->repo,
9113 a->patch_cb, a->patch_arg,
9114 a->progress_cb, a->progress_arg);
9115 break; /* Done with this file. */
9118 err = got_object_open_as_blob(&blob_staged, a->repo,
9119 staged_blob_id, 8192, fd2);
9122 switch (got_fileindex_entry_staged_filetype_get(ie)) {
9123 case GOT_FILEIDX_MODE_BAD_SYMLINK:
9124 case GOT_FILEIDX_MODE_REGULAR_FILE:
9125 err = merge_blob(&local_changes_subsumed, a->worktree,
9126 blob_base, ondisk_path, relpath,
9127 got_fileindex_perms_to_st(ie), label_orig,
9128 blob_staged, commit_id ? commit_id :
9129 a->worktree->base_commit_id, a->repo,
9130 a->progress_cb, a->progress_arg);
9132 case GOT_FILEIDX_MODE_SYMLINK:
9133 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9134 char *staged_target;
9135 err = got_object_blob_read_to_str(
9136 &staged_target, blob_staged);
9139 err = merge_symlink(a->worktree, blob_base,
9140 ondisk_path, relpath, label_orig,
9141 staged_target, commit_id ? commit_id :
9142 a->worktree->base_commit_id,
9143 a->repo, a->progress_cb, a->progress_arg);
9144 free(staged_target);
9146 err = merge_blob(&local_changes_subsumed,
9147 a->worktree, blob_base, ondisk_path,
9148 relpath, got_fileindex_perms_to_st(ie),
9149 label_orig, blob_staged,
9150 commit_id ? commit_id :
9151 a->worktree->base_commit_id, a->repo,
9152 a->progress_cb, a->progress_arg);
9156 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9160 got_fileindex_entry_stage_set(ie,
9161 GOT_FILEIDX_STAGE_NONE);
9162 got_fileindex_entry_staged_filetype_set(ie, 0);
9165 case GOT_STATUS_DELETE:
9167 int choice = GOT_PATCH_CHOICE_NONE;
9168 err = (*a->patch_cb)(&choice, a->patch_arg,
9169 staged_status, ie->path, NULL, 1, 1);
9172 if (choice == GOT_PATCH_CHOICE_NO)
9174 if (choice != GOT_PATCH_CHOICE_YES) {
9175 err = got_error(GOT_ERR_PATCH_CHOICE);
9179 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9180 got_fileindex_entry_staged_filetype_set(ie, 0);
9181 err = get_file_status(&status, &sb, ie, ondisk_path,
9182 dirfd, de_name, a->repo);
9185 err = (*a->progress_cb)(a->progress_arg, status, relpath);
9190 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9191 err = got_error_from_errno("close");
9193 got_object_blob_close(blob_base);
9194 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9195 err = got_error_from_errno("close");
9197 got_object_blob_close(blob_staged);
9203 const struct got_error *
9204 got_worktree_unstage(struct got_worktree *worktree,
9205 struct got_pathlist_head *paths,
9206 got_worktree_checkout_cb progress_cb, void *progress_arg,
9207 got_worktree_patch_cb patch_cb, void *patch_arg,
9208 struct got_repository *repo)
9210 const struct got_error *err = NULL, *sync_err, *unlockerr;
9211 struct got_pathlist_entry *pe;
9212 struct got_fileindex *fileindex = NULL;
9213 char *fileindex_path = NULL;
9214 struct unstage_path_arg upa;
9216 err = lock_worktree(worktree, LOCK_EX);
9220 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9224 upa.worktree = worktree;
9225 upa.fileindex = fileindex;
9227 upa.progress_cb = progress_cb;
9228 upa.progress_arg = progress_arg;
9229 upa.patch_cb = patch_cb;
9230 upa.patch_arg = patch_arg;
9231 TAILQ_FOREACH(pe, paths, entry) {
9232 err = worktree_status(worktree, pe->path, fileindex, repo,
9233 unstage_path, &upa, NULL, NULL, 1, 0);
9238 sync_err = sync_fileindex(fileindex, fileindex_path);
9239 if (sync_err && err == NULL)
9242 free(fileindex_path);
9244 got_fileindex_free(fileindex);
9245 unlockerr = lock_worktree(worktree, LOCK_SH);
9246 if (unlockerr && err == NULL)
9251 struct report_file_info_arg {
9252 struct got_worktree *worktree;
9253 got_worktree_path_info_cb info_cb;
9255 struct got_pathlist_head *paths;
9256 got_cancel_cb cancel_cb;
9260 static const struct got_error *
9261 report_file_info(void *arg, struct got_fileindex_entry *ie)
9263 struct report_file_info_arg *a = arg;
9264 struct got_pathlist_entry *pe;
9265 struct got_object_id blob_id, staged_blob_id, commit_id;
9266 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9267 struct got_object_id *commit_idp = NULL;
9270 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9271 return got_error(GOT_ERR_CANCELLED);
9273 TAILQ_FOREACH(pe, a->paths, entry) {
9274 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9275 got_path_is_child(ie->path, pe->path, pe->path_len))
9278 if (pe == NULL) /* not found */
9281 if (got_fileindex_entry_has_blob(ie))
9282 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9283 stage = got_fileindex_entry_stage_get(ie);
9284 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9285 stage == GOT_FILEIDX_STAGE_ADD) {
9286 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9287 &staged_blob_id, ie);
9290 if (got_fileindex_entry_has_commit(ie))
9291 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9293 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9294 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9297 const struct got_error *
9298 got_worktree_path_info(struct got_worktree *worktree,
9299 struct got_pathlist_head *paths,
9300 got_worktree_path_info_cb info_cb, void *info_arg,
9301 got_cancel_cb cancel_cb, void *cancel_arg)
9304 const struct got_error *err = NULL, *unlockerr;
9305 struct got_fileindex *fileindex = NULL;
9306 char *fileindex_path = NULL;
9307 struct report_file_info_arg arg;
9309 err = lock_worktree(worktree, LOCK_SH);
9313 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9317 arg.worktree = worktree;
9318 arg.info_cb = info_cb;
9319 arg.info_arg = info_arg;
9321 arg.cancel_cb = cancel_cb;
9322 arg.cancel_arg = cancel_arg;
9323 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9326 free(fileindex_path);
9328 got_fileindex_free(fileindex);
9329 unlockerr = lock_worktree(worktree, LOCK_UN);
9330 if (unlockerr && err == NULL)
9335 static const struct got_error *
9336 patch_check_path(const char *p, char **path, unsigned char *status,
9337 unsigned char *staged_status, struct got_fileindex *fileindex,
9338 struct got_worktree *worktree, struct got_repository *repo)
9340 const struct got_error *err;
9341 struct got_fileindex_entry *ie;
9343 char *ondisk_path = NULL;
9345 err = got_worktree_resolve_path(path, worktree, p);
9349 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9350 *path[0] ? "/" : "", *path) == -1)
9351 return got_error_from_errno("asprintf");
9353 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9355 *staged_status = get_staged_status(ie);
9356 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9361 *staged_status = GOT_STATUS_NO_CHANGE;
9362 *status = GOT_STATUS_UNVERSIONED;
9363 if (lstat(ondisk_path, &sb) == -1) {
9364 if (errno != ENOENT) {
9365 err = got_error_from_errno2("lstat",
9369 *status = GOT_STATUS_NONEXISTENT;
9378 static const struct got_error *
9379 patch_can_rm(const char *path, unsigned char status,
9380 unsigned char staged_status)
9382 if (status == GOT_STATUS_NONEXISTENT)
9383 return got_error_set_errno(ENOENT, path);
9384 if (status != GOT_STATUS_NO_CHANGE &&
9385 status != GOT_STATUS_ADD &&
9386 status != GOT_STATUS_MODIFY &&
9387 status != GOT_STATUS_MODE_CHANGE)
9388 return got_error_path(path, GOT_ERR_FILE_STATUS);
9389 if (staged_status == GOT_STATUS_DELETE)
9390 return got_error_path(path, GOT_ERR_FILE_STATUS);
9394 static const struct got_error *
9395 patch_can_add(const char *path, unsigned char status)
9397 if (status != GOT_STATUS_NONEXISTENT)
9398 return got_error_path(path, GOT_ERR_FILE_STATUS);
9402 static const struct got_error *
9403 patch_can_edit(const char *path, unsigned char status,
9404 unsigned char staged_status)
9406 if (status == GOT_STATUS_NONEXISTENT)
9407 return got_error_set_errno(ENOENT, path);
9408 if (status != GOT_STATUS_NO_CHANGE &&
9409 status != GOT_STATUS_ADD &&
9410 status != GOT_STATUS_MODIFY)
9411 return got_error_path(path, GOT_ERR_FILE_STATUS);
9412 if (staged_status == GOT_STATUS_DELETE)
9413 return got_error_path(path, GOT_ERR_FILE_STATUS);
9417 const struct got_error *
9418 got_worktree_patch_prepare(struct got_fileindex **fileindex,
9419 char **fileindex_path, struct got_worktree *worktree)
9421 return open_fileindex(fileindex, fileindex_path, worktree);
9424 const struct got_error *
9425 got_worktree_patch_check_path(const char *old, const char *new,
9426 char **oldpath, char **newpath, struct got_worktree *worktree,
9427 struct got_repository *repo, struct got_fileindex *fileindex)
9429 const struct got_error *err = NULL;
9430 int file_renamed = 0;
9431 unsigned char status_old, staged_status_old;
9432 unsigned char status_new, staged_status_new;
9437 err = patch_check_path(old != NULL ? old : new, oldpath,
9438 &status_old, &staged_status_old, fileindex, worktree, repo);
9442 err = patch_check_path(new != NULL ? new : old, newpath,
9443 &status_new, &staged_status_new, fileindex, worktree, repo);
9447 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9450 if (old != NULL && new == NULL)
9451 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9452 else if (file_renamed) {
9453 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9455 err = patch_can_add(*newpath, status_new);
9456 } else if (old == NULL)
9457 err = patch_can_add(*newpath, status_new);
9459 err = patch_can_edit(*newpath, status_new, staged_status_new);
9471 const struct got_error *
9472 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9473 struct got_worktree *worktree, struct got_fileindex *fileindex,
9474 got_worktree_checkout_cb progress_cb, void *progress_arg)
9476 struct schedule_addition_args saa;
9478 memset(&saa, 0, sizeof(saa));
9479 saa.worktree = worktree;
9480 saa.fileindex = fileindex;
9481 saa.progress_cb = progress_cb;
9482 saa.progress_arg = progress_arg;
9485 return worktree_status(worktree, path, fileindex, repo,
9486 schedule_addition, &saa, NULL, NULL, 1, 0);
9489 const struct got_error *
9490 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9491 struct got_worktree *worktree, struct got_fileindex *fileindex,
9492 got_worktree_delete_cb progress_cb, void *progress_arg)
9494 struct schedule_deletion_args sda;
9496 memset(&sda, 0, sizeof(sda));
9497 sda.worktree = worktree;
9498 sda.fileindex = fileindex;
9499 sda.progress_cb = progress_cb;
9500 sda.progress_arg = progress_arg;
9502 sda.delete_local_mods = 0;
9503 sda.keep_on_disk = 0;
9504 sda.ignore_missing_paths = 0;
9505 sda.status_codes = NULL;
9507 return worktree_status(worktree, path, fileindex, repo,
9508 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9511 const struct got_error *
9512 got_worktree_patch_complete(struct got_fileindex *fileindex,
9513 const char *fileindex_path)
9515 const struct got_error *err = NULL;
9517 err = sync_fileindex(fileindex, fileindex_path);
9518 got_fileindex_free(fileindex);