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 (fchmod(fileno(f), GOT_DEFAULT_FILE_MODE) == -1) {
972 err = got_error_from_errno2("fchmod", path);
976 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
977 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
978 deriv_target ? deriv_target : "(symlink was deleted)",
979 orig_target ? label_orig : "",
980 orig_target ? "\n" : "",
981 orig_target ? orig_target : "",
982 orig_target ? "\n" : "",
983 GOT_DIFF_CONFLICT_MARKER_SEP,
984 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
985 err = got_error_from_errno2("fprintf", path);
989 if (unlink(ondisk_path) == -1) {
990 err = got_error_from_errno2("unlink", ondisk_path);
993 if (rename(path, ondisk_path) == -1) {
994 err = got_error_from_errno3("rename", path, ondisk_path);
998 if (f != NULL && fclose(f) == EOF && err == NULL)
999 err = got_error_from_errno2("fclose", path);
1006 /* forward declaration */
1007 static const struct got_error *
1008 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
1009 const char *, const char *, uint16_t, const char *,
1010 struct got_blob_object *, struct got_object_id *,
1011 struct got_repository *, got_worktree_checkout_cb, void *);
1014 * Merge a symlink into the work tree, where blob_orig acts as the common
1015 * ancestor, deriv_target is the link target of the first derived version,
1016 * and the symlink on disk acts as the second derived version.
1017 * Assume that contents of both blobs represent symlinks.
1019 static const struct got_error *
1020 merge_symlink(struct got_worktree *worktree,
1021 struct got_blob_object *blob_orig, const char *ondisk_path,
1022 const char *path, const char *label_orig, const char *deriv_target,
1023 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1024 got_worktree_checkout_cb progress_cb, void *progress_arg)
1026 const struct got_error *err = NULL;
1027 char *ancestor_target = NULL;
1029 ssize_t ondisk_len, deriv_len;
1030 char ondisk_target[PATH_MAX];
1031 int have_local_change = 0;
1032 int have_incoming_change = 0;
1034 if (lstat(ondisk_path, &sb) == -1)
1035 return got_error_from_errno2("lstat", ondisk_path);
1037 ondisk_len = readlink(ondisk_path, ondisk_target,
1038 sizeof(ondisk_target));
1039 if (ondisk_len == -1) {
1040 err = got_error_from_errno2("readlink",
1044 ondisk_target[ondisk_len] = '\0';
1047 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
1052 if (ancestor_target == NULL ||
1053 (ondisk_len != strlen(ancestor_target) ||
1054 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
1055 have_local_change = 1;
1057 deriv_len = strlen(deriv_target);
1058 if (ancestor_target == NULL ||
1059 (deriv_len != strlen(ancestor_target) ||
1060 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
1061 have_incoming_change = 1;
1063 if (!have_local_change && !have_incoming_change) {
1064 if (ancestor_target) {
1065 /* Both sides made the same change. */
1066 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1068 } else if (deriv_len == ondisk_len &&
1069 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1070 /* Both sides added the same symlink. */
1071 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1074 /* Both sides added symlinks which don't match. */
1075 err = install_symlink_conflict(deriv_target,
1076 deriv_base_commit_id, ancestor_target,
1077 label_orig, ondisk_target, ondisk_path);
1080 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1083 } else if (!have_local_change && have_incoming_change) {
1084 /* Apply the incoming change. */
1085 err = update_symlink(ondisk_path, deriv_target,
1086 strlen(deriv_target));
1089 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1090 } else if (have_local_change && have_incoming_change) {
1091 if (deriv_len == ondisk_len &&
1092 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
1093 /* Both sides made the same change. */
1094 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1097 err = install_symlink_conflict(deriv_target,
1098 deriv_base_commit_id, ancestor_target, label_orig,
1099 ondisk_target, ondisk_path);
1102 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1108 free(ancestor_target);
1113 * Perform a 3-way merge where blob_orig acts as the common ancestor,
1114 * blob_deriv acts as the first derived version, and the file on disk
1115 * acts as the second derived version.
1117 static const struct got_error *
1118 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1119 struct got_blob_object *blob_orig, const char *ondisk_path,
1120 const char *path, uint16_t st_mode, const char *label_orig,
1121 struct got_blob_object *blob_deriv,
1122 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1123 got_worktree_checkout_cb progress_cb, void *progress_arg)
1125 const struct got_error *err = NULL;
1126 FILE *f_deriv = NULL;
1127 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1128 char *label_deriv = NULL, *parent = NULL;
1130 *local_changes_subsumed = 0;
1132 err = got_path_dirname(&parent, ondisk_path);
1137 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1138 err = got_error_from_errno("asprintf");
1143 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1146 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1151 err = got_object_id_str(&id_str, deriv_base_commit_id);
1154 if (asprintf(&label_deriv, "%s: commit %s",
1155 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1156 err = got_error_from_errno("asprintf");
1160 err = merge_file(local_changes_subsumed, worktree, blob_orig,
1161 ondisk_path, path, st_mode, blob_deriv_path, label_orig,
1162 label_deriv, repo, progress_cb, progress_arg);
1164 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
1165 err = got_error_from_errno("fclose");
1167 if (blob_deriv_path) {
1168 unlink(blob_deriv_path);
1169 free(blob_deriv_path);
1177 static const struct got_error *
1178 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1179 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1180 const char *ondisk_path, const char *path, struct got_object_id *blob_id)
1182 const struct got_error *err = NULL;
1183 struct got_fileindex_entry *new_ie;
1187 err = got_fileindex_entry_alloc(&new_ie, path);
1191 err = got_fileindex_entry_update(new_ie, ondisk_path,
1192 blob_id->sha1, base_commit_id->sha1, 1);
1196 err = got_fileindex_entry_add(fileindex, new_ie);
1199 got_fileindex_entry_free(new_ie);
1206 get_ondisk_perms(int executable, mode_t st_mode)
1208 mode_t xbits = S_IXUSR;
1211 /* Map read bits to execute bits. */
1212 if (st_mode & S_IRGRP)
1214 if (st_mode & S_IROTH)
1216 return st_mode | xbits;
1219 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1222 /* forward declaration */
1223 static const struct got_error *
1224 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1225 const char *path, mode_t te_mode, mode_t st_mode,
1226 struct got_blob_object *blob, int restoring_missing_file,
1227 int reverting_versioned_file, int installing_bad_symlink,
1228 int path_is_unversioned, struct got_repository *repo,
1229 got_worktree_checkout_cb progress_cb, void *progress_arg);
1232 * This function assumes that the provided symlink target points at a
1233 * safe location in the work tree!
1235 static const struct got_error *
1236 replace_existing_symlink(const char *ondisk_path, const char *target_path,
1239 const struct got_error *err = NULL;
1241 char etarget[PATH_MAX];
1245 * "Bad" symlinks (those pointing outside the work tree or into the
1246 * .got directory) are installed in the work tree as a regular file
1247 * which contains the bad symlink target path.
1248 * The new symlink target has already been checked for safety by our
1249 * caller. If we can successfully open a regular file then we simply
1250 * replace this file with a symlink below.
1252 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1255 return got_error_from_errno2("open", ondisk_path);
1257 /* We are updating an existing on-disk symlink. */
1258 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1260 return got_error_from_errno2("readlink", ondisk_path);
1262 if (elen == target_len &&
1263 memcmp(etarget, target_path, target_len) == 0)
1264 return NULL; /* nothing to do */
1267 err = update_symlink(ondisk_path, target_path, target_len);
1268 if (fd != -1 && close(fd) == -1 && err == NULL)
1269 err = got_error_from_errno2("close", ondisk_path);
1273 static const struct got_error *
1274 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1275 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1277 const struct got_error *err = NULL;
1278 char canonpath[PATH_MAX];
1279 char *path_got = NULL;
1281 *is_bad_symlink = 0;
1283 if (target_len >= sizeof(canonpath)) {
1284 *is_bad_symlink = 1;
1289 * We do not use realpath(3) to resolve the symlink's target
1290 * path because we don't want to resolve symlinks recursively.
1291 * Instead we make the path absolute and then canonicalize it.
1292 * Relative symlink target lookup should begin at the directory
1293 * in which the blob object is being installed.
1295 if (!got_path_is_absolute(target_path)) {
1296 char *abspath, *parent;
1297 err = got_path_dirname(&parent, ondisk_path);
1300 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1302 return got_error_from_errno("asprintf");
1305 if (strlen(abspath) >= sizeof(canonpath)) {
1306 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1310 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1315 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1320 /* Only allow symlinks pointing at paths within the work tree. */
1321 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1322 *is_bad_symlink = 1;
1326 /* Do not allow symlinks pointing into the .got directory. */
1327 if (asprintf(&path_got, "%s/%s", wtroot_path,
1328 GOT_WORKTREE_GOT_DIR) == -1)
1329 return got_error_from_errno("asprintf");
1330 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1331 *is_bad_symlink = 1;
1337 static const struct got_error *
1338 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1339 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1340 int restoring_missing_file, int reverting_versioned_file,
1341 int path_is_unversioned, struct got_repository *repo,
1342 got_worktree_checkout_cb progress_cb, void *progress_arg)
1344 const struct got_error *err = NULL;
1345 char target_path[PATH_MAX];
1346 size_t len, target_len = 0;
1347 char *path_got = NULL;
1348 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1349 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1351 *is_bad_symlink = 0;
1354 * Blob object content specifies the target path of the link.
1355 * If a symbolic link cannot be installed we instead create
1356 * a regular file which contains the link target path stored
1357 * in the blob object.
1360 err = got_object_blob_read_block(&len, blob);
1361 if (len + target_len >= sizeof(target_path)) {
1362 /* Path too long; install as a regular file. */
1363 *is_bad_symlink = 1;
1364 got_object_blob_rewind(blob);
1365 return install_blob(worktree, ondisk_path, path,
1366 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1367 restoring_missing_file, reverting_versioned_file,
1368 1, path_is_unversioned, repo, progress_cb,
1372 /* Skip blob object header first time around. */
1373 memcpy(target_path + target_len, buf + hdrlen,
1375 target_len += len - hdrlen;
1379 target_path[target_len] = '\0';
1381 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1382 ondisk_path, worktree->root_path);
1386 if (*is_bad_symlink) {
1387 /* install as a regular file */
1388 *is_bad_symlink = 1;
1389 got_object_blob_rewind(blob);
1390 err = install_blob(worktree, ondisk_path, path,
1391 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1392 restoring_missing_file, reverting_versioned_file, 1,
1393 path_is_unversioned, repo, progress_cb, progress_arg);
1397 if (symlink(target_path, ondisk_path) == -1) {
1398 if (errno == EEXIST) {
1399 if (path_is_unversioned) {
1400 err = (*progress_cb)(progress_arg,
1401 GOT_STATUS_UNVERSIONED, path);
1404 err = replace_existing_symlink(ondisk_path,
1405 target_path, target_len);
1409 err = (*progress_cb)(progress_arg,
1410 reverting_versioned_file ?
1411 GOT_STATUS_REVERT : GOT_STATUS_UPDATE,
1414 goto done; /* Nothing else to do. */
1417 if (errno == ENOENT) {
1419 err = got_path_dirname(&parent, ondisk_path);
1422 err = add_dir_on_disk(worktree, parent);
1427 * Retry, and fall through to error handling
1428 * below if this second attempt fails.
1430 if (symlink(target_path, ondisk_path) != -1) {
1431 err = NULL; /* success */
1436 /* Handle errors from first or second creation attempt. */
1437 if (errno == ENAMETOOLONG) {
1438 /* bad target path; install as a regular file */
1439 *is_bad_symlink = 1;
1440 got_object_blob_rewind(blob);
1441 err = install_blob(worktree, ondisk_path, path,
1442 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1443 restoring_missing_file, reverting_versioned_file, 1,
1444 path_is_unversioned, repo,
1445 progress_cb, progress_arg);
1446 } else if (errno == ENOTDIR) {
1447 err = got_error_path(ondisk_path,
1448 GOT_ERR_FILE_OBSTRUCTED);
1450 err = got_error_from_errno3("symlink",
1451 target_path, ondisk_path);
1453 } else if (progress_cb)
1454 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1455 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1461 static const struct got_error *
1462 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1463 const char *path, mode_t te_mode, mode_t st_mode,
1464 struct got_blob_object *blob, int restoring_missing_file,
1465 int reverting_versioned_file, int installing_bad_symlink,
1466 int path_is_unversioned, struct got_repository *repo,
1467 got_worktree_checkout_cb progress_cb, void *progress_arg)
1469 const struct got_error *err = NULL;
1473 char *tmppath = NULL;
1475 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1476 GOT_DEFAULT_FILE_MODE);
1478 if (errno == ENOENT) {
1480 err = got_path_dirname(&parent, path);
1483 err = add_dir_on_disk(worktree, parent);
1487 fd = open(ondisk_path,
1488 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1489 GOT_DEFAULT_FILE_MODE);
1491 return got_error_from_errno2("open",
1493 } else if (errno == EEXIST) {
1494 if (path_is_unversioned) {
1495 err = (*progress_cb)(progress_arg,
1496 GOT_STATUS_UNVERSIONED, path);
1499 if (!S_ISREG(st_mode) && !installing_bad_symlink) {
1500 /* TODO file is obstructed; do something */
1501 err = got_error_path(ondisk_path,
1502 GOT_ERR_FILE_OBSTRUCTED);
1505 err = got_opentemp_named_fd(&tmppath, &fd,
1512 return got_error_from_errno2("open", ondisk_path);
1515 if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1516 err = got_error_from_errno2("fchmod",
1517 update ? tmppath : ondisk_path);
1522 if (restoring_missing_file)
1523 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1525 else if (reverting_versioned_file)
1526 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1529 err = (*progress_cb)(progress_arg,
1530 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1535 hdrlen = got_object_blob_get_hdrlen(blob);
1537 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1538 err = got_object_blob_read_block(&len, blob);
1542 /* Skip blob object header first time around. */
1543 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1545 err = got_error_from_errno("write");
1547 } else if (outlen != len - hdrlen) {
1548 err = got_error(GOT_ERR_IO);
1555 if (fsync(fd) != 0) {
1556 err = got_error_from_errno("fsync");
1561 if (rename(tmppath, ondisk_path) != 0) {
1562 err = got_error_from_errno3("rename", tmppath,
1570 if (fd != -1 && close(fd) != 0 && err == NULL)
1571 err = got_error_from_errno("close");
1576 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1577 static const struct got_error *
1578 get_modified_file_content_status(unsigned char *status, FILE *f)
1580 const struct got_error *err = NULL;
1581 const char *markers[3] = {
1582 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1583 GOT_DIFF_CONFLICT_MARKER_SEP,
1584 GOT_DIFF_CONFLICT_MARKER_END
1589 const char delim[3] = {'\0', '\0', '\0'};
1591 while (*status == GOT_STATUS_MODIFY) {
1592 line = fparseln(f, &len, NULL, delim, 0);
1596 err = got_ferror(f, GOT_ERR_IO);
1600 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1601 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1603 *status = GOT_STATUS_CONFLICT;
1613 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1615 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1616 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1620 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1622 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1623 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1624 ie->mtime_sec == sb->st_mtim.tv_sec &&
1625 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1626 ie->size == (sb->st_size & 0xffffffff) &&
1627 !xbit_differs(ie, sb->st_mode));
1630 static unsigned char
1631 get_staged_status(struct got_fileindex_entry *ie)
1633 switch (got_fileindex_entry_stage_get(ie)) {
1634 case GOT_FILEIDX_STAGE_ADD:
1635 return GOT_STATUS_ADD;
1636 case GOT_FILEIDX_STAGE_DELETE:
1637 return GOT_STATUS_DELETE;
1638 case GOT_FILEIDX_STAGE_MODIFY:
1639 return GOT_STATUS_MODIFY;
1641 return GOT_STATUS_NO_CHANGE;
1645 static const struct got_error *
1646 get_symlink_modification_status(unsigned char *status,
1647 struct got_fileindex_entry *ie, const char *abspath,
1648 int dirfd, const char *de_name, struct got_blob_object *blob)
1650 const struct got_error *err = NULL;
1651 char target_path[PATH_MAX];
1652 char etarget[PATH_MAX];
1654 size_t len, target_len = 0;
1655 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1656 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1658 *status = GOT_STATUS_NO_CHANGE;
1660 /* Blob object content specifies the target path of the link. */
1662 err = got_object_blob_read_block(&len, blob);
1665 if (len + target_len >= sizeof(target_path)) {
1667 * Should not happen. The blob contents were OK
1668 * when this symlink was installed.
1670 return got_error(GOT_ERR_NO_SPACE);
1673 /* Skip blob object header first time around. */
1674 memcpy(target_path + target_len, buf + hdrlen,
1676 target_len += len - hdrlen;
1680 target_path[target_len] = '\0';
1683 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1685 return got_error_from_errno2("readlinkat", abspath);
1687 elen = readlink(abspath, etarget, sizeof(etarget));
1689 return got_error_from_errno2("readlink", abspath);
1692 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1693 *status = GOT_STATUS_MODIFY;
1698 static const struct got_error *
1699 get_file_status(unsigned char *status, struct stat *sb,
1700 struct got_fileindex_entry *ie, const char *abspath,
1701 int dirfd, const char *de_name, struct got_repository *repo)
1703 const struct got_error *err = NULL;
1704 struct got_object_id id;
1709 struct got_blob_object *blob = NULL;
1711 unsigned char staged_status = get_staged_status(ie);
1713 *status = GOT_STATUS_NO_CHANGE;
1716 * Whenever the caller provides a directory descriptor and a
1717 * directory entry name for the file, use them! This prevents
1718 * race conditions if filesystem paths change beneath our feet.
1721 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1722 if (errno == ENOENT) {
1723 if (got_fileindex_entry_has_file_on_disk(ie))
1724 *status = GOT_STATUS_MISSING;
1726 *status = GOT_STATUS_DELETE;
1729 err = got_error_from_errno2("fstatat", abspath);
1733 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1734 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1735 return got_error_from_errno2("open", abspath);
1736 else if (fd == -1 && errno == ELOOP) {
1737 if (lstat(abspath, sb) == -1)
1738 return got_error_from_errno2("lstat", abspath);
1739 } else if (fd == -1 || fstat(fd, sb) == -1) {
1740 if (errno == ENOENT) {
1741 if (got_fileindex_entry_has_file_on_disk(ie))
1742 *status = GOT_STATUS_MISSING;
1744 *status = GOT_STATUS_DELETE;
1747 err = got_error_from_errno2("fstat", abspath);
1752 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1753 *status = GOT_STATUS_OBSTRUCTED;
1757 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1758 *status = GOT_STATUS_DELETE;
1760 } else if (!got_fileindex_entry_has_blob(ie) &&
1761 staged_status != GOT_STATUS_ADD) {
1762 *status = GOT_STATUS_ADD;
1766 if (!stat_info_differs(ie, sb))
1769 if (S_ISLNK(sb->st_mode) &&
1770 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1771 *status = GOT_STATUS_MODIFY;
1775 if (staged_status == GOT_STATUS_MODIFY ||
1776 staged_status == GOT_STATUS_ADD)
1777 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1779 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1781 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1785 if (S_ISLNK(sb->st_mode)) {
1786 err = get_symlink_modification_status(status, ie,
1787 abspath, dirfd, de_name, blob);
1792 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1794 err = got_error_from_errno2("openat", abspath);
1799 f = fdopen(fd, "r");
1801 err = got_error_from_errno2("fdopen", abspath);
1805 hdrlen = got_object_blob_get_hdrlen(blob);
1807 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1808 err = got_object_blob_read_block(&blen, blob);
1811 /* Skip length of blob object header first time around. */
1812 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1813 if (flen == 0 && ferror(f)) {
1814 err = got_error_from_errno("fread");
1817 if (blen - hdrlen == 0) {
1819 *status = GOT_STATUS_MODIFY;
1821 } else if (flen == 0) {
1822 if (blen - hdrlen != 0)
1823 *status = GOT_STATUS_MODIFY;
1825 } else if (blen - hdrlen == flen) {
1826 /* Skip blob object header first time around. */
1827 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1828 *status = GOT_STATUS_MODIFY;
1832 *status = GOT_STATUS_MODIFY;
1838 if (*status == GOT_STATUS_MODIFY) {
1840 err = get_modified_file_content_status(status, f);
1841 } else if (xbit_differs(ie, sb->st_mode))
1842 *status = GOT_STATUS_MODE_CHANGE;
1845 got_object_blob_close(blob);
1846 if (f != NULL && fclose(f) == EOF && err == NULL)
1847 err = got_error_from_errno2("fclose", abspath);
1848 if (fd != -1 && close(fd) == -1 && err == NULL)
1849 err = got_error_from_errno2("close", abspath);
1854 * Update timestamps in the file index if a file is unmodified and
1855 * we had to run a full content comparison to find out.
1857 static const struct got_error *
1858 sync_timestamps(char *ondisk_path, unsigned char status,
1859 struct got_fileindex_entry *ie, struct stat *sb)
1861 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1862 return got_fileindex_entry_update(ie, ondisk_path,
1863 ie->blob_sha1, ie->commit_sha1, 1);
1868 static const struct got_error *
1869 update_blob(struct got_worktree *worktree,
1870 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1871 struct got_tree_entry *te, const char *path,
1872 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1875 const struct got_error *err = NULL;
1876 struct got_blob_object *blob = NULL;
1878 unsigned char status = GOT_STATUS_NO_CHANGE;
1881 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1882 return got_error_from_errno("asprintf");
1885 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1886 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1889 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1893 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1894 sb.st_mode = got_fileindex_perms_to_st(ie);
1896 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1897 status = GOT_STATUS_UNVERSIONED;
1900 if (status == GOT_STATUS_OBSTRUCTED) {
1901 err = (*progress_cb)(progress_arg, status, path);
1904 if (status == GOT_STATUS_CONFLICT) {
1905 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1910 if (ie && status != GOT_STATUS_MISSING &&
1911 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1912 if (got_fileindex_entry_has_commit(ie) &&
1913 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1914 SHA1_DIGEST_LENGTH) == 0) {
1915 err = sync_timestamps(ondisk_path, status, ie, &sb);
1918 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1922 if (got_fileindex_entry_has_blob(ie) &&
1923 memcmp(ie->blob_sha1, te->id.sha1,
1924 SHA1_DIGEST_LENGTH) == 0) {
1925 err = sync_timestamps(ondisk_path, status, ie, &sb);
1930 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1934 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1935 int update_timestamps;
1936 struct got_blob_object *blob2 = NULL;
1937 char *label_orig = NULL;
1938 if (got_fileindex_entry_has_blob(ie)) {
1939 struct got_object_id id2;
1940 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1941 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1945 if (got_fileindex_entry_has_commit(ie)) {
1946 char id_str[SHA1_DIGEST_STRING_LENGTH];
1947 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1948 sizeof(id_str)) == NULL) {
1949 err = got_error_path(id_str,
1950 GOT_ERR_BAD_OBJ_ID_STR);
1953 if (asprintf(&label_orig, "%s: commit %s",
1954 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1955 err = got_error_from_errno("asprintf");
1959 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1961 err = got_object_blob_read_to_str(&link_target, blob);
1964 err = merge_symlink(worktree, blob2, ondisk_path, path,
1965 label_orig, link_target, worktree->base_commit_id,
1966 repo, progress_cb, progress_arg);
1969 err = merge_blob(&update_timestamps, worktree, blob2,
1970 ondisk_path, path, sb.st_mode, label_orig, blob,
1971 worktree->base_commit_id, repo,
1972 progress_cb, progress_arg);
1976 got_object_blob_close(blob2);
1980 * Do not update timestamps of files with local changes.
1981 * Otherwise, a future status walk would treat them as
1982 * unmodified files again.
1984 err = got_fileindex_entry_update(ie, ondisk_path,
1985 blob->id.sha1, worktree->base_commit_id->sha1,
1987 } else if (status == GOT_STATUS_MODE_CHANGE) {
1988 err = got_fileindex_entry_update(ie, ondisk_path,
1989 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1990 } else if (status == GOT_STATUS_DELETE) {
1991 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1994 err = got_fileindex_entry_update(ie, ondisk_path,
1995 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1999 int is_bad_symlink = 0;
2000 if (S_ISLNK(te->mode)) {
2001 err = install_symlink(&is_bad_symlink, worktree,
2002 ondisk_path, path, blob,
2003 status == GOT_STATUS_MISSING, 0,
2004 status == GOT_STATUS_UNVERSIONED, repo,
2005 progress_cb, progress_arg);
2007 err = install_blob(worktree, ondisk_path, path,
2008 te->mode, sb.st_mode, blob,
2009 status == GOT_STATUS_MISSING, 0, 0,
2010 status == GOT_STATUS_UNVERSIONED, repo,
2011 progress_cb, progress_arg);
2017 err = got_fileindex_entry_update(ie, ondisk_path,
2018 blob->id.sha1, worktree->base_commit_id->sha1, 1);
2020 err = create_fileindex_entry(&ie, fileindex,
2021 worktree->base_commit_id, ondisk_path, path,
2027 if (is_bad_symlink) {
2028 got_fileindex_entry_filetype_set(ie,
2029 GOT_FILEIDX_MODE_BAD_SYMLINK);
2032 got_object_blob_close(blob);
2038 static const struct got_error *
2039 remove_ondisk_file(const char *root_path, const char *path)
2041 const struct got_error *err = NULL;
2042 char *ondisk_path = NULL;
2044 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2045 return got_error_from_errno("asprintf");
2047 if (unlink(ondisk_path) == -1) {
2048 if (errno != ENOENT)
2049 err = got_error_from_errno2("unlink", ondisk_path);
2051 size_t root_len = strlen(root_path);
2054 err = got_path_dirname(&parent, ondisk_path);
2058 ondisk_path = parent;
2059 if (rmdir(ondisk_path) == -1) {
2060 if (errno != ENOTEMPTY)
2061 err = got_error_from_errno2("rmdir",
2065 } while (got_path_cmp(ondisk_path, root_path,
2066 strlen(ondisk_path), root_len) != 0);
2072 static const struct got_error *
2073 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2074 struct got_fileindex_entry *ie, struct got_repository *repo,
2075 got_worktree_checkout_cb progress_cb, void *progress_arg)
2077 const struct got_error *err = NULL;
2078 unsigned char status;
2082 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2083 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2085 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2087 return got_error_from_errno("asprintf");
2089 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2093 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2094 char ondisk_target[PATH_MAX];
2095 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2096 sizeof(ondisk_target));
2097 if (ondisk_len == -1) {
2098 err = got_error_from_errno2("readlink", ondisk_path);
2101 ondisk_target[ondisk_len] = '\0';
2102 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2103 NULL, NULL, /* XXX pass common ancestor info? */
2104 ondisk_target, ondisk_path);
2107 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2112 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2113 status == GOT_STATUS_ADD) {
2114 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2118 * Preserve the working file and change the deleted blob's
2119 * entry into a schedule-add entry.
2121 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
2124 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2127 if (status == GOT_STATUS_NO_CHANGE) {
2128 err = remove_ondisk_file(worktree->root_path, ie->path);
2132 got_fileindex_entry_remove(fileindex, ie);
2139 struct diff_cb_arg {
2140 struct got_fileindex *fileindex;
2141 struct got_worktree *worktree;
2142 struct got_repository *repo;
2143 got_worktree_checkout_cb progress_cb;
2145 got_cancel_cb cancel_cb;
2149 static const struct got_error *
2150 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2151 struct got_tree_entry *te, const char *parent_path)
2153 struct diff_cb_arg *a = arg;
2155 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2156 return got_error(GOT_ERR_CANCELLED);
2158 return update_blob(a->worktree, a->fileindex, ie, te,
2159 ie->path, a->repo, a->progress_cb, a->progress_arg);
2162 static const struct got_error *
2163 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2165 struct diff_cb_arg *a = arg;
2167 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2168 return got_error(GOT_ERR_CANCELLED);
2170 return delete_blob(a->worktree, a->fileindex, ie,
2171 a->repo, a->progress_cb, a->progress_arg);
2174 static const struct got_error *
2175 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2177 struct diff_cb_arg *a = arg;
2178 const struct got_error *err;
2181 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2182 return got_error(GOT_ERR_CANCELLED);
2184 if (got_object_tree_entry_is_submodule(te))
2187 if (asprintf(&path, "%s%s%s", parent_path,
2188 parent_path[0] ? "/" : "", te->name)
2190 return got_error_from_errno("asprintf");
2192 if (S_ISDIR(te->mode))
2193 err = add_dir_on_disk(a->worktree, path);
2195 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2196 a->repo, a->progress_cb, a->progress_arg);
2202 const struct got_error *
2203 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2205 uint32_t uuid_status;
2207 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2208 if (uuid_status != uuid_s_ok) {
2210 return got_error_uuid(uuid_status, "uuid_to_string");
2216 static const struct got_error *
2217 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2219 const struct got_error *err = NULL;
2220 char *uuidstr = NULL;
2224 err = got_worktree_get_uuid(&uuidstr, worktree);
2228 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2229 err = got_error_from_errno("asprintf");
2236 const struct got_error *
2237 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2239 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2242 static const struct got_error *
2243 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2245 return get_ref_name(refname, worktree,
2246 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2249 static const struct got_error *
2250 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2252 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2255 static const struct got_error *
2256 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2258 return get_ref_name(refname, worktree,
2259 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2262 static const struct got_error *
2263 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2265 return get_ref_name(refname, worktree,
2266 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2269 static const struct got_error *
2270 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2272 return get_ref_name(refname, worktree,
2273 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2276 static const struct got_error *
2277 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2279 return get_ref_name(refname, worktree,
2280 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2283 static const struct got_error *
2284 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2286 return get_ref_name(refname, worktree,
2287 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2290 static const struct got_error *
2291 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2293 return get_ref_name(refname, worktree,
2294 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2297 const struct got_error *
2298 got_worktree_get_histedit_script_path(char **path,
2299 struct got_worktree *worktree)
2301 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2302 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2304 return got_error_from_errno("asprintf");
2310 * Prevent Git's garbage collector from deleting our base commit by
2311 * setting a reference to our base commit's ID.
2313 static const struct got_error *
2314 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2316 const struct got_error *err = NULL;
2317 struct got_reference *ref = NULL;
2320 err = got_worktree_get_base_ref_name(&refname, worktree);
2324 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2328 err = got_ref_write(ref, repo);
2336 static const struct got_error *
2337 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2339 const struct got_error *err = NULL;
2341 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2342 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2343 err = got_error_from_errno("asprintf");
2344 *fileindex_path = NULL;
2350 static const struct got_error *
2351 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2352 struct got_worktree *worktree)
2354 const struct got_error *err = NULL;
2357 *fileindex_path = NULL;
2358 *fileindex = got_fileindex_alloc();
2359 if (*fileindex == NULL)
2360 return got_error_from_errno("got_fileindex_alloc");
2362 err = get_fileindex_path(fileindex_path, worktree);
2366 index = fopen(*fileindex_path, "rb");
2367 if (index == NULL) {
2368 if (errno != ENOENT)
2369 err = got_error_from_errno2("fopen", *fileindex_path);
2371 err = got_fileindex_read(*fileindex, index);
2372 if (fclose(index) != 0 && err == NULL)
2373 err = got_error_from_errno("fclose");
2377 free(*fileindex_path);
2378 *fileindex_path = NULL;
2379 got_fileindex_free(*fileindex);
2385 struct bump_base_commit_id_arg {
2386 struct got_object_id *base_commit_id;
2389 const char *entry_name;
2390 got_worktree_checkout_cb progress_cb;
2394 /* Bump base commit ID of all files within an updated part of the work tree. */
2395 static const struct got_error *
2396 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2398 const struct got_error *err;
2399 struct bump_base_commit_id_arg *a = arg;
2401 if (a->entry_name) {
2402 if (strcmp(ie->path, a->path) != 0)
2404 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2407 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2408 SHA1_DIGEST_LENGTH) == 0)
2411 if (a->progress_cb) {
2412 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2417 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2421 static const struct got_error *
2422 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2424 const struct got_error *err = NULL;
2425 char *new_fileindex_path = NULL;
2426 FILE *new_index = NULL;
2427 struct timespec timeout;
2429 err = got_opentemp_named(&new_fileindex_path, &new_index,
2434 err = got_fileindex_write(fileindex, new_index);
2438 if (rename(new_fileindex_path, fileindex_path) != 0) {
2439 err = got_error_from_errno3("rename", new_fileindex_path,
2441 unlink(new_fileindex_path);
2445 * Sleep for a short amount of time to ensure that files modified after
2446 * this program exits have a different time stamp from the one which
2447 * was recorded in the file index.
2450 timeout.tv_nsec = 1;
2451 nanosleep(&timeout, NULL);
2455 free(new_fileindex_path);
2459 static const struct got_error *
2460 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2461 struct got_object_id **tree_id, const char *wt_relpath,
2462 struct got_worktree *worktree, struct got_repository *repo)
2464 const struct got_error *err = NULL;
2465 struct got_object_id *id = NULL;
2466 char *in_repo_path = NULL;
2467 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2469 *entry_type = GOT_OBJ_TYPE_ANY;
2470 *tree_relpath = NULL;
2473 if (wt_relpath[0] == '\0') {
2474 /* Check out all files within the work tree. */
2475 *entry_type = GOT_OBJ_TYPE_TREE;
2476 *tree_relpath = strdup("");
2477 if (*tree_relpath == NULL) {
2478 err = got_error_from_errno("strdup");
2481 err = got_object_id_by_path(tree_id, repo,
2482 worktree->base_commit_id, worktree->path_prefix);
2488 /* Check out a subset of files in the work tree. */
2490 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2491 is_root_wt ? "" : "/", wt_relpath) == -1) {
2492 err = got_error_from_errno("asprintf");
2496 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2502 in_repo_path = NULL;
2504 err = got_object_get_type(entry_type, repo, id);
2508 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2509 /* Check out a single file. */
2510 if (strchr(wt_relpath, '/') == NULL) {
2511 /* Check out a single file in work tree's root dir. */
2512 in_repo_path = strdup(worktree->path_prefix);
2513 if (in_repo_path == NULL) {
2514 err = got_error_from_errno("strdup");
2517 *tree_relpath = strdup("");
2518 if (*tree_relpath == NULL) {
2519 err = got_error_from_errno("strdup");
2523 /* Check out a single file in a subdirectory. */
2524 err = got_path_dirname(tree_relpath, wt_relpath);
2527 if (asprintf(&in_repo_path, "%s%s%s",
2528 worktree->path_prefix, is_root_wt ? "" : "/",
2529 *tree_relpath) == -1) {
2530 err = got_error_from_errno("asprintf");
2534 err = got_object_id_by_path(tree_id, repo,
2535 worktree->base_commit_id, in_repo_path);
2537 /* Check out all files within a subdirectory. */
2538 *tree_id = got_object_id_dup(id);
2539 if (*tree_id == NULL) {
2540 err = got_error_from_errno("got_object_id_dup");
2543 *tree_relpath = strdup(wt_relpath);
2544 if (*tree_relpath == NULL) {
2545 err = got_error_from_errno("strdup");
2553 *entry_type = GOT_OBJ_TYPE_ANY;
2554 free(*tree_relpath);
2555 *tree_relpath = NULL;
2562 static const struct got_error *
2563 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2564 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2565 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2566 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2568 const struct got_error *err = NULL;
2569 struct got_commit_object *commit = NULL;
2570 struct got_tree_object *tree = NULL;
2571 struct got_fileindex_diff_tree_cb diff_cb;
2572 struct diff_cb_arg arg;
2574 err = ref_base_commit(worktree, repo);
2576 if (!(err->code == GOT_ERR_ERRNO &&
2577 (errno == EACCES || errno == EROFS)))
2579 err = (*progress_cb)(progress_arg,
2580 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2585 err = got_object_open_as_commit(&commit, repo,
2586 worktree->base_commit_id);
2590 err = got_object_open_as_tree(&tree, repo, tree_id);
2595 got_object_tree_find_entry(tree, entry_name) == NULL) {
2596 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2600 diff_cb.diff_old_new = diff_old_new;
2601 diff_cb.diff_old = diff_old;
2602 diff_cb.diff_new = diff_new;
2603 arg.fileindex = fileindex;
2604 arg.worktree = worktree;
2606 arg.progress_cb = progress_cb;
2607 arg.progress_arg = progress_arg;
2608 arg.cancel_cb = cancel_cb;
2609 arg.cancel_arg = cancel_arg;
2610 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2611 entry_name, repo, &diff_cb, &arg);
2614 got_object_tree_close(tree);
2616 got_object_commit_close(commit);
2620 const struct got_error *
2621 got_worktree_checkout_files(struct got_worktree *worktree,
2622 struct got_pathlist_head *paths, struct got_repository *repo,
2623 got_worktree_checkout_cb progress_cb, void *progress_arg,
2624 got_cancel_cb cancel_cb, void *cancel_arg)
2626 const struct got_error *err = NULL, *sync_err, *unlockerr;
2627 struct got_commit_object *commit = NULL;
2628 struct got_tree_object *tree = NULL;
2629 struct got_fileindex *fileindex = NULL;
2630 char *fileindex_path = NULL;
2631 struct got_pathlist_entry *pe;
2632 struct tree_path_data {
2633 SIMPLEQ_ENTRY(tree_path_data) entry;
2634 struct got_object_id *tree_id;
2639 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2641 SIMPLEQ_INIT(&tree_paths);
2643 err = lock_worktree(worktree, LOCK_EX);
2647 /* Map all specified paths to in-repository trees. */
2648 TAILQ_FOREACH(pe, paths, entry) {
2649 tpd = malloc(sizeof(*tpd));
2651 err = got_error_from_errno("malloc");
2655 err = find_tree_entry_for_checkout(&tpd->entry_type,
2656 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2662 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2663 err = got_path_basename(&tpd->entry_name, pe->path);
2671 tpd->entry_name = NULL;
2673 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2677 * Read the file index.
2678 * Checking out files is supposed to be an idempotent operation.
2679 * If the on-disk file index is incomplete we will try to complete it.
2681 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2685 tpd = SIMPLEQ_FIRST(&tree_paths);
2686 TAILQ_FOREACH(pe, paths, entry) {
2687 struct bump_base_commit_id_arg bbc_arg;
2689 err = checkout_files(worktree, fileindex, tpd->relpath,
2690 tpd->tree_id, tpd->entry_name, repo,
2691 progress_cb, progress_arg, cancel_cb, cancel_arg);
2695 bbc_arg.base_commit_id = worktree->base_commit_id;
2696 bbc_arg.entry_name = tpd->entry_name;
2697 bbc_arg.path = pe->path;
2698 bbc_arg.path_len = pe->path_len;
2699 bbc_arg.progress_cb = progress_cb;
2700 bbc_arg.progress_arg = progress_arg;
2701 err = got_fileindex_for_each_entry_safe(fileindex,
2702 bump_base_commit_id, &bbc_arg);
2706 tpd = SIMPLEQ_NEXT(tpd, entry);
2708 sync_err = sync_fileindex(fileindex, fileindex_path);
2709 if (sync_err && err == NULL)
2712 free(fileindex_path);
2714 got_object_tree_close(tree);
2716 got_object_commit_close(commit);
2718 got_fileindex_free(fileindex);
2719 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2720 tpd = SIMPLEQ_FIRST(&tree_paths);
2721 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2726 unlockerr = lock_worktree(worktree, LOCK_SH);
2727 if (unlockerr && err == NULL)
2732 struct merge_file_cb_arg {
2733 struct got_worktree *worktree;
2734 struct got_fileindex *fileindex;
2735 got_worktree_checkout_cb progress_cb;
2737 got_cancel_cb cancel_cb;
2739 const char *label_orig;
2740 struct got_object_id *commit_id2;
2743 static const struct got_error *
2744 merge_file_cb(void *arg, struct got_blob_object *blob1,
2745 struct got_blob_object *blob2, struct got_object_id *id1,
2746 struct got_object_id *id2, const char *path1, const char *path2,
2747 mode_t mode1, mode_t mode2, struct got_repository *repo)
2749 static const struct got_error *err = NULL;
2750 struct merge_file_cb_arg *a = arg;
2751 struct got_fileindex_entry *ie;
2752 char *ondisk_path = NULL;
2754 unsigned char status;
2755 int local_changes_subsumed;
2757 if (blob1 && blob2) {
2758 ie = got_fileindex_entry_get(a->fileindex, path2,
2761 return (*a->progress_cb)(a->progress_arg,
2762 GOT_STATUS_MISSING, path2);
2764 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2766 return got_error_from_errno("asprintf");
2768 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2773 if (status == GOT_STATUS_DELETE) {
2774 err = (*a->progress_cb)(a->progress_arg,
2775 GOT_STATUS_MERGE, path2);
2778 if (status != GOT_STATUS_NO_CHANGE &&
2779 status != GOT_STATUS_MODIFY &&
2780 status != GOT_STATUS_CONFLICT &&
2781 status != GOT_STATUS_ADD) {
2782 err = (*a->progress_cb)(a->progress_arg, status, path2);
2786 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2788 err = got_object_blob_read_to_str(&link_target2, blob2);
2791 err = merge_symlink(a->worktree, blob1, ondisk_path,
2792 path2, a->label_orig, link_target2, a->commit_id2,
2793 repo, a->progress_cb, a->progress_arg);
2796 err = merge_blob(&local_changes_subsumed, a->worktree,
2797 blob1, ondisk_path, path2, sb.st_mode,
2798 a->label_orig, blob2, a->commit_id2, repo,
2799 a->progress_cb, a->progress_arg);
2802 ie = got_fileindex_entry_get(a->fileindex, path1,
2805 return (*a->progress_cb)(a->progress_arg,
2806 GOT_STATUS_MISSING, path1);
2808 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2810 return got_error_from_errno("asprintf");
2812 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2818 case GOT_STATUS_NO_CHANGE:
2819 err = (*a->progress_cb)(a->progress_arg,
2820 GOT_STATUS_DELETE, path1);
2823 err = remove_ondisk_file(a->worktree->root_path, path1);
2827 got_fileindex_entry_mark_deleted_from_disk(ie);
2829 case GOT_STATUS_DELETE:
2830 case GOT_STATUS_MISSING:
2831 err = (*a->progress_cb)(a->progress_arg,
2832 GOT_STATUS_DELETE, path1);
2836 got_fileindex_entry_mark_deleted_from_disk(ie);
2838 case GOT_STATUS_ADD: {
2839 struct got_object_id *id;
2842 * Delete the added file only if its content already
2843 * exists in the repository.
2845 err = got_object_blob_file_create(&id, &blob1_f, path1);
2848 if (got_object_id_cmp(id, id1) == 0) {
2849 err = (*a->progress_cb)(a->progress_arg,
2850 GOT_STATUS_DELETE, path1);
2853 err = remove_ondisk_file(a->worktree->root_path,
2858 got_fileindex_entry_remove(a->fileindex,
2861 err = (*a->progress_cb)(a->progress_arg,
2862 GOT_STATUS_CANNOT_DELETE, path1);
2864 if (fclose(blob1_f) == EOF && err == NULL)
2865 err = got_error_from_errno("fclose");
2871 case GOT_STATUS_MODIFY:
2872 case GOT_STATUS_CONFLICT:
2873 err = (*a->progress_cb)(a->progress_arg,
2874 GOT_STATUS_CANNOT_DELETE, path1);
2878 case GOT_STATUS_OBSTRUCTED:
2879 err = (*a->progress_cb)(a->progress_arg, status, path1);
2887 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2889 return got_error_from_errno("asprintf");
2890 ie = got_fileindex_entry_get(a->fileindex, path2,
2893 err = get_file_status(&status, &sb, ie, ondisk_path,
2897 if (status != GOT_STATUS_NO_CHANGE &&
2898 status != GOT_STATUS_MODIFY &&
2899 status != GOT_STATUS_CONFLICT &&
2900 status != GOT_STATUS_ADD) {
2901 err = (*a->progress_cb)(a->progress_arg,
2905 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2907 err = got_object_blob_read_to_str(&link_target2,
2911 err = merge_symlink(a->worktree, NULL,
2912 ondisk_path, path2, a->label_orig,
2913 link_target2, a->commit_id2, repo,
2914 a->progress_cb, a->progress_arg);
2916 } else if (S_ISREG(sb.st_mode)) {
2917 err = merge_blob(&local_changes_subsumed,
2918 a->worktree, NULL, ondisk_path, path2,
2919 sb.st_mode, a->label_orig, blob2,
2920 a->commit_id2, repo, a->progress_cb,
2923 err = got_error_path(ondisk_path,
2924 GOT_ERR_FILE_OBSTRUCTED);
2928 if (status == GOT_STATUS_DELETE) {
2929 err = got_fileindex_entry_update(ie,
2930 ondisk_path, blob2->id.sha1,
2931 a->worktree->base_commit_id->sha1, 0);
2936 int is_bad_symlink = 0;
2937 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2938 if (S_ISLNK(mode2)) {
2939 err = install_symlink(&is_bad_symlink,
2940 a->worktree, ondisk_path, path2, blob2, 0,
2941 0, 1, repo, a->progress_cb, a->progress_arg);
2943 err = install_blob(a->worktree, ondisk_path, path2,
2944 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2945 a->progress_cb, a->progress_arg);
2949 err = got_fileindex_entry_alloc(&ie, path2);
2952 err = got_fileindex_entry_update(ie, ondisk_path,
2955 got_fileindex_entry_free(ie);
2958 err = got_fileindex_entry_add(a->fileindex, ie);
2960 got_fileindex_entry_free(ie);
2963 if (is_bad_symlink) {
2964 got_fileindex_entry_filetype_set(ie,
2965 GOT_FILEIDX_MODE_BAD_SYMLINK);
2974 struct check_merge_ok_arg {
2975 struct got_worktree *worktree;
2976 struct got_repository *repo;
2979 static const struct got_error *
2980 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2982 const struct got_error *err = NULL;
2983 struct check_merge_ok_arg *a = arg;
2984 unsigned char status;
2988 /* Reject merges into a work tree with mixed base commits. */
2989 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2990 SHA1_DIGEST_LENGTH))
2991 return got_error(GOT_ERR_MIXED_COMMITS);
2993 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2995 return got_error_from_errno("asprintf");
2997 /* Reject merges into a work tree with conflicted files. */
2998 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3001 if (status == GOT_STATUS_CONFLICT)
3002 return got_error(GOT_ERR_CONFLICTS);
3007 static const struct got_error *
3008 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3009 const char *fileindex_path, struct got_object_id *commit_id1,
3010 struct got_object_id *commit_id2, struct got_repository *repo,
3011 got_worktree_checkout_cb progress_cb, void *progress_arg,
3012 got_cancel_cb cancel_cb, void *cancel_arg)
3014 const struct got_error *err = NULL, *sync_err;
3015 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3016 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3017 struct merge_file_cb_arg arg;
3018 char *label_orig = NULL;
3021 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3022 worktree->path_prefix);
3023 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3029 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3033 err = got_object_id_str(&id_str, commit_id1);
3037 if (asprintf(&label_orig, "%s: commit %s",
3038 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3039 err = got_error_from_errno("asprintf");
3046 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3047 worktree->path_prefix);
3051 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3055 arg.worktree = worktree;
3056 arg.fileindex = fileindex;
3057 arg.progress_cb = progress_cb;
3058 arg.progress_arg = progress_arg;
3059 arg.cancel_cb = cancel_cb;
3060 arg.cancel_arg = cancel_arg;
3061 arg.label_orig = label_orig;
3062 arg.commit_id2 = commit_id2;
3063 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3064 sync_err = sync_fileindex(fileindex, fileindex_path);
3065 if (sync_err && err == NULL)
3069 got_object_tree_close(tree1);
3071 got_object_tree_close(tree2);
3076 const struct got_error *
3077 got_worktree_merge_files(struct got_worktree *worktree,
3078 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3079 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3080 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3082 const struct got_error *err, *unlockerr;
3083 char *fileindex_path = NULL;
3084 struct got_fileindex *fileindex = NULL;
3085 struct check_merge_ok_arg mok_arg;
3087 err = lock_worktree(worktree, LOCK_EX);
3091 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3095 mok_arg.worktree = worktree;
3096 mok_arg.repo = repo;
3097 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
3102 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3103 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
3106 got_fileindex_free(fileindex);
3107 free(fileindex_path);
3108 unlockerr = lock_worktree(worktree, LOCK_SH);
3109 if (unlockerr && err == NULL)
3114 struct diff_dir_cb_arg {
3115 struct got_fileindex *fileindex;
3116 struct got_worktree *worktree;
3117 const char *status_path;
3118 size_t status_path_len;
3119 struct got_repository *repo;
3120 got_worktree_status_cb status_cb;
3122 got_cancel_cb cancel_cb;
3124 /* A pathlist containing per-directory pathlists of ignore patterns. */
3125 struct got_pathlist_head ignores;
3126 int report_unchanged;
3130 static const struct got_error *
3131 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3132 int dirfd, const char *de_name,
3133 got_worktree_status_cb status_cb, void *status_arg,
3134 struct got_repository *repo, int report_unchanged)
3136 const struct got_error *err = NULL;
3137 unsigned char status = GOT_STATUS_NO_CHANGE;
3138 unsigned char staged_status = get_staged_status(ie);
3140 struct got_object_id blob_id, commit_id, staged_blob_id;
3141 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3142 struct got_object_id *staged_blob_idp = NULL;
3144 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3148 if (status == GOT_STATUS_NO_CHANGE &&
3149 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3152 if (got_fileindex_entry_has_blob(ie)) {
3153 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3154 blob_idp = &blob_id;
3156 if (got_fileindex_entry_has_commit(ie)) {
3157 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3158 commit_idp = &commit_id;
3160 if (staged_status == GOT_STATUS_ADD ||
3161 staged_status == GOT_STATUS_MODIFY) {
3162 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3163 SHA1_DIGEST_LENGTH);
3164 staged_blob_idp = &staged_blob_id;
3167 return (*status_cb)(status_arg, status, staged_status,
3168 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3171 static const struct got_error *
3172 status_old_new(void *arg, struct got_fileindex_entry *ie,
3173 struct dirent *de, const char *parent_path, int dirfd)
3175 const struct got_error *err = NULL;
3176 struct diff_dir_cb_arg *a = arg;
3179 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3180 return got_error(GOT_ERR_CANCELLED);
3182 if (got_path_cmp(parent_path, a->status_path,
3183 strlen(parent_path), a->status_path_len) != 0 &&
3184 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3187 if (parent_path[0]) {
3188 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3189 parent_path, de->d_name) == -1)
3190 return got_error_from_errno("asprintf");
3192 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3194 return got_error_from_errno("asprintf");
3197 err = report_file_status(ie, abspath, dirfd, de->d_name,
3198 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3203 static const struct got_error *
3204 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3206 struct diff_dir_cb_arg *a = arg;
3207 struct got_object_id blob_id, commit_id;
3208 unsigned char status;
3210 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3211 return got_error(GOT_ERR_CANCELLED);
3213 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3216 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3217 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3218 if (got_fileindex_entry_has_file_on_disk(ie))
3219 status = GOT_STATUS_MISSING;
3221 status = GOT_STATUS_DELETE;
3222 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3223 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3227 free_ignorelist(struct got_pathlist_head *ignorelist)
3229 struct got_pathlist_entry *pe;
3231 TAILQ_FOREACH(pe, ignorelist, entry)
3232 free((char *)pe->path);
3233 got_pathlist_free(ignorelist);
3237 free_ignores(struct got_pathlist_head *ignores)
3239 struct got_pathlist_entry *pe;
3241 TAILQ_FOREACH(pe, ignores, entry) {
3242 struct got_pathlist_head *ignorelist = pe->data;
3243 free_ignorelist(ignorelist);
3244 free((char *)pe->path);
3246 got_pathlist_free(ignores);
3249 static const struct got_error *
3250 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3252 const struct got_error *err = NULL;
3253 struct got_pathlist_entry *pe = NULL;
3254 struct got_pathlist_head *ignorelist;
3255 char *line = NULL, *pattern, *dirpath = NULL;
3256 size_t linesize = 0;
3259 ignorelist = calloc(1, sizeof(*ignorelist));
3260 if (ignorelist == NULL)
3261 return got_error_from_errno("calloc");
3262 TAILQ_INIT(ignorelist);
3264 while ((linelen = getline(&line, &linesize, f)) != -1) {
3265 if (linelen > 0 && line[linelen - 1] == '\n')
3266 line[linelen - 1] = '\0';
3268 /* Git's ignores may contain comments. */
3272 /* Git's negated patterns are not (yet?) supported. */
3276 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3278 err = got_error_from_errno("asprintf");
3281 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3286 err = got_error_from_errno("getline");
3290 dirpath = strdup(path);
3291 if (dirpath == NULL) {
3292 err = got_error_from_errno("strdup");
3295 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3298 if (err || pe == NULL) {
3300 free_ignorelist(ignorelist);
3306 match_ignores(struct got_pathlist_head *ignores, const char *path)
3308 struct got_pathlist_entry *pe;
3310 /* Handle patterns which match in all directories. */
3311 TAILQ_FOREACH(pe, ignores, entry) {
3312 struct got_pathlist_head *ignorelist = pe->data;
3313 struct got_pathlist_entry *pi;
3315 TAILQ_FOREACH(pi, ignorelist, entry) {
3316 const char *p, *pattern = pi->path;
3318 if (strncmp(pattern, "**/", 3) != 0)
3323 if (fnmatch(pattern, p,
3324 FNM_PATHNAME | FNM_LEADING_DIR)) {
3325 /* Retry in next directory. */
3326 while (*p && *p != '/')
3338 * The ignores pathlist contains ignore lists from children before
3339 * parents, so we can find the most specific ignorelist by walking
3340 * ignores backwards.
3342 pe = TAILQ_LAST(ignores, got_pathlist_head);
3344 if (got_path_is_child(path, pe->path, pe->path_len)) {
3345 struct got_pathlist_head *ignorelist = pe->data;
3346 struct got_pathlist_entry *pi;
3347 TAILQ_FOREACH(pi, ignorelist, entry) {
3348 const char *pattern = pi->path;
3349 int flags = FNM_LEADING_DIR;
3350 if (strstr(pattern, "/**/") == NULL)
3351 flags |= FNM_PATHNAME;
3352 if (fnmatch(pattern, path, flags))
3357 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3363 static const struct got_error *
3364 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3365 const char *path, int dirfd, const char *ignores_filename)
3367 const struct got_error *err = NULL;
3370 FILE *ignoresfile = NULL;
3372 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3373 path[0] ? "/" : "", ignores_filename) == -1)
3374 return got_error_from_errno("asprintf");
3377 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3379 if (errno != ENOENT && errno != EACCES)
3380 err = got_error_from_errno2("openat",
3383 ignoresfile = fdopen(fd, "r");
3384 if (ignoresfile == NULL)
3385 err = got_error_from_errno2("fdopen",
3389 err = read_ignores(ignores, path, ignoresfile);
3393 ignoresfile = fopen(ignorespath, "r");
3394 if (ignoresfile == NULL) {
3395 if (errno != ENOENT && errno != EACCES)
3396 err = got_error_from_errno2("fopen",
3399 err = read_ignores(ignores, path, ignoresfile);
3402 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3403 err = got_error_from_errno2("fclose", path);
3404 if (fd != -1 && close(fd) == -1 && err == NULL)
3405 err = got_error_from_errno2("close", path);
3410 static const struct got_error *
3411 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3413 const struct got_error *err = NULL;
3414 struct diff_dir_cb_arg *a = arg;
3417 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3418 return got_error(GOT_ERR_CANCELLED);
3420 if (parent_path[0]) {
3421 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3422 return got_error_from_errno("asprintf");
3427 if (de->d_type != DT_DIR &&
3428 got_path_is_child(path, a->status_path, a->status_path_len)
3429 && !match_ignores(&a->ignores, path))
3430 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3431 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3437 static const struct got_error *
3438 status_traverse(void *arg, const char *path, int dirfd)
3440 const struct got_error *err = NULL;
3441 struct diff_dir_cb_arg *a = arg;
3446 err = add_ignores(&a->ignores, a->worktree->root_path,
3447 path, dirfd, ".cvsignore");
3451 err = add_ignores(&a->ignores, a->worktree->root_path, path,
3452 dirfd, ".gitignore");
3457 static const struct got_error *
3458 report_single_file_status(const char *path, const char *ondisk_path,
3459 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3460 void *status_arg, struct got_repository *repo, int report_unchanged)
3462 struct got_fileindex_entry *ie;
3465 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3467 return report_file_status(ie, ondisk_path, -1, NULL,
3468 status_cb, status_arg, repo, report_unchanged);
3470 if (lstat(ondisk_path, &sb) == -1) {
3471 if (errno != ENOENT)
3472 return got_error_from_errno2("lstat", ondisk_path);
3473 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3474 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3478 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3479 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3480 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3485 static const struct got_error *
3486 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3487 const char *root_path, const char *path)
3489 const struct got_error *err;
3490 char *parent_path, *next_parent_path = NULL;
3492 err = add_ignores(ignores, root_path, "", -1,
3497 err = add_ignores(ignores, root_path, "", -1,
3502 err = got_path_dirname(&parent_path, path);
3504 if (err->code == GOT_ERR_BAD_PATH)
3505 return NULL; /* cannot traverse parent */
3509 err = add_ignores(ignores, root_path, parent_path, -1,
3513 err = add_ignores(ignores, root_path, parent_path, -1,
3517 err = got_path_dirname(&next_parent_path, parent_path);
3519 if (err->code == GOT_ERR_BAD_PATH)
3520 err = NULL; /* traversed everything */
3524 parent_path = next_parent_path;
3525 next_parent_path = NULL;
3529 free(next_parent_path);
3533 static const struct got_error *
3534 worktree_status(struct got_worktree *worktree, const char *path,
3535 struct got_fileindex *fileindex, struct got_repository *repo,
3536 got_worktree_status_cb status_cb, void *status_arg,
3537 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3538 int report_unchanged)
3540 const struct got_error *err = NULL;
3542 struct got_fileindex_diff_dir_cb fdiff_cb;
3543 struct diff_dir_cb_arg arg;
3544 char *ondisk_path = NULL;
3546 TAILQ_INIT(&arg.ignores);
3548 if (asprintf(&ondisk_path, "%s%s%s",
3549 worktree->root_path, path[0] ? "/" : "", path) == -1)
3550 return got_error_from_errno("asprintf");
3552 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3554 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3556 err = got_error_from_errno2("open", ondisk_path);
3558 err = report_single_file_status(path, ondisk_path,
3559 fileindex, status_cb, status_arg, repo,
3562 fdiff_cb.diff_old_new = status_old_new;
3563 fdiff_cb.diff_old = status_old;
3564 fdiff_cb.diff_new = status_new;
3565 fdiff_cb.diff_traverse = status_traverse;
3566 arg.fileindex = fileindex;
3567 arg.worktree = worktree;
3568 arg.status_path = path;
3569 arg.status_path_len = strlen(path);
3571 arg.status_cb = status_cb;
3572 arg.status_arg = status_arg;
3573 arg.cancel_cb = cancel_cb;
3574 arg.cancel_arg = cancel_arg;
3575 arg.report_unchanged = report_unchanged;
3576 arg.no_ignores = no_ignores;
3578 err = add_ignores_from_parent_paths(&arg.ignores,
3579 worktree->root_path, path);
3583 err = got_fileindex_diff_dir(fileindex, fd,
3584 worktree->root_path, path, repo, &fdiff_cb, &arg);
3587 free_ignores(&arg.ignores);
3588 if (fd != -1 && close(fd) != 0 && err == NULL)
3589 err = got_error_from_errno("close");
3594 const struct got_error *
3595 got_worktree_status(struct got_worktree *worktree,
3596 struct got_pathlist_head *paths, struct got_repository *repo,
3597 got_worktree_status_cb status_cb, void *status_arg,
3598 got_cancel_cb cancel_cb, void *cancel_arg)
3600 const struct got_error *err = NULL;
3601 char *fileindex_path = NULL;
3602 struct got_fileindex *fileindex = NULL;
3603 struct got_pathlist_entry *pe;
3605 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3609 TAILQ_FOREACH(pe, paths, entry) {
3610 err = worktree_status(worktree, pe->path, fileindex, repo,
3611 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3615 free(fileindex_path);
3616 got_fileindex_free(fileindex);
3620 const struct got_error *
3621 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3624 const struct got_error *err = NULL;
3625 char *resolved = NULL, *cwd = NULL, *path = NULL;
3628 char *abspath = NULL;
3629 char canonpath[PATH_MAX];
3633 cwd = getcwd(NULL, 0);
3635 return got_error_from_errno("getcwd");
3637 if (lstat(arg, &sb) == -1) {
3638 if (errno != ENOENT) {
3639 err = got_error_from_errno2("lstat", arg);
3644 if (S_ISLNK(sb.st_mode)) {
3646 * We cannot use realpath(3) with symlinks since we want to
3647 * operate on the symlink itself.
3648 * But we can make the path absolute, assuming it is relative
3649 * to the current working directory, and then canonicalize it.
3651 if (!got_path_is_absolute(arg)) {
3652 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3653 err = got_error_from_errno("asprintf");
3658 err = got_canonpath(abspath ? abspath : arg, canonpath,
3662 resolved = strdup(canonpath);
3663 if (resolved == NULL) {
3664 err = got_error_from_errno("strdup");
3668 resolved = realpath(arg, NULL);
3669 if (resolved == NULL) {
3670 if (errno != ENOENT) {
3671 err = got_error_from_errno2("realpath", arg);
3674 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3675 err = got_error_from_errno("asprintf");
3678 err = got_canonpath(abspath, canonpath,
3682 resolved = strdup(canonpath);
3683 if (resolved == NULL) {
3684 err = got_error_from_errno("strdup");
3690 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3691 strlen(got_worktree_get_root_path(worktree)))) {
3692 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3696 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3697 err = got_path_skip_common_ancestor(&path,
3698 got_worktree_get_root_path(worktree), resolved);
3704 err = got_error_from_errno("strdup");
3709 /* XXX status walk can't deal with trailing slash! */
3711 while (len > 0 && path[len - 1] == '/') {
3712 path[len - 1] = '\0';
3726 struct schedule_addition_args {
3727 struct got_worktree *worktree;
3728 struct got_fileindex *fileindex;
3729 got_worktree_checkout_cb progress_cb;
3731 struct got_repository *repo;
3734 static const struct got_error *
3735 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3736 const char *relpath, struct got_object_id *blob_id,
3737 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3738 int dirfd, const char *de_name)
3740 struct schedule_addition_args *a = arg;
3741 const struct got_error *err = NULL;
3742 struct got_fileindex_entry *ie;
3746 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3748 return got_error_from_errno("asprintf");
3750 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3752 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3756 /* Re-adding an existing entry is a no-op. */
3757 if (status == GOT_STATUS_ADD)
3759 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3764 if (status != GOT_STATUS_UNVERSIONED) {
3765 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3769 err = got_fileindex_entry_alloc(&ie, relpath);
3772 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3774 got_fileindex_entry_free(ie);
3777 err = got_fileindex_entry_add(a->fileindex, ie);
3779 got_fileindex_entry_free(ie);
3786 if (status == GOT_STATUS_ADD)
3788 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3791 const struct got_error *
3792 got_worktree_schedule_add(struct got_worktree *worktree,
3793 struct got_pathlist_head *paths,
3794 got_worktree_checkout_cb progress_cb, void *progress_arg,
3795 struct got_repository *repo, int no_ignores)
3797 struct got_fileindex *fileindex = NULL;
3798 char *fileindex_path = NULL;
3799 const struct got_error *err = NULL, *sync_err, *unlockerr;
3800 struct got_pathlist_entry *pe;
3801 struct schedule_addition_args saa;
3803 err = lock_worktree(worktree, LOCK_EX);
3807 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3811 saa.worktree = worktree;
3812 saa.fileindex = fileindex;
3813 saa.progress_cb = progress_cb;
3814 saa.progress_arg = progress_arg;
3817 TAILQ_FOREACH(pe, paths, entry) {
3818 err = worktree_status(worktree, pe->path, fileindex, repo,
3819 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3823 sync_err = sync_fileindex(fileindex, fileindex_path);
3824 if (sync_err && err == NULL)
3827 free(fileindex_path);
3829 got_fileindex_free(fileindex);
3830 unlockerr = lock_worktree(worktree, LOCK_SH);
3831 if (unlockerr && err == NULL)
3836 struct schedule_deletion_args {
3837 struct got_worktree *worktree;
3838 struct got_fileindex *fileindex;
3839 got_worktree_delete_cb progress_cb;
3841 struct got_repository *repo;
3842 int delete_local_mods;
3844 const char *status_codes;
3847 static const struct got_error *
3848 schedule_for_deletion(void *arg, unsigned char status,
3849 unsigned char staged_status, const char *relpath,
3850 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3851 struct got_object_id *commit_id, int dirfd, const char *de_name)
3853 struct schedule_deletion_args *a = arg;
3854 const struct got_error *err = NULL;
3855 struct got_fileindex_entry *ie = NULL;
3859 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3861 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3863 staged_status = get_staged_status(ie);
3864 if (staged_status != GOT_STATUS_NO_CHANGE) {
3865 if (staged_status == GOT_STATUS_DELETE)
3867 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3870 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3872 return got_error_from_errno("asprintf");
3874 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3879 if (a->status_codes) {
3880 size_t ncodes = strlen(a->status_codes);
3882 for (i = 0; i < ncodes ; i++) {
3883 if (status == a->status_codes[i])
3887 /* Do not delete files in non-matching status. */
3891 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
3892 a->status_codes[i] != GOT_STATUS_MISSING) {
3893 static char msg[64];
3894 snprintf(msg, sizeof(msg),
3895 "invalid status code '%c'", a->status_codes[i]);
3896 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
3901 if (status != GOT_STATUS_NO_CHANGE) {
3902 if (status == GOT_STATUS_DELETE)
3904 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3905 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3908 if (status != GOT_STATUS_MODIFY &&
3909 status != GOT_STATUS_MISSING) {
3910 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3915 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3919 if (unlinkat(dirfd, de_name, 0) != 0) {
3920 err = got_error_from_errno2("unlinkat",
3924 } else if (unlink(ondisk_path) != 0) {
3925 err = got_error_from_errno2("unlink", ondisk_path);
3929 root_len = strlen(a->worktree->root_path);
3932 err = got_path_dirname(&parent, ondisk_path);
3936 ondisk_path = parent;
3937 if (rmdir(ondisk_path) == -1) {
3938 if (errno != ENOTEMPTY)
3939 err = got_error_from_errno2("rmdir",
3943 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
3944 strlen(ondisk_path), root_len) != 0);
3947 got_fileindex_entry_mark_deleted_from_disk(ie);
3952 if (status == GOT_STATUS_DELETE)
3954 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3955 staged_status, relpath);
3958 const struct got_error *
3959 got_worktree_schedule_delete(struct got_worktree *worktree,
3960 struct got_pathlist_head *paths, int delete_local_mods,
3961 const char *status_codes,
3962 got_worktree_delete_cb progress_cb, void *progress_arg,
3963 struct got_repository *repo, int keep_on_disk)
3965 struct got_fileindex *fileindex = NULL;
3966 char *fileindex_path = NULL;
3967 const struct got_error *err = NULL, *sync_err, *unlockerr;
3968 struct got_pathlist_entry *pe;
3969 struct schedule_deletion_args sda;
3971 err = lock_worktree(worktree, LOCK_EX);
3975 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3979 sda.worktree = worktree;
3980 sda.fileindex = fileindex;
3981 sda.progress_cb = progress_cb;
3982 sda.progress_arg = progress_arg;
3984 sda.delete_local_mods = delete_local_mods;
3985 sda.keep_on_disk = keep_on_disk;
3986 sda.status_codes = status_codes;
3988 TAILQ_FOREACH(pe, paths, entry) {
3989 err = worktree_status(worktree, pe->path, fileindex, repo,
3990 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3994 sync_err = sync_fileindex(fileindex, fileindex_path);
3995 if (sync_err && err == NULL)
3998 free(fileindex_path);
4000 got_fileindex_free(fileindex);
4001 unlockerr = lock_worktree(worktree, LOCK_SH);
4002 if (unlockerr && err == NULL)
4007 static const struct got_error *
4008 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4010 const struct got_error *err = NULL;
4012 size_t linesize = 0, n;
4015 linelen = getline(&line, &linesize, infile);
4016 if (linelen == -1) {
4017 if (ferror(infile)) {
4018 err = got_error_from_errno("getline");
4024 n = fwrite(line, 1, linelen, outfile);
4026 err = got_ferror(outfile, GOT_ERR_IO);
4031 n = fwrite(line, 1, linelen, rejectfile);
4033 err = got_ferror(outfile, GOT_ERR_IO);
4040 static const struct got_error *
4041 skip_one_line(FILE *f)
4044 size_t linesize = 0;
4047 linelen = getline(&line, &linesize, f);
4048 if (linelen == -1) {
4050 return got_error_from_errno("getline");
4057 static const struct got_error *
4058 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4059 int start_old, int end_old, int start_new, int end_new,
4060 FILE *outfile, FILE *rejectfile)
4062 const struct got_error *err;
4064 /* Copy old file's lines leading up to patch. */
4065 while (!feof(f1) && *line_cur1 < start_old) {
4066 err = copy_one_line(f1, outfile, NULL);
4071 /* Skip new file's lines leading up to patch. */
4072 while (!feof(f2) && *line_cur2 < start_new) {
4074 err = copy_one_line(f2, NULL, rejectfile);
4076 err = skip_one_line(f2);
4081 /* Copy patched lines. */
4082 while (!feof(f2) && *line_cur2 <= end_new) {
4083 err = copy_one_line(f2, outfile, NULL);
4088 /* Skip over old file's replaced lines. */
4089 while (!feof(f1) && *line_cur1 <= end_old) {
4091 err = copy_one_line(f1, NULL, rejectfile);
4093 err = skip_one_line(f1);
4102 static const struct got_error *
4103 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4104 FILE *outfile, FILE *rejectfile)
4106 const struct got_error *err;
4109 /* Copy old file's lines until EOF. */
4111 err = copy_one_line(f1, outfile, NULL);
4118 /* Copy new file's lines until EOF. */
4120 err = copy_one_line(f2, NULL, rejectfile);
4130 static const struct got_error *
4131 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
4132 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
4133 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
4134 int *line_cur2, FILE *outfile, FILE *rejectfile,
4135 got_worktree_patch_cb patch_cb, void *patch_arg)
4137 const struct got_error *err = NULL;
4138 int start_old = change->cv.a;
4139 int end_old = change->cv.b;
4140 int start_new = change->cv.c;
4141 int end_new = change->cv.d;
4145 *choice = GOT_PATCH_CHOICE_NONE;
4147 hunkfile = got_opentemp();
4148 if (hunkfile == NULL)
4149 return got_error_from_errno("got_opentemp");
4154 /* XXX TODO needs error checking */
4155 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
4157 if (fseek(f1, pos1, SEEK_SET) == -1) {
4158 err = got_ferror(f1, GOT_ERR_IO);
4161 if (fseek(f2, pos2, SEEK_SET) == -1) {
4162 err = got_ferror(f1, GOT_ERR_IO);
4165 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4166 err = got_ferror(hunkfile, GOT_ERR_IO);
4170 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4171 hunkfile, n, nchanges);
4176 case GOT_PATCH_CHOICE_YES:
4177 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4178 end_old, start_new, end_new, outfile, rejectfile);
4180 case GOT_PATCH_CHOICE_NO:
4181 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4182 end_old, start_new, end_new, rejectfile, outfile);
4184 case GOT_PATCH_CHOICE_QUIT:
4187 err = got_error(GOT_ERR_PATCH_CHOICE);
4191 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4192 err = got_error_from_errno("fclose");
4196 struct revert_file_args {
4197 struct got_worktree *worktree;
4198 struct got_fileindex *fileindex;
4199 got_worktree_checkout_cb progress_cb;
4201 got_worktree_patch_cb patch_cb;
4203 struct got_repository *repo;
4206 static const struct got_error *
4207 create_patched_content(char **path_outfile, int reverse_patch,
4208 struct got_object_id *blob_id, const char *path2,
4209 int dirfd2, const char *de_name2,
4210 const char *relpath, struct got_repository *repo,
4211 got_worktree_patch_cb patch_cb, void *patch_arg)
4213 const struct got_error *err;
4214 struct got_blob_object *blob = NULL;
4215 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4217 char link_target[PATH_MAX];
4218 ssize_t link_len = 0;
4219 char *path1 = NULL, *id_str = NULL;
4220 struct stat sb1, sb2;
4221 struct got_diff_changes *changes = NULL;
4222 struct got_diff_state *ds = NULL;
4223 struct got_diff_args *args = NULL;
4224 struct got_diff_change *change;
4225 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
4228 *path_outfile = NULL;
4230 err = got_object_id_str(&id_str, blob_id);
4235 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4237 if (errno != ELOOP) {
4238 err = got_error_from_errno2("openat", path2);
4241 link_len = readlinkat(dirfd2, de_name2,
4242 link_target, sizeof(link_target));
4244 return got_error_from_errno2("readlinkat", path2);
4245 sb2.st_mode = S_IFLNK;
4246 sb2.st_size = link_len;
4249 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4251 if (errno != ELOOP) {
4252 err = got_error_from_errno2("open", path2);
4255 link_len = readlink(path2, link_target,
4256 sizeof(link_target));
4258 return got_error_from_errno2("readlink", path2);
4259 sb2.st_mode = S_IFLNK;
4260 sb2.st_size = link_len;
4264 if (fstat(fd2, &sb2) == -1) {
4265 err = got_error_from_errno2("fstat", path2);
4269 f2 = fdopen(fd2, "r");
4271 err = got_error_from_errno2("fdopen", path2);
4277 f2 = got_opentemp();
4279 err = got_error_from_errno2("got_opentemp", path2);
4282 n = fwrite(link_target, 1, link_len, f2);
4283 if (n != link_len) {
4284 err = got_ferror(f2, GOT_ERR_IO);
4287 if (fflush(f2) == EOF) {
4288 err = got_error_from_errno("fflush");
4294 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4298 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4302 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4306 if (stat(path1, &sb1) == -1) {
4307 err = got_error_from_errno2("stat", path1);
4311 err = got_diff_files(&changes, &ds, &args, &diff_flags,
4312 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
4316 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4320 if (fseek(f1, 0L, SEEK_SET) == -1)
4321 return got_ferror(f1, GOT_ERR_IO);
4322 if (fseek(f2, 0L, SEEK_SET) == -1)
4323 return got_ferror(f2, GOT_ERR_IO);
4324 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
4326 err = apply_or_reject_change(&choice, change, ++n,
4327 changes->nchanges, ds, args, diff_flags, relpath,
4328 f1, f2, &line_cur1, &line_cur2,
4329 reverse_patch ? NULL : outfile,
4330 reverse_patch ? outfile : NULL,
4331 patch_cb, patch_arg);
4334 if (choice == GOT_PATCH_CHOICE_YES)
4336 else if (choice == GOT_PATCH_CHOICE_QUIT)
4340 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4341 reverse_patch ? NULL : outfile,
4342 reverse_patch ? outfile : NULL);
4346 if (!S_ISLNK(sb2.st_mode)) {
4347 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4348 err = got_error_from_errno2("fchmod", path2);
4356 got_object_blob_close(blob);
4357 if (f1 && fclose(f1) == EOF && err == NULL)
4358 err = got_error_from_errno2("fclose", path1);
4359 if (f2 && fclose(f2) == EOF && err == NULL)
4360 err = got_error_from_errno2("fclose", path2);
4361 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4362 err = got_error_from_errno2("close", path2);
4363 if (outfile && fclose(outfile) == EOF && err == NULL)
4364 err = got_error_from_errno2("fclose", *path_outfile);
4365 if (path1 && unlink(path1) == -1 && err == NULL)
4366 err = got_error_from_errno2("unlink", path1);
4367 if (err || !have_content) {
4368 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4369 err = got_error_from_errno2("unlink", *path_outfile);
4370 free(*path_outfile);
4371 *path_outfile = NULL;
4375 got_diff_state_free(ds);
4379 got_diff_free_changes(changes);
4384 static const struct got_error *
4385 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4386 const char *relpath, struct got_object_id *blob_id,
4387 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4388 int dirfd, const char *de_name)
4390 struct revert_file_args *a = arg;
4391 const struct got_error *err = NULL;
4392 char *parent_path = NULL;
4393 struct got_fileindex_entry *ie;
4394 struct got_tree_object *tree = NULL;
4395 struct got_object_id *tree_id = NULL;
4396 const struct got_tree_entry *te = NULL;
4397 char *tree_path = NULL, *te_name;
4398 char *ondisk_path = NULL, *path_content = NULL;
4399 struct got_blob_object *blob = NULL;
4401 /* Reverting a staged deletion is a no-op. */
4402 if (status == GOT_STATUS_DELETE &&
4403 staged_status != GOT_STATUS_NO_CHANGE)
4406 if (status == GOT_STATUS_UNVERSIONED)
4407 return (*a->progress_cb)(a->progress_arg,
4408 GOT_STATUS_UNVERSIONED, relpath);
4410 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4412 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4414 /* Construct in-repository path of tree which contains this blob. */
4415 err = got_path_dirname(&parent_path, ie->path);
4417 if (err->code != GOT_ERR_BAD_PATH)
4419 parent_path = strdup("/");
4420 if (parent_path == NULL) {
4421 err = got_error_from_errno("strdup");
4425 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4426 tree_path = strdup(parent_path);
4427 if (tree_path == NULL) {
4428 err = got_error_from_errno("strdup");
4432 if (got_path_is_root_dir(parent_path)) {
4433 tree_path = strdup(a->worktree->path_prefix);
4434 if (tree_path == NULL) {
4435 err = got_error_from_errno("strdup");
4439 if (asprintf(&tree_path, "%s/%s",
4440 a->worktree->path_prefix, parent_path) == -1) {
4441 err = got_error_from_errno("asprintf");
4447 err = got_object_id_by_path(&tree_id, a->repo,
4448 a->worktree->base_commit_id, tree_path);
4450 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4451 (status == GOT_STATUS_ADD ||
4452 staged_status == GOT_STATUS_ADD)))
4455 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4459 err = got_path_basename(&te_name, ie->path);
4463 te = got_object_tree_find_entry(tree, te_name);
4465 if (te == NULL && status != GOT_STATUS_ADD &&
4466 staged_status != GOT_STATUS_ADD) {
4467 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4473 case GOT_STATUS_ADD:
4475 int choice = GOT_PATCH_CHOICE_NONE;
4476 err = (*a->patch_cb)(&choice, a->patch_arg,
4477 status, ie->path, NULL, 1, 1);
4480 if (choice != GOT_PATCH_CHOICE_YES)
4483 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4487 got_fileindex_entry_remove(a->fileindex, ie);
4489 case GOT_STATUS_DELETE:
4491 int choice = GOT_PATCH_CHOICE_NONE;
4492 err = (*a->patch_cb)(&choice, a->patch_arg,
4493 status, ie->path, NULL, 1, 1);
4496 if (choice != GOT_PATCH_CHOICE_YES)
4500 case GOT_STATUS_MODIFY:
4501 case GOT_STATUS_MODE_CHANGE:
4502 case GOT_STATUS_CONFLICT:
4503 case GOT_STATUS_MISSING: {
4504 struct got_object_id id;
4505 if (staged_status == GOT_STATUS_ADD ||
4506 staged_status == GOT_STATUS_MODIFY) {
4507 memcpy(id.sha1, ie->staged_blob_sha1,
4508 SHA1_DIGEST_LENGTH);
4510 memcpy(id.sha1, ie->blob_sha1,
4511 SHA1_DIGEST_LENGTH);
4512 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4516 if (asprintf(&ondisk_path, "%s/%s",
4517 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4518 err = got_error_from_errno("asprintf");
4522 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4523 status == GOT_STATUS_CONFLICT)) {
4524 int is_bad_symlink = 0;
4525 err = create_patched_content(&path_content, 1, &id,
4526 ondisk_path, dirfd, de_name, ie->path, a->repo,
4527 a->patch_cb, a->patch_arg);
4528 if (err || path_content == NULL)
4530 if (te && S_ISLNK(te->mode)) {
4531 if (unlink(path_content) == -1) {
4532 err = got_error_from_errno2("unlink",
4536 err = install_symlink(&is_bad_symlink,
4537 a->worktree, ondisk_path, ie->path,
4538 blob, 0, 1, 0, a->repo,
4539 a->progress_cb, a->progress_arg);
4541 if (rename(path_content, ondisk_path) == -1) {
4542 err = got_error_from_errno3("rename",
4543 path_content, ondisk_path);
4548 int is_bad_symlink = 0;
4549 if (te && S_ISLNK(te->mode)) {
4550 err = install_symlink(&is_bad_symlink,
4551 a->worktree, ondisk_path, ie->path,
4552 blob, 0, 1, 0, a->repo,
4553 a->progress_cb, a->progress_arg);
4555 err = install_blob(a->worktree, ondisk_path,
4557 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4558 got_fileindex_perms_to_st(ie), blob,
4559 0, 1, 0, 0, a->repo,
4560 a->progress_cb, a->progress_arg);
4564 if (status == GOT_STATUS_DELETE ||
4565 status == GOT_STATUS_MODE_CHANGE) {
4566 err = got_fileindex_entry_update(ie,
4567 ondisk_path, blob->id.sha1,
4568 a->worktree->base_commit_id->sha1, 1);
4572 if (is_bad_symlink) {
4573 got_fileindex_entry_filetype_set(ie,
4574 GOT_FILEIDX_MODE_BAD_SYMLINK);
4588 got_object_blob_close(blob);
4590 got_object_tree_close(tree);
4595 const struct got_error *
4596 got_worktree_revert(struct got_worktree *worktree,
4597 struct got_pathlist_head *paths,
4598 got_worktree_checkout_cb progress_cb, void *progress_arg,
4599 got_worktree_patch_cb patch_cb, void *patch_arg,
4600 struct got_repository *repo)
4602 struct got_fileindex *fileindex = NULL;
4603 char *fileindex_path = NULL;
4604 const struct got_error *err = NULL, *unlockerr = NULL;
4605 const struct got_error *sync_err = NULL;
4606 struct got_pathlist_entry *pe;
4607 struct revert_file_args rfa;
4609 err = lock_worktree(worktree, LOCK_EX);
4613 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4617 rfa.worktree = worktree;
4618 rfa.fileindex = fileindex;
4619 rfa.progress_cb = progress_cb;
4620 rfa.progress_arg = progress_arg;
4621 rfa.patch_cb = patch_cb;
4622 rfa.patch_arg = patch_arg;
4624 TAILQ_FOREACH(pe, paths, entry) {
4625 err = worktree_status(worktree, pe->path, fileindex, repo,
4626 revert_file, &rfa, NULL, NULL, 0, 0);
4630 sync_err = sync_fileindex(fileindex, fileindex_path);
4631 if (sync_err && err == NULL)
4634 free(fileindex_path);
4636 got_fileindex_free(fileindex);
4637 unlockerr = lock_worktree(worktree, LOCK_SH);
4638 if (unlockerr && err == NULL)
4644 free_commitable(struct got_commitable *ct)
4647 free(ct->in_repo_path);
4648 free(ct->ondisk_path);
4650 free(ct->base_blob_id);
4651 free(ct->staged_blob_id);
4652 free(ct->base_commit_id);
4656 struct collect_commitables_arg {
4657 struct got_pathlist_head *commitable_paths;
4658 struct got_repository *repo;
4659 struct got_worktree *worktree;
4660 struct got_fileindex *fileindex;
4661 int have_staged_files;
4662 int allow_bad_symlinks;
4665 static const struct got_error *
4666 collect_commitables(void *arg, unsigned char status,
4667 unsigned char staged_status, const char *relpath,
4668 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4669 struct got_object_id *commit_id, int dirfd, const char *de_name)
4671 struct collect_commitables_arg *a = arg;
4672 const struct got_error *err = NULL;
4673 struct got_commitable *ct = NULL;
4674 struct got_pathlist_entry *new = NULL;
4675 char *parent_path = NULL, *path = NULL;
4678 if (a->have_staged_files) {
4679 if (staged_status != GOT_STATUS_MODIFY &&
4680 staged_status != GOT_STATUS_ADD &&
4681 staged_status != GOT_STATUS_DELETE)
4684 if (status == GOT_STATUS_CONFLICT)
4685 return got_error(GOT_ERR_COMMIT_CONFLICT);
4687 if (status != GOT_STATUS_MODIFY &&
4688 status != GOT_STATUS_MODE_CHANGE &&
4689 status != GOT_STATUS_ADD &&
4690 status != GOT_STATUS_DELETE)
4694 if (asprintf(&path, "/%s", relpath) == -1) {
4695 err = got_error_from_errno("asprintf");
4698 if (strcmp(path, "/") == 0) {
4699 parent_path = strdup("");
4700 if (parent_path == NULL)
4701 return got_error_from_errno("strdup");
4703 err = got_path_dirname(&parent_path, path);
4708 ct = calloc(1, sizeof(*ct));
4710 err = got_error_from_errno("calloc");
4714 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4716 err = got_error_from_errno("asprintf");
4720 if (staged_status == GOT_STATUS_ADD ||
4721 staged_status == GOT_STATUS_MODIFY) {
4722 struct got_fileindex_entry *ie;
4723 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4724 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4725 case GOT_FILEIDX_MODE_REGULAR_FILE:
4726 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4729 case GOT_FILEIDX_MODE_SYMLINK:
4733 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4736 ct->mode |= got_fileindex_entry_perms_get(ie);
4737 } else if (status != GOT_STATUS_DELETE &&
4738 staged_status != GOT_STATUS_DELETE) {
4740 if (fstatat(dirfd, de_name, &sb,
4741 AT_SYMLINK_NOFOLLOW) == -1) {
4742 err = got_error_from_errno2("fstatat",
4746 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4747 err = got_error_from_errno2("lstat", ct->ondisk_path);
4750 ct->mode = sb.st_mode;
4753 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4754 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4756 err = got_error_from_errno("asprintf");
4760 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4761 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4763 char target_path[PATH_MAX];
4765 target_len = readlink(ct->ondisk_path, target_path,
4766 sizeof(target_path));
4767 if (target_len == -1) {
4768 err = got_error_from_errno2("readlink",
4772 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4773 target_len, ct->ondisk_path, a->worktree->root_path);
4776 if (is_bad_symlink) {
4777 err = got_error_path(ct->ondisk_path,
4778 GOT_ERR_BAD_SYMLINK);
4784 ct->status = status;
4785 ct->staged_status = staged_status;
4786 ct->blob_id = NULL; /* will be filled in when blob gets created */
4787 if (ct->status != GOT_STATUS_ADD &&
4788 ct->staged_status != GOT_STATUS_ADD) {
4789 ct->base_blob_id = got_object_id_dup(blob_id);
4790 if (ct->base_blob_id == NULL) {
4791 err = got_error_from_errno("got_object_id_dup");
4794 ct->base_commit_id = got_object_id_dup(commit_id);
4795 if (ct->base_commit_id == NULL) {
4796 err = got_error_from_errno("got_object_id_dup");
4800 if (ct->staged_status == GOT_STATUS_ADD ||
4801 ct->staged_status == GOT_STATUS_MODIFY) {
4802 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4803 if (ct->staged_blob_id == NULL) {
4804 err = got_error_from_errno("got_object_id_dup");
4808 ct->path = strdup(path);
4809 if (ct->path == NULL) {
4810 err = got_error_from_errno("strdup");
4813 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4815 if (ct && (err || new == NULL))
4816 free_commitable(ct);
4822 static const struct got_error *write_tree(struct got_object_id **, int *,
4823 struct got_tree_object *, const char *, struct got_pathlist_head *,
4824 got_worktree_status_cb status_cb, void *status_arg,
4825 struct got_repository *);
4827 static const struct got_error *
4828 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4829 struct got_tree_entry *te, const char *parent_path,
4830 struct got_pathlist_head *commitable_paths,
4831 got_worktree_status_cb status_cb, void *status_arg,
4832 struct got_repository *repo)
4834 const struct got_error *err = NULL;
4835 struct got_tree_object *subtree;
4838 if (asprintf(&subpath, "%s%s%s", parent_path,
4839 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4840 return got_error_from_errno("asprintf");
4842 err = got_object_open_as_tree(&subtree, repo, &te->id);
4846 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4847 commitable_paths, status_cb, status_arg, repo);
4848 got_object_tree_close(subtree);
4853 static const struct got_error *
4854 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4856 const struct got_error *err = NULL;
4857 char *ct_parent_path = NULL;
4861 if (strchr(ct->in_repo_path, '/') == NULL) {
4862 *match = got_path_is_root_dir(path);
4866 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4869 *match = (strcmp(path, ct_parent_path) == 0);
4870 free(ct_parent_path);
4875 get_ct_file_mode(struct got_commitable *ct)
4877 if (S_ISLNK(ct->mode))
4880 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4883 static const struct got_error *
4884 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4885 struct got_tree_entry *te, struct got_commitable *ct)
4887 const struct got_error *err = NULL;
4891 err = got_object_tree_entry_dup(new_te, te);
4895 (*new_te)->mode = get_ct_file_mode(ct);
4897 if (ct->staged_status == GOT_STATUS_MODIFY)
4898 memcpy(&(*new_te)->id, ct->staged_blob_id,
4899 sizeof((*new_te)->id));
4901 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4903 if (err && *new_te) {
4910 static const struct got_error *
4911 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4912 struct got_commitable *ct)
4914 const struct got_error *err = NULL;
4915 char *ct_name = NULL;
4919 *new_te = calloc(1, sizeof(**new_te));
4920 if (*new_te == NULL)
4921 return got_error_from_errno("calloc");
4923 err = got_path_basename(&ct_name, ct->path);
4926 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4927 sizeof((*new_te)->name)) {
4928 err = got_error(GOT_ERR_NO_SPACE);
4932 (*new_te)->mode = get_ct_file_mode(ct);
4934 if (ct->staged_status == GOT_STATUS_ADD)
4935 memcpy(&(*new_te)->id, ct->staged_blob_id,
4936 sizeof((*new_te)->id));
4938 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4941 if (err && *new_te) {
4948 static const struct got_error *
4949 insert_tree_entry(struct got_tree_entry *new_te,
4950 struct got_pathlist_head *paths)
4952 const struct got_error *err = NULL;
4953 struct got_pathlist_entry *new_pe;
4955 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4959 return got_error(GOT_ERR_TREE_DUP_ENTRY);
4963 static const struct got_error *
4964 report_ct_status(struct got_commitable *ct,
4965 got_worktree_status_cb status_cb, void *status_arg)
4967 const char *ct_path = ct->path;
4968 unsigned char status;
4970 while (ct_path[0] == '/')
4973 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4974 status = ct->staged_status;
4976 status = ct->status;
4978 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4979 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4982 static const struct got_error *
4983 match_modified_subtree(int *modified, struct got_tree_entry *te,
4984 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4986 const struct got_error *err = NULL;
4987 struct got_pathlist_entry *pe;
4992 if (asprintf(&te_path, "%s%s%s", base_tree_path,
4993 got_path_is_root_dir(base_tree_path) ? "" : "/",
4995 return got_error_from_errno("asprintf");
4997 TAILQ_FOREACH(pe, commitable_paths, entry) {
4998 struct got_commitable *ct = pe->data;
4999 *modified = got_path_is_child(ct->in_repo_path, te_path,
5009 static const struct got_error *
5010 match_deleted_or_modified_ct(struct got_commitable **ctp,
5011 struct got_tree_entry *te, const char *base_tree_path,
5012 struct got_pathlist_head *commitable_paths)
5014 const struct got_error *err = NULL;
5015 struct got_pathlist_entry *pe;
5019 TAILQ_FOREACH(pe, commitable_paths, entry) {
5020 struct got_commitable *ct = pe->data;
5021 char *ct_name = NULL;
5024 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5025 if (ct->status != GOT_STATUS_MODIFY &&
5026 ct->status != GOT_STATUS_MODE_CHANGE &&
5027 ct->status != GOT_STATUS_DELETE)
5030 if (ct->staged_status != GOT_STATUS_MODIFY &&
5031 ct->staged_status != GOT_STATUS_DELETE)
5035 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5038 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5044 err = got_path_basename(&ct_name, pe->path);
5048 if (strcmp(te->name, ct_name) != 0) {
5061 static const struct got_error *
5062 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5063 const char *child_path, const char *path_base_tree,
5064 struct got_pathlist_head *commitable_paths,
5065 got_worktree_status_cb status_cb, void *status_arg,
5066 struct got_repository *repo)
5068 const struct got_error *err = NULL;
5069 struct got_tree_entry *new_te;
5071 struct got_object_id *id = NULL;
5076 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5077 got_path_is_root_dir(path_base_tree) ? "" : "/",
5079 return got_error_from_errno("asprintf");
5081 new_te = calloc(1, sizeof(*new_te));
5083 return got_error_from_errno("calloc");
5084 new_te->mode = S_IFDIR;
5086 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5087 sizeof(new_te->name)) {
5088 err = got_error(GOT_ERR_NO_SPACE);
5091 err = write_tree(&id, &nentries, NULL, subtree_path,
5092 commitable_paths, status_cb, status_arg, repo);
5097 memcpy(&new_te->id, id, sizeof(new_te->id));
5106 static const struct got_error *
5107 write_tree(struct got_object_id **new_tree_id, int *nentries,
5108 struct got_tree_object *base_tree, const char *path_base_tree,
5109 struct got_pathlist_head *commitable_paths,
5110 got_worktree_status_cb status_cb, void *status_arg,
5111 struct got_repository *repo)
5113 const struct got_error *err = NULL;
5114 struct got_pathlist_head paths;
5115 struct got_tree_entry *te, *new_te = NULL;
5116 struct got_pathlist_entry *pe;
5121 /* Insert, and recurse into, newly added entries first. */
5122 TAILQ_FOREACH(pe, commitable_paths, entry) {
5123 struct got_commitable *ct = pe->data;
5124 char *child_path = NULL, *slash;
5126 if ((ct->status != GOT_STATUS_ADD &&
5127 ct->staged_status != GOT_STATUS_ADD) ||
5128 (ct->flags & GOT_COMMITABLE_ADDED))
5131 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5132 strlen(path_base_tree)))
5135 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5140 slash = strchr(child_path, '/');
5141 if (slash == NULL) {
5142 err = alloc_added_blob_tree_entry(&new_te, ct);
5145 err = report_ct_status(ct, status_cb, status_arg);
5148 ct->flags |= GOT_COMMITABLE_ADDED;
5149 err = insert_tree_entry(new_te, &paths);
5154 *slash = '\0'; /* trim trailing path components */
5155 if (base_tree == NULL ||
5156 got_object_tree_find_entry(base_tree, child_path)
5158 err = make_subtree_for_added_blob(&new_te,
5159 child_path, path_base_tree,
5160 commitable_paths, status_cb, status_arg,
5164 err = insert_tree_entry(new_te, &paths);
5173 int i, nbase_entries;
5174 /* Handle modified and deleted entries. */
5175 nbase_entries = got_object_tree_get_nentries(base_tree);
5176 for (i = 0; i < nbase_entries; i++) {
5177 struct got_commitable *ct = NULL;
5179 te = got_object_tree_get_entry(base_tree, i);
5180 if (got_object_tree_entry_is_submodule(te)) {
5181 /* Entry is a submodule; just copy it. */
5182 err = got_object_tree_entry_dup(&new_te, te);
5185 err = insert_tree_entry(new_te, &paths);
5192 if (S_ISDIR(te->mode)) {
5194 err = got_object_tree_entry_dup(&new_te, te);
5197 err = match_modified_subtree(&modified, te,
5198 path_base_tree, commitable_paths);
5201 /* Avoid recursion into unmodified subtrees. */
5203 struct got_object_id *new_id;
5205 err = write_subtree(&new_id,
5207 path_base_tree, commitable_paths,
5208 status_cb, status_arg, repo);
5211 if (nsubentries == 0) {
5212 /* All entries were deleted. */
5216 memcpy(&new_te->id, new_id,
5217 sizeof(new_te->id));
5220 err = insert_tree_entry(new_te, &paths);
5227 err = match_deleted_or_modified_ct(&ct, te,
5228 path_base_tree, commitable_paths);
5232 /* NB: Deleted entries get dropped here. */
5233 if (ct->status == GOT_STATUS_MODIFY ||
5234 ct->status == GOT_STATUS_MODE_CHANGE ||
5235 ct->staged_status == GOT_STATUS_MODIFY) {
5236 err = alloc_modified_blob_tree_entry(
5240 err = insert_tree_entry(new_te, &paths);
5245 err = report_ct_status(ct, status_cb,
5250 /* Entry is unchanged; just copy it. */
5251 err = got_object_tree_entry_dup(&new_te, te);
5254 err = insert_tree_entry(new_te, &paths);
5262 /* Write new list of entries; deleted entries have been dropped. */
5263 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5265 got_pathlist_free(&paths);
5269 static const struct got_error *
5270 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
5271 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
5272 int have_staged_files)
5274 const struct got_error *err = NULL;
5275 struct got_pathlist_entry *pe;
5277 TAILQ_FOREACH(pe, commitable_paths, entry) {
5278 struct got_fileindex_entry *ie;
5279 struct got_commitable *ct = pe->data;
5281 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5283 if (ct->status == GOT_STATUS_DELETE ||
5284 ct->staged_status == GOT_STATUS_DELETE) {
5285 got_fileindex_entry_remove(fileindex, ie);
5286 } else if (ct->staged_status == GOT_STATUS_ADD ||
5287 ct->staged_status == GOT_STATUS_MODIFY) {
5288 got_fileindex_entry_stage_set(ie,
5289 GOT_FILEIDX_STAGE_NONE);
5290 err = got_fileindex_entry_update(ie,
5291 ct->ondisk_path, ct->staged_blob_id->sha1,
5292 new_base_commit_id->sha1,
5293 !have_staged_files);
5295 err = got_fileindex_entry_update(ie,
5296 ct->ondisk_path, ct->blob_id->sha1,
5297 new_base_commit_id->sha1,
5298 !have_staged_files);
5300 err = got_fileindex_entry_alloc(&ie, pe->path);
5303 err = got_fileindex_entry_update(ie, ct->ondisk_path,
5304 ct->blob_id->sha1, new_base_commit_id->sha1, 1);
5306 got_fileindex_entry_free(ie);
5309 err = got_fileindex_entry_add(fileindex, ie);
5311 got_fileindex_entry_free(ie);
5320 static const struct got_error *
5321 check_out_of_date(const char *in_repo_path, unsigned char status,
5322 unsigned char staged_status, struct got_object_id *base_blob_id,
5323 struct got_object_id *base_commit_id,
5324 struct got_object_id *head_commit_id, struct got_repository *repo,
5327 const struct got_error *err = NULL;
5328 struct got_object_id *id = NULL;
5330 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5331 /* Trivial case: base commit == head commit */
5332 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5335 * Ensure file content which local changes were based
5336 * on matches file content in the branch head.
5338 err = got_object_id_by_path(&id, repo, head_commit_id,
5341 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5342 err = got_error(ood_errcode);
5344 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5345 err = got_error(ood_errcode);
5347 /* Require that added files don't exist in the branch head. */
5348 err = got_object_id_by_path(&id, repo, head_commit_id,
5350 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5352 err = id ? got_error(ood_errcode) : NULL;
5359 const struct got_error *
5360 commit_worktree(struct got_object_id **new_commit_id,
5361 struct got_pathlist_head *commitable_paths,
5362 struct got_object_id *head_commit_id, struct got_worktree *worktree,
5363 const char *author, const char *committer,
5364 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5365 got_worktree_status_cb status_cb, void *status_arg,
5366 struct got_repository *repo)
5368 const struct got_error *err = NULL, *unlockerr = NULL;
5369 struct got_pathlist_entry *pe;
5370 const char *head_ref_name = NULL;
5371 struct got_commit_object *head_commit = NULL;
5372 struct got_reference *head_ref2 = NULL;
5373 struct got_object_id *head_commit_id2 = NULL;
5374 struct got_tree_object *head_tree = NULL;
5375 struct got_object_id *new_tree_id = NULL;
5377 struct got_object_id_queue parent_ids;
5378 struct got_object_qid *pid = NULL;
5379 char *logmsg = NULL;
5381 *new_commit_id = NULL;
5383 SIMPLEQ_INIT(&parent_ids);
5385 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5389 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5393 if (commit_msg_cb != NULL) {
5394 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5399 if (logmsg == NULL || strlen(logmsg) == 0) {
5400 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5404 /* Create blobs from added and modified files and record their IDs. */
5405 TAILQ_FOREACH(pe, commitable_paths, entry) {
5406 struct got_commitable *ct = pe->data;
5409 /* Blobs for staged files already exist. */
5410 if (ct->staged_status == GOT_STATUS_ADD ||
5411 ct->staged_status == GOT_STATUS_MODIFY)
5414 if (ct->status != GOT_STATUS_ADD &&
5415 ct->status != GOT_STATUS_MODIFY &&
5416 ct->status != GOT_STATUS_MODE_CHANGE)
5419 if (asprintf(&ondisk_path, "%s/%s",
5420 worktree->root_path, pe->path) == -1) {
5421 err = got_error_from_errno("asprintf");
5424 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5430 /* Recursively write new tree objects. */
5431 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5432 commitable_paths, status_cb, status_arg, repo);
5436 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5439 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5440 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5441 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5442 got_object_qid_free(pid);
5448 /* Check if a concurrent commit to our branch has occurred. */
5449 head_ref_name = got_worktree_get_head_ref_name(worktree);
5450 if (head_ref_name == NULL) {
5451 err = got_error_from_errno("got_worktree_get_head_ref_name");
5454 /* Lock the reference here to prevent concurrent modification. */
5455 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5458 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5461 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5462 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5465 /* Update branch head in repository. */
5466 err = got_ref_change_ref(head_ref2, *new_commit_id);
5469 err = got_ref_write(head_ref2, repo);
5473 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5477 err = ref_base_commit(worktree, repo);
5482 got_object_tree_close(head_tree);
5484 got_object_commit_close(head_commit);
5485 free(head_commit_id2);
5487 unlockerr = got_ref_unlock(head_ref2);
5488 if (unlockerr && err == NULL)
5490 got_ref_close(head_ref2);
5495 static const struct got_error *
5496 check_path_is_commitable(const char *path,
5497 struct got_pathlist_head *commitable_paths)
5499 struct got_pathlist_entry *cpe = NULL;
5500 size_t path_len = strlen(path);
5502 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5503 struct got_commitable *ct = cpe->data;
5504 const char *ct_path = ct->path;
5506 while (ct_path[0] == '/')
5509 if (strcmp(path, ct_path) == 0 ||
5510 got_path_is_child(ct_path, path, path_len))
5515 return got_error_path(path, GOT_ERR_BAD_PATH);
5520 static const struct got_error *
5521 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5523 int *have_staged_files = arg;
5525 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5526 *have_staged_files = 1;
5527 return got_error(GOT_ERR_CANCELLED);
5533 static const struct got_error *
5534 check_non_staged_files(struct got_fileindex *fileindex,
5535 struct got_pathlist_head *paths)
5537 struct got_pathlist_entry *pe;
5538 struct got_fileindex_entry *ie;
5540 TAILQ_FOREACH(pe, paths, entry) {
5541 if (pe->path[0] == '\0')
5543 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5545 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5546 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5547 return got_error_path(pe->path,
5548 GOT_ERR_FILE_NOT_STAGED);
5554 const struct got_error *
5555 got_worktree_commit(struct got_object_id **new_commit_id,
5556 struct got_worktree *worktree, struct got_pathlist_head *paths,
5557 const char *author, const char *committer, int allow_bad_symlinks,
5558 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5559 got_worktree_status_cb status_cb, void *status_arg,
5560 struct got_repository *repo)
5562 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5563 struct got_fileindex *fileindex = NULL;
5564 char *fileindex_path = NULL;
5565 struct got_pathlist_head commitable_paths;
5566 struct collect_commitables_arg cc_arg;
5567 struct got_pathlist_entry *pe;
5568 struct got_reference *head_ref = NULL;
5569 struct got_object_id *head_commit_id = NULL;
5570 int have_staged_files = 0;
5572 *new_commit_id = NULL;
5574 TAILQ_INIT(&commitable_paths);
5576 err = lock_worktree(worktree, LOCK_EX);
5580 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5584 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5588 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5592 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5593 &have_staged_files);
5594 if (err && err->code != GOT_ERR_CANCELLED)
5596 if (have_staged_files) {
5597 err = check_non_staged_files(fileindex, paths);
5602 cc_arg.commitable_paths = &commitable_paths;
5603 cc_arg.worktree = worktree;
5604 cc_arg.fileindex = fileindex;
5606 cc_arg.have_staged_files = have_staged_files;
5607 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5608 TAILQ_FOREACH(pe, paths, entry) {
5609 err = worktree_status(worktree, pe->path, fileindex, repo,
5610 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5615 if (TAILQ_EMPTY(&commitable_paths)) {
5616 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5620 TAILQ_FOREACH(pe, paths, entry) {
5621 err = check_path_is_commitable(pe->path, &commitable_paths);
5626 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5627 struct got_commitable *ct = pe->data;
5628 const char *ct_path = ct->in_repo_path;
5630 while (ct_path[0] == '/')
5632 err = check_out_of_date(ct_path, ct->status,
5633 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5634 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5640 err = commit_worktree(new_commit_id, &commitable_paths,
5641 head_commit_id, worktree, author, committer,
5642 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5646 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5647 fileindex, have_staged_files);
5648 sync_err = sync_fileindex(fileindex, fileindex_path);
5649 if (sync_err && err == NULL)
5653 got_fileindex_free(fileindex);
5654 free(fileindex_path);
5655 unlockerr = lock_worktree(worktree, LOCK_SH);
5656 if (unlockerr && err == NULL)
5658 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5659 struct got_commitable *ct = pe->data;
5660 free_commitable(ct);
5662 got_pathlist_free(&commitable_paths);
5667 got_commitable_get_path(struct got_commitable *ct)
5673 got_commitable_get_status(struct got_commitable *ct)
5678 struct check_rebase_ok_arg {
5679 struct got_worktree *worktree;
5680 struct got_repository *repo;
5683 static const struct got_error *
5684 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5686 const struct got_error *err = NULL;
5687 struct check_rebase_ok_arg *a = arg;
5688 unsigned char status;
5692 /* Reject rebase of a work tree with mixed base commits. */
5693 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5694 SHA1_DIGEST_LENGTH))
5695 return got_error(GOT_ERR_MIXED_COMMITS);
5697 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5699 return got_error_from_errno("asprintf");
5701 /* Reject rebase of a work tree with modified or staged files. */
5702 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5707 if (status != GOT_STATUS_NO_CHANGE)
5708 return got_error(GOT_ERR_MODIFIED);
5709 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5710 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5715 const struct got_error *
5716 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5717 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5718 struct got_worktree *worktree, struct got_reference *branch,
5719 struct got_repository *repo)
5721 const struct got_error *err = NULL;
5722 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5723 char *branch_ref_name = NULL;
5724 char *fileindex_path = NULL;
5725 struct check_rebase_ok_arg ok_arg;
5726 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5727 struct got_object_id *wt_branch_tip = NULL;
5729 *new_base_branch_ref = NULL;
5733 err = lock_worktree(worktree, LOCK_EX);
5737 err = open_fileindex(fileindex, &fileindex_path, worktree);
5741 ok_arg.worktree = worktree;
5743 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5748 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5752 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5756 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5760 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5765 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5768 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5769 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5773 err = got_ref_alloc_symref(new_base_branch_ref,
5774 new_base_branch_ref_name, wt_branch);
5777 err = got_ref_write(*new_base_branch_ref, repo);
5781 /* TODO Lock original branch's ref while rebasing? */
5783 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5787 err = got_ref_write(branch_ref, repo);
5791 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5792 worktree->base_commit_id);
5795 err = got_ref_write(*tmp_branch, repo);
5799 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5803 free(fileindex_path);
5804 free(tmp_branch_name);
5805 free(new_base_branch_ref_name);
5806 free(branch_ref_name);
5808 got_ref_close(branch_ref);
5810 got_ref_close(wt_branch);
5811 free(wt_branch_tip);
5813 if (*new_base_branch_ref) {
5814 got_ref_close(*new_base_branch_ref);
5815 *new_base_branch_ref = NULL;
5818 got_ref_close(*tmp_branch);
5822 got_fileindex_free(*fileindex);
5825 lock_worktree(worktree, LOCK_SH);
5830 const struct got_error *
5831 got_worktree_rebase_continue(struct got_object_id **commit_id,
5832 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5833 struct got_reference **branch, struct got_fileindex **fileindex,
5834 struct got_worktree *worktree, struct got_repository *repo)
5836 const struct got_error *err;
5837 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5838 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5839 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5840 char *fileindex_path = NULL;
5841 int have_staged_files = 0;
5844 *new_base_branch = NULL;
5849 err = lock_worktree(worktree, LOCK_EX);
5853 err = open_fileindex(fileindex, &fileindex_path, worktree);
5857 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5858 &have_staged_files);
5859 if (err && err->code != GOT_ERR_CANCELLED)
5861 if (have_staged_files) {
5862 err = got_error(GOT_ERR_STAGED_PATHS);
5866 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5870 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5874 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5878 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5882 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5886 err = got_ref_open(branch, repo,
5887 got_ref_get_symref_target(branch_ref), 0);
5891 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5895 err = got_ref_resolve(commit_id, repo, commit_ref);
5899 err = got_ref_open(new_base_branch, repo,
5900 new_base_branch_ref_name, 0);
5904 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5908 free(commit_ref_name);
5909 free(branch_ref_name);
5910 free(fileindex_path);
5912 got_ref_close(commit_ref);
5914 got_ref_close(branch_ref);
5919 got_ref_close(*tmp_branch);
5922 if (*new_base_branch) {
5923 got_ref_close(*new_base_branch);
5924 *new_base_branch = NULL;
5927 got_ref_close(*branch);
5931 got_fileindex_free(*fileindex);
5934 lock_worktree(worktree, LOCK_SH);
5939 const struct got_error *
5940 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5942 const struct got_error *err;
5943 char *tmp_branch_name = NULL;
5945 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5949 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5950 free(tmp_branch_name);
5954 static const struct got_error *
5955 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5956 char **logmsg, void *arg)
5962 static const struct got_error *
5963 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5964 const char *path, struct got_object_id *blob_id,
5965 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5966 int dirfd, const char *de_name)
5971 struct collect_merged_paths_arg {
5972 got_worktree_checkout_cb progress_cb;
5974 struct got_pathlist_head *merged_paths;
5977 static const struct got_error *
5978 collect_merged_paths(void *arg, unsigned char status, const char *path)
5980 const struct got_error *err;
5981 struct collect_merged_paths_arg *a = arg;
5983 struct got_pathlist_entry *new;
5985 err = (*a->progress_cb)(a->progress_arg, status, path);
5989 if (status != GOT_STATUS_MERGE &&
5990 status != GOT_STATUS_ADD &&
5991 status != GOT_STATUS_DELETE &&
5992 status != GOT_STATUS_CONFLICT)
5997 return got_error_from_errno("strdup");
5999 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6000 if (err || new == NULL)
6006 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6008 struct got_pathlist_entry *pe;
6010 TAILQ_FOREACH(pe, merged_paths, entry)
6011 free((char *)pe->path);
6013 got_pathlist_free(merged_paths);
6016 static const struct got_error *
6017 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6018 int is_rebase, struct got_repository *repo)
6020 const struct got_error *err;
6021 struct got_reference *commit_ref = NULL;
6023 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6025 if (err->code != GOT_ERR_NOT_REF)
6027 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6030 err = got_ref_write(commit_ref, repo);
6033 } else if (is_rebase) {
6034 struct got_object_id *stored_id;
6037 err = got_ref_resolve(&stored_id, repo, commit_ref);
6040 cmp = got_object_id_cmp(commit_id, stored_id);
6043 err = got_error(GOT_ERR_REBASE_COMMITID);
6049 got_ref_close(commit_ref);
6053 static const struct got_error *
6054 rebase_merge_files(struct got_pathlist_head *merged_paths,
6055 const char *commit_ref_name, struct got_worktree *worktree,
6056 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6057 struct got_object_id *commit_id, struct got_repository *repo,
6058 got_worktree_checkout_cb progress_cb, void *progress_arg,
6059 got_cancel_cb cancel_cb, void *cancel_arg)
6061 const struct got_error *err;
6062 struct got_reference *commit_ref = NULL;
6063 struct collect_merged_paths_arg cmp_arg;
6064 char *fileindex_path;
6066 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6068 err = get_fileindex_path(&fileindex_path, worktree);
6072 cmp_arg.progress_cb = progress_cb;
6073 cmp_arg.progress_arg = progress_arg;
6074 cmp_arg.merged_paths = merged_paths;
6075 err = merge_files(worktree, fileindex, fileindex_path,
6076 parent_commit_id, commit_id, repo, collect_merged_paths,
6077 &cmp_arg, cancel_cb, cancel_arg);
6079 got_ref_close(commit_ref);
6083 const struct got_error *
6084 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6085 struct got_worktree *worktree, struct got_fileindex *fileindex,
6086 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6087 struct got_repository *repo,
6088 got_worktree_checkout_cb progress_cb, void *progress_arg,
6089 got_cancel_cb cancel_cb, void *cancel_arg)
6091 const struct got_error *err;
6092 char *commit_ref_name;
6094 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6098 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6102 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6103 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6104 progress_arg, cancel_cb, cancel_arg);
6106 free(commit_ref_name);
6110 const struct got_error *
6111 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6112 struct got_worktree *worktree, struct got_fileindex *fileindex,
6113 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6114 struct got_repository *repo,
6115 got_worktree_checkout_cb progress_cb, void *progress_arg,
6116 got_cancel_cb cancel_cb, void *cancel_arg)
6118 const struct got_error *err;
6119 char *commit_ref_name;
6121 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6125 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6129 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6130 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6131 progress_arg, cancel_cb, cancel_arg);
6133 free(commit_ref_name);
6137 static const struct got_error *
6138 rebase_commit(struct got_object_id **new_commit_id,
6139 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6140 struct got_worktree *worktree, struct got_fileindex *fileindex,
6141 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6142 const char *new_logmsg, struct got_repository *repo)
6144 const struct got_error *err, *sync_err;
6145 struct got_pathlist_head commitable_paths;
6146 struct collect_commitables_arg cc_arg;
6147 char *fileindex_path = NULL;
6148 struct got_reference *head_ref = NULL;
6149 struct got_object_id *head_commit_id = NULL;
6150 char *logmsg = NULL;
6152 TAILQ_INIT(&commitable_paths);
6153 *new_commit_id = NULL;
6155 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6157 err = get_fileindex_path(&fileindex_path, worktree);
6161 cc_arg.commitable_paths = &commitable_paths;
6162 cc_arg.worktree = worktree;
6164 cc_arg.have_staged_files = 0;
6166 * If possible get the status of individual files directly to
6167 * avoid crawling the entire work tree once per rebased commit.
6168 * TODO: Ideally, merged_paths would contain a list of commitables
6169 * we could use so we could skip worktree_status() entirely.
6172 struct got_pathlist_entry *pe;
6173 TAILQ_FOREACH(pe, merged_paths, entry) {
6174 err = worktree_status(worktree, pe->path, fileindex,
6175 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6181 err = worktree_status(worktree, "", fileindex, repo,
6182 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6187 if (TAILQ_EMPTY(&commitable_paths)) {
6188 /* No-op change; commit will be elided. */
6189 err = got_ref_delete(commit_ref, repo);
6192 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6196 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6200 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6205 logmsg = strdup(new_logmsg);
6206 if (logmsg == NULL) {
6207 err = got_error_from_errno("strdup");
6211 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6216 /* NB: commit_worktree will call free(logmsg) */
6217 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6218 worktree, got_object_commit_get_author(orig_commit),
6219 got_object_commit_get_committer(orig_commit),
6220 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6224 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6228 err = got_ref_delete(commit_ref, repo);
6232 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
6234 sync_err = sync_fileindex(fileindex, fileindex_path);
6235 if (sync_err && err == NULL)
6238 free(fileindex_path);
6239 free(head_commit_id);
6241 got_ref_close(head_ref);
6243 free(*new_commit_id);
6244 *new_commit_id = NULL;
6249 const struct got_error *
6250 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6251 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6252 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6253 struct got_commit_object *orig_commit,
6254 struct got_object_id *orig_commit_id, struct got_repository *repo)
6256 const struct got_error *err;
6257 char *commit_ref_name;
6258 struct got_reference *commit_ref = NULL;
6259 struct got_object_id *commit_id = NULL;
6261 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6265 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6268 err = got_ref_resolve(&commit_id, repo, commit_ref);
6271 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6272 err = got_error(GOT_ERR_REBASE_COMMITID);
6276 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6277 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6280 got_ref_close(commit_ref);
6281 free(commit_ref_name);
6286 const struct got_error *
6287 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6288 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6289 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6290 struct got_commit_object *orig_commit,
6291 struct got_object_id *orig_commit_id, const char *new_logmsg,
6292 struct got_repository *repo)
6294 const struct got_error *err;
6295 char *commit_ref_name;
6296 struct got_reference *commit_ref = NULL;
6298 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6302 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6306 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6307 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6310 got_ref_close(commit_ref);
6311 free(commit_ref_name);
6315 const struct got_error *
6316 got_worktree_rebase_postpone(struct got_worktree *worktree,
6317 struct got_fileindex *fileindex)
6320 got_fileindex_free(fileindex);
6321 return lock_worktree(worktree, LOCK_SH);
6324 static const struct got_error *
6325 delete_ref(const char *name, struct got_repository *repo)
6327 const struct got_error *err;
6328 struct got_reference *ref;
6330 err = got_ref_open(&ref, repo, name, 0);
6332 if (err->code == GOT_ERR_NOT_REF)
6337 err = got_ref_delete(ref, repo);
6342 static const struct got_error *
6343 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6345 const struct got_error *err;
6346 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6347 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6349 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6352 err = delete_ref(tmp_branch_name, repo);
6356 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6359 err = delete_ref(new_base_branch_ref_name, repo);
6363 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6366 err = delete_ref(branch_ref_name, repo);
6370 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6373 err = delete_ref(commit_ref_name, repo);
6378 free(tmp_branch_name);
6379 free(new_base_branch_ref_name);
6380 free(branch_ref_name);
6381 free(commit_ref_name);
6385 const struct got_error *
6386 got_worktree_rebase_complete(struct got_worktree *worktree,
6387 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6388 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6389 struct got_repository *repo)
6391 const struct got_error *err, *unlockerr;
6392 struct got_object_id *new_head_commit_id = NULL;
6394 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6398 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6402 err = got_ref_write(rebased_branch, repo);
6406 err = got_worktree_set_head_ref(worktree, rebased_branch);
6410 err = delete_rebase_refs(worktree, repo);
6413 got_fileindex_free(fileindex);
6414 free(new_head_commit_id);
6415 unlockerr = lock_worktree(worktree, LOCK_SH);
6416 if (unlockerr && err == NULL)
6421 const struct got_error *
6422 got_worktree_rebase_abort(struct got_worktree *worktree,
6423 struct got_fileindex *fileindex, struct got_repository *repo,
6424 struct got_reference *new_base_branch,
6425 got_worktree_checkout_cb progress_cb, void *progress_arg)
6427 const struct got_error *err, *unlockerr, *sync_err;
6428 struct got_reference *resolved = NULL;
6429 struct got_object_id *commit_id = NULL;
6430 char *fileindex_path = NULL;
6431 struct revert_file_args rfa;
6432 struct got_object_id *tree_id = NULL;
6434 err = lock_worktree(worktree, LOCK_EX);
6438 err = got_ref_open(&resolved, repo,
6439 got_ref_get_symref_target(new_base_branch), 0);
6443 err = got_worktree_set_head_ref(worktree, resolved);
6448 * XXX commits to the base branch could have happened while
6449 * we were busy rebasing; should we store the original commit ID
6450 * when rebase begins and read it back here?
6452 err = got_ref_resolve(&commit_id, repo, resolved);
6456 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6460 err = got_object_id_by_path(&tree_id, repo,
6461 worktree->base_commit_id, worktree->path_prefix);
6465 err = delete_rebase_refs(worktree, repo);
6469 err = get_fileindex_path(&fileindex_path, worktree);
6473 rfa.worktree = worktree;
6474 rfa.fileindex = fileindex;
6475 rfa.progress_cb = progress_cb;
6476 rfa.progress_arg = progress_arg;
6477 rfa.patch_cb = NULL;
6478 rfa.patch_arg = NULL;
6480 err = worktree_status(worktree, "", fileindex, repo,
6481 revert_file, &rfa, NULL, NULL, 0, 0);
6485 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6486 repo, progress_cb, progress_arg, NULL, NULL);
6488 sync_err = sync_fileindex(fileindex, fileindex_path);
6489 if (sync_err && err == NULL)
6492 got_ref_close(resolved);
6496 got_fileindex_free(fileindex);
6497 free(fileindex_path);
6499 unlockerr = lock_worktree(worktree, LOCK_SH);
6500 if (unlockerr && err == NULL)
6505 const struct got_error *
6506 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6507 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6508 struct got_fileindex **fileindex, struct got_worktree *worktree,
6509 struct got_repository *repo)
6511 const struct got_error *err = NULL;
6512 char *tmp_branch_name = NULL;
6513 char *branch_ref_name = NULL;
6514 char *base_commit_ref_name = NULL;
6515 char *fileindex_path = NULL;
6516 struct check_rebase_ok_arg ok_arg;
6517 struct got_reference *wt_branch = NULL;
6518 struct got_reference *base_commit_ref = NULL;
6522 *base_commit_id = NULL;
6525 err = lock_worktree(worktree, LOCK_EX);
6529 err = open_fileindex(fileindex, &fileindex_path, worktree);
6533 ok_arg.worktree = worktree;
6535 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6540 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6544 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6548 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6553 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6558 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6562 err = got_ref_write(*branch_ref, repo);
6566 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6567 worktree->base_commit_id);
6570 err = got_ref_write(base_commit_ref, repo);
6573 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6574 if (*base_commit_id == NULL) {
6575 err = got_error_from_errno("got_object_id_dup");
6579 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6580 worktree->base_commit_id);
6583 err = got_ref_write(*tmp_branch, repo);
6587 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6591 free(fileindex_path);
6592 free(tmp_branch_name);
6593 free(branch_ref_name);
6594 free(base_commit_ref_name);
6596 got_ref_close(wt_branch);
6599 got_ref_close(*branch_ref);
6603 got_ref_close(*tmp_branch);
6606 free(*base_commit_id);
6608 got_fileindex_free(*fileindex);
6611 lock_worktree(worktree, LOCK_SH);
6616 const struct got_error *
6617 got_worktree_histedit_postpone(struct got_worktree *worktree,
6618 struct got_fileindex *fileindex)
6621 got_fileindex_free(fileindex);
6622 return lock_worktree(worktree, LOCK_SH);
6625 const struct got_error *
6626 got_worktree_histedit_in_progress(int *in_progress,
6627 struct got_worktree *worktree)
6629 const struct got_error *err;
6630 char *tmp_branch_name = NULL;
6632 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6636 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6637 free(tmp_branch_name);
6641 const struct got_error *
6642 got_worktree_histedit_continue(struct got_object_id **commit_id,
6643 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6644 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6645 struct got_worktree *worktree, struct got_repository *repo)
6647 const struct got_error *err;
6648 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6649 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6650 struct got_reference *commit_ref = NULL;
6651 struct got_reference *base_commit_ref = NULL;
6652 char *fileindex_path = NULL;
6653 int have_staged_files = 0;
6657 *base_commit_id = NULL;
6660 err = lock_worktree(worktree, LOCK_EX);
6664 err = open_fileindex(fileindex, &fileindex_path, worktree);
6668 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6669 &have_staged_files);
6670 if (err && err->code != GOT_ERR_CANCELLED)
6672 if (have_staged_files) {
6673 err = got_error(GOT_ERR_STAGED_PATHS);
6677 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6681 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6685 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6689 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6694 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6698 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6701 err = got_ref_resolve(commit_id, repo, commit_ref);
6705 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6708 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6712 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6716 free(commit_ref_name);
6717 free(branch_ref_name);
6718 free(fileindex_path);
6720 got_ref_close(commit_ref);
6721 if (base_commit_ref)
6722 got_ref_close(base_commit_ref);
6726 free(*base_commit_id);
6727 *base_commit_id = NULL;
6729 got_ref_close(*tmp_branch);
6733 got_fileindex_free(*fileindex);
6736 lock_worktree(worktree, LOCK_EX);
6741 static const struct got_error *
6742 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6744 const struct got_error *err;
6745 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6746 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6748 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6751 err = delete_ref(tmp_branch_name, repo);
6755 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6759 err = delete_ref(base_commit_ref_name, repo);
6763 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6766 err = delete_ref(branch_ref_name, repo);
6770 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6773 err = delete_ref(commit_ref_name, repo);
6777 free(tmp_branch_name);
6778 free(base_commit_ref_name);
6779 free(branch_ref_name);
6780 free(commit_ref_name);
6784 const struct got_error *
6785 got_worktree_histedit_abort(struct got_worktree *worktree,
6786 struct got_fileindex *fileindex, struct got_repository *repo,
6787 struct got_reference *branch, struct got_object_id *base_commit_id,
6788 got_worktree_checkout_cb progress_cb, void *progress_arg)
6790 const struct got_error *err, *unlockerr, *sync_err;
6791 struct got_reference *resolved = NULL;
6792 char *fileindex_path = NULL;
6793 struct got_object_id *tree_id = NULL;
6794 struct revert_file_args rfa;
6796 err = lock_worktree(worktree, LOCK_EX);
6800 err = got_ref_open(&resolved, repo,
6801 got_ref_get_symref_target(branch), 0);
6805 err = got_worktree_set_head_ref(worktree, resolved);
6809 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6813 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6814 worktree->path_prefix);
6818 err = delete_histedit_refs(worktree, repo);
6822 err = get_fileindex_path(&fileindex_path, worktree);
6826 rfa.worktree = worktree;
6827 rfa.fileindex = fileindex;
6828 rfa.progress_cb = progress_cb;
6829 rfa.progress_arg = progress_arg;
6830 rfa.patch_cb = NULL;
6831 rfa.patch_arg = NULL;
6833 err = worktree_status(worktree, "", fileindex, repo,
6834 revert_file, &rfa, NULL, NULL, 0, 0);
6838 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6839 repo, progress_cb, progress_arg, NULL, NULL);
6841 sync_err = sync_fileindex(fileindex, fileindex_path);
6842 if (sync_err && err == NULL)
6845 got_ref_close(resolved);
6847 free(fileindex_path);
6849 unlockerr = lock_worktree(worktree, LOCK_SH);
6850 if (unlockerr && err == NULL)
6855 const struct got_error *
6856 got_worktree_histedit_complete(struct got_worktree *worktree,
6857 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6858 struct got_reference *edited_branch, struct got_repository *repo)
6860 const struct got_error *err, *unlockerr;
6861 struct got_object_id *new_head_commit_id = NULL;
6862 struct got_reference *resolved = NULL;
6864 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6868 err = got_ref_open(&resolved, repo,
6869 got_ref_get_symref_target(edited_branch), 0);
6873 err = got_ref_change_ref(resolved, new_head_commit_id);
6877 err = got_ref_write(resolved, repo);
6881 err = got_worktree_set_head_ref(worktree, resolved);
6885 err = delete_histedit_refs(worktree, repo);
6888 got_fileindex_free(fileindex);
6889 free(new_head_commit_id);
6890 unlockerr = lock_worktree(worktree, LOCK_SH);
6891 if (unlockerr && err == NULL)
6896 const struct got_error *
6897 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6898 struct got_object_id *commit_id, struct got_repository *repo)
6900 const struct got_error *err;
6901 char *commit_ref_name;
6903 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6907 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6911 err = delete_ref(commit_ref_name, repo);
6913 free(commit_ref_name);
6917 const struct got_error *
6918 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6919 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6920 struct got_worktree *worktree, const char *refname,
6921 struct got_repository *repo)
6923 const struct got_error *err = NULL;
6924 char *fileindex_path = NULL;
6925 struct check_rebase_ok_arg ok_arg;
6929 *base_branch_ref = NULL;
6931 err = lock_worktree(worktree, LOCK_EX);
6935 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6936 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6937 "cannot integrate a branch into itself; "
6938 "update -b or different branch name required");
6942 err = open_fileindex(fileindex, &fileindex_path, worktree);
6946 /* Preconditions are the same as for rebase. */
6947 ok_arg.worktree = worktree;
6949 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6954 err = got_ref_open(branch_ref, repo, refname, 1);
6958 err = got_ref_open(base_branch_ref, repo,
6959 got_worktree_get_head_ref_name(worktree), 1);
6963 got_ref_close(*branch_ref);
6966 if (*base_branch_ref) {
6967 got_ref_close(*base_branch_ref);
6968 *base_branch_ref = NULL;
6971 got_fileindex_free(*fileindex);
6974 lock_worktree(worktree, LOCK_SH);
6979 const struct got_error *
6980 got_worktree_integrate_continue(struct got_worktree *worktree,
6981 struct got_fileindex *fileindex, struct got_repository *repo,
6982 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6983 got_worktree_checkout_cb progress_cb, void *progress_arg,
6984 got_cancel_cb cancel_cb, void *cancel_arg)
6986 const struct got_error *err = NULL, *sync_err, *unlockerr;
6987 char *fileindex_path = NULL;
6988 struct got_object_id *tree_id = NULL, *commit_id = NULL;
6990 err = get_fileindex_path(&fileindex_path, worktree);
6994 err = got_ref_resolve(&commit_id, repo, branch_ref);
6998 err = got_object_id_by_path(&tree_id, repo, commit_id,
6999 worktree->path_prefix);
7003 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7007 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7008 progress_cb, progress_arg, cancel_cb, cancel_arg);
7012 err = got_ref_change_ref(base_branch_ref, commit_id);
7016 err = got_ref_write(base_branch_ref, repo);
7018 sync_err = sync_fileindex(fileindex, fileindex_path);
7019 if (sync_err && err == NULL)
7023 unlockerr = got_ref_unlock(branch_ref);
7024 if (unlockerr && err == NULL)
7026 got_ref_close(branch_ref);
7028 unlockerr = got_ref_unlock(base_branch_ref);
7029 if (unlockerr && err == NULL)
7031 got_ref_close(base_branch_ref);
7033 got_fileindex_free(fileindex);
7034 free(fileindex_path);
7037 unlockerr = lock_worktree(worktree, LOCK_SH);
7038 if (unlockerr && err == NULL)
7043 const struct got_error *
7044 got_worktree_integrate_abort(struct got_worktree *worktree,
7045 struct got_fileindex *fileindex, struct got_repository *repo,
7046 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7048 const struct got_error *err = NULL, *unlockerr = NULL;
7050 got_fileindex_free(fileindex);
7052 err = lock_worktree(worktree, LOCK_SH);
7054 unlockerr = got_ref_unlock(branch_ref);
7055 if (unlockerr && err == NULL)
7057 got_ref_close(branch_ref);
7059 unlockerr = got_ref_unlock(base_branch_ref);
7060 if (unlockerr && err == NULL)
7062 got_ref_close(base_branch_ref);
7067 struct check_stage_ok_arg {
7068 struct got_object_id *head_commit_id;
7069 struct got_worktree *worktree;
7070 struct got_fileindex *fileindex;
7071 struct got_repository *repo;
7075 const struct got_error *
7076 check_stage_ok(void *arg, unsigned char status,
7077 unsigned char staged_status, const char *relpath,
7078 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7079 struct got_object_id *commit_id, int dirfd, const char *de_name)
7081 struct check_stage_ok_arg *a = arg;
7082 const struct got_error *err = NULL;
7083 struct got_fileindex_entry *ie;
7084 struct got_object_id base_commit_id;
7085 struct got_object_id *base_commit_idp = NULL;
7086 char *in_repo_path = NULL, *p;
7088 if (status == GOT_STATUS_UNVERSIONED ||
7089 status == GOT_STATUS_NO_CHANGE)
7091 if (status == GOT_STATUS_NONEXISTENT)
7092 return got_error_set_errno(ENOENT, relpath);
7094 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7096 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7098 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7099 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7101 return got_error_from_errno("asprintf");
7103 if (got_fileindex_entry_has_commit(ie)) {
7104 memcpy(base_commit_id.sha1, ie->commit_sha1,
7105 SHA1_DIGEST_LENGTH);
7106 base_commit_idp = &base_commit_id;
7109 if (status == GOT_STATUS_CONFLICT) {
7110 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7112 } else if (status != GOT_STATUS_ADD &&
7113 status != GOT_STATUS_MODIFY &&
7114 status != GOT_STATUS_DELETE) {
7115 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7119 a->have_changes = 1;
7124 err = check_out_of_date(p, status, staged_status,
7125 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7126 GOT_ERR_STAGE_OUT_OF_DATE);
7132 struct stage_path_arg {
7133 struct got_worktree *worktree;
7134 struct got_fileindex *fileindex;
7135 struct got_repository *repo;
7136 got_worktree_status_cb status_cb;
7138 got_worktree_patch_cb patch_cb;
7140 int staged_something;
7141 int allow_bad_symlinks;
7144 static const struct got_error *
7145 stage_path(void *arg, unsigned char status,
7146 unsigned char staged_status, const char *relpath,
7147 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7148 struct got_object_id *commit_id, int dirfd, const char *de_name)
7150 struct stage_path_arg *a = arg;
7151 const struct got_error *err = NULL;
7152 struct got_fileindex_entry *ie;
7153 char *ondisk_path = NULL, *path_content = NULL;
7155 struct got_object_id *new_staged_blob_id = NULL;
7158 if (status == GOT_STATUS_UNVERSIONED)
7161 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7163 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7165 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7167 return got_error_from_errno("asprintf");
7170 case GOT_STATUS_ADD:
7171 case GOT_STATUS_MODIFY:
7172 /* XXX could sb.st_mode be passed in by our caller? */
7173 if (lstat(ondisk_path, &sb) == -1) {
7174 err = got_error_from_errno2("lstat", ondisk_path);
7178 if (status == GOT_STATUS_ADD) {
7179 int choice = GOT_PATCH_CHOICE_NONE;
7180 err = (*a->patch_cb)(&choice, a->patch_arg,
7181 status, ie->path, NULL, 1, 1);
7184 if (choice != GOT_PATCH_CHOICE_YES)
7187 err = create_patched_content(&path_content, 0,
7188 staged_blob_id ? staged_blob_id : blob_id,
7189 ondisk_path, dirfd, de_name, ie->path,
7190 a->repo, a->patch_cb, a->patch_arg);
7191 if (err || path_content == NULL)
7195 err = got_object_blob_create(&new_staged_blob_id,
7196 path_content ? path_content : ondisk_path, a->repo);
7199 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7200 SHA1_DIGEST_LENGTH);
7201 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7202 stage = GOT_FILEIDX_STAGE_ADD;
7204 stage = GOT_FILEIDX_STAGE_MODIFY;
7205 got_fileindex_entry_stage_set(ie, stage);
7206 if (S_ISLNK(sb.st_mode)) {
7207 int is_bad_symlink = 0;
7208 if (!a->allow_bad_symlinks) {
7209 char target_path[PATH_MAX];
7211 target_len = readlink(ondisk_path, target_path,
7212 sizeof(target_path));
7213 if (target_len == -1) {
7214 err = got_error_from_errno2("readlink",
7218 err = is_bad_symlink_target(&is_bad_symlink,
7219 target_path, target_len, ondisk_path,
7220 a->worktree->root_path);
7223 if (is_bad_symlink) {
7224 err = got_error_path(ondisk_path,
7225 GOT_ERR_BAD_SYMLINK);
7230 got_fileindex_entry_staged_filetype_set(ie,
7231 GOT_FILEIDX_MODE_BAD_SYMLINK);
7233 got_fileindex_entry_staged_filetype_set(ie,
7234 GOT_FILEIDX_MODE_SYMLINK);
7236 got_fileindex_entry_staged_filetype_set(ie,
7237 GOT_FILEIDX_MODE_REGULAR_FILE);
7239 a->staged_something = 1;
7240 if (a->status_cb == NULL)
7242 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7243 get_staged_status(ie), relpath, blob_id,
7244 new_staged_blob_id, NULL, dirfd, de_name);
7246 case GOT_STATUS_DELETE:
7247 if (staged_status == GOT_STATUS_DELETE)
7250 int choice = GOT_PATCH_CHOICE_NONE;
7251 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7252 ie->path, NULL, 1, 1);
7255 if (choice == GOT_PATCH_CHOICE_NO)
7257 if (choice != GOT_PATCH_CHOICE_YES) {
7258 err = got_error(GOT_ERR_PATCH_CHOICE);
7262 stage = GOT_FILEIDX_STAGE_DELETE;
7263 got_fileindex_entry_stage_set(ie, stage);
7264 a->staged_something = 1;
7265 if (a->status_cb == NULL)
7267 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7268 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7271 case GOT_STATUS_NO_CHANGE:
7273 case GOT_STATUS_CONFLICT:
7274 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7276 case GOT_STATUS_NONEXISTENT:
7277 err = got_error_set_errno(ENOENT, relpath);
7280 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7284 if (path_content && unlink(path_content) == -1 && err == NULL)
7285 err = got_error_from_errno2("unlink", path_content);
7288 free(new_staged_blob_id);
7292 const struct got_error *
7293 got_worktree_stage(struct got_worktree *worktree,
7294 struct got_pathlist_head *paths,
7295 got_worktree_status_cb status_cb, void *status_arg,
7296 got_worktree_patch_cb patch_cb, void *patch_arg,
7297 int allow_bad_symlinks, struct got_repository *repo)
7299 const struct got_error *err = NULL, *sync_err, *unlockerr;
7300 struct got_pathlist_entry *pe;
7301 struct got_fileindex *fileindex = NULL;
7302 char *fileindex_path = NULL;
7303 struct got_reference *head_ref = NULL;
7304 struct got_object_id *head_commit_id = NULL;
7305 struct check_stage_ok_arg oka;
7306 struct stage_path_arg spa;
7308 err = lock_worktree(worktree, LOCK_EX);
7312 err = got_ref_open(&head_ref, repo,
7313 got_worktree_get_head_ref_name(worktree), 0);
7316 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7319 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7323 /* Check pre-conditions before staging anything. */
7324 oka.head_commit_id = head_commit_id;
7325 oka.worktree = worktree;
7326 oka.fileindex = fileindex;
7328 oka.have_changes = 0;
7329 TAILQ_FOREACH(pe, paths, entry) {
7330 err = worktree_status(worktree, pe->path, fileindex, repo,
7331 check_stage_ok, &oka, NULL, NULL, 0, 0);
7335 if (!oka.have_changes) {
7336 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7340 spa.worktree = worktree;
7341 spa.fileindex = fileindex;
7343 spa.patch_cb = patch_cb;
7344 spa.patch_arg = patch_arg;
7345 spa.status_cb = status_cb;
7346 spa.status_arg = status_arg;
7347 spa.staged_something = 0;
7348 spa.allow_bad_symlinks = allow_bad_symlinks;
7349 TAILQ_FOREACH(pe, paths, entry) {
7350 err = worktree_status(worktree, pe->path, fileindex, repo,
7351 stage_path, &spa, NULL, NULL, 0, 0);
7355 if (!spa.staged_something) {
7356 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7360 sync_err = sync_fileindex(fileindex, fileindex_path);
7361 if (sync_err && err == NULL)
7365 got_ref_close(head_ref);
7366 free(head_commit_id);
7367 free(fileindex_path);
7369 got_fileindex_free(fileindex);
7370 unlockerr = lock_worktree(worktree, LOCK_SH);
7371 if (unlockerr && err == NULL)
7376 struct unstage_path_arg {
7377 struct got_worktree *worktree;
7378 struct got_fileindex *fileindex;
7379 struct got_repository *repo;
7380 got_worktree_checkout_cb progress_cb;
7382 got_worktree_patch_cb patch_cb;
7386 static const struct got_error *
7387 create_unstaged_content(char **path_unstaged_content,
7388 char **path_new_staged_content, struct got_object_id *blob_id,
7389 struct got_object_id *staged_blob_id, const char *relpath,
7390 struct got_repository *repo,
7391 got_worktree_patch_cb patch_cb, void *patch_arg)
7393 const struct got_error *err;
7394 struct got_blob_object *blob = NULL, *staged_blob = NULL;
7395 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7396 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7397 struct stat sb1, sb2;
7398 struct got_diff_changes *changes = NULL;
7399 struct got_diff_state *ds = NULL;
7400 struct got_diff_args *args = NULL;
7401 struct got_diff_change *change;
7402 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
7403 int have_content = 0, have_rejected_content = 0;
7405 *path_unstaged_content = NULL;
7406 *path_new_staged_content = NULL;
7408 err = got_object_id_str(&label1, blob_id);
7411 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7415 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7419 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7423 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7427 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7431 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7435 if (stat(path1, &sb1) == -1) {
7436 err = got_error_from_errno2("stat", path1);
7440 if (stat(path2, &sb2) == -1) {
7441 err = got_error_from_errno2("stat", path2);
7445 err = got_diff_files(&changes, &ds, &args, &diff_flags,
7446 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
7450 err = got_opentemp_named(path_unstaged_content, &outfile,
7451 "got-unstaged-content");
7454 err = got_opentemp_named(path_new_staged_content, &rejectfile,
7455 "got-new-staged-content");
7459 if (fseek(f1, 0L, SEEK_SET) == -1) {
7460 err = got_ferror(f1, GOT_ERR_IO);
7463 if (fseek(f2, 0L, SEEK_SET) == -1) {
7464 err = got_ferror(f2, GOT_ERR_IO);
7467 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
7469 err = apply_or_reject_change(&choice, change, ++n,
7470 changes->nchanges, ds, args, diff_flags, relpath,
7471 f1, f2, &line_cur1, &line_cur2,
7472 outfile, rejectfile, patch_cb, patch_arg);
7475 if (choice == GOT_PATCH_CHOICE_YES)
7478 have_rejected_content = 1;
7479 if (choice == GOT_PATCH_CHOICE_QUIT)
7482 if (have_content || have_rejected_content)
7483 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7484 outfile, rejectfile);
7488 got_object_blob_close(blob);
7490 got_object_blob_close(staged_blob);
7491 if (f1 && fclose(f1) == EOF && err == NULL)
7492 err = got_error_from_errno2("fclose", path1);
7493 if (f2 && fclose(f2) == EOF && err == NULL)
7494 err = got_error_from_errno2("fclose", path2);
7495 if (outfile && fclose(outfile) == EOF && err == NULL)
7496 err = got_error_from_errno2("fclose", *path_unstaged_content);
7497 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7498 err = got_error_from_errno2("fclose", *path_new_staged_content);
7499 if (path1 && unlink(path1) == -1 && err == NULL)
7500 err = got_error_from_errno2("unlink", path1);
7501 if (path2 && unlink(path2) == -1 && err == NULL)
7502 err = got_error_from_errno2("unlink", path2);
7503 if (err || !have_content) {
7504 if (*path_unstaged_content &&
7505 unlink(*path_unstaged_content) == -1 && err == NULL)
7506 err = got_error_from_errno2("unlink",
7507 *path_unstaged_content);
7508 free(*path_unstaged_content);
7509 *path_unstaged_content = NULL;
7511 if (err || !have_content || !have_rejected_content) {
7512 if (*path_new_staged_content &&
7513 unlink(*path_new_staged_content) == -1 && err == NULL)
7514 err = got_error_from_errno2("unlink",
7515 *path_new_staged_content);
7516 free(*path_new_staged_content);
7517 *path_new_staged_content = NULL;
7521 got_diff_state_free(ds);
7525 got_diff_free_changes(changes);
7531 static const struct got_error *
7532 unstage_hunks(struct got_object_id *staged_blob_id,
7533 struct got_blob_object *blob_base,
7534 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
7535 const char *ondisk_path, const char *label_orig,
7536 struct got_worktree *worktree, struct got_repository *repo,
7537 got_worktree_patch_cb patch_cb, void *patch_arg,
7538 got_worktree_checkout_cb progress_cb, void *progress_arg)
7540 const struct got_error *err = NULL;
7541 char *path_unstaged_content = NULL;
7542 char *path_new_staged_content = NULL;
7543 struct got_object_id *new_staged_blob_id = NULL;
7547 err = create_unstaged_content(&path_unstaged_content,
7548 &path_new_staged_content, blob_id, staged_blob_id,
7549 ie->path, repo, patch_cb, patch_arg);
7553 if (path_unstaged_content == NULL)
7556 if (path_new_staged_content) {
7557 err = got_object_blob_create(&new_staged_blob_id,
7558 path_new_staged_content, repo);
7563 f = fopen(path_unstaged_content, "r");
7565 err = got_error_from_errno2("fopen",
7566 path_unstaged_content);
7569 if (fstat(fileno(f), &sb) == -1) {
7570 err = got_error_from_errno2("fstat", path_unstaged_content);
7573 if (got_fileindex_entry_staged_filetype_get(ie) ==
7574 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
7575 char link_target[PATH_MAX];
7577 r = fread(link_target, 1, sizeof(link_target), f);
7578 if (r == 0 && ferror(f)) {
7579 err = got_error_from_errno("fread");
7582 if (r >= sizeof(link_target)) { /* should not happen */
7583 err = got_error(GOT_ERR_NO_SPACE);
7586 link_target[r] = '\0';
7587 err = merge_symlink(worktree, blob_base,
7588 ondisk_path, ie->path, label_orig, link_target,
7589 worktree->base_commit_id, repo, progress_cb,
7592 int local_changes_subsumed;
7593 err = merge_file(&local_changes_subsumed, worktree,
7594 blob_base, ondisk_path, ie->path,
7595 got_fileindex_perms_to_st(ie),
7596 path_unstaged_content, label_orig, "unstaged",
7597 repo, progress_cb, progress_arg);
7602 if (new_staged_blob_id) {
7603 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7604 SHA1_DIGEST_LENGTH);
7606 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7608 free(new_staged_blob_id);
7609 if (path_unstaged_content &&
7610 unlink(path_unstaged_content) == -1 && err == NULL)
7611 err = got_error_from_errno2("unlink", path_unstaged_content);
7612 if (path_new_staged_content &&
7613 unlink(path_new_staged_content) == -1 && err == NULL)
7614 err = got_error_from_errno2("unlink", path_new_staged_content);
7615 if (f && fclose(f) != 0 && err == NULL)
7616 err = got_error_from_errno2("fclose", path_unstaged_content);
7617 free(path_unstaged_content);
7618 free(path_new_staged_content);
7622 static const struct got_error *
7623 unstage_path(void *arg, unsigned char status,
7624 unsigned char staged_status, const char *relpath,
7625 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7626 struct got_object_id *commit_id, int dirfd, const char *de_name)
7628 const struct got_error *err = NULL;
7629 struct unstage_path_arg *a = arg;
7630 struct got_fileindex_entry *ie;
7631 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7632 char *ondisk_path = NULL;
7633 char *id_str = NULL, *label_orig = NULL;
7634 int local_changes_subsumed;
7637 if (staged_status != GOT_STATUS_ADD &&
7638 staged_status != GOT_STATUS_MODIFY &&
7639 staged_status != GOT_STATUS_DELETE)
7642 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7644 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7646 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7648 return got_error_from_errno("asprintf");
7650 err = got_object_id_str(&id_str,
7651 commit_id ? commit_id : a->worktree->base_commit_id);
7654 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7656 err = got_error_from_errno("asprintf");
7660 switch (staged_status) {
7661 case GOT_STATUS_MODIFY:
7662 err = got_object_open_as_blob(&blob_base, a->repo,
7667 case GOT_STATUS_ADD:
7669 if (staged_status == GOT_STATUS_ADD) {
7670 int choice = GOT_PATCH_CHOICE_NONE;
7671 err = (*a->patch_cb)(&choice, a->patch_arg,
7672 staged_status, ie->path, NULL, 1, 1);
7675 if (choice != GOT_PATCH_CHOICE_YES)
7678 err = unstage_hunks(staged_blob_id,
7679 blob_base, blob_id, ie, ondisk_path,
7680 label_orig, a->worktree, a->repo,
7681 a->patch_cb, a->patch_arg,
7682 a->progress_cb, a->progress_arg);
7683 break; /* Done with this file. */
7686 err = got_object_open_as_blob(&blob_staged, a->repo,
7687 staged_blob_id, 8192);
7690 switch (got_fileindex_entry_staged_filetype_get(ie)) {
7691 case GOT_FILEIDX_MODE_BAD_SYMLINK:
7692 case GOT_FILEIDX_MODE_REGULAR_FILE:
7693 err = merge_blob(&local_changes_subsumed, a->worktree,
7694 blob_base, ondisk_path, relpath,
7695 got_fileindex_perms_to_st(ie), label_orig,
7696 blob_staged, commit_id ? commit_id :
7697 a->worktree->base_commit_id, a->repo,
7698 a->progress_cb, a->progress_arg);
7700 case GOT_FILEIDX_MODE_SYMLINK:
7701 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7702 char *staged_target;
7703 err = got_object_blob_read_to_str(
7704 &staged_target, blob_staged);
7707 err = merge_symlink(a->worktree, blob_base,
7708 ondisk_path, relpath, label_orig,
7709 staged_target, commit_id ? commit_id :
7710 a->worktree->base_commit_id,
7711 a->repo, a->progress_cb, a->progress_arg);
7712 free(staged_target);
7714 err = merge_blob(&local_changes_subsumed,
7715 a->worktree, blob_base, ondisk_path,
7716 relpath, got_fileindex_perms_to_st(ie),
7717 label_orig, blob_staged,
7718 commit_id ? commit_id :
7719 a->worktree->base_commit_id, a->repo,
7720 a->progress_cb, a->progress_arg);
7724 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
7728 got_fileindex_entry_stage_set(ie,
7729 GOT_FILEIDX_STAGE_NONE);
7731 case GOT_STATUS_DELETE:
7733 int choice = GOT_PATCH_CHOICE_NONE;
7734 err = (*a->patch_cb)(&choice, a->patch_arg,
7735 staged_status, ie->path, NULL, 1, 1);
7738 if (choice == GOT_PATCH_CHOICE_NO)
7740 if (choice != GOT_PATCH_CHOICE_YES) {
7741 err = got_error(GOT_ERR_PATCH_CHOICE);
7745 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7746 err = get_file_status(&status, &sb, ie, ondisk_path,
7747 dirfd, de_name, a->repo);
7750 err = (*a->progress_cb)(a->progress_arg, status, relpath);
7756 got_object_blob_close(blob_base);
7758 got_object_blob_close(blob_staged);
7764 const struct got_error *
7765 got_worktree_unstage(struct got_worktree *worktree,
7766 struct got_pathlist_head *paths,
7767 got_worktree_checkout_cb progress_cb, void *progress_arg,
7768 got_worktree_patch_cb patch_cb, void *patch_arg,
7769 struct got_repository *repo)
7771 const struct got_error *err = NULL, *sync_err, *unlockerr;
7772 struct got_pathlist_entry *pe;
7773 struct got_fileindex *fileindex = NULL;
7774 char *fileindex_path = NULL;
7775 struct unstage_path_arg upa;
7777 err = lock_worktree(worktree, LOCK_EX);
7781 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7785 upa.worktree = worktree;
7786 upa.fileindex = fileindex;
7788 upa.progress_cb = progress_cb;
7789 upa.progress_arg = progress_arg;
7790 upa.patch_cb = patch_cb;
7791 upa.patch_arg = patch_arg;
7792 TAILQ_FOREACH(pe, paths, entry) {
7793 err = worktree_status(worktree, pe->path, fileindex, repo,
7794 unstage_path, &upa, NULL, NULL, 0, 0);
7799 sync_err = sync_fileindex(fileindex, fileindex_path);
7800 if (sync_err && err == NULL)
7803 free(fileindex_path);
7805 got_fileindex_free(fileindex);
7806 unlockerr = lock_worktree(worktree, LOCK_SH);
7807 if (unlockerr && err == NULL)
7812 struct report_file_info_arg {
7813 struct got_worktree *worktree;
7814 got_worktree_path_info_cb info_cb;
7816 struct got_pathlist_head *paths;
7817 got_cancel_cb cancel_cb;
7821 static const struct got_error *
7822 report_file_info(void *arg, struct got_fileindex_entry *ie)
7824 struct report_file_info_arg *a = arg;
7825 struct got_pathlist_entry *pe;
7826 struct got_object_id blob_id, staged_blob_id, commit_id;
7827 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
7828 struct got_object_id *commit_idp = NULL;
7831 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
7832 return got_error(GOT_ERR_CANCELLED);
7834 TAILQ_FOREACH(pe, a->paths, entry) {
7835 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
7836 got_path_is_child(ie->path, pe->path, pe->path_len))
7839 if (pe == NULL) /* not found */
7842 if (got_fileindex_entry_has_blob(ie)) {
7843 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
7844 blob_idp = &blob_id;
7846 stage = got_fileindex_entry_stage_get(ie);
7847 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
7848 stage == GOT_FILEIDX_STAGE_ADD) {
7849 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
7850 SHA1_DIGEST_LENGTH);
7851 staged_blob_idp = &staged_blob_id;
7854 if (got_fileindex_entry_has_commit(ie)) {
7855 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
7856 commit_idp = &commit_id;
7859 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
7860 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
7863 const struct got_error *
7864 got_worktree_path_info(struct got_worktree *worktree,
7865 struct got_pathlist_head *paths,
7866 got_worktree_path_info_cb info_cb, void *info_arg,
7867 got_cancel_cb cancel_cb, void *cancel_arg)
7870 const struct got_error *err = NULL, *unlockerr;
7871 struct got_fileindex *fileindex = NULL;
7872 char *fileindex_path = NULL;
7873 struct report_file_info_arg arg;
7875 err = lock_worktree(worktree, LOCK_SH);
7879 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7883 arg.worktree = worktree;
7884 arg.info_cb = info_cb;
7885 arg.info_arg = info_arg;
7887 arg.cancel_cb = cancel_cb;
7888 arg.cancel_arg = cancel_arg;
7889 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
7892 free(fileindex_path);
7894 got_fileindex_free(fileindex);
7895 unlockerr = lock_worktree(worktree, LOCK_UN);
7896 if (unlockerr && err == NULL)