2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/queue.h>
38 #include "got_error.h"
39 #include "got_repository.h"
40 #include "got_reference.h"
41 #include "got_object.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
45 #include "got_opentemp.h"
48 #include "got_lib_worktree.h"
49 #include "got_lib_sha1.h"
50 #include "got_lib_fileindex.h"
51 #include "got_lib_inflate.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_object_idset.h"
57 #include "got_lib_diff.h"
58 #include "got_lib_gotconfig.h"
61 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
64 #define GOT_MERGE_LABEL_MERGED "merged change"
65 #define GOT_MERGE_LABEL_BASE "3-way merge base"
67 static const struct got_error *
68 create_meta_file(const char *path_got, const char *name, const char *content)
70 const struct got_error *err = NULL;
73 if (asprintf(&path, "%s/%s", path_got, name) == -1)
74 return got_error_from_errno("asprintf");
76 err = got_path_create_file(path, content);
81 static const struct got_error *
82 update_meta_file(const char *path_got, const char *name, const char *content)
84 const struct got_error *err = NULL;
89 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
90 err = got_error_from_errno("asprintf");
95 err = got_opentemp_named(&tmppath, &tmpfile, path);
100 int len = fprintf(tmpfile, "%s\n", content);
101 if (len != strlen(content) + 1) {
102 err = got_error_from_errno2("fprintf", tmppath);
107 if (rename(tmppath, path) != 0) {
108 err = got_error_from_errno3("rename", tmppath, path);
114 if (fclose(tmpfile) != 0 && err == NULL)
115 err = got_error_from_errno2("fclose", tmppath);
120 static const struct got_error *
121 read_meta_file(char **content, const char *path_got, const char *name)
123 const struct got_error *err = NULL;
131 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
132 err = got_error_from_errno("asprintf");
137 fd = open(path, O_RDONLY | O_NOFOLLOW);
140 err = got_error_path(path, GOT_ERR_WORKTREE_META);
142 err = got_error_from_errno2("open", path);
145 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
146 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
147 : got_error_from_errno2("flock", path));
151 if (fstat(fd, &sb) != 0) {
152 err = got_error_from_errno2("fstat", path);
155 *content = calloc(1, sb.st_size);
156 if (*content == NULL) {
157 err = got_error_from_errno("calloc");
161 n = read(fd, *content, sb.st_size);
162 if (n != sb.st_size) {
163 err = (n == -1 ? got_error_from_errno2("read", path) :
164 got_error_path(path, GOT_ERR_WORKTREE_META));
167 if ((*content)[sb.st_size - 1] != '\n') {
168 err = got_error_path(path, GOT_ERR_WORKTREE_META);
171 (*content)[sb.st_size - 1] = '\0';
174 if (fd != -1 && close(fd) == -1 && err == NULL)
175 err = got_error_from_errno2("close", path_got);
184 static const struct got_error *
185 write_head_ref(const char *path_got, struct got_reference *head_ref)
187 const struct got_error *err = NULL;
190 if (got_ref_is_symbolic(head_ref)) {
191 refstr = got_ref_to_str(head_ref);
193 return got_error_from_errno("got_ref_to_str");
195 refstr = strdup(got_ref_get_name(head_ref));
197 return got_error_from_errno("strdup");
199 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
204 const struct got_error *
205 got_worktree_init(const char *path, struct got_reference *head_ref,
206 const char *prefix, struct got_repository *repo)
208 const struct got_error *err = NULL;
209 struct got_object_id *commit_id = NULL;
211 uint32_t uuid_status;
213 char *path_got = NULL;
214 char *formatstr = NULL;
215 char *absprefix = NULL;
216 char *basestr = NULL;
217 char *uuidstr = NULL;
219 if (strcmp(path, got_repo_get_path(repo)) == 0) {
220 err = got_error(GOT_ERR_WORKTREE_REPO);
224 err = got_ref_resolve(&commit_id, repo, head_ref);
227 err = got_object_get_type(&obj_type, repo, commit_id);
230 if (obj_type != GOT_OBJ_TYPE_COMMIT)
231 return got_error(GOT_ERR_OBJ_TYPE);
233 if (!got_path_is_absolute(prefix)) {
234 if (asprintf(&absprefix, "/%s", prefix) == -1)
235 return got_error_from_errno("asprintf");
238 /* Create top-level directory (may already exist). */
239 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
240 err = got_error_from_errno2("mkdir", path);
244 /* Create .got directory (may already exist). */
245 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
246 err = got_error_from_errno("asprintf");
249 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
250 err = got_error_from_errno2("mkdir", path_got);
254 /* Create an empty lock file. */
255 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
259 /* Create an empty file index. */
260 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
264 /* Write the HEAD reference. */
265 err = write_head_ref(path_got, head_ref);
269 /* Record our base commit. */
270 err = got_object_id_str(&basestr, commit_id);
273 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
277 /* Store path to repository. */
278 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
279 got_repo_get_path(repo));
283 /* Store in-repository path prefix. */
284 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
285 absprefix ? absprefix : prefix);
290 uuid_create(&uuid, &uuid_status);
291 if (uuid_status != uuid_s_ok) {
292 err = got_error_uuid(uuid_status, "uuid_create");
295 uuid_to_string(&uuid, &uuidstr, &uuid_status);
296 if (uuid_status != uuid_s_ok) {
297 err = got_error_uuid(uuid_status, "uuid_to_string");
300 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
304 /* Stamp work tree with format file. */
305 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
306 err = got_error_from_errno("asprintf");
309 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
323 static const struct got_error *
324 open_worktree(struct got_worktree **worktree, const char *path)
326 const struct got_error *err = NULL;
328 char *formatstr = NULL;
329 char *uuidstr = NULL;
330 char *path_lock = NULL;
331 char *base_commit_id_str = NULL;
332 int version, fd = -1;
334 struct got_repository *repo = NULL;
335 uint32_t uuid_status;
339 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
340 err = got_error_from_errno("asprintf");
345 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
346 err = got_error_from_errno("asprintf");
351 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
353 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
354 : got_error_from_errno2("open", path_lock));
358 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
362 version = strtonum(formatstr, 1, INT_MAX, &errstr);
364 err = got_error_msg(GOT_ERR_WORKTREE_META,
365 "could not parse work tree format version number");
368 if (version != GOT_WORKTREE_FORMAT_VERSION) {
369 err = got_error(GOT_ERR_WORKTREE_VERS);
373 *worktree = calloc(1, sizeof(**worktree));
374 if (*worktree == NULL) {
375 err = got_error_from_errno("calloc");
378 (*worktree)->lockfd = -1;
380 (*worktree)->root_path = realpath(path, NULL);
381 if ((*worktree)->root_path == NULL) {
382 err = got_error_from_errno2("realpath", path);
385 err = read_meta_file(&(*worktree)->repo_path, path_got,
386 GOT_WORKTREE_REPOSITORY);
390 err = read_meta_file(&(*worktree)->path_prefix, path_got,
391 GOT_WORKTREE_PATH_PREFIX);
395 err = read_meta_file(&base_commit_id_str, path_got,
396 GOT_WORKTREE_BASE_COMMIT);
400 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
403 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
404 if (uuid_status != uuid_s_ok) {
405 err = got_error_uuid(uuid_status, "uuid_from_string");
409 err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
413 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
418 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
419 GOT_WORKTREE_HEAD_REF);
423 if (asprintf(&(*worktree)->gotconfig_path, "%s/%s/%s",
424 (*worktree)->root_path,
425 GOT_WORKTREE_GOT_DIR, GOT_GOTCONFIG_FILENAME) == -1) {
426 err = got_error_from_errno("asprintf");
430 err = got_gotconfig_read(&(*worktree)->gotconfig,
431 (*worktree)->gotconfig_path);
434 got_repo_close(repo);
437 free(base_commit_id_str);
443 if (*worktree != NULL)
444 got_worktree_close(*worktree);
447 (*worktree)->lockfd = fd;
452 const struct got_error *
453 got_worktree_open(struct got_worktree **worktree, const char *path)
455 const struct got_error *err = NULL;
458 worktree_path = strdup(path);
459 if (worktree_path == NULL)
460 return got_error_from_errno("strdup");
465 err = open_worktree(worktree, worktree_path);
466 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT)) {
474 if (worktree_path[0] == '/' && worktree_path[1] == '\0')
476 err = got_path_dirname(&parent_path, worktree_path);
478 if (err->code != GOT_ERR_BAD_PATH) {
485 worktree_path = parent_path;
489 return got_error(GOT_ERR_NOT_WORKTREE);
492 const struct got_error *
493 got_worktree_close(struct got_worktree *worktree)
495 const struct got_error *err = NULL;
496 free(worktree->repo_path);
497 free(worktree->path_prefix);
498 free(worktree->base_commit_id);
499 free(worktree->head_ref_name);
500 if (worktree->lockfd != -1)
501 if (close(worktree->lockfd) != 0)
502 err = got_error_from_errno2("close",
503 got_worktree_get_root_path(worktree));
504 free(worktree->root_path);
505 free(worktree->gotconfig_path);
506 got_gotconfig_free(worktree->gotconfig);
512 got_worktree_get_root_path(struct got_worktree *worktree)
514 return worktree->root_path;
518 got_worktree_get_repo_path(struct got_worktree *worktree)
520 return worktree->repo_path;
523 got_worktree_get_path_prefix(struct got_worktree *worktree)
525 return worktree->path_prefix;
528 const struct got_error *
529 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
530 const char *path_prefix)
532 char *absprefix = NULL;
534 if (!got_path_is_absolute(path_prefix)) {
535 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
536 return got_error_from_errno("asprintf");
538 *match = (strcmp(absprefix ? absprefix : path_prefix,
539 worktree->path_prefix) == 0);
545 got_worktree_get_head_ref_name(struct got_worktree *worktree)
547 return worktree->head_ref_name;
550 const struct got_error *
551 got_worktree_set_head_ref(struct got_worktree *worktree,
552 struct got_reference *head_ref)
554 const struct got_error *err = NULL;
555 char *path_got = NULL, *head_ref_name = NULL;
557 if (asprintf(&path_got, "%s/%s", worktree->root_path,
558 GOT_WORKTREE_GOT_DIR) == -1) {
559 err = got_error_from_errno("asprintf");
564 head_ref_name = strdup(got_ref_get_name(head_ref));
565 if (head_ref_name == NULL) {
566 err = got_error_from_errno("strdup");
570 err = write_head_ref(path_got, head_ref);
574 free(worktree->head_ref_name);
575 worktree->head_ref_name = head_ref_name;
583 struct got_object_id *
584 got_worktree_get_base_commit_id(struct got_worktree *worktree)
586 return worktree->base_commit_id;
589 const struct got_error *
590 got_worktree_set_base_commit_id(struct got_worktree *worktree,
591 struct got_repository *repo, struct got_object_id *commit_id)
593 const struct got_error *err;
594 struct got_object *obj = NULL;
596 char *path_got = NULL;
598 if (asprintf(&path_got, "%s/%s", worktree->root_path,
599 GOT_WORKTREE_GOT_DIR) == -1) {
600 err = got_error_from_errno("asprintf");
605 err = got_object_open(&obj, repo, commit_id);
609 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
610 err = got_error(GOT_ERR_OBJ_TYPE);
614 /* Record our base commit. */
615 err = got_object_id_str(&id_str, commit_id);
618 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
622 free(worktree->base_commit_id);
623 worktree->base_commit_id = got_object_id_dup(commit_id);
624 if (worktree->base_commit_id == NULL) {
625 err = got_error_from_errno("got_object_id_dup");
630 got_object_close(obj);
636 const struct got_gotconfig *
637 got_worktree_get_gotconfig(struct got_worktree *worktree)
639 return worktree->gotconfig;
642 static const struct got_error *
643 lock_worktree(struct got_worktree *worktree, int operation)
645 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
646 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
647 : got_error_from_errno2("flock",
648 got_worktree_get_root_path(worktree)));
652 static const struct got_error *
653 add_dir_on_disk(struct got_worktree *worktree, const char *path)
655 const struct got_error *err = NULL;
658 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
659 return got_error_from_errno("asprintf");
661 err = got_path_mkdir(abspath);
662 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
665 if (lstat(abspath, &sb) == -1) {
666 err = got_error_from_errno2("lstat", abspath);
667 } else if (!S_ISDIR(sb.st_mode)) {
668 /* TODO directory is obstructed; do something */
669 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
676 static const struct got_error *
677 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
679 const struct got_error *err = NULL;
682 size_t flen1 = 0, flen2 = 0;
687 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
688 if (flen1 == 0 && ferror(f1)) {
689 err = got_error_from_errno("fread");
692 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
693 if (flen2 == 0 && ferror(f2)) {
694 err = got_error_from_errno("fread");
701 } else if (flen2 == 0) {
705 } else if (flen1 == flen2) {
706 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
719 static const struct got_error *
720 check_files_equal(int *same, const char *f1_path, const char *f2_path)
722 const struct got_error *err = NULL;
725 FILE *f1 = NULL, *f2 = NULL;
729 if (lstat(f1_path, &sb) != 0) {
730 err = got_error_from_errno2("lstat", f1_path);
735 if (lstat(f2_path, &sb) != 0) {
736 err = got_error_from_errno2("lstat", f2_path);
741 if (size1 != size2) {
746 f1 = fopen(f1_path, "r");
748 return got_error_from_errno2("fopen", f1_path);
750 f2 = fopen(f2_path, "r");
752 err = got_error_from_errno2("fopen", f2_path);
756 err = check_file_contents_equal(same, f1, f2);
758 if (f1 && fclose(f1) != 0 && err == NULL)
759 err = got_error_from_errno("fclose");
760 if (f2 && fclose(f2) != 0 && err == NULL)
761 err = got_error_from_errno("fclose");
767 * Perform a 3-way merge where blob_orig acts as the common ancestor,
768 * the file at deriv_path acts as the first derived version, and the
769 * file on disk acts as the second derived version.
771 static const struct got_error *
772 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
773 struct got_blob_object *blob_orig, const char *ondisk_path,
774 const char *path, uint16_t st_mode, const char *deriv_path,
775 const char *label_orig, const char *label_deriv,
776 struct got_repository *repo,
777 got_worktree_checkout_cb progress_cb, void *progress_arg)
779 const struct got_error *err = NULL;
782 char *blob_orig_path = NULL;
783 char *merged_path = NULL, *base_path = NULL;
786 char *symlink_path = NULL;
787 FILE *symlinkf = NULL;
789 *local_changes_subsumed = 0;
791 err = got_path_dirname(&parent, ondisk_path);
795 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
796 err = got_error_from_errno("asprintf");
800 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
805 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
806 err = got_error_from_errno("asprintf");
811 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
815 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
821 * If the file has no blob, this is an "add vs add" conflict,
822 * and we simply use an empty ancestor file to make both files
823 * appear in the merged result in their entirety.
828 * In order the run a 3-way merge with a symlink we copy the symlink's
829 * target path into a temporary file and use that file with diff3.
831 if (S_ISLNK(st_mode)) {
832 char target_path[PATH_MAX];
837 if (asprintf(&base_path, "%s/got-symlink-merge",
839 err = got_error_from_errno("asprintf");
843 err = got_opentemp_named(&symlink_path, &symlinkf, base_path);
846 target_len = readlink(ondisk_path, target_path,
847 sizeof(target_path));
848 if (target_len == -1) {
849 err = got_error_from_errno2("readlink", ondisk_path);
852 n = fwrite(target_path, 1, target_len, symlinkf);
853 if (n != target_len) {
854 err = got_ferror(symlinkf, GOT_ERR_IO);
857 if (fflush(symlinkf) == EOF) {
858 err = got_error_from_errno2("fflush", symlink_path);
863 err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
864 blob_orig_path, symlink_path ? symlink_path : ondisk_path,
865 label_deriv, label_orig, NULL);
869 err = (*progress_cb)(progress_arg,
870 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
874 if (fsync(merged_fd) != 0) {
875 err = got_error_from_errno("fsync");
879 /* Check if a clean merge has subsumed all local changes. */
880 if (overlapcnt == 0) {
881 err = check_files_equal(local_changes_subsumed, deriv_path,
887 if (fchmod(merged_fd, st_mode) != 0) {
888 err = got_error_from_errno2("fchmod", merged_path);
892 if (rename(merged_path, ondisk_path) != 0) {
893 err = got_error_from_errno3("rename", merged_path,
903 if (unlink(symlink_path) == -1 && err == NULL)
904 err = got_error_from_errno2("unlink", symlink_path);
906 if (symlinkf && fclose(symlinkf) == EOF && err == NULL)
907 err = got_error_from_errno2("fclose", symlink_path);
909 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
910 err = got_error_from_errno("close");
911 if (f_orig && fclose(f_orig) != 0 && err == NULL)
912 err = got_error_from_errno("fclose");
915 if (blob_orig_path) {
916 unlink(blob_orig_path);
917 free(blob_orig_path);
923 static const struct got_error *
924 update_symlink(const char *ondisk_path, const char *target_path,
927 /* This is not atomic but matches what 'ln -sf' does. */
928 if (unlink(ondisk_path) == -1)
929 return got_error_from_errno2("unlink", ondisk_path);
930 if (symlink(target_path, ondisk_path) == -1)
931 return got_error_from_errno3("symlink", target_path,
937 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
938 * in the work tree with a file that contains conflict markers and the
939 * conflicting target paths of the original version, a "derived version"
940 * of a symlink from an incoming change, and a local version of the symlink.
942 * The original versions's target path can be NULL if it is not available,
943 * such as if both derived versions added a new symlink at the same path.
945 * The incoming derived symlink target is NULL in case the incoming change
946 * has deleted this symlink.
948 static const struct got_error *
949 install_symlink_conflict(const char *deriv_target,
950 struct got_object_id *deriv_base_commit_id, const char *orig_target,
951 const char *label_orig, const char *local_target, const char *ondisk_path)
953 const struct got_error *err;
954 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
957 err = got_object_id_str(&id_str, deriv_base_commit_id);
959 return got_error_from_errno("asprintf");
961 if (asprintf(&label_deriv, "%s: commit %s",
962 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
963 err = got_error_from_errno("asprintf");
967 err = got_opentemp_named(&path, &f, "got-symlink-conflict");
971 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
972 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
973 deriv_target ? deriv_target : "(symlink was deleted)",
974 orig_target ? label_orig : "",
975 orig_target ? "\n" : "",
976 orig_target ? orig_target : "",
977 orig_target ? "\n" : "",
978 GOT_DIFF_CONFLICT_MARKER_SEP,
979 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
980 err = got_error_from_errno2("fprintf", path);
984 if (unlink(ondisk_path) == -1) {
985 err = got_error_from_errno2("unlink", ondisk_path);
988 if (rename(path, ondisk_path) == -1) {
989 err = got_error_from_errno3("rename", path, ondisk_path);
992 if (chmod(ondisk_path, GOT_DEFAULT_FILE_MODE) == -1) {
993 err = got_error_from_errno2("chmod", ondisk_path);
997 if (f != NULL && fclose(f) == EOF && err == NULL)
998 err = got_error_from_errno2("fclose", path);
1005 /* forward declaration */
1006 static const struct got_error *
1007 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
1008 const char *, const char *, uint16_t, const char *,
1009 struct got_blob_object *, struct got_object_id *,
1010 struct got_repository *, got_worktree_checkout_cb, void *);
1013 * Merge a symlink into the work tree, where blob_orig acts as the common
1014 * ancestor, deriv_target is the link target of the first derived version,
1015 * and the symlink on disk acts as the second derived version.
1016 * Assume that contents of both blobs represent symlinks.
1018 static const struct got_error *
1019 merge_symlink(struct got_worktree *worktree,
1020 struct got_blob_object *blob_orig, const char *ondisk_path,
1021 const char *path, const char *label_orig, const char *deriv_target,
1022 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1023 got_worktree_checkout_cb progress_cb, void *progress_arg)
1025 const struct got_error *err = NULL;
1026 char *ancestor_target = NULL;
1028 ssize_t ondisk_len, deriv_len;
1029 char ondisk_target[PATH_MAX];
1030 int have_local_change = 0;
1031 int have_incoming_change = 0;
1033 if (lstat(ondisk_path, &sb) == -1)
1034 return got_error_from_errno2("lstat", ondisk_path);
1036 ondisk_len = readlink(ondisk_path, ondisk_target,
1037 sizeof(ondisk_target));
1038 if (ondisk_len == -1) {
1039 err = got_error_from_errno2("readlink",
1043 ondisk_target[ondisk_len] = '\0';
1046 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
1051 if (ancestor_target == NULL ||
1052 (ondisk_len != strlen(ancestor_target) ||
1053 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
1054 have_local_change = 1;
1056 deriv_len = strlen(deriv_target);
1057 if (ancestor_target == NULL ||
1058 (deriv_len != strlen(ancestor_target) ||
1059 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
1060 have_incoming_change = 1;
1062 if (!have_local_change && !have_incoming_change) {
1063 if (ancestor_target) {
1064 /* Both sides made the same change. */
1065 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1067 } else if (deriv_len == ondisk_len &&
1068 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1069 /* Both sides added the same symlink. */
1070 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1073 /* Both sides added symlinks which don't match. */
1074 err = install_symlink_conflict(deriv_target,
1075 deriv_base_commit_id, ancestor_target,
1076 label_orig, ondisk_target, ondisk_path);
1079 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1082 } else if (!have_local_change && have_incoming_change) {
1083 /* Apply the incoming change. */
1084 err = update_symlink(ondisk_path, deriv_target,
1085 strlen(deriv_target));
1088 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1089 } else if (have_local_change && have_incoming_change) {
1090 if (deriv_len == ondisk_len &&
1091 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
1092 /* Both sides made the same change. */
1093 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1096 err = install_symlink_conflict(deriv_target,
1097 deriv_base_commit_id, ancestor_target, label_orig,
1098 ondisk_target, ondisk_path);
1101 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1107 free(ancestor_target);
1112 * Perform a 3-way merge where blob_orig acts as the common ancestor,
1113 * blob_deriv acts as the first derived version, and the file on disk
1114 * acts as the second derived version.
1116 static const struct got_error *
1117 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1118 struct got_blob_object *blob_orig, const char *ondisk_path,
1119 const char *path, uint16_t st_mode, const char *label_orig,
1120 struct got_blob_object *blob_deriv,
1121 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1122 got_worktree_checkout_cb progress_cb, void *progress_arg)
1124 const struct got_error *err = NULL;
1125 FILE *f_deriv = NULL;
1126 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1127 char *label_deriv = NULL, *parent = NULL;
1129 *local_changes_subsumed = 0;
1131 err = got_path_dirname(&parent, ondisk_path);
1136 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1137 err = got_error_from_errno("asprintf");
1142 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1145 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1150 err = got_object_id_str(&id_str, deriv_base_commit_id);
1153 if (asprintf(&label_deriv, "%s: commit %s",
1154 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1155 err = got_error_from_errno("asprintf");
1159 err = merge_file(local_changes_subsumed, worktree, blob_orig,
1160 ondisk_path, path, st_mode, blob_deriv_path, label_orig,
1161 label_deriv, repo, progress_cb, progress_arg);
1163 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
1164 err = got_error_from_errno("fclose");
1166 if (blob_deriv_path) {
1167 unlink(blob_deriv_path);
1168 free(blob_deriv_path);
1176 static const struct got_error *
1177 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1178 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1179 const char *ondisk_path, const char *path, struct got_object_id *blob_id)
1181 const struct got_error *err = NULL;
1182 struct got_fileindex_entry *new_ie;
1186 err = got_fileindex_entry_alloc(&new_ie, path);
1190 err = got_fileindex_entry_update(new_ie, ondisk_path,
1191 blob_id->sha1, base_commit_id->sha1, 1);
1195 err = got_fileindex_entry_add(fileindex, new_ie);
1198 got_fileindex_entry_free(new_ie);
1205 get_ondisk_perms(int executable, mode_t st_mode)
1207 mode_t xbits = S_IXUSR;
1210 /* Map read bits to execute bits. */
1211 if (st_mode & S_IRGRP)
1213 if (st_mode & S_IROTH)
1215 return st_mode | xbits;
1218 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1221 /* forward declaration */
1222 static const struct got_error *
1223 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1224 const char *path, mode_t te_mode, mode_t st_mode,
1225 struct got_blob_object *blob, int restoring_missing_file,
1226 int reverting_versioned_file, int installing_bad_symlink,
1227 int path_is_unversioned, struct got_repository *repo,
1228 got_worktree_checkout_cb progress_cb, void *progress_arg);
1231 * This function assumes that the provided symlink target points at a
1232 * safe location in the work tree!
1234 static const struct got_error *
1235 replace_existing_symlink(const char *ondisk_path, const char *target_path,
1238 const struct got_error *err = NULL;
1240 char etarget[PATH_MAX];
1244 * "Bad" symlinks (those pointing outside the work tree or into the
1245 * .got directory) are installed in the work tree as a regular file
1246 * which contains the bad symlink target path.
1247 * The new symlink target has already been checked for safety by our
1248 * caller. If we can successfully open a regular file then we simply
1249 * replace this file with a symlink below.
1251 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1254 return got_error_from_errno2("open", ondisk_path);
1256 /* We are updating an existing on-disk symlink. */
1257 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1259 return got_error_from_errno2("readlink", ondisk_path);
1261 if (elen == target_len &&
1262 memcmp(etarget, target_path, target_len) == 0)
1263 return NULL; /* nothing to do */
1266 err = update_symlink(ondisk_path, target_path, target_len);
1267 if (fd != -1 && close(fd) == -1 && err == NULL)
1268 err = got_error_from_errno2("close", ondisk_path);
1272 static const struct got_error *
1273 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1274 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1276 const struct got_error *err = NULL;
1277 char canonpath[PATH_MAX];
1278 char *path_got = NULL;
1280 *is_bad_symlink = 0;
1282 if (target_len >= sizeof(canonpath)) {
1283 *is_bad_symlink = 1;
1288 * We do not use realpath(3) to resolve the symlink's target
1289 * path because we don't want to resolve symlinks recursively.
1290 * Instead we make the path absolute and then canonicalize it.
1291 * Relative symlink target lookup should begin at the directory
1292 * in which the blob object is being installed.
1294 if (!got_path_is_absolute(target_path)) {
1295 char *abspath, *parent;
1296 err = got_path_dirname(&parent, ondisk_path);
1299 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1301 return got_error_from_errno("asprintf");
1304 if (strlen(abspath) >= sizeof(canonpath)) {
1305 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1309 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1314 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1319 /* Only allow symlinks pointing at paths within the work tree. */
1320 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1321 *is_bad_symlink = 1;
1325 /* Do not allow symlinks pointing into the .got directory. */
1326 if (asprintf(&path_got, "%s/%s", wtroot_path,
1327 GOT_WORKTREE_GOT_DIR) == -1)
1328 return got_error_from_errno("asprintf");
1329 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1330 *is_bad_symlink = 1;
1336 static const struct got_error *
1337 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1338 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1339 int restoring_missing_file, int reverting_versioned_file,
1340 int path_is_unversioned, struct got_repository *repo,
1341 got_worktree_checkout_cb progress_cb, void *progress_arg)
1343 const struct got_error *err = NULL;
1344 char target_path[PATH_MAX];
1345 size_t len, target_len = 0;
1346 char *path_got = NULL;
1347 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1348 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1350 *is_bad_symlink = 0;
1353 * Blob object content specifies the target path of the link.
1354 * If a symbolic link cannot be installed we instead create
1355 * a regular file which contains the link target path stored
1356 * in the blob object.
1359 err = got_object_blob_read_block(&len, blob);
1360 if (len + target_len >= sizeof(target_path)) {
1361 /* Path too long; install as a regular file. */
1362 *is_bad_symlink = 1;
1363 got_object_blob_rewind(blob);
1364 return install_blob(worktree, ondisk_path, path,
1365 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1366 restoring_missing_file, reverting_versioned_file,
1367 1, path_is_unversioned, repo, progress_cb,
1371 /* Skip blob object header first time around. */
1372 memcpy(target_path + target_len, buf + hdrlen,
1374 target_len += len - hdrlen;
1378 target_path[target_len] = '\0';
1380 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1381 ondisk_path, worktree->root_path);
1385 if (*is_bad_symlink) {
1386 /* install as a regular file */
1387 *is_bad_symlink = 1;
1388 got_object_blob_rewind(blob);
1389 err = install_blob(worktree, ondisk_path, path,
1390 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1391 restoring_missing_file, reverting_versioned_file, 1,
1392 path_is_unversioned, repo, progress_cb, progress_arg);
1396 if (symlink(target_path, ondisk_path) == -1) {
1397 if (errno == EEXIST) {
1398 if (path_is_unversioned) {
1399 err = (*progress_cb)(progress_arg,
1400 GOT_STATUS_UNVERSIONED, path);
1403 err = replace_existing_symlink(ondisk_path,
1404 target_path, target_len);
1408 err = (*progress_cb)(progress_arg,
1409 reverting_versioned_file ?
1410 GOT_STATUS_REVERT : GOT_STATUS_UPDATE,
1413 goto done; /* Nothing else to do. */
1416 if (errno == ENOENT) {
1418 err = got_path_dirname(&parent, ondisk_path);
1421 err = add_dir_on_disk(worktree, parent);
1426 * Retry, and fall through to error handling
1427 * below if this second attempt fails.
1429 if (symlink(target_path, ondisk_path) != -1) {
1430 err = NULL; /* success */
1435 /* Handle errors from first or second creation attempt. */
1436 if (errno == ENAMETOOLONG) {
1437 /* bad target path; install as a regular file */
1438 *is_bad_symlink = 1;
1439 got_object_blob_rewind(blob);
1440 err = install_blob(worktree, ondisk_path, path,
1441 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1442 restoring_missing_file, reverting_versioned_file, 1,
1443 path_is_unversioned, repo,
1444 progress_cb, progress_arg);
1445 } else if (errno == ENOTDIR) {
1446 err = got_error_path(ondisk_path,
1447 GOT_ERR_FILE_OBSTRUCTED);
1449 err = got_error_from_errno3("symlink",
1450 target_path, ondisk_path);
1452 } else if (progress_cb)
1453 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1454 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1460 static const struct got_error *
1461 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1462 const char *path, mode_t te_mode, mode_t st_mode,
1463 struct got_blob_object *blob, int restoring_missing_file,
1464 int reverting_versioned_file, int installing_bad_symlink,
1465 int path_is_unversioned, struct got_repository *repo,
1466 got_worktree_checkout_cb progress_cb, void *progress_arg)
1468 const struct got_error *err = NULL;
1472 char *tmppath = NULL;
1474 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1475 GOT_DEFAULT_FILE_MODE);
1477 if (errno == ENOENT) {
1479 err = got_path_dirname(&parent, path);
1482 err = add_dir_on_disk(worktree, parent);
1486 fd = open(ondisk_path,
1487 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1488 GOT_DEFAULT_FILE_MODE);
1490 return got_error_from_errno2("open",
1492 } else if (errno == EEXIST) {
1493 if (path_is_unversioned) {
1494 err = (*progress_cb)(progress_arg,
1495 GOT_STATUS_UNVERSIONED, path);
1498 if (!S_ISREG(st_mode) && !installing_bad_symlink) {
1499 /* TODO file is obstructed; do something */
1500 err = got_error_path(ondisk_path,
1501 GOT_ERR_FILE_OBSTRUCTED);
1504 err = got_opentemp_named_fd(&tmppath, &fd,
1511 return got_error_from_errno2("open", ondisk_path);
1515 if (restoring_missing_file)
1516 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1518 else if (reverting_versioned_file)
1519 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1522 err = (*progress_cb)(progress_arg,
1523 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1528 hdrlen = got_object_blob_get_hdrlen(blob);
1530 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1531 err = got_object_blob_read_block(&len, blob);
1535 /* Skip blob object header first time around. */
1536 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1538 err = got_error_from_errno("write");
1540 } else if (outlen != len - hdrlen) {
1541 err = got_error(GOT_ERR_IO);
1548 if (fsync(fd) != 0) {
1549 err = got_error_from_errno("fsync");
1554 if (rename(tmppath, ondisk_path) != 0) {
1555 err = got_error_from_errno3("rename", tmppath,
1562 if (chmod(ondisk_path,
1563 get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1564 err = got_error_from_errno2("chmod", ondisk_path);
1569 if (fd != -1 && close(fd) != 0 && err == NULL)
1570 err = got_error_from_errno("close");
1575 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1576 static const struct got_error *
1577 get_modified_file_content_status(unsigned char *status, FILE *f)
1579 const struct got_error *err = NULL;
1580 const char *markers[3] = {
1581 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1582 GOT_DIFF_CONFLICT_MARKER_SEP,
1583 GOT_DIFF_CONFLICT_MARKER_END
1588 const char delim[3] = {'\0', '\0', '\0'};
1590 while (*status == GOT_STATUS_MODIFY) {
1591 line = fparseln(f, &len, NULL, delim, 0);
1595 err = got_ferror(f, GOT_ERR_IO);
1599 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1600 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1602 *status = GOT_STATUS_CONFLICT;
1612 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1614 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1615 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1619 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1621 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1622 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1623 ie->mtime_sec == sb->st_mtim.tv_sec &&
1624 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1625 ie->size == (sb->st_size & 0xffffffff) &&
1626 !xbit_differs(ie, sb->st_mode));
1629 static unsigned char
1630 get_staged_status(struct got_fileindex_entry *ie)
1632 switch (got_fileindex_entry_stage_get(ie)) {
1633 case GOT_FILEIDX_STAGE_ADD:
1634 return GOT_STATUS_ADD;
1635 case GOT_FILEIDX_STAGE_DELETE:
1636 return GOT_STATUS_DELETE;
1637 case GOT_FILEIDX_STAGE_MODIFY:
1638 return GOT_STATUS_MODIFY;
1640 return GOT_STATUS_NO_CHANGE;
1644 static const struct got_error *
1645 get_symlink_modification_status(unsigned char *status,
1646 struct got_fileindex_entry *ie, const char *abspath,
1647 int dirfd, const char *de_name, struct got_blob_object *blob)
1649 const struct got_error *err = NULL;
1650 char target_path[PATH_MAX];
1651 char etarget[PATH_MAX];
1653 size_t len, target_len = 0;
1654 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1655 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1657 *status = GOT_STATUS_NO_CHANGE;
1659 /* Blob object content specifies the target path of the link. */
1661 err = got_object_blob_read_block(&len, blob);
1664 if (len + target_len >= sizeof(target_path)) {
1666 * Should not happen. The blob contents were OK
1667 * when this symlink was installed.
1669 return got_error(GOT_ERR_NO_SPACE);
1672 /* Skip blob object header first time around. */
1673 memcpy(target_path + target_len, buf + hdrlen,
1675 target_len += len - hdrlen;
1679 target_path[target_len] = '\0';
1682 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1684 return got_error_from_errno2("readlinkat", abspath);
1686 elen = readlink(abspath, etarget, sizeof(etarget));
1688 return got_error_from_errno2("readlink", abspath);
1691 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1692 *status = GOT_STATUS_MODIFY;
1697 static const struct got_error *
1698 get_file_status(unsigned char *status, struct stat *sb,
1699 struct got_fileindex_entry *ie, const char *abspath,
1700 int dirfd, const char *de_name, struct got_repository *repo)
1702 const struct got_error *err = NULL;
1703 struct got_object_id id;
1708 struct got_blob_object *blob = NULL;
1710 unsigned char staged_status = get_staged_status(ie);
1712 *status = GOT_STATUS_NO_CHANGE;
1715 * Whenever the caller provides a directory descriptor and a
1716 * directory entry name for the file, use them! This prevents
1717 * race conditions if filesystem paths change beneath our feet.
1720 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1721 if (errno == ENOENT) {
1722 if (got_fileindex_entry_has_file_on_disk(ie))
1723 *status = GOT_STATUS_MISSING;
1725 *status = GOT_STATUS_DELETE;
1728 err = got_error_from_errno2("fstatat", abspath);
1732 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1733 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1734 return got_error_from_errno2("open", abspath);
1735 else if (fd == -1 && errno == ELOOP) {
1736 if (lstat(abspath, sb) == -1)
1737 return got_error_from_errno2("lstat", abspath);
1738 } else if (fd == -1 || fstat(fd, sb) == -1) {
1739 if (errno == ENOENT) {
1740 if (got_fileindex_entry_has_file_on_disk(ie))
1741 *status = GOT_STATUS_MISSING;
1743 *status = GOT_STATUS_DELETE;
1746 err = got_error_from_errno2("fstat", abspath);
1751 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1752 *status = GOT_STATUS_OBSTRUCTED;
1756 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1757 *status = GOT_STATUS_DELETE;
1759 } else if (!got_fileindex_entry_has_blob(ie) &&
1760 staged_status != GOT_STATUS_ADD) {
1761 *status = GOT_STATUS_ADD;
1765 if (!stat_info_differs(ie, sb))
1768 if (S_ISLNK(sb->st_mode) &&
1769 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1770 *status = GOT_STATUS_MODIFY;
1774 if (staged_status == GOT_STATUS_MODIFY ||
1775 staged_status == GOT_STATUS_ADD)
1776 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1778 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1780 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1784 if (S_ISLNK(sb->st_mode)) {
1785 err = get_symlink_modification_status(status, ie,
1786 abspath, dirfd, de_name, blob);
1791 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1793 err = got_error_from_errno2("openat", abspath);
1798 f = fdopen(fd, "r");
1800 err = got_error_from_errno2("fdopen", abspath);
1804 hdrlen = got_object_blob_get_hdrlen(blob);
1806 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1807 err = got_object_blob_read_block(&blen, blob);
1810 /* Skip length of blob object header first time around. */
1811 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1812 if (flen == 0 && ferror(f)) {
1813 err = got_error_from_errno("fread");
1816 if (blen - hdrlen == 0) {
1818 *status = GOT_STATUS_MODIFY;
1820 } else if (flen == 0) {
1821 if (blen - hdrlen != 0)
1822 *status = GOT_STATUS_MODIFY;
1824 } else if (blen - hdrlen == flen) {
1825 /* Skip blob object header first time around. */
1826 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1827 *status = GOT_STATUS_MODIFY;
1831 *status = GOT_STATUS_MODIFY;
1837 if (*status == GOT_STATUS_MODIFY) {
1839 err = get_modified_file_content_status(status, f);
1840 } else if (xbit_differs(ie, sb->st_mode))
1841 *status = GOT_STATUS_MODE_CHANGE;
1844 got_object_blob_close(blob);
1845 if (f != NULL && fclose(f) == EOF && err == NULL)
1846 err = got_error_from_errno2("fclose", abspath);
1847 if (fd != -1 && close(fd) == -1 && err == NULL)
1848 err = got_error_from_errno2("close", abspath);
1853 * Update timestamps in the file index if a file is unmodified and
1854 * we had to run a full content comparison to find out.
1856 static const struct got_error *
1857 sync_timestamps(char *ondisk_path, unsigned char status,
1858 struct got_fileindex_entry *ie, struct stat *sb)
1860 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1861 return got_fileindex_entry_update(ie, ondisk_path,
1862 ie->blob_sha1, ie->commit_sha1, 1);
1867 static const struct got_error *
1868 update_blob(struct got_worktree *worktree,
1869 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1870 struct got_tree_entry *te, const char *path,
1871 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1874 const struct got_error *err = NULL;
1875 struct got_blob_object *blob = NULL;
1877 unsigned char status = GOT_STATUS_NO_CHANGE;
1880 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1881 return got_error_from_errno("asprintf");
1884 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1885 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1888 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1892 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1893 sb.st_mode = got_fileindex_perms_to_st(ie);
1895 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1896 status = GOT_STATUS_UNVERSIONED;
1899 if (status == GOT_STATUS_OBSTRUCTED) {
1900 err = (*progress_cb)(progress_arg, status, path);
1903 if (status == GOT_STATUS_CONFLICT) {
1904 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1909 if (ie && status != GOT_STATUS_MISSING &&
1910 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1911 if (got_fileindex_entry_has_commit(ie) &&
1912 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1913 SHA1_DIGEST_LENGTH) == 0) {
1914 err = sync_timestamps(ondisk_path, status, ie, &sb);
1917 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1921 if (got_fileindex_entry_has_blob(ie) &&
1922 memcmp(ie->blob_sha1, te->id.sha1,
1923 SHA1_DIGEST_LENGTH) == 0) {
1924 err = sync_timestamps(ondisk_path, status, ie, &sb);
1929 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1933 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1934 int update_timestamps;
1935 struct got_blob_object *blob2 = NULL;
1936 char *label_orig = NULL;
1937 if (got_fileindex_entry_has_blob(ie)) {
1938 struct got_object_id id2;
1939 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1940 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1944 if (got_fileindex_entry_has_commit(ie)) {
1945 char id_str[SHA1_DIGEST_STRING_LENGTH];
1946 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1947 sizeof(id_str)) == NULL) {
1948 err = got_error_path(id_str,
1949 GOT_ERR_BAD_OBJ_ID_STR);
1952 if (asprintf(&label_orig, "%s: commit %s",
1953 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1954 err = got_error_from_errno("asprintf");
1958 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1960 err = got_object_blob_read_to_str(&link_target, blob);
1963 err = merge_symlink(worktree, blob2, ondisk_path, path,
1964 label_orig, link_target, worktree->base_commit_id,
1965 repo, progress_cb, progress_arg);
1968 err = merge_blob(&update_timestamps, worktree, blob2,
1969 ondisk_path, path, sb.st_mode, label_orig, blob,
1970 worktree->base_commit_id, repo,
1971 progress_cb, progress_arg);
1975 got_object_blob_close(blob2);
1979 * Do not update timestamps of files with local changes.
1980 * Otherwise, a future status walk would treat them as
1981 * unmodified files again.
1983 err = got_fileindex_entry_update(ie, ondisk_path,
1984 blob->id.sha1, worktree->base_commit_id->sha1,
1986 } else if (status == GOT_STATUS_MODE_CHANGE) {
1987 err = got_fileindex_entry_update(ie, ondisk_path,
1988 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1989 } else if (status == GOT_STATUS_DELETE) {
1990 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1993 err = got_fileindex_entry_update(ie, ondisk_path,
1994 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1998 int is_bad_symlink = 0;
1999 if (S_ISLNK(te->mode)) {
2000 err = install_symlink(&is_bad_symlink, worktree,
2001 ondisk_path, path, blob,
2002 status == GOT_STATUS_MISSING, 0,
2003 status == GOT_STATUS_UNVERSIONED, repo,
2004 progress_cb, progress_arg);
2006 err = install_blob(worktree, ondisk_path, path,
2007 te->mode, sb.st_mode, blob,
2008 status == GOT_STATUS_MISSING, 0, 0,
2009 status == GOT_STATUS_UNVERSIONED, repo,
2010 progress_cb, progress_arg);
2016 err = got_fileindex_entry_update(ie, ondisk_path,
2017 blob->id.sha1, worktree->base_commit_id->sha1, 1);
2019 err = create_fileindex_entry(&ie, fileindex,
2020 worktree->base_commit_id, ondisk_path, path,
2026 if (is_bad_symlink) {
2027 got_fileindex_entry_filetype_set(ie,
2028 GOT_FILEIDX_MODE_BAD_SYMLINK);
2031 got_object_blob_close(blob);
2037 static const struct got_error *
2038 remove_ondisk_file(const char *root_path, const char *path)
2040 const struct got_error *err = NULL;
2041 char *ondisk_path = NULL;
2043 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2044 return got_error_from_errno("asprintf");
2046 if (unlink(ondisk_path) == -1) {
2047 if (errno != ENOENT)
2048 err = got_error_from_errno2("unlink", ondisk_path);
2050 size_t root_len = strlen(root_path);
2053 err = got_path_dirname(&parent, ondisk_path);
2057 ondisk_path = parent;
2058 if (rmdir(ondisk_path) == -1) {
2059 if (errno != ENOTEMPTY)
2060 err = got_error_from_errno2("rmdir",
2064 } while (got_path_cmp(ondisk_path, root_path,
2065 strlen(ondisk_path), root_len) != 0);
2071 static const struct got_error *
2072 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2073 struct got_fileindex_entry *ie, struct got_repository *repo,
2074 got_worktree_checkout_cb progress_cb, void *progress_arg)
2076 const struct got_error *err = NULL;
2077 unsigned char status;
2081 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2082 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2084 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2086 return got_error_from_errno("asprintf");
2088 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2092 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2093 char ondisk_target[PATH_MAX];
2094 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2095 sizeof(ondisk_target));
2096 if (ondisk_len == -1) {
2097 err = got_error_from_errno2("readlink", ondisk_path);
2100 ondisk_target[ondisk_len] = '\0';
2101 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2102 NULL, NULL, /* XXX pass common ancestor info? */
2103 ondisk_target, ondisk_path);
2106 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2111 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2112 status == GOT_STATUS_ADD) {
2113 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2117 * Preserve the working file and change the deleted blob's
2118 * entry into a schedule-add entry.
2120 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
2123 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2126 if (status == GOT_STATUS_NO_CHANGE) {
2127 err = remove_ondisk_file(worktree->root_path, ie->path);
2131 got_fileindex_entry_remove(fileindex, ie);
2138 struct diff_cb_arg {
2139 struct got_fileindex *fileindex;
2140 struct got_worktree *worktree;
2141 struct got_repository *repo;
2142 got_worktree_checkout_cb progress_cb;
2144 got_cancel_cb cancel_cb;
2148 static const struct got_error *
2149 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2150 struct got_tree_entry *te, const char *parent_path)
2152 struct diff_cb_arg *a = arg;
2154 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2155 return got_error(GOT_ERR_CANCELLED);
2157 return update_blob(a->worktree, a->fileindex, ie, te,
2158 ie->path, a->repo, a->progress_cb, a->progress_arg);
2161 static const struct got_error *
2162 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2164 struct diff_cb_arg *a = arg;
2166 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2167 return got_error(GOT_ERR_CANCELLED);
2169 return delete_blob(a->worktree, a->fileindex, ie,
2170 a->repo, a->progress_cb, a->progress_arg);
2173 static const struct got_error *
2174 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2176 struct diff_cb_arg *a = arg;
2177 const struct got_error *err;
2180 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2181 return got_error(GOT_ERR_CANCELLED);
2183 if (got_object_tree_entry_is_submodule(te))
2186 if (asprintf(&path, "%s%s%s", parent_path,
2187 parent_path[0] ? "/" : "", te->name)
2189 return got_error_from_errno("asprintf");
2191 if (S_ISDIR(te->mode))
2192 err = add_dir_on_disk(a->worktree, path);
2194 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2195 a->repo, a->progress_cb, a->progress_arg);
2201 const struct got_error *
2202 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2204 uint32_t uuid_status;
2206 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2207 if (uuid_status != uuid_s_ok) {
2209 return got_error_uuid(uuid_status, "uuid_to_string");
2215 static const struct got_error *
2216 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2218 const struct got_error *err = NULL;
2219 char *uuidstr = NULL;
2223 err = got_worktree_get_uuid(&uuidstr, worktree);
2227 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2228 err = got_error_from_errno("asprintf");
2235 const struct got_error *
2236 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2238 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2241 static const struct got_error *
2242 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2244 return get_ref_name(refname, worktree,
2245 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2248 static const struct got_error *
2249 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2251 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2254 static const struct got_error *
2255 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2257 return get_ref_name(refname, worktree,
2258 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2261 static const struct got_error *
2262 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2264 return get_ref_name(refname, worktree,
2265 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2268 static const struct got_error *
2269 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2271 return get_ref_name(refname, worktree,
2272 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2275 static const struct got_error *
2276 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2278 return get_ref_name(refname, worktree,
2279 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2282 static const struct got_error *
2283 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2285 return get_ref_name(refname, worktree,
2286 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2289 static const struct got_error *
2290 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2292 return get_ref_name(refname, worktree,
2293 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2296 const struct got_error *
2297 got_worktree_get_histedit_script_path(char **path,
2298 struct got_worktree *worktree)
2300 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2301 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2303 return got_error_from_errno("asprintf");
2309 * Prevent Git's garbage collector from deleting our base commit by
2310 * setting a reference to our base commit's ID.
2312 static const struct got_error *
2313 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2315 const struct got_error *err = NULL;
2316 struct got_reference *ref = NULL;
2319 err = got_worktree_get_base_ref_name(&refname, worktree);
2323 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2327 err = got_ref_write(ref, repo);
2335 static const struct got_error *
2336 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2338 const struct got_error *err = NULL;
2340 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2341 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2342 err = got_error_from_errno("asprintf");
2343 *fileindex_path = NULL;
2349 static const struct got_error *
2350 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2351 struct got_worktree *worktree)
2353 const struct got_error *err = NULL;
2356 *fileindex_path = NULL;
2357 *fileindex = got_fileindex_alloc();
2358 if (*fileindex == NULL)
2359 return got_error_from_errno("got_fileindex_alloc");
2361 err = get_fileindex_path(fileindex_path, worktree);
2365 index = fopen(*fileindex_path, "rb");
2366 if (index == NULL) {
2367 if (errno != ENOENT)
2368 err = got_error_from_errno2("fopen", *fileindex_path);
2370 err = got_fileindex_read(*fileindex, index);
2371 if (fclose(index) != 0 && err == NULL)
2372 err = got_error_from_errno("fclose");
2376 free(*fileindex_path);
2377 *fileindex_path = NULL;
2378 got_fileindex_free(*fileindex);
2384 struct bump_base_commit_id_arg {
2385 struct got_object_id *base_commit_id;
2388 const char *entry_name;
2389 got_worktree_checkout_cb progress_cb;
2393 /* Bump base commit ID of all files within an updated part of the work tree. */
2394 static const struct got_error *
2395 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2397 const struct got_error *err;
2398 struct bump_base_commit_id_arg *a = arg;
2400 if (a->entry_name) {
2401 if (strcmp(ie->path, a->path) != 0)
2403 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2406 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2407 SHA1_DIGEST_LENGTH) == 0)
2410 if (a->progress_cb) {
2411 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2416 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2420 static const struct got_error *
2421 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2423 const struct got_error *err = NULL;
2424 char *new_fileindex_path = NULL;
2425 FILE *new_index = NULL;
2426 struct timespec timeout;
2428 err = got_opentemp_named(&new_fileindex_path, &new_index,
2433 err = got_fileindex_write(fileindex, new_index);
2437 if (rename(new_fileindex_path, fileindex_path) != 0) {
2438 err = got_error_from_errno3("rename", new_fileindex_path,
2440 unlink(new_fileindex_path);
2444 * Sleep for a short amount of time to ensure that files modified after
2445 * this program exits have a different time stamp from the one which
2446 * was recorded in the file index.
2449 timeout.tv_nsec = 1;
2450 nanosleep(&timeout, NULL);
2454 free(new_fileindex_path);
2458 static const struct got_error *
2459 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2460 struct got_object_id **tree_id, const char *wt_relpath,
2461 struct got_worktree *worktree, struct got_repository *repo)
2463 const struct got_error *err = NULL;
2464 struct got_object_id *id = NULL;
2465 char *in_repo_path = NULL;
2466 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2468 *entry_type = GOT_OBJ_TYPE_ANY;
2469 *tree_relpath = NULL;
2472 if (wt_relpath[0] == '\0') {
2473 /* Check out all files within the work tree. */
2474 *entry_type = GOT_OBJ_TYPE_TREE;
2475 *tree_relpath = strdup("");
2476 if (*tree_relpath == NULL) {
2477 err = got_error_from_errno("strdup");
2480 err = got_object_id_by_path(tree_id, repo,
2481 worktree->base_commit_id, worktree->path_prefix);
2487 /* Check out a subset of files in the work tree. */
2489 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2490 is_root_wt ? "" : "/", wt_relpath) == -1) {
2491 err = got_error_from_errno("asprintf");
2495 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2501 in_repo_path = NULL;
2503 err = got_object_get_type(entry_type, repo, id);
2507 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2508 /* Check out a single file. */
2509 if (strchr(wt_relpath, '/') == NULL) {
2510 /* Check out a single file in work tree's root dir. */
2511 in_repo_path = strdup(worktree->path_prefix);
2512 if (in_repo_path == NULL) {
2513 err = got_error_from_errno("strdup");
2516 *tree_relpath = strdup("");
2517 if (*tree_relpath == NULL) {
2518 err = got_error_from_errno("strdup");
2522 /* Check out a single file in a subdirectory. */
2523 err = got_path_dirname(tree_relpath, wt_relpath);
2526 if (asprintf(&in_repo_path, "%s%s%s",
2527 worktree->path_prefix, is_root_wt ? "" : "/",
2528 *tree_relpath) == -1) {
2529 err = got_error_from_errno("asprintf");
2533 err = got_object_id_by_path(tree_id, repo,
2534 worktree->base_commit_id, in_repo_path);
2536 /* Check out all files within a subdirectory. */
2537 *tree_id = got_object_id_dup(id);
2538 if (*tree_id == NULL) {
2539 err = got_error_from_errno("got_object_id_dup");
2542 *tree_relpath = strdup(wt_relpath);
2543 if (*tree_relpath == NULL) {
2544 err = got_error_from_errno("strdup");
2552 *entry_type = GOT_OBJ_TYPE_ANY;
2553 free(*tree_relpath);
2554 *tree_relpath = NULL;
2561 static const struct got_error *
2562 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2563 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2564 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2565 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2567 const struct got_error *err = NULL;
2568 struct got_commit_object *commit = NULL;
2569 struct got_tree_object *tree = NULL;
2570 struct got_fileindex_diff_tree_cb diff_cb;
2571 struct diff_cb_arg arg;
2573 err = ref_base_commit(worktree, repo);
2575 if (!(err->code == GOT_ERR_ERRNO &&
2576 (errno == EACCES || errno == EROFS)))
2578 err = (*progress_cb)(progress_arg,
2579 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2584 err = got_object_open_as_commit(&commit, repo,
2585 worktree->base_commit_id);
2589 err = got_object_open_as_tree(&tree, repo, tree_id);
2594 got_object_tree_find_entry(tree, entry_name) == NULL) {
2595 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2599 diff_cb.diff_old_new = diff_old_new;
2600 diff_cb.diff_old = diff_old;
2601 diff_cb.diff_new = diff_new;
2602 arg.fileindex = fileindex;
2603 arg.worktree = worktree;
2605 arg.progress_cb = progress_cb;
2606 arg.progress_arg = progress_arg;
2607 arg.cancel_cb = cancel_cb;
2608 arg.cancel_arg = cancel_arg;
2609 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2610 entry_name, repo, &diff_cb, &arg);
2613 got_object_tree_close(tree);
2615 got_object_commit_close(commit);
2619 const struct got_error *
2620 got_worktree_checkout_files(struct got_worktree *worktree,
2621 struct got_pathlist_head *paths, struct got_repository *repo,
2622 got_worktree_checkout_cb progress_cb, void *progress_arg,
2623 got_cancel_cb cancel_cb, void *cancel_arg)
2625 const struct got_error *err = NULL, *sync_err, *unlockerr;
2626 struct got_commit_object *commit = NULL;
2627 struct got_tree_object *tree = NULL;
2628 struct got_fileindex *fileindex = NULL;
2629 char *fileindex_path = NULL;
2630 struct got_pathlist_entry *pe;
2631 struct tree_path_data {
2632 SIMPLEQ_ENTRY(tree_path_data) entry;
2633 struct got_object_id *tree_id;
2638 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2640 SIMPLEQ_INIT(&tree_paths);
2642 err = lock_worktree(worktree, LOCK_EX);
2646 /* Map all specified paths to in-repository trees. */
2647 TAILQ_FOREACH(pe, paths, entry) {
2648 tpd = malloc(sizeof(*tpd));
2650 err = got_error_from_errno("malloc");
2654 err = find_tree_entry_for_checkout(&tpd->entry_type,
2655 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2661 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2662 err = got_path_basename(&tpd->entry_name, pe->path);
2670 tpd->entry_name = NULL;
2672 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2676 * Read the file index.
2677 * Checking out files is supposed to be an idempotent operation.
2678 * If the on-disk file index is incomplete we will try to complete it.
2680 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2684 tpd = SIMPLEQ_FIRST(&tree_paths);
2685 TAILQ_FOREACH(pe, paths, entry) {
2686 struct bump_base_commit_id_arg bbc_arg;
2688 err = checkout_files(worktree, fileindex, tpd->relpath,
2689 tpd->tree_id, tpd->entry_name, repo,
2690 progress_cb, progress_arg, cancel_cb, cancel_arg);
2694 bbc_arg.base_commit_id = worktree->base_commit_id;
2695 bbc_arg.entry_name = tpd->entry_name;
2696 bbc_arg.path = pe->path;
2697 bbc_arg.path_len = pe->path_len;
2698 bbc_arg.progress_cb = progress_cb;
2699 bbc_arg.progress_arg = progress_arg;
2700 err = got_fileindex_for_each_entry_safe(fileindex,
2701 bump_base_commit_id, &bbc_arg);
2705 tpd = SIMPLEQ_NEXT(tpd, entry);
2707 sync_err = sync_fileindex(fileindex, fileindex_path);
2708 if (sync_err && err == NULL)
2711 free(fileindex_path);
2713 got_object_tree_close(tree);
2715 got_object_commit_close(commit);
2717 got_fileindex_free(fileindex);
2718 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2719 tpd = SIMPLEQ_FIRST(&tree_paths);
2720 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2725 unlockerr = lock_worktree(worktree, LOCK_SH);
2726 if (unlockerr && err == NULL)
2731 struct merge_file_cb_arg {
2732 struct got_worktree *worktree;
2733 struct got_fileindex *fileindex;
2734 got_worktree_checkout_cb progress_cb;
2736 got_cancel_cb cancel_cb;
2738 const char *label_orig;
2739 struct got_object_id *commit_id2;
2742 static const struct got_error *
2743 merge_file_cb(void *arg, struct got_blob_object *blob1,
2744 struct got_blob_object *blob2, struct got_object_id *id1,
2745 struct got_object_id *id2, const char *path1, const char *path2,
2746 mode_t mode1, mode_t mode2, struct got_repository *repo)
2748 static const struct got_error *err = NULL;
2749 struct merge_file_cb_arg *a = arg;
2750 struct got_fileindex_entry *ie;
2751 char *ondisk_path = NULL;
2753 unsigned char status;
2754 int local_changes_subsumed;
2756 if (blob1 && blob2) {
2757 ie = got_fileindex_entry_get(a->fileindex, path2,
2760 return (*a->progress_cb)(a->progress_arg,
2761 GOT_STATUS_MISSING, path2);
2763 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2765 return got_error_from_errno("asprintf");
2767 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2772 if (status == GOT_STATUS_DELETE) {
2773 err = (*a->progress_cb)(a->progress_arg,
2774 GOT_STATUS_MERGE, path2);
2777 if (status != GOT_STATUS_NO_CHANGE &&
2778 status != GOT_STATUS_MODIFY &&
2779 status != GOT_STATUS_CONFLICT &&
2780 status != GOT_STATUS_ADD) {
2781 err = (*a->progress_cb)(a->progress_arg, status, path2);
2785 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2787 err = got_object_blob_read_to_str(&link_target2, blob2);
2790 err = merge_symlink(a->worktree, blob1, ondisk_path,
2791 path2, a->label_orig, link_target2, a->commit_id2,
2792 repo, a->progress_cb, a->progress_arg);
2795 err = merge_blob(&local_changes_subsumed, a->worktree,
2796 blob1, ondisk_path, path2, sb.st_mode,
2797 a->label_orig, blob2, a->commit_id2, repo,
2798 a->progress_cb, a->progress_arg);
2801 ie = got_fileindex_entry_get(a->fileindex, path1,
2804 return (*a->progress_cb)(a->progress_arg,
2805 GOT_STATUS_MISSING, path1);
2807 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2809 return got_error_from_errno("asprintf");
2811 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2817 case GOT_STATUS_NO_CHANGE:
2818 err = (*a->progress_cb)(a->progress_arg,
2819 GOT_STATUS_DELETE, path1);
2822 err = remove_ondisk_file(a->worktree->root_path, path1);
2826 got_fileindex_entry_mark_deleted_from_disk(ie);
2828 case GOT_STATUS_DELETE:
2829 case GOT_STATUS_MISSING:
2830 err = (*a->progress_cb)(a->progress_arg,
2831 GOT_STATUS_DELETE, path1);
2835 got_fileindex_entry_mark_deleted_from_disk(ie);
2837 case GOT_STATUS_ADD: {
2838 struct got_object_id *id;
2841 * Delete the added file only if its content already
2842 * exists in the repository.
2844 err = got_object_blob_file_create(&id, &blob1_f, path1);
2847 if (got_object_id_cmp(id, id1) == 0) {
2848 err = (*a->progress_cb)(a->progress_arg,
2849 GOT_STATUS_DELETE, path1);
2852 err = remove_ondisk_file(a->worktree->root_path,
2857 got_fileindex_entry_remove(a->fileindex,
2860 err = (*a->progress_cb)(a->progress_arg,
2861 GOT_STATUS_CANNOT_DELETE, path1);
2863 if (fclose(blob1_f) == EOF && err == NULL)
2864 err = got_error_from_errno("fclose");
2870 case GOT_STATUS_MODIFY:
2871 case GOT_STATUS_CONFLICT:
2872 err = (*a->progress_cb)(a->progress_arg,
2873 GOT_STATUS_CANNOT_DELETE, path1);
2877 case GOT_STATUS_OBSTRUCTED:
2878 err = (*a->progress_cb)(a->progress_arg, status, path1);
2886 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2888 return got_error_from_errno("asprintf");
2889 ie = got_fileindex_entry_get(a->fileindex, path2,
2892 err = get_file_status(&status, &sb, ie, ondisk_path,
2896 if (status != GOT_STATUS_NO_CHANGE &&
2897 status != GOT_STATUS_MODIFY &&
2898 status != GOT_STATUS_CONFLICT &&
2899 status != GOT_STATUS_ADD) {
2900 err = (*a->progress_cb)(a->progress_arg,
2904 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2906 err = got_object_blob_read_to_str(&link_target2,
2910 err = merge_symlink(a->worktree, NULL,
2911 ondisk_path, path2, a->label_orig,
2912 link_target2, a->commit_id2, repo,
2913 a->progress_cb, a->progress_arg);
2915 } else if (S_ISREG(sb.st_mode)) {
2916 err = merge_blob(&local_changes_subsumed,
2917 a->worktree, NULL, ondisk_path, path2,
2918 sb.st_mode, a->label_orig, blob2,
2919 a->commit_id2, repo, a->progress_cb,
2922 err = got_error_path(ondisk_path,
2923 GOT_ERR_FILE_OBSTRUCTED);
2927 if (status == GOT_STATUS_DELETE) {
2928 err = got_fileindex_entry_update(ie,
2929 ondisk_path, blob2->id.sha1,
2930 a->worktree->base_commit_id->sha1, 0);
2935 int is_bad_symlink = 0;
2936 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2937 if (S_ISLNK(mode2)) {
2938 err = install_symlink(&is_bad_symlink,
2939 a->worktree, ondisk_path, path2, blob2, 0,
2940 0, 1, repo, a->progress_cb, a->progress_arg);
2942 err = install_blob(a->worktree, ondisk_path, path2,
2943 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2944 a->progress_cb, a->progress_arg);
2948 err = got_fileindex_entry_alloc(&ie, path2);
2951 err = got_fileindex_entry_update(ie, ondisk_path,
2954 got_fileindex_entry_free(ie);
2957 err = got_fileindex_entry_add(a->fileindex, ie);
2959 got_fileindex_entry_free(ie);
2962 if (is_bad_symlink) {
2963 got_fileindex_entry_filetype_set(ie,
2964 GOT_FILEIDX_MODE_BAD_SYMLINK);
2973 struct check_merge_ok_arg {
2974 struct got_worktree *worktree;
2975 struct got_repository *repo;
2978 static const struct got_error *
2979 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2981 const struct got_error *err = NULL;
2982 struct check_merge_ok_arg *a = arg;
2983 unsigned char status;
2987 /* Reject merges into a work tree with mixed base commits. */
2988 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2989 SHA1_DIGEST_LENGTH))
2990 return got_error(GOT_ERR_MIXED_COMMITS);
2992 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2994 return got_error_from_errno("asprintf");
2996 /* Reject merges into a work tree with conflicted files. */
2997 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3000 if (status == GOT_STATUS_CONFLICT)
3001 return got_error(GOT_ERR_CONFLICTS);
3006 static const struct got_error *
3007 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3008 const char *fileindex_path, struct got_object_id *commit_id1,
3009 struct got_object_id *commit_id2, struct got_repository *repo,
3010 got_worktree_checkout_cb progress_cb, void *progress_arg,
3011 got_cancel_cb cancel_cb, void *cancel_arg)
3013 const struct got_error *err = NULL, *sync_err;
3014 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3015 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3016 struct merge_file_cb_arg arg;
3017 char *label_orig = NULL;
3020 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3021 worktree->path_prefix);
3022 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3028 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3032 err = got_object_id_str(&id_str, commit_id1);
3036 if (asprintf(&label_orig, "%s: commit %s",
3037 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3038 err = got_error_from_errno("asprintf");
3045 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3046 worktree->path_prefix);
3050 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3054 arg.worktree = worktree;
3055 arg.fileindex = fileindex;
3056 arg.progress_cb = progress_cb;
3057 arg.progress_arg = progress_arg;
3058 arg.cancel_cb = cancel_cb;
3059 arg.cancel_arg = cancel_arg;
3060 arg.label_orig = label_orig;
3061 arg.commit_id2 = commit_id2;
3062 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3063 sync_err = sync_fileindex(fileindex, fileindex_path);
3064 if (sync_err && err == NULL)
3068 got_object_tree_close(tree1);
3070 got_object_tree_close(tree2);
3075 const struct got_error *
3076 got_worktree_merge_files(struct got_worktree *worktree,
3077 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3078 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3079 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3081 const struct got_error *err, *unlockerr;
3082 char *fileindex_path = NULL;
3083 struct got_fileindex *fileindex = NULL;
3084 struct check_merge_ok_arg mok_arg;
3086 err = lock_worktree(worktree, LOCK_EX);
3090 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3094 mok_arg.worktree = worktree;
3095 mok_arg.repo = repo;
3096 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
3101 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3102 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
3105 got_fileindex_free(fileindex);
3106 free(fileindex_path);
3107 unlockerr = lock_worktree(worktree, LOCK_SH);
3108 if (unlockerr && err == NULL)
3113 struct diff_dir_cb_arg {
3114 struct got_fileindex *fileindex;
3115 struct got_worktree *worktree;
3116 const char *status_path;
3117 size_t status_path_len;
3118 struct got_repository *repo;
3119 got_worktree_status_cb status_cb;
3121 got_cancel_cb cancel_cb;
3123 /* A pathlist containing per-directory pathlists of ignore patterns. */
3124 struct got_pathlist_head ignores;
3125 int report_unchanged;
3129 static const struct got_error *
3130 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3131 int dirfd, const char *de_name,
3132 got_worktree_status_cb status_cb, void *status_arg,
3133 struct got_repository *repo, int report_unchanged)
3135 const struct got_error *err = NULL;
3136 unsigned char status = GOT_STATUS_NO_CHANGE;
3137 unsigned char staged_status = get_staged_status(ie);
3139 struct got_object_id blob_id, commit_id, staged_blob_id;
3140 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3141 struct got_object_id *staged_blob_idp = NULL;
3143 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3147 if (status == GOT_STATUS_NO_CHANGE &&
3148 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3151 if (got_fileindex_entry_has_blob(ie)) {
3152 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3153 blob_idp = &blob_id;
3155 if (got_fileindex_entry_has_commit(ie)) {
3156 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3157 commit_idp = &commit_id;
3159 if (staged_status == GOT_STATUS_ADD ||
3160 staged_status == GOT_STATUS_MODIFY) {
3161 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3162 SHA1_DIGEST_LENGTH);
3163 staged_blob_idp = &staged_blob_id;
3166 return (*status_cb)(status_arg, status, staged_status,
3167 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3170 static const struct got_error *
3171 status_old_new(void *arg, struct got_fileindex_entry *ie,
3172 struct dirent *de, const char *parent_path, int dirfd)
3174 const struct got_error *err = NULL;
3175 struct diff_dir_cb_arg *a = arg;
3178 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3179 return got_error(GOT_ERR_CANCELLED);
3181 if (got_path_cmp(parent_path, a->status_path,
3182 strlen(parent_path), a->status_path_len) != 0 &&
3183 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3186 if (parent_path[0]) {
3187 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3188 parent_path, de->d_name) == -1)
3189 return got_error_from_errno("asprintf");
3191 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3193 return got_error_from_errno("asprintf");
3196 err = report_file_status(ie, abspath, dirfd, de->d_name,
3197 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3202 static const struct got_error *
3203 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3205 struct diff_dir_cb_arg *a = arg;
3206 struct got_object_id blob_id, commit_id;
3207 unsigned char status;
3209 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3210 return got_error(GOT_ERR_CANCELLED);
3212 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3215 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3216 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3217 if (got_fileindex_entry_has_file_on_disk(ie))
3218 status = GOT_STATUS_MISSING;
3220 status = GOT_STATUS_DELETE;
3221 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3222 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3226 free_ignorelist(struct got_pathlist_head *ignorelist)
3228 struct got_pathlist_entry *pe;
3230 TAILQ_FOREACH(pe, ignorelist, entry)
3231 free((char *)pe->path);
3232 got_pathlist_free(ignorelist);
3236 free_ignores(struct got_pathlist_head *ignores)
3238 struct got_pathlist_entry *pe;
3240 TAILQ_FOREACH(pe, ignores, entry) {
3241 struct got_pathlist_head *ignorelist = pe->data;
3242 free_ignorelist(ignorelist);
3243 free((char *)pe->path);
3245 got_pathlist_free(ignores);
3248 static const struct got_error *
3249 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3251 const struct got_error *err = NULL;
3252 struct got_pathlist_entry *pe = NULL;
3253 struct got_pathlist_head *ignorelist;
3254 char *line = NULL, *pattern, *dirpath = NULL;
3255 size_t linesize = 0;
3258 ignorelist = calloc(1, sizeof(*ignorelist));
3259 if (ignorelist == NULL)
3260 return got_error_from_errno("calloc");
3261 TAILQ_INIT(ignorelist);
3263 while ((linelen = getline(&line, &linesize, f)) != -1) {
3264 if (linelen > 0 && line[linelen - 1] == '\n')
3265 line[linelen - 1] = '\0';
3267 /* Git's ignores may contain comments. */
3271 /* Git's negated patterns are not (yet?) supported. */
3275 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3277 err = got_error_from_errno("asprintf");
3280 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3285 err = got_error_from_errno("getline");
3289 dirpath = strdup(path);
3290 if (dirpath == NULL) {
3291 err = got_error_from_errno("strdup");
3294 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3297 if (err || pe == NULL) {
3299 free_ignorelist(ignorelist);
3305 match_ignores(struct got_pathlist_head *ignores, const char *path)
3307 struct got_pathlist_entry *pe;
3309 /* Handle patterns which match in all directories. */
3310 TAILQ_FOREACH(pe, ignores, entry) {
3311 struct got_pathlist_head *ignorelist = pe->data;
3312 struct got_pathlist_entry *pi;
3314 TAILQ_FOREACH(pi, ignorelist, entry) {
3315 const char *p, *pattern = pi->path;
3317 if (strncmp(pattern, "**/", 3) != 0)
3322 if (fnmatch(pattern, p,
3323 FNM_PATHNAME | FNM_LEADING_DIR)) {
3324 /* Retry in next directory. */
3325 while (*p && *p != '/')
3337 * The ignores pathlist contains ignore lists from children before
3338 * parents, so we can find the most specific ignorelist by walking
3339 * ignores backwards.
3341 pe = TAILQ_LAST(ignores, got_pathlist_head);
3343 if (got_path_is_child(path, pe->path, pe->path_len)) {
3344 struct got_pathlist_head *ignorelist = pe->data;
3345 struct got_pathlist_entry *pi;
3346 TAILQ_FOREACH(pi, ignorelist, entry) {
3347 const char *pattern = pi->path;
3348 int flags = FNM_LEADING_DIR;
3349 if (strstr(pattern, "/**/") == NULL)
3350 flags |= FNM_PATHNAME;
3351 if (fnmatch(pattern, path, flags))
3356 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3362 static const struct got_error *
3363 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3364 const char *path, int dirfd, const char *ignores_filename)
3366 const struct got_error *err = NULL;
3369 FILE *ignoresfile = NULL;
3371 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3372 path[0] ? "/" : "", ignores_filename) == -1)
3373 return got_error_from_errno("asprintf");
3376 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3378 if (errno != ENOENT && errno != EACCES)
3379 err = got_error_from_errno2("openat",
3382 ignoresfile = fdopen(fd, "r");
3383 if (ignoresfile == NULL)
3384 err = got_error_from_errno2("fdopen",
3388 err = read_ignores(ignores, path, ignoresfile);
3392 ignoresfile = fopen(ignorespath, "r");
3393 if (ignoresfile == NULL) {
3394 if (errno != ENOENT && errno != EACCES)
3395 err = got_error_from_errno2("fopen",
3398 err = read_ignores(ignores, path, ignoresfile);
3401 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3402 err = got_error_from_errno2("fclose", path);
3403 if (fd != -1 && close(fd) == -1 && err == NULL)
3404 err = got_error_from_errno2("close", path);
3409 static const struct got_error *
3410 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3412 const struct got_error *err = NULL;
3413 struct diff_dir_cb_arg *a = arg;
3416 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3417 return got_error(GOT_ERR_CANCELLED);
3419 if (parent_path[0]) {
3420 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3421 return got_error_from_errno("asprintf");
3426 if (de->d_type != DT_DIR &&
3427 got_path_is_child(path, a->status_path, a->status_path_len)
3428 && !match_ignores(&a->ignores, path))
3429 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3430 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3436 static const struct got_error *
3437 status_traverse(void *arg, const char *path, int dirfd)
3439 const struct got_error *err = NULL;
3440 struct diff_dir_cb_arg *a = arg;
3445 err = add_ignores(&a->ignores, a->worktree->root_path,
3446 path, dirfd, ".cvsignore");
3450 err = add_ignores(&a->ignores, a->worktree->root_path, path,
3451 dirfd, ".gitignore");
3456 static const struct got_error *
3457 report_single_file_status(const char *path, const char *ondisk_path,
3458 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3459 void *status_arg, struct got_repository *repo, int report_unchanged)
3461 struct got_fileindex_entry *ie;
3464 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3466 return report_file_status(ie, ondisk_path, -1, NULL,
3467 status_cb, status_arg, repo, report_unchanged);
3469 if (lstat(ondisk_path, &sb) == -1) {
3470 if (errno != ENOENT)
3471 return got_error_from_errno2("lstat", ondisk_path);
3472 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3473 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3477 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3478 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3479 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3484 static const struct got_error *
3485 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3486 const char *root_path, const char *path)
3488 const struct got_error *err;
3489 char *parent_path, *next_parent_path = NULL;
3491 err = add_ignores(ignores, root_path, "", -1,
3496 err = add_ignores(ignores, root_path, "", -1,
3501 err = got_path_dirname(&parent_path, path);
3503 if (err->code == GOT_ERR_BAD_PATH)
3504 return NULL; /* cannot traverse parent */
3508 err = add_ignores(ignores, root_path, parent_path, -1,
3512 err = add_ignores(ignores, root_path, parent_path, -1,
3516 err = got_path_dirname(&next_parent_path, parent_path);
3518 if (err->code == GOT_ERR_BAD_PATH)
3519 err = NULL; /* traversed everything */
3523 parent_path = next_parent_path;
3524 next_parent_path = NULL;
3528 free(next_parent_path);
3532 static const struct got_error *
3533 worktree_status(struct got_worktree *worktree, const char *path,
3534 struct got_fileindex *fileindex, struct got_repository *repo,
3535 got_worktree_status_cb status_cb, void *status_arg,
3536 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3537 int report_unchanged)
3539 const struct got_error *err = NULL;
3541 struct got_fileindex_diff_dir_cb fdiff_cb;
3542 struct diff_dir_cb_arg arg;
3543 char *ondisk_path = NULL;
3545 TAILQ_INIT(&arg.ignores);
3547 if (asprintf(&ondisk_path, "%s%s%s",
3548 worktree->root_path, path[0] ? "/" : "", path) == -1)
3549 return got_error_from_errno("asprintf");
3551 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3553 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3555 err = got_error_from_errno2("open", ondisk_path);
3557 err = report_single_file_status(path, ondisk_path,
3558 fileindex, status_cb, status_arg, repo,
3561 fdiff_cb.diff_old_new = status_old_new;
3562 fdiff_cb.diff_old = status_old;
3563 fdiff_cb.diff_new = status_new;
3564 fdiff_cb.diff_traverse = status_traverse;
3565 arg.fileindex = fileindex;
3566 arg.worktree = worktree;
3567 arg.status_path = path;
3568 arg.status_path_len = strlen(path);
3570 arg.status_cb = status_cb;
3571 arg.status_arg = status_arg;
3572 arg.cancel_cb = cancel_cb;
3573 arg.cancel_arg = cancel_arg;
3574 arg.report_unchanged = report_unchanged;
3575 arg.no_ignores = no_ignores;
3577 err = add_ignores_from_parent_paths(&arg.ignores,
3578 worktree->root_path, path);
3582 err = got_fileindex_diff_dir(fileindex, fd,
3583 worktree->root_path, path, repo, &fdiff_cb, &arg);
3586 free_ignores(&arg.ignores);
3587 if (fd != -1 && close(fd) != 0 && err == NULL)
3588 err = got_error_from_errno("close");
3593 const struct got_error *
3594 got_worktree_status(struct got_worktree *worktree,
3595 struct got_pathlist_head *paths, struct got_repository *repo,
3596 got_worktree_status_cb status_cb, void *status_arg,
3597 got_cancel_cb cancel_cb, void *cancel_arg)
3599 const struct got_error *err = NULL;
3600 char *fileindex_path = NULL;
3601 struct got_fileindex *fileindex = NULL;
3602 struct got_pathlist_entry *pe;
3604 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3608 TAILQ_FOREACH(pe, paths, entry) {
3609 err = worktree_status(worktree, pe->path, fileindex, repo,
3610 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3614 free(fileindex_path);
3615 got_fileindex_free(fileindex);
3619 const struct got_error *
3620 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3623 const struct got_error *err = NULL;
3624 char *resolved = NULL, *cwd = NULL, *path = NULL;
3630 cwd = getcwd(NULL, 0);
3632 return got_error_from_errno("getcwd");
3634 if (lstat(arg, &sb) == -1) {
3635 if (errno != ENOENT) {
3636 err = got_error_from_errno2("lstat", arg);
3640 if (S_ISLNK(sb.st_mode)) {
3642 * We cannot use realpath(3) with symlinks since we want to
3643 * operate on the symlink itself.
3644 * But we can make the path absolute, assuming it is relative
3645 * to the current working directory, and then canonicalize it.
3647 char *abspath = NULL;
3648 char canonpath[PATH_MAX];
3649 if (!got_path_is_absolute(arg)) {
3650 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3651 err = got_error_from_errno("asprintf");
3656 err = got_canonpath(abspath ? abspath : arg, canonpath,
3660 resolved = strdup(canonpath);
3661 if (resolved == NULL) {
3662 err = got_error_from_errno("strdup");
3666 resolved = realpath(arg, NULL);
3667 if (resolved == NULL) {
3668 if (errno != ENOENT) {
3669 err = got_error_from_errno2("realpath", arg);
3672 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
3673 err = got_error_from_errno("asprintf");
3679 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3680 strlen(got_worktree_get_root_path(worktree)))) {
3681 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3685 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3686 err = got_path_skip_common_ancestor(&path,
3687 got_worktree_get_root_path(worktree), resolved);
3693 err = got_error_from_errno("strdup");
3698 /* XXX status walk can't deal with trailing slash! */
3700 while (len > 0 && path[len - 1] == '/') {
3701 path[len - 1] = '\0';
3714 struct schedule_addition_args {
3715 struct got_worktree *worktree;
3716 struct got_fileindex *fileindex;
3717 got_worktree_checkout_cb progress_cb;
3719 struct got_repository *repo;
3722 static const struct got_error *
3723 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3724 const char *relpath, struct got_object_id *blob_id,
3725 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3726 int dirfd, const char *de_name)
3728 struct schedule_addition_args *a = arg;
3729 const struct got_error *err = NULL;
3730 struct got_fileindex_entry *ie;
3734 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3736 return got_error_from_errno("asprintf");
3738 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3740 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3744 /* Re-adding an existing entry is a no-op. */
3745 if (status == GOT_STATUS_ADD)
3747 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3752 if (status != GOT_STATUS_UNVERSIONED) {
3753 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3757 err = got_fileindex_entry_alloc(&ie, relpath);
3760 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3762 got_fileindex_entry_free(ie);
3765 err = got_fileindex_entry_add(a->fileindex, ie);
3767 got_fileindex_entry_free(ie);
3774 if (status == GOT_STATUS_ADD)
3776 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3779 const struct got_error *
3780 got_worktree_schedule_add(struct got_worktree *worktree,
3781 struct got_pathlist_head *paths,
3782 got_worktree_checkout_cb progress_cb, void *progress_arg,
3783 struct got_repository *repo, int no_ignores)
3785 struct got_fileindex *fileindex = NULL;
3786 char *fileindex_path = NULL;
3787 const struct got_error *err = NULL, *sync_err, *unlockerr;
3788 struct got_pathlist_entry *pe;
3789 struct schedule_addition_args saa;
3791 err = lock_worktree(worktree, LOCK_EX);
3795 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3799 saa.worktree = worktree;
3800 saa.fileindex = fileindex;
3801 saa.progress_cb = progress_cb;
3802 saa.progress_arg = progress_arg;
3805 TAILQ_FOREACH(pe, paths, entry) {
3806 err = worktree_status(worktree, pe->path, fileindex, repo,
3807 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3811 sync_err = sync_fileindex(fileindex, fileindex_path);
3812 if (sync_err && err == NULL)
3815 free(fileindex_path);
3817 got_fileindex_free(fileindex);
3818 unlockerr = lock_worktree(worktree, LOCK_SH);
3819 if (unlockerr && err == NULL)
3824 struct schedule_deletion_args {
3825 struct got_worktree *worktree;
3826 struct got_fileindex *fileindex;
3827 got_worktree_delete_cb progress_cb;
3829 struct got_repository *repo;
3830 int delete_local_mods;
3832 const char *status_codes;
3835 static const struct got_error *
3836 schedule_for_deletion(void *arg, unsigned char status,
3837 unsigned char staged_status, const char *relpath,
3838 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3839 struct got_object_id *commit_id, int dirfd, const char *de_name)
3841 struct schedule_deletion_args *a = arg;
3842 const struct got_error *err = NULL;
3843 struct got_fileindex_entry *ie = NULL;
3845 char *ondisk_path, *parent = NULL;
3847 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3849 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3851 staged_status = get_staged_status(ie);
3852 if (staged_status != GOT_STATUS_NO_CHANGE) {
3853 if (staged_status == GOT_STATUS_DELETE)
3855 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3858 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3860 return got_error_from_errno("asprintf");
3862 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3867 if (a->status_codes) {
3868 size_t ncodes = strlen(a->status_codes);
3870 for (i = 0; i < ncodes ; i++) {
3871 if (status == a->status_codes[i])
3875 /* Do not delete files in non-matching status. */
3879 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
3880 a->status_codes[i] != GOT_STATUS_MISSING) {
3881 static char msg[64];
3882 snprintf(msg, sizeof(msg),
3883 "invalid status code '%c'", a->status_codes[i]);
3884 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
3889 if (status != GOT_STATUS_NO_CHANGE) {
3890 if (status == GOT_STATUS_DELETE)
3892 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3893 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3896 if (status != GOT_STATUS_MODIFY &&
3897 status != GOT_STATUS_MISSING) {
3898 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3903 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3905 if (unlinkat(dirfd, de_name, 0) != 0) {
3906 err = got_error_from_errno2("unlinkat",
3910 } else if (unlink(ondisk_path) != 0) {
3911 err = got_error_from_errno2("unlink", ondisk_path);
3915 parent = dirname(ondisk_path);
3917 if (parent == NULL) {
3918 err = got_error_from_errno2("dirname", ondisk_path);
3921 while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3922 if (rmdir(parent) == -1) {
3923 if (errno != ENOTEMPTY)
3924 err = got_error_from_errno2("rmdir",
3928 parent = dirname(parent);
3929 if (parent == NULL) {
3930 err = got_error_from_errno2("dirname", parent);
3936 got_fileindex_entry_mark_deleted_from_disk(ie);
3941 if (status == GOT_STATUS_DELETE)
3943 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3944 staged_status, relpath);
3947 const struct got_error *
3948 got_worktree_schedule_delete(struct got_worktree *worktree,
3949 struct got_pathlist_head *paths, int delete_local_mods,
3950 const char *status_codes,
3951 got_worktree_delete_cb progress_cb, void *progress_arg,
3952 struct got_repository *repo, int keep_on_disk)
3954 struct got_fileindex *fileindex = NULL;
3955 char *fileindex_path = NULL;
3956 const struct got_error *err = NULL, *sync_err, *unlockerr;
3957 struct got_pathlist_entry *pe;
3958 struct schedule_deletion_args sda;
3960 err = lock_worktree(worktree, LOCK_EX);
3964 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3968 sda.worktree = worktree;
3969 sda.fileindex = fileindex;
3970 sda.progress_cb = progress_cb;
3971 sda.progress_arg = progress_arg;
3973 sda.delete_local_mods = delete_local_mods;
3974 sda.keep_on_disk = keep_on_disk;
3975 sda.status_codes = status_codes;
3977 TAILQ_FOREACH(pe, paths, entry) {
3978 err = worktree_status(worktree, pe->path, fileindex, repo,
3979 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3983 sync_err = sync_fileindex(fileindex, fileindex_path);
3984 if (sync_err && err == NULL)
3987 free(fileindex_path);
3989 got_fileindex_free(fileindex);
3990 unlockerr = lock_worktree(worktree, LOCK_SH);
3991 if (unlockerr && err == NULL)
3996 static const struct got_error *
3997 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3999 const struct got_error *err = NULL;
4001 size_t linesize = 0, n;
4004 linelen = getline(&line, &linesize, infile);
4005 if (linelen == -1) {
4006 if (ferror(infile)) {
4007 err = got_error_from_errno("getline");
4013 n = fwrite(line, 1, linelen, outfile);
4015 err = got_ferror(outfile, GOT_ERR_IO);
4020 n = fwrite(line, 1, linelen, rejectfile);
4022 err = got_ferror(outfile, GOT_ERR_IO);
4029 static const struct got_error *
4030 skip_one_line(FILE *f)
4033 size_t linesize = 0;
4036 linelen = getline(&line, &linesize, f);
4037 if (linelen == -1) {
4039 return got_error_from_errno("getline");
4046 static const struct got_error *
4047 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4048 int start_old, int end_old, int start_new, int end_new,
4049 FILE *outfile, FILE *rejectfile)
4051 const struct got_error *err;
4053 /* Copy old file's lines leading up to patch. */
4054 while (!feof(f1) && *line_cur1 < start_old) {
4055 err = copy_one_line(f1, outfile, NULL);
4060 /* Skip new file's lines leading up to patch. */
4061 while (!feof(f2) && *line_cur2 < start_new) {
4063 err = copy_one_line(f2, NULL, rejectfile);
4065 err = skip_one_line(f2);
4070 /* Copy patched lines. */
4071 while (!feof(f2) && *line_cur2 <= end_new) {
4072 err = copy_one_line(f2, outfile, NULL);
4077 /* Skip over old file's replaced lines. */
4078 while (!feof(f1) && *line_cur1 <= end_old) {
4080 err = copy_one_line(f1, NULL, rejectfile);
4082 err = skip_one_line(f1);
4091 static const struct got_error *
4092 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4093 FILE *outfile, FILE *rejectfile)
4095 const struct got_error *err;
4098 /* Copy old file's lines until EOF. */
4100 err = copy_one_line(f1, outfile, NULL);
4107 /* Copy new file's lines until EOF. */
4109 err = copy_one_line(f2, NULL, rejectfile);
4119 static const struct got_error *
4120 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
4121 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
4122 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
4123 int *line_cur2, FILE *outfile, FILE *rejectfile,
4124 got_worktree_patch_cb patch_cb, void *patch_arg)
4126 const struct got_error *err = NULL;
4127 int start_old = change->cv.a;
4128 int end_old = change->cv.b;
4129 int start_new = change->cv.c;
4130 int end_new = change->cv.d;
4134 *choice = GOT_PATCH_CHOICE_NONE;
4136 hunkfile = got_opentemp();
4137 if (hunkfile == NULL)
4138 return got_error_from_errno("got_opentemp");
4143 /* XXX TODO needs error checking */
4144 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
4146 if (fseek(f1, pos1, SEEK_SET) == -1) {
4147 err = got_ferror(f1, GOT_ERR_IO);
4150 if (fseek(f2, pos2, SEEK_SET) == -1) {
4151 err = got_ferror(f1, GOT_ERR_IO);
4154 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4155 err = got_ferror(hunkfile, GOT_ERR_IO);
4159 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4160 hunkfile, n, nchanges);
4165 case GOT_PATCH_CHOICE_YES:
4166 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4167 end_old, start_new, end_new, outfile, rejectfile);
4169 case GOT_PATCH_CHOICE_NO:
4170 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4171 end_old, start_new, end_new, rejectfile, outfile);
4173 case GOT_PATCH_CHOICE_QUIT:
4176 err = got_error(GOT_ERR_PATCH_CHOICE);
4180 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4181 err = got_error_from_errno("fclose");
4185 struct revert_file_args {
4186 struct got_worktree *worktree;
4187 struct got_fileindex *fileindex;
4188 got_worktree_checkout_cb progress_cb;
4190 got_worktree_patch_cb patch_cb;
4192 struct got_repository *repo;
4195 static const struct got_error *
4196 create_patched_content(char **path_outfile, int reverse_patch,
4197 struct got_object_id *blob_id, const char *path2,
4198 int dirfd2, const char *de_name2,
4199 const char *relpath, struct got_repository *repo,
4200 got_worktree_patch_cb patch_cb, void *patch_arg)
4202 const struct got_error *err;
4203 struct got_blob_object *blob = NULL;
4204 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4206 char link_target[PATH_MAX];
4207 ssize_t link_len = 0;
4208 char *path1 = NULL, *id_str = NULL;
4209 struct stat sb1, sb2;
4210 struct got_diff_changes *changes = NULL;
4211 struct got_diff_state *ds = NULL;
4212 struct got_diff_args *args = NULL;
4213 struct got_diff_change *change;
4214 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
4217 *path_outfile = NULL;
4219 err = got_object_id_str(&id_str, blob_id);
4224 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4226 if (errno != ELOOP) {
4227 err = got_error_from_errno2("openat", path2);
4230 link_len = readlinkat(dirfd2, de_name2,
4231 link_target, sizeof(link_target));
4233 return got_error_from_errno2("readlinkat", path2);
4234 sb2.st_mode = S_IFLNK;
4235 sb2.st_size = link_len;
4238 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4240 if (errno != ELOOP) {
4241 err = got_error_from_errno2("open", path2);
4244 link_len = readlink(path2, link_target,
4245 sizeof(link_target));
4247 return got_error_from_errno2("readlink", path2);
4248 sb2.st_mode = S_IFLNK;
4249 sb2.st_size = link_len;
4253 if (fstat(fd2, &sb2) == -1) {
4254 err = got_error_from_errno2("fstat", path2);
4258 f2 = fdopen(fd2, "r");
4260 err = got_error_from_errno2("fdopen", path2);
4266 f2 = got_opentemp();
4268 err = got_error_from_errno2("got_opentemp", path2);
4271 n = fwrite(link_target, 1, link_len, f2);
4272 if (n != link_len) {
4273 err = got_ferror(f2, GOT_ERR_IO);
4276 if (fflush(f2) == EOF) {
4277 err = got_error_from_errno("fflush");
4283 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4287 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4291 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4295 if (stat(path1, &sb1) == -1) {
4296 err = got_error_from_errno2("stat", path1);
4300 err = got_diff_files(&changes, &ds, &args, &diff_flags,
4301 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
4305 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4309 if (fseek(f1, 0L, SEEK_SET) == -1)
4310 return got_ferror(f1, GOT_ERR_IO);
4311 if (fseek(f2, 0L, SEEK_SET) == -1)
4312 return got_ferror(f2, GOT_ERR_IO);
4313 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
4315 err = apply_or_reject_change(&choice, change, ++n,
4316 changes->nchanges, ds, args, diff_flags, relpath,
4317 f1, f2, &line_cur1, &line_cur2,
4318 reverse_patch ? NULL : outfile,
4319 reverse_patch ? outfile : NULL,
4320 patch_cb, patch_arg);
4323 if (choice == GOT_PATCH_CHOICE_YES)
4325 else if (choice == GOT_PATCH_CHOICE_QUIT)
4329 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4330 reverse_patch ? NULL : outfile,
4331 reverse_patch ? outfile : NULL);
4335 if (!S_ISLNK(sb2.st_mode)) {
4336 if (chmod(*path_outfile, sb2.st_mode) == -1) {
4337 err = got_error_from_errno2("chmod", path2);
4345 got_object_blob_close(blob);
4346 if (f1 && fclose(f1) == EOF && err == NULL)
4347 err = got_error_from_errno2("fclose", path1);
4348 if (f2 && fclose(f2) == EOF && err == NULL)
4349 err = got_error_from_errno2("fclose", path2);
4350 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4351 err = got_error_from_errno2("close", path2);
4352 if (outfile && fclose(outfile) == EOF && err == NULL)
4353 err = got_error_from_errno2("fclose", *path_outfile);
4354 if (path1 && unlink(path1) == -1 && err == NULL)
4355 err = got_error_from_errno2("unlink", path1);
4356 if (err || !have_content) {
4357 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4358 err = got_error_from_errno2("unlink", *path_outfile);
4359 free(*path_outfile);
4360 *path_outfile = NULL;
4364 got_diff_state_free(ds);
4368 got_diff_free_changes(changes);
4373 static const struct got_error *
4374 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4375 const char *relpath, struct got_object_id *blob_id,
4376 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4377 int dirfd, const char *de_name)
4379 struct revert_file_args *a = arg;
4380 const struct got_error *err = NULL;
4381 char *parent_path = NULL;
4382 struct got_fileindex_entry *ie;
4383 struct got_tree_object *tree = NULL;
4384 struct got_object_id *tree_id = NULL;
4385 const struct got_tree_entry *te = NULL;
4386 char *tree_path = NULL, *te_name;
4387 char *ondisk_path = NULL, *path_content = NULL;
4388 struct got_blob_object *blob = NULL;
4390 /* Reverting a staged deletion is a no-op. */
4391 if (status == GOT_STATUS_DELETE &&
4392 staged_status != GOT_STATUS_NO_CHANGE)
4395 if (status == GOT_STATUS_UNVERSIONED)
4396 return (*a->progress_cb)(a->progress_arg,
4397 GOT_STATUS_UNVERSIONED, relpath);
4399 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4401 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4403 /* Construct in-repository path of tree which contains this blob. */
4404 err = got_path_dirname(&parent_path, ie->path);
4406 if (err->code != GOT_ERR_BAD_PATH)
4408 parent_path = strdup("/");
4409 if (parent_path == NULL) {
4410 err = got_error_from_errno("strdup");
4414 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4415 tree_path = strdup(parent_path);
4416 if (tree_path == NULL) {
4417 err = got_error_from_errno("strdup");
4421 if (got_path_is_root_dir(parent_path)) {
4422 tree_path = strdup(a->worktree->path_prefix);
4423 if (tree_path == NULL) {
4424 err = got_error_from_errno("strdup");
4428 if (asprintf(&tree_path, "%s/%s",
4429 a->worktree->path_prefix, parent_path) == -1) {
4430 err = got_error_from_errno("asprintf");
4436 err = got_object_id_by_path(&tree_id, a->repo,
4437 a->worktree->base_commit_id, tree_path);
4439 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4440 (status == GOT_STATUS_ADD ||
4441 staged_status == GOT_STATUS_ADD)))
4444 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4448 err = got_path_basename(&te_name, ie->path);
4452 te = got_object_tree_find_entry(tree, te_name);
4454 if (te == NULL && status != GOT_STATUS_ADD &&
4455 staged_status != GOT_STATUS_ADD) {
4456 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4462 case GOT_STATUS_ADD:
4464 int choice = GOT_PATCH_CHOICE_NONE;
4465 err = (*a->patch_cb)(&choice, a->patch_arg,
4466 status, ie->path, NULL, 1, 1);
4469 if (choice != GOT_PATCH_CHOICE_YES)
4472 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4476 got_fileindex_entry_remove(a->fileindex, ie);
4478 case GOT_STATUS_DELETE:
4480 int choice = GOT_PATCH_CHOICE_NONE;
4481 err = (*a->patch_cb)(&choice, a->patch_arg,
4482 status, ie->path, NULL, 1, 1);
4485 if (choice != GOT_PATCH_CHOICE_YES)
4489 case GOT_STATUS_MODIFY:
4490 case GOT_STATUS_MODE_CHANGE:
4491 case GOT_STATUS_CONFLICT:
4492 case GOT_STATUS_MISSING: {
4493 struct got_object_id id;
4494 if (staged_status == GOT_STATUS_ADD ||
4495 staged_status == GOT_STATUS_MODIFY) {
4496 memcpy(id.sha1, ie->staged_blob_sha1,
4497 SHA1_DIGEST_LENGTH);
4499 memcpy(id.sha1, ie->blob_sha1,
4500 SHA1_DIGEST_LENGTH);
4501 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4505 if (asprintf(&ondisk_path, "%s/%s",
4506 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4507 err = got_error_from_errno("asprintf");
4511 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4512 status == GOT_STATUS_CONFLICT)) {
4513 int is_bad_symlink = 0;
4514 err = create_patched_content(&path_content, 1, &id,
4515 ondisk_path, dirfd, de_name, ie->path, a->repo,
4516 a->patch_cb, a->patch_arg);
4517 if (err || path_content == NULL)
4519 if (te && S_ISLNK(te->mode)) {
4520 if (unlink(path_content) == -1) {
4521 err = got_error_from_errno2("unlink",
4525 err = install_symlink(&is_bad_symlink,
4526 a->worktree, ondisk_path, ie->path,
4527 blob, 0, 1, 0, a->repo,
4528 a->progress_cb, a->progress_arg);
4530 if (rename(path_content, ondisk_path) == -1) {
4531 err = got_error_from_errno3("rename",
4532 path_content, ondisk_path);
4537 int is_bad_symlink = 0;
4538 if (te && S_ISLNK(te->mode)) {
4539 err = install_symlink(&is_bad_symlink,
4540 a->worktree, ondisk_path, ie->path,
4541 blob, 0, 1, 0, a->repo,
4542 a->progress_cb, a->progress_arg);
4544 err = install_blob(a->worktree, ondisk_path,
4546 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4547 got_fileindex_perms_to_st(ie), blob,
4548 0, 1, 0, 0, a->repo,
4549 a->progress_cb, a->progress_arg);
4553 if (status == GOT_STATUS_DELETE ||
4554 status == GOT_STATUS_MODE_CHANGE) {
4555 err = got_fileindex_entry_update(ie,
4556 ondisk_path, blob->id.sha1,
4557 a->worktree->base_commit_id->sha1, 1);
4561 if (is_bad_symlink) {
4562 got_fileindex_entry_filetype_set(ie,
4563 GOT_FILEIDX_MODE_BAD_SYMLINK);
4577 got_object_blob_close(blob);
4579 got_object_tree_close(tree);
4584 const struct got_error *
4585 got_worktree_revert(struct got_worktree *worktree,
4586 struct got_pathlist_head *paths,
4587 got_worktree_checkout_cb progress_cb, void *progress_arg,
4588 got_worktree_patch_cb patch_cb, void *patch_arg,
4589 struct got_repository *repo)
4591 struct got_fileindex *fileindex = NULL;
4592 char *fileindex_path = NULL;
4593 const struct got_error *err = NULL, *unlockerr = NULL;
4594 const struct got_error *sync_err = NULL;
4595 struct got_pathlist_entry *pe;
4596 struct revert_file_args rfa;
4598 err = lock_worktree(worktree, LOCK_EX);
4602 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4606 rfa.worktree = worktree;
4607 rfa.fileindex = fileindex;
4608 rfa.progress_cb = progress_cb;
4609 rfa.progress_arg = progress_arg;
4610 rfa.patch_cb = patch_cb;
4611 rfa.patch_arg = patch_arg;
4613 TAILQ_FOREACH(pe, paths, entry) {
4614 err = worktree_status(worktree, pe->path, fileindex, repo,
4615 revert_file, &rfa, NULL, NULL, 0, 0);
4619 sync_err = sync_fileindex(fileindex, fileindex_path);
4620 if (sync_err && err == NULL)
4623 free(fileindex_path);
4625 got_fileindex_free(fileindex);
4626 unlockerr = lock_worktree(worktree, LOCK_SH);
4627 if (unlockerr && err == NULL)
4633 free_commitable(struct got_commitable *ct)
4636 free(ct->in_repo_path);
4637 free(ct->ondisk_path);
4639 free(ct->base_blob_id);
4640 free(ct->staged_blob_id);
4641 free(ct->base_commit_id);
4645 struct collect_commitables_arg {
4646 struct got_pathlist_head *commitable_paths;
4647 struct got_repository *repo;
4648 struct got_worktree *worktree;
4649 struct got_fileindex *fileindex;
4650 int have_staged_files;
4651 int allow_bad_symlinks;
4654 static const struct got_error *
4655 collect_commitables(void *arg, unsigned char status,
4656 unsigned char staged_status, const char *relpath,
4657 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4658 struct got_object_id *commit_id, int dirfd, const char *de_name)
4660 struct collect_commitables_arg *a = arg;
4661 const struct got_error *err = NULL;
4662 struct got_commitable *ct = NULL;
4663 struct got_pathlist_entry *new = NULL;
4664 char *parent_path = NULL, *path = NULL;
4667 if (a->have_staged_files) {
4668 if (staged_status != GOT_STATUS_MODIFY &&
4669 staged_status != GOT_STATUS_ADD &&
4670 staged_status != GOT_STATUS_DELETE)
4673 if (status == GOT_STATUS_CONFLICT)
4674 return got_error(GOT_ERR_COMMIT_CONFLICT);
4676 if (status != GOT_STATUS_MODIFY &&
4677 status != GOT_STATUS_MODE_CHANGE &&
4678 status != GOT_STATUS_ADD &&
4679 status != GOT_STATUS_DELETE)
4683 if (asprintf(&path, "/%s", relpath) == -1) {
4684 err = got_error_from_errno("asprintf");
4687 if (strcmp(path, "/") == 0) {
4688 parent_path = strdup("");
4689 if (parent_path == NULL)
4690 return got_error_from_errno("strdup");
4692 err = got_path_dirname(&parent_path, path);
4697 ct = calloc(1, sizeof(*ct));
4699 err = got_error_from_errno("calloc");
4703 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4705 err = got_error_from_errno("asprintf");
4709 if (staged_status == GOT_STATUS_ADD ||
4710 staged_status == GOT_STATUS_MODIFY) {
4711 struct got_fileindex_entry *ie;
4712 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4713 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4714 case GOT_FILEIDX_MODE_REGULAR_FILE:
4715 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4718 case GOT_FILEIDX_MODE_SYMLINK:
4722 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4725 ct->mode |= got_fileindex_entry_perms_get(ie);
4726 } else if (status != GOT_STATUS_DELETE &&
4727 staged_status != GOT_STATUS_DELETE) {
4729 if (fstatat(dirfd, de_name, &sb,
4730 AT_SYMLINK_NOFOLLOW) == -1) {
4731 err = got_error_from_errno2("fstatat",
4735 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4736 err = got_error_from_errno2("lstat", ct->ondisk_path);
4739 ct->mode = sb.st_mode;
4742 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4743 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4745 err = got_error_from_errno("asprintf");
4749 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4750 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4752 char target_path[PATH_MAX];
4754 target_len = readlink(ct->ondisk_path, target_path,
4755 sizeof(target_path));
4756 if (target_len == -1) {
4757 err = got_error_from_errno2("readlink",
4761 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4762 target_len, ct->ondisk_path, a->worktree->root_path);
4765 if (is_bad_symlink) {
4766 err = got_error_path(ct->ondisk_path,
4767 GOT_ERR_BAD_SYMLINK);
4773 ct->status = status;
4774 ct->staged_status = staged_status;
4775 ct->blob_id = NULL; /* will be filled in when blob gets created */
4776 if (ct->status != GOT_STATUS_ADD &&
4777 ct->staged_status != GOT_STATUS_ADD) {
4778 ct->base_blob_id = got_object_id_dup(blob_id);
4779 if (ct->base_blob_id == NULL) {
4780 err = got_error_from_errno("got_object_id_dup");
4783 ct->base_commit_id = got_object_id_dup(commit_id);
4784 if (ct->base_commit_id == NULL) {
4785 err = got_error_from_errno("got_object_id_dup");
4789 if (ct->staged_status == GOT_STATUS_ADD ||
4790 ct->staged_status == GOT_STATUS_MODIFY) {
4791 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4792 if (ct->staged_blob_id == NULL) {
4793 err = got_error_from_errno("got_object_id_dup");
4797 ct->path = strdup(path);
4798 if (ct->path == NULL) {
4799 err = got_error_from_errno("strdup");
4802 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4804 if (ct && (err || new == NULL))
4805 free_commitable(ct);
4811 static const struct got_error *write_tree(struct got_object_id **, int *,
4812 struct got_tree_object *, const char *, struct got_pathlist_head *,
4813 got_worktree_status_cb status_cb, void *status_arg,
4814 struct got_repository *);
4816 static const struct got_error *
4817 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4818 struct got_tree_entry *te, const char *parent_path,
4819 struct got_pathlist_head *commitable_paths,
4820 got_worktree_status_cb status_cb, void *status_arg,
4821 struct got_repository *repo)
4823 const struct got_error *err = NULL;
4824 struct got_tree_object *subtree;
4827 if (asprintf(&subpath, "%s%s%s", parent_path,
4828 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4829 return got_error_from_errno("asprintf");
4831 err = got_object_open_as_tree(&subtree, repo, &te->id);
4835 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4836 commitable_paths, status_cb, status_arg, repo);
4837 got_object_tree_close(subtree);
4842 static const struct got_error *
4843 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4845 const struct got_error *err = NULL;
4846 char *ct_parent_path = NULL;
4850 if (strchr(ct->in_repo_path, '/') == NULL) {
4851 *match = got_path_is_root_dir(path);
4855 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4858 *match = (strcmp(path, ct_parent_path) == 0);
4859 free(ct_parent_path);
4864 get_ct_file_mode(struct got_commitable *ct)
4866 if (S_ISLNK(ct->mode))
4869 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4872 static const struct got_error *
4873 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4874 struct got_tree_entry *te, struct got_commitable *ct)
4876 const struct got_error *err = NULL;
4880 err = got_object_tree_entry_dup(new_te, te);
4884 (*new_te)->mode = get_ct_file_mode(ct);
4886 if (ct->staged_status == GOT_STATUS_MODIFY)
4887 memcpy(&(*new_te)->id, ct->staged_blob_id,
4888 sizeof((*new_te)->id));
4890 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4892 if (err && *new_te) {
4899 static const struct got_error *
4900 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4901 struct got_commitable *ct)
4903 const struct got_error *err = NULL;
4904 char *ct_name = NULL;
4908 *new_te = calloc(1, sizeof(**new_te));
4909 if (*new_te == NULL)
4910 return got_error_from_errno("calloc");
4912 err = got_path_basename(&ct_name, ct->path);
4915 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4916 sizeof((*new_te)->name)) {
4917 err = got_error(GOT_ERR_NO_SPACE);
4921 (*new_te)->mode = get_ct_file_mode(ct);
4923 if (ct->staged_status == GOT_STATUS_ADD)
4924 memcpy(&(*new_te)->id, ct->staged_blob_id,
4925 sizeof((*new_te)->id));
4927 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4930 if (err && *new_te) {
4937 static const struct got_error *
4938 insert_tree_entry(struct got_tree_entry *new_te,
4939 struct got_pathlist_head *paths)
4941 const struct got_error *err = NULL;
4942 struct got_pathlist_entry *new_pe;
4944 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4948 return got_error(GOT_ERR_TREE_DUP_ENTRY);
4952 static const struct got_error *
4953 report_ct_status(struct got_commitable *ct,
4954 got_worktree_status_cb status_cb, void *status_arg)
4956 const char *ct_path = ct->path;
4957 unsigned char status;
4959 while (ct_path[0] == '/')
4962 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4963 status = ct->staged_status;
4965 status = ct->status;
4967 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4968 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4971 static const struct got_error *
4972 match_modified_subtree(int *modified, struct got_tree_entry *te,
4973 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4975 const struct got_error *err = NULL;
4976 struct got_pathlist_entry *pe;
4981 if (asprintf(&te_path, "%s%s%s", base_tree_path,
4982 got_path_is_root_dir(base_tree_path) ? "" : "/",
4984 return got_error_from_errno("asprintf");
4986 TAILQ_FOREACH(pe, commitable_paths, entry) {
4987 struct got_commitable *ct = pe->data;
4988 *modified = got_path_is_child(ct->in_repo_path, te_path,
4998 static const struct got_error *
4999 match_deleted_or_modified_ct(struct got_commitable **ctp,
5000 struct got_tree_entry *te, const char *base_tree_path,
5001 struct got_pathlist_head *commitable_paths)
5003 const struct got_error *err = NULL;
5004 struct got_pathlist_entry *pe;
5008 TAILQ_FOREACH(pe, commitable_paths, entry) {
5009 struct got_commitable *ct = pe->data;
5010 char *ct_name = NULL;
5013 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5014 if (ct->status != GOT_STATUS_MODIFY &&
5015 ct->status != GOT_STATUS_MODE_CHANGE &&
5016 ct->status != GOT_STATUS_DELETE)
5019 if (ct->staged_status != GOT_STATUS_MODIFY &&
5020 ct->staged_status != GOT_STATUS_DELETE)
5024 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5027 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5033 err = got_path_basename(&ct_name, pe->path);
5037 if (strcmp(te->name, ct_name) != 0) {
5050 static const struct got_error *
5051 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5052 const char *child_path, const char *path_base_tree,
5053 struct got_pathlist_head *commitable_paths,
5054 got_worktree_status_cb status_cb, void *status_arg,
5055 struct got_repository *repo)
5057 const struct got_error *err = NULL;
5058 struct got_tree_entry *new_te;
5060 struct got_object_id *id = NULL;
5065 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5066 got_path_is_root_dir(path_base_tree) ? "" : "/",
5068 return got_error_from_errno("asprintf");
5070 new_te = calloc(1, sizeof(*new_te));
5072 return got_error_from_errno("calloc");
5073 new_te->mode = S_IFDIR;
5075 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5076 sizeof(new_te->name)) {
5077 err = got_error(GOT_ERR_NO_SPACE);
5080 err = write_tree(&id, &nentries, NULL, subtree_path,
5081 commitable_paths, status_cb, status_arg, repo);
5086 memcpy(&new_te->id, id, sizeof(new_te->id));
5095 static const struct got_error *
5096 write_tree(struct got_object_id **new_tree_id, int *nentries,
5097 struct got_tree_object *base_tree, const char *path_base_tree,
5098 struct got_pathlist_head *commitable_paths,
5099 got_worktree_status_cb status_cb, void *status_arg,
5100 struct got_repository *repo)
5102 const struct got_error *err = NULL;
5103 struct got_pathlist_head paths;
5104 struct got_tree_entry *te, *new_te = NULL;
5105 struct got_pathlist_entry *pe;
5110 /* Insert, and recurse into, newly added entries first. */
5111 TAILQ_FOREACH(pe, commitable_paths, entry) {
5112 struct got_commitable *ct = pe->data;
5113 char *child_path = NULL, *slash;
5115 if ((ct->status != GOT_STATUS_ADD &&
5116 ct->staged_status != GOT_STATUS_ADD) ||
5117 (ct->flags & GOT_COMMITABLE_ADDED))
5120 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5121 strlen(path_base_tree)))
5124 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5129 slash = strchr(child_path, '/');
5130 if (slash == NULL) {
5131 err = alloc_added_blob_tree_entry(&new_te, ct);
5134 err = report_ct_status(ct, status_cb, status_arg);
5137 ct->flags |= GOT_COMMITABLE_ADDED;
5138 err = insert_tree_entry(new_te, &paths);
5143 *slash = '\0'; /* trim trailing path components */
5144 if (base_tree == NULL ||
5145 got_object_tree_find_entry(base_tree, child_path)
5147 err = make_subtree_for_added_blob(&new_te,
5148 child_path, path_base_tree,
5149 commitable_paths, status_cb, status_arg,
5153 err = insert_tree_entry(new_te, &paths);
5162 int i, nbase_entries;
5163 /* Handle modified and deleted entries. */
5164 nbase_entries = got_object_tree_get_nentries(base_tree);
5165 for (i = 0; i < nbase_entries; i++) {
5166 struct got_commitable *ct = NULL;
5168 te = got_object_tree_get_entry(base_tree, i);
5169 if (got_object_tree_entry_is_submodule(te)) {
5170 /* Entry is a submodule; just copy it. */
5171 err = got_object_tree_entry_dup(&new_te, te);
5174 err = insert_tree_entry(new_te, &paths);
5181 if (S_ISDIR(te->mode)) {
5183 err = got_object_tree_entry_dup(&new_te, te);
5186 err = match_modified_subtree(&modified, te,
5187 path_base_tree, commitable_paths);
5190 /* Avoid recursion into unmodified subtrees. */
5192 struct got_object_id *new_id;
5194 err = write_subtree(&new_id,
5196 path_base_tree, commitable_paths,
5197 status_cb, status_arg, repo);
5200 if (nsubentries == 0) {
5201 /* All entries were deleted. */
5205 memcpy(&new_te->id, new_id,
5206 sizeof(new_te->id));
5209 err = insert_tree_entry(new_te, &paths);
5216 err = match_deleted_or_modified_ct(&ct, te,
5217 path_base_tree, commitable_paths);
5221 /* NB: Deleted entries get dropped here. */
5222 if (ct->status == GOT_STATUS_MODIFY ||
5223 ct->status == GOT_STATUS_MODE_CHANGE ||
5224 ct->staged_status == GOT_STATUS_MODIFY) {
5225 err = alloc_modified_blob_tree_entry(
5229 err = insert_tree_entry(new_te, &paths);
5234 err = report_ct_status(ct, status_cb,
5239 /* Entry is unchanged; just copy it. */
5240 err = got_object_tree_entry_dup(&new_te, te);
5243 err = insert_tree_entry(new_te, &paths);
5251 /* Write new list of entries; deleted entries have been dropped. */
5252 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5254 got_pathlist_free(&paths);
5258 static const struct got_error *
5259 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
5260 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
5261 int have_staged_files)
5263 const struct got_error *err = NULL;
5264 struct got_pathlist_entry *pe;
5266 TAILQ_FOREACH(pe, commitable_paths, entry) {
5267 struct got_fileindex_entry *ie;
5268 struct got_commitable *ct = pe->data;
5270 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5272 if (ct->status == GOT_STATUS_DELETE ||
5273 ct->staged_status == GOT_STATUS_DELETE) {
5274 got_fileindex_entry_remove(fileindex, ie);
5275 } else if (ct->staged_status == GOT_STATUS_ADD ||
5276 ct->staged_status == GOT_STATUS_MODIFY) {
5277 got_fileindex_entry_stage_set(ie,
5278 GOT_FILEIDX_STAGE_NONE);
5279 err = got_fileindex_entry_update(ie,
5280 ct->ondisk_path, ct->staged_blob_id->sha1,
5281 new_base_commit_id->sha1,
5282 !have_staged_files);
5284 err = got_fileindex_entry_update(ie,
5285 ct->ondisk_path, ct->blob_id->sha1,
5286 new_base_commit_id->sha1,
5287 !have_staged_files);
5289 err = got_fileindex_entry_alloc(&ie, pe->path);
5292 err = got_fileindex_entry_update(ie, ct->ondisk_path,
5293 ct->blob_id->sha1, new_base_commit_id->sha1, 1);
5295 got_fileindex_entry_free(ie);
5298 err = got_fileindex_entry_add(fileindex, ie);
5300 got_fileindex_entry_free(ie);
5309 static const struct got_error *
5310 check_out_of_date(const char *in_repo_path, unsigned char status,
5311 unsigned char staged_status, struct got_object_id *base_blob_id,
5312 struct got_object_id *base_commit_id,
5313 struct got_object_id *head_commit_id, struct got_repository *repo,
5316 const struct got_error *err = NULL;
5317 struct got_object_id *id = NULL;
5319 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5320 /* Trivial case: base commit == head commit */
5321 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5324 * Ensure file content which local changes were based
5325 * on matches file content in the branch head.
5327 err = got_object_id_by_path(&id, repo, head_commit_id,
5330 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5331 err = got_error(ood_errcode);
5333 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5334 err = got_error(ood_errcode);
5336 /* Require that added files don't exist in the branch head. */
5337 err = got_object_id_by_path(&id, repo, head_commit_id,
5339 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5341 err = id ? got_error(ood_errcode) : NULL;
5348 const struct got_error *
5349 commit_worktree(struct got_object_id **new_commit_id,
5350 struct got_pathlist_head *commitable_paths,
5351 struct got_object_id *head_commit_id, struct got_worktree *worktree,
5352 const char *author, const char *committer,
5353 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5354 got_worktree_status_cb status_cb, void *status_arg,
5355 struct got_repository *repo)
5357 const struct got_error *err = NULL, *unlockerr = NULL;
5358 struct got_pathlist_entry *pe;
5359 const char *head_ref_name = NULL;
5360 struct got_commit_object *head_commit = NULL;
5361 struct got_reference *head_ref2 = NULL;
5362 struct got_object_id *head_commit_id2 = NULL;
5363 struct got_tree_object *head_tree = NULL;
5364 struct got_object_id *new_tree_id = NULL;
5366 struct got_object_id_queue parent_ids;
5367 struct got_object_qid *pid = NULL;
5368 char *logmsg = NULL;
5370 *new_commit_id = NULL;
5372 SIMPLEQ_INIT(&parent_ids);
5374 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5378 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5382 if (commit_msg_cb != NULL) {
5383 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5388 if (logmsg == NULL || strlen(logmsg) == 0) {
5389 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5393 /* Create blobs from added and modified files and record their IDs. */
5394 TAILQ_FOREACH(pe, commitable_paths, entry) {
5395 struct got_commitable *ct = pe->data;
5398 /* Blobs for staged files already exist. */
5399 if (ct->staged_status == GOT_STATUS_ADD ||
5400 ct->staged_status == GOT_STATUS_MODIFY)
5403 if (ct->status != GOT_STATUS_ADD &&
5404 ct->status != GOT_STATUS_MODIFY &&
5405 ct->status != GOT_STATUS_MODE_CHANGE)
5408 if (asprintf(&ondisk_path, "%s/%s",
5409 worktree->root_path, pe->path) == -1) {
5410 err = got_error_from_errno("asprintf");
5413 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5419 /* Recursively write new tree objects. */
5420 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5421 commitable_paths, status_cb, status_arg, repo);
5425 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5428 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5429 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5430 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5431 got_object_qid_free(pid);
5437 /* Check if a concurrent commit to our branch has occurred. */
5438 head_ref_name = got_worktree_get_head_ref_name(worktree);
5439 if (head_ref_name == NULL) {
5440 err = got_error_from_errno("got_worktree_get_head_ref_name");
5443 /* Lock the reference here to prevent concurrent modification. */
5444 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5447 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5450 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5451 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5454 /* Update branch head in repository. */
5455 err = got_ref_change_ref(head_ref2, *new_commit_id);
5458 err = got_ref_write(head_ref2, repo);
5462 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5466 err = ref_base_commit(worktree, repo);
5471 got_object_tree_close(head_tree);
5473 got_object_commit_close(head_commit);
5474 free(head_commit_id2);
5476 unlockerr = got_ref_unlock(head_ref2);
5477 if (unlockerr && err == NULL)
5479 got_ref_close(head_ref2);
5484 static const struct got_error *
5485 check_path_is_commitable(const char *path,
5486 struct got_pathlist_head *commitable_paths)
5488 struct got_pathlist_entry *cpe = NULL;
5489 size_t path_len = strlen(path);
5491 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5492 struct got_commitable *ct = cpe->data;
5493 const char *ct_path = ct->path;
5495 while (ct_path[0] == '/')
5498 if (strcmp(path, ct_path) == 0 ||
5499 got_path_is_child(ct_path, path, path_len))
5504 return got_error_path(path, GOT_ERR_BAD_PATH);
5509 static const struct got_error *
5510 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5512 int *have_staged_files = arg;
5514 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5515 *have_staged_files = 1;
5516 return got_error(GOT_ERR_CANCELLED);
5522 static const struct got_error *
5523 check_non_staged_files(struct got_fileindex *fileindex,
5524 struct got_pathlist_head *paths)
5526 struct got_pathlist_entry *pe;
5527 struct got_fileindex_entry *ie;
5529 TAILQ_FOREACH(pe, paths, entry) {
5530 if (pe->path[0] == '\0')
5532 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5534 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5535 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5536 return got_error_path(pe->path,
5537 GOT_ERR_FILE_NOT_STAGED);
5543 const struct got_error *
5544 got_worktree_commit(struct got_object_id **new_commit_id,
5545 struct got_worktree *worktree, struct got_pathlist_head *paths,
5546 const char *author, const char *committer, int allow_bad_symlinks,
5547 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5548 got_worktree_status_cb status_cb, void *status_arg,
5549 struct got_repository *repo)
5551 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5552 struct got_fileindex *fileindex = NULL;
5553 char *fileindex_path = NULL;
5554 struct got_pathlist_head commitable_paths;
5555 struct collect_commitables_arg cc_arg;
5556 struct got_pathlist_entry *pe;
5557 struct got_reference *head_ref = NULL;
5558 struct got_object_id *head_commit_id = NULL;
5559 int have_staged_files = 0;
5561 *new_commit_id = NULL;
5563 TAILQ_INIT(&commitable_paths);
5565 err = lock_worktree(worktree, LOCK_EX);
5569 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5573 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5577 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5581 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5582 &have_staged_files);
5583 if (err && err->code != GOT_ERR_CANCELLED)
5585 if (have_staged_files) {
5586 err = check_non_staged_files(fileindex, paths);
5591 cc_arg.commitable_paths = &commitable_paths;
5592 cc_arg.worktree = worktree;
5593 cc_arg.fileindex = fileindex;
5595 cc_arg.have_staged_files = have_staged_files;
5596 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5597 TAILQ_FOREACH(pe, paths, entry) {
5598 err = worktree_status(worktree, pe->path, fileindex, repo,
5599 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5604 if (TAILQ_EMPTY(&commitable_paths)) {
5605 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5609 TAILQ_FOREACH(pe, paths, entry) {
5610 err = check_path_is_commitable(pe->path, &commitable_paths);
5615 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5616 struct got_commitable *ct = pe->data;
5617 const char *ct_path = ct->in_repo_path;
5619 while (ct_path[0] == '/')
5621 err = check_out_of_date(ct_path, ct->status,
5622 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5623 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5629 err = commit_worktree(new_commit_id, &commitable_paths,
5630 head_commit_id, worktree, author, committer,
5631 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5635 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5636 fileindex, have_staged_files);
5637 sync_err = sync_fileindex(fileindex, fileindex_path);
5638 if (sync_err && err == NULL)
5642 got_fileindex_free(fileindex);
5643 free(fileindex_path);
5644 unlockerr = lock_worktree(worktree, LOCK_SH);
5645 if (unlockerr && err == NULL)
5647 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5648 struct got_commitable *ct = pe->data;
5649 free_commitable(ct);
5651 got_pathlist_free(&commitable_paths);
5656 got_commitable_get_path(struct got_commitable *ct)
5662 got_commitable_get_status(struct got_commitable *ct)
5667 struct check_rebase_ok_arg {
5668 struct got_worktree *worktree;
5669 struct got_repository *repo;
5672 static const struct got_error *
5673 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5675 const struct got_error *err = NULL;
5676 struct check_rebase_ok_arg *a = arg;
5677 unsigned char status;
5681 /* Reject rebase of a work tree with mixed base commits. */
5682 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5683 SHA1_DIGEST_LENGTH))
5684 return got_error(GOT_ERR_MIXED_COMMITS);
5686 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5688 return got_error_from_errno("asprintf");
5690 /* Reject rebase of a work tree with modified or staged files. */
5691 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5696 if (status != GOT_STATUS_NO_CHANGE)
5697 return got_error(GOT_ERR_MODIFIED);
5698 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5699 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5704 const struct got_error *
5705 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5706 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5707 struct got_worktree *worktree, struct got_reference *branch,
5708 struct got_repository *repo)
5710 const struct got_error *err = NULL;
5711 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5712 char *branch_ref_name = NULL;
5713 char *fileindex_path = NULL;
5714 struct check_rebase_ok_arg ok_arg;
5715 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5716 struct got_object_id *wt_branch_tip = NULL;
5718 *new_base_branch_ref = NULL;
5722 err = lock_worktree(worktree, LOCK_EX);
5726 err = open_fileindex(fileindex, &fileindex_path, worktree);
5730 ok_arg.worktree = worktree;
5732 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5737 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5741 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5745 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5749 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5754 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5757 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5758 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5762 err = got_ref_alloc_symref(new_base_branch_ref,
5763 new_base_branch_ref_name, wt_branch);
5766 err = got_ref_write(*new_base_branch_ref, repo);
5770 /* TODO Lock original branch's ref while rebasing? */
5772 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5776 err = got_ref_write(branch_ref, repo);
5780 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5781 worktree->base_commit_id);
5784 err = got_ref_write(*tmp_branch, repo);
5788 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5792 free(fileindex_path);
5793 free(tmp_branch_name);
5794 free(new_base_branch_ref_name);
5795 free(branch_ref_name);
5797 got_ref_close(branch_ref);
5799 got_ref_close(wt_branch);
5800 free(wt_branch_tip);
5802 if (*new_base_branch_ref) {
5803 got_ref_close(*new_base_branch_ref);
5804 *new_base_branch_ref = NULL;
5807 got_ref_close(*tmp_branch);
5811 got_fileindex_free(*fileindex);
5814 lock_worktree(worktree, LOCK_SH);
5819 const struct got_error *
5820 got_worktree_rebase_continue(struct got_object_id **commit_id,
5821 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5822 struct got_reference **branch, struct got_fileindex **fileindex,
5823 struct got_worktree *worktree, struct got_repository *repo)
5825 const struct got_error *err;
5826 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5827 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5828 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5829 char *fileindex_path = NULL;
5830 int have_staged_files = 0;
5833 *new_base_branch = NULL;
5838 err = lock_worktree(worktree, LOCK_EX);
5842 err = open_fileindex(fileindex, &fileindex_path, worktree);
5846 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5847 &have_staged_files);
5848 if (err && err->code != GOT_ERR_CANCELLED)
5850 if (have_staged_files) {
5851 err = got_error(GOT_ERR_STAGED_PATHS);
5855 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5859 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5863 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5867 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5871 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5875 err = got_ref_open(branch, repo,
5876 got_ref_get_symref_target(branch_ref), 0);
5880 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5884 err = got_ref_resolve(commit_id, repo, commit_ref);
5888 err = got_ref_open(new_base_branch, repo,
5889 new_base_branch_ref_name, 0);
5893 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5897 free(commit_ref_name);
5898 free(branch_ref_name);
5899 free(fileindex_path);
5901 got_ref_close(commit_ref);
5903 got_ref_close(branch_ref);
5908 got_ref_close(*tmp_branch);
5911 if (*new_base_branch) {
5912 got_ref_close(*new_base_branch);
5913 *new_base_branch = NULL;
5916 got_ref_close(*branch);
5920 got_fileindex_free(*fileindex);
5923 lock_worktree(worktree, LOCK_SH);
5928 const struct got_error *
5929 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5931 const struct got_error *err;
5932 char *tmp_branch_name = NULL;
5934 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5938 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5939 free(tmp_branch_name);
5943 static const struct got_error *
5944 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5945 char **logmsg, void *arg)
5951 static const struct got_error *
5952 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5953 const char *path, struct got_object_id *blob_id,
5954 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5955 int dirfd, const char *de_name)
5960 struct collect_merged_paths_arg {
5961 got_worktree_checkout_cb progress_cb;
5963 struct got_pathlist_head *merged_paths;
5966 static const struct got_error *
5967 collect_merged_paths(void *arg, unsigned char status, const char *path)
5969 const struct got_error *err;
5970 struct collect_merged_paths_arg *a = arg;
5972 struct got_pathlist_entry *new;
5974 err = (*a->progress_cb)(a->progress_arg, status, path);
5978 if (status != GOT_STATUS_MERGE &&
5979 status != GOT_STATUS_ADD &&
5980 status != GOT_STATUS_DELETE &&
5981 status != GOT_STATUS_CONFLICT)
5986 return got_error_from_errno("strdup");
5988 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5989 if (err || new == NULL)
5995 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5997 struct got_pathlist_entry *pe;
5999 TAILQ_FOREACH(pe, merged_paths, entry)
6000 free((char *)pe->path);
6002 got_pathlist_free(merged_paths);
6005 static const struct got_error *
6006 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6007 int is_rebase, struct got_repository *repo)
6009 const struct got_error *err;
6010 struct got_reference *commit_ref = NULL;
6012 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6014 if (err->code != GOT_ERR_NOT_REF)
6016 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6019 err = got_ref_write(commit_ref, repo);
6022 } else if (is_rebase) {
6023 struct got_object_id *stored_id;
6026 err = got_ref_resolve(&stored_id, repo, commit_ref);
6029 cmp = got_object_id_cmp(commit_id, stored_id);
6032 err = got_error(GOT_ERR_REBASE_COMMITID);
6038 got_ref_close(commit_ref);
6042 static const struct got_error *
6043 rebase_merge_files(struct got_pathlist_head *merged_paths,
6044 const char *commit_ref_name, struct got_worktree *worktree,
6045 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6046 struct got_object_id *commit_id, struct got_repository *repo,
6047 got_worktree_checkout_cb progress_cb, void *progress_arg,
6048 got_cancel_cb cancel_cb, void *cancel_arg)
6050 const struct got_error *err;
6051 struct got_reference *commit_ref = NULL;
6052 struct collect_merged_paths_arg cmp_arg;
6053 char *fileindex_path;
6055 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6057 err = get_fileindex_path(&fileindex_path, worktree);
6061 cmp_arg.progress_cb = progress_cb;
6062 cmp_arg.progress_arg = progress_arg;
6063 cmp_arg.merged_paths = merged_paths;
6064 err = merge_files(worktree, fileindex, fileindex_path,
6065 parent_commit_id, commit_id, repo, collect_merged_paths,
6066 &cmp_arg, cancel_cb, cancel_arg);
6068 got_ref_close(commit_ref);
6072 const struct got_error *
6073 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6074 struct got_worktree *worktree, struct got_fileindex *fileindex,
6075 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6076 struct got_repository *repo,
6077 got_worktree_checkout_cb progress_cb, void *progress_arg,
6078 got_cancel_cb cancel_cb, void *cancel_arg)
6080 const struct got_error *err;
6081 char *commit_ref_name;
6083 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6087 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6091 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6092 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6093 progress_arg, cancel_cb, cancel_arg);
6095 free(commit_ref_name);
6099 const struct got_error *
6100 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6101 struct got_worktree *worktree, struct got_fileindex *fileindex,
6102 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6103 struct got_repository *repo,
6104 got_worktree_checkout_cb progress_cb, void *progress_arg,
6105 got_cancel_cb cancel_cb, void *cancel_arg)
6107 const struct got_error *err;
6108 char *commit_ref_name;
6110 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6114 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6118 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6119 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6120 progress_arg, cancel_cb, cancel_arg);
6122 free(commit_ref_name);
6126 static const struct got_error *
6127 rebase_commit(struct got_object_id **new_commit_id,
6128 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6129 struct got_worktree *worktree, struct got_fileindex *fileindex,
6130 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6131 const char *new_logmsg, struct got_repository *repo)
6133 const struct got_error *err, *sync_err;
6134 struct got_pathlist_head commitable_paths;
6135 struct collect_commitables_arg cc_arg;
6136 char *fileindex_path = NULL;
6137 struct got_reference *head_ref = NULL;
6138 struct got_object_id *head_commit_id = NULL;
6139 char *logmsg = NULL;
6141 TAILQ_INIT(&commitable_paths);
6142 *new_commit_id = NULL;
6144 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6146 err = get_fileindex_path(&fileindex_path, worktree);
6150 cc_arg.commitable_paths = &commitable_paths;
6151 cc_arg.worktree = worktree;
6153 cc_arg.have_staged_files = 0;
6155 * If possible get the status of individual files directly to
6156 * avoid crawling the entire work tree once per rebased commit.
6157 * TODO: Ideally, merged_paths would contain a list of commitables
6158 * we could use so we could skip worktree_status() entirely.
6161 struct got_pathlist_entry *pe;
6162 TAILQ_FOREACH(pe, merged_paths, entry) {
6163 err = worktree_status(worktree, pe->path, fileindex,
6164 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6170 err = worktree_status(worktree, "", fileindex, repo,
6171 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6176 if (TAILQ_EMPTY(&commitable_paths)) {
6177 /* No-op change; commit will be elided. */
6178 err = got_ref_delete(commit_ref, repo);
6181 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6185 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6189 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6194 logmsg = strdup(new_logmsg);
6195 if (logmsg == NULL) {
6196 err = got_error_from_errno("strdup");
6200 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6205 /* NB: commit_worktree will call free(logmsg) */
6206 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6207 worktree, got_object_commit_get_author(orig_commit),
6208 got_object_commit_get_committer(orig_commit),
6209 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6213 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6217 err = got_ref_delete(commit_ref, repo);
6221 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
6223 sync_err = sync_fileindex(fileindex, fileindex_path);
6224 if (sync_err && err == NULL)
6227 free(fileindex_path);
6228 free(head_commit_id);
6230 got_ref_close(head_ref);
6232 free(*new_commit_id);
6233 *new_commit_id = NULL;
6238 const struct got_error *
6239 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6240 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6241 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6242 struct got_commit_object *orig_commit,
6243 struct got_object_id *orig_commit_id, struct got_repository *repo)
6245 const struct got_error *err;
6246 char *commit_ref_name;
6247 struct got_reference *commit_ref = NULL;
6248 struct got_object_id *commit_id = NULL;
6250 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6254 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6257 err = got_ref_resolve(&commit_id, repo, commit_ref);
6260 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6261 err = got_error(GOT_ERR_REBASE_COMMITID);
6265 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6266 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6269 got_ref_close(commit_ref);
6270 free(commit_ref_name);
6275 const struct got_error *
6276 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6277 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6278 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6279 struct got_commit_object *orig_commit,
6280 struct got_object_id *orig_commit_id, const char *new_logmsg,
6281 struct got_repository *repo)
6283 const struct got_error *err;
6284 char *commit_ref_name;
6285 struct got_reference *commit_ref = NULL;
6287 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6291 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6295 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6296 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6299 got_ref_close(commit_ref);
6300 free(commit_ref_name);
6304 const struct got_error *
6305 got_worktree_rebase_postpone(struct got_worktree *worktree,
6306 struct got_fileindex *fileindex)
6309 got_fileindex_free(fileindex);
6310 return lock_worktree(worktree, LOCK_SH);
6313 static const struct got_error *
6314 delete_ref(const char *name, struct got_repository *repo)
6316 const struct got_error *err;
6317 struct got_reference *ref;
6319 err = got_ref_open(&ref, repo, name, 0);
6321 if (err->code == GOT_ERR_NOT_REF)
6326 err = got_ref_delete(ref, repo);
6331 static const struct got_error *
6332 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6334 const struct got_error *err;
6335 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6336 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6338 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6341 err = delete_ref(tmp_branch_name, repo);
6345 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6348 err = delete_ref(new_base_branch_ref_name, repo);
6352 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6355 err = delete_ref(branch_ref_name, repo);
6359 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6362 err = delete_ref(commit_ref_name, repo);
6367 free(tmp_branch_name);
6368 free(new_base_branch_ref_name);
6369 free(branch_ref_name);
6370 free(commit_ref_name);
6374 const struct got_error *
6375 got_worktree_rebase_complete(struct got_worktree *worktree,
6376 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6377 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6378 struct got_repository *repo)
6380 const struct got_error *err, *unlockerr;
6381 struct got_object_id *new_head_commit_id = NULL;
6383 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6387 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6391 err = got_ref_write(rebased_branch, repo);
6395 err = got_worktree_set_head_ref(worktree, rebased_branch);
6399 err = delete_rebase_refs(worktree, repo);
6402 got_fileindex_free(fileindex);
6403 free(new_head_commit_id);
6404 unlockerr = lock_worktree(worktree, LOCK_SH);
6405 if (unlockerr && err == NULL)
6410 const struct got_error *
6411 got_worktree_rebase_abort(struct got_worktree *worktree,
6412 struct got_fileindex *fileindex, struct got_repository *repo,
6413 struct got_reference *new_base_branch,
6414 got_worktree_checkout_cb progress_cb, void *progress_arg)
6416 const struct got_error *err, *unlockerr, *sync_err;
6417 struct got_reference *resolved = NULL;
6418 struct got_object_id *commit_id = NULL;
6419 char *fileindex_path = NULL;
6420 struct revert_file_args rfa;
6421 struct got_object_id *tree_id = NULL;
6423 err = lock_worktree(worktree, LOCK_EX);
6427 err = got_ref_open(&resolved, repo,
6428 got_ref_get_symref_target(new_base_branch), 0);
6432 err = got_worktree_set_head_ref(worktree, resolved);
6437 * XXX commits to the base branch could have happened while
6438 * we were busy rebasing; should we store the original commit ID
6439 * when rebase begins and read it back here?
6441 err = got_ref_resolve(&commit_id, repo, resolved);
6445 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6449 err = got_object_id_by_path(&tree_id, repo,
6450 worktree->base_commit_id, worktree->path_prefix);
6454 err = delete_rebase_refs(worktree, repo);
6458 err = get_fileindex_path(&fileindex_path, worktree);
6462 rfa.worktree = worktree;
6463 rfa.fileindex = fileindex;
6464 rfa.progress_cb = progress_cb;
6465 rfa.progress_arg = progress_arg;
6466 rfa.patch_cb = NULL;
6467 rfa.patch_arg = NULL;
6469 err = worktree_status(worktree, "", fileindex, repo,
6470 revert_file, &rfa, NULL, NULL, 0, 0);
6474 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6475 repo, progress_cb, progress_arg, NULL, NULL);
6477 sync_err = sync_fileindex(fileindex, fileindex_path);
6478 if (sync_err && err == NULL)
6481 got_ref_close(resolved);
6485 got_fileindex_free(fileindex);
6486 free(fileindex_path);
6488 unlockerr = lock_worktree(worktree, LOCK_SH);
6489 if (unlockerr && err == NULL)
6494 const struct got_error *
6495 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6496 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6497 struct got_fileindex **fileindex, struct got_worktree *worktree,
6498 struct got_repository *repo)
6500 const struct got_error *err = NULL;
6501 char *tmp_branch_name = NULL;
6502 char *branch_ref_name = NULL;
6503 char *base_commit_ref_name = NULL;
6504 char *fileindex_path = NULL;
6505 struct check_rebase_ok_arg ok_arg;
6506 struct got_reference *wt_branch = NULL;
6507 struct got_reference *base_commit_ref = NULL;
6511 *base_commit_id = NULL;
6514 err = lock_worktree(worktree, LOCK_EX);
6518 err = open_fileindex(fileindex, &fileindex_path, worktree);
6522 ok_arg.worktree = worktree;
6524 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6529 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6533 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6537 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6542 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6547 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6551 err = got_ref_write(*branch_ref, repo);
6555 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6556 worktree->base_commit_id);
6559 err = got_ref_write(base_commit_ref, repo);
6562 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6563 if (*base_commit_id == NULL) {
6564 err = got_error_from_errno("got_object_id_dup");
6568 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6569 worktree->base_commit_id);
6572 err = got_ref_write(*tmp_branch, repo);
6576 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6580 free(fileindex_path);
6581 free(tmp_branch_name);
6582 free(branch_ref_name);
6583 free(base_commit_ref_name);
6585 got_ref_close(wt_branch);
6588 got_ref_close(*branch_ref);
6592 got_ref_close(*tmp_branch);
6595 free(*base_commit_id);
6597 got_fileindex_free(*fileindex);
6600 lock_worktree(worktree, LOCK_SH);
6605 const struct got_error *
6606 got_worktree_histedit_postpone(struct got_worktree *worktree,
6607 struct got_fileindex *fileindex)
6610 got_fileindex_free(fileindex);
6611 return lock_worktree(worktree, LOCK_SH);
6614 const struct got_error *
6615 got_worktree_histedit_in_progress(int *in_progress,
6616 struct got_worktree *worktree)
6618 const struct got_error *err;
6619 char *tmp_branch_name = NULL;
6621 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6625 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6626 free(tmp_branch_name);
6630 const struct got_error *
6631 got_worktree_histedit_continue(struct got_object_id **commit_id,
6632 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6633 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6634 struct got_worktree *worktree, struct got_repository *repo)
6636 const struct got_error *err;
6637 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6638 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6639 struct got_reference *commit_ref = NULL;
6640 struct got_reference *base_commit_ref = NULL;
6641 char *fileindex_path = NULL;
6642 int have_staged_files = 0;
6646 *base_commit_id = NULL;
6649 err = lock_worktree(worktree, LOCK_EX);
6653 err = open_fileindex(fileindex, &fileindex_path, worktree);
6657 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6658 &have_staged_files);
6659 if (err && err->code != GOT_ERR_CANCELLED)
6661 if (have_staged_files) {
6662 err = got_error(GOT_ERR_STAGED_PATHS);
6666 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6670 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6674 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6678 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6683 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6687 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6690 err = got_ref_resolve(commit_id, repo, commit_ref);
6694 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6697 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6701 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6705 free(commit_ref_name);
6706 free(branch_ref_name);
6707 free(fileindex_path);
6709 got_ref_close(commit_ref);
6710 if (base_commit_ref)
6711 got_ref_close(base_commit_ref);
6715 free(*base_commit_id);
6716 *base_commit_id = NULL;
6718 got_ref_close(*tmp_branch);
6722 got_fileindex_free(*fileindex);
6725 lock_worktree(worktree, LOCK_EX);
6730 static const struct got_error *
6731 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6733 const struct got_error *err;
6734 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6735 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6737 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6740 err = delete_ref(tmp_branch_name, repo);
6744 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6748 err = delete_ref(base_commit_ref_name, repo);
6752 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6755 err = delete_ref(branch_ref_name, repo);
6759 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6762 err = delete_ref(commit_ref_name, repo);
6766 free(tmp_branch_name);
6767 free(base_commit_ref_name);
6768 free(branch_ref_name);
6769 free(commit_ref_name);
6773 const struct got_error *
6774 got_worktree_histedit_abort(struct got_worktree *worktree,
6775 struct got_fileindex *fileindex, struct got_repository *repo,
6776 struct got_reference *branch, struct got_object_id *base_commit_id,
6777 got_worktree_checkout_cb progress_cb, void *progress_arg)
6779 const struct got_error *err, *unlockerr, *sync_err;
6780 struct got_reference *resolved = NULL;
6781 char *fileindex_path = NULL;
6782 struct got_object_id *tree_id = NULL;
6783 struct revert_file_args rfa;
6785 err = lock_worktree(worktree, LOCK_EX);
6789 err = got_ref_open(&resolved, repo,
6790 got_ref_get_symref_target(branch), 0);
6794 err = got_worktree_set_head_ref(worktree, resolved);
6798 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6802 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6803 worktree->path_prefix);
6807 err = delete_histedit_refs(worktree, repo);
6811 err = get_fileindex_path(&fileindex_path, worktree);
6815 rfa.worktree = worktree;
6816 rfa.fileindex = fileindex;
6817 rfa.progress_cb = progress_cb;
6818 rfa.progress_arg = progress_arg;
6819 rfa.patch_cb = NULL;
6820 rfa.patch_arg = NULL;
6822 err = worktree_status(worktree, "", fileindex, repo,
6823 revert_file, &rfa, NULL, NULL, 0, 0);
6827 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6828 repo, progress_cb, progress_arg, NULL, NULL);
6830 sync_err = sync_fileindex(fileindex, fileindex_path);
6831 if (sync_err && err == NULL)
6834 got_ref_close(resolved);
6836 free(fileindex_path);
6838 unlockerr = lock_worktree(worktree, LOCK_SH);
6839 if (unlockerr && err == NULL)
6844 const struct got_error *
6845 got_worktree_histedit_complete(struct got_worktree *worktree,
6846 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6847 struct got_reference *edited_branch, struct got_repository *repo)
6849 const struct got_error *err, *unlockerr;
6850 struct got_object_id *new_head_commit_id = NULL;
6851 struct got_reference *resolved = NULL;
6853 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6857 err = got_ref_open(&resolved, repo,
6858 got_ref_get_symref_target(edited_branch), 0);
6862 err = got_ref_change_ref(resolved, new_head_commit_id);
6866 err = got_ref_write(resolved, repo);
6870 err = got_worktree_set_head_ref(worktree, resolved);
6874 err = delete_histedit_refs(worktree, repo);
6877 got_fileindex_free(fileindex);
6878 free(new_head_commit_id);
6879 unlockerr = lock_worktree(worktree, LOCK_SH);
6880 if (unlockerr && err == NULL)
6885 const struct got_error *
6886 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6887 struct got_object_id *commit_id, struct got_repository *repo)
6889 const struct got_error *err;
6890 char *commit_ref_name;
6892 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6896 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6900 err = delete_ref(commit_ref_name, repo);
6902 free(commit_ref_name);
6906 const struct got_error *
6907 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6908 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6909 struct got_worktree *worktree, const char *refname,
6910 struct got_repository *repo)
6912 const struct got_error *err = NULL;
6913 char *fileindex_path = NULL;
6914 struct check_rebase_ok_arg ok_arg;
6918 *base_branch_ref = NULL;
6920 err = lock_worktree(worktree, LOCK_EX);
6924 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6925 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6926 "cannot integrate a branch into itself; "
6927 "update -b or different branch name required");
6931 err = open_fileindex(fileindex, &fileindex_path, worktree);
6935 /* Preconditions are the same as for rebase. */
6936 ok_arg.worktree = worktree;
6938 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6943 err = got_ref_open(branch_ref, repo, refname, 1);
6947 err = got_ref_open(base_branch_ref, repo,
6948 got_worktree_get_head_ref_name(worktree), 1);
6952 got_ref_close(*branch_ref);
6955 if (*base_branch_ref) {
6956 got_ref_close(*base_branch_ref);
6957 *base_branch_ref = NULL;
6960 got_fileindex_free(*fileindex);
6963 lock_worktree(worktree, LOCK_SH);
6968 const struct got_error *
6969 got_worktree_integrate_continue(struct got_worktree *worktree,
6970 struct got_fileindex *fileindex, struct got_repository *repo,
6971 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6972 got_worktree_checkout_cb progress_cb, void *progress_arg,
6973 got_cancel_cb cancel_cb, void *cancel_arg)
6975 const struct got_error *err = NULL, *sync_err, *unlockerr;
6976 char *fileindex_path = NULL;
6977 struct got_object_id *tree_id = NULL, *commit_id = NULL;
6979 err = get_fileindex_path(&fileindex_path, worktree);
6983 err = got_ref_resolve(&commit_id, repo, branch_ref);
6987 err = got_object_id_by_path(&tree_id, repo, commit_id,
6988 worktree->path_prefix);
6992 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6996 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6997 progress_cb, progress_arg, cancel_cb, cancel_arg);
7001 err = got_ref_change_ref(base_branch_ref, commit_id);
7005 err = got_ref_write(base_branch_ref, repo);
7007 sync_err = sync_fileindex(fileindex, fileindex_path);
7008 if (sync_err && err == NULL)
7012 unlockerr = got_ref_unlock(branch_ref);
7013 if (unlockerr && err == NULL)
7015 got_ref_close(branch_ref);
7017 unlockerr = got_ref_unlock(base_branch_ref);
7018 if (unlockerr && err == NULL)
7020 got_ref_close(base_branch_ref);
7022 got_fileindex_free(fileindex);
7023 free(fileindex_path);
7026 unlockerr = lock_worktree(worktree, LOCK_SH);
7027 if (unlockerr && err == NULL)
7032 const struct got_error *
7033 got_worktree_integrate_abort(struct got_worktree *worktree,
7034 struct got_fileindex *fileindex, struct got_repository *repo,
7035 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7037 const struct got_error *err = NULL, *unlockerr = NULL;
7039 got_fileindex_free(fileindex);
7041 err = lock_worktree(worktree, LOCK_SH);
7043 unlockerr = got_ref_unlock(branch_ref);
7044 if (unlockerr && err == NULL)
7046 got_ref_close(branch_ref);
7048 unlockerr = got_ref_unlock(base_branch_ref);
7049 if (unlockerr && err == NULL)
7051 got_ref_close(base_branch_ref);
7056 struct check_stage_ok_arg {
7057 struct got_object_id *head_commit_id;
7058 struct got_worktree *worktree;
7059 struct got_fileindex *fileindex;
7060 struct got_repository *repo;
7064 const struct got_error *
7065 check_stage_ok(void *arg, unsigned char status,
7066 unsigned char staged_status, const char *relpath,
7067 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7068 struct got_object_id *commit_id, int dirfd, const char *de_name)
7070 struct check_stage_ok_arg *a = arg;
7071 const struct got_error *err = NULL;
7072 struct got_fileindex_entry *ie;
7073 struct got_object_id base_commit_id;
7074 struct got_object_id *base_commit_idp = NULL;
7075 char *in_repo_path = NULL, *p;
7077 if (status == GOT_STATUS_UNVERSIONED ||
7078 status == GOT_STATUS_NO_CHANGE)
7080 if (status == GOT_STATUS_NONEXISTENT)
7081 return got_error_set_errno(ENOENT, relpath);
7083 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7085 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7087 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7088 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7090 return got_error_from_errno("asprintf");
7092 if (got_fileindex_entry_has_commit(ie)) {
7093 memcpy(base_commit_id.sha1, ie->commit_sha1,
7094 SHA1_DIGEST_LENGTH);
7095 base_commit_idp = &base_commit_id;
7098 if (status == GOT_STATUS_CONFLICT) {
7099 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7101 } else if (status != GOT_STATUS_ADD &&
7102 status != GOT_STATUS_MODIFY &&
7103 status != GOT_STATUS_DELETE) {
7104 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7108 a->have_changes = 1;
7113 err = check_out_of_date(p, status, staged_status,
7114 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7115 GOT_ERR_STAGE_OUT_OF_DATE);
7121 struct stage_path_arg {
7122 struct got_worktree *worktree;
7123 struct got_fileindex *fileindex;
7124 struct got_repository *repo;
7125 got_worktree_status_cb status_cb;
7127 got_worktree_patch_cb patch_cb;
7129 int staged_something;
7130 int allow_bad_symlinks;
7133 static const struct got_error *
7134 stage_path(void *arg, unsigned char status,
7135 unsigned char staged_status, const char *relpath,
7136 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7137 struct got_object_id *commit_id, int dirfd, const char *de_name)
7139 struct stage_path_arg *a = arg;
7140 const struct got_error *err = NULL;
7141 struct got_fileindex_entry *ie;
7142 char *ondisk_path = NULL, *path_content = NULL;
7144 struct got_object_id *new_staged_blob_id = NULL;
7147 if (status == GOT_STATUS_UNVERSIONED)
7150 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7152 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7154 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7156 return got_error_from_errno("asprintf");
7159 case GOT_STATUS_ADD:
7160 case GOT_STATUS_MODIFY:
7161 /* XXX could sb.st_mode be passed in by our caller? */
7162 if (lstat(ondisk_path, &sb) == -1) {
7163 err = got_error_from_errno2("lstat", ondisk_path);
7167 if (status == GOT_STATUS_ADD) {
7168 int choice = GOT_PATCH_CHOICE_NONE;
7169 err = (*a->patch_cb)(&choice, a->patch_arg,
7170 status, ie->path, NULL, 1, 1);
7173 if (choice != GOT_PATCH_CHOICE_YES)
7176 err = create_patched_content(&path_content, 0,
7177 staged_blob_id ? staged_blob_id : blob_id,
7178 ondisk_path, dirfd, de_name, ie->path,
7179 a->repo, a->patch_cb, a->patch_arg);
7180 if (err || path_content == NULL)
7184 err = got_object_blob_create(&new_staged_blob_id,
7185 path_content ? path_content : ondisk_path, a->repo);
7188 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7189 SHA1_DIGEST_LENGTH);
7190 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7191 stage = GOT_FILEIDX_STAGE_ADD;
7193 stage = GOT_FILEIDX_STAGE_MODIFY;
7194 got_fileindex_entry_stage_set(ie, stage);
7195 if (S_ISLNK(sb.st_mode)) {
7196 int is_bad_symlink = 0;
7197 if (!a->allow_bad_symlinks) {
7198 char target_path[PATH_MAX];
7200 target_len = readlink(ondisk_path, target_path,
7201 sizeof(target_path));
7202 if (target_len == -1) {
7203 err = got_error_from_errno2("readlink",
7207 err = is_bad_symlink_target(&is_bad_symlink,
7208 target_path, target_len, ondisk_path,
7209 a->worktree->root_path);
7212 if (is_bad_symlink) {
7213 err = got_error_path(ondisk_path,
7214 GOT_ERR_BAD_SYMLINK);
7219 got_fileindex_entry_staged_filetype_set(ie,
7220 GOT_FILEIDX_MODE_BAD_SYMLINK);
7222 got_fileindex_entry_staged_filetype_set(ie,
7223 GOT_FILEIDX_MODE_SYMLINK);
7225 got_fileindex_entry_staged_filetype_set(ie,
7226 GOT_FILEIDX_MODE_REGULAR_FILE);
7228 a->staged_something = 1;
7229 if (a->status_cb == NULL)
7231 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7232 get_staged_status(ie), relpath, blob_id,
7233 new_staged_blob_id, NULL, dirfd, de_name);
7235 case GOT_STATUS_DELETE:
7236 if (staged_status == GOT_STATUS_DELETE)
7239 int choice = GOT_PATCH_CHOICE_NONE;
7240 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7241 ie->path, NULL, 1, 1);
7244 if (choice == GOT_PATCH_CHOICE_NO)
7246 if (choice != GOT_PATCH_CHOICE_YES) {
7247 err = got_error(GOT_ERR_PATCH_CHOICE);
7251 stage = GOT_FILEIDX_STAGE_DELETE;
7252 got_fileindex_entry_stage_set(ie, stage);
7253 a->staged_something = 1;
7254 if (a->status_cb == NULL)
7256 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7257 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7260 case GOT_STATUS_NO_CHANGE:
7262 case GOT_STATUS_CONFLICT:
7263 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7265 case GOT_STATUS_NONEXISTENT:
7266 err = got_error_set_errno(ENOENT, relpath);
7269 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7273 if (path_content && unlink(path_content) == -1 && err == NULL)
7274 err = got_error_from_errno2("unlink", path_content);
7277 free(new_staged_blob_id);
7281 const struct got_error *
7282 got_worktree_stage(struct got_worktree *worktree,
7283 struct got_pathlist_head *paths,
7284 got_worktree_status_cb status_cb, void *status_arg,
7285 got_worktree_patch_cb patch_cb, void *patch_arg,
7286 int allow_bad_symlinks, struct got_repository *repo)
7288 const struct got_error *err = NULL, *sync_err, *unlockerr;
7289 struct got_pathlist_entry *pe;
7290 struct got_fileindex *fileindex = NULL;
7291 char *fileindex_path = NULL;
7292 struct got_reference *head_ref = NULL;
7293 struct got_object_id *head_commit_id = NULL;
7294 struct check_stage_ok_arg oka;
7295 struct stage_path_arg spa;
7297 err = lock_worktree(worktree, LOCK_EX);
7301 err = got_ref_open(&head_ref, repo,
7302 got_worktree_get_head_ref_name(worktree), 0);
7305 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7308 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7312 /* Check pre-conditions before staging anything. */
7313 oka.head_commit_id = head_commit_id;
7314 oka.worktree = worktree;
7315 oka.fileindex = fileindex;
7317 oka.have_changes = 0;
7318 TAILQ_FOREACH(pe, paths, entry) {
7319 err = worktree_status(worktree, pe->path, fileindex, repo,
7320 check_stage_ok, &oka, NULL, NULL, 0, 0);
7324 if (!oka.have_changes) {
7325 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7329 spa.worktree = worktree;
7330 spa.fileindex = fileindex;
7332 spa.patch_cb = patch_cb;
7333 spa.patch_arg = patch_arg;
7334 spa.status_cb = status_cb;
7335 spa.status_arg = status_arg;
7336 spa.staged_something = 0;
7337 spa.allow_bad_symlinks = allow_bad_symlinks;
7338 TAILQ_FOREACH(pe, paths, entry) {
7339 err = worktree_status(worktree, pe->path, fileindex, repo,
7340 stage_path, &spa, NULL, NULL, 0, 0);
7344 if (!spa.staged_something) {
7345 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7349 sync_err = sync_fileindex(fileindex, fileindex_path);
7350 if (sync_err && err == NULL)
7354 got_ref_close(head_ref);
7355 free(head_commit_id);
7356 free(fileindex_path);
7358 got_fileindex_free(fileindex);
7359 unlockerr = lock_worktree(worktree, LOCK_SH);
7360 if (unlockerr && err == NULL)
7365 struct unstage_path_arg {
7366 struct got_worktree *worktree;
7367 struct got_fileindex *fileindex;
7368 struct got_repository *repo;
7369 got_worktree_checkout_cb progress_cb;
7371 got_worktree_patch_cb patch_cb;
7375 static const struct got_error *
7376 create_unstaged_content(char **path_unstaged_content,
7377 char **path_new_staged_content, struct got_object_id *blob_id,
7378 struct got_object_id *staged_blob_id, const char *relpath,
7379 struct got_repository *repo,
7380 got_worktree_patch_cb patch_cb, void *patch_arg)
7382 const struct got_error *err;
7383 struct got_blob_object *blob = NULL, *staged_blob = NULL;
7384 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7385 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7386 struct stat sb1, sb2;
7387 struct got_diff_changes *changes = NULL;
7388 struct got_diff_state *ds = NULL;
7389 struct got_diff_args *args = NULL;
7390 struct got_diff_change *change;
7391 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
7392 int have_content = 0, have_rejected_content = 0;
7394 *path_unstaged_content = NULL;
7395 *path_new_staged_content = NULL;
7397 err = got_object_id_str(&label1, blob_id);
7400 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7404 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7408 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7412 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7416 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7420 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7424 if (stat(path1, &sb1) == -1) {
7425 err = got_error_from_errno2("stat", path1);
7429 if (stat(path2, &sb2) == -1) {
7430 err = got_error_from_errno2("stat", path2);
7434 err = got_diff_files(&changes, &ds, &args, &diff_flags,
7435 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
7439 err = got_opentemp_named(path_unstaged_content, &outfile,
7440 "got-unstaged-content");
7443 err = got_opentemp_named(path_new_staged_content, &rejectfile,
7444 "got-new-staged-content");
7448 if (fseek(f1, 0L, SEEK_SET) == -1) {
7449 err = got_ferror(f1, GOT_ERR_IO);
7452 if (fseek(f2, 0L, SEEK_SET) == -1) {
7453 err = got_ferror(f2, GOT_ERR_IO);
7456 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
7458 err = apply_or_reject_change(&choice, change, ++n,
7459 changes->nchanges, ds, args, diff_flags, relpath,
7460 f1, f2, &line_cur1, &line_cur2,
7461 outfile, rejectfile, patch_cb, patch_arg);
7464 if (choice == GOT_PATCH_CHOICE_YES)
7467 have_rejected_content = 1;
7468 if (choice == GOT_PATCH_CHOICE_QUIT)
7471 if (have_content || have_rejected_content)
7472 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7473 outfile, rejectfile);
7477 got_object_blob_close(blob);
7479 got_object_blob_close(staged_blob);
7480 if (f1 && fclose(f1) == EOF && err == NULL)
7481 err = got_error_from_errno2("fclose", path1);
7482 if (f2 && fclose(f2) == EOF && err == NULL)
7483 err = got_error_from_errno2("fclose", path2);
7484 if (outfile && fclose(outfile) == EOF && err == NULL)
7485 err = got_error_from_errno2("fclose", *path_unstaged_content);
7486 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7487 err = got_error_from_errno2("fclose", *path_new_staged_content);
7488 if (path1 && unlink(path1) == -1 && err == NULL)
7489 err = got_error_from_errno2("unlink", path1);
7490 if (path2 && unlink(path2) == -1 && err == NULL)
7491 err = got_error_from_errno2("unlink", path2);
7492 if (err || !have_content) {
7493 if (*path_unstaged_content &&
7494 unlink(*path_unstaged_content) == -1 && err == NULL)
7495 err = got_error_from_errno2("unlink",
7496 *path_unstaged_content);
7497 free(*path_unstaged_content);
7498 *path_unstaged_content = NULL;
7500 if (err || !have_content || !have_rejected_content) {
7501 if (*path_new_staged_content &&
7502 unlink(*path_new_staged_content) == -1 && err == NULL)
7503 err = got_error_from_errno2("unlink",
7504 *path_new_staged_content);
7505 free(*path_new_staged_content);
7506 *path_new_staged_content = NULL;
7510 got_diff_state_free(ds);
7514 got_diff_free_changes(changes);
7520 static const struct got_error *
7521 unstage_hunks(struct got_object_id *staged_blob_id,
7522 struct got_blob_object *blob_base,
7523 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
7524 const char *ondisk_path, const char *label_orig,
7525 struct got_worktree *worktree, struct got_repository *repo,
7526 got_worktree_patch_cb patch_cb, void *patch_arg,
7527 got_worktree_checkout_cb progress_cb, void *progress_arg)
7529 const struct got_error *err = NULL;
7530 char *path_unstaged_content = NULL;
7531 char *path_new_staged_content = NULL;
7532 struct got_object_id *new_staged_blob_id = NULL;
7536 err = create_unstaged_content(&path_unstaged_content,
7537 &path_new_staged_content, blob_id, staged_blob_id,
7538 ie->path, repo, patch_cb, patch_arg);
7542 if (path_unstaged_content == NULL)
7545 if (path_new_staged_content) {
7546 err = got_object_blob_create(&new_staged_blob_id,
7547 path_new_staged_content, repo);
7552 f = fopen(path_unstaged_content, "r");
7554 err = got_error_from_errno2("fopen",
7555 path_unstaged_content);
7558 if (fstat(fileno(f), &sb) == -1) {
7559 err = got_error_from_errno2("fstat", path_unstaged_content);
7562 if (got_fileindex_entry_staged_filetype_get(ie) ==
7563 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
7564 char link_target[PATH_MAX];
7566 r = fread(link_target, 1, sizeof(link_target), f);
7567 if (r == 0 && ferror(f)) {
7568 err = got_error_from_errno("fread");
7571 if (r >= sizeof(link_target)) { /* should not happen */
7572 err = got_error(GOT_ERR_NO_SPACE);
7575 link_target[r] = '\0';
7576 err = merge_symlink(worktree, blob_base,
7577 ondisk_path, ie->path, label_orig, link_target,
7578 worktree->base_commit_id, repo, progress_cb,
7581 int local_changes_subsumed;
7582 err = merge_file(&local_changes_subsumed, worktree,
7583 blob_base, ondisk_path, ie->path,
7584 got_fileindex_perms_to_st(ie),
7585 path_unstaged_content, label_orig, "unstaged",
7586 repo, progress_cb, progress_arg);
7591 if (new_staged_blob_id) {
7592 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7593 SHA1_DIGEST_LENGTH);
7595 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7597 free(new_staged_blob_id);
7598 if (path_unstaged_content &&
7599 unlink(path_unstaged_content) == -1 && err == NULL)
7600 err = got_error_from_errno2("unlink", path_unstaged_content);
7601 if (path_new_staged_content &&
7602 unlink(path_new_staged_content) == -1 && err == NULL)
7603 err = got_error_from_errno2("unlink", path_new_staged_content);
7604 if (f && fclose(f) != 0 && err == NULL)
7605 err = got_error_from_errno2("fclose", path_unstaged_content);
7606 free(path_unstaged_content);
7607 free(path_new_staged_content);
7611 static const struct got_error *
7612 unstage_path(void *arg, unsigned char status,
7613 unsigned char staged_status, const char *relpath,
7614 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7615 struct got_object_id *commit_id, int dirfd, const char *de_name)
7617 const struct got_error *err = NULL;
7618 struct unstage_path_arg *a = arg;
7619 struct got_fileindex_entry *ie;
7620 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7621 char *ondisk_path = NULL;
7622 char *id_str = NULL, *label_orig = NULL;
7623 int local_changes_subsumed;
7626 if (staged_status != GOT_STATUS_ADD &&
7627 staged_status != GOT_STATUS_MODIFY &&
7628 staged_status != GOT_STATUS_DELETE)
7631 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7633 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7635 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7637 return got_error_from_errno("asprintf");
7639 err = got_object_id_str(&id_str,
7640 commit_id ? commit_id : a->worktree->base_commit_id);
7643 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7645 err = got_error_from_errno("asprintf");
7649 switch (staged_status) {
7650 case GOT_STATUS_MODIFY:
7651 err = got_object_open_as_blob(&blob_base, a->repo,
7656 case GOT_STATUS_ADD:
7658 if (staged_status == GOT_STATUS_ADD) {
7659 int choice = GOT_PATCH_CHOICE_NONE;
7660 err = (*a->patch_cb)(&choice, a->patch_arg,
7661 staged_status, ie->path, NULL, 1, 1);
7664 if (choice != GOT_PATCH_CHOICE_YES)
7667 err = unstage_hunks(staged_blob_id,
7668 blob_base, blob_id, ie, ondisk_path,
7669 label_orig, a->worktree, a->repo,
7670 a->patch_cb, a->patch_arg,
7671 a->progress_cb, a->progress_arg);
7672 break; /* Done with this file. */
7675 err = got_object_open_as_blob(&blob_staged, a->repo,
7676 staged_blob_id, 8192);
7679 switch (got_fileindex_entry_staged_filetype_get(ie)) {
7680 case GOT_FILEIDX_MODE_BAD_SYMLINK:
7681 case GOT_FILEIDX_MODE_REGULAR_FILE:
7682 err = merge_blob(&local_changes_subsumed, a->worktree,
7683 blob_base, ondisk_path, relpath,
7684 got_fileindex_perms_to_st(ie), label_orig,
7685 blob_staged, commit_id ? commit_id :
7686 a->worktree->base_commit_id, a->repo,
7687 a->progress_cb, a->progress_arg);
7689 case GOT_FILEIDX_MODE_SYMLINK:
7690 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7691 char *staged_target;
7692 err = got_object_blob_read_to_str(
7693 &staged_target, blob_staged);
7696 err = merge_symlink(a->worktree, blob_base,
7697 ondisk_path, relpath, label_orig,
7698 staged_target, commit_id ? commit_id :
7699 a->worktree->base_commit_id,
7700 a->repo, a->progress_cb, a->progress_arg);
7701 free(staged_target);
7703 err = merge_blob(&local_changes_subsumed,
7704 a->worktree, blob_base, ondisk_path,
7705 relpath, got_fileindex_perms_to_st(ie),
7706 label_orig, blob_staged,
7707 commit_id ? commit_id :
7708 a->worktree->base_commit_id, a->repo,
7709 a->progress_cb, a->progress_arg);
7713 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
7717 got_fileindex_entry_stage_set(ie,
7718 GOT_FILEIDX_STAGE_NONE);
7720 case GOT_STATUS_DELETE:
7722 int choice = GOT_PATCH_CHOICE_NONE;
7723 err = (*a->patch_cb)(&choice, a->patch_arg,
7724 staged_status, ie->path, NULL, 1, 1);
7727 if (choice == GOT_PATCH_CHOICE_NO)
7729 if (choice != GOT_PATCH_CHOICE_YES) {
7730 err = got_error(GOT_ERR_PATCH_CHOICE);
7734 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7735 err = get_file_status(&status, &sb, ie, ondisk_path,
7736 dirfd, de_name, a->repo);
7739 err = (*a->progress_cb)(a->progress_arg, status, relpath);
7745 got_object_blob_close(blob_base);
7747 got_object_blob_close(blob_staged);
7753 const struct got_error *
7754 got_worktree_unstage(struct got_worktree *worktree,
7755 struct got_pathlist_head *paths,
7756 got_worktree_checkout_cb progress_cb, void *progress_arg,
7757 got_worktree_patch_cb patch_cb, void *patch_arg,
7758 struct got_repository *repo)
7760 const struct got_error *err = NULL, *sync_err, *unlockerr;
7761 struct got_pathlist_entry *pe;
7762 struct got_fileindex *fileindex = NULL;
7763 char *fileindex_path = NULL;
7764 struct unstage_path_arg upa;
7766 err = lock_worktree(worktree, LOCK_EX);
7770 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7774 upa.worktree = worktree;
7775 upa.fileindex = fileindex;
7777 upa.progress_cb = progress_cb;
7778 upa.progress_arg = progress_arg;
7779 upa.patch_cb = patch_cb;
7780 upa.patch_arg = patch_arg;
7781 TAILQ_FOREACH(pe, paths, entry) {
7782 err = worktree_status(worktree, pe->path, fileindex, repo,
7783 unstage_path, &upa, NULL, NULL, 0, 0);
7788 sync_err = sync_fileindex(fileindex, fileindex_path);
7789 if (sync_err && err == NULL)
7792 free(fileindex_path);
7794 got_fileindex_free(fileindex);
7795 unlockerr = lock_worktree(worktree, LOCK_SH);
7796 if (unlockerr && err == NULL)
7801 struct report_file_info_arg {
7802 struct got_worktree *worktree;
7803 got_worktree_path_info_cb info_cb;
7805 struct got_pathlist_head *paths;
7806 got_cancel_cb cancel_cb;
7810 static const struct got_error *
7811 report_file_info(void *arg, struct got_fileindex_entry *ie)
7813 struct report_file_info_arg *a = arg;
7814 struct got_pathlist_entry *pe;
7815 struct got_object_id blob_id, staged_blob_id, commit_id;
7816 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
7817 struct got_object_id *commit_idp = NULL;
7820 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
7821 return got_error(GOT_ERR_CANCELLED);
7823 TAILQ_FOREACH(pe, a->paths, entry) {
7824 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
7825 got_path_is_child(ie->path, pe->path, pe->path_len))
7828 if (pe == NULL) /* not found */
7831 if (got_fileindex_entry_has_blob(ie)) {
7832 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
7833 blob_idp = &blob_id;
7835 stage = got_fileindex_entry_stage_get(ie);
7836 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
7837 stage == GOT_FILEIDX_STAGE_ADD) {
7838 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
7839 SHA1_DIGEST_LENGTH);
7840 staged_blob_idp = &staged_blob_id;
7843 if (got_fileindex_entry_has_commit(ie)) {
7844 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
7845 commit_idp = &commit_id;
7848 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
7849 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
7852 const struct got_error *
7853 got_worktree_path_info(struct got_worktree *worktree,
7854 struct got_pathlist_head *paths,
7855 got_worktree_path_info_cb info_cb, void *info_arg,
7856 got_cancel_cb cancel_cb, void *cancel_arg)
7859 const struct got_error *err = NULL, *unlockerr;
7860 struct got_fileindex *fileindex = NULL;
7861 char *fileindex_path = NULL;
7862 struct report_file_info_arg arg;
7864 err = lock_worktree(worktree, LOCK_SH);
7868 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7872 arg.worktree = worktree;
7873 arg.info_cb = info_cb;
7874 arg.info_arg = info_arg;
7876 arg.cancel_cb = cancel_cb;
7877 arg.cancel_arg = cancel_arg;
7878 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
7881 free(fileindex_path);
7883 got_fileindex_free(fileindex);
7884 unlockerr = lock_worktree(worktree, LOCK_UN);
7885 if (unlockerr && err == NULL)