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 err = got_path_dirname(&parent, ondisk_path);
795 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
796 err = got_error_from_errno("asprintf");
800 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
805 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
806 err = got_error_from_errno("asprintf");
811 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
815 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
821 * If the file has no blob, this is an "add vs add" conflict,
822 * and we simply use an empty ancestor file to make both files
823 * appear in the merged result in their entirety.
828 * In order the run a 3-way merge with a symlink we copy the symlink's
829 * target path into a temporary file and use that file with diff3.
831 if (S_ISLNK(st_mode)) {
832 char target_path[PATH_MAX];
837 if (asprintf(&base_path, "%s/got-symlink-merge",
839 err = got_error_from_errno("asprintf");
843 err = got_opentemp_named(&symlink_path, &symlinkf, base_path);
846 target_len = readlink(ondisk_path, target_path,
847 sizeof(target_path));
848 if (target_len == -1) {
849 err = got_error_from_errno2("readlink", ondisk_path);
852 n = fwrite(target_path, 1, target_len, symlinkf);
853 if (n != target_len) {
854 err = got_ferror(symlinkf, GOT_ERR_IO);
857 if (fflush(symlinkf) == EOF) {
858 err = got_error_from_errno2("fflush", symlink_path);
863 err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
864 blob_orig_path, symlink_path ? symlink_path : ondisk_path,
865 label_deriv, label_orig, NULL);
869 err = (*progress_cb)(progress_arg,
870 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
874 if (fsync(merged_fd) != 0) {
875 err = got_error_from_errno("fsync");
879 /* Check if a clean merge has subsumed all local changes. */
880 if (overlapcnt == 0) {
881 err = check_files_equal(local_changes_subsumed, deriv_path,
887 if (fchmod(merged_fd, st_mode) != 0) {
888 err = got_error_from_errno2("fchmod", merged_path);
892 if (rename(merged_path, ondisk_path) != 0) {
893 err = got_error_from_errno3("rename", merged_path,
903 if (unlink(symlink_path) == -1 && err == NULL)
904 err = got_error_from_errno2("unlink", symlink_path);
906 if (symlinkf && fclose(symlinkf) == EOF && err == NULL)
907 err = got_error_from_errno2("fclose", symlink_path);
909 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
910 err = got_error_from_errno("close");
911 if (f_orig && fclose(f_orig) != 0 && err == NULL)
912 err = got_error_from_errno("fclose");
915 if (blob_orig_path) {
916 unlink(blob_orig_path);
917 free(blob_orig_path);
923 static const struct got_error *
924 update_symlink(const char *ondisk_path, const char *target_path,
927 /* This is not atomic but matches what 'ln -sf' does. */
928 if (unlink(ondisk_path) == -1)
929 return got_error_from_errno2("unlink", ondisk_path);
930 if (symlink(target_path, ondisk_path) == -1)
931 return got_error_from_errno3("symlink", target_path,
937 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
938 * in the work tree with a file that contains conflict markers and the
939 * conflicting target paths of the original version, a "derived version"
940 * of a symlink from an incoming change, and a local version of the symlink.
942 * The original versions's target path can be NULL if it is not available,
943 * such as if both derived versions added a new symlink at the same path.
945 * The incoming derived symlink target is NULL in case the incoming change
946 * has deleted this symlink.
948 static const struct got_error *
949 install_symlink_conflict(const char *deriv_target,
950 struct got_object_id *deriv_base_commit_id, const char *orig_target,
951 const char *label_orig, const char *local_target, const char *ondisk_path)
953 const struct got_error *err;
954 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
957 err = got_object_id_str(&id_str, deriv_base_commit_id);
959 return got_error_from_errno("asprintf");
961 if (asprintf(&label_deriv, "%s: commit %s",
962 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
963 err = got_error_from_errno("asprintf");
967 err = got_opentemp_named(&path, &f, "got-symlink-conflict");
971 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
972 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
973 deriv_target ? deriv_target : "(symlink was deleted)",
974 orig_target ? label_orig : "",
975 orig_target ? "\n" : "",
976 orig_target ? orig_target : "",
977 orig_target ? "\n" : "",
978 GOT_DIFF_CONFLICT_MARKER_SEP,
979 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
980 err = got_error_from_errno2("fprintf", path);
984 if (unlink(ondisk_path) == -1) {
985 err = got_error_from_errno2("unlink", ondisk_path);
988 if (rename(path, ondisk_path) == -1) {
989 err = got_error_from_errno3("rename", path, ondisk_path);
992 if (chmod(ondisk_path, GOT_DEFAULT_FILE_MODE) == -1) {
993 err = got_error_from_errno2("chmod", ondisk_path);
997 if (f != NULL && fclose(f) == EOF && err == NULL)
998 err = got_error_from_errno2("fclose", path);
1005 /* forward declaration */
1006 static const struct got_error *
1007 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
1008 const char *, const char *, uint16_t, const char *,
1009 struct got_blob_object *, struct got_object_id *,
1010 struct got_repository *, got_worktree_checkout_cb, void *);
1013 * Merge a symlink into the work tree, where blob_orig acts as the common
1014 * ancestor, deriv_target is the link target of the first derived version,
1015 * and the symlink on disk acts as the second derived version.
1016 * Assume that contents of both blobs represent symlinks.
1018 static const struct got_error *
1019 merge_symlink(struct got_worktree *worktree,
1020 struct got_blob_object *blob_orig, const char *ondisk_path,
1021 const char *path, const char *label_orig, const char *deriv_target,
1022 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1023 got_worktree_checkout_cb progress_cb, void *progress_arg)
1025 const struct got_error *err = NULL;
1026 char *ancestor_target = NULL;
1028 ssize_t ondisk_len, deriv_len;
1029 char ondisk_target[PATH_MAX];
1030 int have_local_change = 0;
1031 int have_incoming_change = 0;
1033 if (lstat(ondisk_path, &sb) == -1)
1034 return got_error_from_errno2("lstat", ondisk_path);
1036 ondisk_len = readlink(ondisk_path, ondisk_target,
1037 sizeof(ondisk_target));
1038 if (ondisk_len == -1) {
1039 err = got_error_from_errno2("readlink",
1043 ondisk_target[ondisk_len] = '\0';
1046 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
1051 if (ancestor_target == NULL ||
1052 (ondisk_len != strlen(ancestor_target) ||
1053 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
1054 have_local_change = 1;
1056 deriv_len = strlen(deriv_target);
1057 if (ancestor_target == NULL ||
1058 (deriv_len != strlen(ancestor_target) ||
1059 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
1060 have_incoming_change = 1;
1062 if (!have_local_change && !have_incoming_change) {
1063 if (ancestor_target) {
1064 /* Both sides made the same change. */
1065 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1067 } else if (deriv_len == ondisk_len &&
1068 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1069 /* Both sides added the same symlink. */
1070 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1073 /* Both sides added symlinks which don't match. */
1074 err = install_symlink_conflict(deriv_target,
1075 deriv_base_commit_id, ancestor_target,
1076 label_orig, ondisk_target, ondisk_path);
1079 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1082 } else if (!have_local_change && have_incoming_change) {
1083 /* Apply the incoming change. */
1084 err = update_symlink(ondisk_path, deriv_target,
1085 strlen(deriv_target));
1088 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1089 } else if (have_local_change && have_incoming_change) {
1090 if (deriv_len == ondisk_len &&
1091 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
1092 /* Both sides made the same change. */
1093 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1096 err = install_symlink_conflict(deriv_target,
1097 deriv_base_commit_id, ancestor_target, label_orig,
1098 ondisk_target, ondisk_path);
1101 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1107 free(ancestor_target);
1112 * Perform a 3-way merge where blob_orig acts as the common ancestor,
1113 * blob_deriv acts as the first derived version, and the file on disk
1114 * acts as the second derived version.
1116 static const struct got_error *
1117 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1118 struct got_blob_object *blob_orig, const char *ondisk_path,
1119 const char *path, uint16_t st_mode, const char *label_orig,
1120 struct got_blob_object *blob_deriv,
1121 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1122 got_worktree_checkout_cb progress_cb, void *progress_arg)
1124 const struct got_error *err = NULL;
1125 FILE *f_deriv = NULL;
1126 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1127 char *label_deriv = NULL, *parent = NULL;
1129 *local_changes_subsumed = 0;
1131 err = got_path_dirname(&parent, ondisk_path);
1136 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1137 err = got_error_from_errno("asprintf");
1142 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1145 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1150 err = got_object_id_str(&id_str, deriv_base_commit_id);
1153 if (asprintf(&label_deriv, "%s: commit %s",
1154 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1155 err = got_error_from_errno("asprintf");
1159 err = merge_file(local_changes_subsumed, worktree, blob_orig,
1160 ondisk_path, path, st_mode, blob_deriv_path, label_orig,
1161 label_deriv, repo, progress_cb, progress_arg);
1163 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
1164 err = got_error_from_errno("fclose");
1166 if (blob_deriv_path) {
1167 unlink(blob_deriv_path);
1168 free(blob_deriv_path);
1176 static const struct got_error *
1177 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1178 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1179 const char *ondisk_path, const char *path, struct got_object_id *blob_id)
1181 const struct got_error *err = NULL;
1182 struct got_fileindex_entry *new_ie;
1186 err = got_fileindex_entry_alloc(&new_ie, path);
1190 err = got_fileindex_entry_update(new_ie, ondisk_path,
1191 blob_id->sha1, base_commit_id->sha1, 1);
1195 err = got_fileindex_entry_add(fileindex, new_ie);
1198 got_fileindex_entry_free(new_ie);
1205 get_ondisk_perms(int executable, mode_t st_mode)
1207 mode_t xbits = S_IXUSR;
1210 /* Map read bits to execute bits. */
1211 if (st_mode & S_IRGRP)
1213 if (st_mode & S_IROTH)
1215 return st_mode | xbits;
1218 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1221 /* forward declaration */
1222 static const struct got_error *
1223 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1224 const char *path, mode_t te_mode, mode_t st_mode,
1225 struct got_blob_object *blob, int restoring_missing_file,
1226 int reverting_versioned_file, int installing_bad_symlink,
1227 int path_is_unversioned, struct got_repository *repo,
1228 got_worktree_checkout_cb progress_cb, void *progress_arg);
1231 * This function assumes that the provided symlink target points at a
1232 * safe location in the work tree!
1234 static const struct got_error *
1235 replace_existing_symlink(const char *ondisk_path, const char *target_path,
1238 const struct got_error *err = NULL;
1240 char etarget[PATH_MAX];
1244 * "Bad" symlinks (those pointing outside the work tree or into the
1245 * .got directory) are installed in the work tree as a regular file
1246 * which contains the bad symlink target path.
1247 * The new symlink target has already been checked for safety by our
1248 * caller. If we can successfully open a regular file then we simply
1249 * replace this file with a symlink below.
1251 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1254 return got_error_from_errno2("open", ondisk_path);
1256 /* We are updating an existing on-disk symlink. */
1257 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1259 return got_error_from_errno2("readlink", ondisk_path);
1261 if (elen == target_len &&
1262 memcmp(etarget, target_path, target_len) == 0)
1263 return NULL; /* nothing to do */
1266 err = update_symlink(ondisk_path, target_path, target_len);
1267 if (fd != -1 && close(fd) == -1 && err == NULL)
1268 err = got_error_from_errno2("close", ondisk_path);
1272 static const struct got_error *
1273 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1274 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1276 const struct got_error *err = NULL;
1277 char canonpath[PATH_MAX];
1278 char *path_got = NULL;
1280 *is_bad_symlink = 0;
1282 if (target_len >= sizeof(canonpath)) {
1283 *is_bad_symlink = 1;
1288 * We do not use realpath(3) to resolve the symlink's target
1289 * path because we don't want to resolve symlinks recursively.
1290 * Instead we make the path absolute and then canonicalize it.
1291 * Relative symlink target lookup should begin at the directory
1292 * in which the blob object is being installed.
1294 if (!got_path_is_absolute(target_path)) {
1295 char *abspath, *parent;
1296 err = got_path_dirname(&parent, ondisk_path);
1299 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1301 return got_error_from_errno("asprintf");
1304 if (strlen(abspath) >= sizeof(canonpath)) {
1305 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1309 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1314 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1319 /* Only allow symlinks pointing at paths within the work tree. */
1320 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1321 *is_bad_symlink = 1;
1325 /* Do not allow symlinks pointing into the .got directory. */
1326 if (asprintf(&path_got, "%s/%s", wtroot_path,
1327 GOT_WORKTREE_GOT_DIR) == -1)
1328 return got_error_from_errno("asprintf");
1329 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1330 *is_bad_symlink = 1;
1336 static const struct got_error *
1337 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1338 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1339 int restoring_missing_file, int reverting_versioned_file,
1340 int path_is_unversioned, struct got_repository *repo,
1341 got_worktree_checkout_cb progress_cb, void *progress_arg)
1343 const struct got_error *err = NULL;
1344 char target_path[PATH_MAX];
1345 size_t len, target_len = 0;
1346 char *path_got = NULL;
1347 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1348 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1350 *is_bad_symlink = 0;
1353 * Blob object content specifies the target path of the link.
1354 * If a symbolic link cannot be installed we instead create
1355 * a regular file which contains the link target path stored
1356 * in the blob object.
1359 err = got_object_blob_read_block(&len, blob);
1360 if (len + target_len >= sizeof(target_path)) {
1361 /* Path too long; install as a regular file. */
1362 *is_bad_symlink = 1;
1363 got_object_blob_rewind(blob);
1364 return install_blob(worktree, ondisk_path, path,
1365 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1366 restoring_missing_file, reverting_versioned_file,
1367 1, path_is_unversioned, repo, progress_cb,
1371 /* Skip blob object header first time around. */
1372 memcpy(target_path + target_len, buf + hdrlen,
1374 target_len += len - hdrlen;
1378 target_path[target_len] = '\0';
1380 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1381 ondisk_path, worktree->root_path);
1385 if (*is_bad_symlink) {
1386 /* install as a regular file */
1387 *is_bad_symlink = 1;
1388 got_object_blob_rewind(blob);
1389 err = install_blob(worktree, ondisk_path, path,
1390 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1391 restoring_missing_file, reverting_versioned_file, 1,
1392 path_is_unversioned, repo, progress_cb, progress_arg);
1396 if (symlink(target_path, ondisk_path) == -1) {
1397 if (errno == EEXIST) {
1398 if (path_is_unversioned) {
1399 err = (*progress_cb)(progress_arg,
1400 GOT_STATUS_UNVERSIONED, path);
1403 err = replace_existing_symlink(ondisk_path,
1404 target_path, target_len);
1408 err = (*progress_cb)(progress_arg,
1409 reverting_versioned_file ?
1410 GOT_STATUS_REVERT : GOT_STATUS_UPDATE,
1413 goto done; /* Nothing else to do. */
1416 if (errno == ENOENT) {
1418 err = got_path_dirname(&parent, ondisk_path);
1421 err = add_dir_on_disk(worktree, parent);
1426 * Retry, and fall through to error handling
1427 * below if this second attempt fails.
1429 if (symlink(target_path, ondisk_path) != -1) {
1430 err = NULL; /* success */
1435 /* Handle errors from first or second creation attempt. */
1436 if (errno == ENAMETOOLONG) {
1437 /* bad target path; install as a regular file */
1438 *is_bad_symlink = 1;
1439 got_object_blob_rewind(blob);
1440 err = install_blob(worktree, ondisk_path, path,
1441 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1442 restoring_missing_file, reverting_versioned_file, 1,
1443 path_is_unversioned, repo,
1444 progress_cb, progress_arg);
1445 } else if (errno == ENOTDIR) {
1446 err = got_error_path(ondisk_path,
1447 GOT_ERR_FILE_OBSTRUCTED);
1449 err = got_error_from_errno3("symlink",
1450 target_path, ondisk_path);
1452 } else if (progress_cb)
1453 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1454 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1460 static const struct got_error *
1461 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1462 const char *path, mode_t te_mode, mode_t st_mode,
1463 struct got_blob_object *blob, int restoring_missing_file,
1464 int reverting_versioned_file, int installing_bad_symlink,
1465 int path_is_unversioned, struct got_repository *repo,
1466 got_worktree_checkout_cb progress_cb, void *progress_arg)
1468 const struct got_error *err = NULL;
1472 char *tmppath = NULL;
1474 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1475 GOT_DEFAULT_FILE_MODE);
1477 if (errno == ENOENT) {
1479 err = got_path_dirname(&parent, path);
1482 err = add_dir_on_disk(worktree, parent);
1486 fd = open(ondisk_path,
1487 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1488 GOT_DEFAULT_FILE_MODE);
1490 return got_error_from_errno2("open",
1492 } else if (errno == EEXIST) {
1493 if (path_is_unversioned) {
1494 err = (*progress_cb)(progress_arg,
1495 GOT_STATUS_UNVERSIONED, path);
1498 if (!S_ISREG(st_mode) && !installing_bad_symlink) {
1499 /* TODO file is obstructed; do something */
1500 err = got_error_path(ondisk_path,
1501 GOT_ERR_FILE_OBSTRUCTED);
1504 err = got_opentemp_named_fd(&tmppath, &fd,
1511 return got_error_from_errno2("open", ondisk_path);
1515 if (restoring_missing_file)
1516 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1518 else if (reverting_versioned_file)
1519 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1522 err = (*progress_cb)(progress_arg,
1523 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1528 hdrlen = got_object_blob_get_hdrlen(blob);
1530 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1531 err = got_object_blob_read_block(&len, blob);
1535 /* Skip blob object header first time around. */
1536 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1538 err = got_error_from_errno("write");
1540 } else if (outlen != len - hdrlen) {
1541 err = got_error(GOT_ERR_IO);
1548 if (fsync(fd) != 0) {
1549 err = got_error_from_errno("fsync");
1554 if (rename(tmppath, ondisk_path) != 0) {
1555 err = got_error_from_errno3("rename", tmppath,
1562 if (chmod(ondisk_path,
1563 get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1564 err = got_error_from_errno2("chmod", ondisk_path);
1569 if (fd != -1 && close(fd) != 0 && err == NULL)
1570 err = got_error_from_errno("close");
1575 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1576 static const struct got_error *
1577 get_modified_file_content_status(unsigned char *status, FILE *f)
1579 const struct got_error *err = NULL;
1580 const char *markers[3] = {
1581 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1582 GOT_DIFF_CONFLICT_MARKER_SEP,
1583 GOT_DIFF_CONFLICT_MARKER_END
1588 const char delim[3] = {'\0', '\0', '\0'};
1590 while (*status == GOT_STATUS_MODIFY) {
1591 line = fparseln(f, &len, NULL, delim, 0);
1595 err = got_ferror(f, GOT_ERR_IO);
1599 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1600 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1602 *status = GOT_STATUS_CONFLICT;
1612 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1614 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1615 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1619 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1621 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1622 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1623 ie->mtime_sec == sb->st_mtim.tv_sec &&
1624 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1625 ie->size == (sb->st_size & 0xffffffff) &&
1626 !xbit_differs(ie, sb->st_mode));
1629 static unsigned char
1630 get_staged_status(struct got_fileindex_entry *ie)
1632 switch (got_fileindex_entry_stage_get(ie)) {
1633 case GOT_FILEIDX_STAGE_ADD:
1634 return GOT_STATUS_ADD;
1635 case GOT_FILEIDX_STAGE_DELETE:
1636 return GOT_STATUS_DELETE;
1637 case GOT_FILEIDX_STAGE_MODIFY:
1638 return GOT_STATUS_MODIFY;
1640 return GOT_STATUS_NO_CHANGE;
1644 static const struct got_error *
1645 get_symlink_modification_status(unsigned char *status,
1646 struct got_fileindex_entry *ie, const char *abspath,
1647 int dirfd, const char *de_name, struct got_blob_object *blob)
1649 const struct got_error *err = NULL;
1650 char target_path[PATH_MAX];
1651 char etarget[PATH_MAX];
1653 size_t len, target_len = 0;
1654 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1655 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1657 *status = GOT_STATUS_NO_CHANGE;
1659 /* Blob object content specifies the target path of the link. */
1661 err = got_object_blob_read_block(&len, blob);
1664 if (len + target_len >= sizeof(target_path)) {
1666 * Should not happen. The blob contents were OK
1667 * when this symlink was installed.
1669 return got_error(GOT_ERR_NO_SPACE);
1672 /* Skip blob object header first time around. */
1673 memcpy(target_path + target_len, buf + hdrlen,
1675 target_len += len - hdrlen;
1679 target_path[target_len] = '\0';
1682 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1684 return got_error_from_errno2("readlinkat", abspath);
1686 elen = readlink(abspath, etarget, sizeof(etarget));
1688 return got_error_from_errno2("readlink", abspath);
1691 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1692 *status = GOT_STATUS_MODIFY;
1697 static const struct got_error *
1698 get_file_status(unsigned char *status, struct stat *sb,
1699 struct got_fileindex_entry *ie, const char *abspath,
1700 int dirfd, const char *de_name, struct got_repository *repo)
1702 const struct got_error *err = NULL;
1703 struct got_object_id id;
1708 struct got_blob_object *blob = NULL;
1710 unsigned char staged_status = get_staged_status(ie);
1712 *status = GOT_STATUS_NO_CHANGE;
1715 * Whenever the caller provides a directory descriptor and a
1716 * directory entry name for the file, use them! This prevents
1717 * race conditions if filesystem paths change beneath our feet.
1720 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1721 if (errno == ENOENT) {
1722 if (got_fileindex_entry_has_file_on_disk(ie))
1723 *status = GOT_STATUS_MISSING;
1725 *status = GOT_STATUS_DELETE;
1728 err = got_error_from_errno2("fstatat", abspath);
1732 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1733 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1734 return got_error_from_errno2("open", abspath);
1735 else if (fd == -1 && errno == ELOOP) {
1736 if (lstat(abspath, sb) == -1)
1737 return got_error_from_errno2("lstat", abspath);
1738 } else if (fd == -1 || fstat(fd, sb) == -1) {
1739 if (errno == ENOENT) {
1740 if (got_fileindex_entry_has_file_on_disk(ie))
1741 *status = GOT_STATUS_MISSING;
1743 *status = GOT_STATUS_DELETE;
1746 err = got_error_from_errno2("fstat", abspath);
1751 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1752 *status = GOT_STATUS_OBSTRUCTED;
1756 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1757 *status = GOT_STATUS_DELETE;
1759 } else if (!got_fileindex_entry_has_blob(ie) &&
1760 staged_status != GOT_STATUS_ADD) {
1761 *status = GOT_STATUS_ADD;
1765 if (!stat_info_differs(ie, sb))
1768 if (S_ISLNK(sb->st_mode) &&
1769 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1770 *status = GOT_STATUS_MODIFY;
1774 if (staged_status == GOT_STATUS_MODIFY ||
1775 staged_status == GOT_STATUS_ADD)
1776 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1778 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1780 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1784 if (S_ISLNK(sb->st_mode)) {
1785 err = get_symlink_modification_status(status, ie,
1786 abspath, dirfd, de_name, blob);
1791 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1793 err = got_error_from_errno2("openat", abspath);
1798 f = fdopen(fd, "r");
1800 err = got_error_from_errno2("fdopen", abspath);
1804 hdrlen = got_object_blob_get_hdrlen(blob);
1806 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1807 err = got_object_blob_read_block(&blen, blob);
1810 /* Skip length of blob object header first time around. */
1811 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1812 if (flen == 0 && ferror(f)) {
1813 err = got_error_from_errno("fread");
1816 if (blen - hdrlen == 0) {
1818 *status = GOT_STATUS_MODIFY;
1820 } else if (flen == 0) {
1821 if (blen - hdrlen != 0)
1822 *status = GOT_STATUS_MODIFY;
1824 } else if (blen - hdrlen == flen) {
1825 /* Skip blob object header first time around. */
1826 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1827 *status = GOT_STATUS_MODIFY;
1831 *status = GOT_STATUS_MODIFY;
1837 if (*status == GOT_STATUS_MODIFY) {
1839 err = get_modified_file_content_status(status, f);
1840 } else if (xbit_differs(ie, sb->st_mode))
1841 *status = GOT_STATUS_MODE_CHANGE;
1844 got_object_blob_close(blob);
1845 if (f != NULL && fclose(f) == EOF && err == NULL)
1846 err = got_error_from_errno2("fclose", abspath);
1847 if (fd != -1 && close(fd) == -1 && err == NULL)
1848 err = got_error_from_errno2("close", abspath);
1853 * Update timestamps in the file index if a file is unmodified and
1854 * we had to run a full content comparison to find out.
1856 static const struct got_error *
1857 sync_timestamps(char *ondisk_path, unsigned char status,
1858 struct got_fileindex_entry *ie, struct stat *sb)
1860 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1861 return got_fileindex_entry_update(ie, ondisk_path,
1862 ie->blob_sha1, ie->commit_sha1, 1);
1867 static const struct got_error *
1868 update_blob(struct got_worktree *worktree,
1869 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1870 struct got_tree_entry *te, const char *path,
1871 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1874 const struct got_error *err = NULL;
1875 struct got_blob_object *blob = NULL;
1877 unsigned char status = GOT_STATUS_NO_CHANGE;
1880 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1881 return got_error_from_errno("asprintf");
1884 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1885 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1888 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1892 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1893 sb.st_mode = got_fileindex_perms_to_st(ie);
1895 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1896 status = GOT_STATUS_UNVERSIONED;
1899 if (status == GOT_STATUS_OBSTRUCTED) {
1900 err = (*progress_cb)(progress_arg, status, path);
1903 if (status == GOT_STATUS_CONFLICT) {
1904 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1909 if (ie && status != GOT_STATUS_MISSING &&
1910 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1911 if (got_fileindex_entry_has_commit(ie) &&
1912 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1913 SHA1_DIGEST_LENGTH) == 0) {
1914 err = sync_timestamps(ondisk_path, status, ie, &sb);
1917 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1921 if (got_fileindex_entry_has_blob(ie) &&
1922 memcmp(ie->blob_sha1, te->id.sha1,
1923 SHA1_DIGEST_LENGTH) == 0) {
1924 err = sync_timestamps(ondisk_path, status, ie, &sb);
1929 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1933 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1934 int update_timestamps;
1935 struct got_blob_object *blob2 = NULL;
1936 char *label_orig = NULL;
1937 if (got_fileindex_entry_has_blob(ie)) {
1938 struct got_object_id id2;
1939 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1940 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1944 if (got_fileindex_entry_has_commit(ie)) {
1945 char id_str[SHA1_DIGEST_STRING_LENGTH];
1946 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1947 sizeof(id_str)) == NULL) {
1948 err = got_error_path(id_str,
1949 GOT_ERR_BAD_OBJ_ID_STR);
1952 if (asprintf(&label_orig, "%s: commit %s",
1953 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1954 err = got_error_from_errno("asprintf");
1958 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1960 err = got_object_blob_read_to_str(&link_target, blob);
1963 err = merge_symlink(worktree, blob2, ondisk_path, path,
1964 label_orig, link_target, worktree->base_commit_id,
1965 repo, progress_cb, progress_arg);
1968 err = merge_blob(&update_timestamps, worktree, blob2,
1969 ondisk_path, path, sb.st_mode, label_orig, blob,
1970 worktree->base_commit_id, repo,
1971 progress_cb, progress_arg);
1975 got_object_blob_close(blob2);
1979 * Do not update timestamps of files with local changes.
1980 * Otherwise, a future status walk would treat them as
1981 * unmodified files again.
1983 err = got_fileindex_entry_update(ie, ondisk_path,
1984 blob->id.sha1, worktree->base_commit_id->sha1,
1986 } else if (status == GOT_STATUS_MODE_CHANGE) {
1987 err = got_fileindex_entry_update(ie, ondisk_path,
1988 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1989 } else if (status == GOT_STATUS_DELETE) {
1990 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1993 err = got_fileindex_entry_update(ie, ondisk_path,
1994 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1998 int is_bad_symlink = 0;
1999 if (S_ISLNK(te->mode)) {
2000 err = install_symlink(&is_bad_symlink, worktree,
2001 ondisk_path, path, blob,
2002 status == GOT_STATUS_MISSING, 0,
2003 status == GOT_STATUS_UNVERSIONED, repo,
2004 progress_cb, progress_arg);
2006 err = install_blob(worktree, ondisk_path, path,
2007 te->mode, sb.st_mode, blob,
2008 status == GOT_STATUS_MISSING, 0, 0,
2009 status == GOT_STATUS_UNVERSIONED, repo,
2010 progress_cb, progress_arg);
2016 err = got_fileindex_entry_update(ie, ondisk_path,
2017 blob->id.sha1, worktree->base_commit_id->sha1, 1);
2019 err = create_fileindex_entry(&ie, fileindex,
2020 worktree->base_commit_id, ondisk_path, path,
2026 if (is_bad_symlink) {
2027 got_fileindex_entry_filetype_set(ie,
2028 GOT_FILEIDX_MODE_BAD_SYMLINK);
2031 got_object_blob_close(blob);
2037 static const struct got_error *
2038 remove_ondisk_file(const char *root_path, const char *path)
2040 const struct got_error *err = NULL;
2041 char *ondisk_path = NULL;
2043 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2044 return got_error_from_errno("asprintf");
2046 if (unlink(ondisk_path) == -1) {
2047 if (errno != ENOENT)
2048 err = got_error_from_errno2("unlink", ondisk_path);
2050 char *parent = dirname(ondisk_path);
2051 while (parent && strcmp(parent, root_path) != 0) {
2052 if (rmdir(parent) == -1) {
2053 if (errno != ENOTEMPTY)
2054 err = got_error_from_errno2("rmdir",
2058 parent = dirname(parent);
2065 static const struct got_error *
2066 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2067 struct got_fileindex_entry *ie, struct got_repository *repo,
2068 got_worktree_checkout_cb progress_cb, void *progress_arg)
2070 const struct got_error *err = NULL;
2071 unsigned char status;
2075 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2076 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2078 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2080 return got_error_from_errno("asprintf");
2082 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2086 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2087 char ondisk_target[PATH_MAX];
2088 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2089 sizeof(ondisk_target));
2090 if (ondisk_len == -1) {
2091 err = got_error_from_errno2("readlink", ondisk_path);
2094 ondisk_target[ondisk_len] = '\0';
2095 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2096 NULL, NULL, /* XXX pass common ancestor info? */
2097 ondisk_target, ondisk_path);
2100 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2105 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2106 status == GOT_STATUS_ADD) {
2107 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2111 * Preserve the working file and change the deleted blob's
2112 * entry into a schedule-add entry.
2114 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
2117 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2120 if (status == GOT_STATUS_NO_CHANGE) {
2121 err = remove_ondisk_file(worktree->root_path, ie->path);
2125 got_fileindex_entry_remove(fileindex, ie);
2132 struct diff_cb_arg {
2133 struct got_fileindex *fileindex;
2134 struct got_worktree *worktree;
2135 struct got_repository *repo;
2136 got_worktree_checkout_cb progress_cb;
2138 got_cancel_cb cancel_cb;
2142 static const struct got_error *
2143 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2144 struct got_tree_entry *te, const char *parent_path)
2146 struct diff_cb_arg *a = arg;
2148 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2149 return got_error(GOT_ERR_CANCELLED);
2151 return update_blob(a->worktree, a->fileindex, ie, te,
2152 ie->path, a->repo, a->progress_cb, a->progress_arg);
2155 static const struct got_error *
2156 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2158 struct diff_cb_arg *a = arg;
2160 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2161 return got_error(GOT_ERR_CANCELLED);
2163 return delete_blob(a->worktree, a->fileindex, ie,
2164 a->repo, a->progress_cb, a->progress_arg);
2167 static const struct got_error *
2168 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2170 struct diff_cb_arg *a = arg;
2171 const struct got_error *err;
2174 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2175 return got_error(GOT_ERR_CANCELLED);
2177 if (got_object_tree_entry_is_submodule(te))
2180 if (asprintf(&path, "%s%s%s", parent_path,
2181 parent_path[0] ? "/" : "", te->name)
2183 return got_error_from_errno("asprintf");
2185 if (S_ISDIR(te->mode))
2186 err = add_dir_on_disk(a->worktree, path);
2188 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2189 a->repo, a->progress_cb, a->progress_arg);
2195 const struct got_error *
2196 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2198 uint32_t uuid_status;
2200 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2201 if (uuid_status != uuid_s_ok) {
2203 return got_error_uuid(uuid_status, "uuid_to_string");
2209 static const struct got_error *
2210 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2212 const struct got_error *err = NULL;
2213 char *uuidstr = NULL;
2217 err = got_worktree_get_uuid(&uuidstr, worktree);
2221 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2222 err = got_error_from_errno("asprintf");
2229 const struct got_error *
2230 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2232 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2235 static const struct got_error *
2236 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2238 return get_ref_name(refname, worktree,
2239 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2242 static const struct got_error *
2243 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2245 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2248 static const struct got_error *
2249 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2251 return get_ref_name(refname, worktree,
2252 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2255 static const struct got_error *
2256 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2258 return get_ref_name(refname, worktree,
2259 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2262 static const struct got_error *
2263 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2265 return get_ref_name(refname, worktree,
2266 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2269 static const struct got_error *
2270 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2272 return get_ref_name(refname, worktree,
2273 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2276 static const struct got_error *
2277 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2279 return get_ref_name(refname, worktree,
2280 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2283 static const struct got_error *
2284 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2286 return get_ref_name(refname, worktree,
2287 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2290 const struct got_error *
2291 got_worktree_get_histedit_script_path(char **path,
2292 struct got_worktree *worktree)
2294 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2295 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2297 return got_error_from_errno("asprintf");
2303 * Prevent Git's garbage collector from deleting our base commit by
2304 * setting a reference to our base commit's ID.
2306 static const struct got_error *
2307 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2309 const struct got_error *err = NULL;
2310 struct got_reference *ref = NULL;
2313 err = got_worktree_get_base_ref_name(&refname, worktree);
2317 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2321 err = got_ref_write(ref, repo);
2329 static const struct got_error *
2330 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2332 const struct got_error *err = NULL;
2334 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2335 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2336 err = got_error_from_errno("asprintf");
2337 *fileindex_path = NULL;
2343 static const struct got_error *
2344 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2345 struct got_worktree *worktree)
2347 const struct got_error *err = NULL;
2350 *fileindex_path = NULL;
2351 *fileindex = got_fileindex_alloc();
2352 if (*fileindex == NULL)
2353 return got_error_from_errno("got_fileindex_alloc");
2355 err = get_fileindex_path(fileindex_path, worktree);
2359 index = fopen(*fileindex_path, "rb");
2360 if (index == NULL) {
2361 if (errno != ENOENT)
2362 err = got_error_from_errno2("fopen", *fileindex_path);
2364 err = got_fileindex_read(*fileindex, index);
2365 if (fclose(index) != 0 && err == NULL)
2366 err = got_error_from_errno("fclose");
2370 free(*fileindex_path);
2371 *fileindex_path = NULL;
2372 got_fileindex_free(*fileindex);
2378 struct bump_base_commit_id_arg {
2379 struct got_object_id *base_commit_id;
2382 const char *entry_name;
2383 got_worktree_checkout_cb progress_cb;
2387 /* Bump base commit ID of all files within an updated part of the work tree. */
2388 static const struct got_error *
2389 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2391 const struct got_error *err;
2392 struct bump_base_commit_id_arg *a = arg;
2394 if (a->entry_name) {
2395 if (strcmp(ie->path, a->path) != 0)
2397 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2400 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2401 SHA1_DIGEST_LENGTH) == 0)
2404 if (a->progress_cb) {
2405 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2410 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2414 static const struct got_error *
2415 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2417 const struct got_error *err = NULL;
2418 char *new_fileindex_path = NULL;
2419 FILE *new_index = NULL;
2420 struct timespec timeout;
2422 err = got_opentemp_named(&new_fileindex_path, &new_index,
2427 err = got_fileindex_write(fileindex, new_index);
2431 if (rename(new_fileindex_path, fileindex_path) != 0) {
2432 err = got_error_from_errno3("rename", new_fileindex_path,
2434 unlink(new_fileindex_path);
2438 * Sleep for a short amount of time to ensure that files modified after
2439 * this program exits have a different time stamp from the one which
2440 * was recorded in the file index.
2443 timeout.tv_nsec = 1;
2444 nanosleep(&timeout, NULL);
2448 free(new_fileindex_path);
2452 static const struct got_error *
2453 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2454 struct got_object_id **tree_id, const char *wt_relpath,
2455 struct got_worktree *worktree, struct got_repository *repo)
2457 const struct got_error *err = NULL;
2458 struct got_object_id *id = NULL;
2459 char *in_repo_path = NULL;
2460 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2462 *entry_type = GOT_OBJ_TYPE_ANY;
2463 *tree_relpath = NULL;
2466 if (wt_relpath[0] == '\0') {
2467 /* Check out all files within the work tree. */
2468 *entry_type = GOT_OBJ_TYPE_TREE;
2469 *tree_relpath = strdup("");
2470 if (*tree_relpath == NULL) {
2471 err = got_error_from_errno("strdup");
2474 err = got_object_id_by_path(tree_id, repo,
2475 worktree->base_commit_id, worktree->path_prefix);
2481 /* Check out a subset of files in the work tree. */
2483 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2484 is_root_wt ? "" : "/", wt_relpath) == -1) {
2485 err = got_error_from_errno("asprintf");
2489 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2495 in_repo_path = NULL;
2497 err = got_object_get_type(entry_type, repo, id);
2501 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2502 /* Check out a single file. */
2503 if (strchr(wt_relpath, '/') == NULL) {
2504 /* Check out a single file in work tree's root dir. */
2505 in_repo_path = strdup(worktree->path_prefix);
2506 if (in_repo_path == NULL) {
2507 err = got_error_from_errno("strdup");
2510 *tree_relpath = strdup("");
2511 if (*tree_relpath == NULL) {
2512 err = got_error_from_errno("strdup");
2516 /* Check out a single file in a subdirectory. */
2517 err = got_path_dirname(tree_relpath, wt_relpath);
2520 if (asprintf(&in_repo_path, "%s%s%s",
2521 worktree->path_prefix, is_root_wt ? "" : "/",
2522 *tree_relpath) == -1) {
2523 err = got_error_from_errno("asprintf");
2527 err = got_object_id_by_path(tree_id, repo,
2528 worktree->base_commit_id, in_repo_path);
2530 /* Check out all files within a subdirectory. */
2531 *tree_id = got_object_id_dup(id);
2532 if (*tree_id == NULL) {
2533 err = got_error_from_errno("got_object_id_dup");
2536 *tree_relpath = strdup(wt_relpath);
2537 if (*tree_relpath == NULL) {
2538 err = got_error_from_errno("strdup");
2546 *entry_type = GOT_OBJ_TYPE_ANY;
2547 free(*tree_relpath);
2548 *tree_relpath = NULL;
2555 static const struct got_error *
2556 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2557 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2558 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2559 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2561 const struct got_error *err = NULL;
2562 struct got_commit_object *commit = NULL;
2563 struct got_tree_object *tree = NULL;
2564 struct got_fileindex_diff_tree_cb diff_cb;
2565 struct diff_cb_arg arg;
2567 err = ref_base_commit(worktree, repo);
2569 if (!(err->code == GOT_ERR_ERRNO &&
2570 (errno == EACCES || errno == EROFS)))
2572 err = (*progress_cb)(progress_arg,
2573 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2578 err = got_object_open_as_commit(&commit, repo,
2579 worktree->base_commit_id);
2583 err = got_object_open_as_tree(&tree, repo, tree_id);
2588 got_object_tree_find_entry(tree, entry_name) == NULL) {
2589 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2593 diff_cb.diff_old_new = diff_old_new;
2594 diff_cb.diff_old = diff_old;
2595 diff_cb.diff_new = diff_new;
2596 arg.fileindex = fileindex;
2597 arg.worktree = worktree;
2599 arg.progress_cb = progress_cb;
2600 arg.progress_arg = progress_arg;
2601 arg.cancel_cb = cancel_cb;
2602 arg.cancel_arg = cancel_arg;
2603 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2604 entry_name, repo, &diff_cb, &arg);
2607 got_object_tree_close(tree);
2609 got_object_commit_close(commit);
2613 const struct got_error *
2614 got_worktree_checkout_files(struct got_worktree *worktree,
2615 struct got_pathlist_head *paths, struct got_repository *repo,
2616 got_worktree_checkout_cb progress_cb, void *progress_arg,
2617 got_cancel_cb cancel_cb, void *cancel_arg)
2619 const struct got_error *err = NULL, *sync_err, *unlockerr;
2620 struct got_commit_object *commit = NULL;
2621 struct got_tree_object *tree = NULL;
2622 struct got_fileindex *fileindex = NULL;
2623 char *fileindex_path = NULL;
2624 struct got_pathlist_entry *pe;
2625 struct tree_path_data {
2626 SIMPLEQ_ENTRY(tree_path_data) entry;
2627 struct got_object_id *tree_id;
2632 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2634 SIMPLEQ_INIT(&tree_paths);
2636 err = lock_worktree(worktree, LOCK_EX);
2640 /* Map all specified paths to in-repository trees. */
2641 TAILQ_FOREACH(pe, paths, entry) {
2642 tpd = malloc(sizeof(*tpd));
2644 err = got_error_from_errno("malloc");
2648 err = find_tree_entry_for_checkout(&tpd->entry_type,
2649 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2655 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2656 err = got_path_basename(&tpd->entry_name, pe->path);
2664 tpd->entry_name = NULL;
2666 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2670 * Read the file index.
2671 * Checking out files is supposed to be an idempotent operation.
2672 * If the on-disk file index is incomplete we will try to complete it.
2674 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2678 tpd = SIMPLEQ_FIRST(&tree_paths);
2679 TAILQ_FOREACH(pe, paths, entry) {
2680 struct bump_base_commit_id_arg bbc_arg;
2682 err = checkout_files(worktree, fileindex, tpd->relpath,
2683 tpd->tree_id, tpd->entry_name, repo,
2684 progress_cb, progress_arg, cancel_cb, cancel_arg);
2688 bbc_arg.base_commit_id = worktree->base_commit_id;
2689 bbc_arg.entry_name = tpd->entry_name;
2690 bbc_arg.path = pe->path;
2691 bbc_arg.path_len = pe->path_len;
2692 bbc_arg.progress_cb = progress_cb;
2693 bbc_arg.progress_arg = progress_arg;
2694 err = got_fileindex_for_each_entry_safe(fileindex,
2695 bump_base_commit_id, &bbc_arg);
2699 tpd = SIMPLEQ_NEXT(tpd, entry);
2701 sync_err = sync_fileindex(fileindex, fileindex_path);
2702 if (sync_err && err == NULL)
2705 free(fileindex_path);
2707 got_object_tree_close(tree);
2709 got_object_commit_close(commit);
2711 got_fileindex_free(fileindex);
2712 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2713 tpd = SIMPLEQ_FIRST(&tree_paths);
2714 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2719 unlockerr = lock_worktree(worktree, LOCK_SH);
2720 if (unlockerr && err == NULL)
2725 struct merge_file_cb_arg {
2726 struct got_worktree *worktree;
2727 struct got_fileindex *fileindex;
2728 got_worktree_checkout_cb progress_cb;
2730 got_cancel_cb cancel_cb;
2732 const char *label_orig;
2733 struct got_object_id *commit_id2;
2736 static const struct got_error *
2737 merge_file_cb(void *arg, struct got_blob_object *blob1,
2738 struct got_blob_object *blob2, struct got_object_id *id1,
2739 struct got_object_id *id2, const char *path1, const char *path2,
2740 mode_t mode1, mode_t mode2, struct got_repository *repo)
2742 static const struct got_error *err = NULL;
2743 struct merge_file_cb_arg *a = arg;
2744 struct got_fileindex_entry *ie;
2745 char *ondisk_path = NULL;
2747 unsigned char status;
2748 int local_changes_subsumed;
2750 if (blob1 && blob2) {
2751 ie = got_fileindex_entry_get(a->fileindex, path2,
2754 return (*a->progress_cb)(a->progress_arg,
2755 GOT_STATUS_MISSING, path2);
2757 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2759 return got_error_from_errno("asprintf");
2761 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2766 if (status == GOT_STATUS_DELETE) {
2767 err = (*a->progress_cb)(a->progress_arg,
2768 GOT_STATUS_MERGE, path2);
2771 if (status != GOT_STATUS_NO_CHANGE &&
2772 status != GOT_STATUS_MODIFY &&
2773 status != GOT_STATUS_CONFLICT &&
2774 status != GOT_STATUS_ADD) {
2775 err = (*a->progress_cb)(a->progress_arg, status, path2);
2779 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2781 err = got_object_blob_read_to_str(&link_target2, blob2);
2784 err = merge_symlink(a->worktree, blob1, ondisk_path,
2785 path2, a->label_orig, link_target2, a->commit_id2,
2786 repo, a->progress_cb, a->progress_arg);
2789 err = merge_blob(&local_changes_subsumed, a->worktree,
2790 blob1, ondisk_path, path2, sb.st_mode,
2791 a->label_orig, blob2, a->commit_id2, repo,
2792 a->progress_cb, a->progress_arg);
2795 ie = got_fileindex_entry_get(a->fileindex, path1,
2798 return (*a->progress_cb)(a->progress_arg,
2799 GOT_STATUS_MISSING, path1);
2801 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2803 return got_error_from_errno("asprintf");
2805 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2811 case GOT_STATUS_NO_CHANGE:
2812 err = (*a->progress_cb)(a->progress_arg,
2813 GOT_STATUS_DELETE, path1);
2816 err = remove_ondisk_file(a->worktree->root_path, path1);
2820 got_fileindex_entry_mark_deleted_from_disk(ie);
2822 case GOT_STATUS_DELETE:
2823 case GOT_STATUS_MISSING:
2824 err = (*a->progress_cb)(a->progress_arg,
2825 GOT_STATUS_DELETE, path1);
2829 got_fileindex_entry_mark_deleted_from_disk(ie);
2831 case GOT_STATUS_ADD: {
2832 struct got_object_id *id;
2835 * Delete the added file only if its content already
2836 * exists in the repository.
2838 err = got_object_blob_file_create(&id, &blob1_f, path1);
2841 if (got_object_id_cmp(id, id1) == 0) {
2842 err = (*a->progress_cb)(a->progress_arg,
2843 GOT_STATUS_DELETE, path1);
2846 err = remove_ondisk_file(a->worktree->root_path,
2851 got_fileindex_entry_remove(a->fileindex,
2854 err = (*a->progress_cb)(a->progress_arg,
2855 GOT_STATUS_CANNOT_DELETE, path1);
2857 if (fclose(blob1_f) == EOF && err == NULL)
2858 err = got_error_from_errno("fclose");
2864 case GOT_STATUS_MODIFY:
2865 case GOT_STATUS_CONFLICT:
2866 err = (*a->progress_cb)(a->progress_arg,
2867 GOT_STATUS_CANNOT_DELETE, path1);
2871 case GOT_STATUS_OBSTRUCTED:
2872 err = (*a->progress_cb)(a->progress_arg, status, path1);
2880 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2882 return got_error_from_errno("asprintf");
2883 ie = got_fileindex_entry_get(a->fileindex, path2,
2886 err = get_file_status(&status, &sb, ie, ondisk_path,
2890 if (status != GOT_STATUS_NO_CHANGE &&
2891 status != GOT_STATUS_MODIFY &&
2892 status != GOT_STATUS_CONFLICT &&
2893 status != GOT_STATUS_ADD) {
2894 err = (*a->progress_cb)(a->progress_arg,
2898 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2900 err = got_object_blob_read_to_str(&link_target2,
2904 err = merge_symlink(a->worktree, NULL,
2905 ondisk_path, path2, a->label_orig,
2906 link_target2, a->commit_id2, repo,
2907 a->progress_cb, a->progress_arg);
2909 } else if (S_ISREG(sb.st_mode)) {
2910 err = merge_blob(&local_changes_subsumed,
2911 a->worktree, NULL, ondisk_path, path2,
2912 sb.st_mode, a->label_orig, blob2,
2913 a->commit_id2, repo, a->progress_cb,
2916 err = got_error_path(ondisk_path,
2917 GOT_ERR_FILE_OBSTRUCTED);
2921 if (status == GOT_STATUS_DELETE) {
2922 err = got_fileindex_entry_update(ie,
2923 ondisk_path, blob2->id.sha1,
2924 a->worktree->base_commit_id->sha1, 0);
2929 int is_bad_symlink = 0;
2930 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2931 if (S_ISLNK(mode2)) {
2932 err = install_symlink(&is_bad_symlink,
2933 a->worktree, ondisk_path, path2, blob2, 0,
2934 0, 1, repo, a->progress_cb, a->progress_arg);
2936 err = install_blob(a->worktree, ondisk_path, path2,
2937 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2938 a->progress_cb, a->progress_arg);
2942 err = got_fileindex_entry_alloc(&ie, path2);
2945 err = got_fileindex_entry_update(ie, ondisk_path,
2948 got_fileindex_entry_free(ie);
2951 err = got_fileindex_entry_add(a->fileindex, ie);
2953 got_fileindex_entry_free(ie);
2956 if (is_bad_symlink) {
2957 got_fileindex_entry_filetype_set(ie,
2958 GOT_FILEIDX_MODE_BAD_SYMLINK);
2967 struct check_merge_ok_arg {
2968 struct got_worktree *worktree;
2969 struct got_repository *repo;
2972 static const struct got_error *
2973 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2975 const struct got_error *err = NULL;
2976 struct check_merge_ok_arg *a = arg;
2977 unsigned char status;
2981 /* Reject merges into a work tree with mixed base commits. */
2982 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2983 SHA1_DIGEST_LENGTH))
2984 return got_error(GOT_ERR_MIXED_COMMITS);
2986 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2988 return got_error_from_errno("asprintf");
2990 /* Reject merges into a work tree with conflicted files. */
2991 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2994 if (status == GOT_STATUS_CONFLICT)
2995 return got_error(GOT_ERR_CONFLICTS);
3000 static const struct got_error *
3001 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3002 const char *fileindex_path, struct got_object_id *commit_id1,
3003 struct got_object_id *commit_id2, struct got_repository *repo,
3004 got_worktree_checkout_cb progress_cb, void *progress_arg,
3005 got_cancel_cb cancel_cb, void *cancel_arg)
3007 const struct got_error *err = NULL, *sync_err;
3008 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3009 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3010 struct merge_file_cb_arg arg;
3011 char *label_orig = NULL;
3014 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3015 worktree->path_prefix);
3016 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3022 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3026 err = got_object_id_str(&id_str, commit_id1);
3030 if (asprintf(&label_orig, "%s: commit %s",
3031 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3032 err = got_error_from_errno("asprintf");
3039 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3040 worktree->path_prefix);
3044 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3048 arg.worktree = worktree;
3049 arg.fileindex = fileindex;
3050 arg.progress_cb = progress_cb;
3051 arg.progress_arg = progress_arg;
3052 arg.cancel_cb = cancel_cb;
3053 arg.cancel_arg = cancel_arg;
3054 arg.label_orig = label_orig;
3055 arg.commit_id2 = commit_id2;
3056 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3057 sync_err = sync_fileindex(fileindex, fileindex_path);
3058 if (sync_err && err == NULL)
3062 got_object_tree_close(tree1);
3064 got_object_tree_close(tree2);
3069 const struct got_error *
3070 got_worktree_merge_files(struct got_worktree *worktree,
3071 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3072 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3073 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3075 const struct got_error *err, *unlockerr;
3076 char *fileindex_path = NULL;
3077 struct got_fileindex *fileindex = NULL;
3078 struct check_merge_ok_arg mok_arg;
3080 err = lock_worktree(worktree, LOCK_EX);
3084 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3088 mok_arg.worktree = worktree;
3089 mok_arg.repo = repo;
3090 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
3095 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3096 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
3099 got_fileindex_free(fileindex);
3100 free(fileindex_path);
3101 unlockerr = lock_worktree(worktree, LOCK_SH);
3102 if (unlockerr && err == NULL)
3107 struct diff_dir_cb_arg {
3108 struct got_fileindex *fileindex;
3109 struct got_worktree *worktree;
3110 const char *status_path;
3111 size_t status_path_len;
3112 struct got_repository *repo;
3113 got_worktree_status_cb status_cb;
3115 got_cancel_cb cancel_cb;
3117 /* A pathlist containing per-directory pathlists of ignore patterns. */
3118 struct got_pathlist_head ignores;
3119 int report_unchanged;
3123 static const struct got_error *
3124 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3125 int dirfd, const char *de_name,
3126 got_worktree_status_cb status_cb, void *status_arg,
3127 struct got_repository *repo, int report_unchanged)
3129 const struct got_error *err = NULL;
3130 unsigned char status = GOT_STATUS_NO_CHANGE;
3131 unsigned char staged_status = get_staged_status(ie);
3133 struct got_object_id blob_id, commit_id, staged_blob_id;
3134 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3135 struct got_object_id *staged_blob_idp = NULL;
3137 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3141 if (status == GOT_STATUS_NO_CHANGE &&
3142 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3145 if (got_fileindex_entry_has_blob(ie)) {
3146 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3147 blob_idp = &blob_id;
3149 if (got_fileindex_entry_has_commit(ie)) {
3150 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3151 commit_idp = &commit_id;
3153 if (staged_status == GOT_STATUS_ADD ||
3154 staged_status == GOT_STATUS_MODIFY) {
3155 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3156 SHA1_DIGEST_LENGTH);
3157 staged_blob_idp = &staged_blob_id;
3160 return (*status_cb)(status_arg, status, staged_status,
3161 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3164 static const struct got_error *
3165 status_old_new(void *arg, struct got_fileindex_entry *ie,
3166 struct dirent *de, const char *parent_path, int dirfd)
3168 const struct got_error *err = NULL;
3169 struct diff_dir_cb_arg *a = arg;
3172 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3173 return got_error(GOT_ERR_CANCELLED);
3175 if (got_path_cmp(parent_path, a->status_path,
3176 strlen(parent_path), a->status_path_len) != 0 &&
3177 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3180 if (parent_path[0]) {
3181 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3182 parent_path, de->d_name) == -1)
3183 return got_error_from_errno("asprintf");
3185 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3187 return got_error_from_errno("asprintf");
3190 err = report_file_status(ie, abspath, dirfd, de->d_name,
3191 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3196 static const struct got_error *
3197 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3199 struct diff_dir_cb_arg *a = arg;
3200 struct got_object_id blob_id, commit_id;
3201 unsigned char status;
3203 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3204 return got_error(GOT_ERR_CANCELLED);
3206 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3209 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3210 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3211 if (got_fileindex_entry_has_file_on_disk(ie))
3212 status = GOT_STATUS_MISSING;
3214 status = GOT_STATUS_DELETE;
3215 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3216 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3220 free_ignorelist(struct got_pathlist_head *ignorelist)
3222 struct got_pathlist_entry *pe;
3224 TAILQ_FOREACH(pe, ignorelist, entry)
3225 free((char *)pe->path);
3226 got_pathlist_free(ignorelist);
3230 free_ignores(struct got_pathlist_head *ignores)
3232 struct got_pathlist_entry *pe;
3234 TAILQ_FOREACH(pe, ignores, entry) {
3235 struct got_pathlist_head *ignorelist = pe->data;
3236 free_ignorelist(ignorelist);
3237 free((char *)pe->path);
3239 got_pathlist_free(ignores);
3242 static const struct got_error *
3243 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3245 const struct got_error *err = NULL;
3246 struct got_pathlist_entry *pe = NULL;
3247 struct got_pathlist_head *ignorelist;
3248 char *line = NULL, *pattern, *dirpath = NULL;
3249 size_t linesize = 0;
3252 ignorelist = calloc(1, sizeof(*ignorelist));
3253 if (ignorelist == NULL)
3254 return got_error_from_errno("calloc");
3255 TAILQ_INIT(ignorelist);
3257 while ((linelen = getline(&line, &linesize, f)) != -1) {
3258 if (linelen > 0 && line[linelen - 1] == '\n')
3259 line[linelen - 1] = '\0';
3261 /* Git's ignores may contain comments. */
3265 /* Git's negated patterns are not (yet?) supported. */
3269 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3271 err = got_error_from_errno("asprintf");
3274 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3279 err = got_error_from_errno("getline");
3283 dirpath = strdup(path);
3284 if (dirpath == NULL) {
3285 err = got_error_from_errno("strdup");
3288 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3291 if (err || pe == NULL) {
3293 free_ignorelist(ignorelist);
3299 match_ignores(struct got_pathlist_head *ignores, const char *path)
3301 struct got_pathlist_entry *pe;
3303 /* Handle patterns which match in all directories. */
3304 TAILQ_FOREACH(pe, ignores, entry) {
3305 struct got_pathlist_head *ignorelist = pe->data;
3306 struct got_pathlist_entry *pi;
3308 TAILQ_FOREACH(pi, ignorelist, entry) {
3309 const char *p, *pattern = pi->path;
3311 if (strncmp(pattern, "**/", 3) != 0)
3316 if (fnmatch(pattern, p,
3317 FNM_PATHNAME | FNM_LEADING_DIR)) {
3318 /* Retry in next directory. */
3319 while (*p && *p != '/')
3331 * The ignores pathlist contains ignore lists from children before
3332 * parents, so we can find the most specific ignorelist by walking
3333 * ignores backwards.
3335 pe = TAILQ_LAST(ignores, got_pathlist_head);
3337 if (got_path_is_child(path, pe->path, pe->path_len)) {
3338 struct got_pathlist_head *ignorelist = pe->data;
3339 struct got_pathlist_entry *pi;
3340 TAILQ_FOREACH(pi, ignorelist, entry) {
3341 const char *pattern = pi->path;
3342 int flags = FNM_LEADING_DIR;
3343 if (strstr(pattern, "/**/") == NULL)
3344 flags |= FNM_PATHNAME;
3345 if (fnmatch(pattern, path, flags))
3350 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3356 static const struct got_error *
3357 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3358 const char *path, int dirfd, const char *ignores_filename)
3360 const struct got_error *err = NULL;
3363 FILE *ignoresfile = NULL;
3365 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3366 path[0] ? "/" : "", ignores_filename) == -1)
3367 return got_error_from_errno("asprintf");
3370 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3372 if (errno != ENOENT && errno != EACCES)
3373 err = got_error_from_errno2("openat",
3376 ignoresfile = fdopen(fd, "r");
3377 if (ignoresfile == NULL)
3378 err = got_error_from_errno2("fdopen",
3382 err = read_ignores(ignores, path, ignoresfile);
3386 ignoresfile = fopen(ignorespath, "r");
3387 if (ignoresfile == NULL) {
3388 if (errno != ENOENT && errno != EACCES)
3389 err = got_error_from_errno2("fopen",
3392 err = read_ignores(ignores, path, ignoresfile);
3395 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3396 err = got_error_from_errno2("fclose", path);
3397 if (fd != -1 && close(fd) == -1 && err == NULL)
3398 err = got_error_from_errno2("close", path);
3403 static const struct got_error *
3404 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3406 const struct got_error *err = NULL;
3407 struct diff_dir_cb_arg *a = arg;
3410 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3411 return got_error(GOT_ERR_CANCELLED);
3413 if (parent_path[0]) {
3414 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3415 return got_error_from_errno("asprintf");
3420 if (de->d_type != DT_DIR &&
3421 got_path_is_child(path, a->status_path, a->status_path_len)
3422 && !match_ignores(&a->ignores, path))
3423 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3424 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3430 static const struct got_error *
3431 status_traverse(void *arg, const char *path, int dirfd)
3433 const struct got_error *err = NULL;
3434 struct diff_dir_cb_arg *a = arg;
3439 err = add_ignores(&a->ignores, a->worktree->root_path,
3440 path, dirfd, ".cvsignore");
3444 err = add_ignores(&a->ignores, a->worktree->root_path, path,
3445 dirfd, ".gitignore");
3450 static const struct got_error *
3451 report_single_file_status(const char *path, const char *ondisk_path,
3452 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3453 void *status_arg, struct got_repository *repo, int report_unchanged)
3455 struct got_fileindex_entry *ie;
3458 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3460 return report_file_status(ie, ondisk_path, -1, NULL,
3461 status_cb, status_arg, repo, report_unchanged);
3463 if (lstat(ondisk_path, &sb) == -1) {
3464 if (errno != ENOENT)
3465 return got_error_from_errno2("lstat", ondisk_path);
3466 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3467 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3471 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3472 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3473 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3478 static const struct got_error *
3479 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3480 const char *root_path, const char *path)
3482 const struct got_error *err;
3483 char *parent_path, *next_parent_path = NULL;
3485 err = add_ignores(ignores, root_path, "", -1,
3490 err = add_ignores(ignores, root_path, "", -1,
3495 err = got_path_dirname(&parent_path, path);
3497 if (err->code == GOT_ERR_BAD_PATH)
3498 return NULL; /* cannot traverse parent */
3502 err = add_ignores(ignores, root_path, parent_path, -1,
3506 err = add_ignores(ignores, root_path, parent_path, -1,
3510 err = got_path_dirname(&next_parent_path, parent_path);
3512 if (err->code == GOT_ERR_BAD_PATH)
3513 err = NULL; /* traversed everything */
3517 parent_path = next_parent_path;
3518 next_parent_path = NULL;
3522 free(next_parent_path);
3526 static const struct got_error *
3527 worktree_status(struct got_worktree *worktree, const char *path,
3528 struct got_fileindex *fileindex, struct got_repository *repo,
3529 got_worktree_status_cb status_cb, void *status_arg,
3530 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3531 int report_unchanged)
3533 const struct got_error *err = NULL;
3535 struct got_fileindex_diff_dir_cb fdiff_cb;
3536 struct diff_dir_cb_arg arg;
3537 char *ondisk_path = NULL;
3539 TAILQ_INIT(&arg.ignores);
3541 if (asprintf(&ondisk_path, "%s%s%s",
3542 worktree->root_path, path[0] ? "/" : "", path) == -1)
3543 return got_error_from_errno("asprintf");
3545 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3547 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3549 err = got_error_from_errno2("open", ondisk_path);
3551 err = report_single_file_status(path, ondisk_path,
3552 fileindex, status_cb, status_arg, repo,
3555 fdiff_cb.diff_old_new = status_old_new;
3556 fdiff_cb.diff_old = status_old;
3557 fdiff_cb.diff_new = status_new;
3558 fdiff_cb.diff_traverse = status_traverse;
3559 arg.fileindex = fileindex;
3560 arg.worktree = worktree;
3561 arg.status_path = path;
3562 arg.status_path_len = strlen(path);
3564 arg.status_cb = status_cb;
3565 arg.status_arg = status_arg;
3566 arg.cancel_cb = cancel_cb;
3567 arg.cancel_arg = cancel_arg;
3568 arg.report_unchanged = report_unchanged;
3569 arg.no_ignores = no_ignores;
3571 err = add_ignores_from_parent_paths(&arg.ignores,
3572 worktree->root_path, path);
3576 err = got_fileindex_diff_dir(fileindex, fd,
3577 worktree->root_path, path, repo, &fdiff_cb, &arg);
3580 free_ignores(&arg.ignores);
3581 if (fd != -1 && close(fd) != 0 && err == NULL)
3582 err = got_error_from_errno("close");
3587 const struct got_error *
3588 got_worktree_status(struct got_worktree *worktree,
3589 struct got_pathlist_head *paths, struct got_repository *repo,
3590 got_worktree_status_cb status_cb, void *status_arg,
3591 got_cancel_cb cancel_cb, void *cancel_arg)
3593 const struct got_error *err = NULL;
3594 char *fileindex_path = NULL;
3595 struct got_fileindex *fileindex = NULL;
3596 struct got_pathlist_entry *pe;
3598 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3602 TAILQ_FOREACH(pe, paths, entry) {
3603 err = worktree_status(worktree, pe->path, fileindex, repo,
3604 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3608 free(fileindex_path);
3609 got_fileindex_free(fileindex);
3613 const struct got_error *
3614 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3617 const struct got_error *err = NULL;
3618 char *resolved = NULL, *cwd = NULL, *path = NULL;
3624 cwd = getcwd(NULL, 0);
3626 return got_error_from_errno("getcwd");
3628 if (lstat(arg, &sb) == -1) {
3629 if (errno != ENOENT) {
3630 err = got_error_from_errno2("lstat", arg);
3634 if (S_ISLNK(sb.st_mode)) {
3636 * We cannot use realpath(3) with symlinks since we want to
3637 * operate on the symlink itself.
3638 * But we can make the path absolute, assuming it is relative
3639 * to the current working directory, and then canonicalize it.
3641 char *abspath = NULL;
3642 char canonpath[PATH_MAX];
3643 if (!got_path_is_absolute(arg)) {
3644 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3645 err = got_error_from_errno("asprintf");
3650 err = got_canonpath(abspath ? abspath : arg, canonpath,
3654 resolved = strdup(canonpath);
3655 if (resolved == NULL) {
3656 err = got_error_from_errno("strdup");
3660 resolved = realpath(arg, NULL);
3661 if (resolved == NULL) {
3662 if (errno != ENOENT) {
3663 err = got_error_from_errno2("realpath", arg);
3666 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
3667 err = got_error_from_errno("asprintf");
3673 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3674 strlen(got_worktree_get_root_path(worktree)))) {
3675 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3679 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3680 err = got_path_skip_common_ancestor(&path,
3681 got_worktree_get_root_path(worktree), resolved);
3687 err = got_error_from_errno("strdup");
3692 /* XXX status walk can't deal with trailing slash! */
3694 while (len > 0 && path[len - 1] == '/') {
3695 path[len - 1] = '\0';
3708 struct schedule_addition_args {
3709 struct got_worktree *worktree;
3710 struct got_fileindex *fileindex;
3711 got_worktree_checkout_cb progress_cb;
3713 struct got_repository *repo;
3716 static const struct got_error *
3717 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3718 const char *relpath, struct got_object_id *blob_id,
3719 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3720 int dirfd, const char *de_name)
3722 struct schedule_addition_args *a = arg;
3723 const struct got_error *err = NULL;
3724 struct got_fileindex_entry *ie;
3728 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3730 return got_error_from_errno("asprintf");
3732 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3734 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3738 /* Re-adding an existing entry is a no-op. */
3739 if (status == GOT_STATUS_ADD)
3741 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3746 if (status != GOT_STATUS_UNVERSIONED) {
3747 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3751 err = got_fileindex_entry_alloc(&ie, relpath);
3754 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3756 got_fileindex_entry_free(ie);
3759 err = got_fileindex_entry_add(a->fileindex, ie);
3761 got_fileindex_entry_free(ie);
3768 if (status == GOT_STATUS_ADD)
3770 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3773 const struct got_error *
3774 got_worktree_schedule_add(struct got_worktree *worktree,
3775 struct got_pathlist_head *paths,
3776 got_worktree_checkout_cb progress_cb, void *progress_arg,
3777 struct got_repository *repo, int no_ignores)
3779 struct got_fileindex *fileindex = NULL;
3780 char *fileindex_path = NULL;
3781 const struct got_error *err = NULL, *sync_err, *unlockerr;
3782 struct got_pathlist_entry *pe;
3783 struct schedule_addition_args saa;
3785 err = lock_worktree(worktree, LOCK_EX);
3789 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3793 saa.worktree = worktree;
3794 saa.fileindex = fileindex;
3795 saa.progress_cb = progress_cb;
3796 saa.progress_arg = progress_arg;
3799 TAILQ_FOREACH(pe, paths, entry) {
3800 err = worktree_status(worktree, pe->path, fileindex, repo,
3801 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3805 sync_err = sync_fileindex(fileindex, fileindex_path);
3806 if (sync_err && err == NULL)
3809 free(fileindex_path);
3811 got_fileindex_free(fileindex);
3812 unlockerr = lock_worktree(worktree, LOCK_SH);
3813 if (unlockerr && err == NULL)
3818 struct schedule_deletion_args {
3819 struct got_worktree *worktree;
3820 struct got_fileindex *fileindex;
3821 got_worktree_delete_cb progress_cb;
3823 struct got_repository *repo;
3824 int delete_local_mods;
3826 const char *status_codes;
3829 static const struct got_error *
3830 schedule_for_deletion(void *arg, unsigned char status,
3831 unsigned char staged_status, const char *relpath,
3832 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3833 struct got_object_id *commit_id, int dirfd, const char *de_name)
3835 struct schedule_deletion_args *a = arg;
3836 const struct got_error *err = NULL;
3837 struct got_fileindex_entry *ie = NULL;
3839 char *ondisk_path, *parent = NULL;
3841 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3843 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3845 staged_status = get_staged_status(ie);
3846 if (staged_status != GOT_STATUS_NO_CHANGE) {
3847 if (staged_status == GOT_STATUS_DELETE)
3849 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3852 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3854 return got_error_from_errno("asprintf");
3856 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3861 if (a->status_codes) {
3862 size_t ncodes = strlen(a->status_codes);
3864 for (i = 0; i < ncodes ; i++) {
3865 if (status == a->status_codes[i])
3869 /* Do not delete files in non-matching status. */
3873 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
3874 a->status_codes[i] != GOT_STATUS_MISSING) {
3875 static char msg[64];
3876 snprintf(msg, sizeof(msg),
3877 "invalid status code '%c'", a->status_codes[i]);
3878 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
3883 if (status != GOT_STATUS_NO_CHANGE) {
3884 if (status == GOT_STATUS_DELETE)
3886 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3887 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3890 if (status != GOT_STATUS_MODIFY &&
3891 status != GOT_STATUS_MISSING) {
3892 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3897 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3899 if (unlinkat(dirfd, de_name, 0) != 0) {
3900 err = got_error_from_errno2("unlinkat",
3904 } else if (unlink(ondisk_path) != 0) {
3905 err = got_error_from_errno2("unlink", ondisk_path);
3909 parent = dirname(ondisk_path);
3911 if (parent == NULL) {
3912 err = got_error_from_errno2("dirname", ondisk_path);
3915 while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3916 if (rmdir(parent) == -1) {
3917 if (errno != ENOTEMPTY)
3918 err = got_error_from_errno2("rmdir",
3922 parent = dirname(parent);
3923 if (parent == NULL) {
3924 err = got_error_from_errno2("dirname", parent);
3930 got_fileindex_entry_mark_deleted_from_disk(ie);
3935 if (status == GOT_STATUS_DELETE)
3937 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3938 staged_status, relpath);
3941 const struct got_error *
3942 got_worktree_schedule_delete(struct got_worktree *worktree,
3943 struct got_pathlist_head *paths, int delete_local_mods,
3944 const char *status_codes,
3945 got_worktree_delete_cb progress_cb, void *progress_arg,
3946 struct got_repository *repo, int keep_on_disk)
3948 struct got_fileindex *fileindex = NULL;
3949 char *fileindex_path = NULL;
3950 const struct got_error *err = NULL, *sync_err, *unlockerr;
3951 struct got_pathlist_entry *pe;
3952 struct schedule_deletion_args sda;
3954 err = lock_worktree(worktree, LOCK_EX);
3958 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3962 sda.worktree = worktree;
3963 sda.fileindex = fileindex;
3964 sda.progress_cb = progress_cb;
3965 sda.progress_arg = progress_arg;
3967 sda.delete_local_mods = delete_local_mods;
3968 sda.keep_on_disk = keep_on_disk;
3969 sda.status_codes = status_codes;
3971 TAILQ_FOREACH(pe, paths, entry) {
3972 err = worktree_status(worktree, pe->path, fileindex, repo,
3973 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3977 sync_err = sync_fileindex(fileindex, fileindex_path);
3978 if (sync_err && err == NULL)
3981 free(fileindex_path);
3983 got_fileindex_free(fileindex);
3984 unlockerr = lock_worktree(worktree, LOCK_SH);
3985 if (unlockerr && err == NULL)
3990 static const struct got_error *
3991 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3993 const struct got_error *err = NULL;
3995 size_t linesize = 0, n;
3998 linelen = getline(&line, &linesize, infile);
3999 if (linelen == -1) {
4000 if (ferror(infile)) {
4001 err = got_error_from_errno("getline");
4007 n = fwrite(line, 1, linelen, outfile);
4009 err = got_ferror(outfile, GOT_ERR_IO);
4014 n = fwrite(line, 1, linelen, rejectfile);
4016 err = got_ferror(outfile, GOT_ERR_IO);
4023 static const struct got_error *
4024 skip_one_line(FILE *f)
4027 size_t linesize = 0;
4030 linelen = getline(&line, &linesize, f);
4031 if (linelen == -1) {
4033 return got_error_from_errno("getline");
4040 static const struct got_error *
4041 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4042 int start_old, int end_old, int start_new, int end_new,
4043 FILE *outfile, FILE *rejectfile)
4045 const struct got_error *err;
4047 /* Copy old file's lines leading up to patch. */
4048 while (!feof(f1) && *line_cur1 < start_old) {
4049 err = copy_one_line(f1, outfile, NULL);
4054 /* Skip new file's lines leading up to patch. */
4055 while (!feof(f2) && *line_cur2 < start_new) {
4057 err = copy_one_line(f2, NULL, rejectfile);
4059 err = skip_one_line(f2);
4064 /* Copy patched lines. */
4065 while (!feof(f2) && *line_cur2 <= end_new) {
4066 err = copy_one_line(f2, outfile, NULL);
4071 /* Skip over old file's replaced lines. */
4072 while (!feof(f1) && *line_cur1 <= end_old) {
4074 err = copy_one_line(f1, NULL, rejectfile);
4076 err = skip_one_line(f1);
4085 static const struct got_error *
4086 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4087 FILE *outfile, FILE *rejectfile)
4089 const struct got_error *err;
4092 /* Copy old file's lines until EOF. */
4094 err = copy_one_line(f1, outfile, NULL);
4101 /* Copy new file's lines until EOF. */
4103 err = copy_one_line(f2, NULL, rejectfile);
4113 static const struct got_error *
4114 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
4115 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
4116 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
4117 int *line_cur2, FILE *outfile, FILE *rejectfile,
4118 got_worktree_patch_cb patch_cb, void *patch_arg)
4120 const struct got_error *err = NULL;
4121 int start_old = change->cv.a;
4122 int end_old = change->cv.b;
4123 int start_new = change->cv.c;
4124 int end_new = change->cv.d;
4128 *choice = GOT_PATCH_CHOICE_NONE;
4130 hunkfile = got_opentemp();
4131 if (hunkfile == NULL)
4132 return got_error_from_errno("got_opentemp");
4137 /* XXX TODO needs error checking */
4138 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
4140 if (fseek(f1, pos1, SEEK_SET) == -1) {
4141 err = got_ferror(f1, GOT_ERR_IO);
4144 if (fseek(f2, pos2, SEEK_SET) == -1) {
4145 err = got_ferror(f1, GOT_ERR_IO);
4148 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4149 err = got_ferror(hunkfile, GOT_ERR_IO);
4153 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4154 hunkfile, n, nchanges);
4159 case GOT_PATCH_CHOICE_YES:
4160 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4161 end_old, start_new, end_new, outfile, rejectfile);
4163 case GOT_PATCH_CHOICE_NO:
4164 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4165 end_old, start_new, end_new, rejectfile, outfile);
4167 case GOT_PATCH_CHOICE_QUIT:
4170 err = got_error(GOT_ERR_PATCH_CHOICE);
4174 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4175 err = got_error_from_errno("fclose");
4179 struct revert_file_args {
4180 struct got_worktree *worktree;
4181 struct got_fileindex *fileindex;
4182 got_worktree_checkout_cb progress_cb;
4184 got_worktree_patch_cb patch_cb;
4186 struct got_repository *repo;
4189 static const struct got_error *
4190 create_patched_content(char **path_outfile, int reverse_patch,
4191 struct got_object_id *blob_id, const char *path2,
4192 int dirfd2, const char *de_name2,
4193 const char *relpath, struct got_repository *repo,
4194 got_worktree_patch_cb patch_cb, void *patch_arg)
4196 const struct got_error *err;
4197 struct got_blob_object *blob = NULL;
4198 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4200 char link_target[PATH_MAX];
4201 ssize_t link_len = 0;
4202 char *path1 = NULL, *id_str = NULL;
4203 struct stat sb1, sb2;
4204 struct got_diff_changes *changes = NULL;
4205 struct got_diff_state *ds = NULL;
4206 struct got_diff_args *args = NULL;
4207 struct got_diff_change *change;
4208 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
4211 *path_outfile = NULL;
4213 err = got_object_id_str(&id_str, blob_id);
4218 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4220 if (errno != ELOOP) {
4221 err = got_error_from_errno2("openat", path2);
4224 link_len = readlinkat(dirfd2, de_name2,
4225 link_target, sizeof(link_target));
4227 return got_error_from_errno2("readlinkat", path2);
4228 sb2.st_mode = S_IFLNK;
4229 sb2.st_size = link_len;
4232 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4234 if (errno != ELOOP) {
4235 err = got_error_from_errno2("open", path2);
4238 link_len = readlink(path2, link_target,
4239 sizeof(link_target));
4241 return got_error_from_errno2("readlink", path2);
4242 sb2.st_mode = S_IFLNK;
4243 sb2.st_size = link_len;
4247 if (fstat(fd2, &sb2) == -1) {
4248 err = got_error_from_errno2("fstat", path2);
4252 f2 = fdopen(fd2, "r");
4254 err = got_error_from_errno2("fdopen", path2);
4260 f2 = got_opentemp();
4262 err = got_error_from_errno2("got_opentemp", path2);
4265 n = fwrite(link_target, 1, link_len, f2);
4266 if (n != link_len) {
4267 err = got_ferror(f2, GOT_ERR_IO);
4270 if (fflush(f2) == EOF) {
4271 err = got_error_from_errno("fflush");
4277 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4281 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4285 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4289 if (stat(path1, &sb1) == -1) {
4290 err = got_error_from_errno2("stat", path1);
4294 err = got_diff_files(&changes, &ds, &args, &diff_flags,
4295 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
4299 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4303 if (fseek(f1, 0L, SEEK_SET) == -1)
4304 return got_ferror(f1, GOT_ERR_IO);
4305 if (fseek(f2, 0L, SEEK_SET) == -1)
4306 return got_ferror(f2, GOT_ERR_IO);
4307 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
4309 err = apply_or_reject_change(&choice, change, ++n,
4310 changes->nchanges, ds, args, diff_flags, relpath,
4311 f1, f2, &line_cur1, &line_cur2,
4312 reverse_patch ? NULL : outfile,
4313 reverse_patch ? outfile : NULL,
4314 patch_cb, patch_arg);
4317 if (choice == GOT_PATCH_CHOICE_YES)
4319 else if (choice == GOT_PATCH_CHOICE_QUIT)
4323 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4324 reverse_patch ? NULL : outfile,
4325 reverse_patch ? outfile : NULL);
4329 if (!S_ISLNK(sb2.st_mode)) {
4330 if (chmod(*path_outfile, sb2.st_mode) == -1) {
4331 err = got_error_from_errno2("chmod", path2);
4339 got_object_blob_close(blob);
4340 if (f1 && fclose(f1) == EOF && err == NULL)
4341 err = got_error_from_errno2("fclose", path1);
4342 if (f2 && fclose(f2) == EOF && err == NULL)
4343 err = got_error_from_errno2("fclose", path2);
4344 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4345 err = got_error_from_errno2("close", path2);
4346 if (outfile && fclose(outfile) == EOF && err == NULL)
4347 err = got_error_from_errno2("fclose", *path_outfile);
4348 if (path1 && unlink(path1) == -1 && err == NULL)
4349 err = got_error_from_errno2("unlink", path1);
4350 if (err || !have_content) {
4351 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4352 err = got_error_from_errno2("unlink", *path_outfile);
4353 free(*path_outfile);
4354 *path_outfile = NULL;
4358 got_diff_state_free(ds);
4362 got_diff_free_changes(changes);
4367 static const struct got_error *
4368 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4369 const char *relpath, struct got_object_id *blob_id,
4370 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4371 int dirfd, const char *de_name)
4373 struct revert_file_args *a = arg;
4374 const struct got_error *err = NULL;
4375 char *parent_path = NULL;
4376 struct got_fileindex_entry *ie;
4377 struct got_tree_object *tree = NULL;
4378 struct got_object_id *tree_id = NULL;
4379 const struct got_tree_entry *te = NULL;
4380 char *tree_path = NULL, *te_name;
4381 char *ondisk_path = NULL, *path_content = NULL;
4382 struct got_blob_object *blob = NULL;
4384 /* Reverting a staged deletion is a no-op. */
4385 if (status == GOT_STATUS_DELETE &&
4386 staged_status != GOT_STATUS_NO_CHANGE)
4389 if (status == GOT_STATUS_UNVERSIONED)
4390 return (*a->progress_cb)(a->progress_arg,
4391 GOT_STATUS_UNVERSIONED, relpath);
4393 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4395 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4397 /* Construct in-repository path of tree which contains this blob. */
4398 err = got_path_dirname(&parent_path, ie->path);
4400 if (err->code != GOT_ERR_BAD_PATH)
4402 parent_path = strdup("/");
4403 if (parent_path == NULL) {
4404 err = got_error_from_errno("strdup");
4408 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4409 tree_path = strdup(parent_path);
4410 if (tree_path == NULL) {
4411 err = got_error_from_errno("strdup");
4415 if (got_path_is_root_dir(parent_path)) {
4416 tree_path = strdup(a->worktree->path_prefix);
4417 if (tree_path == NULL) {
4418 err = got_error_from_errno("strdup");
4422 if (asprintf(&tree_path, "%s/%s",
4423 a->worktree->path_prefix, parent_path) == -1) {
4424 err = got_error_from_errno("asprintf");
4430 err = got_object_id_by_path(&tree_id, a->repo,
4431 a->worktree->base_commit_id, tree_path);
4433 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4434 (status == GOT_STATUS_ADD ||
4435 staged_status == GOT_STATUS_ADD)))
4438 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4442 err = got_path_basename(&te_name, ie->path);
4446 te = got_object_tree_find_entry(tree, te_name);
4448 if (te == NULL && status != GOT_STATUS_ADD &&
4449 staged_status != GOT_STATUS_ADD) {
4450 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4456 case GOT_STATUS_ADD:
4458 int choice = GOT_PATCH_CHOICE_NONE;
4459 err = (*a->patch_cb)(&choice, a->patch_arg,
4460 status, ie->path, NULL, 1, 1);
4463 if (choice != GOT_PATCH_CHOICE_YES)
4466 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4470 got_fileindex_entry_remove(a->fileindex, ie);
4472 case GOT_STATUS_DELETE:
4474 int choice = GOT_PATCH_CHOICE_NONE;
4475 err = (*a->patch_cb)(&choice, a->patch_arg,
4476 status, ie->path, NULL, 1, 1);
4479 if (choice != GOT_PATCH_CHOICE_YES)
4483 case GOT_STATUS_MODIFY:
4484 case GOT_STATUS_MODE_CHANGE:
4485 case GOT_STATUS_CONFLICT:
4486 case GOT_STATUS_MISSING: {
4487 struct got_object_id id;
4488 if (staged_status == GOT_STATUS_ADD ||
4489 staged_status == GOT_STATUS_MODIFY) {
4490 memcpy(id.sha1, ie->staged_blob_sha1,
4491 SHA1_DIGEST_LENGTH);
4493 memcpy(id.sha1, ie->blob_sha1,
4494 SHA1_DIGEST_LENGTH);
4495 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4499 if (asprintf(&ondisk_path, "%s/%s",
4500 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4501 err = got_error_from_errno("asprintf");
4505 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4506 status == GOT_STATUS_CONFLICT)) {
4507 int is_bad_symlink = 0;
4508 err = create_patched_content(&path_content, 1, &id,
4509 ondisk_path, dirfd, de_name, ie->path, a->repo,
4510 a->patch_cb, a->patch_arg);
4511 if (err || path_content == NULL)
4513 if (te && S_ISLNK(te->mode)) {
4514 if (unlink(path_content) == -1) {
4515 err = got_error_from_errno2("unlink",
4519 err = install_symlink(&is_bad_symlink,
4520 a->worktree, ondisk_path, ie->path,
4521 blob, 0, 1, 0, a->repo,
4522 a->progress_cb, a->progress_arg);
4524 if (rename(path_content, ondisk_path) == -1) {
4525 err = got_error_from_errno3("rename",
4526 path_content, ondisk_path);
4531 int is_bad_symlink = 0;
4532 if (te && S_ISLNK(te->mode)) {
4533 err = install_symlink(&is_bad_symlink,
4534 a->worktree, ondisk_path, ie->path,
4535 blob, 0, 1, 0, a->repo,
4536 a->progress_cb, a->progress_arg);
4538 err = install_blob(a->worktree, ondisk_path,
4540 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4541 got_fileindex_perms_to_st(ie), blob,
4542 0, 1, 0, 0, a->repo,
4543 a->progress_cb, a->progress_arg);
4547 if (status == GOT_STATUS_DELETE ||
4548 status == GOT_STATUS_MODE_CHANGE) {
4549 err = got_fileindex_entry_update(ie,
4550 ondisk_path, blob->id.sha1,
4551 a->worktree->base_commit_id->sha1, 1);
4555 if (is_bad_symlink) {
4556 got_fileindex_entry_filetype_set(ie,
4557 GOT_FILEIDX_MODE_BAD_SYMLINK);
4571 got_object_blob_close(blob);
4573 got_object_tree_close(tree);
4578 const struct got_error *
4579 got_worktree_revert(struct got_worktree *worktree,
4580 struct got_pathlist_head *paths,
4581 got_worktree_checkout_cb progress_cb, void *progress_arg,
4582 got_worktree_patch_cb patch_cb, void *patch_arg,
4583 struct got_repository *repo)
4585 struct got_fileindex *fileindex = NULL;
4586 char *fileindex_path = NULL;
4587 const struct got_error *err = NULL, *unlockerr = NULL;
4588 const struct got_error *sync_err = NULL;
4589 struct got_pathlist_entry *pe;
4590 struct revert_file_args rfa;
4592 err = lock_worktree(worktree, LOCK_EX);
4596 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4600 rfa.worktree = worktree;
4601 rfa.fileindex = fileindex;
4602 rfa.progress_cb = progress_cb;
4603 rfa.progress_arg = progress_arg;
4604 rfa.patch_cb = patch_cb;
4605 rfa.patch_arg = patch_arg;
4607 TAILQ_FOREACH(pe, paths, entry) {
4608 err = worktree_status(worktree, pe->path, fileindex, repo,
4609 revert_file, &rfa, NULL, NULL, 0, 0);
4613 sync_err = sync_fileindex(fileindex, fileindex_path);
4614 if (sync_err && err == NULL)
4617 free(fileindex_path);
4619 got_fileindex_free(fileindex);
4620 unlockerr = lock_worktree(worktree, LOCK_SH);
4621 if (unlockerr && err == NULL)
4627 free_commitable(struct got_commitable *ct)
4630 free(ct->in_repo_path);
4631 free(ct->ondisk_path);
4633 free(ct->base_blob_id);
4634 free(ct->staged_blob_id);
4635 free(ct->base_commit_id);
4639 struct collect_commitables_arg {
4640 struct got_pathlist_head *commitable_paths;
4641 struct got_repository *repo;
4642 struct got_worktree *worktree;
4643 struct got_fileindex *fileindex;
4644 int have_staged_files;
4645 int allow_bad_symlinks;
4648 static const struct got_error *
4649 collect_commitables(void *arg, unsigned char status,
4650 unsigned char staged_status, const char *relpath,
4651 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4652 struct got_object_id *commit_id, int dirfd, const char *de_name)
4654 struct collect_commitables_arg *a = arg;
4655 const struct got_error *err = NULL;
4656 struct got_commitable *ct = NULL;
4657 struct got_pathlist_entry *new = NULL;
4658 char *parent_path = NULL, *path = NULL;
4661 if (a->have_staged_files) {
4662 if (staged_status != GOT_STATUS_MODIFY &&
4663 staged_status != GOT_STATUS_ADD &&
4664 staged_status != GOT_STATUS_DELETE)
4667 if (status == GOT_STATUS_CONFLICT)
4668 return got_error(GOT_ERR_COMMIT_CONFLICT);
4670 if (status != GOT_STATUS_MODIFY &&
4671 status != GOT_STATUS_MODE_CHANGE &&
4672 status != GOT_STATUS_ADD &&
4673 status != GOT_STATUS_DELETE)
4677 if (asprintf(&path, "/%s", relpath) == -1) {
4678 err = got_error_from_errno("asprintf");
4681 if (strcmp(path, "/") == 0) {
4682 parent_path = strdup("");
4683 if (parent_path == NULL)
4684 return got_error_from_errno("strdup");
4686 err = got_path_dirname(&parent_path, path);
4691 ct = calloc(1, sizeof(*ct));
4693 err = got_error_from_errno("calloc");
4697 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4699 err = got_error_from_errno("asprintf");
4703 if (staged_status == GOT_STATUS_ADD ||
4704 staged_status == GOT_STATUS_MODIFY) {
4705 struct got_fileindex_entry *ie;
4706 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4707 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4708 case GOT_FILEIDX_MODE_REGULAR_FILE:
4709 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4712 case GOT_FILEIDX_MODE_SYMLINK:
4716 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4719 ct->mode |= got_fileindex_entry_perms_get(ie);
4720 } else if (status != GOT_STATUS_DELETE &&
4721 staged_status != GOT_STATUS_DELETE) {
4723 if (fstatat(dirfd, de_name, &sb,
4724 AT_SYMLINK_NOFOLLOW) == -1) {
4725 err = got_error_from_errno2("fstatat",
4729 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4730 err = got_error_from_errno2("lstat", ct->ondisk_path);
4733 ct->mode = sb.st_mode;
4736 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4737 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4739 err = got_error_from_errno("asprintf");
4743 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4744 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4746 char target_path[PATH_MAX];
4748 target_len = readlink(ct->ondisk_path, target_path,
4749 sizeof(target_path));
4750 if (target_len == -1) {
4751 err = got_error_from_errno2("readlink",
4755 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4756 target_len, ct->ondisk_path, a->worktree->root_path);
4759 if (is_bad_symlink) {
4760 err = got_error_path(ct->ondisk_path,
4761 GOT_ERR_BAD_SYMLINK);
4767 ct->status = status;
4768 ct->staged_status = staged_status;
4769 ct->blob_id = NULL; /* will be filled in when blob gets created */
4770 if (ct->status != GOT_STATUS_ADD &&
4771 ct->staged_status != GOT_STATUS_ADD) {
4772 ct->base_blob_id = got_object_id_dup(blob_id);
4773 if (ct->base_blob_id == NULL) {
4774 err = got_error_from_errno("got_object_id_dup");
4777 ct->base_commit_id = got_object_id_dup(commit_id);
4778 if (ct->base_commit_id == NULL) {
4779 err = got_error_from_errno("got_object_id_dup");
4783 if (ct->staged_status == GOT_STATUS_ADD ||
4784 ct->staged_status == GOT_STATUS_MODIFY) {
4785 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4786 if (ct->staged_blob_id == NULL) {
4787 err = got_error_from_errno("got_object_id_dup");
4791 ct->path = strdup(path);
4792 if (ct->path == NULL) {
4793 err = got_error_from_errno("strdup");
4796 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4798 if (ct && (err || new == NULL))
4799 free_commitable(ct);
4805 static const struct got_error *write_tree(struct got_object_id **, int *,
4806 struct got_tree_object *, const char *, struct got_pathlist_head *,
4807 got_worktree_status_cb status_cb, void *status_arg,
4808 struct got_repository *);
4810 static const struct got_error *
4811 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4812 struct got_tree_entry *te, const char *parent_path,
4813 struct got_pathlist_head *commitable_paths,
4814 got_worktree_status_cb status_cb, void *status_arg,
4815 struct got_repository *repo)
4817 const struct got_error *err = NULL;
4818 struct got_tree_object *subtree;
4821 if (asprintf(&subpath, "%s%s%s", parent_path,
4822 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4823 return got_error_from_errno("asprintf");
4825 err = got_object_open_as_tree(&subtree, repo, &te->id);
4829 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4830 commitable_paths, status_cb, status_arg, repo);
4831 got_object_tree_close(subtree);
4836 static const struct got_error *
4837 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4839 const struct got_error *err = NULL;
4840 char *ct_parent_path = NULL;
4844 if (strchr(ct->in_repo_path, '/') == NULL) {
4845 *match = got_path_is_root_dir(path);
4849 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4852 *match = (strcmp(path, ct_parent_path) == 0);
4853 free(ct_parent_path);
4858 get_ct_file_mode(struct got_commitable *ct)
4860 if (S_ISLNK(ct->mode))
4863 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4866 static const struct got_error *
4867 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4868 struct got_tree_entry *te, struct got_commitable *ct)
4870 const struct got_error *err = NULL;
4874 err = got_object_tree_entry_dup(new_te, te);
4878 (*new_te)->mode = get_ct_file_mode(ct);
4880 if (ct->staged_status == GOT_STATUS_MODIFY)
4881 memcpy(&(*new_te)->id, ct->staged_blob_id,
4882 sizeof((*new_te)->id));
4884 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4886 if (err && *new_te) {
4893 static const struct got_error *
4894 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4895 struct got_commitable *ct)
4897 const struct got_error *err = NULL;
4898 char *ct_name = NULL;
4902 *new_te = calloc(1, sizeof(**new_te));
4903 if (*new_te == NULL)
4904 return got_error_from_errno("calloc");
4906 err = got_path_basename(&ct_name, ct->path);
4909 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4910 sizeof((*new_te)->name)) {
4911 err = got_error(GOT_ERR_NO_SPACE);
4915 (*new_te)->mode = get_ct_file_mode(ct);
4917 if (ct->staged_status == GOT_STATUS_ADD)
4918 memcpy(&(*new_te)->id, ct->staged_blob_id,
4919 sizeof((*new_te)->id));
4921 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4924 if (err && *new_te) {
4931 static const struct got_error *
4932 insert_tree_entry(struct got_tree_entry *new_te,
4933 struct got_pathlist_head *paths)
4935 const struct got_error *err = NULL;
4936 struct got_pathlist_entry *new_pe;
4938 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4942 return got_error(GOT_ERR_TREE_DUP_ENTRY);
4946 static const struct got_error *
4947 report_ct_status(struct got_commitable *ct,
4948 got_worktree_status_cb status_cb, void *status_arg)
4950 const char *ct_path = ct->path;
4951 unsigned char status;
4953 while (ct_path[0] == '/')
4956 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4957 status = ct->staged_status;
4959 status = ct->status;
4961 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4962 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4965 static const struct got_error *
4966 match_modified_subtree(int *modified, struct got_tree_entry *te,
4967 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4969 const struct got_error *err = NULL;
4970 struct got_pathlist_entry *pe;
4975 if (asprintf(&te_path, "%s%s%s", base_tree_path,
4976 got_path_is_root_dir(base_tree_path) ? "" : "/",
4978 return got_error_from_errno("asprintf");
4980 TAILQ_FOREACH(pe, commitable_paths, entry) {
4981 struct got_commitable *ct = pe->data;
4982 *modified = got_path_is_child(ct->in_repo_path, te_path,
4992 static const struct got_error *
4993 match_deleted_or_modified_ct(struct got_commitable **ctp,
4994 struct got_tree_entry *te, const char *base_tree_path,
4995 struct got_pathlist_head *commitable_paths)
4997 const struct got_error *err = NULL;
4998 struct got_pathlist_entry *pe;
5002 TAILQ_FOREACH(pe, commitable_paths, entry) {
5003 struct got_commitable *ct = pe->data;
5004 char *ct_name = NULL;
5007 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5008 if (ct->status != GOT_STATUS_MODIFY &&
5009 ct->status != GOT_STATUS_MODE_CHANGE &&
5010 ct->status != GOT_STATUS_DELETE)
5013 if (ct->staged_status != GOT_STATUS_MODIFY &&
5014 ct->staged_status != GOT_STATUS_DELETE)
5018 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5021 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5027 err = got_path_basename(&ct_name, pe->path);
5031 if (strcmp(te->name, ct_name) != 0) {
5044 static const struct got_error *
5045 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5046 const char *child_path, const char *path_base_tree,
5047 struct got_pathlist_head *commitable_paths,
5048 got_worktree_status_cb status_cb, void *status_arg,
5049 struct got_repository *repo)
5051 const struct got_error *err = NULL;
5052 struct got_tree_entry *new_te;
5054 struct got_object_id *id = NULL;
5059 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5060 got_path_is_root_dir(path_base_tree) ? "" : "/",
5062 return got_error_from_errno("asprintf");
5064 new_te = calloc(1, sizeof(*new_te));
5066 return got_error_from_errno("calloc");
5067 new_te->mode = S_IFDIR;
5069 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5070 sizeof(new_te->name)) {
5071 err = got_error(GOT_ERR_NO_SPACE);
5074 err = write_tree(&id, &nentries, NULL, subtree_path,
5075 commitable_paths, status_cb, status_arg, repo);
5080 memcpy(&new_te->id, id, sizeof(new_te->id));
5089 static const struct got_error *
5090 write_tree(struct got_object_id **new_tree_id, int *nentries,
5091 struct got_tree_object *base_tree, const char *path_base_tree,
5092 struct got_pathlist_head *commitable_paths,
5093 got_worktree_status_cb status_cb, void *status_arg,
5094 struct got_repository *repo)
5096 const struct got_error *err = NULL;
5097 struct got_pathlist_head paths;
5098 struct got_tree_entry *te, *new_te = NULL;
5099 struct got_pathlist_entry *pe;
5104 /* Insert, and recurse into, newly added entries first. */
5105 TAILQ_FOREACH(pe, commitable_paths, entry) {
5106 struct got_commitable *ct = pe->data;
5107 char *child_path = NULL, *slash;
5109 if ((ct->status != GOT_STATUS_ADD &&
5110 ct->staged_status != GOT_STATUS_ADD) ||
5111 (ct->flags & GOT_COMMITABLE_ADDED))
5114 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5115 strlen(path_base_tree)))
5118 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5123 slash = strchr(child_path, '/');
5124 if (slash == NULL) {
5125 err = alloc_added_blob_tree_entry(&new_te, ct);
5128 err = report_ct_status(ct, status_cb, status_arg);
5131 ct->flags |= GOT_COMMITABLE_ADDED;
5132 err = insert_tree_entry(new_te, &paths);
5137 *slash = '\0'; /* trim trailing path components */
5138 if (base_tree == NULL ||
5139 got_object_tree_find_entry(base_tree, child_path)
5141 err = make_subtree_for_added_blob(&new_te,
5142 child_path, path_base_tree,
5143 commitable_paths, status_cb, status_arg,
5147 err = insert_tree_entry(new_te, &paths);
5156 int i, nbase_entries;
5157 /* Handle modified and deleted entries. */
5158 nbase_entries = got_object_tree_get_nentries(base_tree);
5159 for (i = 0; i < nbase_entries; i++) {
5160 struct got_commitable *ct = NULL;
5162 te = got_object_tree_get_entry(base_tree, i);
5163 if (got_object_tree_entry_is_submodule(te)) {
5164 /* Entry is a submodule; just copy it. */
5165 err = got_object_tree_entry_dup(&new_te, te);
5168 err = insert_tree_entry(new_te, &paths);
5175 if (S_ISDIR(te->mode)) {
5177 err = got_object_tree_entry_dup(&new_te, te);
5180 err = match_modified_subtree(&modified, te,
5181 path_base_tree, commitable_paths);
5184 /* Avoid recursion into unmodified subtrees. */
5186 struct got_object_id *new_id;
5188 err = write_subtree(&new_id,
5190 path_base_tree, commitable_paths,
5191 status_cb, status_arg, repo);
5194 if (nsubentries == 0) {
5195 /* All entries were deleted. */
5199 memcpy(&new_te->id, new_id,
5200 sizeof(new_te->id));
5203 err = insert_tree_entry(new_te, &paths);
5210 err = match_deleted_or_modified_ct(&ct, te,
5211 path_base_tree, commitable_paths);
5215 /* NB: Deleted entries get dropped here. */
5216 if (ct->status == GOT_STATUS_MODIFY ||
5217 ct->status == GOT_STATUS_MODE_CHANGE ||
5218 ct->staged_status == GOT_STATUS_MODIFY) {
5219 err = alloc_modified_blob_tree_entry(
5223 err = insert_tree_entry(new_te, &paths);
5228 err = report_ct_status(ct, status_cb,
5233 /* Entry is unchanged; just copy it. */
5234 err = got_object_tree_entry_dup(&new_te, te);
5237 err = insert_tree_entry(new_te, &paths);
5245 /* Write new list of entries; deleted entries have been dropped. */
5246 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5248 got_pathlist_free(&paths);
5252 static const struct got_error *
5253 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
5254 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
5255 int have_staged_files)
5257 const struct got_error *err = NULL;
5258 struct got_pathlist_entry *pe;
5260 TAILQ_FOREACH(pe, commitable_paths, entry) {
5261 struct got_fileindex_entry *ie;
5262 struct got_commitable *ct = pe->data;
5264 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5266 if (ct->status == GOT_STATUS_DELETE ||
5267 ct->staged_status == GOT_STATUS_DELETE) {
5268 got_fileindex_entry_remove(fileindex, ie);
5269 } else if (ct->staged_status == GOT_STATUS_ADD ||
5270 ct->staged_status == GOT_STATUS_MODIFY) {
5271 got_fileindex_entry_stage_set(ie,
5272 GOT_FILEIDX_STAGE_NONE);
5273 err = got_fileindex_entry_update(ie,
5274 ct->ondisk_path, ct->staged_blob_id->sha1,
5275 new_base_commit_id->sha1,
5276 !have_staged_files);
5278 err = got_fileindex_entry_update(ie,
5279 ct->ondisk_path, ct->blob_id->sha1,
5280 new_base_commit_id->sha1,
5281 !have_staged_files);
5283 err = got_fileindex_entry_alloc(&ie, pe->path);
5286 err = got_fileindex_entry_update(ie, ct->ondisk_path,
5287 ct->blob_id->sha1, new_base_commit_id->sha1, 1);
5289 got_fileindex_entry_free(ie);
5292 err = got_fileindex_entry_add(fileindex, ie);
5294 got_fileindex_entry_free(ie);
5303 static const struct got_error *
5304 check_out_of_date(const char *in_repo_path, unsigned char status,
5305 unsigned char staged_status, struct got_object_id *base_blob_id,
5306 struct got_object_id *base_commit_id,
5307 struct got_object_id *head_commit_id, struct got_repository *repo,
5310 const struct got_error *err = NULL;
5311 struct got_object_id *id = NULL;
5313 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5314 /* Trivial case: base commit == head commit */
5315 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5318 * Ensure file content which local changes were based
5319 * on matches file content in the branch head.
5321 err = got_object_id_by_path(&id, repo, head_commit_id,
5324 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5325 err = got_error(ood_errcode);
5327 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5328 err = got_error(ood_errcode);
5330 /* Require that added files don't exist in the branch head. */
5331 err = got_object_id_by_path(&id, repo, head_commit_id,
5333 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5335 err = id ? got_error(ood_errcode) : NULL;
5342 const struct got_error *
5343 commit_worktree(struct got_object_id **new_commit_id,
5344 struct got_pathlist_head *commitable_paths,
5345 struct got_object_id *head_commit_id, struct got_worktree *worktree,
5346 const char *author, const char *committer,
5347 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5348 got_worktree_status_cb status_cb, void *status_arg,
5349 struct got_repository *repo)
5351 const struct got_error *err = NULL, *unlockerr = NULL;
5352 struct got_pathlist_entry *pe;
5353 const char *head_ref_name = NULL;
5354 struct got_commit_object *head_commit = NULL;
5355 struct got_reference *head_ref2 = NULL;
5356 struct got_object_id *head_commit_id2 = NULL;
5357 struct got_tree_object *head_tree = NULL;
5358 struct got_object_id *new_tree_id = NULL;
5360 struct got_object_id_queue parent_ids;
5361 struct got_object_qid *pid = NULL;
5362 char *logmsg = NULL;
5364 *new_commit_id = NULL;
5366 SIMPLEQ_INIT(&parent_ids);
5368 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5372 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5376 if (commit_msg_cb != NULL) {
5377 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5382 if (logmsg == NULL || strlen(logmsg) == 0) {
5383 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5387 /* Create blobs from added and modified files and record their IDs. */
5388 TAILQ_FOREACH(pe, commitable_paths, entry) {
5389 struct got_commitable *ct = pe->data;
5392 /* Blobs for staged files already exist. */
5393 if (ct->staged_status == GOT_STATUS_ADD ||
5394 ct->staged_status == GOT_STATUS_MODIFY)
5397 if (ct->status != GOT_STATUS_ADD &&
5398 ct->status != GOT_STATUS_MODIFY &&
5399 ct->status != GOT_STATUS_MODE_CHANGE)
5402 if (asprintf(&ondisk_path, "%s/%s",
5403 worktree->root_path, pe->path) == -1) {
5404 err = got_error_from_errno("asprintf");
5407 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5413 /* Recursively write new tree objects. */
5414 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5415 commitable_paths, status_cb, status_arg, repo);
5419 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5422 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5423 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5424 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5425 got_object_qid_free(pid);
5431 /* Check if a concurrent commit to our branch has occurred. */
5432 head_ref_name = got_worktree_get_head_ref_name(worktree);
5433 if (head_ref_name == NULL) {
5434 err = got_error_from_errno("got_worktree_get_head_ref_name");
5437 /* Lock the reference here to prevent concurrent modification. */
5438 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5441 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5444 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5445 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5448 /* Update branch head in repository. */
5449 err = got_ref_change_ref(head_ref2, *new_commit_id);
5452 err = got_ref_write(head_ref2, repo);
5456 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5460 err = ref_base_commit(worktree, repo);
5465 got_object_tree_close(head_tree);
5467 got_object_commit_close(head_commit);
5468 free(head_commit_id2);
5470 unlockerr = got_ref_unlock(head_ref2);
5471 if (unlockerr && err == NULL)
5473 got_ref_close(head_ref2);
5478 static const struct got_error *
5479 check_path_is_commitable(const char *path,
5480 struct got_pathlist_head *commitable_paths)
5482 struct got_pathlist_entry *cpe = NULL;
5483 size_t path_len = strlen(path);
5485 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5486 struct got_commitable *ct = cpe->data;
5487 const char *ct_path = ct->path;
5489 while (ct_path[0] == '/')
5492 if (strcmp(path, ct_path) == 0 ||
5493 got_path_is_child(ct_path, path, path_len))
5498 return got_error_path(path, GOT_ERR_BAD_PATH);
5503 static const struct got_error *
5504 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5506 int *have_staged_files = arg;
5508 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5509 *have_staged_files = 1;
5510 return got_error(GOT_ERR_CANCELLED);
5516 static const struct got_error *
5517 check_non_staged_files(struct got_fileindex *fileindex,
5518 struct got_pathlist_head *paths)
5520 struct got_pathlist_entry *pe;
5521 struct got_fileindex_entry *ie;
5523 TAILQ_FOREACH(pe, paths, entry) {
5524 if (pe->path[0] == '\0')
5526 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5528 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5529 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5530 return got_error_path(pe->path,
5531 GOT_ERR_FILE_NOT_STAGED);
5537 const struct got_error *
5538 got_worktree_commit(struct got_object_id **new_commit_id,
5539 struct got_worktree *worktree, struct got_pathlist_head *paths,
5540 const char *author, const char *committer, int allow_bad_symlinks,
5541 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5542 got_worktree_status_cb status_cb, void *status_arg,
5543 struct got_repository *repo)
5545 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5546 struct got_fileindex *fileindex = NULL;
5547 char *fileindex_path = NULL;
5548 struct got_pathlist_head commitable_paths;
5549 struct collect_commitables_arg cc_arg;
5550 struct got_pathlist_entry *pe;
5551 struct got_reference *head_ref = NULL;
5552 struct got_object_id *head_commit_id = NULL;
5553 int have_staged_files = 0;
5555 *new_commit_id = NULL;
5557 TAILQ_INIT(&commitable_paths);
5559 err = lock_worktree(worktree, LOCK_EX);
5563 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5567 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5571 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5575 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5576 &have_staged_files);
5577 if (err && err->code != GOT_ERR_CANCELLED)
5579 if (have_staged_files) {
5580 err = check_non_staged_files(fileindex, paths);
5585 cc_arg.commitable_paths = &commitable_paths;
5586 cc_arg.worktree = worktree;
5587 cc_arg.fileindex = fileindex;
5589 cc_arg.have_staged_files = have_staged_files;
5590 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5591 TAILQ_FOREACH(pe, paths, entry) {
5592 err = worktree_status(worktree, pe->path, fileindex, repo,
5593 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5598 if (TAILQ_EMPTY(&commitable_paths)) {
5599 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5603 TAILQ_FOREACH(pe, paths, entry) {
5604 err = check_path_is_commitable(pe->path, &commitable_paths);
5609 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5610 struct got_commitable *ct = pe->data;
5611 const char *ct_path = ct->in_repo_path;
5613 while (ct_path[0] == '/')
5615 err = check_out_of_date(ct_path, ct->status,
5616 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5617 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5623 err = commit_worktree(new_commit_id, &commitable_paths,
5624 head_commit_id, worktree, author, committer,
5625 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5629 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5630 fileindex, have_staged_files);
5631 sync_err = sync_fileindex(fileindex, fileindex_path);
5632 if (sync_err && err == NULL)
5636 got_fileindex_free(fileindex);
5637 free(fileindex_path);
5638 unlockerr = lock_worktree(worktree, LOCK_SH);
5639 if (unlockerr && err == NULL)
5641 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5642 struct got_commitable *ct = pe->data;
5643 free_commitable(ct);
5645 got_pathlist_free(&commitable_paths);
5650 got_commitable_get_path(struct got_commitable *ct)
5656 got_commitable_get_status(struct got_commitable *ct)
5661 struct check_rebase_ok_arg {
5662 struct got_worktree *worktree;
5663 struct got_repository *repo;
5666 static const struct got_error *
5667 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5669 const struct got_error *err = NULL;
5670 struct check_rebase_ok_arg *a = arg;
5671 unsigned char status;
5675 /* Reject rebase of a work tree with mixed base commits. */
5676 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5677 SHA1_DIGEST_LENGTH))
5678 return got_error(GOT_ERR_MIXED_COMMITS);
5680 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5682 return got_error_from_errno("asprintf");
5684 /* Reject rebase of a work tree with modified or staged files. */
5685 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5690 if (status != GOT_STATUS_NO_CHANGE)
5691 return got_error(GOT_ERR_MODIFIED);
5692 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5693 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5698 const struct got_error *
5699 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5700 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5701 struct got_worktree *worktree, struct got_reference *branch,
5702 struct got_repository *repo)
5704 const struct got_error *err = NULL;
5705 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5706 char *branch_ref_name = NULL;
5707 char *fileindex_path = NULL;
5708 struct check_rebase_ok_arg ok_arg;
5709 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5710 struct got_object_id *wt_branch_tip = NULL;
5712 *new_base_branch_ref = NULL;
5716 err = lock_worktree(worktree, LOCK_EX);
5720 err = open_fileindex(fileindex, &fileindex_path, worktree);
5724 ok_arg.worktree = worktree;
5726 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5731 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5735 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5739 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5743 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5748 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5751 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5752 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5756 err = got_ref_alloc_symref(new_base_branch_ref,
5757 new_base_branch_ref_name, wt_branch);
5760 err = got_ref_write(*new_base_branch_ref, repo);
5764 /* TODO Lock original branch's ref while rebasing? */
5766 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5770 err = got_ref_write(branch_ref, repo);
5774 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5775 worktree->base_commit_id);
5778 err = got_ref_write(*tmp_branch, repo);
5782 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5786 free(fileindex_path);
5787 free(tmp_branch_name);
5788 free(new_base_branch_ref_name);
5789 free(branch_ref_name);
5791 got_ref_close(branch_ref);
5793 got_ref_close(wt_branch);
5794 free(wt_branch_tip);
5796 if (*new_base_branch_ref) {
5797 got_ref_close(*new_base_branch_ref);
5798 *new_base_branch_ref = NULL;
5801 got_ref_close(*tmp_branch);
5805 got_fileindex_free(*fileindex);
5808 lock_worktree(worktree, LOCK_SH);
5813 const struct got_error *
5814 got_worktree_rebase_continue(struct got_object_id **commit_id,
5815 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5816 struct got_reference **branch, struct got_fileindex **fileindex,
5817 struct got_worktree *worktree, struct got_repository *repo)
5819 const struct got_error *err;
5820 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5821 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5822 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5823 char *fileindex_path = NULL;
5824 int have_staged_files = 0;
5827 *new_base_branch = NULL;
5832 err = lock_worktree(worktree, LOCK_EX);
5836 err = open_fileindex(fileindex, &fileindex_path, worktree);
5840 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5841 &have_staged_files);
5842 if (err && err->code != GOT_ERR_CANCELLED)
5844 if (have_staged_files) {
5845 err = got_error(GOT_ERR_STAGED_PATHS);
5849 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5853 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5857 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5861 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5865 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5869 err = got_ref_open(branch, repo,
5870 got_ref_get_symref_target(branch_ref), 0);
5874 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5878 err = got_ref_resolve(commit_id, repo, commit_ref);
5882 err = got_ref_open(new_base_branch, repo,
5883 new_base_branch_ref_name, 0);
5887 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5891 free(commit_ref_name);
5892 free(branch_ref_name);
5893 free(fileindex_path);
5895 got_ref_close(commit_ref);
5897 got_ref_close(branch_ref);
5902 got_ref_close(*tmp_branch);
5905 if (*new_base_branch) {
5906 got_ref_close(*new_base_branch);
5907 *new_base_branch = NULL;
5910 got_ref_close(*branch);
5914 got_fileindex_free(*fileindex);
5917 lock_worktree(worktree, LOCK_SH);
5922 const struct got_error *
5923 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5925 const struct got_error *err;
5926 char *tmp_branch_name = NULL;
5928 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5932 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5933 free(tmp_branch_name);
5937 static const struct got_error *
5938 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5939 char **logmsg, void *arg)
5945 static const struct got_error *
5946 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5947 const char *path, struct got_object_id *blob_id,
5948 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5949 int dirfd, const char *de_name)
5954 struct collect_merged_paths_arg {
5955 got_worktree_checkout_cb progress_cb;
5957 struct got_pathlist_head *merged_paths;
5960 static const struct got_error *
5961 collect_merged_paths(void *arg, unsigned char status, const char *path)
5963 const struct got_error *err;
5964 struct collect_merged_paths_arg *a = arg;
5966 struct got_pathlist_entry *new;
5968 err = (*a->progress_cb)(a->progress_arg, status, path);
5972 if (status != GOT_STATUS_MERGE &&
5973 status != GOT_STATUS_ADD &&
5974 status != GOT_STATUS_DELETE &&
5975 status != GOT_STATUS_CONFLICT)
5980 return got_error_from_errno("strdup");
5982 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5983 if (err || new == NULL)
5989 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5991 struct got_pathlist_entry *pe;
5993 TAILQ_FOREACH(pe, merged_paths, entry)
5994 free((char *)pe->path);
5996 got_pathlist_free(merged_paths);
5999 static const struct got_error *
6000 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6001 int is_rebase, struct got_repository *repo)
6003 const struct got_error *err;
6004 struct got_reference *commit_ref = NULL;
6006 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6008 if (err->code != GOT_ERR_NOT_REF)
6010 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6013 err = got_ref_write(commit_ref, repo);
6016 } else if (is_rebase) {
6017 struct got_object_id *stored_id;
6020 err = got_ref_resolve(&stored_id, repo, commit_ref);
6023 cmp = got_object_id_cmp(commit_id, stored_id);
6026 err = got_error(GOT_ERR_REBASE_COMMITID);
6032 got_ref_close(commit_ref);
6036 static const struct got_error *
6037 rebase_merge_files(struct got_pathlist_head *merged_paths,
6038 const char *commit_ref_name, struct got_worktree *worktree,
6039 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6040 struct got_object_id *commit_id, struct got_repository *repo,
6041 got_worktree_checkout_cb progress_cb, void *progress_arg,
6042 got_cancel_cb cancel_cb, void *cancel_arg)
6044 const struct got_error *err;
6045 struct got_reference *commit_ref = NULL;
6046 struct collect_merged_paths_arg cmp_arg;
6047 char *fileindex_path;
6049 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6051 err = get_fileindex_path(&fileindex_path, worktree);
6055 cmp_arg.progress_cb = progress_cb;
6056 cmp_arg.progress_arg = progress_arg;
6057 cmp_arg.merged_paths = merged_paths;
6058 err = merge_files(worktree, fileindex, fileindex_path,
6059 parent_commit_id, commit_id, repo, collect_merged_paths,
6060 &cmp_arg, cancel_cb, cancel_arg);
6062 got_ref_close(commit_ref);
6066 const struct got_error *
6067 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6068 struct got_worktree *worktree, struct got_fileindex *fileindex,
6069 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6070 struct got_repository *repo,
6071 got_worktree_checkout_cb progress_cb, void *progress_arg,
6072 got_cancel_cb cancel_cb, void *cancel_arg)
6074 const struct got_error *err;
6075 char *commit_ref_name;
6077 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6081 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6085 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6086 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6087 progress_arg, cancel_cb, cancel_arg);
6089 free(commit_ref_name);
6093 const struct got_error *
6094 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6095 struct got_worktree *worktree, struct got_fileindex *fileindex,
6096 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6097 struct got_repository *repo,
6098 got_worktree_checkout_cb progress_cb, void *progress_arg,
6099 got_cancel_cb cancel_cb, void *cancel_arg)
6101 const struct got_error *err;
6102 char *commit_ref_name;
6104 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6108 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6112 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6113 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6114 progress_arg, cancel_cb, cancel_arg);
6116 free(commit_ref_name);
6120 static const struct got_error *
6121 rebase_commit(struct got_object_id **new_commit_id,
6122 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6123 struct got_worktree *worktree, struct got_fileindex *fileindex,
6124 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6125 const char *new_logmsg, struct got_repository *repo)
6127 const struct got_error *err, *sync_err;
6128 struct got_pathlist_head commitable_paths;
6129 struct collect_commitables_arg cc_arg;
6130 char *fileindex_path = NULL;
6131 struct got_reference *head_ref = NULL;
6132 struct got_object_id *head_commit_id = NULL;
6133 char *logmsg = NULL;
6135 TAILQ_INIT(&commitable_paths);
6136 *new_commit_id = NULL;
6138 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6140 err = get_fileindex_path(&fileindex_path, worktree);
6144 cc_arg.commitable_paths = &commitable_paths;
6145 cc_arg.worktree = worktree;
6147 cc_arg.have_staged_files = 0;
6149 * If possible get the status of individual files directly to
6150 * avoid crawling the entire work tree once per rebased commit.
6151 * TODO: Ideally, merged_paths would contain a list of commitables
6152 * we could use so we could skip worktree_status() entirely.
6155 struct got_pathlist_entry *pe;
6156 TAILQ_FOREACH(pe, merged_paths, entry) {
6157 err = worktree_status(worktree, pe->path, fileindex,
6158 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6164 err = worktree_status(worktree, "", fileindex, repo,
6165 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6170 if (TAILQ_EMPTY(&commitable_paths)) {
6171 /* No-op change; commit will be elided. */
6172 err = got_ref_delete(commit_ref, repo);
6175 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6179 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6183 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6188 logmsg = strdup(new_logmsg);
6189 if (logmsg == NULL) {
6190 err = got_error_from_errno("strdup");
6194 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6199 /* NB: commit_worktree will call free(logmsg) */
6200 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6201 worktree, got_object_commit_get_author(orig_commit),
6202 got_object_commit_get_committer(orig_commit),
6203 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6207 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6211 err = got_ref_delete(commit_ref, repo);
6215 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
6217 sync_err = sync_fileindex(fileindex, fileindex_path);
6218 if (sync_err && err == NULL)
6221 free(fileindex_path);
6222 free(head_commit_id);
6224 got_ref_close(head_ref);
6226 free(*new_commit_id);
6227 *new_commit_id = NULL;
6232 const struct got_error *
6233 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6234 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6235 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6236 struct got_commit_object *orig_commit,
6237 struct got_object_id *orig_commit_id, struct got_repository *repo)
6239 const struct got_error *err;
6240 char *commit_ref_name;
6241 struct got_reference *commit_ref = NULL;
6242 struct got_object_id *commit_id = NULL;
6244 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6248 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6251 err = got_ref_resolve(&commit_id, repo, commit_ref);
6254 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6255 err = got_error(GOT_ERR_REBASE_COMMITID);
6259 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6260 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6263 got_ref_close(commit_ref);
6264 free(commit_ref_name);
6269 const struct got_error *
6270 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6271 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6272 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6273 struct got_commit_object *orig_commit,
6274 struct got_object_id *orig_commit_id, const char *new_logmsg,
6275 struct got_repository *repo)
6277 const struct got_error *err;
6278 char *commit_ref_name;
6279 struct got_reference *commit_ref = NULL;
6281 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6285 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6289 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6290 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6293 got_ref_close(commit_ref);
6294 free(commit_ref_name);
6298 const struct got_error *
6299 got_worktree_rebase_postpone(struct got_worktree *worktree,
6300 struct got_fileindex *fileindex)
6303 got_fileindex_free(fileindex);
6304 return lock_worktree(worktree, LOCK_SH);
6307 static const struct got_error *
6308 delete_ref(const char *name, struct got_repository *repo)
6310 const struct got_error *err;
6311 struct got_reference *ref;
6313 err = got_ref_open(&ref, repo, name, 0);
6315 if (err->code == GOT_ERR_NOT_REF)
6320 err = got_ref_delete(ref, repo);
6325 static const struct got_error *
6326 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6328 const struct got_error *err;
6329 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6330 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6332 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6335 err = delete_ref(tmp_branch_name, repo);
6339 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6342 err = delete_ref(new_base_branch_ref_name, repo);
6346 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6349 err = delete_ref(branch_ref_name, repo);
6353 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6356 err = delete_ref(commit_ref_name, repo);
6361 free(tmp_branch_name);
6362 free(new_base_branch_ref_name);
6363 free(branch_ref_name);
6364 free(commit_ref_name);
6368 const struct got_error *
6369 got_worktree_rebase_complete(struct got_worktree *worktree,
6370 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6371 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6372 struct got_repository *repo)
6374 const struct got_error *err, *unlockerr;
6375 struct got_object_id *new_head_commit_id = NULL;
6377 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6381 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6385 err = got_ref_write(rebased_branch, repo);
6389 err = got_worktree_set_head_ref(worktree, rebased_branch);
6393 err = delete_rebase_refs(worktree, repo);
6396 got_fileindex_free(fileindex);
6397 free(new_head_commit_id);
6398 unlockerr = lock_worktree(worktree, LOCK_SH);
6399 if (unlockerr && err == NULL)
6404 const struct got_error *
6405 got_worktree_rebase_abort(struct got_worktree *worktree,
6406 struct got_fileindex *fileindex, struct got_repository *repo,
6407 struct got_reference *new_base_branch,
6408 got_worktree_checkout_cb progress_cb, void *progress_arg)
6410 const struct got_error *err, *unlockerr, *sync_err;
6411 struct got_reference *resolved = NULL;
6412 struct got_object_id *commit_id = NULL;
6413 char *fileindex_path = NULL;
6414 struct revert_file_args rfa;
6415 struct got_object_id *tree_id = NULL;
6417 err = lock_worktree(worktree, LOCK_EX);
6421 err = got_ref_open(&resolved, repo,
6422 got_ref_get_symref_target(new_base_branch), 0);
6426 err = got_worktree_set_head_ref(worktree, resolved);
6431 * XXX commits to the base branch could have happened while
6432 * we were busy rebasing; should we store the original commit ID
6433 * when rebase begins and read it back here?
6435 err = got_ref_resolve(&commit_id, repo, resolved);
6439 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6443 err = got_object_id_by_path(&tree_id, repo,
6444 worktree->base_commit_id, worktree->path_prefix);
6448 err = delete_rebase_refs(worktree, repo);
6452 err = get_fileindex_path(&fileindex_path, worktree);
6456 rfa.worktree = worktree;
6457 rfa.fileindex = fileindex;
6458 rfa.progress_cb = progress_cb;
6459 rfa.progress_arg = progress_arg;
6460 rfa.patch_cb = NULL;
6461 rfa.patch_arg = NULL;
6463 err = worktree_status(worktree, "", fileindex, repo,
6464 revert_file, &rfa, NULL, NULL, 0, 0);
6468 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6469 repo, progress_cb, progress_arg, NULL, NULL);
6471 sync_err = sync_fileindex(fileindex, fileindex_path);
6472 if (sync_err && err == NULL)
6475 got_ref_close(resolved);
6479 got_fileindex_free(fileindex);
6480 free(fileindex_path);
6482 unlockerr = lock_worktree(worktree, LOCK_SH);
6483 if (unlockerr && err == NULL)
6488 const struct got_error *
6489 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6490 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6491 struct got_fileindex **fileindex, struct got_worktree *worktree,
6492 struct got_repository *repo)
6494 const struct got_error *err = NULL;
6495 char *tmp_branch_name = NULL;
6496 char *branch_ref_name = NULL;
6497 char *base_commit_ref_name = NULL;
6498 char *fileindex_path = NULL;
6499 struct check_rebase_ok_arg ok_arg;
6500 struct got_reference *wt_branch = NULL;
6501 struct got_reference *base_commit_ref = NULL;
6505 *base_commit_id = NULL;
6508 err = lock_worktree(worktree, LOCK_EX);
6512 err = open_fileindex(fileindex, &fileindex_path, worktree);
6516 ok_arg.worktree = worktree;
6518 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6523 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6527 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6531 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6536 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6541 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6545 err = got_ref_write(*branch_ref, repo);
6549 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6550 worktree->base_commit_id);
6553 err = got_ref_write(base_commit_ref, repo);
6556 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6557 if (*base_commit_id == NULL) {
6558 err = got_error_from_errno("got_object_id_dup");
6562 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6563 worktree->base_commit_id);
6566 err = got_ref_write(*tmp_branch, repo);
6570 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6574 free(fileindex_path);
6575 free(tmp_branch_name);
6576 free(branch_ref_name);
6577 free(base_commit_ref_name);
6579 got_ref_close(wt_branch);
6582 got_ref_close(*branch_ref);
6586 got_ref_close(*tmp_branch);
6589 free(*base_commit_id);
6591 got_fileindex_free(*fileindex);
6594 lock_worktree(worktree, LOCK_SH);
6599 const struct got_error *
6600 got_worktree_histedit_postpone(struct got_worktree *worktree,
6601 struct got_fileindex *fileindex)
6604 got_fileindex_free(fileindex);
6605 return lock_worktree(worktree, LOCK_SH);
6608 const struct got_error *
6609 got_worktree_histedit_in_progress(int *in_progress,
6610 struct got_worktree *worktree)
6612 const struct got_error *err;
6613 char *tmp_branch_name = NULL;
6615 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6619 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6620 free(tmp_branch_name);
6624 const struct got_error *
6625 got_worktree_histedit_continue(struct got_object_id **commit_id,
6626 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6627 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6628 struct got_worktree *worktree, struct got_repository *repo)
6630 const struct got_error *err;
6631 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6632 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6633 struct got_reference *commit_ref = NULL;
6634 struct got_reference *base_commit_ref = NULL;
6635 char *fileindex_path = NULL;
6636 int have_staged_files = 0;
6640 *base_commit_id = NULL;
6643 err = lock_worktree(worktree, LOCK_EX);
6647 err = open_fileindex(fileindex, &fileindex_path, worktree);
6651 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6652 &have_staged_files);
6653 if (err && err->code != GOT_ERR_CANCELLED)
6655 if (have_staged_files) {
6656 err = got_error(GOT_ERR_STAGED_PATHS);
6660 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6664 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6668 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6672 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6677 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6681 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6684 err = got_ref_resolve(commit_id, repo, commit_ref);
6688 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6691 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6695 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6699 free(commit_ref_name);
6700 free(branch_ref_name);
6701 free(fileindex_path);
6703 got_ref_close(commit_ref);
6704 if (base_commit_ref)
6705 got_ref_close(base_commit_ref);
6709 free(*base_commit_id);
6710 *base_commit_id = NULL;
6712 got_ref_close(*tmp_branch);
6716 got_fileindex_free(*fileindex);
6719 lock_worktree(worktree, LOCK_EX);
6724 static const struct got_error *
6725 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6727 const struct got_error *err;
6728 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6729 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6731 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6734 err = delete_ref(tmp_branch_name, repo);
6738 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6742 err = delete_ref(base_commit_ref_name, repo);
6746 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6749 err = delete_ref(branch_ref_name, repo);
6753 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6756 err = delete_ref(commit_ref_name, repo);
6760 free(tmp_branch_name);
6761 free(base_commit_ref_name);
6762 free(branch_ref_name);
6763 free(commit_ref_name);
6767 const struct got_error *
6768 got_worktree_histedit_abort(struct got_worktree *worktree,
6769 struct got_fileindex *fileindex, struct got_repository *repo,
6770 struct got_reference *branch, struct got_object_id *base_commit_id,
6771 got_worktree_checkout_cb progress_cb, void *progress_arg)
6773 const struct got_error *err, *unlockerr, *sync_err;
6774 struct got_reference *resolved = NULL;
6775 char *fileindex_path = NULL;
6776 struct got_object_id *tree_id = NULL;
6777 struct revert_file_args rfa;
6779 err = lock_worktree(worktree, LOCK_EX);
6783 err = got_ref_open(&resolved, repo,
6784 got_ref_get_symref_target(branch), 0);
6788 err = got_worktree_set_head_ref(worktree, resolved);
6792 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6796 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6797 worktree->path_prefix);
6801 err = delete_histedit_refs(worktree, repo);
6805 err = get_fileindex_path(&fileindex_path, worktree);
6809 rfa.worktree = worktree;
6810 rfa.fileindex = fileindex;
6811 rfa.progress_cb = progress_cb;
6812 rfa.progress_arg = progress_arg;
6813 rfa.patch_cb = NULL;
6814 rfa.patch_arg = NULL;
6816 err = worktree_status(worktree, "", fileindex, repo,
6817 revert_file, &rfa, NULL, NULL, 0, 0);
6821 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6822 repo, progress_cb, progress_arg, NULL, NULL);
6824 sync_err = sync_fileindex(fileindex, fileindex_path);
6825 if (sync_err && err == NULL)
6828 got_ref_close(resolved);
6830 free(fileindex_path);
6832 unlockerr = lock_worktree(worktree, LOCK_SH);
6833 if (unlockerr && err == NULL)
6838 const struct got_error *
6839 got_worktree_histedit_complete(struct got_worktree *worktree,
6840 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6841 struct got_reference *edited_branch, struct got_repository *repo)
6843 const struct got_error *err, *unlockerr;
6844 struct got_object_id *new_head_commit_id = NULL;
6845 struct got_reference *resolved = NULL;
6847 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6851 err = got_ref_open(&resolved, repo,
6852 got_ref_get_symref_target(edited_branch), 0);
6856 err = got_ref_change_ref(resolved, new_head_commit_id);
6860 err = got_ref_write(resolved, repo);
6864 err = got_worktree_set_head_ref(worktree, resolved);
6868 err = delete_histedit_refs(worktree, repo);
6871 got_fileindex_free(fileindex);
6872 free(new_head_commit_id);
6873 unlockerr = lock_worktree(worktree, LOCK_SH);
6874 if (unlockerr && err == NULL)
6879 const struct got_error *
6880 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6881 struct got_object_id *commit_id, struct got_repository *repo)
6883 const struct got_error *err;
6884 char *commit_ref_name;
6886 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6890 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6894 err = delete_ref(commit_ref_name, repo);
6896 free(commit_ref_name);
6900 const struct got_error *
6901 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6902 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6903 struct got_worktree *worktree, const char *refname,
6904 struct got_repository *repo)
6906 const struct got_error *err = NULL;
6907 char *fileindex_path = NULL;
6908 struct check_rebase_ok_arg ok_arg;
6912 *base_branch_ref = NULL;
6914 err = lock_worktree(worktree, LOCK_EX);
6918 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6919 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6920 "cannot integrate a branch into itself; "
6921 "update -b or different branch name required");
6925 err = open_fileindex(fileindex, &fileindex_path, worktree);
6929 /* Preconditions are the same as for rebase. */
6930 ok_arg.worktree = worktree;
6932 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6937 err = got_ref_open(branch_ref, repo, refname, 1);
6941 err = got_ref_open(base_branch_ref, repo,
6942 got_worktree_get_head_ref_name(worktree), 1);
6946 got_ref_close(*branch_ref);
6949 if (*base_branch_ref) {
6950 got_ref_close(*base_branch_ref);
6951 *base_branch_ref = NULL;
6954 got_fileindex_free(*fileindex);
6957 lock_worktree(worktree, LOCK_SH);
6962 const struct got_error *
6963 got_worktree_integrate_continue(struct got_worktree *worktree,
6964 struct got_fileindex *fileindex, struct got_repository *repo,
6965 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6966 got_worktree_checkout_cb progress_cb, void *progress_arg,
6967 got_cancel_cb cancel_cb, void *cancel_arg)
6969 const struct got_error *err = NULL, *sync_err, *unlockerr;
6970 char *fileindex_path = NULL;
6971 struct got_object_id *tree_id = NULL, *commit_id = NULL;
6973 err = get_fileindex_path(&fileindex_path, worktree);
6977 err = got_ref_resolve(&commit_id, repo, branch_ref);
6981 err = got_object_id_by_path(&tree_id, repo, commit_id,
6982 worktree->path_prefix);
6986 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6990 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6991 progress_cb, progress_arg, cancel_cb, cancel_arg);
6995 err = got_ref_change_ref(base_branch_ref, commit_id);
6999 err = got_ref_write(base_branch_ref, repo);
7001 sync_err = sync_fileindex(fileindex, fileindex_path);
7002 if (sync_err && err == NULL)
7006 unlockerr = got_ref_unlock(branch_ref);
7007 if (unlockerr && err == NULL)
7009 got_ref_close(branch_ref);
7011 unlockerr = got_ref_unlock(base_branch_ref);
7012 if (unlockerr && err == NULL)
7014 got_ref_close(base_branch_ref);
7016 got_fileindex_free(fileindex);
7017 free(fileindex_path);
7020 unlockerr = lock_worktree(worktree, LOCK_SH);
7021 if (unlockerr && err == NULL)
7026 const struct got_error *
7027 got_worktree_integrate_abort(struct got_worktree *worktree,
7028 struct got_fileindex *fileindex, struct got_repository *repo,
7029 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7031 const struct got_error *err = NULL, *unlockerr = NULL;
7033 got_fileindex_free(fileindex);
7035 err = lock_worktree(worktree, LOCK_SH);
7037 unlockerr = got_ref_unlock(branch_ref);
7038 if (unlockerr && err == NULL)
7040 got_ref_close(branch_ref);
7042 unlockerr = got_ref_unlock(base_branch_ref);
7043 if (unlockerr && err == NULL)
7045 got_ref_close(base_branch_ref);
7050 struct check_stage_ok_arg {
7051 struct got_object_id *head_commit_id;
7052 struct got_worktree *worktree;
7053 struct got_fileindex *fileindex;
7054 struct got_repository *repo;
7058 const struct got_error *
7059 check_stage_ok(void *arg, unsigned char status,
7060 unsigned char staged_status, const char *relpath,
7061 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7062 struct got_object_id *commit_id, int dirfd, const char *de_name)
7064 struct check_stage_ok_arg *a = arg;
7065 const struct got_error *err = NULL;
7066 struct got_fileindex_entry *ie;
7067 struct got_object_id base_commit_id;
7068 struct got_object_id *base_commit_idp = NULL;
7069 char *in_repo_path = NULL, *p;
7071 if (status == GOT_STATUS_UNVERSIONED ||
7072 status == GOT_STATUS_NO_CHANGE)
7074 if (status == GOT_STATUS_NONEXISTENT)
7075 return got_error_set_errno(ENOENT, relpath);
7077 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7079 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7081 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7082 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7084 return got_error_from_errno("asprintf");
7086 if (got_fileindex_entry_has_commit(ie)) {
7087 memcpy(base_commit_id.sha1, ie->commit_sha1,
7088 SHA1_DIGEST_LENGTH);
7089 base_commit_idp = &base_commit_id;
7092 if (status == GOT_STATUS_CONFLICT) {
7093 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7095 } else if (status != GOT_STATUS_ADD &&
7096 status != GOT_STATUS_MODIFY &&
7097 status != GOT_STATUS_DELETE) {
7098 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7102 a->have_changes = 1;
7107 err = check_out_of_date(p, status, staged_status,
7108 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7109 GOT_ERR_STAGE_OUT_OF_DATE);
7115 struct stage_path_arg {
7116 struct got_worktree *worktree;
7117 struct got_fileindex *fileindex;
7118 struct got_repository *repo;
7119 got_worktree_status_cb status_cb;
7121 got_worktree_patch_cb patch_cb;
7123 int staged_something;
7124 int allow_bad_symlinks;
7127 static const struct got_error *
7128 stage_path(void *arg, unsigned char status,
7129 unsigned char staged_status, const char *relpath,
7130 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7131 struct got_object_id *commit_id, int dirfd, const char *de_name)
7133 struct stage_path_arg *a = arg;
7134 const struct got_error *err = NULL;
7135 struct got_fileindex_entry *ie;
7136 char *ondisk_path = NULL, *path_content = NULL;
7138 struct got_object_id *new_staged_blob_id = NULL;
7141 if (status == GOT_STATUS_UNVERSIONED)
7144 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7146 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7148 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7150 return got_error_from_errno("asprintf");
7153 case GOT_STATUS_ADD:
7154 case GOT_STATUS_MODIFY:
7155 /* XXX could sb.st_mode be passed in by our caller? */
7156 if (lstat(ondisk_path, &sb) == -1) {
7157 err = got_error_from_errno2("lstat", ondisk_path);
7161 if (status == GOT_STATUS_ADD) {
7162 int choice = GOT_PATCH_CHOICE_NONE;
7163 err = (*a->patch_cb)(&choice, a->patch_arg,
7164 status, ie->path, NULL, 1, 1);
7167 if (choice != GOT_PATCH_CHOICE_YES)
7170 err = create_patched_content(&path_content, 0,
7171 staged_blob_id ? staged_blob_id : blob_id,
7172 ondisk_path, dirfd, de_name, ie->path,
7173 a->repo, a->patch_cb, a->patch_arg);
7174 if (err || path_content == NULL)
7178 err = got_object_blob_create(&new_staged_blob_id,
7179 path_content ? path_content : ondisk_path, a->repo);
7182 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7183 SHA1_DIGEST_LENGTH);
7184 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7185 stage = GOT_FILEIDX_STAGE_ADD;
7187 stage = GOT_FILEIDX_STAGE_MODIFY;
7188 got_fileindex_entry_stage_set(ie, stage);
7189 if (S_ISLNK(sb.st_mode)) {
7190 int is_bad_symlink = 0;
7191 if (!a->allow_bad_symlinks) {
7192 char target_path[PATH_MAX];
7194 target_len = readlink(ondisk_path, target_path,
7195 sizeof(target_path));
7196 if (target_len == -1) {
7197 err = got_error_from_errno2("readlink",
7201 err = is_bad_symlink_target(&is_bad_symlink,
7202 target_path, target_len, ondisk_path,
7203 a->worktree->root_path);
7206 if (is_bad_symlink) {
7207 err = got_error_path(ondisk_path,
7208 GOT_ERR_BAD_SYMLINK);
7213 got_fileindex_entry_staged_filetype_set(ie,
7214 GOT_FILEIDX_MODE_BAD_SYMLINK);
7216 got_fileindex_entry_staged_filetype_set(ie,
7217 GOT_FILEIDX_MODE_SYMLINK);
7219 got_fileindex_entry_staged_filetype_set(ie,
7220 GOT_FILEIDX_MODE_REGULAR_FILE);
7222 a->staged_something = 1;
7223 if (a->status_cb == NULL)
7225 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7226 get_staged_status(ie), relpath, blob_id,
7227 new_staged_blob_id, NULL, dirfd, de_name);
7229 case GOT_STATUS_DELETE:
7230 if (staged_status == GOT_STATUS_DELETE)
7233 int choice = GOT_PATCH_CHOICE_NONE;
7234 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7235 ie->path, NULL, 1, 1);
7238 if (choice == GOT_PATCH_CHOICE_NO)
7240 if (choice != GOT_PATCH_CHOICE_YES) {
7241 err = got_error(GOT_ERR_PATCH_CHOICE);
7245 stage = GOT_FILEIDX_STAGE_DELETE;
7246 got_fileindex_entry_stage_set(ie, stage);
7247 a->staged_something = 1;
7248 if (a->status_cb == NULL)
7250 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7251 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7254 case GOT_STATUS_NO_CHANGE:
7256 case GOT_STATUS_CONFLICT:
7257 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7259 case GOT_STATUS_NONEXISTENT:
7260 err = got_error_set_errno(ENOENT, relpath);
7263 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7267 if (path_content && unlink(path_content) == -1 && err == NULL)
7268 err = got_error_from_errno2("unlink", path_content);
7271 free(new_staged_blob_id);
7275 const struct got_error *
7276 got_worktree_stage(struct got_worktree *worktree,
7277 struct got_pathlist_head *paths,
7278 got_worktree_status_cb status_cb, void *status_arg,
7279 got_worktree_patch_cb patch_cb, void *patch_arg,
7280 int allow_bad_symlinks, struct got_repository *repo)
7282 const struct got_error *err = NULL, *sync_err, *unlockerr;
7283 struct got_pathlist_entry *pe;
7284 struct got_fileindex *fileindex = NULL;
7285 char *fileindex_path = NULL;
7286 struct got_reference *head_ref = NULL;
7287 struct got_object_id *head_commit_id = NULL;
7288 struct check_stage_ok_arg oka;
7289 struct stage_path_arg spa;
7291 err = lock_worktree(worktree, LOCK_EX);
7295 err = got_ref_open(&head_ref, repo,
7296 got_worktree_get_head_ref_name(worktree), 0);
7299 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7302 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7306 /* Check pre-conditions before staging anything. */
7307 oka.head_commit_id = head_commit_id;
7308 oka.worktree = worktree;
7309 oka.fileindex = fileindex;
7311 oka.have_changes = 0;
7312 TAILQ_FOREACH(pe, paths, entry) {
7313 err = worktree_status(worktree, pe->path, fileindex, repo,
7314 check_stage_ok, &oka, NULL, NULL, 0, 0);
7318 if (!oka.have_changes) {
7319 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7323 spa.worktree = worktree;
7324 spa.fileindex = fileindex;
7326 spa.patch_cb = patch_cb;
7327 spa.patch_arg = patch_arg;
7328 spa.status_cb = status_cb;
7329 spa.status_arg = status_arg;
7330 spa.staged_something = 0;
7331 spa.allow_bad_symlinks = allow_bad_symlinks;
7332 TAILQ_FOREACH(pe, paths, entry) {
7333 err = worktree_status(worktree, pe->path, fileindex, repo,
7334 stage_path, &spa, NULL, NULL, 0, 0);
7338 if (!spa.staged_something) {
7339 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7343 sync_err = sync_fileindex(fileindex, fileindex_path);
7344 if (sync_err && err == NULL)
7348 got_ref_close(head_ref);
7349 free(head_commit_id);
7350 free(fileindex_path);
7352 got_fileindex_free(fileindex);
7353 unlockerr = lock_worktree(worktree, LOCK_SH);
7354 if (unlockerr && err == NULL)
7359 struct unstage_path_arg {
7360 struct got_worktree *worktree;
7361 struct got_fileindex *fileindex;
7362 struct got_repository *repo;
7363 got_worktree_checkout_cb progress_cb;
7365 got_worktree_patch_cb patch_cb;
7369 static const struct got_error *
7370 create_unstaged_content(char **path_unstaged_content,
7371 char **path_new_staged_content, struct got_object_id *blob_id,
7372 struct got_object_id *staged_blob_id, const char *relpath,
7373 struct got_repository *repo,
7374 got_worktree_patch_cb patch_cb, void *patch_arg)
7376 const struct got_error *err;
7377 struct got_blob_object *blob = NULL, *staged_blob = NULL;
7378 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7379 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7380 struct stat sb1, sb2;
7381 struct got_diff_changes *changes = NULL;
7382 struct got_diff_state *ds = NULL;
7383 struct got_diff_args *args = NULL;
7384 struct got_diff_change *change;
7385 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
7386 int have_content = 0, have_rejected_content = 0;
7388 *path_unstaged_content = NULL;
7389 *path_new_staged_content = NULL;
7391 err = got_object_id_str(&label1, blob_id);
7394 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7398 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7402 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7406 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7410 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7414 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7418 if (stat(path1, &sb1) == -1) {
7419 err = got_error_from_errno2("stat", path1);
7423 if (stat(path2, &sb2) == -1) {
7424 err = got_error_from_errno2("stat", path2);
7428 err = got_diff_files(&changes, &ds, &args, &diff_flags,
7429 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
7433 err = got_opentemp_named(path_unstaged_content, &outfile,
7434 "got-unstaged-content");
7437 err = got_opentemp_named(path_new_staged_content, &rejectfile,
7438 "got-new-staged-content");
7442 if (fseek(f1, 0L, SEEK_SET) == -1) {
7443 err = got_ferror(f1, GOT_ERR_IO);
7446 if (fseek(f2, 0L, SEEK_SET) == -1) {
7447 err = got_ferror(f2, GOT_ERR_IO);
7450 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
7452 err = apply_or_reject_change(&choice, change, ++n,
7453 changes->nchanges, ds, args, diff_flags, relpath,
7454 f1, f2, &line_cur1, &line_cur2,
7455 outfile, rejectfile, patch_cb, patch_arg);
7458 if (choice == GOT_PATCH_CHOICE_YES)
7461 have_rejected_content = 1;
7462 if (choice == GOT_PATCH_CHOICE_QUIT)
7465 if (have_content || have_rejected_content)
7466 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7467 outfile, rejectfile);
7471 got_object_blob_close(blob);
7473 got_object_blob_close(staged_blob);
7474 if (f1 && fclose(f1) == EOF && err == NULL)
7475 err = got_error_from_errno2("fclose", path1);
7476 if (f2 && fclose(f2) == EOF && err == NULL)
7477 err = got_error_from_errno2("fclose", path2);
7478 if (outfile && fclose(outfile) == EOF && err == NULL)
7479 err = got_error_from_errno2("fclose", *path_unstaged_content);
7480 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7481 err = got_error_from_errno2("fclose", *path_new_staged_content);
7482 if (path1 && unlink(path1) == -1 && err == NULL)
7483 err = got_error_from_errno2("unlink", path1);
7484 if (path2 && unlink(path2) == -1 && err == NULL)
7485 err = got_error_from_errno2("unlink", path2);
7486 if (err || !have_content) {
7487 if (*path_unstaged_content &&
7488 unlink(*path_unstaged_content) == -1 && err == NULL)
7489 err = got_error_from_errno2("unlink",
7490 *path_unstaged_content);
7491 free(*path_unstaged_content);
7492 *path_unstaged_content = NULL;
7494 if (err || !have_content || !have_rejected_content) {
7495 if (*path_new_staged_content &&
7496 unlink(*path_new_staged_content) == -1 && err == NULL)
7497 err = got_error_from_errno2("unlink",
7498 *path_new_staged_content);
7499 free(*path_new_staged_content);
7500 *path_new_staged_content = NULL;
7504 got_diff_state_free(ds);
7508 got_diff_free_changes(changes);
7514 static const struct got_error *
7515 unstage_hunks(struct got_object_id *staged_blob_id,
7516 struct got_blob_object *blob_base,
7517 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
7518 const char *ondisk_path, const char *label_orig,
7519 struct got_worktree *worktree, struct got_repository *repo,
7520 got_worktree_patch_cb patch_cb, void *patch_arg,
7521 got_worktree_checkout_cb progress_cb, void *progress_arg)
7523 const struct got_error *err = NULL;
7524 char *path_unstaged_content = NULL;
7525 char *path_new_staged_content = NULL;
7526 struct got_object_id *new_staged_blob_id = NULL;
7530 err = create_unstaged_content(&path_unstaged_content,
7531 &path_new_staged_content, blob_id, staged_blob_id,
7532 ie->path, repo, patch_cb, patch_arg);
7536 if (path_unstaged_content == NULL)
7539 if (path_new_staged_content) {
7540 err = got_object_blob_create(&new_staged_blob_id,
7541 path_new_staged_content, repo);
7546 f = fopen(path_unstaged_content, "r");
7548 err = got_error_from_errno2("fopen",
7549 path_unstaged_content);
7552 if (fstat(fileno(f), &sb) == -1) {
7553 err = got_error_from_errno2("fstat", path_unstaged_content);
7556 if (got_fileindex_entry_staged_filetype_get(ie) ==
7557 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
7558 char link_target[PATH_MAX];
7560 r = fread(link_target, 1, sizeof(link_target), f);
7561 if (r == 0 && ferror(f)) {
7562 err = got_error_from_errno("fread");
7565 if (r >= sizeof(link_target)) { /* should not happen */
7566 err = got_error(GOT_ERR_NO_SPACE);
7569 link_target[r] = '\0';
7570 err = merge_symlink(worktree, blob_base,
7571 ondisk_path, ie->path, label_orig, link_target,
7572 worktree->base_commit_id, repo, progress_cb,
7575 int local_changes_subsumed;
7576 err = merge_file(&local_changes_subsumed, worktree,
7577 blob_base, ondisk_path, ie->path,
7578 got_fileindex_perms_to_st(ie),
7579 path_unstaged_content, label_orig, "unstaged",
7580 repo, progress_cb, progress_arg);
7585 if (new_staged_blob_id) {
7586 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7587 SHA1_DIGEST_LENGTH);
7589 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7591 free(new_staged_blob_id);
7592 if (path_unstaged_content &&
7593 unlink(path_unstaged_content) == -1 && err == NULL)
7594 err = got_error_from_errno2("unlink", path_unstaged_content);
7595 if (path_new_staged_content &&
7596 unlink(path_new_staged_content) == -1 && err == NULL)
7597 err = got_error_from_errno2("unlink", path_new_staged_content);
7598 if (f && fclose(f) != 0 && err == NULL)
7599 err = got_error_from_errno2("fclose", path_unstaged_content);
7600 free(path_unstaged_content);
7601 free(path_new_staged_content);
7605 static const struct got_error *
7606 unstage_path(void *arg, unsigned char status,
7607 unsigned char staged_status, const char *relpath,
7608 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7609 struct got_object_id *commit_id, int dirfd, const char *de_name)
7611 const struct got_error *err = NULL;
7612 struct unstage_path_arg *a = arg;
7613 struct got_fileindex_entry *ie;
7614 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7615 char *ondisk_path = NULL;
7616 char *id_str = NULL, *label_orig = NULL;
7617 int local_changes_subsumed;
7620 if (staged_status != GOT_STATUS_ADD &&
7621 staged_status != GOT_STATUS_MODIFY &&
7622 staged_status != GOT_STATUS_DELETE)
7625 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7627 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7629 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7631 return got_error_from_errno("asprintf");
7633 err = got_object_id_str(&id_str,
7634 commit_id ? commit_id : a->worktree->base_commit_id);
7637 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7639 err = got_error_from_errno("asprintf");
7643 switch (staged_status) {
7644 case GOT_STATUS_MODIFY:
7645 err = got_object_open_as_blob(&blob_base, a->repo,
7650 case GOT_STATUS_ADD:
7652 if (staged_status == GOT_STATUS_ADD) {
7653 int choice = GOT_PATCH_CHOICE_NONE;
7654 err = (*a->patch_cb)(&choice, a->patch_arg,
7655 staged_status, ie->path, NULL, 1, 1);
7658 if (choice != GOT_PATCH_CHOICE_YES)
7661 err = unstage_hunks(staged_blob_id,
7662 blob_base, blob_id, ie, ondisk_path,
7663 label_orig, a->worktree, a->repo,
7664 a->patch_cb, a->patch_arg,
7665 a->progress_cb, a->progress_arg);
7666 break; /* Done with this file. */
7669 err = got_object_open_as_blob(&blob_staged, a->repo,
7670 staged_blob_id, 8192);
7673 switch (got_fileindex_entry_staged_filetype_get(ie)) {
7674 case GOT_FILEIDX_MODE_BAD_SYMLINK:
7675 case GOT_FILEIDX_MODE_REGULAR_FILE:
7676 err = merge_blob(&local_changes_subsumed, a->worktree,
7677 blob_base, ondisk_path, relpath,
7678 got_fileindex_perms_to_st(ie), label_orig,
7679 blob_staged, commit_id ? commit_id :
7680 a->worktree->base_commit_id, a->repo,
7681 a->progress_cb, a->progress_arg);
7683 case GOT_FILEIDX_MODE_SYMLINK:
7684 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7685 char *staged_target;
7686 err = got_object_blob_read_to_str(
7687 &staged_target, blob_staged);
7690 err = merge_symlink(a->worktree, blob_base,
7691 ondisk_path, relpath, label_orig,
7692 staged_target, commit_id ? commit_id :
7693 a->worktree->base_commit_id,
7694 a->repo, a->progress_cb, a->progress_arg);
7695 free(staged_target);
7697 err = merge_blob(&local_changes_subsumed,
7698 a->worktree, blob_base, ondisk_path,
7699 relpath, got_fileindex_perms_to_st(ie),
7700 label_orig, blob_staged,
7701 commit_id ? commit_id :
7702 a->worktree->base_commit_id, a->repo,
7703 a->progress_cb, a->progress_arg);
7707 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
7711 got_fileindex_entry_stage_set(ie,
7712 GOT_FILEIDX_STAGE_NONE);
7714 case GOT_STATUS_DELETE:
7716 int choice = GOT_PATCH_CHOICE_NONE;
7717 err = (*a->patch_cb)(&choice, a->patch_arg,
7718 staged_status, ie->path, NULL, 1, 1);
7721 if (choice == GOT_PATCH_CHOICE_NO)
7723 if (choice != GOT_PATCH_CHOICE_YES) {
7724 err = got_error(GOT_ERR_PATCH_CHOICE);
7728 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7729 err = get_file_status(&status, &sb, ie, ondisk_path,
7730 dirfd, de_name, a->repo);
7733 err = (*a->progress_cb)(a->progress_arg, status, relpath);
7739 got_object_blob_close(blob_base);
7741 got_object_blob_close(blob_staged);
7747 const struct got_error *
7748 got_worktree_unstage(struct got_worktree *worktree,
7749 struct got_pathlist_head *paths,
7750 got_worktree_checkout_cb progress_cb, void *progress_arg,
7751 got_worktree_patch_cb patch_cb, void *patch_arg,
7752 struct got_repository *repo)
7754 const struct got_error *err = NULL, *sync_err, *unlockerr;
7755 struct got_pathlist_entry *pe;
7756 struct got_fileindex *fileindex = NULL;
7757 char *fileindex_path = NULL;
7758 struct unstage_path_arg upa;
7760 err = lock_worktree(worktree, LOCK_EX);
7764 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7768 upa.worktree = worktree;
7769 upa.fileindex = fileindex;
7771 upa.progress_cb = progress_cb;
7772 upa.progress_arg = progress_arg;
7773 upa.patch_cb = patch_cb;
7774 upa.patch_arg = patch_arg;
7775 TAILQ_FOREACH(pe, paths, entry) {
7776 err = worktree_status(worktree, pe->path, fileindex, repo,
7777 unstage_path, &upa, NULL, NULL, 0, 0);
7782 sync_err = sync_fileindex(fileindex, fileindex_path);
7783 if (sync_err && err == NULL)
7786 free(fileindex_path);
7788 got_fileindex_free(fileindex);
7789 unlockerr = lock_worktree(worktree, LOCK_SH);
7790 if (unlockerr && err == NULL)
7795 struct report_file_info_arg {
7796 struct got_worktree *worktree;
7797 got_worktree_path_info_cb info_cb;
7799 struct got_pathlist_head *paths;
7800 got_cancel_cb cancel_cb;
7804 static const struct got_error *
7805 report_file_info(void *arg, struct got_fileindex_entry *ie)
7807 struct report_file_info_arg *a = arg;
7808 struct got_pathlist_entry *pe;
7809 struct got_object_id blob_id, staged_blob_id, commit_id;
7810 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
7811 struct got_object_id *commit_idp = NULL;
7814 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
7815 return got_error(GOT_ERR_CANCELLED);
7817 TAILQ_FOREACH(pe, a->paths, entry) {
7818 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
7819 got_path_is_child(ie->path, pe->path, pe->path_len))
7822 if (pe == NULL) /* not found */
7825 if (got_fileindex_entry_has_blob(ie)) {
7826 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
7827 blob_idp = &blob_id;
7829 stage = got_fileindex_entry_stage_get(ie);
7830 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
7831 stage == GOT_FILEIDX_STAGE_ADD) {
7832 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
7833 SHA1_DIGEST_LENGTH);
7834 staged_blob_idp = &staged_blob_id;
7837 if (got_fileindex_entry_has_commit(ie)) {
7838 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
7839 commit_idp = &commit_id;
7842 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
7843 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
7846 const struct got_error *
7847 got_worktree_path_info(struct got_worktree *worktree,
7848 struct got_pathlist_head *paths,
7849 got_worktree_path_info_cb info_cb, void *info_arg,
7850 got_cancel_cb cancel_cb, void *cancel_arg)
7853 const struct got_error *err = NULL, *unlockerr;
7854 struct got_fileindex *fileindex = NULL;
7855 char *fileindex_path = NULL;
7856 struct report_file_info_arg arg;
7858 err = lock_worktree(worktree, LOCK_SH);
7862 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7866 arg.worktree = worktree;
7867 arg.info_cb = info_cb;
7868 arg.info_arg = info_arg;
7870 arg.cancel_cb = cancel_cb;
7871 arg.cancel_arg = cancel_arg;
7872 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
7875 free(fileindex_path);
7877 got_fileindex_free(fileindex);
7878 unlockerr = lock_worktree(worktree, LOCK_UN);
7879 if (unlockerr && err == NULL)