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_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1500 !S_ISREG(st_mode) && !installing_bad_symlink) {
1501 /* TODO file is obstructed; do something */
1502 err = got_error_path(ondisk_path,
1503 GOT_ERR_FILE_OBSTRUCTED);
1506 err = got_opentemp_named_fd(&tmppath, &fd,
1513 return got_error_from_errno2("open", ondisk_path);
1516 if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1517 err = got_error_from_errno2("fchmod",
1518 update ? tmppath : ondisk_path);
1523 if (restoring_missing_file)
1524 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1526 else if (reverting_versioned_file)
1527 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1530 err = (*progress_cb)(progress_arg,
1531 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1536 hdrlen = got_object_blob_get_hdrlen(blob);
1538 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1539 err = got_object_blob_read_block(&len, blob);
1543 /* Skip blob object header first time around. */
1544 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1546 err = got_error_from_errno("write");
1548 } else if (outlen != len - hdrlen) {
1549 err = got_error(GOT_ERR_IO);
1556 if (fsync(fd) != 0) {
1557 err = got_error_from_errno("fsync");
1562 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1563 err = got_error_from_errno2("unlink", ondisk_path);
1566 if (rename(tmppath, ondisk_path) != 0) {
1567 err = got_error_from_errno3("rename", tmppath,
1576 if (fd != -1 && close(fd) != 0 && err == NULL)
1577 err = got_error_from_errno("close");
1578 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1579 err = got_error_from_errno2("unlink", tmppath);
1584 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1585 static const struct got_error *
1586 get_modified_file_content_status(unsigned char *status, FILE *f)
1588 const struct got_error *err = NULL;
1589 const char *markers[3] = {
1590 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1591 GOT_DIFF_CONFLICT_MARKER_SEP,
1592 GOT_DIFF_CONFLICT_MARKER_END
1597 const char delim[3] = {'\0', '\0', '\0'};
1599 while (*status == GOT_STATUS_MODIFY) {
1600 line = fparseln(f, &len, NULL, delim, 0);
1604 err = got_ferror(f, GOT_ERR_IO);
1608 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1609 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1611 *status = GOT_STATUS_CONFLICT;
1621 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1623 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1624 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1628 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1630 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1631 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1632 ie->mtime_sec == sb->st_mtim.tv_sec &&
1633 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1634 ie->size == (sb->st_size & 0xffffffff) &&
1635 !xbit_differs(ie, sb->st_mode));
1638 static unsigned char
1639 get_staged_status(struct got_fileindex_entry *ie)
1641 switch (got_fileindex_entry_stage_get(ie)) {
1642 case GOT_FILEIDX_STAGE_ADD:
1643 return GOT_STATUS_ADD;
1644 case GOT_FILEIDX_STAGE_DELETE:
1645 return GOT_STATUS_DELETE;
1646 case GOT_FILEIDX_STAGE_MODIFY:
1647 return GOT_STATUS_MODIFY;
1649 return GOT_STATUS_NO_CHANGE;
1653 static const struct got_error *
1654 get_symlink_modification_status(unsigned char *status,
1655 struct got_fileindex_entry *ie, const char *abspath,
1656 int dirfd, const char *de_name, struct got_blob_object *blob)
1658 const struct got_error *err = NULL;
1659 char target_path[PATH_MAX];
1660 char etarget[PATH_MAX];
1662 size_t len, target_len = 0;
1663 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1664 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1666 *status = GOT_STATUS_NO_CHANGE;
1668 /* Blob object content specifies the target path of the link. */
1670 err = got_object_blob_read_block(&len, blob);
1673 if (len + target_len >= sizeof(target_path)) {
1675 * Should not happen. The blob contents were OK
1676 * when this symlink was installed.
1678 return got_error(GOT_ERR_NO_SPACE);
1681 /* Skip blob object header first time around. */
1682 memcpy(target_path + target_len, buf + hdrlen,
1684 target_len += len - hdrlen;
1688 target_path[target_len] = '\0';
1691 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1693 return got_error_from_errno2("readlinkat", abspath);
1695 elen = readlink(abspath, etarget, sizeof(etarget));
1697 return got_error_from_errno2("readlink", abspath);
1700 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1701 *status = GOT_STATUS_MODIFY;
1706 static const struct got_error *
1707 get_file_status(unsigned char *status, struct stat *sb,
1708 struct got_fileindex_entry *ie, const char *abspath,
1709 int dirfd, const char *de_name, struct got_repository *repo)
1711 const struct got_error *err = NULL;
1712 struct got_object_id id;
1717 struct got_blob_object *blob = NULL;
1719 unsigned char staged_status = get_staged_status(ie);
1721 *status = GOT_STATUS_NO_CHANGE;
1724 * Whenever the caller provides a directory descriptor and a
1725 * directory entry name for the file, use them! This prevents
1726 * race conditions if filesystem paths change beneath our feet.
1729 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1730 if (errno == ENOENT) {
1731 if (got_fileindex_entry_has_file_on_disk(ie))
1732 *status = GOT_STATUS_MISSING;
1734 *status = GOT_STATUS_DELETE;
1737 err = got_error_from_errno2("fstatat", abspath);
1741 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1742 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1743 return got_error_from_errno2("open", abspath);
1744 else if (fd == -1 && errno == ELOOP) {
1745 if (lstat(abspath, sb) == -1)
1746 return got_error_from_errno2("lstat", abspath);
1747 } else if (fd == -1 || fstat(fd, sb) == -1) {
1748 if (errno == ENOENT) {
1749 if (got_fileindex_entry_has_file_on_disk(ie))
1750 *status = GOT_STATUS_MISSING;
1752 *status = GOT_STATUS_DELETE;
1755 err = got_error_from_errno2("fstat", abspath);
1760 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1761 *status = GOT_STATUS_OBSTRUCTED;
1765 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1766 *status = GOT_STATUS_DELETE;
1768 } else if (!got_fileindex_entry_has_blob(ie) &&
1769 staged_status != GOT_STATUS_ADD) {
1770 *status = GOT_STATUS_ADD;
1774 if (!stat_info_differs(ie, sb))
1777 if (S_ISLNK(sb->st_mode) &&
1778 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1779 *status = GOT_STATUS_MODIFY;
1783 if (staged_status == GOT_STATUS_MODIFY ||
1784 staged_status == GOT_STATUS_ADD)
1785 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1787 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1789 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1793 if (S_ISLNK(sb->st_mode)) {
1794 err = get_symlink_modification_status(status, ie,
1795 abspath, dirfd, de_name, blob);
1800 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1802 err = got_error_from_errno2("openat", abspath);
1807 f = fdopen(fd, "r");
1809 err = got_error_from_errno2("fdopen", abspath);
1813 hdrlen = got_object_blob_get_hdrlen(blob);
1815 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1816 err = got_object_blob_read_block(&blen, blob);
1819 /* Skip length of blob object header first time around. */
1820 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1821 if (flen == 0 && ferror(f)) {
1822 err = got_error_from_errno("fread");
1825 if (blen - hdrlen == 0) {
1827 *status = GOT_STATUS_MODIFY;
1829 } else if (flen == 0) {
1830 if (blen - hdrlen != 0)
1831 *status = GOT_STATUS_MODIFY;
1833 } else if (blen - hdrlen == flen) {
1834 /* Skip blob object header first time around. */
1835 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1836 *status = GOT_STATUS_MODIFY;
1840 *status = GOT_STATUS_MODIFY;
1846 if (*status == GOT_STATUS_MODIFY) {
1848 err = get_modified_file_content_status(status, f);
1849 } else if (xbit_differs(ie, sb->st_mode))
1850 *status = GOT_STATUS_MODE_CHANGE;
1853 got_object_blob_close(blob);
1854 if (f != NULL && fclose(f) == EOF && err == NULL)
1855 err = got_error_from_errno2("fclose", abspath);
1856 if (fd != -1 && close(fd) == -1 && err == NULL)
1857 err = got_error_from_errno2("close", abspath);
1862 * Update timestamps in the file index if a file is unmodified and
1863 * we had to run a full content comparison to find out.
1865 static const struct got_error *
1866 sync_timestamps(char *ondisk_path, unsigned char status,
1867 struct got_fileindex_entry *ie, struct stat *sb)
1869 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1870 return got_fileindex_entry_update(ie, ondisk_path,
1871 ie->blob_sha1, ie->commit_sha1, 1);
1876 static const struct got_error *
1877 update_blob(struct got_worktree *worktree,
1878 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1879 struct got_tree_entry *te, const char *path,
1880 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1883 const struct got_error *err = NULL;
1884 struct got_blob_object *blob = NULL;
1886 unsigned char status = GOT_STATUS_NO_CHANGE;
1889 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1890 return got_error_from_errno("asprintf");
1893 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1894 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1897 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1901 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1902 sb.st_mode = got_fileindex_perms_to_st(ie);
1904 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1905 status = GOT_STATUS_UNVERSIONED;
1908 if (status == GOT_STATUS_OBSTRUCTED) {
1909 err = (*progress_cb)(progress_arg, status, path);
1912 if (status == GOT_STATUS_CONFLICT) {
1913 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1918 if (ie && status != GOT_STATUS_MISSING &&
1919 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1920 if (got_fileindex_entry_has_commit(ie) &&
1921 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1922 SHA1_DIGEST_LENGTH) == 0) {
1923 err = sync_timestamps(ondisk_path, status, ie, &sb);
1926 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1930 if (got_fileindex_entry_has_blob(ie) &&
1931 memcmp(ie->blob_sha1, te->id.sha1,
1932 SHA1_DIGEST_LENGTH) == 0) {
1933 err = sync_timestamps(ondisk_path, status, ie, &sb);
1938 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1942 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1943 int update_timestamps;
1944 struct got_blob_object *blob2 = NULL;
1945 char *label_orig = NULL;
1946 if (got_fileindex_entry_has_blob(ie)) {
1947 struct got_object_id id2;
1948 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1949 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1953 if (got_fileindex_entry_has_commit(ie)) {
1954 char id_str[SHA1_DIGEST_STRING_LENGTH];
1955 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1956 sizeof(id_str)) == NULL) {
1957 err = got_error_path(id_str,
1958 GOT_ERR_BAD_OBJ_ID_STR);
1961 if (asprintf(&label_orig, "%s: commit %s",
1962 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1963 err = got_error_from_errno("asprintf");
1967 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1969 err = got_object_blob_read_to_str(&link_target, blob);
1972 err = merge_symlink(worktree, blob2, ondisk_path, path,
1973 label_orig, link_target, worktree->base_commit_id,
1974 repo, progress_cb, progress_arg);
1977 err = merge_blob(&update_timestamps, worktree, blob2,
1978 ondisk_path, path, sb.st_mode, label_orig, blob,
1979 worktree->base_commit_id, repo,
1980 progress_cb, progress_arg);
1984 got_object_blob_close(blob2);
1988 * Do not update timestamps of files with local changes.
1989 * Otherwise, a future status walk would treat them as
1990 * unmodified files again.
1992 err = got_fileindex_entry_update(ie, ondisk_path,
1993 blob->id.sha1, worktree->base_commit_id->sha1,
1995 } else if (status == GOT_STATUS_MODE_CHANGE) {
1996 err = got_fileindex_entry_update(ie, ondisk_path,
1997 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1998 } else if (status == GOT_STATUS_DELETE) {
1999 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2002 err = got_fileindex_entry_update(ie, ondisk_path,
2003 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2007 int is_bad_symlink = 0;
2008 if (S_ISLNK(te->mode)) {
2009 err = install_symlink(&is_bad_symlink, worktree,
2010 ondisk_path, path, blob,
2011 status == GOT_STATUS_MISSING, 0,
2012 status == GOT_STATUS_UNVERSIONED, repo,
2013 progress_cb, progress_arg);
2015 err = install_blob(worktree, ondisk_path, path,
2016 te->mode, sb.st_mode, blob,
2017 status == GOT_STATUS_MISSING, 0, 0,
2018 status == GOT_STATUS_UNVERSIONED, repo,
2019 progress_cb, progress_arg);
2025 err = got_fileindex_entry_update(ie, ondisk_path,
2026 blob->id.sha1, worktree->base_commit_id->sha1, 1);
2028 err = create_fileindex_entry(&ie, fileindex,
2029 worktree->base_commit_id, ondisk_path, path,
2035 if (is_bad_symlink) {
2036 got_fileindex_entry_filetype_set(ie,
2037 GOT_FILEIDX_MODE_BAD_SYMLINK);
2040 got_object_blob_close(blob);
2046 static const struct got_error *
2047 remove_ondisk_file(const char *root_path, const char *path)
2049 const struct got_error *err = NULL;
2050 char *ondisk_path = NULL;
2052 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2053 return got_error_from_errno("asprintf");
2055 if (unlink(ondisk_path) == -1) {
2056 if (errno != ENOENT)
2057 err = got_error_from_errno2("unlink", ondisk_path);
2059 size_t root_len = strlen(root_path);
2062 err = got_path_dirname(&parent, ondisk_path);
2066 ondisk_path = parent;
2067 if (rmdir(ondisk_path) == -1) {
2068 if (errno != ENOTEMPTY)
2069 err = got_error_from_errno2("rmdir",
2073 } while (got_path_cmp(ondisk_path, root_path,
2074 strlen(ondisk_path), root_len) != 0);
2080 static const struct got_error *
2081 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2082 struct got_fileindex_entry *ie, struct got_repository *repo,
2083 got_worktree_checkout_cb progress_cb, void *progress_arg)
2085 const struct got_error *err = NULL;
2086 unsigned char status;
2090 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2091 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2093 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2095 return got_error_from_errno("asprintf");
2097 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2101 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2102 char ondisk_target[PATH_MAX];
2103 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2104 sizeof(ondisk_target));
2105 if (ondisk_len == -1) {
2106 err = got_error_from_errno2("readlink", ondisk_path);
2109 ondisk_target[ondisk_len] = '\0';
2110 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2111 NULL, NULL, /* XXX pass common ancestor info? */
2112 ondisk_target, ondisk_path);
2115 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2120 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2121 status == GOT_STATUS_ADD) {
2122 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2126 * Preserve the working file and change the deleted blob's
2127 * entry into a schedule-add entry.
2129 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
2132 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2135 if (status == GOT_STATUS_NO_CHANGE) {
2136 err = remove_ondisk_file(worktree->root_path, ie->path);
2140 got_fileindex_entry_remove(fileindex, ie);
2147 struct diff_cb_arg {
2148 struct got_fileindex *fileindex;
2149 struct got_worktree *worktree;
2150 struct got_repository *repo;
2151 got_worktree_checkout_cb progress_cb;
2153 got_cancel_cb cancel_cb;
2157 static const struct got_error *
2158 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2159 struct got_tree_entry *te, const char *parent_path)
2161 struct diff_cb_arg *a = arg;
2163 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2164 return got_error(GOT_ERR_CANCELLED);
2166 return update_blob(a->worktree, a->fileindex, ie, te,
2167 ie->path, a->repo, a->progress_cb, a->progress_arg);
2170 static const struct got_error *
2171 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2173 struct diff_cb_arg *a = arg;
2175 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2176 return got_error(GOT_ERR_CANCELLED);
2178 return delete_blob(a->worktree, a->fileindex, ie,
2179 a->repo, a->progress_cb, a->progress_arg);
2182 static const struct got_error *
2183 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2185 struct diff_cb_arg *a = arg;
2186 const struct got_error *err;
2189 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2190 return got_error(GOT_ERR_CANCELLED);
2192 if (got_object_tree_entry_is_submodule(te))
2195 if (asprintf(&path, "%s%s%s", parent_path,
2196 parent_path[0] ? "/" : "", te->name)
2198 return got_error_from_errno("asprintf");
2200 if (S_ISDIR(te->mode))
2201 err = add_dir_on_disk(a->worktree, path);
2203 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2204 a->repo, a->progress_cb, a->progress_arg);
2210 const struct got_error *
2211 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2213 uint32_t uuid_status;
2215 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2216 if (uuid_status != uuid_s_ok) {
2218 return got_error_uuid(uuid_status, "uuid_to_string");
2224 static const struct got_error *
2225 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2227 const struct got_error *err = NULL;
2228 char *uuidstr = NULL;
2232 err = got_worktree_get_uuid(&uuidstr, worktree);
2236 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2237 err = got_error_from_errno("asprintf");
2244 const struct got_error *
2245 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2247 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2250 static const struct got_error *
2251 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2253 return get_ref_name(refname, worktree,
2254 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2257 static const struct got_error *
2258 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2260 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2263 static const struct got_error *
2264 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2266 return get_ref_name(refname, worktree,
2267 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2270 static const struct got_error *
2271 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2273 return get_ref_name(refname, worktree,
2274 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2277 static const struct got_error *
2278 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2280 return get_ref_name(refname, worktree,
2281 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2284 static const struct got_error *
2285 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2287 return get_ref_name(refname, worktree,
2288 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2291 static const struct got_error *
2292 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2294 return get_ref_name(refname, worktree,
2295 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2298 static const struct got_error *
2299 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2301 return get_ref_name(refname, worktree,
2302 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2305 const struct got_error *
2306 got_worktree_get_histedit_script_path(char **path,
2307 struct got_worktree *worktree)
2309 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2310 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2312 return got_error_from_errno("asprintf");
2318 * Prevent Git's garbage collector from deleting our base commit by
2319 * setting a reference to our base commit's ID.
2321 static const struct got_error *
2322 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2324 const struct got_error *err = NULL;
2325 struct got_reference *ref = NULL;
2328 err = got_worktree_get_base_ref_name(&refname, worktree);
2332 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2336 err = got_ref_write(ref, repo);
2344 static const struct got_error *
2345 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2347 const struct got_error *err = NULL;
2349 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2350 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2351 err = got_error_from_errno("asprintf");
2352 *fileindex_path = NULL;
2358 static const struct got_error *
2359 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2360 struct got_worktree *worktree)
2362 const struct got_error *err = NULL;
2365 *fileindex_path = NULL;
2366 *fileindex = got_fileindex_alloc();
2367 if (*fileindex == NULL)
2368 return got_error_from_errno("got_fileindex_alloc");
2370 err = get_fileindex_path(fileindex_path, worktree);
2374 index = fopen(*fileindex_path, "rb");
2375 if (index == NULL) {
2376 if (errno != ENOENT)
2377 err = got_error_from_errno2("fopen", *fileindex_path);
2379 err = got_fileindex_read(*fileindex, index);
2380 if (fclose(index) != 0 && err == NULL)
2381 err = got_error_from_errno("fclose");
2385 free(*fileindex_path);
2386 *fileindex_path = NULL;
2387 got_fileindex_free(*fileindex);
2393 struct bump_base_commit_id_arg {
2394 struct got_object_id *base_commit_id;
2397 const char *entry_name;
2398 got_worktree_checkout_cb progress_cb;
2402 /* Bump base commit ID of all files within an updated part of the work tree. */
2403 static const struct got_error *
2404 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2406 const struct got_error *err;
2407 struct bump_base_commit_id_arg *a = arg;
2409 if (a->entry_name) {
2410 if (strcmp(ie->path, a->path) != 0)
2412 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2415 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2416 SHA1_DIGEST_LENGTH) == 0)
2419 if (a->progress_cb) {
2420 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2425 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2429 static const struct got_error *
2430 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2432 const struct got_error *err = NULL;
2433 char *new_fileindex_path = NULL;
2434 FILE *new_index = NULL;
2435 struct timespec timeout;
2437 err = got_opentemp_named(&new_fileindex_path, &new_index,
2442 err = got_fileindex_write(fileindex, new_index);
2446 if (rename(new_fileindex_path, fileindex_path) != 0) {
2447 err = got_error_from_errno3("rename", new_fileindex_path,
2449 unlink(new_fileindex_path);
2453 * Sleep for a short amount of time to ensure that files modified after
2454 * this program exits have a different time stamp from the one which
2455 * was recorded in the file index.
2458 timeout.tv_nsec = 1;
2459 nanosleep(&timeout, NULL);
2463 free(new_fileindex_path);
2467 static const struct got_error *
2468 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2469 struct got_object_id **tree_id, const char *wt_relpath,
2470 struct got_worktree *worktree, struct got_repository *repo)
2472 const struct got_error *err = NULL;
2473 struct got_object_id *id = NULL;
2474 char *in_repo_path = NULL;
2475 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2477 *entry_type = GOT_OBJ_TYPE_ANY;
2478 *tree_relpath = NULL;
2481 if (wt_relpath[0] == '\0') {
2482 /* Check out all files within the work tree. */
2483 *entry_type = GOT_OBJ_TYPE_TREE;
2484 *tree_relpath = strdup("");
2485 if (*tree_relpath == NULL) {
2486 err = got_error_from_errno("strdup");
2489 err = got_object_id_by_path(tree_id, repo,
2490 worktree->base_commit_id, worktree->path_prefix);
2496 /* Check out a subset of files in the work tree. */
2498 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2499 is_root_wt ? "" : "/", wt_relpath) == -1) {
2500 err = got_error_from_errno("asprintf");
2504 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2510 in_repo_path = NULL;
2512 err = got_object_get_type(entry_type, repo, id);
2516 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2517 /* Check out a single file. */
2518 if (strchr(wt_relpath, '/') == NULL) {
2519 /* Check out a single file in work tree's root dir. */
2520 in_repo_path = strdup(worktree->path_prefix);
2521 if (in_repo_path == NULL) {
2522 err = got_error_from_errno("strdup");
2525 *tree_relpath = strdup("");
2526 if (*tree_relpath == NULL) {
2527 err = got_error_from_errno("strdup");
2531 /* Check out a single file in a subdirectory. */
2532 err = got_path_dirname(tree_relpath, wt_relpath);
2535 if (asprintf(&in_repo_path, "%s%s%s",
2536 worktree->path_prefix, is_root_wt ? "" : "/",
2537 *tree_relpath) == -1) {
2538 err = got_error_from_errno("asprintf");
2542 err = got_object_id_by_path(tree_id, repo,
2543 worktree->base_commit_id, in_repo_path);
2545 /* Check out all files within a subdirectory. */
2546 *tree_id = got_object_id_dup(id);
2547 if (*tree_id == NULL) {
2548 err = got_error_from_errno("got_object_id_dup");
2551 *tree_relpath = strdup(wt_relpath);
2552 if (*tree_relpath == NULL) {
2553 err = got_error_from_errno("strdup");
2561 *entry_type = GOT_OBJ_TYPE_ANY;
2562 free(*tree_relpath);
2563 *tree_relpath = NULL;
2570 static const struct got_error *
2571 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2572 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2573 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2574 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2576 const struct got_error *err = NULL;
2577 struct got_commit_object *commit = NULL;
2578 struct got_tree_object *tree = NULL;
2579 struct got_fileindex_diff_tree_cb diff_cb;
2580 struct diff_cb_arg arg;
2582 err = ref_base_commit(worktree, repo);
2584 if (!(err->code == GOT_ERR_ERRNO &&
2585 (errno == EACCES || errno == EROFS)))
2587 err = (*progress_cb)(progress_arg,
2588 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2593 err = got_object_open_as_commit(&commit, repo,
2594 worktree->base_commit_id);
2598 err = got_object_open_as_tree(&tree, repo, tree_id);
2603 got_object_tree_find_entry(tree, entry_name) == NULL) {
2604 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2608 diff_cb.diff_old_new = diff_old_new;
2609 diff_cb.diff_old = diff_old;
2610 diff_cb.diff_new = diff_new;
2611 arg.fileindex = fileindex;
2612 arg.worktree = worktree;
2614 arg.progress_cb = progress_cb;
2615 arg.progress_arg = progress_arg;
2616 arg.cancel_cb = cancel_cb;
2617 arg.cancel_arg = cancel_arg;
2618 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2619 entry_name, repo, &diff_cb, &arg);
2622 got_object_tree_close(tree);
2624 got_object_commit_close(commit);
2628 const struct got_error *
2629 got_worktree_checkout_files(struct got_worktree *worktree,
2630 struct got_pathlist_head *paths, struct got_repository *repo,
2631 got_worktree_checkout_cb progress_cb, void *progress_arg,
2632 got_cancel_cb cancel_cb, void *cancel_arg)
2634 const struct got_error *err = NULL, *sync_err, *unlockerr;
2635 struct got_commit_object *commit = NULL;
2636 struct got_tree_object *tree = NULL;
2637 struct got_fileindex *fileindex = NULL;
2638 char *fileindex_path = NULL;
2639 struct got_pathlist_entry *pe;
2640 struct tree_path_data {
2641 SIMPLEQ_ENTRY(tree_path_data) entry;
2642 struct got_object_id *tree_id;
2647 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2649 SIMPLEQ_INIT(&tree_paths);
2651 err = lock_worktree(worktree, LOCK_EX);
2655 /* Map all specified paths to in-repository trees. */
2656 TAILQ_FOREACH(pe, paths, entry) {
2657 tpd = malloc(sizeof(*tpd));
2659 err = got_error_from_errno("malloc");
2663 err = find_tree_entry_for_checkout(&tpd->entry_type,
2664 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2670 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2671 err = got_path_basename(&tpd->entry_name, pe->path);
2679 tpd->entry_name = NULL;
2681 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2685 * Read the file index.
2686 * Checking out files is supposed to be an idempotent operation.
2687 * If the on-disk file index is incomplete we will try to complete it.
2689 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2693 tpd = SIMPLEQ_FIRST(&tree_paths);
2694 TAILQ_FOREACH(pe, paths, entry) {
2695 struct bump_base_commit_id_arg bbc_arg;
2697 err = checkout_files(worktree, fileindex, tpd->relpath,
2698 tpd->tree_id, tpd->entry_name, repo,
2699 progress_cb, progress_arg, cancel_cb, cancel_arg);
2703 bbc_arg.base_commit_id = worktree->base_commit_id;
2704 bbc_arg.entry_name = tpd->entry_name;
2705 bbc_arg.path = pe->path;
2706 bbc_arg.path_len = pe->path_len;
2707 bbc_arg.progress_cb = progress_cb;
2708 bbc_arg.progress_arg = progress_arg;
2709 err = got_fileindex_for_each_entry_safe(fileindex,
2710 bump_base_commit_id, &bbc_arg);
2714 tpd = SIMPLEQ_NEXT(tpd, entry);
2716 sync_err = sync_fileindex(fileindex, fileindex_path);
2717 if (sync_err && err == NULL)
2720 free(fileindex_path);
2722 got_object_tree_close(tree);
2724 got_object_commit_close(commit);
2726 got_fileindex_free(fileindex);
2727 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2728 tpd = SIMPLEQ_FIRST(&tree_paths);
2729 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2734 unlockerr = lock_worktree(worktree, LOCK_SH);
2735 if (unlockerr && err == NULL)
2740 struct merge_file_cb_arg {
2741 struct got_worktree *worktree;
2742 struct got_fileindex *fileindex;
2743 got_worktree_checkout_cb progress_cb;
2745 got_cancel_cb cancel_cb;
2747 const char *label_orig;
2748 struct got_object_id *commit_id2;
2751 static const struct got_error *
2752 merge_file_cb(void *arg, struct got_blob_object *blob1,
2753 struct got_blob_object *blob2, struct got_object_id *id1,
2754 struct got_object_id *id2, const char *path1, const char *path2,
2755 mode_t mode1, mode_t mode2, struct got_repository *repo)
2757 static const struct got_error *err = NULL;
2758 struct merge_file_cb_arg *a = arg;
2759 struct got_fileindex_entry *ie;
2760 char *ondisk_path = NULL;
2762 unsigned char status;
2763 int local_changes_subsumed;
2765 if (blob1 && blob2) {
2766 ie = got_fileindex_entry_get(a->fileindex, path2,
2769 return (*a->progress_cb)(a->progress_arg,
2770 GOT_STATUS_MISSING, path2);
2772 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2774 return got_error_from_errno("asprintf");
2776 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2781 if (status == GOT_STATUS_DELETE) {
2782 err = (*a->progress_cb)(a->progress_arg,
2783 GOT_STATUS_MERGE, path2);
2786 if (status != GOT_STATUS_NO_CHANGE &&
2787 status != GOT_STATUS_MODIFY &&
2788 status != GOT_STATUS_CONFLICT &&
2789 status != GOT_STATUS_ADD) {
2790 err = (*a->progress_cb)(a->progress_arg, status, path2);
2794 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2796 err = got_object_blob_read_to_str(&link_target2, blob2);
2799 err = merge_symlink(a->worktree, blob1, ondisk_path,
2800 path2, a->label_orig, link_target2, a->commit_id2,
2801 repo, a->progress_cb, a->progress_arg);
2804 err = merge_blob(&local_changes_subsumed, a->worktree,
2805 blob1, ondisk_path, path2, sb.st_mode,
2806 a->label_orig, blob2, a->commit_id2, repo,
2807 a->progress_cb, a->progress_arg);
2810 ie = got_fileindex_entry_get(a->fileindex, path1,
2813 return (*a->progress_cb)(a->progress_arg,
2814 GOT_STATUS_MISSING, path1);
2816 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2818 return got_error_from_errno("asprintf");
2820 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2826 case GOT_STATUS_NO_CHANGE:
2827 err = (*a->progress_cb)(a->progress_arg,
2828 GOT_STATUS_DELETE, path1);
2831 err = remove_ondisk_file(a->worktree->root_path, path1);
2835 got_fileindex_entry_mark_deleted_from_disk(ie);
2837 case GOT_STATUS_DELETE:
2838 case GOT_STATUS_MISSING:
2839 err = (*a->progress_cb)(a->progress_arg,
2840 GOT_STATUS_DELETE, path1);
2844 got_fileindex_entry_mark_deleted_from_disk(ie);
2846 case GOT_STATUS_ADD: {
2847 struct got_object_id *id;
2850 * Delete the added file only if its content already
2851 * exists in the repository.
2853 err = got_object_blob_file_create(&id, &blob1_f, path1);
2856 if (got_object_id_cmp(id, id1) == 0) {
2857 err = (*a->progress_cb)(a->progress_arg,
2858 GOT_STATUS_DELETE, path1);
2861 err = remove_ondisk_file(a->worktree->root_path,
2866 got_fileindex_entry_remove(a->fileindex,
2869 err = (*a->progress_cb)(a->progress_arg,
2870 GOT_STATUS_CANNOT_DELETE, path1);
2872 if (fclose(blob1_f) == EOF && err == NULL)
2873 err = got_error_from_errno("fclose");
2879 case GOT_STATUS_MODIFY:
2880 case GOT_STATUS_CONFLICT:
2881 err = (*a->progress_cb)(a->progress_arg,
2882 GOT_STATUS_CANNOT_DELETE, path1);
2886 case GOT_STATUS_OBSTRUCTED:
2887 err = (*a->progress_cb)(a->progress_arg, status, path1);
2895 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2897 return got_error_from_errno("asprintf");
2898 ie = got_fileindex_entry_get(a->fileindex, path2,
2901 err = get_file_status(&status, &sb, ie, ondisk_path,
2905 if (status != GOT_STATUS_NO_CHANGE &&
2906 status != GOT_STATUS_MODIFY &&
2907 status != GOT_STATUS_CONFLICT &&
2908 status != GOT_STATUS_ADD) {
2909 err = (*a->progress_cb)(a->progress_arg,
2913 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2915 err = got_object_blob_read_to_str(&link_target2,
2919 err = merge_symlink(a->worktree, NULL,
2920 ondisk_path, path2, a->label_orig,
2921 link_target2, a->commit_id2, repo,
2922 a->progress_cb, a->progress_arg);
2924 } else if (S_ISREG(sb.st_mode)) {
2925 err = merge_blob(&local_changes_subsumed,
2926 a->worktree, NULL, ondisk_path, path2,
2927 sb.st_mode, a->label_orig, blob2,
2928 a->commit_id2, repo, a->progress_cb,
2931 err = got_error_path(ondisk_path,
2932 GOT_ERR_FILE_OBSTRUCTED);
2936 if (status == GOT_STATUS_DELETE) {
2937 err = got_fileindex_entry_update(ie,
2938 ondisk_path, blob2->id.sha1,
2939 a->worktree->base_commit_id->sha1, 0);
2944 int is_bad_symlink = 0;
2945 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2946 if (S_ISLNK(mode2)) {
2947 err = install_symlink(&is_bad_symlink,
2948 a->worktree, ondisk_path, path2, blob2, 0,
2949 0, 1, repo, a->progress_cb, a->progress_arg);
2951 err = install_blob(a->worktree, ondisk_path, path2,
2952 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2953 a->progress_cb, a->progress_arg);
2957 err = got_fileindex_entry_alloc(&ie, path2);
2960 err = got_fileindex_entry_update(ie, ondisk_path,
2963 got_fileindex_entry_free(ie);
2966 err = got_fileindex_entry_add(a->fileindex, ie);
2968 got_fileindex_entry_free(ie);
2971 if (is_bad_symlink) {
2972 got_fileindex_entry_filetype_set(ie,
2973 GOT_FILEIDX_MODE_BAD_SYMLINK);
2982 struct check_merge_ok_arg {
2983 struct got_worktree *worktree;
2984 struct got_repository *repo;
2987 static const struct got_error *
2988 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2990 const struct got_error *err = NULL;
2991 struct check_merge_ok_arg *a = arg;
2992 unsigned char status;
2996 /* Reject merges into a work tree with mixed base commits. */
2997 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2998 SHA1_DIGEST_LENGTH))
2999 return got_error(GOT_ERR_MIXED_COMMITS);
3001 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3003 return got_error_from_errno("asprintf");
3005 /* Reject merges into a work tree with conflicted files. */
3006 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3009 if (status == GOT_STATUS_CONFLICT)
3010 return got_error(GOT_ERR_CONFLICTS);
3015 static const struct got_error *
3016 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3017 const char *fileindex_path, struct got_object_id *commit_id1,
3018 struct got_object_id *commit_id2, struct got_repository *repo,
3019 got_worktree_checkout_cb progress_cb, void *progress_arg,
3020 got_cancel_cb cancel_cb, void *cancel_arg)
3022 const struct got_error *err = NULL, *sync_err;
3023 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3024 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3025 struct merge_file_cb_arg arg;
3026 char *label_orig = NULL;
3029 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3030 worktree->path_prefix);
3031 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3037 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3041 err = got_object_id_str(&id_str, commit_id1);
3045 if (asprintf(&label_orig, "%s: commit %s",
3046 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3047 err = got_error_from_errno("asprintf");
3054 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3055 worktree->path_prefix);
3059 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3063 arg.worktree = worktree;
3064 arg.fileindex = fileindex;
3065 arg.progress_cb = progress_cb;
3066 arg.progress_arg = progress_arg;
3067 arg.cancel_cb = cancel_cb;
3068 arg.cancel_arg = cancel_arg;
3069 arg.label_orig = label_orig;
3070 arg.commit_id2 = commit_id2;
3071 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3072 sync_err = sync_fileindex(fileindex, fileindex_path);
3073 if (sync_err && err == NULL)
3077 got_object_tree_close(tree1);
3079 got_object_tree_close(tree2);
3084 const struct got_error *
3085 got_worktree_merge_files(struct got_worktree *worktree,
3086 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3087 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3088 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3090 const struct got_error *err, *unlockerr;
3091 char *fileindex_path = NULL;
3092 struct got_fileindex *fileindex = NULL;
3093 struct check_merge_ok_arg mok_arg;
3095 err = lock_worktree(worktree, LOCK_EX);
3099 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3103 mok_arg.worktree = worktree;
3104 mok_arg.repo = repo;
3105 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
3110 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3111 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
3114 got_fileindex_free(fileindex);
3115 free(fileindex_path);
3116 unlockerr = lock_worktree(worktree, LOCK_SH);
3117 if (unlockerr && err == NULL)
3122 struct diff_dir_cb_arg {
3123 struct got_fileindex *fileindex;
3124 struct got_worktree *worktree;
3125 const char *status_path;
3126 size_t status_path_len;
3127 struct got_repository *repo;
3128 got_worktree_status_cb status_cb;
3130 got_cancel_cb cancel_cb;
3132 /* A pathlist containing per-directory pathlists of ignore patterns. */
3133 struct got_pathlist_head ignores;
3134 int report_unchanged;
3138 static const struct got_error *
3139 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3140 int dirfd, const char *de_name,
3141 got_worktree_status_cb status_cb, void *status_arg,
3142 struct got_repository *repo, int report_unchanged)
3144 const struct got_error *err = NULL;
3145 unsigned char status = GOT_STATUS_NO_CHANGE;
3146 unsigned char staged_status = get_staged_status(ie);
3148 struct got_object_id blob_id, commit_id, staged_blob_id;
3149 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3150 struct got_object_id *staged_blob_idp = NULL;
3152 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3156 if (status == GOT_STATUS_NO_CHANGE &&
3157 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3160 if (got_fileindex_entry_has_blob(ie)) {
3161 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3162 blob_idp = &blob_id;
3164 if (got_fileindex_entry_has_commit(ie)) {
3165 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3166 commit_idp = &commit_id;
3168 if (staged_status == GOT_STATUS_ADD ||
3169 staged_status == GOT_STATUS_MODIFY) {
3170 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3171 SHA1_DIGEST_LENGTH);
3172 staged_blob_idp = &staged_blob_id;
3175 return (*status_cb)(status_arg, status, staged_status,
3176 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3179 static const struct got_error *
3180 status_old_new(void *arg, struct got_fileindex_entry *ie,
3181 struct dirent *de, const char *parent_path, int dirfd)
3183 const struct got_error *err = NULL;
3184 struct diff_dir_cb_arg *a = arg;
3187 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3188 return got_error(GOT_ERR_CANCELLED);
3190 if (got_path_cmp(parent_path, a->status_path,
3191 strlen(parent_path), a->status_path_len) != 0 &&
3192 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3195 if (parent_path[0]) {
3196 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3197 parent_path, de->d_name) == -1)
3198 return got_error_from_errno("asprintf");
3200 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3202 return got_error_from_errno("asprintf");
3205 err = report_file_status(ie, abspath, dirfd, de->d_name,
3206 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3211 static const struct got_error *
3212 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3214 struct diff_dir_cb_arg *a = arg;
3215 struct got_object_id blob_id, commit_id;
3216 unsigned char status;
3218 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3219 return got_error(GOT_ERR_CANCELLED);
3221 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3224 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3225 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3226 if (got_fileindex_entry_has_file_on_disk(ie))
3227 status = GOT_STATUS_MISSING;
3229 status = GOT_STATUS_DELETE;
3230 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3231 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3235 free_ignorelist(struct got_pathlist_head *ignorelist)
3237 struct got_pathlist_entry *pe;
3239 TAILQ_FOREACH(pe, ignorelist, entry)
3240 free((char *)pe->path);
3241 got_pathlist_free(ignorelist);
3245 free_ignores(struct got_pathlist_head *ignores)
3247 struct got_pathlist_entry *pe;
3249 TAILQ_FOREACH(pe, ignores, entry) {
3250 struct got_pathlist_head *ignorelist = pe->data;
3251 free_ignorelist(ignorelist);
3252 free((char *)pe->path);
3254 got_pathlist_free(ignores);
3257 static const struct got_error *
3258 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3260 const struct got_error *err = NULL;
3261 struct got_pathlist_entry *pe = NULL;
3262 struct got_pathlist_head *ignorelist;
3263 char *line = NULL, *pattern, *dirpath = NULL;
3264 size_t linesize = 0;
3267 ignorelist = calloc(1, sizeof(*ignorelist));
3268 if (ignorelist == NULL)
3269 return got_error_from_errno("calloc");
3270 TAILQ_INIT(ignorelist);
3272 while ((linelen = getline(&line, &linesize, f)) != -1) {
3273 if (linelen > 0 && line[linelen - 1] == '\n')
3274 line[linelen - 1] = '\0';
3276 /* Git's ignores may contain comments. */
3280 /* Git's negated patterns are not (yet?) supported. */
3284 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3286 err = got_error_from_errno("asprintf");
3289 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3294 err = got_error_from_errno("getline");
3298 dirpath = strdup(path);
3299 if (dirpath == NULL) {
3300 err = got_error_from_errno("strdup");
3303 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3306 if (err || pe == NULL) {
3308 free_ignorelist(ignorelist);
3314 match_ignores(struct got_pathlist_head *ignores, const char *path)
3316 struct got_pathlist_entry *pe;
3318 /* Handle patterns which match in all directories. */
3319 TAILQ_FOREACH(pe, ignores, entry) {
3320 struct got_pathlist_head *ignorelist = pe->data;
3321 struct got_pathlist_entry *pi;
3323 TAILQ_FOREACH(pi, ignorelist, entry) {
3324 const char *p, *pattern = pi->path;
3326 if (strncmp(pattern, "**/", 3) != 0)
3331 if (fnmatch(pattern, p,
3332 FNM_PATHNAME | FNM_LEADING_DIR)) {
3333 /* Retry in next directory. */
3334 while (*p && *p != '/')
3346 * The ignores pathlist contains ignore lists from children before
3347 * parents, so we can find the most specific ignorelist by walking
3348 * ignores backwards.
3350 pe = TAILQ_LAST(ignores, got_pathlist_head);
3352 if (got_path_is_child(path, pe->path, pe->path_len)) {
3353 struct got_pathlist_head *ignorelist = pe->data;
3354 struct got_pathlist_entry *pi;
3355 TAILQ_FOREACH(pi, ignorelist, entry) {
3356 const char *pattern = pi->path;
3357 int flags = FNM_LEADING_DIR;
3358 if (strstr(pattern, "/**/") == NULL)
3359 flags |= FNM_PATHNAME;
3360 if (fnmatch(pattern, path, flags))
3365 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3371 static const struct got_error *
3372 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3373 const char *path, int dirfd, const char *ignores_filename)
3375 const struct got_error *err = NULL;
3378 FILE *ignoresfile = NULL;
3380 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3381 path[0] ? "/" : "", ignores_filename) == -1)
3382 return got_error_from_errno("asprintf");
3385 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3387 if (errno != ENOENT && errno != EACCES)
3388 err = got_error_from_errno2("openat",
3391 ignoresfile = fdopen(fd, "r");
3392 if (ignoresfile == NULL)
3393 err = got_error_from_errno2("fdopen",
3397 err = read_ignores(ignores, path, ignoresfile);
3401 ignoresfile = fopen(ignorespath, "r");
3402 if (ignoresfile == NULL) {
3403 if (errno != ENOENT && errno != EACCES)
3404 err = got_error_from_errno2("fopen",
3407 err = read_ignores(ignores, path, ignoresfile);
3410 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3411 err = got_error_from_errno2("fclose", path);
3412 if (fd != -1 && close(fd) == -1 && err == NULL)
3413 err = got_error_from_errno2("close", path);
3418 static const struct got_error *
3419 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3421 const struct got_error *err = NULL;
3422 struct diff_dir_cb_arg *a = arg;
3425 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3426 return got_error(GOT_ERR_CANCELLED);
3428 if (parent_path[0]) {
3429 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3430 return got_error_from_errno("asprintf");
3435 if (de->d_type != DT_DIR &&
3436 got_path_is_child(path, a->status_path, a->status_path_len)
3437 && !match_ignores(&a->ignores, path))
3438 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3439 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3445 static const struct got_error *
3446 status_traverse(void *arg, const char *path, int dirfd)
3448 const struct got_error *err = NULL;
3449 struct diff_dir_cb_arg *a = arg;
3454 err = add_ignores(&a->ignores, a->worktree->root_path,
3455 path, dirfd, ".cvsignore");
3459 err = add_ignores(&a->ignores, a->worktree->root_path, path,
3460 dirfd, ".gitignore");
3465 static const struct got_error *
3466 report_single_file_status(const char *path, const char *ondisk_path,
3467 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3468 void *status_arg, struct got_repository *repo, int report_unchanged)
3470 struct got_fileindex_entry *ie;
3473 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3475 return report_file_status(ie, ondisk_path, -1, NULL,
3476 status_cb, status_arg, repo, report_unchanged);
3478 if (lstat(ondisk_path, &sb) == -1) {
3479 if (errno != ENOENT)
3480 return got_error_from_errno2("lstat", ondisk_path);
3481 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3482 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3486 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3487 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3488 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3493 static const struct got_error *
3494 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3495 const char *root_path, const char *path)
3497 const struct got_error *err;
3498 char *parent_path, *next_parent_path = NULL;
3500 err = add_ignores(ignores, root_path, "", -1,
3505 err = add_ignores(ignores, root_path, "", -1,
3510 err = got_path_dirname(&parent_path, path);
3512 if (err->code == GOT_ERR_BAD_PATH)
3513 return NULL; /* cannot traverse parent */
3517 err = add_ignores(ignores, root_path, parent_path, -1,
3521 err = add_ignores(ignores, root_path, parent_path, -1,
3525 err = got_path_dirname(&next_parent_path, parent_path);
3527 if (err->code == GOT_ERR_BAD_PATH)
3528 err = NULL; /* traversed everything */
3532 parent_path = next_parent_path;
3533 next_parent_path = NULL;
3537 free(next_parent_path);
3541 static const struct got_error *
3542 worktree_status(struct got_worktree *worktree, const char *path,
3543 struct got_fileindex *fileindex, struct got_repository *repo,
3544 got_worktree_status_cb status_cb, void *status_arg,
3545 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3546 int report_unchanged)
3548 const struct got_error *err = NULL;
3550 struct got_fileindex_diff_dir_cb fdiff_cb;
3551 struct diff_dir_cb_arg arg;
3552 char *ondisk_path = NULL;
3554 TAILQ_INIT(&arg.ignores);
3556 if (asprintf(&ondisk_path, "%s%s%s",
3557 worktree->root_path, path[0] ? "/" : "", path) == -1)
3558 return got_error_from_errno("asprintf");
3560 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3562 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3564 err = got_error_from_errno2("open", ondisk_path);
3566 err = report_single_file_status(path, ondisk_path,
3567 fileindex, status_cb, status_arg, repo,
3570 fdiff_cb.diff_old_new = status_old_new;
3571 fdiff_cb.diff_old = status_old;
3572 fdiff_cb.diff_new = status_new;
3573 fdiff_cb.diff_traverse = status_traverse;
3574 arg.fileindex = fileindex;
3575 arg.worktree = worktree;
3576 arg.status_path = path;
3577 arg.status_path_len = strlen(path);
3579 arg.status_cb = status_cb;
3580 arg.status_arg = status_arg;
3581 arg.cancel_cb = cancel_cb;
3582 arg.cancel_arg = cancel_arg;
3583 arg.report_unchanged = report_unchanged;
3584 arg.no_ignores = no_ignores;
3586 err = add_ignores_from_parent_paths(&arg.ignores,
3587 worktree->root_path, path);
3591 err = got_fileindex_diff_dir(fileindex, fd,
3592 worktree->root_path, path, repo, &fdiff_cb, &arg);
3595 free_ignores(&arg.ignores);
3596 if (fd != -1 && close(fd) != 0 && err == NULL)
3597 err = got_error_from_errno("close");
3602 const struct got_error *
3603 got_worktree_status(struct got_worktree *worktree,
3604 struct got_pathlist_head *paths, struct got_repository *repo,
3605 got_worktree_status_cb status_cb, void *status_arg,
3606 got_cancel_cb cancel_cb, void *cancel_arg)
3608 const struct got_error *err = NULL;
3609 char *fileindex_path = NULL;
3610 struct got_fileindex *fileindex = NULL;
3611 struct got_pathlist_entry *pe;
3613 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3617 TAILQ_FOREACH(pe, paths, entry) {
3618 err = worktree_status(worktree, pe->path, fileindex, repo,
3619 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3623 free(fileindex_path);
3624 got_fileindex_free(fileindex);
3628 const struct got_error *
3629 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3632 const struct got_error *err = NULL;
3633 char *resolved = NULL, *cwd = NULL, *path = NULL;
3636 char *abspath = NULL;
3637 char canonpath[PATH_MAX];
3641 cwd = getcwd(NULL, 0);
3643 return got_error_from_errno("getcwd");
3645 if (lstat(arg, &sb) == -1) {
3646 if (errno != ENOENT) {
3647 err = got_error_from_errno2("lstat", arg);
3652 if (S_ISLNK(sb.st_mode)) {
3654 * We cannot use realpath(3) with symlinks since we want to
3655 * operate on the symlink itself.
3656 * But we can make the path absolute, assuming it is relative
3657 * to the current working directory, and then canonicalize it.
3659 if (!got_path_is_absolute(arg)) {
3660 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3661 err = got_error_from_errno("asprintf");
3666 err = got_canonpath(abspath ? abspath : arg, canonpath,
3670 resolved = strdup(canonpath);
3671 if (resolved == NULL) {
3672 err = got_error_from_errno("strdup");
3676 resolved = realpath(arg, NULL);
3677 if (resolved == NULL) {
3678 if (errno != ENOENT) {
3679 err = got_error_from_errno2("realpath", arg);
3682 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3683 err = got_error_from_errno("asprintf");
3686 err = got_canonpath(abspath, canonpath,
3690 resolved = strdup(canonpath);
3691 if (resolved == NULL) {
3692 err = got_error_from_errno("strdup");
3698 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3699 strlen(got_worktree_get_root_path(worktree)))) {
3700 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3704 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3705 err = got_path_skip_common_ancestor(&path,
3706 got_worktree_get_root_path(worktree), resolved);
3712 err = got_error_from_errno("strdup");
3717 /* XXX status walk can't deal with trailing slash! */
3719 while (len > 0 && path[len - 1] == '/') {
3720 path[len - 1] = '\0';
3734 struct schedule_addition_args {
3735 struct got_worktree *worktree;
3736 struct got_fileindex *fileindex;
3737 got_worktree_checkout_cb progress_cb;
3739 struct got_repository *repo;
3742 static const struct got_error *
3743 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3744 const char *relpath, struct got_object_id *blob_id,
3745 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3746 int dirfd, const char *de_name)
3748 struct schedule_addition_args *a = arg;
3749 const struct got_error *err = NULL;
3750 struct got_fileindex_entry *ie;
3754 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3756 return got_error_from_errno("asprintf");
3758 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3760 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3764 /* Re-adding an existing entry is a no-op. */
3765 if (status == GOT_STATUS_ADD)
3767 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3772 if (status != GOT_STATUS_UNVERSIONED) {
3773 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3777 err = got_fileindex_entry_alloc(&ie, relpath);
3780 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3782 got_fileindex_entry_free(ie);
3785 err = got_fileindex_entry_add(a->fileindex, ie);
3787 got_fileindex_entry_free(ie);
3794 if (status == GOT_STATUS_ADD)
3796 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3799 const struct got_error *
3800 got_worktree_schedule_add(struct got_worktree *worktree,
3801 struct got_pathlist_head *paths,
3802 got_worktree_checkout_cb progress_cb, void *progress_arg,
3803 struct got_repository *repo, int no_ignores)
3805 struct got_fileindex *fileindex = NULL;
3806 char *fileindex_path = NULL;
3807 const struct got_error *err = NULL, *sync_err, *unlockerr;
3808 struct got_pathlist_entry *pe;
3809 struct schedule_addition_args saa;
3811 err = lock_worktree(worktree, LOCK_EX);
3815 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3819 saa.worktree = worktree;
3820 saa.fileindex = fileindex;
3821 saa.progress_cb = progress_cb;
3822 saa.progress_arg = progress_arg;
3825 TAILQ_FOREACH(pe, paths, entry) {
3826 err = worktree_status(worktree, pe->path, fileindex, repo,
3827 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3831 sync_err = sync_fileindex(fileindex, fileindex_path);
3832 if (sync_err && err == NULL)
3835 free(fileindex_path);
3837 got_fileindex_free(fileindex);
3838 unlockerr = lock_worktree(worktree, LOCK_SH);
3839 if (unlockerr && err == NULL)
3844 struct schedule_deletion_args {
3845 struct got_worktree *worktree;
3846 struct got_fileindex *fileindex;
3847 got_worktree_delete_cb progress_cb;
3849 struct got_repository *repo;
3850 int delete_local_mods;
3852 const char *status_codes;
3855 static const struct got_error *
3856 schedule_for_deletion(void *arg, unsigned char status,
3857 unsigned char staged_status, const char *relpath,
3858 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3859 struct got_object_id *commit_id, int dirfd, const char *de_name)
3861 struct schedule_deletion_args *a = arg;
3862 const struct got_error *err = NULL;
3863 struct got_fileindex_entry *ie = NULL;
3867 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3869 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3871 staged_status = get_staged_status(ie);
3872 if (staged_status != GOT_STATUS_NO_CHANGE) {
3873 if (staged_status == GOT_STATUS_DELETE)
3875 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3878 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3880 return got_error_from_errno("asprintf");
3882 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3887 if (a->status_codes) {
3888 size_t ncodes = strlen(a->status_codes);
3890 for (i = 0; i < ncodes ; i++) {
3891 if (status == a->status_codes[i])
3895 /* Do not delete files in non-matching status. */
3899 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
3900 a->status_codes[i] != GOT_STATUS_MISSING) {
3901 static char msg[64];
3902 snprintf(msg, sizeof(msg),
3903 "invalid status code '%c'", a->status_codes[i]);
3904 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
3909 if (status != GOT_STATUS_NO_CHANGE) {
3910 if (status == GOT_STATUS_DELETE)
3912 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3913 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3916 if (status != GOT_STATUS_MODIFY &&
3917 status != GOT_STATUS_MISSING) {
3918 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3923 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3927 if (unlinkat(dirfd, de_name, 0) != 0) {
3928 err = got_error_from_errno2("unlinkat",
3932 } else if (unlink(ondisk_path) != 0) {
3933 err = got_error_from_errno2("unlink", ondisk_path);
3937 root_len = strlen(a->worktree->root_path);
3940 err = got_path_dirname(&parent, ondisk_path);
3944 ondisk_path = parent;
3945 if (rmdir(ondisk_path) == -1) {
3946 if (errno != ENOTEMPTY)
3947 err = got_error_from_errno2("rmdir",
3951 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
3952 strlen(ondisk_path), root_len) != 0);
3955 got_fileindex_entry_mark_deleted_from_disk(ie);
3960 if (status == GOT_STATUS_DELETE)
3962 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3963 staged_status, relpath);
3966 const struct got_error *
3967 got_worktree_schedule_delete(struct got_worktree *worktree,
3968 struct got_pathlist_head *paths, int delete_local_mods,
3969 const char *status_codes,
3970 got_worktree_delete_cb progress_cb, void *progress_arg,
3971 struct got_repository *repo, int keep_on_disk)
3973 struct got_fileindex *fileindex = NULL;
3974 char *fileindex_path = NULL;
3975 const struct got_error *err = NULL, *sync_err, *unlockerr;
3976 struct got_pathlist_entry *pe;
3977 struct schedule_deletion_args sda;
3979 err = lock_worktree(worktree, LOCK_EX);
3983 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3987 sda.worktree = worktree;
3988 sda.fileindex = fileindex;
3989 sda.progress_cb = progress_cb;
3990 sda.progress_arg = progress_arg;
3992 sda.delete_local_mods = delete_local_mods;
3993 sda.keep_on_disk = keep_on_disk;
3994 sda.status_codes = status_codes;
3996 TAILQ_FOREACH(pe, paths, entry) {
3997 err = worktree_status(worktree, pe->path, fileindex, repo,
3998 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
4002 sync_err = sync_fileindex(fileindex, fileindex_path);
4003 if (sync_err && err == NULL)
4006 free(fileindex_path);
4008 got_fileindex_free(fileindex);
4009 unlockerr = lock_worktree(worktree, LOCK_SH);
4010 if (unlockerr && err == NULL)
4015 static const struct got_error *
4016 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4018 const struct got_error *err = NULL;
4020 size_t linesize = 0, n;
4023 linelen = getline(&line, &linesize, infile);
4024 if (linelen == -1) {
4025 if (ferror(infile)) {
4026 err = got_error_from_errno("getline");
4032 n = fwrite(line, 1, linelen, outfile);
4034 err = got_ferror(outfile, GOT_ERR_IO);
4039 n = fwrite(line, 1, linelen, rejectfile);
4041 err = got_ferror(outfile, GOT_ERR_IO);
4048 static const struct got_error *
4049 skip_one_line(FILE *f)
4052 size_t linesize = 0;
4055 linelen = getline(&line, &linesize, f);
4056 if (linelen == -1) {
4058 return got_error_from_errno("getline");
4065 static const struct got_error *
4066 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4067 int start_old, int end_old, int start_new, int end_new,
4068 FILE *outfile, FILE *rejectfile)
4070 const struct got_error *err;
4072 /* Copy old file's lines leading up to patch. */
4073 while (!feof(f1) && *line_cur1 < start_old) {
4074 err = copy_one_line(f1, outfile, NULL);
4079 /* Skip new file's lines leading up to patch. */
4080 while (!feof(f2) && *line_cur2 < start_new) {
4082 err = copy_one_line(f2, NULL, rejectfile);
4084 err = skip_one_line(f2);
4089 /* Copy patched lines. */
4090 while (!feof(f2) && *line_cur2 <= end_new) {
4091 err = copy_one_line(f2, outfile, NULL);
4096 /* Skip over old file's replaced lines. */
4097 while (!feof(f1) && *line_cur1 <= end_old) {
4099 err = copy_one_line(f1, NULL, rejectfile);
4101 err = skip_one_line(f1);
4110 static const struct got_error *
4111 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4112 FILE *outfile, FILE *rejectfile)
4114 const struct got_error *err;
4117 /* Copy old file's lines until EOF. */
4119 err = copy_one_line(f1, outfile, NULL);
4126 /* Copy new file's lines until EOF. */
4128 err = copy_one_line(f2, NULL, rejectfile);
4138 static const struct got_error *
4139 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
4140 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
4141 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
4142 int *line_cur2, FILE *outfile, FILE *rejectfile,
4143 got_worktree_patch_cb patch_cb, void *patch_arg)
4145 const struct got_error *err = NULL;
4146 int start_old = change->cv.a;
4147 int end_old = change->cv.b;
4148 int start_new = change->cv.c;
4149 int end_new = change->cv.d;
4153 *choice = GOT_PATCH_CHOICE_NONE;
4155 hunkfile = got_opentemp();
4156 if (hunkfile == NULL)
4157 return got_error_from_errno("got_opentemp");
4162 /* XXX TODO needs error checking */
4163 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
4165 if (fseek(f1, pos1, SEEK_SET) == -1) {
4166 err = got_ferror(f1, GOT_ERR_IO);
4169 if (fseek(f2, pos2, SEEK_SET) == -1) {
4170 err = got_ferror(f1, GOT_ERR_IO);
4173 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4174 err = got_ferror(hunkfile, GOT_ERR_IO);
4178 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4179 hunkfile, n, nchanges);
4184 case GOT_PATCH_CHOICE_YES:
4185 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4186 end_old, start_new, end_new, outfile, rejectfile);
4188 case GOT_PATCH_CHOICE_NO:
4189 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4190 end_old, start_new, end_new, rejectfile, outfile);
4192 case GOT_PATCH_CHOICE_QUIT:
4195 err = got_error(GOT_ERR_PATCH_CHOICE);
4199 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4200 err = got_error_from_errno("fclose");
4204 struct revert_file_args {
4205 struct got_worktree *worktree;
4206 struct got_fileindex *fileindex;
4207 got_worktree_checkout_cb progress_cb;
4209 got_worktree_patch_cb patch_cb;
4211 struct got_repository *repo;
4214 static const struct got_error *
4215 create_patched_content(char **path_outfile, int reverse_patch,
4216 struct got_object_id *blob_id, const char *path2,
4217 int dirfd2, const char *de_name2,
4218 const char *relpath, struct got_repository *repo,
4219 got_worktree_patch_cb patch_cb, void *patch_arg)
4221 const struct got_error *err;
4222 struct got_blob_object *blob = NULL;
4223 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4225 char link_target[PATH_MAX];
4226 ssize_t link_len = 0;
4227 char *path1 = NULL, *id_str = NULL;
4228 struct stat sb1, sb2;
4229 struct got_diff_changes *changes = NULL;
4230 struct got_diff_state *ds = NULL;
4231 struct got_diff_args *args = NULL;
4232 struct got_diff_change *change;
4233 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
4236 *path_outfile = NULL;
4238 err = got_object_id_str(&id_str, blob_id);
4243 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4245 if (errno != ELOOP) {
4246 err = got_error_from_errno2("openat", path2);
4249 link_len = readlinkat(dirfd2, de_name2,
4250 link_target, sizeof(link_target));
4252 return got_error_from_errno2("readlinkat", path2);
4253 sb2.st_mode = S_IFLNK;
4254 sb2.st_size = link_len;
4257 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4259 if (errno != ELOOP) {
4260 err = got_error_from_errno2("open", path2);
4263 link_len = readlink(path2, link_target,
4264 sizeof(link_target));
4266 return got_error_from_errno2("readlink", path2);
4267 sb2.st_mode = S_IFLNK;
4268 sb2.st_size = link_len;
4272 if (fstat(fd2, &sb2) == -1) {
4273 err = got_error_from_errno2("fstat", path2);
4277 f2 = fdopen(fd2, "r");
4279 err = got_error_from_errno2("fdopen", path2);
4285 f2 = got_opentemp();
4287 err = got_error_from_errno2("got_opentemp", path2);
4290 n = fwrite(link_target, 1, link_len, f2);
4291 if (n != link_len) {
4292 err = got_ferror(f2, GOT_ERR_IO);
4295 if (fflush(f2) == EOF) {
4296 err = got_error_from_errno("fflush");
4302 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4306 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4310 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4314 if (stat(path1, &sb1) == -1) {
4315 err = got_error_from_errno2("stat", path1);
4319 err = got_diff_files(&changes, &ds, &args, &diff_flags,
4320 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
4324 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4328 if (fseek(f1, 0L, SEEK_SET) == -1)
4329 return got_ferror(f1, GOT_ERR_IO);
4330 if (fseek(f2, 0L, SEEK_SET) == -1)
4331 return got_ferror(f2, GOT_ERR_IO);
4332 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
4334 err = apply_or_reject_change(&choice, change, ++n,
4335 changes->nchanges, ds, args, diff_flags, relpath,
4336 f1, f2, &line_cur1, &line_cur2,
4337 reverse_patch ? NULL : outfile,
4338 reverse_patch ? outfile : NULL,
4339 patch_cb, patch_arg);
4342 if (choice == GOT_PATCH_CHOICE_YES)
4344 else if (choice == GOT_PATCH_CHOICE_QUIT)
4348 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4349 reverse_patch ? NULL : outfile,
4350 reverse_patch ? outfile : NULL);
4354 if (!S_ISLNK(sb2.st_mode)) {
4355 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4356 err = got_error_from_errno2("fchmod", path2);
4364 got_object_blob_close(blob);
4365 if (f1 && fclose(f1) == EOF && err == NULL)
4366 err = got_error_from_errno2("fclose", path1);
4367 if (f2 && fclose(f2) == EOF && err == NULL)
4368 err = got_error_from_errno2("fclose", path2);
4369 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4370 err = got_error_from_errno2("close", path2);
4371 if (outfile && fclose(outfile) == EOF && err == NULL)
4372 err = got_error_from_errno2("fclose", *path_outfile);
4373 if (path1 && unlink(path1) == -1 && err == NULL)
4374 err = got_error_from_errno2("unlink", path1);
4375 if (err || !have_content) {
4376 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4377 err = got_error_from_errno2("unlink", *path_outfile);
4378 free(*path_outfile);
4379 *path_outfile = NULL;
4383 got_diff_state_free(ds);
4387 got_diff_free_changes(changes);
4392 static const struct got_error *
4393 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4394 const char *relpath, struct got_object_id *blob_id,
4395 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4396 int dirfd, const char *de_name)
4398 struct revert_file_args *a = arg;
4399 const struct got_error *err = NULL;
4400 char *parent_path = NULL;
4401 struct got_fileindex_entry *ie;
4402 struct got_tree_object *tree = NULL;
4403 struct got_object_id *tree_id = NULL;
4404 const struct got_tree_entry *te = NULL;
4405 char *tree_path = NULL, *te_name;
4406 char *ondisk_path = NULL, *path_content = NULL;
4407 struct got_blob_object *blob = NULL;
4409 /* Reverting a staged deletion is a no-op. */
4410 if (status == GOT_STATUS_DELETE &&
4411 staged_status != GOT_STATUS_NO_CHANGE)
4414 if (status == GOT_STATUS_UNVERSIONED)
4415 return (*a->progress_cb)(a->progress_arg,
4416 GOT_STATUS_UNVERSIONED, relpath);
4418 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4420 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4422 /* Construct in-repository path of tree which contains this blob. */
4423 err = got_path_dirname(&parent_path, ie->path);
4425 if (err->code != GOT_ERR_BAD_PATH)
4427 parent_path = strdup("/");
4428 if (parent_path == NULL) {
4429 err = got_error_from_errno("strdup");
4433 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4434 tree_path = strdup(parent_path);
4435 if (tree_path == NULL) {
4436 err = got_error_from_errno("strdup");
4440 if (got_path_is_root_dir(parent_path)) {
4441 tree_path = strdup(a->worktree->path_prefix);
4442 if (tree_path == NULL) {
4443 err = got_error_from_errno("strdup");
4447 if (asprintf(&tree_path, "%s/%s",
4448 a->worktree->path_prefix, parent_path) == -1) {
4449 err = got_error_from_errno("asprintf");
4455 err = got_object_id_by_path(&tree_id, a->repo,
4456 a->worktree->base_commit_id, tree_path);
4458 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4459 (status == GOT_STATUS_ADD ||
4460 staged_status == GOT_STATUS_ADD)))
4463 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4467 err = got_path_basename(&te_name, ie->path);
4471 te = got_object_tree_find_entry(tree, te_name);
4473 if (te == NULL && status != GOT_STATUS_ADD &&
4474 staged_status != GOT_STATUS_ADD) {
4475 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4481 case GOT_STATUS_ADD:
4483 int choice = GOT_PATCH_CHOICE_NONE;
4484 err = (*a->patch_cb)(&choice, a->patch_arg,
4485 status, ie->path, NULL, 1, 1);
4488 if (choice != GOT_PATCH_CHOICE_YES)
4491 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4495 got_fileindex_entry_remove(a->fileindex, ie);
4497 case GOT_STATUS_DELETE:
4499 int choice = GOT_PATCH_CHOICE_NONE;
4500 err = (*a->patch_cb)(&choice, a->patch_arg,
4501 status, ie->path, NULL, 1, 1);
4504 if (choice != GOT_PATCH_CHOICE_YES)
4508 case GOT_STATUS_MODIFY:
4509 case GOT_STATUS_MODE_CHANGE:
4510 case GOT_STATUS_CONFLICT:
4511 case GOT_STATUS_MISSING: {
4512 struct got_object_id id;
4513 if (staged_status == GOT_STATUS_ADD ||
4514 staged_status == GOT_STATUS_MODIFY) {
4515 memcpy(id.sha1, ie->staged_blob_sha1,
4516 SHA1_DIGEST_LENGTH);
4518 memcpy(id.sha1, ie->blob_sha1,
4519 SHA1_DIGEST_LENGTH);
4520 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4524 if (asprintf(&ondisk_path, "%s/%s",
4525 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4526 err = got_error_from_errno("asprintf");
4530 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4531 status == GOT_STATUS_CONFLICT)) {
4532 int is_bad_symlink = 0;
4533 err = create_patched_content(&path_content, 1, &id,
4534 ondisk_path, dirfd, de_name, ie->path, a->repo,
4535 a->patch_cb, a->patch_arg);
4536 if (err || path_content == NULL)
4538 if (te && S_ISLNK(te->mode)) {
4539 if (unlink(path_content) == -1) {
4540 err = got_error_from_errno2("unlink",
4544 err = install_symlink(&is_bad_symlink,
4545 a->worktree, ondisk_path, ie->path,
4546 blob, 0, 1, 0, a->repo,
4547 a->progress_cb, a->progress_arg);
4549 if (rename(path_content, ondisk_path) == -1) {
4550 err = got_error_from_errno3("rename",
4551 path_content, ondisk_path);
4556 int is_bad_symlink = 0;
4557 if (te && S_ISLNK(te->mode)) {
4558 err = install_symlink(&is_bad_symlink,
4559 a->worktree, ondisk_path, ie->path,
4560 blob, 0, 1, 0, a->repo,
4561 a->progress_cb, a->progress_arg);
4563 err = install_blob(a->worktree, ondisk_path,
4565 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4566 got_fileindex_perms_to_st(ie), blob,
4567 0, 1, 0, 0, a->repo,
4568 a->progress_cb, a->progress_arg);
4572 if (status == GOT_STATUS_DELETE ||
4573 status == GOT_STATUS_MODE_CHANGE) {
4574 err = got_fileindex_entry_update(ie,
4575 ondisk_path, blob->id.sha1,
4576 a->worktree->base_commit_id->sha1, 1);
4580 if (is_bad_symlink) {
4581 got_fileindex_entry_filetype_set(ie,
4582 GOT_FILEIDX_MODE_BAD_SYMLINK);
4596 got_object_blob_close(blob);
4598 got_object_tree_close(tree);
4603 const struct got_error *
4604 got_worktree_revert(struct got_worktree *worktree,
4605 struct got_pathlist_head *paths,
4606 got_worktree_checkout_cb progress_cb, void *progress_arg,
4607 got_worktree_patch_cb patch_cb, void *patch_arg,
4608 struct got_repository *repo)
4610 struct got_fileindex *fileindex = NULL;
4611 char *fileindex_path = NULL;
4612 const struct got_error *err = NULL, *unlockerr = NULL;
4613 const struct got_error *sync_err = NULL;
4614 struct got_pathlist_entry *pe;
4615 struct revert_file_args rfa;
4617 err = lock_worktree(worktree, LOCK_EX);
4621 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4625 rfa.worktree = worktree;
4626 rfa.fileindex = fileindex;
4627 rfa.progress_cb = progress_cb;
4628 rfa.progress_arg = progress_arg;
4629 rfa.patch_cb = patch_cb;
4630 rfa.patch_arg = patch_arg;
4632 TAILQ_FOREACH(pe, paths, entry) {
4633 err = worktree_status(worktree, pe->path, fileindex, repo,
4634 revert_file, &rfa, NULL, NULL, 0, 0);
4638 sync_err = sync_fileindex(fileindex, fileindex_path);
4639 if (sync_err && err == NULL)
4642 free(fileindex_path);
4644 got_fileindex_free(fileindex);
4645 unlockerr = lock_worktree(worktree, LOCK_SH);
4646 if (unlockerr && err == NULL)
4652 free_commitable(struct got_commitable *ct)
4655 free(ct->in_repo_path);
4656 free(ct->ondisk_path);
4658 free(ct->base_blob_id);
4659 free(ct->staged_blob_id);
4660 free(ct->base_commit_id);
4664 struct collect_commitables_arg {
4665 struct got_pathlist_head *commitable_paths;
4666 struct got_repository *repo;
4667 struct got_worktree *worktree;
4668 struct got_fileindex *fileindex;
4669 int have_staged_files;
4670 int allow_bad_symlinks;
4673 static const struct got_error *
4674 collect_commitables(void *arg, unsigned char status,
4675 unsigned char staged_status, const char *relpath,
4676 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4677 struct got_object_id *commit_id, int dirfd, const char *de_name)
4679 struct collect_commitables_arg *a = arg;
4680 const struct got_error *err = NULL;
4681 struct got_commitable *ct = NULL;
4682 struct got_pathlist_entry *new = NULL;
4683 char *parent_path = NULL, *path = NULL;
4686 if (a->have_staged_files) {
4687 if (staged_status != GOT_STATUS_MODIFY &&
4688 staged_status != GOT_STATUS_ADD &&
4689 staged_status != GOT_STATUS_DELETE)
4692 if (status == GOT_STATUS_CONFLICT)
4693 return got_error(GOT_ERR_COMMIT_CONFLICT);
4695 if (status != GOT_STATUS_MODIFY &&
4696 status != GOT_STATUS_MODE_CHANGE &&
4697 status != GOT_STATUS_ADD &&
4698 status != GOT_STATUS_DELETE)
4702 if (asprintf(&path, "/%s", relpath) == -1) {
4703 err = got_error_from_errno("asprintf");
4706 if (strcmp(path, "/") == 0) {
4707 parent_path = strdup("");
4708 if (parent_path == NULL)
4709 return got_error_from_errno("strdup");
4711 err = got_path_dirname(&parent_path, path);
4716 ct = calloc(1, sizeof(*ct));
4718 err = got_error_from_errno("calloc");
4722 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4724 err = got_error_from_errno("asprintf");
4728 if (staged_status == GOT_STATUS_ADD ||
4729 staged_status == GOT_STATUS_MODIFY) {
4730 struct got_fileindex_entry *ie;
4731 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4732 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4733 case GOT_FILEIDX_MODE_REGULAR_FILE:
4734 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4737 case GOT_FILEIDX_MODE_SYMLINK:
4741 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4744 ct->mode |= got_fileindex_entry_perms_get(ie);
4745 } else if (status != GOT_STATUS_DELETE &&
4746 staged_status != GOT_STATUS_DELETE) {
4748 if (fstatat(dirfd, de_name, &sb,
4749 AT_SYMLINK_NOFOLLOW) == -1) {
4750 err = got_error_from_errno2("fstatat",
4754 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4755 err = got_error_from_errno2("lstat", ct->ondisk_path);
4758 ct->mode = sb.st_mode;
4761 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4762 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4764 err = got_error_from_errno("asprintf");
4768 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4769 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4771 char target_path[PATH_MAX];
4773 target_len = readlink(ct->ondisk_path, target_path,
4774 sizeof(target_path));
4775 if (target_len == -1) {
4776 err = got_error_from_errno2("readlink",
4780 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4781 target_len, ct->ondisk_path, a->worktree->root_path);
4784 if (is_bad_symlink) {
4785 err = got_error_path(ct->ondisk_path,
4786 GOT_ERR_BAD_SYMLINK);
4792 ct->status = status;
4793 ct->staged_status = staged_status;
4794 ct->blob_id = NULL; /* will be filled in when blob gets created */
4795 if (ct->status != GOT_STATUS_ADD &&
4796 ct->staged_status != GOT_STATUS_ADD) {
4797 ct->base_blob_id = got_object_id_dup(blob_id);
4798 if (ct->base_blob_id == NULL) {
4799 err = got_error_from_errno("got_object_id_dup");
4802 ct->base_commit_id = got_object_id_dup(commit_id);
4803 if (ct->base_commit_id == NULL) {
4804 err = got_error_from_errno("got_object_id_dup");
4808 if (ct->staged_status == GOT_STATUS_ADD ||
4809 ct->staged_status == GOT_STATUS_MODIFY) {
4810 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4811 if (ct->staged_blob_id == NULL) {
4812 err = got_error_from_errno("got_object_id_dup");
4816 ct->path = strdup(path);
4817 if (ct->path == NULL) {
4818 err = got_error_from_errno("strdup");
4821 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4823 if (ct && (err || new == NULL))
4824 free_commitable(ct);
4830 static const struct got_error *write_tree(struct got_object_id **, int *,
4831 struct got_tree_object *, const char *, struct got_pathlist_head *,
4832 got_worktree_status_cb status_cb, void *status_arg,
4833 struct got_repository *);
4835 static const struct got_error *
4836 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4837 struct got_tree_entry *te, const char *parent_path,
4838 struct got_pathlist_head *commitable_paths,
4839 got_worktree_status_cb status_cb, void *status_arg,
4840 struct got_repository *repo)
4842 const struct got_error *err = NULL;
4843 struct got_tree_object *subtree;
4846 if (asprintf(&subpath, "%s%s%s", parent_path,
4847 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4848 return got_error_from_errno("asprintf");
4850 err = got_object_open_as_tree(&subtree, repo, &te->id);
4854 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4855 commitable_paths, status_cb, status_arg, repo);
4856 got_object_tree_close(subtree);
4861 static const struct got_error *
4862 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4864 const struct got_error *err = NULL;
4865 char *ct_parent_path = NULL;
4869 if (strchr(ct->in_repo_path, '/') == NULL) {
4870 *match = got_path_is_root_dir(path);
4874 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4877 *match = (strcmp(path, ct_parent_path) == 0);
4878 free(ct_parent_path);
4883 get_ct_file_mode(struct got_commitable *ct)
4885 if (S_ISLNK(ct->mode))
4888 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4891 static const struct got_error *
4892 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4893 struct got_tree_entry *te, struct got_commitable *ct)
4895 const struct got_error *err = NULL;
4899 err = got_object_tree_entry_dup(new_te, te);
4903 (*new_te)->mode = get_ct_file_mode(ct);
4905 if (ct->staged_status == GOT_STATUS_MODIFY)
4906 memcpy(&(*new_te)->id, ct->staged_blob_id,
4907 sizeof((*new_te)->id));
4909 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4911 if (err && *new_te) {
4918 static const struct got_error *
4919 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4920 struct got_commitable *ct)
4922 const struct got_error *err = NULL;
4923 char *ct_name = NULL;
4927 *new_te = calloc(1, sizeof(**new_te));
4928 if (*new_te == NULL)
4929 return got_error_from_errno("calloc");
4931 err = got_path_basename(&ct_name, ct->path);
4934 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4935 sizeof((*new_te)->name)) {
4936 err = got_error(GOT_ERR_NO_SPACE);
4940 (*new_te)->mode = get_ct_file_mode(ct);
4942 if (ct->staged_status == GOT_STATUS_ADD)
4943 memcpy(&(*new_te)->id, ct->staged_blob_id,
4944 sizeof((*new_te)->id));
4946 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4949 if (err && *new_te) {
4956 static const struct got_error *
4957 insert_tree_entry(struct got_tree_entry *new_te,
4958 struct got_pathlist_head *paths)
4960 const struct got_error *err = NULL;
4961 struct got_pathlist_entry *new_pe;
4963 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4967 return got_error(GOT_ERR_TREE_DUP_ENTRY);
4971 static const struct got_error *
4972 report_ct_status(struct got_commitable *ct,
4973 got_worktree_status_cb status_cb, void *status_arg)
4975 const char *ct_path = ct->path;
4976 unsigned char status;
4978 while (ct_path[0] == '/')
4981 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4982 status = ct->staged_status;
4984 status = ct->status;
4986 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4987 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4990 static const struct got_error *
4991 match_modified_subtree(int *modified, struct got_tree_entry *te,
4992 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4994 const struct got_error *err = NULL;
4995 struct got_pathlist_entry *pe;
5000 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5001 got_path_is_root_dir(base_tree_path) ? "" : "/",
5003 return got_error_from_errno("asprintf");
5005 TAILQ_FOREACH(pe, commitable_paths, entry) {
5006 struct got_commitable *ct = pe->data;
5007 *modified = got_path_is_child(ct->in_repo_path, te_path,
5017 static const struct got_error *
5018 match_deleted_or_modified_ct(struct got_commitable **ctp,
5019 struct got_tree_entry *te, const char *base_tree_path,
5020 struct got_pathlist_head *commitable_paths)
5022 const struct got_error *err = NULL;
5023 struct got_pathlist_entry *pe;
5027 TAILQ_FOREACH(pe, commitable_paths, entry) {
5028 struct got_commitable *ct = pe->data;
5029 char *ct_name = NULL;
5032 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5033 if (ct->status != GOT_STATUS_MODIFY &&
5034 ct->status != GOT_STATUS_MODE_CHANGE &&
5035 ct->status != GOT_STATUS_DELETE)
5038 if (ct->staged_status != GOT_STATUS_MODIFY &&
5039 ct->staged_status != GOT_STATUS_DELETE)
5043 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5046 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5052 err = got_path_basename(&ct_name, pe->path);
5056 if (strcmp(te->name, ct_name) != 0) {
5069 static const struct got_error *
5070 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5071 const char *child_path, const char *path_base_tree,
5072 struct got_pathlist_head *commitable_paths,
5073 got_worktree_status_cb status_cb, void *status_arg,
5074 struct got_repository *repo)
5076 const struct got_error *err = NULL;
5077 struct got_tree_entry *new_te;
5079 struct got_object_id *id = NULL;
5084 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5085 got_path_is_root_dir(path_base_tree) ? "" : "/",
5087 return got_error_from_errno("asprintf");
5089 new_te = calloc(1, sizeof(*new_te));
5091 return got_error_from_errno("calloc");
5092 new_te->mode = S_IFDIR;
5094 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5095 sizeof(new_te->name)) {
5096 err = got_error(GOT_ERR_NO_SPACE);
5099 err = write_tree(&id, &nentries, NULL, subtree_path,
5100 commitable_paths, status_cb, status_arg, repo);
5105 memcpy(&new_te->id, id, sizeof(new_te->id));
5114 static const struct got_error *
5115 write_tree(struct got_object_id **new_tree_id, int *nentries,
5116 struct got_tree_object *base_tree, const char *path_base_tree,
5117 struct got_pathlist_head *commitable_paths,
5118 got_worktree_status_cb status_cb, void *status_arg,
5119 struct got_repository *repo)
5121 const struct got_error *err = NULL;
5122 struct got_pathlist_head paths;
5123 struct got_tree_entry *te, *new_te = NULL;
5124 struct got_pathlist_entry *pe;
5129 /* Insert, and recurse into, newly added entries first. */
5130 TAILQ_FOREACH(pe, commitable_paths, entry) {
5131 struct got_commitable *ct = pe->data;
5132 char *child_path = NULL, *slash;
5134 if ((ct->status != GOT_STATUS_ADD &&
5135 ct->staged_status != GOT_STATUS_ADD) ||
5136 (ct->flags & GOT_COMMITABLE_ADDED))
5139 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5140 strlen(path_base_tree)))
5143 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5148 slash = strchr(child_path, '/');
5149 if (slash == NULL) {
5150 err = alloc_added_blob_tree_entry(&new_te, ct);
5153 err = report_ct_status(ct, status_cb, status_arg);
5156 ct->flags |= GOT_COMMITABLE_ADDED;
5157 err = insert_tree_entry(new_te, &paths);
5162 *slash = '\0'; /* trim trailing path components */
5163 if (base_tree == NULL ||
5164 got_object_tree_find_entry(base_tree, child_path)
5166 err = make_subtree_for_added_blob(&new_te,
5167 child_path, path_base_tree,
5168 commitable_paths, status_cb, status_arg,
5172 err = insert_tree_entry(new_te, &paths);
5181 int i, nbase_entries;
5182 /* Handle modified and deleted entries. */
5183 nbase_entries = got_object_tree_get_nentries(base_tree);
5184 for (i = 0; i < nbase_entries; i++) {
5185 struct got_commitable *ct = NULL;
5187 te = got_object_tree_get_entry(base_tree, i);
5188 if (got_object_tree_entry_is_submodule(te)) {
5189 /* Entry is a submodule; just copy it. */
5190 err = got_object_tree_entry_dup(&new_te, te);
5193 err = insert_tree_entry(new_te, &paths);
5200 if (S_ISDIR(te->mode)) {
5202 err = got_object_tree_entry_dup(&new_te, te);
5205 err = match_modified_subtree(&modified, te,
5206 path_base_tree, commitable_paths);
5209 /* Avoid recursion into unmodified subtrees. */
5211 struct got_object_id *new_id;
5213 err = write_subtree(&new_id,
5215 path_base_tree, commitable_paths,
5216 status_cb, status_arg, repo);
5219 if (nsubentries == 0) {
5220 /* All entries were deleted. */
5224 memcpy(&new_te->id, new_id,
5225 sizeof(new_te->id));
5228 err = insert_tree_entry(new_te, &paths);
5235 err = match_deleted_or_modified_ct(&ct, te,
5236 path_base_tree, commitable_paths);
5240 /* NB: Deleted entries get dropped here. */
5241 if (ct->status == GOT_STATUS_MODIFY ||
5242 ct->status == GOT_STATUS_MODE_CHANGE ||
5243 ct->staged_status == GOT_STATUS_MODIFY) {
5244 err = alloc_modified_blob_tree_entry(
5248 err = insert_tree_entry(new_te, &paths);
5253 err = report_ct_status(ct, status_cb,
5258 /* Entry is unchanged; just copy it. */
5259 err = got_object_tree_entry_dup(&new_te, te);
5262 err = insert_tree_entry(new_te, &paths);
5270 /* Write new list of entries; deleted entries have been dropped. */
5271 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5273 got_pathlist_free(&paths);
5277 static const struct got_error *
5278 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
5279 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
5280 int have_staged_files)
5282 const struct got_error *err = NULL;
5283 struct got_pathlist_entry *pe;
5285 TAILQ_FOREACH(pe, commitable_paths, entry) {
5286 struct got_fileindex_entry *ie;
5287 struct got_commitable *ct = pe->data;
5289 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5291 if (ct->status == GOT_STATUS_DELETE ||
5292 ct->staged_status == GOT_STATUS_DELETE) {
5293 got_fileindex_entry_remove(fileindex, ie);
5294 } else if (ct->staged_status == GOT_STATUS_ADD ||
5295 ct->staged_status == GOT_STATUS_MODIFY) {
5296 got_fileindex_entry_stage_set(ie,
5297 GOT_FILEIDX_STAGE_NONE);
5298 got_fileindex_entry_staged_filetype_set(ie, 0);
5299 err = got_fileindex_entry_update(ie,
5300 ct->ondisk_path, ct->staged_blob_id->sha1,
5301 new_base_commit_id->sha1,
5302 !have_staged_files);
5304 err = got_fileindex_entry_update(ie,
5305 ct->ondisk_path, ct->blob_id->sha1,
5306 new_base_commit_id->sha1,
5307 !have_staged_files);
5309 err = got_fileindex_entry_alloc(&ie, pe->path);
5312 err = got_fileindex_entry_update(ie, ct->ondisk_path,
5313 ct->blob_id->sha1, new_base_commit_id->sha1, 1);
5315 got_fileindex_entry_free(ie);
5318 err = got_fileindex_entry_add(fileindex, ie);
5320 got_fileindex_entry_free(ie);
5329 static const struct got_error *
5330 check_out_of_date(const char *in_repo_path, unsigned char status,
5331 unsigned char staged_status, struct got_object_id *base_blob_id,
5332 struct got_object_id *base_commit_id,
5333 struct got_object_id *head_commit_id, struct got_repository *repo,
5336 const struct got_error *err = NULL;
5337 struct got_object_id *id = NULL;
5339 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5340 /* Trivial case: base commit == head commit */
5341 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5344 * Ensure file content which local changes were based
5345 * on matches file content in the branch head.
5347 err = got_object_id_by_path(&id, repo, head_commit_id,
5350 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5351 err = got_error(ood_errcode);
5353 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5354 err = got_error(ood_errcode);
5356 /* Require that added files don't exist in the branch head. */
5357 err = got_object_id_by_path(&id, repo, head_commit_id,
5359 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5361 err = id ? got_error(ood_errcode) : NULL;
5368 const struct got_error *
5369 commit_worktree(struct got_object_id **new_commit_id,
5370 struct got_pathlist_head *commitable_paths,
5371 struct got_object_id *head_commit_id, struct got_worktree *worktree,
5372 const char *author, const char *committer,
5373 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5374 got_worktree_status_cb status_cb, void *status_arg,
5375 struct got_repository *repo)
5377 const struct got_error *err = NULL, *unlockerr = NULL;
5378 struct got_pathlist_entry *pe;
5379 const char *head_ref_name = NULL;
5380 struct got_commit_object *head_commit = NULL;
5381 struct got_reference *head_ref2 = NULL;
5382 struct got_object_id *head_commit_id2 = NULL;
5383 struct got_tree_object *head_tree = NULL;
5384 struct got_object_id *new_tree_id = NULL;
5386 struct got_object_id_queue parent_ids;
5387 struct got_object_qid *pid = NULL;
5388 char *logmsg = NULL;
5390 *new_commit_id = NULL;
5392 SIMPLEQ_INIT(&parent_ids);
5394 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5398 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5402 if (commit_msg_cb != NULL) {
5403 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5408 if (logmsg == NULL || strlen(logmsg) == 0) {
5409 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5413 /* Create blobs from added and modified files and record their IDs. */
5414 TAILQ_FOREACH(pe, commitable_paths, entry) {
5415 struct got_commitable *ct = pe->data;
5418 /* Blobs for staged files already exist. */
5419 if (ct->staged_status == GOT_STATUS_ADD ||
5420 ct->staged_status == GOT_STATUS_MODIFY)
5423 if (ct->status != GOT_STATUS_ADD &&
5424 ct->status != GOT_STATUS_MODIFY &&
5425 ct->status != GOT_STATUS_MODE_CHANGE)
5428 if (asprintf(&ondisk_path, "%s/%s",
5429 worktree->root_path, pe->path) == -1) {
5430 err = got_error_from_errno("asprintf");
5433 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5439 /* Recursively write new tree objects. */
5440 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5441 commitable_paths, status_cb, status_arg, repo);
5445 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5448 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5449 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5450 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5451 got_object_qid_free(pid);
5457 /* Check if a concurrent commit to our branch has occurred. */
5458 head_ref_name = got_worktree_get_head_ref_name(worktree);
5459 if (head_ref_name == NULL) {
5460 err = got_error_from_errno("got_worktree_get_head_ref_name");
5463 /* Lock the reference here to prevent concurrent modification. */
5464 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5467 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5470 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5471 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5474 /* Update branch head in repository. */
5475 err = got_ref_change_ref(head_ref2, *new_commit_id);
5478 err = got_ref_write(head_ref2, repo);
5482 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5486 err = ref_base_commit(worktree, repo);
5491 got_object_tree_close(head_tree);
5493 got_object_commit_close(head_commit);
5494 free(head_commit_id2);
5496 unlockerr = got_ref_unlock(head_ref2);
5497 if (unlockerr && err == NULL)
5499 got_ref_close(head_ref2);
5504 static const struct got_error *
5505 check_path_is_commitable(const char *path,
5506 struct got_pathlist_head *commitable_paths)
5508 struct got_pathlist_entry *cpe = NULL;
5509 size_t path_len = strlen(path);
5511 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5512 struct got_commitable *ct = cpe->data;
5513 const char *ct_path = ct->path;
5515 while (ct_path[0] == '/')
5518 if (strcmp(path, ct_path) == 0 ||
5519 got_path_is_child(ct_path, path, path_len))
5524 return got_error_path(path, GOT_ERR_BAD_PATH);
5529 static const struct got_error *
5530 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5532 int *have_staged_files = arg;
5534 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5535 *have_staged_files = 1;
5536 return got_error(GOT_ERR_CANCELLED);
5542 static const struct got_error *
5543 check_non_staged_files(struct got_fileindex *fileindex,
5544 struct got_pathlist_head *paths)
5546 struct got_pathlist_entry *pe;
5547 struct got_fileindex_entry *ie;
5549 TAILQ_FOREACH(pe, paths, entry) {
5550 if (pe->path[0] == '\0')
5552 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5554 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5555 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5556 return got_error_path(pe->path,
5557 GOT_ERR_FILE_NOT_STAGED);
5563 const struct got_error *
5564 got_worktree_commit(struct got_object_id **new_commit_id,
5565 struct got_worktree *worktree, struct got_pathlist_head *paths,
5566 const char *author, const char *committer, int allow_bad_symlinks,
5567 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5568 got_worktree_status_cb status_cb, void *status_arg,
5569 struct got_repository *repo)
5571 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5572 struct got_fileindex *fileindex = NULL;
5573 char *fileindex_path = NULL;
5574 struct got_pathlist_head commitable_paths;
5575 struct collect_commitables_arg cc_arg;
5576 struct got_pathlist_entry *pe;
5577 struct got_reference *head_ref = NULL;
5578 struct got_object_id *head_commit_id = NULL;
5579 int have_staged_files = 0;
5581 *new_commit_id = NULL;
5583 TAILQ_INIT(&commitable_paths);
5585 err = lock_worktree(worktree, LOCK_EX);
5589 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5593 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5597 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5601 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5602 &have_staged_files);
5603 if (err && err->code != GOT_ERR_CANCELLED)
5605 if (have_staged_files) {
5606 err = check_non_staged_files(fileindex, paths);
5611 cc_arg.commitable_paths = &commitable_paths;
5612 cc_arg.worktree = worktree;
5613 cc_arg.fileindex = fileindex;
5615 cc_arg.have_staged_files = have_staged_files;
5616 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5617 TAILQ_FOREACH(pe, paths, entry) {
5618 err = worktree_status(worktree, pe->path, fileindex, repo,
5619 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5624 if (TAILQ_EMPTY(&commitable_paths)) {
5625 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5629 TAILQ_FOREACH(pe, paths, entry) {
5630 err = check_path_is_commitable(pe->path, &commitable_paths);
5635 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5636 struct got_commitable *ct = pe->data;
5637 const char *ct_path = ct->in_repo_path;
5639 while (ct_path[0] == '/')
5641 err = check_out_of_date(ct_path, ct->status,
5642 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5643 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5649 err = commit_worktree(new_commit_id, &commitable_paths,
5650 head_commit_id, worktree, author, committer,
5651 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5655 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5656 fileindex, have_staged_files);
5657 sync_err = sync_fileindex(fileindex, fileindex_path);
5658 if (sync_err && err == NULL)
5662 got_fileindex_free(fileindex);
5663 free(fileindex_path);
5664 unlockerr = lock_worktree(worktree, LOCK_SH);
5665 if (unlockerr && err == NULL)
5667 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5668 struct got_commitable *ct = pe->data;
5669 free_commitable(ct);
5671 got_pathlist_free(&commitable_paths);
5676 got_commitable_get_path(struct got_commitable *ct)
5682 got_commitable_get_status(struct got_commitable *ct)
5687 struct check_rebase_ok_arg {
5688 struct got_worktree *worktree;
5689 struct got_repository *repo;
5692 static const struct got_error *
5693 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5695 const struct got_error *err = NULL;
5696 struct check_rebase_ok_arg *a = arg;
5697 unsigned char status;
5701 /* Reject rebase of a work tree with mixed base commits. */
5702 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5703 SHA1_DIGEST_LENGTH))
5704 return got_error(GOT_ERR_MIXED_COMMITS);
5706 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5708 return got_error_from_errno("asprintf");
5710 /* Reject rebase of a work tree with modified or staged files. */
5711 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5716 if (status != GOT_STATUS_NO_CHANGE)
5717 return got_error(GOT_ERR_MODIFIED);
5718 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5719 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5724 const struct got_error *
5725 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5726 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5727 struct got_worktree *worktree, struct got_reference *branch,
5728 struct got_repository *repo)
5730 const struct got_error *err = NULL;
5731 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5732 char *branch_ref_name = NULL;
5733 char *fileindex_path = NULL;
5734 struct check_rebase_ok_arg ok_arg;
5735 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5736 struct got_object_id *wt_branch_tip = NULL;
5738 *new_base_branch_ref = NULL;
5742 err = lock_worktree(worktree, LOCK_EX);
5746 err = open_fileindex(fileindex, &fileindex_path, worktree);
5750 ok_arg.worktree = worktree;
5752 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5757 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5761 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5765 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5769 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5774 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5777 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5778 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5782 err = got_ref_alloc_symref(new_base_branch_ref,
5783 new_base_branch_ref_name, wt_branch);
5786 err = got_ref_write(*new_base_branch_ref, repo);
5790 /* TODO Lock original branch's ref while rebasing? */
5792 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5796 err = got_ref_write(branch_ref, repo);
5800 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5801 worktree->base_commit_id);
5804 err = got_ref_write(*tmp_branch, repo);
5808 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5812 free(fileindex_path);
5813 free(tmp_branch_name);
5814 free(new_base_branch_ref_name);
5815 free(branch_ref_name);
5817 got_ref_close(branch_ref);
5819 got_ref_close(wt_branch);
5820 free(wt_branch_tip);
5822 if (*new_base_branch_ref) {
5823 got_ref_close(*new_base_branch_ref);
5824 *new_base_branch_ref = NULL;
5827 got_ref_close(*tmp_branch);
5831 got_fileindex_free(*fileindex);
5834 lock_worktree(worktree, LOCK_SH);
5839 const struct got_error *
5840 got_worktree_rebase_continue(struct got_object_id **commit_id,
5841 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5842 struct got_reference **branch, struct got_fileindex **fileindex,
5843 struct got_worktree *worktree, struct got_repository *repo)
5845 const struct got_error *err;
5846 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5847 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5848 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5849 char *fileindex_path = NULL;
5850 int have_staged_files = 0;
5853 *new_base_branch = NULL;
5858 err = lock_worktree(worktree, LOCK_EX);
5862 err = open_fileindex(fileindex, &fileindex_path, worktree);
5866 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5867 &have_staged_files);
5868 if (err && err->code != GOT_ERR_CANCELLED)
5870 if (have_staged_files) {
5871 err = got_error(GOT_ERR_STAGED_PATHS);
5875 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5879 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5883 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5887 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5891 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5895 err = got_ref_open(branch, repo,
5896 got_ref_get_symref_target(branch_ref), 0);
5900 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5904 err = got_ref_resolve(commit_id, repo, commit_ref);
5908 err = got_ref_open(new_base_branch, repo,
5909 new_base_branch_ref_name, 0);
5913 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5917 free(commit_ref_name);
5918 free(branch_ref_name);
5919 free(fileindex_path);
5921 got_ref_close(commit_ref);
5923 got_ref_close(branch_ref);
5928 got_ref_close(*tmp_branch);
5931 if (*new_base_branch) {
5932 got_ref_close(*new_base_branch);
5933 *new_base_branch = NULL;
5936 got_ref_close(*branch);
5940 got_fileindex_free(*fileindex);
5943 lock_worktree(worktree, LOCK_SH);
5948 const struct got_error *
5949 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5951 const struct got_error *err;
5952 char *tmp_branch_name = NULL;
5954 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5958 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5959 free(tmp_branch_name);
5963 static const struct got_error *
5964 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5965 char **logmsg, void *arg)
5971 static const struct got_error *
5972 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5973 const char *path, struct got_object_id *blob_id,
5974 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5975 int dirfd, const char *de_name)
5980 struct collect_merged_paths_arg {
5981 got_worktree_checkout_cb progress_cb;
5983 struct got_pathlist_head *merged_paths;
5986 static const struct got_error *
5987 collect_merged_paths(void *arg, unsigned char status, const char *path)
5989 const struct got_error *err;
5990 struct collect_merged_paths_arg *a = arg;
5992 struct got_pathlist_entry *new;
5994 err = (*a->progress_cb)(a->progress_arg, status, path);
5998 if (status != GOT_STATUS_MERGE &&
5999 status != GOT_STATUS_ADD &&
6000 status != GOT_STATUS_DELETE &&
6001 status != GOT_STATUS_CONFLICT)
6006 return got_error_from_errno("strdup");
6008 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6009 if (err || new == NULL)
6015 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6017 struct got_pathlist_entry *pe;
6019 TAILQ_FOREACH(pe, merged_paths, entry)
6020 free((char *)pe->path);
6022 got_pathlist_free(merged_paths);
6025 static const struct got_error *
6026 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6027 int is_rebase, struct got_repository *repo)
6029 const struct got_error *err;
6030 struct got_reference *commit_ref = NULL;
6032 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6034 if (err->code != GOT_ERR_NOT_REF)
6036 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6039 err = got_ref_write(commit_ref, repo);
6042 } else if (is_rebase) {
6043 struct got_object_id *stored_id;
6046 err = got_ref_resolve(&stored_id, repo, commit_ref);
6049 cmp = got_object_id_cmp(commit_id, stored_id);
6052 err = got_error(GOT_ERR_REBASE_COMMITID);
6058 got_ref_close(commit_ref);
6062 static const struct got_error *
6063 rebase_merge_files(struct got_pathlist_head *merged_paths,
6064 const char *commit_ref_name, struct got_worktree *worktree,
6065 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6066 struct got_object_id *commit_id, struct got_repository *repo,
6067 got_worktree_checkout_cb progress_cb, void *progress_arg,
6068 got_cancel_cb cancel_cb, void *cancel_arg)
6070 const struct got_error *err;
6071 struct got_reference *commit_ref = NULL;
6072 struct collect_merged_paths_arg cmp_arg;
6073 char *fileindex_path;
6075 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6077 err = get_fileindex_path(&fileindex_path, worktree);
6081 cmp_arg.progress_cb = progress_cb;
6082 cmp_arg.progress_arg = progress_arg;
6083 cmp_arg.merged_paths = merged_paths;
6084 err = merge_files(worktree, fileindex, fileindex_path,
6085 parent_commit_id, commit_id, repo, collect_merged_paths,
6086 &cmp_arg, cancel_cb, cancel_arg);
6088 got_ref_close(commit_ref);
6092 const struct got_error *
6093 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6094 struct got_worktree *worktree, struct got_fileindex *fileindex,
6095 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6096 struct got_repository *repo,
6097 got_worktree_checkout_cb progress_cb, void *progress_arg,
6098 got_cancel_cb cancel_cb, void *cancel_arg)
6100 const struct got_error *err;
6101 char *commit_ref_name;
6103 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6107 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6111 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6112 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6113 progress_arg, cancel_cb, cancel_arg);
6115 free(commit_ref_name);
6119 const struct got_error *
6120 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6121 struct got_worktree *worktree, struct got_fileindex *fileindex,
6122 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6123 struct got_repository *repo,
6124 got_worktree_checkout_cb progress_cb, void *progress_arg,
6125 got_cancel_cb cancel_cb, void *cancel_arg)
6127 const struct got_error *err;
6128 char *commit_ref_name;
6130 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6134 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6138 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6139 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6140 progress_arg, cancel_cb, cancel_arg);
6142 free(commit_ref_name);
6146 static const struct got_error *
6147 rebase_commit(struct got_object_id **new_commit_id,
6148 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6149 struct got_worktree *worktree, struct got_fileindex *fileindex,
6150 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6151 const char *new_logmsg, struct got_repository *repo)
6153 const struct got_error *err, *sync_err;
6154 struct got_pathlist_head commitable_paths;
6155 struct collect_commitables_arg cc_arg;
6156 char *fileindex_path = NULL;
6157 struct got_reference *head_ref = NULL;
6158 struct got_object_id *head_commit_id = NULL;
6159 char *logmsg = NULL;
6161 TAILQ_INIT(&commitable_paths);
6162 *new_commit_id = NULL;
6164 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6166 err = get_fileindex_path(&fileindex_path, worktree);
6170 cc_arg.commitable_paths = &commitable_paths;
6171 cc_arg.worktree = worktree;
6173 cc_arg.have_staged_files = 0;
6175 * If possible get the status of individual files directly to
6176 * avoid crawling the entire work tree once per rebased commit.
6177 * TODO: Ideally, merged_paths would contain a list of commitables
6178 * we could use so we could skip worktree_status() entirely.
6181 struct got_pathlist_entry *pe;
6182 TAILQ_FOREACH(pe, merged_paths, entry) {
6183 err = worktree_status(worktree, pe->path, fileindex,
6184 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6190 err = worktree_status(worktree, "", fileindex, repo,
6191 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6196 if (TAILQ_EMPTY(&commitable_paths)) {
6197 /* No-op change; commit will be elided. */
6198 err = got_ref_delete(commit_ref, repo);
6201 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6205 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6209 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6214 logmsg = strdup(new_logmsg);
6215 if (logmsg == NULL) {
6216 err = got_error_from_errno("strdup");
6220 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6225 /* NB: commit_worktree will call free(logmsg) */
6226 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6227 worktree, got_object_commit_get_author(orig_commit),
6228 got_object_commit_get_committer(orig_commit),
6229 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6233 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6237 err = got_ref_delete(commit_ref, repo);
6241 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
6243 sync_err = sync_fileindex(fileindex, fileindex_path);
6244 if (sync_err && err == NULL)
6247 free(fileindex_path);
6248 free(head_commit_id);
6250 got_ref_close(head_ref);
6252 free(*new_commit_id);
6253 *new_commit_id = NULL;
6258 const struct got_error *
6259 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6260 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6261 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6262 struct got_commit_object *orig_commit,
6263 struct got_object_id *orig_commit_id, struct got_repository *repo)
6265 const struct got_error *err;
6266 char *commit_ref_name;
6267 struct got_reference *commit_ref = NULL;
6268 struct got_object_id *commit_id = NULL;
6270 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6274 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6277 err = got_ref_resolve(&commit_id, repo, commit_ref);
6280 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6281 err = got_error(GOT_ERR_REBASE_COMMITID);
6285 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6286 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6289 got_ref_close(commit_ref);
6290 free(commit_ref_name);
6295 const struct got_error *
6296 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6297 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6298 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6299 struct got_commit_object *orig_commit,
6300 struct got_object_id *orig_commit_id, const char *new_logmsg,
6301 struct got_repository *repo)
6303 const struct got_error *err;
6304 char *commit_ref_name;
6305 struct got_reference *commit_ref = NULL;
6307 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6311 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6315 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6316 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6319 got_ref_close(commit_ref);
6320 free(commit_ref_name);
6324 const struct got_error *
6325 got_worktree_rebase_postpone(struct got_worktree *worktree,
6326 struct got_fileindex *fileindex)
6329 got_fileindex_free(fileindex);
6330 return lock_worktree(worktree, LOCK_SH);
6333 static const struct got_error *
6334 delete_ref(const char *name, struct got_repository *repo)
6336 const struct got_error *err;
6337 struct got_reference *ref;
6339 err = got_ref_open(&ref, repo, name, 0);
6341 if (err->code == GOT_ERR_NOT_REF)
6346 err = got_ref_delete(ref, repo);
6351 static const struct got_error *
6352 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6354 const struct got_error *err;
6355 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6356 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6358 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6361 err = delete_ref(tmp_branch_name, repo);
6365 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6368 err = delete_ref(new_base_branch_ref_name, repo);
6372 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6375 err = delete_ref(branch_ref_name, repo);
6379 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6382 err = delete_ref(commit_ref_name, repo);
6387 free(tmp_branch_name);
6388 free(new_base_branch_ref_name);
6389 free(branch_ref_name);
6390 free(commit_ref_name);
6394 const struct got_error *
6395 got_worktree_rebase_complete(struct got_worktree *worktree,
6396 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6397 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6398 struct got_repository *repo)
6400 const struct got_error *err, *unlockerr;
6401 struct got_object_id *new_head_commit_id = NULL;
6403 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6407 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6411 err = got_ref_write(rebased_branch, repo);
6415 err = got_worktree_set_head_ref(worktree, rebased_branch);
6419 err = delete_rebase_refs(worktree, repo);
6422 got_fileindex_free(fileindex);
6423 free(new_head_commit_id);
6424 unlockerr = lock_worktree(worktree, LOCK_SH);
6425 if (unlockerr && err == NULL)
6430 const struct got_error *
6431 got_worktree_rebase_abort(struct got_worktree *worktree,
6432 struct got_fileindex *fileindex, struct got_repository *repo,
6433 struct got_reference *new_base_branch,
6434 got_worktree_checkout_cb progress_cb, void *progress_arg)
6436 const struct got_error *err, *unlockerr, *sync_err;
6437 struct got_reference *resolved = NULL;
6438 struct got_object_id *commit_id = NULL;
6439 char *fileindex_path = NULL;
6440 struct revert_file_args rfa;
6441 struct got_object_id *tree_id = NULL;
6443 err = lock_worktree(worktree, LOCK_EX);
6447 err = got_ref_open(&resolved, repo,
6448 got_ref_get_symref_target(new_base_branch), 0);
6452 err = got_worktree_set_head_ref(worktree, resolved);
6457 * XXX commits to the base branch could have happened while
6458 * we were busy rebasing; should we store the original commit ID
6459 * when rebase begins and read it back here?
6461 err = got_ref_resolve(&commit_id, repo, resolved);
6465 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6469 err = got_object_id_by_path(&tree_id, repo,
6470 worktree->base_commit_id, worktree->path_prefix);
6474 err = delete_rebase_refs(worktree, repo);
6478 err = get_fileindex_path(&fileindex_path, worktree);
6482 rfa.worktree = worktree;
6483 rfa.fileindex = fileindex;
6484 rfa.progress_cb = progress_cb;
6485 rfa.progress_arg = progress_arg;
6486 rfa.patch_cb = NULL;
6487 rfa.patch_arg = NULL;
6489 err = worktree_status(worktree, "", fileindex, repo,
6490 revert_file, &rfa, NULL, NULL, 0, 0);
6494 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6495 repo, progress_cb, progress_arg, NULL, NULL);
6497 sync_err = sync_fileindex(fileindex, fileindex_path);
6498 if (sync_err && err == NULL)
6501 got_ref_close(resolved);
6505 got_fileindex_free(fileindex);
6506 free(fileindex_path);
6508 unlockerr = lock_worktree(worktree, LOCK_SH);
6509 if (unlockerr && err == NULL)
6514 const struct got_error *
6515 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6516 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6517 struct got_fileindex **fileindex, struct got_worktree *worktree,
6518 struct got_repository *repo)
6520 const struct got_error *err = NULL;
6521 char *tmp_branch_name = NULL;
6522 char *branch_ref_name = NULL;
6523 char *base_commit_ref_name = NULL;
6524 char *fileindex_path = NULL;
6525 struct check_rebase_ok_arg ok_arg;
6526 struct got_reference *wt_branch = NULL;
6527 struct got_reference *base_commit_ref = NULL;
6531 *base_commit_id = NULL;
6534 err = lock_worktree(worktree, LOCK_EX);
6538 err = open_fileindex(fileindex, &fileindex_path, worktree);
6542 ok_arg.worktree = worktree;
6544 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6549 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6553 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6557 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6562 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6567 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6571 err = got_ref_write(*branch_ref, repo);
6575 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6576 worktree->base_commit_id);
6579 err = got_ref_write(base_commit_ref, repo);
6582 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6583 if (*base_commit_id == NULL) {
6584 err = got_error_from_errno("got_object_id_dup");
6588 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6589 worktree->base_commit_id);
6592 err = got_ref_write(*tmp_branch, repo);
6596 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6600 free(fileindex_path);
6601 free(tmp_branch_name);
6602 free(branch_ref_name);
6603 free(base_commit_ref_name);
6605 got_ref_close(wt_branch);
6608 got_ref_close(*branch_ref);
6612 got_ref_close(*tmp_branch);
6615 free(*base_commit_id);
6617 got_fileindex_free(*fileindex);
6620 lock_worktree(worktree, LOCK_SH);
6625 const struct got_error *
6626 got_worktree_histedit_postpone(struct got_worktree *worktree,
6627 struct got_fileindex *fileindex)
6630 got_fileindex_free(fileindex);
6631 return lock_worktree(worktree, LOCK_SH);
6634 const struct got_error *
6635 got_worktree_histedit_in_progress(int *in_progress,
6636 struct got_worktree *worktree)
6638 const struct got_error *err;
6639 char *tmp_branch_name = NULL;
6641 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6645 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6646 free(tmp_branch_name);
6650 const struct got_error *
6651 got_worktree_histedit_continue(struct got_object_id **commit_id,
6652 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6653 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6654 struct got_worktree *worktree, struct got_repository *repo)
6656 const struct got_error *err;
6657 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6658 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6659 struct got_reference *commit_ref = NULL;
6660 struct got_reference *base_commit_ref = NULL;
6661 char *fileindex_path = NULL;
6662 int have_staged_files = 0;
6666 *base_commit_id = NULL;
6669 err = lock_worktree(worktree, LOCK_EX);
6673 err = open_fileindex(fileindex, &fileindex_path, worktree);
6677 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6678 &have_staged_files);
6679 if (err && err->code != GOT_ERR_CANCELLED)
6681 if (have_staged_files) {
6682 err = got_error(GOT_ERR_STAGED_PATHS);
6686 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6690 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6694 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6698 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6703 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6707 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6710 err = got_ref_resolve(commit_id, repo, commit_ref);
6714 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6717 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6721 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6725 free(commit_ref_name);
6726 free(branch_ref_name);
6727 free(fileindex_path);
6729 got_ref_close(commit_ref);
6730 if (base_commit_ref)
6731 got_ref_close(base_commit_ref);
6735 free(*base_commit_id);
6736 *base_commit_id = NULL;
6738 got_ref_close(*tmp_branch);
6742 got_fileindex_free(*fileindex);
6745 lock_worktree(worktree, LOCK_EX);
6750 static const struct got_error *
6751 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6753 const struct got_error *err;
6754 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6755 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6757 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6760 err = delete_ref(tmp_branch_name, repo);
6764 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6768 err = delete_ref(base_commit_ref_name, repo);
6772 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6775 err = delete_ref(branch_ref_name, repo);
6779 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6782 err = delete_ref(commit_ref_name, repo);
6786 free(tmp_branch_name);
6787 free(base_commit_ref_name);
6788 free(branch_ref_name);
6789 free(commit_ref_name);
6793 const struct got_error *
6794 got_worktree_histedit_abort(struct got_worktree *worktree,
6795 struct got_fileindex *fileindex, struct got_repository *repo,
6796 struct got_reference *branch, struct got_object_id *base_commit_id,
6797 got_worktree_checkout_cb progress_cb, void *progress_arg)
6799 const struct got_error *err, *unlockerr, *sync_err;
6800 struct got_reference *resolved = NULL;
6801 char *fileindex_path = NULL;
6802 struct got_object_id *tree_id = NULL;
6803 struct revert_file_args rfa;
6805 err = lock_worktree(worktree, LOCK_EX);
6809 err = got_ref_open(&resolved, repo,
6810 got_ref_get_symref_target(branch), 0);
6814 err = got_worktree_set_head_ref(worktree, resolved);
6818 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6822 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6823 worktree->path_prefix);
6827 err = delete_histedit_refs(worktree, repo);
6831 err = get_fileindex_path(&fileindex_path, worktree);
6835 rfa.worktree = worktree;
6836 rfa.fileindex = fileindex;
6837 rfa.progress_cb = progress_cb;
6838 rfa.progress_arg = progress_arg;
6839 rfa.patch_cb = NULL;
6840 rfa.patch_arg = NULL;
6842 err = worktree_status(worktree, "", fileindex, repo,
6843 revert_file, &rfa, NULL, NULL, 0, 0);
6847 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6848 repo, progress_cb, progress_arg, NULL, NULL);
6850 sync_err = sync_fileindex(fileindex, fileindex_path);
6851 if (sync_err && err == NULL)
6854 got_ref_close(resolved);
6856 free(fileindex_path);
6858 unlockerr = lock_worktree(worktree, LOCK_SH);
6859 if (unlockerr && err == NULL)
6864 const struct got_error *
6865 got_worktree_histedit_complete(struct got_worktree *worktree,
6866 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6867 struct got_reference *edited_branch, struct got_repository *repo)
6869 const struct got_error *err, *unlockerr;
6870 struct got_object_id *new_head_commit_id = NULL;
6871 struct got_reference *resolved = NULL;
6873 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6877 err = got_ref_open(&resolved, repo,
6878 got_ref_get_symref_target(edited_branch), 0);
6882 err = got_ref_change_ref(resolved, new_head_commit_id);
6886 err = got_ref_write(resolved, repo);
6890 err = got_worktree_set_head_ref(worktree, resolved);
6894 err = delete_histedit_refs(worktree, repo);
6897 got_fileindex_free(fileindex);
6898 free(new_head_commit_id);
6899 unlockerr = lock_worktree(worktree, LOCK_SH);
6900 if (unlockerr && err == NULL)
6905 const struct got_error *
6906 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6907 struct got_object_id *commit_id, struct got_repository *repo)
6909 const struct got_error *err;
6910 char *commit_ref_name;
6912 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6916 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6920 err = delete_ref(commit_ref_name, repo);
6922 free(commit_ref_name);
6926 const struct got_error *
6927 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6928 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6929 struct got_worktree *worktree, const char *refname,
6930 struct got_repository *repo)
6932 const struct got_error *err = NULL;
6933 char *fileindex_path = NULL;
6934 struct check_rebase_ok_arg ok_arg;
6938 *base_branch_ref = NULL;
6940 err = lock_worktree(worktree, LOCK_EX);
6944 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6945 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6946 "cannot integrate a branch into itself; "
6947 "update -b or different branch name required");
6951 err = open_fileindex(fileindex, &fileindex_path, worktree);
6955 /* Preconditions are the same as for rebase. */
6956 ok_arg.worktree = worktree;
6958 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6963 err = got_ref_open(branch_ref, repo, refname, 1);
6967 err = got_ref_open(base_branch_ref, repo,
6968 got_worktree_get_head_ref_name(worktree), 1);
6972 got_ref_close(*branch_ref);
6975 if (*base_branch_ref) {
6976 got_ref_close(*base_branch_ref);
6977 *base_branch_ref = NULL;
6980 got_fileindex_free(*fileindex);
6983 lock_worktree(worktree, LOCK_SH);
6988 const struct got_error *
6989 got_worktree_integrate_continue(struct got_worktree *worktree,
6990 struct got_fileindex *fileindex, struct got_repository *repo,
6991 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6992 got_worktree_checkout_cb progress_cb, void *progress_arg,
6993 got_cancel_cb cancel_cb, void *cancel_arg)
6995 const struct got_error *err = NULL, *sync_err, *unlockerr;
6996 char *fileindex_path = NULL;
6997 struct got_object_id *tree_id = NULL, *commit_id = NULL;
6999 err = get_fileindex_path(&fileindex_path, worktree);
7003 err = got_ref_resolve(&commit_id, repo, branch_ref);
7007 err = got_object_id_by_path(&tree_id, repo, commit_id,
7008 worktree->path_prefix);
7012 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7016 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7017 progress_cb, progress_arg, cancel_cb, cancel_arg);
7021 err = got_ref_change_ref(base_branch_ref, commit_id);
7025 err = got_ref_write(base_branch_ref, repo);
7027 sync_err = sync_fileindex(fileindex, fileindex_path);
7028 if (sync_err && err == NULL)
7032 unlockerr = got_ref_unlock(branch_ref);
7033 if (unlockerr && err == NULL)
7035 got_ref_close(branch_ref);
7037 unlockerr = got_ref_unlock(base_branch_ref);
7038 if (unlockerr && err == NULL)
7040 got_ref_close(base_branch_ref);
7042 got_fileindex_free(fileindex);
7043 free(fileindex_path);
7046 unlockerr = lock_worktree(worktree, LOCK_SH);
7047 if (unlockerr && err == NULL)
7052 const struct got_error *
7053 got_worktree_integrate_abort(struct got_worktree *worktree,
7054 struct got_fileindex *fileindex, struct got_repository *repo,
7055 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7057 const struct got_error *err = NULL, *unlockerr = NULL;
7059 got_fileindex_free(fileindex);
7061 err = lock_worktree(worktree, LOCK_SH);
7063 unlockerr = got_ref_unlock(branch_ref);
7064 if (unlockerr && err == NULL)
7066 got_ref_close(branch_ref);
7068 unlockerr = got_ref_unlock(base_branch_ref);
7069 if (unlockerr && err == NULL)
7071 got_ref_close(base_branch_ref);
7076 struct check_stage_ok_arg {
7077 struct got_object_id *head_commit_id;
7078 struct got_worktree *worktree;
7079 struct got_fileindex *fileindex;
7080 struct got_repository *repo;
7084 const struct got_error *
7085 check_stage_ok(void *arg, unsigned char status,
7086 unsigned char staged_status, const char *relpath,
7087 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7088 struct got_object_id *commit_id, int dirfd, const char *de_name)
7090 struct check_stage_ok_arg *a = arg;
7091 const struct got_error *err = NULL;
7092 struct got_fileindex_entry *ie;
7093 struct got_object_id base_commit_id;
7094 struct got_object_id *base_commit_idp = NULL;
7095 char *in_repo_path = NULL, *p;
7097 if (status == GOT_STATUS_UNVERSIONED ||
7098 status == GOT_STATUS_NO_CHANGE)
7100 if (status == GOT_STATUS_NONEXISTENT)
7101 return got_error_set_errno(ENOENT, relpath);
7103 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7105 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7107 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7108 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7110 return got_error_from_errno("asprintf");
7112 if (got_fileindex_entry_has_commit(ie)) {
7113 memcpy(base_commit_id.sha1, ie->commit_sha1,
7114 SHA1_DIGEST_LENGTH);
7115 base_commit_idp = &base_commit_id;
7118 if (status == GOT_STATUS_CONFLICT) {
7119 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7121 } else if (status != GOT_STATUS_ADD &&
7122 status != GOT_STATUS_MODIFY &&
7123 status != GOT_STATUS_DELETE) {
7124 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7128 a->have_changes = 1;
7133 err = check_out_of_date(p, status, staged_status,
7134 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7135 GOT_ERR_STAGE_OUT_OF_DATE);
7141 struct stage_path_arg {
7142 struct got_worktree *worktree;
7143 struct got_fileindex *fileindex;
7144 struct got_repository *repo;
7145 got_worktree_status_cb status_cb;
7147 got_worktree_patch_cb patch_cb;
7149 int staged_something;
7150 int allow_bad_symlinks;
7153 static const struct got_error *
7154 stage_path(void *arg, unsigned char status,
7155 unsigned char staged_status, const char *relpath,
7156 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7157 struct got_object_id *commit_id, int dirfd, const char *de_name)
7159 struct stage_path_arg *a = arg;
7160 const struct got_error *err = NULL;
7161 struct got_fileindex_entry *ie;
7162 char *ondisk_path = NULL, *path_content = NULL;
7164 struct got_object_id *new_staged_blob_id = NULL;
7167 if (status == GOT_STATUS_UNVERSIONED)
7170 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7172 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7174 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7176 return got_error_from_errno("asprintf");
7179 case GOT_STATUS_ADD:
7180 case GOT_STATUS_MODIFY:
7181 /* XXX could sb.st_mode be passed in by our caller? */
7182 if (lstat(ondisk_path, &sb) == -1) {
7183 err = got_error_from_errno2("lstat", ondisk_path);
7187 if (status == GOT_STATUS_ADD) {
7188 int choice = GOT_PATCH_CHOICE_NONE;
7189 err = (*a->patch_cb)(&choice, a->patch_arg,
7190 status, ie->path, NULL, 1, 1);
7193 if (choice != GOT_PATCH_CHOICE_YES)
7196 err = create_patched_content(&path_content, 0,
7197 staged_blob_id ? staged_blob_id : blob_id,
7198 ondisk_path, dirfd, de_name, ie->path,
7199 a->repo, a->patch_cb, a->patch_arg);
7200 if (err || path_content == NULL)
7204 err = got_object_blob_create(&new_staged_blob_id,
7205 path_content ? path_content : ondisk_path, a->repo);
7208 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7209 SHA1_DIGEST_LENGTH);
7210 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7211 stage = GOT_FILEIDX_STAGE_ADD;
7213 stage = GOT_FILEIDX_STAGE_MODIFY;
7214 got_fileindex_entry_stage_set(ie, stage);
7215 if (S_ISLNK(sb.st_mode)) {
7216 int is_bad_symlink = 0;
7217 if (!a->allow_bad_symlinks) {
7218 char target_path[PATH_MAX];
7220 target_len = readlink(ondisk_path, target_path,
7221 sizeof(target_path));
7222 if (target_len == -1) {
7223 err = got_error_from_errno2("readlink",
7227 err = is_bad_symlink_target(&is_bad_symlink,
7228 target_path, target_len, ondisk_path,
7229 a->worktree->root_path);
7232 if (is_bad_symlink) {
7233 err = got_error_path(ondisk_path,
7234 GOT_ERR_BAD_SYMLINK);
7239 got_fileindex_entry_staged_filetype_set(ie,
7240 GOT_FILEIDX_MODE_BAD_SYMLINK);
7242 got_fileindex_entry_staged_filetype_set(ie,
7243 GOT_FILEIDX_MODE_SYMLINK);
7245 got_fileindex_entry_staged_filetype_set(ie,
7246 GOT_FILEIDX_MODE_REGULAR_FILE);
7248 a->staged_something = 1;
7249 if (a->status_cb == NULL)
7251 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7252 get_staged_status(ie), relpath, blob_id,
7253 new_staged_blob_id, NULL, dirfd, de_name);
7255 case GOT_STATUS_DELETE:
7256 if (staged_status == GOT_STATUS_DELETE)
7259 int choice = GOT_PATCH_CHOICE_NONE;
7260 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7261 ie->path, NULL, 1, 1);
7264 if (choice == GOT_PATCH_CHOICE_NO)
7266 if (choice != GOT_PATCH_CHOICE_YES) {
7267 err = got_error(GOT_ERR_PATCH_CHOICE);
7271 stage = GOT_FILEIDX_STAGE_DELETE;
7272 got_fileindex_entry_stage_set(ie, stage);
7273 a->staged_something = 1;
7274 if (a->status_cb == NULL)
7276 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7277 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7280 case GOT_STATUS_NO_CHANGE:
7282 case GOT_STATUS_CONFLICT:
7283 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7285 case GOT_STATUS_NONEXISTENT:
7286 err = got_error_set_errno(ENOENT, relpath);
7289 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7293 if (path_content && unlink(path_content) == -1 && err == NULL)
7294 err = got_error_from_errno2("unlink", path_content);
7297 free(new_staged_blob_id);
7301 const struct got_error *
7302 got_worktree_stage(struct got_worktree *worktree,
7303 struct got_pathlist_head *paths,
7304 got_worktree_status_cb status_cb, void *status_arg,
7305 got_worktree_patch_cb patch_cb, void *patch_arg,
7306 int allow_bad_symlinks, struct got_repository *repo)
7308 const struct got_error *err = NULL, *sync_err, *unlockerr;
7309 struct got_pathlist_entry *pe;
7310 struct got_fileindex *fileindex = NULL;
7311 char *fileindex_path = NULL;
7312 struct got_reference *head_ref = NULL;
7313 struct got_object_id *head_commit_id = NULL;
7314 struct check_stage_ok_arg oka;
7315 struct stage_path_arg spa;
7317 err = lock_worktree(worktree, LOCK_EX);
7321 err = got_ref_open(&head_ref, repo,
7322 got_worktree_get_head_ref_name(worktree), 0);
7325 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7328 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7332 /* Check pre-conditions before staging anything. */
7333 oka.head_commit_id = head_commit_id;
7334 oka.worktree = worktree;
7335 oka.fileindex = fileindex;
7337 oka.have_changes = 0;
7338 TAILQ_FOREACH(pe, paths, entry) {
7339 err = worktree_status(worktree, pe->path, fileindex, repo,
7340 check_stage_ok, &oka, NULL, NULL, 0, 0);
7344 if (!oka.have_changes) {
7345 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7349 spa.worktree = worktree;
7350 spa.fileindex = fileindex;
7352 spa.patch_cb = patch_cb;
7353 spa.patch_arg = patch_arg;
7354 spa.status_cb = status_cb;
7355 spa.status_arg = status_arg;
7356 spa.staged_something = 0;
7357 spa.allow_bad_symlinks = allow_bad_symlinks;
7358 TAILQ_FOREACH(pe, paths, entry) {
7359 err = worktree_status(worktree, pe->path, fileindex, repo,
7360 stage_path, &spa, NULL, NULL, 0, 0);
7364 if (!spa.staged_something) {
7365 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7369 sync_err = sync_fileindex(fileindex, fileindex_path);
7370 if (sync_err && err == NULL)
7374 got_ref_close(head_ref);
7375 free(head_commit_id);
7376 free(fileindex_path);
7378 got_fileindex_free(fileindex);
7379 unlockerr = lock_worktree(worktree, LOCK_SH);
7380 if (unlockerr && err == NULL)
7385 struct unstage_path_arg {
7386 struct got_worktree *worktree;
7387 struct got_fileindex *fileindex;
7388 struct got_repository *repo;
7389 got_worktree_checkout_cb progress_cb;
7391 got_worktree_patch_cb patch_cb;
7395 static const struct got_error *
7396 create_unstaged_content(char **path_unstaged_content,
7397 char **path_new_staged_content, struct got_object_id *blob_id,
7398 struct got_object_id *staged_blob_id, const char *relpath,
7399 struct got_repository *repo,
7400 got_worktree_patch_cb patch_cb, void *patch_arg)
7402 const struct got_error *err;
7403 struct got_blob_object *blob = NULL, *staged_blob = NULL;
7404 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7405 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7406 struct stat sb1, sb2;
7407 struct got_diff_changes *changes = NULL;
7408 struct got_diff_state *ds = NULL;
7409 struct got_diff_args *args = NULL;
7410 struct got_diff_change *change;
7411 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
7412 int have_content = 0, have_rejected_content = 0;
7414 *path_unstaged_content = NULL;
7415 *path_new_staged_content = NULL;
7417 err = got_object_id_str(&label1, blob_id);
7420 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7424 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7428 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7432 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7436 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7440 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7444 if (stat(path1, &sb1) == -1) {
7445 err = got_error_from_errno2("stat", path1);
7449 if (stat(path2, &sb2) == -1) {
7450 err = got_error_from_errno2("stat", path2);
7454 err = got_diff_files(&changes, &ds, &args, &diff_flags,
7455 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
7459 err = got_opentemp_named(path_unstaged_content, &outfile,
7460 "got-unstaged-content");
7463 err = got_opentemp_named(path_new_staged_content, &rejectfile,
7464 "got-new-staged-content");
7468 if (fseek(f1, 0L, SEEK_SET) == -1) {
7469 err = got_ferror(f1, GOT_ERR_IO);
7472 if (fseek(f2, 0L, SEEK_SET) == -1) {
7473 err = got_ferror(f2, GOT_ERR_IO);
7476 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
7478 err = apply_or_reject_change(&choice, change, ++n,
7479 changes->nchanges, ds, args, diff_flags, relpath,
7480 f1, f2, &line_cur1, &line_cur2,
7481 outfile, rejectfile, patch_cb, patch_arg);
7484 if (choice == GOT_PATCH_CHOICE_YES)
7487 have_rejected_content = 1;
7488 if (choice == GOT_PATCH_CHOICE_QUIT)
7491 if (have_content || have_rejected_content)
7492 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7493 outfile, rejectfile);
7497 got_object_blob_close(blob);
7499 got_object_blob_close(staged_blob);
7500 if (f1 && fclose(f1) == EOF && err == NULL)
7501 err = got_error_from_errno2("fclose", path1);
7502 if (f2 && fclose(f2) == EOF && err == NULL)
7503 err = got_error_from_errno2("fclose", path2);
7504 if (outfile && fclose(outfile) == EOF && err == NULL)
7505 err = got_error_from_errno2("fclose", *path_unstaged_content);
7506 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7507 err = got_error_from_errno2("fclose", *path_new_staged_content);
7508 if (path1 && unlink(path1) == -1 && err == NULL)
7509 err = got_error_from_errno2("unlink", path1);
7510 if (path2 && unlink(path2) == -1 && err == NULL)
7511 err = got_error_from_errno2("unlink", path2);
7512 if (err || !have_content) {
7513 if (*path_unstaged_content &&
7514 unlink(*path_unstaged_content) == -1 && err == NULL)
7515 err = got_error_from_errno2("unlink",
7516 *path_unstaged_content);
7517 free(*path_unstaged_content);
7518 *path_unstaged_content = NULL;
7520 if (err || !have_content || !have_rejected_content) {
7521 if (*path_new_staged_content &&
7522 unlink(*path_new_staged_content) == -1 && err == NULL)
7523 err = got_error_from_errno2("unlink",
7524 *path_new_staged_content);
7525 free(*path_new_staged_content);
7526 *path_new_staged_content = NULL;
7530 got_diff_state_free(ds);
7534 got_diff_free_changes(changes);
7540 static const struct got_error *
7541 unstage_hunks(struct got_object_id *staged_blob_id,
7542 struct got_blob_object *blob_base,
7543 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
7544 const char *ondisk_path, const char *label_orig,
7545 struct got_worktree *worktree, struct got_repository *repo,
7546 got_worktree_patch_cb patch_cb, void *patch_arg,
7547 got_worktree_checkout_cb progress_cb, void *progress_arg)
7549 const struct got_error *err = NULL;
7550 char *path_unstaged_content = NULL;
7551 char *path_new_staged_content = NULL;
7552 struct got_object_id *new_staged_blob_id = NULL;
7556 err = create_unstaged_content(&path_unstaged_content,
7557 &path_new_staged_content, blob_id, staged_blob_id,
7558 ie->path, repo, patch_cb, patch_arg);
7562 if (path_unstaged_content == NULL)
7565 if (path_new_staged_content) {
7566 err = got_object_blob_create(&new_staged_blob_id,
7567 path_new_staged_content, repo);
7572 f = fopen(path_unstaged_content, "r");
7574 err = got_error_from_errno2("fopen",
7575 path_unstaged_content);
7578 if (fstat(fileno(f), &sb) == -1) {
7579 err = got_error_from_errno2("fstat", path_unstaged_content);
7582 if (got_fileindex_entry_staged_filetype_get(ie) ==
7583 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
7584 char link_target[PATH_MAX];
7586 r = fread(link_target, 1, sizeof(link_target), f);
7587 if (r == 0 && ferror(f)) {
7588 err = got_error_from_errno("fread");
7591 if (r >= sizeof(link_target)) { /* should not happen */
7592 err = got_error(GOT_ERR_NO_SPACE);
7595 link_target[r] = '\0';
7596 err = merge_symlink(worktree, blob_base,
7597 ondisk_path, ie->path, label_orig, link_target,
7598 worktree->base_commit_id, repo, progress_cb,
7601 int local_changes_subsumed;
7602 err = merge_file(&local_changes_subsumed, worktree,
7603 blob_base, ondisk_path, ie->path,
7604 got_fileindex_perms_to_st(ie),
7605 path_unstaged_content, label_orig, "unstaged",
7606 repo, progress_cb, progress_arg);
7611 if (new_staged_blob_id) {
7612 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7613 SHA1_DIGEST_LENGTH);
7615 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7616 got_fileindex_entry_staged_filetype_set(ie, 0);
7619 free(new_staged_blob_id);
7620 if (path_unstaged_content &&
7621 unlink(path_unstaged_content) == -1 && err == NULL)
7622 err = got_error_from_errno2("unlink", path_unstaged_content);
7623 if (path_new_staged_content &&
7624 unlink(path_new_staged_content) == -1 && err == NULL)
7625 err = got_error_from_errno2("unlink", path_new_staged_content);
7626 if (f && fclose(f) != 0 && err == NULL)
7627 err = got_error_from_errno2("fclose", path_unstaged_content);
7628 free(path_unstaged_content);
7629 free(path_new_staged_content);
7633 static const struct got_error *
7634 unstage_path(void *arg, unsigned char status,
7635 unsigned char staged_status, const char *relpath,
7636 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7637 struct got_object_id *commit_id, int dirfd, const char *de_name)
7639 const struct got_error *err = NULL;
7640 struct unstage_path_arg *a = arg;
7641 struct got_fileindex_entry *ie;
7642 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7643 char *ondisk_path = NULL;
7644 char *id_str = NULL, *label_orig = NULL;
7645 int local_changes_subsumed;
7648 if (staged_status != GOT_STATUS_ADD &&
7649 staged_status != GOT_STATUS_MODIFY &&
7650 staged_status != GOT_STATUS_DELETE)
7653 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7655 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7657 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7659 return got_error_from_errno("asprintf");
7661 err = got_object_id_str(&id_str,
7662 commit_id ? commit_id : a->worktree->base_commit_id);
7665 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7667 err = got_error_from_errno("asprintf");
7671 switch (staged_status) {
7672 case GOT_STATUS_MODIFY:
7673 err = got_object_open_as_blob(&blob_base, a->repo,
7678 case GOT_STATUS_ADD:
7680 if (staged_status == GOT_STATUS_ADD) {
7681 int choice = GOT_PATCH_CHOICE_NONE;
7682 err = (*a->patch_cb)(&choice, a->patch_arg,
7683 staged_status, ie->path, NULL, 1, 1);
7686 if (choice != GOT_PATCH_CHOICE_YES)
7689 err = unstage_hunks(staged_blob_id,
7690 blob_base, blob_id, ie, ondisk_path,
7691 label_orig, a->worktree, a->repo,
7692 a->patch_cb, a->patch_arg,
7693 a->progress_cb, a->progress_arg);
7694 break; /* Done with this file. */
7697 err = got_object_open_as_blob(&blob_staged, a->repo,
7698 staged_blob_id, 8192);
7701 switch (got_fileindex_entry_staged_filetype_get(ie)) {
7702 case GOT_FILEIDX_MODE_BAD_SYMLINK:
7703 case GOT_FILEIDX_MODE_REGULAR_FILE:
7704 err = merge_blob(&local_changes_subsumed, a->worktree,
7705 blob_base, ondisk_path, relpath,
7706 got_fileindex_perms_to_st(ie), label_orig,
7707 blob_staged, commit_id ? commit_id :
7708 a->worktree->base_commit_id, a->repo,
7709 a->progress_cb, a->progress_arg);
7711 case GOT_FILEIDX_MODE_SYMLINK:
7712 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7713 char *staged_target;
7714 err = got_object_blob_read_to_str(
7715 &staged_target, blob_staged);
7718 err = merge_symlink(a->worktree, blob_base,
7719 ondisk_path, relpath, label_orig,
7720 staged_target, commit_id ? commit_id :
7721 a->worktree->base_commit_id,
7722 a->repo, a->progress_cb, a->progress_arg);
7723 free(staged_target);
7725 err = merge_blob(&local_changes_subsumed,
7726 a->worktree, blob_base, ondisk_path,
7727 relpath, got_fileindex_perms_to_st(ie),
7728 label_orig, blob_staged,
7729 commit_id ? commit_id :
7730 a->worktree->base_commit_id, a->repo,
7731 a->progress_cb, a->progress_arg);
7735 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
7739 got_fileindex_entry_stage_set(ie,
7740 GOT_FILEIDX_STAGE_NONE);
7741 got_fileindex_entry_staged_filetype_set(ie, 0);
7744 case GOT_STATUS_DELETE:
7746 int choice = GOT_PATCH_CHOICE_NONE;
7747 err = (*a->patch_cb)(&choice, a->patch_arg,
7748 staged_status, ie->path, NULL, 1, 1);
7751 if (choice == GOT_PATCH_CHOICE_NO)
7753 if (choice != GOT_PATCH_CHOICE_YES) {
7754 err = got_error(GOT_ERR_PATCH_CHOICE);
7758 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7759 got_fileindex_entry_staged_filetype_set(ie, 0);
7760 err = get_file_status(&status, &sb, ie, ondisk_path,
7761 dirfd, de_name, a->repo);
7764 err = (*a->progress_cb)(a->progress_arg, status, relpath);
7770 got_object_blob_close(blob_base);
7772 got_object_blob_close(blob_staged);
7778 const struct got_error *
7779 got_worktree_unstage(struct got_worktree *worktree,
7780 struct got_pathlist_head *paths,
7781 got_worktree_checkout_cb progress_cb, void *progress_arg,
7782 got_worktree_patch_cb patch_cb, void *patch_arg,
7783 struct got_repository *repo)
7785 const struct got_error *err = NULL, *sync_err, *unlockerr;
7786 struct got_pathlist_entry *pe;
7787 struct got_fileindex *fileindex = NULL;
7788 char *fileindex_path = NULL;
7789 struct unstage_path_arg upa;
7791 err = lock_worktree(worktree, LOCK_EX);
7795 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7799 upa.worktree = worktree;
7800 upa.fileindex = fileindex;
7802 upa.progress_cb = progress_cb;
7803 upa.progress_arg = progress_arg;
7804 upa.patch_cb = patch_cb;
7805 upa.patch_arg = patch_arg;
7806 TAILQ_FOREACH(pe, paths, entry) {
7807 err = worktree_status(worktree, pe->path, fileindex, repo,
7808 unstage_path, &upa, NULL, NULL, 0, 0);
7813 sync_err = sync_fileindex(fileindex, fileindex_path);
7814 if (sync_err && err == NULL)
7817 free(fileindex_path);
7819 got_fileindex_free(fileindex);
7820 unlockerr = lock_worktree(worktree, LOCK_SH);
7821 if (unlockerr && err == NULL)
7826 struct report_file_info_arg {
7827 struct got_worktree *worktree;
7828 got_worktree_path_info_cb info_cb;
7830 struct got_pathlist_head *paths;
7831 got_cancel_cb cancel_cb;
7835 static const struct got_error *
7836 report_file_info(void *arg, struct got_fileindex_entry *ie)
7838 struct report_file_info_arg *a = arg;
7839 struct got_pathlist_entry *pe;
7840 struct got_object_id blob_id, staged_blob_id, commit_id;
7841 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
7842 struct got_object_id *commit_idp = NULL;
7845 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
7846 return got_error(GOT_ERR_CANCELLED);
7848 TAILQ_FOREACH(pe, a->paths, entry) {
7849 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
7850 got_path_is_child(ie->path, pe->path, pe->path_len))
7853 if (pe == NULL) /* not found */
7856 if (got_fileindex_entry_has_blob(ie)) {
7857 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
7858 blob_idp = &blob_id;
7860 stage = got_fileindex_entry_stage_get(ie);
7861 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
7862 stage == GOT_FILEIDX_STAGE_ADD) {
7863 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
7864 SHA1_DIGEST_LENGTH);
7865 staged_blob_idp = &staged_blob_id;
7868 if (got_fileindex_entry_has_commit(ie)) {
7869 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
7870 commit_idp = &commit_id;
7873 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
7874 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
7877 const struct got_error *
7878 got_worktree_path_info(struct got_worktree *worktree,
7879 struct got_pathlist_head *paths,
7880 got_worktree_path_info_cb info_cb, void *info_arg,
7881 got_cancel_cb cancel_cb, void *cancel_arg)
7884 const struct got_error *err = NULL, *unlockerr;
7885 struct got_fileindex *fileindex = NULL;
7886 char *fileindex_path = NULL;
7887 struct report_file_info_arg arg;
7889 err = lock_worktree(worktree, LOCK_SH);
7893 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7897 arg.worktree = worktree;
7898 arg.info_cb = info_cb;
7899 arg.info_arg = info_arg;
7901 arg.cancel_cb = cancel_cb;
7902 arg.cancel_arg = cancel_arg;
7903 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
7906 free(fileindex_path);
7908 got_fileindex_free(fileindex);
7909 unlockerr = lock_worktree(worktree, LOCK_UN);
7910 if (unlockerr && err == NULL)