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 = realpath(path, NULL);
380 if ((*worktree)->root_path == NULL) {
381 err = got_error_from_errno2("realpath", path);
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);
833 static const struct got_error *
834 update_symlink(const char *ondisk_path, const char *target_path,
837 /* This is not atomic but matches what 'ln -sf' does. */
838 if (unlink(ondisk_path) == -1)
839 return got_error_from_errno2("unlink", ondisk_path);
840 if (symlink(target_path, ondisk_path) == -1)
841 return got_error_from_errno3("symlink", target_path,
847 * Merge a symlink into the work tree, where blob_orig acts as the common
848 * ancestor, blob_deriv acts as the first derived version, and the symlink
849 * on disk acts as the second derived version.
850 * Assume that contents of both blobs represent symlinks.
852 static const struct got_error *
853 merge_symlink(struct got_worktree *worktree,
854 struct got_blob_object *blob_orig, const char *ondisk_path,
855 const char *path, uint16_t st_mode, const char *label_orig,
856 struct got_blob_object *blob_deriv,
857 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
858 got_worktree_checkout_cb progress_cb, void *progress_arg)
860 const struct got_error *err = NULL;
861 char *ancestor_target = NULL, *deriv_target = NULL;
864 char ondisk_target[PATH_MAX];
866 if (lstat(ondisk_path, &sb) == -1)
867 return got_error_from_errno2("lstat", ondisk_path);
869 if (!S_ISLNK(sb.st_mode)) {
870 /* TODO symlink is obstructed; do something */
871 return got_error_path(ondisk_path, GOT_ERR_FILE_OBSTRUCTED);
874 ondisk_len = readlink(ondisk_path, ondisk_target,
875 sizeof(ondisk_target));
876 if (ondisk_len == -1) {
877 err = got_error_from_errno2("readlink",
882 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
886 err = got_object_blob_read_to_str(&deriv_target, blob_deriv);
890 if (ondisk_len != strlen(ancestor_target) ||
891 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0) {
893 * The symlink has changed on-disk (second derived version).
894 * Keep that change and discard the incoming change (first
896 * TODO: Need tree-conflict resolution to handle this.
898 err = (*progress_cb)(progress_arg, GOT_STATUS_OBSTRUCTED,
900 } else if (ondisk_len == strlen(deriv_target) &&
901 memcmp(ondisk_target, deriv_target, ondisk_len) == 0) {
902 /* Both versions made the same change. */
903 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
905 /* Apply the incoming change. */
906 err = update_symlink(ondisk_path, deriv_target,
907 strlen(deriv_target));
910 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
913 free(ancestor_target);
919 * Perform a 3-way merge where blob_orig acts as the common ancestor,
920 * blob_deriv acts as the first derived version, and the file on disk
921 * acts as the second derived version.
923 static const struct got_error *
924 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
925 struct got_blob_object *blob_orig, const char *ondisk_path,
926 const char *path, uint16_t st_mode, const char *label_orig,
927 struct got_blob_object *blob_deriv,
928 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
929 got_worktree_checkout_cb progress_cb, void *progress_arg)
931 const struct got_error *err = NULL;
932 FILE *f_deriv = NULL;
933 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
934 char *label_deriv = NULL, *parent;
936 *local_changes_subsumed = 0;
938 parent = dirname(ondisk_path);
940 return got_error_from_errno2("dirname", ondisk_path);
943 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
944 err = got_error_from_errno("asprintf");
949 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
952 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
957 err = got_object_id_str(&id_str, deriv_base_commit_id);
960 if (asprintf(&label_deriv, "%s: commit %s",
961 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
962 err = got_error_from_errno("asprintf");
966 err = merge_file(local_changes_subsumed, worktree, blob_orig,
967 ondisk_path, path, st_mode, blob_deriv_path, label_orig,
968 label_deriv, repo, progress_cb, progress_arg);
970 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
971 err = got_error_from_errno("fclose");
973 if (blob_deriv_path) {
974 unlink(blob_deriv_path);
975 free(blob_deriv_path);
982 static const struct got_error *
983 create_fileindex_entry(struct got_fileindex *fileindex,
984 struct got_object_id *base_commit_id, const char *ondisk_path,
985 const char *path, struct got_object_id *blob_id)
987 const struct got_error *err = NULL;
988 struct got_fileindex_entry *new_ie;
990 err = got_fileindex_entry_alloc(&new_ie, path);
994 err = got_fileindex_entry_update(new_ie, ondisk_path,
995 blob_id->sha1, base_commit_id->sha1, 1);
999 err = got_fileindex_entry_add(fileindex, new_ie);
1002 got_fileindex_entry_free(new_ie);
1007 get_ondisk_perms(int executable, mode_t st_mode)
1009 mode_t xbits = S_IXUSR;
1012 /* Map read bits to execute bits. */
1013 if (st_mode & S_IRGRP)
1015 if (st_mode & S_IROTH)
1017 return st_mode | xbits;
1020 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1023 /* forward declaration */
1024 static const struct got_error *
1025 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1026 const char *path, mode_t te_mode, mode_t st_mode,
1027 struct got_blob_object *blob, int restoring_missing_file,
1028 int reverting_versioned_file, struct got_repository *repo,
1029 got_worktree_checkout_cb progress_cb, void *progress_arg);
1031 static const struct got_error *
1032 install_symlink(struct got_worktree *worktree, const char *ondisk_path,
1033 const char *path, mode_t te_mode, mode_t st_mode,
1034 struct got_blob_object *blob, int restoring_missing_file,
1035 int reverting_versioned_file, struct got_repository *repo,
1036 got_worktree_checkout_cb progress_cb, void *progress_arg)
1038 const struct got_error *err = NULL;
1039 char target_path[PATH_MAX];
1040 size_t len, target_len = 0;
1041 char *resolved_path = NULL, *abspath = NULL;
1042 char *path_got = NULL;
1043 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1044 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1047 * Blob object content specifies the target path of the link.
1048 * If a symbolic link cannot be installed we instead create
1049 * a regular file which contains the link target path stored
1050 * in the blob object.
1053 err = got_object_blob_read_block(&len, blob);
1054 if (len + target_len >= sizeof(target_path)) {
1055 /* Path too long; install as a regular file. */
1056 got_object_blob_rewind(blob);
1057 return install_blob(worktree, ondisk_path, path,
1058 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1059 restoring_missing_file, reverting_versioned_file,
1060 repo, progress_cb, progress_arg);
1063 /* Skip blob object header first time around. */
1064 memcpy(target_path + target_len, buf + hdrlen,
1066 target_len += len - hdrlen;
1070 target_path[target_len] = '\0';
1073 * Relative symlink target lookup should begin at the directory
1074 * in which the blob object is being installed.
1076 if (!got_path_is_absolute(target_path)) {
1077 char *parent = dirname(ondisk_path);
1078 if (parent == NULL) {
1079 err = got_error_from_errno2("dirname", ondisk_path);
1082 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1083 err = got_error_from_errno("asprintf");
1089 * unveil(2) restricts our view of paths in the filesystem.
1090 * ENOENT will occur if a link target path does not exist or
1091 * if it points outside our unveiled path space.
1093 resolved_path = realpath(abspath ? abspath : target_path, NULL);
1094 if (resolved_path == NULL) {
1095 if (errno != ENOENT) {
1096 err = got_error_from_errno2("realpath", target_path);
1101 /* Only allow symlinks pointing at paths within the work tree. */
1102 if (!got_path_is_child(resolved_path ? resolved_path : (abspath ?
1103 abspath : target_path), worktree->root_path,
1104 strlen(worktree->root_path))) {
1105 /* install as a regular file */
1106 got_object_blob_rewind(blob);
1107 err = install_blob(worktree, ondisk_path, path,
1108 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1109 restoring_missing_file, reverting_versioned_file,
1110 repo, progress_cb, progress_arg);
1114 /* Do not allow symlinks pointing into the .got directory. */
1115 if (asprintf(&path_got, "%s/%s", worktree->root_path,
1116 GOT_WORKTREE_GOT_DIR) == -1) {
1117 err = got_error_from_errno("asprintf");
1120 if (got_path_is_child(resolved_path ? resolved_path : (abspath ?
1121 abspath : target_path), path_got, strlen(path_got))) {
1122 /* install as a regular file */
1123 got_object_blob_rewind(blob);
1124 err = install_blob(worktree, ondisk_path, path,
1125 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1126 restoring_missing_file, reverting_versioned_file,
1127 repo, progress_cb, progress_arg);
1131 if (symlink(target_path, ondisk_path) == -1) {
1132 if (errno == EEXIST) {
1135 char etarget[PATH_MAX];
1136 if (lstat(ondisk_path, &sb) == -1) {
1137 err = got_error_from_errno2("lstat",
1141 if (!S_ISLNK(sb.st_mode)) {
1142 err = got_error_path(ondisk_path,
1143 GOT_ERR_FILE_OBSTRUCTED);
1146 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1148 err = got_error_from_errno2("readlink",
1152 if (elen == target_len &&
1153 memcmp(etarget, target_path, target_len) == 0) {
1154 err = NULL; /* nothing to do */
1157 err = update_symlink(ondisk_path, target_path,
1161 err = (*progress_cb)(progress_arg,
1162 GOT_STATUS_UPDATE, path);
1167 if (errno == ENOENT) {
1168 char *parent = dirname(ondisk_path);
1169 if (parent == NULL) {
1170 err = got_error_from_errno2("dirname",
1174 err = add_dir_on_disk(worktree, parent);
1178 * Retry, and fall through to error handling
1179 * below if this second attempt fails.
1181 if (symlink(target_path, ondisk_path) != -1) {
1182 err = NULL; /* success */
1187 /* Handle errors from first or second creation attempt. */
1188 if (errno == ENAMETOOLONG) {
1189 /* bad target path; install as a regular file */
1190 got_object_blob_rewind(blob);
1191 err = install_blob(worktree, ondisk_path, path,
1192 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1193 restoring_missing_file, reverting_versioned_file,
1194 repo, progress_cb, progress_arg);
1195 } else if (errno == ENOTDIR) {
1196 err = got_error_path(ondisk_path,
1197 GOT_ERR_FILE_OBSTRUCTED);
1199 err = got_error_from_errno3("symlink",
1200 target_path, ondisk_path);
1203 err = (*progress_cb)(progress_arg, GOT_STATUS_ADD, path);
1205 free(resolved_path);
1211 static const struct got_error *
1212 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1213 const char *path, mode_t te_mode, mode_t st_mode,
1214 struct got_blob_object *blob, int restoring_missing_file,
1215 int reverting_versioned_file, struct got_repository *repo,
1216 got_worktree_checkout_cb progress_cb, void *progress_arg)
1218 const struct got_error *err = NULL;
1222 char *tmppath = NULL;
1224 if (S_ISLNK(te_mode))
1225 return install_symlink(worktree, ondisk_path, path, te_mode,
1226 st_mode, blob, restoring_missing_file,
1227 reverting_versioned_file, repo, progress_cb, progress_arg);
1229 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1230 GOT_DEFAULT_FILE_MODE);
1232 if (errno == ENOENT) {
1233 char *parent = dirname(path);
1235 return got_error_from_errno2("dirname", path);
1236 err = add_dir_on_disk(worktree, parent);
1239 fd = open(ondisk_path,
1240 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1241 GOT_DEFAULT_FILE_MODE);
1243 return got_error_from_errno2("open",
1245 } else if (errno == EEXIST) {
1246 if (!S_ISREG(st_mode)) {
1247 /* TODO file is obstructed; do something */
1248 err = got_error_path(ondisk_path,
1249 GOT_ERR_FILE_OBSTRUCTED);
1252 err = got_opentemp_named_fd(&tmppath, &fd,
1259 return got_error_from_errno2("open", ondisk_path);
1262 if (restoring_missing_file)
1263 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
1264 else if (reverting_versioned_file)
1265 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
1267 err = (*progress_cb)(progress_arg,
1268 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1272 hdrlen = got_object_blob_get_hdrlen(blob);
1274 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1275 err = got_object_blob_read_block(&len, blob);
1279 /* Skip blob object header first time around. */
1280 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1282 err = got_error_from_errno("write");
1284 } else if (outlen != len - hdrlen) {
1285 err = got_error(GOT_ERR_IO);
1292 if (fsync(fd) != 0) {
1293 err = got_error_from_errno("fsync");
1298 if (rename(tmppath, ondisk_path) != 0) {
1299 err = got_error_from_errno3("rename", tmppath,
1306 if (chmod(ondisk_path,
1307 get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1308 err = got_error_from_errno2("chmod", ondisk_path);
1313 if (fd != -1 && close(fd) != 0 && err == NULL)
1314 err = got_error_from_errno("close");
1319 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1320 static const struct got_error *
1321 get_modified_file_content_status(unsigned char *status, FILE *f)
1323 const struct got_error *err = NULL;
1324 const char *markers[3] = {
1325 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1326 GOT_DIFF_CONFLICT_MARKER_SEP,
1327 GOT_DIFF_CONFLICT_MARKER_END
1332 const char delim[3] = {'\0', '\0', '\0'};
1334 while (*status == GOT_STATUS_MODIFY) {
1335 line = fparseln(f, &len, NULL, delim, 0);
1339 err = got_ferror(f, GOT_ERR_IO);
1343 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1344 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1346 *status = GOT_STATUS_CONFLICT;
1356 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1358 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1359 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1363 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1365 return !(ie->ctime_sec == sb->st_ctime &&
1366 ie->ctime_nsec == sb->st_ctimensec &&
1367 ie->mtime_sec == sb->st_mtime &&
1368 ie->mtime_nsec == sb->st_mtimensec &&
1369 ie->size == (sb->st_size & 0xffffffff) &&
1370 !xbit_differs(ie, sb->st_mode));
1373 static unsigned char
1374 get_staged_status(struct got_fileindex_entry *ie)
1376 switch (got_fileindex_entry_stage_get(ie)) {
1377 case GOT_FILEIDX_STAGE_ADD:
1378 return GOT_STATUS_ADD;
1379 case GOT_FILEIDX_STAGE_DELETE:
1380 return GOT_STATUS_DELETE;
1381 case GOT_FILEIDX_STAGE_MODIFY:
1382 return GOT_STATUS_MODIFY;
1384 return GOT_STATUS_NO_CHANGE;
1388 static const struct got_error *
1389 get_symlink_status(unsigned char *status, struct stat *sb,
1390 struct got_fileindex_entry *ie, const char *abspath,
1391 int dirfd, const char *de_name, struct got_blob_object *blob)
1393 const struct got_error *err = NULL;
1394 char target_path[PATH_MAX];
1395 char etarget[PATH_MAX];
1397 size_t len, target_len = 0;
1398 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1399 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1401 *status = GOT_STATUS_NO_CHANGE;
1403 /* Blob object content specifies the target path of the link. */
1405 err = got_object_blob_read_block(&len, blob);
1408 if (len + target_len >= sizeof(target_path)) {
1410 * Should not happen. The blob contents were OK
1411 * when this symlink was installed.
1413 return got_error(GOT_ERR_NO_SPACE);
1416 /* Skip blob object header first time around. */
1417 memcpy(target_path + target_len, buf + hdrlen,
1419 target_len += len - hdrlen;
1423 target_path[target_len] = '\0';
1426 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1428 return got_error_from_errno2("readlinkat", abspath);
1430 elen = readlink(abspath, etarget, sizeof(etarget));
1432 return got_error_from_errno2("readlinkat", abspath);
1435 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1436 *status = GOT_STATUS_MODIFY;
1441 static const struct got_error *
1442 get_file_status(unsigned char *status, struct stat *sb,
1443 struct got_fileindex_entry *ie, const char *abspath,
1444 int dirfd, const char *de_name, struct got_repository *repo)
1446 const struct got_error *err = NULL;
1447 struct got_object_id id;
1452 struct got_blob_object *blob = NULL;
1454 unsigned char staged_status = get_staged_status(ie);
1456 *status = GOT_STATUS_NO_CHANGE;
1459 * Whenever the caller provides a directory descriptor and a
1460 * directory entry name for the file, use them! This prevents
1461 * race conditions if filesystem paths change beneath our feet.
1464 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1465 if (errno == ENOENT) {
1466 if (got_fileindex_entry_has_file_on_disk(ie))
1467 *status = GOT_STATUS_MISSING;
1469 *status = GOT_STATUS_DELETE;
1472 err = got_error_from_errno2("fstatat", abspath);
1476 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1477 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1478 return got_error_from_errno2("open", abspath);
1479 else if (fd == -1 && errno == ELOOP) {
1480 if (lstat(abspath, sb) == -1)
1481 return got_error_from_errno2("lstat", abspath);
1482 } else if (fd == -1 || fstat(fd, sb) == -1) {
1483 if (errno == ENOENT) {
1484 if (got_fileindex_entry_has_file_on_disk(ie))
1485 *status = GOT_STATUS_MISSING;
1487 *status = GOT_STATUS_DELETE;
1490 err = got_error_from_errno2("fstat", abspath);
1495 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1496 *status = GOT_STATUS_OBSTRUCTED;
1500 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1501 *status = GOT_STATUS_DELETE;
1503 } else if (!got_fileindex_entry_has_blob(ie) &&
1504 staged_status != GOT_STATUS_ADD) {
1505 *status = GOT_STATUS_ADD;
1509 if (!stat_info_differs(ie, sb))
1512 if (staged_status == GOT_STATUS_MODIFY ||
1513 staged_status == GOT_STATUS_ADD)
1514 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1516 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1518 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1522 if (S_ISLNK(sb->st_mode)) {
1523 /* Staging changes to symlinks is not yet(?) supported. */
1524 if (staged_status != GOT_STATUS_NO_CHANGE) {
1525 err = got_error_path(abspath, GOT_ERR_FILE_STATUS);
1528 err = get_symlink_status(status, sb, ie, abspath, dirfd,
1535 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1537 err = got_error_from_errno2("openat", abspath);
1542 f = fdopen(fd, "r");
1544 err = got_error_from_errno2("fdopen", abspath);
1548 hdrlen = got_object_blob_get_hdrlen(blob);
1550 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1551 err = got_object_blob_read_block(&blen, blob);
1554 /* Skip length of blob object header first time around. */
1555 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1556 if (flen == 0 && ferror(f)) {
1557 err = got_error_from_errno("fread");
1562 *status = GOT_STATUS_MODIFY;
1564 } else if (flen == 0) {
1566 *status = GOT_STATUS_MODIFY;
1568 } else if (blen - hdrlen == flen) {
1569 /* Skip blob object header first time around. */
1570 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1571 *status = GOT_STATUS_MODIFY;
1575 *status = GOT_STATUS_MODIFY;
1581 if (*status == GOT_STATUS_MODIFY) {
1583 err = get_modified_file_content_status(status, f);
1584 } else if (xbit_differs(ie, sb->st_mode))
1585 *status = GOT_STATUS_MODE_CHANGE;
1588 got_object_blob_close(blob);
1589 if (f != NULL && fclose(f) == EOF && err == NULL)
1590 err = got_error_from_errno2("fclose", abspath);
1591 if (fd != -1 && close(fd) == -1 && err == NULL)
1592 err = got_error_from_errno2("close", abspath);
1597 * Update timestamps in the file index if a file is unmodified and
1598 * we had to run a full content comparison to find out.
1600 static const struct got_error *
1601 sync_timestamps(char *ondisk_path, unsigned char status,
1602 struct got_fileindex_entry *ie, struct stat *sb)
1604 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1605 return got_fileindex_entry_update(ie, ondisk_path,
1606 ie->blob_sha1, ie->commit_sha1, 1);
1611 static const struct got_error *
1612 update_blob(struct got_worktree *worktree,
1613 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1614 struct got_tree_entry *te, const char *path,
1615 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1618 const struct got_error *err = NULL;
1619 struct got_blob_object *blob = NULL;
1621 unsigned char status = GOT_STATUS_NO_CHANGE;
1624 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1625 return got_error_from_errno("asprintf");
1628 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1629 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1632 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1636 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1637 sb.st_mode = got_fileindex_perms_to_st(ie);
1639 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1641 if (status == GOT_STATUS_OBSTRUCTED) {
1642 err = (*progress_cb)(progress_arg, status, path);
1645 if (status == GOT_STATUS_CONFLICT) {
1646 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1651 if (ie && status != GOT_STATUS_MISSING &&
1652 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1653 if (got_fileindex_entry_has_commit(ie) &&
1654 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1655 SHA1_DIGEST_LENGTH) == 0) {
1656 err = sync_timestamps(ondisk_path, status, ie, &sb);
1659 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1663 if (got_fileindex_entry_has_blob(ie) &&
1664 memcmp(ie->blob_sha1, te->id.sha1,
1665 SHA1_DIGEST_LENGTH) == 0) {
1666 err = sync_timestamps(ondisk_path, status, ie, &sb);
1671 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1675 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1676 int update_timestamps;
1677 struct got_blob_object *blob2 = NULL;
1678 char *label_orig = NULL;
1679 if (got_fileindex_entry_has_blob(ie)) {
1680 struct got_object_id id2;
1681 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1682 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1686 if (got_fileindex_entry_has_commit(ie)) {
1687 char id_str[SHA1_DIGEST_STRING_LENGTH];
1688 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1689 sizeof(id_str)) == NULL) {
1690 err = got_error_path(id_str,
1691 GOT_ERR_BAD_OBJ_ID_STR);
1694 if (asprintf(&label_orig, "%s: commit %s",
1695 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1696 err = got_error_from_errno("asprintf");
1700 err = merge_blob(&update_timestamps, worktree, blob2,
1701 ondisk_path, path, sb.st_mode, label_orig, blob,
1702 worktree->base_commit_id, repo,
1703 progress_cb, progress_arg);
1706 got_object_blob_close(blob2);
1710 * Do not update timestamps of files with local changes.
1711 * Otherwise, a future status walk would treat them as
1712 * unmodified files again.
1714 err = got_fileindex_entry_update(ie, ondisk_path,
1715 blob->id.sha1, worktree->base_commit_id->sha1,
1717 } else if (status == GOT_STATUS_MODE_CHANGE) {
1718 err = got_fileindex_entry_update(ie, ondisk_path,
1719 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1720 } else if (status == GOT_STATUS_DELETE) {
1721 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1724 err = got_fileindex_entry_update(ie, ondisk_path,
1725 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1729 err = install_blob(worktree, ondisk_path, path, te->mode,
1730 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1731 repo, progress_cb, progress_arg);
1735 err = got_fileindex_entry_update(ie, ondisk_path,
1736 blob->id.sha1, worktree->base_commit_id->sha1, 1);
1738 err = create_fileindex_entry(fileindex,
1739 worktree->base_commit_id, ondisk_path, path,
1745 got_object_blob_close(blob);
1751 static const struct got_error *
1752 remove_ondisk_file(const char *root_path, const char *path)
1754 const struct got_error *err = NULL;
1755 char *ondisk_path = NULL;
1757 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1758 return got_error_from_errno("asprintf");
1760 if (unlink(ondisk_path) == -1) {
1761 if (errno != ENOENT)
1762 err = got_error_from_errno2("unlink", ondisk_path);
1764 char *parent = dirname(ondisk_path);
1765 while (parent && strcmp(parent, root_path) != 0) {
1766 if (rmdir(parent) == -1) {
1767 if (errno != ENOTEMPTY)
1768 err = got_error_from_errno2("rmdir",
1772 parent = dirname(parent);
1779 static const struct got_error *
1780 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1781 struct got_fileindex_entry *ie, struct got_repository *repo,
1782 got_worktree_checkout_cb progress_cb, void *progress_arg)
1784 const struct got_error *err = NULL;
1785 unsigned char status;
1789 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1790 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1792 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1794 return got_error_from_errno("asprintf");
1796 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
1800 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1801 status == GOT_STATUS_ADD) {
1802 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1806 * Preserve the working file and change the deleted blob's
1807 * entry into a schedule-add entry.
1809 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1812 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1815 if (status == GOT_STATUS_NO_CHANGE) {
1816 err = remove_ondisk_file(worktree->root_path, ie->path);
1820 got_fileindex_entry_remove(fileindex, ie);
1827 struct diff_cb_arg {
1828 struct got_fileindex *fileindex;
1829 struct got_worktree *worktree;
1830 struct got_repository *repo;
1831 got_worktree_checkout_cb progress_cb;
1833 got_cancel_cb cancel_cb;
1837 static const struct got_error *
1838 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1839 struct got_tree_entry *te, const char *parent_path)
1841 struct diff_cb_arg *a = arg;
1843 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1844 return got_error(GOT_ERR_CANCELLED);
1846 return update_blob(a->worktree, a->fileindex, ie, te,
1847 ie->path, a->repo, a->progress_cb, a->progress_arg);
1850 static const struct got_error *
1851 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1853 struct diff_cb_arg *a = arg;
1855 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1856 return got_error(GOT_ERR_CANCELLED);
1858 return delete_blob(a->worktree, a->fileindex, ie,
1859 a->repo, a->progress_cb, a->progress_arg);
1862 static const struct got_error *
1863 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1865 struct diff_cb_arg *a = arg;
1866 const struct got_error *err;
1869 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1870 return got_error(GOT_ERR_CANCELLED);
1872 if (got_object_tree_entry_is_submodule(te))
1875 if (asprintf(&path, "%s%s%s", parent_path,
1876 parent_path[0] ? "/" : "", te->name)
1878 return got_error_from_errno("asprintf");
1880 if (S_ISDIR(te->mode))
1881 err = add_dir_on_disk(a->worktree, path);
1883 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1884 a->repo, a->progress_cb, a->progress_arg);
1890 static const struct got_error *
1891 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1893 const struct got_error *err = NULL;
1894 char *uuidstr = NULL;
1895 uint32_t uuid_status;
1899 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1900 if (uuid_status != uuid_s_ok)
1901 return got_error_uuid(uuid_status, "uuid_to_string");
1903 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1905 err = got_error_from_errno("asprintf");
1912 const struct got_error *
1913 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1915 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1918 static const struct got_error *
1919 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1921 return get_ref_name(refname, worktree,
1922 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1925 static const struct got_error *
1926 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1928 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1931 static const struct got_error *
1932 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1934 return get_ref_name(refname, worktree,
1935 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1938 static const struct got_error *
1939 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1941 return get_ref_name(refname, worktree,
1942 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1945 static const struct got_error *
1946 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1948 return get_ref_name(refname, worktree,
1949 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1952 static const struct got_error *
1953 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1955 return get_ref_name(refname, worktree,
1956 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1959 static const struct got_error *
1960 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1962 return get_ref_name(refname, worktree,
1963 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1966 static const struct got_error *
1967 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1969 return get_ref_name(refname, worktree,
1970 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1973 const struct got_error *
1974 got_worktree_get_histedit_script_path(char **path,
1975 struct got_worktree *worktree)
1977 if (asprintf(path, "%s/%s/%s", worktree->root_path,
1978 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
1980 return got_error_from_errno("asprintf");
1986 * Prevent Git's garbage collector from deleting our base commit by
1987 * setting a reference to our base commit's ID.
1989 static const struct got_error *
1990 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1992 const struct got_error *err = NULL;
1993 struct got_reference *ref = NULL;
1996 err = got_worktree_get_base_ref_name(&refname, worktree);
2000 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2004 err = got_ref_write(ref, repo);
2012 static const struct got_error *
2013 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2015 const struct got_error *err = NULL;
2017 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2018 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2019 err = got_error_from_errno("asprintf");
2020 *fileindex_path = NULL;
2026 static const struct got_error *
2027 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2028 struct got_worktree *worktree)
2030 const struct got_error *err = NULL;
2033 *fileindex_path = NULL;
2034 *fileindex = got_fileindex_alloc();
2035 if (*fileindex == NULL)
2036 return got_error_from_errno("got_fileindex_alloc");
2038 err = get_fileindex_path(fileindex_path, worktree);
2042 index = fopen(*fileindex_path, "rb");
2043 if (index == NULL) {
2044 if (errno != ENOENT)
2045 err = got_error_from_errno2("fopen", *fileindex_path);
2047 err = got_fileindex_read(*fileindex, index);
2048 if (fclose(index) != 0 && err == NULL)
2049 err = got_error_from_errno("fclose");
2053 free(*fileindex_path);
2054 *fileindex_path = NULL;
2055 got_fileindex_free(*fileindex);
2061 struct bump_base_commit_id_arg {
2062 struct got_object_id *base_commit_id;
2065 const char *entry_name;
2066 got_worktree_checkout_cb progress_cb;
2070 /* Bump base commit ID of all files within an updated part of the work tree. */
2071 static const struct got_error *
2072 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2074 const struct got_error *err;
2075 struct bump_base_commit_id_arg *a = arg;
2077 if (a->entry_name) {
2078 if (strcmp(ie->path, a->path) != 0)
2080 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2083 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2084 SHA1_DIGEST_LENGTH) == 0)
2087 if (a->progress_cb) {
2088 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2093 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2097 static const struct got_error *
2098 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2100 const struct got_error *err = NULL;
2101 char *new_fileindex_path = NULL;
2102 FILE *new_index = NULL;
2103 struct timespec timeout;
2105 err = got_opentemp_named(&new_fileindex_path, &new_index,
2110 err = got_fileindex_write(fileindex, new_index);
2114 if (rename(new_fileindex_path, fileindex_path) != 0) {
2115 err = got_error_from_errno3("rename", new_fileindex_path,
2117 unlink(new_fileindex_path);
2121 * Sleep for a short amount of time to ensure that files modified after
2122 * this program exits have a different time stamp from the one which
2123 * was recorded in the file index.
2126 timeout.tv_nsec = 1;
2127 nanosleep(&timeout, NULL);
2131 free(new_fileindex_path);
2135 static const struct got_error *
2136 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2137 struct got_object_id **tree_id, const char *wt_relpath,
2138 struct got_worktree *worktree, struct got_repository *repo)
2140 const struct got_error *err = NULL;
2141 struct got_object_id *id = NULL;
2142 char *in_repo_path = NULL;
2143 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2145 *entry_type = GOT_OBJ_TYPE_ANY;
2146 *tree_relpath = NULL;
2149 if (wt_relpath[0] == '\0') {
2150 /* Check out all files within the work tree. */
2151 *entry_type = GOT_OBJ_TYPE_TREE;
2152 *tree_relpath = strdup("");
2153 if (*tree_relpath == NULL) {
2154 err = got_error_from_errno("strdup");
2157 err = got_object_id_by_path(tree_id, repo,
2158 worktree->base_commit_id, worktree->path_prefix);
2164 /* Check out a subset of files in the work tree. */
2166 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2167 is_root_wt ? "" : "/", wt_relpath) == -1) {
2168 err = got_error_from_errno("asprintf");
2172 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2178 in_repo_path = NULL;
2180 err = got_object_get_type(entry_type, repo, id);
2184 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2185 /* Check out a single file. */
2186 if (strchr(wt_relpath, '/') == NULL) {
2187 /* Check out a single file in work tree's root dir. */
2188 in_repo_path = strdup(worktree->path_prefix);
2189 if (in_repo_path == NULL) {
2190 err = got_error_from_errno("strdup");
2193 *tree_relpath = strdup("");
2194 if (*tree_relpath == NULL) {
2195 err = got_error_from_errno("strdup");
2199 /* Check out a single file in a subdirectory. */
2200 err = got_path_dirname(tree_relpath, wt_relpath);
2203 if (asprintf(&in_repo_path, "%s%s%s",
2204 worktree->path_prefix, is_root_wt ? "" : "/",
2205 *tree_relpath) == -1) {
2206 err = got_error_from_errno("asprintf");
2210 err = got_object_id_by_path(tree_id, repo,
2211 worktree->base_commit_id, in_repo_path);
2213 /* Check out all files within a subdirectory. */
2214 *tree_id = got_object_id_dup(id);
2215 if (*tree_id == NULL) {
2216 err = got_error_from_errno("got_object_id_dup");
2219 *tree_relpath = strdup(wt_relpath);
2220 if (*tree_relpath == NULL) {
2221 err = got_error_from_errno("strdup");
2229 *entry_type = GOT_OBJ_TYPE_ANY;
2230 free(*tree_relpath);
2231 *tree_relpath = NULL;
2238 static const struct got_error *
2239 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2240 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2241 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2242 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2244 const struct got_error *err = NULL;
2245 struct got_commit_object *commit = NULL;
2246 struct got_tree_object *tree = NULL;
2247 struct got_fileindex_diff_tree_cb diff_cb;
2248 struct diff_cb_arg arg;
2250 err = ref_base_commit(worktree, repo);
2252 if (!(err->code == GOT_ERR_ERRNO &&
2253 (errno == EACCES || errno == EROFS)))
2255 err = (*progress_cb)(progress_arg,
2256 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2261 err = got_object_open_as_commit(&commit, repo,
2262 worktree->base_commit_id);
2266 err = got_object_open_as_tree(&tree, repo, tree_id);
2271 got_object_tree_find_entry(tree, entry_name) == NULL) {
2272 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2276 diff_cb.diff_old_new = diff_old_new;
2277 diff_cb.diff_old = diff_old;
2278 diff_cb.diff_new = diff_new;
2279 arg.fileindex = fileindex;
2280 arg.worktree = worktree;
2282 arg.progress_cb = progress_cb;
2283 arg.progress_arg = progress_arg;
2284 arg.cancel_cb = cancel_cb;
2285 arg.cancel_arg = cancel_arg;
2286 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2287 entry_name, repo, &diff_cb, &arg);
2290 got_object_tree_close(tree);
2292 got_object_commit_close(commit);
2296 const struct got_error *
2297 got_worktree_checkout_files(struct got_worktree *worktree,
2298 struct got_pathlist_head *paths, struct got_repository *repo,
2299 got_worktree_checkout_cb progress_cb, void *progress_arg,
2300 got_cancel_cb cancel_cb, void *cancel_arg)
2302 const struct got_error *err = NULL, *sync_err, *unlockerr;
2303 struct got_commit_object *commit = NULL;
2304 struct got_tree_object *tree = NULL;
2305 struct got_fileindex *fileindex = NULL;
2306 char *fileindex_path = NULL;
2307 struct got_pathlist_entry *pe;
2308 struct tree_path_data {
2309 SIMPLEQ_ENTRY(tree_path_data) entry;
2310 struct got_object_id *tree_id;
2315 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2317 SIMPLEQ_INIT(&tree_paths);
2319 err = lock_worktree(worktree, LOCK_EX);
2323 /* Map all specified paths to in-repository trees. */
2324 TAILQ_FOREACH(pe, paths, entry) {
2325 tpd = malloc(sizeof(*tpd));
2327 err = got_error_from_errno("malloc");
2331 err = find_tree_entry_for_checkout(&tpd->entry_type,
2332 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2338 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2339 err = got_path_basename(&tpd->entry_name, pe->path);
2347 tpd->entry_name = NULL;
2349 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2353 * Read the file index.
2354 * Checking out files is supposed to be an idempotent operation.
2355 * If the on-disk file index is incomplete we will try to complete it.
2357 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2361 tpd = SIMPLEQ_FIRST(&tree_paths);
2362 TAILQ_FOREACH(pe, paths, entry) {
2363 struct bump_base_commit_id_arg bbc_arg;
2365 err = checkout_files(worktree, fileindex, tpd->relpath,
2366 tpd->tree_id, tpd->entry_name, repo,
2367 progress_cb, progress_arg, cancel_cb, cancel_arg);
2371 bbc_arg.base_commit_id = worktree->base_commit_id;
2372 bbc_arg.entry_name = tpd->entry_name;
2373 bbc_arg.path = pe->path;
2374 bbc_arg.path_len = pe->path_len;
2375 bbc_arg.progress_cb = progress_cb;
2376 bbc_arg.progress_arg = progress_arg;
2377 err = got_fileindex_for_each_entry_safe(fileindex,
2378 bump_base_commit_id, &bbc_arg);
2382 tpd = SIMPLEQ_NEXT(tpd, entry);
2384 sync_err = sync_fileindex(fileindex, fileindex_path);
2385 if (sync_err && err == NULL)
2388 free(fileindex_path);
2390 got_object_tree_close(tree);
2392 got_object_commit_close(commit);
2394 got_fileindex_free(fileindex);
2395 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2396 tpd = SIMPLEQ_FIRST(&tree_paths);
2397 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2402 unlockerr = lock_worktree(worktree, LOCK_SH);
2403 if (unlockerr && err == NULL)
2408 struct merge_file_cb_arg {
2409 struct got_worktree *worktree;
2410 struct got_fileindex *fileindex;
2411 got_worktree_checkout_cb progress_cb;
2413 got_cancel_cb cancel_cb;
2415 const char *label_orig;
2416 struct got_object_id *commit_id2;
2419 static const struct got_error *
2420 merge_file_cb(void *arg, struct got_blob_object *blob1,
2421 struct got_blob_object *blob2, struct got_object_id *id1,
2422 struct got_object_id *id2, const char *path1, const char *path2,
2423 mode_t mode1, mode_t mode2, struct got_repository *repo)
2425 static const struct got_error *err = NULL;
2426 struct merge_file_cb_arg *a = arg;
2427 struct got_fileindex_entry *ie;
2428 char *ondisk_path = NULL;
2430 unsigned char status;
2431 int local_changes_subsumed;
2433 if (blob1 && blob2) {
2434 ie = got_fileindex_entry_get(a->fileindex, path2,
2437 return (*a->progress_cb)(a->progress_arg,
2438 GOT_STATUS_MISSING, path2);
2440 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2442 return got_error_from_errno("asprintf");
2444 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2449 if (status == GOT_STATUS_DELETE) {
2450 err = (*a->progress_cb)(a->progress_arg,
2451 GOT_STATUS_MERGE, path2);
2454 if (status != GOT_STATUS_NO_CHANGE &&
2455 status != GOT_STATUS_MODIFY &&
2456 status != GOT_STATUS_CONFLICT &&
2457 status != GOT_STATUS_ADD) {
2458 err = (*a->progress_cb)(a->progress_arg, status, path2);
2462 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2463 err = merge_symlink(a->worktree, blob1,
2464 ondisk_path, path2, sb.st_mode, a->label_orig,
2465 blob2, a->commit_id2, repo, a->progress_cb,
2468 err = merge_blob(&local_changes_subsumed, a->worktree,
2469 blob1, ondisk_path, path2, sb.st_mode,
2470 a->label_orig, blob2, a->commit_id2, repo,
2471 a->progress_cb, a->progress_arg);
2474 ie = got_fileindex_entry_get(a->fileindex, path1,
2477 return (*a->progress_cb)(a->progress_arg,
2478 GOT_STATUS_MISSING, path1);
2480 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2482 return got_error_from_errno("asprintf");
2484 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2490 case GOT_STATUS_NO_CHANGE:
2491 err = (*a->progress_cb)(a->progress_arg,
2492 GOT_STATUS_DELETE, path1);
2495 err = remove_ondisk_file(a->worktree->root_path, path1);
2499 got_fileindex_entry_mark_deleted_from_disk(ie);
2501 case GOT_STATUS_DELETE:
2502 case GOT_STATUS_MISSING:
2503 err = (*a->progress_cb)(a->progress_arg,
2504 GOT_STATUS_DELETE, path1);
2508 got_fileindex_entry_mark_deleted_from_disk(ie);
2510 case GOT_STATUS_ADD:
2511 case GOT_STATUS_MODIFY:
2512 case GOT_STATUS_CONFLICT:
2513 err = (*a->progress_cb)(a->progress_arg,
2514 GOT_STATUS_CANNOT_DELETE, path1);
2518 case GOT_STATUS_OBSTRUCTED:
2519 err = (*a->progress_cb)(a->progress_arg, status, path1);
2527 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2529 return got_error_from_errno("asprintf");
2530 ie = got_fileindex_entry_get(a->fileindex, path2,
2533 err = get_file_status(&status, &sb, ie, ondisk_path,
2537 if (status != GOT_STATUS_NO_CHANGE &&
2538 status != GOT_STATUS_MODIFY &&
2539 status != GOT_STATUS_CONFLICT &&
2540 status != GOT_STATUS_ADD) {
2541 err = (*a->progress_cb)(a->progress_arg,
2545 err = merge_blob(&local_changes_subsumed, a->worktree,
2546 NULL, ondisk_path, path2, sb.st_mode,
2547 a->label_orig, blob2, a->commit_id2, repo,
2550 if (status == GOT_STATUS_DELETE) {
2551 err = got_fileindex_entry_update(ie,
2552 ondisk_path, blob2->id.sha1,
2553 a->worktree->base_commit_id->sha1, 0);
2558 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2559 err = install_blob(a->worktree, ondisk_path, path2,
2560 /* XXX get this from parent tree! */
2561 GOT_DEFAULT_FILE_MODE,
2562 sb.st_mode, blob2, 0, 0, repo,
2563 a->progress_cb, a->progress_arg);
2566 err = got_fileindex_entry_alloc(&ie, path2);
2569 err = got_fileindex_entry_update(ie, ondisk_path,
2572 got_fileindex_entry_free(ie);
2575 err = got_fileindex_entry_add(a->fileindex, ie);
2577 got_fileindex_entry_free(ie);
2587 struct check_merge_ok_arg {
2588 struct got_worktree *worktree;
2589 struct got_repository *repo;
2592 static const struct got_error *
2593 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2595 const struct got_error *err = NULL;
2596 struct check_merge_ok_arg *a = arg;
2597 unsigned char status;
2601 /* Reject merges into a work tree with mixed base commits. */
2602 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2603 SHA1_DIGEST_LENGTH))
2604 return got_error(GOT_ERR_MIXED_COMMITS);
2606 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2608 return got_error_from_errno("asprintf");
2610 /* Reject merges into a work tree with conflicted files. */
2611 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2614 if (status == GOT_STATUS_CONFLICT)
2615 return got_error(GOT_ERR_CONFLICTS);
2620 static const struct got_error *
2621 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2622 const char *fileindex_path, struct got_object_id *commit_id1,
2623 struct got_object_id *commit_id2, struct got_repository *repo,
2624 got_worktree_checkout_cb progress_cb, void *progress_arg,
2625 got_cancel_cb cancel_cb, void *cancel_arg)
2627 const struct got_error *err = NULL, *sync_err;
2628 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2629 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2630 struct merge_file_cb_arg arg;
2631 char *label_orig = NULL;
2636 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2637 worktree->path_prefix);
2641 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2645 err = got_object_id_str(&id_str, commit_id1);
2649 if (asprintf(&label_orig, "%s: commit %s",
2650 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2651 err = got_error_from_errno("asprintf");
2658 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2659 worktree->path_prefix);
2663 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2667 arg.worktree = worktree;
2668 arg.fileindex = fileindex;
2669 arg.progress_cb = progress_cb;
2670 arg.progress_arg = progress_arg;
2671 arg.cancel_cb = cancel_cb;
2672 arg.cancel_arg = cancel_arg;
2673 arg.label_orig = label_orig;
2674 arg.commit_id2 = commit_id2;
2675 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2676 sync_err = sync_fileindex(fileindex, fileindex_path);
2677 if (sync_err && err == NULL)
2681 got_object_tree_close(tree1);
2683 got_object_tree_close(tree2);
2688 const struct got_error *
2689 got_worktree_merge_files(struct got_worktree *worktree,
2690 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2691 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2692 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2694 const struct got_error *err, *unlockerr;
2695 char *fileindex_path = NULL;
2696 struct got_fileindex *fileindex = NULL;
2697 struct check_merge_ok_arg mok_arg;
2699 err = lock_worktree(worktree, LOCK_EX);
2703 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2707 mok_arg.worktree = worktree;
2708 mok_arg.repo = repo;
2709 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2714 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2715 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2718 got_fileindex_free(fileindex);
2719 free(fileindex_path);
2720 unlockerr = lock_worktree(worktree, LOCK_SH);
2721 if (unlockerr && err == NULL)
2726 struct diff_dir_cb_arg {
2727 struct got_fileindex *fileindex;
2728 struct got_worktree *worktree;
2729 const char *status_path;
2730 size_t status_path_len;
2731 struct got_repository *repo;
2732 got_worktree_status_cb status_cb;
2734 got_cancel_cb cancel_cb;
2736 /* A pathlist containing per-directory pathlists of ignore patterns. */
2737 struct got_pathlist_head ignores;
2738 int report_unchanged;
2742 static const struct got_error *
2743 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2744 int dirfd, const char *de_name,
2745 got_worktree_status_cb status_cb, void *status_arg,
2746 struct got_repository *repo, int report_unchanged)
2748 const struct got_error *err = NULL;
2749 unsigned char status = GOT_STATUS_NO_CHANGE;
2750 unsigned char staged_status = get_staged_status(ie);
2752 struct got_object_id blob_id, commit_id, staged_blob_id;
2753 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2754 struct got_object_id *staged_blob_idp = NULL;
2756 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
2760 if (status == GOT_STATUS_NO_CHANGE &&
2761 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
2764 if (got_fileindex_entry_has_blob(ie)) {
2765 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2766 blob_idp = &blob_id;
2768 if (got_fileindex_entry_has_commit(ie)) {
2769 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2770 commit_idp = &commit_id;
2772 if (staged_status == GOT_STATUS_ADD ||
2773 staged_status == GOT_STATUS_MODIFY) {
2774 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2775 SHA1_DIGEST_LENGTH);
2776 staged_blob_idp = &staged_blob_id;
2779 return (*status_cb)(status_arg, status, staged_status,
2780 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
2783 static const struct got_error *
2784 status_old_new(void *arg, struct got_fileindex_entry *ie,
2785 struct dirent *de, const char *parent_path, int dirfd)
2787 const struct got_error *err = NULL;
2788 struct diff_dir_cb_arg *a = arg;
2791 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2792 return got_error(GOT_ERR_CANCELLED);
2794 if (got_path_cmp(parent_path, a->status_path,
2795 strlen(parent_path), a->status_path_len) != 0 &&
2796 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2799 if (parent_path[0]) {
2800 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2801 parent_path, de->d_name) == -1)
2802 return got_error_from_errno("asprintf");
2804 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2806 return got_error_from_errno("asprintf");
2809 err = report_file_status(ie, abspath, dirfd, de->d_name,
2810 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
2815 static const struct got_error *
2816 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2818 struct diff_dir_cb_arg *a = arg;
2819 struct got_object_id blob_id, commit_id;
2820 unsigned char status;
2822 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2823 return got_error(GOT_ERR_CANCELLED);
2825 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2828 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2829 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2830 if (got_fileindex_entry_has_file_on_disk(ie))
2831 status = GOT_STATUS_MISSING;
2833 status = GOT_STATUS_DELETE;
2834 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2835 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
2839 free_ignorelist(struct got_pathlist_head *ignorelist)
2841 struct got_pathlist_entry *pe;
2843 TAILQ_FOREACH(pe, ignorelist, entry)
2844 free((char *)pe->path);
2845 got_pathlist_free(ignorelist);
2849 free_ignores(struct got_pathlist_head *ignores)
2851 struct got_pathlist_entry *pe;
2853 TAILQ_FOREACH(pe, ignores, entry) {
2854 struct got_pathlist_head *ignorelist = pe->data;
2855 free_ignorelist(ignorelist);
2856 free((char *)pe->path);
2858 got_pathlist_free(ignores);
2861 static const struct got_error *
2862 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
2864 const struct got_error *err = NULL;
2865 struct got_pathlist_entry *pe = NULL;
2866 struct got_pathlist_head *ignorelist;
2867 char *line = NULL, *pattern, *dirpath = NULL;
2868 size_t linesize = 0;
2871 ignorelist = calloc(1, sizeof(*ignorelist));
2872 if (ignorelist == NULL)
2873 return got_error_from_errno("calloc");
2874 TAILQ_INIT(ignorelist);
2876 while ((linelen = getline(&line, &linesize, f)) != -1) {
2877 if (linelen > 0 && line[linelen - 1] == '\n')
2878 line[linelen - 1] = '\0';
2880 /* Git's ignores may contain comments. */
2884 /* Git's negated patterns are not (yet?) supported. */
2888 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
2890 err = got_error_from_errno("asprintf");
2893 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
2898 err = got_error_from_errno("getline");
2902 dirpath = strdup(path);
2903 if (dirpath == NULL) {
2904 err = got_error_from_errno("strdup");
2907 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
2910 if (err || pe == NULL) {
2912 free_ignorelist(ignorelist);
2918 match_ignores(struct got_pathlist_head *ignores, const char *path)
2920 struct got_pathlist_entry *pe;
2922 /* Handle patterns which match in all directories. */
2923 TAILQ_FOREACH(pe, ignores, entry) {
2924 struct got_pathlist_head *ignorelist = pe->data;
2925 struct got_pathlist_entry *pi;
2927 TAILQ_FOREACH(pi, ignorelist, entry) {
2928 const char *p, *pattern = pi->path;
2930 if (strncmp(pattern, "**/", 3) != 0)
2935 if (fnmatch(pattern, p,
2936 FNM_PATHNAME | FNM_LEADING_DIR)) {
2937 /* Retry in next directory. */
2938 while (*p && *p != '/')
2950 * The ignores pathlist contains ignore lists from children before
2951 * parents, so we can find the most specific ignorelist by walking
2952 * ignores backwards.
2954 pe = TAILQ_LAST(ignores, got_pathlist_head);
2956 if (got_path_is_child(path, pe->path, pe->path_len)) {
2957 struct got_pathlist_head *ignorelist = pe->data;
2958 struct got_pathlist_entry *pi;
2959 TAILQ_FOREACH(pi, ignorelist, entry) {
2960 const char *pattern = pi->path;
2961 int flags = FNM_LEADING_DIR;
2962 if (strstr(pattern, "/**/") == NULL)
2963 flags |= FNM_PATHNAME;
2964 if (fnmatch(pattern, path, flags))
2969 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
2975 static const struct got_error *
2976 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
2977 const char *path, int dirfd, const char *ignores_filename)
2979 const struct got_error *err = NULL;
2982 FILE *ignoresfile = NULL;
2984 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
2985 path[0] ? "/" : "", ignores_filename) == -1)
2986 return got_error_from_errno("asprintf");
2989 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
2991 if (errno != ENOENT && errno != EACCES)
2992 err = got_error_from_errno2("openat",
2995 ignoresfile = fdopen(fd, "r");
2996 if (ignoresfile == NULL)
2997 err = got_error_from_errno2("fdopen",
3001 err = read_ignores(ignores, path, ignoresfile);
3005 ignoresfile = fopen(ignorespath, "r");
3006 if (ignoresfile == NULL) {
3007 if (errno != ENOENT && errno != EACCES)
3008 err = got_error_from_errno2("fopen",
3011 err = read_ignores(ignores, path, ignoresfile);
3014 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3015 err = got_error_from_errno2("fclose", path);
3016 if (fd != -1 && close(fd) == -1 && err == NULL)
3017 err = got_error_from_errno2("close", path);
3022 static const struct got_error *
3023 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3025 const struct got_error *err = NULL;
3026 struct diff_dir_cb_arg *a = arg;
3029 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3030 return got_error(GOT_ERR_CANCELLED);
3032 if (parent_path[0]) {
3033 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3034 return got_error_from_errno("asprintf");
3039 if (de->d_type != DT_DIR &&
3040 got_path_is_child(path, a->status_path, a->status_path_len)
3041 && !match_ignores(&a->ignores, path))
3042 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3043 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3049 static const struct got_error *
3050 status_traverse(void *arg, const char *path, int dirfd)
3052 const struct got_error *err = NULL;
3053 struct diff_dir_cb_arg *a = arg;
3058 err = add_ignores(&a->ignores, a->worktree->root_path,
3059 path, dirfd, ".cvsignore");
3063 err = add_ignores(&a->ignores, a->worktree->root_path, path,
3064 dirfd, ".gitignore");
3069 static const struct got_error *
3070 report_single_file_status(const char *path, const char *ondisk_path,
3071 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3072 void *status_arg, struct got_repository *repo, int report_unchanged)
3074 struct got_fileindex_entry *ie;
3077 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3079 return report_file_status(ie, ondisk_path, -1, NULL,
3080 status_cb, status_arg, repo, report_unchanged);
3082 if (lstat(ondisk_path, &sb) == -1) {
3083 if (errno != ENOENT)
3084 return got_error_from_errno2("lstat", ondisk_path);
3085 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3086 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3090 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3091 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3092 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3097 static const struct got_error *
3098 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3099 const char *root_path, const char *path)
3101 const struct got_error *err;
3102 char *parent_path, *next_parent_path = NULL;
3104 err = add_ignores(ignores, root_path, "", -1,
3109 err = add_ignores(ignores, root_path, "", -1,
3114 err = got_path_dirname(&parent_path, path);
3116 if (err->code == GOT_ERR_BAD_PATH)
3117 return NULL; /* cannot traverse parent */
3121 err = add_ignores(ignores, root_path, parent_path, -1,
3125 err = add_ignores(ignores, root_path, parent_path, -1,
3129 err = got_path_dirname(&next_parent_path, parent_path);
3131 if (err->code == GOT_ERR_BAD_PATH)
3132 err = NULL; /* traversed everything */
3136 parent_path = next_parent_path;
3137 next_parent_path = NULL;
3141 free(next_parent_path);
3145 static const struct got_error *
3146 worktree_status(struct got_worktree *worktree, const char *path,
3147 struct got_fileindex *fileindex, struct got_repository *repo,
3148 got_worktree_status_cb status_cb, void *status_arg,
3149 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3150 int report_unchanged)
3152 const struct got_error *err = NULL;
3154 struct got_fileindex_diff_dir_cb fdiff_cb;
3155 struct diff_dir_cb_arg arg;
3156 char *ondisk_path = NULL;
3158 TAILQ_INIT(&arg.ignores);
3160 if (asprintf(&ondisk_path, "%s%s%s",
3161 worktree->root_path, path[0] ? "/" : "", path) == -1)
3162 return got_error_from_errno("asprintf");
3164 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3166 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3168 err = got_error_from_errno2("open", ondisk_path);
3170 err = report_single_file_status(path, ondisk_path,
3171 fileindex, status_cb, status_arg, repo,
3174 fdiff_cb.diff_old_new = status_old_new;
3175 fdiff_cb.diff_old = status_old;
3176 fdiff_cb.diff_new = status_new;
3177 fdiff_cb.diff_traverse = status_traverse;
3178 arg.fileindex = fileindex;
3179 arg.worktree = worktree;
3180 arg.status_path = path;
3181 arg.status_path_len = strlen(path);
3183 arg.status_cb = status_cb;
3184 arg.status_arg = status_arg;
3185 arg.cancel_cb = cancel_cb;
3186 arg.cancel_arg = cancel_arg;
3187 arg.report_unchanged = report_unchanged;
3188 arg.no_ignores = no_ignores;
3190 err = add_ignores_from_parent_paths(&arg.ignores,
3191 worktree->root_path, path);
3195 err = got_fileindex_diff_dir(fileindex, fd,
3196 worktree->root_path, path, repo, &fdiff_cb, &arg);
3199 free_ignores(&arg.ignores);
3200 if (fd != -1 && close(fd) != 0 && err == NULL)
3201 err = got_error_from_errno("close");
3206 const struct got_error *
3207 got_worktree_status(struct got_worktree *worktree,
3208 struct got_pathlist_head *paths, struct got_repository *repo,
3209 got_worktree_status_cb status_cb, void *status_arg,
3210 got_cancel_cb cancel_cb, void *cancel_arg)
3212 const struct got_error *err = NULL;
3213 char *fileindex_path = NULL;
3214 struct got_fileindex *fileindex = NULL;
3215 struct got_pathlist_entry *pe;
3217 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3221 TAILQ_FOREACH(pe, paths, entry) {
3222 err = worktree_status(worktree, pe->path, fileindex, repo,
3223 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3227 free(fileindex_path);
3228 got_fileindex_free(fileindex);
3232 const struct got_error *
3233 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3236 const struct got_error *err = NULL;
3237 char *resolved = NULL, *cwd = NULL, *path = NULL;
3243 cwd = getcwd(NULL, 0);
3245 return got_error_from_errno("getcwd");
3247 if (lstat(arg, &sb) == -1) {
3248 if (errno != ENOENT) {
3249 err = got_error_from_errno2("lstat", arg);
3253 if (S_ISLNK(sb.st_mode)) {
3255 * We cannot use realpath(3) with symlinks since we want to
3256 * operate on the symlink itself.
3257 * But we can make the path absolute, assuming it is relative
3258 * to the current working directory, and then canonicalize it.
3260 char *abspath = NULL;
3261 char canonpath[PATH_MAX];
3262 if (!got_path_is_absolute(arg)) {
3263 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3264 err = got_error_from_errno("asprintf");
3269 err = got_canonpath(abspath ? abspath : arg, canonpath,
3273 resolved = strdup(canonpath);
3274 if (resolved == NULL) {
3275 err = got_error_from_errno("strdup");
3279 resolved = realpath(arg, NULL);
3280 if (resolved == NULL) {
3281 if (errno != ENOENT) {
3282 err = got_error_from_errno2("realpath", arg);
3285 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
3286 err = got_error_from_errno("asprintf");
3292 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3293 strlen(got_worktree_get_root_path(worktree)))) {
3294 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3298 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3299 err = got_path_skip_common_ancestor(&path,
3300 got_worktree_get_root_path(worktree), resolved);
3306 err = got_error_from_errno("strdup");
3311 /* XXX status walk can't deal with trailing slash! */
3313 while (len > 0 && path[len - 1] == '/') {
3314 path[len - 1] = '\0';
3327 struct schedule_addition_args {
3328 struct got_worktree *worktree;
3329 struct got_fileindex *fileindex;
3330 got_worktree_checkout_cb progress_cb;
3332 struct got_repository *repo;
3335 static const struct got_error *
3336 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3337 const char *relpath, struct got_object_id *blob_id,
3338 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3339 int dirfd, const char *de_name)
3341 struct schedule_addition_args *a = arg;
3342 const struct got_error *err = NULL;
3343 struct got_fileindex_entry *ie;
3347 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3349 return got_error_from_errno("asprintf");
3351 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3353 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3357 /* Re-adding an existing entry is a no-op. */
3358 if (status == GOT_STATUS_ADD)
3360 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3365 if (status != GOT_STATUS_UNVERSIONED) {
3366 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3370 err = got_fileindex_entry_alloc(&ie, relpath);
3373 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3375 got_fileindex_entry_free(ie);
3378 err = got_fileindex_entry_add(a->fileindex, ie);
3380 got_fileindex_entry_free(ie);
3387 if (status == GOT_STATUS_ADD)
3389 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3392 const struct got_error *
3393 got_worktree_schedule_add(struct got_worktree *worktree,
3394 struct got_pathlist_head *paths,
3395 got_worktree_checkout_cb progress_cb, void *progress_arg,
3396 struct got_repository *repo, int no_ignores)
3398 struct got_fileindex *fileindex = NULL;
3399 char *fileindex_path = NULL;
3400 const struct got_error *err = NULL, *sync_err, *unlockerr;
3401 struct got_pathlist_entry *pe;
3402 struct schedule_addition_args saa;
3404 err = lock_worktree(worktree, LOCK_EX);
3408 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3412 saa.worktree = worktree;
3413 saa.fileindex = fileindex;
3414 saa.progress_cb = progress_cb;
3415 saa.progress_arg = progress_arg;
3418 TAILQ_FOREACH(pe, paths, entry) {
3419 err = worktree_status(worktree, pe->path, fileindex, repo,
3420 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3424 sync_err = sync_fileindex(fileindex, fileindex_path);
3425 if (sync_err && err == NULL)
3428 free(fileindex_path);
3430 got_fileindex_free(fileindex);
3431 unlockerr = lock_worktree(worktree, LOCK_SH);
3432 if (unlockerr && err == NULL)
3437 struct schedule_deletion_args {
3438 struct got_worktree *worktree;
3439 struct got_fileindex *fileindex;
3440 got_worktree_delete_cb progress_cb;
3442 struct got_repository *repo;
3443 int delete_local_mods;
3447 static const struct got_error *
3448 schedule_for_deletion(void *arg, unsigned char status,
3449 unsigned char staged_status, const char *relpath,
3450 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3451 struct got_object_id *commit_id, int dirfd, const char *de_name)
3453 struct schedule_deletion_args *a = arg;
3454 const struct got_error *err = NULL;
3455 struct got_fileindex_entry *ie = NULL;
3457 char *ondisk_path, *parent = NULL;
3459 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3461 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3463 staged_status = get_staged_status(ie);
3464 if (staged_status != GOT_STATUS_NO_CHANGE) {
3465 if (staged_status == GOT_STATUS_DELETE)
3467 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3470 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3472 return got_error_from_errno("asprintf");
3474 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3479 if (status != GOT_STATUS_NO_CHANGE) {
3480 if (status == GOT_STATUS_DELETE)
3482 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3483 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3486 if (status != GOT_STATUS_MODIFY &&
3487 status != GOT_STATUS_MISSING) {
3488 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3493 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3495 if (unlinkat(dirfd, de_name, 0) != 0) {
3496 err = got_error_from_errno2("unlinkat",
3500 } else if (unlink(ondisk_path) != 0) {
3501 err = got_error_from_errno2("unlink", ondisk_path);
3505 parent = dirname(ondisk_path);
3507 if (parent == NULL) {
3508 err = got_error_from_errno2("dirname", ondisk_path);
3511 while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3512 if (rmdir(parent) == -1) {
3513 if (errno != ENOTEMPTY)
3514 err = got_error_from_errno2("rmdir",
3518 parent = dirname(parent);
3519 if (parent == NULL) {
3520 err = got_error_from_errno2("dirname", parent);
3526 got_fileindex_entry_mark_deleted_from_disk(ie);
3531 if (status == GOT_STATUS_DELETE)
3533 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3534 staged_status, relpath);
3537 const struct got_error *
3538 got_worktree_schedule_delete(struct got_worktree *worktree,
3539 struct got_pathlist_head *paths, int delete_local_mods,
3540 got_worktree_delete_cb progress_cb, void *progress_arg,
3541 struct got_repository *repo, int keep_on_disk)
3543 struct got_fileindex *fileindex = NULL;
3544 char *fileindex_path = NULL;
3545 const struct got_error *err = NULL, *sync_err, *unlockerr;
3546 struct got_pathlist_entry *pe;
3547 struct schedule_deletion_args sda;
3549 err = lock_worktree(worktree, LOCK_EX);
3553 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3557 sda.worktree = worktree;
3558 sda.fileindex = fileindex;
3559 sda.progress_cb = progress_cb;
3560 sda.progress_arg = progress_arg;
3562 sda.delete_local_mods = delete_local_mods;
3563 sda.keep_on_disk = keep_on_disk;
3565 TAILQ_FOREACH(pe, paths, entry) {
3566 err = worktree_status(worktree, pe->path, fileindex, repo,
3567 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3571 sync_err = sync_fileindex(fileindex, fileindex_path);
3572 if (sync_err && err == NULL)
3575 free(fileindex_path);
3577 got_fileindex_free(fileindex);
3578 unlockerr = lock_worktree(worktree, LOCK_SH);
3579 if (unlockerr && err == NULL)
3584 static const struct got_error *
3585 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3587 const struct got_error *err = NULL;
3589 size_t linesize = 0, n;
3592 linelen = getline(&line, &linesize, infile);
3593 if (linelen == -1) {
3594 if (ferror(infile)) {
3595 err = got_error_from_errno("getline");
3601 n = fwrite(line, 1, linelen, outfile);
3603 err = got_ferror(outfile, GOT_ERR_IO);
3608 n = fwrite(line, 1, linelen, rejectfile);
3610 err = got_ferror(outfile, GOT_ERR_IO);
3617 static const struct got_error *
3618 skip_one_line(FILE *f)
3621 size_t linesize = 0;
3624 linelen = getline(&line, &linesize, f);
3625 if (linelen == -1) {
3627 return got_error_from_errno("getline");
3634 static const struct got_error *
3635 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3636 int start_old, int end_old, int start_new, int end_new,
3637 FILE *outfile, FILE *rejectfile)
3639 const struct got_error *err;
3641 /* Copy old file's lines leading up to patch. */
3642 while (!feof(f1) && *line_cur1 < start_old) {
3643 err = copy_one_line(f1, outfile, NULL);
3648 /* Skip new file's lines leading up to patch. */
3649 while (!feof(f2) && *line_cur2 < start_new) {
3651 err = copy_one_line(f2, NULL, rejectfile);
3653 err = skip_one_line(f2);
3658 /* Copy patched lines. */
3659 while (!feof(f2) && *line_cur2 <= end_new) {
3660 err = copy_one_line(f2, outfile, NULL);
3665 /* Skip over old file's replaced lines. */
3666 while (!feof(f1) && *line_cur1 <= end_old) {
3668 err = copy_one_line(f1, NULL, rejectfile);
3670 err = skip_one_line(f1);
3679 static const struct got_error *
3680 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3681 FILE *outfile, FILE *rejectfile)
3683 const struct got_error *err;
3686 /* Copy old file's lines until EOF. */
3688 err = copy_one_line(f1, outfile, NULL);
3695 /* Copy new file's lines until EOF. */
3697 err = copy_one_line(f2, NULL, rejectfile);
3707 static const struct got_error *
3708 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3709 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3710 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3711 int *line_cur2, FILE *outfile, FILE *rejectfile,
3712 got_worktree_patch_cb patch_cb, void *patch_arg)
3714 const struct got_error *err = NULL;
3715 int start_old = change->cv.a;
3716 int end_old = change->cv.b;
3717 int start_new = change->cv.c;
3718 int end_new = change->cv.d;
3722 *choice = GOT_PATCH_CHOICE_NONE;
3724 hunkfile = got_opentemp();
3725 if (hunkfile == NULL)
3726 return got_error_from_errno("got_opentemp");
3731 /* XXX TODO needs error checking */
3732 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
3734 if (fseek(f1, pos1, SEEK_SET) == -1) {
3735 err = got_ferror(f1, GOT_ERR_IO);
3738 if (fseek(f2, pos2, SEEK_SET) == -1) {
3739 err = got_ferror(f1, GOT_ERR_IO);
3742 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
3743 err = got_ferror(hunkfile, GOT_ERR_IO);
3747 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
3748 hunkfile, n, nchanges);
3753 case GOT_PATCH_CHOICE_YES:
3754 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3755 end_old, start_new, end_new, outfile, rejectfile);
3757 case GOT_PATCH_CHOICE_NO:
3758 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3759 end_old, start_new, end_new, rejectfile, outfile);
3761 case GOT_PATCH_CHOICE_QUIT:
3764 err = got_error(GOT_ERR_PATCH_CHOICE);
3768 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3769 err = got_error_from_errno("fclose");
3773 struct revert_file_args {
3774 struct got_worktree *worktree;
3775 struct got_fileindex *fileindex;
3776 got_worktree_checkout_cb progress_cb;
3778 got_worktree_patch_cb patch_cb;
3780 struct got_repository *repo;
3783 static const struct got_error *
3784 create_patched_content(char **path_outfile, int reverse_patch,
3785 struct got_object_id *blob_id, const char *path2,
3786 int dirfd2, const char *de_name2,
3787 const char *relpath, struct got_repository *repo,
3788 got_worktree_patch_cb patch_cb, void *patch_arg)
3790 const struct got_error *err;
3791 struct got_blob_object *blob = NULL;
3792 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3794 char *path1 = NULL, *id_str = NULL;
3795 struct stat sb1, sb2;
3796 struct got_diff_changes *changes = NULL;
3797 struct got_diff_state *ds = NULL;
3798 struct got_diff_args *args = NULL;
3799 struct got_diff_change *change;
3800 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3803 *path_outfile = NULL;
3805 err = got_object_id_str(&id_str, blob_id);
3810 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
3812 err = got_error_from_errno2("openat", path2);
3816 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
3818 err = got_error_from_errno2("open", path2);
3822 if (fstat(fd2, &sb2) == -1) {
3823 err = got_error_from_errno2("fstat", path2);
3827 f2 = fdopen(fd2, "r");
3829 err = got_error_from_errno2("fdopen", path2);
3834 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
3838 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
3842 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
3846 if (stat(path1, &sb1) == -1) {
3847 err = got_error_from_errno2("stat", path1);
3851 err = got_diff_files(&changes, &ds, &args, &diff_flags,
3852 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
3856 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
3860 if (fseek(f1, 0L, SEEK_SET) == -1)
3861 return got_ferror(f1, GOT_ERR_IO);
3862 if (fseek(f2, 0L, SEEK_SET) == -1)
3863 return got_ferror(f2, GOT_ERR_IO);
3864 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
3866 err = apply_or_reject_change(&choice, change, ++n,
3867 changes->nchanges, ds, args, diff_flags, relpath,
3868 f1, f2, &line_cur1, &line_cur2,
3869 reverse_patch ? NULL : outfile,
3870 reverse_patch ? outfile : NULL,
3871 patch_cb, patch_arg);
3874 if (choice == GOT_PATCH_CHOICE_YES)
3876 else if (choice == GOT_PATCH_CHOICE_QUIT)
3880 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
3881 reverse_patch ? NULL : outfile,
3882 reverse_patch ? outfile : NULL);
3886 if (chmod(*path_outfile, sb2.st_mode) == -1) {
3887 err = got_error_from_errno2("chmod", path2);
3894 got_object_blob_close(blob);
3895 if (f1 && fclose(f1) == EOF && err == NULL)
3896 err = got_error_from_errno2("fclose", path1);
3897 if (f2 && fclose(f2) == EOF && err == NULL)
3898 err = got_error_from_errno2("fclose", path2);
3899 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3900 err = got_error_from_errno2("close", path2);
3901 if (outfile && fclose(outfile) == EOF && err == NULL)
3902 err = got_error_from_errno2("fclose", *path_outfile);
3903 if (path1 && unlink(path1) == -1 && err == NULL)
3904 err = got_error_from_errno2("unlink", path1);
3905 if (err || !have_content) {
3906 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
3907 err = got_error_from_errno2("unlink", *path_outfile);
3908 free(*path_outfile);
3909 *path_outfile = NULL;
3913 got_diff_state_free(ds);
3917 got_diff_free_changes(changes);
3922 static const struct got_error *
3923 revert_file(void *arg, unsigned char status, unsigned char staged_status,
3924 const char *relpath, struct got_object_id *blob_id,
3925 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3926 int dirfd, const char *de_name)
3928 struct revert_file_args *a = arg;
3929 const struct got_error *err = NULL;
3930 char *parent_path = NULL;
3931 struct got_fileindex_entry *ie;
3932 struct got_tree_object *tree = NULL;
3933 struct got_object_id *tree_id = NULL;
3934 const struct got_tree_entry *te = NULL;
3935 char *tree_path = NULL, *te_name;
3936 char *ondisk_path = NULL, *path_content = NULL;
3937 struct got_blob_object *blob = NULL;
3939 /* Reverting a staged deletion is a no-op. */
3940 if (status == GOT_STATUS_DELETE &&
3941 staged_status != GOT_STATUS_NO_CHANGE)
3944 if (status == GOT_STATUS_UNVERSIONED)
3945 return (*a->progress_cb)(a->progress_arg,
3946 GOT_STATUS_UNVERSIONED, relpath);
3948 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3950 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3952 /* Construct in-repository path of tree which contains this blob. */
3953 err = got_path_dirname(&parent_path, ie->path);
3955 if (err->code != GOT_ERR_BAD_PATH)
3957 parent_path = strdup("/");
3958 if (parent_path == NULL) {
3959 err = got_error_from_errno("strdup");
3963 if (got_path_is_root_dir(a->worktree->path_prefix)) {
3964 tree_path = strdup(parent_path);
3965 if (tree_path == NULL) {
3966 err = got_error_from_errno("strdup");
3970 if (got_path_is_root_dir(parent_path)) {
3971 tree_path = strdup(a->worktree->path_prefix);
3972 if (tree_path == NULL) {
3973 err = got_error_from_errno("strdup");
3977 if (asprintf(&tree_path, "%s/%s",
3978 a->worktree->path_prefix, parent_path) == -1) {
3979 err = got_error_from_errno("asprintf");
3985 err = got_object_id_by_path(&tree_id, a->repo,
3986 a->worktree->base_commit_id, tree_path);
3988 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
3989 (status == GOT_STATUS_ADD ||
3990 staged_status == GOT_STATUS_ADD)))
3993 err = got_object_open_as_tree(&tree, a->repo, tree_id);
3997 te_name = basename(ie->path);
3998 if (te_name == NULL) {
3999 err = got_error_from_errno2("basename", ie->path);
4003 te = got_object_tree_find_entry(tree, te_name);
4004 if (te == NULL && status != GOT_STATUS_ADD &&
4005 staged_status != GOT_STATUS_ADD) {
4006 err = got_error(GOT_ERR_NO_TREE_ENTRY);
4012 case GOT_STATUS_ADD:
4014 int choice = GOT_PATCH_CHOICE_NONE;
4015 err = (*a->patch_cb)(&choice, a->patch_arg,
4016 status, ie->path, NULL, 1, 1);
4019 if (choice != GOT_PATCH_CHOICE_YES)
4022 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4026 got_fileindex_entry_remove(a->fileindex, ie);
4028 case GOT_STATUS_DELETE:
4030 int choice = GOT_PATCH_CHOICE_NONE;
4031 err = (*a->patch_cb)(&choice, a->patch_arg,
4032 status, ie->path, NULL, 1, 1);
4035 if (choice != GOT_PATCH_CHOICE_YES)
4039 case GOT_STATUS_MODIFY:
4040 case GOT_STATUS_MODE_CHANGE:
4041 case GOT_STATUS_CONFLICT:
4042 case GOT_STATUS_MISSING: {
4043 struct got_object_id id;
4044 if (staged_status == GOT_STATUS_ADD ||
4045 staged_status == GOT_STATUS_MODIFY) {
4046 memcpy(id.sha1, ie->staged_blob_sha1,
4047 SHA1_DIGEST_LENGTH);
4049 memcpy(id.sha1, ie->blob_sha1,
4050 SHA1_DIGEST_LENGTH);
4051 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4055 if (asprintf(&ondisk_path, "%s/%s",
4056 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4057 err = got_error_from_errno("asprintf");
4061 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4062 status == GOT_STATUS_CONFLICT)) {
4063 err = create_patched_content(&path_content, 1, &id,
4064 ondisk_path, dirfd, de_name, ie->path, a->repo,
4065 a->patch_cb, a->patch_arg);
4066 if (err || path_content == NULL)
4068 if (rename(path_content, ondisk_path) == -1) {
4069 err = got_error_from_errno3("rename",
4070 path_content, ondisk_path);
4074 err = install_blob(a->worktree, ondisk_path, ie->path,
4075 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4076 got_fileindex_perms_to_st(ie), blob, 0, 1,
4077 a->repo, a->progress_cb, a->progress_arg);
4080 if (status == GOT_STATUS_DELETE ||
4081 status == GOT_STATUS_MODE_CHANGE) {
4082 err = got_fileindex_entry_update(ie,
4083 ondisk_path, blob->id.sha1,
4084 a->worktree->base_commit_id->sha1, 1);
4100 got_object_blob_close(blob);
4102 got_object_tree_close(tree);
4107 const struct got_error *
4108 got_worktree_revert(struct got_worktree *worktree,
4109 struct got_pathlist_head *paths,
4110 got_worktree_checkout_cb progress_cb, void *progress_arg,
4111 got_worktree_patch_cb patch_cb, void *patch_arg,
4112 struct got_repository *repo)
4114 struct got_fileindex *fileindex = NULL;
4115 char *fileindex_path = NULL;
4116 const struct got_error *err = NULL, *unlockerr = NULL;
4117 const struct got_error *sync_err = NULL;
4118 struct got_pathlist_entry *pe;
4119 struct revert_file_args rfa;
4121 err = lock_worktree(worktree, LOCK_EX);
4125 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4129 rfa.worktree = worktree;
4130 rfa.fileindex = fileindex;
4131 rfa.progress_cb = progress_cb;
4132 rfa.progress_arg = progress_arg;
4133 rfa.patch_cb = patch_cb;
4134 rfa.patch_arg = patch_arg;
4136 TAILQ_FOREACH(pe, paths, entry) {
4137 err = worktree_status(worktree, pe->path, fileindex, repo,
4138 revert_file, &rfa, NULL, NULL, 0, 0);
4142 sync_err = sync_fileindex(fileindex, fileindex_path);
4143 if (sync_err && err == NULL)
4146 free(fileindex_path);
4148 got_fileindex_free(fileindex);
4149 unlockerr = lock_worktree(worktree, LOCK_SH);
4150 if (unlockerr && err == NULL)
4156 free_commitable(struct got_commitable *ct)
4159 free(ct->in_repo_path);
4160 free(ct->ondisk_path);
4162 free(ct->base_blob_id);
4163 free(ct->staged_blob_id);
4164 free(ct->base_commit_id);
4168 struct collect_commitables_arg {
4169 struct got_pathlist_head *commitable_paths;
4170 struct got_repository *repo;
4171 struct got_worktree *worktree;
4172 int have_staged_files;
4175 static const struct got_error *
4176 collect_commitables(void *arg, unsigned char status,
4177 unsigned char staged_status, const char *relpath,
4178 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4179 struct got_object_id *commit_id, int dirfd, const char *de_name)
4181 struct collect_commitables_arg *a = arg;
4182 const struct got_error *err = NULL;
4183 struct got_commitable *ct = NULL;
4184 struct got_pathlist_entry *new = NULL;
4185 char *parent_path = NULL, *path = NULL;
4188 if (a->have_staged_files) {
4189 if (staged_status != GOT_STATUS_MODIFY &&
4190 staged_status != GOT_STATUS_ADD &&
4191 staged_status != GOT_STATUS_DELETE)
4194 if (status == GOT_STATUS_CONFLICT)
4195 return got_error(GOT_ERR_COMMIT_CONFLICT);
4197 if (status != GOT_STATUS_MODIFY &&
4198 status != GOT_STATUS_MODE_CHANGE &&
4199 status != GOT_STATUS_ADD &&
4200 status != GOT_STATUS_DELETE)
4204 if (asprintf(&path, "/%s", relpath) == -1) {
4205 err = got_error_from_errno("asprintf");
4208 if (strcmp(path, "/") == 0) {
4209 parent_path = strdup("");
4210 if (parent_path == NULL)
4211 return got_error_from_errno("strdup");
4213 err = got_path_dirname(&parent_path, path);
4218 ct = calloc(1, sizeof(*ct));
4220 err = got_error_from_errno("calloc");
4224 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4226 err = got_error_from_errno("asprintf");
4229 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
4230 sb.st_mode = GOT_DEFAULT_FILE_MODE;
4233 if (fstatat(dirfd, de_name, &sb,
4234 AT_SYMLINK_NOFOLLOW) == -1) {
4235 err = got_error_from_errno2("fstatat",
4239 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4240 err = got_error_from_errno2("lstat", ct->ondisk_path);
4243 ct->mode = sb.st_mode;
4246 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4247 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4249 err = got_error_from_errno("asprintf");
4253 ct->status = status;
4254 ct->staged_status = staged_status;
4255 ct->blob_id = NULL; /* will be filled in when blob gets created */
4256 if (ct->status != GOT_STATUS_ADD &&
4257 ct->staged_status != GOT_STATUS_ADD) {
4258 ct->base_blob_id = got_object_id_dup(blob_id);
4259 if (ct->base_blob_id == NULL) {
4260 err = got_error_from_errno("got_object_id_dup");
4263 ct->base_commit_id = got_object_id_dup(commit_id);
4264 if (ct->base_commit_id == NULL) {
4265 err = got_error_from_errno("got_object_id_dup");
4269 if (ct->staged_status == GOT_STATUS_ADD ||
4270 ct->staged_status == GOT_STATUS_MODIFY) {
4271 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4272 if (ct->staged_blob_id == NULL) {
4273 err = got_error_from_errno("got_object_id_dup");
4277 ct->path = strdup(path);
4278 if (ct->path == NULL) {
4279 err = got_error_from_errno("strdup");
4282 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4284 if (ct && (err || new == NULL))
4285 free_commitable(ct);
4291 static const struct got_error *write_tree(struct got_object_id **, int *,
4292 struct got_tree_object *, const char *, struct got_pathlist_head *,
4293 got_worktree_status_cb status_cb, void *status_arg,
4294 struct got_repository *);
4296 static const struct got_error *
4297 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4298 struct got_tree_entry *te, const char *parent_path,
4299 struct got_pathlist_head *commitable_paths,
4300 got_worktree_status_cb status_cb, void *status_arg,
4301 struct got_repository *repo)
4303 const struct got_error *err = NULL;
4304 struct got_tree_object *subtree;
4307 if (asprintf(&subpath, "%s%s%s", parent_path,
4308 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4309 return got_error_from_errno("asprintf");
4311 err = got_object_open_as_tree(&subtree, repo, &te->id);
4315 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4316 commitable_paths, status_cb, status_arg, repo);
4317 got_object_tree_close(subtree);
4322 static const struct got_error *
4323 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4325 const struct got_error *err = NULL;
4326 char *ct_parent_path = NULL;
4330 if (strchr(ct->in_repo_path, '/') == NULL) {
4331 *match = got_path_is_root_dir(path);
4335 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4338 *match = (strcmp(path, ct_parent_path) == 0);
4339 free(ct_parent_path);
4344 get_ct_file_mode(struct got_commitable *ct)
4346 if (S_ISLNK(ct->mode))
4349 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4352 static const struct got_error *
4353 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4354 struct got_tree_entry *te, struct got_commitable *ct)
4356 const struct got_error *err = NULL;
4360 err = got_object_tree_entry_dup(new_te, te);
4364 (*new_te)->mode = get_ct_file_mode(ct);
4366 if (ct->staged_status == GOT_STATUS_MODIFY)
4367 memcpy(&(*new_te)->id, ct->staged_blob_id,
4368 sizeof((*new_te)->id));
4370 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4372 if (err && *new_te) {
4379 static const struct got_error *
4380 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4381 struct got_commitable *ct)
4383 const struct got_error *err = NULL;
4388 *new_te = calloc(1, sizeof(**new_te));
4389 if (*new_te == NULL)
4390 return got_error_from_errno("calloc");
4392 ct_name = basename(ct->path);
4393 if (ct_name == NULL) {
4394 err = got_error_from_errno2("basename", ct->path);
4397 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4398 sizeof((*new_te)->name)) {
4399 err = got_error(GOT_ERR_NO_SPACE);
4403 (*new_te)->mode = get_ct_file_mode(ct);
4405 if (ct->staged_status == GOT_STATUS_ADD)
4406 memcpy(&(*new_te)->id, ct->staged_blob_id,
4407 sizeof((*new_te)->id));
4409 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4411 if (err && *new_te) {
4418 static const struct got_error *
4419 insert_tree_entry(struct got_tree_entry *new_te,
4420 struct got_pathlist_head *paths)
4422 const struct got_error *err = NULL;
4423 struct got_pathlist_entry *new_pe;
4425 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4429 return got_error(GOT_ERR_TREE_DUP_ENTRY);
4433 static const struct got_error *
4434 report_ct_status(struct got_commitable *ct,
4435 got_worktree_status_cb status_cb, void *status_arg)
4437 const char *ct_path = ct->path;
4438 unsigned char status;
4440 while (ct_path[0] == '/')
4443 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4444 status = ct->staged_status;
4446 status = ct->status;
4448 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4449 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4452 static const struct got_error *
4453 match_modified_subtree(int *modified, struct got_tree_entry *te,
4454 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4456 const struct got_error *err = NULL;
4457 struct got_pathlist_entry *pe;
4462 if (asprintf(&te_path, "%s%s%s", base_tree_path,
4463 got_path_is_root_dir(base_tree_path) ? "" : "/",
4465 return got_error_from_errno("asprintf");
4467 TAILQ_FOREACH(pe, commitable_paths, entry) {
4468 struct got_commitable *ct = pe->data;
4469 *modified = got_path_is_child(ct->in_repo_path, te_path,
4479 static const struct got_error *
4480 match_deleted_or_modified_ct(struct got_commitable **ctp,
4481 struct got_tree_entry *te, const char *base_tree_path,
4482 struct got_pathlist_head *commitable_paths)
4484 const struct got_error *err = NULL;
4485 struct got_pathlist_entry *pe;
4489 TAILQ_FOREACH(pe, commitable_paths, entry) {
4490 struct got_commitable *ct = pe->data;
4491 char *ct_name = NULL;
4494 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4495 if (ct->status != GOT_STATUS_MODIFY &&
4496 ct->status != GOT_STATUS_MODE_CHANGE &&
4497 ct->status != GOT_STATUS_DELETE)
4500 if (ct->staged_status != GOT_STATUS_MODIFY &&
4501 ct->staged_status != GOT_STATUS_DELETE)
4505 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4508 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4514 ct_name = basename(pe->path);
4515 if (ct_name == NULL)
4516 return got_error_from_errno2("basename", pe->path);
4518 if (strcmp(te->name, ct_name) != 0)
4528 static const struct got_error *
4529 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4530 const char *child_path, const char *path_base_tree,
4531 struct got_pathlist_head *commitable_paths,
4532 got_worktree_status_cb status_cb, void *status_arg,
4533 struct got_repository *repo)
4535 const struct got_error *err = NULL;
4536 struct got_tree_entry *new_te;
4538 struct got_object_id *id = NULL;
4543 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4544 got_path_is_root_dir(path_base_tree) ? "" : "/",
4546 return got_error_from_errno("asprintf");
4548 new_te = calloc(1, sizeof(*new_te));
4550 return got_error_from_errno("calloc");
4551 new_te->mode = S_IFDIR;
4553 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4554 sizeof(new_te->name)) {
4555 err = got_error(GOT_ERR_NO_SPACE);
4558 err = write_tree(&id, &nentries, NULL, subtree_path,
4559 commitable_paths, status_cb, status_arg, repo);
4564 memcpy(&new_te->id, id, sizeof(new_te->id));
4573 static const struct got_error *
4574 write_tree(struct got_object_id **new_tree_id, int *nentries,
4575 struct got_tree_object *base_tree, const char *path_base_tree,
4576 struct got_pathlist_head *commitable_paths,
4577 got_worktree_status_cb status_cb, void *status_arg,
4578 struct got_repository *repo)
4580 const struct got_error *err = NULL;
4581 struct got_pathlist_head paths;
4582 struct got_tree_entry *te, *new_te = NULL;
4583 struct got_pathlist_entry *pe;
4588 /* Insert, and recurse into, newly added entries first. */
4589 TAILQ_FOREACH(pe, commitable_paths, entry) {
4590 struct got_commitable *ct = pe->data;
4591 char *child_path = NULL, *slash;
4593 if ((ct->status != GOT_STATUS_ADD &&
4594 ct->staged_status != GOT_STATUS_ADD) ||
4595 (ct->flags & GOT_COMMITABLE_ADDED))
4598 if (!got_path_is_child(pe->path, path_base_tree,
4599 strlen(path_base_tree)))
4602 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
4607 slash = strchr(child_path, '/');
4608 if (slash == NULL) {
4609 err = alloc_added_blob_tree_entry(&new_te, ct);
4612 err = report_ct_status(ct, status_cb, status_arg);
4615 ct->flags |= GOT_COMMITABLE_ADDED;
4616 err = insert_tree_entry(new_te, &paths);
4621 *slash = '\0'; /* trim trailing path components */
4622 if (base_tree == NULL ||
4623 got_object_tree_find_entry(base_tree, child_path)
4625 err = make_subtree_for_added_blob(&new_te,
4626 child_path, path_base_tree,
4627 commitable_paths, status_cb, status_arg,
4631 err = insert_tree_entry(new_te, &paths);
4640 int i, nbase_entries;
4641 /* Handle modified and deleted entries. */
4642 nbase_entries = got_object_tree_get_nentries(base_tree);
4643 for (i = 0; i < nbase_entries; i++) {
4644 struct got_commitable *ct = NULL;
4646 te = got_object_tree_get_entry(base_tree, i);
4647 if (got_object_tree_entry_is_submodule(te)) {
4648 /* Entry is a submodule; just copy it. */
4649 err = got_object_tree_entry_dup(&new_te, te);
4652 err = insert_tree_entry(new_te, &paths);
4659 if (S_ISDIR(te->mode)) {
4661 err = got_object_tree_entry_dup(&new_te, te);
4664 err = match_modified_subtree(&modified, te,
4665 path_base_tree, commitable_paths);
4668 /* Avoid recursion into unmodified subtrees. */
4670 struct got_object_id *new_id;
4672 err = write_subtree(&new_id,
4674 path_base_tree, commitable_paths,
4675 status_cb, status_arg, repo);
4678 if (nsubentries == 0) {
4679 /* All entries were deleted. */
4683 memcpy(&new_te->id, new_id,
4684 sizeof(new_te->id));
4687 err = insert_tree_entry(new_te, &paths);
4694 err = match_deleted_or_modified_ct(&ct, te,
4695 path_base_tree, commitable_paths);
4699 /* NB: Deleted entries get dropped here. */
4700 if (ct->status == GOT_STATUS_MODIFY ||
4701 ct->status == GOT_STATUS_MODE_CHANGE ||
4702 ct->staged_status == GOT_STATUS_MODIFY) {
4703 err = alloc_modified_blob_tree_entry(
4707 err = insert_tree_entry(new_te, &paths);
4712 err = report_ct_status(ct, status_cb,
4717 /* Entry is unchanged; just copy it. */
4718 err = got_object_tree_entry_dup(&new_te, te);
4721 err = insert_tree_entry(new_te, &paths);
4729 /* Write new list of entries; deleted entries have been dropped. */
4730 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
4732 got_pathlist_free(&paths);
4736 static const struct got_error *
4737 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
4738 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
4739 int have_staged_files)
4741 const struct got_error *err = NULL;
4742 struct got_pathlist_entry *pe;
4744 TAILQ_FOREACH(pe, commitable_paths, entry) {
4745 struct got_fileindex_entry *ie;
4746 struct got_commitable *ct = pe->data;
4748 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4750 if (ct->status == GOT_STATUS_DELETE ||
4751 ct->staged_status == GOT_STATUS_DELETE) {
4752 got_fileindex_entry_remove(fileindex, ie);
4753 } else if (ct->staged_status == GOT_STATUS_ADD ||
4754 ct->staged_status == GOT_STATUS_MODIFY) {
4755 got_fileindex_entry_stage_set(ie,
4756 GOT_FILEIDX_STAGE_NONE);
4757 err = got_fileindex_entry_update(ie,
4758 ct->ondisk_path, ct->staged_blob_id->sha1,
4759 new_base_commit_id->sha1,
4760 !have_staged_files);
4762 err = got_fileindex_entry_update(ie,
4763 ct->ondisk_path, ct->blob_id->sha1,
4764 new_base_commit_id->sha1,
4765 !have_staged_files);
4767 err = got_fileindex_entry_alloc(&ie, pe->path);
4770 err = got_fileindex_entry_update(ie, ct->ondisk_path,
4771 ct->blob_id->sha1, new_base_commit_id->sha1, 1);
4773 got_fileindex_entry_free(ie);
4776 err = got_fileindex_entry_add(fileindex, ie);
4778 got_fileindex_entry_free(ie);
4787 static const struct got_error *
4788 check_out_of_date(const char *in_repo_path, unsigned char status,
4789 unsigned char staged_status, struct got_object_id *base_blob_id,
4790 struct got_object_id *base_commit_id,
4791 struct got_object_id *head_commit_id, struct got_repository *repo,
4794 const struct got_error *err = NULL;
4795 struct got_object_id *id = NULL;
4797 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
4798 /* Trivial case: base commit == head commit */
4799 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
4802 * Ensure file content which local changes were based
4803 * on matches file content in the branch head.
4805 err = got_object_id_by_path(&id, repo, head_commit_id,
4808 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4809 err = got_error(ood_errcode);
4811 } else if (got_object_id_cmp(id, base_blob_id) != 0)
4812 err = got_error(ood_errcode);
4814 /* Require that added files don't exist in the branch head. */
4815 err = got_object_id_by_path(&id, repo, head_commit_id,
4817 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
4819 err = id ? got_error(ood_errcode) : NULL;
4826 const struct got_error *
4827 commit_worktree(struct got_object_id **new_commit_id,
4828 struct got_pathlist_head *commitable_paths,
4829 struct got_object_id *head_commit_id, struct got_worktree *worktree,
4830 const char *author, const char *committer,
4831 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4832 got_worktree_status_cb status_cb, void *status_arg,
4833 struct got_repository *repo)
4835 const struct got_error *err = NULL, *unlockerr = NULL;
4836 struct got_pathlist_entry *pe;
4837 const char *head_ref_name = NULL;
4838 struct got_commit_object *head_commit = NULL;
4839 struct got_reference *head_ref2 = NULL;
4840 struct got_object_id *head_commit_id2 = NULL;
4841 struct got_tree_object *head_tree = NULL;
4842 struct got_object_id *new_tree_id = NULL;
4844 struct got_object_id_queue parent_ids;
4845 struct got_object_qid *pid = NULL;
4846 char *logmsg = NULL;
4848 *new_commit_id = NULL;
4850 SIMPLEQ_INIT(&parent_ids);
4852 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
4856 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
4860 if (commit_msg_cb != NULL) {
4861 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
4866 if (logmsg == NULL || strlen(logmsg) == 0) {
4867 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
4871 /* Create blobs from added and modified files and record their IDs. */
4872 TAILQ_FOREACH(pe, commitable_paths, entry) {
4873 struct got_commitable *ct = pe->data;
4876 /* Blobs for staged files already exist. */
4877 if (ct->staged_status == GOT_STATUS_ADD ||
4878 ct->staged_status == GOT_STATUS_MODIFY)
4881 if (ct->status != GOT_STATUS_ADD &&
4882 ct->status != GOT_STATUS_MODIFY &&
4883 ct->status != GOT_STATUS_MODE_CHANGE)
4886 if (asprintf(&ondisk_path, "%s/%s",
4887 worktree->root_path, pe->path) == -1) {
4888 err = got_error_from_errno("asprintf");
4891 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
4897 /* Recursively write new tree objects. */
4898 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
4899 commitable_paths, status_cb, status_arg, repo);
4903 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
4906 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
4907 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
4908 1, author, time(NULL), committer, time(NULL), logmsg, repo);
4909 got_object_qid_free(pid);
4915 /* Check if a concurrent commit to our branch has occurred. */
4916 head_ref_name = got_worktree_get_head_ref_name(worktree);
4917 if (head_ref_name == NULL) {
4918 err = got_error_from_errno("got_worktree_get_head_ref_name");
4921 /* Lock the reference here to prevent concurrent modification. */
4922 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
4925 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
4928 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
4929 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
4932 /* Update branch head in repository. */
4933 err = got_ref_change_ref(head_ref2, *new_commit_id);
4936 err = got_ref_write(head_ref2, repo);
4940 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
4944 err = ref_base_commit(worktree, repo);
4949 got_object_tree_close(head_tree);
4951 got_object_commit_close(head_commit);
4952 free(head_commit_id2);
4954 unlockerr = got_ref_unlock(head_ref2);
4955 if (unlockerr && err == NULL)
4957 got_ref_close(head_ref2);
4962 static const struct got_error *
4963 check_path_is_commitable(const char *path,
4964 struct got_pathlist_head *commitable_paths)
4966 struct got_pathlist_entry *cpe = NULL;
4967 size_t path_len = strlen(path);
4969 TAILQ_FOREACH(cpe, commitable_paths, entry) {
4970 struct got_commitable *ct = cpe->data;
4971 const char *ct_path = ct->path;
4973 while (ct_path[0] == '/')
4976 if (strcmp(path, ct_path) == 0 ||
4977 got_path_is_child(ct_path, path, path_len))
4982 return got_error_path(path, GOT_ERR_BAD_PATH);
4987 static const struct got_error *
4988 check_staged_file(void *arg, struct got_fileindex_entry *ie)
4990 int *have_staged_files = arg;
4992 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
4993 *have_staged_files = 1;
4994 return got_error(GOT_ERR_CANCELLED);
5000 static const struct got_error *
5001 check_non_staged_files(struct got_fileindex *fileindex,
5002 struct got_pathlist_head *paths)
5004 struct got_pathlist_entry *pe;
5005 struct got_fileindex_entry *ie;
5007 TAILQ_FOREACH(pe, paths, entry) {
5008 if (pe->path[0] == '\0')
5010 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5012 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5013 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5014 return got_error_path(pe->path,
5015 GOT_ERR_FILE_NOT_STAGED);
5021 const struct got_error *
5022 got_worktree_commit(struct got_object_id **new_commit_id,
5023 struct got_worktree *worktree, struct got_pathlist_head *paths,
5024 const char *author, const char *committer,
5025 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5026 got_worktree_status_cb status_cb, void *status_arg,
5027 struct got_repository *repo)
5029 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5030 struct got_fileindex *fileindex = NULL;
5031 char *fileindex_path = NULL;
5032 struct got_pathlist_head commitable_paths;
5033 struct collect_commitables_arg cc_arg;
5034 struct got_pathlist_entry *pe;
5035 struct got_reference *head_ref = NULL;
5036 struct got_object_id *head_commit_id = NULL;
5037 int have_staged_files = 0;
5039 *new_commit_id = NULL;
5041 TAILQ_INIT(&commitable_paths);
5043 err = lock_worktree(worktree, LOCK_EX);
5047 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5051 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5055 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5059 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5060 &have_staged_files);
5061 if (err && err->code != GOT_ERR_CANCELLED)
5063 if (have_staged_files) {
5064 err = check_non_staged_files(fileindex, paths);
5069 cc_arg.commitable_paths = &commitable_paths;
5070 cc_arg.worktree = worktree;
5072 cc_arg.have_staged_files = have_staged_files;
5073 TAILQ_FOREACH(pe, paths, entry) {
5074 err = worktree_status(worktree, pe->path, fileindex, repo,
5075 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5080 if (TAILQ_EMPTY(&commitable_paths)) {
5081 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5085 TAILQ_FOREACH(pe, paths, entry) {
5086 err = check_path_is_commitable(pe->path, &commitable_paths);
5091 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5092 struct got_commitable *ct = pe->data;
5093 const char *ct_path = ct->in_repo_path;
5095 while (ct_path[0] == '/')
5097 err = check_out_of_date(ct_path, ct->status,
5098 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5099 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5105 err = commit_worktree(new_commit_id, &commitable_paths,
5106 head_commit_id, worktree, author, committer,
5107 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5111 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5112 fileindex, have_staged_files);
5113 sync_err = sync_fileindex(fileindex, fileindex_path);
5114 if (sync_err && err == NULL)
5118 got_fileindex_free(fileindex);
5119 free(fileindex_path);
5120 unlockerr = lock_worktree(worktree, LOCK_SH);
5121 if (unlockerr && err == NULL)
5123 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5124 struct got_commitable *ct = pe->data;
5125 free_commitable(ct);
5127 got_pathlist_free(&commitable_paths);
5132 got_commitable_get_path(struct got_commitable *ct)
5138 got_commitable_get_status(struct got_commitable *ct)
5143 struct check_rebase_ok_arg {
5144 struct got_worktree *worktree;
5145 struct got_repository *repo;
5148 static const struct got_error *
5149 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5151 const struct got_error *err = NULL;
5152 struct check_rebase_ok_arg *a = arg;
5153 unsigned char status;
5157 /* Reject rebase of a work tree with mixed base commits. */
5158 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5159 SHA1_DIGEST_LENGTH))
5160 return got_error(GOT_ERR_MIXED_COMMITS);
5162 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5164 return got_error_from_errno("asprintf");
5166 /* Reject rebase of a work tree with modified or staged files. */
5167 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5172 if (status != GOT_STATUS_NO_CHANGE)
5173 return got_error(GOT_ERR_MODIFIED);
5174 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5175 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5180 const struct got_error *
5181 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5182 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5183 struct got_worktree *worktree, struct got_reference *branch,
5184 struct got_repository *repo)
5186 const struct got_error *err = NULL;
5187 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5188 char *branch_ref_name = NULL;
5189 char *fileindex_path = NULL;
5190 struct check_rebase_ok_arg ok_arg;
5191 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5192 struct got_object_id *wt_branch_tip = NULL;
5194 *new_base_branch_ref = NULL;
5198 err = lock_worktree(worktree, LOCK_EX);
5202 err = open_fileindex(fileindex, &fileindex_path, worktree);
5206 ok_arg.worktree = worktree;
5208 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5213 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5217 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5221 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5225 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5230 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5233 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5234 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5238 err = got_ref_alloc_symref(new_base_branch_ref,
5239 new_base_branch_ref_name, wt_branch);
5242 err = got_ref_write(*new_base_branch_ref, repo);
5246 /* TODO Lock original branch's ref while rebasing? */
5248 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5252 err = got_ref_write(branch_ref, repo);
5256 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5257 worktree->base_commit_id);
5260 err = got_ref_write(*tmp_branch, repo);
5264 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5268 free(fileindex_path);
5269 free(tmp_branch_name);
5270 free(new_base_branch_ref_name);
5271 free(branch_ref_name);
5273 got_ref_close(branch_ref);
5275 got_ref_close(wt_branch);
5276 free(wt_branch_tip);
5278 if (*new_base_branch_ref) {
5279 got_ref_close(*new_base_branch_ref);
5280 *new_base_branch_ref = NULL;
5283 got_ref_close(*tmp_branch);
5287 got_fileindex_free(*fileindex);
5290 lock_worktree(worktree, LOCK_SH);
5295 const struct got_error *
5296 got_worktree_rebase_continue(struct got_object_id **commit_id,
5297 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5298 struct got_reference **branch, struct got_fileindex **fileindex,
5299 struct got_worktree *worktree, struct got_repository *repo)
5301 const struct got_error *err;
5302 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5303 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5304 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5305 char *fileindex_path = NULL;
5306 int have_staged_files = 0;
5309 *new_base_branch = NULL;
5314 err = lock_worktree(worktree, LOCK_EX);
5318 err = open_fileindex(fileindex, &fileindex_path, worktree);
5322 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5323 &have_staged_files);
5324 if (err && err->code != GOT_ERR_CANCELLED)
5326 if (have_staged_files) {
5327 err = got_error(GOT_ERR_STAGED_PATHS);
5331 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5335 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5339 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5343 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5347 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5351 err = got_ref_open(branch, repo,
5352 got_ref_get_symref_target(branch_ref), 0);
5356 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5360 err = got_ref_resolve(commit_id, repo, commit_ref);
5364 err = got_ref_open(new_base_branch, repo,
5365 new_base_branch_ref_name, 0);
5369 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5373 free(commit_ref_name);
5374 free(branch_ref_name);
5375 free(fileindex_path);
5377 got_ref_close(commit_ref);
5379 got_ref_close(branch_ref);
5384 got_ref_close(*tmp_branch);
5387 if (*new_base_branch) {
5388 got_ref_close(*new_base_branch);
5389 *new_base_branch = NULL;
5392 got_ref_close(*branch);
5396 got_fileindex_free(*fileindex);
5399 lock_worktree(worktree, LOCK_SH);
5404 const struct got_error *
5405 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5407 const struct got_error *err;
5408 char *tmp_branch_name = NULL;
5410 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5414 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5415 free(tmp_branch_name);
5419 static const struct got_error *
5420 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5421 char **logmsg, void *arg)
5427 static const struct got_error *
5428 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5429 const char *path, struct got_object_id *blob_id,
5430 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5431 int dirfd, const char *de_name)
5436 struct collect_merged_paths_arg {
5437 got_worktree_checkout_cb progress_cb;
5439 struct got_pathlist_head *merged_paths;
5442 static const struct got_error *
5443 collect_merged_paths(void *arg, unsigned char status, const char *path)
5445 const struct got_error *err;
5446 struct collect_merged_paths_arg *a = arg;
5448 struct got_pathlist_entry *new;
5450 err = (*a->progress_cb)(a->progress_arg, status, path);
5454 if (status != GOT_STATUS_MERGE &&
5455 status != GOT_STATUS_ADD &&
5456 status != GOT_STATUS_DELETE &&
5457 status != GOT_STATUS_CONFLICT)
5462 return got_error_from_errno("strdup");
5464 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5465 if (err || new == NULL)
5471 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5473 struct got_pathlist_entry *pe;
5475 TAILQ_FOREACH(pe, merged_paths, entry)
5476 free((char *)pe->path);
5478 got_pathlist_free(merged_paths);
5481 static const struct got_error *
5482 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5483 int is_rebase, struct got_repository *repo)
5485 const struct got_error *err;
5486 struct got_reference *commit_ref = NULL;
5488 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5490 if (err->code != GOT_ERR_NOT_REF)
5492 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
5495 err = got_ref_write(commit_ref, repo);
5498 } else if (is_rebase) {
5499 struct got_object_id *stored_id;
5502 err = got_ref_resolve(&stored_id, repo, commit_ref);
5505 cmp = got_object_id_cmp(commit_id, stored_id);
5508 err = got_error(GOT_ERR_REBASE_COMMITID);
5514 got_ref_close(commit_ref);
5518 static const struct got_error *
5519 rebase_merge_files(struct got_pathlist_head *merged_paths,
5520 const char *commit_ref_name, struct got_worktree *worktree,
5521 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
5522 struct got_object_id *commit_id, struct got_repository *repo,
5523 got_worktree_checkout_cb progress_cb, void *progress_arg,
5524 got_cancel_cb cancel_cb, void *cancel_arg)
5526 const struct got_error *err;
5527 struct got_reference *commit_ref = NULL;
5528 struct collect_merged_paths_arg cmp_arg;
5529 char *fileindex_path;
5531 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5533 err = get_fileindex_path(&fileindex_path, worktree);
5537 cmp_arg.progress_cb = progress_cb;
5538 cmp_arg.progress_arg = progress_arg;
5539 cmp_arg.merged_paths = merged_paths;
5540 err = merge_files(worktree, fileindex, fileindex_path,
5541 parent_commit_id, commit_id, repo, collect_merged_paths,
5542 &cmp_arg, cancel_cb, cancel_arg);
5544 got_ref_close(commit_ref);
5548 const struct got_error *
5549 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
5550 struct got_worktree *worktree, struct got_fileindex *fileindex,
5551 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5552 struct got_repository *repo,
5553 got_worktree_checkout_cb progress_cb, void *progress_arg,
5554 got_cancel_cb cancel_cb, void *cancel_arg)
5556 const struct got_error *err;
5557 char *commit_ref_name;
5559 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5563 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
5567 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5568 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5569 progress_arg, cancel_cb, cancel_arg);
5571 free(commit_ref_name);
5575 const struct got_error *
5576 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
5577 struct got_worktree *worktree, struct got_fileindex *fileindex,
5578 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5579 struct got_repository *repo,
5580 got_worktree_checkout_cb progress_cb, void *progress_arg,
5581 got_cancel_cb cancel_cb, void *cancel_arg)
5583 const struct got_error *err;
5584 char *commit_ref_name;
5586 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5590 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
5594 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5595 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5596 progress_arg, cancel_cb, cancel_arg);
5598 free(commit_ref_name);
5602 static const struct got_error *
5603 rebase_commit(struct got_object_id **new_commit_id,
5604 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
5605 struct got_worktree *worktree, struct got_fileindex *fileindex,
5606 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
5607 const char *new_logmsg, struct got_repository *repo)
5609 const struct got_error *err, *sync_err;
5610 struct got_pathlist_head commitable_paths;
5611 struct collect_commitables_arg cc_arg;
5612 char *fileindex_path = NULL;
5613 struct got_reference *head_ref = NULL;
5614 struct got_object_id *head_commit_id = NULL;
5615 char *logmsg = NULL;
5617 TAILQ_INIT(&commitable_paths);
5618 *new_commit_id = NULL;
5620 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5622 err = get_fileindex_path(&fileindex_path, worktree);
5626 cc_arg.commitable_paths = &commitable_paths;
5627 cc_arg.worktree = worktree;
5629 cc_arg.have_staged_files = 0;
5631 * If possible get the status of individual files directly to
5632 * avoid crawling the entire work tree once per rebased commit.
5633 * TODO: Ideally, merged_paths would contain a list of commitables
5634 * we could use so we could skip worktree_status() entirely.
5637 struct got_pathlist_entry *pe;
5638 TAILQ_FOREACH(pe, merged_paths, entry) {
5639 err = worktree_status(worktree, pe->path, fileindex,
5640 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
5646 err = worktree_status(worktree, "", fileindex, repo,
5647 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5652 if (TAILQ_EMPTY(&commitable_paths)) {
5653 /* No-op change; commit will be elided. */
5654 err = got_ref_delete(commit_ref, repo);
5657 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5661 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5665 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5670 logmsg = strdup(new_logmsg);
5671 if (logmsg == NULL) {
5672 err = got_error_from_errno("strdup");
5676 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
5681 /* NB: commit_worktree will call free(logmsg) */
5682 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
5683 worktree, got_object_commit_get_author(orig_commit),
5684 got_object_commit_get_committer(orig_commit),
5685 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
5689 err = got_ref_change_ref(tmp_branch, *new_commit_id);
5693 err = got_ref_delete(commit_ref, repo);
5697 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5699 sync_err = sync_fileindex(fileindex, fileindex_path);
5700 if (sync_err && err == NULL)
5703 free(fileindex_path);
5704 free(head_commit_id);
5706 got_ref_close(head_ref);
5708 free(*new_commit_id);
5709 *new_commit_id = NULL;
5714 const struct got_error *
5715 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
5716 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5717 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5718 struct got_commit_object *orig_commit,
5719 struct got_object_id *orig_commit_id, struct got_repository *repo)
5721 const struct got_error *err;
5722 char *commit_ref_name;
5723 struct got_reference *commit_ref = NULL;
5724 struct got_object_id *commit_id = NULL;
5726 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5730 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5733 err = got_ref_resolve(&commit_id, repo, commit_ref);
5736 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5737 err = got_error(GOT_ERR_REBASE_COMMITID);
5741 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5742 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
5745 got_ref_close(commit_ref);
5746 free(commit_ref_name);
5751 const struct got_error *
5752 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
5753 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5754 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5755 struct got_commit_object *orig_commit,
5756 struct got_object_id *orig_commit_id, const char *new_logmsg,
5757 struct got_repository *repo)
5759 const struct got_error *err;
5760 char *commit_ref_name;
5761 struct got_reference *commit_ref = NULL;
5763 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5767 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5771 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5772 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
5775 got_ref_close(commit_ref);
5776 free(commit_ref_name);
5780 const struct got_error *
5781 got_worktree_rebase_postpone(struct got_worktree *worktree,
5782 struct got_fileindex *fileindex)
5785 got_fileindex_free(fileindex);
5786 return lock_worktree(worktree, LOCK_SH);
5789 static const struct got_error *
5790 delete_ref(const char *name, struct got_repository *repo)
5792 const struct got_error *err;
5793 struct got_reference *ref;
5795 err = got_ref_open(&ref, repo, name, 0);
5797 if (err->code == GOT_ERR_NOT_REF)
5802 err = got_ref_delete(ref, repo);
5807 static const struct got_error *
5808 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
5810 const struct got_error *err;
5811 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5812 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5814 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5817 err = delete_ref(tmp_branch_name, repo);
5821 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5824 err = delete_ref(new_base_branch_ref_name, repo);
5828 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5831 err = delete_ref(branch_ref_name, repo);
5835 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5838 err = delete_ref(commit_ref_name, repo);
5843 free(tmp_branch_name);
5844 free(new_base_branch_ref_name);
5845 free(branch_ref_name);
5846 free(commit_ref_name);
5850 const struct got_error *
5851 got_worktree_rebase_complete(struct got_worktree *worktree,
5852 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
5853 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
5854 struct got_repository *repo)
5856 const struct got_error *err, *unlockerr;
5857 struct got_object_id *new_head_commit_id = NULL;
5859 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5863 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
5867 err = got_ref_write(rebased_branch, repo);
5871 err = got_worktree_set_head_ref(worktree, rebased_branch);
5875 err = delete_rebase_refs(worktree, repo);
5878 got_fileindex_free(fileindex);
5879 free(new_head_commit_id);
5880 unlockerr = lock_worktree(worktree, LOCK_SH);
5881 if (unlockerr && err == NULL)
5886 const struct got_error *
5887 got_worktree_rebase_abort(struct got_worktree *worktree,
5888 struct got_fileindex *fileindex, struct got_repository *repo,
5889 struct got_reference *new_base_branch,
5890 got_worktree_checkout_cb progress_cb, void *progress_arg)
5892 const struct got_error *err, *unlockerr, *sync_err;
5893 struct got_reference *resolved = NULL;
5894 struct got_object_id *commit_id = NULL;
5895 char *fileindex_path = NULL;
5896 struct revert_file_args rfa;
5897 struct got_object_id *tree_id = NULL;
5899 err = lock_worktree(worktree, LOCK_EX);
5903 err = got_ref_open(&resolved, repo,
5904 got_ref_get_symref_target(new_base_branch), 0);
5908 err = got_worktree_set_head_ref(worktree, resolved);
5913 * XXX commits to the base branch could have happened while
5914 * we were busy rebasing; should we store the original commit ID
5915 * when rebase begins and read it back here?
5917 err = got_ref_resolve(&commit_id, repo, resolved);
5921 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5925 err = got_object_id_by_path(&tree_id, repo,
5926 worktree->base_commit_id, worktree->path_prefix);
5930 err = delete_rebase_refs(worktree, repo);
5934 err = get_fileindex_path(&fileindex_path, worktree);
5938 rfa.worktree = worktree;
5939 rfa.fileindex = fileindex;
5940 rfa.progress_cb = progress_cb;
5941 rfa.progress_arg = progress_arg;
5942 rfa.patch_cb = NULL;
5943 rfa.patch_arg = NULL;
5945 err = worktree_status(worktree, "", fileindex, repo,
5946 revert_file, &rfa, NULL, NULL, 0, 0);
5950 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5951 repo, progress_cb, progress_arg, NULL, NULL);
5953 sync_err = sync_fileindex(fileindex, fileindex_path);
5954 if (sync_err && err == NULL)
5957 got_ref_close(resolved);
5961 got_fileindex_free(fileindex);
5962 free(fileindex_path);
5964 unlockerr = lock_worktree(worktree, LOCK_SH);
5965 if (unlockerr && err == NULL)
5970 const struct got_error *
5971 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
5972 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
5973 struct got_fileindex **fileindex, struct got_worktree *worktree,
5974 struct got_repository *repo)
5976 const struct got_error *err = NULL;
5977 char *tmp_branch_name = NULL;
5978 char *branch_ref_name = NULL;
5979 char *base_commit_ref_name = NULL;
5980 char *fileindex_path = NULL;
5981 struct check_rebase_ok_arg ok_arg;
5982 struct got_reference *wt_branch = NULL;
5983 struct got_reference *base_commit_ref = NULL;
5987 *base_commit_id = NULL;
5990 err = lock_worktree(worktree, LOCK_EX);
5994 err = open_fileindex(fileindex, &fileindex_path, worktree);
5998 ok_arg.worktree = worktree;
6000 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6005 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6009 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6013 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6018 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6023 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6027 err = got_ref_write(*branch_ref, repo);
6031 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6032 worktree->base_commit_id);
6035 err = got_ref_write(base_commit_ref, repo);
6038 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6039 if (*base_commit_id == NULL) {
6040 err = got_error_from_errno("got_object_id_dup");
6044 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6045 worktree->base_commit_id);
6048 err = got_ref_write(*tmp_branch, repo);
6052 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6056 free(fileindex_path);
6057 free(tmp_branch_name);
6058 free(branch_ref_name);
6059 free(base_commit_ref_name);
6061 got_ref_close(wt_branch);
6064 got_ref_close(*branch_ref);
6068 got_ref_close(*tmp_branch);
6071 free(*base_commit_id);
6073 got_fileindex_free(*fileindex);
6076 lock_worktree(worktree, LOCK_SH);
6081 const struct got_error *
6082 got_worktree_histedit_postpone(struct got_worktree *worktree,
6083 struct got_fileindex *fileindex)
6086 got_fileindex_free(fileindex);
6087 return lock_worktree(worktree, LOCK_SH);
6090 const struct got_error *
6091 got_worktree_histedit_in_progress(int *in_progress,
6092 struct got_worktree *worktree)
6094 const struct got_error *err;
6095 char *tmp_branch_name = NULL;
6097 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6101 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6102 free(tmp_branch_name);
6106 const struct got_error *
6107 got_worktree_histedit_continue(struct got_object_id **commit_id,
6108 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6109 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6110 struct got_worktree *worktree, struct got_repository *repo)
6112 const struct got_error *err;
6113 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6114 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6115 struct got_reference *commit_ref = NULL;
6116 struct got_reference *base_commit_ref = NULL;
6117 char *fileindex_path = NULL;
6118 int have_staged_files = 0;
6122 *base_commit_id = NULL;
6125 err = lock_worktree(worktree, LOCK_EX);
6129 err = open_fileindex(fileindex, &fileindex_path, worktree);
6133 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6134 &have_staged_files);
6135 if (err && err->code != GOT_ERR_CANCELLED)
6137 if (have_staged_files) {
6138 err = got_error(GOT_ERR_STAGED_PATHS);
6142 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6146 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6150 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6154 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6159 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6163 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6166 err = got_ref_resolve(commit_id, repo, commit_ref);
6170 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6173 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6177 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6181 free(commit_ref_name);
6182 free(branch_ref_name);
6183 free(fileindex_path);
6185 got_ref_close(commit_ref);
6186 if (base_commit_ref)
6187 got_ref_close(base_commit_ref);
6191 free(*base_commit_id);
6192 *base_commit_id = NULL;
6194 got_ref_close(*tmp_branch);
6198 got_fileindex_free(*fileindex);
6201 lock_worktree(worktree, LOCK_EX);
6206 static const struct got_error *
6207 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6209 const struct got_error *err;
6210 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6211 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6213 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6216 err = delete_ref(tmp_branch_name, repo);
6220 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6224 err = delete_ref(base_commit_ref_name, repo);
6228 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6231 err = delete_ref(branch_ref_name, repo);
6235 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6238 err = delete_ref(commit_ref_name, repo);
6242 free(tmp_branch_name);
6243 free(base_commit_ref_name);
6244 free(branch_ref_name);
6245 free(commit_ref_name);
6249 const struct got_error *
6250 got_worktree_histedit_abort(struct got_worktree *worktree,
6251 struct got_fileindex *fileindex, struct got_repository *repo,
6252 struct got_reference *branch, struct got_object_id *base_commit_id,
6253 got_worktree_checkout_cb progress_cb, void *progress_arg)
6255 const struct got_error *err, *unlockerr, *sync_err;
6256 struct got_reference *resolved = NULL;
6257 char *fileindex_path = NULL;
6258 struct got_object_id *tree_id = NULL;
6259 struct revert_file_args rfa;
6261 err = lock_worktree(worktree, LOCK_EX);
6265 err = got_ref_open(&resolved, repo,
6266 got_ref_get_symref_target(branch), 0);
6270 err = got_worktree_set_head_ref(worktree, resolved);
6274 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6278 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6279 worktree->path_prefix);
6283 err = delete_histedit_refs(worktree, repo);
6287 err = get_fileindex_path(&fileindex_path, worktree);
6291 rfa.worktree = worktree;
6292 rfa.fileindex = fileindex;
6293 rfa.progress_cb = progress_cb;
6294 rfa.progress_arg = progress_arg;
6295 rfa.patch_cb = NULL;
6296 rfa.patch_arg = NULL;
6298 err = worktree_status(worktree, "", fileindex, repo,
6299 revert_file, &rfa, NULL, NULL, 0, 0);
6303 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6304 repo, progress_cb, progress_arg, NULL, NULL);
6306 sync_err = sync_fileindex(fileindex, fileindex_path);
6307 if (sync_err && err == NULL)
6310 got_ref_close(resolved);
6312 free(fileindex_path);
6314 unlockerr = lock_worktree(worktree, LOCK_SH);
6315 if (unlockerr && err == NULL)
6320 const struct got_error *
6321 got_worktree_histedit_complete(struct got_worktree *worktree,
6322 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6323 struct got_reference *edited_branch, struct got_repository *repo)
6325 const struct got_error *err, *unlockerr;
6326 struct got_object_id *new_head_commit_id = NULL;
6327 struct got_reference *resolved = NULL;
6329 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6333 err = got_ref_open(&resolved, repo,
6334 got_ref_get_symref_target(edited_branch), 0);
6338 err = got_ref_change_ref(resolved, new_head_commit_id);
6342 err = got_ref_write(resolved, repo);
6346 err = got_worktree_set_head_ref(worktree, resolved);
6350 err = delete_histedit_refs(worktree, repo);
6353 got_fileindex_free(fileindex);
6354 free(new_head_commit_id);
6355 unlockerr = lock_worktree(worktree, LOCK_SH);
6356 if (unlockerr && err == NULL)
6361 const struct got_error *
6362 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6363 struct got_object_id *commit_id, struct got_repository *repo)
6365 const struct got_error *err;
6366 char *commit_ref_name;
6368 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6372 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6376 err = delete_ref(commit_ref_name, repo);
6378 free(commit_ref_name);
6382 const struct got_error *
6383 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6384 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6385 struct got_worktree *worktree, const char *refname,
6386 struct got_repository *repo)
6388 const struct got_error *err = NULL;
6389 char *fileindex_path = NULL;
6390 struct check_rebase_ok_arg ok_arg;
6394 *base_branch_ref = NULL;
6396 err = lock_worktree(worktree, LOCK_EX);
6400 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6401 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6402 "cannot integrate a branch into itself; "
6403 "update -b or different branch name required");
6407 err = open_fileindex(fileindex, &fileindex_path, worktree);
6411 /* Preconditions are the same as for rebase. */
6412 ok_arg.worktree = worktree;
6414 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6419 err = got_ref_open(branch_ref, repo, refname, 1);
6423 err = got_ref_open(base_branch_ref, repo,
6424 got_worktree_get_head_ref_name(worktree), 1);
6428 got_ref_close(*branch_ref);
6431 if (*base_branch_ref) {
6432 got_ref_close(*base_branch_ref);
6433 *base_branch_ref = NULL;
6436 got_fileindex_free(*fileindex);
6439 lock_worktree(worktree, LOCK_SH);
6444 const struct got_error *
6445 got_worktree_integrate_continue(struct got_worktree *worktree,
6446 struct got_fileindex *fileindex, struct got_repository *repo,
6447 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6448 got_worktree_checkout_cb progress_cb, void *progress_arg,
6449 got_cancel_cb cancel_cb, void *cancel_arg)
6451 const struct got_error *err = NULL, *sync_err, *unlockerr;
6452 char *fileindex_path = NULL;
6453 struct got_object_id *tree_id = NULL, *commit_id = NULL;
6455 err = get_fileindex_path(&fileindex_path, worktree);
6459 err = got_ref_resolve(&commit_id, repo, branch_ref);
6463 err = got_object_id_by_path(&tree_id, repo, commit_id,
6464 worktree->path_prefix);
6468 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6472 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6473 progress_cb, progress_arg, cancel_cb, cancel_arg);
6477 err = got_ref_change_ref(base_branch_ref, commit_id);
6481 err = got_ref_write(base_branch_ref, repo);
6483 sync_err = sync_fileindex(fileindex, fileindex_path);
6484 if (sync_err && err == NULL)
6488 unlockerr = got_ref_unlock(branch_ref);
6489 if (unlockerr && err == NULL)
6491 got_ref_close(branch_ref);
6493 unlockerr = got_ref_unlock(base_branch_ref);
6494 if (unlockerr && err == NULL)
6496 got_ref_close(base_branch_ref);
6498 got_fileindex_free(fileindex);
6499 free(fileindex_path);
6502 unlockerr = lock_worktree(worktree, LOCK_SH);
6503 if (unlockerr && err == NULL)
6508 const struct got_error *
6509 got_worktree_integrate_abort(struct got_worktree *worktree,
6510 struct got_fileindex *fileindex, struct got_repository *repo,
6511 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
6513 const struct got_error *err = NULL, *unlockerr = NULL;
6515 got_fileindex_free(fileindex);
6517 err = lock_worktree(worktree, LOCK_SH);
6519 unlockerr = got_ref_unlock(branch_ref);
6520 if (unlockerr && err == NULL)
6522 got_ref_close(branch_ref);
6524 unlockerr = got_ref_unlock(base_branch_ref);
6525 if (unlockerr && err == NULL)
6527 got_ref_close(base_branch_ref);
6532 struct check_stage_ok_arg {
6533 struct got_object_id *head_commit_id;
6534 struct got_worktree *worktree;
6535 struct got_fileindex *fileindex;
6536 struct got_repository *repo;
6540 const struct got_error *
6541 check_stage_ok(void *arg, unsigned char status,
6542 unsigned char staged_status, const char *relpath,
6543 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6544 struct got_object_id *commit_id, int dirfd, const char *de_name)
6546 struct check_stage_ok_arg *a = arg;
6547 const struct got_error *err = NULL;
6548 struct got_fileindex_entry *ie;
6549 struct got_object_id base_commit_id;
6550 struct got_object_id *base_commit_idp = NULL;
6551 char *in_repo_path = NULL, *p;
6553 if (status == GOT_STATUS_UNVERSIONED ||
6554 status == GOT_STATUS_NO_CHANGE)
6556 if (status == GOT_STATUS_NONEXISTENT)
6557 return got_error_set_errno(ENOENT, relpath);
6559 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6561 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6563 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
6564 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
6566 return got_error_from_errno("asprintf");
6568 if (got_fileindex_entry_has_commit(ie)) {
6569 memcpy(base_commit_id.sha1, ie->commit_sha1,
6570 SHA1_DIGEST_LENGTH);
6571 base_commit_idp = &base_commit_id;
6574 if (status == GOT_STATUS_CONFLICT) {
6575 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
6577 } else if (status != GOT_STATUS_ADD &&
6578 status != GOT_STATUS_MODIFY &&
6579 status != GOT_STATUS_DELETE) {
6580 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
6584 a->have_changes = 1;
6589 err = check_out_of_date(p, status, staged_status,
6590 blob_id, base_commit_idp, a->head_commit_id, a->repo,
6591 GOT_ERR_STAGE_OUT_OF_DATE);
6597 struct stage_path_arg {
6598 struct got_worktree *worktree;
6599 struct got_fileindex *fileindex;
6600 struct got_repository *repo;
6601 got_worktree_status_cb status_cb;
6603 got_worktree_patch_cb patch_cb;
6605 int staged_something;
6608 static const struct got_error *
6609 stage_path(void *arg, unsigned char status,
6610 unsigned char staged_status, const char *relpath,
6611 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6612 struct got_object_id *commit_id, int dirfd, const char *de_name)
6614 struct stage_path_arg *a = arg;
6615 const struct got_error *err = NULL;
6616 struct got_fileindex_entry *ie;
6617 char *ondisk_path = NULL, *path_content = NULL;
6619 struct got_object_id *new_staged_blob_id = NULL;
6621 if (status == GOT_STATUS_UNVERSIONED)
6624 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6626 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6628 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
6630 return got_error_from_errno("asprintf");
6633 case GOT_STATUS_ADD:
6634 case GOT_STATUS_MODIFY:
6636 if (status == GOT_STATUS_ADD) {
6637 int choice = GOT_PATCH_CHOICE_NONE;
6638 err = (*a->patch_cb)(&choice, a->patch_arg,
6639 status, ie->path, NULL, 1, 1);
6642 if (choice != GOT_PATCH_CHOICE_YES)
6645 err = create_patched_content(&path_content, 0,
6646 staged_blob_id ? staged_blob_id : blob_id,
6647 ondisk_path, dirfd, de_name, ie->path,
6648 a->repo, a->patch_cb, a->patch_arg);
6649 if (err || path_content == NULL)
6653 err = got_object_blob_create(&new_staged_blob_id,
6654 path_content ? path_content : ondisk_path, a->repo);
6657 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
6658 SHA1_DIGEST_LENGTH);
6659 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
6660 stage = GOT_FILEIDX_STAGE_ADD;
6662 stage = GOT_FILEIDX_STAGE_MODIFY;
6663 got_fileindex_entry_stage_set(ie, stage);
6664 a->staged_something = 1;
6665 if (a->status_cb == NULL)
6667 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6668 get_staged_status(ie), relpath, blob_id,
6669 new_staged_blob_id, NULL, dirfd, de_name);
6671 case GOT_STATUS_DELETE:
6672 if (staged_status == GOT_STATUS_DELETE)
6675 int choice = GOT_PATCH_CHOICE_NONE;
6676 err = (*a->patch_cb)(&choice, a->patch_arg, status,
6677 ie->path, NULL, 1, 1);
6680 if (choice == GOT_PATCH_CHOICE_NO)
6682 if (choice != GOT_PATCH_CHOICE_YES) {
6683 err = got_error(GOT_ERR_PATCH_CHOICE);
6687 stage = GOT_FILEIDX_STAGE_DELETE;
6688 got_fileindex_entry_stage_set(ie, stage);
6689 a->staged_something = 1;
6690 if (a->status_cb == NULL)
6692 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6693 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
6696 case GOT_STATUS_NO_CHANGE:
6698 case GOT_STATUS_CONFLICT:
6699 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
6701 case GOT_STATUS_NONEXISTENT:
6702 err = got_error_set_errno(ENOENT, relpath);
6705 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
6709 if (path_content && unlink(path_content) == -1 && err == NULL)
6710 err = got_error_from_errno2("unlink", path_content);
6713 free(new_staged_blob_id);
6717 const struct got_error *
6718 got_worktree_stage(struct got_worktree *worktree,
6719 struct got_pathlist_head *paths,
6720 got_worktree_status_cb status_cb, void *status_arg,
6721 got_worktree_patch_cb patch_cb, void *patch_arg,
6722 struct got_repository *repo)
6724 const struct got_error *err = NULL, *sync_err, *unlockerr;
6725 struct got_pathlist_entry *pe;
6726 struct got_fileindex *fileindex = NULL;
6727 char *fileindex_path = NULL;
6728 struct got_reference *head_ref = NULL;
6729 struct got_object_id *head_commit_id = NULL;
6730 struct check_stage_ok_arg oka;
6731 struct stage_path_arg spa;
6733 err = lock_worktree(worktree, LOCK_EX);
6737 err = got_ref_open(&head_ref, repo,
6738 got_worktree_get_head_ref_name(worktree), 0);
6741 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6744 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6748 /* Check pre-conditions before staging anything. */
6749 oka.head_commit_id = head_commit_id;
6750 oka.worktree = worktree;
6751 oka.fileindex = fileindex;
6753 oka.have_changes = 0;
6754 TAILQ_FOREACH(pe, paths, entry) {
6755 err = worktree_status(worktree, pe->path, fileindex, repo,
6756 check_stage_ok, &oka, NULL, NULL, 0, 0);
6760 if (!oka.have_changes) {
6761 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6765 spa.worktree = worktree;
6766 spa.fileindex = fileindex;
6768 spa.patch_cb = patch_cb;
6769 spa.patch_arg = patch_arg;
6770 spa.status_cb = status_cb;
6771 spa.status_arg = status_arg;
6772 spa.staged_something = 0;
6773 TAILQ_FOREACH(pe, paths, entry) {
6774 err = worktree_status(worktree, pe->path, fileindex, repo,
6775 stage_path, &spa, NULL, NULL, 0, 0);
6779 if (!spa.staged_something) {
6780 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6784 sync_err = sync_fileindex(fileindex, fileindex_path);
6785 if (sync_err && err == NULL)
6789 got_ref_close(head_ref);
6790 free(head_commit_id);
6791 free(fileindex_path);
6793 got_fileindex_free(fileindex);
6794 unlockerr = lock_worktree(worktree, LOCK_SH);
6795 if (unlockerr && err == NULL)
6800 struct unstage_path_arg {
6801 struct got_worktree *worktree;
6802 struct got_fileindex *fileindex;
6803 struct got_repository *repo;
6804 got_worktree_checkout_cb progress_cb;
6806 got_worktree_patch_cb patch_cb;
6810 static const struct got_error *
6811 create_unstaged_content(char **path_unstaged_content,
6812 char **path_new_staged_content, struct got_object_id *blob_id,
6813 struct got_object_id *staged_blob_id, const char *relpath,
6814 struct got_repository *repo,
6815 got_worktree_patch_cb patch_cb, void *patch_arg)
6817 const struct got_error *err;
6818 struct got_blob_object *blob = NULL, *staged_blob = NULL;
6819 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
6820 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
6821 struct stat sb1, sb2;
6822 struct got_diff_changes *changes = NULL;
6823 struct got_diff_state *ds = NULL;
6824 struct got_diff_args *args = NULL;
6825 struct got_diff_change *change;
6826 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
6827 int have_content = 0, have_rejected_content = 0;
6829 *path_unstaged_content = NULL;
6830 *path_new_staged_content = NULL;
6832 err = got_object_id_str(&label1, blob_id);
6835 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
6839 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
6843 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
6847 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
6851 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
6855 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
6859 if (stat(path1, &sb1) == -1) {
6860 err = got_error_from_errno2("stat", path1);
6864 if (stat(path2, &sb2) == -1) {
6865 err = got_error_from_errno2("stat", path2);
6869 err = got_diff_files(&changes, &ds, &args, &diff_flags,
6870 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
6874 err = got_opentemp_named(path_unstaged_content, &outfile,
6875 "got-unstaged-content");
6878 err = got_opentemp_named(path_new_staged_content, &rejectfile,
6879 "got-new-staged-content");
6883 if (fseek(f1, 0L, SEEK_SET) == -1) {
6884 err = got_ferror(f1, GOT_ERR_IO);
6887 if (fseek(f2, 0L, SEEK_SET) == -1) {
6888 err = got_ferror(f2, GOT_ERR_IO);
6891 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
6893 err = apply_or_reject_change(&choice, change, ++n,
6894 changes->nchanges, ds, args, diff_flags, relpath,
6895 f1, f2, &line_cur1, &line_cur2,
6896 outfile, rejectfile, patch_cb, patch_arg);
6899 if (choice == GOT_PATCH_CHOICE_YES)
6902 have_rejected_content = 1;
6903 if (choice == GOT_PATCH_CHOICE_QUIT)
6906 if (have_content || have_rejected_content)
6907 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
6908 outfile, rejectfile);
6912 got_object_blob_close(blob);
6914 got_object_blob_close(staged_blob);
6915 if (f1 && fclose(f1) == EOF && err == NULL)
6916 err = got_error_from_errno2("fclose", path1);
6917 if (f2 && fclose(f2) == EOF && err == NULL)
6918 err = got_error_from_errno2("fclose", path2);
6919 if (outfile && fclose(outfile) == EOF && err == NULL)
6920 err = got_error_from_errno2("fclose", *path_unstaged_content);
6921 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
6922 err = got_error_from_errno2("fclose", *path_new_staged_content);
6923 if (path1 && unlink(path1) == -1 && err == NULL)
6924 err = got_error_from_errno2("unlink", path1);
6925 if (path2 && unlink(path2) == -1 && err == NULL)
6926 err = got_error_from_errno2("unlink", path2);
6927 if (err || !have_content) {
6928 if (*path_unstaged_content &&
6929 unlink(*path_unstaged_content) == -1 && err == NULL)
6930 err = got_error_from_errno2("unlink",
6931 *path_unstaged_content);
6932 free(*path_unstaged_content);
6933 *path_unstaged_content = NULL;
6935 if (err || !have_rejected_content) {
6936 if (*path_new_staged_content &&
6937 unlink(*path_new_staged_content) == -1 && err == NULL)
6938 err = got_error_from_errno2("unlink",
6939 *path_new_staged_content);
6940 free(*path_new_staged_content);
6941 *path_new_staged_content = NULL;
6945 got_diff_state_free(ds);
6949 got_diff_free_changes(changes);
6955 static const struct got_error *
6956 unstage_path(void *arg, unsigned char status,
6957 unsigned char staged_status, const char *relpath,
6958 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6959 struct got_object_id *commit_id, int dirfd, const char *de_name)
6961 const struct got_error *err = NULL;
6962 struct unstage_path_arg *a = arg;
6963 struct got_fileindex_entry *ie;
6964 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
6965 char *ondisk_path = NULL, *path_unstaged_content = NULL;
6966 char *path_new_staged_content = NULL;
6967 char *id_str = NULL, *label_orig = NULL;
6968 int local_changes_subsumed;
6971 if (staged_status != GOT_STATUS_ADD &&
6972 staged_status != GOT_STATUS_MODIFY &&
6973 staged_status != GOT_STATUS_DELETE)
6976 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6978 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6980 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
6982 return got_error_from_errno("asprintf");
6984 err = got_object_id_str(&id_str,
6985 commit_id ? commit_id : a->worktree->base_commit_id);
6988 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
6990 err = got_error_from_errno("asprintf");
6994 switch (staged_status) {
6995 case GOT_STATUS_MODIFY:
6996 err = got_object_open_as_blob(&blob_base, a->repo,
7001 case GOT_STATUS_ADD:
7003 if (staged_status == GOT_STATUS_ADD) {
7004 int choice = GOT_PATCH_CHOICE_NONE;
7005 err = (*a->patch_cb)(&choice, a->patch_arg,
7006 staged_status, ie->path, NULL, 1, 1);
7009 if (choice != GOT_PATCH_CHOICE_YES)
7012 err = create_unstaged_content(
7013 &path_unstaged_content,
7014 &path_new_staged_content, blob_id,
7015 staged_blob_id, ie->path, a->repo,
7016 a->patch_cb, a->patch_arg);
7017 if (err || path_unstaged_content == NULL)
7019 if (path_new_staged_content) {
7020 err = got_object_blob_create(
7022 path_new_staged_content,
7026 memcpy(ie->staged_blob_sha1,
7027 staged_blob_id->sha1,
7028 SHA1_DIGEST_LENGTH);
7030 err = merge_file(&local_changes_subsumed,
7031 a->worktree, blob_base, ondisk_path,
7032 relpath, got_fileindex_perms_to_st(ie),
7033 path_unstaged_content, label_orig,
7034 "unstaged", a->repo, a->progress_cb,
7037 path_new_staged_content == NULL)
7038 got_fileindex_entry_stage_set(ie,
7039 GOT_FILEIDX_STAGE_NONE);
7040 break; /* Done with this file. */
7043 err = got_object_open_as_blob(&blob_staged, a->repo,
7044 staged_blob_id, 8192);
7047 err = merge_blob(&local_changes_subsumed, a->worktree,
7048 blob_base, ondisk_path, relpath,
7049 got_fileindex_perms_to_st(ie), label_orig, blob_staged,
7050 commit_id ? commit_id : a->worktree->base_commit_id,
7051 a->repo, a->progress_cb, a->progress_arg);
7053 got_fileindex_entry_stage_set(ie,
7054 GOT_FILEIDX_STAGE_NONE);
7056 case GOT_STATUS_DELETE:
7058 int choice = GOT_PATCH_CHOICE_NONE;
7059 err = (*a->patch_cb)(&choice, a->patch_arg,
7060 staged_status, ie->path, NULL, 1, 1);
7063 if (choice == GOT_PATCH_CHOICE_NO)
7065 if (choice != GOT_PATCH_CHOICE_YES) {
7066 err = got_error(GOT_ERR_PATCH_CHOICE);
7070 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7071 err = get_file_status(&status, &sb, ie, ondisk_path,
7072 dirfd, de_name, a->repo);
7075 err = (*a->progress_cb)(a->progress_arg, status, relpath);
7080 if (path_unstaged_content &&
7081 unlink(path_unstaged_content) == -1 && err == NULL)
7082 err = got_error_from_errno2("unlink", path_unstaged_content);
7083 if (path_new_staged_content &&
7084 unlink(path_new_staged_content) == -1 && err == NULL)
7085 err = got_error_from_errno2("unlink", path_new_staged_content);
7086 free(path_unstaged_content);
7087 free(path_new_staged_content);
7089 got_object_blob_close(blob_base);
7091 got_object_blob_close(blob_staged);
7097 const struct got_error *
7098 got_worktree_unstage(struct got_worktree *worktree,
7099 struct got_pathlist_head *paths,
7100 got_worktree_checkout_cb progress_cb, void *progress_arg,
7101 got_worktree_patch_cb patch_cb, void *patch_arg,
7102 struct got_repository *repo)
7104 const struct got_error *err = NULL, *sync_err, *unlockerr;
7105 struct got_pathlist_entry *pe;
7106 struct got_fileindex *fileindex = NULL;
7107 char *fileindex_path = NULL;
7108 struct unstage_path_arg upa;
7110 err = lock_worktree(worktree, LOCK_EX);
7114 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7118 upa.worktree = worktree;
7119 upa.fileindex = fileindex;
7121 upa.progress_cb = progress_cb;
7122 upa.progress_arg = progress_arg;
7123 upa.patch_cb = patch_cb;
7124 upa.patch_arg = patch_arg;
7125 TAILQ_FOREACH(pe, paths, entry) {
7126 err = worktree_status(worktree, pe->path, fileindex, repo,
7127 unstage_path, &upa, NULL, NULL, 0, 0);
7132 sync_err = sync_fileindex(fileindex, fileindex_path);
7133 if (sync_err && err == NULL)
7136 free(fileindex_path);
7138 got_fileindex_free(fileindex);
7139 unlockerr = lock_worktree(worktree, LOCK_SH);
7140 if (unlockerr && err == NULL)