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 err = got_error_from_errno2("realpath", target_path);
1015 /* Only allow symlinks pointing at paths within the work tree. */
1016 if (!got_path_is_child(resolved_path ? resolved_path : (abspath ?
1017 abspath : target_path), worktree->root_path,
1018 strlen(worktree->root_path))) {
1019 /* install as a regular file */
1020 got_object_blob_rewind(blob);
1021 err = install_blob(worktree, ondisk_path, path,
1022 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1023 restoring_missing_file, reverting_versioned_file,
1024 repo, progress_cb, progress_arg);
1028 if (symlink(target_path, ondisk_path) == -1) {
1029 if (errno == EEXIST) {
1032 char etarget[PATH_MAX];
1033 if (lstat(ondisk_path, &sb) == -1) {
1034 err = got_error_from_errno2("lstat",
1038 if (!S_ISLNK(sb.st_mode)) {
1039 err = got_error_path(ondisk_path,
1040 GOT_ERR_FILE_OBSTRUCTED);
1043 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1045 err = got_error_from_errno2("readlink",
1049 if (elen == target_len &&
1050 memcmp(etarget, target_path, target_len) == 0) {
1051 err = NULL; /* nothing to do */
1054 if (unlink(ondisk_path) == -1) {
1055 err = got_error_from_errno2("unlink",
1059 if (symlink(target_path, ondisk_path) == -1) {
1060 err = got_error_from_errno3("symlink",
1061 target_path, ondisk_path);
1065 err = (*progress_cb)(progress_arg,
1066 GOT_STATUS_UPDATE, path);
1071 if (errno == ENOENT) {
1072 char *parent = dirname(ondisk_path);
1073 if (parent == NULL) {
1074 err = got_error_from_errno2("dirname",
1078 err = add_dir_on_disk(worktree, parent);
1082 * Retry, and fall through to error handling
1083 * below if this second attempt fails.
1085 if (symlink(target_path, ondisk_path) != -1) {
1086 err = NULL; /* success */
1091 /* Handle errors from first or second creation attempt. */
1092 if (errno == ENAMETOOLONG) {
1093 /* bad target path; install as a regular file */
1094 got_object_blob_rewind(blob);
1095 err = install_blob(worktree, ondisk_path, path,
1096 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1097 restoring_missing_file, reverting_versioned_file,
1098 repo, progress_cb, progress_arg);
1099 } else if (errno == ENOTDIR) {
1100 err = got_error_path(ondisk_path,
1101 GOT_ERR_FILE_OBSTRUCTED);
1103 err = got_error_from_errno3("symlink",
1104 target_path, ondisk_path);
1107 err = (*progress_cb)(progress_arg, GOT_STATUS_ADD, path);
1109 free(resolved_path);
1114 static const struct got_error *
1115 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1116 const char *path, mode_t te_mode, mode_t st_mode,
1117 struct got_blob_object *blob, int restoring_missing_file,
1118 int reverting_versioned_file, struct got_repository *repo,
1119 got_worktree_checkout_cb progress_cb, void *progress_arg)
1121 const struct got_error *err = NULL;
1125 char *tmppath = NULL;
1127 if (S_ISLNK(te_mode))
1128 return install_symlink(worktree, ondisk_path, path, te_mode,
1129 st_mode, blob, restoring_missing_file,
1130 reverting_versioned_file, repo, progress_cb, progress_arg);
1132 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1133 GOT_DEFAULT_FILE_MODE);
1135 if (errno == ENOENT) {
1136 char *parent = dirname(path);
1138 return got_error_from_errno2("dirname", path);
1139 err = add_dir_on_disk(worktree, parent);
1142 fd = open(ondisk_path,
1143 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1144 GOT_DEFAULT_FILE_MODE);
1146 return got_error_from_errno2("open",
1148 } else if (errno == EEXIST) {
1149 if (!S_ISREG(st_mode)) {
1150 /* TODO file is obstructed; do something */
1151 err = got_error_path(ondisk_path,
1152 GOT_ERR_FILE_OBSTRUCTED);
1155 err = got_opentemp_named_fd(&tmppath, &fd,
1162 return got_error_from_errno2("open", ondisk_path);
1165 if (restoring_missing_file)
1166 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
1167 else if (reverting_versioned_file)
1168 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
1170 err = (*progress_cb)(progress_arg,
1171 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1175 hdrlen = got_object_blob_get_hdrlen(blob);
1177 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1178 err = got_object_blob_read_block(&len, blob);
1182 /* Skip blob object header first time around. */
1183 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1185 err = got_error_from_errno("write");
1187 } else if (outlen != len - hdrlen) {
1188 err = got_error(GOT_ERR_IO);
1195 if (fsync(fd) != 0) {
1196 err = got_error_from_errno("fsync");
1201 if (rename(tmppath, ondisk_path) != 0) {
1202 err = got_error_from_errno3("rename", tmppath,
1209 if (chmod(ondisk_path,
1210 get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1211 err = got_error_from_errno2("chmod", ondisk_path);
1216 if (fd != -1 && close(fd) != 0 && err == NULL)
1217 err = got_error_from_errno("close");
1222 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1223 static const struct got_error *
1224 get_modified_file_content_status(unsigned char *status, FILE *f)
1226 const struct got_error *err = NULL;
1227 const char *markers[3] = {
1228 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1229 GOT_DIFF_CONFLICT_MARKER_SEP,
1230 GOT_DIFF_CONFLICT_MARKER_END
1235 const char delim[3] = {'\0', '\0', '\0'};
1237 while (*status == GOT_STATUS_MODIFY) {
1238 line = fparseln(f, &len, NULL, delim, 0);
1242 err = got_ferror(f, GOT_ERR_IO);
1246 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1247 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1249 *status = GOT_STATUS_CONFLICT;
1259 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1261 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1262 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1266 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1268 return !(ie->ctime_sec == sb->st_ctime &&
1269 ie->ctime_nsec == sb->st_ctimensec &&
1270 ie->mtime_sec == sb->st_mtime &&
1271 ie->mtime_nsec == sb->st_mtimensec &&
1272 ie->size == (sb->st_size & 0xffffffff) &&
1273 !xbit_differs(ie, sb->st_mode));
1276 static unsigned char
1277 get_staged_status(struct got_fileindex_entry *ie)
1279 switch (got_fileindex_entry_stage_get(ie)) {
1280 case GOT_FILEIDX_STAGE_ADD:
1281 return GOT_STATUS_ADD;
1282 case GOT_FILEIDX_STAGE_DELETE:
1283 return GOT_STATUS_DELETE;
1284 case GOT_FILEIDX_STAGE_MODIFY:
1285 return GOT_STATUS_MODIFY;
1287 return GOT_STATUS_NO_CHANGE;
1291 static const struct got_error *
1292 get_symlink_status(unsigned char *status, struct stat *sb,
1293 struct got_fileindex_entry *ie, const char *abspath,
1294 int dirfd, const char *de_name, struct got_blob_object *blob)
1296 const struct got_error *err = NULL;
1297 char target_path[PATH_MAX];
1298 char etarget[PATH_MAX];
1300 size_t len, target_len = 0;
1301 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1302 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1304 *status = GOT_STATUS_NO_CHANGE;
1306 /* Blob object content specifies the target path of the link. */
1308 err = got_object_blob_read_block(&len, blob);
1311 if (len + target_len >= sizeof(target_path)) {
1313 * Should not happen. The blob contents were OK
1314 * when this symlink was installed.
1316 return got_error(GOT_ERR_NO_SPACE);
1319 /* Skip blob object header first time around. */
1320 memcpy(target_path + target_len, buf + hdrlen,
1322 target_len += len - hdrlen;
1326 target_path[target_len] = '\0';
1329 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1331 return got_error_from_errno2("readlinkat", abspath);
1333 elen = readlink(abspath, etarget, sizeof(etarget));
1335 return got_error_from_errno2("readlinkat", abspath);
1338 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1339 *status = GOT_STATUS_MODIFY;
1344 static const struct got_error *
1345 get_file_status(unsigned char *status, struct stat *sb,
1346 struct got_fileindex_entry *ie, const char *abspath,
1347 int dirfd, const char *de_name, struct got_repository *repo)
1349 const struct got_error *err = NULL;
1350 struct got_object_id id;
1355 struct got_blob_object *blob = NULL;
1357 unsigned char staged_status = get_staged_status(ie);
1359 *status = GOT_STATUS_NO_CHANGE;
1362 * Whenever the caller provides a directory descriptor and a
1363 * directory entry name for the file, use them! This prevents
1364 * race conditions if filesystem paths change beneath our feet.
1367 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1368 if (errno == ENOENT) {
1369 if (got_fileindex_entry_has_file_on_disk(ie))
1370 *status = GOT_STATUS_MISSING;
1372 *status = GOT_STATUS_DELETE;
1375 err = got_error_from_errno2("fstatat", abspath);
1379 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1380 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1381 return got_error_from_errno2("open", abspath);
1382 else if (fd == -1 && errno == ELOOP) {
1383 if (lstat(abspath, sb) == -1)
1384 return got_error_from_errno2("lstat", abspath);
1385 } else if (fd == -1 || fstat(fd, sb) == -1) {
1386 if (errno == ENOENT) {
1387 if (got_fileindex_entry_has_file_on_disk(ie))
1388 *status = GOT_STATUS_MISSING;
1390 *status = GOT_STATUS_DELETE;
1393 err = got_error_from_errno2("fstat", abspath);
1398 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1399 *status = GOT_STATUS_OBSTRUCTED;
1403 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1404 *status = GOT_STATUS_DELETE;
1406 } else if (!got_fileindex_entry_has_blob(ie) &&
1407 staged_status != GOT_STATUS_ADD) {
1408 *status = GOT_STATUS_ADD;
1412 if (!stat_info_differs(ie, sb))
1415 if (staged_status == GOT_STATUS_MODIFY ||
1416 staged_status == GOT_STATUS_ADD)
1417 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1419 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1421 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1425 if (S_ISLNK(sb->st_mode)) {
1426 /* Staging changes to symlinks is not yet(?) supported. */
1427 if (staged_status != GOT_STATUS_NO_CHANGE) {
1428 err = got_error_path(abspath, GOT_ERR_FILE_STATUS);
1431 err = get_symlink_status(status, sb, ie, abspath, dirfd,
1438 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1440 err = got_error_from_errno2("openat", abspath);
1445 f = fdopen(fd, "r");
1447 err = got_error_from_errno2("fdopen", abspath);
1451 hdrlen = got_object_blob_get_hdrlen(blob);
1453 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1454 err = got_object_blob_read_block(&blen, blob);
1457 /* Skip length of blob object header first time around. */
1458 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1459 if (flen == 0 && ferror(f)) {
1460 err = got_error_from_errno("fread");
1465 *status = GOT_STATUS_MODIFY;
1467 } else if (flen == 0) {
1469 *status = GOT_STATUS_MODIFY;
1471 } else if (blen - hdrlen == flen) {
1472 /* Skip blob object header first time around. */
1473 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1474 *status = GOT_STATUS_MODIFY;
1478 *status = GOT_STATUS_MODIFY;
1484 if (*status == GOT_STATUS_MODIFY) {
1486 err = get_modified_file_content_status(status, f);
1487 } else if (xbit_differs(ie, sb->st_mode))
1488 *status = GOT_STATUS_MODE_CHANGE;
1491 got_object_blob_close(blob);
1492 if (f != NULL && fclose(f) == EOF && err == NULL)
1493 err = got_error_from_errno2("fclose", abspath);
1494 if (fd != -1 && close(fd) == -1 && err == NULL)
1495 err = got_error_from_errno2("close", abspath);
1500 * Update timestamps in the file index if a file is unmodified and
1501 * we had to run a full content comparison to find out.
1503 static const struct got_error *
1504 sync_timestamps(char *ondisk_path, unsigned char status,
1505 struct got_fileindex_entry *ie, struct stat *sb)
1507 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1508 return got_fileindex_entry_update(ie, ondisk_path,
1509 ie->blob_sha1, ie->commit_sha1, 1);
1514 static const struct got_error *
1515 update_blob(struct got_worktree *worktree,
1516 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1517 struct got_tree_entry *te, const char *path,
1518 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1521 const struct got_error *err = NULL;
1522 struct got_blob_object *blob = NULL;
1524 unsigned char status = GOT_STATUS_NO_CHANGE;
1527 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1528 return got_error_from_errno("asprintf");
1531 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1532 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1535 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1539 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1540 sb.st_mode = got_fileindex_perms_to_st(ie);
1542 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1544 if (status == GOT_STATUS_OBSTRUCTED) {
1545 err = (*progress_cb)(progress_arg, status, path);
1548 if (status == GOT_STATUS_CONFLICT) {
1549 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1554 if (ie && status != GOT_STATUS_MISSING &&
1555 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1556 if (got_fileindex_entry_has_commit(ie) &&
1557 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1558 SHA1_DIGEST_LENGTH) == 0) {
1559 err = sync_timestamps(ondisk_path, status, ie, &sb);
1562 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1566 if (got_fileindex_entry_has_blob(ie) &&
1567 memcmp(ie->blob_sha1, te->id.sha1,
1568 SHA1_DIGEST_LENGTH) == 0) {
1569 err = sync_timestamps(ondisk_path, status, ie, &sb);
1574 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1578 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1579 int update_timestamps;
1580 struct got_blob_object *blob2 = NULL;
1581 char *label_orig = NULL;
1582 if (got_fileindex_entry_has_blob(ie)) {
1583 struct got_object_id id2;
1584 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1585 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1589 if (got_fileindex_entry_has_commit(ie)) {
1590 char id_str[SHA1_DIGEST_STRING_LENGTH];
1591 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1592 sizeof(id_str)) == NULL) {
1593 err = got_error_path(id_str,
1594 GOT_ERR_BAD_OBJ_ID_STR);
1597 if (asprintf(&label_orig, "%s: commit %s",
1598 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1599 err = got_error_from_errno("asprintf");
1603 err = merge_blob(&update_timestamps, worktree, blob2,
1604 ondisk_path, path, sb.st_mode, label_orig, blob,
1605 worktree->base_commit_id, repo,
1606 progress_cb, progress_arg);
1609 got_object_blob_close(blob2);
1613 * Do not update timestamps of files with local changes.
1614 * Otherwise, a future status walk would treat them as
1615 * unmodified files again.
1617 err = got_fileindex_entry_update(ie, ondisk_path,
1618 blob->id.sha1, worktree->base_commit_id->sha1,
1620 } else if (status == GOT_STATUS_MODE_CHANGE) {
1621 err = got_fileindex_entry_update(ie, ondisk_path,
1622 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1623 } else if (status == GOT_STATUS_DELETE) {
1624 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1627 err = got_fileindex_entry_update(ie, ondisk_path,
1628 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1632 err = install_blob(worktree, ondisk_path, path, te->mode,
1633 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1634 repo, progress_cb, progress_arg);
1638 err = got_fileindex_entry_update(ie, ondisk_path,
1639 blob->id.sha1, worktree->base_commit_id->sha1, 1);
1641 err = create_fileindex_entry(fileindex,
1642 worktree->base_commit_id, ondisk_path, path,
1648 got_object_blob_close(blob);
1654 static const struct got_error *
1655 remove_ondisk_file(const char *root_path, const char *path)
1657 const struct got_error *err = NULL;
1658 char *ondisk_path = NULL;
1660 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1661 return got_error_from_errno("asprintf");
1663 if (unlink(ondisk_path) == -1) {
1664 if (errno != ENOENT)
1665 err = got_error_from_errno2("unlink", ondisk_path);
1667 char *parent = dirname(ondisk_path);
1668 while (parent && strcmp(parent, root_path) != 0) {
1669 if (rmdir(parent) == -1) {
1670 if (errno != ENOTEMPTY)
1671 err = got_error_from_errno2("rmdir",
1675 parent = dirname(parent);
1682 static const struct got_error *
1683 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1684 struct got_fileindex_entry *ie, struct got_repository *repo,
1685 got_worktree_checkout_cb progress_cb, void *progress_arg)
1687 const struct got_error *err = NULL;
1688 unsigned char status;
1692 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1693 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1695 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1697 return got_error_from_errno("asprintf");
1699 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
1703 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1704 status == GOT_STATUS_ADD) {
1705 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1709 * Preserve the working file and change the deleted blob's
1710 * entry into a schedule-add entry.
1712 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1715 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1718 if (status == GOT_STATUS_NO_CHANGE) {
1719 err = remove_ondisk_file(worktree->root_path, ie->path);
1723 got_fileindex_entry_remove(fileindex, ie);
1730 struct diff_cb_arg {
1731 struct got_fileindex *fileindex;
1732 struct got_worktree *worktree;
1733 struct got_repository *repo;
1734 got_worktree_checkout_cb progress_cb;
1736 got_cancel_cb cancel_cb;
1740 static const struct got_error *
1741 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1742 struct got_tree_entry *te, const char *parent_path)
1744 struct diff_cb_arg *a = arg;
1746 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1747 return got_error(GOT_ERR_CANCELLED);
1749 return update_blob(a->worktree, a->fileindex, ie, te,
1750 ie->path, a->repo, a->progress_cb, a->progress_arg);
1753 static const struct got_error *
1754 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1756 struct diff_cb_arg *a = arg;
1758 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1759 return got_error(GOT_ERR_CANCELLED);
1761 return delete_blob(a->worktree, a->fileindex, ie,
1762 a->repo, a->progress_cb, a->progress_arg);
1765 static const struct got_error *
1766 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1768 struct diff_cb_arg *a = arg;
1769 const struct got_error *err;
1772 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1773 return got_error(GOT_ERR_CANCELLED);
1775 if (got_object_tree_entry_is_submodule(te))
1778 if (asprintf(&path, "%s%s%s", parent_path,
1779 parent_path[0] ? "/" : "", te->name)
1781 return got_error_from_errno("asprintf");
1783 if (S_ISDIR(te->mode))
1784 err = add_dir_on_disk(a->worktree, path);
1786 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1787 a->repo, a->progress_cb, a->progress_arg);
1793 static const struct got_error *
1794 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1796 const struct got_error *err = NULL;
1797 char *uuidstr = NULL;
1798 uint32_t uuid_status;
1802 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1803 if (uuid_status != uuid_s_ok)
1804 return got_error_uuid(uuid_status, "uuid_to_string");
1806 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1808 err = got_error_from_errno("asprintf");
1815 const struct got_error *
1816 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1818 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1821 static const struct got_error *
1822 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1824 return get_ref_name(refname, worktree,
1825 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1828 static const struct got_error *
1829 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1831 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1834 static const struct got_error *
1835 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1837 return get_ref_name(refname, worktree,
1838 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1841 static const struct got_error *
1842 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1844 return get_ref_name(refname, worktree,
1845 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1848 static const struct got_error *
1849 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1851 return get_ref_name(refname, worktree,
1852 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1855 static const struct got_error *
1856 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1858 return get_ref_name(refname, worktree,
1859 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1862 static const struct got_error *
1863 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1865 return get_ref_name(refname, worktree,
1866 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1869 static const struct got_error *
1870 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1872 return get_ref_name(refname, worktree,
1873 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1876 const struct got_error *
1877 got_worktree_get_histedit_script_path(char **path,
1878 struct got_worktree *worktree)
1880 if (asprintf(path, "%s/%s/%s", worktree->root_path,
1881 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
1883 return got_error_from_errno("asprintf");
1889 * Prevent Git's garbage collector from deleting our base commit by
1890 * setting a reference to our base commit's ID.
1892 static const struct got_error *
1893 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1895 const struct got_error *err = NULL;
1896 struct got_reference *ref = NULL;
1899 err = got_worktree_get_base_ref_name(&refname, worktree);
1903 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1907 err = got_ref_write(ref, repo);
1915 static const struct got_error *
1916 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
1918 const struct got_error *err = NULL;
1920 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1921 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1922 err = got_error_from_errno("asprintf");
1923 *fileindex_path = NULL;
1929 static const struct got_error *
1930 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1931 struct got_worktree *worktree)
1933 const struct got_error *err = NULL;
1936 *fileindex_path = NULL;
1937 *fileindex = got_fileindex_alloc();
1938 if (*fileindex == NULL)
1939 return got_error_from_errno("got_fileindex_alloc");
1941 err = get_fileindex_path(fileindex_path, worktree);
1945 index = fopen(*fileindex_path, "rb");
1946 if (index == NULL) {
1947 if (errno != ENOENT)
1948 err = got_error_from_errno2("fopen", *fileindex_path);
1950 err = got_fileindex_read(*fileindex, index);
1951 if (fclose(index) != 0 && err == NULL)
1952 err = got_error_from_errno("fclose");
1956 free(*fileindex_path);
1957 *fileindex_path = NULL;
1958 got_fileindex_free(*fileindex);
1964 struct bump_base_commit_id_arg {
1965 struct got_object_id *base_commit_id;
1968 const char *entry_name;
1969 got_worktree_checkout_cb progress_cb;
1973 /* Bump base commit ID of all files within an updated part of the work tree. */
1974 static const struct got_error *
1975 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1977 const struct got_error *err;
1978 struct bump_base_commit_id_arg *a = arg;
1980 if (a->entry_name) {
1981 if (strcmp(ie->path, a->path) != 0)
1983 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1986 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1987 SHA1_DIGEST_LENGTH) == 0)
1990 if (a->progress_cb) {
1991 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1996 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2000 static const struct got_error *
2001 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2003 const struct got_error *err = NULL;
2004 char *new_fileindex_path = NULL;
2005 FILE *new_index = NULL;
2006 struct timespec timeout;
2008 err = got_opentemp_named(&new_fileindex_path, &new_index,
2013 err = got_fileindex_write(fileindex, new_index);
2017 if (rename(new_fileindex_path, fileindex_path) != 0) {
2018 err = got_error_from_errno3("rename", new_fileindex_path,
2020 unlink(new_fileindex_path);
2024 * Sleep for a short amount of time to ensure that files modified after
2025 * this program exits have a different time stamp from the one which
2026 * was recorded in the file index.
2029 timeout.tv_nsec = 1;
2030 nanosleep(&timeout, NULL);
2034 free(new_fileindex_path);
2038 static const struct got_error *
2039 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2040 struct got_object_id **tree_id, const char *wt_relpath,
2041 struct got_worktree *worktree, struct got_repository *repo)
2043 const struct got_error *err = NULL;
2044 struct got_object_id *id = NULL;
2045 char *in_repo_path = NULL;
2046 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2048 *entry_type = GOT_OBJ_TYPE_ANY;
2049 *tree_relpath = NULL;
2052 if (wt_relpath[0] == '\0') {
2053 /* Check out all files within the work tree. */
2054 *entry_type = GOT_OBJ_TYPE_TREE;
2055 *tree_relpath = strdup("");
2056 if (*tree_relpath == NULL) {
2057 err = got_error_from_errno("strdup");
2060 err = got_object_id_by_path(tree_id, repo,
2061 worktree->base_commit_id, worktree->path_prefix);
2067 /* Check out a subset of files in the work tree. */
2069 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2070 is_root_wt ? "" : "/", wt_relpath) == -1) {
2071 err = got_error_from_errno("asprintf");
2075 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2081 in_repo_path = NULL;
2083 err = got_object_get_type(entry_type, repo, id);
2087 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2088 /* Check out a single file. */
2089 if (strchr(wt_relpath, '/') == NULL) {
2090 /* Check out a single file in work tree's root dir. */
2091 in_repo_path = strdup(worktree->path_prefix);
2092 if (in_repo_path == NULL) {
2093 err = got_error_from_errno("strdup");
2096 *tree_relpath = strdup("");
2097 if (*tree_relpath == NULL) {
2098 err = got_error_from_errno("strdup");
2102 /* Check out a single file in a subdirectory. */
2103 err = got_path_dirname(tree_relpath, wt_relpath);
2106 if (asprintf(&in_repo_path, "%s%s%s",
2107 worktree->path_prefix, is_root_wt ? "" : "/",
2108 *tree_relpath) == -1) {
2109 err = got_error_from_errno("asprintf");
2113 err = got_object_id_by_path(tree_id, repo,
2114 worktree->base_commit_id, in_repo_path);
2116 /* Check out all files within a subdirectory. */
2117 *tree_id = got_object_id_dup(id);
2118 if (*tree_id == NULL) {
2119 err = got_error_from_errno("got_object_id_dup");
2122 *tree_relpath = strdup(wt_relpath);
2123 if (*tree_relpath == NULL) {
2124 err = got_error_from_errno("strdup");
2132 *entry_type = GOT_OBJ_TYPE_ANY;
2133 free(*tree_relpath);
2134 *tree_relpath = NULL;
2141 static const struct got_error *
2142 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2143 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2144 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2145 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2147 const struct got_error *err = NULL;
2148 struct got_commit_object *commit = NULL;
2149 struct got_tree_object *tree = NULL;
2150 struct got_fileindex_diff_tree_cb diff_cb;
2151 struct diff_cb_arg arg;
2153 err = ref_base_commit(worktree, repo);
2155 if (!(err->code == GOT_ERR_ERRNO &&
2156 (errno == EACCES || errno == EROFS)))
2158 err = (*progress_cb)(progress_arg,
2159 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2164 err = got_object_open_as_commit(&commit, repo,
2165 worktree->base_commit_id);
2169 err = got_object_open_as_tree(&tree, repo, tree_id);
2174 got_object_tree_find_entry(tree, entry_name) == NULL) {
2175 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2179 diff_cb.diff_old_new = diff_old_new;
2180 diff_cb.diff_old = diff_old;
2181 diff_cb.diff_new = diff_new;
2182 arg.fileindex = fileindex;
2183 arg.worktree = worktree;
2185 arg.progress_cb = progress_cb;
2186 arg.progress_arg = progress_arg;
2187 arg.cancel_cb = cancel_cb;
2188 arg.cancel_arg = cancel_arg;
2189 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2190 entry_name, repo, &diff_cb, &arg);
2193 got_object_tree_close(tree);
2195 got_object_commit_close(commit);
2199 const struct got_error *
2200 got_worktree_checkout_files(struct got_worktree *worktree,
2201 struct got_pathlist_head *paths, struct got_repository *repo,
2202 got_worktree_checkout_cb progress_cb, void *progress_arg,
2203 got_cancel_cb cancel_cb, void *cancel_arg)
2205 const struct got_error *err = NULL, *sync_err, *unlockerr;
2206 struct got_commit_object *commit = NULL;
2207 struct got_tree_object *tree = NULL;
2208 struct got_fileindex *fileindex = NULL;
2209 char *fileindex_path = NULL;
2210 struct got_pathlist_entry *pe;
2211 struct tree_path_data {
2212 SIMPLEQ_ENTRY(tree_path_data) entry;
2213 struct got_object_id *tree_id;
2218 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2220 SIMPLEQ_INIT(&tree_paths);
2222 err = lock_worktree(worktree, LOCK_EX);
2226 /* Map all specified paths to in-repository trees. */
2227 TAILQ_FOREACH(pe, paths, entry) {
2228 tpd = malloc(sizeof(*tpd));
2230 err = got_error_from_errno("malloc");
2234 err = find_tree_entry_for_checkout(&tpd->entry_type,
2235 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2241 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2242 err = got_path_basename(&tpd->entry_name, pe->path);
2250 tpd->entry_name = NULL;
2252 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2256 * Read the file index.
2257 * Checking out files is supposed to be an idempotent operation.
2258 * If the on-disk file index is incomplete we will try to complete it.
2260 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2264 tpd = SIMPLEQ_FIRST(&tree_paths);
2265 TAILQ_FOREACH(pe, paths, entry) {
2266 struct bump_base_commit_id_arg bbc_arg;
2268 err = checkout_files(worktree, fileindex, tpd->relpath,
2269 tpd->tree_id, tpd->entry_name, repo,
2270 progress_cb, progress_arg, cancel_cb, cancel_arg);
2274 bbc_arg.base_commit_id = worktree->base_commit_id;
2275 bbc_arg.entry_name = tpd->entry_name;
2276 bbc_arg.path = pe->path;
2277 bbc_arg.path_len = pe->path_len;
2278 bbc_arg.progress_cb = progress_cb;
2279 bbc_arg.progress_arg = progress_arg;
2280 err = got_fileindex_for_each_entry_safe(fileindex,
2281 bump_base_commit_id, &bbc_arg);
2285 tpd = SIMPLEQ_NEXT(tpd, entry);
2287 sync_err = sync_fileindex(fileindex, fileindex_path);
2288 if (sync_err && err == NULL)
2291 free(fileindex_path);
2293 got_object_tree_close(tree);
2295 got_object_commit_close(commit);
2297 got_fileindex_free(fileindex);
2298 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2299 tpd = SIMPLEQ_FIRST(&tree_paths);
2300 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2305 unlockerr = lock_worktree(worktree, LOCK_SH);
2306 if (unlockerr && err == NULL)
2311 struct merge_file_cb_arg {
2312 struct got_worktree *worktree;
2313 struct got_fileindex *fileindex;
2314 got_worktree_checkout_cb progress_cb;
2316 got_cancel_cb cancel_cb;
2318 const char *label_orig;
2319 struct got_object_id *commit_id2;
2322 static const struct got_error *
2323 merge_file_cb(void *arg, struct got_blob_object *blob1,
2324 struct got_blob_object *blob2, struct got_object_id *id1,
2325 struct got_object_id *id2, const char *path1, const char *path2,
2326 mode_t mode1, mode_t mode2, struct got_repository *repo)
2328 static const struct got_error *err = NULL;
2329 struct merge_file_cb_arg *a = arg;
2330 struct got_fileindex_entry *ie;
2331 char *ondisk_path = NULL;
2333 unsigned char status;
2334 int local_changes_subsumed;
2336 if (blob1 && blob2) {
2337 ie = got_fileindex_entry_get(a->fileindex, path2,
2340 return (*a->progress_cb)(a->progress_arg,
2341 GOT_STATUS_MISSING, path2);
2343 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2345 return got_error_from_errno("asprintf");
2347 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2352 if (status == GOT_STATUS_DELETE) {
2353 err = (*a->progress_cb)(a->progress_arg,
2354 GOT_STATUS_MERGE, path2);
2357 if (status != GOT_STATUS_NO_CHANGE &&
2358 status != GOT_STATUS_MODIFY &&
2359 status != GOT_STATUS_CONFLICT &&
2360 status != GOT_STATUS_ADD) {
2361 err = (*a->progress_cb)(a->progress_arg, status, path2);
2365 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
2366 ondisk_path, path2, sb.st_mode, a->label_orig, blob2,
2367 a->commit_id2, repo, a->progress_cb, a->progress_arg);
2369 ie = got_fileindex_entry_get(a->fileindex, path1,
2372 return (*a->progress_cb)(a->progress_arg,
2373 GOT_STATUS_MISSING, path1);
2375 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2377 return got_error_from_errno("asprintf");
2379 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2385 case GOT_STATUS_NO_CHANGE:
2386 err = (*a->progress_cb)(a->progress_arg,
2387 GOT_STATUS_DELETE, path1);
2390 err = remove_ondisk_file(a->worktree->root_path, path1);
2394 got_fileindex_entry_mark_deleted_from_disk(ie);
2396 case GOT_STATUS_DELETE:
2397 case GOT_STATUS_MISSING:
2398 err = (*a->progress_cb)(a->progress_arg,
2399 GOT_STATUS_DELETE, path1);
2403 got_fileindex_entry_mark_deleted_from_disk(ie);
2405 case GOT_STATUS_ADD:
2406 case GOT_STATUS_MODIFY:
2407 case GOT_STATUS_CONFLICT:
2408 err = (*a->progress_cb)(a->progress_arg,
2409 GOT_STATUS_CANNOT_DELETE, path1);
2413 case GOT_STATUS_OBSTRUCTED:
2414 err = (*a->progress_cb)(a->progress_arg, status, path1);
2422 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2424 return got_error_from_errno("asprintf");
2425 ie = got_fileindex_entry_get(a->fileindex, path2,
2428 err = get_file_status(&status, &sb, ie, ondisk_path,
2432 if (status != GOT_STATUS_NO_CHANGE &&
2433 status != GOT_STATUS_MODIFY &&
2434 status != GOT_STATUS_CONFLICT &&
2435 status != GOT_STATUS_ADD) {
2436 err = (*a->progress_cb)(a->progress_arg,
2440 err = merge_blob(&local_changes_subsumed, a->worktree,
2441 NULL, ondisk_path, path2, sb.st_mode,
2442 a->label_orig, blob2, a->commit_id2, repo,
2445 if (status == GOT_STATUS_DELETE) {
2446 err = got_fileindex_entry_update(ie,
2447 ondisk_path, blob2->id.sha1,
2448 a->worktree->base_commit_id->sha1, 0);
2453 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2454 err = install_blob(a->worktree, ondisk_path, path2,
2455 /* XXX get this from parent tree! */
2456 GOT_DEFAULT_FILE_MODE,
2457 sb.st_mode, blob2, 0, 0, repo,
2458 a->progress_cb, a->progress_arg);
2461 err = got_fileindex_entry_alloc(&ie, path2);
2464 err = got_fileindex_entry_update(ie, ondisk_path,
2467 got_fileindex_entry_free(ie);
2470 err = got_fileindex_entry_add(a->fileindex, ie);
2472 got_fileindex_entry_free(ie);
2482 struct check_merge_ok_arg {
2483 struct got_worktree *worktree;
2484 struct got_repository *repo;
2487 static const struct got_error *
2488 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2490 const struct got_error *err = NULL;
2491 struct check_merge_ok_arg *a = arg;
2492 unsigned char status;
2496 /* Reject merges into a work tree with mixed base commits. */
2497 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2498 SHA1_DIGEST_LENGTH))
2499 return got_error(GOT_ERR_MIXED_COMMITS);
2501 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2503 return got_error_from_errno("asprintf");
2505 /* Reject merges into a work tree with conflicted files. */
2506 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2509 if (status == GOT_STATUS_CONFLICT)
2510 return got_error(GOT_ERR_CONFLICTS);
2515 static const struct got_error *
2516 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2517 const char *fileindex_path, struct got_object_id *commit_id1,
2518 struct got_object_id *commit_id2, struct got_repository *repo,
2519 got_worktree_checkout_cb progress_cb, void *progress_arg,
2520 got_cancel_cb cancel_cb, void *cancel_arg)
2522 const struct got_error *err = NULL, *sync_err;
2523 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2524 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2525 struct merge_file_cb_arg arg;
2526 char *label_orig = NULL;
2531 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2532 worktree->path_prefix);
2536 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2540 err = got_object_id_str(&id_str, commit_id1);
2544 if (asprintf(&label_orig, "%s: commit %s",
2545 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2546 err = got_error_from_errno("asprintf");
2553 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2554 worktree->path_prefix);
2558 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2562 arg.worktree = worktree;
2563 arg.fileindex = fileindex;
2564 arg.progress_cb = progress_cb;
2565 arg.progress_arg = progress_arg;
2566 arg.cancel_cb = cancel_cb;
2567 arg.cancel_arg = cancel_arg;
2568 arg.label_orig = label_orig;
2569 arg.commit_id2 = commit_id2;
2570 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2571 sync_err = sync_fileindex(fileindex, fileindex_path);
2572 if (sync_err && err == NULL)
2576 got_object_tree_close(tree1);
2578 got_object_tree_close(tree2);
2583 const struct got_error *
2584 got_worktree_merge_files(struct got_worktree *worktree,
2585 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2586 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2587 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2589 const struct got_error *err, *unlockerr;
2590 char *fileindex_path = NULL;
2591 struct got_fileindex *fileindex = NULL;
2592 struct check_merge_ok_arg mok_arg;
2594 err = lock_worktree(worktree, LOCK_EX);
2598 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2602 mok_arg.worktree = worktree;
2603 mok_arg.repo = repo;
2604 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2609 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2610 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2613 got_fileindex_free(fileindex);
2614 free(fileindex_path);
2615 unlockerr = lock_worktree(worktree, LOCK_SH);
2616 if (unlockerr && err == NULL)
2621 struct diff_dir_cb_arg {
2622 struct got_fileindex *fileindex;
2623 struct got_worktree *worktree;
2624 const char *status_path;
2625 size_t status_path_len;
2626 struct got_repository *repo;
2627 got_worktree_status_cb status_cb;
2629 got_cancel_cb cancel_cb;
2631 /* A pathlist containing per-directory pathlists of ignore patterns. */
2632 struct got_pathlist_head ignores;
2633 int report_unchanged;
2637 static const struct got_error *
2638 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2639 int dirfd, const char *de_name,
2640 got_worktree_status_cb status_cb, void *status_arg,
2641 struct got_repository *repo, int report_unchanged)
2643 const struct got_error *err = NULL;
2644 unsigned char status = GOT_STATUS_NO_CHANGE;
2645 unsigned char staged_status = get_staged_status(ie);
2647 struct got_object_id blob_id, commit_id, staged_blob_id;
2648 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2649 struct got_object_id *staged_blob_idp = NULL;
2651 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
2655 if (status == GOT_STATUS_NO_CHANGE &&
2656 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
2659 if (got_fileindex_entry_has_blob(ie)) {
2660 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2661 blob_idp = &blob_id;
2663 if (got_fileindex_entry_has_commit(ie)) {
2664 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2665 commit_idp = &commit_id;
2667 if (staged_status == GOT_STATUS_ADD ||
2668 staged_status == GOT_STATUS_MODIFY) {
2669 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2670 SHA1_DIGEST_LENGTH);
2671 staged_blob_idp = &staged_blob_id;
2674 return (*status_cb)(status_arg, status, staged_status,
2675 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
2678 static const struct got_error *
2679 status_old_new(void *arg, struct got_fileindex_entry *ie,
2680 struct dirent *de, const char *parent_path, int dirfd)
2682 const struct got_error *err = NULL;
2683 struct diff_dir_cb_arg *a = arg;
2686 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2687 return got_error(GOT_ERR_CANCELLED);
2689 if (got_path_cmp(parent_path, a->status_path,
2690 strlen(parent_path), a->status_path_len) != 0 &&
2691 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2694 if (parent_path[0]) {
2695 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2696 parent_path, de->d_name) == -1)
2697 return got_error_from_errno("asprintf");
2699 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2701 return got_error_from_errno("asprintf");
2704 err = report_file_status(ie, abspath, dirfd, de->d_name,
2705 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
2710 static const struct got_error *
2711 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2713 struct diff_dir_cb_arg *a = arg;
2714 struct got_object_id blob_id, commit_id;
2715 unsigned char status;
2717 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2718 return got_error(GOT_ERR_CANCELLED);
2720 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2723 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2724 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2725 if (got_fileindex_entry_has_file_on_disk(ie))
2726 status = GOT_STATUS_MISSING;
2728 status = GOT_STATUS_DELETE;
2729 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2730 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
2734 free_ignorelist(struct got_pathlist_head *ignorelist)
2736 struct got_pathlist_entry *pe;
2738 TAILQ_FOREACH(pe, ignorelist, entry)
2739 free((char *)pe->path);
2740 got_pathlist_free(ignorelist);
2744 free_ignores(struct got_pathlist_head *ignores)
2746 struct got_pathlist_entry *pe;
2748 TAILQ_FOREACH(pe, ignores, entry) {
2749 struct got_pathlist_head *ignorelist = pe->data;
2750 free_ignorelist(ignorelist);
2751 free((char *)pe->path);
2753 got_pathlist_free(ignores);
2756 static const struct got_error *
2757 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
2759 const struct got_error *err = NULL;
2760 struct got_pathlist_entry *pe = NULL;
2761 struct got_pathlist_head *ignorelist;
2762 char *line = NULL, *pattern, *dirpath = NULL;
2763 size_t linesize = 0;
2766 ignorelist = calloc(1, sizeof(*ignorelist));
2767 if (ignorelist == NULL)
2768 return got_error_from_errno("calloc");
2769 TAILQ_INIT(ignorelist);
2771 while ((linelen = getline(&line, &linesize, f)) != -1) {
2772 if (linelen > 0 && line[linelen - 1] == '\n')
2773 line[linelen - 1] = '\0';
2775 /* Git's ignores may contain comments. */
2779 /* Git's negated patterns are not (yet?) supported. */
2783 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
2785 err = got_error_from_errno("asprintf");
2788 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
2793 err = got_error_from_errno("getline");
2797 dirpath = strdup(path);
2798 if (dirpath == NULL) {
2799 err = got_error_from_errno("strdup");
2802 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
2805 if (err || pe == NULL) {
2807 free_ignorelist(ignorelist);
2813 match_ignores(struct got_pathlist_head *ignores, const char *path)
2815 struct got_pathlist_entry *pe;
2817 /* Handle patterns which match in all directories. */
2818 TAILQ_FOREACH(pe, ignores, entry) {
2819 struct got_pathlist_head *ignorelist = pe->data;
2820 struct got_pathlist_entry *pi;
2822 TAILQ_FOREACH(pi, ignorelist, entry) {
2823 const char *p, *pattern = pi->path;
2825 if (strncmp(pattern, "**/", 3) != 0)
2830 if (fnmatch(pattern, p,
2831 FNM_PATHNAME | FNM_LEADING_DIR)) {
2832 /* Retry in next directory. */
2833 while (*p && *p != '/')
2845 * The ignores pathlist contains ignore lists from children before
2846 * parents, so we can find the most specific ignorelist by walking
2847 * ignores backwards.
2849 pe = TAILQ_LAST(ignores, got_pathlist_head);
2851 if (got_path_is_child(path, pe->path, pe->path_len)) {
2852 struct got_pathlist_head *ignorelist = pe->data;
2853 struct got_pathlist_entry *pi;
2854 TAILQ_FOREACH(pi, ignorelist, entry) {
2855 const char *pattern = pi->path;
2856 int flags = FNM_LEADING_DIR;
2857 if (strstr(pattern, "/**/") == NULL)
2858 flags |= FNM_PATHNAME;
2859 if (fnmatch(pattern, path, flags))
2864 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
2870 static const struct got_error *
2871 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
2872 const char *path, int dirfd, const char *ignores_filename)
2874 const struct got_error *err = NULL;
2877 FILE *ignoresfile = NULL;
2879 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
2880 path[0] ? "/" : "", ignores_filename) == -1)
2881 return got_error_from_errno("asprintf");
2884 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
2886 if (errno != ENOENT && errno != EACCES)
2887 err = got_error_from_errno2("openat",
2890 ignoresfile = fdopen(fd, "r");
2891 if (ignoresfile == NULL)
2892 err = got_error_from_errno2("fdopen",
2896 err = read_ignores(ignores, path, ignoresfile);
2900 ignoresfile = fopen(ignorespath, "r");
2901 if (ignoresfile == NULL) {
2902 if (errno != ENOENT && errno != EACCES)
2903 err = got_error_from_errno2("fopen",
2906 err = read_ignores(ignores, path, ignoresfile);
2909 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
2910 err = got_error_from_errno2("fclose", path);
2911 if (fd != -1 && close(fd) == -1 && err == NULL)
2912 err = got_error_from_errno2("close", path);
2917 static const struct got_error *
2918 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
2920 const struct got_error *err = NULL;
2921 struct diff_dir_cb_arg *a = arg;
2924 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2925 return got_error(GOT_ERR_CANCELLED);
2927 if (parent_path[0]) {
2928 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2929 return got_error_from_errno("asprintf");
2934 if (de->d_type != DT_DIR &&
2935 got_path_is_child(path, a->status_path, a->status_path_len)
2936 && !match_ignores(&a->ignores, path))
2937 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2938 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2944 static const struct got_error *
2945 status_traverse(void *arg, const char *path, int dirfd)
2947 const struct got_error *err = NULL;
2948 struct diff_dir_cb_arg *a = arg;
2953 err = add_ignores(&a->ignores, a->worktree->root_path,
2954 path, dirfd, ".cvsignore");
2958 err = add_ignores(&a->ignores, a->worktree->root_path, path,
2959 dirfd, ".gitignore");
2964 static const struct got_error *
2965 report_single_file_status(const char *path, const char *ondisk_path,
2966 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2967 void *status_arg, struct got_repository *repo, int report_unchanged)
2969 struct got_fileindex_entry *ie;
2972 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
2974 return report_file_status(ie, ondisk_path, -1, NULL,
2975 status_cb, status_arg, repo, report_unchanged);
2977 if (lstat(ondisk_path, &sb) == -1) {
2978 if (errno != ENOENT)
2979 return got_error_from_errno2("lstat", ondisk_path);
2980 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
2981 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2985 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
2986 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
2987 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2992 static const struct got_error *
2993 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
2994 const char *root_path, const char *path)
2996 const struct got_error *err;
2997 char *parent_path, *next_parent_path = NULL;
2999 err = add_ignores(ignores, root_path, "", -1,
3004 err = add_ignores(ignores, root_path, "", -1,
3009 err = got_path_dirname(&parent_path, path);
3011 if (err->code == GOT_ERR_BAD_PATH)
3012 return NULL; /* cannot traverse parent */
3016 err = add_ignores(ignores, root_path, parent_path, -1,
3020 err = add_ignores(ignores, root_path, parent_path, -1,
3024 err = got_path_dirname(&next_parent_path, parent_path);
3026 if (err->code == GOT_ERR_BAD_PATH)
3027 err = NULL; /* traversed everything */
3031 parent_path = next_parent_path;
3032 next_parent_path = NULL;
3036 free(next_parent_path);
3040 static const struct got_error *
3041 worktree_status(struct got_worktree *worktree, const char *path,
3042 struct got_fileindex *fileindex, struct got_repository *repo,
3043 got_worktree_status_cb status_cb, void *status_arg,
3044 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3045 int report_unchanged)
3047 const struct got_error *err = NULL;
3049 struct got_fileindex_diff_dir_cb fdiff_cb;
3050 struct diff_dir_cb_arg arg;
3051 char *ondisk_path = NULL;
3053 TAILQ_INIT(&arg.ignores);
3055 if (asprintf(&ondisk_path, "%s%s%s",
3056 worktree->root_path, path[0] ? "/" : "", path) == -1)
3057 return got_error_from_errno("asprintf");
3059 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3061 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3063 err = got_error_from_errno2("open", ondisk_path);
3065 err = report_single_file_status(path, ondisk_path,
3066 fileindex, status_cb, status_arg, repo,
3069 fdiff_cb.diff_old_new = status_old_new;
3070 fdiff_cb.diff_old = status_old;
3071 fdiff_cb.diff_new = status_new;
3072 fdiff_cb.diff_traverse = status_traverse;
3073 arg.fileindex = fileindex;
3074 arg.worktree = worktree;
3075 arg.status_path = path;
3076 arg.status_path_len = strlen(path);
3078 arg.status_cb = status_cb;
3079 arg.status_arg = status_arg;
3080 arg.cancel_cb = cancel_cb;
3081 arg.cancel_arg = cancel_arg;
3082 arg.report_unchanged = report_unchanged;
3083 arg.no_ignores = no_ignores;
3085 err = add_ignores_from_parent_paths(&arg.ignores,
3086 worktree->root_path, path);
3090 err = got_fileindex_diff_dir(fileindex, fd,
3091 worktree->root_path, path, repo, &fdiff_cb, &arg);
3094 free_ignores(&arg.ignores);
3095 if (fd != -1 && close(fd) != 0 && err == NULL)
3096 err = got_error_from_errno("close");
3101 const struct got_error *
3102 got_worktree_status(struct got_worktree *worktree,
3103 struct got_pathlist_head *paths, struct got_repository *repo,
3104 got_worktree_status_cb status_cb, void *status_arg,
3105 got_cancel_cb cancel_cb, void *cancel_arg)
3107 const struct got_error *err = NULL;
3108 char *fileindex_path = NULL;
3109 struct got_fileindex *fileindex = NULL;
3110 struct got_pathlist_entry *pe;
3112 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3116 TAILQ_FOREACH(pe, paths, entry) {
3117 err = worktree_status(worktree, pe->path, fileindex, repo,
3118 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3122 free(fileindex_path);
3123 got_fileindex_free(fileindex);
3127 const struct got_error *
3128 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3131 const struct got_error *err = NULL;
3132 char *resolved = NULL, *cwd = NULL, *path = NULL;
3138 cwd = getcwd(NULL, 0);
3140 return got_error_from_errno("getcwd");
3142 if (lstat(arg, &sb) == -1) {
3143 if (errno != ENOENT) {
3144 err = got_error_from_errno2("lstat", arg);
3148 if (S_ISLNK(sb.st_mode)) {
3150 * We cannot use realpath(3) with symlinks since we want to
3151 * operate on the symlink itself.
3152 * But we can make the path absolute, assuming it is relative
3153 * to the current working directory, and then canonicalize it.
3155 char *abspath = NULL;
3156 char canonpath[PATH_MAX];
3157 if (!got_path_is_absolute(arg)) {
3158 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3159 err = got_error_from_errno("asprintf");
3164 err = got_canonpath(abspath ? abspath : arg, canonpath,
3168 resolved = strdup(canonpath);
3169 if (resolved == NULL) {
3170 err = got_error_from_errno("strdup");
3174 resolved = realpath(arg, NULL);
3175 if (resolved == NULL) {
3176 if (errno != ENOENT) {
3177 err = got_error_from_errno2("realpath", arg);
3180 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
3181 err = got_error_from_errno("asprintf");
3187 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3188 strlen(got_worktree_get_root_path(worktree)))) {
3189 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3193 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3194 err = got_path_skip_common_ancestor(&path,
3195 got_worktree_get_root_path(worktree), resolved);
3201 err = got_error_from_errno("strdup");
3206 /* XXX status walk can't deal with trailing slash! */
3208 while (len > 0 && path[len - 1] == '/') {
3209 path[len - 1] = '\0';
3222 struct schedule_addition_args {
3223 struct got_worktree *worktree;
3224 struct got_fileindex *fileindex;
3225 got_worktree_checkout_cb progress_cb;
3227 struct got_repository *repo;
3230 static const struct got_error *
3231 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3232 const char *relpath, struct got_object_id *blob_id,
3233 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3234 int dirfd, const char *de_name)
3236 struct schedule_addition_args *a = arg;
3237 const struct got_error *err = NULL;
3238 struct got_fileindex_entry *ie;
3242 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3244 return got_error_from_errno("asprintf");
3246 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3248 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3252 /* Re-adding an existing entry is a no-op. */
3253 if (status == GOT_STATUS_ADD)
3255 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3260 if (status != GOT_STATUS_UNVERSIONED) {
3261 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3265 err = got_fileindex_entry_alloc(&ie, relpath);
3268 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3270 got_fileindex_entry_free(ie);
3273 err = got_fileindex_entry_add(a->fileindex, ie);
3275 got_fileindex_entry_free(ie);
3282 if (status == GOT_STATUS_ADD)
3284 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3287 const struct got_error *
3288 got_worktree_schedule_add(struct got_worktree *worktree,
3289 struct got_pathlist_head *paths,
3290 got_worktree_checkout_cb progress_cb, void *progress_arg,
3291 struct got_repository *repo, int no_ignores)
3293 struct got_fileindex *fileindex = NULL;
3294 char *fileindex_path = NULL;
3295 const struct got_error *err = NULL, *sync_err, *unlockerr;
3296 struct got_pathlist_entry *pe;
3297 struct schedule_addition_args saa;
3299 err = lock_worktree(worktree, LOCK_EX);
3303 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3307 saa.worktree = worktree;
3308 saa.fileindex = fileindex;
3309 saa.progress_cb = progress_cb;
3310 saa.progress_arg = progress_arg;
3313 TAILQ_FOREACH(pe, paths, entry) {
3314 err = worktree_status(worktree, pe->path, fileindex, repo,
3315 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3319 sync_err = sync_fileindex(fileindex, fileindex_path);
3320 if (sync_err && err == NULL)
3323 free(fileindex_path);
3325 got_fileindex_free(fileindex);
3326 unlockerr = lock_worktree(worktree, LOCK_SH);
3327 if (unlockerr && err == NULL)
3332 struct schedule_deletion_args {
3333 struct got_worktree *worktree;
3334 struct got_fileindex *fileindex;
3335 got_worktree_delete_cb progress_cb;
3337 struct got_repository *repo;
3338 int delete_local_mods;
3342 static const struct got_error *
3343 schedule_for_deletion(void *arg, unsigned char status,
3344 unsigned char staged_status, const char *relpath,
3345 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3346 struct got_object_id *commit_id, int dirfd, const char *de_name)
3348 struct schedule_deletion_args *a = arg;
3349 const struct got_error *err = NULL;
3350 struct got_fileindex_entry *ie = NULL;
3352 char *ondisk_path, *parent = NULL;
3354 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3356 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3358 staged_status = get_staged_status(ie);
3359 if (staged_status != GOT_STATUS_NO_CHANGE) {
3360 if (staged_status == GOT_STATUS_DELETE)
3362 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3365 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3367 return got_error_from_errno("asprintf");
3369 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3374 if (status != GOT_STATUS_NO_CHANGE) {
3375 if (status == GOT_STATUS_DELETE)
3377 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3378 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3381 if (status != GOT_STATUS_MODIFY &&
3382 status != GOT_STATUS_MISSING) {
3383 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3388 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3390 if (unlinkat(dirfd, de_name, 0) != 0) {
3391 err = got_error_from_errno2("unlinkat",
3395 } else if (unlink(ondisk_path) != 0) {
3396 err = got_error_from_errno2("unlink", ondisk_path);
3400 parent = dirname(ondisk_path);
3402 if (parent == NULL) {
3403 err = got_error_from_errno2("dirname", ondisk_path);
3406 while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3407 if (rmdir(parent) == -1) {
3408 if (errno != ENOTEMPTY)
3409 err = got_error_from_errno2("rmdir",
3413 parent = dirname(parent);
3414 if (parent == NULL) {
3415 err = got_error_from_errno2("dirname", parent);
3421 got_fileindex_entry_mark_deleted_from_disk(ie);
3426 if (status == GOT_STATUS_DELETE)
3428 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3429 staged_status, relpath);
3432 const struct got_error *
3433 got_worktree_schedule_delete(struct got_worktree *worktree,
3434 struct got_pathlist_head *paths, int delete_local_mods,
3435 got_worktree_delete_cb progress_cb, void *progress_arg,
3436 struct got_repository *repo, int keep_on_disk)
3438 struct got_fileindex *fileindex = NULL;
3439 char *fileindex_path = NULL;
3440 const struct got_error *err = NULL, *sync_err, *unlockerr;
3441 struct got_pathlist_entry *pe;
3442 struct schedule_deletion_args sda;
3444 err = lock_worktree(worktree, LOCK_EX);
3448 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3452 sda.worktree = worktree;
3453 sda.fileindex = fileindex;
3454 sda.progress_cb = progress_cb;
3455 sda.progress_arg = progress_arg;
3457 sda.delete_local_mods = delete_local_mods;
3458 sda.keep_on_disk = keep_on_disk;
3460 TAILQ_FOREACH(pe, paths, entry) {
3461 err = worktree_status(worktree, pe->path, fileindex, repo,
3462 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3466 sync_err = sync_fileindex(fileindex, fileindex_path);
3467 if (sync_err && err == NULL)
3470 free(fileindex_path);
3472 got_fileindex_free(fileindex);
3473 unlockerr = lock_worktree(worktree, LOCK_SH);
3474 if (unlockerr && err == NULL)
3479 static const struct got_error *
3480 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3482 const struct got_error *err = NULL;
3484 size_t linesize = 0, n;
3487 linelen = getline(&line, &linesize, infile);
3488 if (linelen == -1) {
3489 if (ferror(infile)) {
3490 err = got_error_from_errno("getline");
3496 n = fwrite(line, 1, linelen, outfile);
3498 err = got_ferror(outfile, GOT_ERR_IO);
3503 n = fwrite(line, 1, linelen, rejectfile);
3505 err = got_ferror(outfile, GOT_ERR_IO);
3512 static const struct got_error *
3513 skip_one_line(FILE *f)
3516 size_t linesize = 0;
3519 linelen = getline(&line, &linesize, f);
3520 if (linelen == -1) {
3522 return got_error_from_errno("getline");
3529 static const struct got_error *
3530 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3531 int start_old, int end_old, int start_new, int end_new,
3532 FILE *outfile, FILE *rejectfile)
3534 const struct got_error *err;
3536 /* Copy old file's lines leading up to patch. */
3537 while (!feof(f1) && *line_cur1 < start_old) {
3538 err = copy_one_line(f1, outfile, NULL);
3543 /* Skip new file's lines leading up to patch. */
3544 while (!feof(f2) && *line_cur2 < start_new) {
3546 err = copy_one_line(f2, NULL, rejectfile);
3548 err = skip_one_line(f2);
3553 /* Copy patched lines. */
3554 while (!feof(f2) && *line_cur2 <= end_new) {
3555 err = copy_one_line(f2, outfile, NULL);
3560 /* Skip over old file's replaced lines. */
3561 while (!feof(f1) && *line_cur1 <= end_old) {
3563 err = copy_one_line(f1, NULL, rejectfile);
3565 err = skip_one_line(f1);
3574 static const struct got_error *
3575 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3576 FILE *outfile, FILE *rejectfile)
3578 const struct got_error *err;
3581 /* Copy old file's lines until EOF. */
3583 err = copy_one_line(f1, outfile, NULL);
3590 /* Copy new file's lines until EOF. */
3592 err = copy_one_line(f2, NULL, rejectfile);
3602 static const struct got_error *
3603 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3604 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3605 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3606 int *line_cur2, FILE *outfile, FILE *rejectfile,
3607 got_worktree_patch_cb patch_cb, void *patch_arg)
3609 const struct got_error *err = NULL;
3610 int start_old = change->cv.a;
3611 int end_old = change->cv.b;
3612 int start_new = change->cv.c;
3613 int end_new = change->cv.d;
3617 *choice = GOT_PATCH_CHOICE_NONE;
3619 hunkfile = got_opentemp();
3620 if (hunkfile == NULL)
3621 return got_error_from_errno("got_opentemp");
3626 /* XXX TODO needs error checking */
3627 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
3629 if (fseek(f1, pos1, SEEK_SET) == -1) {
3630 err = got_ferror(f1, GOT_ERR_IO);
3633 if (fseek(f2, pos2, SEEK_SET) == -1) {
3634 err = got_ferror(f1, GOT_ERR_IO);
3637 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
3638 err = got_ferror(hunkfile, GOT_ERR_IO);
3642 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
3643 hunkfile, n, nchanges);
3648 case GOT_PATCH_CHOICE_YES:
3649 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3650 end_old, start_new, end_new, outfile, rejectfile);
3652 case GOT_PATCH_CHOICE_NO:
3653 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3654 end_old, start_new, end_new, rejectfile, outfile);
3656 case GOT_PATCH_CHOICE_QUIT:
3659 err = got_error(GOT_ERR_PATCH_CHOICE);
3663 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3664 err = got_error_from_errno("fclose");
3668 struct revert_file_args {
3669 struct got_worktree *worktree;
3670 struct got_fileindex *fileindex;
3671 got_worktree_checkout_cb progress_cb;
3673 got_worktree_patch_cb patch_cb;
3675 struct got_repository *repo;
3678 static const struct got_error *
3679 create_patched_content(char **path_outfile, int reverse_patch,
3680 struct got_object_id *blob_id, const char *path2,
3681 int dirfd2, const char *de_name2,
3682 const char *relpath, struct got_repository *repo,
3683 got_worktree_patch_cb patch_cb, void *patch_arg)
3685 const struct got_error *err;
3686 struct got_blob_object *blob = NULL;
3687 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3689 char *path1 = NULL, *id_str = NULL;
3690 struct stat sb1, sb2;
3691 struct got_diff_changes *changes = NULL;
3692 struct got_diff_state *ds = NULL;
3693 struct got_diff_args *args = NULL;
3694 struct got_diff_change *change;
3695 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3698 *path_outfile = NULL;
3700 err = got_object_id_str(&id_str, blob_id);
3705 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
3707 err = got_error_from_errno2("openat", path2);
3711 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
3713 err = got_error_from_errno2("open", path2);
3717 if (fstat(fd2, &sb2) == -1) {
3718 err = got_error_from_errno2("fstat", path2);
3722 f2 = fdopen(fd2, "r");
3724 err = got_error_from_errno2("fdopen", path2);
3729 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
3733 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
3737 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
3741 if (stat(path1, &sb1) == -1) {
3742 err = got_error_from_errno2("stat", path1);
3746 err = got_diff_files(&changes, &ds, &args, &diff_flags,
3747 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
3751 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
3755 if (fseek(f1, 0L, SEEK_SET) == -1)
3756 return got_ferror(f1, GOT_ERR_IO);
3757 if (fseek(f2, 0L, SEEK_SET) == -1)
3758 return got_ferror(f2, GOT_ERR_IO);
3759 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
3761 err = apply_or_reject_change(&choice, change, ++n,
3762 changes->nchanges, ds, args, diff_flags, relpath,
3763 f1, f2, &line_cur1, &line_cur2,
3764 reverse_patch ? NULL : outfile,
3765 reverse_patch ? outfile : NULL,
3766 patch_cb, patch_arg);
3769 if (choice == GOT_PATCH_CHOICE_YES)
3771 else if (choice == GOT_PATCH_CHOICE_QUIT)
3775 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
3776 reverse_patch ? NULL : outfile,
3777 reverse_patch ? outfile : NULL);
3781 if (chmod(*path_outfile, sb2.st_mode) == -1) {
3782 err = got_error_from_errno2("chmod", path2);
3789 got_object_blob_close(blob);
3790 if (f1 && fclose(f1) == EOF && err == NULL)
3791 err = got_error_from_errno2("fclose", path1);
3792 if (f2 && fclose(f2) == EOF && err == NULL)
3793 err = got_error_from_errno2("fclose", path2);
3794 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3795 err = got_error_from_errno2("close", path2);
3796 if (outfile && fclose(outfile) == EOF && err == NULL)
3797 err = got_error_from_errno2("fclose", *path_outfile);
3798 if (path1 && unlink(path1) == -1 && err == NULL)
3799 err = got_error_from_errno2("unlink", path1);
3800 if (err || !have_content) {
3801 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
3802 err = got_error_from_errno2("unlink", *path_outfile);
3803 free(*path_outfile);
3804 *path_outfile = NULL;
3808 got_diff_state_free(ds);
3812 got_diff_free_changes(changes);
3817 static const struct got_error *
3818 revert_file(void *arg, unsigned char status, unsigned char staged_status,
3819 const char *relpath, struct got_object_id *blob_id,
3820 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3821 int dirfd, const char *de_name)
3823 struct revert_file_args *a = arg;
3824 const struct got_error *err = NULL;
3825 char *parent_path = NULL;
3826 struct got_fileindex_entry *ie;
3827 struct got_tree_object *tree = NULL;
3828 struct got_object_id *tree_id = NULL;
3829 const struct got_tree_entry *te = NULL;
3830 char *tree_path = NULL, *te_name;
3831 char *ondisk_path = NULL, *path_content = NULL;
3832 struct got_blob_object *blob = NULL;
3834 /* Reverting a staged deletion is a no-op. */
3835 if (status == GOT_STATUS_DELETE &&
3836 staged_status != GOT_STATUS_NO_CHANGE)
3839 if (status == GOT_STATUS_UNVERSIONED)
3840 return (*a->progress_cb)(a->progress_arg,
3841 GOT_STATUS_UNVERSIONED, relpath);
3843 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3845 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3847 /* Construct in-repository path of tree which contains this blob. */
3848 err = got_path_dirname(&parent_path, ie->path);
3850 if (err->code != GOT_ERR_BAD_PATH)
3852 parent_path = strdup("/");
3853 if (parent_path == NULL) {
3854 err = got_error_from_errno("strdup");
3858 if (got_path_is_root_dir(a->worktree->path_prefix)) {
3859 tree_path = strdup(parent_path);
3860 if (tree_path == NULL) {
3861 err = got_error_from_errno("strdup");
3865 if (got_path_is_root_dir(parent_path)) {
3866 tree_path = strdup(a->worktree->path_prefix);
3867 if (tree_path == NULL) {
3868 err = got_error_from_errno("strdup");
3872 if (asprintf(&tree_path, "%s/%s",
3873 a->worktree->path_prefix, parent_path) == -1) {
3874 err = got_error_from_errno("asprintf");
3880 err = got_object_id_by_path(&tree_id, a->repo,
3881 a->worktree->base_commit_id, tree_path);
3883 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
3884 (status == GOT_STATUS_ADD ||
3885 staged_status == GOT_STATUS_ADD)))
3888 err = got_object_open_as_tree(&tree, a->repo, tree_id);
3892 te_name = basename(ie->path);
3893 if (te_name == NULL) {
3894 err = got_error_from_errno2("basename", ie->path);
3898 te = got_object_tree_find_entry(tree, te_name);
3899 if (te == NULL && status != GOT_STATUS_ADD &&
3900 staged_status != GOT_STATUS_ADD) {
3901 err = got_error(GOT_ERR_NO_TREE_ENTRY);
3907 case GOT_STATUS_ADD:
3909 int choice = GOT_PATCH_CHOICE_NONE;
3910 err = (*a->patch_cb)(&choice, a->patch_arg,
3911 status, ie->path, NULL, 1, 1);
3914 if (choice != GOT_PATCH_CHOICE_YES)
3917 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
3921 got_fileindex_entry_remove(a->fileindex, ie);
3923 case GOT_STATUS_DELETE:
3925 int choice = GOT_PATCH_CHOICE_NONE;
3926 err = (*a->patch_cb)(&choice, a->patch_arg,
3927 status, ie->path, NULL, 1, 1);
3930 if (choice != GOT_PATCH_CHOICE_YES)
3934 case GOT_STATUS_MODIFY:
3935 case GOT_STATUS_MODE_CHANGE:
3936 case GOT_STATUS_CONFLICT:
3937 case GOT_STATUS_MISSING: {
3938 struct got_object_id id;
3939 if (staged_status == GOT_STATUS_ADD ||
3940 staged_status == GOT_STATUS_MODIFY) {
3941 memcpy(id.sha1, ie->staged_blob_sha1,
3942 SHA1_DIGEST_LENGTH);
3944 memcpy(id.sha1, ie->blob_sha1,
3945 SHA1_DIGEST_LENGTH);
3946 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
3950 if (asprintf(&ondisk_path, "%s/%s",
3951 got_worktree_get_root_path(a->worktree), relpath) == -1) {
3952 err = got_error_from_errno("asprintf");
3956 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
3957 status == GOT_STATUS_CONFLICT)) {
3958 err = create_patched_content(&path_content, 1, &id,
3959 ondisk_path, dirfd, de_name, ie->path, a->repo,
3960 a->patch_cb, a->patch_arg);
3961 if (err || path_content == NULL)
3963 if (rename(path_content, ondisk_path) == -1) {
3964 err = got_error_from_errno3("rename",
3965 path_content, ondisk_path);
3969 err = install_blob(a->worktree, ondisk_path, ie->path,
3970 te ? te->mode : GOT_DEFAULT_FILE_MODE,
3971 got_fileindex_perms_to_st(ie), blob, 0, 1,
3972 a->repo, a->progress_cb, a->progress_arg);
3975 if (status == GOT_STATUS_DELETE ||
3976 status == GOT_STATUS_MODE_CHANGE) {
3977 err = got_fileindex_entry_update(ie,
3978 ondisk_path, blob->id.sha1,
3979 a->worktree->base_commit_id->sha1, 1);
3995 got_object_blob_close(blob);
3997 got_object_tree_close(tree);
4002 const struct got_error *
4003 got_worktree_revert(struct got_worktree *worktree,
4004 struct got_pathlist_head *paths,
4005 got_worktree_checkout_cb progress_cb, void *progress_arg,
4006 got_worktree_patch_cb patch_cb, void *patch_arg,
4007 struct got_repository *repo)
4009 struct got_fileindex *fileindex = NULL;
4010 char *fileindex_path = NULL;
4011 const struct got_error *err = NULL, *unlockerr = NULL;
4012 const struct got_error *sync_err = NULL;
4013 struct got_pathlist_entry *pe;
4014 struct revert_file_args rfa;
4016 err = lock_worktree(worktree, LOCK_EX);
4020 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4024 rfa.worktree = worktree;
4025 rfa.fileindex = fileindex;
4026 rfa.progress_cb = progress_cb;
4027 rfa.progress_arg = progress_arg;
4028 rfa.patch_cb = patch_cb;
4029 rfa.patch_arg = patch_arg;
4031 TAILQ_FOREACH(pe, paths, entry) {
4032 err = worktree_status(worktree, pe->path, fileindex, repo,
4033 revert_file, &rfa, NULL, NULL, 0, 0);
4037 sync_err = sync_fileindex(fileindex, fileindex_path);
4038 if (sync_err && err == NULL)
4041 free(fileindex_path);
4043 got_fileindex_free(fileindex);
4044 unlockerr = lock_worktree(worktree, LOCK_SH);
4045 if (unlockerr && err == NULL)
4051 free_commitable(struct got_commitable *ct)
4054 free(ct->in_repo_path);
4055 free(ct->ondisk_path);
4057 free(ct->base_blob_id);
4058 free(ct->staged_blob_id);
4059 free(ct->base_commit_id);
4063 struct collect_commitables_arg {
4064 struct got_pathlist_head *commitable_paths;
4065 struct got_repository *repo;
4066 struct got_worktree *worktree;
4067 int have_staged_files;
4070 static const struct got_error *
4071 collect_commitables(void *arg, unsigned char status,
4072 unsigned char staged_status, const char *relpath,
4073 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4074 struct got_object_id *commit_id, int dirfd, const char *de_name)
4076 struct collect_commitables_arg *a = arg;
4077 const struct got_error *err = NULL;
4078 struct got_commitable *ct = NULL;
4079 struct got_pathlist_entry *new = NULL;
4080 char *parent_path = NULL, *path = NULL;
4083 if (a->have_staged_files) {
4084 if (staged_status != GOT_STATUS_MODIFY &&
4085 staged_status != GOT_STATUS_ADD &&
4086 staged_status != GOT_STATUS_DELETE)
4089 if (status == GOT_STATUS_CONFLICT)
4090 return got_error(GOT_ERR_COMMIT_CONFLICT);
4092 if (status != GOT_STATUS_MODIFY &&
4093 status != GOT_STATUS_MODE_CHANGE &&
4094 status != GOT_STATUS_ADD &&
4095 status != GOT_STATUS_DELETE)
4099 if (asprintf(&path, "/%s", relpath) == -1) {
4100 err = got_error_from_errno("asprintf");
4103 if (strcmp(path, "/") == 0) {
4104 parent_path = strdup("");
4105 if (parent_path == NULL)
4106 return got_error_from_errno("strdup");
4108 err = got_path_dirname(&parent_path, path);
4113 ct = calloc(1, sizeof(*ct));
4115 err = got_error_from_errno("calloc");
4119 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4121 err = got_error_from_errno("asprintf");
4124 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
4125 sb.st_mode = GOT_DEFAULT_FILE_MODE;
4128 if (fstatat(dirfd, de_name, &sb,
4129 AT_SYMLINK_NOFOLLOW) == -1) {
4130 err = got_error_from_errno2("fstatat",
4134 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4135 err = got_error_from_errno2("lstat", ct->ondisk_path);
4138 ct->mode = sb.st_mode;
4141 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4142 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4144 err = got_error_from_errno("asprintf");
4148 ct->status = status;
4149 ct->staged_status = staged_status;
4150 ct->blob_id = NULL; /* will be filled in when blob gets created */
4151 if (ct->status != GOT_STATUS_ADD &&
4152 ct->staged_status != GOT_STATUS_ADD) {
4153 ct->base_blob_id = got_object_id_dup(blob_id);
4154 if (ct->base_blob_id == NULL) {
4155 err = got_error_from_errno("got_object_id_dup");
4158 ct->base_commit_id = got_object_id_dup(commit_id);
4159 if (ct->base_commit_id == NULL) {
4160 err = got_error_from_errno("got_object_id_dup");
4164 if (ct->staged_status == GOT_STATUS_ADD ||
4165 ct->staged_status == GOT_STATUS_MODIFY) {
4166 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4167 if (ct->staged_blob_id == NULL) {
4168 err = got_error_from_errno("got_object_id_dup");
4172 ct->path = strdup(path);
4173 if (ct->path == NULL) {
4174 err = got_error_from_errno("strdup");
4177 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4179 if (ct && (err || new == NULL))
4180 free_commitable(ct);
4186 static const struct got_error *write_tree(struct got_object_id **, int *,
4187 struct got_tree_object *, const char *, struct got_pathlist_head *,
4188 got_worktree_status_cb status_cb, void *status_arg,
4189 struct got_repository *);
4191 static const struct got_error *
4192 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4193 struct got_tree_entry *te, const char *parent_path,
4194 struct got_pathlist_head *commitable_paths,
4195 got_worktree_status_cb status_cb, void *status_arg,
4196 struct got_repository *repo)
4198 const struct got_error *err = NULL;
4199 struct got_tree_object *subtree;
4202 if (asprintf(&subpath, "%s%s%s", parent_path,
4203 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4204 return got_error_from_errno("asprintf");
4206 err = got_object_open_as_tree(&subtree, repo, &te->id);
4210 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4211 commitable_paths, status_cb, status_arg, repo);
4212 got_object_tree_close(subtree);
4217 static const struct got_error *
4218 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4220 const struct got_error *err = NULL;
4221 char *ct_parent_path = NULL;
4225 if (strchr(ct->in_repo_path, '/') == NULL) {
4226 *match = got_path_is_root_dir(path);
4230 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4233 *match = (strcmp(path, ct_parent_path) == 0);
4234 free(ct_parent_path);
4239 get_ct_file_mode(struct got_commitable *ct)
4241 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4244 static const struct got_error *
4245 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4246 struct got_tree_entry *te, struct got_commitable *ct)
4248 const struct got_error *err = NULL;
4252 err = got_object_tree_entry_dup(new_te, te);
4256 (*new_te)->mode = get_ct_file_mode(ct);
4258 if (ct->staged_status == GOT_STATUS_MODIFY)
4259 memcpy(&(*new_te)->id, ct->staged_blob_id,
4260 sizeof((*new_te)->id));
4262 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4264 if (err && *new_te) {
4271 static const struct got_error *
4272 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4273 struct got_commitable *ct)
4275 const struct got_error *err = NULL;
4280 *new_te = calloc(1, sizeof(**new_te));
4281 if (*new_te == NULL)
4282 return got_error_from_errno("calloc");
4284 ct_name = basename(ct->path);
4285 if (ct_name == NULL) {
4286 err = got_error_from_errno2("basename", ct->path);
4289 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4290 sizeof((*new_te)->name)) {
4291 err = got_error(GOT_ERR_NO_SPACE);
4295 (*new_te)->mode = get_ct_file_mode(ct);
4297 if (ct->staged_status == GOT_STATUS_ADD)
4298 memcpy(&(*new_te)->id, ct->staged_blob_id,
4299 sizeof((*new_te)->id));
4301 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4303 if (err && *new_te) {
4310 static const struct got_error *
4311 insert_tree_entry(struct got_tree_entry *new_te,
4312 struct got_pathlist_head *paths)
4314 const struct got_error *err = NULL;
4315 struct got_pathlist_entry *new_pe;
4317 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4321 return got_error(GOT_ERR_TREE_DUP_ENTRY);
4325 static const struct got_error *
4326 report_ct_status(struct got_commitable *ct,
4327 got_worktree_status_cb status_cb, void *status_arg)
4329 const char *ct_path = ct->path;
4330 unsigned char status;
4332 while (ct_path[0] == '/')
4335 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4336 status = ct->staged_status;
4338 status = ct->status;
4340 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4341 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4344 static const struct got_error *
4345 match_modified_subtree(int *modified, struct got_tree_entry *te,
4346 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4348 const struct got_error *err = NULL;
4349 struct got_pathlist_entry *pe;
4354 if (asprintf(&te_path, "%s%s%s", base_tree_path,
4355 got_path_is_root_dir(base_tree_path) ? "" : "/",
4357 return got_error_from_errno("asprintf");
4359 TAILQ_FOREACH(pe, commitable_paths, entry) {
4360 struct got_commitable *ct = pe->data;
4361 *modified = got_path_is_child(ct->in_repo_path, te_path,
4371 static const struct got_error *
4372 match_deleted_or_modified_ct(struct got_commitable **ctp,
4373 struct got_tree_entry *te, const char *base_tree_path,
4374 struct got_pathlist_head *commitable_paths)
4376 const struct got_error *err = NULL;
4377 struct got_pathlist_entry *pe;
4381 TAILQ_FOREACH(pe, commitable_paths, entry) {
4382 struct got_commitable *ct = pe->data;
4383 char *ct_name = NULL;
4386 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4387 if (ct->status != GOT_STATUS_MODIFY &&
4388 ct->status != GOT_STATUS_MODE_CHANGE &&
4389 ct->status != GOT_STATUS_DELETE)
4392 if (ct->staged_status != GOT_STATUS_MODIFY &&
4393 ct->staged_status != GOT_STATUS_DELETE)
4397 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4400 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4406 ct_name = basename(pe->path);
4407 if (ct_name == NULL)
4408 return got_error_from_errno2("basename", pe->path);
4410 if (strcmp(te->name, ct_name) != 0)
4420 static const struct got_error *
4421 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4422 const char *child_path, const char *path_base_tree,
4423 struct got_pathlist_head *commitable_paths,
4424 got_worktree_status_cb status_cb, void *status_arg,
4425 struct got_repository *repo)
4427 const struct got_error *err = NULL;
4428 struct got_tree_entry *new_te;
4430 struct got_object_id *id = NULL;
4435 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4436 got_path_is_root_dir(path_base_tree) ? "" : "/",
4438 return got_error_from_errno("asprintf");
4440 new_te = calloc(1, sizeof(*new_te));
4442 return got_error_from_errno("calloc");
4443 new_te->mode = S_IFDIR;
4445 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4446 sizeof(new_te->name)) {
4447 err = got_error(GOT_ERR_NO_SPACE);
4450 err = write_tree(&id, &nentries, NULL, subtree_path,
4451 commitable_paths, status_cb, status_arg, repo);
4456 memcpy(&new_te->id, id, sizeof(new_te->id));
4465 static const struct got_error *
4466 write_tree(struct got_object_id **new_tree_id, int *nentries,
4467 struct got_tree_object *base_tree, const char *path_base_tree,
4468 struct got_pathlist_head *commitable_paths,
4469 got_worktree_status_cb status_cb, void *status_arg,
4470 struct got_repository *repo)
4472 const struct got_error *err = NULL;
4473 struct got_pathlist_head paths;
4474 struct got_tree_entry *te, *new_te = NULL;
4475 struct got_pathlist_entry *pe;
4480 /* Insert, and recurse into, newly added entries first. */
4481 TAILQ_FOREACH(pe, commitable_paths, entry) {
4482 struct got_commitable *ct = pe->data;
4483 char *child_path = NULL, *slash;
4485 if ((ct->status != GOT_STATUS_ADD &&
4486 ct->staged_status != GOT_STATUS_ADD) ||
4487 (ct->flags & GOT_COMMITABLE_ADDED))
4490 if (!got_path_is_child(pe->path, path_base_tree,
4491 strlen(path_base_tree)))
4494 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
4499 slash = strchr(child_path, '/');
4500 if (slash == NULL) {
4501 err = alloc_added_blob_tree_entry(&new_te, ct);
4504 err = report_ct_status(ct, status_cb, status_arg);
4507 ct->flags |= GOT_COMMITABLE_ADDED;
4508 err = insert_tree_entry(new_te, &paths);
4513 *slash = '\0'; /* trim trailing path components */
4514 if (base_tree == NULL ||
4515 got_object_tree_find_entry(base_tree, child_path)
4517 err = make_subtree_for_added_blob(&new_te,
4518 child_path, path_base_tree,
4519 commitable_paths, status_cb, status_arg,
4523 err = insert_tree_entry(new_te, &paths);
4532 int i, nbase_entries;
4533 /* Handle modified and deleted entries. */
4534 nbase_entries = got_object_tree_get_nentries(base_tree);
4535 for (i = 0; i < nbase_entries; i++) {
4536 struct got_commitable *ct = NULL;
4538 te = got_object_tree_get_entry(base_tree, i);
4539 if (got_object_tree_entry_is_submodule(te)) {
4540 /* Entry is a submodule; just copy it. */
4541 err = got_object_tree_entry_dup(&new_te, te);
4544 err = insert_tree_entry(new_te, &paths);
4551 if (S_ISDIR(te->mode)) {
4553 err = got_object_tree_entry_dup(&new_te, te);
4556 err = match_modified_subtree(&modified, te,
4557 path_base_tree, commitable_paths);
4560 /* Avoid recursion into unmodified subtrees. */
4562 struct got_object_id *new_id;
4564 err = write_subtree(&new_id,
4566 path_base_tree, commitable_paths,
4567 status_cb, status_arg, repo);
4570 if (nsubentries == 0) {
4571 /* All entries were deleted. */
4575 memcpy(&new_te->id, new_id,
4576 sizeof(new_te->id));
4579 err = insert_tree_entry(new_te, &paths);
4586 err = match_deleted_or_modified_ct(&ct, te,
4587 path_base_tree, commitable_paths);
4591 /* NB: Deleted entries get dropped here. */
4592 if (ct->status == GOT_STATUS_MODIFY ||
4593 ct->status == GOT_STATUS_MODE_CHANGE ||
4594 ct->staged_status == GOT_STATUS_MODIFY) {
4595 err = alloc_modified_blob_tree_entry(
4599 err = insert_tree_entry(new_te, &paths);
4604 err = report_ct_status(ct, status_cb,
4609 /* Entry is unchanged; just copy it. */
4610 err = got_object_tree_entry_dup(&new_te, te);
4613 err = insert_tree_entry(new_te, &paths);
4621 /* Write new list of entries; deleted entries have been dropped. */
4622 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
4624 got_pathlist_free(&paths);
4628 static const struct got_error *
4629 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
4630 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
4631 int have_staged_files)
4633 const struct got_error *err = NULL;
4634 struct got_pathlist_entry *pe;
4636 TAILQ_FOREACH(pe, commitable_paths, entry) {
4637 struct got_fileindex_entry *ie;
4638 struct got_commitable *ct = pe->data;
4640 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4642 if (ct->status == GOT_STATUS_DELETE ||
4643 ct->staged_status == GOT_STATUS_DELETE) {
4644 got_fileindex_entry_remove(fileindex, ie);
4645 } else if (ct->staged_status == GOT_STATUS_ADD ||
4646 ct->staged_status == GOT_STATUS_MODIFY) {
4647 got_fileindex_entry_stage_set(ie,
4648 GOT_FILEIDX_STAGE_NONE);
4649 err = got_fileindex_entry_update(ie,
4650 ct->ondisk_path, ct->staged_blob_id->sha1,
4651 new_base_commit_id->sha1,
4652 !have_staged_files);
4654 err = got_fileindex_entry_update(ie,
4655 ct->ondisk_path, ct->blob_id->sha1,
4656 new_base_commit_id->sha1,
4657 !have_staged_files);
4659 err = got_fileindex_entry_alloc(&ie, pe->path);
4662 err = got_fileindex_entry_update(ie, ct->ondisk_path,
4663 ct->blob_id->sha1, new_base_commit_id->sha1, 1);
4665 got_fileindex_entry_free(ie);
4668 err = got_fileindex_entry_add(fileindex, ie);
4670 got_fileindex_entry_free(ie);
4679 static const struct got_error *
4680 check_out_of_date(const char *in_repo_path, unsigned char status,
4681 unsigned char staged_status, struct got_object_id *base_blob_id,
4682 struct got_object_id *base_commit_id,
4683 struct got_object_id *head_commit_id, struct got_repository *repo,
4686 const struct got_error *err = NULL;
4687 struct got_object_id *id = NULL;
4689 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
4690 /* Trivial case: base commit == head commit */
4691 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
4694 * Ensure file content which local changes were based
4695 * on matches file content in the branch head.
4697 err = got_object_id_by_path(&id, repo, head_commit_id,
4700 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4701 err = got_error(ood_errcode);
4703 } else if (got_object_id_cmp(id, base_blob_id) != 0)
4704 err = got_error(ood_errcode);
4706 /* Require that added files don't exist in the branch head. */
4707 err = got_object_id_by_path(&id, repo, head_commit_id,
4709 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
4711 err = id ? got_error(ood_errcode) : NULL;
4718 const struct got_error *
4719 commit_worktree(struct got_object_id **new_commit_id,
4720 struct got_pathlist_head *commitable_paths,
4721 struct got_object_id *head_commit_id, struct got_worktree *worktree,
4722 const char *author, const char *committer,
4723 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4724 got_worktree_status_cb status_cb, void *status_arg,
4725 struct got_repository *repo)
4727 const struct got_error *err = NULL, *unlockerr = NULL;
4728 struct got_pathlist_entry *pe;
4729 const char *head_ref_name = NULL;
4730 struct got_commit_object *head_commit = NULL;
4731 struct got_reference *head_ref2 = NULL;
4732 struct got_object_id *head_commit_id2 = NULL;
4733 struct got_tree_object *head_tree = NULL;
4734 struct got_object_id *new_tree_id = NULL;
4736 struct got_object_id_queue parent_ids;
4737 struct got_object_qid *pid = NULL;
4738 char *logmsg = NULL;
4740 *new_commit_id = NULL;
4742 SIMPLEQ_INIT(&parent_ids);
4744 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
4748 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
4752 if (commit_msg_cb != NULL) {
4753 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
4758 if (logmsg == NULL || strlen(logmsg) == 0) {
4759 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
4763 /* Create blobs from added and modified files and record their IDs. */
4764 TAILQ_FOREACH(pe, commitable_paths, entry) {
4765 struct got_commitable *ct = pe->data;
4768 /* Blobs for staged files already exist. */
4769 if (ct->staged_status == GOT_STATUS_ADD ||
4770 ct->staged_status == GOT_STATUS_MODIFY)
4773 if (ct->status != GOT_STATUS_ADD &&
4774 ct->status != GOT_STATUS_MODIFY &&
4775 ct->status != GOT_STATUS_MODE_CHANGE)
4778 if (asprintf(&ondisk_path, "%s/%s",
4779 worktree->root_path, pe->path) == -1) {
4780 err = got_error_from_errno("asprintf");
4783 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
4789 /* Recursively write new tree objects. */
4790 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
4791 commitable_paths, status_cb, status_arg, repo);
4795 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
4798 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
4799 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
4800 1, author, time(NULL), committer, time(NULL), logmsg, repo);
4801 got_object_qid_free(pid);
4807 /* Check if a concurrent commit to our branch has occurred. */
4808 head_ref_name = got_worktree_get_head_ref_name(worktree);
4809 if (head_ref_name == NULL) {
4810 err = got_error_from_errno("got_worktree_get_head_ref_name");
4813 /* Lock the reference here to prevent concurrent modification. */
4814 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
4817 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
4820 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
4821 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
4824 /* Update branch head in repository. */
4825 err = got_ref_change_ref(head_ref2, *new_commit_id);
4828 err = got_ref_write(head_ref2, repo);
4832 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
4836 err = ref_base_commit(worktree, repo);
4841 got_object_tree_close(head_tree);
4843 got_object_commit_close(head_commit);
4844 free(head_commit_id2);
4846 unlockerr = got_ref_unlock(head_ref2);
4847 if (unlockerr && err == NULL)
4849 got_ref_close(head_ref2);
4854 static const struct got_error *
4855 check_path_is_commitable(const char *path,
4856 struct got_pathlist_head *commitable_paths)
4858 struct got_pathlist_entry *cpe = NULL;
4859 size_t path_len = strlen(path);
4861 TAILQ_FOREACH(cpe, commitable_paths, entry) {
4862 struct got_commitable *ct = cpe->data;
4863 const char *ct_path = ct->path;
4865 while (ct_path[0] == '/')
4868 if (strcmp(path, ct_path) == 0 ||
4869 got_path_is_child(ct_path, path, path_len))
4874 return got_error_path(path, GOT_ERR_BAD_PATH);
4879 static const struct got_error *
4880 check_staged_file(void *arg, struct got_fileindex_entry *ie)
4882 int *have_staged_files = arg;
4884 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
4885 *have_staged_files = 1;
4886 return got_error(GOT_ERR_CANCELLED);
4892 static const struct got_error *
4893 check_non_staged_files(struct got_fileindex *fileindex,
4894 struct got_pathlist_head *paths)
4896 struct got_pathlist_entry *pe;
4897 struct got_fileindex_entry *ie;
4899 TAILQ_FOREACH(pe, paths, entry) {
4900 if (pe->path[0] == '\0')
4902 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4904 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
4905 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
4906 return got_error_path(pe->path,
4907 GOT_ERR_FILE_NOT_STAGED);
4913 const struct got_error *
4914 got_worktree_commit(struct got_object_id **new_commit_id,
4915 struct got_worktree *worktree, struct got_pathlist_head *paths,
4916 const char *author, const char *committer,
4917 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4918 got_worktree_status_cb status_cb, void *status_arg,
4919 struct got_repository *repo)
4921 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
4922 struct got_fileindex *fileindex = NULL;
4923 char *fileindex_path = NULL;
4924 struct got_pathlist_head commitable_paths;
4925 struct collect_commitables_arg cc_arg;
4926 struct got_pathlist_entry *pe;
4927 struct got_reference *head_ref = NULL;
4928 struct got_object_id *head_commit_id = NULL;
4929 int have_staged_files = 0;
4931 *new_commit_id = NULL;
4933 TAILQ_INIT(&commitable_paths);
4935 err = lock_worktree(worktree, LOCK_EX);
4939 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4943 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4947 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4951 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
4952 &have_staged_files);
4953 if (err && err->code != GOT_ERR_CANCELLED)
4955 if (have_staged_files) {
4956 err = check_non_staged_files(fileindex, paths);
4961 cc_arg.commitable_paths = &commitable_paths;
4962 cc_arg.worktree = worktree;
4964 cc_arg.have_staged_files = have_staged_files;
4965 TAILQ_FOREACH(pe, paths, entry) {
4966 err = worktree_status(worktree, pe->path, fileindex, repo,
4967 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
4972 if (TAILQ_EMPTY(&commitable_paths)) {
4973 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4977 TAILQ_FOREACH(pe, paths, entry) {
4978 err = check_path_is_commitable(pe->path, &commitable_paths);
4983 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4984 struct got_commitable *ct = pe->data;
4985 const char *ct_path = ct->in_repo_path;
4987 while (ct_path[0] == '/')
4989 err = check_out_of_date(ct_path, ct->status,
4990 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
4991 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
4997 err = commit_worktree(new_commit_id, &commitable_paths,
4998 head_commit_id, worktree, author, committer,
4999 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5003 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5004 fileindex, have_staged_files);
5005 sync_err = sync_fileindex(fileindex, fileindex_path);
5006 if (sync_err && err == NULL)
5010 got_fileindex_free(fileindex);
5011 free(fileindex_path);
5012 unlockerr = lock_worktree(worktree, LOCK_SH);
5013 if (unlockerr && err == NULL)
5015 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5016 struct got_commitable *ct = pe->data;
5017 free_commitable(ct);
5019 got_pathlist_free(&commitable_paths);
5024 got_commitable_get_path(struct got_commitable *ct)
5030 got_commitable_get_status(struct got_commitable *ct)
5035 struct check_rebase_ok_arg {
5036 struct got_worktree *worktree;
5037 struct got_repository *repo;
5040 static const struct got_error *
5041 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5043 const struct got_error *err = NULL;
5044 struct check_rebase_ok_arg *a = arg;
5045 unsigned char status;
5049 /* Reject rebase of a work tree with mixed base commits. */
5050 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5051 SHA1_DIGEST_LENGTH))
5052 return got_error(GOT_ERR_MIXED_COMMITS);
5054 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5056 return got_error_from_errno("asprintf");
5058 /* Reject rebase of a work tree with modified or staged files. */
5059 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5064 if (status != GOT_STATUS_NO_CHANGE)
5065 return got_error(GOT_ERR_MODIFIED);
5066 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5067 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5072 const struct got_error *
5073 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5074 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5075 struct got_worktree *worktree, struct got_reference *branch,
5076 struct got_repository *repo)
5078 const struct got_error *err = NULL;
5079 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5080 char *branch_ref_name = NULL;
5081 char *fileindex_path = NULL;
5082 struct check_rebase_ok_arg ok_arg;
5083 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5084 struct got_object_id *wt_branch_tip = NULL;
5086 *new_base_branch_ref = NULL;
5090 err = lock_worktree(worktree, LOCK_EX);
5094 err = open_fileindex(fileindex, &fileindex_path, worktree);
5098 ok_arg.worktree = worktree;
5100 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5105 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5109 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5113 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5117 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5122 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5125 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5126 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5130 err = got_ref_alloc_symref(new_base_branch_ref,
5131 new_base_branch_ref_name, wt_branch);
5134 err = got_ref_write(*new_base_branch_ref, repo);
5138 /* TODO Lock original branch's ref while rebasing? */
5140 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5144 err = got_ref_write(branch_ref, repo);
5148 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5149 worktree->base_commit_id);
5152 err = got_ref_write(*tmp_branch, repo);
5156 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5160 free(fileindex_path);
5161 free(tmp_branch_name);
5162 free(new_base_branch_ref_name);
5163 free(branch_ref_name);
5165 got_ref_close(branch_ref);
5167 got_ref_close(wt_branch);
5168 free(wt_branch_tip);
5170 if (*new_base_branch_ref) {
5171 got_ref_close(*new_base_branch_ref);
5172 *new_base_branch_ref = NULL;
5175 got_ref_close(*tmp_branch);
5179 got_fileindex_free(*fileindex);
5182 lock_worktree(worktree, LOCK_SH);
5187 const struct got_error *
5188 got_worktree_rebase_continue(struct got_object_id **commit_id,
5189 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5190 struct got_reference **branch, struct got_fileindex **fileindex,
5191 struct got_worktree *worktree, struct got_repository *repo)
5193 const struct got_error *err;
5194 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5195 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5196 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5197 char *fileindex_path = NULL;
5198 int have_staged_files = 0;
5201 *new_base_branch = NULL;
5206 err = lock_worktree(worktree, LOCK_EX);
5210 err = open_fileindex(fileindex, &fileindex_path, worktree);
5214 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5215 &have_staged_files);
5216 if (err && err->code != GOT_ERR_CANCELLED)
5218 if (have_staged_files) {
5219 err = got_error(GOT_ERR_STAGED_PATHS);
5223 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5227 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5231 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5235 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5239 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5243 err = got_ref_open(branch, repo,
5244 got_ref_get_symref_target(branch_ref), 0);
5248 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5252 err = got_ref_resolve(commit_id, repo, commit_ref);
5256 err = got_ref_open(new_base_branch, repo,
5257 new_base_branch_ref_name, 0);
5261 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5265 free(commit_ref_name);
5266 free(branch_ref_name);
5267 free(fileindex_path);
5269 got_ref_close(commit_ref);
5271 got_ref_close(branch_ref);
5276 got_ref_close(*tmp_branch);
5279 if (*new_base_branch) {
5280 got_ref_close(*new_base_branch);
5281 *new_base_branch = NULL;
5284 got_ref_close(*branch);
5288 got_fileindex_free(*fileindex);
5291 lock_worktree(worktree, LOCK_SH);
5296 const struct got_error *
5297 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5299 const struct got_error *err;
5300 char *tmp_branch_name = NULL;
5302 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5306 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5307 free(tmp_branch_name);
5311 static const struct got_error *
5312 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5313 char **logmsg, void *arg)
5319 static const struct got_error *
5320 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5321 const char *path, struct got_object_id *blob_id,
5322 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5323 int dirfd, const char *de_name)
5328 struct collect_merged_paths_arg {
5329 got_worktree_checkout_cb progress_cb;
5331 struct got_pathlist_head *merged_paths;
5334 static const struct got_error *
5335 collect_merged_paths(void *arg, unsigned char status, const char *path)
5337 const struct got_error *err;
5338 struct collect_merged_paths_arg *a = arg;
5340 struct got_pathlist_entry *new;
5342 err = (*a->progress_cb)(a->progress_arg, status, path);
5346 if (status != GOT_STATUS_MERGE &&
5347 status != GOT_STATUS_ADD &&
5348 status != GOT_STATUS_DELETE &&
5349 status != GOT_STATUS_CONFLICT)
5354 return got_error_from_errno("strdup");
5356 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5357 if (err || new == NULL)
5363 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5365 struct got_pathlist_entry *pe;
5367 TAILQ_FOREACH(pe, merged_paths, entry)
5368 free((char *)pe->path);
5370 got_pathlist_free(merged_paths);
5373 static const struct got_error *
5374 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5375 int is_rebase, struct got_repository *repo)
5377 const struct got_error *err;
5378 struct got_reference *commit_ref = NULL;
5380 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5382 if (err->code != GOT_ERR_NOT_REF)
5384 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
5387 err = got_ref_write(commit_ref, repo);
5390 } else if (is_rebase) {
5391 struct got_object_id *stored_id;
5394 err = got_ref_resolve(&stored_id, repo, commit_ref);
5397 cmp = got_object_id_cmp(commit_id, stored_id);
5400 err = got_error(GOT_ERR_REBASE_COMMITID);
5406 got_ref_close(commit_ref);
5410 static const struct got_error *
5411 rebase_merge_files(struct got_pathlist_head *merged_paths,
5412 const char *commit_ref_name, struct got_worktree *worktree,
5413 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
5414 struct got_object_id *commit_id, struct got_repository *repo,
5415 got_worktree_checkout_cb progress_cb, void *progress_arg,
5416 got_cancel_cb cancel_cb, void *cancel_arg)
5418 const struct got_error *err;
5419 struct got_reference *commit_ref = NULL;
5420 struct collect_merged_paths_arg cmp_arg;
5421 char *fileindex_path;
5423 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5425 err = get_fileindex_path(&fileindex_path, worktree);
5429 cmp_arg.progress_cb = progress_cb;
5430 cmp_arg.progress_arg = progress_arg;
5431 cmp_arg.merged_paths = merged_paths;
5432 err = merge_files(worktree, fileindex, fileindex_path,
5433 parent_commit_id, commit_id, repo, collect_merged_paths,
5434 &cmp_arg, cancel_cb, cancel_arg);
5436 got_ref_close(commit_ref);
5440 const struct got_error *
5441 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
5442 struct got_worktree *worktree, struct got_fileindex *fileindex,
5443 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5444 struct got_repository *repo,
5445 got_worktree_checkout_cb progress_cb, void *progress_arg,
5446 got_cancel_cb cancel_cb, void *cancel_arg)
5448 const struct got_error *err;
5449 char *commit_ref_name;
5451 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5455 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
5459 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5460 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5461 progress_arg, cancel_cb, cancel_arg);
5463 free(commit_ref_name);
5467 const struct got_error *
5468 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
5469 struct got_worktree *worktree, struct got_fileindex *fileindex,
5470 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5471 struct got_repository *repo,
5472 got_worktree_checkout_cb progress_cb, void *progress_arg,
5473 got_cancel_cb cancel_cb, void *cancel_arg)
5475 const struct got_error *err;
5476 char *commit_ref_name;
5478 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5482 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
5486 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5487 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5488 progress_arg, cancel_cb, cancel_arg);
5490 free(commit_ref_name);
5494 static const struct got_error *
5495 rebase_commit(struct got_object_id **new_commit_id,
5496 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
5497 struct got_worktree *worktree, struct got_fileindex *fileindex,
5498 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
5499 const char *new_logmsg, struct got_repository *repo)
5501 const struct got_error *err, *sync_err;
5502 struct got_pathlist_head commitable_paths;
5503 struct collect_commitables_arg cc_arg;
5504 char *fileindex_path = NULL;
5505 struct got_reference *head_ref = NULL;
5506 struct got_object_id *head_commit_id = NULL;
5507 char *logmsg = NULL;
5509 TAILQ_INIT(&commitable_paths);
5510 *new_commit_id = NULL;
5512 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5514 err = get_fileindex_path(&fileindex_path, worktree);
5518 cc_arg.commitable_paths = &commitable_paths;
5519 cc_arg.worktree = worktree;
5521 cc_arg.have_staged_files = 0;
5523 * If possible get the status of individual files directly to
5524 * avoid crawling the entire work tree once per rebased commit.
5525 * TODO: Ideally, merged_paths would contain a list of commitables
5526 * we could use so we could skip worktree_status() entirely.
5529 struct got_pathlist_entry *pe;
5530 TAILQ_FOREACH(pe, merged_paths, entry) {
5531 err = worktree_status(worktree, pe->path, fileindex,
5532 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
5538 err = worktree_status(worktree, "", fileindex, repo,
5539 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5544 if (TAILQ_EMPTY(&commitable_paths)) {
5545 /* No-op change; commit will be elided. */
5546 err = got_ref_delete(commit_ref, repo);
5549 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5553 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5557 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5562 logmsg = strdup(new_logmsg);
5563 if (logmsg == NULL) {
5564 err = got_error_from_errno("strdup");
5568 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
5573 /* NB: commit_worktree will call free(logmsg) */
5574 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
5575 worktree, got_object_commit_get_author(orig_commit),
5576 got_object_commit_get_committer(orig_commit),
5577 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
5581 err = got_ref_change_ref(tmp_branch, *new_commit_id);
5585 err = got_ref_delete(commit_ref, repo);
5589 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5591 sync_err = sync_fileindex(fileindex, fileindex_path);
5592 if (sync_err && err == NULL)
5595 free(fileindex_path);
5596 free(head_commit_id);
5598 got_ref_close(head_ref);
5600 free(*new_commit_id);
5601 *new_commit_id = NULL;
5606 const struct got_error *
5607 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
5608 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5609 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5610 struct got_commit_object *orig_commit,
5611 struct got_object_id *orig_commit_id, struct got_repository *repo)
5613 const struct got_error *err;
5614 char *commit_ref_name;
5615 struct got_reference *commit_ref = NULL;
5616 struct got_object_id *commit_id = NULL;
5618 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5622 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5625 err = got_ref_resolve(&commit_id, repo, commit_ref);
5628 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5629 err = got_error(GOT_ERR_REBASE_COMMITID);
5633 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5634 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
5637 got_ref_close(commit_ref);
5638 free(commit_ref_name);
5643 const struct got_error *
5644 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
5645 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5646 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5647 struct got_commit_object *orig_commit,
5648 struct got_object_id *orig_commit_id, const char *new_logmsg,
5649 struct got_repository *repo)
5651 const struct got_error *err;
5652 char *commit_ref_name;
5653 struct got_reference *commit_ref = NULL;
5655 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5659 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5663 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5664 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
5667 got_ref_close(commit_ref);
5668 free(commit_ref_name);
5672 const struct got_error *
5673 got_worktree_rebase_postpone(struct got_worktree *worktree,
5674 struct got_fileindex *fileindex)
5677 got_fileindex_free(fileindex);
5678 return lock_worktree(worktree, LOCK_SH);
5681 static const struct got_error *
5682 delete_ref(const char *name, struct got_repository *repo)
5684 const struct got_error *err;
5685 struct got_reference *ref;
5687 err = got_ref_open(&ref, repo, name, 0);
5689 if (err->code == GOT_ERR_NOT_REF)
5694 err = got_ref_delete(ref, repo);
5699 static const struct got_error *
5700 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
5702 const struct got_error *err;
5703 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5704 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5706 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5709 err = delete_ref(tmp_branch_name, repo);
5713 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5716 err = delete_ref(new_base_branch_ref_name, repo);
5720 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5723 err = delete_ref(branch_ref_name, repo);
5727 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5730 err = delete_ref(commit_ref_name, repo);
5735 free(tmp_branch_name);
5736 free(new_base_branch_ref_name);
5737 free(branch_ref_name);
5738 free(commit_ref_name);
5742 const struct got_error *
5743 got_worktree_rebase_complete(struct got_worktree *worktree,
5744 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
5745 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
5746 struct got_repository *repo)
5748 const struct got_error *err, *unlockerr;
5749 struct got_object_id *new_head_commit_id = NULL;
5751 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5755 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
5759 err = got_ref_write(rebased_branch, repo);
5763 err = got_worktree_set_head_ref(worktree, rebased_branch);
5767 err = delete_rebase_refs(worktree, repo);
5770 got_fileindex_free(fileindex);
5771 free(new_head_commit_id);
5772 unlockerr = lock_worktree(worktree, LOCK_SH);
5773 if (unlockerr && err == NULL)
5778 const struct got_error *
5779 got_worktree_rebase_abort(struct got_worktree *worktree,
5780 struct got_fileindex *fileindex, struct got_repository *repo,
5781 struct got_reference *new_base_branch,
5782 got_worktree_checkout_cb progress_cb, void *progress_arg)
5784 const struct got_error *err, *unlockerr, *sync_err;
5785 struct got_reference *resolved = NULL;
5786 struct got_object_id *commit_id = NULL;
5787 char *fileindex_path = NULL;
5788 struct revert_file_args rfa;
5789 struct got_object_id *tree_id = NULL;
5791 err = lock_worktree(worktree, LOCK_EX);
5795 err = got_ref_open(&resolved, repo,
5796 got_ref_get_symref_target(new_base_branch), 0);
5800 err = got_worktree_set_head_ref(worktree, resolved);
5805 * XXX commits to the base branch could have happened while
5806 * we were busy rebasing; should we store the original commit ID
5807 * when rebase begins and read it back here?
5809 err = got_ref_resolve(&commit_id, repo, resolved);
5813 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5817 err = got_object_id_by_path(&tree_id, repo,
5818 worktree->base_commit_id, worktree->path_prefix);
5822 err = delete_rebase_refs(worktree, repo);
5826 err = get_fileindex_path(&fileindex_path, worktree);
5830 rfa.worktree = worktree;
5831 rfa.fileindex = fileindex;
5832 rfa.progress_cb = progress_cb;
5833 rfa.progress_arg = progress_arg;
5834 rfa.patch_cb = NULL;
5835 rfa.patch_arg = NULL;
5837 err = worktree_status(worktree, "", fileindex, repo,
5838 revert_file, &rfa, NULL, NULL, 0, 0);
5842 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5843 repo, progress_cb, progress_arg, NULL, NULL);
5845 sync_err = sync_fileindex(fileindex, fileindex_path);
5846 if (sync_err && err == NULL)
5849 got_ref_close(resolved);
5853 got_fileindex_free(fileindex);
5854 free(fileindex_path);
5856 unlockerr = lock_worktree(worktree, LOCK_SH);
5857 if (unlockerr && err == NULL)
5862 const struct got_error *
5863 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
5864 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
5865 struct got_fileindex **fileindex, struct got_worktree *worktree,
5866 struct got_repository *repo)
5868 const struct got_error *err = NULL;
5869 char *tmp_branch_name = NULL;
5870 char *branch_ref_name = NULL;
5871 char *base_commit_ref_name = NULL;
5872 char *fileindex_path = NULL;
5873 struct check_rebase_ok_arg ok_arg;
5874 struct got_reference *wt_branch = NULL;
5875 struct got_reference *base_commit_ref = NULL;
5879 *base_commit_id = NULL;
5882 err = lock_worktree(worktree, LOCK_EX);
5886 err = open_fileindex(fileindex, &fileindex_path, worktree);
5890 ok_arg.worktree = worktree;
5892 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5897 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5901 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5905 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5910 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5915 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
5919 err = got_ref_write(*branch_ref, repo);
5923 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
5924 worktree->base_commit_id);
5927 err = got_ref_write(base_commit_ref, repo);
5930 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
5931 if (*base_commit_id == NULL) {
5932 err = got_error_from_errno("got_object_id_dup");
5936 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5937 worktree->base_commit_id);
5940 err = got_ref_write(*tmp_branch, repo);
5944 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5948 free(fileindex_path);
5949 free(tmp_branch_name);
5950 free(branch_ref_name);
5951 free(base_commit_ref_name);
5953 got_ref_close(wt_branch);
5956 got_ref_close(*branch_ref);
5960 got_ref_close(*tmp_branch);
5963 free(*base_commit_id);
5965 got_fileindex_free(*fileindex);
5968 lock_worktree(worktree, LOCK_SH);
5973 const struct got_error *
5974 got_worktree_histedit_postpone(struct got_worktree *worktree,
5975 struct got_fileindex *fileindex)
5978 got_fileindex_free(fileindex);
5979 return lock_worktree(worktree, LOCK_SH);
5982 const struct got_error *
5983 got_worktree_histedit_in_progress(int *in_progress,
5984 struct got_worktree *worktree)
5986 const struct got_error *err;
5987 char *tmp_branch_name = NULL;
5989 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5993 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5994 free(tmp_branch_name);
5998 const struct got_error *
5999 got_worktree_histedit_continue(struct got_object_id **commit_id,
6000 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6001 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6002 struct got_worktree *worktree, struct got_repository *repo)
6004 const struct got_error *err;
6005 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6006 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6007 struct got_reference *commit_ref = NULL;
6008 struct got_reference *base_commit_ref = NULL;
6009 char *fileindex_path = NULL;
6010 int have_staged_files = 0;
6014 *base_commit_id = NULL;
6017 err = lock_worktree(worktree, LOCK_EX);
6021 err = open_fileindex(fileindex, &fileindex_path, worktree);
6025 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6026 &have_staged_files);
6027 if (err && err->code != GOT_ERR_CANCELLED)
6029 if (have_staged_files) {
6030 err = got_error(GOT_ERR_STAGED_PATHS);
6034 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6038 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6042 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6046 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6051 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6055 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6058 err = got_ref_resolve(commit_id, repo, commit_ref);
6062 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6065 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6069 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6073 free(commit_ref_name);
6074 free(branch_ref_name);
6075 free(fileindex_path);
6077 got_ref_close(commit_ref);
6078 if (base_commit_ref)
6079 got_ref_close(base_commit_ref);
6083 free(*base_commit_id);
6084 *base_commit_id = NULL;
6086 got_ref_close(*tmp_branch);
6090 got_fileindex_free(*fileindex);
6093 lock_worktree(worktree, LOCK_EX);
6098 static const struct got_error *
6099 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6101 const struct got_error *err;
6102 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6103 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6105 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6108 err = delete_ref(tmp_branch_name, repo);
6112 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6116 err = delete_ref(base_commit_ref_name, repo);
6120 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6123 err = delete_ref(branch_ref_name, repo);
6127 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6130 err = delete_ref(commit_ref_name, repo);
6134 free(tmp_branch_name);
6135 free(base_commit_ref_name);
6136 free(branch_ref_name);
6137 free(commit_ref_name);
6141 const struct got_error *
6142 got_worktree_histedit_abort(struct got_worktree *worktree,
6143 struct got_fileindex *fileindex, struct got_repository *repo,
6144 struct got_reference *branch, struct got_object_id *base_commit_id,
6145 got_worktree_checkout_cb progress_cb, void *progress_arg)
6147 const struct got_error *err, *unlockerr, *sync_err;
6148 struct got_reference *resolved = NULL;
6149 char *fileindex_path = NULL;
6150 struct got_object_id *tree_id = NULL;
6151 struct revert_file_args rfa;
6153 err = lock_worktree(worktree, LOCK_EX);
6157 err = got_ref_open(&resolved, repo,
6158 got_ref_get_symref_target(branch), 0);
6162 err = got_worktree_set_head_ref(worktree, resolved);
6166 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6170 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6171 worktree->path_prefix);
6175 err = delete_histedit_refs(worktree, repo);
6179 err = get_fileindex_path(&fileindex_path, worktree);
6183 rfa.worktree = worktree;
6184 rfa.fileindex = fileindex;
6185 rfa.progress_cb = progress_cb;
6186 rfa.progress_arg = progress_arg;
6187 rfa.patch_cb = NULL;
6188 rfa.patch_arg = NULL;
6190 err = worktree_status(worktree, "", fileindex, repo,
6191 revert_file, &rfa, NULL, NULL, 0, 0);
6195 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6196 repo, progress_cb, progress_arg, NULL, NULL);
6198 sync_err = sync_fileindex(fileindex, fileindex_path);
6199 if (sync_err && err == NULL)
6202 got_ref_close(resolved);
6204 free(fileindex_path);
6206 unlockerr = lock_worktree(worktree, LOCK_SH);
6207 if (unlockerr && err == NULL)
6212 const struct got_error *
6213 got_worktree_histedit_complete(struct got_worktree *worktree,
6214 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6215 struct got_reference *edited_branch, struct got_repository *repo)
6217 const struct got_error *err, *unlockerr;
6218 struct got_object_id *new_head_commit_id = NULL;
6219 struct got_reference *resolved = NULL;
6221 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6225 err = got_ref_open(&resolved, repo,
6226 got_ref_get_symref_target(edited_branch), 0);
6230 err = got_ref_change_ref(resolved, new_head_commit_id);
6234 err = got_ref_write(resolved, repo);
6238 err = got_worktree_set_head_ref(worktree, resolved);
6242 err = delete_histedit_refs(worktree, repo);
6245 got_fileindex_free(fileindex);
6246 free(new_head_commit_id);
6247 unlockerr = lock_worktree(worktree, LOCK_SH);
6248 if (unlockerr && err == NULL)
6253 const struct got_error *
6254 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6255 struct got_object_id *commit_id, struct got_repository *repo)
6257 const struct got_error *err;
6258 char *commit_ref_name;
6260 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6264 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6268 err = delete_ref(commit_ref_name, repo);
6270 free(commit_ref_name);
6274 const struct got_error *
6275 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6276 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6277 struct got_worktree *worktree, const char *refname,
6278 struct got_repository *repo)
6280 const struct got_error *err = NULL;
6281 char *fileindex_path = NULL;
6282 struct check_rebase_ok_arg ok_arg;
6286 *base_branch_ref = NULL;
6288 err = lock_worktree(worktree, LOCK_EX);
6292 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6293 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6294 "cannot integrate a branch into itself; "
6295 "update -b or different branch name required");
6299 err = open_fileindex(fileindex, &fileindex_path, worktree);
6303 /* Preconditions are the same as for rebase. */
6304 ok_arg.worktree = worktree;
6306 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6311 err = got_ref_open(branch_ref, repo, refname, 1);
6315 err = got_ref_open(base_branch_ref, repo,
6316 got_worktree_get_head_ref_name(worktree), 1);
6320 got_ref_close(*branch_ref);
6323 if (*base_branch_ref) {
6324 got_ref_close(*base_branch_ref);
6325 *base_branch_ref = NULL;
6328 got_fileindex_free(*fileindex);
6331 lock_worktree(worktree, LOCK_SH);
6336 const struct got_error *
6337 got_worktree_integrate_continue(struct got_worktree *worktree,
6338 struct got_fileindex *fileindex, struct got_repository *repo,
6339 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6340 got_worktree_checkout_cb progress_cb, void *progress_arg,
6341 got_cancel_cb cancel_cb, void *cancel_arg)
6343 const struct got_error *err = NULL, *sync_err, *unlockerr;
6344 char *fileindex_path = NULL;
6345 struct got_object_id *tree_id = NULL, *commit_id = NULL;
6347 err = get_fileindex_path(&fileindex_path, worktree);
6351 err = got_ref_resolve(&commit_id, repo, branch_ref);
6355 err = got_object_id_by_path(&tree_id, repo, commit_id,
6356 worktree->path_prefix);
6360 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6364 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6365 progress_cb, progress_arg, cancel_cb, cancel_arg);
6369 err = got_ref_change_ref(base_branch_ref, commit_id);
6373 err = got_ref_write(base_branch_ref, repo);
6375 sync_err = sync_fileindex(fileindex, fileindex_path);
6376 if (sync_err && err == NULL)
6380 unlockerr = got_ref_unlock(branch_ref);
6381 if (unlockerr && err == NULL)
6383 got_ref_close(branch_ref);
6385 unlockerr = got_ref_unlock(base_branch_ref);
6386 if (unlockerr && err == NULL)
6388 got_ref_close(base_branch_ref);
6390 got_fileindex_free(fileindex);
6391 free(fileindex_path);
6394 unlockerr = lock_worktree(worktree, LOCK_SH);
6395 if (unlockerr && err == NULL)
6400 const struct got_error *
6401 got_worktree_integrate_abort(struct got_worktree *worktree,
6402 struct got_fileindex *fileindex, struct got_repository *repo,
6403 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
6405 const struct got_error *err = NULL, *unlockerr = NULL;
6407 got_fileindex_free(fileindex);
6409 err = lock_worktree(worktree, LOCK_SH);
6411 unlockerr = got_ref_unlock(branch_ref);
6412 if (unlockerr && err == NULL)
6414 got_ref_close(branch_ref);
6416 unlockerr = got_ref_unlock(base_branch_ref);
6417 if (unlockerr && err == NULL)
6419 got_ref_close(base_branch_ref);
6424 struct check_stage_ok_arg {
6425 struct got_object_id *head_commit_id;
6426 struct got_worktree *worktree;
6427 struct got_fileindex *fileindex;
6428 struct got_repository *repo;
6432 const struct got_error *
6433 check_stage_ok(void *arg, unsigned char status,
6434 unsigned char staged_status, const char *relpath,
6435 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6436 struct got_object_id *commit_id, int dirfd, const char *de_name)
6438 struct check_stage_ok_arg *a = arg;
6439 const struct got_error *err = NULL;
6440 struct got_fileindex_entry *ie;
6441 struct got_object_id base_commit_id;
6442 struct got_object_id *base_commit_idp = NULL;
6443 char *in_repo_path = NULL, *p;
6445 if (status == GOT_STATUS_UNVERSIONED ||
6446 status == GOT_STATUS_NO_CHANGE)
6448 if (status == GOT_STATUS_NONEXISTENT)
6449 return got_error_set_errno(ENOENT, relpath);
6451 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6453 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6455 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
6456 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
6458 return got_error_from_errno("asprintf");
6460 if (got_fileindex_entry_has_commit(ie)) {
6461 memcpy(base_commit_id.sha1, ie->commit_sha1,
6462 SHA1_DIGEST_LENGTH);
6463 base_commit_idp = &base_commit_id;
6466 if (status == GOT_STATUS_CONFLICT) {
6467 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
6469 } else if (status != GOT_STATUS_ADD &&
6470 status != GOT_STATUS_MODIFY &&
6471 status != GOT_STATUS_DELETE) {
6472 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
6476 a->have_changes = 1;
6481 err = check_out_of_date(p, status, staged_status,
6482 blob_id, base_commit_idp, a->head_commit_id, a->repo,
6483 GOT_ERR_STAGE_OUT_OF_DATE);
6489 struct stage_path_arg {
6490 struct got_worktree *worktree;
6491 struct got_fileindex *fileindex;
6492 struct got_repository *repo;
6493 got_worktree_status_cb status_cb;
6495 got_worktree_patch_cb patch_cb;
6497 int staged_something;
6500 static const struct got_error *
6501 stage_path(void *arg, unsigned char status,
6502 unsigned char staged_status, const char *relpath,
6503 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6504 struct got_object_id *commit_id, int dirfd, const char *de_name)
6506 struct stage_path_arg *a = arg;
6507 const struct got_error *err = NULL;
6508 struct got_fileindex_entry *ie;
6509 char *ondisk_path = NULL, *path_content = NULL;
6511 struct got_object_id *new_staged_blob_id = NULL;
6513 if (status == GOT_STATUS_UNVERSIONED)
6516 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6518 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6520 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
6522 return got_error_from_errno("asprintf");
6525 case GOT_STATUS_ADD:
6526 case GOT_STATUS_MODIFY:
6528 if (status == GOT_STATUS_ADD) {
6529 int choice = GOT_PATCH_CHOICE_NONE;
6530 err = (*a->patch_cb)(&choice, a->patch_arg,
6531 status, ie->path, NULL, 1, 1);
6534 if (choice != GOT_PATCH_CHOICE_YES)
6537 err = create_patched_content(&path_content, 0,
6538 staged_blob_id ? staged_blob_id : blob_id,
6539 ondisk_path, dirfd, de_name, ie->path,
6540 a->repo, a->patch_cb, a->patch_arg);
6541 if (err || path_content == NULL)
6545 err = got_object_blob_create(&new_staged_blob_id,
6546 path_content ? path_content : ondisk_path, a->repo);
6549 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
6550 SHA1_DIGEST_LENGTH);
6551 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
6552 stage = GOT_FILEIDX_STAGE_ADD;
6554 stage = GOT_FILEIDX_STAGE_MODIFY;
6555 got_fileindex_entry_stage_set(ie, stage);
6556 a->staged_something = 1;
6557 if (a->status_cb == NULL)
6559 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6560 get_staged_status(ie), relpath, blob_id,
6561 new_staged_blob_id, NULL, dirfd, de_name);
6563 case GOT_STATUS_DELETE:
6564 if (staged_status == GOT_STATUS_DELETE)
6567 int choice = GOT_PATCH_CHOICE_NONE;
6568 err = (*a->patch_cb)(&choice, a->patch_arg, status,
6569 ie->path, NULL, 1, 1);
6572 if (choice == GOT_PATCH_CHOICE_NO)
6574 if (choice != GOT_PATCH_CHOICE_YES) {
6575 err = got_error(GOT_ERR_PATCH_CHOICE);
6579 stage = GOT_FILEIDX_STAGE_DELETE;
6580 got_fileindex_entry_stage_set(ie, stage);
6581 a->staged_something = 1;
6582 if (a->status_cb == NULL)
6584 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6585 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
6588 case GOT_STATUS_NO_CHANGE:
6590 case GOT_STATUS_CONFLICT:
6591 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
6593 case GOT_STATUS_NONEXISTENT:
6594 err = got_error_set_errno(ENOENT, relpath);
6597 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
6601 if (path_content && unlink(path_content) == -1 && err == NULL)
6602 err = got_error_from_errno2("unlink", path_content);
6605 free(new_staged_blob_id);
6609 const struct got_error *
6610 got_worktree_stage(struct got_worktree *worktree,
6611 struct got_pathlist_head *paths,
6612 got_worktree_status_cb status_cb, void *status_arg,
6613 got_worktree_patch_cb patch_cb, void *patch_arg,
6614 struct got_repository *repo)
6616 const struct got_error *err = NULL, *sync_err, *unlockerr;
6617 struct got_pathlist_entry *pe;
6618 struct got_fileindex *fileindex = NULL;
6619 char *fileindex_path = NULL;
6620 struct got_reference *head_ref = NULL;
6621 struct got_object_id *head_commit_id = NULL;
6622 struct check_stage_ok_arg oka;
6623 struct stage_path_arg spa;
6625 err = lock_worktree(worktree, LOCK_EX);
6629 err = got_ref_open(&head_ref, repo,
6630 got_worktree_get_head_ref_name(worktree), 0);
6633 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6636 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6640 /* Check pre-conditions before staging anything. */
6641 oka.head_commit_id = head_commit_id;
6642 oka.worktree = worktree;
6643 oka.fileindex = fileindex;
6645 oka.have_changes = 0;
6646 TAILQ_FOREACH(pe, paths, entry) {
6647 err = worktree_status(worktree, pe->path, fileindex, repo,
6648 check_stage_ok, &oka, NULL, NULL, 0, 0);
6652 if (!oka.have_changes) {
6653 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6657 spa.worktree = worktree;
6658 spa.fileindex = fileindex;
6660 spa.patch_cb = patch_cb;
6661 spa.patch_arg = patch_arg;
6662 spa.status_cb = status_cb;
6663 spa.status_arg = status_arg;
6664 spa.staged_something = 0;
6665 TAILQ_FOREACH(pe, paths, entry) {
6666 err = worktree_status(worktree, pe->path, fileindex, repo,
6667 stage_path, &spa, NULL, NULL, 0, 0);
6671 if (!spa.staged_something) {
6672 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6676 sync_err = sync_fileindex(fileindex, fileindex_path);
6677 if (sync_err && err == NULL)
6681 got_ref_close(head_ref);
6682 free(head_commit_id);
6683 free(fileindex_path);
6685 got_fileindex_free(fileindex);
6686 unlockerr = lock_worktree(worktree, LOCK_SH);
6687 if (unlockerr && err == NULL)
6692 struct unstage_path_arg {
6693 struct got_worktree *worktree;
6694 struct got_fileindex *fileindex;
6695 struct got_repository *repo;
6696 got_worktree_checkout_cb progress_cb;
6698 got_worktree_patch_cb patch_cb;
6702 static const struct got_error *
6703 create_unstaged_content(char **path_unstaged_content,
6704 char **path_new_staged_content, struct got_object_id *blob_id,
6705 struct got_object_id *staged_blob_id, const char *relpath,
6706 struct got_repository *repo,
6707 got_worktree_patch_cb patch_cb, void *patch_arg)
6709 const struct got_error *err;
6710 struct got_blob_object *blob = NULL, *staged_blob = NULL;
6711 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
6712 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
6713 struct stat sb1, sb2;
6714 struct got_diff_changes *changes = NULL;
6715 struct got_diff_state *ds = NULL;
6716 struct got_diff_args *args = NULL;
6717 struct got_diff_change *change;
6718 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
6719 int have_content = 0, have_rejected_content = 0;
6721 *path_unstaged_content = NULL;
6722 *path_new_staged_content = NULL;
6724 err = got_object_id_str(&label1, blob_id);
6727 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
6731 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
6735 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
6739 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
6743 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
6747 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
6751 if (stat(path1, &sb1) == -1) {
6752 err = got_error_from_errno2("stat", path1);
6756 if (stat(path2, &sb2) == -1) {
6757 err = got_error_from_errno2("stat", path2);
6761 err = got_diff_files(&changes, &ds, &args, &diff_flags,
6762 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
6766 err = got_opentemp_named(path_unstaged_content, &outfile,
6767 "got-unstaged-content");
6770 err = got_opentemp_named(path_new_staged_content, &rejectfile,
6771 "got-new-staged-content");
6775 if (fseek(f1, 0L, SEEK_SET) == -1) {
6776 err = got_ferror(f1, GOT_ERR_IO);
6779 if (fseek(f2, 0L, SEEK_SET) == -1) {
6780 err = got_ferror(f2, GOT_ERR_IO);
6783 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
6785 err = apply_or_reject_change(&choice, change, ++n,
6786 changes->nchanges, ds, args, diff_flags, relpath,
6787 f1, f2, &line_cur1, &line_cur2,
6788 outfile, rejectfile, patch_cb, patch_arg);
6791 if (choice == GOT_PATCH_CHOICE_YES)
6794 have_rejected_content = 1;
6795 if (choice == GOT_PATCH_CHOICE_QUIT)
6798 if (have_content || have_rejected_content)
6799 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
6800 outfile, rejectfile);
6804 got_object_blob_close(blob);
6806 got_object_blob_close(staged_blob);
6807 if (f1 && fclose(f1) == EOF && err == NULL)
6808 err = got_error_from_errno2("fclose", path1);
6809 if (f2 && fclose(f2) == EOF && err == NULL)
6810 err = got_error_from_errno2("fclose", path2);
6811 if (outfile && fclose(outfile) == EOF && err == NULL)
6812 err = got_error_from_errno2("fclose", *path_unstaged_content);
6813 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
6814 err = got_error_from_errno2("fclose", *path_new_staged_content);
6815 if (path1 && unlink(path1) == -1 && err == NULL)
6816 err = got_error_from_errno2("unlink", path1);
6817 if (path2 && unlink(path2) == -1 && err == NULL)
6818 err = got_error_from_errno2("unlink", path2);
6819 if (err || !have_content) {
6820 if (*path_unstaged_content &&
6821 unlink(*path_unstaged_content) == -1 && err == NULL)
6822 err = got_error_from_errno2("unlink",
6823 *path_unstaged_content);
6824 free(*path_unstaged_content);
6825 *path_unstaged_content = NULL;
6827 if (err || !have_rejected_content) {
6828 if (*path_new_staged_content &&
6829 unlink(*path_new_staged_content) == -1 && err == NULL)
6830 err = got_error_from_errno2("unlink",
6831 *path_new_staged_content);
6832 free(*path_new_staged_content);
6833 *path_new_staged_content = NULL;
6837 got_diff_state_free(ds);
6841 got_diff_free_changes(changes);
6847 static const struct got_error *
6848 unstage_path(void *arg, unsigned char status,
6849 unsigned char staged_status, const char *relpath,
6850 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6851 struct got_object_id *commit_id, int dirfd, const char *de_name)
6853 const struct got_error *err = NULL;
6854 struct unstage_path_arg *a = arg;
6855 struct got_fileindex_entry *ie;
6856 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
6857 char *ondisk_path = NULL, *path_unstaged_content = NULL;
6858 char *path_new_staged_content = NULL;
6859 char *id_str = NULL, *label_orig = NULL;
6860 int local_changes_subsumed;
6863 if (staged_status != GOT_STATUS_ADD &&
6864 staged_status != GOT_STATUS_MODIFY &&
6865 staged_status != GOT_STATUS_DELETE)
6868 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6870 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6872 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
6874 return got_error_from_errno("asprintf");
6876 err = got_object_id_str(&id_str,
6877 commit_id ? commit_id : a->worktree->base_commit_id);
6880 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
6882 err = got_error_from_errno("asprintf");
6886 switch (staged_status) {
6887 case GOT_STATUS_MODIFY:
6888 err = got_object_open_as_blob(&blob_base, a->repo,
6893 case GOT_STATUS_ADD:
6895 if (staged_status == GOT_STATUS_ADD) {
6896 int choice = GOT_PATCH_CHOICE_NONE;
6897 err = (*a->patch_cb)(&choice, a->patch_arg,
6898 staged_status, ie->path, NULL, 1, 1);
6901 if (choice != GOT_PATCH_CHOICE_YES)
6904 err = create_unstaged_content(
6905 &path_unstaged_content,
6906 &path_new_staged_content, blob_id,
6907 staged_blob_id, ie->path, a->repo,
6908 a->patch_cb, a->patch_arg);
6909 if (err || path_unstaged_content == NULL)
6911 if (path_new_staged_content) {
6912 err = got_object_blob_create(
6914 path_new_staged_content,
6918 memcpy(ie->staged_blob_sha1,
6919 staged_blob_id->sha1,
6920 SHA1_DIGEST_LENGTH);
6922 err = merge_file(&local_changes_subsumed,
6923 a->worktree, blob_base, ondisk_path,
6924 relpath, got_fileindex_perms_to_st(ie),
6925 path_unstaged_content, label_orig,
6926 "unstaged", a->repo, a->progress_cb,
6929 path_new_staged_content == NULL)
6930 got_fileindex_entry_stage_set(ie,
6931 GOT_FILEIDX_STAGE_NONE);
6932 break; /* Done with this file. */
6935 err = got_object_open_as_blob(&blob_staged, a->repo,
6936 staged_blob_id, 8192);
6939 err = merge_blob(&local_changes_subsumed, a->worktree,
6940 blob_base, ondisk_path, relpath,
6941 got_fileindex_perms_to_st(ie), label_orig, blob_staged,
6942 commit_id ? commit_id : a->worktree->base_commit_id,
6943 a->repo, a->progress_cb, a->progress_arg);
6945 got_fileindex_entry_stage_set(ie,
6946 GOT_FILEIDX_STAGE_NONE);
6948 case GOT_STATUS_DELETE:
6950 int choice = GOT_PATCH_CHOICE_NONE;
6951 err = (*a->patch_cb)(&choice, a->patch_arg,
6952 staged_status, ie->path, NULL, 1, 1);
6955 if (choice == GOT_PATCH_CHOICE_NO)
6957 if (choice != GOT_PATCH_CHOICE_YES) {
6958 err = got_error(GOT_ERR_PATCH_CHOICE);
6962 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
6963 err = get_file_status(&status, &sb, ie, ondisk_path,
6964 dirfd, de_name, a->repo);
6967 err = (*a->progress_cb)(a->progress_arg, status, relpath);
6972 if (path_unstaged_content &&
6973 unlink(path_unstaged_content) == -1 && err == NULL)
6974 err = got_error_from_errno2("unlink", path_unstaged_content);
6975 if (path_new_staged_content &&
6976 unlink(path_new_staged_content) == -1 && err == NULL)
6977 err = got_error_from_errno2("unlink", path_new_staged_content);
6978 free(path_unstaged_content);
6979 free(path_new_staged_content);
6981 got_object_blob_close(blob_base);
6983 got_object_blob_close(blob_staged);
6989 const struct got_error *
6990 got_worktree_unstage(struct got_worktree *worktree,
6991 struct got_pathlist_head *paths,
6992 got_worktree_checkout_cb progress_cb, void *progress_arg,
6993 got_worktree_patch_cb patch_cb, void *patch_arg,
6994 struct got_repository *repo)
6996 const struct got_error *err = NULL, *sync_err, *unlockerr;
6997 struct got_pathlist_entry *pe;
6998 struct got_fileindex *fileindex = NULL;
6999 char *fileindex_path = NULL;
7000 struct unstage_path_arg upa;
7002 err = lock_worktree(worktree, LOCK_EX);
7006 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7010 upa.worktree = worktree;
7011 upa.fileindex = fileindex;
7013 upa.progress_cb = progress_cb;
7014 upa.progress_arg = progress_arg;
7015 upa.patch_cb = patch_cb;
7016 upa.patch_arg = patch_arg;
7017 TAILQ_FOREACH(pe, paths, entry) {
7018 err = worktree_status(worktree, pe->path, fileindex, repo,
7019 unstage_path, &upa, NULL, NULL, 0, 0);
7024 sync_err = sync_fileindex(fileindex, fileindex_path);
7025 if (sync_err && err == NULL)
7028 free(fileindex_path);
7030 got_fileindex_free(fileindex);
7031 unlockerr = lock_worktree(worktree, LOCK_SH);
7032 if (unlockerr && err == NULL)