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, int *nchunks_used,
4140 struct diff_result *diff_result, int n,
4141 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4142 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4143 got_worktree_patch_cb patch_cb, void *patch_arg)
4145 const struct got_error *err = NULL;
4146 struct diff_chunk_context cc = {};
4147 int start_old, end_old, start_new, end_new;
4149 struct diff_output_unidiff_state *diff_state;
4150 struct diff_input_info diff_info;
4153 *choice = GOT_PATCH_CHOICE_NONE;
4155 /* Get changed line numbers without context lines for copy_change(). */
4156 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4157 start_old = cc.left.start;
4158 end_old = cc.left.end;
4159 start_new = cc.right.start;
4160 end_new = cc.right.end;
4162 /* Get the same change with context lines for display. */
4163 memset(&cc, 0, sizeof(cc));
4164 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4166 memset(&diff_info, 0, sizeof(diff_info));
4167 diff_info.left_path = relpath;
4168 diff_info.right_path = relpath;
4170 diff_state = diff_output_unidiff_state_alloc();
4171 if (diff_state == NULL)
4172 return got_error_set_errno(ENOMEM,
4173 "diff_output_unidiff_state_alloc");
4175 hunkfile = got_opentemp();
4176 if (hunkfile == NULL) {
4177 err = got_error_from_errno("got_opentemp");
4181 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4183 if (rc != DIFF_RC_OK) {
4184 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4188 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4189 err = got_ferror(hunkfile, GOT_ERR_IO);
4193 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4194 hunkfile, changeno, nchanges);
4199 case GOT_PATCH_CHOICE_YES:
4200 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4201 end_old, start_new, end_new, outfile, rejectfile);
4203 case GOT_PATCH_CHOICE_NO:
4204 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4205 end_old, start_new, end_new, rejectfile, outfile);
4207 case GOT_PATCH_CHOICE_QUIT:
4210 err = got_error(GOT_ERR_PATCH_CHOICE);
4214 diff_output_unidiff_state_free(diff_state);
4215 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4216 err = got_error_from_errno("fclose");
4220 struct revert_file_args {
4221 struct got_worktree *worktree;
4222 struct got_fileindex *fileindex;
4223 got_worktree_checkout_cb progress_cb;
4225 got_worktree_patch_cb patch_cb;
4227 struct got_repository *repo;
4230 static const struct got_error *
4231 create_patched_content(char **path_outfile, int reverse_patch,
4232 struct got_object_id *blob_id, const char *path2,
4233 int dirfd2, const char *de_name2,
4234 const char *relpath, struct got_repository *repo,
4235 got_worktree_patch_cb patch_cb, void *patch_arg)
4237 const struct got_error *err, *free_err;
4238 struct got_blob_object *blob = NULL;
4239 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4241 char link_target[PATH_MAX];
4242 ssize_t link_len = 0;
4243 char *path1 = NULL, *id_str = NULL;
4245 struct got_diffreg_result *diffreg_result = NULL;
4246 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4247 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4249 *path_outfile = NULL;
4251 err = got_object_id_str(&id_str, blob_id);
4256 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4258 if (errno != ELOOP) {
4259 err = got_error_from_errno2("openat", path2);
4262 link_len = readlinkat(dirfd2, de_name2,
4263 link_target, sizeof(link_target));
4265 return got_error_from_errno2("readlinkat", path2);
4266 sb2.st_mode = S_IFLNK;
4267 sb2.st_size = link_len;
4270 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4272 if (errno != ELOOP) {
4273 err = got_error_from_errno2("open", path2);
4276 link_len = readlink(path2, link_target,
4277 sizeof(link_target));
4279 return got_error_from_errno2("readlink", path2);
4280 sb2.st_mode = S_IFLNK;
4281 sb2.st_size = link_len;
4285 if (fstat(fd2, &sb2) == -1) {
4286 err = got_error_from_errno2("fstat", path2);
4290 f2 = fdopen(fd2, "r");
4292 err = got_error_from_errno2("fdopen", path2);
4298 f2 = got_opentemp();
4300 err = got_error_from_errno2("got_opentemp", path2);
4303 n = fwrite(link_target, 1, link_len, f2);
4304 if (n != link_len) {
4305 err = got_ferror(f2, GOT_ERR_IO);
4308 if (fflush(f2) == EOF) {
4309 err = got_error_from_errno("fflush");
4315 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4319 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4323 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4327 err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4332 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4336 if (fseek(f1, 0L, SEEK_SET) == -1)
4337 return got_ferror(f1, GOT_ERR_IO);
4338 if (fseek(f2, 0L, SEEK_SET) == -1)
4339 return got_ferror(f2, GOT_ERR_IO);
4341 /* Count the number of actual changes in the diff result. */
4342 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4343 struct diff_chunk_context cc = {};
4344 diff_chunk_context_load_change(&cc, &nchunks_used,
4345 diffreg_result->result, n, 0);
4348 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4350 err = apply_or_reject_change(&choice, &nchunks_used,
4351 diffreg_result->result, n, relpath, f1, f2,
4352 &line_cur1, &line_cur2,
4353 reverse_patch ? NULL : outfile,
4354 reverse_patch ? outfile : NULL,
4355 ++i, nchanges, patch_cb, patch_arg);
4358 if (choice == GOT_PATCH_CHOICE_YES)
4360 else if (choice == GOT_PATCH_CHOICE_QUIT)
4364 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4365 reverse_patch ? NULL : outfile,
4366 reverse_patch ? outfile : NULL);
4370 if (!S_ISLNK(sb2.st_mode)) {
4371 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4372 err = got_error_from_errno2("fchmod", path2);
4380 got_object_blob_close(blob);
4381 free_err = got_diffreg_result_free(diffreg_result);
4384 if (f1 && fclose(f1) == EOF && err == NULL)
4385 err = got_error_from_errno2("fclose", path1);
4386 if (f2 && fclose(f2) == EOF && err == NULL)
4387 err = got_error_from_errno2("fclose", path2);
4388 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4389 err = got_error_from_errno2("close", path2);
4390 if (outfile && fclose(outfile) == EOF && err == NULL)
4391 err = got_error_from_errno2("fclose", *path_outfile);
4392 if (path1 && unlink(path1) == -1 && err == NULL)
4393 err = got_error_from_errno2("unlink", path1);
4394 if (err || !have_content) {
4395 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4396 err = got_error_from_errno2("unlink", *path_outfile);
4397 free(*path_outfile);
4398 *path_outfile = NULL;
4404 static const struct got_error *
4405 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4406 const char *relpath, struct got_object_id *blob_id,
4407 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4408 int dirfd, const char *de_name)
4410 struct revert_file_args *a = arg;
4411 const struct got_error *err = NULL;
4412 char *parent_path = NULL;
4413 struct got_fileindex_entry *ie;
4414 struct got_tree_object *tree = NULL;
4415 struct got_object_id *tree_id = NULL;
4416 const struct got_tree_entry *te = NULL;
4417 char *tree_path = NULL, *te_name;
4418 char *ondisk_path = NULL, *path_content = NULL;
4419 struct got_blob_object *blob = NULL;
4421 /* Reverting a staged deletion is a no-op. */
4422 if (status == GOT_STATUS_DELETE &&
4423 staged_status != GOT_STATUS_NO_CHANGE)
4426 if (status == GOT_STATUS_UNVERSIONED)
4427 return (*a->progress_cb)(a->progress_arg,
4428 GOT_STATUS_UNVERSIONED, relpath);
4430 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4432 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4434 /* Construct in-repository path of tree which contains this blob. */
4435 err = got_path_dirname(&parent_path, ie->path);
4437 if (err->code != GOT_ERR_BAD_PATH)
4439 parent_path = strdup("/");
4440 if (parent_path == NULL) {
4441 err = got_error_from_errno("strdup");
4445 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4446 tree_path = strdup(parent_path);
4447 if (tree_path == NULL) {
4448 err = got_error_from_errno("strdup");
4452 if (got_path_is_root_dir(parent_path)) {
4453 tree_path = strdup(a->worktree->path_prefix);
4454 if (tree_path == NULL) {
4455 err = got_error_from_errno("strdup");
4459 if (asprintf(&tree_path, "%s/%s",
4460 a->worktree->path_prefix, parent_path) == -1) {
4461 err = got_error_from_errno("asprintf");
4467 err = got_object_id_by_path(&tree_id, a->repo,
4468 a->worktree->base_commit_id, tree_path);
4470 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4471 (status == GOT_STATUS_ADD ||
4472 staged_status == GOT_STATUS_ADD)))
4475 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4479 err = got_path_basename(&te_name, ie->path);
4483 te = got_object_tree_find_entry(tree, te_name);
4485 if (te == NULL && status != GOT_STATUS_ADD &&
4486 staged_status != GOT_STATUS_ADD) {
4487 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4493 case GOT_STATUS_ADD:
4495 int choice = GOT_PATCH_CHOICE_NONE;
4496 err = (*a->patch_cb)(&choice, a->patch_arg,
4497 status, ie->path, NULL, 1, 1);
4500 if (choice != GOT_PATCH_CHOICE_YES)
4503 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4507 got_fileindex_entry_remove(a->fileindex, ie);
4509 case GOT_STATUS_DELETE:
4511 int choice = GOT_PATCH_CHOICE_NONE;
4512 err = (*a->patch_cb)(&choice, a->patch_arg,
4513 status, ie->path, NULL, 1, 1);
4516 if (choice != GOT_PATCH_CHOICE_YES)
4520 case GOT_STATUS_MODIFY:
4521 case GOT_STATUS_MODE_CHANGE:
4522 case GOT_STATUS_CONFLICT:
4523 case GOT_STATUS_MISSING: {
4524 struct got_object_id id;
4525 if (staged_status == GOT_STATUS_ADD ||
4526 staged_status == GOT_STATUS_MODIFY) {
4527 memcpy(id.sha1, ie->staged_blob_sha1,
4528 SHA1_DIGEST_LENGTH);
4530 memcpy(id.sha1, ie->blob_sha1,
4531 SHA1_DIGEST_LENGTH);
4532 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4536 if (asprintf(&ondisk_path, "%s/%s",
4537 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4538 err = got_error_from_errno("asprintf");
4542 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4543 status == GOT_STATUS_CONFLICT)) {
4544 int is_bad_symlink = 0;
4545 err = create_patched_content(&path_content, 1, &id,
4546 ondisk_path, dirfd, de_name, ie->path, a->repo,
4547 a->patch_cb, a->patch_arg);
4548 if (err || path_content == NULL)
4550 if (te && S_ISLNK(te->mode)) {
4551 if (unlink(path_content) == -1) {
4552 err = got_error_from_errno2("unlink",
4556 err = install_symlink(&is_bad_symlink,
4557 a->worktree, ondisk_path, ie->path,
4558 blob, 0, 1, 0, a->repo,
4559 a->progress_cb, a->progress_arg);
4561 if (rename(path_content, ondisk_path) == -1) {
4562 err = got_error_from_errno3("rename",
4563 path_content, ondisk_path);
4568 int is_bad_symlink = 0;
4569 if (te && S_ISLNK(te->mode)) {
4570 err = install_symlink(&is_bad_symlink,
4571 a->worktree, ondisk_path, ie->path,
4572 blob, 0, 1, 0, a->repo,
4573 a->progress_cb, a->progress_arg);
4575 err = install_blob(a->worktree, ondisk_path,
4577 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4578 got_fileindex_perms_to_st(ie), blob,
4579 0, 1, 0, 0, a->repo,
4580 a->progress_cb, a->progress_arg);
4584 if (status == GOT_STATUS_DELETE ||
4585 status == GOT_STATUS_MODE_CHANGE) {
4586 err = got_fileindex_entry_update(ie,
4587 ondisk_path, blob->id.sha1,
4588 a->worktree->base_commit_id->sha1, 1);
4592 if (is_bad_symlink) {
4593 got_fileindex_entry_filetype_set(ie,
4594 GOT_FILEIDX_MODE_BAD_SYMLINK);
4608 got_object_blob_close(blob);
4610 got_object_tree_close(tree);
4615 const struct got_error *
4616 got_worktree_revert(struct got_worktree *worktree,
4617 struct got_pathlist_head *paths,
4618 got_worktree_checkout_cb progress_cb, void *progress_arg,
4619 got_worktree_patch_cb patch_cb, void *patch_arg,
4620 struct got_repository *repo)
4622 struct got_fileindex *fileindex = NULL;
4623 char *fileindex_path = NULL;
4624 const struct got_error *err = NULL, *unlockerr = NULL;
4625 const struct got_error *sync_err = NULL;
4626 struct got_pathlist_entry *pe;
4627 struct revert_file_args rfa;
4629 err = lock_worktree(worktree, LOCK_EX);
4633 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4637 rfa.worktree = worktree;
4638 rfa.fileindex = fileindex;
4639 rfa.progress_cb = progress_cb;
4640 rfa.progress_arg = progress_arg;
4641 rfa.patch_cb = patch_cb;
4642 rfa.patch_arg = patch_arg;
4644 TAILQ_FOREACH(pe, paths, entry) {
4645 err = worktree_status(worktree, pe->path, fileindex, repo,
4646 revert_file, &rfa, NULL, NULL, 0, 0);
4650 sync_err = sync_fileindex(fileindex, fileindex_path);
4651 if (sync_err && err == NULL)
4654 free(fileindex_path);
4656 got_fileindex_free(fileindex);
4657 unlockerr = lock_worktree(worktree, LOCK_SH);
4658 if (unlockerr && err == NULL)
4664 free_commitable(struct got_commitable *ct)
4667 free(ct->in_repo_path);
4668 free(ct->ondisk_path);
4670 free(ct->base_blob_id);
4671 free(ct->staged_blob_id);
4672 free(ct->base_commit_id);
4676 struct collect_commitables_arg {
4677 struct got_pathlist_head *commitable_paths;
4678 struct got_repository *repo;
4679 struct got_worktree *worktree;
4680 struct got_fileindex *fileindex;
4681 int have_staged_files;
4682 int allow_bad_symlinks;
4685 static const struct got_error *
4686 collect_commitables(void *arg, unsigned char status,
4687 unsigned char staged_status, const char *relpath,
4688 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4689 struct got_object_id *commit_id, int dirfd, const char *de_name)
4691 struct collect_commitables_arg *a = arg;
4692 const struct got_error *err = NULL;
4693 struct got_commitable *ct = NULL;
4694 struct got_pathlist_entry *new = NULL;
4695 char *parent_path = NULL, *path = NULL;
4698 if (a->have_staged_files) {
4699 if (staged_status != GOT_STATUS_MODIFY &&
4700 staged_status != GOT_STATUS_ADD &&
4701 staged_status != GOT_STATUS_DELETE)
4704 if (status == GOT_STATUS_CONFLICT)
4705 return got_error(GOT_ERR_COMMIT_CONFLICT);
4707 if (status != GOT_STATUS_MODIFY &&
4708 status != GOT_STATUS_MODE_CHANGE &&
4709 status != GOT_STATUS_ADD &&
4710 status != GOT_STATUS_DELETE)
4714 if (asprintf(&path, "/%s", relpath) == -1) {
4715 err = got_error_from_errno("asprintf");
4718 if (strcmp(path, "/") == 0) {
4719 parent_path = strdup("");
4720 if (parent_path == NULL)
4721 return got_error_from_errno("strdup");
4723 err = got_path_dirname(&parent_path, path);
4728 ct = calloc(1, sizeof(*ct));
4730 err = got_error_from_errno("calloc");
4734 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4736 err = got_error_from_errno("asprintf");
4740 if (staged_status == GOT_STATUS_ADD ||
4741 staged_status == GOT_STATUS_MODIFY) {
4742 struct got_fileindex_entry *ie;
4743 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4744 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4745 case GOT_FILEIDX_MODE_REGULAR_FILE:
4746 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4749 case GOT_FILEIDX_MODE_SYMLINK:
4753 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4756 ct->mode |= got_fileindex_entry_perms_get(ie);
4757 } else if (status != GOT_STATUS_DELETE &&
4758 staged_status != GOT_STATUS_DELETE) {
4760 if (fstatat(dirfd, de_name, &sb,
4761 AT_SYMLINK_NOFOLLOW) == -1) {
4762 err = got_error_from_errno2("fstatat",
4766 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4767 err = got_error_from_errno2("lstat", ct->ondisk_path);
4770 ct->mode = sb.st_mode;
4773 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4774 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4776 err = got_error_from_errno("asprintf");
4780 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4781 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4783 char target_path[PATH_MAX];
4785 target_len = readlink(ct->ondisk_path, target_path,
4786 sizeof(target_path));
4787 if (target_len == -1) {
4788 err = got_error_from_errno2("readlink",
4792 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4793 target_len, ct->ondisk_path, a->worktree->root_path);
4796 if (is_bad_symlink) {
4797 err = got_error_path(ct->ondisk_path,
4798 GOT_ERR_BAD_SYMLINK);
4804 ct->status = status;
4805 ct->staged_status = staged_status;
4806 ct->blob_id = NULL; /* will be filled in when blob gets created */
4807 if (ct->status != GOT_STATUS_ADD &&
4808 ct->staged_status != GOT_STATUS_ADD) {
4809 ct->base_blob_id = got_object_id_dup(blob_id);
4810 if (ct->base_blob_id == NULL) {
4811 err = got_error_from_errno("got_object_id_dup");
4814 ct->base_commit_id = got_object_id_dup(commit_id);
4815 if (ct->base_commit_id == NULL) {
4816 err = got_error_from_errno("got_object_id_dup");
4820 if (ct->staged_status == GOT_STATUS_ADD ||
4821 ct->staged_status == GOT_STATUS_MODIFY) {
4822 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4823 if (ct->staged_blob_id == NULL) {
4824 err = got_error_from_errno("got_object_id_dup");
4828 ct->path = strdup(path);
4829 if (ct->path == NULL) {
4830 err = got_error_from_errno("strdup");
4833 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4835 if (ct && (err || new == NULL))
4836 free_commitable(ct);
4842 static const struct got_error *write_tree(struct got_object_id **, int *,
4843 struct got_tree_object *, const char *, struct got_pathlist_head *,
4844 got_worktree_status_cb status_cb, void *status_arg,
4845 struct got_repository *);
4847 static const struct got_error *
4848 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4849 struct got_tree_entry *te, const char *parent_path,
4850 struct got_pathlist_head *commitable_paths,
4851 got_worktree_status_cb status_cb, void *status_arg,
4852 struct got_repository *repo)
4854 const struct got_error *err = NULL;
4855 struct got_tree_object *subtree;
4858 if (asprintf(&subpath, "%s%s%s", parent_path,
4859 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4860 return got_error_from_errno("asprintf");
4862 err = got_object_open_as_tree(&subtree, repo, &te->id);
4866 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4867 commitable_paths, status_cb, status_arg, repo);
4868 got_object_tree_close(subtree);
4873 static const struct got_error *
4874 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4876 const struct got_error *err = NULL;
4877 char *ct_parent_path = NULL;
4881 if (strchr(ct->in_repo_path, '/') == NULL) {
4882 *match = got_path_is_root_dir(path);
4886 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4889 *match = (strcmp(path, ct_parent_path) == 0);
4890 free(ct_parent_path);
4895 get_ct_file_mode(struct got_commitable *ct)
4897 if (S_ISLNK(ct->mode))
4900 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4903 static const struct got_error *
4904 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4905 struct got_tree_entry *te, struct got_commitable *ct)
4907 const struct got_error *err = NULL;
4911 err = got_object_tree_entry_dup(new_te, te);
4915 (*new_te)->mode = get_ct_file_mode(ct);
4917 if (ct->staged_status == GOT_STATUS_MODIFY)
4918 memcpy(&(*new_te)->id, ct->staged_blob_id,
4919 sizeof((*new_te)->id));
4921 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4923 if (err && *new_te) {
4930 static const struct got_error *
4931 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4932 struct got_commitable *ct)
4934 const struct got_error *err = NULL;
4935 char *ct_name = NULL;
4939 *new_te = calloc(1, sizeof(**new_te));
4940 if (*new_te == NULL)
4941 return got_error_from_errno("calloc");
4943 err = got_path_basename(&ct_name, ct->path);
4946 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4947 sizeof((*new_te)->name)) {
4948 err = got_error(GOT_ERR_NO_SPACE);
4952 (*new_te)->mode = get_ct_file_mode(ct);
4954 if (ct->staged_status == GOT_STATUS_ADD)
4955 memcpy(&(*new_te)->id, ct->staged_blob_id,
4956 sizeof((*new_te)->id));
4958 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4961 if (err && *new_te) {
4968 static const struct got_error *
4969 insert_tree_entry(struct got_tree_entry *new_te,
4970 struct got_pathlist_head *paths)
4972 const struct got_error *err = NULL;
4973 struct got_pathlist_entry *new_pe;
4975 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4979 return got_error(GOT_ERR_TREE_DUP_ENTRY);
4983 static const struct got_error *
4984 report_ct_status(struct got_commitable *ct,
4985 got_worktree_status_cb status_cb, void *status_arg)
4987 const char *ct_path = ct->path;
4988 unsigned char status;
4990 while (ct_path[0] == '/')
4993 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4994 status = ct->staged_status;
4996 status = ct->status;
4998 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4999 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5002 static const struct got_error *
5003 match_modified_subtree(int *modified, struct got_tree_entry *te,
5004 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5006 const struct got_error *err = NULL;
5007 struct got_pathlist_entry *pe;
5012 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5013 got_path_is_root_dir(base_tree_path) ? "" : "/",
5015 return got_error_from_errno("asprintf");
5017 TAILQ_FOREACH(pe, commitable_paths, entry) {
5018 struct got_commitable *ct = pe->data;
5019 *modified = got_path_is_child(ct->in_repo_path, te_path,
5029 static const struct got_error *
5030 match_deleted_or_modified_ct(struct got_commitable **ctp,
5031 struct got_tree_entry *te, const char *base_tree_path,
5032 struct got_pathlist_head *commitable_paths)
5034 const struct got_error *err = NULL;
5035 struct got_pathlist_entry *pe;
5039 TAILQ_FOREACH(pe, commitable_paths, entry) {
5040 struct got_commitable *ct = pe->data;
5041 char *ct_name = NULL;
5044 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5045 if (ct->status != GOT_STATUS_MODIFY &&
5046 ct->status != GOT_STATUS_MODE_CHANGE &&
5047 ct->status != GOT_STATUS_DELETE)
5050 if (ct->staged_status != GOT_STATUS_MODIFY &&
5051 ct->staged_status != GOT_STATUS_DELETE)
5055 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5058 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5064 err = got_path_basename(&ct_name, pe->path);
5068 if (strcmp(te->name, ct_name) != 0) {
5081 static const struct got_error *
5082 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5083 const char *child_path, const char *path_base_tree,
5084 struct got_pathlist_head *commitable_paths,
5085 got_worktree_status_cb status_cb, void *status_arg,
5086 struct got_repository *repo)
5088 const struct got_error *err = NULL;
5089 struct got_tree_entry *new_te;
5091 struct got_object_id *id = NULL;
5096 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5097 got_path_is_root_dir(path_base_tree) ? "" : "/",
5099 return got_error_from_errno("asprintf");
5101 new_te = calloc(1, sizeof(*new_te));
5103 return got_error_from_errno("calloc");
5104 new_te->mode = S_IFDIR;
5106 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5107 sizeof(new_te->name)) {
5108 err = got_error(GOT_ERR_NO_SPACE);
5111 err = write_tree(&id, &nentries, NULL, subtree_path,
5112 commitable_paths, status_cb, status_arg, repo);
5117 memcpy(&new_te->id, id, sizeof(new_te->id));
5126 static const struct got_error *
5127 write_tree(struct got_object_id **new_tree_id, int *nentries,
5128 struct got_tree_object *base_tree, const char *path_base_tree,
5129 struct got_pathlist_head *commitable_paths,
5130 got_worktree_status_cb status_cb, void *status_arg,
5131 struct got_repository *repo)
5133 const struct got_error *err = NULL;
5134 struct got_pathlist_head paths;
5135 struct got_tree_entry *te, *new_te = NULL;
5136 struct got_pathlist_entry *pe;
5141 /* Insert, and recurse into, newly added entries first. */
5142 TAILQ_FOREACH(pe, commitable_paths, entry) {
5143 struct got_commitable *ct = pe->data;
5144 char *child_path = NULL, *slash;
5146 if ((ct->status != GOT_STATUS_ADD &&
5147 ct->staged_status != GOT_STATUS_ADD) ||
5148 (ct->flags & GOT_COMMITABLE_ADDED))
5151 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5152 strlen(path_base_tree)))
5155 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5160 slash = strchr(child_path, '/');
5161 if (slash == NULL) {
5162 err = alloc_added_blob_tree_entry(&new_te, ct);
5165 err = report_ct_status(ct, status_cb, status_arg);
5168 ct->flags |= GOT_COMMITABLE_ADDED;
5169 err = insert_tree_entry(new_te, &paths);
5174 *slash = '\0'; /* trim trailing path components */
5175 if (base_tree == NULL ||
5176 got_object_tree_find_entry(base_tree, child_path)
5178 err = make_subtree_for_added_blob(&new_te,
5179 child_path, path_base_tree,
5180 commitable_paths, status_cb, status_arg,
5184 err = insert_tree_entry(new_te, &paths);
5193 int i, nbase_entries;
5194 /* Handle modified and deleted entries. */
5195 nbase_entries = got_object_tree_get_nentries(base_tree);
5196 for (i = 0; i < nbase_entries; i++) {
5197 struct got_commitable *ct = NULL;
5199 te = got_object_tree_get_entry(base_tree, i);
5200 if (got_object_tree_entry_is_submodule(te)) {
5201 /* Entry is a submodule; just copy it. */
5202 err = got_object_tree_entry_dup(&new_te, te);
5205 err = insert_tree_entry(new_te, &paths);
5212 if (S_ISDIR(te->mode)) {
5214 err = got_object_tree_entry_dup(&new_te, te);
5217 err = match_modified_subtree(&modified, te,
5218 path_base_tree, commitable_paths);
5221 /* Avoid recursion into unmodified subtrees. */
5223 struct got_object_id *new_id;
5225 err = write_subtree(&new_id,
5227 path_base_tree, commitable_paths,
5228 status_cb, status_arg, repo);
5231 if (nsubentries == 0) {
5232 /* All entries were deleted. */
5236 memcpy(&new_te->id, new_id,
5237 sizeof(new_te->id));
5240 err = insert_tree_entry(new_te, &paths);
5247 err = match_deleted_or_modified_ct(&ct, te,
5248 path_base_tree, commitable_paths);
5252 /* NB: Deleted entries get dropped here. */
5253 if (ct->status == GOT_STATUS_MODIFY ||
5254 ct->status == GOT_STATUS_MODE_CHANGE ||
5255 ct->staged_status == GOT_STATUS_MODIFY) {
5256 err = alloc_modified_blob_tree_entry(
5260 err = insert_tree_entry(new_te, &paths);
5265 err = report_ct_status(ct, status_cb,
5270 /* Entry is unchanged; just copy it. */
5271 err = got_object_tree_entry_dup(&new_te, te);
5274 err = insert_tree_entry(new_te, &paths);
5282 /* Write new list of entries; deleted entries have been dropped. */
5283 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5285 got_pathlist_free(&paths);
5289 static const struct got_error *
5290 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
5291 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
5292 int have_staged_files)
5294 const struct got_error *err = NULL;
5295 struct got_pathlist_entry *pe;
5297 TAILQ_FOREACH(pe, commitable_paths, entry) {
5298 struct got_fileindex_entry *ie;
5299 struct got_commitable *ct = pe->data;
5301 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5303 if (ct->status == GOT_STATUS_DELETE ||
5304 ct->staged_status == GOT_STATUS_DELETE) {
5305 got_fileindex_entry_remove(fileindex, ie);
5306 } else if (ct->staged_status == GOT_STATUS_ADD ||
5307 ct->staged_status == GOT_STATUS_MODIFY) {
5308 got_fileindex_entry_stage_set(ie,
5309 GOT_FILEIDX_STAGE_NONE);
5310 got_fileindex_entry_staged_filetype_set(ie, 0);
5311 err = got_fileindex_entry_update(ie,
5312 ct->ondisk_path, ct->staged_blob_id->sha1,
5313 new_base_commit_id->sha1,
5314 !have_staged_files);
5316 err = got_fileindex_entry_update(ie,
5317 ct->ondisk_path, ct->blob_id->sha1,
5318 new_base_commit_id->sha1,
5319 !have_staged_files);
5321 err = got_fileindex_entry_alloc(&ie, pe->path);
5324 err = got_fileindex_entry_update(ie, ct->ondisk_path,
5325 ct->blob_id->sha1, new_base_commit_id->sha1, 1);
5327 got_fileindex_entry_free(ie);
5330 err = got_fileindex_entry_add(fileindex, ie);
5332 got_fileindex_entry_free(ie);
5341 static const struct got_error *
5342 check_out_of_date(const char *in_repo_path, unsigned char status,
5343 unsigned char staged_status, struct got_object_id *base_blob_id,
5344 struct got_object_id *base_commit_id,
5345 struct got_object_id *head_commit_id, struct got_repository *repo,
5348 const struct got_error *err = NULL;
5349 struct got_object_id *id = NULL;
5351 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5352 /* Trivial case: base commit == head commit */
5353 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5356 * Ensure file content which local changes were based
5357 * on matches file content in the branch head.
5359 err = got_object_id_by_path(&id, repo, head_commit_id,
5362 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5363 err = got_error(ood_errcode);
5365 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5366 err = got_error(ood_errcode);
5368 /* Require that added files don't exist in the branch head. */
5369 err = got_object_id_by_path(&id, repo, head_commit_id,
5371 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5373 err = id ? got_error(ood_errcode) : NULL;
5380 const struct got_error *
5381 commit_worktree(struct got_object_id **new_commit_id,
5382 struct got_pathlist_head *commitable_paths,
5383 struct got_object_id *head_commit_id, struct got_worktree *worktree,
5384 const char *author, const char *committer,
5385 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5386 got_worktree_status_cb status_cb, void *status_arg,
5387 struct got_repository *repo)
5389 const struct got_error *err = NULL, *unlockerr = NULL;
5390 struct got_pathlist_entry *pe;
5391 const char *head_ref_name = NULL;
5392 struct got_commit_object *head_commit = NULL;
5393 struct got_reference *head_ref2 = NULL;
5394 struct got_object_id *head_commit_id2 = NULL;
5395 struct got_tree_object *head_tree = NULL;
5396 struct got_object_id *new_tree_id = NULL;
5398 struct got_object_id_queue parent_ids;
5399 struct got_object_qid *pid = NULL;
5400 char *logmsg = NULL;
5402 *new_commit_id = NULL;
5404 SIMPLEQ_INIT(&parent_ids);
5406 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5410 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5414 if (commit_msg_cb != NULL) {
5415 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5420 if (logmsg == NULL || strlen(logmsg) == 0) {
5421 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5425 /* Create blobs from added and modified files and record their IDs. */
5426 TAILQ_FOREACH(pe, commitable_paths, entry) {
5427 struct got_commitable *ct = pe->data;
5430 /* Blobs for staged files already exist. */
5431 if (ct->staged_status == GOT_STATUS_ADD ||
5432 ct->staged_status == GOT_STATUS_MODIFY)
5435 if (ct->status != GOT_STATUS_ADD &&
5436 ct->status != GOT_STATUS_MODIFY &&
5437 ct->status != GOT_STATUS_MODE_CHANGE)
5440 if (asprintf(&ondisk_path, "%s/%s",
5441 worktree->root_path, pe->path) == -1) {
5442 err = got_error_from_errno("asprintf");
5445 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5451 /* Recursively write new tree objects. */
5452 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5453 commitable_paths, status_cb, status_arg, repo);
5457 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5460 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5461 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5462 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5463 got_object_qid_free(pid);
5469 /* Check if a concurrent commit to our branch has occurred. */
5470 head_ref_name = got_worktree_get_head_ref_name(worktree);
5471 if (head_ref_name == NULL) {
5472 err = got_error_from_errno("got_worktree_get_head_ref_name");
5475 /* Lock the reference here to prevent concurrent modification. */
5476 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5479 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5482 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5483 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5486 /* Update branch head in repository. */
5487 err = got_ref_change_ref(head_ref2, *new_commit_id);
5490 err = got_ref_write(head_ref2, repo);
5494 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5498 err = ref_base_commit(worktree, repo);
5503 got_object_tree_close(head_tree);
5505 got_object_commit_close(head_commit);
5506 free(head_commit_id2);
5508 unlockerr = got_ref_unlock(head_ref2);
5509 if (unlockerr && err == NULL)
5511 got_ref_close(head_ref2);
5516 static const struct got_error *
5517 check_path_is_commitable(const char *path,
5518 struct got_pathlist_head *commitable_paths)
5520 struct got_pathlist_entry *cpe = NULL;
5521 size_t path_len = strlen(path);
5523 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5524 struct got_commitable *ct = cpe->data;
5525 const char *ct_path = ct->path;
5527 while (ct_path[0] == '/')
5530 if (strcmp(path, ct_path) == 0 ||
5531 got_path_is_child(ct_path, path, path_len))
5536 return got_error_path(path, GOT_ERR_BAD_PATH);
5541 static const struct got_error *
5542 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5544 int *have_staged_files = arg;
5546 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5547 *have_staged_files = 1;
5548 return got_error(GOT_ERR_CANCELLED);
5554 static const struct got_error *
5555 check_non_staged_files(struct got_fileindex *fileindex,
5556 struct got_pathlist_head *paths)
5558 struct got_pathlist_entry *pe;
5559 struct got_fileindex_entry *ie;
5561 TAILQ_FOREACH(pe, paths, entry) {
5562 if (pe->path[0] == '\0')
5564 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5566 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5567 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5568 return got_error_path(pe->path,
5569 GOT_ERR_FILE_NOT_STAGED);
5575 const struct got_error *
5576 got_worktree_commit(struct got_object_id **new_commit_id,
5577 struct got_worktree *worktree, struct got_pathlist_head *paths,
5578 const char *author, const char *committer, int allow_bad_symlinks,
5579 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5580 got_worktree_status_cb status_cb, void *status_arg,
5581 struct got_repository *repo)
5583 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5584 struct got_fileindex *fileindex = NULL;
5585 char *fileindex_path = NULL;
5586 struct got_pathlist_head commitable_paths;
5587 struct collect_commitables_arg cc_arg;
5588 struct got_pathlist_entry *pe;
5589 struct got_reference *head_ref = NULL;
5590 struct got_object_id *head_commit_id = NULL;
5591 int have_staged_files = 0;
5593 *new_commit_id = NULL;
5595 TAILQ_INIT(&commitable_paths);
5597 err = lock_worktree(worktree, LOCK_EX);
5601 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5605 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5609 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5613 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5614 &have_staged_files);
5615 if (err && err->code != GOT_ERR_CANCELLED)
5617 if (have_staged_files) {
5618 err = check_non_staged_files(fileindex, paths);
5623 cc_arg.commitable_paths = &commitable_paths;
5624 cc_arg.worktree = worktree;
5625 cc_arg.fileindex = fileindex;
5627 cc_arg.have_staged_files = have_staged_files;
5628 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5629 TAILQ_FOREACH(pe, paths, entry) {
5630 err = worktree_status(worktree, pe->path, fileindex, repo,
5631 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5636 if (TAILQ_EMPTY(&commitable_paths)) {
5637 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5641 TAILQ_FOREACH(pe, paths, entry) {
5642 err = check_path_is_commitable(pe->path, &commitable_paths);
5647 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5648 struct got_commitable *ct = pe->data;
5649 const char *ct_path = ct->in_repo_path;
5651 while (ct_path[0] == '/')
5653 err = check_out_of_date(ct_path, ct->status,
5654 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5655 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5661 err = commit_worktree(new_commit_id, &commitable_paths,
5662 head_commit_id, worktree, author, committer,
5663 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5667 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5668 fileindex, have_staged_files);
5669 sync_err = sync_fileindex(fileindex, fileindex_path);
5670 if (sync_err && err == NULL)
5674 got_fileindex_free(fileindex);
5675 free(fileindex_path);
5676 unlockerr = lock_worktree(worktree, LOCK_SH);
5677 if (unlockerr && err == NULL)
5679 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5680 struct got_commitable *ct = pe->data;
5681 free_commitable(ct);
5683 got_pathlist_free(&commitable_paths);
5688 got_commitable_get_path(struct got_commitable *ct)
5694 got_commitable_get_status(struct got_commitable *ct)
5699 struct check_rebase_ok_arg {
5700 struct got_worktree *worktree;
5701 struct got_repository *repo;
5704 static const struct got_error *
5705 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5707 const struct got_error *err = NULL;
5708 struct check_rebase_ok_arg *a = arg;
5709 unsigned char status;
5713 /* Reject rebase of a work tree with mixed base commits. */
5714 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5715 SHA1_DIGEST_LENGTH))
5716 return got_error(GOT_ERR_MIXED_COMMITS);
5718 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5720 return got_error_from_errno("asprintf");
5722 /* Reject rebase of a work tree with modified or staged files. */
5723 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5728 if (status != GOT_STATUS_NO_CHANGE)
5729 return got_error(GOT_ERR_MODIFIED);
5730 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5731 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5736 const struct got_error *
5737 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5738 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5739 struct got_worktree *worktree, struct got_reference *branch,
5740 struct got_repository *repo)
5742 const struct got_error *err = NULL;
5743 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5744 char *branch_ref_name = NULL;
5745 char *fileindex_path = NULL;
5746 struct check_rebase_ok_arg ok_arg;
5747 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5748 struct got_object_id *wt_branch_tip = NULL;
5750 *new_base_branch_ref = NULL;
5754 err = lock_worktree(worktree, LOCK_EX);
5758 err = open_fileindex(fileindex, &fileindex_path, worktree);
5762 ok_arg.worktree = worktree;
5764 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5769 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5773 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5777 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5781 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5786 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5789 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5790 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5794 err = got_ref_alloc_symref(new_base_branch_ref,
5795 new_base_branch_ref_name, wt_branch);
5798 err = got_ref_write(*new_base_branch_ref, repo);
5802 /* TODO Lock original branch's ref while rebasing? */
5804 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5808 err = got_ref_write(branch_ref, repo);
5812 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5813 worktree->base_commit_id);
5816 err = got_ref_write(*tmp_branch, repo);
5820 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5824 free(fileindex_path);
5825 free(tmp_branch_name);
5826 free(new_base_branch_ref_name);
5827 free(branch_ref_name);
5829 got_ref_close(branch_ref);
5831 got_ref_close(wt_branch);
5832 free(wt_branch_tip);
5834 if (*new_base_branch_ref) {
5835 got_ref_close(*new_base_branch_ref);
5836 *new_base_branch_ref = NULL;
5839 got_ref_close(*tmp_branch);
5843 got_fileindex_free(*fileindex);
5846 lock_worktree(worktree, LOCK_SH);
5851 const struct got_error *
5852 got_worktree_rebase_continue(struct got_object_id **commit_id,
5853 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5854 struct got_reference **branch, struct got_fileindex **fileindex,
5855 struct got_worktree *worktree, struct got_repository *repo)
5857 const struct got_error *err;
5858 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5859 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5860 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5861 char *fileindex_path = NULL;
5862 int have_staged_files = 0;
5865 *new_base_branch = NULL;
5870 err = lock_worktree(worktree, LOCK_EX);
5874 err = open_fileindex(fileindex, &fileindex_path, worktree);
5878 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5879 &have_staged_files);
5880 if (err && err->code != GOT_ERR_CANCELLED)
5882 if (have_staged_files) {
5883 err = got_error(GOT_ERR_STAGED_PATHS);
5887 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5891 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5895 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5899 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5903 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5907 err = got_ref_open(branch, repo,
5908 got_ref_get_symref_target(branch_ref), 0);
5912 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5916 err = got_ref_resolve(commit_id, repo, commit_ref);
5920 err = got_ref_open(new_base_branch, repo,
5921 new_base_branch_ref_name, 0);
5925 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5929 free(commit_ref_name);
5930 free(branch_ref_name);
5931 free(fileindex_path);
5933 got_ref_close(commit_ref);
5935 got_ref_close(branch_ref);
5940 got_ref_close(*tmp_branch);
5943 if (*new_base_branch) {
5944 got_ref_close(*new_base_branch);
5945 *new_base_branch = NULL;
5948 got_ref_close(*branch);
5952 got_fileindex_free(*fileindex);
5955 lock_worktree(worktree, LOCK_SH);
5960 const struct got_error *
5961 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5963 const struct got_error *err;
5964 char *tmp_branch_name = NULL;
5966 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5970 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5971 free(tmp_branch_name);
5975 static const struct got_error *
5976 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5977 char **logmsg, void *arg)
5983 static const struct got_error *
5984 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5985 const char *path, struct got_object_id *blob_id,
5986 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5987 int dirfd, const char *de_name)
5992 struct collect_merged_paths_arg {
5993 got_worktree_checkout_cb progress_cb;
5995 struct got_pathlist_head *merged_paths;
5998 static const struct got_error *
5999 collect_merged_paths(void *arg, unsigned char status, const char *path)
6001 const struct got_error *err;
6002 struct collect_merged_paths_arg *a = arg;
6004 struct got_pathlist_entry *new;
6006 err = (*a->progress_cb)(a->progress_arg, status, path);
6010 if (status != GOT_STATUS_MERGE &&
6011 status != GOT_STATUS_ADD &&
6012 status != GOT_STATUS_DELETE &&
6013 status != GOT_STATUS_CONFLICT)
6018 return got_error_from_errno("strdup");
6020 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6021 if (err || new == NULL)
6027 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6029 struct got_pathlist_entry *pe;
6031 TAILQ_FOREACH(pe, merged_paths, entry)
6032 free((char *)pe->path);
6034 got_pathlist_free(merged_paths);
6037 static const struct got_error *
6038 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6039 int is_rebase, struct got_repository *repo)
6041 const struct got_error *err;
6042 struct got_reference *commit_ref = NULL;
6044 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6046 if (err->code != GOT_ERR_NOT_REF)
6048 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6051 err = got_ref_write(commit_ref, repo);
6054 } else if (is_rebase) {
6055 struct got_object_id *stored_id;
6058 err = got_ref_resolve(&stored_id, repo, commit_ref);
6061 cmp = got_object_id_cmp(commit_id, stored_id);
6064 err = got_error(GOT_ERR_REBASE_COMMITID);
6070 got_ref_close(commit_ref);
6074 static const struct got_error *
6075 rebase_merge_files(struct got_pathlist_head *merged_paths,
6076 const char *commit_ref_name, struct got_worktree *worktree,
6077 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6078 struct got_object_id *commit_id, struct got_repository *repo,
6079 got_worktree_checkout_cb progress_cb, void *progress_arg,
6080 got_cancel_cb cancel_cb, void *cancel_arg)
6082 const struct got_error *err;
6083 struct got_reference *commit_ref = NULL;
6084 struct collect_merged_paths_arg cmp_arg;
6085 char *fileindex_path;
6087 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6089 err = get_fileindex_path(&fileindex_path, worktree);
6093 cmp_arg.progress_cb = progress_cb;
6094 cmp_arg.progress_arg = progress_arg;
6095 cmp_arg.merged_paths = merged_paths;
6096 err = merge_files(worktree, fileindex, fileindex_path,
6097 parent_commit_id, commit_id, repo, collect_merged_paths,
6098 &cmp_arg, cancel_cb, cancel_arg);
6100 got_ref_close(commit_ref);
6104 const struct got_error *
6105 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6106 struct got_worktree *worktree, struct got_fileindex *fileindex,
6107 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6108 struct got_repository *repo,
6109 got_worktree_checkout_cb progress_cb, void *progress_arg,
6110 got_cancel_cb cancel_cb, void *cancel_arg)
6112 const struct got_error *err;
6113 char *commit_ref_name;
6115 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6119 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6123 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6124 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6125 progress_arg, cancel_cb, cancel_arg);
6127 free(commit_ref_name);
6131 const struct got_error *
6132 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6133 struct got_worktree *worktree, struct got_fileindex *fileindex,
6134 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6135 struct got_repository *repo,
6136 got_worktree_checkout_cb progress_cb, void *progress_arg,
6137 got_cancel_cb cancel_cb, void *cancel_arg)
6139 const struct got_error *err;
6140 char *commit_ref_name;
6142 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6146 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6150 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6151 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6152 progress_arg, cancel_cb, cancel_arg);
6154 free(commit_ref_name);
6158 static const struct got_error *
6159 rebase_commit(struct got_object_id **new_commit_id,
6160 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6161 struct got_worktree *worktree, struct got_fileindex *fileindex,
6162 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6163 const char *new_logmsg, struct got_repository *repo)
6165 const struct got_error *err, *sync_err;
6166 struct got_pathlist_head commitable_paths;
6167 struct collect_commitables_arg cc_arg;
6168 char *fileindex_path = NULL;
6169 struct got_reference *head_ref = NULL;
6170 struct got_object_id *head_commit_id = NULL;
6171 char *logmsg = NULL;
6173 TAILQ_INIT(&commitable_paths);
6174 *new_commit_id = NULL;
6176 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6178 err = get_fileindex_path(&fileindex_path, worktree);
6182 cc_arg.commitable_paths = &commitable_paths;
6183 cc_arg.worktree = worktree;
6185 cc_arg.have_staged_files = 0;
6187 * If possible get the status of individual files directly to
6188 * avoid crawling the entire work tree once per rebased commit.
6189 * TODO: Ideally, merged_paths would contain a list of commitables
6190 * we could use so we could skip worktree_status() entirely.
6193 struct got_pathlist_entry *pe;
6194 TAILQ_FOREACH(pe, merged_paths, entry) {
6195 err = worktree_status(worktree, pe->path, fileindex,
6196 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6202 err = worktree_status(worktree, "", fileindex, repo,
6203 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6208 if (TAILQ_EMPTY(&commitable_paths)) {
6209 /* No-op change; commit will be elided. */
6210 err = got_ref_delete(commit_ref, repo);
6213 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6217 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6221 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6226 logmsg = strdup(new_logmsg);
6227 if (logmsg == NULL) {
6228 err = got_error_from_errno("strdup");
6232 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6237 /* NB: commit_worktree will call free(logmsg) */
6238 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6239 worktree, got_object_commit_get_author(orig_commit),
6240 got_object_commit_get_committer(orig_commit),
6241 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6245 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6249 err = got_ref_delete(commit_ref, repo);
6253 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
6255 sync_err = sync_fileindex(fileindex, fileindex_path);
6256 if (sync_err && err == NULL)
6259 free(fileindex_path);
6260 free(head_commit_id);
6262 got_ref_close(head_ref);
6264 free(*new_commit_id);
6265 *new_commit_id = NULL;
6270 const struct got_error *
6271 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6272 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6273 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6274 struct got_commit_object *orig_commit,
6275 struct got_object_id *orig_commit_id, struct got_repository *repo)
6277 const struct got_error *err;
6278 char *commit_ref_name;
6279 struct got_reference *commit_ref = NULL;
6280 struct got_object_id *commit_id = NULL;
6282 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6286 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6289 err = got_ref_resolve(&commit_id, repo, commit_ref);
6292 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6293 err = got_error(GOT_ERR_REBASE_COMMITID);
6297 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6298 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6301 got_ref_close(commit_ref);
6302 free(commit_ref_name);
6307 const struct got_error *
6308 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6309 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6310 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6311 struct got_commit_object *orig_commit,
6312 struct got_object_id *orig_commit_id, const char *new_logmsg,
6313 struct got_repository *repo)
6315 const struct got_error *err;
6316 char *commit_ref_name;
6317 struct got_reference *commit_ref = NULL;
6319 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6323 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6327 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6328 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6331 got_ref_close(commit_ref);
6332 free(commit_ref_name);
6336 const struct got_error *
6337 got_worktree_rebase_postpone(struct got_worktree *worktree,
6338 struct got_fileindex *fileindex)
6341 got_fileindex_free(fileindex);
6342 return lock_worktree(worktree, LOCK_SH);
6345 static const struct got_error *
6346 delete_ref(const char *name, struct got_repository *repo)
6348 const struct got_error *err;
6349 struct got_reference *ref;
6351 err = got_ref_open(&ref, repo, name, 0);
6353 if (err->code == GOT_ERR_NOT_REF)
6358 err = got_ref_delete(ref, repo);
6363 static const struct got_error *
6364 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6366 const struct got_error *err;
6367 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6368 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6370 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6373 err = delete_ref(tmp_branch_name, repo);
6377 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6380 err = delete_ref(new_base_branch_ref_name, repo);
6384 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6387 err = delete_ref(branch_ref_name, repo);
6391 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6394 err = delete_ref(commit_ref_name, repo);
6399 free(tmp_branch_name);
6400 free(new_base_branch_ref_name);
6401 free(branch_ref_name);
6402 free(commit_ref_name);
6406 const struct got_error *
6407 got_worktree_rebase_complete(struct got_worktree *worktree,
6408 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6409 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6410 struct got_repository *repo)
6412 const struct got_error *err, *unlockerr;
6413 struct got_object_id *new_head_commit_id = NULL;
6415 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6419 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6423 err = got_ref_write(rebased_branch, repo);
6427 err = got_worktree_set_head_ref(worktree, rebased_branch);
6431 err = delete_rebase_refs(worktree, repo);
6434 got_fileindex_free(fileindex);
6435 free(new_head_commit_id);
6436 unlockerr = lock_worktree(worktree, LOCK_SH);
6437 if (unlockerr && err == NULL)
6442 const struct got_error *
6443 got_worktree_rebase_abort(struct got_worktree *worktree,
6444 struct got_fileindex *fileindex, struct got_repository *repo,
6445 struct got_reference *new_base_branch,
6446 got_worktree_checkout_cb progress_cb, void *progress_arg)
6448 const struct got_error *err, *unlockerr, *sync_err;
6449 struct got_reference *resolved = NULL;
6450 struct got_object_id *commit_id = NULL;
6451 char *fileindex_path = NULL;
6452 struct revert_file_args rfa;
6453 struct got_object_id *tree_id = NULL;
6455 err = lock_worktree(worktree, LOCK_EX);
6459 err = got_ref_open(&resolved, repo,
6460 got_ref_get_symref_target(new_base_branch), 0);
6464 err = got_worktree_set_head_ref(worktree, resolved);
6469 * XXX commits to the base branch could have happened while
6470 * we were busy rebasing; should we store the original commit ID
6471 * when rebase begins and read it back here?
6473 err = got_ref_resolve(&commit_id, repo, resolved);
6477 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6481 err = got_object_id_by_path(&tree_id, repo,
6482 worktree->base_commit_id, worktree->path_prefix);
6486 err = delete_rebase_refs(worktree, repo);
6490 err = get_fileindex_path(&fileindex_path, worktree);
6494 rfa.worktree = worktree;
6495 rfa.fileindex = fileindex;
6496 rfa.progress_cb = progress_cb;
6497 rfa.progress_arg = progress_arg;
6498 rfa.patch_cb = NULL;
6499 rfa.patch_arg = NULL;
6501 err = worktree_status(worktree, "", fileindex, repo,
6502 revert_file, &rfa, NULL, NULL, 0, 0);
6506 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6507 repo, progress_cb, progress_arg, NULL, NULL);
6509 sync_err = sync_fileindex(fileindex, fileindex_path);
6510 if (sync_err && err == NULL)
6513 got_ref_close(resolved);
6517 got_fileindex_free(fileindex);
6518 free(fileindex_path);
6520 unlockerr = lock_worktree(worktree, LOCK_SH);
6521 if (unlockerr && err == NULL)
6526 const struct got_error *
6527 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6528 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6529 struct got_fileindex **fileindex, struct got_worktree *worktree,
6530 struct got_repository *repo)
6532 const struct got_error *err = NULL;
6533 char *tmp_branch_name = NULL;
6534 char *branch_ref_name = NULL;
6535 char *base_commit_ref_name = NULL;
6536 char *fileindex_path = NULL;
6537 struct check_rebase_ok_arg ok_arg;
6538 struct got_reference *wt_branch = NULL;
6539 struct got_reference *base_commit_ref = NULL;
6543 *base_commit_id = NULL;
6546 err = lock_worktree(worktree, LOCK_EX);
6550 err = open_fileindex(fileindex, &fileindex_path, worktree);
6554 ok_arg.worktree = worktree;
6556 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6561 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6565 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6569 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6574 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6579 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6583 err = got_ref_write(*branch_ref, repo);
6587 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6588 worktree->base_commit_id);
6591 err = got_ref_write(base_commit_ref, repo);
6594 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6595 if (*base_commit_id == NULL) {
6596 err = got_error_from_errno("got_object_id_dup");
6600 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6601 worktree->base_commit_id);
6604 err = got_ref_write(*tmp_branch, repo);
6608 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6612 free(fileindex_path);
6613 free(tmp_branch_name);
6614 free(branch_ref_name);
6615 free(base_commit_ref_name);
6617 got_ref_close(wt_branch);
6620 got_ref_close(*branch_ref);
6624 got_ref_close(*tmp_branch);
6627 free(*base_commit_id);
6629 got_fileindex_free(*fileindex);
6632 lock_worktree(worktree, LOCK_SH);
6637 const struct got_error *
6638 got_worktree_histedit_postpone(struct got_worktree *worktree,
6639 struct got_fileindex *fileindex)
6642 got_fileindex_free(fileindex);
6643 return lock_worktree(worktree, LOCK_SH);
6646 const struct got_error *
6647 got_worktree_histedit_in_progress(int *in_progress,
6648 struct got_worktree *worktree)
6650 const struct got_error *err;
6651 char *tmp_branch_name = NULL;
6653 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6657 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6658 free(tmp_branch_name);
6662 const struct got_error *
6663 got_worktree_histedit_continue(struct got_object_id **commit_id,
6664 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6665 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6666 struct got_worktree *worktree, struct got_repository *repo)
6668 const struct got_error *err;
6669 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6670 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6671 struct got_reference *commit_ref = NULL;
6672 struct got_reference *base_commit_ref = NULL;
6673 char *fileindex_path = NULL;
6674 int have_staged_files = 0;
6678 *base_commit_id = NULL;
6681 err = lock_worktree(worktree, LOCK_EX);
6685 err = open_fileindex(fileindex, &fileindex_path, worktree);
6689 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6690 &have_staged_files);
6691 if (err && err->code != GOT_ERR_CANCELLED)
6693 if (have_staged_files) {
6694 err = got_error(GOT_ERR_STAGED_PATHS);
6698 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6702 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6706 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6710 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6715 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6719 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6722 err = got_ref_resolve(commit_id, repo, commit_ref);
6726 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6729 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6733 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6737 free(commit_ref_name);
6738 free(branch_ref_name);
6739 free(fileindex_path);
6741 got_ref_close(commit_ref);
6742 if (base_commit_ref)
6743 got_ref_close(base_commit_ref);
6747 free(*base_commit_id);
6748 *base_commit_id = NULL;
6750 got_ref_close(*tmp_branch);
6754 got_fileindex_free(*fileindex);
6757 lock_worktree(worktree, LOCK_EX);
6762 static const struct got_error *
6763 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6765 const struct got_error *err;
6766 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6767 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6769 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6772 err = delete_ref(tmp_branch_name, repo);
6776 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6780 err = delete_ref(base_commit_ref_name, repo);
6784 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6787 err = delete_ref(branch_ref_name, repo);
6791 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6794 err = delete_ref(commit_ref_name, repo);
6798 free(tmp_branch_name);
6799 free(base_commit_ref_name);
6800 free(branch_ref_name);
6801 free(commit_ref_name);
6805 const struct got_error *
6806 got_worktree_histedit_abort(struct got_worktree *worktree,
6807 struct got_fileindex *fileindex, struct got_repository *repo,
6808 struct got_reference *branch, struct got_object_id *base_commit_id,
6809 got_worktree_checkout_cb progress_cb, void *progress_arg)
6811 const struct got_error *err, *unlockerr, *sync_err;
6812 struct got_reference *resolved = NULL;
6813 char *fileindex_path = NULL;
6814 struct got_object_id *tree_id = NULL;
6815 struct revert_file_args rfa;
6817 err = lock_worktree(worktree, LOCK_EX);
6821 err = got_ref_open(&resolved, repo,
6822 got_ref_get_symref_target(branch), 0);
6826 err = got_worktree_set_head_ref(worktree, resolved);
6830 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6834 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6835 worktree->path_prefix);
6839 err = delete_histedit_refs(worktree, repo);
6843 err = get_fileindex_path(&fileindex_path, worktree);
6847 rfa.worktree = worktree;
6848 rfa.fileindex = fileindex;
6849 rfa.progress_cb = progress_cb;
6850 rfa.progress_arg = progress_arg;
6851 rfa.patch_cb = NULL;
6852 rfa.patch_arg = NULL;
6854 err = worktree_status(worktree, "", fileindex, repo,
6855 revert_file, &rfa, NULL, NULL, 0, 0);
6859 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6860 repo, progress_cb, progress_arg, NULL, NULL);
6862 sync_err = sync_fileindex(fileindex, fileindex_path);
6863 if (sync_err && err == NULL)
6866 got_ref_close(resolved);
6868 free(fileindex_path);
6870 unlockerr = lock_worktree(worktree, LOCK_SH);
6871 if (unlockerr && err == NULL)
6876 const struct got_error *
6877 got_worktree_histedit_complete(struct got_worktree *worktree,
6878 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6879 struct got_reference *edited_branch, struct got_repository *repo)
6881 const struct got_error *err, *unlockerr;
6882 struct got_object_id *new_head_commit_id = NULL;
6883 struct got_reference *resolved = NULL;
6885 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6889 err = got_ref_open(&resolved, repo,
6890 got_ref_get_symref_target(edited_branch), 0);
6894 err = got_ref_change_ref(resolved, new_head_commit_id);
6898 err = got_ref_write(resolved, repo);
6902 err = got_worktree_set_head_ref(worktree, resolved);
6906 err = delete_histedit_refs(worktree, repo);
6909 got_fileindex_free(fileindex);
6910 free(new_head_commit_id);
6911 unlockerr = lock_worktree(worktree, LOCK_SH);
6912 if (unlockerr && err == NULL)
6917 const struct got_error *
6918 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6919 struct got_object_id *commit_id, struct got_repository *repo)
6921 const struct got_error *err;
6922 char *commit_ref_name;
6924 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6928 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6932 err = delete_ref(commit_ref_name, repo);
6934 free(commit_ref_name);
6938 const struct got_error *
6939 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6940 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6941 struct got_worktree *worktree, const char *refname,
6942 struct got_repository *repo)
6944 const struct got_error *err = NULL;
6945 char *fileindex_path = NULL;
6946 struct check_rebase_ok_arg ok_arg;
6950 *base_branch_ref = NULL;
6952 err = lock_worktree(worktree, LOCK_EX);
6956 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6957 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6958 "cannot integrate a branch into itself; "
6959 "update -b or different branch name required");
6963 err = open_fileindex(fileindex, &fileindex_path, worktree);
6967 /* Preconditions are the same as for rebase. */
6968 ok_arg.worktree = worktree;
6970 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6975 err = got_ref_open(branch_ref, repo, refname, 1);
6979 err = got_ref_open(base_branch_ref, repo,
6980 got_worktree_get_head_ref_name(worktree), 1);
6984 got_ref_close(*branch_ref);
6987 if (*base_branch_ref) {
6988 got_ref_close(*base_branch_ref);
6989 *base_branch_ref = NULL;
6992 got_fileindex_free(*fileindex);
6995 lock_worktree(worktree, LOCK_SH);
7000 const struct got_error *
7001 got_worktree_integrate_continue(struct got_worktree *worktree,
7002 struct got_fileindex *fileindex, struct got_repository *repo,
7003 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7004 got_worktree_checkout_cb progress_cb, void *progress_arg,
7005 got_cancel_cb cancel_cb, void *cancel_arg)
7007 const struct got_error *err = NULL, *sync_err, *unlockerr;
7008 char *fileindex_path = NULL;
7009 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7011 err = get_fileindex_path(&fileindex_path, worktree);
7015 err = got_ref_resolve(&commit_id, repo, branch_ref);
7019 err = got_object_id_by_path(&tree_id, repo, commit_id,
7020 worktree->path_prefix);
7024 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7028 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7029 progress_cb, progress_arg, cancel_cb, cancel_arg);
7033 err = got_ref_change_ref(base_branch_ref, commit_id);
7037 err = got_ref_write(base_branch_ref, repo);
7039 sync_err = sync_fileindex(fileindex, fileindex_path);
7040 if (sync_err && err == NULL)
7044 unlockerr = got_ref_unlock(branch_ref);
7045 if (unlockerr && err == NULL)
7047 got_ref_close(branch_ref);
7049 unlockerr = got_ref_unlock(base_branch_ref);
7050 if (unlockerr && err == NULL)
7052 got_ref_close(base_branch_ref);
7054 got_fileindex_free(fileindex);
7055 free(fileindex_path);
7058 unlockerr = lock_worktree(worktree, LOCK_SH);
7059 if (unlockerr && err == NULL)
7064 const struct got_error *
7065 got_worktree_integrate_abort(struct got_worktree *worktree,
7066 struct got_fileindex *fileindex, struct got_repository *repo,
7067 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7069 const struct got_error *err = NULL, *unlockerr = NULL;
7071 got_fileindex_free(fileindex);
7073 err = lock_worktree(worktree, LOCK_SH);
7075 unlockerr = got_ref_unlock(branch_ref);
7076 if (unlockerr && err == NULL)
7078 got_ref_close(branch_ref);
7080 unlockerr = got_ref_unlock(base_branch_ref);
7081 if (unlockerr && err == NULL)
7083 got_ref_close(base_branch_ref);
7088 struct check_stage_ok_arg {
7089 struct got_object_id *head_commit_id;
7090 struct got_worktree *worktree;
7091 struct got_fileindex *fileindex;
7092 struct got_repository *repo;
7096 const struct got_error *
7097 check_stage_ok(void *arg, unsigned char status,
7098 unsigned char staged_status, const char *relpath,
7099 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7100 struct got_object_id *commit_id, int dirfd, const char *de_name)
7102 struct check_stage_ok_arg *a = arg;
7103 const struct got_error *err = NULL;
7104 struct got_fileindex_entry *ie;
7105 struct got_object_id base_commit_id;
7106 struct got_object_id *base_commit_idp = NULL;
7107 char *in_repo_path = NULL, *p;
7109 if (status == GOT_STATUS_UNVERSIONED ||
7110 status == GOT_STATUS_NO_CHANGE)
7112 if (status == GOT_STATUS_NONEXISTENT)
7113 return got_error_set_errno(ENOENT, relpath);
7115 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7117 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7119 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7120 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7122 return got_error_from_errno("asprintf");
7124 if (got_fileindex_entry_has_commit(ie)) {
7125 memcpy(base_commit_id.sha1, ie->commit_sha1,
7126 SHA1_DIGEST_LENGTH);
7127 base_commit_idp = &base_commit_id;
7130 if (status == GOT_STATUS_CONFLICT) {
7131 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7133 } else if (status != GOT_STATUS_ADD &&
7134 status != GOT_STATUS_MODIFY &&
7135 status != GOT_STATUS_DELETE) {
7136 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7140 a->have_changes = 1;
7145 err = check_out_of_date(p, status, staged_status,
7146 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7147 GOT_ERR_STAGE_OUT_OF_DATE);
7153 struct stage_path_arg {
7154 struct got_worktree *worktree;
7155 struct got_fileindex *fileindex;
7156 struct got_repository *repo;
7157 got_worktree_status_cb status_cb;
7159 got_worktree_patch_cb patch_cb;
7161 int staged_something;
7162 int allow_bad_symlinks;
7165 static const struct got_error *
7166 stage_path(void *arg, unsigned char status,
7167 unsigned char staged_status, const char *relpath,
7168 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7169 struct got_object_id *commit_id, int dirfd, const char *de_name)
7171 struct stage_path_arg *a = arg;
7172 const struct got_error *err = NULL;
7173 struct got_fileindex_entry *ie;
7174 char *ondisk_path = NULL, *path_content = NULL;
7176 struct got_object_id *new_staged_blob_id = NULL;
7179 if (status == GOT_STATUS_UNVERSIONED)
7182 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7184 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7186 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7188 return got_error_from_errno("asprintf");
7191 case GOT_STATUS_ADD:
7192 case GOT_STATUS_MODIFY:
7193 /* XXX could sb.st_mode be passed in by our caller? */
7194 if (lstat(ondisk_path, &sb) == -1) {
7195 err = got_error_from_errno2("lstat", ondisk_path);
7199 if (status == GOT_STATUS_ADD) {
7200 int choice = GOT_PATCH_CHOICE_NONE;
7201 err = (*a->patch_cb)(&choice, a->patch_arg,
7202 status, ie->path, NULL, 1, 1);
7205 if (choice != GOT_PATCH_CHOICE_YES)
7208 err = create_patched_content(&path_content, 0,
7209 staged_blob_id ? staged_blob_id : blob_id,
7210 ondisk_path, dirfd, de_name, ie->path,
7211 a->repo, a->patch_cb, a->patch_arg);
7212 if (err || path_content == NULL)
7216 err = got_object_blob_create(&new_staged_blob_id,
7217 path_content ? path_content : ondisk_path, a->repo);
7220 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7221 SHA1_DIGEST_LENGTH);
7222 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7223 stage = GOT_FILEIDX_STAGE_ADD;
7225 stage = GOT_FILEIDX_STAGE_MODIFY;
7226 got_fileindex_entry_stage_set(ie, stage);
7227 if (S_ISLNK(sb.st_mode)) {
7228 int is_bad_symlink = 0;
7229 if (!a->allow_bad_symlinks) {
7230 char target_path[PATH_MAX];
7232 target_len = readlink(ondisk_path, target_path,
7233 sizeof(target_path));
7234 if (target_len == -1) {
7235 err = got_error_from_errno2("readlink",
7239 err = is_bad_symlink_target(&is_bad_symlink,
7240 target_path, target_len, ondisk_path,
7241 a->worktree->root_path);
7244 if (is_bad_symlink) {
7245 err = got_error_path(ondisk_path,
7246 GOT_ERR_BAD_SYMLINK);
7251 got_fileindex_entry_staged_filetype_set(ie,
7252 GOT_FILEIDX_MODE_BAD_SYMLINK);
7254 got_fileindex_entry_staged_filetype_set(ie,
7255 GOT_FILEIDX_MODE_SYMLINK);
7257 got_fileindex_entry_staged_filetype_set(ie,
7258 GOT_FILEIDX_MODE_REGULAR_FILE);
7260 a->staged_something = 1;
7261 if (a->status_cb == NULL)
7263 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7264 get_staged_status(ie), relpath, blob_id,
7265 new_staged_blob_id, NULL, dirfd, de_name);
7267 case GOT_STATUS_DELETE:
7268 if (staged_status == GOT_STATUS_DELETE)
7271 int choice = GOT_PATCH_CHOICE_NONE;
7272 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7273 ie->path, NULL, 1, 1);
7276 if (choice == GOT_PATCH_CHOICE_NO)
7278 if (choice != GOT_PATCH_CHOICE_YES) {
7279 err = got_error(GOT_ERR_PATCH_CHOICE);
7283 stage = GOT_FILEIDX_STAGE_DELETE;
7284 got_fileindex_entry_stage_set(ie, stage);
7285 a->staged_something = 1;
7286 if (a->status_cb == NULL)
7288 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7289 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7292 case GOT_STATUS_NO_CHANGE:
7294 case GOT_STATUS_CONFLICT:
7295 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7297 case GOT_STATUS_NONEXISTENT:
7298 err = got_error_set_errno(ENOENT, relpath);
7301 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7305 if (path_content && unlink(path_content) == -1 && err == NULL)
7306 err = got_error_from_errno2("unlink", path_content);
7309 free(new_staged_blob_id);
7313 const struct got_error *
7314 got_worktree_stage(struct got_worktree *worktree,
7315 struct got_pathlist_head *paths,
7316 got_worktree_status_cb status_cb, void *status_arg,
7317 got_worktree_patch_cb patch_cb, void *patch_arg,
7318 int allow_bad_symlinks, struct got_repository *repo)
7320 const struct got_error *err = NULL, *sync_err, *unlockerr;
7321 struct got_pathlist_entry *pe;
7322 struct got_fileindex *fileindex = NULL;
7323 char *fileindex_path = NULL;
7324 struct got_reference *head_ref = NULL;
7325 struct got_object_id *head_commit_id = NULL;
7326 struct check_stage_ok_arg oka;
7327 struct stage_path_arg spa;
7329 err = lock_worktree(worktree, LOCK_EX);
7333 err = got_ref_open(&head_ref, repo,
7334 got_worktree_get_head_ref_name(worktree), 0);
7337 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7340 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7344 /* Check pre-conditions before staging anything. */
7345 oka.head_commit_id = head_commit_id;
7346 oka.worktree = worktree;
7347 oka.fileindex = fileindex;
7349 oka.have_changes = 0;
7350 TAILQ_FOREACH(pe, paths, entry) {
7351 err = worktree_status(worktree, pe->path, fileindex, repo,
7352 check_stage_ok, &oka, NULL, NULL, 0, 0);
7356 if (!oka.have_changes) {
7357 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7361 spa.worktree = worktree;
7362 spa.fileindex = fileindex;
7364 spa.patch_cb = patch_cb;
7365 spa.patch_arg = patch_arg;
7366 spa.status_cb = status_cb;
7367 spa.status_arg = status_arg;
7368 spa.staged_something = 0;
7369 spa.allow_bad_symlinks = allow_bad_symlinks;
7370 TAILQ_FOREACH(pe, paths, entry) {
7371 err = worktree_status(worktree, pe->path, fileindex, repo,
7372 stage_path, &spa, NULL, NULL, 0, 0);
7376 if (!spa.staged_something) {
7377 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7381 sync_err = sync_fileindex(fileindex, fileindex_path);
7382 if (sync_err && err == NULL)
7386 got_ref_close(head_ref);
7387 free(head_commit_id);
7388 free(fileindex_path);
7390 got_fileindex_free(fileindex);
7391 unlockerr = lock_worktree(worktree, LOCK_SH);
7392 if (unlockerr && err == NULL)
7397 struct unstage_path_arg {
7398 struct got_worktree *worktree;
7399 struct got_fileindex *fileindex;
7400 struct got_repository *repo;
7401 got_worktree_checkout_cb progress_cb;
7403 got_worktree_patch_cb patch_cb;
7407 static const struct got_error *
7408 create_unstaged_content(char **path_unstaged_content,
7409 char **path_new_staged_content, struct got_object_id *blob_id,
7410 struct got_object_id *staged_blob_id, const char *relpath,
7411 struct got_repository *repo,
7412 got_worktree_patch_cb patch_cb, void *patch_arg)
7414 const struct got_error *err, *free_err;
7415 struct got_blob_object *blob = NULL, *staged_blob = NULL;
7416 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7417 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7418 struct got_diffreg_result *diffreg_result = NULL;
7419 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
7420 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
7422 *path_unstaged_content = NULL;
7423 *path_new_staged_content = NULL;
7425 err = got_object_id_str(&label1, blob_id);
7428 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7432 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7436 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7440 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7444 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7448 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7452 err = got_diff_files(&diffreg_result, f1, label1, f2,
7453 path2, 3, 0, 1, NULL);
7457 err = got_opentemp_named(path_unstaged_content, &outfile,
7458 "got-unstaged-content");
7461 err = got_opentemp_named(path_new_staged_content, &rejectfile,
7462 "got-new-staged-content");
7466 if (fseek(f1, 0L, SEEK_SET) == -1) {
7467 err = got_ferror(f1, GOT_ERR_IO);
7470 if (fseek(f2, 0L, SEEK_SET) == -1) {
7471 err = got_ferror(f2, GOT_ERR_IO);
7474 /* Count the number of actual changes in the diff result. */
7475 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
7476 struct diff_chunk_context cc = {};
7477 diff_chunk_context_load_change(&cc, &nchunks_used,
7478 diffreg_result->result, n, 0);
7481 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
7483 err = apply_or_reject_change(&choice, &nchunks_used,
7484 diffreg_result->result, n, relpath, f1, f2,
7485 &line_cur1, &line_cur2,
7486 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
7489 if (choice == GOT_PATCH_CHOICE_YES)
7492 have_rejected_content = 1;
7493 if (choice == GOT_PATCH_CHOICE_QUIT)
7496 if (have_content || have_rejected_content)
7497 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7498 outfile, rejectfile);
7502 got_object_blob_close(blob);
7504 got_object_blob_close(staged_blob);
7505 free_err = got_diffreg_result_free(diffreg_result);
7506 if (free_err && err == NULL)
7508 if (f1 && fclose(f1) == EOF && err == NULL)
7509 err = got_error_from_errno2("fclose", path1);
7510 if (f2 && fclose(f2) == EOF && err == NULL)
7511 err = got_error_from_errno2("fclose", path2);
7512 if (outfile && fclose(outfile) == EOF && err == NULL)
7513 err = got_error_from_errno2("fclose", *path_unstaged_content);
7514 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7515 err = got_error_from_errno2("fclose", *path_new_staged_content);
7516 if (path1 && unlink(path1) == -1 && err == NULL)
7517 err = got_error_from_errno2("unlink", path1);
7518 if (path2 && unlink(path2) == -1 && err == NULL)
7519 err = got_error_from_errno2("unlink", path2);
7520 if (err || !have_content) {
7521 if (*path_unstaged_content &&
7522 unlink(*path_unstaged_content) == -1 && err == NULL)
7523 err = got_error_from_errno2("unlink",
7524 *path_unstaged_content);
7525 free(*path_unstaged_content);
7526 *path_unstaged_content = NULL;
7528 if (err || !have_content || !have_rejected_content) {
7529 if (*path_new_staged_content &&
7530 unlink(*path_new_staged_content) == -1 && err == NULL)
7531 err = got_error_from_errno2("unlink",
7532 *path_new_staged_content);
7533 free(*path_new_staged_content);
7534 *path_new_staged_content = NULL;
7541 static const struct got_error *
7542 unstage_hunks(struct got_object_id *staged_blob_id,
7543 struct got_blob_object *blob_base,
7544 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
7545 const char *ondisk_path, const char *label_orig,
7546 struct got_worktree *worktree, struct got_repository *repo,
7547 got_worktree_patch_cb patch_cb, void *patch_arg,
7548 got_worktree_checkout_cb progress_cb, void *progress_arg)
7550 const struct got_error *err = NULL;
7551 char *path_unstaged_content = NULL;
7552 char *path_new_staged_content = NULL;
7553 struct got_object_id *new_staged_blob_id = NULL;
7557 err = create_unstaged_content(&path_unstaged_content,
7558 &path_new_staged_content, blob_id, staged_blob_id,
7559 ie->path, repo, patch_cb, patch_arg);
7563 if (path_unstaged_content == NULL)
7566 if (path_new_staged_content) {
7567 err = got_object_blob_create(&new_staged_blob_id,
7568 path_new_staged_content, repo);
7573 f = fopen(path_unstaged_content, "r");
7575 err = got_error_from_errno2("fopen",
7576 path_unstaged_content);
7579 if (fstat(fileno(f), &sb) == -1) {
7580 err = got_error_from_errno2("fstat", path_unstaged_content);
7583 if (got_fileindex_entry_staged_filetype_get(ie) ==
7584 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
7585 char link_target[PATH_MAX];
7587 r = fread(link_target, 1, sizeof(link_target), f);
7588 if (r == 0 && ferror(f)) {
7589 err = got_error_from_errno("fread");
7592 if (r >= sizeof(link_target)) { /* should not happen */
7593 err = got_error(GOT_ERR_NO_SPACE);
7596 link_target[r] = '\0';
7597 err = merge_symlink(worktree, blob_base,
7598 ondisk_path, ie->path, label_orig, link_target,
7599 worktree->base_commit_id, repo, progress_cb,
7602 int local_changes_subsumed;
7603 err = merge_file(&local_changes_subsumed, worktree,
7604 blob_base, ondisk_path, ie->path,
7605 got_fileindex_perms_to_st(ie),
7606 path_unstaged_content, label_orig, "unstaged",
7607 repo, progress_cb, progress_arg);
7612 if (new_staged_blob_id) {
7613 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7614 SHA1_DIGEST_LENGTH);
7616 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7617 got_fileindex_entry_staged_filetype_set(ie, 0);
7620 free(new_staged_blob_id);
7621 if (path_unstaged_content &&
7622 unlink(path_unstaged_content) == -1 && err == NULL)
7623 err = got_error_from_errno2("unlink", path_unstaged_content);
7624 if (path_new_staged_content &&
7625 unlink(path_new_staged_content) == -1 && err == NULL)
7626 err = got_error_from_errno2("unlink", path_new_staged_content);
7627 if (f && fclose(f) != 0 && err == NULL)
7628 err = got_error_from_errno2("fclose", path_unstaged_content);
7629 free(path_unstaged_content);
7630 free(path_new_staged_content);
7634 static const struct got_error *
7635 unstage_path(void *arg, unsigned char status,
7636 unsigned char staged_status, const char *relpath,
7637 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7638 struct got_object_id *commit_id, int dirfd, const char *de_name)
7640 const struct got_error *err = NULL;
7641 struct unstage_path_arg *a = arg;
7642 struct got_fileindex_entry *ie;
7643 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7644 char *ondisk_path = NULL;
7645 char *id_str = NULL, *label_orig = NULL;
7646 int local_changes_subsumed;
7649 if (staged_status != GOT_STATUS_ADD &&
7650 staged_status != GOT_STATUS_MODIFY &&
7651 staged_status != GOT_STATUS_DELETE)
7654 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7656 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7658 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7660 return got_error_from_errno("asprintf");
7662 err = got_object_id_str(&id_str,
7663 commit_id ? commit_id : a->worktree->base_commit_id);
7666 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7668 err = got_error_from_errno("asprintf");
7672 switch (staged_status) {
7673 case GOT_STATUS_MODIFY:
7674 err = got_object_open_as_blob(&blob_base, a->repo,
7679 case GOT_STATUS_ADD:
7681 if (staged_status == GOT_STATUS_ADD) {
7682 int choice = GOT_PATCH_CHOICE_NONE;
7683 err = (*a->patch_cb)(&choice, a->patch_arg,
7684 staged_status, ie->path, NULL, 1, 1);
7687 if (choice != GOT_PATCH_CHOICE_YES)
7690 err = unstage_hunks(staged_blob_id,
7691 blob_base, blob_id, ie, ondisk_path,
7692 label_orig, a->worktree, a->repo,
7693 a->patch_cb, a->patch_arg,
7694 a->progress_cb, a->progress_arg);
7695 break; /* Done with this file. */
7698 err = got_object_open_as_blob(&blob_staged, a->repo,
7699 staged_blob_id, 8192);
7702 switch (got_fileindex_entry_staged_filetype_get(ie)) {
7703 case GOT_FILEIDX_MODE_BAD_SYMLINK:
7704 case GOT_FILEIDX_MODE_REGULAR_FILE:
7705 err = merge_blob(&local_changes_subsumed, a->worktree,
7706 blob_base, ondisk_path, relpath,
7707 got_fileindex_perms_to_st(ie), label_orig,
7708 blob_staged, commit_id ? commit_id :
7709 a->worktree->base_commit_id, a->repo,
7710 a->progress_cb, a->progress_arg);
7712 case GOT_FILEIDX_MODE_SYMLINK:
7713 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7714 char *staged_target;
7715 err = got_object_blob_read_to_str(
7716 &staged_target, blob_staged);
7719 err = merge_symlink(a->worktree, blob_base,
7720 ondisk_path, relpath, label_orig,
7721 staged_target, commit_id ? commit_id :
7722 a->worktree->base_commit_id,
7723 a->repo, a->progress_cb, a->progress_arg);
7724 free(staged_target);
7726 err = merge_blob(&local_changes_subsumed,
7727 a->worktree, blob_base, ondisk_path,
7728 relpath, got_fileindex_perms_to_st(ie),
7729 label_orig, blob_staged,
7730 commit_id ? commit_id :
7731 a->worktree->base_commit_id, a->repo,
7732 a->progress_cb, a->progress_arg);
7736 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
7740 got_fileindex_entry_stage_set(ie,
7741 GOT_FILEIDX_STAGE_NONE);
7742 got_fileindex_entry_staged_filetype_set(ie, 0);
7745 case GOT_STATUS_DELETE:
7747 int choice = GOT_PATCH_CHOICE_NONE;
7748 err = (*a->patch_cb)(&choice, a->patch_arg,
7749 staged_status, ie->path, NULL, 1, 1);
7752 if (choice == GOT_PATCH_CHOICE_NO)
7754 if (choice != GOT_PATCH_CHOICE_YES) {
7755 err = got_error(GOT_ERR_PATCH_CHOICE);
7759 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7760 got_fileindex_entry_staged_filetype_set(ie, 0);
7761 err = get_file_status(&status, &sb, ie, ondisk_path,
7762 dirfd, de_name, a->repo);
7765 err = (*a->progress_cb)(a->progress_arg, status, relpath);
7771 got_object_blob_close(blob_base);
7773 got_object_blob_close(blob_staged);
7779 const struct got_error *
7780 got_worktree_unstage(struct got_worktree *worktree,
7781 struct got_pathlist_head *paths,
7782 got_worktree_checkout_cb progress_cb, void *progress_arg,
7783 got_worktree_patch_cb patch_cb, void *patch_arg,
7784 struct got_repository *repo)
7786 const struct got_error *err = NULL, *sync_err, *unlockerr;
7787 struct got_pathlist_entry *pe;
7788 struct got_fileindex *fileindex = NULL;
7789 char *fileindex_path = NULL;
7790 struct unstage_path_arg upa;
7792 err = lock_worktree(worktree, LOCK_EX);
7796 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7800 upa.worktree = worktree;
7801 upa.fileindex = fileindex;
7803 upa.progress_cb = progress_cb;
7804 upa.progress_arg = progress_arg;
7805 upa.patch_cb = patch_cb;
7806 upa.patch_arg = patch_arg;
7807 TAILQ_FOREACH(pe, paths, entry) {
7808 err = worktree_status(worktree, pe->path, fileindex, repo,
7809 unstage_path, &upa, NULL, NULL, 0, 0);
7814 sync_err = sync_fileindex(fileindex, fileindex_path);
7815 if (sync_err && err == NULL)
7818 free(fileindex_path);
7820 got_fileindex_free(fileindex);
7821 unlockerr = lock_worktree(worktree, LOCK_SH);
7822 if (unlockerr && err == NULL)
7827 struct report_file_info_arg {
7828 struct got_worktree *worktree;
7829 got_worktree_path_info_cb info_cb;
7831 struct got_pathlist_head *paths;
7832 got_cancel_cb cancel_cb;
7836 static const struct got_error *
7837 report_file_info(void *arg, struct got_fileindex_entry *ie)
7839 struct report_file_info_arg *a = arg;
7840 struct got_pathlist_entry *pe;
7841 struct got_object_id blob_id, staged_blob_id, commit_id;
7842 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
7843 struct got_object_id *commit_idp = NULL;
7846 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
7847 return got_error(GOT_ERR_CANCELLED);
7849 TAILQ_FOREACH(pe, a->paths, entry) {
7850 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
7851 got_path_is_child(ie->path, pe->path, pe->path_len))
7854 if (pe == NULL) /* not found */
7857 if (got_fileindex_entry_has_blob(ie)) {
7858 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
7859 blob_idp = &blob_id;
7861 stage = got_fileindex_entry_stage_get(ie);
7862 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
7863 stage == GOT_FILEIDX_STAGE_ADD) {
7864 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
7865 SHA1_DIGEST_LENGTH);
7866 staged_blob_idp = &staged_blob_id;
7869 if (got_fileindex_entry_has_commit(ie)) {
7870 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
7871 commit_idp = &commit_id;
7874 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
7875 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
7878 const struct got_error *
7879 got_worktree_path_info(struct got_worktree *worktree,
7880 struct got_pathlist_head *paths,
7881 got_worktree_path_info_cb info_cb, void *info_arg,
7882 got_cancel_cb cancel_cb, void *cancel_arg)
7885 const struct got_error *err = NULL, *unlockerr;
7886 struct got_fileindex *fileindex = NULL;
7887 char *fileindex_path = NULL;
7888 struct report_file_info_arg arg;
7890 err = lock_worktree(worktree, LOCK_SH);
7894 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7898 arg.worktree = worktree;
7899 arg.info_cb = info_cb;
7900 arg.info_arg = info_arg;
7902 arg.cancel_cb = cancel_cb;
7903 arg.cancel_arg = cancel_arg;
7904 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
7907 free(fileindex_path);
7909 got_fileindex_free(fileindex);
7910 unlockerr = lock_worktree(worktree, LOCK_UN);
7911 if (unlockerr && err == NULL)