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, 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 directory (may already exist). */
189 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_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 GOT_WORKTREE_GOT_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 GOT_WORKTREE_GOT_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)
1106 const struct got_error *err = NULL;
1107 struct got_fileindex_entry *new_ie;
1111 err = got_fileindex_entry_alloc(&new_ie, path);
1115 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1116 blob_id->sha1, base_commit_id->sha1, 1);
1120 err = got_fileindex_entry_add(fileindex, new_ie);
1123 got_fileindex_entry_free(new_ie);
1130 get_ondisk_perms(int executable, mode_t st_mode)
1132 mode_t xbits = S_IXUSR;
1135 /* Map read bits to execute bits. */
1136 if (st_mode & S_IRGRP)
1138 if (st_mode & S_IROTH)
1140 return st_mode | xbits;
1146 /* forward declaration */
1147 static const struct got_error *
1148 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1149 const char *path, mode_t te_mode, mode_t st_mode,
1150 struct got_blob_object *blob, int restoring_missing_file,
1151 int reverting_versioned_file, int installing_bad_symlink,
1152 int path_is_unversioned, struct got_repository *repo,
1153 got_worktree_checkout_cb progress_cb, void *progress_arg);
1156 * This function assumes that the provided symlink target points at a
1157 * safe location in the work tree!
1159 static const struct got_error *
1160 replace_existing_symlink(int *did_something, const char *ondisk_path,
1161 const char *target_path, size_t target_len)
1163 const struct got_error *err = NULL;
1165 char etarget[PATH_MAX];
1171 * "Bad" symlinks (those pointing outside the work tree or into the
1172 * .got directory) are installed in the work tree as a regular file
1173 * which contains the bad symlink target path.
1174 * The new symlink target has already been checked for safety by our
1175 * caller. If we can successfully open a regular file then we simply
1176 * replace this file with a symlink below.
1178 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1180 if (!got_err_open_nofollow_on_symlink())
1181 return got_error_from_errno2("open", ondisk_path);
1183 /* We are updating an existing on-disk symlink. */
1184 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1186 return got_error_from_errno2("readlink", ondisk_path);
1188 if (elen == target_len &&
1189 memcmp(etarget, target_path, target_len) == 0)
1190 return NULL; /* nothing to do */
1194 err = update_symlink(ondisk_path, target_path, target_len);
1195 if (fd != -1 && close(fd) == -1 && err == NULL)
1196 err = got_error_from_errno2("close", ondisk_path);
1200 static const struct got_error *
1201 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1202 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1204 const struct got_error *err = NULL;
1205 char canonpath[PATH_MAX];
1206 char *path_got = NULL;
1208 *is_bad_symlink = 0;
1210 if (target_len >= sizeof(canonpath)) {
1211 *is_bad_symlink = 1;
1216 * We do not use realpath(3) to resolve the symlink's target
1217 * path because we don't want to resolve symlinks recursively.
1218 * Instead we make the path absolute and then canonicalize it.
1219 * Relative symlink target lookup should begin at the directory
1220 * in which the blob object is being installed.
1222 if (!got_path_is_absolute(target_path)) {
1223 char *abspath, *parent;
1224 err = got_path_dirname(&parent, ondisk_path);
1227 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1229 return got_error_from_errno("asprintf");
1232 if (strlen(abspath) >= sizeof(canonpath)) {
1233 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1237 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1242 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1247 /* Only allow symlinks pointing at paths within the work tree. */
1248 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1249 *is_bad_symlink = 1;
1253 /* Do not allow symlinks pointing into the .got directory. */
1254 if (asprintf(&path_got, "%s/%s", wtroot_path,
1255 GOT_WORKTREE_GOT_DIR) == -1)
1256 return got_error_from_errno("asprintf");
1257 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1258 *is_bad_symlink = 1;
1264 static const struct got_error *
1265 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1266 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1267 int restoring_missing_file, int reverting_versioned_file,
1268 int path_is_unversioned, int allow_bad_symlinks,
1269 struct got_repository *repo,
1270 got_worktree_checkout_cb progress_cb, void *progress_arg)
1272 const struct got_error *err = NULL;
1273 char target_path[PATH_MAX];
1274 size_t len, target_len = 0;
1275 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1276 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1278 *is_bad_symlink = 0;
1281 * Blob object content specifies the target path of the link.
1282 * If a symbolic link cannot be installed we instead create
1283 * a regular file which contains the link target path stored
1284 * in the blob object.
1287 err = got_object_blob_read_block(&len, blob);
1291 if (len + target_len >= sizeof(target_path)) {
1292 /* Path too long; install as a regular file. */
1293 *is_bad_symlink = 1;
1294 got_object_blob_rewind(blob);
1295 return install_blob(worktree, ondisk_path, path,
1296 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1297 restoring_missing_file, reverting_versioned_file,
1298 1, path_is_unversioned, repo, progress_cb,
1302 /* Skip blob object header first time around. */
1303 memcpy(target_path + target_len, buf + hdrlen,
1305 target_len += len - hdrlen;
1309 target_path[target_len] = '\0';
1311 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1312 ondisk_path, worktree->root_path);
1316 if (*is_bad_symlink && !allow_bad_symlinks) {
1317 /* install as a regular file */
1318 got_object_blob_rewind(blob);
1319 err = install_blob(worktree, ondisk_path, path,
1320 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1321 restoring_missing_file, reverting_versioned_file, 1,
1322 path_is_unversioned, repo, progress_cb, progress_arg);
1326 if (symlink(target_path, ondisk_path) == -1) {
1327 if (errno == EEXIST) {
1328 int symlink_replaced;
1329 if (path_is_unversioned) {
1330 err = (*progress_cb)(progress_arg,
1331 GOT_STATUS_UNVERSIONED, path);
1334 err = replace_existing_symlink(&symlink_replaced,
1335 ondisk_path, target_path, target_len);
1339 if (symlink_replaced) {
1340 err = (*progress_cb)(progress_arg,
1341 reverting_versioned_file ?
1343 GOT_STATUS_UPDATE, path);
1345 err = (*progress_cb)(progress_arg,
1346 GOT_STATUS_EXISTS, path);
1349 return err; /* Nothing else to do. */
1352 if (errno == ENOENT) {
1354 err = got_path_dirname(&parent, ondisk_path);
1357 err = got_path_mkdir(parent);
1362 * Retry, and fall through to error handling
1363 * below if this second attempt fails.
1365 if (symlink(target_path, ondisk_path) != -1) {
1366 err = NULL; /* success */
1368 err = (*progress_cb)(progress_arg,
1369 reverting_versioned_file ?
1370 GOT_STATUS_REVERT : GOT_STATUS_ADD,
1377 /* Handle errors from first or second creation attempt. */
1378 if (errno == ENAMETOOLONG) {
1379 /* bad target path; install as a regular file */
1380 *is_bad_symlink = 1;
1381 got_object_blob_rewind(blob);
1382 err = install_blob(worktree, ondisk_path, path,
1383 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1384 restoring_missing_file, reverting_versioned_file, 1,
1385 path_is_unversioned, repo,
1386 progress_cb, progress_arg);
1387 } else if (errno == ENOTDIR) {
1388 err = got_error_path(ondisk_path,
1389 GOT_ERR_FILE_OBSTRUCTED);
1391 err = got_error_from_errno3("symlink",
1392 target_path, ondisk_path);
1394 } else if (progress_cb)
1395 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1396 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1400 static const struct got_error *
1401 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1402 const char *path, mode_t te_mode, mode_t st_mode,
1403 struct got_blob_object *blob, int restoring_missing_file,
1404 int reverting_versioned_file, int installing_bad_symlink,
1405 int path_is_unversioned, struct got_repository *repo,
1406 got_worktree_checkout_cb progress_cb, void *progress_arg)
1408 const struct got_error *err = NULL;
1412 char *tmppath = NULL;
1415 mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1416 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1419 if (errno == ENOENT || errno == ENOTDIR) {
1421 err = got_path_dirname(&parent, path);
1424 err = add_dir_on_disk(worktree, parent);
1425 if (err && err->code == GOT_ERR_FILE_OBSTRUCTED)
1426 err = got_error_path(path, err->code);
1430 fd = open(ondisk_path,
1431 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1434 return got_error_from_errno2("open",
1436 } else if (errno == EEXIST) {
1437 if (path_is_unversioned) {
1438 err = (*progress_cb)(progress_arg,
1439 GOT_STATUS_UNVERSIONED, path);
1442 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1443 !S_ISREG(st_mode) && !installing_bad_symlink) {
1444 /* TODO file is obstructed; do something */
1445 err = got_error_path(ondisk_path,
1446 GOT_ERR_FILE_OBSTRUCTED);
1449 err = got_opentemp_named_fd(&tmppath, &fd,
1455 if (fchmod(fd, apply_umask(mode)) == -1) {
1456 err = got_error_from_errno2("fchmod",
1462 return got_error_from_errno2("open", ondisk_path);
1466 if (restoring_missing_file)
1467 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1469 else if (reverting_versioned_file)
1470 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1473 err = (*progress_cb)(progress_arg,
1474 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1479 hdrlen = got_object_blob_get_hdrlen(blob);
1481 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1482 err = got_object_blob_read_block(&len, blob);
1486 /* Skip blob object header first time around. */
1487 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1489 err = got_error_from_errno("write");
1491 } else if (outlen != len - hdrlen) {
1492 err = got_error(GOT_ERR_IO);
1499 if (fsync(fd) != 0) {
1500 err = got_error_from_errno("fsync");
1505 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1506 err = got_error_from_errno2("unlink", ondisk_path);
1509 if (rename(tmppath, ondisk_path) != 0) {
1510 err = got_error_from_errno3("rename", tmppath,
1519 if (fd != -1 && close(fd) == -1 && err == NULL)
1520 err = got_error_from_errno("close");
1521 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1522 err = got_error_from_errno2("unlink", tmppath);
1528 * Upgrade STATUS_MODIFY to STATUS_CONFLICT if a
1529 * conflict marker is found in newly added lines only.
1531 static const struct got_error *
1532 get_modified_file_content_status(unsigned char *status,
1533 struct got_blob_object *blob, const char *path, struct stat *sb,
1536 const struct got_error *err, *free_err;
1537 const char *markers[3] = {
1538 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1539 GOT_DIFF_CONFLICT_MARKER_SEP,
1540 GOT_DIFF_CONFLICT_MARKER_END
1543 struct got_diffreg_result *diffreg_result = NULL;
1544 struct diff_result *r;
1545 int nchunks_parsed, n, i = 0, ln = 0;
1547 size_t linesize = 0;
1550 if (*status != GOT_STATUS_MODIFY)
1553 f1 = got_opentemp();
1555 return got_error_from_errno("got_opentemp");
1558 got_object_blob_rewind(blob);
1559 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
1564 err = got_diff_files(&diffreg_result, f1, 1, NULL, ondisk_file,
1565 1, NULL, 0, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
1569 r = diffreg_result->result;
1571 for (n = 0; n < r->chunks.len; n += nchunks_parsed) {
1572 struct diff_chunk *c;
1573 struct diff_chunk_context cc = {};
1577 * We can optimise a little by advancing straight
1578 * to the next chunk if this one has no added lines.
1580 c = diff_chunk_get(r, n);
1582 if (diff_chunk_type(c) != CHUNK_PLUS) {
1584 continue; /* removed or unchanged lines */
1587 pos = diff_chunk_get_right_start_pos(c);
1588 if (fseek(ondisk_file, pos, SEEK_SET) == -1) {
1589 err = got_ferror(ondisk_file, GOT_ERR_IO);
1593 diff_chunk_context_load_change(&cc, &nchunks_parsed, r, n, 0);
1594 ln = cc.right.start;
1596 while (ln < cc.right.end) {
1597 linelen = getline(&line, &linesize, ondisk_file);
1598 if (linelen == -1) {
1599 if (feof(ondisk_file))
1601 err = got_ferror(ondisk_file, GOT_ERR_IO);
1605 if (line && strncmp(line, markers[i],
1606 strlen(markers[i])) == 0) {
1607 if (strcmp(markers[i],
1608 GOT_DIFF_CONFLICT_MARKER_END) == 0) {
1609 *status = GOT_STATUS_CONFLICT;
1620 if (f1 != NULL && fclose(f1) == EOF && err == NULL)
1621 err = got_error_from_errno("fclose");
1622 free_err = got_diffreg_result_free(diffreg_result);
1630 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1632 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1633 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1637 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1639 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1640 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1641 ie->mtime_sec == sb->st_mtim.tv_sec &&
1642 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1643 ie->size == (sb->st_size & 0xffffffff) &&
1644 !xbit_differs(ie, sb->st_mode));
1647 static unsigned char
1648 get_staged_status(struct got_fileindex_entry *ie)
1650 switch (got_fileindex_entry_stage_get(ie)) {
1651 case GOT_FILEIDX_STAGE_ADD:
1652 return GOT_STATUS_ADD;
1653 case GOT_FILEIDX_STAGE_DELETE:
1654 return GOT_STATUS_DELETE;
1655 case GOT_FILEIDX_STAGE_MODIFY:
1656 return GOT_STATUS_MODIFY;
1658 return GOT_STATUS_NO_CHANGE;
1662 static const struct got_error *
1663 get_symlink_modification_status(unsigned char *status,
1664 struct got_fileindex_entry *ie, const char *abspath,
1665 int dirfd, const char *de_name, struct got_blob_object *blob)
1667 const struct got_error *err = NULL;
1668 char target_path[PATH_MAX];
1669 char etarget[PATH_MAX];
1671 size_t len, target_len = 0;
1672 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1673 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1675 *status = GOT_STATUS_NO_CHANGE;
1677 /* Blob object content specifies the target path of the link. */
1679 err = got_object_blob_read_block(&len, blob);
1682 if (len + target_len >= sizeof(target_path)) {
1684 * Should not happen. The blob contents were OK
1685 * when this symlink was installed.
1687 return got_error(GOT_ERR_NO_SPACE);
1690 /* Skip blob object header first time around. */
1691 memcpy(target_path + target_len, buf + hdrlen,
1693 target_len += len - hdrlen;
1697 target_path[target_len] = '\0';
1700 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1702 return got_error_from_errno2("readlinkat", abspath);
1704 elen = readlink(abspath, etarget, sizeof(etarget));
1706 return got_error_from_errno2("readlink", abspath);
1709 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1710 *status = GOT_STATUS_MODIFY;
1715 static const struct got_error *
1716 get_file_status(unsigned char *status, struct stat *sb,
1717 struct got_fileindex_entry *ie, const char *abspath,
1718 int dirfd, const char *de_name, struct got_repository *repo)
1720 const struct got_error *err = NULL;
1721 struct got_object_id id;
1723 int fd = -1, fd1 = -1;
1726 struct got_blob_object *blob = NULL;
1728 unsigned char staged_status;
1730 staged_status = get_staged_status(ie);
1731 *status = GOT_STATUS_NO_CHANGE;
1732 memset(sb, 0, sizeof(*sb));
1735 * Whenever the caller provides a directory descriptor and a
1736 * directory entry name for the file, use them! This prevents
1737 * race conditions if filesystem paths change beneath our feet.
1740 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1741 if (errno == ENOENT) {
1742 if (got_fileindex_entry_has_file_on_disk(ie))
1743 *status = GOT_STATUS_MISSING;
1745 *status = GOT_STATUS_DELETE;
1748 err = got_error_from_errno2("fstatat", abspath);
1752 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1753 if (fd == -1 && errno != ENOENT &&
1754 !got_err_open_nofollow_on_symlink())
1755 return got_error_from_errno2("open", abspath);
1756 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1757 if (lstat(abspath, sb) == -1)
1758 return got_error_from_errno2("lstat", abspath);
1759 } else if (fd == -1 || fstat(fd, sb) == -1) {
1760 if (errno == ENOENT) {
1761 if (got_fileindex_entry_has_file_on_disk(ie))
1762 *status = GOT_STATUS_MISSING;
1764 *status = GOT_STATUS_DELETE;
1767 err = got_error_from_errno2("fstat", abspath);
1772 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1773 *status = GOT_STATUS_OBSTRUCTED;
1777 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1778 *status = GOT_STATUS_DELETE;
1780 } else if (!got_fileindex_entry_has_blob(ie) &&
1781 staged_status != GOT_STATUS_ADD) {
1782 *status = GOT_STATUS_ADD;
1786 if (!stat_info_differs(ie, sb))
1789 if (S_ISLNK(sb->st_mode) &&
1790 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1791 *status = GOT_STATUS_MODIFY;
1795 if (staged_status == GOT_STATUS_MODIFY ||
1796 staged_status == GOT_STATUS_ADD)
1797 got_fileindex_entry_get_staged_blob_id(&id, ie);
1799 got_fileindex_entry_get_blob_id(&id, ie);
1801 fd1 = got_opentempfd();
1803 err = got_error_from_errno("got_opentempfd");
1806 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1810 if (S_ISLNK(sb->st_mode)) {
1811 err = get_symlink_modification_status(status, ie,
1812 abspath, dirfd, de_name, blob);
1817 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1819 err = got_error_from_errno2("openat", abspath);
1824 f = fdopen(fd, "r");
1826 err = got_error_from_errno2("fdopen", abspath);
1830 hdrlen = got_object_blob_get_hdrlen(blob);
1832 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1833 err = got_object_blob_read_block(&blen, blob);
1836 /* Skip length of blob object header first time around. */
1837 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1838 if (flen == 0 && ferror(f)) {
1839 err = got_error_from_errno("fread");
1842 if (blen - hdrlen == 0) {
1844 *status = GOT_STATUS_MODIFY;
1846 } else if (flen == 0) {
1847 if (blen - hdrlen != 0)
1848 *status = GOT_STATUS_MODIFY;
1850 } else if (blen - hdrlen == flen) {
1851 /* Skip blob object header first time around. */
1852 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1853 *status = GOT_STATUS_MODIFY;
1857 *status = GOT_STATUS_MODIFY;
1863 if (*status == GOT_STATUS_MODIFY) {
1865 err = get_modified_file_content_status(status, blob, ie->path,
1867 } else if (xbit_differs(ie, sb->st_mode))
1868 *status = GOT_STATUS_MODE_CHANGE;
1870 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1871 err = got_error_from_errno("close");
1873 got_object_blob_close(blob);
1874 if (f != NULL && fclose(f) == EOF && err == NULL)
1875 err = got_error_from_errno2("fclose", abspath);
1876 if (fd != -1 && close(fd) == -1 && err == NULL)
1877 err = got_error_from_errno2("close", abspath);
1882 * Update timestamps in the file index if a file is unmodified and
1883 * we had to run a full content comparison to find out.
1885 static const struct got_error *
1886 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1887 struct got_fileindex_entry *ie, struct stat *sb)
1889 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1890 return got_fileindex_entry_update(ie, wt_fd, path,
1891 ie->blob_sha1, ie->commit_sha1, 1);
1896 static const struct got_error *remove_ondisk_file(const char *, const char *);
1898 static const struct got_error *
1899 update_blob(struct got_worktree *worktree,
1900 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1901 struct got_tree_entry *te, const char *path,
1902 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1905 const struct got_error *err = NULL;
1906 struct got_blob_object *blob = NULL;
1907 char *ondisk_path = NULL;
1908 unsigned char status = GOT_STATUS_NO_CHANGE;
1910 int fd1 = -1, fd2 = -1;
1912 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1913 return got_error_from_errno("asprintf");
1916 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1917 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1920 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1924 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1925 sb.st_mode = got_fileindex_perms_to_st(ie);
1927 if (stat(ondisk_path, &sb) == -1) {
1928 if (errno != ENOENT && errno != ENOTDIR) {
1929 err = got_error_from_errno2("stat",
1933 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1934 status = GOT_STATUS_UNVERSIONED;
1936 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1937 status = GOT_STATUS_UNVERSIONED;
1939 status = GOT_STATUS_OBSTRUCTED;
1943 if (status == GOT_STATUS_OBSTRUCTED) {
1945 got_fileindex_entry_mark_skipped(ie);
1946 err = (*progress_cb)(progress_arg, status, path);
1949 if (status == GOT_STATUS_CONFLICT) {
1951 got_fileindex_entry_mark_skipped(ie);
1952 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1957 if (S_ISDIR(te->mode)) { /* file changing into a directory */
1958 if (status == GOT_STATUS_UNVERSIONED) {
1959 err = (*progress_cb)(progress_arg, status, path);
1960 } else if (status != GOT_STATUS_NO_CHANGE &&
1961 status != GOT_STATUS_DELETE &&
1962 status != GOT_STATUS_NONEXISTENT &&
1963 status != GOT_STATUS_MISSING) {
1964 err = (*progress_cb)(progress_arg,
1965 GOT_STATUS_CANNOT_DELETE, path);
1967 if (status != GOT_STATUS_DELETE &&
1968 status != GOT_STATUS_NONEXISTENT &&
1969 status != GOT_STATUS_MISSING) {
1970 err = remove_ondisk_file(worktree->root_path,
1972 if (err && !(err->code == GOT_ERR_ERRNO &&
1976 got_fileindex_entry_remove(fileindex, ie);
1977 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE,
1980 goto done; /* nothing else to do */
1983 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1984 (S_ISLNK(te->mode) ||
1985 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1987 * This is a regular file or an installed bad symlink.
1988 * If the file index indicates that this file is already
1989 * up-to-date with respect to the repository we can skip
1990 * updating contents of this file.
1992 if (got_fileindex_entry_has_commit(ie) &&
1993 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1994 SHA1_DIGEST_LENGTH) == 0) {
1996 err = sync_timestamps(worktree->root_fd,
1997 path, status, ie, &sb);
2000 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2004 if (got_fileindex_entry_has_blob(ie) &&
2005 memcmp(ie->blob_sha1, te->id.sha1,
2006 SHA1_DIGEST_LENGTH) == 0) {
2007 /* Different commit but the same blob. */
2008 if (got_fileindex_entry_has_commit(ie)) {
2009 /* Update the base commit ID of this file. */
2010 memcpy(ie->commit_sha1,
2011 worktree->base_commit_id->sha1,
2012 sizeof(ie->commit_sha1));
2014 err = sync_timestamps(worktree->root_fd,
2015 path, status, ie, &sb);
2018 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2024 fd1 = got_opentempfd();
2026 err = got_error_from_errno("got_opentempfd");
2029 err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
2033 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
2034 int update_timestamps;
2035 struct got_blob_object *blob2 = NULL;
2036 char *label_orig = NULL;
2037 if (got_fileindex_entry_has_blob(ie)) {
2038 fd2 = got_opentempfd();
2040 err = got_error_from_errno("got_opentempfd");
2043 struct got_object_id id2;
2044 got_fileindex_entry_get_blob_id(&id2, ie);
2045 err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
2050 if (got_fileindex_entry_has_commit(ie)) {
2051 char id_str[SHA1_DIGEST_STRING_LENGTH];
2052 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2053 sizeof(id_str)) == NULL) {
2054 err = got_error_path(id_str,
2055 GOT_ERR_BAD_OBJ_ID_STR);
2058 if (asprintf(&label_orig, "%s: commit %s",
2059 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2060 err = got_error_from_errno("asprintf");
2064 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2066 err = got_object_blob_read_to_str(&link_target, blob);
2069 err = merge_symlink(worktree, blob2, ondisk_path, path,
2070 label_orig, link_target, worktree->base_commit_id,
2071 repo, progress_cb, progress_arg);
2074 err = merge_blob(&update_timestamps, worktree, blob2,
2075 ondisk_path, path, sb.st_mode, label_orig, blob,
2076 worktree->base_commit_id, repo,
2077 progress_cb, progress_arg);
2080 if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2081 err = got_error_from_errno("close");
2085 got_object_blob_close(blob2);
2089 * Do not update timestamps of files with local changes.
2090 * Otherwise, a future status walk would treat them as
2091 * unmodified files again.
2093 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2094 blob->id.sha1, worktree->base_commit_id->sha1,
2096 } else if (status == GOT_STATUS_MODE_CHANGE) {
2097 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2098 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2099 } else if (status == GOT_STATUS_DELETE) {
2100 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2103 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2104 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2108 int is_bad_symlink = 0;
2109 if (S_ISLNK(te->mode)) {
2110 err = install_symlink(&is_bad_symlink, worktree,
2111 ondisk_path, path, blob,
2112 status == GOT_STATUS_MISSING, 0,
2113 status == GOT_STATUS_UNVERSIONED, 0,
2114 repo, progress_cb, progress_arg);
2116 err = install_blob(worktree, ondisk_path, path,
2117 te->mode, sb.st_mode, blob,
2118 status == GOT_STATUS_MISSING, 0, 0,
2119 status == GOT_STATUS_UNVERSIONED, repo,
2120 progress_cb, progress_arg);
2126 err = got_fileindex_entry_update(ie,
2127 worktree->root_fd, path, blob->id.sha1,
2128 worktree->base_commit_id->sha1, 1);
2130 err = create_fileindex_entry(&ie, fileindex,
2131 worktree->base_commit_id, worktree->root_fd, path,
2137 if (is_bad_symlink) {
2138 got_fileindex_entry_filetype_set(ie,
2139 GOT_FILEIDX_MODE_BAD_SYMLINK);
2143 if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2144 err = got_error_from_errno("close");
2147 got_object_blob_close(blob);
2153 static const struct got_error *
2154 remove_ondisk_file(const char *root_path, const char *path)
2156 const struct got_error *err = NULL;
2157 char *ondisk_path = NULL, *parent = NULL;
2159 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2160 return got_error_from_errno("asprintf");
2162 if (unlink(ondisk_path) == -1) {
2163 if (errno != ENOENT)
2164 err = got_error_from_errno2("unlink", ondisk_path);
2166 size_t root_len = strlen(root_path);
2167 err = got_path_dirname(&parent, ondisk_path);
2170 while (got_path_cmp(parent, root_path,
2171 strlen(parent), root_len) != 0) {
2173 ondisk_path = parent;
2175 if (rmdir(ondisk_path) == -1) {
2176 if (errno != ENOTEMPTY)
2177 err = got_error_from_errno2("rmdir",
2181 err = got_path_dirname(&parent, ondisk_path);
2192 static const struct got_error *
2193 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2194 struct got_fileindex_entry *ie, struct got_repository *repo,
2195 got_worktree_checkout_cb progress_cb, void *progress_arg)
2197 const struct got_error *err = NULL;
2198 unsigned char status;
2202 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2203 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2205 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2207 return got_error_from_errno("asprintf");
2209 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2213 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2214 char ondisk_target[PATH_MAX];
2215 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2216 sizeof(ondisk_target));
2217 if (ondisk_len == -1) {
2218 err = got_error_from_errno2("readlink", ondisk_path);
2221 ondisk_target[ondisk_len] = '\0';
2222 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2223 NULL, NULL, /* XXX pass common ancestor info? */
2224 ondisk_target, ondisk_path);
2227 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2232 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2233 status == GOT_STATUS_ADD) {
2234 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2238 * Preserve the working file and change the deleted blob's
2239 * entry into a schedule-add entry.
2241 err = got_fileindex_entry_update(ie, worktree->root_fd,
2242 ie->path, NULL, NULL, 0);
2244 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2247 if (status == GOT_STATUS_NO_CHANGE) {
2248 err = remove_ondisk_file(worktree->root_path, ie->path);
2252 got_fileindex_entry_remove(fileindex, ie);
2259 struct diff_cb_arg {
2260 struct got_fileindex *fileindex;
2261 struct got_worktree *worktree;
2262 struct got_repository *repo;
2263 got_worktree_checkout_cb progress_cb;
2265 got_cancel_cb cancel_cb;
2269 static const struct got_error *
2270 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2271 struct got_tree_entry *te, const char *parent_path)
2273 struct diff_cb_arg *a = arg;
2275 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2276 return got_error(GOT_ERR_CANCELLED);
2278 return update_blob(a->worktree, a->fileindex, ie, te,
2279 ie->path, a->repo, a->progress_cb, a->progress_arg);
2282 static const struct got_error *
2283 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2285 struct diff_cb_arg *a = arg;
2287 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2288 return got_error(GOT_ERR_CANCELLED);
2290 return delete_blob(a->worktree, a->fileindex, ie,
2291 a->repo, a->progress_cb, a->progress_arg);
2294 static const struct got_error *
2295 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2297 struct diff_cb_arg *a = arg;
2298 const struct got_error *err;
2301 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2302 return got_error(GOT_ERR_CANCELLED);
2304 if (got_object_tree_entry_is_submodule(te))
2307 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2310 if (asprintf(&path, "%s%s%s", parent_path,
2311 parent_path[0] ? "/" : "", te->name)
2313 return got_error_from_errno("asprintf");
2315 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2316 a->repo, a->progress_cb, a->progress_arg);
2322 const struct got_error *
2323 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2325 uint32_t uuid_status;
2327 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2328 if (uuid_status != uuid_s_ok) {
2330 return got_error_uuid(uuid_status, "uuid_to_string");
2336 static const struct got_error *
2337 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2339 const struct got_error *err = NULL;
2340 char *uuidstr = NULL;
2344 err = got_worktree_get_uuid(&uuidstr, worktree);
2348 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2349 err = got_error_from_errno("asprintf");
2356 const struct got_error *
2357 got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2360 return get_ref_name(refname, worktree, prefix);
2363 static const struct got_error *
2364 get_base_ref_name(char **refname, struct got_worktree *worktree)
2366 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2369 static const struct got_error *
2370 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2372 return get_ref_name(refname, worktree,
2373 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2376 static const struct got_error *
2377 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2379 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2382 static const struct got_error *
2383 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2385 return get_ref_name(refname, worktree,
2386 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2389 static const struct got_error *
2390 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2392 return get_ref_name(refname, worktree,
2393 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2396 static const struct got_error *
2397 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2399 return get_ref_name(refname, worktree,
2400 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2403 static const struct got_error *
2404 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2406 return get_ref_name(refname, worktree,
2407 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2410 static const struct got_error *
2411 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2413 return get_ref_name(refname, worktree,
2414 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2417 static const struct got_error *
2418 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2420 return get_ref_name(refname, worktree,
2421 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2424 const struct got_error *
2425 got_worktree_get_histedit_script_path(char **path,
2426 struct got_worktree *worktree)
2428 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2429 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2431 return got_error_from_errno("asprintf");
2436 static const struct got_error *
2437 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2439 return get_ref_name(refname, worktree,
2440 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2443 static const struct got_error *
2444 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2446 return get_ref_name(refname, worktree,
2447 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2451 * Prevent Git's garbage collector from deleting our base commit by
2452 * setting a reference to our base commit's ID.
2454 static const struct got_error *
2455 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2457 const struct got_error *err = NULL;
2458 struct got_reference *ref = NULL;
2461 err = get_base_ref_name(&refname, worktree);
2465 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2469 err = got_ref_write(ref, repo);
2477 static const struct got_error *
2478 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2480 const struct got_error *err = NULL;
2482 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2483 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2484 err = got_error_from_errno("asprintf");
2485 *fileindex_path = NULL;
2491 static const struct got_error *
2492 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2493 struct got_worktree *worktree)
2495 const struct got_error *err = NULL;
2498 *fileindex_path = NULL;
2499 *fileindex = got_fileindex_alloc();
2500 if (*fileindex == NULL)
2501 return got_error_from_errno("got_fileindex_alloc");
2503 err = get_fileindex_path(fileindex_path, worktree);
2507 index = fopen(*fileindex_path, "rbe");
2508 if (index == NULL) {
2509 if (errno != ENOENT)
2510 err = got_error_from_errno2("fopen", *fileindex_path);
2512 err = got_fileindex_read(*fileindex, index);
2513 if (fclose(index) == EOF && err == NULL)
2514 err = got_error_from_errno("fclose");
2518 free(*fileindex_path);
2519 *fileindex_path = NULL;
2520 got_fileindex_free(*fileindex);
2526 struct bump_base_commit_id_arg {
2527 struct got_object_id *base_commit_id;
2530 const char *entry_name;
2531 got_worktree_checkout_cb progress_cb;
2535 static const struct got_error *
2536 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2538 const struct got_error *err;
2539 struct bump_base_commit_id_arg *a = arg;
2541 if (a->entry_name) {
2542 if (strcmp(ie->path, a->path) != 0)
2544 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2547 if (got_fileindex_entry_was_skipped(ie))
2550 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2551 SHA1_DIGEST_LENGTH) == 0)
2554 if (a->progress_cb) {
2555 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2560 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2564 /* Bump base commit ID of all files within an updated part of the work tree. */
2565 static const struct got_error *
2566 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2567 struct got_fileindex *fileindex,
2568 got_worktree_checkout_cb progress_cb, void *progress_arg)
2570 struct bump_base_commit_id_arg bbc_arg;
2572 bbc_arg.base_commit_id = worktree->base_commit_id;
2573 bbc_arg.entry_name = NULL;
2575 bbc_arg.path_len = 0;
2576 bbc_arg.progress_cb = progress_cb;
2577 bbc_arg.progress_arg = progress_arg;
2579 return got_fileindex_for_each_entry_safe(fileindex,
2580 bump_base_commit_id, &bbc_arg);
2583 static const struct got_error *
2584 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2586 const struct got_error *err = NULL;
2587 char *new_fileindex_path = NULL;
2588 FILE *new_index = NULL;
2589 struct timespec timeout;
2591 err = got_opentemp_named(&new_fileindex_path, &new_index,
2592 fileindex_path, "");
2596 err = got_fileindex_write(fileindex, new_index);
2600 if (rename(new_fileindex_path, fileindex_path) != 0) {
2601 err = got_error_from_errno3("rename", new_fileindex_path,
2603 unlink(new_fileindex_path);
2607 * Sleep for a short amount of time to ensure that files modified after
2608 * this program exits have a different time stamp from the one which
2609 * was recorded in the file index.
2612 timeout.tv_nsec = 1;
2613 nanosleep(&timeout, NULL);
2617 free(new_fileindex_path);
2621 static const struct got_error *
2622 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2623 struct got_object_id **tree_id, const char *wt_relpath,
2624 struct got_commit_object *base_commit, struct got_worktree *worktree,
2625 struct got_repository *repo)
2627 const struct got_error *err = NULL;
2628 struct got_object_id *id = NULL;
2629 char *in_repo_path = NULL;
2630 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2632 *entry_type = GOT_OBJ_TYPE_ANY;
2633 *tree_relpath = NULL;
2636 if (wt_relpath[0] == '\0') {
2637 /* Check out all files within the work tree. */
2638 *entry_type = GOT_OBJ_TYPE_TREE;
2639 *tree_relpath = strdup("");
2640 if (*tree_relpath == NULL) {
2641 err = got_error_from_errno("strdup");
2644 err = got_object_id_by_path(tree_id, repo, base_commit,
2645 worktree->path_prefix);
2651 /* Check out a subset of files in the work tree. */
2653 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2654 is_root_wt ? "" : "/", wt_relpath) == -1) {
2655 err = got_error_from_errno("asprintf");
2659 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2664 in_repo_path = NULL;
2666 err = got_object_get_type(entry_type, repo, id);
2670 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2671 /* Check out a single file. */
2672 if (strchr(wt_relpath, '/') == NULL) {
2673 /* Check out a single file in work tree's root dir. */
2674 in_repo_path = strdup(worktree->path_prefix);
2675 if (in_repo_path == NULL) {
2676 err = got_error_from_errno("strdup");
2679 *tree_relpath = strdup("");
2680 if (*tree_relpath == NULL) {
2681 err = got_error_from_errno("strdup");
2685 /* Check out a single file in a subdirectory. */
2686 err = got_path_dirname(tree_relpath, wt_relpath);
2689 if (asprintf(&in_repo_path, "%s%s%s",
2690 worktree->path_prefix, is_root_wt ? "" : "/",
2691 *tree_relpath) == -1) {
2692 err = got_error_from_errno("asprintf");
2696 err = got_object_id_by_path(tree_id, repo,
2697 base_commit, in_repo_path);
2699 /* Check out all files within a subdirectory. */
2700 *tree_id = got_object_id_dup(id);
2701 if (*tree_id == NULL) {
2702 err = got_error_from_errno("got_object_id_dup");
2705 *tree_relpath = strdup(wt_relpath);
2706 if (*tree_relpath == NULL) {
2707 err = got_error_from_errno("strdup");
2715 *entry_type = GOT_OBJ_TYPE_ANY;
2716 free(*tree_relpath);
2717 *tree_relpath = NULL;
2724 static const struct got_error *
2725 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2726 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2727 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2728 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2730 const struct got_error *err = NULL;
2731 struct got_commit_object *commit = NULL;
2732 struct got_tree_object *tree = NULL;
2733 struct got_fileindex_diff_tree_cb diff_cb;
2734 struct diff_cb_arg arg;
2736 err = ref_base_commit(worktree, repo);
2738 if (!(err->code == GOT_ERR_ERRNO &&
2739 (errno == EACCES || errno == EROFS)))
2741 err = (*progress_cb)(progress_arg,
2742 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2747 err = got_object_open_as_commit(&commit, repo,
2748 worktree->base_commit_id);
2752 err = got_object_open_as_tree(&tree, repo, tree_id);
2757 got_object_tree_find_entry(tree, entry_name) == NULL) {
2758 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2762 diff_cb.diff_old_new = diff_old_new;
2763 diff_cb.diff_old = diff_old;
2764 diff_cb.diff_new = diff_new;
2765 arg.fileindex = fileindex;
2766 arg.worktree = worktree;
2768 arg.progress_cb = progress_cb;
2769 arg.progress_arg = progress_arg;
2770 arg.cancel_cb = cancel_cb;
2771 arg.cancel_arg = cancel_arg;
2772 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2773 entry_name, repo, &diff_cb, &arg);
2776 got_object_tree_close(tree);
2778 got_object_commit_close(commit);
2782 const struct got_error *
2783 got_worktree_checkout_files(struct got_worktree *worktree,
2784 struct got_pathlist_head *paths, struct got_repository *repo,
2785 got_worktree_checkout_cb progress_cb, void *progress_arg,
2786 got_cancel_cb cancel_cb, void *cancel_arg)
2788 const struct got_error *err = NULL, *sync_err, *unlockerr;
2789 struct got_commit_object *commit = NULL;
2790 struct got_tree_object *tree = NULL;
2791 struct got_fileindex *fileindex = NULL;
2792 char *fileindex_path = NULL;
2793 struct got_pathlist_entry *pe;
2794 struct tree_path_data {
2795 STAILQ_ENTRY(tree_path_data) entry;
2796 struct got_object_id *tree_id;
2801 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2803 STAILQ_INIT(&tree_paths);
2805 err = lock_worktree(worktree, LOCK_EX);
2809 err = got_object_open_as_commit(&commit, repo,
2810 worktree->base_commit_id);
2814 /* Map all specified paths to in-repository trees. */
2815 TAILQ_FOREACH(pe, paths, entry) {
2816 tpd = malloc(sizeof(*tpd));
2818 err = got_error_from_errno("malloc");
2822 err = find_tree_entry_for_checkout(&tpd->entry_type,
2823 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2830 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2831 err = got_path_basename(&tpd->entry_name, pe->path);
2839 tpd->entry_name = NULL;
2841 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2845 * Read the file index.
2846 * Checking out files is supposed to be an idempotent operation.
2847 * If the on-disk file index is incomplete we will try to complete it.
2849 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2853 tpd = STAILQ_FIRST(&tree_paths);
2854 TAILQ_FOREACH(pe, paths, entry) {
2855 struct bump_base_commit_id_arg bbc_arg;
2857 err = checkout_files(worktree, fileindex, tpd->relpath,
2858 tpd->tree_id, tpd->entry_name, repo,
2859 progress_cb, progress_arg, cancel_cb, cancel_arg);
2863 bbc_arg.base_commit_id = worktree->base_commit_id;
2864 bbc_arg.entry_name = tpd->entry_name;
2865 bbc_arg.path = pe->path;
2866 bbc_arg.path_len = pe->path_len;
2867 bbc_arg.progress_cb = progress_cb;
2868 bbc_arg.progress_arg = progress_arg;
2869 err = got_fileindex_for_each_entry_safe(fileindex,
2870 bump_base_commit_id, &bbc_arg);
2874 tpd = STAILQ_NEXT(tpd, entry);
2876 sync_err = sync_fileindex(fileindex, fileindex_path);
2877 if (sync_err && err == NULL)
2880 free(fileindex_path);
2882 got_object_tree_close(tree);
2884 got_object_commit_close(commit);
2886 got_fileindex_free(fileindex);
2887 while (!STAILQ_EMPTY(&tree_paths)) {
2888 tpd = STAILQ_FIRST(&tree_paths);
2889 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2894 unlockerr = lock_worktree(worktree, LOCK_SH);
2895 if (unlockerr && err == NULL)
2900 static const struct got_error *
2901 add_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2902 struct got_fileindex_entry *ie, const char *ondisk_path,
2903 const char *path2, struct got_blob_object *blob2, mode_t mode2,
2904 int restoring_missing_file, int reverting_versioned_file,
2905 int path_is_unversioned, int allow_bad_symlinks,
2906 struct got_repository *repo,
2907 got_worktree_checkout_cb progress_cb, void *progress_arg)
2909 const struct got_error *err = NULL;
2910 int is_bad_symlink = 0;
2912 if (S_ISLNK(mode2)) {
2913 err = install_symlink(&is_bad_symlink,
2914 worktree, ondisk_path, path2, blob2,
2915 restoring_missing_file,
2916 reverting_versioned_file,
2917 path_is_unversioned, allow_bad_symlinks,
2918 repo, progress_cb, progress_arg);
2920 err = install_blob(worktree, ondisk_path, path2,
2921 mode2, GOT_DEFAULT_FILE_MODE, blob2,
2922 restoring_missing_file, reverting_versioned_file, 0,
2923 path_is_unversioned, repo, progress_cb, progress_arg);
2928 /* Adding an unversioned file. */
2929 err = got_fileindex_entry_alloc(&ie, path2);
2932 err = got_fileindex_entry_update(ie,
2933 worktree->root_fd, path2, NULL, NULL, 1);
2935 got_fileindex_entry_free(ie);
2938 err = got_fileindex_entry_add(fileindex, ie);
2940 got_fileindex_entry_free(ie);
2944 /* Re-adding a locally deleted file. */
2945 err = got_fileindex_entry_update(ie,
2946 worktree->root_fd, path2, ie->blob_sha1,
2947 worktree->base_commit_id->sha1, 0);
2952 if (is_bad_symlink) {
2953 got_fileindex_entry_filetype_set(ie,
2954 GOT_FILEIDX_MODE_BAD_SYMLINK);
2960 struct merge_file_cb_arg {
2961 struct got_worktree *worktree;
2962 struct got_fileindex *fileindex;
2963 got_worktree_checkout_cb progress_cb;
2965 got_cancel_cb cancel_cb;
2967 const char *label_orig;
2968 struct got_object_id *commit_id2;
2969 int allow_bad_symlinks;
2972 static const struct got_error *
2973 merge_file_cb(void *arg, struct got_blob_object *blob1,
2974 struct got_blob_object *blob2, FILE *f1, FILE *f2,
2975 struct got_object_id *id1, struct got_object_id *id2,
2976 const char *path1, const char *path2,
2977 mode_t mode1, mode_t mode2, struct got_repository *repo)
2979 static const struct got_error *err = NULL;
2980 struct merge_file_cb_arg *a = arg;
2981 struct got_fileindex_entry *ie;
2982 char *ondisk_path = NULL;
2984 unsigned char status;
2985 int local_changes_subsumed;
2986 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2987 char *id_str = NULL, *label_deriv2 = NULL;
2989 if (blob1 && blob2) {
2990 ie = got_fileindex_entry_get(a->fileindex, path2,
2993 return (*a->progress_cb)(a->progress_arg,
2994 GOT_STATUS_MISSING, path2);
2996 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2998 return got_error_from_errno("asprintf");
3000 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3005 if (status == GOT_STATUS_DELETE) {
3006 err = (*a->progress_cb)(a->progress_arg,
3007 GOT_STATUS_MERGE, path2);
3010 if (status != GOT_STATUS_NO_CHANGE &&
3011 status != GOT_STATUS_MODIFY &&
3012 status != GOT_STATUS_CONFLICT &&
3013 status != GOT_STATUS_ADD) {
3014 err = (*a->progress_cb)(a->progress_arg, status, path2);
3018 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
3020 err = got_object_blob_read_to_str(&link_target2, blob2);
3023 err = merge_symlink(a->worktree, blob1, ondisk_path,
3024 path2, a->label_orig, link_target2, a->commit_id2,
3025 repo, a->progress_cb, a->progress_arg);
3030 f_orig = got_opentemp();
3031 if (f_orig == NULL) {
3032 err = got_error_from_errno("got_opentemp");
3035 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3040 f_deriv2 = got_opentemp();
3041 if (f_deriv2 == NULL)
3043 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3048 fd = open(ondisk_path,
3049 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3051 err = got_error_from_errno2("open",
3055 f_deriv = fdopen(fd, "r");
3056 if (f_deriv == NULL) {
3057 err = got_error_from_errno2("fdopen",
3062 err = got_object_id_str(&id_str, a->commit_id2);
3065 if (asprintf(&label_deriv2, "%s: commit %s",
3066 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
3067 err = got_error_from_errno("asprintf");
3070 err = merge_file(&local_changes_subsumed, a->worktree,
3071 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
3072 mode2, a->label_orig, NULL, label_deriv2,
3073 GOT_DIFF_ALGORITHM_PATIENCE, repo,
3074 a->progress_cb, a->progress_arg);
3077 ie = got_fileindex_entry_get(a->fileindex, path1,
3080 return (*a->progress_cb)(a->progress_arg,
3081 GOT_STATUS_MISSING, path1);
3083 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3085 return got_error_from_errno("asprintf");
3087 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3093 case GOT_STATUS_NO_CHANGE:
3094 err = (*a->progress_cb)(a->progress_arg,
3095 GOT_STATUS_DELETE, path1);
3098 err = remove_ondisk_file(a->worktree->root_path, path1);
3102 got_fileindex_entry_mark_deleted_from_disk(ie);
3104 case GOT_STATUS_DELETE:
3105 case GOT_STATUS_MISSING:
3106 err = (*a->progress_cb)(a->progress_arg,
3107 GOT_STATUS_DELETE, path1);
3111 got_fileindex_entry_mark_deleted_from_disk(ie);
3113 case GOT_STATUS_ADD: {
3114 struct got_object_id *id;
3118 * Delete the added file only if its content already
3119 * exists in the repository.
3121 err = got_object_blob_file_create(&id, &blob1_f,
3122 &blob1_size, path1);
3125 if (got_object_id_cmp(id, id1) == 0) {
3126 err = (*a->progress_cb)(a->progress_arg,
3127 GOT_STATUS_DELETE, path1);
3130 err = remove_ondisk_file(a->worktree->root_path,
3135 got_fileindex_entry_remove(a->fileindex,
3138 err = (*a->progress_cb)(a->progress_arg,
3139 GOT_STATUS_CANNOT_DELETE, path1);
3141 if (fclose(blob1_f) == EOF && err == NULL)
3142 err = got_error_from_errno("fclose");
3148 case GOT_STATUS_MODIFY:
3149 case GOT_STATUS_CONFLICT:
3150 err = (*a->progress_cb)(a->progress_arg,
3151 GOT_STATUS_CANNOT_DELETE, path1);
3155 case GOT_STATUS_OBSTRUCTED:
3156 err = (*a->progress_cb)(a->progress_arg, status, path1);
3164 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3166 return got_error_from_errno("asprintf");
3167 ie = got_fileindex_entry_get(a->fileindex, path2,
3170 err = get_file_status(&status, &sb, ie, ondisk_path,
3174 if (status != GOT_STATUS_NO_CHANGE &&
3175 status != GOT_STATUS_MODIFY &&
3176 status != GOT_STATUS_CONFLICT &&
3177 status != GOT_STATUS_ADD &&
3178 status != GOT_STATUS_DELETE) {
3179 err = (*a->progress_cb)(a->progress_arg,
3183 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3185 err = got_object_blob_read_to_str(&link_target2,
3189 err = merge_symlink(a->worktree, NULL,
3190 ondisk_path, path2, a->label_orig,
3191 link_target2, a->commit_id2, repo,
3192 a->progress_cb, a->progress_arg);
3194 } else if (S_ISREG(sb.st_mode)) {
3195 err = merge_blob(&local_changes_subsumed,
3196 a->worktree, NULL, ondisk_path, path2,
3197 sb.st_mode, a->label_orig, blob2,
3198 a->commit_id2, repo, a->progress_cb,
3200 } else if (status != GOT_STATUS_DELETE) {
3201 err = got_error_path(ondisk_path,
3202 GOT_ERR_FILE_OBSTRUCTED);
3206 if (status == GOT_STATUS_DELETE) {
3207 /* Re-add file with content from new blob. */
3208 err = add_file(a->worktree, a->fileindex, ie,
3209 ondisk_path, path2, blob2, mode2,
3210 0, 0, 0, a->allow_bad_symlinks,
3211 repo, a->progress_cb, a->progress_arg);
3216 err = add_file(a->worktree, a->fileindex, NULL,
3217 ondisk_path, path2, blob2, mode2,
3218 0, 0, 1, a->allow_bad_symlinks,
3219 repo, a->progress_cb, a->progress_arg);
3225 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3226 err = got_error_from_errno("fclose");
3227 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3228 err = got_error_from_errno("fclose");
3229 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3230 err = got_error_from_errno("fclose");
3237 static const struct got_error *
3238 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3240 struct got_worktree *worktree = arg;
3242 /* Reject merges into a work tree with mixed base commits. */
3243 if (got_fileindex_entry_has_commit(ie) &&
3244 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3245 SHA1_DIGEST_LENGTH) != 0)
3246 return got_error(GOT_ERR_MIXED_COMMITS);
3251 struct check_merge_conflicts_arg {
3252 struct got_worktree *worktree;
3253 struct got_fileindex *fileindex;
3254 struct got_repository *repo;
3257 static const struct got_error *
3258 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3259 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3260 struct got_object_id *id1, struct got_object_id *id2,
3261 const char *path1, const char *path2,
3262 mode_t mode1, mode_t mode2, struct got_repository *repo)
3264 const struct got_error *err = NULL;
3265 struct check_merge_conflicts_arg *a = arg;
3266 unsigned char status;
3268 struct got_fileindex_entry *ie;
3269 const char *path = path2 ? path2 : path1;
3270 struct got_object_id *id = id2 ? id2 : id1;
3276 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3280 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3282 return got_error_from_errno("asprintf");
3284 /* Reject merges into a work tree with conflicted files. */
3285 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3289 if (status == GOT_STATUS_CONFLICT)
3290 return got_error(GOT_ERR_CONFLICTS);
3295 static const struct got_error *
3296 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3297 const char *fileindex_path, struct got_object_id *commit_id1,
3298 struct got_object_id *commit_id2, struct got_repository *repo,
3299 got_worktree_checkout_cb progress_cb, void *progress_arg,
3300 got_cancel_cb cancel_cb, void *cancel_arg)
3302 const struct got_error *err = NULL, *sync_err;
3303 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3304 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3305 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3306 struct check_merge_conflicts_arg cmc_arg;
3307 struct merge_file_cb_arg arg;
3308 char *label_orig = NULL;
3309 FILE *f1 = NULL, *f2 = NULL;
3310 int fd1 = -1, fd2 = -1;
3313 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3316 err = got_object_id_by_path(&tree_id1, repo, commit1,
3317 worktree->path_prefix);
3318 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3324 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3328 err = got_object_id_str(&id_str, commit_id1);
3332 if (asprintf(&label_orig, "%s: commit %s",
3333 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3334 err = got_error_from_errno("asprintf");
3340 f1 = got_opentemp();
3342 err = got_error_from_errno("got_opentemp");
3347 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3351 err = got_object_id_by_path(&tree_id2, repo, commit2,
3352 worktree->path_prefix);
3356 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3360 f2 = got_opentemp();
3362 err = got_error_from_errno("got_opentemp");
3366 fd1 = got_opentempfd();
3368 err = got_error_from_errno("got_opentempfd");
3372 fd2 = got_opentempfd();
3374 err = got_error_from_errno("got_opentempfd");
3378 cmc_arg.worktree = worktree;
3379 cmc_arg.fileindex = fileindex;
3380 cmc_arg.repo = repo;
3381 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3382 check_merge_conflicts, &cmc_arg, 0);
3386 arg.worktree = worktree;
3387 arg.fileindex = fileindex;
3388 arg.progress_cb = progress_cb;
3389 arg.progress_arg = progress_arg;
3390 arg.cancel_cb = cancel_cb;
3391 arg.cancel_arg = cancel_arg;
3392 arg.label_orig = label_orig;
3393 arg.commit_id2 = commit_id2;
3394 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3395 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3396 merge_file_cb, &arg, 1);
3397 sync_err = sync_fileindex(fileindex, fileindex_path);
3398 if (sync_err && err == NULL)
3402 got_object_commit_close(commit1);
3404 got_object_commit_close(commit2);
3406 got_object_tree_close(tree1);
3408 got_object_tree_close(tree2);
3409 if (f1 && fclose(f1) == EOF && err == NULL)
3410 err = got_error_from_errno("fclose");
3411 if (f2 && fclose(f2) == EOF && err == NULL)
3412 err = got_error_from_errno("fclose");
3413 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3414 err = got_error_from_errno("close");
3415 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3416 err = got_error_from_errno("close");
3421 const struct got_error *
3422 got_worktree_merge_files(struct got_worktree *worktree,
3423 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3424 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3425 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3427 const struct got_error *err, *unlockerr;
3428 char *fileindex_path = NULL;
3429 struct got_fileindex *fileindex = NULL;
3431 err = lock_worktree(worktree, LOCK_EX);
3435 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3439 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3444 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3445 commit_id2, repo, progress_cb, progress_arg,
3446 cancel_cb, cancel_arg);
3449 got_fileindex_free(fileindex);
3450 free(fileindex_path);
3451 unlockerr = lock_worktree(worktree, LOCK_SH);
3452 if (unlockerr && err == NULL)
3457 struct diff_dir_cb_arg {
3458 struct got_fileindex *fileindex;
3459 struct got_worktree *worktree;
3460 const char *status_path;
3461 size_t status_path_len;
3462 struct got_repository *repo;
3463 got_worktree_status_cb status_cb;
3465 got_cancel_cb cancel_cb;
3467 /* A pathlist containing per-directory pathlists of ignore patterns. */
3468 struct got_pathlist_head *ignores;
3469 int report_unchanged;
3473 static const struct got_error *
3474 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3475 int dirfd, const char *de_name,
3476 got_worktree_status_cb status_cb, void *status_arg,
3477 struct got_repository *repo, int report_unchanged)
3479 const struct got_error *err = NULL;
3480 unsigned char status = GOT_STATUS_NO_CHANGE;
3481 unsigned char staged_status;
3483 struct got_object_id blob_id, commit_id, staged_blob_id;
3484 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3485 struct got_object_id *staged_blob_idp = NULL;
3487 staged_status = get_staged_status(ie);
3488 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3492 if (status == GOT_STATUS_NO_CHANGE &&
3493 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3496 if (got_fileindex_entry_has_blob(ie))
3497 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3498 if (got_fileindex_entry_has_commit(ie))
3499 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3500 if (staged_status == GOT_STATUS_ADD ||
3501 staged_status == GOT_STATUS_MODIFY) {
3502 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3503 &staged_blob_id, ie);
3506 return (*status_cb)(status_arg, status, staged_status,
3507 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3510 static const struct got_error *
3511 status_old_new(void *arg, struct got_fileindex_entry *ie,
3512 struct dirent *de, const char *parent_path, int dirfd)
3514 const struct got_error *err = NULL;
3515 struct diff_dir_cb_arg *a = arg;
3518 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3519 return got_error(GOT_ERR_CANCELLED);
3521 if (got_path_cmp(parent_path, a->status_path,
3522 strlen(parent_path), a->status_path_len) != 0 &&
3523 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3526 if (parent_path[0]) {
3527 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3528 parent_path, de->d_name) == -1)
3529 return got_error_from_errno("asprintf");
3531 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3533 return got_error_from_errno("asprintf");
3536 err = report_file_status(ie, abspath, dirfd, de->d_name,
3537 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3542 static const struct got_error *
3543 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3545 struct diff_dir_cb_arg *a = arg;
3546 struct got_object_id blob_id, commit_id;
3547 unsigned char status;
3549 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3550 return got_error(GOT_ERR_CANCELLED);
3552 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3555 got_fileindex_entry_get_blob_id(&blob_id, ie);
3556 got_fileindex_entry_get_commit_id(&commit_id, ie);
3557 if (got_fileindex_entry_has_file_on_disk(ie))
3558 status = GOT_STATUS_MISSING;
3560 status = GOT_STATUS_DELETE;
3561 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3562 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3566 free_ignores(struct got_pathlist_head *ignores)
3568 struct got_pathlist_entry *pe;
3570 TAILQ_FOREACH(pe, ignores, entry) {
3571 struct got_pathlist_head *ignorelist = pe->data;
3573 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3575 got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3578 static const struct got_error *
3579 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3581 const struct got_error *err = NULL;
3582 struct got_pathlist_entry *pe = NULL;
3583 struct got_pathlist_head *ignorelist;
3584 char *line = NULL, *pattern, *dirpath = NULL;
3585 size_t linesize = 0;
3588 ignorelist = calloc(1, sizeof(*ignorelist));
3589 if (ignorelist == NULL)
3590 return got_error_from_errno("calloc");
3591 TAILQ_INIT(ignorelist);
3593 while ((linelen = getline(&line, &linesize, f)) != -1) {
3594 if (linelen > 0 && line[linelen - 1] == '\n')
3595 line[linelen - 1] = '\0';
3597 /* Git's ignores may contain comments. */
3601 /* Git's negated patterns are not (yet?) supported. */
3605 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3607 err = got_error_from_errno("asprintf");
3610 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3615 err = got_error_from_errno("getline");
3619 dirpath = strdup(path);
3620 if (dirpath == NULL) {
3621 err = got_error_from_errno("strdup");
3624 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3627 if (err || pe == NULL) {
3629 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3635 match_path(const char *pattern, size_t pattern_len, const char *path,
3641 * Trailing slashes signify directories.
3642 * Append a * to make such patterns conform to fnmatch rules.
3644 if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
3645 if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
3646 return FNM_NOMATCH; /* XXX */
3648 return fnmatch(buf, path, flags);
3651 return fnmatch(pattern, path, flags);
3655 match_ignores(struct got_pathlist_head *ignores, const char *path)
3657 struct got_pathlist_entry *pe;
3659 /* Handle patterns which match in all directories. */
3660 TAILQ_FOREACH(pe, ignores, entry) {
3661 struct got_pathlist_head *ignorelist = pe->data;
3662 struct got_pathlist_entry *pi;
3664 TAILQ_FOREACH(pi, ignorelist, entry) {
3667 if (pi->path_len < 3 ||
3668 strncmp(pi->path, "**/", 3) != 0)
3672 if (match_path(pi->path + 3,
3673 pi->path_len - 3, p,
3674 FNM_PATHNAME | FNM_LEADING_DIR)) {
3675 /* Retry in next directory. */
3676 while (*p && *p != '/')
3688 * The ignores pathlist contains ignore lists from children before
3689 * parents, so we can find the most specific ignorelist by walking
3690 * ignores backwards.
3692 pe = TAILQ_LAST(ignores, got_pathlist_head);
3694 if (got_path_is_child(path, pe->path, pe->path_len)) {
3695 struct got_pathlist_head *ignorelist = pe->data;
3696 struct got_pathlist_entry *pi;
3697 TAILQ_FOREACH(pi, ignorelist, entry) {
3698 int flags = FNM_LEADING_DIR;
3699 if (strstr(pi->path, "/**/") == NULL)
3700 flags |= FNM_PATHNAME;
3701 if (match_path(pi->path, pi->path_len,
3707 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3713 static const struct got_error *
3714 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3715 const char *path, int dirfd, const char *ignores_filename)
3717 const struct got_error *err = NULL;
3720 FILE *ignoresfile = NULL;
3722 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3723 path[0] ? "/" : "", ignores_filename) == -1)
3724 return got_error_from_errno("asprintf");
3727 fd = openat(dirfd, ignores_filename,
3728 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3730 if (errno != ENOENT && errno != EACCES)
3731 err = got_error_from_errno2("openat",
3734 ignoresfile = fdopen(fd, "r");
3735 if (ignoresfile == NULL)
3736 err = got_error_from_errno2("fdopen",
3740 err = read_ignores(ignores, path, ignoresfile);
3744 ignoresfile = fopen(ignorespath, "re");
3745 if (ignoresfile == NULL) {
3746 if (errno != ENOENT && errno != EACCES)
3747 err = got_error_from_errno2("fopen",
3750 err = read_ignores(ignores, path, ignoresfile);
3753 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3754 err = got_error_from_errno2("fclose", path);
3755 if (fd != -1 && close(fd) == -1 && err == NULL)
3756 err = got_error_from_errno2("close", path);
3761 static const struct got_error *
3762 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3765 const struct got_error *err = NULL;
3766 struct diff_dir_cb_arg *a = arg;
3772 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3773 return got_error(GOT_ERR_CANCELLED);
3775 if (parent_path[0]) {
3776 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3777 return got_error_from_errno("asprintf");
3782 if (de->d_type == DT_DIR) {
3783 if (!a->no_ignores && ignore != NULL &&
3784 match_ignores(a->ignores, path))
3786 } else if (!match_ignores(a->ignores, path) &&
3787 got_path_is_child(path, a->status_path, a->status_path_len))
3788 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3789 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3795 static const struct got_error *
3796 status_traverse(void *arg, const char *path, int dirfd)
3798 const struct got_error *err = NULL;
3799 struct diff_dir_cb_arg *a = arg;
3804 err = add_ignores(a->ignores, a->worktree->root_path,
3805 path, dirfd, ".cvsignore");
3809 err = add_ignores(a->ignores, a->worktree->root_path, path,
3810 dirfd, ".gitignore");
3815 static const struct got_error *
3816 report_single_file_status(const char *path, const char *ondisk_path,
3817 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3818 void *status_arg, struct got_repository *repo, int report_unchanged,
3819 struct got_pathlist_head *ignores, int no_ignores)
3821 struct got_fileindex_entry *ie;
3824 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3826 return report_file_status(ie, ondisk_path, -1, NULL,
3827 status_cb, status_arg, repo, report_unchanged);
3829 if (lstat(ondisk_path, &sb) == -1) {
3830 if (errno != ENOENT)
3831 return got_error_from_errno2("lstat", ondisk_path);
3832 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3833 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3836 if (!no_ignores && match_ignores(ignores, path))
3839 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3840 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3841 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3846 static const struct got_error *
3847 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3848 const char *root_path, const char *path)
3850 const struct got_error *err;
3851 char *parent_path, *next_parent_path = NULL;
3853 err = add_ignores(ignores, root_path, "", -1,
3858 err = add_ignores(ignores, root_path, "", -1,
3863 err = got_path_dirname(&parent_path, path);
3865 if (err->code == GOT_ERR_BAD_PATH)
3866 return NULL; /* cannot traverse parent */
3870 err = add_ignores(ignores, root_path, parent_path, -1,
3874 err = add_ignores(ignores, root_path, parent_path, -1,
3878 err = got_path_dirname(&next_parent_path, parent_path);
3880 if (err->code == GOT_ERR_BAD_PATH)
3881 err = NULL; /* traversed everything */
3884 if (got_path_is_root_dir(parent_path))
3887 parent_path = next_parent_path;
3888 next_parent_path = NULL;
3892 free(next_parent_path);
3896 struct find_missing_children_args {
3897 const char *parent_path;
3899 struct got_pathlist_head *children;
3900 got_cancel_cb cancel_cb;
3904 static const struct got_error *
3905 find_missing_children(void *arg, struct got_fileindex_entry *ie)
3907 const struct got_error *err = NULL;
3908 struct find_missing_children_args *a = arg;
3911 err = a->cancel_cb(a->cancel_arg);
3916 if (got_path_is_child(ie->path, a->parent_path, a->parent_len))
3917 err = got_pathlist_append(a->children, ie->path, NULL);
3922 static const struct got_error *
3923 report_children(struct got_pathlist_head *children,
3924 struct got_worktree *worktree, struct got_fileindex *fileindex,
3925 struct got_repository *repo, int is_root_dir, int report_unchanged,
3926 struct got_pathlist_head *ignores, int no_ignores,
3927 got_worktree_status_cb status_cb, void *status_arg,
3928 got_cancel_cb cancel_cb, void *cancel_arg)
3930 const struct got_error *err = NULL;
3931 struct got_pathlist_entry *pe;
3932 char *ondisk_path = NULL;
3934 TAILQ_FOREACH(pe, children, entry) {
3936 err = cancel_cb(cancel_arg);
3941 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
3942 !is_root_dir ? "/" : "", pe->path) == -1) {
3943 err = got_error_from_errno("asprintf");
3948 err = report_single_file_status(pe->path, ondisk_path,
3949 fileindex, status_cb, status_arg, repo, report_unchanged,
3950 ignores, no_ignores);
3962 static const struct got_error *
3963 worktree_status(struct got_worktree *worktree, const char *path,
3964 struct got_fileindex *fileindex, struct got_repository *repo,
3965 got_worktree_status_cb status_cb, void *status_arg,
3966 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3967 int report_unchanged)
3969 const struct got_error *err = NULL;
3971 struct got_fileindex_diff_dir_cb fdiff_cb;
3972 struct diff_dir_cb_arg arg;
3973 char *ondisk_path = NULL;
3974 struct got_pathlist_head ignores, missing_children;
3975 struct got_fileindex_entry *ie;
3977 TAILQ_INIT(&ignores);
3978 TAILQ_INIT(&missing_children);
3980 if (asprintf(&ondisk_path, "%s%s%s",
3981 worktree->root_path, path[0] ? "/" : "", path) == -1)
3982 return got_error_from_errno("asprintf");
3984 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3986 err = report_single_file_status(path, ondisk_path,
3987 fileindex, status_cb, status_arg, repo,
3988 report_unchanged, &ignores, no_ignores);
3991 struct find_missing_children_args fmca;
3992 fmca.parent_path = path;
3993 fmca.parent_len = strlen(path);
3994 fmca.children = &missing_children;
3995 fmca.cancel_cb = cancel_cb;
3996 fmca.cancel_arg = cancel_arg;
3997 err = got_fileindex_for_each_entry_safe(fileindex,
3998 find_missing_children, &fmca);
4003 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
4005 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
4006 !got_err_open_nofollow_on_symlink())
4007 err = got_error_from_errno2("open", ondisk_path);
4010 err = add_ignores_from_parent_paths(&ignores,
4011 worktree->root_path, ondisk_path);
4015 if (TAILQ_EMPTY(&missing_children)) {
4016 err = report_single_file_status(path,
4017 ondisk_path, fileindex,
4018 status_cb, status_arg, repo,
4019 report_unchanged, &ignores, no_ignores);
4023 err = report_children(&missing_children,
4024 worktree, fileindex, repo,
4025 (path[0] == '\0'), report_unchanged,
4026 &ignores, no_ignores,
4027 status_cb, status_arg,
4028 cancel_cb, cancel_arg);
4034 fdiff_cb.diff_old_new = status_old_new;
4035 fdiff_cb.diff_old = status_old;
4036 fdiff_cb.diff_new = status_new;
4037 fdiff_cb.diff_traverse = status_traverse;
4038 arg.fileindex = fileindex;
4039 arg.worktree = worktree;
4040 arg.status_path = path;
4041 arg.status_path_len = strlen(path);
4043 arg.status_cb = status_cb;
4044 arg.status_arg = status_arg;
4045 arg.cancel_cb = cancel_cb;
4046 arg.cancel_arg = cancel_arg;
4047 arg.report_unchanged = report_unchanged;
4048 arg.no_ignores = no_ignores;
4050 err = add_ignores_from_parent_paths(&ignores,
4051 worktree->root_path, path);
4055 arg.ignores = &ignores;
4056 err = got_fileindex_diff_dir(fileindex, fd,
4057 worktree->root_path, path, repo, &fdiff_cb, &arg);
4060 free_ignores(&ignores);
4061 if (fd != -1 && close(fd) == -1 && err == NULL)
4062 err = got_error_from_errno("close");
4067 const struct got_error *
4068 got_worktree_status(struct got_worktree *worktree,
4069 struct got_pathlist_head *paths, struct got_repository *repo,
4070 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
4071 got_cancel_cb cancel_cb, void *cancel_arg)
4073 const struct got_error *err = NULL;
4074 char *fileindex_path = NULL;
4075 struct got_fileindex *fileindex = NULL;
4076 struct got_pathlist_entry *pe;
4078 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4082 TAILQ_FOREACH(pe, paths, entry) {
4083 err = worktree_status(worktree, pe->path, fileindex, repo,
4084 status_cb, status_arg, cancel_cb, cancel_arg,
4089 free(fileindex_path);
4090 got_fileindex_free(fileindex);
4094 const struct got_error *
4095 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
4098 const struct got_error *err = NULL;
4099 char *resolved = NULL, *cwd = NULL, *path = NULL;
4102 char *abspath = NULL;
4103 char canonpath[PATH_MAX];
4107 cwd = getcwd(NULL, 0);
4109 return got_error_from_errno("getcwd");
4111 if (lstat(arg, &sb) == -1) {
4112 if (errno != ENOENT) {
4113 err = got_error_from_errno2("lstat", arg);
4118 if (S_ISLNK(sb.st_mode)) {
4120 * We cannot use realpath(3) with symlinks since we want to
4121 * operate on the symlink itself.
4122 * But we can make the path absolute, assuming it is relative
4123 * to the current working directory, and then canonicalize it.
4125 if (!got_path_is_absolute(arg)) {
4126 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4127 err = got_error_from_errno("asprintf");
4132 err = got_canonpath(abspath ? abspath : arg, canonpath,
4136 resolved = strdup(canonpath);
4137 if (resolved == NULL) {
4138 err = got_error_from_errno("strdup");
4142 resolved = realpath(arg, NULL);
4143 if (resolved == NULL) {
4144 if (errno != ENOENT) {
4145 err = got_error_from_errno2("realpath", arg);
4148 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4149 err = got_error_from_errno("asprintf");
4152 err = got_canonpath(abspath, canonpath,
4156 resolved = strdup(canonpath);
4157 if (resolved == NULL) {
4158 err = got_error_from_errno("strdup");
4164 if (strncmp(got_worktree_get_root_path(worktree), resolved,
4165 strlen(got_worktree_get_root_path(worktree)))) {
4166 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
4170 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
4171 err = got_path_skip_common_ancestor(&path,
4172 got_worktree_get_root_path(worktree), resolved);
4178 err = got_error_from_errno("strdup");
4183 /* XXX status walk can't deal with trailing slash! */
4185 while (len > 0 && path[len - 1] == '/') {
4186 path[len - 1] = '\0';
4200 struct schedule_addition_args {
4201 struct got_worktree *worktree;
4202 struct got_fileindex *fileindex;
4203 got_worktree_checkout_cb progress_cb;
4205 struct got_repository *repo;
4209 add_noop_status(unsigned char status)
4211 return (status == GOT_STATUS_ADD ||
4212 status == GOT_STATUS_MODIFY ||
4213 status == GOT_STATUS_CONFLICT ||
4214 status == GOT_STATUS_MODE_CHANGE ||
4215 status == GOT_STATUS_NO_CHANGE);
4218 static const struct got_error *
4219 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4220 const char *relpath, struct got_object_id *blob_id,
4221 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4222 int dirfd, const char *de_name)
4224 struct schedule_addition_args *a = arg;
4225 const struct got_error *err = NULL;
4226 struct got_fileindex_entry *ie;
4230 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4232 return got_error_from_errno("asprintf");
4234 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4236 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4240 /* Re-adding an existing entry is a no-op. */
4241 if (staged_status == GOT_STATUS_NO_CHANGE &&
4242 add_noop_status(status))
4244 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4249 if (status != GOT_STATUS_UNVERSIONED) {
4250 if (status == GOT_STATUS_NONEXISTENT)
4251 err = got_error_set_errno(ENOENT, ondisk_path);
4253 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4257 err = got_fileindex_entry_alloc(&ie, relpath);
4260 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4261 relpath, NULL, NULL, 1);
4263 got_fileindex_entry_free(ie);
4266 err = got_fileindex_entry_add(a->fileindex, ie);
4268 got_fileindex_entry_free(ie);
4275 if (staged_status == GOT_STATUS_NO_CHANGE && add_noop_status(status))
4277 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4280 const struct got_error *
4281 got_worktree_schedule_add(struct got_worktree *worktree,
4282 struct got_pathlist_head *paths,
4283 got_worktree_checkout_cb progress_cb, void *progress_arg,
4284 struct got_repository *repo, int no_ignores)
4286 struct got_fileindex *fileindex = NULL;
4287 char *fileindex_path = NULL;
4288 const struct got_error *err = NULL, *sync_err, *unlockerr;
4289 struct got_pathlist_entry *pe;
4290 struct schedule_addition_args saa;
4292 err = lock_worktree(worktree, LOCK_EX);
4296 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4300 saa.worktree = worktree;
4301 saa.fileindex = fileindex;
4302 saa.progress_cb = progress_cb;
4303 saa.progress_arg = progress_arg;
4306 TAILQ_FOREACH(pe, paths, entry) {
4307 err = worktree_status(worktree, pe->path, fileindex, repo,
4308 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4312 sync_err = sync_fileindex(fileindex, fileindex_path);
4313 if (sync_err && err == NULL)
4316 free(fileindex_path);
4318 got_fileindex_free(fileindex);
4319 unlockerr = lock_worktree(worktree, LOCK_SH);
4320 if (unlockerr && err == NULL)
4325 struct schedule_deletion_args {
4326 struct got_worktree *worktree;
4327 struct got_fileindex *fileindex;
4328 got_worktree_delete_cb progress_cb;
4330 struct got_repository *repo;
4331 int delete_local_mods;
4333 int ignore_missing_paths;
4334 const char *status_path;
4335 size_t status_path_len;
4336 const char *status_codes;
4339 static const struct got_error *
4340 schedule_for_deletion(void *arg, unsigned char status,
4341 unsigned char staged_status, const char *relpath,
4342 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4343 struct got_object_id *commit_id, int dirfd, const char *de_name)
4345 struct schedule_deletion_args *a = arg;
4346 const struct got_error *err = NULL;
4347 struct got_fileindex_entry *ie = NULL;
4351 if (status == GOT_STATUS_NONEXISTENT) {
4352 if (a->ignore_missing_paths)
4354 return got_error_set_errno(ENOENT, relpath);
4357 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4359 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4361 staged_status = get_staged_status(ie);
4362 if (staged_status != GOT_STATUS_NO_CHANGE) {
4363 if (staged_status == GOT_STATUS_DELETE)
4365 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4368 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4370 return got_error_from_errno("asprintf");
4372 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4377 if (a->status_codes) {
4378 size_t ncodes = strlen(a->status_codes);
4380 for (i = 0; i < ncodes ; i++) {
4381 if (status == a->status_codes[i])
4385 /* Do not delete files in non-matching status. */
4389 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4390 a->status_codes[i] != GOT_STATUS_MISSING) {
4391 static char msg[64];
4392 snprintf(msg, sizeof(msg),
4393 "invalid status code '%c'", a->status_codes[i]);
4394 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4399 if (status != GOT_STATUS_NO_CHANGE) {
4400 if (status == GOT_STATUS_DELETE)
4402 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4403 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4406 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4407 err = got_error_set_errno(ENOENT, relpath);
4410 if (status != GOT_STATUS_MODIFY &&
4411 status != GOT_STATUS_MISSING) {
4412 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4417 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4421 if (unlinkat(dirfd, de_name, 0) == -1) {
4422 err = got_error_from_errno2("unlinkat",
4426 } else if (unlink(ondisk_path) == -1) {
4427 err = got_error_from_errno2("unlink", ondisk_path);
4431 root_len = strlen(a->worktree->root_path);
4435 err = got_path_dirname(&parent, ondisk_path);
4439 ondisk_path = parent;
4440 if (got_path_cmp(ondisk_path, a->status_path,
4441 strlen(ondisk_path), a->status_path_len) != 0 &&
4442 !got_path_is_child(ondisk_path, a->status_path,
4443 a->status_path_len))
4445 if (rmdir(ondisk_path) == -1) {
4446 if (errno != ENOTEMPTY)
4447 err = got_error_from_errno2("rmdir",
4451 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4452 strlen(ondisk_path), root_len) != 0);
4455 got_fileindex_entry_mark_deleted_from_disk(ie);
4460 if (status == GOT_STATUS_DELETE)
4462 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4463 staged_status, relpath);
4466 const struct got_error *
4467 got_worktree_schedule_delete(struct got_worktree *worktree,
4468 struct got_pathlist_head *paths, int delete_local_mods,
4469 const char *status_codes,
4470 got_worktree_delete_cb progress_cb, void *progress_arg,
4471 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4473 struct got_fileindex *fileindex = NULL;
4474 char *fileindex_path = NULL;
4475 const struct got_error *err = NULL, *sync_err, *unlockerr;
4476 struct got_pathlist_entry *pe;
4477 struct schedule_deletion_args sda;
4479 err = lock_worktree(worktree, LOCK_EX);
4483 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4487 sda.worktree = worktree;
4488 sda.fileindex = fileindex;
4489 sda.progress_cb = progress_cb;
4490 sda.progress_arg = progress_arg;
4492 sda.delete_local_mods = delete_local_mods;
4493 sda.keep_on_disk = keep_on_disk;
4494 sda.ignore_missing_paths = ignore_missing_paths;
4495 sda.status_codes = status_codes;
4497 TAILQ_FOREACH(pe, paths, entry) {
4498 char *ondisk_status_path;
4500 if (asprintf(&ondisk_status_path, "%s%s%s",
4501 got_worktree_get_root_path(worktree),
4502 pe->path[0] == '\0' ? "" : "/", pe->path) == -1) {
4503 err = got_error_from_errno("asprintf");
4506 sda.status_path = ondisk_status_path;
4507 sda.status_path_len = strlen(ondisk_status_path);
4508 err = worktree_status(worktree, pe->path, fileindex, repo,
4509 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4510 free(ondisk_status_path);
4514 sync_err = sync_fileindex(fileindex, fileindex_path);
4515 if (sync_err && err == NULL)
4518 free(fileindex_path);
4520 got_fileindex_free(fileindex);
4521 unlockerr = lock_worktree(worktree, LOCK_SH);
4522 if (unlockerr && err == NULL)
4527 static const struct got_error *
4528 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4530 const struct got_error *err = NULL;
4532 size_t linesize = 0, n;
4535 linelen = getline(&line, &linesize, infile);
4536 if (linelen == -1) {
4537 if (ferror(infile)) {
4538 err = got_error_from_errno("getline");
4544 n = fwrite(line, 1, linelen, outfile);
4546 err = got_ferror(outfile, GOT_ERR_IO);
4551 n = fwrite(line, 1, linelen, rejectfile);
4553 err = got_ferror(rejectfile, GOT_ERR_IO);
4560 static const struct got_error *
4561 skip_one_line(FILE *f)
4564 size_t linesize = 0;
4567 linelen = getline(&line, &linesize, f);
4568 if (linelen == -1) {
4570 return got_error_from_errno("getline");
4577 static const struct got_error *
4578 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4579 int start_old, int end_old, int start_new, int end_new,
4580 FILE *outfile, FILE *rejectfile)
4582 const struct got_error *err;
4584 /* Copy old file's lines leading up to patch. */
4585 while (!feof(f1) && *line_cur1 < start_old) {
4586 err = copy_one_line(f1, outfile, NULL);
4591 /* Skip new file's lines leading up to patch. */
4592 while (!feof(f2) && *line_cur2 < start_new) {
4594 err = copy_one_line(f2, NULL, rejectfile);
4596 err = skip_one_line(f2);
4601 /* Copy patched lines. */
4602 while (!feof(f2) && *line_cur2 <= end_new) {
4603 err = copy_one_line(f2, outfile, NULL);
4608 /* Skip over old file's replaced lines. */
4609 while (!feof(f1) && *line_cur1 <= end_old) {
4611 err = copy_one_line(f1, NULL, rejectfile);
4613 err = skip_one_line(f1);
4622 static const struct got_error *
4623 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4624 FILE *outfile, FILE *rejectfile)
4626 const struct got_error *err;
4629 /* Copy old file's lines until EOF. */
4631 err = copy_one_line(f1, outfile, NULL);
4638 /* Copy new file's lines until EOF. */
4640 err = copy_one_line(f2, NULL, rejectfile);
4650 static const struct got_error *
4651 apply_or_reject_change(int *choice, int *nchunks_used,
4652 struct diff_result *diff_result, int n,
4653 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4654 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4655 got_worktree_patch_cb patch_cb, void *patch_arg)
4657 const struct got_error *err = NULL;
4658 struct diff_chunk_context cc = {};
4659 int start_old, end_old, start_new, end_new;
4661 struct diff_output_unidiff_state *diff_state;
4662 struct diff_input_info diff_info;
4665 *choice = GOT_PATCH_CHOICE_NONE;
4667 /* Get changed line numbers without context lines for copy_change(). */
4668 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4669 start_old = cc.left.start;
4670 end_old = cc.left.end;
4671 start_new = cc.right.start;
4672 end_new = cc.right.end;
4674 /* Get the same change with context lines for display. */
4675 memset(&cc, 0, sizeof(cc));
4676 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4678 memset(&diff_info, 0, sizeof(diff_info));
4679 diff_info.left_path = relpath;
4680 diff_info.right_path = relpath;
4682 diff_state = diff_output_unidiff_state_alloc();
4683 if (diff_state == NULL)
4684 return got_error_set_errno(ENOMEM,
4685 "diff_output_unidiff_state_alloc");
4687 hunkfile = got_opentemp();
4688 if (hunkfile == NULL) {
4689 err = got_error_from_errno("got_opentemp");
4693 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4695 if (rc != DIFF_RC_OK) {
4696 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4700 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4701 err = got_ferror(hunkfile, GOT_ERR_IO);
4705 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4706 hunkfile, changeno, nchanges);
4711 case GOT_PATCH_CHOICE_YES:
4712 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4713 end_old, start_new, end_new, outfile, rejectfile);
4715 case GOT_PATCH_CHOICE_NO:
4716 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4717 end_old, start_new, end_new, rejectfile, outfile);
4719 case GOT_PATCH_CHOICE_QUIT:
4722 err = got_error(GOT_ERR_PATCH_CHOICE);
4726 diff_output_unidiff_state_free(diff_state);
4727 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4728 err = got_error_from_errno("fclose");
4732 struct revert_file_args {
4733 struct got_worktree *worktree;
4734 struct got_fileindex *fileindex;
4735 got_worktree_checkout_cb progress_cb;
4737 got_worktree_patch_cb patch_cb;
4739 struct got_repository *repo;
4740 int unlink_added_files;
4741 struct got_pathlist_head *added_files_to_unlink;
4744 static const struct got_error *
4745 create_patched_content(char **path_outfile, int reverse_patch,
4746 struct got_object_id *blob_id, const char *path2,
4747 int dirfd2, const char *de_name2,
4748 const char *relpath, struct got_repository *repo,
4749 got_worktree_patch_cb patch_cb, void *patch_arg)
4751 const struct got_error *err, *free_err;
4752 struct got_blob_object *blob = NULL;
4753 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4754 int fd = -1, fd2 = -1;
4755 char link_target[PATH_MAX];
4756 ssize_t link_len = 0;
4757 char *path1 = NULL, *id_str = NULL;
4759 struct got_diffreg_result *diffreg_result = NULL;
4760 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4761 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4763 *path_outfile = NULL;
4765 err = got_object_id_str(&id_str, blob_id);
4770 fd2 = openat(dirfd2, de_name2,
4771 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4773 if (!got_err_open_nofollow_on_symlink()) {
4774 err = got_error_from_errno2("openat", path2);
4777 link_len = readlinkat(dirfd2, de_name2,
4778 link_target, sizeof(link_target));
4779 if (link_len == -1) {
4780 return got_error_from_errno2("readlinkat",
4783 sb2.st_mode = S_IFLNK;
4784 sb2.st_size = link_len;
4787 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4789 if (!got_err_open_nofollow_on_symlink()) {
4790 err = got_error_from_errno2("open", path2);
4793 link_len = readlink(path2, link_target,
4794 sizeof(link_target));
4796 return got_error_from_errno2("readlink", path2);
4797 sb2.st_mode = S_IFLNK;
4798 sb2.st_size = link_len;
4802 if (fstat(fd2, &sb2) == -1) {
4803 err = got_error_from_errno2("fstat", path2);
4807 f2 = fdopen(fd2, "r");
4809 err = got_error_from_errno2("fdopen", path2);
4815 f2 = got_opentemp();
4817 err = got_error_from_errno2("got_opentemp", path2);
4820 n = fwrite(link_target, 1, link_len, f2);
4821 if (n != link_len) {
4822 err = got_ferror(f2, GOT_ERR_IO);
4825 if (fflush(f2) == EOF) {
4826 err = got_error_from_errno("fflush");
4832 fd = got_opentempfd();
4834 err = got_error_from_errno("got_opentempfd");
4838 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4842 err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4846 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4850 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4851 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4855 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4860 if (fseek(f1, 0L, SEEK_SET) == -1)
4861 return got_ferror(f1, GOT_ERR_IO);
4862 if (fseek(f2, 0L, SEEK_SET) == -1)
4863 return got_ferror(f2, GOT_ERR_IO);
4865 /* Count the number of actual changes in the diff result. */
4866 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4867 struct diff_chunk_context cc = {};
4868 diff_chunk_context_load_change(&cc, &nchunks_used,
4869 diffreg_result->result, n, 0);
4872 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4874 err = apply_or_reject_change(&choice, &nchunks_used,
4875 diffreg_result->result, n, relpath, f1, f2,
4876 &line_cur1, &line_cur2,
4877 reverse_patch ? NULL : outfile,
4878 reverse_patch ? outfile : NULL,
4879 ++i, nchanges, patch_cb, patch_arg);
4882 if (choice == GOT_PATCH_CHOICE_YES)
4884 else if (choice == GOT_PATCH_CHOICE_QUIT)
4888 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4889 reverse_patch ? NULL : outfile,
4890 reverse_patch ? outfile : NULL);
4894 if (!S_ISLNK(sb2.st_mode)) {
4897 mode = apply_umask(sb2.st_mode);
4898 if (fchmod(fileno(outfile), mode) == -1) {
4899 err = got_error_from_errno2("fchmod", path2);
4906 if (fd != -1 && close(fd) == -1 && err == NULL)
4907 err = got_error_from_errno("close");
4909 got_object_blob_close(blob);
4910 free_err = got_diffreg_result_free(diffreg_result);
4913 if (f1 && fclose(f1) == EOF && err == NULL)
4914 err = got_error_from_errno2("fclose", path1);
4915 if (f2 && fclose(f2) == EOF && err == NULL)
4916 err = got_error_from_errno2("fclose", path2);
4917 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4918 err = got_error_from_errno2("close", path2);
4919 if (outfile && fclose(outfile) == EOF && err == NULL)
4920 err = got_error_from_errno2("fclose", *path_outfile);
4921 if (path1 && unlink(path1) == -1 && err == NULL)
4922 err = got_error_from_errno2("unlink", path1);
4923 if (err || !have_content) {
4924 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4925 err = got_error_from_errno2("unlink", *path_outfile);
4926 free(*path_outfile);
4927 *path_outfile = NULL;
4933 static const struct got_error *
4934 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4935 const char *relpath, struct got_object_id *blob_id,
4936 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4937 int dirfd, const char *de_name)
4939 struct revert_file_args *a = arg;
4940 const struct got_error *err = NULL;
4941 char *parent_path = NULL;
4942 struct got_fileindex_entry *ie;
4943 struct got_commit_object *base_commit = NULL;
4944 struct got_tree_object *tree = NULL;
4945 struct got_object_id *tree_id = NULL;
4946 const struct got_tree_entry *te = NULL;
4947 char *tree_path = NULL, *te_name;
4948 char *ondisk_path = NULL, *path_content = NULL;
4949 struct got_blob_object *blob = NULL;
4952 /* Reverting a staged deletion is a no-op. */
4953 if (status == GOT_STATUS_DELETE &&
4954 staged_status != GOT_STATUS_NO_CHANGE)
4957 if (status == GOT_STATUS_UNVERSIONED)
4958 return (*a->progress_cb)(a->progress_arg,
4959 GOT_STATUS_UNVERSIONED, relpath);
4961 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4963 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4965 /* Construct in-repository path of tree which contains this blob. */
4966 err = got_path_dirname(&parent_path, ie->path);
4968 if (err->code != GOT_ERR_BAD_PATH)
4970 parent_path = strdup("/");
4971 if (parent_path == NULL) {
4972 err = got_error_from_errno("strdup");
4976 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4977 tree_path = strdup(parent_path);
4978 if (tree_path == NULL) {
4979 err = got_error_from_errno("strdup");
4983 if (got_path_is_root_dir(parent_path)) {
4984 tree_path = strdup(a->worktree->path_prefix);
4985 if (tree_path == NULL) {
4986 err = got_error_from_errno("strdup");
4990 if (asprintf(&tree_path, "%s/%s",
4991 a->worktree->path_prefix, parent_path) == -1) {
4992 err = got_error_from_errno("asprintf");
4998 err = got_object_open_as_commit(&base_commit, a->repo,
4999 a->worktree->base_commit_id);
5003 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
5005 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
5006 (status == GOT_STATUS_ADD ||
5007 staged_status == GOT_STATUS_ADD)))
5010 err = got_object_open_as_tree(&tree, a->repo, tree_id);
5014 err = got_path_basename(&te_name, ie->path);
5018 te = got_object_tree_find_entry(tree, te_name);
5020 if (te == NULL && status != GOT_STATUS_ADD &&
5021 staged_status != GOT_STATUS_ADD) {
5022 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
5028 case GOT_STATUS_ADD:
5030 int choice = GOT_PATCH_CHOICE_NONE;
5031 err = (*a->patch_cb)(&choice, a->patch_arg,
5032 status, ie->path, NULL, 1, 1);
5035 if (choice != GOT_PATCH_CHOICE_YES)
5038 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
5042 got_fileindex_entry_remove(a->fileindex, ie);
5043 if (a->unlink_added_files) {
5044 int do_unlink = a->added_files_to_unlink ? 0 : 1;
5046 if (a->added_files_to_unlink) {
5047 struct got_pathlist_entry *pe;
5049 TAILQ_FOREACH(pe, a->added_files_to_unlink,
5051 if (got_path_cmp(pe->path, relpath,
5052 pe->path_len, strlen(relpath)))
5060 if (asprintf(&ondisk_path, "%s/%s",
5061 got_worktree_get_root_path(a->worktree),
5063 err = got_error_from_errno("asprintf");
5066 if (unlink(ondisk_path) == -1) {
5067 err = got_error_from_errno2("unlink",
5074 case GOT_STATUS_DELETE:
5076 int choice = GOT_PATCH_CHOICE_NONE;
5077 err = (*a->patch_cb)(&choice, a->patch_arg,
5078 status, ie->path, NULL, 1, 1);
5081 if (choice != GOT_PATCH_CHOICE_YES)
5085 case GOT_STATUS_MODIFY:
5086 case GOT_STATUS_MODE_CHANGE:
5087 case GOT_STATUS_CONFLICT:
5088 case GOT_STATUS_MISSING: {
5089 struct got_object_id id;
5090 if (staged_status == GOT_STATUS_ADD ||
5091 staged_status == GOT_STATUS_MODIFY)
5092 got_fileindex_entry_get_staged_blob_id(&id, ie);
5094 got_fileindex_entry_get_blob_id(&id, ie);
5095 fd = got_opentempfd();
5097 err = got_error_from_errno("got_opentempfd");
5101 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
5105 if (asprintf(&ondisk_path, "%s/%s",
5106 got_worktree_get_root_path(a->worktree), relpath) == -1) {
5107 err = got_error_from_errno("asprintf");
5111 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
5112 status == GOT_STATUS_CONFLICT)) {
5113 int is_bad_symlink = 0;
5114 err = create_patched_content(&path_content, 1, &id,
5115 ondisk_path, dirfd, de_name, ie->path, a->repo,
5116 a->patch_cb, a->patch_arg);
5117 if (err || path_content == NULL)
5119 if (te && S_ISLNK(te->mode)) {
5120 if (unlink(path_content) == -1) {
5121 err = got_error_from_errno2("unlink",
5125 err = install_symlink(&is_bad_symlink,
5126 a->worktree, ondisk_path, ie->path,
5127 blob, 0, 1, 0, 0, a->repo,
5128 a->progress_cb, a->progress_arg);
5130 if (rename(path_content, ondisk_path) == -1) {
5131 err = got_error_from_errno3("rename",
5132 path_content, ondisk_path);
5137 int is_bad_symlink = 0;
5138 if (te && S_ISLNK(te->mode)) {
5139 err = install_symlink(&is_bad_symlink,
5140 a->worktree, ondisk_path, ie->path,
5141 blob, 0, 1, 0, 0, a->repo,
5142 a->progress_cb, a->progress_arg);
5144 err = install_blob(a->worktree, ondisk_path,
5146 te ? te->mode : GOT_DEFAULT_FILE_MODE,
5147 got_fileindex_perms_to_st(ie), blob,
5148 0, 1, 0, 0, a->repo,
5149 a->progress_cb, a->progress_arg);
5153 if (status == GOT_STATUS_DELETE ||
5154 status == GOT_STATUS_MODE_CHANGE) {
5155 err = got_fileindex_entry_update(ie,
5156 a->worktree->root_fd, relpath,
5158 a->worktree->base_commit_id->sha1, 1);
5162 if (is_bad_symlink) {
5163 got_fileindex_entry_filetype_set(ie,
5164 GOT_FILEIDX_MODE_BAD_SYMLINK);
5177 if (fd != -1 && close(fd) == -1 && err == NULL)
5178 err = got_error_from_errno("close");
5180 got_object_blob_close(blob);
5182 got_object_tree_close(tree);
5185 got_object_commit_close(base_commit);
5189 const struct got_error *
5190 got_worktree_revert(struct got_worktree *worktree,
5191 struct got_pathlist_head *paths,
5192 got_worktree_checkout_cb progress_cb, void *progress_arg,
5193 got_worktree_patch_cb patch_cb, void *patch_arg,
5194 struct got_repository *repo)
5196 struct got_fileindex *fileindex = NULL;
5197 char *fileindex_path = NULL;
5198 const struct got_error *err = NULL, *unlockerr = NULL;
5199 const struct got_error *sync_err = NULL;
5200 struct got_pathlist_entry *pe;
5201 struct revert_file_args rfa;
5203 err = lock_worktree(worktree, LOCK_EX);
5207 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5211 rfa.worktree = worktree;
5212 rfa.fileindex = fileindex;
5213 rfa.progress_cb = progress_cb;
5214 rfa.progress_arg = progress_arg;
5215 rfa.patch_cb = patch_cb;
5216 rfa.patch_arg = patch_arg;
5218 rfa.unlink_added_files = 0;
5219 TAILQ_FOREACH(pe, paths, entry) {
5220 err = worktree_status(worktree, pe->path, fileindex, repo,
5221 revert_file, &rfa, NULL, NULL, 1, 0);
5225 sync_err = sync_fileindex(fileindex, fileindex_path);
5226 if (sync_err && err == NULL)
5229 free(fileindex_path);
5231 got_fileindex_free(fileindex);
5232 unlockerr = lock_worktree(worktree, LOCK_SH);
5233 if (unlockerr && err == NULL)
5239 free_commitable(struct got_commitable *ct)
5242 free(ct->in_repo_path);
5243 free(ct->ondisk_path);
5245 free(ct->base_blob_id);
5246 free(ct->staged_blob_id);
5247 free(ct->base_commit_id);
5251 struct collect_commitables_arg {
5252 struct got_pathlist_head *commitable_paths;
5253 struct got_repository *repo;
5254 struct got_worktree *worktree;
5255 struct got_fileindex *fileindex;
5256 int have_staged_files;
5257 int allow_bad_symlinks;
5258 int diff_header_shown;
5259 int commit_conflicts;
5266 * Create a file which contains the target path of a symlink so we can feed
5267 * it as content to the diff engine.
5269 static const struct got_error *
5270 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5271 const char *abspath)
5273 const struct got_error *err = NULL;
5274 char target_path[PATH_MAX];
5275 ssize_t target_len, outlen;
5280 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5281 if (target_len == -1)
5282 return got_error_from_errno2("readlinkat", abspath);
5284 target_len = readlink(abspath, target_path, PATH_MAX);
5285 if (target_len == -1)
5286 return got_error_from_errno2("readlink", abspath);
5289 *fd = got_opentempfd();
5291 return got_error_from_errno("got_opentempfd");
5293 outlen = write(*fd, target_path, target_len);
5295 err = got_error_from_errno("got_opentempfd");
5299 if (lseek(*fd, 0, SEEK_SET) == -1) {
5300 err = got_error_from_errno2("lseek", abspath);
5311 static const struct got_error *
5312 append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5313 FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5314 int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5316 const struct got_error *err = NULL;
5317 struct got_blob_object *blob1 = NULL;
5318 int fd = -1, fd1 = -1, fd2 = -1;
5319 FILE *ondisk_file = NULL;
5320 char *label1 = NULL;
5324 char *id_str = NULL;
5326 memset(&sb, 0, sizeof(sb));
5329 if (ct->staged_status != GOT_STATUS_MODIFY &&
5330 ct->staged_status != GOT_STATUS_ADD &&
5331 ct->staged_status != GOT_STATUS_DELETE)
5334 if (ct->status != GOT_STATUS_MODIFY &&
5335 ct->status != GOT_STATUS_ADD &&
5336 ct->status != GOT_STATUS_DELETE &&
5337 ct->status != GOT_STATUS_CONFLICT)
5341 err = got_opentemp_truncate(f1);
5343 return got_error_from_errno("got_opentemp_truncate");
5344 err = got_opentemp_truncate(f2);
5346 return got_error_from_errno("got_opentemp_truncate");
5348 if (!*diff_header_shown) {
5349 err = got_object_id_str(&id_str, worktree->base_commit_id);
5352 fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5353 got_worktree_get_root_path(worktree));
5354 fprintf(diff_outfile, "commit - %s\n", id_str);
5355 fprintf(diff_outfile, "path + %s%s\n",
5356 got_worktree_get_root_path(worktree),
5357 diff_staged ? " (staged changes)" : "");
5358 *diff_header_shown = 1;
5362 const char *label1 = NULL, *label2 = NULL;
5363 switch (ct->staged_status) {
5364 case GOT_STATUS_MODIFY:
5368 case GOT_STATUS_ADD:
5371 case GOT_STATUS_DELETE:
5375 return got_error(GOT_ERR_FILE_STATUS);
5377 fd1 = got_opentempfd();
5379 err = got_error_from_errno("got_opentempfd");
5382 fd2 = got_opentempfd();
5384 err = got_error_from_errno("got_opentempfd");
5387 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5388 fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5389 label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5390 NULL, repo, diff_outfile);
5394 fd1 = got_opentempfd();
5396 err = got_error_from_errno("got_opentempfd");
5400 if (ct->status != GOT_STATUS_ADD) {
5401 err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5407 if (ct->status != GOT_STATUS_DELETE) {
5409 fd = openat(dirfd, de_name,
5410 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5412 if (!got_err_open_nofollow_on_symlink()) {
5413 err = got_error_from_errno2("openat",
5417 err = get_symlink_target_file(&fd, dirfd,
5418 de_name, ct->ondisk_path);
5423 fd = open(ct->ondisk_path,
5424 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5426 if (!got_err_open_nofollow_on_symlink()) {
5427 err = got_error_from_errno2("open",
5431 err = get_symlink_target_file(&fd, dirfd,
5432 de_name, ct->ondisk_path);
5437 if (fstatat(fd, ct->ondisk_path, &sb,
5438 AT_SYMLINK_NOFOLLOW) == -1) {
5439 err = got_error_from_errno2("fstatat", ct->ondisk_path);
5442 ondisk_file = fdopen(fd, "r");
5443 if (ondisk_file == NULL) {
5444 err = got_error_from_errno2("fdopen", ct->ondisk_path);
5452 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5458 err = got_diff_blob_file(blob1, f1, size1, label1,
5459 ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5460 GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5462 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5463 err = got_error_from_errno("close");
5464 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5465 err = got_error_from_errno("close");
5467 got_object_blob_close(blob1);
5468 if (fd != -1 && close(fd) == -1 && err == NULL)
5469 err = got_error_from_errno("close");
5470 if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5471 err = got_error_from_errno("fclose");
5475 static const struct got_error *
5476 collect_commitables(void *arg, unsigned char status,
5477 unsigned char staged_status, const char *relpath,
5478 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5479 struct got_object_id *commit_id, int dirfd, const char *de_name)
5481 struct collect_commitables_arg *a = arg;
5482 const struct got_error *err = NULL;
5483 struct got_commitable *ct = NULL;
5484 struct got_pathlist_entry *new = NULL;
5485 char *parent_path = NULL, *path = NULL;
5488 if (a->have_staged_files) {
5489 if (staged_status != GOT_STATUS_MODIFY &&
5490 staged_status != GOT_STATUS_ADD &&
5491 staged_status != GOT_STATUS_DELETE)
5494 if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5495 printf("C %s\n", relpath);
5496 return got_error(GOT_ERR_COMMIT_CONFLICT);
5499 if (status != GOT_STATUS_MODIFY &&
5500 status != GOT_STATUS_MODE_CHANGE &&
5501 status != GOT_STATUS_ADD &&
5502 status != GOT_STATUS_DELETE &&
5503 status != GOT_STATUS_CONFLICT)
5507 if (asprintf(&path, "/%s", relpath) == -1) {
5508 err = got_error_from_errno("asprintf");
5511 if (strcmp(path, "/") == 0) {
5512 parent_path = strdup("");
5513 if (parent_path == NULL)
5514 return got_error_from_errno("strdup");
5516 err = got_path_dirname(&parent_path, path);
5521 ct = calloc(1, sizeof(*ct));
5523 err = got_error_from_errno("calloc");
5527 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5529 err = got_error_from_errno("asprintf");
5533 if (staged_status == GOT_STATUS_ADD ||
5534 staged_status == GOT_STATUS_MODIFY) {
5535 struct got_fileindex_entry *ie;
5536 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5537 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5538 case GOT_FILEIDX_MODE_REGULAR_FILE:
5539 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5542 case GOT_FILEIDX_MODE_SYMLINK:
5546 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5549 ct->mode |= got_fileindex_entry_perms_get(ie);
5550 } else if (status != GOT_STATUS_DELETE &&
5551 staged_status != GOT_STATUS_DELETE) {
5553 if (fstatat(dirfd, de_name, &sb,
5554 AT_SYMLINK_NOFOLLOW) == -1) {
5555 err = got_error_from_errno2("fstatat",
5559 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5560 err = got_error_from_errno2("lstat", ct->ondisk_path);
5563 ct->mode = sb.st_mode;
5566 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5567 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5569 err = got_error_from_errno("asprintf");
5573 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5574 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5576 char target_path[PATH_MAX];
5578 target_len = readlink(ct->ondisk_path, target_path,
5579 sizeof(target_path));
5580 if (target_len == -1) {
5581 err = got_error_from_errno2("readlink",
5585 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5586 target_len, ct->ondisk_path, a->worktree->root_path);
5589 if (is_bad_symlink) {
5590 err = got_error_path(ct->ondisk_path,
5591 GOT_ERR_BAD_SYMLINK);
5597 ct->status = status;
5598 ct->staged_status = staged_status;
5599 ct->blob_id = NULL; /* will be filled in when blob gets created */
5600 if (ct->status != GOT_STATUS_ADD &&
5601 ct->staged_status != GOT_STATUS_ADD) {
5602 ct->base_blob_id = got_object_id_dup(blob_id);
5603 if (ct->base_blob_id == NULL) {
5604 err = got_error_from_errno("got_object_id_dup");
5607 ct->base_commit_id = got_object_id_dup(commit_id);
5608 if (ct->base_commit_id == NULL) {
5609 err = got_error_from_errno("got_object_id_dup");
5613 if (ct->staged_status == GOT_STATUS_ADD ||
5614 ct->staged_status == GOT_STATUS_MODIFY) {
5615 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5616 if (ct->staged_blob_id == NULL) {
5617 err = got_error_from_errno("got_object_id_dup");
5621 ct->path = strdup(path);
5622 if (ct->path == NULL) {
5623 err = got_error_from_errno("strdup");
5626 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5630 if (a->diff_outfile && ct && new != NULL) {
5631 err = append_ct_diff(ct, &a->diff_header_shown,
5632 a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5633 a->have_staged_files, a->repo, a->worktree);
5638 if (ct && (err || new == NULL))
5639 free_commitable(ct);
5645 static const struct got_error *write_tree(struct got_object_id **, int *,
5646 struct got_tree_object *, const char *, struct got_pathlist_head *,
5647 got_worktree_status_cb status_cb, void *status_arg,
5648 struct got_repository *);
5650 static const struct got_error *
5651 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5652 struct got_tree_entry *te, const char *parent_path,
5653 struct got_pathlist_head *commitable_paths,
5654 got_worktree_status_cb status_cb, void *status_arg,
5655 struct got_repository *repo)
5657 const struct got_error *err = NULL;
5658 struct got_tree_object *subtree;
5661 if (asprintf(&subpath, "%s%s%s", parent_path,
5662 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5663 return got_error_from_errno("asprintf");
5665 err = got_object_open_as_tree(&subtree, repo, &te->id);
5669 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5670 commitable_paths, status_cb, status_arg, repo);
5671 got_object_tree_close(subtree);
5676 static const struct got_error *
5677 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5679 const struct got_error *err = NULL;
5680 char *ct_parent_path = NULL;
5684 if (strchr(ct->in_repo_path, '/') == NULL) {
5685 *match = got_path_is_root_dir(path);
5689 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5692 *match = (strcmp(path, ct_parent_path) == 0);
5693 free(ct_parent_path);
5698 get_ct_file_mode(struct got_commitable *ct)
5700 if (S_ISLNK(ct->mode))
5703 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5706 static const struct got_error *
5707 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5708 struct got_tree_entry *te, struct got_commitable *ct)
5710 const struct got_error *err = NULL;
5714 err = got_object_tree_entry_dup(new_te, te);
5718 (*new_te)->mode = get_ct_file_mode(ct);
5720 if (ct->staged_status == GOT_STATUS_MODIFY)
5721 memcpy(&(*new_te)->id, ct->staged_blob_id,
5722 sizeof((*new_te)->id));
5724 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5726 if (err && *new_te) {
5733 static const struct got_error *
5734 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5735 struct got_commitable *ct)
5737 const struct got_error *err = NULL;
5738 char *ct_name = NULL;
5742 *new_te = calloc(1, sizeof(**new_te));
5743 if (*new_te == NULL)
5744 return got_error_from_errno("calloc");
5746 err = got_path_basename(&ct_name, ct->path);
5749 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5750 sizeof((*new_te)->name)) {
5751 err = got_error(GOT_ERR_NO_SPACE);
5755 (*new_te)->mode = get_ct_file_mode(ct);
5757 if (ct->staged_status == GOT_STATUS_ADD)
5758 memcpy(&(*new_te)->id, ct->staged_blob_id,
5759 sizeof((*new_te)->id));
5761 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5764 if (err && *new_te) {
5771 static const struct got_error *
5772 insert_tree_entry(struct got_tree_entry *new_te,
5773 struct got_pathlist_head *paths)
5775 const struct got_error *err = NULL;
5776 struct got_pathlist_entry *new_pe;
5778 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5782 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5786 static const struct got_error *
5787 report_ct_status(struct got_commitable *ct,
5788 got_worktree_status_cb status_cb, void *status_arg)
5790 const char *ct_path = ct->path;
5791 unsigned char status;
5793 if (status_cb == NULL) /* no commit progress output desired */
5796 while (ct_path[0] == '/')
5799 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5800 status = ct->staged_status;
5802 status = ct->status;
5804 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5805 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5808 static const struct got_error *
5809 match_modified_subtree(int *modified, struct got_tree_entry *te,
5810 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5812 const struct got_error *err = NULL;
5813 struct got_pathlist_entry *pe;
5818 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5819 got_path_is_root_dir(base_tree_path) ? "" : "/",
5821 return got_error_from_errno("asprintf");
5823 TAILQ_FOREACH(pe, commitable_paths, entry) {
5824 struct got_commitable *ct = pe->data;
5825 *modified = got_path_is_child(ct->in_repo_path, te_path,
5835 static const struct got_error *
5836 match_deleted_or_modified_ct(struct got_commitable **ctp,
5837 struct got_tree_entry *te, const char *base_tree_path,
5838 struct got_pathlist_head *commitable_paths)
5840 const struct got_error *err = NULL;
5841 struct got_pathlist_entry *pe;
5845 TAILQ_FOREACH(pe, commitable_paths, entry) {
5846 struct got_commitable *ct = pe->data;
5847 char *ct_name = NULL;
5850 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5851 if (ct->status != GOT_STATUS_MODIFY &&
5852 ct->status != GOT_STATUS_MODE_CHANGE &&
5853 ct->status != GOT_STATUS_DELETE &&
5854 ct->status != GOT_STATUS_CONFLICT)
5857 if (ct->staged_status != GOT_STATUS_MODIFY &&
5858 ct->staged_status != GOT_STATUS_DELETE)
5862 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5865 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5871 err = got_path_basename(&ct_name, pe->path);
5875 if (strcmp(te->name, ct_name) != 0) {
5888 static const struct got_error *
5889 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5890 const char *child_path, const char *path_base_tree,
5891 struct got_pathlist_head *commitable_paths,
5892 got_worktree_status_cb status_cb, void *status_arg,
5893 struct got_repository *repo)
5895 const struct got_error *err = NULL;
5896 struct got_tree_entry *new_te;
5898 struct got_object_id *id = NULL;
5903 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5904 got_path_is_root_dir(path_base_tree) ? "" : "/",
5906 return got_error_from_errno("asprintf");
5908 new_te = calloc(1, sizeof(*new_te));
5910 return got_error_from_errno("calloc");
5911 new_te->mode = S_IFDIR;
5913 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5914 sizeof(new_te->name)) {
5915 err = got_error(GOT_ERR_NO_SPACE);
5918 err = write_tree(&id, &nentries, NULL, subtree_path,
5919 commitable_paths, status_cb, status_arg, repo);
5924 memcpy(&new_te->id, id, sizeof(new_te->id));
5933 static const struct got_error *
5934 write_tree(struct got_object_id **new_tree_id, int *nentries,
5935 struct got_tree_object *base_tree, const char *path_base_tree,
5936 struct got_pathlist_head *commitable_paths,
5937 got_worktree_status_cb status_cb, void *status_arg,
5938 struct got_repository *repo)
5940 const struct got_error *err = NULL;
5941 struct got_pathlist_head paths;
5942 struct got_tree_entry *te, *new_te = NULL;
5943 struct got_pathlist_entry *pe;
5948 /* Insert, and recurse into, newly added entries first. */
5949 TAILQ_FOREACH(pe, commitable_paths, entry) {
5950 struct got_commitable *ct = pe->data;
5951 char *child_path = NULL, *slash;
5953 if ((ct->status != GOT_STATUS_ADD &&
5954 ct->staged_status != GOT_STATUS_ADD) ||
5955 (ct->flags & GOT_COMMITABLE_ADDED))
5958 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5959 strlen(path_base_tree)))
5962 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5967 slash = strchr(child_path, '/');
5968 if (slash == NULL) {
5969 err = alloc_added_blob_tree_entry(&new_te, ct);
5972 err = report_ct_status(ct, status_cb, status_arg);
5975 ct->flags |= GOT_COMMITABLE_ADDED;
5976 err = insert_tree_entry(new_te, &paths);
5981 *slash = '\0'; /* trim trailing path components */
5982 if (base_tree == NULL ||
5983 got_object_tree_find_entry(base_tree, child_path)
5985 err = make_subtree_for_added_blob(&new_te,
5986 child_path, path_base_tree,
5987 commitable_paths, status_cb, status_arg,
5991 err = insert_tree_entry(new_te, &paths);
6000 int i, nbase_entries;
6001 /* Handle modified and deleted entries. */
6002 nbase_entries = got_object_tree_get_nentries(base_tree);
6003 for (i = 0; i < nbase_entries; i++) {
6004 struct got_commitable *ct = NULL;
6006 te = got_object_tree_get_entry(base_tree, i);
6007 if (got_object_tree_entry_is_submodule(te)) {
6008 /* Entry is a submodule; just copy it. */
6009 err = got_object_tree_entry_dup(&new_te, te);
6012 err = insert_tree_entry(new_te, &paths);
6019 if (S_ISDIR(te->mode)) {
6021 err = got_object_tree_entry_dup(&new_te, te);
6024 err = match_modified_subtree(&modified, te,
6025 path_base_tree, commitable_paths);
6028 /* Avoid recursion into unmodified subtrees. */
6030 struct got_object_id *new_id;
6032 err = write_subtree(&new_id,
6034 path_base_tree, commitable_paths,
6035 status_cb, status_arg, repo);
6038 if (nsubentries == 0) {
6039 /* All entries were deleted. */
6043 memcpy(&new_te->id, new_id,
6044 sizeof(new_te->id));
6047 err = insert_tree_entry(new_te, &paths);
6054 err = match_deleted_or_modified_ct(&ct, te,
6055 path_base_tree, commitable_paths);
6059 /* NB: Deleted entries get dropped here. */
6060 if (ct->status == GOT_STATUS_MODIFY ||
6061 ct->status == GOT_STATUS_MODE_CHANGE ||
6062 ct->status == GOT_STATUS_CONFLICT ||
6063 ct->staged_status == GOT_STATUS_MODIFY) {
6064 err = alloc_modified_blob_tree_entry(
6068 err = insert_tree_entry(new_te, &paths);
6073 err = report_ct_status(ct, status_cb,
6078 /* Entry is unchanged; just copy it. */
6079 err = got_object_tree_entry_dup(&new_te, te);
6082 err = insert_tree_entry(new_te, &paths);
6090 /* Write new list of entries; deleted entries have been dropped. */
6091 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
6093 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
6097 static const struct got_error *
6098 update_fileindex_after_commit(struct got_worktree *worktree,
6099 struct got_pathlist_head *commitable_paths,
6100 struct got_object_id *new_base_commit_id,
6101 struct got_fileindex *fileindex, int have_staged_files)
6103 const struct got_error *err = NULL;
6104 struct got_pathlist_entry *pe;
6105 char *relpath = NULL;
6107 TAILQ_FOREACH(pe, commitable_paths, entry) {
6108 struct got_fileindex_entry *ie;
6109 struct got_commitable *ct = pe->data;
6111 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6113 err = got_path_skip_common_ancestor(&relpath,
6114 worktree->root_path, ct->ondisk_path);
6119 if (ct->status == GOT_STATUS_DELETE ||
6120 ct->staged_status == GOT_STATUS_DELETE) {
6121 got_fileindex_entry_remove(fileindex, ie);
6122 } else if (ct->staged_status == GOT_STATUS_ADD ||
6123 ct->staged_status == GOT_STATUS_MODIFY) {
6124 got_fileindex_entry_stage_set(ie,
6125 GOT_FILEIDX_STAGE_NONE);
6126 got_fileindex_entry_staged_filetype_set(ie, 0);
6128 err = got_fileindex_entry_update(ie,
6129 worktree->root_fd, relpath,
6130 ct->staged_blob_id->sha1,
6131 new_base_commit_id->sha1,
6132 !have_staged_files);
6134 err = got_fileindex_entry_update(ie,
6135 worktree->root_fd, relpath,
6137 new_base_commit_id->sha1,
6138 !have_staged_files);
6140 err = got_fileindex_entry_alloc(&ie, pe->path);
6143 err = got_fileindex_entry_update(ie,
6144 worktree->root_fd, relpath, ct->blob_id->sha1,
6145 new_base_commit_id->sha1, 1);
6147 got_fileindex_entry_free(ie);
6150 err = got_fileindex_entry_add(fileindex, ie);
6152 got_fileindex_entry_free(ie);
6165 static const struct got_error *
6166 check_out_of_date(const char *in_repo_path, unsigned char status,
6167 unsigned char staged_status, struct got_object_id *base_blob_id,
6168 struct got_object_id *base_commit_id,
6169 struct got_object_id *head_commit_id, struct got_repository *repo,
6172 const struct got_error *err = NULL;
6173 struct got_commit_object *commit = NULL;
6174 struct got_object_id *id = NULL;
6176 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
6177 /* Trivial case: base commit == head commit */
6178 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
6181 * Ensure file content which local changes were based
6182 * on matches file content in the branch head.
6184 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6187 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6189 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6190 err = got_error(ood_errcode);
6192 } else if (got_object_id_cmp(id, base_blob_id) != 0)
6193 err = got_error(ood_errcode);
6195 /* Require that added files don't exist in the branch head. */
6196 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6199 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6200 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
6202 err = id ? got_error(ood_errcode) : NULL;
6207 got_object_commit_close(commit);
6211 static const struct got_error *
6212 commit_worktree(struct got_object_id **new_commit_id,
6213 struct got_pathlist_head *commitable_paths,
6214 struct got_object_id *head_commit_id,
6215 struct got_object_id *parent_id2,
6216 struct got_worktree *worktree,
6217 const char *author, const char *committer, char *diff_path,
6218 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6219 got_worktree_status_cb status_cb, void *status_arg,
6220 struct got_repository *repo)
6222 const struct got_error *err = NULL, *unlockerr = NULL;
6223 struct got_pathlist_entry *pe;
6224 const char *head_ref_name = NULL;
6225 struct got_commit_object *head_commit = NULL;
6226 struct got_reference *head_ref2 = NULL;
6227 struct got_object_id *head_commit_id2 = NULL;
6228 struct got_tree_object *head_tree = NULL;
6229 struct got_object_id *new_tree_id = NULL;
6230 int nentries, nparents = 0;
6231 struct got_object_id_queue parent_ids;
6232 struct got_object_qid *pid = NULL;
6233 char *logmsg = NULL;
6236 *new_commit_id = NULL;
6238 STAILQ_INIT(&parent_ids);
6240 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6244 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6248 if (commit_msg_cb != NULL) {
6249 err = commit_msg_cb(commitable_paths, diff_path,
6250 &logmsg, commit_arg);
6255 if (logmsg == NULL || strlen(logmsg) == 0) {
6256 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6260 /* Create blobs from added and modified files and record their IDs. */
6261 TAILQ_FOREACH(pe, commitable_paths, entry) {
6262 struct got_commitable *ct = pe->data;
6265 /* Blobs for staged files already exist. */
6266 if (ct->staged_status == GOT_STATUS_ADD ||
6267 ct->staged_status == GOT_STATUS_MODIFY)
6270 if (ct->status != GOT_STATUS_ADD &&
6271 ct->status != GOT_STATUS_MODIFY &&
6272 ct->status != GOT_STATUS_MODE_CHANGE &&
6273 ct->status != GOT_STATUS_CONFLICT)
6276 if (asprintf(&ondisk_path, "%s/%s",
6277 worktree->root_path, pe->path) == -1) {
6278 err = got_error_from_errno("asprintf");
6281 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6287 /* Recursively write new tree objects. */
6288 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6289 commitable_paths, status_cb, status_arg, repo);
6293 err = got_object_qid_alloc(&pid, head_commit_id);
6296 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6299 err = got_object_qid_alloc(&pid, parent_id2);
6302 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6305 timestamp = time(NULL);
6306 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6307 nparents, author, timestamp, committer, timestamp, logmsg, repo);
6313 /* Check if a concurrent commit to our branch has occurred. */
6314 head_ref_name = got_worktree_get_head_ref_name(worktree);
6315 if (head_ref_name == NULL) {
6316 err = got_error_from_errno("got_worktree_get_head_ref_name");
6319 /* Lock the reference here to prevent concurrent modification. */
6320 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6323 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6326 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6327 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6330 /* Update branch head in repository. */
6331 err = got_ref_change_ref(head_ref2, *new_commit_id);
6334 err = got_ref_write(head_ref2, repo);
6338 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6342 err = ref_base_commit(worktree, repo);
6346 got_object_id_queue_free(&parent_ids);
6348 got_object_tree_close(head_tree);
6350 got_object_commit_close(head_commit);
6351 free(head_commit_id2);
6353 unlockerr = got_ref_unlock(head_ref2);
6354 if (unlockerr && err == NULL)
6356 got_ref_close(head_ref2);
6361 static const struct got_error *
6362 check_path_is_commitable(const char *path,
6363 struct got_pathlist_head *commitable_paths)
6365 struct got_pathlist_entry *cpe = NULL;
6366 size_t path_len = strlen(path);
6368 TAILQ_FOREACH(cpe, commitable_paths, entry) {
6369 struct got_commitable *ct = cpe->data;
6370 const char *ct_path = ct->path;
6372 while (ct_path[0] == '/')
6375 if (strcmp(path, ct_path) == 0 ||
6376 got_path_is_child(ct_path, path, path_len))
6381 return got_error_path(path, GOT_ERR_BAD_PATH);
6386 static const struct got_error *
6387 check_staged_file(void *arg, struct got_fileindex_entry *ie)
6389 int *have_staged_files = arg;
6391 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6392 *have_staged_files = 1;
6393 return got_error(GOT_ERR_CANCELLED);
6399 static const struct got_error *
6400 check_non_staged_files(struct got_fileindex *fileindex,
6401 struct got_pathlist_head *paths)
6403 struct got_pathlist_entry *pe;
6404 struct got_fileindex_entry *ie;
6406 TAILQ_FOREACH(pe, paths, entry) {
6407 if (pe->path[0] == '\0')
6409 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6411 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6412 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6413 return got_error_path(pe->path,
6414 GOT_ERR_FILE_NOT_STAGED);
6420 const struct got_error *
6421 got_worktree_commit(struct got_object_id **new_commit_id,
6422 struct got_worktree *worktree, struct got_pathlist_head *paths,
6423 const char *author, const char *committer, int allow_bad_symlinks,
6424 int show_diff, int commit_conflicts,
6425 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6426 got_worktree_status_cb status_cb, void *status_arg,
6427 struct got_repository *repo)
6429 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6430 struct got_fileindex *fileindex = NULL;
6431 char *fileindex_path = NULL;
6432 struct got_pathlist_head commitable_paths;
6433 struct collect_commitables_arg cc_arg;
6434 struct got_pathlist_entry *pe;
6435 struct got_reference *head_ref = NULL;
6436 struct got_object_id *head_commit_id = NULL;
6437 char *diff_path = NULL;
6438 int have_staged_files = 0;
6440 *new_commit_id = NULL;
6442 memset(&cc_arg, 0, sizeof(cc_arg));
6443 TAILQ_INIT(&commitable_paths);
6445 err = lock_worktree(worktree, LOCK_EX);
6449 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6453 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6457 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6461 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6462 &have_staged_files);
6463 if (err && err->code != GOT_ERR_CANCELLED)
6465 if (have_staged_files) {
6466 err = check_non_staged_files(fileindex, paths);
6471 cc_arg.commitable_paths = &commitable_paths;
6472 cc_arg.worktree = worktree;
6473 cc_arg.fileindex = fileindex;
6475 cc_arg.have_staged_files = have_staged_files;
6476 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6477 cc_arg.diff_header_shown = 0;
6478 cc_arg.commit_conflicts = commit_conflicts;
6480 err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6481 GOT_TMPDIR_STR "/got", ".diff");
6484 cc_arg.f1 = got_opentemp();
6485 if (cc_arg.f1 == NULL) {
6486 err = got_error_from_errno("got_opentemp");
6489 cc_arg.f2 = got_opentemp();
6490 if (cc_arg.f2 == NULL) {
6491 err = got_error_from_errno("got_opentemp");
6496 TAILQ_FOREACH(pe, paths, entry) {
6497 err = worktree_status(worktree, pe->path, fileindex, repo,
6498 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6504 if (fflush(cc_arg.diff_outfile) == EOF) {
6505 err = got_error_from_errno("fflush");
6510 if (TAILQ_EMPTY(&commitable_paths)) {
6511 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6515 TAILQ_FOREACH(pe, paths, entry) {
6516 err = check_path_is_commitable(pe->path, &commitable_paths);
6521 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6522 struct got_commitable *ct = pe->data;
6523 const char *ct_path = ct->in_repo_path;
6525 while (ct_path[0] == '/')
6527 err = check_out_of_date(ct_path, ct->status,
6528 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6529 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6535 err = commit_worktree(new_commit_id, &commitable_paths,
6536 head_commit_id, NULL, worktree, author, committer,
6537 (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6538 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6542 err = update_fileindex_after_commit(worktree, &commitable_paths,
6543 *new_commit_id, fileindex, have_staged_files);
6544 sync_err = sync_fileindex(fileindex, fileindex_path);
6545 if (sync_err && err == NULL)
6549 got_fileindex_free(fileindex);
6550 free(fileindex_path);
6551 unlockerr = lock_worktree(worktree, LOCK_SH);
6552 if (unlockerr && err == NULL)
6554 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6555 struct got_commitable *ct = pe->data;
6557 free_commitable(ct);
6559 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6560 if (diff_path && unlink(diff_path) == -1 && err == NULL)
6561 err = got_error_from_errno2("unlink", diff_path);
6563 if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6565 err = got_error_from_errno("fclose");
6570 got_commitable_get_path(struct got_commitable *ct)
6576 got_commitable_get_status(struct got_commitable *ct)
6581 struct check_rebase_ok_arg {
6582 struct got_worktree *worktree;
6583 struct got_repository *repo;
6586 static const struct got_error *
6587 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6589 const struct got_error *err = NULL;
6590 struct check_rebase_ok_arg *a = arg;
6591 unsigned char status;
6595 /* Reject rebase of a work tree with mixed base commits. */
6596 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6597 SHA1_DIGEST_LENGTH))
6598 return got_error(GOT_ERR_MIXED_COMMITS);
6600 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6602 return got_error_from_errno("asprintf");
6604 /* Reject rebase of a work tree with modified or staged files. */
6605 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6610 if (status != GOT_STATUS_NO_CHANGE)
6611 return got_error(GOT_ERR_MODIFIED);
6612 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6613 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6618 const struct got_error *
6619 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6620 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6621 struct got_worktree *worktree, struct got_reference *branch,
6622 struct got_repository *repo)
6624 const struct got_error *err = NULL;
6625 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6626 char *branch_ref_name = NULL;
6627 char *fileindex_path = NULL;
6628 struct check_rebase_ok_arg ok_arg;
6629 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6630 struct got_object_id *wt_branch_tip = NULL;
6632 *new_base_branch_ref = NULL;
6636 err = lock_worktree(worktree, LOCK_EX);
6640 err = open_fileindex(fileindex, &fileindex_path, worktree);
6644 ok_arg.worktree = worktree;
6646 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6651 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6655 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6659 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6663 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6668 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6671 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6672 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6676 err = got_ref_alloc_symref(new_base_branch_ref,
6677 new_base_branch_ref_name, wt_branch);
6680 err = got_ref_write(*new_base_branch_ref, repo);
6684 /* TODO Lock original branch's ref while rebasing? */
6686 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6690 err = got_ref_write(branch_ref, repo);
6694 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6695 worktree->base_commit_id);
6698 err = got_ref_write(*tmp_branch, repo);
6702 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6706 free(fileindex_path);
6707 free(tmp_branch_name);
6708 free(new_base_branch_ref_name);
6709 free(branch_ref_name);
6711 got_ref_close(branch_ref);
6713 got_ref_close(wt_branch);
6714 free(wt_branch_tip);
6716 if (*new_base_branch_ref) {
6717 got_ref_close(*new_base_branch_ref);
6718 *new_base_branch_ref = NULL;
6721 got_ref_close(*tmp_branch);
6725 got_fileindex_free(*fileindex);
6728 lock_worktree(worktree, LOCK_SH);
6733 const struct got_error *
6734 got_worktree_rebase_continue(struct got_object_id **commit_id,
6735 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6736 struct got_reference **branch, struct got_fileindex **fileindex,
6737 struct got_worktree *worktree, struct got_repository *repo)
6739 const struct got_error *err;
6740 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6741 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6742 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6743 char *fileindex_path = NULL;
6744 int have_staged_files = 0;
6747 *new_base_branch = NULL;
6752 err = lock_worktree(worktree, LOCK_EX);
6756 err = open_fileindex(fileindex, &fileindex_path, worktree);
6760 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6761 &have_staged_files);
6762 if (err && err->code != GOT_ERR_CANCELLED)
6764 if (have_staged_files) {
6765 err = got_error(GOT_ERR_STAGED_PATHS);
6769 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6773 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6777 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6781 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6785 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6789 err = got_ref_open(branch, repo,
6790 got_ref_get_symref_target(branch_ref), 0);
6794 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6798 err = got_ref_resolve(commit_id, repo, commit_ref);
6802 err = got_ref_open(new_base_branch, repo,
6803 new_base_branch_ref_name, 0);
6807 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6811 free(commit_ref_name);
6812 free(branch_ref_name);
6813 free(fileindex_path);
6815 got_ref_close(commit_ref);
6817 got_ref_close(branch_ref);
6822 got_ref_close(*tmp_branch);
6825 if (*new_base_branch) {
6826 got_ref_close(*new_base_branch);
6827 *new_base_branch = NULL;
6830 got_ref_close(*branch);
6834 got_fileindex_free(*fileindex);
6837 lock_worktree(worktree, LOCK_SH);
6842 const struct got_error *
6843 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6845 const struct got_error *err;
6846 char *tmp_branch_name = NULL;
6848 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6852 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6853 free(tmp_branch_name);
6857 static const struct got_error *
6858 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6859 const char *diff_path, char **logmsg, void *arg)
6865 static const struct got_error *
6866 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6867 const char *path, struct got_object_id *blob_id,
6868 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6869 int dirfd, const char *de_name)
6874 struct collect_merged_paths_arg {
6875 got_worktree_checkout_cb progress_cb;
6877 struct got_pathlist_head *merged_paths;
6880 static const struct got_error *
6881 collect_merged_paths(void *arg, unsigned char status, const char *path)
6883 const struct got_error *err;
6884 struct collect_merged_paths_arg *a = arg;
6886 struct got_pathlist_entry *new;
6888 err = (*a->progress_cb)(a->progress_arg, status, path);
6892 if (status != GOT_STATUS_MERGE &&
6893 status != GOT_STATUS_ADD &&
6894 status != GOT_STATUS_DELETE &&
6895 status != GOT_STATUS_CONFLICT)
6900 return got_error_from_errno("strdup");
6902 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6903 if (err || new == NULL)
6908 static const struct got_error *
6909 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6910 int is_rebase, struct got_repository *repo)
6912 const struct got_error *err;
6913 struct got_reference *commit_ref = NULL;
6915 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6917 if (err->code != GOT_ERR_NOT_REF)
6919 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6922 err = got_ref_write(commit_ref, repo);
6925 } else if (is_rebase) {
6926 struct got_object_id *stored_id;
6929 err = got_ref_resolve(&stored_id, repo, commit_ref);
6932 cmp = got_object_id_cmp(commit_id, stored_id);
6935 err = got_error(GOT_ERR_REBASE_COMMITID);
6941 got_ref_close(commit_ref);
6945 static const struct got_error *
6946 rebase_merge_files(struct got_pathlist_head *merged_paths,
6947 const char *commit_ref_name, struct got_worktree *worktree,
6948 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6949 struct got_object_id *commit_id, struct got_repository *repo,
6950 got_worktree_checkout_cb progress_cb, void *progress_arg,
6951 got_cancel_cb cancel_cb, void *cancel_arg)
6953 const struct got_error *err;
6954 struct got_reference *commit_ref = NULL;
6955 struct collect_merged_paths_arg cmp_arg;
6956 char *fileindex_path;
6958 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6960 err = get_fileindex_path(&fileindex_path, worktree);
6964 cmp_arg.progress_cb = progress_cb;
6965 cmp_arg.progress_arg = progress_arg;
6966 cmp_arg.merged_paths = merged_paths;
6967 err = merge_files(worktree, fileindex, fileindex_path,
6968 parent_commit_id, commit_id, repo, collect_merged_paths,
6969 &cmp_arg, cancel_cb, cancel_arg);
6971 got_ref_close(commit_ref);
6975 const struct got_error *
6976 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6977 struct got_worktree *worktree, struct got_fileindex *fileindex,
6978 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6979 struct got_repository *repo,
6980 got_worktree_checkout_cb progress_cb, void *progress_arg,
6981 got_cancel_cb cancel_cb, void *cancel_arg)
6983 const struct got_error *err;
6984 char *commit_ref_name;
6986 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6990 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6994 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6995 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6996 progress_arg, cancel_cb, cancel_arg);
6998 free(commit_ref_name);
7002 const struct got_error *
7003 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
7004 struct got_worktree *worktree, struct got_fileindex *fileindex,
7005 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
7006 struct got_repository *repo,
7007 got_worktree_checkout_cb progress_cb, void *progress_arg,
7008 got_cancel_cb cancel_cb, void *cancel_arg)
7010 const struct got_error *err;
7011 char *commit_ref_name;
7013 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7017 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7021 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7022 fileindex, parent_commit_id, commit_id, repo, progress_cb,
7023 progress_arg, cancel_cb, cancel_arg);
7025 free(commit_ref_name);
7029 static const struct got_error *
7030 rebase_commit(struct got_object_id **new_commit_id,
7031 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
7032 struct got_worktree *worktree, struct got_fileindex *fileindex,
7033 struct got_reference *tmp_branch, const char *committer,
7034 struct got_commit_object *orig_commit, const char *new_logmsg,
7035 int allow_conflict, struct got_repository *repo)
7037 const struct got_error *err, *sync_err;
7038 struct got_pathlist_head commitable_paths;
7039 struct collect_commitables_arg cc_arg;
7040 char *fileindex_path = NULL;
7041 struct got_reference *head_ref = NULL;
7042 struct got_object_id *head_commit_id = NULL;
7043 char *logmsg = NULL;
7045 memset(&cc_arg, 0, sizeof(cc_arg));
7046 TAILQ_INIT(&commitable_paths);
7047 *new_commit_id = NULL;
7049 /* Work tree is locked/unlocked during rebase preparation/teardown. */
7051 err = get_fileindex_path(&fileindex_path, worktree);
7055 cc_arg.commitable_paths = &commitable_paths;
7056 cc_arg.worktree = worktree;
7058 cc_arg.have_staged_files = 0;
7059 cc_arg.commit_conflicts = allow_conflict;
7061 * If possible get the status of individual files directly to
7062 * avoid crawling the entire work tree once per rebased commit.
7064 * Ideally, merged_paths would contain a list of commitables
7065 * we could use so we could skip worktree_status() entirely.
7066 * However, we would then need carefully keep track of cumulative
7067 * effects of operations such as file additions and deletions
7068 * in 'got histedit -f' (folding multiple commits into one),
7069 * and this extra complexity is not really worth it.
7072 struct got_pathlist_entry *pe;
7073 TAILQ_FOREACH(pe, merged_paths, entry) {
7074 err = worktree_status(worktree, pe->path, fileindex,
7075 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
7081 err = worktree_status(worktree, "", fileindex, repo,
7082 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7087 if (TAILQ_EMPTY(&commitable_paths)) {
7088 /* No-op change; commit will be elided. */
7089 err = got_ref_delete(commit_ref, repo);
7092 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
7096 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7100 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7105 logmsg = strdup(new_logmsg);
7106 if (logmsg == NULL) {
7107 err = got_error_from_errno("strdup");
7111 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
7116 /* NB: commit_worktree will call free(logmsg) */
7117 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
7118 NULL, worktree, got_object_commit_get_author(orig_commit),
7119 committer ? committer :
7120 got_object_commit_get_committer(orig_commit), NULL,
7121 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
7125 err = got_ref_change_ref(tmp_branch, *new_commit_id);
7129 err = got_ref_delete(commit_ref, repo);
7133 err = update_fileindex_after_commit(worktree, &commitable_paths,
7134 *new_commit_id, fileindex, 0);
7135 sync_err = sync_fileindex(fileindex, fileindex_path);
7136 if (sync_err && err == NULL)
7139 free(fileindex_path);
7140 free(head_commit_id);
7142 got_ref_close(head_ref);
7144 free(*new_commit_id);
7145 *new_commit_id = NULL;
7150 const struct got_error *
7151 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
7152 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7153 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7154 const char *committer, struct got_commit_object *orig_commit,
7155 struct got_object_id *orig_commit_id, int allow_conflict,
7156 struct got_repository *repo)
7158 const struct got_error *err;
7159 char *commit_ref_name;
7160 struct got_reference *commit_ref = NULL;
7161 struct got_object_id *commit_id = NULL;
7163 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7167 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7170 err = got_ref_resolve(&commit_id, repo, commit_ref);
7173 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
7174 err = got_error(GOT_ERR_REBASE_COMMITID);
7178 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7179 worktree, fileindex, tmp_branch, committer, orig_commit,
7180 NULL, allow_conflict, repo);
7183 got_ref_close(commit_ref);
7184 free(commit_ref_name);
7189 const struct got_error *
7190 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
7191 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7192 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7193 const char *committer, struct got_commit_object *orig_commit,
7194 struct got_object_id *orig_commit_id, const char *new_logmsg,
7195 int allow_conflict, struct got_repository *repo)
7197 const struct got_error *err;
7198 char *commit_ref_name;
7199 struct got_reference *commit_ref = NULL;
7201 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7205 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7209 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7210 worktree, fileindex, tmp_branch, committer, orig_commit,
7211 new_logmsg, allow_conflict, repo);
7214 got_ref_close(commit_ref);
7215 free(commit_ref_name);
7219 const struct got_error *
7220 got_worktree_rebase_postpone(struct got_worktree *worktree,
7221 struct got_fileindex *fileindex)
7224 got_fileindex_free(fileindex);
7225 return lock_worktree(worktree, LOCK_SH);
7228 static const struct got_error *
7229 delete_ref(const char *name, struct got_repository *repo)
7231 const struct got_error *err;
7232 struct got_reference *ref;
7234 err = got_ref_open(&ref, repo, name, 0);
7236 if (err->code == GOT_ERR_NOT_REF)
7241 err = got_ref_delete(ref, repo);
7246 static const struct got_error *
7247 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7249 const struct got_error *err;
7250 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7251 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7253 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7256 err = delete_ref(tmp_branch_name, repo);
7260 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7263 err = delete_ref(new_base_branch_ref_name, repo);
7267 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7270 err = delete_ref(branch_ref_name, repo);
7274 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7277 err = delete_ref(commit_ref_name, repo);
7282 free(tmp_branch_name);
7283 free(new_base_branch_ref_name);
7284 free(branch_ref_name);
7285 free(commit_ref_name);
7289 static const struct got_error *
7290 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7291 struct got_object_id *new_commit_id, struct got_repository *repo)
7293 const struct got_error *err;
7294 struct got_reference *ref = NULL;
7295 struct got_object_id *old_commit_id = NULL;
7296 const char *branch_name = NULL;
7297 char *new_id_str = NULL;
7298 char *refname = NULL;
7300 branch_name = got_ref_get_name(branch);
7301 if (strncmp(branch_name, "refs/heads/", 11) != 0)
7302 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7305 err = got_object_id_str(&new_id_str, new_commit_id);
7309 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7310 new_id_str) == -1) {
7311 err = got_error_from_errno("asprintf");
7315 err = got_ref_resolve(&old_commit_id, repo, branch);
7319 err = got_ref_alloc(&ref, refname, old_commit_id);
7323 err = got_ref_write(ref, repo);
7327 free(old_commit_id);
7333 const struct got_error *
7334 got_worktree_rebase_complete(struct got_worktree *worktree,
7335 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7336 struct got_reference *rebased_branch, struct got_repository *repo,
7339 const struct got_error *err, *unlockerr, *sync_err;
7340 struct got_object_id *new_head_commit_id = NULL;
7341 char *fileindex_path = NULL;
7343 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7347 if (create_backup) {
7348 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7349 rebased_branch, new_head_commit_id, repo);
7354 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7358 err = got_ref_write(rebased_branch, repo);
7362 err = got_worktree_set_head_ref(worktree, rebased_branch);
7366 err = delete_rebase_refs(worktree, repo);
7370 err = get_fileindex_path(&fileindex_path, worktree);
7373 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7374 sync_err = sync_fileindex(fileindex, fileindex_path);
7375 if (sync_err && err == NULL)
7378 got_fileindex_free(fileindex);
7379 free(fileindex_path);
7380 free(new_head_commit_id);
7381 unlockerr = lock_worktree(worktree, LOCK_SH);
7382 if (unlockerr && err == NULL)
7387 static const struct got_error *
7388 get_paths_changed_between_commits(struct got_pathlist_head *paths,
7389 struct got_object_id *id1, struct got_object_id *id2,
7390 struct got_repository *repo)
7392 const struct got_error *err;
7393 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
7394 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7397 err = got_object_open_as_commit(&commit1, repo, id1);
7401 err = got_object_open_as_tree(&tree1, repo,
7402 got_object_commit_get_tree_id(commit1));
7408 err = got_object_open_as_commit(&commit2, repo, id2);
7412 err = got_object_open_as_tree(&tree2, repo,
7413 got_object_commit_get_tree_id(commit2));
7418 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
7419 got_diff_tree_collect_changed_paths, paths, 0);
7424 got_object_commit_close(commit1);
7426 got_object_commit_close(commit2);
7428 got_object_tree_close(tree1);
7430 got_object_tree_close(tree2);
7434 static const struct got_error *
7435 get_paths_added_between_commits(struct got_pathlist_head *added_paths,
7436 struct got_object_id *id1, struct got_object_id *id2,
7437 const char *path_prefix, struct got_repository *repo)
7439 const struct got_error *err;
7440 struct got_pathlist_head merged_paths;
7441 struct got_pathlist_entry *pe;
7442 char *abspath = NULL, *wt_path = NULL;
7444 TAILQ_INIT(&merged_paths);
7446 err = get_paths_changed_between_commits(&merged_paths, id1, id2, repo);
7450 TAILQ_FOREACH(pe, &merged_paths, entry) {
7451 struct got_diff_changed_path *change = pe->data;
7453 if (change->status != GOT_STATUS_ADD)
7456 if (got_path_is_root_dir(path_prefix)) {
7457 wt_path = strdup(pe->path);
7458 if (wt_path == NULL) {
7459 err = got_error_from_errno("strdup");
7463 if (asprintf(&abspath, "/%s", pe->path) == -1) {
7464 err = got_error_from_errno("asprintf");
7468 err = got_path_skip_common_ancestor(&wt_path,
7469 path_prefix, abspath);
7476 err = got_pathlist_append(added_paths, wt_path, NULL);
7483 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_ALL);
7489 static const struct got_error *
7490 get_paths_added_in_commit(struct got_pathlist_head *added_paths,
7491 struct got_object_id *id, const char *path_prefix,
7492 struct got_repository *repo)
7494 const struct got_error *err;
7495 struct got_commit_object *commit = NULL;
7496 struct got_object_qid *pid;
7498 err = got_object_open_as_commit(&commit, repo, id);
7502 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7504 err = get_paths_added_between_commits(added_paths,
7505 pid ? &pid->id : NULL, id, path_prefix, repo);
7510 got_object_commit_close(commit);
7514 const struct got_error *
7515 got_worktree_rebase_abort(struct got_worktree *worktree,
7516 struct got_fileindex *fileindex, struct got_repository *repo,
7517 struct got_reference *new_base_branch,
7518 got_worktree_checkout_cb progress_cb, void *progress_arg)
7520 const struct got_error *err, *unlockerr, *sync_err;
7521 struct got_reference *resolved = NULL;
7522 struct got_object_id *commit_id = NULL;
7523 struct got_object_id *merged_commit_id = NULL;
7524 struct got_commit_object *commit = NULL;
7525 char *fileindex_path = NULL;
7526 char *commit_ref_name = NULL;
7527 struct got_reference *commit_ref = NULL;
7528 struct revert_file_args rfa;
7529 struct got_object_id *tree_id = NULL;
7530 struct got_pathlist_head added_paths;
7532 TAILQ_INIT(&added_paths);
7534 err = lock_worktree(worktree, LOCK_EX);
7538 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7542 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7546 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
7551 * Determine which files in added status can be safely removed
7552 * from disk while reverting changes in the work tree.
7553 * We want to avoid deleting unrelated files which were added by
7554 * the user for conflict resolution purposes.
7556 err = get_paths_added_in_commit(&added_paths, merged_commit_id,
7557 got_worktree_get_path_prefix(worktree), repo);
7561 err = got_ref_open(&resolved, repo,
7562 got_ref_get_symref_target(new_base_branch), 0);
7566 err = got_worktree_set_head_ref(worktree, resolved);
7571 * XXX commits to the base branch could have happened while
7572 * we were busy rebasing; should we store the original commit ID
7573 * when rebase begins and read it back here?
7575 err = got_ref_resolve(&commit_id, repo, resolved);
7579 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7583 err = got_object_open_as_commit(&commit, repo,
7584 worktree->base_commit_id);
7588 err = got_object_id_by_path(&tree_id, repo, commit,
7589 worktree->path_prefix);
7593 err = delete_rebase_refs(worktree, repo);
7597 err = get_fileindex_path(&fileindex_path, worktree);
7601 rfa.worktree = worktree;
7602 rfa.fileindex = fileindex;
7603 rfa.progress_cb = progress_cb;
7604 rfa.progress_arg = progress_arg;
7605 rfa.patch_cb = NULL;
7606 rfa.patch_arg = NULL;
7608 rfa.unlink_added_files = 1;
7609 rfa.added_files_to_unlink = &added_paths;
7610 err = worktree_status(worktree, "", fileindex, repo,
7611 revert_file, &rfa, NULL, NULL, 1, 0);
7615 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7616 repo, progress_cb, progress_arg, NULL, NULL);
7618 sync_err = sync_fileindex(fileindex, fileindex_path);
7619 if (sync_err && err == NULL)
7622 got_pathlist_free(&added_paths, GOT_PATHLIST_FREE_PATH);
7623 got_ref_close(resolved);
7626 free(merged_commit_id);
7628 got_object_commit_close(commit);
7630 got_fileindex_free(fileindex);
7631 free(fileindex_path);
7632 free(commit_ref_name);
7634 got_ref_close(commit_ref);
7636 unlockerr = lock_worktree(worktree, LOCK_SH);
7637 if (unlockerr && err == NULL)
7642 const struct got_error *
7643 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7644 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7645 struct got_fileindex **fileindex, struct got_worktree *worktree,
7646 struct got_repository *repo)
7648 const struct got_error *err = NULL;
7649 char *tmp_branch_name = NULL;
7650 char *branch_ref_name = NULL;
7651 char *base_commit_ref_name = NULL;
7652 char *fileindex_path = NULL;
7653 struct check_rebase_ok_arg ok_arg;
7654 struct got_reference *wt_branch = NULL;
7655 struct got_reference *base_commit_ref = NULL;
7659 *base_commit_id = NULL;
7662 err = lock_worktree(worktree, LOCK_EX);
7666 err = open_fileindex(fileindex, &fileindex_path, worktree);
7670 ok_arg.worktree = worktree;
7672 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7677 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7681 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7685 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7690 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7695 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7699 err = got_ref_write(*branch_ref, repo);
7703 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7704 worktree->base_commit_id);
7707 err = got_ref_write(base_commit_ref, repo);
7710 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7711 if (*base_commit_id == NULL) {
7712 err = got_error_from_errno("got_object_id_dup");
7716 err = got_ref_alloc(tmp_branch, tmp_branch_name,
7717 worktree->base_commit_id);
7720 err = got_ref_write(*tmp_branch, repo);
7724 err = got_worktree_set_head_ref(worktree, *tmp_branch);
7728 free(fileindex_path);
7729 free(tmp_branch_name);
7730 free(branch_ref_name);
7731 free(base_commit_ref_name);
7733 got_ref_close(wt_branch);
7736 got_ref_close(*branch_ref);
7740 got_ref_close(*tmp_branch);
7743 free(*base_commit_id);
7745 got_fileindex_free(*fileindex);
7748 lock_worktree(worktree, LOCK_SH);
7753 const struct got_error *
7754 got_worktree_histedit_postpone(struct got_worktree *worktree,
7755 struct got_fileindex *fileindex)
7758 got_fileindex_free(fileindex);
7759 return lock_worktree(worktree, LOCK_SH);
7762 const struct got_error *
7763 got_worktree_histedit_in_progress(int *in_progress,
7764 struct got_worktree *worktree)
7766 const struct got_error *err;
7767 char *tmp_branch_name = NULL;
7769 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7773 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7774 free(tmp_branch_name);
7778 const struct got_error *
7779 got_worktree_histedit_continue(struct got_object_id **commit_id,
7780 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7781 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7782 struct got_worktree *worktree, struct got_repository *repo)
7784 const struct got_error *err;
7785 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7786 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7787 struct got_reference *commit_ref = NULL;
7788 struct got_reference *base_commit_ref = NULL;
7789 char *fileindex_path = NULL;
7790 int have_staged_files = 0;
7794 *base_commit_id = NULL;
7797 err = lock_worktree(worktree, LOCK_EX);
7801 err = open_fileindex(fileindex, &fileindex_path, worktree);
7805 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7806 &have_staged_files);
7807 if (err && err->code != GOT_ERR_CANCELLED)
7809 if (have_staged_files) {
7810 err = got_error(GOT_ERR_STAGED_PATHS);
7814 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7818 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7822 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7826 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7831 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7835 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7838 err = got_ref_resolve(commit_id, repo, commit_ref);
7842 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7845 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7849 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7853 free(commit_ref_name);
7854 free(branch_ref_name);
7855 free(fileindex_path);
7857 got_ref_close(commit_ref);
7858 if (base_commit_ref)
7859 got_ref_close(base_commit_ref);
7863 free(*base_commit_id);
7864 *base_commit_id = NULL;
7866 got_ref_close(*tmp_branch);
7870 got_fileindex_free(*fileindex);
7873 lock_worktree(worktree, LOCK_EX);
7878 static const struct got_error *
7879 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7881 const struct got_error *err;
7882 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7883 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7885 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7888 err = delete_ref(tmp_branch_name, repo);
7892 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7896 err = delete_ref(base_commit_ref_name, repo);
7900 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7903 err = delete_ref(branch_ref_name, repo);
7907 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7910 err = delete_ref(commit_ref_name, repo);
7914 free(tmp_branch_name);
7915 free(base_commit_ref_name);
7916 free(branch_ref_name);
7917 free(commit_ref_name);
7921 const struct got_error *
7922 got_worktree_histedit_abort(struct got_worktree *worktree,
7923 struct got_fileindex *fileindex, struct got_repository *repo,
7924 struct got_reference *branch, struct got_object_id *base_commit_id,
7925 got_worktree_checkout_cb progress_cb, void *progress_arg)
7927 const struct got_error *err, *unlockerr, *sync_err;
7928 struct got_reference *resolved = NULL;
7929 char *fileindex_path = NULL;
7930 struct got_object_id *merged_commit_id = NULL;
7931 struct got_commit_object *commit = NULL;
7932 char *commit_ref_name = NULL;
7933 struct got_reference *commit_ref = NULL;
7934 struct got_object_id *tree_id = NULL;
7935 struct revert_file_args rfa;
7936 struct got_pathlist_head added_paths;
7938 TAILQ_INIT(&added_paths);
7940 err = lock_worktree(worktree, LOCK_EX);
7944 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7948 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7950 if (err->code != GOT_ERR_NOT_REF)
7952 /* Can happen on early abort due to invalid histedit script. */
7957 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
7962 * Determine which files in added status can be safely removed
7963 * from disk while reverting changes in the work tree.
7964 * We want to avoid deleting unrelated files added by the
7965 * user during conflict resolution or during histedit -e.
7967 err = get_paths_added_in_commit(&added_paths, merged_commit_id,
7968 got_worktree_get_path_prefix(worktree), repo);
7973 err = got_ref_open(&resolved, repo,
7974 got_ref_get_symref_target(branch), 0);
7978 err = got_worktree_set_head_ref(worktree, resolved);
7982 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7986 err = got_object_open_as_commit(&commit, repo,
7987 worktree->base_commit_id);
7991 err = got_object_id_by_path(&tree_id, repo, commit,
7992 worktree->path_prefix);
7996 err = delete_histedit_refs(worktree, repo);
8000 err = get_fileindex_path(&fileindex_path, worktree);
8004 rfa.worktree = worktree;
8005 rfa.fileindex = fileindex;
8006 rfa.progress_cb = progress_cb;
8007 rfa.progress_arg = progress_arg;
8008 rfa.patch_cb = NULL;
8009 rfa.patch_arg = NULL;
8011 rfa.unlink_added_files = 1;
8012 rfa.added_files_to_unlink = &added_paths;
8013 err = worktree_status(worktree, "", fileindex, repo,
8014 revert_file, &rfa, NULL, NULL, 1, 0);
8018 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8019 repo, progress_cb, progress_arg, NULL, NULL);
8021 sync_err = sync_fileindex(fileindex, fileindex_path);
8022 if (sync_err && err == NULL)
8026 got_ref_close(resolved);
8028 got_ref_close(commit_ref);
8029 free(merged_commit_id);
8031 free(fileindex_path);
8032 free(commit_ref_name);
8034 unlockerr = lock_worktree(worktree, LOCK_SH);
8035 if (unlockerr && err == NULL)
8040 const struct got_error *
8041 got_worktree_histedit_complete(struct got_worktree *worktree,
8042 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8043 struct got_reference *edited_branch, struct got_repository *repo)
8045 const struct got_error *err, *unlockerr, *sync_err;
8046 struct got_object_id *new_head_commit_id = NULL;
8047 struct got_reference *resolved = NULL;
8048 char *fileindex_path = NULL;
8050 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
8054 err = got_ref_open(&resolved, repo,
8055 got_ref_get_symref_target(edited_branch), 0);
8059 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
8060 resolved, new_head_commit_id, repo);
8064 err = got_ref_change_ref(resolved, new_head_commit_id);
8068 err = got_ref_write(resolved, repo);
8072 err = got_worktree_set_head_ref(worktree, resolved);
8076 err = delete_histedit_refs(worktree, repo);
8080 err = get_fileindex_path(&fileindex_path, worktree);
8083 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8084 sync_err = sync_fileindex(fileindex, fileindex_path);
8085 if (sync_err && err == NULL)
8088 got_fileindex_free(fileindex);
8089 free(fileindex_path);
8090 free(new_head_commit_id);
8091 unlockerr = lock_worktree(worktree, LOCK_SH);
8092 if (unlockerr && err == NULL)
8097 const struct got_error *
8098 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
8099 struct got_object_id *commit_id, struct got_repository *repo)
8101 const struct got_error *err;
8102 char *commit_ref_name;
8104 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8108 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
8112 err = delete_ref(commit_ref_name, repo);
8114 free(commit_ref_name);
8118 const struct got_error *
8119 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
8120 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
8121 struct got_worktree *worktree, const char *refname,
8122 struct got_repository *repo)
8124 const struct got_error *err = NULL;
8125 char *fileindex_path = NULL;
8126 struct check_rebase_ok_arg ok_arg;
8130 *base_branch_ref = NULL;
8132 err = lock_worktree(worktree, LOCK_EX);
8136 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
8137 err = got_error_msg(GOT_ERR_SAME_BRANCH,
8138 "cannot integrate a branch into itself; "
8139 "update -b or different branch name required");
8143 err = open_fileindex(fileindex, &fileindex_path, worktree);
8147 /* Preconditions are the same as for rebase. */
8148 ok_arg.worktree = worktree;
8150 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8155 err = got_ref_open(branch_ref, repo, refname, 1);
8159 err = got_ref_open(base_branch_ref, repo,
8160 got_worktree_get_head_ref_name(worktree), 1);
8164 got_ref_close(*branch_ref);
8167 if (*base_branch_ref) {
8168 got_ref_close(*base_branch_ref);
8169 *base_branch_ref = NULL;
8172 got_fileindex_free(*fileindex);
8175 lock_worktree(worktree, LOCK_SH);
8180 const struct got_error *
8181 got_worktree_integrate_continue(struct got_worktree *worktree,
8182 struct got_fileindex *fileindex, struct got_repository *repo,
8183 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
8184 got_worktree_checkout_cb progress_cb, void *progress_arg,
8185 got_cancel_cb cancel_cb, void *cancel_arg)
8187 const struct got_error *err = NULL, *sync_err, *unlockerr;
8188 char *fileindex_path = NULL;
8189 struct got_object_id *tree_id = NULL, *commit_id = NULL;
8190 struct got_commit_object *commit = NULL;
8192 err = get_fileindex_path(&fileindex_path, worktree);
8196 err = got_ref_resolve(&commit_id, repo, branch_ref);
8200 err = got_object_open_as_commit(&commit, repo, commit_id);
8204 err = got_object_id_by_path(&tree_id, repo, commit,
8205 worktree->path_prefix);
8209 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
8213 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
8214 progress_cb, progress_arg, cancel_cb, cancel_arg);
8218 err = got_ref_change_ref(base_branch_ref, commit_id);
8222 err = got_ref_write(base_branch_ref, repo);
8226 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8228 sync_err = sync_fileindex(fileindex, fileindex_path);
8229 if (sync_err && err == NULL)
8233 unlockerr = got_ref_unlock(branch_ref);
8234 if (unlockerr && err == NULL)
8236 got_ref_close(branch_ref);
8238 unlockerr = got_ref_unlock(base_branch_ref);
8239 if (unlockerr && err == NULL)
8241 got_ref_close(base_branch_ref);
8243 got_fileindex_free(fileindex);
8244 free(fileindex_path);
8247 got_object_commit_close(commit);
8249 unlockerr = lock_worktree(worktree, LOCK_SH);
8250 if (unlockerr && err == NULL)
8255 const struct got_error *
8256 got_worktree_integrate_abort(struct got_worktree *worktree,
8257 struct got_fileindex *fileindex, struct got_repository *repo,
8258 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
8260 const struct got_error *err = NULL, *unlockerr = NULL;
8262 got_fileindex_free(fileindex);
8264 err = lock_worktree(worktree, LOCK_SH);
8266 unlockerr = got_ref_unlock(branch_ref);
8267 if (unlockerr && err == NULL)
8269 got_ref_close(branch_ref);
8271 unlockerr = got_ref_unlock(base_branch_ref);
8272 if (unlockerr && err == NULL)
8274 got_ref_close(base_branch_ref);
8279 const struct got_error *
8280 got_worktree_merge_postpone(struct got_worktree *worktree,
8281 struct got_fileindex *fileindex)
8283 const struct got_error *err, *sync_err;
8284 char *fileindex_path = NULL;
8286 err = get_fileindex_path(&fileindex_path, worktree);
8290 sync_err = sync_fileindex(fileindex, fileindex_path);
8292 err = lock_worktree(worktree, LOCK_SH);
8293 if (sync_err && err == NULL)
8296 got_fileindex_free(fileindex);
8297 free(fileindex_path);
8301 static const struct got_error *
8302 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
8304 const struct got_error *err;
8305 char *branch_refname = NULL, *commit_refname = NULL;
8307 err = get_merge_branch_ref_name(&branch_refname, worktree);
8310 err = delete_ref(branch_refname, repo);
8314 err = get_merge_commit_ref_name(&commit_refname, worktree);
8317 err = delete_ref(commit_refname, repo);
8322 free(branch_refname);
8323 free(commit_refname);
8327 struct merge_commit_msg_arg {
8328 struct got_worktree *worktree;
8329 const char *branch_name;
8332 static const struct got_error *
8333 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
8334 const char *diff_path, char **logmsg, void *arg)
8336 struct merge_commit_msg_arg *a = arg;
8338 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
8339 got_worktree_get_head_ref_name(a->worktree)) == -1)
8340 return got_error_from_errno("asprintf");
8346 const struct got_error *
8347 got_worktree_merge_branch(struct got_worktree *worktree,
8348 struct got_fileindex *fileindex,
8349 struct got_object_id *yca_commit_id,
8350 struct got_object_id *branch_tip,
8351 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
8352 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
8354 const struct got_error *err;
8355 char *fileindex_path = NULL;
8357 err = get_fileindex_path(&fileindex_path, worktree);
8361 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
8366 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
8367 branch_tip, repo, progress_cb, progress_arg,
8368 cancel_cb, cancel_arg);
8370 free(fileindex_path);
8374 const struct got_error *
8375 got_worktree_merge_commit(struct got_object_id **new_commit_id,
8376 struct got_worktree *worktree, struct got_fileindex *fileindex,
8377 const char *author, const char *committer, int allow_bad_symlinks,
8378 struct got_object_id *branch_tip, const char *branch_name,
8379 int allow_conflict, struct got_repository *repo,
8380 got_worktree_status_cb status_cb, void *status_arg)
8383 const struct got_error *err = NULL, *sync_err;
8384 struct got_pathlist_head commitable_paths;
8385 struct collect_commitables_arg cc_arg;
8386 struct got_pathlist_entry *pe;
8387 struct got_reference *head_ref = NULL;
8388 struct got_object_id *head_commit_id = NULL;
8389 int have_staged_files = 0;
8390 struct merge_commit_msg_arg mcm_arg;
8391 char *fileindex_path = NULL;
8393 memset(&cc_arg, 0, sizeof(cc_arg));
8394 *new_commit_id = NULL;
8396 TAILQ_INIT(&commitable_paths);
8398 err = get_fileindex_path(&fileindex_path, worktree);
8402 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
8406 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8410 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
8411 &have_staged_files);
8412 if (err && err->code != GOT_ERR_CANCELLED)
8414 if (have_staged_files) {
8415 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
8419 cc_arg.commitable_paths = &commitable_paths;
8420 cc_arg.worktree = worktree;
8421 cc_arg.fileindex = fileindex;
8423 cc_arg.have_staged_files = have_staged_files;
8424 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
8425 cc_arg.commit_conflicts = allow_conflict;
8426 err = worktree_status(worktree, "", fileindex, repo,
8427 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8431 mcm_arg.worktree = worktree;
8432 mcm_arg.branch_name = branch_name;
8433 err = commit_worktree(new_commit_id, &commitable_paths,
8434 head_commit_id, branch_tip, worktree, author, committer, NULL,
8435 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8439 err = update_fileindex_after_commit(worktree, &commitable_paths,
8440 *new_commit_id, fileindex, have_staged_files);
8441 sync_err = sync_fileindex(fileindex, fileindex_path);
8442 if (sync_err && err == NULL)
8445 TAILQ_FOREACH(pe, &commitable_paths, entry) {
8446 struct got_commitable *ct = pe->data;
8448 free_commitable(ct);
8450 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8451 free(fileindex_path);
8455 const struct got_error *
8456 got_worktree_merge_complete(struct got_worktree *worktree,
8457 struct got_fileindex *fileindex, struct got_repository *repo)
8459 const struct got_error *err, *unlockerr, *sync_err;
8460 char *fileindex_path = NULL;
8462 err = delete_merge_refs(worktree, repo);
8466 err = get_fileindex_path(&fileindex_path, worktree);
8469 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8470 sync_err = sync_fileindex(fileindex, fileindex_path);
8471 if (sync_err && err == NULL)
8474 got_fileindex_free(fileindex);
8475 free(fileindex_path);
8476 unlockerr = lock_worktree(worktree, LOCK_SH);
8477 if (unlockerr && err == NULL)
8482 const struct got_error *
8483 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8484 struct got_repository *repo)
8486 const struct got_error *err;
8487 char *branch_refname = NULL;
8488 struct got_reference *branch_ref = NULL;
8492 err = get_merge_branch_ref_name(&branch_refname, worktree);
8495 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8496 free(branch_refname);
8498 if (err->code != GOT_ERR_NOT_REF)
8506 const struct got_error *got_worktree_merge_prepare(
8507 struct got_fileindex **fileindex, struct got_worktree *worktree,
8508 struct got_repository *repo)
8510 const struct got_error *err = NULL;
8511 char *fileindex_path = NULL;
8512 struct got_reference *wt_branch = NULL;
8513 struct got_object_id *wt_branch_tip = NULL;
8514 struct check_rebase_ok_arg ok_arg;
8518 err = lock_worktree(worktree, LOCK_EX);
8522 err = open_fileindex(fileindex, &fileindex_path, worktree);
8526 /* Preconditions are the same as for rebase. */
8527 ok_arg.worktree = worktree;
8529 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8534 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8539 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8543 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8544 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8549 free(fileindex_path);
8551 got_ref_close(wt_branch);
8552 free(wt_branch_tip);
8555 got_fileindex_free(*fileindex);
8558 lock_worktree(worktree, LOCK_SH);
8563 const struct got_error *got_worktree_merge_write_refs(
8564 struct got_worktree *worktree, struct got_reference *branch,
8565 struct got_repository *repo)
8567 const struct got_error *err = NULL;
8568 char *branch_refname = NULL, *commit_refname = NULL;
8569 struct got_reference *branch_ref = NULL, *commit_ref = NULL;
8570 struct got_object_id *branch_tip = NULL;
8572 err = get_merge_branch_ref_name(&branch_refname, worktree);
8576 err = get_merge_commit_ref_name(&commit_refname, worktree);
8580 err = got_ref_resolve(&branch_tip, repo, branch);
8584 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8587 err = got_ref_write(branch_ref, repo);
8591 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8594 err = got_ref_write(commit_ref, repo);
8599 free(branch_refname);
8600 free(commit_refname);
8602 got_ref_close(branch_ref);
8604 got_ref_close(commit_ref);
8609 const struct got_error *
8610 got_worktree_merge_continue(char **branch_name,
8611 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8612 struct got_worktree *worktree, struct got_repository *repo)
8614 const struct got_error *err;
8615 char *commit_refname = NULL, *branch_refname = NULL;
8616 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8617 char *fileindex_path = NULL;
8618 int have_staged_files = 0;
8620 *branch_name = NULL;
8624 err = lock_worktree(worktree, LOCK_EX);
8628 err = open_fileindex(fileindex, &fileindex_path, worktree);
8632 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8633 &have_staged_files);
8634 if (err && err->code != GOT_ERR_CANCELLED)
8636 if (have_staged_files) {
8637 err = got_error(GOT_ERR_STAGED_PATHS);
8641 err = get_merge_branch_ref_name(&branch_refname, worktree);
8645 err = get_merge_commit_ref_name(&commit_refname, worktree);
8649 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8653 if (!got_ref_is_symbolic(branch_ref)) {
8654 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8655 "%s is not a symbolic reference",
8656 got_ref_get_name(branch_ref));
8659 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8660 if (*branch_name == NULL) {
8661 err = got_error_from_errno("strdup");
8665 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8669 err = got_ref_resolve(branch_tip, repo, commit_ref);
8673 free(commit_refname);
8674 free(branch_refname);
8675 free(fileindex_path);
8677 got_ref_close(commit_ref);
8679 got_ref_close(branch_ref);
8683 *branch_name = NULL;
8688 got_fileindex_free(*fileindex);
8691 lock_worktree(worktree, LOCK_SH);
8696 const struct got_error *
8697 got_worktree_merge_abort(struct got_worktree *worktree,
8698 struct got_fileindex *fileindex, struct got_repository *repo,
8699 got_worktree_checkout_cb progress_cb, void *progress_arg)
8701 const struct got_error *err, *unlockerr, *sync_err;
8702 struct got_commit_object *commit = NULL;
8703 char *fileindex_path = NULL;
8704 struct revert_file_args rfa;
8705 char *commit_ref_name = NULL;
8706 struct got_reference *commit_ref = NULL;
8707 struct got_object_id *merged_commit_id = NULL;
8708 struct got_object_id *tree_id = NULL;
8709 struct got_pathlist_head added_paths;
8711 TAILQ_INIT(&added_paths);
8713 err = get_merge_commit_ref_name(&commit_ref_name, worktree);
8717 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8721 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8726 * Determine which files in added status can be safely removed
8727 * from disk while reverting changes in the work tree.
8728 * We want to avoid deleting unrelated files which were added by
8729 * the user for conflict resolution purposes.
8731 err = get_paths_added_between_commits(&added_paths,
8732 got_worktree_get_base_commit_id(worktree), merged_commit_id,
8733 got_worktree_get_path_prefix(worktree), repo);
8738 err = got_object_open_as_commit(&commit, repo,
8739 worktree->base_commit_id);
8743 err = got_object_id_by_path(&tree_id, repo, commit,
8744 worktree->path_prefix);
8748 err = delete_merge_refs(worktree, repo);
8752 err = get_fileindex_path(&fileindex_path, worktree);
8756 rfa.worktree = worktree;
8757 rfa.fileindex = fileindex;
8758 rfa.progress_cb = progress_cb;
8759 rfa.progress_arg = progress_arg;
8760 rfa.patch_cb = NULL;
8761 rfa.patch_arg = NULL;
8763 rfa.unlink_added_files = 1;
8764 rfa.added_files_to_unlink = &added_paths;
8765 err = worktree_status(worktree, "", fileindex, repo,
8766 revert_file, &rfa, NULL, NULL, 1, 0);
8770 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8771 repo, progress_cb, progress_arg, NULL, NULL);
8773 sync_err = sync_fileindex(fileindex, fileindex_path);
8774 if (sync_err && err == NULL)
8778 free(merged_commit_id);
8780 got_object_commit_close(commit);
8782 got_fileindex_free(fileindex);
8783 free(fileindex_path);
8785 got_ref_close(commit_ref);
8786 free(commit_ref_name);
8788 unlockerr = lock_worktree(worktree, LOCK_SH);
8789 if (unlockerr && err == NULL)
8794 struct check_stage_ok_arg {
8795 struct got_object_id *head_commit_id;
8796 struct got_worktree *worktree;
8797 struct got_fileindex *fileindex;
8798 struct got_repository *repo;
8802 static const struct got_error *
8803 check_stage_ok(void *arg, unsigned char status,
8804 unsigned char staged_status, const char *relpath,
8805 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8806 struct got_object_id *commit_id, int dirfd, const char *de_name)
8808 struct check_stage_ok_arg *a = arg;
8809 const struct got_error *err = NULL;
8810 struct got_fileindex_entry *ie;
8811 struct got_object_id base_commit_id;
8812 struct got_object_id *base_commit_idp = NULL;
8813 char *in_repo_path = NULL, *p;
8815 if (status == GOT_STATUS_UNVERSIONED ||
8816 status == GOT_STATUS_NO_CHANGE)
8818 if (status == GOT_STATUS_NONEXISTENT)
8819 return got_error_set_errno(ENOENT, relpath);
8821 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8823 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8825 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8826 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8828 return got_error_from_errno("asprintf");
8830 if (got_fileindex_entry_has_commit(ie)) {
8831 base_commit_idp = got_fileindex_entry_get_commit_id(
8832 &base_commit_id, ie);
8835 if (status == GOT_STATUS_CONFLICT) {
8836 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8838 } else if (status != GOT_STATUS_ADD &&
8839 status != GOT_STATUS_MODIFY &&
8840 status != GOT_STATUS_DELETE) {
8841 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8845 a->have_changes = 1;
8850 err = check_out_of_date(p, status, staged_status,
8851 blob_id, base_commit_idp, a->head_commit_id, a->repo,
8852 GOT_ERR_STAGE_OUT_OF_DATE);
8858 struct stage_path_arg {
8859 struct got_worktree *worktree;
8860 struct got_fileindex *fileindex;
8861 struct got_repository *repo;
8862 got_worktree_status_cb status_cb;
8864 got_worktree_patch_cb patch_cb;
8866 int staged_something;
8867 int allow_bad_symlinks;
8870 static const struct got_error *
8871 stage_path(void *arg, unsigned char status,
8872 unsigned char staged_status, const char *relpath,
8873 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8874 struct got_object_id *commit_id, int dirfd, const char *de_name)
8876 struct stage_path_arg *a = arg;
8877 const struct got_error *err = NULL;
8878 struct got_fileindex_entry *ie;
8879 char *ondisk_path = NULL, *path_content = NULL;
8881 struct got_object_id *new_staged_blob_id = NULL;
8884 if (status == GOT_STATUS_UNVERSIONED)
8887 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8889 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8891 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8893 return got_error_from_errno("asprintf");
8896 case GOT_STATUS_ADD:
8897 case GOT_STATUS_MODIFY:
8898 /* XXX could sb.st_mode be passed in by our caller? */
8899 if (lstat(ondisk_path, &sb) == -1) {
8900 err = got_error_from_errno2("lstat", ondisk_path);
8904 if (status == GOT_STATUS_ADD) {
8905 int choice = GOT_PATCH_CHOICE_NONE;
8906 err = (*a->patch_cb)(&choice, a->patch_arg,
8907 status, ie->path, NULL, 1, 1);
8910 if (choice != GOT_PATCH_CHOICE_YES)
8913 err = create_patched_content(&path_content, 0,
8914 staged_blob_id ? staged_blob_id : blob_id,
8915 ondisk_path, dirfd, de_name, ie->path,
8916 a->repo, a->patch_cb, a->patch_arg);
8917 if (err || path_content == NULL)
8921 err = got_object_blob_create(&new_staged_blob_id,
8922 path_content ? path_content : ondisk_path, a->repo);
8925 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8926 SHA1_DIGEST_LENGTH);
8927 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8928 stage = GOT_FILEIDX_STAGE_ADD;
8930 stage = GOT_FILEIDX_STAGE_MODIFY;
8931 got_fileindex_entry_stage_set(ie, stage);
8932 if (S_ISLNK(sb.st_mode)) {
8933 int is_bad_symlink = 0;
8934 if (!a->allow_bad_symlinks) {
8935 char target_path[PATH_MAX];
8937 target_len = readlink(ondisk_path, target_path,
8938 sizeof(target_path));
8939 if (target_len == -1) {
8940 err = got_error_from_errno2("readlink",
8944 err = is_bad_symlink_target(&is_bad_symlink,
8945 target_path, target_len, ondisk_path,
8946 a->worktree->root_path);
8949 if (is_bad_symlink) {
8950 err = got_error_path(ondisk_path,
8951 GOT_ERR_BAD_SYMLINK);
8956 got_fileindex_entry_staged_filetype_set(ie,
8957 GOT_FILEIDX_MODE_BAD_SYMLINK);
8959 got_fileindex_entry_staged_filetype_set(ie,
8960 GOT_FILEIDX_MODE_SYMLINK);
8962 got_fileindex_entry_staged_filetype_set(ie,
8963 GOT_FILEIDX_MODE_REGULAR_FILE);
8965 a->staged_something = 1;
8966 if (a->status_cb == NULL)
8968 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8969 get_staged_status(ie), relpath, blob_id,
8970 new_staged_blob_id, NULL, dirfd, de_name);
8974 * When staging the reverse of the staged diff,
8975 * implicitly unstage the file.
8977 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8978 sizeof(ie->blob_sha1)) == 0) {
8979 got_fileindex_entry_stage_set(ie,
8980 GOT_FILEIDX_STAGE_NONE);
8983 case GOT_STATUS_DELETE:
8984 if (staged_status == GOT_STATUS_DELETE)
8987 int choice = GOT_PATCH_CHOICE_NONE;
8988 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8989 ie->path, NULL, 1, 1);
8992 if (choice == GOT_PATCH_CHOICE_NO)
8994 if (choice != GOT_PATCH_CHOICE_YES) {
8995 err = got_error(GOT_ERR_PATCH_CHOICE);
8999 stage = GOT_FILEIDX_STAGE_DELETE;
9000 got_fileindex_entry_stage_set(ie, stage);
9001 a->staged_something = 1;
9002 if (a->status_cb == NULL)
9004 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
9005 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
9008 case GOT_STATUS_NO_CHANGE:
9010 case GOT_STATUS_CONFLICT:
9011 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
9013 case GOT_STATUS_NONEXISTENT:
9014 err = got_error_set_errno(ENOENT, relpath);
9017 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
9021 if (path_content && unlink(path_content) == -1 && err == NULL)
9022 err = got_error_from_errno2("unlink", path_content);
9025 free(new_staged_blob_id);
9029 const struct got_error *
9030 got_worktree_stage(struct got_worktree *worktree,
9031 struct got_pathlist_head *paths,
9032 got_worktree_status_cb status_cb, void *status_arg,
9033 got_worktree_patch_cb patch_cb, void *patch_arg,
9034 int allow_bad_symlinks, struct got_repository *repo)
9036 const struct got_error *err = NULL, *sync_err, *unlockerr;
9037 struct got_pathlist_entry *pe;
9038 struct got_fileindex *fileindex = NULL;
9039 char *fileindex_path = NULL;
9040 struct got_reference *head_ref = NULL;
9041 struct got_object_id *head_commit_id = NULL;
9042 struct check_stage_ok_arg oka;
9043 struct stage_path_arg spa;
9045 err = lock_worktree(worktree, LOCK_EX);
9049 err = got_ref_open(&head_ref, repo,
9050 got_worktree_get_head_ref_name(worktree), 0);
9053 err = got_ref_resolve(&head_commit_id, repo, head_ref);
9056 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9060 /* Check pre-conditions before staging anything. */
9061 oka.head_commit_id = head_commit_id;
9062 oka.worktree = worktree;
9063 oka.fileindex = fileindex;
9065 oka.have_changes = 0;
9066 TAILQ_FOREACH(pe, paths, entry) {
9067 err = worktree_status(worktree, pe->path, fileindex, repo,
9068 check_stage_ok, &oka, NULL, NULL, 1, 0);
9072 if (!oka.have_changes) {
9073 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9077 spa.worktree = worktree;
9078 spa.fileindex = fileindex;
9080 spa.patch_cb = patch_cb;
9081 spa.patch_arg = patch_arg;
9082 spa.status_cb = status_cb;
9083 spa.status_arg = status_arg;
9084 spa.staged_something = 0;
9085 spa.allow_bad_symlinks = allow_bad_symlinks;
9086 TAILQ_FOREACH(pe, paths, entry) {
9087 err = worktree_status(worktree, pe->path, fileindex, repo,
9088 stage_path, &spa, NULL, NULL, 1, 0);
9092 if (!spa.staged_something) {
9093 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9097 sync_err = sync_fileindex(fileindex, fileindex_path);
9098 if (sync_err && err == NULL)
9102 got_ref_close(head_ref);
9103 free(head_commit_id);
9104 free(fileindex_path);
9106 got_fileindex_free(fileindex);
9107 unlockerr = lock_worktree(worktree, LOCK_SH);
9108 if (unlockerr && err == NULL)
9113 struct unstage_path_arg {
9114 struct got_worktree *worktree;
9115 struct got_fileindex *fileindex;
9116 struct got_repository *repo;
9117 got_worktree_checkout_cb progress_cb;
9119 got_worktree_patch_cb patch_cb;
9123 static const struct got_error *
9124 create_unstaged_content(char **path_unstaged_content,
9125 char **path_new_staged_content, struct got_object_id *blob_id,
9126 struct got_object_id *staged_blob_id, const char *relpath,
9127 struct got_repository *repo,
9128 got_worktree_patch_cb patch_cb, void *patch_arg)
9130 const struct got_error *err, *free_err;
9131 struct got_blob_object *blob = NULL, *staged_blob = NULL;
9132 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
9133 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
9134 struct got_diffreg_result *diffreg_result = NULL;
9135 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
9136 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
9137 int fd1 = -1, fd2 = -1;
9139 *path_unstaged_content = NULL;
9140 *path_new_staged_content = NULL;
9142 err = got_object_id_str(&label1, blob_id);
9146 fd1 = got_opentempfd();
9148 err = got_error_from_errno("got_opentempfd");
9151 fd2 = got_opentempfd();
9153 err = got_error_from_errno("got_opentempfd");
9157 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
9161 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
9165 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
9169 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
9174 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
9178 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
9182 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
9183 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
9187 err = got_opentemp_named(path_unstaged_content, &outfile,
9188 "got-unstaged-content", "");
9191 err = got_opentemp_named(path_new_staged_content, &rejectfile,
9192 "got-new-staged-content", "");
9196 if (fseek(f1, 0L, SEEK_SET) == -1) {
9197 err = got_ferror(f1, GOT_ERR_IO);
9200 if (fseek(f2, 0L, SEEK_SET) == -1) {
9201 err = got_ferror(f2, GOT_ERR_IO);
9204 /* Count the number of actual changes in the diff result. */
9205 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9206 struct diff_chunk_context cc = {};
9207 diff_chunk_context_load_change(&cc, &nchunks_used,
9208 diffreg_result->result, n, 0);
9211 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9213 err = apply_or_reject_change(&choice, &nchunks_used,
9214 diffreg_result->result, n, relpath, f1, f2,
9215 &line_cur1, &line_cur2,
9216 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
9219 if (choice == GOT_PATCH_CHOICE_YES)
9222 have_rejected_content = 1;
9223 if (choice == GOT_PATCH_CHOICE_QUIT)
9226 if (have_content || have_rejected_content)
9227 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
9228 outfile, rejectfile);
9231 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9232 err = got_error_from_errno("close");
9234 got_object_blob_close(blob);
9235 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9236 err = got_error_from_errno("close");
9238 got_object_blob_close(staged_blob);
9239 free_err = got_diffreg_result_free(diffreg_result);
9240 if (free_err && err == NULL)
9242 if (f1 && fclose(f1) == EOF && err == NULL)
9243 err = got_error_from_errno2("fclose", path1);
9244 if (f2 && fclose(f2) == EOF && err == NULL)
9245 err = got_error_from_errno2("fclose", path2);
9246 if (outfile && fclose(outfile) == EOF && err == NULL)
9247 err = got_error_from_errno2("fclose", *path_unstaged_content);
9248 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
9249 err = got_error_from_errno2("fclose", *path_new_staged_content);
9250 if (path1 && unlink(path1) == -1 && err == NULL)
9251 err = got_error_from_errno2("unlink", path1);
9252 if (path2 && unlink(path2) == -1 && err == NULL)
9253 err = got_error_from_errno2("unlink", path2);
9254 if (err || !have_content) {
9255 if (*path_unstaged_content &&
9256 unlink(*path_unstaged_content) == -1 && err == NULL)
9257 err = got_error_from_errno2("unlink",
9258 *path_unstaged_content);
9259 free(*path_unstaged_content);
9260 *path_unstaged_content = NULL;
9262 if (err || !have_content || !have_rejected_content) {
9263 if (*path_new_staged_content &&
9264 unlink(*path_new_staged_content) == -1 && err == NULL)
9265 err = got_error_from_errno2("unlink",
9266 *path_new_staged_content);
9267 free(*path_new_staged_content);
9268 *path_new_staged_content = NULL;
9275 static const struct got_error *
9276 unstage_hunks(struct got_object_id *staged_blob_id,
9277 struct got_blob_object *blob_base,
9278 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
9279 const char *ondisk_path, const char *label_orig,
9280 struct got_worktree *worktree, struct got_repository *repo,
9281 got_worktree_patch_cb patch_cb, void *patch_arg,
9282 got_worktree_checkout_cb progress_cb, void *progress_arg)
9284 const struct got_error *err = NULL;
9285 char *path_unstaged_content = NULL;
9286 char *path_new_staged_content = NULL;
9287 char *parent = NULL, *base_path = NULL;
9288 char *blob_base_path = NULL;
9289 struct got_object_id *new_staged_blob_id = NULL;
9290 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
9293 err = create_unstaged_content(&path_unstaged_content,
9294 &path_new_staged_content, blob_id, staged_blob_id,
9295 ie->path, repo, patch_cb, patch_arg);
9299 if (path_unstaged_content == NULL)
9302 if (path_new_staged_content) {
9303 err = got_object_blob_create(&new_staged_blob_id,
9304 path_new_staged_content, repo);
9309 f = fopen(path_unstaged_content, "re");
9311 err = got_error_from_errno2("fopen",
9312 path_unstaged_content);
9315 if (fstat(fileno(f), &sb) == -1) {
9316 err = got_error_from_errno2("fstat", path_unstaged_content);
9319 if (got_fileindex_entry_staged_filetype_get(ie) ==
9320 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
9321 char link_target[PATH_MAX];
9323 r = fread(link_target, 1, sizeof(link_target), f);
9324 if (r == 0 && ferror(f)) {
9325 err = got_error_from_errno("fread");
9328 if (r >= sizeof(link_target)) { /* should not happen */
9329 err = got_error(GOT_ERR_NO_SPACE);
9332 link_target[r] = '\0';
9333 err = merge_symlink(worktree, blob_base,
9334 ondisk_path, ie->path, label_orig, link_target,
9335 worktree->base_commit_id, repo, progress_cb,
9338 int local_changes_subsumed;
9340 err = got_path_dirname(&parent, ondisk_path);
9344 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
9346 err = got_error_from_errno("asprintf");
9351 err = got_opentemp_named(&blob_base_path, &f_base,
9355 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
9361 * In order the run a 3-way merge with a symlink we copy the symlink's
9362 * target path into a temporary file and use that file with diff3.
9364 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9365 err = dump_symlink_target_path_to_file(&f_deriv2,
9371 fd = open(ondisk_path,
9372 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
9374 err = got_error_from_errno2("open", ondisk_path);
9377 f_deriv2 = fdopen(fd, "r");
9378 if (f_deriv2 == NULL) {
9379 err = got_error_from_errno2("fdopen", ondisk_path);
9385 err = merge_file(&local_changes_subsumed, worktree,
9386 f_base, f, f_deriv2, ondisk_path, ie->path,
9387 got_fileindex_perms_to_st(ie),
9388 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
9389 repo, progress_cb, progress_arg);
9394 if (new_staged_blob_id) {
9395 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9396 SHA1_DIGEST_LENGTH);
9398 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9399 got_fileindex_entry_staged_filetype_set(ie, 0);
9402 free(new_staged_blob_id);
9403 if (path_unstaged_content &&
9404 unlink(path_unstaged_content) == -1 && err == NULL)
9405 err = got_error_from_errno2("unlink", path_unstaged_content);
9406 if (path_new_staged_content &&
9407 unlink(path_new_staged_content) == -1 && err == NULL)
9408 err = got_error_from_errno2("unlink", path_new_staged_content);
9409 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
9410 err = got_error_from_errno2("unlink", blob_base_path);
9411 if (f_base && fclose(f_base) == EOF && err == NULL)
9412 err = got_error_from_errno2("fclose", path_unstaged_content);
9413 if (f && fclose(f) == EOF && err == NULL)
9414 err = got_error_from_errno2("fclose", path_unstaged_content);
9415 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
9416 err = got_error_from_errno2("fclose", ondisk_path);
9417 free(path_unstaged_content);
9418 free(path_new_staged_content);
9419 free(blob_base_path);
9425 static const struct got_error *
9426 unstage_path(void *arg, unsigned char status,
9427 unsigned char staged_status, const char *relpath,
9428 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9429 struct got_object_id *commit_id, int dirfd, const char *de_name)
9431 const struct got_error *err = NULL;
9432 struct unstage_path_arg *a = arg;
9433 struct got_fileindex_entry *ie;
9434 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
9435 char *ondisk_path = NULL;
9436 char *id_str = NULL, *label_orig = NULL;
9437 int local_changes_subsumed;
9439 int fd1 = -1, fd2 = -1;
9441 if (staged_status != GOT_STATUS_ADD &&
9442 staged_status != GOT_STATUS_MODIFY &&
9443 staged_status != GOT_STATUS_DELETE)
9446 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9448 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9450 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
9452 return got_error_from_errno("asprintf");
9454 err = got_object_id_str(&id_str,
9455 commit_id ? commit_id : a->worktree->base_commit_id);
9458 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9460 err = got_error_from_errno("asprintf");
9464 fd1 = got_opentempfd();
9466 err = got_error_from_errno("got_opentempfd");
9469 fd2 = got_opentempfd();
9471 err = got_error_from_errno("got_opentempfd");
9475 switch (staged_status) {
9476 case GOT_STATUS_MODIFY:
9477 err = got_object_open_as_blob(&blob_base, a->repo,
9478 blob_id, 8192, fd1);
9482 case GOT_STATUS_ADD:
9484 if (staged_status == GOT_STATUS_ADD) {
9485 int choice = GOT_PATCH_CHOICE_NONE;
9486 err = (*a->patch_cb)(&choice, a->patch_arg,
9487 staged_status, ie->path, NULL, 1, 1);
9490 if (choice != GOT_PATCH_CHOICE_YES)
9493 err = unstage_hunks(staged_blob_id,
9494 blob_base, blob_id, ie, ondisk_path,
9495 label_orig, a->worktree, a->repo,
9496 a->patch_cb, a->patch_arg,
9497 a->progress_cb, a->progress_arg);
9498 break; /* Done with this file. */
9501 err = got_object_open_as_blob(&blob_staged, a->repo,
9502 staged_blob_id, 8192, fd2);
9505 switch (got_fileindex_entry_staged_filetype_get(ie)) {
9506 case GOT_FILEIDX_MODE_BAD_SYMLINK:
9507 case GOT_FILEIDX_MODE_REGULAR_FILE:
9508 err = merge_blob(&local_changes_subsumed, a->worktree,
9509 blob_base, ondisk_path, relpath,
9510 got_fileindex_perms_to_st(ie), label_orig,
9511 blob_staged, commit_id ? commit_id :
9512 a->worktree->base_commit_id, a->repo,
9513 a->progress_cb, a->progress_arg);
9515 case GOT_FILEIDX_MODE_SYMLINK:
9516 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9517 char *staged_target;
9518 err = got_object_blob_read_to_str(
9519 &staged_target, blob_staged);
9522 err = merge_symlink(a->worktree, blob_base,
9523 ondisk_path, relpath, label_orig,
9524 staged_target, commit_id ? commit_id :
9525 a->worktree->base_commit_id,
9526 a->repo, a->progress_cb, a->progress_arg);
9527 free(staged_target);
9529 err = merge_blob(&local_changes_subsumed,
9530 a->worktree, blob_base, ondisk_path,
9531 relpath, got_fileindex_perms_to_st(ie),
9532 label_orig, blob_staged,
9533 commit_id ? commit_id :
9534 a->worktree->base_commit_id, a->repo,
9535 a->progress_cb, a->progress_arg);
9539 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9543 got_fileindex_entry_stage_set(ie,
9544 GOT_FILEIDX_STAGE_NONE);
9545 got_fileindex_entry_staged_filetype_set(ie, 0);
9548 case GOT_STATUS_DELETE:
9550 int choice = GOT_PATCH_CHOICE_NONE;
9551 err = (*a->patch_cb)(&choice, a->patch_arg,
9552 staged_status, ie->path, NULL, 1, 1);
9555 if (choice == GOT_PATCH_CHOICE_NO)
9557 if (choice != GOT_PATCH_CHOICE_YES) {
9558 err = got_error(GOT_ERR_PATCH_CHOICE);
9562 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9563 got_fileindex_entry_staged_filetype_set(ie, 0);
9564 err = get_file_status(&status, &sb, ie, ondisk_path,
9565 dirfd, de_name, a->repo);
9568 err = (*a->progress_cb)(a->progress_arg, status, relpath);
9573 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9574 err = got_error_from_errno("close");
9576 got_object_blob_close(blob_base);
9577 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9578 err = got_error_from_errno("close");
9580 got_object_blob_close(blob_staged);
9586 const struct got_error *
9587 got_worktree_unstage(struct got_worktree *worktree,
9588 struct got_pathlist_head *paths,
9589 got_worktree_checkout_cb progress_cb, void *progress_arg,
9590 got_worktree_patch_cb patch_cb, void *patch_arg,
9591 struct got_repository *repo)
9593 const struct got_error *err = NULL, *sync_err, *unlockerr;
9594 struct got_pathlist_entry *pe;
9595 struct got_fileindex *fileindex = NULL;
9596 char *fileindex_path = NULL;
9597 struct unstage_path_arg upa;
9599 err = lock_worktree(worktree, LOCK_EX);
9603 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9607 upa.worktree = worktree;
9608 upa.fileindex = fileindex;
9610 upa.progress_cb = progress_cb;
9611 upa.progress_arg = progress_arg;
9612 upa.patch_cb = patch_cb;
9613 upa.patch_arg = patch_arg;
9614 TAILQ_FOREACH(pe, paths, entry) {
9615 err = worktree_status(worktree, pe->path, fileindex, repo,
9616 unstage_path, &upa, NULL, NULL, 1, 0);
9621 sync_err = sync_fileindex(fileindex, fileindex_path);
9622 if (sync_err && err == NULL)
9625 free(fileindex_path);
9627 got_fileindex_free(fileindex);
9628 unlockerr = lock_worktree(worktree, LOCK_SH);
9629 if (unlockerr && err == NULL)
9634 struct report_file_info_arg {
9635 struct got_worktree *worktree;
9636 got_worktree_path_info_cb info_cb;
9638 struct got_pathlist_head *paths;
9639 got_cancel_cb cancel_cb;
9643 static const struct got_error *
9644 report_file_info(void *arg, struct got_fileindex_entry *ie)
9646 struct report_file_info_arg *a = arg;
9647 struct got_pathlist_entry *pe;
9648 struct got_object_id blob_id, staged_blob_id, commit_id;
9649 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9650 struct got_object_id *commit_idp = NULL;
9653 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9654 return got_error(GOT_ERR_CANCELLED);
9656 TAILQ_FOREACH(pe, a->paths, entry) {
9657 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9658 got_path_is_child(ie->path, pe->path, pe->path_len))
9661 if (pe == NULL) /* not found */
9664 if (got_fileindex_entry_has_blob(ie))
9665 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9666 stage = got_fileindex_entry_stage_get(ie);
9667 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9668 stage == GOT_FILEIDX_STAGE_ADD) {
9669 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9670 &staged_blob_id, ie);
9673 if (got_fileindex_entry_has_commit(ie))
9674 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9676 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9677 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9680 const struct got_error *
9681 got_worktree_path_info(struct got_worktree *worktree,
9682 struct got_pathlist_head *paths,
9683 got_worktree_path_info_cb info_cb, void *info_arg,
9684 got_cancel_cb cancel_cb, void *cancel_arg)
9687 const struct got_error *err = NULL, *unlockerr;
9688 struct got_fileindex *fileindex = NULL;
9689 char *fileindex_path = NULL;
9690 struct report_file_info_arg arg;
9692 err = lock_worktree(worktree, LOCK_SH);
9696 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9700 arg.worktree = worktree;
9701 arg.info_cb = info_cb;
9702 arg.info_arg = info_arg;
9704 arg.cancel_cb = cancel_cb;
9705 arg.cancel_arg = cancel_arg;
9706 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9709 free(fileindex_path);
9711 got_fileindex_free(fileindex);
9712 unlockerr = lock_worktree(worktree, LOCK_UN);
9713 if (unlockerr && err == NULL)
9718 static const struct got_error *
9719 patch_check_path(const char *p, char **path, unsigned char *status,
9720 unsigned char *staged_status, struct got_fileindex *fileindex,
9721 struct got_worktree *worktree, struct got_repository *repo)
9723 const struct got_error *err;
9724 struct got_fileindex_entry *ie;
9726 char *ondisk_path = NULL;
9728 err = got_worktree_resolve_path(path, worktree, p);
9732 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9733 *path[0] ? "/" : "", *path) == -1)
9734 return got_error_from_errno("asprintf");
9736 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9738 *staged_status = get_staged_status(ie);
9739 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9744 *staged_status = GOT_STATUS_NO_CHANGE;
9745 *status = GOT_STATUS_UNVERSIONED;
9746 if (lstat(ondisk_path, &sb) == -1) {
9747 if (errno != ENOENT) {
9748 err = got_error_from_errno2("lstat",
9752 *status = GOT_STATUS_NONEXISTENT;
9761 static const struct got_error *
9762 patch_can_rm(const char *path, unsigned char status,
9763 unsigned char staged_status)
9765 if (status == GOT_STATUS_NONEXISTENT)
9766 return got_error_set_errno(ENOENT, path);
9767 if (status != GOT_STATUS_NO_CHANGE &&
9768 status != GOT_STATUS_ADD &&
9769 status != GOT_STATUS_MODIFY &&
9770 status != GOT_STATUS_MODE_CHANGE)
9771 return got_error_path(path, GOT_ERR_FILE_STATUS);
9772 if (staged_status == GOT_STATUS_DELETE)
9773 return got_error_path(path, GOT_ERR_FILE_STATUS);
9777 static const struct got_error *
9778 patch_can_add(const char *path, unsigned char status)
9780 if (status != GOT_STATUS_NONEXISTENT)
9781 return got_error_path(path, GOT_ERR_FILE_STATUS);
9785 static const struct got_error *
9786 patch_can_edit(const char *path, unsigned char status,
9787 unsigned char staged_status)
9789 if (status == GOT_STATUS_NONEXISTENT)
9790 return got_error_set_errno(ENOENT, path);
9791 if (status != GOT_STATUS_NO_CHANGE &&
9792 status != GOT_STATUS_ADD &&
9793 status != GOT_STATUS_MODIFY)
9794 return got_error_path(path, GOT_ERR_FILE_STATUS);
9795 if (staged_status == GOT_STATUS_DELETE)
9796 return got_error_path(path, GOT_ERR_FILE_STATUS);
9800 const struct got_error *
9801 got_worktree_patch_prepare(struct got_fileindex **fileindex,
9802 char **fileindex_path, struct got_worktree *worktree)
9804 return open_fileindex(fileindex, fileindex_path, worktree);
9807 const struct got_error *
9808 got_worktree_patch_check_path(const char *old, const char *new,
9809 char **oldpath, char **newpath, struct got_worktree *worktree,
9810 struct got_repository *repo, struct got_fileindex *fileindex)
9812 const struct got_error *err = NULL;
9813 int file_renamed = 0;
9814 unsigned char status_old, staged_status_old;
9815 unsigned char status_new, staged_status_new;
9820 err = patch_check_path(old != NULL ? old : new, oldpath,
9821 &status_old, &staged_status_old, fileindex, worktree, repo);
9825 err = patch_check_path(new != NULL ? new : old, newpath,
9826 &status_new, &staged_status_new, fileindex, worktree, repo);
9830 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9833 if (old != NULL && new == NULL)
9834 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9835 else if (file_renamed) {
9836 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9838 err = patch_can_add(*newpath, status_new);
9839 } else if (old == NULL)
9840 err = patch_can_add(*newpath, status_new);
9842 err = patch_can_edit(*newpath, status_new, staged_status_new);
9854 const struct got_error *
9855 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9856 struct got_worktree *worktree, struct got_fileindex *fileindex,
9857 got_worktree_checkout_cb progress_cb, void *progress_arg)
9859 struct schedule_addition_args saa;
9861 memset(&saa, 0, sizeof(saa));
9862 saa.worktree = worktree;
9863 saa.fileindex = fileindex;
9864 saa.progress_cb = progress_cb;
9865 saa.progress_arg = progress_arg;
9868 return worktree_status(worktree, path, fileindex, repo,
9869 schedule_addition, &saa, NULL, NULL, 1, 0);
9872 const struct got_error *
9873 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9874 struct got_worktree *worktree, struct got_fileindex *fileindex,
9875 got_worktree_delete_cb progress_cb, void *progress_arg)
9877 const struct got_error *err;
9878 struct schedule_deletion_args sda;
9879 char *ondisk_status_path;
9881 memset(&sda, 0, sizeof(sda));
9882 sda.worktree = worktree;
9883 sda.fileindex = fileindex;
9884 sda.progress_cb = progress_cb;
9885 sda.progress_arg = progress_arg;
9887 sda.delete_local_mods = 0;
9888 sda.keep_on_disk = 0;
9889 sda.ignore_missing_paths = 0;
9890 sda.status_codes = NULL;
9891 if (asprintf(&ondisk_status_path, "%s/%s",
9892 got_worktree_get_root_path(worktree), path) == -1)
9893 return got_error_from_errno("asprintf");
9894 sda.status_path = ondisk_status_path;
9895 sda.status_path_len = strlen(ondisk_status_path);
9897 err = worktree_status(worktree, path, fileindex, repo,
9898 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9899 free(ondisk_status_path);
9903 const struct got_error *
9904 got_worktree_patch_complete(struct got_fileindex *fileindex,
9905 const char *fileindex_path)
9907 const struct got_error *err = NULL;
9909 err = sync_fileindex(fileindex, fileindex_path);
9910 got_fileindex_free(fileindex);