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>
37 #include "got_error.h"
38 #include "got_repository.h"
39 #include "got_reference.h"
40 #include "got_object.h"
42 #include "got_cancel.h"
43 #include "got_worktree.h"
44 #include "got_opentemp.h"
47 #include "got_lib_worktree.h"
48 #include "got_lib_sha1.h"
49 #include "got_lib_fileindex.h"
50 #include "got_lib_inflate.h"
51 #include "got_lib_delta.h"
52 #include "got_lib_object.h"
53 #include "got_lib_object_parse.h"
54 #include "got_lib_object_create.h"
55 #include "got_lib_object_idset.h"
56 #include "got_lib_diff.h"
59 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
62 static const struct got_error *
63 create_meta_file(const char *path_got, const char *name, const char *content)
65 const struct got_error *err = NULL;
68 if (asprintf(&path, "%s/%s", path_got, name) == -1)
69 return got_error_from_errno("asprintf");
71 err = got_path_create_file(path, content);
76 static const struct got_error *
77 update_meta_file(const char *path_got, const char *name, const char *content)
79 const struct got_error *err = NULL;
84 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
85 err = got_error_from_errno("asprintf");
90 err = got_opentemp_named(&tmppath, &tmpfile, path);
95 int len = fprintf(tmpfile, "%s\n", content);
96 if (len != strlen(content) + 1) {
97 err = got_error_from_errno2("fprintf", tmppath);
102 if (rename(tmppath, path) != 0) {
103 err = got_error_from_errno3("rename", tmppath, path);
109 if (fclose(tmpfile) != 0 && err == NULL)
110 err = got_error_from_errno2("fclose", tmppath);
115 static const struct got_error *
116 read_meta_file(char **content, const char *path_got, const char *name)
118 const struct got_error *err = NULL;
126 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
127 err = got_error_from_errno("asprintf");
132 fd = open(path, O_RDONLY | O_NOFOLLOW);
135 err = got_error_path(path, GOT_ERR_WORKTREE_META);
137 err = got_error_from_errno2("open", path);
140 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
141 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
142 : got_error_from_errno2("flock", path));
146 if (fstat(fd, &sb) != 0) {
147 err = got_error_from_errno2("fstat", path);
150 *content = calloc(1, sb.st_size);
151 if (*content == NULL) {
152 err = got_error_from_errno("calloc");
156 n = read(fd, *content, sb.st_size);
157 if (n != sb.st_size) {
158 err = (n == -1 ? got_error_from_errno2("read", path) :
159 got_error_path(path, GOT_ERR_WORKTREE_META));
162 if ((*content)[sb.st_size - 1] != '\n') {
163 err = got_error_path(path, GOT_ERR_WORKTREE_META);
166 (*content)[sb.st_size - 1] = '\0';
169 if (fd != -1 && close(fd) == -1 && err == NULL)
170 err = got_error_from_errno2("close", path_got);
179 static const struct got_error *
180 write_head_ref(const char *path_got, struct got_reference *head_ref)
182 const struct got_error *err = NULL;
185 if (got_ref_is_symbolic(head_ref)) {
186 refstr = got_ref_to_str(head_ref);
188 return got_error_from_errno("got_ref_to_str");
190 refstr = strdup(got_ref_get_name(head_ref));
192 return got_error_from_errno("strdup");
194 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
199 const struct got_error *
200 got_worktree_init(const char *path, struct got_reference *head_ref,
201 const char *prefix, struct got_repository *repo)
203 const struct got_error *err = NULL;
204 struct got_object_id *commit_id = NULL;
206 uint32_t uuid_status;
208 char *path_got = NULL;
209 char *formatstr = NULL;
210 char *absprefix = NULL;
211 char *basestr = NULL;
212 char *uuidstr = NULL;
214 if (strcmp(path, got_repo_get_path(repo)) == 0) {
215 err = got_error(GOT_ERR_WORKTREE_REPO);
219 err = got_ref_resolve(&commit_id, repo, head_ref);
222 err = got_object_get_type(&obj_type, repo, commit_id);
225 if (obj_type != GOT_OBJ_TYPE_COMMIT)
226 return got_error(GOT_ERR_OBJ_TYPE);
228 if (!got_path_is_absolute(prefix)) {
229 if (asprintf(&absprefix, "/%s", prefix) == -1)
230 return got_error_from_errno("asprintf");
233 /* Create top-level directory (may already exist). */
234 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
235 err = got_error_from_errno2("mkdir", path);
239 /* Create .got directory (may already exist). */
240 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
241 err = got_error_from_errno("asprintf");
244 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
245 err = got_error_from_errno2("mkdir", path_got);
249 /* Create an empty lock file. */
250 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
254 /* Create an empty file index. */
255 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
259 /* Write the HEAD reference. */
260 err = write_head_ref(path_got, head_ref);
264 /* Record our base commit. */
265 err = got_object_id_str(&basestr, commit_id);
268 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
272 /* Store path to repository. */
273 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
274 got_repo_get_path(repo));
278 /* Store in-repository path prefix. */
279 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
280 absprefix ? absprefix : prefix);
285 uuid_create(&uuid, &uuid_status);
286 if (uuid_status != uuid_s_ok) {
287 err = got_error_uuid(uuid_status, "uuid_create");
290 uuid_to_string(&uuid, &uuidstr, &uuid_status);
291 if (uuid_status != uuid_s_ok) {
292 err = got_error_uuid(uuid_status, "uuid_to_string");
295 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
299 /* Stamp work tree with format file. */
300 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
301 err = got_error_from_errno("asprintf");
304 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
318 static const struct got_error *
319 open_worktree(struct got_worktree **worktree, const char *path)
321 const struct got_error *err = NULL;
323 char *formatstr = NULL;
324 char *uuidstr = NULL;
325 char *path_lock = NULL;
326 char *base_commit_id_str = NULL;
327 int version, fd = -1;
329 struct got_repository *repo = NULL;
330 uint32_t uuid_status;
334 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
335 err = got_error_from_errno("asprintf");
340 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
341 err = got_error_from_errno("asprintf");
346 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
348 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
349 : got_error_from_errno2("open", path_lock));
353 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
357 version = strtonum(formatstr, 1, INT_MAX, &errstr);
359 err = got_error_msg(GOT_ERR_WORKTREE_META,
360 "could not parse work tree format version number");
363 if (version != GOT_WORKTREE_FORMAT_VERSION) {
364 err = got_error(GOT_ERR_WORKTREE_VERS);
368 *worktree = calloc(1, sizeof(**worktree));
369 if (*worktree == NULL) {
370 err = got_error_from_errno("calloc");
373 (*worktree)->lockfd = -1;
375 (*worktree)->root_path = strdup(path);
376 if ((*worktree)->root_path == NULL) {
377 err = got_error_from_errno("strdup");
380 err = read_meta_file(&(*worktree)->repo_path, path_got,
381 GOT_WORKTREE_REPOSITORY);
385 err = read_meta_file(&(*worktree)->path_prefix, path_got,
386 GOT_WORKTREE_PATH_PREFIX);
390 err = read_meta_file(&base_commit_id_str, path_got,
391 GOT_WORKTREE_BASE_COMMIT);
395 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
398 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
399 if (uuid_status != uuid_s_ok) {
400 err = got_error_uuid(uuid_status, "uuid_from_string");
404 err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
408 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
413 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
414 GOT_WORKTREE_HEAD_REF);
417 got_repo_close(repo);
420 free(base_commit_id_str);
426 if (*worktree != NULL)
427 got_worktree_close(*worktree);
430 (*worktree)->lockfd = fd;
435 const struct got_error *
436 got_worktree_open(struct got_worktree **worktree, const char *path)
438 const struct got_error *err = NULL;
441 err = open_worktree(worktree, path);
442 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
446 path = dirname(path);
448 return got_error_from_errno2("dirname", path);
449 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
451 return got_error(GOT_ERR_NOT_WORKTREE);
454 const struct got_error *
455 got_worktree_close(struct got_worktree *worktree)
457 const struct got_error *err = NULL;
458 free(worktree->repo_path);
459 free(worktree->path_prefix);
460 free(worktree->base_commit_id);
461 free(worktree->head_ref_name);
462 if (worktree->lockfd != -1)
463 if (close(worktree->lockfd) != 0)
464 err = got_error_from_errno2("close",
465 got_worktree_get_root_path(worktree));
466 free(worktree->root_path);
472 got_worktree_get_root_path(struct got_worktree *worktree)
474 return worktree->root_path;
478 got_worktree_get_repo_path(struct got_worktree *worktree)
480 return worktree->repo_path;
484 got_worktree_get_path_prefix(struct got_worktree *worktree)
486 return worktree->path_prefix;
489 const struct got_error *
490 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
491 const char *path_prefix)
493 char *absprefix = NULL;
495 if (!got_path_is_absolute(path_prefix)) {
496 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
497 return got_error_from_errno("asprintf");
499 *match = (strcmp(absprefix ? absprefix : path_prefix,
500 worktree->path_prefix) == 0);
506 got_worktree_get_head_ref_name(struct got_worktree *worktree)
508 return worktree->head_ref_name;
511 const struct got_error *
512 got_worktree_set_head_ref(struct got_worktree *worktree,
513 struct got_reference *head_ref)
515 const struct got_error *err = NULL;
516 char *path_got = NULL, *head_ref_name = NULL;
518 if (asprintf(&path_got, "%s/%s", worktree->root_path,
519 GOT_WORKTREE_GOT_DIR) == -1) {
520 err = got_error_from_errno("asprintf");
525 head_ref_name = strdup(got_ref_get_name(head_ref));
526 if (head_ref_name == NULL) {
527 err = got_error_from_errno("strdup");
531 err = write_head_ref(path_got, head_ref);
535 free(worktree->head_ref_name);
536 worktree->head_ref_name = head_ref_name;
544 struct got_object_id *
545 got_worktree_get_base_commit_id(struct got_worktree *worktree)
547 return worktree->base_commit_id;
550 const struct got_error *
551 got_worktree_set_base_commit_id(struct got_worktree *worktree,
552 struct got_repository *repo, struct got_object_id *commit_id)
554 const struct got_error *err;
555 struct got_object *obj = NULL;
557 char *path_got = NULL;
559 if (asprintf(&path_got, "%s/%s", worktree->root_path,
560 GOT_WORKTREE_GOT_DIR) == -1) {
561 err = got_error_from_errno("asprintf");
566 err = got_object_open(&obj, repo, commit_id);
570 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
571 err = got_error(GOT_ERR_OBJ_TYPE);
575 /* Record our base commit. */
576 err = got_object_id_str(&id_str, commit_id);
579 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
583 free(worktree->base_commit_id);
584 worktree->base_commit_id = got_object_id_dup(commit_id);
585 if (worktree->base_commit_id == NULL) {
586 err = got_error_from_errno("got_object_id_dup");
591 got_object_close(obj);
597 static const struct got_error *
598 lock_worktree(struct got_worktree *worktree, int operation)
600 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
601 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
602 : got_error_from_errno2("flock",
603 got_worktree_get_root_path(worktree)));
607 static const struct got_error *
608 add_dir_on_disk(struct got_worktree *worktree, const char *path)
610 const struct got_error *err = NULL;
613 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
614 return got_error_from_errno("asprintf");
616 err = got_path_mkdir(abspath);
617 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
620 if (lstat(abspath, &sb) == -1) {
621 err = got_error_from_errno2("lstat", abspath);
622 } else if (!S_ISDIR(sb.st_mode)) {
623 /* TODO directory is obstructed; do something */
624 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
631 static const struct got_error *
632 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
634 const struct got_error *err = NULL;
637 size_t flen1 = 0, flen2 = 0;
642 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
643 if (flen1 == 0 && ferror(f1)) {
644 err = got_error_from_errno("fread");
647 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
648 if (flen2 == 0 && ferror(f2)) {
649 err = got_error_from_errno("fread");
656 } else if (flen2 == 0) {
660 } else if (flen1 == flen2) {
661 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
674 static const struct got_error *
675 check_files_equal(int *same, const char *f1_path, const char *f2_path)
677 const struct got_error *err = NULL;
680 FILE *f1 = NULL, *f2 = NULL;
684 if (lstat(f1_path, &sb) != 0) {
685 err = got_error_from_errno2("lstat", f1_path);
690 if (lstat(f2_path, &sb) != 0) {
691 err = got_error_from_errno2("lstat", f2_path);
696 if (size1 != size2) {
701 f1 = fopen(f1_path, "r");
703 return got_error_from_errno2("open", f1_path);
705 f2 = fopen(f2_path, "r");
707 err = got_error_from_errno2("open", f2_path);
711 err = check_file_contents_equal(same, f1, f2);
713 if (f1 && fclose(f1) != 0 && err == NULL)
714 err = got_error_from_errno("fclose");
715 if (f2 && fclose(f2) != 0 && err == NULL)
716 err = got_error_from_errno("fclose");
722 * Perform a 3-way merge where blob_orig acts as the common ancestor,
723 * the file at deriv_path acts as the first derived version, and the
724 * file on disk acts as the second derived version.
726 static const struct got_error *
727 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
728 struct got_blob_object *blob_orig, const char *ondisk_path,
729 const char *path, uint16_t st_mode, const char *deriv_path,
730 const char *label_deriv, struct got_repository *repo,
731 got_worktree_checkout_cb progress_cb, void *progress_arg)
733 const struct got_error *err = NULL;
736 char *blob_orig_path = NULL;
737 char *merged_path = NULL, *base_path = NULL;
741 *local_changes_subsumed = 0;
743 parent = dirname(ondisk_path);
745 return got_error_from_errno2("dirname", ondisk_path);
747 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
748 return got_error_from_errno("asprintf");
750 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
755 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
756 err = got_error_from_errno("asprintf");
761 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
765 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
771 * If the file has no blob, this is an "add vs add" conflict,
772 * and we simply use an empty ancestor file to make both files
773 * appear in the merged result in their entirety.
777 err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
778 blob_orig_path, ondisk_path, label_deriv, path);
782 err = (*progress_cb)(progress_arg,
783 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
787 if (fsync(merged_fd) != 0) {
788 err = got_error_from_errno("fsync");
792 /* Check if a clean merge has subsumed all local changes. */
793 if (overlapcnt == 0) {
794 err = check_files_equal(local_changes_subsumed, deriv_path,
800 if (chmod(merged_path, st_mode) != 0) {
801 err = got_error_from_errno2("chmod", merged_path);
805 if (rename(merged_path, ondisk_path) != 0) {
806 err = got_error_from_errno3("rename", merged_path,
813 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
814 err = got_error_from_errno("close");
815 if (f_orig && fclose(f_orig) != 0 && err == NULL)
816 err = got_error_from_errno("fclose");
819 if (blob_orig_path) {
820 unlink(blob_orig_path);
821 free(blob_orig_path);
827 * Perform a 3-way merge where blob_orig acts as the common ancestor,
828 * blob_deriv acts as the first derived version, and the file on disk
829 * acts as the second derived version.
831 static const struct got_error *
832 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
833 struct got_blob_object *blob_orig, const char *ondisk_path,
834 const char *path, uint16_t st_mode, struct got_blob_object *blob_deriv,
835 struct got_object_id *deriv_base_commit_id,
836 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
839 const struct got_error *err = NULL;
840 FILE *f_deriv = NULL;
841 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
842 char *label_deriv = NULL, *parent;
844 *local_changes_subsumed = 0;
846 parent = dirname(ondisk_path);
848 return got_error_from_errno2("dirname", ondisk_path);
851 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
852 err = got_error_from_errno("asprintf");
857 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
860 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
865 err = got_object_id_str(&id_str, deriv_base_commit_id);
868 if (asprintf(&label_deriv, "commit %s", id_str) == -1) {
869 err = got_error_from_errno("asprintf");
873 err = merge_file(local_changes_subsumed, worktree, blob_orig,
874 ondisk_path, path, st_mode, blob_deriv_path, label_deriv,
875 repo, progress_cb, progress_arg);
877 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
878 err = got_error_from_errno("fclose");
880 if (blob_deriv_path) {
881 unlink(blob_deriv_path);
882 free(blob_deriv_path);
889 static const struct got_error *
890 update_blob_fileindex_entry(struct got_worktree *worktree,
891 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
892 const char *ondisk_path, const char *path, struct got_blob_object *blob,
893 int update_timestamps)
895 const struct got_error *err = NULL;
898 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
900 err = got_fileindex_entry_update(ie, ondisk_path,
901 blob->id.sha1, worktree->base_commit_id->sha1,
904 struct got_fileindex_entry *new_ie;
905 err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
906 path, blob->id.sha1, worktree->base_commit_id->sha1);
908 err = got_fileindex_entry_add(fileindex, new_ie);
913 static const struct got_error *
914 install_blob(struct got_worktree *worktree, const char *ondisk_path,
915 const char *path, uint16_t te_mode, uint16_t st_mode,
916 struct got_blob_object *blob, int restoring_missing_file,
917 int reverting_versioned_file, struct got_repository *repo,
918 got_worktree_checkout_cb progress_cb, void *progress_arg)
920 const struct got_error *err = NULL;
924 char *tmppath = NULL;
926 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
927 GOT_DEFAULT_FILE_MODE);
929 if (errno == ENOENT) {
930 char *parent = dirname(path);
932 return got_error_from_errno2("dirname", path);
933 err = add_dir_on_disk(worktree, parent);
936 fd = open(ondisk_path,
937 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
938 GOT_DEFAULT_FILE_MODE);
940 return got_error_from_errno2("open",
942 } else if (errno == EEXIST) {
943 if (!S_ISREG(st_mode)) {
944 /* TODO file is obstructed; do something */
945 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
948 err = got_opentemp_named_fd(&tmppath, &fd,
955 return got_error_from_errno2("open", ondisk_path);
958 if (restoring_missing_file)
959 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
960 else if (reverting_versioned_file)
961 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
963 err = (*progress_cb)(progress_arg,
964 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
968 hdrlen = got_object_blob_get_hdrlen(blob);
970 const uint8_t *buf = got_object_blob_get_read_buf(blob);
971 err = got_object_blob_read_block(&len, blob);
975 /* Skip blob object header first time around. */
976 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
978 err = got_error_from_errno("write");
980 } else if (outlen != len - hdrlen) {
981 err = got_error(GOT_ERR_IO);
988 if (fsync(fd) != 0) {
989 err = got_error_from_errno("fsync");
994 if (rename(tmppath, ondisk_path) != 0) {
995 err = got_error_from_errno3("rename", tmppath,
1002 if (te_mode & S_IXUSR) {
1003 if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
1004 err = got_error_from_errno2("chmod", ondisk_path);
1008 if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
1009 err = got_error_from_errno2("chmod", ondisk_path);
1015 if (fd != -1 && close(fd) != 0 && err == NULL)
1016 err = got_error_from_errno("close");
1021 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1022 static const struct got_error *
1023 get_modified_file_content_status(unsigned char *status, FILE *f)
1025 const struct got_error *err = NULL;
1026 const char *markers[3] = {
1027 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1028 GOT_DIFF_CONFLICT_MARKER_SEP,
1029 GOT_DIFF_CONFLICT_MARKER_END
1034 const char delim[3] = {'\0', '\0', '\0'};
1036 while (*status == GOT_STATUS_MODIFY) {
1037 line = fparseln(f, &len, NULL, delim, 0);
1041 err = got_ferror(f, GOT_ERR_IO);
1045 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1046 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1048 *status = GOT_STATUS_CONFLICT;
1058 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1060 return !(ie->ctime_sec == sb->st_ctime &&
1061 ie->ctime_nsec == sb->st_ctimensec &&
1062 ie->mtime_sec == sb->st_mtime &&
1063 ie->mtime_nsec == sb->st_mtimensec &&
1064 ie->size == (sb->st_size & 0xffffffff));
1067 static unsigned char
1068 get_staged_status(struct got_fileindex_entry *ie)
1070 switch (got_fileindex_entry_stage_get(ie)) {
1071 case GOT_FILEIDX_STAGE_ADD:
1072 return GOT_STATUS_ADD;
1073 case GOT_FILEIDX_STAGE_DELETE:
1074 return GOT_STATUS_DELETE;
1075 case GOT_FILEIDX_STAGE_MODIFY:
1076 return GOT_STATUS_MODIFY;
1078 return GOT_STATUS_NO_CHANGE;
1082 static const struct got_error *
1083 get_file_status(unsigned char *status, struct stat *sb,
1084 struct got_fileindex_entry *ie, const char *abspath,
1085 struct got_repository *repo)
1087 const struct got_error *err = NULL;
1088 struct got_object_id id;
1092 struct got_blob_object *blob = NULL;
1094 unsigned char staged_status = get_staged_status(ie);
1096 *status = GOT_STATUS_NO_CHANGE;
1098 if (lstat(abspath, sb) == -1) {
1099 if (errno == ENOENT) {
1100 if (got_fileindex_entry_has_file_on_disk(ie))
1101 *status = GOT_STATUS_MISSING;
1103 *status = GOT_STATUS_DELETE;
1106 return got_error_from_errno2("lstat", abspath);
1109 if (!S_ISREG(sb->st_mode)) {
1110 *status = GOT_STATUS_OBSTRUCTED;
1114 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1115 *status = GOT_STATUS_DELETE;
1117 } else if (!got_fileindex_entry_has_blob(ie) &&
1118 staged_status != GOT_STATUS_ADD) {
1119 *status = GOT_STATUS_ADD;
1123 if (!stat_info_differs(ie, sb))
1126 if (staged_status == GOT_STATUS_MODIFY ||
1127 staged_status == GOT_STATUS_ADD)
1128 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1130 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1132 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1136 f = fopen(abspath, "r");
1138 err = got_error_from_errno2("fopen", abspath);
1141 hdrlen = got_object_blob_get_hdrlen(blob);
1143 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1144 err = got_object_blob_read_block(&blen, blob);
1147 /* Skip length of blob object header first time around. */
1148 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1149 if (flen == 0 && ferror(f)) {
1150 err = got_error_from_errno("fread");
1155 *status = GOT_STATUS_MODIFY;
1157 } else if (flen == 0) {
1159 *status = GOT_STATUS_MODIFY;
1161 } else if (blen - hdrlen == flen) {
1162 /* Skip blob object header first time around. */
1163 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1164 *status = GOT_STATUS_MODIFY;
1168 *status = GOT_STATUS_MODIFY;
1174 if (*status == GOT_STATUS_MODIFY) {
1176 err = get_modified_file_content_status(status, f);
1180 got_object_blob_close(blob);
1187 * Update timestamps in the file index if a file is unmodified and
1188 * we had to run a full content comparison to find out.
1190 static const struct got_error *
1191 sync_timestamps(char *ondisk_path, unsigned char status,
1192 struct got_fileindex_entry *ie, struct stat *sb)
1194 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1195 return got_fileindex_entry_update(ie, ondisk_path,
1196 ie->blob_sha1, ie->commit_sha1, 1);
1201 static const struct got_error *
1202 update_blob(struct got_worktree *worktree,
1203 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1204 struct got_tree_entry *te, const char *path,
1205 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1208 const struct got_error *err = NULL;
1209 struct got_blob_object *blob = NULL;
1211 unsigned char status = GOT_STATUS_NO_CHANGE;
1214 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1215 return got_error_from_errno("asprintf");
1218 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1219 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1222 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1225 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1226 sb.st_mode = got_fileindex_perms_to_st(ie);
1228 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1230 if (status == GOT_STATUS_OBSTRUCTED) {
1231 err = (*progress_cb)(progress_arg, status, path);
1235 if (ie && status != GOT_STATUS_MISSING) {
1236 if (got_fileindex_entry_has_commit(ie) &&
1237 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1238 SHA1_DIGEST_LENGTH) == 0) {
1239 err = sync_timestamps(ondisk_path, status, ie, &sb);
1242 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1246 if (got_fileindex_entry_has_blob(ie) &&
1247 memcmp(ie->blob_sha1, te->id->sha1,
1248 SHA1_DIGEST_LENGTH) == 0) {
1249 err = sync_timestamps(ondisk_path, status, ie, &sb);
1254 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1258 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1259 int update_timestamps;
1260 struct got_blob_object *blob2 = NULL;
1261 if (got_fileindex_entry_has_blob(ie)) {
1262 struct got_object_id id2;
1263 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1264 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1268 err = merge_blob(&update_timestamps, worktree, blob2,
1269 ondisk_path, path, sb.st_mode, blob,
1270 worktree->base_commit_id, repo,
1271 progress_cb, progress_arg);
1273 got_object_blob_close(blob2);
1275 * Do not update timestamps of files with local changes.
1276 * Otherwise, a future status walk would treat them as
1277 * unmodified files again.
1279 err = got_fileindex_entry_update(ie, ondisk_path,
1280 blob->id.sha1, worktree->base_commit_id->sha1,
1282 } else if (status == GOT_STATUS_DELETE) {
1283 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1286 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1287 ondisk_path, path, blob, 0);
1291 err = install_blob(worktree, ondisk_path, path, te->mode,
1292 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1293 repo, progress_cb, progress_arg);
1296 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1297 ondisk_path, path, blob, 1);
1301 got_object_blob_close(blob);
1307 static const struct got_error *
1308 remove_ondisk_file(const char *root_path, const char *path)
1310 const struct got_error *err = NULL;
1311 char *ondisk_path = NULL;
1313 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1314 return got_error_from_errno("asprintf");
1316 if (unlink(ondisk_path) == -1) {
1317 if (errno != ENOENT)
1318 err = got_error_from_errno2("unlink", ondisk_path);
1320 char *parent = dirname(ondisk_path);
1321 while (parent && strcmp(parent, root_path) != 0) {
1322 if (rmdir(parent) == -1) {
1323 if (errno != ENOTEMPTY)
1324 err = got_error_from_errno2("rmdir",
1328 parent = dirname(parent);
1335 static const struct got_error *
1336 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1337 struct got_fileindex_entry *ie, struct got_repository *repo,
1338 got_worktree_checkout_cb progress_cb, void *progress_arg)
1340 const struct got_error *err = NULL;
1341 unsigned char status;
1345 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1346 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1348 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1350 return got_error_from_errno("asprintf");
1352 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1356 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1357 status == GOT_STATUS_ADD) {
1358 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1362 * Preserve the working file and change the deleted blob's
1363 * entry into a schedule-add entry.
1365 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1370 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1373 if (status == GOT_STATUS_NO_CHANGE) {
1374 err = remove_ondisk_file(worktree->root_path, ie->path);
1378 got_fileindex_entry_remove(fileindex, ie);
1384 struct diff_cb_arg {
1385 struct got_fileindex *fileindex;
1386 struct got_worktree *worktree;
1387 struct got_repository *repo;
1388 got_worktree_checkout_cb progress_cb;
1390 got_cancel_cb cancel_cb;
1394 static const struct got_error *
1395 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1396 struct got_tree_entry *te, const char *parent_path)
1398 struct diff_cb_arg *a = arg;
1400 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1401 return got_error(GOT_ERR_CANCELLED);
1403 return update_blob(a->worktree, a->fileindex, ie, te,
1404 ie->path, a->repo, a->progress_cb, a->progress_arg);
1407 static const struct got_error *
1408 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1410 struct diff_cb_arg *a = arg;
1412 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1413 return got_error(GOT_ERR_CANCELLED);
1415 return delete_blob(a->worktree, a->fileindex, ie,
1416 a->repo, a->progress_cb, a->progress_arg);
1419 static const struct got_error *
1420 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1422 struct diff_cb_arg *a = arg;
1423 const struct got_error *err;
1426 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1427 return got_error(GOT_ERR_CANCELLED);
1429 if (got_object_tree_entry_is_submodule(te))
1432 if (asprintf(&path, "%s%s%s", parent_path,
1433 parent_path[0] ? "/" : "", te->name)
1435 return got_error_from_errno("asprintf");
1437 if (S_ISDIR(te->mode))
1438 err = add_dir_on_disk(a->worktree, path);
1440 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1441 a->repo, a->progress_cb, a->progress_arg);
1447 static const struct got_error *
1448 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1450 const struct got_error *err = NULL;
1451 char *uuidstr = NULL;
1452 uint32_t uuid_status;
1456 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1457 if (uuid_status != uuid_s_ok)
1458 return got_error_uuid(uuid_status, "uuid_to_string");
1460 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1462 err = got_error_from_errno("asprintf");
1469 const struct got_error *
1470 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1472 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1475 static const struct got_error *
1476 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1478 return get_ref_name(refname, worktree,
1479 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1482 static const struct got_error *
1483 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1485 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1488 static const struct got_error *
1489 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1491 return get_ref_name(refname, worktree,
1492 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1495 static const struct got_error *
1496 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1498 return get_ref_name(refname, worktree,
1499 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1502 static const struct got_error *
1503 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1505 return get_ref_name(refname, worktree,
1506 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1509 static const struct got_error *
1510 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1512 return get_ref_name(refname, worktree,
1513 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1516 static const struct got_error *
1517 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1519 return get_ref_name(refname, worktree,
1520 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1523 static const struct got_error *
1524 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1526 return get_ref_name(refname, worktree,
1527 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1530 const struct got_error *
1531 got_worktree_get_histedit_script_path(char **path,
1532 struct got_worktree *worktree)
1534 if (asprintf(path, "%s/%s/%s", worktree->root_path,
1535 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
1537 return got_error_from_errno("asprintf");
1543 * Prevent Git's garbage collector from deleting our base commit by
1544 * setting a reference to our base commit's ID.
1546 static const struct got_error *
1547 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1549 const struct got_error *err = NULL;
1550 struct got_reference *ref = NULL;
1553 err = got_worktree_get_base_ref_name(&refname, worktree);
1557 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1561 err = got_ref_write(ref, repo);
1569 static const struct got_error *
1570 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
1572 const struct got_error *err = NULL;
1574 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1575 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1576 err = got_error_from_errno("asprintf");
1577 *fileindex_path = NULL;
1583 static const struct got_error *
1584 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1585 struct got_worktree *worktree)
1587 const struct got_error *err = NULL;
1590 *fileindex_path = NULL;
1591 *fileindex = got_fileindex_alloc();
1592 if (*fileindex == NULL)
1593 return got_error_from_errno("got_fileindex_alloc");
1595 err = get_fileindex_path(fileindex_path, worktree);
1599 index = fopen(*fileindex_path, "rb");
1600 if (index == NULL) {
1601 if (errno != ENOENT)
1602 err = got_error_from_errno2("fopen", *fileindex_path);
1604 err = got_fileindex_read(*fileindex, index);
1605 if (fclose(index) != 0 && err == NULL)
1606 err = got_error_from_errno("fclose");
1610 free(*fileindex_path);
1611 *fileindex_path = NULL;
1612 got_fileindex_free(*fileindex);
1618 struct bump_base_commit_id_arg {
1619 struct got_object_id *base_commit_id;
1622 const char *entry_name;
1623 got_worktree_checkout_cb progress_cb;
1627 /* Bump base commit ID of all files within an updated part of the work tree. */
1628 static const struct got_error *
1629 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1631 const struct got_error *err;
1632 struct bump_base_commit_id_arg *a = arg;
1634 if (a->entry_name) {
1635 if (strcmp(ie->path, a->path) != 0)
1637 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1640 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1641 SHA1_DIGEST_LENGTH) == 0)
1644 if (a->progress_cb) {
1645 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1650 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1654 static const struct got_error *
1655 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1657 const struct got_error *err = NULL;
1658 char *new_fileindex_path = NULL;
1659 FILE *new_index = NULL;
1661 err = got_opentemp_named(&new_fileindex_path, &new_index,
1666 err = got_fileindex_write(fileindex, new_index);
1670 if (rename(new_fileindex_path, fileindex_path) != 0) {
1671 err = got_error_from_errno3("rename", new_fileindex_path,
1673 unlink(new_fileindex_path);
1678 free(new_fileindex_path);
1682 static const struct got_error *
1683 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1684 struct got_object_id **tree_id, const char *wt_relpath,
1685 struct got_worktree *worktree, struct got_repository *repo)
1687 const struct got_error *err = NULL;
1688 struct got_object_id *id = NULL;
1689 char *in_repo_path = NULL;
1690 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1692 *entry_type = GOT_OBJ_TYPE_ANY;
1693 *tree_relpath = NULL;
1696 if (wt_relpath[0] == '\0') {
1697 /* Check out all files within the work tree. */
1698 *entry_type = GOT_OBJ_TYPE_TREE;
1699 *tree_relpath = strdup("");
1700 if (*tree_relpath == NULL) {
1701 err = got_error_from_errno("strdup");
1704 err = got_object_id_by_path(tree_id, repo,
1705 worktree->base_commit_id, worktree->path_prefix);
1711 /* Check out a subset of files in the work tree. */
1713 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1714 is_root_wt ? "" : "/", wt_relpath) == -1) {
1715 err = got_error_from_errno("asprintf");
1719 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
1725 in_repo_path = NULL;
1727 err = got_object_get_type(entry_type, repo, id);
1731 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
1732 /* Check out a single file. */
1733 if (strchr(wt_relpath, '/') == NULL) {
1734 /* Check out a single file in work tree's root dir. */
1735 in_repo_path = strdup(worktree->path_prefix);
1736 if (in_repo_path == NULL) {
1737 err = got_error_from_errno("strdup");
1740 *tree_relpath = strdup("");
1741 if (*tree_relpath == NULL) {
1742 err = got_error_from_errno("strdup");
1746 /* Check out a single file in a subdirectory. */
1747 err = got_path_dirname(tree_relpath, wt_relpath);
1750 if (asprintf(&in_repo_path, "%s%s%s",
1751 worktree->path_prefix, is_root_wt ? "" : "/",
1752 *tree_relpath) == -1) {
1753 err = got_error_from_errno("asprintf");
1757 err = got_object_id_by_path(tree_id, repo,
1758 worktree->base_commit_id, in_repo_path);
1760 /* Check out all files within a subdirectory. */
1761 *tree_id = got_object_id_dup(id);
1762 if (*tree_id == NULL) {
1763 err = got_error_from_errno("got_object_id_dup");
1766 *tree_relpath = strdup(wt_relpath);
1767 if (*tree_relpath == NULL) {
1768 err = got_error_from_errno("strdup");
1776 *entry_type = GOT_OBJ_TYPE_ANY;
1777 free(*tree_relpath);
1778 *tree_relpath = NULL;
1785 static const struct got_error *
1786 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1787 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
1788 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1789 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
1791 const struct got_error *err = NULL;
1792 struct got_commit_object *commit = NULL;
1793 struct got_tree_object *tree = NULL;
1794 struct got_fileindex_diff_tree_cb diff_cb;
1795 struct diff_cb_arg arg;
1797 err = ref_base_commit(worktree, repo);
1801 err = got_object_open_as_commit(&commit, repo,
1802 worktree->base_commit_id);
1806 err = got_object_open_as_tree(&tree, repo, tree_id);
1811 got_object_tree_find_entry(tree, entry_name) == NULL) {
1812 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1816 diff_cb.diff_old_new = diff_old_new;
1817 diff_cb.diff_old = diff_old;
1818 diff_cb.diff_new = diff_new;
1819 arg.fileindex = fileindex;
1820 arg.worktree = worktree;
1822 arg.progress_cb = progress_cb;
1823 arg.progress_arg = progress_arg;
1824 arg.cancel_cb = cancel_cb;
1825 arg.cancel_arg = cancel_arg;
1826 err = got_fileindex_diff_tree(fileindex, tree, relpath,
1827 entry_name, repo, &diff_cb, &arg);
1830 got_object_tree_close(tree);
1832 got_object_commit_close(commit);
1836 const struct got_error *
1837 got_worktree_checkout_files(struct got_worktree *worktree,
1838 struct got_pathlist_head *paths, struct got_repository *repo,
1839 got_worktree_checkout_cb progress_cb, void *progress_arg,
1840 got_cancel_cb cancel_cb, void *cancel_arg)
1842 const struct got_error *err = NULL, *sync_err, *unlockerr;
1843 struct got_commit_object *commit = NULL;
1844 struct got_tree_object *tree = NULL;
1845 struct got_fileindex *fileindex = NULL;
1846 char *fileindex_path = NULL;
1847 struct got_pathlist_entry *pe;
1848 struct tree_path_data {
1849 SIMPLEQ_ENTRY(tree_path_data) entry;
1850 struct got_object_id *tree_id;
1855 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
1857 SIMPLEQ_INIT(&tree_paths);
1859 err = lock_worktree(worktree, LOCK_EX);
1863 /* Map all specified paths to in-repository trees. */
1864 TAILQ_FOREACH(pe, paths, entry) {
1865 tpd = malloc(sizeof(*tpd));
1867 err = got_error_from_errno("malloc");
1871 err = find_tree_entry_for_checkout(&tpd->entry_type,
1872 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
1878 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
1879 err = got_path_basename(&tpd->entry_name, pe->path);
1887 tpd->entry_name = NULL;
1889 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
1893 * Read the file index.
1894 * Checking out files is supposed to be an idempotent operation.
1895 * If the on-disk file index is incomplete we will try to complete it.
1897 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1901 tpd = SIMPLEQ_FIRST(&tree_paths);
1902 TAILQ_FOREACH(pe, paths, entry) {
1903 struct bump_base_commit_id_arg bbc_arg;
1905 err = checkout_files(worktree, fileindex, tpd->relpath,
1906 tpd->tree_id, tpd->entry_name, repo,
1907 progress_cb, progress_arg, cancel_cb, cancel_arg);
1911 bbc_arg.base_commit_id = worktree->base_commit_id;
1912 bbc_arg.entry_name = tpd->entry_name;
1913 bbc_arg.path = pe->path;
1914 bbc_arg.path_len = pe->path_len;
1915 bbc_arg.progress_cb = progress_cb;
1916 bbc_arg.progress_arg = progress_arg;
1917 err = got_fileindex_for_each_entry_safe(fileindex,
1918 bump_base_commit_id, &bbc_arg);
1922 tpd = SIMPLEQ_NEXT(tpd, entry);
1924 sync_err = sync_fileindex(fileindex, fileindex_path);
1925 if (sync_err && err == NULL)
1928 free(fileindex_path);
1930 got_object_tree_close(tree);
1932 got_object_commit_close(commit);
1934 got_fileindex_free(fileindex);
1935 while (!SIMPLEQ_EMPTY(&tree_paths)) {
1936 tpd = SIMPLEQ_FIRST(&tree_paths);
1937 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
1942 unlockerr = lock_worktree(worktree, LOCK_SH);
1943 if (unlockerr && err == NULL)
1948 struct merge_file_cb_arg {
1949 struct got_worktree *worktree;
1950 struct got_fileindex *fileindex;
1951 got_worktree_checkout_cb progress_cb;
1953 got_cancel_cb cancel_cb;
1955 struct got_object_id *commit_id2;
1958 static const struct got_error *
1959 merge_file_cb(void *arg, struct got_blob_object *blob1,
1960 struct got_blob_object *blob2, struct got_object_id *id1,
1961 struct got_object_id *id2, const char *path1, const char *path2,
1962 struct got_repository *repo)
1964 static const struct got_error *err = NULL;
1965 struct merge_file_cb_arg *a = arg;
1966 struct got_fileindex_entry *ie;
1967 char *ondisk_path = NULL;
1969 unsigned char status;
1970 int local_changes_subsumed;
1972 if (blob1 && blob2) {
1973 ie = got_fileindex_entry_get(a->fileindex, path2,
1976 return (*a->progress_cb)(a->progress_arg,
1977 GOT_STATUS_MISSING, path2);
1979 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1981 return got_error_from_errno("asprintf");
1983 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1987 if (status == GOT_STATUS_DELETE) {
1988 err = (*a->progress_cb)(a->progress_arg,
1989 GOT_STATUS_MERGE, path2);
1992 if (status != GOT_STATUS_NO_CHANGE &&
1993 status != GOT_STATUS_MODIFY &&
1994 status != GOT_STATUS_CONFLICT &&
1995 status != GOT_STATUS_ADD) {
1996 err = (*a->progress_cb)(a->progress_arg, status, path2);
2000 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
2001 ondisk_path, path2, sb.st_mode, blob2, a->commit_id2, repo,
2002 a->progress_cb, a->progress_arg);
2004 ie = got_fileindex_entry_get(a->fileindex, path1,
2007 return (*a->progress_cb)(a->progress_arg,
2008 GOT_STATUS_MISSING, path2);
2010 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2012 return got_error_from_errno("asprintf");
2014 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2019 case GOT_STATUS_NO_CHANGE:
2020 err = (*a->progress_cb)(a->progress_arg,
2021 GOT_STATUS_DELETE, path1);
2024 err = remove_ondisk_file(a->worktree->root_path, path1);
2028 got_fileindex_entry_mark_deleted_from_disk(ie);
2030 case GOT_STATUS_DELETE:
2031 case GOT_STATUS_MISSING:
2032 err = (*a->progress_cb)(a->progress_arg,
2033 GOT_STATUS_DELETE, path1);
2037 got_fileindex_entry_mark_deleted_from_disk(ie);
2039 case GOT_STATUS_ADD:
2040 case GOT_STATUS_MODIFY:
2041 case GOT_STATUS_CONFLICT:
2042 err = (*a->progress_cb)(a->progress_arg,
2043 GOT_STATUS_CANNOT_DELETE, path1);
2047 case GOT_STATUS_OBSTRUCTED:
2048 err = (*a->progress_cb)(a->progress_arg, status, path1);
2056 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2058 return got_error_from_errno("asprintf");
2059 ie = got_fileindex_entry_get(a->fileindex, path2,
2062 err = get_file_status(&status, &sb, ie, ondisk_path,
2066 if (status != GOT_STATUS_NO_CHANGE &&
2067 status != GOT_STATUS_MODIFY &&
2068 status != GOT_STATUS_CONFLICT &&
2069 status != GOT_STATUS_ADD) {
2070 err = (*a->progress_cb)(a->progress_arg,
2074 err = merge_blob(&local_changes_subsumed, a->worktree,
2075 NULL, ondisk_path, path2, sb.st_mode, blob2,
2076 a->commit_id2, repo,
2077 a->progress_cb, a->progress_arg);
2078 if (status == GOT_STATUS_DELETE) {
2079 err = update_blob_fileindex_entry(a->worktree,
2080 a->fileindex, ie, ondisk_path, ie->path,
2086 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2087 err = install_blob(a->worktree, ondisk_path, path2,
2088 /* XXX get this from parent tree! */
2089 GOT_DEFAULT_FILE_MODE,
2090 sb.st_mode, blob2, 0, 0, repo,
2091 a->progress_cb, a->progress_arg);
2094 err = got_fileindex_entry_alloc(&ie,
2095 ondisk_path, path2, NULL, NULL);
2098 err = got_fileindex_entry_add(a->fileindex, ie);
2100 got_fileindex_entry_free(ie);
2110 struct check_merge_ok_arg {
2111 struct got_worktree *worktree;
2112 struct got_repository *repo;
2115 static const struct got_error *
2116 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2118 const struct got_error *err = NULL;
2119 struct check_merge_ok_arg *a = arg;
2120 unsigned char status;
2124 /* Reject merges into a work tree with mixed base commits. */
2125 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2126 SHA1_DIGEST_LENGTH))
2127 return got_error(GOT_ERR_MIXED_COMMITS);
2129 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2131 return got_error_from_errno("asprintf");
2133 /* Reject merges into a work tree with conflicted files. */
2134 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
2137 if (status == GOT_STATUS_CONFLICT)
2138 return got_error(GOT_ERR_CONFLICTS);
2143 static const struct got_error *
2144 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2145 const char *fileindex_path, struct got_object_id *commit_id1,
2146 struct got_object_id *commit_id2, struct got_repository *repo,
2147 got_worktree_checkout_cb progress_cb, void *progress_arg,
2148 got_cancel_cb cancel_cb, void *cancel_arg)
2150 const struct got_error *err = NULL, *sync_err;
2151 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2152 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2153 struct merge_file_cb_arg arg;
2156 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2157 worktree->path_prefix);
2161 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2166 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2167 worktree->path_prefix);
2171 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2175 arg.worktree = worktree;
2176 arg.fileindex = fileindex;
2177 arg.progress_cb = progress_cb;
2178 arg.progress_arg = progress_arg;
2179 arg.cancel_cb = cancel_cb;
2180 arg.cancel_arg = cancel_arg;
2181 arg.commit_id2 = commit_id2;
2182 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2183 sync_err = sync_fileindex(fileindex, fileindex_path);
2184 if (sync_err && err == NULL)
2188 got_object_tree_close(tree1);
2190 got_object_tree_close(tree2);
2194 const struct got_error *
2195 got_worktree_merge_files(struct got_worktree *worktree,
2196 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2197 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2198 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2200 const struct got_error *err, *unlockerr;
2201 char *fileindex_path = NULL;
2202 struct got_fileindex *fileindex = NULL;
2203 struct check_merge_ok_arg mok_arg;
2205 err = lock_worktree(worktree, LOCK_EX);
2209 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2213 mok_arg.worktree = worktree;
2214 mok_arg.repo = repo;
2215 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2220 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2221 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2224 got_fileindex_free(fileindex);
2225 free(fileindex_path);
2226 unlockerr = lock_worktree(worktree, LOCK_SH);
2227 if (unlockerr && err == NULL)
2232 struct diff_dir_cb_arg {
2233 struct got_fileindex *fileindex;
2234 struct got_worktree *worktree;
2235 const char *status_path;
2236 size_t status_path_len;
2237 struct got_repository *repo;
2238 got_worktree_status_cb status_cb;
2240 got_cancel_cb cancel_cb;
2242 /* A pathlist containing per-directory pathlists of ignore patterns. */
2243 struct got_pathlist_head ignores;
2246 static const struct got_error *
2247 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2248 got_worktree_status_cb status_cb, void *status_arg,
2249 struct got_repository *repo)
2251 const struct got_error *err = NULL;
2252 unsigned char status = GOT_STATUS_NO_CHANGE;
2253 unsigned char staged_status = get_staged_status(ie);
2255 struct got_object_id blob_id, commit_id, staged_blob_id;
2256 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2257 struct got_object_id *staged_blob_idp = NULL;
2259 err = get_file_status(&status, &sb, ie, abspath, repo);
2263 if (status == GOT_STATUS_NO_CHANGE &&
2264 staged_status == GOT_STATUS_NO_CHANGE)
2267 if (got_fileindex_entry_has_blob(ie)) {
2268 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2269 blob_idp = &blob_id;
2271 if (got_fileindex_entry_has_commit(ie)) {
2272 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2273 commit_idp = &commit_id;
2275 if (staged_status == GOT_STATUS_ADD ||
2276 staged_status == GOT_STATUS_MODIFY) {
2277 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2278 SHA1_DIGEST_LENGTH);
2279 staged_blob_idp = &staged_blob_id;
2282 return (*status_cb)(status_arg, status, staged_status,
2283 ie->path, blob_idp, staged_blob_idp, commit_idp);
2286 static const struct got_error *
2287 status_old_new(void *arg, struct got_fileindex_entry *ie,
2288 struct dirent *de, const char *parent_path)
2290 const struct got_error *err = NULL;
2291 struct diff_dir_cb_arg *a = arg;
2294 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2295 return got_error(GOT_ERR_CANCELLED);
2297 if (got_path_cmp(parent_path, a->status_path,
2298 strlen(parent_path), a->status_path_len) != 0 &&
2299 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2302 if (parent_path[0]) {
2303 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2304 parent_path, de->d_name) == -1)
2305 return got_error_from_errno("asprintf");
2307 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2309 return got_error_from_errno("asprintf");
2312 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
2318 static const struct got_error *
2319 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2321 struct diff_dir_cb_arg *a = arg;
2322 struct got_object_id blob_id, commit_id;
2323 unsigned char status;
2325 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2326 return got_error(GOT_ERR_CANCELLED);
2328 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2331 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2332 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2333 if (got_fileindex_entry_has_file_on_disk(ie))
2334 status = GOT_STATUS_MISSING;
2336 status = GOT_STATUS_DELETE;
2337 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2338 ie->path, &blob_id, NULL, &commit_id);
2342 free_ignorelist(struct got_pathlist_head *ignorelist)
2344 struct got_pathlist_entry *pe;
2346 TAILQ_FOREACH(pe, ignorelist, entry)
2347 free((char *)pe->path);
2348 got_pathlist_free(ignorelist);
2352 free_ignores(struct got_pathlist_head *ignores)
2354 struct got_pathlist_entry *pe;
2356 TAILQ_FOREACH(pe, ignores, entry) {
2357 struct got_pathlist_head *ignorelist = pe->data;
2358 free_ignorelist(ignorelist);
2359 free((char *)pe->path);
2361 got_pathlist_free(ignores);
2364 static const struct got_error *
2365 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
2367 const struct got_error *err = NULL;
2368 struct got_pathlist_entry *pe = NULL;
2369 struct got_pathlist_head *ignorelist;
2370 char *line = NULL, *pattern, *dirpath = NULL;
2371 size_t linesize = 0;
2374 ignorelist = calloc(1, sizeof(*ignorelist));
2375 if (ignorelist == NULL)
2376 return got_error_from_errno("calloc");
2377 TAILQ_INIT(ignorelist);
2379 while ((linelen = getline(&line, &linesize, f)) != -1) {
2380 if (linelen > 0 && line[linelen - 1] == '\n')
2381 line[linelen - 1] = '\0';
2382 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
2384 err = got_error_from_errno("asprintf");
2387 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
2392 err = got_error_from_errno("getline");
2396 dirpath = strdup(path);
2397 if (dirpath == NULL) {
2398 err = got_error_from_errno("strdup");
2401 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
2404 if (err || pe == NULL) {
2406 free_ignorelist(ignorelist);
2412 match_ignores(struct got_pathlist_head *ignores, const char *path)
2414 struct got_pathlist_entry *pe;
2417 * The ignores pathlist contains ignore lists from children before
2418 * parents, so we can find the most specific ignorelist by walking
2419 * ignores backwards.
2421 pe = TAILQ_LAST(ignores, got_pathlist_head);
2423 if (got_path_is_child(path, pe->path, pe->path_len)) {
2424 struct got_pathlist_head *ignorelist = pe->data;
2425 struct got_pathlist_entry *pi;
2426 TAILQ_FOREACH(pi, ignorelist, entry) {
2427 if (fnmatch(pi->path, path,
2428 FNM_PATHNAME | FNM_LEADING_DIR))
2433 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
2439 static const struct got_error *
2440 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
2443 const struct got_error *err = NULL;
2445 FILE *ignoresfile = NULL;
2447 /* TODO: read .gitignores as well... */
2448 if (asprintf(&ignorespath, "%s/%s%s.cvsignore", root_path, path,
2449 path[0] ? "/" : "") == -1)
2450 return got_error_from_errno("asprintf");
2452 ignoresfile = fopen(ignorespath, "r");
2453 if (ignoresfile == NULL) {
2454 if (errno != ENOENT && errno != EACCES)
2455 err = got_error_from_errno2("fopen",
2458 err = read_ignores(ignores, path, ignoresfile);
2460 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
2461 err = got_error_from_errno2("flose", path);
2466 static const struct got_error *
2467 status_new(void *arg, struct dirent *de, const char *parent_path)
2469 const struct got_error *err = NULL;
2470 struct diff_dir_cb_arg *a = arg;
2473 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2474 return got_error(GOT_ERR_CANCELLED);
2476 /* XXX ignore symlinks for now */
2477 if (de->d_type == DT_LNK)
2480 if (parent_path[0]) {
2481 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2482 return got_error_from_errno("asprintf");
2487 if (de->d_type == DT_DIR)
2488 err = add_ignores(&a->ignores, a->worktree->root_path, path);
2489 else if (got_path_is_child(path, a->status_path, a->status_path_len)
2490 && !match_ignores(&a->ignores, path))
2491 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2492 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2498 static const struct got_error *
2499 report_single_file_status(const char *path, const char *ondisk_path,
2500 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2501 void *status_arg, struct got_repository *repo)
2503 struct got_fileindex_entry *ie;
2506 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
2508 return report_file_status(ie, ondisk_path, status_cb,
2511 if (lstat(ondisk_path, &sb) == -1) {
2512 if (errno != ENOENT)
2513 return got_error_from_errno2("lstat", ondisk_path);
2514 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
2515 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2519 if (S_ISREG(sb.st_mode))
2520 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
2521 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2526 static const struct got_error *
2527 worktree_status(struct got_worktree *worktree, const char *path,
2528 struct got_fileindex *fileindex, struct got_repository *repo,
2529 got_worktree_status_cb status_cb, void *status_arg,
2530 got_cancel_cb cancel_cb, void *cancel_arg)
2532 const struct got_error *err = NULL;
2533 DIR *workdir = NULL;
2534 struct got_fileindex_diff_dir_cb fdiff_cb;
2535 struct diff_dir_cb_arg arg;
2536 char *ondisk_path = NULL;
2538 if (asprintf(&ondisk_path, "%s%s%s",
2539 worktree->root_path, path[0] ? "/" : "", path) == -1)
2540 return got_error_from_errno("asprintf");
2542 workdir = opendir(ondisk_path);
2543 if (workdir == NULL) {
2544 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES)
2545 err = got_error_from_errno2("opendir", ondisk_path);
2547 err = report_single_file_status(path, ondisk_path,
2548 fileindex, status_cb, status_arg, repo);
2550 fdiff_cb.diff_old_new = status_old_new;
2551 fdiff_cb.diff_old = status_old;
2552 fdiff_cb.diff_new = status_new;
2553 arg.fileindex = fileindex;
2554 arg.worktree = worktree;
2555 arg.status_path = path;
2556 arg.status_path_len = strlen(path);
2558 arg.status_cb = status_cb;
2559 arg.status_arg = status_arg;
2560 arg.cancel_cb = cancel_cb;
2561 arg.cancel_arg = cancel_arg;
2562 TAILQ_INIT(&arg.ignores);
2563 err = add_ignores(&arg.ignores, worktree->root_path, path);
2565 err = got_fileindex_diff_dir(fileindex, workdir,
2566 worktree->root_path, path, repo, &fdiff_cb, &arg);
2567 free_ignores(&arg.ignores);
2576 const struct got_error *
2577 got_worktree_status(struct got_worktree *worktree,
2578 struct got_pathlist_head *paths, struct got_repository *repo,
2579 got_worktree_status_cb status_cb, void *status_arg,
2580 got_cancel_cb cancel_cb, void *cancel_arg)
2582 const struct got_error *err = NULL;
2583 char *fileindex_path = NULL;
2584 struct got_fileindex *fileindex = NULL;
2585 struct got_pathlist_entry *pe;
2587 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2591 TAILQ_FOREACH(pe, paths, entry) {
2592 err = worktree_status(worktree, pe->path, fileindex, repo,
2593 status_cb, status_arg, cancel_cb, cancel_arg);
2597 free(fileindex_path);
2598 got_fileindex_free(fileindex);
2602 const struct got_error *
2603 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2606 const struct got_error *err = NULL;
2607 char *resolved, *cwd = NULL, *path = NULL;
2612 resolved = realpath(arg, NULL);
2613 if (resolved == NULL) {
2614 if (errno != ENOENT)
2615 return got_error_from_errno2("realpath", arg);
2616 cwd = getcwd(NULL, 0);
2618 return got_error_from_errno("getcwd");
2619 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
2620 err = got_error_from_errno("asprintf");
2625 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2626 strlen(got_worktree_get_root_path(worktree)))) {
2627 err = got_error(GOT_ERR_BAD_PATH);
2631 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2632 err = got_path_skip_common_ancestor(&path,
2633 got_worktree_get_root_path(worktree), resolved);
2639 err = got_error_from_errno("strdup");
2644 /* XXX status walk can't deal with trailing slash! */
2646 while (len > 0 && path[len - 1] == '/') {
2647 path[len - 1] = '\0';
2660 static const struct got_error *
2661 schedule_addition(const char *ondisk_path, struct got_fileindex *fileindex,
2662 const char *relpath, got_worktree_status_cb status_cb, void *status_arg,
2663 struct got_repository *repo)
2665 const struct got_error *err = NULL;
2666 struct got_fileindex_entry *ie;
2667 unsigned char status;
2670 ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
2672 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2675 /* Re-adding an existing entry is a no-op. */
2676 if (status == GOT_STATUS_ADD)
2678 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
2681 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2685 err = got_fileindex_entry_add(fileindex, ie);
2687 got_fileindex_entry_free(ie);
2691 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2694 const struct got_error *
2695 got_worktree_schedule_add(struct got_worktree *worktree,
2696 struct got_pathlist_head *paths,
2697 got_worktree_status_cb status_cb, void *status_arg,
2698 struct got_repository *repo)
2700 struct got_fileindex *fileindex = NULL;
2701 char *fileindex_path = NULL;
2702 const struct got_error *err = NULL, *sync_err, *unlockerr;
2703 struct got_pathlist_entry *pe;
2705 err = lock_worktree(worktree, LOCK_EX);
2709 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2713 TAILQ_FOREACH(pe, paths, entry) {
2715 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
2717 return got_error_from_errno("asprintf");
2718 err = schedule_addition(ondisk_path, fileindex, pe->path,
2719 status_cb, status_arg, repo);
2724 sync_err = sync_fileindex(fileindex, fileindex_path);
2725 if (sync_err && err == NULL)
2728 free(fileindex_path);
2730 got_fileindex_free(fileindex);
2731 unlockerr = lock_worktree(worktree, LOCK_SH);
2732 if (unlockerr && err == NULL)
2737 static const struct got_error *
2738 schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
2739 const char *relpath, int delete_local_mods,
2740 got_worktree_status_cb status_cb, void *status_arg,
2741 struct got_repository *repo)
2743 const struct got_error *err = NULL;
2744 struct got_fileindex_entry *ie = NULL;
2745 unsigned char status, staged_status;
2748 ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
2750 return got_error(GOT_ERR_BAD_PATH);
2752 staged_status = get_staged_status(ie);
2753 if (staged_status != GOT_STATUS_NO_CHANGE) {
2754 if (staged_status == GOT_STATUS_DELETE)
2756 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
2759 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2763 if (status != GOT_STATUS_NO_CHANGE) {
2764 if (status == GOT_STATUS_DELETE)
2766 if (status == GOT_STATUS_MODIFY && !delete_local_mods)
2767 return got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
2768 if (status != GOT_STATUS_MODIFY &&
2769 status != GOT_STATUS_MISSING)
2770 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
2773 if (status != GOT_STATUS_MISSING && unlink(ondisk_path) != 0)
2774 return got_error_from_errno2("unlink", ondisk_path);
2776 got_fileindex_entry_mark_deleted_from_disk(ie);
2777 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2780 const struct got_error *
2781 got_worktree_schedule_delete(struct got_worktree *worktree,
2782 struct got_pathlist_head *paths, int delete_local_mods,
2783 got_worktree_status_cb status_cb, void *status_arg,
2784 struct got_repository *repo)
2786 struct got_fileindex *fileindex = NULL;
2787 char *fileindex_path = NULL;
2788 const struct got_error *err = NULL, *sync_err, *unlockerr;
2789 struct got_pathlist_entry *pe;
2791 err = lock_worktree(worktree, LOCK_EX);
2795 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2799 TAILQ_FOREACH(pe, paths, entry) {
2801 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
2803 return got_error_from_errno("asprintf");
2804 err = schedule_for_deletion(ondisk_path, fileindex, pe->path,
2805 delete_local_mods, status_cb, status_arg, repo);
2810 sync_err = sync_fileindex(fileindex, fileindex_path);
2811 if (sync_err && err == NULL)
2814 free(fileindex_path);
2816 got_fileindex_free(fileindex);
2817 unlockerr = lock_worktree(worktree, LOCK_SH);
2818 if (unlockerr && err == NULL)
2823 static const struct got_error *
2824 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
2826 const struct got_error *err = NULL;
2828 size_t linesize = 0, n;
2831 linelen = getline(&line, &linesize, infile);
2832 if (linelen == -1) {
2833 if (ferror(infile)) {
2834 err = got_error_from_errno("getline");
2840 n = fwrite(line, 1, linelen, outfile);
2842 err = got_ferror(outfile, GOT_ERR_IO);
2847 n = fwrite(line, 1, linelen, rejectfile);
2849 err = got_ferror(outfile, GOT_ERR_IO);
2856 static const struct got_error *
2857 skip_one_line(FILE *f)
2860 size_t linesize = 0;
2863 linelen = getline(&line, &linesize, f);
2865 if (linelen == -1 && ferror(f))
2866 return got_error_from_errno("getline");
2870 static const struct got_error *
2871 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
2872 int start_old, int end_old, int start_new, int end_new,
2873 FILE *outfile, FILE *rejectfile)
2875 const struct got_error *err;
2877 /* Copy old file's lines leading up to patch. */
2878 while (!feof(f1) && *line_cur1 < start_old) {
2879 err = copy_one_line(f1, outfile, NULL);
2884 /* Skip new file's lines leading up to patch. */
2885 while (!feof(f2) && *line_cur2 < start_new) {
2887 err = copy_one_line(f2, NULL, rejectfile);
2889 err = skip_one_line(f2);
2894 /* Copy patched lines. */
2895 while (!feof(f2) && *line_cur2 <= end_new) {
2896 err = copy_one_line(f2, outfile, NULL);
2901 /* Skip over old file's replaced lines. */
2902 while (!feof(f1) && *line_cur1 <= end_old) {
2904 err = copy_one_line(f1, NULL, rejectfile);
2906 err = skip_one_line(f1);
2915 static const struct got_error *
2916 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
2917 FILE *outfile, FILE *rejectfile)
2919 const struct got_error *err;
2922 /* Copy old file's lines until EOF. */
2924 err = copy_one_line(f1, outfile, NULL);
2931 /* Copy new file's lines until EOF. */
2933 err = copy_one_line(f2, NULL, rejectfile);
2943 static const struct got_error *
2944 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
2945 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
2946 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
2947 int *line_cur2, FILE *outfile, FILE *rejectfile,
2948 got_worktree_patch_cb patch_cb, void *patch_arg)
2950 const struct got_error *err = NULL;
2951 int start_old = change->cv.a;
2952 int end_old = change->cv.b;
2953 int start_new = change->cv.c;
2954 int end_new = change->cv.d;
2958 *choice = GOT_PATCH_CHOICE_NONE;
2960 hunkfile = got_opentemp();
2961 if (hunkfile == NULL)
2962 return got_error_from_errno("got_opentemp");
2967 /* XXX TODO needs error checking */
2968 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
2970 if (fseek(f1, pos1, SEEK_SET) == -1) {
2971 err = got_ferror(f1, GOT_ERR_IO);
2974 if (fseek(f2, pos2, SEEK_SET) == -1) {
2975 err = got_ferror(f1, GOT_ERR_IO);
2978 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
2979 err = got_ferror(hunkfile, GOT_ERR_IO);
2983 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
2984 hunkfile, n, nchanges);
2989 case GOT_PATCH_CHOICE_YES:
2990 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
2991 end_old, start_new, end_new, outfile, rejectfile);
2993 case GOT_PATCH_CHOICE_NO:
2994 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
2995 end_old, start_new, end_new, rejectfile, outfile);
2997 case GOT_PATCH_CHOICE_QUIT:
3000 err = got_error(GOT_ERR_PATCH_CHOICE);
3004 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3005 err = got_error_from_errno("fclose");
3009 struct revert_file_args {
3010 struct got_worktree *worktree;
3011 struct got_fileindex *fileindex;
3012 got_worktree_checkout_cb progress_cb;
3014 got_worktree_patch_cb patch_cb;
3016 struct got_repository *repo;
3019 static const struct got_error *
3020 create_patched_content(char **path_outfile, int reverse_patch,
3021 struct got_object_id *blob_id, const char *path2,
3022 const char *relpath, struct got_repository *repo,
3023 got_worktree_patch_cb patch_cb, void *patch_arg)
3025 const struct got_error *err;
3026 struct got_blob_object *blob = NULL;
3027 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3028 char *path1 = NULL, *id_str = NULL;
3029 struct stat sb1, sb2;
3030 struct got_diff_changes *changes = NULL;
3031 struct got_diff_state *ds = NULL;
3032 struct got_diff_args *args = NULL;
3033 struct got_diff_change *change;
3034 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3037 *path_outfile = NULL;
3039 err = got_object_id_str(&id_str, blob_id);
3043 f2 = fopen(path2, "r");
3045 err = got_error_from_errno2("fopen", path2);
3049 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
3053 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
3057 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
3061 if (stat(path1, &sb1) == -1) {
3062 err = got_error_from_errno2("stat", path1);
3065 if (stat(path2, &sb2) == -1) {
3066 err = got_error_from_errno2("stat", path2);
3070 err = got_diff_files(&changes, &ds, &args, &diff_flags,
3071 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
3075 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
3079 if (fseek(f1, 0L, SEEK_SET) == -1)
3080 return got_ferror(f1, GOT_ERR_IO);
3081 if (fseek(f2, 0L, SEEK_SET) == -1)
3082 return got_ferror(f2, GOT_ERR_IO);
3083 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
3085 err = apply_or_reject_change(&choice, change, ++n,
3086 changes->nchanges, ds, args, diff_flags, relpath,
3087 f1, f2, &line_cur1, &line_cur2,
3088 reverse_patch ? NULL : outfile,
3089 reverse_patch ? outfile : NULL,
3090 patch_cb, patch_arg);
3093 if (choice == GOT_PATCH_CHOICE_YES)
3095 else if (choice == GOT_PATCH_CHOICE_QUIT)
3099 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
3100 reverse_patch ? NULL : outfile,
3101 reverse_patch ? outfile : NULL);
3105 got_object_blob_close(blob);
3106 if (f1 && fclose(f1) == EOF && err == NULL)
3107 err = got_error_from_errno2("fclose", path1);
3108 if (f2 && fclose(f2) == EOF && err == NULL)
3109 err = got_error_from_errno2("fclose", path2);
3110 if (outfile && fclose(outfile) == EOF && err == NULL)
3111 err = got_error_from_errno2("fclose", *path_outfile);
3112 if (path1 && unlink(path1) == -1 && err == NULL)
3113 err = got_error_from_errno2("unlink", path1);
3114 if (err || !have_content) {
3115 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
3116 err = got_error_from_errno2("unlink", *path_outfile);
3117 free(*path_outfile);
3118 *path_outfile = NULL;
3122 got_diff_state_free(ds);
3126 got_diff_free_changes(changes);
3131 static const struct got_error *
3132 revert_file(void *arg, unsigned char status, unsigned char staged_status,
3133 const char *relpath, struct got_object_id *blob_id,
3134 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
3136 struct revert_file_args *a = arg;
3137 const struct got_error *err = NULL;
3138 char *parent_path = NULL;
3139 struct got_fileindex_entry *ie;
3140 struct got_tree_object *tree = NULL;
3141 struct got_object_id *tree_id = NULL;
3142 const struct got_tree_entry *te = NULL;
3143 char *tree_path = NULL, *te_name;
3144 char *ondisk_path = NULL, *path_content = NULL;
3145 struct got_blob_object *blob = NULL;
3147 /* Reverting a staged deletion is a no-op. */
3148 if (status == GOT_STATUS_DELETE &&
3149 staged_status != GOT_STATUS_NO_CHANGE)
3152 if (status == GOT_STATUS_UNVERSIONED)
3153 return (*a->progress_cb)(a->progress_arg,
3154 GOT_STATUS_UNVERSIONED, relpath);
3156 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3158 return got_error(GOT_ERR_BAD_PATH);
3160 /* Construct in-repository path of tree which contains this blob. */
3161 err = got_path_dirname(&parent_path, ie->path);
3163 if (err->code != GOT_ERR_BAD_PATH)
3165 parent_path = strdup("/");
3166 if (parent_path == NULL) {
3167 err = got_error_from_errno("strdup");
3171 if (got_path_is_root_dir(a->worktree->path_prefix)) {
3172 tree_path = strdup(parent_path);
3173 if (tree_path == NULL) {
3174 err = got_error_from_errno("strdup");
3178 if (got_path_is_root_dir(parent_path)) {
3179 tree_path = strdup(a->worktree->path_prefix);
3180 if (tree_path == NULL) {
3181 err = got_error_from_errno("strdup");
3185 if (asprintf(&tree_path, "%s/%s",
3186 a->worktree->path_prefix, parent_path) == -1) {
3187 err = got_error_from_errno("asprintf");
3193 err = got_object_id_by_path(&tree_id, a->repo,
3194 a->worktree->base_commit_id, tree_path);
3196 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
3197 (status == GOT_STATUS_ADD ||
3198 staged_status == GOT_STATUS_ADD)))
3201 err = got_object_open_as_tree(&tree, a->repo, tree_id);
3205 te_name = basename(ie->path);
3206 if (te_name == NULL) {
3207 err = got_error_from_errno2("basename", ie->path);
3211 te = got_object_tree_find_entry(tree, te_name);
3212 if (te == NULL && status != GOT_STATUS_ADD &&
3213 staged_status != GOT_STATUS_ADD) {
3214 err = got_error(GOT_ERR_NO_TREE_ENTRY);
3220 case GOT_STATUS_ADD:
3222 int choice = GOT_PATCH_CHOICE_NONE;
3223 err = (*a->patch_cb)(&choice, a->patch_arg,
3224 status, ie->path, NULL, 1, 1);
3227 if (choice != GOT_PATCH_CHOICE_YES)
3230 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
3234 got_fileindex_entry_remove(a->fileindex, ie);
3236 case GOT_STATUS_DELETE:
3238 int choice = GOT_PATCH_CHOICE_NONE;
3239 err = (*a->patch_cb)(&choice, a->patch_arg,
3240 status, ie->path, NULL, 1, 1);
3243 if (choice != GOT_PATCH_CHOICE_YES)
3247 case GOT_STATUS_MODIFY:
3248 case GOT_STATUS_CONFLICT:
3249 case GOT_STATUS_MISSING: {
3250 struct got_object_id id;
3251 if (staged_status == GOT_STATUS_ADD ||
3252 staged_status == GOT_STATUS_MODIFY) {
3253 memcpy(id.sha1, ie->staged_blob_sha1,
3254 SHA1_DIGEST_LENGTH);
3256 memcpy(id.sha1, ie->blob_sha1,
3257 SHA1_DIGEST_LENGTH);
3258 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
3262 if (asprintf(&ondisk_path, "%s/%s",
3263 got_worktree_get_root_path(a->worktree), relpath) == -1) {
3264 err = got_error_from_errno("asprintf");
3268 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
3269 status == GOT_STATUS_CONFLICT)) {
3270 err = create_patched_content(&path_content, 1, &id,
3271 ondisk_path, ie->path, a->repo,
3272 a->patch_cb, a->patch_arg);
3273 if (err || path_content == NULL)
3275 if (rename(path_content, ondisk_path) == -1) {
3276 err = got_error_from_errno3("rename",
3277 path_content, ondisk_path);
3281 err = install_blob(a->worktree, ondisk_path, ie->path,
3282 te ? te->mode : GOT_DEFAULT_FILE_MODE,
3283 got_fileindex_perms_to_st(ie), blob, 0, 1,
3284 a->repo, a->progress_cb, a->progress_arg);
3287 if (status == GOT_STATUS_DELETE) {
3288 err = update_blob_fileindex_entry(a->worktree,
3289 a->fileindex, ie, ondisk_path, ie->path,
3306 got_object_blob_close(blob);
3308 got_object_tree_close(tree);
3313 const struct got_error *
3314 got_worktree_revert(struct got_worktree *worktree,
3315 struct got_pathlist_head *paths,
3316 got_worktree_checkout_cb progress_cb, void *progress_arg,
3317 got_worktree_patch_cb patch_cb, void *patch_arg,
3318 struct got_repository *repo)
3320 struct got_fileindex *fileindex = NULL;
3321 char *fileindex_path = NULL;
3322 const struct got_error *err = NULL, *unlockerr = NULL;
3323 const struct got_error *sync_err = NULL;
3324 struct got_pathlist_entry *pe;
3325 struct revert_file_args rfa;
3327 err = lock_worktree(worktree, LOCK_EX);
3331 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3335 rfa.worktree = worktree;
3336 rfa.fileindex = fileindex;
3337 rfa.progress_cb = progress_cb;
3338 rfa.progress_arg = progress_arg;
3339 rfa.patch_cb = patch_cb;
3340 rfa.patch_arg = patch_arg;
3342 TAILQ_FOREACH(pe, paths, entry) {
3343 err = worktree_status(worktree, pe->path, fileindex, repo,
3344 revert_file, &rfa, NULL, NULL);
3348 sync_err = sync_fileindex(fileindex, fileindex_path);
3349 if (sync_err && err == NULL)
3352 free(fileindex_path);
3354 got_fileindex_free(fileindex);
3355 unlockerr = lock_worktree(worktree, LOCK_SH);
3356 if (unlockerr && err == NULL)
3362 free_commitable(struct got_commitable *ct)
3365 free(ct->in_repo_path);
3366 free(ct->ondisk_path);
3368 free(ct->base_blob_id);
3369 free(ct->staged_blob_id);
3370 free(ct->base_commit_id);
3374 struct collect_commitables_arg {
3375 struct got_pathlist_head *commitable_paths;
3376 struct got_repository *repo;
3377 struct got_worktree *worktree;
3378 int have_staged_files;
3381 static const struct got_error *
3382 collect_commitables(void *arg, unsigned char status,
3383 unsigned char staged_status, const char *relpath,
3384 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3385 struct got_object_id *commit_id)
3387 struct collect_commitables_arg *a = arg;
3388 const struct got_error *err = NULL;
3389 struct got_commitable *ct = NULL;
3390 struct got_pathlist_entry *new = NULL;
3391 char *parent_path = NULL, *path = NULL;
3394 if (a->have_staged_files) {
3395 if (staged_status != GOT_STATUS_MODIFY &&
3396 staged_status != GOT_STATUS_ADD &&
3397 staged_status != GOT_STATUS_DELETE)
3400 if (status == GOT_STATUS_CONFLICT)
3401 return got_error(GOT_ERR_COMMIT_CONFLICT);
3403 if (status != GOT_STATUS_MODIFY &&
3404 status != GOT_STATUS_ADD &&
3405 status != GOT_STATUS_DELETE)
3409 if (asprintf(&path, "/%s", relpath) == -1) {
3410 err = got_error_from_errno("asprintf");
3413 if (strcmp(path, "/") == 0) {
3414 parent_path = strdup("");
3415 if (parent_path == NULL)
3416 return got_error_from_errno("strdup");
3418 err = got_path_dirname(&parent_path, path);
3423 ct = calloc(1, sizeof(*ct));
3425 err = got_error_from_errno("calloc");
3429 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
3431 err = got_error_from_errno("asprintf");
3434 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
3435 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3437 if (lstat(ct->ondisk_path, &sb) != 0) {
3438 err = got_error_from_errno2("lstat", ct->ondisk_path);
3441 ct->mode = sb.st_mode;
3444 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
3445 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
3447 err = got_error_from_errno("asprintf");
3451 ct->status = status;
3452 ct->staged_status = staged_status;
3453 ct->blob_id = NULL; /* will be filled in when blob gets created */
3454 if (ct->status != GOT_STATUS_ADD &&
3455 ct->staged_status != GOT_STATUS_ADD) {
3456 ct->base_blob_id = got_object_id_dup(blob_id);
3457 if (ct->base_blob_id == NULL) {
3458 err = got_error_from_errno("got_object_id_dup");
3461 ct->base_commit_id = got_object_id_dup(commit_id);
3462 if (ct->base_commit_id == NULL) {
3463 err = got_error_from_errno("got_object_id_dup");
3467 if (ct->staged_status == GOT_STATUS_ADD ||
3468 ct->staged_status == GOT_STATUS_MODIFY) {
3469 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
3470 if (ct->staged_blob_id == NULL) {
3471 err = got_error_from_errno("got_object_id_dup");
3475 ct->path = strdup(path);
3476 if (ct->path == NULL) {
3477 err = got_error_from_errno("strdup");
3480 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
3482 if (ct && (err || new == NULL))
3483 free_commitable(ct);
3489 static const struct got_error *write_tree(struct got_object_id **,
3490 struct got_tree_object *, const char *, struct got_pathlist_head *,
3491 got_worktree_status_cb status_cb, void *status_arg,
3492 struct got_repository *);
3494 static const struct got_error *
3495 write_subtree(struct got_object_id **new_subtree_id,
3496 struct got_tree_entry *te, const char *parent_path,
3497 struct got_pathlist_head *commitable_paths,
3498 got_worktree_status_cb status_cb, void *status_arg,
3499 struct got_repository *repo)
3501 const struct got_error *err = NULL;
3502 struct got_tree_object *subtree;
3505 if (asprintf(&subpath, "%s%s%s", parent_path,
3506 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
3507 return got_error_from_errno("asprintf");
3509 err = got_object_open_as_tree(&subtree, repo, te->id);
3513 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
3514 status_cb, status_arg, repo);
3515 got_object_tree_close(subtree);
3520 static const struct got_error *
3521 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
3523 const struct got_error *err = NULL;
3524 char *ct_parent_path = NULL;
3528 if (strchr(ct->in_repo_path, '/') == NULL) {
3529 *match = got_path_is_root_dir(path);
3533 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
3536 *match = (strcmp(path, ct_parent_path) == 0);
3537 free(ct_parent_path);
3542 get_ct_file_mode(struct got_commitable *ct)
3544 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
3547 static const struct got_error *
3548 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
3549 struct got_tree_entry *te, struct got_commitable *ct)
3551 const struct got_error *err = NULL;
3555 err = got_object_tree_entry_dup(new_te, te);
3559 (*new_te)->mode = get_ct_file_mode(ct);
3561 free((*new_te)->id);
3562 if (ct->staged_status == GOT_STATUS_MODIFY)
3563 (*new_te)->id = got_object_id_dup(ct->staged_blob_id);
3565 (*new_te)->id = got_object_id_dup(ct->blob_id);
3566 if ((*new_te)->id == NULL) {
3567 err = got_error_from_errno("got_object_id_dup");
3571 if (err && *new_te) {
3572 got_object_tree_entry_close(*new_te);
3578 static const struct got_error *
3579 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
3580 struct got_commitable *ct)
3582 const struct got_error *err = NULL;
3587 *new_te = calloc(1, sizeof(**new_te));
3588 if (*new_te == NULL)
3589 return got_error_from_errno("calloc");
3591 ct_name = basename(ct->path);
3592 if (ct_name == NULL) {
3593 err = got_error_from_errno2("basename", ct->path);
3596 (*new_te)->name = strdup(ct_name);
3597 if ((*new_te)->name == NULL) {
3598 err = got_error_from_errno("strdup");
3602 (*new_te)->mode = get_ct_file_mode(ct);
3604 if (ct->staged_status == GOT_STATUS_ADD)
3605 (*new_te)->id = got_object_id_dup(ct->staged_blob_id);
3607 (*new_te)->id = got_object_id_dup(ct->blob_id);
3608 if ((*new_te)->id == NULL) {
3609 err = got_error_from_errno("got_object_id_dup");
3613 if (err && *new_te) {
3614 got_object_tree_entry_close(*new_te);
3620 static const struct got_error *
3621 insert_tree_entry(struct got_tree_entry *new_te,
3622 struct got_pathlist_head *paths)
3624 const struct got_error *err = NULL;
3625 struct got_pathlist_entry *new_pe;
3627 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
3631 return got_error(GOT_ERR_TREE_DUP_ENTRY);
3635 static const struct got_error *
3636 report_ct_status(struct got_commitable *ct,
3637 got_worktree_status_cb status_cb, void *status_arg)
3639 const char *ct_path = ct->path;
3640 unsigned char status;
3642 while (ct_path[0] == '/')
3645 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
3646 status = ct->staged_status;
3648 status = ct->status;
3650 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
3651 ct_path, ct->blob_id, NULL, NULL);
3654 static const struct got_error *
3655 match_modified_subtree(int *modified, struct got_tree_entry *te,
3656 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
3658 const struct got_error *err = NULL;
3659 struct got_pathlist_entry *pe;
3664 if (asprintf(&te_path, "%s%s%s", base_tree_path,
3665 got_path_is_root_dir(base_tree_path) ? "" : "/",
3667 return got_error_from_errno("asprintf");
3669 TAILQ_FOREACH(pe, commitable_paths, entry) {
3670 struct got_commitable *ct = pe->data;
3671 *modified = got_path_is_child(ct->in_repo_path, te_path,
3681 static const struct got_error *
3682 match_deleted_or_modified_ct(struct got_commitable **ctp,
3683 struct got_tree_entry *te, const char *base_tree_path,
3684 struct got_pathlist_head *commitable_paths)
3686 const struct got_error *err = NULL;
3687 struct got_pathlist_entry *pe;
3691 TAILQ_FOREACH(pe, commitable_paths, entry) {
3692 struct got_commitable *ct = pe->data;
3693 char *ct_name = NULL;
3696 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
3697 if (ct->status != GOT_STATUS_MODIFY &&
3698 ct->status != GOT_STATUS_DELETE)
3701 if (ct->staged_status != GOT_STATUS_MODIFY &&
3702 ct->staged_status != GOT_STATUS_DELETE)
3706 if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
3709 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
3715 ct_name = basename(pe->path);
3716 if (ct_name == NULL)
3717 return got_error_from_errno2("basename", pe->path);
3719 if (strcmp(te->name, ct_name) != 0)
3729 static const struct got_error *
3730 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
3731 const char *child_path, const char *path_base_tree,
3732 struct got_pathlist_head *commitable_paths,
3733 got_worktree_status_cb status_cb, void *status_arg,
3734 struct got_repository *repo)
3736 const struct got_error *err = NULL;
3737 struct got_tree_entry *new_te;
3742 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
3743 got_path_is_root_dir(path_base_tree) ? "" : "/",
3745 return got_error_from_errno("asprintf");
3747 new_te = calloc(1, sizeof(*new_te));
3749 return got_error_from_errno("calloc");
3750 new_te->mode = S_IFDIR;
3751 new_te->name = strdup(child_path);
3752 if (new_te->name == NULL) {
3753 err = got_error_from_errno("strdup");
3754 got_object_tree_entry_close(new_te);
3757 err = write_tree(&new_te->id, NULL, subtree_path,
3758 commitable_paths, status_cb, status_arg, repo);
3760 got_object_tree_entry_close(new_te);
3770 static const struct got_error *
3771 write_tree(struct got_object_id **new_tree_id,
3772 struct got_tree_object *base_tree, const char *path_base_tree,
3773 struct got_pathlist_head *commitable_paths,
3774 got_worktree_status_cb status_cb, void *status_arg,
3775 struct got_repository *repo)
3777 const struct got_error *err = NULL;
3778 const struct got_tree_entries *base_entries = NULL;
3779 struct got_pathlist_head paths;
3780 struct got_tree_entries new_tree_entries;
3781 struct got_tree_entry *te, *new_te = NULL;
3782 struct got_pathlist_entry *pe;
3785 new_tree_entries.nentries = 0;
3786 SIMPLEQ_INIT(&new_tree_entries.head);
3788 /* Insert, and recurse into, newly added entries first. */
3789 TAILQ_FOREACH(pe, commitable_paths, entry) {
3790 struct got_commitable *ct = pe->data;
3791 char *child_path = NULL, *slash;
3793 if ((ct->status != GOT_STATUS_ADD &&
3794 ct->staged_status != GOT_STATUS_ADD) ||
3795 (ct->flags & GOT_COMMITABLE_ADDED))
3798 if (!got_path_is_child(pe->path, path_base_tree,
3799 strlen(path_base_tree)))
3802 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
3807 slash = strchr(child_path, '/');
3808 if (slash == NULL) {
3809 err = alloc_added_blob_tree_entry(&new_te, ct);
3812 err = report_ct_status(ct, status_cb, status_arg);
3815 ct->flags |= GOT_COMMITABLE_ADDED;
3816 err = insert_tree_entry(new_te, &paths);
3820 *slash = '\0'; /* trim trailing path components */
3821 if (base_tree == NULL ||
3822 got_object_tree_find_entry(base_tree, child_path)
3824 err = make_subtree_for_added_blob(&new_te,
3825 child_path, path_base_tree,
3826 commitable_paths, status_cb, status_arg,
3830 err = insert_tree_entry(new_te, &paths);
3838 /* Handle modified and deleted entries. */
3839 base_entries = got_object_tree_get_entries(base_tree);
3840 SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
3841 struct got_commitable *ct = NULL;
3843 if (got_object_tree_entry_is_submodule(te)) {
3844 /* Entry is a submodule; just copy it. */
3845 err = got_object_tree_entry_dup(&new_te, te);
3848 err = insert_tree_entry(new_te, &paths);
3854 if (S_ISDIR(te->mode)) {
3856 err = got_object_tree_entry_dup(&new_te, te);
3859 err = match_modified_subtree(&modified, te,
3860 path_base_tree, commitable_paths);
3863 /* Avoid recursion into unmodified subtrees. */
3866 err = write_subtree(&new_te->id, te,
3867 path_base_tree, commitable_paths,
3868 status_cb, status_arg, repo);
3872 err = insert_tree_entry(new_te, &paths);
3878 err = match_deleted_or_modified_ct(&ct, te,
3879 path_base_tree, commitable_paths);
3881 /* NB: Deleted entries get dropped here. */
3882 if (ct->status == GOT_STATUS_MODIFY ||
3883 ct->staged_status == GOT_STATUS_MODIFY) {
3884 err = alloc_modified_blob_tree_entry(
3888 err = insert_tree_entry(new_te, &paths);
3892 err = report_ct_status(ct, status_cb,
3897 /* Entry is unchanged; just copy it. */
3898 err = got_object_tree_entry_dup(&new_te, te);
3901 err = insert_tree_entry(new_te, &paths);
3908 /* Write new list of entries; deleted entries have been dropped. */
3909 TAILQ_FOREACH(pe, &paths, entry) {
3910 struct got_tree_entry *te = pe->data;
3911 new_tree_entries.nentries++;
3912 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
3914 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
3916 got_object_tree_entries_close(&new_tree_entries);
3917 got_pathlist_free(&paths);
3921 static const struct got_error *
3922 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
3923 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
3924 int have_staged_files)
3926 const struct got_error *err = NULL;
3927 struct got_pathlist_entry *pe;
3929 TAILQ_FOREACH(pe, commitable_paths, entry) {
3930 struct got_fileindex_entry *ie;
3931 struct got_commitable *ct = pe->data;
3933 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
3935 if (ct->status == GOT_STATUS_DELETE ||
3936 ct->staged_status == GOT_STATUS_DELETE) {
3937 got_fileindex_entry_remove(fileindex, ie);
3938 got_fileindex_entry_free(ie);
3939 } else if (ct->staged_status == GOT_STATUS_ADD ||
3940 ct->staged_status == GOT_STATUS_MODIFY) {
3941 got_fileindex_entry_stage_set(ie,
3942 GOT_FILEIDX_STAGE_NONE);
3943 err = got_fileindex_entry_update(ie,
3944 ct->ondisk_path, ct->staged_blob_id->sha1,
3945 new_base_commit_id->sha1,
3946 !have_staged_files);
3948 err = got_fileindex_entry_update(ie,
3949 ct->ondisk_path, ct->blob_id->sha1,
3950 new_base_commit_id->sha1,
3951 !have_staged_files);
3953 err = got_fileindex_entry_alloc(&ie,
3954 ct->ondisk_path, pe->path, ct->blob_id->sha1,
3955 new_base_commit_id->sha1);
3958 err = got_fileindex_entry_add(fileindex, ie);
3967 static const struct got_error *
3968 check_out_of_date(const char *in_repo_path, unsigned char status,
3969 unsigned char staged_status, struct got_object_id *base_blob_id,
3970 struct got_object_id *base_commit_id,
3971 struct got_object_id *head_commit_id, struct got_repository *repo,
3974 const struct got_error *err = NULL;
3975 struct got_object_id *id = NULL;
3977 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
3978 /* Trivial case: base commit == head commit */
3979 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
3982 * Ensure file content which local changes were based
3983 * on matches file content in the branch head.
3985 err = got_object_id_by_path(&id, repo, head_commit_id,
3988 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3989 err = got_error(ood_errcode);
3991 } else if (got_object_id_cmp(id, base_blob_id) != 0)
3992 err = got_error(ood_errcode);
3994 /* Require that added files don't exist in the branch head. */
3995 err = got_object_id_by_path(&id, repo, head_commit_id,
3997 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3999 err = id ? got_error(ood_errcode) : NULL;
4006 const struct got_error *
4007 commit_worktree(struct got_object_id **new_commit_id,
4008 struct got_pathlist_head *commitable_paths,
4009 struct got_object_id *head_commit_id, struct got_worktree *worktree,
4010 const char *author, const char *committer,
4011 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4012 got_worktree_status_cb status_cb, void *status_arg,
4013 struct got_repository *repo)
4015 const struct got_error *err = NULL, *unlockerr = NULL;
4016 struct got_pathlist_entry *pe;
4017 const char *head_ref_name = NULL;
4018 struct got_commit_object *head_commit = NULL;
4019 struct got_reference *head_ref2 = NULL;
4020 struct got_object_id *head_commit_id2 = NULL;
4021 struct got_tree_object *head_tree = NULL;
4022 struct got_object_id *new_tree_id = NULL;
4023 struct got_object_id_queue parent_ids;
4024 struct got_object_qid *pid = NULL;
4025 char *logmsg = NULL;
4027 *new_commit_id = NULL;
4029 SIMPLEQ_INIT(&parent_ids);
4031 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
4035 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
4039 if (commit_msg_cb != NULL) {
4040 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
4045 if (logmsg == NULL || strlen(logmsg) == 0) {
4046 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
4050 /* Create blobs from added and modified files and record their IDs. */
4051 TAILQ_FOREACH(pe, commitable_paths, entry) {
4052 struct got_commitable *ct = pe->data;
4055 /* Blobs for staged files already exist. */
4056 if (ct->staged_status == GOT_STATUS_ADD ||
4057 ct->staged_status == GOT_STATUS_MODIFY)
4060 if (ct->status != GOT_STATUS_ADD &&
4061 ct->status != GOT_STATUS_MODIFY)
4064 if (asprintf(&ondisk_path, "%s/%s",
4065 worktree->root_path, pe->path) == -1) {
4066 err = got_error_from_errno("asprintf");
4069 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
4075 /* Recursively write new tree objects. */
4076 err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
4077 status_cb, status_arg, repo);
4081 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
4084 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
4085 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
4086 1, author, time(NULL), committer, time(NULL), logmsg, repo);
4087 got_object_qid_free(pid);
4093 /* Check if a concurrent commit to our branch has occurred. */
4094 head_ref_name = got_worktree_get_head_ref_name(worktree);
4095 if (head_ref_name == NULL) {
4096 err = got_error_from_errno("got_worktree_get_head_ref_name");
4099 /* Lock the reference here to prevent concurrent modification. */
4100 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
4103 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
4106 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
4107 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
4110 /* Update branch head in repository. */
4111 err = got_ref_change_ref(head_ref2, *new_commit_id);
4114 err = got_ref_write(head_ref2, repo);
4118 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
4122 err = ref_base_commit(worktree, repo);
4127 got_object_tree_close(head_tree);
4129 got_object_commit_close(head_commit);
4130 free(head_commit_id2);
4132 unlockerr = got_ref_unlock(head_ref2);
4133 if (unlockerr && err == NULL)
4135 got_ref_close(head_ref2);
4140 static const struct got_error *
4141 check_path_is_commitable(const char *path,
4142 struct got_pathlist_head *commitable_paths)
4144 struct got_pathlist_entry *cpe = NULL;
4145 size_t path_len = strlen(path);
4147 TAILQ_FOREACH(cpe, commitable_paths, entry) {
4148 struct got_commitable *ct = cpe->data;
4149 const char *ct_path = ct->path;
4151 while (ct_path[0] == '/')
4154 if (strcmp(path, ct_path) == 0 ||
4155 got_path_is_child(ct_path, path, path_len))
4160 return got_error_path(path, GOT_ERR_BAD_PATH);
4165 static const struct got_error *
4166 check_staged_file(void *arg, struct got_fileindex_entry *ie)
4168 int *have_staged_files = arg;
4170 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
4171 *have_staged_files = 1;
4172 return got_error(GOT_ERR_CANCELLED);
4178 static const struct got_error *
4179 check_non_staged_files(struct got_fileindex *fileindex,
4180 struct got_pathlist_head *paths)
4182 struct got_pathlist_entry *pe;
4183 struct got_fileindex_entry *ie;
4185 TAILQ_FOREACH(pe, paths, entry) {
4186 if (pe->path[0] == '\0')
4188 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4190 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
4191 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
4192 return got_error_path(pe->path,
4193 GOT_ERR_FILE_NOT_STAGED);
4199 const struct got_error *
4200 got_worktree_commit(struct got_object_id **new_commit_id,
4201 struct got_worktree *worktree, struct got_pathlist_head *paths,
4202 const char *author, const char *committer,
4203 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4204 got_worktree_status_cb status_cb, void *status_arg,
4205 struct got_repository *repo)
4207 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
4208 struct got_fileindex *fileindex = NULL;
4209 char *fileindex_path = NULL;
4210 struct got_pathlist_head commitable_paths;
4211 struct collect_commitables_arg cc_arg;
4212 struct got_pathlist_entry *pe;
4213 struct got_reference *head_ref = NULL;
4214 struct got_object_id *head_commit_id = NULL;
4215 int have_staged_files = 0;
4217 *new_commit_id = NULL;
4219 TAILQ_INIT(&commitable_paths);
4221 err = lock_worktree(worktree, LOCK_EX);
4225 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4229 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4233 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4237 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
4238 &have_staged_files);
4239 if (err && err->code != GOT_ERR_CANCELLED)
4241 if (have_staged_files) {
4242 err = check_non_staged_files(fileindex, paths);
4247 cc_arg.commitable_paths = &commitable_paths;
4248 cc_arg.worktree = worktree;
4250 cc_arg.have_staged_files = have_staged_files;
4251 TAILQ_FOREACH(pe, paths, entry) {
4252 err = worktree_status(worktree, pe->path, fileindex, repo,
4253 collect_commitables, &cc_arg, NULL, NULL);
4258 if (TAILQ_EMPTY(&commitable_paths)) {
4259 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4263 TAILQ_FOREACH(pe, paths, entry) {
4264 err = check_path_is_commitable(pe->path, &commitable_paths);
4269 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4270 struct got_commitable *ct = pe->data;
4271 const char *ct_path = ct->in_repo_path;
4273 while (ct_path[0] == '/')
4275 err = check_out_of_date(ct_path, ct->status,
4276 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
4277 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
4283 err = commit_worktree(new_commit_id, &commitable_paths,
4284 head_commit_id, worktree, author, committer,
4285 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
4289 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4290 fileindex, have_staged_files);
4291 sync_err = sync_fileindex(fileindex, fileindex_path);
4292 if (sync_err && err == NULL)
4296 got_fileindex_free(fileindex);
4297 free(fileindex_path);
4298 unlockerr = lock_worktree(worktree, LOCK_SH);
4299 if (unlockerr && err == NULL)
4301 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4302 struct got_commitable *ct = pe->data;
4303 free_commitable(ct);
4305 got_pathlist_free(&commitable_paths);
4310 got_commitable_get_path(struct got_commitable *ct)
4316 got_commitable_get_status(struct got_commitable *ct)
4321 struct check_rebase_ok_arg {
4322 struct got_worktree *worktree;
4323 struct got_repository *repo;
4326 static const struct got_error *
4327 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
4329 const struct got_error *err = NULL;
4330 struct check_rebase_ok_arg *a = arg;
4331 unsigned char status;
4335 /* Reject rebase of a work tree with mixed base commits. */
4336 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
4337 SHA1_DIGEST_LENGTH))
4338 return got_error(GOT_ERR_MIXED_COMMITS);
4340 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
4342 return got_error_from_errno("asprintf");
4344 /* Reject rebase of a work tree with modified or staged files. */
4345 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
4350 if (status != GOT_STATUS_NO_CHANGE)
4351 return got_error(GOT_ERR_MODIFIED);
4352 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
4353 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
4358 const struct got_error *
4359 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
4360 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
4361 struct got_worktree *worktree, struct got_reference *branch,
4362 struct got_repository *repo)
4364 const struct got_error *err = NULL;
4365 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4366 char *branch_ref_name = NULL;
4367 char *fileindex_path = NULL;
4368 struct check_rebase_ok_arg ok_arg;
4369 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
4371 *new_base_branch_ref = NULL;
4375 err = lock_worktree(worktree, LOCK_EX);
4379 err = open_fileindex(fileindex, &fileindex_path, worktree);
4383 ok_arg.worktree = worktree;
4385 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4390 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4394 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4398 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4402 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
4407 err = got_ref_alloc_symref(new_base_branch_ref,
4408 new_base_branch_ref_name, wt_branch);
4411 err = got_ref_write(*new_base_branch_ref, repo);
4415 /* TODO Lock original branch's ref while rebasing? */
4417 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
4421 err = got_ref_write(branch_ref, repo);
4425 err = got_ref_alloc(tmp_branch, tmp_branch_name,
4426 worktree->base_commit_id);
4429 err = got_ref_write(*tmp_branch, repo);
4433 err = got_worktree_set_head_ref(worktree, *tmp_branch);
4437 free(fileindex_path);
4438 free(tmp_branch_name);
4439 free(new_base_branch_ref_name);
4440 free(branch_ref_name);
4442 got_ref_close(branch_ref);
4444 got_ref_close(wt_branch);
4446 if (*new_base_branch_ref) {
4447 got_ref_close(*new_base_branch_ref);
4448 *new_base_branch_ref = NULL;
4451 got_ref_close(*tmp_branch);
4455 got_fileindex_free(*fileindex);
4458 lock_worktree(worktree, LOCK_SH);
4463 const struct got_error *
4464 got_worktree_rebase_continue(struct got_object_id **commit_id,
4465 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
4466 struct got_reference **branch, struct got_fileindex **fileindex,
4467 struct got_worktree *worktree, struct got_repository *repo)
4469 const struct got_error *err;
4470 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
4471 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
4472 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
4473 char *fileindex_path = NULL;
4474 int have_staged_files = 0;
4477 *new_base_branch = NULL;
4482 err = lock_worktree(worktree, LOCK_EX);
4486 err = open_fileindex(fileindex, &fileindex_path, worktree);
4490 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
4491 &have_staged_files);
4492 if (err && err->code != GOT_ERR_CANCELLED)
4494 if (have_staged_files) {
4495 err = got_error(GOT_ERR_STAGED_PATHS);
4499 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4503 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4507 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4511 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4515 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
4519 err = got_ref_open(branch, repo,
4520 got_ref_get_symref_target(branch_ref), 0);
4524 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4528 err = got_ref_resolve(commit_id, repo, commit_ref);
4532 err = got_ref_open(new_base_branch, repo,
4533 new_base_branch_ref_name, 0);
4537 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4541 free(commit_ref_name);
4542 free(branch_ref_name);
4543 free(fileindex_path);
4545 got_ref_close(commit_ref);
4547 got_ref_close(branch_ref);
4552 got_ref_close(*tmp_branch);
4555 if (*new_base_branch) {
4556 got_ref_close(*new_base_branch);
4557 *new_base_branch = NULL;
4560 got_ref_close(*branch);
4564 got_fileindex_free(*fileindex);
4567 lock_worktree(worktree, LOCK_SH);
4572 const struct got_error *
4573 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
4575 const struct got_error *err;
4576 char *tmp_branch_name = NULL;
4578 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4582 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4583 free(tmp_branch_name);
4587 static const struct got_error *
4588 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
4589 char **logmsg, void *arg)
4595 static const struct got_error *
4596 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
4597 const char *path, struct got_object_id *blob_id,
4598 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
4603 struct collect_merged_paths_arg {
4604 got_worktree_checkout_cb progress_cb;
4606 struct got_pathlist_head *merged_paths;
4609 static const struct got_error *
4610 collect_merged_paths(void *arg, unsigned char status, const char *path)
4612 const struct got_error *err;
4613 struct collect_merged_paths_arg *a = arg;
4615 struct got_pathlist_entry *new;
4617 err = (*a->progress_cb)(a->progress_arg, status, path);
4621 if (status != GOT_STATUS_MERGE &&
4622 status != GOT_STATUS_ADD &&
4623 status != GOT_STATUS_DELETE &&
4624 status != GOT_STATUS_CONFLICT)
4629 return got_error_from_errno("strdup");
4631 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
4632 if (err || new == NULL)
4638 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
4640 struct got_pathlist_entry *pe;
4642 TAILQ_FOREACH(pe, merged_paths, entry)
4643 free((char *)pe->path);
4645 got_pathlist_free(merged_paths);
4648 static const struct got_error *
4649 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
4650 struct got_repository *repo)
4652 const struct got_error *err;
4653 struct got_reference *commit_ref = NULL;
4655 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4657 if (err->code != GOT_ERR_NOT_REF)
4659 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
4662 err = got_ref_write(commit_ref, repo);
4666 struct got_object_id *stored_id;
4669 err = got_ref_resolve(&stored_id, repo, commit_ref);
4672 cmp = got_object_id_cmp(commit_id, stored_id);
4675 err = got_error(GOT_ERR_REBASE_COMMITID);
4681 got_ref_close(commit_ref);
4685 static const struct got_error *
4686 rebase_merge_files(struct got_pathlist_head *merged_paths,
4687 const char *commit_ref_name, struct got_worktree *worktree,
4688 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
4689 struct got_object_id *commit_id, struct got_repository *repo,
4690 got_worktree_checkout_cb progress_cb, void *progress_arg,
4691 got_cancel_cb cancel_cb, void *cancel_arg)
4693 const struct got_error *err;
4694 struct got_reference *commit_ref = NULL;
4695 struct collect_merged_paths_arg cmp_arg;
4696 char *fileindex_path;
4698 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4700 err = get_fileindex_path(&fileindex_path, worktree);
4704 cmp_arg.progress_cb = progress_cb;
4705 cmp_arg.progress_arg = progress_arg;
4706 cmp_arg.merged_paths = merged_paths;
4707 err = merge_files(worktree, fileindex, fileindex_path,
4708 parent_commit_id, commit_id, repo, collect_merged_paths,
4709 &cmp_arg, cancel_cb, cancel_arg);
4711 got_ref_close(commit_ref);
4715 const struct got_error *
4716 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
4717 struct got_worktree *worktree, struct got_fileindex *fileindex,
4718 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4719 struct got_repository *repo,
4720 got_worktree_checkout_cb progress_cb, void *progress_arg,
4721 got_cancel_cb cancel_cb, void *cancel_arg)
4723 const struct got_error *err;
4724 char *commit_ref_name;
4726 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4730 err = store_commit_id(commit_ref_name, commit_id, repo);
4734 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4735 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4736 progress_arg, cancel_cb, cancel_arg);
4738 free(commit_ref_name);
4742 const struct got_error *
4743 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
4744 struct got_worktree *worktree, struct got_fileindex *fileindex,
4745 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4746 struct got_repository *repo,
4747 got_worktree_checkout_cb progress_cb, void *progress_arg,
4748 got_cancel_cb cancel_cb, void *cancel_arg)
4750 const struct got_error *err;
4751 char *commit_ref_name;
4753 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4757 err = store_commit_id(commit_ref_name, commit_id, repo);
4761 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4762 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4763 progress_arg, cancel_cb, cancel_arg);
4765 free(commit_ref_name);
4769 static const struct got_error *
4770 rebase_commit(struct got_object_id **new_commit_id,
4771 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
4772 struct got_worktree *worktree, struct got_fileindex *fileindex,
4773 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
4774 const char *new_logmsg, struct got_repository *repo)
4776 const struct got_error *err, *sync_err;
4777 struct got_pathlist_head commitable_paths;
4778 struct collect_commitables_arg cc_arg;
4779 char *fileindex_path = NULL;
4780 struct got_reference *head_ref = NULL;
4781 struct got_object_id *head_commit_id = NULL;
4782 char *logmsg = NULL;
4784 TAILQ_INIT(&commitable_paths);
4785 *new_commit_id = NULL;
4787 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4789 err = get_fileindex_path(&fileindex_path, worktree);
4793 cc_arg.commitable_paths = &commitable_paths;
4794 cc_arg.worktree = worktree;
4796 cc_arg.have_staged_files = 0;
4798 * If possible get the status of individual files directly to
4799 * avoid crawling the entire work tree once per rebased commit.
4800 * TODO: Ideally, merged_paths would contain a list of commitables
4801 * we could use so we could skip worktree_status() entirely.
4804 struct got_pathlist_entry *pe;
4805 if (TAILQ_EMPTY(merged_paths)) {
4806 err = got_error(GOT_ERR_NO_MERGED_PATHS);
4809 TAILQ_FOREACH(pe, merged_paths, entry) {
4810 err = worktree_status(worktree, pe->path, fileindex,
4811 repo, collect_commitables, &cc_arg, NULL, NULL);
4816 err = worktree_status(worktree, "", fileindex, repo,
4817 collect_commitables, &cc_arg, NULL, NULL);
4822 if (TAILQ_EMPTY(&commitable_paths)) {
4823 /* No-op change; commit will be elided. */
4824 err = got_ref_delete(commit_ref, repo);
4827 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4831 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4835 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4840 logmsg = strdup(new_logmsg);
4841 if (logmsg == NULL) {
4842 err = got_error_from_errno("strdup");
4846 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
4851 /* NB: commit_worktree will call free(logmsg) */
4852 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
4853 worktree, got_object_commit_get_author(orig_commit),
4854 got_object_commit_get_committer(orig_commit),
4855 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
4859 err = got_ref_change_ref(tmp_branch, *new_commit_id);
4863 err = got_ref_delete(commit_ref, repo);
4867 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4869 sync_err = sync_fileindex(fileindex, fileindex_path);
4870 if (sync_err && err == NULL)
4873 free(fileindex_path);
4874 free(head_commit_id);
4876 got_ref_close(head_ref);
4878 free(*new_commit_id);
4879 *new_commit_id = NULL;
4884 const struct got_error *
4885 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
4886 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4887 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4888 struct got_commit_object *orig_commit,
4889 struct got_object_id *orig_commit_id, struct got_repository *repo)
4891 const struct got_error *err;
4892 char *commit_ref_name;
4893 struct got_reference *commit_ref = NULL;
4894 struct got_object_id *commit_id = NULL;
4896 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4900 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4903 err = got_ref_resolve(&commit_id, repo, commit_ref);
4906 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4907 err = got_error(GOT_ERR_REBASE_COMMITID);
4911 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4912 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
4915 got_ref_close(commit_ref);
4916 free(commit_ref_name);
4921 const struct got_error *
4922 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
4923 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4924 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4925 struct got_commit_object *orig_commit,
4926 struct got_object_id *orig_commit_id, const char *new_logmsg,
4927 struct got_repository *repo)
4929 const struct got_error *err;
4930 char *commit_ref_name;
4931 struct got_reference *commit_ref = NULL;
4932 struct got_object_id *commit_id = NULL;
4934 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4938 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4941 err = got_ref_resolve(&commit_id, repo, commit_ref);
4944 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4945 err = got_error(GOT_ERR_HISTEDIT_COMMITID);
4949 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4950 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
4953 got_ref_close(commit_ref);
4954 free(commit_ref_name);
4959 const struct got_error *
4960 got_worktree_rebase_postpone(struct got_worktree *worktree,
4961 struct got_fileindex *fileindex)
4964 got_fileindex_free(fileindex);
4965 return lock_worktree(worktree, LOCK_SH);
4968 static const struct got_error *
4969 delete_ref(const char *name, struct got_repository *repo)
4971 const struct got_error *err;
4972 struct got_reference *ref;
4974 err = got_ref_open(&ref, repo, name, 0);
4976 if (err->code == GOT_ERR_NOT_REF)
4981 err = got_ref_delete(ref, repo);
4986 static const struct got_error *
4987 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
4989 const struct got_error *err;
4990 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4991 char *branch_ref_name = NULL, *commit_ref_name = NULL;
4993 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4996 err = delete_ref(tmp_branch_name, repo);
5000 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5003 err = delete_ref(new_base_branch_ref_name, repo);
5007 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5010 err = delete_ref(branch_ref_name, repo);
5014 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5017 err = delete_ref(commit_ref_name, repo);
5022 free(tmp_branch_name);
5023 free(new_base_branch_ref_name);
5024 free(branch_ref_name);
5025 free(commit_ref_name);
5029 const struct got_error *
5030 got_worktree_rebase_complete(struct got_worktree *worktree,
5031 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
5032 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
5033 struct got_repository *repo)
5035 const struct got_error *err, *unlockerr;
5036 struct got_object_id *new_head_commit_id = NULL;
5038 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5042 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
5046 err = got_ref_write(rebased_branch, repo);
5050 err = got_worktree_set_head_ref(worktree, rebased_branch);
5054 err = delete_rebase_refs(worktree, repo);
5057 got_fileindex_free(fileindex);
5058 free(new_head_commit_id);
5059 unlockerr = lock_worktree(worktree, LOCK_SH);
5060 if (unlockerr && err == NULL)
5065 const struct got_error *
5066 got_worktree_rebase_abort(struct got_worktree *worktree,
5067 struct got_fileindex *fileindex, struct got_repository *repo,
5068 struct got_reference *new_base_branch,
5069 got_worktree_checkout_cb progress_cb, void *progress_arg)
5071 const struct got_error *err, *unlockerr, *sync_err;
5072 struct got_reference *resolved = NULL;
5073 struct got_object_id *commit_id = NULL;
5074 char *fileindex_path = NULL;
5075 struct revert_file_args rfa;
5076 struct got_object_id *tree_id = NULL;
5078 err = lock_worktree(worktree, LOCK_EX);
5082 err = got_ref_open(&resolved, repo,
5083 got_ref_get_symref_target(new_base_branch), 0);
5087 err = got_worktree_set_head_ref(worktree, resolved);
5092 * XXX commits to the base branch could have happened while
5093 * we were busy rebasing; should we store the original commit ID
5094 * when rebase begins and read it back here?
5096 err = got_ref_resolve(&commit_id, repo, resolved);
5100 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5104 err = got_object_id_by_path(&tree_id, repo,
5105 worktree->base_commit_id, worktree->path_prefix);
5109 err = delete_rebase_refs(worktree, repo);
5113 err = get_fileindex_path(&fileindex_path, worktree);
5117 rfa.worktree = worktree;
5118 rfa.fileindex = fileindex;
5119 rfa.progress_cb = progress_cb;
5120 rfa.progress_arg = progress_arg;
5121 rfa.patch_cb = NULL;
5122 rfa.patch_arg = NULL;
5124 err = worktree_status(worktree, "", fileindex, repo,
5125 revert_file, &rfa, NULL, NULL);
5129 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5130 repo, progress_cb, progress_arg, NULL, NULL);
5132 sync_err = sync_fileindex(fileindex, fileindex_path);
5133 if (sync_err && err == NULL)
5136 got_ref_close(resolved);
5140 got_fileindex_free(fileindex);
5141 free(fileindex_path);
5143 unlockerr = lock_worktree(worktree, LOCK_SH);
5144 if (unlockerr && err == NULL)
5149 const struct got_error *
5150 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
5151 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
5152 struct got_fileindex **fileindex, struct got_worktree *worktree,
5153 struct got_repository *repo)
5155 const struct got_error *err = NULL;
5156 char *tmp_branch_name = NULL;
5157 char *branch_ref_name = NULL;
5158 char *base_commit_ref_name = NULL;
5159 char *fileindex_path = NULL;
5160 struct check_rebase_ok_arg ok_arg;
5161 struct got_reference *wt_branch = NULL;
5162 struct got_reference *base_commit_ref = NULL;
5166 *base_commit_id = NULL;
5169 err = lock_worktree(worktree, LOCK_EX);
5173 err = open_fileindex(fileindex, &fileindex_path, worktree);
5177 ok_arg.worktree = worktree;
5179 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5184 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5188 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5192 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5197 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5202 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
5206 err = got_ref_write(*branch_ref, repo);
5210 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
5211 worktree->base_commit_id);
5214 err = got_ref_write(base_commit_ref, repo);
5217 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
5218 if (*base_commit_id == NULL) {
5219 err = got_error_from_errno("got_object_id_dup");
5223 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5224 worktree->base_commit_id);
5227 err = got_ref_write(*tmp_branch, repo);
5231 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5235 free(fileindex_path);
5236 free(tmp_branch_name);
5237 free(branch_ref_name);
5238 free(base_commit_ref_name);
5240 got_ref_close(wt_branch);
5243 got_ref_close(*branch_ref);
5247 got_ref_close(*tmp_branch);
5250 free(*base_commit_id);
5252 got_fileindex_free(*fileindex);
5255 lock_worktree(worktree, LOCK_SH);
5260 const struct got_error *
5261 got_worktree_histedit_postpone(struct got_worktree *worktree,
5262 struct got_fileindex *fileindex)
5265 got_fileindex_free(fileindex);
5266 return lock_worktree(worktree, LOCK_SH);
5269 const struct got_error *
5270 got_worktree_histedit_in_progress(int *in_progress,
5271 struct got_worktree *worktree)
5273 const struct got_error *err;
5274 char *tmp_branch_name = NULL;
5276 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5280 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5281 free(tmp_branch_name);
5285 const struct got_error *
5286 got_worktree_histedit_continue(struct got_object_id **commit_id,
5287 struct got_reference **tmp_branch, struct got_reference **branch_ref,
5288 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
5289 struct got_worktree *worktree, struct got_repository *repo)
5291 const struct got_error *err;
5292 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
5293 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5294 struct got_reference *commit_ref = NULL;
5295 struct got_reference *base_commit_ref = NULL;
5296 char *fileindex_path = NULL;
5297 int have_staged_files = 0;
5301 *base_commit_id = NULL;
5304 err = lock_worktree(worktree, LOCK_EX);
5308 err = open_fileindex(fileindex, &fileindex_path, worktree);
5312 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5313 &have_staged_files);
5314 if (err && err->code != GOT_ERR_CANCELLED)
5316 if (have_staged_files) {
5317 err = got_error(GOT_ERR_STAGED_PATHS);
5321 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5325 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5329 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5333 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5338 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
5342 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5345 err = got_ref_resolve(commit_id, repo, commit_ref);
5349 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
5352 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
5356 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5360 free(commit_ref_name);
5361 free(branch_ref_name);
5362 free(fileindex_path);
5364 got_ref_close(commit_ref);
5365 if (base_commit_ref)
5366 got_ref_close(base_commit_ref);
5370 free(*base_commit_id);
5371 *base_commit_id = NULL;
5373 got_ref_close(*tmp_branch);
5377 got_fileindex_free(*fileindex);
5380 lock_worktree(worktree, LOCK_EX);
5385 static const struct got_error *
5386 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
5388 const struct got_error *err;
5389 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
5390 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5392 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5395 err = delete_ref(tmp_branch_name, repo);
5399 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5403 err = delete_ref(base_commit_ref_name, repo);
5407 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5410 err = delete_ref(branch_ref_name, repo);
5414 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5417 err = delete_ref(commit_ref_name, repo);
5421 free(tmp_branch_name);
5422 free(base_commit_ref_name);
5423 free(branch_ref_name);
5424 free(commit_ref_name);
5428 const struct got_error *
5429 got_worktree_histedit_abort(struct got_worktree *worktree,
5430 struct got_fileindex *fileindex, struct got_repository *repo,
5431 struct got_reference *branch, struct got_object_id *base_commit_id,
5432 got_worktree_checkout_cb progress_cb, void *progress_arg)
5434 const struct got_error *err, *unlockerr, *sync_err;
5435 struct got_reference *resolved = NULL;
5436 char *fileindex_path = NULL;
5437 struct got_object_id *tree_id = NULL;
5438 struct revert_file_args rfa;
5440 err = lock_worktree(worktree, LOCK_EX);
5444 err = got_ref_open(&resolved, repo,
5445 got_ref_get_symref_target(branch), 0);
5449 err = got_worktree_set_head_ref(worktree, resolved);
5453 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
5457 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
5458 worktree->path_prefix);
5462 err = delete_histedit_refs(worktree, repo);
5466 err = get_fileindex_path(&fileindex_path, worktree);
5470 rfa.worktree = worktree;
5471 rfa.fileindex = fileindex;
5472 rfa.progress_cb = progress_cb;
5473 rfa.progress_arg = progress_arg;
5474 rfa.patch_cb = NULL;
5475 rfa.patch_arg = NULL;
5477 err = worktree_status(worktree, "", fileindex, repo,
5478 revert_file, &rfa, NULL, NULL);
5482 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5483 repo, progress_cb, progress_arg, NULL, NULL);
5485 sync_err = sync_fileindex(fileindex, fileindex_path);
5486 if (sync_err && err == NULL)
5489 got_ref_close(resolved);
5491 free(fileindex_path);
5493 unlockerr = lock_worktree(worktree, LOCK_SH);
5494 if (unlockerr && err == NULL)
5499 const struct got_error *
5500 got_worktree_histedit_complete(struct got_worktree *worktree,
5501 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5502 struct got_reference *edited_branch, struct got_repository *repo)
5504 const struct got_error *err, *unlockerr;
5505 struct got_object_id *new_head_commit_id = NULL;
5506 struct got_reference *resolved = NULL;
5508 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5512 err = got_ref_open(&resolved, repo,
5513 got_ref_get_symref_target(edited_branch), 0);
5517 err = got_ref_change_ref(resolved, new_head_commit_id);
5521 err = got_ref_write(resolved, repo);
5525 err = got_worktree_set_head_ref(worktree, resolved);
5529 err = delete_histedit_refs(worktree, repo);
5532 got_fileindex_free(fileindex);
5533 free(new_head_commit_id);
5534 unlockerr = lock_worktree(worktree, LOCK_SH);
5535 if (unlockerr && err == NULL)
5540 const struct got_error *
5541 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
5542 struct got_object_id *commit_id, struct got_repository *repo)
5544 const struct got_error *err;
5545 char *commit_ref_name;
5547 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5551 err = store_commit_id(commit_ref_name, commit_id, repo);
5555 err = delete_ref(commit_ref_name, repo);
5557 free(commit_ref_name);
5561 struct check_stage_ok_arg {
5562 struct got_object_id *head_commit_id;
5563 struct got_worktree *worktree;
5564 struct got_fileindex *fileindex;
5565 struct got_repository *repo;
5569 const struct got_error *
5570 check_stage_ok(void *arg, unsigned char status,
5571 unsigned char staged_status, const char *relpath,
5572 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5573 struct got_object_id *commit_id)
5575 struct check_stage_ok_arg *a = arg;
5576 const struct got_error *err = NULL;
5577 struct got_fileindex_entry *ie;
5578 struct got_object_id base_commit_id;
5579 struct got_object_id *base_commit_idp = NULL;
5580 char *in_repo_path = NULL, *p;
5582 if (status == GOT_STATUS_UNVERSIONED)
5584 if (status == GOT_STATUS_NONEXISTENT)
5585 return got_error_set_errno(ENOENT, relpath);
5587 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5589 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
5591 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
5592 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5594 return got_error_from_errno("asprintf");
5596 if (got_fileindex_entry_has_commit(ie)) {
5597 memcpy(base_commit_id.sha1, ie->commit_sha1,
5598 SHA1_DIGEST_LENGTH);
5599 base_commit_idp = &base_commit_id;
5602 if (status == GOT_STATUS_NO_CHANGE) {
5603 err = got_error_path(ie->path, GOT_ERR_STAGE_NO_CHANGE);
5605 } else if (status == GOT_STATUS_CONFLICT) {
5606 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
5608 } else if (status != GOT_STATUS_ADD &&
5609 status != GOT_STATUS_MODIFY &&
5610 status != GOT_STATUS_DELETE) {
5611 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
5615 a->have_changes = 1;
5620 err = check_out_of_date(p, status, staged_status,
5621 blob_id, base_commit_idp, a->head_commit_id, a->repo,
5622 GOT_ERR_STAGE_OUT_OF_DATE);
5628 struct stage_path_arg {
5629 struct got_worktree *worktree;
5630 struct got_fileindex *fileindex;
5631 struct got_repository *repo;
5632 got_worktree_status_cb status_cb;
5634 got_worktree_patch_cb patch_cb;
5638 static const struct got_error *
5639 stage_path(void *arg, unsigned char status,
5640 unsigned char staged_status, const char *relpath,
5641 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5642 struct got_object_id *commit_id)
5644 struct stage_path_arg *a = arg;
5645 const struct got_error *err = NULL;
5646 struct got_fileindex_entry *ie;
5647 char *ondisk_path = NULL, *path_content = NULL;
5649 struct got_object_id *new_staged_blob_id = NULL;
5651 if (status == GOT_STATUS_UNVERSIONED)
5654 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5656 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
5658 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
5660 return got_error_from_errno("asprintf");
5663 case GOT_STATUS_ADD:
5664 case GOT_STATUS_MODIFY:
5666 if (status == GOT_STATUS_ADD) {
5667 int choice = GOT_PATCH_CHOICE_NONE;
5668 err = (*a->patch_cb)(&choice, a->patch_arg,
5669 status, ie->path, NULL, 1, 1);
5672 if (choice != GOT_PATCH_CHOICE_YES)
5675 err = create_patched_content(&path_content, 0,
5676 staged_blob_id ? staged_blob_id : blob_id,
5677 ondisk_path, ie->path, a->repo,
5678 a->patch_cb, a->patch_arg);
5679 if (err || path_content == NULL)
5683 err = got_object_blob_create(&new_staged_blob_id,
5684 path_content ? path_content : ondisk_path, a->repo);
5687 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
5688 SHA1_DIGEST_LENGTH);
5689 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
5690 stage = GOT_FILEIDX_STAGE_ADD;
5692 stage = GOT_FILEIDX_STAGE_MODIFY;
5693 got_fileindex_entry_stage_set(ie, stage);
5694 if (a->status_cb == NULL)
5696 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
5697 get_staged_status(ie), relpath, blob_id,
5698 new_staged_blob_id, NULL);
5700 case GOT_STATUS_DELETE:
5701 if (staged_status == GOT_STATUS_DELETE)
5704 int choice = GOT_PATCH_CHOICE_NONE;
5705 err = (*a->patch_cb)(&choice, a->patch_arg, status,
5706 ie->path, NULL, 1, 1);
5709 if (choice == GOT_PATCH_CHOICE_NO)
5711 if (choice != GOT_PATCH_CHOICE_YES) {
5712 err = got_error(GOT_ERR_PATCH_CHOICE);
5716 stage = GOT_FILEIDX_STAGE_DELETE;
5717 got_fileindex_entry_stage_set(ie, stage);
5718 if (a->status_cb == NULL)
5720 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
5721 get_staged_status(ie), relpath, NULL, NULL, NULL);
5723 case GOT_STATUS_NO_CHANGE:
5724 err = got_error_path(relpath, GOT_ERR_STAGE_NO_CHANGE);
5726 case GOT_STATUS_CONFLICT:
5727 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
5729 case GOT_STATUS_NONEXISTENT:
5730 err = got_error_set_errno(ENOENT, relpath);
5733 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
5737 if (path_content && unlink(path_content) == -1 && err == NULL)
5738 err = got_error_from_errno2("unlink", path_content);
5741 free(new_staged_blob_id);
5745 const struct got_error *
5746 got_worktree_stage(struct got_worktree *worktree,
5747 struct got_pathlist_head *paths,
5748 got_worktree_status_cb status_cb, void *status_arg,
5749 got_worktree_patch_cb patch_cb, void *patch_arg,
5750 struct got_repository *repo)
5752 const struct got_error *err = NULL, *sync_err, *unlockerr;
5753 struct got_pathlist_entry *pe;
5754 struct got_fileindex *fileindex = NULL;
5755 char *fileindex_path = NULL;
5756 struct got_reference *head_ref = NULL;
5757 struct got_object_id *head_commit_id = NULL;
5758 struct check_stage_ok_arg oka;
5759 struct stage_path_arg spa;
5761 err = lock_worktree(worktree, LOCK_EX);
5765 err = got_ref_open(&head_ref, repo,
5766 got_worktree_get_head_ref_name(worktree), 0);
5769 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5772 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5776 /* Check pre-conditions before staging anything. */
5777 oka.head_commit_id = head_commit_id;
5778 oka.worktree = worktree;
5779 oka.fileindex = fileindex;
5781 oka.have_changes = 0;
5782 TAILQ_FOREACH(pe, paths, entry) {
5783 err = worktree_status(worktree, pe->path, fileindex, repo,
5784 check_stage_ok, &oka, NULL, NULL);
5788 if (!oka.have_changes) {
5789 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
5793 spa.worktree = worktree;
5794 spa.fileindex = fileindex;
5796 spa.patch_cb = patch_cb;
5797 spa.patch_arg = patch_arg;
5798 spa.status_cb = status_cb;
5799 spa.status_arg = status_arg;
5800 TAILQ_FOREACH(pe, paths, entry) {
5801 err = worktree_status(worktree, pe->path, fileindex, repo,
5802 stage_path, &spa, NULL, NULL);
5807 sync_err = sync_fileindex(fileindex, fileindex_path);
5808 if (sync_err && err == NULL)
5812 got_ref_close(head_ref);
5813 free(head_commit_id);
5814 free(fileindex_path);
5816 got_fileindex_free(fileindex);
5817 unlockerr = lock_worktree(worktree, LOCK_SH);
5818 if (unlockerr && err == NULL)
5823 struct unstage_path_arg {
5824 struct got_worktree *worktree;
5825 struct got_fileindex *fileindex;
5826 struct got_repository *repo;
5827 got_worktree_checkout_cb progress_cb;
5829 got_worktree_patch_cb patch_cb;
5833 static const struct got_error *
5834 create_unstaged_content(char **path_unstaged_content,
5835 char **path_new_staged_content, struct got_object_id *blob_id,
5836 struct got_object_id *staged_blob_id, const char *relpath,
5837 struct got_repository *repo,
5838 got_worktree_patch_cb patch_cb, void *patch_arg)
5840 const struct got_error *err;
5841 struct got_blob_object *blob = NULL, *staged_blob = NULL;
5842 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
5843 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
5844 struct stat sb1, sb2;
5845 struct got_diff_changes *changes = NULL;
5846 struct got_diff_state *ds = NULL;
5847 struct got_diff_args *args = NULL;
5848 struct got_diff_change *change;
5849 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
5850 int have_content = 0, have_rejected_content = 0;
5852 *path_unstaged_content = NULL;
5853 *path_new_staged_content = NULL;
5855 err = got_object_id_str(&label1, blob_id);
5858 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
5862 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
5866 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
5870 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
5874 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
5878 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
5882 if (stat(path1, &sb1) == -1) {
5883 err = got_error_from_errno2("stat", path1);
5887 if (stat(path2, &sb2) == -1) {
5888 err = got_error_from_errno2("stat", path2);
5892 err = got_diff_files(&changes, &ds, &args, &diff_flags,
5893 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
5897 err = got_opentemp_named(path_unstaged_content, &outfile,
5898 "got-unstaged-content");
5901 err = got_opentemp_named(path_new_staged_content, &rejectfile,
5902 "got-new-staged-content");
5906 if (fseek(f1, 0L, SEEK_SET) == -1) {
5907 err = got_ferror(f1, GOT_ERR_IO);
5910 if (fseek(f2, 0L, SEEK_SET) == -1) {
5911 err = got_ferror(f2, GOT_ERR_IO);
5914 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
5916 err = apply_or_reject_change(&choice, change, ++n,
5917 changes->nchanges, ds, args, diff_flags, relpath,
5918 f1, f2, &line_cur1, &line_cur2,
5919 outfile, rejectfile, patch_cb, patch_arg);
5922 if (choice == GOT_PATCH_CHOICE_YES)
5925 have_rejected_content = 1;
5926 if (choice == GOT_PATCH_CHOICE_QUIT)
5929 if (have_content || have_rejected_content)
5930 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
5931 outfile, rejectfile);
5935 got_object_blob_close(blob);
5937 got_object_blob_close(staged_blob);
5938 if (f1 && fclose(f1) == EOF && err == NULL)
5939 err = got_error_from_errno2("fclose", path1);
5940 if (f2 && fclose(f2) == EOF && err == NULL)
5941 err = got_error_from_errno2("fclose", path2);
5942 if (outfile && fclose(outfile) == EOF && err == NULL)
5943 err = got_error_from_errno2("fclose", *path_unstaged_content);
5944 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
5945 err = got_error_from_errno2("fclose", *path_new_staged_content);
5946 if (path1 && unlink(path1) == -1 && err == NULL)
5947 err = got_error_from_errno2("unlink", path1);
5948 if (path2 && unlink(path2) == -1 && err == NULL)
5949 err = got_error_from_errno2("unlink", path2);
5950 if (err || !have_content) {
5951 if (*path_unstaged_content &&
5952 unlink(*path_unstaged_content) == -1 && err == NULL)
5953 err = got_error_from_errno2("unlink",
5954 *path_unstaged_content);
5955 free(*path_unstaged_content);
5956 *path_unstaged_content = NULL;
5958 if (err || !have_rejected_content) {
5959 if (*path_new_staged_content &&
5960 unlink(*path_new_staged_content) == -1 && err == NULL)
5961 err = got_error_from_errno2("unlink",
5962 *path_new_staged_content);
5963 free(*path_new_staged_content);
5964 *path_new_staged_content = NULL;
5968 got_diff_state_free(ds);
5972 got_diff_free_changes(changes);
5978 static const struct got_error *
5979 unstage_path(void *arg, unsigned char status,
5980 unsigned char staged_status, const char *relpath,
5981 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5982 struct got_object_id *commit_id)
5984 const struct got_error *err = NULL;
5985 struct unstage_path_arg *a = arg;
5986 struct got_fileindex_entry *ie;
5987 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
5988 char *ondisk_path = NULL, *path_unstaged_content = NULL;
5989 char *path_new_staged_content = NULL;
5990 int local_changes_subsumed;
5993 if (staged_status != GOT_STATUS_ADD &&
5994 staged_status != GOT_STATUS_MODIFY &&
5995 staged_status != GOT_STATUS_DELETE)
5998 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6000 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6002 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
6004 return got_error_from_errno("asprintf");
6006 switch (staged_status) {
6007 case GOT_STATUS_MODIFY:
6008 err = got_object_open_as_blob(&blob_base, a->repo,
6013 case GOT_STATUS_ADD:
6015 if (staged_status == GOT_STATUS_ADD) {
6016 int choice = GOT_PATCH_CHOICE_NONE;
6017 err = (*a->patch_cb)(&choice, a->patch_arg,
6018 staged_status, ie->path, NULL, 1, 1);
6021 if (choice != GOT_PATCH_CHOICE_YES)
6024 err = create_unstaged_content(
6025 &path_unstaged_content,
6026 &path_new_staged_content, blob_id,
6027 staged_blob_id, ie->path, a->repo,
6028 a->patch_cb, a->patch_arg);
6029 if (err || path_unstaged_content == NULL)
6031 if (path_new_staged_content) {
6032 err = got_object_blob_create(
6034 path_new_staged_content,
6038 memcpy(ie->staged_blob_sha1,
6039 staged_blob_id->sha1,
6040 SHA1_DIGEST_LENGTH);
6042 err = merge_file(&local_changes_subsumed,
6043 a->worktree, blob_base, ondisk_path,
6044 relpath, got_fileindex_perms_to_st(ie),
6045 path_unstaged_content, "unstaged",
6046 a->repo, a->progress_cb, a->progress_arg);
6048 path_new_staged_content == NULL)
6049 got_fileindex_entry_stage_set(ie,
6050 GOT_FILEIDX_STAGE_NONE);
6051 break; /* Done with this file. */
6054 err = got_object_open_as_blob(&blob_staged, a->repo,
6055 staged_blob_id, 8192);
6058 err = merge_blob(&local_changes_subsumed, a->worktree,
6059 blob_base, ondisk_path, relpath,
6060 got_fileindex_perms_to_st(ie), blob_staged,
6061 commit_id ? commit_id : a->worktree->base_commit_id,
6062 a->repo, a->progress_cb, a->progress_arg);
6064 got_fileindex_entry_stage_set(ie,
6065 GOT_FILEIDX_STAGE_NONE);
6067 case GOT_STATUS_DELETE:
6069 int choice = GOT_PATCH_CHOICE_NONE;
6070 err = (*a->patch_cb)(&choice, a->patch_arg,
6071 staged_status, ie->path, NULL, 1, 1);
6074 if (choice == GOT_PATCH_CHOICE_NO)
6076 if (choice != GOT_PATCH_CHOICE_YES) {
6077 err = got_error(GOT_ERR_PATCH_CHOICE);
6081 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
6082 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
6085 err = (*a->progress_cb)(a->progress_arg, status, relpath);
6090 if (path_unstaged_content &&
6091 unlink(path_unstaged_content) == -1 && err == NULL)
6092 err = got_error_from_errno2("unlink", path_unstaged_content);
6093 if (path_new_staged_content &&
6094 unlink(path_new_staged_content) == -1 && err == NULL)
6095 err = got_error_from_errno2("unlink", path_new_staged_content);
6096 free(path_unstaged_content);
6097 free(path_new_staged_content);
6099 got_object_blob_close(blob_base);
6101 got_object_blob_close(blob_staged);
6105 const struct got_error *
6106 got_worktree_unstage(struct got_worktree *worktree,
6107 struct got_pathlist_head *paths,
6108 got_worktree_checkout_cb progress_cb, void *progress_arg,
6109 got_worktree_patch_cb patch_cb, void *patch_arg,
6110 struct got_repository *repo)
6112 const struct got_error *err = NULL, *sync_err, *unlockerr;
6113 struct got_pathlist_entry *pe;
6114 struct got_fileindex *fileindex = NULL;
6115 char *fileindex_path = NULL;
6116 struct unstage_path_arg upa;
6118 err = lock_worktree(worktree, LOCK_EX);
6122 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6126 upa.worktree = worktree;
6127 upa.fileindex = fileindex;
6129 upa.progress_cb = progress_cb;
6130 upa.progress_arg = progress_arg;
6131 upa.patch_cb = patch_cb;
6132 upa.patch_arg = patch_arg;
6133 TAILQ_FOREACH(pe, paths, entry) {
6134 err = worktree_status(worktree, pe->path, fileindex, repo,
6135 unstage_path, &upa, NULL, NULL);
6140 sync_err = sync_fileindex(fileindex, fileindex_path);
6141 if (sync_err && err == NULL)
6144 free(fileindex_path);
6146 got_fileindex_free(fileindex);
6147 unlockerr = lock_worktree(worktree, LOCK_SH);
6148 if (unlockerr && err == NULL)