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");
1789 *status = GOT_STATUS_MODIFY;
1791 } else if (flen == 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 case GOT_STATUS_MODIFY:
2804 case GOT_STATUS_CONFLICT:
2805 err = (*a->progress_cb)(a->progress_arg,
2806 GOT_STATUS_CANNOT_DELETE, path1);
2810 case GOT_STATUS_OBSTRUCTED:
2811 err = (*a->progress_cb)(a->progress_arg, status, path1);
2819 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2821 return got_error_from_errno("asprintf");
2822 ie = got_fileindex_entry_get(a->fileindex, path2,
2825 err = get_file_status(&status, &sb, ie, ondisk_path,
2829 if (status != GOT_STATUS_NO_CHANGE &&
2830 status != GOT_STATUS_MODIFY &&
2831 status != GOT_STATUS_CONFLICT &&
2832 status != GOT_STATUS_ADD) {
2833 err = (*a->progress_cb)(a->progress_arg,
2837 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2839 err = got_object_blob_read_to_str(&link_target2,
2843 err = merge_symlink(a->worktree, NULL,
2844 ondisk_path, path2, a->label_orig,
2845 link_target2, a->commit_id2, repo,
2846 a->progress_cb, a->progress_arg);
2848 } else if (S_ISREG(sb.st_mode)) {
2849 err = merge_blob(&local_changes_subsumed,
2850 a->worktree, NULL, ondisk_path, path2,
2851 sb.st_mode, a->label_orig, blob2,
2852 a->commit_id2, repo, a->progress_cb,
2855 err = got_error_path(ondisk_path,
2856 GOT_ERR_FILE_OBSTRUCTED);
2860 if (status == GOT_STATUS_DELETE) {
2861 err = got_fileindex_entry_update(ie,
2862 ondisk_path, blob2->id.sha1,
2863 a->worktree->base_commit_id->sha1, 0);
2868 int is_bad_symlink = 0;
2869 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2870 if (S_ISLNK(mode2)) {
2871 err = install_symlink(&is_bad_symlink,
2872 a->worktree, ondisk_path, path2, blob2, 0,
2873 0, 1, repo, a->progress_cb, a->progress_arg);
2875 err = install_blob(a->worktree, ondisk_path, path2,
2876 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2877 a->progress_cb, a->progress_arg);
2881 err = got_fileindex_entry_alloc(&ie, path2);
2884 err = got_fileindex_entry_update(ie, ondisk_path,
2887 got_fileindex_entry_free(ie);
2890 err = got_fileindex_entry_add(a->fileindex, ie);
2892 got_fileindex_entry_free(ie);
2895 if (is_bad_symlink) {
2896 got_fileindex_entry_filetype_set(ie,
2897 GOT_FILEIDX_MODE_BAD_SYMLINK);
2906 struct check_merge_ok_arg {
2907 struct got_worktree *worktree;
2908 struct got_repository *repo;
2911 static const struct got_error *
2912 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2914 const struct got_error *err = NULL;
2915 struct check_merge_ok_arg *a = arg;
2916 unsigned char status;
2920 /* Reject merges into a work tree with mixed base commits. */
2921 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2922 SHA1_DIGEST_LENGTH))
2923 return got_error(GOT_ERR_MIXED_COMMITS);
2925 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2927 return got_error_from_errno("asprintf");
2929 /* Reject merges into a work tree with conflicted files. */
2930 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2933 if (status == GOT_STATUS_CONFLICT)
2934 return got_error(GOT_ERR_CONFLICTS);
2939 static const struct got_error *
2940 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2941 const char *fileindex_path, struct got_object_id *commit_id1,
2942 struct got_object_id *commit_id2, struct got_repository *repo,
2943 got_worktree_checkout_cb progress_cb, void *progress_arg,
2944 got_cancel_cb cancel_cb, void *cancel_arg)
2946 const struct got_error *err = NULL, *sync_err;
2947 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2948 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2949 struct merge_file_cb_arg arg;
2950 char *label_orig = NULL;
2953 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2954 worktree->path_prefix);
2955 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
2961 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2965 err = got_object_id_str(&id_str, commit_id1);
2969 if (asprintf(&label_orig, "%s: commit %s",
2970 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2971 err = got_error_from_errno("asprintf");
2978 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2979 worktree->path_prefix);
2983 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2987 arg.worktree = worktree;
2988 arg.fileindex = fileindex;
2989 arg.progress_cb = progress_cb;
2990 arg.progress_arg = progress_arg;
2991 arg.cancel_cb = cancel_cb;
2992 arg.cancel_arg = cancel_arg;
2993 arg.label_orig = label_orig;
2994 arg.commit_id2 = commit_id2;
2995 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2996 sync_err = sync_fileindex(fileindex, fileindex_path);
2997 if (sync_err && err == NULL)
3001 got_object_tree_close(tree1);
3003 got_object_tree_close(tree2);
3008 const struct got_error *
3009 got_worktree_merge_files(struct got_worktree *worktree,
3010 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3011 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3012 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3014 const struct got_error *err, *unlockerr;
3015 char *fileindex_path = NULL;
3016 struct got_fileindex *fileindex = NULL;
3017 struct check_merge_ok_arg mok_arg;
3019 err = lock_worktree(worktree, LOCK_EX);
3023 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3027 mok_arg.worktree = worktree;
3028 mok_arg.repo = repo;
3029 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
3034 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3035 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
3038 got_fileindex_free(fileindex);
3039 free(fileindex_path);
3040 unlockerr = lock_worktree(worktree, LOCK_SH);
3041 if (unlockerr && err == NULL)
3046 struct diff_dir_cb_arg {
3047 struct got_fileindex *fileindex;
3048 struct got_worktree *worktree;
3049 const char *status_path;
3050 size_t status_path_len;
3051 struct got_repository *repo;
3052 got_worktree_status_cb status_cb;
3054 got_cancel_cb cancel_cb;
3056 /* A pathlist containing per-directory pathlists of ignore patterns. */
3057 struct got_pathlist_head ignores;
3058 int report_unchanged;
3062 static const struct got_error *
3063 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3064 int dirfd, const char *de_name,
3065 got_worktree_status_cb status_cb, void *status_arg,
3066 struct got_repository *repo, int report_unchanged)
3068 const struct got_error *err = NULL;
3069 unsigned char status = GOT_STATUS_NO_CHANGE;
3070 unsigned char staged_status = get_staged_status(ie);
3072 struct got_object_id blob_id, commit_id, staged_blob_id;
3073 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3074 struct got_object_id *staged_blob_idp = NULL;
3076 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3080 if (status == GOT_STATUS_NO_CHANGE &&
3081 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3084 if (got_fileindex_entry_has_blob(ie)) {
3085 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3086 blob_idp = &blob_id;
3088 if (got_fileindex_entry_has_commit(ie)) {
3089 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3090 commit_idp = &commit_id;
3092 if (staged_status == GOT_STATUS_ADD ||
3093 staged_status == GOT_STATUS_MODIFY) {
3094 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3095 SHA1_DIGEST_LENGTH);
3096 staged_blob_idp = &staged_blob_id;
3099 return (*status_cb)(status_arg, status, staged_status,
3100 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3103 static const struct got_error *
3104 status_old_new(void *arg, struct got_fileindex_entry *ie,
3105 struct dirent *de, const char *parent_path, int dirfd)
3107 const struct got_error *err = NULL;
3108 struct diff_dir_cb_arg *a = arg;
3111 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3112 return got_error(GOT_ERR_CANCELLED);
3114 if (got_path_cmp(parent_path, a->status_path,
3115 strlen(parent_path), a->status_path_len) != 0 &&
3116 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3119 if (parent_path[0]) {
3120 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3121 parent_path, de->d_name) == -1)
3122 return got_error_from_errno("asprintf");
3124 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3126 return got_error_from_errno("asprintf");
3129 err = report_file_status(ie, abspath, dirfd, de->d_name,
3130 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3135 static const struct got_error *
3136 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3138 struct diff_dir_cb_arg *a = arg;
3139 struct got_object_id blob_id, commit_id;
3140 unsigned char status;
3142 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3143 return got_error(GOT_ERR_CANCELLED);
3145 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3148 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3149 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3150 if (got_fileindex_entry_has_file_on_disk(ie))
3151 status = GOT_STATUS_MISSING;
3153 status = GOT_STATUS_DELETE;
3154 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3155 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3159 free_ignorelist(struct got_pathlist_head *ignorelist)
3161 struct got_pathlist_entry *pe;
3163 TAILQ_FOREACH(pe, ignorelist, entry)
3164 free((char *)pe->path);
3165 got_pathlist_free(ignorelist);
3169 free_ignores(struct got_pathlist_head *ignores)
3171 struct got_pathlist_entry *pe;
3173 TAILQ_FOREACH(pe, ignores, entry) {
3174 struct got_pathlist_head *ignorelist = pe->data;
3175 free_ignorelist(ignorelist);
3176 free((char *)pe->path);
3178 got_pathlist_free(ignores);
3181 static const struct got_error *
3182 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3184 const struct got_error *err = NULL;
3185 struct got_pathlist_entry *pe = NULL;
3186 struct got_pathlist_head *ignorelist;
3187 char *line = NULL, *pattern, *dirpath = NULL;
3188 size_t linesize = 0;
3191 ignorelist = calloc(1, sizeof(*ignorelist));
3192 if (ignorelist == NULL)
3193 return got_error_from_errno("calloc");
3194 TAILQ_INIT(ignorelist);
3196 while ((linelen = getline(&line, &linesize, f)) != -1) {
3197 if (linelen > 0 && line[linelen - 1] == '\n')
3198 line[linelen - 1] = '\0';
3200 /* Git's ignores may contain comments. */
3204 /* Git's negated patterns are not (yet?) supported. */
3208 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3210 err = got_error_from_errno("asprintf");
3213 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3218 err = got_error_from_errno("getline");
3222 dirpath = strdup(path);
3223 if (dirpath == NULL) {
3224 err = got_error_from_errno("strdup");
3227 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3230 if (err || pe == NULL) {
3232 free_ignorelist(ignorelist);
3238 match_ignores(struct got_pathlist_head *ignores, const char *path)
3240 struct got_pathlist_entry *pe;
3242 /* Handle patterns which match in all directories. */
3243 TAILQ_FOREACH(pe, ignores, entry) {
3244 struct got_pathlist_head *ignorelist = pe->data;
3245 struct got_pathlist_entry *pi;
3247 TAILQ_FOREACH(pi, ignorelist, entry) {
3248 const char *p, *pattern = pi->path;
3250 if (strncmp(pattern, "**/", 3) != 0)
3255 if (fnmatch(pattern, p,
3256 FNM_PATHNAME | FNM_LEADING_DIR)) {
3257 /* Retry in next directory. */
3258 while (*p && *p != '/')
3270 * The ignores pathlist contains ignore lists from children before
3271 * parents, so we can find the most specific ignorelist by walking
3272 * ignores backwards.
3274 pe = TAILQ_LAST(ignores, got_pathlist_head);
3276 if (got_path_is_child(path, pe->path, pe->path_len)) {
3277 struct got_pathlist_head *ignorelist = pe->data;
3278 struct got_pathlist_entry *pi;
3279 TAILQ_FOREACH(pi, ignorelist, entry) {
3280 const char *pattern = pi->path;
3281 int flags = FNM_LEADING_DIR;
3282 if (strstr(pattern, "/**/") == NULL)
3283 flags |= FNM_PATHNAME;
3284 if (fnmatch(pattern, path, flags))
3289 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3295 static const struct got_error *
3296 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3297 const char *path, int dirfd, const char *ignores_filename)
3299 const struct got_error *err = NULL;
3302 FILE *ignoresfile = NULL;
3304 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3305 path[0] ? "/" : "", ignores_filename) == -1)
3306 return got_error_from_errno("asprintf");
3309 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3311 if (errno != ENOENT && errno != EACCES)
3312 err = got_error_from_errno2("openat",
3315 ignoresfile = fdopen(fd, "r");
3316 if (ignoresfile == NULL)
3317 err = got_error_from_errno2("fdopen",
3321 err = read_ignores(ignores, path, ignoresfile);
3325 ignoresfile = fopen(ignorespath, "r");
3326 if (ignoresfile == NULL) {
3327 if (errno != ENOENT && errno != EACCES)
3328 err = got_error_from_errno2("fopen",
3331 err = read_ignores(ignores, path, ignoresfile);
3334 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3335 err = got_error_from_errno2("fclose", path);
3336 if (fd != -1 && close(fd) == -1 && err == NULL)
3337 err = got_error_from_errno2("close", path);
3342 static const struct got_error *
3343 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3345 const struct got_error *err = NULL;
3346 struct diff_dir_cb_arg *a = arg;
3349 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3350 return got_error(GOT_ERR_CANCELLED);
3352 if (parent_path[0]) {
3353 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3354 return got_error_from_errno("asprintf");
3359 if (de->d_type != DT_DIR &&
3360 got_path_is_child(path, a->status_path, a->status_path_len)
3361 && !match_ignores(&a->ignores, path))
3362 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3363 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3369 static const struct got_error *
3370 status_traverse(void *arg, const char *path, int dirfd)
3372 const struct got_error *err = NULL;
3373 struct diff_dir_cb_arg *a = arg;
3378 err = add_ignores(&a->ignores, a->worktree->root_path,
3379 path, dirfd, ".cvsignore");
3383 err = add_ignores(&a->ignores, a->worktree->root_path, path,
3384 dirfd, ".gitignore");
3389 static const struct got_error *
3390 report_single_file_status(const char *path, const char *ondisk_path,
3391 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3392 void *status_arg, struct got_repository *repo, int report_unchanged)
3394 struct got_fileindex_entry *ie;
3397 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3399 return report_file_status(ie, ondisk_path, -1, NULL,
3400 status_cb, status_arg, repo, report_unchanged);
3402 if (lstat(ondisk_path, &sb) == -1) {
3403 if (errno != ENOENT)
3404 return got_error_from_errno2("lstat", ondisk_path);
3405 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3406 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3410 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3411 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3412 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3417 static const struct got_error *
3418 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3419 const char *root_path, const char *path)
3421 const struct got_error *err;
3422 char *parent_path, *next_parent_path = NULL;
3424 err = add_ignores(ignores, root_path, "", -1,
3429 err = add_ignores(ignores, root_path, "", -1,
3434 err = got_path_dirname(&parent_path, path);
3436 if (err->code == GOT_ERR_BAD_PATH)
3437 return NULL; /* cannot traverse parent */
3441 err = add_ignores(ignores, root_path, parent_path, -1,
3445 err = add_ignores(ignores, root_path, parent_path, -1,
3449 err = got_path_dirname(&next_parent_path, parent_path);
3451 if (err->code == GOT_ERR_BAD_PATH)
3452 err = NULL; /* traversed everything */
3456 parent_path = next_parent_path;
3457 next_parent_path = NULL;
3461 free(next_parent_path);
3465 static const struct got_error *
3466 worktree_status(struct got_worktree *worktree, const char *path,
3467 struct got_fileindex *fileindex, struct got_repository *repo,
3468 got_worktree_status_cb status_cb, void *status_arg,
3469 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3470 int report_unchanged)
3472 const struct got_error *err = NULL;
3474 struct got_fileindex_diff_dir_cb fdiff_cb;
3475 struct diff_dir_cb_arg arg;
3476 char *ondisk_path = NULL;
3478 TAILQ_INIT(&arg.ignores);
3480 if (asprintf(&ondisk_path, "%s%s%s",
3481 worktree->root_path, path[0] ? "/" : "", path) == -1)
3482 return got_error_from_errno("asprintf");
3484 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3486 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3488 err = got_error_from_errno2("open", ondisk_path);
3490 err = report_single_file_status(path, ondisk_path,
3491 fileindex, status_cb, status_arg, repo,
3494 fdiff_cb.diff_old_new = status_old_new;
3495 fdiff_cb.diff_old = status_old;
3496 fdiff_cb.diff_new = status_new;
3497 fdiff_cb.diff_traverse = status_traverse;
3498 arg.fileindex = fileindex;
3499 arg.worktree = worktree;
3500 arg.status_path = path;
3501 arg.status_path_len = strlen(path);
3503 arg.status_cb = status_cb;
3504 arg.status_arg = status_arg;
3505 arg.cancel_cb = cancel_cb;
3506 arg.cancel_arg = cancel_arg;
3507 arg.report_unchanged = report_unchanged;
3508 arg.no_ignores = no_ignores;
3510 err = add_ignores_from_parent_paths(&arg.ignores,
3511 worktree->root_path, path);
3515 err = got_fileindex_diff_dir(fileindex, fd,
3516 worktree->root_path, path, repo, &fdiff_cb, &arg);
3519 free_ignores(&arg.ignores);
3520 if (fd != -1 && close(fd) != 0 && err == NULL)
3521 err = got_error_from_errno("close");
3526 const struct got_error *
3527 got_worktree_status(struct got_worktree *worktree,
3528 struct got_pathlist_head *paths, struct got_repository *repo,
3529 got_worktree_status_cb status_cb, void *status_arg,
3530 got_cancel_cb cancel_cb, void *cancel_arg)
3532 const struct got_error *err = NULL;
3533 char *fileindex_path = NULL;
3534 struct got_fileindex *fileindex = NULL;
3535 struct got_pathlist_entry *pe;
3537 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3541 TAILQ_FOREACH(pe, paths, entry) {
3542 err = worktree_status(worktree, pe->path, fileindex, repo,
3543 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3547 free(fileindex_path);
3548 got_fileindex_free(fileindex);
3552 const struct got_error *
3553 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3556 const struct got_error *err = NULL;
3557 char *resolved = NULL, *cwd = NULL, *path = NULL;
3563 cwd = getcwd(NULL, 0);
3565 return got_error_from_errno("getcwd");
3567 if (lstat(arg, &sb) == -1) {
3568 if (errno != ENOENT) {
3569 err = got_error_from_errno2("lstat", arg);
3573 if (S_ISLNK(sb.st_mode)) {
3575 * We cannot use realpath(3) with symlinks since we want to
3576 * operate on the symlink itself.
3577 * But we can make the path absolute, assuming it is relative
3578 * to the current working directory, and then canonicalize it.
3580 char *abspath = NULL;
3581 char canonpath[PATH_MAX];
3582 if (!got_path_is_absolute(arg)) {
3583 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3584 err = got_error_from_errno("asprintf");
3589 err = got_canonpath(abspath ? abspath : arg, canonpath,
3593 resolved = strdup(canonpath);
3594 if (resolved == NULL) {
3595 err = got_error_from_errno("strdup");
3599 resolved = realpath(arg, NULL);
3600 if (resolved == NULL) {
3601 if (errno != ENOENT) {
3602 err = got_error_from_errno2("realpath", arg);
3605 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
3606 err = got_error_from_errno("asprintf");
3612 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3613 strlen(got_worktree_get_root_path(worktree)))) {
3614 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3618 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3619 err = got_path_skip_common_ancestor(&path,
3620 got_worktree_get_root_path(worktree), resolved);
3626 err = got_error_from_errno("strdup");
3631 /* XXX status walk can't deal with trailing slash! */
3633 while (len > 0 && path[len - 1] == '/') {
3634 path[len - 1] = '\0';
3647 struct schedule_addition_args {
3648 struct got_worktree *worktree;
3649 struct got_fileindex *fileindex;
3650 got_worktree_checkout_cb progress_cb;
3652 struct got_repository *repo;
3655 static const struct got_error *
3656 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3657 const char *relpath, struct got_object_id *blob_id,
3658 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3659 int dirfd, const char *de_name)
3661 struct schedule_addition_args *a = arg;
3662 const struct got_error *err = NULL;
3663 struct got_fileindex_entry *ie;
3667 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3669 return got_error_from_errno("asprintf");
3671 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3673 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3677 /* Re-adding an existing entry is a no-op. */
3678 if (status == GOT_STATUS_ADD)
3680 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3685 if (status != GOT_STATUS_UNVERSIONED) {
3686 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3690 err = got_fileindex_entry_alloc(&ie, relpath);
3693 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3695 got_fileindex_entry_free(ie);
3698 err = got_fileindex_entry_add(a->fileindex, ie);
3700 got_fileindex_entry_free(ie);
3707 if (status == GOT_STATUS_ADD)
3709 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3712 const struct got_error *
3713 got_worktree_schedule_add(struct got_worktree *worktree,
3714 struct got_pathlist_head *paths,
3715 got_worktree_checkout_cb progress_cb, void *progress_arg,
3716 struct got_repository *repo, int no_ignores)
3718 struct got_fileindex *fileindex = NULL;
3719 char *fileindex_path = NULL;
3720 const struct got_error *err = NULL, *sync_err, *unlockerr;
3721 struct got_pathlist_entry *pe;
3722 struct schedule_addition_args saa;
3724 err = lock_worktree(worktree, LOCK_EX);
3728 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3732 saa.worktree = worktree;
3733 saa.fileindex = fileindex;
3734 saa.progress_cb = progress_cb;
3735 saa.progress_arg = progress_arg;
3738 TAILQ_FOREACH(pe, paths, entry) {
3739 err = worktree_status(worktree, pe->path, fileindex, repo,
3740 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3744 sync_err = sync_fileindex(fileindex, fileindex_path);
3745 if (sync_err && err == NULL)
3748 free(fileindex_path);
3750 got_fileindex_free(fileindex);
3751 unlockerr = lock_worktree(worktree, LOCK_SH);
3752 if (unlockerr && err == NULL)
3757 struct schedule_deletion_args {
3758 struct got_worktree *worktree;
3759 struct got_fileindex *fileindex;
3760 got_worktree_delete_cb progress_cb;
3762 struct got_repository *repo;
3763 int delete_local_mods;
3765 const char *status_codes;
3768 static const struct got_error *
3769 schedule_for_deletion(void *arg, unsigned char status,
3770 unsigned char staged_status, const char *relpath,
3771 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3772 struct got_object_id *commit_id, int dirfd, const char *de_name)
3774 struct schedule_deletion_args *a = arg;
3775 const struct got_error *err = NULL;
3776 struct got_fileindex_entry *ie = NULL;
3778 char *ondisk_path, *parent = NULL;
3780 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3782 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3784 staged_status = get_staged_status(ie);
3785 if (staged_status != GOT_STATUS_NO_CHANGE) {
3786 if (staged_status == GOT_STATUS_DELETE)
3788 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3791 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3793 return got_error_from_errno("asprintf");
3795 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3800 if (a->status_codes) {
3801 size_t ncodes = strlen(a->status_codes);
3803 for (i = 0; i < ncodes ; i++) {
3804 if (status == a->status_codes[i])
3808 /* Do not delete files in non-matching status. */
3812 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
3813 a->status_codes[i] != GOT_STATUS_MISSING) {
3814 static char msg[64];
3815 snprintf(msg, sizeof(msg),
3816 "invalid status code '%c'", a->status_codes[i]);
3817 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
3822 if (status != GOT_STATUS_NO_CHANGE) {
3823 if (status == GOT_STATUS_DELETE)
3825 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3826 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3829 if (status != GOT_STATUS_MODIFY &&
3830 status != GOT_STATUS_MISSING) {
3831 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3836 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3838 if (unlinkat(dirfd, de_name, 0) != 0) {
3839 err = got_error_from_errno2("unlinkat",
3843 } else if (unlink(ondisk_path) != 0) {
3844 err = got_error_from_errno2("unlink", ondisk_path);
3848 parent = dirname(ondisk_path);
3850 if (parent == NULL) {
3851 err = got_error_from_errno2("dirname", ondisk_path);
3854 while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3855 if (rmdir(parent) == -1) {
3856 if (errno != ENOTEMPTY)
3857 err = got_error_from_errno2("rmdir",
3861 parent = dirname(parent);
3862 if (parent == NULL) {
3863 err = got_error_from_errno2("dirname", parent);
3869 got_fileindex_entry_mark_deleted_from_disk(ie);
3874 if (status == GOT_STATUS_DELETE)
3876 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3877 staged_status, relpath);
3880 const struct got_error *
3881 got_worktree_schedule_delete(struct got_worktree *worktree,
3882 struct got_pathlist_head *paths, int delete_local_mods,
3883 const char *status_codes,
3884 got_worktree_delete_cb progress_cb, void *progress_arg,
3885 struct got_repository *repo, int keep_on_disk)
3887 struct got_fileindex *fileindex = NULL;
3888 char *fileindex_path = NULL;
3889 const struct got_error *err = NULL, *sync_err, *unlockerr;
3890 struct got_pathlist_entry *pe;
3891 struct schedule_deletion_args sda;
3893 err = lock_worktree(worktree, LOCK_EX);
3897 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3901 sda.worktree = worktree;
3902 sda.fileindex = fileindex;
3903 sda.progress_cb = progress_cb;
3904 sda.progress_arg = progress_arg;
3906 sda.delete_local_mods = delete_local_mods;
3907 sda.keep_on_disk = keep_on_disk;
3908 sda.status_codes = status_codes;
3910 TAILQ_FOREACH(pe, paths, entry) {
3911 err = worktree_status(worktree, pe->path, fileindex, repo,
3912 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3916 sync_err = sync_fileindex(fileindex, fileindex_path);
3917 if (sync_err && err == NULL)
3920 free(fileindex_path);
3922 got_fileindex_free(fileindex);
3923 unlockerr = lock_worktree(worktree, LOCK_SH);
3924 if (unlockerr && err == NULL)
3929 static const struct got_error *
3930 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3932 const struct got_error *err = NULL;
3934 size_t linesize = 0, n;
3937 linelen = getline(&line, &linesize, infile);
3938 if (linelen == -1) {
3939 if (ferror(infile)) {
3940 err = got_error_from_errno("getline");
3946 n = fwrite(line, 1, linelen, outfile);
3948 err = got_ferror(outfile, GOT_ERR_IO);
3953 n = fwrite(line, 1, linelen, rejectfile);
3955 err = got_ferror(outfile, GOT_ERR_IO);
3962 static const struct got_error *
3963 skip_one_line(FILE *f)
3966 size_t linesize = 0;
3969 linelen = getline(&line, &linesize, f);
3970 if (linelen == -1) {
3972 return got_error_from_errno("getline");
3979 static const struct got_error *
3980 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3981 int start_old, int end_old, int start_new, int end_new,
3982 FILE *outfile, FILE *rejectfile)
3984 const struct got_error *err;
3986 /* Copy old file's lines leading up to patch. */
3987 while (!feof(f1) && *line_cur1 < start_old) {
3988 err = copy_one_line(f1, outfile, NULL);
3993 /* Skip new file's lines leading up to patch. */
3994 while (!feof(f2) && *line_cur2 < start_new) {
3996 err = copy_one_line(f2, NULL, rejectfile);
3998 err = skip_one_line(f2);
4003 /* Copy patched lines. */
4004 while (!feof(f2) && *line_cur2 <= end_new) {
4005 err = copy_one_line(f2, outfile, NULL);
4010 /* Skip over old file's replaced lines. */
4011 while (!feof(f1) && *line_cur1 <= end_old) {
4013 err = copy_one_line(f1, NULL, rejectfile);
4015 err = skip_one_line(f1);
4024 static const struct got_error *
4025 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4026 FILE *outfile, FILE *rejectfile)
4028 const struct got_error *err;
4031 /* Copy old file's lines until EOF. */
4033 err = copy_one_line(f1, outfile, NULL);
4040 /* Copy new file's lines until EOF. */
4042 err = copy_one_line(f2, NULL, rejectfile);
4052 static const struct got_error *
4053 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
4054 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
4055 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
4056 int *line_cur2, FILE *outfile, FILE *rejectfile,
4057 got_worktree_patch_cb patch_cb, void *patch_arg)
4059 const struct got_error *err = NULL;
4060 int start_old = change->cv.a;
4061 int end_old = change->cv.b;
4062 int start_new = change->cv.c;
4063 int end_new = change->cv.d;
4067 *choice = GOT_PATCH_CHOICE_NONE;
4069 hunkfile = got_opentemp();
4070 if (hunkfile == NULL)
4071 return got_error_from_errno("got_opentemp");
4076 /* XXX TODO needs error checking */
4077 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
4079 if (fseek(f1, pos1, SEEK_SET) == -1) {
4080 err = got_ferror(f1, GOT_ERR_IO);
4083 if (fseek(f2, pos2, SEEK_SET) == -1) {
4084 err = got_ferror(f1, GOT_ERR_IO);
4087 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4088 err = got_ferror(hunkfile, GOT_ERR_IO);
4092 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4093 hunkfile, n, nchanges);
4098 case GOT_PATCH_CHOICE_YES:
4099 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4100 end_old, start_new, end_new, outfile, rejectfile);
4102 case GOT_PATCH_CHOICE_NO:
4103 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4104 end_old, start_new, end_new, rejectfile, outfile);
4106 case GOT_PATCH_CHOICE_QUIT:
4109 err = got_error(GOT_ERR_PATCH_CHOICE);
4113 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4114 err = got_error_from_errno("fclose");
4118 struct revert_file_args {
4119 struct got_worktree *worktree;
4120 struct got_fileindex *fileindex;
4121 got_worktree_checkout_cb progress_cb;
4123 got_worktree_patch_cb patch_cb;
4125 struct got_repository *repo;
4128 static const struct got_error *
4129 create_patched_content(char **path_outfile, int reverse_patch,
4130 struct got_object_id *blob_id, const char *path2,
4131 int dirfd2, const char *de_name2,
4132 const char *relpath, struct got_repository *repo,
4133 got_worktree_patch_cb patch_cb, void *patch_arg)
4135 const struct got_error *err;
4136 struct got_blob_object *blob = NULL;
4137 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4139 char link_target[PATH_MAX];
4140 ssize_t link_len = 0;
4141 char *path1 = NULL, *id_str = NULL;
4142 struct stat sb1, sb2;
4143 struct got_diff_changes *changes = NULL;
4144 struct got_diff_state *ds = NULL;
4145 struct got_diff_args *args = NULL;
4146 struct got_diff_change *change;
4147 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
4150 *path_outfile = NULL;
4152 err = got_object_id_str(&id_str, blob_id);
4157 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4159 if (errno != ELOOP) {
4160 err = got_error_from_errno2("openat", path2);
4163 link_len = readlinkat(dirfd2, de_name2,
4164 link_target, sizeof(link_target));
4166 return got_error_from_errno2("readlinkat", path2);
4167 sb2.st_mode = S_IFLNK;
4168 sb2.st_size = link_len;
4171 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4173 if (errno != ELOOP) {
4174 err = got_error_from_errno2("open", path2);
4177 link_len = readlink(path2, link_target,
4178 sizeof(link_target));
4180 return got_error_from_errno2("readlink", path2);
4181 sb2.st_mode = S_IFLNK;
4182 sb2.st_size = link_len;
4186 if (fstat(fd2, &sb2) == -1) {
4187 err = got_error_from_errno2("fstat", path2);
4191 f2 = fdopen(fd2, "r");
4193 err = got_error_from_errno2("fdopen", path2);
4199 f2 = got_opentemp();
4201 err = got_error_from_errno2("got_opentemp", path2);
4204 n = fwrite(link_target, 1, link_len, f2);
4205 if (n != link_len) {
4206 err = got_ferror(f2, GOT_ERR_IO);
4209 if (fflush(f2) == EOF) {
4210 err = got_error_from_errno("fflush");
4216 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4220 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4224 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4228 if (stat(path1, &sb1) == -1) {
4229 err = got_error_from_errno2("stat", path1);
4233 err = got_diff_files(&changes, &ds, &args, &diff_flags,
4234 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
4238 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4242 if (fseek(f1, 0L, SEEK_SET) == -1)
4243 return got_ferror(f1, GOT_ERR_IO);
4244 if (fseek(f2, 0L, SEEK_SET) == -1)
4245 return got_ferror(f2, GOT_ERR_IO);
4246 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
4248 err = apply_or_reject_change(&choice, change, ++n,
4249 changes->nchanges, ds, args, diff_flags, relpath,
4250 f1, f2, &line_cur1, &line_cur2,
4251 reverse_patch ? NULL : outfile,
4252 reverse_patch ? outfile : NULL,
4253 patch_cb, patch_arg);
4256 if (choice == GOT_PATCH_CHOICE_YES)
4258 else if (choice == GOT_PATCH_CHOICE_QUIT)
4262 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4263 reverse_patch ? NULL : outfile,
4264 reverse_patch ? outfile : NULL);
4268 if (!S_ISLNK(sb2.st_mode)) {
4269 if (chmod(*path_outfile, sb2.st_mode) == -1) {
4270 err = got_error_from_errno2("chmod", path2);
4278 got_object_blob_close(blob);
4279 if (f1 && fclose(f1) == EOF && err == NULL)
4280 err = got_error_from_errno2("fclose", path1);
4281 if (f2 && fclose(f2) == EOF && err == NULL)
4282 err = got_error_from_errno2("fclose", path2);
4283 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4284 err = got_error_from_errno2("close", path2);
4285 if (outfile && fclose(outfile) == EOF && err == NULL)
4286 err = got_error_from_errno2("fclose", *path_outfile);
4287 if (path1 && unlink(path1) == -1 && err == NULL)
4288 err = got_error_from_errno2("unlink", path1);
4289 if (err || !have_content) {
4290 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4291 err = got_error_from_errno2("unlink", *path_outfile);
4292 free(*path_outfile);
4293 *path_outfile = NULL;
4297 got_diff_state_free(ds);
4301 got_diff_free_changes(changes);
4306 static const struct got_error *
4307 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4308 const char *relpath, struct got_object_id *blob_id,
4309 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4310 int dirfd, const char *de_name)
4312 struct revert_file_args *a = arg;
4313 const struct got_error *err = NULL;
4314 char *parent_path = NULL;
4315 struct got_fileindex_entry *ie;
4316 struct got_tree_object *tree = NULL;
4317 struct got_object_id *tree_id = NULL;
4318 const struct got_tree_entry *te = NULL;
4319 char *tree_path = NULL, *te_name;
4320 char *ondisk_path = NULL, *path_content = NULL;
4321 struct got_blob_object *blob = NULL;
4323 /* Reverting a staged deletion is a no-op. */
4324 if (status == GOT_STATUS_DELETE &&
4325 staged_status != GOT_STATUS_NO_CHANGE)
4328 if (status == GOT_STATUS_UNVERSIONED)
4329 return (*a->progress_cb)(a->progress_arg,
4330 GOT_STATUS_UNVERSIONED, relpath);
4332 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4334 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4336 /* Construct in-repository path of tree which contains this blob. */
4337 err = got_path_dirname(&parent_path, ie->path);
4339 if (err->code != GOT_ERR_BAD_PATH)
4341 parent_path = strdup("/");
4342 if (parent_path == NULL) {
4343 err = got_error_from_errno("strdup");
4347 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4348 tree_path = strdup(parent_path);
4349 if (tree_path == NULL) {
4350 err = got_error_from_errno("strdup");
4354 if (got_path_is_root_dir(parent_path)) {
4355 tree_path = strdup(a->worktree->path_prefix);
4356 if (tree_path == NULL) {
4357 err = got_error_from_errno("strdup");
4361 if (asprintf(&tree_path, "%s/%s",
4362 a->worktree->path_prefix, parent_path) == -1) {
4363 err = got_error_from_errno("asprintf");
4369 err = got_object_id_by_path(&tree_id, a->repo,
4370 a->worktree->base_commit_id, tree_path);
4372 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4373 (status == GOT_STATUS_ADD ||
4374 staged_status == GOT_STATUS_ADD)))
4377 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4381 te_name = basename(ie->path);
4382 if (te_name == NULL) {
4383 err = got_error_from_errno2("basename", ie->path);
4387 te = got_object_tree_find_entry(tree, te_name);
4388 if (te == NULL && status != GOT_STATUS_ADD &&
4389 staged_status != GOT_STATUS_ADD) {
4390 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4396 case GOT_STATUS_ADD:
4398 int choice = GOT_PATCH_CHOICE_NONE;
4399 err = (*a->patch_cb)(&choice, a->patch_arg,
4400 status, ie->path, NULL, 1, 1);
4403 if (choice != GOT_PATCH_CHOICE_YES)
4406 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4410 got_fileindex_entry_remove(a->fileindex, ie);
4412 case GOT_STATUS_DELETE:
4414 int choice = GOT_PATCH_CHOICE_NONE;
4415 err = (*a->patch_cb)(&choice, a->patch_arg,
4416 status, ie->path, NULL, 1, 1);
4419 if (choice != GOT_PATCH_CHOICE_YES)
4423 case GOT_STATUS_MODIFY:
4424 case GOT_STATUS_MODE_CHANGE:
4425 case GOT_STATUS_CONFLICT:
4426 case GOT_STATUS_MISSING: {
4427 struct got_object_id id;
4428 if (staged_status == GOT_STATUS_ADD ||
4429 staged_status == GOT_STATUS_MODIFY) {
4430 memcpy(id.sha1, ie->staged_blob_sha1,
4431 SHA1_DIGEST_LENGTH);
4433 memcpy(id.sha1, ie->blob_sha1,
4434 SHA1_DIGEST_LENGTH);
4435 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4439 if (asprintf(&ondisk_path, "%s/%s",
4440 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4441 err = got_error_from_errno("asprintf");
4445 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4446 status == GOT_STATUS_CONFLICT)) {
4447 int is_bad_symlink = 0;
4448 err = create_patched_content(&path_content, 1, &id,
4449 ondisk_path, dirfd, de_name, ie->path, a->repo,
4450 a->patch_cb, a->patch_arg);
4451 if (err || path_content == NULL)
4453 if (te && S_ISLNK(te->mode)) {
4454 if (unlink(path_content) == -1) {
4455 err = got_error_from_errno2("unlink",
4459 err = install_symlink(&is_bad_symlink,
4460 a->worktree, ondisk_path, ie->path,
4461 blob, 0, 1, 0, a->repo,
4462 a->progress_cb, a->progress_arg);
4464 if (rename(path_content, ondisk_path) == -1) {
4465 err = got_error_from_errno3("rename",
4466 path_content, ondisk_path);
4471 int is_bad_symlink = 0;
4472 if (te && S_ISLNK(te->mode)) {
4473 err = install_symlink(&is_bad_symlink,
4474 a->worktree, ondisk_path, ie->path,
4475 blob, 0, 1, 0, a->repo,
4476 a->progress_cb, a->progress_arg);
4478 err = install_blob(a->worktree, ondisk_path,
4480 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4481 got_fileindex_perms_to_st(ie), blob,
4482 0, 1, 0, 0, a->repo,
4483 a->progress_cb, a->progress_arg);
4487 if (status == GOT_STATUS_DELETE ||
4488 status == GOT_STATUS_MODE_CHANGE) {
4489 err = got_fileindex_entry_update(ie,
4490 ondisk_path, blob->id.sha1,
4491 a->worktree->base_commit_id->sha1, 1);
4495 if (is_bad_symlink) {
4496 got_fileindex_entry_filetype_set(ie,
4497 GOT_FILEIDX_MODE_BAD_SYMLINK);
4511 got_object_blob_close(blob);
4513 got_object_tree_close(tree);
4518 const struct got_error *
4519 got_worktree_revert(struct got_worktree *worktree,
4520 struct got_pathlist_head *paths,
4521 got_worktree_checkout_cb progress_cb, void *progress_arg,
4522 got_worktree_patch_cb patch_cb, void *patch_arg,
4523 struct got_repository *repo)
4525 struct got_fileindex *fileindex = NULL;
4526 char *fileindex_path = NULL;
4527 const struct got_error *err = NULL, *unlockerr = NULL;
4528 const struct got_error *sync_err = NULL;
4529 struct got_pathlist_entry *pe;
4530 struct revert_file_args rfa;
4532 err = lock_worktree(worktree, LOCK_EX);
4536 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4540 rfa.worktree = worktree;
4541 rfa.fileindex = fileindex;
4542 rfa.progress_cb = progress_cb;
4543 rfa.progress_arg = progress_arg;
4544 rfa.patch_cb = patch_cb;
4545 rfa.patch_arg = patch_arg;
4547 TAILQ_FOREACH(pe, paths, entry) {
4548 err = worktree_status(worktree, pe->path, fileindex, repo,
4549 revert_file, &rfa, NULL, NULL, 0, 0);
4553 sync_err = sync_fileindex(fileindex, fileindex_path);
4554 if (sync_err && err == NULL)
4557 free(fileindex_path);
4559 got_fileindex_free(fileindex);
4560 unlockerr = lock_worktree(worktree, LOCK_SH);
4561 if (unlockerr && err == NULL)
4567 free_commitable(struct got_commitable *ct)
4570 free(ct->in_repo_path);
4571 free(ct->ondisk_path);
4573 free(ct->base_blob_id);
4574 free(ct->staged_blob_id);
4575 free(ct->base_commit_id);
4579 struct collect_commitables_arg {
4580 struct got_pathlist_head *commitable_paths;
4581 struct got_repository *repo;
4582 struct got_worktree *worktree;
4583 struct got_fileindex *fileindex;
4584 int have_staged_files;
4585 int allow_bad_symlinks;
4588 static const struct got_error *
4589 collect_commitables(void *arg, unsigned char status,
4590 unsigned char staged_status, const char *relpath,
4591 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4592 struct got_object_id *commit_id, int dirfd, const char *de_name)
4594 struct collect_commitables_arg *a = arg;
4595 const struct got_error *err = NULL;
4596 struct got_commitable *ct = NULL;
4597 struct got_pathlist_entry *new = NULL;
4598 char *parent_path = NULL, *path = NULL;
4601 if (a->have_staged_files) {
4602 if (staged_status != GOT_STATUS_MODIFY &&
4603 staged_status != GOT_STATUS_ADD &&
4604 staged_status != GOT_STATUS_DELETE)
4607 if (status == GOT_STATUS_CONFLICT)
4608 return got_error(GOT_ERR_COMMIT_CONFLICT);
4610 if (status != GOT_STATUS_MODIFY &&
4611 status != GOT_STATUS_MODE_CHANGE &&
4612 status != GOT_STATUS_ADD &&
4613 status != GOT_STATUS_DELETE)
4617 if (asprintf(&path, "/%s", relpath) == -1) {
4618 err = got_error_from_errno("asprintf");
4621 if (strcmp(path, "/") == 0) {
4622 parent_path = strdup("");
4623 if (parent_path == NULL)
4624 return got_error_from_errno("strdup");
4626 err = got_path_dirname(&parent_path, path);
4631 ct = calloc(1, sizeof(*ct));
4633 err = got_error_from_errno("calloc");
4637 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4639 err = got_error_from_errno("asprintf");
4643 if (staged_status == GOT_STATUS_ADD ||
4644 staged_status == GOT_STATUS_MODIFY) {
4645 struct got_fileindex_entry *ie;
4646 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4647 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4648 case GOT_FILEIDX_MODE_REGULAR_FILE:
4649 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4652 case GOT_FILEIDX_MODE_SYMLINK:
4656 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4659 ct->mode |= got_fileindex_entry_perms_get(ie);
4660 } else if (status != GOT_STATUS_DELETE &&
4661 staged_status != GOT_STATUS_DELETE) {
4663 if (fstatat(dirfd, de_name, &sb,
4664 AT_SYMLINK_NOFOLLOW) == -1) {
4665 err = got_error_from_errno2("fstatat",
4669 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4670 err = got_error_from_errno2("lstat", ct->ondisk_path);
4673 ct->mode = sb.st_mode;
4676 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4677 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4679 err = got_error_from_errno("asprintf");
4683 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4684 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4686 char target_path[PATH_MAX];
4688 target_len = readlink(ct->ondisk_path, target_path,
4689 sizeof(target_path));
4690 if (target_len == -1) {
4691 err = got_error_from_errno2("readlink",
4695 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4696 target_len, ct->ondisk_path, a->worktree->root_path);
4699 if (is_bad_symlink) {
4700 err = got_error_path(ct->ondisk_path,
4701 GOT_ERR_BAD_SYMLINK);
4707 ct->status = status;
4708 ct->staged_status = staged_status;
4709 ct->blob_id = NULL; /* will be filled in when blob gets created */
4710 if (ct->status != GOT_STATUS_ADD &&
4711 ct->staged_status != GOT_STATUS_ADD) {
4712 ct->base_blob_id = got_object_id_dup(blob_id);
4713 if (ct->base_blob_id == NULL) {
4714 err = got_error_from_errno("got_object_id_dup");
4717 ct->base_commit_id = got_object_id_dup(commit_id);
4718 if (ct->base_commit_id == NULL) {
4719 err = got_error_from_errno("got_object_id_dup");
4723 if (ct->staged_status == GOT_STATUS_ADD ||
4724 ct->staged_status == GOT_STATUS_MODIFY) {
4725 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4726 if (ct->staged_blob_id == NULL) {
4727 err = got_error_from_errno("got_object_id_dup");
4731 ct->path = strdup(path);
4732 if (ct->path == NULL) {
4733 err = got_error_from_errno("strdup");
4736 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4738 if (ct && (err || new == NULL))
4739 free_commitable(ct);
4745 static const struct got_error *write_tree(struct got_object_id **, int *,
4746 struct got_tree_object *, const char *, struct got_pathlist_head *,
4747 got_worktree_status_cb status_cb, void *status_arg,
4748 struct got_repository *);
4750 static const struct got_error *
4751 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4752 struct got_tree_entry *te, const char *parent_path,
4753 struct got_pathlist_head *commitable_paths,
4754 got_worktree_status_cb status_cb, void *status_arg,
4755 struct got_repository *repo)
4757 const struct got_error *err = NULL;
4758 struct got_tree_object *subtree;
4761 if (asprintf(&subpath, "%s%s%s", parent_path,
4762 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4763 return got_error_from_errno("asprintf");
4765 err = got_object_open_as_tree(&subtree, repo, &te->id);
4769 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4770 commitable_paths, status_cb, status_arg, repo);
4771 got_object_tree_close(subtree);
4776 static const struct got_error *
4777 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4779 const struct got_error *err = NULL;
4780 char *ct_parent_path = NULL;
4784 if (strchr(ct->in_repo_path, '/') == NULL) {
4785 *match = got_path_is_root_dir(path);
4789 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4792 *match = (strcmp(path, ct_parent_path) == 0);
4793 free(ct_parent_path);
4798 get_ct_file_mode(struct got_commitable *ct)
4800 if (S_ISLNK(ct->mode))
4803 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4806 static const struct got_error *
4807 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4808 struct got_tree_entry *te, struct got_commitable *ct)
4810 const struct got_error *err = NULL;
4814 err = got_object_tree_entry_dup(new_te, te);
4818 (*new_te)->mode = get_ct_file_mode(ct);
4820 if (ct->staged_status == GOT_STATUS_MODIFY)
4821 memcpy(&(*new_te)->id, ct->staged_blob_id,
4822 sizeof((*new_te)->id));
4824 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4826 if (err && *new_te) {
4833 static const struct got_error *
4834 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4835 struct got_commitable *ct)
4837 const struct got_error *err = NULL;
4842 *new_te = calloc(1, sizeof(**new_te));
4843 if (*new_te == NULL)
4844 return got_error_from_errno("calloc");
4846 ct_name = basename(ct->path);
4847 if (ct_name == NULL) {
4848 err = got_error_from_errno2("basename", ct->path);
4851 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4852 sizeof((*new_te)->name)) {
4853 err = got_error(GOT_ERR_NO_SPACE);
4857 (*new_te)->mode = get_ct_file_mode(ct);
4859 if (ct->staged_status == GOT_STATUS_ADD)
4860 memcpy(&(*new_te)->id, ct->staged_blob_id,
4861 sizeof((*new_te)->id));
4863 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4865 if (err && *new_te) {
4872 static const struct got_error *
4873 insert_tree_entry(struct got_tree_entry *new_te,
4874 struct got_pathlist_head *paths)
4876 const struct got_error *err = NULL;
4877 struct got_pathlist_entry *new_pe;
4879 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4883 return got_error(GOT_ERR_TREE_DUP_ENTRY);
4887 static const struct got_error *
4888 report_ct_status(struct got_commitable *ct,
4889 got_worktree_status_cb status_cb, void *status_arg)
4891 const char *ct_path = ct->path;
4892 unsigned char status;
4894 while (ct_path[0] == '/')
4897 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4898 status = ct->staged_status;
4900 status = ct->status;
4902 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4903 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4906 static const struct got_error *
4907 match_modified_subtree(int *modified, struct got_tree_entry *te,
4908 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4910 const struct got_error *err = NULL;
4911 struct got_pathlist_entry *pe;
4916 if (asprintf(&te_path, "%s%s%s", base_tree_path,
4917 got_path_is_root_dir(base_tree_path) ? "" : "/",
4919 return got_error_from_errno("asprintf");
4921 TAILQ_FOREACH(pe, commitable_paths, entry) {
4922 struct got_commitable *ct = pe->data;
4923 *modified = got_path_is_child(ct->in_repo_path, te_path,
4933 static const struct got_error *
4934 match_deleted_or_modified_ct(struct got_commitable **ctp,
4935 struct got_tree_entry *te, const char *base_tree_path,
4936 struct got_pathlist_head *commitable_paths)
4938 const struct got_error *err = NULL;
4939 struct got_pathlist_entry *pe;
4943 TAILQ_FOREACH(pe, commitable_paths, entry) {
4944 struct got_commitable *ct = pe->data;
4945 char *ct_name = NULL;
4948 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4949 if (ct->status != GOT_STATUS_MODIFY &&
4950 ct->status != GOT_STATUS_MODE_CHANGE &&
4951 ct->status != GOT_STATUS_DELETE)
4954 if (ct->staged_status != GOT_STATUS_MODIFY &&
4955 ct->staged_status != GOT_STATUS_DELETE)
4959 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4962 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4968 ct_name = basename(pe->path);
4969 if (ct_name == NULL)
4970 return got_error_from_errno2("basename", pe->path);
4972 if (strcmp(te->name, ct_name) != 0)
4982 static const struct got_error *
4983 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4984 const char *child_path, const char *path_base_tree,
4985 struct got_pathlist_head *commitable_paths,
4986 got_worktree_status_cb status_cb, void *status_arg,
4987 struct got_repository *repo)
4989 const struct got_error *err = NULL;
4990 struct got_tree_entry *new_te;
4992 struct got_object_id *id = NULL;
4997 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4998 got_path_is_root_dir(path_base_tree) ? "" : "/",
5000 return got_error_from_errno("asprintf");
5002 new_te = calloc(1, sizeof(*new_te));
5004 return got_error_from_errno("calloc");
5005 new_te->mode = S_IFDIR;
5007 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5008 sizeof(new_te->name)) {
5009 err = got_error(GOT_ERR_NO_SPACE);
5012 err = write_tree(&id, &nentries, NULL, subtree_path,
5013 commitable_paths, status_cb, status_arg, repo);
5018 memcpy(&new_te->id, id, sizeof(new_te->id));
5027 static const struct got_error *
5028 write_tree(struct got_object_id **new_tree_id, int *nentries,
5029 struct got_tree_object *base_tree, const char *path_base_tree,
5030 struct got_pathlist_head *commitable_paths,
5031 got_worktree_status_cb status_cb, void *status_arg,
5032 struct got_repository *repo)
5034 const struct got_error *err = NULL;
5035 struct got_pathlist_head paths;
5036 struct got_tree_entry *te, *new_te = NULL;
5037 struct got_pathlist_entry *pe;
5042 /* Insert, and recurse into, newly added entries first. */
5043 TAILQ_FOREACH(pe, commitable_paths, entry) {
5044 struct got_commitable *ct = pe->data;
5045 char *child_path = NULL, *slash;
5047 if ((ct->status != GOT_STATUS_ADD &&
5048 ct->staged_status != GOT_STATUS_ADD) ||
5049 (ct->flags & GOT_COMMITABLE_ADDED))
5052 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5053 strlen(path_base_tree)))
5056 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5061 slash = strchr(child_path, '/');
5062 if (slash == NULL) {
5063 err = alloc_added_blob_tree_entry(&new_te, ct);
5066 err = report_ct_status(ct, status_cb, status_arg);
5069 ct->flags |= GOT_COMMITABLE_ADDED;
5070 err = insert_tree_entry(new_te, &paths);
5075 *slash = '\0'; /* trim trailing path components */
5076 if (base_tree == NULL ||
5077 got_object_tree_find_entry(base_tree, child_path)
5079 err = make_subtree_for_added_blob(&new_te,
5080 child_path, path_base_tree,
5081 commitable_paths, status_cb, status_arg,
5085 err = insert_tree_entry(new_te, &paths);
5094 int i, nbase_entries;
5095 /* Handle modified and deleted entries. */
5096 nbase_entries = got_object_tree_get_nentries(base_tree);
5097 for (i = 0; i < nbase_entries; i++) {
5098 struct got_commitable *ct = NULL;
5100 te = got_object_tree_get_entry(base_tree, i);
5101 if (got_object_tree_entry_is_submodule(te)) {
5102 /* Entry is a submodule; just copy it. */
5103 err = got_object_tree_entry_dup(&new_te, te);
5106 err = insert_tree_entry(new_te, &paths);
5113 if (S_ISDIR(te->mode)) {
5115 err = got_object_tree_entry_dup(&new_te, te);
5118 err = match_modified_subtree(&modified, te,
5119 path_base_tree, commitable_paths);
5122 /* Avoid recursion into unmodified subtrees. */
5124 struct got_object_id *new_id;
5126 err = write_subtree(&new_id,
5128 path_base_tree, commitable_paths,
5129 status_cb, status_arg, repo);
5132 if (nsubentries == 0) {
5133 /* All entries were deleted. */
5137 memcpy(&new_te->id, new_id,
5138 sizeof(new_te->id));
5141 err = insert_tree_entry(new_te, &paths);
5148 err = match_deleted_or_modified_ct(&ct, te,
5149 path_base_tree, commitable_paths);
5153 /* NB: Deleted entries get dropped here. */
5154 if (ct->status == GOT_STATUS_MODIFY ||
5155 ct->status == GOT_STATUS_MODE_CHANGE ||
5156 ct->staged_status == GOT_STATUS_MODIFY) {
5157 err = alloc_modified_blob_tree_entry(
5161 err = insert_tree_entry(new_te, &paths);
5166 err = report_ct_status(ct, status_cb,
5171 /* Entry is unchanged; just copy it. */
5172 err = got_object_tree_entry_dup(&new_te, te);
5175 err = insert_tree_entry(new_te, &paths);
5183 /* Write new list of entries; deleted entries have been dropped. */
5184 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5186 got_pathlist_free(&paths);
5190 static const struct got_error *
5191 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
5192 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
5193 int have_staged_files)
5195 const struct got_error *err = NULL;
5196 struct got_pathlist_entry *pe;
5198 TAILQ_FOREACH(pe, commitable_paths, entry) {
5199 struct got_fileindex_entry *ie;
5200 struct got_commitable *ct = pe->data;
5202 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5204 if (ct->status == GOT_STATUS_DELETE ||
5205 ct->staged_status == GOT_STATUS_DELETE) {
5206 got_fileindex_entry_remove(fileindex, ie);
5207 } else if (ct->staged_status == GOT_STATUS_ADD ||
5208 ct->staged_status == GOT_STATUS_MODIFY) {
5209 got_fileindex_entry_stage_set(ie,
5210 GOT_FILEIDX_STAGE_NONE);
5211 err = got_fileindex_entry_update(ie,
5212 ct->ondisk_path, ct->staged_blob_id->sha1,
5213 new_base_commit_id->sha1,
5214 !have_staged_files);
5216 err = got_fileindex_entry_update(ie,
5217 ct->ondisk_path, ct->blob_id->sha1,
5218 new_base_commit_id->sha1,
5219 !have_staged_files);
5221 err = got_fileindex_entry_alloc(&ie, pe->path);
5224 err = got_fileindex_entry_update(ie, ct->ondisk_path,
5225 ct->blob_id->sha1, new_base_commit_id->sha1, 1);
5227 got_fileindex_entry_free(ie);
5230 err = got_fileindex_entry_add(fileindex, ie);
5232 got_fileindex_entry_free(ie);
5241 static const struct got_error *
5242 check_out_of_date(const char *in_repo_path, unsigned char status,
5243 unsigned char staged_status, struct got_object_id *base_blob_id,
5244 struct got_object_id *base_commit_id,
5245 struct got_object_id *head_commit_id, struct got_repository *repo,
5248 const struct got_error *err = NULL;
5249 struct got_object_id *id = NULL;
5251 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5252 /* Trivial case: base commit == head commit */
5253 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5256 * Ensure file content which local changes were based
5257 * on matches file content in the branch head.
5259 err = got_object_id_by_path(&id, repo, head_commit_id,
5262 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5263 err = got_error(ood_errcode);
5265 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5266 err = got_error(ood_errcode);
5268 /* Require that added files don't exist in the branch head. */
5269 err = got_object_id_by_path(&id, repo, head_commit_id,
5271 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5273 err = id ? got_error(ood_errcode) : NULL;
5280 const struct got_error *
5281 commit_worktree(struct got_object_id **new_commit_id,
5282 struct got_pathlist_head *commitable_paths,
5283 struct got_object_id *head_commit_id, struct got_worktree *worktree,
5284 const char *author, const char *committer,
5285 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5286 got_worktree_status_cb status_cb, void *status_arg,
5287 struct got_repository *repo)
5289 const struct got_error *err = NULL, *unlockerr = NULL;
5290 struct got_pathlist_entry *pe;
5291 const char *head_ref_name = NULL;
5292 struct got_commit_object *head_commit = NULL;
5293 struct got_reference *head_ref2 = NULL;
5294 struct got_object_id *head_commit_id2 = NULL;
5295 struct got_tree_object *head_tree = NULL;
5296 struct got_object_id *new_tree_id = NULL;
5298 struct got_object_id_queue parent_ids;
5299 struct got_object_qid *pid = NULL;
5300 char *logmsg = NULL;
5302 *new_commit_id = NULL;
5304 SIMPLEQ_INIT(&parent_ids);
5306 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5310 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5314 if (commit_msg_cb != NULL) {
5315 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5320 if (logmsg == NULL || strlen(logmsg) == 0) {
5321 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5325 /* Create blobs from added and modified files and record their IDs. */
5326 TAILQ_FOREACH(pe, commitable_paths, entry) {
5327 struct got_commitable *ct = pe->data;
5330 /* Blobs for staged files already exist. */
5331 if (ct->staged_status == GOT_STATUS_ADD ||
5332 ct->staged_status == GOT_STATUS_MODIFY)
5335 if (ct->status != GOT_STATUS_ADD &&
5336 ct->status != GOT_STATUS_MODIFY &&
5337 ct->status != GOT_STATUS_MODE_CHANGE)
5340 if (asprintf(&ondisk_path, "%s/%s",
5341 worktree->root_path, pe->path) == -1) {
5342 err = got_error_from_errno("asprintf");
5345 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5351 /* Recursively write new tree objects. */
5352 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5353 commitable_paths, status_cb, status_arg, repo);
5357 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5360 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5361 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5362 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5363 got_object_qid_free(pid);
5369 /* Check if a concurrent commit to our branch has occurred. */
5370 head_ref_name = got_worktree_get_head_ref_name(worktree);
5371 if (head_ref_name == NULL) {
5372 err = got_error_from_errno("got_worktree_get_head_ref_name");
5375 /* Lock the reference here to prevent concurrent modification. */
5376 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5379 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5382 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5383 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5386 /* Update branch head in repository. */
5387 err = got_ref_change_ref(head_ref2, *new_commit_id);
5390 err = got_ref_write(head_ref2, repo);
5394 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5398 err = ref_base_commit(worktree, repo);
5403 got_object_tree_close(head_tree);
5405 got_object_commit_close(head_commit);
5406 free(head_commit_id2);
5408 unlockerr = got_ref_unlock(head_ref2);
5409 if (unlockerr && err == NULL)
5411 got_ref_close(head_ref2);
5416 static const struct got_error *
5417 check_path_is_commitable(const char *path,
5418 struct got_pathlist_head *commitable_paths)
5420 struct got_pathlist_entry *cpe = NULL;
5421 size_t path_len = strlen(path);
5423 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5424 struct got_commitable *ct = cpe->data;
5425 const char *ct_path = ct->path;
5427 while (ct_path[0] == '/')
5430 if (strcmp(path, ct_path) == 0 ||
5431 got_path_is_child(ct_path, path, path_len))
5436 return got_error_path(path, GOT_ERR_BAD_PATH);
5441 static const struct got_error *
5442 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5444 int *have_staged_files = arg;
5446 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5447 *have_staged_files = 1;
5448 return got_error(GOT_ERR_CANCELLED);
5454 static const struct got_error *
5455 check_non_staged_files(struct got_fileindex *fileindex,
5456 struct got_pathlist_head *paths)
5458 struct got_pathlist_entry *pe;
5459 struct got_fileindex_entry *ie;
5461 TAILQ_FOREACH(pe, paths, entry) {
5462 if (pe->path[0] == '\0')
5464 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5466 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5467 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5468 return got_error_path(pe->path,
5469 GOT_ERR_FILE_NOT_STAGED);
5475 const struct got_error *
5476 got_worktree_commit(struct got_object_id **new_commit_id,
5477 struct got_worktree *worktree, struct got_pathlist_head *paths,
5478 const char *author, const char *committer, int allow_bad_symlinks,
5479 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5480 got_worktree_status_cb status_cb, void *status_arg,
5481 struct got_repository *repo)
5483 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5484 struct got_fileindex *fileindex = NULL;
5485 char *fileindex_path = NULL;
5486 struct got_pathlist_head commitable_paths;
5487 struct collect_commitables_arg cc_arg;
5488 struct got_pathlist_entry *pe;
5489 struct got_reference *head_ref = NULL;
5490 struct got_object_id *head_commit_id = NULL;
5491 int have_staged_files = 0;
5493 *new_commit_id = NULL;
5495 TAILQ_INIT(&commitable_paths);
5497 err = lock_worktree(worktree, LOCK_EX);
5501 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5505 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5509 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5513 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5514 &have_staged_files);
5515 if (err && err->code != GOT_ERR_CANCELLED)
5517 if (have_staged_files) {
5518 err = check_non_staged_files(fileindex, paths);
5523 cc_arg.commitable_paths = &commitable_paths;
5524 cc_arg.worktree = worktree;
5525 cc_arg.fileindex = fileindex;
5527 cc_arg.have_staged_files = have_staged_files;
5528 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5529 TAILQ_FOREACH(pe, paths, entry) {
5530 err = worktree_status(worktree, pe->path, fileindex, repo,
5531 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5536 if (TAILQ_EMPTY(&commitable_paths)) {
5537 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5541 TAILQ_FOREACH(pe, paths, entry) {
5542 err = check_path_is_commitable(pe->path, &commitable_paths);
5547 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5548 struct got_commitable *ct = pe->data;
5549 const char *ct_path = ct->in_repo_path;
5551 while (ct_path[0] == '/')
5553 err = check_out_of_date(ct_path, ct->status,
5554 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5555 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5561 err = commit_worktree(new_commit_id, &commitable_paths,
5562 head_commit_id, worktree, author, committer,
5563 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5567 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5568 fileindex, have_staged_files);
5569 sync_err = sync_fileindex(fileindex, fileindex_path);
5570 if (sync_err && err == NULL)
5574 got_fileindex_free(fileindex);
5575 free(fileindex_path);
5576 unlockerr = lock_worktree(worktree, LOCK_SH);
5577 if (unlockerr && err == NULL)
5579 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5580 struct got_commitable *ct = pe->data;
5581 free_commitable(ct);
5583 got_pathlist_free(&commitable_paths);
5588 got_commitable_get_path(struct got_commitable *ct)
5594 got_commitable_get_status(struct got_commitable *ct)
5599 struct check_rebase_ok_arg {
5600 struct got_worktree *worktree;
5601 struct got_repository *repo;
5604 static const struct got_error *
5605 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5607 const struct got_error *err = NULL;
5608 struct check_rebase_ok_arg *a = arg;
5609 unsigned char status;
5613 /* Reject rebase of a work tree with mixed base commits. */
5614 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5615 SHA1_DIGEST_LENGTH))
5616 return got_error(GOT_ERR_MIXED_COMMITS);
5618 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5620 return got_error_from_errno("asprintf");
5622 /* Reject rebase of a work tree with modified or staged files. */
5623 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5628 if (status != GOT_STATUS_NO_CHANGE)
5629 return got_error(GOT_ERR_MODIFIED);
5630 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5631 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5636 const struct got_error *
5637 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5638 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5639 struct got_worktree *worktree, struct got_reference *branch,
5640 struct got_repository *repo)
5642 const struct got_error *err = NULL;
5643 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5644 char *branch_ref_name = NULL;
5645 char *fileindex_path = NULL;
5646 struct check_rebase_ok_arg ok_arg;
5647 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5648 struct got_object_id *wt_branch_tip = NULL;
5650 *new_base_branch_ref = NULL;
5654 err = lock_worktree(worktree, LOCK_EX);
5658 err = open_fileindex(fileindex, &fileindex_path, worktree);
5662 ok_arg.worktree = worktree;
5664 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5669 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5673 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5677 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5681 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5686 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5689 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5690 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5694 err = got_ref_alloc_symref(new_base_branch_ref,
5695 new_base_branch_ref_name, wt_branch);
5698 err = got_ref_write(*new_base_branch_ref, repo);
5702 /* TODO Lock original branch's ref while rebasing? */
5704 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5708 err = got_ref_write(branch_ref, repo);
5712 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5713 worktree->base_commit_id);
5716 err = got_ref_write(*tmp_branch, repo);
5720 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5724 free(fileindex_path);
5725 free(tmp_branch_name);
5726 free(new_base_branch_ref_name);
5727 free(branch_ref_name);
5729 got_ref_close(branch_ref);
5731 got_ref_close(wt_branch);
5732 free(wt_branch_tip);
5734 if (*new_base_branch_ref) {
5735 got_ref_close(*new_base_branch_ref);
5736 *new_base_branch_ref = NULL;
5739 got_ref_close(*tmp_branch);
5743 got_fileindex_free(*fileindex);
5746 lock_worktree(worktree, LOCK_SH);
5751 const struct got_error *
5752 got_worktree_rebase_continue(struct got_object_id **commit_id,
5753 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5754 struct got_reference **branch, struct got_fileindex **fileindex,
5755 struct got_worktree *worktree, struct got_repository *repo)
5757 const struct got_error *err;
5758 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5759 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5760 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5761 char *fileindex_path = NULL;
5762 int have_staged_files = 0;
5765 *new_base_branch = NULL;
5770 err = lock_worktree(worktree, LOCK_EX);
5774 err = open_fileindex(fileindex, &fileindex_path, worktree);
5778 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5779 &have_staged_files);
5780 if (err && err->code != GOT_ERR_CANCELLED)
5782 if (have_staged_files) {
5783 err = got_error(GOT_ERR_STAGED_PATHS);
5787 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5791 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5795 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5799 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5803 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5807 err = got_ref_open(branch, repo,
5808 got_ref_get_symref_target(branch_ref), 0);
5812 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5816 err = got_ref_resolve(commit_id, repo, commit_ref);
5820 err = got_ref_open(new_base_branch, repo,
5821 new_base_branch_ref_name, 0);
5825 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5829 free(commit_ref_name);
5830 free(branch_ref_name);
5831 free(fileindex_path);
5833 got_ref_close(commit_ref);
5835 got_ref_close(branch_ref);
5840 got_ref_close(*tmp_branch);
5843 if (*new_base_branch) {
5844 got_ref_close(*new_base_branch);
5845 *new_base_branch = NULL;
5848 got_ref_close(*branch);
5852 got_fileindex_free(*fileindex);
5855 lock_worktree(worktree, LOCK_SH);
5860 const struct got_error *
5861 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5863 const struct got_error *err;
5864 char *tmp_branch_name = NULL;
5866 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5870 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5871 free(tmp_branch_name);
5875 static const struct got_error *
5876 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5877 char **logmsg, void *arg)
5883 static const struct got_error *
5884 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5885 const char *path, struct got_object_id *blob_id,
5886 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5887 int dirfd, const char *de_name)
5892 struct collect_merged_paths_arg {
5893 got_worktree_checkout_cb progress_cb;
5895 struct got_pathlist_head *merged_paths;
5898 static const struct got_error *
5899 collect_merged_paths(void *arg, unsigned char status, const char *path)
5901 const struct got_error *err;
5902 struct collect_merged_paths_arg *a = arg;
5904 struct got_pathlist_entry *new;
5906 err = (*a->progress_cb)(a->progress_arg, status, path);
5910 if (status != GOT_STATUS_MERGE &&
5911 status != GOT_STATUS_ADD &&
5912 status != GOT_STATUS_DELETE &&
5913 status != GOT_STATUS_CONFLICT)
5918 return got_error_from_errno("strdup");
5920 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5921 if (err || new == NULL)
5927 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5929 struct got_pathlist_entry *pe;
5931 TAILQ_FOREACH(pe, merged_paths, entry)
5932 free((char *)pe->path);
5934 got_pathlist_free(merged_paths);
5937 static const struct got_error *
5938 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5939 int is_rebase, struct got_repository *repo)
5941 const struct got_error *err;
5942 struct got_reference *commit_ref = NULL;
5944 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5946 if (err->code != GOT_ERR_NOT_REF)
5948 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
5951 err = got_ref_write(commit_ref, repo);
5954 } else if (is_rebase) {
5955 struct got_object_id *stored_id;
5958 err = got_ref_resolve(&stored_id, repo, commit_ref);
5961 cmp = got_object_id_cmp(commit_id, stored_id);
5964 err = got_error(GOT_ERR_REBASE_COMMITID);
5970 got_ref_close(commit_ref);
5974 static const struct got_error *
5975 rebase_merge_files(struct got_pathlist_head *merged_paths,
5976 const char *commit_ref_name, struct got_worktree *worktree,
5977 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
5978 struct got_object_id *commit_id, struct got_repository *repo,
5979 got_worktree_checkout_cb progress_cb, void *progress_arg,
5980 got_cancel_cb cancel_cb, void *cancel_arg)
5982 const struct got_error *err;
5983 struct got_reference *commit_ref = NULL;
5984 struct collect_merged_paths_arg cmp_arg;
5985 char *fileindex_path;
5987 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5989 err = get_fileindex_path(&fileindex_path, worktree);
5993 cmp_arg.progress_cb = progress_cb;
5994 cmp_arg.progress_arg = progress_arg;
5995 cmp_arg.merged_paths = merged_paths;
5996 err = merge_files(worktree, fileindex, fileindex_path,
5997 parent_commit_id, commit_id, repo, collect_merged_paths,
5998 &cmp_arg, cancel_cb, cancel_arg);
6000 got_ref_close(commit_ref);
6004 const struct got_error *
6005 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6006 struct got_worktree *worktree, struct got_fileindex *fileindex,
6007 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6008 struct got_repository *repo,
6009 got_worktree_checkout_cb progress_cb, void *progress_arg,
6010 got_cancel_cb cancel_cb, void *cancel_arg)
6012 const struct got_error *err;
6013 char *commit_ref_name;
6015 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6019 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6023 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6024 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6025 progress_arg, cancel_cb, cancel_arg);
6027 free(commit_ref_name);
6031 const struct got_error *
6032 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6033 struct got_worktree *worktree, struct got_fileindex *fileindex,
6034 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6035 struct got_repository *repo,
6036 got_worktree_checkout_cb progress_cb, void *progress_arg,
6037 got_cancel_cb cancel_cb, void *cancel_arg)
6039 const struct got_error *err;
6040 char *commit_ref_name;
6042 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6046 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6050 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6051 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6052 progress_arg, cancel_cb, cancel_arg);
6054 free(commit_ref_name);
6058 static const struct got_error *
6059 rebase_commit(struct got_object_id **new_commit_id,
6060 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6061 struct got_worktree *worktree, struct got_fileindex *fileindex,
6062 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6063 const char *new_logmsg, struct got_repository *repo)
6065 const struct got_error *err, *sync_err;
6066 struct got_pathlist_head commitable_paths;
6067 struct collect_commitables_arg cc_arg;
6068 char *fileindex_path = NULL;
6069 struct got_reference *head_ref = NULL;
6070 struct got_object_id *head_commit_id = NULL;
6071 char *logmsg = NULL;
6073 TAILQ_INIT(&commitable_paths);
6074 *new_commit_id = NULL;
6076 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6078 err = get_fileindex_path(&fileindex_path, worktree);
6082 cc_arg.commitable_paths = &commitable_paths;
6083 cc_arg.worktree = worktree;
6085 cc_arg.have_staged_files = 0;
6087 * If possible get the status of individual files directly to
6088 * avoid crawling the entire work tree once per rebased commit.
6089 * TODO: Ideally, merged_paths would contain a list of commitables
6090 * we could use so we could skip worktree_status() entirely.
6093 struct got_pathlist_entry *pe;
6094 TAILQ_FOREACH(pe, merged_paths, entry) {
6095 err = worktree_status(worktree, pe->path, fileindex,
6096 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6102 err = worktree_status(worktree, "", fileindex, repo,
6103 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6108 if (TAILQ_EMPTY(&commitable_paths)) {
6109 /* No-op change; commit will be elided. */
6110 err = got_ref_delete(commit_ref, repo);
6113 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6117 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6121 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6126 logmsg = strdup(new_logmsg);
6127 if (logmsg == NULL) {
6128 err = got_error_from_errno("strdup");
6132 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6137 /* NB: commit_worktree will call free(logmsg) */
6138 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6139 worktree, got_object_commit_get_author(orig_commit),
6140 got_object_commit_get_committer(orig_commit),
6141 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6145 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6149 err = got_ref_delete(commit_ref, repo);
6153 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
6155 sync_err = sync_fileindex(fileindex, fileindex_path);
6156 if (sync_err && err == NULL)
6159 free(fileindex_path);
6160 free(head_commit_id);
6162 got_ref_close(head_ref);
6164 free(*new_commit_id);
6165 *new_commit_id = NULL;
6170 const struct got_error *
6171 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6172 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6173 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6174 struct got_commit_object *orig_commit,
6175 struct got_object_id *orig_commit_id, struct got_repository *repo)
6177 const struct got_error *err;
6178 char *commit_ref_name;
6179 struct got_reference *commit_ref = NULL;
6180 struct got_object_id *commit_id = NULL;
6182 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6186 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6189 err = got_ref_resolve(&commit_id, repo, commit_ref);
6192 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6193 err = got_error(GOT_ERR_REBASE_COMMITID);
6197 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6198 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6201 got_ref_close(commit_ref);
6202 free(commit_ref_name);
6207 const struct got_error *
6208 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6209 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6210 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6211 struct got_commit_object *orig_commit,
6212 struct got_object_id *orig_commit_id, const char *new_logmsg,
6213 struct got_repository *repo)
6215 const struct got_error *err;
6216 char *commit_ref_name;
6217 struct got_reference *commit_ref = NULL;
6219 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6223 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6227 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6228 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6231 got_ref_close(commit_ref);
6232 free(commit_ref_name);
6236 const struct got_error *
6237 got_worktree_rebase_postpone(struct got_worktree *worktree,
6238 struct got_fileindex *fileindex)
6241 got_fileindex_free(fileindex);
6242 return lock_worktree(worktree, LOCK_SH);
6245 static const struct got_error *
6246 delete_ref(const char *name, struct got_repository *repo)
6248 const struct got_error *err;
6249 struct got_reference *ref;
6251 err = got_ref_open(&ref, repo, name, 0);
6253 if (err->code == GOT_ERR_NOT_REF)
6258 err = got_ref_delete(ref, repo);
6263 static const struct got_error *
6264 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6266 const struct got_error *err;
6267 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6268 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6270 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6273 err = delete_ref(tmp_branch_name, repo);
6277 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6280 err = delete_ref(new_base_branch_ref_name, repo);
6284 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6287 err = delete_ref(branch_ref_name, repo);
6291 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6294 err = delete_ref(commit_ref_name, repo);
6299 free(tmp_branch_name);
6300 free(new_base_branch_ref_name);
6301 free(branch_ref_name);
6302 free(commit_ref_name);
6306 const struct got_error *
6307 got_worktree_rebase_complete(struct got_worktree *worktree,
6308 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6309 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6310 struct got_repository *repo)
6312 const struct got_error *err, *unlockerr;
6313 struct got_object_id *new_head_commit_id = NULL;
6315 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6319 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6323 err = got_ref_write(rebased_branch, repo);
6327 err = got_worktree_set_head_ref(worktree, rebased_branch);
6331 err = delete_rebase_refs(worktree, repo);
6334 got_fileindex_free(fileindex);
6335 free(new_head_commit_id);
6336 unlockerr = lock_worktree(worktree, LOCK_SH);
6337 if (unlockerr && err == NULL)
6342 const struct got_error *
6343 got_worktree_rebase_abort(struct got_worktree *worktree,
6344 struct got_fileindex *fileindex, struct got_repository *repo,
6345 struct got_reference *new_base_branch,
6346 got_worktree_checkout_cb progress_cb, void *progress_arg)
6348 const struct got_error *err, *unlockerr, *sync_err;
6349 struct got_reference *resolved = NULL;
6350 struct got_object_id *commit_id = NULL;
6351 char *fileindex_path = NULL;
6352 struct revert_file_args rfa;
6353 struct got_object_id *tree_id = NULL;
6355 err = lock_worktree(worktree, LOCK_EX);
6359 err = got_ref_open(&resolved, repo,
6360 got_ref_get_symref_target(new_base_branch), 0);
6364 err = got_worktree_set_head_ref(worktree, resolved);
6369 * XXX commits to the base branch could have happened while
6370 * we were busy rebasing; should we store the original commit ID
6371 * when rebase begins and read it back here?
6373 err = got_ref_resolve(&commit_id, repo, resolved);
6377 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6381 err = got_object_id_by_path(&tree_id, repo,
6382 worktree->base_commit_id, worktree->path_prefix);
6386 err = delete_rebase_refs(worktree, repo);
6390 err = get_fileindex_path(&fileindex_path, worktree);
6394 rfa.worktree = worktree;
6395 rfa.fileindex = fileindex;
6396 rfa.progress_cb = progress_cb;
6397 rfa.progress_arg = progress_arg;
6398 rfa.patch_cb = NULL;
6399 rfa.patch_arg = NULL;
6401 err = worktree_status(worktree, "", fileindex, repo,
6402 revert_file, &rfa, NULL, NULL, 0, 0);
6406 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6407 repo, progress_cb, progress_arg, NULL, NULL);
6409 sync_err = sync_fileindex(fileindex, fileindex_path);
6410 if (sync_err && err == NULL)
6413 got_ref_close(resolved);
6417 got_fileindex_free(fileindex);
6418 free(fileindex_path);
6420 unlockerr = lock_worktree(worktree, LOCK_SH);
6421 if (unlockerr && err == NULL)
6426 const struct got_error *
6427 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6428 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6429 struct got_fileindex **fileindex, struct got_worktree *worktree,
6430 struct got_repository *repo)
6432 const struct got_error *err = NULL;
6433 char *tmp_branch_name = NULL;
6434 char *branch_ref_name = NULL;
6435 char *base_commit_ref_name = NULL;
6436 char *fileindex_path = NULL;
6437 struct check_rebase_ok_arg ok_arg;
6438 struct got_reference *wt_branch = NULL;
6439 struct got_reference *base_commit_ref = NULL;
6443 *base_commit_id = NULL;
6446 err = lock_worktree(worktree, LOCK_EX);
6450 err = open_fileindex(fileindex, &fileindex_path, worktree);
6454 ok_arg.worktree = worktree;
6456 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6461 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6465 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6469 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6474 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6479 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6483 err = got_ref_write(*branch_ref, repo);
6487 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6488 worktree->base_commit_id);
6491 err = got_ref_write(base_commit_ref, repo);
6494 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6495 if (*base_commit_id == NULL) {
6496 err = got_error_from_errno("got_object_id_dup");
6500 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6501 worktree->base_commit_id);
6504 err = got_ref_write(*tmp_branch, repo);
6508 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6512 free(fileindex_path);
6513 free(tmp_branch_name);
6514 free(branch_ref_name);
6515 free(base_commit_ref_name);
6517 got_ref_close(wt_branch);
6520 got_ref_close(*branch_ref);
6524 got_ref_close(*tmp_branch);
6527 free(*base_commit_id);
6529 got_fileindex_free(*fileindex);
6532 lock_worktree(worktree, LOCK_SH);
6537 const struct got_error *
6538 got_worktree_histedit_postpone(struct got_worktree *worktree,
6539 struct got_fileindex *fileindex)
6542 got_fileindex_free(fileindex);
6543 return lock_worktree(worktree, LOCK_SH);
6546 const struct got_error *
6547 got_worktree_histedit_in_progress(int *in_progress,
6548 struct got_worktree *worktree)
6550 const struct got_error *err;
6551 char *tmp_branch_name = NULL;
6553 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6557 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6558 free(tmp_branch_name);
6562 const struct got_error *
6563 got_worktree_histedit_continue(struct got_object_id **commit_id,
6564 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6565 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6566 struct got_worktree *worktree, struct got_repository *repo)
6568 const struct got_error *err;
6569 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6570 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6571 struct got_reference *commit_ref = NULL;
6572 struct got_reference *base_commit_ref = NULL;
6573 char *fileindex_path = NULL;
6574 int have_staged_files = 0;
6578 *base_commit_id = NULL;
6581 err = lock_worktree(worktree, LOCK_EX);
6585 err = open_fileindex(fileindex, &fileindex_path, worktree);
6589 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6590 &have_staged_files);
6591 if (err && err->code != GOT_ERR_CANCELLED)
6593 if (have_staged_files) {
6594 err = got_error(GOT_ERR_STAGED_PATHS);
6598 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6602 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6606 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6610 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6615 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6619 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6622 err = got_ref_resolve(commit_id, repo, commit_ref);
6626 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6629 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6633 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6637 free(commit_ref_name);
6638 free(branch_ref_name);
6639 free(fileindex_path);
6641 got_ref_close(commit_ref);
6642 if (base_commit_ref)
6643 got_ref_close(base_commit_ref);
6647 free(*base_commit_id);
6648 *base_commit_id = NULL;
6650 got_ref_close(*tmp_branch);
6654 got_fileindex_free(*fileindex);
6657 lock_worktree(worktree, LOCK_EX);
6662 static const struct got_error *
6663 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6665 const struct got_error *err;
6666 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6667 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6669 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6672 err = delete_ref(tmp_branch_name, repo);
6676 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6680 err = delete_ref(base_commit_ref_name, repo);
6684 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6687 err = delete_ref(branch_ref_name, repo);
6691 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6694 err = delete_ref(commit_ref_name, repo);
6698 free(tmp_branch_name);
6699 free(base_commit_ref_name);
6700 free(branch_ref_name);
6701 free(commit_ref_name);
6705 const struct got_error *
6706 got_worktree_histedit_abort(struct got_worktree *worktree,
6707 struct got_fileindex *fileindex, struct got_repository *repo,
6708 struct got_reference *branch, struct got_object_id *base_commit_id,
6709 got_worktree_checkout_cb progress_cb, void *progress_arg)
6711 const struct got_error *err, *unlockerr, *sync_err;
6712 struct got_reference *resolved = NULL;
6713 char *fileindex_path = NULL;
6714 struct got_object_id *tree_id = NULL;
6715 struct revert_file_args rfa;
6717 err = lock_worktree(worktree, LOCK_EX);
6721 err = got_ref_open(&resolved, repo,
6722 got_ref_get_symref_target(branch), 0);
6726 err = got_worktree_set_head_ref(worktree, resolved);
6730 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6734 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6735 worktree->path_prefix);
6739 err = delete_histedit_refs(worktree, repo);
6743 err = get_fileindex_path(&fileindex_path, worktree);
6747 rfa.worktree = worktree;
6748 rfa.fileindex = fileindex;
6749 rfa.progress_cb = progress_cb;
6750 rfa.progress_arg = progress_arg;
6751 rfa.patch_cb = NULL;
6752 rfa.patch_arg = NULL;
6754 err = worktree_status(worktree, "", fileindex, repo,
6755 revert_file, &rfa, NULL, NULL, 0, 0);
6759 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6760 repo, progress_cb, progress_arg, NULL, NULL);
6762 sync_err = sync_fileindex(fileindex, fileindex_path);
6763 if (sync_err && err == NULL)
6766 got_ref_close(resolved);
6768 free(fileindex_path);
6770 unlockerr = lock_worktree(worktree, LOCK_SH);
6771 if (unlockerr && err == NULL)
6776 const struct got_error *
6777 got_worktree_histedit_complete(struct got_worktree *worktree,
6778 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6779 struct got_reference *edited_branch, struct got_repository *repo)
6781 const struct got_error *err, *unlockerr;
6782 struct got_object_id *new_head_commit_id = NULL;
6783 struct got_reference *resolved = NULL;
6785 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6789 err = got_ref_open(&resolved, repo,
6790 got_ref_get_symref_target(edited_branch), 0);
6794 err = got_ref_change_ref(resolved, new_head_commit_id);
6798 err = got_ref_write(resolved, repo);
6802 err = got_worktree_set_head_ref(worktree, resolved);
6806 err = delete_histedit_refs(worktree, repo);
6809 got_fileindex_free(fileindex);
6810 free(new_head_commit_id);
6811 unlockerr = lock_worktree(worktree, LOCK_SH);
6812 if (unlockerr && err == NULL)
6817 const struct got_error *
6818 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6819 struct got_object_id *commit_id, struct got_repository *repo)
6821 const struct got_error *err;
6822 char *commit_ref_name;
6824 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6828 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6832 err = delete_ref(commit_ref_name, repo);
6834 free(commit_ref_name);
6838 const struct got_error *
6839 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6840 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6841 struct got_worktree *worktree, const char *refname,
6842 struct got_repository *repo)
6844 const struct got_error *err = NULL;
6845 char *fileindex_path = NULL;
6846 struct check_rebase_ok_arg ok_arg;
6850 *base_branch_ref = NULL;
6852 err = lock_worktree(worktree, LOCK_EX);
6856 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6857 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6858 "cannot integrate a branch into itself; "
6859 "update -b or different branch name required");
6863 err = open_fileindex(fileindex, &fileindex_path, worktree);
6867 /* Preconditions are the same as for rebase. */
6868 ok_arg.worktree = worktree;
6870 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6875 err = got_ref_open(branch_ref, repo, refname, 1);
6879 err = got_ref_open(base_branch_ref, repo,
6880 got_worktree_get_head_ref_name(worktree), 1);
6884 got_ref_close(*branch_ref);
6887 if (*base_branch_ref) {
6888 got_ref_close(*base_branch_ref);
6889 *base_branch_ref = NULL;
6892 got_fileindex_free(*fileindex);
6895 lock_worktree(worktree, LOCK_SH);
6900 const struct got_error *
6901 got_worktree_integrate_continue(struct got_worktree *worktree,
6902 struct got_fileindex *fileindex, struct got_repository *repo,
6903 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6904 got_worktree_checkout_cb progress_cb, void *progress_arg,
6905 got_cancel_cb cancel_cb, void *cancel_arg)
6907 const struct got_error *err = NULL, *sync_err, *unlockerr;
6908 char *fileindex_path = NULL;
6909 struct got_object_id *tree_id = NULL, *commit_id = NULL;
6911 err = get_fileindex_path(&fileindex_path, worktree);
6915 err = got_ref_resolve(&commit_id, repo, branch_ref);
6919 err = got_object_id_by_path(&tree_id, repo, commit_id,
6920 worktree->path_prefix);
6924 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6928 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6929 progress_cb, progress_arg, cancel_cb, cancel_arg);
6933 err = got_ref_change_ref(base_branch_ref, commit_id);
6937 err = got_ref_write(base_branch_ref, repo);
6939 sync_err = sync_fileindex(fileindex, fileindex_path);
6940 if (sync_err && err == NULL)
6944 unlockerr = got_ref_unlock(branch_ref);
6945 if (unlockerr && err == NULL)
6947 got_ref_close(branch_ref);
6949 unlockerr = got_ref_unlock(base_branch_ref);
6950 if (unlockerr && err == NULL)
6952 got_ref_close(base_branch_ref);
6954 got_fileindex_free(fileindex);
6955 free(fileindex_path);
6958 unlockerr = lock_worktree(worktree, LOCK_SH);
6959 if (unlockerr && err == NULL)
6964 const struct got_error *
6965 got_worktree_integrate_abort(struct got_worktree *worktree,
6966 struct got_fileindex *fileindex, struct got_repository *repo,
6967 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
6969 const struct got_error *err = NULL, *unlockerr = NULL;
6971 got_fileindex_free(fileindex);
6973 err = lock_worktree(worktree, LOCK_SH);
6975 unlockerr = got_ref_unlock(branch_ref);
6976 if (unlockerr && err == NULL)
6978 got_ref_close(branch_ref);
6980 unlockerr = got_ref_unlock(base_branch_ref);
6981 if (unlockerr && err == NULL)
6983 got_ref_close(base_branch_ref);
6988 struct check_stage_ok_arg {
6989 struct got_object_id *head_commit_id;
6990 struct got_worktree *worktree;
6991 struct got_fileindex *fileindex;
6992 struct got_repository *repo;
6996 const struct got_error *
6997 check_stage_ok(void *arg, unsigned char status,
6998 unsigned char staged_status, const char *relpath,
6999 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7000 struct got_object_id *commit_id, int dirfd, const char *de_name)
7002 struct check_stage_ok_arg *a = arg;
7003 const struct got_error *err = NULL;
7004 struct got_fileindex_entry *ie;
7005 struct got_object_id base_commit_id;
7006 struct got_object_id *base_commit_idp = NULL;
7007 char *in_repo_path = NULL, *p;
7009 if (status == GOT_STATUS_UNVERSIONED ||
7010 status == GOT_STATUS_NO_CHANGE)
7012 if (status == GOT_STATUS_NONEXISTENT)
7013 return got_error_set_errno(ENOENT, relpath);
7015 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7017 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7019 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7020 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7022 return got_error_from_errno("asprintf");
7024 if (got_fileindex_entry_has_commit(ie)) {
7025 memcpy(base_commit_id.sha1, ie->commit_sha1,
7026 SHA1_DIGEST_LENGTH);
7027 base_commit_idp = &base_commit_id;
7030 if (status == GOT_STATUS_CONFLICT) {
7031 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7033 } else if (status != GOT_STATUS_ADD &&
7034 status != GOT_STATUS_MODIFY &&
7035 status != GOT_STATUS_DELETE) {
7036 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7040 a->have_changes = 1;
7045 err = check_out_of_date(p, status, staged_status,
7046 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7047 GOT_ERR_STAGE_OUT_OF_DATE);
7053 struct stage_path_arg {
7054 struct got_worktree *worktree;
7055 struct got_fileindex *fileindex;
7056 struct got_repository *repo;
7057 got_worktree_status_cb status_cb;
7059 got_worktree_patch_cb patch_cb;
7061 int staged_something;
7062 int allow_bad_symlinks;
7065 static const struct got_error *
7066 stage_path(void *arg, unsigned char status,
7067 unsigned char staged_status, const char *relpath,
7068 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7069 struct got_object_id *commit_id, int dirfd, const char *de_name)
7071 struct stage_path_arg *a = arg;
7072 const struct got_error *err = NULL;
7073 struct got_fileindex_entry *ie;
7074 char *ondisk_path = NULL, *path_content = NULL;
7076 struct got_object_id *new_staged_blob_id = NULL;
7079 if (status == GOT_STATUS_UNVERSIONED)
7082 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7084 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7086 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7088 return got_error_from_errno("asprintf");
7091 case GOT_STATUS_ADD:
7092 case GOT_STATUS_MODIFY:
7093 /* XXX could sb.st_mode be passed in by our caller? */
7094 if (lstat(ondisk_path, &sb) == -1) {
7095 err = got_error_from_errno2("lstat", ondisk_path);
7099 if (status == GOT_STATUS_ADD) {
7100 int choice = GOT_PATCH_CHOICE_NONE;
7101 err = (*a->patch_cb)(&choice, a->patch_arg,
7102 status, ie->path, NULL, 1, 1);
7105 if (choice != GOT_PATCH_CHOICE_YES)
7108 err = create_patched_content(&path_content, 0,
7109 staged_blob_id ? staged_blob_id : blob_id,
7110 ondisk_path, dirfd, de_name, ie->path,
7111 a->repo, a->patch_cb, a->patch_arg);
7112 if (err || path_content == NULL)
7116 err = got_object_blob_create(&new_staged_blob_id,
7117 path_content ? path_content : ondisk_path, a->repo);
7120 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7121 SHA1_DIGEST_LENGTH);
7122 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7123 stage = GOT_FILEIDX_STAGE_ADD;
7125 stage = GOT_FILEIDX_STAGE_MODIFY;
7126 got_fileindex_entry_stage_set(ie, stage);
7127 if (S_ISLNK(sb.st_mode)) {
7128 int is_bad_symlink = 0;
7129 if (!a->allow_bad_symlinks) {
7130 char target_path[PATH_MAX];
7132 target_len = readlink(ondisk_path, target_path,
7133 sizeof(target_path));
7134 if (target_len == -1) {
7135 err = got_error_from_errno2("readlink",
7139 err = is_bad_symlink_target(&is_bad_symlink,
7140 target_path, target_len, ondisk_path,
7141 a->worktree->root_path);
7144 if (is_bad_symlink) {
7145 err = got_error_path(ondisk_path,
7146 GOT_ERR_BAD_SYMLINK);
7151 got_fileindex_entry_staged_filetype_set(ie,
7152 GOT_FILEIDX_MODE_BAD_SYMLINK);
7154 got_fileindex_entry_staged_filetype_set(ie,
7155 GOT_FILEIDX_MODE_SYMLINK);
7157 got_fileindex_entry_staged_filetype_set(ie,
7158 GOT_FILEIDX_MODE_REGULAR_FILE);
7160 a->staged_something = 1;
7161 if (a->status_cb == NULL)
7163 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7164 get_staged_status(ie), relpath, blob_id,
7165 new_staged_blob_id, NULL, dirfd, de_name);
7167 case GOT_STATUS_DELETE:
7168 if (staged_status == GOT_STATUS_DELETE)
7171 int choice = GOT_PATCH_CHOICE_NONE;
7172 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7173 ie->path, NULL, 1, 1);
7176 if (choice == GOT_PATCH_CHOICE_NO)
7178 if (choice != GOT_PATCH_CHOICE_YES) {
7179 err = got_error(GOT_ERR_PATCH_CHOICE);
7183 stage = GOT_FILEIDX_STAGE_DELETE;
7184 got_fileindex_entry_stage_set(ie, stage);
7185 a->staged_something = 1;
7186 if (a->status_cb == NULL)
7188 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7189 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7192 case GOT_STATUS_NO_CHANGE:
7194 case GOT_STATUS_CONFLICT:
7195 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7197 case GOT_STATUS_NONEXISTENT:
7198 err = got_error_set_errno(ENOENT, relpath);
7201 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7205 if (path_content && unlink(path_content) == -1 && err == NULL)
7206 err = got_error_from_errno2("unlink", path_content);
7209 free(new_staged_blob_id);
7213 const struct got_error *
7214 got_worktree_stage(struct got_worktree *worktree,
7215 struct got_pathlist_head *paths,
7216 got_worktree_status_cb status_cb, void *status_arg,
7217 got_worktree_patch_cb patch_cb, void *patch_arg,
7218 int allow_bad_symlinks, struct got_repository *repo)
7220 const struct got_error *err = NULL, *sync_err, *unlockerr;
7221 struct got_pathlist_entry *pe;
7222 struct got_fileindex *fileindex = NULL;
7223 char *fileindex_path = NULL;
7224 struct got_reference *head_ref = NULL;
7225 struct got_object_id *head_commit_id = NULL;
7226 struct check_stage_ok_arg oka;
7227 struct stage_path_arg spa;
7229 err = lock_worktree(worktree, LOCK_EX);
7233 err = got_ref_open(&head_ref, repo,
7234 got_worktree_get_head_ref_name(worktree), 0);
7237 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7240 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7244 /* Check pre-conditions before staging anything. */
7245 oka.head_commit_id = head_commit_id;
7246 oka.worktree = worktree;
7247 oka.fileindex = fileindex;
7249 oka.have_changes = 0;
7250 TAILQ_FOREACH(pe, paths, entry) {
7251 err = worktree_status(worktree, pe->path, fileindex, repo,
7252 check_stage_ok, &oka, NULL, NULL, 0, 0);
7256 if (!oka.have_changes) {
7257 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7261 spa.worktree = worktree;
7262 spa.fileindex = fileindex;
7264 spa.patch_cb = patch_cb;
7265 spa.patch_arg = patch_arg;
7266 spa.status_cb = status_cb;
7267 spa.status_arg = status_arg;
7268 spa.staged_something = 0;
7269 spa.allow_bad_symlinks = allow_bad_symlinks;
7270 TAILQ_FOREACH(pe, paths, entry) {
7271 err = worktree_status(worktree, pe->path, fileindex, repo,
7272 stage_path, &spa, NULL, NULL, 0, 0);
7276 if (!spa.staged_something) {
7277 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7281 sync_err = sync_fileindex(fileindex, fileindex_path);
7282 if (sync_err && err == NULL)
7286 got_ref_close(head_ref);
7287 free(head_commit_id);
7288 free(fileindex_path);
7290 got_fileindex_free(fileindex);
7291 unlockerr = lock_worktree(worktree, LOCK_SH);
7292 if (unlockerr && err == NULL)
7297 struct unstage_path_arg {
7298 struct got_worktree *worktree;
7299 struct got_fileindex *fileindex;
7300 struct got_repository *repo;
7301 got_worktree_checkout_cb progress_cb;
7303 got_worktree_patch_cb patch_cb;
7307 static const struct got_error *
7308 create_unstaged_content(char **path_unstaged_content,
7309 char **path_new_staged_content, struct got_object_id *blob_id,
7310 struct got_object_id *staged_blob_id, const char *relpath,
7311 struct got_repository *repo,
7312 got_worktree_patch_cb patch_cb, void *patch_arg)
7314 const struct got_error *err;
7315 struct got_blob_object *blob = NULL, *staged_blob = NULL;
7316 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7317 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7318 struct stat sb1, sb2;
7319 struct got_diff_changes *changes = NULL;
7320 struct got_diff_state *ds = NULL;
7321 struct got_diff_args *args = NULL;
7322 struct got_diff_change *change;
7323 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
7324 int have_content = 0, have_rejected_content = 0;
7326 *path_unstaged_content = NULL;
7327 *path_new_staged_content = NULL;
7329 err = got_object_id_str(&label1, blob_id);
7332 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7336 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7340 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7344 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7348 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7352 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7356 if (stat(path1, &sb1) == -1) {
7357 err = got_error_from_errno2("stat", path1);
7361 if (stat(path2, &sb2) == -1) {
7362 err = got_error_from_errno2("stat", path2);
7366 err = got_diff_files(&changes, &ds, &args, &diff_flags,
7367 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
7371 err = got_opentemp_named(path_unstaged_content, &outfile,
7372 "got-unstaged-content");
7375 err = got_opentemp_named(path_new_staged_content, &rejectfile,
7376 "got-new-staged-content");
7380 if (fseek(f1, 0L, SEEK_SET) == -1) {
7381 err = got_ferror(f1, GOT_ERR_IO);
7384 if (fseek(f2, 0L, SEEK_SET) == -1) {
7385 err = got_ferror(f2, GOT_ERR_IO);
7388 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
7390 err = apply_or_reject_change(&choice, change, ++n,
7391 changes->nchanges, ds, args, diff_flags, relpath,
7392 f1, f2, &line_cur1, &line_cur2,
7393 outfile, rejectfile, patch_cb, patch_arg);
7396 if (choice == GOT_PATCH_CHOICE_YES)
7399 have_rejected_content = 1;
7400 if (choice == GOT_PATCH_CHOICE_QUIT)
7403 if (have_content || have_rejected_content)
7404 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7405 outfile, rejectfile);
7409 got_object_blob_close(blob);
7411 got_object_blob_close(staged_blob);
7412 if (f1 && fclose(f1) == EOF && err == NULL)
7413 err = got_error_from_errno2("fclose", path1);
7414 if (f2 && fclose(f2) == EOF && err == NULL)
7415 err = got_error_from_errno2("fclose", path2);
7416 if (outfile && fclose(outfile) == EOF && err == NULL)
7417 err = got_error_from_errno2("fclose", *path_unstaged_content);
7418 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7419 err = got_error_from_errno2("fclose", *path_new_staged_content);
7420 if (path1 && unlink(path1) == -1 && err == NULL)
7421 err = got_error_from_errno2("unlink", path1);
7422 if (path2 && unlink(path2) == -1 && err == NULL)
7423 err = got_error_from_errno2("unlink", path2);
7424 if (err || !have_content) {
7425 if (*path_unstaged_content &&
7426 unlink(*path_unstaged_content) == -1 && err == NULL)
7427 err = got_error_from_errno2("unlink",
7428 *path_unstaged_content);
7429 free(*path_unstaged_content);
7430 *path_unstaged_content = NULL;
7432 if (err || !have_content || !have_rejected_content) {
7433 if (*path_new_staged_content &&
7434 unlink(*path_new_staged_content) == -1 && err == NULL)
7435 err = got_error_from_errno2("unlink",
7436 *path_new_staged_content);
7437 free(*path_new_staged_content);
7438 *path_new_staged_content = NULL;
7442 got_diff_state_free(ds);
7446 got_diff_free_changes(changes);
7452 static const struct got_error *
7453 unstage_hunks(struct got_object_id *staged_blob_id,
7454 struct got_blob_object *blob_base,
7455 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
7456 const char *ondisk_path, const char *label_orig,
7457 struct got_worktree *worktree, struct got_repository *repo,
7458 got_worktree_patch_cb patch_cb, void *patch_arg,
7459 got_worktree_checkout_cb progress_cb, void *progress_arg)
7461 const struct got_error *err = NULL;
7462 char *path_unstaged_content = NULL;
7463 char *path_new_staged_content = NULL;
7464 struct got_object_id *new_staged_blob_id = NULL;
7468 err = create_unstaged_content(&path_unstaged_content,
7469 &path_new_staged_content, blob_id, staged_blob_id,
7470 ie->path, repo, patch_cb, patch_arg);
7474 if (path_unstaged_content == NULL)
7477 if (path_new_staged_content) {
7478 err = got_object_blob_create(&new_staged_blob_id,
7479 path_new_staged_content, repo);
7484 f = fopen(path_unstaged_content, "r");
7486 err = got_error_from_errno2("fopen",
7487 path_unstaged_content);
7490 if (fstat(fileno(f), &sb) == -1) {
7491 err = got_error_from_errno2("fstat", path_unstaged_content);
7494 if (got_fileindex_entry_staged_filetype_get(ie) ==
7495 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
7496 char link_target[PATH_MAX];
7498 r = fread(link_target, 1, sizeof(link_target), f);
7499 if (r == 0 && ferror(f)) {
7500 err = got_error_from_errno("fread");
7503 if (r >= sizeof(link_target)) { /* should not happen */
7504 err = got_error(GOT_ERR_NO_SPACE);
7507 link_target[r] = '\0';
7508 err = merge_symlink(worktree, blob_base,
7509 ondisk_path, ie->path, label_orig, link_target,
7510 worktree->base_commit_id, repo, progress_cb,
7513 int local_changes_subsumed;
7514 err = merge_file(&local_changes_subsumed, worktree,
7515 blob_base, ondisk_path, ie->path,
7516 got_fileindex_perms_to_st(ie),
7517 path_unstaged_content, label_orig, "unstaged",
7518 repo, progress_cb, progress_arg);
7523 if (new_staged_blob_id) {
7524 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7525 SHA1_DIGEST_LENGTH);
7527 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7529 free(new_staged_blob_id);
7530 if (path_unstaged_content &&
7531 unlink(path_unstaged_content) == -1 && err == NULL)
7532 err = got_error_from_errno2("unlink", path_unstaged_content);
7533 if (path_new_staged_content &&
7534 unlink(path_new_staged_content) == -1 && err == NULL)
7535 err = got_error_from_errno2("unlink", path_new_staged_content);
7536 if (f && fclose(f) != 0 && err == NULL)
7537 err = got_error_from_errno2("fclose", path_unstaged_content);
7538 free(path_unstaged_content);
7539 free(path_new_staged_content);
7543 static const struct got_error *
7544 unstage_path(void *arg, unsigned char status,
7545 unsigned char staged_status, const char *relpath,
7546 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7547 struct got_object_id *commit_id, int dirfd, const char *de_name)
7549 const struct got_error *err = NULL;
7550 struct unstage_path_arg *a = arg;
7551 struct got_fileindex_entry *ie;
7552 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7553 char *ondisk_path = NULL;
7554 char *id_str = NULL, *label_orig = NULL;
7555 int local_changes_subsumed;
7558 if (staged_status != GOT_STATUS_ADD &&
7559 staged_status != GOT_STATUS_MODIFY &&
7560 staged_status != GOT_STATUS_DELETE)
7563 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7565 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7567 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7569 return got_error_from_errno("asprintf");
7571 err = got_object_id_str(&id_str,
7572 commit_id ? commit_id : a->worktree->base_commit_id);
7575 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7577 err = got_error_from_errno("asprintf");
7581 switch (staged_status) {
7582 case GOT_STATUS_MODIFY:
7583 err = got_object_open_as_blob(&blob_base, a->repo,
7588 case GOT_STATUS_ADD:
7590 if (staged_status == GOT_STATUS_ADD) {
7591 int choice = GOT_PATCH_CHOICE_NONE;
7592 err = (*a->patch_cb)(&choice, a->patch_arg,
7593 staged_status, ie->path, NULL, 1, 1);
7596 if (choice != GOT_PATCH_CHOICE_YES)
7599 err = unstage_hunks(staged_blob_id,
7600 blob_base, blob_id, ie, ondisk_path,
7601 label_orig, a->worktree, a->repo,
7602 a->patch_cb, a->patch_arg,
7603 a->progress_cb, a->progress_arg);
7604 break; /* Done with this file. */
7607 err = got_object_open_as_blob(&blob_staged, a->repo,
7608 staged_blob_id, 8192);
7611 switch (got_fileindex_entry_staged_filetype_get(ie)) {
7612 case GOT_FILEIDX_MODE_BAD_SYMLINK:
7613 case GOT_FILEIDX_MODE_REGULAR_FILE:
7614 err = merge_blob(&local_changes_subsumed, a->worktree,
7615 blob_base, ondisk_path, relpath,
7616 got_fileindex_perms_to_st(ie), label_orig,
7617 blob_staged, commit_id ? commit_id :
7618 a->worktree->base_commit_id, a->repo,
7619 a->progress_cb, a->progress_arg);
7621 case GOT_FILEIDX_MODE_SYMLINK:
7622 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7623 char *staged_target;
7624 err = got_object_blob_read_to_str(
7625 &staged_target, blob_staged);
7628 err = merge_symlink(a->worktree, blob_base,
7629 ondisk_path, relpath, label_orig,
7630 staged_target, commit_id ? commit_id :
7631 a->worktree->base_commit_id,
7632 a->repo, a->progress_cb, a->progress_arg);
7633 free(staged_target);
7635 err = merge_blob(&local_changes_subsumed,
7636 a->worktree, blob_base, ondisk_path,
7637 relpath, got_fileindex_perms_to_st(ie),
7638 label_orig, blob_staged,
7639 commit_id ? commit_id :
7640 a->worktree->base_commit_id, a->repo,
7641 a->progress_cb, a->progress_arg);
7645 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
7649 got_fileindex_entry_stage_set(ie,
7650 GOT_FILEIDX_STAGE_NONE);
7652 case GOT_STATUS_DELETE:
7654 int choice = GOT_PATCH_CHOICE_NONE;
7655 err = (*a->patch_cb)(&choice, a->patch_arg,
7656 staged_status, ie->path, NULL, 1, 1);
7659 if (choice == GOT_PATCH_CHOICE_NO)
7661 if (choice != GOT_PATCH_CHOICE_YES) {
7662 err = got_error(GOT_ERR_PATCH_CHOICE);
7666 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7667 err = get_file_status(&status, &sb, ie, ondisk_path,
7668 dirfd, de_name, a->repo);
7671 err = (*a->progress_cb)(a->progress_arg, status, relpath);
7677 got_object_blob_close(blob_base);
7679 got_object_blob_close(blob_staged);
7685 const struct got_error *
7686 got_worktree_unstage(struct got_worktree *worktree,
7687 struct got_pathlist_head *paths,
7688 got_worktree_checkout_cb progress_cb, void *progress_arg,
7689 got_worktree_patch_cb patch_cb, void *patch_arg,
7690 struct got_repository *repo)
7692 const struct got_error *err = NULL, *sync_err, *unlockerr;
7693 struct got_pathlist_entry *pe;
7694 struct got_fileindex *fileindex = NULL;
7695 char *fileindex_path = NULL;
7696 struct unstage_path_arg upa;
7698 err = lock_worktree(worktree, LOCK_EX);
7702 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7706 upa.worktree = worktree;
7707 upa.fileindex = fileindex;
7709 upa.progress_cb = progress_cb;
7710 upa.progress_arg = progress_arg;
7711 upa.patch_cb = patch_cb;
7712 upa.patch_arg = patch_arg;
7713 TAILQ_FOREACH(pe, paths, entry) {
7714 err = worktree_status(worktree, pe->path, fileindex, repo,
7715 unstage_path, &upa, NULL, NULL, 0, 0);
7720 sync_err = sync_fileindex(fileindex, fileindex_path);
7721 if (sync_err && err == NULL)
7724 free(fileindex_path);
7726 got_fileindex_free(fileindex);
7727 unlockerr = lock_worktree(worktree, LOCK_SH);
7728 if (unlockerr && err == NULL)
7733 struct report_file_info_arg {
7734 struct got_worktree *worktree;
7735 got_worktree_path_info_cb info_cb;
7737 struct got_pathlist_head *paths;
7738 got_cancel_cb cancel_cb;
7742 static const struct got_error *
7743 report_file_info(void *arg, struct got_fileindex_entry *ie)
7745 struct report_file_info_arg *a = arg;
7746 struct got_pathlist_entry *pe;
7747 struct got_object_id blob_id, staged_blob_id, commit_id;
7748 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
7749 struct got_object_id *commit_idp = NULL;
7752 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
7753 return got_error(GOT_ERR_CANCELLED);
7755 TAILQ_FOREACH(pe, a->paths, entry) {
7756 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
7757 got_path_is_child(ie->path, pe->path, pe->path_len))
7760 if (pe == NULL) /* not found */
7763 if (got_fileindex_entry_has_blob(ie)) {
7764 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
7765 blob_idp = &blob_id;
7767 stage = got_fileindex_entry_stage_get(ie);
7768 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
7769 stage == GOT_FILEIDX_STAGE_ADD) {
7770 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
7771 SHA1_DIGEST_LENGTH);
7772 staged_blob_idp = &staged_blob_id;
7775 if (got_fileindex_entry_has_commit(ie)) {
7776 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
7777 commit_idp = &commit_id;
7780 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
7781 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
7784 const struct got_error *
7785 got_worktree_path_info(struct got_worktree *worktree,
7786 struct got_pathlist_head *paths,
7787 got_worktree_path_info_cb info_cb, void *info_arg,
7788 got_cancel_cb cancel_cb, void *cancel_arg)
7791 const struct got_error *err = NULL, *unlockerr;
7792 struct got_fileindex *fileindex = NULL;
7793 char *fileindex_path = NULL;
7794 struct report_file_info_arg arg;
7796 err = lock_worktree(worktree, LOCK_SH);
7800 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7804 arg.worktree = worktree;
7805 arg.info_cb = info_cb;
7806 arg.info_arg = info_arg;
7808 arg.cancel_cb = cancel_cb;
7809 arg.cancel_arg = cancel_arg;
7810 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
7813 free(fileindex_path);
7815 got_fileindex_free(fileindex);
7816 unlockerr = lock_worktree(worktree, LOCK_UN);
7817 if (unlockerr && err == NULL)