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_PATH);
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);
3782 match_path(const char *pattern, size_t pattern_len, const char *path,
3788 * Trailing slashes signify directories.
3789 * Append a * to make such patterns conform to fnmatch rules.
3791 if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
3792 if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
3793 return FNM_NOMATCH; /* XXX */
3795 return fnmatch(buf, path, flags);
3798 return fnmatch(pattern, path, flags);
3802 match_ignores(struct got_pathlist_head *ignores, const char *path)
3804 struct got_pathlist_entry *pe;
3806 /* Handle patterns which match in all directories. */
3807 TAILQ_FOREACH(pe, ignores, entry) {
3808 struct got_pathlist_head *ignorelist = pe->data;
3809 struct got_pathlist_entry *pi;
3811 TAILQ_FOREACH(pi, ignorelist, entry) {
3814 if (pi->path_len < 3 ||
3815 strncmp(pi->path, "**/", 3) != 0)
3819 if (match_path(pi->path + 3,
3820 pi->path_len - 3, p,
3821 FNM_PATHNAME | FNM_LEADING_DIR)) {
3822 /* Retry in next directory. */
3823 while (*p && *p != '/')
3835 * The ignores pathlist contains ignore lists from children before
3836 * parents, so we can find the most specific ignorelist by walking
3837 * ignores backwards.
3839 pe = TAILQ_LAST(ignores, got_pathlist_head);
3841 if (got_path_is_child(path, pe->path, pe->path_len)) {
3842 struct got_pathlist_head *ignorelist = pe->data;
3843 struct got_pathlist_entry *pi;
3844 TAILQ_FOREACH(pi, ignorelist, entry) {
3845 int flags = FNM_LEADING_DIR;
3846 if (strstr(pi->path, "/**/") == NULL)
3847 flags |= FNM_PATHNAME;
3848 if (match_path(pi->path, pi->path_len,
3854 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3860 static const struct got_error *
3861 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3862 const char *path, int dirfd, const char *ignores_filename)
3864 const struct got_error *err = NULL;
3867 FILE *ignoresfile = NULL;
3869 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3870 path[0] ? "/" : "", ignores_filename) == -1)
3871 return got_error_from_errno("asprintf");
3874 fd = openat(dirfd, ignores_filename,
3875 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3877 if (errno != ENOENT && errno != EACCES)
3878 err = got_error_from_errno2("openat",
3881 ignoresfile = fdopen(fd, "r");
3882 if (ignoresfile == NULL)
3883 err = got_error_from_errno2("fdopen",
3887 err = read_ignores(ignores, path, ignoresfile);
3891 ignoresfile = fopen(ignorespath, "re");
3892 if (ignoresfile == NULL) {
3893 if (errno != ENOENT && errno != EACCES)
3894 err = got_error_from_errno2("fopen",
3897 err = read_ignores(ignores, path, ignoresfile);
3900 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3901 err = got_error_from_errno2("fclose", path);
3902 if (fd != -1 && close(fd) == -1 && err == NULL)
3903 err = got_error_from_errno2("close", path);
3908 static const struct got_error *
3909 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3912 const struct got_error *err = NULL;
3913 struct diff_dir_cb_arg *a = arg;
3920 err = a->cancel_cb(a->cancel_arg);
3925 if (parent_path[0]) {
3926 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3927 return got_error_from_errno("asprintf");
3932 if (de->d_type == DT_DIR) {
3933 if (!a->no_ignores && ignore != NULL &&
3934 match_ignores(a->ignores, path))
3936 } else if (!match_ignores(a->ignores, path) &&
3937 got_path_is_child(path, a->status_path, a->status_path_len))
3938 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3939 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3945 static const struct got_error *
3946 status_traverse(void *arg, const char *path, int dirfd)
3948 const struct got_error *err = NULL;
3949 struct diff_dir_cb_arg *a = arg;
3954 err = add_ignores(a->ignores, a->worktree->root_path,
3955 path, dirfd, ".cvsignore");
3959 err = add_ignores(a->ignores, a->worktree->root_path, path,
3960 dirfd, ".gitignore");
3965 static const struct got_error *
3966 report_single_file_status(const char *path, const char *ondisk_path,
3967 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3968 void *status_arg, struct got_repository *repo, int report_unchanged,
3969 struct got_pathlist_head *ignores, int no_ignores)
3971 struct got_fileindex_entry *ie;
3974 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3976 return report_file_status(ie, ondisk_path, -1, NULL,
3977 status_cb, status_arg, repo, report_unchanged);
3979 if (lstat(ondisk_path, &sb) == -1) {
3980 if (errno != ENOENT)
3981 return got_error_from_errno2("lstat", ondisk_path);
3982 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3983 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3986 if (!no_ignores && match_ignores(ignores, path))
3989 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3990 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3991 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3996 static const struct got_error *
3997 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3998 const char *root_path, const char *path)
4000 const struct got_error *err;
4001 char *parent_path, *next_parent_path = NULL;
4003 err = add_ignores(ignores, root_path, "", -1,
4008 err = add_ignores(ignores, root_path, "", -1,
4013 err = got_path_dirname(&parent_path, path);
4015 if (err->code == GOT_ERR_BAD_PATH)
4016 return NULL; /* cannot traverse parent */
4020 err = add_ignores(ignores, root_path, parent_path, -1,
4024 err = add_ignores(ignores, root_path, parent_path, -1,
4028 err = got_path_dirname(&next_parent_path, parent_path);
4030 if (err->code == GOT_ERR_BAD_PATH)
4031 err = NULL; /* traversed everything */
4034 if (got_path_is_root_dir(parent_path))
4037 parent_path = next_parent_path;
4038 next_parent_path = NULL;
4042 free(next_parent_path);
4046 struct find_missing_children_args {
4047 const char *parent_path;
4049 struct got_pathlist_head *children;
4050 got_cancel_cb cancel_cb;
4054 static const struct got_error *
4055 find_missing_children(void *arg, struct got_fileindex_entry *ie)
4057 const struct got_error *err = NULL;
4058 struct find_missing_children_args *a = arg;
4061 err = a->cancel_cb(a->cancel_arg);
4066 if (got_path_is_child(ie->path, a->parent_path, a->parent_len))
4067 err = got_pathlist_append(a->children, ie->path, NULL);
4072 static const struct got_error *
4073 report_children(struct got_pathlist_head *children,
4074 struct got_worktree *worktree, struct got_fileindex *fileindex,
4075 struct got_repository *repo, int is_root_dir, int report_unchanged,
4076 struct got_pathlist_head *ignores, int no_ignores,
4077 got_worktree_status_cb status_cb, void *status_arg,
4078 got_cancel_cb cancel_cb, void *cancel_arg)
4080 const struct got_error *err = NULL;
4081 struct got_pathlist_entry *pe;
4082 char *ondisk_path = NULL;
4084 TAILQ_FOREACH(pe, children, entry) {
4086 err = cancel_cb(cancel_arg);
4091 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
4092 !is_root_dir ? "/" : "", pe->path) == -1) {
4093 err = got_error_from_errno("asprintf");
4098 err = report_single_file_status(pe->path, ondisk_path,
4099 fileindex, status_cb, status_arg, repo, report_unchanged,
4100 ignores, no_ignores);
4112 static const struct got_error *
4113 worktree_status(struct got_worktree *worktree, const char *path,
4114 struct got_fileindex *fileindex, struct got_repository *repo,
4115 got_worktree_status_cb status_cb, void *status_arg,
4116 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
4117 int report_unchanged)
4119 const struct got_error *err = NULL;
4121 struct got_fileindex_diff_dir_cb fdiff_cb;
4122 struct diff_dir_cb_arg arg;
4123 char *ondisk_path = NULL;
4124 struct got_pathlist_head ignores, missing_children;
4125 struct got_fileindex_entry *ie;
4127 TAILQ_INIT(&ignores);
4128 TAILQ_INIT(&missing_children);
4130 if (asprintf(&ondisk_path, "%s%s%s",
4131 worktree->root_path, path[0] ? "/" : "", path) == -1)
4132 return got_error_from_errno("asprintf");
4134 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
4136 err = report_single_file_status(path, ondisk_path,
4137 fileindex, status_cb, status_arg, repo,
4138 report_unchanged, &ignores, no_ignores);
4141 struct find_missing_children_args fmca;
4142 fmca.parent_path = path;
4143 fmca.parent_len = strlen(path);
4144 fmca.children = &missing_children;
4145 fmca.cancel_cb = cancel_cb;
4146 fmca.cancel_arg = cancel_arg;
4147 err = got_fileindex_for_each_entry_safe(fileindex,
4148 find_missing_children, &fmca);
4153 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
4155 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
4156 !got_err_open_nofollow_on_symlink())
4157 err = got_error_from_errno2("open", ondisk_path);
4160 err = add_ignores_from_parent_paths(&ignores,
4161 worktree->root_path, ondisk_path);
4165 if (TAILQ_EMPTY(&missing_children)) {
4166 err = report_single_file_status(path,
4167 ondisk_path, fileindex,
4168 status_cb, status_arg, repo,
4169 report_unchanged, &ignores, no_ignores);
4173 err = report_children(&missing_children,
4174 worktree, fileindex, repo,
4175 (path[0] == '\0'), report_unchanged,
4176 &ignores, no_ignores,
4177 status_cb, status_arg,
4178 cancel_cb, cancel_arg);
4184 fdiff_cb.diff_old_new = status_old_new;
4185 fdiff_cb.diff_old = status_old;
4186 fdiff_cb.diff_new = status_new;
4187 fdiff_cb.diff_traverse = status_traverse;
4188 arg.fileindex = fileindex;
4189 arg.worktree = worktree;
4190 arg.status_path = path;
4191 arg.status_path_len = strlen(path);
4193 arg.status_cb = status_cb;
4194 arg.status_arg = status_arg;
4195 arg.cancel_cb = cancel_cb;
4196 arg.cancel_arg = cancel_arg;
4197 arg.report_unchanged = report_unchanged;
4198 arg.no_ignores = no_ignores;
4200 err = add_ignores_from_parent_paths(&ignores,
4201 worktree->root_path, path);
4205 arg.ignores = &ignores;
4206 err = got_fileindex_diff_dir(fileindex, fd,
4207 worktree->root_path, path, repo, &fdiff_cb, &arg);
4210 free_ignores(&ignores);
4211 if (fd != -1 && close(fd) == -1 && err == NULL)
4212 err = got_error_from_errno("close");
4217 const struct got_error *
4218 got_worktree_status(struct got_worktree *worktree,
4219 struct got_pathlist_head *paths, struct got_repository *repo,
4220 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
4221 got_cancel_cb cancel_cb, void *cancel_arg)
4223 const struct got_error *err = NULL;
4224 char *fileindex_path = NULL;
4225 struct got_fileindex *fileindex = NULL;
4226 struct got_pathlist_entry *pe;
4228 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4232 TAILQ_FOREACH(pe, paths, entry) {
4233 err = worktree_status(worktree, pe->path, fileindex, repo,
4234 status_cb, status_arg, cancel_cb, cancel_arg,
4239 free(fileindex_path);
4240 got_fileindex_free(fileindex);
4244 const struct got_error *
4245 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
4248 const struct got_error *err = NULL;
4249 char *resolved = NULL, *cwd = NULL, *path = NULL;
4252 char *abspath = NULL;
4253 char canonpath[PATH_MAX];
4257 cwd = getcwd(NULL, 0);
4259 return got_error_from_errno("getcwd");
4261 if (lstat(arg, &sb) == -1) {
4262 if (errno != ENOENT) {
4263 err = got_error_from_errno2("lstat", arg);
4268 if (S_ISLNK(sb.st_mode)) {
4270 * We cannot use realpath(3) with symlinks since we want to
4271 * operate on the symlink itself.
4272 * But we can make the path absolute, assuming it is relative
4273 * to the current working directory, and then canonicalize it.
4275 if (!got_path_is_absolute(arg)) {
4276 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4277 err = got_error_from_errno("asprintf");
4282 err = got_canonpath(abspath ? abspath : arg, canonpath,
4286 resolved = strdup(canonpath);
4287 if (resolved == NULL) {
4288 err = got_error_from_errno("strdup");
4292 resolved = realpath(arg, NULL);
4293 if (resolved == NULL) {
4294 if (errno != ENOENT) {
4295 err = got_error_from_errno2("realpath", arg);
4298 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4299 err = got_error_from_errno("asprintf");
4302 err = got_canonpath(abspath, canonpath,
4306 resolved = strdup(canonpath);
4307 if (resolved == NULL) {
4308 err = got_error_from_errno("strdup");
4314 if (strncmp(got_worktree_get_root_path(worktree), resolved,
4315 strlen(got_worktree_get_root_path(worktree)))) {
4316 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
4320 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
4321 err = got_path_skip_common_ancestor(&path,
4322 got_worktree_get_root_path(worktree), resolved);
4328 err = got_error_from_errno("strdup");
4333 /* XXX status walk can't deal with trailing slash! */
4335 while (len > 0 && path[len - 1] == '/') {
4336 path[len - 1] = '\0';
4350 struct schedule_addition_args {
4351 struct got_worktree *worktree;
4352 struct got_fileindex *fileindex;
4353 got_worktree_checkout_cb progress_cb;
4355 struct got_repository *repo;
4359 add_noop_status(unsigned char status)
4361 return (status == GOT_STATUS_ADD ||
4362 status == GOT_STATUS_MODIFY ||
4363 status == GOT_STATUS_CONFLICT ||
4364 status == GOT_STATUS_MODE_CHANGE ||
4365 status == GOT_STATUS_NO_CHANGE);
4368 static const struct got_error *
4369 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4370 const char *relpath, struct got_object_id *blob_id,
4371 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4372 int dirfd, const char *de_name)
4374 struct schedule_addition_args *a = arg;
4375 const struct got_error *err = NULL;
4376 struct got_fileindex_entry *ie;
4380 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4382 return got_error_from_errno("asprintf");
4384 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4386 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4390 /* Re-adding an existing entry is a no-op. */
4391 if (staged_status == GOT_STATUS_NO_CHANGE &&
4392 add_noop_status(status))
4394 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4399 if (status != GOT_STATUS_UNVERSIONED) {
4400 if (status == GOT_STATUS_NONEXISTENT)
4401 err = got_error_set_errno(ENOENT, ondisk_path);
4403 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4407 err = got_fileindex_entry_alloc(&ie, relpath);
4410 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4411 relpath, NULL, NULL, 1);
4413 got_fileindex_entry_free(ie);
4416 err = got_fileindex_entry_add(a->fileindex, ie);
4418 got_fileindex_entry_free(ie);
4425 if (staged_status == GOT_STATUS_NO_CHANGE && add_noop_status(status))
4427 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4430 const struct got_error *
4431 got_worktree_schedule_add(struct got_worktree *worktree,
4432 struct got_pathlist_head *paths,
4433 got_worktree_checkout_cb progress_cb, void *progress_arg,
4434 struct got_repository *repo, int no_ignores)
4436 struct got_fileindex *fileindex = NULL;
4437 char *fileindex_path = NULL;
4438 const struct got_error *err = NULL, *sync_err, *unlockerr;
4439 struct got_pathlist_entry *pe;
4440 struct schedule_addition_args saa;
4442 err = lock_worktree(worktree, LOCK_EX);
4446 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4450 saa.worktree = worktree;
4451 saa.fileindex = fileindex;
4452 saa.progress_cb = progress_cb;
4453 saa.progress_arg = progress_arg;
4456 TAILQ_FOREACH(pe, paths, entry) {
4457 err = worktree_status(worktree, pe->path, fileindex, repo,
4458 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4462 sync_err = sync_fileindex(fileindex, fileindex_path);
4463 if (sync_err && err == NULL)
4466 free(fileindex_path);
4468 got_fileindex_free(fileindex);
4469 unlockerr = lock_worktree(worktree, LOCK_SH);
4470 if (unlockerr && err == NULL)
4475 struct schedule_deletion_args {
4476 struct got_worktree *worktree;
4477 struct got_fileindex *fileindex;
4478 got_worktree_delete_cb progress_cb;
4480 struct got_repository *repo;
4481 int delete_local_mods;
4483 int ignore_missing_paths;
4484 const char *status_path;
4485 size_t status_path_len;
4486 const char *status_codes;
4489 static const struct got_error *
4490 schedule_for_deletion(void *arg, unsigned char status,
4491 unsigned char staged_status, const char *relpath,
4492 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4493 struct got_object_id *commit_id, int dirfd, const char *de_name)
4495 struct schedule_deletion_args *a = arg;
4496 const struct got_error *err = NULL;
4497 struct got_fileindex_entry *ie = NULL;
4501 if (status == GOT_STATUS_NONEXISTENT) {
4502 if (a->ignore_missing_paths)
4504 return got_error_set_errno(ENOENT, relpath);
4507 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4509 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4511 staged_status = get_staged_status(ie);
4512 if (staged_status != GOT_STATUS_NO_CHANGE) {
4513 if (staged_status == GOT_STATUS_DELETE)
4515 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4518 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4520 return got_error_from_errno("asprintf");
4522 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4527 if (a->status_codes) {
4528 size_t ncodes = strlen(a->status_codes);
4530 for (i = 0; i < ncodes ; i++) {
4531 if (status == a->status_codes[i])
4535 /* Do not delete files in non-matching status. */
4539 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4540 a->status_codes[i] != GOT_STATUS_MISSING) {
4541 static char msg[64];
4542 snprintf(msg, sizeof(msg),
4543 "invalid status code '%c'", a->status_codes[i]);
4544 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4549 if (status != GOT_STATUS_NO_CHANGE) {
4550 if (status == GOT_STATUS_DELETE)
4552 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4553 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4556 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4557 err = got_error_set_errno(ENOENT, relpath);
4560 if (status != GOT_STATUS_MODIFY &&
4561 status != GOT_STATUS_MISSING) {
4562 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4567 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4571 if (unlinkat(dirfd, de_name, 0) == -1) {
4572 err = got_error_from_errno2("unlinkat",
4576 } else if (unlink(ondisk_path) == -1) {
4577 err = got_error_from_errno2("unlink", ondisk_path);
4581 root_len = strlen(a->worktree->root_path);
4585 err = got_path_dirname(&parent, ondisk_path);
4589 ondisk_path = parent;
4590 if (got_path_cmp(ondisk_path, a->status_path,
4591 strlen(ondisk_path), a->status_path_len) != 0 &&
4592 !got_path_is_child(ondisk_path, a->status_path,
4593 a->status_path_len))
4595 if (rmdir(ondisk_path) == -1) {
4596 if (errno != ENOTEMPTY)
4597 err = got_error_from_errno2("rmdir",
4601 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4602 strlen(ondisk_path), root_len) != 0);
4605 if (got_fileindex_entry_has_blob(ie))
4606 got_fileindex_entry_mark_deleted_from_disk(ie);
4608 got_fileindex_entry_remove(a->fileindex, ie);
4613 if (status == GOT_STATUS_DELETE)
4615 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4616 staged_status, relpath);
4619 const struct got_error *
4620 got_worktree_schedule_delete(struct got_worktree *worktree,
4621 struct got_pathlist_head *paths, int delete_local_mods,
4622 const char *status_codes,
4623 got_worktree_delete_cb progress_cb, void *progress_arg,
4624 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4626 struct got_fileindex *fileindex = NULL;
4627 char *fileindex_path = NULL;
4628 const struct got_error *err = NULL, *sync_err, *unlockerr;
4629 struct got_pathlist_entry *pe;
4630 struct schedule_deletion_args sda;
4632 err = lock_worktree(worktree, LOCK_EX);
4636 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4640 sda.worktree = worktree;
4641 sda.fileindex = fileindex;
4642 sda.progress_cb = progress_cb;
4643 sda.progress_arg = progress_arg;
4645 sda.delete_local_mods = delete_local_mods;
4646 sda.keep_on_disk = keep_on_disk;
4647 sda.ignore_missing_paths = ignore_missing_paths;
4648 sda.status_codes = status_codes;
4650 TAILQ_FOREACH(pe, paths, entry) {
4651 char *ondisk_status_path;
4653 if (asprintf(&ondisk_status_path, "%s%s%s",
4654 got_worktree_get_root_path(worktree),
4655 pe->path[0] == '\0' ? "" : "/", pe->path) == -1) {
4656 err = got_error_from_errno("asprintf");
4659 sda.status_path = ondisk_status_path;
4660 sda.status_path_len = strlen(ondisk_status_path);
4661 err = worktree_status(worktree, pe->path, fileindex, repo,
4662 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4663 free(ondisk_status_path);
4667 sync_err = sync_fileindex(fileindex, fileindex_path);
4668 if (sync_err && err == NULL)
4671 free(fileindex_path);
4673 got_fileindex_free(fileindex);
4674 unlockerr = lock_worktree(worktree, LOCK_SH);
4675 if (unlockerr && err == NULL)
4680 static const struct got_error *
4681 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4683 const struct got_error *err = NULL;
4685 size_t linesize = 0, n;
4688 linelen = getline(&line, &linesize, infile);
4689 if (linelen == -1) {
4690 if (ferror(infile)) {
4691 err = got_error_from_errno("getline");
4697 n = fwrite(line, 1, linelen, outfile);
4699 err = got_ferror(outfile, GOT_ERR_IO);
4704 n = fwrite(line, 1, linelen, rejectfile);
4706 err = got_ferror(rejectfile, GOT_ERR_IO);
4713 static const struct got_error *
4714 skip_one_line(FILE *f)
4717 size_t linesize = 0;
4720 linelen = getline(&line, &linesize, f);
4721 if (linelen == -1) {
4723 return got_error_from_errno("getline");
4730 static const struct got_error *
4731 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4732 int start_old, int end_old, int start_new, int end_new,
4733 FILE *outfile, FILE *rejectfile)
4735 const struct got_error *err;
4737 /* Copy old file's lines leading up to patch. */
4738 while (!feof(f1) && *line_cur1 < start_old) {
4739 err = copy_one_line(f1, outfile, NULL);
4744 /* Skip new file's lines leading up to patch. */
4745 while (!feof(f2) && *line_cur2 < start_new) {
4747 err = copy_one_line(f2, NULL, rejectfile);
4749 err = skip_one_line(f2);
4754 /* Copy patched lines. */
4755 while (!feof(f2) && *line_cur2 <= end_new) {
4756 err = copy_one_line(f2, outfile, NULL);
4761 /* Skip over old file's replaced lines. */
4762 while (!feof(f1) && *line_cur1 <= end_old) {
4764 err = copy_one_line(f1, NULL, rejectfile);
4766 err = skip_one_line(f1);
4775 static const struct got_error *
4776 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4777 FILE *outfile, FILE *rejectfile)
4779 const struct got_error *err;
4782 /* Copy old file's lines until EOF. */
4784 err = copy_one_line(f1, outfile, NULL);
4791 /* Copy new file's lines until EOF. */
4793 err = copy_one_line(f2, NULL, rejectfile);
4803 static const struct got_error *
4804 apply_or_reject_change(int *choice, int *nchunks_used,
4805 struct diff_result *diff_result, int n,
4806 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4807 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4808 got_worktree_patch_cb patch_cb, void *patch_arg)
4810 const struct got_error *err = NULL;
4811 struct diff_chunk_context cc = {};
4812 int start_old, end_old, start_new, end_new;
4814 struct diff_output_unidiff_state *diff_state;
4815 struct diff_input_info diff_info;
4818 *choice = GOT_PATCH_CHOICE_NONE;
4820 /* Get changed line numbers without context lines for copy_change(). */
4821 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4822 start_old = cc.left.start;
4823 end_old = cc.left.end;
4824 start_new = cc.right.start;
4825 end_new = cc.right.end;
4827 /* Get the same change with context lines for display. */
4828 memset(&cc, 0, sizeof(cc));
4829 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4831 memset(&diff_info, 0, sizeof(diff_info));
4832 diff_info.left_path = relpath;
4833 diff_info.right_path = relpath;
4835 diff_state = diff_output_unidiff_state_alloc();
4836 if (diff_state == NULL)
4837 return got_error_set_errno(ENOMEM,
4838 "diff_output_unidiff_state_alloc");
4840 hunkfile = got_opentemp();
4841 if (hunkfile == NULL) {
4842 err = got_error_from_errno("got_opentemp");
4846 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4848 if (rc != DIFF_RC_OK) {
4849 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4853 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4854 err = got_ferror(hunkfile, GOT_ERR_IO);
4858 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4859 hunkfile, changeno, nchanges);
4864 case GOT_PATCH_CHOICE_YES:
4865 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4866 end_old, start_new, end_new, outfile, rejectfile);
4868 case GOT_PATCH_CHOICE_NO:
4869 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4870 end_old, start_new, end_new, rejectfile, outfile);
4872 case GOT_PATCH_CHOICE_QUIT:
4875 err = got_error(GOT_ERR_PATCH_CHOICE);
4879 diff_output_unidiff_state_free(diff_state);
4880 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4881 err = got_error_from_errno("fclose");
4885 struct revert_file_args {
4886 struct got_worktree *worktree;
4887 struct got_fileindex *fileindex;
4888 got_worktree_checkout_cb progress_cb;
4890 got_worktree_patch_cb patch_cb;
4892 struct got_repository *repo;
4893 int unlink_added_files;
4894 struct got_pathlist_head *added_files_to_unlink;
4897 static const struct got_error *
4898 create_patched_content(char **path_outfile, int reverse_patch,
4899 struct got_object_id *blob_id, const char *path2,
4900 int dirfd2, const char *de_name2,
4901 const char *relpath, struct got_repository *repo,
4902 got_worktree_patch_cb patch_cb, void *patch_arg)
4904 const struct got_error *err, *free_err;
4905 struct got_blob_object *blob = NULL;
4906 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4907 int fd = -1, fd2 = -1;
4908 char link_target[PATH_MAX];
4909 ssize_t link_len = 0;
4910 char *path1 = NULL, *id_str = NULL;
4912 struct got_diffreg_result *diffreg_result = NULL;
4913 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4914 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4916 *path_outfile = NULL;
4918 err = got_object_id_str(&id_str, blob_id);
4923 fd2 = openat(dirfd2, de_name2,
4924 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4926 if (!got_err_open_nofollow_on_symlink()) {
4927 err = got_error_from_errno2("openat", path2);
4930 link_len = readlinkat(dirfd2, de_name2,
4931 link_target, sizeof(link_target));
4932 if (link_len == -1) {
4933 return got_error_from_errno2("readlinkat",
4936 sb2.st_mode = S_IFLNK;
4937 sb2.st_size = link_len;
4940 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4942 if (!got_err_open_nofollow_on_symlink()) {
4943 err = got_error_from_errno2("open", path2);
4946 link_len = readlink(path2, link_target,
4947 sizeof(link_target));
4949 return got_error_from_errno2("readlink", path2);
4950 sb2.st_mode = S_IFLNK;
4951 sb2.st_size = link_len;
4955 if (fstat(fd2, &sb2) == -1) {
4956 err = got_error_from_errno2("fstat", path2);
4960 f2 = fdopen(fd2, "r");
4962 err = got_error_from_errno2("fdopen", path2);
4968 f2 = got_opentemp();
4970 err = got_error_from_errno2("got_opentemp", path2);
4973 n = fwrite(link_target, 1, link_len, f2);
4974 if (n != link_len) {
4975 err = got_ferror(f2, GOT_ERR_IO);
4978 if (fflush(f2) == EOF) {
4979 err = got_error_from_errno("fflush");
4985 fd = got_opentempfd();
4987 err = got_error_from_errno("got_opentempfd");
4991 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4995 err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4999 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
5003 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
5004 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
5008 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
5013 if (fseek(f1, 0L, SEEK_SET) == -1)
5014 return got_ferror(f1, GOT_ERR_IO);
5015 if (fseek(f2, 0L, SEEK_SET) == -1)
5016 return got_ferror(f2, GOT_ERR_IO);
5018 /* Count the number of actual changes in the diff result. */
5019 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
5020 struct diff_chunk_context cc = {};
5021 diff_chunk_context_load_change(&cc, &nchunks_used,
5022 diffreg_result->result, n, 0);
5025 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
5027 err = apply_or_reject_change(&choice, &nchunks_used,
5028 diffreg_result->result, n, relpath, f1, f2,
5029 &line_cur1, &line_cur2,
5030 reverse_patch ? NULL : outfile,
5031 reverse_patch ? outfile : NULL,
5032 ++i, nchanges, patch_cb, patch_arg);
5035 if (choice == GOT_PATCH_CHOICE_YES)
5037 else if (choice == GOT_PATCH_CHOICE_QUIT)
5041 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
5042 reverse_patch ? NULL : outfile,
5043 reverse_patch ? outfile : NULL);
5047 if (!S_ISLNK(sb2.st_mode)) {
5050 mode = apply_umask(sb2.st_mode);
5051 if (fchmod(fileno(outfile), mode) == -1) {
5052 err = got_error_from_errno2("fchmod", path2);
5059 if (fd != -1 && close(fd) == -1 && err == NULL)
5060 err = got_error_from_errno("close");
5062 got_object_blob_close(blob);
5063 free_err = got_diffreg_result_free(diffreg_result);
5066 if (f1 && fclose(f1) == EOF && err == NULL)
5067 err = got_error_from_errno2("fclose", path1);
5068 if (f2 && fclose(f2) == EOF && err == NULL)
5069 err = got_error_from_errno2("fclose", path2);
5070 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5071 err = got_error_from_errno2("close", path2);
5072 if (outfile && fclose(outfile) == EOF && err == NULL)
5073 err = got_error_from_errno2("fclose", *path_outfile);
5074 if (path1 && unlink(path1) == -1 && err == NULL)
5075 err = got_error_from_errno2("unlink", path1);
5076 if (err || !have_content) {
5077 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
5078 err = got_error_from_errno2("unlink", *path_outfile);
5079 free(*path_outfile);
5080 *path_outfile = NULL;
5086 static const struct got_error *
5087 revert_file(void *arg, unsigned char status, unsigned char staged_status,
5088 const char *relpath, struct got_object_id *blob_id,
5089 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5090 int dirfd, const char *de_name)
5092 struct revert_file_args *a = arg;
5093 const struct got_error *err = NULL;
5094 char *parent_path = NULL;
5095 struct got_fileindex_entry *ie;
5096 struct got_commit_object *base_commit = NULL;
5097 struct got_tree_object *tree = NULL;
5098 struct got_object_id *tree_id = NULL;
5099 const struct got_tree_entry *te = NULL;
5100 char *tree_path = NULL, *te_name;
5101 char *ondisk_path = NULL, *path_content = NULL;
5102 struct got_blob_object *blob = NULL;
5105 /* Reverting a staged deletion is a no-op. */
5106 if (status == GOT_STATUS_DELETE &&
5107 staged_status != GOT_STATUS_NO_CHANGE)
5110 if (status == GOT_STATUS_UNVERSIONED)
5111 return (*a->progress_cb)(a->progress_arg,
5112 GOT_STATUS_UNVERSIONED, relpath);
5114 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5116 return got_error_path(relpath, GOT_ERR_BAD_PATH);
5118 /* Construct in-repository path of tree which contains this blob. */
5119 err = got_path_dirname(&parent_path, ie->path);
5121 if (err->code != GOT_ERR_BAD_PATH)
5123 parent_path = strdup("/");
5124 if (parent_path == NULL) {
5125 err = got_error_from_errno("strdup");
5129 if (got_path_is_root_dir(a->worktree->path_prefix)) {
5130 tree_path = strdup(parent_path);
5131 if (tree_path == NULL) {
5132 err = got_error_from_errno("strdup");
5136 if (got_path_is_root_dir(parent_path)) {
5137 tree_path = strdup(a->worktree->path_prefix);
5138 if (tree_path == NULL) {
5139 err = got_error_from_errno("strdup");
5143 if (asprintf(&tree_path, "%s/%s",
5144 a->worktree->path_prefix, parent_path) == -1) {
5145 err = got_error_from_errno("asprintf");
5151 err = got_object_open_as_commit(&base_commit, a->repo,
5152 a->worktree->base_commit_id);
5156 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
5158 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
5159 (status == GOT_STATUS_ADD ||
5160 staged_status == GOT_STATUS_ADD)))
5163 err = got_object_open_as_tree(&tree, a->repo, tree_id);
5167 err = got_path_basename(&te_name, ie->path);
5171 te = got_object_tree_find_entry(tree, te_name);
5173 if (te == NULL && status != GOT_STATUS_ADD &&
5174 staged_status != GOT_STATUS_ADD) {
5175 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
5181 case GOT_STATUS_ADD:
5183 int choice = GOT_PATCH_CHOICE_NONE;
5184 err = (*a->patch_cb)(&choice, a->patch_arg,
5185 status, ie->path, NULL, 1, 1);
5188 if (choice != GOT_PATCH_CHOICE_YES)
5191 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
5195 got_fileindex_entry_remove(a->fileindex, ie);
5196 if (a->unlink_added_files) {
5197 int do_unlink = a->added_files_to_unlink ? 0 : 1;
5199 if (a->added_files_to_unlink) {
5200 struct got_pathlist_entry *pe;
5202 TAILQ_FOREACH(pe, a->added_files_to_unlink,
5204 if (got_path_cmp(pe->path, relpath,
5205 pe->path_len, strlen(relpath)))
5213 if (asprintf(&ondisk_path, "%s/%s",
5214 got_worktree_get_root_path(a->worktree),
5216 err = got_error_from_errno("asprintf");
5219 if (unlink(ondisk_path) == -1) {
5220 err = got_error_from_errno2("unlink",
5227 case GOT_STATUS_DELETE:
5229 int choice = GOT_PATCH_CHOICE_NONE;
5230 err = (*a->patch_cb)(&choice, a->patch_arg,
5231 status, ie->path, NULL, 1, 1);
5234 if (choice != GOT_PATCH_CHOICE_YES)
5238 case GOT_STATUS_MODIFY:
5239 case GOT_STATUS_MODE_CHANGE:
5240 case GOT_STATUS_CONFLICT:
5241 case GOT_STATUS_MISSING: {
5242 struct got_object_id id;
5243 if (staged_status == GOT_STATUS_ADD ||
5244 staged_status == GOT_STATUS_MODIFY)
5245 got_fileindex_entry_get_staged_blob_id(&id, ie);
5247 got_fileindex_entry_get_blob_id(&id, ie);
5248 fd = got_opentempfd();
5250 err = got_error_from_errno("got_opentempfd");
5254 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
5258 if (asprintf(&ondisk_path, "%s/%s",
5259 got_worktree_get_root_path(a->worktree), relpath) == -1) {
5260 err = got_error_from_errno("asprintf");
5264 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
5265 status == GOT_STATUS_CONFLICT)) {
5266 int is_bad_symlink = 0;
5267 err = create_patched_content(&path_content, 1, &id,
5268 ondisk_path, dirfd, de_name, ie->path, a->repo,
5269 a->patch_cb, a->patch_arg);
5270 if (err || path_content == NULL)
5272 if (te && S_ISLNK(te->mode)) {
5273 if (unlink(path_content) == -1) {
5274 err = got_error_from_errno2("unlink",
5278 err = install_symlink(&is_bad_symlink,
5279 a->worktree, ondisk_path, ie->path,
5280 blob, 0, 1, 0, 0, a->repo,
5281 a->progress_cb, a->progress_arg);
5283 if (rename(path_content, ondisk_path) == -1) {
5284 err = got_error_from_errno3("rename",
5285 path_content, ondisk_path);
5290 int is_bad_symlink = 0;
5291 if (te && S_ISLNK(te->mode)) {
5292 err = install_symlink(&is_bad_symlink,
5293 a->worktree, ondisk_path, ie->path,
5294 blob, 0, 1, 0, 0, a->repo,
5295 a->progress_cb, a->progress_arg);
5297 err = install_blob(a->worktree, ondisk_path,
5299 te ? te->mode : GOT_DEFAULT_FILE_MODE,
5300 got_fileindex_perms_to_st(ie), blob,
5301 0, 1, 0, 0, NULL, a->repo,
5302 a->progress_cb, a->progress_arg);
5306 if (status == GOT_STATUS_DELETE ||
5307 status == GOT_STATUS_MODE_CHANGE) {
5308 err = got_fileindex_entry_update(ie,
5309 a->worktree->root_fd, relpath,
5311 a->worktree->base_commit_id->sha1, 1);
5315 if (is_bad_symlink) {
5316 got_fileindex_entry_filetype_set(ie,
5317 GOT_FILEIDX_MODE_BAD_SYMLINK);
5330 if (fd != -1 && close(fd) == -1 && err == NULL)
5331 err = got_error_from_errno("close");
5333 got_object_blob_close(blob);
5335 got_object_tree_close(tree);
5338 got_object_commit_close(base_commit);
5342 const struct got_error *
5343 got_worktree_revert(struct got_worktree *worktree,
5344 struct got_pathlist_head *paths,
5345 got_worktree_checkout_cb progress_cb, void *progress_arg,
5346 got_worktree_patch_cb patch_cb, void *patch_arg,
5347 struct got_repository *repo)
5349 struct got_fileindex *fileindex = NULL;
5350 char *fileindex_path = NULL;
5351 const struct got_error *err = NULL, *unlockerr = NULL;
5352 const struct got_error *sync_err = NULL;
5353 struct got_pathlist_entry *pe;
5354 struct revert_file_args rfa;
5356 err = lock_worktree(worktree, LOCK_EX);
5360 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5364 rfa.worktree = worktree;
5365 rfa.fileindex = fileindex;
5366 rfa.progress_cb = progress_cb;
5367 rfa.progress_arg = progress_arg;
5368 rfa.patch_cb = patch_cb;
5369 rfa.patch_arg = patch_arg;
5371 rfa.unlink_added_files = 0;
5372 TAILQ_FOREACH(pe, paths, entry) {
5373 err = worktree_status(worktree, pe->path, fileindex, repo,
5374 revert_file, &rfa, NULL, NULL, 1, 0);
5378 sync_err = sync_fileindex(fileindex, fileindex_path);
5379 if (sync_err && err == NULL)
5382 free(fileindex_path);
5384 got_fileindex_free(fileindex);
5385 unlockerr = lock_worktree(worktree, LOCK_SH);
5386 if (unlockerr && err == NULL)
5392 free_commitable(struct got_commitable *ct)
5395 free(ct->in_repo_path);
5396 free(ct->ondisk_path);
5398 free(ct->base_blob_id);
5399 free(ct->staged_blob_id);
5400 free(ct->base_commit_id);
5404 struct collect_commitables_arg {
5405 struct got_pathlist_head *commitable_paths;
5406 struct got_repository *repo;
5407 struct got_worktree *worktree;
5408 struct got_fileindex *fileindex;
5409 int have_staged_files;
5410 int allow_bad_symlinks;
5411 int diff_header_shown;
5412 int commit_conflicts;
5419 * Create a file which contains the target path of a symlink so we can feed
5420 * it as content to the diff engine.
5422 static const struct got_error *
5423 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5424 const char *abspath)
5426 const struct got_error *err = NULL;
5427 char target_path[PATH_MAX];
5428 ssize_t target_len, outlen;
5433 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5434 if (target_len == -1)
5435 return got_error_from_errno2("readlinkat", abspath);
5437 target_len = readlink(abspath, target_path, PATH_MAX);
5438 if (target_len == -1)
5439 return got_error_from_errno2("readlink", abspath);
5442 *fd = got_opentempfd();
5444 return got_error_from_errno("got_opentempfd");
5446 outlen = write(*fd, target_path, target_len);
5448 err = got_error_from_errno("got_opentempfd");
5452 if (lseek(*fd, 0, SEEK_SET) == -1) {
5453 err = got_error_from_errno2("lseek", abspath);
5464 static const struct got_error *
5465 append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5466 FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5467 int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5469 const struct got_error *err = NULL;
5470 struct got_blob_object *blob1 = NULL;
5471 int fd = -1, fd1 = -1, fd2 = -1;
5472 FILE *ondisk_file = NULL;
5473 char *label1 = NULL;
5477 char *id_str = NULL;
5479 memset(&sb, 0, sizeof(sb));
5482 if (ct->staged_status != GOT_STATUS_MODIFY &&
5483 ct->staged_status != GOT_STATUS_ADD &&
5484 ct->staged_status != GOT_STATUS_DELETE)
5487 if (ct->status != GOT_STATUS_MODIFY &&
5488 ct->status != GOT_STATUS_ADD &&
5489 ct->status != GOT_STATUS_DELETE &&
5490 ct->status != GOT_STATUS_CONFLICT)
5494 err = got_opentemp_truncate(f1);
5496 return got_error_from_errno("got_opentemp_truncate");
5497 err = got_opentemp_truncate(f2);
5499 return got_error_from_errno("got_opentemp_truncate");
5501 if (!*diff_header_shown) {
5502 err = got_object_id_str(&id_str, worktree->base_commit_id);
5505 fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5506 got_worktree_get_root_path(worktree));
5507 fprintf(diff_outfile, "commit - %s\n", id_str);
5508 fprintf(diff_outfile, "path + %s%s\n",
5509 got_worktree_get_root_path(worktree),
5510 diff_staged ? " (staged changes)" : "");
5511 *diff_header_shown = 1;
5515 const char *label1 = NULL, *label2 = NULL;
5516 switch (ct->staged_status) {
5517 case GOT_STATUS_MODIFY:
5521 case GOT_STATUS_ADD:
5524 case GOT_STATUS_DELETE:
5528 return got_error(GOT_ERR_FILE_STATUS);
5530 fd1 = got_opentempfd();
5532 err = got_error_from_errno("got_opentempfd");
5535 fd2 = got_opentempfd();
5537 err = got_error_from_errno("got_opentempfd");
5540 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5541 fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5542 label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5543 NULL, repo, diff_outfile);
5547 fd1 = got_opentempfd();
5549 err = got_error_from_errno("got_opentempfd");
5553 if (ct->status != GOT_STATUS_ADD) {
5554 err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5560 if (ct->status != GOT_STATUS_DELETE) {
5562 fd = openat(dirfd, de_name,
5563 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5565 if (!got_err_open_nofollow_on_symlink()) {
5566 err = got_error_from_errno2("openat",
5570 err = get_symlink_target_file(&fd, dirfd,
5571 de_name, ct->ondisk_path);
5576 fd = open(ct->ondisk_path,
5577 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5579 if (!got_err_open_nofollow_on_symlink()) {
5580 err = got_error_from_errno2("open",
5584 err = get_symlink_target_file(&fd, dirfd,
5585 de_name, ct->ondisk_path);
5590 if (fstatat(fd, ct->ondisk_path, &sb,
5591 AT_SYMLINK_NOFOLLOW) == -1) {
5592 err = got_error_from_errno2("fstatat", ct->ondisk_path);
5595 ondisk_file = fdopen(fd, "r");
5596 if (ondisk_file == NULL) {
5597 err = got_error_from_errno2("fdopen", ct->ondisk_path);
5605 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5611 err = got_diff_blob_file(blob1, f1, size1, label1,
5612 ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5613 GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5615 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5616 err = got_error_from_errno("close");
5617 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5618 err = got_error_from_errno("close");
5620 got_object_blob_close(blob1);
5621 if (fd != -1 && close(fd) == -1 && err == NULL)
5622 err = got_error_from_errno("close");
5623 if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5624 err = got_error_from_errno("fclose");
5628 static const struct got_error *
5629 collect_commitables(void *arg, unsigned char status,
5630 unsigned char staged_status, const char *relpath,
5631 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5632 struct got_object_id *commit_id, int dirfd, const char *de_name)
5634 struct collect_commitables_arg *a = arg;
5635 const struct got_error *err = NULL;
5636 struct got_commitable *ct = NULL;
5637 struct got_pathlist_entry *new = NULL;
5638 char *parent_path = NULL, *path = NULL;
5641 if (a->have_staged_files) {
5642 if (staged_status != GOT_STATUS_MODIFY &&
5643 staged_status != GOT_STATUS_ADD &&
5644 staged_status != GOT_STATUS_DELETE)
5647 if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5648 printf("C %s\n", relpath);
5649 return got_error(GOT_ERR_COMMIT_CONFLICT);
5652 if (status != GOT_STATUS_MODIFY &&
5653 status != GOT_STATUS_MODE_CHANGE &&
5654 status != GOT_STATUS_ADD &&
5655 status != GOT_STATUS_DELETE &&
5656 status != GOT_STATUS_CONFLICT)
5660 if (asprintf(&path, "/%s", relpath) == -1) {
5661 err = got_error_from_errno("asprintf");
5664 if (strcmp(path, "/") == 0) {
5665 parent_path = strdup("");
5666 if (parent_path == NULL)
5667 return got_error_from_errno("strdup");
5669 err = got_path_dirname(&parent_path, path);
5674 ct = calloc(1, sizeof(*ct));
5676 err = got_error_from_errno("calloc");
5680 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5682 err = got_error_from_errno("asprintf");
5686 if (staged_status == GOT_STATUS_ADD ||
5687 staged_status == GOT_STATUS_MODIFY) {
5688 struct got_fileindex_entry *ie;
5689 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5690 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5691 case GOT_FILEIDX_MODE_REGULAR_FILE:
5692 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5695 case GOT_FILEIDX_MODE_SYMLINK:
5699 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5702 ct->mode |= got_fileindex_entry_perms_get(ie);
5703 } else if (status != GOT_STATUS_DELETE &&
5704 staged_status != GOT_STATUS_DELETE) {
5706 if (fstatat(dirfd, de_name, &sb,
5707 AT_SYMLINK_NOFOLLOW) == -1) {
5708 err = got_error_from_errno2("fstatat",
5712 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5713 err = got_error_from_errno2("lstat", ct->ondisk_path);
5716 ct->mode = sb.st_mode;
5719 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5720 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5722 err = got_error_from_errno("asprintf");
5726 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5727 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5729 char target_path[PATH_MAX];
5731 target_len = readlink(ct->ondisk_path, target_path,
5732 sizeof(target_path));
5733 if (target_len == -1) {
5734 err = got_error_from_errno2("readlink",
5738 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5739 target_len, ct->ondisk_path, a->worktree->root_path,
5740 a->worktree->meta_dir);
5743 if (is_bad_symlink) {
5744 err = got_error_path(ct->ondisk_path,
5745 GOT_ERR_BAD_SYMLINK);
5751 ct->status = status;
5752 ct->staged_status = staged_status;
5753 ct->blob_id = NULL; /* will be filled in when blob gets created */
5754 if (ct->status != GOT_STATUS_ADD &&
5755 ct->staged_status != GOT_STATUS_ADD) {
5756 ct->base_blob_id = got_object_id_dup(blob_id);
5757 if (ct->base_blob_id == NULL) {
5758 err = got_error_from_errno("got_object_id_dup");
5761 ct->base_commit_id = got_object_id_dup(commit_id);
5762 if (ct->base_commit_id == NULL) {
5763 err = got_error_from_errno("got_object_id_dup");
5767 if (ct->staged_status == GOT_STATUS_ADD ||
5768 ct->staged_status == GOT_STATUS_MODIFY) {
5769 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5770 if (ct->staged_blob_id == NULL) {
5771 err = got_error_from_errno("got_object_id_dup");
5775 ct->path = strdup(path);
5776 if (ct->path == NULL) {
5777 err = got_error_from_errno("strdup");
5781 if (a->diff_outfile) {
5782 err = append_ct_diff(ct, &a->diff_header_shown,
5783 a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5784 a->have_staged_files, a->repo, a->worktree);
5789 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5791 if (ct && (err || new == NULL))
5792 free_commitable(ct);
5798 static const struct got_error *write_tree(struct got_object_id **, int *,
5799 struct got_tree_object *, const char *, struct got_pathlist_head *,
5800 got_worktree_status_cb status_cb, void *status_arg,
5801 struct got_repository *);
5803 static const struct got_error *
5804 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5805 struct got_tree_entry *te, const char *parent_path,
5806 struct got_pathlist_head *commitable_paths,
5807 got_worktree_status_cb status_cb, void *status_arg,
5808 struct got_repository *repo)
5810 const struct got_error *err = NULL;
5811 struct got_tree_object *subtree;
5814 if (asprintf(&subpath, "%s%s%s", parent_path,
5815 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5816 return got_error_from_errno("asprintf");
5818 err = got_object_open_as_tree(&subtree, repo, &te->id);
5822 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5823 commitable_paths, status_cb, status_arg, repo);
5824 got_object_tree_close(subtree);
5829 static const struct got_error *
5830 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5832 const struct got_error *err = NULL;
5833 char *ct_parent_path = NULL;
5837 if (strchr(ct->in_repo_path, '/') == NULL) {
5838 *match = got_path_is_root_dir(path);
5842 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5845 *match = (strcmp(path, ct_parent_path) == 0);
5846 free(ct_parent_path);
5851 get_ct_file_mode(struct got_commitable *ct)
5853 if (S_ISLNK(ct->mode))
5856 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5859 static const struct got_error *
5860 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5861 struct got_tree_entry *te, struct got_commitable *ct)
5863 const struct got_error *err = NULL;
5867 err = got_object_tree_entry_dup(new_te, te);
5871 (*new_te)->mode = get_ct_file_mode(ct);
5873 if (ct->staged_status == GOT_STATUS_MODIFY)
5874 memcpy(&(*new_te)->id, ct->staged_blob_id,
5875 sizeof((*new_te)->id));
5877 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5879 if (err && *new_te) {
5886 static const struct got_error *
5887 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5888 struct got_commitable *ct)
5890 const struct got_error *err = NULL;
5891 char *ct_name = NULL;
5895 *new_te = calloc(1, sizeof(**new_te));
5896 if (*new_te == NULL)
5897 return got_error_from_errno("calloc");
5899 err = got_path_basename(&ct_name, ct->path);
5902 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5903 sizeof((*new_te)->name)) {
5904 err = got_error(GOT_ERR_NO_SPACE);
5908 (*new_te)->mode = get_ct_file_mode(ct);
5910 if (ct->staged_status == GOT_STATUS_ADD)
5911 memcpy(&(*new_te)->id, ct->staged_blob_id,
5912 sizeof((*new_te)->id));
5914 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5917 if (err && *new_te) {
5924 static const struct got_error *
5925 insert_tree_entry(struct got_tree_entry *new_te,
5926 struct got_pathlist_head *paths)
5928 const struct got_error *err = NULL;
5929 struct got_pathlist_entry *new_pe;
5931 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5935 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5939 static const struct got_error *
5940 report_ct_status(struct got_commitable *ct,
5941 got_worktree_status_cb status_cb, void *status_arg)
5943 const char *ct_path = ct->path;
5944 unsigned char status;
5946 if (status_cb == NULL) /* no commit progress output desired */
5949 while (ct_path[0] == '/')
5952 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5953 status = ct->staged_status;
5955 status = ct->status;
5957 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5958 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5961 static const struct got_error *
5962 match_modified_subtree(int *modified, struct got_tree_entry *te,
5963 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5965 const struct got_error *err = NULL;
5966 struct got_pathlist_entry *pe;
5971 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5972 got_path_is_root_dir(base_tree_path) ? "" : "/",
5974 return got_error_from_errno("asprintf");
5976 TAILQ_FOREACH(pe, commitable_paths, entry) {
5977 struct got_commitable *ct = pe->data;
5978 *modified = got_path_is_child(ct->in_repo_path, te_path,
5988 static const struct got_error *
5989 match_deleted_or_modified_ct(struct got_commitable **ctp,
5990 struct got_tree_entry *te, const char *base_tree_path,
5991 struct got_pathlist_head *commitable_paths)
5993 const struct got_error *err = NULL;
5994 struct got_pathlist_entry *pe;
5998 TAILQ_FOREACH(pe, commitable_paths, entry) {
5999 struct got_commitable *ct = pe->data;
6000 char *ct_name = NULL;
6003 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
6004 if (ct->status != GOT_STATUS_MODIFY &&
6005 ct->status != GOT_STATUS_MODE_CHANGE &&
6006 ct->status != GOT_STATUS_DELETE &&
6007 ct->status != GOT_STATUS_CONFLICT)
6010 if (ct->staged_status != GOT_STATUS_MODIFY &&
6011 ct->staged_status != GOT_STATUS_DELETE)
6015 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
6018 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
6024 err = got_path_basename(&ct_name, pe->path);
6028 if (strcmp(te->name, ct_name) != 0) {
6041 static const struct got_error *
6042 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
6043 const char *child_path, const char *path_base_tree,
6044 struct got_pathlist_head *commitable_paths,
6045 got_worktree_status_cb status_cb, void *status_arg,
6046 struct got_repository *repo)
6048 const struct got_error *err = NULL;
6049 struct got_tree_entry *new_te;
6051 struct got_object_id *id = NULL;
6056 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
6057 got_path_is_root_dir(path_base_tree) ? "" : "/",
6059 return got_error_from_errno("asprintf");
6061 new_te = calloc(1, sizeof(*new_te));
6063 return got_error_from_errno("calloc");
6064 new_te->mode = S_IFDIR;
6066 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
6067 sizeof(new_te->name)) {
6068 err = got_error(GOT_ERR_NO_SPACE);
6071 err = write_tree(&id, &nentries, NULL, subtree_path,
6072 commitable_paths, status_cb, status_arg, repo);
6077 memcpy(&new_te->id, id, sizeof(new_te->id));
6086 static const struct got_error *
6087 write_tree(struct got_object_id **new_tree_id, int *nentries,
6088 struct got_tree_object *base_tree, const char *path_base_tree,
6089 struct got_pathlist_head *commitable_paths,
6090 got_worktree_status_cb status_cb, void *status_arg,
6091 struct got_repository *repo)
6093 const struct got_error *err = NULL;
6094 struct got_pathlist_head paths;
6095 struct got_tree_entry *te, *new_te = NULL;
6096 struct got_pathlist_entry *pe;
6101 /* Insert, and recurse into, newly added entries first. */
6102 TAILQ_FOREACH(pe, commitable_paths, entry) {
6103 struct got_commitable *ct = pe->data;
6104 char *child_path = NULL, *slash;
6106 if ((ct->status != GOT_STATUS_ADD &&
6107 ct->staged_status != GOT_STATUS_ADD) ||
6108 (ct->flags & GOT_COMMITABLE_ADDED))
6111 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
6112 strlen(path_base_tree)))
6115 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
6120 slash = strchr(child_path, '/');
6121 if (slash == NULL) {
6122 err = alloc_added_blob_tree_entry(&new_te, ct);
6125 err = report_ct_status(ct, status_cb, status_arg);
6128 ct->flags |= GOT_COMMITABLE_ADDED;
6129 err = insert_tree_entry(new_te, &paths);
6134 *slash = '\0'; /* trim trailing path components */
6135 if (base_tree == NULL ||
6136 got_object_tree_find_entry(base_tree, child_path)
6138 err = make_subtree_for_added_blob(&new_te,
6139 child_path, path_base_tree,
6140 commitable_paths, status_cb, status_arg,
6144 err = insert_tree_entry(new_te, &paths);
6153 int i, nbase_entries;
6154 /* Handle modified and deleted entries. */
6155 nbase_entries = got_object_tree_get_nentries(base_tree);
6156 for (i = 0; i < nbase_entries; i++) {
6157 struct got_commitable *ct = NULL;
6159 te = got_object_tree_get_entry(base_tree, i);
6160 if (got_object_tree_entry_is_submodule(te)) {
6161 /* Entry is a submodule; just copy it. */
6162 err = got_object_tree_entry_dup(&new_te, te);
6165 err = insert_tree_entry(new_te, &paths);
6172 if (S_ISDIR(te->mode)) {
6174 err = got_object_tree_entry_dup(&new_te, te);
6177 err = match_modified_subtree(&modified, te,
6178 path_base_tree, commitable_paths);
6181 /* Avoid recursion into unmodified subtrees. */
6183 struct got_object_id *new_id;
6185 err = write_subtree(&new_id,
6187 path_base_tree, commitable_paths,
6188 status_cb, status_arg, repo);
6191 if (nsubentries == 0) {
6192 /* All entries were deleted. */
6196 memcpy(&new_te->id, new_id,
6197 sizeof(new_te->id));
6200 err = insert_tree_entry(new_te, &paths);
6207 err = match_deleted_or_modified_ct(&ct, te,
6208 path_base_tree, commitable_paths);
6212 /* NB: Deleted entries get dropped here. */
6213 if (ct->status == GOT_STATUS_MODIFY ||
6214 ct->status == GOT_STATUS_MODE_CHANGE ||
6215 ct->status == GOT_STATUS_CONFLICT ||
6216 ct->staged_status == GOT_STATUS_MODIFY) {
6217 err = alloc_modified_blob_tree_entry(
6221 err = insert_tree_entry(new_te, &paths);
6226 err = report_ct_status(ct, status_cb,
6231 /* Entry is unchanged; just copy it. */
6232 err = got_object_tree_entry_dup(&new_te, te);
6235 err = insert_tree_entry(new_te, &paths);
6243 /* Write new list of entries; deleted entries have been dropped. */
6244 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
6246 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
6250 static const struct got_error *
6251 update_fileindex_after_commit(struct got_worktree *worktree,
6252 struct got_pathlist_head *commitable_paths,
6253 struct got_object_id *new_base_commit_id,
6254 struct got_fileindex *fileindex, int have_staged_files)
6256 const struct got_error *err = NULL;
6257 struct got_pathlist_entry *pe;
6258 char *relpath = NULL;
6260 TAILQ_FOREACH(pe, commitable_paths, entry) {
6261 struct got_fileindex_entry *ie;
6262 struct got_commitable *ct = pe->data;
6264 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6266 err = got_path_skip_common_ancestor(&relpath,
6267 worktree->root_path, ct->ondisk_path);
6272 if (ct->status == GOT_STATUS_DELETE ||
6273 ct->staged_status == GOT_STATUS_DELETE) {
6274 got_fileindex_entry_remove(fileindex, ie);
6275 } else if (ct->staged_status == GOT_STATUS_ADD ||
6276 ct->staged_status == GOT_STATUS_MODIFY) {
6277 got_fileindex_entry_stage_set(ie,
6278 GOT_FILEIDX_STAGE_NONE);
6279 got_fileindex_entry_staged_filetype_set(ie, 0);
6281 err = got_fileindex_entry_update(ie,
6282 worktree->root_fd, relpath,
6283 ct->staged_blob_id->sha1,
6284 new_base_commit_id->sha1,
6285 !have_staged_files);
6287 err = got_fileindex_entry_update(ie,
6288 worktree->root_fd, relpath,
6290 new_base_commit_id->sha1,
6291 !have_staged_files);
6293 err = got_fileindex_entry_alloc(&ie, pe->path);
6296 err = got_fileindex_entry_update(ie,
6297 worktree->root_fd, relpath, ct->blob_id->sha1,
6298 new_base_commit_id->sha1, 1);
6300 got_fileindex_entry_free(ie);
6303 err = got_fileindex_entry_add(fileindex, ie);
6305 got_fileindex_entry_free(ie);
6318 static const struct got_error *
6319 check_out_of_date(const char *in_repo_path, unsigned char status,
6320 unsigned char staged_status, struct got_object_id *base_blob_id,
6321 struct got_object_id *base_commit_id,
6322 struct got_object_id *head_commit_id, struct got_repository *repo,
6325 const struct got_error *err = NULL;
6326 struct got_commit_object *commit = NULL;
6327 struct got_object_id *id = NULL;
6329 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
6330 /* Trivial case: base commit == head commit */
6331 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
6334 * Ensure file content which local changes were based
6335 * on matches file content in the branch head.
6337 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6340 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6342 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6343 err = got_error(ood_errcode);
6345 } else if (got_object_id_cmp(id, base_blob_id) != 0)
6346 err = got_error(ood_errcode);
6348 /* Require that added files don't exist in the branch head. */
6349 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6352 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6353 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
6355 err = id ? got_error(ood_errcode) : NULL;
6360 got_object_commit_close(commit);
6364 static const struct got_error *
6365 commit_worktree(struct got_object_id **new_commit_id,
6366 struct got_pathlist_head *commitable_paths,
6367 struct got_object_id *head_commit_id,
6368 struct got_object_id *parent_id2,
6369 struct got_worktree *worktree,
6370 const char *author, const char *committer, char *diff_path,
6371 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6372 got_worktree_status_cb status_cb, void *status_arg,
6373 struct got_repository *repo)
6375 const struct got_error *err = NULL, *unlockerr = NULL;
6376 struct got_pathlist_entry *pe;
6377 const char *head_ref_name = NULL;
6378 struct got_commit_object *head_commit = NULL;
6379 struct got_reference *head_ref2 = NULL;
6380 struct got_object_id *head_commit_id2 = NULL;
6381 struct got_tree_object *head_tree = NULL;
6382 struct got_object_id *new_tree_id = NULL;
6383 int nentries, nparents = 0;
6384 struct got_object_id_queue parent_ids;
6385 struct got_object_qid *pid = NULL;
6386 char *logmsg = NULL;
6389 *new_commit_id = NULL;
6391 STAILQ_INIT(&parent_ids);
6393 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6397 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6401 if (commit_msg_cb != NULL) {
6402 err = commit_msg_cb(commitable_paths, diff_path,
6403 &logmsg, commit_arg);
6408 if (logmsg == NULL || strlen(logmsg) == 0) {
6409 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6413 /* Create blobs from added and modified files and record their IDs. */
6414 TAILQ_FOREACH(pe, commitable_paths, entry) {
6415 struct got_commitable *ct = pe->data;
6418 /* Blobs for staged files already exist. */
6419 if (ct->staged_status == GOT_STATUS_ADD ||
6420 ct->staged_status == GOT_STATUS_MODIFY)
6423 if (ct->status != GOT_STATUS_ADD &&
6424 ct->status != GOT_STATUS_MODIFY &&
6425 ct->status != GOT_STATUS_MODE_CHANGE &&
6426 ct->status != GOT_STATUS_CONFLICT)
6429 if (asprintf(&ondisk_path, "%s/%s",
6430 worktree->root_path, pe->path) == -1) {
6431 err = got_error_from_errno("asprintf");
6434 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6440 /* Recursively write new tree objects. */
6441 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6442 commitable_paths, status_cb, status_arg, repo);
6446 err = got_object_qid_alloc(&pid, head_commit_id);
6449 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6452 err = got_object_qid_alloc(&pid, parent_id2);
6455 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6458 timestamp = time(NULL);
6459 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6460 nparents, author, timestamp, committer, timestamp, logmsg, repo);
6466 /* Check if a concurrent commit to our branch has occurred. */
6467 head_ref_name = got_worktree_get_head_ref_name(worktree);
6468 if (head_ref_name == NULL) {
6469 err = got_error_from_errno("got_worktree_get_head_ref_name");
6472 /* Lock the reference here to prevent concurrent modification. */
6473 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6476 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6479 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6480 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6483 /* Update branch head in repository. */
6484 err = got_ref_change_ref(head_ref2, *new_commit_id);
6487 err = got_ref_write(head_ref2, repo);
6491 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6495 err = ref_base_commit(worktree, repo);
6499 got_object_id_queue_free(&parent_ids);
6501 got_object_tree_close(head_tree);
6503 got_object_commit_close(head_commit);
6504 free(head_commit_id2);
6506 unlockerr = got_ref_unlock(head_ref2);
6507 if (unlockerr && err == NULL)
6509 got_ref_close(head_ref2);
6514 static const struct got_error *
6515 check_path_is_commitable(const char *path,
6516 struct got_pathlist_head *commitable_paths)
6518 struct got_pathlist_entry *cpe = NULL;
6519 size_t path_len = strlen(path);
6521 TAILQ_FOREACH(cpe, commitable_paths, entry) {
6522 struct got_commitable *ct = cpe->data;
6523 const char *ct_path = ct->path;
6525 while (ct_path[0] == '/')
6528 if (strcmp(path, ct_path) == 0 ||
6529 got_path_is_child(ct_path, path, path_len))
6534 return got_error_path(path, GOT_ERR_BAD_PATH);
6539 static const struct got_error *
6540 check_staged_file(void *arg, struct got_fileindex_entry *ie)
6542 int *have_staged_files = arg;
6544 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6545 *have_staged_files = 1;
6546 return got_error(GOT_ERR_CANCELLED);
6552 static const struct got_error *
6553 check_non_staged_files(struct got_fileindex *fileindex,
6554 struct got_pathlist_head *paths)
6556 struct got_pathlist_entry *pe;
6557 struct got_fileindex_entry *ie;
6559 TAILQ_FOREACH(pe, paths, entry) {
6560 if (pe->path[0] == '\0')
6562 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6564 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6565 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6566 return got_error_path(pe->path,
6567 GOT_ERR_FILE_NOT_STAGED);
6573 const struct got_error *
6574 got_worktree_commit(struct got_object_id **new_commit_id,
6575 struct got_worktree *worktree, struct got_pathlist_head *paths,
6576 const char *author, const char *committer, int allow_bad_symlinks,
6577 int show_diff, int commit_conflicts,
6578 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6579 got_worktree_status_cb status_cb, void *status_arg,
6580 struct got_repository *repo)
6582 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6583 struct got_fileindex *fileindex = NULL;
6584 char *fileindex_path = NULL;
6585 struct got_pathlist_head commitable_paths;
6586 struct collect_commitables_arg cc_arg;
6587 struct got_pathlist_entry *pe;
6588 struct got_reference *head_ref = NULL;
6589 struct got_object_id *head_commit_id = NULL;
6590 char *diff_path = NULL;
6591 int have_staged_files = 0;
6593 *new_commit_id = NULL;
6595 memset(&cc_arg, 0, sizeof(cc_arg));
6596 TAILQ_INIT(&commitable_paths);
6598 err = lock_worktree(worktree, LOCK_EX);
6602 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6606 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6610 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6614 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6615 &have_staged_files);
6616 if (err && err->code != GOT_ERR_CANCELLED)
6618 if (have_staged_files) {
6619 err = check_non_staged_files(fileindex, paths);
6624 cc_arg.commitable_paths = &commitable_paths;
6625 cc_arg.worktree = worktree;
6626 cc_arg.fileindex = fileindex;
6628 cc_arg.have_staged_files = have_staged_files;
6629 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6630 cc_arg.diff_header_shown = 0;
6631 cc_arg.commit_conflicts = commit_conflicts;
6633 err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6634 GOT_TMPDIR_STR "/got", ".diff");
6637 cc_arg.f1 = got_opentemp();
6638 if (cc_arg.f1 == NULL) {
6639 err = got_error_from_errno("got_opentemp");
6642 cc_arg.f2 = got_opentemp();
6643 if (cc_arg.f2 == NULL) {
6644 err = got_error_from_errno("got_opentemp");
6649 TAILQ_FOREACH(pe, paths, entry) {
6650 err = worktree_status(worktree, pe->path, fileindex, repo,
6651 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6657 if (fflush(cc_arg.diff_outfile) == EOF) {
6658 err = got_error_from_errno("fflush");
6663 if (TAILQ_EMPTY(&commitable_paths)) {
6664 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6668 TAILQ_FOREACH(pe, paths, entry) {
6669 err = check_path_is_commitable(pe->path, &commitable_paths);
6674 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6675 struct got_commitable *ct = pe->data;
6676 const char *ct_path = ct->in_repo_path;
6678 while (ct_path[0] == '/')
6680 err = check_out_of_date(ct_path, ct->status,
6681 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6682 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6688 err = commit_worktree(new_commit_id, &commitable_paths,
6689 head_commit_id, NULL, worktree, author, committer,
6690 (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6691 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6695 err = update_fileindex_after_commit(worktree, &commitable_paths,
6696 *new_commit_id, fileindex, have_staged_files);
6697 sync_err = sync_fileindex(fileindex, fileindex_path);
6698 if (sync_err && err == NULL)
6702 got_fileindex_free(fileindex);
6703 free(fileindex_path);
6704 unlockerr = lock_worktree(worktree, LOCK_SH);
6705 if (unlockerr && err == NULL)
6707 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6708 struct got_commitable *ct = pe->data;
6710 free_commitable(ct);
6712 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6713 if (diff_path && unlink(diff_path) == -1 && err == NULL)
6714 err = got_error_from_errno2("unlink", diff_path);
6716 if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6718 err = got_error_from_errno("fclose");
6723 got_commitable_get_path(struct got_commitable *ct)
6729 got_commitable_get_status(struct got_commitable *ct)
6734 struct check_rebase_ok_arg {
6735 struct got_worktree *worktree;
6736 struct got_repository *repo;
6739 static const struct got_error *
6740 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6742 const struct got_error *err = NULL;
6743 struct check_rebase_ok_arg *a = arg;
6744 unsigned char status;
6748 /* Reject rebase of a work tree with mixed base commits. */
6749 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6750 SHA1_DIGEST_LENGTH))
6751 return got_error(GOT_ERR_MIXED_COMMITS);
6753 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6755 return got_error_from_errno("asprintf");
6757 /* Reject rebase of a work tree with modified or staged files. */
6758 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6763 if (status != GOT_STATUS_NO_CHANGE)
6764 return got_error(GOT_ERR_MODIFIED);
6765 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6766 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6771 const struct got_error *
6772 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6773 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6774 struct got_worktree *worktree, struct got_reference *branch,
6775 struct got_repository *repo)
6777 const struct got_error *err = NULL;
6778 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6779 char *branch_ref_name = NULL;
6780 char *fileindex_path = NULL;
6781 struct check_rebase_ok_arg ok_arg;
6782 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6783 struct got_object_id *wt_branch_tip = NULL;
6785 *new_base_branch_ref = NULL;
6789 err = lock_worktree(worktree, LOCK_EX);
6793 err = open_fileindex(fileindex, &fileindex_path, worktree);
6797 ok_arg.worktree = worktree;
6799 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6804 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6808 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6812 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6816 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6821 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6824 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6825 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6829 err = got_ref_alloc_symref(new_base_branch_ref,
6830 new_base_branch_ref_name, wt_branch);
6833 err = got_ref_write(*new_base_branch_ref, repo);
6837 /* TODO Lock original branch's ref while rebasing? */
6839 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6843 err = got_ref_write(branch_ref, repo);
6847 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6848 worktree->base_commit_id);
6851 err = got_ref_write(*tmp_branch, repo);
6855 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6859 free(fileindex_path);
6860 free(tmp_branch_name);
6861 free(new_base_branch_ref_name);
6862 free(branch_ref_name);
6864 got_ref_close(branch_ref);
6866 got_ref_close(wt_branch);
6867 free(wt_branch_tip);
6869 if (*new_base_branch_ref) {
6870 got_ref_close(*new_base_branch_ref);
6871 *new_base_branch_ref = NULL;
6874 got_ref_close(*tmp_branch);
6878 got_fileindex_free(*fileindex);
6881 lock_worktree(worktree, LOCK_SH);
6886 const struct got_error *
6887 got_worktree_rebase_continue(struct got_object_id **commit_id,
6888 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6889 struct got_reference **branch, struct got_fileindex **fileindex,
6890 struct got_worktree *worktree, struct got_repository *repo)
6892 const struct got_error *err;
6893 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6894 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6895 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6896 char *fileindex_path = NULL;
6897 int have_staged_files = 0;
6900 *new_base_branch = NULL;
6905 err = lock_worktree(worktree, LOCK_EX);
6909 err = open_fileindex(fileindex, &fileindex_path, worktree);
6913 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6914 &have_staged_files);
6915 if (err && err->code != GOT_ERR_CANCELLED)
6917 if (have_staged_files) {
6918 err = got_error(GOT_ERR_STAGED_PATHS);
6922 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6926 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6930 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6934 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6938 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6942 err = got_ref_open(branch, repo,
6943 got_ref_get_symref_target(branch_ref), 0);
6947 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6951 err = got_ref_resolve(commit_id, repo, commit_ref);
6955 err = got_ref_open(new_base_branch, repo,
6956 new_base_branch_ref_name, 0);
6960 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6964 free(commit_ref_name);
6965 free(branch_ref_name);
6966 free(fileindex_path);
6968 got_ref_close(commit_ref);
6970 got_ref_close(branch_ref);
6975 got_ref_close(*tmp_branch);
6978 if (*new_base_branch) {
6979 got_ref_close(*new_base_branch);
6980 *new_base_branch = NULL;
6983 got_ref_close(*branch);
6987 got_fileindex_free(*fileindex);
6990 lock_worktree(worktree, LOCK_SH);
6995 const struct got_error *
6996 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6998 const struct got_error *err;
6999 char *tmp_branch_name = NULL;
7001 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7005 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7006 free(tmp_branch_name);
7010 static const struct got_error *
7011 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
7012 const char *diff_path, char **logmsg, void *arg)
7018 static const struct got_error *
7019 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
7020 const char *path, struct got_object_id *blob_id,
7021 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7022 int dirfd, const char *de_name)
7027 struct collect_merged_paths_arg {
7028 got_worktree_checkout_cb progress_cb;
7030 struct got_pathlist_head *merged_paths;
7033 static const struct got_error *
7034 collect_merged_paths(void *arg, unsigned char status, const char *path)
7036 const struct got_error *err;
7037 struct collect_merged_paths_arg *a = arg;
7039 struct got_pathlist_entry *new;
7041 err = (*a->progress_cb)(a->progress_arg, status, path);
7045 if (status != GOT_STATUS_MERGE &&
7046 status != GOT_STATUS_ADD &&
7047 status != GOT_STATUS_DELETE &&
7048 status != GOT_STATUS_CONFLICT)
7053 return got_error_from_errno("strdup");
7055 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
7056 if (err || new == NULL)
7061 static const struct got_error *
7062 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
7063 int is_rebase, struct got_repository *repo)
7065 const struct got_error *err;
7066 struct got_reference *commit_ref = NULL;
7068 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7070 if (err->code != GOT_ERR_NOT_REF)
7072 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
7075 err = got_ref_write(commit_ref, repo);
7078 } else if (is_rebase) {
7079 struct got_object_id *stored_id;
7082 err = got_ref_resolve(&stored_id, repo, commit_ref);
7085 cmp = got_object_id_cmp(commit_id, stored_id);
7088 err = got_error(GOT_ERR_REBASE_COMMITID);
7094 got_ref_close(commit_ref);
7098 static const struct got_error *
7099 rebase_merge_files(struct got_pathlist_head *merged_paths,
7100 const char *commit_ref_name, struct got_worktree *worktree,
7101 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
7102 struct got_object_id *commit_id, struct got_repository *repo,
7103 got_worktree_checkout_cb progress_cb, void *progress_arg,
7104 got_cancel_cb cancel_cb, void *cancel_arg)
7106 const struct got_error *err;
7107 struct got_reference *commit_ref = NULL;
7108 struct collect_merged_paths_arg cmp_arg;
7109 char *fileindex_path;
7111 /* Work tree is locked/unlocked during rebase preparation/teardown. */
7113 err = get_fileindex_path(&fileindex_path, worktree);
7117 cmp_arg.progress_cb = progress_cb;
7118 cmp_arg.progress_arg = progress_arg;
7119 cmp_arg.merged_paths = merged_paths;
7120 err = merge_files(worktree, fileindex, fileindex_path,
7121 parent_commit_id, commit_id, repo, collect_merged_paths,
7122 &cmp_arg, cancel_cb, cancel_arg);
7124 got_ref_close(commit_ref);
7128 const struct got_error *
7129 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
7130 struct got_worktree *worktree, struct got_fileindex *fileindex,
7131 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
7132 struct got_repository *repo,
7133 got_worktree_checkout_cb progress_cb, void *progress_arg,
7134 got_cancel_cb cancel_cb, void *cancel_arg)
7136 const struct got_error *err;
7137 char *commit_ref_name;
7139 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7143 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
7147 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7148 fileindex, parent_commit_id, commit_id, repo, progress_cb,
7149 progress_arg, cancel_cb, cancel_arg);
7151 free(commit_ref_name);
7155 const struct got_error *
7156 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
7157 struct got_worktree *worktree, struct got_fileindex *fileindex,
7158 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
7159 struct got_repository *repo,
7160 got_worktree_checkout_cb progress_cb, void *progress_arg,
7161 got_cancel_cb cancel_cb, void *cancel_arg)
7163 const struct got_error *err;
7164 char *commit_ref_name;
7166 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7170 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7174 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7175 fileindex, parent_commit_id, commit_id, repo, progress_cb,
7176 progress_arg, cancel_cb, cancel_arg);
7178 free(commit_ref_name);
7182 static const struct got_error *
7183 rebase_commit(struct got_object_id **new_commit_id,
7184 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
7185 struct got_worktree *worktree, struct got_fileindex *fileindex,
7186 struct got_reference *tmp_branch, const char *committer,
7187 struct got_commit_object *orig_commit, const char *new_logmsg,
7188 int allow_conflict, struct got_repository *repo)
7190 const struct got_error *err, *sync_err;
7191 struct got_pathlist_head commitable_paths;
7192 struct collect_commitables_arg cc_arg;
7193 char *fileindex_path = NULL;
7194 struct got_reference *head_ref = NULL;
7195 struct got_object_id *head_commit_id = NULL;
7196 char *logmsg = NULL;
7198 memset(&cc_arg, 0, sizeof(cc_arg));
7199 TAILQ_INIT(&commitable_paths);
7200 *new_commit_id = NULL;
7202 /* Work tree is locked/unlocked during rebase preparation/teardown. */
7204 err = get_fileindex_path(&fileindex_path, worktree);
7208 cc_arg.commitable_paths = &commitable_paths;
7209 cc_arg.worktree = worktree;
7211 cc_arg.have_staged_files = 0;
7212 cc_arg.commit_conflicts = allow_conflict;
7214 * If possible get the status of individual files directly to
7215 * avoid crawling the entire work tree once per rebased commit.
7217 * Ideally, merged_paths would contain a list of commitables
7218 * we could use so we could skip worktree_status() entirely.
7219 * However, we would then need carefully keep track of cumulative
7220 * effects of operations such as file additions and deletions
7221 * in 'got histedit -f' (folding multiple commits into one),
7222 * and this extra complexity is not really worth it.
7225 struct got_pathlist_entry *pe;
7226 TAILQ_FOREACH(pe, merged_paths, entry) {
7227 err = worktree_status(worktree, pe->path, fileindex,
7228 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
7234 err = worktree_status(worktree, "", fileindex, repo,
7235 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7240 if (TAILQ_EMPTY(&commitable_paths)) {
7241 /* No-op change; commit will be elided. */
7242 err = got_ref_delete(commit_ref, repo);
7245 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
7249 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7253 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7258 logmsg = strdup(new_logmsg);
7259 if (logmsg == NULL) {
7260 err = got_error_from_errno("strdup");
7264 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
7269 /* NB: commit_worktree will call free(logmsg) */
7270 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
7271 NULL, worktree, got_object_commit_get_author(orig_commit),
7272 committer ? committer :
7273 got_object_commit_get_committer(orig_commit), NULL,
7274 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
7278 err = got_ref_change_ref(tmp_branch, *new_commit_id);
7282 err = got_ref_delete(commit_ref, repo);
7286 err = update_fileindex_after_commit(worktree, &commitable_paths,
7287 *new_commit_id, fileindex, 0);
7288 sync_err = sync_fileindex(fileindex, fileindex_path);
7289 if (sync_err && err == NULL)
7292 free(fileindex_path);
7293 free(head_commit_id);
7295 got_ref_close(head_ref);
7297 free(*new_commit_id);
7298 *new_commit_id = NULL;
7303 const struct got_error *
7304 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
7305 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7306 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7307 const char *committer, struct got_commit_object *orig_commit,
7308 struct got_object_id *orig_commit_id, int allow_conflict,
7309 struct got_repository *repo)
7311 const struct got_error *err;
7312 char *commit_ref_name;
7313 struct got_reference *commit_ref = NULL;
7314 struct got_object_id *commit_id = NULL;
7316 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7320 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7323 err = got_ref_resolve(&commit_id, repo, commit_ref);
7326 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
7327 err = got_error(GOT_ERR_REBASE_COMMITID);
7331 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7332 worktree, fileindex, tmp_branch, committer, orig_commit,
7333 NULL, allow_conflict, repo);
7336 got_ref_close(commit_ref);
7337 free(commit_ref_name);
7342 const struct got_error *
7343 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
7344 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7345 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7346 const char *committer, struct got_commit_object *orig_commit,
7347 struct got_object_id *orig_commit_id, const char *new_logmsg,
7348 int allow_conflict, struct got_repository *repo)
7350 const struct got_error *err;
7351 char *commit_ref_name;
7352 struct got_reference *commit_ref = NULL;
7354 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7358 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7362 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7363 worktree, fileindex, tmp_branch, committer, orig_commit,
7364 new_logmsg, allow_conflict, repo);
7367 got_ref_close(commit_ref);
7368 free(commit_ref_name);
7372 const struct got_error *
7373 got_worktree_rebase_postpone(struct got_worktree *worktree,
7374 struct got_fileindex *fileindex)
7377 got_fileindex_free(fileindex);
7378 return lock_worktree(worktree, LOCK_SH);
7381 static const struct got_error *
7382 delete_ref(const char *name, struct got_repository *repo)
7384 const struct got_error *err;
7385 struct got_reference *ref;
7387 err = got_ref_open(&ref, repo, name, 0);
7389 if (err->code == GOT_ERR_NOT_REF)
7394 err = got_ref_delete(ref, repo);
7399 static const struct got_error *
7400 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7402 const struct got_error *err;
7403 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7404 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7406 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7409 err = delete_ref(tmp_branch_name, repo);
7413 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7416 err = delete_ref(new_base_branch_ref_name, repo);
7420 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7423 err = delete_ref(branch_ref_name, repo);
7427 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7430 err = delete_ref(commit_ref_name, repo);
7435 free(tmp_branch_name);
7436 free(new_base_branch_ref_name);
7437 free(branch_ref_name);
7438 free(commit_ref_name);
7442 static const struct got_error *
7443 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7444 struct got_object_id *new_commit_id, struct got_repository *repo)
7446 const struct got_error *err;
7447 struct got_reference *ref = NULL;
7448 struct got_object_id *old_commit_id = NULL;
7449 const char *branch_name = NULL;
7450 char *new_id_str = NULL;
7451 char *refname = NULL;
7453 branch_name = got_ref_get_name(branch);
7454 if (strncmp(branch_name, "refs/heads/", 11) != 0)
7455 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7458 err = got_object_id_str(&new_id_str, new_commit_id);
7462 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7463 new_id_str) == -1) {
7464 err = got_error_from_errno("asprintf");
7468 err = got_ref_resolve(&old_commit_id, repo, branch);
7472 err = got_ref_alloc(&ref, refname, old_commit_id);
7476 err = got_ref_write(ref, repo);
7480 free(old_commit_id);
7486 const struct got_error *
7487 got_worktree_rebase_complete(struct got_worktree *worktree,
7488 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7489 struct got_reference *rebased_branch, struct got_repository *repo,
7492 const struct got_error *err, *unlockerr, *sync_err;
7493 struct got_object_id *new_head_commit_id = NULL;
7494 char *fileindex_path = NULL;
7496 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7500 if (create_backup) {
7501 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7502 rebased_branch, new_head_commit_id, repo);
7507 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7511 err = got_ref_write(rebased_branch, repo);
7515 err = got_worktree_set_head_ref(worktree, rebased_branch);
7519 err = delete_rebase_refs(worktree, repo);
7523 err = get_fileindex_path(&fileindex_path, worktree);
7526 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7527 sync_err = sync_fileindex(fileindex, fileindex_path);
7528 if (sync_err && err == NULL)
7531 got_fileindex_free(fileindex);
7532 free(fileindex_path);
7533 free(new_head_commit_id);
7534 unlockerr = lock_worktree(worktree, LOCK_SH);
7535 if (unlockerr && err == NULL)
7540 static const struct got_error *
7541 get_paths_changed_between_commits(struct got_pathlist_head *paths,
7542 struct got_object_id *id1, struct got_object_id *id2,
7543 struct got_repository *repo)
7545 const struct got_error *err;
7546 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
7547 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7550 err = got_object_open_as_commit(&commit1, repo, id1);
7554 err = got_object_open_as_tree(&tree1, repo,
7555 got_object_commit_get_tree_id(commit1));
7561 err = got_object_open_as_commit(&commit2, repo, id2);
7565 err = got_object_open_as_tree(&tree2, repo,
7566 got_object_commit_get_tree_id(commit2));
7571 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
7572 got_diff_tree_collect_changed_paths, paths, 0);
7577 got_object_commit_close(commit1);
7579 got_object_commit_close(commit2);
7581 got_object_tree_close(tree1);
7583 got_object_tree_close(tree2);
7587 static const struct got_error *
7588 get_paths_added_between_commits(struct got_pathlist_head *added_paths,
7589 struct got_object_id *id1, struct got_object_id *id2,
7590 const char *path_prefix, struct got_repository *repo)
7592 const struct got_error *err;
7593 struct got_pathlist_head merged_paths;
7594 struct got_pathlist_entry *pe;
7595 char *abspath = NULL, *wt_path = NULL;
7597 TAILQ_INIT(&merged_paths);
7599 err = get_paths_changed_between_commits(&merged_paths, id1, id2, repo);
7603 TAILQ_FOREACH(pe, &merged_paths, entry) {
7604 struct got_diff_changed_path *change = pe->data;
7606 if (change->status != GOT_STATUS_ADD)
7609 if (got_path_is_root_dir(path_prefix)) {
7610 wt_path = strdup(pe->path);
7611 if (wt_path == NULL) {
7612 err = got_error_from_errno("strdup");
7616 if (asprintf(&abspath, "/%s", pe->path) == -1) {
7617 err = got_error_from_errno("asprintf");
7621 err = got_path_skip_common_ancestor(&wt_path,
7622 path_prefix, abspath);
7629 err = got_pathlist_append(added_paths, wt_path, NULL);
7636 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_ALL);
7642 static const struct got_error *
7643 get_paths_added_in_commit(struct got_pathlist_head *added_paths,
7644 struct got_object_id *id, const char *path_prefix,
7645 struct got_repository *repo)
7647 const struct got_error *err;
7648 struct got_commit_object *commit = NULL;
7649 struct got_object_qid *pid;
7651 err = got_object_open_as_commit(&commit, repo, id);
7655 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7657 err = get_paths_added_between_commits(added_paths,
7658 pid ? &pid->id : NULL, id, path_prefix, repo);
7663 got_object_commit_close(commit);
7667 const struct got_error *
7668 got_worktree_rebase_abort(struct got_worktree *worktree,
7669 struct got_fileindex *fileindex, struct got_repository *repo,
7670 struct got_reference *new_base_branch,
7671 got_worktree_checkout_cb progress_cb, void *progress_arg)
7673 const struct got_error *err, *unlockerr, *sync_err;
7674 struct got_reference *resolved = NULL;
7675 struct got_object_id *commit_id = NULL;
7676 struct got_object_id *merged_commit_id = NULL;
7677 struct got_commit_object *commit = NULL;
7678 char *fileindex_path = NULL;
7679 char *commit_ref_name = NULL;
7680 struct got_reference *commit_ref = NULL;
7681 struct revert_file_args rfa;
7682 struct got_object_id *tree_id = NULL;
7683 struct got_pathlist_head added_paths;
7685 TAILQ_INIT(&added_paths);
7687 err = lock_worktree(worktree, LOCK_EX);
7691 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7695 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7699 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
7704 * Determine which files in added status can be safely removed
7705 * from disk while reverting changes in the work tree.
7706 * We want to avoid deleting unrelated files which were added by
7707 * the user for conflict resolution purposes.
7709 err = get_paths_added_in_commit(&added_paths, merged_commit_id,
7710 got_worktree_get_path_prefix(worktree), repo);
7714 err = got_ref_open(&resolved, repo,
7715 got_ref_get_symref_target(new_base_branch), 0);
7719 err = got_worktree_set_head_ref(worktree, resolved);
7724 * XXX commits to the base branch could have happened while
7725 * we were busy rebasing; should we store the original commit ID
7726 * when rebase begins and read it back here?
7728 err = got_ref_resolve(&commit_id, repo, resolved);
7732 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7736 err = got_object_open_as_commit(&commit, repo,
7737 worktree->base_commit_id);
7741 err = got_object_id_by_path(&tree_id, repo, commit,
7742 worktree->path_prefix);
7746 err = delete_rebase_refs(worktree, repo);
7750 err = get_fileindex_path(&fileindex_path, worktree);
7754 rfa.worktree = worktree;
7755 rfa.fileindex = fileindex;
7756 rfa.progress_cb = progress_cb;
7757 rfa.progress_arg = progress_arg;
7758 rfa.patch_cb = NULL;
7759 rfa.patch_arg = NULL;
7761 rfa.unlink_added_files = 1;
7762 rfa.added_files_to_unlink = &added_paths;
7763 err = worktree_status(worktree, "", fileindex, repo,
7764 revert_file, &rfa, NULL, NULL, 1, 0);
7768 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7769 repo, progress_cb, progress_arg, NULL, NULL);
7771 sync_err = sync_fileindex(fileindex, fileindex_path);
7772 if (sync_err && err == NULL)
7775 got_pathlist_free(&added_paths, GOT_PATHLIST_FREE_PATH);
7776 got_ref_close(resolved);
7779 free(merged_commit_id);
7781 got_object_commit_close(commit);
7783 got_fileindex_free(fileindex);
7784 free(fileindex_path);
7785 free(commit_ref_name);
7787 got_ref_close(commit_ref);
7789 unlockerr = lock_worktree(worktree, LOCK_SH);
7790 if (unlockerr && err == NULL)
7795 const struct got_error *
7796 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7797 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7798 struct got_fileindex **fileindex, struct got_worktree *worktree,
7799 struct got_repository *repo)
7801 const struct got_error *err = NULL;
7802 char *tmp_branch_name = NULL;
7803 char *branch_ref_name = NULL;
7804 char *base_commit_ref_name = NULL;
7805 char *fileindex_path = NULL;
7806 struct check_rebase_ok_arg ok_arg;
7807 struct got_reference *wt_branch = NULL;
7808 struct got_reference *base_commit_ref = NULL;
7812 *base_commit_id = NULL;
7815 err = lock_worktree(worktree, LOCK_EX);
7819 err = open_fileindex(fileindex, &fileindex_path, worktree);
7823 ok_arg.worktree = worktree;
7825 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7830 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7834 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7838 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7843 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7848 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7852 err = got_ref_write(*branch_ref, repo);
7856 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7857 worktree->base_commit_id);
7860 err = got_ref_write(base_commit_ref, repo);
7863 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7864 if (*base_commit_id == NULL) {
7865 err = got_error_from_errno("got_object_id_dup");
7869 err = got_ref_alloc(tmp_branch, tmp_branch_name,
7870 worktree->base_commit_id);
7873 err = got_ref_write(*tmp_branch, repo);
7877 err = got_worktree_set_head_ref(worktree, *tmp_branch);
7881 free(fileindex_path);
7882 free(tmp_branch_name);
7883 free(branch_ref_name);
7884 free(base_commit_ref_name);
7886 got_ref_close(wt_branch);
7889 got_ref_close(*branch_ref);
7893 got_ref_close(*tmp_branch);
7896 free(*base_commit_id);
7898 got_fileindex_free(*fileindex);
7901 lock_worktree(worktree, LOCK_SH);
7906 const struct got_error *
7907 got_worktree_histedit_postpone(struct got_worktree *worktree,
7908 struct got_fileindex *fileindex)
7911 got_fileindex_free(fileindex);
7912 return lock_worktree(worktree, LOCK_SH);
7915 const struct got_error *
7916 got_worktree_histedit_in_progress(int *in_progress,
7917 struct got_worktree *worktree)
7919 const struct got_error *err;
7920 char *tmp_branch_name = NULL;
7922 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7926 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7927 free(tmp_branch_name);
7931 const struct got_error *
7932 got_worktree_histedit_continue(struct got_object_id **commit_id,
7933 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7934 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7935 struct got_worktree *worktree, struct got_repository *repo)
7937 const struct got_error *err;
7938 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7939 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7940 struct got_reference *commit_ref = NULL;
7941 struct got_reference *base_commit_ref = NULL;
7942 char *fileindex_path = NULL;
7943 int have_staged_files = 0;
7947 *base_commit_id = NULL;
7950 err = lock_worktree(worktree, LOCK_EX);
7954 err = open_fileindex(fileindex, &fileindex_path, worktree);
7958 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7959 &have_staged_files);
7960 if (err && err->code != GOT_ERR_CANCELLED)
7962 if (have_staged_files) {
7963 err = got_error(GOT_ERR_STAGED_PATHS);
7967 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7971 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7975 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7979 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7984 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7988 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7991 err = got_ref_resolve(commit_id, repo, commit_ref);
7995 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7998 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
8002 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
8006 free(commit_ref_name);
8007 free(branch_ref_name);
8008 free(fileindex_path);
8010 got_ref_close(commit_ref);
8011 if (base_commit_ref)
8012 got_ref_close(base_commit_ref);
8016 free(*base_commit_id);
8017 *base_commit_id = NULL;
8019 got_ref_close(*tmp_branch);
8023 got_fileindex_free(*fileindex);
8026 lock_worktree(worktree, LOCK_EX);
8031 static const struct got_error *
8032 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
8034 const struct got_error *err;
8035 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
8036 char *branch_ref_name = NULL, *commit_ref_name = NULL;
8038 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
8041 err = delete_ref(tmp_branch_name, repo);
8045 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
8049 err = delete_ref(base_commit_ref_name, repo);
8053 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
8056 err = delete_ref(branch_ref_name, repo);
8060 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8063 err = delete_ref(commit_ref_name, repo);
8067 free(tmp_branch_name);
8068 free(base_commit_ref_name);
8069 free(branch_ref_name);
8070 free(commit_ref_name);
8074 const struct got_error *
8075 got_worktree_histedit_abort(struct got_worktree *worktree,
8076 struct got_fileindex *fileindex, struct got_repository *repo,
8077 struct got_reference *branch, struct got_object_id *base_commit_id,
8078 got_worktree_checkout_cb progress_cb, void *progress_arg)
8080 const struct got_error *err, *unlockerr, *sync_err;
8081 struct got_reference *resolved = NULL;
8082 char *fileindex_path = NULL;
8083 struct got_object_id *merged_commit_id = NULL;
8084 struct got_commit_object *commit = NULL;
8085 char *commit_ref_name = NULL;
8086 struct got_reference *commit_ref = NULL;
8087 struct got_object_id *tree_id = NULL;
8088 struct revert_file_args rfa;
8089 struct got_pathlist_head added_paths;
8091 TAILQ_INIT(&added_paths);
8093 err = lock_worktree(worktree, LOCK_EX);
8097 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8101 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8103 if (err->code != GOT_ERR_NOT_REF)
8105 /* Can happen on early abort due to invalid histedit script. */
8110 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8115 * Determine which files in added status can be safely removed
8116 * from disk while reverting changes in the work tree.
8117 * We want to avoid deleting unrelated files added by the
8118 * user during conflict resolution or during histedit -e.
8120 err = get_paths_added_in_commit(&added_paths, merged_commit_id,
8121 got_worktree_get_path_prefix(worktree), repo);
8126 err = got_ref_open(&resolved, repo,
8127 got_ref_get_symref_target(branch), 0);
8131 err = got_worktree_set_head_ref(worktree, resolved);
8135 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
8139 err = got_object_open_as_commit(&commit, repo,
8140 worktree->base_commit_id);
8144 err = got_object_id_by_path(&tree_id, repo, commit,
8145 worktree->path_prefix);
8149 err = delete_histedit_refs(worktree, repo);
8153 err = get_fileindex_path(&fileindex_path, worktree);
8157 rfa.worktree = worktree;
8158 rfa.fileindex = fileindex;
8159 rfa.progress_cb = progress_cb;
8160 rfa.progress_arg = progress_arg;
8161 rfa.patch_cb = NULL;
8162 rfa.patch_arg = NULL;
8164 rfa.unlink_added_files = 1;
8165 rfa.added_files_to_unlink = &added_paths;
8166 err = worktree_status(worktree, "", fileindex, repo,
8167 revert_file, &rfa, NULL, NULL, 1, 0);
8171 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8172 repo, progress_cb, progress_arg, NULL, NULL);
8174 sync_err = sync_fileindex(fileindex, fileindex_path);
8175 if (sync_err && err == NULL)
8179 got_ref_close(resolved);
8181 got_ref_close(commit_ref);
8182 free(merged_commit_id);
8184 free(fileindex_path);
8185 free(commit_ref_name);
8187 unlockerr = lock_worktree(worktree, LOCK_SH);
8188 if (unlockerr && err == NULL)
8193 const struct got_error *
8194 got_worktree_histedit_complete(struct got_worktree *worktree,
8195 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8196 struct got_reference *edited_branch, struct got_repository *repo)
8198 const struct got_error *err, *unlockerr, *sync_err;
8199 struct got_object_id *new_head_commit_id = NULL;
8200 struct got_reference *resolved = NULL;
8201 char *fileindex_path = NULL;
8203 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
8207 err = got_ref_open(&resolved, repo,
8208 got_ref_get_symref_target(edited_branch), 0);
8212 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
8213 resolved, new_head_commit_id, repo);
8217 err = got_ref_change_ref(resolved, new_head_commit_id);
8221 err = got_ref_write(resolved, repo);
8225 err = got_worktree_set_head_ref(worktree, resolved);
8229 err = delete_histedit_refs(worktree, repo);
8233 err = get_fileindex_path(&fileindex_path, worktree);
8236 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8237 sync_err = sync_fileindex(fileindex, fileindex_path);
8238 if (sync_err && err == NULL)
8241 got_fileindex_free(fileindex);
8242 free(fileindex_path);
8243 free(new_head_commit_id);
8244 unlockerr = lock_worktree(worktree, LOCK_SH);
8245 if (unlockerr && err == NULL)
8250 const struct got_error *
8251 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
8252 struct got_object_id *commit_id, struct got_repository *repo)
8254 const struct got_error *err;
8255 char *commit_ref_name;
8257 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8261 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
8265 err = delete_ref(commit_ref_name, repo);
8267 free(commit_ref_name);
8271 const struct got_error *
8272 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
8273 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
8274 struct got_worktree *worktree, const char *refname,
8275 struct got_repository *repo)
8277 const struct got_error *err = NULL;
8278 char *fileindex_path = NULL;
8279 struct check_rebase_ok_arg ok_arg;
8283 *base_branch_ref = NULL;
8285 err = lock_worktree(worktree, LOCK_EX);
8289 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
8290 err = got_error_msg(GOT_ERR_SAME_BRANCH,
8291 "cannot integrate a branch into itself; "
8292 "update -b or different branch name required");
8296 err = open_fileindex(fileindex, &fileindex_path, worktree);
8300 /* Preconditions are the same as for rebase. */
8301 ok_arg.worktree = worktree;
8303 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8308 err = got_ref_open(branch_ref, repo, refname, 1);
8312 err = got_ref_open(base_branch_ref, repo,
8313 got_worktree_get_head_ref_name(worktree), 1);
8317 got_ref_close(*branch_ref);
8320 if (*base_branch_ref) {
8321 got_ref_close(*base_branch_ref);
8322 *base_branch_ref = NULL;
8325 got_fileindex_free(*fileindex);
8328 lock_worktree(worktree, LOCK_SH);
8333 const struct got_error *
8334 got_worktree_integrate_continue(struct got_worktree *worktree,
8335 struct got_fileindex *fileindex, struct got_repository *repo,
8336 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
8337 got_worktree_checkout_cb progress_cb, void *progress_arg,
8338 got_cancel_cb cancel_cb, void *cancel_arg)
8340 const struct got_error *err = NULL, *sync_err, *unlockerr;
8341 char *fileindex_path = NULL;
8342 struct got_object_id *tree_id = NULL, *commit_id = NULL;
8343 struct got_commit_object *commit = NULL;
8345 err = get_fileindex_path(&fileindex_path, worktree);
8349 err = got_ref_resolve(&commit_id, repo, branch_ref);
8353 err = got_object_open_as_commit(&commit, repo, commit_id);
8357 err = got_object_id_by_path(&tree_id, repo, commit,
8358 worktree->path_prefix);
8362 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
8366 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
8367 progress_cb, progress_arg, cancel_cb, cancel_arg);
8371 err = got_ref_change_ref(base_branch_ref, commit_id);
8375 err = got_ref_write(base_branch_ref, repo);
8379 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8381 sync_err = sync_fileindex(fileindex, fileindex_path);
8382 if (sync_err && err == NULL)
8386 unlockerr = got_ref_unlock(branch_ref);
8387 if (unlockerr && err == NULL)
8389 got_ref_close(branch_ref);
8391 unlockerr = got_ref_unlock(base_branch_ref);
8392 if (unlockerr && err == NULL)
8394 got_ref_close(base_branch_ref);
8396 got_fileindex_free(fileindex);
8397 free(fileindex_path);
8400 got_object_commit_close(commit);
8402 unlockerr = lock_worktree(worktree, LOCK_SH);
8403 if (unlockerr && err == NULL)
8408 const struct got_error *
8409 got_worktree_integrate_abort(struct got_worktree *worktree,
8410 struct got_fileindex *fileindex, struct got_repository *repo,
8411 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
8413 const struct got_error *err = NULL, *unlockerr = NULL;
8415 got_fileindex_free(fileindex);
8417 err = lock_worktree(worktree, LOCK_SH);
8419 unlockerr = got_ref_unlock(branch_ref);
8420 if (unlockerr && err == NULL)
8422 got_ref_close(branch_ref);
8424 unlockerr = got_ref_unlock(base_branch_ref);
8425 if (unlockerr && err == NULL)
8427 got_ref_close(base_branch_ref);
8432 const struct got_error *
8433 got_worktree_merge_postpone(struct got_worktree *worktree,
8434 struct got_fileindex *fileindex)
8436 const struct got_error *err, *sync_err;
8437 char *fileindex_path = NULL;
8439 err = get_fileindex_path(&fileindex_path, worktree);
8443 sync_err = sync_fileindex(fileindex, fileindex_path);
8445 err = lock_worktree(worktree, LOCK_SH);
8446 if (sync_err && err == NULL)
8449 got_fileindex_free(fileindex);
8450 free(fileindex_path);
8454 static const struct got_error *
8455 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
8457 const struct got_error *err;
8458 char *branch_refname = NULL, *commit_refname = NULL;
8460 err = get_merge_branch_ref_name(&branch_refname, worktree);
8463 err = delete_ref(branch_refname, repo);
8467 err = get_merge_commit_ref_name(&commit_refname, worktree);
8470 err = delete_ref(commit_refname, repo);
8475 free(branch_refname);
8476 free(commit_refname);
8480 struct merge_commit_msg_arg {
8481 struct got_worktree *worktree;
8482 const char *branch_name;
8485 static const struct got_error *
8486 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
8487 const char *diff_path, char **logmsg, void *arg)
8489 struct merge_commit_msg_arg *a = arg;
8491 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
8492 got_worktree_get_head_ref_name(a->worktree)) == -1)
8493 return got_error_from_errno("asprintf");
8499 const struct got_error *
8500 got_worktree_merge_branch(struct got_worktree *worktree,
8501 struct got_fileindex *fileindex,
8502 struct got_object_id *yca_commit_id,
8503 struct got_object_id *branch_tip,
8504 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
8505 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
8507 const struct got_error *err;
8508 char *fileindex_path = NULL;
8509 struct check_mixed_commits_args cma;
8511 err = get_fileindex_path(&fileindex_path, worktree);
8515 cma.worktree = worktree;
8516 cma.cancel_cb = cancel_cb;
8517 cma.cancel_arg = cancel_arg;
8519 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
8524 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
8525 branch_tip, repo, progress_cb, progress_arg,
8526 cancel_cb, cancel_arg);
8528 free(fileindex_path);
8532 const struct got_error *
8533 got_worktree_merge_commit(struct got_object_id **new_commit_id,
8534 struct got_worktree *worktree, struct got_fileindex *fileindex,
8535 const char *author, const char *committer, int allow_bad_symlinks,
8536 struct got_object_id *branch_tip, const char *branch_name,
8537 int allow_conflict, struct got_repository *repo,
8538 got_worktree_status_cb status_cb, void *status_arg)
8541 const struct got_error *err = NULL, *sync_err;
8542 struct got_pathlist_head commitable_paths;
8543 struct collect_commitables_arg cc_arg;
8544 struct got_pathlist_entry *pe;
8545 struct got_reference *head_ref = NULL;
8546 struct got_object_id *head_commit_id = NULL;
8547 int have_staged_files = 0;
8548 struct merge_commit_msg_arg mcm_arg;
8549 char *fileindex_path = NULL;
8551 memset(&cc_arg, 0, sizeof(cc_arg));
8552 *new_commit_id = NULL;
8554 TAILQ_INIT(&commitable_paths);
8556 err = get_fileindex_path(&fileindex_path, worktree);
8560 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
8564 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8568 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
8569 &have_staged_files);
8570 if (err && err->code != GOT_ERR_CANCELLED)
8572 if (have_staged_files) {
8573 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
8577 cc_arg.commitable_paths = &commitable_paths;
8578 cc_arg.worktree = worktree;
8579 cc_arg.fileindex = fileindex;
8581 cc_arg.have_staged_files = have_staged_files;
8582 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
8583 cc_arg.commit_conflicts = allow_conflict;
8584 err = worktree_status(worktree, "", fileindex, repo,
8585 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8589 mcm_arg.worktree = worktree;
8590 mcm_arg.branch_name = branch_name;
8591 err = commit_worktree(new_commit_id, &commitable_paths,
8592 head_commit_id, branch_tip, worktree, author, committer, NULL,
8593 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8597 err = update_fileindex_after_commit(worktree, &commitable_paths,
8598 *new_commit_id, fileindex, have_staged_files);
8599 sync_err = sync_fileindex(fileindex, fileindex_path);
8600 if (sync_err && err == NULL)
8603 TAILQ_FOREACH(pe, &commitable_paths, entry) {
8604 struct got_commitable *ct = pe->data;
8606 free_commitable(ct);
8608 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8609 free(fileindex_path);
8613 const struct got_error *
8614 got_worktree_merge_complete(struct got_worktree *worktree,
8615 struct got_fileindex *fileindex, struct got_repository *repo)
8617 const struct got_error *err, *unlockerr, *sync_err;
8618 char *fileindex_path = NULL;
8620 err = delete_merge_refs(worktree, repo);
8624 err = get_fileindex_path(&fileindex_path, worktree);
8627 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8628 sync_err = sync_fileindex(fileindex, fileindex_path);
8629 if (sync_err && err == NULL)
8632 got_fileindex_free(fileindex);
8633 free(fileindex_path);
8634 unlockerr = lock_worktree(worktree, LOCK_SH);
8635 if (unlockerr && err == NULL)
8640 const struct got_error *
8641 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8642 struct got_repository *repo)
8644 const struct got_error *err;
8645 char *branch_refname = NULL;
8646 struct got_reference *branch_ref = NULL;
8650 err = get_merge_branch_ref_name(&branch_refname, worktree);
8653 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8654 free(branch_refname);
8656 if (err->code != GOT_ERR_NOT_REF)
8664 const struct got_error *got_worktree_merge_prepare(
8665 struct got_fileindex **fileindex, struct got_worktree *worktree,
8666 struct got_repository *repo)
8668 const struct got_error *err = NULL;
8669 char *fileindex_path = NULL;
8670 struct got_reference *wt_branch = NULL;
8671 struct got_object_id *wt_branch_tip = NULL;
8672 struct check_rebase_ok_arg ok_arg;
8676 err = lock_worktree(worktree, LOCK_EX);
8680 err = open_fileindex(fileindex, &fileindex_path, worktree);
8684 /* Preconditions are the same as for rebase. */
8685 ok_arg.worktree = worktree;
8687 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8692 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8697 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8701 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8702 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8707 free(fileindex_path);
8709 got_ref_close(wt_branch);
8710 free(wt_branch_tip);
8713 got_fileindex_free(*fileindex);
8716 lock_worktree(worktree, LOCK_SH);
8721 const struct got_error *got_worktree_merge_write_refs(
8722 struct got_worktree *worktree, struct got_reference *branch,
8723 struct got_repository *repo)
8725 const struct got_error *err = NULL;
8726 char *branch_refname = NULL, *commit_refname = NULL;
8727 struct got_reference *branch_ref = NULL, *commit_ref = NULL;
8728 struct got_object_id *branch_tip = NULL;
8730 err = get_merge_branch_ref_name(&branch_refname, worktree);
8734 err = get_merge_commit_ref_name(&commit_refname, worktree);
8738 err = got_ref_resolve(&branch_tip, repo, branch);
8742 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8745 err = got_ref_write(branch_ref, repo);
8749 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8752 err = got_ref_write(commit_ref, repo);
8757 free(branch_refname);
8758 free(commit_refname);
8760 got_ref_close(branch_ref);
8762 got_ref_close(commit_ref);
8767 const struct got_error *
8768 got_worktree_merge_continue(char **branch_name,
8769 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8770 struct got_worktree *worktree, struct got_repository *repo)
8772 const struct got_error *err;
8773 char *commit_refname = NULL, *branch_refname = NULL;
8774 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8775 char *fileindex_path = NULL;
8776 int have_staged_files = 0;
8778 *branch_name = NULL;
8782 err = lock_worktree(worktree, LOCK_EX);
8786 err = open_fileindex(fileindex, &fileindex_path, worktree);
8790 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8791 &have_staged_files);
8792 if (err && err->code != GOT_ERR_CANCELLED)
8794 if (have_staged_files) {
8795 err = got_error(GOT_ERR_STAGED_PATHS);
8799 err = get_merge_branch_ref_name(&branch_refname, worktree);
8803 err = get_merge_commit_ref_name(&commit_refname, worktree);
8807 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8811 if (!got_ref_is_symbolic(branch_ref)) {
8812 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8813 "%s is not a symbolic reference",
8814 got_ref_get_name(branch_ref));
8817 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8818 if (*branch_name == NULL) {
8819 err = got_error_from_errno("strdup");
8823 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8827 err = got_ref_resolve(branch_tip, repo, commit_ref);
8831 free(commit_refname);
8832 free(branch_refname);
8833 free(fileindex_path);
8835 got_ref_close(commit_ref);
8837 got_ref_close(branch_ref);
8841 *branch_name = NULL;
8846 got_fileindex_free(*fileindex);
8849 lock_worktree(worktree, LOCK_SH);
8854 const struct got_error *
8855 got_worktree_merge_abort(struct got_worktree *worktree,
8856 struct got_fileindex *fileindex, struct got_repository *repo,
8857 got_worktree_checkout_cb progress_cb, void *progress_arg)
8859 const struct got_error *err, *unlockerr, *sync_err;
8860 struct got_commit_object *commit = NULL;
8861 char *fileindex_path = NULL;
8862 struct revert_file_args rfa;
8863 char *commit_ref_name = NULL;
8864 struct got_reference *commit_ref = NULL;
8865 struct got_object_id *merged_commit_id = NULL;
8866 struct got_object_id *tree_id = NULL;
8867 struct got_pathlist_head added_paths;
8869 TAILQ_INIT(&added_paths);
8871 err = get_merge_commit_ref_name(&commit_ref_name, worktree);
8875 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8879 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8884 * Determine which files in added status can be safely removed
8885 * from disk while reverting changes in the work tree.
8886 * We want to avoid deleting unrelated files which were added by
8887 * the user for conflict resolution purposes.
8889 err = get_paths_added_between_commits(&added_paths,
8890 got_worktree_get_base_commit_id(worktree), merged_commit_id,
8891 got_worktree_get_path_prefix(worktree), repo);
8896 err = got_object_open_as_commit(&commit, repo,
8897 worktree->base_commit_id);
8901 err = got_object_id_by_path(&tree_id, repo, commit,
8902 worktree->path_prefix);
8906 err = delete_merge_refs(worktree, repo);
8910 err = get_fileindex_path(&fileindex_path, worktree);
8914 rfa.worktree = worktree;
8915 rfa.fileindex = fileindex;
8916 rfa.progress_cb = progress_cb;
8917 rfa.progress_arg = progress_arg;
8918 rfa.patch_cb = NULL;
8919 rfa.patch_arg = NULL;
8921 rfa.unlink_added_files = 1;
8922 rfa.added_files_to_unlink = &added_paths;
8923 err = worktree_status(worktree, "", fileindex, repo,
8924 revert_file, &rfa, NULL, NULL, 1, 0);
8928 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8929 repo, progress_cb, progress_arg, NULL, NULL);
8931 sync_err = sync_fileindex(fileindex, fileindex_path);
8932 if (sync_err && err == NULL)
8936 free(merged_commit_id);
8938 got_object_commit_close(commit);
8940 got_fileindex_free(fileindex);
8941 free(fileindex_path);
8943 got_ref_close(commit_ref);
8944 free(commit_ref_name);
8946 unlockerr = lock_worktree(worktree, LOCK_SH);
8947 if (unlockerr && err == NULL)
8952 struct check_stage_ok_arg {
8953 struct got_object_id *head_commit_id;
8954 struct got_worktree *worktree;
8955 struct got_fileindex *fileindex;
8956 struct got_repository *repo;
8960 static const struct got_error *
8961 check_stage_ok(void *arg, unsigned char status,
8962 unsigned char staged_status, const char *relpath,
8963 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8964 struct got_object_id *commit_id, int dirfd, const char *de_name)
8966 struct check_stage_ok_arg *a = arg;
8967 const struct got_error *err = NULL;
8968 struct got_fileindex_entry *ie;
8969 struct got_object_id base_commit_id;
8970 struct got_object_id *base_commit_idp = NULL;
8971 char *in_repo_path = NULL, *p;
8973 if (status == GOT_STATUS_UNVERSIONED ||
8974 status == GOT_STATUS_NO_CHANGE)
8976 if (status == GOT_STATUS_NONEXISTENT)
8977 return got_error_set_errno(ENOENT, relpath);
8979 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8981 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8983 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8984 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8986 return got_error_from_errno("asprintf");
8988 if (got_fileindex_entry_has_commit(ie)) {
8989 base_commit_idp = got_fileindex_entry_get_commit_id(
8990 &base_commit_id, ie);
8993 if (status == GOT_STATUS_CONFLICT) {
8994 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8996 } else if (status != GOT_STATUS_ADD &&
8997 status != GOT_STATUS_MODIFY &&
8998 status != GOT_STATUS_DELETE) {
8999 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
9003 a->have_changes = 1;
9008 err = check_out_of_date(p, status, staged_status,
9009 blob_id, base_commit_idp, a->head_commit_id, a->repo,
9010 GOT_ERR_STAGE_OUT_OF_DATE);
9016 struct stage_path_arg {
9017 struct got_worktree *worktree;
9018 struct got_fileindex *fileindex;
9019 struct got_repository *repo;
9020 got_worktree_status_cb status_cb;
9022 got_worktree_patch_cb patch_cb;
9024 int staged_something;
9025 int allow_bad_symlinks;
9028 static const struct got_error *
9029 stage_path(void *arg, unsigned char status,
9030 unsigned char staged_status, const char *relpath,
9031 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9032 struct got_object_id *commit_id, int dirfd, const char *de_name)
9034 struct stage_path_arg *a = arg;
9035 const struct got_error *err = NULL;
9036 struct got_fileindex_entry *ie;
9037 char *ondisk_path = NULL, *path_content = NULL;
9039 struct got_object_id *new_staged_blob_id = NULL;
9042 if (status == GOT_STATUS_UNVERSIONED)
9045 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9047 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9049 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
9051 return got_error_from_errno("asprintf");
9054 case GOT_STATUS_ADD:
9055 case GOT_STATUS_MODIFY:
9056 /* XXX could sb.st_mode be passed in by our caller? */
9057 if (lstat(ondisk_path, &sb) == -1) {
9058 err = got_error_from_errno2("lstat", ondisk_path);
9062 if (status == GOT_STATUS_ADD) {
9063 int choice = GOT_PATCH_CHOICE_NONE;
9064 err = (*a->patch_cb)(&choice, a->patch_arg,
9065 status, ie->path, NULL, 1, 1);
9068 if (choice != GOT_PATCH_CHOICE_YES)
9071 err = create_patched_content(&path_content, 0,
9072 staged_blob_id ? staged_blob_id : blob_id,
9073 ondisk_path, dirfd, de_name, ie->path,
9074 a->repo, a->patch_cb, a->patch_arg);
9075 if (err || path_content == NULL)
9079 err = got_object_blob_create(&new_staged_blob_id,
9080 path_content ? path_content : ondisk_path, a->repo);
9083 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9084 SHA1_DIGEST_LENGTH);
9085 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
9086 stage = GOT_FILEIDX_STAGE_ADD;
9088 stage = GOT_FILEIDX_STAGE_MODIFY;
9089 got_fileindex_entry_stage_set(ie, stage);
9090 if (S_ISLNK(sb.st_mode)) {
9091 int is_bad_symlink = 0;
9092 if (!a->allow_bad_symlinks) {
9093 char target_path[PATH_MAX];
9095 target_len = readlink(ondisk_path, target_path,
9096 sizeof(target_path));
9097 if (target_len == -1) {
9098 err = got_error_from_errno2("readlink",
9102 err = is_bad_symlink_target(&is_bad_symlink,
9103 target_path, target_len, ondisk_path,
9104 a->worktree->root_path,
9105 a->worktree->meta_dir);
9108 if (is_bad_symlink) {
9109 err = got_error_path(ondisk_path,
9110 GOT_ERR_BAD_SYMLINK);
9115 got_fileindex_entry_staged_filetype_set(ie,
9116 GOT_FILEIDX_MODE_BAD_SYMLINK);
9118 got_fileindex_entry_staged_filetype_set(ie,
9119 GOT_FILEIDX_MODE_SYMLINK);
9121 got_fileindex_entry_staged_filetype_set(ie,
9122 GOT_FILEIDX_MODE_REGULAR_FILE);
9124 a->staged_something = 1;
9125 if (a->status_cb == NULL)
9127 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
9128 get_staged_status(ie), relpath, blob_id,
9129 new_staged_blob_id, NULL, dirfd, de_name);
9133 * When staging the reverse of the staged diff,
9134 * implicitly unstage the file.
9136 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
9137 sizeof(ie->blob_sha1)) == 0) {
9138 got_fileindex_entry_stage_set(ie,
9139 GOT_FILEIDX_STAGE_NONE);
9142 case GOT_STATUS_DELETE:
9143 if (staged_status == GOT_STATUS_DELETE)
9146 int choice = GOT_PATCH_CHOICE_NONE;
9147 err = (*a->patch_cb)(&choice, a->patch_arg, status,
9148 ie->path, NULL, 1, 1);
9151 if (choice == GOT_PATCH_CHOICE_NO)
9153 if (choice != GOT_PATCH_CHOICE_YES) {
9154 err = got_error(GOT_ERR_PATCH_CHOICE);
9158 stage = GOT_FILEIDX_STAGE_DELETE;
9159 got_fileindex_entry_stage_set(ie, stage);
9160 a->staged_something = 1;
9161 if (a->status_cb == NULL)
9163 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
9164 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
9167 case GOT_STATUS_NO_CHANGE:
9169 case GOT_STATUS_CONFLICT:
9170 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
9172 case GOT_STATUS_NONEXISTENT:
9173 err = got_error_set_errno(ENOENT, relpath);
9176 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
9180 if (path_content && unlink(path_content) == -1 && err == NULL)
9181 err = got_error_from_errno2("unlink", path_content);
9184 free(new_staged_blob_id);
9188 const struct got_error *
9189 got_worktree_stage(struct got_worktree *worktree,
9190 struct got_pathlist_head *paths,
9191 got_worktree_status_cb status_cb, void *status_arg,
9192 got_worktree_patch_cb patch_cb, void *patch_arg,
9193 int allow_bad_symlinks, struct got_repository *repo)
9195 const struct got_error *err = NULL, *sync_err, *unlockerr;
9196 struct got_pathlist_entry *pe;
9197 struct got_fileindex *fileindex = NULL;
9198 char *fileindex_path = NULL;
9199 struct got_reference *head_ref = NULL;
9200 struct got_object_id *head_commit_id = NULL;
9201 struct check_stage_ok_arg oka;
9202 struct stage_path_arg spa;
9204 err = lock_worktree(worktree, LOCK_EX);
9208 err = got_ref_open(&head_ref, repo,
9209 got_worktree_get_head_ref_name(worktree), 0);
9212 err = got_ref_resolve(&head_commit_id, repo, head_ref);
9215 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9219 /* Check pre-conditions before staging anything. */
9220 oka.head_commit_id = head_commit_id;
9221 oka.worktree = worktree;
9222 oka.fileindex = fileindex;
9224 oka.have_changes = 0;
9225 TAILQ_FOREACH(pe, paths, entry) {
9226 err = worktree_status(worktree, pe->path, fileindex, repo,
9227 check_stage_ok, &oka, NULL, NULL, 1, 0);
9231 if (!oka.have_changes) {
9232 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9236 spa.worktree = worktree;
9237 spa.fileindex = fileindex;
9239 spa.patch_cb = patch_cb;
9240 spa.patch_arg = patch_arg;
9241 spa.status_cb = status_cb;
9242 spa.status_arg = status_arg;
9243 spa.staged_something = 0;
9244 spa.allow_bad_symlinks = allow_bad_symlinks;
9245 TAILQ_FOREACH(pe, paths, entry) {
9246 err = worktree_status(worktree, pe->path, fileindex, repo,
9247 stage_path, &spa, NULL, NULL, 1, 0);
9251 if (!spa.staged_something) {
9252 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9256 sync_err = sync_fileindex(fileindex, fileindex_path);
9257 if (sync_err && err == NULL)
9261 got_ref_close(head_ref);
9262 free(head_commit_id);
9263 free(fileindex_path);
9265 got_fileindex_free(fileindex);
9266 unlockerr = lock_worktree(worktree, LOCK_SH);
9267 if (unlockerr && err == NULL)
9272 struct unstage_path_arg {
9273 struct got_worktree *worktree;
9274 struct got_fileindex *fileindex;
9275 struct got_repository *repo;
9276 got_worktree_checkout_cb progress_cb;
9278 got_worktree_patch_cb patch_cb;
9282 static const struct got_error *
9283 create_unstaged_content(char **path_unstaged_content,
9284 char **path_new_staged_content, struct got_object_id *blob_id,
9285 struct got_object_id *staged_blob_id, const char *relpath,
9286 struct got_repository *repo,
9287 got_worktree_patch_cb patch_cb, void *patch_arg)
9289 const struct got_error *err, *free_err;
9290 struct got_blob_object *blob = NULL, *staged_blob = NULL;
9291 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
9292 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
9293 struct got_diffreg_result *diffreg_result = NULL;
9294 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
9295 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
9296 int fd1 = -1, fd2 = -1;
9298 *path_unstaged_content = NULL;
9299 *path_new_staged_content = NULL;
9301 err = got_object_id_str(&label1, blob_id);
9305 fd1 = got_opentempfd();
9307 err = got_error_from_errno("got_opentempfd");
9310 fd2 = got_opentempfd();
9312 err = got_error_from_errno("got_opentempfd");
9316 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
9320 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
9324 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
9328 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
9333 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
9337 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
9341 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
9342 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
9346 err = got_opentemp_named(path_unstaged_content, &outfile,
9347 "got-unstaged-content", "");
9350 err = got_opentemp_named(path_new_staged_content, &rejectfile,
9351 "got-new-staged-content", "");
9355 if (fseek(f1, 0L, SEEK_SET) == -1) {
9356 err = got_ferror(f1, GOT_ERR_IO);
9359 if (fseek(f2, 0L, SEEK_SET) == -1) {
9360 err = got_ferror(f2, GOT_ERR_IO);
9363 /* Count the number of actual changes in the diff result. */
9364 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9365 struct diff_chunk_context cc = {};
9366 diff_chunk_context_load_change(&cc, &nchunks_used,
9367 diffreg_result->result, n, 0);
9370 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9372 err = apply_or_reject_change(&choice, &nchunks_used,
9373 diffreg_result->result, n, relpath, f1, f2,
9374 &line_cur1, &line_cur2,
9375 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
9378 if (choice == GOT_PATCH_CHOICE_YES)
9381 have_rejected_content = 1;
9382 if (choice == GOT_PATCH_CHOICE_QUIT)
9385 if (have_content || have_rejected_content)
9386 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
9387 outfile, rejectfile);
9390 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9391 err = got_error_from_errno("close");
9393 got_object_blob_close(blob);
9394 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9395 err = got_error_from_errno("close");
9397 got_object_blob_close(staged_blob);
9398 free_err = got_diffreg_result_free(diffreg_result);
9399 if (free_err && err == NULL)
9401 if (f1 && fclose(f1) == EOF && err == NULL)
9402 err = got_error_from_errno2("fclose", path1);
9403 if (f2 && fclose(f2) == EOF && err == NULL)
9404 err = got_error_from_errno2("fclose", path2);
9405 if (outfile && fclose(outfile) == EOF && err == NULL)
9406 err = got_error_from_errno2("fclose", *path_unstaged_content);
9407 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
9408 err = got_error_from_errno2("fclose", *path_new_staged_content);
9409 if (path1 && unlink(path1) == -1 && err == NULL)
9410 err = got_error_from_errno2("unlink", path1);
9411 if (path2 && unlink(path2) == -1 && err == NULL)
9412 err = got_error_from_errno2("unlink", path2);
9413 if (err || !have_content) {
9414 if (*path_unstaged_content &&
9415 unlink(*path_unstaged_content) == -1 && err == NULL)
9416 err = got_error_from_errno2("unlink",
9417 *path_unstaged_content);
9418 free(*path_unstaged_content);
9419 *path_unstaged_content = NULL;
9421 if (err || !have_content || !have_rejected_content) {
9422 if (*path_new_staged_content &&
9423 unlink(*path_new_staged_content) == -1 && err == NULL)
9424 err = got_error_from_errno2("unlink",
9425 *path_new_staged_content);
9426 free(*path_new_staged_content);
9427 *path_new_staged_content = NULL;
9434 static const struct got_error *
9435 unstage_hunks(struct got_object_id *staged_blob_id,
9436 struct got_blob_object *blob_base,
9437 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
9438 const char *ondisk_path, const char *label_orig,
9439 struct got_worktree *worktree, struct got_repository *repo,
9440 got_worktree_patch_cb patch_cb, void *patch_arg,
9441 got_worktree_checkout_cb progress_cb, void *progress_arg)
9443 const struct got_error *err = NULL;
9444 char *path_unstaged_content = NULL;
9445 char *path_new_staged_content = NULL;
9446 char *parent = NULL, *base_path = NULL;
9447 char *blob_base_path = NULL;
9448 struct got_object_id *new_staged_blob_id = NULL;
9449 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
9452 err = create_unstaged_content(&path_unstaged_content,
9453 &path_new_staged_content, blob_id, staged_blob_id,
9454 ie->path, repo, patch_cb, patch_arg);
9458 if (path_unstaged_content == NULL)
9461 if (path_new_staged_content) {
9462 err = got_object_blob_create(&new_staged_blob_id,
9463 path_new_staged_content, repo);
9468 f = fopen(path_unstaged_content, "re");
9470 err = got_error_from_errno2("fopen",
9471 path_unstaged_content);
9474 if (fstat(fileno(f), &sb) == -1) {
9475 err = got_error_from_errno2("fstat", path_unstaged_content);
9478 if (got_fileindex_entry_staged_filetype_get(ie) ==
9479 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
9480 char link_target[PATH_MAX];
9482 r = fread(link_target, 1, sizeof(link_target), f);
9483 if (r == 0 && ferror(f)) {
9484 err = got_error_from_errno("fread");
9487 if (r >= sizeof(link_target)) { /* should not happen */
9488 err = got_error(GOT_ERR_NO_SPACE);
9491 link_target[r] = '\0';
9492 err = merge_symlink(worktree, blob_base,
9493 ondisk_path, ie->path, label_orig, link_target,
9494 worktree->base_commit_id, repo, progress_cb,
9497 int local_changes_subsumed;
9499 err = got_path_dirname(&parent, ondisk_path);
9503 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
9505 err = got_error_from_errno("asprintf");
9510 err = got_opentemp_named(&blob_base_path, &f_base,
9514 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
9520 * In order the run a 3-way merge with a symlink we copy the symlink's
9521 * target path into a temporary file and use that file with diff3.
9523 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9524 err = dump_symlink_target_path_to_file(&f_deriv2,
9530 fd = open(ondisk_path,
9531 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
9533 err = got_error_from_errno2("open", ondisk_path);
9536 f_deriv2 = fdopen(fd, "r");
9537 if (f_deriv2 == NULL) {
9538 err = got_error_from_errno2("fdopen", ondisk_path);
9544 err = merge_file(&local_changes_subsumed, worktree,
9545 f_base, f, f_deriv2, ondisk_path, ie->path,
9546 got_fileindex_perms_to_st(ie),
9547 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
9548 repo, progress_cb, progress_arg);
9553 if (new_staged_blob_id) {
9554 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9555 SHA1_DIGEST_LENGTH);
9557 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9558 got_fileindex_entry_staged_filetype_set(ie, 0);
9561 free(new_staged_blob_id);
9562 if (path_unstaged_content &&
9563 unlink(path_unstaged_content) == -1 && err == NULL)
9564 err = got_error_from_errno2("unlink", path_unstaged_content);
9565 if (path_new_staged_content &&
9566 unlink(path_new_staged_content) == -1 && err == NULL)
9567 err = got_error_from_errno2("unlink", path_new_staged_content);
9568 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
9569 err = got_error_from_errno2("unlink", blob_base_path);
9570 if (f_base && fclose(f_base) == EOF && err == NULL)
9571 err = got_error_from_errno2("fclose", path_unstaged_content);
9572 if (f && fclose(f) == EOF && err == NULL)
9573 err = got_error_from_errno2("fclose", path_unstaged_content);
9574 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
9575 err = got_error_from_errno2("fclose", ondisk_path);
9576 free(path_unstaged_content);
9577 free(path_new_staged_content);
9578 free(blob_base_path);
9584 static const struct got_error *
9585 unstage_path(void *arg, unsigned char status,
9586 unsigned char staged_status, const char *relpath,
9587 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9588 struct got_object_id *commit_id, int dirfd, const char *de_name)
9590 const struct got_error *err = NULL;
9591 struct unstage_path_arg *a = arg;
9592 struct got_fileindex_entry *ie;
9593 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
9594 char *ondisk_path = NULL;
9595 char *id_str = NULL, *label_orig = NULL;
9596 int local_changes_subsumed;
9598 int fd1 = -1, fd2 = -1;
9600 if (staged_status != GOT_STATUS_ADD &&
9601 staged_status != GOT_STATUS_MODIFY &&
9602 staged_status != GOT_STATUS_DELETE)
9605 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9607 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9609 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
9611 return got_error_from_errno("asprintf");
9613 err = got_object_id_str(&id_str,
9614 commit_id ? commit_id : a->worktree->base_commit_id);
9617 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9619 err = got_error_from_errno("asprintf");
9623 fd1 = got_opentempfd();
9625 err = got_error_from_errno("got_opentempfd");
9628 fd2 = got_opentempfd();
9630 err = got_error_from_errno("got_opentempfd");
9634 switch (staged_status) {
9635 case GOT_STATUS_MODIFY:
9636 err = got_object_open_as_blob(&blob_base, a->repo,
9637 blob_id, 8192, fd1);
9641 case GOT_STATUS_ADD:
9643 if (staged_status == GOT_STATUS_ADD) {
9644 int choice = GOT_PATCH_CHOICE_NONE;
9645 err = (*a->patch_cb)(&choice, a->patch_arg,
9646 staged_status, ie->path, NULL, 1, 1);
9649 if (choice != GOT_PATCH_CHOICE_YES)
9652 err = unstage_hunks(staged_blob_id,
9653 blob_base, blob_id, ie, ondisk_path,
9654 label_orig, a->worktree, a->repo,
9655 a->patch_cb, a->patch_arg,
9656 a->progress_cb, a->progress_arg);
9657 break; /* Done with this file. */
9660 err = got_object_open_as_blob(&blob_staged, a->repo,
9661 staged_blob_id, 8192, fd2);
9664 switch (got_fileindex_entry_staged_filetype_get(ie)) {
9665 case GOT_FILEIDX_MODE_BAD_SYMLINK:
9666 case GOT_FILEIDX_MODE_REGULAR_FILE:
9667 err = merge_blob(&local_changes_subsumed, a->worktree,
9668 blob_base, ondisk_path, relpath,
9669 got_fileindex_perms_to_st(ie), label_orig,
9670 blob_staged, commit_id ? commit_id :
9671 a->worktree->base_commit_id, a->repo,
9672 a->progress_cb, a->progress_arg);
9674 case GOT_FILEIDX_MODE_SYMLINK:
9675 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9676 char *staged_target;
9677 err = got_object_blob_read_to_str(
9678 &staged_target, blob_staged);
9681 err = merge_symlink(a->worktree, blob_base,
9682 ondisk_path, relpath, label_orig,
9683 staged_target, commit_id ? commit_id :
9684 a->worktree->base_commit_id,
9685 a->repo, a->progress_cb, a->progress_arg);
9686 free(staged_target);
9688 err = merge_blob(&local_changes_subsumed,
9689 a->worktree, blob_base, ondisk_path,
9690 relpath, got_fileindex_perms_to_st(ie),
9691 label_orig, blob_staged,
9692 commit_id ? commit_id :
9693 a->worktree->base_commit_id, a->repo,
9694 a->progress_cb, a->progress_arg);
9698 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9702 got_fileindex_entry_stage_set(ie,
9703 GOT_FILEIDX_STAGE_NONE);
9704 got_fileindex_entry_staged_filetype_set(ie, 0);
9707 case GOT_STATUS_DELETE:
9709 int choice = GOT_PATCH_CHOICE_NONE;
9710 err = (*a->patch_cb)(&choice, a->patch_arg,
9711 staged_status, ie->path, NULL, 1, 1);
9714 if (choice == GOT_PATCH_CHOICE_NO)
9716 if (choice != GOT_PATCH_CHOICE_YES) {
9717 err = got_error(GOT_ERR_PATCH_CHOICE);
9721 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9722 got_fileindex_entry_staged_filetype_set(ie, 0);
9723 err = get_file_status(&status, &sb, ie, ondisk_path,
9724 dirfd, de_name, a->repo);
9727 err = (*a->progress_cb)(a->progress_arg, status, relpath);
9732 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9733 err = got_error_from_errno("close");
9735 got_object_blob_close(blob_base);
9736 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9737 err = got_error_from_errno("close");
9739 got_object_blob_close(blob_staged);
9745 const struct got_error *
9746 got_worktree_unstage(struct got_worktree *worktree,
9747 struct got_pathlist_head *paths,
9748 got_worktree_checkout_cb progress_cb, void *progress_arg,
9749 got_worktree_patch_cb patch_cb, void *patch_arg,
9750 struct got_repository *repo)
9752 const struct got_error *err = NULL, *sync_err, *unlockerr;
9753 struct got_pathlist_entry *pe;
9754 struct got_fileindex *fileindex = NULL;
9755 char *fileindex_path = NULL;
9756 struct unstage_path_arg upa;
9758 err = lock_worktree(worktree, LOCK_EX);
9762 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9766 upa.worktree = worktree;
9767 upa.fileindex = fileindex;
9769 upa.progress_cb = progress_cb;
9770 upa.progress_arg = progress_arg;
9771 upa.patch_cb = patch_cb;
9772 upa.patch_arg = patch_arg;
9773 TAILQ_FOREACH(pe, paths, entry) {
9774 err = worktree_status(worktree, pe->path, fileindex, repo,
9775 unstage_path, &upa, NULL, NULL, 1, 0);
9780 sync_err = sync_fileindex(fileindex, fileindex_path);
9781 if (sync_err && err == NULL)
9784 free(fileindex_path);
9786 got_fileindex_free(fileindex);
9787 unlockerr = lock_worktree(worktree, LOCK_SH);
9788 if (unlockerr && err == NULL)
9793 struct report_file_info_arg {
9794 struct got_worktree *worktree;
9795 got_worktree_path_info_cb info_cb;
9797 struct got_pathlist_head *paths;
9798 got_cancel_cb cancel_cb;
9802 static const struct got_error *
9803 report_file_info(void *arg, struct got_fileindex_entry *ie)
9805 const struct got_error *err;
9806 struct report_file_info_arg *a = arg;
9807 struct got_pathlist_entry *pe;
9808 struct got_object_id blob_id, staged_blob_id, commit_id;
9809 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9810 struct got_object_id *commit_idp = NULL;
9814 err = a->cancel_cb(a->cancel_arg);
9819 TAILQ_FOREACH(pe, a->paths, entry) {
9820 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9821 got_path_is_child(ie->path, pe->path, pe->path_len))
9824 if (pe == NULL) /* not found */
9827 if (got_fileindex_entry_has_blob(ie))
9828 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9829 stage = got_fileindex_entry_stage_get(ie);
9830 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9831 stage == GOT_FILEIDX_STAGE_ADD) {
9832 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9833 &staged_blob_id, ie);
9836 if (got_fileindex_entry_has_commit(ie))
9837 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9839 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9840 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9843 const struct got_error *
9844 got_worktree_path_info(struct got_worktree *worktree,
9845 struct got_pathlist_head *paths,
9846 got_worktree_path_info_cb info_cb, void *info_arg,
9847 got_cancel_cb cancel_cb, void *cancel_arg)
9850 const struct got_error *err = NULL, *unlockerr;
9851 struct got_fileindex *fileindex = NULL;
9852 char *fileindex_path = NULL;
9853 struct report_file_info_arg arg;
9855 err = lock_worktree(worktree, LOCK_SH);
9859 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9863 arg.worktree = worktree;
9864 arg.info_cb = info_cb;
9865 arg.info_arg = info_arg;
9867 arg.cancel_cb = cancel_cb;
9868 arg.cancel_arg = cancel_arg;
9869 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9872 free(fileindex_path);
9874 got_fileindex_free(fileindex);
9875 unlockerr = lock_worktree(worktree, LOCK_UN);
9876 if (unlockerr && err == NULL)
9881 static const struct got_error *
9882 patch_check_path(const char *p, char **path, unsigned char *status,
9883 unsigned char *staged_status, struct got_fileindex *fileindex,
9884 struct got_worktree *worktree, struct got_repository *repo)
9886 const struct got_error *err;
9887 struct got_fileindex_entry *ie;
9889 char *ondisk_path = NULL;
9891 err = got_worktree_resolve_path(path, worktree, p);
9895 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9896 *path[0] ? "/" : "", *path) == -1)
9897 return got_error_from_errno("asprintf");
9899 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9901 *staged_status = get_staged_status(ie);
9902 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9907 *staged_status = GOT_STATUS_NO_CHANGE;
9908 *status = GOT_STATUS_UNVERSIONED;
9909 if (lstat(ondisk_path, &sb) == -1) {
9910 if (errno != ENOENT) {
9911 err = got_error_from_errno2("lstat",
9915 *status = GOT_STATUS_NONEXISTENT;
9924 static const struct got_error *
9925 patch_can_rm(const char *path, unsigned char status,
9926 unsigned char staged_status)
9928 if (status == GOT_STATUS_NONEXISTENT)
9929 return got_error_set_errno(ENOENT, path);
9930 if (status != GOT_STATUS_NO_CHANGE &&
9931 status != GOT_STATUS_ADD &&
9932 status != GOT_STATUS_MODIFY &&
9933 status != GOT_STATUS_MODE_CHANGE)
9934 return got_error_path(path, GOT_ERR_FILE_STATUS);
9935 if (staged_status == GOT_STATUS_DELETE)
9936 return got_error_path(path, GOT_ERR_FILE_STATUS);
9940 static const struct got_error *
9941 patch_can_add(const char *path, unsigned char status)
9943 if (status != GOT_STATUS_NONEXISTENT)
9944 return got_error_path(path, GOT_ERR_FILE_STATUS);
9948 static const struct got_error *
9949 patch_can_edit(const char *path, unsigned char status,
9950 unsigned char staged_status)
9952 if (status == GOT_STATUS_NONEXISTENT)
9953 return got_error_set_errno(ENOENT, path);
9954 if (status != GOT_STATUS_NO_CHANGE &&
9955 status != GOT_STATUS_ADD &&
9956 status != GOT_STATUS_MODIFY)
9957 return got_error_path(path, GOT_ERR_FILE_STATUS);
9958 if (staged_status == GOT_STATUS_DELETE)
9959 return got_error_path(path, GOT_ERR_FILE_STATUS);
9963 const struct got_error *
9964 got_worktree_patch_prepare(struct got_fileindex **fileindex,
9965 char **fileindex_path, struct got_worktree *worktree)
9967 return open_fileindex(fileindex, fileindex_path, worktree);
9970 const struct got_error *
9971 got_worktree_patch_check_path(const char *old, const char *new,
9972 char **oldpath, char **newpath, struct got_worktree *worktree,
9973 struct got_repository *repo, struct got_fileindex *fileindex)
9975 const struct got_error *err = NULL;
9976 int file_renamed = 0;
9977 unsigned char status_old, staged_status_old;
9978 unsigned char status_new, staged_status_new;
9983 err = patch_check_path(old != NULL ? old : new, oldpath,
9984 &status_old, &staged_status_old, fileindex, worktree, repo);
9988 err = patch_check_path(new != NULL ? new : old, newpath,
9989 &status_new, &staged_status_new, fileindex, worktree, repo);
9993 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9996 if (old != NULL && new == NULL)
9997 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9998 else if (file_renamed) {
9999 err = patch_can_rm(*oldpath, status_old, staged_status_old);
10001 err = patch_can_add(*newpath, status_new);
10002 } else if (old == NULL)
10003 err = patch_can_add(*newpath, status_new);
10005 err = patch_can_edit(*newpath, status_new, staged_status_new);
10017 const struct got_error *
10018 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
10019 struct got_worktree *worktree, struct got_fileindex *fileindex,
10020 got_worktree_checkout_cb progress_cb, void *progress_arg)
10022 struct schedule_addition_args saa;
10024 memset(&saa, 0, sizeof(saa));
10025 saa.worktree = worktree;
10026 saa.fileindex = fileindex;
10027 saa.progress_cb = progress_cb;
10028 saa.progress_arg = progress_arg;
10031 return worktree_status(worktree, path, fileindex, repo,
10032 schedule_addition, &saa, NULL, NULL, 1, 0);
10035 const struct got_error *
10036 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
10037 struct got_worktree *worktree, struct got_fileindex *fileindex,
10038 got_worktree_delete_cb progress_cb, void *progress_arg)
10040 const struct got_error *err;
10041 struct schedule_deletion_args sda;
10042 char *ondisk_status_path;
10044 memset(&sda, 0, sizeof(sda));
10045 sda.worktree = worktree;
10046 sda.fileindex = fileindex;
10047 sda.progress_cb = progress_cb;
10048 sda.progress_arg = progress_arg;
10050 sda.delete_local_mods = 0;
10051 sda.keep_on_disk = 0;
10052 sda.ignore_missing_paths = 0;
10053 sda.status_codes = NULL;
10054 if (asprintf(&ondisk_status_path, "%s/%s",
10055 got_worktree_get_root_path(worktree), path) == -1)
10056 return got_error_from_errno("asprintf");
10057 sda.status_path = ondisk_status_path;
10058 sda.status_path_len = strlen(ondisk_status_path);
10060 err = worktree_status(worktree, path, fileindex, repo,
10061 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
10062 free(ondisk_status_path);
10066 const struct got_error *
10067 got_worktree_patch_complete(struct got_fileindex *fileindex,
10068 const char *fileindex_path)
10070 const struct got_error *err = NULL;
10072 err = sync_fileindex(fileindex, fileindex_path);
10073 got_fileindex_free(fileindex);