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 err = open_worktree(worktree, path);
459 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
463 path = dirname(path);
465 return got_error_from_errno2("dirname", path);
466 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
468 return got_error(GOT_ERR_NOT_WORKTREE);
471 const struct got_error *
472 got_worktree_close(struct got_worktree *worktree)
474 const struct got_error *err = NULL;
475 free(worktree->repo_path);
476 free(worktree->path_prefix);
477 free(worktree->base_commit_id);
478 free(worktree->head_ref_name);
479 if (worktree->lockfd != -1)
480 if (close(worktree->lockfd) != 0)
481 err = got_error_from_errno2("close",
482 got_worktree_get_root_path(worktree));
483 free(worktree->root_path);
484 free(worktree->gotconfig_path);
485 got_gotconfig_free(worktree->gotconfig);
491 got_worktree_get_root_path(struct got_worktree *worktree)
493 return worktree->root_path;
497 got_worktree_get_repo_path(struct got_worktree *worktree)
499 return worktree->repo_path;
502 got_worktree_get_path_prefix(struct got_worktree *worktree)
504 return worktree->path_prefix;
507 const struct got_error *
508 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
509 const char *path_prefix)
511 char *absprefix = NULL;
513 if (!got_path_is_absolute(path_prefix)) {
514 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
515 return got_error_from_errno("asprintf");
517 *match = (strcmp(absprefix ? absprefix : path_prefix,
518 worktree->path_prefix) == 0);
524 got_worktree_get_head_ref_name(struct got_worktree *worktree)
526 return worktree->head_ref_name;
529 const struct got_error *
530 got_worktree_set_head_ref(struct got_worktree *worktree,
531 struct got_reference *head_ref)
533 const struct got_error *err = NULL;
534 char *path_got = NULL, *head_ref_name = NULL;
536 if (asprintf(&path_got, "%s/%s", worktree->root_path,
537 GOT_WORKTREE_GOT_DIR) == -1) {
538 err = got_error_from_errno("asprintf");
543 head_ref_name = strdup(got_ref_get_name(head_ref));
544 if (head_ref_name == NULL) {
545 err = got_error_from_errno("strdup");
549 err = write_head_ref(path_got, head_ref);
553 free(worktree->head_ref_name);
554 worktree->head_ref_name = head_ref_name;
562 struct got_object_id *
563 got_worktree_get_base_commit_id(struct got_worktree *worktree)
565 return worktree->base_commit_id;
568 const struct got_error *
569 got_worktree_set_base_commit_id(struct got_worktree *worktree,
570 struct got_repository *repo, struct got_object_id *commit_id)
572 const struct got_error *err;
573 struct got_object *obj = NULL;
575 char *path_got = NULL;
577 if (asprintf(&path_got, "%s/%s", worktree->root_path,
578 GOT_WORKTREE_GOT_DIR) == -1) {
579 err = got_error_from_errno("asprintf");
584 err = got_object_open(&obj, repo, commit_id);
588 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
589 err = got_error(GOT_ERR_OBJ_TYPE);
593 /* Record our base commit. */
594 err = got_object_id_str(&id_str, commit_id);
597 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
601 free(worktree->base_commit_id);
602 worktree->base_commit_id = got_object_id_dup(commit_id);
603 if (worktree->base_commit_id == NULL) {
604 err = got_error_from_errno("got_object_id_dup");
609 got_object_close(obj);
615 const struct got_gotconfig *
616 got_worktree_get_gotconfig(struct got_worktree *worktree)
618 return worktree->gotconfig;
621 static const struct got_error *
622 lock_worktree(struct got_worktree *worktree, int operation)
624 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
625 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
626 : got_error_from_errno2("flock",
627 got_worktree_get_root_path(worktree)));
631 static const struct got_error *
632 add_dir_on_disk(struct got_worktree *worktree, const char *path)
634 const struct got_error *err = NULL;
637 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
638 return got_error_from_errno("asprintf");
640 err = got_path_mkdir(abspath);
641 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
644 if (lstat(abspath, &sb) == -1) {
645 err = got_error_from_errno2("lstat", abspath);
646 } else if (!S_ISDIR(sb.st_mode)) {
647 /* TODO directory is obstructed; do something */
648 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
655 static const struct got_error *
656 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
658 const struct got_error *err = NULL;
661 size_t flen1 = 0, flen2 = 0;
666 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
667 if (flen1 == 0 && ferror(f1)) {
668 err = got_error_from_errno("fread");
671 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
672 if (flen2 == 0 && ferror(f2)) {
673 err = got_error_from_errno("fread");
680 } else if (flen2 == 0) {
684 } else if (flen1 == flen2) {
685 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
698 static const struct got_error *
699 check_files_equal(int *same, const char *f1_path, const char *f2_path)
701 const struct got_error *err = NULL;
704 FILE *f1 = NULL, *f2 = NULL;
708 if (lstat(f1_path, &sb) != 0) {
709 err = got_error_from_errno2("lstat", f1_path);
714 if (lstat(f2_path, &sb) != 0) {
715 err = got_error_from_errno2("lstat", f2_path);
720 if (size1 != size2) {
725 f1 = fopen(f1_path, "r");
727 return got_error_from_errno2("fopen", f1_path);
729 f2 = fopen(f2_path, "r");
731 err = got_error_from_errno2("fopen", f2_path);
735 err = check_file_contents_equal(same, f1, f2);
737 if (f1 && fclose(f1) != 0 && err == NULL)
738 err = got_error_from_errno("fclose");
739 if (f2 && fclose(f2) != 0 && err == NULL)
740 err = got_error_from_errno("fclose");
746 * Perform a 3-way merge where blob_orig acts as the common ancestor,
747 * the file at deriv_path acts as the first derived version, and the
748 * file on disk acts as the second derived version.
750 static const struct got_error *
751 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
752 struct got_blob_object *blob_orig, const char *ondisk_path,
753 const char *path, uint16_t st_mode, const char *deriv_path,
754 const char *label_orig, const char *label_deriv,
755 struct got_repository *repo,
756 got_worktree_checkout_cb progress_cb, void *progress_arg)
758 const struct got_error *err = NULL;
761 char *blob_orig_path = NULL;
762 char *merged_path = NULL, *base_path = NULL;
765 char *symlink_path = NULL;
766 FILE *symlinkf = NULL;
768 *local_changes_subsumed = 0;
770 parent = dirname(ondisk_path);
772 return got_error_from_errno2("dirname", ondisk_path);
774 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
775 return got_error_from_errno("asprintf");
777 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
782 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
783 err = got_error_from_errno("asprintf");
788 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
792 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
798 * If the file has no blob, this is an "add vs add" conflict,
799 * and we simply use an empty ancestor file to make both files
800 * appear in the merged result in their entirety.
805 * In order the run a 3-way merge with a symlink we copy the symlink's
806 * target path into a temporary file and use that file with diff3.
808 if (S_ISLNK(st_mode)) {
809 char target_path[PATH_MAX];
814 if (asprintf(&base_path, "%s/got-symlink-merge",
816 err = got_error_from_errno("asprintf");
820 err = got_opentemp_named(&symlink_path, &symlinkf, base_path);
823 target_len = readlink(ondisk_path, target_path,
824 sizeof(target_path));
825 if (target_len == -1) {
826 err = got_error_from_errno2("readlink", ondisk_path);
829 n = fwrite(target_path, 1, target_len, symlinkf);
830 if (n != target_len) {
831 err = got_ferror(symlinkf, GOT_ERR_IO);
834 if (fflush(symlinkf) == EOF) {
835 err = got_error_from_errno2("fflush", symlink_path);
840 err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
841 blob_orig_path, symlink_path ? symlink_path : ondisk_path,
842 label_deriv, label_orig, NULL);
846 err = (*progress_cb)(progress_arg,
847 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
851 if (fsync(merged_fd) != 0) {
852 err = got_error_from_errno("fsync");
856 /* Check if a clean merge has subsumed all local changes. */
857 if (overlapcnt == 0) {
858 err = check_files_equal(local_changes_subsumed, deriv_path,
864 if (fchmod(merged_fd, st_mode) != 0) {
865 err = got_error_from_errno2("fchmod", merged_path);
869 if (rename(merged_path, ondisk_path) != 0) {
870 err = got_error_from_errno3("rename", merged_path,
880 if (unlink(symlink_path) == -1 && err == NULL)
881 err = got_error_from_errno2("unlink", symlink_path);
883 if (symlinkf && fclose(symlinkf) == EOF && err == NULL)
884 err = got_error_from_errno2("fclose", symlink_path);
886 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
887 err = got_error_from_errno("close");
888 if (f_orig && fclose(f_orig) != 0 && err == NULL)
889 err = got_error_from_errno("fclose");
892 if (blob_orig_path) {
893 unlink(blob_orig_path);
894 free(blob_orig_path);
899 static const struct got_error *
900 update_symlink(const char *ondisk_path, const char *target_path,
903 /* This is not atomic but matches what 'ln -sf' does. */
904 if (unlink(ondisk_path) == -1)
905 return got_error_from_errno2("unlink", ondisk_path);
906 if (symlink(target_path, ondisk_path) == -1)
907 return got_error_from_errno3("symlink", target_path,
913 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
914 * in the work tree with a file that contains conflict markers and the
915 * conflicting target paths of the original version, a "derived version"
916 * of a symlink from an incoming change, and a local version of the symlink.
918 * The original versions's target path can be NULL if it is not available,
919 * such as if both derived versions added a new symlink at the same path.
921 * The incoming derived symlink target is NULL in case the incoming change
922 * has deleted this symlink.
924 static const struct got_error *
925 install_symlink_conflict(const char *deriv_target,
926 struct got_object_id *deriv_base_commit_id, const char *orig_target,
927 const char *label_orig, const char *local_target, const char *ondisk_path)
929 const struct got_error *err;
930 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
933 err = got_object_id_str(&id_str, deriv_base_commit_id);
935 return got_error_from_errno("asprintf");
937 if (asprintf(&label_deriv, "%s: commit %s",
938 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
939 err = got_error_from_errno("asprintf");
943 err = got_opentemp_named(&path, &f, "got-symlink-conflict");
947 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
948 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
949 deriv_target ? deriv_target : "(symlink was deleted)",
950 orig_target ? label_orig : "",
951 orig_target ? "\n" : "",
952 orig_target ? orig_target : "",
953 orig_target ? "\n" : "",
954 GOT_DIFF_CONFLICT_MARKER_SEP,
955 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
956 err = got_error_from_errno2("fprintf", path);
960 if (unlink(ondisk_path) == -1) {
961 err = got_error_from_errno2("unlink", ondisk_path);
964 if (rename(path, ondisk_path) == -1) {
965 err = got_error_from_errno3("rename", path, ondisk_path);
968 if (chmod(ondisk_path, GOT_DEFAULT_FILE_MODE) == -1) {
969 err = got_error_from_errno2("chmod", ondisk_path);
973 if (f != NULL && fclose(f) == EOF && err == NULL)
974 err = got_error_from_errno2("fclose", path);
981 /* forward declaration */
982 static const struct got_error *
983 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
984 const char *, const char *, uint16_t, const char *,
985 struct got_blob_object *, struct got_object_id *,
986 struct got_repository *, got_worktree_checkout_cb, void *);
989 * Merge a symlink into the work tree, where blob_orig acts as the common
990 * ancestor, deriv_target is the link target of the first derived version,
991 * and the symlink on disk acts as the second derived version.
992 * Assume that contents of both blobs represent symlinks.
994 static const struct got_error *
995 merge_symlink(struct got_worktree *worktree,
996 struct got_blob_object *blob_orig, const char *ondisk_path,
997 const char *path, const char *label_orig, const char *deriv_target,
998 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
999 got_worktree_checkout_cb progress_cb, void *progress_arg)
1001 const struct got_error *err = NULL;
1002 char *ancestor_target = NULL;
1004 ssize_t ondisk_len, deriv_len;
1005 char ondisk_target[PATH_MAX];
1006 int have_local_change = 0;
1007 int have_incoming_change = 0;
1009 if (lstat(ondisk_path, &sb) == -1)
1010 return got_error_from_errno2("lstat", ondisk_path);
1012 ondisk_len = readlink(ondisk_path, ondisk_target,
1013 sizeof(ondisk_target));
1014 if (ondisk_len == -1) {
1015 err = got_error_from_errno2("readlink",
1019 ondisk_target[ondisk_len] = '\0';
1022 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
1027 if (ancestor_target == NULL ||
1028 (ondisk_len != strlen(ancestor_target) ||
1029 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
1030 have_local_change = 1;
1032 deriv_len = strlen(deriv_target);
1033 if (ancestor_target == NULL ||
1034 (deriv_len != strlen(ancestor_target) ||
1035 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
1036 have_incoming_change = 1;
1038 if (!have_local_change && !have_incoming_change) {
1039 if (ancestor_target) {
1040 /* Both sides made the same change. */
1041 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1043 } else if (deriv_len == ondisk_len &&
1044 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1045 /* Both sides added the same symlink. */
1046 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1049 /* Both sides added symlinks which don't match. */
1050 err = install_symlink_conflict(deriv_target,
1051 deriv_base_commit_id, ancestor_target,
1052 label_orig, ondisk_target, ondisk_path);
1055 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1058 } else if (!have_local_change && have_incoming_change) {
1059 /* Apply the incoming change. */
1060 err = update_symlink(ondisk_path, deriv_target,
1061 strlen(deriv_target));
1064 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1065 } else if (have_local_change && have_incoming_change) {
1066 if (deriv_len == ondisk_len &&
1067 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
1068 /* Both sides made the same change. */
1069 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1072 err = install_symlink_conflict(deriv_target,
1073 deriv_base_commit_id, ancestor_target, label_orig,
1074 ondisk_target, ondisk_path);
1077 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1083 free(ancestor_target);
1088 * Perform a 3-way merge where blob_orig acts as the common ancestor,
1089 * blob_deriv acts as the first derived version, and the file on disk
1090 * acts as the second derived version.
1092 static const struct got_error *
1093 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1094 struct got_blob_object *blob_orig, const char *ondisk_path,
1095 const char *path, uint16_t st_mode, const char *label_orig,
1096 struct got_blob_object *blob_deriv,
1097 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1098 got_worktree_checkout_cb progress_cb, void *progress_arg)
1100 const struct got_error *err = NULL;
1101 FILE *f_deriv = NULL;
1102 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1103 char *label_deriv = NULL, *parent;
1105 *local_changes_subsumed = 0;
1107 parent = dirname(ondisk_path);
1109 return got_error_from_errno2("dirname", ondisk_path);
1112 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1113 err = got_error_from_errno("asprintf");
1118 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1121 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1126 err = got_object_id_str(&id_str, deriv_base_commit_id);
1129 if (asprintf(&label_deriv, "%s: commit %s",
1130 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1131 err = got_error_from_errno("asprintf");
1135 err = merge_file(local_changes_subsumed, worktree, blob_orig,
1136 ondisk_path, path, st_mode, blob_deriv_path, label_orig,
1137 label_deriv, repo, progress_cb, progress_arg);
1139 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
1140 err = got_error_from_errno("fclose");
1142 if (blob_deriv_path) {
1143 unlink(blob_deriv_path);
1144 free(blob_deriv_path);
1151 static const struct got_error *
1152 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1153 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1154 const char *ondisk_path, const char *path, struct got_object_id *blob_id)
1156 const struct got_error *err = NULL;
1157 struct got_fileindex_entry *new_ie;
1161 err = got_fileindex_entry_alloc(&new_ie, path);
1165 err = got_fileindex_entry_update(new_ie, ondisk_path,
1166 blob_id->sha1, base_commit_id->sha1, 1);
1170 err = got_fileindex_entry_add(fileindex, new_ie);
1173 got_fileindex_entry_free(new_ie);
1180 get_ondisk_perms(int executable, mode_t st_mode)
1182 mode_t xbits = S_IXUSR;
1185 /* Map read bits to execute bits. */
1186 if (st_mode & S_IRGRP)
1188 if (st_mode & S_IROTH)
1190 return st_mode | xbits;
1193 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1196 /* forward declaration */
1197 static const struct got_error *
1198 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1199 const char *path, mode_t te_mode, mode_t st_mode,
1200 struct got_blob_object *blob, int restoring_missing_file,
1201 int reverting_versioned_file, int installing_bad_symlink,
1202 int path_is_unversioned, struct got_repository *repo,
1203 got_worktree_checkout_cb progress_cb, void *progress_arg);
1206 * This function assumes that the provided symlink target points at a
1207 * safe location in the work tree!
1209 static const struct got_error *
1210 replace_existing_symlink(const char *ondisk_path, const char *target_path,
1213 const struct got_error *err = NULL;
1215 char etarget[PATH_MAX];
1219 * "Bad" symlinks (those pointing outside the work tree or into the
1220 * .got directory) are installed in the work tree as a regular file
1221 * which contains the bad symlink target path.
1222 * The new symlink target has already been checked for safety by our
1223 * caller. If we can successfully open a regular file then we simply
1224 * replace this file with a symlink below.
1226 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1229 return got_error_from_errno2("open", ondisk_path);
1231 /* We are updating an existing on-disk symlink. */
1232 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1234 return got_error_from_errno2("readlink", ondisk_path);
1236 if (elen == target_len &&
1237 memcmp(etarget, target_path, target_len) == 0)
1238 return NULL; /* nothing to do */
1241 err = update_symlink(ondisk_path, target_path, target_len);
1242 if (fd != -1 && close(fd) == -1 && err == NULL)
1243 err = got_error_from_errno2("close", ondisk_path);
1247 static const struct got_error *
1248 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1249 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1251 const struct got_error *err = NULL;
1252 char canonpath[PATH_MAX];
1253 char *path_got = NULL;
1255 *is_bad_symlink = 0;
1257 if (target_len >= sizeof(canonpath)) {
1258 *is_bad_symlink = 1;
1263 * We do not use realpath(3) to resolve the symlink's target
1264 * path because we don't want to resolve symlinks recursively.
1265 * Instead we make the path absolute and then canonicalize it.
1266 * Relative symlink target lookup should begin at the directory
1267 * in which the blob object is being installed.
1269 if (!got_path_is_absolute(target_path)) {
1271 char *parent = dirname(ondisk_path);
1273 return got_error_from_errno2("dirname", ondisk_path);
1274 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1)
1275 return got_error_from_errno("asprintf");
1276 if (strlen(abspath) >= sizeof(canonpath)) {
1277 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1281 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1286 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1291 /* Only allow symlinks pointing at paths within the work tree. */
1292 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1293 *is_bad_symlink = 1;
1297 /* Do not allow symlinks pointing into the .got directory. */
1298 if (asprintf(&path_got, "%s/%s", wtroot_path,
1299 GOT_WORKTREE_GOT_DIR) == -1)
1300 return got_error_from_errno("asprintf");
1301 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1302 *is_bad_symlink = 1;
1308 static const struct got_error *
1309 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1310 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1311 int restoring_missing_file, int reverting_versioned_file,
1312 int path_is_unversioned, struct got_repository *repo,
1313 got_worktree_checkout_cb progress_cb, void *progress_arg)
1315 const struct got_error *err = NULL;
1316 char target_path[PATH_MAX];
1317 size_t len, target_len = 0;
1318 char *path_got = NULL;
1319 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1320 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1322 *is_bad_symlink = 0;
1325 * Blob object content specifies the target path of the link.
1326 * If a symbolic link cannot be installed we instead create
1327 * a regular file which contains the link target path stored
1328 * in the blob object.
1331 err = got_object_blob_read_block(&len, blob);
1332 if (len + target_len >= sizeof(target_path)) {
1333 /* Path too long; install as a regular file. */
1334 *is_bad_symlink = 1;
1335 got_object_blob_rewind(blob);
1336 return install_blob(worktree, ondisk_path, path,
1337 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1338 restoring_missing_file, reverting_versioned_file,
1339 1, path_is_unversioned, repo, progress_cb,
1343 /* Skip blob object header first time around. */
1344 memcpy(target_path + target_len, buf + hdrlen,
1346 target_len += len - hdrlen;
1350 target_path[target_len] = '\0';
1352 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1353 ondisk_path, worktree->root_path);
1357 if (*is_bad_symlink) {
1358 /* install as a regular file */
1359 *is_bad_symlink = 1;
1360 got_object_blob_rewind(blob);
1361 err = install_blob(worktree, ondisk_path, path,
1362 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1363 restoring_missing_file, reverting_versioned_file, 1,
1364 path_is_unversioned, repo, progress_cb, progress_arg);
1368 if (symlink(target_path, ondisk_path) == -1) {
1369 if (errno == EEXIST) {
1370 if (path_is_unversioned) {
1371 err = (*progress_cb)(progress_arg,
1372 GOT_STATUS_UNVERSIONED, path);
1375 err = replace_existing_symlink(ondisk_path,
1376 target_path, target_len);
1380 err = (*progress_cb)(progress_arg,
1381 reverting_versioned_file ?
1382 GOT_STATUS_REVERT : GOT_STATUS_UPDATE,
1385 goto done; /* Nothing else to do. */
1388 if (errno == ENOENT) {
1389 char *parent = dirname(ondisk_path);
1390 if (parent == NULL) {
1391 err = got_error_from_errno2("dirname",
1395 err = add_dir_on_disk(worktree, parent);
1399 * Retry, and fall through to error handling
1400 * below if this second attempt fails.
1402 if (symlink(target_path, ondisk_path) != -1) {
1403 err = NULL; /* success */
1408 /* Handle errors from first or second creation attempt. */
1409 if (errno == ENAMETOOLONG) {
1410 /* bad target path; install as a regular file */
1411 *is_bad_symlink = 1;
1412 got_object_blob_rewind(blob);
1413 err = install_blob(worktree, ondisk_path, path,
1414 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1415 restoring_missing_file, reverting_versioned_file, 1,
1416 path_is_unversioned, repo,
1417 progress_cb, progress_arg);
1418 } else if (errno == ENOTDIR) {
1419 err = got_error_path(ondisk_path,
1420 GOT_ERR_FILE_OBSTRUCTED);
1422 err = got_error_from_errno3("symlink",
1423 target_path, ondisk_path);
1425 } else if (progress_cb)
1426 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1427 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1433 static const struct got_error *
1434 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1435 const char *path, mode_t te_mode, mode_t st_mode,
1436 struct got_blob_object *blob, int restoring_missing_file,
1437 int reverting_versioned_file, int installing_bad_symlink,
1438 int path_is_unversioned, struct got_repository *repo,
1439 got_worktree_checkout_cb progress_cb, void *progress_arg)
1441 const struct got_error *err = NULL;
1445 char *tmppath = NULL;
1447 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1448 GOT_DEFAULT_FILE_MODE);
1450 if (errno == ENOENT) {
1451 char *parent = dirname(path);
1453 return got_error_from_errno2("dirname", path);
1454 err = add_dir_on_disk(worktree, parent);
1457 fd = open(ondisk_path,
1458 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1459 GOT_DEFAULT_FILE_MODE);
1461 return got_error_from_errno2("open",
1463 } else if (errno == EEXIST) {
1464 if (path_is_unversioned) {
1465 err = (*progress_cb)(progress_arg,
1466 GOT_STATUS_UNVERSIONED, path);
1469 if (!S_ISREG(st_mode) && !installing_bad_symlink) {
1470 /* TODO file is obstructed; do something */
1471 err = got_error_path(ondisk_path,
1472 GOT_ERR_FILE_OBSTRUCTED);
1475 err = got_opentemp_named_fd(&tmppath, &fd,
1482 return got_error_from_errno2("open", ondisk_path);
1486 if (restoring_missing_file)
1487 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1489 else if (reverting_versioned_file)
1490 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1493 err = (*progress_cb)(progress_arg,
1494 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1499 hdrlen = got_object_blob_get_hdrlen(blob);
1501 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1502 err = got_object_blob_read_block(&len, blob);
1506 /* Skip blob object header first time around. */
1507 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1509 err = got_error_from_errno("write");
1511 } else if (outlen != len - hdrlen) {
1512 err = got_error(GOT_ERR_IO);
1519 if (fsync(fd) != 0) {
1520 err = got_error_from_errno("fsync");
1525 if (rename(tmppath, ondisk_path) != 0) {
1526 err = got_error_from_errno3("rename", tmppath,
1533 if (chmod(ondisk_path,
1534 get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1535 err = got_error_from_errno2("chmod", ondisk_path);
1540 if (fd != -1 && close(fd) != 0 && err == NULL)
1541 err = got_error_from_errno("close");
1546 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1547 static const struct got_error *
1548 get_modified_file_content_status(unsigned char *status, FILE *f)
1550 const struct got_error *err = NULL;
1551 const char *markers[3] = {
1552 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1553 GOT_DIFF_CONFLICT_MARKER_SEP,
1554 GOT_DIFF_CONFLICT_MARKER_END
1559 const char delim[3] = {'\0', '\0', '\0'};
1561 while (*status == GOT_STATUS_MODIFY) {
1562 line = fparseln(f, &len, NULL, delim, 0);
1566 err = got_ferror(f, GOT_ERR_IO);
1570 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1571 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1573 *status = GOT_STATUS_CONFLICT;
1583 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1585 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1586 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1590 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1592 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1593 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1594 ie->mtime_sec == sb->st_mtim.tv_sec &&
1595 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1596 ie->size == (sb->st_size & 0xffffffff) &&
1597 !xbit_differs(ie, sb->st_mode));
1600 static unsigned char
1601 get_staged_status(struct got_fileindex_entry *ie)
1603 switch (got_fileindex_entry_stage_get(ie)) {
1604 case GOT_FILEIDX_STAGE_ADD:
1605 return GOT_STATUS_ADD;
1606 case GOT_FILEIDX_STAGE_DELETE:
1607 return GOT_STATUS_DELETE;
1608 case GOT_FILEIDX_STAGE_MODIFY:
1609 return GOT_STATUS_MODIFY;
1611 return GOT_STATUS_NO_CHANGE;
1615 static const struct got_error *
1616 get_symlink_modification_status(unsigned char *status,
1617 struct got_fileindex_entry *ie, const char *abspath,
1618 int dirfd, const char *de_name, struct got_blob_object *blob)
1620 const struct got_error *err = NULL;
1621 char target_path[PATH_MAX];
1622 char etarget[PATH_MAX];
1624 size_t len, target_len = 0;
1625 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1626 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1628 *status = GOT_STATUS_NO_CHANGE;
1630 /* Blob object content specifies the target path of the link. */
1632 err = got_object_blob_read_block(&len, blob);
1635 if (len + target_len >= sizeof(target_path)) {
1637 * Should not happen. The blob contents were OK
1638 * when this symlink was installed.
1640 return got_error(GOT_ERR_NO_SPACE);
1643 /* Skip blob object header first time around. */
1644 memcpy(target_path + target_len, buf + hdrlen,
1646 target_len += len - hdrlen;
1650 target_path[target_len] = '\0';
1653 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1655 return got_error_from_errno2("readlinkat", abspath);
1657 elen = readlink(abspath, etarget, sizeof(etarget));
1659 return got_error_from_errno2("readlink", abspath);
1662 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1663 *status = GOT_STATUS_MODIFY;
1668 static const struct got_error *
1669 get_file_status(unsigned char *status, struct stat *sb,
1670 struct got_fileindex_entry *ie, const char *abspath,
1671 int dirfd, const char *de_name, struct got_repository *repo)
1673 const struct got_error *err = NULL;
1674 struct got_object_id id;
1679 struct got_blob_object *blob = NULL;
1681 unsigned char staged_status = get_staged_status(ie);
1683 *status = GOT_STATUS_NO_CHANGE;
1686 * Whenever the caller provides a directory descriptor and a
1687 * directory entry name for the file, use them! This prevents
1688 * race conditions if filesystem paths change beneath our feet.
1691 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1692 if (errno == ENOENT) {
1693 if (got_fileindex_entry_has_file_on_disk(ie))
1694 *status = GOT_STATUS_MISSING;
1696 *status = GOT_STATUS_DELETE;
1699 err = got_error_from_errno2("fstatat", abspath);
1703 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1704 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1705 return got_error_from_errno2("open", abspath);
1706 else if (fd == -1 && errno == ELOOP) {
1707 if (lstat(abspath, sb) == -1)
1708 return got_error_from_errno2("lstat", abspath);
1709 } else if (fd == -1 || fstat(fd, sb) == -1) {
1710 if (errno == ENOENT) {
1711 if (got_fileindex_entry_has_file_on_disk(ie))
1712 *status = GOT_STATUS_MISSING;
1714 *status = GOT_STATUS_DELETE;
1717 err = got_error_from_errno2("fstat", abspath);
1722 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1723 *status = GOT_STATUS_OBSTRUCTED;
1727 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1728 *status = GOT_STATUS_DELETE;
1730 } else if (!got_fileindex_entry_has_blob(ie) &&
1731 staged_status != GOT_STATUS_ADD) {
1732 *status = GOT_STATUS_ADD;
1736 if (!stat_info_differs(ie, sb))
1739 if (S_ISLNK(sb->st_mode) &&
1740 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1741 *status = GOT_STATUS_MODIFY;
1745 if (staged_status == GOT_STATUS_MODIFY ||
1746 staged_status == GOT_STATUS_ADD)
1747 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1749 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1751 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1755 if (S_ISLNK(sb->st_mode)) {
1756 err = get_symlink_modification_status(status, ie,
1757 abspath, dirfd, de_name, blob);
1762 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1764 err = got_error_from_errno2("openat", abspath);
1769 f = fdopen(fd, "r");
1771 err = got_error_from_errno2("fdopen", abspath);
1775 hdrlen = got_object_blob_get_hdrlen(blob);
1777 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1778 err = got_object_blob_read_block(&blen, blob);
1781 /* Skip length of blob object header first time around. */
1782 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1783 if (flen == 0 && ferror(f)) {
1784 err = got_error_from_errno("fread");
1787 if (blen - hdrlen == 0) {
1789 *status = GOT_STATUS_MODIFY;
1791 } else if (flen == 0) {
1792 if (blen - hdrlen != 0)
1793 *status = GOT_STATUS_MODIFY;
1795 } else if (blen - hdrlen == flen) {
1796 /* Skip blob object header first time around. */
1797 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1798 *status = GOT_STATUS_MODIFY;
1802 *status = GOT_STATUS_MODIFY;
1808 if (*status == GOT_STATUS_MODIFY) {
1810 err = get_modified_file_content_status(status, f);
1811 } else if (xbit_differs(ie, sb->st_mode))
1812 *status = GOT_STATUS_MODE_CHANGE;
1815 got_object_blob_close(blob);
1816 if (f != NULL && fclose(f) == EOF && err == NULL)
1817 err = got_error_from_errno2("fclose", abspath);
1818 if (fd != -1 && close(fd) == -1 && err == NULL)
1819 err = got_error_from_errno2("close", abspath);
1824 * Update timestamps in the file index if a file is unmodified and
1825 * we had to run a full content comparison to find out.
1827 static const struct got_error *
1828 sync_timestamps(char *ondisk_path, unsigned char status,
1829 struct got_fileindex_entry *ie, struct stat *sb)
1831 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1832 return got_fileindex_entry_update(ie, ondisk_path,
1833 ie->blob_sha1, ie->commit_sha1, 1);
1838 static const struct got_error *
1839 update_blob(struct got_worktree *worktree,
1840 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1841 struct got_tree_entry *te, const char *path,
1842 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1845 const struct got_error *err = NULL;
1846 struct got_blob_object *blob = NULL;
1848 unsigned char status = GOT_STATUS_NO_CHANGE;
1851 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1852 return got_error_from_errno("asprintf");
1855 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1856 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1859 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1863 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1864 sb.st_mode = got_fileindex_perms_to_st(ie);
1866 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1867 status = GOT_STATUS_UNVERSIONED;
1870 if (status == GOT_STATUS_OBSTRUCTED) {
1871 err = (*progress_cb)(progress_arg, status, path);
1874 if (status == GOT_STATUS_CONFLICT) {
1875 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1880 if (ie && status != GOT_STATUS_MISSING &&
1881 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1882 if (got_fileindex_entry_has_commit(ie) &&
1883 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1884 SHA1_DIGEST_LENGTH) == 0) {
1885 err = sync_timestamps(ondisk_path, status, ie, &sb);
1888 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1892 if (got_fileindex_entry_has_blob(ie) &&
1893 memcmp(ie->blob_sha1, te->id.sha1,
1894 SHA1_DIGEST_LENGTH) == 0) {
1895 err = sync_timestamps(ondisk_path, status, ie, &sb);
1900 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1904 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1905 int update_timestamps;
1906 struct got_blob_object *blob2 = NULL;
1907 char *label_orig = NULL;
1908 if (got_fileindex_entry_has_blob(ie)) {
1909 struct got_object_id id2;
1910 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1911 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1915 if (got_fileindex_entry_has_commit(ie)) {
1916 char id_str[SHA1_DIGEST_STRING_LENGTH];
1917 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1918 sizeof(id_str)) == NULL) {
1919 err = got_error_path(id_str,
1920 GOT_ERR_BAD_OBJ_ID_STR);
1923 if (asprintf(&label_orig, "%s: commit %s",
1924 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1925 err = got_error_from_errno("asprintf");
1929 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1931 err = got_object_blob_read_to_str(&link_target, blob);
1934 err = merge_symlink(worktree, blob2, ondisk_path, path,
1935 label_orig, link_target, worktree->base_commit_id,
1936 repo, progress_cb, progress_arg);
1939 err = merge_blob(&update_timestamps, worktree, blob2,
1940 ondisk_path, path, sb.st_mode, label_orig, blob,
1941 worktree->base_commit_id, repo,
1942 progress_cb, progress_arg);
1946 got_object_blob_close(blob2);
1950 * Do not update timestamps of files with local changes.
1951 * Otherwise, a future status walk would treat them as
1952 * unmodified files again.
1954 err = got_fileindex_entry_update(ie, ondisk_path,
1955 blob->id.sha1, worktree->base_commit_id->sha1,
1957 } else if (status == GOT_STATUS_MODE_CHANGE) {
1958 err = got_fileindex_entry_update(ie, ondisk_path,
1959 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1960 } else if (status == GOT_STATUS_DELETE) {
1961 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1964 err = got_fileindex_entry_update(ie, ondisk_path,
1965 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1969 int is_bad_symlink = 0;
1970 if (S_ISLNK(te->mode)) {
1971 err = install_symlink(&is_bad_symlink, worktree,
1972 ondisk_path, path, blob,
1973 status == GOT_STATUS_MISSING, 0,
1974 status == GOT_STATUS_UNVERSIONED, repo,
1975 progress_cb, progress_arg);
1977 err = install_blob(worktree, ondisk_path, path,
1978 te->mode, sb.st_mode, blob,
1979 status == GOT_STATUS_MISSING, 0, 0,
1980 status == GOT_STATUS_UNVERSIONED, repo,
1981 progress_cb, progress_arg);
1987 err = got_fileindex_entry_update(ie, ondisk_path,
1988 blob->id.sha1, worktree->base_commit_id->sha1, 1);
1990 err = create_fileindex_entry(&ie, fileindex,
1991 worktree->base_commit_id, ondisk_path, path,
1997 if (is_bad_symlink) {
1998 got_fileindex_entry_filetype_set(ie,
1999 GOT_FILEIDX_MODE_BAD_SYMLINK);
2002 got_object_blob_close(blob);
2008 static const struct got_error *
2009 remove_ondisk_file(const char *root_path, const char *path)
2011 const struct got_error *err = NULL;
2012 char *ondisk_path = NULL;
2014 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2015 return got_error_from_errno("asprintf");
2017 if (unlink(ondisk_path) == -1) {
2018 if (errno != ENOENT)
2019 err = got_error_from_errno2("unlink", ondisk_path);
2021 char *parent = dirname(ondisk_path);
2022 while (parent && strcmp(parent, root_path) != 0) {
2023 if (rmdir(parent) == -1) {
2024 if (errno != ENOTEMPTY)
2025 err = got_error_from_errno2("rmdir",
2029 parent = dirname(parent);
2036 static const struct got_error *
2037 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2038 struct got_fileindex_entry *ie, struct got_repository *repo,
2039 got_worktree_checkout_cb progress_cb, void *progress_arg)
2041 const struct got_error *err = NULL;
2042 unsigned char status;
2046 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2047 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2049 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2051 return got_error_from_errno("asprintf");
2053 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2057 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2058 char ondisk_target[PATH_MAX];
2059 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2060 sizeof(ondisk_target));
2061 if (ondisk_len == -1) {
2062 err = got_error_from_errno2("readlink", ondisk_path);
2065 ondisk_target[ondisk_len] = '\0';
2066 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2067 NULL, NULL, /* XXX pass common ancestor info? */
2068 ondisk_target, ondisk_path);
2071 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2076 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2077 status == GOT_STATUS_ADD) {
2078 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2082 * Preserve the working file and change the deleted blob's
2083 * entry into a schedule-add entry.
2085 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
2088 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2091 if (status == GOT_STATUS_NO_CHANGE) {
2092 err = remove_ondisk_file(worktree->root_path, ie->path);
2096 got_fileindex_entry_remove(fileindex, ie);
2103 struct diff_cb_arg {
2104 struct got_fileindex *fileindex;
2105 struct got_worktree *worktree;
2106 struct got_repository *repo;
2107 got_worktree_checkout_cb progress_cb;
2109 got_cancel_cb cancel_cb;
2113 static const struct got_error *
2114 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2115 struct got_tree_entry *te, const char *parent_path)
2117 struct diff_cb_arg *a = arg;
2119 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2120 return got_error(GOT_ERR_CANCELLED);
2122 return update_blob(a->worktree, a->fileindex, ie, te,
2123 ie->path, a->repo, a->progress_cb, a->progress_arg);
2126 static const struct got_error *
2127 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2129 struct diff_cb_arg *a = arg;
2131 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2132 return got_error(GOT_ERR_CANCELLED);
2134 return delete_blob(a->worktree, a->fileindex, ie,
2135 a->repo, a->progress_cb, a->progress_arg);
2138 static const struct got_error *
2139 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2141 struct diff_cb_arg *a = arg;
2142 const struct got_error *err;
2145 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2146 return got_error(GOT_ERR_CANCELLED);
2148 if (got_object_tree_entry_is_submodule(te))
2151 if (asprintf(&path, "%s%s%s", parent_path,
2152 parent_path[0] ? "/" : "", te->name)
2154 return got_error_from_errno("asprintf");
2156 if (S_ISDIR(te->mode))
2157 err = add_dir_on_disk(a->worktree, path);
2159 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2160 a->repo, a->progress_cb, a->progress_arg);
2166 const struct got_error *
2167 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2169 uint32_t uuid_status;
2171 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2172 if (uuid_status != uuid_s_ok) {
2174 return got_error_uuid(uuid_status, "uuid_to_string");
2180 static const struct got_error *
2181 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2183 const struct got_error *err = NULL;
2184 char *uuidstr = NULL;
2188 err = got_worktree_get_uuid(&uuidstr, worktree);
2192 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2193 err = got_error_from_errno("asprintf");
2200 const struct got_error *
2201 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2203 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2206 static const struct got_error *
2207 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2209 return get_ref_name(refname, worktree,
2210 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2213 static const struct got_error *
2214 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2216 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2219 static const struct got_error *
2220 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2222 return get_ref_name(refname, worktree,
2223 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2226 static const struct got_error *
2227 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2229 return get_ref_name(refname, worktree,
2230 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2233 static const struct got_error *
2234 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2236 return get_ref_name(refname, worktree,
2237 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2240 static const struct got_error *
2241 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2243 return get_ref_name(refname, worktree,
2244 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2247 static const struct got_error *
2248 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2250 return get_ref_name(refname, worktree,
2251 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2254 static const struct got_error *
2255 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2257 return get_ref_name(refname, worktree,
2258 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2261 const struct got_error *
2262 got_worktree_get_histedit_script_path(char **path,
2263 struct got_worktree *worktree)
2265 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2266 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2268 return got_error_from_errno("asprintf");
2274 * Prevent Git's garbage collector from deleting our base commit by
2275 * setting a reference to our base commit's ID.
2277 static const struct got_error *
2278 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2280 const struct got_error *err = NULL;
2281 struct got_reference *ref = NULL;
2284 err = got_worktree_get_base_ref_name(&refname, worktree);
2288 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2292 err = got_ref_write(ref, repo);
2300 static const struct got_error *
2301 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2303 const struct got_error *err = NULL;
2305 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2306 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2307 err = got_error_from_errno("asprintf");
2308 *fileindex_path = NULL;
2314 static const struct got_error *
2315 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2316 struct got_worktree *worktree)
2318 const struct got_error *err = NULL;
2321 *fileindex_path = NULL;
2322 *fileindex = got_fileindex_alloc();
2323 if (*fileindex == NULL)
2324 return got_error_from_errno("got_fileindex_alloc");
2326 err = get_fileindex_path(fileindex_path, worktree);
2330 index = fopen(*fileindex_path, "rb");
2331 if (index == NULL) {
2332 if (errno != ENOENT)
2333 err = got_error_from_errno2("fopen", *fileindex_path);
2335 err = got_fileindex_read(*fileindex, index);
2336 if (fclose(index) != 0 && err == NULL)
2337 err = got_error_from_errno("fclose");
2341 free(*fileindex_path);
2342 *fileindex_path = NULL;
2343 got_fileindex_free(*fileindex);
2349 struct bump_base_commit_id_arg {
2350 struct got_object_id *base_commit_id;
2353 const char *entry_name;
2354 got_worktree_checkout_cb progress_cb;
2358 /* Bump base commit ID of all files within an updated part of the work tree. */
2359 static const struct got_error *
2360 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2362 const struct got_error *err;
2363 struct bump_base_commit_id_arg *a = arg;
2365 if (a->entry_name) {
2366 if (strcmp(ie->path, a->path) != 0)
2368 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2371 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2372 SHA1_DIGEST_LENGTH) == 0)
2375 if (a->progress_cb) {
2376 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2381 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2385 static const struct got_error *
2386 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2388 const struct got_error *err = NULL;
2389 char *new_fileindex_path = NULL;
2390 FILE *new_index = NULL;
2391 struct timespec timeout;
2393 err = got_opentemp_named(&new_fileindex_path, &new_index,
2398 err = got_fileindex_write(fileindex, new_index);
2402 if (rename(new_fileindex_path, fileindex_path) != 0) {
2403 err = got_error_from_errno3("rename", new_fileindex_path,
2405 unlink(new_fileindex_path);
2409 * Sleep for a short amount of time to ensure that files modified after
2410 * this program exits have a different time stamp from the one which
2411 * was recorded in the file index.
2414 timeout.tv_nsec = 1;
2415 nanosleep(&timeout, NULL);
2419 free(new_fileindex_path);
2423 static const struct got_error *
2424 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2425 struct got_object_id **tree_id, const char *wt_relpath,
2426 struct got_worktree *worktree, struct got_repository *repo)
2428 const struct got_error *err = NULL;
2429 struct got_object_id *id = NULL;
2430 char *in_repo_path = NULL;
2431 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2433 *entry_type = GOT_OBJ_TYPE_ANY;
2434 *tree_relpath = NULL;
2437 if (wt_relpath[0] == '\0') {
2438 /* Check out all files within the work tree. */
2439 *entry_type = GOT_OBJ_TYPE_TREE;
2440 *tree_relpath = strdup("");
2441 if (*tree_relpath == NULL) {
2442 err = got_error_from_errno("strdup");
2445 err = got_object_id_by_path(tree_id, repo,
2446 worktree->base_commit_id, worktree->path_prefix);
2452 /* Check out a subset of files in the work tree. */
2454 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2455 is_root_wt ? "" : "/", wt_relpath) == -1) {
2456 err = got_error_from_errno("asprintf");
2460 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2466 in_repo_path = NULL;
2468 err = got_object_get_type(entry_type, repo, id);
2472 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2473 /* Check out a single file. */
2474 if (strchr(wt_relpath, '/') == NULL) {
2475 /* Check out a single file in work tree's root dir. */
2476 in_repo_path = strdup(worktree->path_prefix);
2477 if (in_repo_path == NULL) {
2478 err = got_error_from_errno("strdup");
2481 *tree_relpath = strdup("");
2482 if (*tree_relpath == NULL) {
2483 err = got_error_from_errno("strdup");
2487 /* Check out a single file in a subdirectory. */
2488 err = got_path_dirname(tree_relpath, wt_relpath);
2491 if (asprintf(&in_repo_path, "%s%s%s",
2492 worktree->path_prefix, is_root_wt ? "" : "/",
2493 *tree_relpath) == -1) {
2494 err = got_error_from_errno("asprintf");
2498 err = got_object_id_by_path(tree_id, repo,
2499 worktree->base_commit_id, in_repo_path);
2501 /* Check out all files within a subdirectory. */
2502 *tree_id = got_object_id_dup(id);
2503 if (*tree_id == NULL) {
2504 err = got_error_from_errno("got_object_id_dup");
2507 *tree_relpath = strdup(wt_relpath);
2508 if (*tree_relpath == NULL) {
2509 err = got_error_from_errno("strdup");
2517 *entry_type = GOT_OBJ_TYPE_ANY;
2518 free(*tree_relpath);
2519 *tree_relpath = NULL;
2526 static const struct got_error *
2527 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2528 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2529 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2530 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2532 const struct got_error *err = NULL;
2533 struct got_commit_object *commit = NULL;
2534 struct got_tree_object *tree = NULL;
2535 struct got_fileindex_diff_tree_cb diff_cb;
2536 struct diff_cb_arg arg;
2538 err = ref_base_commit(worktree, repo);
2540 if (!(err->code == GOT_ERR_ERRNO &&
2541 (errno == EACCES || errno == EROFS)))
2543 err = (*progress_cb)(progress_arg,
2544 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2549 err = got_object_open_as_commit(&commit, repo,
2550 worktree->base_commit_id);
2554 err = got_object_open_as_tree(&tree, repo, tree_id);
2559 got_object_tree_find_entry(tree, entry_name) == NULL) {
2560 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2564 diff_cb.diff_old_new = diff_old_new;
2565 diff_cb.diff_old = diff_old;
2566 diff_cb.diff_new = diff_new;
2567 arg.fileindex = fileindex;
2568 arg.worktree = worktree;
2570 arg.progress_cb = progress_cb;
2571 arg.progress_arg = progress_arg;
2572 arg.cancel_cb = cancel_cb;
2573 arg.cancel_arg = cancel_arg;
2574 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2575 entry_name, repo, &diff_cb, &arg);
2578 got_object_tree_close(tree);
2580 got_object_commit_close(commit);
2584 const struct got_error *
2585 got_worktree_checkout_files(struct got_worktree *worktree,
2586 struct got_pathlist_head *paths, struct got_repository *repo,
2587 got_worktree_checkout_cb progress_cb, void *progress_arg,
2588 got_cancel_cb cancel_cb, void *cancel_arg)
2590 const struct got_error *err = NULL, *sync_err, *unlockerr;
2591 struct got_commit_object *commit = NULL;
2592 struct got_tree_object *tree = NULL;
2593 struct got_fileindex *fileindex = NULL;
2594 char *fileindex_path = NULL;
2595 struct got_pathlist_entry *pe;
2596 struct tree_path_data {
2597 SIMPLEQ_ENTRY(tree_path_data) entry;
2598 struct got_object_id *tree_id;
2603 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2605 SIMPLEQ_INIT(&tree_paths);
2607 err = lock_worktree(worktree, LOCK_EX);
2611 /* Map all specified paths to in-repository trees. */
2612 TAILQ_FOREACH(pe, paths, entry) {
2613 tpd = malloc(sizeof(*tpd));
2615 err = got_error_from_errno("malloc");
2619 err = find_tree_entry_for_checkout(&tpd->entry_type,
2620 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2626 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2627 err = got_path_basename(&tpd->entry_name, pe->path);
2635 tpd->entry_name = NULL;
2637 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2641 * Read the file index.
2642 * Checking out files is supposed to be an idempotent operation.
2643 * If the on-disk file index is incomplete we will try to complete it.
2645 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2649 tpd = SIMPLEQ_FIRST(&tree_paths);
2650 TAILQ_FOREACH(pe, paths, entry) {
2651 struct bump_base_commit_id_arg bbc_arg;
2653 err = checkout_files(worktree, fileindex, tpd->relpath,
2654 tpd->tree_id, tpd->entry_name, repo,
2655 progress_cb, progress_arg, cancel_cb, cancel_arg);
2659 bbc_arg.base_commit_id = worktree->base_commit_id;
2660 bbc_arg.entry_name = tpd->entry_name;
2661 bbc_arg.path = pe->path;
2662 bbc_arg.path_len = pe->path_len;
2663 bbc_arg.progress_cb = progress_cb;
2664 bbc_arg.progress_arg = progress_arg;
2665 err = got_fileindex_for_each_entry_safe(fileindex,
2666 bump_base_commit_id, &bbc_arg);
2670 tpd = SIMPLEQ_NEXT(tpd, entry);
2672 sync_err = sync_fileindex(fileindex, fileindex_path);
2673 if (sync_err && err == NULL)
2676 free(fileindex_path);
2678 got_object_tree_close(tree);
2680 got_object_commit_close(commit);
2682 got_fileindex_free(fileindex);
2683 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2684 tpd = SIMPLEQ_FIRST(&tree_paths);
2685 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2690 unlockerr = lock_worktree(worktree, LOCK_SH);
2691 if (unlockerr && err == NULL)
2696 struct merge_file_cb_arg {
2697 struct got_worktree *worktree;
2698 struct got_fileindex *fileindex;
2699 got_worktree_checkout_cb progress_cb;
2701 got_cancel_cb cancel_cb;
2703 const char *label_orig;
2704 struct got_object_id *commit_id2;
2707 static const struct got_error *
2708 merge_file_cb(void *arg, struct got_blob_object *blob1,
2709 struct got_blob_object *blob2, struct got_object_id *id1,
2710 struct got_object_id *id2, const char *path1, const char *path2,
2711 mode_t mode1, mode_t mode2, struct got_repository *repo)
2713 static const struct got_error *err = NULL;
2714 struct merge_file_cb_arg *a = arg;
2715 struct got_fileindex_entry *ie;
2716 char *ondisk_path = NULL;
2718 unsigned char status;
2719 int local_changes_subsumed;
2721 if (blob1 && blob2) {
2722 ie = got_fileindex_entry_get(a->fileindex, path2,
2725 return (*a->progress_cb)(a->progress_arg,
2726 GOT_STATUS_MISSING, path2);
2728 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2730 return got_error_from_errno("asprintf");
2732 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2737 if (status == GOT_STATUS_DELETE) {
2738 err = (*a->progress_cb)(a->progress_arg,
2739 GOT_STATUS_MERGE, path2);
2742 if (status != GOT_STATUS_NO_CHANGE &&
2743 status != GOT_STATUS_MODIFY &&
2744 status != GOT_STATUS_CONFLICT &&
2745 status != GOT_STATUS_ADD) {
2746 err = (*a->progress_cb)(a->progress_arg, status, path2);
2750 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2752 err = got_object_blob_read_to_str(&link_target2, blob2);
2755 err = merge_symlink(a->worktree, blob1, ondisk_path,
2756 path2, a->label_orig, link_target2, a->commit_id2,
2757 repo, a->progress_cb, a->progress_arg);
2760 err = merge_blob(&local_changes_subsumed, a->worktree,
2761 blob1, ondisk_path, path2, sb.st_mode,
2762 a->label_orig, blob2, a->commit_id2, repo,
2763 a->progress_cb, a->progress_arg);
2766 ie = got_fileindex_entry_get(a->fileindex, path1,
2769 return (*a->progress_cb)(a->progress_arg,
2770 GOT_STATUS_MISSING, path1);
2772 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2774 return got_error_from_errno("asprintf");
2776 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2782 case GOT_STATUS_NO_CHANGE:
2783 err = (*a->progress_cb)(a->progress_arg,
2784 GOT_STATUS_DELETE, path1);
2787 err = remove_ondisk_file(a->worktree->root_path, path1);
2791 got_fileindex_entry_mark_deleted_from_disk(ie);
2793 case GOT_STATUS_DELETE:
2794 case GOT_STATUS_MISSING:
2795 err = (*a->progress_cb)(a->progress_arg,
2796 GOT_STATUS_DELETE, path1);
2800 got_fileindex_entry_mark_deleted_from_disk(ie);
2802 case GOT_STATUS_ADD: {
2803 struct got_object_id *id;
2806 * Delete the added file only if its content already
2807 * exists in the repository.
2809 err = got_object_blob_file_create(&id, &blob1_f, path1);
2812 if (got_object_id_cmp(id, id1) == 0) {
2813 err = (*a->progress_cb)(a->progress_arg,
2814 GOT_STATUS_DELETE, path1);
2817 err = remove_ondisk_file(a->worktree->root_path,
2822 got_fileindex_entry_remove(a->fileindex,
2825 err = (*a->progress_cb)(a->progress_arg,
2826 GOT_STATUS_CANNOT_DELETE, path1);
2828 if (fclose(blob1_f) == EOF && err == NULL)
2829 err = got_error_from_errno("fclose");
2835 case GOT_STATUS_MODIFY:
2836 case GOT_STATUS_CONFLICT:
2837 err = (*a->progress_cb)(a->progress_arg,
2838 GOT_STATUS_CANNOT_DELETE, path1);
2842 case GOT_STATUS_OBSTRUCTED:
2843 err = (*a->progress_cb)(a->progress_arg, status, path1);
2851 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2853 return got_error_from_errno("asprintf");
2854 ie = got_fileindex_entry_get(a->fileindex, path2,
2857 err = get_file_status(&status, &sb, ie, ondisk_path,
2861 if (status != GOT_STATUS_NO_CHANGE &&
2862 status != GOT_STATUS_MODIFY &&
2863 status != GOT_STATUS_CONFLICT &&
2864 status != GOT_STATUS_ADD) {
2865 err = (*a->progress_cb)(a->progress_arg,
2869 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2871 err = got_object_blob_read_to_str(&link_target2,
2875 err = merge_symlink(a->worktree, NULL,
2876 ondisk_path, path2, a->label_orig,
2877 link_target2, a->commit_id2, repo,
2878 a->progress_cb, a->progress_arg);
2880 } else if (S_ISREG(sb.st_mode)) {
2881 err = merge_blob(&local_changes_subsumed,
2882 a->worktree, NULL, ondisk_path, path2,
2883 sb.st_mode, a->label_orig, blob2,
2884 a->commit_id2, repo, a->progress_cb,
2887 err = got_error_path(ondisk_path,
2888 GOT_ERR_FILE_OBSTRUCTED);
2892 if (status == GOT_STATUS_DELETE) {
2893 err = got_fileindex_entry_update(ie,
2894 ondisk_path, blob2->id.sha1,
2895 a->worktree->base_commit_id->sha1, 0);
2900 int is_bad_symlink = 0;
2901 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2902 if (S_ISLNK(mode2)) {
2903 err = install_symlink(&is_bad_symlink,
2904 a->worktree, ondisk_path, path2, blob2, 0,
2905 0, 1, repo, a->progress_cb, a->progress_arg);
2907 err = install_blob(a->worktree, ondisk_path, path2,
2908 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2909 a->progress_cb, a->progress_arg);
2913 err = got_fileindex_entry_alloc(&ie, path2);
2916 err = got_fileindex_entry_update(ie, ondisk_path,
2919 got_fileindex_entry_free(ie);
2922 err = got_fileindex_entry_add(a->fileindex, ie);
2924 got_fileindex_entry_free(ie);
2927 if (is_bad_symlink) {
2928 got_fileindex_entry_filetype_set(ie,
2929 GOT_FILEIDX_MODE_BAD_SYMLINK);
2938 struct check_merge_ok_arg {
2939 struct got_worktree *worktree;
2940 struct got_repository *repo;
2943 static const struct got_error *
2944 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2946 const struct got_error *err = NULL;
2947 struct check_merge_ok_arg *a = arg;
2948 unsigned char status;
2952 /* Reject merges into a work tree with mixed base commits. */
2953 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2954 SHA1_DIGEST_LENGTH))
2955 return got_error(GOT_ERR_MIXED_COMMITS);
2957 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2959 return got_error_from_errno("asprintf");
2961 /* Reject merges into a work tree with conflicted files. */
2962 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2965 if (status == GOT_STATUS_CONFLICT)
2966 return got_error(GOT_ERR_CONFLICTS);
2971 static const struct got_error *
2972 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2973 const char *fileindex_path, struct got_object_id *commit_id1,
2974 struct got_object_id *commit_id2, struct got_repository *repo,
2975 got_worktree_checkout_cb progress_cb, void *progress_arg,
2976 got_cancel_cb cancel_cb, void *cancel_arg)
2978 const struct got_error *err = NULL, *sync_err;
2979 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2980 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2981 struct merge_file_cb_arg arg;
2982 char *label_orig = NULL;
2985 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2986 worktree->path_prefix);
2987 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
2993 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2997 err = got_object_id_str(&id_str, commit_id1);
3001 if (asprintf(&label_orig, "%s: commit %s",
3002 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3003 err = got_error_from_errno("asprintf");
3010 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3011 worktree->path_prefix);
3015 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3019 arg.worktree = worktree;
3020 arg.fileindex = fileindex;
3021 arg.progress_cb = progress_cb;
3022 arg.progress_arg = progress_arg;
3023 arg.cancel_cb = cancel_cb;
3024 arg.cancel_arg = cancel_arg;
3025 arg.label_orig = label_orig;
3026 arg.commit_id2 = commit_id2;
3027 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3028 sync_err = sync_fileindex(fileindex, fileindex_path);
3029 if (sync_err && err == NULL)
3033 got_object_tree_close(tree1);
3035 got_object_tree_close(tree2);
3040 const struct got_error *
3041 got_worktree_merge_files(struct got_worktree *worktree,
3042 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3043 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3044 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3046 const struct got_error *err, *unlockerr;
3047 char *fileindex_path = NULL;
3048 struct got_fileindex *fileindex = NULL;
3049 struct check_merge_ok_arg mok_arg;
3051 err = lock_worktree(worktree, LOCK_EX);
3055 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3059 mok_arg.worktree = worktree;
3060 mok_arg.repo = repo;
3061 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
3066 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3067 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
3070 got_fileindex_free(fileindex);
3071 free(fileindex_path);
3072 unlockerr = lock_worktree(worktree, LOCK_SH);
3073 if (unlockerr && err == NULL)
3078 struct diff_dir_cb_arg {
3079 struct got_fileindex *fileindex;
3080 struct got_worktree *worktree;
3081 const char *status_path;
3082 size_t status_path_len;
3083 struct got_repository *repo;
3084 got_worktree_status_cb status_cb;
3086 got_cancel_cb cancel_cb;
3088 /* A pathlist containing per-directory pathlists of ignore patterns. */
3089 struct got_pathlist_head ignores;
3090 int report_unchanged;
3094 static const struct got_error *
3095 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3096 int dirfd, const char *de_name,
3097 got_worktree_status_cb status_cb, void *status_arg,
3098 struct got_repository *repo, int report_unchanged)
3100 const struct got_error *err = NULL;
3101 unsigned char status = GOT_STATUS_NO_CHANGE;
3102 unsigned char staged_status = get_staged_status(ie);
3104 struct got_object_id blob_id, commit_id, staged_blob_id;
3105 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3106 struct got_object_id *staged_blob_idp = NULL;
3108 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3112 if (status == GOT_STATUS_NO_CHANGE &&
3113 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3116 if (got_fileindex_entry_has_blob(ie)) {
3117 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3118 blob_idp = &blob_id;
3120 if (got_fileindex_entry_has_commit(ie)) {
3121 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3122 commit_idp = &commit_id;
3124 if (staged_status == GOT_STATUS_ADD ||
3125 staged_status == GOT_STATUS_MODIFY) {
3126 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3127 SHA1_DIGEST_LENGTH);
3128 staged_blob_idp = &staged_blob_id;
3131 return (*status_cb)(status_arg, status, staged_status,
3132 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3135 static const struct got_error *
3136 status_old_new(void *arg, struct got_fileindex_entry *ie,
3137 struct dirent *de, const char *parent_path, int dirfd)
3139 const struct got_error *err = NULL;
3140 struct diff_dir_cb_arg *a = arg;
3143 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3144 return got_error(GOT_ERR_CANCELLED);
3146 if (got_path_cmp(parent_path, a->status_path,
3147 strlen(parent_path), a->status_path_len) != 0 &&
3148 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3151 if (parent_path[0]) {
3152 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3153 parent_path, de->d_name) == -1)
3154 return got_error_from_errno("asprintf");
3156 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3158 return got_error_from_errno("asprintf");
3161 err = report_file_status(ie, abspath, dirfd, de->d_name,
3162 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3167 static const struct got_error *
3168 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3170 struct diff_dir_cb_arg *a = arg;
3171 struct got_object_id blob_id, commit_id;
3172 unsigned char status;
3174 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3175 return got_error(GOT_ERR_CANCELLED);
3177 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3180 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3181 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3182 if (got_fileindex_entry_has_file_on_disk(ie))
3183 status = GOT_STATUS_MISSING;
3185 status = GOT_STATUS_DELETE;
3186 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3187 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3191 free_ignorelist(struct got_pathlist_head *ignorelist)
3193 struct got_pathlist_entry *pe;
3195 TAILQ_FOREACH(pe, ignorelist, entry)
3196 free((char *)pe->path);
3197 got_pathlist_free(ignorelist);
3201 free_ignores(struct got_pathlist_head *ignores)
3203 struct got_pathlist_entry *pe;
3205 TAILQ_FOREACH(pe, ignores, entry) {
3206 struct got_pathlist_head *ignorelist = pe->data;
3207 free_ignorelist(ignorelist);
3208 free((char *)pe->path);
3210 got_pathlist_free(ignores);
3213 static const struct got_error *
3214 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3216 const struct got_error *err = NULL;
3217 struct got_pathlist_entry *pe = NULL;
3218 struct got_pathlist_head *ignorelist;
3219 char *line = NULL, *pattern, *dirpath = NULL;
3220 size_t linesize = 0;
3223 ignorelist = calloc(1, sizeof(*ignorelist));
3224 if (ignorelist == NULL)
3225 return got_error_from_errno("calloc");
3226 TAILQ_INIT(ignorelist);
3228 while ((linelen = getline(&line, &linesize, f)) != -1) {
3229 if (linelen > 0 && line[linelen - 1] == '\n')
3230 line[linelen - 1] = '\0';
3232 /* Git's ignores may contain comments. */
3236 /* Git's negated patterns are not (yet?) supported. */
3240 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3242 err = got_error_from_errno("asprintf");
3245 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3250 err = got_error_from_errno("getline");
3254 dirpath = strdup(path);
3255 if (dirpath == NULL) {
3256 err = got_error_from_errno("strdup");
3259 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3262 if (err || pe == NULL) {
3264 free_ignorelist(ignorelist);
3270 match_ignores(struct got_pathlist_head *ignores, const char *path)
3272 struct got_pathlist_entry *pe;
3274 /* Handle patterns which match in all directories. */
3275 TAILQ_FOREACH(pe, ignores, entry) {
3276 struct got_pathlist_head *ignorelist = pe->data;
3277 struct got_pathlist_entry *pi;
3279 TAILQ_FOREACH(pi, ignorelist, entry) {
3280 const char *p, *pattern = pi->path;
3282 if (strncmp(pattern, "**/", 3) != 0)
3287 if (fnmatch(pattern, p,
3288 FNM_PATHNAME | FNM_LEADING_DIR)) {
3289 /* Retry in next directory. */
3290 while (*p && *p != '/')
3302 * The ignores pathlist contains ignore lists from children before
3303 * parents, so we can find the most specific ignorelist by walking
3304 * ignores backwards.
3306 pe = TAILQ_LAST(ignores, got_pathlist_head);
3308 if (got_path_is_child(path, pe->path, pe->path_len)) {
3309 struct got_pathlist_head *ignorelist = pe->data;
3310 struct got_pathlist_entry *pi;
3311 TAILQ_FOREACH(pi, ignorelist, entry) {
3312 const char *pattern = pi->path;
3313 int flags = FNM_LEADING_DIR;
3314 if (strstr(pattern, "/**/") == NULL)
3315 flags |= FNM_PATHNAME;
3316 if (fnmatch(pattern, path, flags))
3321 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3327 static const struct got_error *
3328 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3329 const char *path, int dirfd, const char *ignores_filename)
3331 const struct got_error *err = NULL;
3334 FILE *ignoresfile = NULL;
3336 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3337 path[0] ? "/" : "", ignores_filename) == -1)
3338 return got_error_from_errno("asprintf");
3341 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3343 if (errno != ENOENT && errno != EACCES)
3344 err = got_error_from_errno2("openat",
3347 ignoresfile = fdopen(fd, "r");
3348 if (ignoresfile == NULL)
3349 err = got_error_from_errno2("fdopen",
3353 err = read_ignores(ignores, path, ignoresfile);
3357 ignoresfile = fopen(ignorespath, "r");
3358 if (ignoresfile == NULL) {
3359 if (errno != ENOENT && errno != EACCES)
3360 err = got_error_from_errno2("fopen",
3363 err = read_ignores(ignores, path, ignoresfile);
3366 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3367 err = got_error_from_errno2("fclose", path);
3368 if (fd != -1 && close(fd) == -1 && err == NULL)
3369 err = got_error_from_errno2("close", path);
3374 static const struct got_error *
3375 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3377 const struct got_error *err = NULL;
3378 struct diff_dir_cb_arg *a = arg;
3381 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3382 return got_error(GOT_ERR_CANCELLED);
3384 if (parent_path[0]) {
3385 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3386 return got_error_from_errno("asprintf");
3391 if (de->d_type != DT_DIR &&
3392 got_path_is_child(path, a->status_path, a->status_path_len)
3393 && !match_ignores(&a->ignores, path))
3394 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3395 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3401 static const struct got_error *
3402 status_traverse(void *arg, const char *path, int dirfd)
3404 const struct got_error *err = NULL;
3405 struct diff_dir_cb_arg *a = arg;
3410 err = add_ignores(&a->ignores, a->worktree->root_path,
3411 path, dirfd, ".cvsignore");
3415 err = add_ignores(&a->ignores, a->worktree->root_path, path,
3416 dirfd, ".gitignore");
3421 static const struct got_error *
3422 report_single_file_status(const char *path, const char *ondisk_path,
3423 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3424 void *status_arg, struct got_repository *repo, int report_unchanged)
3426 struct got_fileindex_entry *ie;
3429 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3431 return report_file_status(ie, ondisk_path, -1, NULL,
3432 status_cb, status_arg, repo, report_unchanged);
3434 if (lstat(ondisk_path, &sb) == -1) {
3435 if (errno != ENOENT)
3436 return got_error_from_errno2("lstat", ondisk_path);
3437 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3438 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3442 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3443 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3444 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3449 static const struct got_error *
3450 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3451 const char *root_path, const char *path)
3453 const struct got_error *err;
3454 char *parent_path, *next_parent_path = NULL;
3456 err = add_ignores(ignores, root_path, "", -1,
3461 err = add_ignores(ignores, root_path, "", -1,
3466 err = got_path_dirname(&parent_path, path);
3468 if (err->code == GOT_ERR_BAD_PATH)
3469 return NULL; /* cannot traverse parent */
3473 err = add_ignores(ignores, root_path, parent_path, -1,
3477 err = add_ignores(ignores, root_path, parent_path, -1,
3481 err = got_path_dirname(&next_parent_path, parent_path);
3483 if (err->code == GOT_ERR_BAD_PATH)
3484 err = NULL; /* traversed everything */
3488 parent_path = next_parent_path;
3489 next_parent_path = NULL;
3493 free(next_parent_path);
3497 static const struct got_error *
3498 worktree_status(struct got_worktree *worktree, const char *path,
3499 struct got_fileindex *fileindex, struct got_repository *repo,
3500 got_worktree_status_cb status_cb, void *status_arg,
3501 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3502 int report_unchanged)
3504 const struct got_error *err = NULL;
3506 struct got_fileindex_diff_dir_cb fdiff_cb;
3507 struct diff_dir_cb_arg arg;
3508 char *ondisk_path = NULL;
3510 TAILQ_INIT(&arg.ignores);
3512 if (asprintf(&ondisk_path, "%s%s%s",
3513 worktree->root_path, path[0] ? "/" : "", path) == -1)
3514 return got_error_from_errno("asprintf");
3516 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3518 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3520 err = got_error_from_errno2("open", ondisk_path);
3522 err = report_single_file_status(path, ondisk_path,
3523 fileindex, status_cb, status_arg, repo,
3526 fdiff_cb.diff_old_new = status_old_new;
3527 fdiff_cb.diff_old = status_old;
3528 fdiff_cb.diff_new = status_new;
3529 fdiff_cb.diff_traverse = status_traverse;
3530 arg.fileindex = fileindex;
3531 arg.worktree = worktree;
3532 arg.status_path = path;
3533 arg.status_path_len = strlen(path);
3535 arg.status_cb = status_cb;
3536 arg.status_arg = status_arg;
3537 arg.cancel_cb = cancel_cb;
3538 arg.cancel_arg = cancel_arg;
3539 arg.report_unchanged = report_unchanged;
3540 arg.no_ignores = no_ignores;
3542 err = add_ignores_from_parent_paths(&arg.ignores,
3543 worktree->root_path, path);
3547 err = got_fileindex_diff_dir(fileindex, fd,
3548 worktree->root_path, path, repo, &fdiff_cb, &arg);
3551 free_ignores(&arg.ignores);
3552 if (fd != -1 && close(fd) != 0 && err == NULL)
3553 err = got_error_from_errno("close");
3558 const struct got_error *
3559 got_worktree_status(struct got_worktree *worktree,
3560 struct got_pathlist_head *paths, struct got_repository *repo,
3561 got_worktree_status_cb status_cb, void *status_arg,
3562 got_cancel_cb cancel_cb, void *cancel_arg)
3564 const struct got_error *err = NULL;
3565 char *fileindex_path = NULL;
3566 struct got_fileindex *fileindex = NULL;
3567 struct got_pathlist_entry *pe;
3569 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3573 TAILQ_FOREACH(pe, paths, entry) {
3574 err = worktree_status(worktree, pe->path, fileindex, repo,
3575 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3579 free(fileindex_path);
3580 got_fileindex_free(fileindex);
3584 const struct got_error *
3585 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3588 const struct got_error *err = NULL;
3589 char *resolved = NULL, *cwd = NULL, *path = NULL;
3595 cwd = getcwd(NULL, 0);
3597 return got_error_from_errno("getcwd");
3599 if (lstat(arg, &sb) == -1) {
3600 if (errno != ENOENT) {
3601 err = got_error_from_errno2("lstat", arg);
3605 if (S_ISLNK(sb.st_mode)) {
3607 * We cannot use realpath(3) with symlinks since we want to
3608 * operate on the symlink itself.
3609 * But we can make the path absolute, assuming it is relative
3610 * to the current working directory, and then canonicalize it.
3612 char *abspath = NULL;
3613 char canonpath[PATH_MAX];
3614 if (!got_path_is_absolute(arg)) {
3615 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3616 err = got_error_from_errno("asprintf");
3621 err = got_canonpath(abspath ? abspath : arg, canonpath,
3625 resolved = strdup(canonpath);
3626 if (resolved == NULL) {
3627 err = got_error_from_errno("strdup");
3631 resolved = realpath(arg, NULL);
3632 if (resolved == NULL) {
3633 if (errno != ENOENT) {
3634 err = got_error_from_errno2("realpath", arg);
3637 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
3638 err = got_error_from_errno("asprintf");
3644 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3645 strlen(got_worktree_get_root_path(worktree)))) {
3646 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3650 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3651 err = got_path_skip_common_ancestor(&path,
3652 got_worktree_get_root_path(worktree), resolved);
3658 err = got_error_from_errno("strdup");
3663 /* XXX status walk can't deal with trailing slash! */
3665 while (len > 0 && path[len - 1] == '/') {
3666 path[len - 1] = '\0';
3679 struct schedule_addition_args {
3680 struct got_worktree *worktree;
3681 struct got_fileindex *fileindex;
3682 got_worktree_checkout_cb progress_cb;
3684 struct got_repository *repo;
3687 static const struct got_error *
3688 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3689 const char *relpath, struct got_object_id *blob_id,
3690 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3691 int dirfd, const char *de_name)
3693 struct schedule_addition_args *a = arg;
3694 const struct got_error *err = NULL;
3695 struct got_fileindex_entry *ie;
3699 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3701 return got_error_from_errno("asprintf");
3703 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3705 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3709 /* Re-adding an existing entry is a no-op. */
3710 if (status == GOT_STATUS_ADD)
3712 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3717 if (status != GOT_STATUS_UNVERSIONED) {
3718 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3722 err = got_fileindex_entry_alloc(&ie, relpath);
3725 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3727 got_fileindex_entry_free(ie);
3730 err = got_fileindex_entry_add(a->fileindex, ie);
3732 got_fileindex_entry_free(ie);
3739 if (status == GOT_STATUS_ADD)
3741 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3744 const struct got_error *
3745 got_worktree_schedule_add(struct got_worktree *worktree,
3746 struct got_pathlist_head *paths,
3747 got_worktree_checkout_cb progress_cb, void *progress_arg,
3748 struct got_repository *repo, int no_ignores)
3750 struct got_fileindex *fileindex = NULL;
3751 char *fileindex_path = NULL;
3752 const struct got_error *err = NULL, *sync_err, *unlockerr;
3753 struct got_pathlist_entry *pe;
3754 struct schedule_addition_args saa;
3756 err = lock_worktree(worktree, LOCK_EX);
3760 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3764 saa.worktree = worktree;
3765 saa.fileindex = fileindex;
3766 saa.progress_cb = progress_cb;
3767 saa.progress_arg = progress_arg;
3770 TAILQ_FOREACH(pe, paths, entry) {
3771 err = worktree_status(worktree, pe->path, fileindex, repo,
3772 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3776 sync_err = sync_fileindex(fileindex, fileindex_path);
3777 if (sync_err && err == NULL)
3780 free(fileindex_path);
3782 got_fileindex_free(fileindex);
3783 unlockerr = lock_worktree(worktree, LOCK_SH);
3784 if (unlockerr && err == NULL)
3789 struct schedule_deletion_args {
3790 struct got_worktree *worktree;
3791 struct got_fileindex *fileindex;
3792 got_worktree_delete_cb progress_cb;
3794 struct got_repository *repo;
3795 int delete_local_mods;
3797 const char *status_codes;
3800 static const struct got_error *
3801 schedule_for_deletion(void *arg, unsigned char status,
3802 unsigned char staged_status, const char *relpath,
3803 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3804 struct got_object_id *commit_id, int dirfd, const char *de_name)
3806 struct schedule_deletion_args *a = arg;
3807 const struct got_error *err = NULL;
3808 struct got_fileindex_entry *ie = NULL;
3810 char *ondisk_path, *parent = NULL;
3812 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3814 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3816 staged_status = get_staged_status(ie);
3817 if (staged_status != GOT_STATUS_NO_CHANGE) {
3818 if (staged_status == GOT_STATUS_DELETE)
3820 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3823 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3825 return got_error_from_errno("asprintf");
3827 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3832 if (a->status_codes) {
3833 size_t ncodes = strlen(a->status_codes);
3835 for (i = 0; i < ncodes ; i++) {
3836 if (status == a->status_codes[i])
3840 /* Do not delete files in non-matching status. */
3844 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
3845 a->status_codes[i] != GOT_STATUS_MISSING) {
3846 static char msg[64];
3847 snprintf(msg, sizeof(msg),
3848 "invalid status code '%c'", a->status_codes[i]);
3849 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
3854 if (status != GOT_STATUS_NO_CHANGE) {
3855 if (status == GOT_STATUS_DELETE)
3857 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3858 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3861 if (status != GOT_STATUS_MODIFY &&
3862 status != GOT_STATUS_MISSING) {
3863 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3868 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3870 if (unlinkat(dirfd, de_name, 0) != 0) {
3871 err = got_error_from_errno2("unlinkat",
3875 } else if (unlink(ondisk_path) != 0) {
3876 err = got_error_from_errno2("unlink", ondisk_path);
3880 parent = dirname(ondisk_path);
3882 if (parent == NULL) {
3883 err = got_error_from_errno2("dirname", ondisk_path);
3886 while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3887 if (rmdir(parent) == -1) {
3888 if (errno != ENOTEMPTY)
3889 err = got_error_from_errno2("rmdir",
3893 parent = dirname(parent);
3894 if (parent == NULL) {
3895 err = got_error_from_errno2("dirname", parent);
3901 got_fileindex_entry_mark_deleted_from_disk(ie);
3906 if (status == GOT_STATUS_DELETE)
3908 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3909 staged_status, relpath);
3912 const struct got_error *
3913 got_worktree_schedule_delete(struct got_worktree *worktree,
3914 struct got_pathlist_head *paths, int delete_local_mods,
3915 const char *status_codes,
3916 got_worktree_delete_cb progress_cb, void *progress_arg,
3917 struct got_repository *repo, int keep_on_disk)
3919 struct got_fileindex *fileindex = NULL;
3920 char *fileindex_path = NULL;
3921 const struct got_error *err = NULL, *sync_err, *unlockerr;
3922 struct got_pathlist_entry *pe;
3923 struct schedule_deletion_args sda;
3925 err = lock_worktree(worktree, LOCK_EX);
3929 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3933 sda.worktree = worktree;
3934 sda.fileindex = fileindex;
3935 sda.progress_cb = progress_cb;
3936 sda.progress_arg = progress_arg;
3938 sda.delete_local_mods = delete_local_mods;
3939 sda.keep_on_disk = keep_on_disk;
3940 sda.status_codes = status_codes;
3942 TAILQ_FOREACH(pe, paths, entry) {
3943 err = worktree_status(worktree, pe->path, fileindex, repo,
3944 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3948 sync_err = sync_fileindex(fileindex, fileindex_path);
3949 if (sync_err && err == NULL)
3952 free(fileindex_path);
3954 got_fileindex_free(fileindex);
3955 unlockerr = lock_worktree(worktree, LOCK_SH);
3956 if (unlockerr && err == NULL)
3961 static const struct got_error *
3962 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3964 const struct got_error *err = NULL;
3966 size_t linesize = 0, n;
3969 linelen = getline(&line, &linesize, infile);
3970 if (linelen == -1) {
3971 if (ferror(infile)) {
3972 err = got_error_from_errno("getline");
3978 n = fwrite(line, 1, linelen, outfile);
3980 err = got_ferror(outfile, GOT_ERR_IO);
3985 n = fwrite(line, 1, linelen, rejectfile);
3987 err = got_ferror(outfile, GOT_ERR_IO);
3994 static const struct got_error *
3995 skip_one_line(FILE *f)
3998 size_t linesize = 0;
4001 linelen = getline(&line, &linesize, f);
4002 if (linelen == -1) {
4004 return got_error_from_errno("getline");
4011 static const struct got_error *
4012 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4013 int start_old, int end_old, int start_new, int end_new,
4014 FILE *outfile, FILE *rejectfile)
4016 const struct got_error *err;
4018 /* Copy old file's lines leading up to patch. */
4019 while (!feof(f1) && *line_cur1 < start_old) {
4020 err = copy_one_line(f1, outfile, NULL);
4025 /* Skip new file's lines leading up to patch. */
4026 while (!feof(f2) && *line_cur2 < start_new) {
4028 err = copy_one_line(f2, NULL, rejectfile);
4030 err = skip_one_line(f2);
4035 /* Copy patched lines. */
4036 while (!feof(f2) && *line_cur2 <= end_new) {
4037 err = copy_one_line(f2, outfile, NULL);
4042 /* Skip over old file's replaced lines. */
4043 while (!feof(f1) && *line_cur1 <= end_old) {
4045 err = copy_one_line(f1, NULL, rejectfile);
4047 err = skip_one_line(f1);
4056 static const struct got_error *
4057 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4058 FILE *outfile, FILE *rejectfile)
4060 const struct got_error *err;
4063 /* Copy old file's lines until EOF. */
4065 err = copy_one_line(f1, outfile, NULL);
4072 /* Copy new file's lines until EOF. */
4074 err = copy_one_line(f2, NULL, rejectfile);
4084 static const struct got_error *
4085 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
4086 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
4087 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
4088 int *line_cur2, FILE *outfile, FILE *rejectfile,
4089 got_worktree_patch_cb patch_cb, void *patch_arg)
4091 const struct got_error *err = NULL;
4092 int start_old = change->cv.a;
4093 int end_old = change->cv.b;
4094 int start_new = change->cv.c;
4095 int end_new = change->cv.d;
4099 *choice = GOT_PATCH_CHOICE_NONE;
4101 hunkfile = got_opentemp();
4102 if (hunkfile == NULL)
4103 return got_error_from_errno("got_opentemp");
4108 /* XXX TODO needs error checking */
4109 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
4111 if (fseek(f1, pos1, SEEK_SET) == -1) {
4112 err = got_ferror(f1, GOT_ERR_IO);
4115 if (fseek(f2, pos2, SEEK_SET) == -1) {
4116 err = got_ferror(f1, GOT_ERR_IO);
4119 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4120 err = got_ferror(hunkfile, GOT_ERR_IO);
4124 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4125 hunkfile, n, nchanges);
4130 case GOT_PATCH_CHOICE_YES:
4131 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4132 end_old, start_new, end_new, outfile, rejectfile);
4134 case GOT_PATCH_CHOICE_NO:
4135 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4136 end_old, start_new, end_new, rejectfile, outfile);
4138 case GOT_PATCH_CHOICE_QUIT:
4141 err = got_error(GOT_ERR_PATCH_CHOICE);
4145 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4146 err = got_error_from_errno("fclose");
4150 struct revert_file_args {
4151 struct got_worktree *worktree;
4152 struct got_fileindex *fileindex;
4153 got_worktree_checkout_cb progress_cb;
4155 got_worktree_patch_cb patch_cb;
4157 struct got_repository *repo;
4160 static const struct got_error *
4161 create_patched_content(char **path_outfile, int reverse_patch,
4162 struct got_object_id *blob_id, const char *path2,
4163 int dirfd2, const char *de_name2,
4164 const char *relpath, struct got_repository *repo,
4165 got_worktree_patch_cb patch_cb, void *patch_arg)
4167 const struct got_error *err;
4168 struct got_blob_object *blob = NULL;
4169 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4171 char link_target[PATH_MAX];
4172 ssize_t link_len = 0;
4173 char *path1 = NULL, *id_str = NULL;
4174 struct stat sb1, sb2;
4175 struct got_diff_changes *changes = NULL;
4176 struct got_diff_state *ds = NULL;
4177 struct got_diff_args *args = NULL;
4178 struct got_diff_change *change;
4179 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
4182 *path_outfile = NULL;
4184 err = got_object_id_str(&id_str, blob_id);
4189 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4191 if (errno != ELOOP) {
4192 err = got_error_from_errno2("openat", path2);
4195 link_len = readlinkat(dirfd2, de_name2,
4196 link_target, sizeof(link_target));
4198 return got_error_from_errno2("readlinkat", path2);
4199 sb2.st_mode = S_IFLNK;
4200 sb2.st_size = link_len;
4203 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4205 if (errno != ELOOP) {
4206 err = got_error_from_errno2("open", path2);
4209 link_len = readlink(path2, link_target,
4210 sizeof(link_target));
4212 return got_error_from_errno2("readlink", path2);
4213 sb2.st_mode = S_IFLNK;
4214 sb2.st_size = link_len;
4218 if (fstat(fd2, &sb2) == -1) {
4219 err = got_error_from_errno2("fstat", path2);
4223 f2 = fdopen(fd2, "r");
4225 err = got_error_from_errno2("fdopen", path2);
4231 f2 = got_opentemp();
4233 err = got_error_from_errno2("got_opentemp", path2);
4236 n = fwrite(link_target, 1, link_len, f2);
4237 if (n != link_len) {
4238 err = got_ferror(f2, GOT_ERR_IO);
4241 if (fflush(f2) == EOF) {
4242 err = got_error_from_errno("fflush");
4248 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4252 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4256 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4260 if (stat(path1, &sb1) == -1) {
4261 err = got_error_from_errno2("stat", path1);
4265 err = got_diff_files(&changes, &ds, &args, &diff_flags,
4266 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
4270 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4274 if (fseek(f1, 0L, SEEK_SET) == -1)
4275 return got_ferror(f1, GOT_ERR_IO);
4276 if (fseek(f2, 0L, SEEK_SET) == -1)
4277 return got_ferror(f2, GOT_ERR_IO);
4278 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
4280 err = apply_or_reject_change(&choice, change, ++n,
4281 changes->nchanges, ds, args, diff_flags, relpath,
4282 f1, f2, &line_cur1, &line_cur2,
4283 reverse_patch ? NULL : outfile,
4284 reverse_patch ? outfile : NULL,
4285 patch_cb, patch_arg);
4288 if (choice == GOT_PATCH_CHOICE_YES)
4290 else if (choice == GOT_PATCH_CHOICE_QUIT)
4294 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4295 reverse_patch ? NULL : outfile,
4296 reverse_patch ? outfile : NULL);
4300 if (!S_ISLNK(sb2.st_mode)) {
4301 if (chmod(*path_outfile, sb2.st_mode) == -1) {
4302 err = got_error_from_errno2("chmod", path2);
4310 got_object_blob_close(blob);
4311 if (f1 && fclose(f1) == EOF && err == NULL)
4312 err = got_error_from_errno2("fclose", path1);
4313 if (f2 && fclose(f2) == EOF && err == NULL)
4314 err = got_error_from_errno2("fclose", path2);
4315 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4316 err = got_error_from_errno2("close", path2);
4317 if (outfile && fclose(outfile) == EOF && err == NULL)
4318 err = got_error_from_errno2("fclose", *path_outfile);
4319 if (path1 && unlink(path1) == -1 && err == NULL)
4320 err = got_error_from_errno2("unlink", path1);
4321 if (err || !have_content) {
4322 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4323 err = got_error_from_errno2("unlink", *path_outfile);
4324 free(*path_outfile);
4325 *path_outfile = NULL;
4329 got_diff_state_free(ds);
4333 got_diff_free_changes(changes);
4338 static const struct got_error *
4339 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4340 const char *relpath, struct got_object_id *blob_id,
4341 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4342 int dirfd, const char *de_name)
4344 struct revert_file_args *a = arg;
4345 const struct got_error *err = NULL;
4346 char *parent_path = NULL;
4347 struct got_fileindex_entry *ie;
4348 struct got_tree_object *tree = NULL;
4349 struct got_object_id *tree_id = NULL;
4350 const struct got_tree_entry *te = NULL;
4351 char *tree_path = NULL, *te_name;
4352 char *ondisk_path = NULL, *path_content = NULL;
4353 struct got_blob_object *blob = NULL;
4355 /* Reverting a staged deletion is a no-op. */
4356 if (status == GOT_STATUS_DELETE &&
4357 staged_status != GOT_STATUS_NO_CHANGE)
4360 if (status == GOT_STATUS_UNVERSIONED)
4361 return (*a->progress_cb)(a->progress_arg,
4362 GOT_STATUS_UNVERSIONED, relpath);
4364 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4366 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4368 /* Construct in-repository path of tree which contains this blob. */
4369 err = got_path_dirname(&parent_path, ie->path);
4371 if (err->code != GOT_ERR_BAD_PATH)
4373 parent_path = strdup("/");
4374 if (parent_path == NULL) {
4375 err = got_error_from_errno("strdup");
4379 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4380 tree_path = strdup(parent_path);
4381 if (tree_path == NULL) {
4382 err = got_error_from_errno("strdup");
4386 if (got_path_is_root_dir(parent_path)) {
4387 tree_path = strdup(a->worktree->path_prefix);
4388 if (tree_path == NULL) {
4389 err = got_error_from_errno("strdup");
4393 if (asprintf(&tree_path, "%s/%s",
4394 a->worktree->path_prefix, parent_path) == -1) {
4395 err = got_error_from_errno("asprintf");
4401 err = got_object_id_by_path(&tree_id, a->repo,
4402 a->worktree->base_commit_id, tree_path);
4404 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4405 (status == GOT_STATUS_ADD ||
4406 staged_status == GOT_STATUS_ADD)))
4409 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4413 err = got_path_basename(&te_name, ie->path);
4417 te = got_object_tree_find_entry(tree, te_name);
4419 if (te == NULL && status != GOT_STATUS_ADD &&
4420 staged_status != GOT_STATUS_ADD) {
4421 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4427 case GOT_STATUS_ADD:
4429 int choice = GOT_PATCH_CHOICE_NONE;
4430 err = (*a->patch_cb)(&choice, a->patch_arg,
4431 status, ie->path, NULL, 1, 1);
4434 if (choice != GOT_PATCH_CHOICE_YES)
4437 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4441 got_fileindex_entry_remove(a->fileindex, ie);
4443 case GOT_STATUS_DELETE:
4445 int choice = GOT_PATCH_CHOICE_NONE;
4446 err = (*a->patch_cb)(&choice, a->patch_arg,
4447 status, ie->path, NULL, 1, 1);
4450 if (choice != GOT_PATCH_CHOICE_YES)
4454 case GOT_STATUS_MODIFY:
4455 case GOT_STATUS_MODE_CHANGE:
4456 case GOT_STATUS_CONFLICT:
4457 case GOT_STATUS_MISSING: {
4458 struct got_object_id id;
4459 if (staged_status == GOT_STATUS_ADD ||
4460 staged_status == GOT_STATUS_MODIFY) {
4461 memcpy(id.sha1, ie->staged_blob_sha1,
4462 SHA1_DIGEST_LENGTH);
4464 memcpy(id.sha1, ie->blob_sha1,
4465 SHA1_DIGEST_LENGTH);
4466 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4470 if (asprintf(&ondisk_path, "%s/%s",
4471 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4472 err = got_error_from_errno("asprintf");
4476 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4477 status == GOT_STATUS_CONFLICT)) {
4478 int is_bad_symlink = 0;
4479 err = create_patched_content(&path_content, 1, &id,
4480 ondisk_path, dirfd, de_name, ie->path, a->repo,
4481 a->patch_cb, a->patch_arg);
4482 if (err || path_content == NULL)
4484 if (te && S_ISLNK(te->mode)) {
4485 if (unlink(path_content) == -1) {
4486 err = got_error_from_errno2("unlink",
4490 err = install_symlink(&is_bad_symlink,
4491 a->worktree, ondisk_path, ie->path,
4492 blob, 0, 1, 0, a->repo,
4493 a->progress_cb, a->progress_arg);
4495 if (rename(path_content, ondisk_path) == -1) {
4496 err = got_error_from_errno3("rename",
4497 path_content, ondisk_path);
4502 int is_bad_symlink = 0;
4503 if (te && S_ISLNK(te->mode)) {
4504 err = install_symlink(&is_bad_symlink,
4505 a->worktree, ondisk_path, ie->path,
4506 blob, 0, 1, 0, a->repo,
4507 a->progress_cb, a->progress_arg);
4509 err = install_blob(a->worktree, ondisk_path,
4511 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4512 got_fileindex_perms_to_st(ie), blob,
4513 0, 1, 0, 0, a->repo,
4514 a->progress_cb, a->progress_arg);
4518 if (status == GOT_STATUS_DELETE ||
4519 status == GOT_STATUS_MODE_CHANGE) {
4520 err = got_fileindex_entry_update(ie,
4521 ondisk_path, blob->id.sha1,
4522 a->worktree->base_commit_id->sha1, 1);
4526 if (is_bad_symlink) {
4527 got_fileindex_entry_filetype_set(ie,
4528 GOT_FILEIDX_MODE_BAD_SYMLINK);
4542 got_object_blob_close(blob);
4544 got_object_tree_close(tree);
4549 const struct got_error *
4550 got_worktree_revert(struct got_worktree *worktree,
4551 struct got_pathlist_head *paths,
4552 got_worktree_checkout_cb progress_cb, void *progress_arg,
4553 got_worktree_patch_cb patch_cb, void *patch_arg,
4554 struct got_repository *repo)
4556 struct got_fileindex *fileindex = NULL;
4557 char *fileindex_path = NULL;
4558 const struct got_error *err = NULL, *unlockerr = NULL;
4559 const struct got_error *sync_err = NULL;
4560 struct got_pathlist_entry *pe;
4561 struct revert_file_args rfa;
4563 err = lock_worktree(worktree, LOCK_EX);
4567 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4571 rfa.worktree = worktree;
4572 rfa.fileindex = fileindex;
4573 rfa.progress_cb = progress_cb;
4574 rfa.progress_arg = progress_arg;
4575 rfa.patch_cb = patch_cb;
4576 rfa.patch_arg = patch_arg;
4578 TAILQ_FOREACH(pe, paths, entry) {
4579 err = worktree_status(worktree, pe->path, fileindex, repo,
4580 revert_file, &rfa, NULL, NULL, 0, 0);
4584 sync_err = sync_fileindex(fileindex, fileindex_path);
4585 if (sync_err && err == NULL)
4588 free(fileindex_path);
4590 got_fileindex_free(fileindex);
4591 unlockerr = lock_worktree(worktree, LOCK_SH);
4592 if (unlockerr && err == NULL)
4598 free_commitable(struct got_commitable *ct)
4601 free(ct->in_repo_path);
4602 free(ct->ondisk_path);
4604 free(ct->base_blob_id);
4605 free(ct->staged_blob_id);
4606 free(ct->base_commit_id);
4610 struct collect_commitables_arg {
4611 struct got_pathlist_head *commitable_paths;
4612 struct got_repository *repo;
4613 struct got_worktree *worktree;
4614 struct got_fileindex *fileindex;
4615 int have_staged_files;
4616 int allow_bad_symlinks;
4619 static const struct got_error *
4620 collect_commitables(void *arg, unsigned char status,
4621 unsigned char staged_status, const char *relpath,
4622 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4623 struct got_object_id *commit_id, int dirfd, const char *de_name)
4625 struct collect_commitables_arg *a = arg;
4626 const struct got_error *err = NULL;
4627 struct got_commitable *ct = NULL;
4628 struct got_pathlist_entry *new = NULL;
4629 char *parent_path = NULL, *path = NULL;
4632 if (a->have_staged_files) {
4633 if (staged_status != GOT_STATUS_MODIFY &&
4634 staged_status != GOT_STATUS_ADD &&
4635 staged_status != GOT_STATUS_DELETE)
4638 if (status == GOT_STATUS_CONFLICT)
4639 return got_error(GOT_ERR_COMMIT_CONFLICT);
4641 if (status != GOT_STATUS_MODIFY &&
4642 status != GOT_STATUS_MODE_CHANGE &&
4643 status != GOT_STATUS_ADD &&
4644 status != GOT_STATUS_DELETE)
4648 if (asprintf(&path, "/%s", relpath) == -1) {
4649 err = got_error_from_errno("asprintf");
4652 if (strcmp(path, "/") == 0) {
4653 parent_path = strdup("");
4654 if (parent_path == NULL)
4655 return got_error_from_errno("strdup");
4657 err = got_path_dirname(&parent_path, path);
4662 ct = calloc(1, sizeof(*ct));
4664 err = got_error_from_errno("calloc");
4668 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4670 err = got_error_from_errno("asprintf");
4674 if (staged_status == GOT_STATUS_ADD ||
4675 staged_status == GOT_STATUS_MODIFY) {
4676 struct got_fileindex_entry *ie;
4677 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4678 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4679 case GOT_FILEIDX_MODE_REGULAR_FILE:
4680 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4683 case GOT_FILEIDX_MODE_SYMLINK:
4687 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4690 ct->mode |= got_fileindex_entry_perms_get(ie);
4691 } else if (status != GOT_STATUS_DELETE &&
4692 staged_status != GOT_STATUS_DELETE) {
4694 if (fstatat(dirfd, de_name, &sb,
4695 AT_SYMLINK_NOFOLLOW) == -1) {
4696 err = got_error_from_errno2("fstatat",
4700 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4701 err = got_error_from_errno2("lstat", ct->ondisk_path);
4704 ct->mode = sb.st_mode;
4707 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4708 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4710 err = got_error_from_errno("asprintf");
4714 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4715 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4717 char target_path[PATH_MAX];
4719 target_len = readlink(ct->ondisk_path, target_path,
4720 sizeof(target_path));
4721 if (target_len == -1) {
4722 err = got_error_from_errno2("readlink",
4726 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4727 target_len, ct->ondisk_path, a->worktree->root_path);
4730 if (is_bad_symlink) {
4731 err = got_error_path(ct->ondisk_path,
4732 GOT_ERR_BAD_SYMLINK);
4738 ct->status = status;
4739 ct->staged_status = staged_status;
4740 ct->blob_id = NULL; /* will be filled in when blob gets created */
4741 if (ct->status != GOT_STATUS_ADD &&
4742 ct->staged_status != GOT_STATUS_ADD) {
4743 ct->base_blob_id = got_object_id_dup(blob_id);
4744 if (ct->base_blob_id == NULL) {
4745 err = got_error_from_errno("got_object_id_dup");
4748 ct->base_commit_id = got_object_id_dup(commit_id);
4749 if (ct->base_commit_id == NULL) {
4750 err = got_error_from_errno("got_object_id_dup");
4754 if (ct->staged_status == GOT_STATUS_ADD ||
4755 ct->staged_status == GOT_STATUS_MODIFY) {
4756 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4757 if (ct->staged_blob_id == NULL) {
4758 err = got_error_from_errno("got_object_id_dup");
4762 ct->path = strdup(path);
4763 if (ct->path == NULL) {
4764 err = got_error_from_errno("strdup");
4767 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4769 if (ct && (err || new == NULL))
4770 free_commitable(ct);
4776 static const struct got_error *write_tree(struct got_object_id **, int *,
4777 struct got_tree_object *, const char *, struct got_pathlist_head *,
4778 got_worktree_status_cb status_cb, void *status_arg,
4779 struct got_repository *);
4781 static const struct got_error *
4782 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4783 struct got_tree_entry *te, const char *parent_path,
4784 struct got_pathlist_head *commitable_paths,
4785 got_worktree_status_cb status_cb, void *status_arg,
4786 struct got_repository *repo)
4788 const struct got_error *err = NULL;
4789 struct got_tree_object *subtree;
4792 if (asprintf(&subpath, "%s%s%s", parent_path,
4793 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4794 return got_error_from_errno("asprintf");
4796 err = got_object_open_as_tree(&subtree, repo, &te->id);
4800 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4801 commitable_paths, status_cb, status_arg, repo);
4802 got_object_tree_close(subtree);
4807 static const struct got_error *
4808 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4810 const struct got_error *err = NULL;
4811 char *ct_parent_path = NULL;
4815 if (strchr(ct->in_repo_path, '/') == NULL) {
4816 *match = got_path_is_root_dir(path);
4820 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4823 *match = (strcmp(path, ct_parent_path) == 0);
4824 free(ct_parent_path);
4829 get_ct_file_mode(struct got_commitable *ct)
4831 if (S_ISLNK(ct->mode))
4834 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4837 static const struct got_error *
4838 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4839 struct got_tree_entry *te, struct got_commitable *ct)
4841 const struct got_error *err = NULL;
4845 err = got_object_tree_entry_dup(new_te, te);
4849 (*new_te)->mode = get_ct_file_mode(ct);
4851 if (ct->staged_status == GOT_STATUS_MODIFY)
4852 memcpy(&(*new_te)->id, ct->staged_blob_id,
4853 sizeof((*new_te)->id));
4855 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4857 if (err && *new_te) {
4864 static const struct got_error *
4865 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4866 struct got_commitable *ct)
4868 const struct got_error *err = NULL;
4869 char *ct_name = NULL;
4873 *new_te = calloc(1, sizeof(**new_te));
4874 if (*new_te == NULL)
4875 return got_error_from_errno("calloc");
4877 err = got_path_basename(&ct_name, ct->path);
4880 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4881 sizeof((*new_te)->name)) {
4882 err = got_error(GOT_ERR_NO_SPACE);
4886 (*new_te)->mode = get_ct_file_mode(ct);
4888 if (ct->staged_status == GOT_STATUS_ADD)
4889 memcpy(&(*new_te)->id, ct->staged_blob_id,
4890 sizeof((*new_te)->id));
4892 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4895 if (err && *new_te) {
4902 static const struct got_error *
4903 insert_tree_entry(struct got_tree_entry *new_te,
4904 struct got_pathlist_head *paths)
4906 const struct got_error *err = NULL;
4907 struct got_pathlist_entry *new_pe;
4909 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4913 return got_error(GOT_ERR_TREE_DUP_ENTRY);
4917 static const struct got_error *
4918 report_ct_status(struct got_commitable *ct,
4919 got_worktree_status_cb status_cb, void *status_arg)
4921 const char *ct_path = ct->path;
4922 unsigned char status;
4924 while (ct_path[0] == '/')
4927 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4928 status = ct->staged_status;
4930 status = ct->status;
4932 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4933 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4936 static const struct got_error *
4937 match_modified_subtree(int *modified, struct got_tree_entry *te,
4938 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4940 const struct got_error *err = NULL;
4941 struct got_pathlist_entry *pe;
4946 if (asprintf(&te_path, "%s%s%s", base_tree_path,
4947 got_path_is_root_dir(base_tree_path) ? "" : "/",
4949 return got_error_from_errno("asprintf");
4951 TAILQ_FOREACH(pe, commitable_paths, entry) {
4952 struct got_commitable *ct = pe->data;
4953 *modified = got_path_is_child(ct->in_repo_path, te_path,
4963 static const struct got_error *
4964 match_deleted_or_modified_ct(struct got_commitable **ctp,
4965 struct got_tree_entry *te, const char *base_tree_path,
4966 struct got_pathlist_head *commitable_paths)
4968 const struct got_error *err = NULL;
4969 struct got_pathlist_entry *pe;
4973 TAILQ_FOREACH(pe, commitable_paths, entry) {
4974 struct got_commitable *ct = pe->data;
4975 char *ct_name = NULL;
4978 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4979 if (ct->status != GOT_STATUS_MODIFY &&
4980 ct->status != GOT_STATUS_MODE_CHANGE &&
4981 ct->status != GOT_STATUS_DELETE)
4984 if (ct->staged_status != GOT_STATUS_MODIFY &&
4985 ct->staged_status != GOT_STATUS_DELETE)
4989 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4992 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4998 err = got_path_basename(&ct_name, pe->path);
5002 if (strcmp(te->name, ct_name) != 0) {
5015 static const struct got_error *
5016 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5017 const char *child_path, const char *path_base_tree,
5018 struct got_pathlist_head *commitable_paths,
5019 got_worktree_status_cb status_cb, void *status_arg,
5020 struct got_repository *repo)
5022 const struct got_error *err = NULL;
5023 struct got_tree_entry *new_te;
5025 struct got_object_id *id = NULL;
5030 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5031 got_path_is_root_dir(path_base_tree) ? "" : "/",
5033 return got_error_from_errno("asprintf");
5035 new_te = calloc(1, sizeof(*new_te));
5037 return got_error_from_errno("calloc");
5038 new_te->mode = S_IFDIR;
5040 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5041 sizeof(new_te->name)) {
5042 err = got_error(GOT_ERR_NO_SPACE);
5045 err = write_tree(&id, &nentries, NULL, subtree_path,
5046 commitable_paths, status_cb, status_arg, repo);
5051 memcpy(&new_te->id, id, sizeof(new_te->id));
5060 static const struct got_error *
5061 write_tree(struct got_object_id **new_tree_id, int *nentries,
5062 struct got_tree_object *base_tree, const char *path_base_tree,
5063 struct got_pathlist_head *commitable_paths,
5064 got_worktree_status_cb status_cb, void *status_arg,
5065 struct got_repository *repo)
5067 const struct got_error *err = NULL;
5068 struct got_pathlist_head paths;
5069 struct got_tree_entry *te, *new_te = NULL;
5070 struct got_pathlist_entry *pe;
5075 /* Insert, and recurse into, newly added entries first. */
5076 TAILQ_FOREACH(pe, commitable_paths, entry) {
5077 struct got_commitable *ct = pe->data;
5078 char *child_path = NULL, *slash;
5080 if ((ct->status != GOT_STATUS_ADD &&
5081 ct->staged_status != GOT_STATUS_ADD) ||
5082 (ct->flags & GOT_COMMITABLE_ADDED))
5085 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5086 strlen(path_base_tree)))
5089 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5094 slash = strchr(child_path, '/');
5095 if (slash == NULL) {
5096 err = alloc_added_blob_tree_entry(&new_te, ct);
5099 err = report_ct_status(ct, status_cb, status_arg);
5102 ct->flags |= GOT_COMMITABLE_ADDED;
5103 err = insert_tree_entry(new_te, &paths);
5108 *slash = '\0'; /* trim trailing path components */
5109 if (base_tree == NULL ||
5110 got_object_tree_find_entry(base_tree, child_path)
5112 err = make_subtree_for_added_blob(&new_te,
5113 child_path, path_base_tree,
5114 commitable_paths, status_cb, status_arg,
5118 err = insert_tree_entry(new_te, &paths);
5127 int i, nbase_entries;
5128 /* Handle modified and deleted entries. */
5129 nbase_entries = got_object_tree_get_nentries(base_tree);
5130 for (i = 0; i < nbase_entries; i++) {
5131 struct got_commitable *ct = NULL;
5133 te = got_object_tree_get_entry(base_tree, i);
5134 if (got_object_tree_entry_is_submodule(te)) {
5135 /* Entry is a submodule; just copy it. */
5136 err = got_object_tree_entry_dup(&new_te, te);
5139 err = insert_tree_entry(new_te, &paths);
5146 if (S_ISDIR(te->mode)) {
5148 err = got_object_tree_entry_dup(&new_te, te);
5151 err = match_modified_subtree(&modified, te,
5152 path_base_tree, commitable_paths);
5155 /* Avoid recursion into unmodified subtrees. */
5157 struct got_object_id *new_id;
5159 err = write_subtree(&new_id,
5161 path_base_tree, commitable_paths,
5162 status_cb, status_arg, repo);
5165 if (nsubentries == 0) {
5166 /* All entries were deleted. */
5170 memcpy(&new_te->id, new_id,
5171 sizeof(new_te->id));
5174 err = insert_tree_entry(new_te, &paths);
5181 err = match_deleted_or_modified_ct(&ct, te,
5182 path_base_tree, commitable_paths);
5186 /* NB: Deleted entries get dropped here. */
5187 if (ct->status == GOT_STATUS_MODIFY ||
5188 ct->status == GOT_STATUS_MODE_CHANGE ||
5189 ct->staged_status == GOT_STATUS_MODIFY) {
5190 err = alloc_modified_blob_tree_entry(
5194 err = insert_tree_entry(new_te, &paths);
5199 err = report_ct_status(ct, status_cb,
5204 /* Entry is unchanged; just copy it. */
5205 err = got_object_tree_entry_dup(&new_te, te);
5208 err = insert_tree_entry(new_te, &paths);
5216 /* Write new list of entries; deleted entries have been dropped. */
5217 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5219 got_pathlist_free(&paths);
5223 static const struct got_error *
5224 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
5225 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
5226 int have_staged_files)
5228 const struct got_error *err = NULL;
5229 struct got_pathlist_entry *pe;
5231 TAILQ_FOREACH(pe, commitable_paths, entry) {
5232 struct got_fileindex_entry *ie;
5233 struct got_commitable *ct = pe->data;
5235 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5237 if (ct->status == GOT_STATUS_DELETE ||
5238 ct->staged_status == GOT_STATUS_DELETE) {
5239 got_fileindex_entry_remove(fileindex, ie);
5240 } else if (ct->staged_status == GOT_STATUS_ADD ||
5241 ct->staged_status == GOT_STATUS_MODIFY) {
5242 got_fileindex_entry_stage_set(ie,
5243 GOT_FILEIDX_STAGE_NONE);
5244 err = got_fileindex_entry_update(ie,
5245 ct->ondisk_path, ct->staged_blob_id->sha1,
5246 new_base_commit_id->sha1,
5247 !have_staged_files);
5249 err = got_fileindex_entry_update(ie,
5250 ct->ondisk_path, ct->blob_id->sha1,
5251 new_base_commit_id->sha1,
5252 !have_staged_files);
5254 err = got_fileindex_entry_alloc(&ie, pe->path);
5257 err = got_fileindex_entry_update(ie, ct->ondisk_path,
5258 ct->blob_id->sha1, new_base_commit_id->sha1, 1);
5260 got_fileindex_entry_free(ie);
5263 err = got_fileindex_entry_add(fileindex, ie);
5265 got_fileindex_entry_free(ie);
5274 static const struct got_error *
5275 check_out_of_date(const char *in_repo_path, unsigned char status,
5276 unsigned char staged_status, struct got_object_id *base_blob_id,
5277 struct got_object_id *base_commit_id,
5278 struct got_object_id *head_commit_id, struct got_repository *repo,
5281 const struct got_error *err = NULL;
5282 struct got_object_id *id = NULL;
5284 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5285 /* Trivial case: base commit == head commit */
5286 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5289 * Ensure file content which local changes were based
5290 * on matches file content in the branch head.
5292 err = got_object_id_by_path(&id, repo, head_commit_id,
5295 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5296 err = got_error(ood_errcode);
5298 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5299 err = got_error(ood_errcode);
5301 /* Require that added files don't exist in the branch head. */
5302 err = got_object_id_by_path(&id, repo, head_commit_id,
5304 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5306 err = id ? got_error(ood_errcode) : NULL;
5313 const struct got_error *
5314 commit_worktree(struct got_object_id **new_commit_id,
5315 struct got_pathlist_head *commitable_paths,
5316 struct got_object_id *head_commit_id, struct got_worktree *worktree,
5317 const char *author, const char *committer,
5318 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5319 got_worktree_status_cb status_cb, void *status_arg,
5320 struct got_repository *repo)
5322 const struct got_error *err = NULL, *unlockerr = NULL;
5323 struct got_pathlist_entry *pe;
5324 const char *head_ref_name = NULL;
5325 struct got_commit_object *head_commit = NULL;
5326 struct got_reference *head_ref2 = NULL;
5327 struct got_object_id *head_commit_id2 = NULL;
5328 struct got_tree_object *head_tree = NULL;
5329 struct got_object_id *new_tree_id = NULL;
5331 struct got_object_id_queue parent_ids;
5332 struct got_object_qid *pid = NULL;
5333 char *logmsg = NULL;
5335 *new_commit_id = NULL;
5337 SIMPLEQ_INIT(&parent_ids);
5339 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5343 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5347 if (commit_msg_cb != NULL) {
5348 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5353 if (logmsg == NULL || strlen(logmsg) == 0) {
5354 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5358 /* Create blobs from added and modified files and record their IDs. */
5359 TAILQ_FOREACH(pe, commitable_paths, entry) {
5360 struct got_commitable *ct = pe->data;
5363 /* Blobs for staged files already exist. */
5364 if (ct->staged_status == GOT_STATUS_ADD ||
5365 ct->staged_status == GOT_STATUS_MODIFY)
5368 if (ct->status != GOT_STATUS_ADD &&
5369 ct->status != GOT_STATUS_MODIFY &&
5370 ct->status != GOT_STATUS_MODE_CHANGE)
5373 if (asprintf(&ondisk_path, "%s/%s",
5374 worktree->root_path, pe->path) == -1) {
5375 err = got_error_from_errno("asprintf");
5378 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5384 /* Recursively write new tree objects. */
5385 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5386 commitable_paths, status_cb, status_arg, repo);
5390 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5393 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5394 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5395 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5396 got_object_qid_free(pid);
5402 /* Check if a concurrent commit to our branch has occurred. */
5403 head_ref_name = got_worktree_get_head_ref_name(worktree);
5404 if (head_ref_name == NULL) {
5405 err = got_error_from_errno("got_worktree_get_head_ref_name");
5408 /* Lock the reference here to prevent concurrent modification. */
5409 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5412 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5415 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5416 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5419 /* Update branch head in repository. */
5420 err = got_ref_change_ref(head_ref2, *new_commit_id);
5423 err = got_ref_write(head_ref2, repo);
5427 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5431 err = ref_base_commit(worktree, repo);
5436 got_object_tree_close(head_tree);
5438 got_object_commit_close(head_commit);
5439 free(head_commit_id2);
5441 unlockerr = got_ref_unlock(head_ref2);
5442 if (unlockerr && err == NULL)
5444 got_ref_close(head_ref2);
5449 static const struct got_error *
5450 check_path_is_commitable(const char *path,
5451 struct got_pathlist_head *commitable_paths)
5453 struct got_pathlist_entry *cpe = NULL;
5454 size_t path_len = strlen(path);
5456 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5457 struct got_commitable *ct = cpe->data;
5458 const char *ct_path = ct->path;
5460 while (ct_path[0] == '/')
5463 if (strcmp(path, ct_path) == 0 ||
5464 got_path_is_child(ct_path, path, path_len))
5469 return got_error_path(path, GOT_ERR_BAD_PATH);
5474 static const struct got_error *
5475 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5477 int *have_staged_files = arg;
5479 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5480 *have_staged_files = 1;
5481 return got_error(GOT_ERR_CANCELLED);
5487 static const struct got_error *
5488 check_non_staged_files(struct got_fileindex *fileindex,
5489 struct got_pathlist_head *paths)
5491 struct got_pathlist_entry *pe;
5492 struct got_fileindex_entry *ie;
5494 TAILQ_FOREACH(pe, paths, entry) {
5495 if (pe->path[0] == '\0')
5497 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5499 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5500 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5501 return got_error_path(pe->path,
5502 GOT_ERR_FILE_NOT_STAGED);
5508 const struct got_error *
5509 got_worktree_commit(struct got_object_id **new_commit_id,
5510 struct got_worktree *worktree, struct got_pathlist_head *paths,
5511 const char *author, const char *committer, int allow_bad_symlinks,
5512 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5513 got_worktree_status_cb status_cb, void *status_arg,
5514 struct got_repository *repo)
5516 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5517 struct got_fileindex *fileindex = NULL;
5518 char *fileindex_path = NULL;
5519 struct got_pathlist_head commitable_paths;
5520 struct collect_commitables_arg cc_arg;
5521 struct got_pathlist_entry *pe;
5522 struct got_reference *head_ref = NULL;
5523 struct got_object_id *head_commit_id = NULL;
5524 int have_staged_files = 0;
5526 *new_commit_id = NULL;
5528 TAILQ_INIT(&commitable_paths);
5530 err = lock_worktree(worktree, LOCK_EX);
5534 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5538 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5542 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5546 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5547 &have_staged_files);
5548 if (err && err->code != GOT_ERR_CANCELLED)
5550 if (have_staged_files) {
5551 err = check_non_staged_files(fileindex, paths);
5556 cc_arg.commitable_paths = &commitable_paths;
5557 cc_arg.worktree = worktree;
5558 cc_arg.fileindex = fileindex;
5560 cc_arg.have_staged_files = have_staged_files;
5561 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5562 TAILQ_FOREACH(pe, paths, entry) {
5563 err = worktree_status(worktree, pe->path, fileindex, repo,
5564 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5569 if (TAILQ_EMPTY(&commitable_paths)) {
5570 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5574 TAILQ_FOREACH(pe, paths, entry) {
5575 err = check_path_is_commitable(pe->path, &commitable_paths);
5580 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5581 struct got_commitable *ct = pe->data;
5582 const char *ct_path = ct->in_repo_path;
5584 while (ct_path[0] == '/')
5586 err = check_out_of_date(ct_path, ct->status,
5587 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5588 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5594 err = commit_worktree(new_commit_id, &commitable_paths,
5595 head_commit_id, worktree, author, committer,
5596 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5600 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5601 fileindex, have_staged_files);
5602 sync_err = sync_fileindex(fileindex, fileindex_path);
5603 if (sync_err && err == NULL)
5607 got_fileindex_free(fileindex);
5608 free(fileindex_path);
5609 unlockerr = lock_worktree(worktree, LOCK_SH);
5610 if (unlockerr && err == NULL)
5612 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5613 struct got_commitable *ct = pe->data;
5614 free_commitable(ct);
5616 got_pathlist_free(&commitable_paths);
5621 got_commitable_get_path(struct got_commitable *ct)
5627 got_commitable_get_status(struct got_commitable *ct)
5632 struct check_rebase_ok_arg {
5633 struct got_worktree *worktree;
5634 struct got_repository *repo;
5637 static const struct got_error *
5638 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5640 const struct got_error *err = NULL;
5641 struct check_rebase_ok_arg *a = arg;
5642 unsigned char status;
5646 /* Reject rebase of a work tree with mixed base commits. */
5647 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5648 SHA1_DIGEST_LENGTH))
5649 return got_error(GOT_ERR_MIXED_COMMITS);
5651 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5653 return got_error_from_errno("asprintf");
5655 /* Reject rebase of a work tree with modified or staged files. */
5656 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5661 if (status != GOT_STATUS_NO_CHANGE)
5662 return got_error(GOT_ERR_MODIFIED);
5663 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5664 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5669 const struct got_error *
5670 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5671 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5672 struct got_worktree *worktree, struct got_reference *branch,
5673 struct got_repository *repo)
5675 const struct got_error *err = NULL;
5676 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5677 char *branch_ref_name = NULL;
5678 char *fileindex_path = NULL;
5679 struct check_rebase_ok_arg ok_arg;
5680 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5681 struct got_object_id *wt_branch_tip = NULL;
5683 *new_base_branch_ref = NULL;
5687 err = lock_worktree(worktree, LOCK_EX);
5691 err = open_fileindex(fileindex, &fileindex_path, worktree);
5695 ok_arg.worktree = worktree;
5697 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5702 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5706 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5710 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5714 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5719 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5722 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5723 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5727 err = got_ref_alloc_symref(new_base_branch_ref,
5728 new_base_branch_ref_name, wt_branch);
5731 err = got_ref_write(*new_base_branch_ref, repo);
5735 /* TODO Lock original branch's ref while rebasing? */
5737 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5741 err = got_ref_write(branch_ref, repo);
5745 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5746 worktree->base_commit_id);
5749 err = got_ref_write(*tmp_branch, repo);
5753 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5757 free(fileindex_path);
5758 free(tmp_branch_name);
5759 free(new_base_branch_ref_name);
5760 free(branch_ref_name);
5762 got_ref_close(branch_ref);
5764 got_ref_close(wt_branch);
5765 free(wt_branch_tip);
5767 if (*new_base_branch_ref) {
5768 got_ref_close(*new_base_branch_ref);
5769 *new_base_branch_ref = NULL;
5772 got_ref_close(*tmp_branch);
5776 got_fileindex_free(*fileindex);
5779 lock_worktree(worktree, LOCK_SH);
5784 const struct got_error *
5785 got_worktree_rebase_continue(struct got_object_id **commit_id,
5786 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5787 struct got_reference **branch, struct got_fileindex **fileindex,
5788 struct got_worktree *worktree, struct got_repository *repo)
5790 const struct got_error *err;
5791 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5792 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5793 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5794 char *fileindex_path = NULL;
5795 int have_staged_files = 0;
5798 *new_base_branch = NULL;
5803 err = lock_worktree(worktree, LOCK_EX);
5807 err = open_fileindex(fileindex, &fileindex_path, worktree);
5811 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5812 &have_staged_files);
5813 if (err && err->code != GOT_ERR_CANCELLED)
5815 if (have_staged_files) {
5816 err = got_error(GOT_ERR_STAGED_PATHS);
5820 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5824 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5828 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5832 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5836 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5840 err = got_ref_open(branch, repo,
5841 got_ref_get_symref_target(branch_ref), 0);
5845 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5849 err = got_ref_resolve(commit_id, repo, commit_ref);
5853 err = got_ref_open(new_base_branch, repo,
5854 new_base_branch_ref_name, 0);
5858 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5862 free(commit_ref_name);
5863 free(branch_ref_name);
5864 free(fileindex_path);
5866 got_ref_close(commit_ref);
5868 got_ref_close(branch_ref);
5873 got_ref_close(*tmp_branch);
5876 if (*new_base_branch) {
5877 got_ref_close(*new_base_branch);
5878 *new_base_branch = NULL;
5881 got_ref_close(*branch);
5885 got_fileindex_free(*fileindex);
5888 lock_worktree(worktree, LOCK_SH);
5893 const struct got_error *
5894 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5896 const struct got_error *err;
5897 char *tmp_branch_name = NULL;
5899 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5903 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5904 free(tmp_branch_name);
5908 static const struct got_error *
5909 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5910 char **logmsg, void *arg)
5916 static const struct got_error *
5917 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5918 const char *path, struct got_object_id *blob_id,
5919 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5920 int dirfd, const char *de_name)
5925 struct collect_merged_paths_arg {
5926 got_worktree_checkout_cb progress_cb;
5928 struct got_pathlist_head *merged_paths;
5931 static const struct got_error *
5932 collect_merged_paths(void *arg, unsigned char status, const char *path)
5934 const struct got_error *err;
5935 struct collect_merged_paths_arg *a = arg;
5937 struct got_pathlist_entry *new;
5939 err = (*a->progress_cb)(a->progress_arg, status, path);
5943 if (status != GOT_STATUS_MERGE &&
5944 status != GOT_STATUS_ADD &&
5945 status != GOT_STATUS_DELETE &&
5946 status != GOT_STATUS_CONFLICT)
5951 return got_error_from_errno("strdup");
5953 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5954 if (err || new == NULL)
5960 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5962 struct got_pathlist_entry *pe;
5964 TAILQ_FOREACH(pe, merged_paths, entry)
5965 free((char *)pe->path);
5967 got_pathlist_free(merged_paths);
5970 static const struct got_error *
5971 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5972 int is_rebase, struct got_repository *repo)
5974 const struct got_error *err;
5975 struct got_reference *commit_ref = NULL;
5977 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5979 if (err->code != GOT_ERR_NOT_REF)
5981 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
5984 err = got_ref_write(commit_ref, repo);
5987 } else if (is_rebase) {
5988 struct got_object_id *stored_id;
5991 err = got_ref_resolve(&stored_id, repo, commit_ref);
5994 cmp = got_object_id_cmp(commit_id, stored_id);
5997 err = got_error(GOT_ERR_REBASE_COMMITID);
6003 got_ref_close(commit_ref);
6007 static const struct got_error *
6008 rebase_merge_files(struct got_pathlist_head *merged_paths,
6009 const char *commit_ref_name, struct got_worktree *worktree,
6010 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6011 struct got_object_id *commit_id, struct got_repository *repo,
6012 got_worktree_checkout_cb progress_cb, void *progress_arg,
6013 got_cancel_cb cancel_cb, void *cancel_arg)
6015 const struct got_error *err;
6016 struct got_reference *commit_ref = NULL;
6017 struct collect_merged_paths_arg cmp_arg;
6018 char *fileindex_path;
6020 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6022 err = get_fileindex_path(&fileindex_path, worktree);
6026 cmp_arg.progress_cb = progress_cb;
6027 cmp_arg.progress_arg = progress_arg;
6028 cmp_arg.merged_paths = merged_paths;
6029 err = merge_files(worktree, fileindex, fileindex_path,
6030 parent_commit_id, commit_id, repo, collect_merged_paths,
6031 &cmp_arg, cancel_cb, cancel_arg);
6033 got_ref_close(commit_ref);
6037 const struct got_error *
6038 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6039 struct got_worktree *worktree, struct got_fileindex *fileindex,
6040 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6041 struct got_repository *repo,
6042 got_worktree_checkout_cb progress_cb, void *progress_arg,
6043 got_cancel_cb cancel_cb, void *cancel_arg)
6045 const struct got_error *err;
6046 char *commit_ref_name;
6048 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6052 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6056 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6057 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6058 progress_arg, cancel_cb, cancel_arg);
6060 free(commit_ref_name);
6064 const struct got_error *
6065 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6066 struct got_worktree *worktree, struct got_fileindex *fileindex,
6067 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6068 struct got_repository *repo,
6069 got_worktree_checkout_cb progress_cb, void *progress_arg,
6070 got_cancel_cb cancel_cb, void *cancel_arg)
6072 const struct got_error *err;
6073 char *commit_ref_name;
6075 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6079 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6083 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6084 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6085 progress_arg, cancel_cb, cancel_arg);
6087 free(commit_ref_name);
6091 static const struct got_error *
6092 rebase_commit(struct got_object_id **new_commit_id,
6093 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6094 struct got_worktree *worktree, struct got_fileindex *fileindex,
6095 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6096 const char *new_logmsg, struct got_repository *repo)
6098 const struct got_error *err, *sync_err;
6099 struct got_pathlist_head commitable_paths;
6100 struct collect_commitables_arg cc_arg;
6101 char *fileindex_path = NULL;
6102 struct got_reference *head_ref = NULL;
6103 struct got_object_id *head_commit_id = NULL;
6104 char *logmsg = NULL;
6106 TAILQ_INIT(&commitable_paths);
6107 *new_commit_id = NULL;
6109 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6111 err = get_fileindex_path(&fileindex_path, worktree);
6115 cc_arg.commitable_paths = &commitable_paths;
6116 cc_arg.worktree = worktree;
6118 cc_arg.have_staged_files = 0;
6120 * If possible get the status of individual files directly to
6121 * avoid crawling the entire work tree once per rebased commit.
6122 * TODO: Ideally, merged_paths would contain a list of commitables
6123 * we could use so we could skip worktree_status() entirely.
6126 struct got_pathlist_entry *pe;
6127 TAILQ_FOREACH(pe, merged_paths, entry) {
6128 err = worktree_status(worktree, pe->path, fileindex,
6129 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6135 err = worktree_status(worktree, "", fileindex, repo,
6136 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6141 if (TAILQ_EMPTY(&commitable_paths)) {
6142 /* No-op change; commit will be elided. */
6143 err = got_ref_delete(commit_ref, repo);
6146 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6150 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6154 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6159 logmsg = strdup(new_logmsg);
6160 if (logmsg == NULL) {
6161 err = got_error_from_errno("strdup");
6165 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6170 /* NB: commit_worktree will call free(logmsg) */
6171 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6172 worktree, got_object_commit_get_author(orig_commit),
6173 got_object_commit_get_committer(orig_commit),
6174 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6178 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6182 err = got_ref_delete(commit_ref, repo);
6186 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
6188 sync_err = sync_fileindex(fileindex, fileindex_path);
6189 if (sync_err && err == NULL)
6192 free(fileindex_path);
6193 free(head_commit_id);
6195 got_ref_close(head_ref);
6197 free(*new_commit_id);
6198 *new_commit_id = NULL;
6203 const struct got_error *
6204 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6205 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6206 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6207 struct got_commit_object *orig_commit,
6208 struct got_object_id *orig_commit_id, struct got_repository *repo)
6210 const struct got_error *err;
6211 char *commit_ref_name;
6212 struct got_reference *commit_ref = NULL;
6213 struct got_object_id *commit_id = NULL;
6215 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6219 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6222 err = got_ref_resolve(&commit_id, repo, commit_ref);
6225 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6226 err = got_error(GOT_ERR_REBASE_COMMITID);
6230 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6231 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6234 got_ref_close(commit_ref);
6235 free(commit_ref_name);
6240 const struct got_error *
6241 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6242 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6243 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6244 struct got_commit_object *orig_commit,
6245 struct got_object_id *orig_commit_id, const char *new_logmsg,
6246 struct got_repository *repo)
6248 const struct got_error *err;
6249 char *commit_ref_name;
6250 struct got_reference *commit_ref = NULL;
6252 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6256 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6260 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6261 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6264 got_ref_close(commit_ref);
6265 free(commit_ref_name);
6269 const struct got_error *
6270 got_worktree_rebase_postpone(struct got_worktree *worktree,
6271 struct got_fileindex *fileindex)
6274 got_fileindex_free(fileindex);
6275 return lock_worktree(worktree, LOCK_SH);
6278 static const struct got_error *
6279 delete_ref(const char *name, struct got_repository *repo)
6281 const struct got_error *err;
6282 struct got_reference *ref;
6284 err = got_ref_open(&ref, repo, name, 0);
6286 if (err->code == GOT_ERR_NOT_REF)
6291 err = got_ref_delete(ref, repo);
6296 static const struct got_error *
6297 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6299 const struct got_error *err;
6300 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6301 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6303 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6306 err = delete_ref(tmp_branch_name, repo);
6310 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6313 err = delete_ref(new_base_branch_ref_name, repo);
6317 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6320 err = delete_ref(branch_ref_name, repo);
6324 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6327 err = delete_ref(commit_ref_name, repo);
6332 free(tmp_branch_name);
6333 free(new_base_branch_ref_name);
6334 free(branch_ref_name);
6335 free(commit_ref_name);
6339 const struct got_error *
6340 got_worktree_rebase_complete(struct got_worktree *worktree,
6341 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6342 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6343 struct got_repository *repo)
6345 const struct got_error *err, *unlockerr;
6346 struct got_object_id *new_head_commit_id = NULL;
6348 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6352 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6356 err = got_ref_write(rebased_branch, repo);
6360 err = got_worktree_set_head_ref(worktree, rebased_branch);
6364 err = delete_rebase_refs(worktree, repo);
6367 got_fileindex_free(fileindex);
6368 free(new_head_commit_id);
6369 unlockerr = lock_worktree(worktree, LOCK_SH);
6370 if (unlockerr && err == NULL)
6375 const struct got_error *
6376 got_worktree_rebase_abort(struct got_worktree *worktree,
6377 struct got_fileindex *fileindex, struct got_repository *repo,
6378 struct got_reference *new_base_branch,
6379 got_worktree_checkout_cb progress_cb, void *progress_arg)
6381 const struct got_error *err, *unlockerr, *sync_err;
6382 struct got_reference *resolved = NULL;
6383 struct got_object_id *commit_id = NULL;
6384 char *fileindex_path = NULL;
6385 struct revert_file_args rfa;
6386 struct got_object_id *tree_id = NULL;
6388 err = lock_worktree(worktree, LOCK_EX);
6392 err = got_ref_open(&resolved, repo,
6393 got_ref_get_symref_target(new_base_branch), 0);
6397 err = got_worktree_set_head_ref(worktree, resolved);
6402 * XXX commits to the base branch could have happened while
6403 * we were busy rebasing; should we store the original commit ID
6404 * when rebase begins and read it back here?
6406 err = got_ref_resolve(&commit_id, repo, resolved);
6410 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6414 err = got_object_id_by_path(&tree_id, repo,
6415 worktree->base_commit_id, worktree->path_prefix);
6419 err = delete_rebase_refs(worktree, repo);
6423 err = get_fileindex_path(&fileindex_path, worktree);
6427 rfa.worktree = worktree;
6428 rfa.fileindex = fileindex;
6429 rfa.progress_cb = progress_cb;
6430 rfa.progress_arg = progress_arg;
6431 rfa.patch_cb = NULL;
6432 rfa.patch_arg = NULL;
6434 err = worktree_status(worktree, "", fileindex, repo,
6435 revert_file, &rfa, NULL, NULL, 0, 0);
6439 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6440 repo, progress_cb, progress_arg, NULL, NULL);
6442 sync_err = sync_fileindex(fileindex, fileindex_path);
6443 if (sync_err && err == NULL)
6446 got_ref_close(resolved);
6450 got_fileindex_free(fileindex);
6451 free(fileindex_path);
6453 unlockerr = lock_worktree(worktree, LOCK_SH);
6454 if (unlockerr && err == NULL)
6459 const struct got_error *
6460 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6461 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6462 struct got_fileindex **fileindex, struct got_worktree *worktree,
6463 struct got_repository *repo)
6465 const struct got_error *err = NULL;
6466 char *tmp_branch_name = NULL;
6467 char *branch_ref_name = NULL;
6468 char *base_commit_ref_name = NULL;
6469 char *fileindex_path = NULL;
6470 struct check_rebase_ok_arg ok_arg;
6471 struct got_reference *wt_branch = NULL;
6472 struct got_reference *base_commit_ref = NULL;
6476 *base_commit_id = NULL;
6479 err = lock_worktree(worktree, LOCK_EX);
6483 err = open_fileindex(fileindex, &fileindex_path, worktree);
6487 ok_arg.worktree = worktree;
6489 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6494 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6498 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6502 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6507 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6512 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6516 err = got_ref_write(*branch_ref, repo);
6520 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6521 worktree->base_commit_id);
6524 err = got_ref_write(base_commit_ref, repo);
6527 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6528 if (*base_commit_id == NULL) {
6529 err = got_error_from_errno("got_object_id_dup");
6533 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6534 worktree->base_commit_id);
6537 err = got_ref_write(*tmp_branch, repo);
6541 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6545 free(fileindex_path);
6546 free(tmp_branch_name);
6547 free(branch_ref_name);
6548 free(base_commit_ref_name);
6550 got_ref_close(wt_branch);
6553 got_ref_close(*branch_ref);
6557 got_ref_close(*tmp_branch);
6560 free(*base_commit_id);
6562 got_fileindex_free(*fileindex);
6565 lock_worktree(worktree, LOCK_SH);
6570 const struct got_error *
6571 got_worktree_histedit_postpone(struct got_worktree *worktree,
6572 struct got_fileindex *fileindex)
6575 got_fileindex_free(fileindex);
6576 return lock_worktree(worktree, LOCK_SH);
6579 const struct got_error *
6580 got_worktree_histedit_in_progress(int *in_progress,
6581 struct got_worktree *worktree)
6583 const struct got_error *err;
6584 char *tmp_branch_name = NULL;
6586 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6590 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6591 free(tmp_branch_name);
6595 const struct got_error *
6596 got_worktree_histedit_continue(struct got_object_id **commit_id,
6597 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6598 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6599 struct got_worktree *worktree, struct got_repository *repo)
6601 const struct got_error *err;
6602 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6603 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6604 struct got_reference *commit_ref = NULL;
6605 struct got_reference *base_commit_ref = NULL;
6606 char *fileindex_path = NULL;
6607 int have_staged_files = 0;
6611 *base_commit_id = NULL;
6614 err = lock_worktree(worktree, LOCK_EX);
6618 err = open_fileindex(fileindex, &fileindex_path, worktree);
6622 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6623 &have_staged_files);
6624 if (err && err->code != GOT_ERR_CANCELLED)
6626 if (have_staged_files) {
6627 err = got_error(GOT_ERR_STAGED_PATHS);
6631 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6635 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6639 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6643 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6648 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6652 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6655 err = got_ref_resolve(commit_id, repo, commit_ref);
6659 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6662 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6666 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6670 free(commit_ref_name);
6671 free(branch_ref_name);
6672 free(fileindex_path);
6674 got_ref_close(commit_ref);
6675 if (base_commit_ref)
6676 got_ref_close(base_commit_ref);
6680 free(*base_commit_id);
6681 *base_commit_id = NULL;
6683 got_ref_close(*tmp_branch);
6687 got_fileindex_free(*fileindex);
6690 lock_worktree(worktree, LOCK_EX);
6695 static const struct got_error *
6696 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6698 const struct got_error *err;
6699 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6700 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6702 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6705 err = delete_ref(tmp_branch_name, repo);
6709 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6713 err = delete_ref(base_commit_ref_name, repo);
6717 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6720 err = delete_ref(branch_ref_name, repo);
6724 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6727 err = delete_ref(commit_ref_name, repo);
6731 free(tmp_branch_name);
6732 free(base_commit_ref_name);
6733 free(branch_ref_name);
6734 free(commit_ref_name);
6738 const struct got_error *
6739 got_worktree_histedit_abort(struct got_worktree *worktree,
6740 struct got_fileindex *fileindex, struct got_repository *repo,
6741 struct got_reference *branch, struct got_object_id *base_commit_id,
6742 got_worktree_checkout_cb progress_cb, void *progress_arg)
6744 const struct got_error *err, *unlockerr, *sync_err;
6745 struct got_reference *resolved = NULL;
6746 char *fileindex_path = NULL;
6747 struct got_object_id *tree_id = NULL;
6748 struct revert_file_args rfa;
6750 err = lock_worktree(worktree, LOCK_EX);
6754 err = got_ref_open(&resolved, repo,
6755 got_ref_get_symref_target(branch), 0);
6759 err = got_worktree_set_head_ref(worktree, resolved);
6763 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6767 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6768 worktree->path_prefix);
6772 err = delete_histedit_refs(worktree, repo);
6776 err = get_fileindex_path(&fileindex_path, worktree);
6780 rfa.worktree = worktree;
6781 rfa.fileindex = fileindex;
6782 rfa.progress_cb = progress_cb;
6783 rfa.progress_arg = progress_arg;
6784 rfa.patch_cb = NULL;
6785 rfa.patch_arg = NULL;
6787 err = worktree_status(worktree, "", fileindex, repo,
6788 revert_file, &rfa, NULL, NULL, 0, 0);
6792 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6793 repo, progress_cb, progress_arg, NULL, NULL);
6795 sync_err = sync_fileindex(fileindex, fileindex_path);
6796 if (sync_err && err == NULL)
6799 got_ref_close(resolved);
6801 free(fileindex_path);
6803 unlockerr = lock_worktree(worktree, LOCK_SH);
6804 if (unlockerr && err == NULL)
6809 const struct got_error *
6810 got_worktree_histedit_complete(struct got_worktree *worktree,
6811 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6812 struct got_reference *edited_branch, struct got_repository *repo)
6814 const struct got_error *err, *unlockerr;
6815 struct got_object_id *new_head_commit_id = NULL;
6816 struct got_reference *resolved = NULL;
6818 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6822 err = got_ref_open(&resolved, repo,
6823 got_ref_get_symref_target(edited_branch), 0);
6827 err = got_ref_change_ref(resolved, new_head_commit_id);
6831 err = got_ref_write(resolved, repo);
6835 err = got_worktree_set_head_ref(worktree, resolved);
6839 err = delete_histedit_refs(worktree, repo);
6842 got_fileindex_free(fileindex);
6843 free(new_head_commit_id);
6844 unlockerr = lock_worktree(worktree, LOCK_SH);
6845 if (unlockerr && err == NULL)
6850 const struct got_error *
6851 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6852 struct got_object_id *commit_id, struct got_repository *repo)
6854 const struct got_error *err;
6855 char *commit_ref_name;
6857 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6861 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6865 err = delete_ref(commit_ref_name, repo);
6867 free(commit_ref_name);
6871 const struct got_error *
6872 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6873 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6874 struct got_worktree *worktree, const char *refname,
6875 struct got_repository *repo)
6877 const struct got_error *err = NULL;
6878 char *fileindex_path = NULL;
6879 struct check_rebase_ok_arg ok_arg;
6883 *base_branch_ref = NULL;
6885 err = lock_worktree(worktree, LOCK_EX);
6889 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6890 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6891 "cannot integrate a branch into itself; "
6892 "update -b or different branch name required");
6896 err = open_fileindex(fileindex, &fileindex_path, worktree);
6900 /* Preconditions are the same as for rebase. */
6901 ok_arg.worktree = worktree;
6903 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6908 err = got_ref_open(branch_ref, repo, refname, 1);
6912 err = got_ref_open(base_branch_ref, repo,
6913 got_worktree_get_head_ref_name(worktree), 1);
6917 got_ref_close(*branch_ref);
6920 if (*base_branch_ref) {
6921 got_ref_close(*base_branch_ref);
6922 *base_branch_ref = NULL;
6925 got_fileindex_free(*fileindex);
6928 lock_worktree(worktree, LOCK_SH);
6933 const struct got_error *
6934 got_worktree_integrate_continue(struct got_worktree *worktree,
6935 struct got_fileindex *fileindex, struct got_repository *repo,
6936 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6937 got_worktree_checkout_cb progress_cb, void *progress_arg,
6938 got_cancel_cb cancel_cb, void *cancel_arg)
6940 const struct got_error *err = NULL, *sync_err, *unlockerr;
6941 char *fileindex_path = NULL;
6942 struct got_object_id *tree_id = NULL, *commit_id = NULL;
6944 err = get_fileindex_path(&fileindex_path, worktree);
6948 err = got_ref_resolve(&commit_id, repo, branch_ref);
6952 err = got_object_id_by_path(&tree_id, repo, commit_id,
6953 worktree->path_prefix);
6957 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6961 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6962 progress_cb, progress_arg, cancel_cb, cancel_arg);
6966 err = got_ref_change_ref(base_branch_ref, commit_id);
6970 err = got_ref_write(base_branch_ref, repo);
6972 sync_err = sync_fileindex(fileindex, fileindex_path);
6973 if (sync_err && err == NULL)
6977 unlockerr = got_ref_unlock(branch_ref);
6978 if (unlockerr && err == NULL)
6980 got_ref_close(branch_ref);
6982 unlockerr = got_ref_unlock(base_branch_ref);
6983 if (unlockerr && err == NULL)
6985 got_ref_close(base_branch_ref);
6987 got_fileindex_free(fileindex);
6988 free(fileindex_path);
6991 unlockerr = lock_worktree(worktree, LOCK_SH);
6992 if (unlockerr && err == NULL)
6997 const struct got_error *
6998 got_worktree_integrate_abort(struct got_worktree *worktree,
6999 struct got_fileindex *fileindex, struct got_repository *repo,
7000 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7002 const struct got_error *err = NULL, *unlockerr = NULL;
7004 got_fileindex_free(fileindex);
7006 err = lock_worktree(worktree, LOCK_SH);
7008 unlockerr = got_ref_unlock(branch_ref);
7009 if (unlockerr && err == NULL)
7011 got_ref_close(branch_ref);
7013 unlockerr = got_ref_unlock(base_branch_ref);
7014 if (unlockerr && err == NULL)
7016 got_ref_close(base_branch_ref);
7021 struct check_stage_ok_arg {
7022 struct got_object_id *head_commit_id;
7023 struct got_worktree *worktree;
7024 struct got_fileindex *fileindex;
7025 struct got_repository *repo;
7029 const struct got_error *
7030 check_stage_ok(void *arg, unsigned char status,
7031 unsigned char staged_status, const char *relpath,
7032 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7033 struct got_object_id *commit_id, int dirfd, const char *de_name)
7035 struct check_stage_ok_arg *a = arg;
7036 const struct got_error *err = NULL;
7037 struct got_fileindex_entry *ie;
7038 struct got_object_id base_commit_id;
7039 struct got_object_id *base_commit_idp = NULL;
7040 char *in_repo_path = NULL, *p;
7042 if (status == GOT_STATUS_UNVERSIONED ||
7043 status == GOT_STATUS_NO_CHANGE)
7045 if (status == GOT_STATUS_NONEXISTENT)
7046 return got_error_set_errno(ENOENT, relpath);
7048 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7050 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7052 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7053 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7055 return got_error_from_errno("asprintf");
7057 if (got_fileindex_entry_has_commit(ie)) {
7058 memcpy(base_commit_id.sha1, ie->commit_sha1,
7059 SHA1_DIGEST_LENGTH);
7060 base_commit_idp = &base_commit_id;
7063 if (status == GOT_STATUS_CONFLICT) {
7064 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7066 } else if (status != GOT_STATUS_ADD &&
7067 status != GOT_STATUS_MODIFY &&
7068 status != GOT_STATUS_DELETE) {
7069 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7073 a->have_changes = 1;
7078 err = check_out_of_date(p, status, staged_status,
7079 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7080 GOT_ERR_STAGE_OUT_OF_DATE);
7086 struct stage_path_arg {
7087 struct got_worktree *worktree;
7088 struct got_fileindex *fileindex;
7089 struct got_repository *repo;
7090 got_worktree_status_cb status_cb;
7092 got_worktree_patch_cb patch_cb;
7094 int staged_something;
7095 int allow_bad_symlinks;
7098 static const struct got_error *
7099 stage_path(void *arg, unsigned char status,
7100 unsigned char staged_status, const char *relpath,
7101 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7102 struct got_object_id *commit_id, int dirfd, const char *de_name)
7104 struct stage_path_arg *a = arg;
7105 const struct got_error *err = NULL;
7106 struct got_fileindex_entry *ie;
7107 char *ondisk_path = NULL, *path_content = NULL;
7109 struct got_object_id *new_staged_blob_id = NULL;
7112 if (status == GOT_STATUS_UNVERSIONED)
7115 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7117 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7119 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7121 return got_error_from_errno("asprintf");
7124 case GOT_STATUS_ADD:
7125 case GOT_STATUS_MODIFY:
7126 /* XXX could sb.st_mode be passed in by our caller? */
7127 if (lstat(ondisk_path, &sb) == -1) {
7128 err = got_error_from_errno2("lstat", ondisk_path);
7132 if (status == GOT_STATUS_ADD) {
7133 int choice = GOT_PATCH_CHOICE_NONE;
7134 err = (*a->patch_cb)(&choice, a->patch_arg,
7135 status, ie->path, NULL, 1, 1);
7138 if (choice != GOT_PATCH_CHOICE_YES)
7141 err = create_patched_content(&path_content, 0,
7142 staged_blob_id ? staged_blob_id : blob_id,
7143 ondisk_path, dirfd, de_name, ie->path,
7144 a->repo, a->patch_cb, a->patch_arg);
7145 if (err || path_content == NULL)
7149 err = got_object_blob_create(&new_staged_blob_id,
7150 path_content ? path_content : ondisk_path, a->repo);
7153 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7154 SHA1_DIGEST_LENGTH);
7155 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7156 stage = GOT_FILEIDX_STAGE_ADD;
7158 stage = GOT_FILEIDX_STAGE_MODIFY;
7159 got_fileindex_entry_stage_set(ie, stage);
7160 if (S_ISLNK(sb.st_mode)) {
7161 int is_bad_symlink = 0;
7162 if (!a->allow_bad_symlinks) {
7163 char target_path[PATH_MAX];
7165 target_len = readlink(ondisk_path, target_path,
7166 sizeof(target_path));
7167 if (target_len == -1) {
7168 err = got_error_from_errno2("readlink",
7172 err = is_bad_symlink_target(&is_bad_symlink,
7173 target_path, target_len, ondisk_path,
7174 a->worktree->root_path);
7177 if (is_bad_symlink) {
7178 err = got_error_path(ondisk_path,
7179 GOT_ERR_BAD_SYMLINK);
7184 got_fileindex_entry_staged_filetype_set(ie,
7185 GOT_FILEIDX_MODE_BAD_SYMLINK);
7187 got_fileindex_entry_staged_filetype_set(ie,
7188 GOT_FILEIDX_MODE_SYMLINK);
7190 got_fileindex_entry_staged_filetype_set(ie,
7191 GOT_FILEIDX_MODE_REGULAR_FILE);
7193 a->staged_something = 1;
7194 if (a->status_cb == NULL)
7196 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7197 get_staged_status(ie), relpath, blob_id,
7198 new_staged_blob_id, NULL, dirfd, de_name);
7200 case GOT_STATUS_DELETE:
7201 if (staged_status == GOT_STATUS_DELETE)
7204 int choice = GOT_PATCH_CHOICE_NONE;
7205 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7206 ie->path, NULL, 1, 1);
7209 if (choice == GOT_PATCH_CHOICE_NO)
7211 if (choice != GOT_PATCH_CHOICE_YES) {
7212 err = got_error(GOT_ERR_PATCH_CHOICE);
7216 stage = GOT_FILEIDX_STAGE_DELETE;
7217 got_fileindex_entry_stage_set(ie, stage);
7218 a->staged_something = 1;
7219 if (a->status_cb == NULL)
7221 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7222 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7225 case GOT_STATUS_NO_CHANGE:
7227 case GOT_STATUS_CONFLICT:
7228 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7230 case GOT_STATUS_NONEXISTENT:
7231 err = got_error_set_errno(ENOENT, relpath);
7234 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7238 if (path_content && unlink(path_content) == -1 && err == NULL)
7239 err = got_error_from_errno2("unlink", path_content);
7242 free(new_staged_blob_id);
7246 const struct got_error *
7247 got_worktree_stage(struct got_worktree *worktree,
7248 struct got_pathlist_head *paths,
7249 got_worktree_status_cb status_cb, void *status_arg,
7250 got_worktree_patch_cb patch_cb, void *patch_arg,
7251 int allow_bad_symlinks, struct got_repository *repo)
7253 const struct got_error *err = NULL, *sync_err, *unlockerr;
7254 struct got_pathlist_entry *pe;
7255 struct got_fileindex *fileindex = NULL;
7256 char *fileindex_path = NULL;
7257 struct got_reference *head_ref = NULL;
7258 struct got_object_id *head_commit_id = NULL;
7259 struct check_stage_ok_arg oka;
7260 struct stage_path_arg spa;
7262 err = lock_worktree(worktree, LOCK_EX);
7266 err = got_ref_open(&head_ref, repo,
7267 got_worktree_get_head_ref_name(worktree), 0);
7270 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7273 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7277 /* Check pre-conditions before staging anything. */
7278 oka.head_commit_id = head_commit_id;
7279 oka.worktree = worktree;
7280 oka.fileindex = fileindex;
7282 oka.have_changes = 0;
7283 TAILQ_FOREACH(pe, paths, entry) {
7284 err = worktree_status(worktree, pe->path, fileindex, repo,
7285 check_stage_ok, &oka, NULL, NULL, 0, 0);
7289 if (!oka.have_changes) {
7290 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7294 spa.worktree = worktree;
7295 spa.fileindex = fileindex;
7297 spa.patch_cb = patch_cb;
7298 spa.patch_arg = patch_arg;
7299 spa.status_cb = status_cb;
7300 spa.status_arg = status_arg;
7301 spa.staged_something = 0;
7302 spa.allow_bad_symlinks = allow_bad_symlinks;
7303 TAILQ_FOREACH(pe, paths, entry) {
7304 err = worktree_status(worktree, pe->path, fileindex, repo,
7305 stage_path, &spa, NULL, NULL, 0, 0);
7309 if (!spa.staged_something) {
7310 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7314 sync_err = sync_fileindex(fileindex, fileindex_path);
7315 if (sync_err && err == NULL)
7319 got_ref_close(head_ref);
7320 free(head_commit_id);
7321 free(fileindex_path);
7323 got_fileindex_free(fileindex);
7324 unlockerr = lock_worktree(worktree, LOCK_SH);
7325 if (unlockerr && err == NULL)
7330 struct unstage_path_arg {
7331 struct got_worktree *worktree;
7332 struct got_fileindex *fileindex;
7333 struct got_repository *repo;
7334 got_worktree_checkout_cb progress_cb;
7336 got_worktree_patch_cb patch_cb;
7340 static const struct got_error *
7341 create_unstaged_content(char **path_unstaged_content,
7342 char **path_new_staged_content, struct got_object_id *blob_id,
7343 struct got_object_id *staged_blob_id, const char *relpath,
7344 struct got_repository *repo,
7345 got_worktree_patch_cb patch_cb, void *patch_arg)
7347 const struct got_error *err;
7348 struct got_blob_object *blob = NULL, *staged_blob = NULL;
7349 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7350 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7351 struct stat sb1, sb2;
7352 struct got_diff_changes *changes = NULL;
7353 struct got_diff_state *ds = NULL;
7354 struct got_diff_args *args = NULL;
7355 struct got_diff_change *change;
7356 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
7357 int have_content = 0, have_rejected_content = 0;
7359 *path_unstaged_content = NULL;
7360 *path_new_staged_content = NULL;
7362 err = got_object_id_str(&label1, blob_id);
7365 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7369 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7373 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7377 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7381 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7385 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7389 if (stat(path1, &sb1) == -1) {
7390 err = got_error_from_errno2("stat", path1);
7394 if (stat(path2, &sb2) == -1) {
7395 err = got_error_from_errno2("stat", path2);
7399 err = got_diff_files(&changes, &ds, &args, &diff_flags,
7400 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
7404 err = got_opentemp_named(path_unstaged_content, &outfile,
7405 "got-unstaged-content");
7408 err = got_opentemp_named(path_new_staged_content, &rejectfile,
7409 "got-new-staged-content");
7413 if (fseek(f1, 0L, SEEK_SET) == -1) {
7414 err = got_ferror(f1, GOT_ERR_IO);
7417 if (fseek(f2, 0L, SEEK_SET) == -1) {
7418 err = got_ferror(f2, GOT_ERR_IO);
7421 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
7423 err = apply_or_reject_change(&choice, change, ++n,
7424 changes->nchanges, ds, args, diff_flags, relpath,
7425 f1, f2, &line_cur1, &line_cur2,
7426 outfile, rejectfile, patch_cb, patch_arg);
7429 if (choice == GOT_PATCH_CHOICE_YES)
7432 have_rejected_content = 1;
7433 if (choice == GOT_PATCH_CHOICE_QUIT)
7436 if (have_content || have_rejected_content)
7437 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7438 outfile, rejectfile);
7442 got_object_blob_close(blob);
7444 got_object_blob_close(staged_blob);
7445 if (f1 && fclose(f1) == EOF && err == NULL)
7446 err = got_error_from_errno2("fclose", path1);
7447 if (f2 && fclose(f2) == EOF && err == NULL)
7448 err = got_error_from_errno2("fclose", path2);
7449 if (outfile && fclose(outfile) == EOF && err == NULL)
7450 err = got_error_from_errno2("fclose", *path_unstaged_content);
7451 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7452 err = got_error_from_errno2("fclose", *path_new_staged_content);
7453 if (path1 && unlink(path1) == -1 && err == NULL)
7454 err = got_error_from_errno2("unlink", path1);
7455 if (path2 && unlink(path2) == -1 && err == NULL)
7456 err = got_error_from_errno2("unlink", path2);
7457 if (err || !have_content) {
7458 if (*path_unstaged_content &&
7459 unlink(*path_unstaged_content) == -1 && err == NULL)
7460 err = got_error_from_errno2("unlink",
7461 *path_unstaged_content);
7462 free(*path_unstaged_content);
7463 *path_unstaged_content = NULL;
7465 if (err || !have_content || !have_rejected_content) {
7466 if (*path_new_staged_content &&
7467 unlink(*path_new_staged_content) == -1 && err == NULL)
7468 err = got_error_from_errno2("unlink",
7469 *path_new_staged_content);
7470 free(*path_new_staged_content);
7471 *path_new_staged_content = NULL;
7475 got_diff_state_free(ds);
7479 got_diff_free_changes(changes);
7485 static const struct got_error *
7486 unstage_hunks(struct got_object_id *staged_blob_id,
7487 struct got_blob_object *blob_base,
7488 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
7489 const char *ondisk_path, const char *label_orig,
7490 struct got_worktree *worktree, struct got_repository *repo,
7491 got_worktree_patch_cb patch_cb, void *patch_arg,
7492 got_worktree_checkout_cb progress_cb, void *progress_arg)
7494 const struct got_error *err = NULL;
7495 char *path_unstaged_content = NULL;
7496 char *path_new_staged_content = NULL;
7497 struct got_object_id *new_staged_blob_id = NULL;
7501 err = create_unstaged_content(&path_unstaged_content,
7502 &path_new_staged_content, blob_id, staged_blob_id,
7503 ie->path, repo, patch_cb, patch_arg);
7507 if (path_unstaged_content == NULL)
7510 if (path_new_staged_content) {
7511 err = got_object_blob_create(&new_staged_blob_id,
7512 path_new_staged_content, repo);
7517 f = fopen(path_unstaged_content, "r");
7519 err = got_error_from_errno2("fopen",
7520 path_unstaged_content);
7523 if (fstat(fileno(f), &sb) == -1) {
7524 err = got_error_from_errno2("fstat", path_unstaged_content);
7527 if (got_fileindex_entry_staged_filetype_get(ie) ==
7528 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
7529 char link_target[PATH_MAX];
7531 r = fread(link_target, 1, sizeof(link_target), f);
7532 if (r == 0 && ferror(f)) {
7533 err = got_error_from_errno("fread");
7536 if (r >= sizeof(link_target)) { /* should not happen */
7537 err = got_error(GOT_ERR_NO_SPACE);
7540 link_target[r] = '\0';
7541 err = merge_symlink(worktree, blob_base,
7542 ondisk_path, ie->path, label_orig, link_target,
7543 worktree->base_commit_id, repo, progress_cb,
7546 int local_changes_subsumed;
7547 err = merge_file(&local_changes_subsumed, worktree,
7548 blob_base, ondisk_path, ie->path,
7549 got_fileindex_perms_to_st(ie),
7550 path_unstaged_content, label_orig, "unstaged",
7551 repo, progress_cb, progress_arg);
7556 if (new_staged_blob_id) {
7557 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7558 SHA1_DIGEST_LENGTH);
7560 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7562 free(new_staged_blob_id);
7563 if (path_unstaged_content &&
7564 unlink(path_unstaged_content) == -1 && err == NULL)
7565 err = got_error_from_errno2("unlink", path_unstaged_content);
7566 if (path_new_staged_content &&
7567 unlink(path_new_staged_content) == -1 && err == NULL)
7568 err = got_error_from_errno2("unlink", path_new_staged_content);
7569 if (f && fclose(f) != 0 && err == NULL)
7570 err = got_error_from_errno2("fclose", path_unstaged_content);
7571 free(path_unstaged_content);
7572 free(path_new_staged_content);
7576 static const struct got_error *
7577 unstage_path(void *arg, unsigned char status,
7578 unsigned char staged_status, const char *relpath,
7579 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7580 struct got_object_id *commit_id, int dirfd, const char *de_name)
7582 const struct got_error *err = NULL;
7583 struct unstage_path_arg *a = arg;
7584 struct got_fileindex_entry *ie;
7585 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7586 char *ondisk_path = NULL;
7587 char *id_str = NULL, *label_orig = NULL;
7588 int local_changes_subsumed;
7591 if (staged_status != GOT_STATUS_ADD &&
7592 staged_status != GOT_STATUS_MODIFY &&
7593 staged_status != GOT_STATUS_DELETE)
7596 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7598 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7600 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7602 return got_error_from_errno("asprintf");
7604 err = got_object_id_str(&id_str,
7605 commit_id ? commit_id : a->worktree->base_commit_id);
7608 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7610 err = got_error_from_errno("asprintf");
7614 switch (staged_status) {
7615 case GOT_STATUS_MODIFY:
7616 err = got_object_open_as_blob(&blob_base, a->repo,
7621 case GOT_STATUS_ADD:
7623 if (staged_status == GOT_STATUS_ADD) {
7624 int choice = GOT_PATCH_CHOICE_NONE;
7625 err = (*a->patch_cb)(&choice, a->patch_arg,
7626 staged_status, ie->path, NULL, 1, 1);
7629 if (choice != GOT_PATCH_CHOICE_YES)
7632 err = unstage_hunks(staged_blob_id,
7633 blob_base, blob_id, ie, ondisk_path,
7634 label_orig, a->worktree, a->repo,
7635 a->patch_cb, a->patch_arg,
7636 a->progress_cb, a->progress_arg);
7637 break; /* Done with this file. */
7640 err = got_object_open_as_blob(&blob_staged, a->repo,
7641 staged_blob_id, 8192);
7644 switch (got_fileindex_entry_staged_filetype_get(ie)) {
7645 case GOT_FILEIDX_MODE_BAD_SYMLINK:
7646 case GOT_FILEIDX_MODE_REGULAR_FILE:
7647 err = merge_blob(&local_changes_subsumed, a->worktree,
7648 blob_base, ondisk_path, relpath,
7649 got_fileindex_perms_to_st(ie), label_orig,
7650 blob_staged, commit_id ? commit_id :
7651 a->worktree->base_commit_id, a->repo,
7652 a->progress_cb, a->progress_arg);
7654 case GOT_FILEIDX_MODE_SYMLINK:
7655 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7656 char *staged_target;
7657 err = got_object_blob_read_to_str(
7658 &staged_target, blob_staged);
7661 err = merge_symlink(a->worktree, blob_base,
7662 ondisk_path, relpath, label_orig,
7663 staged_target, commit_id ? commit_id :
7664 a->worktree->base_commit_id,
7665 a->repo, a->progress_cb, a->progress_arg);
7666 free(staged_target);
7668 err = merge_blob(&local_changes_subsumed,
7669 a->worktree, blob_base, ondisk_path,
7670 relpath, got_fileindex_perms_to_st(ie),
7671 label_orig, blob_staged,
7672 commit_id ? commit_id :
7673 a->worktree->base_commit_id, a->repo,
7674 a->progress_cb, a->progress_arg);
7678 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
7682 got_fileindex_entry_stage_set(ie,
7683 GOT_FILEIDX_STAGE_NONE);
7685 case GOT_STATUS_DELETE:
7687 int choice = GOT_PATCH_CHOICE_NONE;
7688 err = (*a->patch_cb)(&choice, a->patch_arg,
7689 staged_status, ie->path, NULL, 1, 1);
7692 if (choice == GOT_PATCH_CHOICE_NO)
7694 if (choice != GOT_PATCH_CHOICE_YES) {
7695 err = got_error(GOT_ERR_PATCH_CHOICE);
7699 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7700 err = get_file_status(&status, &sb, ie, ondisk_path,
7701 dirfd, de_name, a->repo);
7704 err = (*a->progress_cb)(a->progress_arg, status, relpath);
7710 got_object_blob_close(blob_base);
7712 got_object_blob_close(blob_staged);
7718 const struct got_error *
7719 got_worktree_unstage(struct got_worktree *worktree,
7720 struct got_pathlist_head *paths,
7721 got_worktree_checkout_cb progress_cb, void *progress_arg,
7722 got_worktree_patch_cb patch_cb, void *patch_arg,
7723 struct got_repository *repo)
7725 const struct got_error *err = NULL, *sync_err, *unlockerr;
7726 struct got_pathlist_entry *pe;
7727 struct got_fileindex *fileindex = NULL;
7728 char *fileindex_path = NULL;
7729 struct unstage_path_arg upa;
7731 err = lock_worktree(worktree, LOCK_EX);
7735 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7739 upa.worktree = worktree;
7740 upa.fileindex = fileindex;
7742 upa.progress_cb = progress_cb;
7743 upa.progress_arg = progress_arg;
7744 upa.patch_cb = patch_cb;
7745 upa.patch_arg = patch_arg;
7746 TAILQ_FOREACH(pe, paths, entry) {
7747 err = worktree_status(worktree, pe->path, fileindex, repo,
7748 unstage_path, &upa, NULL, NULL, 0, 0);
7753 sync_err = sync_fileindex(fileindex, fileindex_path);
7754 if (sync_err && err == NULL)
7757 free(fileindex_path);
7759 got_fileindex_free(fileindex);
7760 unlockerr = lock_worktree(worktree, LOCK_SH);
7761 if (unlockerr && err == NULL)
7766 struct report_file_info_arg {
7767 struct got_worktree *worktree;
7768 got_worktree_path_info_cb info_cb;
7770 struct got_pathlist_head *paths;
7771 got_cancel_cb cancel_cb;
7775 static const struct got_error *
7776 report_file_info(void *arg, struct got_fileindex_entry *ie)
7778 struct report_file_info_arg *a = arg;
7779 struct got_pathlist_entry *pe;
7780 struct got_object_id blob_id, staged_blob_id, commit_id;
7781 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
7782 struct got_object_id *commit_idp = NULL;
7785 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
7786 return got_error(GOT_ERR_CANCELLED);
7788 TAILQ_FOREACH(pe, a->paths, entry) {
7789 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
7790 got_path_is_child(ie->path, pe->path, pe->path_len))
7793 if (pe == NULL) /* not found */
7796 if (got_fileindex_entry_has_blob(ie)) {
7797 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
7798 blob_idp = &blob_id;
7800 stage = got_fileindex_entry_stage_get(ie);
7801 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
7802 stage == GOT_FILEIDX_STAGE_ADD) {
7803 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
7804 SHA1_DIGEST_LENGTH);
7805 staged_blob_idp = &staged_blob_id;
7808 if (got_fileindex_entry_has_commit(ie)) {
7809 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
7810 commit_idp = &commit_id;
7813 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
7814 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
7817 const struct got_error *
7818 got_worktree_path_info(struct got_worktree *worktree,
7819 struct got_pathlist_head *paths,
7820 got_worktree_path_info_cb info_cb, void *info_arg,
7821 got_cancel_cb cancel_cb, void *cancel_arg)
7824 const struct got_error *err = NULL, *unlockerr;
7825 struct got_fileindex *fileindex = NULL;
7826 char *fileindex_path = NULL;
7827 struct report_file_info_arg arg;
7829 err = lock_worktree(worktree, LOCK_SH);
7833 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7837 arg.worktree = worktree;
7838 arg.info_cb = info_cb;
7839 arg.info_arg = info_arg;
7841 arg.cancel_cb = cancel_cb;
7842 arg.cancel_arg = cancel_arg;
7843 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
7846 free(fileindex_path);
7848 got_fileindex_free(fileindex);
7849 unlockerr = lock_worktree(worktree, LOCK_UN);
7850 if (unlockerr && err == NULL)