2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/queue.h>
38 #include "got_error.h"
39 #include "got_repository.h"
40 #include "got_reference.h"
41 #include "got_object.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
45 #include "got_opentemp.h"
48 #include "got_lib_worktree.h"
49 #include "got_lib_sha1.h"
50 #include "got_lib_fileindex.h"
51 #include "got_lib_inflate.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_object_idset.h"
57 #include "got_lib_diff.h"
58 #include "got_lib_gotconfig.h"
61 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
64 #define GOT_MERGE_LABEL_MERGED "merged change"
65 #define GOT_MERGE_LABEL_BASE "3-way merge base"
67 static const struct got_error *
68 create_meta_file(const char *path_got, const char *name, const char *content)
70 const struct got_error *err = NULL;
73 if (asprintf(&path, "%s/%s", path_got, name) == -1)
74 return got_error_from_errno("asprintf");
76 err = got_path_create_file(path, content);
81 static const struct got_error *
82 update_meta_file(const char *path_got, const char *name, const char *content)
84 const struct got_error *err = NULL;
89 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
90 err = got_error_from_errno("asprintf");
95 err = got_opentemp_named(&tmppath, &tmpfile, path);
100 int len = fprintf(tmpfile, "%s\n", content);
101 if (len != strlen(content) + 1) {
102 err = got_error_from_errno2("fprintf", tmppath);
107 if (rename(tmppath, path) != 0) {
108 err = got_error_from_errno3("rename", tmppath, path);
114 if (fclose(tmpfile) != 0 && err == NULL)
115 err = got_error_from_errno2("fclose", tmppath);
120 static const struct got_error *
121 read_meta_file(char **content, const char *path_got, const char *name)
123 const struct got_error *err = NULL;
131 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
132 err = got_error_from_errno("asprintf");
137 fd = open(path, O_RDONLY | O_NOFOLLOW);
140 err = got_error_path(path, GOT_ERR_WORKTREE_META);
142 err = got_error_from_errno2("open", path);
145 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
146 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
147 : got_error_from_errno2("flock", path));
151 if (fstat(fd, &sb) != 0) {
152 err = got_error_from_errno2("fstat", path);
155 *content = calloc(1, sb.st_size);
156 if (*content == NULL) {
157 err = got_error_from_errno("calloc");
161 n = read(fd, *content, sb.st_size);
162 if (n != sb.st_size) {
163 err = (n == -1 ? got_error_from_errno2("read", path) :
164 got_error_path(path, GOT_ERR_WORKTREE_META));
167 if ((*content)[sb.st_size - 1] != '\n') {
168 err = got_error_path(path, GOT_ERR_WORKTREE_META);
171 (*content)[sb.st_size - 1] = '\0';
174 if (fd != -1 && close(fd) == -1 && err == NULL)
175 err = got_error_from_errno2("close", path_got);
184 static const struct got_error *
185 write_head_ref(const char *path_got, struct got_reference *head_ref)
187 const struct got_error *err = NULL;
190 if (got_ref_is_symbolic(head_ref)) {
191 refstr = got_ref_to_str(head_ref);
193 return got_error_from_errno("got_ref_to_str");
195 refstr = strdup(got_ref_get_name(head_ref));
197 return got_error_from_errno("strdup");
199 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
204 const struct got_error *
205 got_worktree_init(const char *path, struct got_reference *head_ref,
206 const char *prefix, struct got_repository *repo)
208 const struct got_error *err = NULL;
209 struct got_object_id *commit_id = NULL;
211 uint32_t uuid_status;
213 char *path_got = NULL;
214 char *formatstr = NULL;
215 char *absprefix = NULL;
216 char *basestr = NULL;
217 char *uuidstr = NULL;
219 if (strcmp(path, got_repo_get_path(repo)) == 0) {
220 err = got_error(GOT_ERR_WORKTREE_REPO);
224 err = got_ref_resolve(&commit_id, repo, head_ref);
227 err = got_object_get_type(&obj_type, repo, commit_id);
230 if (obj_type != GOT_OBJ_TYPE_COMMIT)
231 return got_error(GOT_ERR_OBJ_TYPE);
233 if (!got_path_is_absolute(prefix)) {
234 if (asprintf(&absprefix, "/%s", prefix) == -1)
235 return got_error_from_errno("asprintf");
238 /* Create top-level directory (may already exist). */
239 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
240 err = got_error_from_errno2("mkdir", path);
244 /* Create .got directory (may already exist). */
245 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
246 err = got_error_from_errno("asprintf");
249 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
250 err = got_error_from_errno2("mkdir", path_got);
254 /* Create an empty lock file. */
255 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
259 /* Create an empty file index. */
260 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
264 /* Write the HEAD reference. */
265 err = write_head_ref(path_got, head_ref);
269 /* Record our base commit. */
270 err = got_object_id_str(&basestr, commit_id);
273 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
277 /* Store path to repository. */
278 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
279 got_repo_get_path(repo));
283 /* Store in-repository path prefix. */
284 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
285 absprefix ? absprefix : prefix);
290 uuid_create(&uuid, &uuid_status);
291 if (uuid_status != uuid_s_ok) {
292 err = got_error_uuid(uuid_status, "uuid_create");
295 uuid_to_string(&uuid, &uuidstr, &uuid_status);
296 if (uuid_status != uuid_s_ok) {
297 err = got_error_uuid(uuid_status, "uuid_to_string");
300 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
304 /* Stamp work tree with format file. */
305 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
306 err = got_error_from_errno("asprintf");
309 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
323 static const struct got_error *
324 open_worktree(struct got_worktree **worktree, const char *path)
326 const struct got_error *err = NULL;
328 char *formatstr = NULL;
329 char *uuidstr = NULL;
330 char *path_lock = NULL;
331 char *base_commit_id_str = NULL;
332 int version, fd = -1;
334 struct got_repository *repo = NULL;
335 uint32_t uuid_status;
339 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
340 err = got_error_from_errno("asprintf");
345 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
346 err = got_error_from_errno("asprintf");
351 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
353 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
354 : got_error_from_errno2("open", path_lock));
358 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
362 version = strtonum(formatstr, 1, INT_MAX, &errstr);
364 err = got_error_msg(GOT_ERR_WORKTREE_META,
365 "could not parse work tree format version number");
368 if (version != GOT_WORKTREE_FORMAT_VERSION) {
369 err = got_error(GOT_ERR_WORKTREE_VERS);
373 *worktree = calloc(1, sizeof(**worktree));
374 if (*worktree == NULL) {
375 err = got_error_from_errno("calloc");
378 (*worktree)->lockfd = -1;
380 (*worktree)->root_path = realpath(path, NULL);
381 if ((*worktree)->root_path == NULL) {
382 err = got_error_from_errno2("realpath", path);
385 err = read_meta_file(&(*worktree)->repo_path, path_got,
386 GOT_WORKTREE_REPOSITORY);
390 err = read_meta_file(&(*worktree)->path_prefix, path_got,
391 GOT_WORKTREE_PATH_PREFIX);
395 err = read_meta_file(&base_commit_id_str, path_got,
396 GOT_WORKTREE_BASE_COMMIT);
400 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
403 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
404 if (uuid_status != uuid_s_ok) {
405 err = got_error_uuid(uuid_status, "uuid_from_string");
409 err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
413 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
418 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
419 GOT_WORKTREE_HEAD_REF);
423 if (asprintf(&(*worktree)->gotconfig_path, "%s/%s/%s",
424 (*worktree)->root_path,
425 GOT_WORKTREE_GOT_DIR, GOT_GOTCONFIG_FILENAME) == -1) {
426 err = got_error_from_errno("asprintf");
430 err = got_gotconfig_read(&(*worktree)->gotconfig,
431 (*worktree)->gotconfig_path);
434 got_repo_close(repo);
437 free(base_commit_id_str);
443 if (*worktree != NULL)
444 got_worktree_close(*worktree);
447 (*worktree)->lockfd = fd;
452 const struct got_error *
453 got_worktree_open(struct got_worktree **worktree, const char *path)
455 const struct got_error *err = NULL;
458 worktree_path = strdup(path);
459 if (worktree_path == NULL)
460 return got_error_from_errno("strdup");
465 err = open_worktree(worktree, worktree_path);
466 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT)) {
474 if (worktree_path[0] == '/' && worktree_path[1] == '\0')
476 err = got_path_dirname(&parent_path, worktree_path);
478 if (err->code != GOT_ERR_BAD_PATH) {
485 worktree_path = parent_path;
489 return got_error(GOT_ERR_NOT_WORKTREE);
492 const struct got_error *
493 got_worktree_close(struct got_worktree *worktree)
495 const struct got_error *err = NULL;
496 free(worktree->repo_path);
497 free(worktree->path_prefix);
498 free(worktree->base_commit_id);
499 free(worktree->head_ref_name);
500 if (worktree->lockfd != -1)
501 if (close(worktree->lockfd) != 0)
502 err = got_error_from_errno2("close",
503 got_worktree_get_root_path(worktree));
504 free(worktree->root_path);
505 free(worktree->gotconfig_path);
506 got_gotconfig_free(worktree->gotconfig);
512 got_worktree_get_root_path(struct got_worktree *worktree)
514 return worktree->root_path;
518 got_worktree_get_repo_path(struct got_worktree *worktree)
520 return worktree->repo_path;
523 got_worktree_get_path_prefix(struct got_worktree *worktree)
525 return worktree->path_prefix;
528 const struct got_error *
529 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
530 const char *path_prefix)
532 char *absprefix = NULL;
534 if (!got_path_is_absolute(path_prefix)) {
535 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
536 return got_error_from_errno("asprintf");
538 *match = (strcmp(absprefix ? absprefix : path_prefix,
539 worktree->path_prefix) == 0);
545 got_worktree_get_head_ref_name(struct got_worktree *worktree)
547 return worktree->head_ref_name;
550 const struct got_error *
551 got_worktree_set_head_ref(struct got_worktree *worktree,
552 struct got_reference *head_ref)
554 const struct got_error *err = NULL;
555 char *path_got = NULL, *head_ref_name = NULL;
557 if (asprintf(&path_got, "%s/%s", worktree->root_path,
558 GOT_WORKTREE_GOT_DIR) == -1) {
559 err = got_error_from_errno("asprintf");
564 head_ref_name = strdup(got_ref_get_name(head_ref));
565 if (head_ref_name == NULL) {
566 err = got_error_from_errno("strdup");
570 err = write_head_ref(path_got, head_ref);
574 free(worktree->head_ref_name);
575 worktree->head_ref_name = head_ref_name;
583 struct got_object_id *
584 got_worktree_get_base_commit_id(struct got_worktree *worktree)
586 return worktree->base_commit_id;
589 const struct got_error *
590 got_worktree_set_base_commit_id(struct got_worktree *worktree,
591 struct got_repository *repo, struct got_object_id *commit_id)
593 const struct got_error *err;
594 struct got_object *obj = NULL;
596 char *path_got = NULL;
598 if (asprintf(&path_got, "%s/%s", worktree->root_path,
599 GOT_WORKTREE_GOT_DIR) == -1) {
600 err = got_error_from_errno("asprintf");
605 err = got_object_open(&obj, repo, commit_id);
609 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
610 err = got_error(GOT_ERR_OBJ_TYPE);
614 /* Record our base commit. */
615 err = got_object_id_str(&id_str, commit_id);
618 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
622 free(worktree->base_commit_id);
623 worktree->base_commit_id = got_object_id_dup(commit_id);
624 if (worktree->base_commit_id == NULL) {
625 err = got_error_from_errno("got_object_id_dup");
630 got_object_close(obj);
636 const struct got_gotconfig *
637 got_worktree_get_gotconfig(struct got_worktree *worktree)
639 return worktree->gotconfig;
642 static const struct got_error *
643 lock_worktree(struct got_worktree *worktree, int operation)
645 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
646 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
647 : got_error_from_errno2("flock",
648 got_worktree_get_root_path(worktree)));
652 static const struct got_error *
653 add_dir_on_disk(struct got_worktree *worktree, const char *path)
655 const struct got_error *err = NULL;
658 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
659 return got_error_from_errno("asprintf");
661 err = got_path_mkdir(abspath);
662 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
665 if (lstat(abspath, &sb) == -1) {
666 err = got_error_from_errno2("lstat", abspath);
667 } else if (!S_ISDIR(sb.st_mode)) {
668 /* TODO directory is obstructed; do something */
669 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
676 static const struct got_error *
677 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
679 const struct got_error *err = NULL;
682 size_t flen1 = 0, flen2 = 0;
687 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
688 if (flen1 == 0 && ferror(f1)) {
689 err = got_error_from_errno("fread");
692 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
693 if (flen2 == 0 && ferror(f2)) {
694 err = got_error_from_errno("fread");
701 } else if (flen2 == 0) {
705 } else if (flen1 == flen2) {
706 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
719 static const struct got_error *
720 check_files_equal(int *same, const char *f1_path, const char *f2_path)
722 const struct got_error *err = NULL;
725 FILE *f1 = NULL, *f2 = NULL;
729 if (lstat(f1_path, &sb) != 0) {
730 err = got_error_from_errno2("lstat", f1_path);
735 if (lstat(f2_path, &sb) != 0) {
736 err = got_error_from_errno2("lstat", f2_path);
741 if (size1 != size2) {
746 f1 = fopen(f1_path, "r");
748 return got_error_from_errno2("fopen", f1_path);
750 f2 = fopen(f2_path, "r");
752 err = got_error_from_errno2("fopen", f2_path);
756 err = check_file_contents_equal(same, f1, f2);
758 if (f1 && fclose(f1) != 0 && err == NULL)
759 err = got_error_from_errno("fclose");
760 if (f2 && fclose(f2) != 0 && err == NULL)
761 err = got_error_from_errno("fclose");
767 * Perform a 3-way merge where blob_orig acts as the common ancestor,
768 * the file at deriv_path acts as the first derived version, and the
769 * file on disk acts as the second derived version.
771 static const struct got_error *
772 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
773 struct got_blob_object *blob_orig, const char *ondisk_path,
774 const char *path, uint16_t st_mode, const char *deriv_path,
775 const char *label_orig, const char *label_deriv,
776 struct got_repository *repo,
777 got_worktree_checkout_cb progress_cb, void *progress_arg)
779 const struct got_error *err = NULL;
782 char *blob_orig_path = NULL;
783 char *merged_path = NULL, *base_path = NULL;
786 char *symlink_path = NULL;
787 FILE *symlinkf = NULL;
789 *local_changes_subsumed = 0;
791 err = got_path_dirname(&parent, ondisk_path);
795 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
796 err = got_error_from_errno("asprintf");
800 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
805 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
806 err = got_error_from_errno("asprintf");
811 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
815 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
821 * If the file has no blob, this is an "add vs add" conflict,
822 * and we simply use an empty ancestor file to make both files
823 * appear in the merged result in their entirety.
828 * In order the run a 3-way merge with a symlink we copy the symlink's
829 * target path into a temporary file and use that file with diff3.
831 if (S_ISLNK(st_mode)) {
832 char target_path[PATH_MAX];
837 if (asprintf(&base_path, "%s/got-symlink-merge",
839 err = got_error_from_errno("asprintf");
843 err = got_opentemp_named(&symlink_path, &symlinkf, base_path);
846 target_len = readlink(ondisk_path, target_path,
847 sizeof(target_path));
848 if (target_len == -1) {
849 err = got_error_from_errno2("readlink", ondisk_path);
852 n = fwrite(target_path, 1, target_len, symlinkf);
853 if (n != target_len) {
854 err = got_ferror(symlinkf, GOT_ERR_IO);
857 if (fflush(symlinkf) == EOF) {
858 err = got_error_from_errno2("fflush", symlink_path);
863 err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
864 blob_orig_path, symlink_path ? symlink_path : ondisk_path,
865 label_deriv, label_orig, NULL);
869 err = (*progress_cb)(progress_arg,
870 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
874 if (fsync(merged_fd) != 0) {
875 err = got_error_from_errno("fsync");
879 /* Check if a clean merge has subsumed all local changes. */
880 if (overlapcnt == 0) {
881 err = check_files_equal(local_changes_subsumed, deriv_path,
887 if (fchmod(merged_fd, st_mode) != 0) {
888 err = got_error_from_errno2("fchmod", merged_path);
892 if (rename(merged_path, ondisk_path) != 0) {
893 err = got_error_from_errno3("rename", merged_path,
903 if (unlink(symlink_path) == -1 && err == NULL)
904 err = got_error_from_errno2("unlink", symlink_path);
906 if (symlinkf && fclose(symlinkf) == EOF && err == NULL)
907 err = got_error_from_errno2("fclose", symlink_path);
909 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
910 err = got_error_from_errno("close");
911 if (f_orig && fclose(f_orig) != 0 && err == NULL)
912 err = got_error_from_errno("fclose");
915 if (blob_orig_path) {
916 unlink(blob_orig_path);
917 free(blob_orig_path);
923 static const struct got_error *
924 update_symlink(const char *ondisk_path, const char *target_path,
927 /* This is not atomic but matches what 'ln -sf' does. */
928 if (unlink(ondisk_path) == -1)
929 return got_error_from_errno2("unlink", ondisk_path);
930 if (symlink(target_path, ondisk_path) == -1)
931 return got_error_from_errno3("symlink", target_path,
937 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
938 * in the work tree with a file that contains conflict markers and the
939 * conflicting target paths of the original version, a "derived version"
940 * of a symlink from an incoming change, and a local version of the symlink.
942 * The original versions's target path can be NULL if it is not available,
943 * such as if both derived versions added a new symlink at the same path.
945 * The incoming derived symlink target is NULL in case the incoming change
946 * has deleted this symlink.
948 static const struct got_error *
949 install_symlink_conflict(const char *deriv_target,
950 struct got_object_id *deriv_base_commit_id, const char *orig_target,
951 const char *label_orig, const char *local_target, const char *ondisk_path)
953 const struct got_error *err;
954 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
957 err = got_object_id_str(&id_str, deriv_base_commit_id);
959 return got_error_from_errno("asprintf");
961 if (asprintf(&label_deriv, "%s: commit %s",
962 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
963 err = got_error_from_errno("asprintf");
967 err = got_opentemp_named(&path, &f, "got-symlink-conflict");
971 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
972 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
973 deriv_target ? deriv_target : "(symlink was deleted)",
974 orig_target ? label_orig : "",
975 orig_target ? "\n" : "",
976 orig_target ? orig_target : "",
977 orig_target ? "\n" : "",
978 GOT_DIFF_CONFLICT_MARKER_SEP,
979 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
980 err = got_error_from_errno2("fprintf", path);
984 if (unlink(ondisk_path) == -1) {
985 err = got_error_from_errno2("unlink", ondisk_path);
988 if (rename(path, ondisk_path) == -1) {
989 err = got_error_from_errno3("rename", path, ondisk_path);
992 if (chmod(ondisk_path, GOT_DEFAULT_FILE_MODE) == -1) {
993 err = got_error_from_errno2("chmod", ondisk_path);
997 if (f != NULL && fclose(f) == EOF && err == NULL)
998 err = got_error_from_errno2("fclose", path);
1005 /* forward declaration */
1006 static const struct got_error *
1007 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
1008 const char *, const char *, uint16_t, const char *,
1009 struct got_blob_object *, struct got_object_id *,
1010 struct got_repository *, got_worktree_checkout_cb, void *);
1013 * Merge a symlink into the work tree, where blob_orig acts as the common
1014 * ancestor, deriv_target is the link target of the first derived version,
1015 * and the symlink on disk acts as the second derived version.
1016 * Assume that contents of both blobs represent symlinks.
1018 static const struct got_error *
1019 merge_symlink(struct got_worktree *worktree,
1020 struct got_blob_object *blob_orig, const char *ondisk_path,
1021 const char *path, const char *label_orig, const char *deriv_target,
1022 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1023 got_worktree_checkout_cb progress_cb, void *progress_arg)
1025 const struct got_error *err = NULL;
1026 char *ancestor_target = NULL;
1028 ssize_t ondisk_len, deriv_len;
1029 char ondisk_target[PATH_MAX];
1030 int have_local_change = 0;
1031 int have_incoming_change = 0;
1033 if (lstat(ondisk_path, &sb) == -1)
1034 return got_error_from_errno2("lstat", ondisk_path);
1036 ondisk_len = readlink(ondisk_path, ondisk_target,
1037 sizeof(ondisk_target));
1038 if (ondisk_len == -1) {
1039 err = got_error_from_errno2("readlink",
1043 ondisk_target[ondisk_len] = '\0';
1046 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
1051 if (ancestor_target == NULL ||
1052 (ondisk_len != strlen(ancestor_target) ||
1053 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
1054 have_local_change = 1;
1056 deriv_len = strlen(deriv_target);
1057 if (ancestor_target == NULL ||
1058 (deriv_len != strlen(ancestor_target) ||
1059 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
1060 have_incoming_change = 1;
1062 if (!have_local_change && !have_incoming_change) {
1063 if (ancestor_target) {
1064 /* Both sides made the same change. */
1065 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1067 } else if (deriv_len == ondisk_len &&
1068 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1069 /* Both sides added the same symlink. */
1070 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1073 /* Both sides added symlinks which don't match. */
1074 err = install_symlink_conflict(deriv_target,
1075 deriv_base_commit_id, ancestor_target,
1076 label_orig, ondisk_target, ondisk_path);
1079 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1082 } else if (!have_local_change && have_incoming_change) {
1083 /* Apply the incoming change. */
1084 err = update_symlink(ondisk_path, deriv_target,
1085 strlen(deriv_target));
1088 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1089 } else if (have_local_change && have_incoming_change) {
1090 if (deriv_len == ondisk_len &&
1091 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
1092 /* Both sides made the same change. */
1093 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1096 err = install_symlink_conflict(deriv_target,
1097 deriv_base_commit_id, ancestor_target, label_orig,
1098 ondisk_target, ondisk_path);
1101 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1107 free(ancestor_target);
1112 * Perform a 3-way merge where blob_orig acts as the common ancestor,
1113 * blob_deriv acts as the first derived version, and the file on disk
1114 * acts as the second derived version.
1116 static const struct got_error *
1117 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1118 struct got_blob_object *blob_orig, const char *ondisk_path,
1119 const char *path, uint16_t st_mode, const char *label_orig,
1120 struct got_blob_object *blob_deriv,
1121 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1122 got_worktree_checkout_cb progress_cb, void *progress_arg)
1124 const struct got_error *err = NULL;
1125 FILE *f_deriv = NULL;
1126 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1127 char *label_deriv = NULL, *parent = NULL;
1129 *local_changes_subsumed = 0;
1131 err = got_path_dirname(&parent, ondisk_path);
1136 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1137 err = got_error_from_errno("asprintf");
1142 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1145 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1150 err = got_object_id_str(&id_str, deriv_base_commit_id);
1153 if (asprintf(&label_deriv, "%s: commit %s",
1154 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1155 err = got_error_from_errno("asprintf");
1159 err = merge_file(local_changes_subsumed, worktree, blob_orig,
1160 ondisk_path, path, st_mode, blob_deriv_path, label_orig,
1161 label_deriv, repo, progress_cb, progress_arg);
1163 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
1164 err = got_error_from_errno("fclose");
1166 if (blob_deriv_path) {
1167 unlink(blob_deriv_path);
1168 free(blob_deriv_path);
1176 static const struct got_error *
1177 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1178 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1179 const char *ondisk_path, const char *path, struct got_object_id *blob_id)
1181 const struct got_error *err = NULL;
1182 struct got_fileindex_entry *new_ie;
1186 err = got_fileindex_entry_alloc(&new_ie, path);
1190 err = got_fileindex_entry_update(new_ie, ondisk_path,
1191 blob_id->sha1, base_commit_id->sha1, 1);
1195 err = got_fileindex_entry_add(fileindex, new_ie);
1198 got_fileindex_entry_free(new_ie);
1205 get_ondisk_perms(int executable, mode_t st_mode)
1207 mode_t xbits = S_IXUSR;
1210 /* Map read bits to execute bits. */
1211 if (st_mode & S_IRGRP)
1213 if (st_mode & S_IROTH)
1215 return st_mode | xbits;
1218 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1221 /* forward declaration */
1222 static const struct got_error *
1223 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1224 const char *path, mode_t te_mode, mode_t st_mode,
1225 struct got_blob_object *blob, int restoring_missing_file,
1226 int reverting_versioned_file, int installing_bad_symlink,
1227 int path_is_unversioned, struct got_repository *repo,
1228 got_worktree_checkout_cb progress_cb, void *progress_arg);
1231 * This function assumes that the provided symlink target points at a
1232 * safe location in the work tree!
1234 static const struct got_error *
1235 replace_existing_symlink(const char *ondisk_path, const char *target_path,
1238 const struct got_error *err = NULL;
1240 char etarget[PATH_MAX];
1244 * "Bad" symlinks (those pointing outside the work tree or into the
1245 * .got directory) are installed in the work tree as a regular file
1246 * which contains the bad symlink target path.
1247 * The new symlink target has already been checked for safety by our
1248 * caller. If we can successfully open a regular file then we simply
1249 * replace this file with a symlink below.
1251 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1254 return got_error_from_errno2("open", ondisk_path);
1256 /* We are updating an existing on-disk symlink. */
1257 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1259 return got_error_from_errno2("readlink", ondisk_path);
1261 if (elen == target_len &&
1262 memcmp(etarget, target_path, target_len) == 0)
1263 return NULL; /* nothing to do */
1266 err = update_symlink(ondisk_path, target_path, target_len);
1267 if (fd != -1 && close(fd) == -1 && err == NULL)
1268 err = got_error_from_errno2("close", ondisk_path);
1272 static const struct got_error *
1273 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1274 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1276 const struct got_error *err = NULL;
1277 char canonpath[PATH_MAX];
1278 char *path_got = NULL;
1280 *is_bad_symlink = 0;
1282 if (target_len >= sizeof(canonpath)) {
1283 *is_bad_symlink = 1;
1288 * We do not use realpath(3) to resolve the symlink's target
1289 * path because we don't want to resolve symlinks recursively.
1290 * Instead we make the path absolute and then canonicalize it.
1291 * Relative symlink target lookup should begin at the directory
1292 * in which the blob object is being installed.
1294 if (!got_path_is_absolute(target_path)) {
1296 char *parent = dirname(ondisk_path);
1298 return got_error_from_errno2("dirname", ondisk_path);
1299 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1)
1300 return got_error_from_errno("asprintf");
1301 if (strlen(abspath) >= sizeof(canonpath)) {
1302 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1306 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1311 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1316 /* Only allow symlinks pointing at paths within the work tree. */
1317 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1318 *is_bad_symlink = 1;
1322 /* Do not allow symlinks pointing into the .got directory. */
1323 if (asprintf(&path_got, "%s/%s", wtroot_path,
1324 GOT_WORKTREE_GOT_DIR) == -1)
1325 return got_error_from_errno("asprintf");
1326 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1327 *is_bad_symlink = 1;
1333 static const struct got_error *
1334 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1335 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1336 int restoring_missing_file, int reverting_versioned_file,
1337 int path_is_unversioned, struct got_repository *repo,
1338 got_worktree_checkout_cb progress_cb, void *progress_arg)
1340 const struct got_error *err = NULL;
1341 char target_path[PATH_MAX];
1342 size_t len, target_len = 0;
1343 char *path_got = NULL;
1344 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1345 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1347 *is_bad_symlink = 0;
1350 * Blob object content specifies the target path of the link.
1351 * If a symbolic link cannot be installed we instead create
1352 * a regular file which contains the link target path stored
1353 * in the blob object.
1356 err = got_object_blob_read_block(&len, blob);
1357 if (len + target_len >= sizeof(target_path)) {
1358 /* Path too long; install as a regular file. */
1359 *is_bad_symlink = 1;
1360 got_object_blob_rewind(blob);
1361 return install_blob(worktree, ondisk_path, path,
1362 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1363 restoring_missing_file, reverting_versioned_file,
1364 1, path_is_unversioned, repo, progress_cb,
1368 /* Skip blob object header first time around. */
1369 memcpy(target_path + target_len, buf + hdrlen,
1371 target_len += len - hdrlen;
1375 target_path[target_len] = '\0';
1377 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1378 ondisk_path, worktree->root_path);
1382 if (*is_bad_symlink) {
1383 /* install as a regular file */
1384 *is_bad_symlink = 1;
1385 got_object_blob_rewind(blob);
1386 err = install_blob(worktree, ondisk_path, path,
1387 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1388 restoring_missing_file, reverting_versioned_file, 1,
1389 path_is_unversioned, repo, progress_cb, progress_arg);
1393 if (symlink(target_path, ondisk_path) == -1) {
1394 if (errno == EEXIST) {
1395 if (path_is_unversioned) {
1396 err = (*progress_cb)(progress_arg,
1397 GOT_STATUS_UNVERSIONED, path);
1400 err = replace_existing_symlink(ondisk_path,
1401 target_path, target_len);
1405 err = (*progress_cb)(progress_arg,
1406 reverting_versioned_file ?
1407 GOT_STATUS_REVERT : GOT_STATUS_UPDATE,
1410 goto done; /* Nothing else to do. */
1413 if (errno == ENOENT) {
1414 char *parent = dirname(ondisk_path);
1415 if (parent == NULL) {
1416 err = got_error_from_errno2("dirname",
1420 err = add_dir_on_disk(worktree, parent);
1424 * Retry, and fall through to error handling
1425 * below if this second attempt fails.
1427 if (symlink(target_path, ondisk_path) != -1) {
1428 err = NULL; /* success */
1433 /* Handle errors from first or second creation attempt. */
1434 if (errno == ENAMETOOLONG) {
1435 /* bad target path; install as a regular file */
1436 *is_bad_symlink = 1;
1437 got_object_blob_rewind(blob);
1438 err = install_blob(worktree, ondisk_path, path,
1439 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1440 restoring_missing_file, reverting_versioned_file, 1,
1441 path_is_unversioned, repo,
1442 progress_cb, progress_arg);
1443 } else if (errno == ENOTDIR) {
1444 err = got_error_path(ondisk_path,
1445 GOT_ERR_FILE_OBSTRUCTED);
1447 err = got_error_from_errno3("symlink",
1448 target_path, ondisk_path);
1450 } else if (progress_cb)
1451 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1452 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1458 static const struct got_error *
1459 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1460 const char *path, mode_t te_mode, mode_t st_mode,
1461 struct got_blob_object *blob, int restoring_missing_file,
1462 int reverting_versioned_file, int installing_bad_symlink,
1463 int path_is_unversioned, struct got_repository *repo,
1464 got_worktree_checkout_cb progress_cb, void *progress_arg)
1466 const struct got_error *err = NULL;
1470 char *tmppath = NULL;
1472 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1473 GOT_DEFAULT_FILE_MODE);
1475 if (errno == ENOENT) {
1476 char *parent = dirname(path);
1478 return got_error_from_errno2("dirname", path);
1479 err = add_dir_on_disk(worktree, parent);
1482 fd = open(ondisk_path,
1483 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1484 GOT_DEFAULT_FILE_MODE);
1486 return got_error_from_errno2("open",
1488 } else if (errno == EEXIST) {
1489 if (path_is_unversioned) {
1490 err = (*progress_cb)(progress_arg,
1491 GOT_STATUS_UNVERSIONED, path);
1494 if (!S_ISREG(st_mode) && !installing_bad_symlink) {
1495 /* TODO file is obstructed; do something */
1496 err = got_error_path(ondisk_path,
1497 GOT_ERR_FILE_OBSTRUCTED);
1500 err = got_opentemp_named_fd(&tmppath, &fd,
1507 return got_error_from_errno2("open", ondisk_path);
1511 if (restoring_missing_file)
1512 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1514 else if (reverting_versioned_file)
1515 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1518 err = (*progress_cb)(progress_arg,
1519 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1524 hdrlen = got_object_blob_get_hdrlen(blob);
1526 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1527 err = got_object_blob_read_block(&len, blob);
1531 /* Skip blob object header first time around. */
1532 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1534 err = got_error_from_errno("write");
1536 } else if (outlen != len - hdrlen) {
1537 err = got_error(GOT_ERR_IO);
1544 if (fsync(fd) != 0) {
1545 err = got_error_from_errno("fsync");
1550 if (rename(tmppath, ondisk_path) != 0) {
1551 err = got_error_from_errno3("rename", tmppath,
1558 if (chmod(ondisk_path,
1559 get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1560 err = got_error_from_errno2("chmod", ondisk_path);
1565 if (fd != -1 && close(fd) != 0 && err == NULL)
1566 err = got_error_from_errno("close");
1571 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1572 static const struct got_error *
1573 get_modified_file_content_status(unsigned char *status, FILE *f)
1575 const struct got_error *err = NULL;
1576 const char *markers[3] = {
1577 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1578 GOT_DIFF_CONFLICT_MARKER_SEP,
1579 GOT_DIFF_CONFLICT_MARKER_END
1584 const char delim[3] = {'\0', '\0', '\0'};
1586 while (*status == GOT_STATUS_MODIFY) {
1587 line = fparseln(f, &len, NULL, delim, 0);
1591 err = got_ferror(f, GOT_ERR_IO);
1595 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1596 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1598 *status = GOT_STATUS_CONFLICT;
1608 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1610 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1611 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1615 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1617 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1618 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1619 ie->mtime_sec == sb->st_mtim.tv_sec &&
1620 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1621 ie->size == (sb->st_size & 0xffffffff) &&
1622 !xbit_differs(ie, sb->st_mode));
1625 static unsigned char
1626 get_staged_status(struct got_fileindex_entry *ie)
1628 switch (got_fileindex_entry_stage_get(ie)) {
1629 case GOT_FILEIDX_STAGE_ADD:
1630 return GOT_STATUS_ADD;
1631 case GOT_FILEIDX_STAGE_DELETE:
1632 return GOT_STATUS_DELETE;
1633 case GOT_FILEIDX_STAGE_MODIFY:
1634 return GOT_STATUS_MODIFY;
1636 return GOT_STATUS_NO_CHANGE;
1640 static const struct got_error *
1641 get_symlink_modification_status(unsigned char *status,
1642 struct got_fileindex_entry *ie, const char *abspath,
1643 int dirfd, const char *de_name, struct got_blob_object *blob)
1645 const struct got_error *err = NULL;
1646 char target_path[PATH_MAX];
1647 char etarget[PATH_MAX];
1649 size_t len, target_len = 0;
1650 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1651 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1653 *status = GOT_STATUS_NO_CHANGE;
1655 /* Blob object content specifies the target path of the link. */
1657 err = got_object_blob_read_block(&len, blob);
1660 if (len + target_len >= sizeof(target_path)) {
1662 * Should not happen. The blob contents were OK
1663 * when this symlink was installed.
1665 return got_error(GOT_ERR_NO_SPACE);
1668 /* Skip blob object header first time around. */
1669 memcpy(target_path + target_len, buf + hdrlen,
1671 target_len += len - hdrlen;
1675 target_path[target_len] = '\0';
1678 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1680 return got_error_from_errno2("readlinkat", abspath);
1682 elen = readlink(abspath, etarget, sizeof(etarget));
1684 return got_error_from_errno2("readlink", abspath);
1687 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1688 *status = GOT_STATUS_MODIFY;
1693 static const struct got_error *
1694 get_file_status(unsigned char *status, struct stat *sb,
1695 struct got_fileindex_entry *ie, const char *abspath,
1696 int dirfd, const char *de_name, struct got_repository *repo)
1698 const struct got_error *err = NULL;
1699 struct got_object_id id;
1704 struct got_blob_object *blob = NULL;
1706 unsigned char staged_status = get_staged_status(ie);
1708 *status = GOT_STATUS_NO_CHANGE;
1711 * Whenever the caller provides a directory descriptor and a
1712 * directory entry name for the file, use them! This prevents
1713 * race conditions if filesystem paths change beneath our feet.
1716 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1717 if (errno == ENOENT) {
1718 if (got_fileindex_entry_has_file_on_disk(ie))
1719 *status = GOT_STATUS_MISSING;
1721 *status = GOT_STATUS_DELETE;
1724 err = got_error_from_errno2("fstatat", abspath);
1728 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1729 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1730 return got_error_from_errno2("open", abspath);
1731 else if (fd == -1 && errno == ELOOP) {
1732 if (lstat(abspath, sb) == -1)
1733 return got_error_from_errno2("lstat", abspath);
1734 } else if (fd == -1 || fstat(fd, sb) == -1) {
1735 if (errno == ENOENT) {
1736 if (got_fileindex_entry_has_file_on_disk(ie))
1737 *status = GOT_STATUS_MISSING;
1739 *status = GOT_STATUS_DELETE;
1742 err = got_error_from_errno2("fstat", abspath);
1747 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1748 *status = GOT_STATUS_OBSTRUCTED;
1752 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1753 *status = GOT_STATUS_DELETE;
1755 } else if (!got_fileindex_entry_has_blob(ie) &&
1756 staged_status != GOT_STATUS_ADD) {
1757 *status = GOT_STATUS_ADD;
1761 if (!stat_info_differs(ie, sb))
1764 if (S_ISLNK(sb->st_mode) &&
1765 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1766 *status = GOT_STATUS_MODIFY;
1770 if (staged_status == GOT_STATUS_MODIFY ||
1771 staged_status == GOT_STATUS_ADD)
1772 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1774 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1776 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1780 if (S_ISLNK(sb->st_mode)) {
1781 err = get_symlink_modification_status(status, ie,
1782 abspath, dirfd, de_name, blob);
1787 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1789 err = got_error_from_errno2("openat", abspath);
1794 f = fdopen(fd, "r");
1796 err = got_error_from_errno2("fdopen", abspath);
1800 hdrlen = got_object_blob_get_hdrlen(blob);
1802 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1803 err = got_object_blob_read_block(&blen, blob);
1806 /* Skip length of blob object header first time around. */
1807 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1808 if (flen == 0 && ferror(f)) {
1809 err = got_error_from_errno("fread");
1812 if (blen - hdrlen == 0) {
1814 *status = GOT_STATUS_MODIFY;
1816 } else if (flen == 0) {
1817 if (blen - hdrlen != 0)
1818 *status = GOT_STATUS_MODIFY;
1820 } else if (blen - hdrlen == flen) {
1821 /* Skip blob object header first time around. */
1822 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1823 *status = GOT_STATUS_MODIFY;
1827 *status = GOT_STATUS_MODIFY;
1833 if (*status == GOT_STATUS_MODIFY) {
1835 err = get_modified_file_content_status(status, f);
1836 } else if (xbit_differs(ie, sb->st_mode))
1837 *status = GOT_STATUS_MODE_CHANGE;
1840 got_object_blob_close(blob);
1841 if (f != NULL && fclose(f) == EOF && err == NULL)
1842 err = got_error_from_errno2("fclose", abspath);
1843 if (fd != -1 && close(fd) == -1 && err == NULL)
1844 err = got_error_from_errno2("close", abspath);
1849 * Update timestamps in the file index if a file is unmodified and
1850 * we had to run a full content comparison to find out.
1852 static const struct got_error *
1853 sync_timestamps(char *ondisk_path, unsigned char status,
1854 struct got_fileindex_entry *ie, struct stat *sb)
1856 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1857 return got_fileindex_entry_update(ie, ondisk_path,
1858 ie->blob_sha1, ie->commit_sha1, 1);
1863 static const struct got_error *
1864 update_blob(struct got_worktree *worktree,
1865 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1866 struct got_tree_entry *te, const char *path,
1867 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1870 const struct got_error *err = NULL;
1871 struct got_blob_object *blob = NULL;
1873 unsigned char status = GOT_STATUS_NO_CHANGE;
1876 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1877 return got_error_from_errno("asprintf");
1880 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1881 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1884 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1888 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1889 sb.st_mode = got_fileindex_perms_to_st(ie);
1891 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1892 status = GOT_STATUS_UNVERSIONED;
1895 if (status == GOT_STATUS_OBSTRUCTED) {
1896 err = (*progress_cb)(progress_arg, status, path);
1899 if (status == GOT_STATUS_CONFLICT) {
1900 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1905 if (ie && status != GOT_STATUS_MISSING &&
1906 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1907 if (got_fileindex_entry_has_commit(ie) &&
1908 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1909 SHA1_DIGEST_LENGTH) == 0) {
1910 err = sync_timestamps(ondisk_path, status, ie, &sb);
1913 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1917 if (got_fileindex_entry_has_blob(ie) &&
1918 memcmp(ie->blob_sha1, te->id.sha1,
1919 SHA1_DIGEST_LENGTH) == 0) {
1920 err = sync_timestamps(ondisk_path, status, ie, &sb);
1925 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1929 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1930 int update_timestamps;
1931 struct got_blob_object *blob2 = NULL;
1932 char *label_orig = NULL;
1933 if (got_fileindex_entry_has_blob(ie)) {
1934 struct got_object_id id2;
1935 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1936 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1940 if (got_fileindex_entry_has_commit(ie)) {
1941 char id_str[SHA1_DIGEST_STRING_LENGTH];
1942 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1943 sizeof(id_str)) == NULL) {
1944 err = got_error_path(id_str,
1945 GOT_ERR_BAD_OBJ_ID_STR);
1948 if (asprintf(&label_orig, "%s: commit %s",
1949 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1950 err = got_error_from_errno("asprintf");
1954 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1956 err = got_object_blob_read_to_str(&link_target, blob);
1959 err = merge_symlink(worktree, blob2, ondisk_path, path,
1960 label_orig, link_target, worktree->base_commit_id,
1961 repo, progress_cb, progress_arg);
1964 err = merge_blob(&update_timestamps, worktree, blob2,
1965 ondisk_path, path, sb.st_mode, label_orig, blob,
1966 worktree->base_commit_id, repo,
1967 progress_cb, progress_arg);
1971 got_object_blob_close(blob2);
1975 * Do not update timestamps of files with local changes.
1976 * Otherwise, a future status walk would treat them as
1977 * unmodified files again.
1979 err = got_fileindex_entry_update(ie, ondisk_path,
1980 blob->id.sha1, worktree->base_commit_id->sha1,
1982 } else if (status == GOT_STATUS_MODE_CHANGE) {
1983 err = got_fileindex_entry_update(ie, ondisk_path,
1984 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1985 } else if (status == GOT_STATUS_DELETE) {
1986 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1989 err = got_fileindex_entry_update(ie, ondisk_path,
1990 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1994 int is_bad_symlink = 0;
1995 if (S_ISLNK(te->mode)) {
1996 err = install_symlink(&is_bad_symlink, worktree,
1997 ondisk_path, path, blob,
1998 status == GOT_STATUS_MISSING, 0,
1999 status == GOT_STATUS_UNVERSIONED, repo,
2000 progress_cb, progress_arg);
2002 err = install_blob(worktree, ondisk_path, path,
2003 te->mode, sb.st_mode, blob,
2004 status == GOT_STATUS_MISSING, 0, 0,
2005 status == GOT_STATUS_UNVERSIONED, repo,
2006 progress_cb, progress_arg);
2012 err = got_fileindex_entry_update(ie, ondisk_path,
2013 blob->id.sha1, worktree->base_commit_id->sha1, 1);
2015 err = create_fileindex_entry(&ie, fileindex,
2016 worktree->base_commit_id, ondisk_path, path,
2022 if (is_bad_symlink) {
2023 got_fileindex_entry_filetype_set(ie,
2024 GOT_FILEIDX_MODE_BAD_SYMLINK);
2027 got_object_blob_close(blob);
2033 static const struct got_error *
2034 remove_ondisk_file(const char *root_path, const char *path)
2036 const struct got_error *err = NULL;
2037 char *ondisk_path = NULL;
2039 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2040 return got_error_from_errno("asprintf");
2042 if (unlink(ondisk_path) == -1) {
2043 if (errno != ENOENT)
2044 err = got_error_from_errno2("unlink", ondisk_path);
2046 char *parent = dirname(ondisk_path);
2047 while (parent && strcmp(parent, root_path) != 0) {
2048 if (rmdir(parent) == -1) {
2049 if (errno != ENOTEMPTY)
2050 err = got_error_from_errno2("rmdir",
2054 parent = dirname(parent);
2061 static const struct got_error *
2062 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2063 struct got_fileindex_entry *ie, struct got_repository *repo,
2064 got_worktree_checkout_cb progress_cb, void *progress_arg)
2066 const struct got_error *err = NULL;
2067 unsigned char status;
2071 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2072 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2074 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2076 return got_error_from_errno("asprintf");
2078 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2082 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2083 char ondisk_target[PATH_MAX];
2084 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2085 sizeof(ondisk_target));
2086 if (ondisk_len == -1) {
2087 err = got_error_from_errno2("readlink", ondisk_path);
2090 ondisk_target[ondisk_len] = '\0';
2091 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2092 NULL, NULL, /* XXX pass common ancestor info? */
2093 ondisk_target, ondisk_path);
2096 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2101 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2102 status == GOT_STATUS_ADD) {
2103 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2107 * Preserve the working file and change the deleted blob's
2108 * entry into a schedule-add entry.
2110 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
2113 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2116 if (status == GOT_STATUS_NO_CHANGE) {
2117 err = remove_ondisk_file(worktree->root_path, ie->path);
2121 got_fileindex_entry_remove(fileindex, ie);
2128 struct diff_cb_arg {
2129 struct got_fileindex *fileindex;
2130 struct got_worktree *worktree;
2131 struct got_repository *repo;
2132 got_worktree_checkout_cb progress_cb;
2134 got_cancel_cb cancel_cb;
2138 static const struct got_error *
2139 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2140 struct got_tree_entry *te, const char *parent_path)
2142 struct diff_cb_arg *a = arg;
2144 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2145 return got_error(GOT_ERR_CANCELLED);
2147 return update_blob(a->worktree, a->fileindex, ie, te,
2148 ie->path, a->repo, a->progress_cb, a->progress_arg);
2151 static const struct got_error *
2152 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2154 struct diff_cb_arg *a = arg;
2156 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2157 return got_error(GOT_ERR_CANCELLED);
2159 return delete_blob(a->worktree, a->fileindex, ie,
2160 a->repo, a->progress_cb, a->progress_arg);
2163 static const struct got_error *
2164 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2166 struct diff_cb_arg *a = arg;
2167 const struct got_error *err;
2170 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2171 return got_error(GOT_ERR_CANCELLED);
2173 if (got_object_tree_entry_is_submodule(te))
2176 if (asprintf(&path, "%s%s%s", parent_path,
2177 parent_path[0] ? "/" : "", te->name)
2179 return got_error_from_errno("asprintf");
2181 if (S_ISDIR(te->mode))
2182 err = add_dir_on_disk(a->worktree, path);
2184 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2185 a->repo, a->progress_cb, a->progress_arg);
2191 const struct got_error *
2192 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2194 uint32_t uuid_status;
2196 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2197 if (uuid_status != uuid_s_ok) {
2199 return got_error_uuid(uuid_status, "uuid_to_string");
2205 static const struct got_error *
2206 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2208 const struct got_error *err = NULL;
2209 char *uuidstr = NULL;
2213 err = got_worktree_get_uuid(&uuidstr, worktree);
2217 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2218 err = got_error_from_errno("asprintf");
2225 const struct got_error *
2226 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2228 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2231 static const struct got_error *
2232 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2234 return get_ref_name(refname, worktree,
2235 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2238 static const struct got_error *
2239 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2241 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2244 static const struct got_error *
2245 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2247 return get_ref_name(refname, worktree,
2248 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2251 static const struct got_error *
2252 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2254 return get_ref_name(refname, worktree,
2255 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2258 static const struct got_error *
2259 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2261 return get_ref_name(refname, worktree,
2262 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2265 static const struct got_error *
2266 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2268 return get_ref_name(refname, worktree,
2269 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2272 static const struct got_error *
2273 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2275 return get_ref_name(refname, worktree,
2276 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2279 static const struct got_error *
2280 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2282 return get_ref_name(refname, worktree,
2283 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2286 const struct got_error *
2287 got_worktree_get_histedit_script_path(char **path,
2288 struct got_worktree *worktree)
2290 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2291 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2293 return got_error_from_errno("asprintf");
2299 * Prevent Git's garbage collector from deleting our base commit by
2300 * setting a reference to our base commit's ID.
2302 static const struct got_error *
2303 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2305 const struct got_error *err = NULL;
2306 struct got_reference *ref = NULL;
2309 err = got_worktree_get_base_ref_name(&refname, worktree);
2313 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2317 err = got_ref_write(ref, repo);
2325 static const struct got_error *
2326 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2328 const struct got_error *err = NULL;
2330 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2331 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2332 err = got_error_from_errno("asprintf");
2333 *fileindex_path = NULL;
2339 static const struct got_error *
2340 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2341 struct got_worktree *worktree)
2343 const struct got_error *err = NULL;
2346 *fileindex_path = NULL;
2347 *fileindex = got_fileindex_alloc();
2348 if (*fileindex == NULL)
2349 return got_error_from_errno("got_fileindex_alloc");
2351 err = get_fileindex_path(fileindex_path, worktree);
2355 index = fopen(*fileindex_path, "rb");
2356 if (index == NULL) {
2357 if (errno != ENOENT)
2358 err = got_error_from_errno2("fopen", *fileindex_path);
2360 err = got_fileindex_read(*fileindex, index);
2361 if (fclose(index) != 0 && err == NULL)
2362 err = got_error_from_errno("fclose");
2366 free(*fileindex_path);
2367 *fileindex_path = NULL;
2368 got_fileindex_free(*fileindex);
2374 struct bump_base_commit_id_arg {
2375 struct got_object_id *base_commit_id;
2378 const char *entry_name;
2379 got_worktree_checkout_cb progress_cb;
2383 /* Bump base commit ID of all files within an updated part of the work tree. */
2384 static const struct got_error *
2385 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2387 const struct got_error *err;
2388 struct bump_base_commit_id_arg *a = arg;
2390 if (a->entry_name) {
2391 if (strcmp(ie->path, a->path) != 0)
2393 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2396 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2397 SHA1_DIGEST_LENGTH) == 0)
2400 if (a->progress_cb) {
2401 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2406 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2410 static const struct got_error *
2411 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2413 const struct got_error *err = NULL;
2414 char *new_fileindex_path = NULL;
2415 FILE *new_index = NULL;
2416 struct timespec timeout;
2418 err = got_opentemp_named(&new_fileindex_path, &new_index,
2423 err = got_fileindex_write(fileindex, new_index);
2427 if (rename(new_fileindex_path, fileindex_path) != 0) {
2428 err = got_error_from_errno3("rename", new_fileindex_path,
2430 unlink(new_fileindex_path);
2434 * Sleep for a short amount of time to ensure that files modified after
2435 * this program exits have a different time stamp from the one which
2436 * was recorded in the file index.
2439 timeout.tv_nsec = 1;
2440 nanosleep(&timeout, NULL);
2444 free(new_fileindex_path);
2448 static const struct got_error *
2449 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2450 struct got_object_id **tree_id, const char *wt_relpath,
2451 struct got_worktree *worktree, struct got_repository *repo)
2453 const struct got_error *err = NULL;
2454 struct got_object_id *id = NULL;
2455 char *in_repo_path = NULL;
2456 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2458 *entry_type = GOT_OBJ_TYPE_ANY;
2459 *tree_relpath = NULL;
2462 if (wt_relpath[0] == '\0') {
2463 /* Check out all files within the work tree. */
2464 *entry_type = GOT_OBJ_TYPE_TREE;
2465 *tree_relpath = strdup("");
2466 if (*tree_relpath == NULL) {
2467 err = got_error_from_errno("strdup");
2470 err = got_object_id_by_path(tree_id, repo,
2471 worktree->base_commit_id, worktree->path_prefix);
2477 /* Check out a subset of files in the work tree. */
2479 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2480 is_root_wt ? "" : "/", wt_relpath) == -1) {
2481 err = got_error_from_errno("asprintf");
2485 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2491 in_repo_path = NULL;
2493 err = got_object_get_type(entry_type, repo, id);
2497 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2498 /* Check out a single file. */
2499 if (strchr(wt_relpath, '/') == NULL) {
2500 /* Check out a single file in work tree's root dir. */
2501 in_repo_path = strdup(worktree->path_prefix);
2502 if (in_repo_path == NULL) {
2503 err = got_error_from_errno("strdup");
2506 *tree_relpath = strdup("");
2507 if (*tree_relpath == NULL) {
2508 err = got_error_from_errno("strdup");
2512 /* Check out a single file in a subdirectory. */
2513 err = got_path_dirname(tree_relpath, wt_relpath);
2516 if (asprintf(&in_repo_path, "%s%s%s",
2517 worktree->path_prefix, is_root_wt ? "" : "/",
2518 *tree_relpath) == -1) {
2519 err = got_error_from_errno("asprintf");
2523 err = got_object_id_by_path(tree_id, repo,
2524 worktree->base_commit_id, in_repo_path);
2526 /* Check out all files within a subdirectory. */
2527 *tree_id = got_object_id_dup(id);
2528 if (*tree_id == NULL) {
2529 err = got_error_from_errno("got_object_id_dup");
2532 *tree_relpath = strdup(wt_relpath);
2533 if (*tree_relpath == NULL) {
2534 err = got_error_from_errno("strdup");
2542 *entry_type = GOT_OBJ_TYPE_ANY;
2543 free(*tree_relpath);
2544 *tree_relpath = NULL;
2551 static const struct got_error *
2552 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2553 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2554 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2555 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2557 const struct got_error *err = NULL;
2558 struct got_commit_object *commit = NULL;
2559 struct got_tree_object *tree = NULL;
2560 struct got_fileindex_diff_tree_cb diff_cb;
2561 struct diff_cb_arg arg;
2563 err = ref_base_commit(worktree, repo);
2565 if (!(err->code == GOT_ERR_ERRNO &&
2566 (errno == EACCES || errno == EROFS)))
2568 err = (*progress_cb)(progress_arg,
2569 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2574 err = got_object_open_as_commit(&commit, repo,
2575 worktree->base_commit_id);
2579 err = got_object_open_as_tree(&tree, repo, tree_id);
2584 got_object_tree_find_entry(tree, entry_name) == NULL) {
2585 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2589 diff_cb.diff_old_new = diff_old_new;
2590 diff_cb.diff_old = diff_old;
2591 diff_cb.diff_new = diff_new;
2592 arg.fileindex = fileindex;
2593 arg.worktree = worktree;
2595 arg.progress_cb = progress_cb;
2596 arg.progress_arg = progress_arg;
2597 arg.cancel_cb = cancel_cb;
2598 arg.cancel_arg = cancel_arg;
2599 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2600 entry_name, repo, &diff_cb, &arg);
2603 got_object_tree_close(tree);
2605 got_object_commit_close(commit);
2609 const struct got_error *
2610 got_worktree_checkout_files(struct got_worktree *worktree,
2611 struct got_pathlist_head *paths, struct got_repository *repo,
2612 got_worktree_checkout_cb progress_cb, void *progress_arg,
2613 got_cancel_cb cancel_cb, void *cancel_arg)
2615 const struct got_error *err = NULL, *sync_err, *unlockerr;
2616 struct got_commit_object *commit = NULL;
2617 struct got_tree_object *tree = NULL;
2618 struct got_fileindex *fileindex = NULL;
2619 char *fileindex_path = NULL;
2620 struct got_pathlist_entry *pe;
2621 struct tree_path_data {
2622 SIMPLEQ_ENTRY(tree_path_data) entry;
2623 struct got_object_id *tree_id;
2628 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2630 SIMPLEQ_INIT(&tree_paths);
2632 err = lock_worktree(worktree, LOCK_EX);
2636 /* Map all specified paths to in-repository trees. */
2637 TAILQ_FOREACH(pe, paths, entry) {
2638 tpd = malloc(sizeof(*tpd));
2640 err = got_error_from_errno("malloc");
2644 err = find_tree_entry_for_checkout(&tpd->entry_type,
2645 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2651 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2652 err = got_path_basename(&tpd->entry_name, pe->path);
2660 tpd->entry_name = NULL;
2662 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2666 * Read the file index.
2667 * Checking out files is supposed to be an idempotent operation.
2668 * If the on-disk file index is incomplete we will try to complete it.
2670 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2674 tpd = SIMPLEQ_FIRST(&tree_paths);
2675 TAILQ_FOREACH(pe, paths, entry) {
2676 struct bump_base_commit_id_arg bbc_arg;
2678 err = checkout_files(worktree, fileindex, tpd->relpath,
2679 tpd->tree_id, tpd->entry_name, repo,
2680 progress_cb, progress_arg, cancel_cb, cancel_arg);
2684 bbc_arg.base_commit_id = worktree->base_commit_id;
2685 bbc_arg.entry_name = tpd->entry_name;
2686 bbc_arg.path = pe->path;
2687 bbc_arg.path_len = pe->path_len;
2688 bbc_arg.progress_cb = progress_cb;
2689 bbc_arg.progress_arg = progress_arg;
2690 err = got_fileindex_for_each_entry_safe(fileindex,
2691 bump_base_commit_id, &bbc_arg);
2695 tpd = SIMPLEQ_NEXT(tpd, entry);
2697 sync_err = sync_fileindex(fileindex, fileindex_path);
2698 if (sync_err && err == NULL)
2701 free(fileindex_path);
2703 got_object_tree_close(tree);
2705 got_object_commit_close(commit);
2707 got_fileindex_free(fileindex);
2708 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2709 tpd = SIMPLEQ_FIRST(&tree_paths);
2710 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2715 unlockerr = lock_worktree(worktree, LOCK_SH);
2716 if (unlockerr && err == NULL)
2721 struct merge_file_cb_arg {
2722 struct got_worktree *worktree;
2723 struct got_fileindex *fileindex;
2724 got_worktree_checkout_cb progress_cb;
2726 got_cancel_cb cancel_cb;
2728 const char *label_orig;
2729 struct got_object_id *commit_id2;
2732 static const struct got_error *
2733 merge_file_cb(void *arg, struct got_blob_object *blob1,
2734 struct got_blob_object *blob2, struct got_object_id *id1,
2735 struct got_object_id *id2, const char *path1, const char *path2,
2736 mode_t mode1, mode_t mode2, struct got_repository *repo)
2738 static const struct got_error *err = NULL;
2739 struct merge_file_cb_arg *a = arg;
2740 struct got_fileindex_entry *ie;
2741 char *ondisk_path = NULL;
2743 unsigned char status;
2744 int local_changes_subsumed;
2746 if (blob1 && blob2) {
2747 ie = got_fileindex_entry_get(a->fileindex, path2,
2750 return (*a->progress_cb)(a->progress_arg,
2751 GOT_STATUS_MISSING, path2);
2753 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2755 return got_error_from_errno("asprintf");
2757 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2762 if (status == GOT_STATUS_DELETE) {
2763 err = (*a->progress_cb)(a->progress_arg,
2764 GOT_STATUS_MERGE, path2);
2767 if (status != GOT_STATUS_NO_CHANGE &&
2768 status != GOT_STATUS_MODIFY &&
2769 status != GOT_STATUS_CONFLICT &&
2770 status != GOT_STATUS_ADD) {
2771 err = (*a->progress_cb)(a->progress_arg, status, path2);
2775 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2777 err = got_object_blob_read_to_str(&link_target2, blob2);
2780 err = merge_symlink(a->worktree, blob1, ondisk_path,
2781 path2, a->label_orig, link_target2, a->commit_id2,
2782 repo, a->progress_cb, a->progress_arg);
2785 err = merge_blob(&local_changes_subsumed, a->worktree,
2786 blob1, ondisk_path, path2, sb.st_mode,
2787 a->label_orig, blob2, a->commit_id2, repo,
2788 a->progress_cb, a->progress_arg);
2791 ie = got_fileindex_entry_get(a->fileindex, path1,
2794 return (*a->progress_cb)(a->progress_arg,
2795 GOT_STATUS_MISSING, path1);
2797 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2799 return got_error_from_errno("asprintf");
2801 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2807 case GOT_STATUS_NO_CHANGE:
2808 err = (*a->progress_cb)(a->progress_arg,
2809 GOT_STATUS_DELETE, path1);
2812 err = remove_ondisk_file(a->worktree->root_path, path1);
2816 got_fileindex_entry_mark_deleted_from_disk(ie);
2818 case GOT_STATUS_DELETE:
2819 case GOT_STATUS_MISSING:
2820 err = (*a->progress_cb)(a->progress_arg,
2821 GOT_STATUS_DELETE, path1);
2825 got_fileindex_entry_mark_deleted_from_disk(ie);
2827 case GOT_STATUS_ADD: {
2828 struct got_object_id *id;
2831 * Delete the added file only if its content already
2832 * exists in the repository.
2834 err = got_object_blob_file_create(&id, &blob1_f, path1);
2837 if (got_object_id_cmp(id, id1) == 0) {
2838 err = (*a->progress_cb)(a->progress_arg,
2839 GOT_STATUS_DELETE, path1);
2842 err = remove_ondisk_file(a->worktree->root_path,
2847 got_fileindex_entry_remove(a->fileindex,
2850 err = (*a->progress_cb)(a->progress_arg,
2851 GOT_STATUS_CANNOT_DELETE, path1);
2853 if (fclose(blob1_f) == EOF && err == NULL)
2854 err = got_error_from_errno("fclose");
2860 case GOT_STATUS_MODIFY:
2861 case GOT_STATUS_CONFLICT:
2862 err = (*a->progress_cb)(a->progress_arg,
2863 GOT_STATUS_CANNOT_DELETE, path1);
2867 case GOT_STATUS_OBSTRUCTED:
2868 err = (*a->progress_cb)(a->progress_arg, status, path1);
2876 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2878 return got_error_from_errno("asprintf");
2879 ie = got_fileindex_entry_get(a->fileindex, path2,
2882 err = get_file_status(&status, &sb, ie, ondisk_path,
2886 if (status != GOT_STATUS_NO_CHANGE &&
2887 status != GOT_STATUS_MODIFY &&
2888 status != GOT_STATUS_CONFLICT &&
2889 status != GOT_STATUS_ADD) {
2890 err = (*a->progress_cb)(a->progress_arg,
2894 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2896 err = got_object_blob_read_to_str(&link_target2,
2900 err = merge_symlink(a->worktree, NULL,
2901 ondisk_path, path2, a->label_orig,
2902 link_target2, a->commit_id2, repo,
2903 a->progress_cb, a->progress_arg);
2905 } else if (S_ISREG(sb.st_mode)) {
2906 err = merge_blob(&local_changes_subsumed,
2907 a->worktree, NULL, ondisk_path, path2,
2908 sb.st_mode, a->label_orig, blob2,
2909 a->commit_id2, repo, a->progress_cb,
2912 err = got_error_path(ondisk_path,
2913 GOT_ERR_FILE_OBSTRUCTED);
2917 if (status == GOT_STATUS_DELETE) {
2918 err = got_fileindex_entry_update(ie,
2919 ondisk_path, blob2->id.sha1,
2920 a->worktree->base_commit_id->sha1, 0);
2925 int is_bad_symlink = 0;
2926 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2927 if (S_ISLNK(mode2)) {
2928 err = install_symlink(&is_bad_symlink,
2929 a->worktree, ondisk_path, path2, blob2, 0,
2930 0, 1, repo, a->progress_cb, a->progress_arg);
2932 err = install_blob(a->worktree, ondisk_path, path2,
2933 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2934 a->progress_cb, a->progress_arg);
2938 err = got_fileindex_entry_alloc(&ie, path2);
2941 err = got_fileindex_entry_update(ie, ondisk_path,
2944 got_fileindex_entry_free(ie);
2947 err = got_fileindex_entry_add(a->fileindex, ie);
2949 got_fileindex_entry_free(ie);
2952 if (is_bad_symlink) {
2953 got_fileindex_entry_filetype_set(ie,
2954 GOT_FILEIDX_MODE_BAD_SYMLINK);
2963 struct check_merge_ok_arg {
2964 struct got_worktree *worktree;
2965 struct got_repository *repo;
2968 static const struct got_error *
2969 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2971 const struct got_error *err = NULL;
2972 struct check_merge_ok_arg *a = arg;
2973 unsigned char status;
2977 /* Reject merges into a work tree with mixed base commits. */
2978 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2979 SHA1_DIGEST_LENGTH))
2980 return got_error(GOT_ERR_MIXED_COMMITS);
2982 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2984 return got_error_from_errno("asprintf");
2986 /* Reject merges into a work tree with conflicted files. */
2987 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2990 if (status == GOT_STATUS_CONFLICT)
2991 return got_error(GOT_ERR_CONFLICTS);
2996 static const struct got_error *
2997 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2998 const char *fileindex_path, struct got_object_id *commit_id1,
2999 struct got_object_id *commit_id2, struct got_repository *repo,
3000 got_worktree_checkout_cb progress_cb, void *progress_arg,
3001 got_cancel_cb cancel_cb, void *cancel_arg)
3003 const struct got_error *err = NULL, *sync_err;
3004 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3005 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3006 struct merge_file_cb_arg arg;
3007 char *label_orig = NULL;
3010 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3011 worktree->path_prefix);
3012 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3018 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3022 err = got_object_id_str(&id_str, commit_id1);
3026 if (asprintf(&label_orig, "%s: commit %s",
3027 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3028 err = got_error_from_errno("asprintf");
3035 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3036 worktree->path_prefix);
3040 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3044 arg.worktree = worktree;
3045 arg.fileindex = fileindex;
3046 arg.progress_cb = progress_cb;
3047 arg.progress_arg = progress_arg;
3048 arg.cancel_cb = cancel_cb;
3049 arg.cancel_arg = cancel_arg;
3050 arg.label_orig = label_orig;
3051 arg.commit_id2 = commit_id2;
3052 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3053 sync_err = sync_fileindex(fileindex, fileindex_path);
3054 if (sync_err && err == NULL)
3058 got_object_tree_close(tree1);
3060 got_object_tree_close(tree2);
3065 const struct got_error *
3066 got_worktree_merge_files(struct got_worktree *worktree,
3067 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3068 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3069 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3071 const struct got_error *err, *unlockerr;
3072 char *fileindex_path = NULL;
3073 struct got_fileindex *fileindex = NULL;
3074 struct check_merge_ok_arg mok_arg;
3076 err = lock_worktree(worktree, LOCK_EX);
3080 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3084 mok_arg.worktree = worktree;
3085 mok_arg.repo = repo;
3086 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
3091 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3092 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
3095 got_fileindex_free(fileindex);
3096 free(fileindex_path);
3097 unlockerr = lock_worktree(worktree, LOCK_SH);
3098 if (unlockerr && err == NULL)
3103 struct diff_dir_cb_arg {
3104 struct got_fileindex *fileindex;
3105 struct got_worktree *worktree;
3106 const char *status_path;
3107 size_t status_path_len;
3108 struct got_repository *repo;
3109 got_worktree_status_cb status_cb;
3111 got_cancel_cb cancel_cb;
3113 /* A pathlist containing per-directory pathlists of ignore patterns. */
3114 struct got_pathlist_head ignores;
3115 int report_unchanged;
3119 static const struct got_error *
3120 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3121 int dirfd, const char *de_name,
3122 got_worktree_status_cb status_cb, void *status_arg,
3123 struct got_repository *repo, int report_unchanged)
3125 const struct got_error *err = NULL;
3126 unsigned char status = GOT_STATUS_NO_CHANGE;
3127 unsigned char staged_status = get_staged_status(ie);
3129 struct got_object_id blob_id, commit_id, staged_blob_id;
3130 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3131 struct got_object_id *staged_blob_idp = NULL;
3133 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3137 if (status == GOT_STATUS_NO_CHANGE &&
3138 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3141 if (got_fileindex_entry_has_blob(ie)) {
3142 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3143 blob_idp = &blob_id;
3145 if (got_fileindex_entry_has_commit(ie)) {
3146 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3147 commit_idp = &commit_id;
3149 if (staged_status == GOT_STATUS_ADD ||
3150 staged_status == GOT_STATUS_MODIFY) {
3151 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3152 SHA1_DIGEST_LENGTH);
3153 staged_blob_idp = &staged_blob_id;
3156 return (*status_cb)(status_arg, status, staged_status,
3157 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3160 static const struct got_error *
3161 status_old_new(void *arg, struct got_fileindex_entry *ie,
3162 struct dirent *de, const char *parent_path, int dirfd)
3164 const struct got_error *err = NULL;
3165 struct diff_dir_cb_arg *a = arg;
3168 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3169 return got_error(GOT_ERR_CANCELLED);
3171 if (got_path_cmp(parent_path, a->status_path,
3172 strlen(parent_path), a->status_path_len) != 0 &&
3173 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3176 if (parent_path[0]) {
3177 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3178 parent_path, de->d_name) == -1)
3179 return got_error_from_errno("asprintf");
3181 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3183 return got_error_from_errno("asprintf");
3186 err = report_file_status(ie, abspath, dirfd, de->d_name,
3187 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3192 static const struct got_error *
3193 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3195 struct diff_dir_cb_arg *a = arg;
3196 struct got_object_id blob_id, commit_id;
3197 unsigned char status;
3199 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3200 return got_error(GOT_ERR_CANCELLED);
3202 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3205 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3206 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3207 if (got_fileindex_entry_has_file_on_disk(ie))
3208 status = GOT_STATUS_MISSING;
3210 status = GOT_STATUS_DELETE;
3211 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3212 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3216 free_ignorelist(struct got_pathlist_head *ignorelist)
3218 struct got_pathlist_entry *pe;
3220 TAILQ_FOREACH(pe, ignorelist, entry)
3221 free((char *)pe->path);
3222 got_pathlist_free(ignorelist);
3226 free_ignores(struct got_pathlist_head *ignores)
3228 struct got_pathlist_entry *pe;
3230 TAILQ_FOREACH(pe, ignores, entry) {
3231 struct got_pathlist_head *ignorelist = pe->data;
3232 free_ignorelist(ignorelist);
3233 free((char *)pe->path);
3235 got_pathlist_free(ignores);
3238 static const struct got_error *
3239 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3241 const struct got_error *err = NULL;
3242 struct got_pathlist_entry *pe = NULL;
3243 struct got_pathlist_head *ignorelist;
3244 char *line = NULL, *pattern, *dirpath = NULL;
3245 size_t linesize = 0;
3248 ignorelist = calloc(1, sizeof(*ignorelist));
3249 if (ignorelist == NULL)
3250 return got_error_from_errno("calloc");
3251 TAILQ_INIT(ignorelist);
3253 while ((linelen = getline(&line, &linesize, f)) != -1) {
3254 if (linelen > 0 && line[linelen - 1] == '\n')
3255 line[linelen - 1] = '\0';
3257 /* Git's ignores may contain comments. */
3261 /* Git's negated patterns are not (yet?) supported. */
3265 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3267 err = got_error_from_errno("asprintf");
3270 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3275 err = got_error_from_errno("getline");
3279 dirpath = strdup(path);
3280 if (dirpath == NULL) {
3281 err = got_error_from_errno("strdup");
3284 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3287 if (err || pe == NULL) {
3289 free_ignorelist(ignorelist);
3295 match_ignores(struct got_pathlist_head *ignores, const char *path)
3297 struct got_pathlist_entry *pe;
3299 /* Handle patterns which match in all directories. */
3300 TAILQ_FOREACH(pe, ignores, entry) {
3301 struct got_pathlist_head *ignorelist = pe->data;
3302 struct got_pathlist_entry *pi;
3304 TAILQ_FOREACH(pi, ignorelist, entry) {
3305 const char *p, *pattern = pi->path;
3307 if (strncmp(pattern, "**/", 3) != 0)
3312 if (fnmatch(pattern, p,
3313 FNM_PATHNAME | FNM_LEADING_DIR)) {
3314 /* Retry in next directory. */
3315 while (*p && *p != '/')
3327 * The ignores pathlist contains ignore lists from children before
3328 * parents, so we can find the most specific ignorelist by walking
3329 * ignores backwards.
3331 pe = TAILQ_LAST(ignores, got_pathlist_head);
3333 if (got_path_is_child(path, pe->path, pe->path_len)) {
3334 struct got_pathlist_head *ignorelist = pe->data;
3335 struct got_pathlist_entry *pi;
3336 TAILQ_FOREACH(pi, ignorelist, entry) {
3337 const char *pattern = pi->path;
3338 int flags = FNM_LEADING_DIR;
3339 if (strstr(pattern, "/**/") == NULL)
3340 flags |= FNM_PATHNAME;
3341 if (fnmatch(pattern, path, flags))
3346 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3352 static const struct got_error *
3353 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3354 const char *path, int dirfd, const char *ignores_filename)
3356 const struct got_error *err = NULL;
3359 FILE *ignoresfile = NULL;
3361 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3362 path[0] ? "/" : "", ignores_filename) == -1)
3363 return got_error_from_errno("asprintf");
3366 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3368 if (errno != ENOENT && errno != EACCES)
3369 err = got_error_from_errno2("openat",
3372 ignoresfile = fdopen(fd, "r");
3373 if (ignoresfile == NULL)
3374 err = got_error_from_errno2("fdopen",
3378 err = read_ignores(ignores, path, ignoresfile);
3382 ignoresfile = fopen(ignorespath, "r");
3383 if (ignoresfile == NULL) {
3384 if (errno != ENOENT && errno != EACCES)
3385 err = got_error_from_errno2("fopen",
3388 err = read_ignores(ignores, path, ignoresfile);
3391 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3392 err = got_error_from_errno2("fclose", path);
3393 if (fd != -1 && close(fd) == -1 && err == NULL)
3394 err = got_error_from_errno2("close", path);
3399 static const struct got_error *
3400 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3402 const struct got_error *err = NULL;
3403 struct diff_dir_cb_arg *a = arg;
3406 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3407 return got_error(GOT_ERR_CANCELLED);
3409 if (parent_path[0]) {
3410 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3411 return got_error_from_errno("asprintf");
3416 if (de->d_type != DT_DIR &&
3417 got_path_is_child(path, a->status_path, a->status_path_len)
3418 && !match_ignores(&a->ignores, path))
3419 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3420 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3426 static const struct got_error *
3427 status_traverse(void *arg, const char *path, int dirfd)
3429 const struct got_error *err = NULL;
3430 struct diff_dir_cb_arg *a = arg;
3435 err = add_ignores(&a->ignores, a->worktree->root_path,
3436 path, dirfd, ".cvsignore");
3440 err = add_ignores(&a->ignores, a->worktree->root_path, path,
3441 dirfd, ".gitignore");
3446 static const struct got_error *
3447 report_single_file_status(const char *path, const char *ondisk_path,
3448 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3449 void *status_arg, struct got_repository *repo, int report_unchanged)
3451 struct got_fileindex_entry *ie;
3454 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3456 return report_file_status(ie, ondisk_path, -1, NULL,
3457 status_cb, status_arg, repo, report_unchanged);
3459 if (lstat(ondisk_path, &sb) == -1) {
3460 if (errno != ENOENT)
3461 return got_error_from_errno2("lstat", ondisk_path);
3462 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3463 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3467 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3468 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3469 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3474 static const struct got_error *
3475 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3476 const char *root_path, const char *path)
3478 const struct got_error *err;
3479 char *parent_path, *next_parent_path = NULL;
3481 err = add_ignores(ignores, root_path, "", -1,
3486 err = add_ignores(ignores, root_path, "", -1,
3491 err = got_path_dirname(&parent_path, path);
3493 if (err->code == GOT_ERR_BAD_PATH)
3494 return NULL; /* cannot traverse parent */
3498 err = add_ignores(ignores, root_path, parent_path, -1,
3502 err = add_ignores(ignores, root_path, parent_path, -1,
3506 err = got_path_dirname(&next_parent_path, parent_path);
3508 if (err->code == GOT_ERR_BAD_PATH)
3509 err = NULL; /* traversed everything */
3513 parent_path = next_parent_path;
3514 next_parent_path = NULL;
3518 free(next_parent_path);
3522 static const struct got_error *
3523 worktree_status(struct got_worktree *worktree, const char *path,
3524 struct got_fileindex *fileindex, struct got_repository *repo,
3525 got_worktree_status_cb status_cb, void *status_arg,
3526 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3527 int report_unchanged)
3529 const struct got_error *err = NULL;
3531 struct got_fileindex_diff_dir_cb fdiff_cb;
3532 struct diff_dir_cb_arg arg;
3533 char *ondisk_path = NULL;
3535 TAILQ_INIT(&arg.ignores);
3537 if (asprintf(&ondisk_path, "%s%s%s",
3538 worktree->root_path, path[0] ? "/" : "", path) == -1)
3539 return got_error_from_errno("asprintf");
3541 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3543 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3545 err = got_error_from_errno2("open", ondisk_path);
3547 err = report_single_file_status(path, ondisk_path,
3548 fileindex, status_cb, status_arg, repo,
3551 fdiff_cb.diff_old_new = status_old_new;
3552 fdiff_cb.diff_old = status_old;
3553 fdiff_cb.diff_new = status_new;
3554 fdiff_cb.diff_traverse = status_traverse;
3555 arg.fileindex = fileindex;
3556 arg.worktree = worktree;
3557 arg.status_path = path;
3558 arg.status_path_len = strlen(path);
3560 arg.status_cb = status_cb;
3561 arg.status_arg = status_arg;
3562 arg.cancel_cb = cancel_cb;
3563 arg.cancel_arg = cancel_arg;
3564 arg.report_unchanged = report_unchanged;
3565 arg.no_ignores = no_ignores;
3567 err = add_ignores_from_parent_paths(&arg.ignores,
3568 worktree->root_path, path);
3572 err = got_fileindex_diff_dir(fileindex, fd,
3573 worktree->root_path, path, repo, &fdiff_cb, &arg);
3576 free_ignores(&arg.ignores);
3577 if (fd != -1 && close(fd) != 0 && err == NULL)
3578 err = got_error_from_errno("close");
3583 const struct got_error *
3584 got_worktree_status(struct got_worktree *worktree,
3585 struct got_pathlist_head *paths, struct got_repository *repo,
3586 got_worktree_status_cb status_cb, void *status_arg,
3587 got_cancel_cb cancel_cb, void *cancel_arg)
3589 const struct got_error *err = NULL;
3590 char *fileindex_path = NULL;
3591 struct got_fileindex *fileindex = NULL;
3592 struct got_pathlist_entry *pe;
3594 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3598 TAILQ_FOREACH(pe, paths, entry) {
3599 err = worktree_status(worktree, pe->path, fileindex, repo,
3600 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3604 free(fileindex_path);
3605 got_fileindex_free(fileindex);
3609 const struct got_error *
3610 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3613 const struct got_error *err = NULL;
3614 char *resolved = NULL, *cwd = NULL, *path = NULL;
3620 cwd = getcwd(NULL, 0);
3622 return got_error_from_errno("getcwd");
3624 if (lstat(arg, &sb) == -1) {
3625 if (errno != ENOENT) {
3626 err = got_error_from_errno2("lstat", arg);
3630 if (S_ISLNK(sb.st_mode)) {
3632 * We cannot use realpath(3) with symlinks since we want to
3633 * operate on the symlink itself.
3634 * But we can make the path absolute, assuming it is relative
3635 * to the current working directory, and then canonicalize it.
3637 char *abspath = NULL;
3638 char canonpath[PATH_MAX];
3639 if (!got_path_is_absolute(arg)) {
3640 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3641 err = got_error_from_errno("asprintf");
3646 err = got_canonpath(abspath ? abspath : arg, canonpath,
3650 resolved = strdup(canonpath);
3651 if (resolved == NULL) {
3652 err = got_error_from_errno("strdup");
3656 resolved = realpath(arg, NULL);
3657 if (resolved == NULL) {
3658 if (errno != ENOENT) {
3659 err = got_error_from_errno2("realpath", arg);
3662 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
3663 err = got_error_from_errno("asprintf");
3669 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3670 strlen(got_worktree_get_root_path(worktree)))) {
3671 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3675 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3676 err = got_path_skip_common_ancestor(&path,
3677 got_worktree_get_root_path(worktree), resolved);
3683 err = got_error_from_errno("strdup");
3688 /* XXX status walk can't deal with trailing slash! */
3690 while (len > 0 && path[len - 1] == '/') {
3691 path[len - 1] = '\0';
3704 struct schedule_addition_args {
3705 struct got_worktree *worktree;
3706 struct got_fileindex *fileindex;
3707 got_worktree_checkout_cb progress_cb;
3709 struct got_repository *repo;
3712 static const struct got_error *
3713 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3714 const char *relpath, struct got_object_id *blob_id,
3715 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3716 int dirfd, const char *de_name)
3718 struct schedule_addition_args *a = arg;
3719 const struct got_error *err = NULL;
3720 struct got_fileindex_entry *ie;
3724 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3726 return got_error_from_errno("asprintf");
3728 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3730 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3734 /* Re-adding an existing entry is a no-op. */
3735 if (status == GOT_STATUS_ADD)
3737 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3742 if (status != GOT_STATUS_UNVERSIONED) {
3743 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3747 err = got_fileindex_entry_alloc(&ie, relpath);
3750 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3752 got_fileindex_entry_free(ie);
3755 err = got_fileindex_entry_add(a->fileindex, ie);
3757 got_fileindex_entry_free(ie);
3764 if (status == GOT_STATUS_ADD)
3766 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3769 const struct got_error *
3770 got_worktree_schedule_add(struct got_worktree *worktree,
3771 struct got_pathlist_head *paths,
3772 got_worktree_checkout_cb progress_cb, void *progress_arg,
3773 struct got_repository *repo, int no_ignores)
3775 struct got_fileindex *fileindex = NULL;
3776 char *fileindex_path = NULL;
3777 const struct got_error *err = NULL, *sync_err, *unlockerr;
3778 struct got_pathlist_entry *pe;
3779 struct schedule_addition_args saa;
3781 err = lock_worktree(worktree, LOCK_EX);
3785 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3789 saa.worktree = worktree;
3790 saa.fileindex = fileindex;
3791 saa.progress_cb = progress_cb;
3792 saa.progress_arg = progress_arg;
3795 TAILQ_FOREACH(pe, paths, entry) {
3796 err = worktree_status(worktree, pe->path, fileindex, repo,
3797 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3801 sync_err = sync_fileindex(fileindex, fileindex_path);
3802 if (sync_err && err == NULL)
3805 free(fileindex_path);
3807 got_fileindex_free(fileindex);
3808 unlockerr = lock_worktree(worktree, LOCK_SH);
3809 if (unlockerr && err == NULL)
3814 struct schedule_deletion_args {
3815 struct got_worktree *worktree;
3816 struct got_fileindex *fileindex;
3817 got_worktree_delete_cb progress_cb;
3819 struct got_repository *repo;
3820 int delete_local_mods;
3822 const char *status_codes;
3825 static const struct got_error *
3826 schedule_for_deletion(void *arg, unsigned char status,
3827 unsigned char staged_status, const char *relpath,
3828 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3829 struct got_object_id *commit_id, int dirfd, const char *de_name)
3831 struct schedule_deletion_args *a = arg;
3832 const struct got_error *err = NULL;
3833 struct got_fileindex_entry *ie = NULL;
3835 char *ondisk_path, *parent = NULL;
3837 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3839 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3841 staged_status = get_staged_status(ie);
3842 if (staged_status != GOT_STATUS_NO_CHANGE) {
3843 if (staged_status == GOT_STATUS_DELETE)
3845 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3848 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3850 return got_error_from_errno("asprintf");
3852 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3857 if (a->status_codes) {
3858 size_t ncodes = strlen(a->status_codes);
3860 for (i = 0; i < ncodes ; i++) {
3861 if (status == a->status_codes[i])
3865 /* Do not delete files in non-matching status. */
3869 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
3870 a->status_codes[i] != GOT_STATUS_MISSING) {
3871 static char msg[64];
3872 snprintf(msg, sizeof(msg),
3873 "invalid status code '%c'", a->status_codes[i]);
3874 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
3879 if (status != GOT_STATUS_NO_CHANGE) {
3880 if (status == GOT_STATUS_DELETE)
3882 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3883 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3886 if (status != GOT_STATUS_MODIFY &&
3887 status != GOT_STATUS_MISSING) {
3888 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3893 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3895 if (unlinkat(dirfd, de_name, 0) != 0) {
3896 err = got_error_from_errno2("unlinkat",
3900 } else if (unlink(ondisk_path) != 0) {
3901 err = got_error_from_errno2("unlink", ondisk_path);
3905 parent = dirname(ondisk_path);
3907 if (parent == NULL) {
3908 err = got_error_from_errno2("dirname", ondisk_path);
3911 while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3912 if (rmdir(parent) == -1) {
3913 if (errno != ENOTEMPTY)
3914 err = got_error_from_errno2("rmdir",
3918 parent = dirname(parent);
3919 if (parent == NULL) {
3920 err = got_error_from_errno2("dirname", parent);
3926 got_fileindex_entry_mark_deleted_from_disk(ie);
3931 if (status == GOT_STATUS_DELETE)
3933 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3934 staged_status, relpath);
3937 const struct got_error *
3938 got_worktree_schedule_delete(struct got_worktree *worktree,
3939 struct got_pathlist_head *paths, int delete_local_mods,
3940 const char *status_codes,
3941 got_worktree_delete_cb progress_cb, void *progress_arg,
3942 struct got_repository *repo, int keep_on_disk)
3944 struct got_fileindex *fileindex = NULL;
3945 char *fileindex_path = NULL;
3946 const struct got_error *err = NULL, *sync_err, *unlockerr;
3947 struct got_pathlist_entry *pe;
3948 struct schedule_deletion_args sda;
3950 err = lock_worktree(worktree, LOCK_EX);
3954 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3958 sda.worktree = worktree;
3959 sda.fileindex = fileindex;
3960 sda.progress_cb = progress_cb;
3961 sda.progress_arg = progress_arg;
3963 sda.delete_local_mods = delete_local_mods;
3964 sda.keep_on_disk = keep_on_disk;
3965 sda.status_codes = status_codes;
3967 TAILQ_FOREACH(pe, paths, entry) {
3968 err = worktree_status(worktree, pe->path, fileindex, repo,
3969 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3973 sync_err = sync_fileindex(fileindex, fileindex_path);
3974 if (sync_err && err == NULL)
3977 free(fileindex_path);
3979 got_fileindex_free(fileindex);
3980 unlockerr = lock_worktree(worktree, LOCK_SH);
3981 if (unlockerr && err == NULL)
3986 static const struct got_error *
3987 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3989 const struct got_error *err = NULL;
3991 size_t linesize = 0, n;
3994 linelen = getline(&line, &linesize, infile);
3995 if (linelen == -1) {
3996 if (ferror(infile)) {
3997 err = got_error_from_errno("getline");
4003 n = fwrite(line, 1, linelen, outfile);
4005 err = got_ferror(outfile, GOT_ERR_IO);
4010 n = fwrite(line, 1, linelen, rejectfile);
4012 err = got_ferror(outfile, GOT_ERR_IO);
4019 static const struct got_error *
4020 skip_one_line(FILE *f)
4023 size_t linesize = 0;
4026 linelen = getline(&line, &linesize, f);
4027 if (linelen == -1) {
4029 return got_error_from_errno("getline");
4036 static const struct got_error *
4037 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4038 int start_old, int end_old, int start_new, int end_new,
4039 FILE *outfile, FILE *rejectfile)
4041 const struct got_error *err;
4043 /* Copy old file's lines leading up to patch. */
4044 while (!feof(f1) && *line_cur1 < start_old) {
4045 err = copy_one_line(f1, outfile, NULL);
4050 /* Skip new file's lines leading up to patch. */
4051 while (!feof(f2) && *line_cur2 < start_new) {
4053 err = copy_one_line(f2, NULL, rejectfile);
4055 err = skip_one_line(f2);
4060 /* Copy patched lines. */
4061 while (!feof(f2) && *line_cur2 <= end_new) {
4062 err = copy_one_line(f2, outfile, NULL);
4067 /* Skip over old file's replaced lines. */
4068 while (!feof(f1) && *line_cur1 <= end_old) {
4070 err = copy_one_line(f1, NULL, rejectfile);
4072 err = skip_one_line(f1);
4081 static const struct got_error *
4082 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4083 FILE *outfile, FILE *rejectfile)
4085 const struct got_error *err;
4088 /* Copy old file's lines until EOF. */
4090 err = copy_one_line(f1, outfile, NULL);
4097 /* Copy new file's lines until EOF. */
4099 err = copy_one_line(f2, NULL, rejectfile);
4109 static const struct got_error *
4110 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
4111 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
4112 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
4113 int *line_cur2, FILE *outfile, FILE *rejectfile,
4114 got_worktree_patch_cb patch_cb, void *patch_arg)
4116 const struct got_error *err = NULL;
4117 int start_old = change->cv.a;
4118 int end_old = change->cv.b;
4119 int start_new = change->cv.c;
4120 int end_new = change->cv.d;
4124 *choice = GOT_PATCH_CHOICE_NONE;
4126 hunkfile = got_opentemp();
4127 if (hunkfile == NULL)
4128 return got_error_from_errno("got_opentemp");
4133 /* XXX TODO needs error checking */
4134 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
4136 if (fseek(f1, pos1, SEEK_SET) == -1) {
4137 err = got_ferror(f1, GOT_ERR_IO);
4140 if (fseek(f2, pos2, SEEK_SET) == -1) {
4141 err = got_ferror(f1, GOT_ERR_IO);
4144 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4145 err = got_ferror(hunkfile, GOT_ERR_IO);
4149 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4150 hunkfile, n, nchanges);
4155 case GOT_PATCH_CHOICE_YES:
4156 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4157 end_old, start_new, end_new, outfile, rejectfile);
4159 case GOT_PATCH_CHOICE_NO:
4160 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4161 end_old, start_new, end_new, rejectfile, outfile);
4163 case GOT_PATCH_CHOICE_QUIT:
4166 err = got_error(GOT_ERR_PATCH_CHOICE);
4170 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4171 err = got_error_from_errno("fclose");
4175 struct revert_file_args {
4176 struct got_worktree *worktree;
4177 struct got_fileindex *fileindex;
4178 got_worktree_checkout_cb progress_cb;
4180 got_worktree_patch_cb patch_cb;
4182 struct got_repository *repo;
4185 static const struct got_error *
4186 create_patched_content(char **path_outfile, int reverse_patch,
4187 struct got_object_id *blob_id, const char *path2,
4188 int dirfd2, const char *de_name2,
4189 const char *relpath, struct got_repository *repo,
4190 got_worktree_patch_cb patch_cb, void *patch_arg)
4192 const struct got_error *err;
4193 struct got_blob_object *blob = NULL;
4194 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4196 char link_target[PATH_MAX];
4197 ssize_t link_len = 0;
4198 char *path1 = NULL, *id_str = NULL;
4199 struct stat sb1, sb2;
4200 struct got_diff_changes *changes = NULL;
4201 struct got_diff_state *ds = NULL;
4202 struct got_diff_args *args = NULL;
4203 struct got_diff_change *change;
4204 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
4207 *path_outfile = NULL;
4209 err = got_object_id_str(&id_str, blob_id);
4214 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4216 if (errno != ELOOP) {
4217 err = got_error_from_errno2("openat", path2);
4220 link_len = readlinkat(dirfd2, de_name2,
4221 link_target, sizeof(link_target));
4223 return got_error_from_errno2("readlinkat", path2);
4224 sb2.st_mode = S_IFLNK;
4225 sb2.st_size = link_len;
4228 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4230 if (errno != ELOOP) {
4231 err = got_error_from_errno2("open", path2);
4234 link_len = readlink(path2, link_target,
4235 sizeof(link_target));
4237 return got_error_from_errno2("readlink", path2);
4238 sb2.st_mode = S_IFLNK;
4239 sb2.st_size = link_len;
4243 if (fstat(fd2, &sb2) == -1) {
4244 err = got_error_from_errno2("fstat", path2);
4248 f2 = fdopen(fd2, "r");
4250 err = got_error_from_errno2("fdopen", path2);
4256 f2 = got_opentemp();
4258 err = got_error_from_errno2("got_opentemp", path2);
4261 n = fwrite(link_target, 1, link_len, f2);
4262 if (n != link_len) {
4263 err = got_ferror(f2, GOT_ERR_IO);
4266 if (fflush(f2) == EOF) {
4267 err = got_error_from_errno("fflush");
4273 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4277 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4281 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4285 if (stat(path1, &sb1) == -1) {
4286 err = got_error_from_errno2("stat", path1);
4290 err = got_diff_files(&changes, &ds, &args, &diff_flags,
4291 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
4295 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4299 if (fseek(f1, 0L, SEEK_SET) == -1)
4300 return got_ferror(f1, GOT_ERR_IO);
4301 if (fseek(f2, 0L, SEEK_SET) == -1)
4302 return got_ferror(f2, GOT_ERR_IO);
4303 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
4305 err = apply_or_reject_change(&choice, change, ++n,
4306 changes->nchanges, ds, args, diff_flags, relpath,
4307 f1, f2, &line_cur1, &line_cur2,
4308 reverse_patch ? NULL : outfile,
4309 reverse_patch ? outfile : NULL,
4310 patch_cb, patch_arg);
4313 if (choice == GOT_PATCH_CHOICE_YES)
4315 else if (choice == GOT_PATCH_CHOICE_QUIT)
4319 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4320 reverse_patch ? NULL : outfile,
4321 reverse_patch ? outfile : NULL);
4325 if (!S_ISLNK(sb2.st_mode)) {
4326 if (chmod(*path_outfile, sb2.st_mode) == -1) {
4327 err = got_error_from_errno2("chmod", path2);
4335 got_object_blob_close(blob);
4336 if (f1 && fclose(f1) == EOF && err == NULL)
4337 err = got_error_from_errno2("fclose", path1);
4338 if (f2 && fclose(f2) == EOF && err == NULL)
4339 err = got_error_from_errno2("fclose", path2);
4340 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4341 err = got_error_from_errno2("close", path2);
4342 if (outfile && fclose(outfile) == EOF && err == NULL)
4343 err = got_error_from_errno2("fclose", *path_outfile);
4344 if (path1 && unlink(path1) == -1 && err == NULL)
4345 err = got_error_from_errno2("unlink", path1);
4346 if (err || !have_content) {
4347 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4348 err = got_error_from_errno2("unlink", *path_outfile);
4349 free(*path_outfile);
4350 *path_outfile = NULL;
4354 got_diff_state_free(ds);
4358 got_diff_free_changes(changes);
4363 static const struct got_error *
4364 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4365 const char *relpath, struct got_object_id *blob_id,
4366 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4367 int dirfd, const char *de_name)
4369 struct revert_file_args *a = arg;
4370 const struct got_error *err = NULL;
4371 char *parent_path = NULL;
4372 struct got_fileindex_entry *ie;
4373 struct got_tree_object *tree = NULL;
4374 struct got_object_id *tree_id = NULL;
4375 const struct got_tree_entry *te = NULL;
4376 char *tree_path = NULL, *te_name;
4377 char *ondisk_path = NULL, *path_content = NULL;
4378 struct got_blob_object *blob = NULL;
4380 /* Reverting a staged deletion is a no-op. */
4381 if (status == GOT_STATUS_DELETE &&
4382 staged_status != GOT_STATUS_NO_CHANGE)
4385 if (status == GOT_STATUS_UNVERSIONED)
4386 return (*a->progress_cb)(a->progress_arg,
4387 GOT_STATUS_UNVERSIONED, relpath);
4389 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4391 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4393 /* Construct in-repository path of tree which contains this blob. */
4394 err = got_path_dirname(&parent_path, ie->path);
4396 if (err->code != GOT_ERR_BAD_PATH)
4398 parent_path = strdup("/");
4399 if (parent_path == NULL) {
4400 err = got_error_from_errno("strdup");
4404 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4405 tree_path = strdup(parent_path);
4406 if (tree_path == NULL) {
4407 err = got_error_from_errno("strdup");
4411 if (got_path_is_root_dir(parent_path)) {
4412 tree_path = strdup(a->worktree->path_prefix);
4413 if (tree_path == NULL) {
4414 err = got_error_from_errno("strdup");
4418 if (asprintf(&tree_path, "%s/%s",
4419 a->worktree->path_prefix, parent_path) == -1) {
4420 err = got_error_from_errno("asprintf");
4426 err = got_object_id_by_path(&tree_id, a->repo,
4427 a->worktree->base_commit_id, tree_path);
4429 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4430 (status == GOT_STATUS_ADD ||
4431 staged_status == GOT_STATUS_ADD)))
4434 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4438 err = got_path_basename(&te_name, ie->path);
4442 te = got_object_tree_find_entry(tree, te_name);
4444 if (te == NULL && status != GOT_STATUS_ADD &&
4445 staged_status != GOT_STATUS_ADD) {
4446 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4452 case GOT_STATUS_ADD:
4454 int choice = GOT_PATCH_CHOICE_NONE;
4455 err = (*a->patch_cb)(&choice, a->patch_arg,
4456 status, ie->path, NULL, 1, 1);
4459 if (choice != GOT_PATCH_CHOICE_YES)
4462 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4466 got_fileindex_entry_remove(a->fileindex, ie);
4468 case GOT_STATUS_DELETE:
4470 int choice = GOT_PATCH_CHOICE_NONE;
4471 err = (*a->patch_cb)(&choice, a->patch_arg,
4472 status, ie->path, NULL, 1, 1);
4475 if (choice != GOT_PATCH_CHOICE_YES)
4479 case GOT_STATUS_MODIFY:
4480 case GOT_STATUS_MODE_CHANGE:
4481 case GOT_STATUS_CONFLICT:
4482 case GOT_STATUS_MISSING: {
4483 struct got_object_id id;
4484 if (staged_status == GOT_STATUS_ADD ||
4485 staged_status == GOT_STATUS_MODIFY) {
4486 memcpy(id.sha1, ie->staged_blob_sha1,
4487 SHA1_DIGEST_LENGTH);
4489 memcpy(id.sha1, ie->blob_sha1,
4490 SHA1_DIGEST_LENGTH);
4491 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4495 if (asprintf(&ondisk_path, "%s/%s",
4496 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4497 err = got_error_from_errno("asprintf");
4501 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4502 status == GOT_STATUS_CONFLICT)) {
4503 int is_bad_symlink = 0;
4504 err = create_patched_content(&path_content, 1, &id,
4505 ondisk_path, dirfd, de_name, ie->path, a->repo,
4506 a->patch_cb, a->patch_arg);
4507 if (err || path_content == NULL)
4509 if (te && S_ISLNK(te->mode)) {
4510 if (unlink(path_content) == -1) {
4511 err = got_error_from_errno2("unlink",
4515 err = install_symlink(&is_bad_symlink,
4516 a->worktree, ondisk_path, ie->path,
4517 blob, 0, 1, 0, a->repo,
4518 a->progress_cb, a->progress_arg);
4520 if (rename(path_content, ondisk_path) == -1) {
4521 err = got_error_from_errno3("rename",
4522 path_content, ondisk_path);
4527 int is_bad_symlink = 0;
4528 if (te && S_ISLNK(te->mode)) {
4529 err = install_symlink(&is_bad_symlink,
4530 a->worktree, ondisk_path, ie->path,
4531 blob, 0, 1, 0, a->repo,
4532 a->progress_cb, a->progress_arg);
4534 err = install_blob(a->worktree, ondisk_path,
4536 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4537 got_fileindex_perms_to_st(ie), blob,
4538 0, 1, 0, 0, a->repo,
4539 a->progress_cb, a->progress_arg);
4543 if (status == GOT_STATUS_DELETE ||
4544 status == GOT_STATUS_MODE_CHANGE) {
4545 err = got_fileindex_entry_update(ie,
4546 ondisk_path, blob->id.sha1,
4547 a->worktree->base_commit_id->sha1, 1);
4551 if (is_bad_symlink) {
4552 got_fileindex_entry_filetype_set(ie,
4553 GOT_FILEIDX_MODE_BAD_SYMLINK);
4567 got_object_blob_close(blob);
4569 got_object_tree_close(tree);
4574 const struct got_error *
4575 got_worktree_revert(struct got_worktree *worktree,
4576 struct got_pathlist_head *paths,
4577 got_worktree_checkout_cb progress_cb, void *progress_arg,
4578 got_worktree_patch_cb patch_cb, void *patch_arg,
4579 struct got_repository *repo)
4581 struct got_fileindex *fileindex = NULL;
4582 char *fileindex_path = NULL;
4583 const struct got_error *err = NULL, *unlockerr = NULL;
4584 const struct got_error *sync_err = NULL;
4585 struct got_pathlist_entry *pe;
4586 struct revert_file_args rfa;
4588 err = lock_worktree(worktree, LOCK_EX);
4592 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4596 rfa.worktree = worktree;
4597 rfa.fileindex = fileindex;
4598 rfa.progress_cb = progress_cb;
4599 rfa.progress_arg = progress_arg;
4600 rfa.patch_cb = patch_cb;
4601 rfa.patch_arg = patch_arg;
4603 TAILQ_FOREACH(pe, paths, entry) {
4604 err = worktree_status(worktree, pe->path, fileindex, repo,
4605 revert_file, &rfa, NULL, NULL, 0, 0);
4609 sync_err = sync_fileindex(fileindex, fileindex_path);
4610 if (sync_err && err == NULL)
4613 free(fileindex_path);
4615 got_fileindex_free(fileindex);
4616 unlockerr = lock_worktree(worktree, LOCK_SH);
4617 if (unlockerr && err == NULL)
4623 free_commitable(struct got_commitable *ct)
4626 free(ct->in_repo_path);
4627 free(ct->ondisk_path);
4629 free(ct->base_blob_id);
4630 free(ct->staged_blob_id);
4631 free(ct->base_commit_id);
4635 struct collect_commitables_arg {
4636 struct got_pathlist_head *commitable_paths;
4637 struct got_repository *repo;
4638 struct got_worktree *worktree;
4639 struct got_fileindex *fileindex;
4640 int have_staged_files;
4641 int allow_bad_symlinks;
4644 static const struct got_error *
4645 collect_commitables(void *arg, unsigned char status,
4646 unsigned char staged_status, const char *relpath,
4647 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4648 struct got_object_id *commit_id, int dirfd, const char *de_name)
4650 struct collect_commitables_arg *a = arg;
4651 const struct got_error *err = NULL;
4652 struct got_commitable *ct = NULL;
4653 struct got_pathlist_entry *new = NULL;
4654 char *parent_path = NULL, *path = NULL;
4657 if (a->have_staged_files) {
4658 if (staged_status != GOT_STATUS_MODIFY &&
4659 staged_status != GOT_STATUS_ADD &&
4660 staged_status != GOT_STATUS_DELETE)
4663 if (status == GOT_STATUS_CONFLICT)
4664 return got_error(GOT_ERR_COMMIT_CONFLICT);
4666 if (status != GOT_STATUS_MODIFY &&
4667 status != GOT_STATUS_MODE_CHANGE &&
4668 status != GOT_STATUS_ADD &&
4669 status != GOT_STATUS_DELETE)
4673 if (asprintf(&path, "/%s", relpath) == -1) {
4674 err = got_error_from_errno("asprintf");
4677 if (strcmp(path, "/") == 0) {
4678 parent_path = strdup("");
4679 if (parent_path == NULL)
4680 return got_error_from_errno("strdup");
4682 err = got_path_dirname(&parent_path, path);
4687 ct = calloc(1, sizeof(*ct));
4689 err = got_error_from_errno("calloc");
4693 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4695 err = got_error_from_errno("asprintf");
4699 if (staged_status == GOT_STATUS_ADD ||
4700 staged_status == GOT_STATUS_MODIFY) {
4701 struct got_fileindex_entry *ie;
4702 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4703 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4704 case GOT_FILEIDX_MODE_REGULAR_FILE:
4705 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4708 case GOT_FILEIDX_MODE_SYMLINK:
4712 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4715 ct->mode |= got_fileindex_entry_perms_get(ie);
4716 } else if (status != GOT_STATUS_DELETE &&
4717 staged_status != GOT_STATUS_DELETE) {
4719 if (fstatat(dirfd, de_name, &sb,
4720 AT_SYMLINK_NOFOLLOW) == -1) {
4721 err = got_error_from_errno2("fstatat",
4725 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4726 err = got_error_from_errno2("lstat", ct->ondisk_path);
4729 ct->mode = sb.st_mode;
4732 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4733 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4735 err = got_error_from_errno("asprintf");
4739 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4740 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4742 char target_path[PATH_MAX];
4744 target_len = readlink(ct->ondisk_path, target_path,
4745 sizeof(target_path));
4746 if (target_len == -1) {
4747 err = got_error_from_errno2("readlink",
4751 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4752 target_len, ct->ondisk_path, a->worktree->root_path);
4755 if (is_bad_symlink) {
4756 err = got_error_path(ct->ondisk_path,
4757 GOT_ERR_BAD_SYMLINK);
4763 ct->status = status;
4764 ct->staged_status = staged_status;
4765 ct->blob_id = NULL; /* will be filled in when blob gets created */
4766 if (ct->status != GOT_STATUS_ADD &&
4767 ct->staged_status != GOT_STATUS_ADD) {
4768 ct->base_blob_id = got_object_id_dup(blob_id);
4769 if (ct->base_blob_id == NULL) {
4770 err = got_error_from_errno("got_object_id_dup");
4773 ct->base_commit_id = got_object_id_dup(commit_id);
4774 if (ct->base_commit_id == NULL) {
4775 err = got_error_from_errno("got_object_id_dup");
4779 if (ct->staged_status == GOT_STATUS_ADD ||
4780 ct->staged_status == GOT_STATUS_MODIFY) {
4781 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4782 if (ct->staged_blob_id == NULL) {
4783 err = got_error_from_errno("got_object_id_dup");
4787 ct->path = strdup(path);
4788 if (ct->path == NULL) {
4789 err = got_error_from_errno("strdup");
4792 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4794 if (ct && (err || new == NULL))
4795 free_commitable(ct);
4801 static const struct got_error *write_tree(struct got_object_id **, int *,
4802 struct got_tree_object *, const char *, struct got_pathlist_head *,
4803 got_worktree_status_cb status_cb, void *status_arg,
4804 struct got_repository *);
4806 static const struct got_error *
4807 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4808 struct got_tree_entry *te, const char *parent_path,
4809 struct got_pathlist_head *commitable_paths,
4810 got_worktree_status_cb status_cb, void *status_arg,
4811 struct got_repository *repo)
4813 const struct got_error *err = NULL;
4814 struct got_tree_object *subtree;
4817 if (asprintf(&subpath, "%s%s%s", parent_path,
4818 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4819 return got_error_from_errno("asprintf");
4821 err = got_object_open_as_tree(&subtree, repo, &te->id);
4825 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4826 commitable_paths, status_cb, status_arg, repo);
4827 got_object_tree_close(subtree);
4832 static const struct got_error *
4833 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4835 const struct got_error *err = NULL;
4836 char *ct_parent_path = NULL;
4840 if (strchr(ct->in_repo_path, '/') == NULL) {
4841 *match = got_path_is_root_dir(path);
4845 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4848 *match = (strcmp(path, ct_parent_path) == 0);
4849 free(ct_parent_path);
4854 get_ct_file_mode(struct got_commitable *ct)
4856 if (S_ISLNK(ct->mode))
4859 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4862 static const struct got_error *
4863 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4864 struct got_tree_entry *te, struct got_commitable *ct)
4866 const struct got_error *err = NULL;
4870 err = got_object_tree_entry_dup(new_te, te);
4874 (*new_te)->mode = get_ct_file_mode(ct);
4876 if (ct->staged_status == GOT_STATUS_MODIFY)
4877 memcpy(&(*new_te)->id, ct->staged_blob_id,
4878 sizeof((*new_te)->id));
4880 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4882 if (err && *new_te) {
4889 static const struct got_error *
4890 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4891 struct got_commitable *ct)
4893 const struct got_error *err = NULL;
4894 char *ct_name = NULL;
4898 *new_te = calloc(1, sizeof(**new_te));
4899 if (*new_te == NULL)
4900 return got_error_from_errno("calloc");
4902 err = got_path_basename(&ct_name, ct->path);
4905 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4906 sizeof((*new_te)->name)) {
4907 err = got_error(GOT_ERR_NO_SPACE);
4911 (*new_te)->mode = get_ct_file_mode(ct);
4913 if (ct->staged_status == GOT_STATUS_ADD)
4914 memcpy(&(*new_te)->id, ct->staged_blob_id,
4915 sizeof((*new_te)->id));
4917 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4920 if (err && *new_te) {
4927 static const struct got_error *
4928 insert_tree_entry(struct got_tree_entry *new_te,
4929 struct got_pathlist_head *paths)
4931 const struct got_error *err = NULL;
4932 struct got_pathlist_entry *new_pe;
4934 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4938 return got_error(GOT_ERR_TREE_DUP_ENTRY);
4942 static const struct got_error *
4943 report_ct_status(struct got_commitable *ct,
4944 got_worktree_status_cb status_cb, void *status_arg)
4946 const char *ct_path = ct->path;
4947 unsigned char status;
4949 while (ct_path[0] == '/')
4952 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4953 status = ct->staged_status;
4955 status = ct->status;
4957 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4958 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4961 static const struct got_error *
4962 match_modified_subtree(int *modified, struct got_tree_entry *te,
4963 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4965 const struct got_error *err = NULL;
4966 struct got_pathlist_entry *pe;
4971 if (asprintf(&te_path, "%s%s%s", base_tree_path,
4972 got_path_is_root_dir(base_tree_path) ? "" : "/",
4974 return got_error_from_errno("asprintf");
4976 TAILQ_FOREACH(pe, commitable_paths, entry) {
4977 struct got_commitable *ct = pe->data;
4978 *modified = got_path_is_child(ct->in_repo_path, te_path,
4988 static const struct got_error *
4989 match_deleted_or_modified_ct(struct got_commitable **ctp,
4990 struct got_tree_entry *te, const char *base_tree_path,
4991 struct got_pathlist_head *commitable_paths)
4993 const struct got_error *err = NULL;
4994 struct got_pathlist_entry *pe;
4998 TAILQ_FOREACH(pe, commitable_paths, entry) {
4999 struct got_commitable *ct = pe->data;
5000 char *ct_name = NULL;
5003 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5004 if (ct->status != GOT_STATUS_MODIFY &&
5005 ct->status != GOT_STATUS_MODE_CHANGE &&
5006 ct->status != GOT_STATUS_DELETE)
5009 if (ct->staged_status != GOT_STATUS_MODIFY &&
5010 ct->staged_status != GOT_STATUS_DELETE)
5014 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5017 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5023 err = got_path_basename(&ct_name, pe->path);
5027 if (strcmp(te->name, ct_name) != 0) {
5040 static const struct got_error *
5041 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5042 const char *child_path, const char *path_base_tree,
5043 struct got_pathlist_head *commitable_paths,
5044 got_worktree_status_cb status_cb, void *status_arg,
5045 struct got_repository *repo)
5047 const struct got_error *err = NULL;
5048 struct got_tree_entry *new_te;
5050 struct got_object_id *id = NULL;
5055 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5056 got_path_is_root_dir(path_base_tree) ? "" : "/",
5058 return got_error_from_errno("asprintf");
5060 new_te = calloc(1, sizeof(*new_te));
5062 return got_error_from_errno("calloc");
5063 new_te->mode = S_IFDIR;
5065 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5066 sizeof(new_te->name)) {
5067 err = got_error(GOT_ERR_NO_SPACE);
5070 err = write_tree(&id, &nentries, NULL, subtree_path,
5071 commitable_paths, status_cb, status_arg, repo);
5076 memcpy(&new_te->id, id, sizeof(new_te->id));
5085 static const struct got_error *
5086 write_tree(struct got_object_id **new_tree_id, int *nentries,
5087 struct got_tree_object *base_tree, const char *path_base_tree,
5088 struct got_pathlist_head *commitable_paths,
5089 got_worktree_status_cb status_cb, void *status_arg,
5090 struct got_repository *repo)
5092 const struct got_error *err = NULL;
5093 struct got_pathlist_head paths;
5094 struct got_tree_entry *te, *new_te = NULL;
5095 struct got_pathlist_entry *pe;
5100 /* Insert, and recurse into, newly added entries first. */
5101 TAILQ_FOREACH(pe, commitable_paths, entry) {
5102 struct got_commitable *ct = pe->data;
5103 char *child_path = NULL, *slash;
5105 if ((ct->status != GOT_STATUS_ADD &&
5106 ct->staged_status != GOT_STATUS_ADD) ||
5107 (ct->flags & GOT_COMMITABLE_ADDED))
5110 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5111 strlen(path_base_tree)))
5114 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5119 slash = strchr(child_path, '/');
5120 if (slash == NULL) {
5121 err = alloc_added_blob_tree_entry(&new_te, ct);
5124 err = report_ct_status(ct, status_cb, status_arg);
5127 ct->flags |= GOT_COMMITABLE_ADDED;
5128 err = insert_tree_entry(new_te, &paths);
5133 *slash = '\0'; /* trim trailing path components */
5134 if (base_tree == NULL ||
5135 got_object_tree_find_entry(base_tree, child_path)
5137 err = make_subtree_for_added_blob(&new_te,
5138 child_path, path_base_tree,
5139 commitable_paths, status_cb, status_arg,
5143 err = insert_tree_entry(new_te, &paths);
5152 int i, nbase_entries;
5153 /* Handle modified and deleted entries. */
5154 nbase_entries = got_object_tree_get_nentries(base_tree);
5155 for (i = 0; i < nbase_entries; i++) {
5156 struct got_commitable *ct = NULL;
5158 te = got_object_tree_get_entry(base_tree, i);
5159 if (got_object_tree_entry_is_submodule(te)) {
5160 /* Entry is a submodule; just copy it. */
5161 err = got_object_tree_entry_dup(&new_te, te);
5164 err = insert_tree_entry(new_te, &paths);
5171 if (S_ISDIR(te->mode)) {
5173 err = got_object_tree_entry_dup(&new_te, te);
5176 err = match_modified_subtree(&modified, te,
5177 path_base_tree, commitable_paths);
5180 /* Avoid recursion into unmodified subtrees. */
5182 struct got_object_id *new_id;
5184 err = write_subtree(&new_id,
5186 path_base_tree, commitable_paths,
5187 status_cb, status_arg, repo);
5190 if (nsubentries == 0) {
5191 /* All entries were deleted. */
5195 memcpy(&new_te->id, new_id,
5196 sizeof(new_te->id));
5199 err = insert_tree_entry(new_te, &paths);
5206 err = match_deleted_or_modified_ct(&ct, te,
5207 path_base_tree, commitable_paths);
5211 /* NB: Deleted entries get dropped here. */
5212 if (ct->status == GOT_STATUS_MODIFY ||
5213 ct->status == GOT_STATUS_MODE_CHANGE ||
5214 ct->staged_status == GOT_STATUS_MODIFY) {
5215 err = alloc_modified_blob_tree_entry(
5219 err = insert_tree_entry(new_te, &paths);
5224 err = report_ct_status(ct, status_cb,
5229 /* Entry is unchanged; just copy it. */
5230 err = got_object_tree_entry_dup(&new_te, te);
5233 err = insert_tree_entry(new_te, &paths);
5241 /* Write new list of entries; deleted entries have been dropped. */
5242 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5244 got_pathlist_free(&paths);
5248 static const struct got_error *
5249 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
5250 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
5251 int have_staged_files)
5253 const struct got_error *err = NULL;
5254 struct got_pathlist_entry *pe;
5256 TAILQ_FOREACH(pe, commitable_paths, entry) {
5257 struct got_fileindex_entry *ie;
5258 struct got_commitable *ct = pe->data;
5260 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5262 if (ct->status == GOT_STATUS_DELETE ||
5263 ct->staged_status == GOT_STATUS_DELETE) {
5264 got_fileindex_entry_remove(fileindex, ie);
5265 } else if (ct->staged_status == GOT_STATUS_ADD ||
5266 ct->staged_status == GOT_STATUS_MODIFY) {
5267 got_fileindex_entry_stage_set(ie,
5268 GOT_FILEIDX_STAGE_NONE);
5269 err = got_fileindex_entry_update(ie,
5270 ct->ondisk_path, ct->staged_blob_id->sha1,
5271 new_base_commit_id->sha1,
5272 !have_staged_files);
5274 err = got_fileindex_entry_update(ie,
5275 ct->ondisk_path, ct->blob_id->sha1,
5276 new_base_commit_id->sha1,
5277 !have_staged_files);
5279 err = got_fileindex_entry_alloc(&ie, pe->path);
5282 err = got_fileindex_entry_update(ie, ct->ondisk_path,
5283 ct->blob_id->sha1, new_base_commit_id->sha1, 1);
5285 got_fileindex_entry_free(ie);
5288 err = got_fileindex_entry_add(fileindex, ie);
5290 got_fileindex_entry_free(ie);
5299 static const struct got_error *
5300 check_out_of_date(const char *in_repo_path, unsigned char status,
5301 unsigned char staged_status, struct got_object_id *base_blob_id,
5302 struct got_object_id *base_commit_id,
5303 struct got_object_id *head_commit_id, struct got_repository *repo,
5306 const struct got_error *err = NULL;
5307 struct got_object_id *id = NULL;
5309 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5310 /* Trivial case: base commit == head commit */
5311 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5314 * Ensure file content which local changes were based
5315 * on matches file content in the branch head.
5317 err = got_object_id_by_path(&id, repo, head_commit_id,
5320 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5321 err = got_error(ood_errcode);
5323 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5324 err = got_error(ood_errcode);
5326 /* Require that added files don't exist in the branch head. */
5327 err = got_object_id_by_path(&id, repo, head_commit_id,
5329 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5331 err = id ? got_error(ood_errcode) : NULL;
5338 const struct got_error *
5339 commit_worktree(struct got_object_id **new_commit_id,
5340 struct got_pathlist_head *commitable_paths,
5341 struct got_object_id *head_commit_id, struct got_worktree *worktree,
5342 const char *author, const char *committer,
5343 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5344 got_worktree_status_cb status_cb, void *status_arg,
5345 struct got_repository *repo)
5347 const struct got_error *err = NULL, *unlockerr = NULL;
5348 struct got_pathlist_entry *pe;
5349 const char *head_ref_name = NULL;
5350 struct got_commit_object *head_commit = NULL;
5351 struct got_reference *head_ref2 = NULL;
5352 struct got_object_id *head_commit_id2 = NULL;
5353 struct got_tree_object *head_tree = NULL;
5354 struct got_object_id *new_tree_id = NULL;
5356 struct got_object_id_queue parent_ids;
5357 struct got_object_qid *pid = NULL;
5358 char *logmsg = NULL;
5360 *new_commit_id = NULL;
5362 SIMPLEQ_INIT(&parent_ids);
5364 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5368 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5372 if (commit_msg_cb != NULL) {
5373 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5378 if (logmsg == NULL || strlen(logmsg) == 0) {
5379 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5383 /* Create blobs from added and modified files and record their IDs. */
5384 TAILQ_FOREACH(pe, commitable_paths, entry) {
5385 struct got_commitable *ct = pe->data;
5388 /* Blobs for staged files already exist. */
5389 if (ct->staged_status == GOT_STATUS_ADD ||
5390 ct->staged_status == GOT_STATUS_MODIFY)
5393 if (ct->status != GOT_STATUS_ADD &&
5394 ct->status != GOT_STATUS_MODIFY &&
5395 ct->status != GOT_STATUS_MODE_CHANGE)
5398 if (asprintf(&ondisk_path, "%s/%s",
5399 worktree->root_path, pe->path) == -1) {
5400 err = got_error_from_errno("asprintf");
5403 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5409 /* Recursively write new tree objects. */
5410 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5411 commitable_paths, status_cb, status_arg, repo);
5415 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5418 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5419 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5420 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5421 got_object_qid_free(pid);
5427 /* Check if a concurrent commit to our branch has occurred. */
5428 head_ref_name = got_worktree_get_head_ref_name(worktree);
5429 if (head_ref_name == NULL) {
5430 err = got_error_from_errno("got_worktree_get_head_ref_name");
5433 /* Lock the reference here to prevent concurrent modification. */
5434 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5437 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5440 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5441 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5444 /* Update branch head in repository. */
5445 err = got_ref_change_ref(head_ref2, *new_commit_id);
5448 err = got_ref_write(head_ref2, repo);
5452 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5456 err = ref_base_commit(worktree, repo);
5461 got_object_tree_close(head_tree);
5463 got_object_commit_close(head_commit);
5464 free(head_commit_id2);
5466 unlockerr = got_ref_unlock(head_ref2);
5467 if (unlockerr && err == NULL)
5469 got_ref_close(head_ref2);
5474 static const struct got_error *
5475 check_path_is_commitable(const char *path,
5476 struct got_pathlist_head *commitable_paths)
5478 struct got_pathlist_entry *cpe = NULL;
5479 size_t path_len = strlen(path);
5481 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5482 struct got_commitable *ct = cpe->data;
5483 const char *ct_path = ct->path;
5485 while (ct_path[0] == '/')
5488 if (strcmp(path, ct_path) == 0 ||
5489 got_path_is_child(ct_path, path, path_len))
5494 return got_error_path(path, GOT_ERR_BAD_PATH);
5499 static const struct got_error *
5500 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5502 int *have_staged_files = arg;
5504 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5505 *have_staged_files = 1;
5506 return got_error(GOT_ERR_CANCELLED);
5512 static const struct got_error *
5513 check_non_staged_files(struct got_fileindex *fileindex,
5514 struct got_pathlist_head *paths)
5516 struct got_pathlist_entry *pe;
5517 struct got_fileindex_entry *ie;
5519 TAILQ_FOREACH(pe, paths, entry) {
5520 if (pe->path[0] == '\0')
5522 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5524 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5525 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5526 return got_error_path(pe->path,
5527 GOT_ERR_FILE_NOT_STAGED);
5533 const struct got_error *
5534 got_worktree_commit(struct got_object_id **new_commit_id,
5535 struct got_worktree *worktree, struct got_pathlist_head *paths,
5536 const char *author, const char *committer, int allow_bad_symlinks,
5537 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5538 got_worktree_status_cb status_cb, void *status_arg,
5539 struct got_repository *repo)
5541 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5542 struct got_fileindex *fileindex = NULL;
5543 char *fileindex_path = NULL;
5544 struct got_pathlist_head commitable_paths;
5545 struct collect_commitables_arg cc_arg;
5546 struct got_pathlist_entry *pe;
5547 struct got_reference *head_ref = NULL;
5548 struct got_object_id *head_commit_id = NULL;
5549 int have_staged_files = 0;
5551 *new_commit_id = NULL;
5553 TAILQ_INIT(&commitable_paths);
5555 err = lock_worktree(worktree, LOCK_EX);
5559 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5563 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5567 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5571 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5572 &have_staged_files);
5573 if (err && err->code != GOT_ERR_CANCELLED)
5575 if (have_staged_files) {
5576 err = check_non_staged_files(fileindex, paths);
5581 cc_arg.commitable_paths = &commitable_paths;
5582 cc_arg.worktree = worktree;
5583 cc_arg.fileindex = fileindex;
5585 cc_arg.have_staged_files = have_staged_files;
5586 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5587 TAILQ_FOREACH(pe, paths, entry) {
5588 err = worktree_status(worktree, pe->path, fileindex, repo,
5589 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5594 if (TAILQ_EMPTY(&commitable_paths)) {
5595 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5599 TAILQ_FOREACH(pe, paths, entry) {
5600 err = check_path_is_commitable(pe->path, &commitable_paths);
5605 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5606 struct got_commitable *ct = pe->data;
5607 const char *ct_path = ct->in_repo_path;
5609 while (ct_path[0] == '/')
5611 err = check_out_of_date(ct_path, ct->status,
5612 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5613 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5619 err = commit_worktree(new_commit_id, &commitable_paths,
5620 head_commit_id, worktree, author, committer,
5621 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5625 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5626 fileindex, have_staged_files);
5627 sync_err = sync_fileindex(fileindex, fileindex_path);
5628 if (sync_err && err == NULL)
5632 got_fileindex_free(fileindex);
5633 free(fileindex_path);
5634 unlockerr = lock_worktree(worktree, LOCK_SH);
5635 if (unlockerr && err == NULL)
5637 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5638 struct got_commitable *ct = pe->data;
5639 free_commitable(ct);
5641 got_pathlist_free(&commitable_paths);
5646 got_commitable_get_path(struct got_commitable *ct)
5652 got_commitable_get_status(struct got_commitable *ct)
5657 struct check_rebase_ok_arg {
5658 struct got_worktree *worktree;
5659 struct got_repository *repo;
5662 static const struct got_error *
5663 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5665 const struct got_error *err = NULL;
5666 struct check_rebase_ok_arg *a = arg;
5667 unsigned char status;
5671 /* Reject rebase of a work tree with mixed base commits. */
5672 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5673 SHA1_DIGEST_LENGTH))
5674 return got_error(GOT_ERR_MIXED_COMMITS);
5676 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5678 return got_error_from_errno("asprintf");
5680 /* Reject rebase of a work tree with modified or staged files. */
5681 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5686 if (status != GOT_STATUS_NO_CHANGE)
5687 return got_error(GOT_ERR_MODIFIED);
5688 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5689 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5694 const struct got_error *
5695 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5696 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5697 struct got_worktree *worktree, struct got_reference *branch,
5698 struct got_repository *repo)
5700 const struct got_error *err = NULL;
5701 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5702 char *branch_ref_name = NULL;
5703 char *fileindex_path = NULL;
5704 struct check_rebase_ok_arg ok_arg;
5705 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5706 struct got_object_id *wt_branch_tip = NULL;
5708 *new_base_branch_ref = NULL;
5712 err = lock_worktree(worktree, LOCK_EX);
5716 err = open_fileindex(fileindex, &fileindex_path, worktree);
5720 ok_arg.worktree = worktree;
5722 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5727 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5731 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5735 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5739 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5744 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5747 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5748 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5752 err = got_ref_alloc_symref(new_base_branch_ref,
5753 new_base_branch_ref_name, wt_branch);
5756 err = got_ref_write(*new_base_branch_ref, repo);
5760 /* TODO Lock original branch's ref while rebasing? */
5762 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5766 err = got_ref_write(branch_ref, repo);
5770 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5771 worktree->base_commit_id);
5774 err = got_ref_write(*tmp_branch, repo);
5778 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5782 free(fileindex_path);
5783 free(tmp_branch_name);
5784 free(new_base_branch_ref_name);
5785 free(branch_ref_name);
5787 got_ref_close(branch_ref);
5789 got_ref_close(wt_branch);
5790 free(wt_branch_tip);
5792 if (*new_base_branch_ref) {
5793 got_ref_close(*new_base_branch_ref);
5794 *new_base_branch_ref = NULL;
5797 got_ref_close(*tmp_branch);
5801 got_fileindex_free(*fileindex);
5804 lock_worktree(worktree, LOCK_SH);
5809 const struct got_error *
5810 got_worktree_rebase_continue(struct got_object_id **commit_id,
5811 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5812 struct got_reference **branch, struct got_fileindex **fileindex,
5813 struct got_worktree *worktree, struct got_repository *repo)
5815 const struct got_error *err;
5816 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5817 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5818 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5819 char *fileindex_path = NULL;
5820 int have_staged_files = 0;
5823 *new_base_branch = NULL;
5828 err = lock_worktree(worktree, LOCK_EX);
5832 err = open_fileindex(fileindex, &fileindex_path, worktree);
5836 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5837 &have_staged_files);
5838 if (err && err->code != GOT_ERR_CANCELLED)
5840 if (have_staged_files) {
5841 err = got_error(GOT_ERR_STAGED_PATHS);
5845 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5849 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5853 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5857 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5861 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5865 err = got_ref_open(branch, repo,
5866 got_ref_get_symref_target(branch_ref), 0);
5870 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5874 err = got_ref_resolve(commit_id, repo, commit_ref);
5878 err = got_ref_open(new_base_branch, repo,
5879 new_base_branch_ref_name, 0);
5883 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5887 free(commit_ref_name);
5888 free(branch_ref_name);
5889 free(fileindex_path);
5891 got_ref_close(commit_ref);
5893 got_ref_close(branch_ref);
5898 got_ref_close(*tmp_branch);
5901 if (*new_base_branch) {
5902 got_ref_close(*new_base_branch);
5903 *new_base_branch = NULL;
5906 got_ref_close(*branch);
5910 got_fileindex_free(*fileindex);
5913 lock_worktree(worktree, LOCK_SH);
5918 const struct got_error *
5919 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5921 const struct got_error *err;
5922 char *tmp_branch_name = NULL;
5924 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5928 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5929 free(tmp_branch_name);
5933 static const struct got_error *
5934 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5935 char **logmsg, void *arg)
5941 static const struct got_error *
5942 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5943 const char *path, struct got_object_id *blob_id,
5944 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5945 int dirfd, const char *de_name)
5950 struct collect_merged_paths_arg {
5951 got_worktree_checkout_cb progress_cb;
5953 struct got_pathlist_head *merged_paths;
5956 static const struct got_error *
5957 collect_merged_paths(void *arg, unsigned char status, const char *path)
5959 const struct got_error *err;
5960 struct collect_merged_paths_arg *a = arg;
5962 struct got_pathlist_entry *new;
5964 err = (*a->progress_cb)(a->progress_arg, status, path);
5968 if (status != GOT_STATUS_MERGE &&
5969 status != GOT_STATUS_ADD &&
5970 status != GOT_STATUS_DELETE &&
5971 status != GOT_STATUS_CONFLICT)
5976 return got_error_from_errno("strdup");
5978 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5979 if (err || new == NULL)
5985 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5987 struct got_pathlist_entry *pe;
5989 TAILQ_FOREACH(pe, merged_paths, entry)
5990 free((char *)pe->path);
5992 got_pathlist_free(merged_paths);
5995 static const struct got_error *
5996 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5997 int is_rebase, struct got_repository *repo)
5999 const struct got_error *err;
6000 struct got_reference *commit_ref = NULL;
6002 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6004 if (err->code != GOT_ERR_NOT_REF)
6006 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6009 err = got_ref_write(commit_ref, repo);
6012 } else if (is_rebase) {
6013 struct got_object_id *stored_id;
6016 err = got_ref_resolve(&stored_id, repo, commit_ref);
6019 cmp = got_object_id_cmp(commit_id, stored_id);
6022 err = got_error(GOT_ERR_REBASE_COMMITID);
6028 got_ref_close(commit_ref);
6032 static const struct got_error *
6033 rebase_merge_files(struct got_pathlist_head *merged_paths,
6034 const char *commit_ref_name, struct got_worktree *worktree,
6035 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6036 struct got_object_id *commit_id, struct got_repository *repo,
6037 got_worktree_checkout_cb progress_cb, void *progress_arg,
6038 got_cancel_cb cancel_cb, void *cancel_arg)
6040 const struct got_error *err;
6041 struct got_reference *commit_ref = NULL;
6042 struct collect_merged_paths_arg cmp_arg;
6043 char *fileindex_path;
6045 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6047 err = get_fileindex_path(&fileindex_path, worktree);
6051 cmp_arg.progress_cb = progress_cb;
6052 cmp_arg.progress_arg = progress_arg;
6053 cmp_arg.merged_paths = merged_paths;
6054 err = merge_files(worktree, fileindex, fileindex_path,
6055 parent_commit_id, commit_id, repo, collect_merged_paths,
6056 &cmp_arg, cancel_cb, cancel_arg);
6058 got_ref_close(commit_ref);
6062 const struct got_error *
6063 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6064 struct got_worktree *worktree, struct got_fileindex *fileindex,
6065 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6066 struct got_repository *repo,
6067 got_worktree_checkout_cb progress_cb, void *progress_arg,
6068 got_cancel_cb cancel_cb, void *cancel_arg)
6070 const struct got_error *err;
6071 char *commit_ref_name;
6073 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6077 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6081 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6082 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6083 progress_arg, cancel_cb, cancel_arg);
6085 free(commit_ref_name);
6089 const struct got_error *
6090 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6091 struct got_worktree *worktree, struct got_fileindex *fileindex,
6092 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6093 struct got_repository *repo,
6094 got_worktree_checkout_cb progress_cb, void *progress_arg,
6095 got_cancel_cb cancel_cb, void *cancel_arg)
6097 const struct got_error *err;
6098 char *commit_ref_name;
6100 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6104 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6108 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6109 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6110 progress_arg, cancel_cb, cancel_arg);
6112 free(commit_ref_name);
6116 static const struct got_error *
6117 rebase_commit(struct got_object_id **new_commit_id,
6118 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6119 struct got_worktree *worktree, struct got_fileindex *fileindex,
6120 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6121 const char *new_logmsg, struct got_repository *repo)
6123 const struct got_error *err, *sync_err;
6124 struct got_pathlist_head commitable_paths;
6125 struct collect_commitables_arg cc_arg;
6126 char *fileindex_path = NULL;
6127 struct got_reference *head_ref = NULL;
6128 struct got_object_id *head_commit_id = NULL;
6129 char *logmsg = NULL;
6131 TAILQ_INIT(&commitable_paths);
6132 *new_commit_id = NULL;
6134 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6136 err = get_fileindex_path(&fileindex_path, worktree);
6140 cc_arg.commitable_paths = &commitable_paths;
6141 cc_arg.worktree = worktree;
6143 cc_arg.have_staged_files = 0;
6145 * If possible get the status of individual files directly to
6146 * avoid crawling the entire work tree once per rebased commit.
6147 * TODO: Ideally, merged_paths would contain a list of commitables
6148 * we could use so we could skip worktree_status() entirely.
6151 struct got_pathlist_entry *pe;
6152 TAILQ_FOREACH(pe, merged_paths, entry) {
6153 err = worktree_status(worktree, pe->path, fileindex,
6154 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6160 err = worktree_status(worktree, "", fileindex, repo,
6161 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6166 if (TAILQ_EMPTY(&commitable_paths)) {
6167 /* No-op change; commit will be elided. */
6168 err = got_ref_delete(commit_ref, repo);
6171 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6175 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6179 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6184 logmsg = strdup(new_logmsg);
6185 if (logmsg == NULL) {
6186 err = got_error_from_errno("strdup");
6190 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6195 /* NB: commit_worktree will call free(logmsg) */
6196 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6197 worktree, got_object_commit_get_author(orig_commit),
6198 got_object_commit_get_committer(orig_commit),
6199 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6203 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6207 err = got_ref_delete(commit_ref, repo);
6211 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
6213 sync_err = sync_fileindex(fileindex, fileindex_path);
6214 if (sync_err && err == NULL)
6217 free(fileindex_path);
6218 free(head_commit_id);
6220 got_ref_close(head_ref);
6222 free(*new_commit_id);
6223 *new_commit_id = NULL;
6228 const struct got_error *
6229 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6230 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6231 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6232 struct got_commit_object *orig_commit,
6233 struct got_object_id *orig_commit_id, struct got_repository *repo)
6235 const struct got_error *err;
6236 char *commit_ref_name;
6237 struct got_reference *commit_ref = NULL;
6238 struct got_object_id *commit_id = NULL;
6240 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6244 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6247 err = got_ref_resolve(&commit_id, repo, commit_ref);
6250 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6251 err = got_error(GOT_ERR_REBASE_COMMITID);
6255 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6256 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6259 got_ref_close(commit_ref);
6260 free(commit_ref_name);
6265 const struct got_error *
6266 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6267 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6268 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6269 struct got_commit_object *orig_commit,
6270 struct got_object_id *orig_commit_id, const char *new_logmsg,
6271 struct got_repository *repo)
6273 const struct got_error *err;
6274 char *commit_ref_name;
6275 struct got_reference *commit_ref = NULL;
6277 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6281 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6285 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6286 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6289 got_ref_close(commit_ref);
6290 free(commit_ref_name);
6294 const struct got_error *
6295 got_worktree_rebase_postpone(struct got_worktree *worktree,
6296 struct got_fileindex *fileindex)
6299 got_fileindex_free(fileindex);
6300 return lock_worktree(worktree, LOCK_SH);
6303 static const struct got_error *
6304 delete_ref(const char *name, struct got_repository *repo)
6306 const struct got_error *err;
6307 struct got_reference *ref;
6309 err = got_ref_open(&ref, repo, name, 0);
6311 if (err->code == GOT_ERR_NOT_REF)
6316 err = got_ref_delete(ref, repo);
6321 static const struct got_error *
6322 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6324 const struct got_error *err;
6325 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6326 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6328 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6331 err = delete_ref(tmp_branch_name, repo);
6335 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6338 err = delete_ref(new_base_branch_ref_name, repo);
6342 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6345 err = delete_ref(branch_ref_name, repo);
6349 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6352 err = delete_ref(commit_ref_name, repo);
6357 free(tmp_branch_name);
6358 free(new_base_branch_ref_name);
6359 free(branch_ref_name);
6360 free(commit_ref_name);
6364 const struct got_error *
6365 got_worktree_rebase_complete(struct got_worktree *worktree,
6366 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6367 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6368 struct got_repository *repo)
6370 const struct got_error *err, *unlockerr;
6371 struct got_object_id *new_head_commit_id = NULL;
6373 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6377 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6381 err = got_ref_write(rebased_branch, repo);
6385 err = got_worktree_set_head_ref(worktree, rebased_branch);
6389 err = delete_rebase_refs(worktree, repo);
6392 got_fileindex_free(fileindex);
6393 free(new_head_commit_id);
6394 unlockerr = lock_worktree(worktree, LOCK_SH);
6395 if (unlockerr && err == NULL)
6400 const struct got_error *
6401 got_worktree_rebase_abort(struct got_worktree *worktree,
6402 struct got_fileindex *fileindex, struct got_repository *repo,
6403 struct got_reference *new_base_branch,
6404 got_worktree_checkout_cb progress_cb, void *progress_arg)
6406 const struct got_error *err, *unlockerr, *sync_err;
6407 struct got_reference *resolved = NULL;
6408 struct got_object_id *commit_id = NULL;
6409 char *fileindex_path = NULL;
6410 struct revert_file_args rfa;
6411 struct got_object_id *tree_id = NULL;
6413 err = lock_worktree(worktree, LOCK_EX);
6417 err = got_ref_open(&resolved, repo,
6418 got_ref_get_symref_target(new_base_branch), 0);
6422 err = got_worktree_set_head_ref(worktree, resolved);
6427 * XXX commits to the base branch could have happened while
6428 * we were busy rebasing; should we store the original commit ID
6429 * when rebase begins and read it back here?
6431 err = got_ref_resolve(&commit_id, repo, resolved);
6435 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6439 err = got_object_id_by_path(&tree_id, repo,
6440 worktree->base_commit_id, worktree->path_prefix);
6444 err = delete_rebase_refs(worktree, repo);
6448 err = get_fileindex_path(&fileindex_path, worktree);
6452 rfa.worktree = worktree;
6453 rfa.fileindex = fileindex;
6454 rfa.progress_cb = progress_cb;
6455 rfa.progress_arg = progress_arg;
6456 rfa.patch_cb = NULL;
6457 rfa.patch_arg = NULL;
6459 err = worktree_status(worktree, "", fileindex, repo,
6460 revert_file, &rfa, NULL, NULL, 0, 0);
6464 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6465 repo, progress_cb, progress_arg, NULL, NULL);
6467 sync_err = sync_fileindex(fileindex, fileindex_path);
6468 if (sync_err && err == NULL)
6471 got_ref_close(resolved);
6475 got_fileindex_free(fileindex);
6476 free(fileindex_path);
6478 unlockerr = lock_worktree(worktree, LOCK_SH);
6479 if (unlockerr && err == NULL)
6484 const struct got_error *
6485 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6486 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6487 struct got_fileindex **fileindex, struct got_worktree *worktree,
6488 struct got_repository *repo)
6490 const struct got_error *err = NULL;
6491 char *tmp_branch_name = NULL;
6492 char *branch_ref_name = NULL;
6493 char *base_commit_ref_name = NULL;
6494 char *fileindex_path = NULL;
6495 struct check_rebase_ok_arg ok_arg;
6496 struct got_reference *wt_branch = NULL;
6497 struct got_reference *base_commit_ref = NULL;
6501 *base_commit_id = NULL;
6504 err = lock_worktree(worktree, LOCK_EX);
6508 err = open_fileindex(fileindex, &fileindex_path, worktree);
6512 ok_arg.worktree = worktree;
6514 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6519 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6523 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6527 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6532 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6537 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6541 err = got_ref_write(*branch_ref, repo);
6545 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6546 worktree->base_commit_id);
6549 err = got_ref_write(base_commit_ref, repo);
6552 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6553 if (*base_commit_id == NULL) {
6554 err = got_error_from_errno("got_object_id_dup");
6558 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6559 worktree->base_commit_id);
6562 err = got_ref_write(*tmp_branch, repo);
6566 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6570 free(fileindex_path);
6571 free(tmp_branch_name);
6572 free(branch_ref_name);
6573 free(base_commit_ref_name);
6575 got_ref_close(wt_branch);
6578 got_ref_close(*branch_ref);
6582 got_ref_close(*tmp_branch);
6585 free(*base_commit_id);
6587 got_fileindex_free(*fileindex);
6590 lock_worktree(worktree, LOCK_SH);
6595 const struct got_error *
6596 got_worktree_histedit_postpone(struct got_worktree *worktree,
6597 struct got_fileindex *fileindex)
6600 got_fileindex_free(fileindex);
6601 return lock_worktree(worktree, LOCK_SH);
6604 const struct got_error *
6605 got_worktree_histedit_in_progress(int *in_progress,
6606 struct got_worktree *worktree)
6608 const struct got_error *err;
6609 char *tmp_branch_name = NULL;
6611 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6615 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6616 free(tmp_branch_name);
6620 const struct got_error *
6621 got_worktree_histedit_continue(struct got_object_id **commit_id,
6622 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6623 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6624 struct got_worktree *worktree, struct got_repository *repo)
6626 const struct got_error *err;
6627 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6628 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6629 struct got_reference *commit_ref = NULL;
6630 struct got_reference *base_commit_ref = NULL;
6631 char *fileindex_path = NULL;
6632 int have_staged_files = 0;
6636 *base_commit_id = NULL;
6639 err = lock_worktree(worktree, LOCK_EX);
6643 err = open_fileindex(fileindex, &fileindex_path, worktree);
6647 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6648 &have_staged_files);
6649 if (err && err->code != GOT_ERR_CANCELLED)
6651 if (have_staged_files) {
6652 err = got_error(GOT_ERR_STAGED_PATHS);
6656 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6660 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6664 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6668 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6673 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6677 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6680 err = got_ref_resolve(commit_id, repo, commit_ref);
6684 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6687 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6691 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6695 free(commit_ref_name);
6696 free(branch_ref_name);
6697 free(fileindex_path);
6699 got_ref_close(commit_ref);
6700 if (base_commit_ref)
6701 got_ref_close(base_commit_ref);
6705 free(*base_commit_id);
6706 *base_commit_id = NULL;
6708 got_ref_close(*tmp_branch);
6712 got_fileindex_free(*fileindex);
6715 lock_worktree(worktree, LOCK_EX);
6720 static const struct got_error *
6721 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6723 const struct got_error *err;
6724 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6725 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6727 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6730 err = delete_ref(tmp_branch_name, repo);
6734 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6738 err = delete_ref(base_commit_ref_name, repo);
6742 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6745 err = delete_ref(branch_ref_name, repo);
6749 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6752 err = delete_ref(commit_ref_name, repo);
6756 free(tmp_branch_name);
6757 free(base_commit_ref_name);
6758 free(branch_ref_name);
6759 free(commit_ref_name);
6763 const struct got_error *
6764 got_worktree_histedit_abort(struct got_worktree *worktree,
6765 struct got_fileindex *fileindex, struct got_repository *repo,
6766 struct got_reference *branch, struct got_object_id *base_commit_id,
6767 got_worktree_checkout_cb progress_cb, void *progress_arg)
6769 const struct got_error *err, *unlockerr, *sync_err;
6770 struct got_reference *resolved = NULL;
6771 char *fileindex_path = NULL;
6772 struct got_object_id *tree_id = NULL;
6773 struct revert_file_args rfa;
6775 err = lock_worktree(worktree, LOCK_EX);
6779 err = got_ref_open(&resolved, repo,
6780 got_ref_get_symref_target(branch), 0);
6784 err = got_worktree_set_head_ref(worktree, resolved);
6788 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6792 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6793 worktree->path_prefix);
6797 err = delete_histedit_refs(worktree, repo);
6801 err = get_fileindex_path(&fileindex_path, worktree);
6805 rfa.worktree = worktree;
6806 rfa.fileindex = fileindex;
6807 rfa.progress_cb = progress_cb;
6808 rfa.progress_arg = progress_arg;
6809 rfa.patch_cb = NULL;
6810 rfa.patch_arg = NULL;
6812 err = worktree_status(worktree, "", fileindex, repo,
6813 revert_file, &rfa, NULL, NULL, 0, 0);
6817 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6818 repo, progress_cb, progress_arg, NULL, NULL);
6820 sync_err = sync_fileindex(fileindex, fileindex_path);
6821 if (sync_err && err == NULL)
6824 got_ref_close(resolved);
6826 free(fileindex_path);
6828 unlockerr = lock_worktree(worktree, LOCK_SH);
6829 if (unlockerr && err == NULL)
6834 const struct got_error *
6835 got_worktree_histedit_complete(struct got_worktree *worktree,
6836 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6837 struct got_reference *edited_branch, struct got_repository *repo)
6839 const struct got_error *err, *unlockerr;
6840 struct got_object_id *new_head_commit_id = NULL;
6841 struct got_reference *resolved = NULL;
6843 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6847 err = got_ref_open(&resolved, repo,
6848 got_ref_get_symref_target(edited_branch), 0);
6852 err = got_ref_change_ref(resolved, new_head_commit_id);
6856 err = got_ref_write(resolved, repo);
6860 err = got_worktree_set_head_ref(worktree, resolved);
6864 err = delete_histedit_refs(worktree, repo);
6867 got_fileindex_free(fileindex);
6868 free(new_head_commit_id);
6869 unlockerr = lock_worktree(worktree, LOCK_SH);
6870 if (unlockerr && err == NULL)
6875 const struct got_error *
6876 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6877 struct got_object_id *commit_id, struct got_repository *repo)
6879 const struct got_error *err;
6880 char *commit_ref_name;
6882 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6886 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6890 err = delete_ref(commit_ref_name, repo);
6892 free(commit_ref_name);
6896 const struct got_error *
6897 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6898 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6899 struct got_worktree *worktree, const char *refname,
6900 struct got_repository *repo)
6902 const struct got_error *err = NULL;
6903 char *fileindex_path = NULL;
6904 struct check_rebase_ok_arg ok_arg;
6908 *base_branch_ref = NULL;
6910 err = lock_worktree(worktree, LOCK_EX);
6914 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6915 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6916 "cannot integrate a branch into itself; "
6917 "update -b or different branch name required");
6921 err = open_fileindex(fileindex, &fileindex_path, worktree);
6925 /* Preconditions are the same as for rebase. */
6926 ok_arg.worktree = worktree;
6928 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6933 err = got_ref_open(branch_ref, repo, refname, 1);
6937 err = got_ref_open(base_branch_ref, repo,
6938 got_worktree_get_head_ref_name(worktree), 1);
6942 got_ref_close(*branch_ref);
6945 if (*base_branch_ref) {
6946 got_ref_close(*base_branch_ref);
6947 *base_branch_ref = NULL;
6950 got_fileindex_free(*fileindex);
6953 lock_worktree(worktree, LOCK_SH);
6958 const struct got_error *
6959 got_worktree_integrate_continue(struct got_worktree *worktree,
6960 struct got_fileindex *fileindex, struct got_repository *repo,
6961 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6962 got_worktree_checkout_cb progress_cb, void *progress_arg,
6963 got_cancel_cb cancel_cb, void *cancel_arg)
6965 const struct got_error *err = NULL, *sync_err, *unlockerr;
6966 char *fileindex_path = NULL;
6967 struct got_object_id *tree_id = NULL, *commit_id = NULL;
6969 err = get_fileindex_path(&fileindex_path, worktree);
6973 err = got_ref_resolve(&commit_id, repo, branch_ref);
6977 err = got_object_id_by_path(&tree_id, repo, commit_id,
6978 worktree->path_prefix);
6982 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6986 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6987 progress_cb, progress_arg, cancel_cb, cancel_arg);
6991 err = got_ref_change_ref(base_branch_ref, commit_id);
6995 err = got_ref_write(base_branch_ref, repo);
6997 sync_err = sync_fileindex(fileindex, fileindex_path);
6998 if (sync_err && err == NULL)
7002 unlockerr = got_ref_unlock(branch_ref);
7003 if (unlockerr && err == NULL)
7005 got_ref_close(branch_ref);
7007 unlockerr = got_ref_unlock(base_branch_ref);
7008 if (unlockerr && err == NULL)
7010 got_ref_close(base_branch_ref);
7012 got_fileindex_free(fileindex);
7013 free(fileindex_path);
7016 unlockerr = lock_worktree(worktree, LOCK_SH);
7017 if (unlockerr && err == NULL)
7022 const struct got_error *
7023 got_worktree_integrate_abort(struct got_worktree *worktree,
7024 struct got_fileindex *fileindex, struct got_repository *repo,
7025 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7027 const struct got_error *err = NULL, *unlockerr = NULL;
7029 got_fileindex_free(fileindex);
7031 err = lock_worktree(worktree, LOCK_SH);
7033 unlockerr = got_ref_unlock(branch_ref);
7034 if (unlockerr && err == NULL)
7036 got_ref_close(branch_ref);
7038 unlockerr = got_ref_unlock(base_branch_ref);
7039 if (unlockerr && err == NULL)
7041 got_ref_close(base_branch_ref);
7046 struct check_stage_ok_arg {
7047 struct got_object_id *head_commit_id;
7048 struct got_worktree *worktree;
7049 struct got_fileindex *fileindex;
7050 struct got_repository *repo;
7054 const struct got_error *
7055 check_stage_ok(void *arg, unsigned char status,
7056 unsigned char staged_status, const char *relpath,
7057 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7058 struct got_object_id *commit_id, int dirfd, const char *de_name)
7060 struct check_stage_ok_arg *a = arg;
7061 const struct got_error *err = NULL;
7062 struct got_fileindex_entry *ie;
7063 struct got_object_id base_commit_id;
7064 struct got_object_id *base_commit_idp = NULL;
7065 char *in_repo_path = NULL, *p;
7067 if (status == GOT_STATUS_UNVERSIONED ||
7068 status == GOT_STATUS_NO_CHANGE)
7070 if (status == GOT_STATUS_NONEXISTENT)
7071 return got_error_set_errno(ENOENT, relpath);
7073 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7075 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7077 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7078 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7080 return got_error_from_errno("asprintf");
7082 if (got_fileindex_entry_has_commit(ie)) {
7083 memcpy(base_commit_id.sha1, ie->commit_sha1,
7084 SHA1_DIGEST_LENGTH);
7085 base_commit_idp = &base_commit_id;
7088 if (status == GOT_STATUS_CONFLICT) {
7089 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7091 } else if (status != GOT_STATUS_ADD &&
7092 status != GOT_STATUS_MODIFY &&
7093 status != GOT_STATUS_DELETE) {
7094 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7098 a->have_changes = 1;
7103 err = check_out_of_date(p, status, staged_status,
7104 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7105 GOT_ERR_STAGE_OUT_OF_DATE);
7111 struct stage_path_arg {
7112 struct got_worktree *worktree;
7113 struct got_fileindex *fileindex;
7114 struct got_repository *repo;
7115 got_worktree_status_cb status_cb;
7117 got_worktree_patch_cb patch_cb;
7119 int staged_something;
7120 int allow_bad_symlinks;
7123 static const struct got_error *
7124 stage_path(void *arg, unsigned char status,
7125 unsigned char staged_status, const char *relpath,
7126 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7127 struct got_object_id *commit_id, int dirfd, const char *de_name)
7129 struct stage_path_arg *a = arg;
7130 const struct got_error *err = NULL;
7131 struct got_fileindex_entry *ie;
7132 char *ondisk_path = NULL, *path_content = NULL;
7134 struct got_object_id *new_staged_blob_id = NULL;
7137 if (status == GOT_STATUS_UNVERSIONED)
7140 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7142 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7144 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7146 return got_error_from_errno("asprintf");
7149 case GOT_STATUS_ADD:
7150 case GOT_STATUS_MODIFY:
7151 /* XXX could sb.st_mode be passed in by our caller? */
7152 if (lstat(ondisk_path, &sb) == -1) {
7153 err = got_error_from_errno2("lstat", ondisk_path);
7157 if (status == GOT_STATUS_ADD) {
7158 int choice = GOT_PATCH_CHOICE_NONE;
7159 err = (*a->patch_cb)(&choice, a->patch_arg,
7160 status, ie->path, NULL, 1, 1);
7163 if (choice != GOT_PATCH_CHOICE_YES)
7166 err = create_patched_content(&path_content, 0,
7167 staged_blob_id ? staged_blob_id : blob_id,
7168 ondisk_path, dirfd, de_name, ie->path,
7169 a->repo, a->patch_cb, a->patch_arg);
7170 if (err || path_content == NULL)
7174 err = got_object_blob_create(&new_staged_blob_id,
7175 path_content ? path_content : ondisk_path, a->repo);
7178 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7179 SHA1_DIGEST_LENGTH);
7180 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7181 stage = GOT_FILEIDX_STAGE_ADD;
7183 stage = GOT_FILEIDX_STAGE_MODIFY;
7184 got_fileindex_entry_stage_set(ie, stage);
7185 if (S_ISLNK(sb.st_mode)) {
7186 int is_bad_symlink = 0;
7187 if (!a->allow_bad_symlinks) {
7188 char target_path[PATH_MAX];
7190 target_len = readlink(ondisk_path, target_path,
7191 sizeof(target_path));
7192 if (target_len == -1) {
7193 err = got_error_from_errno2("readlink",
7197 err = is_bad_symlink_target(&is_bad_symlink,
7198 target_path, target_len, ondisk_path,
7199 a->worktree->root_path);
7202 if (is_bad_symlink) {
7203 err = got_error_path(ondisk_path,
7204 GOT_ERR_BAD_SYMLINK);
7209 got_fileindex_entry_staged_filetype_set(ie,
7210 GOT_FILEIDX_MODE_BAD_SYMLINK);
7212 got_fileindex_entry_staged_filetype_set(ie,
7213 GOT_FILEIDX_MODE_SYMLINK);
7215 got_fileindex_entry_staged_filetype_set(ie,
7216 GOT_FILEIDX_MODE_REGULAR_FILE);
7218 a->staged_something = 1;
7219 if (a->status_cb == NULL)
7221 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7222 get_staged_status(ie), relpath, blob_id,
7223 new_staged_blob_id, NULL, dirfd, de_name);
7225 case GOT_STATUS_DELETE:
7226 if (staged_status == GOT_STATUS_DELETE)
7229 int choice = GOT_PATCH_CHOICE_NONE;
7230 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7231 ie->path, NULL, 1, 1);
7234 if (choice == GOT_PATCH_CHOICE_NO)
7236 if (choice != GOT_PATCH_CHOICE_YES) {
7237 err = got_error(GOT_ERR_PATCH_CHOICE);
7241 stage = GOT_FILEIDX_STAGE_DELETE;
7242 got_fileindex_entry_stage_set(ie, stage);
7243 a->staged_something = 1;
7244 if (a->status_cb == NULL)
7246 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7247 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7250 case GOT_STATUS_NO_CHANGE:
7252 case GOT_STATUS_CONFLICT:
7253 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7255 case GOT_STATUS_NONEXISTENT:
7256 err = got_error_set_errno(ENOENT, relpath);
7259 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7263 if (path_content && unlink(path_content) == -1 && err == NULL)
7264 err = got_error_from_errno2("unlink", path_content);
7267 free(new_staged_blob_id);
7271 const struct got_error *
7272 got_worktree_stage(struct got_worktree *worktree,
7273 struct got_pathlist_head *paths,
7274 got_worktree_status_cb status_cb, void *status_arg,
7275 got_worktree_patch_cb patch_cb, void *patch_arg,
7276 int allow_bad_symlinks, struct got_repository *repo)
7278 const struct got_error *err = NULL, *sync_err, *unlockerr;
7279 struct got_pathlist_entry *pe;
7280 struct got_fileindex *fileindex = NULL;
7281 char *fileindex_path = NULL;
7282 struct got_reference *head_ref = NULL;
7283 struct got_object_id *head_commit_id = NULL;
7284 struct check_stage_ok_arg oka;
7285 struct stage_path_arg spa;
7287 err = lock_worktree(worktree, LOCK_EX);
7291 err = got_ref_open(&head_ref, repo,
7292 got_worktree_get_head_ref_name(worktree), 0);
7295 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7298 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7302 /* Check pre-conditions before staging anything. */
7303 oka.head_commit_id = head_commit_id;
7304 oka.worktree = worktree;
7305 oka.fileindex = fileindex;
7307 oka.have_changes = 0;
7308 TAILQ_FOREACH(pe, paths, entry) {
7309 err = worktree_status(worktree, pe->path, fileindex, repo,
7310 check_stage_ok, &oka, NULL, NULL, 0, 0);
7314 if (!oka.have_changes) {
7315 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7319 spa.worktree = worktree;
7320 spa.fileindex = fileindex;
7322 spa.patch_cb = patch_cb;
7323 spa.patch_arg = patch_arg;
7324 spa.status_cb = status_cb;
7325 spa.status_arg = status_arg;
7326 spa.staged_something = 0;
7327 spa.allow_bad_symlinks = allow_bad_symlinks;
7328 TAILQ_FOREACH(pe, paths, entry) {
7329 err = worktree_status(worktree, pe->path, fileindex, repo,
7330 stage_path, &spa, NULL, NULL, 0, 0);
7334 if (!spa.staged_something) {
7335 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7339 sync_err = sync_fileindex(fileindex, fileindex_path);
7340 if (sync_err && err == NULL)
7344 got_ref_close(head_ref);
7345 free(head_commit_id);
7346 free(fileindex_path);
7348 got_fileindex_free(fileindex);
7349 unlockerr = lock_worktree(worktree, LOCK_SH);
7350 if (unlockerr && err == NULL)
7355 struct unstage_path_arg {
7356 struct got_worktree *worktree;
7357 struct got_fileindex *fileindex;
7358 struct got_repository *repo;
7359 got_worktree_checkout_cb progress_cb;
7361 got_worktree_patch_cb patch_cb;
7365 static const struct got_error *
7366 create_unstaged_content(char **path_unstaged_content,
7367 char **path_new_staged_content, struct got_object_id *blob_id,
7368 struct got_object_id *staged_blob_id, const char *relpath,
7369 struct got_repository *repo,
7370 got_worktree_patch_cb patch_cb, void *patch_arg)
7372 const struct got_error *err;
7373 struct got_blob_object *blob = NULL, *staged_blob = NULL;
7374 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7375 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7376 struct stat sb1, sb2;
7377 struct got_diff_changes *changes = NULL;
7378 struct got_diff_state *ds = NULL;
7379 struct got_diff_args *args = NULL;
7380 struct got_diff_change *change;
7381 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
7382 int have_content = 0, have_rejected_content = 0;
7384 *path_unstaged_content = NULL;
7385 *path_new_staged_content = NULL;
7387 err = got_object_id_str(&label1, blob_id);
7390 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7394 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7398 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7402 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7406 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7410 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7414 if (stat(path1, &sb1) == -1) {
7415 err = got_error_from_errno2("stat", path1);
7419 if (stat(path2, &sb2) == -1) {
7420 err = got_error_from_errno2("stat", path2);
7424 err = got_diff_files(&changes, &ds, &args, &diff_flags,
7425 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
7429 err = got_opentemp_named(path_unstaged_content, &outfile,
7430 "got-unstaged-content");
7433 err = got_opentemp_named(path_new_staged_content, &rejectfile,
7434 "got-new-staged-content");
7438 if (fseek(f1, 0L, SEEK_SET) == -1) {
7439 err = got_ferror(f1, GOT_ERR_IO);
7442 if (fseek(f2, 0L, SEEK_SET) == -1) {
7443 err = got_ferror(f2, GOT_ERR_IO);
7446 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
7448 err = apply_or_reject_change(&choice, change, ++n,
7449 changes->nchanges, ds, args, diff_flags, relpath,
7450 f1, f2, &line_cur1, &line_cur2,
7451 outfile, rejectfile, patch_cb, patch_arg);
7454 if (choice == GOT_PATCH_CHOICE_YES)
7457 have_rejected_content = 1;
7458 if (choice == GOT_PATCH_CHOICE_QUIT)
7461 if (have_content || have_rejected_content)
7462 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7463 outfile, rejectfile);
7467 got_object_blob_close(blob);
7469 got_object_blob_close(staged_blob);
7470 if (f1 && fclose(f1) == EOF && err == NULL)
7471 err = got_error_from_errno2("fclose", path1);
7472 if (f2 && fclose(f2) == EOF && err == NULL)
7473 err = got_error_from_errno2("fclose", path2);
7474 if (outfile && fclose(outfile) == EOF && err == NULL)
7475 err = got_error_from_errno2("fclose", *path_unstaged_content);
7476 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7477 err = got_error_from_errno2("fclose", *path_new_staged_content);
7478 if (path1 && unlink(path1) == -1 && err == NULL)
7479 err = got_error_from_errno2("unlink", path1);
7480 if (path2 && unlink(path2) == -1 && err == NULL)
7481 err = got_error_from_errno2("unlink", path2);
7482 if (err || !have_content) {
7483 if (*path_unstaged_content &&
7484 unlink(*path_unstaged_content) == -1 && err == NULL)
7485 err = got_error_from_errno2("unlink",
7486 *path_unstaged_content);
7487 free(*path_unstaged_content);
7488 *path_unstaged_content = NULL;
7490 if (err || !have_content || !have_rejected_content) {
7491 if (*path_new_staged_content &&
7492 unlink(*path_new_staged_content) == -1 && err == NULL)
7493 err = got_error_from_errno2("unlink",
7494 *path_new_staged_content);
7495 free(*path_new_staged_content);
7496 *path_new_staged_content = NULL;
7500 got_diff_state_free(ds);
7504 got_diff_free_changes(changes);
7510 static const struct got_error *
7511 unstage_hunks(struct got_object_id *staged_blob_id,
7512 struct got_blob_object *blob_base,
7513 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
7514 const char *ondisk_path, const char *label_orig,
7515 struct got_worktree *worktree, struct got_repository *repo,
7516 got_worktree_patch_cb patch_cb, void *patch_arg,
7517 got_worktree_checkout_cb progress_cb, void *progress_arg)
7519 const struct got_error *err = NULL;
7520 char *path_unstaged_content = NULL;
7521 char *path_new_staged_content = NULL;
7522 struct got_object_id *new_staged_blob_id = NULL;
7526 err = create_unstaged_content(&path_unstaged_content,
7527 &path_new_staged_content, blob_id, staged_blob_id,
7528 ie->path, repo, patch_cb, patch_arg);
7532 if (path_unstaged_content == NULL)
7535 if (path_new_staged_content) {
7536 err = got_object_blob_create(&new_staged_blob_id,
7537 path_new_staged_content, repo);
7542 f = fopen(path_unstaged_content, "r");
7544 err = got_error_from_errno2("fopen",
7545 path_unstaged_content);
7548 if (fstat(fileno(f), &sb) == -1) {
7549 err = got_error_from_errno2("fstat", path_unstaged_content);
7552 if (got_fileindex_entry_staged_filetype_get(ie) ==
7553 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
7554 char link_target[PATH_MAX];
7556 r = fread(link_target, 1, sizeof(link_target), f);
7557 if (r == 0 && ferror(f)) {
7558 err = got_error_from_errno("fread");
7561 if (r >= sizeof(link_target)) { /* should not happen */
7562 err = got_error(GOT_ERR_NO_SPACE);
7565 link_target[r] = '\0';
7566 err = merge_symlink(worktree, blob_base,
7567 ondisk_path, ie->path, label_orig, link_target,
7568 worktree->base_commit_id, repo, progress_cb,
7571 int local_changes_subsumed;
7572 err = merge_file(&local_changes_subsumed, worktree,
7573 blob_base, ondisk_path, ie->path,
7574 got_fileindex_perms_to_st(ie),
7575 path_unstaged_content, label_orig, "unstaged",
7576 repo, progress_cb, progress_arg);
7581 if (new_staged_blob_id) {
7582 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7583 SHA1_DIGEST_LENGTH);
7585 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7587 free(new_staged_blob_id);
7588 if (path_unstaged_content &&
7589 unlink(path_unstaged_content) == -1 && err == NULL)
7590 err = got_error_from_errno2("unlink", path_unstaged_content);
7591 if (path_new_staged_content &&
7592 unlink(path_new_staged_content) == -1 && err == NULL)
7593 err = got_error_from_errno2("unlink", path_new_staged_content);
7594 if (f && fclose(f) != 0 && err == NULL)
7595 err = got_error_from_errno2("fclose", path_unstaged_content);
7596 free(path_unstaged_content);
7597 free(path_new_staged_content);
7601 static const struct got_error *
7602 unstage_path(void *arg, unsigned char status,
7603 unsigned char staged_status, const char *relpath,
7604 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7605 struct got_object_id *commit_id, int dirfd, const char *de_name)
7607 const struct got_error *err = NULL;
7608 struct unstage_path_arg *a = arg;
7609 struct got_fileindex_entry *ie;
7610 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7611 char *ondisk_path = NULL;
7612 char *id_str = NULL, *label_orig = NULL;
7613 int local_changes_subsumed;
7616 if (staged_status != GOT_STATUS_ADD &&
7617 staged_status != GOT_STATUS_MODIFY &&
7618 staged_status != GOT_STATUS_DELETE)
7621 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7623 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7625 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7627 return got_error_from_errno("asprintf");
7629 err = got_object_id_str(&id_str,
7630 commit_id ? commit_id : a->worktree->base_commit_id);
7633 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7635 err = got_error_from_errno("asprintf");
7639 switch (staged_status) {
7640 case GOT_STATUS_MODIFY:
7641 err = got_object_open_as_blob(&blob_base, a->repo,
7646 case GOT_STATUS_ADD:
7648 if (staged_status == GOT_STATUS_ADD) {
7649 int choice = GOT_PATCH_CHOICE_NONE;
7650 err = (*a->patch_cb)(&choice, a->patch_arg,
7651 staged_status, ie->path, NULL, 1, 1);
7654 if (choice != GOT_PATCH_CHOICE_YES)
7657 err = unstage_hunks(staged_blob_id,
7658 blob_base, blob_id, ie, ondisk_path,
7659 label_orig, a->worktree, a->repo,
7660 a->patch_cb, a->patch_arg,
7661 a->progress_cb, a->progress_arg);
7662 break; /* Done with this file. */
7665 err = got_object_open_as_blob(&blob_staged, a->repo,
7666 staged_blob_id, 8192);
7669 switch (got_fileindex_entry_staged_filetype_get(ie)) {
7670 case GOT_FILEIDX_MODE_BAD_SYMLINK:
7671 case GOT_FILEIDX_MODE_REGULAR_FILE:
7672 err = merge_blob(&local_changes_subsumed, a->worktree,
7673 blob_base, ondisk_path, relpath,
7674 got_fileindex_perms_to_st(ie), label_orig,
7675 blob_staged, commit_id ? commit_id :
7676 a->worktree->base_commit_id, a->repo,
7677 a->progress_cb, a->progress_arg);
7679 case GOT_FILEIDX_MODE_SYMLINK:
7680 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7681 char *staged_target;
7682 err = got_object_blob_read_to_str(
7683 &staged_target, blob_staged);
7686 err = merge_symlink(a->worktree, blob_base,
7687 ondisk_path, relpath, label_orig,
7688 staged_target, commit_id ? commit_id :
7689 a->worktree->base_commit_id,
7690 a->repo, a->progress_cb, a->progress_arg);
7691 free(staged_target);
7693 err = merge_blob(&local_changes_subsumed,
7694 a->worktree, blob_base, ondisk_path,
7695 relpath, got_fileindex_perms_to_st(ie),
7696 label_orig, blob_staged,
7697 commit_id ? commit_id :
7698 a->worktree->base_commit_id, a->repo,
7699 a->progress_cb, a->progress_arg);
7703 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
7707 got_fileindex_entry_stage_set(ie,
7708 GOT_FILEIDX_STAGE_NONE);
7710 case GOT_STATUS_DELETE:
7712 int choice = GOT_PATCH_CHOICE_NONE;
7713 err = (*a->patch_cb)(&choice, a->patch_arg,
7714 staged_status, ie->path, NULL, 1, 1);
7717 if (choice == GOT_PATCH_CHOICE_NO)
7719 if (choice != GOT_PATCH_CHOICE_YES) {
7720 err = got_error(GOT_ERR_PATCH_CHOICE);
7724 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7725 err = get_file_status(&status, &sb, ie, ondisk_path,
7726 dirfd, de_name, a->repo);
7729 err = (*a->progress_cb)(a->progress_arg, status, relpath);
7735 got_object_blob_close(blob_base);
7737 got_object_blob_close(blob_staged);
7743 const struct got_error *
7744 got_worktree_unstage(struct got_worktree *worktree,
7745 struct got_pathlist_head *paths,
7746 got_worktree_checkout_cb progress_cb, void *progress_arg,
7747 got_worktree_patch_cb patch_cb, void *patch_arg,
7748 struct got_repository *repo)
7750 const struct got_error *err = NULL, *sync_err, *unlockerr;
7751 struct got_pathlist_entry *pe;
7752 struct got_fileindex *fileindex = NULL;
7753 char *fileindex_path = NULL;
7754 struct unstage_path_arg upa;
7756 err = lock_worktree(worktree, LOCK_EX);
7760 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7764 upa.worktree = worktree;
7765 upa.fileindex = fileindex;
7767 upa.progress_cb = progress_cb;
7768 upa.progress_arg = progress_arg;
7769 upa.patch_cb = patch_cb;
7770 upa.patch_arg = patch_arg;
7771 TAILQ_FOREACH(pe, paths, entry) {
7772 err = worktree_status(worktree, pe->path, fileindex, repo,
7773 unstage_path, &upa, NULL, NULL, 0, 0);
7778 sync_err = sync_fileindex(fileindex, fileindex_path);
7779 if (sync_err && err == NULL)
7782 free(fileindex_path);
7784 got_fileindex_free(fileindex);
7785 unlockerr = lock_worktree(worktree, LOCK_SH);
7786 if (unlockerr && err == NULL)
7791 struct report_file_info_arg {
7792 struct got_worktree *worktree;
7793 got_worktree_path_info_cb info_cb;
7795 struct got_pathlist_head *paths;
7796 got_cancel_cb cancel_cb;
7800 static const struct got_error *
7801 report_file_info(void *arg, struct got_fileindex_entry *ie)
7803 struct report_file_info_arg *a = arg;
7804 struct got_pathlist_entry *pe;
7805 struct got_object_id blob_id, staged_blob_id, commit_id;
7806 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
7807 struct got_object_id *commit_idp = NULL;
7810 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
7811 return got_error(GOT_ERR_CANCELLED);
7813 TAILQ_FOREACH(pe, a->paths, entry) {
7814 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
7815 got_path_is_child(ie->path, pe->path, pe->path_len))
7818 if (pe == NULL) /* not found */
7821 if (got_fileindex_entry_has_blob(ie)) {
7822 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
7823 blob_idp = &blob_id;
7825 stage = got_fileindex_entry_stage_get(ie);
7826 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
7827 stage == GOT_FILEIDX_STAGE_ADD) {
7828 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
7829 SHA1_DIGEST_LENGTH);
7830 staged_blob_idp = &staged_blob_id;
7833 if (got_fileindex_entry_has_commit(ie)) {
7834 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
7835 commit_idp = &commit_id;
7838 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
7839 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
7842 const struct got_error *
7843 got_worktree_path_info(struct got_worktree *worktree,
7844 struct got_pathlist_head *paths,
7845 got_worktree_path_info_cb info_cb, void *info_arg,
7846 got_cancel_cb cancel_cb, void *cancel_arg)
7849 const struct got_error *err = NULL, *unlockerr;
7850 struct got_fileindex *fileindex = NULL;
7851 char *fileindex_path = NULL;
7852 struct report_file_info_arg arg;
7854 err = lock_worktree(worktree, LOCK_SH);
7858 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7862 arg.worktree = worktree;
7863 arg.info_cb = info_cb;
7864 arg.info_arg = info_arg;
7866 arg.cancel_cb = cancel_cb;
7867 arg.cancel_arg = cancel_arg;
7868 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
7871 free(fileindex_path);
7873 got_fileindex_free(fileindex);
7874 unlockerr = lock_worktree(worktree, LOCK_UN);
7875 if (unlockerr && err == NULL)