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 char *path_got = NULL;
958 const uint8_t *buf = got_object_blob_get_read_buf(blob);
959 size_t hdrlen = got_object_blob_get_hdrlen(blob);
962 * Blob object content specifies the target path of the link.
963 * If a symbolic link cannot be installed we instead create
964 * a regular file which contains the link target path stored
965 * in the blob object.
968 err = got_object_blob_read_block(&len, blob);
969 if (len + target_len >= sizeof(target_path)) {
970 /* Path too long; install as a regular file. */
971 got_object_blob_rewind(blob);
972 return install_blob(worktree, ondisk_path, path,
973 GOT_DEFAULT_FILE_MODE, st_mode, blob,
974 restoring_missing_file, reverting_versioned_file,
975 repo, progress_cb, progress_arg);
978 /* Skip blob object header first time around. */
979 memcpy(target_path + target_len, buf + hdrlen,
981 target_len += len - hdrlen;
985 target_path[target_len] = '\0';
988 * Relative symlink target lookup should begin at the directory
989 * in which the blob object is being installed.
991 if (!got_path_is_absolute(target_path)) {
992 char *parent = dirname(ondisk_path);
993 if (parent == NULL) {
994 err = got_error_from_errno2("dirname", ondisk_path);
997 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
998 err = got_error_from_errno("asprintf");
1004 * unveil(2) restricts our view of paths in the filesystem.
1005 * ENOENT will occur if a link target path does not exist or
1006 * if it points outside our unveiled path space.
1008 resolved_path = realpath(abspath ? abspath : target_path, NULL);
1009 if (resolved_path == NULL) {
1010 if (errno != ENOENT) {
1011 err = got_error_from_errno2("realpath", target_path);
1016 /* Only allow symlinks pointing at paths within the work tree. */
1017 if (!got_path_is_child(resolved_path ? resolved_path : (abspath ?
1018 abspath : target_path), worktree->root_path,
1019 strlen(worktree->root_path))) {
1020 /* install as a regular file */
1021 got_object_blob_rewind(blob);
1022 err = install_blob(worktree, ondisk_path, path,
1023 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1024 restoring_missing_file, reverting_versioned_file,
1025 repo, progress_cb, progress_arg);
1029 /* Do not allow symlinks pointing into the .got directory. */
1030 if (asprintf(&path_got, "%s/%s", worktree->root_path,
1031 GOT_WORKTREE_GOT_DIR) == -1) {
1032 err = got_error_from_errno("asprintf");
1035 if (got_path_is_child(resolved_path ? resolved_path : (abspath ?
1036 abspath : target_path), path_got, strlen(path_got))) {
1037 /* install as a regular file */
1038 got_object_blob_rewind(blob);
1039 err = install_blob(worktree, ondisk_path, path,
1040 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1041 restoring_missing_file, reverting_versioned_file,
1042 repo, progress_cb, progress_arg);
1046 if (symlink(target_path, ondisk_path) == -1) {
1047 if (errno == EEXIST) {
1050 char etarget[PATH_MAX];
1051 if (lstat(ondisk_path, &sb) == -1) {
1052 err = got_error_from_errno2("lstat",
1056 if (!S_ISLNK(sb.st_mode)) {
1057 err = got_error_path(ondisk_path,
1058 GOT_ERR_FILE_OBSTRUCTED);
1061 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1063 err = got_error_from_errno2("readlink",
1067 if (elen == target_len &&
1068 memcmp(etarget, target_path, target_len) == 0) {
1069 err = NULL; /* nothing to do */
1072 if (unlink(ondisk_path) == -1) {
1073 err = got_error_from_errno2("unlink",
1077 if (symlink(target_path, ondisk_path) == -1) {
1078 err = got_error_from_errno3("symlink",
1079 target_path, ondisk_path);
1083 err = (*progress_cb)(progress_arg,
1084 GOT_STATUS_UPDATE, path);
1089 if (errno == ENOENT) {
1090 char *parent = dirname(ondisk_path);
1091 if (parent == NULL) {
1092 err = got_error_from_errno2("dirname",
1096 err = add_dir_on_disk(worktree, parent);
1100 * Retry, and fall through to error handling
1101 * below if this second attempt fails.
1103 if (symlink(target_path, ondisk_path) != -1) {
1104 err = NULL; /* success */
1109 /* Handle errors from first or second creation attempt. */
1110 if (errno == ENAMETOOLONG) {
1111 /* bad target path; install as a regular file */
1112 got_object_blob_rewind(blob);
1113 err = install_blob(worktree, ondisk_path, path,
1114 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1115 restoring_missing_file, reverting_versioned_file,
1116 repo, progress_cb, progress_arg);
1117 } else if (errno == ENOTDIR) {
1118 err = got_error_path(ondisk_path,
1119 GOT_ERR_FILE_OBSTRUCTED);
1121 err = got_error_from_errno3("symlink",
1122 target_path, ondisk_path);
1125 err = (*progress_cb)(progress_arg, GOT_STATUS_ADD, path);
1127 free(resolved_path);
1133 static const struct got_error *
1134 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1135 const char *path, mode_t te_mode, mode_t st_mode,
1136 struct got_blob_object *blob, int restoring_missing_file,
1137 int reverting_versioned_file, struct got_repository *repo,
1138 got_worktree_checkout_cb progress_cb, void *progress_arg)
1140 const struct got_error *err = NULL;
1144 char *tmppath = NULL;
1146 if (S_ISLNK(te_mode))
1147 return install_symlink(worktree, ondisk_path, path, te_mode,
1148 st_mode, blob, restoring_missing_file,
1149 reverting_versioned_file, repo, progress_cb, progress_arg);
1151 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1152 GOT_DEFAULT_FILE_MODE);
1154 if (errno == ENOENT) {
1155 char *parent = dirname(path);
1157 return got_error_from_errno2("dirname", path);
1158 err = add_dir_on_disk(worktree, parent);
1161 fd = open(ondisk_path,
1162 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1163 GOT_DEFAULT_FILE_MODE);
1165 return got_error_from_errno2("open",
1167 } else if (errno == EEXIST) {
1168 if (!S_ISREG(st_mode)) {
1169 /* TODO file is obstructed; do something */
1170 err = got_error_path(ondisk_path,
1171 GOT_ERR_FILE_OBSTRUCTED);
1174 err = got_opentemp_named_fd(&tmppath, &fd,
1181 return got_error_from_errno2("open", ondisk_path);
1184 if (restoring_missing_file)
1185 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
1186 else if (reverting_versioned_file)
1187 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
1189 err = (*progress_cb)(progress_arg,
1190 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1194 hdrlen = got_object_blob_get_hdrlen(blob);
1196 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1197 err = got_object_blob_read_block(&len, blob);
1201 /* Skip blob object header first time around. */
1202 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1204 err = got_error_from_errno("write");
1206 } else if (outlen != len - hdrlen) {
1207 err = got_error(GOT_ERR_IO);
1214 if (fsync(fd) != 0) {
1215 err = got_error_from_errno("fsync");
1220 if (rename(tmppath, ondisk_path) != 0) {
1221 err = got_error_from_errno3("rename", tmppath,
1228 if (chmod(ondisk_path,
1229 get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1230 err = got_error_from_errno2("chmod", ondisk_path);
1235 if (fd != -1 && close(fd) != 0 && err == NULL)
1236 err = got_error_from_errno("close");
1241 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1242 static const struct got_error *
1243 get_modified_file_content_status(unsigned char *status, FILE *f)
1245 const struct got_error *err = NULL;
1246 const char *markers[3] = {
1247 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1248 GOT_DIFF_CONFLICT_MARKER_SEP,
1249 GOT_DIFF_CONFLICT_MARKER_END
1254 const char delim[3] = {'\0', '\0', '\0'};
1256 while (*status == GOT_STATUS_MODIFY) {
1257 line = fparseln(f, &len, NULL, delim, 0);
1261 err = got_ferror(f, GOT_ERR_IO);
1265 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1266 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1268 *status = GOT_STATUS_CONFLICT;
1278 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1280 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1281 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1285 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1287 return !(ie->ctime_sec == sb->st_ctime &&
1288 ie->ctime_nsec == sb->st_ctimensec &&
1289 ie->mtime_sec == sb->st_mtime &&
1290 ie->mtime_nsec == sb->st_mtimensec &&
1291 ie->size == (sb->st_size & 0xffffffff) &&
1292 !xbit_differs(ie, sb->st_mode));
1295 static unsigned char
1296 get_staged_status(struct got_fileindex_entry *ie)
1298 switch (got_fileindex_entry_stage_get(ie)) {
1299 case GOT_FILEIDX_STAGE_ADD:
1300 return GOT_STATUS_ADD;
1301 case GOT_FILEIDX_STAGE_DELETE:
1302 return GOT_STATUS_DELETE;
1303 case GOT_FILEIDX_STAGE_MODIFY:
1304 return GOT_STATUS_MODIFY;
1306 return GOT_STATUS_NO_CHANGE;
1310 static const struct got_error *
1311 get_symlink_status(unsigned char *status, struct stat *sb,
1312 struct got_fileindex_entry *ie, const char *abspath,
1313 int dirfd, const char *de_name, struct got_blob_object *blob)
1315 const struct got_error *err = NULL;
1316 char target_path[PATH_MAX];
1317 char etarget[PATH_MAX];
1319 size_t len, target_len = 0;
1320 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1321 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1323 *status = GOT_STATUS_NO_CHANGE;
1325 /* Blob object content specifies the target path of the link. */
1327 err = got_object_blob_read_block(&len, blob);
1330 if (len + target_len >= sizeof(target_path)) {
1332 * Should not happen. The blob contents were OK
1333 * when this symlink was installed.
1335 return got_error(GOT_ERR_NO_SPACE);
1338 /* Skip blob object header first time around. */
1339 memcpy(target_path + target_len, buf + hdrlen,
1341 target_len += len - hdrlen;
1345 target_path[target_len] = '\0';
1348 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1350 return got_error_from_errno2("readlinkat", abspath);
1352 elen = readlink(abspath, etarget, sizeof(etarget));
1354 return got_error_from_errno2("readlinkat", abspath);
1357 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1358 *status = GOT_STATUS_MODIFY;
1363 static const struct got_error *
1364 get_file_status(unsigned char *status, struct stat *sb,
1365 struct got_fileindex_entry *ie, const char *abspath,
1366 int dirfd, const char *de_name, struct got_repository *repo)
1368 const struct got_error *err = NULL;
1369 struct got_object_id id;
1374 struct got_blob_object *blob = NULL;
1376 unsigned char staged_status = get_staged_status(ie);
1378 *status = GOT_STATUS_NO_CHANGE;
1381 * Whenever the caller provides a directory descriptor and a
1382 * directory entry name for the file, use them! This prevents
1383 * race conditions if filesystem paths change beneath our feet.
1386 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1387 if (errno == ENOENT) {
1388 if (got_fileindex_entry_has_file_on_disk(ie))
1389 *status = GOT_STATUS_MISSING;
1391 *status = GOT_STATUS_DELETE;
1394 err = got_error_from_errno2("fstatat", abspath);
1398 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1399 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1400 return got_error_from_errno2("open", abspath);
1401 else if (fd == -1 && errno == ELOOP) {
1402 if (lstat(abspath, sb) == -1)
1403 return got_error_from_errno2("lstat", abspath);
1404 } else if (fd == -1 || fstat(fd, sb) == -1) {
1405 if (errno == ENOENT) {
1406 if (got_fileindex_entry_has_file_on_disk(ie))
1407 *status = GOT_STATUS_MISSING;
1409 *status = GOT_STATUS_DELETE;
1412 err = got_error_from_errno2("fstat", abspath);
1417 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1418 *status = GOT_STATUS_OBSTRUCTED;
1422 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1423 *status = GOT_STATUS_DELETE;
1425 } else if (!got_fileindex_entry_has_blob(ie) &&
1426 staged_status != GOT_STATUS_ADD) {
1427 *status = GOT_STATUS_ADD;
1431 if (!stat_info_differs(ie, sb))
1434 if (staged_status == GOT_STATUS_MODIFY ||
1435 staged_status == GOT_STATUS_ADD)
1436 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1438 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1440 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1444 if (S_ISLNK(sb->st_mode)) {
1445 /* Staging changes to symlinks is not yet(?) supported. */
1446 if (staged_status != GOT_STATUS_NO_CHANGE) {
1447 err = got_error_path(abspath, GOT_ERR_FILE_STATUS);
1450 err = get_symlink_status(status, sb, ie, abspath, dirfd,
1457 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1459 err = got_error_from_errno2("openat", abspath);
1464 f = fdopen(fd, "r");
1466 err = got_error_from_errno2("fdopen", abspath);
1470 hdrlen = got_object_blob_get_hdrlen(blob);
1472 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1473 err = got_object_blob_read_block(&blen, blob);
1476 /* Skip length of blob object header first time around. */
1477 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1478 if (flen == 0 && ferror(f)) {
1479 err = got_error_from_errno("fread");
1484 *status = GOT_STATUS_MODIFY;
1486 } else if (flen == 0) {
1488 *status = GOT_STATUS_MODIFY;
1490 } else if (blen - hdrlen == flen) {
1491 /* Skip blob object header first time around. */
1492 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1493 *status = GOT_STATUS_MODIFY;
1497 *status = GOT_STATUS_MODIFY;
1503 if (*status == GOT_STATUS_MODIFY) {
1505 err = get_modified_file_content_status(status, f);
1506 } else if (xbit_differs(ie, sb->st_mode))
1507 *status = GOT_STATUS_MODE_CHANGE;
1510 got_object_blob_close(blob);
1511 if (f != NULL && fclose(f) == EOF && err == NULL)
1512 err = got_error_from_errno2("fclose", abspath);
1513 if (fd != -1 && close(fd) == -1 && err == NULL)
1514 err = got_error_from_errno2("close", abspath);
1519 * Update timestamps in the file index if a file is unmodified and
1520 * we had to run a full content comparison to find out.
1522 static const struct got_error *
1523 sync_timestamps(char *ondisk_path, unsigned char status,
1524 struct got_fileindex_entry *ie, struct stat *sb)
1526 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1527 return got_fileindex_entry_update(ie, ondisk_path,
1528 ie->blob_sha1, ie->commit_sha1, 1);
1533 static const struct got_error *
1534 update_blob(struct got_worktree *worktree,
1535 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1536 struct got_tree_entry *te, const char *path,
1537 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1540 const struct got_error *err = NULL;
1541 struct got_blob_object *blob = NULL;
1543 unsigned char status = GOT_STATUS_NO_CHANGE;
1546 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1547 return got_error_from_errno("asprintf");
1550 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1551 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1554 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1558 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1559 sb.st_mode = got_fileindex_perms_to_st(ie);
1561 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1563 if (status == GOT_STATUS_OBSTRUCTED) {
1564 err = (*progress_cb)(progress_arg, status, path);
1567 if (status == GOT_STATUS_CONFLICT) {
1568 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1573 if (ie && status != GOT_STATUS_MISSING &&
1574 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1575 if (got_fileindex_entry_has_commit(ie) &&
1576 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1577 SHA1_DIGEST_LENGTH) == 0) {
1578 err = sync_timestamps(ondisk_path, status, ie, &sb);
1581 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1585 if (got_fileindex_entry_has_blob(ie) &&
1586 memcmp(ie->blob_sha1, te->id.sha1,
1587 SHA1_DIGEST_LENGTH) == 0) {
1588 err = sync_timestamps(ondisk_path, status, ie, &sb);
1593 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1597 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1598 int update_timestamps;
1599 struct got_blob_object *blob2 = NULL;
1600 char *label_orig = NULL;
1601 if (got_fileindex_entry_has_blob(ie)) {
1602 struct got_object_id id2;
1603 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1604 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1608 if (got_fileindex_entry_has_commit(ie)) {
1609 char id_str[SHA1_DIGEST_STRING_LENGTH];
1610 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1611 sizeof(id_str)) == NULL) {
1612 err = got_error_path(id_str,
1613 GOT_ERR_BAD_OBJ_ID_STR);
1616 if (asprintf(&label_orig, "%s: commit %s",
1617 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1618 err = got_error_from_errno("asprintf");
1622 err = merge_blob(&update_timestamps, worktree, blob2,
1623 ondisk_path, path, sb.st_mode, label_orig, blob,
1624 worktree->base_commit_id, repo,
1625 progress_cb, progress_arg);
1628 got_object_blob_close(blob2);
1632 * Do not update timestamps of files with local changes.
1633 * Otherwise, a future status walk would treat them as
1634 * unmodified files again.
1636 err = got_fileindex_entry_update(ie, ondisk_path,
1637 blob->id.sha1, worktree->base_commit_id->sha1,
1639 } else if (status == GOT_STATUS_MODE_CHANGE) {
1640 err = got_fileindex_entry_update(ie, ondisk_path,
1641 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1642 } else if (status == GOT_STATUS_DELETE) {
1643 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1646 err = got_fileindex_entry_update(ie, ondisk_path,
1647 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1651 err = install_blob(worktree, ondisk_path, path, te->mode,
1652 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1653 repo, progress_cb, progress_arg);
1657 err = got_fileindex_entry_update(ie, ondisk_path,
1658 blob->id.sha1, worktree->base_commit_id->sha1, 1);
1660 err = create_fileindex_entry(fileindex,
1661 worktree->base_commit_id, ondisk_path, path,
1667 got_object_blob_close(blob);
1673 static const struct got_error *
1674 remove_ondisk_file(const char *root_path, const char *path)
1676 const struct got_error *err = NULL;
1677 char *ondisk_path = NULL;
1679 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1680 return got_error_from_errno("asprintf");
1682 if (unlink(ondisk_path) == -1) {
1683 if (errno != ENOENT)
1684 err = got_error_from_errno2("unlink", ondisk_path);
1686 char *parent = dirname(ondisk_path);
1687 while (parent && strcmp(parent, root_path) != 0) {
1688 if (rmdir(parent) == -1) {
1689 if (errno != ENOTEMPTY)
1690 err = got_error_from_errno2("rmdir",
1694 parent = dirname(parent);
1701 static const struct got_error *
1702 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1703 struct got_fileindex_entry *ie, struct got_repository *repo,
1704 got_worktree_checkout_cb progress_cb, void *progress_arg)
1706 const struct got_error *err = NULL;
1707 unsigned char status;
1711 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1712 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1714 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1716 return got_error_from_errno("asprintf");
1718 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
1722 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1723 status == GOT_STATUS_ADD) {
1724 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1728 * Preserve the working file and change the deleted blob's
1729 * entry into a schedule-add entry.
1731 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1734 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1737 if (status == GOT_STATUS_NO_CHANGE) {
1738 err = remove_ondisk_file(worktree->root_path, ie->path);
1742 got_fileindex_entry_remove(fileindex, ie);
1749 struct diff_cb_arg {
1750 struct got_fileindex *fileindex;
1751 struct got_worktree *worktree;
1752 struct got_repository *repo;
1753 got_worktree_checkout_cb progress_cb;
1755 got_cancel_cb cancel_cb;
1759 static const struct got_error *
1760 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1761 struct got_tree_entry *te, const char *parent_path)
1763 struct diff_cb_arg *a = arg;
1765 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1766 return got_error(GOT_ERR_CANCELLED);
1768 return update_blob(a->worktree, a->fileindex, ie, te,
1769 ie->path, a->repo, a->progress_cb, a->progress_arg);
1772 static const struct got_error *
1773 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1775 struct diff_cb_arg *a = arg;
1777 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1778 return got_error(GOT_ERR_CANCELLED);
1780 return delete_blob(a->worktree, a->fileindex, ie,
1781 a->repo, a->progress_cb, a->progress_arg);
1784 static const struct got_error *
1785 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1787 struct diff_cb_arg *a = arg;
1788 const struct got_error *err;
1791 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1792 return got_error(GOT_ERR_CANCELLED);
1794 if (got_object_tree_entry_is_submodule(te))
1797 if (asprintf(&path, "%s%s%s", parent_path,
1798 parent_path[0] ? "/" : "", te->name)
1800 return got_error_from_errno("asprintf");
1802 if (S_ISDIR(te->mode))
1803 err = add_dir_on_disk(a->worktree, path);
1805 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1806 a->repo, a->progress_cb, a->progress_arg);
1812 static const struct got_error *
1813 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1815 const struct got_error *err = NULL;
1816 char *uuidstr = NULL;
1817 uint32_t uuid_status;
1821 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1822 if (uuid_status != uuid_s_ok)
1823 return got_error_uuid(uuid_status, "uuid_to_string");
1825 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1827 err = got_error_from_errno("asprintf");
1834 const struct got_error *
1835 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1837 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1840 static const struct got_error *
1841 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1843 return get_ref_name(refname, worktree,
1844 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1847 static const struct got_error *
1848 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1850 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1853 static const struct got_error *
1854 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1856 return get_ref_name(refname, worktree,
1857 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1860 static const struct got_error *
1861 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1863 return get_ref_name(refname, worktree,
1864 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1867 static const struct got_error *
1868 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1870 return get_ref_name(refname, worktree,
1871 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1874 static const struct got_error *
1875 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1877 return get_ref_name(refname, worktree,
1878 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1881 static const struct got_error *
1882 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1884 return get_ref_name(refname, worktree,
1885 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1888 static const struct got_error *
1889 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1891 return get_ref_name(refname, worktree,
1892 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1895 const struct got_error *
1896 got_worktree_get_histedit_script_path(char **path,
1897 struct got_worktree *worktree)
1899 if (asprintf(path, "%s/%s/%s", worktree->root_path,
1900 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
1902 return got_error_from_errno("asprintf");
1908 * Prevent Git's garbage collector from deleting our base commit by
1909 * setting a reference to our base commit's ID.
1911 static const struct got_error *
1912 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1914 const struct got_error *err = NULL;
1915 struct got_reference *ref = NULL;
1918 err = got_worktree_get_base_ref_name(&refname, worktree);
1922 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1926 err = got_ref_write(ref, repo);
1934 static const struct got_error *
1935 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
1937 const struct got_error *err = NULL;
1939 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1940 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1941 err = got_error_from_errno("asprintf");
1942 *fileindex_path = NULL;
1948 static const struct got_error *
1949 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1950 struct got_worktree *worktree)
1952 const struct got_error *err = NULL;
1955 *fileindex_path = NULL;
1956 *fileindex = got_fileindex_alloc();
1957 if (*fileindex == NULL)
1958 return got_error_from_errno("got_fileindex_alloc");
1960 err = get_fileindex_path(fileindex_path, worktree);
1964 index = fopen(*fileindex_path, "rb");
1965 if (index == NULL) {
1966 if (errno != ENOENT)
1967 err = got_error_from_errno2("fopen", *fileindex_path);
1969 err = got_fileindex_read(*fileindex, index);
1970 if (fclose(index) != 0 && err == NULL)
1971 err = got_error_from_errno("fclose");
1975 free(*fileindex_path);
1976 *fileindex_path = NULL;
1977 got_fileindex_free(*fileindex);
1983 struct bump_base_commit_id_arg {
1984 struct got_object_id *base_commit_id;
1987 const char *entry_name;
1988 got_worktree_checkout_cb progress_cb;
1992 /* Bump base commit ID of all files within an updated part of the work tree. */
1993 static const struct got_error *
1994 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1996 const struct got_error *err;
1997 struct bump_base_commit_id_arg *a = arg;
1999 if (a->entry_name) {
2000 if (strcmp(ie->path, a->path) != 0)
2002 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2005 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2006 SHA1_DIGEST_LENGTH) == 0)
2009 if (a->progress_cb) {
2010 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2015 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2019 static const struct got_error *
2020 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2022 const struct got_error *err = NULL;
2023 char *new_fileindex_path = NULL;
2024 FILE *new_index = NULL;
2025 struct timespec timeout;
2027 err = got_opentemp_named(&new_fileindex_path, &new_index,
2032 err = got_fileindex_write(fileindex, new_index);
2036 if (rename(new_fileindex_path, fileindex_path) != 0) {
2037 err = got_error_from_errno3("rename", new_fileindex_path,
2039 unlink(new_fileindex_path);
2043 * Sleep for a short amount of time to ensure that files modified after
2044 * this program exits have a different time stamp from the one which
2045 * was recorded in the file index.
2048 timeout.tv_nsec = 1;
2049 nanosleep(&timeout, NULL);
2053 free(new_fileindex_path);
2057 static const struct got_error *
2058 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2059 struct got_object_id **tree_id, const char *wt_relpath,
2060 struct got_worktree *worktree, struct got_repository *repo)
2062 const struct got_error *err = NULL;
2063 struct got_object_id *id = NULL;
2064 char *in_repo_path = NULL;
2065 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2067 *entry_type = GOT_OBJ_TYPE_ANY;
2068 *tree_relpath = NULL;
2071 if (wt_relpath[0] == '\0') {
2072 /* Check out all files within the work tree. */
2073 *entry_type = GOT_OBJ_TYPE_TREE;
2074 *tree_relpath = strdup("");
2075 if (*tree_relpath == NULL) {
2076 err = got_error_from_errno("strdup");
2079 err = got_object_id_by_path(tree_id, repo,
2080 worktree->base_commit_id, worktree->path_prefix);
2086 /* Check out a subset of files in the work tree. */
2088 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2089 is_root_wt ? "" : "/", wt_relpath) == -1) {
2090 err = got_error_from_errno("asprintf");
2094 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2100 in_repo_path = NULL;
2102 err = got_object_get_type(entry_type, repo, id);
2106 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2107 /* Check out a single file. */
2108 if (strchr(wt_relpath, '/') == NULL) {
2109 /* Check out a single file in work tree's root dir. */
2110 in_repo_path = strdup(worktree->path_prefix);
2111 if (in_repo_path == NULL) {
2112 err = got_error_from_errno("strdup");
2115 *tree_relpath = strdup("");
2116 if (*tree_relpath == NULL) {
2117 err = got_error_from_errno("strdup");
2121 /* Check out a single file in a subdirectory. */
2122 err = got_path_dirname(tree_relpath, wt_relpath);
2125 if (asprintf(&in_repo_path, "%s%s%s",
2126 worktree->path_prefix, is_root_wt ? "" : "/",
2127 *tree_relpath) == -1) {
2128 err = got_error_from_errno("asprintf");
2132 err = got_object_id_by_path(tree_id, repo,
2133 worktree->base_commit_id, in_repo_path);
2135 /* Check out all files within a subdirectory. */
2136 *tree_id = got_object_id_dup(id);
2137 if (*tree_id == NULL) {
2138 err = got_error_from_errno("got_object_id_dup");
2141 *tree_relpath = strdup(wt_relpath);
2142 if (*tree_relpath == NULL) {
2143 err = got_error_from_errno("strdup");
2151 *entry_type = GOT_OBJ_TYPE_ANY;
2152 free(*tree_relpath);
2153 *tree_relpath = NULL;
2160 static const struct got_error *
2161 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2162 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2163 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2164 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2166 const struct got_error *err = NULL;
2167 struct got_commit_object *commit = NULL;
2168 struct got_tree_object *tree = NULL;
2169 struct got_fileindex_diff_tree_cb diff_cb;
2170 struct diff_cb_arg arg;
2172 err = ref_base_commit(worktree, repo);
2174 if (!(err->code == GOT_ERR_ERRNO &&
2175 (errno == EACCES || errno == EROFS)))
2177 err = (*progress_cb)(progress_arg,
2178 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2183 err = got_object_open_as_commit(&commit, repo,
2184 worktree->base_commit_id);
2188 err = got_object_open_as_tree(&tree, repo, tree_id);
2193 got_object_tree_find_entry(tree, entry_name) == NULL) {
2194 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2198 diff_cb.diff_old_new = diff_old_new;
2199 diff_cb.diff_old = diff_old;
2200 diff_cb.diff_new = diff_new;
2201 arg.fileindex = fileindex;
2202 arg.worktree = worktree;
2204 arg.progress_cb = progress_cb;
2205 arg.progress_arg = progress_arg;
2206 arg.cancel_cb = cancel_cb;
2207 arg.cancel_arg = cancel_arg;
2208 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2209 entry_name, repo, &diff_cb, &arg);
2212 got_object_tree_close(tree);
2214 got_object_commit_close(commit);
2218 const struct got_error *
2219 got_worktree_checkout_files(struct got_worktree *worktree,
2220 struct got_pathlist_head *paths, struct got_repository *repo,
2221 got_worktree_checkout_cb progress_cb, void *progress_arg,
2222 got_cancel_cb cancel_cb, void *cancel_arg)
2224 const struct got_error *err = NULL, *sync_err, *unlockerr;
2225 struct got_commit_object *commit = NULL;
2226 struct got_tree_object *tree = NULL;
2227 struct got_fileindex *fileindex = NULL;
2228 char *fileindex_path = NULL;
2229 struct got_pathlist_entry *pe;
2230 struct tree_path_data {
2231 SIMPLEQ_ENTRY(tree_path_data) entry;
2232 struct got_object_id *tree_id;
2237 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2239 SIMPLEQ_INIT(&tree_paths);
2241 err = lock_worktree(worktree, LOCK_EX);
2245 /* Map all specified paths to in-repository trees. */
2246 TAILQ_FOREACH(pe, paths, entry) {
2247 tpd = malloc(sizeof(*tpd));
2249 err = got_error_from_errno("malloc");
2253 err = find_tree_entry_for_checkout(&tpd->entry_type,
2254 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2260 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2261 err = got_path_basename(&tpd->entry_name, pe->path);
2269 tpd->entry_name = NULL;
2271 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2275 * Read the file index.
2276 * Checking out files is supposed to be an idempotent operation.
2277 * If the on-disk file index is incomplete we will try to complete it.
2279 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2283 tpd = SIMPLEQ_FIRST(&tree_paths);
2284 TAILQ_FOREACH(pe, paths, entry) {
2285 struct bump_base_commit_id_arg bbc_arg;
2287 err = checkout_files(worktree, fileindex, tpd->relpath,
2288 tpd->tree_id, tpd->entry_name, repo,
2289 progress_cb, progress_arg, cancel_cb, cancel_arg);
2293 bbc_arg.base_commit_id = worktree->base_commit_id;
2294 bbc_arg.entry_name = tpd->entry_name;
2295 bbc_arg.path = pe->path;
2296 bbc_arg.path_len = pe->path_len;
2297 bbc_arg.progress_cb = progress_cb;
2298 bbc_arg.progress_arg = progress_arg;
2299 err = got_fileindex_for_each_entry_safe(fileindex,
2300 bump_base_commit_id, &bbc_arg);
2304 tpd = SIMPLEQ_NEXT(tpd, entry);
2306 sync_err = sync_fileindex(fileindex, fileindex_path);
2307 if (sync_err && err == NULL)
2310 free(fileindex_path);
2312 got_object_tree_close(tree);
2314 got_object_commit_close(commit);
2316 got_fileindex_free(fileindex);
2317 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2318 tpd = SIMPLEQ_FIRST(&tree_paths);
2319 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2324 unlockerr = lock_worktree(worktree, LOCK_SH);
2325 if (unlockerr && err == NULL)
2330 struct merge_file_cb_arg {
2331 struct got_worktree *worktree;
2332 struct got_fileindex *fileindex;
2333 got_worktree_checkout_cb progress_cb;
2335 got_cancel_cb cancel_cb;
2337 const char *label_orig;
2338 struct got_object_id *commit_id2;
2341 static const struct got_error *
2342 merge_file_cb(void *arg, struct got_blob_object *blob1,
2343 struct got_blob_object *blob2, struct got_object_id *id1,
2344 struct got_object_id *id2, const char *path1, const char *path2,
2345 mode_t mode1, mode_t mode2, struct got_repository *repo)
2347 static const struct got_error *err = NULL;
2348 struct merge_file_cb_arg *a = arg;
2349 struct got_fileindex_entry *ie;
2350 char *ondisk_path = NULL;
2352 unsigned char status;
2353 int local_changes_subsumed;
2355 if (blob1 && blob2) {
2356 ie = got_fileindex_entry_get(a->fileindex, path2,
2359 return (*a->progress_cb)(a->progress_arg,
2360 GOT_STATUS_MISSING, path2);
2362 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2364 return got_error_from_errno("asprintf");
2366 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2371 if (status == GOT_STATUS_DELETE) {
2372 err = (*a->progress_cb)(a->progress_arg,
2373 GOT_STATUS_MERGE, path2);
2376 if (status != GOT_STATUS_NO_CHANGE &&
2377 status != GOT_STATUS_MODIFY &&
2378 status != GOT_STATUS_CONFLICT &&
2379 status != GOT_STATUS_ADD) {
2380 err = (*a->progress_cb)(a->progress_arg, status, path2);
2384 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
2385 ondisk_path, path2, sb.st_mode, a->label_orig, blob2,
2386 a->commit_id2, repo, a->progress_cb, a->progress_arg);
2388 ie = got_fileindex_entry_get(a->fileindex, path1,
2391 return (*a->progress_cb)(a->progress_arg,
2392 GOT_STATUS_MISSING, path1);
2394 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2396 return got_error_from_errno("asprintf");
2398 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2404 case GOT_STATUS_NO_CHANGE:
2405 err = (*a->progress_cb)(a->progress_arg,
2406 GOT_STATUS_DELETE, path1);
2409 err = remove_ondisk_file(a->worktree->root_path, path1);
2413 got_fileindex_entry_mark_deleted_from_disk(ie);
2415 case GOT_STATUS_DELETE:
2416 case GOT_STATUS_MISSING:
2417 err = (*a->progress_cb)(a->progress_arg,
2418 GOT_STATUS_DELETE, path1);
2422 got_fileindex_entry_mark_deleted_from_disk(ie);
2424 case GOT_STATUS_ADD:
2425 case GOT_STATUS_MODIFY:
2426 case GOT_STATUS_CONFLICT:
2427 err = (*a->progress_cb)(a->progress_arg,
2428 GOT_STATUS_CANNOT_DELETE, path1);
2432 case GOT_STATUS_OBSTRUCTED:
2433 err = (*a->progress_cb)(a->progress_arg, status, path1);
2441 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2443 return got_error_from_errno("asprintf");
2444 ie = got_fileindex_entry_get(a->fileindex, path2,
2447 err = get_file_status(&status, &sb, ie, ondisk_path,
2451 if (status != GOT_STATUS_NO_CHANGE &&
2452 status != GOT_STATUS_MODIFY &&
2453 status != GOT_STATUS_CONFLICT &&
2454 status != GOT_STATUS_ADD) {
2455 err = (*a->progress_cb)(a->progress_arg,
2459 err = merge_blob(&local_changes_subsumed, a->worktree,
2460 NULL, ondisk_path, path2, sb.st_mode,
2461 a->label_orig, blob2, a->commit_id2, repo,
2464 if (status == GOT_STATUS_DELETE) {
2465 err = got_fileindex_entry_update(ie,
2466 ondisk_path, blob2->id.sha1,
2467 a->worktree->base_commit_id->sha1, 0);
2472 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2473 err = install_blob(a->worktree, ondisk_path, path2,
2474 /* XXX get this from parent tree! */
2475 GOT_DEFAULT_FILE_MODE,
2476 sb.st_mode, blob2, 0, 0, repo,
2477 a->progress_cb, a->progress_arg);
2480 err = got_fileindex_entry_alloc(&ie, path2);
2483 err = got_fileindex_entry_update(ie, ondisk_path,
2486 got_fileindex_entry_free(ie);
2489 err = got_fileindex_entry_add(a->fileindex, ie);
2491 got_fileindex_entry_free(ie);
2501 struct check_merge_ok_arg {
2502 struct got_worktree *worktree;
2503 struct got_repository *repo;
2506 static const struct got_error *
2507 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2509 const struct got_error *err = NULL;
2510 struct check_merge_ok_arg *a = arg;
2511 unsigned char status;
2515 /* Reject merges into a work tree with mixed base commits. */
2516 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2517 SHA1_DIGEST_LENGTH))
2518 return got_error(GOT_ERR_MIXED_COMMITS);
2520 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2522 return got_error_from_errno("asprintf");
2524 /* Reject merges into a work tree with conflicted files. */
2525 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2528 if (status == GOT_STATUS_CONFLICT)
2529 return got_error(GOT_ERR_CONFLICTS);
2534 static const struct got_error *
2535 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2536 const char *fileindex_path, struct got_object_id *commit_id1,
2537 struct got_object_id *commit_id2, struct got_repository *repo,
2538 got_worktree_checkout_cb progress_cb, void *progress_arg,
2539 got_cancel_cb cancel_cb, void *cancel_arg)
2541 const struct got_error *err = NULL, *sync_err;
2542 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2543 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2544 struct merge_file_cb_arg arg;
2545 char *label_orig = NULL;
2550 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2551 worktree->path_prefix);
2555 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2559 err = got_object_id_str(&id_str, commit_id1);
2563 if (asprintf(&label_orig, "%s: commit %s",
2564 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2565 err = got_error_from_errno("asprintf");
2572 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2573 worktree->path_prefix);
2577 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2581 arg.worktree = worktree;
2582 arg.fileindex = fileindex;
2583 arg.progress_cb = progress_cb;
2584 arg.progress_arg = progress_arg;
2585 arg.cancel_cb = cancel_cb;
2586 arg.cancel_arg = cancel_arg;
2587 arg.label_orig = label_orig;
2588 arg.commit_id2 = commit_id2;
2589 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2590 sync_err = sync_fileindex(fileindex, fileindex_path);
2591 if (sync_err && err == NULL)
2595 got_object_tree_close(tree1);
2597 got_object_tree_close(tree2);
2602 const struct got_error *
2603 got_worktree_merge_files(struct got_worktree *worktree,
2604 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2605 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2606 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2608 const struct got_error *err, *unlockerr;
2609 char *fileindex_path = NULL;
2610 struct got_fileindex *fileindex = NULL;
2611 struct check_merge_ok_arg mok_arg;
2613 err = lock_worktree(worktree, LOCK_EX);
2617 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2621 mok_arg.worktree = worktree;
2622 mok_arg.repo = repo;
2623 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2628 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2629 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2632 got_fileindex_free(fileindex);
2633 free(fileindex_path);
2634 unlockerr = lock_worktree(worktree, LOCK_SH);
2635 if (unlockerr && err == NULL)
2640 struct diff_dir_cb_arg {
2641 struct got_fileindex *fileindex;
2642 struct got_worktree *worktree;
2643 const char *status_path;
2644 size_t status_path_len;
2645 struct got_repository *repo;
2646 got_worktree_status_cb status_cb;
2648 got_cancel_cb cancel_cb;
2650 /* A pathlist containing per-directory pathlists of ignore patterns. */
2651 struct got_pathlist_head ignores;
2652 int report_unchanged;
2656 static const struct got_error *
2657 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2658 int dirfd, const char *de_name,
2659 got_worktree_status_cb status_cb, void *status_arg,
2660 struct got_repository *repo, int report_unchanged)
2662 const struct got_error *err = NULL;
2663 unsigned char status = GOT_STATUS_NO_CHANGE;
2664 unsigned char staged_status = get_staged_status(ie);
2666 struct got_object_id blob_id, commit_id, staged_blob_id;
2667 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2668 struct got_object_id *staged_blob_idp = NULL;
2670 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
2674 if (status == GOT_STATUS_NO_CHANGE &&
2675 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
2678 if (got_fileindex_entry_has_blob(ie)) {
2679 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2680 blob_idp = &blob_id;
2682 if (got_fileindex_entry_has_commit(ie)) {
2683 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2684 commit_idp = &commit_id;
2686 if (staged_status == GOT_STATUS_ADD ||
2687 staged_status == GOT_STATUS_MODIFY) {
2688 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2689 SHA1_DIGEST_LENGTH);
2690 staged_blob_idp = &staged_blob_id;
2693 return (*status_cb)(status_arg, status, staged_status,
2694 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
2697 static const struct got_error *
2698 status_old_new(void *arg, struct got_fileindex_entry *ie,
2699 struct dirent *de, const char *parent_path, int dirfd)
2701 const struct got_error *err = NULL;
2702 struct diff_dir_cb_arg *a = arg;
2705 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2706 return got_error(GOT_ERR_CANCELLED);
2708 if (got_path_cmp(parent_path, a->status_path,
2709 strlen(parent_path), a->status_path_len) != 0 &&
2710 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2713 if (parent_path[0]) {
2714 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2715 parent_path, de->d_name) == -1)
2716 return got_error_from_errno("asprintf");
2718 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2720 return got_error_from_errno("asprintf");
2723 err = report_file_status(ie, abspath, dirfd, de->d_name,
2724 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
2729 static const struct got_error *
2730 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2732 struct diff_dir_cb_arg *a = arg;
2733 struct got_object_id blob_id, commit_id;
2734 unsigned char status;
2736 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2737 return got_error(GOT_ERR_CANCELLED);
2739 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2742 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2743 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2744 if (got_fileindex_entry_has_file_on_disk(ie))
2745 status = GOT_STATUS_MISSING;
2747 status = GOT_STATUS_DELETE;
2748 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2749 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
2753 free_ignorelist(struct got_pathlist_head *ignorelist)
2755 struct got_pathlist_entry *pe;
2757 TAILQ_FOREACH(pe, ignorelist, entry)
2758 free((char *)pe->path);
2759 got_pathlist_free(ignorelist);
2763 free_ignores(struct got_pathlist_head *ignores)
2765 struct got_pathlist_entry *pe;
2767 TAILQ_FOREACH(pe, ignores, entry) {
2768 struct got_pathlist_head *ignorelist = pe->data;
2769 free_ignorelist(ignorelist);
2770 free((char *)pe->path);
2772 got_pathlist_free(ignores);
2775 static const struct got_error *
2776 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
2778 const struct got_error *err = NULL;
2779 struct got_pathlist_entry *pe = NULL;
2780 struct got_pathlist_head *ignorelist;
2781 char *line = NULL, *pattern, *dirpath = NULL;
2782 size_t linesize = 0;
2785 ignorelist = calloc(1, sizeof(*ignorelist));
2786 if (ignorelist == NULL)
2787 return got_error_from_errno("calloc");
2788 TAILQ_INIT(ignorelist);
2790 while ((linelen = getline(&line, &linesize, f)) != -1) {
2791 if (linelen > 0 && line[linelen - 1] == '\n')
2792 line[linelen - 1] = '\0';
2794 /* Git's ignores may contain comments. */
2798 /* Git's negated patterns are not (yet?) supported. */
2802 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
2804 err = got_error_from_errno("asprintf");
2807 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
2812 err = got_error_from_errno("getline");
2816 dirpath = strdup(path);
2817 if (dirpath == NULL) {
2818 err = got_error_from_errno("strdup");
2821 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
2824 if (err || pe == NULL) {
2826 free_ignorelist(ignorelist);
2832 match_ignores(struct got_pathlist_head *ignores, const char *path)
2834 struct got_pathlist_entry *pe;
2836 /* Handle patterns which match in all directories. */
2837 TAILQ_FOREACH(pe, ignores, entry) {
2838 struct got_pathlist_head *ignorelist = pe->data;
2839 struct got_pathlist_entry *pi;
2841 TAILQ_FOREACH(pi, ignorelist, entry) {
2842 const char *p, *pattern = pi->path;
2844 if (strncmp(pattern, "**/", 3) != 0)
2849 if (fnmatch(pattern, p,
2850 FNM_PATHNAME | FNM_LEADING_DIR)) {
2851 /* Retry in next directory. */
2852 while (*p && *p != '/')
2864 * The ignores pathlist contains ignore lists from children before
2865 * parents, so we can find the most specific ignorelist by walking
2866 * ignores backwards.
2868 pe = TAILQ_LAST(ignores, got_pathlist_head);
2870 if (got_path_is_child(path, pe->path, pe->path_len)) {
2871 struct got_pathlist_head *ignorelist = pe->data;
2872 struct got_pathlist_entry *pi;
2873 TAILQ_FOREACH(pi, ignorelist, entry) {
2874 const char *pattern = pi->path;
2875 int flags = FNM_LEADING_DIR;
2876 if (strstr(pattern, "/**/") == NULL)
2877 flags |= FNM_PATHNAME;
2878 if (fnmatch(pattern, path, flags))
2883 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
2889 static const struct got_error *
2890 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
2891 const char *path, int dirfd, const char *ignores_filename)
2893 const struct got_error *err = NULL;
2896 FILE *ignoresfile = NULL;
2898 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
2899 path[0] ? "/" : "", ignores_filename) == -1)
2900 return got_error_from_errno("asprintf");
2903 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
2905 if (errno != ENOENT && errno != EACCES)
2906 err = got_error_from_errno2("openat",
2909 ignoresfile = fdopen(fd, "r");
2910 if (ignoresfile == NULL)
2911 err = got_error_from_errno2("fdopen",
2915 err = read_ignores(ignores, path, ignoresfile);
2919 ignoresfile = fopen(ignorespath, "r");
2920 if (ignoresfile == NULL) {
2921 if (errno != ENOENT && errno != EACCES)
2922 err = got_error_from_errno2("fopen",
2925 err = read_ignores(ignores, path, ignoresfile);
2928 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
2929 err = got_error_from_errno2("fclose", path);
2930 if (fd != -1 && close(fd) == -1 && err == NULL)
2931 err = got_error_from_errno2("close", path);
2936 static const struct got_error *
2937 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
2939 const struct got_error *err = NULL;
2940 struct diff_dir_cb_arg *a = arg;
2943 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2944 return got_error(GOT_ERR_CANCELLED);
2946 if (parent_path[0]) {
2947 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2948 return got_error_from_errno("asprintf");
2953 if (de->d_type != DT_DIR &&
2954 got_path_is_child(path, a->status_path, a->status_path_len)
2955 && !match_ignores(&a->ignores, path))
2956 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2957 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2963 static const struct got_error *
2964 status_traverse(void *arg, const char *path, int dirfd)
2966 const struct got_error *err = NULL;
2967 struct diff_dir_cb_arg *a = arg;
2972 err = add_ignores(&a->ignores, a->worktree->root_path,
2973 path, dirfd, ".cvsignore");
2977 err = add_ignores(&a->ignores, a->worktree->root_path, path,
2978 dirfd, ".gitignore");
2983 static const struct got_error *
2984 report_single_file_status(const char *path, const char *ondisk_path,
2985 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2986 void *status_arg, struct got_repository *repo, int report_unchanged)
2988 struct got_fileindex_entry *ie;
2991 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
2993 return report_file_status(ie, ondisk_path, -1, NULL,
2994 status_cb, status_arg, repo, report_unchanged);
2996 if (lstat(ondisk_path, &sb) == -1) {
2997 if (errno != ENOENT)
2998 return got_error_from_errno2("lstat", ondisk_path);
2999 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3000 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3004 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3005 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3006 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3011 static const struct got_error *
3012 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3013 const char *root_path, const char *path)
3015 const struct got_error *err;
3016 char *parent_path, *next_parent_path = NULL;
3018 err = add_ignores(ignores, root_path, "", -1,
3023 err = add_ignores(ignores, root_path, "", -1,
3028 err = got_path_dirname(&parent_path, path);
3030 if (err->code == GOT_ERR_BAD_PATH)
3031 return NULL; /* cannot traverse parent */
3035 err = add_ignores(ignores, root_path, parent_path, -1,
3039 err = add_ignores(ignores, root_path, parent_path, -1,
3043 err = got_path_dirname(&next_parent_path, parent_path);
3045 if (err->code == GOT_ERR_BAD_PATH)
3046 err = NULL; /* traversed everything */
3050 parent_path = next_parent_path;
3051 next_parent_path = NULL;
3055 free(next_parent_path);
3059 static const struct got_error *
3060 worktree_status(struct got_worktree *worktree, const char *path,
3061 struct got_fileindex *fileindex, struct got_repository *repo,
3062 got_worktree_status_cb status_cb, void *status_arg,
3063 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3064 int report_unchanged)
3066 const struct got_error *err = NULL;
3068 struct got_fileindex_diff_dir_cb fdiff_cb;
3069 struct diff_dir_cb_arg arg;
3070 char *ondisk_path = NULL;
3072 TAILQ_INIT(&arg.ignores);
3074 if (asprintf(&ondisk_path, "%s%s%s",
3075 worktree->root_path, path[0] ? "/" : "", path) == -1)
3076 return got_error_from_errno("asprintf");
3078 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3080 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3082 err = got_error_from_errno2("open", ondisk_path);
3084 err = report_single_file_status(path, ondisk_path,
3085 fileindex, status_cb, status_arg, repo,
3088 fdiff_cb.diff_old_new = status_old_new;
3089 fdiff_cb.diff_old = status_old;
3090 fdiff_cb.diff_new = status_new;
3091 fdiff_cb.diff_traverse = status_traverse;
3092 arg.fileindex = fileindex;
3093 arg.worktree = worktree;
3094 arg.status_path = path;
3095 arg.status_path_len = strlen(path);
3097 arg.status_cb = status_cb;
3098 arg.status_arg = status_arg;
3099 arg.cancel_cb = cancel_cb;
3100 arg.cancel_arg = cancel_arg;
3101 arg.report_unchanged = report_unchanged;
3102 arg.no_ignores = no_ignores;
3104 err = add_ignores_from_parent_paths(&arg.ignores,
3105 worktree->root_path, path);
3109 err = got_fileindex_diff_dir(fileindex, fd,
3110 worktree->root_path, path, repo, &fdiff_cb, &arg);
3113 free_ignores(&arg.ignores);
3114 if (fd != -1 && close(fd) != 0 && err == NULL)
3115 err = got_error_from_errno("close");
3120 const struct got_error *
3121 got_worktree_status(struct got_worktree *worktree,
3122 struct got_pathlist_head *paths, struct got_repository *repo,
3123 got_worktree_status_cb status_cb, void *status_arg,
3124 got_cancel_cb cancel_cb, void *cancel_arg)
3126 const struct got_error *err = NULL;
3127 char *fileindex_path = NULL;
3128 struct got_fileindex *fileindex = NULL;
3129 struct got_pathlist_entry *pe;
3131 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3135 TAILQ_FOREACH(pe, paths, entry) {
3136 err = worktree_status(worktree, pe->path, fileindex, repo,
3137 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3141 free(fileindex_path);
3142 got_fileindex_free(fileindex);
3146 const struct got_error *
3147 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3150 const struct got_error *err = NULL;
3151 char *resolved = NULL, *cwd = NULL, *path = NULL;
3157 cwd = getcwd(NULL, 0);
3159 return got_error_from_errno("getcwd");
3161 if (lstat(arg, &sb) == -1) {
3162 if (errno != ENOENT) {
3163 err = got_error_from_errno2("lstat", arg);
3167 if (S_ISLNK(sb.st_mode)) {
3169 * We cannot use realpath(3) with symlinks since we want to
3170 * operate on the symlink itself.
3171 * But we can make the path absolute, assuming it is relative
3172 * to the current working directory, and then canonicalize it.
3174 char *abspath = NULL;
3175 char canonpath[PATH_MAX];
3176 if (!got_path_is_absolute(arg)) {
3177 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3178 err = got_error_from_errno("asprintf");
3183 err = got_canonpath(abspath ? abspath : arg, canonpath,
3187 resolved = strdup(canonpath);
3188 if (resolved == NULL) {
3189 err = got_error_from_errno("strdup");
3193 resolved = realpath(arg, NULL);
3194 if (resolved == NULL) {
3195 if (errno != ENOENT) {
3196 err = got_error_from_errno2("realpath", arg);
3199 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
3200 err = got_error_from_errno("asprintf");
3206 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3207 strlen(got_worktree_get_root_path(worktree)))) {
3208 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3212 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3213 err = got_path_skip_common_ancestor(&path,
3214 got_worktree_get_root_path(worktree), resolved);
3220 err = got_error_from_errno("strdup");
3225 /* XXX status walk can't deal with trailing slash! */
3227 while (len > 0 && path[len - 1] == '/') {
3228 path[len - 1] = '\0';
3241 struct schedule_addition_args {
3242 struct got_worktree *worktree;
3243 struct got_fileindex *fileindex;
3244 got_worktree_checkout_cb progress_cb;
3246 struct got_repository *repo;
3249 static const struct got_error *
3250 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3251 const char *relpath, struct got_object_id *blob_id,
3252 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3253 int dirfd, const char *de_name)
3255 struct schedule_addition_args *a = arg;
3256 const struct got_error *err = NULL;
3257 struct got_fileindex_entry *ie;
3261 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3263 return got_error_from_errno("asprintf");
3265 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3267 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3271 /* Re-adding an existing entry is a no-op. */
3272 if (status == GOT_STATUS_ADD)
3274 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3279 if (status != GOT_STATUS_UNVERSIONED) {
3280 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3284 err = got_fileindex_entry_alloc(&ie, relpath);
3287 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3289 got_fileindex_entry_free(ie);
3292 err = got_fileindex_entry_add(a->fileindex, ie);
3294 got_fileindex_entry_free(ie);
3301 if (status == GOT_STATUS_ADD)
3303 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3306 const struct got_error *
3307 got_worktree_schedule_add(struct got_worktree *worktree,
3308 struct got_pathlist_head *paths,
3309 got_worktree_checkout_cb progress_cb, void *progress_arg,
3310 struct got_repository *repo, int no_ignores)
3312 struct got_fileindex *fileindex = NULL;
3313 char *fileindex_path = NULL;
3314 const struct got_error *err = NULL, *sync_err, *unlockerr;
3315 struct got_pathlist_entry *pe;
3316 struct schedule_addition_args saa;
3318 err = lock_worktree(worktree, LOCK_EX);
3322 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3326 saa.worktree = worktree;
3327 saa.fileindex = fileindex;
3328 saa.progress_cb = progress_cb;
3329 saa.progress_arg = progress_arg;
3332 TAILQ_FOREACH(pe, paths, entry) {
3333 err = worktree_status(worktree, pe->path, fileindex, repo,
3334 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3338 sync_err = sync_fileindex(fileindex, fileindex_path);
3339 if (sync_err && err == NULL)
3342 free(fileindex_path);
3344 got_fileindex_free(fileindex);
3345 unlockerr = lock_worktree(worktree, LOCK_SH);
3346 if (unlockerr && err == NULL)
3351 struct schedule_deletion_args {
3352 struct got_worktree *worktree;
3353 struct got_fileindex *fileindex;
3354 got_worktree_delete_cb progress_cb;
3356 struct got_repository *repo;
3357 int delete_local_mods;
3361 static const struct got_error *
3362 schedule_for_deletion(void *arg, unsigned char status,
3363 unsigned char staged_status, const char *relpath,
3364 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3365 struct got_object_id *commit_id, int dirfd, const char *de_name)
3367 struct schedule_deletion_args *a = arg;
3368 const struct got_error *err = NULL;
3369 struct got_fileindex_entry *ie = NULL;
3371 char *ondisk_path, *parent = NULL;
3373 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3375 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3377 staged_status = get_staged_status(ie);
3378 if (staged_status != GOT_STATUS_NO_CHANGE) {
3379 if (staged_status == GOT_STATUS_DELETE)
3381 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3384 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3386 return got_error_from_errno("asprintf");
3388 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3393 if (status != GOT_STATUS_NO_CHANGE) {
3394 if (status == GOT_STATUS_DELETE)
3396 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3397 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3400 if (status != GOT_STATUS_MODIFY &&
3401 status != GOT_STATUS_MISSING) {
3402 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3407 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3409 if (unlinkat(dirfd, de_name, 0) != 0) {
3410 err = got_error_from_errno2("unlinkat",
3414 } else if (unlink(ondisk_path) != 0) {
3415 err = got_error_from_errno2("unlink", ondisk_path);
3419 parent = dirname(ondisk_path);
3421 if (parent == NULL) {
3422 err = got_error_from_errno2("dirname", ondisk_path);
3425 while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3426 if (rmdir(parent) == -1) {
3427 if (errno != ENOTEMPTY)
3428 err = got_error_from_errno2("rmdir",
3432 parent = dirname(parent);
3433 if (parent == NULL) {
3434 err = got_error_from_errno2("dirname", parent);
3440 got_fileindex_entry_mark_deleted_from_disk(ie);
3445 if (status == GOT_STATUS_DELETE)
3447 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3448 staged_status, relpath);
3451 const struct got_error *
3452 got_worktree_schedule_delete(struct got_worktree *worktree,
3453 struct got_pathlist_head *paths, int delete_local_mods,
3454 got_worktree_delete_cb progress_cb, void *progress_arg,
3455 struct got_repository *repo, int keep_on_disk)
3457 struct got_fileindex *fileindex = NULL;
3458 char *fileindex_path = NULL;
3459 const struct got_error *err = NULL, *sync_err, *unlockerr;
3460 struct got_pathlist_entry *pe;
3461 struct schedule_deletion_args sda;
3463 err = lock_worktree(worktree, LOCK_EX);
3467 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3471 sda.worktree = worktree;
3472 sda.fileindex = fileindex;
3473 sda.progress_cb = progress_cb;
3474 sda.progress_arg = progress_arg;
3476 sda.delete_local_mods = delete_local_mods;
3477 sda.keep_on_disk = keep_on_disk;
3479 TAILQ_FOREACH(pe, paths, entry) {
3480 err = worktree_status(worktree, pe->path, fileindex, repo,
3481 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3485 sync_err = sync_fileindex(fileindex, fileindex_path);
3486 if (sync_err && err == NULL)
3489 free(fileindex_path);
3491 got_fileindex_free(fileindex);
3492 unlockerr = lock_worktree(worktree, LOCK_SH);
3493 if (unlockerr && err == NULL)
3498 static const struct got_error *
3499 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3501 const struct got_error *err = NULL;
3503 size_t linesize = 0, n;
3506 linelen = getline(&line, &linesize, infile);
3507 if (linelen == -1) {
3508 if (ferror(infile)) {
3509 err = got_error_from_errno("getline");
3515 n = fwrite(line, 1, linelen, outfile);
3517 err = got_ferror(outfile, GOT_ERR_IO);
3522 n = fwrite(line, 1, linelen, rejectfile);
3524 err = got_ferror(outfile, GOT_ERR_IO);
3531 static const struct got_error *
3532 skip_one_line(FILE *f)
3535 size_t linesize = 0;
3538 linelen = getline(&line, &linesize, f);
3539 if (linelen == -1) {
3541 return got_error_from_errno("getline");
3548 static const struct got_error *
3549 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3550 int start_old, int end_old, int start_new, int end_new,
3551 FILE *outfile, FILE *rejectfile)
3553 const struct got_error *err;
3555 /* Copy old file's lines leading up to patch. */
3556 while (!feof(f1) && *line_cur1 < start_old) {
3557 err = copy_one_line(f1, outfile, NULL);
3562 /* Skip new file's lines leading up to patch. */
3563 while (!feof(f2) && *line_cur2 < start_new) {
3565 err = copy_one_line(f2, NULL, rejectfile);
3567 err = skip_one_line(f2);
3572 /* Copy patched lines. */
3573 while (!feof(f2) && *line_cur2 <= end_new) {
3574 err = copy_one_line(f2, outfile, NULL);
3579 /* Skip over old file's replaced lines. */
3580 while (!feof(f1) && *line_cur1 <= end_old) {
3582 err = copy_one_line(f1, NULL, rejectfile);
3584 err = skip_one_line(f1);
3593 static const struct got_error *
3594 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3595 FILE *outfile, FILE *rejectfile)
3597 const struct got_error *err;
3600 /* Copy old file's lines until EOF. */
3602 err = copy_one_line(f1, outfile, NULL);
3609 /* Copy new file's lines until EOF. */
3611 err = copy_one_line(f2, NULL, rejectfile);
3621 static const struct got_error *
3622 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3623 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3624 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3625 int *line_cur2, FILE *outfile, FILE *rejectfile,
3626 got_worktree_patch_cb patch_cb, void *patch_arg)
3628 const struct got_error *err = NULL;
3629 int start_old = change->cv.a;
3630 int end_old = change->cv.b;
3631 int start_new = change->cv.c;
3632 int end_new = change->cv.d;
3636 *choice = GOT_PATCH_CHOICE_NONE;
3638 hunkfile = got_opentemp();
3639 if (hunkfile == NULL)
3640 return got_error_from_errno("got_opentemp");
3645 /* XXX TODO needs error checking */
3646 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
3648 if (fseek(f1, pos1, SEEK_SET) == -1) {
3649 err = got_ferror(f1, GOT_ERR_IO);
3652 if (fseek(f2, pos2, SEEK_SET) == -1) {
3653 err = got_ferror(f1, GOT_ERR_IO);
3656 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
3657 err = got_ferror(hunkfile, GOT_ERR_IO);
3661 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
3662 hunkfile, n, nchanges);
3667 case GOT_PATCH_CHOICE_YES:
3668 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3669 end_old, start_new, end_new, outfile, rejectfile);
3671 case GOT_PATCH_CHOICE_NO:
3672 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3673 end_old, start_new, end_new, rejectfile, outfile);
3675 case GOT_PATCH_CHOICE_QUIT:
3678 err = got_error(GOT_ERR_PATCH_CHOICE);
3682 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3683 err = got_error_from_errno("fclose");
3687 struct revert_file_args {
3688 struct got_worktree *worktree;
3689 struct got_fileindex *fileindex;
3690 got_worktree_checkout_cb progress_cb;
3692 got_worktree_patch_cb patch_cb;
3694 struct got_repository *repo;
3697 static const struct got_error *
3698 create_patched_content(char **path_outfile, int reverse_patch,
3699 struct got_object_id *blob_id, const char *path2,
3700 int dirfd2, const char *de_name2,
3701 const char *relpath, struct got_repository *repo,
3702 got_worktree_patch_cb patch_cb, void *patch_arg)
3704 const struct got_error *err;
3705 struct got_blob_object *blob = NULL;
3706 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3708 char *path1 = NULL, *id_str = NULL;
3709 struct stat sb1, sb2;
3710 struct got_diff_changes *changes = NULL;
3711 struct got_diff_state *ds = NULL;
3712 struct got_diff_args *args = NULL;
3713 struct got_diff_change *change;
3714 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3717 *path_outfile = NULL;
3719 err = got_object_id_str(&id_str, blob_id);
3724 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
3726 err = got_error_from_errno2("openat", path2);
3730 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
3732 err = got_error_from_errno2("open", path2);
3736 if (fstat(fd2, &sb2) == -1) {
3737 err = got_error_from_errno2("fstat", path2);
3741 f2 = fdopen(fd2, "r");
3743 err = got_error_from_errno2("fdopen", path2);
3748 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
3752 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
3756 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
3760 if (stat(path1, &sb1) == -1) {
3761 err = got_error_from_errno2("stat", path1);
3765 err = got_diff_files(&changes, &ds, &args, &diff_flags,
3766 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
3770 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
3774 if (fseek(f1, 0L, SEEK_SET) == -1)
3775 return got_ferror(f1, GOT_ERR_IO);
3776 if (fseek(f2, 0L, SEEK_SET) == -1)
3777 return got_ferror(f2, GOT_ERR_IO);
3778 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
3780 err = apply_or_reject_change(&choice, change, ++n,
3781 changes->nchanges, ds, args, diff_flags, relpath,
3782 f1, f2, &line_cur1, &line_cur2,
3783 reverse_patch ? NULL : outfile,
3784 reverse_patch ? outfile : NULL,
3785 patch_cb, patch_arg);
3788 if (choice == GOT_PATCH_CHOICE_YES)
3790 else if (choice == GOT_PATCH_CHOICE_QUIT)
3794 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
3795 reverse_patch ? NULL : outfile,
3796 reverse_patch ? outfile : NULL);
3800 if (chmod(*path_outfile, sb2.st_mode) == -1) {
3801 err = got_error_from_errno2("chmod", path2);
3808 got_object_blob_close(blob);
3809 if (f1 && fclose(f1) == EOF && err == NULL)
3810 err = got_error_from_errno2("fclose", path1);
3811 if (f2 && fclose(f2) == EOF && err == NULL)
3812 err = got_error_from_errno2("fclose", path2);
3813 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3814 err = got_error_from_errno2("close", path2);
3815 if (outfile && fclose(outfile) == EOF && err == NULL)
3816 err = got_error_from_errno2("fclose", *path_outfile);
3817 if (path1 && unlink(path1) == -1 && err == NULL)
3818 err = got_error_from_errno2("unlink", path1);
3819 if (err || !have_content) {
3820 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
3821 err = got_error_from_errno2("unlink", *path_outfile);
3822 free(*path_outfile);
3823 *path_outfile = NULL;
3827 got_diff_state_free(ds);
3831 got_diff_free_changes(changes);
3836 static const struct got_error *
3837 revert_file(void *arg, unsigned char status, unsigned char staged_status,
3838 const char *relpath, struct got_object_id *blob_id,
3839 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3840 int dirfd, const char *de_name)
3842 struct revert_file_args *a = arg;
3843 const struct got_error *err = NULL;
3844 char *parent_path = NULL;
3845 struct got_fileindex_entry *ie;
3846 struct got_tree_object *tree = NULL;
3847 struct got_object_id *tree_id = NULL;
3848 const struct got_tree_entry *te = NULL;
3849 char *tree_path = NULL, *te_name;
3850 char *ondisk_path = NULL, *path_content = NULL;
3851 struct got_blob_object *blob = NULL;
3853 /* Reverting a staged deletion is a no-op. */
3854 if (status == GOT_STATUS_DELETE &&
3855 staged_status != GOT_STATUS_NO_CHANGE)
3858 if (status == GOT_STATUS_UNVERSIONED)
3859 return (*a->progress_cb)(a->progress_arg,
3860 GOT_STATUS_UNVERSIONED, relpath);
3862 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3864 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3866 /* Construct in-repository path of tree which contains this blob. */
3867 err = got_path_dirname(&parent_path, ie->path);
3869 if (err->code != GOT_ERR_BAD_PATH)
3871 parent_path = strdup("/");
3872 if (parent_path == NULL) {
3873 err = got_error_from_errno("strdup");
3877 if (got_path_is_root_dir(a->worktree->path_prefix)) {
3878 tree_path = strdup(parent_path);
3879 if (tree_path == NULL) {
3880 err = got_error_from_errno("strdup");
3884 if (got_path_is_root_dir(parent_path)) {
3885 tree_path = strdup(a->worktree->path_prefix);
3886 if (tree_path == NULL) {
3887 err = got_error_from_errno("strdup");
3891 if (asprintf(&tree_path, "%s/%s",
3892 a->worktree->path_prefix, parent_path) == -1) {
3893 err = got_error_from_errno("asprintf");
3899 err = got_object_id_by_path(&tree_id, a->repo,
3900 a->worktree->base_commit_id, tree_path);
3902 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
3903 (status == GOT_STATUS_ADD ||
3904 staged_status == GOT_STATUS_ADD)))
3907 err = got_object_open_as_tree(&tree, a->repo, tree_id);
3911 te_name = basename(ie->path);
3912 if (te_name == NULL) {
3913 err = got_error_from_errno2("basename", ie->path);
3917 te = got_object_tree_find_entry(tree, te_name);
3918 if (te == NULL && status != GOT_STATUS_ADD &&
3919 staged_status != GOT_STATUS_ADD) {
3920 err = got_error(GOT_ERR_NO_TREE_ENTRY);
3926 case GOT_STATUS_ADD:
3928 int choice = GOT_PATCH_CHOICE_NONE;
3929 err = (*a->patch_cb)(&choice, a->patch_arg,
3930 status, ie->path, NULL, 1, 1);
3933 if (choice != GOT_PATCH_CHOICE_YES)
3936 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
3940 got_fileindex_entry_remove(a->fileindex, ie);
3942 case GOT_STATUS_DELETE:
3944 int choice = GOT_PATCH_CHOICE_NONE;
3945 err = (*a->patch_cb)(&choice, a->patch_arg,
3946 status, ie->path, NULL, 1, 1);
3949 if (choice != GOT_PATCH_CHOICE_YES)
3953 case GOT_STATUS_MODIFY:
3954 case GOT_STATUS_MODE_CHANGE:
3955 case GOT_STATUS_CONFLICT:
3956 case GOT_STATUS_MISSING: {
3957 struct got_object_id id;
3958 if (staged_status == GOT_STATUS_ADD ||
3959 staged_status == GOT_STATUS_MODIFY) {
3960 memcpy(id.sha1, ie->staged_blob_sha1,
3961 SHA1_DIGEST_LENGTH);
3963 memcpy(id.sha1, ie->blob_sha1,
3964 SHA1_DIGEST_LENGTH);
3965 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
3969 if (asprintf(&ondisk_path, "%s/%s",
3970 got_worktree_get_root_path(a->worktree), relpath) == -1) {
3971 err = got_error_from_errno("asprintf");
3975 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
3976 status == GOT_STATUS_CONFLICT)) {
3977 err = create_patched_content(&path_content, 1, &id,
3978 ondisk_path, dirfd, de_name, ie->path, a->repo,
3979 a->patch_cb, a->patch_arg);
3980 if (err || path_content == NULL)
3982 if (rename(path_content, ondisk_path) == -1) {
3983 err = got_error_from_errno3("rename",
3984 path_content, ondisk_path);
3988 err = install_blob(a->worktree, ondisk_path, ie->path,
3989 te ? te->mode : GOT_DEFAULT_FILE_MODE,
3990 got_fileindex_perms_to_st(ie), blob, 0, 1,
3991 a->repo, a->progress_cb, a->progress_arg);
3994 if (status == GOT_STATUS_DELETE ||
3995 status == GOT_STATUS_MODE_CHANGE) {
3996 err = got_fileindex_entry_update(ie,
3997 ondisk_path, blob->id.sha1,
3998 a->worktree->base_commit_id->sha1, 1);
4014 got_object_blob_close(blob);
4016 got_object_tree_close(tree);
4021 const struct got_error *
4022 got_worktree_revert(struct got_worktree *worktree,
4023 struct got_pathlist_head *paths,
4024 got_worktree_checkout_cb progress_cb, void *progress_arg,
4025 got_worktree_patch_cb patch_cb, void *patch_arg,
4026 struct got_repository *repo)
4028 struct got_fileindex *fileindex = NULL;
4029 char *fileindex_path = NULL;
4030 const struct got_error *err = NULL, *unlockerr = NULL;
4031 const struct got_error *sync_err = NULL;
4032 struct got_pathlist_entry *pe;
4033 struct revert_file_args rfa;
4035 err = lock_worktree(worktree, LOCK_EX);
4039 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4043 rfa.worktree = worktree;
4044 rfa.fileindex = fileindex;
4045 rfa.progress_cb = progress_cb;
4046 rfa.progress_arg = progress_arg;
4047 rfa.patch_cb = patch_cb;
4048 rfa.patch_arg = patch_arg;
4050 TAILQ_FOREACH(pe, paths, entry) {
4051 err = worktree_status(worktree, pe->path, fileindex, repo,
4052 revert_file, &rfa, NULL, NULL, 0, 0);
4056 sync_err = sync_fileindex(fileindex, fileindex_path);
4057 if (sync_err && err == NULL)
4060 free(fileindex_path);
4062 got_fileindex_free(fileindex);
4063 unlockerr = lock_worktree(worktree, LOCK_SH);
4064 if (unlockerr && err == NULL)
4070 free_commitable(struct got_commitable *ct)
4073 free(ct->in_repo_path);
4074 free(ct->ondisk_path);
4076 free(ct->base_blob_id);
4077 free(ct->staged_blob_id);
4078 free(ct->base_commit_id);
4082 struct collect_commitables_arg {
4083 struct got_pathlist_head *commitable_paths;
4084 struct got_repository *repo;
4085 struct got_worktree *worktree;
4086 int have_staged_files;
4089 static const struct got_error *
4090 collect_commitables(void *arg, unsigned char status,
4091 unsigned char staged_status, const char *relpath,
4092 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4093 struct got_object_id *commit_id, int dirfd, const char *de_name)
4095 struct collect_commitables_arg *a = arg;
4096 const struct got_error *err = NULL;
4097 struct got_commitable *ct = NULL;
4098 struct got_pathlist_entry *new = NULL;
4099 char *parent_path = NULL, *path = NULL;
4102 if (a->have_staged_files) {
4103 if (staged_status != GOT_STATUS_MODIFY &&
4104 staged_status != GOT_STATUS_ADD &&
4105 staged_status != GOT_STATUS_DELETE)
4108 if (status == GOT_STATUS_CONFLICT)
4109 return got_error(GOT_ERR_COMMIT_CONFLICT);
4111 if (status != GOT_STATUS_MODIFY &&
4112 status != GOT_STATUS_MODE_CHANGE &&
4113 status != GOT_STATUS_ADD &&
4114 status != GOT_STATUS_DELETE)
4118 if (asprintf(&path, "/%s", relpath) == -1) {
4119 err = got_error_from_errno("asprintf");
4122 if (strcmp(path, "/") == 0) {
4123 parent_path = strdup("");
4124 if (parent_path == NULL)
4125 return got_error_from_errno("strdup");
4127 err = got_path_dirname(&parent_path, path);
4132 ct = calloc(1, sizeof(*ct));
4134 err = got_error_from_errno("calloc");
4138 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4140 err = got_error_from_errno("asprintf");
4143 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
4144 sb.st_mode = GOT_DEFAULT_FILE_MODE;
4147 if (fstatat(dirfd, de_name, &sb,
4148 AT_SYMLINK_NOFOLLOW) == -1) {
4149 err = got_error_from_errno2("fstatat",
4153 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4154 err = got_error_from_errno2("lstat", ct->ondisk_path);
4157 ct->mode = sb.st_mode;
4160 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4161 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4163 err = got_error_from_errno("asprintf");
4167 ct->status = status;
4168 ct->staged_status = staged_status;
4169 ct->blob_id = NULL; /* will be filled in when blob gets created */
4170 if (ct->status != GOT_STATUS_ADD &&
4171 ct->staged_status != GOT_STATUS_ADD) {
4172 ct->base_blob_id = got_object_id_dup(blob_id);
4173 if (ct->base_blob_id == NULL) {
4174 err = got_error_from_errno("got_object_id_dup");
4177 ct->base_commit_id = got_object_id_dup(commit_id);
4178 if (ct->base_commit_id == NULL) {
4179 err = got_error_from_errno("got_object_id_dup");
4183 if (ct->staged_status == GOT_STATUS_ADD ||
4184 ct->staged_status == GOT_STATUS_MODIFY) {
4185 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4186 if (ct->staged_blob_id == NULL) {
4187 err = got_error_from_errno("got_object_id_dup");
4191 ct->path = strdup(path);
4192 if (ct->path == NULL) {
4193 err = got_error_from_errno("strdup");
4196 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4198 if (ct && (err || new == NULL))
4199 free_commitable(ct);
4205 static const struct got_error *write_tree(struct got_object_id **, int *,
4206 struct got_tree_object *, const char *, struct got_pathlist_head *,
4207 got_worktree_status_cb status_cb, void *status_arg,
4208 struct got_repository *);
4210 static const struct got_error *
4211 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4212 struct got_tree_entry *te, const char *parent_path,
4213 struct got_pathlist_head *commitable_paths,
4214 got_worktree_status_cb status_cb, void *status_arg,
4215 struct got_repository *repo)
4217 const struct got_error *err = NULL;
4218 struct got_tree_object *subtree;
4221 if (asprintf(&subpath, "%s%s%s", parent_path,
4222 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4223 return got_error_from_errno("asprintf");
4225 err = got_object_open_as_tree(&subtree, repo, &te->id);
4229 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4230 commitable_paths, status_cb, status_arg, repo);
4231 got_object_tree_close(subtree);
4236 static const struct got_error *
4237 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4239 const struct got_error *err = NULL;
4240 char *ct_parent_path = NULL;
4244 if (strchr(ct->in_repo_path, '/') == NULL) {
4245 *match = got_path_is_root_dir(path);
4249 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4252 *match = (strcmp(path, ct_parent_path) == 0);
4253 free(ct_parent_path);
4258 get_ct_file_mode(struct got_commitable *ct)
4260 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4263 static const struct got_error *
4264 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4265 struct got_tree_entry *te, struct got_commitable *ct)
4267 const struct got_error *err = NULL;
4271 err = got_object_tree_entry_dup(new_te, te);
4275 (*new_te)->mode = get_ct_file_mode(ct);
4277 if (ct->staged_status == GOT_STATUS_MODIFY)
4278 memcpy(&(*new_te)->id, ct->staged_blob_id,
4279 sizeof((*new_te)->id));
4281 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4283 if (err && *new_te) {
4290 static const struct got_error *
4291 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4292 struct got_commitable *ct)
4294 const struct got_error *err = NULL;
4299 *new_te = calloc(1, sizeof(**new_te));
4300 if (*new_te == NULL)
4301 return got_error_from_errno("calloc");
4303 ct_name = basename(ct->path);
4304 if (ct_name == NULL) {
4305 err = got_error_from_errno2("basename", ct->path);
4308 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4309 sizeof((*new_te)->name)) {
4310 err = got_error(GOT_ERR_NO_SPACE);
4314 (*new_te)->mode = get_ct_file_mode(ct);
4316 if (ct->staged_status == GOT_STATUS_ADD)
4317 memcpy(&(*new_te)->id, ct->staged_blob_id,
4318 sizeof((*new_te)->id));
4320 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4322 if (err && *new_te) {
4329 static const struct got_error *
4330 insert_tree_entry(struct got_tree_entry *new_te,
4331 struct got_pathlist_head *paths)
4333 const struct got_error *err = NULL;
4334 struct got_pathlist_entry *new_pe;
4336 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4340 return got_error(GOT_ERR_TREE_DUP_ENTRY);
4344 static const struct got_error *
4345 report_ct_status(struct got_commitable *ct,
4346 got_worktree_status_cb status_cb, void *status_arg)
4348 const char *ct_path = ct->path;
4349 unsigned char status;
4351 while (ct_path[0] == '/')
4354 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4355 status = ct->staged_status;
4357 status = ct->status;
4359 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4360 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4363 static const struct got_error *
4364 match_modified_subtree(int *modified, struct got_tree_entry *te,
4365 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4367 const struct got_error *err = NULL;
4368 struct got_pathlist_entry *pe;
4373 if (asprintf(&te_path, "%s%s%s", base_tree_path,
4374 got_path_is_root_dir(base_tree_path) ? "" : "/",
4376 return got_error_from_errno("asprintf");
4378 TAILQ_FOREACH(pe, commitable_paths, entry) {
4379 struct got_commitable *ct = pe->data;
4380 *modified = got_path_is_child(ct->in_repo_path, te_path,
4390 static const struct got_error *
4391 match_deleted_or_modified_ct(struct got_commitable **ctp,
4392 struct got_tree_entry *te, const char *base_tree_path,
4393 struct got_pathlist_head *commitable_paths)
4395 const struct got_error *err = NULL;
4396 struct got_pathlist_entry *pe;
4400 TAILQ_FOREACH(pe, commitable_paths, entry) {
4401 struct got_commitable *ct = pe->data;
4402 char *ct_name = NULL;
4405 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4406 if (ct->status != GOT_STATUS_MODIFY &&
4407 ct->status != GOT_STATUS_MODE_CHANGE &&
4408 ct->status != GOT_STATUS_DELETE)
4411 if (ct->staged_status != GOT_STATUS_MODIFY &&
4412 ct->staged_status != GOT_STATUS_DELETE)
4416 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4419 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4425 ct_name = basename(pe->path);
4426 if (ct_name == NULL)
4427 return got_error_from_errno2("basename", pe->path);
4429 if (strcmp(te->name, ct_name) != 0)
4439 static const struct got_error *
4440 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4441 const char *child_path, const char *path_base_tree,
4442 struct got_pathlist_head *commitable_paths,
4443 got_worktree_status_cb status_cb, void *status_arg,
4444 struct got_repository *repo)
4446 const struct got_error *err = NULL;
4447 struct got_tree_entry *new_te;
4449 struct got_object_id *id = NULL;
4454 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4455 got_path_is_root_dir(path_base_tree) ? "" : "/",
4457 return got_error_from_errno("asprintf");
4459 new_te = calloc(1, sizeof(*new_te));
4461 return got_error_from_errno("calloc");
4462 new_te->mode = S_IFDIR;
4464 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4465 sizeof(new_te->name)) {
4466 err = got_error(GOT_ERR_NO_SPACE);
4469 err = write_tree(&id, &nentries, NULL, subtree_path,
4470 commitable_paths, status_cb, status_arg, repo);
4475 memcpy(&new_te->id, id, sizeof(new_te->id));
4484 static const struct got_error *
4485 write_tree(struct got_object_id **new_tree_id, int *nentries,
4486 struct got_tree_object *base_tree, const char *path_base_tree,
4487 struct got_pathlist_head *commitable_paths,
4488 got_worktree_status_cb status_cb, void *status_arg,
4489 struct got_repository *repo)
4491 const struct got_error *err = NULL;
4492 struct got_pathlist_head paths;
4493 struct got_tree_entry *te, *new_te = NULL;
4494 struct got_pathlist_entry *pe;
4499 /* Insert, and recurse into, newly added entries first. */
4500 TAILQ_FOREACH(pe, commitable_paths, entry) {
4501 struct got_commitable *ct = pe->data;
4502 char *child_path = NULL, *slash;
4504 if ((ct->status != GOT_STATUS_ADD &&
4505 ct->staged_status != GOT_STATUS_ADD) ||
4506 (ct->flags & GOT_COMMITABLE_ADDED))
4509 if (!got_path_is_child(pe->path, path_base_tree,
4510 strlen(path_base_tree)))
4513 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
4518 slash = strchr(child_path, '/');
4519 if (slash == NULL) {
4520 err = alloc_added_blob_tree_entry(&new_te, ct);
4523 err = report_ct_status(ct, status_cb, status_arg);
4526 ct->flags |= GOT_COMMITABLE_ADDED;
4527 err = insert_tree_entry(new_te, &paths);
4532 *slash = '\0'; /* trim trailing path components */
4533 if (base_tree == NULL ||
4534 got_object_tree_find_entry(base_tree, child_path)
4536 err = make_subtree_for_added_blob(&new_te,
4537 child_path, path_base_tree,
4538 commitable_paths, status_cb, status_arg,
4542 err = insert_tree_entry(new_te, &paths);
4551 int i, nbase_entries;
4552 /* Handle modified and deleted entries. */
4553 nbase_entries = got_object_tree_get_nentries(base_tree);
4554 for (i = 0; i < nbase_entries; i++) {
4555 struct got_commitable *ct = NULL;
4557 te = got_object_tree_get_entry(base_tree, i);
4558 if (got_object_tree_entry_is_submodule(te)) {
4559 /* Entry is a submodule; just copy it. */
4560 err = got_object_tree_entry_dup(&new_te, te);
4563 err = insert_tree_entry(new_te, &paths);
4570 if (S_ISDIR(te->mode)) {
4572 err = got_object_tree_entry_dup(&new_te, te);
4575 err = match_modified_subtree(&modified, te,
4576 path_base_tree, commitable_paths);
4579 /* Avoid recursion into unmodified subtrees. */
4581 struct got_object_id *new_id;
4583 err = write_subtree(&new_id,
4585 path_base_tree, commitable_paths,
4586 status_cb, status_arg, repo);
4589 if (nsubentries == 0) {
4590 /* All entries were deleted. */
4594 memcpy(&new_te->id, new_id,
4595 sizeof(new_te->id));
4598 err = insert_tree_entry(new_te, &paths);
4605 err = match_deleted_or_modified_ct(&ct, te,
4606 path_base_tree, commitable_paths);
4610 /* NB: Deleted entries get dropped here. */
4611 if (ct->status == GOT_STATUS_MODIFY ||
4612 ct->status == GOT_STATUS_MODE_CHANGE ||
4613 ct->staged_status == GOT_STATUS_MODIFY) {
4614 err = alloc_modified_blob_tree_entry(
4618 err = insert_tree_entry(new_te, &paths);
4623 err = report_ct_status(ct, status_cb,
4628 /* Entry is unchanged; just copy it. */
4629 err = got_object_tree_entry_dup(&new_te, te);
4632 err = insert_tree_entry(new_te, &paths);
4640 /* Write new list of entries; deleted entries have been dropped. */
4641 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
4643 got_pathlist_free(&paths);
4647 static const struct got_error *
4648 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
4649 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
4650 int have_staged_files)
4652 const struct got_error *err = NULL;
4653 struct got_pathlist_entry *pe;
4655 TAILQ_FOREACH(pe, commitable_paths, entry) {
4656 struct got_fileindex_entry *ie;
4657 struct got_commitable *ct = pe->data;
4659 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4661 if (ct->status == GOT_STATUS_DELETE ||
4662 ct->staged_status == GOT_STATUS_DELETE) {
4663 got_fileindex_entry_remove(fileindex, ie);
4664 } else if (ct->staged_status == GOT_STATUS_ADD ||
4665 ct->staged_status == GOT_STATUS_MODIFY) {
4666 got_fileindex_entry_stage_set(ie,
4667 GOT_FILEIDX_STAGE_NONE);
4668 err = got_fileindex_entry_update(ie,
4669 ct->ondisk_path, ct->staged_blob_id->sha1,
4670 new_base_commit_id->sha1,
4671 !have_staged_files);
4673 err = got_fileindex_entry_update(ie,
4674 ct->ondisk_path, ct->blob_id->sha1,
4675 new_base_commit_id->sha1,
4676 !have_staged_files);
4678 err = got_fileindex_entry_alloc(&ie, pe->path);
4681 err = got_fileindex_entry_update(ie, ct->ondisk_path,
4682 ct->blob_id->sha1, new_base_commit_id->sha1, 1);
4684 got_fileindex_entry_free(ie);
4687 err = got_fileindex_entry_add(fileindex, ie);
4689 got_fileindex_entry_free(ie);
4698 static const struct got_error *
4699 check_out_of_date(const char *in_repo_path, unsigned char status,
4700 unsigned char staged_status, struct got_object_id *base_blob_id,
4701 struct got_object_id *base_commit_id,
4702 struct got_object_id *head_commit_id, struct got_repository *repo,
4705 const struct got_error *err = NULL;
4706 struct got_object_id *id = NULL;
4708 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
4709 /* Trivial case: base commit == head commit */
4710 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
4713 * Ensure file content which local changes were based
4714 * on matches file content in the branch head.
4716 err = got_object_id_by_path(&id, repo, head_commit_id,
4719 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4720 err = got_error(ood_errcode);
4722 } else if (got_object_id_cmp(id, base_blob_id) != 0)
4723 err = got_error(ood_errcode);
4725 /* Require that added files don't exist in the branch head. */
4726 err = got_object_id_by_path(&id, repo, head_commit_id,
4728 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
4730 err = id ? got_error(ood_errcode) : NULL;
4737 const struct got_error *
4738 commit_worktree(struct got_object_id **new_commit_id,
4739 struct got_pathlist_head *commitable_paths,
4740 struct got_object_id *head_commit_id, struct got_worktree *worktree,
4741 const char *author, const char *committer,
4742 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4743 got_worktree_status_cb status_cb, void *status_arg,
4744 struct got_repository *repo)
4746 const struct got_error *err = NULL, *unlockerr = NULL;
4747 struct got_pathlist_entry *pe;
4748 const char *head_ref_name = NULL;
4749 struct got_commit_object *head_commit = NULL;
4750 struct got_reference *head_ref2 = NULL;
4751 struct got_object_id *head_commit_id2 = NULL;
4752 struct got_tree_object *head_tree = NULL;
4753 struct got_object_id *new_tree_id = NULL;
4755 struct got_object_id_queue parent_ids;
4756 struct got_object_qid *pid = NULL;
4757 char *logmsg = NULL;
4759 *new_commit_id = NULL;
4761 SIMPLEQ_INIT(&parent_ids);
4763 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
4767 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
4771 if (commit_msg_cb != NULL) {
4772 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
4777 if (logmsg == NULL || strlen(logmsg) == 0) {
4778 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
4782 /* Create blobs from added and modified files and record their IDs. */
4783 TAILQ_FOREACH(pe, commitable_paths, entry) {
4784 struct got_commitable *ct = pe->data;
4787 /* Blobs for staged files already exist. */
4788 if (ct->staged_status == GOT_STATUS_ADD ||
4789 ct->staged_status == GOT_STATUS_MODIFY)
4792 if (ct->status != GOT_STATUS_ADD &&
4793 ct->status != GOT_STATUS_MODIFY &&
4794 ct->status != GOT_STATUS_MODE_CHANGE)
4797 if (asprintf(&ondisk_path, "%s/%s",
4798 worktree->root_path, pe->path) == -1) {
4799 err = got_error_from_errno("asprintf");
4802 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
4808 /* Recursively write new tree objects. */
4809 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
4810 commitable_paths, status_cb, status_arg, repo);
4814 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
4817 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
4818 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
4819 1, author, time(NULL), committer, time(NULL), logmsg, repo);
4820 got_object_qid_free(pid);
4826 /* Check if a concurrent commit to our branch has occurred. */
4827 head_ref_name = got_worktree_get_head_ref_name(worktree);
4828 if (head_ref_name == NULL) {
4829 err = got_error_from_errno("got_worktree_get_head_ref_name");
4832 /* Lock the reference here to prevent concurrent modification. */
4833 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
4836 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
4839 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
4840 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
4843 /* Update branch head in repository. */
4844 err = got_ref_change_ref(head_ref2, *new_commit_id);
4847 err = got_ref_write(head_ref2, repo);
4851 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
4855 err = ref_base_commit(worktree, repo);
4860 got_object_tree_close(head_tree);
4862 got_object_commit_close(head_commit);
4863 free(head_commit_id2);
4865 unlockerr = got_ref_unlock(head_ref2);
4866 if (unlockerr && err == NULL)
4868 got_ref_close(head_ref2);
4873 static const struct got_error *
4874 check_path_is_commitable(const char *path,
4875 struct got_pathlist_head *commitable_paths)
4877 struct got_pathlist_entry *cpe = NULL;
4878 size_t path_len = strlen(path);
4880 TAILQ_FOREACH(cpe, commitable_paths, entry) {
4881 struct got_commitable *ct = cpe->data;
4882 const char *ct_path = ct->path;
4884 while (ct_path[0] == '/')
4887 if (strcmp(path, ct_path) == 0 ||
4888 got_path_is_child(ct_path, path, path_len))
4893 return got_error_path(path, GOT_ERR_BAD_PATH);
4898 static const struct got_error *
4899 check_staged_file(void *arg, struct got_fileindex_entry *ie)
4901 int *have_staged_files = arg;
4903 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
4904 *have_staged_files = 1;
4905 return got_error(GOT_ERR_CANCELLED);
4911 static const struct got_error *
4912 check_non_staged_files(struct got_fileindex *fileindex,
4913 struct got_pathlist_head *paths)
4915 struct got_pathlist_entry *pe;
4916 struct got_fileindex_entry *ie;
4918 TAILQ_FOREACH(pe, paths, entry) {
4919 if (pe->path[0] == '\0')
4921 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4923 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
4924 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
4925 return got_error_path(pe->path,
4926 GOT_ERR_FILE_NOT_STAGED);
4932 const struct got_error *
4933 got_worktree_commit(struct got_object_id **new_commit_id,
4934 struct got_worktree *worktree, struct got_pathlist_head *paths,
4935 const char *author, const char *committer,
4936 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4937 got_worktree_status_cb status_cb, void *status_arg,
4938 struct got_repository *repo)
4940 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
4941 struct got_fileindex *fileindex = NULL;
4942 char *fileindex_path = NULL;
4943 struct got_pathlist_head commitable_paths;
4944 struct collect_commitables_arg cc_arg;
4945 struct got_pathlist_entry *pe;
4946 struct got_reference *head_ref = NULL;
4947 struct got_object_id *head_commit_id = NULL;
4948 int have_staged_files = 0;
4950 *new_commit_id = NULL;
4952 TAILQ_INIT(&commitable_paths);
4954 err = lock_worktree(worktree, LOCK_EX);
4958 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4962 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4966 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4970 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
4971 &have_staged_files);
4972 if (err && err->code != GOT_ERR_CANCELLED)
4974 if (have_staged_files) {
4975 err = check_non_staged_files(fileindex, paths);
4980 cc_arg.commitable_paths = &commitable_paths;
4981 cc_arg.worktree = worktree;
4983 cc_arg.have_staged_files = have_staged_files;
4984 TAILQ_FOREACH(pe, paths, entry) {
4985 err = worktree_status(worktree, pe->path, fileindex, repo,
4986 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
4991 if (TAILQ_EMPTY(&commitable_paths)) {
4992 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4996 TAILQ_FOREACH(pe, paths, entry) {
4997 err = check_path_is_commitable(pe->path, &commitable_paths);
5002 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5003 struct got_commitable *ct = pe->data;
5004 const char *ct_path = ct->in_repo_path;
5006 while (ct_path[0] == '/')
5008 err = check_out_of_date(ct_path, ct->status,
5009 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5010 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5016 err = commit_worktree(new_commit_id, &commitable_paths,
5017 head_commit_id, worktree, author, committer,
5018 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5022 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5023 fileindex, have_staged_files);
5024 sync_err = sync_fileindex(fileindex, fileindex_path);
5025 if (sync_err && err == NULL)
5029 got_fileindex_free(fileindex);
5030 free(fileindex_path);
5031 unlockerr = lock_worktree(worktree, LOCK_SH);
5032 if (unlockerr && err == NULL)
5034 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5035 struct got_commitable *ct = pe->data;
5036 free_commitable(ct);
5038 got_pathlist_free(&commitable_paths);
5043 got_commitable_get_path(struct got_commitable *ct)
5049 got_commitable_get_status(struct got_commitable *ct)
5054 struct check_rebase_ok_arg {
5055 struct got_worktree *worktree;
5056 struct got_repository *repo;
5059 static const struct got_error *
5060 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5062 const struct got_error *err = NULL;
5063 struct check_rebase_ok_arg *a = arg;
5064 unsigned char status;
5068 /* Reject rebase of a work tree with mixed base commits. */
5069 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5070 SHA1_DIGEST_LENGTH))
5071 return got_error(GOT_ERR_MIXED_COMMITS);
5073 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5075 return got_error_from_errno("asprintf");
5077 /* Reject rebase of a work tree with modified or staged files. */
5078 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5083 if (status != GOT_STATUS_NO_CHANGE)
5084 return got_error(GOT_ERR_MODIFIED);
5085 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5086 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5091 const struct got_error *
5092 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5093 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5094 struct got_worktree *worktree, struct got_reference *branch,
5095 struct got_repository *repo)
5097 const struct got_error *err = NULL;
5098 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5099 char *branch_ref_name = NULL;
5100 char *fileindex_path = NULL;
5101 struct check_rebase_ok_arg ok_arg;
5102 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5103 struct got_object_id *wt_branch_tip = NULL;
5105 *new_base_branch_ref = NULL;
5109 err = lock_worktree(worktree, LOCK_EX);
5113 err = open_fileindex(fileindex, &fileindex_path, worktree);
5117 ok_arg.worktree = worktree;
5119 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5124 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5128 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5132 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5136 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5141 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5144 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5145 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5149 err = got_ref_alloc_symref(new_base_branch_ref,
5150 new_base_branch_ref_name, wt_branch);
5153 err = got_ref_write(*new_base_branch_ref, repo);
5157 /* TODO Lock original branch's ref while rebasing? */
5159 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5163 err = got_ref_write(branch_ref, repo);
5167 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5168 worktree->base_commit_id);
5171 err = got_ref_write(*tmp_branch, repo);
5175 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5179 free(fileindex_path);
5180 free(tmp_branch_name);
5181 free(new_base_branch_ref_name);
5182 free(branch_ref_name);
5184 got_ref_close(branch_ref);
5186 got_ref_close(wt_branch);
5187 free(wt_branch_tip);
5189 if (*new_base_branch_ref) {
5190 got_ref_close(*new_base_branch_ref);
5191 *new_base_branch_ref = NULL;
5194 got_ref_close(*tmp_branch);
5198 got_fileindex_free(*fileindex);
5201 lock_worktree(worktree, LOCK_SH);
5206 const struct got_error *
5207 got_worktree_rebase_continue(struct got_object_id **commit_id,
5208 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5209 struct got_reference **branch, struct got_fileindex **fileindex,
5210 struct got_worktree *worktree, struct got_repository *repo)
5212 const struct got_error *err;
5213 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5214 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5215 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5216 char *fileindex_path = NULL;
5217 int have_staged_files = 0;
5220 *new_base_branch = NULL;
5225 err = lock_worktree(worktree, LOCK_EX);
5229 err = open_fileindex(fileindex, &fileindex_path, worktree);
5233 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5234 &have_staged_files);
5235 if (err && err->code != GOT_ERR_CANCELLED)
5237 if (have_staged_files) {
5238 err = got_error(GOT_ERR_STAGED_PATHS);
5242 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5246 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5250 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5254 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5258 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5262 err = got_ref_open(branch, repo,
5263 got_ref_get_symref_target(branch_ref), 0);
5267 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5271 err = got_ref_resolve(commit_id, repo, commit_ref);
5275 err = got_ref_open(new_base_branch, repo,
5276 new_base_branch_ref_name, 0);
5280 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5284 free(commit_ref_name);
5285 free(branch_ref_name);
5286 free(fileindex_path);
5288 got_ref_close(commit_ref);
5290 got_ref_close(branch_ref);
5295 got_ref_close(*tmp_branch);
5298 if (*new_base_branch) {
5299 got_ref_close(*new_base_branch);
5300 *new_base_branch = NULL;
5303 got_ref_close(*branch);
5307 got_fileindex_free(*fileindex);
5310 lock_worktree(worktree, LOCK_SH);
5315 const struct got_error *
5316 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5318 const struct got_error *err;
5319 char *tmp_branch_name = NULL;
5321 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5325 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5326 free(tmp_branch_name);
5330 static const struct got_error *
5331 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5332 char **logmsg, void *arg)
5338 static const struct got_error *
5339 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5340 const char *path, struct got_object_id *blob_id,
5341 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5342 int dirfd, const char *de_name)
5347 struct collect_merged_paths_arg {
5348 got_worktree_checkout_cb progress_cb;
5350 struct got_pathlist_head *merged_paths;
5353 static const struct got_error *
5354 collect_merged_paths(void *arg, unsigned char status, const char *path)
5356 const struct got_error *err;
5357 struct collect_merged_paths_arg *a = arg;
5359 struct got_pathlist_entry *new;
5361 err = (*a->progress_cb)(a->progress_arg, status, path);
5365 if (status != GOT_STATUS_MERGE &&
5366 status != GOT_STATUS_ADD &&
5367 status != GOT_STATUS_DELETE &&
5368 status != GOT_STATUS_CONFLICT)
5373 return got_error_from_errno("strdup");
5375 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5376 if (err || new == NULL)
5382 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5384 struct got_pathlist_entry *pe;
5386 TAILQ_FOREACH(pe, merged_paths, entry)
5387 free((char *)pe->path);
5389 got_pathlist_free(merged_paths);
5392 static const struct got_error *
5393 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5394 int is_rebase, struct got_repository *repo)
5396 const struct got_error *err;
5397 struct got_reference *commit_ref = NULL;
5399 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5401 if (err->code != GOT_ERR_NOT_REF)
5403 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
5406 err = got_ref_write(commit_ref, repo);
5409 } else if (is_rebase) {
5410 struct got_object_id *stored_id;
5413 err = got_ref_resolve(&stored_id, repo, commit_ref);
5416 cmp = got_object_id_cmp(commit_id, stored_id);
5419 err = got_error(GOT_ERR_REBASE_COMMITID);
5425 got_ref_close(commit_ref);
5429 static const struct got_error *
5430 rebase_merge_files(struct got_pathlist_head *merged_paths,
5431 const char *commit_ref_name, struct got_worktree *worktree,
5432 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
5433 struct got_object_id *commit_id, struct got_repository *repo,
5434 got_worktree_checkout_cb progress_cb, void *progress_arg,
5435 got_cancel_cb cancel_cb, void *cancel_arg)
5437 const struct got_error *err;
5438 struct got_reference *commit_ref = NULL;
5439 struct collect_merged_paths_arg cmp_arg;
5440 char *fileindex_path;
5442 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5444 err = get_fileindex_path(&fileindex_path, worktree);
5448 cmp_arg.progress_cb = progress_cb;
5449 cmp_arg.progress_arg = progress_arg;
5450 cmp_arg.merged_paths = merged_paths;
5451 err = merge_files(worktree, fileindex, fileindex_path,
5452 parent_commit_id, commit_id, repo, collect_merged_paths,
5453 &cmp_arg, cancel_cb, cancel_arg);
5455 got_ref_close(commit_ref);
5459 const struct got_error *
5460 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
5461 struct got_worktree *worktree, struct got_fileindex *fileindex,
5462 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5463 struct got_repository *repo,
5464 got_worktree_checkout_cb progress_cb, void *progress_arg,
5465 got_cancel_cb cancel_cb, void *cancel_arg)
5467 const struct got_error *err;
5468 char *commit_ref_name;
5470 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5474 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
5478 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5479 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5480 progress_arg, cancel_cb, cancel_arg);
5482 free(commit_ref_name);
5486 const struct got_error *
5487 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
5488 struct got_worktree *worktree, struct got_fileindex *fileindex,
5489 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5490 struct got_repository *repo,
5491 got_worktree_checkout_cb progress_cb, void *progress_arg,
5492 got_cancel_cb cancel_cb, void *cancel_arg)
5494 const struct got_error *err;
5495 char *commit_ref_name;
5497 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5501 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
5505 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5506 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5507 progress_arg, cancel_cb, cancel_arg);
5509 free(commit_ref_name);
5513 static const struct got_error *
5514 rebase_commit(struct got_object_id **new_commit_id,
5515 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
5516 struct got_worktree *worktree, struct got_fileindex *fileindex,
5517 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
5518 const char *new_logmsg, struct got_repository *repo)
5520 const struct got_error *err, *sync_err;
5521 struct got_pathlist_head commitable_paths;
5522 struct collect_commitables_arg cc_arg;
5523 char *fileindex_path = NULL;
5524 struct got_reference *head_ref = NULL;
5525 struct got_object_id *head_commit_id = NULL;
5526 char *logmsg = NULL;
5528 TAILQ_INIT(&commitable_paths);
5529 *new_commit_id = NULL;
5531 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5533 err = get_fileindex_path(&fileindex_path, worktree);
5537 cc_arg.commitable_paths = &commitable_paths;
5538 cc_arg.worktree = worktree;
5540 cc_arg.have_staged_files = 0;
5542 * If possible get the status of individual files directly to
5543 * avoid crawling the entire work tree once per rebased commit.
5544 * TODO: Ideally, merged_paths would contain a list of commitables
5545 * we could use so we could skip worktree_status() entirely.
5548 struct got_pathlist_entry *pe;
5549 TAILQ_FOREACH(pe, merged_paths, entry) {
5550 err = worktree_status(worktree, pe->path, fileindex,
5551 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
5557 err = worktree_status(worktree, "", fileindex, repo,
5558 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5563 if (TAILQ_EMPTY(&commitable_paths)) {
5564 /* No-op change; commit will be elided. */
5565 err = got_ref_delete(commit_ref, repo);
5568 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5572 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5576 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5581 logmsg = strdup(new_logmsg);
5582 if (logmsg == NULL) {
5583 err = got_error_from_errno("strdup");
5587 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
5592 /* NB: commit_worktree will call free(logmsg) */
5593 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
5594 worktree, got_object_commit_get_author(orig_commit),
5595 got_object_commit_get_committer(orig_commit),
5596 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
5600 err = got_ref_change_ref(tmp_branch, *new_commit_id);
5604 err = got_ref_delete(commit_ref, repo);
5608 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5610 sync_err = sync_fileindex(fileindex, fileindex_path);
5611 if (sync_err && err == NULL)
5614 free(fileindex_path);
5615 free(head_commit_id);
5617 got_ref_close(head_ref);
5619 free(*new_commit_id);
5620 *new_commit_id = NULL;
5625 const struct got_error *
5626 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
5627 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5628 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5629 struct got_commit_object *orig_commit,
5630 struct got_object_id *orig_commit_id, struct got_repository *repo)
5632 const struct got_error *err;
5633 char *commit_ref_name;
5634 struct got_reference *commit_ref = NULL;
5635 struct got_object_id *commit_id = NULL;
5637 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5641 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5644 err = got_ref_resolve(&commit_id, repo, commit_ref);
5647 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5648 err = got_error(GOT_ERR_REBASE_COMMITID);
5652 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5653 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
5656 got_ref_close(commit_ref);
5657 free(commit_ref_name);
5662 const struct got_error *
5663 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
5664 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5665 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5666 struct got_commit_object *orig_commit,
5667 struct got_object_id *orig_commit_id, const char *new_logmsg,
5668 struct got_repository *repo)
5670 const struct got_error *err;
5671 char *commit_ref_name;
5672 struct got_reference *commit_ref = NULL;
5674 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5678 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5682 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5683 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
5686 got_ref_close(commit_ref);
5687 free(commit_ref_name);
5691 const struct got_error *
5692 got_worktree_rebase_postpone(struct got_worktree *worktree,
5693 struct got_fileindex *fileindex)
5696 got_fileindex_free(fileindex);
5697 return lock_worktree(worktree, LOCK_SH);
5700 static const struct got_error *
5701 delete_ref(const char *name, struct got_repository *repo)
5703 const struct got_error *err;
5704 struct got_reference *ref;
5706 err = got_ref_open(&ref, repo, name, 0);
5708 if (err->code == GOT_ERR_NOT_REF)
5713 err = got_ref_delete(ref, repo);
5718 static const struct got_error *
5719 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
5721 const struct got_error *err;
5722 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5723 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5725 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5728 err = delete_ref(tmp_branch_name, repo);
5732 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5735 err = delete_ref(new_base_branch_ref_name, repo);
5739 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5742 err = delete_ref(branch_ref_name, repo);
5746 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5749 err = delete_ref(commit_ref_name, repo);
5754 free(tmp_branch_name);
5755 free(new_base_branch_ref_name);
5756 free(branch_ref_name);
5757 free(commit_ref_name);
5761 const struct got_error *
5762 got_worktree_rebase_complete(struct got_worktree *worktree,
5763 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
5764 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
5765 struct got_repository *repo)
5767 const struct got_error *err, *unlockerr;
5768 struct got_object_id *new_head_commit_id = NULL;
5770 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5774 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
5778 err = got_ref_write(rebased_branch, repo);
5782 err = got_worktree_set_head_ref(worktree, rebased_branch);
5786 err = delete_rebase_refs(worktree, repo);
5789 got_fileindex_free(fileindex);
5790 free(new_head_commit_id);
5791 unlockerr = lock_worktree(worktree, LOCK_SH);
5792 if (unlockerr && err == NULL)
5797 const struct got_error *
5798 got_worktree_rebase_abort(struct got_worktree *worktree,
5799 struct got_fileindex *fileindex, struct got_repository *repo,
5800 struct got_reference *new_base_branch,
5801 got_worktree_checkout_cb progress_cb, void *progress_arg)
5803 const struct got_error *err, *unlockerr, *sync_err;
5804 struct got_reference *resolved = NULL;
5805 struct got_object_id *commit_id = NULL;
5806 char *fileindex_path = NULL;
5807 struct revert_file_args rfa;
5808 struct got_object_id *tree_id = NULL;
5810 err = lock_worktree(worktree, LOCK_EX);
5814 err = got_ref_open(&resolved, repo,
5815 got_ref_get_symref_target(new_base_branch), 0);
5819 err = got_worktree_set_head_ref(worktree, resolved);
5824 * XXX commits to the base branch could have happened while
5825 * we were busy rebasing; should we store the original commit ID
5826 * when rebase begins and read it back here?
5828 err = got_ref_resolve(&commit_id, repo, resolved);
5832 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5836 err = got_object_id_by_path(&tree_id, repo,
5837 worktree->base_commit_id, worktree->path_prefix);
5841 err = delete_rebase_refs(worktree, repo);
5845 err = get_fileindex_path(&fileindex_path, worktree);
5849 rfa.worktree = worktree;
5850 rfa.fileindex = fileindex;
5851 rfa.progress_cb = progress_cb;
5852 rfa.progress_arg = progress_arg;
5853 rfa.patch_cb = NULL;
5854 rfa.patch_arg = NULL;
5856 err = worktree_status(worktree, "", fileindex, repo,
5857 revert_file, &rfa, NULL, NULL, 0, 0);
5861 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5862 repo, progress_cb, progress_arg, NULL, NULL);
5864 sync_err = sync_fileindex(fileindex, fileindex_path);
5865 if (sync_err && err == NULL)
5868 got_ref_close(resolved);
5872 got_fileindex_free(fileindex);
5873 free(fileindex_path);
5875 unlockerr = lock_worktree(worktree, LOCK_SH);
5876 if (unlockerr && err == NULL)
5881 const struct got_error *
5882 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
5883 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
5884 struct got_fileindex **fileindex, struct got_worktree *worktree,
5885 struct got_repository *repo)
5887 const struct got_error *err = NULL;
5888 char *tmp_branch_name = NULL;
5889 char *branch_ref_name = NULL;
5890 char *base_commit_ref_name = NULL;
5891 char *fileindex_path = NULL;
5892 struct check_rebase_ok_arg ok_arg;
5893 struct got_reference *wt_branch = NULL;
5894 struct got_reference *base_commit_ref = NULL;
5898 *base_commit_id = NULL;
5901 err = lock_worktree(worktree, LOCK_EX);
5905 err = open_fileindex(fileindex, &fileindex_path, worktree);
5909 ok_arg.worktree = worktree;
5911 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5916 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5920 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5924 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5929 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5934 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
5938 err = got_ref_write(*branch_ref, repo);
5942 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
5943 worktree->base_commit_id);
5946 err = got_ref_write(base_commit_ref, repo);
5949 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
5950 if (*base_commit_id == NULL) {
5951 err = got_error_from_errno("got_object_id_dup");
5955 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5956 worktree->base_commit_id);
5959 err = got_ref_write(*tmp_branch, repo);
5963 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5967 free(fileindex_path);
5968 free(tmp_branch_name);
5969 free(branch_ref_name);
5970 free(base_commit_ref_name);
5972 got_ref_close(wt_branch);
5975 got_ref_close(*branch_ref);
5979 got_ref_close(*tmp_branch);
5982 free(*base_commit_id);
5984 got_fileindex_free(*fileindex);
5987 lock_worktree(worktree, LOCK_SH);
5992 const struct got_error *
5993 got_worktree_histedit_postpone(struct got_worktree *worktree,
5994 struct got_fileindex *fileindex)
5997 got_fileindex_free(fileindex);
5998 return lock_worktree(worktree, LOCK_SH);
6001 const struct got_error *
6002 got_worktree_histedit_in_progress(int *in_progress,
6003 struct got_worktree *worktree)
6005 const struct got_error *err;
6006 char *tmp_branch_name = NULL;
6008 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6012 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6013 free(tmp_branch_name);
6017 const struct got_error *
6018 got_worktree_histedit_continue(struct got_object_id **commit_id,
6019 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6020 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6021 struct got_worktree *worktree, struct got_repository *repo)
6023 const struct got_error *err;
6024 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6025 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6026 struct got_reference *commit_ref = NULL;
6027 struct got_reference *base_commit_ref = NULL;
6028 char *fileindex_path = NULL;
6029 int have_staged_files = 0;
6033 *base_commit_id = NULL;
6036 err = lock_worktree(worktree, LOCK_EX);
6040 err = open_fileindex(fileindex, &fileindex_path, worktree);
6044 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6045 &have_staged_files);
6046 if (err && err->code != GOT_ERR_CANCELLED)
6048 if (have_staged_files) {
6049 err = got_error(GOT_ERR_STAGED_PATHS);
6053 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6057 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6061 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6065 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6070 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6074 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6077 err = got_ref_resolve(commit_id, repo, commit_ref);
6081 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6084 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6088 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6092 free(commit_ref_name);
6093 free(branch_ref_name);
6094 free(fileindex_path);
6096 got_ref_close(commit_ref);
6097 if (base_commit_ref)
6098 got_ref_close(base_commit_ref);
6102 free(*base_commit_id);
6103 *base_commit_id = NULL;
6105 got_ref_close(*tmp_branch);
6109 got_fileindex_free(*fileindex);
6112 lock_worktree(worktree, LOCK_EX);
6117 static const struct got_error *
6118 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6120 const struct got_error *err;
6121 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6122 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6124 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6127 err = delete_ref(tmp_branch_name, repo);
6131 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6135 err = delete_ref(base_commit_ref_name, repo);
6139 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6142 err = delete_ref(branch_ref_name, repo);
6146 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6149 err = delete_ref(commit_ref_name, repo);
6153 free(tmp_branch_name);
6154 free(base_commit_ref_name);
6155 free(branch_ref_name);
6156 free(commit_ref_name);
6160 const struct got_error *
6161 got_worktree_histedit_abort(struct got_worktree *worktree,
6162 struct got_fileindex *fileindex, struct got_repository *repo,
6163 struct got_reference *branch, struct got_object_id *base_commit_id,
6164 got_worktree_checkout_cb progress_cb, void *progress_arg)
6166 const struct got_error *err, *unlockerr, *sync_err;
6167 struct got_reference *resolved = NULL;
6168 char *fileindex_path = NULL;
6169 struct got_object_id *tree_id = NULL;
6170 struct revert_file_args rfa;
6172 err = lock_worktree(worktree, LOCK_EX);
6176 err = got_ref_open(&resolved, repo,
6177 got_ref_get_symref_target(branch), 0);
6181 err = got_worktree_set_head_ref(worktree, resolved);
6185 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6189 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6190 worktree->path_prefix);
6194 err = delete_histedit_refs(worktree, repo);
6198 err = get_fileindex_path(&fileindex_path, worktree);
6202 rfa.worktree = worktree;
6203 rfa.fileindex = fileindex;
6204 rfa.progress_cb = progress_cb;
6205 rfa.progress_arg = progress_arg;
6206 rfa.patch_cb = NULL;
6207 rfa.patch_arg = NULL;
6209 err = worktree_status(worktree, "", fileindex, repo,
6210 revert_file, &rfa, NULL, NULL, 0, 0);
6214 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6215 repo, progress_cb, progress_arg, NULL, NULL);
6217 sync_err = sync_fileindex(fileindex, fileindex_path);
6218 if (sync_err && err == NULL)
6221 got_ref_close(resolved);
6223 free(fileindex_path);
6225 unlockerr = lock_worktree(worktree, LOCK_SH);
6226 if (unlockerr && err == NULL)
6231 const struct got_error *
6232 got_worktree_histedit_complete(struct got_worktree *worktree,
6233 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6234 struct got_reference *edited_branch, struct got_repository *repo)
6236 const struct got_error *err, *unlockerr;
6237 struct got_object_id *new_head_commit_id = NULL;
6238 struct got_reference *resolved = NULL;
6240 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6244 err = got_ref_open(&resolved, repo,
6245 got_ref_get_symref_target(edited_branch), 0);
6249 err = got_ref_change_ref(resolved, new_head_commit_id);
6253 err = got_ref_write(resolved, repo);
6257 err = got_worktree_set_head_ref(worktree, resolved);
6261 err = delete_histedit_refs(worktree, repo);
6264 got_fileindex_free(fileindex);
6265 free(new_head_commit_id);
6266 unlockerr = lock_worktree(worktree, LOCK_SH);
6267 if (unlockerr && err == NULL)
6272 const struct got_error *
6273 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6274 struct got_object_id *commit_id, struct got_repository *repo)
6276 const struct got_error *err;
6277 char *commit_ref_name;
6279 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6283 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6287 err = delete_ref(commit_ref_name, repo);
6289 free(commit_ref_name);
6293 const struct got_error *
6294 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6295 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6296 struct got_worktree *worktree, const char *refname,
6297 struct got_repository *repo)
6299 const struct got_error *err = NULL;
6300 char *fileindex_path = NULL;
6301 struct check_rebase_ok_arg ok_arg;
6305 *base_branch_ref = NULL;
6307 err = lock_worktree(worktree, LOCK_EX);
6311 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6312 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6313 "cannot integrate a branch into itself; "
6314 "update -b or different branch name required");
6318 err = open_fileindex(fileindex, &fileindex_path, worktree);
6322 /* Preconditions are the same as for rebase. */
6323 ok_arg.worktree = worktree;
6325 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6330 err = got_ref_open(branch_ref, repo, refname, 1);
6334 err = got_ref_open(base_branch_ref, repo,
6335 got_worktree_get_head_ref_name(worktree), 1);
6339 got_ref_close(*branch_ref);
6342 if (*base_branch_ref) {
6343 got_ref_close(*base_branch_ref);
6344 *base_branch_ref = NULL;
6347 got_fileindex_free(*fileindex);
6350 lock_worktree(worktree, LOCK_SH);
6355 const struct got_error *
6356 got_worktree_integrate_continue(struct got_worktree *worktree,
6357 struct got_fileindex *fileindex, struct got_repository *repo,
6358 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6359 got_worktree_checkout_cb progress_cb, void *progress_arg,
6360 got_cancel_cb cancel_cb, void *cancel_arg)
6362 const struct got_error *err = NULL, *sync_err, *unlockerr;
6363 char *fileindex_path = NULL;
6364 struct got_object_id *tree_id = NULL, *commit_id = NULL;
6366 err = get_fileindex_path(&fileindex_path, worktree);
6370 err = got_ref_resolve(&commit_id, repo, branch_ref);
6374 err = got_object_id_by_path(&tree_id, repo, commit_id,
6375 worktree->path_prefix);
6379 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6383 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6384 progress_cb, progress_arg, cancel_cb, cancel_arg);
6388 err = got_ref_change_ref(base_branch_ref, commit_id);
6392 err = got_ref_write(base_branch_ref, repo);
6394 sync_err = sync_fileindex(fileindex, fileindex_path);
6395 if (sync_err && err == NULL)
6399 unlockerr = got_ref_unlock(branch_ref);
6400 if (unlockerr && err == NULL)
6402 got_ref_close(branch_ref);
6404 unlockerr = got_ref_unlock(base_branch_ref);
6405 if (unlockerr && err == NULL)
6407 got_ref_close(base_branch_ref);
6409 got_fileindex_free(fileindex);
6410 free(fileindex_path);
6413 unlockerr = lock_worktree(worktree, LOCK_SH);
6414 if (unlockerr && err == NULL)
6419 const struct got_error *
6420 got_worktree_integrate_abort(struct got_worktree *worktree,
6421 struct got_fileindex *fileindex, struct got_repository *repo,
6422 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
6424 const struct got_error *err = NULL, *unlockerr = NULL;
6426 got_fileindex_free(fileindex);
6428 err = lock_worktree(worktree, LOCK_SH);
6430 unlockerr = got_ref_unlock(branch_ref);
6431 if (unlockerr && err == NULL)
6433 got_ref_close(branch_ref);
6435 unlockerr = got_ref_unlock(base_branch_ref);
6436 if (unlockerr && err == NULL)
6438 got_ref_close(base_branch_ref);
6443 struct check_stage_ok_arg {
6444 struct got_object_id *head_commit_id;
6445 struct got_worktree *worktree;
6446 struct got_fileindex *fileindex;
6447 struct got_repository *repo;
6451 const struct got_error *
6452 check_stage_ok(void *arg, unsigned char status,
6453 unsigned char staged_status, const char *relpath,
6454 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6455 struct got_object_id *commit_id, int dirfd, const char *de_name)
6457 struct check_stage_ok_arg *a = arg;
6458 const struct got_error *err = NULL;
6459 struct got_fileindex_entry *ie;
6460 struct got_object_id base_commit_id;
6461 struct got_object_id *base_commit_idp = NULL;
6462 char *in_repo_path = NULL, *p;
6464 if (status == GOT_STATUS_UNVERSIONED ||
6465 status == GOT_STATUS_NO_CHANGE)
6467 if (status == GOT_STATUS_NONEXISTENT)
6468 return got_error_set_errno(ENOENT, relpath);
6470 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6472 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6474 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
6475 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
6477 return got_error_from_errno("asprintf");
6479 if (got_fileindex_entry_has_commit(ie)) {
6480 memcpy(base_commit_id.sha1, ie->commit_sha1,
6481 SHA1_DIGEST_LENGTH);
6482 base_commit_idp = &base_commit_id;
6485 if (status == GOT_STATUS_CONFLICT) {
6486 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
6488 } else if (status != GOT_STATUS_ADD &&
6489 status != GOT_STATUS_MODIFY &&
6490 status != GOT_STATUS_DELETE) {
6491 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
6495 a->have_changes = 1;
6500 err = check_out_of_date(p, status, staged_status,
6501 blob_id, base_commit_idp, a->head_commit_id, a->repo,
6502 GOT_ERR_STAGE_OUT_OF_DATE);
6508 struct stage_path_arg {
6509 struct got_worktree *worktree;
6510 struct got_fileindex *fileindex;
6511 struct got_repository *repo;
6512 got_worktree_status_cb status_cb;
6514 got_worktree_patch_cb patch_cb;
6516 int staged_something;
6519 static const struct got_error *
6520 stage_path(void *arg, unsigned char status,
6521 unsigned char staged_status, const char *relpath,
6522 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6523 struct got_object_id *commit_id, int dirfd, const char *de_name)
6525 struct stage_path_arg *a = arg;
6526 const struct got_error *err = NULL;
6527 struct got_fileindex_entry *ie;
6528 char *ondisk_path = NULL, *path_content = NULL;
6530 struct got_object_id *new_staged_blob_id = NULL;
6532 if (status == GOT_STATUS_UNVERSIONED)
6535 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6537 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6539 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
6541 return got_error_from_errno("asprintf");
6544 case GOT_STATUS_ADD:
6545 case GOT_STATUS_MODIFY:
6547 if (status == GOT_STATUS_ADD) {
6548 int choice = GOT_PATCH_CHOICE_NONE;
6549 err = (*a->patch_cb)(&choice, a->patch_arg,
6550 status, ie->path, NULL, 1, 1);
6553 if (choice != GOT_PATCH_CHOICE_YES)
6556 err = create_patched_content(&path_content, 0,
6557 staged_blob_id ? staged_blob_id : blob_id,
6558 ondisk_path, dirfd, de_name, ie->path,
6559 a->repo, a->patch_cb, a->patch_arg);
6560 if (err || path_content == NULL)
6564 err = got_object_blob_create(&new_staged_blob_id,
6565 path_content ? path_content : ondisk_path, a->repo);
6568 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
6569 SHA1_DIGEST_LENGTH);
6570 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
6571 stage = GOT_FILEIDX_STAGE_ADD;
6573 stage = GOT_FILEIDX_STAGE_MODIFY;
6574 got_fileindex_entry_stage_set(ie, stage);
6575 a->staged_something = 1;
6576 if (a->status_cb == NULL)
6578 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6579 get_staged_status(ie), relpath, blob_id,
6580 new_staged_blob_id, NULL, dirfd, de_name);
6582 case GOT_STATUS_DELETE:
6583 if (staged_status == GOT_STATUS_DELETE)
6586 int choice = GOT_PATCH_CHOICE_NONE;
6587 err = (*a->patch_cb)(&choice, a->patch_arg, status,
6588 ie->path, NULL, 1, 1);
6591 if (choice == GOT_PATCH_CHOICE_NO)
6593 if (choice != GOT_PATCH_CHOICE_YES) {
6594 err = got_error(GOT_ERR_PATCH_CHOICE);
6598 stage = GOT_FILEIDX_STAGE_DELETE;
6599 got_fileindex_entry_stage_set(ie, stage);
6600 a->staged_something = 1;
6601 if (a->status_cb == NULL)
6603 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6604 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
6607 case GOT_STATUS_NO_CHANGE:
6609 case GOT_STATUS_CONFLICT:
6610 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
6612 case GOT_STATUS_NONEXISTENT:
6613 err = got_error_set_errno(ENOENT, relpath);
6616 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
6620 if (path_content && unlink(path_content) == -1 && err == NULL)
6621 err = got_error_from_errno2("unlink", path_content);
6624 free(new_staged_blob_id);
6628 const struct got_error *
6629 got_worktree_stage(struct got_worktree *worktree,
6630 struct got_pathlist_head *paths,
6631 got_worktree_status_cb status_cb, void *status_arg,
6632 got_worktree_patch_cb patch_cb, void *patch_arg,
6633 struct got_repository *repo)
6635 const struct got_error *err = NULL, *sync_err, *unlockerr;
6636 struct got_pathlist_entry *pe;
6637 struct got_fileindex *fileindex = NULL;
6638 char *fileindex_path = NULL;
6639 struct got_reference *head_ref = NULL;
6640 struct got_object_id *head_commit_id = NULL;
6641 struct check_stage_ok_arg oka;
6642 struct stage_path_arg spa;
6644 err = lock_worktree(worktree, LOCK_EX);
6648 err = got_ref_open(&head_ref, repo,
6649 got_worktree_get_head_ref_name(worktree), 0);
6652 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6655 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6659 /* Check pre-conditions before staging anything. */
6660 oka.head_commit_id = head_commit_id;
6661 oka.worktree = worktree;
6662 oka.fileindex = fileindex;
6664 oka.have_changes = 0;
6665 TAILQ_FOREACH(pe, paths, entry) {
6666 err = worktree_status(worktree, pe->path, fileindex, repo,
6667 check_stage_ok, &oka, NULL, NULL, 0, 0);
6671 if (!oka.have_changes) {
6672 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6676 spa.worktree = worktree;
6677 spa.fileindex = fileindex;
6679 spa.patch_cb = patch_cb;
6680 spa.patch_arg = patch_arg;
6681 spa.status_cb = status_cb;
6682 spa.status_arg = status_arg;
6683 spa.staged_something = 0;
6684 TAILQ_FOREACH(pe, paths, entry) {
6685 err = worktree_status(worktree, pe->path, fileindex, repo,
6686 stage_path, &spa, NULL, NULL, 0, 0);
6690 if (!spa.staged_something) {
6691 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6695 sync_err = sync_fileindex(fileindex, fileindex_path);
6696 if (sync_err && err == NULL)
6700 got_ref_close(head_ref);
6701 free(head_commit_id);
6702 free(fileindex_path);
6704 got_fileindex_free(fileindex);
6705 unlockerr = lock_worktree(worktree, LOCK_SH);
6706 if (unlockerr && err == NULL)
6711 struct unstage_path_arg {
6712 struct got_worktree *worktree;
6713 struct got_fileindex *fileindex;
6714 struct got_repository *repo;
6715 got_worktree_checkout_cb progress_cb;
6717 got_worktree_patch_cb patch_cb;
6721 static const struct got_error *
6722 create_unstaged_content(char **path_unstaged_content,
6723 char **path_new_staged_content, struct got_object_id *blob_id,
6724 struct got_object_id *staged_blob_id, const char *relpath,
6725 struct got_repository *repo,
6726 got_worktree_patch_cb patch_cb, void *patch_arg)
6728 const struct got_error *err;
6729 struct got_blob_object *blob = NULL, *staged_blob = NULL;
6730 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
6731 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
6732 struct stat sb1, sb2;
6733 struct got_diff_changes *changes = NULL;
6734 struct got_diff_state *ds = NULL;
6735 struct got_diff_args *args = NULL;
6736 struct got_diff_change *change;
6737 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
6738 int have_content = 0, have_rejected_content = 0;
6740 *path_unstaged_content = NULL;
6741 *path_new_staged_content = NULL;
6743 err = got_object_id_str(&label1, blob_id);
6746 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
6750 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
6754 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
6758 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
6762 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
6766 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
6770 if (stat(path1, &sb1) == -1) {
6771 err = got_error_from_errno2("stat", path1);
6775 if (stat(path2, &sb2) == -1) {
6776 err = got_error_from_errno2("stat", path2);
6780 err = got_diff_files(&changes, &ds, &args, &diff_flags,
6781 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
6785 err = got_opentemp_named(path_unstaged_content, &outfile,
6786 "got-unstaged-content");
6789 err = got_opentemp_named(path_new_staged_content, &rejectfile,
6790 "got-new-staged-content");
6794 if (fseek(f1, 0L, SEEK_SET) == -1) {
6795 err = got_ferror(f1, GOT_ERR_IO);
6798 if (fseek(f2, 0L, SEEK_SET) == -1) {
6799 err = got_ferror(f2, GOT_ERR_IO);
6802 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
6804 err = apply_or_reject_change(&choice, change, ++n,
6805 changes->nchanges, ds, args, diff_flags, relpath,
6806 f1, f2, &line_cur1, &line_cur2,
6807 outfile, rejectfile, patch_cb, patch_arg);
6810 if (choice == GOT_PATCH_CHOICE_YES)
6813 have_rejected_content = 1;
6814 if (choice == GOT_PATCH_CHOICE_QUIT)
6817 if (have_content || have_rejected_content)
6818 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
6819 outfile, rejectfile);
6823 got_object_blob_close(blob);
6825 got_object_blob_close(staged_blob);
6826 if (f1 && fclose(f1) == EOF && err == NULL)
6827 err = got_error_from_errno2("fclose", path1);
6828 if (f2 && fclose(f2) == EOF && err == NULL)
6829 err = got_error_from_errno2("fclose", path2);
6830 if (outfile && fclose(outfile) == EOF && err == NULL)
6831 err = got_error_from_errno2("fclose", *path_unstaged_content);
6832 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
6833 err = got_error_from_errno2("fclose", *path_new_staged_content);
6834 if (path1 && unlink(path1) == -1 && err == NULL)
6835 err = got_error_from_errno2("unlink", path1);
6836 if (path2 && unlink(path2) == -1 && err == NULL)
6837 err = got_error_from_errno2("unlink", path2);
6838 if (err || !have_content) {
6839 if (*path_unstaged_content &&
6840 unlink(*path_unstaged_content) == -1 && err == NULL)
6841 err = got_error_from_errno2("unlink",
6842 *path_unstaged_content);
6843 free(*path_unstaged_content);
6844 *path_unstaged_content = NULL;
6846 if (err || !have_rejected_content) {
6847 if (*path_new_staged_content &&
6848 unlink(*path_new_staged_content) == -1 && err == NULL)
6849 err = got_error_from_errno2("unlink",
6850 *path_new_staged_content);
6851 free(*path_new_staged_content);
6852 *path_new_staged_content = NULL;
6856 got_diff_state_free(ds);
6860 got_diff_free_changes(changes);
6866 static const struct got_error *
6867 unstage_path(void *arg, unsigned char status,
6868 unsigned char staged_status, const char *relpath,
6869 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6870 struct got_object_id *commit_id, int dirfd, const char *de_name)
6872 const struct got_error *err = NULL;
6873 struct unstage_path_arg *a = arg;
6874 struct got_fileindex_entry *ie;
6875 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
6876 char *ondisk_path = NULL, *path_unstaged_content = NULL;
6877 char *path_new_staged_content = NULL;
6878 char *id_str = NULL, *label_orig = NULL;
6879 int local_changes_subsumed;
6882 if (staged_status != GOT_STATUS_ADD &&
6883 staged_status != GOT_STATUS_MODIFY &&
6884 staged_status != GOT_STATUS_DELETE)
6887 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6889 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6891 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
6893 return got_error_from_errno("asprintf");
6895 err = got_object_id_str(&id_str,
6896 commit_id ? commit_id : a->worktree->base_commit_id);
6899 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
6901 err = got_error_from_errno("asprintf");
6905 switch (staged_status) {
6906 case GOT_STATUS_MODIFY:
6907 err = got_object_open_as_blob(&blob_base, a->repo,
6912 case GOT_STATUS_ADD:
6914 if (staged_status == GOT_STATUS_ADD) {
6915 int choice = GOT_PATCH_CHOICE_NONE;
6916 err = (*a->patch_cb)(&choice, a->patch_arg,
6917 staged_status, ie->path, NULL, 1, 1);
6920 if (choice != GOT_PATCH_CHOICE_YES)
6923 err = create_unstaged_content(
6924 &path_unstaged_content,
6925 &path_new_staged_content, blob_id,
6926 staged_blob_id, ie->path, a->repo,
6927 a->patch_cb, a->patch_arg);
6928 if (err || path_unstaged_content == NULL)
6930 if (path_new_staged_content) {
6931 err = got_object_blob_create(
6933 path_new_staged_content,
6937 memcpy(ie->staged_blob_sha1,
6938 staged_blob_id->sha1,
6939 SHA1_DIGEST_LENGTH);
6941 err = merge_file(&local_changes_subsumed,
6942 a->worktree, blob_base, ondisk_path,
6943 relpath, got_fileindex_perms_to_st(ie),
6944 path_unstaged_content, label_orig,
6945 "unstaged", a->repo, a->progress_cb,
6948 path_new_staged_content == NULL)
6949 got_fileindex_entry_stage_set(ie,
6950 GOT_FILEIDX_STAGE_NONE);
6951 break; /* Done with this file. */
6954 err = got_object_open_as_blob(&blob_staged, a->repo,
6955 staged_blob_id, 8192);
6958 err = merge_blob(&local_changes_subsumed, a->worktree,
6959 blob_base, ondisk_path, relpath,
6960 got_fileindex_perms_to_st(ie), label_orig, blob_staged,
6961 commit_id ? commit_id : a->worktree->base_commit_id,
6962 a->repo, a->progress_cb, a->progress_arg);
6964 got_fileindex_entry_stage_set(ie,
6965 GOT_FILEIDX_STAGE_NONE);
6967 case GOT_STATUS_DELETE:
6969 int choice = GOT_PATCH_CHOICE_NONE;
6970 err = (*a->patch_cb)(&choice, a->patch_arg,
6971 staged_status, ie->path, NULL, 1, 1);
6974 if (choice == GOT_PATCH_CHOICE_NO)
6976 if (choice != GOT_PATCH_CHOICE_YES) {
6977 err = got_error(GOT_ERR_PATCH_CHOICE);
6981 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
6982 err = get_file_status(&status, &sb, ie, ondisk_path,
6983 dirfd, de_name, a->repo);
6986 err = (*a->progress_cb)(a->progress_arg, status, relpath);
6991 if (path_unstaged_content &&
6992 unlink(path_unstaged_content) == -1 && err == NULL)
6993 err = got_error_from_errno2("unlink", path_unstaged_content);
6994 if (path_new_staged_content &&
6995 unlink(path_new_staged_content) == -1 && err == NULL)
6996 err = got_error_from_errno2("unlink", path_new_staged_content);
6997 free(path_unstaged_content);
6998 free(path_new_staged_content);
7000 got_object_blob_close(blob_base);
7002 got_object_blob_close(blob_staged);
7008 const struct got_error *
7009 got_worktree_unstage(struct got_worktree *worktree,
7010 struct got_pathlist_head *paths,
7011 got_worktree_checkout_cb progress_cb, void *progress_arg,
7012 got_worktree_patch_cb patch_cb, void *patch_arg,
7013 struct got_repository *repo)
7015 const struct got_error *err = NULL, *sync_err, *unlockerr;
7016 struct got_pathlist_entry *pe;
7017 struct got_fileindex *fileindex = NULL;
7018 char *fileindex_path = NULL;
7019 struct unstage_path_arg upa;
7021 err = lock_worktree(worktree, LOCK_EX);
7025 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7029 upa.worktree = worktree;
7030 upa.fileindex = fileindex;
7032 upa.progress_cb = progress_cb;
7033 upa.progress_arg = progress_arg;
7034 upa.patch_cb = patch_cb;
7035 upa.patch_arg = patch_arg;
7036 TAILQ_FOREACH(pe, paths, entry) {
7037 err = worktree_status(worktree, pe->path, fileindex, repo,
7038 unstage_path, &upa, NULL, NULL, 0, 0);
7043 sync_err = sync_fileindex(fileindex, fileindex_path);
7044 if (sync_err && err == NULL)
7047 free(fileindex_path);
7049 got_fileindex_free(fileindex);
7050 unlockerr = lock_worktree(worktree, LOCK_SH);
7051 if (unlockerr && err == NULL)