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"
58 #include "got_lib_gotconfig.h"
61 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
64 #define GOT_MERGE_LABEL_MERGED "merged change"
65 #define GOT_MERGE_LABEL_BASE "3-way merge base"
67 static const struct got_error *
68 create_meta_file(const char *path_got, const char *name, const char *content)
70 const struct got_error *err = NULL;
73 if (asprintf(&path, "%s/%s", path_got, name) == -1)
74 return got_error_from_errno("asprintf");
76 err = got_path_create_file(path, content);
81 static const struct got_error *
82 update_meta_file(const char *path_got, const char *name, const char *content)
84 const struct got_error *err = NULL;
89 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
90 err = got_error_from_errno("asprintf");
95 err = got_opentemp_named(&tmppath, &tmpfile, path);
100 int len = fprintf(tmpfile, "%s\n", content);
101 if (len != strlen(content) + 1) {
102 err = got_error_from_errno2("fprintf", tmppath);
107 if (rename(tmppath, path) != 0) {
108 err = got_error_from_errno3("rename", tmppath, path);
114 if (fclose(tmpfile) != 0 && err == NULL)
115 err = got_error_from_errno2("fclose", tmppath);
120 static const struct got_error *
121 read_meta_file(char **content, const char *path_got, const char *name)
123 const struct got_error *err = NULL;
131 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
132 err = got_error_from_errno("asprintf");
137 fd = open(path, O_RDONLY | O_NOFOLLOW);
140 err = got_error_path(path, GOT_ERR_WORKTREE_META);
142 err = got_error_from_errno2("open", path);
145 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
146 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
147 : got_error_from_errno2("flock", path));
151 if (fstat(fd, &sb) != 0) {
152 err = got_error_from_errno2("fstat", path);
155 *content = calloc(1, sb.st_size);
156 if (*content == NULL) {
157 err = got_error_from_errno("calloc");
161 n = read(fd, *content, sb.st_size);
162 if (n != sb.st_size) {
163 err = (n == -1 ? got_error_from_errno2("read", path) :
164 got_error_path(path, GOT_ERR_WORKTREE_META));
167 if ((*content)[sb.st_size - 1] != '\n') {
168 err = got_error_path(path, GOT_ERR_WORKTREE_META);
171 (*content)[sb.st_size - 1] = '\0';
174 if (fd != -1 && close(fd) == -1 && err == NULL)
175 err = got_error_from_errno2("close", path_got);
184 static const struct got_error *
185 write_head_ref(const char *path_got, struct got_reference *head_ref)
187 const struct got_error *err = NULL;
190 if (got_ref_is_symbolic(head_ref)) {
191 refstr = got_ref_to_str(head_ref);
193 return got_error_from_errno("got_ref_to_str");
195 refstr = strdup(got_ref_get_name(head_ref));
197 return got_error_from_errno("strdup");
199 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
204 const struct got_error *
205 got_worktree_init(const char *path, struct got_reference *head_ref,
206 const char *prefix, struct got_repository *repo)
208 const struct got_error *err = NULL;
209 struct got_object_id *commit_id = NULL;
211 uint32_t uuid_status;
213 char *path_got = NULL;
214 char *formatstr = NULL;
215 char *absprefix = NULL;
216 char *basestr = NULL;
217 char *uuidstr = NULL;
219 if (strcmp(path, got_repo_get_path(repo)) == 0) {
220 err = got_error(GOT_ERR_WORKTREE_REPO);
224 err = got_ref_resolve(&commit_id, repo, head_ref);
227 err = got_object_get_type(&obj_type, repo, commit_id);
230 if (obj_type != GOT_OBJ_TYPE_COMMIT)
231 return got_error(GOT_ERR_OBJ_TYPE);
233 if (!got_path_is_absolute(prefix)) {
234 if (asprintf(&absprefix, "/%s", prefix) == -1)
235 return got_error_from_errno("asprintf");
238 /* Create top-level directory (may already exist). */
239 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
240 err = got_error_from_errno2("mkdir", path);
244 /* Create .got directory (may already exist). */
245 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
246 err = got_error_from_errno("asprintf");
249 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
250 err = got_error_from_errno2("mkdir", path_got);
254 /* Create an empty lock file. */
255 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
259 /* Create an empty file index. */
260 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
264 /* Write the HEAD reference. */
265 err = write_head_ref(path_got, head_ref);
269 /* Record our base commit. */
270 err = got_object_id_str(&basestr, commit_id);
273 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
277 /* Store path to repository. */
278 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
279 got_repo_get_path(repo));
283 /* Store in-repository path prefix. */
284 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
285 absprefix ? absprefix : prefix);
290 uuid_create(&uuid, &uuid_status);
291 if (uuid_status != uuid_s_ok) {
292 err = got_error_uuid(uuid_status, "uuid_create");
295 uuid_to_string(&uuid, &uuidstr, &uuid_status);
296 if (uuid_status != uuid_s_ok) {
297 err = got_error_uuid(uuid_status, "uuid_to_string");
300 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
304 /* Stamp work tree with format file. */
305 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
306 err = got_error_from_errno("asprintf");
309 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
323 static const struct got_error *
324 open_worktree(struct got_worktree **worktree, const char *path)
326 const struct got_error *err = NULL;
328 char *formatstr = NULL;
329 char *uuidstr = NULL;
330 char *path_lock = NULL;
331 char *base_commit_id_str = NULL;
332 int version, fd = -1;
334 struct got_repository *repo = NULL;
335 uint32_t uuid_status;
339 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
340 err = got_error_from_errno("asprintf");
345 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
346 err = got_error_from_errno("asprintf");
351 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
353 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
354 : got_error_from_errno2("open", path_lock));
358 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
362 version = strtonum(formatstr, 1, INT_MAX, &errstr);
364 err = got_error_msg(GOT_ERR_WORKTREE_META,
365 "could not parse work tree format version number");
368 if (version != GOT_WORKTREE_FORMAT_VERSION) {
369 err = got_error(GOT_ERR_WORKTREE_VERS);
373 *worktree = calloc(1, sizeof(**worktree));
374 if (*worktree == NULL) {
375 err = got_error_from_errno("calloc");
378 (*worktree)->lockfd = -1;
380 (*worktree)->root_path = realpath(path, NULL);
381 if ((*worktree)->root_path == NULL) {
382 err = got_error_from_errno2("realpath", path);
385 err = read_meta_file(&(*worktree)->repo_path, path_got,
386 GOT_WORKTREE_REPOSITORY);
390 err = read_meta_file(&(*worktree)->path_prefix, path_got,
391 GOT_WORKTREE_PATH_PREFIX);
395 err = read_meta_file(&base_commit_id_str, path_got,
396 GOT_WORKTREE_BASE_COMMIT);
400 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
403 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
404 if (uuid_status != uuid_s_ok) {
405 err = got_error_uuid(uuid_status, "uuid_from_string");
409 err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
413 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
418 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
419 GOT_WORKTREE_HEAD_REF);
423 if (asprintf(&(*worktree)->gotconfig_path, "%s/%s/%s",
424 (*worktree)->root_path,
425 GOT_WORKTREE_GOT_DIR, GOT_GOTCONFIG_FILENAME) == -1) {
426 err = got_error_from_errno("asprintf");
430 err = got_gotconfig_read(&(*worktree)->gotconfig,
431 (*worktree)->gotconfig_path);
434 got_repo_close(repo);
437 free(base_commit_id_str);
443 if (*worktree != NULL)
444 got_worktree_close(*worktree);
447 (*worktree)->lockfd = fd;
452 const struct got_error *
453 got_worktree_open(struct got_worktree **worktree, const char *path)
455 const struct got_error *err = NULL;
458 worktree_path = strdup(path);
459 if (worktree_path == NULL)
460 return got_error_from_errno("strdup");
465 err = open_worktree(worktree, worktree_path);
466 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT)) {
474 if (worktree_path[0] == '/' && worktree_path[1] == '\0')
476 err = got_path_dirname(&parent_path, worktree_path);
478 if (err->code != GOT_ERR_BAD_PATH) {
485 worktree_path = parent_path;
489 return got_error(GOT_ERR_NOT_WORKTREE);
492 const struct got_error *
493 got_worktree_close(struct got_worktree *worktree)
495 const struct got_error *err = NULL;
496 free(worktree->repo_path);
497 free(worktree->path_prefix);
498 free(worktree->base_commit_id);
499 free(worktree->head_ref_name);
500 if (worktree->lockfd != -1)
501 if (close(worktree->lockfd) != 0)
502 err = got_error_from_errno2("close",
503 got_worktree_get_root_path(worktree));
504 free(worktree->root_path);
505 free(worktree->gotconfig_path);
506 got_gotconfig_free(worktree->gotconfig);
512 got_worktree_get_root_path(struct got_worktree *worktree)
514 return worktree->root_path;
518 got_worktree_get_repo_path(struct got_worktree *worktree)
520 return worktree->repo_path;
523 got_worktree_get_path_prefix(struct got_worktree *worktree)
525 return worktree->path_prefix;
528 const struct got_error *
529 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
530 const char *path_prefix)
532 char *absprefix = NULL;
534 if (!got_path_is_absolute(path_prefix)) {
535 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
536 return got_error_from_errno("asprintf");
538 *match = (strcmp(absprefix ? absprefix : path_prefix,
539 worktree->path_prefix) == 0);
545 got_worktree_get_head_ref_name(struct got_worktree *worktree)
547 return worktree->head_ref_name;
550 const struct got_error *
551 got_worktree_set_head_ref(struct got_worktree *worktree,
552 struct got_reference *head_ref)
554 const struct got_error *err = NULL;
555 char *path_got = NULL, *head_ref_name = NULL;
557 if (asprintf(&path_got, "%s/%s", worktree->root_path,
558 GOT_WORKTREE_GOT_DIR) == -1) {
559 err = got_error_from_errno("asprintf");
564 head_ref_name = strdup(got_ref_get_name(head_ref));
565 if (head_ref_name == NULL) {
566 err = got_error_from_errno("strdup");
570 err = write_head_ref(path_got, head_ref);
574 free(worktree->head_ref_name);
575 worktree->head_ref_name = head_ref_name;
583 struct got_object_id *
584 got_worktree_get_base_commit_id(struct got_worktree *worktree)
586 return worktree->base_commit_id;
589 const struct got_error *
590 got_worktree_set_base_commit_id(struct got_worktree *worktree,
591 struct got_repository *repo, struct got_object_id *commit_id)
593 const struct got_error *err;
594 struct got_object *obj = NULL;
596 char *path_got = NULL;
598 if (asprintf(&path_got, "%s/%s", worktree->root_path,
599 GOT_WORKTREE_GOT_DIR) == -1) {
600 err = got_error_from_errno("asprintf");
605 err = got_object_open(&obj, repo, commit_id);
609 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
610 err = got_error(GOT_ERR_OBJ_TYPE);
614 /* Record our base commit. */
615 err = got_object_id_str(&id_str, commit_id);
618 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
622 free(worktree->base_commit_id);
623 worktree->base_commit_id = got_object_id_dup(commit_id);
624 if (worktree->base_commit_id == NULL) {
625 err = got_error_from_errno("got_object_id_dup");
630 got_object_close(obj);
636 const struct got_gotconfig *
637 got_worktree_get_gotconfig(struct got_worktree *worktree)
639 return worktree->gotconfig;
642 static const struct got_error *
643 lock_worktree(struct got_worktree *worktree, int operation)
645 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
646 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
647 : got_error_from_errno2("flock",
648 got_worktree_get_root_path(worktree)));
652 static const struct got_error *
653 add_dir_on_disk(struct got_worktree *worktree, const char *path)
655 const struct got_error *err = NULL;
658 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
659 return got_error_from_errno("asprintf");
661 err = got_path_mkdir(abspath);
662 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
665 if (lstat(abspath, &sb) == -1) {
666 err = got_error_from_errno2("lstat", abspath);
667 } else if (!S_ISDIR(sb.st_mode)) {
668 /* TODO directory is obstructed; do something */
669 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
676 static const struct got_error *
677 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
679 const struct got_error *err = NULL;
682 size_t flen1 = 0, flen2 = 0;
687 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
688 if (flen1 == 0 && ferror(f1)) {
689 err = got_error_from_errno("fread");
692 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
693 if (flen2 == 0 && ferror(f2)) {
694 err = got_error_from_errno("fread");
701 } else if (flen2 == 0) {
705 } else if (flen1 == flen2) {
706 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
719 static const struct got_error *
720 check_files_equal(int *same, const char *f1_path, const char *f2_path)
722 const struct got_error *err = NULL;
725 FILE *f1 = NULL, *f2 = NULL;
729 if (lstat(f1_path, &sb) != 0) {
730 err = got_error_from_errno2("lstat", f1_path);
735 if (lstat(f2_path, &sb) != 0) {
736 err = got_error_from_errno2("lstat", f2_path);
741 if (size1 != size2) {
746 f1 = fopen(f1_path, "r");
748 return got_error_from_errno2("fopen", f1_path);
750 f2 = fopen(f2_path, "r");
752 err = got_error_from_errno2("fopen", f2_path);
756 err = check_file_contents_equal(same, f1, f2);
758 if (f1 && fclose(f1) != 0 && err == NULL)
759 err = got_error_from_errno("fclose");
760 if (f2 && fclose(f2) != 0 && err == NULL)
761 err = got_error_from_errno("fclose");
767 * Perform a 3-way merge where blob_orig acts as the common ancestor,
768 * the file at deriv_path acts as the first derived version, and the
769 * file on disk acts as the second derived version.
771 static const struct got_error *
772 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
773 struct got_blob_object *blob_orig, const char *ondisk_path,
774 const char *path, uint16_t st_mode, const char *deriv_path,
775 const char *label_orig, const char *label_deriv,
776 struct got_repository *repo,
777 got_worktree_checkout_cb progress_cb, void *progress_arg)
779 const struct got_error *err = NULL;
782 char *blob_orig_path = NULL;
783 char *merged_path = NULL, *base_path = NULL;
786 char *symlink_path = NULL;
787 FILE *symlinkf = NULL;
789 *local_changes_subsumed = 0;
791 parent = dirname(ondisk_path);
793 return got_error_from_errno2("dirname", ondisk_path);
795 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
796 return got_error_from_errno("asprintf");
798 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
803 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
804 err = got_error_from_errno("asprintf");
809 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
813 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
819 * If the file has no blob, this is an "add vs add" conflict,
820 * and we simply use an empty ancestor file to make both files
821 * appear in the merged result in their entirety.
826 * In order the run a 3-way merge with a symlink we copy the symlink's
827 * target path into a temporary file and use that file with diff3.
829 if (S_ISLNK(st_mode)) {
830 char target_path[PATH_MAX];
835 if (asprintf(&base_path, "%s/got-symlink-merge",
837 err = got_error_from_errno("asprintf");
841 err = got_opentemp_named(&symlink_path, &symlinkf, base_path);
844 target_len = readlink(ondisk_path, target_path,
845 sizeof(target_path));
846 if (target_len == -1) {
847 err = got_error_from_errno2("readlink", ondisk_path);
850 n = fwrite(target_path, 1, target_len, symlinkf);
851 if (n != target_len) {
852 err = got_ferror(symlinkf, GOT_ERR_IO);
855 if (fflush(symlinkf) == EOF) {
856 err = got_error_from_errno2("fflush", symlink_path);
861 err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
862 blob_orig_path, symlink_path ? symlink_path : ondisk_path,
863 label_deriv, label_orig, NULL);
867 err = (*progress_cb)(progress_arg,
868 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
872 if (fsync(merged_fd) != 0) {
873 err = got_error_from_errno("fsync");
877 /* Check if a clean merge has subsumed all local changes. */
878 if (overlapcnt == 0) {
879 err = check_files_equal(local_changes_subsumed, deriv_path,
885 if (fchmod(merged_fd, st_mode) != 0) {
886 err = got_error_from_errno2("fchmod", merged_path);
890 if (rename(merged_path, ondisk_path) != 0) {
891 err = got_error_from_errno3("rename", merged_path,
901 if (unlink(symlink_path) == -1 && err == NULL)
902 err = got_error_from_errno2("unlink", symlink_path);
904 if (symlinkf && fclose(symlinkf) == EOF && err == NULL)
905 err = got_error_from_errno2("fclose", symlink_path);
907 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
908 err = got_error_from_errno("close");
909 if (f_orig && fclose(f_orig) != 0 && err == NULL)
910 err = got_error_from_errno("fclose");
913 if (blob_orig_path) {
914 unlink(blob_orig_path);
915 free(blob_orig_path);
920 static const struct got_error *
921 update_symlink(const char *ondisk_path, const char *target_path,
924 /* This is not atomic but matches what 'ln -sf' does. */
925 if (unlink(ondisk_path) == -1)
926 return got_error_from_errno2("unlink", ondisk_path);
927 if (symlink(target_path, ondisk_path) == -1)
928 return got_error_from_errno3("symlink", target_path,
934 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
935 * in the work tree with a file that contains conflict markers and the
936 * conflicting target paths of the original version, a "derived version"
937 * of a symlink from an incoming change, and a local version of the symlink.
939 * The original versions's target path can be NULL if it is not available,
940 * such as if both derived versions added a new symlink at the same path.
942 * The incoming derived symlink target is NULL in case the incoming change
943 * has deleted this symlink.
945 static const struct got_error *
946 install_symlink_conflict(const char *deriv_target,
947 struct got_object_id *deriv_base_commit_id, const char *orig_target,
948 const char *label_orig, const char *local_target, const char *ondisk_path)
950 const struct got_error *err;
951 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
954 err = got_object_id_str(&id_str, deriv_base_commit_id);
956 return got_error_from_errno("asprintf");
958 if (asprintf(&label_deriv, "%s: commit %s",
959 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
960 err = got_error_from_errno("asprintf");
964 err = got_opentemp_named(&path, &f, "got-symlink-conflict");
968 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
969 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
970 deriv_target ? deriv_target : "(symlink was deleted)",
971 orig_target ? label_orig : "",
972 orig_target ? "\n" : "",
973 orig_target ? orig_target : "",
974 orig_target ? "\n" : "",
975 GOT_DIFF_CONFLICT_MARKER_SEP,
976 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
977 err = got_error_from_errno2("fprintf", path);
981 if (unlink(ondisk_path) == -1) {
982 err = got_error_from_errno2("unlink", ondisk_path);
985 if (rename(path, ondisk_path) == -1) {
986 err = got_error_from_errno3("rename", path, ondisk_path);
989 if (chmod(ondisk_path, GOT_DEFAULT_FILE_MODE) == -1) {
990 err = got_error_from_errno2("chmod", ondisk_path);
994 if (f != NULL && fclose(f) == EOF && err == NULL)
995 err = got_error_from_errno2("fclose", path);
1002 /* forward declaration */
1003 static const struct got_error *
1004 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
1005 const char *, const char *, uint16_t, const char *,
1006 struct got_blob_object *, struct got_object_id *,
1007 struct got_repository *, got_worktree_checkout_cb, void *);
1010 * Merge a symlink into the work tree, where blob_orig acts as the common
1011 * ancestor, deriv_target is the link target of the first derived version,
1012 * and the symlink on disk acts as the second derived version.
1013 * Assume that contents of both blobs represent symlinks.
1015 static const struct got_error *
1016 merge_symlink(struct got_worktree *worktree,
1017 struct got_blob_object *blob_orig, const char *ondisk_path,
1018 const char *path, const char *label_orig, const char *deriv_target,
1019 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1020 got_worktree_checkout_cb progress_cb, void *progress_arg)
1022 const struct got_error *err = NULL;
1023 char *ancestor_target = NULL;
1025 ssize_t ondisk_len, deriv_len;
1026 char ondisk_target[PATH_MAX];
1027 int have_local_change = 0;
1028 int have_incoming_change = 0;
1030 if (lstat(ondisk_path, &sb) == -1)
1031 return got_error_from_errno2("lstat", ondisk_path);
1033 ondisk_len = readlink(ondisk_path, ondisk_target,
1034 sizeof(ondisk_target));
1035 if (ondisk_len == -1) {
1036 err = got_error_from_errno2("readlink",
1040 ondisk_target[ondisk_len] = '\0';
1043 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
1048 if (ancestor_target == NULL ||
1049 (ondisk_len != strlen(ancestor_target) ||
1050 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
1051 have_local_change = 1;
1053 deriv_len = strlen(deriv_target);
1054 if (ancestor_target == NULL ||
1055 (deriv_len != strlen(ancestor_target) ||
1056 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
1057 have_incoming_change = 1;
1059 if (!have_local_change && !have_incoming_change) {
1060 if (ancestor_target) {
1061 /* Both sides made the same change. */
1062 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1064 } else if (deriv_len == ondisk_len &&
1065 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1066 /* Both sides added the same symlink. */
1067 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1070 /* Both sides added symlinks which don't match. */
1071 err = install_symlink_conflict(deriv_target,
1072 deriv_base_commit_id, ancestor_target,
1073 label_orig, ondisk_target, ondisk_path);
1076 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1079 } else if (!have_local_change && have_incoming_change) {
1080 /* Apply the incoming change. */
1081 err = update_symlink(ondisk_path, deriv_target,
1082 strlen(deriv_target));
1085 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1086 } else if (have_local_change && have_incoming_change) {
1087 if (deriv_len == ondisk_len &&
1088 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
1089 /* Both sides made the same change. */
1090 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1093 err = install_symlink_conflict(deriv_target,
1094 deriv_base_commit_id, ancestor_target, label_orig,
1095 ondisk_target, ondisk_path);
1098 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1104 free(ancestor_target);
1109 * Perform a 3-way merge where blob_orig acts as the common ancestor,
1110 * blob_deriv acts as the first derived version, and the file on disk
1111 * acts as the second derived version.
1113 static const struct got_error *
1114 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1115 struct got_blob_object *blob_orig, const char *ondisk_path,
1116 const char *path, uint16_t st_mode, const char *label_orig,
1117 struct got_blob_object *blob_deriv,
1118 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1119 got_worktree_checkout_cb progress_cb, void *progress_arg)
1121 const struct got_error *err = NULL;
1122 FILE *f_deriv = NULL;
1123 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1124 char *label_deriv = NULL, *parent;
1126 *local_changes_subsumed = 0;
1128 parent = dirname(ondisk_path);
1130 return got_error_from_errno2("dirname", ondisk_path);
1133 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1134 err = got_error_from_errno("asprintf");
1139 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1142 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1147 err = got_object_id_str(&id_str, deriv_base_commit_id);
1150 if (asprintf(&label_deriv, "%s: commit %s",
1151 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1152 err = got_error_from_errno("asprintf");
1156 err = merge_file(local_changes_subsumed, worktree, blob_orig,
1157 ondisk_path, path, st_mode, blob_deriv_path, label_orig,
1158 label_deriv, repo, progress_cb, progress_arg);
1160 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
1161 err = got_error_from_errno("fclose");
1163 if (blob_deriv_path) {
1164 unlink(blob_deriv_path);
1165 free(blob_deriv_path);
1172 static const struct got_error *
1173 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1174 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1175 const char *ondisk_path, const char *path, struct got_object_id *blob_id)
1177 const struct got_error *err = NULL;
1178 struct got_fileindex_entry *new_ie;
1182 err = got_fileindex_entry_alloc(&new_ie, path);
1186 err = got_fileindex_entry_update(new_ie, ondisk_path,
1187 blob_id->sha1, base_commit_id->sha1, 1);
1191 err = got_fileindex_entry_add(fileindex, new_ie);
1194 got_fileindex_entry_free(new_ie);
1201 get_ondisk_perms(int executable, mode_t st_mode)
1203 mode_t xbits = S_IXUSR;
1206 /* Map read bits to execute bits. */
1207 if (st_mode & S_IRGRP)
1209 if (st_mode & S_IROTH)
1211 return st_mode | xbits;
1214 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1217 /* forward declaration */
1218 static const struct got_error *
1219 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1220 const char *path, mode_t te_mode, mode_t st_mode,
1221 struct got_blob_object *blob, int restoring_missing_file,
1222 int reverting_versioned_file, int installing_bad_symlink,
1223 int path_is_unversioned, struct got_repository *repo,
1224 got_worktree_checkout_cb progress_cb, void *progress_arg);
1227 * This function assumes that the provided symlink target points at a
1228 * safe location in the work tree!
1230 static const struct got_error *
1231 replace_existing_symlink(const char *ondisk_path, const char *target_path,
1234 const struct got_error *err = NULL;
1236 char etarget[PATH_MAX];
1240 * "Bad" symlinks (those pointing outside the work tree or into the
1241 * .got directory) are installed in the work tree as a regular file
1242 * which contains the bad symlink target path.
1243 * The new symlink target has already been checked for safety by our
1244 * caller. If we can successfully open a regular file then we simply
1245 * replace this file with a symlink below.
1247 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1250 return got_error_from_errno2("open", ondisk_path);
1252 /* We are updating an existing on-disk symlink. */
1253 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1255 return got_error_from_errno2("readlink", ondisk_path);
1257 if (elen == target_len &&
1258 memcmp(etarget, target_path, target_len) == 0)
1259 return NULL; /* nothing to do */
1262 err = update_symlink(ondisk_path, target_path, target_len);
1263 if (fd != -1 && close(fd) == -1 && err == NULL)
1264 err = got_error_from_errno2("close", ondisk_path);
1268 static const struct got_error *
1269 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1270 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1272 const struct got_error *err = NULL;
1273 char canonpath[PATH_MAX];
1274 char *path_got = NULL;
1276 *is_bad_symlink = 0;
1278 if (target_len >= sizeof(canonpath)) {
1279 *is_bad_symlink = 1;
1284 * We do not use realpath(3) to resolve the symlink's target
1285 * path because we don't want to resolve symlinks recursively.
1286 * Instead we make the path absolute and then canonicalize it.
1287 * Relative symlink target lookup should begin at the directory
1288 * in which the blob object is being installed.
1290 if (!got_path_is_absolute(target_path)) {
1292 char *parent = dirname(ondisk_path);
1294 return got_error_from_errno2("dirname", ondisk_path);
1295 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1)
1296 return got_error_from_errno("asprintf");
1297 if (strlen(abspath) >= sizeof(canonpath)) {
1298 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1302 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1307 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1312 /* Only allow symlinks pointing at paths within the work tree. */
1313 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1314 *is_bad_symlink = 1;
1318 /* Do not allow symlinks pointing into the .got directory. */
1319 if (asprintf(&path_got, "%s/%s", wtroot_path,
1320 GOT_WORKTREE_GOT_DIR) == -1)
1321 return got_error_from_errno("asprintf");
1322 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1323 *is_bad_symlink = 1;
1329 static const struct got_error *
1330 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1331 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1332 int restoring_missing_file, int reverting_versioned_file,
1333 int path_is_unversioned, struct got_repository *repo,
1334 got_worktree_checkout_cb progress_cb, void *progress_arg)
1336 const struct got_error *err = NULL;
1337 char target_path[PATH_MAX];
1338 size_t len, target_len = 0;
1339 char *path_got = NULL;
1340 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1341 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1343 *is_bad_symlink = 0;
1346 * Blob object content specifies the target path of the link.
1347 * If a symbolic link cannot be installed we instead create
1348 * a regular file which contains the link target path stored
1349 * in the blob object.
1352 err = got_object_blob_read_block(&len, blob);
1353 if (len + target_len >= sizeof(target_path)) {
1354 /* Path too long; install as a regular file. */
1355 *is_bad_symlink = 1;
1356 got_object_blob_rewind(blob);
1357 return install_blob(worktree, ondisk_path, path,
1358 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1359 restoring_missing_file, reverting_versioned_file,
1360 1, path_is_unversioned, repo, progress_cb,
1364 /* Skip blob object header first time around. */
1365 memcpy(target_path + target_len, buf + hdrlen,
1367 target_len += len - hdrlen;
1371 target_path[target_len] = '\0';
1373 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1374 ondisk_path, worktree->root_path);
1378 if (*is_bad_symlink) {
1379 /* install as a regular file */
1380 *is_bad_symlink = 1;
1381 got_object_blob_rewind(blob);
1382 err = install_blob(worktree, ondisk_path, path,
1383 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1384 restoring_missing_file, reverting_versioned_file, 1,
1385 path_is_unversioned, repo, progress_cb, progress_arg);
1389 if (symlink(target_path, ondisk_path) == -1) {
1390 if (errno == EEXIST) {
1391 if (path_is_unversioned) {
1392 err = (*progress_cb)(progress_arg,
1393 GOT_STATUS_UNVERSIONED, path);
1396 err = replace_existing_symlink(ondisk_path,
1397 target_path, target_len);
1401 err = (*progress_cb)(progress_arg,
1402 reverting_versioned_file ?
1403 GOT_STATUS_REVERT : GOT_STATUS_UPDATE,
1406 goto done; /* Nothing else to do. */
1409 if (errno == ENOENT) {
1410 char *parent = dirname(ondisk_path);
1411 if (parent == NULL) {
1412 err = got_error_from_errno2("dirname",
1416 err = add_dir_on_disk(worktree, parent);
1420 * Retry, and fall through to error handling
1421 * below if this second attempt fails.
1423 if (symlink(target_path, ondisk_path) != -1) {
1424 err = NULL; /* success */
1429 /* Handle errors from first or second creation attempt. */
1430 if (errno == ENAMETOOLONG) {
1431 /* bad target path; install as a regular file */
1432 *is_bad_symlink = 1;
1433 got_object_blob_rewind(blob);
1434 err = install_blob(worktree, ondisk_path, path,
1435 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1436 restoring_missing_file, reverting_versioned_file, 1,
1437 path_is_unversioned, repo,
1438 progress_cb, progress_arg);
1439 } else if (errno == ENOTDIR) {
1440 err = got_error_path(ondisk_path,
1441 GOT_ERR_FILE_OBSTRUCTED);
1443 err = got_error_from_errno3("symlink",
1444 target_path, ondisk_path);
1446 } else if (progress_cb)
1447 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1448 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1454 static const struct got_error *
1455 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1456 const char *path, mode_t te_mode, mode_t st_mode,
1457 struct got_blob_object *blob, int restoring_missing_file,
1458 int reverting_versioned_file, int installing_bad_symlink,
1459 int path_is_unversioned, struct got_repository *repo,
1460 got_worktree_checkout_cb progress_cb, void *progress_arg)
1462 const struct got_error *err = NULL;
1466 char *tmppath = NULL;
1468 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1469 GOT_DEFAULT_FILE_MODE);
1471 if (errno == ENOENT) {
1472 char *parent = dirname(path);
1474 return got_error_from_errno2("dirname", path);
1475 err = add_dir_on_disk(worktree, parent);
1478 fd = open(ondisk_path,
1479 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1480 GOT_DEFAULT_FILE_MODE);
1482 return got_error_from_errno2("open",
1484 } else if (errno == EEXIST) {
1485 if (path_is_unversioned) {
1486 err = (*progress_cb)(progress_arg,
1487 GOT_STATUS_UNVERSIONED, path);
1490 if (!S_ISREG(st_mode) && !installing_bad_symlink) {
1491 /* TODO file is obstructed; do something */
1492 err = got_error_path(ondisk_path,
1493 GOT_ERR_FILE_OBSTRUCTED);
1496 err = got_opentemp_named_fd(&tmppath, &fd,
1503 return got_error_from_errno2("open", ondisk_path);
1507 if (restoring_missing_file)
1508 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1510 else if (reverting_versioned_file)
1511 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1514 err = (*progress_cb)(progress_arg,
1515 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1520 hdrlen = got_object_blob_get_hdrlen(blob);
1522 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1523 err = got_object_blob_read_block(&len, blob);
1527 /* Skip blob object header first time around. */
1528 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1530 err = got_error_from_errno("write");
1532 } else if (outlen != len - hdrlen) {
1533 err = got_error(GOT_ERR_IO);
1540 if (fsync(fd) != 0) {
1541 err = got_error_from_errno("fsync");
1546 if (rename(tmppath, ondisk_path) != 0) {
1547 err = got_error_from_errno3("rename", tmppath,
1554 if (chmod(ondisk_path,
1555 get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1556 err = got_error_from_errno2("chmod", ondisk_path);
1561 if (fd != -1 && close(fd) != 0 && err == NULL)
1562 err = got_error_from_errno("close");
1567 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1568 static const struct got_error *
1569 get_modified_file_content_status(unsigned char *status, FILE *f)
1571 const struct got_error *err = NULL;
1572 const char *markers[3] = {
1573 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1574 GOT_DIFF_CONFLICT_MARKER_SEP,
1575 GOT_DIFF_CONFLICT_MARKER_END
1580 const char delim[3] = {'\0', '\0', '\0'};
1582 while (*status == GOT_STATUS_MODIFY) {
1583 line = fparseln(f, &len, NULL, delim, 0);
1587 err = got_ferror(f, GOT_ERR_IO);
1591 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1592 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1594 *status = GOT_STATUS_CONFLICT;
1604 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1606 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1607 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1611 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1613 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1614 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1615 ie->mtime_sec == sb->st_mtim.tv_sec &&
1616 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1617 ie->size == (sb->st_size & 0xffffffff) &&
1618 !xbit_differs(ie, sb->st_mode));
1621 static unsigned char
1622 get_staged_status(struct got_fileindex_entry *ie)
1624 switch (got_fileindex_entry_stage_get(ie)) {
1625 case GOT_FILEIDX_STAGE_ADD:
1626 return GOT_STATUS_ADD;
1627 case GOT_FILEIDX_STAGE_DELETE:
1628 return GOT_STATUS_DELETE;
1629 case GOT_FILEIDX_STAGE_MODIFY:
1630 return GOT_STATUS_MODIFY;
1632 return GOT_STATUS_NO_CHANGE;
1636 static const struct got_error *
1637 get_symlink_modification_status(unsigned char *status,
1638 struct got_fileindex_entry *ie, const char *abspath,
1639 int dirfd, const char *de_name, struct got_blob_object *blob)
1641 const struct got_error *err = NULL;
1642 char target_path[PATH_MAX];
1643 char etarget[PATH_MAX];
1645 size_t len, target_len = 0;
1646 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1647 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1649 *status = GOT_STATUS_NO_CHANGE;
1651 /* Blob object content specifies the target path of the link. */
1653 err = got_object_blob_read_block(&len, blob);
1656 if (len + target_len >= sizeof(target_path)) {
1658 * Should not happen. The blob contents were OK
1659 * when this symlink was installed.
1661 return got_error(GOT_ERR_NO_SPACE);
1664 /* Skip blob object header first time around. */
1665 memcpy(target_path + target_len, buf + hdrlen,
1667 target_len += len - hdrlen;
1671 target_path[target_len] = '\0';
1674 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1676 return got_error_from_errno2("readlinkat", abspath);
1678 elen = readlink(abspath, etarget, sizeof(etarget));
1680 return got_error_from_errno2("readlink", abspath);
1683 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1684 *status = GOT_STATUS_MODIFY;
1689 static const struct got_error *
1690 get_file_status(unsigned char *status, struct stat *sb,
1691 struct got_fileindex_entry *ie, const char *abspath,
1692 int dirfd, const char *de_name, struct got_repository *repo)
1694 const struct got_error *err = NULL;
1695 struct got_object_id id;
1700 struct got_blob_object *blob = NULL;
1702 unsigned char staged_status = get_staged_status(ie);
1704 *status = GOT_STATUS_NO_CHANGE;
1707 * Whenever the caller provides a directory descriptor and a
1708 * directory entry name for the file, use them! This prevents
1709 * race conditions if filesystem paths change beneath our feet.
1712 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1713 if (errno == ENOENT) {
1714 if (got_fileindex_entry_has_file_on_disk(ie))
1715 *status = GOT_STATUS_MISSING;
1717 *status = GOT_STATUS_DELETE;
1720 err = got_error_from_errno2("fstatat", abspath);
1724 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1725 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1726 return got_error_from_errno2("open", abspath);
1727 else if (fd == -1 && errno == ELOOP) {
1728 if (lstat(abspath, sb) == -1)
1729 return got_error_from_errno2("lstat", abspath);
1730 } else if (fd == -1 || fstat(fd, sb) == -1) {
1731 if (errno == ENOENT) {
1732 if (got_fileindex_entry_has_file_on_disk(ie))
1733 *status = GOT_STATUS_MISSING;
1735 *status = GOT_STATUS_DELETE;
1738 err = got_error_from_errno2("fstat", abspath);
1743 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1744 *status = GOT_STATUS_OBSTRUCTED;
1748 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1749 *status = GOT_STATUS_DELETE;
1751 } else if (!got_fileindex_entry_has_blob(ie) &&
1752 staged_status != GOT_STATUS_ADD) {
1753 *status = GOT_STATUS_ADD;
1757 if (!stat_info_differs(ie, sb))
1760 if (S_ISLNK(sb->st_mode) &&
1761 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1762 *status = GOT_STATUS_MODIFY;
1766 if (staged_status == GOT_STATUS_MODIFY ||
1767 staged_status == GOT_STATUS_ADD)
1768 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1770 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1772 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1776 if (S_ISLNK(sb->st_mode)) {
1777 err = get_symlink_modification_status(status, ie,
1778 abspath, dirfd, de_name, blob);
1783 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1785 err = got_error_from_errno2("openat", abspath);
1790 f = fdopen(fd, "r");
1792 err = got_error_from_errno2("fdopen", abspath);
1796 hdrlen = got_object_blob_get_hdrlen(blob);
1798 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1799 err = got_object_blob_read_block(&blen, blob);
1802 /* Skip length of blob object header first time around. */
1803 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1804 if (flen == 0 && ferror(f)) {
1805 err = got_error_from_errno("fread");
1808 if (blen - hdrlen == 0) {
1810 *status = GOT_STATUS_MODIFY;
1812 } else if (flen == 0) {
1813 if (blen - hdrlen != 0)
1814 *status = GOT_STATUS_MODIFY;
1816 } else if (blen - hdrlen == flen) {
1817 /* Skip blob object header first time around. */
1818 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1819 *status = GOT_STATUS_MODIFY;
1823 *status = GOT_STATUS_MODIFY;
1829 if (*status == GOT_STATUS_MODIFY) {
1831 err = get_modified_file_content_status(status, f);
1832 } else if (xbit_differs(ie, sb->st_mode))
1833 *status = GOT_STATUS_MODE_CHANGE;
1836 got_object_blob_close(blob);
1837 if (f != NULL && fclose(f) == EOF && err == NULL)
1838 err = got_error_from_errno2("fclose", abspath);
1839 if (fd != -1 && close(fd) == -1 && err == NULL)
1840 err = got_error_from_errno2("close", abspath);
1845 * Update timestamps in the file index if a file is unmodified and
1846 * we had to run a full content comparison to find out.
1848 static const struct got_error *
1849 sync_timestamps(char *ondisk_path, unsigned char status,
1850 struct got_fileindex_entry *ie, struct stat *sb)
1852 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1853 return got_fileindex_entry_update(ie, ondisk_path,
1854 ie->blob_sha1, ie->commit_sha1, 1);
1859 static const struct got_error *
1860 update_blob(struct got_worktree *worktree,
1861 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1862 struct got_tree_entry *te, const char *path,
1863 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1866 const struct got_error *err = NULL;
1867 struct got_blob_object *blob = NULL;
1869 unsigned char status = GOT_STATUS_NO_CHANGE;
1872 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1873 return got_error_from_errno("asprintf");
1876 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1877 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1880 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1884 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1885 sb.st_mode = got_fileindex_perms_to_st(ie);
1887 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1888 status = GOT_STATUS_UNVERSIONED;
1891 if (status == GOT_STATUS_OBSTRUCTED) {
1892 err = (*progress_cb)(progress_arg, status, path);
1895 if (status == GOT_STATUS_CONFLICT) {
1896 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1901 if (ie && status != GOT_STATUS_MISSING &&
1902 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1903 if (got_fileindex_entry_has_commit(ie) &&
1904 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1905 SHA1_DIGEST_LENGTH) == 0) {
1906 err = sync_timestamps(ondisk_path, status, ie, &sb);
1909 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1913 if (got_fileindex_entry_has_blob(ie) &&
1914 memcmp(ie->blob_sha1, te->id.sha1,
1915 SHA1_DIGEST_LENGTH) == 0) {
1916 err = sync_timestamps(ondisk_path, status, ie, &sb);
1921 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1925 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1926 int update_timestamps;
1927 struct got_blob_object *blob2 = NULL;
1928 char *label_orig = NULL;
1929 if (got_fileindex_entry_has_blob(ie)) {
1930 struct got_object_id id2;
1931 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1932 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1936 if (got_fileindex_entry_has_commit(ie)) {
1937 char id_str[SHA1_DIGEST_STRING_LENGTH];
1938 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1939 sizeof(id_str)) == NULL) {
1940 err = got_error_path(id_str,
1941 GOT_ERR_BAD_OBJ_ID_STR);
1944 if (asprintf(&label_orig, "%s: commit %s",
1945 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1946 err = got_error_from_errno("asprintf");
1950 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1952 err = got_object_blob_read_to_str(&link_target, blob);
1955 err = merge_symlink(worktree, blob2, ondisk_path, path,
1956 label_orig, link_target, worktree->base_commit_id,
1957 repo, progress_cb, progress_arg);
1960 err = merge_blob(&update_timestamps, worktree, blob2,
1961 ondisk_path, path, sb.st_mode, label_orig, blob,
1962 worktree->base_commit_id, repo,
1963 progress_cb, progress_arg);
1967 got_object_blob_close(blob2);
1971 * Do not update timestamps of files with local changes.
1972 * Otherwise, a future status walk would treat them as
1973 * unmodified files again.
1975 err = got_fileindex_entry_update(ie, ondisk_path,
1976 blob->id.sha1, worktree->base_commit_id->sha1,
1978 } else if (status == GOT_STATUS_MODE_CHANGE) {
1979 err = got_fileindex_entry_update(ie, ondisk_path,
1980 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1981 } else if (status == GOT_STATUS_DELETE) {
1982 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1985 err = got_fileindex_entry_update(ie, ondisk_path,
1986 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1990 int is_bad_symlink = 0;
1991 if (S_ISLNK(te->mode)) {
1992 err = install_symlink(&is_bad_symlink, worktree,
1993 ondisk_path, path, blob,
1994 status == GOT_STATUS_MISSING, 0,
1995 status == GOT_STATUS_UNVERSIONED, repo,
1996 progress_cb, progress_arg);
1998 err = install_blob(worktree, ondisk_path, path,
1999 te->mode, sb.st_mode, blob,
2000 status == GOT_STATUS_MISSING, 0, 0,
2001 status == GOT_STATUS_UNVERSIONED, repo,
2002 progress_cb, progress_arg);
2008 err = got_fileindex_entry_update(ie, ondisk_path,
2009 blob->id.sha1, worktree->base_commit_id->sha1, 1);
2011 err = create_fileindex_entry(&ie, fileindex,
2012 worktree->base_commit_id, ondisk_path, path,
2018 if (is_bad_symlink) {
2019 got_fileindex_entry_filetype_set(ie,
2020 GOT_FILEIDX_MODE_BAD_SYMLINK);
2023 got_object_blob_close(blob);
2029 static const struct got_error *
2030 remove_ondisk_file(const char *root_path, const char *path)
2032 const struct got_error *err = NULL;
2033 char *ondisk_path = NULL;
2035 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2036 return got_error_from_errno("asprintf");
2038 if (unlink(ondisk_path) == -1) {
2039 if (errno != ENOENT)
2040 err = got_error_from_errno2("unlink", ondisk_path);
2042 char *parent = dirname(ondisk_path);
2043 while (parent && strcmp(parent, root_path) != 0) {
2044 if (rmdir(parent) == -1) {
2045 if (errno != ENOTEMPTY)
2046 err = got_error_from_errno2("rmdir",
2050 parent = dirname(parent);
2057 static const struct got_error *
2058 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2059 struct got_fileindex_entry *ie, struct got_repository *repo,
2060 got_worktree_checkout_cb progress_cb, void *progress_arg)
2062 const struct got_error *err = NULL;
2063 unsigned char status;
2067 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2068 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2070 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2072 return got_error_from_errno("asprintf");
2074 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2078 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2079 char ondisk_target[PATH_MAX];
2080 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2081 sizeof(ondisk_target));
2082 if (ondisk_len == -1) {
2083 err = got_error_from_errno2("readlink", ondisk_path);
2086 ondisk_target[ondisk_len] = '\0';
2087 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2088 NULL, NULL, /* XXX pass common ancestor info? */
2089 ondisk_target, ondisk_path);
2092 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2097 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2098 status == GOT_STATUS_ADD) {
2099 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2103 * Preserve the working file and change the deleted blob's
2104 * entry into a schedule-add entry.
2106 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
2109 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2112 if (status == GOT_STATUS_NO_CHANGE) {
2113 err = remove_ondisk_file(worktree->root_path, ie->path);
2117 got_fileindex_entry_remove(fileindex, ie);
2124 struct diff_cb_arg {
2125 struct got_fileindex *fileindex;
2126 struct got_worktree *worktree;
2127 struct got_repository *repo;
2128 got_worktree_checkout_cb progress_cb;
2130 got_cancel_cb cancel_cb;
2134 static const struct got_error *
2135 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2136 struct got_tree_entry *te, const char *parent_path)
2138 struct diff_cb_arg *a = arg;
2140 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2141 return got_error(GOT_ERR_CANCELLED);
2143 return update_blob(a->worktree, a->fileindex, ie, te,
2144 ie->path, a->repo, a->progress_cb, a->progress_arg);
2147 static const struct got_error *
2148 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2150 struct diff_cb_arg *a = arg;
2152 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2153 return got_error(GOT_ERR_CANCELLED);
2155 return delete_blob(a->worktree, a->fileindex, ie,
2156 a->repo, a->progress_cb, a->progress_arg);
2159 static const struct got_error *
2160 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2162 struct diff_cb_arg *a = arg;
2163 const struct got_error *err;
2166 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2167 return got_error(GOT_ERR_CANCELLED);
2169 if (got_object_tree_entry_is_submodule(te))
2172 if (asprintf(&path, "%s%s%s", parent_path,
2173 parent_path[0] ? "/" : "", te->name)
2175 return got_error_from_errno("asprintf");
2177 if (S_ISDIR(te->mode))
2178 err = add_dir_on_disk(a->worktree, path);
2180 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2181 a->repo, a->progress_cb, a->progress_arg);
2187 const struct got_error *
2188 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2190 uint32_t uuid_status;
2192 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2193 if (uuid_status != uuid_s_ok) {
2195 return got_error_uuid(uuid_status, "uuid_to_string");
2201 static const struct got_error *
2202 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2204 const struct got_error *err = NULL;
2205 char *uuidstr = NULL;
2209 err = got_worktree_get_uuid(&uuidstr, worktree);
2213 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2214 err = got_error_from_errno("asprintf");
2221 const struct got_error *
2222 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2224 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2227 static const struct got_error *
2228 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2230 return get_ref_name(refname, worktree,
2231 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2234 static const struct got_error *
2235 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2237 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2240 static const struct got_error *
2241 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2243 return get_ref_name(refname, worktree,
2244 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2247 static const struct got_error *
2248 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2250 return get_ref_name(refname, worktree,
2251 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2254 static const struct got_error *
2255 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2257 return get_ref_name(refname, worktree,
2258 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2261 static const struct got_error *
2262 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2264 return get_ref_name(refname, worktree,
2265 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2268 static const struct got_error *
2269 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2271 return get_ref_name(refname, worktree,
2272 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2275 static const struct got_error *
2276 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2278 return get_ref_name(refname, worktree,
2279 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2282 const struct got_error *
2283 got_worktree_get_histedit_script_path(char **path,
2284 struct got_worktree *worktree)
2286 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2287 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2289 return got_error_from_errno("asprintf");
2295 * Prevent Git's garbage collector from deleting our base commit by
2296 * setting a reference to our base commit's ID.
2298 static const struct got_error *
2299 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2301 const struct got_error *err = NULL;
2302 struct got_reference *ref = NULL;
2305 err = got_worktree_get_base_ref_name(&refname, worktree);
2309 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2313 err = got_ref_write(ref, repo);
2321 static const struct got_error *
2322 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2324 const struct got_error *err = NULL;
2326 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2327 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2328 err = got_error_from_errno("asprintf");
2329 *fileindex_path = NULL;
2335 static const struct got_error *
2336 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2337 struct got_worktree *worktree)
2339 const struct got_error *err = NULL;
2342 *fileindex_path = NULL;
2343 *fileindex = got_fileindex_alloc();
2344 if (*fileindex == NULL)
2345 return got_error_from_errno("got_fileindex_alloc");
2347 err = get_fileindex_path(fileindex_path, worktree);
2351 index = fopen(*fileindex_path, "rb");
2352 if (index == NULL) {
2353 if (errno != ENOENT)
2354 err = got_error_from_errno2("fopen", *fileindex_path);
2356 err = got_fileindex_read(*fileindex, index);
2357 if (fclose(index) != 0 && err == NULL)
2358 err = got_error_from_errno("fclose");
2362 free(*fileindex_path);
2363 *fileindex_path = NULL;
2364 got_fileindex_free(*fileindex);
2370 struct bump_base_commit_id_arg {
2371 struct got_object_id *base_commit_id;
2374 const char *entry_name;
2375 got_worktree_checkout_cb progress_cb;
2379 /* Bump base commit ID of all files within an updated part of the work tree. */
2380 static const struct got_error *
2381 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2383 const struct got_error *err;
2384 struct bump_base_commit_id_arg *a = arg;
2386 if (a->entry_name) {
2387 if (strcmp(ie->path, a->path) != 0)
2389 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2392 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2393 SHA1_DIGEST_LENGTH) == 0)
2396 if (a->progress_cb) {
2397 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2402 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2406 static const struct got_error *
2407 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2409 const struct got_error *err = NULL;
2410 char *new_fileindex_path = NULL;
2411 FILE *new_index = NULL;
2412 struct timespec timeout;
2414 err = got_opentemp_named(&new_fileindex_path, &new_index,
2419 err = got_fileindex_write(fileindex, new_index);
2423 if (rename(new_fileindex_path, fileindex_path) != 0) {
2424 err = got_error_from_errno3("rename", new_fileindex_path,
2426 unlink(new_fileindex_path);
2430 * Sleep for a short amount of time to ensure that files modified after
2431 * this program exits have a different time stamp from the one which
2432 * was recorded in the file index.
2435 timeout.tv_nsec = 1;
2436 nanosleep(&timeout, NULL);
2440 free(new_fileindex_path);
2444 static const struct got_error *
2445 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2446 struct got_object_id **tree_id, const char *wt_relpath,
2447 struct got_worktree *worktree, struct got_repository *repo)
2449 const struct got_error *err = NULL;
2450 struct got_object_id *id = NULL;
2451 char *in_repo_path = NULL;
2452 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2454 *entry_type = GOT_OBJ_TYPE_ANY;
2455 *tree_relpath = NULL;
2458 if (wt_relpath[0] == '\0') {
2459 /* Check out all files within the work tree. */
2460 *entry_type = GOT_OBJ_TYPE_TREE;
2461 *tree_relpath = strdup("");
2462 if (*tree_relpath == NULL) {
2463 err = got_error_from_errno("strdup");
2466 err = got_object_id_by_path(tree_id, repo,
2467 worktree->base_commit_id, worktree->path_prefix);
2473 /* Check out a subset of files in the work tree. */
2475 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2476 is_root_wt ? "" : "/", wt_relpath) == -1) {
2477 err = got_error_from_errno("asprintf");
2481 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2487 in_repo_path = NULL;
2489 err = got_object_get_type(entry_type, repo, id);
2493 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2494 /* Check out a single file. */
2495 if (strchr(wt_relpath, '/') == NULL) {
2496 /* Check out a single file in work tree's root dir. */
2497 in_repo_path = strdup(worktree->path_prefix);
2498 if (in_repo_path == NULL) {
2499 err = got_error_from_errno("strdup");
2502 *tree_relpath = strdup("");
2503 if (*tree_relpath == NULL) {
2504 err = got_error_from_errno("strdup");
2508 /* Check out a single file in a subdirectory. */
2509 err = got_path_dirname(tree_relpath, wt_relpath);
2512 if (asprintf(&in_repo_path, "%s%s%s",
2513 worktree->path_prefix, is_root_wt ? "" : "/",
2514 *tree_relpath) == -1) {
2515 err = got_error_from_errno("asprintf");
2519 err = got_object_id_by_path(tree_id, repo,
2520 worktree->base_commit_id, in_repo_path);
2522 /* Check out all files within a subdirectory. */
2523 *tree_id = got_object_id_dup(id);
2524 if (*tree_id == NULL) {
2525 err = got_error_from_errno("got_object_id_dup");
2528 *tree_relpath = strdup(wt_relpath);
2529 if (*tree_relpath == NULL) {
2530 err = got_error_from_errno("strdup");
2538 *entry_type = GOT_OBJ_TYPE_ANY;
2539 free(*tree_relpath);
2540 *tree_relpath = NULL;
2547 static const struct got_error *
2548 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2549 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2550 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2551 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2553 const struct got_error *err = NULL;
2554 struct got_commit_object *commit = NULL;
2555 struct got_tree_object *tree = NULL;
2556 struct got_fileindex_diff_tree_cb diff_cb;
2557 struct diff_cb_arg arg;
2559 err = ref_base_commit(worktree, repo);
2561 if (!(err->code == GOT_ERR_ERRNO &&
2562 (errno == EACCES || errno == EROFS)))
2564 err = (*progress_cb)(progress_arg,
2565 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2570 err = got_object_open_as_commit(&commit, repo,
2571 worktree->base_commit_id);
2575 err = got_object_open_as_tree(&tree, repo, tree_id);
2580 got_object_tree_find_entry(tree, entry_name) == NULL) {
2581 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2585 diff_cb.diff_old_new = diff_old_new;
2586 diff_cb.diff_old = diff_old;
2587 diff_cb.diff_new = diff_new;
2588 arg.fileindex = fileindex;
2589 arg.worktree = worktree;
2591 arg.progress_cb = progress_cb;
2592 arg.progress_arg = progress_arg;
2593 arg.cancel_cb = cancel_cb;
2594 arg.cancel_arg = cancel_arg;
2595 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2596 entry_name, repo, &diff_cb, &arg);
2599 got_object_tree_close(tree);
2601 got_object_commit_close(commit);
2605 const struct got_error *
2606 got_worktree_checkout_files(struct got_worktree *worktree,
2607 struct got_pathlist_head *paths, struct got_repository *repo,
2608 got_worktree_checkout_cb progress_cb, void *progress_arg,
2609 got_cancel_cb cancel_cb, void *cancel_arg)
2611 const struct got_error *err = NULL, *sync_err, *unlockerr;
2612 struct got_commit_object *commit = NULL;
2613 struct got_tree_object *tree = NULL;
2614 struct got_fileindex *fileindex = NULL;
2615 char *fileindex_path = NULL;
2616 struct got_pathlist_entry *pe;
2617 struct tree_path_data {
2618 SIMPLEQ_ENTRY(tree_path_data) entry;
2619 struct got_object_id *tree_id;
2624 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2626 SIMPLEQ_INIT(&tree_paths);
2628 err = lock_worktree(worktree, LOCK_EX);
2632 /* Map all specified paths to in-repository trees. */
2633 TAILQ_FOREACH(pe, paths, entry) {
2634 tpd = malloc(sizeof(*tpd));
2636 err = got_error_from_errno("malloc");
2640 err = find_tree_entry_for_checkout(&tpd->entry_type,
2641 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2647 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2648 err = got_path_basename(&tpd->entry_name, pe->path);
2656 tpd->entry_name = NULL;
2658 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2662 * Read the file index.
2663 * Checking out files is supposed to be an idempotent operation.
2664 * If the on-disk file index is incomplete we will try to complete it.
2666 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2670 tpd = SIMPLEQ_FIRST(&tree_paths);
2671 TAILQ_FOREACH(pe, paths, entry) {
2672 struct bump_base_commit_id_arg bbc_arg;
2674 err = checkout_files(worktree, fileindex, tpd->relpath,
2675 tpd->tree_id, tpd->entry_name, repo,
2676 progress_cb, progress_arg, cancel_cb, cancel_arg);
2680 bbc_arg.base_commit_id = worktree->base_commit_id;
2681 bbc_arg.entry_name = tpd->entry_name;
2682 bbc_arg.path = pe->path;
2683 bbc_arg.path_len = pe->path_len;
2684 bbc_arg.progress_cb = progress_cb;
2685 bbc_arg.progress_arg = progress_arg;
2686 err = got_fileindex_for_each_entry_safe(fileindex,
2687 bump_base_commit_id, &bbc_arg);
2691 tpd = SIMPLEQ_NEXT(tpd, entry);
2693 sync_err = sync_fileindex(fileindex, fileindex_path);
2694 if (sync_err && err == NULL)
2697 free(fileindex_path);
2699 got_object_tree_close(tree);
2701 got_object_commit_close(commit);
2703 got_fileindex_free(fileindex);
2704 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2705 tpd = SIMPLEQ_FIRST(&tree_paths);
2706 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2711 unlockerr = lock_worktree(worktree, LOCK_SH);
2712 if (unlockerr && err == NULL)
2717 struct merge_file_cb_arg {
2718 struct got_worktree *worktree;
2719 struct got_fileindex *fileindex;
2720 got_worktree_checkout_cb progress_cb;
2722 got_cancel_cb cancel_cb;
2724 const char *label_orig;
2725 struct got_object_id *commit_id2;
2728 static const struct got_error *
2729 merge_file_cb(void *arg, struct got_blob_object *blob1,
2730 struct got_blob_object *blob2, struct got_object_id *id1,
2731 struct got_object_id *id2, const char *path1, const char *path2,
2732 mode_t mode1, mode_t mode2, struct got_repository *repo)
2734 static const struct got_error *err = NULL;
2735 struct merge_file_cb_arg *a = arg;
2736 struct got_fileindex_entry *ie;
2737 char *ondisk_path = NULL;
2739 unsigned char status;
2740 int local_changes_subsumed;
2742 if (blob1 && blob2) {
2743 ie = got_fileindex_entry_get(a->fileindex, path2,
2746 return (*a->progress_cb)(a->progress_arg,
2747 GOT_STATUS_MISSING, path2);
2749 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2751 return got_error_from_errno("asprintf");
2753 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2758 if (status == GOT_STATUS_DELETE) {
2759 err = (*a->progress_cb)(a->progress_arg,
2760 GOT_STATUS_MERGE, path2);
2763 if (status != GOT_STATUS_NO_CHANGE &&
2764 status != GOT_STATUS_MODIFY &&
2765 status != GOT_STATUS_CONFLICT &&
2766 status != GOT_STATUS_ADD) {
2767 err = (*a->progress_cb)(a->progress_arg, status, path2);
2771 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2773 err = got_object_blob_read_to_str(&link_target2, blob2);
2776 err = merge_symlink(a->worktree, blob1, ondisk_path,
2777 path2, a->label_orig, link_target2, a->commit_id2,
2778 repo, a->progress_cb, a->progress_arg);
2781 err = merge_blob(&local_changes_subsumed, a->worktree,
2782 blob1, ondisk_path, path2, sb.st_mode,
2783 a->label_orig, blob2, a->commit_id2, repo,
2784 a->progress_cb, a->progress_arg);
2787 ie = got_fileindex_entry_get(a->fileindex, path1,
2790 return (*a->progress_cb)(a->progress_arg,
2791 GOT_STATUS_MISSING, path1);
2793 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2795 return got_error_from_errno("asprintf");
2797 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2803 case GOT_STATUS_NO_CHANGE:
2804 err = (*a->progress_cb)(a->progress_arg,
2805 GOT_STATUS_DELETE, path1);
2808 err = remove_ondisk_file(a->worktree->root_path, path1);
2812 got_fileindex_entry_mark_deleted_from_disk(ie);
2814 case GOT_STATUS_DELETE:
2815 case GOT_STATUS_MISSING:
2816 err = (*a->progress_cb)(a->progress_arg,
2817 GOT_STATUS_DELETE, path1);
2821 got_fileindex_entry_mark_deleted_from_disk(ie);
2823 case GOT_STATUS_ADD: {
2824 struct got_object_id *id;
2827 * Delete the added file only if its content already
2828 * exists in the repository.
2830 err = got_object_blob_file_create(&id, &blob1_f, path1);
2833 if (got_object_id_cmp(id, id1) == 0) {
2834 err = (*a->progress_cb)(a->progress_arg,
2835 GOT_STATUS_DELETE, path1);
2838 err = remove_ondisk_file(a->worktree->root_path,
2843 got_fileindex_entry_remove(a->fileindex,
2846 err = (*a->progress_cb)(a->progress_arg,
2847 GOT_STATUS_CANNOT_DELETE, path1);
2849 if (fclose(blob1_f) == EOF && err == NULL)
2850 err = got_error_from_errno("fclose");
2856 case GOT_STATUS_MODIFY:
2857 case GOT_STATUS_CONFLICT:
2858 err = (*a->progress_cb)(a->progress_arg,
2859 GOT_STATUS_CANNOT_DELETE, path1);
2863 case GOT_STATUS_OBSTRUCTED:
2864 err = (*a->progress_cb)(a->progress_arg, status, path1);
2872 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2874 return got_error_from_errno("asprintf");
2875 ie = got_fileindex_entry_get(a->fileindex, path2,
2878 err = get_file_status(&status, &sb, ie, ondisk_path,
2882 if (status != GOT_STATUS_NO_CHANGE &&
2883 status != GOT_STATUS_MODIFY &&
2884 status != GOT_STATUS_CONFLICT &&
2885 status != GOT_STATUS_ADD) {
2886 err = (*a->progress_cb)(a->progress_arg,
2890 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2892 err = got_object_blob_read_to_str(&link_target2,
2896 err = merge_symlink(a->worktree, NULL,
2897 ondisk_path, path2, a->label_orig,
2898 link_target2, a->commit_id2, repo,
2899 a->progress_cb, a->progress_arg);
2901 } else if (S_ISREG(sb.st_mode)) {
2902 err = merge_blob(&local_changes_subsumed,
2903 a->worktree, NULL, ondisk_path, path2,
2904 sb.st_mode, a->label_orig, blob2,
2905 a->commit_id2, repo, a->progress_cb,
2908 err = got_error_path(ondisk_path,
2909 GOT_ERR_FILE_OBSTRUCTED);
2913 if (status == GOT_STATUS_DELETE) {
2914 err = got_fileindex_entry_update(ie,
2915 ondisk_path, blob2->id.sha1,
2916 a->worktree->base_commit_id->sha1, 0);
2921 int is_bad_symlink = 0;
2922 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2923 if (S_ISLNK(mode2)) {
2924 err = install_symlink(&is_bad_symlink,
2925 a->worktree, ondisk_path, path2, blob2, 0,
2926 0, 1, repo, a->progress_cb, a->progress_arg);
2928 err = install_blob(a->worktree, ondisk_path, path2,
2929 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2930 a->progress_cb, a->progress_arg);
2934 err = got_fileindex_entry_alloc(&ie, path2);
2937 err = got_fileindex_entry_update(ie, ondisk_path,
2940 got_fileindex_entry_free(ie);
2943 err = got_fileindex_entry_add(a->fileindex, ie);
2945 got_fileindex_entry_free(ie);
2948 if (is_bad_symlink) {
2949 got_fileindex_entry_filetype_set(ie,
2950 GOT_FILEIDX_MODE_BAD_SYMLINK);
2959 struct check_merge_ok_arg {
2960 struct got_worktree *worktree;
2961 struct got_repository *repo;
2964 static const struct got_error *
2965 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2967 const struct got_error *err = NULL;
2968 struct check_merge_ok_arg *a = arg;
2969 unsigned char status;
2973 /* Reject merges into a work tree with mixed base commits. */
2974 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2975 SHA1_DIGEST_LENGTH))
2976 return got_error(GOT_ERR_MIXED_COMMITS);
2978 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2980 return got_error_from_errno("asprintf");
2982 /* Reject merges into a work tree with conflicted files. */
2983 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2986 if (status == GOT_STATUS_CONFLICT)
2987 return got_error(GOT_ERR_CONFLICTS);
2992 static const struct got_error *
2993 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2994 const char *fileindex_path, struct got_object_id *commit_id1,
2995 struct got_object_id *commit_id2, struct got_repository *repo,
2996 got_worktree_checkout_cb progress_cb, void *progress_arg,
2997 got_cancel_cb cancel_cb, void *cancel_arg)
2999 const struct got_error *err = NULL, *sync_err;
3000 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3001 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3002 struct merge_file_cb_arg arg;
3003 char *label_orig = NULL;
3006 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3007 worktree->path_prefix);
3008 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3014 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3018 err = got_object_id_str(&id_str, commit_id1);
3022 if (asprintf(&label_orig, "%s: commit %s",
3023 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3024 err = got_error_from_errno("asprintf");
3031 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3032 worktree->path_prefix);
3036 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3040 arg.worktree = worktree;
3041 arg.fileindex = fileindex;
3042 arg.progress_cb = progress_cb;
3043 arg.progress_arg = progress_arg;
3044 arg.cancel_cb = cancel_cb;
3045 arg.cancel_arg = cancel_arg;
3046 arg.label_orig = label_orig;
3047 arg.commit_id2 = commit_id2;
3048 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3049 sync_err = sync_fileindex(fileindex, fileindex_path);
3050 if (sync_err && err == NULL)
3054 got_object_tree_close(tree1);
3056 got_object_tree_close(tree2);
3061 const struct got_error *
3062 got_worktree_merge_files(struct got_worktree *worktree,
3063 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3064 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3065 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3067 const struct got_error *err, *unlockerr;
3068 char *fileindex_path = NULL;
3069 struct got_fileindex *fileindex = NULL;
3070 struct check_merge_ok_arg mok_arg;
3072 err = lock_worktree(worktree, LOCK_EX);
3076 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3080 mok_arg.worktree = worktree;
3081 mok_arg.repo = repo;
3082 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
3087 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3088 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
3091 got_fileindex_free(fileindex);
3092 free(fileindex_path);
3093 unlockerr = lock_worktree(worktree, LOCK_SH);
3094 if (unlockerr && err == NULL)
3099 struct diff_dir_cb_arg {
3100 struct got_fileindex *fileindex;
3101 struct got_worktree *worktree;
3102 const char *status_path;
3103 size_t status_path_len;
3104 struct got_repository *repo;
3105 got_worktree_status_cb status_cb;
3107 got_cancel_cb cancel_cb;
3109 /* A pathlist containing per-directory pathlists of ignore patterns. */
3110 struct got_pathlist_head ignores;
3111 int report_unchanged;
3115 static const struct got_error *
3116 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3117 int dirfd, const char *de_name,
3118 got_worktree_status_cb status_cb, void *status_arg,
3119 struct got_repository *repo, int report_unchanged)
3121 const struct got_error *err = NULL;
3122 unsigned char status = GOT_STATUS_NO_CHANGE;
3123 unsigned char staged_status = get_staged_status(ie);
3125 struct got_object_id blob_id, commit_id, staged_blob_id;
3126 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3127 struct got_object_id *staged_blob_idp = NULL;
3129 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3133 if (status == GOT_STATUS_NO_CHANGE &&
3134 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3137 if (got_fileindex_entry_has_blob(ie)) {
3138 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3139 blob_idp = &blob_id;
3141 if (got_fileindex_entry_has_commit(ie)) {
3142 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3143 commit_idp = &commit_id;
3145 if (staged_status == GOT_STATUS_ADD ||
3146 staged_status == GOT_STATUS_MODIFY) {
3147 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3148 SHA1_DIGEST_LENGTH);
3149 staged_blob_idp = &staged_blob_id;
3152 return (*status_cb)(status_arg, status, staged_status,
3153 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3156 static const struct got_error *
3157 status_old_new(void *arg, struct got_fileindex_entry *ie,
3158 struct dirent *de, const char *parent_path, int dirfd)
3160 const struct got_error *err = NULL;
3161 struct diff_dir_cb_arg *a = arg;
3164 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3165 return got_error(GOT_ERR_CANCELLED);
3167 if (got_path_cmp(parent_path, a->status_path,
3168 strlen(parent_path), a->status_path_len) != 0 &&
3169 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3172 if (parent_path[0]) {
3173 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3174 parent_path, de->d_name) == -1)
3175 return got_error_from_errno("asprintf");
3177 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3179 return got_error_from_errno("asprintf");
3182 err = report_file_status(ie, abspath, dirfd, de->d_name,
3183 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3188 static const struct got_error *
3189 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3191 struct diff_dir_cb_arg *a = arg;
3192 struct got_object_id blob_id, commit_id;
3193 unsigned char status;
3195 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3196 return got_error(GOT_ERR_CANCELLED);
3198 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3201 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3202 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3203 if (got_fileindex_entry_has_file_on_disk(ie))
3204 status = GOT_STATUS_MISSING;
3206 status = GOT_STATUS_DELETE;
3207 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3208 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3212 free_ignorelist(struct got_pathlist_head *ignorelist)
3214 struct got_pathlist_entry *pe;
3216 TAILQ_FOREACH(pe, ignorelist, entry)
3217 free((char *)pe->path);
3218 got_pathlist_free(ignorelist);
3222 free_ignores(struct got_pathlist_head *ignores)
3224 struct got_pathlist_entry *pe;
3226 TAILQ_FOREACH(pe, ignores, entry) {
3227 struct got_pathlist_head *ignorelist = pe->data;
3228 free_ignorelist(ignorelist);
3229 free((char *)pe->path);
3231 got_pathlist_free(ignores);
3234 static const struct got_error *
3235 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3237 const struct got_error *err = NULL;
3238 struct got_pathlist_entry *pe = NULL;
3239 struct got_pathlist_head *ignorelist;
3240 char *line = NULL, *pattern, *dirpath = NULL;
3241 size_t linesize = 0;
3244 ignorelist = calloc(1, sizeof(*ignorelist));
3245 if (ignorelist == NULL)
3246 return got_error_from_errno("calloc");
3247 TAILQ_INIT(ignorelist);
3249 while ((linelen = getline(&line, &linesize, f)) != -1) {
3250 if (linelen > 0 && line[linelen - 1] == '\n')
3251 line[linelen - 1] = '\0';
3253 /* Git's ignores may contain comments. */
3257 /* Git's negated patterns are not (yet?) supported. */
3261 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3263 err = got_error_from_errno("asprintf");
3266 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3271 err = got_error_from_errno("getline");
3275 dirpath = strdup(path);
3276 if (dirpath == NULL) {
3277 err = got_error_from_errno("strdup");
3280 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3283 if (err || pe == NULL) {
3285 free_ignorelist(ignorelist);
3291 match_ignores(struct got_pathlist_head *ignores, const char *path)
3293 struct got_pathlist_entry *pe;
3295 /* Handle patterns which match in all directories. */
3296 TAILQ_FOREACH(pe, ignores, entry) {
3297 struct got_pathlist_head *ignorelist = pe->data;
3298 struct got_pathlist_entry *pi;
3300 TAILQ_FOREACH(pi, ignorelist, entry) {
3301 const char *p, *pattern = pi->path;
3303 if (strncmp(pattern, "**/", 3) != 0)
3308 if (fnmatch(pattern, p,
3309 FNM_PATHNAME | FNM_LEADING_DIR)) {
3310 /* Retry in next directory. */
3311 while (*p && *p != '/')
3323 * The ignores pathlist contains ignore lists from children before
3324 * parents, so we can find the most specific ignorelist by walking
3325 * ignores backwards.
3327 pe = TAILQ_LAST(ignores, got_pathlist_head);
3329 if (got_path_is_child(path, pe->path, pe->path_len)) {
3330 struct got_pathlist_head *ignorelist = pe->data;
3331 struct got_pathlist_entry *pi;
3332 TAILQ_FOREACH(pi, ignorelist, entry) {
3333 const char *pattern = pi->path;
3334 int flags = FNM_LEADING_DIR;
3335 if (strstr(pattern, "/**/") == NULL)
3336 flags |= FNM_PATHNAME;
3337 if (fnmatch(pattern, path, flags))
3342 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3348 static const struct got_error *
3349 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3350 const char *path, int dirfd, const char *ignores_filename)
3352 const struct got_error *err = NULL;
3355 FILE *ignoresfile = NULL;
3357 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3358 path[0] ? "/" : "", ignores_filename) == -1)
3359 return got_error_from_errno("asprintf");
3362 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3364 if (errno != ENOENT && errno != EACCES)
3365 err = got_error_from_errno2("openat",
3368 ignoresfile = fdopen(fd, "r");
3369 if (ignoresfile == NULL)
3370 err = got_error_from_errno2("fdopen",
3374 err = read_ignores(ignores, path, ignoresfile);
3378 ignoresfile = fopen(ignorespath, "r");
3379 if (ignoresfile == NULL) {
3380 if (errno != ENOENT && errno != EACCES)
3381 err = got_error_from_errno2("fopen",
3384 err = read_ignores(ignores, path, ignoresfile);
3387 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3388 err = got_error_from_errno2("fclose", path);
3389 if (fd != -1 && close(fd) == -1 && err == NULL)
3390 err = got_error_from_errno2("close", path);
3395 static const struct got_error *
3396 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3398 const struct got_error *err = NULL;
3399 struct diff_dir_cb_arg *a = arg;
3402 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3403 return got_error(GOT_ERR_CANCELLED);
3405 if (parent_path[0]) {
3406 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3407 return got_error_from_errno("asprintf");
3412 if (de->d_type != DT_DIR &&
3413 got_path_is_child(path, a->status_path, a->status_path_len)
3414 && !match_ignores(&a->ignores, path))
3415 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3416 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3422 static const struct got_error *
3423 status_traverse(void *arg, const char *path, int dirfd)
3425 const struct got_error *err = NULL;
3426 struct diff_dir_cb_arg *a = arg;
3431 err = add_ignores(&a->ignores, a->worktree->root_path,
3432 path, dirfd, ".cvsignore");
3436 err = add_ignores(&a->ignores, a->worktree->root_path, path,
3437 dirfd, ".gitignore");
3442 static const struct got_error *
3443 report_single_file_status(const char *path, const char *ondisk_path,
3444 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3445 void *status_arg, struct got_repository *repo, int report_unchanged)
3447 struct got_fileindex_entry *ie;
3450 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3452 return report_file_status(ie, ondisk_path, -1, NULL,
3453 status_cb, status_arg, repo, report_unchanged);
3455 if (lstat(ondisk_path, &sb) == -1) {
3456 if (errno != ENOENT)
3457 return got_error_from_errno2("lstat", ondisk_path);
3458 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3459 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3463 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3464 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3465 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3470 static const struct got_error *
3471 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3472 const char *root_path, const char *path)
3474 const struct got_error *err;
3475 char *parent_path, *next_parent_path = NULL;
3477 err = add_ignores(ignores, root_path, "", -1,
3482 err = add_ignores(ignores, root_path, "", -1,
3487 err = got_path_dirname(&parent_path, path);
3489 if (err->code == GOT_ERR_BAD_PATH)
3490 return NULL; /* cannot traverse parent */
3494 err = add_ignores(ignores, root_path, parent_path, -1,
3498 err = add_ignores(ignores, root_path, parent_path, -1,
3502 err = got_path_dirname(&next_parent_path, parent_path);
3504 if (err->code == GOT_ERR_BAD_PATH)
3505 err = NULL; /* traversed everything */
3509 parent_path = next_parent_path;
3510 next_parent_path = NULL;
3514 free(next_parent_path);
3518 static const struct got_error *
3519 worktree_status(struct got_worktree *worktree, const char *path,
3520 struct got_fileindex *fileindex, struct got_repository *repo,
3521 got_worktree_status_cb status_cb, void *status_arg,
3522 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3523 int report_unchanged)
3525 const struct got_error *err = NULL;
3527 struct got_fileindex_diff_dir_cb fdiff_cb;
3528 struct diff_dir_cb_arg arg;
3529 char *ondisk_path = NULL;
3531 TAILQ_INIT(&arg.ignores);
3533 if (asprintf(&ondisk_path, "%s%s%s",
3534 worktree->root_path, path[0] ? "/" : "", path) == -1)
3535 return got_error_from_errno("asprintf");
3537 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3539 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3541 err = got_error_from_errno2("open", ondisk_path);
3543 err = report_single_file_status(path, ondisk_path,
3544 fileindex, status_cb, status_arg, repo,
3547 fdiff_cb.diff_old_new = status_old_new;
3548 fdiff_cb.diff_old = status_old;
3549 fdiff_cb.diff_new = status_new;
3550 fdiff_cb.diff_traverse = status_traverse;
3551 arg.fileindex = fileindex;
3552 arg.worktree = worktree;
3553 arg.status_path = path;
3554 arg.status_path_len = strlen(path);
3556 arg.status_cb = status_cb;
3557 arg.status_arg = status_arg;
3558 arg.cancel_cb = cancel_cb;
3559 arg.cancel_arg = cancel_arg;
3560 arg.report_unchanged = report_unchanged;
3561 arg.no_ignores = no_ignores;
3563 err = add_ignores_from_parent_paths(&arg.ignores,
3564 worktree->root_path, path);
3568 err = got_fileindex_diff_dir(fileindex, fd,
3569 worktree->root_path, path, repo, &fdiff_cb, &arg);
3572 free_ignores(&arg.ignores);
3573 if (fd != -1 && close(fd) != 0 && err == NULL)
3574 err = got_error_from_errno("close");
3579 const struct got_error *
3580 got_worktree_status(struct got_worktree *worktree,
3581 struct got_pathlist_head *paths, struct got_repository *repo,
3582 got_worktree_status_cb status_cb, void *status_arg,
3583 got_cancel_cb cancel_cb, void *cancel_arg)
3585 const struct got_error *err = NULL;
3586 char *fileindex_path = NULL;
3587 struct got_fileindex *fileindex = NULL;
3588 struct got_pathlist_entry *pe;
3590 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3594 TAILQ_FOREACH(pe, paths, entry) {
3595 err = worktree_status(worktree, pe->path, fileindex, repo,
3596 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3600 free(fileindex_path);
3601 got_fileindex_free(fileindex);
3605 const struct got_error *
3606 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3609 const struct got_error *err = NULL;
3610 char *resolved = NULL, *cwd = NULL, *path = NULL;
3616 cwd = getcwd(NULL, 0);
3618 return got_error_from_errno("getcwd");
3620 if (lstat(arg, &sb) == -1) {
3621 if (errno != ENOENT) {
3622 err = got_error_from_errno2("lstat", arg);
3626 if (S_ISLNK(sb.st_mode)) {
3628 * We cannot use realpath(3) with symlinks since we want to
3629 * operate on the symlink itself.
3630 * But we can make the path absolute, assuming it is relative
3631 * to the current working directory, and then canonicalize it.
3633 char *abspath = NULL;
3634 char canonpath[PATH_MAX];
3635 if (!got_path_is_absolute(arg)) {
3636 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3637 err = got_error_from_errno("asprintf");
3642 err = got_canonpath(abspath ? abspath : arg, canonpath,
3646 resolved = strdup(canonpath);
3647 if (resolved == NULL) {
3648 err = got_error_from_errno("strdup");
3652 resolved = realpath(arg, NULL);
3653 if (resolved == NULL) {
3654 if (errno != ENOENT) {
3655 err = got_error_from_errno2("realpath", arg);
3658 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
3659 err = got_error_from_errno("asprintf");
3665 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3666 strlen(got_worktree_get_root_path(worktree)))) {
3667 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3671 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3672 err = got_path_skip_common_ancestor(&path,
3673 got_worktree_get_root_path(worktree), resolved);
3679 err = got_error_from_errno("strdup");
3684 /* XXX status walk can't deal with trailing slash! */
3686 while (len > 0 && path[len - 1] == '/') {
3687 path[len - 1] = '\0';
3700 struct schedule_addition_args {
3701 struct got_worktree *worktree;
3702 struct got_fileindex *fileindex;
3703 got_worktree_checkout_cb progress_cb;
3705 struct got_repository *repo;
3708 static const struct got_error *
3709 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3710 const char *relpath, struct got_object_id *blob_id,
3711 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3712 int dirfd, const char *de_name)
3714 struct schedule_addition_args *a = arg;
3715 const struct got_error *err = NULL;
3716 struct got_fileindex_entry *ie;
3720 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3722 return got_error_from_errno("asprintf");
3724 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3726 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3730 /* Re-adding an existing entry is a no-op. */
3731 if (status == GOT_STATUS_ADD)
3733 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3738 if (status != GOT_STATUS_UNVERSIONED) {
3739 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3743 err = got_fileindex_entry_alloc(&ie, relpath);
3746 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3748 got_fileindex_entry_free(ie);
3751 err = got_fileindex_entry_add(a->fileindex, ie);
3753 got_fileindex_entry_free(ie);
3760 if (status == GOT_STATUS_ADD)
3762 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3765 const struct got_error *
3766 got_worktree_schedule_add(struct got_worktree *worktree,
3767 struct got_pathlist_head *paths,
3768 got_worktree_checkout_cb progress_cb, void *progress_arg,
3769 struct got_repository *repo, int no_ignores)
3771 struct got_fileindex *fileindex = NULL;
3772 char *fileindex_path = NULL;
3773 const struct got_error *err = NULL, *sync_err, *unlockerr;
3774 struct got_pathlist_entry *pe;
3775 struct schedule_addition_args saa;
3777 err = lock_worktree(worktree, LOCK_EX);
3781 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3785 saa.worktree = worktree;
3786 saa.fileindex = fileindex;
3787 saa.progress_cb = progress_cb;
3788 saa.progress_arg = progress_arg;
3791 TAILQ_FOREACH(pe, paths, entry) {
3792 err = worktree_status(worktree, pe->path, fileindex, repo,
3793 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3797 sync_err = sync_fileindex(fileindex, fileindex_path);
3798 if (sync_err && err == NULL)
3801 free(fileindex_path);
3803 got_fileindex_free(fileindex);
3804 unlockerr = lock_worktree(worktree, LOCK_SH);
3805 if (unlockerr && err == NULL)
3810 struct schedule_deletion_args {
3811 struct got_worktree *worktree;
3812 struct got_fileindex *fileindex;
3813 got_worktree_delete_cb progress_cb;
3815 struct got_repository *repo;
3816 int delete_local_mods;
3818 const char *status_codes;
3821 static const struct got_error *
3822 schedule_for_deletion(void *arg, unsigned char status,
3823 unsigned char staged_status, const char *relpath,
3824 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3825 struct got_object_id *commit_id, int dirfd, const char *de_name)
3827 struct schedule_deletion_args *a = arg;
3828 const struct got_error *err = NULL;
3829 struct got_fileindex_entry *ie = NULL;
3831 char *ondisk_path, *parent = NULL;
3833 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3835 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3837 staged_status = get_staged_status(ie);
3838 if (staged_status != GOT_STATUS_NO_CHANGE) {
3839 if (staged_status == GOT_STATUS_DELETE)
3841 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3844 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3846 return got_error_from_errno("asprintf");
3848 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3853 if (a->status_codes) {
3854 size_t ncodes = strlen(a->status_codes);
3856 for (i = 0; i < ncodes ; i++) {
3857 if (status == a->status_codes[i])
3861 /* Do not delete files in non-matching status. */
3865 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
3866 a->status_codes[i] != GOT_STATUS_MISSING) {
3867 static char msg[64];
3868 snprintf(msg, sizeof(msg),
3869 "invalid status code '%c'", a->status_codes[i]);
3870 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
3875 if (status != GOT_STATUS_NO_CHANGE) {
3876 if (status == GOT_STATUS_DELETE)
3878 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3879 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3882 if (status != GOT_STATUS_MODIFY &&
3883 status != GOT_STATUS_MISSING) {
3884 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3889 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3891 if (unlinkat(dirfd, de_name, 0) != 0) {
3892 err = got_error_from_errno2("unlinkat",
3896 } else if (unlink(ondisk_path) != 0) {
3897 err = got_error_from_errno2("unlink", ondisk_path);
3901 parent = dirname(ondisk_path);
3903 if (parent == NULL) {
3904 err = got_error_from_errno2("dirname", ondisk_path);
3907 while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3908 if (rmdir(parent) == -1) {
3909 if (errno != ENOTEMPTY)
3910 err = got_error_from_errno2("rmdir",
3914 parent = dirname(parent);
3915 if (parent == NULL) {
3916 err = got_error_from_errno2("dirname", parent);
3922 got_fileindex_entry_mark_deleted_from_disk(ie);
3927 if (status == GOT_STATUS_DELETE)
3929 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3930 staged_status, relpath);
3933 const struct got_error *
3934 got_worktree_schedule_delete(struct got_worktree *worktree,
3935 struct got_pathlist_head *paths, int delete_local_mods,
3936 const char *status_codes,
3937 got_worktree_delete_cb progress_cb, void *progress_arg,
3938 struct got_repository *repo, int keep_on_disk)
3940 struct got_fileindex *fileindex = NULL;
3941 char *fileindex_path = NULL;
3942 const struct got_error *err = NULL, *sync_err, *unlockerr;
3943 struct got_pathlist_entry *pe;
3944 struct schedule_deletion_args sda;
3946 err = lock_worktree(worktree, LOCK_EX);
3950 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3954 sda.worktree = worktree;
3955 sda.fileindex = fileindex;
3956 sda.progress_cb = progress_cb;
3957 sda.progress_arg = progress_arg;
3959 sda.delete_local_mods = delete_local_mods;
3960 sda.keep_on_disk = keep_on_disk;
3961 sda.status_codes = status_codes;
3963 TAILQ_FOREACH(pe, paths, entry) {
3964 err = worktree_status(worktree, pe->path, fileindex, repo,
3965 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3969 sync_err = sync_fileindex(fileindex, fileindex_path);
3970 if (sync_err && err == NULL)
3973 free(fileindex_path);
3975 got_fileindex_free(fileindex);
3976 unlockerr = lock_worktree(worktree, LOCK_SH);
3977 if (unlockerr && err == NULL)
3982 static const struct got_error *
3983 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3985 const struct got_error *err = NULL;
3987 size_t linesize = 0, n;
3990 linelen = getline(&line, &linesize, infile);
3991 if (linelen == -1) {
3992 if (ferror(infile)) {
3993 err = got_error_from_errno("getline");
3999 n = fwrite(line, 1, linelen, outfile);
4001 err = got_ferror(outfile, GOT_ERR_IO);
4006 n = fwrite(line, 1, linelen, rejectfile);
4008 err = got_ferror(outfile, GOT_ERR_IO);
4015 static const struct got_error *
4016 skip_one_line(FILE *f)
4019 size_t linesize = 0;
4022 linelen = getline(&line, &linesize, f);
4023 if (linelen == -1) {
4025 return got_error_from_errno("getline");
4032 static const struct got_error *
4033 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4034 int start_old, int end_old, int start_new, int end_new,
4035 FILE *outfile, FILE *rejectfile)
4037 const struct got_error *err;
4039 /* Copy old file's lines leading up to patch. */
4040 while (!feof(f1) && *line_cur1 < start_old) {
4041 err = copy_one_line(f1, outfile, NULL);
4046 /* Skip new file's lines leading up to patch. */
4047 while (!feof(f2) && *line_cur2 < start_new) {
4049 err = copy_one_line(f2, NULL, rejectfile);
4051 err = skip_one_line(f2);
4056 /* Copy patched lines. */
4057 while (!feof(f2) && *line_cur2 <= end_new) {
4058 err = copy_one_line(f2, outfile, NULL);
4063 /* Skip over old file's replaced lines. */
4064 while (!feof(f1) && *line_cur1 <= end_old) {
4066 err = copy_one_line(f1, NULL, rejectfile);
4068 err = skip_one_line(f1);
4077 static const struct got_error *
4078 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4079 FILE *outfile, FILE *rejectfile)
4081 const struct got_error *err;
4084 /* Copy old file's lines until EOF. */
4086 err = copy_one_line(f1, outfile, NULL);
4093 /* Copy new file's lines until EOF. */
4095 err = copy_one_line(f2, NULL, rejectfile);
4105 static const struct got_error *
4106 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
4107 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
4108 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
4109 int *line_cur2, FILE *outfile, FILE *rejectfile,
4110 got_worktree_patch_cb patch_cb, void *patch_arg)
4112 const struct got_error *err = NULL;
4113 int start_old = change->cv.a;
4114 int end_old = change->cv.b;
4115 int start_new = change->cv.c;
4116 int end_new = change->cv.d;
4120 *choice = GOT_PATCH_CHOICE_NONE;
4122 hunkfile = got_opentemp();
4123 if (hunkfile == NULL)
4124 return got_error_from_errno("got_opentemp");
4129 /* XXX TODO needs error checking */
4130 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
4132 if (fseek(f1, pos1, SEEK_SET) == -1) {
4133 err = got_ferror(f1, GOT_ERR_IO);
4136 if (fseek(f2, pos2, SEEK_SET) == -1) {
4137 err = got_ferror(f1, GOT_ERR_IO);
4140 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4141 err = got_ferror(hunkfile, GOT_ERR_IO);
4145 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4146 hunkfile, n, nchanges);
4151 case GOT_PATCH_CHOICE_YES:
4152 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4153 end_old, start_new, end_new, outfile, rejectfile);
4155 case GOT_PATCH_CHOICE_NO:
4156 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4157 end_old, start_new, end_new, rejectfile, outfile);
4159 case GOT_PATCH_CHOICE_QUIT:
4162 err = got_error(GOT_ERR_PATCH_CHOICE);
4166 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4167 err = got_error_from_errno("fclose");
4171 struct revert_file_args {
4172 struct got_worktree *worktree;
4173 struct got_fileindex *fileindex;
4174 got_worktree_checkout_cb progress_cb;
4176 got_worktree_patch_cb patch_cb;
4178 struct got_repository *repo;
4181 static const struct got_error *
4182 create_patched_content(char **path_outfile, int reverse_patch,
4183 struct got_object_id *blob_id, const char *path2,
4184 int dirfd2, const char *de_name2,
4185 const char *relpath, struct got_repository *repo,
4186 got_worktree_patch_cb patch_cb, void *patch_arg)
4188 const struct got_error *err;
4189 struct got_blob_object *blob = NULL;
4190 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4192 char link_target[PATH_MAX];
4193 ssize_t link_len = 0;
4194 char *path1 = NULL, *id_str = NULL;
4195 struct stat sb1, sb2;
4196 struct got_diff_changes *changes = NULL;
4197 struct got_diff_state *ds = NULL;
4198 struct got_diff_args *args = NULL;
4199 struct got_diff_change *change;
4200 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
4203 *path_outfile = NULL;
4205 err = got_object_id_str(&id_str, blob_id);
4210 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4212 if (errno != ELOOP) {
4213 err = got_error_from_errno2("openat", path2);
4216 link_len = readlinkat(dirfd2, de_name2,
4217 link_target, sizeof(link_target));
4219 return got_error_from_errno2("readlinkat", path2);
4220 sb2.st_mode = S_IFLNK;
4221 sb2.st_size = link_len;
4224 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4226 if (errno != ELOOP) {
4227 err = got_error_from_errno2("open", path2);
4230 link_len = readlink(path2, link_target,
4231 sizeof(link_target));
4233 return got_error_from_errno2("readlink", path2);
4234 sb2.st_mode = S_IFLNK;
4235 sb2.st_size = link_len;
4239 if (fstat(fd2, &sb2) == -1) {
4240 err = got_error_from_errno2("fstat", path2);
4244 f2 = fdopen(fd2, "r");
4246 err = got_error_from_errno2("fdopen", path2);
4252 f2 = got_opentemp();
4254 err = got_error_from_errno2("got_opentemp", path2);
4257 n = fwrite(link_target, 1, link_len, f2);
4258 if (n != link_len) {
4259 err = got_ferror(f2, GOT_ERR_IO);
4262 if (fflush(f2) == EOF) {
4263 err = got_error_from_errno("fflush");
4269 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4273 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4277 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4281 if (stat(path1, &sb1) == -1) {
4282 err = got_error_from_errno2("stat", path1);
4286 err = got_diff_files(&changes, &ds, &args, &diff_flags,
4287 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
4291 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4295 if (fseek(f1, 0L, SEEK_SET) == -1)
4296 return got_ferror(f1, GOT_ERR_IO);
4297 if (fseek(f2, 0L, SEEK_SET) == -1)
4298 return got_ferror(f2, GOT_ERR_IO);
4299 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
4301 err = apply_or_reject_change(&choice, change, ++n,
4302 changes->nchanges, ds, args, diff_flags, relpath,
4303 f1, f2, &line_cur1, &line_cur2,
4304 reverse_patch ? NULL : outfile,
4305 reverse_patch ? outfile : NULL,
4306 patch_cb, patch_arg);
4309 if (choice == GOT_PATCH_CHOICE_YES)
4311 else if (choice == GOT_PATCH_CHOICE_QUIT)
4315 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4316 reverse_patch ? NULL : outfile,
4317 reverse_patch ? outfile : NULL);
4321 if (!S_ISLNK(sb2.st_mode)) {
4322 if (chmod(*path_outfile, sb2.st_mode) == -1) {
4323 err = got_error_from_errno2("chmod", path2);
4331 got_object_blob_close(blob);
4332 if (f1 && fclose(f1) == EOF && err == NULL)
4333 err = got_error_from_errno2("fclose", path1);
4334 if (f2 && fclose(f2) == EOF && err == NULL)
4335 err = got_error_from_errno2("fclose", path2);
4336 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4337 err = got_error_from_errno2("close", path2);
4338 if (outfile && fclose(outfile) == EOF && err == NULL)
4339 err = got_error_from_errno2("fclose", *path_outfile);
4340 if (path1 && unlink(path1) == -1 && err == NULL)
4341 err = got_error_from_errno2("unlink", path1);
4342 if (err || !have_content) {
4343 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4344 err = got_error_from_errno2("unlink", *path_outfile);
4345 free(*path_outfile);
4346 *path_outfile = NULL;
4350 got_diff_state_free(ds);
4354 got_diff_free_changes(changes);
4359 static const struct got_error *
4360 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4361 const char *relpath, struct got_object_id *blob_id,
4362 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4363 int dirfd, const char *de_name)
4365 struct revert_file_args *a = arg;
4366 const struct got_error *err = NULL;
4367 char *parent_path = NULL;
4368 struct got_fileindex_entry *ie;
4369 struct got_tree_object *tree = NULL;
4370 struct got_object_id *tree_id = NULL;
4371 const struct got_tree_entry *te = NULL;
4372 char *tree_path = NULL, *te_name;
4373 char *ondisk_path = NULL, *path_content = NULL;
4374 struct got_blob_object *blob = NULL;
4376 /* Reverting a staged deletion is a no-op. */
4377 if (status == GOT_STATUS_DELETE &&
4378 staged_status != GOT_STATUS_NO_CHANGE)
4381 if (status == GOT_STATUS_UNVERSIONED)
4382 return (*a->progress_cb)(a->progress_arg,
4383 GOT_STATUS_UNVERSIONED, relpath);
4385 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4387 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4389 /* Construct in-repository path of tree which contains this blob. */
4390 err = got_path_dirname(&parent_path, ie->path);
4392 if (err->code != GOT_ERR_BAD_PATH)
4394 parent_path = strdup("/");
4395 if (parent_path == NULL) {
4396 err = got_error_from_errno("strdup");
4400 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4401 tree_path = strdup(parent_path);
4402 if (tree_path == NULL) {
4403 err = got_error_from_errno("strdup");
4407 if (got_path_is_root_dir(parent_path)) {
4408 tree_path = strdup(a->worktree->path_prefix);
4409 if (tree_path == NULL) {
4410 err = got_error_from_errno("strdup");
4414 if (asprintf(&tree_path, "%s/%s",
4415 a->worktree->path_prefix, parent_path) == -1) {
4416 err = got_error_from_errno("asprintf");
4422 err = got_object_id_by_path(&tree_id, a->repo,
4423 a->worktree->base_commit_id, tree_path);
4425 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4426 (status == GOT_STATUS_ADD ||
4427 staged_status == GOT_STATUS_ADD)))
4430 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4434 err = got_path_basename(&te_name, ie->path);
4438 te = got_object_tree_find_entry(tree, te_name);
4440 if (te == NULL && status != GOT_STATUS_ADD &&
4441 staged_status != GOT_STATUS_ADD) {
4442 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4448 case GOT_STATUS_ADD:
4450 int choice = GOT_PATCH_CHOICE_NONE;
4451 err = (*a->patch_cb)(&choice, a->patch_arg,
4452 status, ie->path, NULL, 1, 1);
4455 if (choice != GOT_PATCH_CHOICE_YES)
4458 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4462 got_fileindex_entry_remove(a->fileindex, ie);
4464 case GOT_STATUS_DELETE:
4466 int choice = GOT_PATCH_CHOICE_NONE;
4467 err = (*a->patch_cb)(&choice, a->patch_arg,
4468 status, ie->path, NULL, 1, 1);
4471 if (choice != GOT_PATCH_CHOICE_YES)
4475 case GOT_STATUS_MODIFY:
4476 case GOT_STATUS_MODE_CHANGE:
4477 case GOT_STATUS_CONFLICT:
4478 case GOT_STATUS_MISSING: {
4479 struct got_object_id id;
4480 if (staged_status == GOT_STATUS_ADD ||
4481 staged_status == GOT_STATUS_MODIFY) {
4482 memcpy(id.sha1, ie->staged_blob_sha1,
4483 SHA1_DIGEST_LENGTH);
4485 memcpy(id.sha1, ie->blob_sha1,
4486 SHA1_DIGEST_LENGTH);
4487 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4491 if (asprintf(&ondisk_path, "%s/%s",
4492 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4493 err = got_error_from_errno("asprintf");
4497 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4498 status == GOT_STATUS_CONFLICT)) {
4499 int is_bad_symlink = 0;
4500 err = create_patched_content(&path_content, 1, &id,
4501 ondisk_path, dirfd, de_name, ie->path, a->repo,
4502 a->patch_cb, a->patch_arg);
4503 if (err || path_content == NULL)
4505 if (te && S_ISLNK(te->mode)) {
4506 if (unlink(path_content) == -1) {
4507 err = got_error_from_errno2("unlink",
4511 err = install_symlink(&is_bad_symlink,
4512 a->worktree, ondisk_path, ie->path,
4513 blob, 0, 1, 0, a->repo,
4514 a->progress_cb, a->progress_arg);
4516 if (rename(path_content, ondisk_path) == -1) {
4517 err = got_error_from_errno3("rename",
4518 path_content, ondisk_path);
4523 int is_bad_symlink = 0;
4524 if (te && S_ISLNK(te->mode)) {
4525 err = install_symlink(&is_bad_symlink,
4526 a->worktree, ondisk_path, ie->path,
4527 blob, 0, 1, 0, a->repo,
4528 a->progress_cb, a->progress_arg);
4530 err = install_blob(a->worktree, ondisk_path,
4532 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4533 got_fileindex_perms_to_st(ie), blob,
4534 0, 1, 0, 0, a->repo,
4535 a->progress_cb, a->progress_arg);
4539 if (status == GOT_STATUS_DELETE ||
4540 status == GOT_STATUS_MODE_CHANGE) {
4541 err = got_fileindex_entry_update(ie,
4542 ondisk_path, blob->id.sha1,
4543 a->worktree->base_commit_id->sha1, 1);
4547 if (is_bad_symlink) {
4548 got_fileindex_entry_filetype_set(ie,
4549 GOT_FILEIDX_MODE_BAD_SYMLINK);
4563 got_object_blob_close(blob);
4565 got_object_tree_close(tree);
4570 const struct got_error *
4571 got_worktree_revert(struct got_worktree *worktree,
4572 struct got_pathlist_head *paths,
4573 got_worktree_checkout_cb progress_cb, void *progress_arg,
4574 got_worktree_patch_cb patch_cb, void *patch_arg,
4575 struct got_repository *repo)
4577 struct got_fileindex *fileindex = NULL;
4578 char *fileindex_path = NULL;
4579 const struct got_error *err = NULL, *unlockerr = NULL;
4580 const struct got_error *sync_err = NULL;
4581 struct got_pathlist_entry *pe;
4582 struct revert_file_args rfa;
4584 err = lock_worktree(worktree, LOCK_EX);
4588 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4592 rfa.worktree = worktree;
4593 rfa.fileindex = fileindex;
4594 rfa.progress_cb = progress_cb;
4595 rfa.progress_arg = progress_arg;
4596 rfa.patch_cb = patch_cb;
4597 rfa.patch_arg = patch_arg;
4599 TAILQ_FOREACH(pe, paths, entry) {
4600 err = worktree_status(worktree, pe->path, fileindex, repo,
4601 revert_file, &rfa, NULL, NULL, 0, 0);
4605 sync_err = sync_fileindex(fileindex, fileindex_path);
4606 if (sync_err && err == NULL)
4609 free(fileindex_path);
4611 got_fileindex_free(fileindex);
4612 unlockerr = lock_worktree(worktree, LOCK_SH);
4613 if (unlockerr && err == NULL)
4619 free_commitable(struct got_commitable *ct)
4622 free(ct->in_repo_path);
4623 free(ct->ondisk_path);
4625 free(ct->base_blob_id);
4626 free(ct->staged_blob_id);
4627 free(ct->base_commit_id);
4631 struct collect_commitables_arg {
4632 struct got_pathlist_head *commitable_paths;
4633 struct got_repository *repo;
4634 struct got_worktree *worktree;
4635 struct got_fileindex *fileindex;
4636 int have_staged_files;
4637 int allow_bad_symlinks;
4640 static const struct got_error *
4641 collect_commitables(void *arg, unsigned char status,
4642 unsigned char staged_status, const char *relpath,
4643 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4644 struct got_object_id *commit_id, int dirfd, const char *de_name)
4646 struct collect_commitables_arg *a = arg;
4647 const struct got_error *err = NULL;
4648 struct got_commitable *ct = NULL;
4649 struct got_pathlist_entry *new = NULL;
4650 char *parent_path = NULL, *path = NULL;
4653 if (a->have_staged_files) {
4654 if (staged_status != GOT_STATUS_MODIFY &&
4655 staged_status != GOT_STATUS_ADD &&
4656 staged_status != GOT_STATUS_DELETE)
4659 if (status == GOT_STATUS_CONFLICT)
4660 return got_error(GOT_ERR_COMMIT_CONFLICT);
4662 if (status != GOT_STATUS_MODIFY &&
4663 status != GOT_STATUS_MODE_CHANGE &&
4664 status != GOT_STATUS_ADD &&
4665 status != GOT_STATUS_DELETE)
4669 if (asprintf(&path, "/%s", relpath) == -1) {
4670 err = got_error_from_errno("asprintf");
4673 if (strcmp(path, "/") == 0) {
4674 parent_path = strdup("");
4675 if (parent_path == NULL)
4676 return got_error_from_errno("strdup");
4678 err = got_path_dirname(&parent_path, path);
4683 ct = calloc(1, sizeof(*ct));
4685 err = got_error_from_errno("calloc");
4689 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4691 err = got_error_from_errno("asprintf");
4695 if (staged_status == GOT_STATUS_ADD ||
4696 staged_status == GOT_STATUS_MODIFY) {
4697 struct got_fileindex_entry *ie;
4698 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4699 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4700 case GOT_FILEIDX_MODE_REGULAR_FILE:
4701 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4704 case GOT_FILEIDX_MODE_SYMLINK:
4708 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4711 ct->mode |= got_fileindex_entry_perms_get(ie);
4712 } else if (status != GOT_STATUS_DELETE &&
4713 staged_status != GOT_STATUS_DELETE) {
4715 if (fstatat(dirfd, de_name, &sb,
4716 AT_SYMLINK_NOFOLLOW) == -1) {
4717 err = got_error_from_errno2("fstatat",
4721 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4722 err = got_error_from_errno2("lstat", ct->ondisk_path);
4725 ct->mode = sb.st_mode;
4728 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4729 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4731 err = got_error_from_errno("asprintf");
4735 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4736 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4738 char target_path[PATH_MAX];
4740 target_len = readlink(ct->ondisk_path, target_path,
4741 sizeof(target_path));
4742 if (target_len == -1) {
4743 err = got_error_from_errno2("readlink",
4747 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4748 target_len, ct->ondisk_path, a->worktree->root_path);
4751 if (is_bad_symlink) {
4752 err = got_error_path(ct->ondisk_path,
4753 GOT_ERR_BAD_SYMLINK);
4759 ct->status = status;
4760 ct->staged_status = staged_status;
4761 ct->blob_id = NULL; /* will be filled in when blob gets created */
4762 if (ct->status != GOT_STATUS_ADD &&
4763 ct->staged_status != GOT_STATUS_ADD) {
4764 ct->base_blob_id = got_object_id_dup(blob_id);
4765 if (ct->base_blob_id == NULL) {
4766 err = got_error_from_errno("got_object_id_dup");
4769 ct->base_commit_id = got_object_id_dup(commit_id);
4770 if (ct->base_commit_id == NULL) {
4771 err = got_error_from_errno("got_object_id_dup");
4775 if (ct->staged_status == GOT_STATUS_ADD ||
4776 ct->staged_status == GOT_STATUS_MODIFY) {
4777 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4778 if (ct->staged_blob_id == NULL) {
4779 err = got_error_from_errno("got_object_id_dup");
4783 ct->path = strdup(path);
4784 if (ct->path == NULL) {
4785 err = got_error_from_errno("strdup");
4788 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4790 if (ct && (err || new == NULL))
4791 free_commitable(ct);
4797 static const struct got_error *write_tree(struct got_object_id **, int *,
4798 struct got_tree_object *, const char *, struct got_pathlist_head *,
4799 got_worktree_status_cb status_cb, void *status_arg,
4800 struct got_repository *);
4802 static const struct got_error *
4803 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4804 struct got_tree_entry *te, const char *parent_path,
4805 struct got_pathlist_head *commitable_paths,
4806 got_worktree_status_cb status_cb, void *status_arg,
4807 struct got_repository *repo)
4809 const struct got_error *err = NULL;
4810 struct got_tree_object *subtree;
4813 if (asprintf(&subpath, "%s%s%s", parent_path,
4814 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4815 return got_error_from_errno("asprintf");
4817 err = got_object_open_as_tree(&subtree, repo, &te->id);
4821 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4822 commitable_paths, status_cb, status_arg, repo);
4823 got_object_tree_close(subtree);
4828 static const struct got_error *
4829 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4831 const struct got_error *err = NULL;
4832 char *ct_parent_path = NULL;
4836 if (strchr(ct->in_repo_path, '/') == NULL) {
4837 *match = got_path_is_root_dir(path);
4841 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4844 *match = (strcmp(path, ct_parent_path) == 0);
4845 free(ct_parent_path);
4850 get_ct_file_mode(struct got_commitable *ct)
4852 if (S_ISLNK(ct->mode))
4855 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4858 static const struct got_error *
4859 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4860 struct got_tree_entry *te, struct got_commitable *ct)
4862 const struct got_error *err = NULL;
4866 err = got_object_tree_entry_dup(new_te, te);
4870 (*new_te)->mode = get_ct_file_mode(ct);
4872 if (ct->staged_status == GOT_STATUS_MODIFY)
4873 memcpy(&(*new_te)->id, ct->staged_blob_id,
4874 sizeof((*new_te)->id));
4876 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4878 if (err && *new_te) {
4885 static const struct got_error *
4886 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4887 struct got_commitable *ct)
4889 const struct got_error *err = NULL;
4890 char *ct_name = NULL;
4894 *new_te = calloc(1, sizeof(**new_te));
4895 if (*new_te == NULL)
4896 return got_error_from_errno("calloc");
4898 err = got_path_basename(&ct_name, ct->path);
4901 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4902 sizeof((*new_te)->name)) {
4903 err = got_error(GOT_ERR_NO_SPACE);
4907 (*new_te)->mode = get_ct_file_mode(ct);
4909 if (ct->staged_status == GOT_STATUS_ADD)
4910 memcpy(&(*new_te)->id, ct->staged_blob_id,
4911 sizeof((*new_te)->id));
4913 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4916 if (err && *new_te) {
4923 static const struct got_error *
4924 insert_tree_entry(struct got_tree_entry *new_te,
4925 struct got_pathlist_head *paths)
4927 const struct got_error *err = NULL;
4928 struct got_pathlist_entry *new_pe;
4930 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4934 return got_error(GOT_ERR_TREE_DUP_ENTRY);
4938 static const struct got_error *
4939 report_ct_status(struct got_commitable *ct,
4940 got_worktree_status_cb status_cb, void *status_arg)
4942 const char *ct_path = ct->path;
4943 unsigned char status;
4945 while (ct_path[0] == '/')
4948 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4949 status = ct->staged_status;
4951 status = ct->status;
4953 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4954 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4957 static const struct got_error *
4958 match_modified_subtree(int *modified, struct got_tree_entry *te,
4959 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4961 const struct got_error *err = NULL;
4962 struct got_pathlist_entry *pe;
4967 if (asprintf(&te_path, "%s%s%s", base_tree_path,
4968 got_path_is_root_dir(base_tree_path) ? "" : "/",
4970 return got_error_from_errno("asprintf");
4972 TAILQ_FOREACH(pe, commitable_paths, entry) {
4973 struct got_commitable *ct = pe->data;
4974 *modified = got_path_is_child(ct->in_repo_path, te_path,
4984 static const struct got_error *
4985 match_deleted_or_modified_ct(struct got_commitable **ctp,
4986 struct got_tree_entry *te, const char *base_tree_path,
4987 struct got_pathlist_head *commitable_paths)
4989 const struct got_error *err = NULL;
4990 struct got_pathlist_entry *pe;
4994 TAILQ_FOREACH(pe, commitable_paths, entry) {
4995 struct got_commitable *ct = pe->data;
4996 char *ct_name = NULL;
4999 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5000 if (ct->status != GOT_STATUS_MODIFY &&
5001 ct->status != GOT_STATUS_MODE_CHANGE &&
5002 ct->status != GOT_STATUS_DELETE)
5005 if (ct->staged_status != GOT_STATUS_MODIFY &&
5006 ct->staged_status != GOT_STATUS_DELETE)
5010 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5013 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5019 err = got_path_basename(&ct_name, pe->path);
5023 if (strcmp(te->name, ct_name) != 0) {
5036 static const struct got_error *
5037 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5038 const char *child_path, const char *path_base_tree,
5039 struct got_pathlist_head *commitable_paths,
5040 got_worktree_status_cb status_cb, void *status_arg,
5041 struct got_repository *repo)
5043 const struct got_error *err = NULL;
5044 struct got_tree_entry *new_te;
5046 struct got_object_id *id = NULL;
5051 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5052 got_path_is_root_dir(path_base_tree) ? "" : "/",
5054 return got_error_from_errno("asprintf");
5056 new_te = calloc(1, sizeof(*new_te));
5058 return got_error_from_errno("calloc");
5059 new_te->mode = S_IFDIR;
5061 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5062 sizeof(new_te->name)) {
5063 err = got_error(GOT_ERR_NO_SPACE);
5066 err = write_tree(&id, &nentries, NULL, subtree_path,
5067 commitable_paths, status_cb, status_arg, repo);
5072 memcpy(&new_te->id, id, sizeof(new_te->id));
5081 static const struct got_error *
5082 write_tree(struct got_object_id **new_tree_id, int *nentries,
5083 struct got_tree_object *base_tree, const char *path_base_tree,
5084 struct got_pathlist_head *commitable_paths,
5085 got_worktree_status_cb status_cb, void *status_arg,
5086 struct got_repository *repo)
5088 const struct got_error *err = NULL;
5089 struct got_pathlist_head paths;
5090 struct got_tree_entry *te, *new_te = NULL;
5091 struct got_pathlist_entry *pe;
5096 /* Insert, and recurse into, newly added entries first. */
5097 TAILQ_FOREACH(pe, commitable_paths, entry) {
5098 struct got_commitable *ct = pe->data;
5099 char *child_path = NULL, *slash;
5101 if ((ct->status != GOT_STATUS_ADD &&
5102 ct->staged_status != GOT_STATUS_ADD) ||
5103 (ct->flags & GOT_COMMITABLE_ADDED))
5106 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5107 strlen(path_base_tree)))
5110 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5115 slash = strchr(child_path, '/');
5116 if (slash == NULL) {
5117 err = alloc_added_blob_tree_entry(&new_te, ct);
5120 err = report_ct_status(ct, status_cb, status_arg);
5123 ct->flags |= GOT_COMMITABLE_ADDED;
5124 err = insert_tree_entry(new_te, &paths);
5129 *slash = '\0'; /* trim trailing path components */
5130 if (base_tree == NULL ||
5131 got_object_tree_find_entry(base_tree, child_path)
5133 err = make_subtree_for_added_blob(&new_te,
5134 child_path, path_base_tree,
5135 commitable_paths, status_cb, status_arg,
5139 err = insert_tree_entry(new_te, &paths);
5148 int i, nbase_entries;
5149 /* Handle modified and deleted entries. */
5150 nbase_entries = got_object_tree_get_nentries(base_tree);
5151 for (i = 0; i < nbase_entries; i++) {
5152 struct got_commitable *ct = NULL;
5154 te = got_object_tree_get_entry(base_tree, i);
5155 if (got_object_tree_entry_is_submodule(te)) {
5156 /* Entry is a submodule; just copy it. */
5157 err = got_object_tree_entry_dup(&new_te, te);
5160 err = insert_tree_entry(new_te, &paths);
5167 if (S_ISDIR(te->mode)) {
5169 err = got_object_tree_entry_dup(&new_te, te);
5172 err = match_modified_subtree(&modified, te,
5173 path_base_tree, commitable_paths);
5176 /* Avoid recursion into unmodified subtrees. */
5178 struct got_object_id *new_id;
5180 err = write_subtree(&new_id,
5182 path_base_tree, commitable_paths,
5183 status_cb, status_arg, repo);
5186 if (nsubentries == 0) {
5187 /* All entries were deleted. */
5191 memcpy(&new_te->id, new_id,
5192 sizeof(new_te->id));
5195 err = insert_tree_entry(new_te, &paths);
5202 err = match_deleted_or_modified_ct(&ct, te,
5203 path_base_tree, commitable_paths);
5207 /* NB: Deleted entries get dropped here. */
5208 if (ct->status == GOT_STATUS_MODIFY ||
5209 ct->status == GOT_STATUS_MODE_CHANGE ||
5210 ct->staged_status == GOT_STATUS_MODIFY) {
5211 err = alloc_modified_blob_tree_entry(
5215 err = insert_tree_entry(new_te, &paths);
5220 err = report_ct_status(ct, status_cb,
5225 /* Entry is unchanged; just copy it. */
5226 err = got_object_tree_entry_dup(&new_te, te);
5229 err = insert_tree_entry(new_te, &paths);
5237 /* Write new list of entries; deleted entries have been dropped. */
5238 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5240 got_pathlist_free(&paths);
5244 static const struct got_error *
5245 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
5246 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
5247 int have_staged_files)
5249 const struct got_error *err = NULL;
5250 struct got_pathlist_entry *pe;
5252 TAILQ_FOREACH(pe, commitable_paths, entry) {
5253 struct got_fileindex_entry *ie;
5254 struct got_commitable *ct = pe->data;
5256 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5258 if (ct->status == GOT_STATUS_DELETE ||
5259 ct->staged_status == GOT_STATUS_DELETE) {
5260 got_fileindex_entry_remove(fileindex, ie);
5261 } else if (ct->staged_status == GOT_STATUS_ADD ||
5262 ct->staged_status == GOT_STATUS_MODIFY) {
5263 got_fileindex_entry_stage_set(ie,
5264 GOT_FILEIDX_STAGE_NONE);
5265 err = got_fileindex_entry_update(ie,
5266 ct->ondisk_path, ct->staged_blob_id->sha1,
5267 new_base_commit_id->sha1,
5268 !have_staged_files);
5270 err = got_fileindex_entry_update(ie,
5271 ct->ondisk_path, ct->blob_id->sha1,
5272 new_base_commit_id->sha1,
5273 !have_staged_files);
5275 err = got_fileindex_entry_alloc(&ie, pe->path);
5278 err = got_fileindex_entry_update(ie, ct->ondisk_path,
5279 ct->blob_id->sha1, new_base_commit_id->sha1, 1);
5281 got_fileindex_entry_free(ie);
5284 err = got_fileindex_entry_add(fileindex, ie);
5286 got_fileindex_entry_free(ie);
5295 static const struct got_error *
5296 check_out_of_date(const char *in_repo_path, unsigned char status,
5297 unsigned char staged_status, struct got_object_id *base_blob_id,
5298 struct got_object_id *base_commit_id,
5299 struct got_object_id *head_commit_id, struct got_repository *repo,
5302 const struct got_error *err = NULL;
5303 struct got_object_id *id = NULL;
5305 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5306 /* Trivial case: base commit == head commit */
5307 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5310 * Ensure file content which local changes were based
5311 * on matches file content in the branch head.
5313 err = got_object_id_by_path(&id, repo, head_commit_id,
5316 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5317 err = got_error(ood_errcode);
5319 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5320 err = got_error(ood_errcode);
5322 /* Require that added files don't exist in the branch head. */
5323 err = got_object_id_by_path(&id, repo, head_commit_id,
5325 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5327 err = id ? got_error(ood_errcode) : NULL;
5334 const struct got_error *
5335 commit_worktree(struct got_object_id **new_commit_id,
5336 struct got_pathlist_head *commitable_paths,
5337 struct got_object_id *head_commit_id, struct got_worktree *worktree,
5338 const char *author, const char *committer,
5339 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5340 got_worktree_status_cb status_cb, void *status_arg,
5341 struct got_repository *repo)
5343 const struct got_error *err = NULL, *unlockerr = NULL;
5344 struct got_pathlist_entry *pe;
5345 const char *head_ref_name = NULL;
5346 struct got_commit_object *head_commit = NULL;
5347 struct got_reference *head_ref2 = NULL;
5348 struct got_object_id *head_commit_id2 = NULL;
5349 struct got_tree_object *head_tree = NULL;
5350 struct got_object_id *new_tree_id = NULL;
5352 struct got_object_id_queue parent_ids;
5353 struct got_object_qid *pid = NULL;
5354 char *logmsg = NULL;
5356 *new_commit_id = NULL;
5358 SIMPLEQ_INIT(&parent_ids);
5360 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5364 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5368 if (commit_msg_cb != NULL) {
5369 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5374 if (logmsg == NULL || strlen(logmsg) == 0) {
5375 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5379 /* Create blobs from added and modified files and record their IDs. */
5380 TAILQ_FOREACH(pe, commitable_paths, entry) {
5381 struct got_commitable *ct = pe->data;
5384 /* Blobs for staged files already exist. */
5385 if (ct->staged_status == GOT_STATUS_ADD ||
5386 ct->staged_status == GOT_STATUS_MODIFY)
5389 if (ct->status != GOT_STATUS_ADD &&
5390 ct->status != GOT_STATUS_MODIFY &&
5391 ct->status != GOT_STATUS_MODE_CHANGE)
5394 if (asprintf(&ondisk_path, "%s/%s",
5395 worktree->root_path, pe->path) == -1) {
5396 err = got_error_from_errno("asprintf");
5399 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5405 /* Recursively write new tree objects. */
5406 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5407 commitable_paths, status_cb, status_arg, repo);
5411 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5414 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5415 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5416 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5417 got_object_qid_free(pid);
5423 /* Check if a concurrent commit to our branch has occurred. */
5424 head_ref_name = got_worktree_get_head_ref_name(worktree);
5425 if (head_ref_name == NULL) {
5426 err = got_error_from_errno("got_worktree_get_head_ref_name");
5429 /* Lock the reference here to prevent concurrent modification. */
5430 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5433 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5436 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5437 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5440 /* Update branch head in repository. */
5441 err = got_ref_change_ref(head_ref2, *new_commit_id);
5444 err = got_ref_write(head_ref2, repo);
5448 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5452 err = ref_base_commit(worktree, repo);
5457 got_object_tree_close(head_tree);
5459 got_object_commit_close(head_commit);
5460 free(head_commit_id2);
5462 unlockerr = got_ref_unlock(head_ref2);
5463 if (unlockerr && err == NULL)
5465 got_ref_close(head_ref2);
5470 static const struct got_error *
5471 check_path_is_commitable(const char *path,
5472 struct got_pathlist_head *commitable_paths)
5474 struct got_pathlist_entry *cpe = NULL;
5475 size_t path_len = strlen(path);
5477 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5478 struct got_commitable *ct = cpe->data;
5479 const char *ct_path = ct->path;
5481 while (ct_path[0] == '/')
5484 if (strcmp(path, ct_path) == 0 ||
5485 got_path_is_child(ct_path, path, path_len))
5490 return got_error_path(path, GOT_ERR_BAD_PATH);
5495 static const struct got_error *
5496 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5498 int *have_staged_files = arg;
5500 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5501 *have_staged_files = 1;
5502 return got_error(GOT_ERR_CANCELLED);
5508 static const struct got_error *
5509 check_non_staged_files(struct got_fileindex *fileindex,
5510 struct got_pathlist_head *paths)
5512 struct got_pathlist_entry *pe;
5513 struct got_fileindex_entry *ie;
5515 TAILQ_FOREACH(pe, paths, entry) {
5516 if (pe->path[0] == '\0')
5518 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5520 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5521 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5522 return got_error_path(pe->path,
5523 GOT_ERR_FILE_NOT_STAGED);
5529 const struct got_error *
5530 got_worktree_commit(struct got_object_id **new_commit_id,
5531 struct got_worktree *worktree, struct got_pathlist_head *paths,
5532 const char *author, const char *committer, int allow_bad_symlinks,
5533 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5534 got_worktree_status_cb status_cb, void *status_arg,
5535 struct got_repository *repo)
5537 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5538 struct got_fileindex *fileindex = NULL;
5539 char *fileindex_path = NULL;
5540 struct got_pathlist_head commitable_paths;
5541 struct collect_commitables_arg cc_arg;
5542 struct got_pathlist_entry *pe;
5543 struct got_reference *head_ref = NULL;
5544 struct got_object_id *head_commit_id = NULL;
5545 int have_staged_files = 0;
5547 *new_commit_id = NULL;
5549 TAILQ_INIT(&commitable_paths);
5551 err = lock_worktree(worktree, LOCK_EX);
5555 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5559 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5563 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5567 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5568 &have_staged_files);
5569 if (err && err->code != GOT_ERR_CANCELLED)
5571 if (have_staged_files) {
5572 err = check_non_staged_files(fileindex, paths);
5577 cc_arg.commitable_paths = &commitable_paths;
5578 cc_arg.worktree = worktree;
5579 cc_arg.fileindex = fileindex;
5581 cc_arg.have_staged_files = have_staged_files;
5582 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5583 TAILQ_FOREACH(pe, paths, entry) {
5584 err = worktree_status(worktree, pe->path, fileindex, repo,
5585 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5590 if (TAILQ_EMPTY(&commitable_paths)) {
5591 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5595 TAILQ_FOREACH(pe, paths, entry) {
5596 err = check_path_is_commitable(pe->path, &commitable_paths);
5601 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5602 struct got_commitable *ct = pe->data;
5603 const char *ct_path = ct->in_repo_path;
5605 while (ct_path[0] == '/')
5607 err = check_out_of_date(ct_path, ct->status,
5608 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5609 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5615 err = commit_worktree(new_commit_id, &commitable_paths,
5616 head_commit_id, worktree, author, committer,
5617 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5621 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5622 fileindex, have_staged_files);
5623 sync_err = sync_fileindex(fileindex, fileindex_path);
5624 if (sync_err && err == NULL)
5628 got_fileindex_free(fileindex);
5629 free(fileindex_path);
5630 unlockerr = lock_worktree(worktree, LOCK_SH);
5631 if (unlockerr && err == NULL)
5633 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5634 struct got_commitable *ct = pe->data;
5635 free_commitable(ct);
5637 got_pathlist_free(&commitable_paths);
5642 got_commitable_get_path(struct got_commitable *ct)
5648 got_commitable_get_status(struct got_commitable *ct)
5653 struct check_rebase_ok_arg {
5654 struct got_worktree *worktree;
5655 struct got_repository *repo;
5658 static const struct got_error *
5659 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5661 const struct got_error *err = NULL;
5662 struct check_rebase_ok_arg *a = arg;
5663 unsigned char status;
5667 /* Reject rebase of a work tree with mixed base commits. */
5668 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5669 SHA1_DIGEST_LENGTH))
5670 return got_error(GOT_ERR_MIXED_COMMITS);
5672 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5674 return got_error_from_errno("asprintf");
5676 /* Reject rebase of a work tree with modified or staged files. */
5677 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5682 if (status != GOT_STATUS_NO_CHANGE)
5683 return got_error(GOT_ERR_MODIFIED);
5684 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5685 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5690 const struct got_error *
5691 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5692 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5693 struct got_worktree *worktree, struct got_reference *branch,
5694 struct got_repository *repo)
5696 const struct got_error *err = NULL;
5697 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5698 char *branch_ref_name = NULL;
5699 char *fileindex_path = NULL;
5700 struct check_rebase_ok_arg ok_arg;
5701 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5702 struct got_object_id *wt_branch_tip = NULL;
5704 *new_base_branch_ref = NULL;
5708 err = lock_worktree(worktree, LOCK_EX);
5712 err = open_fileindex(fileindex, &fileindex_path, worktree);
5716 ok_arg.worktree = worktree;
5718 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5723 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5727 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5731 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5735 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5740 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5743 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5744 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5748 err = got_ref_alloc_symref(new_base_branch_ref,
5749 new_base_branch_ref_name, wt_branch);
5752 err = got_ref_write(*new_base_branch_ref, repo);
5756 /* TODO Lock original branch's ref while rebasing? */
5758 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5762 err = got_ref_write(branch_ref, repo);
5766 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5767 worktree->base_commit_id);
5770 err = got_ref_write(*tmp_branch, repo);
5774 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5778 free(fileindex_path);
5779 free(tmp_branch_name);
5780 free(new_base_branch_ref_name);
5781 free(branch_ref_name);
5783 got_ref_close(branch_ref);
5785 got_ref_close(wt_branch);
5786 free(wt_branch_tip);
5788 if (*new_base_branch_ref) {
5789 got_ref_close(*new_base_branch_ref);
5790 *new_base_branch_ref = NULL;
5793 got_ref_close(*tmp_branch);
5797 got_fileindex_free(*fileindex);
5800 lock_worktree(worktree, LOCK_SH);
5805 const struct got_error *
5806 got_worktree_rebase_continue(struct got_object_id **commit_id,
5807 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5808 struct got_reference **branch, struct got_fileindex **fileindex,
5809 struct got_worktree *worktree, struct got_repository *repo)
5811 const struct got_error *err;
5812 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5813 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5814 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5815 char *fileindex_path = NULL;
5816 int have_staged_files = 0;
5819 *new_base_branch = NULL;
5824 err = lock_worktree(worktree, LOCK_EX);
5828 err = open_fileindex(fileindex, &fileindex_path, worktree);
5832 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5833 &have_staged_files);
5834 if (err && err->code != GOT_ERR_CANCELLED)
5836 if (have_staged_files) {
5837 err = got_error(GOT_ERR_STAGED_PATHS);
5841 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5845 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5849 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5853 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5857 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5861 err = got_ref_open(branch, repo,
5862 got_ref_get_symref_target(branch_ref), 0);
5866 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5870 err = got_ref_resolve(commit_id, repo, commit_ref);
5874 err = got_ref_open(new_base_branch, repo,
5875 new_base_branch_ref_name, 0);
5879 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5883 free(commit_ref_name);
5884 free(branch_ref_name);
5885 free(fileindex_path);
5887 got_ref_close(commit_ref);
5889 got_ref_close(branch_ref);
5894 got_ref_close(*tmp_branch);
5897 if (*new_base_branch) {
5898 got_ref_close(*new_base_branch);
5899 *new_base_branch = NULL;
5902 got_ref_close(*branch);
5906 got_fileindex_free(*fileindex);
5909 lock_worktree(worktree, LOCK_SH);
5914 const struct got_error *
5915 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5917 const struct got_error *err;
5918 char *tmp_branch_name = NULL;
5920 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5924 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5925 free(tmp_branch_name);
5929 static const struct got_error *
5930 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5931 char **logmsg, void *arg)
5937 static const struct got_error *
5938 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5939 const char *path, struct got_object_id *blob_id,
5940 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5941 int dirfd, const char *de_name)
5946 struct collect_merged_paths_arg {
5947 got_worktree_checkout_cb progress_cb;
5949 struct got_pathlist_head *merged_paths;
5952 static const struct got_error *
5953 collect_merged_paths(void *arg, unsigned char status, const char *path)
5955 const struct got_error *err;
5956 struct collect_merged_paths_arg *a = arg;
5958 struct got_pathlist_entry *new;
5960 err = (*a->progress_cb)(a->progress_arg, status, path);
5964 if (status != GOT_STATUS_MERGE &&
5965 status != GOT_STATUS_ADD &&
5966 status != GOT_STATUS_DELETE &&
5967 status != GOT_STATUS_CONFLICT)
5972 return got_error_from_errno("strdup");
5974 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5975 if (err || new == NULL)
5981 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5983 struct got_pathlist_entry *pe;
5985 TAILQ_FOREACH(pe, merged_paths, entry)
5986 free((char *)pe->path);
5988 got_pathlist_free(merged_paths);
5991 static const struct got_error *
5992 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5993 int is_rebase, struct got_repository *repo)
5995 const struct got_error *err;
5996 struct got_reference *commit_ref = NULL;
5998 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6000 if (err->code != GOT_ERR_NOT_REF)
6002 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6005 err = got_ref_write(commit_ref, repo);
6008 } else if (is_rebase) {
6009 struct got_object_id *stored_id;
6012 err = got_ref_resolve(&stored_id, repo, commit_ref);
6015 cmp = got_object_id_cmp(commit_id, stored_id);
6018 err = got_error(GOT_ERR_REBASE_COMMITID);
6024 got_ref_close(commit_ref);
6028 static const struct got_error *
6029 rebase_merge_files(struct got_pathlist_head *merged_paths,
6030 const char *commit_ref_name, struct got_worktree *worktree,
6031 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6032 struct got_object_id *commit_id, struct got_repository *repo,
6033 got_worktree_checkout_cb progress_cb, void *progress_arg,
6034 got_cancel_cb cancel_cb, void *cancel_arg)
6036 const struct got_error *err;
6037 struct got_reference *commit_ref = NULL;
6038 struct collect_merged_paths_arg cmp_arg;
6039 char *fileindex_path;
6041 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6043 err = get_fileindex_path(&fileindex_path, worktree);
6047 cmp_arg.progress_cb = progress_cb;
6048 cmp_arg.progress_arg = progress_arg;
6049 cmp_arg.merged_paths = merged_paths;
6050 err = merge_files(worktree, fileindex, fileindex_path,
6051 parent_commit_id, commit_id, repo, collect_merged_paths,
6052 &cmp_arg, cancel_cb, cancel_arg);
6054 got_ref_close(commit_ref);
6058 const struct got_error *
6059 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6060 struct got_worktree *worktree, struct got_fileindex *fileindex,
6061 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6062 struct got_repository *repo,
6063 got_worktree_checkout_cb progress_cb, void *progress_arg,
6064 got_cancel_cb cancel_cb, void *cancel_arg)
6066 const struct got_error *err;
6067 char *commit_ref_name;
6069 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6073 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6077 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6078 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6079 progress_arg, cancel_cb, cancel_arg);
6081 free(commit_ref_name);
6085 const struct got_error *
6086 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6087 struct got_worktree *worktree, struct got_fileindex *fileindex,
6088 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6089 struct got_repository *repo,
6090 got_worktree_checkout_cb progress_cb, void *progress_arg,
6091 got_cancel_cb cancel_cb, void *cancel_arg)
6093 const struct got_error *err;
6094 char *commit_ref_name;
6096 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6100 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6104 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6105 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6106 progress_arg, cancel_cb, cancel_arg);
6108 free(commit_ref_name);
6112 static const struct got_error *
6113 rebase_commit(struct got_object_id **new_commit_id,
6114 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6115 struct got_worktree *worktree, struct got_fileindex *fileindex,
6116 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6117 const char *new_logmsg, struct got_repository *repo)
6119 const struct got_error *err, *sync_err;
6120 struct got_pathlist_head commitable_paths;
6121 struct collect_commitables_arg cc_arg;
6122 char *fileindex_path = NULL;
6123 struct got_reference *head_ref = NULL;
6124 struct got_object_id *head_commit_id = NULL;
6125 char *logmsg = NULL;
6127 TAILQ_INIT(&commitable_paths);
6128 *new_commit_id = NULL;
6130 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6132 err = get_fileindex_path(&fileindex_path, worktree);
6136 cc_arg.commitable_paths = &commitable_paths;
6137 cc_arg.worktree = worktree;
6139 cc_arg.have_staged_files = 0;
6141 * If possible get the status of individual files directly to
6142 * avoid crawling the entire work tree once per rebased commit.
6143 * TODO: Ideally, merged_paths would contain a list of commitables
6144 * we could use so we could skip worktree_status() entirely.
6147 struct got_pathlist_entry *pe;
6148 TAILQ_FOREACH(pe, merged_paths, entry) {
6149 err = worktree_status(worktree, pe->path, fileindex,
6150 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6156 err = worktree_status(worktree, "", fileindex, repo,
6157 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6162 if (TAILQ_EMPTY(&commitable_paths)) {
6163 /* No-op change; commit will be elided. */
6164 err = got_ref_delete(commit_ref, repo);
6167 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6171 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6175 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6180 logmsg = strdup(new_logmsg);
6181 if (logmsg == NULL) {
6182 err = got_error_from_errno("strdup");
6186 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6191 /* NB: commit_worktree will call free(logmsg) */
6192 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6193 worktree, got_object_commit_get_author(orig_commit),
6194 got_object_commit_get_committer(orig_commit),
6195 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6199 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6203 err = got_ref_delete(commit_ref, repo);
6207 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
6209 sync_err = sync_fileindex(fileindex, fileindex_path);
6210 if (sync_err && err == NULL)
6213 free(fileindex_path);
6214 free(head_commit_id);
6216 got_ref_close(head_ref);
6218 free(*new_commit_id);
6219 *new_commit_id = NULL;
6224 const struct got_error *
6225 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6226 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6227 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6228 struct got_commit_object *orig_commit,
6229 struct got_object_id *orig_commit_id, struct got_repository *repo)
6231 const struct got_error *err;
6232 char *commit_ref_name;
6233 struct got_reference *commit_ref = NULL;
6234 struct got_object_id *commit_id = NULL;
6236 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6240 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6243 err = got_ref_resolve(&commit_id, repo, commit_ref);
6246 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6247 err = got_error(GOT_ERR_REBASE_COMMITID);
6251 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6252 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6255 got_ref_close(commit_ref);
6256 free(commit_ref_name);
6261 const struct got_error *
6262 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6263 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6264 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6265 struct got_commit_object *orig_commit,
6266 struct got_object_id *orig_commit_id, const char *new_logmsg,
6267 struct got_repository *repo)
6269 const struct got_error *err;
6270 char *commit_ref_name;
6271 struct got_reference *commit_ref = NULL;
6273 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6277 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6281 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6282 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6285 got_ref_close(commit_ref);
6286 free(commit_ref_name);
6290 const struct got_error *
6291 got_worktree_rebase_postpone(struct got_worktree *worktree,
6292 struct got_fileindex *fileindex)
6295 got_fileindex_free(fileindex);
6296 return lock_worktree(worktree, LOCK_SH);
6299 static const struct got_error *
6300 delete_ref(const char *name, struct got_repository *repo)
6302 const struct got_error *err;
6303 struct got_reference *ref;
6305 err = got_ref_open(&ref, repo, name, 0);
6307 if (err->code == GOT_ERR_NOT_REF)
6312 err = got_ref_delete(ref, repo);
6317 static const struct got_error *
6318 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6320 const struct got_error *err;
6321 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6322 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6324 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6327 err = delete_ref(tmp_branch_name, repo);
6331 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6334 err = delete_ref(new_base_branch_ref_name, repo);
6338 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6341 err = delete_ref(branch_ref_name, repo);
6345 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6348 err = delete_ref(commit_ref_name, repo);
6353 free(tmp_branch_name);
6354 free(new_base_branch_ref_name);
6355 free(branch_ref_name);
6356 free(commit_ref_name);
6360 const struct got_error *
6361 got_worktree_rebase_complete(struct got_worktree *worktree,
6362 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6363 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6364 struct got_repository *repo)
6366 const struct got_error *err, *unlockerr;
6367 struct got_object_id *new_head_commit_id = NULL;
6369 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6373 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6377 err = got_ref_write(rebased_branch, repo);
6381 err = got_worktree_set_head_ref(worktree, rebased_branch);
6385 err = delete_rebase_refs(worktree, repo);
6388 got_fileindex_free(fileindex);
6389 free(new_head_commit_id);
6390 unlockerr = lock_worktree(worktree, LOCK_SH);
6391 if (unlockerr && err == NULL)
6396 const struct got_error *
6397 got_worktree_rebase_abort(struct got_worktree *worktree,
6398 struct got_fileindex *fileindex, struct got_repository *repo,
6399 struct got_reference *new_base_branch,
6400 got_worktree_checkout_cb progress_cb, void *progress_arg)
6402 const struct got_error *err, *unlockerr, *sync_err;
6403 struct got_reference *resolved = NULL;
6404 struct got_object_id *commit_id = NULL;
6405 char *fileindex_path = NULL;
6406 struct revert_file_args rfa;
6407 struct got_object_id *tree_id = NULL;
6409 err = lock_worktree(worktree, LOCK_EX);
6413 err = got_ref_open(&resolved, repo,
6414 got_ref_get_symref_target(new_base_branch), 0);
6418 err = got_worktree_set_head_ref(worktree, resolved);
6423 * XXX commits to the base branch could have happened while
6424 * we were busy rebasing; should we store the original commit ID
6425 * when rebase begins and read it back here?
6427 err = got_ref_resolve(&commit_id, repo, resolved);
6431 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6435 err = got_object_id_by_path(&tree_id, repo,
6436 worktree->base_commit_id, worktree->path_prefix);
6440 err = delete_rebase_refs(worktree, repo);
6444 err = get_fileindex_path(&fileindex_path, worktree);
6448 rfa.worktree = worktree;
6449 rfa.fileindex = fileindex;
6450 rfa.progress_cb = progress_cb;
6451 rfa.progress_arg = progress_arg;
6452 rfa.patch_cb = NULL;
6453 rfa.patch_arg = NULL;
6455 err = worktree_status(worktree, "", fileindex, repo,
6456 revert_file, &rfa, NULL, NULL, 0, 0);
6460 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6461 repo, progress_cb, progress_arg, NULL, NULL);
6463 sync_err = sync_fileindex(fileindex, fileindex_path);
6464 if (sync_err && err == NULL)
6467 got_ref_close(resolved);
6471 got_fileindex_free(fileindex);
6472 free(fileindex_path);
6474 unlockerr = lock_worktree(worktree, LOCK_SH);
6475 if (unlockerr && err == NULL)
6480 const struct got_error *
6481 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6482 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6483 struct got_fileindex **fileindex, struct got_worktree *worktree,
6484 struct got_repository *repo)
6486 const struct got_error *err = NULL;
6487 char *tmp_branch_name = NULL;
6488 char *branch_ref_name = NULL;
6489 char *base_commit_ref_name = NULL;
6490 char *fileindex_path = NULL;
6491 struct check_rebase_ok_arg ok_arg;
6492 struct got_reference *wt_branch = NULL;
6493 struct got_reference *base_commit_ref = NULL;
6497 *base_commit_id = NULL;
6500 err = lock_worktree(worktree, LOCK_EX);
6504 err = open_fileindex(fileindex, &fileindex_path, worktree);
6508 ok_arg.worktree = worktree;
6510 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6515 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6519 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6523 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6528 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6533 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6537 err = got_ref_write(*branch_ref, repo);
6541 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6542 worktree->base_commit_id);
6545 err = got_ref_write(base_commit_ref, repo);
6548 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6549 if (*base_commit_id == NULL) {
6550 err = got_error_from_errno("got_object_id_dup");
6554 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6555 worktree->base_commit_id);
6558 err = got_ref_write(*tmp_branch, repo);
6562 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6566 free(fileindex_path);
6567 free(tmp_branch_name);
6568 free(branch_ref_name);
6569 free(base_commit_ref_name);
6571 got_ref_close(wt_branch);
6574 got_ref_close(*branch_ref);
6578 got_ref_close(*tmp_branch);
6581 free(*base_commit_id);
6583 got_fileindex_free(*fileindex);
6586 lock_worktree(worktree, LOCK_SH);
6591 const struct got_error *
6592 got_worktree_histedit_postpone(struct got_worktree *worktree,
6593 struct got_fileindex *fileindex)
6596 got_fileindex_free(fileindex);
6597 return lock_worktree(worktree, LOCK_SH);
6600 const struct got_error *
6601 got_worktree_histedit_in_progress(int *in_progress,
6602 struct got_worktree *worktree)
6604 const struct got_error *err;
6605 char *tmp_branch_name = NULL;
6607 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6611 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6612 free(tmp_branch_name);
6616 const struct got_error *
6617 got_worktree_histedit_continue(struct got_object_id **commit_id,
6618 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6619 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6620 struct got_worktree *worktree, struct got_repository *repo)
6622 const struct got_error *err;
6623 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6624 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6625 struct got_reference *commit_ref = NULL;
6626 struct got_reference *base_commit_ref = NULL;
6627 char *fileindex_path = NULL;
6628 int have_staged_files = 0;
6632 *base_commit_id = NULL;
6635 err = lock_worktree(worktree, LOCK_EX);
6639 err = open_fileindex(fileindex, &fileindex_path, worktree);
6643 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6644 &have_staged_files);
6645 if (err && err->code != GOT_ERR_CANCELLED)
6647 if (have_staged_files) {
6648 err = got_error(GOT_ERR_STAGED_PATHS);
6652 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6656 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6660 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6664 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6669 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6673 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6676 err = got_ref_resolve(commit_id, repo, commit_ref);
6680 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6683 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6687 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6691 free(commit_ref_name);
6692 free(branch_ref_name);
6693 free(fileindex_path);
6695 got_ref_close(commit_ref);
6696 if (base_commit_ref)
6697 got_ref_close(base_commit_ref);
6701 free(*base_commit_id);
6702 *base_commit_id = NULL;
6704 got_ref_close(*tmp_branch);
6708 got_fileindex_free(*fileindex);
6711 lock_worktree(worktree, LOCK_EX);
6716 static const struct got_error *
6717 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6719 const struct got_error *err;
6720 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6721 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6723 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6726 err = delete_ref(tmp_branch_name, repo);
6730 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6734 err = delete_ref(base_commit_ref_name, repo);
6738 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6741 err = delete_ref(branch_ref_name, repo);
6745 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6748 err = delete_ref(commit_ref_name, repo);
6752 free(tmp_branch_name);
6753 free(base_commit_ref_name);
6754 free(branch_ref_name);
6755 free(commit_ref_name);
6759 const struct got_error *
6760 got_worktree_histedit_abort(struct got_worktree *worktree,
6761 struct got_fileindex *fileindex, struct got_repository *repo,
6762 struct got_reference *branch, struct got_object_id *base_commit_id,
6763 got_worktree_checkout_cb progress_cb, void *progress_arg)
6765 const struct got_error *err, *unlockerr, *sync_err;
6766 struct got_reference *resolved = NULL;
6767 char *fileindex_path = NULL;
6768 struct got_object_id *tree_id = NULL;
6769 struct revert_file_args rfa;
6771 err = lock_worktree(worktree, LOCK_EX);
6775 err = got_ref_open(&resolved, repo,
6776 got_ref_get_symref_target(branch), 0);
6780 err = got_worktree_set_head_ref(worktree, resolved);
6784 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6788 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6789 worktree->path_prefix);
6793 err = delete_histedit_refs(worktree, repo);
6797 err = get_fileindex_path(&fileindex_path, worktree);
6801 rfa.worktree = worktree;
6802 rfa.fileindex = fileindex;
6803 rfa.progress_cb = progress_cb;
6804 rfa.progress_arg = progress_arg;
6805 rfa.patch_cb = NULL;
6806 rfa.patch_arg = NULL;
6808 err = worktree_status(worktree, "", fileindex, repo,
6809 revert_file, &rfa, NULL, NULL, 0, 0);
6813 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6814 repo, progress_cb, progress_arg, NULL, NULL);
6816 sync_err = sync_fileindex(fileindex, fileindex_path);
6817 if (sync_err && err == NULL)
6820 got_ref_close(resolved);
6822 free(fileindex_path);
6824 unlockerr = lock_worktree(worktree, LOCK_SH);
6825 if (unlockerr && err == NULL)
6830 const struct got_error *
6831 got_worktree_histedit_complete(struct got_worktree *worktree,
6832 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6833 struct got_reference *edited_branch, struct got_repository *repo)
6835 const struct got_error *err, *unlockerr;
6836 struct got_object_id *new_head_commit_id = NULL;
6837 struct got_reference *resolved = NULL;
6839 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6843 err = got_ref_open(&resolved, repo,
6844 got_ref_get_symref_target(edited_branch), 0);
6848 err = got_ref_change_ref(resolved, new_head_commit_id);
6852 err = got_ref_write(resolved, repo);
6856 err = got_worktree_set_head_ref(worktree, resolved);
6860 err = delete_histedit_refs(worktree, repo);
6863 got_fileindex_free(fileindex);
6864 free(new_head_commit_id);
6865 unlockerr = lock_worktree(worktree, LOCK_SH);
6866 if (unlockerr && err == NULL)
6871 const struct got_error *
6872 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6873 struct got_object_id *commit_id, struct got_repository *repo)
6875 const struct got_error *err;
6876 char *commit_ref_name;
6878 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6882 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6886 err = delete_ref(commit_ref_name, repo);
6888 free(commit_ref_name);
6892 const struct got_error *
6893 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6894 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6895 struct got_worktree *worktree, const char *refname,
6896 struct got_repository *repo)
6898 const struct got_error *err = NULL;
6899 char *fileindex_path = NULL;
6900 struct check_rebase_ok_arg ok_arg;
6904 *base_branch_ref = NULL;
6906 err = lock_worktree(worktree, LOCK_EX);
6910 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6911 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6912 "cannot integrate a branch into itself; "
6913 "update -b or different branch name required");
6917 err = open_fileindex(fileindex, &fileindex_path, worktree);
6921 /* Preconditions are the same as for rebase. */
6922 ok_arg.worktree = worktree;
6924 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6929 err = got_ref_open(branch_ref, repo, refname, 1);
6933 err = got_ref_open(base_branch_ref, repo,
6934 got_worktree_get_head_ref_name(worktree), 1);
6938 got_ref_close(*branch_ref);
6941 if (*base_branch_ref) {
6942 got_ref_close(*base_branch_ref);
6943 *base_branch_ref = NULL;
6946 got_fileindex_free(*fileindex);
6949 lock_worktree(worktree, LOCK_SH);
6954 const struct got_error *
6955 got_worktree_integrate_continue(struct got_worktree *worktree,
6956 struct got_fileindex *fileindex, struct got_repository *repo,
6957 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6958 got_worktree_checkout_cb progress_cb, void *progress_arg,
6959 got_cancel_cb cancel_cb, void *cancel_arg)
6961 const struct got_error *err = NULL, *sync_err, *unlockerr;
6962 char *fileindex_path = NULL;
6963 struct got_object_id *tree_id = NULL, *commit_id = NULL;
6965 err = get_fileindex_path(&fileindex_path, worktree);
6969 err = got_ref_resolve(&commit_id, repo, branch_ref);
6973 err = got_object_id_by_path(&tree_id, repo, commit_id,
6974 worktree->path_prefix);
6978 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6982 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6983 progress_cb, progress_arg, cancel_cb, cancel_arg);
6987 err = got_ref_change_ref(base_branch_ref, commit_id);
6991 err = got_ref_write(base_branch_ref, repo);
6993 sync_err = sync_fileindex(fileindex, fileindex_path);
6994 if (sync_err && err == NULL)
6998 unlockerr = got_ref_unlock(branch_ref);
6999 if (unlockerr && err == NULL)
7001 got_ref_close(branch_ref);
7003 unlockerr = got_ref_unlock(base_branch_ref);
7004 if (unlockerr && err == NULL)
7006 got_ref_close(base_branch_ref);
7008 got_fileindex_free(fileindex);
7009 free(fileindex_path);
7012 unlockerr = lock_worktree(worktree, LOCK_SH);
7013 if (unlockerr && err == NULL)
7018 const struct got_error *
7019 got_worktree_integrate_abort(struct got_worktree *worktree,
7020 struct got_fileindex *fileindex, struct got_repository *repo,
7021 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7023 const struct got_error *err = NULL, *unlockerr = NULL;
7025 got_fileindex_free(fileindex);
7027 err = lock_worktree(worktree, LOCK_SH);
7029 unlockerr = got_ref_unlock(branch_ref);
7030 if (unlockerr && err == NULL)
7032 got_ref_close(branch_ref);
7034 unlockerr = got_ref_unlock(base_branch_ref);
7035 if (unlockerr && err == NULL)
7037 got_ref_close(base_branch_ref);
7042 struct check_stage_ok_arg {
7043 struct got_object_id *head_commit_id;
7044 struct got_worktree *worktree;
7045 struct got_fileindex *fileindex;
7046 struct got_repository *repo;
7050 const struct got_error *
7051 check_stage_ok(void *arg, unsigned char status,
7052 unsigned char staged_status, const char *relpath,
7053 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7054 struct got_object_id *commit_id, int dirfd, const char *de_name)
7056 struct check_stage_ok_arg *a = arg;
7057 const struct got_error *err = NULL;
7058 struct got_fileindex_entry *ie;
7059 struct got_object_id base_commit_id;
7060 struct got_object_id *base_commit_idp = NULL;
7061 char *in_repo_path = NULL, *p;
7063 if (status == GOT_STATUS_UNVERSIONED ||
7064 status == GOT_STATUS_NO_CHANGE)
7066 if (status == GOT_STATUS_NONEXISTENT)
7067 return got_error_set_errno(ENOENT, relpath);
7069 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7071 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7073 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7074 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7076 return got_error_from_errno("asprintf");
7078 if (got_fileindex_entry_has_commit(ie)) {
7079 memcpy(base_commit_id.sha1, ie->commit_sha1,
7080 SHA1_DIGEST_LENGTH);
7081 base_commit_idp = &base_commit_id;
7084 if (status == GOT_STATUS_CONFLICT) {
7085 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7087 } else if (status != GOT_STATUS_ADD &&
7088 status != GOT_STATUS_MODIFY &&
7089 status != GOT_STATUS_DELETE) {
7090 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7094 a->have_changes = 1;
7099 err = check_out_of_date(p, status, staged_status,
7100 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7101 GOT_ERR_STAGE_OUT_OF_DATE);
7107 struct stage_path_arg {
7108 struct got_worktree *worktree;
7109 struct got_fileindex *fileindex;
7110 struct got_repository *repo;
7111 got_worktree_status_cb status_cb;
7113 got_worktree_patch_cb patch_cb;
7115 int staged_something;
7116 int allow_bad_symlinks;
7119 static const struct got_error *
7120 stage_path(void *arg, unsigned char status,
7121 unsigned char staged_status, const char *relpath,
7122 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7123 struct got_object_id *commit_id, int dirfd, const char *de_name)
7125 struct stage_path_arg *a = arg;
7126 const struct got_error *err = NULL;
7127 struct got_fileindex_entry *ie;
7128 char *ondisk_path = NULL, *path_content = NULL;
7130 struct got_object_id *new_staged_blob_id = NULL;
7133 if (status == GOT_STATUS_UNVERSIONED)
7136 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7138 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7140 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7142 return got_error_from_errno("asprintf");
7145 case GOT_STATUS_ADD:
7146 case GOT_STATUS_MODIFY:
7147 /* XXX could sb.st_mode be passed in by our caller? */
7148 if (lstat(ondisk_path, &sb) == -1) {
7149 err = got_error_from_errno2("lstat", ondisk_path);
7153 if (status == GOT_STATUS_ADD) {
7154 int choice = GOT_PATCH_CHOICE_NONE;
7155 err = (*a->patch_cb)(&choice, a->patch_arg,
7156 status, ie->path, NULL, 1, 1);
7159 if (choice != GOT_PATCH_CHOICE_YES)
7162 err = create_patched_content(&path_content, 0,
7163 staged_blob_id ? staged_blob_id : blob_id,
7164 ondisk_path, dirfd, de_name, ie->path,
7165 a->repo, a->patch_cb, a->patch_arg);
7166 if (err || path_content == NULL)
7170 err = got_object_blob_create(&new_staged_blob_id,
7171 path_content ? path_content : ondisk_path, a->repo);
7174 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7175 SHA1_DIGEST_LENGTH);
7176 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7177 stage = GOT_FILEIDX_STAGE_ADD;
7179 stage = GOT_FILEIDX_STAGE_MODIFY;
7180 got_fileindex_entry_stage_set(ie, stage);
7181 if (S_ISLNK(sb.st_mode)) {
7182 int is_bad_symlink = 0;
7183 if (!a->allow_bad_symlinks) {
7184 char target_path[PATH_MAX];
7186 target_len = readlink(ondisk_path, target_path,
7187 sizeof(target_path));
7188 if (target_len == -1) {
7189 err = got_error_from_errno2("readlink",
7193 err = is_bad_symlink_target(&is_bad_symlink,
7194 target_path, target_len, ondisk_path,
7195 a->worktree->root_path);
7198 if (is_bad_symlink) {
7199 err = got_error_path(ondisk_path,
7200 GOT_ERR_BAD_SYMLINK);
7205 got_fileindex_entry_staged_filetype_set(ie,
7206 GOT_FILEIDX_MODE_BAD_SYMLINK);
7208 got_fileindex_entry_staged_filetype_set(ie,
7209 GOT_FILEIDX_MODE_SYMLINK);
7211 got_fileindex_entry_staged_filetype_set(ie,
7212 GOT_FILEIDX_MODE_REGULAR_FILE);
7214 a->staged_something = 1;
7215 if (a->status_cb == NULL)
7217 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7218 get_staged_status(ie), relpath, blob_id,
7219 new_staged_blob_id, NULL, dirfd, de_name);
7221 case GOT_STATUS_DELETE:
7222 if (staged_status == GOT_STATUS_DELETE)
7225 int choice = GOT_PATCH_CHOICE_NONE;
7226 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7227 ie->path, NULL, 1, 1);
7230 if (choice == GOT_PATCH_CHOICE_NO)
7232 if (choice != GOT_PATCH_CHOICE_YES) {
7233 err = got_error(GOT_ERR_PATCH_CHOICE);
7237 stage = GOT_FILEIDX_STAGE_DELETE;
7238 got_fileindex_entry_stage_set(ie, stage);
7239 a->staged_something = 1;
7240 if (a->status_cb == NULL)
7242 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7243 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7246 case GOT_STATUS_NO_CHANGE:
7248 case GOT_STATUS_CONFLICT:
7249 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7251 case GOT_STATUS_NONEXISTENT:
7252 err = got_error_set_errno(ENOENT, relpath);
7255 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7259 if (path_content && unlink(path_content) == -1 && err == NULL)
7260 err = got_error_from_errno2("unlink", path_content);
7263 free(new_staged_blob_id);
7267 const struct got_error *
7268 got_worktree_stage(struct got_worktree *worktree,
7269 struct got_pathlist_head *paths,
7270 got_worktree_status_cb status_cb, void *status_arg,
7271 got_worktree_patch_cb patch_cb, void *patch_arg,
7272 int allow_bad_symlinks, struct got_repository *repo)
7274 const struct got_error *err = NULL, *sync_err, *unlockerr;
7275 struct got_pathlist_entry *pe;
7276 struct got_fileindex *fileindex = NULL;
7277 char *fileindex_path = NULL;
7278 struct got_reference *head_ref = NULL;
7279 struct got_object_id *head_commit_id = NULL;
7280 struct check_stage_ok_arg oka;
7281 struct stage_path_arg spa;
7283 err = lock_worktree(worktree, LOCK_EX);
7287 err = got_ref_open(&head_ref, repo,
7288 got_worktree_get_head_ref_name(worktree), 0);
7291 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7294 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7298 /* Check pre-conditions before staging anything. */
7299 oka.head_commit_id = head_commit_id;
7300 oka.worktree = worktree;
7301 oka.fileindex = fileindex;
7303 oka.have_changes = 0;
7304 TAILQ_FOREACH(pe, paths, entry) {
7305 err = worktree_status(worktree, pe->path, fileindex, repo,
7306 check_stage_ok, &oka, NULL, NULL, 0, 0);
7310 if (!oka.have_changes) {
7311 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7315 spa.worktree = worktree;
7316 spa.fileindex = fileindex;
7318 spa.patch_cb = patch_cb;
7319 spa.patch_arg = patch_arg;
7320 spa.status_cb = status_cb;
7321 spa.status_arg = status_arg;
7322 spa.staged_something = 0;
7323 spa.allow_bad_symlinks = allow_bad_symlinks;
7324 TAILQ_FOREACH(pe, paths, entry) {
7325 err = worktree_status(worktree, pe->path, fileindex, repo,
7326 stage_path, &spa, NULL, NULL, 0, 0);
7330 if (!spa.staged_something) {
7331 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7335 sync_err = sync_fileindex(fileindex, fileindex_path);
7336 if (sync_err && err == NULL)
7340 got_ref_close(head_ref);
7341 free(head_commit_id);
7342 free(fileindex_path);
7344 got_fileindex_free(fileindex);
7345 unlockerr = lock_worktree(worktree, LOCK_SH);
7346 if (unlockerr && err == NULL)
7351 struct unstage_path_arg {
7352 struct got_worktree *worktree;
7353 struct got_fileindex *fileindex;
7354 struct got_repository *repo;
7355 got_worktree_checkout_cb progress_cb;
7357 got_worktree_patch_cb patch_cb;
7361 static const struct got_error *
7362 create_unstaged_content(char **path_unstaged_content,
7363 char **path_new_staged_content, struct got_object_id *blob_id,
7364 struct got_object_id *staged_blob_id, const char *relpath,
7365 struct got_repository *repo,
7366 got_worktree_patch_cb patch_cb, void *patch_arg)
7368 const struct got_error *err;
7369 struct got_blob_object *blob = NULL, *staged_blob = NULL;
7370 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7371 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7372 struct stat sb1, sb2;
7373 struct got_diff_changes *changes = NULL;
7374 struct got_diff_state *ds = NULL;
7375 struct got_diff_args *args = NULL;
7376 struct got_diff_change *change;
7377 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
7378 int have_content = 0, have_rejected_content = 0;
7380 *path_unstaged_content = NULL;
7381 *path_new_staged_content = NULL;
7383 err = got_object_id_str(&label1, blob_id);
7386 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7390 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7394 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7398 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7402 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7406 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7410 if (stat(path1, &sb1) == -1) {
7411 err = got_error_from_errno2("stat", path1);
7415 if (stat(path2, &sb2) == -1) {
7416 err = got_error_from_errno2("stat", path2);
7420 err = got_diff_files(&changes, &ds, &args, &diff_flags,
7421 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
7425 err = got_opentemp_named(path_unstaged_content, &outfile,
7426 "got-unstaged-content");
7429 err = got_opentemp_named(path_new_staged_content, &rejectfile,
7430 "got-new-staged-content");
7434 if (fseek(f1, 0L, SEEK_SET) == -1) {
7435 err = got_ferror(f1, GOT_ERR_IO);
7438 if (fseek(f2, 0L, SEEK_SET) == -1) {
7439 err = got_ferror(f2, GOT_ERR_IO);
7442 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
7444 err = apply_or_reject_change(&choice, change, ++n,
7445 changes->nchanges, ds, args, diff_flags, relpath,
7446 f1, f2, &line_cur1, &line_cur2,
7447 outfile, rejectfile, patch_cb, patch_arg);
7450 if (choice == GOT_PATCH_CHOICE_YES)
7453 have_rejected_content = 1;
7454 if (choice == GOT_PATCH_CHOICE_QUIT)
7457 if (have_content || have_rejected_content)
7458 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7459 outfile, rejectfile);
7463 got_object_blob_close(blob);
7465 got_object_blob_close(staged_blob);
7466 if (f1 && fclose(f1) == EOF && err == NULL)
7467 err = got_error_from_errno2("fclose", path1);
7468 if (f2 && fclose(f2) == EOF && err == NULL)
7469 err = got_error_from_errno2("fclose", path2);
7470 if (outfile && fclose(outfile) == EOF && err == NULL)
7471 err = got_error_from_errno2("fclose", *path_unstaged_content);
7472 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7473 err = got_error_from_errno2("fclose", *path_new_staged_content);
7474 if (path1 && unlink(path1) == -1 && err == NULL)
7475 err = got_error_from_errno2("unlink", path1);
7476 if (path2 && unlink(path2) == -1 && err == NULL)
7477 err = got_error_from_errno2("unlink", path2);
7478 if (err || !have_content) {
7479 if (*path_unstaged_content &&
7480 unlink(*path_unstaged_content) == -1 && err == NULL)
7481 err = got_error_from_errno2("unlink",
7482 *path_unstaged_content);
7483 free(*path_unstaged_content);
7484 *path_unstaged_content = NULL;
7486 if (err || !have_content || !have_rejected_content) {
7487 if (*path_new_staged_content &&
7488 unlink(*path_new_staged_content) == -1 && err == NULL)
7489 err = got_error_from_errno2("unlink",
7490 *path_new_staged_content);
7491 free(*path_new_staged_content);
7492 *path_new_staged_content = NULL;
7496 got_diff_state_free(ds);
7500 got_diff_free_changes(changes);
7506 static const struct got_error *
7507 unstage_hunks(struct got_object_id *staged_blob_id,
7508 struct got_blob_object *blob_base,
7509 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
7510 const char *ondisk_path, const char *label_orig,
7511 struct got_worktree *worktree, struct got_repository *repo,
7512 got_worktree_patch_cb patch_cb, void *patch_arg,
7513 got_worktree_checkout_cb progress_cb, void *progress_arg)
7515 const struct got_error *err = NULL;
7516 char *path_unstaged_content = NULL;
7517 char *path_new_staged_content = NULL;
7518 struct got_object_id *new_staged_blob_id = NULL;
7522 err = create_unstaged_content(&path_unstaged_content,
7523 &path_new_staged_content, blob_id, staged_blob_id,
7524 ie->path, repo, patch_cb, patch_arg);
7528 if (path_unstaged_content == NULL)
7531 if (path_new_staged_content) {
7532 err = got_object_blob_create(&new_staged_blob_id,
7533 path_new_staged_content, repo);
7538 f = fopen(path_unstaged_content, "r");
7540 err = got_error_from_errno2("fopen",
7541 path_unstaged_content);
7544 if (fstat(fileno(f), &sb) == -1) {
7545 err = got_error_from_errno2("fstat", path_unstaged_content);
7548 if (got_fileindex_entry_staged_filetype_get(ie) ==
7549 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
7550 char link_target[PATH_MAX];
7552 r = fread(link_target, 1, sizeof(link_target), f);
7553 if (r == 0 && ferror(f)) {
7554 err = got_error_from_errno("fread");
7557 if (r >= sizeof(link_target)) { /* should not happen */
7558 err = got_error(GOT_ERR_NO_SPACE);
7561 link_target[r] = '\0';
7562 err = merge_symlink(worktree, blob_base,
7563 ondisk_path, ie->path, label_orig, link_target,
7564 worktree->base_commit_id, repo, progress_cb,
7567 int local_changes_subsumed;
7568 err = merge_file(&local_changes_subsumed, worktree,
7569 blob_base, ondisk_path, ie->path,
7570 got_fileindex_perms_to_st(ie),
7571 path_unstaged_content, label_orig, "unstaged",
7572 repo, progress_cb, progress_arg);
7577 if (new_staged_blob_id) {
7578 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7579 SHA1_DIGEST_LENGTH);
7581 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7583 free(new_staged_blob_id);
7584 if (path_unstaged_content &&
7585 unlink(path_unstaged_content) == -1 && err == NULL)
7586 err = got_error_from_errno2("unlink", path_unstaged_content);
7587 if (path_new_staged_content &&
7588 unlink(path_new_staged_content) == -1 && err == NULL)
7589 err = got_error_from_errno2("unlink", path_new_staged_content);
7590 if (f && fclose(f) != 0 && err == NULL)
7591 err = got_error_from_errno2("fclose", path_unstaged_content);
7592 free(path_unstaged_content);
7593 free(path_new_staged_content);
7597 static const struct got_error *
7598 unstage_path(void *arg, unsigned char status,
7599 unsigned char staged_status, const char *relpath,
7600 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7601 struct got_object_id *commit_id, int dirfd, const char *de_name)
7603 const struct got_error *err = NULL;
7604 struct unstage_path_arg *a = arg;
7605 struct got_fileindex_entry *ie;
7606 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7607 char *ondisk_path = NULL;
7608 char *id_str = NULL, *label_orig = NULL;
7609 int local_changes_subsumed;
7612 if (staged_status != GOT_STATUS_ADD &&
7613 staged_status != GOT_STATUS_MODIFY &&
7614 staged_status != GOT_STATUS_DELETE)
7617 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7619 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7621 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7623 return got_error_from_errno("asprintf");
7625 err = got_object_id_str(&id_str,
7626 commit_id ? commit_id : a->worktree->base_commit_id);
7629 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7631 err = got_error_from_errno("asprintf");
7635 switch (staged_status) {
7636 case GOT_STATUS_MODIFY:
7637 err = got_object_open_as_blob(&blob_base, a->repo,
7642 case GOT_STATUS_ADD:
7644 if (staged_status == GOT_STATUS_ADD) {
7645 int choice = GOT_PATCH_CHOICE_NONE;
7646 err = (*a->patch_cb)(&choice, a->patch_arg,
7647 staged_status, ie->path, NULL, 1, 1);
7650 if (choice != GOT_PATCH_CHOICE_YES)
7653 err = unstage_hunks(staged_blob_id,
7654 blob_base, blob_id, ie, ondisk_path,
7655 label_orig, a->worktree, a->repo,
7656 a->patch_cb, a->patch_arg,
7657 a->progress_cb, a->progress_arg);
7658 break; /* Done with this file. */
7661 err = got_object_open_as_blob(&blob_staged, a->repo,
7662 staged_blob_id, 8192);
7665 switch (got_fileindex_entry_staged_filetype_get(ie)) {
7666 case GOT_FILEIDX_MODE_BAD_SYMLINK:
7667 case GOT_FILEIDX_MODE_REGULAR_FILE:
7668 err = merge_blob(&local_changes_subsumed, a->worktree,
7669 blob_base, ondisk_path, relpath,
7670 got_fileindex_perms_to_st(ie), label_orig,
7671 blob_staged, commit_id ? commit_id :
7672 a->worktree->base_commit_id, a->repo,
7673 a->progress_cb, a->progress_arg);
7675 case GOT_FILEIDX_MODE_SYMLINK:
7676 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7677 char *staged_target;
7678 err = got_object_blob_read_to_str(
7679 &staged_target, blob_staged);
7682 err = merge_symlink(a->worktree, blob_base,
7683 ondisk_path, relpath, label_orig,
7684 staged_target, commit_id ? commit_id :
7685 a->worktree->base_commit_id,
7686 a->repo, a->progress_cb, a->progress_arg);
7687 free(staged_target);
7689 err = merge_blob(&local_changes_subsumed,
7690 a->worktree, blob_base, ondisk_path,
7691 relpath, got_fileindex_perms_to_st(ie),
7692 label_orig, blob_staged,
7693 commit_id ? commit_id :
7694 a->worktree->base_commit_id, a->repo,
7695 a->progress_cb, a->progress_arg);
7699 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
7703 got_fileindex_entry_stage_set(ie,
7704 GOT_FILEIDX_STAGE_NONE);
7706 case GOT_STATUS_DELETE:
7708 int choice = GOT_PATCH_CHOICE_NONE;
7709 err = (*a->patch_cb)(&choice, a->patch_arg,
7710 staged_status, ie->path, NULL, 1, 1);
7713 if (choice == GOT_PATCH_CHOICE_NO)
7715 if (choice != GOT_PATCH_CHOICE_YES) {
7716 err = got_error(GOT_ERR_PATCH_CHOICE);
7720 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7721 err = get_file_status(&status, &sb, ie, ondisk_path,
7722 dirfd, de_name, a->repo);
7725 err = (*a->progress_cb)(a->progress_arg, status, relpath);
7731 got_object_blob_close(blob_base);
7733 got_object_blob_close(blob_staged);
7739 const struct got_error *
7740 got_worktree_unstage(struct got_worktree *worktree,
7741 struct got_pathlist_head *paths,
7742 got_worktree_checkout_cb progress_cb, void *progress_arg,
7743 got_worktree_patch_cb patch_cb, void *patch_arg,
7744 struct got_repository *repo)
7746 const struct got_error *err = NULL, *sync_err, *unlockerr;
7747 struct got_pathlist_entry *pe;
7748 struct got_fileindex *fileindex = NULL;
7749 char *fileindex_path = NULL;
7750 struct unstage_path_arg upa;
7752 err = lock_worktree(worktree, LOCK_EX);
7756 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7760 upa.worktree = worktree;
7761 upa.fileindex = fileindex;
7763 upa.progress_cb = progress_cb;
7764 upa.progress_arg = progress_arg;
7765 upa.patch_cb = patch_cb;
7766 upa.patch_arg = patch_arg;
7767 TAILQ_FOREACH(pe, paths, entry) {
7768 err = worktree_status(worktree, pe->path, fileindex, repo,
7769 unstage_path, &upa, NULL, NULL, 0, 0);
7774 sync_err = sync_fileindex(fileindex, fileindex_path);
7775 if (sync_err && err == NULL)
7778 free(fileindex_path);
7780 got_fileindex_free(fileindex);
7781 unlockerr = lock_worktree(worktree, LOCK_SH);
7782 if (unlockerr && err == NULL)
7787 struct report_file_info_arg {
7788 struct got_worktree *worktree;
7789 got_worktree_path_info_cb info_cb;
7791 struct got_pathlist_head *paths;
7792 got_cancel_cb cancel_cb;
7796 static const struct got_error *
7797 report_file_info(void *arg, struct got_fileindex_entry *ie)
7799 struct report_file_info_arg *a = arg;
7800 struct got_pathlist_entry *pe;
7801 struct got_object_id blob_id, staged_blob_id, commit_id;
7802 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
7803 struct got_object_id *commit_idp = NULL;
7806 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
7807 return got_error(GOT_ERR_CANCELLED);
7809 TAILQ_FOREACH(pe, a->paths, entry) {
7810 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
7811 got_path_is_child(ie->path, pe->path, pe->path_len))
7814 if (pe == NULL) /* not found */
7817 if (got_fileindex_entry_has_blob(ie)) {
7818 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
7819 blob_idp = &blob_id;
7821 stage = got_fileindex_entry_stage_get(ie);
7822 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
7823 stage == GOT_FILEIDX_STAGE_ADD) {
7824 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
7825 SHA1_DIGEST_LENGTH);
7826 staged_blob_idp = &staged_blob_id;
7829 if (got_fileindex_entry_has_commit(ie)) {
7830 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
7831 commit_idp = &commit_id;
7834 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
7835 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
7838 const struct got_error *
7839 got_worktree_path_info(struct got_worktree *worktree,
7840 struct got_pathlist_head *paths,
7841 got_worktree_path_info_cb info_cb, void *info_arg,
7842 got_cancel_cb cancel_cb, void *cancel_arg)
7845 const struct got_error *err = NULL, *unlockerr;
7846 struct got_fileindex *fileindex = NULL;
7847 char *fileindex_path = NULL;
7848 struct report_file_info_arg arg;
7850 err = lock_worktree(worktree, LOCK_SH);
7854 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7858 arg.worktree = worktree;
7859 arg.info_cb = info_cb;
7860 arg.info_arg = info_arg;
7862 arg.cancel_cb = cancel_cb;
7863 arg.cancel_arg = cancel_arg;
7864 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
7867 free(fileindex_path);
7869 got_fileindex_free(fileindex);
7870 unlockerr = lock_worktree(worktree, LOCK_UN);
7871 if (unlockerr && err == NULL)