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.
17 #include "got_compat.h"
20 #include <sys/queue.h>
36 #include "got_error.h"
37 #include "got_repository.h"
38 #include "got_reference.h"
39 #include "got_object.h"
41 #include "got_cancel.h"
42 #include "got_worktree.h"
43 #include "got_opentemp.h"
46 #include "got_lib_worktree.h"
47 #include "got_lib_hash.h"
48 #include "got_lib_fileindex.h"
49 #include "got_lib_inflate.h"
50 #include "got_lib_delta.h"
51 #include "got_lib_object.h"
52 #include "got_lib_object_parse.h"
53 #include "got_lib_object_create.h"
54 #include "got_lib_object_idset.h"
55 #include "got_lib_diff.h"
56 #include "got_lib_gotconfig.h"
59 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
62 #define GOT_MERGE_LABEL_MERGED "merged change"
63 #define GOT_MERGE_LABEL_BASE "3-way merge base"
66 apply_umask(mode_t mode)
75 static const struct got_error *
76 create_meta_file(const char *path_got, const char *name, const char *content)
78 const struct got_error *err = NULL;
81 if (asprintf(&path, "%s/%s", path_got, name) == -1)
82 return got_error_from_errno("asprintf");
84 err = got_path_create_file(path, content);
89 static const struct got_error *
90 update_meta_file(const char *path_got, const char *name, const char *content)
92 const struct got_error *err = NULL;
97 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
98 err = got_error_from_errno("asprintf");
103 err = got_opentemp_named(&tmppath, &tmpfile, path, "");
108 int len = fprintf(tmpfile, "%s\n", content);
109 if (len != strlen(content) + 1) {
110 err = got_error_from_errno2("fprintf", tmppath);
115 if (rename(tmppath, path) != 0) {
116 err = got_error_from_errno3("rename", tmppath, path);
122 if (fclose(tmpfile) == EOF && err == NULL)
123 err = got_error_from_errno2("fclose", tmppath);
128 static const struct got_error *
129 write_head_ref(const char *path_got, struct got_reference *head_ref)
131 const struct got_error *err = NULL;
134 if (got_ref_is_symbolic(head_ref)) {
135 refstr = got_ref_to_str(head_ref);
137 return got_error_from_errno("got_ref_to_str");
139 refstr = strdup(got_ref_get_name(head_ref));
141 return got_error_from_errno("strdup");
143 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
148 const struct got_error *
149 got_worktree_init(const char *path, struct got_reference *head_ref,
150 const char *prefix, const char *meta_dir, struct got_repository *repo)
152 const struct got_error *err = NULL;
153 struct got_object_id *commit_id = NULL;
155 uint32_t uuid_status;
157 char *path_got = NULL;
158 char *formatstr = NULL;
159 char *absprefix = NULL;
160 char *basestr = NULL;
161 char *uuidstr = NULL;
163 if (strcmp(path, got_repo_get_path(repo)) == 0) {
164 err = got_error(GOT_ERR_WORKTREE_REPO);
168 err = got_ref_resolve(&commit_id, repo, head_ref);
171 err = got_object_get_type(&obj_type, repo, commit_id);
174 if (obj_type != GOT_OBJ_TYPE_COMMIT)
175 return got_error(GOT_ERR_OBJ_TYPE);
177 if (!got_path_is_absolute(prefix)) {
178 if (asprintf(&absprefix, "/%s", prefix) == -1)
179 return got_error_from_errno("asprintf");
182 /* Create top-level directory (may already exist). */
183 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
184 err = got_error_from_errno2("mkdir", path);
188 /* Create .got/.cvg directory (may already exist). */
189 if (asprintf(&path_got, "%s/%s", path, meta_dir) == -1) {
190 err = got_error_from_errno("asprintf");
193 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
194 err = got_error_from_errno2("mkdir", path_got);
198 /* Create an empty lock file. */
199 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
203 /* Create an empty file index. */
204 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
208 /* Write the HEAD reference. */
209 err = write_head_ref(path_got, head_ref);
213 /* Record our base commit. */
214 err = got_object_id_str(&basestr, commit_id);
217 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
221 /* Store path to repository. */
222 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
223 got_repo_get_path(repo));
227 /* Store in-repository path prefix. */
228 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
229 absprefix ? absprefix : prefix);
234 uuid_create(&uuid, &uuid_status);
235 if (uuid_status != uuid_s_ok) {
236 err = got_error_uuid(uuid_status, "uuid_create");
239 uuid_to_string(&uuid, &uuidstr, &uuid_status);
240 if (uuid_status != uuid_s_ok) {
241 err = got_error_uuid(uuid_status, "uuid_to_string");
244 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
248 /* Stamp work tree with format file. */
249 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
250 err = got_error_from_errno("asprintf");
253 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
267 const struct got_error *
268 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
269 const char *path_prefix)
271 char *absprefix = NULL;
273 if (!got_path_is_absolute(path_prefix)) {
274 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
275 return got_error_from_errno("asprintf");
277 *match = (strcmp(absprefix ? absprefix : path_prefix,
278 worktree->path_prefix) == 0);
284 got_worktree_get_head_ref_name(struct got_worktree *worktree)
286 return worktree->head_ref_name;
289 const struct got_error *
290 got_worktree_set_head_ref(struct got_worktree *worktree,
291 struct got_reference *head_ref)
293 const struct got_error *err = NULL;
294 char *path_got = NULL, *head_ref_name = NULL;
296 if (asprintf(&path_got, "%s/%s", worktree->root_path,
297 worktree->meta_dir) == -1) {
298 err = got_error_from_errno("asprintf");
303 head_ref_name = strdup(got_ref_get_name(head_ref));
304 if (head_ref_name == NULL) {
305 err = got_error_from_errno("strdup");
309 err = write_head_ref(path_got, head_ref);
313 free(worktree->head_ref_name);
314 worktree->head_ref_name = head_ref_name;
322 struct got_object_id *
323 got_worktree_get_base_commit_id(struct got_worktree *worktree)
325 return worktree->base_commit_id;
328 const struct got_error *
329 got_worktree_set_base_commit_id(struct got_worktree *worktree,
330 struct got_repository *repo, struct got_object_id *commit_id)
332 const struct got_error *err;
333 struct got_object *obj = NULL;
335 char *path_got = NULL;
337 if (asprintf(&path_got, "%s/%s", worktree->root_path,
338 worktree->meta_dir) == -1) {
339 err = got_error_from_errno("asprintf");
344 err = got_object_open(&obj, repo, commit_id);
348 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
349 err = got_error(GOT_ERR_OBJ_TYPE);
353 /* Record our base commit. */
354 err = got_object_id_str(&id_str, commit_id);
357 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
361 free(worktree->base_commit_id);
362 worktree->base_commit_id = got_object_id_dup(commit_id);
363 if (worktree->base_commit_id == NULL) {
364 err = got_error_from_errno("got_object_id_dup");
369 got_object_close(obj);
375 const struct got_gotconfig *
376 got_worktree_get_gotconfig(struct got_worktree *worktree)
378 return worktree->gotconfig;
381 static const struct got_error *
382 lock_worktree(struct got_worktree *worktree, int operation)
384 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
385 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
386 : got_error_from_errno2("flock",
387 got_worktree_get_root_path(worktree)));
391 static const struct got_error *
392 add_dir_on_disk(struct got_worktree *worktree, const char *path)
394 const struct got_error *err = NULL;
397 /* We only accept worktree-relative paths here. */
398 if (got_path_is_absolute(path)) {
399 return got_error_fmt(GOT_ERR_BAD_PATH,
400 "%s does not accept absolute paths: %s",
404 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
405 return got_error_from_errno("asprintf");
407 err = got_path_mkdir(abspath);
408 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
411 if (lstat(abspath, &sb) == -1) {
412 err = got_error_from_errno2("lstat", abspath);
413 } else if (!S_ISDIR(sb.st_mode)) {
414 /* TODO directory is obstructed; do something */
415 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
422 static const struct got_error *
423 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
425 const struct got_error *err = NULL;
428 size_t flen1 = 0, flen2 = 0;
433 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
434 if (flen1 == 0 && ferror(f1)) {
435 err = got_error_from_errno("fread");
438 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
439 if (flen2 == 0 && ferror(f2)) {
440 err = got_error_from_errno("fread");
447 } else if (flen2 == 0) {
451 } else if (flen1 == flen2) {
452 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
465 static const struct got_error *
466 check_files_equal(int *same, FILE *f1, FILE *f2)
473 if (fstat(fileno(f1), &sb) != 0)
474 return got_error_from_errno("fstat");
477 if (fstat(fileno(f2), &sb) != 0)
478 return got_error_from_errno("fstat");
481 if (size1 != size2) {
486 if (fseek(f1, 0L, SEEK_SET) == -1)
487 return got_ferror(f1, GOT_ERR_IO);
488 if (fseek(f2, 0L, SEEK_SET) == -1)
489 return got_ferror(f2, GOT_ERR_IO);
491 return check_file_contents_equal(same, f1, f2);
494 static const struct got_error *
495 copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
503 if (fseek(f, 0L, SEEK_SET) == -1)
504 return got_ferror(f, GOT_ERR_IO);
507 flen = fread(fbuf, 1, sizeof(fbuf), f);
510 return got_error_from_errno("fread");
514 outlen = write(outfd, fbuf, flen);
516 return got_error_from_errno("write");
518 return got_error(GOT_ERR_IO);
525 static const struct got_error *
526 merge_binary_file(int *overlapcnt, int merged_fd,
527 FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
528 const char *label_deriv, const char *label_orig, const char *label_deriv2,
529 const char *ondisk_path)
531 const struct got_error *err = NULL;
532 int same_content, changed_deriv, changed_deriv2;
533 int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
534 off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
535 char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
536 char *base_path_orig = NULL, *base_path_deriv = NULL;
537 char *base_path_deriv2 = NULL;
541 err = check_files_equal(&same_content, f_deriv, f_deriv2);
546 return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
548 err = check_files_equal(&same_content, f_deriv, f_orig);
551 changed_deriv = !same_content;
552 err = check_files_equal(&same_content, f_deriv2, f_orig);
555 changed_deriv2 = !same_content;
557 if (changed_deriv && changed_deriv2) {
559 if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
560 err = got_error_from_errno("asprintf");
563 if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
564 err = got_error_from_errno("asprintf");
567 if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
568 err = got_error_from_errno("asprintf");
571 err = got_opentemp_named_fd(&path_orig, &fd_orig,
575 err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
576 base_path_deriv, "");
579 err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
580 base_path_deriv2, "");
583 err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
586 err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
589 err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
592 if (dprintf(merged_fd, "Binary files differ and cannot be "
593 "merged automatically:\n") < 0) {
594 err = got_error_from_errno("dprintf");
597 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
598 GOT_DIFF_CONFLICT_MARKER_BEGIN,
599 label_deriv ? " " : "",
600 label_deriv ? label_deriv : "",
602 err = got_error_from_errno("dprintf");
606 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
607 GOT_DIFF_CONFLICT_MARKER_ORIG,
608 label_orig ? " " : "",
609 label_orig ? label_orig : "",
611 err = got_error_from_errno("dprintf");
615 if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
616 GOT_DIFF_CONFLICT_MARKER_SEP,
618 GOT_DIFF_CONFLICT_MARKER_END,
619 label_deriv2 ? " " : "",
620 label_deriv2 ? label_deriv2 : "") < 0) {
621 err = got_error_from_errno("dprintf");
624 } else if (changed_deriv)
625 err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
626 else if (changed_deriv2)
627 err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
629 if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
631 err = got_error_from_errno2("unlink", path_orig);
632 if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
633 err = got_error_from_errno2("close", path_orig);
634 if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
635 err = got_error_from_errno2("close", path_deriv);
636 if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
637 err = got_error_from_errno2("close", path_deriv2);
641 free(base_path_orig);
642 free(base_path_deriv);
643 free(base_path_deriv2);
648 * Perform a 3-way merge where the file f_orig acts as the common
649 * ancestor, the file f_deriv acts as the first derived version,
650 * and the file f_deriv2 acts as the second derived version.
651 * The merge result will be written to a new file at ondisk_path; any
652 * existing file at this path will be replaced.
654 static const struct got_error *
655 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
656 FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
657 const char *path, uint16_t st_mode,
658 const char *label_orig, const char *label_deriv, const char *label_deriv2,
659 enum got_diff_algorithm diff_algo, struct got_repository *repo,
660 got_worktree_checkout_cb progress_cb, void *progress_arg)
662 const struct got_error *err = NULL;
664 FILE *f_merged = NULL;
665 char *merged_path = NULL, *base_path = NULL;
669 *local_changes_subsumed = 0;
671 err = got_path_dirname(&parent, ondisk_path);
675 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
676 err = got_error_from_errno("asprintf");
680 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path, "");
684 err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
685 f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
687 if (err->code != GOT_ERR_FILE_BINARY)
689 err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
690 f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
696 err = (*progress_cb)(progress_arg,
697 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
701 if (fsync(merged_fd) != 0) {
702 err = got_error_from_errno("fsync");
706 f_merged = fdopen(merged_fd, "r");
707 if (f_merged == NULL) {
708 err = got_error_from_errno("fdopen");
713 /* Check if a clean merge has subsumed all local changes. */
714 if (overlapcnt == 0) {
715 err = check_files_equal(local_changes_subsumed, f_deriv,
721 if (fchmod(fileno(f_merged), apply_umask(st_mode)) != 0) {
722 err = got_error_from_errno2("fchmod", merged_path);
726 if (rename(merged_path, ondisk_path) != 0) {
727 err = got_error_from_errno3("rename", merged_path,
736 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
737 err = got_error_from_errno("close");
738 if (f_merged && fclose(f_merged) == EOF && err == NULL)
739 err = got_error_from_errno("fclose");
746 static const struct got_error *
747 update_symlink(const char *ondisk_path, const char *target_path,
750 /* This is not atomic but matches what 'ln -sf' does. */
751 if (unlink(ondisk_path) == -1)
752 return got_error_from_errno2("unlink", ondisk_path);
753 if (symlink(target_path, ondisk_path) == -1)
754 return got_error_from_errno3("symlink", target_path,
760 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
761 * in the work tree with a file that contains conflict markers and the
762 * conflicting target paths of the original version, a "derived version"
763 * of a symlink from an incoming change, and a local version of the symlink.
765 * The original versions's target path can be NULL if it is not available,
766 * such as if both derived versions added a new symlink at the same path.
768 * The incoming derived symlink target is NULL in case the incoming change
769 * has deleted this symlink.
771 static const struct got_error *
772 install_symlink_conflict(const char *deriv_target,
773 struct got_object_id *deriv_base_commit_id, const char *orig_target,
774 const char *label_orig, const char *local_target, const char *ondisk_path)
776 const struct got_error *err;
777 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
780 err = got_object_id_str(&id_str, deriv_base_commit_id);
782 return got_error_from_errno("asprintf");
784 if (asprintf(&label_deriv, "%s: commit %s",
785 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
786 err = got_error_from_errno("asprintf");
790 err = got_opentemp_named(&path, &f, "got-symlink-conflict", "");
794 if (fchmod(fileno(f), apply_umask(GOT_DEFAULT_FILE_MODE)) == -1) {
795 err = got_error_from_errno2("fchmod", path);
799 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
800 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
801 deriv_target ? deriv_target : "(symlink was deleted)",
802 orig_target ? label_orig : "",
803 orig_target ? "\n" : "",
804 orig_target ? orig_target : "",
805 orig_target ? "\n" : "",
806 GOT_DIFF_CONFLICT_MARKER_SEP,
807 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
808 err = got_error_from_errno2("fprintf", path);
812 if (unlink(ondisk_path) == -1) {
813 err = got_error_from_errno2("unlink", ondisk_path);
816 if (rename(path, ondisk_path) == -1) {
817 err = got_error_from_errno3("rename", path, ondisk_path);
821 if (f != NULL && fclose(f) == EOF && err == NULL)
822 err = got_error_from_errno2("fclose", path);
829 /* forward declaration */
830 static const struct got_error *
831 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
832 const char *, const char *, uint16_t, const char *,
833 struct got_blob_object *, struct got_object_id *,
834 struct got_repository *, got_worktree_checkout_cb, void *);
837 * Merge a symlink into the work tree, where blob_orig acts as the common
838 * ancestor, deriv_target is the link target of the first derived version,
839 * and the symlink on disk acts as the second derived version.
840 * Assume that contents of both blobs represent symlinks.
842 static const struct got_error *
843 merge_symlink(struct got_worktree *worktree,
844 struct got_blob_object *blob_orig, const char *ondisk_path,
845 const char *path, const char *label_orig, const char *deriv_target,
846 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
847 got_worktree_checkout_cb progress_cb, void *progress_arg)
849 const struct got_error *err = NULL;
850 char *ancestor_target = NULL;
852 ssize_t ondisk_len, deriv_len;
853 char ondisk_target[PATH_MAX];
854 int have_local_change = 0;
855 int have_incoming_change = 0;
857 if (lstat(ondisk_path, &sb) == -1)
858 return got_error_from_errno2("lstat", ondisk_path);
860 ondisk_len = readlink(ondisk_path, ondisk_target,
861 sizeof(ondisk_target));
862 if (ondisk_len == -1) {
863 err = got_error_from_errno2("readlink",
867 ondisk_target[ondisk_len] = '\0';
870 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
875 if (ancestor_target == NULL ||
876 (ondisk_len != strlen(ancestor_target) ||
877 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
878 have_local_change = 1;
880 deriv_len = strlen(deriv_target);
881 if (ancestor_target == NULL ||
882 (deriv_len != strlen(ancestor_target) ||
883 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
884 have_incoming_change = 1;
886 if (!have_local_change && !have_incoming_change) {
887 if (ancestor_target) {
888 /* Both sides made the same change. */
889 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
891 } else if (deriv_len == ondisk_len &&
892 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
893 /* Both sides added the same symlink. */
894 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
897 /* Both sides added symlinks which don't match. */
898 err = install_symlink_conflict(deriv_target,
899 deriv_base_commit_id, ancestor_target,
900 label_orig, ondisk_target, ondisk_path);
903 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
906 } else if (!have_local_change && have_incoming_change) {
907 /* Apply the incoming change. */
908 err = update_symlink(ondisk_path, deriv_target,
909 strlen(deriv_target));
912 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
913 } else if (have_local_change && have_incoming_change) {
914 if (deriv_len == ondisk_len &&
915 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
916 /* Both sides made the same change. */
917 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
920 err = install_symlink_conflict(deriv_target,
921 deriv_base_commit_id, ancestor_target, label_orig,
922 ondisk_target, ondisk_path);
925 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
931 free(ancestor_target);
935 static const struct got_error *
936 dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
938 const struct got_error *err = NULL;
939 char target_path[PATH_MAX];
948 return got_error_from_errno("got_opentemp");
949 target_len = readlink(ondisk_path, target_path, sizeof(target_path));
950 if (target_len == -1) {
951 err = got_error_from_errno2("readlink", ondisk_path);
954 n = fwrite(target_path, 1, target_len, f);
955 if (n != target_len) {
956 err = got_ferror(f, GOT_ERR_IO);
959 if (fflush(f) == EOF) {
960 err = got_error_from_errno("fflush");
963 if (fseek(f, 0L, SEEK_SET) == -1) {
964 err = got_ferror(f, GOT_ERR_IO);
976 * Perform a 3-way merge where blob_orig acts as the common ancestor,
977 * blob_deriv acts as the first derived version, and the file on disk
978 * acts as the second derived version.
980 static const struct got_error *
981 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
982 struct got_blob_object *blob_orig, const char *ondisk_path,
983 const char *path, uint16_t st_mode, const char *label_orig,
984 struct got_blob_object *blob_deriv,
985 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
986 got_worktree_checkout_cb progress_cb, void *progress_arg)
988 const struct got_error *err = NULL;
989 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
990 char *blob_orig_path = NULL;
991 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
992 char *label_deriv = NULL, *parent = NULL;
994 *local_changes_subsumed = 0;
996 err = got_path_dirname(&parent, ondisk_path);
1001 if (asprintf(&base_path, "%s/got-merge-blob-orig",
1003 err = got_error_from_errno("asprintf");
1008 err = got_opentemp_named(&blob_orig_path, &f_orig,
1012 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1019 * No common ancestor exists. This is an "add vs add" conflict
1020 * and we simply use an empty ancestor file to make both files
1021 * appear in the merged result in their entirety.
1023 f_orig = got_opentemp();
1024 if (f_orig == NULL) {
1025 err = got_error_from_errno("got_opentemp");
1030 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1031 err = got_error_from_errno("asprintf");
1036 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path, "");
1039 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1044 err = got_object_id_str(&id_str, deriv_base_commit_id);
1047 if (asprintf(&label_deriv, "%s: commit %s",
1048 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1049 err = got_error_from_errno("asprintf");
1054 * In order the run a 3-way merge with a symlink we copy the symlink's
1055 * target path into a temporary file and use that file with diff3.
1057 if (S_ISLNK(st_mode)) {
1058 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1063 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1065 err = got_error_from_errno2("open", ondisk_path);
1068 f_deriv2 = fdopen(fd, "r");
1069 if (f_deriv2 == NULL) {
1070 err = got_error_from_errno2("fdopen", ondisk_path);
1076 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1077 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1078 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1080 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1081 err = got_error_from_errno("fclose");
1082 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1083 err = got_error_from_errno("fclose");
1084 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1085 err = got_error_from_errno("fclose");
1087 if (blob_orig_path) {
1088 unlink(blob_orig_path);
1089 free(blob_orig_path);
1091 if (blob_deriv_path) {
1092 unlink(blob_deriv_path);
1093 free(blob_deriv_path);
1101 static const struct got_error *
1102 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1103 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1104 int wt_fd, const char *path, struct got_object_id *blob_id,
1105 int update_timestamps)
1107 const struct got_error *err = NULL;
1108 struct got_fileindex_entry *new_ie;
1112 err = got_fileindex_entry_alloc(&new_ie, path);
1116 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1117 blob_id->sha1, base_commit_id->sha1, update_timestamps);
1121 err = got_fileindex_entry_add(fileindex, new_ie);
1124 got_fileindex_entry_free(new_ie);
1131 get_ondisk_perms(int executable, mode_t st_mode)
1133 mode_t xbits = S_IXUSR;
1136 /* Map read bits to execute bits. */
1137 if (st_mode & S_IRGRP)
1139 if (st_mode & S_IROTH)
1141 return st_mode | xbits;
1147 /* forward declaration */
1148 static const struct got_error *
1149 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1150 const char *path, mode_t te_mode, mode_t st_mode,
1151 struct got_blob_object *blob, int restoring_missing_file,
1152 int reverting_versioned_file, int installing_bad_symlink,
1153 int path_is_unversioned, int *update_timestamps,
1154 struct got_repository *repo,
1155 got_worktree_checkout_cb progress_cb, void *progress_arg);
1158 * This function assumes that the provided symlink target points at a
1159 * safe location in the work tree!
1161 static const struct got_error *
1162 replace_existing_symlink(int *did_something, const char *ondisk_path,
1163 const char *target_path, size_t target_len)
1165 const struct got_error *err = NULL;
1167 char etarget[PATH_MAX];
1173 * "Bad" symlinks (those pointing outside the work tree or into the
1174 * .got directory) are installed in the work tree as a regular file
1175 * which contains the bad symlink target path.
1176 * The new symlink target has already been checked for safety by our
1177 * caller. If we can successfully open a regular file then we simply
1178 * replace this file with a symlink below.
1180 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1182 if (!got_err_open_nofollow_on_symlink())
1183 return got_error_from_errno2("open", ondisk_path);
1185 /* We are updating an existing on-disk symlink. */
1186 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1188 return got_error_from_errno2("readlink", ondisk_path);
1190 if (elen == target_len &&
1191 memcmp(etarget, target_path, target_len) == 0)
1192 return NULL; /* nothing to do */
1196 err = update_symlink(ondisk_path, target_path, target_len);
1197 if (fd != -1 && close(fd) == -1 && err == NULL)
1198 err = got_error_from_errno2("close", ondisk_path);
1202 static const struct got_error *
1203 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1204 size_t target_len, const char *ondisk_path, const char *wtroot_path,
1205 const char *meta_dir)
1207 const struct got_error *err = NULL;
1208 char canonpath[PATH_MAX];
1209 char *path_got = NULL;
1211 *is_bad_symlink = 0;
1213 if (target_len >= sizeof(canonpath)) {
1214 *is_bad_symlink = 1;
1219 * We do not use realpath(3) to resolve the symlink's target
1220 * path because we don't want to resolve symlinks recursively.
1221 * Instead we make the path absolute and then canonicalize it.
1222 * Relative symlink target lookup should begin at the directory
1223 * in which the blob object is being installed.
1225 if (!got_path_is_absolute(target_path)) {
1226 char *abspath, *parent;
1227 err = got_path_dirname(&parent, ondisk_path);
1230 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1232 return got_error_from_errno("asprintf");
1235 if (strlen(abspath) >= sizeof(canonpath)) {
1236 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1240 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1245 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1250 /* Only allow symlinks pointing at paths within the work tree. */
1251 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1252 *is_bad_symlink = 1;
1256 /* Do not allow symlinks pointing into the meta directory. */
1257 if (asprintf(&path_got, "%s/%s", wtroot_path, meta_dir) == -1)
1258 return got_error_from_errno("asprintf");
1259 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1260 *is_bad_symlink = 1;
1266 static const struct got_error *
1267 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1268 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1269 int restoring_missing_file, int reverting_versioned_file,
1270 int path_is_unversioned, int allow_bad_symlinks,
1271 struct got_repository *repo,
1272 got_worktree_checkout_cb progress_cb, void *progress_arg)
1274 const struct got_error *err = NULL;
1275 char target_path[PATH_MAX];
1276 size_t len, target_len = 0;
1277 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1278 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1280 *is_bad_symlink = 0;
1283 * Blob object content specifies the target path of the link.
1284 * If a symbolic link cannot be installed we instead create
1285 * a regular file which contains the link target path stored
1286 * in the blob object.
1289 err = got_object_blob_read_block(&len, blob);
1293 if (len + target_len >= sizeof(target_path)) {
1294 /* Path too long; install as a regular file. */
1295 *is_bad_symlink = 1;
1296 got_object_blob_rewind(blob);
1297 return install_blob(worktree, ondisk_path, path,
1298 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1299 restoring_missing_file, reverting_versioned_file,
1300 1, path_is_unversioned, NULL, repo, progress_cb,
1304 /* Skip blob object header first time around. */
1305 memcpy(target_path + target_len, buf + hdrlen,
1307 target_len += len - hdrlen;
1311 target_path[target_len] = '\0';
1313 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1314 ondisk_path, worktree->root_path, worktree->meta_dir);
1318 if (*is_bad_symlink && !allow_bad_symlinks) {
1319 /* install as a regular file */
1320 got_object_blob_rewind(blob);
1321 err = install_blob(worktree, ondisk_path, path,
1322 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1323 restoring_missing_file, reverting_versioned_file, 1,
1324 path_is_unversioned, NULL, repo,
1325 progress_cb, progress_arg);
1329 if (symlink(target_path, ondisk_path) == -1) {
1330 if (errno == EEXIST) {
1331 int symlink_replaced;
1332 if (path_is_unversioned) {
1333 err = (*progress_cb)(progress_arg,
1334 GOT_STATUS_UNVERSIONED, path);
1337 err = replace_existing_symlink(&symlink_replaced,
1338 ondisk_path, target_path, target_len);
1342 if (symlink_replaced) {
1343 err = (*progress_cb)(progress_arg,
1344 reverting_versioned_file ?
1346 GOT_STATUS_UPDATE, path);
1348 err = (*progress_cb)(progress_arg,
1349 GOT_STATUS_EXISTS, path);
1352 return err; /* Nothing else to do. */
1355 if (errno == ENOENT) {
1357 err = got_path_dirname(&parent, ondisk_path);
1360 err = got_path_mkdir(parent);
1365 * Retry, and fall through to error handling
1366 * below if this second attempt fails.
1368 if (symlink(target_path, ondisk_path) != -1) {
1369 err = NULL; /* success */
1371 err = (*progress_cb)(progress_arg,
1372 reverting_versioned_file ?
1373 GOT_STATUS_REVERT : GOT_STATUS_ADD,
1380 /* Handle errors from first or second creation attempt. */
1381 if (errno == ENAMETOOLONG) {
1382 /* bad target path; install as a regular file */
1383 *is_bad_symlink = 1;
1384 got_object_blob_rewind(blob);
1385 err = install_blob(worktree, ondisk_path, path,
1386 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1387 restoring_missing_file, reverting_versioned_file, 1,
1388 path_is_unversioned, NULL, repo,
1389 progress_cb, progress_arg);
1390 } else if (errno == ENOTDIR) {
1391 err = got_error_path(ondisk_path,
1392 GOT_ERR_FILE_OBSTRUCTED);
1394 err = got_error_from_errno3("symlink",
1395 target_path, ondisk_path);
1397 } else if (progress_cb)
1398 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1399 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1403 static const struct got_error *
1404 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1405 const char *path, mode_t te_mode, mode_t st_mode,
1406 struct got_blob_object *blob, int restoring_missing_file,
1407 int reverting_versioned_file, int installing_bad_symlink,
1408 int path_is_unversioned, int *update_timestamps,
1409 struct got_repository *repo,
1410 got_worktree_checkout_cb progress_cb, void *progress_arg)
1412 const struct got_error *err = NULL;
1416 char *tmppath = NULL;
1419 if (update_timestamps)
1420 *update_timestamps = 1;
1422 mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1423 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1426 if (errno == ENOENT || errno == ENOTDIR) {
1428 err = got_path_dirname(&parent, path);
1431 err = add_dir_on_disk(worktree, parent);
1432 if (err && err->code == GOT_ERR_FILE_OBSTRUCTED)
1433 err = got_error_path(path, err->code);
1437 fd = open(ondisk_path,
1438 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1441 return got_error_from_errno2("open",
1443 } else if (errno == EEXIST) {
1444 if (path_is_unversioned) {
1445 if (update_timestamps)
1446 *update_timestamps = 0;
1447 err = (*progress_cb)(progress_arg,
1448 GOT_STATUS_EXISTS, path);
1451 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1452 !S_ISREG(st_mode) && !installing_bad_symlink) {
1453 /* TODO file is obstructed; do something */
1454 err = got_error_path(ondisk_path,
1455 GOT_ERR_FILE_OBSTRUCTED);
1458 err = got_opentemp_named_fd(&tmppath, &fd,
1464 if (fchmod(fd, apply_umask(mode)) == -1) {
1465 err = got_error_from_errno2("fchmod",
1471 return got_error_from_errno2("open", ondisk_path);
1475 if (restoring_missing_file)
1476 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1478 else if (reverting_versioned_file)
1479 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1482 err = (*progress_cb)(progress_arg,
1483 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1488 hdrlen = got_object_blob_get_hdrlen(blob);
1490 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1491 err = got_object_blob_read_block(&len, blob);
1495 /* Skip blob object header first time around. */
1496 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1498 err = got_error_from_errno("write");
1500 } else if (outlen != len - hdrlen) {
1501 err = got_error(GOT_ERR_IO);
1508 if (fsync(fd) != 0) {
1509 err = got_error_from_errno("fsync");
1514 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1515 err = got_error_from_errno2("unlink", ondisk_path);
1518 if (rename(tmppath, ondisk_path) != 0) {
1519 err = got_error_from_errno3("rename", tmppath,
1528 if (fd != -1 && close(fd) == -1 && err == NULL)
1529 err = got_error_from_errno("close");
1530 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1531 err = got_error_from_errno2("unlink", tmppath);
1537 * Upgrade STATUS_MODIFY to STATUS_CONFLICT if a
1538 * conflict marker is found in newly added lines only.
1540 static const struct got_error *
1541 get_modified_file_content_status(unsigned char *status,
1542 struct got_blob_object *blob, const char *path, struct stat *sb,
1545 const struct got_error *err, *free_err;
1546 const char *markers[3] = {
1547 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1548 GOT_DIFF_CONFLICT_MARKER_SEP,
1549 GOT_DIFF_CONFLICT_MARKER_END
1552 struct got_diffreg_result *diffreg_result = NULL;
1553 struct diff_result *r;
1554 int nchunks_parsed, n, i = 0, ln = 0;
1556 size_t linesize = 0;
1559 if (*status != GOT_STATUS_MODIFY)
1562 f1 = got_opentemp();
1564 return got_error_from_errno("got_opentemp");
1567 got_object_blob_rewind(blob);
1568 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
1573 err = got_diff_files(&diffreg_result, f1, 1, NULL, ondisk_file,
1574 1, NULL, 0, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
1578 r = diffreg_result->result;
1580 for (n = 0; n < r->chunks.len; n += nchunks_parsed) {
1581 struct diff_chunk *c;
1582 struct diff_chunk_context cc = {};
1586 * We can optimise a little by advancing straight
1587 * to the next chunk if this one has no added lines.
1589 c = diff_chunk_get(r, n);
1591 if (diff_chunk_type(c) != CHUNK_PLUS) {
1593 continue; /* removed or unchanged lines */
1596 pos = diff_chunk_get_right_start_pos(c);
1597 if (fseek(ondisk_file, pos, SEEK_SET) == -1) {
1598 err = got_ferror(ondisk_file, GOT_ERR_IO);
1602 diff_chunk_context_load_change(&cc, &nchunks_parsed, r, n, 0);
1603 ln = cc.right.start;
1605 while (ln < cc.right.end) {
1606 linelen = getline(&line, &linesize, ondisk_file);
1607 if (linelen == -1) {
1608 if (feof(ondisk_file))
1610 err = got_ferror(ondisk_file, GOT_ERR_IO);
1614 if (line && strncmp(line, markers[i],
1615 strlen(markers[i])) == 0) {
1616 if (strcmp(markers[i],
1617 GOT_DIFF_CONFLICT_MARKER_END) == 0) {
1618 *status = GOT_STATUS_CONFLICT;
1629 if (f1 != NULL && fclose(f1) == EOF && err == NULL)
1630 err = got_error_from_errno("fclose");
1631 free_err = got_diffreg_result_free(diffreg_result);
1639 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1641 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1642 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1646 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1648 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1649 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1650 ie->mtime_sec == sb->st_mtim.tv_sec &&
1651 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1652 ie->size == (sb->st_size & 0xffffffff) &&
1653 !xbit_differs(ie, sb->st_mode));
1656 static unsigned char
1657 get_staged_status(struct got_fileindex_entry *ie)
1659 switch (got_fileindex_entry_stage_get(ie)) {
1660 case GOT_FILEIDX_STAGE_ADD:
1661 return GOT_STATUS_ADD;
1662 case GOT_FILEIDX_STAGE_DELETE:
1663 return GOT_STATUS_DELETE;
1664 case GOT_FILEIDX_STAGE_MODIFY:
1665 return GOT_STATUS_MODIFY;
1667 return GOT_STATUS_NO_CHANGE;
1671 static const struct got_error *
1672 get_symlink_modification_status(unsigned char *status,
1673 struct got_fileindex_entry *ie, const char *abspath,
1674 int dirfd, const char *de_name, struct got_blob_object *blob)
1676 const struct got_error *err = NULL;
1677 char target_path[PATH_MAX];
1678 char etarget[PATH_MAX];
1680 size_t len, target_len = 0;
1681 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1682 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1684 *status = GOT_STATUS_NO_CHANGE;
1686 /* Blob object content specifies the target path of the link. */
1688 err = got_object_blob_read_block(&len, blob);
1691 if (len + target_len >= sizeof(target_path)) {
1693 * Should not happen. The blob contents were OK
1694 * when this symlink was installed.
1696 return got_error(GOT_ERR_NO_SPACE);
1699 /* Skip blob object header first time around. */
1700 memcpy(target_path + target_len, buf + hdrlen,
1702 target_len += len - hdrlen;
1706 target_path[target_len] = '\0';
1709 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1711 return got_error_from_errno2("readlinkat", abspath);
1713 elen = readlink(abspath, etarget, sizeof(etarget));
1715 return got_error_from_errno2("readlink", abspath);
1718 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1719 *status = GOT_STATUS_MODIFY;
1724 static const struct got_error *
1725 get_file_status(unsigned char *status, struct stat *sb,
1726 struct got_fileindex_entry *ie, const char *abspath,
1727 int dirfd, const char *de_name, struct got_repository *repo)
1729 const struct got_error *err = NULL;
1730 struct got_object_id id;
1732 int fd = -1, fd1 = -1;
1735 struct got_blob_object *blob = NULL;
1737 unsigned char staged_status;
1739 staged_status = get_staged_status(ie);
1740 *status = GOT_STATUS_NO_CHANGE;
1741 memset(sb, 0, sizeof(*sb));
1744 * Whenever the caller provides a directory descriptor and a
1745 * directory entry name for the file, use them! This prevents
1746 * race conditions if filesystem paths change beneath our feet.
1749 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1750 if (errno == ENOENT) {
1751 if (got_fileindex_entry_has_file_on_disk(ie))
1752 *status = GOT_STATUS_MISSING;
1754 *status = GOT_STATUS_DELETE;
1757 err = got_error_from_errno2("fstatat", abspath);
1761 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1762 if (fd == -1 && errno != ENOENT &&
1763 !got_err_open_nofollow_on_symlink())
1764 return got_error_from_errno2("open", abspath);
1765 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1766 if (lstat(abspath, sb) == -1)
1767 return got_error_from_errno2("lstat", abspath);
1768 } else if (fd == -1 || fstat(fd, sb) == -1) {
1769 if (errno == ENOENT) {
1770 if (got_fileindex_entry_has_file_on_disk(ie))
1771 *status = GOT_STATUS_MISSING;
1773 *status = GOT_STATUS_DELETE;
1776 err = got_error_from_errno2("fstat", abspath);
1781 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1782 *status = GOT_STATUS_OBSTRUCTED;
1786 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1787 *status = GOT_STATUS_DELETE;
1789 } else if (!got_fileindex_entry_has_blob(ie) &&
1790 staged_status != GOT_STATUS_ADD) {
1791 *status = GOT_STATUS_ADD;
1795 if (!stat_info_differs(ie, sb))
1798 if (S_ISLNK(sb->st_mode) &&
1799 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1800 *status = GOT_STATUS_MODIFY;
1804 if (staged_status == GOT_STATUS_MODIFY ||
1805 staged_status == GOT_STATUS_ADD)
1806 got_fileindex_entry_get_staged_blob_id(&id, ie);
1808 got_fileindex_entry_get_blob_id(&id, ie);
1810 fd1 = got_opentempfd();
1812 err = got_error_from_errno("got_opentempfd");
1815 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1819 if (S_ISLNK(sb->st_mode)) {
1820 err = get_symlink_modification_status(status, ie,
1821 abspath, dirfd, de_name, blob);
1826 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1828 err = got_error_from_errno2("openat", abspath);
1833 f = fdopen(fd, "r");
1835 err = got_error_from_errno2("fdopen", abspath);
1839 hdrlen = got_object_blob_get_hdrlen(blob);
1841 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1842 err = got_object_blob_read_block(&blen, blob);
1845 /* Skip length of blob object header first time around. */
1846 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1847 if (flen == 0 && ferror(f)) {
1848 err = got_error_from_errno("fread");
1851 if (blen - hdrlen == 0) {
1853 *status = GOT_STATUS_MODIFY;
1855 } else if (flen == 0) {
1856 if (blen - hdrlen != 0)
1857 *status = GOT_STATUS_MODIFY;
1859 } else if (blen - hdrlen == flen) {
1860 /* Skip blob object header first time around. */
1861 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1862 *status = GOT_STATUS_MODIFY;
1866 *status = GOT_STATUS_MODIFY;
1872 if (*status == GOT_STATUS_MODIFY) {
1874 err = get_modified_file_content_status(status, blob, ie->path,
1876 } else if (xbit_differs(ie, sb->st_mode))
1877 *status = GOT_STATUS_MODE_CHANGE;
1879 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1880 err = got_error_from_errno("close");
1882 got_object_blob_close(blob);
1883 if (f != NULL && fclose(f) == EOF && err == NULL)
1884 err = got_error_from_errno2("fclose", abspath);
1885 if (fd != -1 && close(fd) == -1 && err == NULL)
1886 err = got_error_from_errno2("close", abspath);
1891 * Update timestamps in the file index if a file is unmodified and
1892 * we had to run a full content comparison to find out.
1894 static const struct got_error *
1895 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1896 struct got_fileindex_entry *ie, struct stat *sb)
1898 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1899 return got_fileindex_entry_update(ie, wt_fd, path,
1900 ie->blob_sha1, ie->commit_sha1, 1);
1905 static const struct got_error *remove_ondisk_file(const char *, const char *);
1907 static const struct got_error *
1908 update_blob(struct got_worktree *worktree,
1909 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1910 struct got_tree_entry *te, const char *path,
1911 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1914 const struct got_error *err = NULL;
1915 struct got_blob_object *blob = NULL;
1916 char *ondisk_path = NULL;
1917 unsigned char status = GOT_STATUS_NO_CHANGE;
1919 int fd1 = -1, fd2 = -1;
1920 int update_timestamps;
1922 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1923 return got_error_from_errno("asprintf");
1926 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1927 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1930 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1934 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1935 sb.st_mode = got_fileindex_perms_to_st(ie);
1937 if (stat(ondisk_path, &sb) == -1) {
1938 if (errno != ENOENT && errno != ENOTDIR) {
1939 err = got_error_from_errno2("stat",
1943 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1944 status = GOT_STATUS_UNVERSIONED;
1946 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1947 status = GOT_STATUS_UNVERSIONED;
1949 status = GOT_STATUS_OBSTRUCTED;
1953 if (status == GOT_STATUS_OBSTRUCTED) {
1955 got_fileindex_entry_mark_skipped(ie);
1956 err = (*progress_cb)(progress_arg, status, path);
1959 if (status == GOT_STATUS_CONFLICT) {
1961 got_fileindex_entry_mark_skipped(ie);
1962 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1967 if (S_ISDIR(te->mode)) { /* file changing into a directory */
1968 if (status == GOT_STATUS_UNVERSIONED) {
1969 err = (*progress_cb)(progress_arg, status, path);
1970 } else if (status != GOT_STATUS_NO_CHANGE &&
1971 status != GOT_STATUS_DELETE &&
1972 status != GOT_STATUS_NONEXISTENT &&
1973 status != GOT_STATUS_MISSING) {
1974 err = (*progress_cb)(progress_arg,
1975 GOT_STATUS_CANNOT_DELETE, path);
1977 if (status != GOT_STATUS_DELETE &&
1978 status != GOT_STATUS_NONEXISTENT &&
1979 status != GOT_STATUS_MISSING) {
1980 err = remove_ondisk_file(worktree->root_path,
1982 if (err && !(err->code == GOT_ERR_ERRNO &&
1986 got_fileindex_entry_remove(fileindex, ie);
1987 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE,
1990 goto done; /* nothing else to do */
1993 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1994 (S_ISLNK(te->mode) ||
1995 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1997 * This is a regular file or an installed bad symlink.
1998 * If the file index indicates that this file is already
1999 * up-to-date with respect to the repository we can skip
2000 * updating contents of this file.
2002 if (got_fileindex_entry_has_commit(ie) &&
2003 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
2004 SHA1_DIGEST_LENGTH) == 0) {
2006 err = sync_timestamps(worktree->root_fd,
2007 path, status, ie, &sb);
2010 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2014 if (got_fileindex_entry_has_blob(ie) &&
2015 memcmp(ie->blob_sha1, te->id.sha1,
2016 SHA1_DIGEST_LENGTH) == 0) {
2017 /* Different commit but the same blob. */
2018 if (got_fileindex_entry_has_commit(ie)) {
2019 /* Update the base commit ID of this file. */
2020 memcpy(ie->commit_sha1,
2021 worktree->base_commit_id->sha1,
2022 sizeof(ie->commit_sha1));
2024 err = sync_timestamps(worktree->root_fd,
2025 path, status, ie, &sb);
2028 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2034 fd1 = got_opentempfd();
2036 err = got_error_from_errno("got_opentempfd");
2039 err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
2043 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
2044 int update_timestamps;
2045 struct got_blob_object *blob2 = NULL;
2046 char *label_orig = NULL;
2047 if (got_fileindex_entry_has_blob(ie)) {
2048 fd2 = got_opentempfd();
2050 err = got_error_from_errno("got_opentempfd");
2053 struct got_object_id id2;
2054 got_fileindex_entry_get_blob_id(&id2, ie);
2055 err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
2060 if (got_fileindex_entry_has_commit(ie)) {
2061 char id_str[SHA1_DIGEST_STRING_LENGTH];
2062 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2063 sizeof(id_str)) == NULL) {
2064 err = got_error_path(id_str,
2065 GOT_ERR_BAD_OBJ_ID_STR);
2068 if (asprintf(&label_orig, "%s: commit %s",
2069 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2070 err = got_error_from_errno("asprintf");
2074 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2076 err = got_object_blob_read_to_str(&link_target, blob);
2079 err = merge_symlink(worktree, blob2, ondisk_path, path,
2080 label_orig, link_target, worktree->base_commit_id,
2081 repo, progress_cb, progress_arg);
2084 err = merge_blob(&update_timestamps, worktree, blob2,
2085 ondisk_path, path, sb.st_mode, label_orig, blob,
2086 worktree->base_commit_id, repo,
2087 progress_cb, progress_arg);
2090 if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2091 err = got_error_from_errno("close");
2095 got_object_blob_close(blob2);
2099 * Do not update timestamps of files with local changes.
2100 * Otherwise, a future status walk would treat them as
2101 * unmodified files again.
2103 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2104 blob->id.sha1, worktree->base_commit_id->sha1,
2106 } else if (status == GOT_STATUS_MODE_CHANGE) {
2107 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2108 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2109 } else if (status == GOT_STATUS_DELETE) {
2110 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2113 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2114 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2118 int is_bad_symlink = 0;
2119 if (S_ISLNK(te->mode)) {
2120 err = install_symlink(&is_bad_symlink, worktree,
2121 ondisk_path, path, blob,
2122 status == GOT_STATUS_MISSING, 0,
2123 status == GOT_STATUS_UNVERSIONED, 0,
2124 repo, progress_cb, progress_arg);
2126 err = install_blob(worktree, ondisk_path, path,
2127 te->mode, sb.st_mode, blob,
2128 status == GOT_STATUS_MISSING, 0, 0,
2129 status == GOT_STATUS_UNVERSIONED,
2131 repo, progress_cb, progress_arg);
2137 err = got_fileindex_entry_update(ie,
2138 worktree->root_fd, path, blob->id.sha1,
2139 worktree->base_commit_id->sha1, 1);
2141 err = create_fileindex_entry(&ie, fileindex,
2142 worktree->base_commit_id, worktree->root_fd, path,
2143 &blob->id, update_timestamps);
2148 if (is_bad_symlink) {
2149 got_fileindex_entry_filetype_set(ie,
2150 GOT_FILEIDX_MODE_BAD_SYMLINK);
2154 if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2155 err = got_error_from_errno("close");
2158 got_object_blob_close(blob);
2164 static const struct got_error *
2165 remove_ondisk_file(const char *root_path, const char *path)
2167 const struct got_error *err = NULL;
2168 char *ondisk_path = NULL, *parent = NULL;
2170 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2171 return got_error_from_errno("asprintf");
2173 if (unlink(ondisk_path) == -1) {
2174 if (errno != ENOENT)
2175 err = got_error_from_errno2("unlink", ondisk_path);
2177 size_t root_len = strlen(root_path);
2178 err = got_path_dirname(&parent, ondisk_path);
2181 while (got_path_cmp(parent, root_path,
2182 strlen(parent), root_len) != 0) {
2184 ondisk_path = parent;
2186 if (rmdir(ondisk_path) == -1) {
2187 if (errno != ENOTEMPTY)
2188 err = got_error_from_errno2("rmdir",
2192 err = got_path_dirname(&parent, ondisk_path);
2203 static const struct got_error *
2204 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2205 struct got_fileindex_entry *ie, struct got_repository *repo,
2206 got_worktree_checkout_cb progress_cb, void *progress_arg)
2208 const struct got_error *err = NULL;
2209 unsigned char status;
2213 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2214 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2216 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2218 return got_error_from_errno("asprintf");
2220 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2224 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2225 char ondisk_target[PATH_MAX];
2226 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2227 sizeof(ondisk_target));
2228 if (ondisk_len == -1) {
2229 err = got_error_from_errno2("readlink", ondisk_path);
2232 ondisk_target[ondisk_len] = '\0';
2233 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2234 NULL, NULL, /* XXX pass common ancestor info? */
2235 ondisk_target, ondisk_path);
2238 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2243 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2244 status == GOT_STATUS_ADD) {
2245 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2249 * Preserve the working file and change the deleted blob's
2250 * entry into a schedule-add entry.
2252 err = got_fileindex_entry_update(ie, worktree->root_fd,
2253 ie->path, NULL, NULL, 0);
2255 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2258 if (status == GOT_STATUS_NO_CHANGE) {
2259 err = remove_ondisk_file(worktree->root_path, ie->path);
2263 got_fileindex_entry_remove(fileindex, ie);
2270 struct diff_cb_arg {
2271 struct got_fileindex *fileindex;
2272 struct got_worktree *worktree;
2273 struct got_repository *repo;
2274 got_worktree_checkout_cb progress_cb;
2276 got_cancel_cb cancel_cb;
2280 static const struct got_error *
2281 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2282 struct got_tree_entry *te, const char *parent_path)
2284 const struct got_error *err = NULL;
2285 struct diff_cb_arg *a = arg;
2288 err = a->cancel_cb(a->cancel_arg);
2293 return update_blob(a->worktree, a->fileindex, ie, te,
2294 ie->path, a->repo, a->progress_cb, a->progress_arg);
2297 static const struct got_error *
2298 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2300 const struct got_error *err = NULL;
2301 struct diff_cb_arg *a = arg;
2304 err = a->cancel_cb(a->cancel_arg);
2309 return delete_blob(a->worktree, a->fileindex, ie,
2310 a->repo, a->progress_cb, a->progress_arg);
2313 static const struct got_error *
2314 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2316 const struct got_error *err = NULL;
2317 struct diff_cb_arg *a = arg;
2321 err = a->cancel_cb(a->cancel_arg);
2326 if (got_object_tree_entry_is_submodule(te))
2329 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2332 if (asprintf(&path, "%s%s%s", parent_path,
2333 parent_path[0] ? "/" : "", te->name)
2335 return got_error_from_errno("asprintf");
2337 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2338 a->repo, a->progress_cb, a->progress_arg);
2344 const struct got_error *
2345 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2347 uint32_t uuid_status;
2349 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2350 if (uuid_status != uuid_s_ok) {
2352 return got_error_uuid(uuid_status, "uuid_to_string");
2358 static const struct got_error *
2359 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2361 const struct got_error *err = NULL;
2362 char *uuidstr = NULL;
2366 err = got_worktree_get_uuid(&uuidstr, worktree);
2370 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2371 err = got_error_from_errno("asprintf");
2378 const struct got_error *
2379 got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2382 return get_ref_name(refname, worktree, prefix);
2385 static const struct got_error *
2386 get_base_ref_name(char **refname, struct got_worktree *worktree)
2388 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2391 static const struct got_error *
2392 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2394 return get_ref_name(refname, worktree,
2395 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2398 static const struct got_error *
2399 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2401 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2404 static const struct got_error *
2405 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2407 return get_ref_name(refname, worktree,
2408 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2411 static const struct got_error *
2412 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2414 return get_ref_name(refname, worktree,
2415 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2418 static const struct got_error *
2419 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2421 return get_ref_name(refname, worktree,
2422 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2425 static const struct got_error *
2426 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2428 return get_ref_name(refname, worktree,
2429 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2432 static const struct got_error *
2433 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2435 return get_ref_name(refname, worktree,
2436 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2439 static const struct got_error *
2440 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2442 return get_ref_name(refname, worktree,
2443 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2446 const struct got_error *
2447 got_worktree_get_histedit_script_path(char **path,
2448 struct got_worktree *worktree)
2450 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2451 worktree->meta_dir, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2453 return got_error_from_errno("asprintf");
2458 static const struct got_error *
2459 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2461 return get_ref_name(refname, worktree,
2462 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2465 static const struct got_error *
2466 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2468 return get_ref_name(refname, worktree,
2469 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2473 * Prevent Git's garbage collector from deleting our base commit by
2474 * setting a reference to our base commit's ID.
2476 static const struct got_error *
2477 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2479 const struct got_error *err = NULL;
2480 struct got_reference *ref = NULL;
2483 err = get_base_ref_name(&refname, worktree);
2487 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2491 err = got_ref_write(ref, repo);
2499 static const struct got_error *
2500 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2502 const struct got_error *err = NULL;
2504 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2505 worktree->meta_dir, GOT_WORKTREE_FILE_INDEX) == -1) {
2506 err = got_error_from_errno("asprintf");
2507 *fileindex_path = NULL;
2513 static const struct got_error *
2514 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2515 struct got_worktree *worktree)
2517 const struct got_error *err = NULL;
2520 *fileindex_path = NULL;
2521 *fileindex = got_fileindex_alloc();
2522 if (*fileindex == NULL)
2523 return got_error_from_errno("got_fileindex_alloc");
2525 err = get_fileindex_path(fileindex_path, worktree);
2529 index = fopen(*fileindex_path, "rbe");
2530 if (index == NULL) {
2531 if (errno != ENOENT)
2532 err = got_error_from_errno2("fopen", *fileindex_path);
2534 err = got_fileindex_read(*fileindex, index);
2535 if (fclose(index) == EOF && err == NULL)
2536 err = got_error_from_errno("fclose");
2540 free(*fileindex_path);
2541 *fileindex_path = NULL;
2542 got_fileindex_free(*fileindex);
2548 struct bump_base_commit_id_arg {
2549 struct got_object_id *base_commit_id;
2552 const char *entry_name;
2553 got_worktree_checkout_cb progress_cb;
2557 static const struct got_error *
2558 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2560 const struct got_error *err;
2561 struct bump_base_commit_id_arg *a = arg;
2563 if (a->entry_name) {
2564 if (strcmp(ie->path, a->path) != 0)
2566 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2569 if (got_fileindex_entry_was_skipped(ie))
2572 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2573 SHA1_DIGEST_LENGTH) == 0)
2576 if (a->progress_cb) {
2577 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2582 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2586 /* Bump base commit ID of all files within an updated part of the work tree. */
2587 static const struct got_error *
2588 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2589 struct got_fileindex *fileindex,
2590 got_worktree_checkout_cb progress_cb, void *progress_arg)
2592 struct bump_base_commit_id_arg bbc_arg;
2594 bbc_arg.base_commit_id = worktree->base_commit_id;
2595 bbc_arg.entry_name = NULL;
2597 bbc_arg.path_len = 0;
2598 bbc_arg.progress_cb = progress_cb;
2599 bbc_arg.progress_arg = progress_arg;
2601 return got_fileindex_for_each_entry_safe(fileindex,
2602 bump_base_commit_id, &bbc_arg);
2605 static const struct got_error *
2606 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2608 const struct got_error *err = NULL;
2609 char *new_fileindex_path = NULL;
2610 FILE *new_index = NULL;
2611 struct timespec timeout;
2613 err = got_opentemp_named(&new_fileindex_path, &new_index,
2614 fileindex_path, "");
2618 err = got_fileindex_write(fileindex, new_index);
2622 if (rename(new_fileindex_path, fileindex_path) != 0) {
2623 err = got_error_from_errno3("rename", new_fileindex_path,
2625 unlink(new_fileindex_path);
2629 * Sleep for a short amount of time to ensure that files modified after
2630 * this program exits have a different time stamp from the one which
2631 * was recorded in the file index.
2634 timeout.tv_nsec = 1;
2635 nanosleep(&timeout, NULL);
2639 free(new_fileindex_path);
2643 static const struct got_error *
2644 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2645 struct got_object_id **tree_id, const char *wt_relpath,
2646 struct got_commit_object *base_commit, struct got_worktree *worktree,
2647 struct got_repository *repo)
2649 const struct got_error *err = NULL;
2650 struct got_object_id *id = NULL;
2651 char *in_repo_path = NULL;
2652 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2654 *entry_type = GOT_OBJ_TYPE_ANY;
2655 *tree_relpath = NULL;
2658 if (wt_relpath[0] == '\0') {
2659 /* Check out all files within the work tree. */
2660 *entry_type = GOT_OBJ_TYPE_TREE;
2661 *tree_relpath = strdup("");
2662 if (*tree_relpath == NULL) {
2663 err = got_error_from_errno("strdup");
2666 err = got_object_id_by_path(tree_id, repo, base_commit,
2667 worktree->path_prefix);
2673 /* Check out a subset of files in the work tree. */
2675 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2676 is_root_wt ? "" : "/", wt_relpath) == -1) {
2677 err = got_error_from_errno("asprintf");
2681 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2686 in_repo_path = NULL;
2688 err = got_object_get_type(entry_type, repo, id);
2692 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2693 /* Check out a single file. */
2694 if (strchr(wt_relpath, '/') == NULL) {
2695 /* Check out a single file in work tree's root dir. */
2696 in_repo_path = strdup(worktree->path_prefix);
2697 if (in_repo_path == NULL) {
2698 err = got_error_from_errno("strdup");
2701 *tree_relpath = strdup("");
2702 if (*tree_relpath == NULL) {
2703 err = got_error_from_errno("strdup");
2707 /* Check out a single file in a subdirectory. */
2708 err = got_path_dirname(tree_relpath, wt_relpath);
2711 if (asprintf(&in_repo_path, "%s%s%s",
2712 worktree->path_prefix, is_root_wt ? "" : "/",
2713 *tree_relpath) == -1) {
2714 err = got_error_from_errno("asprintf");
2718 err = got_object_id_by_path(tree_id, repo,
2719 base_commit, in_repo_path);
2721 /* Check out all files within a subdirectory. */
2722 *tree_id = got_object_id_dup(id);
2723 if (*tree_id == NULL) {
2724 err = got_error_from_errno("got_object_id_dup");
2727 *tree_relpath = strdup(wt_relpath);
2728 if (*tree_relpath == NULL) {
2729 err = got_error_from_errno("strdup");
2737 *entry_type = GOT_OBJ_TYPE_ANY;
2738 free(*tree_relpath);
2739 *tree_relpath = NULL;
2746 static const struct got_error *
2747 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2748 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2749 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2750 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2752 const struct got_error *err = NULL;
2753 struct got_commit_object *commit = NULL;
2754 struct got_tree_object *tree = NULL;
2755 struct got_fileindex_diff_tree_cb diff_cb;
2756 struct diff_cb_arg arg;
2758 err = ref_base_commit(worktree, repo);
2760 if (!(err->code == GOT_ERR_ERRNO &&
2761 (errno == EACCES || errno == EROFS)))
2763 err = (*progress_cb)(progress_arg,
2764 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2769 err = got_object_open_as_commit(&commit, repo,
2770 worktree->base_commit_id);
2774 err = got_object_open_as_tree(&tree, repo, tree_id);
2779 got_object_tree_find_entry(tree, entry_name) == NULL) {
2780 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2784 diff_cb.diff_old_new = diff_old_new;
2785 diff_cb.diff_old = diff_old;
2786 diff_cb.diff_new = diff_new;
2787 arg.fileindex = fileindex;
2788 arg.worktree = worktree;
2790 arg.progress_cb = progress_cb;
2791 arg.progress_arg = progress_arg;
2792 arg.cancel_cb = cancel_cb;
2793 arg.cancel_arg = cancel_arg;
2794 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2795 entry_name, repo, &diff_cb, &arg);
2798 got_object_tree_close(tree);
2800 got_object_commit_close(commit);
2804 const struct got_error *
2805 got_worktree_checkout_files(struct got_worktree *worktree,
2806 struct got_pathlist_head *paths, struct got_repository *repo,
2807 got_worktree_checkout_cb progress_cb, void *progress_arg,
2808 got_cancel_cb cancel_cb, void *cancel_arg)
2810 const struct got_error *err = NULL, *sync_err, *unlockerr;
2811 struct got_commit_object *commit = NULL;
2812 struct got_tree_object *tree = NULL;
2813 struct got_fileindex *fileindex = NULL;
2814 char *fileindex_path = NULL;
2815 struct got_pathlist_entry *pe;
2816 struct tree_path_data {
2817 STAILQ_ENTRY(tree_path_data) entry;
2818 struct got_object_id *tree_id;
2823 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2825 STAILQ_INIT(&tree_paths);
2827 err = lock_worktree(worktree, LOCK_EX);
2831 err = got_object_open_as_commit(&commit, repo,
2832 worktree->base_commit_id);
2836 /* Map all specified paths to in-repository trees. */
2837 TAILQ_FOREACH(pe, paths, entry) {
2838 tpd = malloc(sizeof(*tpd));
2840 err = got_error_from_errno("malloc");
2844 err = find_tree_entry_for_checkout(&tpd->entry_type,
2845 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2852 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2853 err = got_path_basename(&tpd->entry_name, pe->path);
2861 tpd->entry_name = NULL;
2863 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2867 * Read the file index.
2868 * Checking out files is supposed to be an idempotent operation.
2869 * If the on-disk file index is incomplete we will try to complete it.
2871 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2875 tpd = STAILQ_FIRST(&tree_paths);
2876 TAILQ_FOREACH(pe, paths, entry) {
2877 struct bump_base_commit_id_arg bbc_arg;
2879 err = checkout_files(worktree, fileindex, tpd->relpath,
2880 tpd->tree_id, tpd->entry_name, repo,
2881 progress_cb, progress_arg, cancel_cb, cancel_arg);
2885 bbc_arg.base_commit_id = worktree->base_commit_id;
2886 bbc_arg.entry_name = tpd->entry_name;
2887 bbc_arg.path = pe->path;
2888 bbc_arg.path_len = pe->path_len;
2889 bbc_arg.progress_cb = progress_cb;
2890 bbc_arg.progress_arg = progress_arg;
2891 err = got_fileindex_for_each_entry_safe(fileindex,
2892 bump_base_commit_id, &bbc_arg);
2896 tpd = STAILQ_NEXT(tpd, entry);
2898 sync_err = sync_fileindex(fileindex, fileindex_path);
2899 if (sync_err && err == NULL)
2902 free(fileindex_path);
2904 got_object_tree_close(tree);
2906 got_object_commit_close(commit);
2908 got_fileindex_free(fileindex);
2909 while (!STAILQ_EMPTY(&tree_paths)) {
2910 tpd = STAILQ_FIRST(&tree_paths);
2911 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2916 unlockerr = lock_worktree(worktree, LOCK_SH);
2917 if (unlockerr && err == NULL)
2922 static const struct got_error *
2923 add_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2924 struct got_fileindex_entry *ie, const char *ondisk_path,
2925 const char *path2, struct got_blob_object *blob2, mode_t mode2,
2926 int restoring_missing_file, int reverting_versioned_file,
2927 int path_is_unversioned, int allow_bad_symlinks,
2928 struct got_repository *repo,
2929 got_worktree_checkout_cb progress_cb, void *progress_arg)
2931 const struct got_error *err = NULL;
2932 int is_bad_symlink = 0;
2934 if (S_ISLNK(mode2)) {
2935 err = install_symlink(&is_bad_symlink,
2936 worktree, ondisk_path, path2, blob2,
2937 restoring_missing_file,
2938 reverting_versioned_file,
2939 path_is_unversioned, allow_bad_symlinks,
2940 repo, progress_cb, progress_arg);
2942 err = install_blob(worktree, ondisk_path, path2,
2943 mode2, GOT_DEFAULT_FILE_MODE, blob2,
2944 restoring_missing_file, reverting_versioned_file, 0,
2945 path_is_unversioned, NULL, repo,
2946 progress_cb, progress_arg);
2951 /* Adding an unversioned file. */
2952 err = got_fileindex_entry_alloc(&ie, path2);
2955 err = got_fileindex_entry_update(ie,
2956 worktree->root_fd, path2, NULL, NULL, 1);
2958 got_fileindex_entry_free(ie);
2961 err = got_fileindex_entry_add(fileindex, ie);
2963 got_fileindex_entry_free(ie);
2967 /* Re-adding a locally deleted file. */
2968 err = got_fileindex_entry_update(ie,
2969 worktree->root_fd, path2, ie->blob_sha1,
2970 worktree->base_commit_id->sha1, 0);
2975 if (is_bad_symlink) {
2976 got_fileindex_entry_filetype_set(ie,
2977 GOT_FILEIDX_MODE_BAD_SYMLINK);
2983 struct merge_file_cb_arg {
2984 struct got_worktree *worktree;
2985 struct got_fileindex *fileindex;
2986 got_worktree_checkout_cb progress_cb;
2988 got_cancel_cb cancel_cb;
2990 const char *label_orig;
2991 struct got_object_id *commit_id2;
2992 int allow_bad_symlinks;
2995 static const struct got_error *
2996 merge_file_cb(void *arg, struct got_blob_object *blob1,
2997 struct got_blob_object *blob2, FILE *f1, FILE *f2,
2998 struct got_object_id *id1, struct got_object_id *id2,
2999 const char *path1, const char *path2,
3000 mode_t mode1, mode_t mode2, struct got_repository *repo)
3002 static const struct got_error *err = NULL;
3003 struct merge_file_cb_arg *a = arg;
3004 struct got_fileindex_entry *ie;
3005 char *ondisk_path = NULL;
3007 unsigned char status;
3008 int local_changes_subsumed;
3009 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
3010 char *id_str = NULL, *label_deriv2 = NULL;
3011 struct got_object_id *id = NULL;
3013 if (blob1 && blob2) {
3014 ie = got_fileindex_entry_get(a->fileindex, path2,
3017 return (*a->progress_cb)(a->progress_arg,
3018 GOT_STATUS_MISSING, path2);
3020 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3022 return got_error_from_errno("asprintf");
3024 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3029 if (status == GOT_STATUS_DELETE) {
3030 err = (*a->progress_cb)(a->progress_arg,
3031 GOT_STATUS_MERGE, path2);
3034 if (status != GOT_STATUS_NO_CHANGE &&
3035 status != GOT_STATUS_MODIFY &&
3036 status != GOT_STATUS_CONFLICT &&
3037 status != GOT_STATUS_ADD) {
3038 err = (*a->progress_cb)(a->progress_arg, status, path2);
3042 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
3044 err = got_object_blob_read_to_str(&link_target2, blob2);
3047 err = merge_symlink(a->worktree, blob1, ondisk_path,
3048 path2, a->label_orig, link_target2, a->commit_id2,
3049 repo, a->progress_cb, a->progress_arg);
3054 f_orig = got_opentemp();
3055 if (f_orig == NULL) {
3056 err = got_error_from_errno("got_opentemp");
3059 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3064 f_deriv2 = got_opentemp();
3065 if (f_deriv2 == NULL)
3067 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3072 fd = open(ondisk_path,
3073 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3075 err = got_error_from_errno2("open",
3079 f_deriv = fdopen(fd, "r");
3080 if (f_deriv == NULL) {
3081 err = got_error_from_errno2("fdopen",
3086 err = got_object_id_str(&id_str, a->commit_id2);
3089 if (asprintf(&label_deriv2, "%s: commit %s",
3090 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
3091 err = got_error_from_errno("asprintf");
3094 err = merge_file(&local_changes_subsumed, a->worktree,
3095 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
3096 mode2, a->label_orig, NULL, label_deriv2,
3097 GOT_DIFF_ALGORITHM_PATIENCE, repo,
3098 a->progress_cb, a->progress_arg);
3101 ie = got_fileindex_entry_get(a->fileindex, path1,
3104 return (*a->progress_cb)(a->progress_arg,
3105 GOT_STATUS_MISSING, path1);
3107 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3109 return got_error_from_errno("asprintf");
3111 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3117 case GOT_STATUS_NO_CHANGE:
3118 err = (*a->progress_cb)(a->progress_arg,
3119 GOT_STATUS_DELETE, path1);
3122 err = remove_ondisk_file(a->worktree->root_path, path1);
3126 got_fileindex_entry_mark_deleted_from_disk(ie);
3128 case GOT_STATUS_DELETE:
3129 case GOT_STATUS_MISSING:
3130 err = (*a->progress_cb)(a->progress_arg,
3131 GOT_STATUS_DELETE, path1);
3135 got_fileindex_entry_mark_deleted_from_disk(ie);
3137 case GOT_STATUS_MODIFY: {
3142 * Delete the file only if its content already
3143 * exists in the repository.
3145 err = got_object_blob_file_create(&id, &blob1_f,
3146 &blob1_size, path1);
3149 if (fclose(blob1_f) == EOF) {
3150 err = got_error_from_errno("fclose");
3154 /* Implied existence check. */
3155 err = got_object_get_type(&obj_type, repo, id);
3157 if (err->code != GOT_ERR_NO_OBJ)
3159 err = (*a->progress_cb)(a->progress_arg,
3160 GOT_STATUS_CANNOT_DELETE, path1);
3162 } else if (obj_type != GOT_OBJ_TYPE_BLOB) {
3163 err = (*a->progress_cb)(a->progress_arg,
3164 GOT_STATUS_CANNOT_DELETE, path1);
3167 err = (*a->progress_cb)(a->progress_arg,
3168 GOT_STATUS_DELETE, path1);
3171 err = remove_ondisk_file(a->worktree->root_path,
3176 got_fileindex_entry_mark_deleted_from_disk(ie);
3179 case GOT_STATUS_ADD: {
3183 * Delete the file only if its content already
3184 * exists in the repository.
3186 err = got_object_blob_file_create(&id, &blob1_f,
3187 &blob1_size, path1);
3190 if (fclose(blob1_f) == EOF) {
3191 err = got_error_from_errno("fclose");
3194 if (got_object_id_cmp(id, id1) == 0) {
3195 err = (*a->progress_cb)(a->progress_arg,
3196 GOT_STATUS_DELETE, path1);
3199 err = remove_ondisk_file(a->worktree->root_path,
3204 got_fileindex_entry_remove(a->fileindex,
3207 err = (*a->progress_cb)(a->progress_arg,
3208 GOT_STATUS_CANNOT_DELETE, path1);
3214 case GOT_STATUS_CONFLICT:
3215 err = (*a->progress_cb)(a->progress_arg,
3216 GOT_STATUS_CANNOT_DELETE, path1);
3220 case GOT_STATUS_OBSTRUCTED:
3221 err = (*a->progress_cb)(a->progress_arg, status, path1);
3229 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3231 return got_error_from_errno("asprintf");
3232 ie = got_fileindex_entry_get(a->fileindex, path2,
3235 err = get_file_status(&status, &sb, ie, ondisk_path,
3239 if (status != GOT_STATUS_NO_CHANGE &&
3240 status != GOT_STATUS_MODIFY &&
3241 status != GOT_STATUS_CONFLICT &&
3242 status != GOT_STATUS_ADD &&
3243 status != GOT_STATUS_DELETE) {
3244 err = (*a->progress_cb)(a->progress_arg,
3248 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3250 err = got_object_blob_read_to_str(&link_target2,
3254 err = merge_symlink(a->worktree, NULL,
3255 ondisk_path, path2, a->label_orig,
3256 link_target2, a->commit_id2, repo,
3257 a->progress_cb, a->progress_arg);
3259 } else if (S_ISREG(sb.st_mode)) {
3260 err = merge_blob(&local_changes_subsumed,
3261 a->worktree, NULL, ondisk_path, path2,
3262 sb.st_mode, a->label_orig, blob2,
3263 a->commit_id2, repo, a->progress_cb,
3265 } else if (status != GOT_STATUS_DELETE) {
3266 err = got_error_path(ondisk_path,
3267 GOT_ERR_FILE_OBSTRUCTED);
3271 if (status == GOT_STATUS_DELETE) {
3272 /* Re-add file with content from new blob. */
3273 err = add_file(a->worktree, a->fileindex, ie,
3274 ondisk_path, path2, blob2, mode2,
3275 0, 0, 0, a->allow_bad_symlinks,
3276 repo, a->progress_cb, a->progress_arg);
3281 err = add_file(a->worktree, a->fileindex, NULL,
3282 ondisk_path, path2, blob2, mode2,
3283 0, 0, 1, a->allow_bad_symlinks,
3284 repo, a->progress_cb, a->progress_arg);
3290 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3291 err = got_error_from_errno("fclose");
3292 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3293 err = got_error_from_errno("fclose");
3294 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3295 err = got_error_from_errno("fclose");
3303 struct check_mixed_commits_args {
3304 struct got_worktree *worktree;
3305 got_cancel_cb cancel_cb;
3309 static const struct got_error *
3310 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3312 const struct got_error *err = NULL;
3313 struct check_mixed_commits_args *a = arg;
3316 err = a->cancel_cb(a->cancel_arg);
3321 /* Reject merges into a work tree with mixed base commits. */
3322 if (got_fileindex_entry_has_commit(ie) &&
3323 memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3324 SHA1_DIGEST_LENGTH) != 0)
3325 return got_error(GOT_ERR_MIXED_COMMITS);
3330 const struct got_error *
3331 got_worktree_get_state(char *state, struct got_repository *repo,
3332 struct got_worktree *worktree,
3333 got_cancel_cb cancel_cb, void *cancel_arg)
3335 const struct got_error *err;
3336 struct got_object_id *base_id, *head_id = NULL;
3337 struct got_reference *head_ref;
3338 struct got_fileindex *fileindex = NULL;
3339 char *fileindex_path = NULL;
3340 struct check_mixed_commits_args cma;
3342 if (worktree == NULL)
3343 return got_error(GOT_ERR_NOT_WORKTREE);
3345 err = got_ref_open(&head_ref, repo,
3346 got_worktree_get_head_ref_name(worktree), 0);
3350 err = got_ref_resolve(&head_id, repo, head_ref);
3354 *state = GOT_WORKTREE_STATE_UNKNOWN;
3355 base_id = got_worktree_get_base_commit_id(worktree);
3357 cma.worktree = worktree;
3358 cma.cancel_cb = cancel_cb;
3359 cma.cancel_arg = cancel_arg;
3361 if (got_object_id_cmp(base_id, head_id) == 0) {
3362 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3366 err = got_fileindex_for_each_entry_safe(fileindex,
3367 check_mixed_commits, &cma);
3369 *state = GOT_WORKTREE_STATE_UPTODATE;
3370 else if (err->code == GOT_ERR_MIXED_COMMITS) {
3371 *state = GOT_WORKTREE_STATE_OUTOFDATE;
3375 *state = GOT_WORKTREE_STATE_OUTOFDATE;
3379 free(fileindex_path);
3380 got_ref_close(head_ref);
3381 if (fileindex != NULL)
3382 got_fileindex_free(fileindex);
3386 struct check_merge_conflicts_arg {
3387 struct got_worktree *worktree;
3388 struct got_fileindex *fileindex;
3389 struct got_repository *repo;
3392 static const struct got_error *
3393 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3394 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3395 struct got_object_id *id1, struct got_object_id *id2,
3396 const char *path1, const char *path2,
3397 mode_t mode1, mode_t mode2, struct got_repository *repo)
3399 const struct got_error *err = NULL;
3400 struct check_merge_conflicts_arg *a = arg;
3401 unsigned char status;
3403 struct got_fileindex_entry *ie;
3404 const char *path = path2 ? path2 : path1;
3405 struct got_object_id *id = id2 ? id2 : id1;
3411 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3415 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3417 return got_error_from_errno("asprintf");
3419 /* Reject merges into a work tree with conflicted files. */
3420 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3424 if (status == GOT_STATUS_CONFLICT)
3425 return got_error(GOT_ERR_CONFLICTS);
3430 static const struct got_error *
3431 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3432 const char *fileindex_path, struct got_object_id *commit_id1,
3433 struct got_object_id *commit_id2, struct got_repository *repo,
3434 got_worktree_checkout_cb progress_cb, void *progress_arg,
3435 got_cancel_cb cancel_cb, void *cancel_arg)
3437 const struct got_error *err = NULL, *sync_err;
3438 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3439 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3440 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3441 struct check_merge_conflicts_arg cmc_arg;
3442 struct merge_file_cb_arg arg;
3443 char *label_orig = NULL;
3444 FILE *f1 = NULL, *f2 = NULL;
3445 int fd1 = -1, fd2 = -1;
3448 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3451 err = got_object_id_by_path(&tree_id1, repo, commit1,
3452 worktree->path_prefix);
3453 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3459 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3463 err = got_object_id_str(&id_str, commit_id1);
3467 if (asprintf(&label_orig, "%s: commit %s",
3468 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3469 err = got_error_from_errno("asprintf");
3475 f1 = got_opentemp();
3477 err = got_error_from_errno("got_opentemp");
3482 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3486 err = got_object_id_by_path(&tree_id2, repo, commit2,
3487 worktree->path_prefix);
3491 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3495 f2 = got_opentemp();
3497 err = got_error_from_errno("got_opentemp");
3501 fd1 = got_opentempfd();
3503 err = got_error_from_errno("got_opentempfd");
3507 fd2 = got_opentempfd();
3509 err = got_error_from_errno("got_opentempfd");
3513 cmc_arg.worktree = worktree;
3514 cmc_arg.fileindex = fileindex;
3515 cmc_arg.repo = repo;
3516 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3517 check_merge_conflicts, &cmc_arg, 0);
3521 arg.worktree = worktree;
3522 arg.fileindex = fileindex;
3523 arg.progress_cb = progress_cb;
3524 arg.progress_arg = progress_arg;
3525 arg.cancel_cb = cancel_cb;
3526 arg.cancel_arg = cancel_arg;
3527 arg.label_orig = label_orig;
3528 arg.commit_id2 = commit_id2;
3529 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3530 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3531 merge_file_cb, &arg, 1);
3532 sync_err = sync_fileindex(fileindex, fileindex_path);
3533 if (sync_err && err == NULL)
3537 got_object_commit_close(commit1);
3539 got_object_commit_close(commit2);
3541 got_object_tree_close(tree1);
3543 got_object_tree_close(tree2);
3544 if (f1 && fclose(f1) == EOF && err == NULL)
3545 err = got_error_from_errno("fclose");
3546 if (f2 && fclose(f2) == EOF && err == NULL)
3547 err = got_error_from_errno("fclose");
3548 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3549 err = got_error_from_errno("close");
3550 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3551 err = got_error_from_errno("close");
3556 const struct got_error *
3557 got_worktree_merge_files(struct got_worktree *worktree,
3558 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3559 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3560 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3562 const struct got_error *err, *unlockerr;
3563 char *fileindex_path = NULL;
3564 struct got_fileindex *fileindex = NULL;
3565 struct check_mixed_commits_args cma;
3567 err = lock_worktree(worktree, LOCK_EX);
3571 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3575 cma.worktree = worktree;
3576 cma.cancel_cb = cancel_cb;
3577 cma.cancel_arg = cancel_arg;
3579 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3584 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3585 commit_id2, repo, progress_cb, progress_arg,
3586 cancel_cb, cancel_arg);
3589 got_fileindex_free(fileindex);
3590 free(fileindex_path);
3591 unlockerr = lock_worktree(worktree, LOCK_SH);
3592 if (unlockerr && err == NULL)
3597 struct diff_dir_cb_arg {
3598 struct got_fileindex *fileindex;
3599 struct got_worktree *worktree;
3600 const char *status_path;
3601 size_t status_path_len;
3602 struct got_repository *repo;
3603 got_worktree_status_cb status_cb;
3605 got_cancel_cb cancel_cb;
3607 /* A pathlist containing per-directory pathlists of ignore patterns. */
3608 struct got_pathlist_head *ignores;
3609 int report_unchanged;
3613 static const struct got_error *
3614 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3615 int dirfd, const char *de_name,
3616 got_worktree_status_cb status_cb, void *status_arg,
3617 struct got_repository *repo, int report_unchanged)
3619 const struct got_error *err = NULL;
3620 unsigned char status = GOT_STATUS_NO_CHANGE;
3621 unsigned char staged_status;
3623 struct got_object_id blob_id, commit_id, staged_blob_id;
3624 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3625 struct got_object_id *staged_blob_idp = NULL;
3627 staged_status = get_staged_status(ie);
3628 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3632 if (status == GOT_STATUS_NO_CHANGE &&
3633 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3636 if (got_fileindex_entry_has_blob(ie))
3637 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3638 if (got_fileindex_entry_has_commit(ie))
3639 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3640 if (staged_status == GOT_STATUS_ADD ||
3641 staged_status == GOT_STATUS_MODIFY) {
3642 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3643 &staged_blob_id, ie);
3646 return (*status_cb)(status_arg, status, staged_status,
3647 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3650 static const struct got_error *
3651 status_old_new(void *arg, struct got_fileindex_entry *ie,
3652 struct dirent *de, const char *parent_path, int dirfd)
3654 const struct got_error *err = NULL;
3655 struct diff_dir_cb_arg *a = arg;
3659 err = a->cancel_cb(a->cancel_arg);
3664 if (got_path_cmp(parent_path, a->status_path,
3665 strlen(parent_path), a->status_path_len) != 0 &&
3666 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3669 if (parent_path[0]) {
3670 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3671 parent_path, de->d_name) == -1)
3672 return got_error_from_errno("asprintf");
3674 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3676 return got_error_from_errno("asprintf");
3679 err = report_file_status(ie, abspath, dirfd, de->d_name,
3680 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3685 static const struct got_error *
3686 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3688 const struct got_error *err;
3689 struct diff_dir_cb_arg *a = arg;
3690 struct got_object_id blob_id, commit_id;
3691 unsigned char status;
3694 err = a->cancel_cb(a->cancel_arg);
3699 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3702 got_fileindex_entry_get_blob_id(&blob_id, ie);
3703 got_fileindex_entry_get_commit_id(&commit_id, ie);
3704 if (got_fileindex_entry_has_file_on_disk(ie))
3705 status = GOT_STATUS_MISSING;
3707 status = GOT_STATUS_DELETE;
3708 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3709 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3713 free_ignores(struct got_pathlist_head *ignores)
3715 struct got_pathlist_entry *pe;
3717 TAILQ_FOREACH(pe, ignores, entry) {
3718 struct got_pathlist_head *ignorelist = pe->data;
3720 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3722 got_pathlist_free(ignores, GOT_PATHLIST_FREE_ALL);
3725 static const struct got_error *
3726 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3728 const struct got_error *err = NULL;
3729 struct got_pathlist_entry *pe = NULL;
3730 struct got_pathlist_head *ignorelist;
3731 char *line = NULL, *pattern, *dirpath = NULL;
3732 size_t linesize = 0;
3735 ignorelist = calloc(1, sizeof(*ignorelist));
3736 if (ignorelist == NULL)
3737 return got_error_from_errno("calloc");
3738 TAILQ_INIT(ignorelist);
3740 while ((linelen = getline(&line, &linesize, f)) != -1) {
3741 if (linelen > 0 && line[linelen - 1] == '\n')
3742 line[linelen - 1] = '\0';
3744 /* Git's ignores may contain comments. */
3748 /* Git's negated patterns are not (yet?) supported. */
3752 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3754 err = got_error_from_errno("asprintf");
3757 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3762 err = got_error_from_errno("getline");
3766 dirpath = strdup(path);
3767 if (dirpath == NULL) {
3768 err = got_error_from_errno("strdup");
3771 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3774 if (err || pe == NULL) {
3776 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3783 match_path(const char *pattern, size_t pattern_len, const char *path,
3789 * Trailing slashes signify directories.
3790 * Append a * to make such patterns conform to fnmatch rules.
3792 if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
3793 if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
3794 return FNM_NOMATCH; /* XXX */
3796 return fnmatch(buf, path, flags);
3799 return fnmatch(pattern, path, flags);
3803 match_ignores(struct got_pathlist_head *ignores, const char *path)
3805 struct got_pathlist_entry *pe;
3807 /* Handle patterns which match in all directories. */
3808 TAILQ_FOREACH(pe, ignores, entry) {
3809 struct got_pathlist_head *ignorelist = pe->data;
3810 struct got_pathlist_entry *pi;
3812 TAILQ_FOREACH(pi, ignorelist, entry) {
3815 if (pi->path_len < 3 ||
3816 strncmp(pi->path, "**/", 3) != 0)
3820 if (match_path(pi->path + 3,
3821 pi->path_len - 3, p,
3822 FNM_PATHNAME | FNM_LEADING_DIR)) {
3823 /* Retry in next directory. */
3824 while (*p && *p != '/')
3836 * The ignores pathlist contains ignore lists from children before
3837 * parents, so we can find the most specific ignorelist by walking
3838 * ignores backwards.
3840 pe = TAILQ_LAST(ignores, got_pathlist_head);
3842 if (got_path_is_child(path, pe->path, pe->path_len)) {
3843 struct got_pathlist_head *ignorelist = pe->data;
3844 struct got_pathlist_entry *pi;
3845 TAILQ_FOREACH(pi, ignorelist, entry) {
3846 int flags = FNM_LEADING_DIR;
3847 if (strstr(pi->path, "/**/") == NULL)
3848 flags |= FNM_PATHNAME;
3849 if (match_path(pi->path, pi->path_len,
3855 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3861 static const struct got_error *
3862 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3863 const char *path, int dirfd, const char *ignores_filename)
3865 const struct got_error *err = NULL;
3868 FILE *ignoresfile = NULL;
3870 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3871 path[0] ? "/" : "", ignores_filename) == -1)
3872 return got_error_from_errno("asprintf");
3875 fd = openat(dirfd, ignores_filename,
3876 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3878 if (errno != ENOENT && errno != EACCES)
3879 err = got_error_from_errno2("openat",
3882 ignoresfile = fdopen(fd, "r");
3883 if (ignoresfile == NULL)
3884 err = got_error_from_errno2("fdopen",
3888 err = read_ignores(ignores, path, ignoresfile);
3892 ignoresfile = fopen(ignorespath, "re");
3893 if (ignoresfile == NULL) {
3894 if (errno != ENOENT && errno != EACCES)
3895 err = got_error_from_errno2("fopen",
3898 err = read_ignores(ignores, path, ignoresfile);
3901 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3902 err = got_error_from_errno2("fclose", path);
3903 if (fd != -1 && close(fd) == -1 && err == NULL)
3904 err = got_error_from_errno2("close", path);
3909 static const struct got_error *
3910 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3913 const struct got_error *err = NULL;
3914 struct diff_dir_cb_arg *a = arg;
3921 err = a->cancel_cb(a->cancel_arg);
3926 if (parent_path[0]) {
3927 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3928 return got_error_from_errno("asprintf");
3933 if (de->d_type == DT_DIR) {
3934 if (!a->no_ignores && ignore != NULL &&
3935 match_ignores(a->ignores, path))
3937 } else if (!match_ignores(a->ignores, path) &&
3938 got_path_is_child(path, a->status_path, a->status_path_len))
3939 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3940 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3946 static const struct got_error *
3947 status_traverse(void *arg, const char *path, int dirfd)
3949 const struct got_error *err = NULL;
3950 struct diff_dir_cb_arg *a = arg;
3955 err = add_ignores(a->ignores, a->worktree->root_path,
3956 path, dirfd, ".cvsignore");
3960 err = add_ignores(a->ignores, a->worktree->root_path, path,
3961 dirfd, ".gitignore");
3966 static const struct got_error *
3967 report_single_file_status(const char *path, const char *ondisk_path,
3968 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3969 void *status_arg, struct got_repository *repo, int report_unchanged,
3970 struct got_pathlist_head *ignores, int no_ignores)
3972 struct got_fileindex_entry *ie;
3975 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3977 return report_file_status(ie, ondisk_path, -1, NULL,
3978 status_cb, status_arg, repo, report_unchanged);
3980 if (lstat(ondisk_path, &sb) == -1) {
3981 if (errno != ENOENT)
3982 return got_error_from_errno2("lstat", ondisk_path);
3983 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3984 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3987 if (!no_ignores && match_ignores(ignores, path))
3990 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3991 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3992 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3997 static const struct got_error *
3998 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3999 const char *root_path, const char *path)
4001 const struct got_error *err;
4002 char *parent_path, *next_parent_path = NULL;
4004 err = add_ignores(ignores, root_path, "", -1,
4009 err = add_ignores(ignores, root_path, "", -1,
4014 err = got_path_dirname(&parent_path, path);
4016 if (err->code == GOT_ERR_BAD_PATH)
4017 return NULL; /* cannot traverse parent */
4021 err = add_ignores(ignores, root_path, parent_path, -1,
4025 err = add_ignores(ignores, root_path, parent_path, -1,
4029 err = got_path_dirname(&next_parent_path, parent_path);
4031 if (err->code == GOT_ERR_BAD_PATH)
4032 err = NULL; /* traversed everything */
4035 if (got_path_is_root_dir(parent_path))
4038 parent_path = next_parent_path;
4039 next_parent_path = NULL;
4043 free(next_parent_path);
4047 struct find_missing_children_args {
4048 const char *parent_path;
4050 struct got_pathlist_head *children;
4051 got_cancel_cb cancel_cb;
4055 static const struct got_error *
4056 find_missing_children(void *arg, struct got_fileindex_entry *ie)
4058 const struct got_error *err = NULL;
4059 struct find_missing_children_args *a = arg;
4062 err = a->cancel_cb(a->cancel_arg);
4067 if (got_path_is_child(ie->path, a->parent_path, a->parent_len))
4068 err = got_pathlist_append(a->children, ie->path, NULL);
4073 static const struct got_error *
4074 report_children(struct got_pathlist_head *children,
4075 struct got_worktree *worktree, struct got_fileindex *fileindex,
4076 struct got_repository *repo, int is_root_dir, int report_unchanged,
4077 struct got_pathlist_head *ignores, int no_ignores,
4078 got_worktree_status_cb status_cb, void *status_arg,
4079 got_cancel_cb cancel_cb, void *cancel_arg)
4081 const struct got_error *err = NULL;
4082 struct got_pathlist_entry *pe;
4083 char *ondisk_path = NULL;
4085 TAILQ_FOREACH(pe, children, entry) {
4087 err = cancel_cb(cancel_arg);
4092 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
4093 !is_root_dir ? "/" : "", pe->path) == -1) {
4094 err = got_error_from_errno("asprintf");
4099 err = report_single_file_status(pe->path, ondisk_path,
4100 fileindex, status_cb, status_arg, repo, report_unchanged,
4101 ignores, no_ignores);
4113 static const struct got_error *
4114 worktree_status(struct got_worktree *worktree, const char *path,
4115 struct got_fileindex *fileindex, struct got_repository *repo,
4116 got_worktree_status_cb status_cb, void *status_arg,
4117 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
4118 int report_unchanged)
4120 const struct got_error *err = NULL;
4122 struct got_fileindex_diff_dir_cb fdiff_cb;
4123 struct diff_dir_cb_arg arg;
4124 char *ondisk_path = NULL;
4125 struct got_pathlist_head ignores, missing_children;
4126 struct got_fileindex_entry *ie;
4128 TAILQ_INIT(&ignores);
4129 TAILQ_INIT(&missing_children);
4131 if (asprintf(&ondisk_path, "%s%s%s",
4132 worktree->root_path, path[0] ? "/" : "", path) == -1)
4133 return got_error_from_errno("asprintf");
4135 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
4137 err = report_single_file_status(path, ondisk_path,
4138 fileindex, status_cb, status_arg, repo,
4139 report_unchanged, &ignores, no_ignores);
4142 struct find_missing_children_args fmca;
4143 fmca.parent_path = path;
4144 fmca.parent_len = strlen(path);
4145 fmca.children = &missing_children;
4146 fmca.cancel_cb = cancel_cb;
4147 fmca.cancel_arg = cancel_arg;
4148 err = got_fileindex_for_each_entry_safe(fileindex,
4149 find_missing_children, &fmca);
4154 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
4156 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
4157 !got_err_open_nofollow_on_symlink())
4158 err = got_error_from_errno2("open", ondisk_path);
4161 err = add_ignores_from_parent_paths(&ignores,
4162 worktree->root_path, ondisk_path);
4166 if (TAILQ_EMPTY(&missing_children)) {
4167 err = report_single_file_status(path,
4168 ondisk_path, fileindex,
4169 status_cb, status_arg, repo,
4170 report_unchanged, &ignores, no_ignores);
4174 err = report_children(&missing_children,
4175 worktree, fileindex, repo,
4176 (path[0] == '\0'), report_unchanged,
4177 &ignores, no_ignores,
4178 status_cb, status_arg,
4179 cancel_cb, cancel_arg);
4185 fdiff_cb.diff_old_new = status_old_new;
4186 fdiff_cb.diff_old = status_old;
4187 fdiff_cb.diff_new = status_new;
4188 fdiff_cb.diff_traverse = status_traverse;
4189 arg.fileindex = fileindex;
4190 arg.worktree = worktree;
4191 arg.status_path = path;
4192 arg.status_path_len = strlen(path);
4194 arg.status_cb = status_cb;
4195 arg.status_arg = status_arg;
4196 arg.cancel_cb = cancel_cb;
4197 arg.cancel_arg = cancel_arg;
4198 arg.report_unchanged = report_unchanged;
4199 arg.no_ignores = no_ignores;
4201 err = add_ignores_from_parent_paths(&ignores,
4202 worktree->root_path, path);
4206 arg.ignores = &ignores;
4207 err = got_fileindex_diff_dir(fileindex, fd,
4208 worktree->root_path, path, repo, &fdiff_cb, &arg);
4211 free_ignores(&ignores);
4212 got_pathlist_free(&missing_children, GOT_PATHLIST_FREE_NONE);
4213 if (fd != -1 && close(fd) == -1 && err == NULL)
4214 err = got_error_from_errno("close");
4219 const struct got_error *
4220 got_worktree_status(struct got_worktree *worktree,
4221 struct got_pathlist_head *paths, struct got_repository *repo,
4222 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
4223 got_cancel_cb cancel_cb, void *cancel_arg)
4225 const struct got_error *err = NULL;
4226 char *fileindex_path = NULL;
4227 struct got_fileindex *fileindex = NULL;
4228 struct got_pathlist_entry *pe;
4230 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4234 TAILQ_FOREACH(pe, paths, entry) {
4235 err = worktree_status(worktree, pe->path, fileindex, repo,
4236 status_cb, status_arg, cancel_cb, cancel_arg,
4241 free(fileindex_path);
4242 got_fileindex_free(fileindex);
4246 const struct got_error *
4247 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
4250 const struct got_error *err = NULL;
4251 char *resolved = NULL, *cwd = NULL, *path = NULL;
4254 char *abspath = NULL;
4255 char canonpath[PATH_MAX];
4259 cwd = getcwd(NULL, 0);
4261 return got_error_from_errno("getcwd");
4263 if (lstat(arg, &sb) == -1) {
4264 if (errno != ENOENT) {
4265 err = got_error_from_errno2("lstat", arg);
4270 if (S_ISLNK(sb.st_mode)) {
4272 * We cannot use realpath(3) with symlinks since we want to
4273 * operate on the symlink itself.
4274 * But we can make the path absolute, assuming it is relative
4275 * to the current working directory, and then canonicalize it.
4277 if (!got_path_is_absolute(arg)) {
4278 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4279 err = got_error_from_errno("asprintf");
4284 err = got_canonpath(abspath ? abspath : arg, canonpath,
4288 resolved = strdup(canonpath);
4289 if (resolved == NULL) {
4290 err = got_error_from_errno("strdup");
4294 resolved = realpath(arg, NULL);
4295 if (resolved == NULL) {
4296 if (errno != ENOENT) {
4297 err = got_error_from_errno2("realpath", arg);
4300 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4301 err = got_error_from_errno("asprintf");
4304 err = got_canonpath(abspath, canonpath,
4308 resolved = strdup(canonpath);
4309 if (resolved == NULL) {
4310 err = got_error_from_errno("strdup");
4316 if (strncmp(got_worktree_get_root_path(worktree), resolved,
4317 strlen(got_worktree_get_root_path(worktree)))) {
4318 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
4322 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
4323 err = got_path_skip_common_ancestor(&path,
4324 got_worktree_get_root_path(worktree), resolved);
4330 err = got_error_from_errno("strdup");
4335 /* XXX status walk can't deal with trailing slash! */
4337 while (len > 0 && path[len - 1] == '/') {
4338 path[len - 1] = '\0';
4352 struct schedule_addition_args {
4353 struct got_worktree *worktree;
4354 struct got_fileindex *fileindex;
4355 got_worktree_checkout_cb progress_cb;
4357 struct got_repository *repo;
4361 add_noop_status(unsigned char status)
4363 return (status == GOT_STATUS_ADD ||
4364 status == GOT_STATUS_MODIFY ||
4365 status == GOT_STATUS_CONFLICT ||
4366 status == GOT_STATUS_MODE_CHANGE ||
4367 status == GOT_STATUS_NO_CHANGE);
4370 static const struct got_error *
4371 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4372 const char *relpath, struct got_object_id *blob_id,
4373 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4374 int dirfd, const char *de_name)
4376 struct schedule_addition_args *a = arg;
4377 const struct got_error *err = NULL;
4378 struct got_fileindex_entry *ie;
4382 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4384 return got_error_from_errno("asprintf");
4386 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4388 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4392 /* Re-adding an existing entry is a no-op. */
4393 if (staged_status == GOT_STATUS_NO_CHANGE &&
4394 add_noop_status(status))
4396 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4401 if (status != GOT_STATUS_UNVERSIONED) {
4402 if (status == GOT_STATUS_NONEXISTENT)
4403 err = got_error_set_errno(ENOENT, ondisk_path);
4405 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4409 err = got_fileindex_entry_alloc(&ie, relpath);
4412 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4413 relpath, NULL, NULL, 1);
4415 got_fileindex_entry_free(ie);
4418 err = got_fileindex_entry_add(a->fileindex, ie);
4420 got_fileindex_entry_free(ie);
4427 if (staged_status == GOT_STATUS_NO_CHANGE && add_noop_status(status))
4429 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4432 const struct got_error *
4433 got_worktree_schedule_add(struct got_worktree *worktree,
4434 struct got_pathlist_head *paths,
4435 got_worktree_checkout_cb progress_cb, void *progress_arg,
4436 struct got_repository *repo, int no_ignores)
4438 struct got_fileindex *fileindex = NULL;
4439 char *fileindex_path = NULL;
4440 const struct got_error *err = NULL, *sync_err, *unlockerr;
4441 struct got_pathlist_entry *pe;
4442 struct schedule_addition_args saa;
4444 err = lock_worktree(worktree, LOCK_EX);
4448 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4452 saa.worktree = worktree;
4453 saa.fileindex = fileindex;
4454 saa.progress_cb = progress_cb;
4455 saa.progress_arg = progress_arg;
4458 TAILQ_FOREACH(pe, paths, entry) {
4459 err = worktree_status(worktree, pe->path, fileindex, repo,
4460 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4464 sync_err = sync_fileindex(fileindex, fileindex_path);
4465 if (sync_err && err == NULL)
4468 free(fileindex_path);
4470 got_fileindex_free(fileindex);
4471 unlockerr = lock_worktree(worktree, LOCK_SH);
4472 if (unlockerr && err == NULL)
4477 struct schedule_deletion_args {
4478 struct got_worktree *worktree;
4479 struct got_fileindex *fileindex;
4480 got_worktree_delete_cb progress_cb;
4482 struct got_repository *repo;
4483 int delete_local_mods;
4485 int ignore_missing_paths;
4486 const char *status_path;
4487 size_t status_path_len;
4488 const char *status_codes;
4491 static const struct got_error *
4492 schedule_for_deletion(void *arg, unsigned char status,
4493 unsigned char staged_status, const char *relpath,
4494 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4495 struct got_object_id *commit_id, int dirfd, const char *de_name)
4497 struct schedule_deletion_args *a = arg;
4498 const struct got_error *err = NULL;
4499 struct got_fileindex_entry *ie = NULL;
4503 if (status == GOT_STATUS_NONEXISTENT) {
4504 if (a->ignore_missing_paths)
4506 return got_error_set_errno(ENOENT, relpath);
4509 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4511 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4513 staged_status = get_staged_status(ie);
4514 if (staged_status != GOT_STATUS_NO_CHANGE) {
4515 if (staged_status == GOT_STATUS_DELETE)
4517 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4520 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4522 return got_error_from_errno("asprintf");
4524 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4529 if (a->status_codes) {
4530 size_t ncodes = strlen(a->status_codes);
4532 for (i = 0; i < ncodes ; i++) {
4533 if (status == a->status_codes[i])
4537 /* Do not delete files in non-matching status. */
4541 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4542 a->status_codes[i] != GOT_STATUS_MISSING) {
4543 static char msg[64];
4544 snprintf(msg, sizeof(msg),
4545 "invalid status code '%c'", a->status_codes[i]);
4546 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4551 if (status != GOT_STATUS_NO_CHANGE) {
4552 if (status == GOT_STATUS_DELETE)
4554 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4555 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4558 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4559 err = got_error_set_errno(ENOENT, relpath);
4562 if (status != GOT_STATUS_MODIFY &&
4563 status != GOT_STATUS_MISSING) {
4564 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4569 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4573 if (unlinkat(dirfd, de_name, 0) == -1) {
4574 err = got_error_from_errno2("unlinkat",
4578 } else if (unlink(ondisk_path) == -1) {
4579 err = got_error_from_errno2("unlink", ondisk_path);
4583 root_len = strlen(a->worktree->root_path);
4587 err = got_path_dirname(&parent, ondisk_path);
4591 ondisk_path = parent;
4592 if (got_path_cmp(ondisk_path, a->status_path,
4593 strlen(ondisk_path), a->status_path_len) != 0 &&
4594 !got_path_is_child(ondisk_path, a->status_path,
4595 a->status_path_len))
4597 if (rmdir(ondisk_path) == -1) {
4598 if (errno != ENOTEMPTY)
4599 err = got_error_from_errno2("rmdir",
4603 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4604 strlen(ondisk_path), root_len) != 0);
4607 if (got_fileindex_entry_has_blob(ie))
4608 got_fileindex_entry_mark_deleted_from_disk(ie);
4610 got_fileindex_entry_remove(a->fileindex, ie);
4615 if (status == GOT_STATUS_DELETE)
4617 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4618 staged_status, relpath);
4621 const struct got_error *
4622 got_worktree_schedule_delete(struct got_worktree *worktree,
4623 struct got_pathlist_head *paths, int delete_local_mods,
4624 const char *status_codes,
4625 got_worktree_delete_cb progress_cb, void *progress_arg,
4626 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4628 struct got_fileindex *fileindex = NULL;
4629 char *fileindex_path = NULL;
4630 const struct got_error *err = NULL, *sync_err, *unlockerr;
4631 struct got_pathlist_entry *pe;
4632 struct schedule_deletion_args sda;
4634 err = lock_worktree(worktree, LOCK_EX);
4638 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4642 sda.worktree = worktree;
4643 sda.fileindex = fileindex;
4644 sda.progress_cb = progress_cb;
4645 sda.progress_arg = progress_arg;
4647 sda.delete_local_mods = delete_local_mods;
4648 sda.keep_on_disk = keep_on_disk;
4649 sda.ignore_missing_paths = ignore_missing_paths;
4650 sda.status_codes = status_codes;
4652 TAILQ_FOREACH(pe, paths, entry) {
4653 char *ondisk_status_path;
4655 if (asprintf(&ondisk_status_path, "%s%s%s",
4656 got_worktree_get_root_path(worktree),
4657 pe->path[0] == '\0' ? "" : "/", pe->path) == -1) {
4658 err = got_error_from_errno("asprintf");
4661 sda.status_path = ondisk_status_path;
4662 sda.status_path_len = strlen(ondisk_status_path);
4663 err = worktree_status(worktree, pe->path, fileindex, repo,
4664 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4665 free(ondisk_status_path);
4669 sync_err = sync_fileindex(fileindex, fileindex_path);
4670 if (sync_err && err == NULL)
4673 free(fileindex_path);
4675 got_fileindex_free(fileindex);
4676 unlockerr = lock_worktree(worktree, LOCK_SH);
4677 if (unlockerr && err == NULL)
4682 static const struct got_error *
4683 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4685 const struct got_error *err = NULL;
4687 size_t linesize = 0, n;
4690 linelen = getline(&line, &linesize, infile);
4691 if (linelen == -1) {
4692 if (ferror(infile)) {
4693 err = got_error_from_errno("getline");
4699 n = fwrite(line, 1, linelen, outfile);
4701 err = got_ferror(outfile, GOT_ERR_IO);
4706 n = fwrite(line, 1, linelen, rejectfile);
4708 err = got_ferror(rejectfile, GOT_ERR_IO);
4715 static const struct got_error *
4716 skip_one_line(FILE *f)
4719 size_t linesize = 0;
4722 linelen = getline(&line, &linesize, f);
4723 if (linelen == -1) {
4725 return got_error_from_errno("getline");
4732 static const struct got_error *
4733 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4734 int start_old, int end_old, int start_new, int end_new,
4735 FILE *outfile, FILE *rejectfile)
4737 const struct got_error *err;
4739 /* Copy old file's lines leading up to patch. */
4740 while (!feof(f1) && *line_cur1 < start_old) {
4741 err = copy_one_line(f1, outfile, NULL);
4746 /* Skip new file's lines leading up to patch. */
4747 while (!feof(f2) && *line_cur2 < start_new) {
4749 err = copy_one_line(f2, NULL, rejectfile);
4751 err = skip_one_line(f2);
4756 /* Copy patched lines. */
4757 while (!feof(f2) && *line_cur2 <= end_new) {
4758 err = copy_one_line(f2, outfile, NULL);
4763 /* Skip over old file's replaced lines. */
4764 while (!feof(f1) && *line_cur1 <= end_old) {
4766 err = copy_one_line(f1, NULL, rejectfile);
4768 err = skip_one_line(f1);
4777 static const struct got_error *
4778 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4779 FILE *outfile, FILE *rejectfile)
4781 const struct got_error *err;
4784 /* Copy old file's lines until EOF. */
4786 err = copy_one_line(f1, outfile, NULL);
4793 /* Copy new file's lines until EOF. */
4795 err = copy_one_line(f2, NULL, rejectfile);
4805 static const struct got_error *
4806 apply_or_reject_change(int *choice, int *nchunks_used,
4807 struct diff_result *diff_result, int n,
4808 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4809 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4810 got_worktree_patch_cb patch_cb, void *patch_arg)
4812 const struct got_error *err = NULL;
4813 struct diff_chunk_context cc = {};
4814 int start_old, end_old, start_new, end_new;
4816 struct diff_output_unidiff_state *diff_state;
4817 struct diff_input_info diff_info;
4820 *choice = GOT_PATCH_CHOICE_NONE;
4822 /* Get changed line numbers without context lines for copy_change(). */
4823 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4824 start_old = cc.left.start;
4825 end_old = cc.left.end;
4826 start_new = cc.right.start;
4827 end_new = cc.right.end;
4829 /* Get the same change with context lines for display. */
4830 memset(&cc, 0, sizeof(cc));
4831 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4833 memset(&diff_info, 0, sizeof(diff_info));
4834 diff_info.left_path = relpath;
4835 diff_info.right_path = relpath;
4837 diff_state = diff_output_unidiff_state_alloc();
4838 if (diff_state == NULL)
4839 return got_error_set_errno(ENOMEM,
4840 "diff_output_unidiff_state_alloc");
4842 hunkfile = got_opentemp();
4843 if (hunkfile == NULL) {
4844 err = got_error_from_errno("got_opentemp");
4848 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4850 if (rc != DIFF_RC_OK) {
4851 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4855 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4856 err = got_ferror(hunkfile, GOT_ERR_IO);
4860 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4861 hunkfile, changeno, nchanges);
4866 case GOT_PATCH_CHOICE_YES:
4867 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4868 end_old, start_new, end_new, outfile, rejectfile);
4870 case GOT_PATCH_CHOICE_NO:
4871 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4872 end_old, start_new, end_new, rejectfile, outfile);
4874 case GOT_PATCH_CHOICE_QUIT:
4877 err = got_error(GOT_ERR_PATCH_CHOICE);
4881 diff_output_unidiff_state_free(diff_state);
4882 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4883 err = got_error_from_errno("fclose");
4887 struct revert_file_args {
4888 struct got_worktree *worktree;
4889 struct got_fileindex *fileindex;
4890 got_worktree_checkout_cb progress_cb;
4892 got_worktree_patch_cb patch_cb;
4894 struct got_repository *repo;
4895 int unlink_added_files;
4896 struct got_pathlist_head *added_files_to_unlink;
4899 static const struct got_error *
4900 create_patched_content(char **path_outfile, int reverse_patch,
4901 struct got_object_id *blob_id, const char *path2,
4902 int dirfd2, const char *de_name2,
4903 const char *relpath, struct got_repository *repo,
4904 got_worktree_patch_cb patch_cb, void *patch_arg)
4906 const struct got_error *err, *free_err;
4907 struct got_blob_object *blob = NULL;
4908 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4909 int fd = -1, fd2 = -1;
4910 char link_target[PATH_MAX];
4911 ssize_t link_len = 0;
4912 char *path1 = NULL, *id_str = NULL;
4914 struct got_diffreg_result *diffreg_result = NULL;
4915 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4916 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4918 *path_outfile = NULL;
4920 err = got_object_id_str(&id_str, blob_id);
4925 fd2 = openat(dirfd2, de_name2,
4926 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4928 if (!got_err_open_nofollow_on_symlink()) {
4929 err = got_error_from_errno2("openat", path2);
4932 link_len = readlinkat(dirfd2, de_name2,
4933 link_target, sizeof(link_target));
4934 if (link_len == -1) {
4935 return got_error_from_errno2("readlinkat",
4938 sb2.st_mode = S_IFLNK;
4939 sb2.st_size = link_len;
4942 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4944 if (!got_err_open_nofollow_on_symlink()) {
4945 err = got_error_from_errno2("open", path2);
4948 link_len = readlink(path2, link_target,
4949 sizeof(link_target));
4951 return got_error_from_errno2("readlink", path2);
4952 sb2.st_mode = S_IFLNK;
4953 sb2.st_size = link_len;
4957 if (fstat(fd2, &sb2) == -1) {
4958 err = got_error_from_errno2("fstat", path2);
4962 f2 = fdopen(fd2, "r");
4964 err = got_error_from_errno2("fdopen", path2);
4970 f2 = got_opentemp();
4972 err = got_error_from_errno2("got_opentemp", path2);
4975 n = fwrite(link_target, 1, link_len, f2);
4976 if (n != link_len) {
4977 err = got_ferror(f2, GOT_ERR_IO);
4980 if (fflush(f2) == EOF) {
4981 err = got_error_from_errno("fflush");
4987 fd = got_opentempfd();
4989 err = got_error_from_errno("got_opentempfd");
4993 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4997 err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
5001 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
5005 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
5006 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
5010 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
5015 if (fseek(f1, 0L, SEEK_SET) == -1)
5016 return got_ferror(f1, GOT_ERR_IO);
5017 if (fseek(f2, 0L, SEEK_SET) == -1)
5018 return got_ferror(f2, GOT_ERR_IO);
5020 /* Count the number of actual changes in the diff result. */
5021 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
5022 struct diff_chunk_context cc = {};
5023 diff_chunk_context_load_change(&cc, &nchunks_used,
5024 diffreg_result->result, n, 0);
5027 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
5029 err = apply_or_reject_change(&choice, &nchunks_used,
5030 diffreg_result->result, n, relpath, f1, f2,
5031 &line_cur1, &line_cur2,
5032 reverse_patch ? NULL : outfile,
5033 reverse_patch ? outfile : NULL,
5034 ++i, nchanges, patch_cb, patch_arg);
5037 if (choice == GOT_PATCH_CHOICE_YES)
5039 else if (choice == GOT_PATCH_CHOICE_QUIT)
5043 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
5044 reverse_patch ? NULL : outfile,
5045 reverse_patch ? outfile : NULL);
5049 if (!S_ISLNK(sb2.st_mode)) {
5052 mode = apply_umask(sb2.st_mode);
5053 if (fchmod(fileno(outfile), mode) == -1) {
5054 err = got_error_from_errno2("fchmod", path2);
5061 if (fd != -1 && close(fd) == -1 && err == NULL)
5062 err = got_error_from_errno("close");
5064 got_object_blob_close(blob);
5065 free_err = got_diffreg_result_free(diffreg_result);
5068 if (f1 && fclose(f1) == EOF && err == NULL)
5069 err = got_error_from_errno2("fclose", path1);
5070 if (f2 && fclose(f2) == EOF && err == NULL)
5071 err = got_error_from_errno2("fclose", path2);
5072 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5073 err = got_error_from_errno2("close", path2);
5074 if (outfile && fclose(outfile) == EOF && err == NULL)
5075 err = got_error_from_errno2("fclose", *path_outfile);
5076 if (path1 && unlink(path1) == -1 && err == NULL)
5077 err = got_error_from_errno2("unlink", path1);
5078 if (err || !have_content) {
5079 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
5080 err = got_error_from_errno2("unlink", *path_outfile);
5081 free(*path_outfile);
5082 *path_outfile = NULL;
5088 static const struct got_error *
5089 revert_file(void *arg, unsigned char status, unsigned char staged_status,
5090 const char *relpath, struct got_object_id *blob_id,
5091 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5092 int dirfd, const char *de_name)
5094 struct revert_file_args *a = arg;
5095 const struct got_error *err = NULL;
5096 char *parent_path = NULL;
5097 struct got_fileindex_entry *ie;
5098 struct got_commit_object *base_commit = NULL;
5099 struct got_tree_object *tree = NULL;
5100 struct got_object_id *tree_id = NULL;
5101 const struct got_tree_entry *te = NULL;
5102 char *tree_path = NULL, *te_name;
5103 char *ondisk_path = NULL, *path_content = NULL;
5104 struct got_blob_object *blob = NULL;
5107 /* Reverting a staged deletion is a no-op. */
5108 if (status == GOT_STATUS_DELETE &&
5109 staged_status != GOT_STATUS_NO_CHANGE)
5112 if (status == GOT_STATUS_UNVERSIONED)
5113 return (*a->progress_cb)(a->progress_arg,
5114 GOT_STATUS_UNVERSIONED, relpath);
5116 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5118 return got_error_path(relpath, GOT_ERR_BAD_PATH);
5120 /* Construct in-repository path of tree which contains this blob. */
5121 err = got_path_dirname(&parent_path, ie->path);
5123 if (err->code != GOT_ERR_BAD_PATH)
5125 parent_path = strdup("/");
5126 if (parent_path == NULL) {
5127 err = got_error_from_errno("strdup");
5131 if (got_path_is_root_dir(a->worktree->path_prefix)) {
5132 tree_path = strdup(parent_path);
5133 if (tree_path == NULL) {
5134 err = got_error_from_errno("strdup");
5138 if (got_path_is_root_dir(parent_path)) {
5139 tree_path = strdup(a->worktree->path_prefix);
5140 if (tree_path == NULL) {
5141 err = got_error_from_errno("strdup");
5145 if (asprintf(&tree_path, "%s/%s",
5146 a->worktree->path_prefix, parent_path) == -1) {
5147 err = got_error_from_errno("asprintf");
5153 err = got_object_open_as_commit(&base_commit, a->repo,
5154 a->worktree->base_commit_id);
5158 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
5160 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
5161 (status == GOT_STATUS_ADD ||
5162 staged_status == GOT_STATUS_ADD)))
5165 err = got_object_open_as_tree(&tree, a->repo, tree_id);
5169 err = got_path_basename(&te_name, ie->path);
5173 te = got_object_tree_find_entry(tree, te_name);
5175 if (te == NULL && status != GOT_STATUS_ADD &&
5176 staged_status != GOT_STATUS_ADD) {
5177 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
5183 case GOT_STATUS_ADD:
5185 int choice = GOT_PATCH_CHOICE_NONE;
5186 err = (*a->patch_cb)(&choice, a->patch_arg,
5187 status, ie->path, NULL, 1, 1);
5190 if (choice != GOT_PATCH_CHOICE_YES)
5193 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
5197 got_fileindex_entry_remove(a->fileindex, ie);
5198 if (a->unlink_added_files) {
5199 int do_unlink = a->added_files_to_unlink ? 0 : 1;
5201 if (a->added_files_to_unlink) {
5202 struct got_pathlist_entry *pe;
5204 TAILQ_FOREACH(pe, a->added_files_to_unlink,
5206 if (got_path_cmp(pe->path, relpath,
5207 pe->path_len, strlen(relpath)))
5215 if (asprintf(&ondisk_path, "%s/%s",
5216 got_worktree_get_root_path(a->worktree),
5218 err = got_error_from_errno("asprintf");
5221 if (unlink(ondisk_path) == -1) {
5222 err = got_error_from_errno2("unlink",
5229 case GOT_STATUS_DELETE:
5231 int choice = GOT_PATCH_CHOICE_NONE;
5232 err = (*a->patch_cb)(&choice, a->patch_arg,
5233 status, ie->path, NULL, 1, 1);
5236 if (choice != GOT_PATCH_CHOICE_YES)
5240 case GOT_STATUS_MODIFY:
5241 case GOT_STATUS_MODE_CHANGE:
5242 case GOT_STATUS_CONFLICT:
5243 case GOT_STATUS_MISSING: {
5244 struct got_object_id id;
5245 if (staged_status == GOT_STATUS_ADD ||
5246 staged_status == GOT_STATUS_MODIFY)
5247 got_fileindex_entry_get_staged_blob_id(&id, ie);
5249 got_fileindex_entry_get_blob_id(&id, ie);
5250 fd = got_opentempfd();
5252 err = got_error_from_errno("got_opentempfd");
5256 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
5260 if (asprintf(&ondisk_path, "%s/%s",
5261 got_worktree_get_root_path(a->worktree), relpath) == -1) {
5262 err = got_error_from_errno("asprintf");
5266 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
5267 status == GOT_STATUS_CONFLICT)) {
5268 int is_bad_symlink = 0;
5269 err = create_patched_content(&path_content, 1, &id,
5270 ondisk_path, dirfd, de_name, ie->path, a->repo,
5271 a->patch_cb, a->patch_arg);
5272 if (err || path_content == NULL)
5274 if (te && S_ISLNK(te->mode)) {
5275 if (unlink(path_content) == -1) {
5276 err = got_error_from_errno2("unlink",
5280 err = install_symlink(&is_bad_symlink,
5281 a->worktree, ondisk_path, ie->path,
5282 blob, 0, 1, 0, 0, a->repo,
5283 a->progress_cb, a->progress_arg);
5285 if (rename(path_content, ondisk_path) == -1) {
5286 err = got_error_from_errno3("rename",
5287 path_content, ondisk_path);
5292 int is_bad_symlink = 0;
5293 if (te && S_ISLNK(te->mode)) {
5294 err = install_symlink(&is_bad_symlink,
5295 a->worktree, ondisk_path, ie->path,
5296 blob, 0, 1, 0, 0, a->repo,
5297 a->progress_cb, a->progress_arg);
5299 err = install_blob(a->worktree, ondisk_path,
5301 te ? te->mode : GOT_DEFAULT_FILE_MODE,
5302 got_fileindex_perms_to_st(ie), blob,
5303 0, 1, 0, 0, NULL, a->repo,
5304 a->progress_cb, a->progress_arg);
5308 if (status == GOT_STATUS_DELETE ||
5309 status == GOT_STATUS_MODE_CHANGE) {
5310 err = got_fileindex_entry_update(ie,
5311 a->worktree->root_fd, relpath,
5313 a->worktree->base_commit_id->sha1, 1);
5317 if (is_bad_symlink) {
5318 got_fileindex_entry_filetype_set(ie,
5319 GOT_FILEIDX_MODE_BAD_SYMLINK);
5332 if (fd != -1 && close(fd) == -1 && err == NULL)
5333 err = got_error_from_errno("close");
5335 got_object_blob_close(blob);
5337 got_object_tree_close(tree);
5340 got_object_commit_close(base_commit);
5344 const struct got_error *
5345 got_worktree_revert(struct got_worktree *worktree,
5346 struct got_pathlist_head *paths,
5347 got_worktree_checkout_cb progress_cb, void *progress_arg,
5348 got_worktree_patch_cb patch_cb, void *patch_arg,
5349 struct got_repository *repo)
5351 struct got_fileindex *fileindex = NULL;
5352 char *fileindex_path = NULL;
5353 const struct got_error *err = NULL, *unlockerr = NULL;
5354 const struct got_error *sync_err = NULL;
5355 struct got_pathlist_entry *pe;
5356 struct revert_file_args rfa;
5358 err = lock_worktree(worktree, LOCK_EX);
5362 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5366 rfa.worktree = worktree;
5367 rfa.fileindex = fileindex;
5368 rfa.progress_cb = progress_cb;
5369 rfa.progress_arg = progress_arg;
5370 rfa.patch_cb = patch_cb;
5371 rfa.patch_arg = patch_arg;
5373 rfa.unlink_added_files = 0;
5374 TAILQ_FOREACH(pe, paths, entry) {
5375 err = worktree_status(worktree, pe->path, fileindex, repo,
5376 revert_file, &rfa, NULL, NULL, 1, 0);
5380 sync_err = sync_fileindex(fileindex, fileindex_path);
5381 if (sync_err && err == NULL)
5384 free(fileindex_path);
5386 got_fileindex_free(fileindex);
5387 unlockerr = lock_worktree(worktree, LOCK_SH);
5388 if (unlockerr && err == NULL)
5394 free_commitable(struct got_commitable *ct)
5397 free(ct->in_repo_path);
5398 free(ct->ondisk_path);
5400 free(ct->base_blob_id);
5401 free(ct->staged_blob_id);
5402 free(ct->base_commit_id);
5406 struct collect_commitables_arg {
5407 struct got_pathlist_head *commitable_paths;
5408 struct got_repository *repo;
5409 struct got_worktree *worktree;
5410 struct got_fileindex *fileindex;
5411 int have_staged_files;
5412 int allow_bad_symlinks;
5413 int diff_header_shown;
5414 int commit_conflicts;
5421 * Create a file which contains the target path of a symlink so we can feed
5422 * it as content to the diff engine.
5424 static const struct got_error *
5425 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5426 const char *abspath)
5428 const struct got_error *err = NULL;
5429 char target_path[PATH_MAX];
5430 ssize_t target_len, outlen;
5435 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5436 if (target_len == -1)
5437 return got_error_from_errno2("readlinkat", abspath);
5439 target_len = readlink(abspath, target_path, PATH_MAX);
5440 if (target_len == -1)
5441 return got_error_from_errno2("readlink", abspath);
5444 *fd = got_opentempfd();
5446 return got_error_from_errno("got_opentempfd");
5448 outlen = write(*fd, target_path, target_len);
5450 err = got_error_from_errno("got_opentempfd");
5454 if (lseek(*fd, 0, SEEK_SET) == -1) {
5455 err = got_error_from_errno2("lseek", abspath);
5466 static const struct got_error *
5467 append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5468 FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5469 int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5471 const struct got_error *err = NULL;
5472 struct got_blob_object *blob1 = NULL;
5473 int fd = -1, fd1 = -1, fd2 = -1;
5474 FILE *ondisk_file = NULL;
5475 char *label1 = NULL;
5479 char *id_str = NULL;
5481 memset(&sb, 0, sizeof(sb));
5484 if (ct->staged_status != GOT_STATUS_MODIFY &&
5485 ct->staged_status != GOT_STATUS_ADD &&
5486 ct->staged_status != GOT_STATUS_DELETE)
5489 if (ct->status != GOT_STATUS_MODIFY &&
5490 ct->status != GOT_STATUS_ADD &&
5491 ct->status != GOT_STATUS_DELETE &&
5492 ct->status != GOT_STATUS_CONFLICT)
5496 err = got_opentemp_truncate(f1);
5498 return got_error_from_errno("got_opentemp_truncate");
5499 err = got_opentemp_truncate(f2);
5501 return got_error_from_errno("got_opentemp_truncate");
5503 if (!*diff_header_shown) {
5504 err = got_object_id_str(&id_str, worktree->base_commit_id);
5507 fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5508 got_worktree_get_root_path(worktree));
5509 fprintf(diff_outfile, "commit - %s\n", id_str);
5510 fprintf(diff_outfile, "path + %s%s\n",
5511 got_worktree_get_root_path(worktree),
5512 diff_staged ? " (staged changes)" : "");
5513 *diff_header_shown = 1;
5517 const char *label1 = NULL, *label2 = NULL;
5518 switch (ct->staged_status) {
5519 case GOT_STATUS_MODIFY:
5523 case GOT_STATUS_ADD:
5526 case GOT_STATUS_DELETE:
5530 return got_error(GOT_ERR_FILE_STATUS);
5532 fd1 = got_opentempfd();
5534 err = got_error_from_errno("got_opentempfd");
5537 fd2 = got_opentempfd();
5539 err = got_error_from_errno("got_opentempfd");
5542 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5543 fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5544 label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5545 NULL, repo, diff_outfile);
5549 fd1 = got_opentempfd();
5551 err = got_error_from_errno("got_opentempfd");
5555 if (ct->status != GOT_STATUS_ADD) {
5556 err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5562 if (ct->status != GOT_STATUS_DELETE) {
5564 fd = openat(dirfd, de_name,
5565 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5567 if (!got_err_open_nofollow_on_symlink()) {
5568 err = got_error_from_errno2("openat",
5572 err = get_symlink_target_file(&fd, dirfd,
5573 de_name, ct->ondisk_path);
5578 fd = open(ct->ondisk_path,
5579 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5581 if (!got_err_open_nofollow_on_symlink()) {
5582 err = got_error_from_errno2("open",
5586 err = get_symlink_target_file(&fd, dirfd,
5587 de_name, ct->ondisk_path);
5592 if (fstatat(fd, ct->ondisk_path, &sb,
5593 AT_SYMLINK_NOFOLLOW) == -1) {
5594 err = got_error_from_errno2("fstatat", ct->ondisk_path);
5597 ondisk_file = fdopen(fd, "r");
5598 if (ondisk_file == NULL) {
5599 err = got_error_from_errno2("fdopen", ct->ondisk_path);
5607 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5613 err = got_diff_blob_file(blob1, f1, size1, label1,
5614 ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5615 GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5617 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5618 err = got_error_from_errno("close");
5619 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5620 err = got_error_from_errno("close");
5622 got_object_blob_close(blob1);
5623 if (fd != -1 && close(fd) == -1 && err == NULL)
5624 err = got_error_from_errno("close");
5625 if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5626 err = got_error_from_errno("fclose");
5630 static const struct got_error *
5631 collect_commitables(void *arg, unsigned char status,
5632 unsigned char staged_status, const char *relpath,
5633 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5634 struct got_object_id *commit_id, int dirfd, const char *de_name)
5636 struct collect_commitables_arg *a = arg;
5637 const struct got_error *err = NULL;
5638 struct got_commitable *ct = NULL;
5639 struct got_pathlist_entry *new = NULL;
5640 char *parent_path = NULL, *path = NULL;
5643 if (a->have_staged_files) {
5644 if (staged_status != GOT_STATUS_MODIFY &&
5645 staged_status != GOT_STATUS_ADD &&
5646 staged_status != GOT_STATUS_DELETE)
5649 if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5650 printf("C %s\n", relpath);
5651 return got_error(GOT_ERR_COMMIT_CONFLICT);
5654 if (status != GOT_STATUS_MODIFY &&
5655 status != GOT_STATUS_MODE_CHANGE &&
5656 status != GOT_STATUS_ADD &&
5657 status != GOT_STATUS_DELETE &&
5658 status != GOT_STATUS_CONFLICT)
5662 if (asprintf(&path, "/%s", relpath) == -1) {
5663 err = got_error_from_errno("asprintf");
5666 if (strcmp(path, "/") == 0) {
5667 parent_path = strdup("");
5668 if (parent_path == NULL)
5669 return got_error_from_errno("strdup");
5671 err = got_path_dirname(&parent_path, path);
5676 ct = calloc(1, sizeof(*ct));
5678 err = got_error_from_errno("calloc");
5682 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5684 err = got_error_from_errno("asprintf");
5688 if (staged_status == GOT_STATUS_ADD ||
5689 staged_status == GOT_STATUS_MODIFY) {
5690 struct got_fileindex_entry *ie;
5691 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5692 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5693 case GOT_FILEIDX_MODE_REGULAR_FILE:
5694 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5697 case GOT_FILEIDX_MODE_SYMLINK:
5701 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5704 ct->mode |= got_fileindex_entry_perms_get(ie);
5705 } else if (status != GOT_STATUS_DELETE &&
5706 staged_status != GOT_STATUS_DELETE) {
5708 if (fstatat(dirfd, de_name, &sb,
5709 AT_SYMLINK_NOFOLLOW) == -1) {
5710 err = got_error_from_errno2("fstatat",
5714 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5715 err = got_error_from_errno2("lstat", ct->ondisk_path);
5718 ct->mode = sb.st_mode;
5721 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5722 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5724 err = got_error_from_errno("asprintf");
5728 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5729 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5731 char target_path[PATH_MAX];
5733 target_len = readlink(ct->ondisk_path, target_path,
5734 sizeof(target_path));
5735 if (target_len == -1) {
5736 err = got_error_from_errno2("readlink",
5740 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5741 target_len, ct->ondisk_path, a->worktree->root_path,
5742 a->worktree->meta_dir);
5745 if (is_bad_symlink) {
5746 err = got_error_path(ct->ondisk_path,
5747 GOT_ERR_BAD_SYMLINK);
5753 ct->status = status;
5754 ct->staged_status = staged_status;
5755 ct->blob_id = NULL; /* will be filled in when blob gets created */
5756 if (ct->status != GOT_STATUS_ADD &&
5757 ct->staged_status != GOT_STATUS_ADD) {
5758 ct->base_blob_id = got_object_id_dup(blob_id);
5759 if (ct->base_blob_id == NULL) {
5760 err = got_error_from_errno("got_object_id_dup");
5763 ct->base_commit_id = got_object_id_dup(commit_id);
5764 if (ct->base_commit_id == NULL) {
5765 err = got_error_from_errno("got_object_id_dup");
5769 if (ct->staged_status == GOT_STATUS_ADD ||
5770 ct->staged_status == GOT_STATUS_MODIFY) {
5771 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5772 if (ct->staged_blob_id == NULL) {
5773 err = got_error_from_errno("got_object_id_dup");
5777 ct->path = strdup(path);
5778 if (ct->path == NULL) {
5779 err = got_error_from_errno("strdup");
5783 if (a->diff_outfile) {
5784 err = append_ct_diff(ct, &a->diff_header_shown,
5785 a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5786 a->have_staged_files, a->repo, a->worktree);
5791 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5793 if (ct && (err || new == NULL))
5794 free_commitable(ct);
5800 static const struct got_error *write_tree(struct got_object_id **, int *,
5801 struct got_tree_object *, const char *, struct got_pathlist_head *,
5802 got_worktree_status_cb status_cb, void *status_arg,
5803 struct got_repository *);
5805 static const struct got_error *
5806 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5807 struct got_tree_entry *te, const char *parent_path,
5808 struct got_pathlist_head *commitable_paths,
5809 got_worktree_status_cb status_cb, void *status_arg,
5810 struct got_repository *repo)
5812 const struct got_error *err = NULL;
5813 struct got_tree_object *subtree;
5816 if (asprintf(&subpath, "%s%s%s", parent_path,
5817 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5818 return got_error_from_errno("asprintf");
5820 err = got_object_open_as_tree(&subtree, repo, &te->id);
5824 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5825 commitable_paths, status_cb, status_arg, repo);
5826 got_object_tree_close(subtree);
5831 static const struct got_error *
5832 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5834 const struct got_error *err = NULL;
5835 char *ct_parent_path = NULL;
5839 if (strchr(ct->in_repo_path, '/') == NULL) {
5840 *match = got_path_is_root_dir(path);
5844 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5847 *match = (strcmp(path, ct_parent_path) == 0);
5848 free(ct_parent_path);
5853 get_ct_file_mode(struct got_commitable *ct)
5855 if (S_ISLNK(ct->mode))
5858 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5861 static const struct got_error *
5862 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5863 struct got_tree_entry *te, struct got_commitable *ct)
5865 const struct got_error *err = NULL;
5869 err = got_object_tree_entry_dup(new_te, te);
5873 (*new_te)->mode = get_ct_file_mode(ct);
5875 if (ct->staged_status == GOT_STATUS_MODIFY)
5876 memcpy(&(*new_te)->id, ct->staged_blob_id,
5877 sizeof((*new_te)->id));
5879 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5881 if (err && *new_te) {
5888 static const struct got_error *
5889 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5890 struct got_commitable *ct)
5892 const struct got_error *err = NULL;
5893 char *ct_name = NULL;
5897 *new_te = calloc(1, sizeof(**new_te));
5898 if (*new_te == NULL)
5899 return got_error_from_errno("calloc");
5901 err = got_path_basename(&ct_name, ct->path);
5904 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5905 sizeof((*new_te)->name)) {
5906 err = got_error(GOT_ERR_NO_SPACE);
5910 (*new_te)->mode = get_ct_file_mode(ct);
5912 if (ct->staged_status == GOT_STATUS_ADD)
5913 memcpy(&(*new_te)->id, ct->staged_blob_id,
5914 sizeof((*new_te)->id));
5916 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5919 if (err && *new_te) {
5926 static const struct got_error *
5927 insert_tree_entry(struct got_tree_entry *new_te,
5928 struct got_pathlist_head *paths)
5930 const struct got_error *err = NULL;
5931 struct got_pathlist_entry *new_pe;
5933 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5937 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5941 static const struct got_error *
5942 report_ct_status(struct got_commitable *ct,
5943 got_worktree_status_cb status_cb, void *status_arg)
5945 const char *ct_path = ct->path;
5946 unsigned char status;
5948 if (status_cb == NULL) /* no commit progress output desired */
5951 while (ct_path[0] == '/')
5954 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5955 status = ct->staged_status;
5957 status = ct->status;
5959 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5960 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5963 static const struct got_error *
5964 match_modified_subtree(int *modified, struct got_tree_entry *te,
5965 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5967 const struct got_error *err = NULL;
5968 struct got_pathlist_entry *pe;
5973 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5974 got_path_is_root_dir(base_tree_path) ? "" : "/",
5976 return got_error_from_errno("asprintf");
5978 TAILQ_FOREACH(pe, commitable_paths, entry) {
5979 struct got_commitable *ct = pe->data;
5980 *modified = got_path_is_child(ct->in_repo_path, te_path,
5990 static const struct got_error *
5991 match_deleted_or_modified_ct(struct got_commitable **ctp,
5992 struct got_tree_entry *te, const char *base_tree_path,
5993 struct got_pathlist_head *commitable_paths)
5995 const struct got_error *err = NULL;
5996 struct got_pathlist_entry *pe;
6000 TAILQ_FOREACH(pe, commitable_paths, entry) {
6001 struct got_commitable *ct = pe->data;
6002 char *ct_name = NULL;
6005 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
6006 if (ct->status != GOT_STATUS_MODIFY &&
6007 ct->status != GOT_STATUS_MODE_CHANGE &&
6008 ct->status != GOT_STATUS_DELETE &&
6009 ct->status != GOT_STATUS_CONFLICT)
6012 if (ct->staged_status != GOT_STATUS_MODIFY &&
6013 ct->staged_status != GOT_STATUS_DELETE)
6017 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
6020 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
6026 err = got_path_basename(&ct_name, pe->path);
6030 if (strcmp(te->name, ct_name) != 0) {
6043 static const struct got_error *
6044 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
6045 const char *child_path, const char *path_base_tree,
6046 struct got_pathlist_head *commitable_paths,
6047 got_worktree_status_cb status_cb, void *status_arg,
6048 struct got_repository *repo)
6050 const struct got_error *err = NULL;
6051 struct got_tree_entry *new_te;
6053 struct got_object_id *id = NULL;
6058 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
6059 got_path_is_root_dir(path_base_tree) ? "" : "/",
6061 return got_error_from_errno("asprintf");
6063 new_te = calloc(1, sizeof(*new_te));
6065 return got_error_from_errno("calloc");
6066 new_te->mode = S_IFDIR;
6068 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
6069 sizeof(new_te->name)) {
6070 err = got_error(GOT_ERR_NO_SPACE);
6073 err = write_tree(&id, &nentries, NULL, subtree_path,
6074 commitable_paths, status_cb, status_arg, repo);
6079 memcpy(&new_te->id, id, sizeof(new_te->id));
6088 static const struct got_error *
6089 write_tree(struct got_object_id **new_tree_id, int *nentries,
6090 struct got_tree_object *base_tree, const char *path_base_tree,
6091 struct got_pathlist_head *commitable_paths,
6092 got_worktree_status_cb status_cb, void *status_arg,
6093 struct got_repository *repo)
6095 const struct got_error *err = NULL;
6096 struct got_pathlist_head paths;
6097 struct got_tree_entry *te, *new_te = NULL;
6098 struct got_pathlist_entry *pe;
6103 /* Insert, and recurse into, newly added entries first. */
6104 TAILQ_FOREACH(pe, commitable_paths, entry) {
6105 struct got_commitable *ct = pe->data;
6106 char *child_path = NULL, *slash;
6108 if ((ct->status != GOT_STATUS_ADD &&
6109 ct->staged_status != GOT_STATUS_ADD) ||
6110 (ct->flags & GOT_COMMITABLE_ADDED))
6113 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
6114 strlen(path_base_tree)))
6117 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
6122 slash = strchr(child_path, '/');
6123 if (slash == NULL) {
6124 err = alloc_added_blob_tree_entry(&new_te, ct);
6127 err = report_ct_status(ct, status_cb, status_arg);
6130 ct->flags |= GOT_COMMITABLE_ADDED;
6131 err = insert_tree_entry(new_te, &paths);
6136 *slash = '\0'; /* trim trailing path components */
6137 if (base_tree == NULL ||
6138 got_object_tree_find_entry(base_tree, child_path)
6140 err = make_subtree_for_added_blob(&new_te,
6141 child_path, path_base_tree,
6142 commitable_paths, status_cb, status_arg,
6146 err = insert_tree_entry(new_te, &paths);
6155 int i, nbase_entries;
6156 /* Handle modified and deleted entries. */
6157 nbase_entries = got_object_tree_get_nentries(base_tree);
6158 for (i = 0; i < nbase_entries; i++) {
6159 struct got_commitable *ct = NULL;
6161 te = got_object_tree_get_entry(base_tree, i);
6162 if (got_object_tree_entry_is_submodule(te)) {
6163 /* Entry is a submodule; just copy it. */
6164 err = got_object_tree_entry_dup(&new_te, te);
6167 err = insert_tree_entry(new_te, &paths);
6174 if (S_ISDIR(te->mode)) {
6176 err = got_object_tree_entry_dup(&new_te, te);
6179 err = match_modified_subtree(&modified, te,
6180 path_base_tree, commitable_paths);
6183 /* Avoid recursion into unmodified subtrees. */
6185 struct got_object_id *new_id;
6187 err = write_subtree(&new_id,
6189 path_base_tree, commitable_paths,
6190 status_cb, status_arg, repo);
6193 if (nsubentries == 0) {
6194 /* All entries were deleted. */
6198 memcpy(&new_te->id, new_id,
6199 sizeof(new_te->id));
6202 err = insert_tree_entry(new_te, &paths);
6209 err = match_deleted_or_modified_ct(&ct, te,
6210 path_base_tree, commitable_paths);
6214 /* NB: Deleted entries get dropped here. */
6215 if (ct->status == GOT_STATUS_MODIFY ||
6216 ct->status == GOT_STATUS_MODE_CHANGE ||
6217 ct->status == GOT_STATUS_CONFLICT ||
6218 ct->staged_status == GOT_STATUS_MODIFY) {
6219 err = alloc_modified_blob_tree_entry(
6223 err = insert_tree_entry(new_te, &paths);
6228 err = report_ct_status(ct, status_cb,
6233 /* Entry is unchanged; just copy it. */
6234 err = got_object_tree_entry_dup(&new_te, te);
6237 err = insert_tree_entry(new_te, &paths);
6245 /* Write new list of entries; deleted entries have been dropped. */
6246 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
6248 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
6252 static const struct got_error *
6253 update_fileindex_after_commit(struct got_worktree *worktree,
6254 struct got_pathlist_head *commitable_paths,
6255 struct got_object_id *new_base_commit_id,
6256 struct got_fileindex *fileindex, int have_staged_files)
6258 const struct got_error *err = NULL;
6259 struct got_pathlist_entry *pe;
6260 char *relpath = NULL;
6262 TAILQ_FOREACH(pe, commitable_paths, entry) {
6263 struct got_fileindex_entry *ie;
6264 struct got_commitable *ct = pe->data;
6266 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6268 err = got_path_skip_common_ancestor(&relpath,
6269 worktree->root_path, ct->ondisk_path);
6274 if (ct->status == GOT_STATUS_DELETE ||
6275 ct->staged_status == GOT_STATUS_DELETE) {
6276 got_fileindex_entry_remove(fileindex, ie);
6277 } else if (ct->staged_status == GOT_STATUS_ADD ||
6278 ct->staged_status == GOT_STATUS_MODIFY) {
6279 got_fileindex_entry_stage_set(ie,
6280 GOT_FILEIDX_STAGE_NONE);
6281 got_fileindex_entry_staged_filetype_set(ie, 0);
6283 err = got_fileindex_entry_update(ie,
6284 worktree->root_fd, relpath,
6285 ct->staged_blob_id->sha1,
6286 new_base_commit_id->sha1,
6287 !have_staged_files);
6289 err = got_fileindex_entry_update(ie,
6290 worktree->root_fd, relpath,
6292 new_base_commit_id->sha1,
6293 !have_staged_files);
6295 err = got_fileindex_entry_alloc(&ie, pe->path);
6298 err = got_fileindex_entry_update(ie,
6299 worktree->root_fd, relpath, ct->blob_id->sha1,
6300 new_base_commit_id->sha1, 1);
6302 got_fileindex_entry_free(ie);
6305 err = got_fileindex_entry_add(fileindex, ie);
6307 got_fileindex_entry_free(ie);
6320 static const struct got_error *
6321 check_out_of_date(const char *in_repo_path, unsigned char status,
6322 unsigned char staged_status, struct got_object_id *base_blob_id,
6323 struct got_object_id *base_commit_id,
6324 struct got_object_id *head_commit_id, struct got_repository *repo,
6327 const struct got_error *err = NULL;
6328 struct got_commit_object *commit = NULL;
6329 struct got_object_id *id = NULL;
6331 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
6332 /* Trivial case: base commit == head commit */
6333 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
6336 * Ensure file content which local changes were based
6337 * on matches file content in the branch head.
6339 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6342 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6344 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6345 err = got_error(ood_errcode);
6347 } else if (got_object_id_cmp(id, base_blob_id) != 0)
6348 err = got_error(ood_errcode);
6350 /* Require that added files don't exist in the branch head. */
6351 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6354 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6355 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
6357 err = id ? got_error(ood_errcode) : NULL;
6362 got_object_commit_close(commit);
6366 static const struct got_error *
6367 commit_worktree(struct got_object_id **new_commit_id,
6368 struct got_pathlist_head *commitable_paths,
6369 struct got_object_id *head_commit_id,
6370 struct got_object_id *parent_id2,
6371 struct got_worktree *worktree,
6372 const char *author, const char *committer, char *diff_path,
6373 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6374 got_worktree_status_cb status_cb, void *status_arg,
6375 struct got_repository *repo)
6377 const struct got_error *err = NULL, *unlockerr = NULL;
6378 struct got_pathlist_entry *pe;
6379 const char *head_ref_name = NULL;
6380 struct got_commit_object *head_commit = NULL;
6381 struct got_reference *head_ref2 = NULL;
6382 struct got_object_id *head_commit_id2 = NULL;
6383 struct got_tree_object *head_tree = NULL;
6384 struct got_object_id *new_tree_id = NULL;
6385 int nentries, nparents = 0;
6386 struct got_object_id_queue parent_ids;
6387 struct got_object_qid *pid = NULL;
6388 char *logmsg = NULL;
6391 *new_commit_id = NULL;
6393 STAILQ_INIT(&parent_ids);
6395 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6399 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6403 if (commit_msg_cb != NULL) {
6404 err = commit_msg_cb(commitable_paths, diff_path,
6405 &logmsg, commit_arg);
6410 if (logmsg == NULL || strlen(logmsg) == 0) {
6411 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6415 /* Create blobs from added and modified files and record their IDs. */
6416 TAILQ_FOREACH(pe, commitable_paths, entry) {
6417 struct got_commitable *ct = pe->data;
6420 /* Blobs for staged files already exist. */
6421 if (ct->staged_status == GOT_STATUS_ADD ||
6422 ct->staged_status == GOT_STATUS_MODIFY)
6425 if (ct->status != GOT_STATUS_ADD &&
6426 ct->status != GOT_STATUS_MODIFY &&
6427 ct->status != GOT_STATUS_MODE_CHANGE &&
6428 ct->status != GOT_STATUS_CONFLICT)
6431 if (asprintf(&ondisk_path, "%s/%s",
6432 worktree->root_path, pe->path) == -1) {
6433 err = got_error_from_errno("asprintf");
6436 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6442 /* Recursively write new tree objects. */
6443 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6444 commitable_paths, status_cb, status_arg, repo);
6448 err = got_object_qid_alloc(&pid, head_commit_id);
6451 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6454 err = got_object_qid_alloc(&pid, parent_id2);
6457 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6460 timestamp = time(NULL);
6461 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6462 nparents, author, timestamp, committer, timestamp, logmsg, repo);
6468 /* Check if a concurrent commit to our branch has occurred. */
6469 head_ref_name = got_worktree_get_head_ref_name(worktree);
6470 if (head_ref_name == NULL) {
6471 err = got_error_from_errno("got_worktree_get_head_ref_name");
6474 /* Lock the reference here to prevent concurrent modification. */
6475 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6478 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6481 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6482 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6485 /* Update branch head in repository. */
6486 err = got_ref_change_ref(head_ref2, *new_commit_id);
6489 err = got_ref_write(head_ref2, repo);
6493 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6497 err = ref_base_commit(worktree, repo);
6501 got_object_id_queue_free(&parent_ids);
6503 got_object_tree_close(head_tree);
6505 got_object_commit_close(head_commit);
6506 free(head_commit_id2);
6508 unlockerr = got_ref_unlock(head_ref2);
6509 if (unlockerr && err == NULL)
6511 got_ref_close(head_ref2);
6516 static const struct got_error *
6517 check_path_is_commitable(const char *path,
6518 struct got_pathlist_head *commitable_paths)
6520 struct got_pathlist_entry *cpe = NULL;
6521 size_t path_len = strlen(path);
6523 TAILQ_FOREACH(cpe, commitable_paths, entry) {
6524 struct got_commitable *ct = cpe->data;
6525 const char *ct_path = ct->path;
6527 while (ct_path[0] == '/')
6530 if (strcmp(path, ct_path) == 0 ||
6531 got_path_is_child(ct_path, path, path_len))
6536 return got_error_path(path, GOT_ERR_BAD_PATH);
6541 static const struct got_error *
6542 check_staged_file(void *arg, struct got_fileindex_entry *ie)
6544 int *have_staged_files = arg;
6546 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6547 *have_staged_files = 1;
6548 return got_error(GOT_ERR_CANCELLED);
6554 static const struct got_error *
6555 check_non_staged_files(struct got_fileindex *fileindex,
6556 struct got_pathlist_head *paths)
6558 struct got_pathlist_entry *pe;
6559 struct got_fileindex_entry *ie;
6561 TAILQ_FOREACH(pe, paths, entry) {
6562 if (pe->path[0] == '\0')
6564 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6566 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6567 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6568 return got_error_path(pe->path,
6569 GOT_ERR_FILE_NOT_STAGED);
6575 const struct got_error *
6576 got_worktree_commit(struct got_object_id **new_commit_id,
6577 struct got_worktree *worktree, struct got_pathlist_head *paths,
6578 const char *author, const char *committer, int allow_bad_symlinks,
6579 int show_diff, int commit_conflicts,
6580 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6581 got_worktree_status_cb status_cb, void *status_arg,
6582 struct got_repository *repo)
6584 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6585 struct got_fileindex *fileindex = NULL;
6586 char *fileindex_path = NULL;
6587 struct got_pathlist_head commitable_paths;
6588 struct collect_commitables_arg cc_arg;
6589 struct got_pathlist_entry *pe;
6590 struct got_reference *head_ref = NULL;
6591 struct got_object_id *head_commit_id = NULL;
6592 char *diff_path = NULL;
6593 int have_staged_files = 0;
6595 *new_commit_id = NULL;
6597 memset(&cc_arg, 0, sizeof(cc_arg));
6598 TAILQ_INIT(&commitable_paths);
6600 err = lock_worktree(worktree, LOCK_EX);
6604 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6608 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6612 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6616 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6617 &have_staged_files);
6618 if (err && err->code != GOT_ERR_CANCELLED)
6620 if (have_staged_files) {
6621 err = check_non_staged_files(fileindex, paths);
6626 cc_arg.commitable_paths = &commitable_paths;
6627 cc_arg.worktree = worktree;
6628 cc_arg.fileindex = fileindex;
6630 cc_arg.have_staged_files = have_staged_files;
6631 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6632 cc_arg.diff_header_shown = 0;
6633 cc_arg.commit_conflicts = commit_conflicts;
6635 err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6636 GOT_TMPDIR_STR "/got", ".diff");
6639 cc_arg.f1 = got_opentemp();
6640 if (cc_arg.f1 == NULL) {
6641 err = got_error_from_errno("got_opentemp");
6644 cc_arg.f2 = got_opentemp();
6645 if (cc_arg.f2 == NULL) {
6646 err = got_error_from_errno("got_opentemp");
6651 TAILQ_FOREACH(pe, paths, entry) {
6652 err = worktree_status(worktree, pe->path, fileindex, repo,
6653 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6659 if (fflush(cc_arg.diff_outfile) == EOF) {
6660 err = got_error_from_errno("fflush");
6665 if (TAILQ_EMPTY(&commitable_paths)) {
6666 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6670 TAILQ_FOREACH(pe, paths, entry) {
6671 err = check_path_is_commitable(pe->path, &commitable_paths);
6676 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6677 struct got_commitable *ct = pe->data;
6678 const char *ct_path = ct->in_repo_path;
6680 while (ct_path[0] == '/')
6682 err = check_out_of_date(ct_path, ct->status,
6683 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6684 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6690 err = commit_worktree(new_commit_id, &commitable_paths,
6691 head_commit_id, NULL, worktree, author, committer,
6692 (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6693 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6697 err = update_fileindex_after_commit(worktree, &commitable_paths,
6698 *new_commit_id, fileindex, have_staged_files);
6699 sync_err = sync_fileindex(fileindex, fileindex_path);
6700 if (sync_err && err == NULL)
6704 got_fileindex_free(fileindex);
6705 free(fileindex_path);
6706 unlockerr = lock_worktree(worktree, LOCK_SH);
6707 if (unlockerr && err == NULL)
6709 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6710 struct got_commitable *ct = pe->data;
6712 free_commitable(ct);
6714 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6715 if (diff_path && unlink(diff_path) == -1 && err == NULL)
6716 err = got_error_from_errno2("unlink", diff_path);
6718 if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6720 err = got_error_from_errno("fclose");
6725 got_commitable_get_path(struct got_commitable *ct)
6731 got_commitable_get_status(struct got_commitable *ct)
6736 struct check_rebase_ok_arg {
6737 struct got_worktree *worktree;
6738 struct got_repository *repo;
6741 static const struct got_error *
6742 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6744 const struct got_error *err = NULL;
6745 struct check_rebase_ok_arg *a = arg;
6746 unsigned char status;
6750 /* Reject rebase of a work tree with mixed base commits. */
6751 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6752 SHA1_DIGEST_LENGTH))
6753 return got_error(GOT_ERR_MIXED_COMMITS);
6755 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6757 return got_error_from_errno("asprintf");
6759 /* Reject rebase of a work tree with modified or staged files. */
6760 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6765 if (status != GOT_STATUS_NO_CHANGE)
6766 return got_error(GOT_ERR_MODIFIED);
6767 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6768 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6773 const struct got_error *
6774 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6775 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6776 struct got_worktree *worktree, struct got_reference *branch,
6777 struct got_repository *repo)
6779 const struct got_error *err = NULL;
6780 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6781 char *branch_ref_name = NULL;
6782 char *fileindex_path = NULL;
6783 struct check_rebase_ok_arg ok_arg;
6784 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6785 struct got_object_id *wt_branch_tip = NULL;
6787 *new_base_branch_ref = NULL;
6791 err = lock_worktree(worktree, LOCK_EX);
6795 err = open_fileindex(fileindex, &fileindex_path, worktree);
6799 ok_arg.worktree = worktree;
6801 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6806 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6810 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6814 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6818 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6823 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6826 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6827 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6831 err = got_ref_alloc_symref(new_base_branch_ref,
6832 new_base_branch_ref_name, wt_branch);
6835 err = got_ref_write(*new_base_branch_ref, repo);
6839 /* TODO Lock original branch's ref while rebasing? */
6841 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6845 err = got_ref_write(branch_ref, repo);
6849 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6850 worktree->base_commit_id);
6853 err = got_ref_write(*tmp_branch, repo);
6857 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6861 free(fileindex_path);
6862 free(tmp_branch_name);
6863 free(new_base_branch_ref_name);
6864 free(branch_ref_name);
6866 got_ref_close(branch_ref);
6868 got_ref_close(wt_branch);
6869 free(wt_branch_tip);
6871 if (*new_base_branch_ref) {
6872 got_ref_close(*new_base_branch_ref);
6873 *new_base_branch_ref = NULL;
6876 got_ref_close(*tmp_branch);
6880 got_fileindex_free(*fileindex);
6883 lock_worktree(worktree, LOCK_SH);
6888 const struct got_error *
6889 got_worktree_rebase_continue(struct got_object_id **commit_id,
6890 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6891 struct got_reference **branch, struct got_fileindex **fileindex,
6892 struct got_worktree *worktree, struct got_repository *repo)
6894 const struct got_error *err;
6895 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6896 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6897 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6898 char *fileindex_path = NULL;
6899 int have_staged_files = 0;
6902 *new_base_branch = NULL;
6907 err = lock_worktree(worktree, LOCK_EX);
6911 err = open_fileindex(fileindex, &fileindex_path, worktree);
6915 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6916 &have_staged_files);
6917 if (err && err->code != GOT_ERR_CANCELLED)
6919 if (have_staged_files) {
6920 err = got_error(GOT_ERR_STAGED_PATHS);
6924 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6928 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6932 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6936 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6940 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6944 err = got_ref_open(branch, repo,
6945 got_ref_get_symref_target(branch_ref), 0);
6949 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6953 err = got_ref_resolve(commit_id, repo, commit_ref);
6957 err = got_ref_open(new_base_branch, repo,
6958 new_base_branch_ref_name, 0);
6962 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6966 free(commit_ref_name);
6967 free(branch_ref_name);
6968 free(fileindex_path);
6970 got_ref_close(commit_ref);
6972 got_ref_close(branch_ref);
6977 got_ref_close(*tmp_branch);
6980 if (*new_base_branch) {
6981 got_ref_close(*new_base_branch);
6982 *new_base_branch = NULL;
6985 got_ref_close(*branch);
6989 got_fileindex_free(*fileindex);
6992 lock_worktree(worktree, LOCK_SH);
6997 const struct got_error *
6998 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
7000 const struct got_error *err;
7001 char *tmp_branch_name = NULL;
7003 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7007 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7008 free(tmp_branch_name);
7012 static const struct got_error *
7013 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
7014 const char *diff_path, char **logmsg, void *arg)
7020 static const struct got_error *
7021 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
7022 const char *path, struct got_object_id *blob_id,
7023 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7024 int dirfd, const char *de_name)
7029 struct collect_merged_paths_arg {
7030 got_worktree_checkout_cb progress_cb;
7032 struct got_pathlist_head *merged_paths;
7035 static const struct got_error *
7036 collect_merged_paths(void *arg, unsigned char status, const char *path)
7038 const struct got_error *err;
7039 struct collect_merged_paths_arg *a = arg;
7041 struct got_pathlist_entry *new;
7043 err = (*a->progress_cb)(a->progress_arg, status, path);
7047 if (status != GOT_STATUS_MERGE &&
7048 status != GOT_STATUS_ADD &&
7049 status != GOT_STATUS_DELETE &&
7050 status != GOT_STATUS_CONFLICT)
7055 return got_error_from_errno("strdup");
7057 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
7058 if (err || new == NULL)
7063 static const struct got_error *
7064 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
7065 int is_rebase, struct got_repository *repo)
7067 const struct got_error *err;
7068 struct got_reference *commit_ref = NULL;
7070 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7072 if (err->code != GOT_ERR_NOT_REF)
7074 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
7077 err = got_ref_write(commit_ref, repo);
7080 } else if (is_rebase) {
7081 struct got_object_id *stored_id;
7084 err = got_ref_resolve(&stored_id, repo, commit_ref);
7087 cmp = got_object_id_cmp(commit_id, stored_id);
7090 err = got_error(GOT_ERR_REBASE_COMMITID);
7096 got_ref_close(commit_ref);
7100 static const struct got_error *
7101 rebase_merge_files(struct got_pathlist_head *merged_paths,
7102 const char *commit_ref_name, struct got_worktree *worktree,
7103 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
7104 struct got_object_id *commit_id, struct got_repository *repo,
7105 got_worktree_checkout_cb progress_cb, void *progress_arg,
7106 got_cancel_cb cancel_cb, void *cancel_arg)
7108 const struct got_error *err;
7109 struct got_reference *commit_ref = NULL;
7110 struct collect_merged_paths_arg cmp_arg;
7111 char *fileindex_path;
7113 /* Work tree is locked/unlocked during rebase preparation/teardown. */
7115 err = get_fileindex_path(&fileindex_path, worktree);
7119 cmp_arg.progress_cb = progress_cb;
7120 cmp_arg.progress_arg = progress_arg;
7121 cmp_arg.merged_paths = merged_paths;
7122 err = merge_files(worktree, fileindex, fileindex_path,
7123 parent_commit_id, commit_id, repo, collect_merged_paths,
7124 &cmp_arg, cancel_cb, cancel_arg);
7126 got_ref_close(commit_ref);
7130 const struct got_error *
7131 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
7132 struct got_worktree *worktree, struct got_fileindex *fileindex,
7133 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
7134 struct got_repository *repo,
7135 got_worktree_checkout_cb progress_cb, void *progress_arg,
7136 got_cancel_cb cancel_cb, void *cancel_arg)
7138 const struct got_error *err;
7139 char *commit_ref_name;
7141 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7145 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
7149 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7150 fileindex, parent_commit_id, commit_id, repo, progress_cb,
7151 progress_arg, cancel_cb, cancel_arg);
7153 free(commit_ref_name);
7157 const struct got_error *
7158 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
7159 struct got_worktree *worktree, struct got_fileindex *fileindex,
7160 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
7161 struct got_repository *repo,
7162 got_worktree_checkout_cb progress_cb, void *progress_arg,
7163 got_cancel_cb cancel_cb, void *cancel_arg)
7165 const struct got_error *err;
7166 char *commit_ref_name;
7168 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7172 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7176 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7177 fileindex, parent_commit_id, commit_id, repo, progress_cb,
7178 progress_arg, cancel_cb, cancel_arg);
7180 free(commit_ref_name);
7184 static const struct got_error *
7185 rebase_commit(struct got_object_id **new_commit_id,
7186 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
7187 struct got_worktree *worktree, struct got_fileindex *fileindex,
7188 struct got_reference *tmp_branch, const char *committer,
7189 struct got_commit_object *orig_commit, const char *new_logmsg,
7190 int allow_conflict, struct got_repository *repo)
7192 const struct got_error *err, *sync_err;
7193 struct got_pathlist_head commitable_paths;
7194 struct collect_commitables_arg cc_arg;
7195 char *fileindex_path = NULL;
7196 struct got_reference *head_ref = NULL;
7197 struct got_object_id *head_commit_id = NULL;
7198 char *logmsg = NULL;
7200 memset(&cc_arg, 0, sizeof(cc_arg));
7201 TAILQ_INIT(&commitable_paths);
7202 *new_commit_id = NULL;
7204 /* Work tree is locked/unlocked during rebase preparation/teardown. */
7206 err = get_fileindex_path(&fileindex_path, worktree);
7210 cc_arg.commitable_paths = &commitable_paths;
7211 cc_arg.worktree = worktree;
7213 cc_arg.have_staged_files = 0;
7214 cc_arg.commit_conflicts = allow_conflict;
7216 * If possible get the status of individual files directly to
7217 * avoid crawling the entire work tree once per rebased commit.
7219 * Ideally, merged_paths would contain a list of commitables
7220 * we could use so we could skip worktree_status() entirely.
7221 * However, we would then need carefully keep track of cumulative
7222 * effects of operations such as file additions and deletions
7223 * in 'got histedit -f' (folding multiple commits into one),
7224 * and this extra complexity is not really worth it.
7227 struct got_pathlist_entry *pe;
7228 TAILQ_FOREACH(pe, merged_paths, entry) {
7229 err = worktree_status(worktree, pe->path, fileindex,
7230 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
7236 err = worktree_status(worktree, "", fileindex, repo,
7237 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7242 if (TAILQ_EMPTY(&commitable_paths)) {
7243 /* No-op change; commit will be elided. */
7244 err = got_ref_delete(commit_ref, repo);
7247 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
7251 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7255 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7260 logmsg = strdup(new_logmsg);
7261 if (logmsg == NULL) {
7262 err = got_error_from_errno("strdup");
7266 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
7271 /* NB: commit_worktree will call free(logmsg) */
7272 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
7273 NULL, worktree, got_object_commit_get_author(orig_commit),
7274 committer ? committer :
7275 got_object_commit_get_committer(orig_commit), NULL,
7276 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
7280 err = got_ref_change_ref(tmp_branch, *new_commit_id);
7284 err = got_ref_delete(commit_ref, repo);
7288 err = update_fileindex_after_commit(worktree, &commitable_paths,
7289 *new_commit_id, fileindex, 0);
7290 sync_err = sync_fileindex(fileindex, fileindex_path);
7291 if (sync_err && err == NULL)
7294 free(fileindex_path);
7295 free(head_commit_id);
7297 got_ref_close(head_ref);
7299 free(*new_commit_id);
7300 *new_commit_id = NULL;
7305 const struct got_error *
7306 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
7307 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7308 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7309 const char *committer, struct got_commit_object *orig_commit,
7310 struct got_object_id *orig_commit_id, int allow_conflict,
7311 struct got_repository *repo)
7313 const struct got_error *err;
7314 char *commit_ref_name;
7315 struct got_reference *commit_ref = NULL;
7316 struct got_object_id *commit_id = NULL;
7318 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7322 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7325 err = got_ref_resolve(&commit_id, repo, commit_ref);
7328 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
7329 err = got_error(GOT_ERR_REBASE_COMMITID);
7333 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7334 worktree, fileindex, tmp_branch, committer, orig_commit,
7335 NULL, allow_conflict, repo);
7338 got_ref_close(commit_ref);
7339 free(commit_ref_name);
7344 const struct got_error *
7345 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
7346 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7347 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7348 const char *committer, struct got_commit_object *orig_commit,
7349 struct got_object_id *orig_commit_id, const char *new_logmsg,
7350 int allow_conflict, struct got_repository *repo)
7352 const struct got_error *err;
7353 char *commit_ref_name;
7354 struct got_reference *commit_ref = NULL;
7356 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7360 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7364 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7365 worktree, fileindex, tmp_branch, committer, orig_commit,
7366 new_logmsg, allow_conflict, repo);
7369 got_ref_close(commit_ref);
7370 free(commit_ref_name);
7374 const struct got_error *
7375 got_worktree_rebase_postpone(struct got_worktree *worktree,
7376 struct got_fileindex *fileindex)
7379 got_fileindex_free(fileindex);
7380 return lock_worktree(worktree, LOCK_SH);
7383 static const struct got_error *
7384 delete_ref(const char *name, struct got_repository *repo)
7386 const struct got_error *err;
7387 struct got_reference *ref;
7389 err = got_ref_open(&ref, repo, name, 0);
7391 if (err->code == GOT_ERR_NOT_REF)
7396 err = got_ref_delete(ref, repo);
7401 static const struct got_error *
7402 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7404 const struct got_error *err;
7405 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7406 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7408 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7411 err = delete_ref(tmp_branch_name, repo);
7415 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7418 err = delete_ref(new_base_branch_ref_name, repo);
7422 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7425 err = delete_ref(branch_ref_name, repo);
7429 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7432 err = delete_ref(commit_ref_name, repo);
7437 free(tmp_branch_name);
7438 free(new_base_branch_ref_name);
7439 free(branch_ref_name);
7440 free(commit_ref_name);
7444 static const struct got_error *
7445 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7446 struct got_object_id *new_commit_id, struct got_repository *repo)
7448 const struct got_error *err;
7449 struct got_reference *ref = NULL;
7450 struct got_object_id *old_commit_id = NULL;
7451 const char *branch_name = NULL;
7452 char *new_id_str = NULL;
7453 char *refname = NULL;
7455 branch_name = got_ref_get_name(branch);
7456 if (strncmp(branch_name, "refs/heads/", 11) != 0)
7457 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7460 err = got_object_id_str(&new_id_str, new_commit_id);
7464 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7465 new_id_str) == -1) {
7466 err = got_error_from_errno("asprintf");
7470 err = got_ref_resolve(&old_commit_id, repo, branch);
7474 err = got_ref_alloc(&ref, refname, old_commit_id);
7478 err = got_ref_write(ref, repo);
7482 free(old_commit_id);
7488 const struct got_error *
7489 got_worktree_rebase_complete(struct got_worktree *worktree,
7490 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7491 struct got_reference *rebased_branch, struct got_repository *repo,
7494 const struct got_error *err, *unlockerr, *sync_err;
7495 struct got_object_id *new_head_commit_id = NULL;
7496 char *fileindex_path = NULL;
7498 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7502 if (create_backup) {
7503 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7504 rebased_branch, new_head_commit_id, repo);
7509 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7513 err = got_ref_write(rebased_branch, repo);
7517 err = got_worktree_set_head_ref(worktree, rebased_branch);
7521 err = delete_rebase_refs(worktree, repo);
7525 err = get_fileindex_path(&fileindex_path, worktree);
7528 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7529 sync_err = sync_fileindex(fileindex, fileindex_path);
7530 if (sync_err && err == NULL)
7533 got_fileindex_free(fileindex);
7534 free(fileindex_path);
7535 free(new_head_commit_id);
7536 unlockerr = lock_worktree(worktree, LOCK_SH);
7537 if (unlockerr && err == NULL)
7542 static const struct got_error *
7543 get_paths_changed_between_commits(struct got_pathlist_head *paths,
7544 struct got_object_id *id1, struct got_object_id *id2,
7545 struct got_repository *repo)
7547 const struct got_error *err;
7548 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
7549 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7552 err = got_object_open_as_commit(&commit1, repo, id1);
7556 err = got_object_open_as_tree(&tree1, repo,
7557 got_object_commit_get_tree_id(commit1));
7563 err = got_object_open_as_commit(&commit2, repo, id2);
7567 err = got_object_open_as_tree(&tree2, repo,
7568 got_object_commit_get_tree_id(commit2));
7573 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
7574 got_diff_tree_collect_changed_paths, paths, 0);
7579 got_object_commit_close(commit1);
7581 got_object_commit_close(commit2);
7583 got_object_tree_close(tree1);
7585 got_object_tree_close(tree2);
7589 static const struct got_error *
7590 get_paths_added_between_commits(struct got_pathlist_head *added_paths,
7591 struct got_object_id *id1, struct got_object_id *id2,
7592 const char *path_prefix, struct got_repository *repo)
7594 const struct got_error *err;
7595 struct got_pathlist_head merged_paths;
7596 struct got_pathlist_entry *pe;
7597 char *abspath = NULL, *wt_path = NULL;
7599 TAILQ_INIT(&merged_paths);
7601 err = get_paths_changed_between_commits(&merged_paths, id1, id2, repo);
7605 TAILQ_FOREACH(pe, &merged_paths, entry) {
7606 struct got_diff_changed_path *change = pe->data;
7608 if (change->status != GOT_STATUS_ADD)
7611 if (got_path_is_root_dir(path_prefix)) {
7612 wt_path = strdup(pe->path);
7613 if (wt_path == NULL) {
7614 err = got_error_from_errno("strdup");
7618 if (asprintf(&abspath, "/%s", pe->path) == -1) {
7619 err = got_error_from_errno("asprintf");
7623 err = got_path_skip_common_ancestor(&wt_path,
7624 path_prefix, abspath);
7631 err = got_pathlist_append(added_paths, wt_path, NULL);
7638 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_ALL);
7644 static const struct got_error *
7645 get_paths_added_in_commit(struct got_pathlist_head *added_paths,
7646 struct got_object_id *id, const char *path_prefix,
7647 struct got_repository *repo)
7649 const struct got_error *err;
7650 struct got_commit_object *commit = NULL;
7651 struct got_object_qid *pid;
7653 err = got_object_open_as_commit(&commit, repo, id);
7657 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7659 err = get_paths_added_between_commits(added_paths,
7660 pid ? &pid->id : NULL, id, path_prefix, repo);
7665 got_object_commit_close(commit);
7669 const struct got_error *
7670 got_worktree_rebase_abort(struct got_worktree *worktree,
7671 struct got_fileindex *fileindex, struct got_repository *repo,
7672 struct got_reference *new_base_branch,
7673 got_worktree_checkout_cb progress_cb, void *progress_arg)
7675 const struct got_error *err, *unlockerr, *sync_err;
7676 struct got_reference *resolved = NULL;
7677 struct got_object_id *commit_id = NULL;
7678 struct got_object_id *merged_commit_id = NULL;
7679 struct got_commit_object *commit = NULL;
7680 char *fileindex_path = NULL;
7681 char *commit_ref_name = NULL;
7682 struct got_reference *commit_ref = NULL;
7683 struct revert_file_args rfa;
7684 struct got_object_id *tree_id = NULL;
7685 struct got_pathlist_head added_paths;
7687 TAILQ_INIT(&added_paths);
7689 err = lock_worktree(worktree, LOCK_EX);
7693 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7697 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7701 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
7706 * Determine which files in added status can be safely removed
7707 * from disk while reverting changes in the work tree.
7708 * We want to avoid deleting unrelated files which were added by
7709 * the user for conflict resolution purposes.
7711 err = get_paths_added_in_commit(&added_paths, merged_commit_id,
7712 got_worktree_get_path_prefix(worktree), repo);
7716 err = got_ref_open(&resolved, repo,
7717 got_ref_get_symref_target(new_base_branch), 0);
7721 err = got_worktree_set_head_ref(worktree, resolved);
7726 * XXX commits to the base branch could have happened while
7727 * we were busy rebasing; should we store the original commit ID
7728 * when rebase begins and read it back here?
7730 err = got_ref_resolve(&commit_id, repo, resolved);
7734 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7738 err = got_object_open_as_commit(&commit, repo,
7739 worktree->base_commit_id);
7743 err = got_object_id_by_path(&tree_id, repo, commit,
7744 worktree->path_prefix);
7748 err = delete_rebase_refs(worktree, repo);
7752 err = get_fileindex_path(&fileindex_path, worktree);
7756 rfa.worktree = worktree;
7757 rfa.fileindex = fileindex;
7758 rfa.progress_cb = progress_cb;
7759 rfa.progress_arg = progress_arg;
7760 rfa.patch_cb = NULL;
7761 rfa.patch_arg = NULL;
7763 rfa.unlink_added_files = 1;
7764 rfa.added_files_to_unlink = &added_paths;
7765 err = worktree_status(worktree, "", fileindex, repo,
7766 revert_file, &rfa, NULL, NULL, 1, 0);
7770 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7771 repo, progress_cb, progress_arg, NULL, NULL);
7773 sync_err = sync_fileindex(fileindex, fileindex_path);
7774 if (sync_err && err == NULL)
7777 got_pathlist_free(&added_paths, GOT_PATHLIST_FREE_PATH);
7778 got_ref_close(resolved);
7781 free(merged_commit_id);
7783 got_object_commit_close(commit);
7785 got_fileindex_free(fileindex);
7786 free(fileindex_path);
7787 free(commit_ref_name);
7789 got_ref_close(commit_ref);
7791 unlockerr = lock_worktree(worktree, LOCK_SH);
7792 if (unlockerr && err == NULL)
7797 const struct got_error *
7798 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7799 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7800 struct got_fileindex **fileindex, struct got_worktree *worktree,
7801 struct got_repository *repo)
7803 const struct got_error *err = NULL;
7804 char *tmp_branch_name = NULL;
7805 char *branch_ref_name = NULL;
7806 char *base_commit_ref_name = NULL;
7807 char *fileindex_path = NULL;
7808 struct check_rebase_ok_arg ok_arg;
7809 struct got_reference *wt_branch = NULL;
7810 struct got_reference *base_commit_ref = NULL;
7814 *base_commit_id = NULL;
7817 err = lock_worktree(worktree, LOCK_EX);
7821 err = open_fileindex(fileindex, &fileindex_path, worktree);
7825 ok_arg.worktree = worktree;
7827 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7832 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7836 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7840 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7845 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7850 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7854 err = got_ref_write(*branch_ref, repo);
7858 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7859 worktree->base_commit_id);
7862 err = got_ref_write(base_commit_ref, repo);
7865 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7866 if (*base_commit_id == NULL) {
7867 err = got_error_from_errno("got_object_id_dup");
7871 err = got_ref_alloc(tmp_branch, tmp_branch_name,
7872 worktree->base_commit_id);
7875 err = got_ref_write(*tmp_branch, repo);
7879 err = got_worktree_set_head_ref(worktree, *tmp_branch);
7883 free(fileindex_path);
7884 free(tmp_branch_name);
7885 free(branch_ref_name);
7886 free(base_commit_ref_name);
7888 got_ref_close(wt_branch);
7891 got_ref_close(*branch_ref);
7895 got_ref_close(*tmp_branch);
7898 free(*base_commit_id);
7900 got_fileindex_free(*fileindex);
7903 lock_worktree(worktree, LOCK_SH);
7908 const struct got_error *
7909 got_worktree_histedit_postpone(struct got_worktree *worktree,
7910 struct got_fileindex *fileindex)
7913 got_fileindex_free(fileindex);
7914 return lock_worktree(worktree, LOCK_SH);
7917 const struct got_error *
7918 got_worktree_histedit_in_progress(int *in_progress,
7919 struct got_worktree *worktree)
7921 const struct got_error *err;
7922 char *tmp_branch_name = NULL;
7924 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7928 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7929 free(tmp_branch_name);
7933 const struct got_error *
7934 got_worktree_histedit_continue(struct got_object_id **commit_id,
7935 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7936 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7937 struct got_worktree *worktree, struct got_repository *repo)
7939 const struct got_error *err;
7940 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7941 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7942 struct got_reference *commit_ref = NULL;
7943 struct got_reference *base_commit_ref = NULL;
7944 char *fileindex_path = NULL;
7945 int have_staged_files = 0;
7949 *base_commit_id = NULL;
7952 err = lock_worktree(worktree, LOCK_EX);
7956 err = open_fileindex(fileindex, &fileindex_path, worktree);
7960 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7961 &have_staged_files);
7962 if (err && err->code != GOT_ERR_CANCELLED)
7964 if (have_staged_files) {
7965 err = got_error(GOT_ERR_STAGED_PATHS);
7969 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7973 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7977 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7981 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7986 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7990 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7993 err = got_ref_resolve(commit_id, repo, commit_ref);
7997 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
8000 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
8004 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
8008 free(commit_ref_name);
8009 free(branch_ref_name);
8010 free(fileindex_path);
8012 got_ref_close(commit_ref);
8013 if (base_commit_ref)
8014 got_ref_close(base_commit_ref);
8018 free(*base_commit_id);
8019 *base_commit_id = NULL;
8021 got_ref_close(*tmp_branch);
8025 got_fileindex_free(*fileindex);
8028 lock_worktree(worktree, LOCK_EX);
8033 static const struct got_error *
8034 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
8036 const struct got_error *err;
8037 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
8038 char *branch_ref_name = NULL, *commit_ref_name = NULL;
8040 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
8043 err = delete_ref(tmp_branch_name, repo);
8047 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
8051 err = delete_ref(base_commit_ref_name, repo);
8055 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
8058 err = delete_ref(branch_ref_name, repo);
8062 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8065 err = delete_ref(commit_ref_name, repo);
8069 free(tmp_branch_name);
8070 free(base_commit_ref_name);
8071 free(branch_ref_name);
8072 free(commit_ref_name);
8076 const struct got_error *
8077 got_worktree_histedit_abort(struct got_worktree *worktree,
8078 struct got_fileindex *fileindex, struct got_repository *repo,
8079 struct got_reference *branch, struct got_object_id *base_commit_id,
8080 got_worktree_checkout_cb progress_cb, void *progress_arg)
8082 const struct got_error *err, *unlockerr, *sync_err;
8083 struct got_reference *resolved = NULL;
8084 char *fileindex_path = NULL;
8085 struct got_object_id *merged_commit_id = NULL;
8086 struct got_commit_object *commit = NULL;
8087 char *commit_ref_name = NULL;
8088 struct got_reference *commit_ref = NULL;
8089 struct got_object_id *tree_id = NULL;
8090 struct revert_file_args rfa;
8091 struct got_pathlist_head added_paths;
8093 TAILQ_INIT(&added_paths);
8095 err = lock_worktree(worktree, LOCK_EX);
8099 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8103 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8105 if (err->code != GOT_ERR_NOT_REF)
8107 /* Can happen on early abort due to invalid histedit script. */
8112 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8117 * Determine which files in added status can be safely removed
8118 * from disk while reverting changes in the work tree.
8119 * We want to avoid deleting unrelated files added by the
8120 * user during conflict resolution or during histedit -e.
8122 err = get_paths_added_in_commit(&added_paths, merged_commit_id,
8123 got_worktree_get_path_prefix(worktree), repo);
8128 err = got_ref_open(&resolved, repo,
8129 got_ref_get_symref_target(branch), 0);
8133 err = got_worktree_set_head_ref(worktree, resolved);
8137 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
8141 err = got_object_open_as_commit(&commit, repo,
8142 worktree->base_commit_id);
8146 err = got_object_id_by_path(&tree_id, repo, commit,
8147 worktree->path_prefix);
8151 err = delete_histedit_refs(worktree, repo);
8155 err = get_fileindex_path(&fileindex_path, worktree);
8159 rfa.worktree = worktree;
8160 rfa.fileindex = fileindex;
8161 rfa.progress_cb = progress_cb;
8162 rfa.progress_arg = progress_arg;
8163 rfa.patch_cb = NULL;
8164 rfa.patch_arg = NULL;
8166 rfa.unlink_added_files = 1;
8167 rfa.added_files_to_unlink = &added_paths;
8168 err = worktree_status(worktree, "", fileindex, repo,
8169 revert_file, &rfa, NULL, NULL, 1, 0);
8173 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8174 repo, progress_cb, progress_arg, NULL, NULL);
8176 sync_err = sync_fileindex(fileindex, fileindex_path);
8177 if (sync_err && err == NULL)
8181 got_ref_close(resolved);
8183 got_ref_close(commit_ref);
8184 free(merged_commit_id);
8186 free(fileindex_path);
8187 free(commit_ref_name);
8189 unlockerr = lock_worktree(worktree, LOCK_SH);
8190 if (unlockerr && err == NULL)
8195 const struct got_error *
8196 got_worktree_histedit_complete(struct got_worktree *worktree,
8197 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8198 struct got_reference *edited_branch, struct got_repository *repo)
8200 const struct got_error *err, *unlockerr, *sync_err;
8201 struct got_object_id *new_head_commit_id = NULL;
8202 struct got_reference *resolved = NULL;
8203 char *fileindex_path = NULL;
8205 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
8209 err = got_ref_open(&resolved, repo,
8210 got_ref_get_symref_target(edited_branch), 0);
8214 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
8215 resolved, new_head_commit_id, repo);
8219 err = got_ref_change_ref(resolved, new_head_commit_id);
8223 err = got_ref_write(resolved, repo);
8227 err = got_worktree_set_head_ref(worktree, resolved);
8231 err = delete_histedit_refs(worktree, repo);
8235 err = get_fileindex_path(&fileindex_path, worktree);
8238 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8239 sync_err = sync_fileindex(fileindex, fileindex_path);
8240 if (sync_err && err == NULL)
8243 got_fileindex_free(fileindex);
8244 free(fileindex_path);
8245 free(new_head_commit_id);
8246 unlockerr = lock_worktree(worktree, LOCK_SH);
8247 if (unlockerr && err == NULL)
8252 const struct got_error *
8253 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
8254 struct got_object_id *commit_id, struct got_repository *repo)
8256 const struct got_error *err;
8257 char *commit_ref_name;
8259 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8263 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
8267 err = delete_ref(commit_ref_name, repo);
8269 free(commit_ref_name);
8273 const struct got_error *
8274 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
8275 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
8276 struct got_worktree *worktree, const char *refname,
8277 struct got_repository *repo)
8279 const struct got_error *err = NULL;
8280 char *fileindex_path = NULL;
8281 struct check_rebase_ok_arg ok_arg;
8285 *base_branch_ref = NULL;
8287 err = lock_worktree(worktree, LOCK_EX);
8291 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
8292 err = got_error_msg(GOT_ERR_SAME_BRANCH,
8293 "cannot integrate a branch into itself; "
8294 "update -b or different branch name required");
8298 err = open_fileindex(fileindex, &fileindex_path, worktree);
8302 /* Preconditions are the same as for rebase. */
8303 ok_arg.worktree = worktree;
8305 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8310 err = got_ref_open(branch_ref, repo, refname, 1);
8314 err = got_ref_open(base_branch_ref, repo,
8315 got_worktree_get_head_ref_name(worktree), 1);
8319 got_ref_close(*branch_ref);
8322 if (*base_branch_ref) {
8323 got_ref_close(*base_branch_ref);
8324 *base_branch_ref = NULL;
8327 got_fileindex_free(*fileindex);
8330 lock_worktree(worktree, LOCK_SH);
8335 const struct got_error *
8336 got_worktree_integrate_continue(struct got_worktree *worktree,
8337 struct got_fileindex *fileindex, struct got_repository *repo,
8338 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
8339 got_worktree_checkout_cb progress_cb, void *progress_arg,
8340 got_cancel_cb cancel_cb, void *cancel_arg)
8342 const struct got_error *err = NULL, *sync_err, *unlockerr;
8343 char *fileindex_path = NULL;
8344 struct got_object_id *tree_id = NULL, *commit_id = NULL;
8345 struct got_commit_object *commit = NULL;
8347 err = get_fileindex_path(&fileindex_path, worktree);
8351 err = got_ref_resolve(&commit_id, repo, branch_ref);
8355 err = got_object_open_as_commit(&commit, repo, commit_id);
8359 err = got_object_id_by_path(&tree_id, repo, commit,
8360 worktree->path_prefix);
8364 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
8368 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
8369 progress_cb, progress_arg, cancel_cb, cancel_arg);
8373 err = got_ref_change_ref(base_branch_ref, commit_id);
8377 err = got_ref_write(base_branch_ref, repo);
8381 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8383 sync_err = sync_fileindex(fileindex, fileindex_path);
8384 if (sync_err && err == NULL)
8388 unlockerr = got_ref_unlock(branch_ref);
8389 if (unlockerr && err == NULL)
8391 got_ref_close(branch_ref);
8393 unlockerr = got_ref_unlock(base_branch_ref);
8394 if (unlockerr && err == NULL)
8396 got_ref_close(base_branch_ref);
8398 got_fileindex_free(fileindex);
8399 free(fileindex_path);
8402 got_object_commit_close(commit);
8404 unlockerr = lock_worktree(worktree, LOCK_SH);
8405 if (unlockerr && err == NULL)
8410 const struct got_error *
8411 got_worktree_integrate_abort(struct got_worktree *worktree,
8412 struct got_fileindex *fileindex, struct got_repository *repo,
8413 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
8415 const struct got_error *err = NULL, *unlockerr = NULL;
8417 got_fileindex_free(fileindex);
8419 err = lock_worktree(worktree, LOCK_SH);
8421 unlockerr = got_ref_unlock(branch_ref);
8422 if (unlockerr && err == NULL)
8424 got_ref_close(branch_ref);
8426 unlockerr = got_ref_unlock(base_branch_ref);
8427 if (unlockerr && err == NULL)
8429 got_ref_close(base_branch_ref);
8434 const struct got_error *
8435 got_worktree_merge_postpone(struct got_worktree *worktree,
8436 struct got_fileindex *fileindex)
8438 const struct got_error *err, *sync_err;
8439 char *fileindex_path = NULL;
8441 err = get_fileindex_path(&fileindex_path, worktree);
8445 sync_err = sync_fileindex(fileindex, fileindex_path);
8447 err = lock_worktree(worktree, LOCK_SH);
8448 if (sync_err && err == NULL)
8451 got_fileindex_free(fileindex);
8452 free(fileindex_path);
8456 static const struct got_error *
8457 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
8459 const struct got_error *err;
8460 char *branch_refname = NULL, *commit_refname = NULL;
8462 err = get_merge_branch_ref_name(&branch_refname, worktree);
8465 err = delete_ref(branch_refname, repo);
8469 err = get_merge_commit_ref_name(&commit_refname, worktree);
8472 err = delete_ref(commit_refname, repo);
8477 free(branch_refname);
8478 free(commit_refname);
8482 struct merge_commit_msg_arg {
8483 struct got_worktree *worktree;
8484 const char *branch_name;
8487 static const struct got_error *
8488 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
8489 const char *diff_path, char **logmsg, void *arg)
8491 struct merge_commit_msg_arg *a = arg;
8493 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
8494 got_worktree_get_head_ref_name(a->worktree)) == -1)
8495 return got_error_from_errno("asprintf");
8501 const struct got_error *
8502 got_worktree_merge_branch(struct got_worktree *worktree,
8503 struct got_fileindex *fileindex,
8504 struct got_object_id *yca_commit_id,
8505 struct got_object_id *branch_tip,
8506 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
8507 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
8509 const struct got_error *err;
8510 char *fileindex_path = NULL;
8511 struct check_mixed_commits_args cma;
8513 err = get_fileindex_path(&fileindex_path, worktree);
8517 cma.worktree = worktree;
8518 cma.cancel_cb = cancel_cb;
8519 cma.cancel_arg = cancel_arg;
8521 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
8526 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
8527 branch_tip, repo, progress_cb, progress_arg,
8528 cancel_cb, cancel_arg);
8530 free(fileindex_path);
8534 const struct got_error *
8535 got_worktree_merge_commit(struct got_object_id **new_commit_id,
8536 struct got_worktree *worktree, struct got_fileindex *fileindex,
8537 const char *author, const char *committer, int allow_bad_symlinks,
8538 struct got_object_id *branch_tip, const char *branch_name,
8539 int allow_conflict, struct got_repository *repo,
8540 got_worktree_status_cb status_cb, void *status_arg)
8543 const struct got_error *err = NULL, *sync_err;
8544 struct got_pathlist_head commitable_paths;
8545 struct collect_commitables_arg cc_arg;
8546 struct got_pathlist_entry *pe;
8547 struct got_reference *head_ref = NULL;
8548 struct got_object_id *head_commit_id = NULL;
8549 int have_staged_files = 0;
8550 struct merge_commit_msg_arg mcm_arg;
8551 char *fileindex_path = NULL;
8553 memset(&cc_arg, 0, sizeof(cc_arg));
8554 *new_commit_id = NULL;
8556 TAILQ_INIT(&commitable_paths);
8558 err = get_fileindex_path(&fileindex_path, worktree);
8562 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
8566 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8570 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
8571 &have_staged_files);
8572 if (err && err->code != GOT_ERR_CANCELLED)
8574 if (have_staged_files) {
8575 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
8579 cc_arg.commitable_paths = &commitable_paths;
8580 cc_arg.worktree = worktree;
8581 cc_arg.fileindex = fileindex;
8583 cc_arg.have_staged_files = have_staged_files;
8584 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
8585 cc_arg.commit_conflicts = allow_conflict;
8586 err = worktree_status(worktree, "", fileindex, repo,
8587 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8591 mcm_arg.worktree = worktree;
8592 mcm_arg.branch_name = branch_name;
8593 err = commit_worktree(new_commit_id, &commitable_paths,
8594 head_commit_id, branch_tip, worktree, author, committer, NULL,
8595 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8599 err = update_fileindex_after_commit(worktree, &commitable_paths,
8600 *new_commit_id, fileindex, have_staged_files);
8601 sync_err = sync_fileindex(fileindex, fileindex_path);
8602 if (sync_err && err == NULL)
8605 TAILQ_FOREACH(pe, &commitable_paths, entry) {
8606 struct got_commitable *ct = pe->data;
8608 free_commitable(ct);
8610 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8611 free(fileindex_path);
8615 const struct got_error *
8616 got_worktree_merge_complete(struct got_worktree *worktree,
8617 struct got_fileindex *fileindex, struct got_repository *repo)
8619 const struct got_error *err, *unlockerr, *sync_err;
8620 char *fileindex_path = NULL;
8622 err = delete_merge_refs(worktree, repo);
8626 err = get_fileindex_path(&fileindex_path, worktree);
8629 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8630 sync_err = sync_fileindex(fileindex, fileindex_path);
8631 if (sync_err && err == NULL)
8634 got_fileindex_free(fileindex);
8635 free(fileindex_path);
8636 unlockerr = lock_worktree(worktree, LOCK_SH);
8637 if (unlockerr && err == NULL)
8642 const struct got_error *
8643 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8644 struct got_repository *repo)
8646 const struct got_error *err;
8647 char *branch_refname = NULL;
8648 struct got_reference *branch_ref = NULL;
8652 err = get_merge_branch_ref_name(&branch_refname, worktree);
8655 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8656 free(branch_refname);
8658 if (err->code != GOT_ERR_NOT_REF)
8666 const struct got_error *got_worktree_merge_prepare(
8667 struct got_fileindex **fileindex, struct got_worktree *worktree,
8668 struct got_repository *repo)
8670 const struct got_error *err = NULL;
8671 char *fileindex_path = NULL;
8672 struct got_reference *wt_branch = NULL;
8673 struct got_object_id *wt_branch_tip = NULL;
8674 struct check_rebase_ok_arg ok_arg;
8678 err = lock_worktree(worktree, LOCK_EX);
8682 err = open_fileindex(fileindex, &fileindex_path, worktree);
8686 /* Preconditions are the same as for rebase. */
8687 ok_arg.worktree = worktree;
8689 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8694 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8699 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8703 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8704 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8709 free(fileindex_path);
8711 got_ref_close(wt_branch);
8712 free(wt_branch_tip);
8715 got_fileindex_free(*fileindex);
8718 lock_worktree(worktree, LOCK_SH);
8723 const struct got_error *got_worktree_merge_write_refs(
8724 struct got_worktree *worktree, struct got_reference *branch,
8725 struct got_repository *repo)
8727 const struct got_error *err = NULL;
8728 char *branch_refname = NULL, *commit_refname = NULL;
8729 struct got_reference *branch_ref = NULL, *commit_ref = NULL;
8730 struct got_object_id *branch_tip = NULL;
8732 err = get_merge_branch_ref_name(&branch_refname, worktree);
8736 err = get_merge_commit_ref_name(&commit_refname, worktree);
8740 err = got_ref_resolve(&branch_tip, repo, branch);
8744 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8747 err = got_ref_write(branch_ref, repo);
8751 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8754 err = got_ref_write(commit_ref, repo);
8759 free(branch_refname);
8760 free(commit_refname);
8762 got_ref_close(branch_ref);
8764 got_ref_close(commit_ref);
8769 const struct got_error *
8770 got_worktree_merge_continue(char **branch_name,
8771 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8772 struct got_worktree *worktree, struct got_repository *repo)
8774 const struct got_error *err;
8775 char *commit_refname = NULL, *branch_refname = NULL;
8776 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8777 char *fileindex_path = NULL;
8778 int have_staged_files = 0;
8780 *branch_name = NULL;
8784 err = lock_worktree(worktree, LOCK_EX);
8788 err = open_fileindex(fileindex, &fileindex_path, worktree);
8792 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8793 &have_staged_files);
8794 if (err && err->code != GOT_ERR_CANCELLED)
8796 if (have_staged_files) {
8797 err = got_error(GOT_ERR_STAGED_PATHS);
8801 err = get_merge_branch_ref_name(&branch_refname, worktree);
8805 err = get_merge_commit_ref_name(&commit_refname, worktree);
8809 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8813 if (!got_ref_is_symbolic(branch_ref)) {
8814 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8815 "%s is not a symbolic reference",
8816 got_ref_get_name(branch_ref));
8819 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8820 if (*branch_name == NULL) {
8821 err = got_error_from_errno("strdup");
8825 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8829 err = got_ref_resolve(branch_tip, repo, commit_ref);
8833 free(commit_refname);
8834 free(branch_refname);
8835 free(fileindex_path);
8837 got_ref_close(commit_ref);
8839 got_ref_close(branch_ref);
8843 *branch_name = NULL;
8848 got_fileindex_free(*fileindex);
8851 lock_worktree(worktree, LOCK_SH);
8856 const struct got_error *
8857 got_worktree_merge_abort(struct got_worktree *worktree,
8858 struct got_fileindex *fileindex, struct got_repository *repo,
8859 got_worktree_checkout_cb progress_cb, void *progress_arg)
8861 const struct got_error *err, *unlockerr, *sync_err;
8862 struct got_commit_object *commit = NULL;
8863 char *fileindex_path = NULL;
8864 struct revert_file_args rfa;
8865 char *commit_ref_name = NULL;
8866 struct got_reference *commit_ref = NULL;
8867 struct got_object_id *merged_commit_id = NULL;
8868 struct got_object_id *tree_id = NULL;
8869 struct got_pathlist_head added_paths;
8871 TAILQ_INIT(&added_paths);
8873 err = get_merge_commit_ref_name(&commit_ref_name, worktree);
8877 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8881 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8886 * Determine which files in added status can be safely removed
8887 * from disk while reverting changes in the work tree.
8888 * We want to avoid deleting unrelated files which were added by
8889 * the user for conflict resolution purposes.
8891 err = get_paths_added_between_commits(&added_paths,
8892 got_worktree_get_base_commit_id(worktree), merged_commit_id,
8893 got_worktree_get_path_prefix(worktree), repo);
8898 err = got_object_open_as_commit(&commit, repo,
8899 worktree->base_commit_id);
8903 err = got_object_id_by_path(&tree_id, repo, commit,
8904 worktree->path_prefix);
8908 err = delete_merge_refs(worktree, repo);
8912 err = get_fileindex_path(&fileindex_path, worktree);
8916 rfa.worktree = worktree;
8917 rfa.fileindex = fileindex;
8918 rfa.progress_cb = progress_cb;
8919 rfa.progress_arg = progress_arg;
8920 rfa.patch_cb = NULL;
8921 rfa.patch_arg = NULL;
8923 rfa.unlink_added_files = 1;
8924 rfa.added_files_to_unlink = &added_paths;
8925 err = worktree_status(worktree, "", fileindex, repo,
8926 revert_file, &rfa, NULL, NULL, 1, 0);
8930 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8931 repo, progress_cb, progress_arg, NULL, NULL);
8933 sync_err = sync_fileindex(fileindex, fileindex_path);
8934 if (sync_err && err == NULL)
8938 free(merged_commit_id);
8940 got_object_commit_close(commit);
8942 got_fileindex_free(fileindex);
8943 free(fileindex_path);
8945 got_ref_close(commit_ref);
8946 free(commit_ref_name);
8948 unlockerr = lock_worktree(worktree, LOCK_SH);
8949 if (unlockerr && err == NULL)
8954 struct check_stage_ok_arg {
8955 struct got_object_id *head_commit_id;
8956 struct got_worktree *worktree;
8957 struct got_fileindex *fileindex;
8958 struct got_repository *repo;
8962 static const struct got_error *
8963 check_stage_ok(void *arg, unsigned char status,
8964 unsigned char staged_status, const char *relpath,
8965 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8966 struct got_object_id *commit_id, int dirfd, const char *de_name)
8968 struct check_stage_ok_arg *a = arg;
8969 const struct got_error *err = NULL;
8970 struct got_fileindex_entry *ie;
8971 struct got_object_id base_commit_id;
8972 struct got_object_id *base_commit_idp = NULL;
8973 char *in_repo_path = NULL, *p;
8975 if (status == GOT_STATUS_UNVERSIONED ||
8976 status == GOT_STATUS_NO_CHANGE)
8978 if (status == GOT_STATUS_NONEXISTENT)
8979 return got_error_set_errno(ENOENT, relpath);
8981 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8983 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8985 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8986 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8988 return got_error_from_errno("asprintf");
8990 if (got_fileindex_entry_has_commit(ie)) {
8991 base_commit_idp = got_fileindex_entry_get_commit_id(
8992 &base_commit_id, ie);
8995 if (status == GOT_STATUS_CONFLICT) {
8996 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8998 } else if (status != GOT_STATUS_ADD &&
8999 status != GOT_STATUS_MODIFY &&
9000 status != GOT_STATUS_DELETE) {
9001 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
9005 a->have_changes = 1;
9010 err = check_out_of_date(p, status, staged_status,
9011 blob_id, base_commit_idp, a->head_commit_id, a->repo,
9012 GOT_ERR_STAGE_OUT_OF_DATE);
9018 struct stage_path_arg {
9019 struct got_worktree *worktree;
9020 struct got_fileindex *fileindex;
9021 struct got_repository *repo;
9022 got_worktree_status_cb status_cb;
9024 got_worktree_patch_cb patch_cb;
9026 int staged_something;
9027 int allow_bad_symlinks;
9030 static const struct got_error *
9031 stage_path(void *arg, unsigned char status,
9032 unsigned char staged_status, const char *relpath,
9033 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9034 struct got_object_id *commit_id, int dirfd, const char *de_name)
9036 struct stage_path_arg *a = arg;
9037 const struct got_error *err = NULL;
9038 struct got_fileindex_entry *ie;
9039 char *ondisk_path = NULL, *path_content = NULL;
9041 struct got_object_id *new_staged_blob_id = NULL;
9044 if (status == GOT_STATUS_UNVERSIONED)
9047 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9049 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9051 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
9053 return got_error_from_errno("asprintf");
9056 case GOT_STATUS_ADD:
9057 case GOT_STATUS_MODIFY:
9058 /* XXX could sb.st_mode be passed in by our caller? */
9059 if (lstat(ondisk_path, &sb) == -1) {
9060 err = got_error_from_errno2("lstat", ondisk_path);
9064 if (status == GOT_STATUS_ADD) {
9065 int choice = GOT_PATCH_CHOICE_NONE;
9066 err = (*a->patch_cb)(&choice, a->patch_arg,
9067 status, ie->path, NULL, 1, 1);
9070 if (choice != GOT_PATCH_CHOICE_YES)
9073 err = create_patched_content(&path_content, 0,
9074 staged_blob_id ? staged_blob_id : blob_id,
9075 ondisk_path, dirfd, de_name, ie->path,
9076 a->repo, a->patch_cb, a->patch_arg);
9077 if (err || path_content == NULL)
9081 err = got_object_blob_create(&new_staged_blob_id,
9082 path_content ? path_content : ondisk_path, a->repo);
9085 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9086 SHA1_DIGEST_LENGTH);
9087 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
9088 stage = GOT_FILEIDX_STAGE_ADD;
9090 stage = GOT_FILEIDX_STAGE_MODIFY;
9091 got_fileindex_entry_stage_set(ie, stage);
9092 if (S_ISLNK(sb.st_mode)) {
9093 int is_bad_symlink = 0;
9094 if (!a->allow_bad_symlinks) {
9095 char target_path[PATH_MAX];
9097 target_len = readlink(ondisk_path, target_path,
9098 sizeof(target_path));
9099 if (target_len == -1) {
9100 err = got_error_from_errno2("readlink",
9104 err = is_bad_symlink_target(&is_bad_symlink,
9105 target_path, target_len, ondisk_path,
9106 a->worktree->root_path,
9107 a->worktree->meta_dir);
9110 if (is_bad_symlink) {
9111 err = got_error_path(ondisk_path,
9112 GOT_ERR_BAD_SYMLINK);
9117 got_fileindex_entry_staged_filetype_set(ie,
9118 GOT_FILEIDX_MODE_BAD_SYMLINK);
9120 got_fileindex_entry_staged_filetype_set(ie,
9121 GOT_FILEIDX_MODE_SYMLINK);
9123 got_fileindex_entry_staged_filetype_set(ie,
9124 GOT_FILEIDX_MODE_REGULAR_FILE);
9126 a->staged_something = 1;
9127 if (a->status_cb == NULL)
9129 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
9130 get_staged_status(ie), relpath, blob_id,
9131 new_staged_blob_id, NULL, dirfd, de_name);
9135 * When staging the reverse of the staged diff,
9136 * implicitly unstage the file.
9138 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
9139 sizeof(ie->blob_sha1)) == 0) {
9140 got_fileindex_entry_stage_set(ie,
9141 GOT_FILEIDX_STAGE_NONE);
9144 case GOT_STATUS_DELETE:
9145 if (staged_status == GOT_STATUS_DELETE)
9148 int choice = GOT_PATCH_CHOICE_NONE;
9149 err = (*a->patch_cb)(&choice, a->patch_arg, status,
9150 ie->path, NULL, 1, 1);
9153 if (choice == GOT_PATCH_CHOICE_NO)
9155 if (choice != GOT_PATCH_CHOICE_YES) {
9156 err = got_error(GOT_ERR_PATCH_CHOICE);
9160 stage = GOT_FILEIDX_STAGE_DELETE;
9161 got_fileindex_entry_stage_set(ie, stage);
9162 a->staged_something = 1;
9163 if (a->status_cb == NULL)
9165 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
9166 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
9169 case GOT_STATUS_NO_CHANGE:
9171 case GOT_STATUS_CONFLICT:
9172 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
9174 case GOT_STATUS_NONEXISTENT:
9175 err = got_error_set_errno(ENOENT, relpath);
9178 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
9182 if (path_content && unlink(path_content) == -1 && err == NULL)
9183 err = got_error_from_errno2("unlink", path_content);
9186 free(new_staged_blob_id);
9190 const struct got_error *
9191 got_worktree_stage(struct got_worktree *worktree,
9192 struct got_pathlist_head *paths,
9193 got_worktree_status_cb status_cb, void *status_arg,
9194 got_worktree_patch_cb patch_cb, void *patch_arg,
9195 int allow_bad_symlinks, struct got_repository *repo)
9197 const struct got_error *err = NULL, *sync_err, *unlockerr;
9198 struct got_pathlist_entry *pe;
9199 struct got_fileindex *fileindex = NULL;
9200 char *fileindex_path = NULL;
9201 struct got_reference *head_ref = NULL;
9202 struct got_object_id *head_commit_id = NULL;
9203 struct check_stage_ok_arg oka;
9204 struct stage_path_arg spa;
9206 err = lock_worktree(worktree, LOCK_EX);
9210 err = got_ref_open(&head_ref, repo,
9211 got_worktree_get_head_ref_name(worktree), 0);
9214 err = got_ref_resolve(&head_commit_id, repo, head_ref);
9217 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9221 /* Check pre-conditions before staging anything. */
9222 oka.head_commit_id = head_commit_id;
9223 oka.worktree = worktree;
9224 oka.fileindex = fileindex;
9226 oka.have_changes = 0;
9227 TAILQ_FOREACH(pe, paths, entry) {
9228 err = worktree_status(worktree, pe->path, fileindex, repo,
9229 check_stage_ok, &oka, NULL, NULL, 1, 0);
9233 if (!oka.have_changes) {
9234 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9238 spa.worktree = worktree;
9239 spa.fileindex = fileindex;
9241 spa.patch_cb = patch_cb;
9242 spa.patch_arg = patch_arg;
9243 spa.status_cb = status_cb;
9244 spa.status_arg = status_arg;
9245 spa.staged_something = 0;
9246 spa.allow_bad_symlinks = allow_bad_symlinks;
9247 TAILQ_FOREACH(pe, paths, entry) {
9248 err = worktree_status(worktree, pe->path, fileindex, repo,
9249 stage_path, &spa, NULL, NULL, 1, 0);
9253 if (!spa.staged_something) {
9254 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9258 sync_err = sync_fileindex(fileindex, fileindex_path);
9259 if (sync_err && err == NULL)
9263 got_ref_close(head_ref);
9264 free(head_commit_id);
9265 free(fileindex_path);
9267 got_fileindex_free(fileindex);
9268 unlockerr = lock_worktree(worktree, LOCK_SH);
9269 if (unlockerr && err == NULL)
9274 struct unstage_path_arg {
9275 struct got_worktree *worktree;
9276 struct got_fileindex *fileindex;
9277 struct got_repository *repo;
9278 got_worktree_checkout_cb progress_cb;
9280 got_worktree_patch_cb patch_cb;
9284 static const struct got_error *
9285 create_unstaged_content(char **path_unstaged_content,
9286 char **path_new_staged_content, struct got_object_id *blob_id,
9287 struct got_object_id *staged_blob_id, const char *relpath,
9288 struct got_repository *repo,
9289 got_worktree_patch_cb patch_cb, void *patch_arg)
9291 const struct got_error *err, *free_err;
9292 struct got_blob_object *blob = NULL, *staged_blob = NULL;
9293 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
9294 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
9295 struct got_diffreg_result *diffreg_result = NULL;
9296 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
9297 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
9298 int fd1 = -1, fd2 = -1;
9300 *path_unstaged_content = NULL;
9301 *path_new_staged_content = NULL;
9303 err = got_object_id_str(&label1, blob_id);
9307 fd1 = got_opentempfd();
9309 err = got_error_from_errno("got_opentempfd");
9312 fd2 = got_opentempfd();
9314 err = got_error_from_errno("got_opentempfd");
9318 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
9322 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
9326 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
9330 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
9335 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
9339 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
9343 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
9344 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
9348 err = got_opentemp_named(path_unstaged_content, &outfile,
9349 "got-unstaged-content", "");
9352 err = got_opentemp_named(path_new_staged_content, &rejectfile,
9353 "got-new-staged-content", "");
9357 if (fseek(f1, 0L, SEEK_SET) == -1) {
9358 err = got_ferror(f1, GOT_ERR_IO);
9361 if (fseek(f2, 0L, SEEK_SET) == -1) {
9362 err = got_ferror(f2, GOT_ERR_IO);
9365 /* Count the number of actual changes in the diff result. */
9366 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9367 struct diff_chunk_context cc = {};
9368 diff_chunk_context_load_change(&cc, &nchunks_used,
9369 diffreg_result->result, n, 0);
9372 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9374 err = apply_or_reject_change(&choice, &nchunks_used,
9375 diffreg_result->result, n, relpath, f1, f2,
9376 &line_cur1, &line_cur2,
9377 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
9380 if (choice == GOT_PATCH_CHOICE_YES)
9383 have_rejected_content = 1;
9384 if (choice == GOT_PATCH_CHOICE_QUIT)
9387 if (have_content || have_rejected_content)
9388 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
9389 outfile, rejectfile);
9392 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9393 err = got_error_from_errno("close");
9395 got_object_blob_close(blob);
9396 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9397 err = got_error_from_errno("close");
9399 got_object_blob_close(staged_blob);
9400 free_err = got_diffreg_result_free(diffreg_result);
9401 if (free_err && err == NULL)
9403 if (f1 && fclose(f1) == EOF && err == NULL)
9404 err = got_error_from_errno2("fclose", path1);
9405 if (f2 && fclose(f2) == EOF && err == NULL)
9406 err = got_error_from_errno2("fclose", path2);
9407 if (outfile && fclose(outfile) == EOF && err == NULL)
9408 err = got_error_from_errno2("fclose", *path_unstaged_content);
9409 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
9410 err = got_error_from_errno2("fclose", *path_new_staged_content);
9411 if (path1 && unlink(path1) == -1 && err == NULL)
9412 err = got_error_from_errno2("unlink", path1);
9413 if (path2 && unlink(path2) == -1 && err == NULL)
9414 err = got_error_from_errno2("unlink", path2);
9415 if (err || !have_content) {
9416 if (*path_unstaged_content &&
9417 unlink(*path_unstaged_content) == -1 && err == NULL)
9418 err = got_error_from_errno2("unlink",
9419 *path_unstaged_content);
9420 free(*path_unstaged_content);
9421 *path_unstaged_content = NULL;
9423 if (err || !have_content || !have_rejected_content) {
9424 if (*path_new_staged_content &&
9425 unlink(*path_new_staged_content) == -1 && err == NULL)
9426 err = got_error_from_errno2("unlink",
9427 *path_new_staged_content);
9428 free(*path_new_staged_content);
9429 *path_new_staged_content = NULL;
9436 static const struct got_error *
9437 unstage_hunks(struct got_object_id *staged_blob_id,
9438 struct got_blob_object *blob_base,
9439 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
9440 const char *ondisk_path, const char *label_orig,
9441 struct got_worktree *worktree, struct got_repository *repo,
9442 got_worktree_patch_cb patch_cb, void *patch_arg,
9443 got_worktree_checkout_cb progress_cb, void *progress_arg)
9445 const struct got_error *err = NULL;
9446 char *path_unstaged_content = NULL;
9447 char *path_new_staged_content = NULL;
9448 char *parent = NULL, *base_path = NULL;
9449 char *blob_base_path = NULL;
9450 struct got_object_id *new_staged_blob_id = NULL;
9451 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
9454 err = create_unstaged_content(&path_unstaged_content,
9455 &path_new_staged_content, blob_id, staged_blob_id,
9456 ie->path, repo, patch_cb, patch_arg);
9460 if (path_unstaged_content == NULL)
9463 if (path_new_staged_content) {
9464 err = got_object_blob_create(&new_staged_blob_id,
9465 path_new_staged_content, repo);
9470 f = fopen(path_unstaged_content, "re");
9472 err = got_error_from_errno2("fopen",
9473 path_unstaged_content);
9476 if (fstat(fileno(f), &sb) == -1) {
9477 err = got_error_from_errno2("fstat", path_unstaged_content);
9480 if (got_fileindex_entry_staged_filetype_get(ie) ==
9481 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
9482 char link_target[PATH_MAX];
9484 r = fread(link_target, 1, sizeof(link_target), f);
9485 if (r == 0 && ferror(f)) {
9486 err = got_error_from_errno("fread");
9489 if (r >= sizeof(link_target)) { /* should not happen */
9490 err = got_error(GOT_ERR_NO_SPACE);
9493 link_target[r] = '\0';
9494 err = merge_symlink(worktree, blob_base,
9495 ondisk_path, ie->path, label_orig, link_target,
9496 worktree->base_commit_id, repo, progress_cb,
9499 int local_changes_subsumed;
9501 err = got_path_dirname(&parent, ondisk_path);
9505 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
9507 err = got_error_from_errno("asprintf");
9512 err = got_opentemp_named(&blob_base_path, &f_base,
9516 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
9522 * In order the run a 3-way merge with a symlink we copy the symlink's
9523 * target path into a temporary file and use that file with diff3.
9525 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9526 err = dump_symlink_target_path_to_file(&f_deriv2,
9532 fd = open(ondisk_path,
9533 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
9535 err = got_error_from_errno2("open", ondisk_path);
9538 f_deriv2 = fdopen(fd, "r");
9539 if (f_deriv2 == NULL) {
9540 err = got_error_from_errno2("fdopen", ondisk_path);
9546 err = merge_file(&local_changes_subsumed, worktree,
9547 f_base, f, f_deriv2, ondisk_path, ie->path,
9548 got_fileindex_perms_to_st(ie),
9549 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
9550 repo, progress_cb, progress_arg);
9555 if (new_staged_blob_id) {
9556 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9557 SHA1_DIGEST_LENGTH);
9559 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9560 got_fileindex_entry_staged_filetype_set(ie, 0);
9563 free(new_staged_blob_id);
9564 if (path_unstaged_content &&
9565 unlink(path_unstaged_content) == -1 && err == NULL)
9566 err = got_error_from_errno2("unlink", path_unstaged_content);
9567 if (path_new_staged_content &&
9568 unlink(path_new_staged_content) == -1 && err == NULL)
9569 err = got_error_from_errno2("unlink", path_new_staged_content);
9570 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
9571 err = got_error_from_errno2("unlink", blob_base_path);
9572 if (f_base && fclose(f_base) == EOF && err == NULL)
9573 err = got_error_from_errno2("fclose", path_unstaged_content);
9574 if (f && fclose(f) == EOF && err == NULL)
9575 err = got_error_from_errno2("fclose", path_unstaged_content);
9576 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
9577 err = got_error_from_errno2("fclose", ondisk_path);
9578 free(path_unstaged_content);
9579 free(path_new_staged_content);
9580 free(blob_base_path);
9586 static const struct got_error *
9587 unstage_path(void *arg, unsigned char status,
9588 unsigned char staged_status, const char *relpath,
9589 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9590 struct got_object_id *commit_id, int dirfd, const char *de_name)
9592 const struct got_error *err = NULL;
9593 struct unstage_path_arg *a = arg;
9594 struct got_fileindex_entry *ie;
9595 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
9596 char *ondisk_path = NULL;
9597 char *id_str = NULL, *label_orig = NULL;
9598 int local_changes_subsumed;
9600 int fd1 = -1, fd2 = -1;
9602 if (staged_status != GOT_STATUS_ADD &&
9603 staged_status != GOT_STATUS_MODIFY &&
9604 staged_status != GOT_STATUS_DELETE)
9607 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9609 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9611 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
9613 return got_error_from_errno("asprintf");
9615 err = got_object_id_str(&id_str,
9616 commit_id ? commit_id : a->worktree->base_commit_id);
9619 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9621 err = got_error_from_errno("asprintf");
9625 fd1 = got_opentempfd();
9627 err = got_error_from_errno("got_opentempfd");
9630 fd2 = got_opentempfd();
9632 err = got_error_from_errno("got_opentempfd");
9636 switch (staged_status) {
9637 case GOT_STATUS_MODIFY:
9638 err = got_object_open_as_blob(&blob_base, a->repo,
9639 blob_id, 8192, fd1);
9643 case GOT_STATUS_ADD:
9645 if (staged_status == GOT_STATUS_ADD) {
9646 int choice = GOT_PATCH_CHOICE_NONE;
9647 err = (*a->patch_cb)(&choice, a->patch_arg,
9648 staged_status, ie->path, NULL, 1, 1);
9651 if (choice != GOT_PATCH_CHOICE_YES)
9654 err = unstage_hunks(staged_blob_id,
9655 blob_base, blob_id, ie, ondisk_path,
9656 label_orig, a->worktree, a->repo,
9657 a->patch_cb, a->patch_arg,
9658 a->progress_cb, a->progress_arg);
9659 break; /* Done with this file. */
9662 err = got_object_open_as_blob(&blob_staged, a->repo,
9663 staged_blob_id, 8192, fd2);
9666 switch (got_fileindex_entry_staged_filetype_get(ie)) {
9667 case GOT_FILEIDX_MODE_BAD_SYMLINK:
9668 case GOT_FILEIDX_MODE_REGULAR_FILE:
9669 err = merge_blob(&local_changes_subsumed, a->worktree,
9670 blob_base, ondisk_path, relpath,
9671 got_fileindex_perms_to_st(ie), label_orig,
9672 blob_staged, commit_id ? commit_id :
9673 a->worktree->base_commit_id, a->repo,
9674 a->progress_cb, a->progress_arg);
9676 case GOT_FILEIDX_MODE_SYMLINK:
9677 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9678 char *staged_target;
9679 err = got_object_blob_read_to_str(
9680 &staged_target, blob_staged);
9683 err = merge_symlink(a->worktree, blob_base,
9684 ondisk_path, relpath, label_orig,
9685 staged_target, commit_id ? commit_id :
9686 a->worktree->base_commit_id,
9687 a->repo, a->progress_cb, a->progress_arg);
9688 free(staged_target);
9690 err = merge_blob(&local_changes_subsumed,
9691 a->worktree, blob_base, ondisk_path,
9692 relpath, got_fileindex_perms_to_st(ie),
9693 label_orig, blob_staged,
9694 commit_id ? commit_id :
9695 a->worktree->base_commit_id, a->repo,
9696 a->progress_cb, a->progress_arg);
9700 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9704 got_fileindex_entry_stage_set(ie,
9705 GOT_FILEIDX_STAGE_NONE);
9706 got_fileindex_entry_staged_filetype_set(ie, 0);
9709 case GOT_STATUS_DELETE:
9711 int choice = GOT_PATCH_CHOICE_NONE;
9712 err = (*a->patch_cb)(&choice, a->patch_arg,
9713 staged_status, ie->path, NULL, 1, 1);
9716 if (choice == GOT_PATCH_CHOICE_NO)
9718 if (choice != GOT_PATCH_CHOICE_YES) {
9719 err = got_error(GOT_ERR_PATCH_CHOICE);
9723 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9724 got_fileindex_entry_staged_filetype_set(ie, 0);
9725 err = get_file_status(&status, &sb, ie, ondisk_path,
9726 dirfd, de_name, a->repo);
9729 err = (*a->progress_cb)(a->progress_arg, status, relpath);
9734 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9735 err = got_error_from_errno("close");
9737 got_object_blob_close(blob_base);
9738 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9739 err = got_error_from_errno("close");
9741 got_object_blob_close(blob_staged);
9747 const struct got_error *
9748 got_worktree_unstage(struct got_worktree *worktree,
9749 struct got_pathlist_head *paths,
9750 got_worktree_checkout_cb progress_cb, void *progress_arg,
9751 got_worktree_patch_cb patch_cb, void *patch_arg,
9752 struct got_repository *repo)
9754 const struct got_error *err = NULL, *sync_err, *unlockerr;
9755 struct got_pathlist_entry *pe;
9756 struct got_fileindex *fileindex = NULL;
9757 char *fileindex_path = NULL;
9758 struct unstage_path_arg upa;
9760 err = lock_worktree(worktree, LOCK_EX);
9764 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9768 upa.worktree = worktree;
9769 upa.fileindex = fileindex;
9771 upa.progress_cb = progress_cb;
9772 upa.progress_arg = progress_arg;
9773 upa.patch_cb = patch_cb;
9774 upa.patch_arg = patch_arg;
9775 TAILQ_FOREACH(pe, paths, entry) {
9776 err = worktree_status(worktree, pe->path, fileindex, repo,
9777 unstage_path, &upa, NULL, NULL, 1, 0);
9782 sync_err = sync_fileindex(fileindex, fileindex_path);
9783 if (sync_err && err == NULL)
9786 free(fileindex_path);
9788 got_fileindex_free(fileindex);
9789 unlockerr = lock_worktree(worktree, LOCK_SH);
9790 if (unlockerr && err == NULL)
9795 struct report_file_info_arg {
9796 struct got_worktree *worktree;
9797 got_worktree_path_info_cb info_cb;
9799 struct got_pathlist_head *paths;
9800 got_cancel_cb cancel_cb;
9804 static const struct got_error *
9805 report_file_info(void *arg, struct got_fileindex_entry *ie)
9807 const struct got_error *err;
9808 struct report_file_info_arg *a = arg;
9809 struct got_pathlist_entry *pe;
9810 struct got_object_id blob_id, staged_blob_id, commit_id;
9811 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9812 struct got_object_id *commit_idp = NULL;
9816 err = a->cancel_cb(a->cancel_arg);
9821 TAILQ_FOREACH(pe, a->paths, entry) {
9822 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9823 got_path_is_child(ie->path, pe->path, pe->path_len))
9826 if (pe == NULL) /* not found */
9829 if (got_fileindex_entry_has_blob(ie))
9830 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9831 stage = got_fileindex_entry_stage_get(ie);
9832 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9833 stage == GOT_FILEIDX_STAGE_ADD) {
9834 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9835 &staged_blob_id, ie);
9838 if (got_fileindex_entry_has_commit(ie))
9839 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9841 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9842 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9845 const struct got_error *
9846 got_worktree_path_info(struct got_worktree *worktree,
9847 struct got_pathlist_head *paths,
9848 got_worktree_path_info_cb info_cb, void *info_arg,
9849 got_cancel_cb cancel_cb, void *cancel_arg)
9852 const struct got_error *err = NULL, *unlockerr;
9853 struct got_fileindex *fileindex = NULL;
9854 char *fileindex_path = NULL;
9855 struct report_file_info_arg arg;
9857 err = lock_worktree(worktree, LOCK_SH);
9861 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9865 arg.worktree = worktree;
9866 arg.info_cb = info_cb;
9867 arg.info_arg = info_arg;
9869 arg.cancel_cb = cancel_cb;
9870 arg.cancel_arg = cancel_arg;
9871 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9874 free(fileindex_path);
9876 got_fileindex_free(fileindex);
9877 unlockerr = lock_worktree(worktree, LOCK_UN);
9878 if (unlockerr && err == NULL)
9883 static const struct got_error *
9884 patch_check_path(const char *p, char **path, unsigned char *status,
9885 unsigned char *staged_status, struct got_fileindex *fileindex,
9886 struct got_worktree *worktree, struct got_repository *repo)
9888 const struct got_error *err;
9889 struct got_fileindex_entry *ie;
9891 char *ondisk_path = NULL;
9893 err = got_worktree_resolve_path(path, worktree, p);
9897 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9898 *path[0] ? "/" : "", *path) == -1)
9899 return got_error_from_errno("asprintf");
9901 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9903 *staged_status = get_staged_status(ie);
9904 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9909 *staged_status = GOT_STATUS_NO_CHANGE;
9910 *status = GOT_STATUS_UNVERSIONED;
9911 if (lstat(ondisk_path, &sb) == -1) {
9912 if (errno != ENOENT) {
9913 err = got_error_from_errno2("lstat",
9917 *status = GOT_STATUS_NONEXISTENT;
9926 static const struct got_error *
9927 patch_can_rm(const char *path, unsigned char status,
9928 unsigned char staged_status)
9930 if (status == GOT_STATUS_NONEXISTENT)
9931 return got_error_set_errno(ENOENT, path);
9932 if (status != GOT_STATUS_NO_CHANGE &&
9933 status != GOT_STATUS_ADD &&
9934 status != GOT_STATUS_MODIFY &&
9935 status != GOT_STATUS_MODE_CHANGE)
9936 return got_error_path(path, GOT_ERR_FILE_STATUS);
9937 if (staged_status == GOT_STATUS_DELETE)
9938 return got_error_path(path, GOT_ERR_FILE_STATUS);
9942 static const struct got_error *
9943 patch_can_add(const char *path, unsigned char status)
9945 if (status != GOT_STATUS_NONEXISTENT)
9946 return got_error_path(path, GOT_ERR_FILE_STATUS);
9950 static const struct got_error *
9951 patch_can_edit(const char *path, unsigned char status,
9952 unsigned char staged_status)
9954 if (status == GOT_STATUS_NONEXISTENT)
9955 return got_error_set_errno(ENOENT, path);
9956 if (status != GOT_STATUS_NO_CHANGE &&
9957 status != GOT_STATUS_ADD &&
9958 status != GOT_STATUS_MODIFY)
9959 return got_error_path(path, GOT_ERR_FILE_STATUS);
9960 if (staged_status == GOT_STATUS_DELETE)
9961 return got_error_path(path, GOT_ERR_FILE_STATUS);
9965 const struct got_error *
9966 got_worktree_patch_prepare(struct got_fileindex **fileindex,
9967 char **fileindex_path, struct got_worktree *worktree)
9969 return open_fileindex(fileindex, fileindex_path, worktree);
9972 const struct got_error *
9973 got_worktree_patch_check_path(const char *old, const char *new,
9974 char **oldpath, char **newpath, struct got_worktree *worktree,
9975 struct got_repository *repo, struct got_fileindex *fileindex)
9977 const struct got_error *err = NULL;
9978 int file_renamed = 0;
9979 unsigned char status_old, staged_status_old;
9980 unsigned char status_new, staged_status_new;
9985 err = patch_check_path(old != NULL ? old : new, oldpath,
9986 &status_old, &staged_status_old, fileindex, worktree, repo);
9990 err = patch_check_path(new != NULL ? new : old, newpath,
9991 &status_new, &staged_status_new, fileindex, worktree, repo);
9995 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9998 if (old != NULL && new == NULL)
9999 err = patch_can_rm(*oldpath, status_old, staged_status_old);
10000 else if (file_renamed) {
10001 err = patch_can_rm(*oldpath, status_old, staged_status_old);
10003 err = patch_can_add(*newpath, status_new);
10004 } else if (old == NULL)
10005 err = patch_can_add(*newpath, status_new);
10007 err = patch_can_edit(*newpath, status_new, staged_status_new);
10019 const struct got_error *
10020 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
10021 struct got_worktree *worktree, struct got_fileindex *fileindex,
10022 got_worktree_checkout_cb progress_cb, void *progress_arg)
10024 struct schedule_addition_args saa;
10026 memset(&saa, 0, sizeof(saa));
10027 saa.worktree = worktree;
10028 saa.fileindex = fileindex;
10029 saa.progress_cb = progress_cb;
10030 saa.progress_arg = progress_arg;
10033 return worktree_status(worktree, path, fileindex, repo,
10034 schedule_addition, &saa, NULL, NULL, 1, 0);
10037 const struct got_error *
10038 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
10039 struct got_worktree *worktree, struct got_fileindex *fileindex,
10040 got_worktree_delete_cb progress_cb, void *progress_arg)
10042 const struct got_error *err;
10043 struct schedule_deletion_args sda;
10044 char *ondisk_status_path;
10046 memset(&sda, 0, sizeof(sda));
10047 sda.worktree = worktree;
10048 sda.fileindex = fileindex;
10049 sda.progress_cb = progress_cb;
10050 sda.progress_arg = progress_arg;
10052 sda.delete_local_mods = 0;
10053 sda.keep_on_disk = 0;
10054 sda.ignore_missing_paths = 0;
10055 sda.status_codes = NULL;
10056 if (asprintf(&ondisk_status_path, "%s/%s",
10057 got_worktree_get_root_path(worktree), path) == -1)
10058 return got_error_from_errno("asprintf");
10059 sda.status_path = ondisk_status_path;
10060 sda.status_path_len = strlen(ondisk_status_path);
10062 err = worktree_status(worktree, path, fileindex, repo,
10063 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
10064 free(ondisk_status_path);
10068 const struct got_error *
10069 got_worktree_patch_complete(struct got_fileindex *fileindex,
10070 const char *fileindex_path)
10072 const struct got_error *err = NULL;
10074 err = sync_fileindex(fileindex, fileindex_path);
10075 got_fileindex_free(fileindex);