2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/queue.h>
38 #include "got_error.h"
39 #include "got_repository.h"
40 #include "got_reference.h"
41 #include "got_object.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
45 #include "got_opentemp.h"
48 #include "got_lib_worktree.h"
49 #include "got_lib_sha1.h"
50 #include "got_lib_fileindex.h"
51 #include "got_lib_inflate.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_object_idset.h"
57 #include "got_lib_diff.h"
58 #include "got_lib_gotconfig.h"
61 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
64 #define GOT_MERGE_LABEL_MERGED "merged change"
65 #define GOT_MERGE_LABEL_BASE "3-way merge base"
67 static const struct got_error *
68 create_meta_file(const char *path_got, const char *name, const char *content)
70 const struct got_error *err = NULL;
73 if (asprintf(&path, "%s/%s", path_got, name) == -1)
74 return got_error_from_errno("asprintf");
76 err = got_path_create_file(path, content);
81 static const struct got_error *
82 update_meta_file(const char *path_got, const char *name, const char *content)
84 const struct got_error *err = NULL;
89 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
90 err = got_error_from_errno("asprintf");
95 err = got_opentemp_named(&tmppath, &tmpfile, path);
100 int len = fprintf(tmpfile, "%s\n", content);
101 if (len != strlen(content) + 1) {
102 err = got_error_from_errno2("fprintf", tmppath);
107 if (rename(tmppath, path) != 0) {
108 err = got_error_from_errno3("rename", tmppath, path);
114 if (fclose(tmpfile) != 0 && err == NULL)
115 err = got_error_from_errno2("fclose", tmppath);
120 static const struct got_error *
121 read_meta_file(char **content, const char *path_got, const char *name)
123 const struct got_error *err = NULL;
131 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
132 err = got_error_from_errno("asprintf");
137 fd = open(path, O_RDONLY | O_NOFOLLOW);
140 err = got_error_path(path, GOT_ERR_WORKTREE_META);
142 err = got_error_from_errno2("open", path);
145 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
146 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
147 : got_error_from_errno2("flock", path));
151 if (fstat(fd, &sb) != 0) {
152 err = got_error_from_errno2("fstat", path);
155 *content = calloc(1, sb.st_size);
156 if (*content == NULL) {
157 err = got_error_from_errno("calloc");
161 n = read(fd, *content, sb.st_size);
162 if (n != sb.st_size) {
163 err = (n == -1 ? got_error_from_errno2("read", path) :
164 got_error_path(path, GOT_ERR_WORKTREE_META));
167 if ((*content)[sb.st_size - 1] != '\n') {
168 err = got_error_path(path, GOT_ERR_WORKTREE_META);
171 (*content)[sb.st_size - 1] = '\0';
174 if (fd != -1 && close(fd) == -1 && err == NULL)
175 err = got_error_from_errno2("close", path_got);
184 static const struct got_error *
185 write_head_ref(const char *path_got, struct got_reference *head_ref)
187 const struct got_error *err = NULL;
190 if (got_ref_is_symbolic(head_ref)) {
191 refstr = got_ref_to_str(head_ref);
193 return got_error_from_errno("got_ref_to_str");
195 refstr = strdup(got_ref_get_name(head_ref));
197 return got_error_from_errno("strdup");
199 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
204 const struct got_error *
205 got_worktree_init(const char *path, struct got_reference *head_ref,
206 const char *prefix, struct got_repository *repo)
208 const struct got_error *err = NULL;
209 struct got_object_id *commit_id = NULL;
211 uint32_t uuid_status;
213 char *path_got = NULL;
214 char *formatstr = NULL;
215 char *absprefix = NULL;
216 char *basestr = NULL;
217 char *uuidstr = NULL;
219 if (strcmp(path, got_repo_get_path(repo)) == 0) {
220 err = got_error(GOT_ERR_WORKTREE_REPO);
224 err = got_ref_resolve(&commit_id, repo, head_ref);
227 err = got_object_get_type(&obj_type, repo, commit_id);
230 if (obj_type != GOT_OBJ_TYPE_COMMIT)
231 return got_error(GOT_ERR_OBJ_TYPE);
233 if (!got_path_is_absolute(prefix)) {
234 if (asprintf(&absprefix, "/%s", prefix) == -1)
235 return got_error_from_errno("asprintf");
238 /* Create top-level directory (may already exist). */
239 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
240 err = got_error_from_errno2("mkdir", path);
244 /* Create .got directory (may already exist). */
245 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
246 err = got_error_from_errno("asprintf");
249 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
250 err = got_error_from_errno2("mkdir", path_got);
254 /* Create an empty lock file. */
255 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
259 /* Create an empty file index. */
260 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
264 /* Write the HEAD reference. */
265 err = write_head_ref(path_got, head_ref);
269 /* Record our base commit. */
270 err = got_object_id_str(&basestr, commit_id);
273 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
277 /* Store path to repository. */
278 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
279 got_repo_get_path(repo));
283 /* Store in-repository path prefix. */
284 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
285 absprefix ? absprefix : prefix);
290 uuid_create(&uuid, &uuid_status);
291 if (uuid_status != uuid_s_ok) {
292 err = got_error_uuid(uuid_status, "uuid_create");
295 uuid_to_string(&uuid, &uuidstr, &uuid_status);
296 if (uuid_status != uuid_s_ok) {
297 err = got_error_uuid(uuid_status, "uuid_to_string");
300 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
304 /* Stamp work tree with format file. */
305 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
306 err = got_error_from_errno("asprintf");
309 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
323 static const struct got_error *
324 open_worktree(struct got_worktree **worktree, const char *path)
326 const struct got_error *err = NULL;
328 char *formatstr = NULL;
329 char *uuidstr = NULL;
330 char *path_lock = NULL;
331 char *base_commit_id_str = NULL;
332 int version, fd = -1;
334 struct got_repository *repo = NULL;
335 uint32_t uuid_status;
339 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
340 err = got_error_from_errno("asprintf");
345 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
346 err = got_error_from_errno("asprintf");
351 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
353 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
354 : got_error_from_errno2("open", path_lock));
358 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
362 version = strtonum(formatstr, 1, INT_MAX, &errstr);
364 err = got_error_msg(GOT_ERR_WORKTREE_META,
365 "could not parse work tree format version number");
368 if (version != GOT_WORKTREE_FORMAT_VERSION) {
369 err = got_error(GOT_ERR_WORKTREE_VERS);
373 *worktree = calloc(1, sizeof(**worktree));
374 if (*worktree == NULL) {
375 err = got_error_from_errno("calloc");
378 (*worktree)->lockfd = -1;
380 (*worktree)->root_path = realpath(path, NULL);
381 if ((*worktree)->root_path == NULL) {
382 err = got_error_from_errno2("realpath", path);
385 err = read_meta_file(&(*worktree)->repo_path, path_got,
386 GOT_WORKTREE_REPOSITORY);
390 err = read_meta_file(&(*worktree)->path_prefix, path_got,
391 GOT_WORKTREE_PATH_PREFIX);
395 err = read_meta_file(&base_commit_id_str, path_got,
396 GOT_WORKTREE_BASE_COMMIT);
400 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
403 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
404 if (uuid_status != uuid_s_ok) {
405 err = got_error_uuid(uuid_status, "uuid_from_string");
409 err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
413 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
418 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
419 GOT_WORKTREE_HEAD_REF);
423 if (asprintf(&(*worktree)->gotconfig_path, "%s/%s/%s",
424 (*worktree)->root_path,
425 GOT_WORKTREE_GOT_DIR, GOT_GOTCONFIG_FILENAME) == -1) {
426 err = got_error_from_errno("asprintf");
430 err = got_gotconfig_read(&(*worktree)->gotconfig,
431 (*worktree)->gotconfig_path);
434 got_repo_close(repo);
437 free(base_commit_id_str);
443 if (*worktree != NULL)
444 got_worktree_close(*worktree);
447 (*worktree)->lockfd = fd;
452 const struct got_error *
453 got_worktree_open(struct got_worktree **worktree, const char *path)
455 const struct got_error *err = NULL;
458 worktree_path = strdup(path);
459 if (worktree_path == NULL)
460 return got_error_from_errno("strdup");
465 err = open_worktree(worktree, worktree_path);
466 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT)) {
474 if (worktree_path[0] == '/' && worktree_path[1] == '\0')
476 err = got_path_dirname(&parent_path, worktree_path);
478 if (err->code != GOT_ERR_BAD_PATH) {
485 worktree_path = parent_path;
489 return got_error(GOT_ERR_NOT_WORKTREE);
492 const struct got_error *
493 got_worktree_close(struct got_worktree *worktree)
495 const struct got_error *err = NULL;
496 free(worktree->repo_path);
497 free(worktree->path_prefix);
498 free(worktree->base_commit_id);
499 free(worktree->head_ref_name);
500 if (worktree->lockfd != -1)
501 if (close(worktree->lockfd) != 0)
502 err = got_error_from_errno2("close",
503 got_worktree_get_root_path(worktree));
504 free(worktree->root_path);
505 free(worktree->gotconfig_path);
506 got_gotconfig_free(worktree->gotconfig);
512 got_worktree_get_root_path(struct got_worktree *worktree)
514 return worktree->root_path;
518 got_worktree_get_repo_path(struct got_worktree *worktree)
520 return worktree->repo_path;
523 got_worktree_get_path_prefix(struct got_worktree *worktree)
525 return worktree->path_prefix;
528 const struct got_error *
529 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
530 const char *path_prefix)
532 char *absprefix = NULL;
534 if (!got_path_is_absolute(path_prefix)) {
535 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
536 return got_error_from_errno("asprintf");
538 *match = (strcmp(absprefix ? absprefix : path_prefix,
539 worktree->path_prefix) == 0);
545 got_worktree_get_head_ref_name(struct got_worktree *worktree)
547 return worktree->head_ref_name;
550 const struct got_error *
551 got_worktree_set_head_ref(struct got_worktree *worktree,
552 struct got_reference *head_ref)
554 const struct got_error *err = NULL;
555 char *path_got = NULL, *head_ref_name = NULL;
557 if (asprintf(&path_got, "%s/%s", worktree->root_path,
558 GOT_WORKTREE_GOT_DIR) == -1) {
559 err = got_error_from_errno("asprintf");
564 head_ref_name = strdup(got_ref_get_name(head_ref));
565 if (head_ref_name == NULL) {
566 err = got_error_from_errno("strdup");
570 err = write_head_ref(path_got, head_ref);
574 free(worktree->head_ref_name);
575 worktree->head_ref_name = head_ref_name;
583 struct got_object_id *
584 got_worktree_get_base_commit_id(struct got_worktree *worktree)
586 return worktree->base_commit_id;
589 const struct got_error *
590 got_worktree_set_base_commit_id(struct got_worktree *worktree,
591 struct got_repository *repo, struct got_object_id *commit_id)
593 const struct got_error *err;
594 struct got_object *obj = NULL;
596 char *path_got = NULL;
598 if (asprintf(&path_got, "%s/%s", worktree->root_path,
599 GOT_WORKTREE_GOT_DIR) == -1) {
600 err = got_error_from_errno("asprintf");
605 err = got_object_open(&obj, repo, commit_id);
609 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
610 err = got_error(GOT_ERR_OBJ_TYPE);
614 /* Record our base commit. */
615 err = got_object_id_str(&id_str, commit_id);
618 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
622 free(worktree->base_commit_id);
623 worktree->base_commit_id = got_object_id_dup(commit_id);
624 if (worktree->base_commit_id == NULL) {
625 err = got_error_from_errno("got_object_id_dup");
630 got_object_close(obj);
636 const struct got_gotconfig *
637 got_worktree_get_gotconfig(struct got_worktree *worktree)
639 return worktree->gotconfig;
642 static const struct got_error *
643 lock_worktree(struct got_worktree *worktree, int operation)
645 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
646 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
647 : got_error_from_errno2("flock",
648 got_worktree_get_root_path(worktree)));
652 static const struct got_error *
653 add_dir_on_disk(struct got_worktree *worktree, const char *path)
655 const struct got_error *err = NULL;
658 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
659 return got_error_from_errno("asprintf");
661 err = got_path_mkdir(abspath);
662 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
665 if (lstat(abspath, &sb) == -1) {
666 err = got_error_from_errno2("lstat", abspath);
667 } else if (!S_ISDIR(sb.st_mode)) {
668 /* TODO directory is obstructed; do something */
669 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
676 static const struct got_error *
677 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
679 const struct got_error *err = NULL;
682 size_t flen1 = 0, flen2 = 0;
687 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
688 if (flen1 == 0 && ferror(f1)) {
689 err = got_error_from_errno("fread");
692 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
693 if (flen2 == 0 && ferror(f2)) {
694 err = got_error_from_errno("fread");
701 } else if (flen2 == 0) {
705 } else if (flen1 == flen2) {
706 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
719 static const struct got_error *
720 check_files_equal(int *same, const char *f1_path, const char *f2_path)
722 const struct got_error *err = NULL;
725 FILE *f1 = NULL, *f2 = NULL;
729 if (lstat(f1_path, &sb) != 0) {
730 err = got_error_from_errno2("lstat", f1_path);
735 if (lstat(f2_path, &sb) != 0) {
736 err = got_error_from_errno2("lstat", f2_path);
741 if (size1 != size2) {
746 f1 = fopen(f1_path, "r");
748 return got_error_from_errno2("fopen", f1_path);
750 f2 = fopen(f2_path, "r");
752 err = got_error_from_errno2("fopen", f2_path);
756 err = check_file_contents_equal(same, f1, f2);
758 if (f1 && fclose(f1) != 0 && err == NULL)
759 err = got_error_from_errno("fclose");
760 if (f2 && fclose(f2) != 0 && err == NULL)
761 err = got_error_from_errno("fclose");
767 * Perform a 3-way merge where blob_orig acts as the common ancestor,
768 * the file at deriv_path acts as the first derived version, and the
769 * file on disk acts as the second derived version.
771 static const struct got_error *
772 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
773 struct got_blob_object *blob_orig, const char *ondisk_path,
774 const char *path, uint16_t st_mode, const char *deriv_path,
775 const char *label_orig, const char *label_deriv,
776 struct got_repository *repo,
777 got_worktree_checkout_cb progress_cb, void *progress_arg)
779 const struct got_error *err = NULL;
782 char *blob_orig_path = NULL;
783 char *merged_path = NULL, *base_path = NULL;
786 char *symlink_path = NULL;
787 FILE *symlinkf = NULL;
789 *local_changes_subsumed = 0;
791 err = got_path_dirname(&parent, ondisk_path);
795 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
796 err = got_error_from_errno("asprintf");
800 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
805 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
806 err = got_error_from_errno("asprintf");
811 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
815 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
821 * If the file has no blob, this is an "add vs add" conflict,
822 * and we simply use an empty ancestor file to make both files
823 * appear in the merged result in their entirety.
828 * In order the run a 3-way merge with a symlink we copy the symlink's
829 * target path into a temporary file and use that file with diff3.
831 if (S_ISLNK(st_mode)) {
832 char target_path[PATH_MAX];
837 if (asprintf(&base_path, "%s/got-symlink-merge",
839 err = got_error_from_errno("asprintf");
843 err = got_opentemp_named(&symlink_path, &symlinkf, base_path);
846 target_len = readlink(ondisk_path, target_path,
847 sizeof(target_path));
848 if (target_len == -1) {
849 err = got_error_from_errno2("readlink", ondisk_path);
852 n = fwrite(target_path, 1, target_len, symlinkf);
853 if (n != target_len) {
854 err = got_ferror(symlinkf, GOT_ERR_IO);
857 if (fflush(symlinkf) == EOF) {
858 err = got_error_from_errno2("fflush", symlink_path);
863 err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
864 blob_orig_path, symlink_path ? symlink_path : ondisk_path,
865 label_deriv, label_orig, NULL);
869 err = (*progress_cb)(progress_arg,
870 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
874 if (fsync(merged_fd) != 0) {
875 err = got_error_from_errno("fsync");
879 /* Check if a clean merge has subsumed all local changes. */
880 if (overlapcnt == 0) {
881 err = check_files_equal(local_changes_subsumed, deriv_path,
887 if (fchmod(merged_fd, st_mode) != 0) {
888 err = got_error_from_errno2("fchmod", merged_path);
892 if (rename(merged_path, ondisk_path) != 0) {
893 err = got_error_from_errno3("rename", merged_path,
903 if (unlink(symlink_path) == -1 && err == NULL)
904 err = got_error_from_errno2("unlink", symlink_path);
906 if (symlinkf && fclose(symlinkf) == EOF && err == NULL)
907 err = got_error_from_errno2("fclose", symlink_path);
909 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
910 err = got_error_from_errno("close");
911 if (f_orig && fclose(f_orig) != 0 && err == NULL)
912 err = got_error_from_errno("fclose");
915 if (blob_orig_path) {
916 unlink(blob_orig_path);
917 free(blob_orig_path);
923 static const struct got_error *
924 update_symlink(const char *ondisk_path, const char *target_path,
927 /* This is not atomic but matches what 'ln -sf' does. */
928 if (unlink(ondisk_path) == -1)
929 return got_error_from_errno2("unlink", ondisk_path);
930 if (symlink(target_path, ondisk_path) == -1)
931 return got_error_from_errno3("symlink", target_path,
937 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
938 * in the work tree with a file that contains conflict markers and the
939 * conflicting target paths of the original version, a "derived version"
940 * of a symlink from an incoming change, and a local version of the symlink.
942 * The original versions's target path can be NULL if it is not available,
943 * such as if both derived versions added a new symlink at the same path.
945 * The incoming derived symlink target is NULL in case the incoming change
946 * has deleted this symlink.
948 static const struct got_error *
949 install_symlink_conflict(const char *deriv_target,
950 struct got_object_id *deriv_base_commit_id, const char *orig_target,
951 const char *label_orig, const char *local_target, const char *ondisk_path)
953 const struct got_error *err;
954 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
957 err = got_object_id_str(&id_str, deriv_base_commit_id);
959 return got_error_from_errno("asprintf");
961 if (asprintf(&label_deriv, "%s: commit %s",
962 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
963 err = got_error_from_errno("asprintf");
967 err = got_opentemp_named(&path, &f, "got-symlink-conflict");
971 if (fchmod(fileno(f), GOT_DEFAULT_FILE_MODE) == -1) {
972 err = got_error_from_errno2("fchmod", path);
976 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
977 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
978 deriv_target ? deriv_target : "(symlink was deleted)",
979 orig_target ? label_orig : "",
980 orig_target ? "\n" : "",
981 orig_target ? orig_target : "",
982 orig_target ? "\n" : "",
983 GOT_DIFF_CONFLICT_MARKER_SEP,
984 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
985 err = got_error_from_errno2("fprintf", path);
989 if (unlink(ondisk_path) == -1) {
990 err = got_error_from_errno2("unlink", ondisk_path);
993 if (rename(path, ondisk_path) == -1) {
994 err = got_error_from_errno3("rename", path, ondisk_path);
998 if (f != NULL && fclose(f) == EOF && err == NULL)
999 err = got_error_from_errno2("fclose", path);
1006 /* forward declaration */
1007 static const struct got_error *
1008 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
1009 const char *, const char *, uint16_t, const char *,
1010 struct got_blob_object *, struct got_object_id *,
1011 struct got_repository *, got_worktree_checkout_cb, void *);
1014 * Merge a symlink into the work tree, where blob_orig acts as the common
1015 * ancestor, deriv_target is the link target of the first derived version,
1016 * and the symlink on disk acts as the second derived version.
1017 * Assume that contents of both blobs represent symlinks.
1019 static const struct got_error *
1020 merge_symlink(struct got_worktree *worktree,
1021 struct got_blob_object *blob_orig, const char *ondisk_path,
1022 const char *path, const char *label_orig, const char *deriv_target,
1023 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1024 got_worktree_checkout_cb progress_cb, void *progress_arg)
1026 const struct got_error *err = NULL;
1027 char *ancestor_target = NULL;
1029 ssize_t ondisk_len, deriv_len;
1030 char ondisk_target[PATH_MAX];
1031 int have_local_change = 0;
1032 int have_incoming_change = 0;
1034 if (lstat(ondisk_path, &sb) == -1)
1035 return got_error_from_errno2("lstat", ondisk_path);
1037 ondisk_len = readlink(ondisk_path, ondisk_target,
1038 sizeof(ondisk_target));
1039 if (ondisk_len == -1) {
1040 err = got_error_from_errno2("readlink",
1044 ondisk_target[ondisk_len] = '\0';
1047 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
1052 if (ancestor_target == NULL ||
1053 (ondisk_len != strlen(ancestor_target) ||
1054 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
1055 have_local_change = 1;
1057 deriv_len = strlen(deriv_target);
1058 if (ancestor_target == NULL ||
1059 (deriv_len != strlen(ancestor_target) ||
1060 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
1061 have_incoming_change = 1;
1063 if (!have_local_change && !have_incoming_change) {
1064 if (ancestor_target) {
1065 /* Both sides made the same change. */
1066 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1068 } else if (deriv_len == ondisk_len &&
1069 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1070 /* Both sides added the same symlink. */
1071 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1074 /* Both sides added symlinks which don't match. */
1075 err = install_symlink_conflict(deriv_target,
1076 deriv_base_commit_id, ancestor_target,
1077 label_orig, ondisk_target, ondisk_path);
1080 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1083 } else if (!have_local_change && have_incoming_change) {
1084 /* Apply the incoming change. */
1085 err = update_symlink(ondisk_path, deriv_target,
1086 strlen(deriv_target));
1089 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1090 } else if (have_local_change && have_incoming_change) {
1091 if (deriv_len == ondisk_len &&
1092 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
1093 /* Both sides made the same change. */
1094 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1097 err = install_symlink_conflict(deriv_target,
1098 deriv_base_commit_id, ancestor_target, label_orig,
1099 ondisk_target, ondisk_path);
1102 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1108 free(ancestor_target);
1113 * Perform a 3-way merge where blob_orig acts as the common ancestor,
1114 * blob_deriv acts as the first derived version, and the file on disk
1115 * acts as the second derived version.
1117 static const struct got_error *
1118 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1119 struct got_blob_object *blob_orig, const char *ondisk_path,
1120 const char *path, uint16_t st_mode, const char *label_orig,
1121 struct got_blob_object *blob_deriv,
1122 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1123 got_worktree_checkout_cb progress_cb, void *progress_arg)
1125 const struct got_error *err = NULL;
1126 FILE *f_deriv = NULL;
1127 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1128 char *label_deriv = NULL, *parent = NULL;
1130 *local_changes_subsumed = 0;
1132 err = got_path_dirname(&parent, ondisk_path);
1137 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1138 err = got_error_from_errno("asprintf");
1143 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1146 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1151 err = got_object_id_str(&id_str, deriv_base_commit_id);
1154 if (asprintf(&label_deriv, "%s: commit %s",
1155 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1156 err = got_error_from_errno("asprintf");
1160 err = merge_file(local_changes_subsumed, worktree, blob_orig,
1161 ondisk_path, path, st_mode, blob_deriv_path, label_orig,
1162 label_deriv, repo, progress_cb, progress_arg);
1164 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
1165 err = got_error_from_errno("fclose");
1167 if (blob_deriv_path) {
1168 unlink(blob_deriv_path);
1169 free(blob_deriv_path);
1177 static const struct got_error *
1178 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1179 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1180 const char *ondisk_path, const char *path, struct got_object_id *blob_id)
1182 const struct got_error *err = NULL;
1183 struct got_fileindex_entry *new_ie;
1187 err = got_fileindex_entry_alloc(&new_ie, path);
1191 err = got_fileindex_entry_update(new_ie, ondisk_path,
1192 blob_id->sha1, base_commit_id->sha1, 1);
1196 err = got_fileindex_entry_add(fileindex, new_ie);
1199 got_fileindex_entry_free(new_ie);
1206 get_ondisk_perms(int executable, mode_t st_mode)
1208 mode_t xbits = S_IXUSR;
1211 /* Map read bits to execute bits. */
1212 if (st_mode & S_IRGRP)
1214 if (st_mode & S_IROTH)
1216 return st_mode | xbits;
1219 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1222 /* forward declaration */
1223 static const struct got_error *
1224 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1225 const char *path, mode_t te_mode, mode_t st_mode,
1226 struct got_blob_object *blob, int restoring_missing_file,
1227 int reverting_versioned_file, int installing_bad_symlink,
1228 int path_is_unversioned, struct got_repository *repo,
1229 got_worktree_checkout_cb progress_cb, void *progress_arg);
1232 * This function assumes that the provided symlink target points at a
1233 * safe location in the work tree!
1235 static const struct got_error *
1236 replace_existing_symlink(const char *ondisk_path, const char *target_path,
1239 const struct got_error *err = NULL;
1241 char etarget[PATH_MAX];
1245 * "Bad" symlinks (those pointing outside the work tree or into the
1246 * .got directory) are installed in the work tree as a regular file
1247 * which contains the bad symlink target path.
1248 * The new symlink target has already been checked for safety by our
1249 * caller. If we can successfully open a regular file then we simply
1250 * replace this file with a symlink below.
1252 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1255 return got_error_from_errno2("open", ondisk_path);
1257 /* We are updating an existing on-disk symlink. */
1258 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1260 return got_error_from_errno2("readlink", ondisk_path);
1262 if (elen == target_len &&
1263 memcmp(etarget, target_path, target_len) == 0)
1264 return NULL; /* nothing to do */
1267 err = update_symlink(ondisk_path, target_path, target_len);
1268 if (fd != -1 && close(fd) == -1 && err == NULL)
1269 err = got_error_from_errno2("close", ondisk_path);
1273 static const struct got_error *
1274 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1275 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1277 const struct got_error *err = NULL;
1278 char canonpath[PATH_MAX];
1279 char *path_got = NULL;
1281 *is_bad_symlink = 0;
1283 if (target_len >= sizeof(canonpath)) {
1284 *is_bad_symlink = 1;
1289 * We do not use realpath(3) to resolve the symlink's target
1290 * path because we don't want to resolve symlinks recursively.
1291 * Instead we make the path absolute and then canonicalize it.
1292 * Relative symlink target lookup should begin at the directory
1293 * in which the blob object is being installed.
1295 if (!got_path_is_absolute(target_path)) {
1296 char *abspath, *parent;
1297 err = got_path_dirname(&parent, ondisk_path);
1300 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1302 return got_error_from_errno("asprintf");
1305 if (strlen(abspath) >= sizeof(canonpath)) {
1306 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1310 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1315 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1320 /* Only allow symlinks pointing at paths within the work tree. */
1321 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1322 *is_bad_symlink = 1;
1326 /* Do not allow symlinks pointing into the .got directory. */
1327 if (asprintf(&path_got, "%s/%s", wtroot_path,
1328 GOT_WORKTREE_GOT_DIR) == -1)
1329 return got_error_from_errno("asprintf");
1330 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1331 *is_bad_symlink = 1;
1337 static const struct got_error *
1338 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1339 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1340 int restoring_missing_file, int reverting_versioned_file,
1341 int path_is_unversioned, struct got_repository *repo,
1342 got_worktree_checkout_cb progress_cb, void *progress_arg)
1344 const struct got_error *err = NULL;
1345 char target_path[PATH_MAX];
1346 size_t len, target_len = 0;
1347 char *path_got = NULL;
1348 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1349 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1351 *is_bad_symlink = 0;
1354 * Blob object content specifies the target path of the link.
1355 * If a symbolic link cannot be installed we instead create
1356 * a regular file which contains the link target path stored
1357 * in the blob object.
1360 err = got_object_blob_read_block(&len, blob);
1361 if (len + target_len >= sizeof(target_path)) {
1362 /* Path too long; install as a regular file. */
1363 *is_bad_symlink = 1;
1364 got_object_blob_rewind(blob);
1365 return install_blob(worktree, ondisk_path, path,
1366 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1367 restoring_missing_file, reverting_versioned_file,
1368 1, path_is_unversioned, repo, progress_cb,
1372 /* Skip blob object header first time around. */
1373 memcpy(target_path + target_len, buf + hdrlen,
1375 target_len += len - hdrlen;
1379 target_path[target_len] = '\0';
1381 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1382 ondisk_path, worktree->root_path);
1386 if (*is_bad_symlink) {
1387 /* install as a regular file */
1388 *is_bad_symlink = 1;
1389 got_object_blob_rewind(blob);
1390 err = install_blob(worktree, ondisk_path, path,
1391 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1392 restoring_missing_file, reverting_versioned_file, 1,
1393 path_is_unversioned, repo, progress_cb, progress_arg);
1397 if (symlink(target_path, ondisk_path) == -1) {
1398 if (errno == EEXIST) {
1399 if (path_is_unversioned) {
1400 err = (*progress_cb)(progress_arg,
1401 GOT_STATUS_UNVERSIONED, path);
1404 err = replace_existing_symlink(ondisk_path,
1405 target_path, target_len);
1409 err = (*progress_cb)(progress_arg,
1410 reverting_versioned_file ?
1411 GOT_STATUS_REVERT : GOT_STATUS_UPDATE,
1414 goto done; /* Nothing else to do. */
1417 if (errno == ENOENT) {
1419 err = got_path_dirname(&parent, ondisk_path);
1422 err = add_dir_on_disk(worktree, parent);
1427 * Retry, and fall through to error handling
1428 * below if this second attempt fails.
1430 if (symlink(target_path, ondisk_path) != -1) {
1431 err = NULL; /* success */
1436 /* Handle errors from first or second creation attempt. */
1437 if (errno == ENAMETOOLONG) {
1438 /* bad target path; install as a regular file */
1439 *is_bad_symlink = 1;
1440 got_object_blob_rewind(blob);
1441 err = install_blob(worktree, ondisk_path, path,
1442 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1443 restoring_missing_file, reverting_versioned_file, 1,
1444 path_is_unversioned, repo,
1445 progress_cb, progress_arg);
1446 } else if (errno == ENOTDIR) {
1447 err = got_error_path(ondisk_path,
1448 GOT_ERR_FILE_OBSTRUCTED);
1450 err = got_error_from_errno3("symlink",
1451 target_path, ondisk_path);
1453 } else if (progress_cb)
1454 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1455 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1461 static const struct got_error *
1462 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1463 const char *path, mode_t te_mode, mode_t st_mode,
1464 struct got_blob_object *blob, int restoring_missing_file,
1465 int reverting_versioned_file, int installing_bad_symlink,
1466 int path_is_unversioned, struct got_repository *repo,
1467 got_worktree_checkout_cb progress_cb, void *progress_arg)
1469 const struct got_error *err = NULL;
1473 char *tmppath = NULL;
1475 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1476 GOT_DEFAULT_FILE_MODE);
1478 if (errno == ENOENT) {
1480 err = got_path_dirname(&parent, path);
1483 err = add_dir_on_disk(worktree, parent);
1487 fd = open(ondisk_path,
1488 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1489 GOT_DEFAULT_FILE_MODE);
1491 return got_error_from_errno2("open",
1493 } else if (errno == EEXIST) {
1494 if (path_is_unversioned) {
1495 err = (*progress_cb)(progress_arg,
1496 GOT_STATUS_UNVERSIONED, path);
1499 if (!S_ISREG(st_mode) && !installing_bad_symlink) {
1500 /* TODO file is obstructed; do something */
1501 err = got_error_path(ondisk_path,
1502 GOT_ERR_FILE_OBSTRUCTED);
1505 err = got_opentemp_named_fd(&tmppath, &fd,
1512 return got_error_from_errno2("open", ondisk_path);
1515 if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1516 err = got_error_from_errno2("fchmod",
1517 update ? tmppath : ondisk_path);
1522 if (restoring_missing_file)
1523 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1525 else if (reverting_versioned_file)
1526 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1529 err = (*progress_cb)(progress_arg,
1530 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1535 hdrlen = got_object_blob_get_hdrlen(blob);
1537 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1538 err = got_object_blob_read_block(&len, blob);
1542 /* Skip blob object header first time around. */
1543 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1545 err = got_error_from_errno("write");
1547 } else if (outlen != len - hdrlen) {
1548 err = got_error(GOT_ERR_IO);
1555 if (fsync(fd) != 0) {
1556 err = got_error_from_errno("fsync");
1561 if (rename(tmppath, ondisk_path) != 0) {
1562 err = got_error_from_errno3("rename", tmppath,
1571 if (fd != -1 && close(fd) != 0 && err == NULL)
1572 err = got_error_from_errno("close");
1573 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1574 err = got_error_from_errno2("unlink", tmppath);
1579 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1580 static const struct got_error *
1581 get_modified_file_content_status(unsigned char *status, FILE *f)
1583 const struct got_error *err = NULL;
1584 const char *markers[3] = {
1585 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1586 GOT_DIFF_CONFLICT_MARKER_SEP,
1587 GOT_DIFF_CONFLICT_MARKER_END
1592 const char delim[3] = {'\0', '\0', '\0'};
1594 while (*status == GOT_STATUS_MODIFY) {
1595 line = fparseln(f, &len, NULL, delim, 0);
1599 err = got_ferror(f, GOT_ERR_IO);
1603 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1604 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1606 *status = GOT_STATUS_CONFLICT;
1616 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1618 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1619 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1623 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1625 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1626 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1627 ie->mtime_sec == sb->st_mtim.tv_sec &&
1628 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1629 ie->size == (sb->st_size & 0xffffffff) &&
1630 !xbit_differs(ie, sb->st_mode));
1633 static unsigned char
1634 get_staged_status(struct got_fileindex_entry *ie)
1636 switch (got_fileindex_entry_stage_get(ie)) {
1637 case GOT_FILEIDX_STAGE_ADD:
1638 return GOT_STATUS_ADD;
1639 case GOT_FILEIDX_STAGE_DELETE:
1640 return GOT_STATUS_DELETE;
1641 case GOT_FILEIDX_STAGE_MODIFY:
1642 return GOT_STATUS_MODIFY;
1644 return GOT_STATUS_NO_CHANGE;
1648 static const struct got_error *
1649 get_symlink_modification_status(unsigned char *status,
1650 struct got_fileindex_entry *ie, const char *abspath,
1651 int dirfd, const char *de_name, struct got_blob_object *blob)
1653 const struct got_error *err = NULL;
1654 char target_path[PATH_MAX];
1655 char etarget[PATH_MAX];
1657 size_t len, target_len = 0;
1658 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1659 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1661 *status = GOT_STATUS_NO_CHANGE;
1663 /* Blob object content specifies the target path of the link. */
1665 err = got_object_blob_read_block(&len, blob);
1668 if (len + target_len >= sizeof(target_path)) {
1670 * Should not happen. The blob contents were OK
1671 * when this symlink was installed.
1673 return got_error(GOT_ERR_NO_SPACE);
1676 /* Skip blob object header first time around. */
1677 memcpy(target_path + target_len, buf + hdrlen,
1679 target_len += len - hdrlen;
1683 target_path[target_len] = '\0';
1686 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1688 return got_error_from_errno2("readlinkat", abspath);
1690 elen = readlink(abspath, etarget, sizeof(etarget));
1692 return got_error_from_errno2("readlink", abspath);
1695 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1696 *status = GOT_STATUS_MODIFY;
1701 static const struct got_error *
1702 get_file_status(unsigned char *status, struct stat *sb,
1703 struct got_fileindex_entry *ie, const char *abspath,
1704 int dirfd, const char *de_name, struct got_repository *repo)
1706 const struct got_error *err = NULL;
1707 struct got_object_id id;
1712 struct got_blob_object *blob = NULL;
1714 unsigned char staged_status = get_staged_status(ie);
1716 *status = GOT_STATUS_NO_CHANGE;
1719 * Whenever the caller provides a directory descriptor and a
1720 * directory entry name for the file, use them! This prevents
1721 * race conditions if filesystem paths change beneath our feet.
1724 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1725 if (errno == ENOENT) {
1726 if (got_fileindex_entry_has_file_on_disk(ie))
1727 *status = GOT_STATUS_MISSING;
1729 *status = GOT_STATUS_DELETE;
1732 err = got_error_from_errno2("fstatat", abspath);
1736 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1737 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1738 return got_error_from_errno2("open", abspath);
1739 else if (fd == -1 && errno == ELOOP) {
1740 if (lstat(abspath, sb) == -1)
1741 return got_error_from_errno2("lstat", abspath);
1742 } else if (fd == -1 || fstat(fd, sb) == -1) {
1743 if (errno == ENOENT) {
1744 if (got_fileindex_entry_has_file_on_disk(ie))
1745 *status = GOT_STATUS_MISSING;
1747 *status = GOT_STATUS_DELETE;
1750 err = got_error_from_errno2("fstat", abspath);
1755 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1756 *status = GOT_STATUS_OBSTRUCTED;
1760 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1761 *status = GOT_STATUS_DELETE;
1763 } else if (!got_fileindex_entry_has_blob(ie) &&
1764 staged_status != GOT_STATUS_ADD) {
1765 *status = GOT_STATUS_ADD;
1769 if (!stat_info_differs(ie, sb))
1772 if (S_ISLNK(sb->st_mode) &&
1773 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1774 *status = GOT_STATUS_MODIFY;
1778 if (staged_status == GOT_STATUS_MODIFY ||
1779 staged_status == GOT_STATUS_ADD)
1780 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1782 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1784 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1788 if (S_ISLNK(sb->st_mode)) {
1789 err = get_symlink_modification_status(status, ie,
1790 abspath, dirfd, de_name, blob);
1795 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1797 err = got_error_from_errno2("openat", abspath);
1802 f = fdopen(fd, "r");
1804 err = got_error_from_errno2("fdopen", abspath);
1808 hdrlen = got_object_blob_get_hdrlen(blob);
1810 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1811 err = got_object_blob_read_block(&blen, blob);
1814 /* Skip length of blob object header first time around. */
1815 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1816 if (flen == 0 && ferror(f)) {
1817 err = got_error_from_errno("fread");
1820 if (blen - hdrlen == 0) {
1822 *status = GOT_STATUS_MODIFY;
1824 } else if (flen == 0) {
1825 if (blen - hdrlen != 0)
1826 *status = GOT_STATUS_MODIFY;
1828 } else if (blen - hdrlen == flen) {
1829 /* Skip blob object header first time around. */
1830 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1831 *status = GOT_STATUS_MODIFY;
1835 *status = GOT_STATUS_MODIFY;
1841 if (*status == GOT_STATUS_MODIFY) {
1843 err = get_modified_file_content_status(status, f);
1844 } else if (xbit_differs(ie, sb->st_mode))
1845 *status = GOT_STATUS_MODE_CHANGE;
1848 got_object_blob_close(blob);
1849 if (f != NULL && fclose(f) == EOF && err == NULL)
1850 err = got_error_from_errno2("fclose", abspath);
1851 if (fd != -1 && close(fd) == -1 && err == NULL)
1852 err = got_error_from_errno2("close", abspath);
1857 * Update timestamps in the file index if a file is unmodified and
1858 * we had to run a full content comparison to find out.
1860 static const struct got_error *
1861 sync_timestamps(char *ondisk_path, unsigned char status,
1862 struct got_fileindex_entry *ie, struct stat *sb)
1864 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1865 return got_fileindex_entry_update(ie, ondisk_path,
1866 ie->blob_sha1, ie->commit_sha1, 1);
1871 static const struct got_error *
1872 update_blob(struct got_worktree *worktree,
1873 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1874 struct got_tree_entry *te, const char *path,
1875 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1878 const struct got_error *err = NULL;
1879 struct got_blob_object *blob = NULL;
1881 unsigned char status = GOT_STATUS_NO_CHANGE;
1884 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1885 return got_error_from_errno("asprintf");
1888 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1889 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1892 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1896 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1897 sb.st_mode = got_fileindex_perms_to_st(ie);
1899 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1900 status = GOT_STATUS_UNVERSIONED;
1903 if (status == GOT_STATUS_OBSTRUCTED) {
1904 err = (*progress_cb)(progress_arg, status, path);
1907 if (status == GOT_STATUS_CONFLICT) {
1908 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1913 if (ie && status != GOT_STATUS_MISSING &&
1914 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1915 if (got_fileindex_entry_has_commit(ie) &&
1916 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1917 SHA1_DIGEST_LENGTH) == 0) {
1918 err = sync_timestamps(ondisk_path, status, ie, &sb);
1921 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1925 if (got_fileindex_entry_has_blob(ie) &&
1926 memcmp(ie->blob_sha1, te->id.sha1,
1927 SHA1_DIGEST_LENGTH) == 0) {
1928 err = sync_timestamps(ondisk_path, status, ie, &sb);
1933 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1937 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1938 int update_timestamps;
1939 struct got_blob_object *blob2 = NULL;
1940 char *label_orig = NULL;
1941 if (got_fileindex_entry_has_blob(ie)) {
1942 struct got_object_id id2;
1943 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1944 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1948 if (got_fileindex_entry_has_commit(ie)) {
1949 char id_str[SHA1_DIGEST_STRING_LENGTH];
1950 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1951 sizeof(id_str)) == NULL) {
1952 err = got_error_path(id_str,
1953 GOT_ERR_BAD_OBJ_ID_STR);
1956 if (asprintf(&label_orig, "%s: commit %s",
1957 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1958 err = got_error_from_errno("asprintf");
1962 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1964 err = got_object_blob_read_to_str(&link_target, blob);
1967 err = merge_symlink(worktree, blob2, ondisk_path, path,
1968 label_orig, link_target, worktree->base_commit_id,
1969 repo, progress_cb, progress_arg);
1972 err = merge_blob(&update_timestamps, worktree, blob2,
1973 ondisk_path, path, sb.st_mode, label_orig, blob,
1974 worktree->base_commit_id, repo,
1975 progress_cb, progress_arg);
1979 got_object_blob_close(blob2);
1983 * Do not update timestamps of files with local changes.
1984 * Otherwise, a future status walk would treat them as
1985 * unmodified files again.
1987 err = got_fileindex_entry_update(ie, ondisk_path,
1988 blob->id.sha1, worktree->base_commit_id->sha1,
1990 } else if (status == GOT_STATUS_MODE_CHANGE) {
1991 err = got_fileindex_entry_update(ie, ondisk_path,
1992 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1993 } else if (status == GOT_STATUS_DELETE) {
1994 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1997 err = got_fileindex_entry_update(ie, ondisk_path,
1998 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2002 int is_bad_symlink = 0;
2003 if (S_ISLNK(te->mode)) {
2004 err = install_symlink(&is_bad_symlink, worktree,
2005 ondisk_path, path, blob,
2006 status == GOT_STATUS_MISSING, 0,
2007 status == GOT_STATUS_UNVERSIONED, repo,
2008 progress_cb, progress_arg);
2010 err = install_blob(worktree, ondisk_path, path,
2011 te->mode, sb.st_mode, blob,
2012 status == GOT_STATUS_MISSING, 0, 0,
2013 status == GOT_STATUS_UNVERSIONED, repo,
2014 progress_cb, progress_arg);
2020 err = got_fileindex_entry_update(ie, ondisk_path,
2021 blob->id.sha1, worktree->base_commit_id->sha1, 1);
2023 err = create_fileindex_entry(&ie, fileindex,
2024 worktree->base_commit_id, ondisk_path, path,
2030 if (is_bad_symlink) {
2031 got_fileindex_entry_filetype_set(ie,
2032 GOT_FILEIDX_MODE_BAD_SYMLINK);
2035 got_object_blob_close(blob);
2041 static const struct got_error *
2042 remove_ondisk_file(const char *root_path, const char *path)
2044 const struct got_error *err = NULL;
2045 char *ondisk_path = NULL;
2047 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2048 return got_error_from_errno("asprintf");
2050 if (unlink(ondisk_path) == -1) {
2051 if (errno != ENOENT)
2052 err = got_error_from_errno2("unlink", ondisk_path);
2054 size_t root_len = strlen(root_path);
2057 err = got_path_dirname(&parent, ondisk_path);
2061 ondisk_path = parent;
2062 if (rmdir(ondisk_path) == -1) {
2063 if (errno != ENOTEMPTY)
2064 err = got_error_from_errno2("rmdir",
2068 } while (got_path_cmp(ondisk_path, root_path,
2069 strlen(ondisk_path), root_len) != 0);
2075 static const struct got_error *
2076 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2077 struct got_fileindex_entry *ie, struct got_repository *repo,
2078 got_worktree_checkout_cb progress_cb, void *progress_arg)
2080 const struct got_error *err = NULL;
2081 unsigned char status;
2085 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2086 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2088 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2090 return got_error_from_errno("asprintf");
2092 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2096 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2097 char ondisk_target[PATH_MAX];
2098 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2099 sizeof(ondisk_target));
2100 if (ondisk_len == -1) {
2101 err = got_error_from_errno2("readlink", ondisk_path);
2104 ondisk_target[ondisk_len] = '\0';
2105 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2106 NULL, NULL, /* XXX pass common ancestor info? */
2107 ondisk_target, ondisk_path);
2110 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2115 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2116 status == GOT_STATUS_ADD) {
2117 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2121 * Preserve the working file and change the deleted blob's
2122 * entry into a schedule-add entry.
2124 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
2127 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2130 if (status == GOT_STATUS_NO_CHANGE) {
2131 err = remove_ondisk_file(worktree->root_path, ie->path);
2135 got_fileindex_entry_remove(fileindex, ie);
2142 struct diff_cb_arg {
2143 struct got_fileindex *fileindex;
2144 struct got_worktree *worktree;
2145 struct got_repository *repo;
2146 got_worktree_checkout_cb progress_cb;
2148 got_cancel_cb cancel_cb;
2152 static const struct got_error *
2153 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2154 struct got_tree_entry *te, const char *parent_path)
2156 struct diff_cb_arg *a = arg;
2158 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2159 return got_error(GOT_ERR_CANCELLED);
2161 return update_blob(a->worktree, a->fileindex, ie, te,
2162 ie->path, a->repo, a->progress_cb, a->progress_arg);
2165 static const struct got_error *
2166 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2168 struct diff_cb_arg *a = arg;
2170 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2171 return got_error(GOT_ERR_CANCELLED);
2173 return delete_blob(a->worktree, a->fileindex, ie,
2174 a->repo, a->progress_cb, a->progress_arg);
2177 static const struct got_error *
2178 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2180 struct diff_cb_arg *a = arg;
2181 const struct got_error *err;
2184 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2185 return got_error(GOT_ERR_CANCELLED);
2187 if (got_object_tree_entry_is_submodule(te))
2190 if (asprintf(&path, "%s%s%s", parent_path,
2191 parent_path[0] ? "/" : "", te->name)
2193 return got_error_from_errno("asprintf");
2195 if (S_ISDIR(te->mode))
2196 err = add_dir_on_disk(a->worktree, path);
2198 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2199 a->repo, a->progress_cb, a->progress_arg);
2205 const struct got_error *
2206 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2208 uint32_t uuid_status;
2210 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2211 if (uuid_status != uuid_s_ok) {
2213 return got_error_uuid(uuid_status, "uuid_to_string");
2219 static const struct got_error *
2220 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2222 const struct got_error *err = NULL;
2223 char *uuidstr = NULL;
2227 err = got_worktree_get_uuid(&uuidstr, worktree);
2231 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2232 err = got_error_from_errno("asprintf");
2239 const struct got_error *
2240 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2242 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2245 static const struct got_error *
2246 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2248 return get_ref_name(refname, worktree,
2249 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2252 static const struct got_error *
2253 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2255 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2258 static const struct got_error *
2259 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2261 return get_ref_name(refname, worktree,
2262 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2265 static const struct got_error *
2266 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2268 return get_ref_name(refname, worktree,
2269 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2272 static const struct got_error *
2273 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2275 return get_ref_name(refname, worktree,
2276 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2279 static const struct got_error *
2280 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2282 return get_ref_name(refname, worktree,
2283 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2286 static const struct got_error *
2287 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2289 return get_ref_name(refname, worktree,
2290 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2293 static const struct got_error *
2294 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2296 return get_ref_name(refname, worktree,
2297 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2300 const struct got_error *
2301 got_worktree_get_histedit_script_path(char **path,
2302 struct got_worktree *worktree)
2304 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2305 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2307 return got_error_from_errno("asprintf");
2313 * Prevent Git's garbage collector from deleting our base commit by
2314 * setting a reference to our base commit's ID.
2316 static const struct got_error *
2317 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2319 const struct got_error *err = NULL;
2320 struct got_reference *ref = NULL;
2323 err = got_worktree_get_base_ref_name(&refname, worktree);
2327 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2331 err = got_ref_write(ref, repo);
2339 static const struct got_error *
2340 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2342 const struct got_error *err = NULL;
2344 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2345 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2346 err = got_error_from_errno("asprintf");
2347 *fileindex_path = NULL;
2353 static const struct got_error *
2354 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2355 struct got_worktree *worktree)
2357 const struct got_error *err = NULL;
2360 *fileindex_path = NULL;
2361 *fileindex = got_fileindex_alloc();
2362 if (*fileindex == NULL)
2363 return got_error_from_errno("got_fileindex_alloc");
2365 err = get_fileindex_path(fileindex_path, worktree);
2369 index = fopen(*fileindex_path, "rb");
2370 if (index == NULL) {
2371 if (errno != ENOENT)
2372 err = got_error_from_errno2("fopen", *fileindex_path);
2374 err = got_fileindex_read(*fileindex, index);
2375 if (fclose(index) != 0 && err == NULL)
2376 err = got_error_from_errno("fclose");
2380 free(*fileindex_path);
2381 *fileindex_path = NULL;
2382 got_fileindex_free(*fileindex);
2388 struct bump_base_commit_id_arg {
2389 struct got_object_id *base_commit_id;
2392 const char *entry_name;
2393 got_worktree_checkout_cb progress_cb;
2397 /* Bump base commit ID of all files within an updated part of the work tree. */
2398 static const struct got_error *
2399 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2401 const struct got_error *err;
2402 struct bump_base_commit_id_arg *a = arg;
2404 if (a->entry_name) {
2405 if (strcmp(ie->path, a->path) != 0)
2407 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2410 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2411 SHA1_DIGEST_LENGTH) == 0)
2414 if (a->progress_cb) {
2415 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2420 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2424 static const struct got_error *
2425 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2427 const struct got_error *err = NULL;
2428 char *new_fileindex_path = NULL;
2429 FILE *new_index = NULL;
2430 struct timespec timeout;
2432 err = got_opentemp_named(&new_fileindex_path, &new_index,
2437 err = got_fileindex_write(fileindex, new_index);
2441 if (rename(new_fileindex_path, fileindex_path) != 0) {
2442 err = got_error_from_errno3("rename", new_fileindex_path,
2444 unlink(new_fileindex_path);
2448 * Sleep for a short amount of time to ensure that files modified after
2449 * this program exits have a different time stamp from the one which
2450 * was recorded in the file index.
2453 timeout.tv_nsec = 1;
2454 nanosleep(&timeout, NULL);
2458 free(new_fileindex_path);
2462 static const struct got_error *
2463 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2464 struct got_object_id **tree_id, const char *wt_relpath,
2465 struct got_worktree *worktree, struct got_repository *repo)
2467 const struct got_error *err = NULL;
2468 struct got_object_id *id = NULL;
2469 char *in_repo_path = NULL;
2470 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2472 *entry_type = GOT_OBJ_TYPE_ANY;
2473 *tree_relpath = NULL;
2476 if (wt_relpath[0] == '\0') {
2477 /* Check out all files within the work tree. */
2478 *entry_type = GOT_OBJ_TYPE_TREE;
2479 *tree_relpath = strdup("");
2480 if (*tree_relpath == NULL) {
2481 err = got_error_from_errno("strdup");
2484 err = got_object_id_by_path(tree_id, repo,
2485 worktree->base_commit_id, worktree->path_prefix);
2491 /* Check out a subset of files in the work tree. */
2493 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2494 is_root_wt ? "" : "/", wt_relpath) == -1) {
2495 err = got_error_from_errno("asprintf");
2499 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2505 in_repo_path = NULL;
2507 err = got_object_get_type(entry_type, repo, id);
2511 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2512 /* Check out a single file. */
2513 if (strchr(wt_relpath, '/') == NULL) {
2514 /* Check out a single file in work tree's root dir. */
2515 in_repo_path = strdup(worktree->path_prefix);
2516 if (in_repo_path == NULL) {
2517 err = got_error_from_errno("strdup");
2520 *tree_relpath = strdup("");
2521 if (*tree_relpath == NULL) {
2522 err = got_error_from_errno("strdup");
2526 /* Check out a single file in a subdirectory. */
2527 err = got_path_dirname(tree_relpath, wt_relpath);
2530 if (asprintf(&in_repo_path, "%s%s%s",
2531 worktree->path_prefix, is_root_wt ? "" : "/",
2532 *tree_relpath) == -1) {
2533 err = got_error_from_errno("asprintf");
2537 err = got_object_id_by_path(tree_id, repo,
2538 worktree->base_commit_id, in_repo_path);
2540 /* Check out all files within a subdirectory. */
2541 *tree_id = got_object_id_dup(id);
2542 if (*tree_id == NULL) {
2543 err = got_error_from_errno("got_object_id_dup");
2546 *tree_relpath = strdup(wt_relpath);
2547 if (*tree_relpath == NULL) {
2548 err = got_error_from_errno("strdup");
2556 *entry_type = GOT_OBJ_TYPE_ANY;
2557 free(*tree_relpath);
2558 *tree_relpath = NULL;
2565 static const struct got_error *
2566 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2567 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2568 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2569 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2571 const struct got_error *err = NULL;
2572 struct got_commit_object *commit = NULL;
2573 struct got_tree_object *tree = NULL;
2574 struct got_fileindex_diff_tree_cb diff_cb;
2575 struct diff_cb_arg arg;
2577 err = ref_base_commit(worktree, repo);
2579 if (!(err->code == GOT_ERR_ERRNO &&
2580 (errno == EACCES || errno == EROFS)))
2582 err = (*progress_cb)(progress_arg,
2583 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2588 err = got_object_open_as_commit(&commit, repo,
2589 worktree->base_commit_id);
2593 err = got_object_open_as_tree(&tree, repo, tree_id);
2598 got_object_tree_find_entry(tree, entry_name) == NULL) {
2599 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2603 diff_cb.diff_old_new = diff_old_new;
2604 diff_cb.diff_old = diff_old;
2605 diff_cb.diff_new = diff_new;
2606 arg.fileindex = fileindex;
2607 arg.worktree = worktree;
2609 arg.progress_cb = progress_cb;
2610 arg.progress_arg = progress_arg;
2611 arg.cancel_cb = cancel_cb;
2612 arg.cancel_arg = cancel_arg;
2613 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2614 entry_name, repo, &diff_cb, &arg);
2617 got_object_tree_close(tree);
2619 got_object_commit_close(commit);
2623 const struct got_error *
2624 got_worktree_checkout_files(struct got_worktree *worktree,
2625 struct got_pathlist_head *paths, struct got_repository *repo,
2626 got_worktree_checkout_cb progress_cb, void *progress_arg,
2627 got_cancel_cb cancel_cb, void *cancel_arg)
2629 const struct got_error *err = NULL, *sync_err, *unlockerr;
2630 struct got_commit_object *commit = NULL;
2631 struct got_tree_object *tree = NULL;
2632 struct got_fileindex *fileindex = NULL;
2633 char *fileindex_path = NULL;
2634 struct got_pathlist_entry *pe;
2635 struct tree_path_data {
2636 SIMPLEQ_ENTRY(tree_path_data) entry;
2637 struct got_object_id *tree_id;
2642 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2644 SIMPLEQ_INIT(&tree_paths);
2646 err = lock_worktree(worktree, LOCK_EX);
2650 /* Map all specified paths to in-repository trees. */
2651 TAILQ_FOREACH(pe, paths, entry) {
2652 tpd = malloc(sizeof(*tpd));
2654 err = got_error_from_errno("malloc");
2658 err = find_tree_entry_for_checkout(&tpd->entry_type,
2659 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2665 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2666 err = got_path_basename(&tpd->entry_name, pe->path);
2674 tpd->entry_name = NULL;
2676 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2680 * Read the file index.
2681 * Checking out files is supposed to be an idempotent operation.
2682 * If the on-disk file index is incomplete we will try to complete it.
2684 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2688 tpd = SIMPLEQ_FIRST(&tree_paths);
2689 TAILQ_FOREACH(pe, paths, entry) {
2690 struct bump_base_commit_id_arg bbc_arg;
2692 err = checkout_files(worktree, fileindex, tpd->relpath,
2693 tpd->tree_id, tpd->entry_name, repo,
2694 progress_cb, progress_arg, cancel_cb, cancel_arg);
2698 bbc_arg.base_commit_id = worktree->base_commit_id;
2699 bbc_arg.entry_name = tpd->entry_name;
2700 bbc_arg.path = pe->path;
2701 bbc_arg.path_len = pe->path_len;
2702 bbc_arg.progress_cb = progress_cb;
2703 bbc_arg.progress_arg = progress_arg;
2704 err = got_fileindex_for_each_entry_safe(fileindex,
2705 bump_base_commit_id, &bbc_arg);
2709 tpd = SIMPLEQ_NEXT(tpd, entry);
2711 sync_err = sync_fileindex(fileindex, fileindex_path);
2712 if (sync_err && err == NULL)
2715 free(fileindex_path);
2717 got_object_tree_close(tree);
2719 got_object_commit_close(commit);
2721 got_fileindex_free(fileindex);
2722 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2723 tpd = SIMPLEQ_FIRST(&tree_paths);
2724 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2729 unlockerr = lock_worktree(worktree, LOCK_SH);
2730 if (unlockerr && err == NULL)
2735 struct merge_file_cb_arg {
2736 struct got_worktree *worktree;
2737 struct got_fileindex *fileindex;
2738 got_worktree_checkout_cb progress_cb;
2740 got_cancel_cb cancel_cb;
2742 const char *label_orig;
2743 struct got_object_id *commit_id2;
2746 static const struct got_error *
2747 merge_file_cb(void *arg, struct got_blob_object *blob1,
2748 struct got_blob_object *blob2, struct got_object_id *id1,
2749 struct got_object_id *id2, const char *path1, const char *path2,
2750 mode_t mode1, mode_t mode2, struct got_repository *repo)
2752 static const struct got_error *err = NULL;
2753 struct merge_file_cb_arg *a = arg;
2754 struct got_fileindex_entry *ie;
2755 char *ondisk_path = NULL;
2757 unsigned char status;
2758 int local_changes_subsumed;
2760 if (blob1 && blob2) {
2761 ie = got_fileindex_entry_get(a->fileindex, path2,
2764 return (*a->progress_cb)(a->progress_arg,
2765 GOT_STATUS_MISSING, path2);
2767 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2769 return got_error_from_errno("asprintf");
2771 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2776 if (status == GOT_STATUS_DELETE) {
2777 err = (*a->progress_cb)(a->progress_arg,
2778 GOT_STATUS_MERGE, path2);
2781 if (status != GOT_STATUS_NO_CHANGE &&
2782 status != GOT_STATUS_MODIFY &&
2783 status != GOT_STATUS_CONFLICT &&
2784 status != GOT_STATUS_ADD) {
2785 err = (*a->progress_cb)(a->progress_arg, status, path2);
2789 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2791 err = got_object_blob_read_to_str(&link_target2, blob2);
2794 err = merge_symlink(a->worktree, blob1, ondisk_path,
2795 path2, a->label_orig, link_target2, a->commit_id2,
2796 repo, a->progress_cb, a->progress_arg);
2799 err = merge_blob(&local_changes_subsumed, a->worktree,
2800 blob1, ondisk_path, path2, sb.st_mode,
2801 a->label_orig, blob2, a->commit_id2, repo,
2802 a->progress_cb, a->progress_arg);
2805 ie = got_fileindex_entry_get(a->fileindex, path1,
2808 return (*a->progress_cb)(a->progress_arg,
2809 GOT_STATUS_MISSING, path1);
2811 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2813 return got_error_from_errno("asprintf");
2815 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2821 case GOT_STATUS_NO_CHANGE:
2822 err = (*a->progress_cb)(a->progress_arg,
2823 GOT_STATUS_DELETE, path1);
2826 err = remove_ondisk_file(a->worktree->root_path, path1);
2830 got_fileindex_entry_mark_deleted_from_disk(ie);
2832 case GOT_STATUS_DELETE:
2833 case GOT_STATUS_MISSING:
2834 err = (*a->progress_cb)(a->progress_arg,
2835 GOT_STATUS_DELETE, path1);
2839 got_fileindex_entry_mark_deleted_from_disk(ie);
2841 case GOT_STATUS_ADD: {
2842 struct got_object_id *id;
2845 * Delete the added file only if its content already
2846 * exists in the repository.
2848 err = got_object_blob_file_create(&id, &blob1_f, path1);
2851 if (got_object_id_cmp(id, id1) == 0) {
2852 err = (*a->progress_cb)(a->progress_arg,
2853 GOT_STATUS_DELETE, path1);
2856 err = remove_ondisk_file(a->worktree->root_path,
2861 got_fileindex_entry_remove(a->fileindex,
2864 err = (*a->progress_cb)(a->progress_arg,
2865 GOT_STATUS_CANNOT_DELETE, path1);
2867 if (fclose(blob1_f) == EOF && err == NULL)
2868 err = got_error_from_errno("fclose");
2874 case GOT_STATUS_MODIFY:
2875 case GOT_STATUS_CONFLICT:
2876 err = (*a->progress_cb)(a->progress_arg,
2877 GOT_STATUS_CANNOT_DELETE, path1);
2881 case GOT_STATUS_OBSTRUCTED:
2882 err = (*a->progress_cb)(a->progress_arg, status, path1);
2890 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2892 return got_error_from_errno("asprintf");
2893 ie = got_fileindex_entry_get(a->fileindex, path2,
2896 err = get_file_status(&status, &sb, ie, ondisk_path,
2900 if (status != GOT_STATUS_NO_CHANGE &&
2901 status != GOT_STATUS_MODIFY &&
2902 status != GOT_STATUS_CONFLICT &&
2903 status != GOT_STATUS_ADD) {
2904 err = (*a->progress_cb)(a->progress_arg,
2908 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2910 err = got_object_blob_read_to_str(&link_target2,
2914 err = merge_symlink(a->worktree, NULL,
2915 ondisk_path, path2, a->label_orig,
2916 link_target2, a->commit_id2, repo,
2917 a->progress_cb, a->progress_arg);
2919 } else if (S_ISREG(sb.st_mode)) {
2920 err = merge_blob(&local_changes_subsumed,
2921 a->worktree, NULL, ondisk_path, path2,
2922 sb.st_mode, a->label_orig, blob2,
2923 a->commit_id2, repo, a->progress_cb,
2926 err = got_error_path(ondisk_path,
2927 GOT_ERR_FILE_OBSTRUCTED);
2931 if (status == GOT_STATUS_DELETE) {
2932 err = got_fileindex_entry_update(ie,
2933 ondisk_path, blob2->id.sha1,
2934 a->worktree->base_commit_id->sha1, 0);
2939 int is_bad_symlink = 0;
2940 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2941 if (S_ISLNK(mode2)) {
2942 err = install_symlink(&is_bad_symlink,
2943 a->worktree, ondisk_path, path2, blob2, 0,
2944 0, 1, repo, a->progress_cb, a->progress_arg);
2946 err = install_blob(a->worktree, ondisk_path, path2,
2947 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2948 a->progress_cb, a->progress_arg);
2952 err = got_fileindex_entry_alloc(&ie, path2);
2955 err = got_fileindex_entry_update(ie, ondisk_path,
2958 got_fileindex_entry_free(ie);
2961 err = got_fileindex_entry_add(a->fileindex, ie);
2963 got_fileindex_entry_free(ie);
2966 if (is_bad_symlink) {
2967 got_fileindex_entry_filetype_set(ie,
2968 GOT_FILEIDX_MODE_BAD_SYMLINK);
2977 struct check_merge_ok_arg {
2978 struct got_worktree *worktree;
2979 struct got_repository *repo;
2982 static const struct got_error *
2983 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2985 const struct got_error *err = NULL;
2986 struct check_merge_ok_arg *a = arg;
2987 unsigned char status;
2991 /* Reject merges into a work tree with mixed base commits. */
2992 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2993 SHA1_DIGEST_LENGTH))
2994 return got_error(GOT_ERR_MIXED_COMMITS);
2996 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2998 return got_error_from_errno("asprintf");
3000 /* Reject merges into a work tree with conflicted files. */
3001 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3004 if (status == GOT_STATUS_CONFLICT)
3005 return got_error(GOT_ERR_CONFLICTS);
3010 static const struct got_error *
3011 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3012 const char *fileindex_path, struct got_object_id *commit_id1,
3013 struct got_object_id *commit_id2, struct got_repository *repo,
3014 got_worktree_checkout_cb progress_cb, void *progress_arg,
3015 got_cancel_cb cancel_cb, void *cancel_arg)
3017 const struct got_error *err = NULL, *sync_err;
3018 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3019 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3020 struct merge_file_cb_arg arg;
3021 char *label_orig = NULL;
3024 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3025 worktree->path_prefix);
3026 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3032 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3036 err = got_object_id_str(&id_str, commit_id1);
3040 if (asprintf(&label_orig, "%s: commit %s",
3041 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3042 err = got_error_from_errno("asprintf");
3049 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3050 worktree->path_prefix);
3054 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3058 arg.worktree = worktree;
3059 arg.fileindex = fileindex;
3060 arg.progress_cb = progress_cb;
3061 arg.progress_arg = progress_arg;
3062 arg.cancel_cb = cancel_cb;
3063 arg.cancel_arg = cancel_arg;
3064 arg.label_orig = label_orig;
3065 arg.commit_id2 = commit_id2;
3066 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3067 sync_err = sync_fileindex(fileindex, fileindex_path);
3068 if (sync_err && err == NULL)
3072 got_object_tree_close(tree1);
3074 got_object_tree_close(tree2);
3079 const struct got_error *
3080 got_worktree_merge_files(struct got_worktree *worktree,
3081 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3082 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3083 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3085 const struct got_error *err, *unlockerr;
3086 char *fileindex_path = NULL;
3087 struct got_fileindex *fileindex = NULL;
3088 struct check_merge_ok_arg mok_arg;
3090 err = lock_worktree(worktree, LOCK_EX);
3094 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3098 mok_arg.worktree = worktree;
3099 mok_arg.repo = repo;
3100 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
3105 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3106 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
3109 got_fileindex_free(fileindex);
3110 free(fileindex_path);
3111 unlockerr = lock_worktree(worktree, LOCK_SH);
3112 if (unlockerr && err == NULL)
3117 struct diff_dir_cb_arg {
3118 struct got_fileindex *fileindex;
3119 struct got_worktree *worktree;
3120 const char *status_path;
3121 size_t status_path_len;
3122 struct got_repository *repo;
3123 got_worktree_status_cb status_cb;
3125 got_cancel_cb cancel_cb;
3127 /* A pathlist containing per-directory pathlists of ignore patterns. */
3128 struct got_pathlist_head ignores;
3129 int report_unchanged;
3133 static const struct got_error *
3134 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3135 int dirfd, const char *de_name,
3136 got_worktree_status_cb status_cb, void *status_arg,
3137 struct got_repository *repo, int report_unchanged)
3139 const struct got_error *err = NULL;
3140 unsigned char status = GOT_STATUS_NO_CHANGE;
3141 unsigned char staged_status = get_staged_status(ie);
3143 struct got_object_id blob_id, commit_id, staged_blob_id;
3144 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3145 struct got_object_id *staged_blob_idp = NULL;
3147 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3151 if (status == GOT_STATUS_NO_CHANGE &&
3152 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3155 if (got_fileindex_entry_has_blob(ie)) {
3156 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3157 blob_idp = &blob_id;
3159 if (got_fileindex_entry_has_commit(ie)) {
3160 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3161 commit_idp = &commit_id;
3163 if (staged_status == GOT_STATUS_ADD ||
3164 staged_status == GOT_STATUS_MODIFY) {
3165 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3166 SHA1_DIGEST_LENGTH);
3167 staged_blob_idp = &staged_blob_id;
3170 return (*status_cb)(status_arg, status, staged_status,
3171 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3174 static const struct got_error *
3175 status_old_new(void *arg, struct got_fileindex_entry *ie,
3176 struct dirent *de, const char *parent_path, int dirfd)
3178 const struct got_error *err = NULL;
3179 struct diff_dir_cb_arg *a = arg;
3182 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3183 return got_error(GOT_ERR_CANCELLED);
3185 if (got_path_cmp(parent_path, a->status_path,
3186 strlen(parent_path), a->status_path_len) != 0 &&
3187 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3190 if (parent_path[0]) {
3191 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3192 parent_path, de->d_name) == -1)
3193 return got_error_from_errno("asprintf");
3195 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3197 return got_error_from_errno("asprintf");
3200 err = report_file_status(ie, abspath, dirfd, de->d_name,
3201 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3206 static const struct got_error *
3207 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3209 struct diff_dir_cb_arg *a = arg;
3210 struct got_object_id blob_id, commit_id;
3211 unsigned char status;
3213 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3214 return got_error(GOT_ERR_CANCELLED);
3216 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3219 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3220 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3221 if (got_fileindex_entry_has_file_on_disk(ie))
3222 status = GOT_STATUS_MISSING;
3224 status = GOT_STATUS_DELETE;
3225 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3226 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3230 free_ignorelist(struct got_pathlist_head *ignorelist)
3232 struct got_pathlist_entry *pe;
3234 TAILQ_FOREACH(pe, ignorelist, entry)
3235 free((char *)pe->path);
3236 got_pathlist_free(ignorelist);
3240 free_ignores(struct got_pathlist_head *ignores)
3242 struct got_pathlist_entry *pe;
3244 TAILQ_FOREACH(pe, ignores, entry) {
3245 struct got_pathlist_head *ignorelist = pe->data;
3246 free_ignorelist(ignorelist);
3247 free((char *)pe->path);
3249 got_pathlist_free(ignores);
3252 static const struct got_error *
3253 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3255 const struct got_error *err = NULL;
3256 struct got_pathlist_entry *pe = NULL;
3257 struct got_pathlist_head *ignorelist;
3258 char *line = NULL, *pattern, *dirpath = NULL;
3259 size_t linesize = 0;
3262 ignorelist = calloc(1, sizeof(*ignorelist));
3263 if (ignorelist == NULL)
3264 return got_error_from_errno("calloc");
3265 TAILQ_INIT(ignorelist);
3267 while ((linelen = getline(&line, &linesize, f)) != -1) {
3268 if (linelen > 0 && line[linelen - 1] == '\n')
3269 line[linelen - 1] = '\0';
3271 /* Git's ignores may contain comments. */
3275 /* Git's negated patterns are not (yet?) supported. */
3279 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3281 err = got_error_from_errno("asprintf");
3284 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3289 err = got_error_from_errno("getline");
3293 dirpath = strdup(path);
3294 if (dirpath == NULL) {
3295 err = got_error_from_errno("strdup");
3298 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3301 if (err || pe == NULL) {
3303 free_ignorelist(ignorelist);
3309 match_ignores(struct got_pathlist_head *ignores, const char *path)
3311 struct got_pathlist_entry *pe;
3313 /* Handle patterns which match in all directories. */
3314 TAILQ_FOREACH(pe, ignores, entry) {
3315 struct got_pathlist_head *ignorelist = pe->data;
3316 struct got_pathlist_entry *pi;
3318 TAILQ_FOREACH(pi, ignorelist, entry) {
3319 const char *p, *pattern = pi->path;
3321 if (strncmp(pattern, "**/", 3) != 0)
3326 if (fnmatch(pattern, p,
3327 FNM_PATHNAME | FNM_LEADING_DIR)) {
3328 /* Retry in next directory. */
3329 while (*p && *p != '/')
3341 * The ignores pathlist contains ignore lists from children before
3342 * parents, so we can find the most specific ignorelist by walking
3343 * ignores backwards.
3345 pe = TAILQ_LAST(ignores, got_pathlist_head);
3347 if (got_path_is_child(path, pe->path, pe->path_len)) {
3348 struct got_pathlist_head *ignorelist = pe->data;
3349 struct got_pathlist_entry *pi;
3350 TAILQ_FOREACH(pi, ignorelist, entry) {
3351 const char *pattern = pi->path;
3352 int flags = FNM_LEADING_DIR;
3353 if (strstr(pattern, "/**/") == NULL)
3354 flags |= FNM_PATHNAME;
3355 if (fnmatch(pattern, path, flags))
3360 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3366 static const struct got_error *
3367 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3368 const char *path, int dirfd, const char *ignores_filename)
3370 const struct got_error *err = NULL;
3373 FILE *ignoresfile = NULL;
3375 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3376 path[0] ? "/" : "", ignores_filename) == -1)
3377 return got_error_from_errno("asprintf");
3380 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3382 if (errno != ENOENT && errno != EACCES)
3383 err = got_error_from_errno2("openat",
3386 ignoresfile = fdopen(fd, "r");
3387 if (ignoresfile == NULL)
3388 err = got_error_from_errno2("fdopen",
3392 err = read_ignores(ignores, path, ignoresfile);
3396 ignoresfile = fopen(ignorespath, "r");
3397 if (ignoresfile == NULL) {
3398 if (errno != ENOENT && errno != EACCES)
3399 err = got_error_from_errno2("fopen",
3402 err = read_ignores(ignores, path, ignoresfile);
3405 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3406 err = got_error_from_errno2("fclose", path);
3407 if (fd != -1 && close(fd) == -1 && err == NULL)
3408 err = got_error_from_errno2("close", path);
3413 static const struct got_error *
3414 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3416 const struct got_error *err = NULL;
3417 struct diff_dir_cb_arg *a = arg;
3420 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3421 return got_error(GOT_ERR_CANCELLED);
3423 if (parent_path[0]) {
3424 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3425 return got_error_from_errno("asprintf");
3430 if (de->d_type != DT_DIR &&
3431 got_path_is_child(path, a->status_path, a->status_path_len)
3432 && !match_ignores(&a->ignores, path))
3433 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3434 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3440 static const struct got_error *
3441 status_traverse(void *arg, const char *path, int dirfd)
3443 const struct got_error *err = NULL;
3444 struct diff_dir_cb_arg *a = arg;
3449 err = add_ignores(&a->ignores, a->worktree->root_path,
3450 path, dirfd, ".cvsignore");
3454 err = add_ignores(&a->ignores, a->worktree->root_path, path,
3455 dirfd, ".gitignore");
3460 static const struct got_error *
3461 report_single_file_status(const char *path, const char *ondisk_path,
3462 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3463 void *status_arg, struct got_repository *repo, int report_unchanged)
3465 struct got_fileindex_entry *ie;
3468 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3470 return report_file_status(ie, ondisk_path, -1, NULL,
3471 status_cb, status_arg, repo, report_unchanged);
3473 if (lstat(ondisk_path, &sb) == -1) {
3474 if (errno != ENOENT)
3475 return got_error_from_errno2("lstat", ondisk_path);
3476 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3477 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3481 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3482 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3483 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3488 static const struct got_error *
3489 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3490 const char *root_path, const char *path)
3492 const struct got_error *err;
3493 char *parent_path, *next_parent_path = NULL;
3495 err = add_ignores(ignores, root_path, "", -1,
3500 err = add_ignores(ignores, root_path, "", -1,
3505 err = got_path_dirname(&parent_path, path);
3507 if (err->code == GOT_ERR_BAD_PATH)
3508 return NULL; /* cannot traverse parent */
3512 err = add_ignores(ignores, root_path, parent_path, -1,
3516 err = add_ignores(ignores, root_path, parent_path, -1,
3520 err = got_path_dirname(&next_parent_path, parent_path);
3522 if (err->code == GOT_ERR_BAD_PATH)
3523 err = NULL; /* traversed everything */
3527 parent_path = next_parent_path;
3528 next_parent_path = NULL;
3532 free(next_parent_path);
3536 static const struct got_error *
3537 worktree_status(struct got_worktree *worktree, const char *path,
3538 struct got_fileindex *fileindex, struct got_repository *repo,
3539 got_worktree_status_cb status_cb, void *status_arg,
3540 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3541 int report_unchanged)
3543 const struct got_error *err = NULL;
3545 struct got_fileindex_diff_dir_cb fdiff_cb;
3546 struct diff_dir_cb_arg arg;
3547 char *ondisk_path = NULL;
3549 TAILQ_INIT(&arg.ignores);
3551 if (asprintf(&ondisk_path, "%s%s%s",
3552 worktree->root_path, path[0] ? "/" : "", path) == -1)
3553 return got_error_from_errno("asprintf");
3555 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3557 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3559 err = got_error_from_errno2("open", ondisk_path);
3561 err = report_single_file_status(path, ondisk_path,
3562 fileindex, status_cb, status_arg, repo,
3565 fdiff_cb.diff_old_new = status_old_new;
3566 fdiff_cb.diff_old = status_old;
3567 fdiff_cb.diff_new = status_new;
3568 fdiff_cb.diff_traverse = status_traverse;
3569 arg.fileindex = fileindex;
3570 arg.worktree = worktree;
3571 arg.status_path = path;
3572 arg.status_path_len = strlen(path);
3574 arg.status_cb = status_cb;
3575 arg.status_arg = status_arg;
3576 arg.cancel_cb = cancel_cb;
3577 arg.cancel_arg = cancel_arg;
3578 arg.report_unchanged = report_unchanged;
3579 arg.no_ignores = no_ignores;
3581 err = add_ignores_from_parent_paths(&arg.ignores,
3582 worktree->root_path, path);
3586 err = got_fileindex_diff_dir(fileindex, fd,
3587 worktree->root_path, path, repo, &fdiff_cb, &arg);
3590 free_ignores(&arg.ignores);
3591 if (fd != -1 && close(fd) != 0 && err == NULL)
3592 err = got_error_from_errno("close");
3597 const struct got_error *
3598 got_worktree_status(struct got_worktree *worktree,
3599 struct got_pathlist_head *paths, struct got_repository *repo,
3600 got_worktree_status_cb status_cb, void *status_arg,
3601 got_cancel_cb cancel_cb, void *cancel_arg)
3603 const struct got_error *err = NULL;
3604 char *fileindex_path = NULL;
3605 struct got_fileindex *fileindex = NULL;
3606 struct got_pathlist_entry *pe;
3608 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3612 TAILQ_FOREACH(pe, paths, entry) {
3613 err = worktree_status(worktree, pe->path, fileindex, repo,
3614 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3618 free(fileindex_path);
3619 got_fileindex_free(fileindex);
3623 const struct got_error *
3624 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3627 const struct got_error *err = NULL;
3628 char *resolved = NULL, *cwd = NULL, *path = NULL;
3631 char *abspath = NULL;
3632 char canonpath[PATH_MAX];
3636 cwd = getcwd(NULL, 0);
3638 return got_error_from_errno("getcwd");
3640 if (lstat(arg, &sb) == -1) {
3641 if (errno != ENOENT) {
3642 err = got_error_from_errno2("lstat", arg);
3647 if (S_ISLNK(sb.st_mode)) {
3649 * We cannot use realpath(3) with symlinks since we want to
3650 * operate on the symlink itself.
3651 * But we can make the path absolute, assuming it is relative
3652 * to the current working directory, and then canonicalize it.
3654 if (!got_path_is_absolute(arg)) {
3655 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3656 err = got_error_from_errno("asprintf");
3661 err = got_canonpath(abspath ? abspath : arg, canonpath,
3665 resolved = strdup(canonpath);
3666 if (resolved == NULL) {
3667 err = got_error_from_errno("strdup");
3671 resolved = realpath(arg, NULL);
3672 if (resolved == NULL) {
3673 if (errno != ENOENT) {
3674 err = got_error_from_errno2("realpath", arg);
3677 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3678 err = got_error_from_errno("asprintf");
3681 err = got_canonpath(abspath, canonpath,
3685 resolved = strdup(canonpath);
3686 if (resolved == NULL) {
3687 err = got_error_from_errno("strdup");
3693 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3694 strlen(got_worktree_get_root_path(worktree)))) {
3695 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3699 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3700 err = got_path_skip_common_ancestor(&path,
3701 got_worktree_get_root_path(worktree), resolved);
3707 err = got_error_from_errno("strdup");
3712 /* XXX status walk can't deal with trailing slash! */
3714 while (len > 0 && path[len - 1] == '/') {
3715 path[len - 1] = '\0';
3729 struct schedule_addition_args {
3730 struct got_worktree *worktree;
3731 struct got_fileindex *fileindex;
3732 got_worktree_checkout_cb progress_cb;
3734 struct got_repository *repo;
3737 static const struct got_error *
3738 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3739 const char *relpath, struct got_object_id *blob_id,
3740 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3741 int dirfd, const char *de_name)
3743 struct schedule_addition_args *a = arg;
3744 const struct got_error *err = NULL;
3745 struct got_fileindex_entry *ie;
3749 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3751 return got_error_from_errno("asprintf");
3753 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3755 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3759 /* Re-adding an existing entry is a no-op. */
3760 if (status == GOT_STATUS_ADD)
3762 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3767 if (status != GOT_STATUS_UNVERSIONED) {
3768 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3772 err = got_fileindex_entry_alloc(&ie, relpath);
3775 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3777 got_fileindex_entry_free(ie);
3780 err = got_fileindex_entry_add(a->fileindex, ie);
3782 got_fileindex_entry_free(ie);
3789 if (status == GOT_STATUS_ADD)
3791 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3794 const struct got_error *
3795 got_worktree_schedule_add(struct got_worktree *worktree,
3796 struct got_pathlist_head *paths,
3797 got_worktree_checkout_cb progress_cb, void *progress_arg,
3798 struct got_repository *repo, int no_ignores)
3800 struct got_fileindex *fileindex = NULL;
3801 char *fileindex_path = NULL;
3802 const struct got_error *err = NULL, *sync_err, *unlockerr;
3803 struct got_pathlist_entry *pe;
3804 struct schedule_addition_args saa;
3806 err = lock_worktree(worktree, LOCK_EX);
3810 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3814 saa.worktree = worktree;
3815 saa.fileindex = fileindex;
3816 saa.progress_cb = progress_cb;
3817 saa.progress_arg = progress_arg;
3820 TAILQ_FOREACH(pe, paths, entry) {
3821 err = worktree_status(worktree, pe->path, fileindex, repo,
3822 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3826 sync_err = sync_fileindex(fileindex, fileindex_path);
3827 if (sync_err && err == NULL)
3830 free(fileindex_path);
3832 got_fileindex_free(fileindex);
3833 unlockerr = lock_worktree(worktree, LOCK_SH);
3834 if (unlockerr && err == NULL)
3839 struct schedule_deletion_args {
3840 struct got_worktree *worktree;
3841 struct got_fileindex *fileindex;
3842 got_worktree_delete_cb progress_cb;
3844 struct got_repository *repo;
3845 int delete_local_mods;
3847 const char *status_codes;
3850 static const struct got_error *
3851 schedule_for_deletion(void *arg, unsigned char status,
3852 unsigned char staged_status, const char *relpath,
3853 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3854 struct got_object_id *commit_id, int dirfd, const char *de_name)
3856 struct schedule_deletion_args *a = arg;
3857 const struct got_error *err = NULL;
3858 struct got_fileindex_entry *ie = NULL;
3862 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3864 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3866 staged_status = get_staged_status(ie);
3867 if (staged_status != GOT_STATUS_NO_CHANGE) {
3868 if (staged_status == GOT_STATUS_DELETE)
3870 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3873 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3875 return got_error_from_errno("asprintf");
3877 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3882 if (a->status_codes) {
3883 size_t ncodes = strlen(a->status_codes);
3885 for (i = 0; i < ncodes ; i++) {
3886 if (status == a->status_codes[i])
3890 /* Do not delete files in non-matching status. */
3894 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
3895 a->status_codes[i] != GOT_STATUS_MISSING) {
3896 static char msg[64];
3897 snprintf(msg, sizeof(msg),
3898 "invalid status code '%c'", a->status_codes[i]);
3899 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
3904 if (status != GOT_STATUS_NO_CHANGE) {
3905 if (status == GOT_STATUS_DELETE)
3907 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3908 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3911 if (status != GOT_STATUS_MODIFY &&
3912 status != GOT_STATUS_MISSING) {
3913 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3918 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3922 if (unlinkat(dirfd, de_name, 0) != 0) {
3923 err = got_error_from_errno2("unlinkat",
3927 } else if (unlink(ondisk_path) != 0) {
3928 err = got_error_from_errno2("unlink", ondisk_path);
3932 root_len = strlen(a->worktree->root_path);
3935 err = got_path_dirname(&parent, ondisk_path);
3939 ondisk_path = parent;
3940 if (rmdir(ondisk_path) == -1) {
3941 if (errno != ENOTEMPTY)
3942 err = got_error_from_errno2("rmdir",
3946 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
3947 strlen(ondisk_path), root_len) != 0);
3950 got_fileindex_entry_mark_deleted_from_disk(ie);
3955 if (status == GOT_STATUS_DELETE)
3957 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3958 staged_status, relpath);
3961 const struct got_error *
3962 got_worktree_schedule_delete(struct got_worktree *worktree,
3963 struct got_pathlist_head *paths, int delete_local_mods,
3964 const char *status_codes,
3965 got_worktree_delete_cb progress_cb, void *progress_arg,
3966 struct got_repository *repo, int keep_on_disk)
3968 struct got_fileindex *fileindex = NULL;
3969 char *fileindex_path = NULL;
3970 const struct got_error *err = NULL, *sync_err, *unlockerr;
3971 struct got_pathlist_entry *pe;
3972 struct schedule_deletion_args sda;
3974 err = lock_worktree(worktree, LOCK_EX);
3978 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3982 sda.worktree = worktree;
3983 sda.fileindex = fileindex;
3984 sda.progress_cb = progress_cb;
3985 sda.progress_arg = progress_arg;
3987 sda.delete_local_mods = delete_local_mods;
3988 sda.keep_on_disk = keep_on_disk;
3989 sda.status_codes = status_codes;
3991 TAILQ_FOREACH(pe, paths, entry) {
3992 err = worktree_status(worktree, pe->path, fileindex, repo,
3993 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3997 sync_err = sync_fileindex(fileindex, fileindex_path);
3998 if (sync_err && err == NULL)
4001 free(fileindex_path);
4003 got_fileindex_free(fileindex);
4004 unlockerr = lock_worktree(worktree, LOCK_SH);
4005 if (unlockerr && err == NULL)
4010 static const struct got_error *
4011 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4013 const struct got_error *err = NULL;
4015 size_t linesize = 0, n;
4018 linelen = getline(&line, &linesize, infile);
4019 if (linelen == -1) {
4020 if (ferror(infile)) {
4021 err = got_error_from_errno("getline");
4027 n = fwrite(line, 1, linelen, outfile);
4029 err = got_ferror(outfile, GOT_ERR_IO);
4034 n = fwrite(line, 1, linelen, rejectfile);
4036 err = got_ferror(outfile, GOT_ERR_IO);
4043 static const struct got_error *
4044 skip_one_line(FILE *f)
4047 size_t linesize = 0;
4050 linelen = getline(&line, &linesize, f);
4051 if (linelen == -1) {
4053 return got_error_from_errno("getline");
4060 static const struct got_error *
4061 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4062 int start_old, int end_old, int start_new, int end_new,
4063 FILE *outfile, FILE *rejectfile)
4065 const struct got_error *err;
4067 /* Copy old file's lines leading up to patch. */
4068 while (!feof(f1) && *line_cur1 < start_old) {
4069 err = copy_one_line(f1, outfile, NULL);
4074 /* Skip new file's lines leading up to patch. */
4075 while (!feof(f2) && *line_cur2 < start_new) {
4077 err = copy_one_line(f2, NULL, rejectfile);
4079 err = skip_one_line(f2);
4084 /* Copy patched lines. */
4085 while (!feof(f2) && *line_cur2 <= end_new) {
4086 err = copy_one_line(f2, outfile, NULL);
4091 /* Skip over old file's replaced lines. */
4092 while (!feof(f1) && *line_cur1 <= end_old) {
4094 err = copy_one_line(f1, NULL, rejectfile);
4096 err = skip_one_line(f1);
4105 static const struct got_error *
4106 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4107 FILE *outfile, FILE *rejectfile)
4109 const struct got_error *err;
4112 /* Copy old file's lines until EOF. */
4114 err = copy_one_line(f1, outfile, NULL);
4121 /* Copy new file's lines until EOF. */
4123 err = copy_one_line(f2, NULL, rejectfile);
4133 static const struct got_error *
4134 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
4135 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
4136 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
4137 int *line_cur2, FILE *outfile, FILE *rejectfile,
4138 got_worktree_patch_cb patch_cb, void *patch_arg)
4140 const struct got_error *err = NULL;
4141 int start_old = change->cv.a;
4142 int end_old = change->cv.b;
4143 int start_new = change->cv.c;
4144 int end_new = change->cv.d;
4148 *choice = GOT_PATCH_CHOICE_NONE;
4150 hunkfile = got_opentemp();
4151 if (hunkfile == NULL)
4152 return got_error_from_errno("got_opentemp");
4157 /* XXX TODO needs error checking */
4158 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
4160 if (fseek(f1, pos1, SEEK_SET) == -1) {
4161 err = got_ferror(f1, GOT_ERR_IO);
4164 if (fseek(f2, pos2, SEEK_SET) == -1) {
4165 err = got_ferror(f1, GOT_ERR_IO);
4168 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4169 err = got_ferror(hunkfile, GOT_ERR_IO);
4173 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4174 hunkfile, n, nchanges);
4179 case GOT_PATCH_CHOICE_YES:
4180 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4181 end_old, start_new, end_new, outfile, rejectfile);
4183 case GOT_PATCH_CHOICE_NO:
4184 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4185 end_old, start_new, end_new, rejectfile, outfile);
4187 case GOT_PATCH_CHOICE_QUIT:
4190 err = got_error(GOT_ERR_PATCH_CHOICE);
4194 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4195 err = got_error_from_errno("fclose");
4199 struct revert_file_args {
4200 struct got_worktree *worktree;
4201 struct got_fileindex *fileindex;
4202 got_worktree_checkout_cb progress_cb;
4204 got_worktree_patch_cb patch_cb;
4206 struct got_repository *repo;
4209 static const struct got_error *
4210 create_patched_content(char **path_outfile, int reverse_patch,
4211 struct got_object_id *blob_id, const char *path2,
4212 int dirfd2, const char *de_name2,
4213 const char *relpath, struct got_repository *repo,
4214 got_worktree_patch_cb patch_cb, void *patch_arg)
4216 const struct got_error *err;
4217 struct got_blob_object *blob = NULL;
4218 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4220 char link_target[PATH_MAX];
4221 ssize_t link_len = 0;
4222 char *path1 = NULL, *id_str = NULL;
4223 struct stat sb1, sb2;
4224 struct got_diff_changes *changes = NULL;
4225 struct got_diff_state *ds = NULL;
4226 struct got_diff_args *args = NULL;
4227 struct got_diff_change *change;
4228 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
4231 *path_outfile = NULL;
4233 err = got_object_id_str(&id_str, blob_id);
4238 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4240 if (errno != ELOOP) {
4241 err = got_error_from_errno2("openat", path2);
4244 link_len = readlinkat(dirfd2, de_name2,
4245 link_target, sizeof(link_target));
4247 return got_error_from_errno2("readlinkat", path2);
4248 sb2.st_mode = S_IFLNK;
4249 sb2.st_size = link_len;
4252 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4254 if (errno != ELOOP) {
4255 err = got_error_from_errno2("open", path2);
4258 link_len = readlink(path2, link_target,
4259 sizeof(link_target));
4261 return got_error_from_errno2("readlink", path2);
4262 sb2.st_mode = S_IFLNK;
4263 sb2.st_size = link_len;
4267 if (fstat(fd2, &sb2) == -1) {
4268 err = got_error_from_errno2("fstat", path2);
4272 f2 = fdopen(fd2, "r");
4274 err = got_error_from_errno2("fdopen", path2);
4280 f2 = got_opentemp();
4282 err = got_error_from_errno2("got_opentemp", path2);
4285 n = fwrite(link_target, 1, link_len, f2);
4286 if (n != link_len) {
4287 err = got_ferror(f2, GOT_ERR_IO);
4290 if (fflush(f2) == EOF) {
4291 err = got_error_from_errno("fflush");
4297 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4301 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4305 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4309 if (stat(path1, &sb1) == -1) {
4310 err = got_error_from_errno2("stat", path1);
4314 err = got_diff_files(&changes, &ds, &args, &diff_flags,
4315 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
4319 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4323 if (fseek(f1, 0L, SEEK_SET) == -1)
4324 return got_ferror(f1, GOT_ERR_IO);
4325 if (fseek(f2, 0L, SEEK_SET) == -1)
4326 return got_ferror(f2, GOT_ERR_IO);
4327 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
4329 err = apply_or_reject_change(&choice, change, ++n,
4330 changes->nchanges, ds, args, diff_flags, relpath,
4331 f1, f2, &line_cur1, &line_cur2,
4332 reverse_patch ? NULL : outfile,
4333 reverse_patch ? outfile : NULL,
4334 patch_cb, patch_arg);
4337 if (choice == GOT_PATCH_CHOICE_YES)
4339 else if (choice == GOT_PATCH_CHOICE_QUIT)
4343 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4344 reverse_patch ? NULL : outfile,
4345 reverse_patch ? outfile : NULL);
4349 if (!S_ISLNK(sb2.st_mode)) {
4350 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4351 err = got_error_from_errno2("fchmod", path2);
4359 got_object_blob_close(blob);
4360 if (f1 && fclose(f1) == EOF && err == NULL)
4361 err = got_error_from_errno2("fclose", path1);
4362 if (f2 && fclose(f2) == EOF && err == NULL)
4363 err = got_error_from_errno2("fclose", path2);
4364 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4365 err = got_error_from_errno2("close", path2);
4366 if (outfile && fclose(outfile) == EOF && err == NULL)
4367 err = got_error_from_errno2("fclose", *path_outfile);
4368 if (path1 && unlink(path1) == -1 && err == NULL)
4369 err = got_error_from_errno2("unlink", path1);
4370 if (err || !have_content) {
4371 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4372 err = got_error_from_errno2("unlink", *path_outfile);
4373 free(*path_outfile);
4374 *path_outfile = NULL;
4378 got_diff_state_free(ds);
4382 got_diff_free_changes(changes);
4387 static const struct got_error *
4388 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4389 const char *relpath, struct got_object_id *blob_id,
4390 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4391 int dirfd, const char *de_name)
4393 struct revert_file_args *a = arg;
4394 const struct got_error *err = NULL;
4395 char *parent_path = NULL;
4396 struct got_fileindex_entry *ie;
4397 struct got_tree_object *tree = NULL;
4398 struct got_object_id *tree_id = NULL;
4399 const struct got_tree_entry *te = NULL;
4400 char *tree_path = NULL, *te_name;
4401 char *ondisk_path = NULL, *path_content = NULL;
4402 struct got_blob_object *blob = NULL;
4404 /* Reverting a staged deletion is a no-op. */
4405 if (status == GOT_STATUS_DELETE &&
4406 staged_status != GOT_STATUS_NO_CHANGE)
4409 if (status == GOT_STATUS_UNVERSIONED)
4410 return (*a->progress_cb)(a->progress_arg,
4411 GOT_STATUS_UNVERSIONED, relpath);
4413 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4415 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4417 /* Construct in-repository path of tree which contains this blob. */
4418 err = got_path_dirname(&parent_path, ie->path);
4420 if (err->code != GOT_ERR_BAD_PATH)
4422 parent_path = strdup("/");
4423 if (parent_path == NULL) {
4424 err = got_error_from_errno("strdup");
4428 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4429 tree_path = strdup(parent_path);
4430 if (tree_path == NULL) {
4431 err = got_error_from_errno("strdup");
4435 if (got_path_is_root_dir(parent_path)) {
4436 tree_path = strdup(a->worktree->path_prefix);
4437 if (tree_path == NULL) {
4438 err = got_error_from_errno("strdup");
4442 if (asprintf(&tree_path, "%s/%s",
4443 a->worktree->path_prefix, parent_path) == -1) {
4444 err = got_error_from_errno("asprintf");
4450 err = got_object_id_by_path(&tree_id, a->repo,
4451 a->worktree->base_commit_id, tree_path);
4453 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4454 (status == GOT_STATUS_ADD ||
4455 staged_status == GOT_STATUS_ADD)))
4458 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4462 err = got_path_basename(&te_name, ie->path);
4466 te = got_object_tree_find_entry(tree, te_name);
4468 if (te == NULL && status != GOT_STATUS_ADD &&
4469 staged_status != GOT_STATUS_ADD) {
4470 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4476 case GOT_STATUS_ADD:
4478 int choice = GOT_PATCH_CHOICE_NONE;
4479 err = (*a->patch_cb)(&choice, a->patch_arg,
4480 status, ie->path, NULL, 1, 1);
4483 if (choice != GOT_PATCH_CHOICE_YES)
4486 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4490 got_fileindex_entry_remove(a->fileindex, ie);
4492 case GOT_STATUS_DELETE:
4494 int choice = GOT_PATCH_CHOICE_NONE;
4495 err = (*a->patch_cb)(&choice, a->patch_arg,
4496 status, ie->path, NULL, 1, 1);
4499 if (choice != GOT_PATCH_CHOICE_YES)
4503 case GOT_STATUS_MODIFY:
4504 case GOT_STATUS_MODE_CHANGE:
4505 case GOT_STATUS_CONFLICT:
4506 case GOT_STATUS_MISSING: {
4507 struct got_object_id id;
4508 if (staged_status == GOT_STATUS_ADD ||
4509 staged_status == GOT_STATUS_MODIFY) {
4510 memcpy(id.sha1, ie->staged_blob_sha1,
4511 SHA1_DIGEST_LENGTH);
4513 memcpy(id.sha1, ie->blob_sha1,
4514 SHA1_DIGEST_LENGTH);
4515 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4519 if (asprintf(&ondisk_path, "%s/%s",
4520 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4521 err = got_error_from_errno("asprintf");
4525 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4526 status == GOT_STATUS_CONFLICT)) {
4527 int is_bad_symlink = 0;
4528 err = create_patched_content(&path_content, 1, &id,
4529 ondisk_path, dirfd, de_name, ie->path, a->repo,
4530 a->patch_cb, a->patch_arg);
4531 if (err || path_content == NULL)
4533 if (te && S_ISLNK(te->mode)) {
4534 if (unlink(path_content) == -1) {
4535 err = got_error_from_errno2("unlink",
4539 err = install_symlink(&is_bad_symlink,
4540 a->worktree, ondisk_path, ie->path,
4541 blob, 0, 1, 0, a->repo,
4542 a->progress_cb, a->progress_arg);
4544 if (rename(path_content, ondisk_path) == -1) {
4545 err = got_error_from_errno3("rename",
4546 path_content, ondisk_path);
4551 int is_bad_symlink = 0;
4552 if (te && S_ISLNK(te->mode)) {
4553 err = install_symlink(&is_bad_symlink,
4554 a->worktree, ondisk_path, ie->path,
4555 blob, 0, 1, 0, a->repo,
4556 a->progress_cb, a->progress_arg);
4558 err = install_blob(a->worktree, ondisk_path,
4560 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4561 got_fileindex_perms_to_st(ie), blob,
4562 0, 1, 0, 0, a->repo,
4563 a->progress_cb, a->progress_arg);
4567 if (status == GOT_STATUS_DELETE ||
4568 status == GOT_STATUS_MODE_CHANGE) {
4569 err = got_fileindex_entry_update(ie,
4570 ondisk_path, blob->id.sha1,
4571 a->worktree->base_commit_id->sha1, 1);
4575 if (is_bad_symlink) {
4576 got_fileindex_entry_filetype_set(ie,
4577 GOT_FILEIDX_MODE_BAD_SYMLINK);
4591 got_object_blob_close(blob);
4593 got_object_tree_close(tree);
4598 const struct got_error *
4599 got_worktree_revert(struct got_worktree *worktree,
4600 struct got_pathlist_head *paths,
4601 got_worktree_checkout_cb progress_cb, void *progress_arg,
4602 got_worktree_patch_cb patch_cb, void *patch_arg,
4603 struct got_repository *repo)
4605 struct got_fileindex *fileindex = NULL;
4606 char *fileindex_path = NULL;
4607 const struct got_error *err = NULL, *unlockerr = NULL;
4608 const struct got_error *sync_err = NULL;
4609 struct got_pathlist_entry *pe;
4610 struct revert_file_args rfa;
4612 err = lock_worktree(worktree, LOCK_EX);
4616 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4620 rfa.worktree = worktree;
4621 rfa.fileindex = fileindex;
4622 rfa.progress_cb = progress_cb;
4623 rfa.progress_arg = progress_arg;
4624 rfa.patch_cb = patch_cb;
4625 rfa.patch_arg = patch_arg;
4627 TAILQ_FOREACH(pe, paths, entry) {
4628 err = worktree_status(worktree, pe->path, fileindex, repo,
4629 revert_file, &rfa, NULL, NULL, 0, 0);
4633 sync_err = sync_fileindex(fileindex, fileindex_path);
4634 if (sync_err && err == NULL)
4637 free(fileindex_path);
4639 got_fileindex_free(fileindex);
4640 unlockerr = lock_worktree(worktree, LOCK_SH);
4641 if (unlockerr && err == NULL)
4647 free_commitable(struct got_commitable *ct)
4650 free(ct->in_repo_path);
4651 free(ct->ondisk_path);
4653 free(ct->base_blob_id);
4654 free(ct->staged_blob_id);
4655 free(ct->base_commit_id);
4659 struct collect_commitables_arg {
4660 struct got_pathlist_head *commitable_paths;
4661 struct got_repository *repo;
4662 struct got_worktree *worktree;
4663 struct got_fileindex *fileindex;
4664 int have_staged_files;
4665 int allow_bad_symlinks;
4668 static const struct got_error *
4669 collect_commitables(void *arg, unsigned char status,
4670 unsigned char staged_status, const char *relpath,
4671 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4672 struct got_object_id *commit_id, int dirfd, const char *de_name)
4674 struct collect_commitables_arg *a = arg;
4675 const struct got_error *err = NULL;
4676 struct got_commitable *ct = NULL;
4677 struct got_pathlist_entry *new = NULL;
4678 char *parent_path = NULL, *path = NULL;
4681 if (a->have_staged_files) {
4682 if (staged_status != GOT_STATUS_MODIFY &&
4683 staged_status != GOT_STATUS_ADD &&
4684 staged_status != GOT_STATUS_DELETE)
4687 if (status == GOT_STATUS_CONFLICT)
4688 return got_error(GOT_ERR_COMMIT_CONFLICT);
4690 if (status != GOT_STATUS_MODIFY &&
4691 status != GOT_STATUS_MODE_CHANGE &&
4692 status != GOT_STATUS_ADD &&
4693 status != GOT_STATUS_DELETE)
4697 if (asprintf(&path, "/%s", relpath) == -1) {
4698 err = got_error_from_errno("asprintf");
4701 if (strcmp(path, "/") == 0) {
4702 parent_path = strdup("");
4703 if (parent_path == NULL)
4704 return got_error_from_errno("strdup");
4706 err = got_path_dirname(&parent_path, path);
4711 ct = calloc(1, sizeof(*ct));
4713 err = got_error_from_errno("calloc");
4717 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4719 err = got_error_from_errno("asprintf");
4723 if (staged_status == GOT_STATUS_ADD ||
4724 staged_status == GOT_STATUS_MODIFY) {
4725 struct got_fileindex_entry *ie;
4726 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4727 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4728 case GOT_FILEIDX_MODE_REGULAR_FILE:
4729 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4732 case GOT_FILEIDX_MODE_SYMLINK:
4736 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4739 ct->mode |= got_fileindex_entry_perms_get(ie);
4740 } else if (status != GOT_STATUS_DELETE &&
4741 staged_status != GOT_STATUS_DELETE) {
4743 if (fstatat(dirfd, de_name, &sb,
4744 AT_SYMLINK_NOFOLLOW) == -1) {
4745 err = got_error_from_errno2("fstatat",
4749 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4750 err = got_error_from_errno2("lstat", ct->ondisk_path);
4753 ct->mode = sb.st_mode;
4756 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4757 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4759 err = got_error_from_errno("asprintf");
4763 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4764 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4766 char target_path[PATH_MAX];
4768 target_len = readlink(ct->ondisk_path, target_path,
4769 sizeof(target_path));
4770 if (target_len == -1) {
4771 err = got_error_from_errno2("readlink",
4775 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4776 target_len, ct->ondisk_path, a->worktree->root_path);
4779 if (is_bad_symlink) {
4780 err = got_error_path(ct->ondisk_path,
4781 GOT_ERR_BAD_SYMLINK);
4787 ct->status = status;
4788 ct->staged_status = staged_status;
4789 ct->blob_id = NULL; /* will be filled in when blob gets created */
4790 if (ct->status != GOT_STATUS_ADD &&
4791 ct->staged_status != GOT_STATUS_ADD) {
4792 ct->base_blob_id = got_object_id_dup(blob_id);
4793 if (ct->base_blob_id == NULL) {
4794 err = got_error_from_errno("got_object_id_dup");
4797 ct->base_commit_id = got_object_id_dup(commit_id);
4798 if (ct->base_commit_id == NULL) {
4799 err = got_error_from_errno("got_object_id_dup");
4803 if (ct->staged_status == GOT_STATUS_ADD ||
4804 ct->staged_status == GOT_STATUS_MODIFY) {
4805 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4806 if (ct->staged_blob_id == NULL) {
4807 err = got_error_from_errno("got_object_id_dup");
4811 ct->path = strdup(path);
4812 if (ct->path == NULL) {
4813 err = got_error_from_errno("strdup");
4816 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4818 if (ct && (err || new == NULL))
4819 free_commitable(ct);
4825 static const struct got_error *write_tree(struct got_object_id **, int *,
4826 struct got_tree_object *, const char *, struct got_pathlist_head *,
4827 got_worktree_status_cb status_cb, void *status_arg,
4828 struct got_repository *);
4830 static const struct got_error *
4831 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4832 struct got_tree_entry *te, const char *parent_path,
4833 struct got_pathlist_head *commitable_paths,
4834 got_worktree_status_cb status_cb, void *status_arg,
4835 struct got_repository *repo)
4837 const struct got_error *err = NULL;
4838 struct got_tree_object *subtree;
4841 if (asprintf(&subpath, "%s%s%s", parent_path,
4842 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4843 return got_error_from_errno("asprintf");
4845 err = got_object_open_as_tree(&subtree, repo, &te->id);
4849 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4850 commitable_paths, status_cb, status_arg, repo);
4851 got_object_tree_close(subtree);
4856 static const struct got_error *
4857 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4859 const struct got_error *err = NULL;
4860 char *ct_parent_path = NULL;
4864 if (strchr(ct->in_repo_path, '/') == NULL) {
4865 *match = got_path_is_root_dir(path);
4869 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4872 *match = (strcmp(path, ct_parent_path) == 0);
4873 free(ct_parent_path);
4878 get_ct_file_mode(struct got_commitable *ct)
4880 if (S_ISLNK(ct->mode))
4883 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4886 static const struct got_error *
4887 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4888 struct got_tree_entry *te, struct got_commitable *ct)
4890 const struct got_error *err = NULL;
4894 err = got_object_tree_entry_dup(new_te, te);
4898 (*new_te)->mode = get_ct_file_mode(ct);
4900 if (ct->staged_status == GOT_STATUS_MODIFY)
4901 memcpy(&(*new_te)->id, ct->staged_blob_id,
4902 sizeof((*new_te)->id));
4904 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4906 if (err && *new_te) {
4913 static const struct got_error *
4914 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4915 struct got_commitable *ct)
4917 const struct got_error *err = NULL;
4918 char *ct_name = NULL;
4922 *new_te = calloc(1, sizeof(**new_te));
4923 if (*new_te == NULL)
4924 return got_error_from_errno("calloc");
4926 err = got_path_basename(&ct_name, ct->path);
4929 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4930 sizeof((*new_te)->name)) {
4931 err = got_error(GOT_ERR_NO_SPACE);
4935 (*new_te)->mode = get_ct_file_mode(ct);
4937 if (ct->staged_status == GOT_STATUS_ADD)
4938 memcpy(&(*new_te)->id, ct->staged_blob_id,
4939 sizeof((*new_te)->id));
4941 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4944 if (err && *new_te) {
4951 static const struct got_error *
4952 insert_tree_entry(struct got_tree_entry *new_te,
4953 struct got_pathlist_head *paths)
4955 const struct got_error *err = NULL;
4956 struct got_pathlist_entry *new_pe;
4958 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4962 return got_error(GOT_ERR_TREE_DUP_ENTRY);
4966 static const struct got_error *
4967 report_ct_status(struct got_commitable *ct,
4968 got_worktree_status_cb status_cb, void *status_arg)
4970 const char *ct_path = ct->path;
4971 unsigned char status;
4973 while (ct_path[0] == '/')
4976 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4977 status = ct->staged_status;
4979 status = ct->status;
4981 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4982 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4985 static const struct got_error *
4986 match_modified_subtree(int *modified, struct got_tree_entry *te,
4987 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4989 const struct got_error *err = NULL;
4990 struct got_pathlist_entry *pe;
4995 if (asprintf(&te_path, "%s%s%s", base_tree_path,
4996 got_path_is_root_dir(base_tree_path) ? "" : "/",
4998 return got_error_from_errno("asprintf");
5000 TAILQ_FOREACH(pe, commitable_paths, entry) {
5001 struct got_commitable *ct = pe->data;
5002 *modified = got_path_is_child(ct->in_repo_path, te_path,
5012 static const struct got_error *
5013 match_deleted_or_modified_ct(struct got_commitable **ctp,
5014 struct got_tree_entry *te, const char *base_tree_path,
5015 struct got_pathlist_head *commitable_paths)
5017 const struct got_error *err = NULL;
5018 struct got_pathlist_entry *pe;
5022 TAILQ_FOREACH(pe, commitable_paths, entry) {
5023 struct got_commitable *ct = pe->data;
5024 char *ct_name = NULL;
5027 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5028 if (ct->status != GOT_STATUS_MODIFY &&
5029 ct->status != GOT_STATUS_MODE_CHANGE &&
5030 ct->status != GOT_STATUS_DELETE)
5033 if (ct->staged_status != GOT_STATUS_MODIFY &&
5034 ct->staged_status != GOT_STATUS_DELETE)
5038 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5041 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5047 err = got_path_basename(&ct_name, pe->path);
5051 if (strcmp(te->name, ct_name) != 0) {
5064 static const struct got_error *
5065 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5066 const char *child_path, const char *path_base_tree,
5067 struct got_pathlist_head *commitable_paths,
5068 got_worktree_status_cb status_cb, void *status_arg,
5069 struct got_repository *repo)
5071 const struct got_error *err = NULL;
5072 struct got_tree_entry *new_te;
5074 struct got_object_id *id = NULL;
5079 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5080 got_path_is_root_dir(path_base_tree) ? "" : "/",
5082 return got_error_from_errno("asprintf");
5084 new_te = calloc(1, sizeof(*new_te));
5086 return got_error_from_errno("calloc");
5087 new_te->mode = S_IFDIR;
5089 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5090 sizeof(new_te->name)) {
5091 err = got_error(GOT_ERR_NO_SPACE);
5094 err = write_tree(&id, &nentries, NULL, subtree_path,
5095 commitable_paths, status_cb, status_arg, repo);
5100 memcpy(&new_te->id, id, sizeof(new_te->id));
5109 static const struct got_error *
5110 write_tree(struct got_object_id **new_tree_id, int *nentries,
5111 struct got_tree_object *base_tree, const char *path_base_tree,
5112 struct got_pathlist_head *commitable_paths,
5113 got_worktree_status_cb status_cb, void *status_arg,
5114 struct got_repository *repo)
5116 const struct got_error *err = NULL;
5117 struct got_pathlist_head paths;
5118 struct got_tree_entry *te, *new_te = NULL;
5119 struct got_pathlist_entry *pe;
5124 /* Insert, and recurse into, newly added entries first. */
5125 TAILQ_FOREACH(pe, commitable_paths, entry) {
5126 struct got_commitable *ct = pe->data;
5127 char *child_path = NULL, *slash;
5129 if ((ct->status != GOT_STATUS_ADD &&
5130 ct->staged_status != GOT_STATUS_ADD) ||
5131 (ct->flags & GOT_COMMITABLE_ADDED))
5134 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5135 strlen(path_base_tree)))
5138 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5143 slash = strchr(child_path, '/');
5144 if (slash == NULL) {
5145 err = alloc_added_blob_tree_entry(&new_te, ct);
5148 err = report_ct_status(ct, status_cb, status_arg);
5151 ct->flags |= GOT_COMMITABLE_ADDED;
5152 err = insert_tree_entry(new_te, &paths);
5157 *slash = '\0'; /* trim trailing path components */
5158 if (base_tree == NULL ||
5159 got_object_tree_find_entry(base_tree, child_path)
5161 err = make_subtree_for_added_blob(&new_te,
5162 child_path, path_base_tree,
5163 commitable_paths, status_cb, status_arg,
5167 err = insert_tree_entry(new_te, &paths);
5176 int i, nbase_entries;
5177 /* Handle modified and deleted entries. */
5178 nbase_entries = got_object_tree_get_nentries(base_tree);
5179 for (i = 0; i < nbase_entries; i++) {
5180 struct got_commitable *ct = NULL;
5182 te = got_object_tree_get_entry(base_tree, i);
5183 if (got_object_tree_entry_is_submodule(te)) {
5184 /* Entry is a submodule; just copy it. */
5185 err = got_object_tree_entry_dup(&new_te, te);
5188 err = insert_tree_entry(new_te, &paths);
5195 if (S_ISDIR(te->mode)) {
5197 err = got_object_tree_entry_dup(&new_te, te);
5200 err = match_modified_subtree(&modified, te,
5201 path_base_tree, commitable_paths);
5204 /* Avoid recursion into unmodified subtrees. */
5206 struct got_object_id *new_id;
5208 err = write_subtree(&new_id,
5210 path_base_tree, commitable_paths,
5211 status_cb, status_arg, repo);
5214 if (nsubentries == 0) {
5215 /* All entries were deleted. */
5219 memcpy(&new_te->id, new_id,
5220 sizeof(new_te->id));
5223 err = insert_tree_entry(new_te, &paths);
5230 err = match_deleted_or_modified_ct(&ct, te,
5231 path_base_tree, commitable_paths);
5235 /* NB: Deleted entries get dropped here. */
5236 if (ct->status == GOT_STATUS_MODIFY ||
5237 ct->status == GOT_STATUS_MODE_CHANGE ||
5238 ct->staged_status == GOT_STATUS_MODIFY) {
5239 err = alloc_modified_blob_tree_entry(
5243 err = insert_tree_entry(new_te, &paths);
5248 err = report_ct_status(ct, status_cb,
5253 /* Entry is unchanged; just copy it. */
5254 err = got_object_tree_entry_dup(&new_te, te);
5257 err = insert_tree_entry(new_te, &paths);
5265 /* Write new list of entries; deleted entries have been dropped. */
5266 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5268 got_pathlist_free(&paths);
5272 static const struct got_error *
5273 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
5274 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
5275 int have_staged_files)
5277 const struct got_error *err = NULL;
5278 struct got_pathlist_entry *pe;
5280 TAILQ_FOREACH(pe, commitable_paths, entry) {
5281 struct got_fileindex_entry *ie;
5282 struct got_commitable *ct = pe->data;
5284 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5286 if (ct->status == GOT_STATUS_DELETE ||
5287 ct->staged_status == GOT_STATUS_DELETE) {
5288 got_fileindex_entry_remove(fileindex, ie);
5289 } else if (ct->staged_status == GOT_STATUS_ADD ||
5290 ct->staged_status == GOT_STATUS_MODIFY) {
5291 got_fileindex_entry_stage_set(ie,
5292 GOT_FILEIDX_STAGE_NONE);
5293 got_fileindex_entry_staged_filetype_set(ie, 0);
5294 err = got_fileindex_entry_update(ie,
5295 ct->ondisk_path, ct->staged_blob_id->sha1,
5296 new_base_commit_id->sha1,
5297 !have_staged_files);
5299 err = got_fileindex_entry_update(ie,
5300 ct->ondisk_path, ct->blob_id->sha1,
5301 new_base_commit_id->sha1,
5302 !have_staged_files);
5304 err = got_fileindex_entry_alloc(&ie, pe->path);
5307 err = got_fileindex_entry_update(ie, ct->ondisk_path,
5308 ct->blob_id->sha1, new_base_commit_id->sha1, 1);
5310 got_fileindex_entry_free(ie);
5313 err = got_fileindex_entry_add(fileindex, ie);
5315 got_fileindex_entry_free(ie);
5324 static const struct got_error *
5325 check_out_of_date(const char *in_repo_path, unsigned char status,
5326 unsigned char staged_status, struct got_object_id *base_blob_id,
5327 struct got_object_id *base_commit_id,
5328 struct got_object_id *head_commit_id, struct got_repository *repo,
5331 const struct got_error *err = NULL;
5332 struct got_object_id *id = NULL;
5334 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5335 /* Trivial case: base commit == head commit */
5336 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5339 * Ensure file content which local changes were based
5340 * on matches file content in the branch head.
5342 err = got_object_id_by_path(&id, repo, head_commit_id,
5345 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5346 err = got_error(ood_errcode);
5348 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5349 err = got_error(ood_errcode);
5351 /* Require that added files don't exist in the branch head. */
5352 err = got_object_id_by_path(&id, repo, head_commit_id,
5354 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5356 err = id ? got_error(ood_errcode) : NULL;
5363 const struct got_error *
5364 commit_worktree(struct got_object_id **new_commit_id,
5365 struct got_pathlist_head *commitable_paths,
5366 struct got_object_id *head_commit_id, struct got_worktree *worktree,
5367 const char *author, const char *committer,
5368 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5369 got_worktree_status_cb status_cb, void *status_arg,
5370 struct got_repository *repo)
5372 const struct got_error *err = NULL, *unlockerr = NULL;
5373 struct got_pathlist_entry *pe;
5374 const char *head_ref_name = NULL;
5375 struct got_commit_object *head_commit = NULL;
5376 struct got_reference *head_ref2 = NULL;
5377 struct got_object_id *head_commit_id2 = NULL;
5378 struct got_tree_object *head_tree = NULL;
5379 struct got_object_id *new_tree_id = NULL;
5381 struct got_object_id_queue parent_ids;
5382 struct got_object_qid *pid = NULL;
5383 char *logmsg = NULL;
5385 *new_commit_id = NULL;
5387 SIMPLEQ_INIT(&parent_ids);
5389 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5393 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5397 if (commit_msg_cb != NULL) {
5398 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5403 if (logmsg == NULL || strlen(logmsg) == 0) {
5404 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5408 /* Create blobs from added and modified files and record their IDs. */
5409 TAILQ_FOREACH(pe, commitable_paths, entry) {
5410 struct got_commitable *ct = pe->data;
5413 /* Blobs for staged files already exist. */
5414 if (ct->staged_status == GOT_STATUS_ADD ||
5415 ct->staged_status == GOT_STATUS_MODIFY)
5418 if (ct->status != GOT_STATUS_ADD &&
5419 ct->status != GOT_STATUS_MODIFY &&
5420 ct->status != GOT_STATUS_MODE_CHANGE)
5423 if (asprintf(&ondisk_path, "%s/%s",
5424 worktree->root_path, pe->path) == -1) {
5425 err = got_error_from_errno("asprintf");
5428 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5434 /* Recursively write new tree objects. */
5435 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5436 commitable_paths, status_cb, status_arg, repo);
5440 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5443 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5444 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5445 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5446 got_object_qid_free(pid);
5452 /* Check if a concurrent commit to our branch has occurred. */
5453 head_ref_name = got_worktree_get_head_ref_name(worktree);
5454 if (head_ref_name == NULL) {
5455 err = got_error_from_errno("got_worktree_get_head_ref_name");
5458 /* Lock the reference here to prevent concurrent modification. */
5459 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5462 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5465 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5466 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5469 /* Update branch head in repository. */
5470 err = got_ref_change_ref(head_ref2, *new_commit_id);
5473 err = got_ref_write(head_ref2, repo);
5477 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5481 err = ref_base_commit(worktree, repo);
5486 got_object_tree_close(head_tree);
5488 got_object_commit_close(head_commit);
5489 free(head_commit_id2);
5491 unlockerr = got_ref_unlock(head_ref2);
5492 if (unlockerr && err == NULL)
5494 got_ref_close(head_ref2);
5499 static const struct got_error *
5500 check_path_is_commitable(const char *path,
5501 struct got_pathlist_head *commitable_paths)
5503 struct got_pathlist_entry *cpe = NULL;
5504 size_t path_len = strlen(path);
5506 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5507 struct got_commitable *ct = cpe->data;
5508 const char *ct_path = ct->path;
5510 while (ct_path[0] == '/')
5513 if (strcmp(path, ct_path) == 0 ||
5514 got_path_is_child(ct_path, path, path_len))
5519 return got_error_path(path, GOT_ERR_BAD_PATH);
5524 static const struct got_error *
5525 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5527 int *have_staged_files = arg;
5529 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5530 *have_staged_files = 1;
5531 return got_error(GOT_ERR_CANCELLED);
5537 static const struct got_error *
5538 check_non_staged_files(struct got_fileindex *fileindex,
5539 struct got_pathlist_head *paths)
5541 struct got_pathlist_entry *pe;
5542 struct got_fileindex_entry *ie;
5544 TAILQ_FOREACH(pe, paths, entry) {
5545 if (pe->path[0] == '\0')
5547 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5549 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5550 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5551 return got_error_path(pe->path,
5552 GOT_ERR_FILE_NOT_STAGED);
5558 const struct got_error *
5559 got_worktree_commit(struct got_object_id **new_commit_id,
5560 struct got_worktree *worktree, struct got_pathlist_head *paths,
5561 const char *author, const char *committer, int allow_bad_symlinks,
5562 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5563 got_worktree_status_cb status_cb, void *status_arg,
5564 struct got_repository *repo)
5566 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5567 struct got_fileindex *fileindex = NULL;
5568 char *fileindex_path = NULL;
5569 struct got_pathlist_head commitable_paths;
5570 struct collect_commitables_arg cc_arg;
5571 struct got_pathlist_entry *pe;
5572 struct got_reference *head_ref = NULL;
5573 struct got_object_id *head_commit_id = NULL;
5574 int have_staged_files = 0;
5576 *new_commit_id = NULL;
5578 TAILQ_INIT(&commitable_paths);
5580 err = lock_worktree(worktree, LOCK_EX);
5584 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5588 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5592 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5596 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5597 &have_staged_files);
5598 if (err && err->code != GOT_ERR_CANCELLED)
5600 if (have_staged_files) {
5601 err = check_non_staged_files(fileindex, paths);
5606 cc_arg.commitable_paths = &commitable_paths;
5607 cc_arg.worktree = worktree;
5608 cc_arg.fileindex = fileindex;
5610 cc_arg.have_staged_files = have_staged_files;
5611 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5612 TAILQ_FOREACH(pe, paths, entry) {
5613 err = worktree_status(worktree, pe->path, fileindex, repo,
5614 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5619 if (TAILQ_EMPTY(&commitable_paths)) {
5620 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5624 TAILQ_FOREACH(pe, paths, entry) {
5625 err = check_path_is_commitable(pe->path, &commitable_paths);
5630 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5631 struct got_commitable *ct = pe->data;
5632 const char *ct_path = ct->in_repo_path;
5634 while (ct_path[0] == '/')
5636 err = check_out_of_date(ct_path, ct->status,
5637 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5638 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5644 err = commit_worktree(new_commit_id, &commitable_paths,
5645 head_commit_id, worktree, author, committer,
5646 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5650 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5651 fileindex, have_staged_files);
5652 sync_err = sync_fileindex(fileindex, fileindex_path);
5653 if (sync_err && err == NULL)
5657 got_fileindex_free(fileindex);
5658 free(fileindex_path);
5659 unlockerr = lock_worktree(worktree, LOCK_SH);
5660 if (unlockerr && err == NULL)
5662 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5663 struct got_commitable *ct = pe->data;
5664 free_commitable(ct);
5666 got_pathlist_free(&commitable_paths);
5671 got_commitable_get_path(struct got_commitable *ct)
5677 got_commitable_get_status(struct got_commitable *ct)
5682 struct check_rebase_ok_arg {
5683 struct got_worktree *worktree;
5684 struct got_repository *repo;
5687 static const struct got_error *
5688 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5690 const struct got_error *err = NULL;
5691 struct check_rebase_ok_arg *a = arg;
5692 unsigned char status;
5696 /* Reject rebase of a work tree with mixed base commits. */
5697 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5698 SHA1_DIGEST_LENGTH))
5699 return got_error(GOT_ERR_MIXED_COMMITS);
5701 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5703 return got_error_from_errno("asprintf");
5705 /* Reject rebase of a work tree with modified or staged files. */
5706 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5711 if (status != GOT_STATUS_NO_CHANGE)
5712 return got_error(GOT_ERR_MODIFIED);
5713 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5714 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5719 const struct got_error *
5720 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5721 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5722 struct got_worktree *worktree, struct got_reference *branch,
5723 struct got_repository *repo)
5725 const struct got_error *err = NULL;
5726 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5727 char *branch_ref_name = NULL;
5728 char *fileindex_path = NULL;
5729 struct check_rebase_ok_arg ok_arg;
5730 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5731 struct got_object_id *wt_branch_tip = NULL;
5733 *new_base_branch_ref = NULL;
5737 err = lock_worktree(worktree, LOCK_EX);
5741 err = open_fileindex(fileindex, &fileindex_path, worktree);
5745 ok_arg.worktree = worktree;
5747 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5752 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5756 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5760 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5764 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5769 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5772 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5773 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5777 err = got_ref_alloc_symref(new_base_branch_ref,
5778 new_base_branch_ref_name, wt_branch);
5781 err = got_ref_write(*new_base_branch_ref, repo);
5785 /* TODO Lock original branch's ref while rebasing? */
5787 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5791 err = got_ref_write(branch_ref, repo);
5795 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5796 worktree->base_commit_id);
5799 err = got_ref_write(*tmp_branch, repo);
5803 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5807 free(fileindex_path);
5808 free(tmp_branch_name);
5809 free(new_base_branch_ref_name);
5810 free(branch_ref_name);
5812 got_ref_close(branch_ref);
5814 got_ref_close(wt_branch);
5815 free(wt_branch_tip);
5817 if (*new_base_branch_ref) {
5818 got_ref_close(*new_base_branch_ref);
5819 *new_base_branch_ref = NULL;
5822 got_ref_close(*tmp_branch);
5826 got_fileindex_free(*fileindex);
5829 lock_worktree(worktree, LOCK_SH);
5834 const struct got_error *
5835 got_worktree_rebase_continue(struct got_object_id **commit_id,
5836 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5837 struct got_reference **branch, struct got_fileindex **fileindex,
5838 struct got_worktree *worktree, struct got_repository *repo)
5840 const struct got_error *err;
5841 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5842 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5843 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5844 char *fileindex_path = NULL;
5845 int have_staged_files = 0;
5848 *new_base_branch = NULL;
5853 err = lock_worktree(worktree, LOCK_EX);
5857 err = open_fileindex(fileindex, &fileindex_path, worktree);
5861 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5862 &have_staged_files);
5863 if (err && err->code != GOT_ERR_CANCELLED)
5865 if (have_staged_files) {
5866 err = got_error(GOT_ERR_STAGED_PATHS);
5870 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5874 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5878 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5882 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5886 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5890 err = got_ref_open(branch, repo,
5891 got_ref_get_symref_target(branch_ref), 0);
5895 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5899 err = got_ref_resolve(commit_id, repo, commit_ref);
5903 err = got_ref_open(new_base_branch, repo,
5904 new_base_branch_ref_name, 0);
5908 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5912 free(commit_ref_name);
5913 free(branch_ref_name);
5914 free(fileindex_path);
5916 got_ref_close(commit_ref);
5918 got_ref_close(branch_ref);
5923 got_ref_close(*tmp_branch);
5926 if (*new_base_branch) {
5927 got_ref_close(*new_base_branch);
5928 *new_base_branch = NULL;
5931 got_ref_close(*branch);
5935 got_fileindex_free(*fileindex);
5938 lock_worktree(worktree, LOCK_SH);
5943 const struct got_error *
5944 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5946 const struct got_error *err;
5947 char *tmp_branch_name = NULL;
5949 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5953 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5954 free(tmp_branch_name);
5958 static const struct got_error *
5959 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5960 char **logmsg, void *arg)
5966 static const struct got_error *
5967 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5968 const char *path, struct got_object_id *blob_id,
5969 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5970 int dirfd, const char *de_name)
5975 struct collect_merged_paths_arg {
5976 got_worktree_checkout_cb progress_cb;
5978 struct got_pathlist_head *merged_paths;
5981 static const struct got_error *
5982 collect_merged_paths(void *arg, unsigned char status, const char *path)
5984 const struct got_error *err;
5985 struct collect_merged_paths_arg *a = arg;
5987 struct got_pathlist_entry *new;
5989 err = (*a->progress_cb)(a->progress_arg, status, path);
5993 if (status != GOT_STATUS_MERGE &&
5994 status != GOT_STATUS_ADD &&
5995 status != GOT_STATUS_DELETE &&
5996 status != GOT_STATUS_CONFLICT)
6001 return got_error_from_errno("strdup");
6003 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6004 if (err || new == NULL)
6010 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6012 struct got_pathlist_entry *pe;
6014 TAILQ_FOREACH(pe, merged_paths, entry)
6015 free((char *)pe->path);
6017 got_pathlist_free(merged_paths);
6020 static const struct got_error *
6021 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6022 int is_rebase, struct got_repository *repo)
6024 const struct got_error *err;
6025 struct got_reference *commit_ref = NULL;
6027 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6029 if (err->code != GOT_ERR_NOT_REF)
6031 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6034 err = got_ref_write(commit_ref, repo);
6037 } else if (is_rebase) {
6038 struct got_object_id *stored_id;
6041 err = got_ref_resolve(&stored_id, repo, commit_ref);
6044 cmp = got_object_id_cmp(commit_id, stored_id);
6047 err = got_error(GOT_ERR_REBASE_COMMITID);
6053 got_ref_close(commit_ref);
6057 static const struct got_error *
6058 rebase_merge_files(struct got_pathlist_head *merged_paths,
6059 const char *commit_ref_name, struct got_worktree *worktree,
6060 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6061 struct got_object_id *commit_id, struct got_repository *repo,
6062 got_worktree_checkout_cb progress_cb, void *progress_arg,
6063 got_cancel_cb cancel_cb, void *cancel_arg)
6065 const struct got_error *err;
6066 struct got_reference *commit_ref = NULL;
6067 struct collect_merged_paths_arg cmp_arg;
6068 char *fileindex_path;
6070 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6072 err = get_fileindex_path(&fileindex_path, worktree);
6076 cmp_arg.progress_cb = progress_cb;
6077 cmp_arg.progress_arg = progress_arg;
6078 cmp_arg.merged_paths = merged_paths;
6079 err = merge_files(worktree, fileindex, fileindex_path,
6080 parent_commit_id, commit_id, repo, collect_merged_paths,
6081 &cmp_arg, cancel_cb, cancel_arg);
6083 got_ref_close(commit_ref);
6087 const struct got_error *
6088 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6089 struct got_worktree *worktree, struct got_fileindex *fileindex,
6090 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6091 struct got_repository *repo,
6092 got_worktree_checkout_cb progress_cb, void *progress_arg,
6093 got_cancel_cb cancel_cb, void *cancel_arg)
6095 const struct got_error *err;
6096 char *commit_ref_name;
6098 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6102 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6106 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6107 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6108 progress_arg, cancel_cb, cancel_arg);
6110 free(commit_ref_name);
6114 const struct got_error *
6115 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6116 struct got_worktree *worktree, struct got_fileindex *fileindex,
6117 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6118 struct got_repository *repo,
6119 got_worktree_checkout_cb progress_cb, void *progress_arg,
6120 got_cancel_cb cancel_cb, void *cancel_arg)
6122 const struct got_error *err;
6123 char *commit_ref_name;
6125 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6129 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6133 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6134 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6135 progress_arg, cancel_cb, cancel_arg);
6137 free(commit_ref_name);
6141 static const struct got_error *
6142 rebase_commit(struct got_object_id **new_commit_id,
6143 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6144 struct got_worktree *worktree, struct got_fileindex *fileindex,
6145 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6146 const char *new_logmsg, struct got_repository *repo)
6148 const struct got_error *err, *sync_err;
6149 struct got_pathlist_head commitable_paths;
6150 struct collect_commitables_arg cc_arg;
6151 char *fileindex_path = NULL;
6152 struct got_reference *head_ref = NULL;
6153 struct got_object_id *head_commit_id = NULL;
6154 char *logmsg = NULL;
6156 TAILQ_INIT(&commitable_paths);
6157 *new_commit_id = NULL;
6159 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6161 err = get_fileindex_path(&fileindex_path, worktree);
6165 cc_arg.commitable_paths = &commitable_paths;
6166 cc_arg.worktree = worktree;
6168 cc_arg.have_staged_files = 0;
6170 * If possible get the status of individual files directly to
6171 * avoid crawling the entire work tree once per rebased commit.
6172 * TODO: Ideally, merged_paths would contain a list of commitables
6173 * we could use so we could skip worktree_status() entirely.
6176 struct got_pathlist_entry *pe;
6177 TAILQ_FOREACH(pe, merged_paths, entry) {
6178 err = worktree_status(worktree, pe->path, fileindex,
6179 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6185 err = worktree_status(worktree, "", fileindex, repo,
6186 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6191 if (TAILQ_EMPTY(&commitable_paths)) {
6192 /* No-op change; commit will be elided. */
6193 err = got_ref_delete(commit_ref, repo);
6196 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6200 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6204 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6209 logmsg = strdup(new_logmsg);
6210 if (logmsg == NULL) {
6211 err = got_error_from_errno("strdup");
6215 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6220 /* NB: commit_worktree will call free(logmsg) */
6221 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6222 worktree, got_object_commit_get_author(orig_commit),
6223 got_object_commit_get_committer(orig_commit),
6224 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6228 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6232 err = got_ref_delete(commit_ref, repo);
6236 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
6238 sync_err = sync_fileindex(fileindex, fileindex_path);
6239 if (sync_err && err == NULL)
6242 free(fileindex_path);
6243 free(head_commit_id);
6245 got_ref_close(head_ref);
6247 free(*new_commit_id);
6248 *new_commit_id = NULL;
6253 const struct got_error *
6254 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6255 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6256 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6257 struct got_commit_object *orig_commit,
6258 struct got_object_id *orig_commit_id, struct got_repository *repo)
6260 const struct got_error *err;
6261 char *commit_ref_name;
6262 struct got_reference *commit_ref = NULL;
6263 struct got_object_id *commit_id = NULL;
6265 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6269 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6272 err = got_ref_resolve(&commit_id, repo, commit_ref);
6275 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6276 err = got_error(GOT_ERR_REBASE_COMMITID);
6280 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6281 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6284 got_ref_close(commit_ref);
6285 free(commit_ref_name);
6290 const struct got_error *
6291 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6292 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6293 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6294 struct got_commit_object *orig_commit,
6295 struct got_object_id *orig_commit_id, const char *new_logmsg,
6296 struct got_repository *repo)
6298 const struct got_error *err;
6299 char *commit_ref_name;
6300 struct got_reference *commit_ref = NULL;
6302 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6306 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6310 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6311 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6314 got_ref_close(commit_ref);
6315 free(commit_ref_name);
6319 const struct got_error *
6320 got_worktree_rebase_postpone(struct got_worktree *worktree,
6321 struct got_fileindex *fileindex)
6324 got_fileindex_free(fileindex);
6325 return lock_worktree(worktree, LOCK_SH);
6328 static const struct got_error *
6329 delete_ref(const char *name, struct got_repository *repo)
6331 const struct got_error *err;
6332 struct got_reference *ref;
6334 err = got_ref_open(&ref, repo, name, 0);
6336 if (err->code == GOT_ERR_NOT_REF)
6341 err = got_ref_delete(ref, repo);
6346 static const struct got_error *
6347 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6349 const struct got_error *err;
6350 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6351 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6353 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6356 err = delete_ref(tmp_branch_name, repo);
6360 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6363 err = delete_ref(new_base_branch_ref_name, repo);
6367 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6370 err = delete_ref(branch_ref_name, repo);
6374 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6377 err = delete_ref(commit_ref_name, repo);
6382 free(tmp_branch_name);
6383 free(new_base_branch_ref_name);
6384 free(branch_ref_name);
6385 free(commit_ref_name);
6389 const struct got_error *
6390 got_worktree_rebase_complete(struct got_worktree *worktree,
6391 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6392 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6393 struct got_repository *repo)
6395 const struct got_error *err, *unlockerr;
6396 struct got_object_id *new_head_commit_id = NULL;
6398 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6402 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6406 err = got_ref_write(rebased_branch, repo);
6410 err = got_worktree_set_head_ref(worktree, rebased_branch);
6414 err = delete_rebase_refs(worktree, repo);
6417 got_fileindex_free(fileindex);
6418 free(new_head_commit_id);
6419 unlockerr = lock_worktree(worktree, LOCK_SH);
6420 if (unlockerr && err == NULL)
6425 const struct got_error *
6426 got_worktree_rebase_abort(struct got_worktree *worktree,
6427 struct got_fileindex *fileindex, struct got_repository *repo,
6428 struct got_reference *new_base_branch,
6429 got_worktree_checkout_cb progress_cb, void *progress_arg)
6431 const struct got_error *err, *unlockerr, *sync_err;
6432 struct got_reference *resolved = NULL;
6433 struct got_object_id *commit_id = NULL;
6434 char *fileindex_path = NULL;
6435 struct revert_file_args rfa;
6436 struct got_object_id *tree_id = NULL;
6438 err = lock_worktree(worktree, LOCK_EX);
6442 err = got_ref_open(&resolved, repo,
6443 got_ref_get_symref_target(new_base_branch), 0);
6447 err = got_worktree_set_head_ref(worktree, resolved);
6452 * XXX commits to the base branch could have happened while
6453 * we were busy rebasing; should we store the original commit ID
6454 * when rebase begins and read it back here?
6456 err = got_ref_resolve(&commit_id, repo, resolved);
6460 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6464 err = got_object_id_by_path(&tree_id, repo,
6465 worktree->base_commit_id, worktree->path_prefix);
6469 err = delete_rebase_refs(worktree, repo);
6473 err = get_fileindex_path(&fileindex_path, worktree);
6477 rfa.worktree = worktree;
6478 rfa.fileindex = fileindex;
6479 rfa.progress_cb = progress_cb;
6480 rfa.progress_arg = progress_arg;
6481 rfa.patch_cb = NULL;
6482 rfa.patch_arg = NULL;
6484 err = worktree_status(worktree, "", fileindex, repo,
6485 revert_file, &rfa, NULL, NULL, 0, 0);
6489 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6490 repo, progress_cb, progress_arg, NULL, NULL);
6492 sync_err = sync_fileindex(fileindex, fileindex_path);
6493 if (sync_err && err == NULL)
6496 got_ref_close(resolved);
6500 got_fileindex_free(fileindex);
6501 free(fileindex_path);
6503 unlockerr = lock_worktree(worktree, LOCK_SH);
6504 if (unlockerr && err == NULL)
6509 const struct got_error *
6510 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6511 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6512 struct got_fileindex **fileindex, struct got_worktree *worktree,
6513 struct got_repository *repo)
6515 const struct got_error *err = NULL;
6516 char *tmp_branch_name = NULL;
6517 char *branch_ref_name = NULL;
6518 char *base_commit_ref_name = NULL;
6519 char *fileindex_path = NULL;
6520 struct check_rebase_ok_arg ok_arg;
6521 struct got_reference *wt_branch = NULL;
6522 struct got_reference *base_commit_ref = NULL;
6526 *base_commit_id = NULL;
6529 err = lock_worktree(worktree, LOCK_EX);
6533 err = open_fileindex(fileindex, &fileindex_path, worktree);
6537 ok_arg.worktree = worktree;
6539 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6544 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6548 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6552 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6557 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6562 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6566 err = got_ref_write(*branch_ref, repo);
6570 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6571 worktree->base_commit_id);
6574 err = got_ref_write(base_commit_ref, repo);
6577 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6578 if (*base_commit_id == NULL) {
6579 err = got_error_from_errno("got_object_id_dup");
6583 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6584 worktree->base_commit_id);
6587 err = got_ref_write(*tmp_branch, repo);
6591 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6595 free(fileindex_path);
6596 free(tmp_branch_name);
6597 free(branch_ref_name);
6598 free(base_commit_ref_name);
6600 got_ref_close(wt_branch);
6603 got_ref_close(*branch_ref);
6607 got_ref_close(*tmp_branch);
6610 free(*base_commit_id);
6612 got_fileindex_free(*fileindex);
6615 lock_worktree(worktree, LOCK_SH);
6620 const struct got_error *
6621 got_worktree_histedit_postpone(struct got_worktree *worktree,
6622 struct got_fileindex *fileindex)
6625 got_fileindex_free(fileindex);
6626 return lock_worktree(worktree, LOCK_SH);
6629 const struct got_error *
6630 got_worktree_histedit_in_progress(int *in_progress,
6631 struct got_worktree *worktree)
6633 const struct got_error *err;
6634 char *tmp_branch_name = NULL;
6636 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6640 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6641 free(tmp_branch_name);
6645 const struct got_error *
6646 got_worktree_histedit_continue(struct got_object_id **commit_id,
6647 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6648 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6649 struct got_worktree *worktree, struct got_repository *repo)
6651 const struct got_error *err;
6652 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6653 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6654 struct got_reference *commit_ref = NULL;
6655 struct got_reference *base_commit_ref = NULL;
6656 char *fileindex_path = NULL;
6657 int have_staged_files = 0;
6661 *base_commit_id = NULL;
6664 err = lock_worktree(worktree, LOCK_EX);
6668 err = open_fileindex(fileindex, &fileindex_path, worktree);
6672 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6673 &have_staged_files);
6674 if (err && err->code != GOT_ERR_CANCELLED)
6676 if (have_staged_files) {
6677 err = got_error(GOT_ERR_STAGED_PATHS);
6681 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6685 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6689 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6693 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6698 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6702 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6705 err = got_ref_resolve(commit_id, repo, commit_ref);
6709 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6712 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6716 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6720 free(commit_ref_name);
6721 free(branch_ref_name);
6722 free(fileindex_path);
6724 got_ref_close(commit_ref);
6725 if (base_commit_ref)
6726 got_ref_close(base_commit_ref);
6730 free(*base_commit_id);
6731 *base_commit_id = NULL;
6733 got_ref_close(*tmp_branch);
6737 got_fileindex_free(*fileindex);
6740 lock_worktree(worktree, LOCK_EX);
6745 static const struct got_error *
6746 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6748 const struct got_error *err;
6749 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6750 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6752 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6755 err = delete_ref(tmp_branch_name, repo);
6759 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6763 err = delete_ref(base_commit_ref_name, repo);
6767 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6770 err = delete_ref(branch_ref_name, repo);
6774 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6777 err = delete_ref(commit_ref_name, repo);
6781 free(tmp_branch_name);
6782 free(base_commit_ref_name);
6783 free(branch_ref_name);
6784 free(commit_ref_name);
6788 const struct got_error *
6789 got_worktree_histedit_abort(struct got_worktree *worktree,
6790 struct got_fileindex *fileindex, struct got_repository *repo,
6791 struct got_reference *branch, struct got_object_id *base_commit_id,
6792 got_worktree_checkout_cb progress_cb, void *progress_arg)
6794 const struct got_error *err, *unlockerr, *sync_err;
6795 struct got_reference *resolved = NULL;
6796 char *fileindex_path = NULL;
6797 struct got_object_id *tree_id = NULL;
6798 struct revert_file_args rfa;
6800 err = lock_worktree(worktree, LOCK_EX);
6804 err = got_ref_open(&resolved, repo,
6805 got_ref_get_symref_target(branch), 0);
6809 err = got_worktree_set_head_ref(worktree, resolved);
6813 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6817 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6818 worktree->path_prefix);
6822 err = delete_histedit_refs(worktree, repo);
6826 err = get_fileindex_path(&fileindex_path, worktree);
6830 rfa.worktree = worktree;
6831 rfa.fileindex = fileindex;
6832 rfa.progress_cb = progress_cb;
6833 rfa.progress_arg = progress_arg;
6834 rfa.patch_cb = NULL;
6835 rfa.patch_arg = NULL;
6837 err = worktree_status(worktree, "", fileindex, repo,
6838 revert_file, &rfa, NULL, NULL, 0, 0);
6842 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6843 repo, progress_cb, progress_arg, NULL, NULL);
6845 sync_err = sync_fileindex(fileindex, fileindex_path);
6846 if (sync_err && err == NULL)
6849 got_ref_close(resolved);
6851 free(fileindex_path);
6853 unlockerr = lock_worktree(worktree, LOCK_SH);
6854 if (unlockerr && err == NULL)
6859 const struct got_error *
6860 got_worktree_histedit_complete(struct got_worktree *worktree,
6861 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6862 struct got_reference *edited_branch, struct got_repository *repo)
6864 const struct got_error *err, *unlockerr;
6865 struct got_object_id *new_head_commit_id = NULL;
6866 struct got_reference *resolved = NULL;
6868 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6872 err = got_ref_open(&resolved, repo,
6873 got_ref_get_symref_target(edited_branch), 0);
6877 err = got_ref_change_ref(resolved, new_head_commit_id);
6881 err = got_ref_write(resolved, repo);
6885 err = got_worktree_set_head_ref(worktree, resolved);
6889 err = delete_histedit_refs(worktree, repo);
6892 got_fileindex_free(fileindex);
6893 free(new_head_commit_id);
6894 unlockerr = lock_worktree(worktree, LOCK_SH);
6895 if (unlockerr && err == NULL)
6900 const struct got_error *
6901 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6902 struct got_object_id *commit_id, struct got_repository *repo)
6904 const struct got_error *err;
6905 char *commit_ref_name;
6907 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6911 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6915 err = delete_ref(commit_ref_name, repo);
6917 free(commit_ref_name);
6921 const struct got_error *
6922 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6923 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6924 struct got_worktree *worktree, const char *refname,
6925 struct got_repository *repo)
6927 const struct got_error *err = NULL;
6928 char *fileindex_path = NULL;
6929 struct check_rebase_ok_arg ok_arg;
6933 *base_branch_ref = NULL;
6935 err = lock_worktree(worktree, LOCK_EX);
6939 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6940 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6941 "cannot integrate a branch into itself; "
6942 "update -b or different branch name required");
6946 err = open_fileindex(fileindex, &fileindex_path, worktree);
6950 /* Preconditions are the same as for rebase. */
6951 ok_arg.worktree = worktree;
6953 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6958 err = got_ref_open(branch_ref, repo, refname, 1);
6962 err = got_ref_open(base_branch_ref, repo,
6963 got_worktree_get_head_ref_name(worktree), 1);
6967 got_ref_close(*branch_ref);
6970 if (*base_branch_ref) {
6971 got_ref_close(*base_branch_ref);
6972 *base_branch_ref = NULL;
6975 got_fileindex_free(*fileindex);
6978 lock_worktree(worktree, LOCK_SH);
6983 const struct got_error *
6984 got_worktree_integrate_continue(struct got_worktree *worktree,
6985 struct got_fileindex *fileindex, struct got_repository *repo,
6986 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6987 got_worktree_checkout_cb progress_cb, void *progress_arg,
6988 got_cancel_cb cancel_cb, void *cancel_arg)
6990 const struct got_error *err = NULL, *sync_err, *unlockerr;
6991 char *fileindex_path = NULL;
6992 struct got_object_id *tree_id = NULL, *commit_id = NULL;
6994 err = get_fileindex_path(&fileindex_path, worktree);
6998 err = got_ref_resolve(&commit_id, repo, branch_ref);
7002 err = got_object_id_by_path(&tree_id, repo, commit_id,
7003 worktree->path_prefix);
7007 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7011 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7012 progress_cb, progress_arg, cancel_cb, cancel_arg);
7016 err = got_ref_change_ref(base_branch_ref, commit_id);
7020 err = got_ref_write(base_branch_ref, repo);
7022 sync_err = sync_fileindex(fileindex, fileindex_path);
7023 if (sync_err && err == NULL)
7027 unlockerr = got_ref_unlock(branch_ref);
7028 if (unlockerr && err == NULL)
7030 got_ref_close(branch_ref);
7032 unlockerr = got_ref_unlock(base_branch_ref);
7033 if (unlockerr && err == NULL)
7035 got_ref_close(base_branch_ref);
7037 got_fileindex_free(fileindex);
7038 free(fileindex_path);
7041 unlockerr = lock_worktree(worktree, LOCK_SH);
7042 if (unlockerr && err == NULL)
7047 const struct got_error *
7048 got_worktree_integrate_abort(struct got_worktree *worktree,
7049 struct got_fileindex *fileindex, struct got_repository *repo,
7050 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7052 const struct got_error *err = NULL, *unlockerr = NULL;
7054 got_fileindex_free(fileindex);
7056 err = lock_worktree(worktree, LOCK_SH);
7058 unlockerr = got_ref_unlock(branch_ref);
7059 if (unlockerr && err == NULL)
7061 got_ref_close(branch_ref);
7063 unlockerr = got_ref_unlock(base_branch_ref);
7064 if (unlockerr && err == NULL)
7066 got_ref_close(base_branch_ref);
7071 struct check_stage_ok_arg {
7072 struct got_object_id *head_commit_id;
7073 struct got_worktree *worktree;
7074 struct got_fileindex *fileindex;
7075 struct got_repository *repo;
7079 const struct got_error *
7080 check_stage_ok(void *arg, unsigned char status,
7081 unsigned char staged_status, const char *relpath,
7082 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7083 struct got_object_id *commit_id, int dirfd, const char *de_name)
7085 struct check_stage_ok_arg *a = arg;
7086 const struct got_error *err = NULL;
7087 struct got_fileindex_entry *ie;
7088 struct got_object_id base_commit_id;
7089 struct got_object_id *base_commit_idp = NULL;
7090 char *in_repo_path = NULL, *p;
7092 if (status == GOT_STATUS_UNVERSIONED ||
7093 status == GOT_STATUS_NO_CHANGE)
7095 if (status == GOT_STATUS_NONEXISTENT)
7096 return got_error_set_errno(ENOENT, relpath);
7098 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7100 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7102 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7103 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7105 return got_error_from_errno("asprintf");
7107 if (got_fileindex_entry_has_commit(ie)) {
7108 memcpy(base_commit_id.sha1, ie->commit_sha1,
7109 SHA1_DIGEST_LENGTH);
7110 base_commit_idp = &base_commit_id;
7113 if (status == GOT_STATUS_CONFLICT) {
7114 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7116 } else if (status != GOT_STATUS_ADD &&
7117 status != GOT_STATUS_MODIFY &&
7118 status != GOT_STATUS_DELETE) {
7119 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7123 a->have_changes = 1;
7128 err = check_out_of_date(p, status, staged_status,
7129 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7130 GOT_ERR_STAGE_OUT_OF_DATE);
7136 struct stage_path_arg {
7137 struct got_worktree *worktree;
7138 struct got_fileindex *fileindex;
7139 struct got_repository *repo;
7140 got_worktree_status_cb status_cb;
7142 got_worktree_patch_cb patch_cb;
7144 int staged_something;
7145 int allow_bad_symlinks;
7148 static const struct got_error *
7149 stage_path(void *arg, unsigned char status,
7150 unsigned char staged_status, const char *relpath,
7151 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7152 struct got_object_id *commit_id, int dirfd, const char *de_name)
7154 struct stage_path_arg *a = arg;
7155 const struct got_error *err = NULL;
7156 struct got_fileindex_entry *ie;
7157 char *ondisk_path = NULL, *path_content = NULL;
7159 struct got_object_id *new_staged_blob_id = NULL;
7162 if (status == GOT_STATUS_UNVERSIONED)
7165 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7167 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7169 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7171 return got_error_from_errno("asprintf");
7174 case GOT_STATUS_ADD:
7175 case GOT_STATUS_MODIFY:
7176 /* XXX could sb.st_mode be passed in by our caller? */
7177 if (lstat(ondisk_path, &sb) == -1) {
7178 err = got_error_from_errno2("lstat", ondisk_path);
7182 if (status == GOT_STATUS_ADD) {
7183 int choice = GOT_PATCH_CHOICE_NONE;
7184 err = (*a->patch_cb)(&choice, a->patch_arg,
7185 status, ie->path, NULL, 1, 1);
7188 if (choice != GOT_PATCH_CHOICE_YES)
7191 err = create_patched_content(&path_content, 0,
7192 staged_blob_id ? staged_blob_id : blob_id,
7193 ondisk_path, dirfd, de_name, ie->path,
7194 a->repo, a->patch_cb, a->patch_arg);
7195 if (err || path_content == NULL)
7199 err = got_object_blob_create(&new_staged_blob_id,
7200 path_content ? path_content : ondisk_path, a->repo);
7203 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7204 SHA1_DIGEST_LENGTH);
7205 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7206 stage = GOT_FILEIDX_STAGE_ADD;
7208 stage = GOT_FILEIDX_STAGE_MODIFY;
7209 got_fileindex_entry_stage_set(ie, stage);
7210 if (S_ISLNK(sb.st_mode)) {
7211 int is_bad_symlink = 0;
7212 if (!a->allow_bad_symlinks) {
7213 char target_path[PATH_MAX];
7215 target_len = readlink(ondisk_path, target_path,
7216 sizeof(target_path));
7217 if (target_len == -1) {
7218 err = got_error_from_errno2("readlink",
7222 err = is_bad_symlink_target(&is_bad_symlink,
7223 target_path, target_len, ondisk_path,
7224 a->worktree->root_path);
7227 if (is_bad_symlink) {
7228 err = got_error_path(ondisk_path,
7229 GOT_ERR_BAD_SYMLINK);
7234 got_fileindex_entry_staged_filetype_set(ie,
7235 GOT_FILEIDX_MODE_BAD_SYMLINK);
7237 got_fileindex_entry_staged_filetype_set(ie,
7238 GOT_FILEIDX_MODE_SYMLINK);
7240 got_fileindex_entry_staged_filetype_set(ie,
7241 GOT_FILEIDX_MODE_REGULAR_FILE);
7243 a->staged_something = 1;
7244 if (a->status_cb == NULL)
7246 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7247 get_staged_status(ie), relpath, blob_id,
7248 new_staged_blob_id, NULL, dirfd, de_name);
7250 case GOT_STATUS_DELETE:
7251 if (staged_status == GOT_STATUS_DELETE)
7254 int choice = GOT_PATCH_CHOICE_NONE;
7255 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7256 ie->path, NULL, 1, 1);
7259 if (choice == GOT_PATCH_CHOICE_NO)
7261 if (choice != GOT_PATCH_CHOICE_YES) {
7262 err = got_error(GOT_ERR_PATCH_CHOICE);
7266 stage = GOT_FILEIDX_STAGE_DELETE;
7267 got_fileindex_entry_stage_set(ie, stage);
7268 a->staged_something = 1;
7269 if (a->status_cb == NULL)
7271 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7272 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7275 case GOT_STATUS_NO_CHANGE:
7277 case GOT_STATUS_CONFLICT:
7278 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7280 case GOT_STATUS_NONEXISTENT:
7281 err = got_error_set_errno(ENOENT, relpath);
7284 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7288 if (path_content && unlink(path_content) == -1 && err == NULL)
7289 err = got_error_from_errno2("unlink", path_content);
7292 free(new_staged_blob_id);
7296 const struct got_error *
7297 got_worktree_stage(struct got_worktree *worktree,
7298 struct got_pathlist_head *paths,
7299 got_worktree_status_cb status_cb, void *status_arg,
7300 got_worktree_patch_cb patch_cb, void *patch_arg,
7301 int allow_bad_symlinks, struct got_repository *repo)
7303 const struct got_error *err = NULL, *sync_err, *unlockerr;
7304 struct got_pathlist_entry *pe;
7305 struct got_fileindex *fileindex = NULL;
7306 char *fileindex_path = NULL;
7307 struct got_reference *head_ref = NULL;
7308 struct got_object_id *head_commit_id = NULL;
7309 struct check_stage_ok_arg oka;
7310 struct stage_path_arg spa;
7312 err = lock_worktree(worktree, LOCK_EX);
7316 err = got_ref_open(&head_ref, repo,
7317 got_worktree_get_head_ref_name(worktree), 0);
7320 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7323 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7327 /* Check pre-conditions before staging anything. */
7328 oka.head_commit_id = head_commit_id;
7329 oka.worktree = worktree;
7330 oka.fileindex = fileindex;
7332 oka.have_changes = 0;
7333 TAILQ_FOREACH(pe, paths, entry) {
7334 err = worktree_status(worktree, pe->path, fileindex, repo,
7335 check_stage_ok, &oka, NULL, NULL, 0, 0);
7339 if (!oka.have_changes) {
7340 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7344 spa.worktree = worktree;
7345 spa.fileindex = fileindex;
7347 spa.patch_cb = patch_cb;
7348 spa.patch_arg = patch_arg;
7349 spa.status_cb = status_cb;
7350 spa.status_arg = status_arg;
7351 spa.staged_something = 0;
7352 spa.allow_bad_symlinks = allow_bad_symlinks;
7353 TAILQ_FOREACH(pe, paths, entry) {
7354 err = worktree_status(worktree, pe->path, fileindex, repo,
7355 stage_path, &spa, NULL, NULL, 0, 0);
7359 if (!spa.staged_something) {
7360 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7364 sync_err = sync_fileindex(fileindex, fileindex_path);
7365 if (sync_err && err == NULL)
7369 got_ref_close(head_ref);
7370 free(head_commit_id);
7371 free(fileindex_path);
7373 got_fileindex_free(fileindex);
7374 unlockerr = lock_worktree(worktree, LOCK_SH);
7375 if (unlockerr && err == NULL)
7380 struct unstage_path_arg {
7381 struct got_worktree *worktree;
7382 struct got_fileindex *fileindex;
7383 struct got_repository *repo;
7384 got_worktree_checkout_cb progress_cb;
7386 got_worktree_patch_cb patch_cb;
7390 static const struct got_error *
7391 create_unstaged_content(char **path_unstaged_content,
7392 char **path_new_staged_content, struct got_object_id *blob_id,
7393 struct got_object_id *staged_blob_id, const char *relpath,
7394 struct got_repository *repo,
7395 got_worktree_patch_cb patch_cb, void *patch_arg)
7397 const struct got_error *err;
7398 struct got_blob_object *blob = NULL, *staged_blob = NULL;
7399 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7400 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7401 struct stat sb1, sb2;
7402 struct got_diff_changes *changes = NULL;
7403 struct got_diff_state *ds = NULL;
7404 struct got_diff_args *args = NULL;
7405 struct got_diff_change *change;
7406 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
7407 int have_content = 0, have_rejected_content = 0;
7409 *path_unstaged_content = NULL;
7410 *path_new_staged_content = NULL;
7412 err = got_object_id_str(&label1, blob_id);
7415 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7419 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7423 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7427 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7431 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7435 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7439 if (stat(path1, &sb1) == -1) {
7440 err = got_error_from_errno2("stat", path1);
7444 if (stat(path2, &sb2) == -1) {
7445 err = got_error_from_errno2("stat", path2);
7449 err = got_diff_files(&changes, &ds, &args, &diff_flags,
7450 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
7454 err = got_opentemp_named(path_unstaged_content, &outfile,
7455 "got-unstaged-content");
7458 err = got_opentemp_named(path_new_staged_content, &rejectfile,
7459 "got-new-staged-content");
7463 if (fseek(f1, 0L, SEEK_SET) == -1) {
7464 err = got_ferror(f1, GOT_ERR_IO);
7467 if (fseek(f2, 0L, SEEK_SET) == -1) {
7468 err = got_ferror(f2, GOT_ERR_IO);
7471 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
7473 err = apply_or_reject_change(&choice, change, ++n,
7474 changes->nchanges, ds, args, diff_flags, relpath,
7475 f1, f2, &line_cur1, &line_cur2,
7476 outfile, rejectfile, patch_cb, patch_arg);
7479 if (choice == GOT_PATCH_CHOICE_YES)
7482 have_rejected_content = 1;
7483 if (choice == GOT_PATCH_CHOICE_QUIT)
7486 if (have_content || have_rejected_content)
7487 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7488 outfile, rejectfile);
7492 got_object_blob_close(blob);
7494 got_object_blob_close(staged_blob);
7495 if (f1 && fclose(f1) == EOF && err == NULL)
7496 err = got_error_from_errno2("fclose", path1);
7497 if (f2 && fclose(f2) == EOF && err == NULL)
7498 err = got_error_from_errno2("fclose", path2);
7499 if (outfile && fclose(outfile) == EOF && err == NULL)
7500 err = got_error_from_errno2("fclose", *path_unstaged_content);
7501 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7502 err = got_error_from_errno2("fclose", *path_new_staged_content);
7503 if (path1 && unlink(path1) == -1 && err == NULL)
7504 err = got_error_from_errno2("unlink", path1);
7505 if (path2 && unlink(path2) == -1 && err == NULL)
7506 err = got_error_from_errno2("unlink", path2);
7507 if (err || !have_content) {
7508 if (*path_unstaged_content &&
7509 unlink(*path_unstaged_content) == -1 && err == NULL)
7510 err = got_error_from_errno2("unlink",
7511 *path_unstaged_content);
7512 free(*path_unstaged_content);
7513 *path_unstaged_content = NULL;
7515 if (err || !have_content || !have_rejected_content) {
7516 if (*path_new_staged_content &&
7517 unlink(*path_new_staged_content) == -1 && err == NULL)
7518 err = got_error_from_errno2("unlink",
7519 *path_new_staged_content);
7520 free(*path_new_staged_content);
7521 *path_new_staged_content = NULL;
7525 got_diff_state_free(ds);
7529 got_diff_free_changes(changes);
7535 static const struct got_error *
7536 unstage_hunks(struct got_object_id *staged_blob_id,
7537 struct got_blob_object *blob_base,
7538 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
7539 const char *ondisk_path, const char *label_orig,
7540 struct got_worktree *worktree, struct got_repository *repo,
7541 got_worktree_patch_cb patch_cb, void *patch_arg,
7542 got_worktree_checkout_cb progress_cb, void *progress_arg)
7544 const struct got_error *err = NULL;
7545 char *path_unstaged_content = NULL;
7546 char *path_new_staged_content = NULL;
7547 struct got_object_id *new_staged_blob_id = NULL;
7551 err = create_unstaged_content(&path_unstaged_content,
7552 &path_new_staged_content, blob_id, staged_blob_id,
7553 ie->path, repo, patch_cb, patch_arg);
7557 if (path_unstaged_content == NULL)
7560 if (path_new_staged_content) {
7561 err = got_object_blob_create(&new_staged_blob_id,
7562 path_new_staged_content, repo);
7567 f = fopen(path_unstaged_content, "r");
7569 err = got_error_from_errno2("fopen",
7570 path_unstaged_content);
7573 if (fstat(fileno(f), &sb) == -1) {
7574 err = got_error_from_errno2("fstat", path_unstaged_content);
7577 if (got_fileindex_entry_staged_filetype_get(ie) ==
7578 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
7579 char link_target[PATH_MAX];
7581 r = fread(link_target, 1, sizeof(link_target), f);
7582 if (r == 0 && ferror(f)) {
7583 err = got_error_from_errno("fread");
7586 if (r >= sizeof(link_target)) { /* should not happen */
7587 err = got_error(GOT_ERR_NO_SPACE);
7590 link_target[r] = '\0';
7591 err = merge_symlink(worktree, blob_base,
7592 ondisk_path, ie->path, label_orig, link_target,
7593 worktree->base_commit_id, repo, progress_cb,
7596 int local_changes_subsumed;
7597 err = merge_file(&local_changes_subsumed, worktree,
7598 blob_base, ondisk_path, ie->path,
7599 got_fileindex_perms_to_st(ie),
7600 path_unstaged_content, label_orig, "unstaged",
7601 repo, progress_cb, progress_arg);
7606 if (new_staged_blob_id) {
7607 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7608 SHA1_DIGEST_LENGTH);
7610 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7611 got_fileindex_entry_staged_filetype_set(ie, 0);
7614 free(new_staged_blob_id);
7615 if (path_unstaged_content &&
7616 unlink(path_unstaged_content) == -1 && err == NULL)
7617 err = got_error_from_errno2("unlink", path_unstaged_content);
7618 if (path_new_staged_content &&
7619 unlink(path_new_staged_content) == -1 && err == NULL)
7620 err = got_error_from_errno2("unlink", path_new_staged_content);
7621 if (f && fclose(f) != 0 && err == NULL)
7622 err = got_error_from_errno2("fclose", path_unstaged_content);
7623 free(path_unstaged_content);
7624 free(path_new_staged_content);
7628 static const struct got_error *
7629 unstage_path(void *arg, unsigned char status,
7630 unsigned char staged_status, const char *relpath,
7631 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7632 struct got_object_id *commit_id, int dirfd, const char *de_name)
7634 const struct got_error *err = NULL;
7635 struct unstage_path_arg *a = arg;
7636 struct got_fileindex_entry *ie;
7637 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7638 char *ondisk_path = NULL;
7639 char *id_str = NULL, *label_orig = NULL;
7640 int local_changes_subsumed;
7643 if (staged_status != GOT_STATUS_ADD &&
7644 staged_status != GOT_STATUS_MODIFY &&
7645 staged_status != GOT_STATUS_DELETE)
7648 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7650 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7652 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7654 return got_error_from_errno("asprintf");
7656 err = got_object_id_str(&id_str,
7657 commit_id ? commit_id : a->worktree->base_commit_id);
7660 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7662 err = got_error_from_errno("asprintf");
7666 switch (staged_status) {
7667 case GOT_STATUS_MODIFY:
7668 err = got_object_open_as_blob(&blob_base, a->repo,
7673 case GOT_STATUS_ADD:
7675 if (staged_status == GOT_STATUS_ADD) {
7676 int choice = GOT_PATCH_CHOICE_NONE;
7677 err = (*a->patch_cb)(&choice, a->patch_arg,
7678 staged_status, ie->path, NULL, 1, 1);
7681 if (choice != GOT_PATCH_CHOICE_YES)
7684 err = unstage_hunks(staged_blob_id,
7685 blob_base, blob_id, ie, ondisk_path,
7686 label_orig, a->worktree, a->repo,
7687 a->patch_cb, a->patch_arg,
7688 a->progress_cb, a->progress_arg);
7689 break; /* Done with this file. */
7692 err = got_object_open_as_blob(&blob_staged, a->repo,
7693 staged_blob_id, 8192);
7696 switch (got_fileindex_entry_staged_filetype_get(ie)) {
7697 case GOT_FILEIDX_MODE_BAD_SYMLINK:
7698 case GOT_FILEIDX_MODE_REGULAR_FILE:
7699 err = merge_blob(&local_changes_subsumed, a->worktree,
7700 blob_base, ondisk_path, relpath,
7701 got_fileindex_perms_to_st(ie), label_orig,
7702 blob_staged, commit_id ? commit_id :
7703 a->worktree->base_commit_id, a->repo,
7704 a->progress_cb, a->progress_arg);
7706 case GOT_FILEIDX_MODE_SYMLINK:
7707 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7708 char *staged_target;
7709 err = got_object_blob_read_to_str(
7710 &staged_target, blob_staged);
7713 err = merge_symlink(a->worktree, blob_base,
7714 ondisk_path, relpath, label_orig,
7715 staged_target, commit_id ? commit_id :
7716 a->worktree->base_commit_id,
7717 a->repo, a->progress_cb, a->progress_arg);
7718 free(staged_target);
7720 err = merge_blob(&local_changes_subsumed,
7721 a->worktree, blob_base, ondisk_path,
7722 relpath, got_fileindex_perms_to_st(ie),
7723 label_orig, blob_staged,
7724 commit_id ? commit_id :
7725 a->worktree->base_commit_id, a->repo,
7726 a->progress_cb, a->progress_arg);
7730 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
7734 got_fileindex_entry_stage_set(ie,
7735 GOT_FILEIDX_STAGE_NONE);
7736 got_fileindex_entry_staged_filetype_set(ie, 0);
7739 case GOT_STATUS_DELETE:
7741 int choice = GOT_PATCH_CHOICE_NONE;
7742 err = (*a->patch_cb)(&choice, a->patch_arg,
7743 staged_status, ie->path, NULL, 1, 1);
7746 if (choice == GOT_PATCH_CHOICE_NO)
7748 if (choice != GOT_PATCH_CHOICE_YES) {
7749 err = got_error(GOT_ERR_PATCH_CHOICE);
7753 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7754 got_fileindex_entry_staged_filetype_set(ie, 0);
7755 err = get_file_status(&status, &sb, ie, ondisk_path,
7756 dirfd, de_name, a->repo);
7759 err = (*a->progress_cb)(a->progress_arg, status, relpath);
7765 got_object_blob_close(blob_base);
7767 got_object_blob_close(blob_staged);
7773 const struct got_error *
7774 got_worktree_unstage(struct got_worktree *worktree,
7775 struct got_pathlist_head *paths,
7776 got_worktree_checkout_cb progress_cb, void *progress_arg,
7777 got_worktree_patch_cb patch_cb, void *patch_arg,
7778 struct got_repository *repo)
7780 const struct got_error *err = NULL, *sync_err, *unlockerr;
7781 struct got_pathlist_entry *pe;
7782 struct got_fileindex *fileindex = NULL;
7783 char *fileindex_path = NULL;
7784 struct unstage_path_arg upa;
7786 err = lock_worktree(worktree, LOCK_EX);
7790 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7794 upa.worktree = worktree;
7795 upa.fileindex = fileindex;
7797 upa.progress_cb = progress_cb;
7798 upa.progress_arg = progress_arg;
7799 upa.patch_cb = patch_cb;
7800 upa.patch_arg = patch_arg;
7801 TAILQ_FOREACH(pe, paths, entry) {
7802 err = worktree_status(worktree, pe->path, fileindex, repo,
7803 unstage_path, &upa, NULL, NULL, 0, 0);
7808 sync_err = sync_fileindex(fileindex, fileindex_path);
7809 if (sync_err && err == NULL)
7812 free(fileindex_path);
7814 got_fileindex_free(fileindex);
7815 unlockerr = lock_worktree(worktree, LOCK_SH);
7816 if (unlockerr && err == NULL)
7821 struct report_file_info_arg {
7822 struct got_worktree *worktree;
7823 got_worktree_path_info_cb info_cb;
7825 struct got_pathlist_head *paths;
7826 got_cancel_cb cancel_cb;
7830 static const struct got_error *
7831 report_file_info(void *arg, struct got_fileindex_entry *ie)
7833 struct report_file_info_arg *a = arg;
7834 struct got_pathlist_entry *pe;
7835 struct got_object_id blob_id, staged_blob_id, commit_id;
7836 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
7837 struct got_object_id *commit_idp = NULL;
7840 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
7841 return got_error(GOT_ERR_CANCELLED);
7843 TAILQ_FOREACH(pe, a->paths, entry) {
7844 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
7845 got_path_is_child(ie->path, pe->path, pe->path_len))
7848 if (pe == NULL) /* not found */
7851 if (got_fileindex_entry_has_blob(ie)) {
7852 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
7853 blob_idp = &blob_id;
7855 stage = got_fileindex_entry_stage_get(ie);
7856 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
7857 stage == GOT_FILEIDX_STAGE_ADD) {
7858 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
7859 SHA1_DIGEST_LENGTH);
7860 staged_blob_idp = &staged_blob_id;
7863 if (got_fileindex_entry_has_commit(ie)) {
7864 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
7865 commit_idp = &commit_id;
7868 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
7869 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
7872 const struct got_error *
7873 got_worktree_path_info(struct got_worktree *worktree,
7874 struct got_pathlist_head *paths,
7875 got_worktree_path_info_cb info_cb, void *info_arg,
7876 got_cancel_cb cancel_cb, void *cancel_arg)
7879 const struct got_error *err = NULL, *unlockerr;
7880 struct got_fileindex *fileindex = NULL;
7881 char *fileindex_path = NULL;
7882 struct report_file_info_arg arg;
7884 err = lock_worktree(worktree, LOCK_SH);
7888 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7892 arg.worktree = worktree;
7893 arg.info_cb = info_cb;
7894 arg.info_arg = info_arg;
7896 arg.cancel_cb = cancel_cb;
7897 arg.cancel_arg = cancel_arg;
7898 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
7901 free(fileindex_path);
7903 got_fileindex_free(fileindex);
7904 unlockerr = lock_worktree(worktree, LOCK_UN);
7905 if (unlockerr && err == NULL)