2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/queue.h>
39 #include "got_error.h"
40 #include "got_repository.h"
41 #include "got_reference.h"
42 #include "got_object.h"
44 #include "got_cancel.h"
45 #include "got_worktree.h"
46 #include "got_opentemp.h"
49 #include "got_lib_worktree.h"
50 #include "got_lib_hash.h"
51 #include "got_lib_fileindex.h"
52 #include "got_lib_inflate.h"
53 #include "got_lib_delta.h"
54 #include "got_lib_object.h"
55 #include "got_lib_object_parse.h"
56 #include "got_lib_object_create.h"
57 #include "got_lib_object_idset.h"
58 #include "got_lib_diff.h"
59 #include "got_lib_gotconfig.h"
62 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
65 #define GOT_MERGE_LABEL_MERGED "merged change"
66 #define GOT_MERGE_LABEL_BASE "3-way merge base"
68 static mode_t apply_umask(mode_t);
70 static const struct got_error *
71 create_meta_file(const char *path_got, const char *name, const char *content)
73 const struct got_error *err = NULL;
76 if (asprintf(&path, "%s/%s", path_got, name) == -1)
77 return got_error_from_errno("asprintf");
79 err = got_path_create_file(path, content);
84 static const struct got_error *
85 update_meta_file(const char *path_got, const char *name, const char *content)
87 const struct got_error *err = NULL;
92 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
93 err = got_error_from_errno("asprintf");
98 err = got_opentemp_named(&tmppath, &tmpfile, path, "");
103 int len = fprintf(tmpfile, "%s\n", content);
104 if (len != strlen(content) + 1) {
105 err = got_error_from_errno2("fprintf", tmppath);
110 if (rename(tmppath, path) != 0) {
111 err = got_error_from_errno3("rename", tmppath, path);
117 if (fclose(tmpfile) == EOF && err == NULL)
118 err = got_error_from_errno2("fclose", tmppath);
123 static const struct got_error *
124 write_head_ref(const char *path_got, struct got_reference *head_ref)
126 const struct got_error *err = NULL;
129 if (got_ref_is_symbolic(head_ref)) {
130 refstr = got_ref_to_str(head_ref);
132 return got_error_from_errno("got_ref_to_str");
134 refstr = strdup(got_ref_get_name(head_ref));
136 return got_error_from_errno("strdup");
138 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
143 const struct got_error *
144 got_worktree_init(const char *path, struct got_reference *head_ref,
145 const char *prefix, struct got_repository *repo)
147 const struct got_error *err = NULL;
148 struct got_object_id *commit_id = NULL;
150 uint32_t uuid_status;
152 char *path_got = NULL;
153 char *formatstr = NULL;
154 char *absprefix = NULL;
155 char *basestr = NULL;
156 char *uuidstr = NULL;
158 if (strcmp(path, got_repo_get_path(repo)) == 0) {
159 err = got_error(GOT_ERR_WORKTREE_REPO);
163 err = got_ref_resolve(&commit_id, repo, head_ref);
166 err = got_object_get_type(&obj_type, repo, commit_id);
169 if (obj_type != GOT_OBJ_TYPE_COMMIT)
170 return got_error(GOT_ERR_OBJ_TYPE);
172 if (!got_path_is_absolute(prefix)) {
173 if (asprintf(&absprefix, "/%s", prefix) == -1)
174 return got_error_from_errno("asprintf");
177 /* Create top-level directory (may already exist). */
178 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
179 err = got_error_from_errno2("mkdir", path);
183 /* Create .got directory (may already exist). */
184 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
185 err = got_error_from_errno("asprintf");
188 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
189 err = got_error_from_errno2("mkdir", path_got);
193 /* Create an empty lock file. */
194 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
198 /* Create an empty file index. */
199 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
203 /* Write the HEAD reference. */
204 err = write_head_ref(path_got, head_ref);
208 /* Record our base commit. */
209 err = got_object_id_str(&basestr, commit_id);
212 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
216 /* Store path to repository. */
217 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
218 got_repo_get_path(repo));
222 /* Store in-repository path prefix. */
223 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
224 absprefix ? absprefix : prefix);
229 uuid_create(&uuid, &uuid_status);
230 if (uuid_status != uuid_s_ok) {
231 err = got_error_uuid(uuid_status, "uuid_create");
234 uuid_to_string(&uuid, &uuidstr, &uuid_status);
235 if (uuid_status != uuid_s_ok) {
236 err = got_error_uuid(uuid_status, "uuid_to_string");
239 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
243 /* Stamp work tree with format file. */
244 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
245 err = got_error_from_errno("asprintf");
248 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
262 const struct got_error *
263 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
264 const char *path_prefix)
266 char *absprefix = NULL;
268 if (!got_path_is_absolute(path_prefix)) {
269 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
270 return got_error_from_errno("asprintf");
272 *match = (strcmp(absprefix ? absprefix : path_prefix,
273 worktree->path_prefix) == 0);
279 got_worktree_get_head_ref_name(struct got_worktree *worktree)
281 return worktree->head_ref_name;
284 const struct got_error *
285 got_worktree_set_head_ref(struct got_worktree *worktree,
286 struct got_reference *head_ref)
288 const struct got_error *err = NULL;
289 char *path_got = NULL, *head_ref_name = NULL;
291 if (asprintf(&path_got, "%s/%s", worktree->root_path,
292 GOT_WORKTREE_GOT_DIR) == -1) {
293 err = got_error_from_errno("asprintf");
298 head_ref_name = strdup(got_ref_get_name(head_ref));
299 if (head_ref_name == NULL) {
300 err = got_error_from_errno("strdup");
304 err = write_head_ref(path_got, head_ref);
308 free(worktree->head_ref_name);
309 worktree->head_ref_name = head_ref_name;
317 struct got_object_id *
318 got_worktree_get_base_commit_id(struct got_worktree *worktree)
320 return worktree->base_commit_id;
323 const struct got_error *
324 got_worktree_set_base_commit_id(struct got_worktree *worktree,
325 struct got_repository *repo, struct got_object_id *commit_id)
327 const struct got_error *err;
328 struct got_object *obj = NULL;
330 char *path_got = NULL;
332 if (asprintf(&path_got, "%s/%s", worktree->root_path,
333 GOT_WORKTREE_GOT_DIR) == -1) {
334 err = got_error_from_errno("asprintf");
339 err = got_object_open(&obj, repo, commit_id);
343 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
344 err = got_error(GOT_ERR_OBJ_TYPE);
348 /* Record our base commit. */
349 err = got_object_id_str(&id_str, commit_id);
352 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
356 free(worktree->base_commit_id);
357 worktree->base_commit_id = got_object_id_dup(commit_id);
358 if (worktree->base_commit_id == NULL) {
359 err = got_error_from_errno("got_object_id_dup");
364 got_object_close(obj);
370 const struct got_gotconfig *
371 got_worktree_get_gotconfig(struct got_worktree *worktree)
373 return worktree->gotconfig;
376 static const struct got_error *
377 lock_worktree(struct got_worktree *worktree, int operation)
379 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
380 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
381 : got_error_from_errno2("flock",
382 got_worktree_get_root_path(worktree)));
386 static const struct got_error *
387 add_dir_on_disk(struct got_worktree *worktree, const char *path)
389 const struct got_error *err = NULL;
392 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
393 return got_error_from_errno("asprintf");
395 err = got_path_mkdir(abspath);
396 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
399 if (lstat(abspath, &sb) == -1) {
400 err = got_error_from_errno2("lstat", abspath);
401 } else if (!S_ISDIR(sb.st_mode)) {
402 /* TODO directory is obstructed; do something */
403 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
410 static const struct got_error *
411 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
413 const struct got_error *err = NULL;
416 size_t flen1 = 0, flen2 = 0;
421 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
422 if (flen1 == 0 && ferror(f1)) {
423 err = got_error_from_errno("fread");
426 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
427 if (flen2 == 0 && ferror(f2)) {
428 err = got_error_from_errno("fread");
435 } else if (flen2 == 0) {
439 } else if (flen1 == flen2) {
440 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
453 static const struct got_error *
454 check_files_equal(int *same, FILE *f1, FILE *f2)
461 if (fstat(fileno(f1), &sb) != 0)
462 return got_error_from_errno("fstat");
465 if (fstat(fileno(f2), &sb) != 0)
466 return got_error_from_errno("fstat");
469 if (size1 != size2) {
474 if (fseek(f1, 0L, SEEK_SET) == -1)
475 return got_ferror(f1, GOT_ERR_IO);
476 if (fseek(f2, 0L, SEEK_SET) == -1)
477 return got_ferror(f2, GOT_ERR_IO);
479 return check_file_contents_equal(same, f1, f2);
482 static const struct got_error *
483 copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
491 if (fseek(f, 0L, SEEK_SET) == -1)
492 return got_ferror(f, GOT_ERR_IO);
495 flen = fread(fbuf, 1, sizeof(fbuf), f);
498 return got_error_from_errno("fread");
502 outlen = write(outfd, fbuf, flen);
504 return got_error_from_errno("write");
506 return got_error(GOT_ERR_IO);
513 static const struct got_error *
514 merge_binary_file(int *overlapcnt, int merged_fd,
515 FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
516 const char *label_deriv, const char *label_orig, const char *label_deriv2,
517 const char *ondisk_path)
519 const struct got_error *err = NULL;
520 int same_content, changed_deriv, changed_deriv2;
521 int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
522 off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
523 char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
524 char *base_path_orig = NULL, *base_path_deriv = NULL;
525 char *base_path_deriv2 = NULL;
529 err = check_files_equal(&same_content, f_deriv, f_deriv2);
534 return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
536 err = check_files_equal(&same_content, f_deriv, f_orig);
539 changed_deriv = !same_content;
540 err = check_files_equal(&same_content, f_deriv2, f_orig);
543 changed_deriv2 = !same_content;
545 if (changed_deriv && changed_deriv2) {
547 if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
548 err = got_error_from_errno("asprintf");
551 if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
552 err = got_error_from_errno("asprintf");
555 if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
556 err = got_error_from_errno("asprintf");
559 err = got_opentemp_named_fd(&path_orig, &fd_orig,
563 err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
564 base_path_deriv, "");
567 err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
568 base_path_deriv2, "");
571 err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
574 err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
577 err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
580 if (dprintf(merged_fd, "Binary files differ and cannot be "
581 "merged automatically:\n") < 0) {
582 err = got_error_from_errno("dprintf");
585 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
586 GOT_DIFF_CONFLICT_MARKER_BEGIN,
587 label_deriv ? " " : "",
588 label_deriv ? label_deriv : "",
590 err = got_error_from_errno("dprintf");
594 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
595 GOT_DIFF_CONFLICT_MARKER_ORIG,
596 label_orig ? " " : "",
597 label_orig ? label_orig : "",
599 err = got_error_from_errno("dprintf");
603 if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
604 GOT_DIFF_CONFLICT_MARKER_SEP,
606 GOT_DIFF_CONFLICT_MARKER_END,
607 label_deriv2 ? " " : "",
608 label_deriv2 ? label_deriv2 : "") < 0) {
609 err = got_error_from_errno("dprintf");
612 } else if (changed_deriv)
613 err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
614 else if (changed_deriv2)
615 err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
617 if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
619 err = got_error_from_errno2("unlink", path_orig);
620 if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
621 err = got_error_from_errno2("close", path_orig);
622 if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
623 err = got_error_from_errno2("close", path_deriv);
624 if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
625 err = got_error_from_errno2("close", path_deriv2);
629 free(base_path_orig);
630 free(base_path_deriv);
631 free(base_path_deriv2);
636 * Perform a 3-way merge where the file f_orig acts as the common
637 * ancestor, the file f_deriv acts as the first derived version,
638 * and the file f_deriv2 acts as the second derived version.
639 * The merge result will be written to a new file at ondisk_path; any
640 * existing file at this path will be replaced.
642 static const struct got_error *
643 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
644 FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
645 const char *path, uint16_t st_mode,
646 const char *label_orig, const char *label_deriv, const char *label_deriv2,
647 enum got_diff_algorithm diff_algo, struct got_repository *repo,
648 got_worktree_checkout_cb progress_cb, void *progress_arg)
650 const struct got_error *err = NULL;
652 FILE *f_merged = NULL;
653 char *merged_path = NULL, *base_path = NULL;
657 *local_changes_subsumed = 0;
659 err = got_path_dirname(&parent, ondisk_path);
663 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
664 err = got_error_from_errno("asprintf");
668 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path, "");
672 err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
673 f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
675 if (err->code != GOT_ERR_FILE_BINARY)
677 err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
678 f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
684 err = (*progress_cb)(progress_arg,
685 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
689 if (fsync(merged_fd) != 0) {
690 err = got_error_from_errno("fsync");
694 f_merged = fdopen(merged_fd, "r");
695 if (f_merged == NULL) {
696 err = got_error_from_errno("fdopen");
701 /* Check if a clean merge has subsumed all local changes. */
702 if (overlapcnt == 0) {
703 err = check_files_equal(local_changes_subsumed, f_deriv,
709 if (fchmod(fileno(f_merged), apply_umask(st_mode)) != 0) {
710 err = got_error_from_errno2("fchmod", merged_path);
714 if (rename(merged_path, ondisk_path) != 0) {
715 err = got_error_from_errno3("rename", merged_path,
724 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
725 err = got_error_from_errno("close");
726 if (f_merged && fclose(f_merged) == EOF && err == NULL)
727 err = got_error_from_errno("fclose");
734 static const struct got_error *
735 update_symlink(const char *ondisk_path, const char *target_path,
738 /* This is not atomic but matches what 'ln -sf' does. */
739 if (unlink(ondisk_path) == -1)
740 return got_error_from_errno2("unlink", ondisk_path);
741 if (symlink(target_path, ondisk_path) == -1)
742 return got_error_from_errno3("symlink", target_path,
748 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
749 * in the work tree with a file that contains conflict markers and the
750 * conflicting target paths of the original version, a "derived version"
751 * of a symlink from an incoming change, and a local version of the symlink.
753 * The original versions's target path can be NULL if it is not available,
754 * such as if both derived versions added a new symlink at the same path.
756 * The incoming derived symlink target is NULL in case the incoming change
757 * has deleted this symlink.
759 static const struct got_error *
760 install_symlink_conflict(const char *deriv_target,
761 struct got_object_id *deriv_base_commit_id, const char *orig_target,
762 const char *label_orig, const char *local_target, const char *ondisk_path)
764 const struct got_error *err;
765 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
768 err = got_object_id_str(&id_str, deriv_base_commit_id);
770 return got_error_from_errno("asprintf");
772 if (asprintf(&label_deriv, "%s: commit %s",
773 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
774 err = got_error_from_errno("asprintf");
778 err = got_opentemp_named(&path, &f, "got-symlink-conflict", "");
782 if (fchmod(fileno(f), apply_umask(GOT_DEFAULT_FILE_MODE)) == -1) {
783 err = got_error_from_errno2("fchmod", path);
787 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
788 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
789 deriv_target ? deriv_target : "(symlink was deleted)",
790 orig_target ? label_orig : "",
791 orig_target ? "\n" : "",
792 orig_target ? orig_target : "",
793 orig_target ? "\n" : "",
794 GOT_DIFF_CONFLICT_MARKER_SEP,
795 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
796 err = got_error_from_errno2("fprintf", path);
800 if (unlink(ondisk_path) == -1) {
801 err = got_error_from_errno2("unlink", ondisk_path);
804 if (rename(path, ondisk_path) == -1) {
805 err = got_error_from_errno3("rename", path, ondisk_path);
809 if (f != NULL && fclose(f) == EOF && err == NULL)
810 err = got_error_from_errno2("fclose", path);
817 /* forward declaration */
818 static const struct got_error *
819 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
820 const char *, const char *, uint16_t, const char *,
821 struct got_blob_object *, struct got_object_id *,
822 struct got_repository *, got_worktree_checkout_cb, void *);
825 * Merge a symlink into the work tree, where blob_orig acts as the common
826 * ancestor, deriv_target is the link target of the first derived version,
827 * and the symlink on disk acts as the second derived version.
828 * Assume that contents of both blobs represent symlinks.
830 static const struct got_error *
831 merge_symlink(struct got_worktree *worktree,
832 struct got_blob_object *blob_orig, const char *ondisk_path,
833 const char *path, const char *label_orig, const char *deriv_target,
834 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
835 got_worktree_checkout_cb progress_cb, void *progress_arg)
837 const struct got_error *err = NULL;
838 char *ancestor_target = NULL;
840 ssize_t ondisk_len, deriv_len;
841 char ondisk_target[PATH_MAX];
842 int have_local_change = 0;
843 int have_incoming_change = 0;
845 if (lstat(ondisk_path, &sb) == -1)
846 return got_error_from_errno2("lstat", ondisk_path);
848 ondisk_len = readlink(ondisk_path, ondisk_target,
849 sizeof(ondisk_target));
850 if (ondisk_len == -1) {
851 err = got_error_from_errno2("readlink",
855 ondisk_target[ondisk_len] = '\0';
858 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
863 if (ancestor_target == NULL ||
864 (ondisk_len != strlen(ancestor_target) ||
865 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
866 have_local_change = 1;
868 deriv_len = strlen(deriv_target);
869 if (ancestor_target == NULL ||
870 (deriv_len != strlen(ancestor_target) ||
871 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
872 have_incoming_change = 1;
874 if (!have_local_change && !have_incoming_change) {
875 if (ancestor_target) {
876 /* Both sides made the same change. */
877 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
879 } else if (deriv_len == ondisk_len &&
880 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
881 /* Both sides added the same symlink. */
882 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
885 /* Both sides added symlinks which don't match. */
886 err = install_symlink_conflict(deriv_target,
887 deriv_base_commit_id, ancestor_target,
888 label_orig, ondisk_target, ondisk_path);
891 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
894 } else if (!have_local_change && have_incoming_change) {
895 /* Apply the incoming change. */
896 err = update_symlink(ondisk_path, deriv_target,
897 strlen(deriv_target));
900 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
901 } else if (have_local_change && have_incoming_change) {
902 if (deriv_len == ondisk_len &&
903 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
904 /* Both sides made the same change. */
905 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
908 err = install_symlink_conflict(deriv_target,
909 deriv_base_commit_id, ancestor_target, label_orig,
910 ondisk_target, ondisk_path);
913 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
919 free(ancestor_target);
923 static const struct got_error *
924 dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
926 const struct got_error *err = NULL;
927 char target_path[PATH_MAX];
936 return got_error_from_errno("got_opentemp");
937 target_len = readlink(ondisk_path, target_path, sizeof(target_path));
938 if (target_len == -1) {
939 err = got_error_from_errno2("readlink", ondisk_path);
942 n = fwrite(target_path, 1, target_len, f);
943 if (n != target_len) {
944 err = got_ferror(f, GOT_ERR_IO);
947 if (fflush(f) == EOF) {
948 err = got_error_from_errno("fflush");
951 if (fseek(f, 0L, SEEK_SET) == -1) {
952 err = got_ferror(f, GOT_ERR_IO);
964 * Perform a 3-way merge where blob_orig acts as the common ancestor,
965 * blob_deriv acts as the first derived version, and the file on disk
966 * acts as the second derived version.
968 static const struct got_error *
969 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
970 struct got_blob_object *blob_orig, const char *ondisk_path,
971 const char *path, uint16_t st_mode, const char *label_orig,
972 struct got_blob_object *blob_deriv,
973 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
974 got_worktree_checkout_cb progress_cb, void *progress_arg)
976 const struct got_error *err = NULL;
977 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
978 char *blob_orig_path = NULL;
979 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
980 char *label_deriv = NULL, *parent = NULL;
982 *local_changes_subsumed = 0;
984 err = got_path_dirname(&parent, ondisk_path);
989 if (asprintf(&base_path, "%s/got-merge-blob-orig",
991 err = got_error_from_errno("asprintf");
996 err = got_opentemp_named(&blob_orig_path, &f_orig,
1000 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1007 * No common ancestor exists. This is an "add vs add" conflict
1008 * and we simply use an empty ancestor file to make both files
1009 * appear in the merged result in their entirety.
1011 f_orig = got_opentemp();
1012 if (f_orig == NULL) {
1013 err = got_error_from_errno("got_opentemp");
1018 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1019 err = got_error_from_errno("asprintf");
1024 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path, "");
1027 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1032 err = got_object_id_str(&id_str, deriv_base_commit_id);
1035 if (asprintf(&label_deriv, "%s: commit %s",
1036 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1037 err = got_error_from_errno("asprintf");
1042 * In order the run a 3-way merge with a symlink we copy the symlink's
1043 * target path into a temporary file and use that file with diff3.
1045 if (S_ISLNK(st_mode)) {
1046 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1051 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1053 err = got_error_from_errno2("open", ondisk_path);
1056 f_deriv2 = fdopen(fd, "r");
1057 if (f_deriv2 == NULL) {
1058 err = got_error_from_errno2("fdopen", ondisk_path);
1064 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1065 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1066 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1068 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1069 err = got_error_from_errno("fclose");
1070 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1071 err = got_error_from_errno("fclose");
1072 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1073 err = got_error_from_errno("fclose");
1075 if (blob_orig_path) {
1076 unlink(blob_orig_path);
1077 free(blob_orig_path);
1079 if (blob_deriv_path) {
1080 unlink(blob_deriv_path);
1081 free(blob_deriv_path);
1089 static const struct got_error *
1090 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1091 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1092 int wt_fd, const char *path, struct got_object_id *blob_id)
1094 const struct got_error *err = NULL;
1095 struct got_fileindex_entry *new_ie;
1099 err = got_fileindex_entry_alloc(&new_ie, path);
1103 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1104 blob_id->hash, base_commit_id->hash, 1);
1108 err = got_fileindex_entry_add(fileindex, new_ie);
1111 got_fileindex_entry_free(new_ie);
1118 get_ondisk_perms(int executable, mode_t st_mode)
1120 mode_t xbits = S_IXUSR;
1123 /* Map read bits to execute bits. */
1124 if (st_mode & S_IRGRP)
1126 if (st_mode & S_IROTH)
1128 return st_mode | xbits;
1135 apply_umask(mode_t mode)
1144 /* forward declaration */
1145 static const struct got_error *
1146 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1147 const char *path, mode_t te_mode, mode_t st_mode,
1148 struct got_blob_object *blob, int restoring_missing_file,
1149 int reverting_versioned_file, int installing_bad_symlink,
1150 int path_is_unversioned, struct got_repository *repo,
1151 got_worktree_checkout_cb progress_cb, void *progress_arg);
1154 * This function assumes that the provided symlink target points at a
1155 * safe location in the work tree!
1157 static const struct got_error *
1158 replace_existing_symlink(int *did_something, const char *ondisk_path,
1159 const char *target_path, size_t target_len)
1161 const struct got_error *err = NULL;
1163 char etarget[PATH_MAX];
1169 * "Bad" symlinks (those pointing outside the work tree or into the
1170 * .got directory) are installed in the work tree as a regular file
1171 * which contains the bad symlink target path.
1172 * The new symlink target has already been checked for safety by our
1173 * caller. If we can successfully open a regular file then we simply
1174 * replace this file with a symlink below.
1176 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1178 if (!got_err_open_nofollow_on_symlink())
1179 return got_error_from_errno2("open", ondisk_path);
1181 /* We are updating an existing on-disk symlink. */
1182 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1184 return got_error_from_errno2("readlink", ondisk_path);
1186 if (elen == target_len &&
1187 memcmp(etarget, target_path, target_len) == 0)
1188 return NULL; /* nothing to do */
1192 err = update_symlink(ondisk_path, target_path, target_len);
1193 if (fd != -1 && close(fd) == -1 && err == NULL)
1194 err = got_error_from_errno2("close", ondisk_path);
1198 static const struct got_error *
1199 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1200 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1202 const struct got_error *err = NULL;
1203 char canonpath[PATH_MAX];
1204 char *path_got = NULL;
1206 *is_bad_symlink = 0;
1208 if (target_len >= sizeof(canonpath)) {
1209 *is_bad_symlink = 1;
1214 * We do not use realpath(3) to resolve the symlink's target
1215 * path because we don't want to resolve symlinks recursively.
1216 * Instead we make the path absolute and then canonicalize it.
1217 * Relative symlink target lookup should begin at the directory
1218 * in which the blob object is being installed.
1220 if (!got_path_is_absolute(target_path)) {
1221 char *abspath, *parent;
1222 err = got_path_dirname(&parent, ondisk_path);
1225 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1227 return got_error_from_errno("asprintf");
1230 if (strlen(abspath) >= sizeof(canonpath)) {
1231 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1235 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1240 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1245 /* Only allow symlinks pointing at paths within the work tree. */
1246 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1247 *is_bad_symlink = 1;
1251 /* Do not allow symlinks pointing into the .got directory. */
1252 if (asprintf(&path_got, "%s/%s", wtroot_path,
1253 GOT_WORKTREE_GOT_DIR) == -1)
1254 return got_error_from_errno("asprintf");
1255 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1256 *is_bad_symlink = 1;
1262 static const struct got_error *
1263 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1264 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1265 int restoring_missing_file, int reverting_versioned_file,
1266 int path_is_unversioned, int allow_bad_symlinks,
1267 struct got_repository *repo,
1268 got_worktree_checkout_cb progress_cb, void *progress_arg)
1270 const struct got_error *err = NULL;
1271 char target_path[PATH_MAX];
1272 size_t len, target_len = 0;
1273 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1274 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1276 *is_bad_symlink = 0;
1279 * Blob object content specifies the target path of the link.
1280 * If a symbolic link cannot be installed we instead create
1281 * a regular file which contains the link target path stored
1282 * in the blob object.
1285 err = got_object_blob_read_block(&len, blob);
1289 if (len + target_len >= sizeof(target_path)) {
1290 /* Path too long; install as a regular file. */
1291 *is_bad_symlink = 1;
1292 got_object_blob_rewind(blob);
1293 return install_blob(worktree, ondisk_path, path,
1294 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1295 restoring_missing_file, reverting_versioned_file,
1296 1, path_is_unversioned, repo, progress_cb,
1300 /* Skip blob object header first time around. */
1301 memcpy(target_path + target_len, buf + hdrlen,
1303 target_len += len - hdrlen;
1307 target_path[target_len] = '\0';
1309 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1310 ondisk_path, worktree->root_path);
1314 if (*is_bad_symlink && !allow_bad_symlinks) {
1315 /* install as a regular file */
1316 got_object_blob_rewind(blob);
1317 err = install_blob(worktree, ondisk_path, path,
1318 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1319 restoring_missing_file, reverting_versioned_file, 1,
1320 path_is_unversioned, repo, progress_cb, progress_arg);
1324 if (symlink(target_path, ondisk_path) == -1) {
1325 if (errno == EEXIST) {
1326 int symlink_replaced;
1327 if (path_is_unversioned) {
1328 err = (*progress_cb)(progress_arg,
1329 GOT_STATUS_UNVERSIONED, path);
1332 err = replace_existing_symlink(&symlink_replaced,
1333 ondisk_path, target_path, target_len);
1337 if (symlink_replaced) {
1338 err = (*progress_cb)(progress_arg,
1339 reverting_versioned_file ?
1341 GOT_STATUS_UPDATE, path);
1343 err = (*progress_cb)(progress_arg,
1344 GOT_STATUS_EXISTS, path);
1347 return err; /* Nothing else to do. */
1350 if (errno == ENOENT) {
1352 err = got_path_dirname(&parent, ondisk_path);
1355 err = add_dir_on_disk(worktree, parent);
1360 * Retry, and fall through to error handling
1361 * below if this second attempt fails.
1363 if (symlink(target_path, ondisk_path) != -1) {
1364 err = NULL; /* success */
1369 /* Handle errors from first or second creation attempt. */
1370 if (errno == ENAMETOOLONG) {
1371 /* bad target path; install as a regular file */
1372 *is_bad_symlink = 1;
1373 got_object_blob_rewind(blob);
1374 err = install_blob(worktree, ondisk_path, path,
1375 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1376 restoring_missing_file, reverting_versioned_file, 1,
1377 path_is_unversioned, repo,
1378 progress_cb, progress_arg);
1379 } else if (errno == ENOTDIR) {
1380 err = got_error_path(ondisk_path,
1381 GOT_ERR_FILE_OBSTRUCTED);
1383 err = got_error_from_errno3("symlink",
1384 target_path, ondisk_path);
1386 } else if (progress_cb)
1387 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1388 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1392 static const struct got_error *
1393 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1394 const char *path, mode_t te_mode, mode_t st_mode,
1395 struct got_blob_object *blob, int restoring_missing_file,
1396 int reverting_versioned_file, int installing_bad_symlink,
1397 int path_is_unversioned, struct got_repository *repo,
1398 got_worktree_checkout_cb progress_cb, void *progress_arg)
1400 const struct got_error *err = NULL;
1404 char *tmppath = NULL;
1407 mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1408 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1411 if (errno == ENOENT) {
1413 err = got_path_dirname(&parent, path);
1416 err = add_dir_on_disk(worktree, parent);
1420 fd = open(ondisk_path,
1421 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1424 return got_error_from_errno2("open",
1426 } else if (errno == EEXIST) {
1427 if (path_is_unversioned) {
1428 err = (*progress_cb)(progress_arg,
1429 GOT_STATUS_UNVERSIONED, path);
1432 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1433 !S_ISREG(st_mode) && !installing_bad_symlink) {
1434 /* TODO file is obstructed; do something */
1435 err = got_error_path(ondisk_path,
1436 GOT_ERR_FILE_OBSTRUCTED);
1439 err = got_opentemp_named_fd(&tmppath, &fd,
1445 if (fchmod(fd, apply_umask(mode)) == -1) {
1446 err = got_error_from_errno2("fchmod",
1452 return got_error_from_errno2("open", ondisk_path);
1456 if (restoring_missing_file)
1457 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1459 else if (reverting_versioned_file)
1460 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1463 err = (*progress_cb)(progress_arg,
1464 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1469 hdrlen = got_object_blob_get_hdrlen(blob);
1471 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1472 err = got_object_blob_read_block(&len, blob);
1476 /* Skip blob object header first time around. */
1477 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1479 err = got_error_from_errno("write");
1481 } else if (outlen != len - hdrlen) {
1482 err = got_error(GOT_ERR_IO);
1489 if (fsync(fd) != 0) {
1490 err = got_error_from_errno("fsync");
1495 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1496 err = got_error_from_errno2("unlink", ondisk_path);
1499 if (rename(tmppath, ondisk_path) != 0) {
1500 err = got_error_from_errno3("rename", tmppath,
1509 if (fd != -1 && close(fd) == -1 && err == NULL)
1510 err = got_error_from_errno("close");
1511 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1512 err = got_error_from_errno2("unlink", tmppath);
1517 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1518 static const struct got_error *
1519 get_modified_file_content_status(unsigned char *status, FILE *f)
1521 const struct got_error *err = NULL;
1522 const char *markers[3] = {
1523 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1524 GOT_DIFF_CONFLICT_MARKER_SEP,
1525 GOT_DIFF_CONFLICT_MARKER_END
1529 size_t linesize = 0;
1532 while (*status == GOT_STATUS_MODIFY) {
1533 linelen = getline(&line, &linesize, f);
1534 if (linelen == -1) {
1537 err = got_ferror(f, GOT_ERR_IO);
1541 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1542 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1544 *status = GOT_STATUS_CONFLICT;
1555 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1557 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1558 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1562 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1564 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1565 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1566 ie->mtime_sec == sb->st_mtim.tv_sec &&
1567 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1568 ie->size == (sb->st_size & 0xffffffff) &&
1569 !xbit_differs(ie, sb->st_mode));
1572 static unsigned char
1573 get_staged_status(struct got_fileindex_entry *ie)
1575 switch (got_fileindex_entry_stage_get(ie)) {
1576 case GOT_FILEIDX_STAGE_ADD:
1577 return GOT_STATUS_ADD;
1578 case GOT_FILEIDX_STAGE_DELETE:
1579 return GOT_STATUS_DELETE;
1580 case GOT_FILEIDX_STAGE_MODIFY:
1581 return GOT_STATUS_MODIFY;
1583 return GOT_STATUS_NO_CHANGE;
1587 static const struct got_error *
1588 get_symlink_modification_status(unsigned char *status,
1589 struct got_fileindex_entry *ie, const char *abspath,
1590 int dirfd, const char *de_name, struct got_blob_object *blob)
1592 const struct got_error *err = NULL;
1593 char target_path[PATH_MAX];
1594 char etarget[PATH_MAX];
1596 size_t len, target_len = 0;
1597 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1598 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1600 *status = GOT_STATUS_NO_CHANGE;
1602 /* Blob object content specifies the target path of the link. */
1604 err = got_object_blob_read_block(&len, blob);
1607 if (len + target_len >= sizeof(target_path)) {
1609 * Should not happen. The blob contents were OK
1610 * when this symlink was installed.
1612 return got_error(GOT_ERR_NO_SPACE);
1615 /* Skip blob object header first time around. */
1616 memcpy(target_path + target_len, buf + hdrlen,
1618 target_len += len - hdrlen;
1622 target_path[target_len] = '\0';
1625 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1627 return got_error_from_errno2("readlinkat", abspath);
1629 elen = readlink(abspath, etarget, sizeof(etarget));
1631 return got_error_from_errno2("readlink", abspath);
1634 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1635 *status = GOT_STATUS_MODIFY;
1640 static const struct got_error *
1641 get_file_status(unsigned char *status, struct stat *sb,
1642 struct got_fileindex_entry *ie, const char *abspath,
1643 int dirfd, const char *de_name, struct got_repository *repo)
1645 const struct got_error *err = NULL;
1646 struct got_object_id id;
1648 int fd = -1, fd1 = -1;
1651 struct got_blob_object *blob = NULL;
1653 unsigned char staged_status = get_staged_status(ie);
1655 *status = GOT_STATUS_NO_CHANGE;
1656 memset(sb, 0, sizeof(*sb));
1659 * Whenever the caller provides a directory descriptor and a
1660 * directory entry name for the file, use them! This prevents
1661 * race conditions if filesystem paths change beneath our feet.
1664 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1665 if (errno == ENOENT) {
1666 if (got_fileindex_entry_has_file_on_disk(ie))
1667 *status = GOT_STATUS_MISSING;
1669 *status = GOT_STATUS_DELETE;
1672 err = got_error_from_errno2("fstatat", abspath);
1676 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1677 if (fd == -1 && errno != ENOENT &&
1678 !got_err_open_nofollow_on_symlink())
1679 return got_error_from_errno2("open", abspath);
1680 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1681 if (lstat(abspath, sb) == -1)
1682 return got_error_from_errno2("lstat", abspath);
1683 } else if (fd == -1 || fstat(fd, sb) == -1) {
1684 if (errno == ENOENT) {
1685 if (got_fileindex_entry_has_file_on_disk(ie))
1686 *status = GOT_STATUS_MISSING;
1688 *status = GOT_STATUS_DELETE;
1691 err = got_error_from_errno2("fstat", abspath);
1696 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1697 *status = GOT_STATUS_OBSTRUCTED;
1701 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1702 *status = GOT_STATUS_DELETE;
1704 } else if (!got_fileindex_entry_has_blob(ie) &&
1705 staged_status != GOT_STATUS_ADD) {
1706 *status = GOT_STATUS_ADD;
1710 if (!stat_info_differs(ie, sb))
1713 if (S_ISLNK(sb->st_mode) &&
1714 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1715 *status = GOT_STATUS_MODIFY;
1719 if (staged_status == GOT_STATUS_MODIFY ||
1720 staged_status == GOT_STATUS_ADD)
1721 memcpy(id.hash, ie->staged_blob_hash, sizeof(id.hash));
1723 memcpy(id.hash, ie->blob_hash, sizeof(id.hash));
1725 fd1 = got_opentempfd();
1727 err = got_error_from_errno("got_opentempfd");
1730 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1734 if (S_ISLNK(sb->st_mode)) {
1735 err = get_symlink_modification_status(status, ie,
1736 abspath, dirfd, de_name, blob);
1741 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1743 err = got_error_from_errno2("openat", abspath);
1748 f = fdopen(fd, "r");
1750 err = got_error_from_errno2("fdopen", abspath);
1754 hdrlen = got_object_blob_get_hdrlen(blob);
1756 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1757 err = got_object_blob_read_block(&blen, blob);
1760 /* Skip length of blob object header first time around. */
1761 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1762 if (flen == 0 && ferror(f)) {
1763 err = got_error_from_errno("fread");
1766 if (blen - hdrlen == 0) {
1768 *status = GOT_STATUS_MODIFY;
1770 } else if (flen == 0) {
1771 if (blen - hdrlen != 0)
1772 *status = GOT_STATUS_MODIFY;
1774 } else if (blen - hdrlen == flen) {
1775 /* Skip blob object header first time around. */
1776 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1777 *status = GOT_STATUS_MODIFY;
1781 *status = GOT_STATUS_MODIFY;
1787 if (*status == GOT_STATUS_MODIFY) {
1789 err = get_modified_file_content_status(status, f);
1790 } else if (xbit_differs(ie, sb->st_mode))
1791 *status = GOT_STATUS_MODE_CHANGE;
1793 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1794 err = got_error_from_errno("close");
1796 got_object_blob_close(blob);
1797 if (f != NULL && fclose(f) == EOF && err == NULL)
1798 err = got_error_from_errno2("fclose", abspath);
1799 if (fd != -1 && close(fd) == -1 && err == NULL)
1800 err = got_error_from_errno2("close", abspath);
1805 * Update timestamps in the file index if a file is unmodified and
1806 * we had to run a full content comparison to find out.
1808 static const struct got_error *
1809 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1810 struct got_fileindex_entry *ie, struct stat *sb)
1812 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1813 return got_fileindex_entry_update(ie, wt_fd, path,
1814 ie->blob_hash, ie->commit_hash, 1);
1819 static const struct got_error *
1820 update_blob(struct got_worktree *worktree,
1821 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1822 struct got_tree_entry *te, const char *path,
1823 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1826 const struct got_error *err = NULL;
1827 struct got_blob_object *blob = NULL;
1828 char *ondisk_path = NULL;
1829 unsigned char status = GOT_STATUS_NO_CHANGE;
1831 int fd1 = -1, fd2 = -1;
1833 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1834 return got_error_from_errno("asprintf");
1837 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1838 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1841 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1845 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1846 sb.st_mode = got_fileindex_perms_to_st(ie);
1848 if (stat(ondisk_path, &sb) == -1) {
1849 if (errno != ENOENT) {
1850 err = got_error_from_errno2("stat",
1854 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1855 status = GOT_STATUS_UNVERSIONED;
1857 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1858 status = GOT_STATUS_UNVERSIONED;
1860 status = GOT_STATUS_OBSTRUCTED;
1864 if (status == GOT_STATUS_OBSTRUCTED) {
1866 got_fileindex_entry_mark_skipped(ie);
1867 err = (*progress_cb)(progress_arg, status, path);
1870 if (status == GOT_STATUS_CONFLICT) {
1872 got_fileindex_entry_mark_skipped(ie);
1873 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1878 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1879 (S_ISLNK(te->mode) ||
1880 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1882 * This is a regular file or an installed bad symlink.
1883 * If the file index indicates that this file is already
1884 * up-to-date with respect to the repository we can skip
1885 * updating contents of this file.
1887 if (got_fileindex_entry_has_commit(ie) &&
1888 memcmp(ie->commit_hash, worktree->base_commit_id->hash,
1889 SHA1_DIGEST_LENGTH) == 0) {
1891 err = sync_timestamps(worktree->root_fd,
1892 path, status, ie, &sb);
1895 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1899 if (got_fileindex_entry_has_blob(ie) &&
1900 memcmp(ie->blob_hash, te->id.hash,
1901 SHA1_DIGEST_LENGTH) == 0) {
1902 /* Different commit but the same blob. */
1903 err = sync_timestamps(worktree->root_fd,
1904 path, status, ie, &sb);
1907 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1913 fd1 = got_opentempfd();
1915 err = got_error_from_errno("got_opentempfd");
1918 err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
1922 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1923 int update_timestamps;
1924 struct got_blob_object *blob2 = NULL;
1925 char *label_orig = NULL;
1926 if (got_fileindex_entry_has_blob(ie)) {
1927 fd2 = got_opentempfd();
1929 err = got_error_from_errno("got_opentempfd");
1932 struct got_object_id id2;
1933 memcpy(id2.hash, ie->blob_hash, SHA1_DIGEST_LENGTH);
1934 err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
1939 if (got_fileindex_entry_has_commit(ie)) {
1940 char id_str[SHA1_DIGEST_STRING_LENGTH];
1941 if (got_sha1_digest_to_str(ie->commit_hash, id_str,
1942 sizeof(id_str)) == NULL) {
1943 err = got_error_path(id_str,
1944 GOT_ERR_BAD_OBJ_ID_STR);
1947 if (asprintf(&label_orig, "%s: commit %s",
1948 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1949 err = got_error_from_errno("asprintf");
1953 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1955 err = got_object_blob_read_to_str(&link_target, blob);
1958 err = merge_symlink(worktree, blob2, ondisk_path, path,
1959 label_orig, link_target, worktree->base_commit_id,
1960 repo, progress_cb, progress_arg);
1963 err = merge_blob(&update_timestamps, worktree, blob2,
1964 ondisk_path, path, sb.st_mode, label_orig, blob,
1965 worktree->base_commit_id, repo,
1966 progress_cb, progress_arg);
1969 if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
1970 err = got_error_from_errno("close");
1974 got_object_blob_close(blob2);
1978 * Do not update timestamps of files with local changes.
1979 * Otherwise, a future status walk would treat them as
1980 * unmodified files again.
1982 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1983 blob->id.hash, worktree->base_commit_id->hash,
1985 } else if (status == GOT_STATUS_MODE_CHANGE) {
1986 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1987 blob->id.hash, worktree->base_commit_id->hash, 0);
1988 } else if (status == GOT_STATUS_DELETE) {
1989 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1992 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1993 blob->id.hash, worktree->base_commit_id->hash, 0);
1997 int is_bad_symlink = 0;
1998 if (S_ISLNK(te->mode)) {
1999 err = install_symlink(&is_bad_symlink, worktree,
2000 ondisk_path, path, blob,
2001 status == GOT_STATUS_MISSING, 0,
2002 status == GOT_STATUS_UNVERSIONED, 0,
2003 repo, progress_cb, progress_arg);
2005 err = install_blob(worktree, ondisk_path, path,
2006 te->mode, sb.st_mode, blob,
2007 status == GOT_STATUS_MISSING, 0, 0,
2008 status == GOT_STATUS_UNVERSIONED, repo,
2009 progress_cb, progress_arg);
2015 err = got_fileindex_entry_update(ie,
2016 worktree->root_fd, path, blob->id.hash,
2017 worktree->base_commit_id->hash, 1);
2019 err = create_fileindex_entry(&ie, fileindex,
2020 worktree->base_commit_id, worktree->root_fd, path,
2026 if (is_bad_symlink) {
2027 got_fileindex_entry_filetype_set(ie,
2028 GOT_FILEIDX_MODE_BAD_SYMLINK);
2032 if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2033 err = got_error_from_errno("close");
2036 got_object_blob_close(blob);
2042 static const struct got_error *
2043 remove_ondisk_file(const char *root_path, const char *path)
2045 const struct got_error *err = NULL;
2046 char *ondisk_path = NULL, *parent = NULL;
2048 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2049 return got_error_from_errno("asprintf");
2051 if (unlink(ondisk_path) == -1) {
2052 if (errno != ENOENT)
2053 err = got_error_from_errno2("unlink", ondisk_path);
2055 size_t root_len = strlen(root_path);
2056 err = got_path_dirname(&parent, ondisk_path);
2059 while (got_path_cmp(parent, root_path,
2060 strlen(parent), root_len) != 0) {
2062 ondisk_path = parent;
2064 if (rmdir(ondisk_path) == -1) {
2065 if (errno != ENOTEMPTY)
2066 err = got_error_from_errno2("rmdir",
2070 err = got_path_dirname(&parent, ondisk_path);
2081 static const struct got_error *
2082 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2083 struct got_fileindex_entry *ie, struct got_repository *repo,
2084 got_worktree_checkout_cb progress_cb, void *progress_arg)
2086 const struct got_error *err = NULL;
2087 unsigned char status;
2091 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2092 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2094 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2096 return got_error_from_errno("asprintf");
2098 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2102 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2103 char ondisk_target[PATH_MAX];
2104 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2105 sizeof(ondisk_target));
2106 if (ondisk_len == -1) {
2107 err = got_error_from_errno2("readlink", ondisk_path);
2110 ondisk_target[ondisk_len] = '\0';
2111 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2112 NULL, NULL, /* XXX pass common ancestor info? */
2113 ondisk_target, ondisk_path);
2116 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2121 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2122 status == GOT_STATUS_ADD) {
2123 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2127 * Preserve the working file and change the deleted blob's
2128 * entry into a schedule-add entry.
2130 err = got_fileindex_entry_update(ie, worktree->root_fd,
2131 ie->path, NULL, NULL, 0);
2133 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2136 if (status == GOT_STATUS_NO_CHANGE) {
2137 err = remove_ondisk_file(worktree->root_path, ie->path);
2141 got_fileindex_entry_remove(fileindex, ie);
2148 struct diff_cb_arg {
2149 struct got_fileindex *fileindex;
2150 struct got_worktree *worktree;
2151 struct got_repository *repo;
2152 got_worktree_checkout_cb progress_cb;
2154 got_cancel_cb cancel_cb;
2158 static const struct got_error *
2159 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2160 struct got_tree_entry *te, const char *parent_path)
2162 struct diff_cb_arg *a = arg;
2164 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2165 return got_error(GOT_ERR_CANCELLED);
2167 return update_blob(a->worktree, a->fileindex, ie, te,
2168 ie->path, a->repo, a->progress_cb, a->progress_arg);
2171 static const struct got_error *
2172 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2174 struct diff_cb_arg *a = arg;
2176 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2177 return got_error(GOT_ERR_CANCELLED);
2179 return delete_blob(a->worktree, a->fileindex, ie,
2180 a->repo, a->progress_cb, a->progress_arg);
2183 static const struct got_error *
2184 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2186 struct diff_cb_arg *a = arg;
2187 const struct got_error *err;
2190 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2191 return got_error(GOT_ERR_CANCELLED);
2193 if (got_object_tree_entry_is_submodule(te))
2196 if (asprintf(&path, "%s%s%s", parent_path,
2197 parent_path[0] ? "/" : "", te->name)
2199 return got_error_from_errno("asprintf");
2201 if (S_ISDIR(te->mode))
2202 err = add_dir_on_disk(a->worktree, path);
2204 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2205 a->repo, a->progress_cb, a->progress_arg);
2211 const struct got_error *
2212 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2214 uint32_t uuid_status;
2216 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2217 if (uuid_status != uuid_s_ok) {
2219 return got_error_uuid(uuid_status, "uuid_to_string");
2225 static const struct got_error *
2226 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2228 const struct got_error *err = NULL;
2229 char *uuidstr = NULL;
2233 err = got_worktree_get_uuid(&uuidstr, worktree);
2237 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2238 err = got_error_from_errno("asprintf");
2245 const struct got_error *
2246 got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2249 return get_ref_name(refname, worktree, prefix);
2252 const struct got_error *
2253 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2255 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2258 static const struct got_error *
2259 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2261 return get_ref_name(refname, worktree,
2262 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2265 static const struct got_error *
2266 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2268 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2271 static const struct got_error *
2272 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2274 return get_ref_name(refname, worktree,
2275 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2278 static const struct got_error *
2279 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2281 return get_ref_name(refname, worktree,
2282 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2285 static const struct got_error *
2286 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2288 return get_ref_name(refname, worktree,
2289 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2292 static const struct got_error *
2293 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2295 return get_ref_name(refname, worktree,
2296 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2299 static const struct got_error *
2300 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2302 return get_ref_name(refname, worktree,
2303 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2306 static const struct got_error *
2307 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2309 return get_ref_name(refname, worktree,
2310 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2313 const struct got_error *
2314 got_worktree_get_histedit_script_path(char **path,
2315 struct got_worktree *worktree)
2317 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2318 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2320 return got_error_from_errno("asprintf");
2325 static const struct got_error *
2326 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2328 return get_ref_name(refname, worktree,
2329 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2332 static const struct got_error *
2333 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2335 return get_ref_name(refname, worktree,
2336 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2340 * Prevent Git's garbage collector from deleting our base commit by
2341 * setting a reference to our base commit's ID.
2343 static const struct got_error *
2344 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2346 const struct got_error *err = NULL;
2347 struct got_reference *ref = NULL;
2350 err = got_worktree_get_base_ref_name(&refname, worktree);
2354 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2358 err = got_ref_write(ref, repo);
2366 static const struct got_error *
2367 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2369 const struct got_error *err = NULL;
2371 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2372 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2373 err = got_error_from_errno("asprintf");
2374 *fileindex_path = NULL;
2380 static const struct got_error *
2381 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2382 struct got_worktree *worktree)
2384 const struct got_error *err = NULL;
2387 *fileindex_path = NULL;
2388 *fileindex = got_fileindex_alloc();
2389 if (*fileindex == NULL)
2390 return got_error_from_errno("got_fileindex_alloc");
2392 err = get_fileindex_path(fileindex_path, worktree);
2396 index = fopen(*fileindex_path, "rbe");
2397 if (index == NULL) {
2398 if (errno != ENOENT)
2399 err = got_error_from_errno2("fopen", *fileindex_path);
2401 err = got_fileindex_read(*fileindex, index);
2402 if (fclose(index) == EOF && err == NULL)
2403 err = got_error_from_errno("fclose");
2407 free(*fileindex_path);
2408 *fileindex_path = NULL;
2409 got_fileindex_free(*fileindex);
2415 struct bump_base_commit_id_arg {
2416 struct got_object_id *base_commit_id;
2419 const char *entry_name;
2420 got_worktree_checkout_cb progress_cb;
2424 /* Bump base commit ID of all files within an updated part of the work tree. */
2425 static const struct got_error *
2426 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2428 const struct got_error *err;
2429 struct bump_base_commit_id_arg *a = arg;
2431 if (a->entry_name) {
2432 if (strcmp(ie->path, a->path) != 0)
2434 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2437 if (got_fileindex_entry_was_skipped(ie))
2440 if (memcmp(ie->commit_hash, a->base_commit_id->hash,
2441 SHA1_DIGEST_LENGTH) == 0)
2444 if (a->progress_cb) {
2445 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2450 memcpy(ie->commit_hash, a->base_commit_id->hash, SHA1_DIGEST_LENGTH);
2454 static const struct got_error *
2455 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2456 struct got_fileindex *fileindex,
2457 got_worktree_checkout_cb progress_cb, void *progress_arg)
2459 struct bump_base_commit_id_arg bbc_arg;
2461 bbc_arg.base_commit_id = worktree->base_commit_id;
2462 bbc_arg.entry_name = NULL;
2464 bbc_arg.path_len = 0;
2465 bbc_arg.progress_cb = progress_cb;
2466 bbc_arg.progress_arg = progress_arg;
2468 return got_fileindex_for_each_entry_safe(fileindex,
2469 bump_base_commit_id, &bbc_arg);
2472 static const struct got_error *
2473 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2475 const struct got_error *err = NULL;
2476 char *new_fileindex_path = NULL;
2477 FILE *new_index = NULL;
2478 struct timespec timeout;
2480 err = got_opentemp_named(&new_fileindex_path, &new_index,
2481 fileindex_path, "");
2485 err = got_fileindex_write(fileindex, new_index);
2489 if (rename(new_fileindex_path, fileindex_path) != 0) {
2490 err = got_error_from_errno3("rename", new_fileindex_path,
2492 unlink(new_fileindex_path);
2496 * Sleep for a short amount of time to ensure that files modified after
2497 * this program exits have a different time stamp from the one which
2498 * was recorded in the file index.
2501 timeout.tv_nsec = 1;
2502 nanosleep(&timeout, NULL);
2506 free(new_fileindex_path);
2510 static const struct got_error *
2511 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2512 struct got_object_id **tree_id, const char *wt_relpath,
2513 struct got_commit_object *base_commit, struct got_worktree *worktree,
2514 struct got_repository *repo)
2516 const struct got_error *err = NULL;
2517 struct got_object_id *id = NULL;
2518 char *in_repo_path = NULL;
2519 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2521 *entry_type = GOT_OBJ_TYPE_ANY;
2522 *tree_relpath = NULL;
2525 if (wt_relpath[0] == '\0') {
2526 /* Check out all files within the work tree. */
2527 *entry_type = GOT_OBJ_TYPE_TREE;
2528 *tree_relpath = strdup("");
2529 if (*tree_relpath == NULL) {
2530 err = got_error_from_errno("strdup");
2533 err = got_object_id_by_path(tree_id, repo, base_commit,
2534 worktree->path_prefix);
2540 /* Check out a subset of files in the work tree. */
2542 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2543 is_root_wt ? "" : "/", wt_relpath) == -1) {
2544 err = got_error_from_errno("asprintf");
2548 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2553 in_repo_path = NULL;
2555 err = got_object_get_type(entry_type, repo, id);
2559 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2560 /* Check out a single file. */
2561 if (strchr(wt_relpath, '/') == NULL) {
2562 /* Check out a single file in work tree's root dir. */
2563 in_repo_path = strdup(worktree->path_prefix);
2564 if (in_repo_path == NULL) {
2565 err = got_error_from_errno("strdup");
2568 *tree_relpath = strdup("");
2569 if (*tree_relpath == NULL) {
2570 err = got_error_from_errno("strdup");
2574 /* Check out a single file in a subdirectory. */
2575 err = got_path_dirname(tree_relpath, wt_relpath);
2578 if (asprintf(&in_repo_path, "%s%s%s",
2579 worktree->path_prefix, is_root_wt ? "" : "/",
2580 *tree_relpath) == -1) {
2581 err = got_error_from_errno("asprintf");
2585 err = got_object_id_by_path(tree_id, repo,
2586 base_commit, in_repo_path);
2588 /* Check out all files within a subdirectory. */
2589 *tree_id = got_object_id_dup(id);
2590 if (*tree_id == NULL) {
2591 err = got_error_from_errno("got_object_id_dup");
2594 *tree_relpath = strdup(wt_relpath);
2595 if (*tree_relpath == NULL) {
2596 err = got_error_from_errno("strdup");
2604 *entry_type = GOT_OBJ_TYPE_ANY;
2605 free(*tree_relpath);
2606 *tree_relpath = NULL;
2613 static const struct got_error *
2614 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2615 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2616 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2617 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2619 const struct got_error *err = NULL;
2620 struct got_commit_object *commit = NULL;
2621 struct got_tree_object *tree = NULL;
2622 struct got_fileindex_diff_tree_cb diff_cb;
2623 struct diff_cb_arg arg;
2625 err = ref_base_commit(worktree, repo);
2627 if (!(err->code == GOT_ERR_ERRNO &&
2628 (errno == EACCES || errno == EROFS)))
2630 err = (*progress_cb)(progress_arg,
2631 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2636 err = got_object_open_as_commit(&commit, repo,
2637 worktree->base_commit_id);
2641 err = got_object_open_as_tree(&tree, repo, tree_id);
2646 got_object_tree_find_entry(tree, entry_name) == NULL) {
2647 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2651 diff_cb.diff_old_new = diff_old_new;
2652 diff_cb.diff_old = diff_old;
2653 diff_cb.diff_new = diff_new;
2654 arg.fileindex = fileindex;
2655 arg.worktree = worktree;
2657 arg.progress_cb = progress_cb;
2658 arg.progress_arg = progress_arg;
2659 arg.cancel_cb = cancel_cb;
2660 arg.cancel_arg = cancel_arg;
2661 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2662 entry_name, repo, &diff_cb, &arg);
2665 got_object_tree_close(tree);
2667 got_object_commit_close(commit);
2671 const struct got_error *
2672 got_worktree_checkout_files(struct got_worktree *worktree,
2673 struct got_pathlist_head *paths, struct got_repository *repo,
2674 got_worktree_checkout_cb progress_cb, void *progress_arg,
2675 got_cancel_cb cancel_cb, void *cancel_arg)
2677 const struct got_error *err = NULL, *sync_err, *unlockerr;
2678 struct got_commit_object *commit = NULL;
2679 struct got_tree_object *tree = NULL;
2680 struct got_fileindex *fileindex = NULL;
2681 char *fileindex_path = NULL;
2682 struct got_pathlist_entry *pe;
2683 struct tree_path_data {
2684 STAILQ_ENTRY(tree_path_data) entry;
2685 struct got_object_id *tree_id;
2690 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2692 STAILQ_INIT(&tree_paths);
2694 err = lock_worktree(worktree, LOCK_EX);
2698 err = got_object_open_as_commit(&commit, repo,
2699 worktree->base_commit_id);
2703 /* Map all specified paths to in-repository trees. */
2704 TAILQ_FOREACH(pe, paths, entry) {
2705 tpd = malloc(sizeof(*tpd));
2707 err = got_error_from_errno("malloc");
2711 err = find_tree_entry_for_checkout(&tpd->entry_type,
2712 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2719 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2720 err = got_path_basename(&tpd->entry_name, pe->path);
2728 tpd->entry_name = NULL;
2730 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2734 * Read the file index.
2735 * Checking out files is supposed to be an idempotent operation.
2736 * If the on-disk file index is incomplete we will try to complete it.
2738 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2742 tpd = STAILQ_FIRST(&tree_paths);
2743 TAILQ_FOREACH(pe, paths, entry) {
2744 struct bump_base_commit_id_arg bbc_arg;
2746 err = checkout_files(worktree, fileindex, tpd->relpath,
2747 tpd->tree_id, tpd->entry_name, repo,
2748 progress_cb, progress_arg, cancel_cb, cancel_arg);
2752 bbc_arg.base_commit_id = worktree->base_commit_id;
2753 bbc_arg.entry_name = tpd->entry_name;
2754 bbc_arg.path = pe->path;
2755 bbc_arg.path_len = pe->path_len;
2756 bbc_arg.progress_cb = progress_cb;
2757 bbc_arg.progress_arg = progress_arg;
2758 err = got_fileindex_for_each_entry_safe(fileindex,
2759 bump_base_commit_id, &bbc_arg);
2763 tpd = STAILQ_NEXT(tpd, entry);
2765 sync_err = sync_fileindex(fileindex, fileindex_path);
2766 if (sync_err && err == NULL)
2769 free(fileindex_path);
2771 got_object_tree_close(tree);
2773 got_object_commit_close(commit);
2775 got_fileindex_free(fileindex);
2776 while (!STAILQ_EMPTY(&tree_paths)) {
2777 tpd = STAILQ_FIRST(&tree_paths);
2778 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2783 unlockerr = lock_worktree(worktree, LOCK_SH);
2784 if (unlockerr && err == NULL)
2789 struct merge_file_cb_arg {
2790 struct got_worktree *worktree;
2791 struct got_fileindex *fileindex;
2792 got_worktree_checkout_cb progress_cb;
2794 got_cancel_cb cancel_cb;
2796 const char *label_orig;
2797 struct got_object_id *commit_id2;
2798 int allow_bad_symlinks;
2801 static const struct got_error *
2802 merge_file_cb(void *arg, struct got_blob_object *blob1,
2803 struct got_blob_object *blob2, FILE *f1, FILE *f2,
2804 struct got_object_id *id1, struct got_object_id *id2,
2805 const char *path1, const char *path2,
2806 mode_t mode1, mode_t mode2, struct got_repository *repo)
2808 static const struct got_error *err = NULL;
2809 struct merge_file_cb_arg *a = arg;
2810 struct got_fileindex_entry *ie;
2811 char *ondisk_path = NULL;
2813 unsigned char status;
2814 int local_changes_subsumed;
2815 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2816 char *id_str = NULL, *label_deriv2 = NULL;
2818 if (blob1 && blob2) {
2819 ie = got_fileindex_entry_get(a->fileindex, path2,
2822 return (*a->progress_cb)(a->progress_arg,
2823 GOT_STATUS_MISSING, path2);
2825 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2827 return got_error_from_errno("asprintf");
2829 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2834 if (status == GOT_STATUS_DELETE) {
2835 err = (*a->progress_cb)(a->progress_arg,
2836 GOT_STATUS_MERGE, path2);
2839 if (status != GOT_STATUS_NO_CHANGE &&
2840 status != GOT_STATUS_MODIFY &&
2841 status != GOT_STATUS_CONFLICT &&
2842 status != GOT_STATUS_ADD) {
2843 err = (*a->progress_cb)(a->progress_arg, status, path2);
2847 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2849 err = got_object_blob_read_to_str(&link_target2, blob2);
2852 err = merge_symlink(a->worktree, blob1, ondisk_path,
2853 path2, a->label_orig, link_target2, a->commit_id2,
2854 repo, a->progress_cb, a->progress_arg);
2859 f_orig = got_opentemp();
2860 if (f_orig == NULL) {
2861 err = got_error_from_errno("got_opentemp");
2864 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2869 f_deriv2 = got_opentemp();
2870 if (f_deriv2 == NULL)
2872 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2877 fd = open(ondisk_path,
2878 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2880 err = got_error_from_errno2("open",
2884 f_deriv = fdopen(fd, "r");
2885 if (f_deriv == NULL) {
2886 err = got_error_from_errno2("fdopen",
2891 err = got_object_id_str(&id_str, a->commit_id2);
2894 if (asprintf(&label_deriv2, "%s: commit %s",
2895 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2896 err = got_error_from_errno("asprintf");
2899 err = merge_file(&local_changes_subsumed, a->worktree,
2900 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2901 mode2, a->label_orig, NULL, label_deriv2,
2902 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2903 a->progress_cb, a->progress_arg);
2906 ie = got_fileindex_entry_get(a->fileindex, path1,
2909 return (*a->progress_cb)(a->progress_arg,
2910 GOT_STATUS_MISSING, path1);
2912 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2914 return got_error_from_errno("asprintf");
2916 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2922 case GOT_STATUS_NO_CHANGE:
2923 err = (*a->progress_cb)(a->progress_arg,
2924 GOT_STATUS_DELETE, path1);
2927 err = remove_ondisk_file(a->worktree->root_path, path1);
2931 got_fileindex_entry_mark_deleted_from_disk(ie);
2933 case GOT_STATUS_DELETE:
2934 case GOT_STATUS_MISSING:
2935 err = (*a->progress_cb)(a->progress_arg,
2936 GOT_STATUS_DELETE, path1);
2940 got_fileindex_entry_mark_deleted_from_disk(ie);
2942 case GOT_STATUS_ADD: {
2943 struct got_object_id *id;
2947 * Delete the added file only if its content already
2948 * exists in the repository.
2950 err = got_object_blob_file_create(&id, &blob1_f,
2951 &blob1_size, path1);
2954 if (got_object_id_cmp(id, id1) == 0) {
2955 err = (*a->progress_cb)(a->progress_arg,
2956 GOT_STATUS_DELETE, path1);
2959 err = remove_ondisk_file(a->worktree->root_path,
2964 got_fileindex_entry_remove(a->fileindex,
2967 err = (*a->progress_cb)(a->progress_arg,
2968 GOT_STATUS_CANNOT_DELETE, path1);
2970 if (fclose(blob1_f) == EOF && err == NULL)
2971 err = got_error_from_errno("fclose");
2977 case GOT_STATUS_MODIFY:
2978 case GOT_STATUS_CONFLICT:
2979 err = (*a->progress_cb)(a->progress_arg,
2980 GOT_STATUS_CANNOT_DELETE, path1);
2984 case GOT_STATUS_OBSTRUCTED:
2985 err = (*a->progress_cb)(a->progress_arg, status, path1);
2993 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2995 return got_error_from_errno("asprintf");
2996 ie = got_fileindex_entry_get(a->fileindex, path2,
2999 err = get_file_status(&status, &sb, ie, ondisk_path,
3003 if (status != GOT_STATUS_NO_CHANGE &&
3004 status != GOT_STATUS_MODIFY &&
3005 status != GOT_STATUS_CONFLICT &&
3006 status != GOT_STATUS_ADD) {
3007 err = (*a->progress_cb)(a->progress_arg,
3011 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3013 err = got_object_blob_read_to_str(&link_target2,
3017 err = merge_symlink(a->worktree, NULL,
3018 ondisk_path, path2, a->label_orig,
3019 link_target2, a->commit_id2, repo,
3020 a->progress_cb, a->progress_arg);
3022 } else if (S_ISREG(sb.st_mode)) {
3023 err = merge_blob(&local_changes_subsumed,
3024 a->worktree, NULL, ondisk_path, path2,
3025 sb.st_mode, a->label_orig, blob2,
3026 a->commit_id2, repo, a->progress_cb,
3029 err = got_error_path(ondisk_path,
3030 GOT_ERR_FILE_OBSTRUCTED);
3034 if (status == GOT_STATUS_DELETE) {
3035 err = got_fileindex_entry_update(ie,
3036 a->worktree->root_fd, path2, blob2->id.hash,
3037 a->worktree->base_commit_id->hash, 0);
3042 int is_bad_symlink = 0;
3043 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3044 if (S_ISLNK(mode2)) {
3045 err = install_symlink(&is_bad_symlink,
3046 a->worktree, ondisk_path, path2, blob2, 0,
3047 0, 1, a->allow_bad_symlinks, repo,
3048 a->progress_cb, a->progress_arg);
3050 err = install_blob(a->worktree, ondisk_path, path2,
3051 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3052 a->progress_cb, a->progress_arg);
3056 err = got_fileindex_entry_alloc(&ie, path2);
3059 err = got_fileindex_entry_update(ie,
3060 a->worktree->root_fd, path2, NULL, NULL, 1);
3062 got_fileindex_entry_free(ie);
3065 err = got_fileindex_entry_add(a->fileindex, ie);
3067 got_fileindex_entry_free(ie);
3070 if (is_bad_symlink) {
3071 got_fileindex_entry_filetype_set(ie,
3072 GOT_FILEIDX_MODE_BAD_SYMLINK);
3077 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3078 err = got_error_from_errno("fclose");
3079 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3080 err = got_error_from_errno("fclose");
3081 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3082 err = got_error_from_errno("fclose");
3089 static const struct got_error *
3090 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3092 struct got_worktree *worktree = arg;
3094 /* Reject merges into a work tree with mixed base commits. */
3095 if (got_fileindex_entry_has_commit(ie) &&
3096 memcmp(ie->commit_hash, worktree->base_commit_id->hash,
3097 SHA1_DIGEST_LENGTH) != 0)
3098 return got_error(GOT_ERR_MIXED_COMMITS);
3103 struct check_merge_conflicts_arg {
3104 struct got_worktree *worktree;
3105 struct got_fileindex *fileindex;
3106 struct got_repository *repo;
3109 static const struct got_error *
3110 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3111 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3112 struct got_object_id *id1, struct got_object_id *id2,
3113 const char *path1, const char *path2,
3114 mode_t mode1, mode_t mode2, struct got_repository *repo)
3116 const struct got_error *err = NULL;
3117 struct check_merge_conflicts_arg *a = arg;
3118 unsigned char status;
3120 struct got_fileindex_entry *ie;
3121 const char *path = path2 ? path2 : path1;
3122 struct got_object_id *id = id2 ? id2 : id1;
3128 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3132 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3134 return got_error_from_errno("asprintf");
3136 /* Reject merges into a work tree with conflicted files. */
3137 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3141 if (status == GOT_STATUS_CONFLICT)
3142 return got_error(GOT_ERR_CONFLICTS);
3147 static const struct got_error *
3148 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3149 const char *fileindex_path, struct got_object_id *commit_id1,
3150 struct got_object_id *commit_id2, struct got_repository *repo,
3151 got_worktree_checkout_cb progress_cb, void *progress_arg,
3152 got_cancel_cb cancel_cb, void *cancel_arg)
3154 const struct got_error *err = NULL, *sync_err;
3155 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3156 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3157 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3158 struct check_merge_conflicts_arg cmc_arg;
3159 struct merge_file_cb_arg arg;
3160 char *label_orig = NULL;
3161 FILE *f1 = NULL, *f2 = NULL;
3162 int fd1 = -1, fd2 = -1;
3165 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3168 err = got_object_id_by_path(&tree_id1, repo, commit1,
3169 worktree->path_prefix);
3170 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3176 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3180 err = got_object_id_str(&id_str, commit_id1);
3184 if (asprintf(&label_orig, "%s: commit %s",
3185 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3186 err = got_error_from_errno("asprintf");
3192 f1 = got_opentemp();
3194 err = got_error_from_errno("got_opentemp");
3199 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3203 err = got_object_id_by_path(&tree_id2, repo, commit2,
3204 worktree->path_prefix);
3208 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3212 f2 = got_opentemp();
3214 err = got_error_from_errno("got_opentemp");
3218 fd1 = got_opentempfd();
3220 err = got_error_from_errno("got_opentempfd");
3224 fd2 = got_opentempfd();
3226 err = got_error_from_errno("got_opentempfd");
3230 cmc_arg.worktree = worktree;
3231 cmc_arg.fileindex = fileindex;
3232 cmc_arg.repo = repo;
3233 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3234 check_merge_conflicts, &cmc_arg, 0);
3238 arg.worktree = worktree;
3239 arg.fileindex = fileindex;
3240 arg.progress_cb = progress_cb;
3241 arg.progress_arg = progress_arg;
3242 arg.cancel_cb = cancel_cb;
3243 arg.cancel_arg = cancel_arg;
3244 arg.label_orig = label_orig;
3245 arg.commit_id2 = commit_id2;
3246 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3247 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3248 merge_file_cb, &arg, 1);
3249 sync_err = sync_fileindex(fileindex, fileindex_path);
3250 if (sync_err && err == NULL)
3254 got_object_commit_close(commit1);
3256 got_object_commit_close(commit2);
3258 got_object_tree_close(tree1);
3260 got_object_tree_close(tree2);
3261 if (f1 && fclose(f1) == EOF && err == NULL)
3262 err = got_error_from_errno("fclose");
3263 if (f2 && fclose(f2) == EOF && err == NULL)
3264 err = got_error_from_errno("fclose");
3265 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3266 err = got_error_from_errno("close");
3267 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3268 err = got_error_from_errno("close");
3273 const struct got_error *
3274 got_worktree_merge_files(struct got_worktree *worktree,
3275 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3276 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3277 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3279 const struct got_error *err, *unlockerr;
3280 char *fileindex_path = NULL;
3281 struct got_fileindex *fileindex = NULL;
3283 err = lock_worktree(worktree, LOCK_EX);
3287 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3291 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3296 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3297 commit_id2, repo, progress_cb, progress_arg,
3298 cancel_cb, cancel_arg);
3301 got_fileindex_free(fileindex);
3302 free(fileindex_path);
3303 unlockerr = lock_worktree(worktree, LOCK_SH);
3304 if (unlockerr && err == NULL)
3309 struct diff_dir_cb_arg {
3310 struct got_fileindex *fileindex;
3311 struct got_worktree *worktree;
3312 const char *status_path;
3313 size_t status_path_len;
3314 struct got_repository *repo;
3315 got_worktree_status_cb status_cb;
3317 got_cancel_cb cancel_cb;
3319 /* A pathlist containing per-directory pathlists of ignore patterns. */
3320 struct got_pathlist_head *ignores;
3321 int report_unchanged;
3325 static const struct got_error *
3326 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3327 int dirfd, const char *de_name,
3328 got_worktree_status_cb status_cb, void *status_arg,
3329 struct got_repository *repo, int report_unchanged)
3331 const struct got_error *err = NULL;
3332 unsigned char status = GOT_STATUS_NO_CHANGE;
3333 unsigned char staged_status = get_staged_status(ie);
3335 struct got_object_id blob_id, commit_id, staged_blob_id;
3336 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3337 struct got_object_id *staged_blob_idp = NULL;
3339 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3343 if (status == GOT_STATUS_NO_CHANGE &&
3344 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3347 if (got_fileindex_entry_has_blob(ie)) {
3348 memcpy(blob_id.hash, ie->blob_hash, SHA1_DIGEST_LENGTH);
3349 blob_idp = &blob_id;
3351 if (got_fileindex_entry_has_commit(ie)) {
3352 memcpy(commit_id.hash, ie->commit_hash, SHA1_DIGEST_LENGTH);
3353 commit_idp = &commit_id;
3355 if (staged_status == GOT_STATUS_ADD ||
3356 staged_status == GOT_STATUS_MODIFY) {
3357 memcpy(staged_blob_id.hash, ie->staged_blob_hash,
3358 SHA1_DIGEST_LENGTH);
3359 staged_blob_idp = &staged_blob_id;
3362 return (*status_cb)(status_arg, status, staged_status,
3363 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3366 static const struct got_error *
3367 status_old_new(void *arg, struct got_fileindex_entry *ie,
3368 struct dirent *de, const char *parent_path, int dirfd)
3370 const struct got_error *err = NULL;
3371 struct diff_dir_cb_arg *a = arg;
3374 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3375 return got_error(GOT_ERR_CANCELLED);
3377 if (got_path_cmp(parent_path, a->status_path,
3378 strlen(parent_path), a->status_path_len) != 0 &&
3379 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3382 if (parent_path[0]) {
3383 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3384 parent_path, de->d_name) == -1)
3385 return got_error_from_errno("asprintf");
3387 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3389 return got_error_from_errno("asprintf");
3392 err = report_file_status(ie, abspath, dirfd, de->d_name,
3393 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3398 static const struct got_error *
3399 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3401 struct diff_dir_cb_arg *a = arg;
3402 struct got_object_id blob_id, commit_id;
3403 unsigned char status;
3405 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3406 return got_error(GOT_ERR_CANCELLED);
3408 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3411 memcpy(blob_id.hash, ie->blob_hash, SHA1_DIGEST_LENGTH);
3412 memcpy(commit_id.hash, ie->commit_hash, SHA1_DIGEST_LENGTH);
3413 if (got_fileindex_entry_has_file_on_disk(ie))
3414 status = GOT_STATUS_MISSING;
3416 status = GOT_STATUS_DELETE;
3417 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3418 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3422 free_ignores(struct got_pathlist_head *ignores)
3424 struct got_pathlist_entry *pe;
3426 TAILQ_FOREACH(pe, ignores, entry) {
3427 struct got_pathlist_head *ignorelist = pe->data;
3429 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3431 got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3434 static const struct got_error *
3435 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3437 const struct got_error *err = NULL;
3438 struct got_pathlist_entry *pe = NULL;
3439 struct got_pathlist_head *ignorelist;
3440 char *line = NULL, *pattern, *dirpath = NULL;
3441 size_t linesize = 0;
3444 ignorelist = calloc(1, sizeof(*ignorelist));
3445 if (ignorelist == NULL)
3446 return got_error_from_errno("calloc");
3447 TAILQ_INIT(ignorelist);
3449 while ((linelen = getline(&line, &linesize, f)) != -1) {
3450 if (linelen > 0 && line[linelen - 1] == '\n')
3451 line[linelen - 1] = '\0';
3453 /* Git's ignores may contain comments. */
3457 /* Git's negated patterns are not (yet?) supported. */
3461 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3463 err = got_error_from_errno("asprintf");
3466 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3471 err = got_error_from_errno("getline");
3475 dirpath = strdup(path);
3476 if (dirpath == NULL) {
3477 err = got_error_from_errno("strdup");
3480 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3483 if (err || pe == NULL) {
3485 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3491 match_ignores(struct got_pathlist_head *ignores, const char *path)
3493 struct got_pathlist_entry *pe;
3495 /* Handle patterns which match in all directories. */
3496 TAILQ_FOREACH(pe, ignores, entry) {
3497 struct got_pathlist_head *ignorelist = pe->data;
3498 struct got_pathlist_entry *pi;
3500 TAILQ_FOREACH(pi, ignorelist, entry) {
3501 const char *p, *pattern = pi->path;
3503 if (strncmp(pattern, "**/", 3) != 0)
3508 if (fnmatch(pattern, p,
3509 FNM_PATHNAME | FNM_LEADING_DIR)) {
3510 /* Retry in next directory. */
3511 while (*p && *p != '/')
3523 * The ignores pathlist contains ignore lists from children before
3524 * parents, so we can find the most specific ignorelist by walking
3525 * ignores backwards.
3527 pe = TAILQ_LAST(ignores, got_pathlist_head);
3529 if (got_path_is_child(path, pe->path, pe->path_len)) {
3530 struct got_pathlist_head *ignorelist = pe->data;
3531 struct got_pathlist_entry *pi;
3532 TAILQ_FOREACH(pi, ignorelist, entry) {
3533 const char *pattern = pi->path;
3534 int flags = FNM_LEADING_DIR;
3535 if (strstr(pattern, "/**/") == NULL)
3536 flags |= FNM_PATHNAME;
3537 if (fnmatch(pattern, path, flags))
3542 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3548 static const struct got_error *
3549 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3550 const char *path, int dirfd, const char *ignores_filename)
3552 const struct got_error *err = NULL;
3555 FILE *ignoresfile = NULL;
3557 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3558 path[0] ? "/" : "", ignores_filename) == -1)
3559 return got_error_from_errno("asprintf");
3562 fd = openat(dirfd, ignores_filename,
3563 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3565 if (errno != ENOENT && errno != EACCES)
3566 err = got_error_from_errno2("openat",
3569 ignoresfile = fdopen(fd, "r");
3570 if (ignoresfile == NULL)
3571 err = got_error_from_errno2("fdopen",
3575 err = read_ignores(ignores, path, ignoresfile);
3579 ignoresfile = fopen(ignorespath, "re");
3580 if (ignoresfile == NULL) {
3581 if (errno != ENOENT && errno != EACCES)
3582 err = got_error_from_errno2("fopen",
3585 err = read_ignores(ignores, path, ignoresfile);
3588 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3589 err = got_error_from_errno2("fclose", path);
3590 if (fd != -1 && close(fd) == -1 && err == NULL)
3591 err = got_error_from_errno2("close", path);
3596 static const struct got_error *
3597 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3600 const struct got_error *err = NULL;
3601 struct diff_dir_cb_arg *a = arg;
3607 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3608 return got_error(GOT_ERR_CANCELLED);
3610 if (parent_path[0]) {
3611 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3612 return got_error_from_errno("asprintf");
3617 if (de->d_type == DT_DIR) {
3618 if (!a->no_ignores && ignore != NULL &&
3619 match_ignores(a->ignores, path))
3621 } else if (!match_ignores(a->ignores, path) &&
3622 got_path_is_child(path, a->status_path, a->status_path_len))
3623 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3624 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3630 static const struct got_error *
3631 status_traverse(void *arg, const char *path, int dirfd)
3633 const struct got_error *err = NULL;
3634 struct diff_dir_cb_arg *a = arg;
3639 err = add_ignores(a->ignores, a->worktree->root_path,
3640 path, dirfd, ".cvsignore");
3644 err = add_ignores(a->ignores, a->worktree->root_path, path,
3645 dirfd, ".gitignore");
3650 static const struct got_error *
3651 report_single_file_status(const char *path, const char *ondisk_path,
3652 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3653 void *status_arg, struct got_repository *repo, int report_unchanged,
3654 struct got_pathlist_head *ignores, int no_ignores)
3656 struct got_fileindex_entry *ie;
3659 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3661 return report_file_status(ie, ondisk_path, -1, NULL,
3662 status_cb, status_arg, repo, report_unchanged);
3664 if (lstat(ondisk_path, &sb) == -1) {
3665 if (errno != ENOENT)
3666 return got_error_from_errno2("lstat", ondisk_path);
3667 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3668 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3671 if (!no_ignores && match_ignores(ignores, path))
3674 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3675 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3676 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3681 static const struct got_error *
3682 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3683 const char *root_path, const char *path)
3685 const struct got_error *err;
3686 char *parent_path, *next_parent_path = NULL;
3688 err = add_ignores(ignores, root_path, "", -1,
3693 err = add_ignores(ignores, root_path, "", -1,
3698 err = got_path_dirname(&parent_path, path);
3700 if (err->code == GOT_ERR_BAD_PATH)
3701 return NULL; /* cannot traverse parent */
3705 err = add_ignores(ignores, root_path, parent_path, -1,
3709 err = add_ignores(ignores, root_path, parent_path, -1,
3713 err = got_path_dirname(&next_parent_path, parent_path);
3715 if (err->code == GOT_ERR_BAD_PATH)
3716 err = NULL; /* traversed everything */
3719 if (got_path_is_root_dir(parent_path))
3722 parent_path = next_parent_path;
3723 next_parent_path = NULL;
3727 free(next_parent_path);
3731 static const struct got_error *
3732 worktree_status(struct got_worktree *worktree, const char *path,
3733 struct got_fileindex *fileindex, struct got_repository *repo,
3734 got_worktree_status_cb status_cb, void *status_arg,
3735 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3736 int report_unchanged)
3738 const struct got_error *err = NULL;
3740 struct got_fileindex_diff_dir_cb fdiff_cb;
3741 struct diff_dir_cb_arg arg;
3742 char *ondisk_path = NULL;
3743 struct got_pathlist_head ignores;
3744 struct got_fileindex_entry *ie;
3746 TAILQ_INIT(&ignores);
3748 if (asprintf(&ondisk_path, "%s%s%s",
3749 worktree->root_path, path[0] ? "/" : "", path) == -1)
3750 return got_error_from_errno("asprintf");
3752 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3754 err = report_single_file_status(path, ondisk_path,
3755 fileindex, status_cb, status_arg, repo,
3756 report_unchanged, &ignores, no_ignores);
3760 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3762 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3763 !got_err_open_nofollow_on_symlink())
3764 err = got_error_from_errno2("open", ondisk_path);
3767 err = add_ignores_from_parent_paths(&ignores,
3768 worktree->root_path, ondisk_path);
3772 err = report_single_file_status(path, ondisk_path,
3773 fileindex, status_cb, status_arg, repo,
3774 report_unchanged, &ignores, no_ignores);
3777 fdiff_cb.diff_old_new = status_old_new;
3778 fdiff_cb.diff_old = status_old;
3779 fdiff_cb.diff_new = status_new;
3780 fdiff_cb.diff_traverse = status_traverse;
3781 arg.fileindex = fileindex;
3782 arg.worktree = worktree;
3783 arg.status_path = path;
3784 arg.status_path_len = strlen(path);
3786 arg.status_cb = status_cb;
3787 arg.status_arg = status_arg;
3788 arg.cancel_cb = cancel_cb;
3789 arg.cancel_arg = cancel_arg;
3790 arg.report_unchanged = report_unchanged;
3791 arg.no_ignores = no_ignores;
3793 err = add_ignores_from_parent_paths(&ignores,
3794 worktree->root_path, path);
3798 arg.ignores = &ignores;
3799 err = got_fileindex_diff_dir(fileindex, fd,
3800 worktree->root_path, path, repo, &fdiff_cb, &arg);
3803 free_ignores(&ignores);
3804 if (fd != -1 && close(fd) == -1 && err == NULL)
3805 err = got_error_from_errno("close");
3810 const struct got_error *
3811 got_worktree_status(struct got_worktree *worktree,
3812 struct got_pathlist_head *paths, struct got_repository *repo,
3813 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3814 got_cancel_cb cancel_cb, void *cancel_arg)
3816 const struct got_error *err = NULL;
3817 char *fileindex_path = NULL;
3818 struct got_fileindex *fileindex = NULL;
3819 struct got_pathlist_entry *pe;
3821 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3825 TAILQ_FOREACH(pe, paths, entry) {
3826 err = worktree_status(worktree, pe->path, fileindex, repo,
3827 status_cb, status_arg, cancel_cb, cancel_arg,
3832 free(fileindex_path);
3833 got_fileindex_free(fileindex);
3837 const struct got_error *
3838 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3841 const struct got_error *err = NULL;
3842 char *resolved = NULL, *cwd = NULL, *path = NULL;
3845 char *abspath = NULL;
3846 char canonpath[PATH_MAX];
3850 cwd = getcwd(NULL, 0);
3852 return got_error_from_errno("getcwd");
3854 if (lstat(arg, &sb) == -1) {
3855 if (errno != ENOENT) {
3856 err = got_error_from_errno2("lstat", arg);
3861 if (S_ISLNK(sb.st_mode)) {
3863 * We cannot use realpath(3) with symlinks since we want to
3864 * operate on the symlink itself.
3865 * But we can make the path absolute, assuming it is relative
3866 * to the current working directory, and then canonicalize it.
3868 if (!got_path_is_absolute(arg)) {
3869 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3870 err = got_error_from_errno("asprintf");
3875 err = got_canonpath(abspath ? abspath : arg, canonpath,
3879 resolved = strdup(canonpath);
3880 if (resolved == NULL) {
3881 err = got_error_from_errno("strdup");
3885 resolved = realpath(arg, NULL);
3886 if (resolved == NULL) {
3887 if (errno != ENOENT) {
3888 err = got_error_from_errno2("realpath", arg);
3891 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3892 err = got_error_from_errno("asprintf");
3895 err = got_canonpath(abspath, canonpath,
3899 resolved = strdup(canonpath);
3900 if (resolved == NULL) {
3901 err = got_error_from_errno("strdup");
3907 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3908 strlen(got_worktree_get_root_path(worktree)))) {
3909 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3913 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3914 err = got_path_skip_common_ancestor(&path,
3915 got_worktree_get_root_path(worktree), resolved);
3921 err = got_error_from_errno("strdup");
3926 /* XXX status walk can't deal with trailing slash! */
3928 while (len > 0 && path[len - 1] == '/') {
3929 path[len - 1] = '\0';
3943 struct schedule_addition_args {
3944 struct got_worktree *worktree;
3945 struct got_fileindex *fileindex;
3946 got_worktree_checkout_cb progress_cb;
3948 struct got_repository *repo;
3951 static const struct got_error *
3952 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3953 const char *relpath, struct got_object_id *blob_id,
3954 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3955 int dirfd, const char *de_name)
3957 struct schedule_addition_args *a = arg;
3958 const struct got_error *err = NULL;
3959 struct got_fileindex_entry *ie;
3963 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3965 return got_error_from_errno("asprintf");
3967 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3969 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3973 /* Re-adding an existing entry is a no-op. */
3974 if (status == GOT_STATUS_ADD)
3976 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3981 if (status != GOT_STATUS_UNVERSIONED) {
3982 if (status == GOT_STATUS_NONEXISTENT)
3983 err = got_error_set_errno(ENOENT, ondisk_path);
3985 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3989 err = got_fileindex_entry_alloc(&ie, relpath);
3992 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3993 relpath, NULL, NULL, 1);
3995 got_fileindex_entry_free(ie);
3998 err = got_fileindex_entry_add(a->fileindex, ie);
4000 got_fileindex_entry_free(ie);
4007 if (status == GOT_STATUS_ADD)
4009 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4012 const struct got_error *
4013 got_worktree_schedule_add(struct got_worktree *worktree,
4014 struct got_pathlist_head *paths,
4015 got_worktree_checkout_cb progress_cb, void *progress_arg,
4016 struct got_repository *repo, int no_ignores)
4018 struct got_fileindex *fileindex = NULL;
4019 char *fileindex_path = NULL;
4020 const struct got_error *err = NULL, *sync_err, *unlockerr;
4021 struct got_pathlist_entry *pe;
4022 struct schedule_addition_args saa;
4024 err = lock_worktree(worktree, LOCK_EX);
4028 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4032 saa.worktree = worktree;
4033 saa.fileindex = fileindex;
4034 saa.progress_cb = progress_cb;
4035 saa.progress_arg = progress_arg;
4038 TAILQ_FOREACH(pe, paths, entry) {
4039 err = worktree_status(worktree, pe->path, fileindex, repo,
4040 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4044 sync_err = sync_fileindex(fileindex, fileindex_path);
4045 if (sync_err && err == NULL)
4048 free(fileindex_path);
4050 got_fileindex_free(fileindex);
4051 unlockerr = lock_worktree(worktree, LOCK_SH);
4052 if (unlockerr && err == NULL)
4057 struct schedule_deletion_args {
4058 struct got_worktree *worktree;
4059 struct got_fileindex *fileindex;
4060 got_worktree_delete_cb progress_cb;
4062 struct got_repository *repo;
4063 int delete_local_mods;
4065 int ignore_missing_paths;
4066 const char *status_codes;
4069 static const struct got_error *
4070 schedule_for_deletion(void *arg, unsigned char status,
4071 unsigned char staged_status, const char *relpath,
4072 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4073 struct got_object_id *commit_id, int dirfd, const char *de_name)
4075 struct schedule_deletion_args *a = arg;
4076 const struct got_error *err = NULL;
4077 struct got_fileindex_entry *ie = NULL;
4081 if (status == GOT_STATUS_NONEXISTENT) {
4082 if (a->ignore_missing_paths)
4084 return got_error_set_errno(ENOENT, relpath);
4087 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4089 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4091 staged_status = get_staged_status(ie);
4092 if (staged_status != GOT_STATUS_NO_CHANGE) {
4093 if (staged_status == GOT_STATUS_DELETE)
4095 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4098 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4100 return got_error_from_errno("asprintf");
4102 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4107 if (a->status_codes) {
4108 size_t ncodes = strlen(a->status_codes);
4110 for (i = 0; i < ncodes ; i++) {
4111 if (status == a->status_codes[i])
4115 /* Do not delete files in non-matching status. */
4119 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4120 a->status_codes[i] != GOT_STATUS_MISSING) {
4121 static char msg[64];
4122 snprintf(msg, sizeof(msg),
4123 "invalid status code '%c'", a->status_codes[i]);
4124 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4129 if (status != GOT_STATUS_NO_CHANGE) {
4130 if (status == GOT_STATUS_DELETE)
4132 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4133 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4136 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4137 err = got_error_set_errno(ENOENT, relpath);
4140 if (status != GOT_STATUS_MODIFY &&
4141 status != GOT_STATUS_MISSING) {
4142 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4147 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4151 if (unlinkat(dirfd, de_name, 0) == -1) {
4152 err = got_error_from_errno2("unlinkat",
4156 } else if (unlink(ondisk_path) == -1) {
4157 err = got_error_from_errno2("unlink", ondisk_path);
4161 root_len = strlen(a->worktree->root_path);
4164 err = got_path_dirname(&parent, ondisk_path);
4168 ondisk_path = parent;
4169 if (rmdir(ondisk_path) == -1) {
4170 if (errno != ENOTEMPTY)
4171 err = got_error_from_errno2("rmdir",
4175 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4176 strlen(ondisk_path), root_len) != 0);
4179 got_fileindex_entry_mark_deleted_from_disk(ie);
4184 if (status == GOT_STATUS_DELETE)
4186 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4187 staged_status, relpath);
4190 const struct got_error *
4191 got_worktree_schedule_delete(struct got_worktree *worktree,
4192 struct got_pathlist_head *paths, int delete_local_mods,
4193 const char *status_codes,
4194 got_worktree_delete_cb progress_cb, void *progress_arg,
4195 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4197 struct got_fileindex *fileindex = NULL;
4198 char *fileindex_path = NULL;
4199 const struct got_error *err = NULL, *sync_err, *unlockerr;
4200 struct got_pathlist_entry *pe;
4201 struct schedule_deletion_args sda;
4203 err = lock_worktree(worktree, LOCK_EX);
4207 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4211 sda.worktree = worktree;
4212 sda.fileindex = fileindex;
4213 sda.progress_cb = progress_cb;
4214 sda.progress_arg = progress_arg;
4216 sda.delete_local_mods = delete_local_mods;
4217 sda.keep_on_disk = keep_on_disk;
4218 sda.ignore_missing_paths = ignore_missing_paths;
4219 sda.status_codes = status_codes;
4221 TAILQ_FOREACH(pe, paths, entry) {
4222 err = worktree_status(worktree, pe->path, fileindex, repo,
4223 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4227 sync_err = sync_fileindex(fileindex, fileindex_path);
4228 if (sync_err && err == NULL)
4231 free(fileindex_path);
4233 got_fileindex_free(fileindex);
4234 unlockerr = lock_worktree(worktree, LOCK_SH);
4235 if (unlockerr && err == NULL)
4240 static const struct got_error *
4241 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4243 const struct got_error *err = NULL;
4245 size_t linesize = 0, n;
4248 linelen = getline(&line, &linesize, infile);
4249 if (linelen == -1) {
4250 if (ferror(infile)) {
4251 err = got_error_from_errno("getline");
4257 n = fwrite(line, 1, linelen, outfile);
4259 err = got_ferror(outfile, GOT_ERR_IO);
4264 n = fwrite(line, 1, linelen, rejectfile);
4266 err = got_ferror(rejectfile, GOT_ERR_IO);
4273 static const struct got_error *
4274 skip_one_line(FILE *f)
4277 size_t linesize = 0;
4280 linelen = getline(&line, &linesize, f);
4281 if (linelen == -1) {
4283 return got_error_from_errno("getline");
4290 static const struct got_error *
4291 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4292 int start_old, int end_old, int start_new, int end_new,
4293 FILE *outfile, FILE *rejectfile)
4295 const struct got_error *err;
4297 /* Copy old file's lines leading up to patch. */
4298 while (!feof(f1) && *line_cur1 < start_old) {
4299 err = copy_one_line(f1, outfile, NULL);
4304 /* Skip new file's lines leading up to patch. */
4305 while (!feof(f2) && *line_cur2 < start_new) {
4307 err = copy_one_line(f2, NULL, rejectfile);
4309 err = skip_one_line(f2);
4314 /* Copy patched lines. */
4315 while (!feof(f2) && *line_cur2 <= end_new) {
4316 err = copy_one_line(f2, outfile, NULL);
4321 /* Skip over old file's replaced lines. */
4322 while (!feof(f1) && *line_cur1 <= end_old) {
4324 err = copy_one_line(f1, NULL, rejectfile);
4326 err = skip_one_line(f1);
4335 static const struct got_error *
4336 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4337 FILE *outfile, FILE *rejectfile)
4339 const struct got_error *err;
4342 /* Copy old file's lines until EOF. */
4344 err = copy_one_line(f1, outfile, NULL);
4351 /* Copy new file's lines until EOF. */
4353 err = copy_one_line(f2, NULL, rejectfile);
4363 static const struct got_error *
4364 apply_or_reject_change(int *choice, int *nchunks_used,
4365 struct diff_result *diff_result, int n,
4366 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4367 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4368 got_worktree_patch_cb patch_cb, void *patch_arg)
4370 const struct got_error *err = NULL;
4371 struct diff_chunk_context cc = {};
4372 int start_old, end_old, start_new, end_new;
4374 struct diff_output_unidiff_state *diff_state;
4375 struct diff_input_info diff_info;
4378 *choice = GOT_PATCH_CHOICE_NONE;
4380 /* Get changed line numbers without context lines for copy_change(). */
4381 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4382 start_old = cc.left.start;
4383 end_old = cc.left.end;
4384 start_new = cc.right.start;
4385 end_new = cc.right.end;
4387 /* Get the same change with context lines for display. */
4388 memset(&cc, 0, sizeof(cc));
4389 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4391 memset(&diff_info, 0, sizeof(diff_info));
4392 diff_info.left_path = relpath;
4393 diff_info.right_path = relpath;
4395 diff_state = diff_output_unidiff_state_alloc();
4396 if (diff_state == NULL)
4397 return got_error_set_errno(ENOMEM,
4398 "diff_output_unidiff_state_alloc");
4400 hunkfile = got_opentemp();
4401 if (hunkfile == NULL) {
4402 err = got_error_from_errno("got_opentemp");
4406 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4408 if (rc != DIFF_RC_OK) {
4409 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4413 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4414 err = got_ferror(hunkfile, GOT_ERR_IO);
4418 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4419 hunkfile, changeno, nchanges);
4424 case GOT_PATCH_CHOICE_YES:
4425 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4426 end_old, start_new, end_new, outfile, rejectfile);
4428 case GOT_PATCH_CHOICE_NO:
4429 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4430 end_old, start_new, end_new, rejectfile, outfile);
4432 case GOT_PATCH_CHOICE_QUIT:
4435 err = got_error(GOT_ERR_PATCH_CHOICE);
4439 diff_output_unidiff_state_free(diff_state);
4440 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4441 err = got_error_from_errno("fclose");
4445 struct revert_file_args {
4446 struct got_worktree *worktree;
4447 struct got_fileindex *fileindex;
4448 got_worktree_checkout_cb progress_cb;
4450 got_worktree_patch_cb patch_cb;
4452 struct got_repository *repo;
4453 int unlink_added_files;
4456 static const struct got_error *
4457 create_patched_content(char **path_outfile, int reverse_patch,
4458 struct got_object_id *blob_id, const char *path2,
4459 int dirfd2, const char *de_name2,
4460 const char *relpath, struct got_repository *repo,
4461 got_worktree_patch_cb patch_cb, void *patch_arg)
4463 const struct got_error *err, *free_err;
4464 struct got_blob_object *blob = NULL;
4465 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4466 int fd = -1, fd2 = -1;
4467 char link_target[PATH_MAX];
4468 ssize_t link_len = 0;
4469 char *path1 = NULL, *id_str = NULL;
4471 struct got_diffreg_result *diffreg_result = NULL;
4472 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4473 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4475 *path_outfile = NULL;
4477 err = got_object_id_str(&id_str, blob_id);
4482 fd2 = openat(dirfd2, de_name2,
4483 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4485 if (!got_err_open_nofollow_on_symlink()) {
4486 err = got_error_from_errno2("openat", path2);
4489 link_len = readlinkat(dirfd2, de_name2,
4490 link_target, sizeof(link_target));
4491 if (link_len == -1) {
4492 return got_error_from_errno2("readlinkat",
4495 sb2.st_mode = S_IFLNK;
4496 sb2.st_size = link_len;
4499 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4501 if (!got_err_open_nofollow_on_symlink()) {
4502 err = got_error_from_errno2("open", path2);
4505 link_len = readlink(path2, link_target,
4506 sizeof(link_target));
4508 return got_error_from_errno2("readlink", path2);
4509 sb2.st_mode = S_IFLNK;
4510 sb2.st_size = link_len;
4514 if (fstat(fd2, &sb2) == -1) {
4515 err = got_error_from_errno2("fstat", path2);
4519 f2 = fdopen(fd2, "r");
4521 err = got_error_from_errno2("fdopen", path2);
4527 f2 = got_opentemp();
4529 err = got_error_from_errno2("got_opentemp", path2);
4532 n = fwrite(link_target, 1, link_len, f2);
4533 if (n != link_len) {
4534 err = got_ferror(f2, GOT_ERR_IO);
4537 if (fflush(f2) == EOF) {
4538 err = got_error_from_errno("fflush");
4544 fd = got_opentempfd();
4546 err = got_error_from_errno("got_opentempfd");
4550 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4554 err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4558 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4562 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4563 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4567 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4572 if (fseek(f1, 0L, SEEK_SET) == -1)
4573 return got_ferror(f1, GOT_ERR_IO);
4574 if (fseek(f2, 0L, SEEK_SET) == -1)
4575 return got_ferror(f2, GOT_ERR_IO);
4577 /* Count the number of actual changes in the diff result. */
4578 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4579 struct diff_chunk_context cc = {};
4580 diff_chunk_context_load_change(&cc, &nchunks_used,
4581 diffreg_result->result, n, 0);
4584 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4586 err = apply_or_reject_change(&choice, &nchunks_used,
4587 diffreg_result->result, n, relpath, f1, f2,
4588 &line_cur1, &line_cur2,
4589 reverse_patch ? NULL : outfile,
4590 reverse_patch ? outfile : NULL,
4591 ++i, nchanges, patch_cb, patch_arg);
4594 if (choice == GOT_PATCH_CHOICE_YES)
4596 else if (choice == GOT_PATCH_CHOICE_QUIT)
4600 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4601 reverse_patch ? NULL : outfile,
4602 reverse_patch ? outfile : NULL);
4606 if (!S_ISLNK(sb2.st_mode)) {
4609 mode = apply_umask(sb2.st_mode);
4610 if (fchmod(fileno(outfile), mode) == -1) {
4611 err = got_error_from_errno2("fchmod", path2);
4618 if (fd != -1 && close(fd) == -1 && err == NULL)
4619 err = got_error_from_errno("close");
4621 got_object_blob_close(blob);
4622 free_err = got_diffreg_result_free(diffreg_result);
4625 if (f1 && fclose(f1) == EOF && err == NULL)
4626 err = got_error_from_errno2("fclose", path1);
4627 if (f2 && fclose(f2) == EOF && err == NULL)
4628 err = got_error_from_errno2("fclose", path2);
4629 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4630 err = got_error_from_errno2("close", path2);
4631 if (outfile && fclose(outfile) == EOF && err == NULL)
4632 err = got_error_from_errno2("fclose", *path_outfile);
4633 if (path1 && unlink(path1) == -1 && err == NULL)
4634 err = got_error_from_errno2("unlink", path1);
4635 if (err || !have_content) {
4636 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4637 err = got_error_from_errno2("unlink", *path_outfile);
4638 free(*path_outfile);
4639 *path_outfile = NULL;
4645 static const struct got_error *
4646 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4647 const char *relpath, struct got_object_id *blob_id,
4648 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4649 int dirfd, const char *de_name)
4651 struct revert_file_args *a = arg;
4652 const struct got_error *err = NULL;
4653 char *parent_path = NULL;
4654 struct got_fileindex_entry *ie;
4655 struct got_commit_object *base_commit = NULL;
4656 struct got_tree_object *tree = NULL;
4657 struct got_object_id *tree_id = NULL;
4658 const struct got_tree_entry *te = NULL;
4659 char *tree_path = NULL, *te_name;
4660 char *ondisk_path = NULL, *path_content = NULL;
4661 struct got_blob_object *blob = NULL;
4664 /* Reverting a staged deletion is a no-op. */
4665 if (status == GOT_STATUS_DELETE &&
4666 staged_status != GOT_STATUS_NO_CHANGE)
4669 if (status == GOT_STATUS_UNVERSIONED)
4670 return (*a->progress_cb)(a->progress_arg,
4671 GOT_STATUS_UNVERSIONED, relpath);
4673 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4675 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4677 /* Construct in-repository path of tree which contains this blob. */
4678 err = got_path_dirname(&parent_path, ie->path);
4680 if (err->code != GOT_ERR_BAD_PATH)
4682 parent_path = strdup("/");
4683 if (parent_path == NULL) {
4684 err = got_error_from_errno("strdup");
4688 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4689 tree_path = strdup(parent_path);
4690 if (tree_path == NULL) {
4691 err = got_error_from_errno("strdup");
4695 if (got_path_is_root_dir(parent_path)) {
4696 tree_path = strdup(a->worktree->path_prefix);
4697 if (tree_path == NULL) {
4698 err = got_error_from_errno("strdup");
4702 if (asprintf(&tree_path, "%s/%s",
4703 a->worktree->path_prefix, parent_path) == -1) {
4704 err = got_error_from_errno("asprintf");
4710 err = got_object_open_as_commit(&base_commit, a->repo,
4711 a->worktree->base_commit_id);
4715 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4717 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4718 (status == GOT_STATUS_ADD ||
4719 staged_status == GOT_STATUS_ADD)))
4722 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4726 err = got_path_basename(&te_name, ie->path);
4730 te = got_object_tree_find_entry(tree, te_name);
4732 if (te == NULL && status != GOT_STATUS_ADD &&
4733 staged_status != GOT_STATUS_ADD) {
4734 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4740 case GOT_STATUS_ADD:
4742 int choice = GOT_PATCH_CHOICE_NONE;
4743 err = (*a->patch_cb)(&choice, a->patch_arg,
4744 status, ie->path, NULL, 1, 1);
4747 if (choice != GOT_PATCH_CHOICE_YES)
4750 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4754 got_fileindex_entry_remove(a->fileindex, ie);
4755 if (a->unlink_added_files) {
4756 if (asprintf(&ondisk_path, "%s/%s",
4757 got_worktree_get_root_path(a->worktree),
4759 err = got_error_from_errno("asprintf");
4762 if (unlink(ondisk_path) == -1) {
4763 err = got_error_from_errno2("unlink",
4769 case GOT_STATUS_DELETE:
4771 int choice = GOT_PATCH_CHOICE_NONE;
4772 err = (*a->patch_cb)(&choice, a->patch_arg,
4773 status, ie->path, NULL, 1, 1);
4776 if (choice != GOT_PATCH_CHOICE_YES)
4780 case GOT_STATUS_MODIFY:
4781 case GOT_STATUS_MODE_CHANGE:
4782 case GOT_STATUS_CONFLICT:
4783 case GOT_STATUS_MISSING: {
4784 struct got_object_id id;
4785 if (staged_status == GOT_STATUS_ADD ||
4786 staged_status == GOT_STATUS_MODIFY) {
4787 memcpy(id.hash, ie->staged_blob_hash,
4788 SHA1_DIGEST_LENGTH);
4790 memcpy(id.hash, ie->blob_hash,
4791 SHA1_DIGEST_LENGTH);
4792 fd = got_opentempfd();
4794 err = got_error_from_errno("got_opentempfd");
4798 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4802 if (asprintf(&ondisk_path, "%s/%s",
4803 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4804 err = got_error_from_errno("asprintf");
4808 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4809 status == GOT_STATUS_CONFLICT)) {
4810 int is_bad_symlink = 0;
4811 err = create_patched_content(&path_content, 1, &id,
4812 ondisk_path, dirfd, de_name, ie->path, a->repo,
4813 a->patch_cb, a->patch_arg);
4814 if (err || path_content == NULL)
4816 if (te && S_ISLNK(te->mode)) {
4817 if (unlink(path_content) == -1) {
4818 err = got_error_from_errno2("unlink",
4822 err = install_symlink(&is_bad_symlink,
4823 a->worktree, ondisk_path, ie->path,
4824 blob, 0, 1, 0, 0, a->repo,
4825 a->progress_cb, a->progress_arg);
4827 if (rename(path_content, ondisk_path) == -1) {
4828 err = got_error_from_errno3("rename",
4829 path_content, ondisk_path);
4834 int is_bad_symlink = 0;
4835 if (te && S_ISLNK(te->mode)) {
4836 err = install_symlink(&is_bad_symlink,
4837 a->worktree, ondisk_path, ie->path,
4838 blob, 0, 1, 0, 0, a->repo,
4839 a->progress_cb, a->progress_arg);
4841 err = install_blob(a->worktree, ondisk_path,
4843 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4844 got_fileindex_perms_to_st(ie), blob,
4845 0, 1, 0, 0, a->repo,
4846 a->progress_cb, a->progress_arg);
4850 if (status == GOT_STATUS_DELETE ||
4851 status == GOT_STATUS_MODE_CHANGE) {
4852 err = got_fileindex_entry_update(ie,
4853 a->worktree->root_fd, relpath,
4855 a->worktree->base_commit_id->hash, 1);
4859 if (is_bad_symlink) {
4860 got_fileindex_entry_filetype_set(ie,
4861 GOT_FILEIDX_MODE_BAD_SYMLINK);
4874 if (fd != -1 && close(fd) == -1 && err == NULL)
4875 err = got_error_from_errno("close");
4877 got_object_blob_close(blob);
4879 got_object_tree_close(tree);
4882 got_object_commit_close(base_commit);
4886 const struct got_error *
4887 got_worktree_revert(struct got_worktree *worktree,
4888 struct got_pathlist_head *paths,
4889 got_worktree_checkout_cb progress_cb, void *progress_arg,
4890 got_worktree_patch_cb patch_cb, void *patch_arg,
4891 struct got_repository *repo)
4893 struct got_fileindex *fileindex = NULL;
4894 char *fileindex_path = NULL;
4895 const struct got_error *err = NULL, *unlockerr = NULL;
4896 const struct got_error *sync_err = NULL;
4897 struct got_pathlist_entry *pe;
4898 struct revert_file_args rfa;
4900 err = lock_worktree(worktree, LOCK_EX);
4904 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4908 rfa.worktree = worktree;
4909 rfa.fileindex = fileindex;
4910 rfa.progress_cb = progress_cb;
4911 rfa.progress_arg = progress_arg;
4912 rfa.patch_cb = patch_cb;
4913 rfa.patch_arg = patch_arg;
4915 rfa.unlink_added_files = 0;
4916 TAILQ_FOREACH(pe, paths, entry) {
4917 err = worktree_status(worktree, pe->path, fileindex, repo,
4918 revert_file, &rfa, NULL, NULL, 1, 0);
4922 sync_err = sync_fileindex(fileindex, fileindex_path);
4923 if (sync_err && err == NULL)
4926 free(fileindex_path);
4928 got_fileindex_free(fileindex);
4929 unlockerr = lock_worktree(worktree, LOCK_SH);
4930 if (unlockerr && err == NULL)
4936 free_commitable(struct got_commitable *ct)
4939 free(ct->in_repo_path);
4940 free(ct->ondisk_path);
4942 free(ct->base_blob_id);
4943 free(ct->staged_blob_id);
4944 free(ct->base_commit_id);
4948 struct collect_commitables_arg {
4949 struct got_pathlist_head *commitable_paths;
4950 struct got_repository *repo;
4951 struct got_worktree *worktree;
4952 struct got_fileindex *fileindex;
4953 int have_staged_files;
4954 int allow_bad_symlinks;
4955 int diff_header_shown;
4962 * Create a file which contains the target path of a symlink so we can feed
4963 * it as content to the diff engine.
4965 static const struct got_error *
4966 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4967 const char *abspath)
4969 const struct got_error *err = NULL;
4970 char target_path[PATH_MAX];
4971 ssize_t target_len, outlen;
4976 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4977 if (target_len == -1)
4978 return got_error_from_errno2("readlinkat", abspath);
4980 target_len = readlink(abspath, target_path, PATH_MAX);
4981 if (target_len == -1)
4982 return got_error_from_errno2("readlink", abspath);
4985 *fd = got_opentempfd();
4987 return got_error_from_errno("got_opentempfd");
4989 outlen = write(*fd, target_path, target_len);
4991 err = got_error_from_errno("got_opentempfd");
4995 if (lseek(*fd, 0, SEEK_SET) == -1) {
4996 err = got_error_from_errno2("lseek", abspath);
5007 static const struct got_error *
5008 append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5009 FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5010 int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5012 const struct got_error *err = NULL;
5013 struct got_blob_object *blob1 = NULL;
5014 int fd = -1, fd1 = -1, fd2 = -1;
5015 FILE *ondisk_file = NULL;
5016 char *label1 = NULL;
5020 char *id_str = NULL;
5022 memset(&sb, 0, sizeof(sb));
5025 if (ct->staged_status != GOT_STATUS_MODIFY &&
5026 ct->staged_status != GOT_STATUS_ADD &&
5027 ct->staged_status != GOT_STATUS_DELETE)
5030 if (ct->status != GOT_STATUS_MODIFY &&
5031 ct->status != GOT_STATUS_ADD &&
5032 ct->status != GOT_STATUS_DELETE)
5036 err = got_opentemp_truncate(f1);
5038 return got_error_from_errno("got_opentemp_truncate");
5039 err = got_opentemp_truncate(f2);
5041 return got_error_from_errno("got_opentemp_truncate");
5043 if (!*diff_header_shown) {
5044 err = got_object_id_str(&id_str, worktree->base_commit_id);
5047 fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5048 got_worktree_get_root_path(worktree));
5049 fprintf(diff_outfile, "commit - %s\n", id_str);
5050 fprintf(diff_outfile, "path + %s%s\n",
5051 got_worktree_get_root_path(worktree),
5052 diff_staged ? " (staged changes)" : "");
5053 *diff_header_shown = 1;
5057 const char *label1 = NULL, *label2 = NULL;
5058 switch (ct->staged_status) {
5059 case GOT_STATUS_MODIFY:
5063 case GOT_STATUS_ADD:
5066 case GOT_STATUS_DELETE:
5070 return got_error(GOT_ERR_FILE_STATUS);
5072 fd1 = got_opentempfd();
5074 err = got_error_from_errno("got_opentempfd");
5077 fd2 = got_opentempfd();
5079 err = got_error_from_errno("got_opentempfd");
5082 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5083 fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5084 label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5085 NULL, repo, diff_outfile);
5089 fd1 = got_opentempfd();
5091 err = got_error_from_errno("got_opentempfd");
5095 if (ct->status != GOT_STATUS_ADD) {
5096 err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5102 if (ct->status != GOT_STATUS_DELETE) {
5104 fd = openat(dirfd, de_name,
5105 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5107 if (!got_err_open_nofollow_on_symlink()) {
5108 err = got_error_from_errno2("openat",
5112 err = get_symlink_target_file(&fd, dirfd,
5113 de_name, ct->ondisk_path);
5118 fd = open(ct->ondisk_path,
5119 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5121 if (!got_err_open_nofollow_on_symlink()) {
5122 err = got_error_from_errno2("open",
5126 err = get_symlink_target_file(&fd, dirfd,
5127 de_name, ct->ondisk_path);
5132 if (fstatat(fd, ct->ondisk_path, &sb,
5133 AT_SYMLINK_NOFOLLOW) == -1) {
5134 err = got_error_from_errno2("fstatat", ct->ondisk_path);
5137 ondisk_file = fdopen(fd, "r");
5138 if (ondisk_file == NULL) {
5139 err = got_error_from_errno2("fdopen", ct->ondisk_path);
5147 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5153 err = got_diff_blob_file(blob1, f1, size1, label1,
5154 ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5155 GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5157 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5158 err = got_error_from_errno("close");
5159 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5160 err = got_error_from_errno("close");
5162 got_object_blob_close(blob1);
5163 if (fd != -1 && close(fd) == -1 && err == NULL)
5164 err = got_error_from_errno("close");
5165 if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5166 err = got_error_from_errno("fclose");
5170 static const struct got_error *
5171 collect_commitables(void *arg, unsigned char status,
5172 unsigned char staged_status, const char *relpath,
5173 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5174 struct got_object_id *commit_id, int dirfd, const char *de_name)
5176 struct collect_commitables_arg *a = arg;
5177 const struct got_error *err = NULL;
5178 struct got_commitable *ct = NULL;
5179 struct got_pathlist_entry *new = NULL;
5180 char *parent_path = NULL, *path = NULL;
5183 if (a->have_staged_files) {
5184 if (staged_status != GOT_STATUS_MODIFY &&
5185 staged_status != GOT_STATUS_ADD &&
5186 staged_status != GOT_STATUS_DELETE)
5189 if (status == GOT_STATUS_CONFLICT)
5190 return got_error(GOT_ERR_COMMIT_CONFLICT);
5192 if (status != GOT_STATUS_MODIFY &&
5193 status != GOT_STATUS_MODE_CHANGE &&
5194 status != GOT_STATUS_ADD &&
5195 status != GOT_STATUS_DELETE)
5199 if (asprintf(&path, "/%s", relpath) == -1) {
5200 err = got_error_from_errno("asprintf");
5203 if (strcmp(path, "/") == 0) {
5204 parent_path = strdup("");
5205 if (parent_path == NULL)
5206 return got_error_from_errno("strdup");
5208 err = got_path_dirname(&parent_path, path);
5213 ct = calloc(1, sizeof(*ct));
5215 err = got_error_from_errno("calloc");
5219 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5221 err = got_error_from_errno("asprintf");
5225 if (staged_status == GOT_STATUS_ADD ||
5226 staged_status == GOT_STATUS_MODIFY) {
5227 struct got_fileindex_entry *ie;
5228 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5229 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5230 case GOT_FILEIDX_MODE_REGULAR_FILE:
5231 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5234 case GOT_FILEIDX_MODE_SYMLINK:
5238 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5241 ct->mode |= got_fileindex_entry_perms_get(ie);
5242 } else if (status != GOT_STATUS_DELETE &&
5243 staged_status != GOT_STATUS_DELETE) {
5245 if (fstatat(dirfd, de_name, &sb,
5246 AT_SYMLINK_NOFOLLOW) == -1) {
5247 err = got_error_from_errno2("fstatat",
5251 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5252 err = got_error_from_errno2("lstat", ct->ondisk_path);
5255 ct->mode = sb.st_mode;
5258 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5259 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5261 err = got_error_from_errno("asprintf");
5265 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5266 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5268 char target_path[PATH_MAX];
5270 target_len = readlink(ct->ondisk_path, target_path,
5271 sizeof(target_path));
5272 if (target_len == -1) {
5273 err = got_error_from_errno2("readlink",
5277 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5278 target_len, ct->ondisk_path, a->worktree->root_path);
5281 if (is_bad_symlink) {
5282 err = got_error_path(ct->ondisk_path,
5283 GOT_ERR_BAD_SYMLINK);
5289 ct->status = status;
5290 ct->staged_status = staged_status;
5291 ct->blob_id = NULL; /* will be filled in when blob gets created */
5292 if (ct->status != GOT_STATUS_ADD &&
5293 ct->staged_status != GOT_STATUS_ADD) {
5294 ct->base_blob_id = got_object_id_dup(blob_id);
5295 if (ct->base_blob_id == NULL) {
5296 err = got_error_from_errno("got_object_id_dup");
5299 ct->base_commit_id = got_object_id_dup(commit_id);
5300 if (ct->base_commit_id == NULL) {
5301 err = got_error_from_errno("got_object_id_dup");
5305 if (ct->staged_status == GOT_STATUS_ADD ||
5306 ct->staged_status == GOT_STATUS_MODIFY) {
5307 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5308 if (ct->staged_blob_id == NULL) {
5309 err = got_error_from_errno("got_object_id_dup");
5313 ct->path = strdup(path);
5314 if (ct->path == NULL) {
5315 err = got_error_from_errno("strdup");
5318 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5322 if (a->diff_outfile && ct && new != NULL) {
5323 err = append_ct_diff(ct, &a->diff_header_shown,
5324 a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5325 a->have_staged_files, a->repo, a->worktree);
5330 if (ct && (err || new == NULL))
5331 free_commitable(ct);
5337 static const struct got_error *write_tree(struct got_object_id **, int *,
5338 struct got_tree_object *, const char *, struct got_pathlist_head *,
5339 got_worktree_status_cb status_cb, void *status_arg,
5340 struct got_repository *);
5342 static const struct got_error *
5343 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5344 struct got_tree_entry *te, const char *parent_path,
5345 struct got_pathlist_head *commitable_paths,
5346 got_worktree_status_cb status_cb, void *status_arg,
5347 struct got_repository *repo)
5349 const struct got_error *err = NULL;
5350 struct got_tree_object *subtree;
5353 if (asprintf(&subpath, "%s%s%s", parent_path,
5354 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5355 return got_error_from_errno("asprintf");
5357 err = got_object_open_as_tree(&subtree, repo, &te->id);
5361 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5362 commitable_paths, status_cb, status_arg, repo);
5363 got_object_tree_close(subtree);
5368 static const struct got_error *
5369 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5371 const struct got_error *err = NULL;
5372 char *ct_parent_path = NULL;
5376 if (strchr(ct->in_repo_path, '/') == NULL) {
5377 *match = got_path_is_root_dir(path);
5381 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5384 *match = (strcmp(path, ct_parent_path) == 0);
5385 free(ct_parent_path);
5390 get_ct_file_mode(struct got_commitable *ct)
5392 if (S_ISLNK(ct->mode))
5395 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5398 static const struct got_error *
5399 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5400 struct got_tree_entry *te, struct got_commitable *ct)
5402 const struct got_error *err = NULL;
5406 err = got_object_tree_entry_dup(new_te, te);
5410 (*new_te)->mode = get_ct_file_mode(ct);
5412 if (ct->staged_status == GOT_STATUS_MODIFY)
5413 memcpy(&(*new_te)->id, ct->staged_blob_id,
5414 sizeof((*new_te)->id));
5416 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5418 if (err && *new_te) {
5425 static const struct got_error *
5426 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5427 struct got_commitable *ct)
5429 const struct got_error *err = NULL;
5430 char *ct_name = NULL;
5434 *new_te = calloc(1, sizeof(**new_te));
5435 if (*new_te == NULL)
5436 return got_error_from_errno("calloc");
5438 err = got_path_basename(&ct_name, ct->path);
5441 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5442 sizeof((*new_te)->name)) {
5443 err = got_error(GOT_ERR_NO_SPACE);
5447 (*new_te)->mode = get_ct_file_mode(ct);
5449 if (ct->staged_status == GOT_STATUS_ADD)
5450 memcpy(&(*new_te)->id, ct->staged_blob_id,
5451 sizeof((*new_te)->id));
5453 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5456 if (err && *new_te) {
5463 static const struct got_error *
5464 insert_tree_entry(struct got_tree_entry *new_te,
5465 struct got_pathlist_head *paths)
5467 const struct got_error *err = NULL;
5468 struct got_pathlist_entry *new_pe;
5470 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5474 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5478 static const struct got_error *
5479 report_ct_status(struct got_commitable *ct,
5480 got_worktree_status_cb status_cb, void *status_arg)
5482 const char *ct_path = ct->path;
5483 unsigned char status;
5485 if (status_cb == NULL) /* no commit progress output desired */
5488 while (ct_path[0] == '/')
5491 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5492 status = ct->staged_status;
5494 status = ct->status;
5496 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5497 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5500 static const struct got_error *
5501 match_modified_subtree(int *modified, struct got_tree_entry *te,
5502 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5504 const struct got_error *err = NULL;
5505 struct got_pathlist_entry *pe;
5510 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5511 got_path_is_root_dir(base_tree_path) ? "" : "/",
5513 return got_error_from_errno("asprintf");
5515 TAILQ_FOREACH(pe, commitable_paths, entry) {
5516 struct got_commitable *ct = pe->data;
5517 *modified = got_path_is_child(ct->in_repo_path, te_path,
5527 static const struct got_error *
5528 match_deleted_or_modified_ct(struct got_commitable **ctp,
5529 struct got_tree_entry *te, const char *base_tree_path,
5530 struct got_pathlist_head *commitable_paths)
5532 const struct got_error *err = NULL;
5533 struct got_pathlist_entry *pe;
5537 TAILQ_FOREACH(pe, commitable_paths, entry) {
5538 struct got_commitable *ct = pe->data;
5539 char *ct_name = NULL;
5542 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5543 if (ct->status != GOT_STATUS_MODIFY &&
5544 ct->status != GOT_STATUS_MODE_CHANGE &&
5545 ct->status != GOT_STATUS_DELETE)
5548 if (ct->staged_status != GOT_STATUS_MODIFY &&
5549 ct->staged_status != GOT_STATUS_DELETE)
5553 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5556 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5562 err = got_path_basename(&ct_name, pe->path);
5566 if (strcmp(te->name, ct_name) != 0) {
5579 static const struct got_error *
5580 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5581 const char *child_path, const char *path_base_tree,
5582 struct got_pathlist_head *commitable_paths,
5583 got_worktree_status_cb status_cb, void *status_arg,
5584 struct got_repository *repo)
5586 const struct got_error *err = NULL;
5587 struct got_tree_entry *new_te;
5589 struct got_object_id *id = NULL;
5594 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5595 got_path_is_root_dir(path_base_tree) ? "" : "/",
5597 return got_error_from_errno("asprintf");
5599 new_te = calloc(1, sizeof(*new_te));
5601 return got_error_from_errno("calloc");
5602 new_te->mode = S_IFDIR;
5604 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5605 sizeof(new_te->name)) {
5606 err = got_error(GOT_ERR_NO_SPACE);
5609 err = write_tree(&id, &nentries, NULL, subtree_path,
5610 commitable_paths, status_cb, status_arg, repo);
5615 memcpy(&new_te->id, id, sizeof(new_te->id));
5624 static const struct got_error *
5625 write_tree(struct got_object_id **new_tree_id, int *nentries,
5626 struct got_tree_object *base_tree, const char *path_base_tree,
5627 struct got_pathlist_head *commitable_paths,
5628 got_worktree_status_cb status_cb, void *status_arg,
5629 struct got_repository *repo)
5631 const struct got_error *err = NULL;
5632 struct got_pathlist_head paths;
5633 struct got_tree_entry *te, *new_te = NULL;
5634 struct got_pathlist_entry *pe;
5639 /* Insert, and recurse into, newly added entries first. */
5640 TAILQ_FOREACH(pe, commitable_paths, entry) {
5641 struct got_commitable *ct = pe->data;
5642 char *child_path = NULL, *slash;
5644 if ((ct->status != GOT_STATUS_ADD &&
5645 ct->staged_status != GOT_STATUS_ADD) ||
5646 (ct->flags & GOT_COMMITABLE_ADDED))
5649 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5650 strlen(path_base_tree)))
5653 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5658 slash = strchr(child_path, '/');
5659 if (slash == NULL) {
5660 err = alloc_added_blob_tree_entry(&new_te, ct);
5663 err = report_ct_status(ct, status_cb, status_arg);
5666 ct->flags |= GOT_COMMITABLE_ADDED;
5667 err = insert_tree_entry(new_te, &paths);
5672 *slash = '\0'; /* trim trailing path components */
5673 if (base_tree == NULL ||
5674 got_object_tree_find_entry(base_tree, child_path)
5676 err = make_subtree_for_added_blob(&new_te,
5677 child_path, path_base_tree,
5678 commitable_paths, status_cb, status_arg,
5682 err = insert_tree_entry(new_te, &paths);
5691 int i, nbase_entries;
5692 /* Handle modified and deleted entries. */
5693 nbase_entries = got_object_tree_get_nentries(base_tree);
5694 for (i = 0; i < nbase_entries; i++) {
5695 struct got_commitable *ct = NULL;
5697 te = got_object_tree_get_entry(base_tree, i);
5698 if (got_object_tree_entry_is_submodule(te)) {
5699 /* Entry is a submodule; just copy it. */
5700 err = got_object_tree_entry_dup(&new_te, te);
5703 err = insert_tree_entry(new_te, &paths);
5710 if (S_ISDIR(te->mode)) {
5712 err = got_object_tree_entry_dup(&new_te, te);
5715 err = match_modified_subtree(&modified, te,
5716 path_base_tree, commitable_paths);
5719 /* Avoid recursion into unmodified subtrees. */
5721 struct got_object_id *new_id;
5723 err = write_subtree(&new_id,
5725 path_base_tree, commitable_paths,
5726 status_cb, status_arg, repo);
5729 if (nsubentries == 0) {
5730 /* All entries were deleted. */
5734 memcpy(&new_te->id, new_id,
5735 sizeof(new_te->id));
5738 err = insert_tree_entry(new_te, &paths);
5745 err = match_deleted_or_modified_ct(&ct, te,
5746 path_base_tree, commitable_paths);
5750 /* NB: Deleted entries get dropped here. */
5751 if (ct->status == GOT_STATUS_MODIFY ||
5752 ct->status == GOT_STATUS_MODE_CHANGE ||
5753 ct->staged_status == GOT_STATUS_MODIFY) {
5754 err = alloc_modified_blob_tree_entry(
5758 err = insert_tree_entry(new_te, &paths);
5763 err = report_ct_status(ct, status_cb,
5768 /* Entry is unchanged; just copy it. */
5769 err = got_object_tree_entry_dup(&new_te, te);
5772 err = insert_tree_entry(new_te, &paths);
5780 /* Write new list of entries; deleted entries have been dropped. */
5781 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5783 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
5787 static const struct got_error *
5788 update_fileindex_after_commit(struct got_worktree *worktree,
5789 struct got_pathlist_head *commitable_paths,
5790 struct got_object_id *new_base_commit_id,
5791 struct got_fileindex *fileindex, int have_staged_files)
5793 const struct got_error *err = NULL;
5794 struct got_pathlist_entry *pe;
5795 char *relpath = NULL;
5797 TAILQ_FOREACH(pe, commitable_paths, entry) {
5798 struct got_fileindex_entry *ie;
5799 struct got_commitable *ct = pe->data;
5801 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5803 err = got_path_skip_common_ancestor(&relpath,
5804 worktree->root_path, ct->ondisk_path);
5809 if (ct->status == GOT_STATUS_DELETE ||
5810 ct->staged_status == GOT_STATUS_DELETE) {
5811 got_fileindex_entry_remove(fileindex, ie);
5812 } else if (ct->staged_status == GOT_STATUS_ADD ||
5813 ct->staged_status == GOT_STATUS_MODIFY) {
5814 got_fileindex_entry_stage_set(ie,
5815 GOT_FILEIDX_STAGE_NONE);
5816 got_fileindex_entry_staged_filetype_set(ie, 0);
5818 err = got_fileindex_entry_update(ie,
5819 worktree->root_fd, relpath,
5820 ct->staged_blob_id->hash,
5821 new_base_commit_id->hash,
5822 !have_staged_files);
5824 err = got_fileindex_entry_update(ie,
5825 worktree->root_fd, relpath,
5827 new_base_commit_id->hash,
5828 !have_staged_files);
5830 err = got_fileindex_entry_alloc(&ie, pe->path);
5833 err = got_fileindex_entry_update(ie,
5834 worktree->root_fd, relpath, ct->blob_id->hash,
5835 new_base_commit_id->hash, 1);
5837 got_fileindex_entry_free(ie);
5840 err = got_fileindex_entry_add(fileindex, ie);
5842 got_fileindex_entry_free(ie);
5855 static const struct got_error *
5856 check_out_of_date(const char *in_repo_path, unsigned char status,
5857 unsigned char staged_status, struct got_object_id *base_blob_id,
5858 struct got_object_id *base_commit_id,
5859 struct got_object_id *head_commit_id, struct got_repository *repo,
5862 const struct got_error *err = NULL;
5863 struct got_commit_object *commit = NULL;
5864 struct got_object_id *id = NULL;
5866 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5867 /* Trivial case: base commit == head commit */
5868 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5871 * Ensure file content which local changes were based
5872 * on matches file content in the branch head.
5874 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5877 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5879 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5880 err = got_error(ood_errcode);
5882 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5883 err = got_error(ood_errcode);
5885 /* Require that added files don't exist in the branch head. */
5886 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5889 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5890 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5892 err = id ? got_error(ood_errcode) : NULL;
5897 got_object_commit_close(commit);
5901 static const struct got_error *
5902 commit_worktree(struct got_object_id **new_commit_id,
5903 struct got_pathlist_head *commitable_paths,
5904 struct got_object_id *head_commit_id,
5905 struct got_object_id *parent_id2,
5906 struct got_worktree *worktree,
5907 const char *author, const char *committer, char *diff_path,
5908 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5909 got_worktree_status_cb status_cb, void *status_arg,
5910 struct got_repository *repo)
5912 const struct got_error *err = NULL, *unlockerr = NULL;
5913 struct got_pathlist_entry *pe;
5914 const char *head_ref_name = NULL;
5915 struct got_commit_object *head_commit = NULL;
5916 struct got_reference *head_ref2 = NULL;
5917 struct got_object_id *head_commit_id2 = NULL;
5918 struct got_tree_object *head_tree = NULL;
5919 struct got_object_id *new_tree_id = NULL;
5920 int nentries, nparents = 0;
5921 struct got_object_id_queue parent_ids;
5922 struct got_object_qid *pid = NULL;
5923 char *logmsg = NULL;
5926 *new_commit_id = NULL;
5928 STAILQ_INIT(&parent_ids);
5930 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5934 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5938 if (commit_msg_cb != NULL) {
5939 err = commit_msg_cb(commitable_paths, diff_path,
5940 &logmsg, commit_arg);
5945 if (logmsg == NULL || strlen(logmsg) == 0) {
5946 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5950 /* Create blobs from added and modified files and record their IDs. */
5951 TAILQ_FOREACH(pe, commitable_paths, entry) {
5952 struct got_commitable *ct = pe->data;
5955 /* Blobs for staged files already exist. */
5956 if (ct->staged_status == GOT_STATUS_ADD ||
5957 ct->staged_status == GOT_STATUS_MODIFY)
5960 if (ct->status != GOT_STATUS_ADD &&
5961 ct->status != GOT_STATUS_MODIFY &&
5962 ct->status != GOT_STATUS_MODE_CHANGE)
5965 if (asprintf(&ondisk_path, "%s/%s",
5966 worktree->root_path, pe->path) == -1) {
5967 err = got_error_from_errno("asprintf");
5970 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5976 /* Recursively write new tree objects. */
5977 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5978 commitable_paths, status_cb, status_arg, repo);
5982 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5985 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5988 err = got_object_qid_alloc(&pid, parent_id2);
5991 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5994 timestamp = time(NULL);
5995 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5996 nparents, author, timestamp, committer, timestamp, logmsg, repo);
6002 /* Check if a concurrent commit to our branch has occurred. */
6003 head_ref_name = got_worktree_get_head_ref_name(worktree);
6004 if (head_ref_name == NULL) {
6005 err = got_error_from_errno("got_worktree_get_head_ref_name");
6008 /* Lock the reference here to prevent concurrent modification. */
6009 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6012 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6015 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6016 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6019 /* Update branch head in repository. */
6020 err = got_ref_change_ref(head_ref2, *new_commit_id);
6023 err = got_ref_write(head_ref2, repo);
6027 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6031 err = ref_base_commit(worktree, repo);
6035 got_object_id_queue_free(&parent_ids);
6037 got_object_tree_close(head_tree);
6039 got_object_commit_close(head_commit);
6040 free(head_commit_id2);
6042 unlockerr = got_ref_unlock(head_ref2);
6043 if (unlockerr && err == NULL)
6045 got_ref_close(head_ref2);
6050 static const struct got_error *
6051 check_path_is_commitable(const char *path,
6052 struct got_pathlist_head *commitable_paths)
6054 struct got_pathlist_entry *cpe = NULL;
6055 size_t path_len = strlen(path);
6057 TAILQ_FOREACH(cpe, commitable_paths, entry) {
6058 struct got_commitable *ct = cpe->data;
6059 const char *ct_path = ct->path;
6061 while (ct_path[0] == '/')
6064 if (strcmp(path, ct_path) == 0 ||
6065 got_path_is_child(ct_path, path, path_len))
6070 return got_error_path(path, GOT_ERR_BAD_PATH);
6075 static const struct got_error *
6076 check_staged_file(void *arg, struct got_fileindex_entry *ie)
6078 int *have_staged_files = arg;
6080 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6081 *have_staged_files = 1;
6082 return got_error(GOT_ERR_CANCELLED);
6088 static const struct got_error *
6089 check_non_staged_files(struct got_fileindex *fileindex,
6090 struct got_pathlist_head *paths)
6092 struct got_pathlist_entry *pe;
6093 struct got_fileindex_entry *ie;
6095 TAILQ_FOREACH(pe, paths, entry) {
6096 if (pe->path[0] == '\0')
6098 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6100 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6101 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6102 return got_error_path(pe->path,
6103 GOT_ERR_FILE_NOT_STAGED);
6109 const struct got_error *
6110 got_worktree_commit(struct got_object_id **new_commit_id,
6111 struct got_worktree *worktree, struct got_pathlist_head *paths,
6112 const char *author, const char *committer, int allow_bad_symlinks,
6113 int show_diff, got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6114 got_worktree_status_cb status_cb, void *status_arg,
6115 struct got_repository *repo)
6117 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6118 struct got_fileindex *fileindex = NULL;
6119 char *fileindex_path = NULL;
6120 struct got_pathlist_head commitable_paths;
6121 struct collect_commitables_arg cc_arg;
6122 struct got_pathlist_entry *pe;
6123 struct got_reference *head_ref = NULL;
6124 struct got_object_id *head_commit_id = NULL;
6125 char *diff_path = NULL;
6126 int have_staged_files = 0;
6128 *new_commit_id = NULL;
6130 memset(&cc_arg, 0, sizeof(cc_arg));
6131 TAILQ_INIT(&commitable_paths);
6133 err = lock_worktree(worktree, LOCK_EX);
6137 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6141 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6145 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6149 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6150 &have_staged_files);
6151 if (err && err->code != GOT_ERR_CANCELLED)
6153 if (have_staged_files) {
6154 err = check_non_staged_files(fileindex, paths);
6159 cc_arg.commitable_paths = &commitable_paths;
6160 cc_arg.worktree = worktree;
6161 cc_arg.fileindex = fileindex;
6163 cc_arg.have_staged_files = have_staged_files;
6164 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6165 cc_arg.diff_header_shown = 0;
6167 err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6168 GOT_TMPDIR_STR "/got", ".diff");
6171 cc_arg.f1 = got_opentemp();
6172 if (cc_arg.f1 == NULL) {
6173 err = got_error_from_errno("got_opentemp");
6176 cc_arg.f2 = got_opentemp();
6177 if (cc_arg.f2 == NULL) {
6178 err = got_error_from_errno("got_opentemp");
6183 TAILQ_FOREACH(pe, paths, entry) {
6184 err = worktree_status(worktree, pe->path, fileindex, repo,
6185 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6191 if (fflush(cc_arg.diff_outfile) == EOF) {
6192 err = got_error_from_errno("fflush");
6197 if (TAILQ_EMPTY(&commitable_paths)) {
6198 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6202 TAILQ_FOREACH(pe, paths, entry) {
6203 err = check_path_is_commitable(pe->path, &commitable_paths);
6208 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6209 struct got_commitable *ct = pe->data;
6210 const char *ct_path = ct->in_repo_path;
6212 while (ct_path[0] == '/')
6214 err = check_out_of_date(ct_path, ct->status,
6215 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6216 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6222 err = commit_worktree(new_commit_id, &commitable_paths,
6223 head_commit_id, NULL, worktree, author, committer,
6224 (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6225 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6229 err = update_fileindex_after_commit(worktree, &commitable_paths,
6230 *new_commit_id, fileindex, have_staged_files);
6231 sync_err = sync_fileindex(fileindex, fileindex_path);
6232 if (sync_err && err == NULL)
6236 got_fileindex_free(fileindex);
6237 free(fileindex_path);
6238 unlockerr = lock_worktree(worktree, LOCK_SH);
6239 if (unlockerr && err == NULL)
6241 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6242 struct got_commitable *ct = pe->data;
6244 free_commitable(ct);
6246 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6247 if (diff_path && unlink(diff_path) == -1 && err == NULL)
6248 err = got_error_from_errno2("unlink", diff_path);
6250 if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6252 err = got_error_from_errno("fclose");
6257 got_commitable_get_path(struct got_commitable *ct)
6263 got_commitable_get_status(struct got_commitable *ct)
6268 struct check_rebase_ok_arg {
6269 struct got_worktree *worktree;
6270 struct got_repository *repo;
6273 static const struct got_error *
6274 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6276 const struct got_error *err = NULL;
6277 struct check_rebase_ok_arg *a = arg;
6278 unsigned char status;
6282 /* Reject rebase of a work tree with mixed base commits. */
6283 if (memcmp(ie->commit_hash, a->worktree->base_commit_id->hash,
6284 SHA1_DIGEST_LENGTH))
6285 return got_error(GOT_ERR_MIXED_COMMITS);
6287 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6289 return got_error_from_errno("asprintf");
6291 /* Reject rebase of a work tree with modified or staged files. */
6292 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6297 if (status != GOT_STATUS_NO_CHANGE)
6298 return got_error(GOT_ERR_MODIFIED);
6299 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6300 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6305 const struct got_error *
6306 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6307 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6308 struct got_worktree *worktree, struct got_reference *branch,
6309 struct got_repository *repo)
6311 const struct got_error *err = NULL;
6312 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6313 char *branch_ref_name = NULL;
6314 char *fileindex_path = NULL;
6315 struct check_rebase_ok_arg ok_arg;
6316 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6317 struct got_object_id *wt_branch_tip = NULL;
6319 *new_base_branch_ref = NULL;
6323 err = lock_worktree(worktree, LOCK_EX);
6327 err = open_fileindex(fileindex, &fileindex_path, worktree);
6331 ok_arg.worktree = worktree;
6333 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6338 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6342 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6346 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6350 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6355 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6358 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6359 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6363 err = got_ref_alloc_symref(new_base_branch_ref,
6364 new_base_branch_ref_name, wt_branch);
6367 err = got_ref_write(*new_base_branch_ref, repo);
6371 /* TODO Lock original branch's ref while rebasing? */
6373 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6377 err = got_ref_write(branch_ref, repo);
6381 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6382 worktree->base_commit_id);
6385 err = got_ref_write(*tmp_branch, repo);
6389 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6393 free(fileindex_path);
6394 free(tmp_branch_name);
6395 free(new_base_branch_ref_name);
6396 free(branch_ref_name);
6398 got_ref_close(branch_ref);
6400 got_ref_close(wt_branch);
6401 free(wt_branch_tip);
6403 if (*new_base_branch_ref) {
6404 got_ref_close(*new_base_branch_ref);
6405 *new_base_branch_ref = NULL;
6408 got_ref_close(*tmp_branch);
6412 got_fileindex_free(*fileindex);
6415 lock_worktree(worktree, LOCK_SH);
6420 const struct got_error *
6421 got_worktree_rebase_continue(struct got_object_id **commit_id,
6422 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6423 struct got_reference **branch, struct got_fileindex **fileindex,
6424 struct got_worktree *worktree, struct got_repository *repo)
6426 const struct got_error *err;
6427 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6428 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6429 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6430 char *fileindex_path = NULL;
6431 int have_staged_files = 0;
6434 *new_base_branch = NULL;
6439 err = lock_worktree(worktree, LOCK_EX);
6443 err = open_fileindex(fileindex, &fileindex_path, worktree);
6447 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6448 &have_staged_files);
6449 if (err && err->code != GOT_ERR_CANCELLED)
6451 if (have_staged_files) {
6452 err = got_error(GOT_ERR_STAGED_PATHS);
6456 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6460 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6464 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6468 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6472 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6476 err = got_ref_open(branch, repo,
6477 got_ref_get_symref_target(branch_ref), 0);
6481 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6485 err = got_ref_resolve(commit_id, repo, commit_ref);
6489 err = got_ref_open(new_base_branch, repo,
6490 new_base_branch_ref_name, 0);
6494 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6498 free(commit_ref_name);
6499 free(branch_ref_name);
6500 free(fileindex_path);
6502 got_ref_close(commit_ref);
6504 got_ref_close(branch_ref);
6509 got_ref_close(*tmp_branch);
6512 if (*new_base_branch) {
6513 got_ref_close(*new_base_branch);
6514 *new_base_branch = NULL;
6517 got_ref_close(*branch);
6521 got_fileindex_free(*fileindex);
6524 lock_worktree(worktree, LOCK_SH);
6529 const struct got_error *
6530 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6532 const struct got_error *err;
6533 char *tmp_branch_name = NULL;
6535 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6539 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6540 free(tmp_branch_name);
6544 static const struct got_error *
6545 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6546 const char *diff_path, char **logmsg, void *arg)
6552 static const struct got_error *
6553 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6554 const char *path, struct got_object_id *blob_id,
6555 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6556 int dirfd, const char *de_name)
6561 struct collect_merged_paths_arg {
6562 got_worktree_checkout_cb progress_cb;
6564 struct got_pathlist_head *merged_paths;
6567 static const struct got_error *
6568 collect_merged_paths(void *arg, unsigned char status, const char *path)
6570 const struct got_error *err;
6571 struct collect_merged_paths_arg *a = arg;
6573 struct got_pathlist_entry *new;
6575 err = (*a->progress_cb)(a->progress_arg, status, path);
6579 if (status != GOT_STATUS_MERGE &&
6580 status != GOT_STATUS_ADD &&
6581 status != GOT_STATUS_DELETE &&
6582 status != GOT_STATUS_CONFLICT)
6587 return got_error_from_errno("strdup");
6589 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6590 if (err || new == NULL)
6595 static const struct got_error *
6596 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6597 int is_rebase, struct got_repository *repo)
6599 const struct got_error *err;
6600 struct got_reference *commit_ref = NULL;
6602 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6604 if (err->code != GOT_ERR_NOT_REF)
6606 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6609 err = got_ref_write(commit_ref, repo);
6612 } else if (is_rebase) {
6613 struct got_object_id *stored_id;
6616 err = got_ref_resolve(&stored_id, repo, commit_ref);
6619 cmp = got_object_id_cmp(commit_id, stored_id);
6622 err = got_error(GOT_ERR_REBASE_COMMITID);
6628 got_ref_close(commit_ref);
6632 static const struct got_error *
6633 rebase_merge_files(struct got_pathlist_head *merged_paths,
6634 const char *commit_ref_name, struct got_worktree *worktree,
6635 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6636 struct got_object_id *commit_id, struct got_repository *repo,
6637 got_worktree_checkout_cb progress_cb, void *progress_arg,
6638 got_cancel_cb cancel_cb, void *cancel_arg)
6640 const struct got_error *err;
6641 struct got_reference *commit_ref = NULL;
6642 struct collect_merged_paths_arg cmp_arg;
6643 char *fileindex_path;
6645 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6647 err = get_fileindex_path(&fileindex_path, worktree);
6651 cmp_arg.progress_cb = progress_cb;
6652 cmp_arg.progress_arg = progress_arg;
6653 cmp_arg.merged_paths = merged_paths;
6654 err = merge_files(worktree, fileindex, fileindex_path,
6655 parent_commit_id, commit_id, repo, collect_merged_paths,
6656 &cmp_arg, cancel_cb, cancel_arg);
6658 got_ref_close(commit_ref);
6662 const struct got_error *
6663 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6664 struct got_worktree *worktree, struct got_fileindex *fileindex,
6665 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6666 struct got_repository *repo,
6667 got_worktree_checkout_cb progress_cb, void *progress_arg,
6668 got_cancel_cb cancel_cb, void *cancel_arg)
6670 const struct got_error *err;
6671 char *commit_ref_name;
6673 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6677 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6681 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6682 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6683 progress_arg, cancel_cb, cancel_arg);
6685 free(commit_ref_name);
6689 const struct got_error *
6690 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6691 struct got_worktree *worktree, struct got_fileindex *fileindex,
6692 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6693 struct got_repository *repo,
6694 got_worktree_checkout_cb progress_cb, void *progress_arg,
6695 got_cancel_cb cancel_cb, void *cancel_arg)
6697 const struct got_error *err;
6698 char *commit_ref_name;
6700 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6704 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6708 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6709 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6710 progress_arg, cancel_cb, cancel_arg);
6712 free(commit_ref_name);
6716 static const struct got_error *
6717 rebase_commit(struct got_object_id **new_commit_id,
6718 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6719 struct got_worktree *worktree, struct got_fileindex *fileindex,
6720 struct got_reference *tmp_branch, const char *committer,
6721 struct got_commit_object *orig_commit, const char *new_logmsg,
6722 struct got_repository *repo)
6724 const struct got_error *err, *sync_err;
6725 struct got_pathlist_head commitable_paths;
6726 struct collect_commitables_arg cc_arg;
6727 char *fileindex_path = NULL;
6728 struct got_reference *head_ref = NULL;
6729 struct got_object_id *head_commit_id = NULL;
6730 char *logmsg = NULL;
6732 memset(&cc_arg, 0, sizeof(cc_arg));
6733 TAILQ_INIT(&commitable_paths);
6734 *new_commit_id = NULL;
6736 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6738 err = get_fileindex_path(&fileindex_path, worktree);
6742 cc_arg.commitable_paths = &commitable_paths;
6743 cc_arg.worktree = worktree;
6745 cc_arg.have_staged_files = 0;
6747 * If possible get the status of individual files directly to
6748 * avoid crawling the entire work tree once per rebased commit.
6750 * Ideally, merged_paths would contain a list of commitables
6751 * we could use so we could skip worktree_status() entirely.
6752 * However, we would then need carefully keep track of cumulative
6753 * effects of operations such as file additions and deletions
6754 * in 'got histedit -f' (folding multiple commits into one),
6755 * and this extra complexity is not really worth it.
6758 struct got_pathlist_entry *pe;
6759 TAILQ_FOREACH(pe, merged_paths, entry) {
6760 err = worktree_status(worktree, pe->path, fileindex,
6761 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6767 err = worktree_status(worktree, "", fileindex, repo,
6768 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6773 if (TAILQ_EMPTY(&commitable_paths)) {
6774 /* No-op change; commit will be elided. */
6775 err = got_ref_delete(commit_ref, repo);
6778 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6782 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6786 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6791 logmsg = strdup(new_logmsg);
6792 if (logmsg == NULL) {
6793 err = got_error_from_errno("strdup");
6797 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6802 /* NB: commit_worktree will call free(logmsg) */
6803 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6804 NULL, worktree, got_object_commit_get_author(orig_commit),
6805 committer ? committer :
6806 got_object_commit_get_committer(orig_commit), NULL,
6807 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6811 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6815 err = got_ref_delete(commit_ref, repo);
6819 err = update_fileindex_after_commit(worktree, &commitable_paths,
6820 *new_commit_id, fileindex, 0);
6821 sync_err = sync_fileindex(fileindex, fileindex_path);
6822 if (sync_err && err == NULL)
6825 free(fileindex_path);
6826 free(head_commit_id);
6828 got_ref_close(head_ref);
6830 free(*new_commit_id);
6831 *new_commit_id = NULL;
6836 const struct got_error *
6837 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6838 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6839 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6840 const char *committer, struct got_commit_object *orig_commit,
6841 struct got_object_id *orig_commit_id, struct got_repository *repo)
6843 const struct got_error *err;
6844 char *commit_ref_name;
6845 struct got_reference *commit_ref = NULL;
6846 struct got_object_id *commit_id = NULL;
6848 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6852 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6855 err = got_ref_resolve(&commit_id, repo, commit_ref);
6858 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6859 err = got_error(GOT_ERR_REBASE_COMMITID);
6863 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6864 worktree, fileindex, tmp_branch, committer, orig_commit,
6868 got_ref_close(commit_ref);
6869 free(commit_ref_name);
6874 const struct got_error *
6875 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6876 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6877 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6878 const char *committer, struct got_commit_object *orig_commit,
6879 struct got_object_id *orig_commit_id, const char *new_logmsg,
6880 struct got_repository *repo)
6882 const struct got_error *err;
6883 char *commit_ref_name;
6884 struct got_reference *commit_ref = NULL;
6886 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6890 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6894 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6895 worktree, fileindex, tmp_branch, committer, orig_commit,
6899 got_ref_close(commit_ref);
6900 free(commit_ref_name);
6904 const struct got_error *
6905 got_worktree_rebase_postpone(struct got_worktree *worktree,
6906 struct got_fileindex *fileindex)
6909 got_fileindex_free(fileindex);
6910 return lock_worktree(worktree, LOCK_SH);
6913 static const struct got_error *
6914 delete_ref(const char *name, struct got_repository *repo)
6916 const struct got_error *err;
6917 struct got_reference *ref;
6919 err = got_ref_open(&ref, repo, name, 0);
6921 if (err->code == GOT_ERR_NOT_REF)
6926 err = got_ref_delete(ref, repo);
6931 static const struct got_error *
6932 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6934 const struct got_error *err;
6935 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6936 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6938 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6941 err = delete_ref(tmp_branch_name, repo);
6945 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6948 err = delete_ref(new_base_branch_ref_name, repo);
6952 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6955 err = delete_ref(branch_ref_name, repo);
6959 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6962 err = delete_ref(commit_ref_name, repo);
6967 free(tmp_branch_name);
6968 free(new_base_branch_ref_name);
6969 free(branch_ref_name);
6970 free(commit_ref_name);
6974 static const struct got_error *
6975 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6976 struct got_object_id *new_commit_id, struct got_repository *repo)
6978 const struct got_error *err;
6979 struct got_reference *ref = NULL;
6980 struct got_object_id *old_commit_id = NULL;
6981 const char *branch_name = NULL;
6982 char *new_id_str = NULL;
6983 char *refname = NULL;
6985 branch_name = got_ref_get_name(branch);
6986 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6987 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6990 err = got_object_id_str(&new_id_str, new_commit_id);
6994 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6995 new_id_str) == -1) {
6996 err = got_error_from_errno("asprintf");
7000 err = got_ref_resolve(&old_commit_id, repo, branch);
7004 err = got_ref_alloc(&ref, refname, old_commit_id);
7008 err = got_ref_write(ref, repo);
7012 free(old_commit_id);
7018 const struct got_error *
7019 got_worktree_rebase_complete(struct got_worktree *worktree,
7020 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7021 struct got_reference *rebased_branch, struct got_repository *repo,
7024 const struct got_error *err, *unlockerr, *sync_err;
7025 struct got_object_id *new_head_commit_id = NULL;
7026 char *fileindex_path = NULL;
7028 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7032 if (create_backup) {
7033 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7034 rebased_branch, new_head_commit_id, repo);
7039 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7043 err = got_ref_write(rebased_branch, repo);
7047 err = got_worktree_set_head_ref(worktree, rebased_branch);
7051 err = delete_rebase_refs(worktree, repo);
7055 err = get_fileindex_path(&fileindex_path, worktree);
7058 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7059 sync_err = sync_fileindex(fileindex, fileindex_path);
7060 if (sync_err && err == NULL)
7063 got_fileindex_free(fileindex);
7064 free(fileindex_path);
7065 free(new_head_commit_id);
7066 unlockerr = lock_worktree(worktree, LOCK_SH);
7067 if (unlockerr && err == NULL)
7072 const struct got_error *
7073 got_worktree_rebase_abort(struct got_worktree *worktree,
7074 struct got_fileindex *fileindex, struct got_repository *repo,
7075 struct got_reference *new_base_branch,
7076 got_worktree_checkout_cb progress_cb, void *progress_arg)
7078 const struct got_error *err, *unlockerr, *sync_err;
7079 struct got_reference *resolved = NULL;
7080 struct got_object_id *commit_id = NULL;
7081 struct got_commit_object *commit = NULL;
7082 char *fileindex_path = NULL;
7083 struct revert_file_args rfa;
7084 struct got_object_id *tree_id = NULL;
7086 err = lock_worktree(worktree, LOCK_EX);
7090 err = got_object_open_as_commit(&commit, repo,
7091 worktree->base_commit_id);
7095 err = got_ref_open(&resolved, repo,
7096 got_ref_get_symref_target(new_base_branch), 0);
7100 err = got_worktree_set_head_ref(worktree, resolved);
7105 * XXX commits to the base branch could have happened while
7106 * we were busy rebasing; should we store the original commit ID
7107 * when rebase begins and read it back here?
7109 err = got_ref_resolve(&commit_id, repo, resolved);
7113 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7117 err = got_object_id_by_path(&tree_id, repo, commit,
7118 worktree->path_prefix);
7122 err = delete_rebase_refs(worktree, repo);
7126 err = get_fileindex_path(&fileindex_path, worktree);
7130 rfa.worktree = worktree;
7131 rfa.fileindex = fileindex;
7132 rfa.progress_cb = progress_cb;
7133 rfa.progress_arg = progress_arg;
7134 rfa.patch_cb = NULL;
7135 rfa.patch_arg = NULL;
7137 rfa.unlink_added_files = 0;
7138 err = worktree_status(worktree, "", fileindex, repo,
7139 revert_file, &rfa, NULL, NULL, 1, 0);
7143 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7144 repo, progress_cb, progress_arg, NULL, NULL);
7146 sync_err = sync_fileindex(fileindex, fileindex_path);
7147 if (sync_err && err == NULL)
7150 got_ref_close(resolved);
7154 got_object_commit_close(commit);
7156 got_fileindex_free(fileindex);
7157 free(fileindex_path);
7159 unlockerr = lock_worktree(worktree, LOCK_SH);
7160 if (unlockerr && err == NULL)
7165 const struct got_error *
7166 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7167 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7168 struct got_fileindex **fileindex, struct got_worktree *worktree,
7169 struct got_repository *repo)
7171 const struct got_error *err = NULL;
7172 char *tmp_branch_name = NULL;
7173 char *branch_ref_name = NULL;
7174 char *base_commit_ref_name = NULL;
7175 char *fileindex_path = NULL;
7176 struct check_rebase_ok_arg ok_arg;
7177 struct got_reference *wt_branch = NULL;
7178 struct got_reference *base_commit_ref = NULL;
7182 *base_commit_id = NULL;
7185 err = lock_worktree(worktree, LOCK_EX);
7189 err = open_fileindex(fileindex, &fileindex_path, worktree);
7193 ok_arg.worktree = worktree;
7195 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7200 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7204 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7208 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7213 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7218 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7222 err = got_ref_write(*branch_ref, repo);
7226 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7227 worktree->base_commit_id);
7230 err = got_ref_write(base_commit_ref, repo);
7233 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7234 if (*base_commit_id == NULL) {
7235 err = got_error_from_errno("got_object_id_dup");
7239 err = got_ref_alloc(tmp_branch, tmp_branch_name,
7240 worktree->base_commit_id);
7243 err = got_ref_write(*tmp_branch, repo);
7247 err = got_worktree_set_head_ref(worktree, *tmp_branch);
7251 free(fileindex_path);
7252 free(tmp_branch_name);
7253 free(branch_ref_name);
7254 free(base_commit_ref_name);
7256 got_ref_close(wt_branch);
7259 got_ref_close(*branch_ref);
7263 got_ref_close(*tmp_branch);
7266 free(*base_commit_id);
7268 got_fileindex_free(*fileindex);
7271 lock_worktree(worktree, LOCK_SH);
7276 const struct got_error *
7277 got_worktree_histedit_postpone(struct got_worktree *worktree,
7278 struct got_fileindex *fileindex)
7281 got_fileindex_free(fileindex);
7282 return lock_worktree(worktree, LOCK_SH);
7285 const struct got_error *
7286 got_worktree_histedit_in_progress(int *in_progress,
7287 struct got_worktree *worktree)
7289 const struct got_error *err;
7290 char *tmp_branch_name = NULL;
7292 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7296 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7297 free(tmp_branch_name);
7301 const struct got_error *
7302 got_worktree_histedit_continue(struct got_object_id **commit_id,
7303 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7304 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7305 struct got_worktree *worktree, struct got_repository *repo)
7307 const struct got_error *err;
7308 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7309 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7310 struct got_reference *commit_ref = NULL;
7311 struct got_reference *base_commit_ref = NULL;
7312 char *fileindex_path = NULL;
7313 int have_staged_files = 0;
7317 *base_commit_id = NULL;
7320 err = lock_worktree(worktree, LOCK_EX);
7324 err = open_fileindex(fileindex, &fileindex_path, worktree);
7328 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7329 &have_staged_files);
7330 if (err && err->code != GOT_ERR_CANCELLED)
7332 if (have_staged_files) {
7333 err = got_error(GOT_ERR_STAGED_PATHS);
7337 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7341 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7345 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7349 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7354 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7358 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7361 err = got_ref_resolve(commit_id, repo, commit_ref);
7365 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7368 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7372 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7376 free(commit_ref_name);
7377 free(branch_ref_name);
7378 free(fileindex_path);
7380 got_ref_close(commit_ref);
7381 if (base_commit_ref)
7382 got_ref_close(base_commit_ref);
7386 free(*base_commit_id);
7387 *base_commit_id = NULL;
7389 got_ref_close(*tmp_branch);
7393 got_fileindex_free(*fileindex);
7396 lock_worktree(worktree, LOCK_EX);
7401 static const struct got_error *
7402 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7404 const struct got_error *err;
7405 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7406 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7408 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7411 err = delete_ref(tmp_branch_name, repo);
7415 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7419 err = delete_ref(base_commit_ref_name, repo);
7423 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7426 err = delete_ref(branch_ref_name, repo);
7430 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7433 err = delete_ref(commit_ref_name, repo);
7437 free(tmp_branch_name);
7438 free(base_commit_ref_name);
7439 free(branch_ref_name);
7440 free(commit_ref_name);
7444 const struct got_error *
7445 got_worktree_histedit_abort(struct got_worktree *worktree,
7446 struct got_fileindex *fileindex, struct got_repository *repo,
7447 struct got_reference *branch, struct got_object_id *base_commit_id,
7448 got_worktree_checkout_cb progress_cb, void *progress_arg)
7450 const struct got_error *err, *unlockerr, *sync_err;
7451 struct got_reference *resolved = NULL;
7452 char *fileindex_path = NULL;
7453 struct got_commit_object *commit = NULL;
7454 struct got_object_id *tree_id = NULL;
7455 struct revert_file_args rfa;
7457 err = lock_worktree(worktree, LOCK_EX);
7461 err = got_object_open_as_commit(&commit, repo,
7462 worktree->base_commit_id);
7466 err = got_ref_open(&resolved, repo,
7467 got_ref_get_symref_target(branch), 0);
7471 err = got_worktree_set_head_ref(worktree, resolved);
7475 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7479 err = got_object_id_by_path(&tree_id, repo, commit,
7480 worktree->path_prefix);
7484 err = delete_histedit_refs(worktree, repo);
7488 err = get_fileindex_path(&fileindex_path, worktree);
7492 rfa.worktree = worktree;
7493 rfa.fileindex = fileindex;
7494 rfa.progress_cb = progress_cb;
7495 rfa.progress_arg = progress_arg;
7496 rfa.patch_cb = NULL;
7497 rfa.patch_arg = NULL;
7499 rfa.unlink_added_files = 0;
7500 err = worktree_status(worktree, "", fileindex, repo,
7501 revert_file, &rfa, NULL, NULL, 1, 0);
7505 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7506 repo, progress_cb, progress_arg, NULL, NULL);
7508 sync_err = sync_fileindex(fileindex, fileindex_path);
7509 if (sync_err && err == NULL)
7512 got_ref_close(resolved);
7514 free(fileindex_path);
7516 unlockerr = lock_worktree(worktree, LOCK_SH);
7517 if (unlockerr && err == NULL)
7522 const struct got_error *
7523 got_worktree_histedit_complete(struct got_worktree *worktree,
7524 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7525 struct got_reference *edited_branch, struct got_repository *repo)
7527 const struct got_error *err, *unlockerr, *sync_err;
7528 struct got_object_id *new_head_commit_id = NULL;
7529 struct got_reference *resolved = NULL;
7530 char *fileindex_path = NULL;
7532 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7536 err = got_ref_open(&resolved, repo,
7537 got_ref_get_symref_target(edited_branch), 0);
7541 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7542 resolved, new_head_commit_id, repo);
7546 err = got_ref_change_ref(resolved, new_head_commit_id);
7550 err = got_ref_write(resolved, repo);
7554 err = got_worktree_set_head_ref(worktree, resolved);
7558 err = delete_histedit_refs(worktree, repo);
7562 err = get_fileindex_path(&fileindex_path, worktree);
7565 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7566 sync_err = sync_fileindex(fileindex, fileindex_path);
7567 if (sync_err && err == NULL)
7570 got_fileindex_free(fileindex);
7571 free(fileindex_path);
7572 free(new_head_commit_id);
7573 unlockerr = lock_worktree(worktree, LOCK_SH);
7574 if (unlockerr && err == NULL)
7579 const struct got_error *
7580 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7581 struct got_object_id *commit_id, struct got_repository *repo)
7583 const struct got_error *err;
7584 char *commit_ref_name;
7586 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7590 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7594 err = delete_ref(commit_ref_name, repo);
7596 free(commit_ref_name);
7600 const struct got_error *
7601 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7602 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7603 struct got_worktree *worktree, const char *refname,
7604 struct got_repository *repo)
7606 const struct got_error *err = NULL;
7607 char *fileindex_path = NULL;
7608 struct check_rebase_ok_arg ok_arg;
7612 *base_branch_ref = NULL;
7614 err = lock_worktree(worktree, LOCK_EX);
7618 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7619 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7620 "cannot integrate a branch into itself; "
7621 "update -b or different branch name required");
7625 err = open_fileindex(fileindex, &fileindex_path, worktree);
7629 /* Preconditions are the same as for rebase. */
7630 ok_arg.worktree = worktree;
7632 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7637 err = got_ref_open(branch_ref, repo, refname, 1);
7641 err = got_ref_open(base_branch_ref, repo,
7642 got_worktree_get_head_ref_name(worktree), 1);
7646 got_ref_close(*branch_ref);
7649 if (*base_branch_ref) {
7650 got_ref_close(*base_branch_ref);
7651 *base_branch_ref = NULL;
7654 got_fileindex_free(*fileindex);
7657 lock_worktree(worktree, LOCK_SH);
7662 const struct got_error *
7663 got_worktree_integrate_continue(struct got_worktree *worktree,
7664 struct got_fileindex *fileindex, struct got_repository *repo,
7665 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7666 got_worktree_checkout_cb progress_cb, void *progress_arg,
7667 got_cancel_cb cancel_cb, void *cancel_arg)
7669 const struct got_error *err = NULL, *sync_err, *unlockerr;
7670 char *fileindex_path = NULL;
7671 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7672 struct got_commit_object *commit = NULL;
7674 err = get_fileindex_path(&fileindex_path, worktree);
7678 err = got_ref_resolve(&commit_id, repo, branch_ref);
7682 err = got_object_open_as_commit(&commit, repo, commit_id);
7686 err = got_object_id_by_path(&tree_id, repo, commit,
7687 worktree->path_prefix);
7691 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7695 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7696 progress_cb, progress_arg, cancel_cb, cancel_arg);
7700 err = got_ref_change_ref(base_branch_ref, commit_id);
7704 err = got_ref_write(base_branch_ref, repo);
7708 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7710 sync_err = sync_fileindex(fileindex, fileindex_path);
7711 if (sync_err && err == NULL)
7715 unlockerr = got_ref_unlock(branch_ref);
7716 if (unlockerr && err == NULL)
7718 got_ref_close(branch_ref);
7720 unlockerr = got_ref_unlock(base_branch_ref);
7721 if (unlockerr && err == NULL)
7723 got_ref_close(base_branch_ref);
7725 got_fileindex_free(fileindex);
7726 free(fileindex_path);
7729 got_object_commit_close(commit);
7731 unlockerr = lock_worktree(worktree, LOCK_SH);
7732 if (unlockerr && err == NULL)
7737 const struct got_error *
7738 got_worktree_integrate_abort(struct got_worktree *worktree,
7739 struct got_fileindex *fileindex, struct got_repository *repo,
7740 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7742 const struct got_error *err = NULL, *unlockerr = NULL;
7744 got_fileindex_free(fileindex);
7746 err = lock_worktree(worktree, LOCK_SH);
7748 unlockerr = got_ref_unlock(branch_ref);
7749 if (unlockerr && err == NULL)
7751 got_ref_close(branch_ref);
7753 unlockerr = got_ref_unlock(base_branch_ref);
7754 if (unlockerr && err == NULL)
7756 got_ref_close(base_branch_ref);
7761 const struct got_error *
7762 got_worktree_merge_postpone(struct got_worktree *worktree,
7763 struct got_fileindex *fileindex)
7765 const struct got_error *err, *sync_err;
7766 char *fileindex_path = NULL;
7768 err = get_fileindex_path(&fileindex_path, worktree);
7772 sync_err = sync_fileindex(fileindex, fileindex_path);
7774 err = lock_worktree(worktree, LOCK_SH);
7775 if (sync_err && err == NULL)
7778 got_fileindex_free(fileindex);
7779 free(fileindex_path);
7783 static const struct got_error *
7784 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7786 const struct got_error *err;
7787 char *branch_refname = NULL, *commit_refname = NULL;
7789 err = get_merge_branch_ref_name(&branch_refname, worktree);
7792 err = delete_ref(branch_refname, repo);
7796 err = get_merge_commit_ref_name(&commit_refname, worktree);
7799 err = delete_ref(commit_refname, repo);
7804 free(branch_refname);
7805 free(commit_refname);
7809 struct merge_commit_msg_arg {
7810 struct got_worktree *worktree;
7811 const char *branch_name;
7814 static const struct got_error *
7815 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
7816 const char *diff_path, char **logmsg, void *arg)
7818 struct merge_commit_msg_arg *a = arg;
7820 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7821 got_worktree_get_head_ref_name(a->worktree)) == -1)
7822 return got_error_from_errno("asprintf");
7828 const struct got_error *
7829 got_worktree_merge_branch(struct got_worktree *worktree,
7830 struct got_fileindex *fileindex,
7831 struct got_object_id *yca_commit_id,
7832 struct got_object_id *branch_tip,
7833 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7834 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7836 const struct got_error *err;
7837 char *fileindex_path = NULL;
7839 err = get_fileindex_path(&fileindex_path, worktree);
7843 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7848 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7849 branch_tip, repo, progress_cb, progress_arg,
7850 cancel_cb, cancel_arg);
7852 free(fileindex_path);
7856 const struct got_error *
7857 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7858 struct got_worktree *worktree, struct got_fileindex *fileindex,
7859 const char *author, const char *committer, int allow_bad_symlinks,
7860 struct got_object_id *branch_tip, const char *branch_name,
7861 struct got_repository *repo,
7862 got_worktree_status_cb status_cb, void *status_arg)
7865 const struct got_error *err = NULL, *sync_err;
7866 struct got_pathlist_head commitable_paths;
7867 struct collect_commitables_arg cc_arg;
7868 struct got_pathlist_entry *pe;
7869 struct got_reference *head_ref = NULL;
7870 struct got_object_id *head_commit_id = NULL;
7871 int have_staged_files = 0;
7872 struct merge_commit_msg_arg mcm_arg;
7873 char *fileindex_path = NULL;
7875 memset(&cc_arg, 0, sizeof(cc_arg));
7876 *new_commit_id = NULL;
7878 TAILQ_INIT(&commitable_paths);
7880 err = get_fileindex_path(&fileindex_path, worktree);
7884 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7888 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7892 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7893 &have_staged_files);
7894 if (err && err->code != GOT_ERR_CANCELLED)
7896 if (have_staged_files) {
7897 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7901 cc_arg.commitable_paths = &commitable_paths;
7902 cc_arg.worktree = worktree;
7903 cc_arg.fileindex = fileindex;
7905 cc_arg.have_staged_files = have_staged_files;
7906 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7907 err = worktree_status(worktree, "", fileindex, repo,
7908 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7912 if (TAILQ_EMPTY(&commitable_paths)) {
7913 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7914 "merge of %s cannot proceed", branch_name);
7918 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7919 struct got_commitable *ct = pe->data;
7920 const char *ct_path = ct->in_repo_path;
7922 while (ct_path[0] == '/')
7924 err = check_out_of_date(ct_path, ct->status,
7925 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7926 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7932 mcm_arg.worktree = worktree;
7933 mcm_arg.branch_name = branch_name;
7934 err = commit_worktree(new_commit_id, &commitable_paths,
7935 head_commit_id, branch_tip, worktree, author, committer, NULL,
7936 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7940 err = update_fileindex_after_commit(worktree, &commitable_paths,
7941 *new_commit_id, fileindex, have_staged_files);
7942 sync_err = sync_fileindex(fileindex, fileindex_path);
7943 if (sync_err && err == NULL)
7946 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7947 struct got_commitable *ct = pe->data;
7949 free_commitable(ct);
7951 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
7952 free(fileindex_path);
7956 const struct got_error *
7957 got_worktree_merge_complete(struct got_worktree *worktree,
7958 struct got_fileindex *fileindex, struct got_repository *repo)
7960 const struct got_error *err, *unlockerr, *sync_err;
7961 char *fileindex_path = NULL;
7963 err = delete_merge_refs(worktree, repo);
7967 err = get_fileindex_path(&fileindex_path, worktree);
7970 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7971 sync_err = sync_fileindex(fileindex, fileindex_path);
7972 if (sync_err && err == NULL)
7975 got_fileindex_free(fileindex);
7976 free(fileindex_path);
7977 unlockerr = lock_worktree(worktree, LOCK_SH);
7978 if (unlockerr && err == NULL)
7983 const struct got_error *
7984 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7985 struct got_repository *repo)
7987 const struct got_error *err;
7988 char *branch_refname = NULL;
7989 struct got_reference *branch_ref = NULL;
7993 err = get_merge_branch_ref_name(&branch_refname, worktree);
7996 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7997 free(branch_refname);
7999 if (err->code != GOT_ERR_NOT_REF)
8007 const struct got_error *got_worktree_merge_prepare(
8008 struct got_fileindex **fileindex, struct got_worktree *worktree,
8009 struct got_reference *branch, struct got_repository *repo)
8011 const struct got_error *err = NULL;
8012 char *fileindex_path = NULL;
8013 char *branch_refname = NULL, *commit_refname = NULL;
8014 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
8015 struct got_reference *commit_ref = NULL;
8016 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
8017 struct check_rebase_ok_arg ok_arg;
8021 err = lock_worktree(worktree, LOCK_EX);
8025 err = open_fileindex(fileindex, &fileindex_path, worktree);
8029 /* Preconditions are the same as for rebase. */
8030 ok_arg.worktree = worktree;
8032 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8037 err = get_merge_branch_ref_name(&branch_refname, worktree);
8041 err = get_merge_commit_ref_name(&commit_refname, worktree);
8045 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8050 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8054 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8055 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8059 err = got_ref_resolve(&branch_tip, repo, branch);
8063 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8066 err = got_ref_write(branch_ref, repo);
8070 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8073 err = got_ref_write(commit_ref, repo);
8078 free(branch_refname);
8079 free(commit_refname);
8080 free(fileindex_path);
8082 got_ref_close(branch_ref);
8084 got_ref_close(commit_ref);
8086 got_ref_close(wt_branch);
8087 free(wt_branch_tip);
8090 got_fileindex_free(*fileindex);
8093 lock_worktree(worktree, LOCK_SH);
8098 const struct got_error *
8099 got_worktree_merge_continue(char **branch_name,
8100 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8101 struct got_worktree *worktree, struct got_repository *repo)
8103 const struct got_error *err;
8104 char *commit_refname = NULL, *branch_refname = NULL;
8105 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8106 char *fileindex_path = NULL;
8107 int have_staged_files = 0;
8109 *branch_name = NULL;
8113 err = lock_worktree(worktree, LOCK_EX);
8117 err = open_fileindex(fileindex, &fileindex_path, worktree);
8121 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8122 &have_staged_files);
8123 if (err && err->code != GOT_ERR_CANCELLED)
8125 if (have_staged_files) {
8126 err = got_error(GOT_ERR_STAGED_PATHS);
8130 err = get_merge_branch_ref_name(&branch_refname, worktree);
8134 err = get_merge_commit_ref_name(&commit_refname, worktree);
8138 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8142 if (!got_ref_is_symbolic(branch_ref)) {
8143 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8144 "%s is not a symbolic reference",
8145 got_ref_get_name(branch_ref));
8148 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8149 if (*branch_name == NULL) {
8150 err = got_error_from_errno("strdup");
8154 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8158 err = got_ref_resolve(branch_tip, repo, commit_ref);
8162 free(commit_refname);
8163 free(branch_refname);
8164 free(fileindex_path);
8166 got_ref_close(commit_ref);
8168 got_ref_close(branch_ref);
8172 *branch_name = NULL;
8177 got_fileindex_free(*fileindex);
8180 lock_worktree(worktree, LOCK_SH);
8185 const struct got_error *
8186 got_worktree_merge_abort(struct got_worktree *worktree,
8187 struct got_fileindex *fileindex, struct got_repository *repo,
8188 got_worktree_checkout_cb progress_cb, void *progress_arg)
8190 const struct got_error *err, *unlockerr, *sync_err;
8191 struct got_object_id *commit_id = NULL;
8192 struct got_commit_object *commit = NULL;
8193 char *fileindex_path = NULL;
8194 struct revert_file_args rfa;
8195 struct got_object_id *tree_id = NULL;
8197 err = got_object_open_as_commit(&commit, repo,
8198 worktree->base_commit_id);
8202 err = got_object_id_by_path(&tree_id, repo, commit,
8203 worktree->path_prefix);
8207 err = delete_merge_refs(worktree, repo);
8211 err = get_fileindex_path(&fileindex_path, worktree);
8215 rfa.worktree = worktree;
8216 rfa.fileindex = fileindex;
8217 rfa.progress_cb = progress_cb;
8218 rfa.progress_arg = progress_arg;
8219 rfa.patch_cb = NULL;
8220 rfa.patch_arg = NULL;
8222 rfa.unlink_added_files = 1;
8223 err = worktree_status(worktree, "", fileindex, repo,
8224 revert_file, &rfa, NULL, NULL, 1, 0);
8228 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8229 repo, progress_cb, progress_arg, NULL, NULL);
8231 sync_err = sync_fileindex(fileindex, fileindex_path);
8232 if (sync_err && err == NULL)
8238 got_object_commit_close(commit);
8240 got_fileindex_free(fileindex);
8241 free(fileindex_path);
8243 unlockerr = lock_worktree(worktree, LOCK_SH);
8244 if (unlockerr && err == NULL)
8249 struct check_stage_ok_arg {
8250 struct got_object_id *head_commit_id;
8251 struct got_worktree *worktree;
8252 struct got_fileindex *fileindex;
8253 struct got_repository *repo;
8257 static const struct got_error *
8258 check_stage_ok(void *arg, unsigned char status,
8259 unsigned char staged_status, const char *relpath,
8260 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8261 struct got_object_id *commit_id, int dirfd, const char *de_name)
8263 struct check_stage_ok_arg *a = arg;
8264 const struct got_error *err = NULL;
8265 struct got_fileindex_entry *ie;
8266 struct got_object_id base_commit_id;
8267 struct got_object_id *base_commit_idp = NULL;
8268 char *in_repo_path = NULL, *p;
8270 if (status == GOT_STATUS_UNVERSIONED ||
8271 status == GOT_STATUS_NO_CHANGE)
8273 if (status == GOT_STATUS_NONEXISTENT)
8274 return got_error_set_errno(ENOENT, relpath);
8276 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8278 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8280 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8281 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8283 return got_error_from_errno("asprintf");
8285 if (got_fileindex_entry_has_commit(ie)) {
8286 memcpy(base_commit_id.hash, ie->commit_hash,
8287 SHA1_DIGEST_LENGTH);
8288 base_commit_idp = &base_commit_id;
8291 if (status == GOT_STATUS_CONFLICT) {
8292 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8294 } else if (status != GOT_STATUS_ADD &&
8295 status != GOT_STATUS_MODIFY &&
8296 status != GOT_STATUS_DELETE) {
8297 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8301 a->have_changes = 1;
8306 err = check_out_of_date(p, status, staged_status,
8307 blob_id, base_commit_idp, a->head_commit_id, a->repo,
8308 GOT_ERR_STAGE_OUT_OF_DATE);
8314 struct stage_path_arg {
8315 struct got_worktree *worktree;
8316 struct got_fileindex *fileindex;
8317 struct got_repository *repo;
8318 got_worktree_status_cb status_cb;
8320 got_worktree_patch_cb patch_cb;
8322 int staged_something;
8323 int allow_bad_symlinks;
8326 static const struct got_error *
8327 stage_path(void *arg, unsigned char status,
8328 unsigned char staged_status, const char *relpath,
8329 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8330 struct got_object_id *commit_id, int dirfd, const char *de_name)
8332 struct stage_path_arg *a = arg;
8333 const struct got_error *err = NULL;
8334 struct got_fileindex_entry *ie;
8335 char *ondisk_path = NULL, *path_content = NULL;
8337 struct got_object_id *new_staged_blob_id = NULL;
8340 if (status == GOT_STATUS_UNVERSIONED)
8343 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8345 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8347 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8349 return got_error_from_errno("asprintf");
8352 case GOT_STATUS_ADD:
8353 case GOT_STATUS_MODIFY:
8354 /* XXX could sb.st_mode be passed in by our caller? */
8355 if (lstat(ondisk_path, &sb) == -1) {
8356 err = got_error_from_errno2("lstat", ondisk_path);
8360 if (status == GOT_STATUS_ADD) {
8361 int choice = GOT_PATCH_CHOICE_NONE;
8362 err = (*a->patch_cb)(&choice, a->patch_arg,
8363 status, ie->path, NULL, 1, 1);
8366 if (choice != GOT_PATCH_CHOICE_YES)
8369 err = create_patched_content(&path_content, 0,
8370 staged_blob_id ? staged_blob_id : blob_id,
8371 ondisk_path, dirfd, de_name, ie->path,
8372 a->repo, a->patch_cb, a->patch_arg);
8373 if (err || path_content == NULL)
8377 err = got_object_blob_create(&new_staged_blob_id,
8378 path_content ? path_content : ondisk_path, a->repo);
8381 memcpy(ie->staged_blob_hash, new_staged_blob_id->hash,
8382 SHA1_DIGEST_LENGTH);
8383 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8384 stage = GOT_FILEIDX_STAGE_ADD;
8386 stage = GOT_FILEIDX_STAGE_MODIFY;
8387 got_fileindex_entry_stage_set(ie, stage);
8388 if (S_ISLNK(sb.st_mode)) {
8389 int is_bad_symlink = 0;
8390 if (!a->allow_bad_symlinks) {
8391 char target_path[PATH_MAX];
8393 target_len = readlink(ondisk_path, target_path,
8394 sizeof(target_path));
8395 if (target_len == -1) {
8396 err = got_error_from_errno2("readlink",
8400 err = is_bad_symlink_target(&is_bad_symlink,
8401 target_path, target_len, ondisk_path,
8402 a->worktree->root_path);
8405 if (is_bad_symlink) {
8406 err = got_error_path(ondisk_path,
8407 GOT_ERR_BAD_SYMLINK);
8412 got_fileindex_entry_staged_filetype_set(ie,
8413 GOT_FILEIDX_MODE_BAD_SYMLINK);
8415 got_fileindex_entry_staged_filetype_set(ie,
8416 GOT_FILEIDX_MODE_SYMLINK);
8418 got_fileindex_entry_staged_filetype_set(ie,
8419 GOT_FILEIDX_MODE_REGULAR_FILE);
8421 a->staged_something = 1;
8422 if (a->status_cb == NULL)
8424 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8425 get_staged_status(ie), relpath, blob_id,
8426 new_staged_blob_id, NULL, dirfd, de_name);
8430 * When staging the reverse of the staged diff,
8431 * implicitly unstage the file.
8433 if (memcmp(ie->staged_blob_hash, ie->blob_hash,
8434 sizeof(ie->blob_hash)) == 0) {
8435 got_fileindex_entry_stage_set(ie,
8436 GOT_FILEIDX_STAGE_NONE);
8439 case GOT_STATUS_DELETE:
8440 if (staged_status == GOT_STATUS_DELETE)
8443 int choice = GOT_PATCH_CHOICE_NONE;
8444 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8445 ie->path, NULL, 1, 1);
8448 if (choice == GOT_PATCH_CHOICE_NO)
8450 if (choice != GOT_PATCH_CHOICE_YES) {
8451 err = got_error(GOT_ERR_PATCH_CHOICE);
8455 stage = GOT_FILEIDX_STAGE_DELETE;
8456 got_fileindex_entry_stage_set(ie, stage);
8457 a->staged_something = 1;
8458 if (a->status_cb == NULL)
8460 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8461 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8464 case GOT_STATUS_NO_CHANGE:
8466 case GOT_STATUS_CONFLICT:
8467 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8469 case GOT_STATUS_NONEXISTENT:
8470 err = got_error_set_errno(ENOENT, relpath);
8473 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8477 if (path_content && unlink(path_content) == -1 && err == NULL)
8478 err = got_error_from_errno2("unlink", path_content);
8481 free(new_staged_blob_id);
8485 const struct got_error *
8486 got_worktree_stage(struct got_worktree *worktree,
8487 struct got_pathlist_head *paths,
8488 got_worktree_status_cb status_cb, void *status_arg,
8489 got_worktree_patch_cb patch_cb, void *patch_arg,
8490 int allow_bad_symlinks, struct got_repository *repo)
8492 const struct got_error *err = NULL, *sync_err, *unlockerr;
8493 struct got_pathlist_entry *pe;
8494 struct got_fileindex *fileindex = NULL;
8495 char *fileindex_path = NULL;
8496 struct got_reference *head_ref = NULL;
8497 struct got_object_id *head_commit_id = NULL;
8498 struct check_stage_ok_arg oka;
8499 struct stage_path_arg spa;
8501 err = lock_worktree(worktree, LOCK_EX);
8505 err = got_ref_open(&head_ref, repo,
8506 got_worktree_get_head_ref_name(worktree), 0);
8509 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8512 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8516 /* Check pre-conditions before staging anything. */
8517 oka.head_commit_id = head_commit_id;
8518 oka.worktree = worktree;
8519 oka.fileindex = fileindex;
8521 oka.have_changes = 0;
8522 TAILQ_FOREACH(pe, paths, entry) {
8523 err = worktree_status(worktree, pe->path, fileindex, repo,
8524 check_stage_ok, &oka, NULL, NULL, 1, 0);
8528 if (!oka.have_changes) {
8529 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8533 spa.worktree = worktree;
8534 spa.fileindex = fileindex;
8536 spa.patch_cb = patch_cb;
8537 spa.patch_arg = patch_arg;
8538 spa.status_cb = status_cb;
8539 spa.status_arg = status_arg;
8540 spa.staged_something = 0;
8541 spa.allow_bad_symlinks = allow_bad_symlinks;
8542 TAILQ_FOREACH(pe, paths, entry) {
8543 err = worktree_status(worktree, pe->path, fileindex, repo,
8544 stage_path, &spa, NULL, NULL, 1, 0);
8548 if (!spa.staged_something) {
8549 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8553 sync_err = sync_fileindex(fileindex, fileindex_path);
8554 if (sync_err && err == NULL)
8558 got_ref_close(head_ref);
8559 free(head_commit_id);
8560 free(fileindex_path);
8562 got_fileindex_free(fileindex);
8563 unlockerr = lock_worktree(worktree, LOCK_SH);
8564 if (unlockerr && err == NULL)
8569 struct unstage_path_arg {
8570 struct got_worktree *worktree;
8571 struct got_fileindex *fileindex;
8572 struct got_repository *repo;
8573 got_worktree_checkout_cb progress_cb;
8575 got_worktree_patch_cb patch_cb;
8579 static const struct got_error *
8580 create_unstaged_content(char **path_unstaged_content,
8581 char **path_new_staged_content, struct got_object_id *blob_id,
8582 struct got_object_id *staged_blob_id, const char *relpath,
8583 struct got_repository *repo,
8584 got_worktree_patch_cb patch_cb, void *patch_arg)
8586 const struct got_error *err, *free_err;
8587 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8588 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8589 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8590 struct got_diffreg_result *diffreg_result = NULL;
8591 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8592 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8593 int fd1 = -1, fd2 = -1;
8595 *path_unstaged_content = NULL;
8596 *path_new_staged_content = NULL;
8598 err = got_object_id_str(&label1, blob_id);
8602 fd1 = got_opentempfd();
8604 err = got_error_from_errno("got_opentempfd");
8607 fd2 = got_opentempfd();
8609 err = got_error_from_errno("got_opentempfd");
8613 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8617 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
8621 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8625 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8630 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
8634 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8638 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8639 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8643 err = got_opentemp_named(path_unstaged_content, &outfile,
8644 "got-unstaged-content", "");
8647 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8648 "got-new-staged-content", "");
8652 if (fseek(f1, 0L, SEEK_SET) == -1) {
8653 err = got_ferror(f1, GOT_ERR_IO);
8656 if (fseek(f2, 0L, SEEK_SET) == -1) {
8657 err = got_ferror(f2, GOT_ERR_IO);
8660 /* Count the number of actual changes in the diff result. */
8661 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8662 struct diff_chunk_context cc = {};
8663 diff_chunk_context_load_change(&cc, &nchunks_used,
8664 diffreg_result->result, n, 0);
8667 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8669 err = apply_or_reject_change(&choice, &nchunks_used,
8670 diffreg_result->result, n, relpath, f1, f2,
8671 &line_cur1, &line_cur2,
8672 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8675 if (choice == GOT_PATCH_CHOICE_YES)
8678 have_rejected_content = 1;
8679 if (choice == GOT_PATCH_CHOICE_QUIT)
8682 if (have_content || have_rejected_content)
8683 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8684 outfile, rejectfile);
8687 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8688 err = got_error_from_errno("close");
8690 got_object_blob_close(blob);
8691 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8692 err = got_error_from_errno("close");
8694 got_object_blob_close(staged_blob);
8695 free_err = got_diffreg_result_free(diffreg_result);
8696 if (free_err && err == NULL)
8698 if (f1 && fclose(f1) == EOF && err == NULL)
8699 err = got_error_from_errno2("fclose", path1);
8700 if (f2 && fclose(f2) == EOF && err == NULL)
8701 err = got_error_from_errno2("fclose", path2);
8702 if (outfile && fclose(outfile) == EOF && err == NULL)
8703 err = got_error_from_errno2("fclose", *path_unstaged_content);
8704 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8705 err = got_error_from_errno2("fclose", *path_new_staged_content);
8706 if (path1 && unlink(path1) == -1 && err == NULL)
8707 err = got_error_from_errno2("unlink", path1);
8708 if (path2 && unlink(path2) == -1 && err == NULL)
8709 err = got_error_from_errno2("unlink", path2);
8710 if (err || !have_content) {
8711 if (*path_unstaged_content &&
8712 unlink(*path_unstaged_content) == -1 && err == NULL)
8713 err = got_error_from_errno2("unlink",
8714 *path_unstaged_content);
8715 free(*path_unstaged_content);
8716 *path_unstaged_content = NULL;
8718 if (err || !have_content || !have_rejected_content) {
8719 if (*path_new_staged_content &&
8720 unlink(*path_new_staged_content) == -1 && err == NULL)
8721 err = got_error_from_errno2("unlink",
8722 *path_new_staged_content);
8723 free(*path_new_staged_content);
8724 *path_new_staged_content = NULL;
8731 static const struct got_error *
8732 unstage_hunks(struct got_object_id *staged_blob_id,
8733 struct got_blob_object *blob_base,
8734 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8735 const char *ondisk_path, const char *label_orig,
8736 struct got_worktree *worktree, struct got_repository *repo,
8737 got_worktree_patch_cb patch_cb, void *patch_arg,
8738 got_worktree_checkout_cb progress_cb, void *progress_arg)
8740 const struct got_error *err = NULL;
8741 char *path_unstaged_content = NULL;
8742 char *path_new_staged_content = NULL;
8743 char *parent = NULL, *base_path = NULL;
8744 char *blob_base_path = NULL;
8745 struct got_object_id *new_staged_blob_id = NULL;
8746 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8749 err = create_unstaged_content(&path_unstaged_content,
8750 &path_new_staged_content, blob_id, staged_blob_id,
8751 ie->path, repo, patch_cb, patch_arg);
8755 if (path_unstaged_content == NULL)
8758 if (path_new_staged_content) {
8759 err = got_object_blob_create(&new_staged_blob_id,
8760 path_new_staged_content, repo);
8765 f = fopen(path_unstaged_content, "re");
8767 err = got_error_from_errno2("fopen",
8768 path_unstaged_content);
8771 if (fstat(fileno(f), &sb) == -1) {
8772 err = got_error_from_errno2("fstat", path_unstaged_content);
8775 if (got_fileindex_entry_staged_filetype_get(ie) ==
8776 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8777 char link_target[PATH_MAX];
8779 r = fread(link_target, 1, sizeof(link_target), f);
8780 if (r == 0 && ferror(f)) {
8781 err = got_error_from_errno("fread");
8784 if (r >= sizeof(link_target)) { /* should not happen */
8785 err = got_error(GOT_ERR_NO_SPACE);
8788 link_target[r] = '\0';
8789 err = merge_symlink(worktree, blob_base,
8790 ondisk_path, ie->path, label_orig, link_target,
8791 worktree->base_commit_id, repo, progress_cb,
8794 int local_changes_subsumed;
8796 err = got_path_dirname(&parent, ondisk_path);
8800 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8802 err = got_error_from_errno("asprintf");
8807 err = got_opentemp_named(&blob_base_path, &f_base,
8811 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8817 * In order the run a 3-way merge with a symlink we copy the symlink's
8818 * target path into a temporary file and use that file with diff3.
8820 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8821 err = dump_symlink_target_path_to_file(&f_deriv2,
8827 fd = open(ondisk_path,
8828 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8830 err = got_error_from_errno2("open", ondisk_path);
8833 f_deriv2 = fdopen(fd, "r");
8834 if (f_deriv2 == NULL) {
8835 err = got_error_from_errno2("fdopen", ondisk_path);
8841 err = merge_file(&local_changes_subsumed, worktree,
8842 f_base, f, f_deriv2, ondisk_path, ie->path,
8843 got_fileindex_perms_to_st(ie),
8844 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8845 repo, progress_cb, progress_arg);
8850 if (new_staged_blob_id) {
8851 memcpy(ie->staged_blob_hash, new_staged_blob_id->hash,
8852 SHA1_DIGEST_LENGTH);
8854 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8855 got_fileindex_entry_staged_filetype_set(ie, 0);
8858 free(new_staged_blob_id);
8859 if (path_unstaged_content &&
8860 unlink(path_unstaged_content) == -1 && err == NULL)
8861 err = got_error_from_errno2("unlink", path_unstaged_content);
8862 if (path_new_staged_content &&
8863 unlink(path_new_staged_content) == -1 && err == NULL)
8864 err = got_error_from_errno2("unlink", path_new_staged_content);
8865 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8866 err = got_error_from_errno2("unlink", blob_base_path);
8867 if (f_base && fclose(f_base) == EOF && err == NULL)
8868 err = got_error_from_errno2("fclose", path_unstaged_content);
8869 if (f && fclose(f) == EOF && err == NULL)
8870 err = got_error_from_errno2("fclose", path_unstaged_content);
8871 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8872 err = got_error_from_errno2("fclose", ondisk_path);
8873 free(path_unstaged_content);
8874 free(path_new_staged_content);
8875 free(blob_base_path);
8881 static const struct got_error *
8882 unstage_path(void *arg, unsigned char status,
8883 unsigned char staged_status, const char *relpath,
8884 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8885 struct got_object_id *commit_id, int dirfd, const char *de_name)
8887 const struct got_error *err = NULL;
8888 struct unstage_path_arg *a = arg;
8889 struct got_fileindex_entry *ie;
8890 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8891 char *ondisk_path = NULL;
8892 char *id_str = NULL, *label_orig = NULL;
8893 int local_changes_subsumed;
8895 int fd1 = -1, fd2 = -1;
8897 if (staged_status != GOT_STATUS_ADD &&
8898 staged_status != GOT_STATUS_MODIFY &&
8899 staged_status != GOT_STATUS_DELETE)
8902 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8904 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8906 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8908 return got_error_from_errno("asprintf");
8910 err = got_object_id_str(&id_str,
8911 commit_id ? commit_id : a->worktree->base_commit_id);
8914 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8916 err = got_error_from_errno("asprintf");
8920 fd1 = got_opentempfd();
8922 err = got_error_from_errno("got_opentempfd");
8925 fd2 = got_opentempfd();
8927 err = got_error_from_errno("got_opentempfd");
8931 switch (staged_status) {
8932 case GOT_STATUS_MODIFY:
8933 err = got_object_open_as_blob(&blob_base, a->repo,
8934 blob_id, 8192, fd1);
8938 case GOT_STATUS_ADD:
8940 if (staged_status == GOT_STATUS_ADD) {
8941 int choice = GOT_PATCH_CHOICE_NONE;
8942 err = (*a->patch_cb)(&choice, a->patch_arg,
8943 staged_status, ie->path, NULL, 1, 1);
8946 if (choice != GOT_PATCH_CHOICE_YES)
8949 err = unstage_hunks(staged_blob_id,
8950 blob_base, blob_id, ie, ondisk_path,
8951 label_orig, a->worktree, a->repo,
8952 a->patch_cb, a->patch_arg,
8953 a->progress_cb, a->progress_arg);
8954 break; /* Done with this file. */
8957 err = got_object_open_as_blob(&blob_staged, a->repo,
8958 staged_blob_id, 8192, fd2);
8961 switch (got_fileindex_entry_staged_filetype_get(ie)) {
8962 case GOT_FILEIDX_MODE_BAD_SYMLINK:
8963 case GOT_FILEIDX_MODE_REGULAR_FILE:
8964 err = merge_blob(&local_changes_subsumed, a->worktree,
8965 blob_base, ondisk_path, relpath,
8966 got_fileindex_perms_to_st(ie), label_orig,
8967 blob_staged, commit_id ? commit_id :
8968 a->worktree->base_commit_id, a->repo,
8969 a->progress_cb, a->progress_arg);
8971 case GOT_FILEIDX_MODE_SYMLINK:
8972 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8973 char *staged_target;
8974 err = got_object_blob_read_to_str(
8975 &staged_target, blob_staged);
8978 err = merge_symlink(a->worktree, blob_base,
8979 ondisk_path, relpath, label_orig,
8980 staged_target, commit_id ? commit_id :
8981 a->worktree->base_commit_id,
8982 a->repo, a->progress_cb, a->progress_arg);
8983 free(staged_target);
8985 err = merge_blob(&local_changes_subsumed,
8986 a->worktree, blob_base, ondisk_path,
8987 relpath, got_fileindex_perms_to_st(ie),
8988 label_orig, blob_staged,
8989 commit_id ? commit_id :
8990 a->worktree->base_commit_id, a->repo,
8991 a->progress_cb, a->progress_arg);
8995 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8999 got_fileindex_entry_stage_set(ie,
9000 GOT_FILEIDX_STAGE_NONE);
9001 got_fileindex_entry_staged_filetype_set(ie, 0);
9004 case GOT_STATUS_DELETE:
9006 int choice = GOT_PATCH_CHOICE_NONE;
9007 err = (*a->patch_cb)(&choice, a->patch_arg,
9008 staged_status, ie->path, NULL, 1, 1);
9011 if (choice == GOT_PATCH_CHOICE_NO)
9013 if (choice != GOT_PATCH_CHOICE_YES) {
9014 err = got_error(GOT_ERR_PATCH_CHOICE);
9018 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9019 got_fileindex_entry_staged_filetype_set(ie, 0);
9020 err = get_file_status(&status, &sb, ie, ondisk_path,
9021 dirfd, de_name, a->repo);
9024 err = (*a->progress_cb)(a->progress_arg, status, relpath);
9029 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9030 err = got_error_from_errno("close");
9032 got_object_blob_close(blob_base);
9033 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9034 err = got_error_from_errno("close");
9036 got_object_blob_close(blob_staged);
9042 const struct got_error *
9043 got_worktree_unstage(struct got_worktree *worktree,
9044 struct got_pathlist_head *paths,
9045 got_worktree_checkout_cb progress_cb, void *progress_arg,
9046 got_worktree_patch_cb patch_cb, void *patch_arg,
9047 struct got_repository *repo)
9049 const struct got_error *err = NULL, *sync_err, *unlockerr;
9050 struct got_pathlist_entry *pe;
9051 struct got_fileindex *fileindex = NULL;
9052 char *fileindex_path = NULL;
9053 struct unstage_path_arg upa;
9055 err = lock_worktree(worktree, LOCK_EX);
9059 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9063 upa.worktree = worktree;
9064 upa.fileindex = fileindex;
9066 upa.progress_cb = progress_cb;
9067 upa.progress_arg = progress_arg;
9068 upa.patch_cb = patch_cb;
9069 upa.patch_arg = patch_arg;
9070 TAILQ_FOREACH(pe, paths, entry) {
9071 err = worktree_status(worktree, pe->path, fileindex, repo,
9072 unstage_path, &upa, NULL, NULL, 1, 0);
9077 sync_err = sync_fileindex(fileindex, fileindex_path);
9078 if (sync_err && err == NULL)
9081 free(fileindex_path);
9083 got_fileindex_free(fileindex);
9084 unlockerr = lock_worktree(worktree, LOCK_SH);
9085 if (unlockerr && err == NULL)
9090 struct report_file_info_arg {
9091 struct got_worktree *worktree;
9092 got_worktree_path_info_cb info_cb;
9094 struct got_pathlist_head *paths;
9095 got_cancel_cb cancel_cb;
9099 static const struct got_error *
9100 report_file_info(void *arg, struct got_fileindex_entry *ie)
9102 struct report_file_info_arg *a = arg;
9103 struct got_pathlist_entry *pe;
9104 struct got_object_id blob_id, staged_blob_id, commit_id;
9105 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9106 struct got_object_id *commit_idp = NULL;
9109 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9110 return got_error(GOT_ERR_CANCELLED);
9112 TAILQ_FOREACH(pe, a->paths, entry) {
9113 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9114 got_path_is_child(ie->path, pe->path, pe->path_len))
9117 if (pe == NULL) /* not found */
9120 if (got_fileindex_entry_has_blob(ie)) {
9121 memcpy(blob_id.hash, ie->blob_hash, SHA1_DIGEST_LENGTH);
9122 blob_idp = &blob_id;
9124 stage = got_fileindex_entry_stage_get(ie);
9125 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9126 stage == GOT_FILEIDX_STAGE_ADD) {
9127 memcpy(staged_blob_id.hash, ie->staged_blob_hash,
9128 SHA1_DIGEST_LENGTH);
9129 staged_blob_idp = &staged_blob_id;
9132 if (got_fileindex_entry_has_commit(ie)) {
9133 memcpy(commit_id.hash, ie->commit_hash, SHA1_DIGEST_LENGTH);
9134 commit_idp = &commit_id;
9137 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9138 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9141 const struct got_error *
9142 got_worktree_path_info(struct got_worktree *worktree,
9143 struct got_pathlist_head *paths,
9144 got_worktree_path_info_cb info_cb, void *info_arg,
9145 got_cancel_cb cancel_cb, void *cancel_arg)
9148 const struct got_error *err = NULL, *unlockerr;
9149 struct got_fileindex *fileindex = NULL;
9150 char *fileindex_path = NULL;
9151 struct report_file_info_arg arg;
9153 err = lock_worktree(worktree, LOCK_SH);
9157 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9161 arg.worktree = worktree;
9162 arg.info_cb = info_cb;
9163 arg.info_arg = info_arg;
9165 arg.cancel_cb = cancel_cb;
9166 arg.cancel_arg = cancel_arg;
9167 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9170 free(fileindex_path);
9172 got_fileindex_free(fileindex);
9173 unlockerr = lock_worktree(worktree, LOCK_UN);
9174 if (unlockerr && err == NULL)
9179 static const struct got_error *
9180 patch_check_path(const char *p, char **path, unsigned char *status,
9181 unsigned char *staged_status, struct got_fileindex *fileindex,
9182 struct got_worktree *worktree, struct got_repository *repo)
9184 const struct got_error *err;
9185 struct got_fileindex_entry *ie;
9187 char *ondisk_path = NULL;
9189 err = got_worktree_resolve_path(path, worktree, p);
9193 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9194 *path[0] ? "/" : "", *path) == -1)
9195 return got_error_from_errno("asprintf");
9197 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9199 *staged_status = get_staged_status(ie);
9200 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9205 *staged_status = GOT_STATUS_NO_CHANGE;
9206 *status = GOT_STATUS_UNVERSIONED;
9207 if (lstat(ondisk_path, &sb) == -1) {
9208 if (errno != ENOENT) {
9209 err = got_error_from_errno2("lstat",
9213 *status = GOT_STATUS_NONEXISTENT;
9222 static const struct got_error *
9223 patch_can_rm(const char *path, unsigned char status,
9224 unsigned char staged_status)
9226 if (status == GOT_STATUS_NONEXISTENT)
9227 return got_error_set_errno(ENOENT, path);
9228 if (status != GOT_STATUS_NO_CHANGE &&
9229 status != GOT_STATUS_ADD &&
9230 status != GOT_STATUS_MODIFY &&
9231 status != GOT_STATUS_MODE_CHANGE)
9232 return got_error_path(path, GOT_ERR_FILE_STATUS);
9233 if (staged_status == GOT_STATUS_DELETE)
9234 return got_error_path(path, GOT_ERR_FILE_STATUS);
9238 static const struct got_error *
9239 patch_can_add(const char *path, unsigned char status)
9241 if (status != GOT_STATUS_NONEXISTENT)
9242 return got_error_path(path, GOT_ERR_FILE_STATUS);
9246 static const struct got_error *
9247 patch_can_edit(const char *path, unsigned char status,
9248 unsigned char staged_status)
9250 if (status == GOT_STATUS_NONEXISTENT)
9251 return got_error_set_errno(ENOENT, path);
9252 if (status != GOT_STATUS_NO_CHANGE &&
9253 status != GOT_STATUS_ADD &&
9254 status != GOT_STATUS_MODIFY)
9255 return got_error_path(path, GOT_ERR_FILE_STATUS);
9256 if (staged_status == GOT_STATUS_DELETE)
9257 return got_error_path(path, GOT_ERR_FILE_STATUS);
9261 const struct got_error *
9262 got_worktree_patch_prepare(struct got_fileindex **fileindex,
9263 char **fileindex_path, struct got_worktree *worktree)
9265 return open_fileindex(fileindex, fileindex_path, worktree);
9268 const struct got_error *
9269 got_worktree_patch_check_path(const char *old, const char *new,
9270 char **oldpath, char **newpath, struct got_worktree *worktree,
9271 struct got_repository *repo, struct got_fileindex *fileindex)
9273 const struct got_error *err = NULL;
9274 int file_renamed = 0;
9275 unsigned char status_old, staged_status_old;
9276 unsigned char status_new, staged_status_new;
9281 err = patch_check_path(old != NULL ? old : new, oldpath,
9282 &status_old, &staged_status_old, fileindex, worktree, repo);
9286 err = patch_check_path(new != NULL ? new : old, newpath,
9287 &status_new, &staged_status_new, fileindex, worktree, repo);
9291 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9294 if (old != NULL && new == NULL)
9295 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9296 else if (file_renamed) {
9297 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9299 err = patch_can_add(*newpath, status_new);
9300 } else if (old == NULL)
9301 err = patch_can_add(*newpath, status_new);
9303 err = patch_can_edit(*newpath, status_new, staged_status_new);
9315 const struct got_error *
9316 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9317 struct got_worktree *worktree, struct got_fileindex *fileindex,
9318 got_worktree_checkout_cb progress_cb, void *progress_arg)
9320 struct schedule_addition_args saa;
9322 memset(&saa, 0, sizeof(saa));
9323 saa.worktree = worktree;
9324 saa.fileindex = fileindex;
9325 saa.progress_cb = progress_cb;
9326 saa.progress_arg = progress_arg;
9329 return worktree_status(worktree, path, fileindex, repo,
9330 schedule_addition, &saa, NULL, NULL, 1, 0);
9333 const struct got_error *
9334 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9335 struct got_worktree *worktree, struct got_fileindex *fileindex,
9336 got_worktree_delete_cb progress_cb, void *progress_arg)
9338 struct schedule_deletion_args sda;
9340 memset(&sda, 0, sizeof(sda));
9341 sda.worktree = worktree;
9342 sda.fileindex = fileindex;
9343 sda.progress_cb = progress_cb;
9344 sda.progress_arg = progress_arg;
9346 sda.delete_local_mods = 0;
9347 sda.keep_on_disk = 0;
9348 sda.ignore_missing_paths = 0;
9349 sda.status_codes = NULL;
9351 return worktree_status(worktree, path, fileindex, repo,
9352 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9355 const struct got_error *
9356 got_worktree_patch_complete(struct got_fileindex *fileindex,
9357 const char *fileindex_path)
9359 const struct got_error *err = NULL;
9361 err = sync_fileindex(fileindex, fileindex_path);
9362 got_fileindex_free(fileindex);