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, int installing_bad_symlink,
1029 struct got_repository *repo,
1030 got_worktree_checkout_cb progress_cb, void *progress_arg);
1032 static const struct got_error *
1033 install_symlink(struct got_worktree *worktree, const char *ondisk_path,
1034 const char *path, mode_t te_mode, mode_t st_mode,
1035 struct got_blob_object *blob, int restoring_missing_file,
1036 int reverting_versioned_file, struct got_repository *repo,
1037 got_worktree_checkout_cb progress_cb, void *progress_arg)
1039 const struct got_error *err = NULL;
1040 char target_path[PATH_MAX];
1041 size_t len, target_len = 0;
1042 char *resolved_path = NULL, *abspath = NULL;
1043 char *path_got = NULL;
1044 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1045 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1048 * Blob object content specifies the target path of the link.
1049 * If a symbolic link cannot be installed we instead create
1050 * a regular file which contains the link target path stored
1051 * in the blob object.
1054 err = got_object_blob_read_block(&len, blob);
1055 if (len + target_len >= sizeof(target_path)) {
1056 /* Path too long; install as a regular file. */
1057 got_object_blob_rewind(blob);
1058 return install_blob(worktree, ondisk_path, path,
1059 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1060 restoring_missing_file, reverting_versioned_file,
1061 1, repo, progress_cb, progress_arg);
1064 /* Skip blob object header first time around. */
1065 memcpy(target_path + target_len, buf + hdrlen,
1067 target_len += len - hdrlen;
1071 target_path[target_len] = '\0';
1074 * Relative symlink target lookup should begin at the directory
1075 * in which the blob object is being installed.
1077 if (!got_path_is_absolute(target_path)) {
1078 char *parent = dirname(ondisk_path);
1079 if (parent == NULL) {
1080 err = got_error_from_errno2("dirname", ondisk_path);
1083 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1084 err = got_error_from_errno("asprintf");
1090 * unveil(2) restricts our view of paths in the filesystem.
1091 * ENOENT will occur if a link target path does not exist or
1092 * if it points outside our unveiled path space.
1094 resolved_path = realpath(abspath ? abspath : target_path, NULL);
1095 if (resolved_path == NULL) {
1096 if (errno != ENOENT) {
1097 err = got_error_from_errno2("realpath", target_path);
1102 /* Only allow symlinks pointing at paths within the work tree. */
1103 if (!got_path_is_child(resolved_path ? resolved_path : (abspath ?
1104 abspath : target_path), worktree->root_path,
1105 strlen(worktree->root_path))) {
1106 /* install as a regular file */
1107 got_object_blob_rewind(blob);
1108 err = install_blob(worktree, ondisk_path, path,
1109 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1110 restoring_missing_file, reverting_versioned_file, 1,
1111 repo, progress_cb, progress_arg);
1115 /* Do not allow symlinks pointing into the .got directory. */
1116 if (asprintf(&path_got, "%s/%s", worktree->root_path,
1117 GOT_WORKTREE_GOT_DIR) == -1) {
1118 err = got_error_from_errno("asprintf");
1121 if (got_path_is_child(resolved_path ? resolved_path : (abspath ?
1122 abspath : target_path), path_got, strlen(path_got))) {
1123 /* install as a regular file */
1124 got_object_blob_rewind(blob);
1125 err = install_blob(worktree, ondisk_path, path,
1126 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1127 restoring_missing_file, reverting_versioned_file, 1,
1128 repo, progress_cb, progress_arg);
1132 if (symlink(target_path, ondisk_path) == -1) {
1133 if (errno == EEXIST) {
1136 char etarget[PATH_MAX];
1137 if (lstat(ondisk_path, &sb) == -1) {
1138 err = got_error_from_errno2("lstat",
1142 if (!S_ISLNK(sb.st_mode)) {
1143 err = got_error_path(ondisk_path,
1144 GOT_ERR_FILE_OBSTRUCTED);
1147 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1149 err = got_error_from_errno2("readlink",
1153 if (elen == target_len &&
1154 memcmp(etarget, target_path, target_len) == 0) {
1155 err = NULL; /* nothing to do */
1158 err = update_symlink(ondisk_path, target_path,
1163 err = (*progress_cb)(progress_arg,
1164 GOT_STATUS_UPDATE, path);
1170 if (errno == ENOENT) {
1171 char *parent = dirname(ondisk_path);
1172 if (parent == NULL) {
1173 err = got_error_from_errno2("dirname",
1177 err = add_dir_on_disk(worktree, parent);
1181 * Retry, and fall through to error handling
1182 * below if this second attempt fails.
1184 if (symlink(target_path, ondisk_path) != -1) {
1185 err = NULL; /* success */
1190 /* Handle errors from first or second creation attempt. */
1191 if (errno == ENAMETOOLONG) {
1192 /* bad target path; install as a regular file */
1193 got_object_blob_rewind(blob);
1194 err = install_blob(worktree, ondisk_path, path,
1195 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1196 restoring_missing_file, reverting_versioned_file, 1,
1197 repo, progress_cb, progress_arg);
1198 } else if (errno == ENOTDIR) {
1199 err = got_error_path(ondisk_path,
1200 GOT_ERR_FILE_OBSTRUCTED);
1202 err = got_error_from_errno3("symlink",
1203 target_path, ondisk_path);
1205 } else if (progress_cb)
1206 err = (*progress_cb)(progress_arg, GOT_STATUS_ADD, path);
1208 free(resolved_path);
1214 static const struct got_error *
1215 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1216 const char *path, mode_t te_mode, mode_t st_mode,
1217 struct got_blob_object *blob, int restoring_missing_file,
1218 int reverting_versioned_file, int installing_bad_symlink,
1219 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1222 const struct got_error *err = NULL;
1226 char *tmppath = NULL;
1228 if (S_ISLNK(te_mode))
1229 return install_symlink(worktree, ondisk_path, path, te_mode,
1230 st_mode, blob, restoring_missing_file,
1231 reverting_versioned_file, repo, progress_cb, progress_arg);
1233 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1234 GOT_DEFAULT_FILE_MODE);
1236 if (errno == ENOENT) {
1237 char *parent = dirname(path);
1239 return got_error_from_errno2("dirname", path);
1240 err = add_dir_on_disk(worktree, parent);
1243 fd = open(ondisk_path,
1244 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1245 GOT_DEFAULT_FILE_MODE);
1247 return got_error_from_errno2("open",
1249 } else if (errno == EEXIST) {
1250 if (!S_ISREG(st_mode) && !installing_bad_symlink) {
1251 /* TODO file is obstructed; do something */
1252 err = got_error_path(ondisk_path,
1253 GOT_ERR_FILE_OBSTRUCTED);
1256 err = got_opentemp_named_fd(&tmppath, &fd,
1263 return got_error_from_errno2("open", ondisk_path);
1267 if (restoring_missing_file)
1268 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1270 else if (reverting_versioned_file)
1271 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1274 err = (*progress_cb)(progress_arg,
1275 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1280 hdrlen = got_object_blob_get_hdrlen(blob);
1282 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1283 err = got_object_blob_read_block(&len, blob);
1287 /* Skip blob object header first time around. */
1288 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1290 err = got_error_from_errno("write");
1292 } else if (outlen != len - hdrlen) {
1293 err = got_error(GOT_ERR_IO);
1300 if (fsync(fd) != 0) {
1301 err = got_error_from_errno("fsync");
1306 if (rename(tmppath, ondisk_path) != 0) {
1307 err = got_error_from_errno3("rename", tmppath,
1314 if (chmod(ondisk_path,
1315 get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1316 err = got_error_from_errno2("chmod", ondisk_path);
1321 if (fd != -1 && close(fd) != 0 && err == NULL)
1322 err = got_error_from_errno("close");
1327 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1328 static const struct got_error *
1329 get_modified_file_content_status(unsigned char *status, FILE *f)
1331 const struct got_error *err = NULL;
1332 const char *markers[3] = {
1333 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1334 GOT_DIFF_CONFLICT_MARKER_SEP,
1335 GOT_DIFF_CONFLICT_MARKER_END
1340 const char delim[3] = {'\0', '\0', '\0'};
1342 while (*status == GOT_STATUS_MODIFY) {
1343 line = fparseln(f, &len, NULL, delim, 0);
1347 err = got_ferror(f, GOT_ERR_IO);
1351 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1352 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1354 *status = GOT_STATUS_CONFLICT;
1364 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1366 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1367 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1371 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1373 return !(ie->ctime_sec == sb->st_ctime &&
1374 ie->ctime_nsec == sb->st_ctimensec &&
1375 ie->mtime_sec == sb->st_mtime &&
1376 ie->mtime_nsec == sb->st_mtimensec &&
1377 ie->size == (sb->st_size & 0xffffffff) &&
1378 !xbit_differs(ie, sb->st_mode));
1381 static unsigned char
1382 get_staged_status(struct got_fileindex_entry *ie)
1384 switch (got_fileindex_entry_stage_get(ie)) {
1385 case GOT_FILEIDX_STAGE_ADD:
1386 return GOT_STATUS_ADD;
1387 case GOT_FILEIDX_STAGE_DELETE:
1388 return GOT_STATUS_DELETE;
1389 case GOT_FILEIDX_STAGE_MODIFY:
1390 return GOT_STATUS_MODIFY;
1392 return GOT_STATUS_NO_CHANGE;
1396 static const struct got_error *
1397 get_symlink_status(unsigned char *status, struct stat *sb,
1398 struct got_fileindex_entry *ie, const char *abspath,
1399 int dirfd, const char *de_name, struct got_blob_object *blob)
1401 const struct got_error *err = NULL;
1402 char target_path[PATH_MAX];
1403 char etarget[PATH_MAX];
1405 size_t len, target_len = 0;
1406 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1407 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1409 *status = GOT_STATUS_NO_CHANGE;
1411 /* Blob object content specifies the target path of the link. */
1413 err = got_object_blob_read_block(&len, blob);
1416 if (len + target_len >= sizeof(target_path)) {
1418 * Should not happen. The blob contents were OK
1419 * when this symlink was installed.
1421 return got_error(GOT_ERR_NO_SPACE);
1424 /* Skip blob object header first time around. */
1425 memcpy(target_path + target_len, buf + hdrlen,
1427 target_len += len - hdrlen;
1431 target_path[target_len] = '\0';
1434 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1436 return got_error_from_errno2("readlinkat", abspath);
1438 elen = readlink(abspath, etarget, sizeof(etarget));
1440 return got_error_from_errno2("readlinkat", abspath);
1443 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1444 *status = GOT_STATUS_MODIFY;
1449 static const struct got_error *
1450 get_file_status(unsigned char *status, struct stat *sb,
1451 struct got_fileindex_entry *ie, const char *abspath,
1452 int dirfd, const char *de_name, struct got_repository *repo)
1454 const struct got_error *err = NULL;
1455 struct got_object_id id;
1460 struct got_blob_object *blob = NULL;
1462 unsigned char staged_status = get_staged_status(ie);
1464 *status = GOT_STATUS_NO_CHANGE;
1467 * Whenever the caller provides a directory descriptor and a
1468 * directory entry name for the file, use them! This prevents
1469 * race conditions if filesystem paths change beneath our feet.
1472 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1473 if (errno == ENOENT) {
1474 if (got_fileindex_entry_has_file_on_disk(ie))
1475 *status = GOT_STATUS_MISSING;
1477 *status = GOT_STATUS_DELETE;
1480 err = got_error_from_errno2("fstatat", abspath);
1484 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1485 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1486 return got_error_from_errno2("open", abspath);
1487 else if (fd == -1 && errno == ELOOP) {
1488 if (lstat(abspath, sb) == -1)
1489 return got_error_from_errno2("lstat", abspath);
1490 } else if (fd == -1 || fstat(fd, sb) == -1) {
1491 if (errno == ENOENT) {
1492 if (got_fileindex_entry_has_file_on_disk(ie))
1493 *status = GOT_STATUS_MISSING;
1495 *status = GOT_STATUS_DELETE;
1498 err = got_error_from_errno2("fstat", abspath);
1503 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1504 *status = GOT_STATUS_OBSTRUCTED;
1508 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1509 *status = GOT_STATUS_DELETE;
1511 } else if (!got_fileindex_entry_has_blob(ie) &&
1512 staged_status != GOT_STATUS_ADD) {
1513 *status = GOT_STATUS_ADD;
1517 if (!stat_info_differs(ie, sb))
1520 if (staged_status == GOT_STATUS_MODIFY ||
1521 staged_status == GOT_STATUS_ADD)
1522 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1524 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1526 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1530 if (S_ISLNK(sb->st_mode)) {
1531 /* Staging changes to symlinks is not yet(?) supported. */
1532 if (staged_status != GOT_STATUS_NO_CHANGE) {
1533 err = got_error_path(abspath, GOT_ERR_FILE_STATUS);
1536 err = get_symlink_status(status, sb, ie, abspath, dirfd,
1543 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1545 err = got_error_from_errno2("openat", abspath);
1550 f = fdopen(fd, "r");
1552 err = got_error_from_errno2("fdopen", abspath);
1556 hdrlen = got_object_blob_get_hdrlen(blob);
1558 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1559 err = got_object_blob_read_block(&blen, blob);
1562 /* Skip length of blob object header first time around. */
1563 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1564 if (flen == 0 && ferror(f)) {
1565 err = got_error_from_errno("fread");
1570 *status = GOT_STATUS_MODIFY;
1572 } else if (flen == 0) {
1574 *status = GOT_STATUS_MODIFY;
1576 } else if (blen - hdrlen == flen) {
1577 /* Skip blob object header first time around. */
1578 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1579 *status = GOT_STATUS_MODIFY;
1583 *status = GOT_STATUS_MODIFY;
1589 if (*status == GOT_STATUS_MODIFY) {
1591 err = get_modified_file_content_status(status, f);
1592 } else if (xbit_differs(ie, sb->st_mode))
1593 *status = GOT_STATUS_MODE_CHANGE;
1596 got_object_blob_close(blob);
1597 if (f != NULL && fclose(f) == EOF && err == NULL)
1598 err = got_error_from_errno2("fclose", abspath);
1599 if (fd != -1 && close(fd) == -1 && err == NULL)
1600 err = got_error_from_errno2("close", abspath);
1605 * Update timestamps in the file index if a file is unmodified and
1606 * we had to run a full content comparison to find out.
1608 static const struct got_error *
1609 sync_timestamps(char *ondisk_path, unsigned char status,
1610 struct got_fileindex_entry *ie, struct stat *sb)
1612 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1613 return got_fileindex_entry_update(ie, ondisk_path,
1614 ie->blob_sha1, ie->commit_sha1, 1);
1619 static const struct got_error *
1620 update_blob(struct got_worktree *worktree,
1621 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1622 struct got_tree_entry *te, const char *path,
1623 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1626 const struct got_error *err = NULL;
1627 struct got_blob_object *blob = NULL;
1629 unsigned char status = GOT_STATUS_NO_CHANGE;
1632 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1633 return got_error_from_errno("asprintf");
1636 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1637 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1640 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1644 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1645 sb.st_mode = got_fileindex_perms_to_st(ie);
1647 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1649 if (status == GOT_STATUS_OBSTRUCTED) {
1650 err = (*progress_cb)(progress_arg, status, path);
1653 if (status == GOT_STATUS_CONFLICT) {
1654 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1659 if (ie && status != GOT_STATUS_MISSING &&
1660 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1661 if (got_fileindex_entry_has_commit(ie) &&
1662 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1663 SHA1_DIGEST_LENGTH) == 0) {
1664 err = sync_timestamps(ondisk_path, status, ie, &sb);
1667 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1671 if (got_fileindex_entry_has_blob(ie) &&
1672 memcmp(ie->blob_sha1, te->id.sha1,
1673 SHA1_DIGEST_LENGTH) == 0) {
1674 err = sync_timestamps(ondisk_path, status, ie, &sb);
1679 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1683 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1684 int update_timestamps;
1685 struct got_blob_object *blob2 = NULL;
1686 char *label_orig = NULL;
1687 if (got_fileindex_entry_has_blob(ie)) {
1688 struct got_object_id id2;
1689 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1690 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1694 if (got_fileindex_entry_has_commit(ie)) {
1695 char id_str[SHA1_DIGEST_STRING_LENGTH];
1696 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1697 sizeof(id_str)) == NULL) {
1698 err = got_error_path(id_str,
1699 GOT_ERR_BAD_OBJ_ID_STR);
1702 if (asprintf(&label_orig, "%s: commit %s",
1703 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1704 err = got_error_from_errno("asprintf");
1708 err = merge_blob(&update_timestamps, worktree, blob2,
1709 ondisk_path, path, sb.st_mode, label_orig, blob,
1710 worktree->base_commit_id, repo,
1711 progress_cb, progress_arg);
1714 got_object_blob_close(blob2);
1718 * Do not update timestamps of files with local changes.
1719 * Otherwise, a future status walk would treat them as
1720 * unmodified files again.
1722 err = got_fileindex_entry_update(ie, ondisk_path,
1723 blob->id.sha1, worktree->base_commit_id->sha1,
1725 } else if (status == GOT_STATUS_MODE_CHANGE) {
1726 err = got_fileindex_entry_update(ie, ondisk_path,
1727 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1728 } else if (status == GOT_STATUS_DELETE) {
1729 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1732 err = got_fileindex_entry_update(ie, ondisk_path,
1733 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1737 err = install_blob(worktree, ondisk_path, path, te->mode,
1738 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0, 0,
1739 repo, progress_cb, progress_arg);
1743 err = got_fileindex_entry_update(ie, ondisk_path,
1744 blob->id.sha1, worktree->base_commit_id->sha1, 1);
1746 err = create_fileindex_entry(fileindex,
1747 worktree->base_commit_id, ondisk_path, path,
1753 got_object_blob_close(blob);
1759 static const struct got_error *
1760 remove_ondisk_file(const char *root_path, const char *path)
1762 const struct got_error *err = NULL;
1763 char *ondisk_path = NULL;
1765 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1766 return got_error_from_errno("asprintf");
1768 if (unlink(ondisk_path) == -1) {
1769 if (errno != ENOENT)
1770 err = got_error_from_errno2("unlink", ondisk_path);
1772 char *parent = dirname(ondisk_path);
1773 while (parent && strcmp(parent, root_path) != 0) {
1774 if (rmdir(parent) == -1) {
1775 if (errno != ENOTEMPTY)
1776 err = got_error_from_errno2("rmdir",
1780 parent = dirname(parent);
1787 static const struct got_error *
1788 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1789 struct got_fileindex_entry *ie, struct got_repository *repo,
1790 got_worktree_checkout_cb progress_cb, void *progress_arg)
1792 const struct got_error *err = NULL;
1793 unsigned char status;
1797 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1798 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1800 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1802 return got_error_from_errno("asprintf");
1804 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
1808 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1809 status == GOT_STATUS_ADD) {
1810 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1814 * Preserve the working file and change the deleted blob's
1815 * entry into a schedule-add entry.
1817 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1820 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1823 if (status == GOT_STATUS_NO_CHANGE) {
1824 err = remove_ondisk_file(worktree->root_path, ie->path);
1828 got_fileindex_entry_remove(fileindex, ie);
1835 struct diff_cb_arg {
1836 struct got_fileindex *fileindex;
1837 struct got_worktree *worktree;
1838 struct got_repository *repo;
1839 got_worktree_checkout_cb progress_cb;
1841 got_cancel_cb cancel_cb;
1845 static const struct got_error *
1846 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1847 struct got_tree_entry *te, const char *parent_path)
1849 struct diff_cb_arg *a = arg;
1851 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1852 return got_error(GOT_ERR_CANCELLED);
1854 return update_blob(a->worktree, a->fileindex, ie, te,
1855 ie->path, a->repo, a->progress_cb, a->progress_arg);
1858 static const struct got_error *
1859 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1861 struct diff_cb_arg *a = arg;
1863 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1864 return got_error(GOT_ERR_CANCELLED);
1866 return delete_blob(a->worktree, a->fileindex, ie,
1867 a->repo, a->progress_cb, a->progress_arg);
1870 static const struct got_error *
1871 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1873 struct diff_cb_arg *a = arg;
1874 const struct got_error *err;
1877 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1878 return got_error(GOT_ERR_CANCELLED);
1880 if (got_object_tree_entry_is_submodule(te))
1883 if (asprintf(&path, "%s%s%s", parent_path,
1884 parent_path[0] ? "/" : "", te->name)
1886 return got_error_from_errno("asprintf");
1888 if (S_ISDIR(te->mode))
1889 err = add_dir_on_disk(a->worktree, path);
1891 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1892 a->repo, a->progress_cb, a->progress_arg);
1898 static const struct got_error *
1899 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1901 const struct got_error *err = NULL;
1902 char *uuidstr = NULL;
1903 uint32_t uuid_status;
1907 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1908 if (uuid_status != uuid_s_ok)
1909 return got_error_uuid(uuid_status, "uuid_to_string");
1911 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1913 err = got_error_from_errno("asprintf");
1920 const struct got_error *
1921 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1923 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1926 static const struct got_error *
1927 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1929 return get_ref_name(refname, worktree,
1930 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1933 static const struct got_error *
1934 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1936 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1939 static const struct got_error *
1940 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1942 return get_ref_name(refname, worktree,
1943 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1946 static const struct got_error *
1947 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1949 return get_ref_name(refname, worktree,
1950 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1953 static const struct got_error *
1954 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1956 return get_ref_name(refname, worktree,
1957 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1960 static const struct got_error *
1961 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1963 return get_ref_name(refname, worktree,
1964 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1967 static const struct got_error *
1968 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1970 return get_ref_name(refname, worktree,
1971 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1974 static const struct got_error *
1975 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1977 return get_ref_name(refname, worktree,
1978 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1981 const struct got_error *
1982 got_worktree_get_histedit_script_path(char **path,
1983 struct got_worktree *worktree)
1985 if (asprintf(path, "%s/%s/%s", worktree->root_path,
1986 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
1988 return got_error_from_errno("asprintf");
1994 * Prevent Git's garbage collector from deleting our base commit by
1995 * setting a reference to our base commit's ID.
1997 static const struct got_error *
1998 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2000 const struct got_error *err = NULL;
2001 struct got_reference *ref = NULL;
2004 err = got_worktree_get_base_ref_name(&refname, worktree);
2008 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2012 err = got_ref_write(ref, repo);
2020 static const struct got_error *
2021 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2023 const struct got_error *err = NULL;
2025 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2026 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2027 err = got_error_from_errno("asprintf");
2028 *fileindex_path = NULL;
2034 static const struct got_error *
2035 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2036 struct got_worktree *worktree)
2038 const struct got_error *err = NULL;
2041 *fileindex_path = NULL;
2042 *fileindex = got_fileindex_alloc();
2043 if (*fileindex == NULL)
2044 return got_error_from_errno("got_fileindex_alloc");
2046 err = get_fileindex_path(fileindex_path, worktree);
2050 index = fopen(*fileindex_path, "rb");
2051 if (index == NULL) {
2052 if (errno != ENOENT)
2053 err = got_error_from_errno2("fopen", *fileindex_path);
2055 err = got_fileindex_read(*fileindex, index);
2056 if (fclose(index) != 0 && err == NULL)
2057 err = got_error_from_errno("fclose");
2061 free(*fileindex_path);
2062 *fileindex_path = NULL;
2063 got_fileindex_free(*fileindex);
2069 struct bump_base_commit_id_arg {
2070 struct got_object_id *base_commit_id;
2073 const char *entry_name;
2074 got_worktree_checkout_cb progress_cb;
2078 /* Bump base commit ID of all files within an updated part of the work tree. */
2079 static const struct got_error *
2080 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2082 const struct got_error *err;
2083 struct bump_base_commit_id_arg *a = arg;
2085 if (a->entry_name) {
2086 if (strcmp(ie->path, a->path) != 0)
2088 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2091 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2092 SHA1_DIGEST_LENGTH) == 0)
2095 if (a->progress_cb) {
2096 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2101 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2105 static const struct got_error *
2106 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2108 const struct got_error *err = NULL;
2109 char *new_fileindex_path = NULL;
2110 FILE *new_index = NULL;
2111 struct timespec timeout;
2113 err = got_opentemp_named(&new_fileindex_path, &new_index,
2118 err = got_fileindex_write(fileindex, new_index);
2122 if (rename(new_fileindex_path, fileindex_path) != 0) {
2123 err = got_error_from_errno3("rename", new_fileindex_path,
2125 unlink(new_fileindex_path);
2129 * Sleep for a short amount of time to ensure that files modified after
2130 * this program exits have a different time stamp from the one which
2131 * was recorded in the file index.
2134 timeout.tv_nsec = 1;
2135 nanosleep(&timeout, NULL);
2139 free(new_fileindex_path);
2143 static const struct got_error *
2144 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2145 struct got_object_id **tree_id, const char *wt_relpath,
2146 struct got_worktree *worktree, struct got_repository *repo)
2148 const struct got_error *err = NULL;
2149 struct got_object_id *id = NULL;
2150 char *in_repo_path = NULL;
2151 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2153 *entry_type = GOT_OBJ_TYPE_ANY;
2154 *tree_relpath = NULL;
2157 if (wt_relpath[0] == '\0') {
2158 /* Check out all files within the work tree. */
2159 *entry_type = GOT_OBJ_TYPE_TREE;
2160 *tree_relpath = strdup("");
2161 if (*tree_relpath == NULL) {
2162 err = got_error_from_errno("strdup");
2165 err = got_object_id_by_path(tree_id, repo,
2166 worktree->base_commit_id, worktree->path_prefix);
2172 /* Check out a subset of files in the work tree. */
2174 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2175 is_root_wt ? "" : "/", wt_relpath) == -1) {
2176 err = got_error_from_errno("asprintf");
2180 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2186 in_repo_path = NULL;
2188 err = got_object_get_type(entry_type, repo, id);
2192 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2193 /* Check out a single file. */
2194 if (strchr(wt_relpath, '/') == NULL) {
2195 /* Check out a single file in work tree's root dir. */
2196 in_repo_path = strdup(worktree->path_prefix);
2197 if (in_repo_path == NULL) {
2198 err = got_error_from_errno("strdup");
2201 *tree_relpath = strdup("");
2202 if (*tree_relpath == NULL) {
2203 err = got_error_from_errno("strdup");
2207 /* Check out a single file in a subdirectory. */
2208 err = got_path_dirname(tree_relpath, wt_relpath);
2211 if (asprintf(&in_repo_path, "%s%s%s",
2212 worktree->path_prefix, is_root_wt ? "" : "/",
2213 *tree_relpath) == -1) {
2214 err = got_error_from_errno("asprintf");
2218 err = got_object_id_by_path(tree_id, repo,
2219 worktree->base_commit_id, in_repo_path);
2221 /* Check out all files within a subdirectory. */
2222 *tree_id = got_object_id_dup(id);
2223 if (*tree_id == NULL) {
2224 err = got_error_from_errno("got_object_id_dup");
2227 *tree_relpath = strdup(wt_relpath);
2228 if (*tree_relpath == NULL) {
2229 err = got_error_from_errno("strdup");
2237 *entry_type = GOT_OBJ_TYPE_ANY;
2238 free(*tree_relpath);
2239 *tree_relpath = NULL;
2246 static const struct got_error *
2247 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2248 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2249 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2250 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2252 const struct got_error *err = NULL;
2253 struct got_commit_object *commit = NULL;
2254 struct got_tree_object *tree = NULL;
2255 struct got_fileindex_diff_tree_cb diff_cb;
2256 struct diff_cb_arg arg;
2258 err = ref_base_commit(worktree, repo);
2260 if (!(err->code == GOT_ERR_ERRNO &&
2261 (errno == EACCES || errno == EROFS)))
2263 err = (*progress_cb)(progress_arg,
2264 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2269 err = got_object_open_as_commit(&commit, repo,
2270 worktree->base_commit_id);
2274 err = got_object_open_as_tree(&tree, repo, tree_id);
2279 got_object_tree_find_entry(tree, entry_name) == NULL) {
2280 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2284 diff_cb.diff_old_new = diff_old_new;
2285 diff_cb.diff_old = diff_old;
2286 diff_cb.diff_new = diff_new;
2287 arg.fileindex = fileindex;
2288 arg.worktree = worktree;
2290 arg.progress_cb = progress_cb;
2291 arg.progress_arg = progress_arg;
2292 arg.cancel_cb = cancel_cb;
2293 arg.cancel_arg = cancel_arg;
2294 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2295 entry_name, repo, &diff_cb, &arg);
2298 got_object_tree_close(tree);
2300 got_object_commit_close(commit);
2304 const struct got_error *
2305 got_worktree_checkout_files(struct got_worktree *worktree,
2306 struct got_pathlist_head *paths, struct got_repository *repo,
2307 got_worktree_checkout_cb progress_cb, void *progress_arg,
2308 got_cancel_cb cancel_cb, void *cancel_arg)
2310 const struct got_error *err = NULL, *sync_err, *unlockerr;
2311 struct got_commit_object *commit = NULL;
2312 struct got_tree_object *tree = NULL;
2313 struct got_fileindex *fileindex = NULL;
2314 char *fileindex_path = NULL;
2315 struct got_pathlist_entry *pe;
2316 struct tree_path_data {
2317 SIMPLEQ_ENTRY(tree_path_data) entry;
2318 struct got_object_id *tree_id;
2323 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2325 SIMPLEQ_INIT(&tree_paths);
2327 err = lock_worktree(worktree, LOCK_EX);
2331 /* Map all specified paths to in-repository trees. */
2332 TAILQ_FOREACH(pe, paths, entry) {
2333 tpd = malloc(sizeof(*tpd));
2335 err = got_error_from_errno("malloc");
2339 err = find_tree_entry_for_checkout(&tpd->entry_type,
2340 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2346 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2347 err = got_path_basename(&tpd->entry_name, pe->path);
2355 tpd->entry_name = NULL;
2357 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2361 * Read the file index.
2362 * Checking out files is supposed to be an idempotent operation.
2363 * If the on-disk file index is incomplete we will try to complete it.
2365 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2369 tpd = SIMPLEQ_FIRST(&tree_paths);
2370 TAILQ_FOREACH(pe, paths, entry) {
2371 struct bump_base_commit_id_arg bbc_arg;
2373 err = checkout_files(worktree, fileindex, tpd->relpath,
2374 tpd->tree_id, tpd->entry_name, repo,
2375 progress_cb, progress_arg, cancel_cb, cancel_arg);
2379 bbc_arg.base_commit_id = worktree->base_commit_id;
2380 bbc_arg.entry_name = tpd->entry_name;
2381 bbc_arg.path = pe->path;
2382 bbc_arg.path_len = pe->path_len;
2383 bbc_arg.progress_cb = progress_cb;
2384 bbc_arg.progress_arg = progress_arg;
2385 err = got_fileindex_for_each_entry_safe(fileindex,
2386 bump_base_commit_id, &bbc_arg);
2390 tpd = SIMPLEQ_NEXT(tpd, entry);
2392 sync_err = sync_fileindex(fileindex, fileindex_path);
2393 if (sync_err && err == NULL)
2396 free(fileindex_path);
2398 got_object_tree_close(tree);
2400 got_object_commit_close(commit);
2402 got_fileindex_free(fileindex);
2403 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2404 tpd = SIMPLEQ_FIRST(&tree_paths);
2405 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2410 unlockerr = lock_worktree(worktree, LOCK_SH);
2411 if (unlockerr && err == NULL)
2416 struct merge_file_cb_arg {
2417 struct got_worktree *worktree;
2418 struct got_fileindex *fileindex;
2419 got_worktree_checkout_cb progress_cb;
2421 got_cancel_cb cancel_cb;
2423 const char *label_orig;
2424 struct got_object_id *commit_id2;
2427 static const struct got_error *
2428 merge_file_cb(void *arg, struct got_blob_object *blob1,
2429 struct got_blob_object *blob2, struct got_object_id *id1,
2430 struct got_object_id *id2, const char *path1, const char *path2,
2431 mode_t mode1, mode_t mode2, struct got_repository *repo)
2433 static const struct got_error *err = NULL;
2434 struct merge_file_cb_arg *a = arg;
2435 struct got_fileindex_entry *ie;
2436 char *ondisk_path = NULL;
2438 unsigned char status;
2439 int local_changes_subsumed;
2441 if (blob1 && blob2) {
2442 ie = got_fileindex_entry_get(a->fileindex, path2,
2445 return (*a->progress_cb)(a->progress_arg,
2446 GOT_STATUS_MISSING, path2);
2448 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2450 return got_error_from_errno("asprintf");
2452 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2457 if (status == GOT_STATUS_DELETE) {
2458 err = (*a->progress_cb)(a->progress_arg,
2459 GOT_STATUS_MERGE, path2);
2462 if (status != GOT_STATUS_NO_CHANGE &&
2463 status != GOT_STATUS_MODIFY &&
2464 status != GOT_STATUS_CONFLICT &&
2465 status != GOT_STATUS_ADD) {
2466 err = (*a->progress_cb)(a->progress_arg, status, path2);
2470 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2471 err = merge_symlink(a->worktree, blob1,
2472 ondisk_path, path2, sb.st_mode, a->label_orig,
2473 blob2, a->commit_id2, repo, a->progress_cb,
2476 err = merge_blob(&local_changes_subsumed, a->worktree,
2477 blob1, ondisk_path, path2, sb.st_mode,
2478 a->label_orig, blob2, a->commit_id2, repo,
2479 a->progress_cb, a->progress_arg);
2482 ie = got_fileindex_entry_get(a->fileindex, path1,
2485 return (*a->progress_cb)(a->progress_arg,
2486 GOT_STATUS_MISSING, path1);
2488 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2490 return got_error_from_errno("asprintf");
2492 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2498 case GOT_STATUS_NO_CHANGE:
2499 err = (*a->progress_cb)(a->progress_arg,
2500 GOT_STATUS_DELETE, path1);
2503 err = remove_ondisk_file(a->worktree->root_path, path1);
2507 got_fileindex_entry_mark_deleted_from_disk(ie);
2509 case GOT_STATUS_DELETE:
2510 case GOT_STATUS_MISSING:
2511 err = (*a->progress_cb)(a->progress_arg,
2512 GOT_STATUS_DELETE, path1);
2516 got_fileindex_entry_mark_deleted_from_disk(ie);
2518 case GOT_STATUS_ADD:
2519 case GOT_STATUS_MODIFY:
2520 case GOT_STATUS_CONFLICT:
2521 err = (*a->progress_cb)(a->progress_arg,
2522 GOT_STATUS_CANNOT_DELETE, path1);
2526 case GOT_STATUS_OBSTRUCTED:
2527 err = (*a->progress_cb)(a->progress_arg, status, path1);
2535 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2537 return got_error_from_errno("asprintf");
2538 ie = got_fileindex_entry_get(a->fileindex, path2,
2541 err = get_file_status(&status, &sb, ie, ondisk_path,
2545 if (status != GOT_STATUS_NO_CHANGE &&
2546 status != GOT_STATUS_MODIFY &&
2547 status != GOT_STATUS_CONFLICT &&
2548 status != GOT_STATUS_ADD) {
2549 err = (*a->progress_cb)(a->progress_arg,
2553 err = merge_blob(&local_changes_subsumed, a->worktree,
2554 NULL, ondisk_path, path2, sb.st_mode,
2555 a->label_orig, blob2, a->commit_id2, repo,
2558 if (status == GOT_STATUS_DELETE) {
2559 err = got_fileindex_entry_update(ie,
2560 ondisk_path, blob2->id.sha1,
2561 a->worktree->base_commit_id->sha1, 0);
2566 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2567 err = install_blob(a->worktree, ondisk_path, path2,
2568 /* XXX get this from parent tree! */
2569 GOT_DEFAULT_FILE_MODE,
2570 sb.st_mode, blob2, 0, 0, 0, repo,
2571 a->progress_cb, a->progress_arg);
2574 err = got_fileindex_entry_alloc(&ie, path2);
2577 err = got_fileindex_entry_update(ie, ondisk_path,
2580 got_fileindex_entry_free(ie);
2583 err = got_fileindex_entry_add(a->fileindex, ie);
2585 got_fileindex_entry_free(ie);
2595 struct check_merge_ok_arg {
2596 struct got_worktree *worktree;
2597 struct got_repository *repo;
2600 static const struct got_error *
2601 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2603 const struct got_error *err = NULL;
2604 struct check_merge_ok_arg *a = arg;
2605 unsigned char status;
2609 /* Reject merges into a work tree with mixed base commits. */
2610 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2611 SHA1_DIGEST_LENGTH))
2612 return got_error(GOT_ERR_MIXED_COMMITS);
2614 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2616 return got_error_from_errno("asprintf");
2618 /* Reject merges into a work tree with conflicted files. */
2619 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2622 if (status == GOT_STATUS_CONFLICT)
2623 return got_error(GOT_ERR_CONFLICTS);
2628 static const struct got_error *
2629 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2630 const char *fileindex_path, struct got_object_id *commit_id1,
2631 struct got_object_id *commit_id2, struct got_repository *repo,
2632 got_worktree_checkout_cb progress_cb, void *progress_arg,
2633 got_cancel_cb cancel_cb, void *cancel_arg)
2635 const struct got_error *err = NULL, *sync_err;
2636 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2637 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2638 struct merge_file_cb_arg arg;
2639 char *label_orig = NULL;
2644 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2645 worktree->path_prefix);
2649 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2653 err = got_object_id_str(&id_str, commit_id1);
2657 if (asprintf(&label_orig, "%s: commit %s",
2658 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2659 err = got_error_from_errno("asprintf");
2666 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2667 worktree->path_prefix);
2671 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2675 arg.worktree = worktree;
2676 arg.fileindex = fileindex;
2677 arg.progress_cb = progress_cb;
2678 arg.progress_arg = progress_arg;
2679 arg.cancel_cb = cancel_cb;
2680 arg.cancel_arg = cancel_arg;
2681 arg.label_orig = label_orig;
2682 arg.commit_id2 = commit_id2;
2683 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2684 sync_err = sync_fileindex(fileindex, fileindex_path);
2685 if (sync_err && err == NULL)
2689 got_object_tree_close(tree1);
2691 got_object_tree_close(tree2);
2696 const struct got_error *
2697 got_worktree_merge_files(struct got_worktree *worktree,
2698 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2699 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2700 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2702 const struct got_error *err, *unlockerr;
2703 char *fileindex_path = NULL;
2704 struct got_fileindex *fileindex = NULL;
2705 struct check_merge_ok_arg mok_arg;
2707 err = lock_worktree(worktree, LOCK_EX);
2711 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2715 mok_arg.worktree = worktree;
2716 mok_arg.repo = repo;
2717 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2722 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2723 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2726 got_fileindex_free(fileindex);
2727 free(fileindex_path);
2728 unlockerr = lock_worktree(worktree, LOCK_SH);
2729 if (unlockerr && err == NULL)
2734 struct diff_dir_cb_arg {
2735 struct got_fileindex *fileindex;
2736 struct got_worktree *worktree;
2737 const char *status_path;
2738 size_t status_path_len;
2739 struct got_repository *repo;
2740 got_worktree_status_cb status_cb;
2742 got_cancel_cb cancel_cb;
2744 /* A pathlist containing per-directory pathlists of ignore patterns. */
2745 struct got_pathlist_head ignores;
2746 int report_unchanged;
2750 static const struct got_error *
2751 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2752 int dirfd, const char *de_name,
2753 got_worktree_status_cb status_cb, void *status_arg,
2754 struct got_repository *repo, int report_unchanged)
2756 const struct got_error *err = NULL;
2757 unsigned char status = GOT_STATUS_NO_CHANGE;
2758 unsigned char staged_status = get_staged_status(ie);
2760 struct got_object_id blob_id, commit_id, staged_blob_id;
2761 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2762 struct got_object_id *staged_blob_idp = NULL;
2764 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
2768 if (status == GOT_STATUS_NO_CHANGE &&
2769 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
2772 if (got_fileindex_entry_has_blob(ie)) {
2773 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2774 blob_idp = &blob_id;
2776 if (got_fileindex_entry_has_commit(ie)) {
2777 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2778 commit_idp = &commit_id;
2780 if (staged_status == GOT_STATUS_ADD ||
2781 staged_status == GOT_STATUS_MODIFY) {
2782 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2783 SHA1_DIGEST_LENGTH);
2784 staged_blob_idp = &staged_blob_id;
2787 return (*status_cb)(status_arg, status, staged_status,
2788 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
2791 static const struct got_error *
2792 status_old_new(void *arg, struct got_fileindex_entry *ie,
2793 struct dirent *de, const char *parent_path, int dirfd)
2795 const struct got_error *err = NULL;
2796 struct diff_dir_cb_arg *a = arg;
2799 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2800 return got_error(GOT_ERR_CANCELLED);
2802 if (got_path_cmp(parent_path, a->status_path,
2803 strlen(parent_path), a->status_path_len) != 0 &&
2804 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2807 if (parent_path[0]) {
2808 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2809 parent_path, de->d_name) == -1)
2810 return got_error_from_errno("asprintf");
2812 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2814 return got_error_from_errno("asprintf");
2817 err = report_file_status(ie, abspath, dirfd, de->d_name,
2818 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
2823 static const struct got_error *
2824 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2826 struct diff_dir_cb_arg *a = arg;
2827 struct got_object_id blob_id, commit_id;
2828 unsigned char status;
2830 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2831 return got_error(GOT_ERR_CANCELLED);
2833 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2836 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2837 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2838 if (got_fileindex_entry_has_file_on_disk(ie))
2839 status = GOT_STATUS_MISSING;
2841 status = GOT_STATUS_DELETE;
2842 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2843 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
2847 free_ignorelist(struct got_pathlist_head *ignorelist)
2849 struct got_pathlist_entry *pe;
2851 TAILQ_FOREACH(pe, ignorelist, entry)
2852 free((char *)pe->path);
2853 got_pathlist_free(ignorelist);
2857 free_ignores(struct got_pathlist_head *ignores)
2859 struct got_pathlist_entry *pe;
2861 TAILQ_FOREACH(pe, ignores, entry) {
2862 struct got_pathlist_head *ignorelist = pe->data;
2863 free_ignorelist(ignorelist);
2864 free((char *)pe->path);
2866 got_pathlist_free(ignores);
2869 static const struct got_error *
2870 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
2872 const struct got_error *err = NULL;
2873 struct got_pathlist_entry *pe = NULL;
2874 struct got_pathlist_head *ignorelist;
2875 char *line = NULL, *pattern, *dirpath = NULL;
2876 size_t linesize = 0;
2879 ignorelist = calloc(1, sizeof(*ignorelist));
2880 if (ignorelist == NULL)
2881 return got_error_from_errno("calloc");
2882 TAILQ_INIT(ignorelist);
2884 while ((linelen = getline(&line, &linesize, f)) != -1) {
2885 if (linelen > 0 && line[linelen - 1] == '\n')
2886 line[linelen - 1] = '\0';
2888 /* Git's ignores may contain comments. */
2892 /* Git's negated patterns are not (yet?) supported. */
2896 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
2898 err = got_error_from_errno("asprintf");
2901 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
2906 err = got_error_from_errno("getline");
2910 dirpath = strdup(path);
2911 if (dirpath == NULL) {
2912 err = got_error_from_errno("strdup");
2915 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
2918 if (err || pe == NULL) {
2920 free_ignorelist(ignorelist);
2926 match_ignores(struct got_pathlist_head *ignores, const char *path)
2928 struct got_pathlist_entry *pe;
2930 /* Handle patterns which match in all directories. */
2931 TAILQ_FOREACH(pe, ignores, entry) {
2932 struct got_pathlist_head *ignorelist = pe->data;
2933 struct got_pathlist_entry *pi;
2935 TAILQ_FOREACH(pi, ignorelist, entry) {
2936 const char *p, *pattern = pi->path;
2938 if (strncmp(pattern, "**/", 3) != 0)
2943 if (fnmatch(pattern, p,
2944 FNM_PATHNAME | FNM_LEADING_DIR)) {
2945 /* Retry in next directory. */
2946 while (*p && *p != '/')
2958 * The ignores pathlist contains ignore lists from children before
2959 * parents, so we can find the most specific ignorelist by walking
2960 * ignores backwards.
2962 pe = TAILQ_LAST(ignores, got_pathlist_head);
2964 if (got_path_is_child(path, pe->path, pe->path_len)) {
2965 struct got_pathlist_head *ignorelist = pe->data;
2966 struct got_pathlist_entry *pi;
2967 TAILQ_FOREACH(pi, ignorelist, entry) {
2968 const char *pattern = pi->path;
2969 int flags = FNM_LEADING_DIR;
2970 if (strstr(pattern, "/**/") == NULL)
2971 flags |= FNM_PATHNAME;
2972 if (fnmatch(pattern, path, flags))
2977 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
2983 static const struct got_error *
2984 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
2985 const char *path, int dirfd, const char *ignores_filename)
2987 const struct got_error *err = NULL;
2990 FILE *ignoresfile = NULL;
2992 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
2993 path[0] ? "/" : "", ignores_filename) == -1)
2994 return got_error_from_errno("asprintf");
2997 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
2999 if (errno != ENOENT && errno != EACCES)
3000 err = got_error_from_errno2("openat",
3003 ignoresfile = fdopen(fd, "r");
3004 if (ignoresfile == NULL)
3005 err = got_error_from_errno2("fdopen",
3009 err = read_ignores(ignores, path, ignoresfile);
3013 ignoresfile = fopen(ignorespath, "r");
3014 if (ignoresfile == NULL) {
3015 if (errno != ENOENT && errno != EACCES)
3016 err = got_error_from_errno2("fopen",
3019 err = read_ignores(ignores, path, ignoresfile);
3022 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3023 err = got_error_from_errno2("fclose", path);
3024 if (fd != -1 && close(fd) == -1 && err == NULL)
3025 err = got_error_from_errno2("close", path);
3030 static const struct got_error *
3031 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3033 const struct got_error *err = NULL;
3034 struct diff_dir_cb_arg *a = arg;
3037 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3038 return got_error(GOT_ERR_CANCELLED);
3040 if (parent_path[0]) {
3041 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3042 return got_error_from_errno("asprintf");
3047 if (de->d_type != DT_DIR &&
3048 got_path_is_child(path, a->status_path, a->status_path_len)
3049 && !match_ignores(&a->ignores, path))
3050 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3051 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3057 static const struct got_error *
3058 status_traverse(void *arg, const char *path, int dirfd)
3060 const struct got_error *err = NULL;
3061 struct diff_dir_cb_arg *a = arg;
3066 err = add_ignores(&a->ignores, a->worktree->root_path,
3067 path, dirfd, ".cvsignore");
3071 err = add_ignores(&a->ignores, a->worktree->root_path, path,
3072 dirfd, ".gitignore");
3077 static const struct got_error *
3078 report_single_file_status(const char *path, const char *ondisk_path,
3079 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3080 void *status_arg, struct got_repository *repo, int report_unchanged)
3082 struct got_fileindex_entry *ie;
3085 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3087 return report_file_status(ie, ondisk_path, -1, NULL,
3088 status_cb, status_arg, repo, report_unchanged);
3090 if (lstat(ondisk_path, &sb) == -1) {
3091 if (errno != ENOENT)
3092 return got_error_from_errno2("lstat", ondisk_path);
3093 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3094 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3098 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3099 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3100 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3105 static const struct got_error *
3106 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3107 const char *root_path, const char *path)
3109 const struct got_error *err;
3110 char *parent_path, *next_parent_path = NULL;
3112 err = add_ignores(ignores, root_path, "", -1,
3117 err = add_ignores(ignores, root_path, "", -1,
3122 err = got_path_dirname(&parent_path, path);
3124 if (err->code == GOT_ERR_BAD_PATH)
3125 return NULL; /* cannot traverse parent */
3129 err = add_ignores(ignores, root_path, parent_path, -1,
3133 err = add_ignores(ignores, root_path, parent_path, -1,
3137 err = got_path_dirname(&next_parent_path, parent_path);
3139 if (err->code == GOT_ERR_BAD_PATH)
3140 err = NULL; /* traversed everything */
3144 parent_path = next_parent_path;
3145 next_parent_path = NULL;
3149 free(next_parent_path);
3153 static const struct got_error *
3154 worktree_status(struct got_worktree *worktree, const char *path,
3155 struct got_fileindex *fileindex, struct got_repository *repo,
3156 got_worktree_status_cb status_cb, void *status_arg,
3157 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3158 int report_unchanged)
3160 const struct got_error *err = NULL;
3162 struct got_fileindex_diff_dir_cb fdiff_cb;
3163 struct diff_dir_cb_arg arg;
3164 char *ondisk_path = NULL;
3166 TAILQ_INIT(&arg.ignores);
3168 if (asprintf(&ondisk_path, "%s%s%s",
3169 worktree->root_path, path[0] ? "/" : "", path) == -1)
3170 return got_error_from_errno("asprintf");
3172 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3174 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3176 err = got_error_from_errno2("open", ondisk_path);
3178 err = report_single_file_status(path, ondisk_path,
3179 fileindex, status_cb, status_arg, repo,
3182 fdiff_cb.diff_old_new = status_old_new;
3183 fdiff_cb.diff_old = status_old;
3184 fdiff_cb.diff_new = status_new;
3185 fdiff_cb.diff_traverse = status_traverse;
3186 arg.fileindex = fileindex;
3187 arg.worktree = worktree;
3188 arg.status_path = path;
3189 arg.status_path_len = strlen(path);
3191 arg.status_cb = status_cb;
3192 arg.status_arg = status_arg;
3193 arg.cancel_cb = cancel_cb;
3194 arg.cancel_arg = cancel_arg;
3195 arg.report_unchanged = report_unchanged;
3196 arg.no_ignores = no_ignores;
3198 err = add_ignores_from_parent_paths(&arg.ignores,
3199 worktree->root_path, path);
3203 err = got_fileindex_diff_dir(fileindex, fd,
3204 worktree->root_path, path, repo, &fdiff_cb, &arg);
3207 free_ignores(&arg.ignores);
3208 if (fd != -1 && close(fd) != 0 && err == NULL)
3209 err = got_error_from_errno("close");
3214 const struct got_error *
3215 got_worktree_status(struct got_worktree *worktree,
3216 struct got_pathlist_head *paths, struct got_repository *repo,
3217 got_worktree_status_cb status_cb, void *status_arg,
3218 got_cancel_cb cancel_cb, void *cancel_arg)
3220 const struct got_error *err = NULL;
3221 char *fileindex_path = NULL;
3222 struct got_fileindex *fileindex = NULL;
3223 struct got_pathlist_entry *pe;
3225 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3229 TAILQ_FOREACH(pe, paths, entry) {
3230 err = worktree_status(worktree, pe->path, fileindex, repo,
3231 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3235 free(fileindex_path);
3236 got_fileindex_free(fileindex);
3240 const struct got_error *
3241 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3244 const struct got_error *err = NULL;
3245 char *resolved = NULL, *cwd = NULL, *path = NULL;
3251 cwd = getcwd(NULL, 0);
3253 return got_error_from_errno("getcwd");
3255 if (lstat(arg, &sb) == -1) {
3256 if (errno != ENOENT) {
3257 err = got_error_from_errno2("lstat", arg);
3261 if (S_ISLNK(sb.st_mode)) {
3263 * We cannot use realpath(3) with symlinks since we want to
3264 * operate on the symlink itself.
3265 * But we can make the path absolute, assuming it is relative
3266 * to the current working directory, and then canonicalize it.
3268 char *abspath = NULL;
3269 char canonpath[PATH_MAX];
3270 if (!got_path_is_absolute(arg)) {
3271 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3272 err = got_error_from_errno("asprintf");
3277 err = got_canonpath(abspath ? abspath : arg, canonpath,
3281 resolved = strdup(canonpath);
3282 if (resolved == NULL) {
3283 err = got_error_from_errno("strdup");
3287 resolved = realpath(arg, NULL);
3288 if (resolved == NULL) {
3289 if (errno != ENOENT) {
3290 err = got_error_from_errno2("realpath", arg);
3293 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
3294 err = got_error_from_errno("asprintf");
3300 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3301 strlen(got_worktree_get_root_path(worktree)))) {
3302 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3306 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3307 err = got_path_skip_common_ancestor(&path,
3308 got_worktree_get_root_path(worktree), resolved);
3314 err = got_error_from_errno("strdup");
3319 /* XXX status walk can't deal with trailing slash! */
3321 while (len > 0 && path[len - 1] == '/') {
3322 path[len - 1] = '\0';
3335 struct schedule_addition_args {
3336 struct got_worktree *worktree;
3337 struct got_fileindex *fileindex;
3338 got_worktree_checkout_cb progress_cb;
3340 struct got_repository *repo;
3343 static const struct got_error *
3344 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3345 const char *relpath, struct got_object_id *blob_id,
3346 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3347 int dirfd, const char *de_name)
3349 struct schedule_addition_args *a = arg;
3350 const struct got_error *err = NULL;
3351 struct got_fileindex_entry *ie;
3355 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3357 return got_error_from_errno("asprintf");
3359 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3361 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3365 /* Re-adding an existing entry is a no-op. */
3366 if (status == GOT_STATUS_ADD)
3368 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3373 if (status != GOT_STATUS_UNVERSIONED) {
3374 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3378 err = got_fileindex_entry_alloc(&ie, relpath);
3381 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3383 got_fileindex_entry_free(ie);
3386 err = got_fileindex_entry_add(a->fileindex, ie);
3388 got_fileindex_entry_free(ie);
3395 if (status == GOT_STATUS_ADD)
3397 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3400 const struct got_error *
3401 got_worktree_schedule_add(struct got_worktree *worktree,
3402 struct got_pathlist_head *paths,
3403 got_worktree_checkout_cb progress_cb, void *progress_arg,
3404 struct got_repository *repo, int no_ignores)
3406 struct got_fileindex *fileindex = NULL;
3407 char *fileindex_path = NULL;
3408 const struct got_error *err = NULL, *sync_err, *unlockerr;
3409 struct got_pathlist_entry *pe;
3410 struct schedule_addition_args saa;
3412 err = lock_worktree(worktree, LOCK_EX);
3416 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3420 saa.worktree = worktree;
3421 saa.fileindex = fileindex;
3422 saa.progress_cb = progress_cb;
3423 saa.progress_arg = progress_arg;
3426 TAILQ_FOREACH(pe, paths, entry) {
3427 err = worktree_status(worktree, pe->path, fileindex, repo,
3428 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3432 sync_err = sync_fileindex(fileindex, fileindex_path);
3433 if (sync_err && err == NULL)
3436 free(fileindex_path);
3438 got_fileindex_free(fileindex);
3439 unlockerr = lock_worktree(worktree, LOCK_SH);
3440 if (unlockerr && err == NULL)
3445 struct schedule_deletion_args {
3446 struct got_worktree *worktree;
3447 struct got_fileindex *fileindex;
3448 got_worktree_delete_cb progress_cb;
3450 struct got_repository *repo;
3451 int delete_local_mods;
3455 static const struct got_error *
3456 schedule_for_deletion(void *arg, unsigned char status,
3457 unsigned char staged_status, const char *relpath,
3458 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3459 struct got_object_id *commit_id, int dirfd, const char *de_name)
3461 struct schedule_deletion_args *a = arg;
3462 const struct got_error *err = NULL;
3463 struct got_fileindex_entry *ie = NULL;
3465 char *ondisk_path, *parent = NULL;
3467 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3469 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3471 staged_status = get_staged_status(ie);
3472 if (staged_status != GOT_STATUS_NO_CHANGE) {
3473 if (staged_status == GOT_STATUS_DELETE)
3475 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3478 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3480 return got_error_from_errno("asprintf");
3482 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3487 if (status != GOT_STATUS_NO_CHANGE) {
3488 if (status == GOT_STATUS_DELETE)
3490 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3491 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3494 if (status != GOT_STATUS_MODIFY &&
3495 status != GOT_STATUS_MISSING) {
3496 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3501 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3503 if (unlinkat(dirfd, de_name, 0) != 0) {
3504 err = got_error_from_errno2("unlinkat",
3508 } else if (unlink(ondisk_path) != 0) {
3509 err = got_error_from_errno2("unlink", ondisk_path);
3513 parent = dirname(ondisk_path);
3515 if (parent == NULL) {
3516 err = got_error_from_errno2("dirname", ondisk_path);
3519 while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3520 if (rmdir(parent) == -1) {
3521 if (errno != ENOTEMPTY)
3522 err = got_error_from_errno2("rmdir",
3526 parent = dirname(parent);
3527 if (parent == NULL) {
3528 err = got_error_from_errno2("dirname", parent);
3534 got_fileindex_entry_mark_deleted_from_disk(ie);
3539 if (status == GOT_STATUS_DELETE)
3541 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3542 staged_status, relpath);
3545 const struct got_error *
3546 got_worktree_schedule_delete(struct got_worktree *worktree,
3547 struct got_pathlist_head *paths, int delete_local_mods,
3548 got_worktree_delete_cb progress_cb, void *progress_arg,
3549 struct got_repository *repo, int keep_on_disk)
3551 struct got_fileindex *fileindex = NULL;
3552 char *fileindex_path = NULL;
3553 const struct got_error *err = NULL, *sync_err, *unlockerr;
3554 struct got_pathlist_entry *pe;
3555 struct schedule_deletion_args sda;
3557 err = lock_worktree(worktree, LOCK_EX);
3561 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3565 sda.worktree = worktree;
3566 sda.fileindex = fileindex;
3567 sda.progress_cb = progress_cb;
3568 sda.progress_arg = progress_arg;
3570 sda.delete_local_mods = delete_local_mods;
3571 sda.keep_on_disk = keep_on_disk;
3573 TAILQ_FOREACH(pe, paths, entry) {
3574 err = worktree_status(worktree, pe->path, fileindex, repo,
3575 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3579 sync_err = sync_fileindex(fileindex, fileindex_path);
3580 if (sync_err && err == NULL)
3583 free(fileindex_path);
3585 got_fileindex_free(fileindex);
3586 unlockerr = lock_worktree(worktree, LOCK_SH);
3587 if (unlockerr && err == NULL)
3592 static const struct got_error *
3593 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3595 const struct got_error *err = NULL;
3597 size_t linesize = 0, n;
3600 linelen = getline(&line, &linesize, infile);
3601 if (linelen == -1) {
3602 if (ferror(infile)) {
3603 err = got_error_from_errno("getline");
3609 n = fwrite(line, 1, linelen, outfile);
3611 err = got_ferror(outfile, GOT_ERR_IO);
3616 n = fwrite(line, 1, linelen, rejectfile);
3618 err = got_ferror(outfile, GOT_ERR_IO);
3625 static const struct got_error *
3626 skip_one_line(FILE *f)
3629 size_t linesize = 0;
3632 linelen = getline(&line, &linesize, f);
3633 if (linelen == -1) {
3635 return got_error_from_errno("getline");
3642 static const struct got_error *
3643 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3644 int start_old, int end_old, int start_new, int end_new,
3645 FILE *outfile, FILE *rejectfile)
3647 const struct got_error *err;
3649 /* Copy old file's lines leading up to patch. */
3650 while (!feof(f1) && *line_cur1 < start_old) {
3651 err = copy_one_line(f1, outfile, NULL);
3656 /* Skip new file's lines leading up to patch. */
3657 while (!feof(f2) && *line_cur2 < start_new) {
3659 err = copy_one_line(f2, NULL, rejectfile);
3661 err = skip_one_line(f2);
3666 /* Copy patched lines. */
3667 while (!feof(f2) && *line_cur2 <= end_new) {
3668 err = copy_one_line(f2, outfile, NULL);
3673 /* Skip over old file's replaced lines. */
3674 while (!feof(f1) && *line_cur1 <= end_old) {
3676 err = copy_one_line(f1, NULL, rejectfile);
3678 err = skip_one_line(f1);
3687 static const struct got_error *
3688 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3689 FILE *outfile, FILE *rejectfile)
3691 const struct got_error *err;
3694 /* Copy old file's lines until EOF. */
3696 err = copy_one_line(f1, outfile, NULL);
3703 /* Copy new file's lines until EOF. */
3705 err = copy_one_line(f2, NULL, rejectfile);
3715 static const struct got_error *
3716 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3717 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3718 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3719 int *line_cur2, FILE *outfile, FILE *rejectfile,
3720 got_worktree_patch_cb patch_cb, void *patch_arg)
3722 const struct got_error *err = NULL;
3723 int start_old = change->cv.a;
3724 int end_old = change->cv.b;
3725 int start_new = change->cv.c;
3726 int end_new = change->cv.d;
3730 *choice = GOT_PATCH_CHOICE_NONE;
3732 hunkfile = got_opentemp();
3733 if (hunkfile == NULL)
3734 return got_error_from_errno("got_opentemp");
3739 /* XXX TODO needs error checking */
3740 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
3742 if (fseek(f1, pos1, SEEK_SET) == -1) {
3743 err = got_ferror(f1, GOT_ERR_IO);
3746 if (fseek(f2, pos2, SEEK_SET) == -1) {
3747 err = got_ferror(f1, GOT_ERR_IO);
3750 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
3751 err = got_ferror(hunkfile, GOT_ERR_IO);
3755 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
3756 hunkfile, n, nchanges);
3761 case GOT_PATCH_CHOICE_YES:
3762 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3763 end_old, start_new, end_new, outfile, rejectfile);
3765 case GOT_PATCH_CHOICE_NO:
3766 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3767 end_old, start_new, end_new, rejectfile, outfile);
3769 case GOT_PATCH_CHOICE_QUIT:
3772 err = got_error(GOT_ERR_PATCH_CHOICE);
3776 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3777 err = got_error_from_errno("fclose");
3781 struct revert_file_args {
3782 struct got_worktree *worktree;
3783 struct got_fileindex *fileindex;
3784 got_worktree_checkout_cb progress_cb;
3786 got_worktree_patch_cb patch_cb;
3788 struct got_repository *repo;
3791 static const struct got_error *
3792 create_patched_content(char **path_outfile, int reverse_patch,
3793 struct got_object_id *blob_id, const char *path2,
3794 int dirfd2, const char *de_name2,
3795 const char *relpath, struct got_repository *repo,
3796 got_worktree_patch_cb patch_cb, void *patch_arg)
3798 const struct got_error *err;
3799 struct got_blob_object *blob = NULL;
3800 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3802 char *path1 = NULL, *id_str = NULL;
3803 struct stat sb1, sb2;
3804 struct got_diff_changes *changes = NULL;
3805 struct got_diff_state *ds = NULL;
3806 struct got_diff_args *args = NULL;
3807 struct got_diff_change *change;
3808 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3811 *path_outfile = NULL;
3813 err = got_object_id_str(&id_str, blob_id);
3818 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
3820 err = got_error_from_errno2("openat", path2);
3824 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
3826 err = got_error_from_errno2("open", path2);
3830 if (fstat(fd2, &sb2) == -1) {
3831 err = got_error_from_errno2("fstat", path2);
3835 f2 = fdopen(fd2, "r");
3837 err = got_error_from_errno2("fdopen", path2);
3842 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
3846 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
3850 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
3854 if (stat(path1, &sb1) == -1) {
3855 err = got_error_from_errno2("stat", path1);
3859 err = got_diff_files(&changes, &ds, &args, &diff_flags,
3860 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
3864 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
3868 if (fseek(f1, 0L, SEEK_SET) == -1)
3869 return got_ferror(f1, GOT_ERR_IO);
3870 if (fseek(f2, 0L, SEEK_SET) == -1)
3871 return got_ferror(f2, GOT_ERR_IO);
3872 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
3874 err = apply_or_reject_change(&choice, change, ++n,
3875 changes->nchanges, ds, args, diff_flags, relpath,
3876 f1, f2, &line_cur1, &line_cur2,
3877 reverse_patch ? NULL : outfile,
3878 reverse_patch ? outfile : NULL,
3879 patch_cb, patch_arg);
3882 if (choice == GOT_PATCH_CHOICE_YES)
3884 else if (choice == GOT_PATCH_CHOICE_QUIT)
3888 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
3889 reverse_patch ? NULL : outfile,
3890 reverse_patch ? outfile : NULL);
3894 if (chmod(*path_outfile, sb2.st_mode) == -1) {
3895 err = got_error_from_errno2("chmod", path2);
3902 got_object_blob_close(blob);
3903 if (f1 && fclose(f1) == EOF && err == NULL)
3904 err = got_error_from_errno2("fclose", path1);
3905 if (f2 && fclose(f2) == EOF && err == NULL)
3906 err = got_error_from_errno2("fclose", path2);
3907 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3908 err = got_error_from_errno2("close", path2);
3909 if (outfile && fclose(outfile) == EOF && err == NULL)
3910 err = got_error_from_errno2("fclose", *path_outfile);
3911 if (path1 && unlink(path1) == -1 && err == NULL)
3912 err = got_error_from_errno2("unlink", path1);
3913 if (err || !have_content) {
3914 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
3915 err = got_error_from_errno2("unlink", *path_outfile);
3916 free(*path_outfile);
3917 *path_outfile = NULL;
3921 got_diff_state_free(ds);
3925 got_diff_free_changes(changes);
3930 static const struct got_error *
3931 revert_file(void *arg, unsigned char status, unsigned char staged_status,
3932 const char *relpath, struct got_object_id *blob_id,
3933 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3934 int dirfd, const char *de_name)
3936 struct revert_file_args *a = arg;
3937 const struct got_error *err = NULL;
3938 char *parent_path = NULL;
3939 struct got_fileindex_entry *ie;
3940 struct got_tree_object *tree = NULL;
3941 struct got_object_id *tree_id = NULL;
3942 const struct got_tree_entry *te = NULL;
3943 char *tree_path = NULL, *te_name;
3944 char *ondisk_path = NULL, *path_content = NULL;
3945 struct got_blob_object *blob = NULL;
3947 /* Reverting a staged deletion is a no-op. */
3948 if (status == GOT_STATUS_DELETE &&
3949 staged_status != GOT_STATUS_NO_CHANGE)
3952 if (status == GOT_STATUS_UNVERSIONED)
3953 return (*a->progress_cb)(a->progress_arg,
3954 GOT_STATUS_UNVERSIONED, relpath);
3956 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3958 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3960 /* Construct in-repository path of tree which contains this blob. */
3961 err = got_path_dirname(&parent_path, ie->path);
3963 if (err->code != GOT_ERR_BAD_PATH)
3965 parent_path = strdup("/");
3966 if (parent_path == NULL) {
3967 err = got_error_from_errno("strdup");
3971 if (got_path_is_root_dir(a->worktree->path_prefix)) {
3972 tree_path = strdup(parent_path);
3973 if (tree_path == NULL) {
3974 err = got_error_from_errno("strdup");
3978 if (got_path_is_root_dir(parent_path)) {
3979 tree_path = strdup(a->worktree->path_prefix);
3980 if (tree_path == NULL) {
3981 err = got_error_from_errno("strdup");
3985 if (asprintf(&tree_path, "%s/%s",
3986 a->worktree->path_prefix, parent_path) == -1) {
3987 err = got_error_from_errno("asprintf");
3993 err = got_object_id_by_path(&tree_id, a->repo,
3994 a->worktree->base_commit_id, tree_path);
3996 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
3997 (status == GOT_STATUS_ADD ||
3998 staged_status == GOT_STATUS_ADD)))
4001 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4005 te_name = basename(ie->path);
4006 if (te_name == NULL) {
4007 err = got_error_from_errno2("basename", ie->path);
4011 te = got_object_tree_find_entry(tree, te_name);
4012 if (te == NULL && status != GOT_STATUS_ADD &&
4013 staged_status != GOT_STATUS_ADD) {
4014 err = got_error(GOT_ERR_NO_TREE_ENTRY);
4020 case GOT_STATUS_ADD:
4022 int choice = GOT_PATCH_CHOICE_NONE;
4023 err = (*a->patch_cb)(&choice, a->patch_arg,
4024 status, ie->path, NULL, 1, 1);
4027 if (choice != GOT_PATCH_CHOICE_YES)
4030 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4034 got_fileindex_entry_remove(a->fileindex, ie);
4036 case GOT_STATUS_DELETE:
4038 int choice = GOT_PATCH_CHOICE_NONE;
4039 err = (*a->patch_cb)(&choice, a->patch_arg,
4040 status, ie->path, NULL, 1, 1);
4043 if (choice != GOT_PATCH_CHOICE_YES)
4047 case GOT_STATUS_MODIFY:
4048 case GOT_STATUS_MODE_CHANGE:
4049 case GOT_STATUS_CONFLICT:
4050 case GOT_STATUS_MISSING: {
4051 struct got_object_id id;
4052 if (staged_status == GOT_STATUS_ADD ||
4053 staged_status == GOT_STATUS_MODIFY) {
4054 memcpy(id.sha1, ie->staged_blob_sha1,
4055 SHA1_DIGEST_LENGTH);
4057 memcpy(id.sha1, ie->blob_sha1,
4058 SHA1_DIGEST_LENGTH);
4059 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4063 if (asprintf(&ondisk_path, "%s/%s",
4064 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4065 err = got_error_from_errno("asprintf");
4069 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4070 status == GOT_STATUS_CONFLICT)) {
4071 err = create_patched_content(&path_content, 1, &id,
4072 ondisk_path, dirfd, de_name, ie->path, a->repo,
4073 a->patch_cb, a->patch_arg);
4074 if (err || path_content == NULL)
4076 if (rename(path_content, ondisk_path) == -1) {
4077 err = got_error_from_errno3("rename",
4078 path_content, ondisk_path);
4082 err = install_blob(a->worktree, ondisk_path, ie->path,
4083 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4084 got_fileindex_perms_to_st(ie), blob, 0, 1, 0,
4085 a->repo, a->progress_cb, a->progress_arg);
4088 if (status == GOT_STATUS_DELETE ||
4089 status == GOT_STATUS_MODE_CHANGE) {
4090 err = got_fileindex_entry_update(ie,
4091 ondisk_path, blob->id.sha1,
4092 a->worktree->base_commit_id->sha1, 1);
4108 got_object_blob_close(blob);
4110 got_object_tree_close(tree);
4115 const struct got_error *
4116 got_worktree_revert(struct got_worktree *worktree,
4117 struct got_pathlist_head *paths,
4118 got_worktree_checkout_cb progress_cb, void *progress_arg,
4119 got_worktree_patch_cb patch_cb, void *patch_arg,
4120 struct got_repository *repo)
4122 struct got_fileindex *fileindex = NULL;
4123 char *fileindex_path = NULL;
4124 const struct got_error *err = NULL, *unlockerr = NULL;
4125 const struct got_error *sync_err = NULL;
4126 struct got_pathlist_entry *pe;
4127 struct revert_file_args rfa;
4129 err = lock_worktree(worktree, LOCK_EX);
4133 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4137 rfa.worktree = worktree;
4138 rfa.fileindex = fileindex;
4139 rfa.progress_cb = progress_cb;
4140 rfa.progress_arg = progress_arg;
4141 rfa.patch_cb = patch_cb;
4142 rfa.patch_arg = patch_arg;
4144 TAILQ_FOREACH(pe, paths, entry) {
4145 err = worktree_status(worktree, pe->path, fileindex, repo,
4146 revert_file, &rfa, NULL, NULL, 0, 0);
4150 sync_err = sync_fileindex(fileindex, fileindex_path);
4151 if (sync_err && err == NULL)
4154 free(fileindex_path);
4156 got_fileindex_free(fileindex);
4157 unlockerr = lock_worktree(worktree, LOCK_SH);
4158 if (unlockerr && err == NULL)
4164 free_commitable(struct got_commitable *ct)
4167 free(ct->in_repo_path);
4168 free(ct->ondisk_path);
4170 free(ct->base_blob_id);
4171 free(ct->staged_blob_id);
4172 free(ct->base_commit_id);
4176 struct collect_commitables_arg {
4177 struct got_pathlist_head *commitable_paths;
4178 struct got_repository *repo;
4179 struct got_worktree *worktree;
4180 int have_staged_files;
4183 static const struct got_error *
4184 collect_commitables(void *arg, unsigned char status,
4185 unsigned char staged_status, const char *relpath,
4186 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4187 struct got_object_id *commit_id, int dirfd, const char *de_name)
4189 struct collect_commitables_arg *a = arg;
4190 const struct got_error *err = NULL;
4191 struct got_commitable *ct = NULL;
4192 struct got_pathlist_entry *new = NULL;
4193 char *parent_path = NULL, *path = NULL;
4196 if (a->have_staged_files) {
4197 if (staged_status != GOT_STATUS_MODIFY &&
4198 staged_status != GOT_STATUS_ADD &&
4199 staged_status != GOT_STATUS_DELETE)
4202 if (status == GOT_STATUS_CONFLICT)
4203 return got_error(GOT_ERR_COMMIT_CONFLICT);
4205 if (status != GOT_STATUS_MODIFY &&
4206 status != GOT_STATUS_MODE_CHANGE &&
4207 status != GOT_STATUS_ADD &&
4208 status != GOT_STATUS_DELETE)
4212 if (asprintf(&path, "/%s", relpath) == -1) {
4213 err = got_error_from_errno("asprintf");
4216 if (strcmp(path, "/") == 0) {
4217 parent_path = strdup("");
4218 if (parent_path == NULL)
4219 return got_error_from_errno("strdup");
4221 err = got_path_dirname(&parent_path, path);
4226 ct = calloc(1, sizeof(*ct));
4228 err = got_error_from_errno("calloc");
4232 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4234 err = got_error_from_errno("asprintf");
4237 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
4238 sb.st_mode = GOT_DEFAULT_FILE_MODE;
4241 if (fstatat(dirfd, de_name, &sb,
4242 AT_SYMLINK_NOFOLLOW) == -1) {
4243 err = got_error_from_errno2("fstatat",
4247 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4248 err = got_error_from_errno2("lstat", ct->ondisk_path);
4251 ct->mode = sb.st_mode;
4254 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4255 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4257 err = got_error_from_errno("asprintf");
4261 ct->status = status;
4262 ct->staged_status = staged_status;
4263 ct->blob_id = NULL; /* will be filled in when blob gets created */
4264 if (ct->status != GOT_STATUS_ADD &&
4265 ct->staged_status != GOT_STATUS_ADD) {
4266 ct->base_blob_id = got_object_id_dup(blob_id);
4267 if (ct->base_blob_id == NULL) {
4268 err = got_error_from_errno("got_object_id_dup");
4271 ct->base_commit_id = got_object_id_dup(commit_id);
4272 if (ct->base_commit_id == NULL) {
4273 err = got_error_from_errno("got_object_id_dup");
4277 if (ct->staged_status == GOT_STATUS_ADD ||
4278 ct->staged_status == GOT_STATUS_MODIFY) {
4279 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4280 if (ct->staged_blob_id == NULL) {
4281 err = got_error_from_errno("got_object_id_dup");
4285 ct->path = strdup(path);
4286 if (ct->path == NULL) {
4287 err = got_error_from_errno("strdup");
4290 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4292 if (ct && (err || new == NULL))
4293 free_commitable(ct);
4299 static const struct got_error *write_tree(struct got_object_id **, int *,
4300 struct got_tree_object *, const char *, struct got_pathlist_head *,
4301 got_worktree_status_cb status_cb, void *status_arg,
4302 struct got_repository *);
4304 static const struct got_error *
4305 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4306 struct got_tree_entry *te, const char *parent_path,
4307 struct got_pathlist_head *commitable_paths,
4308 got_worktree_status_cb status_cb, void *status_arg,
4309 struct got_repository *repo)
4311 const struct got_error *err = NULL;
4312 struct got_tree_object *subtree;
4315 if (asprintf(&subpath, "%s%s%s", parent_path,
4316 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4317 return got_error_from_errno("asprintf");
4319 err = got_object_open_as_tree(&subtree, repo, &te->id);
4323 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4324 commitable_paths, status_cb, status_arg, repo);
4325 got_object_tree_close(subtree);
4330 static const struct got_error *
4331 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4333 const struct got_error *err = NULL;
4334 char *ct_parent_path = NULL;
4338 if (strchr(ct->in_repo_path, '/') == NULL) {
4339 *match = got_path_is_root_dir(path);
4343 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4346 *match = (strcmp(path, ct_parent_path) == 0);
4347 free(ct_parent_path);
4352 get_ct_file_mode(struct got_commitable *ct)
4354 if (S_ISLNK(ct->mode))
4357 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4360 static const struct got_error *
4361 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4362 struct got_tree_entry *te, struct got_commitable *ct)
4364 const struct got_error *err = NULL;
4368 err = got_object_tree_entry_dup(new_te, te);
4372 (*new_te)->mode = get_ct_file_mode(ct);
4374 if (ct->staged_status == GOT_STATUS_MODIFY)
4375 memcpy(&(*new_te)->id, ct->staged_blob_id,
4376 sizeof((*new_te)->id));
4378 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4380 if (err && *new_te) {
4387 static const struct got_error *
4388 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4389 struct got_commitable *ct)
4391 const struct got_error *err = NULL;
4396 *new_te = calloc(1, sizeof(**new_te));
4397 if (*new_te == NULL)
4398 return got_error_from_errno("calloc");
4400 ct_name = basename(ct->path);
4401 if (ct_name == NULL) {
4402 err = got_error_from_errno2("basename", ct->path);
4405 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4406 sizeof((*new_te)->name)) {
4407 err = got_error(GOT_ERR_NO_SPACE);
4411 (*new_te)->mode = get_ct_file_mode(ct);
4413 if (ct->staged_status == GOT_STATUS_ADD)
4414 memcpy(&(*new_te)->id, ct->staged_blob_id,
4415 sizeof((*new_te)->id));
4417 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4419 if (err && *new_te) {
4426 static const struct got_error *
4427 insert_tree_entry(struct got_tree_entry *new_te,
4428 struct got_pathlist_head *paths)
4430 const struct got_error *err = NULL;
4431 struct got_pathlist_entry *new_pe;
4433 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4437 return got_error(GOT_ERR_TREE_DUP_ENTRY);
4441 static const struct got_error *
4442 report_ct_status(struct got_commitable *ct,
4443 got_worktree_status_cb status_cb, void *status_arg)
4445 const char *ct_path = ct->path;
4446 unsigned char status;
4448 while (ct_path[0] == '/')
4451 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4452 status = ct->staged_status;
4454 status = ct->status;
4456 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4457 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4460 static const struct got_error *
4461 match_modified_subtree(int *modified, struct got_tree_entry *te,
4462 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4464 const struct got_error *err = NULL;
4465 struct got_pathlist_entry *pe;
4470 if (asprintf(&te_path, "%s%s%s", base_tree_path,
4471 got_path_is_root_dir(base_tree_path) ? "" : "/",
4473 return got_error_from_errno("asprintf");
4475 TAILQ_FOREACH(pe, commitable_paths, entry) {
4476 struct got_commitable *ct = pe->data;
4477 *modified = got_path_is_child(ct->in_repo_path, te_path,
4487 static const struct got_error *
4488 match_deleted_or_modified_ct(struct got_commitable **ctp,
4489 struct got_tree_entry *te, const char *base_tree_path,
4490 struct got_pathlist_head *commitable_paths)
4492 const struct got_error *err = NULL;
4493 struct got_pathlist_entry *pe;
4497 TAILQ_FOREACH(pe, commitable_paths, entry) {
4498 struct got_commitable *ct = pe->data;
4499 char *ct_name = NULL;
4502 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4503 if (ct->status != GOT_STATUS_MODIFY &&
4504 ct->status != GOT_STATUS_MODE_CHANGE &&
4505 ct->status != GOT_STATUS_DELETE)
4508 if (ct->staged_status != GOT_STATUS_MODIFY &&
4509 ct->staged_status != GOT_STATUS_DELETE)
4513 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4516 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4522 ct_name = basename(pe->path);
4523 if (ct_name == NULL)
4524 return got_error_from_errno2("basename", pe->path);
4526 if (strcmp(te->name, ct_name) != 0)
4536 static const struct got_error *
4537 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4538 const char *child_path, const char *path_base_tree,
4539 struct got_pathlist_head *commitable_paths,
4540 got_worktree_status_cb status_cb, void *status_arg,
4541 struct got_repository *repo)
4543 const struct got_error *err = NULL;
4544 struct got_tree_entry *new_te;
4546 struct got_object_id *id = NULL;
4551 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4552 got_path_is_root_dir(path_base_tree) ? "" : "/",
4554 return got_error_from_errno("asprintf");
4556 new_te = calloc(1, sizeof(*new_te));
4558 return got_error_from_errno("calloc");
4559 new_te->mode = S_IFDIR;
4561 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4562 sizeof(new_te->name)) {
4563 err = got_error(GOT_ERR_NO_SPACE);
4566 err = write_tree(&id, &nentries, NULL, subtree_path,
4567 commitable_paths, status_cb, status_arg, repo);
4572 memcpy(&new_te->id, id, sizeof(new_te->id));
4581 static const struct got_error *
4582 write_tree(struct got_object_id **new_tree_id, int *nentries,
4583 struct got_tree_object *base_tree, const char *path_base_tree,
4584 struct got_pathlist_head *commitable_paths,
4585 got_worktree_status_cb status_cb, void *status_arg,
4586 struct got_repository *repo)
4588 const struct got_error *err = NULL;
4589 struct got_pathlist_head paths;
4590 struct got_tree_entry *te, *new_te = NULL;
4591 struct got_pathlist_entry *pe;
4596 /* Insert, and recurse into, newly added entries first. */
4597 TAILQ_FOREACH(pe, commitable_paths, entry) {
4598 struct got_commitable *ct = pe->data;
4599 char *child_path = NULL, *slash;
4601 if ((ct->status != GOT_STATUS_ADD &&
4602 ct->staged_status != GOT_STATUS_ADD) ||
4603 (ct->flags & GOT_COMMITABLE_ADDED))
4606 if (!got_path_is_child(pe->path, path_base_tree,
4607 strlen(path_base_tree)))
4610 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
4615 slash = strchr(child_path, '/');
4616 if (slash == NULL) {
4617 err = alloc_added_blob_tree_entry(&new_te, ct);
4620 err = report_ct_status(ct, status_cb, status_arg);
4623 ct->flags |= GOT_COMMITABLE_ADDED;
4624 err = insert_tree_entry(new_te, &paths);
4629 *slash = '\0'; /* trim trailing path components */
4630 if (base_tree == NULL ||
4631 got_object_tree_find_entry(base_tree, child_path)
4633 err = make_subtree_for_added_blob(&new_te,
4634 child_path, path_base_tree,
4635 commitable_paths, status_cb, status_arg,
4639 err = insert_tree_entry(new_te, &paths);
4648 int i, nbase_entries;
4649 /* Handle modified and deleted entries. */
4650 nbase_entries = got_object_tree_get_nentries(base_tree);
4651 for (i = 0; i < nbase_entries; i++) {
4652 struct got_commitable *ct = NULL;
4654 te = got_object_tree_get_entry(base_tree, i);
4655 if (got_object_tree_entry_is_submodule(te)) {
4656 /* Entry is a submodule; just copy it. */
4657 err = got_object_tree_entry_dup(&new_te, te);
4660 err = insert_tree_entry(new_te, &paths);
4667 if (S_ISDIR(te->mode)) {
4669 err = got_object_tree_entry_dup(&new_te, te);
4672 err = match_modified_subtree(&modified, te,
4673 path_base_tree, commitable_paths);
4676 /* Avoid recursion into unmodified subtrees. */
4678 struct got_object_id *new_id;
4680 err = write_subtree(&new_id,
4682 path_base_tree, commitable_paths,
4683 status_cb, status_arg, repo);
4686 if (nsubentries == 0) {
4687 /* All entries were deleted. */
4691 memcpy(&new_te->id, new_id,
4692 sizeof(new_te->id));
4695 err = insert_tree_entry(new_te, &paths);
4702 err = match_deleted_or_modified_ct(&ct, te,
4703 path_base_tree, commitable_paths);
4707 /* NB: Deleted entries get dropped here. */
4708 if (ct->status == GOT_STATUS_MODIFY ||
4709 ct->status == GOT_STATUS_MODE_CHANGE ||
4710 ct->staged_status == GOT_STATUS_MODIFY) {
4711 err = alloc_modified_blob_tree_entry(
4715 err = insert_tree_entry(new_te, &paths);
4720 err = report_ct_status(ct, status_cb,
4725 /* Entry is unchanged; just copy it. */
4726 err = got_object_tree_entry_dup(&new_te, te);
4729 err = insert_tree_entry(new_te, &paths);
4737 /* Write new list of entries; deleted entries have been dropped. */
4738 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
4740 got_pathlist_free(&paths);
4744 static const struct got_error *
4745 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
4746 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
4747 int have_staged_files)
4749 const struct got_error *err = NULL;
4750 struct got_pathlist_entry *pe;
4752 TAILQ_FOREACH(pe, commitable_paths, entry) {
4753 struct got_fileindex_entry *ie;
4754 struct got_commitable *ct = pe->data;
4756 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4758 if (ct->status == GOT_STATUS_DELETE ||
4759 ct->staged_status == GOT_STATUS_DELETE) {
4760 got_fileindex_entry_remove(fileindex, ie);
4761 } else if (ct->staged_status == GOT_STATUS_ADD ||
4762 ct->staged_status == GOT_STATUS_MODIFY) {
4763 got_fileindex_entry_stage_set(ie,
4764 GOT_FILEIDX_STAGE_NONE);
4765 err = got_fileindex_entry_update(ie,
4766 ct->ondisk_path, ct->staged_blob_id->sha1,
4767 new_base_commit_id->sha1,
4768 !have_staged_files);
4770 err = got_fileindex_entry_update(ie,
4771 ct->ondisk_path, ct->blob_id->sha1,
4772 new_base_commit_id->sha1,
4773 !have_staged_files);
4775 err = got_fileindex_entry_alloc(&ie, pe->path);
4778 err = got_fileindex_entry_update(ie, ct->ondisk_path,
4779 ct->blob_id->sha1, new_base_commit_id->sha1, 1);
4781 got_fileindex_entry_free(ie);
4784 err = got_fileindex_entry_add(fileindex, ie);
4786 got_fileindex_entry_free(ie);
4795 static const struct got_error *
4796 check_out_of_date(const char *in_repo_path, unsigned char status,
4797 unsigned char staged_status, struct got_object_id *base_blob_id,
4798 struct got_object_id *base_commit_id,
4799 struct got_object_id *head_commit_id, struct got_repository *repo,
4802 const struct got_error *err = NULL;
4803 struct got_object_id *id = NULL;
4805 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
4806 /* Trivial case: base commit == head commit */
4807 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
4810 * Ensure file content which local changes were based
4811 * on matches file content in the branch head.
4813 err = got_object_id_by_path(&id, repo, head_commit_id,
4816 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4817 err = got_error(ood_errcode);
4819 } else if (got_object_id_cmp(id, base_blob_id) != 0)
4820 err = got_error(ood_errcode);
4822 /* Require that added files don't exist in the branch head. */
4823 err = got_object_id_by_path(&id, repo, head_commit_id,
4825 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
4827 err = id ? got_error(ood_errcode) : NULL;
4834 const struct got_error *
4835 commit_worktree(struct got_object_id **new_commit_id,
4836 struct got_pathlist_head *commitable_paths,
4837 struct got_object_id *head_commit_id, struct got_worktree *worktree,
4838 const char *author, const char *committer,
4839 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4840 got_worktree_status_cb status_cb, void *status_arg,
4841 struct got_repository *repo)
4843 const struct got_error *err = NULL, *unlockerr = NULL;
4844 struct got_pathlist_entry *pe;
4845 const char *head_ref_name = NULL;
4846 struct got_commit_object *head_commit = NULL;
4847 struct got_reference *head_ref2 = NULL;
4848 struct got_object_id *head_commit_id2 = NULL;
4849 struct got_tree_object *head_tree = NULL;
4850 struct got_object_id *new_tree_id = NULL;
4852 struct got_object_id_queue parent_ids;
4853 struct got_object_qid *pid = NULL;
4854 char *logmsg = NULL;
4856 *new_commit_id = NULL;
4858 SIMPLEQ_INIT(&parent_ids);
4860 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
4864 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
4868 if (commit_msg_cb != NULL) {
4869 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
4874 if (logmsg == NULL || strlen(logmsg) == 0) {
4875 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
4879 /* Create blobs from added and modified files and record their IDs. */
4880 TAILQ_FOREACH(pe, commitable_paths, entry) {
4881 struct got_commitable *ct = pe->data;
4884 /* Blobs for staged files already exist. */
4885 if (ct->staged_status == GOT_STATUS_ADD ||
4886 ct->staged_status == GOT_STATUS_MODIFY)
4889 if (ct->status != GOT_STATUS_ADD &&
4890 ct->status != GOT_STATUS_MODIFY &&
4891 ct->status != GOT_STATUS_MODE_CHANGE)
4894 if (asprintf(&ondisk_path, "%s/%s",
4895 worktree->root_path, pe->path) == -1) {
4896 err = got_error_from_errno("asprintf");
4899 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
4906 * When comitting a symlink we convert "bad" symlinks (those
4907 * which point outside the work tree or into .got) to regular
4908 * files. This way, the post-commit work tree state matches
4909 * a fresh checkout of the tree which was committed.
4911 if (S_ISLNK(get_ct_file_mode(ct))) {
4912 struct got_blob_object *blob;
4913 err = got_object_open_as_blob(&blob, repo, ct->blob_id,
4919 err = install_symlink(worktree, ondisk_path, ct->path,
4920 get_ct_file_mode(ct), GOT_DEFAULT_FILE_MODE, blob,
4921 0, 0, repo, NULL, NULL);
4922 got_object_blob_close(blob);
4931 /* Recursively write new tree objects. */
4932 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
4933 commitable_paths, status_cb, status_arg, repo);
4937 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
4940 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
4941 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
4942 1, author, time(NULL), committer, time(NULL), logmsg, repo);
4943 got_object_qid_free(pid);
4949 /* Check if a concurrent commit to our branch has occurred. */
4950 head_ref_name = got_worktree_get_head_ref_name(worktree);
4951 if (head_ref_name == NULL) {
4952 err = got_error_from_errno("got_worktree_get_head_ref_name");
4955 /* Lock the reference here to prevent concurrent modification. */
4956 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
4959 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
4962 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
4963 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
4966 /* Update branch head in repository. */
4967 err = got_ref_change_ref(head_ref2, *new_commit_id);
4970 err = got_ref_write(head_ref2, repo);
4974 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
4978 err = ref_base_commit(worktree, repo);
4983 got_object_tree_close(head_tree);
4985 got_object_commit_close(head_commit);
4986 free(head_commit_id2);
4988 unlockerr = got_ref_unlock(head_ref2);
4989 if (unlockerr && err == NULL)
4991 got_ref_close(head_ref2);
4996 static const struct got_error *
4997 check_path_is_commitable(const char *path,
4998 struct got_pathlist_head *commitable_paths)
5000 struct got_pathlist_entry *cpe = NULL;
5001 size_t path_len = strlen(path);
5003 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5004 struct got_commitable *ct = cpe->data;
5005 const char *ct_path = ct->path;
5007 while (ct_path[0] == '/')
5010 if (strcmp(path, ct_path) == 0 ||
5011 got_path_is_child(ct_path, path, path_len))
5016 return got_error_path(path, GOT_ERR_BAD_PATH);
5021 static const struct got_error *
5022 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5024 int *have_staged_files = arg;
5026 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5027 *have_staged_files = 1;
5028 return got_error(GOT_ERR_CANCELLED);
5034 static const struct got_error *
5035 check_non_staged_files(struct got_fileindex *fileindex,
5036 struct got_pathlist_head *paths)
5038 struct got_pathlist_entry *pe;
5039 struct got_fileindex_entry *ie;
5041 TAILQ_FOREACH(pe, paths, entry) {
5042 if (pe->path[0] == '\0')
5044 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5046 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5047 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5048 return got_error_path(pe->path,
5049 GOT_ERR_FILE_NOT_STAGED);
5055 const struct got_error *
5056 got_worktree_commit(struct got_object_id **new_commit_id,
5057 struct got_worktree *worktree, struct got_pathlist_head *paths,
5058 const char *author, const char *committer,
5059 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5060 got_worktree_status_cb status_cb, void *status_arg,
5061 struct got_repository *repo)
5063 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5064 struct got_fileindex *fileindex = NULL;
5065 char *fileindex_path = NULL;
5066 struct got_pathlist_head commitable_paths;
5067 struct collect_commitables_arg cc_arg;
5068 struct got_pathlist_entry *pe;
5069 struct got_reference *head_ref = NULL;
5070 struct got_object_id *head_commit_id = NULL;
5071 int have_staged_files = 0;
5073 *new_commit_id = NULL;
5075 TAILQ_INIT(&commitable_paths);
5077 err = lock_worktree(worktree, LOCK_EX);
5081 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5085 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5089 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5093 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5094 &have_staged_files);
5095 if (err && err->code != GOT_ERR_CANCELLED)
5097 if (have_staged_files) {
5098 err = check_non_staged_files(fileindex, paths);
5103 cc_arg.commitable_paths = &commitable_paths;
5104 cc_arg.worktree = worktree;
5106 cc_arg.have_staged_files = have_staged_files;
5107 TAILQ_FOREACH(pe, paths, entry) {
5108 err = worktree_status(worktree, pe->path, fileindex, repo,
5109 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5114 if (TAILQ_EMPTY(&commitable_paths)) {
5115 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5119 TAILQ_FOREACH(pe, paths, entry) {
5120 err = check_path_is_commitable(pe->path, &commitable_paths);
5125 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5126 struct got_commitable *ct = pe->data;
5127 const char *ct_path = ct->in_repo_path;
5129 while (ct_path[0] == '/')
5131 err = check_out_of_date(ct_path, ct->status,
5132 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5133 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5139 err = commit_worktree(new_commit_id, &commitable_paths,
5140 head_commit_id, worktree, author, committer,
5141 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5145 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5146 fileindex, have_staged_files);
5147 sync_err = sync_fileindex(fileindex, fileindex_path);
5148 if (sync_err && err == NULL)
5152 got_fileindex_free(fileindex);
5153 free(fileindex_path);
5154 unlockerr = lock_worktree(worktree, LOCK_SH);
5155 if (unlockerr && err == NULL)
5157 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5158 struct got_commitable *ct = pe->data;
5159 free_commitable(ct);
5161 got_pathlist_free(&commitable_paths);
5166 got_commitable_get_path(struct got_commitable *ct)
5172 got_commitable_get_status(struct got_commitable *ct)
5177 struct check_rebase_ok_arg {
5178 struct got_worktree *worktree;
5179 struct got_repository *repo;
5182 static const struct got_error *
5183 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5185 const struct got_error *err = NULL;
5186 struct check_rebase_ok_arg *a = arg;
5187 unsigned char status;
5191 /* Reject rebase of a work tree with mixed base commits. */
5192 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5193 SHA1_DIGEST_LENGTH))
5194 return got_error(GOT_ERR_MIXED_COMMITS);
5196 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5198 return got_error_from_errno("asprintf");
5200 /* Reject rebase of a work tree with modified or staged files. */
5201 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5206 if (status != GOT_STATUS_NO_CHANGE)
5207 return got_error(GOT_ERR_MODIFIED);
5208 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5209 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5214 const struct got_error *
5215 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5216 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5217 struct got_worktree *worktree, struct got_reference *branch,
5218 struct got_repository *repo)
5220 const struct got_error *err = NULL;
5221 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5222 char *branch_ref_name = NULL;
5223 char *fileindex_path = NULL;
5224 struct check_rebase_ok_arg ok_arg;
5225 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5226 struct got_object_id *wt_branch_tip = NULL;
5228 *new_base_branch_ref = NULL;
5232 err = lock_worktree(worktree, LOCK_EX);
5236 err = open_fileindex(fileindex, &fileindex_path, worktree);
5240 ok_arg.worktree = worktree;
5242 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5247 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5251 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5255 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5259 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5264 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5267 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5268 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5272 err = got_ref_alloc_symref(new_base_branch_ref,
5273 new_base_branch_ref_name, wt_branch);
5276 err = got_ref_write(*new_base_branch_ref, repo);
5280 /* TODO Lock original branch's ref while rebasing? */
5282 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5286 err = got_ref_write(branch_ref, repo);
5290 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5291 worktree->base_commit_id);
5294 err = got_ref_write(*tmp_branch, repo);
5298 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5302 free(fileindex_path);
5303 free(tmp_branch_name);
5304 free(new_base_branch_ref_name);
5305 free(branch_ref_name);
5307 got_ref_close(branch_ref);
5309 got_ref_close(wt_branch);
5310 free(wt_branch_tip);
5312 if (*new_base_branch_ref) {
5313 got_ref_close(*new_base_branch_ref);
5314 *new_base_branch_ref = NULL;
5317 got_ref_close(*tmp_branch);
5321 got_fileindex_free(*fileindex);
5324 lock_worktree(worktree, LOCK_SH);
5329 const struct got_error *
5330 got_worktree_rebase_continue(struct got_object_id **commit_id,
5331 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5332 struct got_reference **branch, struct got_fileindex **fileindex,
5333 struct got_worktree *worktree, struct got_repository *repo)
5335 const struct got_error *err;
5336 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5337 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5338 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5339 char *fileindex_path = NULL;
5340 int have_staged_files = 0;
5343 *new_base_branch = NULL;
5348 err = lock_worktree(worktree, LOCK_EX);
5352 err = open_fileindex(fileindex, &fileindex_path, worktree);
5356 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5357 &have_staged_files);
5358 if (err && err->code != GOT_ERR_CANCELLED)
5360 if (have_staged_files) {
5361 err = got_error(GOT_ERR_STAGED_PATHS);
5365 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5369 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5373 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5377 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5381 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5385 err = got_ref_open(branch, repo,
5386 got_ref_get_symref_target(branch_ref), 0);
5390 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5394 err = got_ref_resolve(commit_id, repo, commit_ref);
5398 err = got_ref_open(new_base_branch, repo,
5399 new_base_branch_ref_name, 0);
5403 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5407 free(commit_ref_name);
5408 free(branch_ref_name);
5409 free(fileindex_path);
5411 got_ref_close(commit_ref);
5413 got_ref_close(branch_ref);
5418 got_ref_close(*tmp_branch);
5421 if (*new_base_branch) {
5422 got_ref_close(*new_base_branch);
5423 *new_base_branch = NULL;
5426 got_ref_close(*branch);
5430 got_fileindex_free(*fileindex);
5433 lock_worktree(worktree, LOCK_SH);
5438 const struct got_error *
5439 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5441 const struct got_error *err;
5442 char *tmp_branch_name = NULL;
5444 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5448 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5449 free(tmp_branch_name);
5453 static const struct got_error *
5454 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5455 char **logmsg, void *arg)
5461 static const struct got_error *
5462 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5463 const char *path, struct got_object_id *blob_id,
5464 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5465 int dirfd, const char *de_name)
5470 struct collect_merged_paths_arg {
5471 got_worktree_checkout_cb progress_cb;
5473 struct got_pathlist_head *merged_paths;
5476 static const struct got_error *
5477 collect_merged_paths(void *arg, unsigned char status, const char *path)
5479 const struct got_error *err;
5480 struct collect_merged_paths_arg *a = arg;
5482 struct got_pathlist_entry *new;
5484 err = (*a->progress_cb)(a->progress_arg, status, path);
5488 if (status != GOT_STATUS_MERGE &&
5489 status != GOT_STATUS_ADD &&
5490 status != GOT_STATUS_DELETE &&
5491 status != GOT_STATUS_CONFLICT)
5496 return got_error_from_errno("strdup");
5498 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5499 if (err || new == NULL)
5505 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5507 struct got_pathlist_entry *pe;
5509 TAILQ_FOREACH(pe, merged_paths, entry)
5510 free((char *)pe->path);
5512 got_pathlist_free(merged_paths);
5515 static const struct got_error *
5516 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5517 int is_rebase, struct got_repository *repo)
5519 const struct got_error *err;
5520 struct got_reference *commit_ref = NULL;
5522 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5524 if (err->code != GOT_ERR_NOT_REF)
5526 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
5529 err = got_ref_write(commit_ref, repo);
5532 } else if (is_rebase) {
5533 struct got_object_id *stored_id;
5536 err = got_ref_resolve(&stored_id, repo, commit_ref);
5539 cmp = got_object_id_cmp(commit_id, stored_id);
5542 err = got_error(GOT_ERR_REBASE_COMMITID);
5548 got_ref_close(commit_ref);
5552 static const struct got_error *
5553 rebase_merge_files(struct got_pathlist_head *merged_paths,
5554 const char *commit_ref_name, struct got_worktree *worktree,
5555 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
5556 struct got_object_id *commit_id, struct got_repository *repo,
5557 got_worktree_checkout_cb progress_cb, void *progress_arg,
5558 got_cancel_cb cancel_cb, void *cancel_arg)
5560 const struct got_error *err;
5561 struct got_reference *commit_ref = NULL;
5562 struct collect_merged_paths_arg cmp_arg;
5563 char *fileindex_path;
5565 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5567 err = get_fileindex_path(&fileindex_path, worktree);
5571 cmp_arg.progress_cb = progress_cb;
5572 cmp_arg.progress_arg = progress_arg;
5573 cmp_arg.merged_paths = merged_paths;
5574 err = merge_files(worktree, fileindex, fileindex_path,
5575 parent_commit_id, commit_id, repo, collect_merged_paths,
5576 &cmp_arg, cancel_cb, cancel_arg);
5578 got_ref_close(commit_ref);
5582 const struct got_error *
5583 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
5584 struct got_worktree *worktree, struct got_fileindex *fileindex,
5585 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5586 struct got_repository *repo,
5587 got_worktree_checkout_cb progress_cb, void *progress_arg,
5588 got_cancel_cb cancel_cb, void *cancel_arg)
5590 const struct got_error *err;
5591 char *commit_ref_name;
5593 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5597 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
5601 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5602 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5603 progress_arg, cancel_cb, cancel_arg);
5605 free(commit_ref_name);
5609 const struct got_error *
5610 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
5611 struct got_worktree *worktree, struct got_fileindex *fileindex,
5612 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5613 struct got_repository *repo,
5614 got_worktree_checkout_cb progress_cb, void *progress_arg,
5615 got_cancel_cb cancel_cb, void *cancel_arg)
5617 const struct got_error *err;
5618 char *commit_ref_name;
5620 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5624 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
5628 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5629 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5630 progress_arg, cancel_cb, cancel_arg);
5632 free(commit_ref_name);
5636 static const struct got_error *
5637 rebase_commit(struct got_object_id **new_commit_id,
5638 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
5639 struct got_worktree *worktree, struct got_fileindex *fileindex,
5640 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
5641 const char *new_logmsg, struct got_repository *repo)
5643 const struct got_error *err, *sync_err;
5644 struct got_pathlist_head commitable_paths;
5645 struct collect_commitables_arg cc_arg;
5646 char *fileindex_path = NULL;
5647 struct got_reference *head_ref = NULL;
5648 struct got_object_id *head_commit_id = NULL;
5649 char *logmsg = NULL;
5651 TAILQ_INIT(&commitable_paths);
5652 *new_commit_id = NULL;
5654 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5656 err = get_fileindex_path(&fileindex_path, worktree);
5660 cc_arg.commitable_paths = &commitable_paths;
5661 cc_arg.worktree = worktree;
5663 cc_arg.have_staged_files = 0;
5665 * If possible get the status of individual files directly to
5666 * avoid crawling the entire work tree once per rebased commit.
5667 * TODO: Ideally, merged_paths would contain a list of commitables
5668 * we could use so we could skip worktree_status() entirely.
5671 struct got_pathlist_entry *pe;
5672 TAILQ_FOREACH(pe, merged_paths, entry) {
5673 err = worktree_status(worktree, pe->path, fileindex,
5674 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
5680 err = worktree_status(worktree, "", fileindex, repo,
5681 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5686 if (TAILQ_EMPTY(&commitable_paths)) {
5687 /* No-op change; commit will be elided. */
5688 err = got_ref_delete(commit_ref, repo);
5691 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5695 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5699 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5704 logmsg = strdup(new_logmsg);
5705 if (logmsg == NULL) {
5706 err = got_error_from_errno("strdup");
5710 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
5715 /* NB: commit_worktree will call free(logmsg) */
5716 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
5717 worktree, got_object_commit_get_author(orig_commit),
5718 got_object_commit_get_committer(orig_commit),
5719 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
5723 err = got_ref_change_ref(tmp_branch, *new_commit_id);
5727 err = got_ref_delete(commit_ref, repo);
5731 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5733 sync_err = sync_fileindex(fileindex, fileindex_path);
5734 if (sync_err && err == NULL)
5737 free(fileindex_path);
5738 free(head_commit_id);
5740 got_ref_close(head_ref);
5742 free(*new_commit_id);
5743 *new_commit_id = NULL;
5748 const struct got_error *
5749 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
5750 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5751 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5752 struct got_commit_object *orig_commit,
5753 struct got_object_id *orig_commit_id, struct got_repository *repo)
5755 const struct got_error *err;
5756 char *commit_ref_name;
5757 struct got_reference *commit_ref = NULL;
5758 struct got_object_id *commit_id = NULL;
5760 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5764 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5767 err = got_ref_resolve(&commit_id, repo, commit_ref);
5770 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5771 err = got_error(GOT_ERR_REBASE_COMMITID);
5775 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5776 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
5779 got_ref_close(commit_ref);
5780 free(commit_ref_name);
5785 const struct got_error *
5786 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
5787 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5788 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5789 struct got_commit_object *orig_commit,
5790 struct got_object_id *orig_commit_id, const char *new_logmsg,
5791 struct got_repository *repo)
5793 const struct got_error *err;
5794 char *commit_ref_name;
5795 struct got_reference *commit_ref = NULL;
5797 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5801 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5805 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5806 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
5809 got_ref_close(commit_ref);
5810 free(commit_ref_name);
5814 const struct got_error *
5815 got_worktree_rebase_postpone(struct got_worktree *worktree,
5816 struct got_fileindex *fileindex)
5819 got_fileindex_free(fileindex);
5820 return lock_worktree(worktree, LOCK_SH);
5823 static const struct got_error *
5824 delete_ref(const char *name, struct got_repository *repo)
5826 const struct got_error *err;
5827 struct got_reference *ref;
5829 err = got_ref_open(&ref, repo, name, 0);
5831 if (err->code == GOT_ERR_NOT_REF)
5836 err = got_ref_delete(ref, repo);
5841 static const struct got_error *
5842 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
5844 const struct got_error *err;
5845 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5846 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5848 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5851 err = delete_ref(tmp_branch_name, repo);
5855 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5858 err = delete_ref(new_base_branch_ref_name, repo);
5862 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5865 err = delete_ref(branch_ref_name, repo);
5869 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5872 err = delete_ref(commit_ref_name, repo);
5877 free(tmp_branch_name);
5878 free(new_base_branch_ref_name);
5879 free(branch_ref_name);
5880 free(commit_ref_name);
5884 const struct got_error *
5885 got_worktree_rebase_complete(struct got_worktree *worktree,
5886 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
5887 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
5888 struct got_repository *repo)
5890 const struct got_error *err, *unlockerr;
5891 struct got_object_id *new_head_commit_id = NULL;
5893 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5897 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
5901 err = got_ref_write(rebased_branch, repo);
5905 err = got_worktree_set_head_ref(worktree, rebased_branch);
5909 err = delete_rebase_refs(worktree, repo);
5912 got_fileindex_free(fileindex);
5913 free(new_head_commit_id);
5914 unlockerr = lock_worktree(worktree, LOCK_SH);
5915 if (unlockerr && err == NULL)
5920 const struct got_error *
5921 got_worktree_rebase_abort(struct got_worktree *worktree,
5922 struct got_fileindex *fileindex, struct got_repository *repo,
5923 struct got_reference *new_base_branch,
5924 got_worktree_checkout_cb progress_cb, void *progress_arg)
5926 const struct got_error *err, *unlockerr, *sync_err;
5927 struct got_reference *resolved = NULL;
5928 struct got_object_id *commit_id = NULL;
5929 char *fileindex_path = NULL;
5930 struct revert_file_args rfa;
5931 struct got_object_id *tree_id = NULL;
5933 err = lock_worktree(worktree, LOCK_EX);
5937 err = got_ref_open(&resolved, repo,
5938 got_ref_get_symref_target(new_base_branch), 0);
5942 err = got_worktree_set_head_ref(worktree, resolved);
5947 * XXX commits to the base branch could have happened while
5948 * we were busy rebasing; should we store the original commit ID
5949 * when rebase begins and read it back here?
5951 err = got_ref_resolve(&commit_id, repo, resolved);
5955 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5959 err = got_object_id_by_path(&tree_id, repo,
5960 worktree->base_commit_id, worktree->path_prefix);
5964 err = delete_rebase_refs(worktree, repo);
5968 err = get_fileindex_path(&fileindex_path, worktree);
5972 rfa.worktree = worktree;
5973 rfa.fileindex = fileindex;
5974 rfa.progress_cb = progress_cb;
5975 rfa.progress_arg = progress_arg;
5976 rfa.patch_cb = NULL;
5977 rfa.patch_arg = NULL;
5979 err = worktree_status(worktree, "", fileindex, repo,
5980 revert_file, &rfa, NULL, NULL, 0, 0);
5984 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5985 repo, progress_cb, progress_arg, NULL, NULL);
5987 sync_err = sync_fileindex(fileindex, fileindex_path);
5988 if (sync_err && err == NULL)
5991 got_ref_close(resolved);
5995 got_fileindex_free(fileindex);
5996 free(fileindex_path);
5998 unlockerr = lock_worktree(worktree, LOCK_SH);
5999 if (unlockerr && err == NULL)
6004 const struct got_error *
6005 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6006 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6007 struct got_fileindex **fileindex, struct got_worktree *worktree,
6008 struct got_repository *repo)
6010 const struct got_error *err = NULL;
6011 char *tmp_branch_name = NULL;
6012 char *branch_ref_name = NULL;
6013 char *base_commit_ref_name = NULL;
6014 char *fileindex_path = NULL;
6015 struct check_rebase_ok_arg ok_arg;
6016 struct got_reference *wt_branch = NULL;
6017 struct got_reference *base_commit_ref = NULL;
6021 *base_commit_id = NULL;
6024 err = lock_worktree(worktree, LOCK_EX);
6028 err = open_fileindex(fileindex, &fileindex_path, worktree);
6032 ok_arg.worktree = worktree;
6034 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6039 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6043 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6047 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6052 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6057 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6061 err = got_ref_write(*branch_ref, repo);
6065 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6066 worktree->base_commit_id);
6069 err = got_ref_write(base_commit_ref, repo);
6072 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6073 if (*base_commit_id == NULL) {
6074 err = got_error_from_errno("got_object_id_dup");
6078 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6079 worktree->base_commit_id);
6082 err = got_ref_write(*tmp_branch, repo);
6086 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6090 free(fileindex_path);
6091 free(tmp_branch_name);
6092 free(branch_ref_name);
6093 free(base_commit_ref_name);
6095 got_ref_close(wt_branch);
6098 got_ref_close(*branch_ref);
6102 got_ref_close(*tmp_branch);
6105 free(*base_commit_id);
6107 got_fileindex_free(*fileindex);
6110 lock_worktree(worktree, LOCK_SH);
6115 const struct got_error *
6116 got_worktree_histedit_postpone(struct got_worktree *worktree,
6117 struct got_fileindex *fileindex)
6120 got_fileindex_free(fileindex);
6121 return lock_worktree(worktree, LOCK_SH);
6124 const struct got_error *
6125 got_worktree_histedit_in_progress(int *in_progress,
6126 struct got_worktree *worktree)
6128 const struct got_error *err;
6129 char *tmp_branch_name = NULL;
6131 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6135 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6136 free(tmp_branch_name);
6140 const struct got_error *
6141 got_worktree_histedit_continue(struct got_object_id **commit_id,
6142 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6143 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6144 struct got_worktree *worktree, struct got_repository *repo)
6146 const struct got_error *err;
6147 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6148 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6149 struct got_reference *commit_ref = NULL;
6150 struct got_reference *base_commit_ref = NULL;
6151 char *fileindex_path = NULL;
6152 int have_staged_files = 0;
6156 *base_commit_id = NULL;
6159 err = lock_worktree(worktree, LOCK_EX);
6163 err = open_fileindex(fileindex, &fileindex_path, worktree);
6167 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6168 &have_staged_files);
6169 if (err && err->code != GOT_ERR_CANCELLED)
6171 if (have_staged_files) {
6172 err = got_error(GOT_ERR_STAGED_PATHS);
6176 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6180 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6184 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6188 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6193 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6197 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6200 err = got_ref_resolve(commit_id, repo, commit_ref);
6204 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6207 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6211 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6215 free(commit_ref_name);
6216 free(branch_ref_name);
6217 free(fileindex_path);
6219 got_ref_close(commit_ref);
6220 if (base_commit_ref)
6221 got_ref_close(base_commit_ref);
6225 free(*base_commit_id);
6226 *base_commit_id = NULL;
6228 got_ref_close(*tmp_branch);
6232 got_fileindex_free(*fileindex);
6235 lock_worktree(worktree, LOCK_EX);
6240 static const struct got_error *
6241 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6243 const struct got_error *err;
6244 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6245 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6247 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6250 err = delete_ref(tmp_branch_name, repo);
6254 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6258 err = delete_ref(base_commit_ref_name, repo);
6262 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6265 err = delete_ref(branch_ref_name, repo);
6269 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6272 err = delete_ref(commit_ref_name, repo);
6276 free(tmp_branch_name);
6277 free(base_commit_ref_name);
6278 free(branch_ref_name);
6279 free(commit_ref_name);
6283 const struct got_error *
6284 got_worktree_histedit_abort(struct got_worktree *worktree,
6285 struct got_fileindex *fileindex, struct got_repository *repo,
6286 struct got_reference *branch, struct got_object_id *base_commit_id,
6287 got_worktree_checkout_cb progress_cb, void *progress_arg)
6289 const struct got_error *err, *unlockerr, *sync_err;
6290 struct got_reference *resolved = NULL;
6291 char *fileindex_path = NULL;
6292 struct got_object_id *tree_id = NULL;
6293 struct revert_file_args rfa;
6295 err = lock_worktree(worktree, LOCK_EX);
6299 err = got_ref_open(&resolved, repo,
6300 got_ref_get_symref_target(branch), 0);
6304 err = got_worktree_set_head_ref(worktree, resolved);
6308 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6312 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6313 worktree->path_prefix);
6317 err = delete_histedit_refs(worktree, repo);
6321 err = get_fileindex_path(&fileindex_path, worktree);
6325 rfa.worktree = worktree;
6326 rfa.fileindex = fileindex;
6327 rfa.progress_cb = progress_cb;
6328 rfa.progress_arg = progress_arg;
6329 rfa.patch_cb = NULL;
6330 rfa.patch_arg = NULL;
6332 err = worktree_status(worktree, "", fileindex, repo,
6333 revert_file, &rfa, NULL, NULL, 0, 0);
6337 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6338 repo, progress_cb, progress_arg, NULL, NULL);
6340 sync_err = sync_fileindex(fileindex, fileindex_path);
6341 if (sync_err && err == NULL)
6344 got_ref_close(resolved);
6346 free(fileindex_path);
6348 unlockerr = lock_worktree(worktree, LOCK_SH);
6349 if (unlockerr && err == NULL)
6354 const struct got_error *
6355 got_worktree_histedit_complete(struct got_worktree *worktree,
6356 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6357 struct got_reference *edited_branch, struct got_repository *repo)
6359 const struct got_error *err, *unlockerr;
6360 struct got_object_id *new_head_commit_id = NULL;
6361 struct got_reference *resolved = NULL;
6363 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6367 err = got_ref_open(&resolved, repo,
6368 got_ref_get_symref_target(edited_branch), 0);
6372 err = got_ref_change_ref(resolved, new_head_commit_id);
6376 err = got_ref_write(resolved, repo);
6380 err = got_worktree_set_head_ref(worktree, resolved);
6384 err = delete_histedit_refs(worktree, repo);
6387 got_fileindex_free(fileindex);
6388 free(new_head_commit_id);
6389 unlockerr = lock_worktree(worktree, LOCK_SH);
6390 if (unlockerr && err == NULL)
6395 const struct got_error *
6396 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6397 struct got_object_id *commit_id, struct got_repository *repo)
6399 const struct got_error *err;
6400 char *commit_ref_name;
6402 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6406 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6410 err = delete_ref(commit_ref_name, repo);
6412 free(commit_ref_name);
6416 const struct got_error *
6417 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6418 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6419 struct got_worktree *worktree, const char *refname,
6420 struct got_repository *repo)
6422 const struct got_error *err = NULL;
6423 char *fileindex_path = NULL;
6424 struct check_rebase_ok_arg ok_arg;
6428 *base_branch_ref = NULL;
6430 err = lock_worktree(worktree, LOCK_EX);
6434 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6435 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6436 "cannot integrate a branch into itself; "
6437 "update -b or different branch name required");
6441 err = open_fileindex(fileindex, &fileindex_path, worktree);
6445 /* Preconditions are the same as for rebase. */
6446 ok_arg.worktree = worktree;
6448 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6453 err = got_ref_open(branch_ref, repo, refname, 1);
6457 err = got_ref_open(base_branch_ref, repo,
6458 got_worktree_get_head_ref_name(worktree), 1);
6462 got_ref_close(*branch_ref);
6465 if (*base_branch_ref) {
6466 got_ref_close(*base_branch_ref);
6467 *base_branch_ref = NULL;
6470 got_fileindex_free(*fileindex);
6473 lock_worktree(worktree, LOCK_SH);
6478 const struct got_error *
6479 got_worktree_integrate_continue(struct got_worktree *worktree,
6480 struct got_fileindex *fileindex, struct got_repository *repo,
6481 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6482 got_worktree_checkout_cb progress_cb, void *progress_arg,
6483 got_cancel_cb cancel_cb, void *cancel_arg)
6485 const struct got_error *err = NULL, *sync_err, *unlockerr;
6486 char *fileindex_path = NULL;
6487 struct got_object_id *tree_id = NULL, *commit_id = NULL;
6489 err = get_fileindex_path(&fileindex_path, worktree);
6493 err = got_ref_resolve(&commit_id, repo, branch_ref);
6497 err = got_object_id_by_path(&tree_id, repo, commit_id,
6498 worktree->path_prefix);
6502 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6506 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6507 progress_cb, progress_arg, cancel_cb, cancel_arg);
6511 err = got_ref_change_ref(base_branch_ref, commit_id);
6515 err = got_ref_write(base_branch_ref, repo);
6517 sync_err = sync_fileindex(fileindex, fileindex_path);
6518 if (sync_err && err == NULL)
6522 unlockerr = got_ref_unlock(branch_ref);
6523 if (unlockerr && err == NULL)
6525 got_ref_close(branch_ref);
6527 unlockerr = got_ref_unlock(base_branch_ref);
6528 if (unlockerr && err == NULL)
6530 got_ref_close(base_branch_ref);
6532 got_fileindex_free(fileindex);
6533 free(fileindex_path);
6536 unlockerr = lock_worktree(worktree, LOCK_SH);
6537 if (unlockerr && err == NULL)
6542 const struct got_error *
6543 got_worktree_integrate_abort(struct got_worktree *worktree,
6544 struct got_fileindex *fileindex, struct got_repository *repo,
6545 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
6547 const struct got_error *err = NULL, *unlockerr = NULL;
6549 got_fileindex_free(fileindex);
6551 err = lock_worktree(worktree, LOCK_SH);
6553 unlockerr = got_ref_unlock(branch_ref);
6554 if (unlockerr && err == NULL)
6556 got_ref_close(branch_ref);
6558 unlockerr = got_ref_unlock(base_branch_ref);
6559 if (unlockerr && err == NULL)
6561 got_ref_close(base_branch_ref);
6566 struct check_stage_ok_arg {
6567 struct got_object_id *head_commit_id;
6568 struct got_worktree *worktree;
6569 struct got_fileindex *fileindex;
6570 struct got_repository *repo;
6574 const struct got_error *
6575 check_stage_ok(void *arg, unsigned char status,
6576 unsigned char staged_status, const char *relpath,
6577 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6578 struct got_object_id *commit_id, int dirfd, const char *de_name)
6580 struct check_stage_ok_arg *a = arg;
6581 const struct got_error *err = NULL;
6582 struct got_fileindex_entry *ie;
6583 struct got_object_id base_commit_id;
6584 struct got_object_id *base_commit_idp = NULL;
6585 char *in_repo_path = NULL, *p;
6587 if (status == GOT_STATUS_UNVERSIONED ||
6588 status == GOT_STATUS_NO_CHANGE)
6590 if (status == GOT_STATUS_NONEXISTENT)
6591 return got_error_set_errno(ENOENT, relpath);
6593 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6595 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6597 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
6598 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
6600 return got_error_from_errno("asprintf");
6602 if (got_fileindex_entry_has_commit(ie)) {
6603 memcpy(base_commit_id.sha1, ie->commit_sha1,
6604 SHA1_DIGEST_LENGTH);
6605 base_commit_idp = &base_commit_id;
6608 if (status == GOT_STATUS_CONFLICT) {
6609 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
6611 } else if (status != GOT_STATUS_ADD &&
6612 status != GOT_STATUS_MODIFY &&
6613 status != GOT_STATUS_DELETE) {
6614 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
6618 a->have_changes = 1;
6623 err = check_out_of_date(p, status, staged_status,
6624 blob_id, base_commit_idp, a->head_commit_id, a->repo,
6625 GOT_ERR_STAGE_OUT_OF_DATE);
6631 struct stage_path_arg {
6632 struct got_worktree *worktree;
6633 struct got_fileindex *fileindex;
6634 struct got_repository *repo;
6635 got_worktree_status_cb status_cb;
6637 got_worktree_patch_cb patch_cb;
6639 int staged_something;
6642 static const struct got_error *
6643 stage_path(void *arg, unsigned char status,
6644 unsigned char staged_status, const char *relpath,
6645 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6646 struct got_object_id *commit_id, int dirfd, const char *de_name)
6648 struct stage_path_arg *a = arg;
6649 const struct got_error *err = NULL;
6650 struct got_fileindex_entry *ie;
6651 char *ondisk_path = NULL, *path_content = NULL;
6653 struct got_object_id *new_staged_blob_id = NULL;
6655 if (status == GOT_STATUS_UNVERSIONED)
6658 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6660 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6662 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
6664 return got_error_from_errno("asprintf");
6667 case GOT_STATUS_ADD:
6668 case GOT_STATUS_MODIFY:
6670 if (status == GOT_STATUS_ADD) {
6671 int choice = GOT_PATCH_CHOICE_NONE;
6672 err = (*a->patch_cb)(&choice, a->patch_arg,
6673 status, ie->path, NULL, 1, 1);
6676 if (choice != GOT_PATCH_CHOICE_YES)
6679 err = create_patched_content(&path_content, 0,
6680 staged_blob_id ? staged_blob_id : blob_id,
6681 ondisk_path, dirfd, de_name, ie->path,
6682 a->repo, a->patch_cb, a->patch_arg);
6683 if (err || path_content == NULL)
6687 err = got_object_blob_create(&new_staged_blob_id,
6688 path_content ? path_content : ondisk_path, a->repo);
6691 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
6692 SHA1_DIGEST_LENGTH);
6693 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
6694 stage = GOT_FILEIDX_STAGE_ADD;
6696 stage = GOT_FILEIDX_STAGE_MODIFY;
6697 got_fileindex_entry_stage_set(ie, stage);
6698 a->staged_something = 1;
6699 if (a->status_cb == NULL)
6701 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6702 get_staged_status(ie), relpath, blob_id,
6703 new_staged_blob_id, NULL, dirfd, de_name);
6705 case GOT_STATUS_DELETE:
6706 if (staged_status == GOT_STATUS_DELETE)
6709 int choice = GOT_PATCH_CHOICE_NONE;
6710 err = (*a->patch_cb)(&choice, a->patch_arg, status,
6711 ie->path, NULL, 1, 1);
6714 if (choice == GOT_PATCH_CHOICE_NO)
6716 if (choice != GOT_PATCH_CHOICE_YES) {
6717 err = got_error(GOT_ERR_PATCH_CHOICE);
6721 stage = GOT_FILEIDX_STAGE_DELETE;
6722 got_fileindex_entry_stage_set(ie, stage);
6723 a->staged_something = 1;
6724 if (a->status_cb == NULL)
6726 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6727 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
6730 case GOT_STATUS_NO_CHANGE:
6732 case GOT_STATUS_CONFLICT:
6733 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
6735 case GOT_STATUS_NONEXISTENT:
6736 err = got_error_set_errno(ENOENT, relpath);
6739 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
6743 if (path_content && unlink(path_content) == -1 && err == NULL)
6744 err = got_error_from_errno2("unlink", path_content);
6747 free(new_staged_blob_id);
6751 const struct got_error *
6752 got_worktree_stage(struct got_worktree *worktree,
6753 struct got_pathlist_head *paths,
6754 got_worktree_status_cb status_cb, void *status_arg,
6755 got_worktree_patch_cb patch_cb, void *patch_arg,
6756 struct got_repository *repo)
6758 const struct got_error *err = NULL, *sync_err, *unlockerr;
6759 struct got_pathlist_entry *pe;
6760 struct got_fileindex *fileindex = NULL;
6761 char *fileindex_path = NULL;
6762 struct got_reference *head_ref = NULL;
6763 struct got_object_id *head_commit_id = NULL;
6764 struct check_stage_ok_arg oka;
6765 struct stage_path_arg spa;
6767 err = lock_worktree(worktree, LOCK_EX);
6771 err = got_ref_open(&head_ref, repo,
6772 got_worktree_get_head_ref_name(worktree), 0);
6775 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6778 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6782 /* Check pre-conditions before staging anything. */
6783 oka.head_commit_id = head_commit_id;
6784 oka.worktree = worktree;
6785 oka.fileindex = fileindex;
6787 oka.have_changes = 0;
6788 TAILQ_FOREACH(pe, paths, entry) {
6789 err = worktree_status(worktree, pe->path, fileindex, repo,
6790 check_stage_ok, &oka, NULL, NULL, 0, 0);
6794 if (!oka.have_changes) {
6795 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6799 spa.worktree = worktree;
6800 spa.fileindex = fileindex;
6802 spa.patch_cb = patch_cb;
6803 spa.patch_arg = patch_arg;
6804 spa.status_cb = status_cb;
6805 spa.status_arg = status_arg;
6806 spa.staged_something = 0;
6807 TAILQ_FOREACH(pe, paths, entry) {
6808 err = worktree_status(worktree, pe->path, fileindex, repo,
6809 stage_path, &spa, NULL, NULL, 0, 0);
6813 if (!spa.staged_something) {
6814 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6818 sync_err = sync_fileindex(fileindex, fileindex_path);
6819 if (sync_err && err == NULL)
6823 got_ref_close(head_ref);
6824 free(head_commit_id);
6825 free(fileindex_path);
6827 got_fileindex_free(fileindex);
6828 unlockerr = lock_worktree(worktree, LOCK_SH);
6829 if (unlockerr && err == NULL)
6834 struct unstage_path_arg {
6835 struct got_worktree *worktree;
6836 struct got_fileindex *fileindex;
6837 struct got_repository *repo;
6838 got_worktree_checkout_cb progress_cb;
6840 got_worktree_patch_cb patch_cb;
6844 static const struct got_error *
6845 create_unstaged_content(char **path_unstaged_content,
6846 char **path_new_staged_content, struct got_object_id *blob_id,
6847 struct got_object_id *staged_blob_id, const char *relpath,
6848 struct got_repository *repo,
6849 got_worktree_patch_cb patch_cb, void *patch_arg)
6851 const struct got_error *err;
6852 struct got_blob_object *blob = NULL, *staged_blob = NULL;
6853 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
6854 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
6855 struct stat sb1, sb2;
6856 struct got_diff_changes *changes = NULL;
6857 struct got_diff_state *ds = NULL;
6858 struct got_diff_args *args = NULL;
6859 struct got_diff_change *change;
6860 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
6861 int have_content = 0, have_rejected_content = 0;
6863 *path_unstaged_content = NULL;
6864 *path_new_staged_content = NULL;
6866 err = got_object_id_str(&label1, blob_id);
6869 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
6873 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
6877 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
6881 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
6885 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
6889 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
6893 if (stat(path1, &sb1) == -1) {
6894 err = got_error_from_errno2("stat", path1);
6898 if (stat(path2, &sb2) == -1) {
6899 err = got_error_from_errno2("stat", path2);
6903 err = got_diff_files(&changes, &ds, &args, &diff_flags,
6904 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
6908 err = got_opentemp_named(path_unstaged_content, &outfile,
6909 "got-unstaged-content");
6912 err = got_opentemp_named(path_new_staged_content, &rejectfile,
6913 "got-new-staged-content");
6917 if (fseek(f1, 0L, SEEK_SET) == -1) {
6918 err = got_ferror(f1, GOT_ERR_IO);
6921 if (fseek(f2, 0L, SEEK_SET) == -1) {
6922 err = got_ferror(f2, GOT_ERR_IO);
6925 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
6927 err = apply_or_reject_change(&choice, change, ++n,
6928 changes->nchanges, ds, args, diff_flags, relpath,
6929 f1, f2, &line_cur1, &line_cur2,
6930 outfile, rejectfile, patch_cb, patch_arg);
6933 if (choice == GOT_PATCH_CHOICE_YES)
6936 have_rejected_content = 1;
6937 if (choice == GOT_PATCH_CHOICE_QUIT)
6940 if (have_content || have_rejected_content)
6941 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
6942 outfile, rejectfile);
6946 got_object_blob_close(blob);
6948 got_object_blob_close(staged_blob);
6949 if (f1 && fclose(f1) == EOF && err == NULL)
6950 err = got_error_from_errno2("fclose", path1);
6951 if (f2 && fclose(f2) == EOF && err == NULL)
6952 err = got_error_from_errno2("fclose", path2);
6953 if (outfile && fclose(outfile) == EOF && err == NULL)
6954 err = got_error_from_errno2("fclose", *path_unstaged_content);
6955 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
6956 err = got_error_from_errno2("fclose", *path_new_staged_content);
6957 if (path1 && unlink(path1) == -1 && err == NULL)
6958 err = got_error_from_errno2("unlink", path1);
6959 if (path2 && unlink(path2) == -1 && err == NULL)
6960 err = got_error_from_errno2("unlink", path2);
6961 if (err || !have_content) {
6962 if (*path_unstaged_content &&
6963 unlink(*path_unstaged_content) == -1 && err == NULL)
6964 err = got_error_from_errno2("unlink",
6965 *path_unstaged_content);
6966 free(*path_unstaged_content);
6967 *path_unstaged_content = NULL;
6969 if (err || !have_rejected_content) {
6970 if (*path_new_staged_content &&
6971 unlink(*path_new_staged_content) == -1 && err == NULL)
6972 err = got_error_from_errno2("unlink",
6973 *path_new_staged_content);
6974 free(*path_new_staged_content);
6975 *path_new_staged_content = NULL;
6979 got_diff_state_free(ds);
6983 got_diff_free_changes(changes);
6989 static const struct got_error *
6990 unstage_path(void *arg, unsigned char status,
6991 unsigned char staged_status, const char *relpath,
6992 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6993 struct got_object_id *commit_id, int dirfd, const char *de_name)
6995 const struct got_error *err = NULL;
6996 struct unstage_path_arg *a = arg;
6997 struct got_fileindex_entry *ie;
6998 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
6999 char *ondisk_path = NULL, *path_unstaged_content = NULL;
7000 char *path_new_staged_content = NULL;
7001 char *id_str = NULL, *label_orig = NULL;
7002 int local_changes_subsumed;
7005 if (staged_status != GOT_STATUS_ADD &&
7006 staged_status != GOT_STATUS_MODIFY &&
7007 staged_status != GOT_STATUS_DELETE)
7010 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7012 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7014 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7016 return got_error_from_errno("asprintf");
7018 err = got_object_id_str(&id_str,
7019 commit_id ? commit_id : a->worktree->base_commit_id);
7022 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7024 err = got_error_from_errno("asprintf");
7028 switch (staged_status) {
7029 case GOT_STATUS_MODIFY:
7030 err = got_object_open_as_blob(&blob_base, a->repo,
7035 case GOT_STATUS_ADD:
7037 if (staged_status == GOT_STATUS_ADD) {
7038 int choice = GOT_PATCH_CHOICE_NONE;
7039 err = (*a->patch_cb)(&choice, a->patch_arg,
7040 staged_status, ie->path, NULL, 1, 1);
7043 if (choice != GOT_PATCH_CHOICE_YES)
7046 err = create_unstaged_content(
7047 &path_unstaged_content,
7048 &path_new_staged_content, blob_id,
7049 staged_blob_id, ie->path, a->repo,
7050 a->patch_cb, a->patch_arg);
7051 if (err || path_unstaged_content == NULL)
7053 if (path_new_staged_content) {
7054 err = got_object_blob_create(
7056 path_new_staged_content,
7060 memcpy(ie->staged_blob_sha1,
7061 staged_blob_id->sha1,
7062 SHA1_DIGEST_LENGTH);
7064 err = merge_file(&local_changes_subsumed,
7065 a->worktree, blob_base, ondisk_path,
7066 relpath, got_fileindex_perms_to_st(ie),
7067 path_unstaged_content, label_orig,
7068 "unstaged", a->repo, a->progress_cb,
7071 path_new_staged_content == NULL)
7072 got_fileindex_entry_stage_set(ie,
7073 GOT_FILEIDX_STAGE_NONE);
7074 break; /* Done with this file. */
7077 err = got_object_open_as_blob(&blob_staged, a->repo,
7078 staged_blob_id, 8192);
7081 err = merge_blob(&local_changes_subsumed, a->worktree,
7082 blob_base, ondisk_path, relpath,
7083 got_fileindex_perms_to_st(ie), label_orig, blob_staged,
7084 commit_id ? commit_id : a->worktree->base_commit_id,
7085 a->repo, a->progress_cb, a->progress_arg);
7087 got_fileindex_entry_stage_set(ie,
7088 GOT_FILEIDX_STAGE_NONE);
7090 case GOT_STATUS_DELETE:
7092 int choice = GOT_PATCH_CHOICE_NONE;
7093 err = (*a->patch_cb)(&choice, a->patch_arg,
7094 staged_status, ie->path, NULL, 1, 1);
7097 if (choice == GOT_PATCH_CHOICE_NO)
7099 if (choice != GOT_PATCH_CHOICE_YES) {
7100 err = got_error(GOT_ERR_PATCH_CHOICE);
7104 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7105 err = get_file_status(&status, &sb, ie, ondisk_path,
7106 dirfd, de_name, a->repo);
7109 err = (*a->progress_cb)(a->progress_arg, status, relpath);
7114 if (path_unstaged_content &&
7115 unlink(path_unstaged_content) == -1 && err == NULL)
7116 err = got_error_from_errno2("unlink", path_unstaged_content);
7117 if (path_new_staged_content &&
7118 unlink(path_new_staged_content) == -1 && err == NULL)
7119 err = got_error_from_errno2("unlink", path_new_staged_content);
7120 free(path_unstaged_content);
7121 free(path_new_staged_content);
7123 got_object_blob_close(blob_base);
7125 got_object_blob_close(blob_staged);
7131 const struct got_error *
7132 got_worktree_unstage(struct got_worktree *worktree,
7133 struct got_pathlist_head *paths,
7134 got_worktree_checkout_cb progress_cb, void *progress_arg,
7135 got_worktree_patch_cb patch_cb, void *patch_arg,
7136 struct got_repository *repo)
7138 const struct got_error *err = NULL, *sync_err, *unlockerr;
7139 struct got_pathlist_entry *pe;
7140 struct got_fileindex *fileindex = NULL;
7141 char *fileindex_path = NULL;
7142 struct unstage_path_arg upa;
7144 err = lock_worktree(worktree, LOCK_EX);
7148 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7152 upa.worktree = worktree;
7153 upa.fileindex = fileindex;
7155 upa.progress_cb = progress_cb;
7156 upa.progress_arg = progress_arg;
7157 upa.patch_cb = patch_cb;
7158 upa.patch_arg = patch_arg;
7159 TAILQ_FOREACH(pe, paths, entry) {
7160 err = worktree_status(worktree, pe->path, fileindex, repo,
7161 unstage_path, &upa, NULL, NULL, 0, 0);
7166 sync_err = sync_fileindex(fileindex, fileindex_path);
7167 if (sync_err && err == NULL)
7170 free(fileindex_path);
7172 got_fileindex_free(fileindex);
7173 unlockerr = lock_worktree(worktree, LOCK_SH);
7174 if (unlockerr && err == NULL)