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.
33 #include "got_compat.h"
35 #include "got_error.h"
36 #include "got_repository.h"
37 #include "got_reference.h"
38 #include "got_object.h"
40 #include "got_cancel.h"
41 #include "got_worktree.h"
42 #include "got_opentemp.h"
45 #include "got_lib_worktree.h"
46 #include "got_lib_sha1.h"
47 #include "got_lib_fileindex.h"
48 #include "got_lib_inflate.h"
49 #include "got_lib_delta.h"
50 #include "got_lib_object.h"
51 #include "got_lib_object_parse.h"
52 #include "got_lib_object_create.h"
53 #include "got_lib_object_idset.h"
54 #include "got_lib_diff.h"
55 #include "got_lib_gotconfig.h"
58 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 #define GOT_MERGE_LABEL_MERGED "merged change"
62 #define GOT_MERGE_LABEL_BASE "3-way merge base"
64 static const struct got_error *
65 create_meta_file(const char *path_got, const char *name, const char *content)
67 const struct got_error *err = NULL;
70 if (asprintf(&path, "%s/%s", path_got, name) == -1)
71 return got_error_from_errno("asprintf");
73 err = got_path_create_file(path, content);
78 static const struct got_error *
79 update_meta_file(const char *path_got, const char *name, const char *content)
81 const struct got_error *err = NULL;
86 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
87 err = got_error_from_errno("asprintf");
92 err = got_opentemp_named(&tmppath, &tmpfile, path);
97 int len = fprintf(tmpfile, "%s\n", content);
98 if (len != strlen(content) + 1) {
99 err = got_error_from_errno2("fprintf", tmppath);
104 if (rename(tmppath, path) != 0) {
105 err = got_error_from_errno3("rename", tmppath, path);
111 if (fclose(tmpfile) == EOF && err == NULL)
112 err = got_error_from_errno2("fclose", tmppath);
117 static const struct got_error *
118 write_head_ref(const char *path_got, struct got_reference *head_ref)
120 const struct got_error *err = NULL;
123 if (got_ref_is_symbolic(head_ref)) {
124 refstr = got_ref_to_str(head_ref);
126 return got_error_from_errno("got_ref_to_str");
128 refstr = strdup(got_ref_get_name(head_ref));
130 return got_error_from_errno("strdup");
132 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
137 const struct got_error *
138 got_worktree_init(const char *path, struct got_reference *head_ref,
139 const char *prefix, struct got_repository *repo)
141 const struct got_error *err = NULL;
142 struct got_object_id *commit_id = NULL;
144 uint32_t uuid_status;
146 char *path_got = NULL;
147 char *formatstr = NULL;
148 char *absprefix = NULL;
149 char *basestr = NULL;
150 char *uuidstr = NULL;
152 if (strcmp(path, got_repo_get_path(repo)) == 0) {
153 err = got_error(GOT_ERR_WORKTREE_REPO);
157 err = got_ref_resolve(&commit_id, repo, head_ref);
160 err = got_object_get_type(&obj_type, repo, commit_id);
163 if (obj_type != GOT_OBJ_TYPE_COMMIT)
164 return got_error(GOT_ERR_OBJ_TYPE);
166 if (!got_path_is_absolute(prefix)) {
167 if (asprintf(&absprefix, "/%s", prefix) == -1)
168 return got_error_from_errno("asprintf");
171 /* Create top-level directory (may already exist). */
172 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
173 err = got_error_from_errno2("mkdir", path);
177 /* Create .got directory (may already exist). */
178 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
179 err = got_error_from_errno("asprintf");
182 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
183 err = got_error_from_errno2("mkdir", path_got);
187 /* Create an empty lock file. */
188 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
192 /* Create an empty file index. */
193 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
197 /* Write the HEAD reference. */
198 err = write_head_ref(path_got, head_ref);
202 /* Record our base commit. */
203 err = got_object_id_str(&basestr, commit_id);
206 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
210 /* Store path to repository. */
211 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
212 got_repo_get_path(repo));
216 /* Store in-repository path prefix. */
217 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
218 absprefix ? absprefix : prefix);
223 uuid_create(&uuid, &uuid_status);
224 if (uuid_status != uuid_s_ok) {
225 err = got_error_uuid(uuid_status, "uuid_create");
228 uuid_to_string(&uuid, &uuidstr, &uuid_status);
229 if (uuid_status != uuid_s_ok) {
230 err = got_error_uuid(uuid_status, "uuid_to_string");
233 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
237 /* Stamp work tree with format file. */
238 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
239 err = got_error_from_errno("asprintf");
242 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
256 const struct got_error *
257 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
258 const char *path_prefix)
260 char *absprefix = NULL;
262 if (!got_path_is_absolute(path_prefix)) {
263 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
264 return got_error_from_errno("asprintf");
266 *match = (strcmp(absprefix ? absprefix : path_prefix,
267 worktree->path_prefix) == 0);
273 got_worktree_get_head_ref_name(struct got_worktree *worktree)
275 return worktree->head_ref_name;
278 const struct got_error *
279 got_worktree_set_head_ref(struct got_worktree *worktree,
280 struct got_reference *head_ref)
282 const struct got_error *err = NULL;
283 char *path_got = NULL, *head_ref_name = NULL;
285 if (asprintf(&path_got, "%s/%s", worktree->root_path,
286 GOT_WORKTREE_GOT_DIR) == -1) {
287 err = got_error_from_errno("asprintf");
292 head_ref_name = strdup(got_ref_get_name(head_ref));
293 if (head_ref_name == NULL) {
294 err = got_error_from_errno("strdup");
298 err = write_head_ref(path_got, head_ref);
302 free(worktree->head_ref_name);
303 worktree->head_ref_name = head_ref_name;
311 struct got_object_id *
312 got_worktree_get_base_commit_id(struct got_worktree *worktree)
314 return worktree->base_commit_id;
317 const struct got_error *
318 got_worktree_set_base_commit_id(struct got_worktree *worktree,
319 struct got_repository *repo, struct got_object_id *commit_id)
321 const struct got_error *err;
322 struct got_object *obj = NULL;
324 char *path_got = NULL;
326 if (asprintf(&path_got, "%s/%s", worktree->root_path,
327 GOT_WORKTREE_GOT_DIR) == -1) {
328 err = got_error_from_errno("asprintf");
333 err = got_object_open(&obj, repo, commit_id);
337 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
338 err = got_error(GOT_ERR_OBJ_TYPE);
342 /* Record our base commit. */
343 err = got_object_id_str(&id_str, commit_id);
346 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
350 free(worktree->base_commit_id);
351 worktree->base_commit_id = got_object_id_dup(commit_id);
352 if (worktree->base_commit_id == NULL) {
353 err = got_error_from_errno("got_object_id_dup");
358 got_object_close(obj);
364 const struct got_gotconfig *
365 got_worktree_get_gotconfig(struct got_worktree *worktree)
367 return worktree->gotconfig;
370 static const struct got_error *
371 lock_worktree(struct got_worktree *worktree, int operation)
373 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
374 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
375 : got_error_from_errno2("flock",
376 got_worktree_get_root_path(worktree)));
380 static const struct got_error *
381 add_dir_on_disk(struct got_worktree *worktree, const char *path)
383 const struct got_error *err = NULL;
386 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
387 return got_error_from_errno("asprintf");
389 err = got_path_mkdir(abspath);
390 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
393 if (lstat(abspath, &sb) == -1) {
394 err = got_error_from_errno2("lstat", abspath);
395 } else if (!S_ISDIR(sb.st_mode)) {
396 /* TODO directory is obstructed; do something */
397 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
404 static const struct got_error *
405 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
407 const struct got_error *err = NULL;
410 size_t flen1 = 0, flen2 = 0;
415 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
416 if (flen1 == 0 && ferror(f1)) {
417 err = got_error_from_errno("fread");
420 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
421 if (flen2 == 0 && ferror(f2)) {
422 err = got_error_from_errno("fread");
429 } else if (flen2 == 0) {
433 } else if (flen1 == flen2) {
434 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
447 static const struct got_error *
448 check_files_equal(int *same, FILE *f1, FILE *f2)
455 if (fstat(fileno(f1), &sb) != 0)
456 return got_error_from_errno("fstat");
459 if (fstat(fileno(f2), &sb) != 0)
460 return got_error_from_errno("fstat");
463 if (size1 != size2) {
468 if (fseek(f1, 0L, SEEK_SET) == -1)
469 return got_ferror(f1, GOT_ERR_IO);
470 if (fseek(f2, 0L, SEEK_SET) == -1)
471 return got_ferror(f2, GOT_ERR_IO);
473 return check_file_contents_equal(same, f1, f2);
476 static const struct got_error *
477 copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
485 if (fseek(f, 0L, SEEK_SET) == -1)
486 return got_ferror(f, GOT_ERR_IO);
489 flen = fread(fbuf, 1, sizeof(fbuf), f);
492 return got_error_from_errno("fread");
496 outlen = write(outfd, fbuf, flen);
498 return got_error_from_errno("write");
500 return got_error(GOT_ERR_IO);
507 static const struct got_error *
508 merge_binary_file(int *overlapcnt, int merged_fd,
509 FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
510 const char *label_deriv, const char *label_orig, const char *label_deriv2,
511 const char *ondisk_path)
513 const struct got_error *err = NULL;
514 int same_content, changed_deriv, changed_deriv2;
515 int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
516 off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
517 char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
518 char *base_path_orig = NULL, *base_path_deriv = NULL;
519 char *base_path_deriv2 = NULL;
523 err = check_files_equal(&same_content, f_deriv, f_deriv2);
528 return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
530 err = check_files_equal(&same_content, f_deriv, f_orig);
533 changed_deriv = !same_content;
534 err = check_files_equal(&same_content, f_deriv2, f_orig);
537 changed_deriv2 = !same_content;
539 if (changed_deriv && changed_deriv2) {
541 if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
542 err = got_error_from_errno("asprintf");
545 if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
546 err = got_error_from_errno("asprintf");
549 if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
550 err = got_error_from_errno("asprintf");
553 err = got_opentemp_named_fd(&path_orig, &fd_orig,
557 err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
561 err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
565 err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
568 err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
571 err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
574 if (dprintf(merged_fd, "Binary files differ and cannot be "
575 "merged automatically:\n") < 0) {
576 err = got_error_from_errno("dprintf");
579 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
580 GOT_DIFF_CONFLICT_MARKER_BEGIN,
581 label_deriv ? " " : "",
582 label_deriv ? label_deriv : "",
584 err = got_error_from_errno("dprintf");
588 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
589 GOT_DIFF_CONFLICT_MARKER_ORIG,
590 label_orig ? " " : "",
591 label_orig ? label_orig : "",
593 err = got_error_from_errno("dprintf");
597 if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
598 GOT_DIFF_CONFLICT_MARKER_SEP,
600 GOT_DIFF_CONFLICT_MARKER_END,
601 label_deriv2 ? " " : "",
602 label_deriv2 ? label_deriv2 : "") < 0) {
603 err = got_error_from_errno("dprintf");
606 } else if (changed_deriv)
607 err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
608 else if (changed_deriv2)
609 err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
611 if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
613 err = got_error_from_errno2("unlink", path_orig);
614 if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
615 err = got_error_from_errno2("close", path_orig);
616 if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
617 err = got_error_from_errno2("close", path_deriv);
618 if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
619 err = got_error_from_errno2("close", path_deriv2);
623 free(base_path_orig);
624 free(base_path_deriv);
625 free(base_path_deriv2);
630 * Perform a 3-way merge where the file f_orig acts as the common
631 * ancestor, the file f_deriv acts as the first derived version,
632 * and the file f_deriv2 acts as the second derived version.
633 * The merge result will be written to a new file at ondisk_path; any
634 * existing file at this path will be replaced.
636 static const struct got_error *
637 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
638 FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
639 const char *path, uint16_t st_mode,
640 const char *label_orig, const char *label_deriv, const char *label_deriv2,
641 enum got_diff_algorithm diff_algo, struct got_repository *repo,
642 got_worktree_checkout_cb progress_cb, void *progress_arg)
644 const struct got_error *err = NULL;
646 FILE *f_merged = NULL;
647 char *merged_path = NULL, *base_path = NULL;
651 *local_changes_subsumed = 0;
653 err = got_path_dirname(&parent, ondisk_path);
657 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
658 err = got_error_from_errno("asprintf");
662 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
666 err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
667 f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
669 if (err->code != GOT_ERR_FILE_BINARY)
671 err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
672 f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
678 err = (*progress_cb)(progress_arg,
679 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
683 if (fsync(merged_fd) != 0) {
684 err = got_error_from_errno("fsync");
688 f_merged = fdopen(merged_fd, "r");
689 if (f_merged == NULL) {
690 err = got_error_from_errno("fdopen");
695 /* Check if a clean merge has subsumed all local changes. */
696 if (overlapcnt == 0) {
697 err = check_files_equal(local_changes_subsumed, f_deriv,
703 if (fchmod(fileno(f_merged), st_mode) != 0) {
704 err = got_error_from_errno2("fchmod", merged_path);
708 if (rename(merged_path, ondisk_path) != 0) {
709 err = got_error_from_errno3("rename", merged_path,
718 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
719 err = got_error_from_errno("close");
720 if (f_merged && fclose(f_merged) == EOF && err == NULL)
721 err = got_error_from_errno("fclose");
728 static const struct got_error *
729 update_symlink(const char *ondisk_path, const char *target_path,
732 /* This is not atomic but matches what 'ln -sf' does. */
733 if (unlink(ondisk_path) == -1)
734 return got_error_from_errno2("unlink", ondisk_path);
735 if (symlink(target_path, ondisk_path) == -1)
736 return got_error_from_errno3("symlink", target_path,
742 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
743 * in the work tree with a file that contains conflict markers and the
744 * conflicting target paths of the original version, a "derived version"
745 * of a symlink from an incoming change, and a local version of the symlink.
747 * The original versions's target path can be NULL if it is not available,
748 * such as if both derived versions added a new symlink at the same path.
750 * The incoming derived symlink target is NULL in case the incoming change
751 * has deleted this symlink.
753 static const struct got_error *
754 install_symlink_conflict(const char *deriv_target,
755 struct got_object_id *deriv_base_commit_id, const char *orig_target,
756 const char *label_orig, const char *local_target, const char *ondisk_path)
758 const struct got_error *err;
759 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
762 err = got_object_id_str(&id_str, deriv_base_commit_id);
764 return got_error_from_errno("asprintf");
766 if (asprintf(&label_deriv, "%s: commit %s",
767 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
768 err = got_error_from_errno("asprintf");
772 err = got_opentemp_named(&path, &f, "got-symlink-conflict");
776 if (fchmod(fileno(f), GOT_DEFAULT_FILE_MODE) == -1) {
777 err = got_error_from_errno2("fchmod", path);
781 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
782 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
783 deriv_target ? deriv_target : "(symlink was deleted)",
784 orig_target ? label_orig : "",
785 orig_target ? "\n" : "",
786 orig_target ? orig_target : "",
787 orig_target ? "\n" : "",
788 GOT_DIFF_CONFLICT_MARKER_SEP,
789 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
790 err = got_error_from_errno2("fprintf", path);
794 if (unlink(ondisk_path) == -1) {
795 err = got_error_from_errno2("unlink", ondisk_path);
798 if (rename(path, ondisk_path) == -1) {
799 err = got_error_from_errno3("rename", path, ondisk_path);
803 if (f != NULL && fclose(f) == EOF && err == NULL)
804 err = got_error_from_errno2("fclose", path);
811 /* forward declaration */
812 static const struct got_error *
813 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
814 const char *, const char *, uint16_t, const char *,
815 struct got_blob_object *, struct got_object_id *,
816 struct got_repository *, got_worktree_checkout_cb, void *);
819 * Merge a symlink into the work tree, where blob_orig acts as the common
820 * ancestor, deriv_target is the link target of the first derived version,
821 * and the symlink on disk acts as the second derived version.
822 * Assume that contents of both blobs represent symlinks.
824 static const struct got_error *
825 merge_symlink(struct got_worktree *worktree,
826 struct got_blob_object *blob_orig, const char *ondisk_path,
827 const char *path, const char *label_orig, const char *deriv_target,
828 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
829 got_worktree_checkout_cb progress_cb, void *progress_arg)
831 const struct got_error *err = NULL;
832 char *ancestor_target = NULL;
834 ssize_t ondisk_len, deriv_len;
835 char ondisk_target[PATH_MAX];
836 int have_local_change = 0;
837 int have_incoming_change = 0;
839 if (lstat(ondisk_path, &sb) == -1)
840 return got_error_from_errno2("lstat", ondisk_path);
842 ondisk_len = readlink(ondisk_path, ondisk_target,
843 sizeof(ondisk_target));
844 if (ondisk_len == -1) {
845 err = got_error_from_errno2("readlink",
849 ondisk_target[ondisk_len] = '\0';
852 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
857 if (ancestor_target == NULL ||
858 (ondisk_len != strlen(ancestor_target) ||
859 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
860 have_local_change = 1;
862 deriv_len = strlen(deriv_target);
863 if (ancestor_target == NULL ||
864 (deriv_len != strlen(ancestor_target) ||
865 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
866 have_incoming_change = 1;
868 if (!have_local_change && !have_incoming_change) {
869 if (ancestor_target) {
870 /* Both sides made the same change. */
871 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
873 } else if (deriv_len == ondisk_len &&
874 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
875 /* Both sides added the same symlink. */
876 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
879 /* Both sides added symlinks which don't match. */
880 err = install_symlink_conflict(deriv_target,
881 deriv_base_commit_id, ancestor_target,
882 label_orig, ondisk_target, ondisk_path);
885 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
888 } else if (!have_local_change && have_incoming_change) {
889 /* Apply the incoming change. */
890 err = update_symlink(ondisk_path, deriv_target,
891 strlen(deriv_target));
894 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
895 } else if (have_local_change && have_incoming_change) {
896 if (deriv_len == ondisk_len &&
897 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
898 /* Both sides made the same change. */
899 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
902 err = install_symlink_conflict(deriv_target,
903 deriv_base_commit_id, ancestor_target, label_orig,
904 ondisk_target, ondisk_path);
907 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
913 free(ancestor_target);
917 static const struct got_error *
918 dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
920 const struct got_error *err = NULL;
921 char target_path[PATH_MAX];
930 return got_error_from_errno("got_opentemp");
931 target_len = readlink(ondisk_path, target_path, sizeof(target_path));
932 if (target_len == -1) {
933 err = got_error_from_errno2("readlink", ondisk_path);
936 n = fwrite(target_path, 1, target_len, f);
937 if (n != target_len) {
938 err = got_ferror(f, GOT_ERR_IO);
941 if (fflush(f) == EOF) {
942 err = got_error_from_errno("fflush");
945 if (fseek(f, 0L, SEEK_SET) == -1) {
946 err = got_ferror(f, GOT_ERR_IO);
958 * Perform a 3-way merge where blob_orig acts as the common ancestor,
959 * blob_deriv acts as the first derived version, and the file on disk
960 * acts as the second derived version.
962 static const struct got_error *
963 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
964 struct got_blob_object *blob_orig, const char *ondisk_path,
965 const char *path, uint16_t st_mode, const char *label_orig,
966 struct got_blob_object *blob_deriv,
967 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
968 got_worktree_checkout_cb progress_cb, void *progress_arg)
970 const struct got_error *err = NULL;
971 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
972 char *blob_orig_path = NULL;
973 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
974 char *label_deriv = NULL, *parent = NULL;
976 *local_changes_subsumed = 0;
978 err = got_path_dirname(&parent, ondisk_path);
983 if (asprintf(&base_path, "%s/got-merge-blob-orig",
985 err = got_error_from_errno("asprintf");
990 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
993 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1000 * No common ancestor exists. This is an "add vs add" conflict
1001 * and we simply use an empty ancestor file to make both files
1002 * appear in the merged result in their entirety.
1004 f_orig = got_opentemp();
1005 if (f_orig == NULL) {
1006 err = got_error_from_errno("got_opentemp");
1011 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1012 err = got_error_from_errno("asprintf");
1017 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1020 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1025 err = got_object_id_str(&id_str, deriv_base_commit_id);
1028 if (asprintf(&label_deriv, "%s: commit %s",
1029 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1030 err = got_error_from_errno("asprintf");
1035 * In order the run a 3-way merge with a symlink we copy the symlink's
1036 * target path into a temporary file and use that file with diff3.
1038 if (S_ISLNK(st_mode)) {
1039 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1044 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1046 err = got_error_from_errno2("open", ondisk_path);
1049 f_deriv2 = fdopen(fd, "r");
1050 if (f_deriv2 == NULL) {
1051 err = got_error_from_errno2("fdopen", ondisk_path);
1057 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1058 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1059 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1061 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1062 err = got_error_from_errno("fclose");
1063 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1064 err = got_error_from_errno("fclose");
1065 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1066 err = got_error_from_errno("fclose");
1068 if (blob_orig_path) {
1069 unlink(blob_orig_path);
1070 free(blob_orig_path);
1072 if (blob_deriv_path) {
1073 unlink(blob_deriv_path);
1074 free(blob_deriv_path);
1082 static const struct got_error *
1083 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1084 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1085 int wt_fd, const char *path, struct got_object_id *blob_id)
1087 const struct got_error *err = NULL;
1088 struct got_fileindex_entry *new_ie;
1092 err = got_fileindex_entry_alloc(&new_ie, path);
1096 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1097 blob_id->sha1, base_commit_id->sha1, 1);
1101 err = got_fileindex_entry_add(fileindex, new_ie);
1104 got_fileindex_entry_free(new_ie);
1111 get_ondisk_perms(int executable, mode_t st_mode)
1113 mode_t xbits = S_IXUSR;
1116 /* Map read bits to execute bits. */
1117 if (st_mode & S_IRGRP)
1119 if (st_mode & S_IROTH)
1121 return st_mode | xbits;
1124 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1127 /* forward declaration */
1128 static const struct got_error *
1129 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1130 const char *path, mode_t te_mode, mode_t st_mode,
1131 struct got_blob_object *blob, int restoring_missing_file,
1132 int reverting_versioned_file, int installing_bad_symlink,
1133 int path_is_unversioned, struct got_repository *repo,
1134 got_worktree_checkout_cb progress_cb, void *progress_arg);
1137 * This function assumes that the provided symlink target points at a
1138 * safe location in the work tree!
1140 static const struct got_error *
1141 replace_existing_symlink(int *did_something, const char *ondisk_path,
1142 const char *target_path, size_t target_len)
1144 const struct got_error *err = NULL;
1146 char etarget[PATH_MAX];
1152 * "Bad" symlinks (those pointing outside the work tree or into the
1153 * .got directory) are installed in the work tree as a regular file
1154 * which contains the bad symlink target path.
1155 * The new symlink target has already been checked for safety by our
1156 * caller. If we can successfully open a regular file then we simply
1157 * replace this file with a symlink below.
1159 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1161 if (!got_err_open_nofollow_on_symlink())
1162 return got_error_from_errno2("open", ondisk_path);
1164 /* We are updating an existing on-disk symlink. */
1165 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1167 return got_error_from_errno2("readlink", ondisk_path);
1169 if (elen == target_len &&
1170 memcmp(etarget, target_path, target_len) == 0)
1171 return NULL; /* nothing to do */
1175 err = update_symlink(ondisk_path, target_path, target_len);
1176 if (fd != -1 && close(fd) == -1 && err == NULL)
1177 err = got_error_from_errno2("close", ondisk_path);
1181 static const struct got_error *
1182 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1183 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1185 const struct got_error *err = NULL;
1186 char canonpath[PATH_MAX];
1187 char *path_got = NULL;
1189 *is_bad_symlink = 0;
1191 if (target_len >= sizeof(canonpath)) {
1192 *is_bad_symlink = 1;
1197 * We do not use realpath(3) to resolve the symlink's target
1198 * path because we don't want to resolve symlinks recursively.
1199 * Instead we make the path absolute and then canonicalize it.
1200 * Relative symlink target lookup should begin at the directory
1201 * in which the blob object is being installed.
1203 if (!got_path_is_absolute(target_path)) {
1204 char *abspath, *parent;
1205 err = got_path_dirname(&parent, ondisk_path);
1208 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1210 return got_error_from_errno("asprintf");
1213 if (strlen(abspath) >= sizeof(canonpath)) {
1214 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1218 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1223 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1228 /* Only allow symlinks pointing at paths within the work tree. */
1229 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1230 *is_bad_symlink = 1;
1234 /* Do not allow symlinks pointing into the .got directory. */
1235 if (asprintf(&path_got, "%s/%s", wtroot_path,
1236 GOT_WORKTREE_GOT_DIR) == -1)
1237 return got_error_from_errno("asprintf");
1238 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1239 *is_bad_symlink = 1;
1245 static const struct got_error *
1246 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1247 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1248 int restoring_missing_file, int reverting_versioned_file,
1249 int path_is_unversioned, int allow_bad_symlinks,
1250 struct got_repository *repo,
1251 got_worktree_checkout_cb progress_cb, void *progress_arg)
1253 const struct got_error *err = NULL;
1254 char target_path[PATH_MAX];
1255 size_t len, target_len = 0;
1256 char *path_got = NULL;
1257 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1258 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1260 *is_bad_symlink = 0;
1263 * Blob object content specifies the target path of the link.
1264 * If a symbolic link cannot be installed we instead create
1265 * a regular file which contains the link target path stored
1266 * in the blob object.
1269 err = got_object_blob_read_block(&len, blob);
1270 if (len + target_len >= sizeof(target_path)) {
1271 /* Path too long; install as a regular file. */
1272 *is_bad_symlink = 1;
1273 got_object_blob_rewind(blob);
1274 return install_blob(worktree, ondisk_path, path,
1275 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1276 restoring_missing_file, reverting_versioned_file,
1277 1, path_is_unversioned, repo, progress_cb,
1281 /* Skip blob object header first time around. */
1282 memcpy(target_path + target_len, buf + hdrlen,
1284 target_len += len - hdrlen;
1288 target_path[target_len] = '\0';
1290 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1291 ondisk_path, worktree->root_path);
1295 if (*is_bad_symlink && !allow_bad_symlinks) {
1296 /* install as a regular file */
1297 got_object_blob_rewind(blob);
1298 err = install_blob(worktree, ondisk_path, path,
1299 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1300 restoring_missing_file, reverting_versioned_file, 1,
1301 path_is_unversioned, repo, progress_cb, progress_arg);
1305 if (symlink(target_path, ondisk_path) == -1) {
1306 if (errno == EEXIST) {
1307 int symlink_replaced;
1308 if (path_is_unversioned) {
1309 err = (*progress_cb)(progress_arg,
1310 GOT_STATUS_UNVERSIONED, path);
1313 err = replace_existing_symlink(&symlink_replaced,
1314 ondisk_path, target_path, target_len);
1318 if (symlink_replaced) {
1319 err = (*progress_cb)(progress_arg,
1320 reverting_versioned_file ?
1322 GOT_STATUS_UPDATE, path);
1324 err = (*progress_cb)(progress_arg,
1325 GOT_STATUS_EXISTS, path);
1328 goto done; /* Nothing else to do. */
1331 if (errno == ENOENT) {
1333 err = got_path_dirname(&parent, ondisk_path);
1336 err = add_dir_on_disk(worktree, parent);
1341 * Retry, and fall through to error handling
1342 * below if this second attempt fails.
1344 if (symlink(target_path, ondisk_path) != -1) {
1345 err = NULL; /* success */
1350 /* Handle errors from first or second creation attempt. */
1351 if (errno == ENAMETOOLONG) {
1352 /* bad target path; install as a regular file */
1353 *is_bad_symlink = 1;
1354 got_object_blob_rewind(blob);
1355 err = install_blob(worktree, ondisk_path, path,
1356 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1357 restoring_missing_file, reverting_versioned_file, 1,
1358 path_is_unversioned, repo,
1359 progress_cb, progress_arg);
1360 } else if (errno == ENOTDIR) {
1361 err = got_error_path(ondisk_path,
1362 GOT_ERR_FILE_OBSTRUCTED);
1364 err = got_error_from_errno3("symlink",
1365 target_path, ondisk_path);
1367 } else if (progress_cb)
1368 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1369 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1375 static const struct got_error *
1376 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1377 const char *path, mode_t te_mode, mode_t st_mode,
1378 struct got_blob_object *blob, int restoring_missing_file,
1379 int reverting_versioned_file, int installing_bad_symlink,
1380 int path_is_unversioned, struct got_repository *repo,
1381 got_worktree_checkout_cb progress_cb, void *progress_arg)
1383 const struct got_error *err = NULL;
1387 char *tmppath = NULL;
1389 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1390 O_CLOEXEC, GOT_DEFAULT_FILE_MODE);
1392 if (errno == ENOENT) {
1394 err = got_path_dirname(&parent, path);
1397 err = add_dir_on_disk(worktree, parent);
1401 fd = open(ondisk_path,
1402 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1403 GOT_DEFAULT_FILE_MODE);
1405 return got_error_from_errno2("open",
1407 } else if (errno == EEXIST) {
1408 if (path_is_unversioned) {
1409 err = (*progress_cb)(progress_arg,
1410 GOT_STATUS_UNVERSIONED, path);
1413 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1414 !S_ISREG(st_mode) && !installing_bad_symlink) {
1415 /* TODO file is obstructed; do something */
1416 err = got_error_path(ondisk_path,
1417 GOT_ERR_FILE_OBSTRUCTED);
1420 err = got_opentemp_named_fd(&tmppath, &fd,
1427 return got_error_from_errno2("open", ondisk_path);
1430 if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1431 err = got_error_from_errno2("fchmod",
1432 update ? tmppath : ondisk_path);
1437 if (restoring_missing_file)
1438 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1440 else if (reverting_versioned_file)
1441 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1444 err = (*progress_cb)(progress_arg,
1445 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1450 hdrlen = got_object_blob_get_hdrlen(blob);
1452 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1453 err = got_object_blob_read_block(&len, blob);
1457 /* Skip blob object header first time around. */
1458 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1460 err = got_error_from_errno("write");
1462 } else if (outlen != len - hdrlen) {
1463 err = got_error(GOT_ERR_IO);
1470 if (fsync(fd) != 0) {
1471 err = got_error_from_errno("fsync");
1476 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1477 err = got_error_from_errno2("unlink", ondisk_path);
1480 if (rename(tmppath, ondisk_path) != 0) {
1481 err = got_error_from_errno3("rename", tmppath,
1490 if (fd != -1 && close(fd) == -1 && err == NULL)
1491 err = got_error_from_errno("close");
1492 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1493 err = got_error_from_errno2("unlink", tmppath);
1498 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1499 static const struct got_error *
1500 get_modified_file_content_status(unsigned char *status, FILE *f)
1502 const struct got_error *err = NULL;
1503 const char *markers[3] = {
1504 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1505 GOT_DIFF_CONFLICT_MARKER_SEP,
1506 GOT_DIFF_CONFLICT_MARKER_END
1510 size_t linesize = 0;
1513 while (*status == GOT_STATUS_MODIFY) {
1514 linelen = getline(&line, &linesize, f);
1515 if (linelen == -1) {
1518 err = got_ferror(f, GOT_ERR_IO);
1522 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1523 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1525 *status = GOT_STATUS_CONFLICT;
1536 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1538 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1539 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1543 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1545 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1546 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1547 ie->mtime_sec == sb->st_mtim.tv_sec &&
1548 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1549 ie->size == (sb->st_size & 0xffffffff) &&
1550 !xbit_differs(ie, sb->st_mode));
1553 static unsigned char
1554 get_staged_status(struct got_fileindex_entry *ie)
1556 switch (got_fileindex_entry_stage_get(ie)) {
1557 case GOT_FILEIDX_STAGE_ADD:
1558 return GOT_STATUS_ADD;
1559 case GOT_FILEIDX_STAGE_DELETE:
1560 return GOT_STATUS_DELETE;
1561 case GOT_FILEIDX_STAGE_MODIFY:
1562 return GOT_STATUS_MODIFY;
1564 return GOT_STATUS_NO_CHANGE;
1568 static const struct got_error *
1569 get_symlink_modification_status(unsigned char *status,
1570 struct got_fileindex_entry *ie, const char *abspath,
1571 int dirfd, const char *de_name, struct got_blob_object *blob)
1573 const struct got_error *err = NULL;
1574 char target_path[PATH_MAX];
1575 char etarget[PATH_MAX];
1577 size_t len, target_len = 0;
1578 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1579 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1581 *status = GOT_STATUS_NO_CHANGE;
1583 /* Blob object content specifies the target path of the link. */
1585 err = got_object_blob_read_block(&len, blob);
1588 if (len + target_len >= sizeof(target_path)) {
1590 * Should not happen. The blob contents were OK
1591 * when this symlink was installed.
1593 return got_error(GOT_ERR_NO_SPACE);
1596 /* Skip blob object header first time around. */
1597 memcpy(target_path + target_len, buf + hdrlen,
1599 target_len += len - hdrlen;
1603 target_path[target_len] = '\0';
1606 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1608 return got_error_from_errno2("readlinkat", abspath);
1610 elen = readlink(abspath, etarget, sizeof(etarget));
1612 return got_error_from_errno2("readlink", abspath);
1615 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1616 *status = GOT_STATUS_MODIFY;
1621 static const struct got_error *
1622 get_file_status(unsigned char *status, struct stat *sb,
1623 struct got_fileindex_entry *ie, const char *abspath,
1624 int dirfd, const char *de_name, struct got_repository *repo)
1626 const struct got_error *err = NULL;
1627 struct got_object_id id;
1632 struct got_blob_object *blob = NULL;
1634 unsigned char staged_status = get_staged_status(ie);
1636 *status = GOT_STATUS_NO_CHANGE;
1637 memset(sb, 0, sizeof(*sb));
1640 * Whenever the caller provides a directory descriptor and a
1641 * directory entry name for the file, use them! This prevents
1642 * race conditions if filesystem paths change beneath our feet.
1645 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1646 if (errno == ENOENT) {
1647 if (got_fileindex_entry_has_file_on_disk(ie))
1648 *status = GOT_STATUS_MISSING;
1650 *status = GOT_STATUS_DELETE;
1653 err = got_error_from_errno2("fstatat", abspath);
1657 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1658 if (fd == -1 && errno != ENOENT &&
1659 !got_err_open_nofollow_on_symlink())
1660 return got_error_from_errno2("open", abspath);
1661 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1662 if (lstat(abspath, sb) == -1)
1663 return got_error_from_errno2("lstat", abspath);
1664 } else if (fd == -1 || fstat(fd, sb) == -1) {
1665 if (errno == ENOENT) {
1666 if (got_fileindex_entry_has_file_on_disk(ie))
1667 *status = GOT_STATUS_MISSING;
1669 *status = GOT_STATUS_DELETE;
1672 err = got_error_from_errno2("fstat", abspath);
1677 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1678 *status = GOT_STATUS_OBSTRUCTED;
1682 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1683 *status = GOT_STATUS_DELETE;
1685 } else if (!got_fileindex_entry_has_blob(ie) &&
1686 staged_status != GOT_STATUS_ADD) {
1687 *status = GOT_STATUS_ADD;
1691 if (!stat_info_differs(ie, sb))
1694 if (S_ISLNK(sb->st_mode) &&
1695 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1696 *status = GOT_STATUS_MODIFY;
1700 if (staged_status == GOT_STATUS_MODIFY ||
1701 staged_status == GOT_STATUS_ADD)
1702 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1704 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1706 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1710 if (S_ISLNK(sb->st_mode)) {
1711 err = get_symlink_modification_status(status, ie,
1712 abspath, dirfd, de_name, blob);
1717 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1719 err = got_error_from_errno2("openat", abspath);
1724 f = fdopen(fd, "r");
1726 err = got_error_from_errno2("fdopen", abspath);
1730 hdrlen = got_object_blob_get_hdrlen(blob);
1732 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1733 err = got_object_blob_read_block(&blen, blob);
1736 /* Skip length of blob object header first time around. */
1737 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1738 if (flen == 0 && ferror(f)) {
1739 err = got_error_from_errno("fread");
1742 if (blen - hdrlen == 0) {
1744 *status = GOT_STATUS_MODIFY;
1746 } else if (flen == 0) {
1747 if (blen - hdrlen != 0)
1748 *status = GOT_STATUS_MODIFY;
1750 } else if (blen - hdrlen == flen) {
1751 /* Skip blob object header first time around. */
1752 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1753 *status = GOT_STATUS_MODIFY;
1757 *status = GOT_STATUS_MODIFY;
1763 if (*status == GOT_STATUS_MODIFY) {
1765 err = get_modified_file_content_status(status, f);
1766 } else if (xbit_differs(ie, sb->st_mode))
1767 *status = GOT_STATUS_MODE_CHANGE;
1770 got_object_blob_close(blob);
1771 if (f != NULL && fclose(f) == EOF && err == NULL)
1772 err = got_error_from_errno2("fclose", abspath);
1773 if (fd != -1 && close(fd) == -1 && err == NULL)
1774 err = got_error_from_errno2("close", abspath);
1779 * Update timestamps in the file index if a file is unmodified and
1780 * we had to run a full content comparison to find out.
1782 static const struct got_error *
1783 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1784 struct got_fileindex_entry *ie, struct stat *sb)
1786 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1787 return got_fileindex_entry_update(ie, wt_fd, path,
1788 ie->blob_sha1, ie->commit_sha1, 1);
1793 static const struct got_error *
1794 update_blob(struct got_worktree *worktree,
1795 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1796 struct got_tree_entry *te, const char *path,
1797 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1800 const struct got_error *err = NULL;
1801 struct got_blob_object *blob = NULL;
1803 unsigned char status = GOT_STATUS_NO_CHANGE;
1806 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1807 return got_error_from_errno("asprintf");
1810 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1811 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1814 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1818 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1819 sb.st_mode = got_fileindex_perms_to_st(ie);
1821 if (stat(ondisk_path, &sb) == -1) {
1822 if (errno != ENOENT) {
1823 err = got_error_from_errno2("stat",
1827 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1828 status = GOT_STATUS_UNVERSIONED;
1830 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1831 status = GOT_STATUS_UNVERSIONED;
1833 status = GOT_STATUS_OBSTRUCTED;
1837 if (status == GOT_STATUS_OBSTRUCTED) {
1839 got_fileindex_entry_mark_skipped(ie);
1840 err = (*progress_cb)(progress_arg, status, path);
1843 if (status == GOT_STATUS_CONFLICT) {
1845 got_fileindex_entry_mark_skipped(ie);
1846 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1851 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1852 (S_ISLNK(te->mode) ||
1853 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1855 * This is a regular file or an installed bad symlink.
1856 * If the file index indicates that this file is already
1857 * up-to-date with respect to the repository we can skip
1858 * updating contents of this file.
1860 if (got_fileindex_entry_has_commit(ie) &&
1861 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1862 SHA1_DIGEST_LENGTH) == 0) {
1864 err = sync_timestamps(worktree->root_fd,
1865 path, status, ie, &sb);
1868 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1872 if (got_fileindex_entry_has_blob(ie) &&
1873 memcmp(ie->blob_sha1, te->id.sha1,
1874 SHA1_DIGEST_LENGTH) == 0) {
1875 /* Different commit but the same blob. */
1876 err = sync_timestamps(worktree->root_fd,
1877 path, status, ie, &sb);
1880 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1886 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1890 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1891 int update_timestamps;
1892 struct got_blob_object *blob2 = NULL;
1893 char *label_orig = NULL;
1894 if (got_fileindex_entry_has_blob(ie)) {
1895 struct got_object_id id2;
1896 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1897 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1901 if (got_fileindex_entry_has_commit(ie)) {
1902 char id_str[SHA1_DIGEST_STRING_LENGTH];
1903 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1904 sizeof(id_str)) == NULL) {
1905 err = got_error_path(id_str,
1906 GOT_ERR_BAD_OBJ_ID_STR);
1909 if (asprintf(&label_orig, "%s: commit %s",
1910 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1911 err = got_error_from_errno("asprintf");
1915 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1917 err = got_object_blob_read_to_str(&link_target, blob);
1920 err = merge_symlink(worktree, blob2, ondisk_path, path,
1921 label_orig, link_target, worktree->base_commit_id,
1922 repo, progress_cb, progress_arg);
1925 err = merge_blob(&update_timestamps, worktree, blob2,
1926 ondisk_path, path, sb.st_mode, label_orig, blob,
1927 worktree->base_commit_id, repo,
1928 progress_cb, progress_arg);
1932 got_object_blob_close(blob2);
1936 * Do not update timestamps of files with local changes.
1937 * Otherwise, a future status walk would treat them as
1938 * unmodified files again.
1940 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1941 blob->id.sha1, worktree->base_commit_id->sha1,
1943 } else if (status == GOT_STATUS_MODE_CHANGE) {
1944 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1945 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1946 } else if (status == GOT_STATUS_DELETE) {
1947 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1950 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1951 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1955 int is_bad_symlink = 0;
1956 if (S_ISLNK(te->mode)) {
1957 err = install_symlink(&is_bad_symlink, worktree,
1958 ondisk_path, path, blob,
1959 status == GOT_STATUS_MISSING, 0,
1960 status == GOT_STATUS_UNVERSIONED, 0,
1961 repo, progress_cb, progress_arg);
1963 err = install_blob(worktree, ondisk_path, path,
1964 te->mode, sb.st_mode, blob,
1965 status == GOT_STATUS_MISSING, 0, 0,
1966 status == GOT_STATUS_UNVERSIONED, repo,
1967 progress_cb, progress_arg);
1973 err = got_fileindex_entry_update(ie,
1974 worktree->root_fd, path, blob->id.sha1,
1975 worktree->base_commit_id->sha1, 1);
1977 err = create_fileindex_entry(&ie, fileindex,
1978 worktree->base_commit_id, worktree->root_fd, path,
1984 if (is_bad_symlink) {
1985 got_fileindex_entry_filetype_set(ie,
1986 GOT_FILEIDX_MODE_BAD_SYMLINK);
1989 got_object_blob_close(blob);
1995 static const struct got_error *
1996 remove_ondisk_file(const char *root_path, const char *path)
1998 const struct got_error *err = NULL;
1999 char *ondisk_path = NULL, *parent = NULL;
2001 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2002 return got_error_from_errno("asprintf");
2004 if (unlink(ondisk_path) == -1) {
2005 if (errno != ENOENT)
2006 err = got_error_from_errno2("unlink", ondisk_path);
2008 size_t root_len = strlen(root_path);
2009 err = got_path_dirname(&parent, ondisk_path);
2012 while (got_path_cmp(parent, root_path,
2013 strlen(parent), root_len) != 0) {
2015 ondisk_path = parent;
2017 if (rmdir(ondisk_path) == -1) {
2018 if (errno != ENOTEMPTY)
2019 err = got_error_from_errno2("rmdir",
2023 err = got_path_dirname(&parent, ondisk_path);
2034 static const struct got_error *
2035 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2036 struct got_fileindex_entry *ie, struct got_repository *repo,
2037 got_worktree_checkout_cb progress_cb, void *progress_arg)
2039 const struct got_error *err = NULL;
2040 unsigned char status;
2044 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2045 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2047 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2049 return got_error_from_errno("asprintf");
2051 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2055 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2056 char ondisk_target[PATH_MAX];
2057 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2058 sizeof(ondisk_target));
2059 if (ondisk_len == -1) {
2060 err = got_error_from_errno2("readlink", ondisk_path);
2063 ondisk_target[ondisk_len] = '\0';
2064 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2065 NULL, NULL, /* XXX pass common ancestor info? */
2066 ondisk_target, ondisk_path);
2069 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2074 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2075 status == GOT_STATUS_ADD) {
2076 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2080 * Preserve the working file and change the deleted blob's
2081 * entry into a schedule-add entry.
2083 err = got_fileindex_entry_update(ie, worktree->root_fd,
2084 ie->path, NULL, NULL, 0);
2086 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2089 if (status == GOT_STATUS_NO_CHANGE) {
2090 err = remove_ondisk_file(worktree->root_path, ie->path);
2094 got_fileindex_entry_remove(fileindex, ie);
2101 struct diff_cb_arg {
2102 struct got_fileindex *fileindex;
2103 struct got_worktree *worktree;
2104 struct got_repository *repo;
2105 got_worktree_checkout_cb progress_cb;
2107 got_cancel_cb cancel_cb;
2111 static const struct got_error *
2112 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2113 struct got_tree_entry *te, const char *parent_path)
2115 struct diff_cb_arg *a = arg;
2117 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2118 return got_error(GOT_ERR_CANCELLED);
2120 return update_blob(a->worktree, a->fileindex, ie, te,
2121 ie->path, a->repo, a->progress_cb, a->progress_arg);
2124 static const struct got_error *
2125 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2127 struct diff_cb_arg *a = arg;
2129 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2130 return got_error(GOT_ERR_CANCELLED);
2132 return delete_blob(a->worktree, a->fileindex, ie,
2133 a->repo, a->progress_cb, a->progress_arg);
2136 static const struct got_error *
2137 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2139 struct diff_cb_arg *a = arg;
2140 const struct got_error *err;
2143 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2144 return got_error(GOT_ERR_CANCELLED);
2146 if (got_object_tree_entry_is_submodule(te))
2149 if (asprintf(&path, "%s%s%s", parent_path,
2150 parent_path[0] ? "/" : "", te->name)
2152 return got_error_from_errno("asprintf");
2154 if (S_ISDIR(te->mode))
2155 err = add_dir_on_disk(a->worktree, path);
2157 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2158 a->repo, a->progress_cb, a->progress_arg);
2164 const struct got_error *
2165 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2167 uint32_t uuid_status;
2169 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2170 if (uuid_status != uuid_s_ok) {
2172 return got_error_uuid(uuid_status, "uuid_to_string");
2178 static const struct got_error *
2179 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2181 const struct got_error *err = NULL;
2182 char *uuidstr = NULL;
2186 err = got_worktree_get_uuid(&uuidstr, worktree);
2190 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2191 err = got_error_from_errno("asprintf");
2198 const struct got_error *
2199 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2201 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2204 static const struct got_error *
2205 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2207 return get_ref_name(refname, worktree,
2208 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2211 static const struct got_error *
2212 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2214 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2217 static const struct got_error *
2218 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2220 return get_ref_name(refname, worktree,
2221 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2224 static const struct got_error *
2225 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2227 return get_ref_name(refname, worktree,
2228 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2231 static const struct got_error *
2232 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2234 return get_ref_name(refname, worktree,
2235 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2238 static const struct got_error *
2239 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2241 return get_ref_name(refname, worktree,
2242 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2245 static const struct got_error *
2246 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2248 return get_ref_name(refname, worktree,
2249 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2252 static const struct got_error *
2253 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2255 return get_ref_name(refname, worktree,
2256 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2259 const struct got_error *
2260 got_worktree_get_histedit_script_path(char **path,
2261 struct got_worktree *worktree)
2263 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2264 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2266 return got_error_from_errno("asprintf");
2271 static const struct got_error *
2272 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2274 return get_ref_name(refname, worktree,
2275 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2278 static const struct got_error *
2279 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2281 return get_ref_name(refname, worktree,
2282 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2286 * Prevent Git's garbage collector from deleting our base commit by
2287 * setting a reference to our base commit's ID.
2289 static const struct got_error *
2290 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2292 const struct got_error *err = NULL;
2293 struct got_reference *ref = NULL;
2296 err = got_worktree_get_base_ref_name(&refname, worktree);
2300 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2304 err = got_ref_write(ref, repo);
2312 static const struct got_error *
2313 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2315 const struct got_error *err = NULL;
2317 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2318 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2319 err = got_error_from_errno("asprintf");
2320 *fileindex_path = NULL;
2326 static const struct got_error *
2327 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2328 struct got_worktree *worktree)
2330 const struct got_error *err = NULL;
2333 *fileindex_path = NULL;
2334 *fileindex = got_fileindex_alloc();
2335 if (*fileindex == NULL)
2336 return got_error_from_errno("got_fileindex_alloc");
2338 err = get_fileindex_path(fileindex_path, worktree);
2342 index = fopen(*fileindex_path, "rbe");
2343 if (index == NULL) {
2344 if (errno != ENOENT)
2345 err = got_error_from_errno2("fopen", *fileindex_path);
2347 err = got_fileindex_read(*fileindex, index);
2348 if (fclose(index) == EOF && err == NULL)
2349 err = got_error_from_errno("fclose");
2353 free(*fileindex_path);
2354 *fileindex_path = NULL;
2355 got_fileindex_free(*fileindex);
2361 struct bump_base_commit_id_arg {
2362 struct got_object_id *base_commit_id;
2365 const char *entry_name;
2366 got_worktree_checkout_cb progress_cb;
2370 /* Bump base commit ID of all files within an updated part of the work tree. */
2371 static const struct got_error *
2372 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2374 const struct got_error *err;
2375 struct bump_base_commit_id_arg *a = arg;
2377 if (a->entry_name) {
2378 if (strcmp(ie->path, a->path) != 0)
2380 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2383 if (got_fileindex_entry_was_skipped(ie))
2386 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2387 SHA1_DIGEST_LENGTH) == 0)
2390 if (a->progress_cb) {
2391 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2396 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2400 static const struct got_error *
2401 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2402 struct got_fileindex *fileindex,
2403 got_worktree_checkout_cb progress_cb, void *progress_arg)
2405 struct bump_base_commit_id_arg bbc_arg;
2407 bbc_arg.base_commit_id = worktree->base_commit_id;
2408 bbc_arg.entry_name = NULL;
2410 bbc_arg.path_len = 0;
2411 bbc_arg.progress_cb = progress_cb;
2412 bbc_arg.progress_arg = progress_arg;
2414 return got_fileindex_for_each_entry_safe(fileindex,
2415 bump_base_commit_id, &bbc_arg);
2418 static const struct got_error *
2419 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2421 const struct got_error *err = NULL;
2422 char *new_fileindex_path = NULL;
2423 FILE *new_index = NULL;
2424 struct timespec timeout;
2426 err = got_opentemp_named(&new_fileindex_path, &new_index,
2431 err = got_fileindex_write(fileindex, new_index);
2435 if (rename(new_fileindex_path, fileindex_path) != 0) {
2436 err = got_error_from_errno3("rename", new_fileindex_path,
2438 unlink(new_fileindex_path);
2442 * Sleep for a short amount of time to ensure that files modified after
2443 * this program exits have a different time stamp from the one which
2444 * was recorded in the file index.
2447 timeout.tv_nsec = 1;
2448 nanosleep(&timeout, NULL);
2452 free(new_fileindex_path);
2456 static const struct got_error *
2457 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2458 struct got_object_id **tree_id, const char *wt_relpath,
2459 struct got_worktree *worktree, struct got_repository *repo)
2461 const struct got_error *err = NULL;
2462 struct got_object_id *id = NULL;
2463 char *in_repo_path = NULL;
2464 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2466 *entry_type = GOT_OBJ_TYPE_ANY;
2467 *tree_relpath = NULL;
2470 if (wt_relpath[0] == '\0') {
2471 /* Check out all files within the work tree. */
2472 *entry_type = GOT_OBJ_TYPE_TREE;
2473 *tree_relpath = strdup("");
2474 if (*tree_relpath == NULL) {
2475 err = got_error_from_errno("strdup");
2478 err = got_object_id_by_path(tree_id, repo,
2479 worktree->base_commit_id, worktree->path_prefix);
2485 /* Check out a subset of files in the work tree. */
2487 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2488 is_root_wt ? "" : "/", wt_relpath) == -1) {
2489 err = got_error_from_errno("asprintf");
2493 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2499 in_repo_path = NULL;
2501 err = got_object_get_type(entry_type, repo, id);
2505 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2506 /* Check out a single file. */
2507 if (strchr(wt_relpath, '/') == NULL) {
2508 /* Check out a single file in work tree's root dir. */
2509 in_repo_path = strdup(worktree->path_prefix);
2510 if (in_repo_path == NULL) {
2511 err = got_error_from_errno("strdup");
2514 *tree_relpath = strdup("");
2515 if (*tree_relpath == NULL) {
2516 err = got_error_from_errno("strdup");
2520 /* Check out a single file in a subdirectory. */
2521 err = got_path_dirname(tree_relpath, wt_relpath);
2524 if (asprintf(&in_repo_path, "%s%s%s",
2525 worktree->path_prefix, is_root_wt ? "" : "/",
2526 *tree_relpath) == -1) {
2527 err = got_error_from_errno("asprintf");
2531 err = got_object_id_by_path(tree_id, repo,
2532 worktree->base_commit_id, in_repo_path);
2534 /* Check out all files within a subdirectory. */
2535 *tree_id = got_object_id_dup(id);
2536 if (*tree_id == NULL) {
2537 err = got_error_from_errno("got_object_id_dup");
2540 *tree_relpath = strdup(wt_relpath);
2541 if (*tree_relpath == NULL) {
2542 err = got_error_from_errno("strdup");
2550 *entry_type = GOT_OBJ_TYPE_ANY;
2551 free(*tree_relpath);
2552 *tree_relpath = NULL;
2559 static const struct got_error *
2560 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2561 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2562 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2563 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2565 const struct got_error *err = NULL;
2566 struct got_commit_object *commit = NULL;
2567 struct got_tree_object *tree = NULL;
2568 struct got_fileindex_diff_tree_cb diff_cb;
2569 struct diff_cb_arg arg;
2571 err = ref_base_commit(worktree, repo);
2573 if (!(err->code == GOT_ERR_ERRNO &&
2574 (errno == EACCES || errno == EROFS)))
2576 err = (*progress_cb)(progress_arg,
2577 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2582 err = got_object_open_as_commit(&commit, repo,
2583 worktree->base_commit_id);
2587 err = got_object_open_as_tree(&tree, repo, tree_id);
2592 got_object_tree_find_entry(tree, entry_name) == NULL) {
2593 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2597 diff_cb.diff_old_new = diff_old_new;
2598 diff_cb.diff_old = diff_old;
2599 diff_cb.diff_new = diff_new;
2600 arg.fileindex = fileindex;
2601 arg.worktree = worktree;
2603 arg.progress_cb = progress_cb;
2604 arg.progress_arg = progress_arg;
2605 arg.cancel_cb = cancel_cb;
2606 arg.cancel_arg = cancel_arg;
2607 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2608 entry_name, repo, &diff_cb, &arg);
2611 got_object_tree_close(tree);
2613 got_object_commit_close(commit);
2617 const struct got_error *
2618 got_worktree_checkout_files(struct got_worktree *worktree,
2619 struct got_pathlist_head *paths, struct got_repository *repo,
2620 got_worktree_checkout_cb progress_cb, void *progress_arg,
2621 got_cancel_cb cancel_cb, void *cancel_arg)
2623 const struct got_error *err = NULL, *sync_err, *unlockerr;
2624 struct got_commit_object *commit = NULL;
2625 struct got_tree_object *tree = NULL;
2626 struct got_fileindex *fileindex = NULL;
2627 char *fileindex_path = NULL;
2628 struct got_pathlist_entry *pe;
2629 struct tree_path_data {
2630 STAILQ_ENTRY(tree_path_data) entry;
2631 struct got_object_id *tree_id;
2636 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2638 STAILQ_INIT(&tree_paths);
2640 err = lock_worktree(worktree, LOCK_EX);
2644 /* Map all specified paths to in-repository trees. */
2645 TAILQ_FOREACH(pe, paths, entry) {
2646 tpd = malloc(sizeof(*tpd));
2648 err = got_error_from_errno("malloc");
2652 err = find_tree_entry_for_checkout(&tpd->entry_type,
2653 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2659 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2660 err = got_path_basename(&tpd->entry_name, pe->path);
2668 tpd->entry_name = NULL;
2670 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2674 * Read the file index.
2675 * Checking out files is supposed to be an idempotent operation.
2676 * If the on-disk file index is incomplete we will try to complete it.
2678 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2682 tpd = STAILQ_FIRST(&tree_paths);
2683 TAILQ_FOREACH(pe, paths, entry) {
2684 struct bump_base_commit_id_arg bbc_arg;
2686 err = checkout_files(worktree, fileindex, tpd->relpath,
2687 tpd->tree_id, tpd->entry_name, repo,
2688 progress_cb, progress_arg, cancel_cb, cancel_arg);
2692 bbc_arg.base_commit_id = worktree->base_commit_id;
2693 bbc_arg.entry_name = tpd->entry_name;
2694 bbc_arg.path = pe->path;
2695 bbc_arg.path_len = pe->path_len;
2696 bbc_arg.progress_cb = progress_cb;
2697 bbc_arg.progress_arg = progress_arg;
2698 err = got_fileindex_for_each_entry_safe(fileindex,
2699 bump_base_commit_id, &bbc_arg);
2703 tpd = STAILQ_NEXT(tpd, entry);
2705 sync_err = sync_fileindex(fileindex, fileindex_path);
2706 if (sync_err && err == NULL)
2709 free(fileindex_path);
2711 got_object_tree_close(tree);
2713 got_object_commit_close(commit);
2715 got_fileindex_free(fileindex);
2716 while (!STAILQ_EMPTY(&tree_paths)) {
2717 tpd = STAILQ_FIRST(&tree_paths);
2718 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2723 unlockerr = lock_worktree(worktree, LOCK_SH);
2724 if (unlockerr && err == NULL)
2729 struct merge_file_cb_arg {
2730 struct got_worktree *worktree;
2731 struct got_fileindex *fileindex;
2732 got_worktree_checkout_cb progress_cb;
2734 got_cancel_cb cancel_cb;
2736 const char *label_orig;
2737 struct got_object_id *commit_id2;
2738 int allow_bad_symlinks;
2741 static const struct got_error *
2742 merge_file_cb(void *arg, struct got_blob_object *blob1,
2743 struct got_blob_object *blob2, struct got_object_id *id1,
2744 struct got_object_id *id2, const char *path1, const char *path2,
2745 mode_t mode1, mode_t mode2, struct got_repository *repo)
2747 static const struct got_error *err = NULL;
2748 struct merge_file_cb_arg *a = arg;
2749 struct got_fileindex_entry *ie;
2750 char *ondisk_path = NULL;
2752 unsigned char status;
2753 int local_changes_subsumed;
2754 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2755 char *id_str = NULL, *label_deriv2 = NULL;
2757 if (blob1 && blob2) {
2758 ie = got_fileindex_entry_get(a->fileindex, path2,
2761 return (*a->progress_cb)(a->progress_arg,
2762 GOT_STATUS_MISSING, path2);
2764 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2766 return got_error_from_errno("asprintf");
2768 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2773 if (status == GOT_STATUS_DELETE) {
2774 err = (*a->progress_cb)(a->progress_arg,
2775 GOT_STATUS_MERGE, path2);
2778 if (status != GOT_STATUS_NO_CHANGE &&
2779 status != GOT_STATUS_MODIFY &&
2780 status != GOT_STATUS_CONFLICT &&
2781 status != GOT_STATUS_ADD) {
2782 err = (*a->progress_cb)(a->progress_arg, status, path2);
2786 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2788 err = got_object_blob_read_to_str(&link_target2, blob2);
2791 err = merge_symlink(a->worktree, blob1, ondisk_path,
2792 path2, a->label_orig, link_target2, a->commit_id2,
2793 repo, a->progress_cb, a->progress_arg);
2798 f_orig = got_opentemp();
2799 if (f_orig == NULL) {
2800 err = got_error_from_errno("got_opentemp");
2803 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2808 f_deriv2 = got_opentemp();
2809 if (f_deriv2 == NULL)
2811 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2816 fd = open(ondisk_path,
2817 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2819 err = got_error_from_errno2("open",
2823 f_deriv = fdopen(fd, "r");
2824 if (f_deriv == NULL) {
2825 err = got_error_from_errno2("fdopen",
2830 err = got_object_id_str(&id_str, a->commit_id2);
2833 if (asprintf(&label_deriv2, "%s: commit %s",
2834 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2835 err = got_error_from_errno("asprintf");
2838 err = merge_file(&local_changes_subsumed, a->worktree,
2839 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2840 sb.st_mode, a->label_orig, NULL, label_deriv2,
2841 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2842 a->progress_cb, a->progress_arg);
2845 ie = got_fileindex_entry_get(a->fileindex, path1,
2848 return (*a->progress_cb)(a->progress_arg,
2849 GOT_STATUS_MISSING, path1);
2851 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2853 return got_error_from_errno("asprintf");
2855 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2861 case GOT_STATUS_NO_CHANGE:
2862 err = (*a->progress_cb)(a->progress_arg,
2863 GOT_STATUS_DELETE, path1);
2866 err = remove_ondisk_file(a->worktree->root_path, path1);
2870 got_fileindex_entry_mark_deleted_from_disk(ie);
2872 case GOT_STATUS_DELETE:
2873 case GOT_STATUS_MISSING:
2874 err = (*a->progress_cb)(a->progress_arg,
2875 GOT_STATUS_DELETE, path1);
2879 got_fileindex_entry_mark_deleted_from_disk(ie);
2881 case GOT_STATUS_ADD: {
2882 struct got_object_id *id;
2886 * Delete the added file only if its content already
2887 * exists in the repository.
2889 err = got_object_blob_file_create(&id, &blob1_f,
2890 &blob1_size, path1);
2893 if (got_object_id_cmp(id, id1) == 0) {
2894 err = (*a->progress_cb)(a->progress_arg,
2895 GOT_STATUS_DELETE, path1);
2898 err = remove_ondisk_file(a->worktree->root_path,
2903 got_fileindex_entry_remove(a->fileindex,
2906 err = (*a->progress_cb)(a->progress_arg,
2907 GOT_STATUS_CANNOT_DELETE, path1);
2909 if (fclose(blob1_f) == EOF && err == NULL)
2910 err = got_error_from_errno("fclose");
2916 case GOT_STATUS_MODIFY:
2917 case GOT_STATUS_CONFLICT:
2918 err = (*a->progress_cb)(a->progress_arg,
2919 GOT_STATUS_CANNOT_DELETE, path1);
2923 case GOT_STATUS_OBSTRUCTED:
2924 err = (*a->progress_cb)(a->progress_arg, status, path1);
2932 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2934 return got_error_from_errno("asprintf");
2935 ie = got_fileindex_entry_get(a->fileindex, path2,
2938 err = get_file_status(&status, &sb, ie, ondisk_path,
2942 if (status != GOT_STATUS_NO_CHANGE &&
2943 status != GOT_STATUS_MODIFY &&
2944 status != GOT_STATUS_CONFLICT &&
2945 status != GOT_STATUS_ADD) {
2946 err = (*a->progress_cb)(a->progress_arg,
2950 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2952 err = got_object_blob_read_to_str(&link_target2,
2956 err = merge_symlink(a->worktree, NULL,
2957 ondisk_path, path2, a->label_orig,
2958 link_target2, a->commit_id2, repo,
2959 a->progress_cb, a->progress_arg);
2961 } else if (S_ISREG(sb.st_mode)) {
2962 err = merge_blob(&local_changes_subsumed,
2963 a->worktree, NULL, ondisk_path, path2,
2964 sb.st_mode, a->label_orig, blob2,
2965 a->commit_id2, repo, a->progress_cb,
2968 err = got_error_path(ondisk_path,
2969 GOT_ERR_FILE_OBSTRUCTED);
2973 if (status == GOT_STATUS_DELETE) {
2974 err = got_fileindex_entry_update(ie,
2975 a->worktree->root_fd, path2, blob2->id.sha1,
2976 a->worktree->base_commit_id->sha1, 0);
2981 int is_bad_symlink = 0;
2982 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2983 if (S_ISLNK(mode2)) {
2984 err = install_symlink(&is_bad_symlink,
2985 a->worktree, ondisk_path, path2, blob2, 0,
2986 0, 1, a->allow_bad_symlinks, repo,
2987 a->progress_cb, a->progress_arg);
2989 err = install_blob(a->worktree, ondisk_path, path2,
2990 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2991 a->progress_cb, a->progress_arg);
2995 err = got_fileindex_entry_alloc(&ie, path2);
2998 err = got_fileindex_entry_update(ie,
2999 a->worktree->root_fd, path2, NULL, NULL, 1);
3001 got_fileindex_entry_free(ie);
3004 err = got_fileindex_entry_add(a->fileindex, ie);
3006 got_fileindex_entry_free(ie);
3009 if (is_bad_symlink) {
3010 got_fileindex_entry_filetype_set(ie,
3011 GOT_FILEIDX_MODE_BAD_SYMLINK);
3016 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3017 err = got_error_from_errno("fclose");
3018 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3019 err = got_error_from_errno("fclose");
3020 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3021 err = got_error_from_errno("fclose");
3028 static const struct got_error *
3029 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3031 struct got_worktree *worktree = arg;
3033 /* Reject merges into a work tree with mixed base commits. */
3034 if (got_fileindex_entry_has_commit(ie) &&
3035 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3036 SHA1_DIGEST_LENGTH) != 0)
3037 return got_error(GOT_ERR_MIXED_COMMITS);
3042 struct check_merge_conflicts_arg {
3043 struct got_worktree *worktree;
3044 struct got_fileindex *fileindex;
3045 struct got_repository *repo;
3048 static const struct got_error *
3049 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3050 struct got_blob_object *blob2, struct got_object_id *id1,
3051 struct got_object_id *id2, const char *path1, const char *path2,
3052 mode_t mode1, mode_t mode2, struct got_repository *repo)
3054 const struct got_error *err = NULL;
3055 struct check_merge_conflicts_arg *a = arg;
3056 unsigned char status;
3058 struct got_fileindex_entry *ie;
3059 const char *path = path2 ? path2 : path1;
3060 struct got_object_id *id = id2 ? id2 : id1;
3066 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3070 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3072 return got_error_from_errno("asprintf");
3074 /* Reject merges into a work tree with conflicted files. */
3075 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3079 if (status == GOT_STATUS_CONFLICT)
3080 return got_error(GOT_ERR_CONFLICTS);
3085 static const struct got_error *
3086 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3087 const char *fileindex_path, struct got_object_id *commit_id1,
3088 struct got_object_id *commit_id2, struct got_repository *repo,
3089 got_worktree_checkout_cb progress_cb, void *progress_arg,
3090 got_cancel_cb cancel_cb, void *cancel_arg)
3092 const struct got_error *err = NULL, *sync_err;
3093 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3094 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3095 struct check_merge_conflicts_arg cmc_arg;
3096 struct merge_file_cb_arg arg;
3097 char *label_orig = NULL;
3100 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3101 worktree->path_prefix);
3102 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3108 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3112 err = got_object_id_str(&id_str, commit_id1);
3116 if (asprintf(&label_orig, "%s: commit %s",
3117 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3118 err = got_error_from_errno("asprintf");
3125 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3126 worktree->path_prefix);
3130 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3134 cmc_arg.worktree = worktree;
3135 cmc_arg.fileindex = fileindex;
3136 cmc_arg.repo = repo;
3137 err = got_diff_tree(tree1, tree2, "", "", repo,
3138 check_merge_conflicts, &cmc_arg, 0);
3142 arg.worktree = worktree;
3143 arg.fileindex = fileindex;
3144 arg.progress_cb = progress_cb;
3145 arg.progress_arg = progress_arg;
3146 arg.cancel_cb = cancel_cb;
3147 arg.cancel_arg = cancel_arg;
3148 arg.label_orig = label_orig;
3149 arg.commit_id2 = commit_id2;
3150 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3151 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3152 sync_err = sync_fileindex(fileindex, fileindex_path);
3153 if (sync_err && err == NULL)
3157 got_object_tree_close(tree1);
3159 got_object_tree_close(tree2);
3164 const struct got_error *
3165 got_worktree_merge_files(struct got_worktree *worktree,
3166 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3167 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3168 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3170 const struct got_error *err, *unlockerr;
3171 char *fileindex_path = NULL;
3172 struct got_fileindex *fileindex = NULL;
3174 err = lock_worktree(worktree, LOCK_EX);
3178 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3182 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3187 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3188 commit_id2, repo, progress_cb, progress_arg,
3189 cancel_cb, cancel_arg);
3192 got_fileindex_free(fileindex);
3193 free(fileindex_path);
3194 unlockerr = lock_worktree(worktree, LOCK_SH);
3195 if (unlockerr && err == NULL)
3200 struct diff_dir_cb_arg {
3201 struct got_fileindex *fileindex;
3202 struct got_worktree *worktree;
3203 const char *status_path;
3204 size_t status_path_len;
3205 struct got_repository *repo;
3206 got_worktree_status_cb status_cb;
3208 got_cancel_cb cancel_cb;
3210 /* A pathlist containing per-directory pathlists of ignore patterns. */
3211 struct got_pathlist_head *ignores;
3212 int report_unchanged;
3216 static const struct got_error *
3217 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3218 int dirfd, const char *de_name,
3219 got_worktree_status_cb status_cb, void *status_arg,
3220 struct got_repository *repo, int report_unchanged)
3222 const struct got_error *err = NULL;
3223 unsigned char status = GOT_STATUS_NO_CHANGE;
3224 unsigned char staged_status = get_staged_status(ie);
3226 struct got_object_id blob_id, commit_id, staged_blob_id;
3227 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3228 struct got_object_id *staged_blob_idp = NULL;
3230 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3234 if (status == GOT_STATUS_NO_CHANGE &&
3235 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3238 if (got_fileindex_entry_has_blob(ie)) {
3239 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3240 blob_idp = &blob_id;
3242 if (got_fileindex_entry_has_commit(ie)) {
3243 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3244 commit_idp = &commit_id;
3246 if (staged_status == GOT_STATUS_ADD ||
3247 staged_status == GOT_STATUS_MODIFY) {
3248 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3249 SHA1_DIGEST_LENGTH);
3250 staged_blob_idp = &staged_blob_id;
3253 return (*status_cb)(status_arg, status, staged_status,
3254 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3257 static const struct got_error *
3258 status_old_new(void *arg, struct got_fileindex_entry *ie,
3259 struct dirent *de, const char *parent_path, int dirfd)
3261 const struct got_error *err = NULL;
3262 struct diff_dir_cb_arg *a = arg;
3265 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3266 return got_error(GOT_ERR_CANCELLED);
3268 if (got_path_cmp(parent_path, a->status_path,
3269 strlen(parent_path), a->status_path_len) != 0 &&
3270 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3273 if (parent_path[0]) {
3274 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3275 parent_path, de->d_name) == -1)
3276 return got_error_from_errno("asprintf");
3278 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3280 return got_error_from_errno("asprintf");
3283 err = report_file_status(ie, abspath, dirfd, de->d_name,
3284 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3289 static const struct got_error *
3290 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3292 struct diff_dir_cb_arg *a = arg;
3293 struct got_object_id blob_id, commit_id;
3294 unsigned char status;
3296 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3297 return got_error(GOT_ERR_CANCELLED);
3299 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3302 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3303 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3304 if (got_fileindex_entry_has_file_on_disk(ie))
3305 status = GOT_STATUS_MISSING;
3307 status = GOT_STATUS_DELETE;
3308 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3309 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3313 free_ignorelist(struct got_pathlist_head *ignorelist)
3315 struct got_pathlist_entry *pe;
3317 TAILQ_FOREACH(pe, ignorelist, entry)
3318 free((char *)pe->path);
3319 got_pathlist_free(ignorelist);
3323 free_ignores(struct got_pathlist_head *ignores)
3325 struct got_pathlist_entry *pe;
3327 TAILQ_FOREACH(pe, ignores, entry) {
3328 struct got_pathlist_head *ignorelist = pe->data;
3329 free_ignorelist(ignorelist);
3330 free((char *)pe->path);
3332 got_pathlist_free(ignores);
3335 static const struct got_error *
3336 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3338 const struct got_error *err = NULL;
3339 struct got_pathlist_entry *pe = NULL;
3340 struct got_pathlist_head *ignorelist;
3341 char *line = NULL, *pattern, *dirpath = NULL;
3342 size_t linesize = 0;
3345 ignorelist = calloc(1, sizeof(*ignorelist));
3346 if (ignorelist == NULL)
3347 return got_error_from_errno("calloc");
3348 TAILQ_INIT(ignorelist);
3350 while ((linelen = getline(&line, &linesize, f)) != -1) {
3351 if (linelen > 0 && line[linelen - 1] == '\n')
3352 line[linelen - 1] = '\0';
3354 /* Git's ignores may contain comments. */
3358 /* Git's negated patterns are not (yet?) supported. */
3362 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3364 err = got_error_from_errno("asprintf");
3367 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3372 err = got_error_from_errno("getline");
3376 dirpath = strdup(path);
3377 if (dirpath == NULL) {
3378 err = got_error_from_errno("strdup");
3381 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3384 if (err || pe == NULL) {
3386 free_ignorelist(ignorelist);
3392 match_ignores(struct got_pathlist_head *ignores, const char *path)
3394 struct got_pathlist_entry *pe;
3396 /* Handle patterns which match in all directories. */
3397 TAILQ_FOREACH(pe, ignores, entry) {
3398 struct got_pathlist_head *ignorelist = pe->data;
3399 struct got_pathlist_entry *pi;
3401 TAILQ_FOREACH(pi, ignorelist, entry) {
3402 const char *p, *pattern = pi->path;
3404 if (strncmp(pattern, "**/", 3) != 0)
3409 if (fnmatch(pattern, p,
3410 FNM_PATHNAME | FNM_LEADING_DIR)) {
3411 /* Retry in next directory. */
3412 while (*p && *p != '/')
3424 * The ignores pathlist contains ignore lists from children before
3425 * parents, so we can find the most specific ignorelist by walking
3426 * ignores backwards.
3428 pe = TAILQ_LAST(ignores, got_pathlist_head);
3430 if (got_path_is_child(path, pe->path, pe->path_len)) {
3431 struct got_pathlist_head *ignorelist = pe->data;
3432 struct got_pathlist_entry *pi;
3433 TAILQ_FOREACH(pi, ignorelist, entry) {
3434 const char *pattern = pi->path;
3435 int flags = FNM_LEADING_DIR;
3436 if (strstr(pattern, "/**/") == NULL)
3437 flags |= FNM_PATHNAME;
3438 if (fnmatch(pattern, path, flags))
3443 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3449 static const struct got_error *
3450 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3451 const char *path, int dirfd, const char *ignores_filename)
3453 const struct got_error *err = NULL;
3456 FILE *ignoresfile = NULL;
3458 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3459 path[0] ? "/" : "", ignores_filename) == -1)
3460 return got_error_from_errno("asprintf");
3463 fd = openat(dirfd, ignores_filename,
3464 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3466 if (errno != ENOENT && errno != EACCES)
3467 err = got_error_from_errno2("openat",
3470 ignoresfile = fdopen(fd, "r");
3471 if (ignoresfile == NULL)
3472 err = got_error_from_errno2("fdopen",
3476 err = read_ignores(ignores, path, ignoresfile);
3480 ignoresfile = fopen(ignorespath, "re");
3481 if (ignoresfile == NULL) {
3482 if (errno != ENOENT && errno != EACCES)
3483 err = got_error_from_errno2("fopen",
3486 err = read_ignores(ignores, path, ignoresfile);
3489 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3490 err = got_error_from_errno2("fclose", path);
3491 if (fd != -1 && close(fd) == -1 && err == NULL)
3492 err = got_error_from_errno2("close", path);
3497 static const struct got_error *
3498 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3501 const struct got_error *err = NULL;
3502 struct diff_dir_cb_arg *a = arg;
3508 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3509 return got_error(GOT_ERR_CANCELLED);
3511 if (parent_path[0]) {
3512 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3513 return got_error_from_errno("asprintf");
3518 if (de->d_type == DT_DIR) {
3519 if (!a->no_ignores && ignore != NULL &&
3520 match_ignores(a->ignores, path))
3522 } else if (!match_ignores(a->ignores, path) &&
3523 got_path_is_child(path, a->status_path, a->status_path_len))
3524 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3525 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3531 static const struct got_error *
3532 status_traverse(void *arg, const char *path, int dirfd)
3534 const struct got_error *err = NULL;
3535 struct diff_dir_cb_arg *a = arg;
3540 err = add_ignores(a->ignores, a->worktree->root_path,
3541 path, dirfd, ".cvsignore");
3545 err = add_ignores(a->ignores, a->worktree->root_path, path,
3546 dirfd, ".gitignore");
3551 static const struct got_error *
3552 report_single_file_status(const char *path, const char *ondisk_path,
3553 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3554 void *status_arg, struct got_repository *repo, int report_unchanged,
3555 struct got_pathlist_head *ignores, int no_ignores)
3557 struct got_fileindex_entry *ie;
3560 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3562 return report_file_status(ie, ondisk_path, -1, NULL,
3563 status_cb, status_arg, repo, report_unchanged);
3565 if (lstat(ondisk_path, &sb) == -1) {
3566 if (errno != ENOENT)
3567 return got_error_from_errno2("lstat", ondisk_path);
3568 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3569 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3572 if (!no_ignores && match_ignores(ignores, path))
3575 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3576 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3577 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3582 static const struct got_error *
3583 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3584 const char *root_path, const char *path)
3586 const struct got_error *err;
3587 char *parent_path, *next_parent_path = NULL;
3589 err = add_ignores(ignores, root_path, "", -1,
3594 err = add_ignores(ignores, root_path, "", -1,
3599 err = got_path_dirname(&parent_path, path);
3601 if (err->code == GOT_ERR_BAD_PATH)
3602 return NULL; /* cannot traverse parent */
3606 err = add_ignores(ignores, root_path, parent_path, -1,
3610 err = add_ignores(ignores, root_path, parent_path, -1,
3614 err = got_path_dirname(&next_parent_path, parent_path);
3616 if (err->code == GOT_ERR_BAD_PATH)
3617 err = NULL; /* traversed everything */
3620 if (got_path_is_root_dir(parent_path))
3623 parent_path = next_parent_path;
3624 next_parent_path = NULL;
3628 free(next_parent_path);
3632 static const struct got_error *
3633 worktree_status(struct got_worktree *worktree, const char *path,
3634 struct got_fileindex *fileindex, struct got_repository *repo,
3635 got_worktree_status_cb status_cb, void *status_arg,
3636 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3637 int report_unchanged)
3639 const struct got_error *err = NULL;
3641 struct got_fileindex_diff_dir_cb fdiff_cb;
3642 struct diff_dir_cb_arg arg;
3643 char *ondisk_path = NULL;
3644 struct got_pathlist_head ignores;
3646 TAILQ_INIT(&ignores);
3648 if (asprintf(&ondisk_path, "%s%s%s",
3649 worktree->root_path, path[0] ? "/" : "", path) == -1)
3650 return got_error_from_errno("asprintf");
3652 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3654 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3655 !got_err_open_nofollow_on_symlink())
3656 err = got_error_from_errno2("open", ondisk_path);
3659 err = add_ignores_from_parent_paths(&ignores,
3660 worktree->root_path, ondisk_path);
3664 err = report_single_file_status(path, ondisk_path,
3665 fileindex, status_cb, status_arg, repo,
3666 report_unchanged, &ignores, no_ignores);
3669 fdiff_cb.diff_old_new = status_old_new;
3670 fdiff_cb.diff_old = status_old;
3671 fdiff_cb.diff_new = status_new;
3672 fdiff_cb.diff_traverse = status_traverse;
3673 arg.fileindex = fileindex;
3674 arg.worktree = worktree;
3675 arg.status_path = path;
3676 arg.status_path_len = strlen(path);
3678 arg.status_cb = status_cb;
3679 arg.status_arg = status_arg;
3680 arg.cancel_cb = cancel_cb;
3681 arg.cancel_arg = cancel_arg;
3682 arg.report_unchanged = report_unchanged;
3683 arg.no_ignores = no_ignores;
3685 err = add_ignores_from_parent_paths(&ignores,
3686 worktree->root_path, path);
3690 arg.ignores = &ignores;
3691 err = got_fileindex_diff_dir(fileindex, fd,
3692 worktree->root_path, path, repo, &fdiff_cb, &arg);
3695 free_ignores(&ignores);
3696 if (fd != -1 && close(fd) == -1 && err == NULL)
3697 err = got_error_from_errno("close");
3702 const struct got_error *
3703 got_worktree_status(struct got_worktree *worktree,
3704 struct got_pathlist_head *paths, struct got_repository *repo,
3705 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3706 got_cancel_cb cancel_cb, void *cancel_arg)
3708 const struct got_error *err = NULL;
3709 char *fileindex_path = NULL;
3710 struct got_fileindex *fileindex = NULL;
3711 struct got_pathlist_entry *pe;
3713 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3717 TAILQ_FOREACH(pe, paths, entry) {
3718 err = worktree_status(worktree, pe->path, fileindex, repo,
3719 status_cb, status_arg, cancel_cb, cancel_arg,
3724 free(fileindex_path);
3725 got_fileindex_free(fileindex);
3729 const struct got_error *
3730 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3733 const struct got_error *err = NULL;
3734 char *resolved = NULL, *cwd = NULL, *path = NULL;
3737 char *abspath = NULL;
3738 char canonpath[PATH_MAX];
3742 cwd = getcwd(NULL, 0);
3744 return got_error_from_errno("getcwd");
3746 if (lstat(arg, &sb) == -1) {
3747 if (errno != ENOENT) {
3748 err = got_error_from_errno2("lstat", arg);
3753 if (S_ISLNK(sb.st_mode)) {
3755 * We cannot use realpath(3) with symlinks since we want to
3756 * operate on the symlink itself.
3757 * But we can make the path absolute, assuming it is relative
3758 * to the current working directory, and then canonicalize it.
3760 if (!got_path_is_absolute(arg)) {
3761 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3762 err = got_error_from_errno("asprintf");
3767 err = got_canonpath(abspath ? abspath : arg, canonpath,
3771 resolved = strdup(canonpath);
3772 if (resolved == NULL) {
3773 err = got_error_from_errno("strdup");
3777 resolved = realpath(arg, NULL);
3778 if (resolved == NULL) {
3779 if (errno != ENOENT) {
3780 err = got_error_from_errno2("realpath", arg);
3783 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3784 err = got_error_from_errno("asprintf");
3787 err = got_canonpath(abspath, canonpath,
3791 resolved = strdup(canonpath);
3792 if (resolved == NULL) {
3793 err = got_error_from_errno("strdup");
3799 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3800 strlen(got_worktree_get_root_path(worktree)))) {
3801 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3805 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3806 err = got_path_skip_common_ancestor(&path,
3807 got_worktree_get_root_path(worktree), resolved);
3813 err = got_error_from_errno("strdup");
3818 /* XXX status walk can't deal with trailing slash! */
3820 while (len > 0 && path[len - 1] == '/') {
3821 path[len - 1] = '\0';
3835 struct schedule_addition_args {
3836 struct got_worktree *worktree;
3837 struct got_fileindex *fileindex;
3838 got_worktree_checkout_cb progress_cb;
3840 struct got_repository *repo;
3843 static const struct got_error *
3844 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3845 const char *relpath, struct got_object_id *blob_id,
3846 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3847 int dirfd, const char *de_name)
3849 struct schedule_addition_args *a = arg;
3850 const struct got_error *err = NULL;
3851 struct got_fileindex_entry *ie;
3855 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3857 return got_error_from_errno("asprintf");
3859 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3861 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3865 /* Re-adding an existing entry is a no-op. */
3866 if (status == GOT_STATUS_ADD)
3868 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3873 if (status != GOT_STATUS_UNVERSIONED) {
3874 if (status == GOT_STATUS_NONEXISTENT)
3875 err = got_error_set_errno(ENOENT, ondisk_path);
3877 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3881 err = got_fileindex_entry_alloc(&ie, relpath);
3884 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3885 relpath, NULL, NULL, 1);
3887 got_fileindex_entry_free(ie);
3890 err = got_fileindex_entry_add(a->fileindex, ie);
3892 got_fileindex_entry_free(ie);
3899 if (status == GOT_STATUS_ADD)
3901 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3904 const struct got_error *
3905 got_worktree_schedule_add(struct got_worktree *worktree,
3906 struct got_pathlist_head *paths,
3907 got_worktree_checkout_cb progress_cb, void *progress_arg,
3908 struct got_repository *repo, int no_ignores)
3910 struct got_fileindex *fileindex = NULL;
3911 char *fileindex_path = NULL;
3912 const struct got_error *err = NULL, *sync_err, *unlockerr;
3913 struct got_pathlist_entry *pe;
3914 struct schedule_addition_args saa;
3916 err = lock_worktree(worktree, LOCK_EX);
3920 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3924 saa.worktree = worktree;
3925 saa.fileindex = fileindex;
3926 saa.progress_cb = progress_cb;
3927 saa.progress_arg = progress_arg;
3930 TAILQ_FOREACH(pe, paths, entry) {
3931 err = worktree_status(worktree, pe->path, fileindex, repo,
3932 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3936 sync_err = sync_fileindex(fileindex, fileindex_path);
3937 if (sync_err && err == NULL)
3940 free(fileindex_path);
3942 got_fileindex_free(fileindex);
3943 unlockerr = lock_worktree(worktree, LOCK_SH);
3944 if (unlockerr && err == NULL)
3949 struct schedule_deletion_args {
3950 struct got_worktree *worktree;
3951 struct got_fileindex *fileindex;
3952 got_worktree_delete_cb progress_cb;
3954 struct got_repository *repo;
3955 int delete_local_mods;
3957 int ignore_missing_paths;
3958 const char *status_codes;
3961 static const struct got_error *
3962 schedule_for_deletion(void *arg, unsigned char status,
3963 unsigned char staged_status, const char *relpath,
3964 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3965 struct got_object_id *commit_id, int dirfd, const char *de_name)
3967 struct schedule_deletion_args *a = arg;
3968 const struct got_error *err = NULL;
3969 struct got_fileindex_entry *ie = NULL;
3973 if (status == GOT_STATUS_NONEXISTENT) {
3974 if (a->ignore_missing_paths)
3976 return got_error_set_errno(ENOENT, relpath);
3979 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3981 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
3983 staged_status = get_staged_status(ie);
3984 if (staged_status != GOT_STATUS_NO_CHANGE) {
3985 if (staged_status == GOT_STATUS_DELETE)
3987 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3990 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3992 return got_error_from_errno("asprintf");
3994 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3999 if (a->status_codes) {
4000 size_t ncodes = strlen(a->status_codes);
4002 for (i = 0; i < ncodes ; i++) {
4003 if (status == a->status_codes[i])
4007 /* Do not delete files in non-matching status. */
4011 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4012 a->status_codes[i] != GOT_STATUS_MISSING) {
4013 static char msg[64];
4014 snprintf(msg, sizeof(msg),
4015 "invalid status code '%c'", a->status_codes[i]);
4016 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4021 if (status != GOT_STATUS_NO_CHANGE) {
4022 if (status == GOT_STATUS_DELETE)
4024 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4025 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4028 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4029 err = got_error_set_errno(ENOENT, relpath);
4032 if (status != GOT_STATUS_MODIFY &&
4033 status != GOT_STATUS_MISSING) {
4034 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4039 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4043 if (unlinkat(dirfd, de_name, 0) != 0) {
4044 err = got_error_from_errno2("unlinkat",
4048 } else if (unlink(ondisk_path) != 0) {
4049 err = got_error_from_errno2("unlink", ondisk_path);
4053 root_len = strlen(a->worktree->root_path);
4056 err = got_path_dirname(&parent, ondisk_path);
4060 ondisk_path = parent;
4061 if (rmdir(ondisk_path) == -1) {
4062 if (errno != ENOTEMPTY)
4063 err = got_error_from_errno2("rmdir",
4067 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4068 strlen(ondisk_path), root_len) != 0);
4071 got_fileindex_entry_mark_deleted_from_disk(ie);
4076 if (status == GOT_STATUS_DELETE)
4078 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4079 staged_status, relpath);
4082 const struct got_error *
4083 got_worktree_schedule_delete(struct got_worktree *worktree,
4084 struct got_pathlist_head *paths, int delete_local_mods,
4085 const char *status_codes,
4086 got_worktree_delete_cb progress_cb, void *progress_arg,
4087 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4089 struct got_fileindex *fileindex = NULL;
4090 char *fileindex_path = NULL;
4091 const struct got_error *err = NULL, *sync_err, *unlockerr;
4092 struct got_pathlist_entry *pe;
4093 struct schedule_deletion_args sda;
4095 err = lock_worktree(worktree, LOCK_EX);
4099 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4103 sda.worktree = worktree;
4104 sda.fileindex = fileindex;
4105 sda.progress_cb = progress_cb;
4106 sda.progress_arg = progress_arg;
4108 sda.delete_local_mods = delete_local_mods;
4109 sda.keep_on_disk = keep_on_disk;
4110 sda.ignore_missing_paths = ignore_missing_paths;
4111 sda.status_codes = status_codes;
4113 TAILQ_FOREACH(pe, paths, entry) {
4114 err = worktree_status(worktree, pe->path, fileindex, repo,
4115 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4119 sync_err = sync_fileindex(fileindex, fileindex_path);
4120 if (sync_err && err == NULL)
4123 free(fileindex_path);
4125 got_fileindex_free(fileindex);
4126 unlockerr = lock_worktree(worktree, LOCK_SH);
4127 if (unlockerr && err == NULL)
4132 static const struct got_error *
4133 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4135 const struct got_error *err = NULL;
4137 size_t linesize = 0, n;
4140 linelen = getline(&line, &linesize, infile);
4141 if (linelen == -1) {
4142 if (ferror(infile)) {
4143 err = got_error_from_errno("getline");
4149 n = fwrite(line, 1, linelen, outfile);
4151 err = got_ferror(outfile, GOT_ERR_IO);
4156 n = fwrite(line, 1, linelen, rejectfile);
4158 err = got_ferror(outfile, GOT_ERR_IO);
4165 static const struct got_error *
4166 skip_one_line(FILE *f)
4169 size_t linesize = 0;
4172 linelen = getline(&line, &linesize, f);
4173 if (linelen == -1) {
4175 return got_error_from_errno("getline");
4182 static const struct got_error *
4183 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4184 int start_old, int end_old, int start_new, int end_new,
4185 FILE *outfile, FILE *rejectfile)
4187 const struct got_error *err;
4189 /* Copy old file's lines leading up to patch. */
4190 while (!feof(f1) && *line_cur1 < start_old) {
4191 err = copy_one_line(f1, outfile, NULL);
4196 /* Skip new file's lines leading up to patch. */
4197 while (!feof(f2) && *line_cur2 < start_new) {
4199 err = copy_one_line(f2, NULL, rejectfile);
4201 err = skip_one_line(f2);
4206 /* Copy patched lines. */
4207 while (!feof(f2) && *line_cur2 <= end_new) {
4208 err = copy_one_line(f2, outfile, NULL);
4213 /* Skip over old file's replaced lines. */
4214 while (!feof(f1) && *line_cur1 <= end_old) {
4216 err = copy_one_line(f1, NULL, rejectfile);
4218 err = skip_one_line(f1);
4227 static const struct got_error *
4228 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4229 FILE *outfile, FILE *rejectfile)
4231 const struct got_error *err;
4234 /* Copy old file's lines until EOF. */
4236 err = copy_one_line(f1, outfile, NULL);
4243 /* Copy new file's lines until EOF. */
4245 err = copy_one_line(f2, NULL, rejectfile);
4255 static const struct got_error *
4256 apply_or_reject_change(int *choice, int *nchunks_used,
4257 struct diff_result *diff_result, int n,
4258 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4259 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4260 got_worktree_patch_cb patch_cb, void *patch_arg)
4262 const struct got_error *err = NULL;
4263 struct diff_chunk_context cc = {};
4264 int start_old, end_old, start_new, end_new;
4266 struct diff_output_unidiff_state *diff_state;
4267 struct diff_input_info diff_info;
4270 *choice = GOT_PATCH_CHOICE_NONE;
4272 /* Get changed line numbers without context lines for copy_change(). */
4273 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4274 start_old = cc.left.start;
4275 end_old = cc.left.end;
4276 start_new = cc.right.start;
4277 end_new = cc.right.end;
4279 /* Get the same change with context lines for display. */
4280 memset(&cc, 0, sizeof(cc));
4281 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4283 memset(&diff_info, 0, sizeof(diff_info));
4284 diff_info.left_path = relpath;
4285 diff_info.right_path = relpath;
4287 diff_state = diff_output_unidiff_state_alloc();
4288 if (diff_state == NULL)
4289 return got_error_set_errno(ENOMEM,
4290 "diff_output_unidiff_state_alloc");
4292 hunkfile = got_opentemp();
4293 if (hunkfile == NULL) {
4294 err = got_error_from_errno("got_opentemp");
4298 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4300 if (rc != DIFF_RC_OK) {
4301 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4305 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4306 err = got_ferror(hunkfile, GOT_ERR_IO);
4310 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4311 hunkfile, changeno, nchanges);
4316 case GOT_PATCH_CHOICE_YES:
4317 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4318 end_old, start_new, end_new, outfile, rejectfile);
4320 case GOT_PATCH_CHOICE_NO:
4321 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4322 end_old, start_new, end_new, rejectfile, outfile);
4324 case GOT_PATCH_CHOICE_QUIT:
4327 err = got_error(GOT_ERR_PATCH_CHOICE);
4331 diff_output_unidiff_state_free(diff_state);
4332 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4333 err = got_error_from_errno("fclose");
4337 struct revert_file_args {
4338 struct got_worktree *worktree;
4339 struct got_fileindex *fileindex;
4340 got_worktree_checkout_cb progress_cb;
4342 got_worktree_patch_cb patch_cb;
4344 struct got_repository *repo;
4345 int unlink_added_files;
4348 static const struct got_error *
4349 create_patched_content(char **path_outfile, int reverse_patch,
4350 struct got_object_id *blob_id, const char *path2,
4351 int dirfd2, const char *de_name2,
4352 const char *relpath, struct got_repository *repo,
4353 got_worktree_patch_cb patch_cb, void *patch_arg)
4355 const struct got_error *err, *free_err;
4356 struct got_blob_object *blob = NULL;
4357 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4359 char link_target[PATH_MAX];
4360 ssize_t link_len = 0;
4361 char *path1 = NULL, *id_str = NULL;
4363 struct got_diffreg_result *diffreg_result = NULL;
4364 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4365 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4367 *path_outfile = NULL;
4369 err = got_object_id_str(&id_str, blob_id);
4374 fd2 = openat(dirfd2, de_name2,
4375 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4377 if (!got_err_open_nofollow_on_symlink()) {
4378 err = got_error_from_errno2("openat", path2);
4381 link_len = readlinkat(dirfd2, de_name2,
4382 link_target, sizeof(link_target));
4383 if (link_len == -1) {
4384 return got_error_from_errno2("readlinkat",
4387 sb2.st_mode = S_IFLNK;
4388 sb2.st_size = link_len;
4391 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4393 if (!got_err_open_nofollow_on_symlink()) {
4394 err = got_error_from_errno2("open", path2);
4397 link_len = readlink(path2, link_target,
4398 sizeof(link_target));
4400 return got_error_from_errno2("readlink", path2);
4401 sb2.st_mode = S_IFLNK;
4402 sb2.st_size = link_len;
4406 if (fstat(fd2, &sb2) == -1) {
4407 err = got_error_from_errno2("fstat", path2);
4411 f2 = fdopen(fd2, "r");
4413 err = got_error_from_errno2("fdopen", path2);
4419 f2 = got_opentemp();
4421 err = got_error_from_errno2("got_opentemp", path2);
4424 n = fwrite(link_target, 1, link_len, f2);
4425 if (n != link_len) {
4426 err = got_ferror(f2, GOT_ERR_IO);
4429 if (fflush(f2) == EOF) {
4430 err = got_error_from_errno("fflush");
4436 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4440 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4444 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4448 err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4453 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4457 if (fseek(f1, 0L, SEEK_SET) == -1)
4458 return got_ferror(f1, GOT_ERR_IO);
4459 if (fseek(f2, 0L, SEEK_SET) == -1)
4460 return got_ferror(f2, GOT_ERR_IO);
4462 /* Count the number of actual changes in the diff result. */
4463 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4464 struct diff_chunk_context cc = {};
4465 diff_chunk_context_load_change(&cc, &nchunks_used,
4466 diffreg_result->result, n, 0);
4469 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4471 err = apply_or_reject_change(&choice, &nchunks_used,
4472 diffreg_result->result, n, relpath, f1, f2,
4473 &line_cur1, &line_cur2,
4474 reverse_patch ? NULL : outfile,
4475 reverse_patch ? outfile : NULL,
4476 ++i, nchanges, patch_cb, patch_arg);
4479 if (choice == GOT_PATCH_CHOICE_YES)
4481 else if (choice == GOT_PATCH_CHOICE_QUIT)
4485 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4486 reverse_patch ? NULL : outfile,
4487 reverse_patch ? outfile : NULL);
4491 if (!S_ISLNK(sb2.st_mode)) {
4492 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4493 err = got_error_from_errno2("fchmod", path2);
4501 got_object_blob_close(blob);
4502 free_err = got_diffreg_result_free(diffreg_result);
4505 if (f1 && fclose(f1) == EOF && err == NULL)
4506 err = got_error_from_errno2("fclose", path1);
4507 if (f2 && fclose(f2) == EOF && err == NULL)
4508 err = got_error_from_errno2("fclose", path2);
4509 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4510 err = got_error_from_errno2("close", path2);
4511 if (outfile && fclose(outfile) == EOF && err == NULL)
4512 err = got_error_from_errno2("fclose", *path_outfile);
4513 if (path1 && unlink(path1) == -1 && err == NULL)
4514 err = got_error_from_errno2("unlink", path1);
4515 if (err || !have_content) {
4516 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4517 err = got_error_from_errno2("unlink", *path_outfile);
4518 free(*path_outfile);
4519 *path_outfile = NULL;
4525 static const struct got_error *
4526 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4527 const char *relpath, struct got_object_id *blob_id,
4528 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4529 int dirfd, const char *de_name)
4531 struct revert_file_args *a = arg;
4532 const struct got_error *err = NULL;
4533 char *parent_path = NULL;
4534 struct got_fileindex_entry *ie;
4535 struct got_tree_object *tree = NULL;
4536 struct got_object_id *tree_id = NULL;
4537 const struct got_tree_entry *te = NULL;
4538 char *tree_path = NULL, *te_name;
4539 char *ondisk_path = NULL, *path_content = NULL;
4540 struct got_blob_object *blob = NULL;
4542 /* Reverting a staged deletion is a no-op. */
4543 if (status == GOT_STATUS_DELETE &&
4544 staged_status != GOT_STATUS_NO_CHANGE)
4547 if (status == GOT_STATUS_UNVERSIONED)
4548 return (*a->progress_cb)(a->progress_arg,
4549 GOT_STATUS_UNVERSIONED, relpath);
4551 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4553 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4555 /* Construct in-repository path of tree which contains this blob. */
4556 err = got_path_dirname(&parent_path, ie->path);
4558 if (err->code != GOT_ERR_BAD_PATH)
4560 parent_path = strdup("/");
4561 if (parent_path == NULL) {
4562 err = got_error_from_errno("strdup");
4566 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4567 tree_path = strdup(parent_path);
4568 if (tree_path == NULL) {
4569 err = got_error_from_errno("strdup");
4573 if (got_path_is_root_dir(parent_path)) {
4574 tree_path = strdup(a->worktree->path_prefix);
4575 if (tree_path == NULL) {
4576 err = got_error_from_errno("strdup");
4580 if (asprintf(&tree_path, "%s/%s",
4581 a->worktree->path_prefix, parent_path) == -1) {
4582 err = got_error_from_errno("asprintf");
4588 err = got_object_id_by_path(&tree_id, a->repo,
4589 a->worktree->base_commit_id, tree_path);
4591 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4592 (status == GOT_STATUS_ADD ||
4593 staged_status == GOT_STATUS_ADD)))
4596 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4600 err = got_path_basename(&te_name, ie->path);
4604 te = got_object_tree_find_entry(tree, te_name);
4606 if (te == NULL && status != GOT_STATUS_ADD &&
4607 staged_status != GOT_STATUS_ADD) {
4608 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4614 case GOT_STATUS_ADD:
4616 int choice = GOT_PATCH_CHOICE_NONE;
4617 err = (*a->patch_cb)(&choice, a->patch_arg,
4618 status, ie->path, NULL, 1, 1);
4621 if (choice != GOT_PATCH_CHOICE_YES)
4624 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4628 got_fileindex_entry_remove(a->fileindex, ie);
4629 if (a->unlink_added_files) {
4630 if (asprintf(&ondisk_path, "%s/%s",
4631 got_worktree_get_root_path(a->worktree),
4633 err = got_error_from_errno("asprintf");
4636 if (unlink(ondisk_path) == -1) {
4637 err = got_error_from_errno2("unlink",
4643 case GOT_STATUS_DELETE:
4645 int choice = GOT_PATCH_CHOICE_NONE;
4646 err = (*a->patch_cb)(&choice, a->patch_arg,
4647 status, ie->path, NULL, 1, 1);
4650 if (choice != GOT_PATCH_CHOICE_YES)
4654 case GOT_STATUS_MODIFY:
4655 case GOT_STATUS_MODE_CHANGE:
4656 case GOT_STATUS_CONFLICT:
4657 case GOT_STATUS_MISSING: {
4658 struct got_object_id id;
4659 if (staged_status == GOT_STATUS_ADD ||
4660 staged_status == GOT_STATUS_MODIFY) {
4661 memcpy(id.sha1, ie->staged_blob_sha1,
4662 SHA1_DIGEST_LENGTH);
4664 memcpy(id.sha1, ie->blob_sha1,
4665 SHA1_DIGEST_LENGTH);
4666 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4670 if (asprintf(&ondisk_path, "%s/%s",
4671 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4672 err = got_error_from_errno("asprintf");
4676 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4677 status == GOT_STATUS_CONFLICT)) {
4678 int is_bad_symlink = 0;
4679 err = create_patched_content(&path_content, 1, &id,
4680 ondisk_path, dirfd, de_name, ie->path, a->repo,
4681 a->patch_cb, a->patch_arg);
4682 if (err || path_content == NULL)
4684 if (te && S_ISLNK(te->mode)) {
4685 if (unlink(path_content) == -1) {
4686 err = got_error_from_errno2("unlink",
4690 err = install_symlink(&is_bad_symlink,
4691 a->worktree, ondisk_path, ie->path,
4692 blob, 0, 1, 0, 0, a->repo,
4693 a->progress_cb, a->progress_arg);
4695 if (rename(path_content, ondisk_path) == -1) {
4696 err = got_error_from_errno3("rename",
4697 path_content, ondisk_path);
4702 int is_bad_symlink = 0;
4703 if (te && S_ISLNK(te->mode)) {
4704 err = install_symlink(&is_bad_symlink,
4705 a->worktree, ondisk_path, ie->path,
4706 blob, 0, 1, 0, 0, a->repo,
4707 a->progress_cb, a->progress_arg);
4709 err = install_blob(a->worktree, ondisk_path,
4711 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4712 got_fileindex_perms_to_st(ie), blob,
4713 0, 1, 0, 0, a->repo,
4714 a->progress_cb, a->progress_arg);
4718 if (status == GOT_STATUS_DELETE ||
4719 status == GOT_STATUS_MODE_CHANGE) {
4720 err = got_fileindex_entry_update(ie,
4721 a->worktree->root_fd, relpath,
4723 a->worktree->base_commit_id->sha1, 1);
4727 if (is_bad_symlink) {
4728 got_fileindex_entry_filetype_set(ie,
4729 GOT_FILEIDX_MODE_BAD_SYMLINK);
4743 got_object_blob_close(blob);
4745 got_object_tree_close(tree);
4750 const struct got_error *
4751 got_worktree_revert(struct got_worktree *worktree,
4752 struct got_pathlist_head *paths,
4753 got_worktree_checkout_cb progress_cb, void *progress_arg,
4754 got_worktree_patch_cb patch_cb, void *patch_arg,
4755 struct got_repository *repo)
4757 struct got_fileindex *fileindex = NULL;
4758 char *fileindex_path = NULL;
4759 const struct got_error *err = NULL, *unlockerr = NULL;
4760 const struct got_error *sync_err = NULL;
4761 struct got_pathlist_entry *pe;
4762 struct revert_file_args rfa;
4764 err = lock_worktree(worktree, LOCK_EX);
4768 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4772 rfa.worktree = worktree;
4773 rfa.fileindex = fileindex;
4774 rfa.progress_cb = progress_cb;
4775 rfa.progress_arg = progress_arg;
4776 rfa.patch_cb = patch_cb;
4777 rfa.patch_arg = patch_arg;
4779 rfa.unlink_added_files = 0;
4780 TAILQ_FOREACH(pe, paths, entry) {
4781 err = worktree_status(worktree, pe->path, fileindex, repo,
4782 revert_file, &rfa, NULL, NULL, 1, 0);
4786 sync_err = sync_fileindex(fileindex, fileindex_path);
4787 if (sync_err && err == NULL)
4790 free(fileindex_path);
4792 got_fileindex_free(fileindex);
4793 unlockerr = lock_worktree(worktree, LOCK_SH);
4794 if (unlockerr && err == NULL)
4800 free_commitable(struct got_commitable *ct)
4803 free(ct->in_repo_path);
4804 free(ct->ondisk_path);
4806 free(ct->base_blob_id);
4807 free(ct->staged_blob_id);
4808 free(ct->base_commit_id);
4812 struct collect_commitables_arg {
4813 struct got_pathlist_head *commitable_paths;
4814 struct got_repository *repo;
4815 struct got_worktree *worktree;
4816 struct got_fileindex *fileindex;
4817 int have_staged_files;
4818 int allow_bad_symlinks;
4821 static const struct got_error *
4822 collect_commitables(void *arg, unsigned char status,
4823 unsigned char staged_status, const char *relpath,
4824 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4825 struct got_object_id *commit_id, int dirfd, const char *de_name)
4827 struct collect_commitables_arg *a = arg;
4828 const struct got_error *err = NULL;
4829 struct got_commitable *ct = NULL;
4830 struct got_pathlist_entry *new = NULL;
4831 char *parent_path = NULL, *path = NULL;
4834 if (a->have_staged_files) {
4835 if (staged_status != GOT_STATUS_MODIFY &&
4836 staged_status != GOT_STATUS_ADD &&
4837 staged_status != GOT_STATUS_DELETE)
4840 if (status == GOT_STATUS_CONFLICT)
4841 return got_error(GOT_ERR_COMMIT_CONFLICT);
4843 if (status != GOT_STATUS_MODIFY &&
4844 status != GOT_STATUS_MODE_CHANGE &&
4845 status != GOT_STATUS_ADD &&
4846 status != GOT_STATUS_DELETE)
4850 if (asprintf(&path, "/%s", relpath) == -1) {
4851 err = got_error_from_errno("asprintf");
4854 if (strcmp(path, "/") == 0) {
4855 parent_path = strdup("");
4856 if (parent_path == NULL)
4857 return got_error_from_errno("strdup");
4859 err = got_path_dirname(&parent_path, path);
4864 ct = calloc(1, sizeof(*ct));
4866 err = got_error_from_errno("calloc");
4870 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4872 err = got_error_from_errno("asprintf");
4876 if (staged_status == GOT_STATUS_ADD ||
4877 staged_status == GOT_STATUS_MODIFY) {
4878 struct got_fileindex_entry *ie;
4879 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4880 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4881 case GOT_FILEIDX_MODE_REGULAR_FILE:
4882 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4885 case GOT_FILEIDX_MODE_SYMLINK:
4889 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4892 ct->mode |= got_fileindex_entry_perms_get(ie);
4893 } else if (status != GOT_STATUS_DELETE &&
4894 staged_status != GOT_STATUS_DELETE) {
4896 if (fstatat(dirfd, de_name, &sb,
4897 AT_SYMLINK_NOFOLLOW) == -1) {
4898 err = got_error_from_errno2("fstatat",
4902 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4903 err = got_error_from_errno2("lstat", ct->ondisk_path);
4906 ct->mode = sb.st_mode;
4909 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4910 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4912 err = got_error_from_errno("asprintf");
4916 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4917 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4919 char target_path[PATH_MAX];
4921 target_len = readlink(ct->ondisk_path, target_path,
4922 sizeof(target_path));
4923 if (target_len == -1) {
4924 err = got_error_from_errno2("readlink",
4928 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4929 target_len, ct->ondisk_path, a->worktree->root_path);
4932 if (is_bad_symlink) {
4933 err = got_error_path(ct->ondisk_path,
4934 GOT_ERR_BAD_SYMLINK);
4940 ct->status = status;
4941 ct->staged_status = staged_status;
4942 ct->blob_id = NULL; /* will be filled in when blob gets created */
4943 if (ct->status != GOT_STATUS_ADD &&
4944 ct->staged_status != GOT_STATUS_ADD) {
4945 ct->base_blob_id = got_object_id_dup(blob_id);
4946 if (ct->base_blob_id == NULL) {
4947 err = got_error_from_errno("got_object_id_dup");
4950 ct->base_commit_id = got_object_id_dup(commit_id);
4951 if (ct->base_commit_id == NULL) {
4952 err = got_error_from_errno("got_object_id_dup");
4956 if (ct->staged_status == GOT_STATUS_ADD ||
4957 ct->staged_status == GOT_STATUS_MODIFY) {
4958 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4959 if (ct->staged_blob_id == NULL) {
4960 err = got_error_from_errno("got_object_id_dup");
4964 ct->path = strdup(path);
4965 if (ct->path == NULL) {
4966 err = got_error_from_errno("strdup");
4969 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4971 if (ct && (err || new == NULL))
4972 free_commitable(ct);
4978 static const struct got_error *write_tree(struct got_object_id **, int *,
4979 struct got_tree_object *, const char *, struct got_pathlist_head *,
4980 got_worktree_status_cb status_cb, void *status_arg,
4981 struct got_repository *);
4983 static const struct got_error *
4984 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4985 struct got_tree_entry *te, const char *parent_path,
4986 struct got_pathlist_head *commitable_paths,
4987 got_worktree_status_cb status_cb, void *status_arg,
4988 struct got_repository *repo)
4990 const struct got_error *err = NULL;
4991 struct got_tree_object *subtree;
4994 if (asprintf(&subpath, "%s%s%s", parent_path,
4995 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4996 return got_error_from_errno("asprintf");
4998 err = got_object_open_as_tree(&subtree, repo, &te->id);
5002 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5003 commitable_paths, status_cb, status_arg, repo);
5004 got_object_tree_close(subtree);
5009 static const struct got_error *
5010 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5012 const struct got_error *err = NULL;
5013 char *ct_parent_path = NULL;
5017 if (strchr(ct->in_repo_path, '/') == NULL) {
5018 *match = got_path_is_root_dir(path);
5022 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5025 *match = (strcmp(path, ct_parent_path) == 0);
5026 free(ct_parent_path);
5031 get_ct_file_mode(struct got_commitable *ct)
5033 if (S_ISLNK(ct->mode))
5036 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5039 static const struct got_error *
5040 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5041 struct got_tree_entry *te, struct got_commitable *ct)
5043 const struct got_error *err = NULL;
5047 err = got_object_tree_entry_dup(new_te, te);
5051 (*new_te)->mode = get_ct_file_mode(ct);
5053 if (ct->staged_status == GOT_STATUS_MODIFY)
5054 memcpy(&(*new_te)->id, ct->staged_blob_id,
5055 sizeof((*new_te)->id));
5057 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5059 if (err && *new_te) {
5066 static const struct got_error *
5067 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5068 struct got_commitable *ct)
5070 const struct got_error *err = NULL;
5071 char *ct_name = NULL;
5075 *new_te = calloc(1, sizeof(**new_te));
5076 if (*new_te == NULL)
5077 return got_error_from_errno("calloc");
5079 err = got_path_basename(&ct_name, ct->path);
5082 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5083 sizeof((*new_te)->name)) {
5084 err = got_error(GOT_ERR_NO_SPACE);
5088 (*new_te)->mode = get_ct_file_mode(ct);
5090 if (ct->staged_status == GOT_STATUS_ADD)
5091 memcpy(&(*new_te)->id, ct->staged_blob_id,
5092 sizeof((*new_te)->id));
5094 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5097 if (err && *new_te) {
5104 static const struct got_error *
5105 insert_tree_entry(struct got_tree_entry *new_te,
5106 struct got_pathlist_head *paths)
5108 const struct got_error *err = NULL;
5109 struct got_pathlist_entry *new_pe;
5111 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5115 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5119 static const struct got_error *
5120 report_ct_status(struct got_commitable *ct,
5121 got_worktree_status_cb status_cb, void *status_arg)
5123 const char *ct_path = ct->path;
5124 unsigned char status;
5126 if (status_cb == NULL) /* no commit progress output desired */
5129 while (ct_path[0] == '/')
5132 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5133 status = ct->staged_status;
5135 status = ct->status;
5137 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5138 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5141 static const struct got_error *
5142 match_modified_subtree(int *modified, struct got_tree_entry *te,
5143 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5145 const struct got_error *err = NULL;
5146 struct got_pathlist_entry *pe;
5151 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5152 got_path_is_root_dir(base_tree_path) ? "" : "/",
5154 return got_error_from_errno("asprintf");
5156 TAILQ_FOREACH(pe, commitable_paths, entry) {
5157 struct got_commitable *ct = pe->data;
5158 *modified = got_path_is_child(ct->in_repo_path, te_path,
5168 static const struct got_error *
5169 match_deleted_or_modified_ct(struct got_commitable **ctp,
5170 struct got_tree_entry *te, const char *base_tree_path,
5171 struct got_pathlist_head *commitable_paths)
5173 const struct got_error *err = NULL;
5174 struct got_pathlist_entry *pe;
5178 TAILQ_FOREACH(pe, commitable_paths, entry) {
5179 struct got_commitable *ct = pe->data;
5180 char *ct_name = NULL;
5183 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5184 if (ct->status != GOT_STATUS_MODIFY &&
5185 ct->status != GOT_STATUS_MODE_CHANGE &&
5186 ct->status != GOT_STATUS_DELETE)
5189 if (ct->staged_status != GOT_STATUS_MODIFY &&
5190 ct->staged_status != GOT_STATUS_DELETE)
5194 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5197 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5203 err = got_path_basename(&ct_name, pe->path);
5207 if (strcmp(te->name, ct_name) != 0) {
5220 static const struct got_error *
5221 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5222 const char *child_path, const char *path_base_tree,
5223 struct got_pathlist_head *commitable_paths,
5224 got_worktree_status_cb status_cb, void *status_arg,
5225 struct got_repository *repo)
5227 const struct got_error *err = NULL;
5228 struct got_tree_entry *new_te;
5230 struct got_object_id *id = NULL;
5235 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5236 got_path_is_root_dir(path_base_tree) ? "" : "/",
5238 return got_error_from_errno("asprintf");
5240 new_te = calloc(1, sizeof(*new_te));
5242 return got_error_from_errno("calloc");
5243 new_te->mode = S_IFDIR;
5245 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5246 sizeof(new_te->name)) {
5247 err = got_error(GOT_ERR_NO_SPACE);
5250 err = write_tree(&id, &nentries, NULL, subtree_path,
5251 commitable_paths, status_cb, status_arg, repo);
5256 memcpy(&new_te->id, id, sizeof(new_te->id));
5265 static const struct got_error *
5266 write_tree(struct got_object_id **new_tree_id, int *nentries,
5267 struct got_tree_object *base_tree, const char *path_base_tree,
5268 struct got_pathlist_head *commitable_paths,
5269 got_worktree_status_cb status_cb, void *status_arg,
5270 struct got_repository *repo)
5272 const struct got_error *err = NULL;
5273 struct got_pathlist_head paths;
5274 struct got_tree_entry *te, *new_te = NULL;
5275 struct got_pathlist_entry *pe;
5280 /* Insert, and recurse into, newly added entries first. */
5281 TAILQ_FOREACH(pe, commitable_paths, entry) {
5282 struct got_commitable *ct = pe->data;
5283 char *child_path = NULL, *slash;
5285 if ((ct->status != GOT_STATUS_ADD &&
5286 ct->staged_status != GOT_STATUS_ADD) ||
5287 (ct->flags & GOT_COMMITABLE_ADDED))
5290 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5291 strlen(path_base_tree)))
5294 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5299 slash = strchr(child_path, '/');
5300 if (slash == NULL) {
5301 err = alloc_added_blob_tree_entry(&new_te, ct);
5304 err = report_ct_status(ct, status_cb, status_arg);
5307 ct->flags |= GOT_COMMITABLE_ADDED;
5308 err = insert_tree_entry(new_te, &paths);
5313 *slash = '\0'; /* trim trailing path components */
5314 if (base_tree == NULL ||
5315 got_object_tree_find_entry(base_tree, child_path)
5317 err = make_subtree_for_added_blob(&new_te,
5318 child_path, path_base_tree,
5319 commitable_paths, status_cb, status_arg,
5323 err = insert_tree_entry(new_te, &paths);
5332 int i, nbase_entries;
5333 /* Handle modified and deleted entries. */
5334 nbase_entries = got_object_tree_get_nentries(base_tree);
5335 for (i = 0; i < nbase_entries; i++) {
5336 struct got_commitable *ct = NULL;
5338 te = got_object_tree_get_entry(base_tree, i);
5339 if (got_object_tree_entry_is_submodule(te)) {
5340 /* Entry is a submodule; just copy it. */
5341 err = got_object_tree_entry_dup(&new_te, te);
5344 err = insert_tree_entry(new_te, &paths);
5351 if (S_ISDIR(te->mode)) {
5353 err = got_object_tree_entry_dup(&new_te, te);
5356 err = match_modified_subtree(&modified, te,
5357 path_base_tree, commitable_paths);
5360 /* Avoid recursion into unmodified subtrees. */
5362 struct got_object_id *new_id;
5364 err = write_subtree(&new_id,
5366 path_base_tree, commitable_paths,
5367 status_cb, status_arg, repo);
5370 if (nsubentries == 0) {
5371 /* All entries were deleted. */
5375 memcpy(&new_te->id, new_id,
5376 sizeof(new_te->id));
5379 err = insert_tree_entry(new_te, &paths);
5386 err = match_deleted_or_modified_ct(&ct, te,
5387 path_base_tree, commitable_paths);
5391 /* NB: Deleted entries get dropped here. */
5392 if (ct->status == GOT_STATUS_MODIFY ||
5393 ct->status == GOT_STATUS_MODE_CHANGE ||
5394 ct->staged_status == GOT_STATUS_MODIFY) {
5395 err = alloc_modified_blob_tree_entry(
5399 err = insert_tree_entry(new_te, &paths);
5404 err = report_ct_status(ct, status_cb,
5409 /* Entry is unchanged; just copy it. */
5410 err = got_object_tree_entry_dup(&new_te, te);
5413 err = insert_tree_entry(new_te, &paths);
5421 /* Write new list of entries; deleted entries have been dropped. */
5422 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5424 got_pathlist_free(&paths);
5428 static const struct got_error *
5429 update_fileindex_after_commit(struct got_worktree *worktree,
5430 struct got_pathlist_head *commitable_paths,
5431 struct got_object_id *new_base_commit_id,
5432 struct got_fileindex *fileindex, int have_staged_files)
5434 const struct got_error *err = NULL;
5435 struct got_pathlist_entry *pe;
5436 char *relpath = NULL;
5438 TAILQ_FOREACH(pe, commitable_paths, entry) {
5439 struct got_fileindex_entry *ie;
5440 struct got_commitable *ct = pe->data;
5442 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5444 err = got_path_skip_common_ancestor(&relpath,
5445 worktree->root_path, ct->ondisk_path);
5450 if (ct->status == GOT_STATUS_DELETE ||
5451 ct->staged_status == GOT_STATUS_DELETE) {
5452 got_fileindex_entry_remove(fileindex, ie);
5453 } else if (ct->staged_status == GOT_STATUS_ADD ||
5454 ct->staged_status == GOT_STATUS_MODIFY) {
5455 got_fileindex_entry_stage_set(ie,
5456 GOT_FILEIDX_STAGE_NONE);
5457 got_fileindex_entry_staged_filetype_set(ie, 0);
5459 err = got_fileindex_entry_update(ie,
5460 worktree->root_fd, relpath,
5461 ct->staged_blob_id->sha1,
5462 new_base_commit_id->sha1,
5463 !have_staged_files);
5465 err = got_fileindex_entry_update(ie,
5466 worktree->root_fd, relpath,
5468 new_base_commit_id->sha1,
5469 !have_staged_files);
5471 err = got_fileindex_entry_alloc(&ie, pe->path);
5474 err = got_fileindex_entry_update(ie,
5475 worktree->root_fd, relpath, ct->blob_id->sha1,
5476 new_base_commit_id->sha1, 1);
5478 got_fileindex_entry_free(ie);
5481 err = got_fileindex_entry_add(fileindex, ie);
5483 got_fileindex_entry_free(ie);
5496 static const struct got_error *
5497 check_out_of_date(const char *in_repo_path, unsigned char status,
5498 unsigned char staged_status, struct got_object_id *base_blob_id,
5499 struct got_object_id *base_commit_id,
5500 struct got_object_id *head_commit_id, struct got_repository *repo,
5503 const struct got_error *err = NULL;
5504 struct got_object_id *id = NULL;
5506 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5507 /* Trivial case: base commit == head commit */
5508 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5511 * Ensure file content which local changes were based
5512 * on matches file content in the branch head.
5514 err = got_object_id_by_path(&id, repo, head_commit_id,
5517 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5518 err = got_error(ood_errcode);
5520 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5521 err = got_error(ood_errcode);
5523 /* Require that added files don't exist in the branch head. */
5524 err = got_object_id_by_path(&id, repo, head_commit_id,
5526 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5528 err = id ? got_error(ood_errcode) : NULL;
5535 const struct got_error *
5536 commit_worktree(struct got_object_id **new_commit_id,
5537 struct got_pathlist_head *commitable_paths,
5538 struct got_object_id *head_commit_id,
5539 struct got_object_id *parent_id2,
5540 struct got_worktree *worktree,
5541 const char *author, const char *committer,
5542 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5543 got_worktree_status_cb status_cb, void *status_arg,
5544 struct got_repository *repo)
5546 const struct got_error *err = NULL, *unlockerr = NULL;
5547 struct got_pathlist_entry *pe;
5548 const char *head_ref_name = NULL;
5549 struct got_commit_object *head_commit = NULL;
5550 struct got_reference *head_ref2 = NULL;
5551 struct got_object_id *head_commit_id2 = NULL;
5552 struct got_tree_object *head_tree = NULL;
5553 struct got_object_id *new_tree_id = NULL;
5554 int nentries, nparents = 0;
5555 struct got_object_id_queue parent_ids;
5556 struct got_object_qid *pid = NULL;
5557 char *logmsg = NULL;
5559 *new_commit_id = NULL;
5561 STAILQ_INIT(&parent_ids);
5563 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5567 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5571 if (commit_msg_cb != NULL) {
5572 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5577 if (logmsg == NULL || strlen(logmsg) == 0) {
5578 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5582 /* Create blobs from added and modified files and record their IDs. */
5583 TAILQ_FOREACH(pe, commitable_paths, entry) {
5584 struct got_commitable *ct = pe->data;
5587 /* Blobs for staged files already exist. */
5588 if (ct->staged_status == GOT_STATUS_ADD ||
5589 ct->staged_status == GOT_STATUS_MODIFY)
5592 if (ct->status != GOT_STATUS_ADD &&
5593 ct->status != GOT_STATUS_MODIFY &&
5594 ct->status != GOT_STATUS_MODE_CHANGE)
5597 if (asprintf(&ondisk_path, "%s/%s",
5598 worktree->root_path, pe->path) == -1) {
5599 err = got_error_from_errno("asprintf");
5602 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5608 /* Recursively write new tree objects. */
5609 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5610 commitable_paths, status_cb, status_arg, repo);
5614 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5617 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5620 err = got_object_qid_alloc(&pid, parent_id2);
5623 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5626 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5627 nparents, author, time(NULL), committer, time(NULL), logmsg, repo);
5633 /* Check if a concurrent commit to our branch has occurred. */
5634 head_ref_name = got_worktree_get_head_ref_name(worktree);
5635 if (head_ref_name == NULL) {
5636 err = got_error_from_errno("got_worktree_get_head_ref_name");
5639 /* Lock the reference here to prevent concurrent modification. */
5640 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5643 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5646 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5647 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5650 /* Update branch head in repository. */
5651 err = got_ref_change_ref(head_ref2, *new_commit_id);
5654 err = got_ref_write(head_ref2, repo);
5658 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5662 err = ref_base_commit(worktree, repo);
5666 got_object_id_queue_free(&parent_ids);
5668 got_object_tree_close(head_tree);
5670 got_object_commit_close(head_commit);
5671 free(head_commit_id2);
5673 unlockerr = got_ref_unlock(head_ref2);
5674 if (unlockerr && err == NULL)
5676 got_ref_close(head_ref2);
5681 static const struct got_error *
5682 check_path_is_commitable(const char *path,
5683 struct got_pathlist_head *commitable_paths)
5685 struct got_pathlist_entry *cpe = NULL;
5686 size_t path_len = strlen(path);
5688 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5689 struct got_commitable *ct = cpe->data;
5690 const char *ct_path = ct->path;
5692 while (ct_path[0] == '/')
5695 if (strcmp(path, ct_path) == 0 ||
5696 got_path_is_child(ct_path, path, path_len))
5701 return got_error_path(path, GOT_ERR_BAD_PATH);
5706 static const struct got_error *
5707 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5709 int *have_staged_files = arg;
5711 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5712 *have_staged_files = 1;
5713 return got_error(GOT_ERR_CANCELLED);
5719 static const struct got_error *
5720 check_non_staged_files(struct got_fileindex *fileindex,
5721 struct got_pathlist_head *paths)
5723 struct got_pathlist_entry *pe;
5724 struct got_fileindex_entry *ie;
5726 TAILQ_FOREACH(pe, paths, entry) {
5727 if (pe->path[0] == '\0')
5729 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5731 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5732 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5733 return got_error_path(pe->path,
5734 GOT_ERR_FILE_NOT_STAGED);
5740 const struct got_error *
5741 got_worktree_commit(struct got_object_id **new_commit_id,
5742 struct got_worktree *worktree, struct got_pathlist_head *paths,
5743 const char *author, const char *committer, int allow_bad_symlinks,
5744 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5745 got_worktree_status_cb status_cb, void *status_arg,
5746 struct got_repository *repo)
5748 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5749 struct got_fileindex *fileindex = NULL;
5750 char *fileindex_path = NULL;
5751 struct got_pathlist_head commitable_paths;
5752 struct collect_commitables_arg cc_arg;
5753 struct got_pathlist_entry *pe;
5754 struct got_reference *head_ref = NULL;
5755 struct got_object_id *head_commit_id = NULL;
5756 int have_staged_files = 0;
5758 *new_commit_id = NULL;
5760 TAILQ_INIT(&commitable_paths);
5762 err = lock_worktree(worktree, LOCK_EX);
5766 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5770 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5774 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5778 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5779 &have_staged_files);
5780 if (err && err->code != GOT_ERR_CANCELLED)
5782 if (have_staged_files) {
5783 err = check_non_staged_files(fileindex, paths);
5788 cc_arg.commitable_paths = &commitable_paths;
5789 cc_arg.worktree = worktree;
5790 cc_arg.fileindex = fileindex;
5792 cc_arg.have_staged_files = have_staged_files;
5793 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5794 TAILQ_FOREACH(pe, paths, entry) {
5795 err = worktree_status(worktree, pe->path, fileindex, repo,
5796 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
5801 if (TAILQ_EMPTY(&commitable_paths)) {
5802 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5806 TAILQ_FOREACH(pe, paths, entry) {
5807 err = check_path_is_commitable(pe->path, &commitable_paths);
5812 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5813 struct got_commitable *ct = pe->data;
5814 const char *ct_path = ct->in_repo_path;
5816 while (ct_path[0] == '/')
5818 err = check_out_of_date(ct_path, ct->status,
5819 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5820 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5826 err = commit_worktree(new_commit_id, &commitable_paths,
5827 head_commit_id, NULL, worktree, author, committer,
5828 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5832 err = update_fileindex_after_commit(worktree, &commitable_paths,
5833 *new_commit_id, fileindex, have_staged_files);
5834 sync_err = sync_fileindex(fileindex, fileindex_path);
5835 if (sync_err && err == NULL)
5839 got_fileindex_free(fileindex);
5840 free(fileindex_path);
5841 unlockerr = lock_worktree(worktree, LOCK_SH);
5842 if (unlockerr && err == NULL)
5844 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5845 struct got_commitable *ct = pe->data;
5846 free_commitable(ct);
5848 got_pathlist_free(&commitable_paths);
5853 got_commitable_get_path(struct got_commitable *ct)
5859 got_commitable_get_status(struct got_commitable *ct)
5864 struct check_rebase_ok_arg {
5865 struct got_worktree *worktree;
5866 struct got_repository *repo;
5869 static const struct got_error *
5870 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5872 const struct got_error *err = NULL;
5873 struct check_rebase_ok_arg *a = arg;
5874 unsigned char status;
5878 /* Reject rebase of a work tree with mixed base commits. */
5879 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5880 SHA1_DIGEST_LENGTH))
5881 return got_error(GOT_ERR_MIXED_COMMITS);
5883 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5885 return got_error_from_errno("asprintf");
5887 /* Reject rebase of a work tree with modified or staged files. */
5888 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5893 if (status != GOT_STATUS_NO_CHANGE)
5894 return got_error(GOT_ERR_MODIFIED);
5895 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5896 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5901 const struct got_error *
5902 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5903 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5904 struct got_worktree *worktree, struct got_reference *branch,
5905 struct got_repository *repo)
5907 const struct got_error *err = NULL;
5908 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5909 char *branch_ref_name = NULL;
5910 char *fileindex_path = NULL;
5911 struct check_rebase_ok_arg ok_arg;
5912 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5913 struct got_object_id *wt_branch_tip = NULL;
5915 *new_base_branch_ref = NULL;
5919 err = lock_worktree(worktree, LOCK_EX);
5923 err = open_fileindex(fileindex, &fileindex_path, worktree);
5927 ok_arg.worktree = worktree;
5929 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5934 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5938 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5942 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5946 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5951 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5954 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5955 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5959 err = got_ref_alloc_symref(new_base_branch_ref,
5960 new_base_branch_ref_name, wt_branch);
5963 err = got_ref_write(*new_base_branch_ref, repo);
5967 /* TODO Lock original branch's ref while rebasing? */
5969 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5973 err = got_ref_write(branch_ref, repo);
5977 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5978 worktree->base_commit_id);
5981 err = got_ref_write(*tmp_branch, repo);
5985 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5989 free(fileindex_path);
5990 free(tmp_branch_name);
5991 free(new_base_branch_ref_name);
5992 free(branch_ref_name);
5994 got_ref_close(branch_ref);
5996 got_ref_close(wt_branch);
5997 free(wt_branch_tip);
5999 if (*new_base_branch_ref) {
6000 got_ref_close(*new_base_branch_ref);
6001 *new_base_branch_ref = NULL;
6004 got_ref_close(*tmp_branch);
6008 got_fileindex_free(*fileindex);
6011 lock_worktree(worktree, LOCK_SH);
6016 const struct got_error *
6017 got_worktree_rebase_continue(struct got_object_id **commit_id,
6018 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6019 struct got_reference **branch, struct got_fileindex **fileindex,
6020 struct got_worktree *worktree, struct got_repository *repo)
6022 const struct got_error *err;
6023 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6024 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6025 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6026 char *fileindex_path = NULL;
6027 int have_staged_files = 0;
6030 *new_base_branch = NULL;
6035 err = lock_worktree(worktree, LOCK_EX);
6039 err = open_fileindex(fileindex, &fileindex_path, worktree);
6043 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6044 &have_staged_files);
6045 if (err && err->code != GOT_ERR_CANCELLED)
6047 if (have_staged_files) {
6048 err = got_error(GOT_ERR_STAGED_PATHS);
6052 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6056 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6060 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6064 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6068 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6072 err = got_ref_open(branch, repo,
6073 got_ref_get_symref_target(branch_ref), 0);
6077 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6081 err = got_ref_resolve(commit_id, repo, commit_ref);
6085 err = got_ref_open(new_base_branch, repo,
6086 new_base_branch_ref_name, 0);
6090 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6094 free(commit_ref_name);
6095 free(branch_ref_name);
6096 free(fileindex_path);
6098 got_ref_close(commit_ref);
6100 got_ref_close(branch_ref);
6105 got_ref_close(*tmp_branch);
6108 if (*new_base_branch) {
6109 got_ref_close(*new_base_branch);
6110 *new_base_branch = NULL;
6113 got_ref_close(*branch);
6117 got_fileindex_free(*fileindex);
6120 lock_worktree(worktree, LOCK_SH);
6125 const struct got_error *
6126 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6128 const struct got_error *err;
6129 char *tmp_branch_name = NULL;
6131 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6135 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6136 free(tmp_branch_name);
6140 static const struct got_error *
6141 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6142 char **logmsg, void *arg)
6148 static const struct got_error *
6149 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6150 const char *path, struct got_object_id *blob_id,
6151 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6152 int dirfd, const char *de_name)
6157 struct collect_merged_paths_arg {
6158 got_worktree_checkout_cb progress_cb;
6160 struct got_pathlist_head *merged_paths;
6163 static const struct got_error *
6164 collect_merged_paths(void *arg, unsigned char status, const char *path)
6166 const struct got_error *err;
6167 struct collect_merged_paths_arg *a = arg;
6169 struct got_pathlist_entry *new;
6171 err = (*a->progress_cb)(a->progress_arg, status, path);
6175 if (status != GOT_STATUS_MERGE &&
6176 status != GOT_STATUS_ADD &&
6177 status != GOT_STATUS_DELETE &&
6178 status != GOT_STATUS_CONFLICT)
6183 return got_error_from_errno("strdup");
6185 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6186 if (err || new == NULL)
6192 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6194 struct got_pathlist_entry *pe;
6196 TAILQ_FOREACH(pe, merged_paths, entry)
6197 free((char *)pe->path);
6199 got_pathlist_free(merged_paths);
6202 static const struct got_error *
6203 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6204 int is_rebase, struct got_repository *repo)
6206 const struct got_error *err;
6207 struct got_reference *commit_ref = NULL;
6209 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6211 if (err->code != GOT_ERR_NOT_REF)
6213 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6216 err = got_ref_write(commit_ref, repo);
6219 } else if (is_rebase) {
6220 struct got_object_id *stored_id;
6223 err = got_ref_resolve(&stored_id, repo, commit_ref);
6226 cmp = got_object_id_cmp(commit_id, stored_id);
6229 err = got_error(GOT_ERR_REBASE_COMMITID);
6235 got_ref_close(commit_ref);
6239 static const struct got_error *
6240 rebase_merge_files(struct got_pathlist_head *merged_paths,
6241 const char *commit_ref_name, struct got_worktree *worktree,
6242 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6243 struct got_object_id *commit_id, struct got_repository *repo,
6244 got_worktree_checkout_cb progress_cb, void *progress_arg,
6245 got_cancel_cb cancel_cb, void *cancel_arg)
6247 const struct got_error *err;
6248 struct got_reference *commit_ref = NULL;
6249 struct collect_merged_paths_arg cmp_arg;
6250 char *fileindex_path;
6252 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6254 err = get_fileindex_path(&fileindex_path, worktree);
6258 cmp_arg.progress_cb = progress_cb;
6259 cmp_arg.progress_arg = progress_arg;
6260 cmp_arg.merged_paths = merged_paths;
6261 err = merge_files(worktree, fileindex, fileindex_path,
6262 parent_commit_id, commit_id, repo, collect_merged_paths,
6263 &cmp_arg, cancel_cb, cancel_arg);
6265 got_ref_close(commit_ref);
6269 const struct got_error *
6270 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6271 struct got_worktree *worktree, struct got_fileindex *fileindex,
6272 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6273 struct got_repository *repo,
6274 got_worktree_checkout_cb progress_cb, void *progress_arg,
6275 got_cancel_cb cancel_cb, void *cancel_arg)
6277 const struct got_error *err;
6278 char *commit_ref_name;
6280 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6284 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6288 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6289 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6290 progress_arg, cancel_cb, cancel_arg);
6292 free(commit_ref_name);
6296 const struct got_error *
6297 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6298 struct got_worktree *worktree, struct got_fileindex *fileindex,
6299 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6300 struct got_repository *repo,
6301 got_worktree_checkout_cb progress_cb, void *progress_arg,
6302 got_cancel_cb cancel_cb, void *cancel_arg)
6304 const struct got_error *err;
6305 char *commit_ref_name;
6307 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6311 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6315 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6316 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6317 progress_arg, cancel_cb, cancel_arg);
6319 free(commit_ref_name);
6323 static const struct got_error *
6324 rebase_commit(struct got_object_id **new_commit_id,
6325 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6326 struct got_worktree *worktree, struct got_fileindex *fileindex,
6327 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6328 const char *new_logmsg, struct got_repository *repo)
6330 const struct got_error *err, *sync_err;
6331 struct got_pathlist_head commitable_paths;
6332 struct collect_commitables_arg cc_arg;
6333 char *fileindex_path = NULL;
6334 struct got_reference *head_ref = NULL;
6335 struct got_object_id *head_commit_id = NULL;
6336 char *logmsg = NULL;
6338 TAILQ_INIT(&commitable_paths);
6339 *new_commit_id = NULL;
6341 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6343 err = get_fileindex_path(&fileindex_path, worktree);
6347 cc_arg.commitable_paths = &commitable_paths;
6348 cc_arg.worktree = worktree;
6350 cc_arg.have_staged_files = 0;
6352 * If possible get the status of individual files directly to
6353 * avoid crawling the entire work tree once per rebased commit.
6355 * Ideally, merged_paths would contain a list of commitables
6356 * we could use so we could skip worktree_status() entirely.
6357 * However, we would then need carefully keep track of cumulative
6358 * effects of operations such as file additions and deletions
6359 * in 'got histedit -f' (folding multiple commits into one),
6360 * and this extra complexity is not really worth it.
6363 struct got_pathlist_entry *pe;
6364 TAILQ_FOREACH(pe, merged_paths, entry) {
6365 err = worktree_status(worktree, pe->path, fileindex,
6366 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6372 err = worktree_status(worktree, "", fileindex, repo,
6373 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6378 if (TAILQ_EMPTY(&commitable_paths)) {
6379 /* No-op change; commit will be elided. */
6380 err = got_ref_delete(commit_ref, repo);
6383 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6387 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6391 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6396 logmsg = strdup(new_logmsg);
6397 if (logmsg == NULL) {
6398 err = got_error_from_errno("strdup");
6402 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6407 /* NB: commit_worktree will call free(logmsg) */
6408 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6409 NULL, worktree, got_object_commit_get_author(orig_commit),
6410 got_object_commit_get_committer(orig_commit),
6411 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6415 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6419 err = got_ref_delete(commit_ref, repo);
6423 err = update_fileindex_after_commit(worktree, &commitable_paths,
6424 *new_commit_id, fileindex, 0);
6425 sync_err = sync_fileindex(fileindex, fileindex_path);
6426 if (sync_err && err == NULL)
6429 free(fileindex_path);
6430 free(head_commit_id);
6432 got_ref_close(head_ref);
6434 free(*new_commit_id);
6435 *new_commit_id = NULL;
6440 const struct got_error *
6441 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6442 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6443 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6444 struct got_commit_object *orig_commit,
6445 struct got_object_id *orig_commit_id, struct got_repository *repo)
6447 const struct got_error *err;
6448 char *commit_ref_name;
6449 struct got_reference *commit_ref = NULL;
6450 struct got_object_id *commit_id = NULL;
6452 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6456 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6459 err = got_ref_resolve(&commit_id, repo, commit_ref);
6462 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6463 err = got_error(GOT_ERR_REBASE_COMMITID);
6467 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6468 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6471 got_ref_close(commit_ref);
6472 free(commit_ref_name);
6477 const struct got_error *
6478 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6479 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6480 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6481 struct got_commit_object *orig_commit,
6482 struct got_object_id *orig_commit_id, const char *new_logmsg,
6483 struct got_repository *repo)
6485 const struct got_error *err;
6486 char *commit_ref_name;
6487 struct got_reference *commit_ref = NULL;
6489 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6493 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6497 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6498 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6501 got_ref_close(commit_ref);
6502 free(commit_ref_name);
6506 const struct got_error *
6507 got_worktree_rebase_postpone(struct got_worktree *worktree,
6508 struct got_fileindex *fileindex)
6511 got_fileindex_free(fileindex);
6512 return lock_worktree(worktree, LOCK_SH);
6515 static const struct got_error *
6516 delete_ref(const char *name, struct got_repository *repo)
6518 const struct got_error *err;
6519 struct got_reference *ref;
6521 err = got_ref_open(&ref, repo, name, 0);
6523 if (err->code == GOT_ERR_NOT_REF)
6528 err = got_ref_delete(ref, repo);
6533 static const struct got_error *
6534 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6536 const struct got_error *err;
6537 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6538 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6540 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6543 err = delete_ref(tmp_branch_name, repo);
6547 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6550 err = delete_ref(new_base_branch_ref_name, repo);
6554 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6557 err = delete_ref(branch_ref_name, repo);
6561 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6564 err = delete_ref(commit_ref_name, repo);
6569 free(tmp_branch_name);
6570 free(new_base_branch_ref_name);
6571 free(branch_ref_name);
6572 free(commit_ref_name);
6576 const struct got_error *
6577 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6578 struct got_object_id *new_commit_id, struct got_repository *repo)
6580 const struct got_error *err;
6581 struct got_reference *ref = NULL;
6582 struct got_object_id *old_commit_id = NULL;
6583 const char *branch_name = NULL;
6584 char *new_id_str = NULL;
6585 char *refname = NULL;
6587 branch_name = got_ref_get_name(branch);
6588 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6589 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6592 err = got_object_id_str(&new_id_str, new_commit_id);
6596 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6597 new_id_str) == -1) {
6598 err = got_error_from_errno("asprintf");
6602 err = got_ref_resolve(&old_commit_id, repo, branch);
6606 err = got_ref_alloc(&ref, refname, old_commit_id);
6610 err = got_ref_write(ref, repo);
6614 free(old_commit_id);
6620 const struct got_error *
6621 got_worktree_rebase_complete(struct got_worktree *worktree,
6622 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6623 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6624 struct got_repository *repo, int create_backup)
6626 const struct got_error *err, *unlockerr, *sync_err;
6627 struct got_object_id *new_head_commit_id = NULL;
6628 char *fileindex_path = NULL;
6630 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6634 if (create_backup) {
6635 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6636 rebased_branch, new_head_commit_id, repo);
6641 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6645 err = got_ref_write(rebased_branch, repo);
6649 err = got_worktree_set_head_ref(worktree, rebased_branch);
6653 err = delete_rebase_refs(worktree, repo);
6657 err = get_fileindex_path(&fileindex_path, worktree);
6660 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6661 sync_err = sync_fileindex(fileindex, fileindex_path);
6662 if (sync_err && err == NULL)
6665 got_fileindex_free(fileindex);
6666 free(fileindex_path);
6667 free(new_head_commit_id);
6668 unlockerr = lock_worktree(worktree, LOCK_SH);
6669 if (unlockerr && err == NULL)
6674 const struct got_error *
6675 got_worktree_rebase_abort(struct got_worktree *worktree,
6676 struct got_fileindex *fileindex, struct got_repository *repo,
6677 struct got_reference *new_base_branch,
6678 got_worktree_checkout_cb progress_cb, void *progress_arg)
6680 const struct got_error *err, *unlockerr, *sync_err;
6681 struct got_reference *resolved = NULL;
6682 struct got_object_id *commit_id = NULL;
6683 char *fileindex_path = NULL;
6684 struct revert_file_args rfa;
6685 struct got_object_id *tree_id = NULL;
6687 err = lock_worktree(worktree, LOCK_EX);
6691 err = got_ref_open(&resolved, repo,
6692 got_ref_get_symref_target(new_base_branch), 0);
6696 err = got_worktree_set_head_ref(worktree, resolved);
6701 * XXX commits to the base branch could have happened while
6702 * we were busy rebasing; should we store the original commit ID
6703 * when rebase begins and read it back here?
6705 err = got_ref_resolve(&commit_id, repo, resolved);
6709 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6713 err = got_object_id_by_path(&tree_id, repo,
6714 worktree->base_commit_id, worktree->path_prefix);
6718 err = delete_rebase_refs(worktree, repo);
6722 err = get_fileindex_path(&fileindex_path, worktree);
6726 rfa.worktree = worktree;
6727 rfa.fileindex = fileindex;
6728 rfa.progress_cb = progress_cb;
6729 rfa.progress_arg = progress_arg;
6730 rfa.patch_cb = NULL;
6731 rfa.patch_arg = NULL;
6733 rfa.unlink_added_files = 0;
6734 err = worktree_status(worktree, "", fileindex, repo,
6735 revert_file, &rfa, NULL, NULL, 1, 0);
6739 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6740 repo, progress_cb, progress_arg, NULL, NULL);
6742 sync_err = sync_fileindex(fileindex, fileindex_path);
6743 if (sync_err && err == NULL)
6746 got_ref_close(resolved);
6750 got_fileindex_free(fileindex);
6751 free(fileindex_path);
6753 unlockerr = lock_worktree(worktree, LOCK_SH);
6754 if (unlockerr && err == NULL)
6759 const struct got_error *
6760 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6761 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6762 struct got_fileindex **fileindex, struct got_worktree *worktree,
6763 struct got_repository *repo)
6765 const struct got_error *err = NULL;
6766 char *tmp_branch_name = NULL;
6767 char *branch_ref_name = NULL;
6768 char *base_commit_ref_name = NULL;
6769 char *fileindex_path = NULL;
6770 struct check_rebase_ok_arg ok_arg;
6771 struct got_reference *wt_branch = NULL;
6772 struct got_reference *base_commit_ref = NULL;
6776 *base_commit_id = NULL;
6779 err = lock_worktree(worktree, LOCK_EX);
6783 err = open_fileindex(fileindex, &fileindex_path, worktree);
6787 ok_arg.worktree = worktree;
6789 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6794 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6798 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6802 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6807 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6812 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6816 err = got_ref_write(*branch_ref, repo);
6820 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6821 worktree->base_commit_id);
6824 err = got_ref_write(base_commit_ref, repo);
6827 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6828 if (*base_commit_id == NULL) {
6829 err = got_error_from_errno("got_object_id_dup");
6833 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6834 worktree->base_commit_id);
6837 err = got_ref_write(*tmp_branch, repo);
6841 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6845 free(fileindex_path);
6846 free(tmp_branch_name);
6847 free(branch_ref_name);
6848 free(base_commit_ref_name);
6850 got_ref_close(wt_branch);
6853 got_ref_close(*branch_ref);
6857 got_ref_close(*tmp_branch);
6860 free(*base_commit_id);
6862 got_fileindex_free(*fileindex);
6865 lock_worktree(worktree, LOCK_SH);
6870 const struct got_error *
6871 got_worktree_histedit_postpone(struct got_worktree *worktree,
6872 struct got_fileindex *fileindex)
6875 got_fileindex_free(fileindex);
6876 return lock_worktree(worktree, LOCK_SH);
6879 const struct got_error *
6880 got_worktree_histedit_in_progress(int *in_progress,
6881 struct got_worktree *worktree)
6883 const struct got_error *err;
6884 char *tmp_branch_name = NULL;
6886 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6890 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6891 free(tmp_branch_name);
6895 const struct got_error *
6896 got_worktree_histedit_continue(struct got_object_id **commit_id,
6897 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6898 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6899 struct got_worktree *worktree, struct got_repository *repo)
6901 const struct got_error *err;
6902 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6903 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6904 struct got_reference *commit_ref = NULL;
6905 struct got_reference *base_commit_ref = NULL;
6906 char *fileindex_path = NULL;
6907 int have_staged_files = 0;
6911 *base_commit_id = NULL;
6914 err = lock_worktree(worktree, LOCK_EX);
6918 err = open_fileindex(fileindex, &fileindex_path, worktree);
6922 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6923 &have_staged_files);
6924 if (err && err->code != GOT_ERR_CANCELLED)
6926 if (have_staged_files) {
6927 err = got_error(GOT_ERR_STAGED_PATHS);
6931 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6935 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6939 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6943 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6948 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6952 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6955 err = got_ref_resolve(commit_id, repo, commit_ref);
6959 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6962 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6966 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6970 free(commit_ref_name);
6971 free(branch_ref_name);
6972 free(fileindex_path);
6974 got_ref_close(commit_ref);
6975 if (base_commit_ref)
6976 got_ref_close(base_commit_ref);
6980 free(*base_commit_id);
6981 *base_commit_id = NULL;
6983 got_ref_close(*tmp_branch);
6987 got_fileindex_free(*fileindex);
6990 lock_worktree(worktree, LOCK_EX);
6995 static const struct got_error *
6996 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6998 const struct got_error *err;
6999 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7000 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7002 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7005 err = delete_ref(tmp_branch_name, repo);
7009 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7013 err = delete_ref(base_commit_ref_name, repo);
7017 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7020 err = delete_ref(branch_ref_name, repo);
7024 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7027 err = delete_ref(commit_ref_name, repo);
7031 free(tmp_branch_name);
7032 free(base_commit_ref_name);
7033 free(branch_ref_name);
7034 free(commit_ref_name);
7038 const struct got_error *
7039 got_worktree_histedit_abort(struct got_worktree *worktree,
7040 struct got_fileindex *fileindex, struct got_repository *repo,
7041 struct got_reference *branch, struct got_object_id *base_commit_id,
7042 got_worktree_checkout_cb progress_cb, void *progress_arg)
7044 const struct got_error *err, *unlockerr, *sync_err;
7045 struct got_reference *resolved = NULL;
7046 char *fileindex_path = NULL;
7047 struct got_object_id *tree_id = NULL;
7048 struct revert_file_args rfa;
7050 err = lock_worktree(worktree, LOCK_EX);
7054 err = got_ref_open(&resolved, repo,
7055 got_ref_get_symref_target(branch), 0);
7059 err = got_worktree_set_head_ref(worktree, resolved);
7063 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7067 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
7068 worktree->path_prefix);
7072 err = delete_histedit_refs(worktree, repo);
7076 err = get_fileindex_path(&fileindex_path, worktree);
7080 rfa.worktree = worktree;
7081 rfa.fileindex = fileindex;
7082 rfa.progress_cb = progress_cb;
7083 rfa.progress_arg = progress_arg;
7084 rfa.patch_cb = NULL;
7085 rfa.patch_arg = NULL;
7087 rfa.unlink_added_files = 0;
7088 err = worktree_status(worktree, "", fileindex, repo,
7089 revert_file, &rfa, NULL, NULL, 1, 0);
7093 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7094 repo, progress_cb, progress_arg, NULL, NULL);
7096 sync_err = sync_fileindex(fileindex, fileindex_path);
7097 if (sync_err && err == NULL)
7100 got_ref_close(resolved);
7102 free(fileindex_path);
7104 unlockerr = lock_worktree(worktree, LOCK_SH);
7105 if (unlockerr && err == NULL)
7110 const struct got_error *
7111 got_worktree_histedit_complete(struct got_worktree *worktree,
7112 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7113 struct got_reference *edited_branch, struct got_repository *repo)
7115 const struct got_error *err, *unlockerr, *sync_err;
7116 struct got_object_id *new_head_commit_id = NULL;
7117 struct got_reference *resolved = NULL;
7118 char *fileindex_path = NULL;
7120 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7124 err = got_ref_open(&resolved, repo,
7125 got_ref_get_symref_target(edited_branch), 0);
7129 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7130 resolved, new_head_commit_id, repo);
7134 err = got_ref_change_ref(resolved, new_head_commit_id);
7138 err = got_ref_write(resolved, repo);
7142 err = got_worktree_set_head_ref(worktree, resolved);
7146 err = delete_histedit_refs(worktree, repo);
7150 err = get_fileindex_path(&fileindex_path, worktree);
7153 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7154 sync_err = sync_fileindex(fileindex, fileindex_path);
7155 if (sync_err && err == NULL)
7158 got_fileindex_free(fileindex);
7159 free(fileindex_path);
7160 free(new_head_commit_id);
7161 unlockerr = lock_worktree(worktree, LOCK_SH);
7162 if (unlockerr && err == NULL)
7167 const struct got_error *
7168 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7169 struct got_object_id *commit_id, struct got_repository *repo)
7171 const struct got_error *err;
7172 char *commit_ref_name;
7174 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7178 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7182 err = delete_ref(commit_ref_name, repo);
7184 free(commit_ref_name);
7188 const struct got_error *
7189 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7190 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7191 struct got_worktree *worktree, const char *refname,
7192 struct got_repository *repo)
7194 const struct got_error *err = NULL;
7195 char *fileindex_path = NULL;
7196 struct check_rebase_ok_arg ok_arg;
7200 *base_branch_ref = NULL;
7202 err = lock_worktree(worktree, LOCK_EX);
7206 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7207 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7208 "cannot integrate a branch into itself; "
7209 "update -b or different branch name required");
7213 err = open_fileindex(fileindex, &fileindex_path, worktree);
7217 /* Preconditions are the same as for rebase. */
7218 ok_arg.worktree = worktree;
7220 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7225 err = got_ref_open(branch_ref, repo, refname, 1);
7229 err = got_ref_open(base_branch_ref, repo,
7230 got_worktree_get_head_ref_name(worktree), 1);
7234 got_ref_close(*branch_ref);
7237 if (*base_branch_ref) {
7238 got_ref_close(*base_branch_ref);
7239 *base_branch_ref = NULL;
7242 got_fileindex_free(*fileindex);
7245 lock_worktree(worktree, LOCK_SH);
7250 const struct got_error *
7251 got_worktree_integrate_continue(struct got_worktree *worktree,
7252 struct got_fileindex *fileindex, struct got_repository *repo,
7253 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7254 got_worktree_checkout_cb progress_cb, void *progress_arg,
7255 got_cancel_cb cancel_cb, void *cancel_arg)
7257 const struct got_error *err = NULL, *sync_err, *unlockerr;
7258 char *fileindex_path = NULL;
7259 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7261 err = get_fileindex_path(&fileindex_path, worktree);
7265 err = got_ref_resolve(&commit_id, repo, branch_ref);
7269 err = got_object_id_by_path(&tree_id, repo, commit_id,
7270 worktree->path_prefix);
7274 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7278 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7279 progress_cb, progress_arg, cancel_cb, cancel_arg);
7283 err = got_ref_change_ref(base_branch_ref, commit_id);
7287 err = got_ref_write(base_branch_ref, repo);
7291 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7293 sync_err = sync_fileindex(fileindex, fileindex_path);
7294 if (sync_err && err == NULL)
7298 unlockerr = got_ref_unlock(branch_ref);
7299 if (unlockerr && err == NULL)
7301 got_ref_close(branch_ref);
7303 unlockerr = got_ref_unlock(base_branch_ref);
7304 if (unlockerr && err == NULL)
7306 got_ref_close(base_branch_ref);
7308 got_fileindex_free(fileindex);
7309 free(fileindex_path);
7312 unlockerr = lock_worktree(worktree, LOCK_SH);
7313 if (unlockerr && err == NULL)
7318 const struct got_error *
7319 got_worktree_integrate_abort(struct got_worktree *worktree,
7320 struct got_fileindex *fileindex, struct got_repository *repo,
7321 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7323 const struct got_error *err = NULL, *unlockerr = NULL;
7325 got_fileindex_free(fileindex);
7327 err = lock_worktree(worktree, LOCK_SH);
7329 unlockerr = got_ref_unlock(branch_ref);
7330 if (unlockerr && err == NULL)
7332 got_ref_close(branch_ref);
7334 unlockerr = got_ref_unlock(base_branch_ref);
7335 if (unlockerr && err == NULL)
7337 got_ref_close(base_branch_ref);
7342 const struct got_error *
7343 got_worktree_merge_postpone(struct got_worktree *worktree,
7344 struct got_fileindex *fileindex)
7346 const struct got_error *err, *sync_err;
7347 char *fileindex_path = NULL;
7349 err = get_fileindex_path(&fileindex_path, worktree);
7353 sync_err = sync_fileindex(fileindex, fileindex_path);
7355 err = lock_worktree(worktree, LOCK_SH);
7356 if (sync_err && err == NULL)
7359 got_fileindex_free(fileindex);
7360 free(fileindex_path);
7364 static const struct got_error *
7365 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7367 const struct got_error *err;
7368 char *branch_refname = NULL, *commit_refname = NULL;
7370 err = get_merge_branch_ref_name(&branch_refname, worktree);
7373 err = delete_ref(branch_refname, repo);
7377 err = get_merge_commit_ref_name(&commit_refname, worktree);
7380 err = delete_ref(commit_refname, repo);
7385 free(branch_refname);
7386 free(commit_refname);
7390 struct merge_commit_msg_arg {
7391 struct got_worktree *worktree;
7392 const char *branch_name;
7395 static const struct got_error *
7396 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths, char **logmsg,
7399 struct merge_commit_msg_arg *a = arg;
7401 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7402 got_worktree_get_head_ref_name(a->worktree)) == -1)
7403 return got_error_from_errno("asprintf");
7409 const struct got_error *
7410 got_worktree_merge_branch(struct got_worktree *worktree,
7411 struct got_fileindex *fileindex,
7412 struct got_object_id *yca_commit_id,
7413 struct got_object_id *branch_tip,
7414 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7415 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7417 const struct got_error *err;
7418 char *fileindex_path = NULL;
7420 err = get_fileindex_path(&fileindex_path, worktree);
7424 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7429 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7430 branch_tip, repo, progress_cb, progress_arg,
7431 cancel_cb, cancel_arg);
7433 free(fileindex_path);
7437 const struct got_error *
7438 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7439 struct got_worktree *worktree, struct got_fileindex *fileindex,
7440 const char *author, const char *committer, int allow_bad_symlinks,
7441 struct got_object_id *branch_tip, const char *branch_name,
7442 struct got_repository *repo,
7443 got_worktree_status_cb status_cb, void *status_arg)
7446 const struct got_error *err = NULL, *sync_err;
7447 struct got_pathlist_head commitable_paths;
7448 struct collect_commitables_arg cc_arg;
7449 struct got_pathlist_entry *pe;
7450 struct got_reference *head_ref = NULL;
7451 struct got_object_id *head_commit_id = NULL;
7452 int have_staged_files = 0;
7453 struct merge_commit_msg_arg mcm_arg;
7454 char *fileindex_path = NULL;
7456 *new_commit_id = NULL;
7458 TAILQ_INIT(&commitable_paths);
7460 err = get_fileindex_path(&fileindex_path, worktree);
7464 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7468 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7472 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7473 &have_staged_files);
7474 if (err && err->code != GOT_ERR_CANCELLED)
7476 if (have_staged_files) {
7477 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7481 cc_arg.commitable_paths = &commitable_paths;
7482 cc_arg.worktree = worktree;
7483 cc_arg.fileindex = fileindex;
7485 cc_arg.have_staged_files = have_staged_files;
7486 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7487 err = worktree_status(worktree, "", fileindex, repo,
7488 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7492 if (TAILQ_EMPTY(&commitable_paths)) {
7493 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7494 "merge of %s cannot proceed", branch_name);
7498 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7499 struct got_commitable *ct = pe->data;
7500 const char *ct_path = ct->in_repo_path;
7502 while (ct_path[0] == '/')
7504 err = check_out_of_date(ct_path, ct->status,
7505 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7506 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7512 mcm_arg.worktree = worktree;
7513 mcm_arg.branch_name = branch_name;
7514 err = commit_worktree(new_commit_id, &commitable_paths,
7515 head_commit_id, branch_tip, worktree, author, committer,
7516 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7520 err = update_fileindex_after_commit(worktree, &commitable_paths,
7521 *new_commit_id, fileindex, have_staged_files);
7522 sync_err = sync_fileindex(fileindex, fileindex_path);
7523 if (sync_err && err == NULL)
7526 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7527 struct got_commitable *ct = pe->data;
7528 free_commitable(ct);
7530 got_pathlist_free(&commitable_paths);
7531 free(fileindex_path);
7535 const struct got_error *
7536 got_worktree_merge_complete(struct got_worktree *worktree,
7537 struct got_fileindex *fileindex, struct got_repository *repo)
7539 const struct got_error *err, *unlockerr, *sync_err;
7540 char *fileindex_path = NULL;
7542 err = delete_merge_refs(worktree, repo);
7546 err = get_fileindex_path(&fileindex_path, worktree);
7549 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7550 sync_err = sync_fileindex(fileindex, fileindex_path);
7551 if (sync_err && err == NULL)
7554 got_fileindex_free(fileindex);
7555 free(fileindex_path);
7556 unlockerr = lock_worktree(worktree, LOCK_SH);
7557 if (unlockerr && err == NULL)
7562 const struct got_error *
7563 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7564 struct got_repository *repo)
7566 const struct got_error *err;
7567 char *branch_refname = NULL;
7568 struct got_reference *branch_ref = NULL;
7572 err = get_merge_branch_ref_name(&branch_refname, worktree);
7575 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7576 free(branch_refname);
7578 if (err->code != GOT_ERR_NOT_REF)
7586 const struct got_error *got_worktree_merge_prepare(
7587 struct got_fileindex **fileindex, struct got_worktree *worktree,
7588 struct got_reference *branch, struct got_repository *repo)
7590 const struct got_error *err = NULL;
7591 char *fileindex_path = NULL;
7592 char *branch_refname = NULL, *commit_refname = NULL;
7593 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
7594 struct got_reference *commit_ref = NULL;
7595 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
7596 struct check_rebase_ok_arg ok_arg;
7600 err = lock_worktree(worktree, LOCK_EX);
7604 err = open_fileindex(fileindex, &fileindex_path, worktree);
7608 /* Preconditions are the same as for rebase. */
7609 ok_arg.worktree = worktree;
7611 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7616 err = get_merge_branch_ref_name(&branch_refname, worktree);
7620 err = get_merge_commit_ref_name(&commit_refname, worktree);
7624 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7629 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
7633 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
7634 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
7638 err = got_ref_resolve(&branch_tip, repo, branch);
7642 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
7645 err = got_ref_write(branch_ref, repo);
7649 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
7652 err = got_ref_write(commit_ref, repo);
7657 free(branch_refname);
7658 free(commit_refname);
7659 free(fileindex_path);
7661 got_ref_close(branch_ref);
7663 got_ref_close(commit_ref);
7665 got_ref_close(wt_branch);
7666 free(wt_branch_tip);
7669 got_fileindex_free(*fileindex);
7672 lock_worktree(worktree, LOCK_SH);
7677 const struct got_error *
7678 got_worktree_merge_continue(char **branch_name,
7679 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
7680 struct got_worktree *worktree, struct got_repository *repo)
7682 const struct got_error *err;
7683 char *commit_refname = NULL, *branch_refname = NULL;
7684 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
7685 char *fileindex_path = NULL;
7686 int have_staged_files = 0;
7688 *branch_name = NULL;
7692 err = lock_worktree(worktree, LOCK_EX);
7696 err = open_fileindex(fileindex, &fileindex_path, worktree);
7700 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7701 &have_staged_files);
7702 if (err && err->code != GOT_ERR_CANCELLED)
7704 if (have_staged_files) {
7705 err = got_error(GOT_ERR_STAGED_PATHS);
7709 err = get_merge_branch_ref_name(&branch_refname, worktree);
7713 err = get_merge_commit_ref_name(&commit_refname, worktree);
7717 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7721 if (!got_ref_is_symbolic(branch_ref)) {
7722 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7723 "%s is not a symbolic reference",
7724 got_ref_get_name(branch_ref));
7727 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
7728 if (*branch_name == NULL) {
7729 err = got_error_from_errno("strdup");
7733 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
7737 err = got_ref_resolve(branch_tip, repo, commit_ref);
7741 free(commit_refname);
7742 free(branch_refname);
7743 free(fileindex_path);
7745 got_ref_close(commit_ref);
7747 got_ref_close(branch_ref);
7751 *branch_name = NULL;
7756 got_fileindex_free(*fileindex);
7759 lock_worktree(worktree, LOCK_SH);
7764 const struct got_error *
7765 got_worktree_merge_abort(struct got_worktree *worktree,
7766 struct got_fileindex *fileindex, struct got_repository *repo,
7767 got_worktree_checkout_cb progress_cb, void *progress_arg)
7769 const struct got_error *err, *unlockerr, *sync_err;
7770 struct got_object_id *commit_id = NULL;
7771 char *fileindex_path = NULL;
7772 struct revert_file_args rfa;
7773 struct got_object_id *tree_id = NULL;
7775 err = got_object_id_by_path(&tree_id, repo,
7776 worktree->base_commit_id, worktree->path_prefix);
7780 err = delete_merge_refs(worktree, repo);
7784 err = get_fileindex_path(&fileindex_path, worktree);
7788 rfa.worktree = worktree;
7789 rfa.fileindex = fileindex;
7790 rfa.progress_cb = progress_cb;
7791 rfa.progress_arg = progress_arg;
7792 rfa.patch_cb = NULL;
7793 rfa.patch_arg = NULL;
7795 rfa.unlink_added_files = 1;
7796 err = worktree_status(worktree, "", fileindex, repo,
7797 revert_file, &rfa, NULL, NULL, 1, 0);
7801 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7802 repo, progress_cb, progress_arg, NULL, NULL);
7804 sync_err = sync_fileindex(fileindex, fileindex_path);
7805 if (sync_err && err == NULL)
7811 got_fileindex_free(fileindex);
7812 free(fileindex_path);
7814 unlockerr = lock_worktree(worktree, LOCK_SH);
7815 if (unlockerr && err == NULL)
7820 struct check_stage_ok_arg {
7821 struct got_object_id *head_commit_id;
7822 struct got_worktree *worktree;
7823 struct got_fileindex *fileindex;
7824 struct got_repository *repo;
7828 const struct got_error *
7829 check_stage_ok(void *arg, unsigned char status,
7830 unsigned char staged_status, const char *relpath,
7831 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7832 struct got_object_id *commit_id, int dirfd, const char *de_name)
7834 struct check_stage_ok_arg *a = arg;
7835 const struct got_error *err = NULL;
7836 struct got_fileindex_entry *ie;
7837 struct got_object_id base_commit_id;
7838 struct got_object_id *base_commit_idp = NULL;
7839 char *in_repo_path = NULL, *p;
7841 if (status == GOT_STATUS_UNVERSIONED ||
7842 status == GOT_STATUS_NO_CHANGE)
7844 if (status == GOT_STATUS_NONEXISTENT)
7845 return got_error_set_errno(ENOENT, relpath);
7847 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7849 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7851 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7852 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7854 return got_error_from_errno("asprintf");
7856 if (got_fileindex_entry_has_commit(ie)) {
7857 memcpy(base_commit_id.sha1, ie->commit_sha1,
7858 SHA1_DIGEST_LENGTH);
7859 base_commit_idp = &base_commit_id;
7862 if (status == GOT_STATUS_CONFLICT) {
7863 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7865 } else if (status != GOT_STATUS_ADD &&
7866 status != GOT_STATUS_MODIFY &&
7867 status != GOT_STATUS_DELETE) {
7868 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7872 a->have_changes = 1;
7877 err = check_out_of_date(p, status, staged_status,
7878 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7879 GOT_ERR_STAGE_OUT_OF_DATE);
7885 struct stage_path_arg {
7886 struct got_worktree *worktree;
7887 struct got_fileindex *fileindex;
7888 struct got_repository *repo;
7889 got_worktree_status_cb status_cb;
7891 got_worktree_patch_cb patch_cb;
7893 int staged_something;
7894 int allow_bad_symlinks;
7897 static const struct got_error *
7898 stage_path(void *arg, unsigned char status,
7899 unsigned char staged_status, const char *relpath,
7900 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7901 struct got_object_id *commit_id, int dirfd, const char *de_name)
7903 struct stage_path_arg *a = arg;
7904 const struct got_error *err = NULL;
7905 struct got_fileindex_entry *ie;
7906 char *ondisk_path = NULL, *path_content = NULL;
7908 struct got_object_id *new_staged_blob_id = NULL;
7911 if (status == GOT_STATUS_UNVERSIONED)
7914 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7916 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7918 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7920 return got_error_from_errno("asprintf");
7923 case GOT_STATUS_ADD:
7924 case GOT_STATUS_MODIFY:
7925 /* XXX could sb.st_mode be passed in by our caller? */
7926 if (lstat(ondisk_path, &sb) == -1) {
7927 err = got_error_from_errno2("lstat", ondisk_path);
7931 if (status == GOT_STATUS_ADD) {
7932 int choice = GOT_PATCH_CHOICE_NONE;
7933 err = (*a->patch_cb)(&choice, a->patch_arg,
7934 status, ie->path, NULL, 1, 1);
7937 if (choice != GOT_PATCH_CHOICE_YES)
7940 err = create_patched_content(&path_content, 0,
7941 staged_blob_id ? staged_blob_id : blob_id,
7942 ondisk_path, dirfd, de_name, ie->path,
7943 a->repo, a->patch_cb, a->patch_arg);
7944 if (err || path_content == NULL)
7948 err = got_object_blob_create(&new_staged_blob_id,
7949 path_content ? path_content : ondisk_path, a->repo);
7952 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7953 SHA1_DIGEST_LENGTH);
7954 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7955 stage = GOT_FILEIDX_STAGE_ADD;
7957 stage = GOT_FILEIDX_STAGE_MODIFY;
7958 got_fileindex_entry_stage_set(ie, stage);
7959 if (S_ISLNK(sb.st_mode)) {
7960 int is_bad_symlink = 0;
7961 if (!a->allow_bad_symlinks) {
7962 char target_path[PATH_MAX];
7964 target_len = readlink(ondisk_path, target_path,
7965 sizeof(target_path));
7966 if (target_len == -1) {
7967 err = got_error_from_errno2("readlink",
7971 err = is_bad_symlink_target(&is_bad_symlink,
7972 target_path, target_len, ondisk_path,
7973 a->worktree->root_path);
7976 if (is_bad_symlink) {
7977 err = got_error_path(ondisk_path,
7978 GOT_ERR_BAD_SYMLINK);
7983 got_fileindex_entry_staged_filetype_set(ie,
7984 GOT_FILEIDX_MODE_BAD_SYMLINK);
7986 got_fileindex_entry_staged_filetype_set(ie,
7987 GOT_FILEIDX_MODE_SYMLINK);
7989 got_fileindex_entry_staged_filetype_set(ie,
7990 GOT_FILEIDX_MODE_REGULAR_FILE);
7992 a->staged_something = 1;
7993 if (a->status_cb == NULL)
7995 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7996 get_staged_status(ie), relpath, blob_id,
7997 new_staged_blob_id, NULL, dirfd, de_name);
7999 case GOT_STATUS_DELETE:
8000 if (staged_status == GOT_STATUS_DELETE)
8003 int choice = GOT_PATCH_CHOICE_NONE;
8004 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8005 ie->path, NULL, 1, 1);
8008 if (choice == GOT_PATCH_CHOICE_NO)
8010 if (choice != GOT_PATCH_CHOICE_YES) {
8011 err = got_error(GOT_ERR_PATCH_CHOICE);
8015 stage = GOT_FILEIDX_STAGE_DELETE;
8016 got_fileindex_entry_stage_set(ie, stage);
8017 a->staged_something = 1;
8018 if (a->status_cb == NULL)
8020 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8021 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8024 case GOT_STATUS_NO_CHANGE:
8026 case GOT_STATUS_CONFLICT:
8027 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8029 case GOT_STATUS_NONEXISTENT:
8030 err = got_error_set_errno(ENOENT, relpath);
8033 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8037 if (path_content && unlink(path_content) == -1 && err == NULL)
8038 err = got_error_from_errno2("unlink", path_content);
8041 free(new_staged_blob_id);
8045 const struct got_error *
8046 got_worktree_stage(struct got_worktree *worktree,
8047 struct got_pathlist_head *paths,
8048 got_worktree_status_cb status_cb, void *status_arg,
8049 got_worktree_patch_cb patch_cb, void *patch_arg,
8050 int allow_bad_symlinks, struct got_repository *repo)
8052 const struct got_error *err = NULL, *sync_err, *unlockerr;
8053 struct got_pathlist_entry *pe;
8054 struct got_fileindex *fileindex = NULL;
8055 char *fileindex_path = NULL;
8056 struct got_reference *head_ref = NULL;
8057 struct got_object_id *head_commit_id = NULL;
8058 struct check_stage_ok_arg oka;
8059 struct stage_path_arg spa;
8061 err = lock_worktree(worktree, LOCK_EX);
8065 err = got_ref_open(&head_ref, repo,
8066 got_worktree_get_head_ref_name(worktree), 0);
8069 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8072 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8076 /* Check pre-conditions before staging anything. */
8077 oka.head_commit_id = head_commit_id;
8078 oka.worktree = worktree;
8079 oka.fileindex = fileindex;
8081 oka.have_changes = 0;
8082 TAILQ_FOREACH(pe, paths, entry) {
8083 err = worktree_status(worktree, pe->path, fileindex, repo,
8084 check_stage_ok, &oka, NULL, NULL, 1, 0);
8088 if (!oka.have_changes) {
8089 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8093 spa.worktree = worktree;
8094 spa.fileindex = fileindex;
8096 spa.patch_cb = patch_cb;
8097 spa.patch_arg = patch_arg;
8098 spa.status_cb = status_cb;
8099 spa.status_arg = status_arg;
8100 spa.staged_something = 0;
8101 spa.allow_bad_symlinks = allow_bad_symlinks;
8102 TAILQ_FOREACH(pe, paths, entry) {
8103 err = worktree_status(worktree, pe->path, fileindex, repo,
8104 stage_path, &spa, NULL, NULL, 1, 0);
8108 if (!spa.staged_something) {
8109 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8113 sync_err = sync_fileindex(fileindex, fileindex_path);
8114 if (sync_err && err == NULL)
8118 got_ref_close(head_ref);
8119 free(head_commit_id);
8120 free(fileindex_path);
8122 got_fileindex_free(fileindex);
8123 unlockerr = lock_worktree(worktree, LOCK_SH);
8124 if (unlockerr && err == NULL)
8129 struct unstage_path_arg {
8130 struct got_worktree *worktree;
8131 struct got_fileindex *fileindex;
8132 struct got_repository *repo;
8133 got_worktree_checkout_cb progress_cb;
8135 got_worktree_patch_cb patch_cb;
8139 static const struct got_error *
8140 create_unstaged_content(char **path_unstaged_content,
8141 char **path_new_staged_content, struct got_object_id *blob_id,
8142 struct got_object_id *staged_blob_id, const char *relpath,
8143 struct got_repository *repo,
8144 got_worktree_patch_cb patch_cb, void *patch_arg)
8146 const struct got_error *err, *free_err;
8147 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8148 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8149 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8150 struct got_diffreg_result *diffreg_result = NULL;
8151 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8152 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8154 *path_unstaged_content = NULL;
8155 *path_new_staged_content = NULL;
8157 err = got_object_id_str(&label1, blob_id);
8160 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
8164 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
8168 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8172 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
8176 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
8180 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8184 err = got_diff_files(&diffreg_result, f1, label1, f2,
8185 path2, 3, 0, 1, NULL);
8189 err = got_opentemp_named(path_unstaged_content, &outfile,
8190 "got-unstaged-content");
8193 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8194 "got-new-staged-content");
8198 if (fseek(f1, 0L, SEEK_SET) == -1) {
8199 err = got_ferror(f1, GOT_ERR_IO);
8202 if (fseek(f2, 0L, SEEK_SET) == -1) {
8203 err = got_ferror(f2, GOT_ERR_IO);
8206 /* Count the number of actual changes in the diff result. */
8207 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8208 struct diff_chunk_context cc = {};
8209 diff_chunk_context_load_change(&cc, &nchunks_used,
8210 diffreg_result->result, n, 0);
8213 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8215 err = apply_or_reject_change(&choice, &nchunks_used,
8216 diffreg_result->result, n, relpath, f1, f2,
8217 &line_cur1, &line_cur2,
8218 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8221 if (choice == GOT_PATCH_CHOICE_YES)
8224 have_rejected_content = 1;
8225 if (choice == GOT_PATCH_CHOICE_QUIT)
8228 if (have_content || have_rejected_content)
8229 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8230 outfile, rejectfile);
8234 got_object_blob_close(blob);
8236 got_object_blob_close(staged_blob);
8237 free_err = got_diffreg_result_free(diffreg_result);
8238 if (free_err && err == NULL)
8240 if (f1 && fclose(f1) == EOF && err == NULL)
8241 err = got_error_from_errno2("fclose", path1);
8242 if (f2 && fclose(f2) == EOF && err == NULL)
8243 err = got_error_from_errno2("fclose", path2);
8244 if (outfile && fclose(outfile) == EOF && err == NULL)
8245 err = got_error_from_errno2("fclose", *path_unstaged_content);
8246 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8247 err = got_error_from_errno2("fclose", *path_new_staged_content);
8248 if (path1 && unlink(path1) == -1 && err == NULL)
8249 err = got_error_from_errno2("unlink", path1);
8250 if (path2 && unlink(path2) == -1 && err == NULL)
8251 err = got_error_from_errno2("unlink", path2);
8252 if (err || !have_content) {
8253 if (*path_unstaged_content &&
8254 unlink(*path_unstaged_content) == -1 && err == NULL)
8255 err = got_error_from_errno2("unlink",
8256 *path_unstaged_content);
8257 free(*path_unstaged_content);
8258 *path_unstaged_content = NULL;
8260 if (err || !have_content || !have_rejected_content) {
8261 if (*path_new_staged_content &&
8262 unlink(*path_new_staged_content) == -1 && err == NULL)
8263 err = got_error_from_errno2("unlink",
8264 *path_new_staged_content);
8265 free(*path_new_staged_content);
8266 *path_new_staged_content = NULL;
8273 static const struct got_error *
8274 unstage_hunks(struct got_object_id *staged_blob_id,
8275 struct got_blob_object *blob_base,
8276 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8277 const char *ondisk_path, const char *label_orig,
8278 struct got_worktree *worktree, struct got_repository *repo,
8279 got_worktree_patch_cb patch_cb, void *patch_arg,
8280 got_worktree_checkout_cb progress_cb, void *progress_arg)
8282 const struct got_error *err = NULL;
8283 char *path_unstaged_content = NULL;
8284 char *path_new_staged_content = NULL;
8285 char *parent = NULL, *base_path = NULL;
8286 char *blob_base_path = NULL;
8287 struct got_object_id *new_staged_blob_id = NULL;
8288 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8291 err = create_unstaged_content(&path_unstaged_content,
8292 &path_new_staged_content, blob_id, staged_blob_id,
8293 ie->path, repo, patch_cb, patch_arg);
8297 if (path_unstaged_content == NULL)
8300 if (path_new_staged_content) {
8301 err = got_object_blob_create(&new_staged_blob_id,
8302 path_new_staged_content, repo);
8307 f = fopen(path_unstaged_content, "re");
8309 err = got_error_from_errno2("fopen",
8310 path_unstaged_content);
8313 if (fstat(fileno(f), &sb) == -1) {
8314 err = got_error_from_errno2("fstat", path_unstaged_content);
8317 if (got_fileindex_entry_staged_filetype_get(ie) ==
8318 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8319 char link_target[PATH_MAX];
8321 r = fread(link_target, 1, sizeof(link_target), f);
8322 if (r == 0 && ferror(f)) {
8323 err = got_error_from_errno("fread");
8326 if (r >= sizeof(link_target)) { /* should not happen */
8327 err = got_error(GOT_ERR_NO_SPACE);
8330 link_target[r] = '\0';
8331 err = merge_symlink(worktree, blob_base,
8332 ondisk_path, ie->path, label_orig, link_target,
8333 worktree->base_commit_id, repo, progress_cb,
8336 int local_changes_subsumed;
8338 err = got_path_dirname(&parent, ondisk_path);
8342 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8344 err = got_error_from_errno("asprintf");
8349 err = got_opentemp_named(&blob_base_path, &f_base, base_path);
8352 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8358 * In order the run a 3-way merge with a symlink we copy the symlink's
8359 * target path into a temporary file and use that file with diff3.
8361 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8362 err = dump_symlink_target_path_to_file(&f_deriv2,
8368 fd = open(ondisk_path,
8369 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8371 err = got_error_from_errno2("open", ondisk_path);
8374 f_deriv2 = fdopen(fd, "r");
8375 if (f_deriv2 == NULL) {
8376 err = got_error_from_errno2("fdopen", ondisk_path);
8382 err = merge_file(&local_changes_subsumed, worktree,
8383 f_base, f, f_deriv2, ondisk_path, ie->path,
8384 got_fileindex_perms_to_st(ie),
8385 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8386 repo, progress_cb, progress_arg);
8391 if (new_staged_blob_id) {
8392 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8393 SHA1_DIGEST_LENGTH);
8395 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8396 got_fileindex_entry_staged_filetype_set(ie, 0);
8399 free(new_staged_blob_id);
8400 if (path_unstaged_content &&
8401 unlink(path_unstaged_content) == -1 && err == NULL)
8402 err = got_error_from_errno2("unlink", path_unstaged_content);
8403 if (path_new_staged_content &&
8404 unlink(path_new_staged_content) == -1 && err == NULL)
8405 err = got_error_from_errno2("unlink", path_new_staged_content);
8406 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8407 err = got_error_from_errno2("unlink", blob_base_path);
8408 if (f_base && fclose(f_base) == EOF && err == NULL)
8409 err = got_error_from_errno2("fclose", path_unstaged_content);
8410 if (f && fclose(f) == EOF && err == NULL)
8411 err = got_error_from_errno2("fclose", path_unstaged_content);
8412 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8413 err = got_error_from_errno2("fclose", ondisk_path);
8414 free(path_unstaged_content);
8415 free(path_new_staged_content);
8416 free(blob_base_path);
8422 static const struct got_error *
8423 unstage_path(void *arg, unsigned char status,
8424 unsigned char staged_status, const char *relpath,
8425 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8426 struct got_object_id *commit_id, int dirfd, const char *de_name)
8428 const struct got_error *err = NULL;
8429 struct unstage_path_arg *a = arg;
8430 struct got_fileindex_entry *ie;
8431 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8432 char *ondisk_path = NULL;
8433 char *id_str = NULL, *label_orig = NULL;
8434 int local_changes_subsumed;
8437 if (staged_status != GOT_STATUS_ADD &&
8438 staged_status != GOT_STATUS_MODIFY &&
8439 staged_status != GOT_STATUS_DELETE)
8442 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8444 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8446 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8448 return got_error_from_errno("asprintf");
8450 err = got_object_id_str(&id_str,
8451 commit_id ? commit_id : a->worktree->base_commit_id);
8454 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8456 err = got_error_from_errno("asprintf");
8460 switch (staged_status) {
8461 case GOT_STATUS_MODIFY:
8462 err = got_object_open_as_blob(&blob_base, a->repo,
8467 case GOT_STATUS_ADD:
8469 if (staged_status == GOT_STATUS_ADD) {
8470 int choice = GOT_PATCH_CHOICE_NONE;
8471 err = (*a->patch_cb)(&choice, a->patch_arg,
8472 staged_status, ie->path, NULL, 1, 1);
8475 if (choice != GOT_PATCH_CHOICE_YES)
8478 err = unstage_hunks(staged_blob_id,
8479 blob_base, blob_id, ie, ondisk_path,
8480 label_orig, a->worktree, a->repo,
8481 a->patch_cb, a->patch_arg,
8482 a->progress_cb, a->progress_arg);
8483 break; /* Done with this file. */
8486 err = got_object_open_as_blob(&blob_staged, a->repo,
8487 staged_blob_id, 8192);
8490 switch (got_fileindex_entry_staged_filetype_get(ie)) {
8491 case GOT_FILEIDX_MODE_BAD_SYMLINK:
8492 case GOT_FILEIDX_MODE_REGULAR_FILE:
8493 err = merge_blob(&local_changes_subsumed, a->worktree,
8494 blob_base, ondisk_path, relpath,
8495 got_fileindex_perms_to_st(ie), label_orig,
8496 blob_staged, commit_id ? commit_id :
8497 a->worktree->base_commit_id, a->repo,
8498 a->progress_cb, a->progress_arg);
8500 case GOT_FILEIDX_MODE_SYMLINK:
8501 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8502 char *staged_target;
8503 err = got_object_blob_read_to_str(
8504 &staged_target, blob_staged);
8507 err = merge_symlink(a->worktree, blob_base,
8508 ondisk_path, relpath, label_orig,
8509 staged_target, commit_id ? commit_id :
8510 a->worktree->base_commit_id,
8511 a->repo, a->progress_cb, a->progress_arg);
8512 free(staged_target);
8514 err = merge_blob(&local_changes_subsumed,
8515 a->worktree, blob_base, ondisk_path,
8516 relpath, got_fileindex_perms_to_st(ie),
8517 label_orig, blob_staged,
8518 commit_id ? commit_id :
8519 a->worktree->base_commit_id, a->repo,
8520 a->progress_cb, a->progress_arg);
8524 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8528 got_fileindex_entry_stage_set(ie,
8529 GOT_FILEIDX_STAGE_NONE);
8530 got_fileindex_entry_staged_filetype_set(ie, 0);
8533 case GOT_STATUS_DELETE:
8535 int choice = GOT_PATCH_CHOICE_NONE;
8536 err = (*a->patch_cb)(&choice, a->patch_arg,
8537 staged_status, ie->path, NULL, 1, 1);
8540 if (choice == GOT_PATCH_CHOICE_NO)
8542 if (choice != GOT_PATCH_CHOICE_YES) {
8543 err = got_error(GOT_ERR_PATCH_CHOICE);
8547 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8548 got_fileindex_entry_staged_filetype_set(ie, 0);
8549 err = get_file_status(&status, &sb, ie, ondisk_path,
8550 dirfd, de_name, a->repo);
8553 err = (*a->progress_cb)(a->progress_arg, status, relpath);
8559 got_object_blob_close(blob_base);
8561 got_object_blob_close(blob_staged);
8567 const struct got_error *
8568 got_worktree_unstage(struct got_worktree *worktree,
8569 struct got_pathlist_head *paths,
8570 got_worktree_checkout_cb progress_cb, void *progress_arg,
8571 got_worktree_patch_cb patch_cb, void *patch_arg,
8572 struct got_repository *repo)
8574 const struct got_error *err = NULL, *sync_err, *unlockerr;
8575 struct got_pathlist_entry *pe;
8576 struct got_fileindex *fileindex = NULL;
8577 char *fileindex_path = NULL;
8578 struct unstage_path_arg upa;
8580 err = lock_worktree(worktree, LOCK_EX);
8584 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8588 upa.worktree = worktree;
8589 upa.fileindex = fileindex;
8591 upa.progress_cb = progress_cb;
8592 upa.progress_arg = progress_arg;
8593 upa.patch_cb = patch_cb;
8594 upa.patch_arg = patch_arg;
8595 TAILQ_FOREACH(pe, paths, entry) {
8596 err = worktree_status(worktree, pe->path, fileindex, repo,
8597 unstage_path, &upa, NULL, NULL, 1, 0);
8602 sync_err = sync_fileindex(fileindex, fileindex_path);
8603 if (sync_err && err == NULL)
8606 free(fileindex_path);
8608 got_fileindex_free(fileindex);
8609 unlockerr = lock_worktree(worktree, LOCK_SH);
8610 if (unlockerr && err == NULL)
8615 struct report_file_info_arg {
8616 struct got_worktree *worktree;
8617 got_worktree_path_info_cb info_cb;
8619 struct got_pathlist_head *paths;
8620 got_cancel_cb cancel_cb;
8624 static const struct got_error *
8625 report_file_info(void *arg, struct got_fileindex_entry *ie)
8627 struct report_file_info_arg *a = arg;
8628 struct got_pathlist_entry *pe;
8629 struct got_object_id blob_id, staged_blob_id, commit_id;
8630 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8631 struct got_object_id *commit_idp = NULL;
8634 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8635 return got_error(GOT_ERR_CANCELLED);
8637 TAILQ_FOREACH(pe, a->paths, entry) {
8638 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8639 got_path_is_child(ie->path, pe->path, pe->path_len))
8642 if (pe == NULL) /* not found */
8645 if (got_fileindex_entry_has_blob(ie)) {
8646 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8647 blob_idp = &blob_id;
8649 stage = got_fileindex_entry_stage_get(ie);
8650 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8651 stage == GOT_FILEIDX_STAGE_ADD) {
8652 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8653 SHA1_DIGEST_LENGTH);
8654 staged_blob_idp = &staged_blob_id;
8657 if (got_fileindex_entry_has_commit(ie)) {
8658 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8659 commit_idp = &commit_id;
8662 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8663 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8666 const struct got_error *
8667 got_worktree_path_info(struct got_worktree *worktree,
8668 struct got_pathlist_head *paths,
8669 got_worktree_path_info_cb info_cb, void *info_arg,
8670 got_cancel_cb cancel_cb, void *cancel_arg)
8673 const struct got_error *err = NULL, *unlockerr;
8674 struct got_fileindex *fileindex = NULL;
8675 char *fileindex_path = NULL;
8676 struct report_file_info_arg arg;
8678 err = lock_worktree(worktree, LOCK_SH);
8682 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8686 arg.worktree = worktree;
8687 arg.info_cb = info_cb;
8688 arg.info_arg = info_arg;
8690 arg.cancel_cb = cancel_cb;
8691 arg.cancel_arg = cancel_arg;
8692 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8695 free(fileindex_path);
8697 got_fileindex_free(fileindex);
8698 unlockerr = lock_worktree(worktree, LOCK_UN);
8699 if (unlockerr && err == NULL)