2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/queue.h>
38 #include "got_error.h"
39 #include "got_repository.h"
40 #include "got_reference.h"
41 #include "got_object.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
45 #include "got_opentemp.h"
48 #include "got_lib_worktree.h"
49 #include "got_lib_sha1.h"
50 #include "got_lib_fileindex.h"
51 #include "got_lib_inflate.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_object_idset.h"
57 #include "got_lib_diff.h"
60 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
63 #define GOT_MERGE_LABEL_MERGED "merged change"
64 #define GOT_MERGE_LABEL_BASE "3-way merge base"
66 static const struct got_error *
67 create_meta_file(const char *path_got, const char *name, const char *content)
69 const struct got_error *err = NULL;
72 if (asprintf(&path, "%s/%s", path_got, name) == -1)
73 return got_error_from_errno("asprintf");
75 err = got_path_create_file(path, content);
80 static const struct got_error *
81 update_meta_file(const char *path_got, const char *name, const char *content)
83 const struct got_error *err = NULL;
88 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
89 err = got_error_from_errno("asprintf");
94 err = got_opentemp_named(&tmppath, &tmpfile, path);
99 int len = fprintf(tmpfile, "%s\n", content);
100 if (len != strlen(content) + 1) {
101 err = got_error_from_errno2("fprintf", tmppath);
106 if (rename(tmppath, path) != 0) {
107 err = got_error_from_errno3("rename", tmppath, path);
113 if (fclose(tmpfile) != 0 && err == NULL)
114 err = got_error_from_errno2("fclose", tmppath);
119 static const struct got_error *
120 read_meta_file(char **content, const char *path_got, const char *name)
122 const struct got_error *err = NULL;
130 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
131 err = got_error_from_errno("asprintf");
136 fd = open(path, O_RDONLY | O_NOFOLLOW);
139 err = got_error_path(path, GOT_ERR_WORKTREE_META);
141 err = got_error_from_errno2("open", path);
144 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
145 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
146 : got_error_from_errno2("flock", path));
150 if (fstat(fd, &sb) != 0) {
151 err = got_error_from_errno2("fstat", path);
154 *content = calloc(1, sb.st_size);
155 if (*content == NULL) {
156 err = got_error_from_errno("calloc");
160 n = read(fd, *content, sb.st_size);
161 if (n != sb.st_size) {
162 err = (n == -1 ? got_error_from_errno2("read", path) :
163 got_error_path(path, GOT_ERR_WORKTREE_META));
166 if ((*content)[sb.st_size - 1] != '\n') {
167 err = got_error_path(path, GOT_ERR_WORKTREE_META);
170 (*content)[sb.st_size - 1] = '\0';
173 if (fd != -1 && close(fd) == -1 && err == NULL)
174 err = got_error_from_errno2("close", path_got);
183 static const struct got_error *
184 write_head_ref(const char *path_got, struct got_reference *head_ref)
186 const struct got_error *err = NULL;
189 if (got_ref_is_symbolic(head_ref)) {
190 refstr = got_ref_to_str(head_ref);
192 return got_error_from_errno("got_ref_to_str");
194 refstr = strdup(got_ref_get_name(head_ref));
196 return got_error_from_errno("strdup");
198 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
203 const struct got_error *
204 got_worktree_init(const char *path, struct got_reference *head_ref,
205 const char *prefix, struct got_repository *repo)
207 const struct got_error *err = NULL;
208 struct got_object_id *commit_id = NULL;
210 uint32_t uuid_status;
212 char *path_got = NULL;
213 char *formatstr = NULL;
214 char *absprefix = NULL;
215 char *basestr = NULL;
216 char *uuidstr = NULL;
218 if (strcmp(path, got_repo_get_path(repo)) == 0) {
219 err = got_error(GOT_ERR_WORKTREE_REPO);
223 err = got_ref_resolve(&commit_id, repo, head_ref);
226 err = got_object_get_type(&obj_type, repo, commit_id);
229 if (obj_type != GOT_OBJ_TYPE_COMMIT)
230 return got_error(GOT_ERR_OBJ_TYPE);
232 if (!got_path_is_absolute(prefix)) {
233 if (asprintf(&absprefix, "/%s", prefix) == -1)
234 return got_error_from_errno("asprintf");
237 /* Create top-level directory (may already exist). */
238 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
239 err = got_error_from_errno2("mkdir", path);
243 /* Create .got directory (may already exist). */
244 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
245 err = got_error_from_errno("asprintf");
248 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
249 err = got_error_from_errno2("mkdir", path_got);
253 /* Create an empty lock file. */
254 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
258 /* Create an empty file index. */
259 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
263 /* Write the HEAD reference. */
264 err = write_head_ref(path_got, head_ref);
268 /* Record our base commit. */
269 err = got_object_id_str(&basestr, commit_id);
272 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
276 /* Store path to repository. */
277 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
278 got_repo_get_path(repo));
282 /* Store in-repository path prefix. */
283 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
284 absprefix ? absprefix : prefix);
289 uuid_create(&uuid, &uuid_status);
290 if (uuid_status != uuid_s_ok) {
291 err = got_error_uuid(uuid_status, "uuid_create");
294 uuid_to_string(&uuid, &uuidstr, &uuid_status);
295 if (uuid_status != uuid_s_ok) {
296 err = got_error_uuid(uuid_status, "uuid_to_string");
299 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
303 /* Stamp work tree with format file. */
304 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
305 err = got_error_from_errno("asprintf");
308 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
322 static const struct got_error *
323 open_worktree(struct got_worktree **worktree, const char *path)
325 const struct got_error *err = NULL;
327 char *formatstr = NULL;
328 char *uuidstr = NULL;
329 char *path_lock = NULL;
330 char *base_commit_id_str = NULL;
331 int version, fd = -1;
333 struct got_repository *repo = NULL;
334 uint32_t uuid_status;
338 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
339 err = got_error_from_errno("asprintf");
344 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
345 err = got_error_from_errno("asprintf");
350 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
352 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
353 : got_error_from_errno2("open", path_lock));
357 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
361 version = strtonum(formatstr, 1, INT_MAX, &errstr);
363 err = got_error_msg(GOT_ERR_WORKTREE_META,
364 "could not parse work tree format version number");
367 if (version != GOT_WORKTREE_FORMAT_VERSION) {
368 err = got_error(GOT_ERR_WORKTREE_VERS);
372 *worktree = calloc(1, sizeof(**worktree));
373 if (*worktree == NULL) {
374 err = got_error_from_errno("calloc");
377 (*worktree)->lockfd = -1;
379 (*worktree)->root_path = realpath(path, NULL);
380 if ((*worktree)->root_path == NULL) {
381 err = got_error_from_errno2("realpath", path);
384 err = read_meta_file(&(*worktree)->repo_path, path_got,
385 GOT_WORKTREE_REPOSITORY);
389 err = read_meta_file(&(*worktree)->path_prefix, path_got,
390 GOT_WORKTREE_PATH_PREFIX);
394 err = read_meta_file(&base_commit_id_str, path_got,
395 GOT_WORKTREE_BASE_COMMIT);
399 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
402 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
403 if (uuid_status != uuid_s_ok) {
404 err = got_error_uuid(uuid_status, "uuid_from_string");
408 err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
412 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
417 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
418 GOT_WORKTREE_HEAD_REF);
421 got_repo_close(repo);
424 free(base_commit_id_str);
430 if (*worktree != NULL)
431 got_worktree_close(*worktree);
434 (*worktree)->lockfd = fd;
439 const struct got_error *
440 got_worktree_open(struct got_worktree **worktree, const char *path)
442 const struct got_error *err = NULL;
445 err = open_worktree(worktree, path);
446 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
450 path = dirname(path);
452 return got_error_from_errno2("dirname", path);
453 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
455 return got_error(GOT_ERR_NOT_WORKTREE);
458 const struct got_error *
459 got_worktree_close(struct got_worktree *worktree)
461 const struct got_error *err = NULL;
462 free(worktree->repo_path);
463 free(worktree->path_prefix);
464 free(worktree->base_commit_id);
465 free(worktree->head_ref_name);
466 if (worktree->lockfd != -1)
467 if (close(worktree->lockfd) != 0)
468 err = got_error_from_errno2("close",
469 got_worktree_get_root_path(worktree));
470 free(worktree->root_path);
476 got_worktree_get_root_path(struct got_worktree *worktree)
478 return worktree->root_path;
482 got_worktree_get_repo_path(struct got_worktree *worktree)
484 return worktree->repo_path;
488 got_worktree_get_path_prefix(struct got_worktree *worktree)
490 return worktree->path_prefix;
493 const struct got_error *
494 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
495 const char *path_prefix)
497 char *absprefix = NULL;
499 if (!got_path_is_absolute(path_prefix)) {
500 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
501 return got_error_from_errno("asprintf");
503 *match = (strcmp(absprefix ? absprefix : path_prefix,
504 worktree->path_prefix) == 0);
510 got_worktree_get_head_ref_name(struct got_worktree *worktree)
512 return worktree->head_ref_name;
515 const struct got_error *
516 got_worktree_set_head_ref(struct got_worktree *worktree,
517 struct got_reference *head_ref)
519 const struct got_error *err = NULL;
520 char *path_got = NULL, *head_ref_name = NULL;
522 if (asprintf(&path_got, "%s/%s", worktree->root_path,
523 GOT_WORKTREE_GOT_DIR) == -1) {
524 err = got_error_from_errno("asprintf");
529 head_ref_name = strdup(got_ref_get_name(head_ref));
530 if (head_ref_name == NULL) {
531 err = got_error_from_errno("strdup");
535 err = write_head_ref(path_got, head_ref);
539 free(worktree->head_ref_name);
540 worktree->head_ref_name = head_ref_name;
548 struct got_object_id *
549 got_worktree_get_base_commit_id(struct got_worktree *worktree)
551 return worktree->base_commit_id;
554 const struct got_error *
555 got_worktree_set_base_commit_id(struct got_worktree *worktree,
556 struct got_repository *repo, struct got_object_id *commit_id)
558 const struct got_error *err;
559 struct got_object *obj = NULL;
561 char *path_got = NULL;
563 if (asprintf(&path_got, "%s/%s", worktree->root_path,
564 GOT_WORKTREE_GOT_DIR) == -1) {
565 err = got_error_from_errno("asprintf");
570 err = got_object_open(&obj, repo, commit_id);
574 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
575 err = got_error(GOT_ERR_OBJ_TYPE);
579 /* Record our base commit. */
580 err = got_object_id_str(&id_str, commit_id);
583 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
587 free(worktree->base_commit_id);
588 worktree->base_commit_id = got_object_id_dup(commit_id);
589 if (worktree->base_commit_id == NULL) {
590 err = got_error_from_errno("got_object_id_dup");
595 got_object_close(obj);
601 static const struct got_error *
602 lock_worktree(struct got_worktree *worktree, int operation)
604 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
605 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
606 : got_error_from_errno2("flock",
607 got_worktree_get_root_path(worktree)));
611 static const struct got_error *
612 add_dir_on_disk(struct got_worktree *worktree, const char *path)
614 const struct got_error *err = NULL;
617 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
618 return got_error_from_errno("asprintf");
620 err = got_path_mkdir(abspath);
621 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
624 if (lstat(abspath, &sb) == -1) {
625 err = got_error_from_errno2("lstat", abspath);
626 } else if (!S_ISDIR(sb.st_mode)) {
627 /* TODO directory is obstructed; do something */
628 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
635 static const struct got_error *
636 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
638 const struct got_error *err = NULL;
641 size_t flen1 = 0, flen2 = 0;
646 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
647 if (flen1 == 0 && ferror(f1)) {
648 err = got_error_from_errno("fread");
651 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
652 if (flen2 == 0 && ferror(f2)) {
653 err = got_error_from_errno("fread");
660 } else if (flen2 == 0) {
664 } else if (flen1 == flen2) {
665 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
678 static const struct got_error *
679 check_files_equal(int *same, const char *f1_path, const char *f2_path)
681 const struct got_error *err = NULL;
684 FILE *f1 = NULL, *f2 = NULL;
688 if (lstat(f1_path, &sb) != 0) {
689 err = got_error_from_errno2("lstat", f1_path);
694 if (lstat(f2_path, &sb) != 0) {
695 err = got_error_from_errno2("lstat", f2_path);
700 if (size1 != size2) {
705 f1 = fopen(f1_path, "r");
707 return got_error_from_errno2("fopen", f1_path);
709 f2 = fopen(f2_path, "r");
711 err = got_error_from_errno2("fopen", f2_path);
715 err = check_file_contents_equal(same, f1, f2);
717 if (f1 && fclose(f1) != 0 && err == NULL)
718 err = got_error_from_errno("fclose");
719 if (f2 && fclose(f2) != 0 && err == NULL)
720 err = got_error_from_errno("fclose");
726 * Perform a 3-way merge where blob_orig acts as the common ancestor,
727 * the file at deriv_path acts as the first derived version, and the
728 * file on disk acts as the second derived version.
730 static const struct got_error *
731 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
732 struct got_blob_object *blob_orig, const char *ondisk_path,
733 const char *path, uint16_t st_mode, const char *deriv_path,
734 const char *label_orig, const char *label_deriv,
735 struct got_repository *repo,
736 got_worktree_checkout_cb progress_cb, void *progress_arg)
738 const struct got_error *err = NULL;
741 char *blob_orig_path = NULL;
742 char *merged_path = NULL, *base_path = NULL;
745 char *symlink_path = NULL;
746 FILE *symlinkf = NULL;
748 *local_changes_subsumed = 0;
750 parent = dirname(ondisk_path);
752 return got_error_from_errno2("dirname", ondisk_path);
754 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
755 return got_error_from_errno("asprintf");
757 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
762 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
763 err = got_error_from_errno("asprintf");
768 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
772 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
778 * If the file has no blob, this is an "add vs add" conflict,
779 * and we simply use an empty ancestor file to make both files
780 * appear in the merged result in their entirety.
785 * In order the run a 3-way merge with a symlink we copy the symlink's
786 * target path into a temporary file and use that file with diff3.
788 if (S_ISLNK(st_mode)) {
789 char target_path[PATH_MAX];
794 if (asprintf(&base_path, "%s/got-symlink-merge",
796 err = got_error_from_errno("asprintf");
800 err = got_opentemp_named(&symlink_path, &symlinkf, base_path);
803 target_len = readlink(ondisk_path, target_path,
804 sizeof(target_path));
805 if (target_len == -1) {
806 err = got_error_from_errno2("readlink", ondisk_path);
809 n = fwrite(target_path, 1, target_len, symlinkf);
810 if (n != target_len) {
811 err = got_ferror(symlinkf, GOT_ERR_IO);
814 if (fflush(symlinkf) == EOF) {
815 err = got_error_from_errno2("fflush", symlink_path);
820 err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
821 blob_orig_path, symlink_path ? symlink_path : ondisk_path,
822 label_deriv, label_orig, NULL);
826 err = (*progress_cb)(progress_arg,
827 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
831 if (fsync(merged_fd) != 0) {
832 err = got_error_from_errno("fsync");
836 /* Check if a clean merge has subsumed all local changes. */
837 if (overlapcnt == 0) {
838 err = check_files_equal(local_changes_subsumed, deriv_path,
844 if (fchmod(merged_fd, st_mode) != 0) {
845 err = got_error_from_errno2("fchmod", merged_path);
849 if (rename(merged_path, ondisk_path) != 0) {
850 err = got_error_from_errno3("rename", merged_path,
860 if (unlink(symlink_path) == -1 && err == NULL)
861 err = got_error_from_errno2("unlink", symlink_path);
863 if (symlinkf && fclose(symlinkf) == EOF && err == NULL)
864 err = got_error_from_errno2("fclose", symlink_path);
866 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
867 err = got_error_from_errno("close");
868 if (f_orig && fclose(f_orig) != 0 && err == NULL)
869 err = got_error_from_errno("fclose");
872 if (blob_orig_path) {
873 unlink(blob_orig_path);
874 free(blob_orig_path);
879 static const struct got_error *
880 update_symlink(const char *ondisk_path, const char *target_path,
883 /* This is not atomic but matches what 'ln -sf' does. */
884 if (unlink(ondisk_path) == -1)
885 return got_error_from_errno2("unlink", ondisk_path);
886 if (symlink(target_path, ondisk_path) == -1)
887 return got_error_from_errno3("symlink", target_path,
893 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
894 * in the work tree with a file that contains conflict markers and the
895 * conflicting target paths of the original version, a "derived version"
896 * of a symlink from an incoming change, and a local version of the symlink.
898 * The original versions's target path can be NULL if it is not available,
899 * such as if both derived versions added a new symlink at the same path.
901 * The incoming derived symlink target is NULL in case the incoming change
902 * has deleted this symlink.
904 static const struct got_error *
905 install_symlink_conflict(const char *deriv_target,
906 struct got_object_id *deriv_base_commit_id, const char *orig_target,
907 const char *label_orig, const char *local_target, const char *ondisk_path)
909 const struct got_error *err;
910 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
913 err = got_object_id_str(&id_str, deriv_base_commit_id);
915 return got_error_from_errno("asprintf");
917 if (asprintf(&label_deriv, "%s: commit %s",
918 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
919 err = got_error_from_errno("asprintf");
923 err = got_opentemp_named(&path, &f, "got-symlink-conflict");
927 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
928 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
929 deriv_target ? deriv_target : "(symlink was deleted)",
930 orig_target ? label_orig : "",
931 orig_target ? "\n" : "",
932 orig_target ? orig_target : "",
933 orig_target ? "\n" : "",
934 GOT_DIFF_CONFLICT_MARKER_SEP,
935 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
936 err = got_error_from_errno2("fprintf", path);
940 if (unlink(ondisk_path) == -1) {
941 err = got_error_from_errno2("unlink", ondisk_path);
944 if (rename(path, ondisk_path) == -1) {
945 err = got_error_from_errno3("rename", path, ondisk_path);
948 if (chmod(ondisk_path, GOT_DEFAULT_FILE_MODE) == -1) {
949 err = got_error_from_errno2("chmod", ondisk_path);
953 if (f != NULL && fclose(f) == EOF && err == NULL)
954 err = got_error_from_errno2("fclose", path);
961 /* forward declaration */
962 static const struct got_error *
963 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
964 const char *, const char *, uint16_t, const char *,
965 struct got_blob_object *, struct got_object_id *,
966 struct got_repository *, got_worktree_checkout_cb, void *);
969 * Merge a symlink into the work tree, where blob_orig acts as the common
970 * ancestor, deriv_target is the link target of the first derived version,
971 * and the symlink on disk acts as the second derived version.
972 * Assume that contents of both blobs represent symlinks.
974 static const struct got_error *
975 merge_symlink(struct got_worktree *worktree,
976 struct got_blob_object *blob_orig, const char *ondisk_path,
977 const char *path, const char *label_orig, const char *deriv_target,
978 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
979 got_worktree_checkout_cb progress_cb, void *progress_arg)
981 const struct got_error *err = NULL;
982 char *ancestor_target = NULL;
984 ssize_t ondisk_len, deriv_len;
985 char ondisk_target[PATH_MAX];
986 int have_local_change = 0;
987 int have_incoming_change = 0;
989 if (lstat(ondisk_path, &sb) == -1)
990 return got_error_from_errno2("lstat", ondisk_path);
992 ondisk_len = readlink(ondisk_path, ondisk_target,
993 sizeof(ondisk_target));
994 if (ondisk_len == -1) {
995 err = got_error_from_errno2("readlink",
999 ondisk_target[ondisk_len] = '\0';
1002 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
1007 if (ancestor_target == NULL ||
1008 (ondisk_len != strlen(ancestor_target) ||
1009 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
1010 have_local_change = 1;
1012 deriv_len = strlen(deriv_target);
1013 if (ancestor_target == NULL ||
1014 (deriv_len != strlen(ancestor_target) ||
1015 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
1016 have_incoming_change = 1;
1018 if (!have_local_change && !have_incoming_change) {
1019 if (ancestor_target) {
1020 /* Both sides made the same change. */
1021 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1023 } else if (deriv_len == ondisk_len &&
1024 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1025 /* Both sides added the same symlink. */
1026 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1029 /* Both sides added symlinks which don't match. */
1030 err = install_symlink_conflict(deriv_target,
1031 deriv_base_commit_id, ancestor_target,
1032 label_orig, ondisk_target, ondisk_path);
1035 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1038 } else if (!have_local_change && have_incoming_change) {
1039 /* Apply the incoming change. */
1040 err = update_symlink(ondisk_path, deriv_target,
1041 strlen(deriv_target));
1044 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1045 } else if (have_local_change && have_incoming_change) {
1046 if (deriv_len == ondisk_len &&
1047 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
1048 /* Both sides made the same change. */
1049 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1052 err = install_symlink_conflict(deriv_target,
1053 deriv_base_commit_id, ancestor_target, label_orig,
1054 ondisk_target, ondisk_path);
1057 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1063 free(ancestor_target);
1068 * Perform a 3-way merge where blob_orig acts as the common ancestor,
1069 * blob_deriv acts as the first derived version, and the file on disk
1070 * acts as the second derived version.
1072 static const struct got_error *
1073 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1074 struct got_blob_object *blob_orig, const char *ondisk_path,
1075 const char *path, uint16_t st_mode, const char *label_orig,
1076 struct got_blob_object *blob_deriv,
1077 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1078 got_worktree_checkout_cb progress_cb, void *progress_arg)
1080 const struct got_error *err = NULL;
1081 FILE *f_deriv = NULL;
1082 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1083 char *label_deriv = NULL, *parent;
1085 *local_changes_subsumed = 0;
1087 parent = dirname(ondisk_path);
1089 return got_error_from_errno2("dirname", ondisk_path);
1092 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1093 err = got_error_from_errno("asprintf");
1098 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1101 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1106 err = got_object_id_str(&id_str, deriv_base_commit_id);
1109 if (asprintf(&label_deriv, "%s: commit %s",
1110 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1111 err = got_error_from_errno("asprintf");
1115 err = merge_file(local_changes_subsumed, worktree, blob_orig,
1116 ondisk_path, path, st_mode, blob_deriv_path, label_orig,
1117 label_deriv, repo, progress_cb, progress_arg);
1119 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
1120 err = got_error_from_errno("fclose");
1122 if (blob_deriv_path) {
1123 unlink(blob_deriv_path);
1124 free(blob_deriv_path);
1131 static const struct got_error *
1132 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1133 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1134 const char *ondisk_path, const char *path, struct got_object_id *blob_id)
1136 const struct got_error *err = NULL;
1137 struct got_fileindex_entry *new_ie;
1141 err = got_fileindex_entry_alloc(&new_ie, path);
1145 err = got_fileindex_entry_update(new_ie, ondisk_path,
1146 blob_id->sha1, base_commit_id->sha1, 1);
1150 err = got_fileindex_entry_add(fileindex, new_ie);
1153 got_fileindex_entry_free(new_ie);
1160 get_ondisk_perms(int executable, mode_t st_mode)
1162 mode_t xbits = S_IXUSR;
1165 /* Map read bits to execute bits. */
1166 if (st_mode & S_IRGRP)
1168 if (st_mode & S_IROTH)
1170 return st_mode | xbits;
1173 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1176 /* forward declaration */
1177 static const struct got_error *
1178 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1179 const char *path, mode_t te_mode, mode_t st_mode,
1180 struct got_blob_object *blob, int restoring_missing_file,
1181 int reverting_versioned_file, int installing_bad_symlink,
1182 int path_is_unversioned, struct got_repository *repo,
1183 got_worktree_checkout_cb progress_cb, void *progress_arg);
1186 * This function assumes that the provided symlink target points at a
1187 * safe location in the work tree!
1189 static const struct got_error *
1190 replace_existing_symlink(const char *ondisk_path, const char *target_path,
1193 const struct got_error *err = NULL;
1195 char etarget[PATH_MAX];
1199 * "Bad" symlinks (those pointing outside the work tree or into the
1200 * .got directory) are installed in the work tree as a regular file
1201 * which contains the bad symlink target path.
1202 * The new symlink target has already been checked for safety by our
1203 * caller. If we can successfully open a regular file then we simply
1204 * replace this file with a symlink below.
1206 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1209 return got_error_from_errno2("open", ondisk_path);
1211 /* We are updating an existing on-disk symlink. */
1212 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1214 return got_error_from_errno2("readlink", ondisk_path);
1216 if (elen == target_len &&
1217 memcmp(etarget, target_path, target_len) == 0)
1218 return NULL; /* nothing to do */
1221 err = update_symlink(ondisk_path, target_path, target_len);
1222 if (fd != -1 && close(fd) == -1 && err == NULL)
1223 err = got_error_from_errno2("close", ondisk_path);
1227 static const struct got_error *
1228 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1229 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1231 const struct got_error *err = NULL;
1232 char canonpath[PATH_MAX];
1233 char *path_got = NULL;
1235 *is_bad_symlink = 0;
1237 if (target_len >= sizeof(canonpath)) {
1238 *is_bad_symlink = 1;
1243 * We do not use realpath(3) to resolve the symlink's target
1244 * path because we don't want to resolve symlinks recursively.
1245 * Instead we make the path absolute and then canonicalize it.
1246 * Relative symlink target lookup should begin at the directory
1247 * in which the blob object is being installed.
1249 if (!got_path_is_absolute(target_path)) {
1251 char *parent = dirname(ondisk_path);
1253 return got_error_from_errno2("dirname", ondisk_path);
1254 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1)
1255 return got_error_from_errno("asprintf");
1256 if (strlen(abspath) >= sizeof(canonpath)) {
1257 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1261 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1266 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1271 /* Only allow symlinks pointing at paths within the work tree. */
1272 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1273 *is_bad_symlink = 1;
1277 /* Do not allow symlinks pointing into the .got directory. */
1278 if (asprintf(&path_got, "%s/%s", wtroot_path,
1279 GOT_WORKTREE_GOT_DIR) == -1)
1280 return got_error_from_errno("asprintf");
1281 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1282 *is_bad_symlink = 1;
1288 static const struct got_error *
1289 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1290 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1291 int restoring_missing_file, int reverting_versioned_file,
1292 int path_is_unversioned, struct got_repository *repo,
1293 got_worktree_checkout_cb progress_cb, void *progress_arg)
1295 const struct got_error *err = NULL;
1296 char target_path[PATH_MAX];
1297 size_t len, target_len = 0;
1298 char *path_got = NULL;
1299 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1300 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1302 *is_bad_symlink = 0;
1305 * Blob object content specifies the target path of the link.
1306 * If a symbolic link cannot be installed we instead create
1307 * a regular file which contains the link target path stored
1308 * in the blob object.
1311 err = got_object_blob_read_block(&len, blob);
1312 if (len + target_len >= sizeof(target_path)) {
1313 /* Path too long; install as a regular file. */
1314 *is_bad_symlink = 1;
1315 got_object_blob_rewind(blob);
1316 return install_blob(worktree, ondisk_path, path,
1317 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1318 restoring_missing_file, reverting_versioned_file,
1319 1, path_is_unversioned, repo, progress_cb,
1323 /* Skip blob object header first time around. */
1324 memcpy(target_path + target_len, buf + hdrlen,
1326 target_len += len - hdrlen;
1330 target_path[target_len] = '\0';
1332 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1333 ondisk_path, worktree->root_path);
1337 if (*is_bad_symlink) {
1338 /* install as a regular file */
1339 *is_bad_symlink = 1;
1340 got_object_blob_rewind(blob);
1341 err = install_blob(worktree, ondisk_path, path,
1342 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1343 restoring_missing_file, reverting_versioned_file, 1,
1344 path_is_unversioned, repo, progress_cb, progress_arg);
1348 if (symlink(target_path, ondisk_path) == -1) {
1349 if (errno == EEXIST) {
1350 if (path_is_unversioned) {
1351 err = (*progress_cb)(progress_arg,
1352 GOT_STATUS_UNVERSIONED, path);
1355 err = replace_existing_symlink(ondisk_path,
1356 target_path, target_len);
1360 err = (*progress_cb)(progress_arg,
1361 reverting_versioned_file ?
1362 GOT_STATUS_REVERT : GOT_STATUS_UPDATE,
1365 goto done; /* Nothing else to do. */
1368 if (errno == ENOENT) {
1369 char *parent = dirname(ondisk_path);
1370 if (parent == NULL) {
1371 err = got_error_from_errno2("dirname",
1375 err = add_dir_on_disk(worktree, parent);
1379 * Retry, and fall through to error handling
1380 * below if this second attempt fails.
1382 if (symlink(target_path, ondisk_path) != -1) {
1383 err = NULL; /* success */
1388 /* Handle errors from first or second creation attempt. */
1389 if (errno == ENAMETOOLONG) {
1390 /* bad target path; install as a regular file */
1391 *is_bad_symlink = 1;
1392 got_object_blob_rewind(blob);
1393 err = install_blob(worktree, ondisk_path, path,
1394 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1395 restoring_missing_file, reverting_versioned_file, 1,
1396 path_is_unversioned, repo,
1397 progress_cb, progress_arg);
1398 } else if (errno == ENOTDIR) {
1399 err = got_error_path(ondisk_path,
1400 GOT_ERR_FILE_OBSTRUCTED);
1402 err = got_error_from_errno3("symlink",
1403 target_path, ondisk_path);
1405 } else if (progress_cb)
1406 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1407 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1413 static const struct got_error *
1414 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1415 const char *path, mode_t te_mode, mode_t st_mode,
1416 struct got_blob_object *blob, int restoring_missing_file,
1417 int reverting_versioned_file, int installing_bad_symlink,
1418 int path_is_unversioned, struct got_repository *repo,
1419 got_worktree_checkout_cb progress_cb, void *progress_arg)
1421 const struct got_error *err = NULL;
1425 char *tmppath = NULL;
1427 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1428 GOT_DEFAULT_FILE_MODE);
1430 if (errno == ENOENT) {
1431 char *parent = dirname(path);
1433 return got_error_from_errno2("dirname", path);
1434 err = add_dir_on_disk(worktree, parent);
1437 fd = open(ondisk_path,
1438 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1439 GOT_DEFAULT_FILE_MODE);
1441 return got_error_from_errno2("open",
1443 } else if (errno == EEXIST) {
1444 if (path_is_unversioned) {
1445 err = (*progress_cb)(progress_arg,
1446 GOT_STATUS_UNVERSIONED, path);
1449 if (!S_ISREG(st_mode) && !installing_bad_symlink) {
1450 /* TODO file is obstructed; do something */
1451 err = got_error_path(ondisk_path,
1452 GOT_ERR_FILE_OBSTRUCTED);
1455 err = got_opentemp_named_fd(&tmppath, &fd,
1462 return got_error_from_errno2("open", ondisk_path);
1466 if (restoring_missing_file)
1467 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1469 else if (reverting_versioned_file)
1470 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1473 err = (*progress_cb)(progress_arg,
1474 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1479 hdrlen = got_object_blob_get_hdrlen(blob);
1481 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1482 err = got_object_blob_read_block(&len, blob);
1486 /* Skip blob object header first time around. */
1487 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1489 err = got_error_from_errno("write");
1491 } else if (outlen != len - hdrlen) {
1492 err = got_error(GOT_ERR_IO);
1499 if (fsync(fd) != 0) {
1500 err = got_error_from_errno("fsync");
1505 if (rename(tmppath, ondisk_path) != 0) {
1506 err = got_error_from_errno3("rename", tmppath,
1513 if (chmod(ondisk_path,
1514 get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1515 err = got_error_from_errno2("chmod", ondisk_path);
1520 if (fd != -1 && close(fd) != 0 && err == NULL)
1521 err = got_error_from_errno("close");
1526 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1527 static const struct got_error *
1528 get_modified_file_content_status(unsigned char *status, FILE *f)
1530 const struct got_error *err = NULL;
1531 const char *markers[3] = {
1532 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1533 GOT_DIFF_CONFLICT_MARKER_SEP,
1534 GOT_DIFF_CONFLICT_MARKER_END
1539 const char delim[3] = {'\0', '\0', '\0'};
1541 while (*status == GOT_STATUS_MODIFY) {
1542 line = fparseln(f, &len, NULL, delim, 0);
1546 err = got_ferror(f, GOT_ERR_IO);
1550 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1551 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1553 *status = GOT_STATUS_CONFLICT;
1563 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1565 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1566 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1570 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1572 return !(ie->ctime_sec == sb->st_ctime &&
1573 ie->ctime_nsec == sb->st_ctimensec &&
1574 ie->mtime_sec == sb->st_mtime &&
1575 ie->mtime_nsec == sb->st_mtimensec &&
1576 ie->size == (sb->st_size & 0xffffffff) &&
1577 !xbit_differs(ie, sb->st_mode));
1580 static unsigned char
1581 get_staged_status(struct got_fileindex_entry *ie)
1583 switch (got_fileindex_entry_stage_get(ie)) {
1584 case GOT_FILEIDX_STAGE_ADD:
1585 return GOT_STATUS_ADD;
1586 case GOT_FILEIDX_STAGE_DELETE:
1587 return GOT_STATUS_DELETE;
1588 case GOT_FILEIDX_STAGE_MODIFY:
1589 return GOT_STATUS_MODIFY;
1591 return GOT_STATUS_NO_CHANGE;
1595 static const struct got_error *
1596 get_symlink_modification_status(unsigned char *status,
1597 struct got_fileindex_entry *ie, const char *abspath,
1598 int dirfd, const char *de_name, struct got_blob_object *blob)
1600 const struct got_error *err = NULL;
1601 char target_path[PATH_MAX];
1602 char etarget[PATH_MAX];
1604 size_t len, target_len = 0;
1605 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1606 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1608 *status = GOT_STATUS_NO_CHANGE;
1610 /* Blob object content specifies the target path of the link. */
1612 err = got_object_blob_read_block(&len, blob);
1615 if (len + target_len >= sizeof(target_path)) {
1617 * Should not happen. The blob contents were OK
1618 * when this symlink was installed.
1620 return got_error(GOT_ERR_NO_SPACE);
1623 /* Skip blob object header first time around. */
1624 memcpy(target_path + target_len, buf + hdrlen,
1626 target_len += len - hdrlen;
1630 target_path[target_len] = '\0';
1633 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1635 return got_error_from_errno2("readlinkat", abspath);
1637 elen = readlink(abspath, etarget, sizeof(etarget));
1639 return got_error_from_errno2("readlink", abspath);
1642 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1643 *status = GOT_STATUS_MODIFY;
1648 static const struct got_error *
1649 get_file_status(unsigned char *status, struct stat *sb,
1650 struct got_fileindex_entry *ie, const char *abspath,
1651 int dirfd, const char *de_name, struct got_repository *repo)
1653 const struct got_error *err = NULL;
1654 struct got_object_id id;
1659 struct got_blob_object *blob = NULL;
1661 unsigned char staged_status = get_staged_status(ie);
1663 *status = GOT_STATUS_NO_CHANGE;
1666 * Whenever the caller provides a directory descriptor and a
1667 * directory entry name for the file, use them! This prevents
1668 * race conditions if filesystem paths change beneath our feet.
1671 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1672 if (errno == ENOENT) {
1673 if (got_fileindex_entry_has_file_on_disk(ie))
1674 *status = GOT_STATUS_MISSING;
1676 *status = GOT_STATUS_DELETE;
1679 err = got_error_from_errno2("fstatat", abspath);
1683 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1684 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1685 return got_error_from_errno2("open", abspath);
1686 else if (fd == -1 && errno == ELOOP) {
1687 if (lstat(abspath, sb) == -1)
1688 return got_error_from_errno2("lstat", abspath);
1689 } else if (fd == -1 || fstat(fd, sb) == -1) {
1690 if (errno == ENOENT) {
1691 if (got_fileindex_entry_has_file_on_disk(ie))
1692 *status = GOT_STATUS_MISSING;
1694 *status = GOT_STATUS_DELETE;
1697 err = got_error_from_errno2("fstat", abspath);
1702 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1703 *status = GOT_STATUS_OBSTRUCTED;
1707 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1708 *status = GOT_STATUS_DELETE;
1710 } else if (!got_fileindex_entry_has_blob(ie) &&
1711 staged_status != GOT_STATUS_ADD) {
1712 *status = GOT_STATUS_ADD;
1716 if (!stat_info_differs(ie, sb))
1719 if (S_ISLNK(sb->st_mode) &&
1720 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1721 *status = GOT_STATUS_MODIFY;
1725 if (staged_status == GOT_STATUS_MODIFY ||
1726 staged_status == GOT_STATUS_ADD)
1727 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1729 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1731 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1735 if (S_ISLNK(sb->st_mode)) {
1736 err = get_symlink_modification_status(status, ie,
1737 abspath, dirfd, de_name, blob);
1742 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1744 err = got_error_from_errno2("openat", abspath);
1749 f = fdopen(fd, "r");
1751 err = got_error_from_errno2("fdopen", abspath);
1755 hdrlen = got_object_blob_get_hdrlen(blob);
1757 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1758 err = got_object_blob_read_block(&blen, blob);
1761 /* Skip length of blob object header first time around. */
1762 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1763 if (flen == 0 && ferror(f)) {
1764 err = got_error_from_errno("fread");
1769 *status = GOT_STATUS_MODIFY;
1771 } else if (flen == 0) {
1773 *status = GOT_STATUS_MODIFY;
1775 } else if (blen - hdrlen == flen) {
1776 /* Skip blob object header first time around. */
1777 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1778 *status = GOT_STATUS_MODIFY;
1782 *status = GOT_STATUS_MODIFY;
1788 if (*status == GOT_STATUS_MODIFY) {
1790 err = get_modified_file_content_status(status, f);
1791 } else if (xbit_differs(ie, sb->st_mode))
1792 *status = GOT_STATUS_MODE_CHANGE;
1795 got_object_blob_close(blob);
1796 if (f != NULL && fclose(f) == EOF && err == NULL)
1797 err = got_error_from_errno2("fclose", abspath);
1798 if (fd != -1 && close(fd) == -1 && err == NULL)
1799 err = got_error_from_errno2("close", abspath);
1804 * Update timestamps in the file index if a file is unmodified and
1805 * we had to run a full content comparison to find out.
1807 static const struct got_error *
1808 sync_timestamps(char *ondisk_path, unsigned char status,
1809 struct got_fileindex_entry *ie, struct stat *sb)
1811 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1812 return got_fileindex_entry_update(ie, ondisk_path,
1813 ie->blob_sha1, ie->commit_sha1, 1);
1818 static const struct got_error *
1819 update_blob(struct got_worktree *worktree,
1820 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1821 struct got_tree_entry *te, const char *path,
1822 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1825 const struct got_error *err = NULL;
1826 struct got_blob_object *blob = NULL;
1828 unsigned char status = GOT_STATUS_NO_CHANGE;
1831 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1832 return got_error_from_errno("asprintf");
1835 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1836 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1839 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1843 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1844 sb.st_mode = got_fileindex_perms_to_st(ie);
1846 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1847 status = GOT_STATUS_UNVERSIONED;
1850 if (status == GOT_STATUS_OBSTRUCTED) {
1851 err = (*progress_cb)(progress_arg, status, path);
1854 if (status == GOT_STATUS_CONFLICT) {
1855 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1860 if (ie && status != GOT_STATUS_MISSING &&
1861 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1862 if (got_fileindex_entry_has_commit(ie) &&
1863 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1864 SHA1_DIGEST_LENGTH) == 0) {
1865 err = sync_timestamps(ondisk_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 err = sync_timestamps(ondisk_path, status, ie, &sb);
1880 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1884 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1885 int update_timestamps;
1886 struct got_blob_object *blob2 = NULL;
1887 char *label_orig = NULL;
1888 if (got_fileindex_entry_has_blob(ie)) {
1889 struct got_object_id id2;
1890 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1891 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1895 if (got_fileindex_entry_has_commit(ie)) {
1896 char id_str[SHA1_DIGEST_STRING_LENGTH];
1897 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1898 sizeof(id_str)) == NULL) {
1899 err = got_error_path(id_str,
1900 GOT_ERR_BAD_OBJ_ID_STR);
1903 if (asprintf(&label_orig, "%s: commit %s",
1904 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1905 err = got_error_from_errno("asprintf");
1909 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1911 err = got_object_blob_read_to_str(&link_target, blob);
1914 err = merge_symlink(worktree, blob2, ondisk_path, path,
1915 label_orig, link_target, worktree->base_commit_id,
1916 repo, progress_cb, progress_arg);
1919 err = merge_blob(&update_timestamps, worktree, blob2,
1920 ondisk_path, path, sb.st_mode, label_orig, blob,
1921 worktree->base_commit_id, repo,
1922 progress_cb, progress_arg);
1926 got_object_blob_close(blob2);
1930 * Do not update timestamps of files with local changes.
1931 * Otherwise, a future status walk would treat them as
1932 * unmodified files again.
1934 err = got_fileindex_entry_update(ie, ondisk_path,
1935 blob->id.sha1, worktree->base_commit_id->sha1,
1937 } else if (status == GOT_STATUS_MODE_CHANGE) {
1938 err = got_fileindex_entry_update(ie, ondisk_path,
1939 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1940 } else if (status == GOT_STATUS_DELETE) {
1941 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1944 err = got_fileindex_entry_update(ie, ondisk_path,
1945 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1949 int is_bad_symlink = 0;
1950 if (S_ISLNK(te->mode)) {
1951 err = install_symlink(&is_bad_symlink, worktree,
1952 ondisk_path, path, blob,
1953 status == GOT_STATUS_MISSING, 0,
1954 status == GOT_STATUS_UNVERSIONED, repo,
1955 progress_cb, progress_arg);
1957 err = install_blob(worktree, ondisk_path, path,
1958 te->mode, sb.st_mode, blob,
1959 status == GOT_STATUS_MISSING, 0, 0,
1960 status == GOT_STATUS_UNVERSIONED, repo,
1961 progress_cb, progress_arg);
1967 err = got_fileindex_entry_update(ie, ondisk_path,
1968 blob->id.sha1, worktree->base_commit_id->sha1, 1);
1970 err = create_fileindex_entry(&ie, fileindex,
1971 worktree->base_commit_id, ondisk_path, path,
1977 if (is_bad_symlink) {
1978 got_fileindex_entry_filetype_set(ie,
1979 GOT_FILEIDX_MODE_BAD_SYMLINK);
1982 got_object_blob_close(blob);
1988 static const struct got_error *
1989 remove_ondisk_file(const char *root_path, const char *path)
1991 const struct got_error *err = NULL;
1992 char *ondisk_path = NULL;
1994 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1995 return got_error_from_errno("asprintf");
1997 if (unlink(ondisk_path) == -1) {
1998 if (errno != ENOENT)
1999 err = got_error_from_errno2("unlink", ondisk_path);
2001 char *parent = dirname(ondisk_path);
2002 while (parent && strcmp(parent, root_path) != 0) {
2003 if (rmdir(parent) == -1) {
2004 if (errno != ENOTEMPTY)
2005 err = got_error_from_errno2("rmdir",
2009 parent = dirname(parent);
2016 static const struct got_error *
2017 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2018 struct got_fileindex_entry *ie, struct got_repository *repo,
2019 got_worktree_checkout_cb progress_cb, void *progress_arg)
2021 const struct got_error *err = NULL;
2022 unsigned char status;
2026 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2027 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2029 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2031 return got_error_from_errno("asprintf");
2033 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2037 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2038 char ondisk_target[PATH_MAX];
2039 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2040 sizeof(ondisk_target));
2041 if (ondisk_len == -1) {
2042 err = got_error_from_errno2("readlink", ondisk_path);
2045 ondisk_target[ondisk_len] = '\0';
2046 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2047 NULL, NULL, /* XXX pass common ancestor info? */
2048 ondisk_target, ondisk_path);
2051 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2056 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2057 status == GOT_STATUS_ADD) {
2058 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2062 * Preserve the working file and change the deleted blob's
2063 * entry into a schedule-add entry.
2065 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
2068 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2071 if (status == GOT_STATUS_NO_CHANGE) {
2072 err = remove_ondisk_file(worktree->root_path, ie->path);
2076 got_fileindex_entry_remove(fileindex, ie);
2083 struct diff_cb_arg {
2084 struct got_fileindex *fileindex;
2085 struct got_worktree *worktree;
2086 struct got_repository *repo;
2087 got_worktree_checkout_cb progress_cb;
2089 got_cancel_cb cancel_cb;
2093 static const struct got_error *
2094 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2095 struct got_tree_entry *te, const char *parent_path)
2097 struct diff_cb_arg *a = arg;
2099 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2100 return got_error(GOT_ERR_CANCELLED);
2102 return update_blob(a->worktree, a->fileindex, ie, te,
2103 ie->path, a->repo, a->progress_cb, a->progress_arg);
2106 static const struct got_error *
2107 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2109 struct diff_cb_arg *a = arg;
2111 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2112 return got_error(GOT_ERR_CANCELLED);
2114 return delete_blob(a->worktree, a->fileindex, ie,
2115 a->repo, a->progress_cb, a->progress_arg);
2118 static const struct got_error *
2119 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2121 struct diff_cb_arg *a = arg;
2122 const struct got_error *err;
2125 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2126 return got_error(GOT_ERR_CANCELLED);
2128 if (got_object_tree_entry_is_submodule(te))
2131 if (asprintf(&path, "%s%s%s", parent_path,
2132 parent_path[0] ? "/" : "", te->name)
2134 return got_error_from_errno("asprintf");
2136 if (S_ISDIR(te->mode))
2137 err = add_dir_on_disk(a->worktree, path);
2139 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2140 a->repo, a->progress_cb, a->progress_arg);
2146 const struct got_error *
2147 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2149 uint32_t uuid_status;
2151 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2152 if (uuid_status != uuid_s_ok) {
2154 return got_error_uuid(uuid_status, "uuid_to_string");
2160 static const struct got_error *
2161 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2163 const struct got_error *err = NULL;
2164 char *uuidstr = NULL;
2168 err = got_worktree_get_uuid(&uuidstr, worktree);
2172 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2173 err = got_error_from_errno("asprintf");
2180 const struct got_error *
2181 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2183 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2186 static const struct got_error *
2187 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2189 return get_ref_name(refname, worktree,
2190 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2193 static const struct got_error *
2194 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2196 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2199 static const struct got_error *
2200 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2202 return get_ref_name(refname, worktree,
2203 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2206 static const struct got_error *
2207 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2209 return get_ref_name(refname, worktree,
2210 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2213 static const struct got_error *
2214 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2216 return get_ref_name(refname, worktree,
2217 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2220 static const struct got_error *
2221 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2223 return get_ref_name(refname, worktree,
2224 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2227 static const struct got_error *
2228 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2230 return get_ref_name(refname, worktree,
2231 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2234 static const struct got_error *
2235 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2237 return get_ref_name(refname, worktree,
2238 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2241 const struct got_error *
2242 got_worktree_get_histedit_script_path(char **path,
2243 struct got_worktree *worktree)
2245 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2246 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2248 return got_error_from_errno("asprintf");
2254 * Prevent Git's garbage collector from deleting our base commit by
2255 * setting a reference to our base commit's ID.
2257 static const struct got_error *
2258 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2260 const struct got_error *err = NULL;
2261 struct got_reference *ref = NULL;
2264 err = got_worktree_get_base_ref_name(&refname, worktree);
2268 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2272 err = got_ref_write(ref, repo);
2280 static const struct got_error *
2281 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2283 const struct got_error *err = NULL;
2285 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2286 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2287 err = got_error_from_errno("asprintf");
2288 *fileindex_path = NULL;
2294 static const struct got_error *
2295 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2296 struct got_worktree *worktree)
2298 const struct got_error *err = NULL;
2301 *fileindex_path = NULL;
2302 *fileindex = got_fileindex_alloc();
2303 if (*fileindex == NULL)
2304 return got_error_from_errno("got_fileindex_alloc");
2306 err = get_fileindex_path(fileindex_path, worktree);
2310 index = fopen(*fileindex_path, "rb");
2311 if (index == NULL) {
2312 if (errno != ENOENT)
2313 err = got_error_from_errno2("fopen", *fileindex_path);
2315 err = got_fileindex_read(*fileindex, index);
2316 if (fclose(index) != 0 && err == NULL)
2317 err = got_error_from_errno("fclose");
2321 free(*fileindex_path);
2322 *fileindex_path = NULL;
2323 got_fileindex_free(*fileindex);
2329 struct bump_base_commit_id_arg {
2330 struct got_object_id *base_commit_id;
2333 const char *entry_name;
2334 got_worktree_checkout_cb progress_cb;
2338 /* Bump base commit ID of all files within an updated part of the work tree. */
2339 static const struct got_error *
2340 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2342 const struct got_error *err;
2343 struct bump_base_commit_id_arg *a = arg;
2345 if (a->entry_name) {
2346 if (strcmp(ie->path, a->path) != 0)
2348 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2351 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2352 SHA1_DIGEST_LENGTH) == 0)
2355 if (a->progress_cb) {
2356 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2361 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2365 static const struct got_error *
2366 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2368 const struct got_error *err = NULL;
2369 char *new_fileindex_path = NULL;
2370 FILE *new_index = NULL;
2371 struct timespec timeout;
2373 err = got_opentemp_named(&new_fileindex_path, &new_index,
2378 err = got_fileindex_write(fileindex, new_index);
2382 if (rename(new_fileindex_path, fileindex_path) != 0) {
2383 err = got_error_from_errno3("rename", new_fileindex_path,
2385 unlink(new_fileindex_path);
2389 * Sleep for a short amount of time to ensure that files modified after
2390 * this program exits have a different time stamp from the one which
2391 * was recorded in the file index.
2394 timeout.tv_nsec = 1;
2395 nanosleep(&timeout, NULL);
2399 free(new_fileindex_path);
2403 static const struct got_error *
2404 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2405 struct got_object_id **tree_id, const char *wt_relpath,
2406 struct got_worktree *worktree, struct got_repository *repo)
2408 const struct got_error *err = NULL;
2409 struct got_object_id *id = NULL;
2410 char *in_repo_path = NULL;
2411 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2413 *entry_type = GOT_OBJ_TYPE_ANY;
2414 *tree_relpath = NULL;
2417 if (wt_relpath[0] == '\0') {
2418 /* Check out all files within the work tree. */
2419 *entry_type = GOT_OBJ_TYPE_TREE;
2420 *tree_relpath = strdup("");
2421 if (*tree_relpath == NULL) {
2422 err = got_error_from_errno("strdup");
2425 err = got_object_id_by_path(tree_id, repo,
2426 worktree->base_commit_id, worktree->path_prefix);
2432 /* Check out a subset of files in the work tree. */
2434 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2435 is_root_wt ? "" : "/", wt_relpath) == -1) {
2436 err = got_error_from_errno("asprintf");
2440 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2446 in_repo_path = NULL;
2448 err = got_object_get_type(entry_type, repo, id);
2452 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2453 /* Check out a single file. */
2454 if (strchr(wt_relpath, '/') == NULL) {
2455 /* Check out a single file in work tree's root dir. */
2456 in_repo_path = strdup(worktree->path_prefix);
2457 if (in_repo_path == NULL) {
2458 err = got_error_from_errno("strdup");
2461 *tree_relpath = strdup("");
2462 if (*tree_relpath == NULL) {
2463 err = got_error_from_errno("strdup");
2467 /* Check out a single file in a subdirectory. */
2468 err = got_path_dirname(tree_relpath, wt_relpath);
2471 if (asprintf(&in_repo_path, "%s%s%s",
2472 worktree->path_prefix, is_root_wt ? "" : "/",
2473 *tree_relpath) == -1) {
2474 err = got_error_from_errno("asprintf");
2478 err = got_object_id_by_path(tree_id, repo,
2479 worktree->base_commit_id, in_repo_path);
2481 /* Check out all files within a subdirectory. */
2482 *tree_id = got_object_id_dup(id);
2483 if (*tree_id == NULL) {
2484 err = got_error_from_errno("got_object_id_dup");
2487 *tree_relpath = strdup(wt_relpath);
2488 if (*tree_relpath == NULL) {
2489 err = got_error_from_errno("strdup");
2497 *entry_type = GOT_OBJ_TYPE_ANY;
2498 free(*tree_relpath);
2499 *tree_relpath = NULL;
2506 static const struct got_error *
2507 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2508 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2509 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2510 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2512 const struct got_error *err = NULL;
2513 struct got_commit_object *commit = NULL;
2514 struct got_tree_object *tree = NULL;
2515 struct got_fileindex_diff_tree_cb diff_cb;
2516 struct diff_cb_arg arg;
2518 err = ref_base_commit(worktree, repo);
2520 if (!(err->code == GOT_ERR_ERRNO &&
2521 (errno == EACCES || errno == EROFS)))
2523 err = (*progress_cb)(progress_arg,
2524 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2529 err = got_object_open_as_commit(&commit, repo,
2530 worktree->base_commit_id);
2534 err = got_object_open_as_tree(&tree, repo, tree_id);
2539 got_object_tree_find_entry(tree, entry_name) == NULL) {
2540 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2544 diff_cb.diff_old_new = diff_old_new;
2545 diff_cb.diff_old = diff_old;
2546 diff_cb.diff_new = diff_new;
2547 arg.fileindex = fileindex;
2548 arg.worktree = worktree;
2550 arg.progress_cb = progress_cb;
2551 arg.progress_arg = progress_arg;
2552 arg.cancel_cb = cancel_cb;
2553 arg.cancel_arg = cancel_arg;
2554 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2555 entry_name, repo, &diff_cb, &arg);
2558 got_object_tree_close(tree);
2560 got_object_commit_close(commit);
2564 const struct got_error *
2565 got_worktree_checkout_files(struct got_worktree *worktree,
2566 struct got_pathlist_head *paths, struct got_repository *repo,
2567 got_worktree_checkout_cb progress_cb, void *progress_arg,
2568 got_cancel_cb cancel_cb, void *cancel_arg)
2570 const struct got_error *err = NULL, *sync_err, *unlockerr;
2571 struct got_commit_object *commit = NULL;
2572 struct got_tree_object *tree = NULL;
2573 struct got_fileindex *fileindex = NULL;
2574 char *fileindex_path = NULL;
2575 struct got_pathlist_entry *pe;
2576 struct tree_path_data {
2577 SIMPLEQ_ENTRY(tree_path_data) entry;
2578 struct got_object_id *tree_id;
2583 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2585 SIMPLEQ_INIT(&tree_paths);
2587 err = lock_worktree(worktree, LOCK_EX);
2591 /* Map all specified paths to in-repository trees. */
2592 TAILQ_FOREACH(pe, paths, entry) {
2593 tpd = malloc(sizeof(*tpd));
2595 err = got_error_from_errno("malloc");
2599 err = find_tree_entry_for_checkout(&tpd->entry_type,
2600 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2606 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2607 err = got_path_basename(&tpd->entry_name, pe->path);
2615 tpd->entry_name = NULL;
2617 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2621 * Read the file index.
2622 * Checking out files is supposed to be an idempotent operation.
2623 * If the on-disk file index is incomplete we will try to complete it.
2625 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2629 tpd = SIMPLEQ_FIRST(&tree_paths);
2630 TAILQ_FOREACH(pe, paths, entry) {
2631 struct bump_base_commit_id_arg bbc_arg;
2633 err = checkout_files(worktree, fileindex, tpd->relpath,
2634 tpd->tree_id, tpd->entry_name, repo,
2635 progress_cb, progress_arg, cancel_cb, cancel_arg);
2639 bbc_arg.base_commit_id = worktree->base_commit_id;
2640 bbc_arg.entry_name = tpd->entry_name;
2641 bbc_arg.path = pe->path;
2642 bbc_arg.path_len = pe->path_len;
2643 bbc_arg.progress_cb = progress_cb;
2644 bbc_arg.progress_arg = progress_arg;
2645 err = got_fileindex_for_each_entry_safe(fileindex,
2646 bump_base_commit_id, &bbc_arg);
2650 tpd = SIMPLEQ_NEXT(tpd, entry);
2652 sync_err = sync_fileindex(fileindex, fileindex_path);
2653 if (sync_err && err == NULL)
2656 free(fileindex_path);
2658 got_object_tree_close(tree);
2660 got_object_commit_close(commit);
2662 got_fileindex_free(fileindex);
2663 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2664 tpd = SIMPLEQ_FIRST(&tree_paths);
2665 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2670 unlockerr = lock_worktree(worktree, LOCK_SH);
2671 if (unlockerr && err == NULL)
2676 struct merge_file_cb_arg {
2677 struct got_worktree *worktree;
2678 struct got_fileindex *fileindex;
2679 got_worktree_checkout_cb progress_cb;
2681 got_cancel_cb cancel_cb;
2683 const char *label_orig;
2684 struct got_object_id *commit_id2;
2687 static const struct got_error *
2688 merge_file_cb(void *arg, struct got_blob_object *blob1,
2689 struct got_blob_object *blob2, struct got_object_id *id1,
2690 struct got_object_id *id2, const char *path1, const char *path2,
2691 mode_t mode1, mode_t mode2, struct got_repository *repo)
2693 static const struct got_error *err = NULL;
2694 struct merge_file_cb_arg *a = arg;
2695 struct got_fileindex_entry *ie;
2696 char *ondisk_path = NULL;
2698 unsigned char status;
2699 int local_changes_subsumed;
2701 if (blob1 && blob2) {
2702 ie = got_fileindex_entry_get(a->fileindex, path2,
2705 return (*a->progress_cb)(a->progress_arg,
2706 GOT_STATUS_MISSING, path2);
2708 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2710 return got_error_from_errno("asprintf");
2712 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2717 if (status == GOT_STATUS_DELETE) {
2718 err = (*a->progress_cb)(a->progress_arg,
2719 GOT_STATUS_MERGE, path2);
2722 if (status != GOT_STATUS_NO_CHANGE &&
2723 status != GOT_STATUS_MODIFY &&
2724 status != GOT_STATUS_CONFLICT &&
2725 status != GOT_STATUS_ADD) {
2726 err = (*a->progress_cb)(a->progress_arg, status, path2);
2730 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2732 err = got_object_blob_read_to_str(&link_target2, blob2);
2735 err = merge_symlink(a->worktree, blob1, ondisk_path,
2736 path2, a->label_orig, link_target2, a->commit_id2,
2737 repo, a->progress_cb, a->progress_arg);
2740 err = merge_blob(&local_changes_subsumed, a->worktree,
2741 blob1, ondisk_path, path2, sb.st_mode,
2742 a->label_orig, blob2, a->commit_id2, repo,
2743 a->progress_cb, a->progress_arg);
2746 ie = got_fileindex_entry_get(a->fileindex, path1,
2749 return (*a->progress_cb)(a->progress_arg,
2750 GOT_STATUS_MISSING, path1);
2752 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2754 return got_error_from_errno("asprintf");
2756 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2762 case GOT_STATUS_NO_CHANGE:
2763 err = (*a->progress_cb)(a->progress_arg,
2764 GOT_STATUS_DELETE, path1);
2767 err = remove_ondisk_file(a->worktree->root_path, path1);
2771 got_fileindex_entry_mark_deleted_from_disk(ie);
2773 case GOT_STATUS_DELETE:
2774 case GOT_STATUS_MISSING:
2775 err = (*a->progress_cb)(a->progress_arg,
2776 GOT_STATUS_DELETE, path1);
2780 got_fileindex_entry_mark_deleted_from_disk(ie);
2782 case GOT_STATUS_ADD:
2783 case GOT_STATUS_MODIFY:
2784 case GOT_STATUS_CONFLICT:
2785 err = (*a->progress_cb)(a->progress_arg,
2786 GOT_STATUS_CANNOT_DELETE, path1);
2790 case GOT_STATUS_OBSTRUCTED:
2791 err = (*a->progress_cb)(a->progress_arg, status, path1);
2799 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2801 return got_error_from_errno("asprintf");
2802 ie = got_fileindex_entry_get(a->fileindex, path2,
2805 err = get_file_status(&status, &sb, ie, ondisk_path,
2809 if (status != GOT_STATUS_NO_CHANGE &&
2810 status != GOT_STATUS_MODIFY &&
2811 status != GOT_STATUS_CONFLICT &&
2812 status != GOT_STATUS_ADD) {
2813 err = (*a->progress_cb)(a->progress_arg,
2817 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2819 err = got_object_blob_read_to_str(&link_target2,
2823 err = merge_symlink(a->worktree, NULL,
2824 ondisk_path, path2, a->label_orig,
2825 link_target2, a->commit_id2, repo,
2826 a->progress_cb, a->progress_arg);
2828 } else if (S_ISREG(sb.st_mode)) {
2829 err = merge_blob(&local_changes_subsumed,
2830 a->worktree, NULL, ondisk_path, path2,
2831 sb.st_mode, a->label_orig, blob2,
2832 a->commit_id2, repo, a->progress_cb,
2835 err = got_error_path(ondisk_path,
2836 GOT_ERR_FILE_OBSTRUCTED);
2840 if (status == GOT_STATUS_DELETE) {
2841 err = got_fileindex_entry_update(ie,
2842 ondisk_path, blob2->id.sha1,
2843 a->worktree->base_commit_id->sha1, 0);
2848 int is_bad_symlink = 0;
2849 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2850 if (S_ISLNK(mode2)) {
2851 err = install_symlink(&is_bad_symlink,
2852 a->worktree, ondisk_path, path2, blob2, 0,
2853 0, 1, repo, a->progress_cb, a->progress_arg);
2855 err = install_blob(a->worktree, ondisk_path, path2,
2856 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2857 a->progress_cb, a->progress_arg);
2861 err = got_fileindex_entry_alloc(&ie, path2);
2864 err = got_fileindex_entry_update(ie, ondisk_path,
2867 got_fileindex_entry_free(ie);
2870 err = got_fileindex_entry_add(a->fileindex, ie);
2872 got_fileindex_entry_free(ie);
2875 if (is_bad_symlink) {
2876 got_fileindex_entry_filetype_set(ie,
2877 GOT_FILEIDX_MODE_BAD_SYMLINK);
2886 struct check_merge_ok_arg {
2887 struct got_worktree *worktree;
2888 struct got_repository *repo;
2891 static const struct got_error *
2892 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2894 const struct got_error *err = NULL;
2895 struct check_merge_ok_arg *a = arg;
2896 unsigned char status;
2900 /* Reject merges into a work tree with mixed base commits. */
2901 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2902 SHA1_DIGEST_LENGTH))
2903 return got_error(GOT_ERR_MIXED_COMMITS);
2905 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2907 return got_error_from_errno("asprintf");
2909 /* Reject merges into a work tree with conflicted files. */
2910 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2913 if (status == GOT_STATUS_CONFLICT)
2914 return got_error(GOT_ERR_CONFLICTS);
2919 static const struct got_error *
2920 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2921 const char *fileindex_path, struct got_object_id *commit_id1,
2922 struct got_object_id *commit_id2, struct got_repository *repo,
2923 got_worktree_checkout_cb progress_cb, void *progress_arg,
2924 got_cancel_cb cancel_cb, void *cancel_arg)
2926 const struct got_error *err = NULL, *sync_err;
2927 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2928 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2929 struct merge_file_cb_arg arg;
2930 char *label_orig = NULL;
2935 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2936 worktree->path_prefix);
2940 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2944 err = got_object_id_str(&id_str, commit_id1);
2948 if (asprintf(&label_orig, "%s: commit %s",
2949 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2950 err = got_error_from_errno("asprintf");
2957 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2958 worktree->path_prefix);
2962 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2966 arg.worktree = worktree;
2967 arg.fileindex = fileindex;
2968 arg.progress_cb = progress_cb;
2969 arg.progress_arg = progress_arg;
2970 arg.cancel_cb = cancel_cb;
2971 arg.cancel_arg = cancel_arg;
2972 arg.label_orig = label_orig;
2973 arg.commit_id2 = commit_id2;
2974 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2975 sync_err = sync_fileindex(fileindex, fileindex_path);
2976 if (sync_err && err == NULL)
2980 got_object_tree_close(tree1);
2982 got_object_tree_close(tree2);
2987 const struct got_error *
2988 got_worktree_merge_files(struct got_worktree *worktree,
2989 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2990 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2991 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2993 const struct got_error *err, *unlockerr;
2994 char *fileindex_path = NULL;
2995 struct got_fileindex *fileindex = NULL;
2996 struct check_merge_ok_arg mok_arg;
2998 err = lock_worktree(worktree, LOCK_EX);
3002 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3006 mok_arg.worktree = worktree;
3007 mok_arg.repo = repo;
3008 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
3013 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3014 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
3017 got_fileindex_free(fileindex);
3018 free(fileindex_path);
3019 unlockerr = lock_worktree(worktree, LOCK_SH);
3020 if (unlockerr && err == NULL)
3025 struct diff_dir_cb_arg {
3026 struct got_fileindex *fileindex;
3027 struct got_worktree *worktree;
3028 const char *status_path;
3029 size_t status_path_len;
3030 struct got_repository *repo;
3031 got_worktree_status_cb status_cb;
3033 got_cancel_cb cancel_cb;
3035 /* A pathlist containing per-directory pathlists of ignore patterns. */
3036 struct got_pathlist_head ignores;
3037 int report_unchanged;
3041 static const struct got_error *
3042 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3043 int dirfd, const char *de_name,
3044 got_worktree_status_cb status_cb, void *status_arg,
3045 struct got_repository *repo, int report_unchanged)
3047 const struct got_error *err = NULL;
3048 unsigned char status = GOT_STATUS_NO_CHANGE;
3049 unsigned char staged_status = get_staged_status(ie);
3051 struct got_object_id blob_id, commit_id, staged_blob_id;
3052 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3053 struct got_object_id *staged_blob_idp = NULL;
3055 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3059 if (status == GOT_STATUS_NO_CHANGE &&
3060 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3063 if (got_fileindex_entry_has_blob(ie)) {
3064 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3065 blob_idp = &blob_id;
3067 if (got_fileindex_entry_has_commit(ie)) {
3068 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3069 commit_idp = &commit_id;
3071 if (staged_status == GOT_STATUS_ADD ||
3072 staged_status == GOT_STATUS_MODIFY) {
3073 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3074 SHA1_DIGEST_LENGTH);
3075 staged_blob_idp = &staged_blob_id;
3078 return (*status_cb)(status_arg, status, staged_status,
3079 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3082 static const struct got_error *
3083 status_old_new(void *arg, struct got_fileindex_entry *ie,
3084 struct dirent *de, const char *parent_path, int dirfd)
3086 const struct got_error *err = NULL;
3087 struct diff_dir_cb_arg *a = arg;
3090 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3091 return got_error(GOT_ERR_CANCELLED);
3093 if (got_path_cmp(parent_path, a->status_path,
3094 strlen(parent_path), a->status_path_len) != 0 &&
3095 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3098 if (parent_path[0]) {
3099 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3100 parent_path, de->d_name) == -1)
3101 return got_error_from_errno("asprintf");
3103 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3105 return got_error_from_errno("asprintf");
3108 err = report_file_status(ie, abspath, dirfd, de->d_name,
3109 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3114 static const struct got_error *
3115 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3117 struct diff_dir_cb_arg *a = arg;
3118 struct got_object_id blob_id, commit_id;
3119 unsigned char status;
3121 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3122 return got_error(GOT_ERR_CANCELLED);
3124 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3127 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3128 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3129 if (got_fileindex_entry_has_file_on_disk(ie))
3130 status = GOT_STATUS_MISSING;
3132 status = GOT_STATUS_DELETE;
3133 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3134 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3138 free_ignorelist(struct got_pathlist_head *ignorelist)
3140 struct got_pathlist_entry *pe;
3142 TAILQ_FOREACH(pe, ignorelist, entry)
3143 free((char *)pe->path);
3144 got_pathlist_free(ignorelist);
3148 free_ignores(struct got_pathlist_head *ignores)
3150 struct got_pathlist_entry *pe;
3152 TAILQ_FOREACH(pe, ignores, entry) {
3153 struct got_pathlist_head *ignorelist = pe->data;
3154 free_ignorelist(ignorelist);
3155 free((char *)pe->path);
3157 got_pathlist_free(ignores);
3160 static const struct got_error *
3161 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3163 const struct got_error *err = NULL;
3164 struct got_pathlist_entry *pe = NULL;
3165 struct got_pathlist_head *ignorelist;
3166 char *line = NULL, *pattern, *dirpath = NULL;
3167 size_t linesize = 0;
3170 ignorelist = calloc(1, sizeof(*ignorelist));
3171 if (ignorelist == NULL)
3172 return got_error_from_errno("calloc");
3173 TAILQ_INIT(ignorelist);
3175 while ((linelen = getline(&line, &linesize, f)) != -1) {
3176 if (linelen > 0 && line[linelen - 1] == '\n')
3177 line[linelen - 1] = '\0';
3179 /* Git's ignores may contain comments. */
3183 /* Git's negated patterns are not (yet?) supported. */
3187 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3189 err = got_error_from_errno("asprintf");
3192 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3197 err = got_error_from_errno("getline");
3201 dirpath = strdup(path);
3202 if (dirpath == NULL) {
3203 err = got_error_from_errno("strdup");
3206 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3209 if (err || pe == NULL) {
3211 free_ignorelist(ignorelist);
3217 match_ignores(struct got_pathlist_head *ignores, const char *path)
3219 struct got_pathlist_entry *pe;
3221 /* Handle patterns which match in all directories. */
3222 TAILQ_FOREACH(pe, ignores, entry) {
3223 struct got_pathlist_head *ignorelist = pe->data;
3224 struct got_pathlist_entry *pi;
3226 TAILQ_FOREACH(pi, ignorelist, entry) {
3227 const char *p, *pattern = pi->path;
3229 if (strncmp(pattern, "**/", 3) != 0)
3234 if (fnmatch(pattern, p,
3235 FNM_PATHNAME | FNM_LEADING_DIR)) {
3236 /* Retry in next directory. */
3237 while (*p && *p != '/')
3249 * The ignores pathlist contains ignore lists from children before
3250 * parents, so we can find the most specific ignorelist by walking
3251 * ignores backwards.
3253 pe = TAILQ_LAST(ignores, got_pathlist_head);
3255 if (got_path_is_child(path, pe->path, pe->path_len)) {
3256 struct got_pathlist_head *ignorelist = pe->data;
3257 struct got_pathlist_entry *pi;
3258 TAILQ_FOREACH(pi, ignorelist, entry) {
3259 const char *pattern = pi->path;
3260 int flags = FNM_LEADING_DIR;
3261 if (strstr(pattern, "/**/") == NULL)
3262 flags |= FNM_PATHNAME;
3263 if (fnmatch(pattern, path, flags))
3268 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3274 static const struct got_error *
3275 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3276 const char *path, int dirfd, const char *ignores_filename)
3278 const struct got_error *err = NULL;
3281 FILE *ignoresfile = NULL;
3283 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3284 path[0] ? "/" : "", ignores_filename) == -1)
3285 return got_error_from_errno("asprintf");
3288 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3290 if (errno != ENOENT && errno != EACCES)
3291 err = got_error_from_errno2("openat",
3294 ignoresfile = fdopen(fd, "r");
3295 if (ignoresfile == NULL)
3296 err = got_error_from_errno2("fdopen",
3300 err = read_ignores(ignores, path, ignoresfile);
3304 ignoresfile = fopen(ignorespath, "r");
3305 if (ignoresfile == NULL) {
3306 if (errno != ENOENT && errno != EACCES)
3307 err = got_error_from_errno2("fopen",
3310 err = read_ignores(ignores, path, ignoresfile);
3313 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3314 err = got_error_from_errno2("fclose", path);
3315 if (fd != -1 && close(fd) == -1 && err == NULL)
3316 err = got_error_from_errno2("close", path);
3321 static const struct got_error *
3322 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3324 const struct got_error *err = NULL;
3325 struct diff_dir_cb_arg *a = arg;
3328 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3329 return got_error(GOT_ERR_CANCELLED);
3331 if (parent_path[0]) {
3332 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3333 return got_error_from_errno("asprintf");
3338 if (de->d_type != DT_DIR &&
3339 got_path_is_child(path, a->status_path, a->status_path_len)
3340 && !match_ignores(&a->ignores, path))
3341 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3342 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3348 static const struct got_error *
3349 status_traverse(void *arg, const char *path, int dirfd)
3351 const struct got_error *err = NULL;
3352 struct diff_dir_cb_arg *a = arg;
3357 err = add_ignores(&a->ignores, a->worktree->root_path,
3358 path, dirfd, ".cvsignore");
3362 err = add_ignores(&a->ignores, a->worktree->root_path, path,
3363 dirfd, ".gitignore");
3368 static const struct got_error *
3369 report_single_file_status(const char *path, const char *ondisk_path,
3370 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3371 void *status_arg, struct got_repository *repo, int report_unchanged)
3373 struct got_fileindex_entry *ie;
3376 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3378 return report_file_status(ie, ondisk_path, -1, NULL,
3379 status_cb, status_arg, repo, report_unchanged);
3381 if (lstat(ondisk_path, &sb) == -1) {
3382 if (errno != ENOENT)
3383 return got_error_from_errno2("lstat", ondisk_path);
3384 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3385 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3389 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3390 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3391 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3396 static const struct got_error *
3397 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3398 const char *root_path, const char *path)
3400 const struct got_error *err;
3401 char *parent_path, *next_parent_path = NULL;
3403 err = add_ignores(ignores, root_path, "", -1,
3408 err = add_ignores(ignores, root_path, "", -1,
3413 err = got_path_dirname(&parent_path, path);
3415 if (err->code == GOT_ERR_BAD_PATH)
3416 return NULL; /* cannot traverse parent */
3420 err = add_ignores(ignores, root_path, parent_path, -1,
3424 err = add_ignores(ignores, root_path, parent_path, -1,
3428 err = got_path_dirname(&next_parent_path, parent_path);
3430 if (err->code == GOT_ERR_BAD_PATH)
3431 err = NULL; /* traversed everything */
3435 parent_path = next_parent_path;
3436 next_parent_path = NULL;
3440 free(next_parent_path);
3444 static const struct got_error *
3445 worktree_status(struct got_worktree *worktree, const char *path,
3446 struct got_fileindex *fileindex, struct got_repository *repo,
3447 got_worktree_status_cb status_cb, void *status_arg,
3448 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3449 int report_unchanged)
3451 const struct got_error *err = NULL;
3453 struct got_fileindex_diff_dir_cb fdiff_cb;
3454 struct diff_dir_cb_arg arg;
3455 char *ondisk_path = NULL;
3457 TAILQ_INIT(&arg.ignores);
3459 if (asprintf(&ondisk_path, "%s%s%s",
3460 worktree->root_path, path[0] ? "/" : "", path) == -1)
3461 return got_error_from_errno("asprintf");
3463 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3465 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3467 err = got_error_from_errno2("open", ondisk_path);
3469 err = report_single_file_status(path, ondisk_path,
3470 fileindex, status_cb, status_arg, repo,
3473 fdiff_cb.diff_old_new = status_old_new;
3474 fdiff_cb.diff_old = status_old;
3475 fdiff_cb.diff_new = status_new;
3476 fdiff_cb.diff_traverse = status_traverse;
3477 arg.fileindex = fileindex;
3478 arg.worktree = worktree;
3479 arg.status_path = path;
3480 arg.status_path_len = strlen(path);
3482 arg.status_cb = status_cb;
3483 arg.status_arg = status_arg;
3484 arg.cancel_cb = cancel_cb;
3485 arg.cancel_arg = cancel_arg;
3486 arg.report_unchanged = report_unchanged;
3487 arg.no_ignores = no_ignores;
3489 err = add_ignores_from_parent_paths(&arg.ignores,
3490 worktree->root_path, path);
3494 err = got_fileindex_diff_dir(fileindex, fd,
3495 worktree->root_path, path, repo, &fdiff_cb, &arg);
3498 free_ignores(&arg.ignores);
3499 if (fd != -1 && close(fd) != 0 && err == NULL)
3500 err = got_error_from_errno("close");
3505 const struct got_error *
3506 got_worktree_status(struct got_worktree *worktree,
3507 struct got_pathlist_head *paths, struct got_repository *repo,
3508 got_worktree_status_cb status_cb, void *status_arg,
3509 got_cancel_cb cancel_cb, void *cancel_arg)
3511 const struct got_error *err = NULL;
3512 char *fileindex_path = NULL;
3513 struct got_fileindex *fileindex = NULL;
3514 struct got_pathlist_entry *pe;
3516 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3520 TAILQ_FOREACH(pe, paths, entry) {
3521 err = worktree_status(worktree, pe->path, fileindex, repo,
3522 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3526 free(fileindex_path);
3527 got_fileindex_free(fileindex);
3531 const struct got_error *
3532 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3535 const struct got_error *err = NULL;
3536 char *resolved = NULL, *cwd = NULL, *path = NULL;
3542 cwd = getcwd(NULL, 0);
3544 return got_error_from_errno("getcwd");
3546 if (lstat(arg, &sb) == -1) {
3547 if (errno != ENOENT) {
3548 err = got_error_from_errno2("lstat", arg);
3552 if (S_ISLNK(sb.st_mode)) {
3554 * We cannot use realpath(3) with symlinks since we want to
3555 * operate on the symlink itself.
3556 * But we can make the path absolute, assuming it is relative
3557 * to the current working directory, and then canonicalize it.
3559 char *abspath = NULL;
3560 char canonpath[PATH_MAX];
3561 if (!got_path_is_absolute(arg)) {
3562 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3563 err = got_error_from_errno("asprintf");
3568 err = got_canonpath(abspath ? abspath : arg, canonpath,
3572 resolved = strdup(canonpath);
3573 if (resolved == NULL) {
3574 err = got_error_from_errno("strdup");
3578 resolved = realpath(arg, NULL);
3579 if (resolved == NULL) {
3580 if (errno != ENOENT) {
3581 err = got_error_from_errno2("realpath", arg);
3584 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
3585 err = got_error_from_errno("asprintf");
3591 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3592 strlen(got_worktree_get_root_path(worktree)))) {
3593 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3597 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3598 err = got_path_skip_common_ancestor(&path,
3599 got_worktree_get_root_path(worktree), resolved);
3605 err = got_error_from_errno("strdup");
3610 /* XXX status walk can't deal with trailing slash! */
3612 while (len > 0 && path[len - 1] == '/') {
3613 path[len - 1] = '\0';
3626 struct schedule_addition_args {
3627 struct got_worktree *worktree;
3628 struct got_fileindex *fileindex;
3629 got_worktree_checkout_cb progress_cb;
3631 struct got_repository *repo;
3634 static const struct got_error *
3635 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3636 const char *relpath, struct got_object_id *blob_id,
3637 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3638 int dirfd, const char *de_name)
3640 struct schedule_addition_args *a = arg;
3641 const struct got_error *err = NULL;
3642 struct got_fileindex_entry *ie;
3646 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3648 return got_error_from_errno("asprintf");
3650 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3652 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3656 /* Re-adding an existing entry is a no-op. */
3657 if (status == GOT_STATUS_ADD)
3659 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3664 if (status != GOT_STATUS_UNVERSIONED) {
3665 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3669 err = got_fileindex_entry_alloc(&ie, relpath);
3672 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3674 got_fileindex_entry_free(ie);
3677 err = got_fileindex_entry_add(a->fileindex, ie);
3679 got_fileindex_entry_free(ie);
3686 if (status == GOT_STATUS_ADD)
3688 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3691 const struct got_error *
3692 got_worktree_schedule_add(struct got_worktree *worktree,
3693 struct got_pathlist_head *paths,
3694 got_worktree_checkout_cb progress_cb, void *progress_arg,
3695 struct got_repository *repo, int no_ignores)
3697 struct got_fileindex *fileindex = NULL;
3698 char *fileindex_path = NULL;
3699 const struct got_error *err = NULL, *sync_err, *unlockerr;
3700 struct got_pathlist_entry *pe;
3701 struct schedule_addition_args saa;
3703 err = lock_worktree(worktree, LOCK_EX);
3707 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3711 saa.worktree = worktree;
3712 saa.fileindex = fileindex;
3713 saa.progress_cb = progress_cb;
3714 saa.progress_arg = progress_arg;
3717 TAILQ_FOREACH(pe, paths, entry) {
3718 err = worktree_status(worktree, pe->path, fileindex, repo,
3719 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3723 sync_err = sync_fileindex(fileindex, fileindex_path);
3724 if (sync_err && err == NULL)
3727 free(fileindex_path);
3729 got_fileindex_free(fileindex);
3730 unlockerr = lock_worktree(worktree, LOCK_SH);
3731 if (unlockerr && err == NULL)
3736 struct schedule_deletion_args {
3737 struct got_worktree *worktree;
3738 struct got_fileindex *fileindex;
3739 got_worktree_delete_cb progress_cb;
3741 struct got_repository *repo;
3742 int delete_local_mods;
3746 static const struct got_error *
3747 schedule_for_deletion(void *arg, unsigned char status,
3748 unsigned char staged_status, const char *relpath,
3749 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3750 struct got_object_id *commit_id, int dirfd, const char *de_name)
3752 struct schedule_deletion_args *a = arg;
3753 const struct got_error *err = NULL;
3754 struct got_fileindex_entry *ie = NULL;
3756 char *ondisk_path, *parent = NULL;
3758 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3760 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3762 staged_status = get_staged_status(ie);
3763 if (staged_status != GOT_STATUS_NO_CHANGE) {
3764 if (staged_status == GOT_STATUS_DELETE)
3766 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3769 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3771 return got_error_from_errno("asprintf");
3773 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3778 if (status != GOT_STATUS_NO_CHANGE) {
3779 if (status == GOT_STATUS_DELETE)
3781 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3782 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3785 if (status != GOT_STATUS_MODIFY &&
3786 status != GOT_STATUS_MISSING) {
3787 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3792 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3794 if (unlinkat(dirfd, de_name, 0) != 0) {
3795 err = got_error_from_errno2("unlinkat",
3799 } else if (unlink(ondisk_path) != 0) {
3800 err = got_error_from_errno2("unlink", ondisk_path);
3804 parent = dirname(ondisk_path);
3806 if (parent == NULL) {
3807 err = got_error_from_errno2("dirname", ondisk_path);
3810 while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3811 if (rmdir(parent) == -1) {
3812 if (errno != ENOTEMPTY)
3813 err = got_error_from_errno2("rmdir",
3817 parent = dirname(parent);
3818 if (parent == NULL) {
3819 err = got_error_from_errno2("dirname", parent);
3825 got_fileindex_entry_mark_deleted_from_disk(ie);
3830 if (status == GOT_STATUS_DELETE)
3832 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3833 staged_status, relpath);
3836 const struct got_error *
3837 got_worktree_schedule_delete(struct got_worktree *worktree,
3838 struct got_pathlist_head *paths, int delete_local_mods,
3839 got_worktree_delete_cb progress_cb, void *progress_arg,
3840 struct got_repository *repo, int keep_on_disk)
3842 struct got_fileindex *fileindex = NULL;
3843 char *fileindex_path = NULL;
3844 const struct got_error *err = NULL, *sync_err, *unlockerr;
3845 struct got_pathlist_entry *pe;
3846 struct schedule_deletion_args sda;
3848 err = lock_worktree(worktree, LOCK_EX);
3852 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3856 sda.worktree = worktree;
3857 sda.fileindex = fileindex;
3858 sda.progress_cb = progress_cb;
3859 sda.progress_arg = progress_arg;
3861 sda.delete_local_mods = delete_local_mods;
3862 sda.keep_on_disk = keep_on_disk;
3864 TAILQ_FOREACH(pe, paths, entry) {
3865 err = worktree_status(worktree, pe->path, fileindex, repo,
3866 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3870 sync_err = sync_fileindex(fileindex, fileindex_path);
3871 if (sync_err && err == NULL)
3874 free(fileindex_path);
3876 got_fileindex_free(fileindex);
3877 unlockerr = lock_worktree(worktree, LOCK_SH);
3878 if (unlockerr && err == NULL)
3883 static const struct got_error *
3884 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3886 const struct got_error *err = NULL;
3888 size_t linesize = 0, n;
3891 linelen = getline(&line, &linesize, infile);
3892 if (linelen == -1) {
3893 if (ferror(infile)) {
3894 err = got_error_from_errno("getline");
3900 n = fwrite(line, 1, linelen, outfile);
3902 err = got_ferror(outfile, GOT_ERR_IO);
3907 n = fwrite(line, 1, linelen, rejectfile);
3909 err = got_ferror(outfile, GOT_ERR_IO);
3916 static const struct got_error *
3917 skip_one_line(FILE *f)
3920 size_t linesize = 0;
3923 linelen = getline(&line, &linesize, f);
3924 if (linelen == -1) {
3926 return got_error_from_errno("getline");
3933 static const struct got_error *
3934 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3935 int start_old, int end_old, int start_new, int end_new,
3936 FILE *outfile, FILE *rejectfile)
3938 const struct got_error *err;
3940 /* Copy old file's lines leading up to patch. */
3941 while (!feof(f1) && *line_cur1 < start_old) {
3942 err = copy_one_line(f1, outfile, NULL);
3947 /* Skip new file's lines leading up to patch. */
3948 while (!feof(f2) && *line_cur2 < start_new) {
3950 err = copy_one_line(f2, NULL, rejectfile);
3952 err = skip_one_line(f2);
3957 /* Copy patched lines. */
3958 while (!feof(f2) && *line_cur2 <= end_new) {
3959 err = copy_one_line(f2, outfile, NULL);
3964 /* Skip over old file's replaced lines. */
3965 while (!feof(f1) && *line_cur1 <= end_old) {
3967 err = copy_one_line(f1, NULL, rejectfile);
3969 err = skip_one_line(f1);
3978 static const struct got_error *
3979 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3980 FILE *outfile, FILE *rejectfile)
3982 const struct got_error *err;
3985 /* Copy old file's lines until EOF. */
3987 err = copy_one_line(f1, outfile, NULL);
3994 /* Copy new file's lines until EOF. */
3996 err = copy_one_line(f2, NULL, rejectfile);
4006 static const struct got_error *
4007 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
4008 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
4009 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
4010 int *line_cur2, FILE *outfile, FILE *rejectfile,
4011 got_worktree_patch_cb patch_cb, void *patch_arg)
4013 const struct got_error *err = NULL;
4014 int start_old = change->cv.a;
4015 int end_old = change->cv.b;
4016 int start_new = change->cv.c;
4017 int end_new = change->cv.d;
4021 *choice = GOT_PATCH_CHOICE_NONE;
4023 hunkfile = got_opentemp();
4024 if (hunkfile == NULL)
4025 return got_error_from_errno("got_opentemp");
4030 /* XXX TODO needs error checking */
4031 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
4033 if (fseek(f1, pos1, SEEK_SET) == -1) {
4034 err = got_ferror(f1, GOT_ERR_IO);
4037 if (fseek(f2, pos2, SEEK_SET) == -1) {
4038 err = got_ferror(f1, GOT_ERR_IO);
4041 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4042 err = got_ferror(hunkfile, GOT_ERR_IO);
4046 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4047 hunkfile, n, nchanges);
4052 case GOT_PATCH_CHOICE_YES:
4053 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4054 end_old, start_new, end_new, outfile, rejectfile);
4056 case GOT_PATCH_CHOICE_NO:
4057 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4058 end_old, start_new, end_new, rejectfile, outfile);
4060 case GOT_PATCH_CHOICE_QUIT:
4063 err = got_error(GOT_ERR_PATCH_CHOICE);
4067 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4068 err = got_error_from_errno("fclose");
4072 struct revert_file_args {
4073 struct got_worktree *worktree;
4074 struct got_fileindex *fileindex;
4075 got_worktree_checkout_cb progress_cb;
4077 got_worktree_patch_cb patch_cb;
4079 struct got_repository *repo;
4082 static const struct got_error *
4083 create_patched_content(char **path_outfile, int reverse_patch,
4084 struct got_object_id *blob_id, const char *path2,
4085 int dirfd2, const char *de_name2,
4086 const char *relpath, struct got_repository *repo,
4087 got_worktree_patch_cb patch_cb, void *patch_arg)
4089 const struct got_error *err;
4090 struct got_blob_object *blob = NULL;
4091 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4093 char link_target[PATH_MAX];
4094 ssize_t link_len = 0;
4095 char *path1 = NULL, *id_str = NULL;
4096 struct stat sb1, sb2;
4097 struct got_diff_changes *changes = NULL;
4098 struct got_diff_state *ds = NULL;
4099 struct got_diff_args *args = NULL;
4100 struct got_diff_change *change;
4101 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
4104 *path_outfile = NULL;
4106 err = got_object_id_str(&id_str, blob_id);
4111 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4113 if (errno != ELOOP) {
4114 err = got_error_from_errno2("openat", path2);
4117 link_len = readlinkat(dirfd2, de_name2,
4118 link_target, sizeof(link_target));
4120 return got_error_from_errno2("readlinkat", path2);
4121 sb2.st_mode = S_IFLNK;
4122 sb2.st_size = link_len;
4125 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4127 if (errno != ELOOP) {
4128 err = got_error_from_errno2("open", path2);
4131 link_len = readlink(path2, link_target,
4132 sizeof(link_target));
4134 return got_error_from_errno2("readlink", path2);
4135 sb2.st_mode = S_IFLNK;
4136 sb2.st_size = link_len;
4140 if (fstat(fd2, &sb2) == -1) {
4141 err = got_error_from_errno2("fstat", path2);
4145 f2 = fdopen(fd2, "r");
4147 err = got_error_from_errno2("fdopen", path2);
4153 f2 = got_opentemp();
4155 err = got_error_from_errno2("got_opentemp", path2);
4158 n = fwrite(link_target, 1, link_len, f2);
4159 if (n != link_len) {
4160 err = got_ferror(f2, GOT_ERR_IO);
4163 if (fflush(f2) == EOF) {
4164 err = got_error_from_errno("fflush");
4170 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4174 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4178 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4182 if (stat(path1, &sb1) == -1) {
4183 err = got_error_from_errno2("stat", path1);
4187 err = got_diff_files(&changes, &ds, &args, &diff_flags,
4188 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
4192 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4196 if (fseek(f1, 0L, SEEK_SET) == -1)
4197 return got_ferror(f1, GOT_ERR_IO);
4198 if (fseek(f2, 0L, SEEK_SET) == -1)
4199 return got_ferror(f2, GOT_ERR_IO);
4200 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
4202 err = apply_or_reject_change(&choice, change, ++n,
4203 changes->nchanges, ds, args, diff_flags, relpath,
4204 f1, f2, &line_cur1, &line_cur2,
4205 reverse_patch ? NULL : outfile,
4206 reverse_patch ? outfile : NULL,
4207 patch_cb, patch_arg);
4210 if (choice == GOT_PATCH_CHOICE_YES)
4212 else if (choice == GOT_PATCH_CHOICE_QUIT)
4216 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4217 reverse_patch ? NULL : outfile,
4218 reverse_patch ? outfile : NULL);
4222 if (!S_ISLNK(sb2.st_mode)) {
4223 if (chmod(*path_outfile, sb2.st_mode) == -1) {
4224 err = got_error_from_errno2("chmod", path2);
4232 got_object_blob_close(blob);
4233 if (f1 && fclose(f1) == EOF && err == NULL)
4234 err = got_error_from_errno2("fclose", path1);
4235 if (f2 && fclose(f2) == EOF && err == NULL)
4236 err = got_error_from_errno2("fclose", path2);
4237 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4238 err = got_error_from_errno2("close", path2);
4239 if (outfile && fclose(outfile) == EOF && err == NULL)
4240 err = got_error_from_errno2("fclose", *path_outfile);
4241 if (path1 && unlink(path1) == -1 && err == NULL)
4242 err = got_error_from_errno2("unlink", path1);
4243 if (err || !have_content) {
4244 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4245 err = got_error_from_errno2("unlink", *path_outfile);
4246 free(*path_outfile);
4247 *path_outfile = NULL;
4251 got_diff_state_free(ds);
4255 got_diff_free_changes(changes);
4260 static const struct got_error *
4261 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4262 const char *relpath, struct got_object_id *blob_id,
4263 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4264 int dirfd, const char *de_name)
4266 struct revert_file_args *a = arg;
4267 const struct got_error *err = NULL;
4268 char *parent_path = NULL;
4269 struct got_fileindex_entry *ie;
4270 struct got_tree_object *tree = NULL;
4271 struct got_object_id *tree_id = NULL;
4272 const struct got_tree_entry *te = NULL;
4273 char *tree_path = NULL, *te_name;
4274 char *ondisk_path = NULL, *path_content = NULL;
4275 struct got_blob_object *blob = NULL;
4277 /* Reverting a staged deletion is a no-op. */
4278 if (status == GOT_STATUS_DELETE &&
4279 staged_status != GOT_STATUS_NO_CHANGE)
4282 if (status == GOT_STATUS_UNVERSIONED)
4283 return (*a->progress_cb)(a->progress_arg,
4284 GOT_STATUS_UNVERSIONED, relpath);
4286 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4288 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4290 /* Construct in-repository path of tree which contains this blob. */
4291 err = got_path_dirname(&parent_path, ie->path);
4293 if (err->code != GOT_ERR_BAD_PATH)
4295 parent_path = strdup("/");
4296 if (parent_path == NULL) {
4297 err = got_error_from_errno("strdup");
4301 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4302 tree_path = strdup(parent_path);
4303 if (tree_path == NULL) {
4304 err = got_error_from_errno("strdup");
4308 if (got_path_is_root_dir(parent_path)) {
4309 tree_path = strdup(a->worktree->path_prefix);
4310 if (tree_path == NULL) {
4311 err = got_error_from_errno("strdup");
4315 if (asprintf(&tree_path, "%s/%s",
4316 a->worktree->path_prefix, parent_path) == -1) {
4317 err = got_error_from_errno("asprintf");
4323 err = got_object_id_by_path(&tree_id, a->repo,
4324 a->worktree->base_commit_id, tree_path);
4326 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4327 (status == GOT_STATUS_ADD ||
4328 staged_status == GOT_STATUS_ADD)))
4331 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4335 te_name = basename(ie->path);
4336 if (te_name == NULL) {
4337 err = got_error_from_errno2("basename", ie->path);
4341 te = got_object_tree_find_entry(tree, te_name);
4342 if (te == NULL && status != GOT_STATUS_ADD &&
4343 staged_status != GOT_STATUS_ADD) {
4344 err = got_error(GOT_ERR_NO_TREE_ENTRY);
4350 case GOT_STATUS_ADD:
4352 int choice = GOT_PATCH_CHOICE_NONE;
4353 err = (*a->patch_cb)(&choice, a->patch_arg,
4354 status, ie->path, NULL, 1, 1);
4357 if (choice != GOT_PATCH_CHOICE_YES)
4360 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4364 got_fileindex_entry_remove(a->fileindex, ie);
4366 case GOT_STATUS_DELETE:
4368 int choice = GOT_PATCH_CHOICE_NONE;
4369 err = (*a->patch_cb)(&choice, a->patch_arg,
4370 status, ie->path, NULL, 1, 1);
4373 if (choice != GOT_PATCH_CHOICE_YES)
4377 case GOT_STATUS_MODIFY:
4378 case GOT_STATUS_MODE_CHANGE:
4379 case GOT_STATUS_CONFLICT:
4380 case GOT_STATUS_MISSING: {
4381 struct got_object_id id;
4382 if (staged_status == GOT_STATUS_ADD ||
4383 staged_status == GOT_STATUS_MODIFY) {
4384 memcpy(id.sha1, ie->staged_blob_sha1,
4385 SHA1_DIGEST_LENGTH);
4387 memcpy(id.sha1, ie->blob_sha1,
4388 SHA1_DIGEST_LENGTH);
4389 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4393 if (asprintf(&ondisk_path, "%s/%s",
4394 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4395 err = got_error_from_errno("asprintf");
4399 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4400 status == GOT_STATUS_CONFLICT)) {
4401 int is_bad_symlink = 0;
4402 err = create_patched_content(&path_content, 1, &id,
4403 ondisk_path, dirfd, de_name, ie->path, a->repo,
4404 a->patch_cb, a->patch_arg);
4405 if (err || path_content == NULL)
4407 if (te && S_ISLNK(te->mode)) {
4408 if (unlink(path_content) == -1) {
4409 err = got_error_from_errno2("unlink",
4413 err = install_symlink(&is_bad_symlink,
4414 a->worktree, ondisk_path, ie->path,
4415 blob, 0, 1, 0, a->repo,
4416 a->progress_cb, a->progress_arg);
4418 if (rename(path_content, ondisk_path) == -1) {
4419 err = got_error_from_errno3("rename",
4420 path_content, ondisk_path);
4425 int is_bad_symlink = 0;
4426 if (te && S_ISLNK(te->mode)) {
4427 err = install_symlink(&is_bad_symlink,
4428 a->worktree, ondisk_path, ie->path,
4429 blob, 0, 1, 0, a->repo,
4430 a->progress_cb, a->progress_arg);
4432 err = install_blob(a->worktree, ondisk_path,
4434 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4435 got_fileindex_perms_to_st(ie), blob,
4436 0, 1, 0, 0, a->repo,
4437 a->progress_cb, a->progress_arg);
4441 if (status == GOT_STATUS_DELETE ||
4442 status == GOT_STATUS_MODE_CHANGE) {
4443 err = got_fileindex_entry_update(ie,
4444 ondisk_path, blob->id.sha1,
4445 a->worktree->base_commit_id->sha1, 1);
4449 if (is_bad_symlink) {
4450 got_fileindex_entry_filetype_set(ie,
4451 GOT_FILEIDX_MODE_BAD_SYMLINK);
4465 got_object_blob_close(blob);
4467 got_object_tree_close(tree);
4472 const struct got_error *
4473 got_worktree_revert(struct got_worktree *worktree,
4474 struct got_pathlist_head *paths,
4475 got_worktree_checkout_cb progress_cb, void *progress_arg,
4476 got_worktree_patch_cb patch_cb, void *patch_arg,
4477 struct got_repository *repo)
4479 struct got_fileindex *fileindex = NULL;
4480 char *fileindex_path = NULL;
4481 const struct got_error *err = NULL, *unlockerr = NULL;
4482 const struct got_error *sync_err = NULL;
4483 struct got_pathlist_entry *pe;
4484 struct revert_file_args rfa;
4486 err = lock_worktree(worktree, LOCK_EX);
4490 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4494 rfa.worktree = worktree;
4495 rfa.fileindex = fileindex;
4496 rfa.progress_cb = progress_cb;
4497 rfa.progress_arg = progress_arg;
4498 rfa.patch_cb = patch_cb;
4499 rfa.patch_arg = patch_arg;
4501 TAILQ_FOREACH(pe, paths, entry) {
4502 err = worktree_status(worktree, pe->path, fileindex, repo,
4503 revert_file, &rfa, NULL, NULL, 0, 0);
4507 sync_err = sync_fileindex(fileindex, fileindex_path);
4508 if (sync_err && err == NULL)
4511 free(fileindex_path);
4513 got_fileindex_free(fileindex);
4514 unlockerr = lock_worktree(worktree, LOCK_SH);
4515 if (unlockerr && err == NULL)
4521 free_commitable(struct got_commitable *ct)
4524 free(ct->in_repo_path);
4525 free(ct->ondisk_path);
4527 free(ct->base_blob_id);
4528 free(ct->staged_blob_id);
4529 free(ct->base_commit_id);
4533 struct collect_commitables_arg {
4534 struct got_pathlist_head *commitable_paths;
4535 struct got_repository *repo;
4536 struct got_worktree *worktree;
4537 struct got_fileindex *fileindex;
4538 int have_staged_files;
4539 int allow_bad_symlinks;
4542 static const struct got_error *
4543 collect_commitables(void *arg, unsigned char status,
4544 unsigned char staged_status, const char *relpath,
4545 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4546 struct got_object_id *commit_id, int dirfd, const char *de_name)
4548 struct collect_commitables_arg *a = arg;
4549 const struct got_error *err = NULL;
4550 struct got_commitable *ct = NULL;
4551 struct got_pathlist_entry *new = NULL;
4552 char *parent_path = NULL, *path = NULL;
4555 if (a->have_staged_files) {
4556 if (staged_status != GOT_STATUS_MODIFY &&
4557 staged_status != GOT_STATUS_ADD &&
4558 staged_status != GOT_STATUS_DELETE)
4561 if (status == GOT_STATUS_CONFLICT)
4562 return got_error(GOT_ERR_COMMIT_CONFLICT);
4564 if (status != GOT_STATUS_MODIFY &&
4565 status != GOT_STATUS_MODE_CHANGE &&
4566 status != GOT_STATUS_ADD &&
4567 status != GOT_STATUS_DELETE)
4571 if (asprintf(&path, "/%s", relpath) == -1) {
4572 err = got_error_from_errno("asprintf");
4575 if (strcmp(path, "/") == 0) {
4576 parent_path = strdup("");
4577 if (parent_path == NULL)
4578 return got_error_from_errno("strdup");
4580 err = got_path_dirname(&parent_path, path);
4585 ct = calloc(1, sizeof(*ct));
4587 err = got_error_from_errno("calloc");
4591 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4593 err = got_error_from_errno("asprintf");
4597 if (staged_status == GOT_STATUS_ADD ||
4598 staged_status == GOT_STATUS_MODIFY) {
4599 struct got_fileindex_entry *ie;
4600 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4601 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4602 case GOT_FILEIDX_MODE_REGULAR_FILE:
4603 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4606 case GOT_FILEIDX_MODE_SYMLINK:
4610 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4613 ct->mode |= got_fileindex_entry_perms_get(ie);
4614 } else if (status != GOT_STATUS_DELETE &&
4615 staged_status != GOT_STATUS_DELETE) {
4617 if (fstatat(dirfd, de_name, &sb,
4618 AT_SYMLINK_NOFOLLOW) == -1) {
4619 err = got_error_from_errno2("fstatat",
4623 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4624 err = got_error_from_errno2("lstat", ct->ondisk_path);
4627 ct->mode = sb.st_mode;
4630 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4631 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4633 err = got_error_from_errno("asprintf");
4637 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4638 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4640 char target_path[PATH_MAX];
4642 target_len = readlink(ct->ondisk_path, target_path,
4643 sizeof(target_path));
4644 if (target_len == -1) {
4645 err = got_error_from_errno2("readlink",
4649 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4650 target_len, ct->ondisk_path, a->worktree->root_path);
4653 if (is_bad_symlink) {
4654 err = got_error_path(ct->ondisk_path,
4655 GOT_ERR_BAD_SYMLINK);
4661 ct->status = status;
4662 ct->staged_status = staged_status;
4663 ct->blob_id = NULL; /* will be filled in when blob gets created */
4664 if (ct->status != GOT_STATUS_ADD &&
4665 ct->staged_status != GOT_STATUS_ADD) {
4666 ct->base_blob_id = got_object_id_dup(blob_id);
4667 if (ct->base_blob_id == NULL) {
4668 err = got_error_from_errno("got_object_id_dup");
4671 ct->base_commit_id = got_object_id_dup(commit_id);
4672 if (ct->base_commit_id == NULL) {
4673 err = got_error_from_errno("got_object_id_dup");
4677 if (ct->staged_status == GOT_STATUS_ADD ||
4678 ct->staged_status == GOT_STATUS_MODIFY) {
4679 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4680 if (ct->staged_blob_id == NULL) {
4681 err = got_error_from_errno("got_object_id_dup");
4685 ct->path = strdup(path);
4686 if (ct->path == NULL) {
4687 err = got_error_from_errno("strdup");
4690 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4692 if (ct && (err || new == NULL))
4693 free_commitable(ct);
4699 static const struct got_error *write_tree(struct got_object_id **, int *,
4700 struct got_tree_object *, const char *, struct got_pathlist_head *,
4701 got_worktree_status_cb status_cb, void *status_arg,
4702 struct got_repository *);
4704 static const struct got_error *
4705 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4706 struct got_tree_entry *te, const char *parent_path,
4707 struct got_pathlist_head *commitable_paths,
4708 got_worktree_status_cb status_cb, void *status_arg,
4709 struct got_repository *repo)
4711 const struct got_error *err = NULL;
4712 struct got_tree_object *subtree;
4715 if (asprintf(&subpath, "%s%s%s", parent_path,
4716 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4717 return got_error_from_errno("asprintf");
4719 err = got_object_open_as_tree(&subtree, repo, &te->id);
4723 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4724 commitable_paths, status_cb, status_arg, repo);
4725 got_object_tree_close(subtree);
4730 static const struct got_error *
4731 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4733 const struct got_error *err = NULL;
4734 char *ct_parent_path = NULL;
4738 if (strchr(ct->in_repo_path, '/') == NULL) {
4739 *match = got_path_is_root_dir(path);
4743 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4746 *match = (strcmp(path, ct_parent_path) == 0);
4747 free(ct_parent_path);
4752 get_ct_file_mode(struct got_commitable *ct)
4754 if (S_ISLNK(ct->mode))
4757 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4760 static const struct got_error *
4761 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4762 struct got_tree_entry *te, struct got_commitable *ct)
4764 const struct got_error *err = NULL;
4768 err = got_object_tree_entry_dup(new_te, te);
4772 (*new_te)->mode = get_ct_file_mode(ct);
4774 if (ct->staged_status == GOT_STATUS_MODIFY)
4775 memcpy(&(*new_te)->id, ct->staged_blob_id,
4776 sizeof((*new_te)->id));
4778 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4780 if (err && *new_te) {
4787 static const struct got_error *
4788 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4789 struct got_commitable *ct)
4791 const struct got_error *err = NULL;
4796 *new_te = calloc(1, sizeof(**new_te));
4797 if (*new_te == NULL)
4798 return got_error_from_errno("calloc");
4800 ct_name = basename(ct->path);
4801 if (ct_name == NULL) {
4802 err = got_error_from_errno2("basename", ct->path);
4805 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4806 sizeof((*new_te)->name)) {
4807 err = got_error(GOT_ERR_NO_SPACE);
4811 (*new_te)->mode = get_ct_file_mode(ct);
4813 if (ct->staged_status == GOT_STATUS_ADD)
4814 memcpy(&(*new_te)->id, ct->staged_blob_id,
4815 sizeof((*new_te)->id));
4817 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4819 if (err && *new_te) {
4826 static const struct got_error *
4827 insert_tree_entry(struct got_tree_entry *new_te,
4828 struct got_pathlist_head *paths)
4830 const struct got_error *err = NULL;
4831 struct got_pathlist_entry *new_pe;
4833 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4837 return got_error(GOT_ERR_TREE_DUP_ENTRY);
4841 static const struct got_error *
4842 report_ct_status(struct got_commitable *ct,
4843 got_worktree_status_cb status_cb, void *status_arg)
4845 const char *ct_path = ct->path;
4846 unsigned char status;
4848 while (ct_path[0] == '/')
4851 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4852 status = ct->staged_status;
4854 status = ct->status;
4856 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4857 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4860 static const struct got_error *
4861 match_modified_subtree(int *modified, struct got_tree_entry *te,
4862 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4864 const struct got_error *err = NULL;
4865 struct got_pathlist_entry *pe;
4870 if (asprintf(&te_path, "%s%s%s", base_tree_path,
4871 got_path_is_root_dir(base_tree_path) ? "" : "/",
4873 return got_error_from_errno("asprintf");
4875 TAILQ_FOREACH(pe, commitable_paths, entry) {
4876 struct got_commitable *ct = pe->data;
4877 *modified = got_path_is_child(ct->in_repo_path, te_path,
4887 static const struct got_error *
4888 match_deleted_or_modified_ct(struct got_commitable **ctp,
4889 struct got_tree_entry *te, const char *base_tree_path,
4890 struct got_pathlist_head *commitable_paths)
4892 const struct got_error *err = NULL;
4893 struct got_pathlist_entry *pe;
4897 TAILQ_FOREACH(pe, commitable_paths, entry) {
4898 struct got_commitable *ct = pe->data;
4899 char *ct_name = NULL;
4902 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4903 if (ct->status != GOT_STATUS_MODIFY &&
4904 ct->status != GOT_STATUS_MODE_CHANGE &&
4905 ct->status != GOT_STATUS_DELETE)
4908 if (ct->staged_status != GOT_STATUS_MODIFY &&
4909 ct->staged_status != GOT_STATUS_DELETE)
4913 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4916 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4922 ct_name = basename(pe->path);
4923 if (ct_name == NULL)
4924 return got_error_from_errno2("basename", pe->path);
4926 if (strcmp(te->name, ct_name) != 0)
4936 static const struct got_error *
4937 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4938 const char *child_path, const char *path_base_tree,
4939 struct got_pathlist_head *commitable_paths,
4940 got_worktree_status_cb status_cb, void *status_arg,
4941 struct got_repository *repo)
4943 const struct got_error *err = NULL;
4944 struct got_tree_entry *new_te;
4946 struct got_object_id *id = NULL;
4951 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4952 got_path_is_root_dir(path_base_tree) ? "" : "/",
4954 return got_error_from_errno("asprintf");
4956 new_te = calloc(1, sizeof(*new_te));
4958 return got_error_from_errno("calloc");
4959 new_te->mode = S_IFDIR;
4961 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4962 sizeof(new_te->name)) {
4963 err = got_error(GOT_ERR_NO_SPACE);
4966 err = write_tree(&id, &nentries, NULL, subtree_path,
4967 commitable_paths, status_cb, status_arg, repo);
4972 memcpy(&new_te->id, id, sizeof(new_te->id));
4981 static const struct got_error *
4982 write_tree(struct got_object_id **new_tree_id, int *nentries,
4983 struct got_tree_object *base_tree, const char *path_base_tree,
4984 struct got_pathlist_head *commitable_paths,
4985 got_worktree_status_cb status_cb, void *status_arg,
4986 struct got_repository *repo)
4988 const struct got_error *err = NULL;
4989 struct got_pathlist_head paths;
4990 struct got_tree_entry *te, *new_te = NULL;
4991 struct got_pathlist_entry *pe;
4996 /* Insert, and recurse into, newly added entries first. */
4997 TAILQ_FOREACH(pe, commitable_paths, entry) {
4998 struct got_commitable *ct = pe->data;
4999 char *child_path = NULL, *slash;
5001 if ((ct->status != GOT_STATUS_ADD &&
5002 ct->staged_status != GOT_STATUS_ADD) ||
5003 (ct->flags & GOT_COMMITABLE_ADDED))
5006 if (!got_path_is_child(pe->path, path_base_tree,
5007 strlen(path_base_tree)))
5010 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5015 slash = strchr(child_path, '/');
5016 if (slash == NULL) {
5017 err = alloc_added_blob_tree_entry(&new_te, ct);
5020 err = report_ct_status(ct, status_cb, status_arg);
5023 ct->flags |= GOT_COMMITABLE_ADDED;
5024 err = insert_tree_entry(new_te, &paths);
5029 *slash = '\0'; /* trim trailing path components */
5030 if (base_tree == NULL ||
5031 got_object_tree_find_entry(base_tree, child_path)
5033 err = make_subtree_for_added_blob(&new_te,
5034 child_path, path_base_tree,
5035 commitable_paths, status_cb, status_arg,
5039 err = insert_tree_entry(new_te, &paths);
5048 int i, nbase_entries;
5049 /* Handle modified and deleted entries. */
5050 nbase_entries = got_object_tree_get_nentries(base_tree);
5051 for (i = 0; i < nbase_entries; i++) {
5052 struct got_commitable *ct = NULL;
5054 te = got_object_tree_get_entry(base_tree, i);
5055 if (got_object_tree_entry_is_submodule(te)) {
5056 /* Entry is a submodule; just copy it. */
5057 err = got_object_tree_entry_dup(&new_te, te);
5060 err = insert_tree_entry(new_te, &paths);
5067 if (S_ISDIR(te->mode)) {
5069 err = got_object_tree_entry_dup(&new_te, te);
5072 err = match_modified_subtree(&modified, te,
5073 path_base_tree, commitable_paths);
5076 /* Avoid recursion into unmodified subtrees. */
5078 struct got_object_id *new_id;
5080 err = write_subtree(&new_id,
5082 path_base_tree, commitable_paths,
5083 status_cb, status_arg, repo);
5086 if (nsubentries == 0) {
5087 /* All entries were deleted. */
5091 memcpy(&new_te->id, new_id,
5092 sizeof(new_te->id));
5095 err = insert_tree_entry(new_te, &paths);
5102 err = match_deleted_or_modified_ct(&ct, te,
5103 path_base_tree, commitable_paths);
5107 /* NB: Deleted entries get dropped here. */
5108 if (ct->status == GOT_STATUS_MODIFY ||
5109 ct->status == GOT_STATUS_MODE_CHANGE ||
5110 ct->staged_status == GOT_STATUS_MODIFY) {
5111 err = alloc_modified_blob_tree_entry(
5115 err = insert_tree_entry(new_te, &paths);
5120 err = report_ct_status(ct, status_cb,
5125 /* Entry is unchanged; just copy it. */
5126 err = got_object_tree_entry_dup(&new_te, te);
5129 err = insert_tree_entry(new_te, &paths);
5137 /* Write new list of entries; deleted entries have been dropped. */
5138 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5140 got_pathlist_free(&paths);
5144 static const struct got_error *
5145 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
5146 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
5147 int have_staged_files)
5149 const struct got_error *err = NULL;
5150 struct got_pathlist_entry *pe;
5152 TAILQ_FOREACH(pe, commitable_paths, entry) {
5153 struct got_fileindex_entry *ie;
5154 struct got_commitable *ct = pe->data;
5156 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5158 if (ct->status == GOT_STATUS_DELETE ||
5159 ct->staged_status == GOT_STATUS_DELETE) {
5160 got_fileindex_entry_remove(fileindex, ie);
5161 } else if (ct->staged_status == GOT_STATUS_ADD ||
5162 ct->staged_status == GOT_STATUS_MODIFY) {
5163 got_fileindex_entry_stage_set(ie,
5164 GOT_FILEIDX_STAGE_NONE);
5165 err = got_fileindex_entry_update(ie,
5166 ct->ondisk_path, ct->staged_blob_id->sha1,
5167 new_base_commit_id->sha1,
5168 !have_staged_files);
5170 err = got_fileindex_entry_update(ie,
5171 ct->ondisk_path, ct->blob_id->sha1,
5172 new_base_commit_id->sha1,
5173 !have_staged_files);
5175 err = got_fileindex_entry_alloc(&ie, pe->path);
5178 err = got_fileindex_entry_update(ie, ct->ondisk_path,
5179 ct->blob_id->sha1, new_base_commit_id->sha1, 1);
5181 got_fileindex_entry_free(ie);
5184 err = got_fileindex_entry_add(fileindex, ie);
5186 got_fileindex_entry_free(ie);
5195 static const struct got_error *
5196 check_out_of_date(const char *in_repo_path, unsigned char status,
5197 unsigned char staged_status, struct got_object_id *base_blob_id,
5198 struct got_object_id *base_commit_id,
5199 struct got_object_id *head_commit_id, struct got_repository *repo,
5202 const struct got_error *err = NULL;
5203 struct got_object_id *id = NULL;
5205 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5206 /* Trivial case: base commit == head commit */
5207 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5210 * Ensure file content which local changes were based
5211 * on matches file content in the branch head.
5213 err = got_object_id_by_path(&id, repo, head_commit_id,
5216 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5217 err = got_error(ood_errcode);
5219 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5220 err = got_error(ood_errcode);
5222 /* Require that added files don't exist in the branch head. */
5223 err = got_object_id_by_path(&id, repo, head_commit_id,
5225 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5227 err = id ? got_error(ood_errcode) : NULL;
5234 const struct got_error *
5235 commit_worktree(struct got_object_id **new_commit_id,
5236 struct got_pathlist_head *commitable_paths,
5237 struct got_object_id *head_commit_id, struct got_worktree *worktree,
5238 const char *author, const char *committer,
5239 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5240 got_worktree_status_cb status_cb, void *status_arg,
5241 struct got_repository *repo)
5243 const struct got_error *err = NULL, *unlockerr = NULL;
5244 struct got_pathlist_entry *pe;
5245 const char *head_ref_name = NULL;
5246 struct got_commit_object *head_commit = NULL;
5247 struct got_reference *head_ref2 = NULL;
5248 struct got_object_id *head_commit_id2 = NULL;
5249 struct got_tree_object *head_tree = NULL;
5250 struct got_object_id *new_tree_id = NULL;
5252 struct got_object_id_queue parent_ids;
5253 struct got_object_qid *pid = NULL;
5254 char *logmsg = NULL;
5256 *new_commit_id = NULL;
5258 SIMPLEQ_INIT(&parent_ids);
5260 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5264 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5268 if (commit_msg_cb != NULL) {
5269 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5274 if (logmsg == NULL || strlen(logmsg) == 0) {
5275 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5279 /* Create blobs from added and modified files and record their IDs. */
5280 TAILQ_FOREACH(pe, commitable_paths, entry) {
5281 struct got_commitable *ct = pe->data;
5284 /* Blobs for staged files already exist. */
5285 if (ct->staged_status == GOT_STATUS_ADD ||
5286 ct->staged_status == GOT_STATUS_MODIFY)
5289 if (ct->status != GOT_STATUS_ADD &&
5290 ct->status != GOT_STATUS_MODIFY &&
5291 ct->status != GOT_STATUS_MODE_CHANGE)
5294 if (asprintf(&ondisk_path, "%s/%s",
5295 worktree->root_path, pe->path) == -1) {
5296 err = got_error_from_errno("asprintf");
5299 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5305 /* Recursively write new tree objects. */
5306 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5307 commitable_paths, status_cb, status_arg, repo);
5311 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5314 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5315 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5316 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5317 got_object_qid_free(pid);
5323 /* Check if a concurrent commit to our branch has occurred. */
5324 head_ref_name = got_worktree_get_head_ref_name(worktree);
5325 if (head_ref_name == NULL) {
5326 err = got_error_from_errno("got_worktree_get_head_ref_name");
5329 /* Lock the reference here to prevent concurrent modification. */
5330 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5333 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5336 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5337 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5340 /* Update branch head in repository. */
5341 err = got_ref_change_ref(head_ref2, *new_commit_id);
5344 err = got_ref_write(head_ref2, repo);
5348 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5352 err = ref_base_commit(worktree, repo);
5357 got_object_tree_close(head_tree);
5359 got_object_commit_close(head_commit);
5360 free(head_commit_id2);
5362 unlockerr = got_ref_unlock(head_ref2);
5363 if (unlockerr && err == NULL)
5365 got_ref_close(head_ref2);
5370 static const struct got_error *
5371 check_path_is_commitable(const char *path,
5372 struct got_pathlist_head *commitable_paths)
5374 struct got_pathlist_entry *cpe = NULL;
5375 size_t path_len = strlen(path);
5377 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5378 struct got_commitable *ct = cpe->data;
5379 const char *ct_path = ct->path;
5381 while (ct_path[0] == '/')
5384 if (strcmp(path, ct_path) == 0 ||
5385 got_path_is_child(ct_path, path, path_len))
5390 return got_error_path(path, GOT_ERR_BAD_PATH);
5395 static const struct got_error *
5396 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5398 int *have_staged_files = arg;
5400 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5401 *have_staged_files = 1;
5402 return got_error(GOT_ERR_CANCELLED);
5408 static const struct got_error *
5409 check_non_staged_files(struct got_fileindex *fileindex,
5410 struct got_pathlist_head *paths)
5412 struct got_pathlist_entry *pe;
5413 struct got_fileindex_entry *ie;
5415 TAILQ_FOREACH(pe, paths, entry) {
5416 if (pe->path[0] == '\0')
5418 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5420 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5421 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5422 return got_error_path(pe->path,
5423 GOT_ERR_FILE_NOT_STAGED);
5429 const struct got_error *
5430 got_worktree_commit(struct got_object_id **new_commit_id,
5431 struct got_worktree *worktree, struct got_pathlist_head *paths,
5432 const char *author, const char *committer, int allow_bad_symlinks,
5433 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5434 got_worktree_status_cb status_cb, void *status_arg,
5435 struct got_repository *repo)
5437 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5438 struct got_fileindex *fileindex = NULL;
5439 char *fileindex_path = NULL;
5440 struct got_pathlist_head commitable_paths;
5441 struct collect_commitables_arg cc_arg;
5442 struct got_pathlist_entry *pe;
5443 struct got_reference *head_ref = NULL;
5444 struct got_object_id *head_commit_id = NULL;
5445 int have_staged_files = 0;
5447 *new_commit_id = NULL;
5449 TAILQ_INIT(&commitable_paths);
5451 err = lock_worktree(worktree, LOCK_EX);
5455 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5459 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5463 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5467 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5468 &have_staged_files);
5469 if (err && err->code != GOT_ERR_CANCELLED)
5471 if (have_staged_files) {
5472 err = check_non_staged_files(fileindex, paths);
5477 cc_arg.commitable_paths = &commitable_paths;
5478 cc_arg.worktree = worktree;
5479 cc_arg.fileindex = fileindex;
5481 cc_arg.have_staged_files = have_staged_files;
5482 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5483 TAILQ_FOREACH(pe, paths, entry) {
5484 err = worktree_status(worktree, pe->path, fileindex, repo,
5485 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5490 if (TAILQ_EMPTY(&commitable_paths)) {
5491 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5495 TAILQ_FOREACH(pe, paths, entry) {
5496 err = check_path_is_commitable(pe->path, &commitable_paths);
5501 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5502 struct got_commitable *ct = pe->data;
5503 const char *ct_path = ct->in_repo_path;
5505 while (ct_path[0] == '/')
5507 err = check_out_of_date(ct_path, ct->status,
5508 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5509 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5515 err = commit_worktree(new_commit_id, &commitable_paths,
5516 head_commit_id, worktree, author, committer,
5517 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5521 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5522 fileindex, have_staged_files);
5523 sync_err = sync_fileindex(fileindex, fileindex_path);
5524 if (sync_err && err == NULL)
5528 got_fileindex_free(fileindex);
5529 free(fileindex_path);
5530 unlockerr = lock_worktree(worktree, LOCK_SH);
5531 if (unlockerr && err == NULL)
5533 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5534 struct got_commitable *ct = pe->data;
5535 free_commitable(ct);
5537 got_pathlist_free(&commitable_paths);
5542 got_commitable_get_path(struct got_commitable *ct)
5548 got_commitable_get_status(struct got_commitable *ct)
5553 struct check_rebase_ok_arg {
5554 struct got_worktree *worktree;
5555 struct got_repository *repo;
5558 static const struct got_error *
5559 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5561 const struct got_error *err = NULL;
5562 struct check_rebase_ok_arg *a = arg;
5563 unsigned char status;
5567 /* Reject rebase of a work tree with mixed base commits. */
5568 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5569 SHA1_DIGEST_LENGTH))
5570 return got_error(GOT_ERR_MIXED_COMMITS);
5572 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5574 return got_error_from_errno("asprintf");
5576 /* Reject rebase of a work tree with modified or staged files. */
5577 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5582 if (status != GOT_STATUS_NO_CHANGE)
5583 return got_error(GOT_ERR_MODIFIED);
5584 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5585 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5590 const struct got_error *
5591 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5592 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5593 struct got_worktree *worktree, struct got_reference *branch,
5594 struct got_repository *repo)
5596 const struct got_error *err = NULL;
5597 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5598 char *branch_ref_name = NULL;
5599 char *fileindex_path = NULL;
5600 struct check_rebase_ok_arg ok_arg;
5601 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5602 struct got_object_id *wt_branch_tip = NULL;
5604 *new_base_branch_ref = NULL;
5608 err = lock_worktree(worktree, LOCK_EX);
5612 err = open_fileindex(fileindex, &fileindex_path, worktree);
5616 ok_arg.worktree = worktree;
5618 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5623 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5627 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5631 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5635 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5640 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5643 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5644 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5648 err = got_ref_alloc_symref(new_base_branch_ref,
5649 new_base_branch_ref_name, wt_branch);
5652 err = got_ref_write(*new_base_branch_ref, repo);
5656 /* TODO Lock original branch's ref while rebasing? */
5658 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5662 err = got_ref_write(branch_ref, repo);
5666 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5667 worktree->base_commit_id);
5670 err = got_ref_write(*tmp_branch, repo);
5674 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5678 free(fileindex_path);
5679 free(tmp_branch_name);
5680 free(new_base_branch_ref_name);
5681 free(branch_ref_name);
5683 got_ref_close(branch_ref);
5685 got_ref_close(wt_branch);
5686 free(wt_branch_tip);
5688 if (*new_base_branch_ref) {
5689 got_ref_close(*new_base_branch_ref);
5690 *new_base_branch_ref = NULL;
5693 got_ref_close(*tmp_branch);
5697 got_fileindex_free(*fileindex);
5700 lock_worktree(worktree, LOCK_SH);
5705 const struct got_error *
5706 got_worktree_rebase_continue(struct got_object_id **commit_id,
5707 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5708 struct got_reference **branch, struct got_fileindex **fileindex,
5709 struct got_worktree *worktree, struct got_repository *repo)
5711 const struct got_error *err;
5712 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5713 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5714 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5715 char *fileindex_path = NULL;
5716 int have_staged_files = 0;
5719 *new_base_branch = NULL;
5724 err = lock_worktree(worktree, LOCK_EX);
5728 err = open_fileindex(fileindex, &fileindex_path, worktree);
5732 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5733 &have_staged_files);
5734 if (err && err->code != GOT_ERR_CANCELLED)
5736 if (have_staged_files) {
5737 err = got_error(GOT_ERR_STAGED_PATHS);
5741 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5745 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5749 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5753 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5757 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5761 err = got_ref_open(branch, repo,
5762 got_ref_get_symref_target(branch_ref), 0);
5766 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5770 err = got_ref_resolve(commit_id, repo, commit_ref);
5774 err = got_ref_open(new_base_branch, repo,
5775 new_base_branch_ref_name, 0);
5779 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5783 free(commit_ref_name);
5784 free(branch_ref_name);
5785 free(fileindex_path);
5787 got_ref_close(commit_ref);
5789 got_ref_close(branch_ref);
5794 got_ref_close(*tmp_branch);
5797 if (*new_base_branch) {
5798 got_ref_close(*new_base_branch);
5799 *new_base_branch = NULL;
5802 got_ref_close(*branch);
5806 got_fileindex_free(*fileindex);
5809 lock_worktree(worktree, LOCK_SH);
5814 const struct got_error *
5815 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5817 const struct got_error *err;
5818 char *tmp_branch_name = NULL;
5820 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5824 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5825 free(tmp_branch_name);
5829 static const struct got_error *
5830 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5831 char **logmsg, void *arg)
5837 static const struct got_error *
5838 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5839 const char *path, struct got_object_id *blob_id,
5840 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5841 int dirfd, const char *de_name)
5846 struct collect_merged_paths_arg {
5847 got_worktree_checkout_cb progress_cb;
5849 struct got_pathlist_head *merged_paths;
5852 static const struct got_error *
5853 collect_merged_paths(void *arg, unsigned char status, const char *path)
5855 const struct got_error *err;
5856 struct collect_merged_paths_arg *a = arg;
5858 struct got_pathlist_entry *new;
5860 err = (*a->progress_cb)(a->progress_arg, status, path);
5864 if (status != GOT_STATUS_MERGE &&
5865 status != GOT_STATUS_ADD &&
5866 status != GOT_STATUS_DELETE &&
5867 status != GOT_STATUS_CONFLICT)
5872 return got_error_from_errno("strdup");
5874 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5875 if (err || new == NULL)
5881 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5883 struct got_pathlist_entry *pe;
5885 TAILQ_FOREACH(pe, merged_paths, entry)
5886 free((char *)pe->path);
5888 got_pathlist_free(merged_paths);
5891 static const struct got_error *
5892 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5893 int is_rebase, struct got_repository *repo)
5895 const struct got_error *err;
5896 struct got_reference *commit_ref = NULL;
5898 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5900 if (err->code != GOT_ERR_NOT_REF)
5902 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
5905 err = got_ref_write(commit_ref, repo);
5908 } else if (is_rebase) {
5909 struct got_object_id *stored_id;
5912 err = got_ref_resolve(&stored_id, repo, commit_ref);
5915 cmp = got_object_id_cmp(commit_id, stored_id);
5918 err = got_error(GOT_ERR_REBASE_COMMITID);
5924 got_ref_close(commit_ref);
5928 static const struct got_error *
5929 rebase_merge_files(struct got_pathlist_head *merged_paths,
5930 const char *commit_ref_name, struct got_worktree *worktree,
5931 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
5932 struct got_object_id *commit_id, struct got_repository *repo,
5933 got_worktree_checkout_cb progress_cb, void *progress_arg,
5934 got_cancel_cb cancel_cb, void *cancel_arg)
5936 const struct got_error *err;
5937 struct got_reference *commit_ref = NULL;
5938 struct collect_merged_paths_arg cmp_arg;
5939 char *fileindex_path;
5941 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5943 err = get_fileindex_path(&fileindex_path, worktree);
5947 cmp_arg.progress_cb = progress_cb;
5948 cmp_arg.progress_arg = progress_arg;
5949 cmp_arg.merged_paths = merged_paths;
5950 err = merge_files(worktree, fileindex, fileindex_path,
5951 parent_commit_id, commit_id, repo, collect_merged_paths,
5952 &cmp_arg, cancel_cb, cancel_arg);
5954 got_ref_close(commit_ref);
5958 const struct got_error *
5959 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
5960 struct got_worktree *worktree, struct got_fileindex *fileindex,
5961 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5962 struct got_repository *repo,
5963 got_worktree_checkout_cb progress_cb, void *progress_arg,
5964 got_cancel_cb cancel_cb, void *cancel_arg)
5966 const struct got_error *err;
5967 char *commit_ref_name;
5969 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5973 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
5977 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5978 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5979 progress_arg, cancel_cb, cancel_arg);
5981 free(commit_ref_name);
5985 const struct got_error *
5986 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
5987 struct got_worktree *worktree, struct got_fileindex *fileindex,
5988 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5989 struct got_repository *repo,
5990 got_worktree_checkout_cb progress_cb, void *progress_arg,
5991 got_cancel_cb cancel_cb, void *cancel_arg)
5993 const struct got_error *err;
5994 char *commit_ref_name;
5996 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6000 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6004 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6005 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6006 progress_arg, cancel_cb, cancel_arg);
6008 free(commit_ref_name);
6012 static const struct got_error *
6013 rebase_commit(struct got_object_id **new_commit_id,
6014 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6015 struct got_worktree *worktree, struct got_fileindex *fileindex,
6016 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6017 const char *new_logmsg, struct got_repository *repo)
6019 const struct got_error *err, *sync_err;
6020 struct got_pathlist_head commitable_paths;
6021 struct collect_commitables_arg cc_arg;
6022 char *fileindex_path = NULL;
6023 struct got_reference *head_ref = NULL;
6024 struct got_object_id *head_commit_id = NULL;
6025 char *logmsg = NULL;
6027 TAILQ_INIT(&commitable_paths);
6028 *new_commit_id = NULL;
6030 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6032 err = get_fileindex_path(&fileindex_path, worktree);
6036 cc_arg.commitable_paths = &commitable_paths;
6037 cc_arg.worktree = worktree;
6039 cc_arg.have_staged_files = 0;
6041 * If possible get the status of individual files directly to
6042 * avoid crawling the entire work tree once per rebased commit.
6043 * TODO: Ideally, merged_paths would contain a list of commitables
6044 * we could use so we could skip worktree_status() entirely.
6047 struct got_pathlist_entry *pe;
6048 TAILQ_FOREACH(pe, merged_paths, entry) {
6049 err = worktree_status(worktree, pe->path, fileindex,
6050 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6056 err = worktree_status(worktree, "", fileindex, repo,
6057 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6062 if (TAILQ_EMPTY(&commitable_paths)) {
6063 /* No-op change; commit will be elided. */
6064 err = got_ref_delete(commit_ref, repo);
6067 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6071 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6075 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6080 logmsg = strdup(new_logmsg);
6081 if (logmsg == NULL) {
6082 err = got_error_from_errno("strdup");
6086 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6091 /* NB: commit_worktree will call free(logmsg) */
6092 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6093 worktree, got_object_commit_get_author(orig_commit),
6094 got_object_commit_get_committer(orig_commit),
6095 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6099 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6103 err = got_ref_delete(commit_ref, repo);
6107 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
6109 sync_err = sync_fileindex(fileindex, fileindex_path);
6110 if (sync_err && err == NULL)
6113 free(fileindex_path);
6114 free(head_commit_id);
6116 got_ref_close(head_ref);
6118 free(*new_commit_id);
6119 *new_commit_id = NULL;
6124 const struct got_error *
6125 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6126 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6127 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6128 struct got_commit_object *orig_commit,
6129 struct got_object_id *orig_commit_id, struct got_repository *repo)
6131 const struct got_error *err;
6132 char *commit_ref_name;
6133 struct got_reference *commit_ref = NULL;
6134 struct got_object_id *commit_id = NULL;
6136 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6140 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6143 err = got_ref_resolve(&commit_id, repo, commit_ref);
6146 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6147 err = got_error(GOT_ERR_REBASE_COMMITID);
6151 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6152 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6155 got_ref_close(commit_ref);
6156 free(commit_ref_name);
6161 const struct got_error *
6162 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6163 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6164 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6165 struct got_commit_object *orig_commit,
6166 struct got_object_id *orig_commit_id, const char *new_logmsg,
6167 struct got_repository *repo)
6169 const struct got_error *err;
6170 char *commit_ref_name;
6171 struct got_reference *commit_ref = NULL;
6173 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6177 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6181 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6182 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6185 got_ref_close(commit_ref);
6186 free(commit_ref_name);
6190 const struct got_error *
6191 got_worktree_rebase_postpone(struct got_worktree *worktree,
6192 struct got_fileindex *fileindex)
6195 got_fileindex_free(fileindex);
6196 return lock_worktree(worktree, LOCK_SH);
6199 static const struct got_error *
6200 delete_ref(const char *name, struct got_repository *repo)
6202 const struct got_error *err;
6203 struct got_reference *ref;
6205 err = got_ref_open(&ref, repo, name, 0);
6207 if (err->code == GOT_ERR_NOT_REF)
6212 err = got_ref_delete(ref, repo);
6217 static const struct got_error *
6218 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6220 const struct got_error *err;
6221 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6222 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6224 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6227 err = delete_ref(tmp_branch_name, repo);
6231 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6234 err = delete_ref(new_base_branch_ref_name, repo);
6238 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6241 err = delete_ref(branch_ref_name, repo);
6245 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6248 err = delete_ref(commit_ref_name, repo);
6253 free(tmp_branch_name);
6254 free(new_base_branch_ref_name);
6255 free(branch_ref_name);
6256 free(commit_ref_name);
6260 const struct got_error *
6261 got_worktree_rebase_complete(struct got_worktree *worktree,
6262 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6263 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6264 struct got_repository *repo)
6266 const struct got_error *err, *unlockerr;
6267 struct got_object_id *new_head_commit_id = NULL;
6269 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6273 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6277 err = got_ref_write(rebased_branch, repo);
6281 err = got_worktree_set_head_ref(worktree, rebased_branch);
6285 err = delete_rebase_refs(worktree, repo);
6288 got_fileindex_free(fileindex);
6289 free(new_head_commit_id);
6290 unlockerr = lock_worktree(worktree, LOCK_SH);
6291 if (unlockerr && err == NULL)
6296 const struct got_error *
6297 got_worktree_rebase_abort(struct got_worktree *worktree,
6298 struct got_fileindex *fileindex, struct got_repository *repo,
6299 struct got_reference *new_base_branch,
6300 got_worktree_checkout_cb progress_cb, void *progress_arg)
6302 const struct got_error *err, *unlockerr, *sync_err;
6303 struct got_reference *resolved = NULL;
6304 struct got_object_id *commit_id = NULL;
6305 char *fileindex_path = NULL;
6306 struct revert_file_args rfa;
6307 struct got_object_id *tree_id = NULL;
6309 err = lock_worktree(worktree, LOCK_EX);
6313 err = got_ref_open(&resolved, repo,
6314 got_ref_get_symref_target(new_base_branch), 0);
6318 err = got_worktree_set_head_ref(worktree, resolved);
6323 * XXX commits to the base branch could have happened while
6324 * we were busy rebasing; should we store the original commit ID
6325 * when rebase begins and read it back here?
6327 err = got_ref_resolve(&commit_id, repo, resolved);
6331 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6335 err = got_object_id_by_path(&tree_id, repo,
6336 worktree->base_commit_id, worktree->path_prefix);
6340 err = delete_rebase_refs(worktree, repo);
6344 err = get_fileindex_path(&fileindex_path, worktree);
6348 rfa.worktree = worktree;
6349 rfa.fileindex = fileindex;
6350 rfa.progress_cb = progress_cb;
6351 rfa.progress_arg = progress_arg;
6352 rfa.patch_cb = NULL;
6353 rfa.patch_arg = NULL;
6355 err = worktree_status(worktree, "", fileindex, repo,
6356 revert_file, &rfa, NULL, NULL, 0, 0);
6360 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6361 repo, progress_cb, progress_arg, NULL, NULL);
6363 sync_err = sync_fileindex(fileindex, fileindex_path);
6364 if (sync_err && err == NULL)
6367 got_ref_close(resolved);
6371 got_fileindex_free(fileindex);
6372 free(fileindex_path);
6374 unlockerr = lock_worktree(worktree, LOCK_SH);
6375 if (unlockerr && err == NULL)
6380 const struct got_error *
6381 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6382 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6383 struct got_fileindex **fileindex, struct got_worktree *worktree,
6384 struct got_repository *repo)
6386 const struct got_error *err = NULL;
6387 char *tmp_branch_name = NULL;
6388 char *branch_ref_name = NULL;
6389 char *base_commit_ref_name = NULL;
6390 char *fileindex_path = NULL;
6391 struct check_rebase_ok_arg ok_arg;
6392 struct got_reference *wt_branch = NULL;
6393 struct got_reference *base_commit_ref = NULL;
6397 *base_commit_id = NULL;
6400 err = lock_worktree(worktree, LOCK_EX);
6404 err = open_fileindex(fileindex, &fileindex_path, worktree);
6408 ok_arg.worktree = worktree;
6410 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6415 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6419 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6423 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6428 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6433 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6437 err = got_ref_write(*branch_ref, repo);
6441 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6442 worktree->base_commit_id);
6445 err = got_ref_write(base_commit_ref, repo);
6448 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6449 if (*base_commit_id == NULL) {
6450 err = got_error_from_errno("got_object_id_dup");
6454 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6455 worktree->base_commit_id);
6458 err = got_ref_write(*tmp_branch, repo);
6462 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6466 free(fileindex_path);
6467 free(tmp_branch_name);
6468 free(branch_ref_name);
6469 free(base_commit_ref_name);
6471 got_ref_close(wt_branch);
6474 got_ref_close(*branch_ref);
6478 got_ref_close(*tmp_branch);
6481 free(*base_commit_id);
6483 got_fileindex_free(*fileindex);
6486 lock_worktree(worktree, LOCK_SH);
6491 const struct got_error *
6492 got_worktree_histedit_postpone(struct got_worktree *worktree,
6493 struct got_fileindex *fileindex)
6496 got_fileindex_free(fileindex);
6497 return lock_worktree(worktree, LOCK_SH);
6500 const struct got_error *
6501 got_worktree_histedit_in_progress(int *in_progress,
6502 struct got_worktree *worktree)
6504 const struct got_error *err;
6505 char *tmp_branch_name = NULL;
6507 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6511 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6512 free(tmp_branch_name);
6516 const struct got_error *
6517 got_worktree_histedit_continue(struct got_object_id **commit_id,
6518 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6519 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6520 struct got_worktree *worktree, struct got_repository *repo)
6522 const struct got_error *err;
6523 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6524 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6525 struct got_reference *commit_ref = NULL;
6526 struct got_reference *base_commit_ref = NULL;
6527 char *fileindex_path = NULL;
6528 int have_staged_files = 0;
6532 *base_commit_id = NULL;
6535 err = lock_worktree(worktree, LOCK_EX);
6539 err = open_fileindex(fileindex, &fileindex_path, worktree);
6543 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6544 &have_staged_files);
6545 if (err && err->code != GOT_ERR_CANCELLED)
6547 if (have_staged_files) {
6548 err = got_error(GOT_ERR_STAGED_PATHS);
6552 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6556 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6560 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6564 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6569 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6573 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6576 err = got_ref_resolve(commit_id, repo, commit_ref);
6580 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6583 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6587 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6591 free(commit_ref_name);
6592 free(branch_ref_name);
6593 free(fileindex_path);
6595 got_ref_close(commit_ref);
6596 if (base_commit_ref)
6597 got_ref_close(base_commit_ref);
6601 free(*base_commit_id);
6602 *base_commit_id = NULL;
6604 got_ref_close(*tmp_branch);
6608 got_fileindex_free(*fileindex);
6611 lock_worktree(worktree, LOCK_EX);
6616 static const struct got_error *
6617 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6619 const struct got_error *err;
6620 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6621 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6623 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6626 err = delete_ref(tmp_branch_name, repo);
6630 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6634 err = delete_ref(base_commit_ref_name, repo);
6638 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6641 err = delete_ref(branch_ref_name, repo);
6645 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6648 err = delete_ref(commit_ref_name, repo);
6652 free(tmp_branch_name);
6653 free(base_commit_ref_name);
6654 free(branch_ref_name);
6655 free(commit_ref_name);
6659 const struct got_error *
6660 got_worktree_histedit_abort(struct got_worktree *worktree,
6661 struct got_fileindex *fileindex, struct got_repository *repo,
6662 struct got_reference *branch, struct got_object_id *base_commit_id,
6663 got_worktree_checkout_cb progress_cb, void *progress_arg)
6665 const struct got_error *err, *unlockerr, *sync_err;
6666 struct got_reference *resolved = NULL;
6667 char *fileindex_path = NULL;
6668 struct got_object_id *tree_id = NULL;
6669 struct revert_file_args rfa;
6671 err = lock_worktree(worktree, LOCK_EX);
6675 err = got_ref_open(&resolved, repo,
6676 got_ref_get_symref_target(branch), 0);
6680 err = got_worktree_set_head_ref(worktree, resolved);
6684 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6688 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6689 worktree->path_prefix);
6693 err = delete_histedit_refs(worktree, repo);
6697 err = get_fileindex_path(&fileindex_path, worktree);
6701 rfa.worktree = worktree;
6702 rfa.fileindex = fileindex;
6703 rfa.progress_cb = progress_cb;
6704 rfa.progress_arg = progress_arg;
6705 rfa.patch_cb = NULL;
6706 rfa.patch_arg = NULL;
6708 err = worktree_status(worktree, "", fileindex, repo,
6709 revert_file, &rfa, NULL, NULL, 0, 0);
6713 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6714 repo, progress_cb, progress_arg, NULL, NULL);
6716 sync_err = sync_fileindex(fileindex, fileindex_path);
6717 if (sync_err && err == NULL)
6720 got_ref_close(resolved);
6722 free(fileindex_path);
6724 unlockerr = lock_worktree(worktree, LOCK_SH);
6725 if (unlockerr && err == NULL)
6730 const struct got_error *
6731 got_worktree_histedit_complete(struct got_worktree *worktree,
6732 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6733 struct got_reference *edited_branch, struct got_repository *repo)
6735 const struct got_error *err, *unlockerr;
6736 struct got_object_id *new_head_commit_id = NULL;
6737 struct got_reference *resolved = NULL;
6739 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6743 err = got_ref_open(&resolved, repo,
6744 got_ref_get_symref_target(edited_branch), 0);
6748 err = got_ref_change_ref(resolved, new_head_commit_id);
6752 err = got_ref_write(resolved, repo);
6756 err = got_worktree_set_head_ref(worktree, resolved);
6760 err = delete_histedit_refs(worktree, repo);
6763 got_fileindex_free(fileindex);
6764 free(new_head_commit_id);
6765 unlockerr = lock_worktree(worktree, LOCK_SH);
6766 if (unlockerr && err == NULL)
6771 const struct got_error *
6772 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6773 struct got_object_id *commit_id, struct got_repository *repo)
6775 const struct got_error *err;
6776 char *commit_ref_name;
6778 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6782 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6786 err = delete_ref(commit_ref_name, repo);
6788 free(commit_ref_name);
6792 const struct got_error *
6793 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6794 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6795 struct got_worktree *worktree, const char *refname,
6796 struct got_repository *repo)
6798 const struct got_error *err = NULL;
6799 char *fileindex_path = NULL;
6800 struct check_rebase_ok_arg ok_arg;
6804 *base_branch_ref = NULL;
6806 err = lock_worktree(worktree, LOCK_EX);
6810 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6811 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6812 "cannot integrate a branch into itself; "
6813 "update -b or different branch name required");
6817 err = open_fileindex(fileindex, &fileindex_path, worktree);
6821 /* Preconditions are the same as for rebase. */
6822 ok_arg.worktree = worktree;
6824 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6829 err = got_ref_open(branch_ref, repo, refname, 1);
6833 err = got_ref_open(base_branch_ref, repo,
6834 got_worktree_get_head_ref_name(worktree), 1);
6838 got_ref_close(*branch_ref);
6841 if (*base_branch_ref) {
6842 got_ref_close(*base_branch_ref);
6843 *base_branch_ref = NULL;
6846 got_fileindex_free(*fileindex);
6849 lock_worktree(worktree, LOCK_SH);
6854 const struct got_error *
6855 got_worktree_integrate_continue(struct got_worktree *worktree,
6856 struct got_fileindex *fileindex, struct got_repository *repo,
6857 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6858 got_worktree_checkout_cb progress_cb, void *progress_arg,
6859 got_cancel_cb cancel_cb, void *cancel_arg)
6861 const struct got_error *err = NULL, *sync_err, *unlockerr;
6862 char *fileindex_path = NULL;
6863 struct got_object_id *tree_id = NULL, *commit_id = NULL;
6865 err = get_fileindex_path(&fileindex_path, worktree);
6869 err = got_ref_resolve(&commit_id, repo, branch_ref);
6873 err = got_object_id_by_path(&tree_id, repo, commit_id,
6874 worktree->path_prefix);
6878 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6882 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6883 progress_cb, progress_arg, cancel_cb, cancel_arg);
6887 err = got_ref_change_ref(base_branch_ref, commit_id);
6891 err = got_ref_write(base_branch_ref, repo);
6893 sync_err = sync_fileindex(fileindex, fileindex_path);
6894 if (sync_err && err == NULL)
6898 unlockerr = got_ref_unlock(branch_ref);
6899 if (unlockerr && err == NULL)
6901 got_ref_close(branch_ref);
6903 unlockerr = got_ref_unlock(base_branch_ref);
6904 if (unlockerr && err == NULL)
6906 got_ref_close(base_branch_ref);
6908 got_fileindex_free(fileindex);
6909 free(fileindex_path);
6912 unlockerr = lock_worktree(worktree, LOCK_SH);
6913 if (unlockerr && err == NULL)
6918 const struct got_error *
6919 got_worktree_integrate_abort(struct got_worktree *worktree,
6920 struct got_fileindex *fileindex, struct got_repository *repo,
6921 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
6923 const struct got_error *err = NULL, *unlockerr = NULL;
6925 got_fileindex_free(fileindex);
6927 err = lock_worktree(worktree, LOCK_SH);
6929 unlockerr = got_ref_unlock(branch_ref);
6930 if (unlockerr && err == NULL)
6932 got_ref_close(branch_ref);
6934 unlockerr = got_ref_unlock(base_branch_ref);
6935 if (unlockerr && err == NULL)
6937 got_ref_close(base_branch_ref);
6942 struct check_stage_ok_arg {
6943 struct got_object_id *head_commit_id;
6944 struct got_worktree *worktree;
6945 struct got_fileindex *fileindex;
6946 struct got_repository *repo;
6950 const struct got_error *
6951 check_stage_ok(void *arg, unsigned char status,
6952 unsigned char staged_status, const char *relpath,
6953 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6954 struct got_object_id *commit_id, int dirfd, const char *de_name)
6956 struct check_stage_ok_arg *a = arg;
6957 const struct got_error *err = NULL;
6958 struct got_fileindex_entry *ie;
6959 struct got_object_id base_commit_id;
6960 struct got_object_id *base_commit_idp = NULL;
6961 char *in_repo_path = NULL, *p;
6963 if (status == GOT_STATUS_UNVERSIONED ||
6964 status == GOT_STATUS_NO_CHANGE)
6966 if (status == GOT_STATUS_NONEXISTENT)
6967 return got_error_set_errno(ENOENT, relpath);
6969 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6971 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6973 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
6974 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
6976 return got_error_from_errno("asprintf");
6978 if (got_fileindex_entry_has_commit(ie)) {
6979 memcpy(base_commit_id.sha1, ie->commit_sha1,
6980 SHA1_DIGEST_LENGTH);
6981 base_commit_idp = &base_commit_id;
6984 if (status == GOT_STATUS_CONFLICT) {
6985 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
6987 } else if (status != GOT_STATUS_ADD &&
6988 status != GOT_STATUS_MODIFY &&
6989 status != GOT_STATUS_DELETE) {
6990 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
6994 a->have_changes = 1;
6999 err = check_out_of_date(p, status, staged_status,
7000 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7001 GOT_ERR_STAGE_OUT_OF_DATE);
7007 struct stage_path_arg {
7008 struct got_worktree *worktree;
7009 struct got_fileindex *fileindex;
7010 struct got_repository *repo;
7011 got_worktree_status_cb status_cb;
7013 got_worktree_patch_cb patch_cb;
7015 int staged_something;
7016 int allow_bad_symlinks;
7019 static const struct got_error *
7020 stage_path(void *arg, unsigned char status,
7021 unsigned char staged_status, const char *relpath,
7022 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7023 struct got_object_id *commit_id, int dirfd, const char *de_name)
7025 struct stage_path_arg *a = arg;
7026 const struct got_error *err = NULL;
7027 struct got_fileindex_entry *ie;
7028 char *ondisk_path = NULL, *path_content = NULL;
7030 struct got_object_id *new_staged_blob_id = NULL;
7033 if (status == GOT_STATUS_UNVERSIONED)
7036 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7038 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7040 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7042 return got_error_from_errno("asprintf");
7045 case GOT_STATUS_ADD:
7046 case GOT_STATUS_MODIFY:
7047 /* XXX could sb.st_mode be passed in by our caller? */
7048 if (lstat(ondisk_path, &sb) == -1) {
7049 err = got_error_from_errno2("lstat", ondisk_path);
7053 if (status == GOT_STATUS_ADD) {
7054 int choice = GOT_PATCH_CHOICE_NONE;
7055 err = (*a->patch_cb)(&choice, a->patch_arg,
7056 status, ie->path, NULL, 1, 1);
7059 if (choice != GOT_PATCH_CHOICE_YES)
7062 err = create_patched_content(&path_content, 0,
7063 staged_blob_id ? staged_blob_id : blob_id,
7064 ondisk_path, dirfd, de_name, ie->path,
7065 a->repo, a->patch_cb, a->patch_arg);
7066 if (err || path_content == NULL)
7070 err = got_object_blob_create(&new_staged_blob_id,
7071 path_content ? path_content : ondisk_path, a->repo);
7074 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7075 SHA1_DIGEST_LENGTH);
7076 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7077 stage = GOT_FILEIDX_STAGE_ADD;
7079 stage = GOT_FILEIDX_STAGE_MODIFY;
7080 got_fileindex_entry_stage_set(ie, stage);
7081 if (S_ISLNK(sb.st_mode)) {
7082 int is_bad_symlink = 0;
7083 if (!a->allow_bad_symlinks) {
7084 char target_path[PATH_MAX];
7086 target_len = readlink(ondisk_path, target_path,
7087 sizeof(target_path));
7088 if (target_len == -1) {
7089 err = got_error_from_errno2("readlink",
7093 err = is_bad_symlink_target(&is_bad_symlink,
7094 target_path, target_len, ondisk_path,
7095 a->worktree->root_path);
7098 if (is_bad_symlink) {
7099 err = got_error_path(ondisk_path,
7100 GOT_ERR_BAD_SYMLINK);
7105 got_fileindex_entry_staged_filetype_set(ie,
7106 GOT_FILEIDX_MODE_BAD_SYMLINK);
7108 got_fileindex_entry_staged_filetype_set(ie,
7109 GOT_FILEIDX_MODE_SYMLINK);
7111 got_fileindex_entry_staged_filetype_set(ie,
7112 GOT_FILEIDX_MODE_REGULAR_FILE);
7114 a->staged_something = 1;
7115 if (a->status_cb == NULL)
7117 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7118 get_staged_status(ie), relpath, blob_id,
7119 new_staged_blob_id, NULL, dirfd, de_name);
7121 case GOT_STATUS_DELETE:
7122 if (staged_status == GOT_STATUS_DELETE)
7125 int choice = GOT_PATCH_CHOICE_NONE;
7126 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7127 ie->path, NULL, 1, 1);
7130 if (choice == GOT_PATCH_CHOICE_NO)
7132 if (choice != GOT_PATCH_CHOICE_YES) {
7133 err = got_error(GOT_ERR_PATCH_CHOICE);
7137 stage = GOT_FILEIDX_STAGE_DELETE;
7138 got_fileindex_entry_stage_set(ie, stage);
7139 a->staged_something = 1;
7140 if (a->status_cb == NULL)
7142 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7143 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7146 case GOT_STATUS_NO_CHANGE:
7148 case GOT_STATUS_CONFLICT:
7149 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7151 case GOT_STATUS_NONEXISTENT:
7152 err = got_error_set_errno(ENOENT, relpath);
7155 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7159 if (path_content && unlink(path_content) == -1 && err == NULL)
7160 err = got_error_from_errno2("unlink", path_content);
7163 free(new_staged_blob_id);
7167 const struct got_error *
7168 got_worktree_stage(struct got_worktree *worktree,
7169 struct got_pathlist_head *paths,
7170 got_worktree_status_cb status_cb, void *status_arg,
7171 got_worktree_patch_cb patch_cb, void *patch_arg,
7172 int allow_bad_symlinks, struct got_repository *repo)
7174 const struct got_error *err = NULL, *sync_err, *unlockerr;
7175 struct got_pathlist_entry *pe;
7176 struct got_fileindex *fileindex = NULL;
7177 char *fileindex_path = NULL;
7178 struct got_reference *head_ref = NULL;
7179 struct got_object_id *head_commit_id = NULL;
7180 struct check_stage_ok_arg oka;
7181 struct stage_path_arg spa;
7183 err = lock_worktree(worktree, LOCK_EX);
7187 err = got_ref_open(&head_ref, repo,
7188 got_worktree_get_head_ref_name(worktree), 0);
7191 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7194 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7198 /* Check pre-conditions before staging anything. */
7199 oka.head_commit_id = head_commit_id;
7200 oka.worktree = worktree;
7201 oka.fileindex = fileindex;
7203 oka.have_changes = 0;
7204 TAILQ_FOREACH(pe, paths, entry) {
7205 err = worktree_status(worktree, pe->path, fileindex, repo,
7206 check_stage_ok, &oka, NULL, NULL, 0, 0);
7210 if (!oka.have_changes) {
7211 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7215 spa.worktree = worktree;
7216 spa.fileindex = fileindex;
7218 spa.patch_cb = patch_cb;
7219 spa.patch_arg = patch_arg;
7220 spa.status_cb = status_cb;
7221 spa.status_arg = status_arg;
7222 spa.staged_something = 0;
7223 spa.allow_bad_symlinks = allow_bad_symlinks;
7224 TAILQ_FOREACH(pe, paths, entry) {
7225 err = worktree_status(worktree, pe->path, fileindex, repo,
7226 stage_path, &spa, NULL, NULL, 0, 0);
7230 if (!spa.staged_something) {
7231 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7235 sync_err = sync_fileindex(fileindex, fileindex_path);
7236 if (sync_err && err == NULL)
7240 got_ref_close(head_ref);
7241 free(head_commit_id);
7242 free(fileindex_path);
7244 got_fileindex_free(fileindex);
7245 unlockerr = lock_worktree(worktree, LOCK_SH);
7246 if (unlockerr && err == NULL)
7251 struct unstage_path_arg {
7252 struct got_worktree *worktree;
7253 struct got_fileindex *fileindex;
7254 struct got_repository *repo;
7255 got_worktree_checkout_cb progress_cb;
7257 got_worktree_patch_cb patch_cb;
7261 static const struct got_error *
7262 create_unstaged_content(char **path_unstaged_content,
7263 char **path_new_staged_content, struct got_object_id *blob_id,
7264 struct got_object_id *staged_blob_id, const char *relpath,
7265 struct got_repository *repo,
7266 got_worktree_patch_cb patch_cb, void *patch_arg)
7268 const struct got_error *err;
7269 struct got_blob_object *blob = NULL, *staged_blob = NULL;
7270 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7271 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7272 struct stat sb1, sb2;
7273 struct got_diff_changes *changes = NULL;
7274 struct got_diff_state *ds = NULL;
7275 struct got_diff_args *args = NULL;
7276 struct got_diff_change *change;
7277 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
7278 int have_content = 0, have_rejected_content = 0;
7280 *path_unstaged_content = NULL;
7281 *path_new_staged_content = NULL;
7283 err = got_object_id_str(&label1, blob_id);
7286 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7290 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7294 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7298 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7302 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7306 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7310 if (stat(path1, &sb1) == -1) {
7311 err = got_error_from_errno2("stat", path1);
7315 if (stat(path2, &sb2) == -1) {
7316 err = got_error_from_errno2("stat", path2);
7320 err = got_diff_files(&changes, &ds, &args, &diff_flags,
7321 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
7325 err = got_opentemp_named(path_unstaged_content, &outfile,
7326 "got-unstaged-content");
7329 err = got_opentemp_named(path_new_staged_content, &rejectfile,
7330 "got-new-staged-content");
7334 if (fseek(f1, 0L, SEEK_SET) == -1) {
7335 err = got_ferror(f1, GOT_ERR_IO);
7338 if (fseek(f2, 0L, SEEK_SET) == -1) {
7339 err = got_ferror(f2, GOT_ERR_IO);
7342 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
7344 err = apply_or_reject_change(&choice, change, ++n,
7345 changes->nchanges, ds, args, diff_flags, relpath,
7346 f1, f2, &line_cur1, &line_cur2,
7347 outfile, rejectfile, patch_cb, patch_arg);
7350 if (choice == GOT_PATCH_CHOICE_YES)
7353 have_rejected_content = 1;
7354 if (choice == GOT_PATCH_CHOICE_QUIT)
7357 if (have_content || have_rejected_content)
7358 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7359 outfile, rejectfile);
7363 got_object_blob_close(blob);
7365 got_object_blob_close(staged_blob);
7366 if (f1 && fclose(f1) == EOF && err == NULL)
7367 err = got_error_from_errno2("fclose", path1);
7368 if (f2 && fclose(f2) == EOF && err == NULL)
7369 err = got_error_from_errno2("fclose", path2);
7370 if (outfile && fclose(outfile) == EOF && err == NULL)
7371 err = got_error_from_errno2("fclose", *path_unstaged_content);
7372 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7373 err = got_error_from_errno2("fclose", *path_new_staged_content);
7374 if (path1 && unlink(path1) == -1 && err == NULL)
7375 err = got_error_from_errno2("unlink", path1);
7376 if (path2 && unlink(path2) == -1 && err == NULL)
7377 err = got_error_from_errno2("unlink", path2);
7378 if (err || !have_content) {
7379 if (*path_unstaged_content &&
7380 unlink(*path_unstaged_content) == -1 && err == NULL)
7381 err = got_error_from_errno2("unlink",
7382 *path_unstaged_content);
7383 free(*path_unstaged_content);
7384 *path_unstaged_content = NULL;
7386 if (err || !have_content || !have_rejected_content) {
7387 if (*path_new_staged_content &&
7388 unlink(*path_new_staged_content) == -1 && err == NULL)
7389 err = got_error_from_errno2("unlink",
7390 *path_new_staged_content);
7391 free(*path_new_staged_content);
7392 *path_new_staged_content = NULL;
7396 got_diff_state_free(ds);
7400 got_diff_free_changes(changes);
7406 static const struct got_error *
7407 unstage_hunks(struct got_object_id *staged_blob_id,
7408 struct got_blob_object *blob_base,
7409 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
7410 const char *ondisk_path, const char *label_orig,
7411 struct got_worktree *worktree, struct got_repository *repo,
7412 got_worktree_patch_cb patch_cb, void *patch_arg,
7413 got_worktree_checkout_cb progress_cb, void *progress_arg)
7415 const struct got_error *err = NULL;
7416 char *path_unstaged_content = NULL;
7417 char *path_new_staged_content = NULL;
7418 struct got_object_id *new_staged_blob_id = NULL;
7422 err = create_unstaged_content(&path_unstaged_content,
7423 &path_new_staged_content, blob_id, staged_blob_id,
7424 ie->path, repo, patch_cb, patch_arg);
7428 if (path_unstaged_content == NULL)
7431 if (path_new_staged_content) {
7432 err = got_object_blob_create(&new_staged_blob_id,
7433 path_new_staged_content, repo);
7438 f = fopen(path_unstaged_content, "r");
7440 err = got_error_from_errno2("fopen",
7441 path_unstaged_content);
7444 if (fstat(fileno(f), &sb) == -1) {
7445 err = got_error_from_errno2("fstat", path_unstaged_content);
7448 if (got_fileindex_entry_staged_filetype_get(ie) ==
7449 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
7450 char link_target[PATH_MAX];
7452 r = fread(link_target, 1, sizeof(link_target), f);
7453 if (r == 0 && ferror(f)) {
7454 err = got_error_from_errno("fread");
7457 if (r >= sizeof(link_target)) { /* should not happen */
7458 err = got_error(GOT_ERR_NO_SPACE);
7461 link_target[r] = '\0';
7462 err = merge_symlink(worktree, blob_base,
7463 ondisk_path, ie->path, label_orig, link_target,
7464 worktree->base_commit_id, repo, progress_cb,
7467 int local_changes_subsumed;
7468 err = merge_file(&local_changes_subsumed, worktree,
7469 blob_base, ondisk_path, ie->path,
7470 got_fileindex_perms_to_st(ie),
7471 path_unstaged_content, label_orig, "unstaged",
7472 repo, progress_cb, progress_arg);
7477 if (new_staged_blob_id) {
7478 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7479 SHA1_DIGEST_LENGTH);
7481 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7483 free(new_staged_blob_id);
7484 if (path_unstaged_content &&
7485 unlink(path_unstaged_content) == -1 && err == NULL)
7486 err = got_error_from_errno2("unlink", path_unstaged_content);
7487 if (path_new_staged_content &&
7488 unlink(path_new_staged_content) == -1 && err == NULL)
7489 err = got_error_from_errno2("unlink", path_new_staged_content);
7490 if (f && fclose(f) != 0 && err == NULL)
7491 err = got_error_from_errno2("fclose", path_unstaged_content);
7492 free(path_unstaged_content);
7493 free(path_new_staged_content);
7497 static const struct got_error *
7498 unstage_path(void *arg, unsigned char status,
7499 unsigned char staged_status, const char *relpath,
7500 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7501 struct got_object_id *commit_id, int dirfd, const char *de_name)
7503 const struct got_error *err = NULL;
7504 struct unstage_path_arg *a = arg;
7505 struct got_fileindex_entry *ie;
7506 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7507 char *ondisk_path = NULL;
7508 char *id_str = NULL, *label_orig = NULL;
7509 int local_changes_subsumed;
7512 if (staged_status != GOT_STATUS_ADD &&
7513 staged_status != GOT_STATUS_MODIFY &&
7514 staged_status != GOT_STATUS_DELETE)
7517 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7519 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7521 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7523 return got_error_from_errno("asprintf");
7525 err = got_object_id_str(&id_str,
7526 commit_id ? commit_id : a->worktree->base_commit_id);
7529 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7531 err = got_error_from_errno("asprintf");
7535 switch (staged_status) {
7536 case GOT_STATUS_MODIFY:
7537 err = got_object_open_as_blob(&blob_base, a->repo,
7542 case GOT_STATUS_ADD:
7544 if (staged_status == GOT_STATUS_ADD) {
7545 int choice = GOT_PATCH_CHOICE_NONE;
7546 err = (*a->patch_cb)(&choice, a->patch_arg,
7547 staged_status, ie->path, NULL, 1, 1);
7550 if (choice != GOT_PATCH_CHOICE_YES)
7553 err = unstage_hunks(staged_blob_id,
7554 blob_base, blob_id, ie, ondisk_path,
7555 label_orig, a->worktree, a->repo,
7556 a->patch_cb, a->patch_arg,
7557 a->progress_cb, a->progress_arg);
7558 break; /* Done with this file. */
7561 err = got_object_open_as_blob(&blob_staged, a->repo,
7562 staged_blob_id, 8192);
7565 switch (got_fileindex_entry_staged_filetype_get(ie)) {
7566 case GOT_FILEIDX_MODE_BAD_SYMLINK:
7567 case GOT_FILEIDX_MODE_REGULAR_FILE:
7568 err = merge_blob(&local_changes_subsumed, a->worktree,
7569 blob_base, ondisk_path, relpath,
7570 got_fileindex_perms_to_st(ie), label_orig,
7571 blob_staged, commit_id ? commit_id :
7572 a->worktree->base_commit_id, a->repo,
7573 a->progress_cb, a->progress_arg);
7575 case GOT_FILEIDX_MODE_SYMLINK:
7576 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7577 char *staged_target;
7578 err = got_object_blob_read_to_str(
7579 &staged_target, blob_staged);
7582 err = merge_symlink(a->worktree, blob_base,
7583 ondisk_path, relpath, label_orig,
7584 staged_target, commit_id ? commit_id :
7585 a->worktree->base_commit_id,
7586 a->repo, a->progress_cb, a->progress_arg);
7587 free(staged_target);
7589 err = merge_blob(&local_changes_subsumed,
7590 a->worktree, blob_base, ondisk_path,
7591 relpath, got_fileindex_perms_to_st(ie),
7592 label_orig, blob_staged,
7593 commit_id ? commit_id :
7594 a->worktree->base_commit_id, a->repo,
7595 a->progress_cb, a->progress_arg);
7599 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
7603 got_fileindex_entry_stage_set(ie,
7604 GOT_FILEIDX_STAGE_NONE);
7606 case GOT_STATUS_DELETE:
7608 int choice = GOT_PATCH_CHOICE_NONE;
7609 err = (*a->patch_cb)(&choice, a->patch_arg,
7610 staged_status, ie->path, NULL, 1, 1);
7613 if (choice == GOT_PATCH_CHOICE_NO)
7615 if (choice != GOT_PATCH_CHOICE_YES) {
7616 err = got_error(GOT_ERR_PATCH_CHOICE);
7620 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7621 err = get_file_status(&status, &sb, ie, ondisk_path,
7622 dirfd, de_name, a->repo);
7625 err = (*a->progress_cb)(a->progress_arg, status, relpath);
7631 got_object_blob_close(blob_base);
7633 got_object_blob_close(blob_staged);
7639 const struct got_error *
7640 got_worktree_unstage(struct got_worktree *worktree,
7641 struct got_pathlist_head *paths,
7642 got_worktree_checkout_cb progress_cb, void *progress_arg,
7643 got_worktree_patch_cb patch_cb, void *patch_arg,
7644 struct got_repository *repo)
7646 const struct got_error *err = NULL, *sync_err, *unlockerr;
7647 struct got_pathlist_entry *pe;
7648 struct got_fileindex *fileindex = NULL;
7649 char *fileindex_path = NULL;
7650 struct unstage_path_arg upa;
7652 err = lock_worktree(worktree, LOCK_EX);
7656 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7660 upa.worktree = worktree;
7661 upa.fileindex = fileindex;
7663 upa.progress_cb = progress_cb;
7664 upa.progress_arg = progress_arg;
7665 upa.patch_cb = patch_cb;
7666 upa.patch_arg = patch_arg;
7667 TAILQ_FOREACH(pe, paths, entry) {
7668 err = worktree_status(worktree, pe->path, fileindex, repo,
7669 unstage_path, &upa, NULL, NULL, 0, 0);
7674 sync_err = sync_fileindex(fileindex, fileindex_path);
7675 if (sync_err && err == NULL)
7678 free(fileindex_path);
7680 got_fileindex_free(fileindex);
7681 unlockerr = lock_worktree(worktree, LOCK_SH);
7682 if (unlockerr && err == NULL)
7687 struct report_file_info_arg {
7688 struct got_worktree *worktree;
7689 got_worktree_path_info_cb info_cb;
7691 struct got_pathlist_head *paths;
7692 got_cancel_cb cancel_cb;
7696 static const struct got_error *
7697 report_file_info(void *arg, struct got_fileindex_entry *ie)
7699 struct report_file_info_arg *a = arg;
7700 struct got_pathlist_entry *pe;
7701 struct got_object_id blob_id, staged_blob_id, commit_id;
7702 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
7703 struct got_object_id *commit_idp = NULL;
7706 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
7707 return got_error(GOT_ERR_CANCELLED);
7709 TAILQ_FOREACH(pe, a->paths, entry) {
7710 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
7711 got_path_is_child(ie->path, pe->path, pe->path_len))
7714 if (pe == NULL) /* not found */
7717 if (got_fileindex_entry_has_blob(ie)) {
7718 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
7719 blob_idp = &blob_id;
7721 stage = got_fileindex_entry_stage_get(ie);
7722 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
7723 stage == GOT_FILEIDX_STAGE_ADD) {
7724 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
7725 SHA1_DIGEST_LENGTH);
7726 staged_blob_idp = &staged_blob_id;
7729 if (got_fileindex_entry_has_commit(ie)) {
7730 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
7731 commit_idp = &commit_id;
7734 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
7735 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
7738 const struct got_error *
7739 got_worktree_path_info(struct got_worktree *worktree,
7740 struct got_pathlist_head *paths,
7741 got_worktree_path_info_cb info_cb, void *info_arg,
7742 got_cancel_cb cancel_cb, void *cancel_arg)
7745 const struct got_error *err = NULL, *unlockerr;
7746 struct got_fileindex *fileindex = NULL;
7747 char *fileindex_path = NULL;
7748 struct report_file_info_arg arg;
7750 err = lock_worktree(worktree, LOCK_SH);
7754 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7758 arg.worktree = worktree;
7759 arg.info_cb = info_cb;
7760 arg.info_arg = info_arg;
7762 arg.cancel_cb = cancel_cb;
7763 arg.cancel_arg = cancel_arg;
7764 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
7767 free(fileindex_path);
7769 got_fileindex_free(fileindex);
7770 unlockerr = lock_worktree(worktree, LOCK_UN);
7771 if (unlockerr && err == NULL)