2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/queue.h>
38 #include "got_error.h"
39 #include "got_repository.h"
40 #include "got_reference.h"
41 #include "got_object.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
45 #include "got_opentemp.h"
48 #include "got_lib_worktree.h"
49 #include "got_lib_sha1.h"
50 #include "got_lib_fileindex.h"
51 #include "got_lib_inflate.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_object_idset.h"
57 #include "got_lib_diff.h"
60 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
63 #define GOT_MERGE_LABEL_MERGED "merged change"
64 #define GOT_MERGE_LABEL_BASE "3-way merge base"
66 static const struct got_error *
67 create_meta_file(const char *path_got, const char *name, const char *content)
69 const struct got_error *err = NULL;
72 if (asprintf(&path, "%s/%s", path_got, name) == -1)
73 return got_error_from_errno("asprintf");
75 err = got_path_create_file(path, content);
80 static const struct got_error *
81 update_meta_file(const char *path_got, const char *name, const char *content)
83 const struct got_error *err = NULL;
88 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
89 err = got_error_from_errno("asprintf");
94 err = got_opentemp_named(&tmppath, &tmpfile, path);
99 int len = fprintf(tmpfile, "%s\n", content);
100 if (len != strlen(content) + 1) {
101 err = got_error_from_errno2("fprintf", tmppath);
106 if (rename(tmppath, path) != 0) {
107 err = got_error_from_errno3("rename", tmppath, path);
113 if (fclose(tmpfile) != 0 && err == NULL)
114 err = got_error_from_errno2("fclose", tmppath);
119 static const struct got_error *
120 read_meta_file(char **content, const char *path_got, const char *name)
122 const struct got_error *err = NULL;
130 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
131 err = got_error_from_errno("asprintf");
136 fd = open(path, O_RDONLY | O_NOFOLLOW);
139 err = got_error_path(path, GOT_ERR_WORKTREE_META);
141 err = got_error_from_errno2("open", path);
144 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
145 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
146 : got_error_from_errno2("flock", path));
150 if (fstat(fd, &sb) != 0) {
151 err = got_error_from_errno2("fstat", path);
154 *content = calloc(1, sb.st_size);
155 if (*content == NULL) {
156 err = got_error_from_errno("calloc");
160 n = read(fd, *content, sb.st_size);
161 if (n != sb.st_size) {
162 err = (n == -1 ? got_error_from_errno2("read", path) :
163 got_error_path(path, GOT_ERR_WORKTREE_META));
166 if ((*content)[sb.st_size - 1] != '\n') {
167 err = got_error_path(path, GOT_ERR_WORKTREE_META);
170 (*content)[sb.st_size - 1] = '\0';
173 if (fd != -1 && close(fd) == -1 && err == NULL)
174 err = got_error_from_errno2("close", path_got);
183 static const struct got_error *
184 write_head_ref(const char *path_got, struct got_reference *head_ref)
186 const struct got_error *err = NULL;
189 if (got_ref_is_symbolic(head_ref)) {
190 refstr = got_ref_to_str(head_ref);
192 return got_error_from_errno("got_ref_to_str");
194 refstr = strdup(got_ref_get_name(head_ref));
196 return got_error_from_errno("strdup");
198 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
203 const struct got_error *
204 got_worktree_init(const char *path, struct got_reference *head_ref,
205 const char *prefix, struct got_repository *repo)
207 const struct got_error *err = NULL;
208 struct got_object_id *commit_id = NULL;
210 uint32_t uuid_status;
212 char *path_got = NULL;
213 char *formatstr = NULL;
214 char *absprefix = NULL;
215 char *basestr = NULL;
216 char *uuidstr = NULL;
218 if (strcmp(path, got_repo_get_path(repo)) == 0) {
219 err = got_error(GOT_ERR_WORKTREE_REPO);
223 err = got_ref_resolve(&commit_id, repo, head_ref);
226 err = got_object_get_type(&obj_type, repo, commit_id);
229 if (obj_type != GOT_OBJ_TYPE_COMMIT)
230 return got_error(GOT_ERR_OBJ_TYPE);
232 if (!got_path_is_absolute(prefix)) {
233 if (asprintf(&absprefix, "/%s", prefix) == -1)
234 return got_error_from_errno("asprintf");
237 /* Create top-level directory (may already exist). */
238 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
239 err = got_error_from_errno2("mkdir", path);
243 /* Create .got directory (may already exist). */
244 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
245 err = got_error_from_errno("asprintf");
248 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
249 err = got_error_from_errno2("mkdir", path_got);
253 /* Create an empty lock file. */
254 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
258 /* Create an empty file index. */
259 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
263 /* Write the HEAD reference. */
264 err = write_head_ref(path_got, head_ref);
268 /* Record our base commit. */
269 err = got_object_id_str(&basestr, commit_id);
272 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
276 /* Store path to repository. */
277 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
278 got_repo_get_path(repo));
282 /* Store in-repository path prefix. */
283 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
284 absprefix ? absprefix : prefix);
289 uuid_create(&uuid, &uuid_status);
290 if (uuid_status != uuid_s_ok) {
291 err = got_error_uuid(uuid_status, "uuid_create");
294 uuid_to_string(&uuid, &uuidstr, &uuid_status);
295 if (uuid_status != uuid_s_ok) {
296 err = got_error_uuid(uuid_status, "uuid_to_string");
299 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
303 /* Stamp work tree with format file. */
304 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
305 err = got_error_from_errno("asprintf");
308 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
322 static const struct got_error *
323 open_worktree(struct got_worktree **worktree, const char *path)
325 const struct got_error *err = NULL;
327 char *formatstr = NULL;
328 char *uuidstr = NULL;
329 char *path_lock = NULL;
330 char *base_commit_id_str = NULL;
331 int version, fd = -1;
333 struct got_repository *repo = NULL;
334 uint32_t uuid_status;
338 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
339 err = got_error_from_errno("asprintf");
344 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
345 err = got_error_from_errno("asprintf");
350 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
352 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
353 : got_error_from_errno2("open", path_lock));
357 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
361 version = strtonum(formatstr, 1, INT_MAX, &errstr);
363 err = got_error_msg(GOT_ERR_WORKTREE_META,
364 "could not parse work tree format version number");
367 if (version != GOT_WORKTREE_FORMAT_VERSION) {
368 err = got_error(GOT_ERR_WORKTREE_VERS);
372 *worktree = calloc(1, sizeof(**worktree));
373 if (*worktree == NULL) {
374 err = got_error_from_errno("calloc");
377 (*worktree)->lockfd = -1;
379 (*worktree)->root_path = strdup(path);
380 if ((*worktree)->root_path == NULL) {
381 err = got_error_from_errno("strdup");
384 err = read_meta_file(&(*worktree)->repo_path, path_got,
385 GOT_WORKTREE_REPOSITORY);
389 err = read_meta_file(&(*worktree)->path_prefix, path_got,
390 GOT_WORKTREE_PATH_PREFIX);
394 err = read_meta_file(&base_commit_id_str, path_got,
395 GOT_WORKTREE_BASE_COMMIT);
399 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
402 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
403 if (uuid_status != uuid_s_ok) {
404 err = got_error_uuid(uuid_status, "uuid_from_string");
408 err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
412 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
417 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
418 GOT_WORKTREE_HEAD_REF);
421 got_repo_close(repo);
424 free(base_commit_id_str);
430 if (*worktree != NULL)
431 got_worktree_close(*worktree);
434 (*worktree)->lockfd = fd;
439 const struct got_error *
440 got_worktree_open(struct got_worktree **worktree, const char *path)
442 const struct got_error *err = NULL;
445 err = open_worktree(worktree, path);
446 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
450 path = dirname(path);
452 return got_error_from_errno2("dirname", path);
453 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
455 return got_error(GOT_ERR_NOT_WORKTREE);
458 const struct got_error *
459 got_worktree_close(struct got_worktree *worktree)
461 const struct got_error *err = NULL;
462 free(worktree->repo_path);
463 free(worktree->path_prefix);
464 free(worktree->base_commit_id);
465 free(worktree->head_ref_name);
466 if (worktree->lockfd != -1)
467 if (close(worktree->lockfd) != 0)
468 err = got_error_from_errno2("close",
469 got_worktree_get_root_path(worktree));
470 free(worktree->root_path);
476 got_worktree_get_root_path(struct got_worktree *worktree)
478 return worktree->root_path;
482 got_worktree_get_repo_path(struct got_worktree *worktree)
484 return worktree->repo_path;
488 got_worktree_get_path_prefix(struct got_worktree *worktree)
490 return worktree->path_prefix;
493 const struct got_error *
494 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
495 const char *path_prefix)
497 char *absprefix = NULL;
499 if (!got_path_is_absolute(path_prefix)) {
500 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
501 return got_error_from_errno("asprintf");
503 *match = (strcmp(absprefix ? absprefix : path_prefix,
504 worktree->path_prefix) == 0);
510 got_worktree_get_head_ref_name(struct got_worktree *worktree)
512 return worktree->head_ref_name;
515 const struct got_error *
516 got_worktree_set_head_ref(struct got_worktree *worktree,
517 struct got_reference *head_ref)
519 const struct got_error *err = NULL;
520 char *path_got = NULL, *head_ref_name = NULL;
522 if (asprintf(&path_got, "%s/%s", worktree->root_path,
523 GOT_WORKTREE_GOT_DIR) == -1) {
524 err = got_error_from_errno("asprintf");
529 head_ref_name = strdup(got_ref_get_name(head_ref));
530 if (head_ref_name == NULL) {
531 err = got_error_from_errno("strdup");
535 err = write_head_ref(path_got, head_ref);
539 free(worktree->head_ref_name);
540 worktree->head_ref_name = head_ref_name;
548 struct got_object_id *
549 got_worktree_get_base_commit_id(struct got_worktree *worktree)
551 return worktree->base_commit_id;
554 const struct got_error *
555 got_worktree_set_base_commit_id(struct got_worktree *worktree,
556 struct got_repository *repo, struct got_object_id *commit_id)
558 const struct got_error *err;
559 struct got_object *obj = NULL;
561 char *path_got = NULL;
563 if (asprintf(&path_got, "%s/%s", worktree->root_path,
564 GOT_WORKTREE_GOT_DIR) == -1) {
565 err = got_error_from_errno("asprintf");
570 err = got_object_open(&obj, repo, commit_id);
574 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
575 err = got_error(GOT_ERR_OBJ_TYPE);
579 /* Record our base commit. */
580 err = got_object_id_str(&id_str, commit_id);
583 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
587 free(worktree->base_commit_id);
588 worktree->base_commit_id = got_object_id_dup(commit_id);
589 if (worktree->base_commit_id == NULL) {
590 err = got_error_from_errno("got_object_id_dup");
595 got_object_close(obj);
601 static const struct got_error *
602 lock_worktree(struct got_worktree *worktree, int operation)
604 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
605 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
606 : got_error_from_errno2("flock",
607 got_worktree_get_root_path(worktree)));
611 static const struct got_error *
612 add_dir_on_disk(struct got_worktree *worktree, const char *path)
614 const struct got_error *err = NULL;
617 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
618 return got_error_from_errno("asprintf");
620 err = got_path_mkdir(abspath);
621 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
624 if (lstat(abspath, &sb) == -1) {
625 err = got_error_from_errno2("lstat", abspath);
626 } else if (!S_ISDIR(sb.st_mode)) {
627 /* TODO directory is obstructed; do something */
628 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
635 static const struct got_error *
636 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
638 const struct got_error *err = NULL;
641 size_t flen1 = 0, flen2 = 0;
646 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
647 if (flen1 == 0 && ferror(f1)) {
648 err = got_error_from_errno("fread");
651 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
652 if (flen2 == 0 && ferror(f2)) {
653 err = got_error_from_errno("fread");
660 } else if (flen2 == 0) {
664 } else if (flen1 == flen2) {
665 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
678 static const struct got_error *
679 check_files_equal(int *same, const char *f1_path, const char *f2_path)
681 const struct got_error *err = NULL;
684 FILE *f1 = NULL, *f2 = NULL;
688 if (lstat(f1_path, &sb) != 0) {
689 err = got_error_from_errno2("lstat", f1_path);
694 if (lstat(f2_path, &sb) != 0) {
695 err = got_error_from_errno2("lstat", f2_path);
700 if (size1 != size2) {
705 f1 = fopen(f1_path, "r");
707 return got_error_from_errno2("fopen", f1_path);
709 f2 = fopen(f2_path, "r");
711 err = got_error_from_errno2("fopen", f2_path);
715 err = check_file_contents_equal(same, f1, f2);
717 if (f1 && fclose(f1) != 0 && err == NULL)
718 err = got_error_from_errno("fclose");
719 if (f2 && fclose(f2) != 0 && err == NULL)
720 err = got_error_from_errno("fclose");
726 * Perform a 3-way merge where blob_orig acts as the common ancestor,
727 * the file at deriv_path acts as the first derived version, and the
728 * file on disk acts as the second derived version.
730 static const struct got_error *
731 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
732 struct got_blob_object *blob_orig, const char *ondisk_path,
733 const char *path, uint16_t st_mode, const char *deriv_path,
734 const char *label_orig, const char *label_deriv,
735 struct got_repository *repo,
736 got_worktree_checkout_cb progress_cb, void *progress_arg)
738 const struct got_error *err = NULL;
741 char *blob_orig_path = NULL;
742 char *merged_path = NULL, *base_path = NULL;
746 *local_changes_subsumed = 0;
748 parent = dirname(ondisk_path);
750 return got_error_from_errno2("dirname", ondisk_path);
752 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
753 return got_error_from_errno("asprintf");
755 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
760 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
761 err = got_error_from_errno("asprintf");
766 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
770 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
776 * If the file has no blob, this is an "add vs add" conflict,
777 * and we simply use an empty ancestor file to make both files
778 * appear in the merged result in their entirety.
782 err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
783 blob_orig_path, ondisk_path, label_deriv, label_orig, NULL);
787 err = (*progress_cb)(progress_arg,
788 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
792 if (fsync(merged_fd) != 0) {
793 err = got_error_from_errno("fsync");
797 /* Check if a clean merge has subsumed all local changes. */
798 if (overlapcnt == 0) {
799 err = check_files_equal(local_changes_subsumed, deriv_path,
805 if (fchmod(merged_fd, st_mode) != 0) {
806 err = got_error_from_errno2("fchmod", merged_path);
810 if (rename(merged_path, ondisk_path) != 0) {
811 err = got_error_from_errno3("rename", merged_path,
820 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
821 err = got_error_from_errno("close");
822 if (f_orig && fclose(f_orig) != 0 && err == NULL)
823 err = got_error_from_errno("fclose");
826 if (blob_orig_path) {
827 unlink(blob_orig_path);
828 free(blob_orig_path);
834 * Perform a 3-way merge where blob_orig acts as the common ancestor,
835 * blob_deriv acts as the first derived version, and the file on disk
836 * acts as the second derived version.
838 static const struct got_error *
839 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
840 struct got_blob_object *blob_orig, const char *ondisk_path,
841 const char *path, uint16_t st_mode, const char *label_orig,
842 struct got_blob_object *blob_deriv,
843 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
844 got_worktree_checkout_cb progress_cb, void *progress_arg)
846 const struct got_error *err = NULL;
847 FILE *f_deriv = NULL;
848 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
849 char *label_deriv = NULL, *parent;
851 *local_changes_subsumed = 0;
853 parent = dirname(ondisk_path);
855 return got_error_from_errno2("dirname", ondisk_path);
858 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
859 err = got_error_from_errno("asprintf");
864 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
867 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
872 err = got_object_id_str(&id_str, deriv_base_commit_id);
875 if (asprintf(&label_deriv, "%s: commit %s",
876 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
877 err = got_error_from_errno("asprintf");
881 err = merge_file(local_changes_subsumed, worktree, blob_orig,
882 ondisk_path, path, st_mode, blob_deriv_path, label_orig,
883 label_deriv, repo, progress_cb, progress_arg);
885 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
886 err = got_error_from_errno("fclose");
888 if (blob_deriv_path) {
889 unlink(blob_deriv_path);
890 free(blob_deriv_path);
897 static const struct got_error *
898 create_fileindex_entry(struct got_fileindex *fileindex,
899 struct got_object_id *base_commit_id, const char *ondisk_path,
900 const char *path, struct got_object_id *blob_id)
902 const struct got_error *err = NULL;
903 struct got_fileindex_entry *new_ie;
905 err = got_fileindex_entry_alloc(&new_ie, path);
909 err = got_fileindex_entry_update(new_ie, ondisk_path,
910 blob_id->sha1, base_commit_id->sha1, 1);
914 err = got_fileindex_entry_add(fileindex, new_ie);
917 got_fileindex_entry_free(new_ie);
922 get_ondisk_perms(int executable, mode_t st_mode)
924 mode_t xbits = S_IXUSR;
927 /* Map read bits to execute bits. */
928 if (st_mode & S_IRGRP)
930 if (st_mode & S_IROTH)
932 return st_mode | xbits;
935 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
938 /* forward declaration */
939 static const struct got_error *
940 install_blob(struct got_worktree *worktree, const char *ondisk_path,
941 const char *path, mode_t te_mode, mode_t st_mode,
942 struct got_blob_object *blob, int restoring_missing_file,
943 int reverting_versioned_file, struct got_repository *repo,
944 got_worktree_checkout_cb progress_cb, void *progress_arg);
946 static const struct got_error *
947 install_symlink(struct got_worktree *worktree, const char *ondisk_path,
948 const char *path, mode_t te_mode, mode_t st_mode,
949 struct got_blob_object *blob, int restoring_missing_file,
950 int reverting_versioned_file, struct got_repository *repo,
951 got_worktree_checkout_cb progress_cb, void *progress_arg)
953 const struct got_error *err = NULL;
954 char target_path[PATH_MAX];
955 size_t len, target_len = 0;
956 char *resolved_path = NULL, *abspath = NULL;
957 const uint8_t *buf = got_object_blob_get_read_buf(blob);
958 size_t hdrlen = got_object_blob_get_hdrlen(blob);
961 * Blob object content specifies the target path of the link.
962 * If a symbolic link cannot be installed we instead create
963 * a regular file which contains the link target path stored
964 * in the blob object.
967 err = got_object_blob_read_block(&len, blob);
968 if (len + target_len >= sizeof(target_path)) {
969 /* Path too long; install as a regular file. */
970 got_object_blob_rewind(blob);
971 return install_blob(worktree, ondisk_path, path,
972 GOT_DEFAULT_FILE_MODE, st_mode, blob,
973 restoring_missing_file, reverting_versioned_file,
974 repo, progress_cb, progress_arg);
977 /* Skip blob object header first time around. */
978 memcpy(target_path + target_len, buf + hdrlen,
980 target_len += len - hdrlen;
984 target_path[target_len] = '\0';
987 * Relative symlink target lookup should begin at the directory
988 * in which the blob object is being installed.
990 if (!got_path_is_absolute(target_path)) {
991 char *parent = dirname(ondisk_path);
992 if (parent == NULL) {
993 err = got_error_from_errno2("dirname", ondisk_path);
996 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
997 err = got_error_from_errno("asprintf");
1003 * unveil(2) restricts our view of paths in the filesystem.
1004 * ENOENT will occur if a link target path does not exist or
1005 * if it points outside our unveiled path space.
1007 resolved_path = realpath(abspath ? abspath : target_path, NULL);
1008 if (resolved_path == NULL) {
1009 if (errno != ENOENT)
1010 return got_error_from_errno2("realpath", target_path);
1013 /* Only allow symlinks pointing at paths within the work tree. */
1014 if (!got_path_is_child(resolved_path ? resolved_path : (abspath ?
1015 abspath : target_path), worktree->root_path,
1016 strlen(worktree->root_path))) {
1017 /* install as a regular file */
1018 got_object_blob_rewind(blob);
1019 err = install_blob(worktree, ondisk_path, path,
1020 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1021 restoring_missing_file, reverting_versioned_file,
1022 repo, progress_cb, progress_arg);
1026 if (symlink(target_path, ondisk_path) == -1) {
1027 if (errno == EEXIST) {
1030 char etarget[PATH_MAX];
1031 if (lstat(ondisk_path, &sb) == -1) {
1032 err = got_error_from_errno2("lstat",
1036 if (!S_ISLNK(sb.st_mode)) {
1037 err = got_error_path(ondisk_path,
1038 GOT_ERR_FILE_OBSTRUCTED);
1041 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1043 err = got_error_from_errno2("readlink",
1047 if (elen == target_len &&
1048 memcmp(etarget, target_path, target_len) == 0) {
1049 err = NULL; /* nothing to do */
1052 if (unlink(ondisk_path) == -1) {
1053 err = got_error_from_errno2("unlink",
1057 if (symlink(target_path, ondisk_path) == -1) {
1058 err = got_error_from_errno3("symlink",
1059 target_path, ondisk_path);
1063 err = (*progress_cb)(progress_arg,
1064 GOT_STATUS_UPDATE, path);
1069 if (errno == ENOENT) {
1070 char *parent = dirname(ondisk_path);
1071 if (parent == NULL) {
1072 err = got_error_from_errno2("dirname",
1076 err = add_dir_on_disk(worktree, parent);
1080 * Retry, and fall through to error handling
1081 * below if this second attempt fails.
1083 if (symlink(target_path, ondisk_path) != -1) {
1084 err = NULL; /* success */
1089 /* Handle errors from first or second creation attempt. */
1090 if (errno == ENAMETOOLONG) {
1091 /* bad target path; install as a regular file */
1092 got_object_blob_rewind(blob);
1093 err = install_blob(worktree, ondisk_path, path,
1094 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1095 restoring_missing_file, reverting_versioned_file,
1096 repo, progress_cb, progress_arg);
1097 } else if (errno == ENOTDIR) {
1098 err = got_error_path(ondisk_path,
1099 GOT_ERR_FILE_OBSTRUCTED);
1101 err = got_error_from_errno3("symlink",
1102 target_path, ondisk_path);
1105 err = (*progress_cb)(progress_arg, GOT_STATUS_ADD, path);
1107 free(resolved_path);
1112 static const struct got_error *
1113 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1114 const char *path, mode_t te_mode, mode_t st_mode,
1115 struct got_blob_object *blob, int restoring_missing_file,
1116 int reverting_versioned_file, struct got_repository *repo,
1117 got_worktree_checkout_cb progress_cb, void *progress_arg)
1119 const struct got_error *err = NULL;
1123 char *tmppath = NULL;
1125 if (S_ISLNK(te_mode))
1126 return install_symlink(worktree, ondisk_path, path, te_mode,
1127 st_mode, blob, restoring_missing_file,
1128 reverting_versioned_file, repo, progress_cb, progress_arg);
1130 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1131 GOT_DEFAULT_FILE_MODE);
1133 if (errno == ENOENT) {
1134 char *parent = dirname(path);
1136 return got_error_from_errno2("dirname", path);
1137 err = add_dir_on_disk(worktree, parent);
1140 fd = open(ondisk_path,
1141 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1142 GOT_DEFAULT_FILE_MODE);
1144 return got_error_from_errno2("open",
1146 } else if (errno == EEXIST) {
1147 if (!S_ISREG(st_mode)) {
1148 /* TODO file is obstructed; do something */
1149 err = got_error_path(ondisk_path,
1150 GOT_ERR_FILE_OBSTRUCTED);
1153 err = got_opentemp_named_fd(&tmppath, &fd,
1160 return got_error_from_errno2("open", ondisk_path);
1163 if (restoring_missing_file)
1164 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
1165 else if (reverting_versioned_file)
1166 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
1168 err = (*progress_cb)(progress_arg,
1169 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1173 hdrlen = got_object_blob_get_hdrlen(blob);
1175 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1176 err = got_object_blob_read_block(&len, blob);
1180 /* Skip blob object header first time around. */
1181 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1183 err = got_error_from_errno("write");
1185 } else if (outlen != len - hdrlen) {
1186 err = got_error(GOT_ERR_IO);
1193 if (fsync(fd) != 0) {
1194 err = got_error_from_errno("fsync");
1199 if (rename(tmppath, ondisk_path) != 0) {
1200 err = got_error_from_errno3("rename", tmppath,
1207 if (chmod(ondisk_path,
1208 get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1209 err = got_error_from_errno2("chmod", ondisk_path);
1214 if (fd != -1 && close(fd) != 0 && err == NULL)
1215 err = got_error_from_errno("close");
1220 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1221 static const struct got_error *
1222 get_modified_file_content_status(unsigned char *status, FILE *f)
1224 const struct got_error *err = NULL;
1225 const char *markers[3] = {
1226 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1227 GOT_DIFF_CONFLICT_MARKER_SEP,
1228 GOT_DIFF_CONFLICT_MARKER_END
1233 const char delim[3] = {'\0', '\0', '\0'};
1235 while (*status == GOT_STATUS_MODIFY) {
1236 line = fparseln(f, &len, NULL, delim, 0);
1240 err = got_ferror(f, GOT_ERR_IO);
1244 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1245 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1247 *status = GOT_STATUS_CONFLICT;
1257 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1259 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1260 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1264 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1266 return !(ie->ctime_sec == sb->st_ctime &&
1267 ie->ctime_nsec == sb->st_ctimensec &&
1268 ie->mtime_sec == sb->st_mtime &&
1269 ie->mtime_nsec == sb->st_mtimensec &&
1270 ie->size == (sb->st_size & 0xffffffff) &&
1271 !xbit_differs(ie, sb->st_mode));
1274 static unsigned char
1275 get_staged_status(struct got_fileindex_entry *ie)
1277 switch (got_fileindex_entry_stage_get(ie)) {
1278 case GOT_FILEIDX_STAGE_ADD:
1279 return GOT_STATUS_ADD;
1280 case GOT_FILEIDX_STAGE_DELETE:
1281 return GOT_STATUS_DELETE;
1282 case GOT_FILEIDX_STAGE_MODIFY:
1283 return GOT_STATUS_MODIFY;
1285 return GOT_STATUS_NO_CHANGE;
1289 static const struct got_error *
1290 get_symlink_status(unsigned char *status, struct stat *sb,
1291 struct got_fileindex_entry *ie, const char *abspath,
1292 int dirfd, const char *de_name, struct got_blob_object *blob)
1294 const struct got_error *err = NULL;
1295 char target_path[PATH_MAX];
1296 char etarget[PATH_MAX];
1298 size_t len, target_len = 0;
1299 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1300 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1302 *status = GOT_STATUS_NO_CHANGE;
1304 /* Blob object content specifies the target path of the link. */
1306 err = got_object_blob_read_block(&len, blob);
1309 if (len + target_len >= sizeof(target_path)) {
1311 * Should not happen. The blob contents were OK
1312 * when this symlink was installed.
1314 return got_error(GOT_ERR_NO_SPACE);
1317 /* Skip blob object header first time around. */
1318 memcpy(target_path + target_len, buf + hdrlen,
1320 target_len += len - hdrlen;
1324 target_path[target_len] = '\0';
1327 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1329 return got_error_from_errno2("readlinkat", abspath);
1331 elen = readlink(abspath, etarget, sizeof(etarget));
1333 return got_error_from_errno2("readlinkat", abspath);
1336 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1337 *status = GOT_STATUS_MODIFY;
1342 static const struct got_error *
1343 get_file_status(unsigned char *status, struct stat *sb,
1344 struct got_fileindex_entry *ie, const char *abspath,
1345 int dirfd, const char *de_name, struct got_repository *repo)
1347 const struct got_error *err = NULL;
1348 struct got_object_id id;
1353 struct got_blob_object *blob = NULL;
1355 unsigned char staged_status = get_staged_status(ie);
1357 *status = GOT_STATUS_NO_CHANGE;
1360 * Whenever the caller provides a directory descriptor and a
1361 * directory entry name for the file, use them! This prevents
1362 * race conditions if filesystem paths change beneath our feet.
1365 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1366 if (errno == ENOENT) {
1367 if (got_fileindex_entry_has_file_on_disk(ie))
1368 *status = GOT_STATUS_MISSING;
1370 *status = GOT_STATUS_DELETE;
1373 err = got_error_from_errno2("fstatat", abspath);
1377 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1378 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1379 return got_error_from_errno2("open", abspath);
1380 else if (fd == -1 && errno == ELOOP) {
1381 if (lstat(abspath, sb) == -1)
1382 return got_error_from_errno2("lstat", abspath);
1383 } else if (fd == -1 || fstat(fd, sb) == -1) {
1384 if (errno == ENOENT) {
1385 if (got_fileindex_entry_has_file_on_disk(ie))
1386 *status = GOT_STATUS_MISSING;
1388 *status = GOT_STATUS_DELETE;
1391 err = got_error_from_errno2("fstat", abspath);
1396 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1397 *status = GOT_STATUS_OBSTRUCTED;
1401 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1402 *status = GOT_STATUS_DELETE;
1404 } else if (!got_fileindex_entry_has_blob(ie) &&
1405 staged_status != GOT_STATUS_ADD) {
1406 *status = GOT_STATUS_ADD;
1410 if (!stat_info_differs(ie, sb))
1413 if (staged_status == GOT_STATUS_MODIFY ||
1414 staged_status == GOT_STATUS_ADD)
1415 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1417 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1419 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1423 if (S_ISLNK(sb->st_mode)) {
1424 /* Staging changes to symlinks is not yet(?) supported. */
1425 if (staged_status != GOT_STATUS_NO_CHANGE) {
1426 err = got_error_path(abspath, GOT_ERR_FILE_STATUS);
1429 err = get_symlink_status(status, sb, ie, abspath, dirfd,
1436 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1438 err = got_error_from_errno2("openat", abspath);
1443 f = fdopen(fd, "r");
1445 err = got_error_from_errno2("fdopen", abspath);
1449 hdrlen = got_object_blob_get_hdrlen(blob);
1451 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1452 err = got_object_blob_read_block(&blen, blob);
1455 /* Skip length of blob object header first time around. */
1456 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1457 if (flen == 0 && ferror(f)) {
1458 err = got_error_from_errno("fread");
1463 *status = GOT_STATUS_MODIFY;
1465 } else if (flen == 0) {
1467 *status = GOT_STATUS_MODIFY;
1469 } else if (blen - hdrlen == flen) {
1470 /* Skip blob object header first time around. */
1471 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1472 *status = GOT_STATUS_MODIFY;
1476 *status = GOT_STATUS_MODIFY;
1482 if (*status == GOT_STATUS_MODIFY) {
1484 err = get_modified_file_content_status(status, f);
1485 } else if (xbit_differs(ie, sb->st_mode))
1486 *status = GOT_STATUS_MODE_CHANGE;
1489 got_object_blob_close(blob);
1490 if (f != NULL && fclose(f) == EOF && err == NULL)
1491 err = got_error_from_errno2("fclose", abspath);
1492 if (fd != -1 && close(fd) == -1 && err == NULL)
1493 err = got_error_from_errno2("close", abspath);
1498 * Update timestamps in the file index if a file is unmodified and
1499 * we had to run a full content comparison to find out.
1501 static const struct got_error *
1502 sync_timestamps(char *ondisk_path, unsigned char status,
1503 struct got_fileindex_entry *ie, struct stat *sb)
1505 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1506 return got_fileindex_entry_update(ie, ondisk_path,
1507 ie->blob_sha1, ie->commit_sha1, 1);
1512 static const struct got_error *
1513 update_blob(struct got_worktree *worktree,
1514 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1515 struct got_tree_entry *te, const char *path,
1516 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1519 const struct got_error *err = NULL;
1520 struct got_blob_object *blob = NULL;
1522 unsigned char status = GOT_STATUS_NO_CHANGE;
1525 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1526 return got_error_from_errno("asprintf");
1529 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1530 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1533 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1537 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1538 sb.st_mode = got_fileindex_perms_to_st(ie);
1540 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1542 if (status == GOT_STATUS_OBSTRUCTED) {
1543 err = (*progress_cb)(progress_arg, status, path);
1546 if (status == GOT_STATUS_CONFLICT) {
1547 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1552 if (ie && status != GOT_STATUS_MISSING &&
1553 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1554 if (got_fileindex_entry_has_commit(ie) &&
1555 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1556 SHA1_DIGEST_LENGTH) == 0) {
1557 err = sync_timestamps(ondisk_path, status, ie, &sb);
1560 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1564 if (got_fileindex_entry_has_blob(ie) &&
1565 memcmp(ie->blob_sha1, te->id.sha1,
1566 SHA1_DIGEST_LENGTH) == 0) {
1567 err = sync_timestamps(ondisk_path, status, ie, &sb);
1572 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1576 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1577 int update_timestamps;
1578 struct got_blob_object *blob2 = NULL;
1579 char *label_orig = NULL;
1580 if (got_fileindex_entry_has_blob(ie)) {
1581 struct got_object_id id2;
1582 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1583 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1587 if (got_fileindex_entry_has_commit(ie)) {
1588 char id_str[SHA1_DIGEST_STRING_LENGTH];
1589 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1590 sizeof(id_str)) == NULL) {
1591 err = got_error_path(id_str,
1592 GOT_ERR_BAD_OBJ_ID_STR);
1595 if (asprintf(&label_orig, "%s: commit %s",
1596 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1597 err = got_error_from_errno("asprintf");
1601 err = merge_blob(&update_timestamps, worktree, blob2,
1602 ondisk_path, path, sb.st_mode, label_orig, blob,
1603 worktree->base_commit_id, repo,
1604 progress_cb, progress_arg);
1607 got_object_blob_close(blob2);
1611 * Do not update timestamps of files with local changes.
1612 * Otherwise, a future status walk would treat them as
1613 * unmodified files again.
1615 err = got_fileindex_entry_update(ie, ondisk_path,
1616 blob->id.sha1, worktree->base_commit_id->sha1,
1618 } else if (status == GOT_STATUS_MODE_CHANGE) {
1619 err = got_fileindex_entry_update(ie, ondisk_path,
1620 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1621 } else if (status == GOT_STATUS_DELETE) {
1622 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1625 err = got_fileindex_entry_update(ie, ondisk_path,
1626 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1630 err = install_blob(worktree, ondisk_path, path, te->mode,
1631 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1632 repo, progress_cb, progress_arg);
1636 err = got_fileindex_entry_update(ie, ondisk_path,
1637 blob->id.sha1, worktree->base_commit_id->sha1, 1);
1639 err = create_fileindex_entry(fileindex,
1640 worktree->base_commit_id, ondisk_path, path,
1646 got_object_blob_close(blob);
1652 static const struct got_error *
1653 remove_ondisk_file(const char *root_path, const char *path)
1655 const struct got_error *err = NULL;
1656 char *ondisk_path = NULL;
1658 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1659 return got_error_from_errno("asprintf");
1661 if (unlink(ondisk_path) == -1) {
1662 if (errno != ENOENT)
1663 err = got_error_from_errno2("unlink", ondisk_path);
1665 char *parent = dirname(ondisk_path);
1666 while (parent && strcmp(parent, root_path) != 0) {
1667 if (rmdir(parent) == -1) {
1668 if (errno != ENOTEMPTY)
1669 err = got_error_from_errno2("rmdir",
1673 parent = dirname(parent);
1680 static const struct got_error *
1681 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1682 struct got_fileindex_entry *ie, struct got_repository *repo,
1683 got_worktree_checkout_cb progress_cb, void *progress_arg)
1685 const struct got_error *err = NULL;
1686 unsigned char status;
1690 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1691 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1693 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1695 return got_error_from_errno("asprintf");
1697 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
1701 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1702 status == GOT_STATUS_ADD) {
1703 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1707 * Preserve the working file and change the deleted blob's
1708 * entry into a schedule-add entry.
1710 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1713 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1716 if (status == GOT_STATUS_NO_CHANGE) {
1717 err = remove_ondisk_file(worktree->root_path, ie->path);
1721 got_fileindex_entry_remove(fileindex, ie);
1728 struct diff_cb_arg {
1729 struct got_fileindex *fileindex;
1730 struct got_worktree *worktree;
1731 struct got_repository *repo;
1732 got_worktree_checkout_cb progress_cb;
1734 got_cancel_cb cancel_cb;
1738 static const struct got_error *
1739 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1740 struct got_tree_entry *te, const char *parent_path)
1742 struct diff_cb_arg *a = arg;
1744 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1745 return got_error(GOT_ERR_CANCELLED);
1747 return update_blob(a->worktree, a->fileindex, ie, te,
1748 ie->path, a->repo, a->progress_cb, a->progress_arg);
1751 static const struct got_error *
1752 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1754 struct diff_cb_arg *a = arg;
1756 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1757 return got_error(GOT_ERR_CANCELLED);
1759 return delete_blob(a->worktree, a->fileindex, ie,
1760 a->repo, a->progress_cb, a->progress_arg);
1763 static const struct got_error *
1764 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1766 struct diff_cb_arg *a = arg;
1767 const struct got_error *err;
1770 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1771 return got_error(GOT_ERR_CANCELLED);
1773 if (got_object_tree_entry_is_submodule(te))
1776 if (asprintf(&path, "%s%s%s", parent_path,
1777 parent_path[0] ? "/" : "", te->name)
1779 return got_error_from_errno("asprintf");
1781 if (S_ISDIR(te->mode))
1782 err = add_dir_on_disk(a->worktree, path);
1784 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1785 a->repo, a->progress_cb, a->progress_arg);
1791 static const struct got_error *
1792 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1794 const struct got_error *err = NULL;
1795 char *uuidstr = NULL;
1796 uint32_t uuid_status;
1800 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1801 if (uuid_status != uuid_s_ok)
1802 return got_error_uuid(uuid_status, "uuid_to_string");
1804 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1806 err = got_error_from_errno("asprintf");
1813 const struct got_error *
1814 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1816 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1819 static const struct got_error *
1820 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1822 return get_ref_name(refname, worktree,
1823 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1826 static const struct got_error *
1827 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1829 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1832 static const struct got_error *
1833 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1835 return get_ref_name(refname, worktree,
1836 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1839 static const struct got_error *
1840 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1842 return get_ref_name(refname, worktree,
1843 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1846 static const struct got_error *
1847 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1849 return get_ref_name(refname, worktree,
1850 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1853 static const struct got_error *
1854 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1856 return get_ref_name(refname, worktree,
1857 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1860 static const struct got_error *
1861 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1863 return get_ref_name(refname, worktree,
1864 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1867 static const struct got_error *
1868 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1870 return get_ref_name(refname, worktree,
1871 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1874 const struct got_error *
1875 got_worktree_get_histedit_script_path(char **path,
1876 struct got_worktree *worktree)
1878 if (asprintf(path, "%s/%s/%s", worktree->root_path,
1879 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
1881 return got_error_from_errno("asprintf");
1887 * Prevent Git's garbage collector from deleting our base commit by
1888 * setting a reference to our base commit's ID.
1890 static const struct got_error *
1891 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1893 const struct got_error *err = NULL;
1894 struct got_reference *ref = NULL;
1897 err = got_worktree_get_base_ref_name(&refname, worktree);
1901 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1905 err = got_ref_write(ref, repo);
1913 static const struct got_error *
1914 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
1916 const struct got_error *err = NULL;
1918 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1919 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1920 err = got_error_from_errno("asprintf");
1921 *fileindex_path = NULL;
1927 static const struct got_error *
1928 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1929 struct got_worktree *worktree)
1931 const struct got_error *err = NULL;
1934 *fileindex_path = NULL;
1935 *fileindex = got_fileindex_alloc();
1936 if (*fileindex == NULL)
1937 return got_error_from_errno("got_fileindex_alloc");
1939 err = get_fileindex_path(fileindex_path, worktree);
1943 index = fopen(*fileindex_path, "rb");
1944 if (index == NULL) {
1945 if (errno != ENOENT)
1946 err = got_error_from_errno2("fopen", *fileindex_path);
1948 err = got_fileindex_read(*fileindex, index);
1949 if (fclose(index) != 0 && err == NULL)
1950 err = got_error_from_errno("fclose");
1954 free(*fileindex_path);
1955 *fileindex_path = NULL;
1956 got_fileindex_free(*fileindex);
1962 struct bump_base_commit_id_arg {
1963 struct got_object_id *base_commit_id;
1966 const char *entry_name;
1967 got_worktree_checkout_cb progress_cb;
1971 /* Bump base commit ID of all files within an updated part of the work tree. */
1972 static const struct got_error *
1973 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1975 const struct got_error *err;
1976 struct bump_base_commit_id_arg *a = arg;
1978 if (a->entry_name) {
1979 if (strcmp(ie->path, a->path) != 0)
1981 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1984 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1985 SHA1_DIGEST_LENGTH) == 0)
1988 if (a->progress_cb) {
1989 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1994 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1998 static const struct got_error *
1999 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2001 const struct got_error *err = NULL;
2002 char *new_fileindex_path = NULL;
2003 FILE *new_index = NULL;
2004 struct timespec timeout;
2006 err = got_opentemp_named(&new_fileindex_path, &new_index,
2011 err = got_fileindex_write(fileindex, new_index);
2015 if (rename(new_fileindex_path, fileindex_path) != 0) {
2016 err = got_error_from_errno3("rename", new_fileindex_path,
2018 unlink(new_fileindex_path);
2022 * Sleep for a short amount of time to ensure that files modified after
2023 * this program exits have a different time stamp from the one which
2024 * was recorded in the file index.
2027 timeout.tv_nsec = 1;
2028 nanosleep(&timeout, NULL);
2032 free(new_fileindex_path);
2036 static const struct got_error *
2037 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2038 struct got_object_id **tree_id, const char *wt_relpath,
2039 struct got_worktree *worktree, struct got_repository *repo)
2041 const struct got_error *err = NULL;
2042 struct got_object_id *id = NULL;
2043 char *in_repo_path = NULL;
2044 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2046 *entry_type = GOT_OBJ_TYPE_ANY;
2047 *tree_relpath = NULL;
2050 if (wt_relpath[0] == '\0') {
2051 /* Check out all files within the work tree. */
2052 *entry_type = GOT_OBJ_TYPE_TREE;
2053 *tree_relpath = strdup("");
2054 if (*tree_relpath == NULL) {
2055 err = got_error_from_errno("strdup");
2058 err = got_object_id_by_path(tree_id, repo,
2059 worktree->base_commit_id, worktree->path_prefix);
2065 /* Check out a subset of files in the work tree. */
2067 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2068 is_root_wt ? "" : "/", wt_relpath) == -1) {
2069 err = got_error_from_errno("asprintf");
2073 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2079 in_repo_path = NULL;
2081 err = got_object_get_type(entry_type, repo, id);
2085 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2086 /* Check out a single file. */
2087 if (strchr(wt_relpath, '/') == NULL) {
2088 /* Check out a single file in work tree's root dir. */
2089 in_repo_path = strdup(worktree->path_prefix);
2090 if (in_repo_path == NULL) {
2091 err = got_error_from_errno("strdup");
2094 *tree_relpath = strdup("");
2095 if (*tree_relpath == NULL) {
2096 err = got_error_from_errno("strdup");
2100 /* Check out a single file in a subdirectory. */
2101 err = got_path_dirname(tree_relpath, wt_relpath);
2104 if (asprintf(&in_repo_path, "%s%s%s",
2105 worktree->path_prefix, is_root_wt ? "" : "/",
2106 *tree_relpath) == -1) {
2107 err = got_error_from_errno("asprintf");
2111 err = got_object_id_by_path(tree_id, repo,
2112 worktree->base_commit_id, in_repo_path);
2114 /* Check out all files within a subdirectory. */
2115 *tree_id = got_object_id_dup(id);
2116 if (*tree_id == NULL) {
2117 err = got_error_from_errno("got_object_id_dup");
2120 *tree_relpath = strdup(wt_relpath);
2121 if (*tree_relpath == NULL) {
2122 err = got_error_from_errno("strdup");
2130 *entry_type = GOT_OBJ_TYPE_ANY;
2131 free(*tree_relpath);
2132 *tree_relpath = NULL;
2139 static const struct got_error *
2140 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2141 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2142 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2143 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2145 const struct got_error *err = NULL;
2146 struct got_commit_object *commit = NULL;
2147 struct got_tree_object *tree = NULL;
2148 struct got_fileindex_diff_tree_cb diff_cb;
2149 struct diff_cb_arg arg;
2151 err = ref_base_commit(worktree, repo);
2153 if (!(err->code == GOT_ERR_ERRNO &&
2154 (errno == EACCES || errno == EROFS)))
2156 err = (*progress_cb)(progress_arg,
2157 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2162 err = got_object_open_as_commit(&commit, repo,
2163 worktree->base_commit_id);
2167 err = got_object_open_as_tree(&tree, repo, tree_id);
2172 got_object_tree_find_entry(tree, entry_name) == NULL) {
2173 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2177 diff_cb.diff_old_new = diff_old_new;
2178 diff_cb.diff_old = diff_old;
2179 diff_cb.diff_new = diff_new;
2180 arg.fileindex = fileindex;
2181 arg.worktree = worktree;
2183 arg.progress_cb = progress_cb;
2184 arg.progress_arg = progress_arg;
2185 arg.cancel_cb = cancel_cb;
2186 arg.cancel_arg = cancel_arg;
2187 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2188 entry_name, repo, &diff_cb, &arg);
2191 got_object_tree_close(tree);
2193 got_object_commit_close(commit);
2197 const struct got_error *
2198 got_worktree_checkout_files(struct got_worktree *worktree,
2199 struct got_pathlist_head *paths, struct got_repository *repo,
2200 got_worktree_checkout_cb progress_cb, void *progress_arg,
2201 got_cancel_cb cancel_cb, void *cancel_arg)
2203 const struct got_error *err = NULL, *sync_err, *unlockerr;
2204 struct got_commit_object *commit = NULL;
2205 struct got_tree_object *tree = NULL;
2206 struct got_fileindex *fileindex = NULL;
2207 char *fileindex_path = NULL;
2208 struct got_pathlist_entry *pe;
2209 struct tree_path_data {
2210 SIMPLEQ_ENTRY(tree_path_data) entry;
2211 struct got_object_id *tree_id;
2216 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2218 SIMPLEQ_INIT(&tree_paths);
2220 err = lock_worktree(worktree, LOCK_EX);
2224 /* Map all specified paths to in-repository trees. */
2225 TAILQ_FOREACH(pe, paths, entry) {
2226 tpd = malloc(sizeof(*tpd));
2228 err = got_error_from_errno("malloc");
2232 err = find_tree_entry_for_checkout(&tpd->entry_type,
2233 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2239 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2240 err = got_path_basename(&tpd->entry_name, pe->path);
2248 tpd->entry_name = NULL;
2250 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2254 * Read the file index.
2255 * Checking out files is supposed to be an idempotent operation.
2256 * If the on-disk file index is incomplete we will try to complete it.
2258 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2262 tpd = SIMPLEQ_FIRST(&tree_paths);
2263 TAILQ_FOREACH(pe, paths, entry) {
2264 struct bump_base_commit_id_arg bbc_arg;
2266 err = checkout_files(worktree, fileindex, tpd->relpath,
2267 tpd->tree_id, tpd->entry_name, repo,
2268 progress_cb, progress_arg, cancel_cb, cancel_arg);
2272 bbc_arg.base_commit_id = worktree->base_commit_id;
2273 bbc_arg.entry_name = tpd->entry_name;
2274 bbc_arg.path = pe->path;
2275 bbc_arg.path_len = pe->path_len;
2276 bbc_arg.progress_cb = progress_cb;
2277 bbc_arg.progress_arg = progress_arg;
2278 err = got_fileindex_for_each_entry_safe(fileindex,
2279 bump_base_commit_id, &bbc_arg);
2283 tpd = SIMPLEQ_NEXT(tpd, entry);
2285 sync_err = sync_fileindex(fileindex, fileindex_path);
2286 if (sync_err && err == NULL)
2289 free(fileindex_path);
2291 got_object_tree_close(tree);
2293 got_object_commit_close(commit);
2295 got_fileindex_free(fileindex);
2296 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2297 tpd = SIMPLEQ_FIRST(&tree_paths);
2298 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2303 unlockerr = lock_worktree(worktree, LOCK_SH);
2304 if (unlockerr && err == NULL)
2309 struct merge_file_cb_arg {
2310 struct got_worktree *worktree;
2311 struct got_fileindex *fileindex;
2312 got_worktree_checkout_cb progress_cb;
2314 got_cancel_cb cancel_cb;
2316 const char *label_orig;
2317 struct got_object_id *commit_id2;
2320 static const struct got_error *
2321 merge_file_cb(void *arg, struct got_blob_object *blob1,
2322 struct got_blob_object *blob2, struct got_object_id *id1,
2323 struct got_object_id *id2, const char *path1, const char *path2,
2324 mode_t mode1, mode_t mode2, struct got_repository *repo)
2326 static const struct got_error *err = NULL;
2327 struct merge_file_cb_arg *a = arg;
2328 struct got_fileindex_entry *ie;
2329 char *ondisk_path = NULL;
2331 unsigned char status;
2332 int local_changes_subsumed;
2334 if (blob1 && blob2) {
2335 ie = got_fileindex_entry_get(a->fileindex, path2,
2338 return (*a->progress_cb)(a->progress_arg,
2339 GOT_STATUS_MISSING, path2);
2341 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2343 return got_error_from_errno("asprintf");
2345 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2350 if (status == GOT_STATUS_DELETE) {
2351 err = (*a->progress_cb)(a->progress_arg,
2352 GOT_STATUS_MERGE, path2);
2355 if (status != GOT_STATUS_NO_CHANGE &&
2356 status != GOT_STATUS_MODIFY &&
2357 status != GOT_STATUS_CONFLICT &&
2358 status != GOT_STATUS_ADD) {
2359 err = (*a->progress_cb)(a->progress_arg, status, path2);
2363 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
2364 ondisk_path, path2, sb.st_mode, a->label_orig, blob2,
2365 a->commit_id2, repo, a->progress_cb, a->progress_arg);
2367 ie = got_fileindex_entry_get(a->fileindex, path1,
2370 return (*a->progress_cb)(a->progress_arg,
2371 GOT_STATUS_MISSING, path1);
2373 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2375 return got_error_from_errno("asprintf");
2377 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2383 case GOT_STATUS_NO_CHANGE:
2384 err = (*a->progress_cb)(a->progress_arg,
2385 GOT_STATUS_DELETE, path1);
2388 err = remove_ondisk_file(a->worktree->root_path, path1);
2392 got_fileindex_entry_mark_deleted_from_disk(ie);
2394 case GOT_STATUS_DELETE:
2395 case GOT_STATUS_MISSING:
2396 err = (*a->progress_cb)(a->progress_arg,
2397 GOT_STATUS_DELETE, path1);
2401 got_fileindex_entry_mark_deleted_from_disk(ie);
2403 case GOT_STATUS_ADD:
2404 case GOT_STATUS_MODIFY:
2405 case GOT_STATUS_CONFLICT:
2406 err = (*a->progress_cb)(a->progress_arg,
2407 GOT_STATUS_CANNOT_DELETE, path1);
2411 case GOT_STATUS_OBSTRUCTED:
2412 err = (*a->progress_cb)(a->progress_arg, status, path1);
2420 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2422 return got_error_from_errno("asprintf");
2423 ie = got_fileindex_entry_get(a->fileindex, path2,
2426 err = get_file_status(&status, &sb, ie, ondisk_path,
2430 if (status != GOT_STATUS_NO_CHANGE &&
2431 status != GOT_STATUS_MODIFY &&
2432 status != GOT_STATUS_CONFLICT &&
2433 status != GOT_STATUS_ADD) {
2434 err = (*a->progress_cb)(a->progress_arg,
2438 err = merge_blob(&local_changes_subsumed, a->worktree,
2439 NULL, ondisk_path, path2, sb.st_mode,
2440 a->label_orig, blob2, a->commit_id2, repo,
2443 if (status == GOT_STATUS_DELETE) {
2444 err = got_fileindex_entry_update(ie,
2445 ondisk_path, blob2->id.sha1,
2446 a->worktree->base_commit_id->sha1, 0);
2451 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2452 err = install_blob(a->worktree, ondisk_path, path2,
2453 /* XXX get this from parent tree! */
2454 GOT_DEFAULT_FILE_MODE,
2455 sb.st_mode, blob2, 0, 0, repo,
2456 a->progress_cb, a->progress_arg);
2459 err = got_fileindex_entry_alloc(&ie, path2);
2462 err = got_fileindex_entry_update(ie, ondisk_path,
2465 got_fileindex_entry_free(ie);
2468 err = got_fileindex_entry_add(a->fileindex, ie);
2470 got_fileindex_entry_free(ie);
2480 struct check_merge_ok_arg {
2481 struct got_worktree *worktree;
2482 struct got_repository *repo;
2485 static const struct got_error *
2486 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2488 const struct got_error *err = NULL;
2489 struct check_merge_ok_arg *a = arg;
2490 unsigned char status;
2494 /* Reject merges into a work tree with mixed base commits. */
2495 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2496 SHA1_DIGEST_LENGTH))
2497 return got_error(GOT_ERR_MIXED_COMMITS);
2499 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2501 return got_error_from_errno("asprintf");
2503 /* Reject merges into a work tree with conflicted files. */
2504 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2507 if (status == GOT_STATUS_CONFLICT)
2508 return got_error(GOT_ERR_CONFLICTS);
2513 static const struct got_error *
2514 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2515 const char *fileindex_path, struct got_object_id *commit_id1,
2516 struct got_object_id *commit_id2, struct got_repository *repo,
2517 got_worktree_checkout_cb progress_cb, void *progress_arg,
2518 got_cancel_cb cancel_cb, void *cancel_arg)
2520 const struct got_error *err = NULL, *sync_err;
2521 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2522 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2523 struct merge_file_cb_arg arg;
2524 char *label_orig = NULL;
2529 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2530 worktree->path_prefix);
2534 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2538 err = got_object_id_str(&id_str, commit_id1);
2542 if (asprintf(&label_orig, "%s: commit %s",
2543 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2544 err = got_error_from_errno("asprintf");
2551 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2552 worktree->path_prefix);
2556 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2560 arg.worktree = worktree;
2561 arg.fileindex = fileindex;
2562 arg.progress_cb = progress_cb;
2563 arg.progress_arg = progress_arg;
2564 arg.cancel_cb = cancel_cb;
2565 arg.cancel_arg = cancel_arg;
2566 arg.label_orig = label_orig;
2567 arg.commit_id2 = commit_id2;
2568 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2569 sync_err = sync_fileindex(fileindex, fileindex_path);
2570 if (sync_err && err == NULL)
2574 got_object_tree_close(tree1);
2576 got_object_tree_close(tree2);
2581 const struct got_error *
2582 got_worktree_merge_files(struct got_worktree *worktree,
2583 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2584 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2585 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2587 const struct got_error *err, *unlockerr;
2588 char *fileindex_path = NULL;
2589 struct got_fileindex *fileindex = NULL;
2590 struct check_merge_ok_arg mok_arg;
2592 err = lock_worktree(worktree, LOCK_EX);
2596 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2600 mok_arg.worktree = worktree;
2601 mok_arg.repo = repo;
2602 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2607 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2608 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2611 got_fileindex_free(fileindex);
2612 free(fileindex_path);
2613 unlockerr = lock_worktree(worktree, LOCK_SH);
2614 if (unlockerr && err == NULL)
2619 struct diff_dir_cb_arg {
2620 struct got_fileindex *fileindex;
2621 struct got_worktree *worktree;
2622 const char *status_path;
2623 size_t status_path_len;
2624 struct got_repository *repo;
2625 got_worktree_status_cb status_cb;
2627 got_cancel_cb cancel_cb;
2629 /* A pathlist containing per-directory pathlists of ignore patterns. */
2630 struct got_pathlist_head ignores;
2631 int report_unchanged;
2635 static const struct got_error *
2636 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2637 int dirfd, const char *de_name,
2638 got_worktree_status_cb status_cb, void *status_arg,
2639 struct got_repository *repo, int report_unchanged)
2641 const struct got_error *err = NULL;
2642 unsigned char status = GOT_STATUS_NO_CHANGE;
2643 unsigned char staged_status = get_staged_status(ie);
2645 struct got_object_id blob_id, commit_id, staged_blob_id;
2646 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2647 struct got_object_id *staged_blob_idp = NULL;
2649 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
2653 if (status == GOT_STATUS_NO_CHANGE &&
2654 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
2657 if (got_fileindex_entry_has_blob(ie)) {
2658 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2659 blob_idp = &blob_id;
2661 if (got_fileindex_entry_has_commit(ie)) {
2662 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2663 commit_idp = &commit_id;
2665 if (staged_status == GOT_STATUS_ADD ||
2666 staged_status == GOT_STATUS_MODIFY) {
2667 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2668 SHA1_DIGEST_LENGTH);
2669 staged_blob_idp = &staged_blob_id;
2672 return (*status_cb)(status_arg, status, staged_status,
2673 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
2676 static const struct got_error *
2677 status_old_new(void *arg, struct got_fileindex_entry *ie,
2678 struct dirent *de, const char *parent_path, int dirfd)
2680 const struct got_error *err = NULL;
2681 struct diff_dir_cb_arg *a = arg;
2684 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2685 return got_error(GOT_ERR_CANCELLED);
2687 if (got_path_cmp(parent_path, a->status_path,
2688 strlen(parent_path), a->status_path_len) != 0 &&
2689 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2692 if (parent_path[0]) {
2693 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2694 parent_path, de->d_name) == -1)
2695 return got_error_from_errno("asprintf");
2697 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2699 return got_error_from_errno("asprintf");
2702 err = report_file_status(ie, abspath, dirfd, de->d_name,
2703 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
2708 static const struct got_error *
2709 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2711 struct diff_dir_cb_arg *a = arg;
2712 struct got_object_id blob_id, commit_id;
2713 unsigned char status;
2715 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2716 return got_error(GOT_ERR_CANCELLED);
2718 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2721 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2722 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2723 if (got_fileindex_entry_has_file_on_disk(ie))
2724 status = GOT_STATUS_MISSING;
2726 status = GOT_STATUS_DELETE;
2727 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2728 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
2732 free_ignorelist(struct got_pathlist_head *ignorelist)
2734 struct got_pathlist_entry *pe;
2736 TAILQ_FOREACH(pe, ignorelist, entry)
2737 free((char *)pe->path);
2738 got_pathlist_free(ignorelist);
2742 free_ignores(struct got_pathlist_head *ignores)
2744 struct got_pathlist_entry *pe;
2746 TAILQ_FOREACH(pe, ignores, entry) {
2747 struct got_pathlist_head *ignorelist = pe->data;
2748 free_ignorelist(ignorelist);
2749 free((char *)pe->path);
2751 got_pathlist_free(ignores);
2754 static const struct got_error *
2755 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
2757 const struct got_error *err = NULL;
2758 struct got_pathlist_entry *pe = NULL;
2759 struct got_pathlist_head *ignorelist;
2760 char *line = NULL, *pattern, *dirpath = NULL;
2761 size_t linesize = 0;
2764 ignorelist = calloc(1, sizeof(*ignorelist));
2765 if (ignorelist == NULL)
2766 return got_error_from_errno("calloc");
2767 TAILQ_INIT(ignorelist);
2769 while ((linelen = getline(&line, &linesize, f)) != -1) {
2770 if (linelen > 0 && line[linelen - 1] == '\n')
2771 line[linelen - 1] = '\0';
2773 /* Git's ignores may contain comments. */
2777 /* Git's negated patterns are not (yet?) supported. */
2781 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
2783 err = got_error_from_errno("asprintf");
2786 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
2791 err = got_error_from_errno("getline");
2795 dirpath = strdup(path);
2796 if (dirpath == NULL) {
2797 err = got_error_from_errno("strdup");
2800 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
2803 if (err || pe == NULL) {
2805 free_ignorelist(ignorelist);
2811 match_ignores(struct got_pathlist_head *ignores, const char *path)
2813 struct got_pathlist_entry *pe;
2815 /* Handle patterns which match in all directories. */
2816 TAILQ_FOREACH(pe, ignores, entry) {
2817 struct got_pathlist_head *ignorelist = pe->data;
2818 struct got_pathlist_entry *pi;
2820 TAILQ_FOREACH(pi, ignorelist, entry) {
2821 const char *p, *pattern = pi->path;
2823 if (strncmp(pattern, "**/", 3) != 0)
2828 if (fnmatch(pattern, p,
2829 FNM_PATHNAME | FNM_LEADING_DIR)) {
2830 /* Retry in next directory. */
2831 while (*p && *p != '/')
2843 * The ignores pathlist contains ignore lists from children before
2844 * parents, so we can find the most specific ignorelist by walking
2845 * ignores backwards.
2847 pe = TAILQ_LAST(ignores, got_pathlist_head);
2849 if (got_path_is_child(path, pe->path, pe->path_len)) {
2850 struct got_pathlist_head *ignorelist = pe->data;
2851 struct got_pathlist_entry *pi;
2852 TAILQ_FOREACH(pi, ignorelist, entry) {
2853 const char *pattern = pi->path;
2854 int flags = FNM_LEADING_DIR;
2855 if (strstr(pattern, "/**/") == NULL)
2856 flags |= FNM_PATHNAME;
2857 if (fnmatch(pattern, path, flags))
2862 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
2868 static const struct got_error *
2869 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
2870 const char *path, int dirfd, const char *ignores_filename)
2872 const struct got_error *err = NULL;
2875 FILE *ignoresfile = NULL;
2877 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
2878 path[0] ? "/" : "", ignores_filename) == -1)
2879 return got_error_from_errno("asprintf");
2882 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
2884 if (errno != ENOENT && errno != EACCES)
2885 err = got_error_from_errno2("openat",
2888 ignoresfile = fdopen(fd, "r");
2889 if (ignoresfile == NULL)
2890 err = got_error_from_errno2("fdopen",
2894 err = read_ignores(ignores, path, ignoresfile);
2898 ignoresfile = fopen(ignorespath, "r");
2899 if (ignoresfile == NULL) {
2900 if (errno != ENOENT && errno != EACCES)
2901 err = got_error_from_errno2("fopen",
2904 err = read_ignores(ignores, path, ignoresfile);
2907 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
2908 err = got_error_from_errno2("fclose", path);
2909 if (fd != -1 && close(fd) == -1 && err == NULL)
2910 err = got_error_from_errno2("close", path);
2915 static const struct got_error *
2916 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
2918 const struct got_error *err = NULL;
2919 struct diff_dir_cb_arg *a = arg;
2922 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2923 return got_error(GOT_ERR_CANCELLED);
2925 if (parent_path[0]) {
2926 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2927 return got_error_from_errno("asprintf");
2932 if (de->d_type != DT_DIR &&
2933 got_path_is_child(path, a->status_path, a->status_path_len)
2934 && !match_ignores(&a->ignores, path))
2935 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2936 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2942 static const struct got_error *
2943 status_traverse(void *arg, const char *path, int dirfd)
2945 const struct got_error *err = NULL;
2946 struct diff_dir_cb_arg *a = arg;
2951 err = add_ignores(&a->ignores, a->worktree->root_path,
2952 path, dirfd, ".cvsignore");
2956 err = add_ignores(&a->ignores, a->worktree->root_path, path,
2957 dirfd, ".gitignore");
2962 static const struct got_error *
2963 report_single_file_status(const char *path, const char *ondisk_path,
2964 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2965 void *status_arg, struct got_repository *repo, int report_unchanged)
2967 struct got_fileindex_entry *ie;
2970 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
2972 return report_file_status(ie, ondisk_path, -1, NULL,
2973 status_cb, status_arg, repo, report_unchanged);
2975 if (lstat(ondisk_path, &sb) == -1) {
2976 if (errno != ENOENT)
2977 return got_error_from_errno2("lstat", ondisk_path);
2978 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
2979 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2983 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
2984 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
2985 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2990 static const struct got_error *
2991 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
2992 const char *root_path, const char *path)
2994 const struct got_error *err;
2995 char *parent_path, *next_parent_path = NULL;
2997 err = add_ignores(ignores, root_path, "", -1,
3002 err = add_ignores(ignores, root_path, "", -1,
3007 err = got_path_dirname(&parent_path, path);
3009 if (err->code == GOT_ERR_BAD_PATH)
3010 return NULL; /* cannot traverse parent */
3014 err = add_ignores(ignores, root_path, parent_path, -1,
3018 err = add_ignores(ignores, root_path, parent_path, -1,
3022 err = got_path_dirname(&next_parent_path, parent_path);
3024 if (err->code == GOT_ERR_BAD_PATH)
3025 err = NULL; /* traversed everything */
3029 parent_path = next_parent_path;
3030 next_parent_path = NULL;
3034 free(next_parent_path);
3038 static const struct got_error *
3039 worktree_status(struct got_worktree *worktree, const char *path,
3040 struct got_fileindex *fileindex, struct got_repository *repo,
3041 got_worktree_status_cb status_cb, void *status_arg,
3042 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3043 int report_unchanged)
3045 const struct got_error *err = NULL;
3047 struct got_fileindex_diff_dir_cb fdiff_cb;
3048 struct diff_dir_cb_arg arg;
3049 char *ondisk_path = NULL;
3051 TAILQ_INIT(&arg.ignores);
3053 if (asprintf(&ondisk_path, "%s%s%s",
3054 worktree->root_path, path[0] ? "/" : "", path) == -1)
3055 return got_error_from_errno("asprintf");
3057 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3059 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3061 err = got_error_from_errno2("open", ondisk_path);
3063 err = report_single_file_status(path, ondisk_path,
3064 fileindex, status_cb, status_arg, repo,
3067 fdiff_cb.diff_old_new = status_old_new;
3068 fdiff_cb.diff_old = status_old;
3069 fdiff_cb.diff_new = status_new;
3070 fdiff_cb.diff_traverse = status_traverse;
3071 arg.fileindex = fileindex;
3072 arg.worktree = worktree;
3073 arg.status_path = path;
3074 arg.status_path_len = strlen(path);
3076 arg.status_cb = status_cb;
3077 arg.status_arg = status_arg;
3078 arg.cancel_cb = cancel_cb;
3079 arg.cancel_arg = cancel_arg;
3080 arg.report_unchanged = report_unchanged;
3081 arg.no_ignores = no_ignores;
3083 err = add_ignores_from_parent_paths(&arg.ignores,
3084 worktree->root_path, path);
3088 err = got_fileindex_diff_dir(fileindex, fd,
3089 worktree->root_path, path, repo, &fdiff_cb, &arg);
3092 free_ignores(&arg.ignores);
3093 if (fd != -1 && close(fd) != 0 && err == NULL)
3094 err = got_error_from_errno("close");
3099 const struct got_error *
3100 got_worktree_status(struct got_worktree *worktree,
3101 struct got_pathlist_head *paths, struct got_repository *repo,
3102 got_worktree_status_cb status_cb, void *status_arg,
3103 got_cancel_cb cancel_cb, void *cancel_arg)
3105 const struct got_error *err = NULL;
3106 char *fileindex_path = NULL;
3107 struct got_fileindex *fileindex = NULL;
3108 struct got_pathlist_entry *pe;
3110 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3114 TAILQ_FOREACH(pe, paths, entry) {
3115 err = worktree_status(worktree, pe->path, fileindex, repo,
3116 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3120 free(fileindex_path);
3121 got_fileindex_free(fileindex);
3125 const struct got_error *
3126 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3129 const struct got_error *err = NULL;
3130 char *resolved = NULL, *cwd = NULL, *path = NULL;
3136 cwd = getcwd(NULL, 0);
3138 return got_error_from_errno("getcwd");
3140 if (lstat(arg, &sb) == -1) {
3141 if (errno != ENOENT) {
3142 err = got_error_from_errno2("lstat", arg);
3146 if (S_ISLNK(sb.st_mode)) {
3148 * We cannot use realpath(3) with symlinks since we want to
3149 * operate on the symlink itself.
3150 * But we can make the path absolute, assuming it is relative
3151 * to the current working directory, and then canonicalize it.
3153 char *abspath = NULL;
3154 char canonpath[PATH_MAX];
3155 if (!got_path_is_absolute(arg)) {
3156 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3157 err = got_error_from_errno("asprintf");
3162 err = got_canonpath(abspath ? abspath : arg, canonpath,
3166 resolved = strdup(canonpath);
3167 if (resolved == NULL) {
3168 err = got_error_from_errno("strdup");
3172 resolved = realpath(arg, NULL);
3173 if (resolved == NULL) {
3174 if (errno != ENOENT) {
3175 err = got_error_from_errno2("realpath", arg);
3178 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
3179 err = got_error_from_errno("asprintf");
3185 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3186 strlen(got_worktree_get_root_path(worktree)))) {
3187 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3191 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3192 err = got_path_skip_common_ancestor(&path,
3193 got_worktree_get_root_path(worktree), resolved);
3199 err = got_error_from_errno("strdup");
3204 /* XXX status walk can't deal with trailing slash! */
3206 while (len > 0 && path[len - 1] == '/') {
3207 path[len - 1] = '\0';
3220 struct schedule_addition_args {
3221 struct got_worktree *worktree;
3222 struct got_fileindex *fileindex;
3223 got_worktree_checkout_cb progress_cb;
3225 struct got_repository *repo;
3228 static const struct got_error *
3229 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3230 const char *relpath, struct got_object_id *blob_id,
3231 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3232 int dirfd, const char *de_name)
3234 struct schedule_addition_args *a = arg;
3235 const struct got_error *err = NULL;
3236 struct got_fileindex_entry *ie;
3240 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3242 return got_error_from_errno("asprintf");
3244 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3246 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3250 /* Re-adding an existing entry is a no-op. */
3251 if (status == GOT_STATUS_ADD)
3253 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3258 if (status != GOT_STATUS_UNVERSIONED) {
3259 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3263 err = got_fileindex_entry_alloc(&ie, relpath);
3266 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3268 got_fileindex_entry_free(ie);
3271 err = got_fileindex_entry_add(a->fileindex, ie);
3273 got_fileindex_entry_free(ie);
3280 if (status == GOT_STATUS_ADD)
3282 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3285 const struct got_error *
3286 got_worktree_schedule_add(struct got_worktree *worktree,
3287 struct got_pathlist_head *paths,
3288 got_worktree_checkout_cb progress_cb, void *progress_arg,
3289 struct got_repository *repo, int no_ignores)
3291 struct got_fileindex *fileindex = NULL;
3292 char *fileindex_path = NULL;
3293 const struct got_error *err = NULL, *sync_err, *unlockerr;
3294 struct got_pathlist_entry *pe;
3295 struct schedule_addition_args saa;
3297 err = lock_worktree(worktree, LOCK_EX);
3301 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3305 saa.worktree = worktree;
3306 saa.fileindex = fileindex;
3307 saa.progress_cb = progress_cb;
3308 saa.progress_arg = progress_arg;
3311 TAILQ_FOREACH(pe, paths, entry) {
3312 err = worktree_status(worktree, pe->path, fileindex, repo,
3313 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3317 sync_err = sync_fileindex(fileindex, fileindex_path);
3318 if (sync_err && err == NULL)
3321 free(fileindex_path);
3323 got_fileindex_free(fileindex);
3324 unlockerr = lock_worktree(worktree, LOCK_SH);
3325 if (unlockerr && err == NULL)
3330 struct schedule_deletion_args {
3331 struct got_worktree *worktree;
3332 struct got_fileindex *fileindex;
3333 got_worktree_delete_cb progress_cb;
3335 struct got_repository *repo;
3336 int delete_local_mods;
3340 static const struct got_error *
3341 schedule_for_deletion(void *arg, unsigned char status,
3342 unsigned char staged_status, const char *relpath,
3343 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3344 struct got_object_id *commit_id, int dirfd, const char *de_name)
3346 struct schedule_deletion_args *a = arg;
3347 const struct got_error *err = NULL;
3348 struct got_fileindex_entry *ie = NULL;
3350 char *ondisk_path, *parent = NULL;
3352 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3354 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3356 staged_status = get_staged_status(ie);
3357 if (staged_status != GOT_STATUS_NO_CHANGE) {
3358 if (staged_status == GOT_STATUS_DELETE)
3360 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3363 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3365 return got_error_from_errno("asprintf");
3367 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3372 if (status != GOT_STATUS_NO_CHANGE) {
3373 if (status == GOT_STATUS_DELETE)
3375 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3376 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3379 if (status != GOT_STATUS_MODIFY &&
3380 status != GOT_STATUS_MISSING) {
3381 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3386 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3388 if (unlinkat(dirfd, de_name, 0) != 0) {
3389 err = got_error_from_errno2("unlinkat",
3393 } else if (unlink(ondisk_path) != 0) {
3394 err = got_error_from_errno2("unlink", ondisk_path);
3398 parent = dirname(ondisk_path);
3400 if (parent == NULL) {
3401 err = got_error_from_errno2("dirname", ondisk_path);
3404 while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3405 if (rmdir(parent) == -1) {
3406 if (errno != ENOTEMPTY)
3407 err = got_error_from_errno2("rmdir",
3411 parent = dirname(parent);
3412 if (parent == NULL) {
3413 err = got_error_from_errno2("dirname", parent);
3419 got_fileindex_entry_mark_deleted_from_disk(ie);
3424 if (status == GOT_STATUS_DELETE)
3426 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3427 staged_status, relpath);
3430 const struct got_error *
3431 got_worktree_schedule_delete(struct got_worktree *worktree,
3432 struct got_pathlist_head *paths, int delete_local_mods,
3433 got_worktree_delete_cb progress_cb, void *progress_arg,
3434 struct got_repository *repo, int keep_on_disk)
3436 struct got_fileindex *fileindex = NULL;
3437 char *fileindex_path = NULL;
3438 const struct got_error *err = NULL, *sync_err, *unlockerr;
3439 struct got_pathlist_entry *pe;
3440 struct schedule_deletion_args sda;
3442 err = lock_worktree(worktree, LOCK_EX);
3446 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3450 sda.worktree = worktree;
3451 sda.fileindex = fileindex;
3452 sda.progress_cb = progress_cb;
3453 sda.progress_arg = progress_arg;
3455 sda.delete_local_mods = delete_local_mods;
3456 sda.keep_on_disk = keep_on_disk;
3458 TAILQ_FOREACH(pe, paths, entry) {
3459 err = worktree_status(worktree, pe->path, fileindex, repo,
3460 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3464 sync_err = sync_fileindex(fileindex, fileindex_path);
3465 if (sync_err && err == NULL)
3468 free(fileindex_path);
3470 got_fileindex_free(fileindex);
3471 unlockerr = lock_worktree(worktree, LOCK_SH);
3472 if (unlockerr && err == NULL)
3477 static const struct got_error *
3478 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3480 const struct got_error *err = NULL;
3482 size_t linesize = 0, n;
3485 linelen = getline(&line, &linesize, infile);
3486 if (linelen == -1) {
3487 if (ferror(infile)) {
3488 err = got_error_from_errno("getline");
3494 n = fwrite(line, 1, linelen, outfile);
3496 err = got_ferror(outfile, GOT_ERR_IO);
3501 n = fwrite(line, 1, linelen, rejectfile);
3503 err = got_ferror(outfile, GOT_ERR_IO);
3510 static const struct got_error *
3511 skip_one_line(FILE *f)
3514 size_t linesize = 0;
3517 linelen = getline(&line, &linesize, f);
3518 if (linelen == -1) {
3520 return got_error_from_errno("getline");
3527 static const struct got_error *
3528 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3529 int start_old, int end_old, int start_new, int end_new,
3530 FILE *outfile, FILE *rejectfile)
3532 const struct got_error *err;
3534 /* Copy old file's lines leading up to patch. */
3535 while (!feof(f1) && *line_cur1 < start_old) {
3536 err = copy_one_line(f1, outfile, NULL);
3541 /* Skip new file's lines leading up to patch. */
3542 while (!feof(f2) && *line_cur2 < start_new) {
3544 err = copy_one_line(f2, NULL, rejectfile);
3546 err = skip_one_line(f2);
3551 /* Copy patched lines. */
3552 while (!feof(f2) && *line_cur2 <= end_new) {
3553 err = copy_one_line(f2, outfile, NULL);
3558 /* Skip over old file's replaced lines. */
3559 while (!feof(f1) && *line_cur1 <= end_old) {
3561 err = copy_one_line(f1, NULL, rejectfile);
3563 err = skip_one_line(f1);
3572 static const struct got_error *
3573 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3574 FILE *outfile, FILE *rejectfile)
3576 const struct got_error *err;
3579 /* Copy old file's lines until EOF. */
3581 err = copy_one_line(f1, outfile, NULL);
3588 /* Copy new file's lines until EOF. */
3590 err = copy_one_line(f2, NULL, rejectfile);
3600 static const struct got_error *
3601 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3602 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3603 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3604 int *line_cur2, FILE *outfile, FILE *rejectfile,
3605 got_worktree_patch_cb patch_cb, void *patch_arg)
3607 const struct got_error *err = NULL;
3608 int start_old = change->cv.a;
3609 int end_old = change->cv.b;
3610 int start_new = change->cv.c;
3611 int end_new = change->cv.d;
3615 *choice = GOT_PATCH_CHOICE_NONE;
3617 hunkfile = got_opentemp();
3618 if (hunkfile == NULL)
3619 return got_error_from_errno("got_opentemp");
3624 /* XXX TODO needs error checking */
3625 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
3627 if (fseek(f1, pos1, SEEK_SET) == -1) {
3628 err = got_ferror(f1, GOT_ERR_IO);
3631 if (fseek(f2, pos2, SEEK_SET) == -1) {
3632 err = got_ferror(f1, GOT_ERR_IO);
3635 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
3636 err = got_ferror(hunkfile, GOT_ERR_IO);
3640 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
3641 hunkfile, n, nchanges);
3646 case GOT_PATCH_CHOICE_YES:
3647 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3648 end_old, start_new, end_new, outfile, rejectfile);
3650 case GOT_PATCH_CHOICE_NO:
3651 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3652 end_old, start_new, end_new, rejectfile, outfile);
3654 case GOT_PATCH_CHOICE_QUIT:
3657 err = got_error(GOT_ERR_PATCH_CHOICE);
3661 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3662 err = got_error_from_errno("fclose");
3666 struct revert_file_args {
3667 struct got_worktree *worktree;
3668 struct got_fileindex *fileindex;
3669 got_worktree_checkout_cb progress_cb;
3671 got_worktree_patch_cb patch_cb;
3673 struct got_repository *repo;
3676 static const struct got_error *
3677 create_patched_content(char **path_outfile, int reverse_patch,
3678 struct got_object_id *blob_id, const char *path2,
3679 int dirfd2, const char *de_name2,
3680 const char *relpath, struct got_repository *repo,
3681 got_worktree_patch_cb patch_cb, void *patch_arg)
3683 const struct got_error *err;
3684 struct got_blob_object *blob = NULL;
3685 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3687 char *path1 = NULL, *id_str = NULL;
3688 struct stat sb1, sb2;
3689 struct got_diff_changes *changes = NULL;
3690 struct got_diff_state *ds = NULL;
3691 struct got_diff_args *args = NULL;
3692 struct got_diff_change *change;
3693 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3696 *path_outfile = NULL;
3698 err = got_object_id_str(&id_str, blob_id);
3703 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
3705 err = got_error_from_errno2("openat", path2);
3709 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
3711 err = got_error_from_errno2("open", path2);
3715 if (fstat(fd2, &sb2) == -1) {
3716 err = got_error_from_errno2("fstat", path2);
3720 f2 = fdopen(fd2, "r");
3722 err = got_error_from_errno2("fdopen", path2);
3727 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
3731 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
3735 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
3739 if (stat(path1, &sb1) == -1) {
3740 err = got_error_from_errno2("stat", path1);
3744 err = got_diff_files(&changes, &ds, &args, &diff_flags,
3745 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
3749 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
3753 if (fseek(f1, 0L, SEEK_SET) == -1)
3754 return got_ferror(f1, GOT_ERR_IO);
3755 if (fseek(f2, 0L, SEEK_SET) == -1)
3756 return got_ferror(f2, GOT_ERR_IO);
3757 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
3759 err = apply_or_reject_change(&choice, change, ++n,
3760 changes->nchanges, ds, args, diff_flags, relpath,
3761 f1, f2, &line_cur1, &line_cur2,
3762 reverse_patch ? NULL : outfile,
3763 reverse_patch ? outfile : NULL,
3764 patch_cb, patch_arg);
3767 if (choice == GOT_PATCH_CHOICE_YES)
3769 else if (choice == GOT_PATCH_CHOICE_QUIT)
3773 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
3774 reverse_patch ? NULL : outfile,
3775 reverse_patch ? outfile : NULL);
3779 if (chmod(*path_outfile, sb2.st_mode) == -1) {
3780 err = got_error_from_errno2("chmod", path2);
3787 got_object_blob_close(blob);
3788 if (f1 && fclose(f1) == EOF && err == NULL)
3789 err = got_error_from_errno2("fclose", path1);
3790 if (f2 && fclose(f2) == EOF && err == NULL)
3791 err = got_error_from_errno2("fclose", path2);
3792 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3793 err = got_error_from_errno2("close", path2);
3794 if (outfile && fclose(outfile) == EOF && err == NULL)
3795 err = got_error_from_errno2("fclose", *path_outfile);
3796 if (path1 && unlink(path1) == -1 && err == NULL)
3797 err = got_error_from_errno2("unlink", path1);
3798 if (err || !have_content) {
3799 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
3800 err = got_error_from_errno2("unlink", *path_outfile);
3801 free(*path_outfile);
3802 *path_outfile = NULL;
3806 got_diff_state_free(ds);
3810 got_diff_free_changes(changes);
3815 static const struct got_error *
3816 revert_file(void *arg, unsigned char status, unsigned char staged_status,
3817 const char *relpath, struct got_object_id *blob_id,
3818 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3819 int dirfd, const char *de_name)
3821 struct revert_file_args *a = arg;
3822 const struct got_error *err = NULL;
3823 char *parent_path = NULL;
3824 struct got_fileindex_entry *ie;
3825 struct got_tree_object *tree = NULL;
3826 struct got_object_id *tree_id = NULL;
3827 const struct got_tree_entry *te = NULL;
3828 char *tree_path = NULL, *te_name;
3829 char *ondisk_path = NULL, *path_content = NULL;
3830 struct got_blob_object *blob = NULL;
3832 /* Reverting a staged deletion is a no-op. */
3833 if (status == GOT_STATUS_DELETE &&
3834 staged_status != GOT_STATUS_NO_CHANGE)
3837 if (status == GOT_STATUS_UNVERSIONED)
3838 return (*a->progress_cb)(a->progress_arg,
3839 GOT_STATUS_UNVERSIONED, relpath);
3841 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3843 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3845 /* Construct in-repository path of tree which contains this blob. */
3846 err = got_path_dirname(&parent_path, ie->path);
3848 if (err->code != GOT_ERR_BAD_PATH)
3850 parent_path = strdup("/");
3851 if (parent_path == NULL) {
3852 err = got_error_from_errno("strdup");
3856 if (got_path_is_root_dir(a->worktree->path_prefix)) {
3857 tree_path = strdup(parent_path);
3858 if (tree_path == NULL) {
3859 err = got_error_from_errno("strdup");
3863 if (got_path_is_root_dir(parent_path)) {
3864 tree_path = strdup(a->worktree->path_prefix);
3865 if (tree_path == NULL) {
3866 err = got_error_from_errno("strdup");
3870 if (asprintf(&tree_path, "%s/%s",
3871 a->worktree->path_prefix, parent_path) == -1) {
3872 err = got_error_from_errno("asprintf");
3878 err = got_object_id_by_path(&tree_id, a->repo,
3879 a->worktree->base_commit_id, tree_path);
3881 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
3882 (status == GOT_STATUS_ADD ||
3883 staged_status == GOT_STATUS_ADD)))
3886 err = got_object_open_as_tree(&tree, a->repo, tree_id);
3890 te_name = basename(ie->path);
3891 if (te_name == NULL) {
3892 err = got_error_from_errno2("basename", ie->path);
3896 te = got_object_tree_find_entry(tree, te_name);
3897 if (te == NULL && status != GOT_STATUS_ADD &&
3898 staged_status != GOT_STATUS_ADD) {
3899 err = got_error(GOT_ERR_NO_TREE_ENTRY);
3905 case GOT_STATUS_ADD:
3907 int choice = GOT_PATCH_CHOICE_NONE;
3908 err = (*a->patch_cb)(&choice, a->patch_arg,
3909 status, ie->path, NULL, 1, 1);
3912 if (choice != GOT_PATCH_CHOICE_YES)
3915 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
3919 got_fileindex_entry_remove(a->fileindex, ie);
3921 case GOT_STATUS_DELETE:
3923 int choice = GOT_PATCH_CHOICE_NONE;
3924 err = (*a->patch_cb)(&choice, a->patch_arg,
3925 status, ie->path, NULL, 1, 1);
3928 if (choice != GOT_PATCH_CHOICE_YES)
3932 case GOT_STATUS_MODIFY:
3933 case GOT_STATUS_MODE_CHANGE:
3934 case GOT_STATUS_CONFLICT:
3935 case GOT_STATUS_MISSING: {
3936 struct got_object_id id;
3937 if (staged_status == GOT_STATUS_ADD ||
3938 staged_status == GOT_STATUS_MODIFY) {
3939 memcpy(id.sha1, ie->staged_blob_sha1,
3940 SHA1_DIGEST_LENGTH);
3942 memcpy(id.sha1, ie->blob_sha1,
3943 SHA1_DIGEST_LENGTH);
3944 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
3948 if (asprintf(&ondisk_path, "%s/%s",
3949 got_worktree_get_root_path(a->worktree), relpath) == -1) {
3950 err = got_error_from_errno("asprintf");
3954 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
3955 status == GOT_STATUS_CONFLICT)) {
3956 err = create_patched_content(&path_content, 1, &id,
3957 ondisk_path, dirfd, de_name, ie->path, a->repo,
3958 a->patch_cb, a->patch_arg);
3959 if (err || path_content == NULL)
3961 if (rename(path_content, ondisk_path) == -1) {
3962 err = got_error_from_errno3("rename",
3963 path_content, ondisk_path);
3967 err = install_blob(a->worktree, ondisk_path, ie->path,
3968 te ? te->mode : GOT_DEFAULT_FILE_MODE,
3969 got_fileindex_perms_to_st(ie), blob, 0, 1,
3970 a->repo, a->progress_cb, a->progress_arg);
3973 if (status == GOT_STATUS_DELETE ||
3974 status == GOT_STATUS_MODE_CHANGE) {
3975 err = got_fileindex_entry_update(ie,
3976 ondisk_path, blob->id.sha1,
3977 a->worktree->base_commit_id->sha1, 1);
3993 got_object_blob_close(blob);
3995 got_object_tree_close(tree);
4000 const struct got_error *
4001 got_worktree_revert(struct got_worktree *worktree,
4002 struct got_pathlist_head *paths,
4003 got_worktree_checkout_cb progress_cb, void *progress_arg,
4004 got_worktree_patch_cb patch_cb, void *patch_arg,
4005 struct got_repository *repo)
4007 struct got_fileindex *fileindex = NULL;
4008 char *fileindex_path = NULL;
4009 const struct got_error *err = NULL, *unlockerr = NULL;
4010 const struct got_error *sync_err = NULL;
4011 struct got_pathlist_entry *pe;
4012 struct revert_file_args rfa;
4014 err = lock_worktree(worktree, LOCK_EX);
4018 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4022 rfa.worktree = worktree;
4023 rfa.fileindex = fileindex;
4024 rfa.progress_cb = progress_cb;
4025 rfa.progress_arg = progress_arg;
4026 rfa.patch_cb = patch_cb;
4027 rfa.patch_arg = patch_arg;
4029 TAILQ_FOREACH(pe, paths, entry) {
4030 err = worktree_status(worktree, pe->path, fileindex, repo,
4031 revert_file, &rfa, NULL, NULL, 0, 0);
4035 sync_err = sync_fileindex(fileindex, fileindex_path);
4036 if (sync_err && err == NULL)
4039 free(fileindex_path);
4041 got_fileindex_free(fileindex);
4042 unlockerr = lock_worktree(worktree, LOCK_SH);
4043 if (unlockerr && err == NULL)
4049 free_commitable(struct got_commitable *ct)
4052 free(ct->in_repo_path);
4053 free(ct->ondisk_path);
4055 free(ct->base_blob_id);
4056 free(ct->staged_blob_id);
4057 free(ct->base_commit_id);
4061 struct collect_commitables_arg {
4062 struct got_pathlist_head *commitable_paths;
4063 struct got_repository *repo;
4064 struct got_worktree *worktree;
4065 int have_staged_files;
4068 static const struct got_error *
4069 collect_commitables(void *arg, unsigned char status,
4070 unsigned char staged_status, const char *relpath,
4071 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4072 struct got_object_id *commit_id, int dirfd, const char *de_name)
4074 struct collect_commitables_arg *a = arg;
4075 const struct got_error *err = NULL;
4076 struct got_commitable *ct = NULL;
4077 struct got_pathlist_entry *new = NULL;
4078 char *parent_path = NULL, *path = NULL;
4081 if (a->have_staged_files) {
4082 if (staged_status != GOT_STATUS_MODIFY &&
4083 staged_status != GOT_STATUS_ADD &&
4084 staged_status != GOT_STATUS_DELETE)
4087 if (status == GOT_STATUS_CONFLICT)
4088 return got_error(GOT_ERR_COMMIT_CONFLICT);
4090 if (status != GOT_STATUS_MODIFY &&
4091 status != GOT_STATUS_MODE_CHANGE &&
4092 status != GOT_STATUS_ADD &&
4093 status != GOT_STATUS_DELETE)
4097 if (asprintf(&path, "/%s", relpath) == -1) {
4098 err = got_error_from_errno("asprintf");
4101 if (strcmp(path, "/") == 0) {
4102 parent_path = strdup("");
4103 if (parent_path == NULL)
4104 return got_error_from_errno("strdup");
4106 err = got_path_dirname(&parent_path, path);
4111 ct = calloc(1, sizeof(*ct));
4113 err = got_error_from_errno("calloc");
4117 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4119 err = got_error_from_errno("asprintf");
4122 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
4123 sb.st_mode = GOT_DEFAULT_FILE_MODE;
4126 if (fstatat(dirfd, de_name, &sb,
4127 AT_SYMLINK_NOFOLLOW) == -1) {
4128 err = got_error_from_errno2("fstatat",
4132 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4133 err = got_error_from_errno2("lstat", ct->ondisk_path);
4136 ct->mode = sb.st_mode;
4139 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4140 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4142 err = got_error_from_errno("asprintf");
4146 ct->status = status;
4147 ct->staged_status = staged_status;
4148 ct->blob_id = NULL; /* will be filled in when blob gets created */
4149 if (ct->status != GOT_STATUS_ADD &&
4150 ct->staged_status != GOT_STATUS_ADD) {
4151 ct->base_blob_id = got_object_id_dup(blob_id);
4152 if (ct->base_blob_id == NULL) {
4153 err = got_error_from_errno("got_object_id_dup");
4156 ct->base_commit_id = got_object_id_dup(commit_id);
4157 if (ct->base_commit_id == NULL) {
4158 err = got_error_from_errno("got_object_id_dup");
4162 if (ct->staged_status == GOT_STATUS_ADD ||
4163 ct->staged_status == GOT_STATUS_MODIFY) {
4164 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4165 if (ct->staged_blob_id == NULL) {
4166 err = got_error_from_errno("got_object_id_dup");
4170 ct->path = strdup(path);
4171 if (ct->path == NULL) {
4172 err = got_error_from_errno("strdup");
4175 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4177 if (ct && (err || new == NULL))
4178 free_commitable(ct);
4184 static const struct got_error *write_tree(struct got_object_id **, int *,
4185 struct got_tree_object *, const char *, struct got_pathlist_head *,
4186 got_worktree_status_cb status_cb, void *status_arg,
4187 struct got_repository *);
4189 static const struct got_error *
4190 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4191 struct got_tree_entry *te, const char *parent_path,
4192 struct got_pathlist_head *commitable_paths,
4193 got_worktree_status_cb status_cb, void *status_arg,
4194 struct got_repository *repo)
4196 const struct got_error *err = NULL;
4197 struct got_tree_object *subtree;
4200 if (asprintf(&subpath, "%s%s%s", parent_path,
4201 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4202 return got_error_from_errno("asprintf");
4204 err = got_object_open_as_tree(&subtree, repo, &te->id);
4208 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4209 commitable_paths, status_cb, status_arg, repo);
4210 got_object_tree_close(subtree);
4215 static const struct got_error *
4216 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4218 const struct got_error *err = NULL;
4219 char *ct_parent_path = NULL;
4223 if (strchr(ct->in_repo_path, '/') == NULL) {
4224 *match = got_path_is_root_dir(path);
4228 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4231 *match = (strcmp(path, ct_parent_path) == 0);
4232 free(ct_parent_path);
4237 get_ct_file_mode(struct got_commitable *ct)
4239 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4242 static const struct got_error *
4243 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4244 struct got_tree_entry *te, struct got_commitable *ct)
4246 const struct got_error *err = NULL;
4250 err = got_object_tree_entry_dup(new_te, te);
4254 (*new_te)->mode = get_ct_file_mode(ct);
4256 if (ct->staged_status == GOT_STATUS_MODIFY)
4257 memcpy(&(*new_te)->id, ct->staged_blob_id,
4258 sizeof((*new_te)->id));
4260 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4262 if (err && *new_te) {
4269 static const struct got_error *
4270 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4271 struct got_commitable *ct)
4273 const struct got_error *err = NULL;
4278 *new_te = calloc(1, sizeof(**new_te));
4279 if (*new_te == NULL)
4280 return got_error_from_errno("calloc");
4282 ct_name = basename(ct->path);
4283 if (ct_name == NULL) {
4284 err = got_error_from_errno2("basename", ct->path);
4287 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4288 sizeof((*new_te)->name)) {
4289 err = got_error(GOT_ERR_NO_SPACE);
4293 (*new_te)->mode = get_ct_file_mode(ct);
4295 if (ct->staged_status == GOT_STATUS_ADD)
4296 memcpy(&(*new_te)->id, ct->staged_blob_id,
4297 sizeof((*new_te)->id));
4299 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4301 if (err && *new_te) {
4308 static const struct got_error *
4309 insert_tree_entry(struct got_tree_entry *new_te,
4310 struct got_pathlist_head *paths)
4312 const struct got_error *err = NULL;
4313 struct got_pathlist_entry *new_pe;
4315 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4319 return got_error(GOT_ERR_TREE_DUP_ENTRY);
4323 static const struct got_error *
4324 report_ct_status(struct got_commitable *ct,
4325 got_worktree_status_cb status_cb, void *status_arg)
4327 const char *ct_path = ct->path;
4328 unsigned char status;
4330 while (ct_path[0] == '/')
4333 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4334 status = ct->staged_status;
4336 status = ct->status;
4338 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4339 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4342 static const struct got_error *
4343 match_modified_subtree(int *modified, struct got_tree_entry *te,
4344 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4346 const struct got_error *err = NULL;
4347 struct got_pathlist_entry *pe;
4352 if (asprintf(&te_path, "%s%s%s", base_tree_path,
4353 got_path_is_root_dir(base_tree_path) ? "" : "/",
4355 return got_error_from_errno("asprintf");
4357 TAILQ_FOREACH(pe, commitable_paths, entry) {
4358 struct got_commitable *ct = pe->data;
4359 *modified = got_path_is_child(ct->in_repo_path, te_path,
4369 static const struct got_error *
4370 match_deleted_or_modified_ct(struct got_commitable **ctp,
4371 struct got_tree_entry *te, const char *base_tree_path,
4372 struct got_pathlist_head *commitable_paths)
4374 const struct got_error *err = NULL;
4375 struct got_pathlist_entry *pe;
4379 TAILQ_FOREACH(pe, commitable_paths, entry) {
4380 struct got_commitable *ct = pe->data;
4381 char *ct_name = NULL;
4384 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4385 if (ct->status != GOT_STATUS_MODIFY &&
4386 ct->status != GOT_STATUS_MODE_CHANGE &&
4387 ct->status != GOT_STATUS_DELETE)
4390 if (ct->staged_status != GOT_STATUS_MODIFY &&
4391 ct->staged_status != GOT_STATUS_DELETE)
4395 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4398 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4404 ct_name = basename(pe->path);
4405 if (ct_name == NULL)
4406 return got_error_from_errno2("basename", pe->path);
4408 if (strcmp(te->name, ct_name) != 0)
4418 static const struct got_error *
4419 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4420 const char *child_path, const char *path_base_tree,
4421 struct got_pathlist_head *commitable_paths,
4422 got_worktree_status_cb status_cb, void *status_arg,
4423 struct got_repository *repo)
4425 const struct got_error *err = NULL;
4426 struct got_tree_entry *new_te;
4428 struct got_object_id *id = NULL;
4433 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4434 got_path_is_root_dir(path_base_tree) ? "" : "/",
4436 return got_error_from_errno("asprintf");
4438 new_te = calloc(1, sizeof(*new_te));
4440 return got_error_from_errno("calloc");
4441 new_te->mode = S_IFDIR;
4443 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4444 sizeof(new_te->name)) {
4445 err = got_error(GOT_ERR_NO_SPACE);
4448 err = write_tree(&id, &nentries, NULL, subtree_path,
4449 commitable_paths, status_cb, status_arg, repo);
4454 memcpy(&new_te->id, id, sizeof(new_te->id));
4463 static const struct got_error *
4464 write_tree(struct got_object_id **new_tree_id, int *nentries,
4465 struct got_tree_object *base_tree, const char *path_base_tree,
4466 struct got_pathlist_head *commitable_paths,
4467 got_worktree_status_cb status_cb, void *status_arg,
4468 struct got_repository *repo)
4470 const struct got_error *err = NULL;
4471 struct got_pathlist_head paths;
4472 struct got_tree_entry *te, *new_te = NULL;
4473 struct got_pathlist_entry *pe;
4478 /* Insert, and recurse into, newly added entries first. */
4479 TAILQ_FOREACH(pe, commitable_paths, entry) {
4480 struct got_commitable *ct = pe->data;
4481 char *child_path = NULL, *slash;
4483 if ((ct->status != GOT_STATUS_ADD &&
4484 ct->staged_status != GOT_STATUS_ADD) ||
4485 (ct->flags & GOT_COMMITABLE_ADDED))
4488 if (!got_path_is_child(pe->path, path_base_tree,
4489 strlen(path_base_tree)))
4492 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
4497 slash = strchr(child_path, '/');
4498 if (slash == NULL) {
4499 err = alloc_added_blob_tree_entry(&new_te, ct);
4502 err = report_ct_status(ct, status_cb, status_arg);
4505 ct->flags |= GOT_COMMITABLE_ADDED;
4506 err = insert_tree_entry(new_te, &paths);
4511 *slash = '\0'; /* trim trailing path components */
4512 if (base_tree == NULL ||
4513 got_object_tree_find_entry(base_tree, child_path)
4515 err = make_subtree_for_added_blob(&new_te,
4516 child_path, path_base_tree,
4517 commitable_paths, status_cb, status_arg,
4521 err = insert_tree_entry(new_te, &paths);
4530 int i, nbase_entries;
4531 /* Handle modified and deleted entries. */
4532 nbase_entries = got_object_tree_get_nentries(base_tree);
4533 for (i = 0; i < nbase_entries; i++) {
4534 struct got_commitable *ct = NULL;
4536 te = got_object_tree_get_entry(base_tree, i);
4537 if (got_object_tree_entry_is_submodule(te)) {
4538 /* Entry is a submodule; just copy it. */
4539 err = got_object_tree_entry_dup(&new_te, te);
4542 err = insert_tree_entry(new_te, &paths);
4549 if (S_ISDIR(te->mode)) {
4551 err = got_object_tree_entry_dup(&new_te, te);
4554 err = match_modified_subtree(&modified, te,
4555 path_base_tree, commitable_paths);
4558 /* Avoid recursion into unmodified subtrees. */
4560 struct got_object_id *new_id;
4562 err = write_subtree(&new_id,
4564 path_base_tree, commitable_paths,
4565 status_cb, status_arg, repo);
4568 if (nsubentries == 0) {
4569 /* All entries were deleted. */
4573 memcpy(&new_te->id, new_id,
4574 sizeof(new_te->id));
4577 err = insert_tree_entry(new_te, &paths);
4584 err = match_deleted_or_modified_ct(&ct, te,
4585 path_base_tree, commitable_paths);
4589 /* NB: Deleted entries get dropped here. */
4590 if (ct->status == GOT_STATUS_MODIFY ||
4591 ct->status == GOT_STATUS_MODE_CHANGE ||
4592 ct->staged_status == GOT_STATUS_MODIFY) {
4593 err = alloc_modified_blob_tree_entry(
4597 err = insert_tree_entry(new_te, &paths);
4602 err = report_ct_status(ct, status_cb,
4607 /* Entry is unchanged; just copy it. */
4608 err = got_object_tree_entry_dup(&new_te, te);
4611 err = insert_tree_entry(new_te, &paths);
4619 /* Write new list of entries; deleted entries have been dropped. */
4620 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
4622 got_pathlist_free(&paths);
4626 static const struct got_error *
4627 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
4628 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
4629 int have_staged_files)
4631 const struct got_error *err = NULL;
4632 struct got_pathlist_entry *pe;
4634 TAILQ_FOREACH(pe, commitable_paths, entry) {
4635 struct got_fileindex_entry *ie;
4636 struct got_commitable *ct = pe->data;
4638 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4640 if (ct->status == GOT_STATUS_DELETE ||
4641 ct->staged_status == GOT_STATUS_DELETE) {
4642 got_fileindex_entry_remove(fileindex, ie);
4643 } else if (ct->staged_status == GOT_STATUS_ADD ||
4644 ct->staged_status == GOT_STATUS_MODIFY) {
4645 got_fileindex_entry_stage_set(ie,
4646 GOT_FILEIDX_STAGE_NONE);
4647 err = got_fileindex_entry_update(ie,
4648 ct->ondisk_path, ct->staged_blob_id->sha1,
4649 new_base_commit_id->sha1,
4650 !have_staged_files);
4652 err = got_fileindex_entry_update(ie,
4653 ct->ondisk_path, ct->blob_id->sha1,
4654 new_base_commit_id->sha1,
4655 !have_staged_files);
4657 err = got_fileindex_entry_alloc(&ie, pe->path);
4660 err = got_fileindex_entry_update(ie, ct->ondisk_path,
4661 ct->blob_id->sha1, new_base_commit_id->sha1, 1);
4663 got_fileindex_entry_free(ie);
4666 err = got_fileindex_entry_add(fileindex, ie);
4668 got_fileindex_entry_free(ie);
4677 static const struct got_error *
4678 check_out_of_date(const char *in_repo_path, unsigned char status,
4679 unsigned char staged_status, struct got_object_id *base_blob_id,
4680 struct got_object_id *base_commit_id,
4681 struct got_object_id *head_commit_id, struct got_repository *repo,
4684 const struct got_error *err = NULL;
4685 struct got_object_id *id = NULL;
4687 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
4688 /* Trivial case: base commit == head commit */
4689 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
4692 * Ensure file content which local changes were based
4693 * on matches file content in the branch head.
4695 err = got_object_id_by_path(&id, repo, head_commit_id,
4698 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4699 err = got_error(ood_errcode);
4701 } else if (got_object_id_cmp(id, base_blob_id) != 0)
4702 err = got_error(ood_errcode);
4704 /* Require that added files don't exist in the branch head. */
4705 err = got_object_id_by_path(&id, repo, head_commit_id,
4707 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
4709 err = id ? got_error(ood_errcode) : NULL;
4716 const struct got_error *
4717 commit_worktree(struct got_object_id **new_commit_id,
4718 struct got_pathlist_head *commitable_paths,
4719 struct got_object_id *head_commit_id, struct got_worktree *worktree,
4720 const char *author, const char *committer,
4721 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4722 got_worktree_status_cb status_cb, void *status_arg,
4723 struct got_repository *repo)
4725 const struct got_error *err = NULL, *unlockerr = NULL;
4726 struct got_pathlist_entry *pe;
4727 const char *head_ref_name = NULL;
4728 struct got_commit_object *head_commit = NULL;
4729 struct got_reference *head_ref2 = NULL;
4730 struct got_object_id *head_commit_id2 = NULL;
4731 struct got_tree_object *head_tree = NULL;
4732 struct got_object_id *new_tree_id = NULL;
4734 struct got_object_id_queue parent_ids;
4735 struct got_object_qid *pid = NULL;
4736 char *logmsg = NULL;
4738 *new_commit_id = NULL;
4740 SIMPLEQ_INIT(&parent_ids);
4742 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
4746 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
4750 if (commit_msg_cb != NULL) {
4751 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
4756 if (logmsg == NULL || strlen(logmsg) == 0) {
4757 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
4761 /* Create blobs from added and modified files and record their IDs. */
4762 TAILQ_FOREACH(pe, commitable_paths, entry) {
4763 struct got_commitable *ct = pe->data;
4766 /* Blobs for staged files already exist. */
4767 if (ct->staged_status == GOT_STATUS_ADD ||
4768 ct->staged_status == GOT_STATUS_MODIFY)
4771 if (ct->status != GOT_STATUS_ADD &&
4772 ct->status != GOT_STATUS_MODIFY &&
4773 ct->status != GOT_STATUS_MODE_CHANGE)
4776 if (asprintf(&ondisk_path, "%s/%s",
4777 worktree->root_path, pe->path) == -1) {
4778 err = got_error_from_errno("asprintf");
4781 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
4787 /* Recursively write new tree objects. */
4788 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
4789 commitable_paths, status_cb, status_arg, repo);
4793 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
4796 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
4797 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
4798 1, author, time(NULL), committer, time(NULL), logmsg, repo);
4799 got_object_qid_free(pid);
4805 /* Check if a concurrent commit to our branch has occurred. */
4806 head_ref_name = got_worktree_get_head_ref_name(worktree);
4807 if (head_ref_name == NULL) {
4808 err = got_error_from_errno("got_worktree_get_head_ref_name");
4811 /* Lock the reference here to prevent concurrent modification. */
4812 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
4815 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
4818 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
4819 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
4822 /* Update branch head in repository. */
4823 err = got_ref_change_ref(head_ref2, *new_commit_id);
4826 err = got_ref_write(head_ref2, repo);
4830 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
4834 err = ref_base_commit(worktree, repo);
4839 got_object_tree_close(head_tree);
4841 got_object_commit_close(head_commit);
4842 free(head_commit_id2);
4844 unlockerr = got_ref_unlock(head_ref2);
4845 if (unlockerr && err == NULL)
4847 got_ref_close(head_ref2);
4852 static const struct got_error *
4853 check_path_is_commitable(const char *path,
4854 struct got_pathlist_head *commitable_paths)
4856 struct got_pathlist_entry *cpe = NULL;
4857 size_t path_len = strlen(path);
4859 TAILQ_FOREACH(cpe, commitable_paths, entry) {
4860 struct got_commitable *ct = cpe->data;
4861 const char *ct_path = ct->path;
4863 while (ct_path[0] == '/')
4866 if (strcmp(path, ct_path) == 0 ||
4867 got_path_is_child(ct_path, path, path_len))
4872 return got_error_path(path, GOT_ERR_BAD_PATH);
4877 static const struct got_error *
4878 check_staged_file(void *arg, struct got_fileindex_entry *ie)
4880 int *have_staged_files = arg;
4882 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
4883 *have_staged_files = 1;
4884 return got_error(GOT_ERR_CANCELLED);
4890 static const struct got_error *
4891 check_non_staged_files(struct got_fileindex *fileindex,
4892 struct got_pathlist_head *paths)
4894 struct got_pathlist_entry *pe;
4895 struct got_fileindex_entry *ie;
4897 TAILQ_FOREACH(pe, paths, entry) {
4898 if (pe->path[0] == '\0')
4900 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4902 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
4903 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
4904 return got_error_path(pe->path,
4905 GOT_ERR_FILE_NOT_STAGED);
4911 const struct got_error *
4912 got_worktree_commit(struct got_object_id **new_commit_id,
4913 struct got_worktree *worktree, struct got_pathlist_head *paths,
4914 const char *author, const char *committer,
4915 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4916 got_worktree_status_cb status_cb, void *status_arg,
4917 struct got_repository *repo)
4919 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
4920 struct got_fileindex *fileindex = NULL;
4921 char *fileindex_path = NULL;
4922 struct got_pathlist_head commitable_paths;
4923 struct collect_commitables_arg cc_arg;
4924 struct got_pathlist_entry *pe;
4925 struct got_reference *head_ref = NULL;
4926 struct got_object_id *head_commit_id = NULL;
4927 int have_staged_files = 0;
4929 *new_commit_id = NULL;
4931 TAILQ_INIT(&commitable_paths);
4933 err = lock_worktree(worktree, LOCK_EX);
4937 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4941 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4945 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4949 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
4950 &have_staged_files);
4951 if (err && err->code != GOT_ERR_CANCELLED)
4953 if (have_staged_files) {
4954 err = check_non_staged_files(fileindex, paths);
4959 cc_arg.commitable_paths = &commitable_paths;
4960 cc_arg.worktree = worktree;
4962 cc_arg.have_staged_files = have_staged_files;
4963 TAILQ_FOREACH(pe, paths, entry) {
4964 err = worktree_status(worktree, pe->path, fileindex, repo,
4965 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
4970 if (TAILQ_EMPTY(&commitable_paths)) {
4971 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4975 TAILQ_FOREACH(pe, paths, entry) {
4976 err = check_path_is_commitable(pe->path, &commitable_paths);
4981 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4982 struct got_commitable *ct = pe->data;
4983 const char *ct_path = ct->in_repo_path;
4985 while (ct_path[0] == '/')
4987 err = check_out_of_date(ct_path, ct->status,
4988 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
4989 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
4995 err = commit_worktree(new_commit_id, &commitable_paths,
4996 head_commit_id, worktree, author, committer,
4997 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5001 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5002 fileindex, have_staged_files);
5003 sync_err = sync_fileindex(fileindex, fileindex_path);
5004 if (sync_err && err == NULL)
5008 got_fileindex_free(fileindex);
5009 free(fileindex_path);
5010 unlockerr = lock_worktree(worktree, LOCK_SH);
5011 if (unlockerr && err == NULL)
5013 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5014 struct got_commitable *ct = pe->data;
5015 free_commitable(ct);
5017 got_pathlist_free(&commitable_paths);
5022 got_commitable_get_path(struct got_commitable *ct)
5028 got_commitable_get_status(struct got_commitable *ct)
5033 struct check_rebase_ok_arg {
5034 struct got_worktree *worktree;
5035 struct got_repository *repo;
5038 static const struct got_error *
5039 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5041 const struct got_error *err = NULL;
5042 struct check_rebase_ok_arg *a = arg;
5043 unsigned char status;
5047 /* Reject rebase of a work tree with mixed base commits. */
5048 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5049 SHA1_DIGEST_LENGTH))
5050 return got_error(GOT_ERR_MIXED_COMMITS);
5052 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5054 return got_error_from_errno("asprintf");
5056 /* Reject rebase of a work tree with modified or staged files. */
5057 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5062 if (status != GOT_STATUS_NO_CHANGE)
5063 return got_error(GOT_ERR_MODIFIED);
5064 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5065 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5070 const struct got_error *
5071 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5072 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5073 struct got_worktree *worktree, struct got_reference *branch,
5074 struct got_repository *repo)
5076 const struct got_error *err = NULL;
5077 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5078 char *branch_ref_name = NULL;
5079 char *fileindex_path = NULL;
5080 struct check_rebase_ok_arg ok_arg;
5081 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5082 struct got_object_id *wt_branch_tip = NULL;
5084 *new_base_branch_ref = NULL;
5088 err = lock_worktree(worktree, LOCK_EX);
5092 err = open_fileindex(fileindex, &fileindex_path, worktree);
5096 ok_arg.worktree = worktree;
5098 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5103 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5107 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5111 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5115 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5120 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5123 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5124 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5128 err = got_ref_alloc_symref(new_base_branch_ref,
5129 new_base_branch_ref_name, wt_branch);
5132 err = got_ref_write(*new_base_branch_ref, repo);
5136 /* TODO Lock original branch's ref while rebasing? */
5138 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5142 err = got_ref_write(branch_ref, repo);
5146 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5147 worktree->base_commit_id);
5150 err = got_ref_write(*tmp_branch, repo);
5154 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5158 free(fileindex_path);
5159 free(tmp_branch_name);
5160 free(new_base_branch_ref_name);
5161 free(branch_ref_name);
5163 got_ref_close(branch_ref);
5165 got_ref_close(wt_branch);
5166 free(wt_branch_tip);
5168 if (*new_base_branch_ref) {
5169 got_ref_close(*new_base_branch_ref);
5170 *new_base_branch_ref = NULL;
5173 got_ref_close(*tmp_branch);
5177 got_fileindex_free(*fileindex);
5180 lock_worktree(worktree, LOCK_SH);
5185 const struct got_error *
5186 got_worktree_rebase_continue(struct got_object_id **commit_id,
5187 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5188 struct got_reference **branch, struct got_fileindex **fileindex,
5189 struct got_worktree *worktree, struct got_repository *repo)
5191 const struct got_error *err;
5192 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5193 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5194 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5195 char *fileindex_path = NULL;
5196 int have_staged_files = 0;
5199 *new_base_branch = NULL;
5204 err = lock_worktree(worktree, LOCK_EX);
5208 err = open_fileindex(fileindex, &fileindex_path, worktree);
5212 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5213 &have_staged_files);
5214 if (err && err->code != GOT_ERR_CANCELLED)
5216 if (have_staged_files) {
5217 err = got_error(GOT_ERR_STAGED_PATHS);
5221 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5225 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5229 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5233 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5237 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5241 err = got_ref_open(branch, repo,
5242 got_ref_get_symref_target(branch_ref), 0);
5246 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5250 err = got_ref_resolve(commit_id, repo, commit_ref);
5254 err = got_ref_open(new_base_branch, repo,
5255 new_base_branch_ref_name, 0);
5259 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5263 free(commit_ref_name);
5264 free(branch_ref_name);
5265 free(fileindex_path);
5267 got_ref_close(commit_ref);
5269 got_ref_close(branch_ref);
5274 got_ref_close(*tmp_branch);
5277 if (*new_base_branch) {
5278 got_ref_close(*new_base_branch);
5279 *new_base_branch = NULL;
5282 got_ref_close(*branch);
5286 got_fileindex_free(*fileindex);
5289 lock_worktree(worktree, LOCK_SH);
5294 const struct got_error *
5295 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5297 const struct got_error *err;
5298 char *tmp_branch_name = NULL;
5300 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5304 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5305 free(tmp_branch_name);
5309 static const struct got_error *
5310 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5311 char **logmsg, void *arg)
5317 static const struct got_error *
5318 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5319 const char *path, struct got_object_id *blob_id,
5320 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5321 int dirfd, const char *de_name)
5326 struct collect_merged_paths_arg {
5327 got_worktree_checkout_cb progress_cb;
5329 struct got_pathlist_head *merged_paths;
5332 static const struct got_error *
5333 collect_merged_paths(void *arg, unsigned char status, const char *path)
5335 const struct got_error *err;
5336 struct collect_merged_paths_arg *a = arg;
5338 struct got_pathlist_entry *new;
5340 err = (*a->progress_cb)(a->progress_arg, status, path);
5344 if (status != GOT_STATUS_MERGE &&
5345 status != GOT_STATUS_ADD &&
5346 status != GOT_STATUS_DELETE &&
5347 status != GOT_STATUS_CONFLICT)
5352 return got_error_from_errno("strdup");
5354 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5355 if (err || new == NULL)
5361 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5363 struct got_pathlist_entry *pe;
5365 TAILQ_FOREACH(pe, merged_paths, entry)
5366 free((char *)pe->path);
5368 got_pathlist_free(merged_paths);
5371 static const struct got_error *
5372 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5373 int is_rebase, struct got_repository *repo)
5375 const struct got_error *err;
5376 struct got_reference *commit_ref = NULL;
5378 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5380 if (err->code != GOT_ERR_NOT_REF)
5382 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
5385 err = got_ref_write(commit_ref, repo);
5388 } else if (is_rebase) {
5389 struct got_object_id *stored_id;
5392 err = got_ref_resolve(&stored_id, repo, commit_ref);
5395 cmp = got_object_id_cmp(commit_id, stored_id);
5398 err = got_error(GOT_ERR_REBASE_COMMITID);
5404 got_ref_close(commit_ref);
5408 static const struct got_error *
5409 rebase_merge_files(struct got_pathlist_head *merged_paths,
5410 const char *commit_ref_name, struct got_worktree *worktree,
5411 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
5412 struct got_object_id *commit_id, struct got_repository *repo,
5413 got_worktree_checkout_cb progress_cb, void *progress_arg,
5414 got_cancel_cb cancel_cb, void *cancel_arg)
5416 const struct got_error *err;
5417 struct got_reference *commit_ref = NULL;
5418 struct collect_merged_paths_arg cmp_arg;
5419 char *fileindex_path;
5421 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5423 err = get_fileindex_path(&fileindex_path, worktree);
5427 cmp_arg.progress_cb = progress_cb;
5428 cmp_arg.progress_arg = progress_arg;
5429 cmp_arg.merged_paths = merged_paths;
5430 err = merge_files(worktree, fileindex, fileindex_path,
5431 parent_commit_id, commit_id, repo, collect_merged_paths,
5432 &cmp_arg, cancel_cb, cancel_arg);
5434 got_ref_close(commit_ref);
5438 const struct got_error *
5439 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
5440 struct got_worktree *worktree, struct got_fileindex *fileindex,
5441 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5442 struct got_repository *repo,
5443 got_worktree_checkout_cb progress_cb, void *progress_arg,
5444 got_cancel_cb cancel_cb, void *cancel_arg)
5446 const struct got_error *err;
5447 char *commit_ref_name;
5449 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5453 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
5457 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5458 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5459 progress_arg, cancel_cb, cancel_arg);
5461 free(commit_ref_name);
5465 const struct got_error *
5466 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
5467 struct got_worktree *worktree, struct got_fileindex *fileindex,
5468 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5469 struct got_repository *repo,
5470 got_worktree_checkout_cb progress_cb, void *progress_arg,
5471 got_cancel_cb cancel_cb, void *cancel_arg)
5473 const struct got_error *err;
5474 char *commit_ref_name;
5476 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5480 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
5484 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5485 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5486 progress_arg, cancel_cb, cancel_arg);
5488 free(commit_ref_name);
5492 static const struct got_error *
5493 rebase_commit(struct got_object_id **new_commit_id,
5494 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
5495 struct got_worktree *worktree, struct got_fileindex *fileindex,
5496 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
5497 const char *new_logmsg, struct got_repository *repo)
5499 const struct got_error *err, *sync_err;
5500 struct got_pathlist_head commitable_paths;
5501 struct collect_commitables_arg cc_arg;
5502 char *fileindex_path = NULL;
5503 struct got_reference *head_ref = NULL;
5504 struct got_object_id *head_commit_id = NULL;
5505 char *logmsg = NULL;
5507 TAILQ_INIT(&commitable_paths);
5508 *new_commit_id = NULL;
5510 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5512 err = get_fileindex_path(&fileindex_path, worktree);
5516 cc_arg.commitable_paths = &commitable_paths;
5517 cc_arg.worktree = worktree;
5519 cc_arg.have_staged_files = 0;
5521 * If possible get the status of individual files directly to
5522 * avoid crawling the entire work tree once per rebased commit.
5523 * TODO: Ideally, merged_paths would contain a list of commitables
5524 * we could use so we could skip worktree_status() entirely.
5527 struct got_pathlist_entry *pe;
5528 TAILQ_FOREACH(pe, merged_paths, entry) {
5529 err = worktree_status(worktree, pe->path, fileindex,
5530 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
5536 err = worktree_status(worktree, "", fileindex, repo,
5537 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5542 if (TAILQ_EMPTY(&commitable_paths)) {
5543 /* No-op change; commit will be elided. */
5544 err = got_ref_delete(commit_ref, repo);
5547 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5551 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5555 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5560 logmsg = strdup(new_logmsg);
5561 if (logmsg == NULL) {
5562 err = got_error_from_errno("strdup");
5566 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
5571 /* NB: commit_worktree will call free(logmsg) */
5572 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
5573 worktree, got_object_commit_get_author(orig_commit),
5574 got_object_commit_get_committer(orig_commit),
5575 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
5579 err = got_ref_change_ref(tmp_branch, *new_commit_id);
5583 err = got_ref_delete(commit_ref, repo);
5587 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5589 sync_err = sync_fileindex(fileindex, fileindex_path);
5590 if (sync_err && err == NULL)
5593 free(fileindex_path);
5594 free(head_commit_id);
5596 got_ref_close(head_ref);
5598 free(*new_commit_id);
5599 *new_commit_id = NULL;
5604 const struct got_error *
5605 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
5606 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5607 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5608 struct got_commit_object *orig_commit,
5609 struct got_object_id *orig_commit_id, struct got_repository *repo)
5611 const struct got_error *err;
5612 char *commit_ref_name;
5613 struct got_reference *commit_ref = NULL;
5614 struct got_object_id *commit_id = NULL;
5616 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5620 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5623 err = got_ref_resolve(&commit_id, repo, commit_ref);
5626 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5627 err = got_error(GOT_ERR_REBASE_COMMITID);
5631 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5632 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
5635 got_ref_close(commit_ref);
5636 free(commit_ref_name);
5641 const struct got_error *
5642 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
5643 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5644 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5645 struct got_commit_object *orig_commit,
5646 struct got_object_id *orig_commit_id, const char *new_logmsg,
5647 struct got_repository *repo)
5649 const struct got_error *err;
5650 char *commit_ref_name;
5651 struct got_reference *commit_ref = NULL;
5653 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5657 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5661 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5662 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
5665 got_ref_close(commit_ref);
5666 free(commit_ref_name);
5670 const struct got_error *
5671 got_worktree_rebase_postpone(struct got_worktree *worktree,
5672 struct got_fileindex *fileindex)
5675 got_fileindex_free(fileindex);
5676 return lock_worktree(worktree, LOCK_SH);
5679 static const struct got_error *
5680 delete_ref(const char *name, struct got_repository *repo)
5682 const struct got_error *err;
5683 struct got_reference *ref;
5685 err = got_ref_open(&ref, repo, name, 0);
5687 if (err->code == GOT_ERR_NOT_REF)
5692 err = got_ref_delete(ref, repo);
5697 static const struct got_error *
5698 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
5700 const struct got_error *err;
5701 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5702 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5704 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5707 err = delete_ref(tmp_branch_name, repo);
5711 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5714 err = delete_ref(new_base_branch_ref_name, repo);
5718 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5721 err = delete_ref(branch_ref_name, repo);
5725 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5728 err = delete_ref(commit_ref_name, repo);
5733 free(tmp_branch_name);
5734 free(new_base_branch_ref_name);
5735 free(branch_ref_name);
5736 free(commit_ref_name);
5740 const struct got_error *
5741 got_worktree_rebase_complete(struct got_worktree *worktree,
5742 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
5743 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
5744 struct got_repository *repo)
5746 const struct got_error *err, *unlockerr;
5747 struct got_object_id *new_head_commit_id = NULL;
5749 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5753 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
5757 err = got_ref_write(rebased_branch, repo);
5761 err = got_worktree_set_head_ref(worktree, rebased_branch);
5765 err = delete_rebase_refs(worktree, repo);
5768 got_fileindex_free(fileindex);
5769 free(new_head_commit_id);
5770 unlockerr = lock_worktree(worktree, LOCK_SH);
5771 if (unlockerr && err == NULL)
5776 const struct got_error *
5777 got_worktree_rebase_abort(struct got_worktree *worktree,
5778 struct got_fileindex *fileindex, struct got_repository *repo,
5779 struct got_reference *new_base_branch,
5780 got_worktree_checkout_cb progress_cb, void *progress_arg)
5782 const struct got_error *err, *unlockerr, *sync_err;
5783 struct got_reference *resolved = NULL;
5784 struct got_object_id *commit_id = NULL;
5785 char *fileindex_path = NULL;
5786 struct revert_file_args rfa;
5787 struct got_object_id *tree_id = NULL;
5789 err = lock_worktree(worktree, LOCK_EX);
5793 err = got_ref_open(&resolved, repo,
5794 got_ref_get_symref_target(new_base_branch), 0);
5798 err = got_worktree_set_head_ref(worktree, resolved);
5803 * XXX commits to the base branch could have happened while
5804 * we were busy rebasing; should we store the original commit ID
5805 * when rebase begins and read it back here?
5807 err = got_ref_resolve(&commit_id, repo, resolved);
5811 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5815 err = got_object_id_by_path(&tree_id, repo,
5816 worktree->base_commit_id, worktree->path_prefix);
5820 err = delete_rebase_refs(worktree, repo);
5824 err = get_fileindex_path(&fileindex_path, worktree);
5828 rfa.worktree = worktree;
5829 rfa.fileindex = fileindex;
5830 rfa.progress_cb = progress_cb;
5831 rfa.progress_arg = progress_arg;
5832 rfa.patch_cb = NULL;
5833 rfa.patch_arg = NULL;
5835 err = worktree_status(worktree, "", fileindex, repo,
5836 revert_file, &rfa, NULL, NULL, 0, 0);
5840 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5841 repo, progress_cb, progress_arg, NULL, NULL);
5843 sync_err = sync_fileindex(fileindex, fileindex_path);
5844 if (sync_err && err == NULL)
5847 got_ref_close(resolved);
5851 got_fileindex_free(fileindex);
5852 free(fileindex_path);
5854 unlockerr = lock_worktree(worktree, LOCK_SH);
5855 if (unlockerr && err == NULL)
5860 const struct got_error *
5861 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
5862 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
5863 struct got_fileindex **fileindex, struct got_worktree *worktree,
5864 struct got_repository *repo)
5866 const struct got_error *err = NULL;
5867 char *tmp_branch_name = NULL;
5868 char *branch_ref_name = NULL;
5869 char *base_commit_ref_name = NULL;
5870 char *fileindex_path = NULL;
5871 struct check_rebase_ok_arg ok_arg;
5872 struct got_reference *wt_branch = NULL;
5873 struct got_reference *base_commit_ref = NULL;
5877 *base_commit_id = NULL;
5880 err = lock_worktree(worktree, LOCK_EX);
5884 err = open_fileindex(fileindex, &fileindex_path, worktree);
5888 ok_arg.worktree = worktree;
5890 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5895 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5899 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5903 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5908 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5913 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
5917 err = got_ref_write(*branch_ref, repo);
5921 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
5922 worktree->base_commit_id);
5925 err = got_ref_write(base_commit_ref, repo);
5928 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
5929 if (*base_commit_id == NULL) {
5930 err = got_error_from_errno("got_object_id_dup");
5934 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5935 worktree->base_commit_id);
5938 err = got_ref_write(*tmp_branch, repo);
5942 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5946 free(fileindex_path);
5947 free(tmp_branch_name);
5948 free(branch_ref_name);
5949 free(base_commit_ref_name);
5951 got_ref_close(wt_branch);
5954 got_ref_close(*branch_ref);
5958 got_ref_close(*tmp_branch);
5961 free(*base_commit_id);
5963 got_fileindex_free(*fileindex);
5966 lock_worktree(worktree, LOCK_SH);
5971 const struct got_error *
5972 got_worktree_histedit_postpone(struct got_worktree *worktree,
5973 struct got_fileindex *fileindex)
5976 got_fileindex_free(fileindex);
5977 return lock_worktree(worktree, LOCK_SH);
5980 const struct got_error *
5981 got_worktree_histedit_in_progress(int *in_progress,
5982 struct got_worktree *worktree)
5984 const struct got_error *err;
5985 char *tmp_branch_name = NULL;
5987 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5991 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5992 free(tmp_branch_name);
5996 const struct got_error *
5997 got_worktree_histedit_continue(struct got_object_id **commit_id,
5998 struct got_reference **tmp_branch, struct got_reference **branch_ref,
5999 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6000 struct got_worktree *worktree, struct got_repository *repo)
6002 const struct got_error *err;
6003 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6004 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6005 struct got_reference *commit_ref = NULL;
6006 struct got_reference *base_commit_ref = NULL;
6007 char *fileindex_path = NULL;
6008 int have_staged_files = 0;
6012 *base_commit_id = NULL;
6015 err = lock_worktree(worktree, LOCK_EX);
6019 err = open_fileindex(fileindex, &fileindex_path, worktree);
6023 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6024 &have_staged_files);
6025 if (err && err->code != GOT_ERR_CANCELLED)
6027 if (have_staged_files) {
6028 err = got_error(GOT_ERR_STAGED_PATHS);
6032 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6036 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6040 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6044 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6049 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6053 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6056 err = got_ref_resolve(commit_id, repo, commit_ref);
6060 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6063 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6067 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6071 free(commit_ref_name);
6072 free(branch_ref_name);
6073 free(fileindex_path);
6075 got_ref_close(commit_ref);
6076 if (base_commit_ref)
6077 got_ref_close(base_commit_ref);
6081 free(*base_commit_id);
6082 *base_commit_id = NULL;
6084 got_ref_close(*tmp_branch);
6088 got_fileindex_free(*fileindex);
6091 lock_worktree(worktree, LOCK_EX);
6096 static const struct got_error *
6097 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6099 const struct got_error *err;
6100 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6101 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6103 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6106 err = delete_ref(tmp_branch_name, repo);
6110 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6114 err = delete_ref(base_commit_ref_name, repo);
6118 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6121 err = delete_ref(branch_ref_name, repo);
6125 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6128 err = delete_ref(commit_ref_name, repo);
6132 free(tmp_branch_name);
6133 free(base_commit_ref_name);
6134 free(branch_ref_name);
6135 free(commit_ref_name);
6139 const struct got_error *
6140 got_worktree_histedit_abort(struct got_worktree *worktree,
6141 struct got_fileindex *fileindex, struct got_repository *repo,
6142 struct got_reference *branch, struct got_object_id *base_commit_id,
6143 got_worktree_checkout_cb progress_cb, void *progress_arg)
6145 const struct got_error *err, *unlockerr, *sync_err;
6146 struct got_reference *resolved = NULL;
6147 char *fileindex_path = NULL;
6148 struct got_object_id *tree_id = NULL;
6149 struct revert_file_args rfa;
6151 err = lock_worktree(worktree, LOCK_EX);
6155 err = got_ref_open(&resolved, repo,
6156 got_ref_get_symref_target(branch), 0);
6160 err = got_worktree_set_head_ref(worktree, resolved);
6164 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6168 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6169 worktree->path_prefix);
6173 err = delete_histedit_refs(worktree, repo);
6177 err = get_fileindex_path(&fileindex_path, worktree);
6181 rfa.worktree = worktree;
6182 rfa.fileindex = fileindex;
6183 rfa.progress_cb = progress_cb;
6184 rfa.progress_arg = progress_arg;
6185 rfa.patch_cb = NULL;
6186 rfa.patch_arg = NULL;
6188 err = worktree_status(worktree, "", fileindex, repo,
6189 revert_file, &rfa, NULL, NULL, 0, 0);
6193 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6194 repo, progress_cb, progress_arg, NULL, NULL);
6196 sync_err = sync_fileindex(fileindex, fileindex_path);
6197 if (sync_err && err == NULL)
6200 got_ref_close(resolved);
6202 free(fileindex_path);
6204 unlockerr = lock_worktree(worktree, LOCK_SH);
6205 if (unlockerr && err == NULL)
6210 const struct got_error *
6211 got_worktree_histedit_complete(struct got_worktree *worktree,
6212 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6213 struct got_reference *edited_branch, struct got_repository *repo)
6215 const struct got_error *err, *unlockerr;
6216 struct got_object_id *new_head_commit_id = NULL;
6217 struct got_reference *resolved = NULL;
6219 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6223 err = got_ref_open(&resolved, repo,
6224 got_ref_get_symref_target(edited_branch), 0);
6228 err = got_ref_change_ref(resolved, new_head_commit_id);
6232 err = got_ref_write(resolved, repo);
6236 err = got_worktree_set_head_ref(worktree, resolved);
6240 err = delete_histedit_refs(worktree, repo);
6243 got_fileindex_free(fileindex);
6244 free(new_head_commit_id);
6245 unlockerr = lock_worktree(worktree, LOCK_SH);
6246 if (unlockerr && err == NULL)
6251 const struct got_error *
6252 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6253 struct got_object_id *commit_id, struct got_repository *repo)
6255 const struct got_error *err;
6256 char *commit_ref_name;
6258 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6262 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6266 err = delete_ref(commit_ref_name, repo);
6268 free(commit_ref_name);
6272 const struct got_error *
6273 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6274 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6275 struct got_worktree *worktree, const char *refname,
6276 struct got_repository *repo)
6278 const struct got_error *err = NULL;
6279 char *fileindex_path = NULL;
6280 struct check_rebase_ok_arg ok_arg;
6284 *base_branch_ref = NULL;
6286 err = lock_worktree(worktree, LOCK_EX);
6290 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6291 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6292 "cannot integrate a branch into itself; "
6293 "update -b or different branch name required");
6297 err = open_fileindex(fileindex, &fileindex_path, worktree);
6301 /* Preconditions are the same as for rebase. */
6302 ok_arg.worktree = worktree;
6304 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6309 err = got_ref_open(branch_ref, repo, refname, 1);
6313 err = got_ref_open(base_branch_ref, repo,
6314 got_worktree_get_head_ref_name(worktree), 1);
6318 got_ref_close(*branch_ref);
6321 if (*base_branch_ref) {
6322 got_ref_close(*base_branch_ref);
6323 *base_branch_ref = NULL;
6326 got_fileindex_free(*fileindex);
6329 lock_worktree(worktree, LOCK_SH);
6334 const struct got_error *
6335 got_worktree_integrate_continue(struct got_worktree *worktree,
6336 struct got_fileindex *fileindex, struct got_repository *repo,
6337 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6338 got_worktree_checkout_cb progress_cb, void *progress_arg,
6339 got_cancel_cb cancel_cb, void *cancel_arg)
6341 const struct got_error *err = NULL, *sync_err, *unlockerr;
6342 char *fileindex_path = NULL;
6343 struct got_object_id *tree_id = NULL, *commit_id = NULL;
6345 err = get_fileindex_path(&fileindex_path, worktree);
6349 err = got_ref_resolve(&commit_id, repo, branch_ref);
6353 err = got_object_id_by_path(&tree_id, repo, commit_id,
6354 worktree->path_prefix);
6358 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6362 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6363 progress_cb, progress_arg, cancel_cb, cancel_arg);
6367 err = got_ref_change_ref(base_branch_ref, commit_id);
6371 err = got_ref_write(base_branch_ref, repo);
6373 sync_err = sync_fileindex(fileindex, fileindex_path);
6374 if (sync_err && err == NULL)
6378 unlockerr = got_ref_unlock(branch_ref);
6379 if (unlockerr && err == NULL)
6381 got_ref_close(branch_ref);
6383 unlockerr = got_ref_unlock(base_branch_ref);
6384 if (unlockerr && err == NULL)
6386 got_ref_close(base_branch_ref);
6388 got_fileindex_free(fileindex);
6389 free(fileindex_path);
6392 unlockerr = lock_worktree(worktree, LOCK_SH);
6393 if (unlockerr && err == NULL)
6398 const struct got_error *
6399 got_worktree_integrate_abort(struct got_worktree *worktree,
6400 struct got_fileindex *fileindex, struct got_repository *repo,
6401 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
6403 const struct got_error *err = NULL, *unlockerr = NULL;
6405 got_fileindex_free(fileindex);
6407 err = lock_worktree(worktree, LOCK_SH);
6409 unlockerr = got_ref_unlock(branch_ref);
6410 if (unlockerr && err == NULL)
6412 got_ref_close(branch_ref);
6414 unlockerr = got_ref_unlock(base_branch_ref);
6415 if (unlockerr && err == NULL)
6417 got_ref_close(base_branch_ref);
6422 struct check_stage_ok_arg {
6423 struct got_object_id *head_commit_id;
6424 struct got_worktree *worktree;
6425 struct got_fileindex *fileindex;
6426 struct got_repository *repo;
6430 const struct got_error *
6431 check_stage_ok(void *arg, unsigned char status,
6432 unsigned char staged_status, const char *relpath,
6433 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6434 struct got_object_id *commit_id, int dirfd, const char *de_name)
6436 struct check_stage_ok_arg *a = arg;
6437 const struct got_error *err = NULL;
6438 struct got_fileindex_entry *ie;
6439 struct got_object_id base_commit_id;
6440 struct got_object_id *base_commit_idp = NULL;
6441 char *in_repo_path = NULL, *p;
6443 if (status == GOT_STATUS_UNVERSIONED ||
6444 status == GOT_STATUS_NO_CHANGE)
6446 if (status == GOT_STATUS_NONEXISTENT)
6447 return got_error_set_errno(ENOENT, relpath);
6449 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6451 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6453 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
6454 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
6456 return got_error_from_errno("asprintf");
6458 if (got_fileindex_entry_has_commit(ie)) {
6459 memcpy(base_commit_id.sha1, ie->commit_sha1,
6460 SHA1_DIGEST_LENGTH);
6461 base_commit_idp = &base_commit_id;
6464 if (status == GOT_STATUS_CONFLICT) {
6465 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
6467 } else if (status != GOT_STATUS_ADD &&
6468 status != GOT_STATUS_MODIFY &&
6469 status != GOT_STATUS_DELETE) {
6470 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
6474 a->have_changes = 1;
6479 err = check_out_of_date(p, status, staged_status,
6480 blob_id, base_commit_idp, a->head_commit_id, a->repo,
6481 GOT_ERR_STAGE_OUT_OF_DATE);
6487 struct stage_path_arg {
6488 struct got_worktree *worktree;
6489 struct got_fileindex *fileindex;
6490 struct got_repository *repo;
6491 got_worktree_status_cb status_cb;
6493 got_worktree_patch_cb patch_cb;
6495 int staged_something;
6498 static const struct got_error *
6499 stage_path(void *arg, unsigned char status,
6500 unsigned char staged_status, const char *relpath,
6501 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6502 struct got_object_id *commit_id, int dirfd, const char *de_name)
6504 struct stage_path_arg *a = arg;
6505 const struct got_error *err = NULL;
6506 struct got_fileindex_entry *ie;
6507 char *ondisk_path = NULL, *path_content = NULL;
6509 struct got_object_id *new_staged_blob_id = NULL;
6511 if (status == GOT_STATUS_UNVERSIONED)
6514 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6516 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6518 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
6520 return got_error_from_errno("asprintf");
6523 case GOT_STATUS_ADD:
6524 case GOT_STATUS_MODIFY:
6526 if (status == GOT_STATUS_ADD) {
6527 int choice = GOT_PATCH_CHOICE_NONE;
6528 err = (*a->patch_cb)(&choice, a->patch_arg,
6529 status, ie->path, NULL, 1, 1);
6532 if (choice != GOT_PATCH_CHOICE_YES)
6535 err = create_patched_content(&path_content, 0,
6536 staged_blob_id ? staged_blob_id : blob_id,
6537 ondisk_path, dirfd, de_name, ie->path,
6538 a->repo, a->patch_cb, a->patch_arg);
6539 if (err || path_content == NULL)
6543 err = got_object_blob_create(&new_staged_blob_id,
6544 path_content ? path_content : ondisk_path, a->repo);
6547 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
6548 SHA1_DIGEST_LENGTH);
6549 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
6550 stage = GOT_FILEIDX_STAGE_ADD;
6552 stage = GOT_FILEIDX_STAGE_MODIFY;
6553 got_fileindex_entry_stage_set(ie, stage);
6554 a->staged_something = 1;
6555 if (a->status_cb == NULL)
6557 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6558 get_staged_status(ie), relpath, blob_id,
6559 new_staged_blob_id, NULL, dirfd, de_name);
6561 case GOT_STATUS_DELETE:
6562 if (staged_status == GOT_STATUS_DELETE)
6565 int choice = GOT_PATCH_CHOICE_NONE;
6566 err = (*a->patch_cb)(&choice, a->patch_arg, status,
6567 ie->path, NULL, 1, 1);
6570 if (choice == GOT_PATCH_CHOICE_NO)
6572 if (choice != GOT_PATCH_CHOICE_YES) {
6573 err = got_error(GOT_ERR_PATCH_CHOICE);
6577 stage = GOT_FILEIDX_STAGE_DELETE;
6578 got_fileindex_entry_stage_set(ie, stage);
6579 a->staged_something = 1;
6580 if (a->status_cb == NULL)
6582 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6583 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
6586 case GOT_STATUS_NO_CHANGE:
6588 case GOT_STATUS_CONFLICT:
6589 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
6591 case GOT_STATUS_NONEXISTENT:
6592 err = got_error_set_errno(ENOENT, relpath);
6595 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
6599 if (path_content && unlink(path_content) == -1 && err == NULL)
6600 err = got_error_from_errno2("unlink", path_content);
6603 free(new_staged_blob_id);
6607 const struct got_error *
6608 got_worktree_stage(struct got_worktree *worktree,
6609 struct got_pathlist_head *paths,
6610 got_worktree_status_cb status_cb, void *status_arg,
6611 got_worktree_patch_cb patch_cb, void *patch_arg,
6612 struct got_repository *repo)
6614 const struct got_error *err = NULL, *sync_err, *unlockerr;
6615 struct got_pathlist_entry *pe;
6616 struct got_fileindex *fileindex = NULL;
6617 char *fileindex_path = NULL;
6618 struct got_reference *head_ref = NULL;
6619 struct got_object_id *head_commit_id = NULL;
6620 struct check_stage_ok_arg oka;
6621 struct stage_path_arg spa;
6623 err = lock_worktree(worktree, LOCK_EX);
6627 err = got_ref_open(&head_ref, repo,
6628 got_worktree_get_head_ref_name(worktree), 0);
6631 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6634 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6638 /* Check pre-conditions before staging anything. */
6639 oka.head_commit_id = head_commit_id;
6640 oka.worktree = worktree;
6641 oka.fileindex = fileindex;
6643 oka.have_changes = 0;
6644 TAILQ_FOREACH(pe, paths, entry) {
6645 err = worktree_status(worktree, pe->path, fileindex, repo,
6646 check_stage_ok, &oka, NULL, NULL, 0, 0);
6650 if (!oka.have_changes) {
6651 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6655 spa.worktree = worktree;
6656 spa.fileindex = fileindex;
6658 spa.patch_cb = patch_cb;
6659 spa.patch_arg = patch_arg;
6660 spa.status_cb = status_cb;
6661 spa.status_arg = status_arg;
6662 spa.staged_something = 0;
6663 TAILQ_FOREACH(pe, paths, entry) {
6664 err = worktree_status(worktree, pe->path, fileindex, repo,
6665 stage_path, &spa, NULL, NULL, 0, 0);
6669 if (!spa.staged_something) {
6670 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6674 sync_err = sync_fileindex(fileindex, fileindex_path);
6675 if (sync_err && err == NULL)
6679 got_ref_close(head_ref);
6680 free(head_commit_id);
6681 free(fileindex_path);
6683 got_fileindex_free(fileindex);
6684 unlockerr = lock_worktree(worktree, LOCK_SH);
6685 if (unlockerr && err == NULL)
6690 struct unstage_path_arg {
6691 struct got_worktree *worktree;
6692 struct got_fileindex *fileindex;
6693 struct got_repository *repo;
6694 got_worktree_checkout_cb progress_cb;
6696 got_worktree_patch_cb patch_cb;
6700 static const struct got_error *
6701 create_unstaged_content(char **path_unstaged_content,
6702 char **path_new_staged_content, struct got_object_id *blob_id,
6703 struct got_object_id *staged_blob_id, const char *relpath,
6704 struct got_repository *repo,
6705 got_worktree_patch_cb patch_cb, void *patch_arg)
6707 const struct got_error *err;
6708 struct got_blob_object *blob = NULL, *staged_blob = NULL;
6709 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
6710 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
6711 struct stat sb1, sb2;
6712 struct got_diff_changes *changes = NULL;
6713 struct got_diff_state *ds = NULL;
6714 struct got_diff_args *args = NULL;
6715 struct got_diff_change *change;
6716 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
6717 int have_content = 0, have_rejected_content = 0;
6719 *path_unstaged_content = NULL;
6720 *path_new_staged_content = NULL;
6722 err = got_object_id_str(&label1, blob_id);
6725 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
6729 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
6733 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
6737 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
6741 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
6745 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
6749 if (stat(path1, &sb1) == -1) {
6750 err = got_error_from_errno2("stat", path1);
6754 if (stat(path2, &sb2) == -1) {
6755 err = got_error_from_errno2("stat", path2);
6759 err = got_diff_files(&changes, &ds, &args, &diff_flags,
6760 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
6764 err = got_opentemp_named(path_unstaged_content, &outfile,
6765 "got-unstaged-content");
6768 err = got_opentemp_named(path_new_staged_content, &rejectfile,
6769 "got-new-staged-content");
6773 if (fseek(f1, 0L, SEEK_SET) == -1) {
6774 err = got_ferror(f1, GOT_ERR_IO);
6777 if (fseek(f2, 0L, SEEK_SET) == -1) {
6778 err = got_ferror(f2, GOT_ERR_IO);
6781 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
6783 err = apply_or_reject_change(&choice, change, ++n,
6784 changes->nchanges, ds, args, diff_flags, relpath,
6785 f1, f2, &line_cur1, &line_cur2,
6786 outfile, rejectfile, patch_cb, patch_arg);
6789 if (choice == GOT_PATCH_CHOICE_YES)
6792 have_rejected_content = 1;
6793 if (choice == GOT_PATCH_CHOICE_QUIT)
6796 if (have_content || have_rejected_content)
6797 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
6798 outfile, rejectfile);
6802 got_object_blob_close(blob);
6804 got_object_blob_close(staged_blob);
6805 if (f1 && fclose(f1) == EOF && err == NULL)
6806 err = got_error_from_errno2("fclose", path1);
6807 if (f2 && fclose(f2) == EOF && err == NULL)
6808 err = got_error_from_errno2("fclose", path2);
6809 if (outfile && fclose(outfile) == EOF && err == NULL)
6810 err = got_error_from_errno2("fclose", *path_unstaged_content);
6811 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
6812 err = got_error_from_errno2("fclose", *path_new_staged_content);
6813 if (path1 && unlink(path1) == -1 && err == NULL)
6814 err = got_error_from_errno2("unlink", path1);
6815 if (path2 && unlink(path2) == -1 && err == NULL)
6816 err = got_error_from_errno2("unlink", path2);
6817 if (err || !have_content) {
6818 if (*path_unstaged_content &&
6819 unlink(*path_unstaged_content) == -1 && err == NULL)
6820 err = got_error_from_errno2("unlink",
6821 *path_unstaged_content);
6822 free(*path_unstaged_content);
6823 *path_unstaged_content = NULL;
6825 if (err || !have_rejected_content) {
6826 if (*path_new_staged_content &&
6827 unlink(*path_new_staged_content) == -1 && err == NULL)
6828 err = got_error_from_errno2("unlink",
6829 *path_new_staged_content);
6830 free(*path_new_staged_content);
6831 *path_new_staged_content = NULL;
6835 got_diff_state_free(ds);
6839 got_diff_free_changes(changes);
6845 static const struct got_error *
6846 unstage_path(void *arg, unsigned char status,
6847 unsigned char staged_status, const char *relpath,
6848 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6849 struct got_object_id *commit_id, int dirfd, const char *de_name)
6851 const struct got_error *err = NULL;
6852 struct unstage_path_arg *a = arg;
6853 struct got_fileindex_entry *ie;
6854 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
6855 char *ondisk_path = NULL, *path_unstaged_content = NULL;
6856 char *path_new_staged_content = NULL;
6857 char *id_str = NULL, *label_orig = NULL;
6858 int local_changes_subsumed;
6861 if (staged_status != GOT_STATUS_ADD &&
6862 staged_status != GOT_STATUS_MODIFY &&
6863 staged_status != GOT_STATUS_DELETE)
6866 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6868 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6870 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
6872 return got_error_from_errno("asprintf");
6874 err = got_object_id_str(&id_str,
6875 commit_id ? commit_id : a->worktree->base_commit_id);
6878 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
6880 err = got_error_from_errno("asprintf");
6884 switch (staged_status) {
6885 case GOT_STATUS_MODIFY:
6886 err = got_object_open_as_blob(&blob_base, a->repo,
6891 case GOT_STATUS_ADD:
6893 if (staged_status == GOT_STATUS_ADD) {
6894 int choice = GOT_PATCH_CHOICE_NONE;
6895 err = (*a->patch_cb)(&choice, a->patch_arg,
6896 staged_status, ie->path, NULL, 1, 1);
6899 if (choice != GOT_PATCH_CHOICE_YES)
6902 err = create_unstaged_content(
6903 &path_unstaged_content,
6904 &path_new_staged_content, blob_id,
6905 staged_blob_id, ie->path, a->repo,
6906 a->patch_cb, a->patch_arg);
6907 if (err || path_unstaged_content == NULL)
6909 if (path_new_staged_content) {
6910 err = got_object_blob_create(
6912 path_new_staged_content,
6916 memcpy(ie->staged_blob_sha1,
6917 staged_blob_id->sha1,
6918 SHA1_DIGEST_LENGTH);
6920 err = merge_file(&local_changes_subsumed,
6921 a->worktree, blob_base, ondisk_path,
6922 relpath, got_fileindex_perms_to_st(ie),
6923 path_unstaged_content, label_orig,
6924 "unstaged", a->repo, a->progress_cb,
6927 path_new_staged_content == NULL)
6928 got_fileindex_entry_stage_set(ie,
6929 GOT_FILEIDX_STAGE_NONE);
6930 break; /* Done with this file. */
6933 err = got_object_open_as_blob(&blob_staged, a->repo,
6934 staged_blob_id, 8192);
6937 err = merge_blob(&local_changes_subsumed, a->worktree,
6938 blob_base, ondisk_path, relpath,
6939 got_fileindex_perms_to_st(ie), label_orig, blob_staged,
6940 commit_id ? commit_id : a->worktree->base_commit_id,
6941 a->repo, a->progress_cb, a->progress_arg);
6943 got_fileindex_entry_stage_set(ie,
6944 GOT_FILEIDX_STAGE_NONE);
6946 case GOT_STATUS_DELETE:
6948 int choice = GOT_PATCH_CHOICE_NONE;
6949 err = (*a->patch_cb)(&choice, a->patch_arg,
6950 staged_status, ie->path, NULL, 1, 1);
6953 if (choice == GOT_PATCH_CHOICE_NO)
6955 if (choice != GOT_PATCH_CHOICE_YES) {
6956 err = got_error(GOT_ERR_PATCH_CHOICE);
6960 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
6961 err = get_file_status(&status, &sb, ie, ondisk_path,
6962 dirfd, de_name, a->repo);
6965 err = (*a->progress_cb)(a->progress_arg, status, relpath);
6970 if (path_unstaged_content &&
6971 unlink(path_unstaged_content) == -1 && err == NULL)
6972 err = got_error_from_errno2("unlink", path_unstaged_content);
6973 if (path_new_staged_content &&
6974 unlink(path_new_staged_content) == -1 && err == NULL)
6975 err = got_error_from_errno2("unlink", path_new_staged_content);
6976 free(path_unstaged_content);
6977 free(path_new_staged_content);
6979 got_object_blob_close(blob_base);
6981 got_object_blob_close(blob_staged);
6987 const struct got_error *
6988 got_worktree_unstage(struct got_worktree *worktree,
6989 struct got_pathlist_head *paths,
6990 got_worktree_checkout_cb progress_cb, void *progress_arg,
6991 got_worktree_patch_cb patch_cb, void *patch_arg,
6992 struct got_repository *repo)
6994 const struct got_error *err = NULL, *sync_err, *unlockerr;
6995 struct got_pathlist_entry *pe;
6996 struct got_fileindex *fileindex = NULL;
6997 char *fileindex_path = NULL;
6998 struct unstage_path_arg upa;
7000 err = lock_worktree(worktree, LOCK_EX);
7004 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7008 upa.worktree = worktree;
7009 upa.fileindex = fileindex;
7011 upa.progress_cb = progress_cb;
7012 upa.progress_arg = progress_arg;
7013 upa.patch_cb = patch_cb;
7014 upa.patch_arg = patch_arg;
7015 TAILQ_FOREACH(pe, paths, entry) {
7016 err = worktree_status(worktree, pe->path, fileindex, repo,
7017 unstage_path, &upa, NULL, NULL, 0, 0);
7022 sync_err = sync_fileindex(fileindex, fileindex_path);
7023 if (sync_err && err == NULL)
7026 free(fileindex_path);
7028 got_fileindex_free(fileindex);
7029 unlockerr = lock_worktree(worktree, LOCK_SH);
7030 if (unlockerr && err == NULL)