2 * Copyright (c) 2018, 2019 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"
60 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
63 static const struct got_error *
64 create_meta_file(const char *path_got, const char *name, const char *content)
66 const struct got_error *err = NULL;
69 if (asprintf(&path, "%s/%s", path_got, name) == -1)
70 return got_error_from_errno("asprintf");
72 err = got_path_create_file(path, content);
77 static const struct got_error *
78 update_meta_file(const char *path_got, const char *name, const char *content)
80 const struct got_error *err = NULL;
85 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
86 err = got_error_from_errno("asprintf");
91 err = got_opentemp_named(&tmppath, &tmpfile, path);
96 int len = fprintf(tmpfile, "%s\n", content);
97 if (len != strlen(content) + 1) {
98 err = got_error_from_errno2("fprintf", tmppath);
103 if (rename(tmppath, path) != 0) {
104 err = got_error_from_errno3("rename", tmppath, path);
110 if (fclose(tmpfile) != 0 && err == NULL)
111 err = got_error_from_errno2("fclose", tmppath);
116 static const struct got_error *
117 read_meta_file(char **content, const char *path_got, const char *name)
119 const struct got_error *err = NULL;
127 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
128 err = got_error_from_errno("asprintf");
133 fd = open(path, O_RDONLY | O_NOFOLLOW);
136 err = got_error_path(path, GOT_ERR_WORKTREE_META);
138 err = got_error_from_errno2("open", path);
141 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
142 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
143 : got_error_from_errno2("flock", path));
147 if (fstat(fd, &sb) != 0) {
148 err = got_error_from_errno2("fstat", path);
151 *content = calloc(1, sb.st_size);
152 if (*content == NULL) {
153 err = got_error_from_errno("calloc");
157 n = read(fd, *content, sb.st_size);
158 if (n != sb.st_size) {
159 err = (n == -1 ? got_error_from_errno2("read", path) :
160 got_error_path(path, GOT_ERR_WORKTREE_META));
163 if ((*content)[sb.st_size - 1] != '\n') {
164 err = got_error_path(path, GOT_ERR_WORKTREE_META);
167 (*content)[sb.st_size - 1] = '\0';
170 if (fd != -1 && close(fd) == -1 && err == NULL)
171 err = got_error_from_errno2("close", path_got);
180 static const struct got_error *
181 write_head_ref(const char *path_got, struct got_reference *head_ref)
183 const struct got_error *err = NULL;
186 if (got_ref_is_symbolic(head_ref)) {
187 refstr = got_ref_to_str(head_ref);
189 return got_error_from_errno("got_ref_to_str");
191 refstr = strdup(got_ref_get_name(head_ref));
193 return got_error_from_errno("strdup");
195 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
200 const struct got_error *
201 got_worktree_init(const char *path, struct got_reference *head_ref,
202 const char *prefix, struct got_repository *repo)
204 const struct got_error *err = NULL;
205 struct got_object_id *commit_id = NULL;
207 uint32_t uuid_status;
209 char *path_got = NULL;
210 char *formatstr = NULL;
211 char *absprefix = NULL;
212 char *basestr = NULL;
213 char *uuidstr = NULL;
215 if (strcmp(path, got_repo_get_path(repo)) == 0) {
216 err = got_error(GOT_ERR_WORKTREE_REPO);
220 err = got_ref_resolve(&commit_id, repo, head_ref);
223 err = got_object_get_type(&obj_type, repo, commit_id);
226 if (obj_type != GOT_OBJ_TYPE_COMMIT)
227 return got_error(GOT_ERR_OBJ_TYPE);
229 if (!got_path_is_absolute(prefix)) {
230 if (asprintf(&absprefix, "/%s", prefix) == -1)
231 return got_error_from_errno("asprintf");
234 /* Create top-level directory (may already exist). */
235 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
236 err = got_error_from_errno2("mkdir", path);
240 /* Create .got directory (may already exist). */
241 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
242 err = got_error_from_errno("asprintf");
245 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
246 err = got_error_from_errno2("mkdir", path_got);
250 /* Create an empty lock file. */
251 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
255 /* Create an empty file index. */
256 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
260 /* Write the HEAD reference. */
261 err = write_head_ref(path_got, head_ref);
265 /* Record our base commit. */
266 err = got_object_id_str(&basestr, commit_id);
269 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
273 /* Store path to repository. */
274 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
275 got_repo_get_path(repo));
279 /* Store in-repository path prefix. */
280 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
281 absprefix ? absprefix : prefix);
286 uuid_create(&uuid, &uuid_status);
287 if (uuid_status != uuid_s_ok) {
288 err = got_error_uuid(uuid_status, "uuid_create");
291 uuid_to_string(&uuid, &uuidstr, &uuid_status);
292 if (uuid_status != uuid_s_ok) {
293 err = got_error_uuid(uuid_status, "uuid_to_string");
296 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
300 /* Stamp work tree with format file. */
301 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
302 err = got_error_from_errno("asprintf");
305 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
319 static const struct got_error *
320 open_worktree(struct got_worktree **worktree, const char *path)
322 const struct got_error *err = NULL;
324 char *formatstr = NULL;
325 char *uuidstr = NULL;
326 char *path_lock = NULL;
327 char *base_commit_id_str = NULL;
328 int version, fd = -1;
330 struct got_repository *repo = NULL;
331 uint32_t uuid_status;
335 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
336 err = got_error_from_errno("asprintf");
341 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
342 err = got_error_from_errno("asprintf");
347 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
349 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
350 : got_error_from_errno2("open", path_lock));
354 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
358 version = strtonum(formatstr, 1, INT_MAX, &errstr);
360 err = got_error_msg(GOT_ERR_WORKTREE_META,
361 "could not parse work tree format version number");
364 if (version != GOT_WORKTREE_FORMAT_VERSION) {
365 err = got_error(GOT_ERR_WORKTREE_VERS);
369 *worktree = calloc(1, sizeof(**worktree));
370 if (*worktree == NULL) {
371 err = got_error_from_errno("calloc");
374 (*worktree)->lockfd = -1;
376 (*worktree)->root_path = strdup(path);
377 if ((*worktree)->root_path == NULL) {
378 err = got_error_from_errno("strdup");
381 err = read_meta_file(&(*worktree)->repo_path, path_got,
382 GOT_WORKTREE_REPOSITORY);
386 err = read_meta_file(&(*worktree)->path_prefix, path_got,
387 GOT_WORKTREE_PATH_PREFIX);
391 err = read_meta_file(&base_commit_id_str, path_got,
392 GOT_WORKTREE_BASE_COMMIT);
396 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
399 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
400 if (uuid_status != uuid_s_ok) {
401 err = got_error_uuid(uuid_status, "uuid_from_string");
405 err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
409 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
414 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
415 GOT_WORKTREE_HEAD_REF);
418 got_repo_close(repo);
421 free(base_commit_id_str);
427 if (*worktree != NULL)
428 got_worktree_close(*worktree);
431 (*worktree)->lockfd = fd;
436 const struct got_error *
437 got_worktree_open(struct got_worktree **worktree, const char *path)
439 const struct got_error *err = NULL;
442 err = open_worktree(worktree, path);
443 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
447 path = dirname(path);
449 return got_error_from_errno2("dirname", path);
450 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
452 return got_error(GOT_ERR_NOT_WORKTREE);
455 const struct got_error *
456 got_worktree_close(struct got_worktree *worktree)
458 const struct got_error *err = NULL;
459 free(worktree->repo_path);
460 free(worktree->path_prefix);
461 free(worktree->base_commit_id);
462 free(worktree->head_ref_name);
463 if (worktree->lockfd != -1)
464 if (close(worktree->lockfd) != 0)
465 err = got_error_from_errno2("close",
466 got_worktree_get_root_path(worktree));
467 free(worktree->root_path);
473 got_worktree_get_root_path(struct got_worktree *worktree)
475 return worktree->root_path;
479 got_worktree_get_repo_path(struct got_worktree *worktree)
481 return worktree->repo_path;
485 got_worktree_get_path_prefix(struct got_worktree *worktree)
487 return worktree->path_prefix;
490 const struct got_error *
491 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
492 const char *path_prefix)
494 char *absprefix = NULL;
496 if (!got_path_is_absolute(path_prefix)) {
497 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
498 return got_error_from_errno("asprintf");
500 *match = (strcmp(absprefix ? absprefix : path_prefix,
501 worktree->path_prefix) == 0);
507 got_worktree_get_head_ref_name(struct got_worktree *worktree)
509 return worktree->head_ref_name;
512 const struct got_error *
513 got_worktree_set_head_ref(struct got_worktree *worktree,
514 struct got_reference *head_ref)
516 const struct got_error *err = NULL;
517 char *path_got = NULL, *head_ref_name = NULL;
519 if (asprintf(&path_got, "%s/%s", worktree->root_path,
520 GOT_WORKTREE_GOT_DIR) == -1) {
521 err = got_error_from_errno("asprintf");
526 head_ref_name = strdup(got_ref_get_name(head_ref));
527 if (head_ref_name == NULL) {
528 err = got_error_from_errno("strdup");
532 err = write_head_ref(path_got, head_ref);
536 free(worktree->head_ref_name);
537 worktree->head_ref_name = head_ref_name;
545 struct got_object_id *
546 got_worktree_get_base_commit_id(struct got_worktree *worktree)
548 return worktree->base_commit_id;
551 const struct got_error *
552 got_worktree_set_base_commit_id(struct got_worktree *worktree,
553 struct got_repository *repo, struct got_object_id *commit_id)
555 const struct got_error *err;
556 struct got_object *obj = NULL;
558 char *path_got = NULL;
560 if (asprintf(&path_got, "%s/%s", worktree->root_path,
561 GOT_WORKTREE_GOT_DIR) == -1) {
562 err = got_error_from_errno("asprintf");
567 err = got_object_open(&obj, repo, commit_id);
571 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
572 err = got_error(GOT_ERR_OBJ_TYPE);
576 /* Record our base commit. */
577 err = got_object_id_str(&id_str, commit_id);
580 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
584 free(worktree->base_commit_id);
585 worktree->base_commit_id = got_object_id_dup(commit_id);
586 if (worktree->base_commit_id == NULL) {
587 err = got_error_from_errno("got_object_id_dup");
592 got_object_close(obj);
598 static const struct got_error *
599 lock_worktree(struct got_worktree *worktree, int operation)
601 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
602 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
603 : got_error_from_errno2("flock",
604 got_worktree_get_root_path(worktree)));
608 static const struct got_error *
609 add_dir_on_disk(struct got_worktree *worktree, const char *path)
611 const struct got_error *err = NULL;
614 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
615 return got_error_from_errno("asprintf");
617 err = got_path_mkdir(abspath);
618 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
621 if (lstat(abspath, &sb) == -1) {
622 err = got_error_from_errno2("lstat", abspath);
623 } else if (!S_ISDIR(sb.st_mode)) {
624 /* TODO directory is obstructed; do something */
625 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
632 static const struct got_error *
633 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
635 const struct got_error *err = NULL;
638 size_t flen1 = 0, flen2 = 0;
643 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
644 if (flen1 == 0 && ferror(f1)) {
645 err = got_error_from_errno("fread");
648 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
649 if (flen2 == 0 && ferror(f2)) {
650 err = got_error_from_errno("fread");
657 } else if (flen2 == 0) {
661 } else if (flen1 == flen2) {
662 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
675 static const struct got_error *
676 check_files_equal(int *same, const char *f1_path, const char *f2_path)
678 const struct got_error *err = NULL;
681 FILE *f1 = NULL, *f2 = NULL;
685 if (lstat(f1_path, &sb) != 0) {
686 err = got_error_from_errno2("lstat", f1_path);
691 if (lstat(f2_path, &sb) != 0) {
692 err = got_error_from_errno2("lstat", f2_path);
697 if (size1 != size2) {
702 f1 = fopen(f1_path, "r");
704 return got_error_from_errno2("open", f1_path);
706 f2 = fopen(f2_path, "r");
708 err = got_error_from_errno2("open", f2_path);
712 err = check_file_contents_equal(same, f1, f2);
714 if (f1 && fclose(f1) != 0 && err == NULL)
715 err = got_error_from_errno("fclose");
716 if (f2 && fclose(f2) != 0 && err == NULL)
717 err = got_error_from_errno("fclose");
723 * Perform a 3-way merge where blob_orig acts as the common ancestor,
724 * the file at deriv_path acts as the first derived version, and the
725 * file on disk acts as the second derived version.
727 static const struct got_error *
728 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
729 struct got_blob_object *blob_orig, const char *ondisk_path,
730 const char *path, uint16_t st_mode, const char *deriv_path,
731 const char *label_deriv, struct got_repository *repo,
732 got_worktree_checkout_cb progress_cb, void *progress_arg)
734 const struct got_error *err = NULL;
737 char *blob_orig_path = NULL;
738 char *merged_path = NULL, *base_path = NULL;
742 *local_changes_subsumed = 0;
744 parent = dirname(ondisk_path);
746 return got_error_from_errno2("dirname", ondisk_path);
748 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
749 return got_error_from_errno("asprintf");
751 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
756 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
757 err = got_error_from_errno("asprintf");
762 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
766 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
772 * If the file has no blob, this is an "add vs add" conflict,
773 * and we simply use an empty ancestor file to make both files
774 * appear in the merged result in their entirety.
778 err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
779 blob_orig_path, ondisk_path, label_deriv, path);
783 err = (*progress_cb)(progress_arg,
784 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
788 if (fsync(merged_fd) != 0) {
789 err = got_error_from_errno("fsync");
793 /* Check if a clean merge has subsumed all local changes. */
794 if (overlapcnt == 0) {
795 err = check_files_equal(local_changes_subsumed, deriv_path,
801 if (chmod(merged_path, st_mode) != 0) {
802 err = got_error_from_errno2("chmod", merged_path);
806 if (rename(merged_path, ondisk_path) != 0) {
807 err = got_error_from_errno3("rename", merged_path,
814 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
815 err = got_error_from_errno("close");
816 if (f_orig && fclose(f_orig) != 0 && err == NULL)
817 err = got_error_from_errno("fclose");
820 if (blob_orig_path) {
821 unlink(blob_orig_path);
822 free(blob_orig_path);
828 * Perform a 3-way merge where blob_orig acts as the common ancestor,
829 * blob_deriv acts as the first derived version, and the file on disk
830 * acts as the second derived version.
832 static const struct got_error *
833 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
834 struct got_blob_object *blob_orig, const char *ondisk_path,
835 const char *path, uint16_t st_mode, struct got_blob_object *blob_deriv,
836 struct got_object_id *deriv_base_commit_id,
837 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
840 const struct got_error *err = NULL;
841 FILE *f_deriv = NULL;
842 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
843 char *label_deriv = NULL, *parent;
845 *local_changes_subsumed = 0;
847 parent = dirname(ondisk_path);
849 return got_error_from_errno2("dirname", ondisk_path);
852 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
853 err = got_error_from_errno("asprintf");
858 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
861 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
866 err = got_object_id_str(&id_str, deriv_base_commit_id);
869 if (asprintf(&label_deriv, "commit %s", id_str) == -1) {
870 err = got_error_from_errno("asprintf");
874 err = merge_file(local_changes_subsumed, worktree, blob_orig,
875 ondisk_path, path, st_mode, blob_deriv_path, label_deriv,
876 repo, progress_cb, progress_arg);
878 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
879 err = got_error_from_errno("fclose");
881 if (blob_deriv_path) {
882 unlink(blob_deriv_path);
883 free(blob_deriv_path);
890 static const struct got_error *
891 update_blob_fileindex_entry(struct got_worktree *worktree,
892 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
893 const char *ondisk_path, const char *path, struct got_blob_object *blob,
894 int update_timestamps)
896 const struct got_error *err = NULL;
899 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
901 err = got_fileindex_entry_update(ie, ondisk_path,
902 blob->id.sha1, worktree->base_commit_id->sha1,
905 struct got_fileindex_entry *new_ie;
906 err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
907 path, blob->id.sha1, worktree->base_commit_id->sha1);
909 err = got_fileindex_entry_add(fileindex, new_ie);
914 static const struct got_error *
915 install_blob(struct got_worktree *worktree, const char *ondisk_path,
916 const char *path, uint16_t te_mode, uint16_t st_mode,
917 struct got_blob_object *blob, int restoring_missing_file,
918 int reverting_versioned_file, struct got_repository *repo,
919 got_worktree_checkout_cb progress_cb, void *progress_arg)
921 const struct got_error *err = NULL;
925 char *tmppath = NULL;
927 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
928 GOT_DEFAULT_FILE_MODE);
930 if (errno == ENOENT) {
931 char *parent = dirname(path);
933 return got_error_from_errno2("dirname", path);
934 err = add_dir_on_disk(worktree, parent);
937 fd = open(ondisk_path,
938 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
939 GOT_DEFAULT_FILE_MODE);
941 return got_error_from_errno2("open",
943 } else if (errno == EEXIST) {
944 if (!S_ISREG(st_mode)) {
945 /* TODO file is obstructed; do something */
946 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
949 err = got_opentemp_named_fd(&tmppath, &fd,
956 return got_error_from_errno2("open", ondisk_path);
959 if (restoring_missing_file)
960 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
961 else if (reverting_versioned_file)
962 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
964 err = (*progress_cb)(progress_arg,
965 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
969 hdrlen = got_object_blob_get_hdrlen(blob);
971 const uint8_t *buf = got_object_blob_get_read_buf(blob);
972 err = got_object_blob_read_block(&len, blob);
976 /* Skip blob object header first time around. */
977 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
979 err = got_error_from_errno("write");
981 } else if (outlen != len - hdrlen) {
982 err = got_error(GOT_ERR_IO);
989 if (fsync(fd) != 0) {
990 err = got_error_from_errno("fsync");
995 if (rename(tmppath, ondisk_path) != 0) {
996 err = got_error_from_errno3("rename", tmppath,
1003 if (te_mode & S_IXUSR) {
1004 if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
1005 err = got_error_from_errno2("chmod", ondisk_path);
1009 if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
1010 err = got_error_from_errno2("chmod", ondisk_path);
1016 if (fd != -1 && close(fd) != 0 && err == NULL)
1017 err = got_error_from_errno("close");
1022 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1023 static const struct got_error *
1024 get_modified_file_content_status(unsigned char *status, FILE *f)
1026 const struct got_error *err = NULL;
1027 const char *markers[3] = {
1028 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1029 GOT_DIFF_CONFLICT_MARKER_SEP,
1030 GOT_DIFF_CONFLICT_MARKER_END
1035 const char delim[3] = {'\0', '\0', '\0'};
1037 while (*status == GOT_STATUS_MODIFY) {
1038 line = fparseln(f, &len, NULL, delim, 0);
1042 err = got_ferror(f, GOT_ERR_IO);
1046 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1047 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1049 *status = GOT_STATUS_CONFLICT;
1059 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1061 return !(ie->ctime_sec == sb->st_ctime &&
1062 ie->ctime_nsec == sb->st_ctimensec &&
1063 ie->mtime_sec == sb->st_mtime &&
1064 ie->mtime_nsec == sb->st_mtimensec &&
1065 ie->size == (sb->st_size & 0xffffffff));
1068 static unsigned char
1069 get_staged_status(struct got_fileindex_entry *ie)
1071 switch (got_fileindex_entry_stage_get(ie)) {
1072 case GOT_FILEIDX_STAGE_ADD:
1073 return GOT_STATUS_ADD;
1074 case GOT_FILEIDX_STAGE_DELETE:
1075 return GOT_STATUS_DELETE;
1076 case GOT_FILEIDX_STAGE_MODIFY:
1077 return GOT_STATUS_MODIFY;
1079 return GOT_STATUS_NO_CHANGE;
1083 static const struct got_error *
1084 get_file_status(unsigned char *status, struct stat *sb,
1085 struct got_fileindex_entry *ie, const char *abspath,
1086 struct got_repository *repo)
1088 const struct got_error *err = NULL;
1089 struct got_object_id id;
1093 struct got_blob_object *blob = NULL;
1095 unsigned char staged_status = get_staged_status(ie);
1097 *status = GOT_STATUS_NO_CHANGE;
1099 if (lstat(abspath, sb) == -1) {
1100 if (errno == ENOENT) {
1101 if (got_fileindex_entry_has_file_on_disk(ie))
1102 *status = GOT_STATUS_MISSING;
1104 *status = GOT_STATUS_DELETE;
1107 return got_error_from_errno2("lstat", abspath);
1110 if (!S_ISREG(sb->st_mode)) {
1111 *status = GOT_STATUS_OBSTRUCTED;
1115 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1116 *status = GOT_STATUS_DELETE;
1118 } else if (!got_fileindex_entry_has_blob(ie) &&
1119 staged_status != GOT_STATUS_ADD) {
1120 *status = GOT_STATUS_ADD;
1124 if (!stat_info_differs(ie, sb))
1127 if (staged_status == GOT_STATUS_MODIFY ||
1128 staged_status == GOT_STATUS_ADD)
1129 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1131 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1133 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1137 f = fopen(abspath, "r");
1139 err = got_error_from_errno2("fopen", abspath);
1142 hdrlen = got_object_blob_get_hdrlen(blob);
1144 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1145 err = got_object_blob_read_block(&blen, blob);
1148 /* Skip length of blob object header first time around. */
1149 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1150 if (flen == 0 && ferror(f)) {
1151 err = got_error_from_errno("fread");
1156 *status = GOT_STATUS_MODIFY;
1158 } else if (flen == 0) {
1160 *status = GOT_STATUS_MODIFY;
1162 } else if (blen - hdrlen == flen) {
1163 /* Skip blob object header first time around. */
1164 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1165 *status = GOT_STATUS_MODIFY;
1169 *status = GOT_STATUS_MODIFY;
1175 if (*status == GOT_STATUS_MODIFY) {
1177 err = get_modified_file_content_status(status, f);
1181 got_object_blob_close(blob);
1188 * Update timestamps in the file index if a file is unmodified and
1189 * we had to run a full content comparison to find out.
1191 static const struct got_error *
1192 sync_timestamps(char *ondisk_path, unsigned char status,
1193 struct got_fileindex_entry *ie, struct stat *sb)
1195 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1196 return got_fileindex_entry_update(ie, ondisk_path,
1197 ie->blob_sha1, ie->commit_sha1, 1);
1202 static const struct got_error *
1203 update_blob(struct got_worktree *worktree,
1204 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1205 struct got_tree_entry *te, const char *path,
1206 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1209 const struct got_error *err = NULL;
1210 struct got_blob_object *blob = NULL;
1212 unsigned char status = GOT_STATUS_NO_CHANGE;
1215 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1216 return got_error_from_errno("asprintf");
1219 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1220 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1223 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1226 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1227 sb.st_mode = got_fileindex_perms_to_st(ie);
1229 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1231 if (status == GOT_STATUS_OBSTRUCTED) {
1232 err = (*progress_cb)(progress_arg, status, path);
1236 if (ie && status != GOT_STATUS_MISSING) {
1237 if (got_fileindex_entry_has_commit(ie) &&
1238 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1239 SHA1_DIGEST_LENGTH) == 0) {
1240 err = sync_timestamps(ondisk_path, status, ie, &sb);
1243 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1247 if (got_fileindex_entry_has_blob(ie) &&
1248 memcmp(ie->blob_sha1, te->id->sha1,
1249 SHA1_DIGEST_LENGTH) == 0) {
1250 err = sync_timestamps(ondisk_path, status, ie, &sb);
1255 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1259 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1260 int update_timestamps;
1261 struct got_blob_object *blob2 = NULL;
1262 if (got_fileindex_entry_has_blob(ie)) {
1263 struct got_object_id id2;
1264 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1265 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1269 err = merge_blob(&update_timestamps, worktree, blob2,
1270 ondisk_path, path, sb.st_mode, blob,
1271 worktree->base_commit_id, repo,
1272 progress_cb, progress_arg);
1274 got_object_blob_close(blob2);
1278 * Do not update timestamps of files with local changes.
1279 * Otherwise, a future status walk would treat them as
1280 * unmodified files again.
1282 err = got_fileindex_entry_update(ie, ondisk_path,
1283 blob->id.sha1, worktree->base_commit_id->sha1,
1285 } else if (status == GOT_STATUS_DELETE) {
1286 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1289 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1290 ondisk_path, path, blob, 0);
1294 err = install_blob(worktree, ondisk_path, path, te->mode,
1295 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1296 repo, progress_cb, progress_arg);
1299 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1300 ondisk_path, path, blob, 1);
1304 got_object_blob_close(blob);
1310 static const struct got_error *
1311 remove_ondisk_file(const char *root_path, const char *path)
1313 const struct got_error *err = NULL;
1314 char *ondisk_path = NULL;
1316 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1317 return got_error_from_errno("asprintf");
1319 if (unlink(ondisk_path) == -1) {
1320 if (errno != ENOENT)
1321 err = got_error_from_errno2("unlink", ondisk_path);
1323 char *parent = dirname(ondisk_path);
1324 while (parent && strcmp(parent, root_path) != 0) {
1325 if (rmdir(parent) == -1) {
1326 if (errno != ENOTEMPTY)
1327 err = got_error_from_errno2("rmdir",
1331 parent = dirname(parent);
1338 static const struct got_error *
1339 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1340 struct got_fileindex_entry *ie, struct got_repository *repo,
1341 got_worktree_checkout_cb progress_cb, void *progress_arg)
1343 const struct got_error *err = NULL;
1344 unsigned char status;
1348 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1349 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1351 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1353 return got_error_from_errno("asprintf");
1355 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1359 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1360 status == GOT_STATUS_ADD) {
1361 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1365 * Preserve the working file and change the deleted blob's
1366 * entry into a schedule-add entry.
1368 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1373 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1376 if (status == GOT_STATUS_NO_CHANGE) {
1377 err = remove_ondisk_file(worktree->root_path, ie->path);
1381 got_fileindex_entry_remove(fileindex, ie);
1387 struct diff_cb_arg {
1388 struct got_fileindex *fileindex;
1389 struct got_worktree *worktree;
1390 struct got_repository *repo;
1391 got_worktree_checkout_cb progress_cb;
1393 got_cancel_cb cancel_cb;
1397 static const struct got_error *
1398 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1399 struct got_tree_entry *te, const char *parent_path)
1401 struct diff_cb_arg *a = arg;
1403 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1404 return got_error(GOT_ERR_CANCELLED);
1406 return update_blob(a->worktree, a->fileindex, ie, te,
1407 ie->path, a->repo, a->progress_cb, a->progress_arg);
1410 static const struct got_error *
1411 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1413 struct diff_cb_arg *a = arg;
1415 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1416 return got_error(GOT_ERR_CANCELLED);
1418 return delete_blob(a->worktree, a->fileindex, ie,
1419 a->repo, a->progress_cb, a->progress_arg);
1422 static const struct got_error *
1423 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1425 struct diff_cb_arg *a = arg;
1426 const struct got_error *err;
1429 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1430 return got_error(GOT_ERR_CANCELLED);
1432 if (got_object_tree_entry_is_submodule(te))
1435 if (asprintf(&path, "%s%s%s", parent_path,
1436 parent_path[0] ? "/" : "", te->name)
1438 return got_error_from_errno("asprintf");
1440 if (S_ISDIR(te->mode))
1441 err = add_dir_on_disk(a->worktree, path);
1443 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1444 a->repo, a->progress_cb, a->progress_arg);
1450 static const struct got_error *
1451 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1453 const struct got_error *err = NULL;
1454 char *uuidstr = NULL;
1455 uint32_t uuid_status;
1459 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1460 if (uuid_status != uuid_s_ok)
1461 return got_error_uuid(uuid_status, "uuid_to_string");
1463 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1465 err = got_error_from_errno("asprintf");
1472 const struct got_error *
1473 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1475 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1478 static const struct got_error *
1479 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1481 return get_ref_name(refname, worktree,
1482 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1485 static const struct got_error *
1486 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1488 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1491 static const struct got_error *
1492 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1494 return get_ref_name(refname, worktree,
1495 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1498 static const struct got_error *
1499 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1501 return get_ref_name(refname, worktree,
1502 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1505 static const struct got_error *
1506 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1508 return get_ref_name(refname, worktree,
1509 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1512 static const struct got_error *
1513 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1515 return get_ref_name(refname, worktree,
1516 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1519 static const struct got_error *
1520 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1522 return get_ref_name(refname, worktree,
1523 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1526 static const struct got_error *
1527 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1529 return get_ref_name(refname, worktree,
1530 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1533 const struct got_error *
1534 got_worktree_get_histedit_script_path(char **path,
1535 struct got_worktree *worktree)
1537 if (asprintf(path, "%s/%s/%s", worktree->root_path,
1538 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
1540 return got_error_from_errno("asprintf");
1546 * Prevent Git's garbage collector from deleting our base commit by
1547 * setting a reference to our base commit's ID.
1549 static const struct got_error *
1550 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1552 const struct got_error *err = NULL;
1553 struct got_reference *ref = NULL;
1556 err = got_worktree_get_base_ref_name(&refname, worktree);
1560 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1564 err = got_ref_write(ref, repo);
1572 static const struct got_error *
1573 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
1575 const struct got_error *err = NULL;
1577 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1578 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1579 err = got_error_from_errno("asprintf");
1580 *fileindex_path = NULL;
1586 static const struct got_error *
1587 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1588 struct got_worktree *worktree)
1590 const struct got_error *err = NULL;
1593 *fileindex_path = NULL;
1594 *fileindex = got_fileindex_alloc();
1595 if (*fileindex == NULL)
1596 return got_error_from_errno("got_fileindex_alloc");
1598 err = get_fileindex_path(fileindex_path, worktree);
1602 index = fopen(*fileindex_path, "rb");
1603 if (index == NULL) {
1604 if (errno != ENOENT)
1605 err = got_error_from_errno2("fopen", *fileindex_path);
1607 err = got_fileindex_read(*fileindex, index);
1608 if (fclose(index) != 0 && err == NULL)
1609 err = got_error_from_errno("fclose");
1613 free(*fileindex_path);
1614 *fileindex_path = NULL;
1615 got_fileindex_free(*fileindex);
1621 struct bump_base_commit_id_arg {
1622 struct got_object_id *base_commit_id;
1625 const char *entry_name;
1626 got_worktree_checkout_cb progress_cb;
1630 /* Bump base commit ID of all files within an updated part of the work tree. */
1631 static const struct got_error *
1632 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1634 const struct got_error *err;
1635 struct bump_base_commit_id_arg *a = arg;
1637 if (a->entry_name) {
1638 if (strcmp(ie->path, a->path) != 0)
1640 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1643 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1644 SHA1_DIGEST_LENGTH) == 0)
1647 if (a->progress_cb) {
1648 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1653 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1657 static const struct got_error *
1658 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1660 const struct got_error *err = NULL;
1661 char *new_fileindex_path = NULL;
1662 FILE *new_index = NULL;
1664 err = got_opentemp_named(&new_fileindex_path, &new_index,
1669 err = got_fileindex_write(fileindex, new_index);
1673 if (rename(new_fileindex_path, fileindex_path) != 0) {
1674 err = got_error_from_errno3("rename", new_fileindex_path,
1676 unlink(new_fileindex_path);
1681 free(new_fileindex_path);
1685 static const struct got_error *
1686 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1687 struct got_object_id **tree_id, const char *wt_relpath,
1688 struct got_worktree *worktree, struct got_repository *repo)
1690 const struct got_error *err = NULL;
1691 struct got_object_id *id = NULL;
1692 char *in_repo_path = NULL;
1693 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1695 *entry_type = GOT_OBJ_TYPE_ANY;
1696 *tree_relpath = NULL;
1699 if (wt_relpath[0] == '\0') {
1700 /* Check out all files within the work tree. */
1701 *entry_type = GOT_OBJ_TYPE_TREE;
1702 *tree_relpath = strdup("");
1703 if (*tree_relpath == NULL) {
1704 err = got_error_from_errno("strdup");
1707 err = got_object_id_by_path(tree_id, repo,
1708 worktree->base_commit_id, worktree->path_prefix);
1714 /* Check out a subset of files in the work tree. */
1716 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1717 is_root_wt ? "" : "/", wt_relpath) == -1) {
1718 err = got_error_from_errno("asprintf");
1722 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
1728 in_repo_path = NULL;
1730 err = got_object_get_type(entry_type, repo, id);
1734 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
1735 /* Check out a single file. */
1736 if (strchr(wt_relpath, '/') == NULL) {
1737 /* Check out a single file in work tree's root dir. */
1738 in_repo_path = strdup(worktree->path_prefix);
1739 if (in_repo_path == NULL) {
1740 err = got_error_from_errno("strdup");
1743 *tree_relpath = strdup("");
1744 if (*tree_relpath == NULL) {
1745 err = got_error_from_errno("strdup");
1749 /* Check out a single file in a subdirectory. */
1750 err = got_path_dirname(tree_relpath, wt_relpath);
1753 if (asprintf(&in_repo_path, "%s%s%s",
1754 worktree->path_prefix, is_root_wt ? "" : "/",
1755 *tree_relpath) == -1) {
1756 err = got_error_from_errno("asprintf");
1760 err = got_object_id_by_path(tree_id, repo,
1761 worktree->base_commit_id, in_repo_path);
1763 /* Check out all files within a subdirectory. */
1764 *tree_id = got_object_id_dup(id);
1765 if (*tree_id == NULL) {
1766 err = got_error_from_errno("got_object_id_dup");
1769 *tree_relpath = strdup(wt_relpath);
1770 if (*tree_relpath == NULL) {
1771 err = got_error_from_errno("strdup");
1779 *entry_type = GOT_OBJ_TYPE_ANY;
1780 free(*tree_relpath);
1781 *tree_relpath = NULL;
1788 static const struct got_error *
1789 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1790 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
1791 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1792 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
1794 const struct got_error *err = NULL;
1795 struct got_commit_object *commit = NULL;
1796 struct got_tree_object *tree = NULL;
1797 struct got_fileindex_diff_tree_cb diff_cb;
1798 struct diff_cb_arg arg;
1800 err = ref_base_commit(worktree, repo);
1804 err = got_object_open_as_commit(&commit, repo,
1805 worktree->base_commit_id);
1809 err = got_object_open_as_tree(&tree, repo, tree_id);
1814 got_object_tree_find_entry(tree, entry_name) == NULL) {
1815 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1819 diff_cb.diff_old_new = diff_old_new;
1820 diff_cb.diff_old = diff_old;
1821 diff_cb.diff_new = diff_new;
1822 arg.fileindex = fileindex;
1823 arg.worktree = worktree;
1825 arg.progress_cb = progress_cb;
1826 arg.progress_arg = progress_arg;
1827 arg.cancel_cb = cancel_cb;
1828 arg.cancel_arg = cancel_arg;
1829 err = got_fileindex_diff_tree(fileindex, tree, relpath,
1830 entry_name, repo, &diff_cb, &arg);
1833 got_object_tree_close(tree);
1835 got_object_commit_close(commit);
1839 const struct got_error *
1840 got_worktree_checkout_files(struct got_worktree *worktree,
1841 struct got_pathlist_head *paths, struct got_repository *repo,
1842 got_worktree_checkout_cb progress_cb, void *progress_arg,
1843 got_cancel_cb cancel_cb, void *cancel_arg)
1845 const struct got_error *err = NULL, *sync_err, *unlockerr;
1846 struct got_commit_object *commit = NULL;
1847 struct got_tree_object *tree = NULL;
1848 struct got_fileindex *fileindex = NULL;
1849 char *fileindex_path = NULL;
1850 struct got_pathlist_entry *pe;
1851 struct tree_path_data {
1852 SIMPLEQ_ENTRY(tree_path_data) entry;
1853 struct got_object_id *tree_id;
1858 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
1860 SIMPLEQ_INIT(&tree_paths);
1862 err = lock_worktree(worktree, LOCK_EX);
1866 /* Map all specified paths to in-repository trees. */
1867 TAILQ_FOREACH(pe, paths, entry) {
1868 tpd = malloc(sizeof(*tpd));
1870 err = got_error_from_errno("malloc");
1874 err = find_tree_entry_for_checkout(&tpd->entry_type,
1875 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
1881 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
1882 err = got_path_basename(&tpd->entry_name, pe->path);
1890 tpd->entry_name = NULL;
1892 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
1896 * Read the file index.
1897 * Checking out files is supposed to be an idempotent operation.
1898 * If the on-disk file index is incomplete we will try to complete it.
1900 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1904 tpd = SIMPLEQ_FIRST(&tree_paths);
1905 TAILQ_FOREACH(pe, paths, entry) {
1906 struct bump_base_commit_id_arg bbc_arg;
1908 err = checkout_files(worktree, fileindex, tpd->relpath,
1909 tpd->tree_id, tpd->entry_name, repo,
1910 progress_cb, progress_arg, cancel_cb, cancel_arg);
1914 bbc_arg.base_commit_id = worktree->base_commit_id;
1915 bbc_arg.entry_name = tpd->entry_name;
1916 bbc_arg.path = pe->path;
1917 bbc_arg.path_len = pe->path_len;
1918 bbc_arg.progress_cb = progress_cb;
1919 bbc_arg.progress_arg = progress_arg;
1920 err = got_fileindex_for_each_entry_safe(fileindex,
1921 bump_base_commit_id, &bbc_arg);
1925 tpd = SIMPLEQ_NEXT(tpd, entry);
1927 sync_err = sync_fileindex(fileindex, fileindex_path);
1928 if (sync_err && err == NULL)
1931 free(fileindex_path);
1933 got_object_tree_close(tree);
1935 got_object_commit_close(commit);
1937 got_fileindex_free(fileindex);
1938 while (!SIMPLEQ_EMPTY(&tree_paths)) {
1939 tpd = SIMPLEQ_FIRST(&tree_paths);
1940 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
1945 unlockerr = lock_worktree(worktree, LOCK_SH);
1946 if (unlockerr && err == NULL)
1951 struct merge_file_cb_arg {
1952 struct got_worktree *worktree;
1953 struct got_fileindex *fileindex;
1954 got_worktree_checkout_cb progress_cb;
1956 got_cancel_cb cancel_cb;
1958 struct got_object_id *commit_id2;
1961 static const struct got_error *
1962 merge_file_cb(void *arg, struct got_blob_object *blob1,
1963 struct got_blob_object *blob2, struct got_object_id *id1,
1964 struct got_object_id *id2, const char *path1, const char *path2,
1965 struct got_repository *repo)
1967 static const struct got_error *err = NULL;
1968 struct merge_file_cb_arg *a = arg;
1969 struct got_fileindex_entry *ie;
1970 char *ondisk_path = NULL;
1972 unsigned char status;
1973 int local_changes_subsumed;
1975 if (blob1 && blob2) {
1976 ie = got_fileindex_entry_get(a->fileindex, path2,
1979 return (*a->progress_cb)(a->progress_arg,
1980 GOT_STATUS_MISSING, path2);
1982 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1984 return got_error_from_errno("asprintf");
1986 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1990 if (status == GOT_STATUS_DELETE) {
1991 err = (*a->progress_cb)(a->progress_arg,
1992 GOT_STATUS_MERGE, path2);
1995 if (status != GOT_STATUS_NO_CHANGE &&
1996 status != GOT_STATUS_MODIFY &&
1997 status != GOT_STATUS_CONFLICT &&
1998 status != GOT_STATUS_ADD) {
1999 err = (*a->progress_cb)(a->progress_arg, status, path2);
2003 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
2004 ondisk_path, path2, sb.st_mode, blob2, a->commit_id2, repo,
2005 a->progress_cb, a->progress_arg);
2007 ie = got_fileindex_entry_get(a->fileindex, path1,
2010 return (*a->progress_cb)(a->progress_arg,
2011 GOT_STATUS_MISSING, path2);
2013 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2015 return got_error_from_errno("asprintf");
2017 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2022 case GOT_STATUS_NO_CHANGE:
2023 err = (*a->progress_cb)(a->progress_arg,
2024 GOT_STATUS_DELETE, path1);
2027 err = remove_ondisk_file(a->worktree->root_path, path1);
2031 got_fileindex_entry_mark_deleted_from_disk(ie);
2033 case GOT_STATUS_DELETE:
2034 case GOT_STATUS_MISSING:
2035 err = (*a->progress_cb)(a->progress_arg,
2036 GOT_STATUS_DELETE, path1);
2040 got_fileindex_entry_mark_deleted_from_disk(ie);
2042 case GOT_STATUS_ADD:
2043 case GOT_STATUS_MODIFY:
2044 case GOT_STATUS_CONFLICT:
2045 err = (*a->progress_cb)(a->progress_arg,
2046 GOT_STATUS_CANNOT_DELETE, path1);
2050 case GOT_STATUS_OBSTRUCTED:
2051 err = (*a->progress_cb)(a->progress_arg, status, path1);
2059 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2061 return got_error_from_errno("asprintf");
2062 ie = got_fileindex_entry_get(a->fileindex, path2,
2065 err = get_file_status(&status, &sb, ie, ondisk_path,
2069 if (status != GOT_STATUS_NO_CHANGE &&
2070 status != GOT_STATUS_MODIFY &&
2071 status != GOT_STATUS_CONFLICT &&
2072 status != GOT_STATUS_ADD) {
2073 err = (*a->progress_cb)(a->progress_arg,
2077 err = merge_blob(&local_changes_subsumed, a->worktree,
2078 NULL, ondisk_path, path2, sb.st_mode, blob2,
2079 a->commit_id2, repo,
2080 a->progress_cb, a->progress_arg);
2081 if (status == GOT_STATUS_DELETE) {
2082 err = update_blob_fileindex_entry(a->worktree,
2083 a->fileindex, ie, ondisk_path, ie->path,
2089 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2090 err = install_blob(a->worktree, ondisk_path, path2,
2091 /* XXX get this from parent tree! */
2092 GOT_DEFAULT_FILE_MODE,
2093 sb.st_mode, blob2, 0, 0, repo,
2094 a->progress_cb, a->progress_arg);
2097 err = got_fileindex_entry_alloc(&ie,
2098 ondisk_path, path2, NULL, NULL);
2101 err = got_fileindex_entry_add(a->fileindex, ie);
2103 got_fileindex_entry_free(ie);
2113 struct check_merge_ok_arg {
2114 struct got_worktree *worktree;
2115 struct got_repository *repo;
2118 static const struct got_error *
2119 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2121 const struct got_error *err = NULL;
2122 struct check_merge_ok_arg *a = arg;
2123 unsigned char status;
2127 /* Reject merges into a work tree with mixed base commits. */
2128 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2129 SHA1_DIGEST_LENGTH))
2130 return got_error(GOT_ERR_MIXED_COMMITS);
2132 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2134 return got_error_from_errno("asprintf");
2136 /* Reject merges into a work tree with conflicted files. */
2137 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
2140 if (status == GOT_STATUS_CONFLICT)
2141 return got_error(GOT_ERR_CONFLICTS);
2146 static const struct got_error *
2147 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2148 const char *fileindex_path, struct got_object_id *commit_id1,
2149 struct got_object_id *commit_id2, struct got_repository *repo,
2150 got_worktree_checkout_cb progress_cb, void *progress_arg,
2151 got_cancel_cb cancel_cb, void *cancel_arg)
2153 const struct got_error *err = NULL, *sync_err;
2154 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2155 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2156 struct merge_file_cb_arg arg;
2159 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2160 worktree->path_prefix);
2164 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2169 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2170 worktree->path_prefix);
2174 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2178 arg.worktree = worktree;
2179 arg.fileindex = fileindex;
2180 arg.progress_cb = progress_cb;
2181 arg.progress_arg = progress_arg;
2182 arg.cancel_cb = cancel_cb;
2183 arg.cancel_arg = cancel_arg;
2184 arg.commit_id2 = commit_id2;
2185 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2186 sync_err = sync_fileindex(fileindex, fileindex_path);
2187 if (sync_err && err == NULL)
2191 got_object_tree_close(tree1);
2193 got_object_tree_close(tree2);
2197 const struct got_error *
2198 got_worktree_merge_files(struct got_worktree *worktree,
2199 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2200 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2201 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2203 const struct got_error *err, *unlockerr;
2204 char *fileindex_path = NULL;
2205 struct got_fileindex *fileindex = NULL;
2206 struct check_merge_ok_arg mok_arg;
2208 err = lock_worktree(worktree, LOCK_EX);
2212 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2216 mok_arg.worktree = worktree;
2217 mok_arg.repo = repo;
2218 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2223 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2224 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2227 got_fileindex_free(fileindex);
2228 free(fileindex_path);
2229 unlockerr = lock_worktree(worktree, LOCK_SH);
2230 if (unlockerr && err == NULL)
2235 struct diff_dir_cb_arg {
2236 struct got_fileindex *fileindex;
2237 struct got_worktree *worktree;
2238 const char *status_path;
2239 size_t status_path_len;
2240 struct got_repository *repo;
2241 got_worktree_status_cb status_cb;
2243 got_cancel_cb cancel_cb;
2245 /* A pathlist containing per-directory pathlists of ignore patterns. */
2246 struct got_pathlist_head ignores;
2249 static const struct got_error *
2250 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2251 got_worktree_status_cb status_cb, void *status_arg,
2252 struct got_repository *repo)
2254 const struct got_error *err = NULL;
2255 unsigned char status = GOT_STATUS_NO_CHANGE;
2256 unsigned char staged_status = get_staged_status(ie);
2258 struct got_object_id blob_id, commit_id, staged_blob_id;
2259 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2260 struct got_object_id *staged_blob_idp = NULL;
2262 err = get_file_status(&status, &sb, ie, abspath, repo);
2266 if (status == GOT_STATUS_NO_CHANGE &&
2267 staged_status == GOT_STATUS_NO_CHANGE)
2270 if (got_fileindex_entry_has_blob(ie)) {
2271 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2272 blob_idp = &blob_id;
2274 if (got_fileindex_entry_has_commit(ie)) {
2275 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2276 commit_idp = &commit_id;
2278 if (staged_status == GOT_STATUS_ADD ||
2279 staged_status == GOT_STATUS_MODIFY) {
2280 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2281 SHA1_DIGEST_LENGTH);
2282 staged_blob_idp = &staged_blob_id;
2285 return (*status_cb)(status_arg, status, staged_status,
2286 ie->path, blob_idp, staged_blob_idp, commit_idp);
2289 static const struct got_error *
2290 status_old_new(void *arg, struct got_fileindex_entry *ie,
2291 struct dirent *de, const char *parent_path)
2293 const struct got_error *err = NULL;
2294 struct diff_dir_cb_arg *a = arg;
2297 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2298 return got_error(GOT_ERR_CANCELLED);
2300 if (got_path_cmp(parent_path, a->status_path,
2301 strlen(parent_path), a->status_path_len) != 0 &&
2302 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2305 if (parent_path[0]) {
2306 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2307 parent_path, de->d_name) == -1)
2308 return got_error_from_errno("asprintf");
2310 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2312 return got_error_from_errno("asprintf");
2315 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
2321 static const struct got_error *
2322 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2324 struct diff_dir_cb_arg *a = arg;
2325 struct got_object_id blob_id, commit_id;
2326 unsigned char status;
2328 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2329 return got_error(GOT_ERR_CANCELLED);
2331 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2334 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2335 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2336 if (got_fileindex_entry_has_file_on_disk(ie))
2337 status = GOT_STATUS_MISSING;
2339 status = GOT_STATUS_DELETE;
2340 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2341 ie->path, &blob_id, NULL, &commit_id);
2345 free_ignorelist(struct got_pathlist_head *ignorelist)
2347 struct got_pathlist_entry *pe;
2349 TAILQ_FOREACH(pe, ignorelist, entry)
2350 free((char *)pe->path);
2351 got_pathlist_free(ignorelist);
2355 free_ignores(struct got_pathlist_head *ignores)
2357 struct got_pathlist_entry *pe;
2359 TAILQ_FOREACH(pe, ignores, entry) {
2360 struct got_pathlist_head *ignorelist = pe->data;
2361 free_ignorelist(ignorelist);
2362 free((char *)pe->path);
2364 got_pathlist_free(ignores);
2367 static const struct got_error *
2368 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
2370 const struct got_error *err = NULL;
2371 struct got_pathlist_entry *pe = NULL;
2372 struct got_pathlist_head *ignorelist;
2373 char *line = NULL, *pattern, *dirpath = NULL;
2374 size_t linesize = 0;
2377 ignorelist = calloc(1, sizeof(*ignorelist));
2378 if (ignorelist == NULL)
2379 return got_error_from_errno("calloc");
2380 TAILQ_INIT(ignorelist);
2382 while ((linelen = getline(&line, &linesize, f)) != -1) {
2383 if (linelen > 0 && line[linelen - 1] == '\n')
2384 line[linelen - 1] = '\0';
2385 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
2387 err = got_error_from_errno("asprintf");
2390 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
2395 err = got_error_from_errno("getline");
2399 dirpath = strdup(path);
2400 if (dirpath == NULL) {
2401 err = got_error_from_errno("strdup");
2404 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
2407 if (err || pe == NULL) {
2409 free_ignorelist(ignorelist);
2415 match_ignores(struct got_pathlist_head *ignores, const char *path)
2417 struct got_pathlist_entry *pe;
2420 * The ignores pathlist contains ignore lists from children before
2421 * parents, so we can find the most specific ignorelist by walking
2422 * ignores backwards.
2424 pe = TAILQ_LAST(ignores, got_pathlist_head);
2426 if (got_path_is_child(path, pe->path, pe->path_len)) {
2427 struct got_pathlist_head *ignorelist = pe->data;
2428 struct got_pathlist_entry *pi;
2429 TAILQ_FOREACH(pi, ignorelist, entry) {
2430 if (fnmatch(pi->path, path,
2431 FNM_PATHNAME | FNM_LEADING_DIR))
2436 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
2442 static const struct got_error *
2443 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
2446 const struct got_error *err = NULL;
2448 FILE *ignoresfile = NULL;
2450 /* TODO: read .gitignores as well... */
2451 if (asprintf(&ignorespath, "%s/%s%s.cvsignore", root_path, path,
2452 path[0] ? "/" : "") == -1)
2453 return got_error_from_errno("asprintf");
2455 ignoresfile = fopen(ignorespath, "r");
2456 if (ignoresfile == NULL) {
2457 if (errno != ENOENT && errno != EACCES)
2458 err = got_error_from_errno2("fopen",
2461 err = read_ignores(ignores, path, ignoresfile);
2463 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
2464 err = got_error_from_errno2("flose", path);
2469 static const struct got_error *
2470 status_new(void *arg, struct dirent *de, const char *parent_path)
2472 const struct got_error *err = NULL;
2473 struct diff_dir_cb_arg *a = arg;
2476 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2477 return got_error(GOT_ERR_CANCELLED);
2479 /* XXX ignore symlinks for now */
2480 if (de->d_type == DT_LNK)
2483 if (parent_path[0]) {
2484 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2485 return got_error_from_errno("asprintf");
2490 if (de->d_type == DT_DIR)
2491 err = add_ignores(&a->ignores, a->worktree->root_path, path);
2492 else if (got_path_is_child(path, a->status_path, a->status_path_len)
2493 && !match_ignores(&a->ignores, path))
2494 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2495 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2501 static const struct got_error *
2502 report_single_file_status(const char *path, const char *ondisk_path,
2503 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2504 void *status_arg, struct got_repository *repo)
2506 struct got_fileindex_entry *ie;
2509 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
2511 return report_file_status(ie, ondisk_path, status_cb,
2514 if (lstat(ondisk_path, &sb) == -1) {
2515 if (errno != ENOENT)
2516 return got_error_from_errno2("lstat", ondisk_path);
2517 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
2518 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2522 if (S_ISREG(sb.st_mode))
2523 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
2524 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2529 static const struct got_error *
2530 worktree_status(struct got_worktree *worktree, const char *path,
2531 struct got_fileindex *fileindex, struct got_repository *repo,
2532 got_worktree_status_cb status_cb, void *status_arg,
2533 got_cancel_cb cancel_cb, void *cancel_arg)
2535 const struct got_error *err = NULL;
2536 DIR *workdir = NULL;
2537 struct got_fileindex_diff_dir_cb fdiff_cb;
2538 struct diff_dir_cb_arg arg;
2539 char *ondisk_path = NULL;
2541 if (asprintf(&ondisk_path, "%s%s%s",
2542 worktree->root_path, path[0] ? "/" : "", path) == -1)
2543 return got_error_from_errno("asprintf");
2545 workdir = opendir(ondisk_path);
2546 if (workdir == NULL) {
2547 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES)
2548 err = got_error_from_errno2("opendir", ondisk_path);
2550 err = report_single_file_status(path, ondisk_path,
2551 fileindex, status_cb, status_arg, repo);
2553 fdiff_cb.diff_old_new = status_old_new;
2554 fdiff_cb.diff_old = status_old;
2555 fdiff_cb.diff_new = status_new;
2556 arg.fileindex = fileindex;
2557 arg.worktree = worktree;
2558 arg.status_path = path;
2559 arg.status_path_len = strlen(path);
2561 arg.status_cb = status_cb;
2562 arg.status_arg = status_arg;
2563 arg.cancel_cb = cancel_cb;
2564 arg.cancel_arg = cancel_arg;
2565 TAILQ_INIT(&arg.ignores);
2566 err = add_ignores(&arg.ignores, worktree->root_path, path);
2568 err = got_fileindex_diff_dir(fileindex, workdir,
2569 worktree->root_path, path, repo, &fdiff_cb, &arg);
2570 free_ignores(&arg.ignores);
2579 const struct got_error *
2580 got_worktree_status(struct got_worktree *worktree,
2581 struct got_pathlist_head *paths, struct got_repository *repo,
2582 got_worktree_status_cb status_cb, void *status_arg,
2583 got_cancel_cb cancel_cb, void *cancel_arg)
2585 const struct got_error *err = NULL;
2586 char *fileindex_path = NULL;
2587 struct got_fileindex *fileindex = NULL;
2588 struct got_pathlist_entry *pe;
2590 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2594 TAILQ_FOREACH(pe, paths, entry) {
2595 err = worktree_status(worktree, pe->path, fileindex, repo,
2596 status_cb, status_arg, cancel_cb, cancel_arg);
2600 free(fileindex_path);
2601 got_fileindex_free(fileindex);
2605 const struct got_error *
2606 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2609 const struct got_error *err = NULL;
2610 char *resolved, *cwd = NULL, *path = NULL;
2615 resolved = realpath(arg, NULL);
2616 if (resolved == NULL) {
2617 if (errno != ENOENT)
2618 return got_error_from_errno2("realpath", arg);
2619 cwd = getcwd(NULL, 0);
2621 return got_error_from_errno("getcwd");
2622 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
2623 err = got_error_from_errno("asprintf");
2628 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2629 strlen(got_worktree_get_root_path(worktree)))) {
2630 err = got_error(GOT_ERR_BAD_PATH);
2634 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2635 err = got_path_skip_common_ancestor(&path,
2636 got_worktree_get_root_path(worktree), resolved);
2642 err = got_error_from_errno("strdup");
2647 /* XXX status walk can't deal with trailing slash! */
2649 while (len > 0 && path[len - 1] == '/') {
2650 path[len - 1] = '\0';
2663 static const struct got_error *
2664 schedule_addition(const char *ondisk_path, struct got_fileindex *fileindex,
2665 const char *relpath, got_worktree_status_cb status_cb, void *status_arg,
2666 struct got_repository *repo)
2668 const struct got_error *err = NULL;
2669 struct got_fileindex_entry *ie;
2670 unsigned char status;
2673 ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
2675 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2678 /* Re-adding an existing entry is a no-op. */
2679 if (status == GOT_STATUS_ADD)
2681 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
2684 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2688 err = got_fileindex_entry_add(fileindex, ie);
2690 got_fileindex_entry_free(ie);
2694 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2697 const struct got_error *
2698 got_worktree_schedule_add(struct got_worktree *worktree,
2699 struct got_pathlist_head *paths,
2700 got_worktree_status_cb status_cb, void *status_arg,
2701 struct got_repository *repo)
2703 struct got_fileindex *fileindex = NULL;
2704 char *fileindex_path = NULL;
2705 const struct got_error *err = NULL, *sync_err, *unlockerr;
2706 struct got_pathlist_entry *pe;
2708 err = lock_worktree(worktree, LOCK_EX);
2712 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2716 TAILQ_FOREACH(pe, paths, entry) {
2718 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
2720 return got_error_from_errno("asprintf");
2721 err = schedule_addition(ondisk_path, fileindex, pe->path,
2722 status_cb, status_arg, repo);
2727 sync_err = sync_fileindex(fileindex, fileindex_path);
2728 if (sync_err && err == NULL)
2731 free(fileindex_path);
2733 got_fileindex_free(fileindex);
2734 unlockerr = lock_worktree(worktree, LOCK_SH);
2735 if (unlockerr && err == NULL)
2740 static const struct got_error *
2741 schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
2742 const char *relpath, int delete_local_mods,
2743 got_worktree_status_cb status_cb, void *status_arg,
2744 struct got_repository *repo)
2746 const struct got_error *err = NULL;
2747 struct got_fileindex_entry *ie = NULL;
2748 unsigned char status, staged_status;
2751 ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
2753 return got_error(GOT_ERR_BAD_PATH);
2755 staged_status = get_staged_status(ie);
2756 if (staged_status != GOT_STATUS_NO_CHANGE) {
2757 if (staged_status == GOT_STATUS_DELETE)
2759 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
2762 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2766 if (status != GOT_STATUS_NO_CHANGE) {
2767 if (status == GOT_STATUS_DELETE)
2769 if (status == GOT_STATUS_MODIFY && !delete_local_mods)
2770 return got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
2771 if (status != GOT_STATUS_MODIFY &&
2772 status != GOT_STATUS_MISSING)
2773 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
2776 if (status != GOT_STATUS_MISSING && unlink(ondisk_path) != 0)
2777 return got_error_from_errno2("unlink", ondisk_path);
2779 got_fileindex_entry_mark_deleted_from_disk(ie);
2780 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2783 const struct got_error *
2784 got_worktree_schedule_delete(struct got_worktree *worktree,
2785 struct got_pathlist_head *paths, int delete_local_mods,
2786 got_worktree_status_cb status_cb, void *status_arg,
2787 struct got_repository *repo)
2789 struct got_fileindex *fileindex = NULL;
2790 char *fileindex_path = NULL;
2791 const struct got_error *err = NULL, *sync_err, *unlockerr;
2792 struct got_pathlist_entry *pe;
2794 err = lock_worktree(worktree, LOCK_EX);
2798 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2802 TAILQ_FOREACH(pe, paths, entry) {
2804 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
2806 return got_error_from_errno("asprintf");
2807 err = schedule_for_deletion(ondisk_path, fileindex, pe->path,
2808 delete_local_mods, status_cb, status_arg, repo);
2813 sync_err = sync_fileindex(fileindex, fileindex_path);
2814 if (sync_err && err == NULL)
2817 free(fileindex_path);
2819 got_fileindex_free(fileindex);
2820 unlockerr = lock_worktree(worktree, LOCK_SH);
2821 if (unlockerr && err == NULL)
2826 static const struct got_error *
2827 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
2829 const struct got_error *err = NULL;
2831 size_t linesize = 0, n;
2834 linelen = getline(&line, &linesize, infile);
2835 if (linelen == -1) {
2836 if (ferror(infile)) {
2837 err = got_error_from_errno("getline");
2843 n = fwrite(line, 1, linelen, outfile);
2845 err = got_ferror(outfile, GOT_ERR_IO);
2850 n = fwrite(line, 1, linelen, rejectfile);
2852 err = got_ferror(outfile, GOT_ERR_IO);
2859 static const struct got_error *
2860 skip_one_line(FILE *f)
2863 size_t linesize = 0;
2866 linelen = getline(&line, &linesize, f);
2868 if (linelen == -1 && ferror(f))
2869 return got_error_from_errno("getline");
2873 static const struct got_error *
2874 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
2875 int start_old, int end_old, int start_new, int end_new,
2876 FILE *outfile, FILE *rejectfile)
2878 const struct got_error *err;
2880 /* Copy old file's lines leading up to patch. */
2881 while (!feof(f1) && *line_cur1 < start_old) {
2882 err = copy_one_line(f1, outfile, NULL);
2887 /* Skip new file's lines leading up to patch. */
2888 while (!feof(f2) && *line_cur2 < start_new) {
2890 err = copy_one_line(f2, NULL, rejectfile);
2892 err = skip_one_line(f2);
2897 /* Copy patched lines. */
2898 while (!feof(f2) && *line_cur2 <= end_new) {
2899 err = copy_one_line(f2, outfile, NULL);
2904 /* Skip over old file's replaced lines. */
2905 while (!feof(f1) && *line_cur1 <= end_old) {
2907 err = copy_one_line(f1, NULL, rejectfile);
2909 err = skip_one_line(f1);
2918 static const struct got_error *
2919 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
2920 FILE *outfile, FILE *rejectfile)
2922 const struct got_error *err;
2925 /* Copy old file's lines until EOF. */
2927 err = copy_one_line(f1, outfile, NULL);
2934 /* Copy new file's lines until EOF. */
2936 err = copy_one_line(f2, NULL, rejectfile);
2946 static const struct got_error *
2947 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
2948 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
2949 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
2950 int *line_cur2, FILE *outfile, FILE *rejectfile,
2951 got_worktree_patch_cb patch_cb, void *patch_arg)
2953 const struct got_error *err = NULL;
2954 int start_old = change->cv.a;
2955 int end_old = change->cv.b;
2956 int start_new = change->cv.c;
2957 int end_new = change->cv.d;
2961 *choice = GOT_PATCH_CHOICE_NONE;
2963 hunkfile = got_opentemp();
2964 if (hunkfile == NULL)
2965 return got_error_from_errno("got_opentemp");
2970 /* XXX TODO needs error checking */
2971 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
2973 if (fseek(f1, pos1, SEEK_SET) == -1) {
2974 err = got_ferror(f1, GOT_ERR_IO);
2977 if (fseek(f2, pos2, SEEK_SET) == -1) {
2978 err = got_ferror(f1, GOT_ERR_IO);
2981 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
2982 err = got_ferror(hunkfile, GOT_ERR_IO);
2986 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
2987 hunkfile, n, nchanges);
2992 case GOT_PATCH_CHOICE_YES:
2993 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
2994 end_old, start_new, end_new, outfile, rejectfile);
2996 case GOT_PATCH_CHOICE_NO:
2997 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
2998 end_old, start_new, end_new, rejectfile, outfile);
3000 case GOT_PATCH_CHOICE_QUIT:
3003 err = got_error(GOT_ERR_PATCH_CHOICE);
3007 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3008 err = got_error_from_errno("fclose");
3012 struct revert_file_args {
3013 struct got_worktree *worktree;
3014 struct got_fileindex *fileindex;
3015 got_worktree_checkout_cb progress_cb;
3017 got_worktree_patch_cb patch_cb;
3019 struct got_repository *repo;
3022 static const struct got_error *
3023 create_patched_content(char **path_outfile, int reverse_patch,
3024 struct got_object_id *blob_id, const char *path2,
3025 const char *relpath, struct got_repository *repo,
3026 got_worktree_patch_cb patch_cb, void *patch_arg)
3028 const struct got_error *err;
3029 struct got_blob_object *blob = NULL;
3030 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3031 char *path1 = NULL, *id_str = NULL;
3032 struct stat sb1, sb2;
3033 struct got_diff_changes *changes = NULL;
3034 struct got_diff_state *ds = NULL;
3035 struct got_diff_args *args = NULL;
3036 struct got_diff_change *change;
3037 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3040 *path_outfile = NULL;
3042 err = got_object_id_str(&id_str, blob_id);
3046 f2 = fopen(path2, "r");
3048 err = got_error_from_errno2("fopen", path2);
3052 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
3056 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
3060 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
3064 if (stat(path1, &sb1) == -1) {
3065 err = got_error_from_errno2("stat", path1);
3068 if (stat(path2, &sb2) == -1) {
3069 err = got_error_from_errno2("stat", path2);
3073 err = got_diff_files(&changes, &ds, &args, &diff_flags,
3074 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
3078 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
3082 if (fseek(f1, 0L, SEEK_SET) == -1)
3083 return got_ferror(f1, GOT_ERR_IO);
3084 if (fseek(f2, 0L, SEEK_SET) == -1)
3085 return got_ferror(f2, GOT_ERR_IO);
3086 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
3088 err = apply_or_reject_change(&choice, change, ++n,
3089 changes->nchanges, ds, args, diff_flags, relpath,
3090 f1, f2, &line_cur1, &line_cur2,
3091 reverse_patch ? NULL : outfile,
3092 reverse_patch ? outfile : NULL,
3093 patch_cb, patch_arg);
3096 if (choice == GOT_PATCH_CHOICE_YES)
3098 else if (choice == GOT_PATCH_CHOICE_QUIT)
3102 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
3103 reverse_patch ? NULL : outfile,
3104 reverse_patch ? outfile : NULL);
3108 got_object_blob_close(blob);
3109 if (f1 && fclose(f1) == EOF && err == NULL)
3110 err = got_error_from_errno2("fclose", path1);
3111 if (f2 && fclose(f2) == EOF && err == NULL)
3112 err = got_error_from_errno2("fclose", path2);
3113 if (outfile && fclose(outfile) == EOF && err == NULL)
3114 err = got_error_from_errno2("fclose", *path_outfile);
3115 if (path1 && unlink(path1) == -1 && err == NULL)
3116 err = got_error_from_errno2("unlink", path1);
3117 if (err || !have_content) {
3118 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
3119 err = got_error_from_errno2("unlink", *path_outfile);
3120 free(*path_outfile);
3121 *path_outfile = NULL;
3125 got_diff_state_free(ds);
3129 got_diff_free_changes(changes);
3134 static const struct got_error *
3135 revert_file(void *arg, unsigned char status, unsigned char staged_status,
3136 const char *relpath, struct got_object_id *blob_id,
3137 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
3139 struct revert_file_args *a = arg;
3140 const struct got_error *err = NULL;
3141 char *parent_path = NULL;
3142 struct got_fileindex_entry *ie;
3143 struct got_tree_object *tree = NULL;
3144 struct got_object_id *tree_id = NULL;
3145 const struct got_tree_entry *te = NULL;
3146 char *tree_path = NULL, *te_name;
3147 char *ondisk_path = NULL, *path_content = NULL;
3148 struct got_blob_object *blob = NULL;
3150 /* Reverting a staged deletion is a no-op. */
3151 if (status == GOT_STATUS_DELETE &&
3152 staged_status != GOT_STATUS_NO_CHANGE)
3155 if (status == GOT_STATUS_UNVERSIONED)
3156 return (*a->progress_cb)(a->progress_arg,
3157 GOT_STATUS_UNVERSIONED, relpath);
3159 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3161 return got_error(GOT_ERR_BAD_PATH);
3163 /* Construct in-repository path of tree which contains this blob. */
3164 err = got_path_dirname(&parent_path, ie->path);
3166 if (err->code != GOT_ERR_BAD_PATH)
3168 parent_path = strdup("/");
3169 if (parent_path == NULL) {
3170 err = got_error_from_errno("strdup");
3174 if (got_path_is_root_dir(a->worktree->path_prefix)) {
3175 tree_path = strdup(parent_path);
3176 if (tree_path == NULL) {
3177 err = got_error_from_errno("strdup");
3181 if (got_path_is_root_dir(parent_path)) {
3182 tree_path = strdup(a->worktree->path_prefix);
3183 if (tree_path == NULL) {
3184 err = got_error_from_errno("strdup");
3188 if (asprintf(&tree_path, "%s/%s",
3189 a->worktree->path_prefix, parent_path) == -1) {
3190 err = got_error_from_errno("asprintf");
3196 err = got_object_id_by_path(&tree_id, a->repo,
3197 a->worktree->base_commit_id, tree_path);
3199 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
3200 (status == GOT_STATUS_ADD ||
3201 staged_status == GOT_STATUS_ADD)))
3204 err = got_object_open_as_tree(&tree, a->repo, tree_id);
3208 te_name = basename(ie->path);
3209 if (te_name == NULL) {
3210 err = got_error_from_errno2("basename", ie->path);
3214 te = got_object_tree_find_entry(tree, te_name);
3215 if (te == NULL && status != GOT_STATUS_ADD &&
3216 staged_status != GOT_STATUS_ADD) {
3217 err = got_error(GOT_ERR_NO_TREE_ENTRY);
3223 case GOT_STATUS_ADD:
3225 int choice = GOT_PATCH_CHOICE_NONE;
3226 err = (*a->patch_cb)(&choice, a->patch_arg,
3227 status, ie->path, NULL, 1, 1);
3230 if (choice != GOT_PATCH_CHOICE_YES)
3233 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
3237 got_fileindex_entry_remove(a->fileindex, ie);
3239 case GOT_STATUS_DELETE:
3241 int choice = GOT_PATCH_CHOICE_NONE;
3242 err = (*a->patch_cb)(&choice, a->patch_arg,
3243 status, ie->path, NULL, 1, 1);
3246 if (choice != GOT_PATCH_CHOICE_YES)
3250 case GOT_STATUS_MODIFY:
3251 case GOT_STATUS_CONFLICT:
3252 case GOT_STATUS_MISSING: {
3253 struct got_object_id id;
3254 if (staged_status == GOT_STATUS_ADD ||
3255 staged_status == GOT_STATUS_MODIFY) {
3256 memcpy(id.sha1, ie->staged_blob_sha1,
3257 SHA1_DIGEST_LENGTH);
3259 memcpy(id.sha1, ie->blob_sha1,
3260 SHA1_DIGEST_LENGTH);
3261 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
3265 if (asprintf(&ondisk_path, "%s/%s",
3266 got_worktree_get_root_path(a->worktree), relpath) == -1) {
3267 err = got_error_from_errno("asprintf");
3271 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
3272 status == GOT_STATUS_CONFLICT)) {
3273 err = create_patched_content(&path_content, 1, &id,
3274 ondisk_path, ie->path, a->repo,
3275 a->patch_cb, a->patch_arg);
3276 if (err || path_content == NULL)
3278 if (rename(path_content, ondisk_path) == -1) {
3279 err = got_error_from_errno3("rename",
3280 path_content, ondisk_path);
3284 err = install_blob(a->worktree, ondisk_path, ie->path,
3285 te ? te->mode : GOT_DEFAULT_FILE_MODE,
3286 got_fileindex_perms_to_st(ie), blob, 0, 1,
3287 a->repo, a->progress_cb, a->progress_arg);
3290 if (status == GOT_STATUS_DELETE) {
3291 err = update_blob_fileindex_entry(a->worktree,
3292 a->fileindex, ie, ondisk_path, ie->path,
3309 got_object_blob_close(blob);
3311 got_object_tree_close(tree);
3316 const struct got_error *
3317 got_worktree_revert(struct got_worktree *worktree,
3318 struct got_pathlist_head *paths,
3319 got_worktree_checkout_cb progress_cb, void *progress_arg,
3320 got_worktree_patch_cb patch_cb, void *patch_arg,
3321 struct got_repository *repo)
3323 struct got_fileindex *fileindex = NULL;
3324 char *fileindex_path = NULL;
3325 const struct got_error *err = NULL, *unlockerr = NULL;
3326 const struct got_error *sync_err = NULL;
3327 struct got_pathlist_entry *pe;
3328 struct revert_file_args rfa;
3330 err = lock_worktree(worktree, LOCK_EX);
3334 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3338 rfa.worktree = worktree;
3339 rfa.fileindex = fileindex;
3340 rfa.progress_cb = progress_cb;
3341 rfa.progress_arg = progress_arg;
3342 rfa.patch_cb = patch_cb;
3343 rfa.patch_arg = patch_arg;
3345 TAILQ_FOREACH(pe, paths, entry) {
3346 err = worktree_status(worktree, pe->path, fileindex, repo,
3347 revert_file, &rfa, NULL, NULL);
3351 sync_err = sync_fileindex(fileindex, fileindex_path);
3352 if (sync_err && err == NULL)
3355 free(fileindex_path);
3357 got_fileindex_free(fileindex);
3358 unlockerr = lock_worktree(worktree, LOCK_SH);
3359 if (unlockerr && err == NULL)
3365 free_commitable(struct got_commitable *ct)
3368 free(ct->in_repo_path);
3369 free(ct->ondisk_path);
3371 free(ct->base_blob_id);
3372 free(ct->staged_blob_id);
3373 free(ct->base_commit_id);
3377 struct collect_commitables_arg {
3378 struct got_pathlist_head *commitable_paths;
3379 struct got_repository *repo;
3380 struct got_worktree *worktree;
3381 int have_staged_files;
3384 static const struct got_error *
3385 collect_commitables(void *arg, unsigned char status,
3386 unsigned char staged_status, const char *relpath,
3387 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3388 struct got_object_id *commit_id)
3390 struct collect_commitables_arg *a = arg;
3391 const struct got_error *err = NULL;
3392 struct got_commitable *ct = NULL;
3393 struct got_pathlist_entry *new = NULL;
3394 char *parent_path = NULL, *path = NULL;
3397 if (a->have_staged_files) {
3398 if (staged_status != GOT_STATUS_MODIFY &&
3399 staged_status != GOT_STATUS_ADD &&
3400 staged_status != GOT_STATUS_DELETE)
3403 if (status == GOT_STATUS_CONFLICT)
3404 return got_error(GOT_ERR_COMMIT_CONFLICT);
3406 if (status != GOT_STATUS_MODIFY &&
3407 status != GOT_STATUS_ADD &&
3408 status != GOT_STATUS_DELETE)
3412 if (asprintf(&path, "/%s", relpath) == -1) {
3413 err = got_error_from_errno("asprintf");
3416 if (strcmp(path, "/") == 0) {
3417 parent_path = strdup("");
3418 if (parent_path == NULL)
3419 return got_error_from_errno("strdup");
3421 err = got_path_dirname(&parent_path, path);
3426 ct = calloc(1, sizeof(*ct));
3428 err = got_error_from_errno("calloc");
3432 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
3434 err = got_error_from_errno("asprintf");
3437 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
3438 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3440 if (lstat(ct->ondisk_path, &sb) != 0) {
3441 err = got_error_from_errno2("lstat", ct->ondisk_path);
3444 ct->mode = sb.st_mode;
3447 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
3448 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
3450 err = got_error_from_errno("asprintf");
3454 ct->status = status;
3455 ct->staged_status = staged_status;
3456 ct->blob_id = NULL; /* will be filled in when blob gets created */
3457 if (ct->status != GOT_STATUS_ADD &&
3458 ct->staged_status != GOT_STATUS_ADD) {
3459 ct->base_blob_id = got_object_id_dup(blob_id);
3460 if (ct->base_blob_id == NULL) {
3461 err = got_error_from_errno("got_object_id_dup");
3464 ct->base_commit_id = got_object_id_dup(commit_id);
3465 if (ct->base_commit_id == NULL) {
3466 err = got_error_from_errno("got_object_id_dup");
3470 if (ct->staged_status == GOT_STATUS_ADD ||
3471 ct->staged_status == GOT_STATUS_MODIFY) {
3472 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
3473 if (ct->staged_blob_id == NULL) {
3474 err = got_error_from_errno("got_object_id_dup");
3478 ct->path = strdup(path);
3479 if (ct->path == NULL) {
3480 err = got_error_from_errno("strdup");
3483 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
3485 if (ct && (err || new == NULL))
3486 free_commitable(ct);
3492 static const struct got_error *write_tree(struct got_object_id **,
3493 struct got_tree_object *, const char *, struct got_pathlist_head *,
3494 got_worktree_status_cb status_cb, void *status_arg,
3495 struct got_repository *);
3497 static const struct got_error *
3498 write_subtree(struct got_object_id **new_subtree_id,
3499 struct got_tree_entry *te, const char *parent_path,
3500 struct got_pathlist_head *commitable_paths,
3501 got_worktree_status_cb status_cb, void *status_arg,
3502 struct got_repository *repo)
3504 const struct got_error *err = NULL;
3505 struct got_tree_object *subtree;
3508 if (asprintf(&subpath, "%s%s%s", parent_path,
3509 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
3510 return got_error_from_errno("asprintf");
3512 err = got_object_open_as_tree(&subtree, repo, te->id);
3516 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
3517 status_cb, status_arg, repo);
3518 got_object_tree_close(subtree);
3523 static const struct got_error *
3524 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
3526 const struct got_error *err = NULL;
3527 char *ct_parent_path = NULL;
3531 if (strchr(ct->in_repo_path, '/') == NULL) {
3532 *match = got_path_is_root_dir(path);
3536 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
3539 *match = (strcmp(path, ct_parent_path) == 0);
3540 free(ct_parent_path);
3545 get_ct_file_mode(struct got_commitable *ct)
3547 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
3550 static const struct got_error *
3551 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
3552 struct got_tree_entry *te, struct got_commitable *ct)
3554 const struct got_error *err = NULL;
3558 err = got_object_tree_entry_dup(new_te, te);
3562 (*new_te)->mode = get_ct_file_mode(ct);
3564 free((*new_te)->id);
3565 if (ct->staged_status == GOT_STATUS_MODIFY)
3566 (*new_te)->id = got_object_id_dup(ct->staged_blob_id);
3568 (*new_te)->id = got_object_id_dup(ct->blob_id);
3569 if ((*new_te)->id == NULL) {
3570 err = got_error_from_errno("got_object_id_dup");
3574 if (err && *new_te) {
3575 got_object_tree_entry_close(*new_te);
3581 static const struct got_error *
3582 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
3583 struct got_commitable *ct)
3585 const struct got_error *err = NULL;
3590 *new_te = calloc(1, sizeof(**new_te));
3591 if (*new_te == NULL)
3592 return got_error_from_errno("calloc");
3594 ct_name = basename(ct->path);
3595 if (ct_name == NULL) {
3596 err = got_error_from_errno2("basename", ct->path);
3599 (*new_te)->name = strdup(ct_name);
3600 if ((*new_te)->name == NULL) {
3601 err = got_error_from_errno("strdup");
3605 (*new_te)->mode = get_ct_file_mode(ct);
3607 if (ct->staged_status == GOT_STATUS_ADD)
3608 (*new_te)->id = got_object_id_dup(ct->staged_blob_id);
3610 (*new_te)->id = got_object_id_dup(ct->blob_id);
3611 if ((*new_te)->id == NULL) {
3612 err = got_error_from_errno("got_object_id_dup");
3616 if (err && *new_te) {
3617 got_object_tree_entry_close(*new_te);
3623 static const struct got_error *
3624 insert_tree_entry(struct got_tree_entry *new_te,
3625 struct got_pathlist_head *paths)
3627 const struct got_error *err = NULL;
3628 struct got_pathlist_entry *new_pe;
3630 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
3634 return got_error(GOT_ERR_TREE_DUP_ENTRY);
3638 static const struct got_error *
3639 report_ct_status(struct got_commitable *ct,
3640 got_worktree_status_cb status_cb, void *status_arg)
3642 const char *ct_path = ct->path;
3643 unsigned char status;
3645 while (ct_path[0] == '/')
3648 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
3649 status = ct->staged_status;
3651 status = ct->status;
3653 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
3654 ct_path, ct->blob_id, NULL, NULL);
3657 static const struct got_error *
3658 match_modified_subtree(int *modified, struct got_tree_entry *te,
3659 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
3661 const struct got_error *err = NULL;
3662 struct got_pathlist_entry *pe;
3667 if (asprintf(&te_path, "%s%s%s", base_tree_path,
3668 got_path_is_root_dir(base_tree_path) ? "" : "/",
3670 return got_error_from_errno("asprintf");
3672 TAILQ_FOREACH(pe, commitable_paths, entry) {
3673 struct got_commitable *ct = pe->data;
3674 *modified = got_path_is_child(ct->in_repo_path, te_path,
3684 static const struct got_error *
3685 match_deleted_or_modified_ct(struct got_commitable **ctp,
3686 struct got_tree_entry *te, const char *base_tree_path,
3687 struct got_pathlist_head *commitable_paths)
3689 const struct got_error *err = NULL;
3690 struct got_pathlist_entry *pe;
3694 TAILQ_FOREACH(pe, commitable_paths, entry) {
3695 struct got_commitable *ct = pe->data;
3696 char *ct_name = NULL;
3699 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
3700 if (ct->status != GOT_STATUS_MODIFY &&
3701 ct->status != GOT_STATUS_DELETE)
3704 if (ct->staged_status != GOT_STATUS_MODIFY &&
3705 ct->staged_status != GOT_STATUS_DELETE)
3709 if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
3712 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
3718 ct_name = basename(pe->path);
3719 if (ct_name == NULL)
3720 return got_error_from_errno2("basename", pe->path);
3722 if (strcmp(te->name, ct_name) != 0)
3732 static const struct got_error *
3733 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
3734 const char *child_path, const char *path_base_tree,
3735 struct got_pathlist_head *commitable_paths,
3736 got_worktree_status_cb status_cb, void *status_arg,
3737 struct got_repository *repo)
3739 const struct got_error *err = NULL;
3740 struct got_tree_entry *new_te;
3745 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
3746 got_path_is_root_dir(path_base_tree) ? "" : "/",
3748 return got_error_from_errno("asprintf");
3750 new_te = calloc(1, sizeof(*new_te));
3752 return got_error_from_errno("calloc");
3753 new_te->mode = S_IFDIR;
3754 new_te->name = strdup(child_path);
3755 if (new_te->name == NULL) {
3756 err = got_error_from_errno("strdup");
3757 got_object_tree_entry_close(new_te);
3760 err = write_tree(&new_te->id, NULL, subtree_path,
3761 commitable_paths, status_cb, status_arg, repo);
3763 got_object_tree_entry_close(new_te);
3773 static const struct got_error *
3774 write_tree(struct got_object_id **new_tree_id,
3775 struct got_tree_object *base_tree, const char *path_base_tree,
3776 struct got_pathlist_head *commitable_paths,
3777 got_worktree_status_cb status_cb, void *status_arg,
3778 struct got_repository *repo)
3780 const struct got_error *err = NULL;
3781 const struct got_tree_entries *base_entries = NULL;
3782 struct got_pathlist_head paths;
3783 struct got_tree_entries new_tree_entries;
3784 struct got_tree_entry *te, *new_te = NULL;
3785 struct got_pathlist_entry *pe;
3788 new_tree_entries.nentries = 0;
3789 SIMPLEQ_INIT(&new_tree_entries.head);
3791 /* Insert, and recurse into, newly added entries first. */
3792 TAILQ_FOREACH(pe, commitable_paths, entry) {
3793 struct got_commitable *ct = pe->data;
3794 char *child_path = NULL, *slash;
3796 if ((ct->status != GOT_STATUS_ADD &&
3797 ct->staged_status != GOT_STATUS_ADD) ||
3798 (ct->flags & GOT_COMMITABLE_ADDED))
3801 if (!got_path_is_child(pe->path, path_base_tree,
3802 strlen(path_base_tree)))
3805 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
3810 slash = strchr(child_path, '/');
3811 if (slash == NULL) {
3812 err = alloc_added_blob_tree_entry(&new_te, ct);
3815 err = report_ct_status(ct, status_cb, status_arg);
3818 ct->flags |= GOT_COMMITABLE_ADDED;
3819 err = insert_tree_entry(new_te, &paths);
3823 *slash = '\0'; /* trim trailing path components */
3824 if (base_tree == NULL ||
3825 got_object_tree_find_entry(base_tree, child_path)
3827 err = make_subtree_for_added_blob(&new_te,
3828 child_path, path_base_tree,
3829 commitable_paths, status_cb, status_arg,
3833 err = insert_tree_entry(new_te, &paths);
3841 /* Handle modified and deleted entries. */
3842 base_entries = got_object_tree_get_entries(base_tree);
3843 SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
3844 struct got_commitable *ct = NULL;
3846 if (got_object_tree_entry_is_submodule(te)) {
3847 /* Entry is a submodule; just copy it. */
3848 err = got_object_tree_entry_dup(&new_te, te);
3851 err = insert_tree_entry(new_te, &paths);
3857 if (S_ISDIR(te->mode)) {
3859 err = got_object_tree_entry_dup(&new_te, te);
3862 err = match_modified_subtree(&modified, te,
3863 path_base_tree, commitable_paths);
3866 /* Avoid recursion into unmodified subtrees. */
3869 err = write_subtree(&new_te->id, te,
3870 path_base_tree, commitable_paths,
3871 status_cb, status_arg, repo);
3875 err = insert_tree_entry(new_te, &paths);
3881 err = match_deleted_or_modified_ct(&ct, te,
3882 path_base_tree, commitable_paths);
3886 /* NB: Deleted entries get dropped here. */
3887 if (ct->status == GOT_STATUS_MODIFY ||
3888 ct->staged_status == GOT_STATUS_MODIFY) {
3889 err = alloc_modified_blob_tree_entry(
3893 err = insert_tree_entry(new_te, &paths);
3897 err = report_ct_status(ct, status_cb,
3902 /* Entry is unchanged; just copy it. */
3903 err = got_object_tree_entry_dup(&new_te, te);
3906 err = insert_tree_entry(new_te, &paths);
3913 /* Write new list of entries; deleted entries have been dropped. */
3914 TAILQ_FOREACH(pe, &paths, entry) {
3915 struct got_tree_entry *te = pe->data;
3916 new_tree_entries.nentries++;
3917 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
3919 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
3921 got_object_tree_entries_close(&new_tree_entries);
3922 got_pathlist_free(&paths);
3926 static const struct got_error *
3927 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
3928 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
3929 int have_staged_files)
3931 const struct got_error *err = NULL;
3932 struct got_pathlist_entry *pe;
3934 TAILQ_FOREACH(pe, commitable_paths, entry) {
3935 struct got_fileindex_entry *ie;
3936 struct got_commitable *ct = pe->data;
3938 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
3940 if (ct->status == GOT_STATUS_DELETE ||
3941 ct->staged_status == GOT_STATUS_DELETE) {
3942 got_fileindex_entry_remove(fileindex, ie);
3943 got_fileindex_entry_free(ie);
3944 } else if (ct->staged_status == GOT_STATUS_ADD ||
3945 ct->staged_status == GOT_STATUS_MODIFY) {
3946 got_fileindex_entry_stage_set(ie,
3947 GOT_FILEIDX_STAGE_NONE);
3948 err = got_fileindex_entry_update(ie,
3949 ct->ondisk_path, ct->staged_blob_id->sha1,
3950 new_base_commit_id->sha1,
3951 !have_staged_files);
3953 err = got_fileindex_entry_update(ie,
3954 ct->ondisk_path, ct->blob_id->sha1,
3955 new_base_commit_id->sha1,
3956 !have_staged_files);
3958 err = got_fileindex_entry_alloc(&ie,
3959 ct->ondisk_path, pe->path, ct->blob_id->sha1,
3960 new_base_commit_id->sha1);
3963 err = got_fileindex_entry_add(fileindex, ie);
3972 static const struct got_error *
3973 check_out_of_date(const char *in_repo_path, unsigned char status,
3974 unsigned char staged_status, struct got_object_id *base_blob_id,
3975 struct got_object_id *base_commit_id,
3976 struct got_object_id *head_commit_id, struct got_repository *repo,
3979 const struct got_error *err = NULL;
3980 struct got_object_id *id = NULL;
3982 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
3983 /* Trivial case: base commit == head commit */
3984 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
3987 * Ensure file content which local changes were based
3988 * on matches file content in the branch head.
3990 err = got_object_id_by_path(&id, repo, head_commit_id,
3993 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3994 err = got_error(ood_errcode);
3996 } else if (got_object_id_cmp(id, base_blob_id) != 0)
3997 err = got_error(ood_errcode);
3999 /* Require that added files don't exist in the branch head. */
4000 err = got_object_id_by_path(&id, repo, head_commit_id,
4002 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
4004 err = id ? got_error(ood_errcode) : NULL;
4011 const struct got_error *
4012 commit_worktree(struct got_object_id **new_commit_id,
4013 struct got_pathlist_head *commitable_paths,
4014 struct got_object_id *head_commit_id, struct got_worktree *worktree,
4015 const char *author, const char *committer,
4016 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4017 got_worktree_status_cb status_cb, void *status_arg,
4018 struct got_repository *repo)
4020 const struct got_error *err = NULL, *unlockerr = NULL;
4021 struct got_pathlist_entry *pe;
4022 const char *head_ref_name = NULL;
4023 struct got_commit_object *head_commit = NULL;
4024 struct got_reference *head_ref2 = NULL;
4025 struct got_object_id *head_commit_id2 = NULL;
4026 struct got_tree_object *head_tree = NULL;
4027 struct got_object_id *new_tree_id = NULL;
4028 struct got_object_id_queue parent_ids;
4029 struct got_object_qid *pid = NULL;
4030 char *logmsg = NULL;
4032 *new_commit_id = NULL;
4034 SIMPLEQ_INIT(&parent_ids);
4036 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
4040 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
4044 if (commit_msg_cb != NULL) {
4045 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
4050 if (logmsg == NULL || strlen(logmsg) == 0) {
4051 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
4055 /* Create blobs from added and modified files and record their IDs. */
4056 TAILQ_FOREACH(pe, commitable_paths, entry) {
4057 struct got_commitable *ct = pe->data;
4060 /* Blobs for staged files already exist. */
4061 if (ct->staged_status == GOT_STATUS_ADD ||
4062 ct->staged_status == GOT_STATUS_MODIFY)
4065 if (ct->status != GOT_STATUS_ADD &&
4066 ct->status != GOT_STATUS_MODIFY)
4069 if (asprintf(&ondisk_path, "%s/%s",
4070 worktree->root_path, pe->path) == -1) {
4071 err = got_error_from_errno("asprintf");
4074 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
4080 /* Recursively write new tree objects. */
4081 err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
4082 status_cb, status_arg, repo);
4086 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
4089 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
4090 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
4091 1, author, time(NULL), committer, time(NULL), logmsg, repo);
4092 got_object_qid_free(pid);
4098 /* Check if a concurrent commit to our branch has occurred. */
4099 head_ref_name = got_worktree_get_head_ref_name(worktree);
4100 if (head_ref_name == NULL) {
4101 err = got_error_from_errno("got_worktree_get_head_ref_name");
4104 /* Lock the reference here to prevent concurrent modification. */
4105 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
4108 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
4111 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
4112 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
4115 /* Update branch head in repository. */
4116 err = got_ref_change_ref(head_ref2, *new_commit_id);
4119 err = got_ref_write(head_ref2, repo);
4123 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
4127 err = ref_base_commit(worktree, repo);
4132 got_object_tree_close(head_tree);
4134 got_object_commit_close(head_commit);
4135 free(head_commit_id2);
4137 unlockerr = got_ref_unlock(head_ref2);
4138 if (unlockerr && err == NULL)
4140 got_ref_close(head_ref2);
4145 static const struct got_error *
4146 check_path_is_commitable(const char *path,
4147 struct got_pathlist_head *commitable_paths)
4149 struct got_pathlist_entry *cpe = NULL;
4150 size_t path_len = strlen(path);
4152 TAILQ_FOREACH(cpe, commitable_paths, entry) {
4153 struct got_commitable *ct = cpe->data;
4154 const char *ct_path = ct->path;
4156 while (ct_path[0] == '/')
4159 if (strcmp(path, ct_path) == 0 ||
4160 got_path_is_child(ct_path, path, path_len))
4165 return got_error_path(path, GOT_ERR_BAD_PATH);
4170 static const struct got_error *
4171 check_staged_file(void *arg, struct got_fileindex_entry *ie)
4173 int *have_staged_files = arg;
4175 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
4176 *have_staged_files = 1;
4177 return got_error(GOT_ERR_CANCELLED);
4183 static const struct got_error *
4184 check_non_staged_files(struct got_fileindex *fileindex,
4185 struct got_pathlist_head *paths)
4187 struct got_pathlist_entry *pe;
4188 struct got_fileindex_entry *ie;
4190 TAILQ_FOREACH(pe, paths, entry) {
4191 if (pe->path[0] == '\0')
4193 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4195 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
4196 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
4197 return got_error_path(pe->path,
4198 GOT_ERR_FILE_NOT_STAGED);
4204 const struct got_error *
4205 got_worktree_commit(struct got_object_id **new_commit_id,
4206 struct got_worktree *worktree, struct got_pathlist_head *paths,
4207 const char *author, const char *committer,
4208 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4209 got_worktree_status_cb status_cb, void *status_arg,
4210 struct got_repository *repo)
4212 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
4213 struct got_fileindex *fileindex = NULL;
4214 char *fileindex_path = NULL;
4215 struct got_pathlist_head commitable_paths;
4216 struct collect_commitables_arg cc_arg;
4217 struct got_pathlist_entry *pe;
4218 struct got_reference *head_ref = NULL;
4219 struct got_object_id *head_commit_id = NULL;
4220 int have_staged_files = 0;
4222 *new_commit_id = NULL;
4224 TAILQ_INIT(&commitable_paths);
4226 err = lock_worktree(worktree, LOCK_EX);
4230 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4234 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4238 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4242 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
4243 &have_staged_files);
4244 if (err && err->code != GOT_ERR_CANCELLED)
4246 if (have_staged_files) {
4247 err = check_non_staged_files(fileindex, paths);
4252 cc_arg.commitable_paths = &commitable_paths;
4253 cc_arg.worktree = worktree;
4255 cc_arg.have_staged_files = have_staged_files;
4256 TAILQ_FOREACH(pe, paths, entry) {
4257 err = worktree_status(worktree, pe->path, fileindex, repo,
4258 collect_commitables, &cc_arg, NULL, NULL);
4263 if (TAILQ_EMPTY(&commitable_paths)) {
4264 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4268 TAILQ_FOREACH(pe, paths, entry) {
4269 err = check_path_is_commitable(pe->path, &commitable_paths);
4274 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4275 struct got_commitable *ct = pe->data;
4276 const char *ct_path = ct->in_repo_path;
4278 while (ct_path[0] == '/')
4280 err = check_out_of_date(ct_path, ct->status,
4281 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
4282 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
4288 err = commit_worktree(new_commit_id, &commitable_paths,
4289 head_commit_id, worktree, author, committer,
4290 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
4294 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4295 fileindex, have_staged_files);
4296 sync_err = sync_fileindex(fileindex, fileindex_path);
4297 if (sync_err && err == NULL)
4301 got_fileindex_free(fileindex);
4302 free(fileindex_path);
4303 unlockerr = lock_worktree(worktree, LOCK_SH);
4304 if (unlockerr && err == NULL)
4306 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4307 struct got_commitable *ct = pe->data;
4308 free_commitable(ct);
4310 got_pathlist_free(&commitable_paths);
4315 got_commitable_get_path(struct got_commitable *ct)
4321 got_commitable_get_status(struct got_commitable *ct)
4326 struct check_rebase_ok_arg {
4327 struct got_worktree *worktree;
4328 struct got_repository *repo;
4331 static const struct got_error *
4332 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
4334 const struct got_error *err = NULL;
4335 struct check_rebase_ok_arg *a = arg;
4336 unsigned char status;
4340 /* Reject rebase of a work tree with mixed base commits. */
4341 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
4342 SHA1_DIGEST_LENGTH))
4343 return got_error(GOT_ERR_MIXED_COMMITS);
4345 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
4347 return got_error_from_errno("asprintf");
4349 /* Reject rebase of a work tree with modified or staged files. */
4350 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
4355 if (status != GOT_STATUS_NO_CHANGE)
4356 return got_error(GOT_ERR_MODIFIED);
4357 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
4358 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
4363 const struct got_error *
4364 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
4365 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
4366 struct got_worktree *worktree, struct got_reference *branch,
4367 struct got_repository *repo)
4369 const struct got_error *err = NULL;
4370 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4371 char *branch_ref_name = NULL;
4372 char *fileindex_path = NULL;
4373 struct check_rebase_ok_arg ok_arg;
4374 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
4376 *new_base_branch_ref = NULL;
4380 err = lock_worktree(worktree, LOCK_EX);
4384 err = open_fileindex(fileindex, &fileindex_path, worktree);
4388 ok_arg.worktree = worktree;
4390 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4395 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4399 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4403 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4407 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
4412 err = got_ref_alloc_symref(new_base_branch_ref,
4413 new_base_branch_ref_name, wt_branch);
4416 err = got_ref_write(*new_base_branch_ref, repo);
4420 /* TODO Lock original branch's ref while rebasing? */
4422 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
4426 err = got_ref_write(branch_ref, repo);
4430 err = got_ref_alloc(tmp_branch, tmp_branch_name,
4431 worktree->base_commit_id);
4434 err = got_ref_write(*tmp_branch, repo);
4438 err = got_worktree_set_head_ref(worktree, *tmp_branch);
4442 free(fileindex_path);
4443 free(tmp_branch_name);
4444 free(new_base_branch_ref_name);
4445 free(branch_ref_name);
4447 got_ref_close(branch_ref);
4449 got_ref_close(wt_branch);
4451 if (*new_base_branch_ref) {
4452 got_ref_close(*new_base_branch_ref);
4453 *new_base_branch_ref = NULL;
4456 got_ref_close(*tmp_branch);
4460 got_fileindex_free(*fileindex);
4463 lock_worktree(worktree, LOCK_SH);
4468 const struct got_error *
4469 got_worktree_rebase_continue(struct got_object_id **commit_id,
4470 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
4471 struct got_reference **branch, struct got_fileindex **fileindex,
4472 struct got_worktree *worktree, struct got_repository *repo)
4474 const struct got_error *err;
4475 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
4476 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
4477 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
4478 char *fileindex_path = NULL;
4479 int have_staged_files = 0;
4482 *new_base_branch = NULL;
4487 err = lock_worktree(worktree, LOCK_EX);
4491 err = open_fileindex(fileindex, &fileindex_path, worktree);
4495 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
4496 &have_staged_files);
4497 if (err && err->code != GOT_ERR_CANCELLED)
4499 if (have_staged_files) {
4500 err = got_error(GOT_ERR_STAGED_PATHS);
4504 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4508 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4512 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4516 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4520 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
4524 err = got_ref_open(branch, repo,
4525 got_ref_get_symref_target(branch_ref), 0);
4529 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4533 err = got_ref_resolve(commit_id, repo, commit_ref);
4537 err = got_ref_open(new_base_branch, repo,
4538 new_base_branch_ref_name, 0);
4542 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4546 free(commit_ref_name);
4547 free(branch_ref_name);
4548 free(fileindex_path);
4550 got_ref_close(commit_ref);
4552 got_ref_close(branch_ref);
4557 got_ref_close(*tmp_branch);
4560 if (*new_base_branch) {
4561 got_ref_close(*new_base_branch);
4562 *new_base_branch = NULL;
4565 got_ref_close(*branch);
4569 got_fileindex_free(*fileindex);
4572 lock_worktree(worktree, LOCK_SH);
4577 const struct got_error *
4578 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
4580 const struct got_error *err;
4581 char *tmp_branch_name = NULL;
4583 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4587 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4588 free(tmp_branch_name);
4592 static const struct got_error *
4593 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
4594 char **logmsg, void *arg)
4600 static const struct got_error *
4601 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
4602 const char *path, struct got_object_id *blob_id,
4603 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
4608 struct collect_merged_paths_arg {
4609 got_worktree_checkout_cb progress_cb;
4611 struct got_pathlist_head *merged_paths;
4614 static const struct got_error *
4615 collect_merged_paths(void *arg, unsigned char status, const char *path)
4617 const struct got_error *err;
4618 struct collect_merged_paths_arg *a = arg;
4620 struct got_pathlist_entry *new;
4622 err = (*a->progress_cb)(a->progress_arg, status, path);
4626 if (status != GOT_STATUS_MERGE &&
4627 status != GOT_STATUS_ADD &&
4628 status != GOT_STATUS_DELETE &&
4629 status != GOT_STATUS_CONFLICT)
4634 return got_error_from_errno("strdup");
4636 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
4637 if (err || new == NULL)
4643 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
4645 struct got_pathlist_entry *pe;
4647 TAILQ_FOREACH(pe, merged_paths, entry)
4648 free((char *)pe->path);
4650 got_pathlist_free(merged_paths);
4653 static const struct got_error *
4654 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
4655 struct got_repository *repo)
4657 const struct got_error *err;
4658 struct got_reference *commit_ref = NULL;
4660 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4662 if (err->code != GOT_ERR_NOT_REF)
4664 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
4667 err = got_ref_write(commit_ref, repo);
4671 struct got_object_id *stored_id;
4674 err = got_ref_resolve(&stored_id, repo, commit_ref);
4677 cmp = got_object_id_cmp(commit_id, stored_id);
4680 err = got_error(GOT_ERR_REBASE_COMMITID);
4686 got_ref_close(commit_ref);
4690 static const struct got_error *
4691 rebase_merge_files(struct got_pathlist_head *merged_paths,
4692 const char *commit_ref_name, struct got_worktree *worktree,
4693 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
4694 struct got_object_id *commit_id, struct got_repository *repo,
4695 got_worktree_checkout_cb progress_cb, void *progress_arg,
4696 got_cancel_cb cancel_cb, void *cancel_arg)
4698 const struct got_error *err;
4699 struct got_reference *commit_ref = NULL;
4700 struct collect_merged_paths_arg cmp_arg;
4701 char *fileindex_path;
4703 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4705 err = get_fileindex_path(&fileindex_path, worktree);
4709 cmp_arg.progress_cb = progress_cb;
4710 cmp_arg.progress_arg = progress_arg;
4711 cmp_arg.merged_paths = merged_paths;
4712 err = merge_files(worktree, fileindex, fileindex_path,
4713 parent_commit_id, commit_id, repo, collect_merged_paths,
4714 &cmp_arg, cancel_cb, cancel_arg);
4716 got_ref_close(commit_ref);
4720 const struct got_error *
4721 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
4722 struct got_worktree *worktree, struct got_fileindex *fileindex,
4723 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4724 struct got_repository *repo,
4725 got_worktree_checkout_cb progress_cb, void *progress_arg,
4726 got_cancel_cb cancel_cb, void *cancel_arg)
4728 const struct got_error *err;
4729 char *commit_ref_name;
4731 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4735 err = store_commit_id(commit_ref_name, commit_id, repo);
4739 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4740 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4741 progress_arg, cancel_cb, cancel_arg);
4743 free(commit_ref_name);
4747 const struct got_error *
4748 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
4749 struct got_worktree *worktree, struct got_fileindex *fileindex,
4750 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4751 struct got_repository *repo,
4752 got_worktree_checkout_cb progress_cb, void *progress_arg,
4753 got_cancel_cb cancel_cb, void *cancel_arg)
4755 const struct got_error *err;
4756 char *commit_ref_name;
4758 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4762 err = store_commit_id(commit_ref_name, commit_id, repo);
4766 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4767 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4768 progress_arg, cancel_cb, cancel_arg);
4770 free(commit_ref_name);
4774 static const struct got_error *
4775 rebase_commit(struct got_object_id **new_commit_id,
4776 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
4777 struct got_worktree *worktree, struct got_fileindex *fileindex,
4778 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
4779 const char *new_logmsg, struct got_repository *repo)
4781 const struct got_error *err, *sync_err;
4782 struct got_pathlist_head commitable_paths;
4783 struct collect_commitables_arg cc_arg;
4784 char *fileindex_path = NULL;
4785 struct got_reference *head_ref = NULL;
4786 struct got_object_id *head_commit_id = NULL;
4787 char *logmsg = NULL;
4789 TAILQ_INIT(&commitable_paths);
4790 *new_commit_id = NULL;
4792 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4794 err = get_fileindex_path(&fileindex_path, worktree);
4798 cc_arg.commitable_paths = &commitable_paths;
4799 cc_arg.worktree = worktree;
4801 cc_arg.have_staged_files = 0;
4803 * If possible get the status of individual files directly to
4804 * avoid crawling the entire work tree once per rebased commit.
4805 * TODO: Ideally, merged_paths would contain a list of commitables
4806 * we could use so we could skip worktree_status() entirely.
4809 struct got_pathlist_entry *pe;
4810 if (TAILQ_EMPTY(merged_paths)) {
4811 err = got_error(GOT_ERR_NO_MERGED_PATHS);
4814 TAILQ_FOREACH(pe, merged_paths, entry) {
4815 err = worktree_status(worktree, pe->path, fileindex,
4816 repo, collect_commitables, &cc_arg, NULL, NULL);
4821 err = worktree_status(worktree, "", fileindex, repo,
4822 collect_commitables, &cc_arg, NULL, NULL);
4827 if (TAILQ_EMPTY(&commitable_paths)) {
4828 /* No-op change; commit will be elided. */
4829 err = got_ref_delete(commit_ref, repo);
4832 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4836 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4840 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4845 logmsg = strdup(new_logmsg);
4846 if (logmsg == NULL) {
4847 err = got_error_from_errno("strdup");
4851 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
4856 /* NB: commit_worktree will call free(logmsg) */
4857 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
4858 worktree, got_object_commit_get_author(orig_commit),
4859 got_object_commit_get_committer(orig_commit),
4860 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
4864 err = got_ref_change_ref(tmp_branch, *new_commit_id);
4868 err = got_ref_delete(commit_ref, repo);
4872 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4874 sync_err = sync_fileindex(fileindex, fileindex_path);
4875 if (sync_err && err == NULL)
4878 free(fileindex_path);
4879 free(head_commit_id);
4881 got_ref_close(head_ref);
4883 free(*new_commit_id);
4884 *new_commit_id = NULL;
4889 const struct got_error *
4890 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
4891 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4892 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4893 struct got_commit_object *orig_commit,
4894 struct got_object_id *orig_commit_id, struct got_repository *repo)
4896 const struct got_error *err;
4897 char *commit_ref_name;
4898 struct got_reference *commit_ref = NULL;
4899 struct got_object_id *commit_id = NULL;
4901 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4905 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4908 err = got_ref_resolve(&commit_id, repo, commit_ref);
4911 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4912 err = got_error(GOT_ERR_REBASE_COMMITID);
4916 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4917 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
4920 got_ref_close(commit_ref);
4921 free(commit_ref_name);
4926 const struct got_error *
4927 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
4928 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4929 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4930 struct got_commit_object *orig_commit,
4931 struct got_object_id *orig_commit_id, const char *new_logmsg,
4932 struct got_repository *repo)
4934 const struct got_error *err;
4935 char *commit_ref_name;
4936 struct got_reference *commit_ref = NULL;
4937 struct got_object_id *commit_id = NULL;
4939 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4943 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4946 err = got_ref_resolve(&commit_id, repo, commit_ref);
4949 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4950 err = got_error(GOT_ERR_HISTEDIT_COMMITID);
4954 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4955 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
4958 got_ref_close(commit_ref);
4959 free(commit_ref_name);
4964 const struct got_error *
4965 got_worktree_rebase_postpone(struct got_worktree *worktree,
4966 struct got_fileindex *fileindex)
4969 got_fileindex_free(fileindex);
4970 return lock_worktree(worktree, LOCK_SH);
4973 static const struct got_error *
4974 delete_ref(const char *name, struct got_repository *repo)
4976 const struct got_error *err;
4977 struct got_reference *ref;
4979 err = got_ref_open(&ref, repo, name, 0);
4981 if (err->code == GOT_ERR_NOT_REF)
4986 err = got_ref_delete(ref, repo);
4991 static const struct got_error *
4992 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
4994 const struct got_error *err;
4995 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4996 char *branch_ref_name = NULL, *commit_ref_name = NULL;
4998 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5001 err = delete_ref(tmp_branch_name, repo);
5005 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5008 err = delete_ref(new_base_branch_ref_name, repo);
5012 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5015 err = delete_ref(branch_ref_name, repo);
5019 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5022 err = delete_ref(commit_ref_name, repo);
5027 free(tmp_branch_name);
5028 free(new_base_branch_ref_name);
5029 free(branch_ref_name);
5030 free(commit_ref_name);
5034 const struct got_error *
5035 got_worktree_rebase_complete(struct got_worktree *worktree,
5036 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
5037 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
5038 struct got_repository *repo)
5040 const struct got_error *err, *unlockerr;
5041 struct got_object_id *new_head_commit_id = NULL;
5043 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5047 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
5051 err = got_ref_write(rebased_branch, repo);
5055 err = got_worktree_set_head_ref(worktree, rebased_branch);
5059 err = delete_rebase_refs(worktree, repo);
5062 got_fileindex_free(fileindex);
5063 free(new_head_commit_id);
5064 unlockerr = lock_worktree(worktree, LOCK_SH);
5065 if (unlockerr && err == NULL)
5070 const struct got_error *
5071 got_worktree_rebase_abort(struct got_worktree *worktree,
5072 struct got_fileindex *fileindex, struct got_repository *repo,
5073 struct got_reference *new_base_branch,
5074 got_worktree_checkout_cb progress_cb, void *progress_arg)
5076 const struct got_error *err, *unlockerr, *sync_err;
5077 struct got_reference *resolved = NULL;
5078 struct got_object_id *commit_id = NULL;
5079 char *fileindex_path = NULL;
5080 struct revert_file_args rfa;
5081 struct got_object_id *tree_id = NULL;
5083 err = lock_worktree(worktree, LOCK_EX);
5087 err = got_ref_open(&resolved, repo,
5088 got_ref_get_symref_target(new_base_branch), 0);
5092 err = got_worktree_set_head_ref(worktree, resolved);
5097 * XXX commits to the base branch could have happened while
5098 * we were busy rebasing; should we store the original commit ID
5099 * when rebase begins and read it back here?
5101 err = got_ref_resolve(&commit_id, repo, resolved);
5105 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5109 err = got_object_id_by_path(&tree_id, repo,
5110 worktree->base_commit_id, worktree->path_prefix);
5114 err = delete_rebase_refs(worktree, repo);
5118 err = get_fileindex_path(&fileindex_path, worktree);
5122 rfa.worktree = worktree;
5123 rfa.fileindex = fileindex;
5124 rfa.progress_cb = progress_cb;
5125 rfa.progress_arg = progress_arg;
5126 rfa.patch_cb = NULL;
5127 rfa.patch_arg = NULL;
5129 err = worktree_status(worktree, "", fileindex, repo,
5130 revert_file, &rfa, NULL, NULL);
5134 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5135 repo, progress_cb, progress_arg, NULL, NULL);
5137 sync_err = sync_fileindex(fileindex, fileindex_path);
5138 if (sync_err && err == NULL)
5141 got_ref_close(resolved);
5145 got_fileindex_free(fileindex);
5146 free(fileindex_path);
5148 unlockerr = lock_worktree(worktree, LOCK_SH);
5149 if (unlockerr && err == NULL)
5154 const struct got_error *
5155 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
5156 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
5157 struct got_fileindex **fileindex, struct got_worktree *worktree,
5158 struct got_repository *repo)
5160 const struct got_error *err = NULL;
5161 char *tmp_branch_name = NULL;
5162 char *branch_ref_name = NULL;
5163 char *base_commit_ref_name = NULL;
5164 char *fileindex_path = NULL;
5165 struct check_rebase_ok_arg ok_arg;
5166 struct got_reference *wt_branch = NULL;
5167 struct got_reference *base_commit_ref = NULL;
5171 *base_commit_id = NULL;
5174 err = lock_worktree(worktree, LOCK_EX);
5178 err = open_fileindex(fileindex, &fileindex_path, worktree);
5182 ok_arg.worktree = worktree;
5184 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5189 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5193 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5197 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5202 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5207 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
5211 err = got_ref_write(*branch_ref, repo);
5215 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
5216 worktree->base_commit_id);
5219 err = got_ref_write(base_commit_ref, repo);
5222 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
5223 if (*base_commit_id == NULL) {
5224 err = got_error_from_errno("got_object_id_dup");
5228 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5229 worktree->base_commit_id);
5232 err = got_ref_write(*tmp_branch, repo);
5236 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5240 free(fileindex_path);
5241 free(tmp_branch_name);
5242 free(branch_ref_name);
5243 free(base_commit_ref_name);
5245 got_ref_close(wt_branch);
5248 got_ref_close(*branch_ref);
5252 got_ref_close(*tmp_branch);
5255 free(*base_commit_id);
5257 got_fileindex_free(*fileindex);
5260 lock_worktree(worktree, LOCK_SH);
5265 const struct got_error *
5266 got_worktree_histedit_postpone(struct got_worktree *worktree,
5267 struct got_fileindex *fileindex)
5270 got_fileindex_free(fileindex);
5271 return lock_worktree(worktree, LOCK_SH);
5274 const struct got_error *
5275 got_worktree_histedit_in_progress(int *in_progress,
5276 struct got_worktree *worktree)
5278 const struct got_error *err;
5279 char *tmp_branch_name = NULL;
5281 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5285 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5286 free(tmp_branch_name);
5290 const struct got_error *
5291 got_worktree_histedit_continue(struct got_object_id **commit_id,
5292 struct got_reference **tmp_branch, struct got_reference **branch_ref,
5293 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
5294 struct got_worktree *worktree, struct got_repository *repo)
5296 const struct got_error *err;
5297 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
5298 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5299 struct got_reference *commit_ref = NULL;
5300 struct got_reference *base_commit_ref = NULL;
5301 char *fileindex_path = NULL;
5302 int have_staged_files = 0;
5306 *base_commit_id = NULL;
5309 err = lock_worktree(worktree, LOCK_EX);
5313 err = open_fileindex(fileindex, &fileindex_path, worktree);
5317 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5318 &have_staged_files);
5319 if (err && err->code != GOT_ERR_CANCELLED)
5321 if (have_staged_files) {
5322 err = got_error(GOT_ERR_STAGED_PATHS);
5326 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5330 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5334 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5338 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5343 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
5347 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5350 err = got_ref_resolve(commit_id, repo, commit_ref);
5354 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
5357 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
5361 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5365 free(commit_ref_name);
5366 free(branch_ref_name);
5367 free(fileindex_path);
5369 got_ref_close(commit_ref);
5370 if (base_commit_ref)
5371 got_ref_close(base_commit_ref);
5375 free(*base_commit_id);
5376 *base_commit_id = NULL;
5378 got_ref_close(*tmp_branch);
5382 got_fileindex_free(*fileindex);
5385 lock_worktree(worktree, LOCK_EX);
5390 static const struct got_error *
5391 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
5393 const struct got_error *err;
5394 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
5395 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5397 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5400 err = delete_ref(tmp_branch_name, repo);
5404 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5408 err = delete_ref(base_commit_ref_name, repo);
5412 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5415 err = delete_ref(branch_ref_name, repo);
5419 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5422 err = delete_ref(commit_ref_name, repo);
5426 free(tmp_branch_name);
5427 free(base_commit_ref_name);
5428 free(branch_ref_name);
5429 free(commit_ref_name);
5433 const struct got_error *
5434 got_worktree_histedit_abort(struct got_worktree *worktree,
5435 struct got_fileindex *fileindex, struct got_repository *repo,
5436 struct got_reference *branch, struct got_object_id *base_commit_id,
5437 got_worktree_checkout_cb progress_cb, void *progress_arg)
5439 const struct got_error *err, *unlockerr, *sync_err;
5440 struct got_reference *resolved = NULL;
5441 char *fileindex_path = NULL;
5442 struct got_object_id *tree_id = NULL;
5443 struct revert_file_args rfa;
5445 err = lock_worktree(worktree, LOCK_EX);
5449 err = got_ref_open(&resolved, repo,
5450 got_ref_get_symref_target(branch), 0);
5454 err = got_worktree_set_head_ref(worktree, resolved);
5458 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
5462 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
5463 worktree->path_prefix);
5467 err = delete_histedit_refs(worktree, repo);
5471 err = get_fileindex_path(&fileindex_path, worktree);
5475 rfa.worktree = worktree;
5476 rfa.fileindex = fileindex;
5477 rfa.progress_cb = progress_cb;
5478 rfa.progress_arg = progress_arg;
5479 rfa.patch_cb = NULL;
5480 rfa.patch_arg = NULL;
5482 err = worktree_status(worktree, "", fileindex, repo,
5483 revert_file, &rfa, NULL, NULL);
5487 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5488 repo, progress_cb, progress_arg, NULL, NULL);
5490 sync_err = sync_fileindex(fileindex, fileindex_path);
5491 if (sync_err && err == NULL)
5494 got_ref_close(resolved);
5496 free(fileindex_path);
5498 unlockerr = lock_worktree(worktree, LOCK_SH);
5499 if (unlockerr && err == NULL)
5504 const struct got_error *
5505 got_worktree_histedit_complete(struct got_worktree *worktree,
5506 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5507 struct got_reference *edited_branch, struct got_repository *repo)
5509 const struct got_error *err, *unlockerr;
5510 struct got_object_id *new_head_commit_id = NULL;
5511 struct got_reference *resolved = NULL;
5513 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5517 err = got_ref_open(&resolved, repo,
5518 got_ref_get_symref_target(edited_branch), 0);
5522 err = got_ref_change_ref(resolved, new_head_commit_id);
5526 err = got_ref_write(resolved, repo);
5530 err = got_worktree_set_head_ref(worktree, resolved);
5534 err = delete_histedit_refs(worktree, repo);
5537 got_fileindex_free(fileindex);
5538 free(new_head_commit_id);
5539 unlockerr = lock_worktree(worktree, LOCK_SH);
5540 if (unlockerr && err == NULL)
5545 const struct got_error *
5546 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
5547 struct got_object_id *commit_id, struct got_repository *repo)
5549 const struct got_error *err;
5550 char *commit_ref_name;
5552 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5556 err = store_commit_id(commit_ref_name, commit_id, repo);
5560 err = delete_ref(commit_ref_name, repo);
5562 free(commit_ref_name);
5566 struct check_stage_ok_arg {
5567 struct got_object_id *head_commit_id;
5568 struct got_worktree *worktree;
5569 struct got_fileindex *fileindex;
5570 struct got_repository *repo;
5574 const struct got_error *
5575 check_stage_ok(void *arg, unsigned char status,
5576 unsigned char staged_status, const char *relpath,
5577 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5578 struct got_object_id *commit_id)
5580 struct check_stage_ok_arg *a = arg;
5581 const struct got_error *err = NULL;
5582 struct got_fileindex_entry *ie;
5583 struct got_object_id base_commit_id;
5584 struct got_object_id *base_commit_idp = NULL;
5585 char *in_repo_path = NULL, *p;
5587 if (status == GOT_STATUS_UNVERSIONED)
5589 if (status == GOT_STATUS_NONEXISTENT)
5590 return got_error_set_errno(ENOENT, relpath);
5592 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5594 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
5596 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
5597 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5599 return got_error_from_errno("asprintf");
5601 if (got_fileindex_entry_has_commit(ie)) {
5602 memcpy(base_commit_id.sha1, ie->commit_sha1,
5603 SHA1_DIGEST_LENGTH);
5604 base_commit_idp = &base_commit_id;
5607 if (status == GOT_STATUS_NO_CHANGE) {
5608 err = got_error_path(ie->path, GOT_ERR_STAGE_NO_CHANGE);
5610 } else if (status == GOT_STATUS_CONFLICT) {
5611 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
5613 } else if (status != GOT_STATUS_ADD &&
5614 status != GOT_STATUS_MODIFY &&
5615 status != GOT_STATUS_DELETE) {
5616 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
5620 a->have_changes = 1;
5625 err = check_out_of_date(p, status, staged_status,
5626 blob_id, base_commit_idp, a->head_commit_id, a->repo,
5627 GOT_ERR_STAGE_OUT_OF_DATE);
5633 struct stage_path_arg {
5634 struct got_worktree *worktree;
5635 struct got_fileindex *fileindex;
5636 struct got_repository *repo;
5637 got_worktree_status_cb status_cb;
5639 got_worktree_patch_cb patch_cb;
5643 static const struct got_error *
5644 stage_path(void *arg, unsigned char status,
5645 unsigned char staged_status, const char *relpath,
5646 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5647 struct got_object_id *commit_id)
5649 struct stage_path_arg *a = arg;
5650 const struct got_error *err = NULL;
5651 struct got_fileindex_entry *ie;
5652 char *ondisk_path = NULL, *path_content = NULL;
5654 struct got_object_id *new_staged_blob_id = NULL;
5656 if (status == GOT_STATUS_UNVERSIONED)
5659 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5661 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
5663 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
5665 return got_error_from_errno("asprintf");
5668 case GOT_STATUS_ADD:
5669 case GOT_STATUS_MODIFY:
5671 if (status == GOT_STATUS_ADD) {
5672 int choice = GOT_PATCH_CHOICE_NONE;
5673 err = (*a->patch_cb)(&choice, a->patch_arg,
5674 status, ie->path, NULL, 1, 1);
5677 if (choice != GOT_PATCH_CHOICE_YES)
5680 err = create_patched_content(&path_content, 0,
5681 staged_blob_id ? staged_blob_id : blob_id,
5682 ondisk_path, ie->path, a->repo,
5683 a->patch_cb, a->patch_arg);
5684 if (err || path_content == NULL)
5688 err = got_object_blob_create(&new_staged_blob_id,
5689 path_content ? path_content : ondisk_path, a->repo);
5692 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
5693 SHA1_DIGEST_LENGTH);
5694 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
5695 stage = GOT_FILEIDX_STAGE_ADD;
5697 stage = GOT_FILEIDX_STAGE_MODIFY;
5698 got_fileindex_entry_stage_set(ie, stage);
5699 if (a->status_cb == NULL)
5701 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
5702 get_staged_status(ie), relpath, blob_id,
5703 new_staged_blob_id, NULL);
5705 case GOT_STATUS_DELETE:
5706 if (staged_status == GOT_STATUS_DELETE)
5709 int choice = GOT_PATCH_CHOICE_NONE;
5710 err = (*a->patch_cb)(&choice, a->patch_arg, status,
5711 ie->path, NULL, 1, 1);
5714 if (choice == GOT_PATCH_CHOICE_NO)
5716 if (choice != GOT_PATCH_CHOICE_YES) {
5717 err = got_error(GOT_ERR_PATCH_CHOICE);
5721 stage = GOT_FILEIDX_STAGE_DELETE;
5722 got_fileindex_entry_stage_set(ie, stage);
5723 if (a->status_cb == NULL)
5725 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
5726 get_staged_status(ie), relpath, NULL, NULL, NULL);
5728 case GOT_STATUS_NO_CHANGE:
5729 err = got_error_path(relpath, GOT_ERR_STAGE_NO_CHANGE);
5731 case GOT_STATUS_CONFLICT:
5732 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
5734 case GOT_STATUS_NONEXISTENT:
5735 err = got_error_set_errno(ENOENT, relpath);
5738 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
5742 if (path_content && unlink(path_content) == -1 && err == NULL)
5743 err = got_error_from_errno2("unlink", path_content);
5746 free(new_staged_blob_id);
5750 const struct got_error *
5751 got_worktree_stage(struct got_worktree *worktree,
5752 struct got_pathlist_head *paths,
5753 got_worktree_status_cb status_cb, void *status_arg,
5754 got_worktree_patch_cb patch_cb, void *patch_arg,
5755 struct got_repository *repo)
5757 const struct got_error *err = NULL, *sync_err, *unlockerr;
5758 struct got_pathlist_entry *pe;
5759 struct got_fileindex *fileindex = NULL;
5760 char *fileindex_path = NULL;
5761 struct got_reference *head_ref = NULL;
5762 struct got_object_id *head_commit_id = NULL;
5763 struct check_stage_ok_arg oka;
5764 struct stage_path_arg spa;
5766 err = lock_worktree(worktree, LOCK_EX);
5770 err = got_ref_open(&head_ref, repo,
5771 got_worktree_get_head_ref_name(worktree), 0);
5774 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5777 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5781 /* Check pre-conditions before staging anything. */
5782 oka.head_commit_id = head_commit_id;
5783 oka.worktree = worktree;
5784 oka.fileindex = fileindex;
5786 oka.have_changes = 0;
5787 TAILQ_FOREACH(pe, paths, entry) {
5788 err = worktree_status(worktree, pe->path, fileindex, repo,
5789 check_stage_ok, &oka, NULL, NULL);
5793 if (!oka.have_changes) {
5794 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
5798 spa.worktree = worktree;
5799 spa.fileindex = fileindex;
5801 spa.patch_cb = patch_cb;
5802 spa.patch_arg = patch_arg;
5803 spa.status_cb = status_cb;
5804 spa.status_arg = status_arg;
5805 TAILQ_FOREACH(pe, paths, entry) {
5806 err = worktree_status(worktree, pe->path, fileindex, repo,
5807 stage_path, &spa, NULL, NULL);
5812 sync_err = sync_fileindex(fileindex, fileindex_path);
5813 if (sync_err && err == NULL)
5817 got_ref_close(head_ref);
5818 free(head_commit_id);
5819 free(fileindex_path);
5821 got_fileindex_free(fileindex);
5822 unlockerr = lock_worktree(worktree, LOCK_SH);
5823 if (unlockerr && err == NULL)
5828 struct unstage_path_arg {
5829 struct got_worktree *worktree;
5830 struct got_fileindex *fileindex;
5831 struct got_repository *repo;
5832 got_worktree_checkout_cb progress_cb;
5834 got_worktree_patch_cb patch_cb;
5838 static const struct got_error *
5839 create_unstaged_content(char **path_unstaged_content,
5840 char **path_new_staged_content, struct got_object_id *blob_id,
5841 struct got_object_id *staged_blob_id, const char *relpath,
5842 struct got_repository *repo,
5843 got_worktree_patch_cb patch_cb, void *patch_arg)
5845 const struct got_error *err;
5846 struct got_blob_object *blob = NULL, *staged_blob = NULL;
5847 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
5848 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
5849 struct stat sb1, sb2;
5850 struct got_diff_changes *changes = NULL;
5851 struct got_diff_state *ds = NULL;
5852 struct got_diff_args *args = NULL;
5853 struct got_diff_change *change;
5854 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
5855 int have_content = 0, have_rejected_content = 0;
5857 *path_unstaged_content = NULL;
5858 *path_new_staged_content = NULL;
5860 err = got_object_id_str(&label1, blob_id);
5863 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
5867 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
5871 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
5875 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
5879 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
5883 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
5887 if (stat(path1, &sb1) == -1) {
5888 err = got_error_from_errno2("stat", path1);
5892 if (stat(path2, &sb2) == -1) {
5893 err = got_error_from_errno2("stat", path2);
5897 err = got_diff_files(&changes, &ds, &args, &diff_flags,
5898 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
5902 err = got_opentemp_named(path_unstaged_content, &outfile,
5903 "got-unstaged-content");
5906 err = got_opentemp_named(path_new_staged_content, &rejectfile,
5907 "got-new-staged-content");
5911 if (fseek(f1, 0L, SEEK_SET) == -1) {
5912 err = got_ferror(f1, GOT_ERR_IO);
5915 if (fseek(f2, 0L, SEEK_SET) == -1) {
5916 err = got_ferror(f2, GOT_ERR_IO);
5919 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
5921 err = apply_or_reject_change(&choice, change, ++n,
5922 changes->nchanges, ds, args, diff_flags, relpath,
5923 f1, f2, &line_cur1, &line_cur2,
5924 outfile, rejectfile, patch_cb, patch_arg);
5927 if (choice == GOT_PATCH_CHOICE_YES)
5930 have_rejected_content = 1;
5931 if (choice == GOT_PATCH_CHOICE_QUIT)
5934 if (have_content || have_rejected_content)
5935 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
5936 outfile, rejectfile);
5940 got_object_blob_close(blob);
5942 got_object_blob_close(staged_blob);
5943 if (f1 && fclose(f1) == EOF && err == NULL)
5944 err = got_error_from_errno2("fclose", path1);
5945 if (f2 && fclose(f2) == EOF && err == NULL)
5946 err = got_error_from_errno2("fclose", path2);
5947 if (outfile && fclose(outfile) == EOF && err == NULL)
5948 err = got_error_from_errno2("fclose", *path_unstaged_content);
5949 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
5950 err = got_error_from_errno2("fclose", *path_new_staged_content);
5951 if (path1 && unlink(path1) == -1 && err == NULL)
5952 err = got_error_from_errno2("unlink", path1);
5953 if (path2 && unlink(path2) == -1 && err == NULL)
5954 err = got_error_from_errno2("unlink", path2);
5955 if (err || !have_content) {
5956 if (*path_unstaged_content &&
5957 unlink(*path_unstaged_content) == -1 && err == NULL)
5958 err = got_error_from_errno2("unlink",
5959 *path_unstaged_content);
5960 free(*path_unstaged_content);
5961 *path_unstaged_content = NULL;
5963 if (err || !have_rejected_content) {
5964 if (*path_new_staged_content &&
5965 unlink(*path_new_staged_content) == -1 && err == NULL)
5966 err = got_error_from_errno2("unlink",
5967 *path_new_staged_content);
5968 free(*path_new_staged_content);
5969 *path_new_staged_content = NULL;
5973 got_diff_state_free(ds);
5977 got_diff_free_changes(changes);
5983 static const struct got_error *
5984 unstage_path(void *arg, unsigned char status,
5985 unsigned char staged_status, const char *relpath,
5986 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5987 struct got_object_id *commit_id)
5989 const struct got_error *err = NULL;
5990 struct unstage_path_arg *a = arg;
5991 struct got_fileindex_entry *ie;
5992 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
5993 char *ondisk_path = NULL, *path_unstaged_content = NULL;
5994 char *path_new_staged_content = NULL;
5995 int local_changes_subsumed;
5998 if (staged_status != GOT_STATUS_ADD &&
5999 staged_status != GOT_STATUS_MODIFY &&
6000 staged_status != GOT_STATUS_DELETE)
6003 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6005 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6007 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
6009 return got_error_from_errno("asprintf");
6011 switch (staged_status) {
6012 case GOT_STATUS_MODIFY:
6013 err = got_object_open_as_blob(&blob_base, a->repo,
6018 case GOT_STATUS_ADD:
6020 if (staged_status == GOT_STATUS_ADD) {
6021 int choice = GOT_PATCH_CHOICE_NONE;
6022 err = (*a->patch_cb)(&choice, a->patch_arg,
6023 staged_status, ie->path, NULL, 1, 1);
6026 if (choice != GOT_PATCH_CHOICE_YES)
6029 err = create_unstaged_content(
6030 &path_unstaged_content,
6031 &path_new_staged_content, blob_id,
6032 staged_blob_id, ie->path, a->repo,
6033 a->patch_cb, a->patch_arg);
6034 if (err || path_unstaged_content == NULL)
6036 if (path_new_staged_content) {
6037 err = got_object_blob_create(
6039 path_new_staged_content,
6043 memcpy(ie->staged_blob_sha1,
6044 staged_blob_id->sha1,
6045 SHA1_DIGEST_LENGTH);
6047 err = merge_file(&local_changes_subsumed,
6048 a->worktree, blob_base, ondisk_path,
6049 relpath, got_fileindex_perms_to_st(ie),
6050 path_unstaged_content, "unstaged",
6051 a->repo, a->progress_cb, a->progress_arg);
6053 path_new_staged_content == NULL)
6054 got_fileindex_entry_stage_set(ie,
6055 GOT_FILEIDX_STAGE_NONE);
6056 break; /* Done with this file. */
6059 err = got_object_open_as_blob(&blob_staged, a->repo,
6060 staged_blob_id, 8192);
6063 err = merge_blob(&local_changes_subsumed, a->worktree,
6064 blob_base, ondisk_path, relpath,
6065 got_fileindex_perms_to_st(ie), blob_staged,
6066 commit_id ? commit_id : a->worktree->base_commit_id,
6067 a->repo, a->progress_cb, a->progress_arg);
6069 got_fileindex_entry_stage_set(ie,
6070 GOT_FILEIDX_STAGE_NONE);
6072 case GOT_STATUS_DELETE:
6074 int choice = GOT_PATCH_CHOICE_NONE;
6075 err = (*a->patch_cb)(&choice, a->patch_arg,
6076 staged_status, ie->path, NULL, 1, 1);
6079 if (choice == GOT_PATCH_CHOICE_NO)
6081 if (choice != GOT_PATCH_CHOICE_YES) {
6082 err = got_error(GOT_ERR_PATCH_CHOICE);
6086 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
6087 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
6090 err = (*a->progress_cb)(a->progress_arg, status, relpath);
6095 if (path_unstaged_content &&
6096 unlink(path_unstaged_content) == -1 && err == NULL)
6097 err = got_error_from_errno2("unlink", path_unstaged_content);
6098 if (path_new_staged_content &&
6099 unlink(path_new_staged_content) == -1 && err == NULL)
6100 err = got_error_from_errno2("unlink", path_new_staged_content);
6101 free(path_unstaged_content);
6102 free(path_new_staged_content);
6104 got_object_blob_close(blob_base);
6106 got_object_blob_close(blob_staged);
6110 const struct got_error *
6111 got_worktree_unstage(struct got_worktree *worktree,
6112 struct got_pathlist_head *paths,
6113 got_worktree_checkout_cb progress_cb, void *progress_arg,
6114 got_worktree_patch_cb patch_cb, void *patch_arg,
6115 struct got_repository *repo)
6117 const struct got_error *err = NULL, *sync_err, *unlockerr;
6118 struct got_pathlist_entry *pe;
6119 struct got_fileindex *fileindex = NULL;
6120 char *fileindex_path = NULL;
6121 struct unstage_path_arg upa;
6123 err = lock_worktree(worktree, LOCK_EX);
6127 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6131 upa.worktree = worktree;
6132 upa.fileindex = fileindex;
6134 upa.progress_cb = progress_cb;
6135 upa.progress_arg = progress_arg;
6136 upa.patch_cb = patch_cb;
6137 upa.patch_arg = patch_arg;
6138 TAILQ_FOREACH(pe, paths, entry) {
6139 err = worktree_status(worktree, pe->path, fileindex, repo,
6140 unstage_path, &upa, NULL, NULL);
6145 sync_err = sync_fileindex(fileindex, fileindex_path);
6146 if (sync_err && err == NULL)
6149 free(fileindex_path);
6151 got_fileindex_free(fileindex);
6152 unlockerr = lock_worktree(worktree, LOCK_SH);
6153 if (unlockerr && err == NULL)