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",
883 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
888 err = got_object_blob_read_to_str(&deriv_target, blob_deriv);
892 if (ancestor_target && (ondisk_len != strlen(ancestor_target) ||
893 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0)) {
895 * The symlink has changed on-disk (second derived version).
896 * Keep that change and discard the incoming change (first
898 * TODO: Need tree-conflict resolution to handle this.
900 err = (*progress_cb)(progress_arg, GOT_STATUS_OBSTRUCTED,
902 } else if (ondisk_len == strlen(deriv_target) &&
903 memcmp(ondisk_target, deriv_target, ondisk_len) == 0) {
904 /* Both versions made the same change. */
905 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
907 /* Apply the incoming change. */
908 err = update_symlink(ondisk_path, deriv_target,
909 strlen(deriv_target));
912 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
915 free(ancestor_target);
921 * Perform a 3-way merge where blob_orig acts as the common ancestor,
922 * blob_deriv acts as the first derived version, and the file on disk
923 * acts as the second derived version.
925 static const struct got_error *
926 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
927 struct got_blob_object *blob_orig, const char *ondisk_path,
928 const char *path, uint16_t st_mode, const char *label_orig,
929 struct got_blob_object *blob_deriv,
930 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
931 got_worktree_checkout_cb progress_cb, void *progress_arg)
933 const struct got_error *err = NULL;
934 FILE *f_deriv = NULL;
935 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
936 char *label_deriv = NULL, *parent;
938 *local_changes_subsumed = 0;
940 parent = dirname(ondisk_path);
942 return got_error_from_errno2("dirname", ondisk_path);
945 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
946 err = got_error_from_errno("asprintf");
951 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
954 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
959 err = got_object_id_str(&id_str, deriv_base_commit_id);
962 if (asprintf(&label_deriv, "%s: commit %s",
963 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
964 err = got_error_from_errno("asprintf");
968 err = merge_file(local_changes_subsumed, worktree, blob_orig,
969 ondisk_path, path, st_mode, blob_deriv_path, label_orig,
970 label_deriv, repo, progress_cb, progress_arg);
972 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
973 err = got_error_from_errno("fclose");
975 if (blob_deriv_path) {
976 unlink(blob_deriv_path);
977 free(blob_deriv_path);
984 static const struct got_error *
985 create_fileindex_entry(struct got_fileindex *fileindex,
986 struct got_object_id *base_commit_id, const char *ondisk_path,
987 const char *path, struct got_object_id *blob_id)
989 const struct got_error *err = NULL;
990 struct got_fileindex_entry *new_ie;
992 err = got_fileindex_entry_alloc(&new_ie, path);
996 err = got_fileindex_entry_update(new_ie, ondisk_path,
997 blob_id->sha1, base_commit_id->sha1, 1);
1001 err = got_fileindex_entry_add(fileindex, new_ie);
1004 got_fileindex_entry_free(new_ie);
1009 get_ondisk_perms(int executable, mode_t st_mode)
1011 mode_t xbits = S_IXUSR;
1014 /* Map read bits to execute bits. */
1015 if (st_mode & S_IRGRP)
1017 if (st_mode & S_IROTH)
1019 return st_mode | xbits;
1022 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1025 /* forward declaration */
1026 static const struct got_error *
1027 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1028 const char *path, mode_t te_mode, mode_t st_mode,
1029 struct got_blob_object *blob, int restoring_missing_file,
1030 int reverting_versioned_file, int installing_bad_symlink,
1031 struct got_repository *repo,
1032 got_worktree_checkout_cb progress_cb, void *progress_arg);
1035 * This function assumes that the provided symlink target points at a
1036 * safe location in the work tree!
1038 static const struct got_error *
1039 replace_existing_symlink(const char *ondisk_path, const char *target_path,
1042 const struct got_error *err = NULL;
1044 char etarget[PATH_MAX];
1048 * "Bad" symlinks (those pointing outside the work tree or into the
1049 * .got directory) are installed in the work tree as a regular file
1050 * which contains the bad symlink target path.
1051 * The new symlink target has already been checked for safety by our
1052 * caller. If we can successfully open a regular file then we simply
1053 * replace this file with a symlink below.
1055 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1058 return got_error_from_errno2("open", ondisk_path);
1060 /* We are updating an existing on-disk symlink. */
1061 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1063 return got_error_from_errno2("readlink", ondisk_path);
1065 if (elen == target_len &&
1066 memcmp(etarget, target_path, target_len) == 0)
1067 return NULL; /* nothing to do */
1070 err = update_symlink(ondisk_path, target_path, target_len);
1071 if (fd != -1 && close(fd) == -1 && err == NULL)
1072 err = got_error_from_errno2("close", ondisk_path);
1076 static const struct got_error *
1077 install_symlink(struct got_worktree *worktree, const char *ondisk_path,
1078 const char *path, mode_t te_mode, mode_t st_mode,
1079 struct got_blob_object *blob, int restoring_missing_file,
1080 int reverting_versioned_file, struct got_repository *repo,
1081 got_worktree_checkout_cb progress_cb, void *progress_arg)
1083 const struct got_error *err = NULL;
1084 char target_path[PATH_MAX];
1085 size_t len, target_len = 0;
1086 char *resolved_path = NULL, *abspath = NULL;
1087 char *path_got = NULL;
1088 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1089 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1092 * Blob object content specifies the target path of the link.
1093 * If a symbolic link cannot be installed we instead create
1094 * a regular file which contains the link target path stored
1095 * in the blob object.
1098 err = got_object_blob_read_block(&len, blob);
1099 if (len + target_len >= sizeof(target_path)) {
1100 /* Path too long; install as a regular file. */
1101 got_object_blob_rewind(blob);
1102 return install_blob(worktree, ondisk_path, path,
1103 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1104 restoring_missing_file, reverting_versioned_file,
1105 1, repo, progress_cb, progress_arg);
1108 /* Skip blob object header first time around. */
1109 memcpy(target_path + target_len, buf + hdrlen,
1111 target_len += len - hdrlen;
1115 target_path[target_len] = '\0';
1118 * Relative symlink target lookup should begin at the directory
1119 * in which the blob object is being installed.
1121 if (!got_path_is_absolute(target_path)) {
1122 char *parent = dirname(ondisk_path);
1123 if (parent == NULL) {
1124 err = got_error_from_errno2("dirname", ondisk_path);
1127 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1128 err = got_error_from_errno("asprintf");
1134 * unveil(2) restricts our view of paths in the filesystem.
1135 * ENOENT will occur if a link target path does not exist or
1136 * if it points outside our unveiled path space.
1138 resolved_path = realpath(abspath ? abspath : target_path, NULL);
1139 if (resolved_path == NULL) {
1140 if (errno != ENOENT) {
1141 err = got_error_from_errno2("realpath", target_path);
1146 /* Only allow symlinks pointing at paths within the work tree. */
1147 if (!got_path_is_child(resolved_path ? resolved_path : (abspath ?
1148 abspath : target_path), worktree->root_path,
1149 strlen(worktree->root_path))) {
1150 /* install as a regular file */
1151 got_object_blob_rewind(blob);
1152 err = install_blob(worktree, ondisk_path, path,
1153 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1154 restoring_missing_file, reverting_versioned_file, 1,
1155 repo, progress_cb, progress_arg);
1159 /* Do not allow symlinks pointing into the .got directory. */
1160 if (asprintf(&path_got, "%s/%s", worktree->root_path,
1161 GOT_WORKTREE_GOT_DIR) == -1) {
1162 err = got_error_from_errno("asprintf");
1165 if (got_path_is_child(resolved_path ? resolved_path : (abspath ?
1166 abspath : target_path), path_got, strlen(path_got))) {
1167 /* install as a regular file */
1168 got_object_blob_rewind(blob);
1169 err = install_blob(worktree, ondisk_path, path,
1170 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1171 restoring_missing_file, reverting_versioned_file, 1,
1172 repo, progress_cb, progress_arg);
1176 if (symlink(target_path, ondisk_path) == -1) {
1177 if (errno == EEXIST) {
1178 err = replace_existing_symlink(ondisk_path,
1179 target_path, target_len);
1183 err = (*progress_cb)(progress_arg,
1184 GOT_STATUS_UPDATE, path);
1186 goto done; /* Nothing else to do. */
1189 if (errno == ENOENT) {
1190 char *parent = dirname(ondisk_path);
1191 if (parent == NULL) {
1192 err = got_error_from_errno2("dirname",
1196 err = add_dir_on_disk(worktree, parent);
1200 * Retry, and fall through to error handling
1201 * below if this second attempt fails.
1203 if (symlink(target_path, ondisk_path) != -1) {
1204 err = NULL; /* success */
1209 /* Handle errors from first or second creation attempt. */
1210 if (errno == ENAMETOOLONG) {
1211 /* bad target path; install as a regular file */
1212 got_object_blob_rewind(blob);
1213 err = install_blob(worktree, ondisk_path, path,
1214 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1215 restoring_missing_file, reverting_versioned_file, 1,
1216 repo, progress_cb, progress_arg);
1217 } else if (errno == ENOTDIR) {
1218 err = got_error_path(ondisk_path,
1219 GOT_ERR_FILE_OBSTRUCTED);
1221 err = got_error_from_errno3("symlink",
1222 target_path, ondisk_path);
1224 } else if (progress_cb)
1225 err = (*progress_cb)(progress_arg, GOT_STATUS_ADD, path);
1227 free(resolved_path);
1233 static const struct got_error *
1234 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1235 const char *path, mode_t te_mode, mode_t st_mode,
1236 struct got_blob_object *blob, int restoring_missing_file,
1237 int reverting_versioned_file, int installing_bad_symlink,
1238 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1241 const struct got_error *err = NULL;
1245 char *tmppath = NULL;
1247 if (S_ISLNK(te_mode))
1248 return install_symlink(worktree, ondisk_path, path, te_mode,
1249 st_mode, blob, restoring_missing_file,
1250 reverting_versioned_file, repo, progress_cb, progress_arg);
1252 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1253 GOT_DEFAULT_FILE_MODE);
1255 if (errno == ENOENT) {
1256 char *parent = dirname(path);
1258 return got_error_from_errno2("dirname", path);
1259 err = add_dir_on_disk(worktree, parent);
1262 fd = open(ondisk_path,
1263 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1264 GOT_DEFAULT_FILE_MODE);
1266 return got_error_from_errno2("open",
1268 } else if (errno == EEXIST) {
1269 if (!S_ISREG(st_mode) && !installing_bad_symlink) {
1270 /* TODO file is obstructed; do something */
1271 err = got_error_path(ondisk_path,
1272 GOT_ERR_FILE_OBSTRUCTED);
1275 err = got_opentemp_named_fd(&tmppath, &fd,
1282 return got_error_from_errno2("open", ondisk_path);
1286 if (restoring_missing_file)
1287 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1289 else if (reverting_versioned_file)
1290 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1293 err = (*progress_cb)(progress_arg,
1294 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1299 hdrlen = got_object_blob_get_hdrlen(blob);
1301 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1302 err = got_object_blob_read_block(&len, blob);
1306 /* Skip blob object header first time around. */
1307 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1309 err = got_error_from_errno("write");
1311 } else if (outlen != len - hdrlen) {
1312 err = got_error(GOT_ERR_IO);
1319 if (fsync(fd) != 0) {
1320 err = got_error_from_errno("fsync");
1325 if (rename(tmppath, ondisk_path) != 0) {
1326 err = got_error_from_errno3("rename", tmppath,
1333 if (chmod(ondisk_path,
1334 get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1335 err = got_error_from_errno2("chmod", ondisk_path);
1340 if (fd != -1 && close(fd) != 0 && err == NULL)
1341 err = got_error_from_errno("close");
1346 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1347 static const struct got_error *
1348 get_modified_file_content_status(unsigned char *status, FILE *f)
1350 const struct got_error *err = NULL;
1351 const char *markers[3] = {
1352 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1353 GOT_DIFF_CONFLICT_MARKER_SEP,
1354 GOT_DIFF_CONFLICT_MARKER_END
1359 const char delim[3] = {'\0', '\0', '\0'};
1361 while (*status == GOT_STATUS_MODIFY) {
1362 line = fparseln(f, &len, NULL, delim, 0);
1366 err = got_ferror(f, GOT_ERR_IO);
1370 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1371 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1373 *status = GOT_STATUS_CONFLICT;
1383 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1385 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1386 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1390 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1392 return !(ie->ctime_sec == sb->st_ctime &&
1393 ie->ctime_nsec == sb->st_ctimensec &&
1394 ie->mtime_sec == sb->st_mtime &&
1395 ie->mtime_nsec == sb->st_mtimensec &&
1396 ie->size == (sb->st_size & 0xffffffff) &&
1397 !xbit_differs(ie, sb->st_mode));
1400 static unsigned char
1401 get_staged_status(struct got_fileindex_entry *ie)
1403 switch (got_fileindex_entry_stage_get(ie)) {
1404 case GOT_FILEIDX_STAGE_ADD:
1405 return GOT_STATUS_ADD;
1406 case GOT_FILEIDX_STAGE_DELETE:
1407 return GOT_STATUS_DELETE;
1408 case GOT_FILEIDX_STAGE_MODIFY:
1409 return GOT_STATUS_MODIFY;
1411 return GOT_STATUS_NO_CHANGE;
1415 static const struct got_error *
1416 get_symlink_status(unsigned char *status, struct stat *sb,
1417 struct got_fileindex_entry *ie, const char *abspath,
1418 int dirfd, const char *de_name, struct got_blob_object *blob)
1420 const struct got_error *err = NULL;
1421 char target_path[PATH_MAX];
1422 char etarget[PATH_MAX];
1424 size_t len, target_len = 0;
1425 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1426 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1428 *status = GOT_STATUS_NO_CHANGE;
1430 /* Blob object content specifies the target path of the link. */
1432 err = got_object_blob_read_block(&len, blob);
1435 if (len + target_len >= sizeof(target_path)) {
1437 * Should not happen. The blob contents were OK
1438 * when this symlink was installed.
1440 return got_error(GOT_ERR_NO_SPACE);
1443 /* Skip blob object header first time around. */
1444 memcpy(target_path + target_len, buf + hdrlen,
1446 target_len += len - hdrlen;
1450 target_path[target_len] = '\0';
1453 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1455 return got_error_from_errno2("readlinkat", abspath);
1457 elen = readlink(abspath, etarget, sizeof(etarget));
1459 return got_error_from_errno2("readlinkat", abspath);
1462 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1463 *status = GOT_STATUS_MODIFY;
1468 static const struct got_error *
1469 get_file_status(unsigned char *status, struct stat *sb,
1470 struct got_fileindex_entry *ie, const char *abspath,
1471 int dirfd, const char *de_name, struct got_repository *repo)
1473 const struct got_error *err = NULL;
1474 struct got_object_id id;
1479 struct got_blob_object *blob = NULL;
1481 unsigned char staged_status = get_staged_status(ie);
1483 *status = GOT_STATUS_NO_CHANGE;
1486 * Whenever the caller provides a directory descriptor and a
1487 * directory entry name for the file, use them! This prevents
1488 * race conditions if filesystem paths change beneath our feet.
1491 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1492 if (errno == ENOENT) {
1493 if (got_fileindex_entry_has_file_on_disk(ie))
1494 *status = GOT_STATUS_MISSING;
1496 *status = GOT_STATUS_DELETE;
1499 err = got_error_from_errno2("fstatat", abspath);
1503 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1504 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1505 return got_error_from_errno2("open", abspath);
1506 else if (fd == -1 && errno == ELOOP) {
1507 if (lstat(abspath, sb) == -1)
1508 return got_error_from_errno2("lstat", abspath);
1509 } else if (fd == -1 || fstat(fd, sb) == -1) {
1510 if (errno == ENOENT) {
1511 if (got_fileindex_entry_has_file_on_disk(ie))
1512 *status = GOT_STATUS_MISSING;
1514 *status = GOT_STATUS_DELETE;
1517 err = got_error_from_errno2("fstat", abspath);
1522 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1523 *status = GOT_STATUS_OBSTRUCTED;
1527 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1528 *status = GOT_STATUS_DELETE;
1530 } else if (!got_fileindex_entry_has_blob(ie) &&
1531 staged_status != GOT_STATUS_ADD) {
1532 *status = GOT_STATUS_ADD;
1536 if (!stat_info_differs(ie, sb))
1539 if (staged_status == GOT_STATUS_MODIFY ||
1540 staged_status == GOT_STATUS_ADD)
1541 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1543 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1545 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1549 if (S_ISLNK(sb->st_mode)) {
1550 /* Staging changes to symlinks is not yet(?) supported. */
1551 if (staged_status != GOT_STATUS_NO_CHANGE) {
1552 err = got_error_path(abspath, GOT_ERR_FILE_STATUS);
1555 err = get_symlink_status(status, sb, ie, abspath, dirfd,
1562 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1564 err = got_error_from_errno2("openat", abspath);
1569 f = fdopen(fd, "r");
1571 err = got_error_from_errno2("fdopen", abspath);
1575 hdrlen = got_object_blob_get_hdrlen(blob);
1577 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1578 err = got_object_blob_read_block(&blen, blob);
1581 /* Skip length of blob object header first time around. */
1582 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1583 if (flen == 0 && ferror(f)) {
1584 err = got_error_from_errno("fread");
1589 *status = GOT_STATUS_MODIFY;
1591 } else if (flen == 0) {
1593 *status = GOT_STATUS_MODIFY;
1595 } else if (blen - hdrlen == flen) {
1596 /* Skip blob object header first time around. */
1597 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1598 *status = GOT_STATUS_MODIFY;
1602 *status = GOT_STATUS_MODIFY;
1608 if (*status == GOT_STATUS_MODIFY) {
1610 err = get_modified_file_content_status(status, f);
1611 } else if (xbit_differs(ie, sb->st_mode))
1612 *status = GOT_STATUS_MODE_CHANGE;
1615 got_object_blob_close(blob);
1616 if (f != NULL && fclose(f) == EOF && err == NULL)
1617 err = got_error_from_errno2("fclose", abspath);
1618 if (fd != -1 && close(fd) == -1 && err == NULL)
1619 err = got_error_from_errno2("close", abspath);
1624 * Update timestamps in the file index if a file is unmodified and
1625 * we had to run a full content comparison to find out.
1627 static const struct got_error *
1628 sync_timestamps(char *ondisk_path, unsigned char status,
1629 struct got_fileindex_entry *ie, struct stat *sb)
1631 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1632 return got_fileindex_entry_update(ie, ondisk_path,
1633 ie->blob_sha1, ie->commit_sha1, 1);
1638 static const struct got_error *
1639 update_blob(struct got_worktree *worktree,
1640 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1641 struct got_tree_entry *te, const char *path,
1642 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1645 const struct got_error *err = NULL;
1646 struct got_blob_object *blob = NULL;
1648 unsigned char status = GOT_STATUS_NO_CHANGE;
1651 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1652 return got_error_from_errno("asprintf");
1655 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1656 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1659 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1663 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1664 sb.st_mode = got_fileindex_perms_to_st(ie);
1666 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1668 if (status == GOT_STATUS_OBSTRUCTED) {
1669 err = (*progress_cb)(progress_arg, status, path);
1672 if (status == GOT_STATUS_CONFLICT) {
1673 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1678 if (ie && status != GOT_STATUS_MISSING &&
1679 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1680 if (got_fileindex_entry_has_commit(ie) &&
1681 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1682 SHA1_DIGEST_LENGTH) == 0) {
1683 err = sync_timestamps(ondisk_path, status, ie, &sb);
1686 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1690 if (got_fileindex_entry_has_blob(ie) &&
1691 memcmp(ie->blob_sha1, te->id.sha1,
1692 SHA1_DIGEST_LENGTH) == 0) {
1693 err = sync_timestamps(ondisk_path, status, ie, &sb);
1698 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1702 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1703 int update_timestamps;
1704 struct got_blob_object *blob2 = NULL;
1705 char *label_orig = NULL;
1706 if (got_fileindex_entry_has_blob(ie)) {
1707 struct got_object_id id2;
1708 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1709 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1713 if (got_fileindex_entry_has_commit(ie)) {
1714 char id_str[SHA1_DIGEST_STRING_LENGTH];
1715 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1716 sizeof(id_str)) == NULL) {
1717 err = got_error_path(id_str,
1718 GOT_ERR_BAD_OBJ_ID_STR);
1721 if (asprintf(&label_orig, "%s: commit %s",
1722 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1723 err = got_error_from_errno("asprintf");
1727 err = merge_blob(&update_timestamps, worktree, blob2,
1728 ondisk_path, path, sb.st_mode, label_orig, blob,
1729 worktree->base_commit_id, repo,
1730 progress_cb, progress_arg);
1733 got_object_blob_close(blob2);
1737 * Do not update timestamps of files with local changes.
1738 * Otherwise, a future status walk would treat them as
1739 * unmodified files again.
1741 err = got_fileindex_entry_update(ie, ondisk_path,
1742 blob->id.sha1, worktree->base_commit_id->sha1,
1744 } else if (status == GOT_STATUS_MODE_CHANGE) {
1745 err = got_fileindex_entry_update(ie, ondisk_path,
1746 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1747 } else if (status == GOT_STATUS_DELETE) {
1748 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1751 err = got_fileindex_entry_update(ie, ondisk_path,
1752 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1756 err = install_blob(worktree, ondisk_path, path, te->mode,
1757 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0, 0,
1758 repo, progress_cb, progress_arg);
1762 err = got_fileindex_entry_update(ie, ondisk_path,
1763 blob->id.sha1, worktree->base_commit_id->sha1, 1);
1765 err = create_fileindex_entry(fileindex,
1766 worktree->base_commit_id, ondisk_path, path,
1772 got_object_blob_close(blob);
1778 static const struct got_error *
1779 remove_ondisk_file(const char *root_path, const char *path)
1781 const struct got_error *err = NULL;
1782 char *ondisk_path = NULL;
1784 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1785 return got_error_from_errno("asprintf");
1787 if (unlink(ondisk_path) == -1) {
1788 if (errno != ENOENT)
1789 err = got_error_from_errno2("unlink", ondisk_path);
1791 char *parent = dirname(ondisk_path);
1792 while (parent && strcmp(parent, root_path) != 0) {
1793 if (rmdir(parent) == -1) {
1794 if (errno != ENOTEMPTY)
1795 err = got_error_from_errno2("rmdir",
1799 parent = dirname(parent);
1806 static const struct got_error *
1807 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1808 struct got_fileindex_entry *ie, struct got_repository *repo,
1809 got_worktree_checkout_cb progress_cb, void *progress_arg)
1811 const struct got_error *err = NULL;
1812 unsigned char status;
1816 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1817 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1819 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1821 return got_error_from_errno("asprintf");
1823 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
1827 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1828 status == GOT_STATUS_ADD) {
1829 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1833 * Preserve the working file and change the deleted blob's
1834 * entry into a schedule-add entry.
1836 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1839 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1842 if (status == GOT_STATUS_NO_CHANGE) {
1843 err = remove_ondisk_file(worktree->root_path, ie->path);
1847 got_fileindex_entry_remove(fileindex, ie);
1854 struct diff_cb_arg {
1855 struct got_fileindex *fileindex;
1856 struct got_worktree *worktree;
1857 struct got_repository *repo;
1858 got_worktree_checkout_cb progress_cb;
1860 got_cancel_cb cancel_cb;
1864 static const struct got_error *
1865 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1866 struct got_tree_entry *te, const char *parent_path)
1868 struct diff_cb_arg *a = arg;
1870 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1871 return got_error(GOT_ERR_CANCELLED);
1873 return update_blob(a->worktree, a->fileindex, ie, te,
1874 ie->path, a->repo, a->progress_cb, a->progress_arg);
1877 static const struct got_error *
1878 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1880 struct diff_cb_arg *a = arg;
1882 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1883 return got_error(GOT_ERR_CANCELLED);
1885 return delete_blob(a->worktree, a->fileindex, ie,
1886 a->repo, a->progress_cb, a->progress_arg);
1889 static const struct got_error *
1890 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1892 struct diff_cb_arg *a = arg;
1893 const struct got_error *err;
1896 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1897 return got_error(GOT_ERR_CANCELLED);
1899 if (got_object_tree_entry_is_submodule(te))
1902 if (asprintf(&path, "%s%s%s", parent_path,
1903 parent_path[0] ? "/" : "", te->name)
1905 return got_error_from_errno("asprintf");
1907 if (S_ISDIR(te->mode))
1908 err = add_dir_on_disk(a->worktree, path);
1910 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1911 a->repo, a->progress_cb, a->progress_arg);
1917 static const struct got_error *
1918 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1920 const struct got_error *err = NULL;
1921 char *uuidstr = NULL;
1922 uint32_t uuid_status;
1926 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1927 if (uuid_status != uuid_s_ok)
1928 return got_error_uuid(uuid_status, "uuid_to_string");
1930 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1932 err = got_error_from_errno("asprintf");
1939 const struct got_error *
1940 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1942 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1945 static const struct got_error *
1946 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1948 return get_ref_name(refname, worktree,
1949 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1952 static const struct got_error *
1953 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1955 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1958 static const struct got_error *
1959 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1961 return get_ref_name(refname, worktree,
1962 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1965 static const struct got_error *
1966 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1968 return get_ref_name(refname, worktree,
1969 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1972 static const struct got_error *
1973 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1975 return get_ref_name(refname, worktree,
1976 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1979 static const struct got_error *
1980 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1982 return get_ref_name(refname, worktree,
1983 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1986 static const struct got_error *
1987 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1989 return get_ref_name(refname, worktree,
1990 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1993 static const struct got_error *
1994 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1996 return get_ref_name(refname, worktree,
1997 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2000 const struct got_error *
2001 got_worktree_get_histedit_script_path(char **path,
2002 struct got_worktree *worktree)
2004 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2005 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2007 return got_error_from_errno("asprintf");
2013 * Prevent Git's garbage collector from deleting our base commit by
2014 * setting a reference to our base commit's ID.
2016 static const struct got_error *
2017 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2019 const struct got_error *err = NULL;
2020 struct got_reference *ref = NULL;
2023 err = got_worktree_get_base_ref_name(&refname, worktree);
2027 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2031 err = got_ref_write(ref, repo);
2039 static const struct got_error *
2040 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2042 const struct got_error *err = NULL;
2044 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2045 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2046 err = got_error_from_errno("asprintf");
2047 *fileindex_path = NULL;
2053 static const struct got_error *
2054 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2055 struct got_worktree *worktree)
2057 const struct got_error *err = NULL;
2060 *fileindex_path = NULL;
2061 *fileindex = got_fileindex_alloc();
2062 if (*fileindex == NULL)
2063 return got_error_from_errno("got_fileindex_alloc");
2065 err = get_fileindex_path(fileindex_path, worktree);
2069 index = fopen(*fileindex_path, "rb");
2070 if (index == NULL) {
2071 if (errno != ENOENT)
2072 err = got_error_from_errno2("fopen", *fileindex_path);
2074 err = got_fileindex_read(*fileindex, index);
2075 if (fclose(index) != 0 && err == NULL)
2076 err = got_error_from_errno("fclose");
2080 free(*fileindex_path);
2081 *fileindex_path = NULL;
2082 got_fileindex_free(*fileindex);
2088 struct bump_base_commit_id_arg {
2089 struct got_object_id *base_commit_id;
2092 const char *entry_name;
2093 got_worktree_checkout_cb progress_cb;
2097 /* Bump base commit ID of all files within an updated part of the work tree. */
2098 static const struct got_error *
2099 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2101 const struct got_error *err;
2102 struct bump_base_commit_id_arg *a = arg;
2104 if (a->entry_name) {
2105 if (strcmp(ie->path, a->path) != 0)
2107 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2110 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2111 SHA1_DIGEST_LENGTH) == 0)
2114 if (a->progress_cb) {
2115 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2120 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2124 static const struct got_error *
2125 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2127 const struct got_error *err = NULL;
2128 char *new_fileindex_path = NULL;
2129 FILE *new_index = NULL;
2130 struct timespec timeout;
2132 err = got_opentemp_named(&new_fileindex_path, &new_index,
2137 err = got_fileindex_write(fileindex, new_index);
2141 if (rename(new_fileindex_path, fileindex_path) != 0) {
2142 err = got_error_from_errno3("rename", new_fileindex_path,
2144 unlink(new_fileindex_path);
2148 * Sleep for a short amount of time to ensure that files modified after
2149 * this program exits have a different time stamp from the one which
2150 * was recorded in the file index.
2153 timeout.tv_nsec = 1;
2154 nanosleep(&timeout, NULL);
2158 free(new_fileindex_path);
2162 static const struct got_error *
2163 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2164 struct got_object_id **tree_id, const char *wt_relpath,
2165 struct got_worktree *worktree, struct got_repository *repo)
2167 const struct got_error *err = NULL;
2168 struct got_object_id *id = NULL;
2169 char *in_repo_path = NULL;
2170 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2172 *entry_type = GOT_OBJ_TYPE_ANY;
2173 *tree_relpath = NULL;
2176 if (wt_relpath[0] == '\0') {
2177 /* Check out all files within the work tree. */
2178 *entry_type = GOT_OBJ_TYPE_TREE;
2179 *tree_relpath = strdup("");
2180 if (*tree_relpath == NULL) {
2181 err = got_error_from_errno("strdup");
2184 err = got_object_id_by_path(tree_id, repo,
2185 worktree->base_commit_id, worktree->path_prefix);
2191 /* Check out a subset of files in the work tree. */
2193 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2194 is_root_wt ? "" : "/", wt_relpath) == -1) {
2195 err = got_error_from_errno("asprintf");
2199 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2205 in_repo_path = NULL;
2207 err = got_object_get_type(entry_type, repo, id);
2211 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2212 /* Check out a single file. */
2213 if (strchr(wt_relpath, '/') == NULL) {
2214 /* Check out a single file in work tree's root dir. */
2215 in_repo_path = strdup(worktree->path_prefix);
2216 if (in_repo_path == NULL) {
2217 err = got_error_from_errno("strdup");
2220 *tree_relpath = strdup("");
2221 if (*tree_relpath == NULL) {
2222 err = got_error_from_errno("strdup");
2226 /* Check out a single file in a subdirectory. */
2227 err = got_path_dirname(tree_relpath, wt_relpath);
2230 if (asprintf(&in_repo_path, "%s%s%s",
2231 worktree->path_prefix, is_root_wt ? "" : "/",
2232 *tree_relpath) == -1) {
2233 err = got_error_from_errno("asprintf");
2237 err = got_object_id_by_path(tree_id, repo,
2238 worktree->base_commit_id, in_repo_path);
2240 /* Check out all files within a subdirectory. */
2241 *tree_id = got_object_id_dup(id);
2242 if (*tree_id == NULL) {
2243 err = got_error_from_errno("got_object_id_dup");
2246 *tree_relpath = strdup(wt_relpath);
2247 if (*tree_relpath == NULL) {
2248 err = got_error_from_errno("strdup");
2256 *entry_type = GOT_OBJ_TYPE_ANY;
2257 free(*tree_relpath);
2258 *tree_relpath = NULL;
2265 static const struct got_error *
2266 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2267 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2268 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2269 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2271 const struct got_error *err = NULL;
2272 struct got_commit_object *commit = NULL;
2273 struct got_tree_object *tree = NULL;
2274 struct got_fileindex_diff_tree_cb diff_cb;
2275 struct diff_cb_arg arg;
2277 err = ref_base_commit(worktree, repo);
2279 if (!(err->code == GOT_ERR_ERRNO &&
2280 (errno == EACCES || errno == EROFS)))
2282 err = (*progress_cb)(progress_arg,
2283 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2288 err = got_object_open_as_commit(&commit, repo,
2289 worktree->base_commit_id);
2293 err = got_object_open_as_tree(&tree, repo, tree_id);
2298 got_object_tree_find_entry(tree, entry_name) == NULL) {
2299 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2303 diff_cb.diff_old_new = diff_old_new;
2304 diff_cb.diff_old = diff_old;
2305 diff_cb.diff_new = diff_new;
2306 arg.fileindex = fileindex;
2307 arg.worktree = worktree;
2309 arg.progress_cb = progress_cb;
2310 arg.progress_arg = progress_arg;
2311 arg.cancel_cb = cancel_cb;
2312 arg.cancel_arg = cancel_arg;
2313 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2314 entry_name, repo, &diff_cb, &arg);
2317 got_object_tree_close(tree);
2319 got_object_commit_close(commit);
2323 const struct got_error *
2324 got_worktree_checkout_files(struct got_worktree *worktree,
2325 struct got_pathlist_head *paths, struct got_repository *repo,
2326 got_worktree_checkout_cb progress_cb, void *progress_arg,
2327 got_cancel_cb cancel_cb, void *cancel_arg)
2329 const struct got_error *err = NULL, *sync_err, *unlockerr;
2330 struct got_commit_object *commit = NULL;
2331 struct got_tree_object *tree = NULL;
2332 struct got_fileindex *fileindex = NULL;
2333 char *fileindex_path = NULL;
2334 struct got_pathlist_entry *pe;
2335 struct tree_path_data {
2336 SIMPLEQ_ENTRY(tree_path_data) entry;
2337 struct got_object_id *tree_id;
2342 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2344 SIMPLEQ_INIT(&tree_paths);
2346 err = lock_worktree(worktree, LOCK_EX);
2350 /* Map all specified paths to in-repository trees. */
2351 TAILQ_FOREACH(pe, paths, entry) {
2352 tpd = malloc(sizeof(*tpd));
2354 err = got_error_from_errno("malloc");
2358 err = find_tree_entry_for_checkout(&tpd->entry_type,
2359 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2365 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2366 err = got_path_basename(&tpd->entry_name, pe->path);
2374 tpd->entry_name = NULL;
2376 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2380 * Read the file index.
2381 * Checking out files is supposed to be an idempotent operation.
2382 * If the on-disk file index is incomplete we will try to complete it.
2384 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2388 tpd = SIMPLEQ_FIRST(&tree_paths);
2389 TAILQ_FOREACH(pe, paths, entry) {
2390 struct bump_base_commit_id_arg bbc_arg;
2392 err = checkout_files(worktree, fileindex, tpd->relpath,
2393 tpd->tree_id, tpd->entry_name, repo,
2394 progress_cb, progress_arg, cancel_cb, cancel_arg);
2398 bbc_arg.base_commit_id = worktree->base_commit_id;
2399 bbc_arg.entry_name = tpd->entry_name;
2400 bbc_arg.path = pe->path;
2401 bbc_arg.path_len = pe->path_len;
2402 bbc_arg.progress_cb = progress_cb;
2403 bbc_arg.progress_arg = progress_arg;
2404 err = got_fileindex_for_each_entry_safe(fileindex,
2405 bump_base_commit_id, &bbc_arg);
2409 tpd = SIMPLEQ_NEXT(tpd, entry);
2411 sync_err = sync_fileindex(fileindex, fileindex_path);
2412 if (sync_err && err == NULL)
2415 free(fileindex_path);
2417 got_object_tree_close(tree);
2419 got_object_commit_close(commit);
2421 got_fileindex_free(fileindex);
2422 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2423 tpd = SIMPLEQ_FIRST(&tree_paths);
2424 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2429 unlockerr = lock_worktree(worktree, LOCK_SH);
2430 if (unlockerr && err == NULL)
2435 struct merge_file_cb_arg {
2436 struct got_worktree *worktree;
2437 struct got_fileindex *fileindex;
2438 got_worktree_checkout_cb progress_cb;
2440 got_cancel_cb cancel_cb;
2442 const char *label_orig;
2443 struct got_object_id *commit_id2;
2446 static const struct got_error *
2447 merge_file_cb(void *arg, struct got_blob_object *blob1,
2448 struct got_blob_object *blob2, struct got_object_id *id1,
2449 struct got_object_id *id2, const char *path1, const char *path2,
2450 mode_t mode1, mode_t mode2, struct got_repository *repo)
2452 static const struct got_error *err = NULL;
2453 struct merge_file_cb_arg *a = arg;
2454 struct got_fileindex_entry *ie;
2455 char *ondisk_path = NULL;
2457 unsigned char status;
2458 int local_changes_subsumed;
2460 if (blob1 && blob2) {
2461 ie = got_fileindex_entry_get(a->fileindex, path2,
2464 return (*a->progress_cb)(a->progress_arg,
2465 GOT_STATUS_MISSING, path2);
2467 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2469 return got_error_from_errno("asprintf");
2471 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2476 if (status == GOT_STATUS_DELETE) {
2477 err = (*a->progress_cb)(a->progress_arg,
2478 GOT_STATUS_MERGE, path2);
2481 if (status != GOT_STATUS_NO_CHANGE &&
2482 status != GOT_STATUS_MODIFY &&
2483 status != GOT_STATUS_CONFLICT &&
2484 status != GOT_STATUS_ADD) {
2485 err = (*a->progress_cb)(a->progress_arg, status, path2);
2489 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2490 err = merge_symlink(a->worktree, blob1,
2491 ondisk_path, path2, sb.st_mode, a->label_orig,
2492 blob2, a->commit_id2, repo, a->progress_cb,
2495 err = merge_blob(&local_changes_subsumed, a->worktree,
2496 blob1, ondisk_path, path2, sb.st_mode,
2497 a->label_orig, blob2, a->commit_id2, repo,
2498 a->progress_cb, a->progress_arg);
2501 ie = got_fileindex_entry_get(a->fileindex, path1,
2504 return (*a->progress_cb)(a->progress_arg,
2505 GOT_STATUS_MISSING, path1);
2507 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2509 return got_error_from_errno("asprintf");
2511 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2517 case GOT_STATUS_NO_CHANGE:
2518 err = (*a->progress_cb)(a->progress_arg,
2519 GOT_STATUS_DELETE, path1);
2522 err = remove_ondisk_file(a->worktree->root_path, path1);
2526 got_fileindex_entry_mark_deleted_from_disk(ie);
2528 case GOT_STATUS_DELETE:
2529 case GOT_STATUS_MISSING:
2530 err = (*a->progress_cb)(a->progress_arg,
2531 GOT_STATUS_DELETE, path1);
2535 got_fileindex_entry_mark_deleted_from_disk(ie);
2537 case GOT_STATUS_ADD:
2538 case GOT_STATUS_MODIFY:
2539 case GOT_STATUS_CONFLICT:
2540 err = (*a->progress_cb)(a->progress_arg,
2541 GOT_STATUS_CANNOT_DELETE, path1);
2545 case GOT_STATUS_OBSTRUCTED:
2546 err = (*a->progress_cb)(a->progress_arg, status, path1);
2554 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2556 return got_error_from_errno("asprintf");
2557 ie = got_fileindex_entry_get(a->fileindex, path2,
2560 err = get_file_status(&status, &sb, ie, ondisk_path,
2564 if (status != GOT_STATUS_NO_CHANGE &&
2565 status != GOT_STATUS_MODIFY &&
2566 status != GOT_STATUS_CONFLICT &&
2567 status != GOT_STATUS_ADD) {
2568 err = (*a->progress_cb)(a->progress_arg,
2572 if (S_ISLNK(mode2)) {
2573 err = merge_symlink(a->worktree, NULL,
2574 ondisk_path, path2, sb.st_mode,
2575 a->label_orig, blob2, a->commit_id2,
2576 repo, a->progress_cb, a->progress_arg);
2580 err = merge_blob(&local_changes_subsumed,
2581 a->worktree, NULL, ondisk_path, path2,
2582 sb.st_mode, a->label_orig, blob2,
2583 a->commit_id2, repo, a->progress_cb,
2588 if (status == GOT_STATUS_DELETE) {
2589 err = got_fileindex_entry_update(ie,
2590 ondisk_path, blob2->id.sha1,
2591 a->worktree->base_commit_id->sha1, 0);
2596 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2597 err = install_blob(a->worktree, ondisk_path, path2,
2598 /* XXX get this from parent tree! */
2599 GOT_DEFAULT_FILE_MODE,
2600 sb.st_mode, blob2, 0, 0, 0, repo,
2601 a->progress_cb, a->progress_arg);
2604 err = got_fileindex_entry_alloc(&ie, path2);
2607 err = got_fileindex_entry_update(ie, ondisk_path,
2610 got_fileindex_entry_free(ie);
2613 err = got_fileindex_entry_add(a->fileindex, ie);
2615 got_fileindex_entry_free(ie);
2625 struct check_merge_ok_arg {
2626 struct got_worktree *worktree;
2627 struct got_repository *repo;
2630 static const struct got_error *
2631 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2633 const struct got_error *err = NULL;
2634 struct check_merge_ok_arg *a = arg;
2635 unsigned char status;
2639 /* Reject merges into a work tree with mixed base commits. */
2640 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2641 SHA1_DIGEST_LENGTH))
2642 return got_error(GOT_ERR_MIXED_COMMITS);
2644 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2646 return got_error_from_errno("asprintf");
2648 /* Reject merges into a work tree with conflicted files. */
2649 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2652 if (status == GOT_STATUS_CONFLICT)
2653 return got_error(GOT_ERR_CONFLICTS);
2658 static const struct got_error *
2659 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2660 const char *fileindex_path, struct got_object_id *commit_id1,
2661 struct got_object_id *commit_id2, struct got_repository *repo,
2662 got_worktree_checkout_cb progress_cb, void *progress_arg,
2663 got_cancel_cb cancel_cb, void *cancel_arg)
2665 const struct got_error *err = NULL, *sync_err;
2666 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2667 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2668 struct merge_file_cb_arg arg;
2669 char *label_orig = NULL;
2674 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2675 worktree->path_prefix);
2679 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2683 err = got_object_id_str(&id_str, commit_id1);
2687 if (asprintf(&label_orig, "%s: commit %s",
2688 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2689 err = got_error_from_errno("asprintf");
2696 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2697 worktree->path_prefix);
2701 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2705 arg.worktree = worktree;
2706 arg.fileindex = fileindex;
2707 arg.progress_cb = progress_cb;
2708 arg.progress_arg = progress_arg;
2709 arg.cancel_cb = cancel_cb;
2710 arg.cancel_arg = cancel_arg;
2711 arg.label_orig = label_orig;
2712 arg.commit_id2 = commit_id2;
2713 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2714 sync_err = sync_fileindex(fileindex, fileindex_path);
2715 if (sync_err && err == NULL)
2719 got_object_tree_close(tree1);
2721 got_object_tree_close(tree2);
2726 const struct got_error *
2727 got_worktree_merge_files(struct got_worktree *worktree,
2728 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2729 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2730 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2732 const struct got_error *err, *unlockerr;
2733 char *fileindex_path = NULL;
2734 struct got_fileindex *fileindex = NULL;
2735 struct check_merge_ok_arg mok_arg;
2737 err = lock_worktree(worktree, LOCK_EX);
2741 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2745 mok_arg.worktree = worktree;
2746 mok_arg.repo = repo;
2747 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2752 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2753 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2756 got_fileindex_free(fileindex);
2757 free(fileindex_path);
2758 unlockerr = lock_worktree(worktree, LOCK_SH);
2759 if (unlockerr && err == NULL)
2764 struct diff_dir_cb_arg {
2765 struct got_fileindex *fileindex;
2766 struct got_worktree *worktree;
2767 const char *status_path;
2768 size_t status_path_len;
2769 struct got_repository *repo;
2770 got_worktree_status_cb status_cb;
2772 got_cancel_cb cancel_cb;
2774 /* A pathlist containing per-directory pathlists of ignore patterns. */
2775 struct got_pathlist_head ignores;
2776 int report_unchanged;
2780 static const struct got_error *
2781 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2782 int dirfd, const char *de_name,
2783 got_worktree_status_cb status_cb, void *status_arg,
2784 struct got_repository *repo, int report_unchanged)
2786 const struct got_error *err = NULL;
2787 unsigned char status = GOT_STATUS_NO_CHANGE;
2788 unsigned char staged_status = get_staged_status(ie);
2790 struct got_object_id blob_id, commit_id, staged_blob_id;
2791 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2792 struct got_object_id *staged_blob_idp = NULL;
2794 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
2798 if (status == GOT_STATUS_NO_CHANGE &&
2799 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
2802 if (got_fileindex_entry_has_blob(ie)) {
2803 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2804 blob_idp = &blob_id;
2806 if (got_fileindex_entry_has_commit(ie)) {
2807 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2808 commit_idp = &commit_id;
2810 if (staged_status == GOT_STATUS_ADD ||
2811 staged_status == GOT_STATUS_MODIFY) {
2812 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2813 SHA1_DIGEST_LENGTH);
2814 staged_blob_idp = &staged_blob_id;
2817 return (*status_cb)(status_arg, status, staged_status,
2818 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
2821 static const struct got_error *
2822 status_old_new(void *arg, struct got_fileindex_entry *ie,
2823 struct dirent *de, const char *parent_path, int dirfd)
2825 const struct got_error *err = NULL;
2826 struct diff_dir_cb_arg *a = arg;
2829 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2830 return got_error(GOT_ERR_CANCELLED);
2832 if (got_path_cmp(parent_path, a->status_path,
2833 strlen(parent_path), a->status_path_len) != 0 &&
2834 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2837 if (parent_path[0]) {
2838 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2839 parent_path, de->d_name) == -1)
2840 return got_error_from_errno("asprintf");
2842 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2844 return got_error_from_errno("asprintf");
2847 err = report_file_status(ie, abspath, dirfd, de->d_name,
2848 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
2853 static const struct got_error *
2854 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2856 struct diff_dir_cb_arg *a = arg;
2857 struct got_object_id blob_id, commit_id;
2858 unsigned char status;
2860 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2861 return got_error(GOT_ERR_CANCELLED);
2863 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2866 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2867 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2868 if (got_fileindex_entry_has_file_on_disk(ie))
2869 status = GOT_STATUS_MISSING;
2871 status = GOT_STATUS_DELETE;
2872 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2873 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
2877 free_ignorelist(struct got_pathlist_head *ignorelist)
2879 struct got_pathlist_entry *pe;
2881 TAILQ_FOREACH(pe, ignorelist, entry)
2882 free((char *)pe->path);
2883 got_pathlist_free(ignorelist);
2887 free_ignores(struct got_pathlist_head *ignores)
2889 struct got_pathlist_entry *pe;
2891 TAILQ_FOREACH(pe, ignores, entry) {
2892 struct got_pathlist_head *ignorelist = pe->data;
2893 free_ignorelist(ignorelist);
2894 free((char *)pe->path);
2896 got_pathlist_free(ignores);
2899 static const struct got_error *
2900 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
2902 const struct got_error *err = NULL;
2903 struct got_pathlist_entry *pe = NULL;
2904 struct got_pathlist_head *ignorelist;
2905 char *line = NULL, *pattern, *dirpath = NULL;
2906 size_t linesize = 0;
2909 ignorelist = calloc(1, sizeof(*ignorelist));
2910 if (ignorelist == NULL)
2911 return got_error_from_errno("calloc");
2912 TAILQ_INIT(ignorelist);
2914 while ((linelen = getline(&line, &linesize, f)) != -1) {
2915 if (linelen > 0 && line[linelen - 1] == '\n')
2916 line[linelen - 1] = '\0';
2918 /* Git's ignores may contain comments. */
2922 /* Git's negated patterns are not (yet?) supported. */
2926 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
2928 err = got_error_from_errno("asprintf");
2931 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
2936 err = got_error_from_errno("getline");
2940 dirpath = strdup(path);
2941 if (dirpath == NULL) {
2942 err = got_error_from_errno("strdup");
2945 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
2948 if (err || pe == NULL) {
2950 free_ignorelist(ignorelist);
2956 match_ignores(struct got_pathlist_head *ignores, const char *path)
2958 struct got_pathlist_entry *pe;
2960 /* Handle patterns which match in all directories. */
2961 TAILQ_FOREACH(pe, ignores, entry) {
2962 struct got_pathlist_head *ignorelist = pe->data;
2963 struct got_pathlist_entry *pi;
2965 TAILQ_FOREACH(pi, ignorelist, entry) {
2966 const char *p, *pattern = pi->path;
2968 if (strncmp(pattern, "**/", 3) != 0)
2973 if (fnmatch(pattern, p,
2974 FNM_PATHNAME | FNM_LEADING_DIR)) {
2975 /* Retry in next directory. */
2976 while (*p && *p != '/')
2988 * The ignores pathlist contains ignore lists from children before
2989 * parents, so we can find the most specific ignorelist by walking
2990 * ignores backwards.
2992 pe = TAILQ_LAST(ignores, got_pathlist_head);
2994 if (got_path_is_child(path, pe->path, pe->path_len)) {
2995 struct got_pathlist_head *ignorelist = pe->data;
2996 struct got_pathlist_entry *pi;
2997 TAILQ_FOREACH(pi, ignorelist, entry) {
2998 const char *pattern = pi->path;
2999 int flags = FNM_LEADING_DIR;
3000 if (strstr(pattern, "/**/") == NULL)
3001 flags |= FNM_PATHNAME;
3002 if (fnmatch(pattern, path, flags))
3007 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3013 static const struct got_error *
3014 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3015 const char *path, int dirfd, const char *ignores_filename)
3017 const struct got_error *err = NULL;
3020 FILE *ignoresfile = NULL;
3022 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3023 path[0] ? "/" : "", ignores_filename) == -1)
3024 return got_error_from_errno("asprintf");
3027 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3029 if (errno != ENOENT && errno != EACCES)
3030 err = got_error_from_errno2("openat",
3033 ignoresfile = fdopen(fd, "r");
3034 if (ignoresfile == NULL)
3035 err = got_error_from_errno2("fdopen",
3039 err = read_ignores(ignores, path, ignoresfile);
3043 ignoresfile = fopen(ignorespath, "r");
3044 if (ignoresfile == NULL) {
3045 if (errno != ENOENT && errno != EACCES)
3046 err = got_error_from_errno2("fopen",
3049 err = read_ignores(ignores, path, ignoresfile);
3052 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3053 err = got_error_from_errno2("fclose", path);
3054 if (fd != -1 && close(fd) == -1 && err == NULL)
3055 err = got_error_from_errno2("close", path);
3060 static const struct got_error *
3061 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3063 const struct got_error *err = NULL;
3064 struct diff_dir_cb_arg *a = arg;
3067 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3068 return got_error(GOT_ERR_CANCELLED);
3070 if (parent_path[0]) {
3071 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3072 return got_error_from_errno("asprintf");
3077 if (de->d_type != DT_DIR &&
3078 got_path_is_child(path, a->status_path, a->status_path_len)
3079 && !match_ignores(&a->ignores, path))
3080 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3081 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3087 static const struct got_error *
3088 status_traverse(void *arg, const char *path, int dirfd)
3090 const struct got_error *err = NULL;
3091 struct diff_dir_cb_arg *a = arg;
3096 err = add_ignores(&a->ignores, a->worktree->root_path,
3097 path, dirfd, ".cvsignore");
3101 err = add_ignores(&a->ignores, a->worktree->root_path, path,
3102 dirfd, ".gitignore");
3107 static const struct got_error *
3108 report_single_file_status(const char *path, const char *ondisk_path,
3109 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3110 void *status_arg, struct got_repository *repo, int report_unchanged)
3112 struct got_fileindex_entry *ie;
3115 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3117 return report_file_status(ie, ondisk_path, -1, NULL,
3118 status_cb, status_arg, repo, report_unchanged);
3120 if (lstat(ondisk_path, &sb) == -1) {
3121 if (errno != ENOENT)
3122 return got_error_from_errno2("lstat", ondisk_path);
3123 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3124 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3128 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3129 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3130 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3135 static const struct got_error *
3136 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3137 const char *root_path, const char *path)
3139 const struct got_error *err;
3140 char *parent_path, *next_parent_path = NULL;
3142 err = add_ignores(ignores, root_path, "", -1,
3147 err = add_ignores(ignores, root_path, "", -1,
3152 err = got_path_dirname(&parent_path, path);
3154 if (err->code == GOT_ERR_BAD_PATH)
3155 return NULL; /* cannot traverse parent */
3159 err = add_ignores(ignores, root_path, parent_path, -1,
3163 err = add_ignores(ignores, root_path, parent_path, -1,
3167 err = got_path_dirname(&next_parent_path, parent_path);
3169 if (err->code == GOT_ERR_BAD_PATH)
3170 err = NULL; /* traversed everything */
3174 parent_path = next_parent_path;
3175 next_parent_path = NULL;
3179 free(next_parent_path);
3183 static const struct got_error *
3184 worktree_status(struct got_worktree *worktree, const char *path,
3185 struct got_fileindex *fileindex, struct got_repository *repo,
3186 got_worktree_status_cb status_cb, void *status_arg,
3187 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3188 int report_unchanged)
3190 const struct got_error *err = NULL;
3192 struct got_fileindex_diff_dir_cb fdiff_cb;
3193 struct diff_dir_cb_arg arg;
3194 char *ondisk_path = NULL;
3196 TAILQ_INIT(&arg.ignores);
3198 if (asprintf(&ondisk_path, "%s%s%s",
3199 worktree->root_path, path[0] ? "/" : "", path) == -1)
3200 return got_error_from_errno("asprintf");
3202 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3204 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3206 err = got_error_from_errno2("open", ondisk_path);
3208 err = report_single_file_status(path, ondisk_path,
3209 fileindex, status_cb, status_arg, repo,
3212 fdiff_cb.diff_old_new = status_old_new;
3213 fdiff_cb.diff_old = status_old;
3214 fdiff_cb.diff_new = status_new;
3215 fdiff_cb.diff_traverse = status_traverse;
3216 arg.fileindex = fileindex;
3217 arg.worktree = worktree;
3218 arg.status_path = path;
3219 arg.status_path_len = strlen(path);
3221 arg.status_cb = status_cb;
3222 arg.status_arg = status_arg;
3223 arg.cancel_cb = cancel_cb;
3224 arg.cancel_arg = cancel_arg;
3225 arg.report_unchanged = report_unchanged;
3226 arg.no_ignores = no_ignores;
3228 err = add_ignores_from_parent_paths(&arg.ignores,
3229 worktree->root_path, path);
3233 err = got_fileindex_diff_dir(fileindex, fd,
3234 worktree->root_path, path, repo, &fdiff_cb, &arg);
3237 free_ignores(&arg.ignores);
3238 if (fd != -1 && close(fd) != 0 && err == NULL)
3239 err = got_error_from_errno("close");
3244 const struct got_error *
3245 got_worktree_status(struct got_worktree *worktree,
3246 struct got_pathlist_head *paths, struct got_repository *repo,
3247 got_worktree_status_cb status_cb, void *status_arg,
3248 got_cancel_cb cancel_cb, void *cancel_arg)
3250 const struct got_error *err = NULL;
3251 char *fileindex_path = NULL;
3252 struct got_fileindex *fileindex = NULL;
3253 struct got_pathlist_entry *pe;
3255 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3259 TAILQ_FOREACH(pe, paths, entry) {
3260 err = worktree_status(worktree, pe->path, fileindex, repo,
3261 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3265 free(fileindex_path);
3266 got_fileindex_free(fileindex);
3270 const struct got_error *
3271 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3274 const struct got_error *err = NULL;
3275 char *resolved = NULL, *cwd = NULL, *path = NULL;
3281 cwd = getcwd(NULL, 0);
3283 return got_error_from_errno("getcwd");
3285 if (lstat(arg, &sb) == -1) {
3286 if (errno != ENOENT) {
3287 err = got_error_from_errno2("lstat", arg);
3291 if (S_ISLNK(sb.st_mode)) {
3293 * We cannot use realpath(3) with symlinks since we want to
3294 * operate on the symlink itself.
3295 * But we can make the path absolute, assuming it is relative
3296 * to the current working directory, and then canonicalize it.
3298 char *abspath = NULL;
3299 char canonpath[PATH_MAX];
3300 if (!got_path_is_absolute(arg)) {
3301 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3302 err = got_error_from_errno("asprintf");
3307 err = got_canonpath(abspath ? abspath : arg, canonpath,
3311 resolved = strdup(canonpath);
3312 if (resolved == NULL) {
3313 err = got_error_from_errno("strdup");
3317 resolved = realpath(arg, NULL);
3318 if (resolved == NULL) {
3319 if (errno != ENOENT) {
3320 err = got_error_from_errno2("realpath", arg);
3323 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
3324 err = got_error_from_errno("asprintf");
3330 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3331 strlen(got_worktree_get_root_path(worktree)))) {
3332 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3336 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3337 err = got_path_skip_common_ancestor(&path,
3338 got_worktree_get_root_path(worktree), resolved);
3344 err = got_error_from_errno("strdup");
3349 /* XXX status walk can't deal with trailing slash! */
3351 while (len > 0 && path[len - 1] == '/') {
3352 path[len - 1] = '\0';
3365 struct schedule_addition_args {
3366 struct got_worktree *worktree;
3367 struct got_fileindex *fileindex;
3368 got_worktree_checkout_cb progress_cb;
3370 struct got_repository *repo;
3373 static const struct got_error *
3374 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3375 const char *relpath, struct got_object_id *blob_id,
3376 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3377 int dirfd, const char *de_name)
3379 struct schedule_addition_args *a = arg;
3380 const struct got_error *err = NULL;
3381 struct got_fileindex_entry *ie;
3385 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3387 return got_error_from_errno("asprintf");
3389 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3391 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3395 /* Re-adding an existing entry is a no-op. */
3396 if (status == GOT_STATUS_ADD)
3398 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3403 if (status != GOT_STATUS_UNVERSIONED) {
3404 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3408 err = got_fileindex_entry_alloc(&ie, relpath);
3411 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3413 got_fileindex_entry_free(ie);
3416 err = got_fileindex_entry_add(a->fileindex, ie);
3418 got_fileindex_entry_free(ie);
3425 if (status == GOT_STATUS_ADD)
3427 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3430 const struct got_error *
3431 got_worktree_schedule_add(struct got_worktree *worktree,
3432 struct got_pathlist_head *paths,
3433 got_worktree_checkout_cb progress_cb, void *progress_arg,
3434 struct got_repository *repo, int no_ignores)
3436 struct got_fileindex *fileindex = NULL;
3437 char *fileindex_path = NULL;
3438 const struct got_error *err = NULL, *sync_err, *unlockerr;
3439 struct got_pathlist_entry *pe;
3440 struct schedule_addition_args saa;
3442 err = lock_worktree(worktree, LOCK_EX);
3446 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3450 saa.worktree = worktree;
3451 saa.fileindex = fileindex;
3452 saa.progress_cb = progress_cb;
3453 saa.progress_arg = progress_arg;
3456 TAILQ_FOREACH(pe, paths, entry) {
3457 err = worktree_status(worktree, pe->path, fileindex, repo,
3458 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3462 sync_err = sync_fileindex(fileindex, fileindex_path);
3463 if (sync_err && err == NULL)
3466 free(fileindex_path);
3468 got_fileindex_free(fileindex);
3469 unlockerr = lock_worktree(worktree, LOCK_SH);
3470 if (unlockerr && err == NULL)
3475 struct schedule_deletion_args {
3476 struct got_worktree *worktree;
3477 struct got_fileindex *fileindex;
3478 got_worktree_delete_cb progress_cb;
3480 struct got_repository *repo;
3481 int delete_local_mods;
3485 static const struct got_error *
3486 schedule_for_deletion(void *arg, unsigned char status,
3487 unsigned char staged_status, const char *relpath,
3488 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3489 struct got_object_id *commit_id, int dirfd, const char *de_name)
3491 struct schedule_deletion_args *a = arg;
3492 const struct got_error *err = NULL;
3493 struct got_fileindex_entry *ie = NULL;
3495 char *ondisk_path, *parent = NULL;
3497 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3499 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3501 staged_status = get_staged_status(ie);
3502 if (staged_status != GOT_STATUS_NO_CHANGE) {
3503 if (staged_status == GOT_STATUS_DELETE)
3505 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3508 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3510 return got_error_from_errno("asprintf");
3512 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3517 if (status != GOT_STATUS_NO_CHANGE) {
3518 if (status == GOT_STATUS_DELETE)
3520 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3521 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3524 if (status != GOT_STATUS_MODIFY &&
3525 status != GOT_STATUS_MISSING) {
3526 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3531 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3533 if (unlinkat(dirfd, de_name, 0) != 0) {
3534 err = got_error_from_errno2("unlinkat",
3538 } else if (unlink(ondisk_path) != 0) {
3539 err = got_error_from_errno2("unlink", ondisk_path);
3543 parent = dirname(ondisk_path);
3545 if (parent == NULL) {
3546 err = got_error_from_errno2("dirname", ondisk_path);
3549 while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3550 if (rmdir(parent) == -1) {
3551 if (errno != ENOTEMPTY)
3552 err = got_error_from_errno2("rmdir",
3556 parent = dirname(parent);
3557 if (parent == NULL) {
3558 err = got_error_from_errno2("dirname", parent);
3564 got_fileindex_entry_mark_deleted_from_disk(ie);
3569 if (status == GOT_STATUS_DELETE)
3571 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3572 staged_status, relpath);
3575 const struct got_error *
3576 got_worktree_schedule_delete(struct got_worktree *worktree,
3577 struct got_pathlist_head *paths, int delete_local_mods,
3578 got_worktree_delete_cb progress_cb, void *progress_arg,
3579 struct got_repository *repo, int keep_on_disk)
3581 struct got_fileindex *fileindex = NULL;
3582 char *fileindex_path = NULL;
3583 const struct got_error *err = NULL, *sync_err, *unlockerr;
3584 struct got_pathlist_entry *pe;
3585 struct schedule_deletion_args sda;
3587 err = lock_worktree(worktree, LOCK_EX);
3591 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3595 sda.worktree = worktree;
3596 sda.fileindex = fileindex;
3597 sda.progress_cb = progress_cb;
3598 sda.progress_arg = progress_arg;
3600 sda.delete_local_mods = delete_local_mods;
3601 sda.keep_on_disk = keep_on_disk;
3603 TAILQ_FOREACH(pe, paths, entry) {
3604 err = worktree_status(worktree, pe->path, fileindex, repo,
3605 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3609 sync_err = sync_fileindex(fileindex, fileindex_path);
3610 if (sync_err && err == NULL)
3613 free(fileindex_path);
3615 got_fileindex_free(fileindex);
3616 unlockerr = lock_worktree(worktree, LOCK_SH);
3617 if (unlockerr && err == NULL)
3622 static const struct got_error *
3623 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3625 const struct got_error *err = NULL;
3627 size_t linesize = 0, n;
3630 linelen = getline(&line, &linesize, infile);
3631 if (linelen == -1) {
3632 if (ferror(infile)) {
3633 err = got_error_from_errno("getline");
3639 n = fwrite(line, 1, linelen, outfile);
3641 err = got_ferror(outfile, GOT_ERR_IO);
3646 n = fwrite(line, 1, linelen, rejectfile);
3648 err = got_ferror(outfile, GOT_ERR_IO);
3655 static const struct got_error *
3656 skip_one_line(FILE *f)
3659 size_t linesize = 0;
3662 linelen = getline(&line, &linesize, f);
3663 if (linelen == -1) {
3665 return got_error_from_errno("getline");
3672 static const struct got_error *
3673 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3674 int start_old, int end_old, int start_new, int end_new,
3675 FILE *outfile, FILE *rejectfile)
3677 const struct got_error *err;
3679 /* Copy old file's lines leading up to patch. */
3680 while (!feof(f1) && *line_cur1 < start_old) {
3681 err = copy_one_line(f1, outfile, NULL);
3686 /* Skip new file's lines leading up to patch. */
3687 while (!feof(f2) && *line_cur2 < start_new) {
3689 err = copy_one_line(f2, NULL, rejectfile);
3691 err = skip_one_line(f2);
3696 /* Copy patched lines. */
3697 while (!feof(f2) && *line_cur2 <= end_new) {
3698 err = copy_one_line(f2, outfile, NULL);
3703 /* Skip over old file's replaced lines. */
3704 while (!feof(f1) && *line_cur1 <= end_old) {
3706 err = copy_one_line(f1, NULL, rejectfile);
3708 err = skip_one_line(f1);
3717 static const struct got_error *
3718 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3719 FILE *outfile, FILE *rejectfile)
3721 const struct got_error *err;
3724 /* Copy old file's lines until EOF. */
3726 err = copy_one_line(f1, outfile, NULL);
3733 /* Copy new file's lines until EOF. */
3735 err = copy_one_line(f2, NULL, rejectfile);
3745 static const struct got_error *
3746 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3747 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3748 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3749 int *line_cur2, FILE *outfile, FILE *rejectfile,
3750 got_worktree_patch_cb patch_cb, void *patch_arg)
3752 const struct got_error *err = NULL;
3753 int start_old = change->cv.a;
3754 int end_old = change->cv.b;
3755 int start_new = change->cv.c;
3756 int end_new = change->cv.d;
3760 *choice = GOT_PATCH_CHOICE_NONE;
3762 hunkfile = got_opentemp();
3763 if (hunkfile == NULL)
3764 return got_error_from_errno("got_opentemp");
3769 /* XXX TODO needs error checking */
3770 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
3772 if (fseek(f1, pos1, SEEK_SET) == -1) {
3773 err = got_ferror(f1, GOT_ERR_IO);
3776 if (fseek(f2, pos2, SEEK_SET) == -1) {
3777 err = got_ferror(f1, GOT_ERR_IO);
3780 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
3781 err = got_ferror(hunkfile, GOT_ERR_IO);
3785 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
3786 hunkfile, n, nchanges);
3791 case GOT_PATCH_CHOICE_YES:
3792 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3793 end_old, start_new, end_new, outfile, rejectfile);
3795 case GOT_PATCH_CHOICE_NO:
3796 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3797 end_old, start_new, end_new, rejectfile, outfile);
3799 case GOT_PATCH_CHOICE_QUIT:
3802 err = got_error(GOT_ERR_PATCH_CHOICE);
3806 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3807 err = got_error_from_errno("fclose");
3811 struct revert_file_args {
3812 struct got_worktree *worktree;
3813 struct got_fileindex *fileindex;
3814 got_worktree_checkout_cb progress_cb;
3816 got_worktree_patch_cb patch_cb;
3818 struct got_repository *repo;
3821 static const struct got_error *
3822 create_patched_content(char **path_outfile, int reverse_patch,
3823 struct got_object_id *blob_id, const char *path2,
3824 int dirfd2, const char *de_name2,
3825 const char *relpath, struct got_repository *repo,
3826 got_worktree_patch_cb patch_cb, void *patch_arg)
3828 const struct got_error *err;
3829 struct got_blob_object *blob = NULL;
3830 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3832 char *path1 = NULL, *id_str = NULL;
3833 struct stat sb1, sb2;
3834 struct got_diff_changes *changes = NULL;
3835 struct got_diff_state *ds = NULL;
3836 struct got_diff_args *args = NULL;
3837 struct got_diff_change *change;
3838 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3841 *path_outfile = NULL;
3843 err = got_object_id_str(&id_str, blob_id);
3848 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
3850 err = got_error_from_errno2("openat", path2);
3854 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
3856 err = got_error_from_errno2("open", path2);
3860 if (fstat(fd2, &sb2) == -1) {
3861 err = got_error_from_errno2("fstat", path2);
3865 f2 = fdopen(fd2, "r");
3867 err = got_error_from_errno2("fdopen", path2);
3872 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
3876 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
3880 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
3884 if (stat(path1, &sb1) == -1) {
3885 err = got_error_from_errno2("stat", path1);
3889 err = got_diff_files(&changes, &ds, &args, &diff_flags,
3890 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
3894 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
3898 if (fseek(f1, 0L, SEEK_SET) == -1)
3899 return got_ferror(f1, GOT_ERR_IO);
3900 if (fseek(f2, 0L, SEEK_SET) == -1)
3901 return got_ferror(f2, GOT_ERR_IO);
3902 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
3904 err = apply_or_reject_change(&choice, change, ++n,
3905 changes->nchanges, ds, args, diff_flags, relpath,
3906 f1, f2, &line_cur1, &line_cur2,
3907 reverse_patch ? NULL : outfile,
3908 reverse_patch ? outfile : NULL,
3909 patch_cb, patch_arg);
3912 if (choice == GOT_PATCH_CHOICE_YES)
3914 else if (choice == GOT_PATCH_CHOICE_QUIT)
3918 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
3919 reverse_patch ? NULL : outfile,
3920 reverse_patch ? outfile : NULL);
3924 if (chmod(*path_outfile, sb2.st_mode) == -1) {
3925 err = got_error_from_errno2("chmod", path2);
3932 got_object_blob_close(blob);
3933 if (f1 && fclose(f1) == EOF && err == NULL)
3934 err = got_error_from_errno2("fclose", path1);
3935 if (f2 && fclose(f2) == EOF && err == NULL)
3936 err = got_error_from_errno2("fclose", path2);
3937 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3938 err = got_error_from_errno2("close", path2);
3939 if (outfile && fclose(outfile) == EOF && err == NULL)
3940 err = got_error_from_errno2("fclose", *path_outfile);
3941 if (path1 && unlink(path1) == -1 && err == NULL)
3942 err = got_error_from_errno2("unlink", path1);
3943 if (err || !have_content) {
3944 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
3945 err = got_error_from_errno2("unlink", *path_outfile);
3946 free(*path_outfile);
3947 *path_outfile = NULL;
3951 got_diff_state_free(ds);
3955 got_diff_free_changes(changes);
3960 static const struct got_error *
3961 revert_file(void *arg, unsigned char status, unsigned char staged_status,
3962 const char *relpath, struct got_object_id *blob_id,
3963 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3964 int dirfd, const char *de_name)
3966 struct revert_file_args *a = arg;
3967 const struct got_error *err = NULL;
3968 char *parent_path = NULL;
3969 struct got_fileindex_entry *ie;
3970 struct got_tree_object *tree = NULL;
3971 struct got_object_id *tree_id = NULL;
3972 const struct got_tree_entry *te = NULL;
3973 char *tree_path = NULL, *te_name;
3974 char *ondisk_path = NULL, *path_content = NULL;
3975 struct got_blob_object *blob = NULL;
3977 /* Reverting a staged deletion is a no-op. */
3978 if (status == GOT_STATUS_DELETE &&
3979 staged_status != GOT_STATUS_NO_CHANGE)
3982 if (status == GOT_STATUS_UNVERSIONED)
3983 return (*a->progress_cb)(a->progress_arg,
3984 GOT_STATUS_UNVERSIONED, relpath);
3986 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3988 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3990 /* Construct in-repository path of tree which contains this blob. */
3991 err = got_path_dirname(&parent_path, ie->path);
3993 if (err->code != GOT_ERR_BAD_PATH)
3995 parent_path = strdup("/");
3996 if (parent_path == NULL) {
3997 err = got_error_from_errno("strdup");
4001 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4002 tree_path = strdup(parent_path);
4003 if (tree_path == NULL) {
4004 err = got_error_from_errno("strdup");
4008 if (got_path_is_root_dir(parent_path)) {
4009 tree_path = strdup(a->worktree->path_prefix);
4010 if (tree_path == NULL) {
4011 err = got_error_from_errno("strdup");
4015 if (asprintf(&tree_path, "%s/%s",
4016 a->worktree->path_prefix, parent_path) == -1) {
4017 err = got_error_from_errno("asprintf");
4023 err = got_object_id_by_path(&tree_id, a->repo,
4024 a->worktree->base_commit_id, tree_path);
4026 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4027 (status == GOT_STATUS_ADD ||
4028 staged_status == GOT_STATUS_ADD)))
4031 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4035 te_name = basename(ie->path);
4036 if (te_name == NULL) {
4037 err = got_error_from_errno2("basename", ie->path);
4041 te = got_object_tree_find_entry(tree, te_name);
4042 if (te == NULL && status != GOT_STATUS_ADD &&
4043 staged_status != GOT_STATUS_ADD) {
4044 err = got_error(GOT_ERR_NO_TREE_ENTRY);
4050 case GOT_STATUS_ADD:
4052 int choice = GOT_PATCH_CHOICE_NONE;
4053 err = (*a->patch_cb)(&choice, a->patch_arg,
4054 status, ie->path, NULL, 1, 1);
4057 if (choice != GOT_PATCH_CHOICE_YES)
4060 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4064 got_fileindex_entry_remove(a->fileindex, ie);
4066 case GOT_STATUS_DELETE:
4068 int choice = GOT_PATCH_CHOICE_NONE;
4069 err = (*a->patch_cb)(&choice, a->patch_arg,
4070 status, ie->path, NULL, 1, 1);
4073 if (choice != GOT_PATCH_CHOICE_YES)
4077 case GOT_STATUS_MODIFY:
4078 case GOT_STATUS_MODE_CHANGE:
4079 case GOT_STATUS_CONFLICT:
4080 case GOT_STATUS_MISSING: {
4081 struct got_object_id id;
4082 if (staged_status == GOT_STATUS_ADD ||
4083 staged_status == GOT_STATUS_MODIFY) {
4084 memcpy(id.sha1, ie->staged_blob_sha1,
4085 SHA1_DIGEST_LENGTH);
4087 memcpy(id.sha1, ie->blob_sha1,
4088 SHA1_DIGEST_LENGTH);
4089 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4093 if (asprintf(&ondisk_path, "%s/%s",
4094 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4095 err = got_error_from_errno("asprintf");
4099 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4100 status == GOT_STATUS_CONFLICT)) {
4101 err = create_patched_content(&path_content, 1, &id,
4102 ondisk_path, dirfd, de_name, ie->path, a->repo,
4103 a->patch_cb, a->patch_arg);
4104 if (err || path_content == NULL)
4106 if (rename(path_content, ondisk_path) == -1) {
4107 err = got_error_from_errno3("rename",
4108 path_content, ondisk_path);
4112 err = install_blob(a->worktree, ondisk_path, ie->path,
4113 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4114 got_fileindex_perms_to_st(ie), blob, 0, 1, 0,
4115 a->repo, a->progress_cb, a->progress_arg);
4118 if (status == GOT_STATUS_DELETE ||
4119 status == GOT_STATUS_MODE_CHANGE) {
4120 err = got_fileindex_entry_update(ie,
4121 ondisk_path, blob->id.sha1,
4122 a->worktree->base_commit_id->sha1, 1);
4138 got_object_blob_close(blob);
4140 got_object_tree_close(tree);
4145 const struct got_error *
4146 got_worktree_revert(struct got_worktree *worktree,
4147 struct got_pathlist_head *paths,
4148 got_worktree_checkout_cb progress_cb, void *progress_arg,
4149 got_worktree_patch_cb patch_cb, void *patch_arg,
4150 struct got_repository *repo)
4152 struct got_fileindex *fileindex = NULL;
4153 char *fileindex_path = NULL;
4154 const struct got_error *err = NULL, *unlockerr = NULL;
4155 const struct got_error *sync_err = NULL;
4156 struct got_pathlist_entry *pe;
4157 struct revert_file_args rfa;
4159 err = lock_worktree(worktree, LOCK_EX);
4163 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4167 rfa.worktree = worktree;
4168 rfa.fileindex = fileindex;
4169 rfa.progress_cb = progress_cb;
4170 rfa.progress_arg = progress_arg;
4171 rfa.patch_cb = patch_cb;
4172 rfa.patch_arg = patch_arg;
4174 TAILQ_FOREACH(pe, paths, entry) {
4175 err = worktree_status(worktree, pe->path, fileindex, repo,
4176 revert_file, &rfa, NULL, NULL, 0, 0);
4180 sync_err = sync_fileindex(fileindex, fileindex_path);
4181 if (sync_err && err == NULL)
4184 free(fileindex_path);
4186 got_fileindex_free(fileindex);
4187 unlockerr = lock_worktree(worktree, LOCK_SH);
4188 if (unlockerr && err == NULL)
4194 free_commitable(struct got_commitable *ct)
4197 free(ct->in_repo_path);
4198 free(ct->ondisk_path);
4200 free(ct->base_blob_id);
4201 free(ct->staged_blob_id);
4202 free(ct->base_commit_id);
4206 struct collect_commitables_arg {
4207 struct got_pathlist_head *commitable_paths;
4208 struct got_repository *repo;
4209 struct got_worktree *worktree;
4210 int have_staged_files;
4213 static const struct got_error *
4214 collect_commitables(void *arg, unsigned char status,
4215 unsigned char staged_status, const char *relpath,
4216 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4217 struct got_object_id *commit_id, int dirfd, const char *de_name)
4219 struct collect_commitables_arg *a = arg;
4220 const struct got_error *err = NULL;
4221 struct got_commitable *ct = NULL;
4222 struct got_pathlist_entry *new = NULL;
4223 char *parent_path = NULL, *path = NULL;
4226 if (a->have_staged_files) {
4227 if (staged_status != GOT_STATUS_MODIFY &&
4228 staged_status != GOT_STATUS_ADD &&
4229 staged_status != GOT_STATUS_DELETE)
4232 if (status == GOT_STATUS_CONFLICT)
4233 return got_error(GOT_ERR_COMMIT_CONFLICT);
4235 if (status != GOT_STATUS_MODIFY &&
4236 status != GOT_STATUS_MODE_CHANGE &&
4237 status != GOT_STATUS_ADD &&
4238 status != GOT_STATUS_DELETE)
4242 if (asprintf(&path, "/%s", relpath) == -1) {
4243 err = got_error_from_errno("asprintf");
4246 if (strcmp(path, "/") == 0) {
4247 parent_path = strdup("");
4248 if (parent_path == NULL)
4249 return got_error_from_errno("strdup");
4251 err = got_path_dirname(&parent_path, path);
4256 ct = calloc(1, sizeof(*ct));
4258 err = got_error_from_errno("calloc");
4262 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4264 err = got_error_from_errno("asprintf");
4267 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
4268 sb.st_mode = GOT_DEFAULT_FILE_MODE;
4271 if (fstatat(dirfd, de_name, &sb,
4272 AT_SYMLINK_NOFOLLOW) == -1) {
4273 err = got_error_from_errno2("fstatat",
4277 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4278 err = got_error_from_errno2("lstat", ct->ondisk_path);
4281 ct->mode = sb.st_mode;
4284 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4285 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4287 err = got_error_from_errno("asprintf");
4291 ct->status = status;
4292 ct->staged_status = staged_status;
4293 ct->blob_id = NULL; /* will be filled in when blob gets created */
4294 if (ct->status != GOT_STATUS_ADD &&
4295 ct->staged_status != GOT_STATUS_ADD) {
4296 ct->base_blob_id = got_object_id_dup(blob_id);
4297 if (ct->base_blob_id == NULL) {
4298 err = got_error_from_errno("got_object_id_dup");
4301 ct->base_commit_id = got_object_id_dup(commit_id);
4302 if (ct->base_commit_id == NULL) {
4303 err = got_error_from_errno("got_object_id_dup");
4307 if (ct->staged_status == GOT_STATUS_ADD ||
4308 ct->staged_status == GOT_STATUS_MODIFY) {
4309 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4310 if (ct->staged_blob_id == NULL) {
4311 err = got_error_from_errno("got_object_id_dup");
4315 ct->path = strdup(path);
4316 if (ct->path == NULL) {
4317 err = got_error_from_errno("strdup");
4320 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4322 if (ct && (err || new == NULL))
4323 free_commitable(ct);
4329 static const struct got_error *write_tree(struct got_object_id **, int *,
4330 struct got_tree_object *, const char *, struct got_pathlist_head *,
4331 got_worktree_status_cb status_cb, void *status_arg,
4332 struct got_repository *);
4334 static const struct got_error *
4335 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4336 struct got_tree_entry *te, const char *parent_path,
4337 struct got_pathlist_head *commitable_paths,
4338 got_worktree_status_cb status_cb, void *status_arg,
4339 struct got_repository *repo)
4341 const struct got_error *err = NULL;
4342 struct got_tree_object *subtree;
4345 if (asprintf(&subpath, "%s%s%s", parent_path,
4346 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4347 return got_error_from_errno("asprintf");
4349 err = got_object_open_as_tree(&subtree, repo, &te->id);
4353 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4354 commitable_paths, status_cb, status_arg, repo);
4355 got_object_tree_close(subtree);
4360 static const struct got_error *
4361 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4363 const struct got_error *err = NULL;
4364 char *ct_parent_path = NULL;
4368 if (strchr(ct->in_repo_path, '/') == NULL) {
4369 *match = got_path_is_root_dir(path);
4373 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4376 *match = (strcmp(path, ct_parent_path) == 0);
4377 free(ct_parent_path);
4382 get_ct_file_mode(struct got_commitable *ct)
4384 if (S_ISLNK(ct->mode))
4387 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4390 static const struct got_error *
4391 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4392 struct got_tree_entry *te, struct got_commitable *ct)
4394 const struct got_error *err = NULL;
4398 err = got_object_tree_entry_dup(new_te, te);
4402 (*new_te)->mode = get_ct_file_mode(ct);
4404 if (ct->staged_status == GOT_STATUS_MODIFY)
4405 memcpy(&(*new_te)->id, ct->staged_blob_id,
4406 sizeof((*new_te)->id));
4408 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4410 if (err && *new_te) {
4417 static const struct got_error *
4418 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4419 struct got_commitable *ct)
4421 const struct got_error *err = NULL;
4426 *new_te = calloc(1, sizeof(**new_te));
4427 if (*new_te == NULL)
4428 return got_error_from_errno("calloc");
4430 ct_name = basename(ct->path);
4431 if (ct_name == NULL) {
4432 err = got_error_from_errno2("basename", ct->path);
4435 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4436 sizeof((*new_te)->name)) {
4437 err = got_error(GOT_ERR_NO_SPACE);
4441 (*new_te)->mode = get_ct_file_mode(ct);
4443 if (ct->staged_status == GOT_STATUS_ADD)
4444 memcpy(&(*new_te)->id, ct->staged_blob_id,
4445 sizeof((*new_te)->id));
4447 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4449 if (err && *new_te) {
4456 static const struct got_error *
4457 insert_tree_entry(struct got_tree_entry *new_te,
4458 struct got_pathlist_head *paths)
4460 const struct got_error *err = NULL;
4461 struct got_pathlist_entry *new_pe;
4463 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4467 return got_error(GOT_ERR_TREE_DUP_ENTRY);
4471 static const struct got_error *
4472 report_ct_status(struct got_commitable *ct,
4473 got_worktree_status_cb status_cb, void *status_arg)
4475 const char *ct_path = ct->path;
4476 unsigned char status;
4478 while (ct_path[0] == '/')
4481 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4482 status = ct->staged_status;
4484 status = ct->status;
4486 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4487 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4490 static const struct got_error *
4491 match_modified_subtree(int *modified, struct got_tree_entry *te,
4492 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4494 const struct got_error *err = NULL;
4495 struct got_pathlist_entry *pe;
4500 if (asprintf(&te_path, "%s%s%s", base_tree_path,
4501 got_path_is_root_dir(base_tree_path) ? "" : "/",
4503 return got_error_from_errno("asprintf");
4505 TAILQ_FOREACH(pe, commitable_paths, entry) {
4506 struct got_commitable *ct = pe->data;
4507 *modified = got_path_is_child(ct->in_repo_path, te_path,
4517 static const struct got_error *
4518 match_deleted_or_modified_ct(struct got_commitable **ctp,
4519 struct got_tree_entry *te, const char *base_tree_path,
4520 struct got_pathlist_head *commitable_paths)
4522 const struct got_error *err = NULL;
4523 struct got_pathlist_entry *pe;
4527 TAILQ_FOREACH(pe, commitable_paths, entry) {
4528 struct got_commitable *ct = pe->data;
4529 char *ct_name = NULL;
4532 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4533 if (ct->status != GOT_STATUS_MODIFY &&
4534 ct->status != GOT_STATUS_MODE_CHANGE &&
4535 ct->status != GOT_STATUS_DELETE)
4538 if (ct->staged_status != GOT_STATUS_MODIFY &&
4539 ct->staged_status != GOT_STATUS_DELETE)
4543 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4546 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4552 ct_name = basename(pe->path);
4553 if (ct_name == NULL)
4554 return got_error_from_errno2("basename", pe->path);
4556 if (strcmp(te->name, ct_name) != 0)
4566 static const struct got_error *
4567 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4568 const char *child_path, const char *path_base_tree,
4569 struct got_pathlist_head *commitable_paths,
4570 got_worktree_status_cb status_cb, void *status_arg,
4571 struct got_repository *repo)
4573 const struct got_error *err = NULL;
4574 struct got_tree_entry *new_te;
4576 struct got_object_id *id = NULL;
4581 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4582 got_path_is_root_dir(path_base_tree) ? "" : "/",
4584 return got_error_from_errno("asprintf");
4586 new_te = calloc(1, sizeof(*new_te));
4588 return got_error_from_errno("calloc");
4589 new_te->mode = S_IFDIR;
4591 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4592 sizeof(new_te->name)) {
4593 err = got_error(GOT_ERR_NO_SPACE);
4596 err = write_tree(&id, &nentries, NULL, subtree_path,
4597 commitable_paths, status_cb, status_arg, repo);
4602 memcpy(&new_te->id, id, sizeof(new_te->id));
4611 static const struct got_error *
4612 write_tree(struct got_object_id **new_tree_id, int *nentries,
4613 struct got_tree_object *base_tree, const char *path_base_tree,
4614 struct got_pathlist_head *commitable_paths,
4615 got_worktree_status_cb status_cb, void *status_arg,
4616 struct got_repository *repo)
4618 const struct got_error *err = NULL;
4619 struct got_pathlist_head paths;
4620 struct got_tree_entry *te, *new_te = NULL;
4621 struct got_pathlist_entry *pe;
4626 /* Insert, and recurse into, newly added entries first. */
4627 TAILQ_FOREACH(pe, commitable_paths, entry) {
4628 struct got_commitable *ct = pe->data;
4629 char *child_path = NULL, *slash;
4631 if ((ct->status != GOT_STATUS_ADD &&
4632 ct->staged_status != GOT_STATUS_ADD) ||
4633 (ct->flags & GOT_COMMITABLE_ADDED))
4636 if (!got_path_is_child(pe->path, path_base_tree,
4637 strlen(path_base_tree)))
4640 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
4645 slash = strchr(child_path, '/');
4646 if (slash == NULL) {
4647 err = alloc_added_blob_tree_entry(&new_te, ct);
4650 err = report_ct_status(ct, status_cb, status_arg);
4653 ct->flags |= GOT_COMMITABLE_ADDED;
4654 err = insert_tree_entry(new_te, &paths);
4659 *slash = '\0'; /* trim trailing path components */
4660 if (base_tree == NULL ||
4661 got_object_tree_find_entry(base_tree, child_path)
4663 err = make_subtree_for_added_blob(&new_te,
4664 child_path, path_base_tree,
4665 commitable_paths, status_cb, status_arg,
4669 err = insert_tree_entry(new_te, &paths);
4678 int i, nbase_entries;
4679 /* Handle modified and deleted entries. */
4680 nbase_entries = got_object_tree_get_nentries(base_tree);
4681 for (i = 0; i < nbase_entries; i++) {
4682 struct got_commitable *ct = NULL;
4684 te = got_object_tree_get_entry(base_tree, i);
4685 if (got_object_tree_entry_is_submodule(te)) {
4686 /* Entry is a submodule; just copy it. */
4687 err = got_object_tree_entry_dup(&new_te, te);
4690 err = insert_tree_entry(new_te, &paths);
4697 if (S_ISDIR(te->mode)) {
4699 err = got_object_tree_entry_dup(&new_te, te);
4702 err = match_modified_subtree(&modified, te,
4703 path_base_tree, commitable_paths);
4706 /* Avoid recursion into unmodified subtrees. */
4708 struct got_object_id *new_id;
4710 err = write_subtree(&new_id,
4712 path_base_tree, commitable_paths,
4713 status_cb, status_arg, repo);
4716 if (nsubentries == 0) {
4717 /* All entries were deleted. */
4721 memcpy(&new_te->id, new_id,
4722 sizeof(new_te->id));
4725 err = insert_tree_entry(new_te, &paths);
4732 err = match_deleted_or_modified_ct(&ct, te,
4733 path_base_tree, commitable_paths);
4737 /* NB: Deleted entries get dropped here. */
4738 if (ct->status == GOT_STATUS_MODIFY ||
4739 ct->status == GOT_STATUS_MODE_CHANGE ||
4740 ct->staged_status == GOT_STATUS_MODIFY) {
4741 err = alloc_modified_blob_tree_entry(
4745 err = insert_tree_entry(new_te, &paths);
4750 err = report_ct_status(ct, status_cb,
4755 /* Entry is unchanged; just copy it. */
4756 err = got_object_tree_entry_dup(&new_te, te);
4759 err = insert_tree_entry(new_te, &paths);
4767 /* Write new list of entries; deleted entries have been dropped. */
4768 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
4770 got_pathlist_free(&paths);
4774 static const struct got_error *
4775 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
4776 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
4777 int have_staged_files)
4779 const struct got_error *err = NULL;
4780 struct got_pathlist_entry *pe;
4782 TAILQ_FOREACH(pe, commitable_paths, entry) {
4783 struct got_fileindex_entry *ie;
4784 struct got_commitable *ct = pe->data;
4786 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4788 if (ct->status == GOT_STATUS_DELETE ||
4789 ct->staged_status == GOT_STATUS_DELETE) {
4790 got_fileindex_entry_remove(fileindex, ie);
4791 } else if (ct->staged_status == GOT_STATUS_ADD ||
4792 ct->staged_status == GOT_STATUS_MODIFY) {
4793 got_fileindex_entry_stage_set(ie,
4794 GOT_FILEIDX_STAGE_NONE);
4795 err = got_fileindex_entry_update(ie,
4796 ct->ondisk_path, ct->staged_blob_id->sha1,
4797 new_base_commit_id->sha1,
4798 !have_staged_files);
4800 err = got_fileindex_entry_update(ie,
4801 ct->ondisk_path, ct->blob_id->sha1,
4802 new_base_commit_id->sha1,
4803 !have_staged_files);
4805 err = got_fileindex_entry_alloc(&ie, pe->path);
4808 err = got_fileindex_entry_update(ie, ct->ondisk_path,
4809 ct->blob_id->sha1, new_base_commit_id->sha1, 1);
4811 got_fileindex_entry_free(ie);
4814 err = got_fileindex_entry_add(fileindex, ie);
4816 got_fileindex_entry_free(ie);
4825 static const struct got_error *
4826 check_out_of_date(const char *in_repo_path, unsigned char status,
4827 unsigned char staged_status, struct got_object_id *base_blob_id,
4828 struct got_object_id *base_commit_id,
4829 struct got_object_id *head_commit_id, struct got_repository *repo,
4832 const struct got_error *err = NULL;
4833 struct got_object_id *id = NULL;
4835 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
4836 /* Trivial case: base commit == head commit */
4837 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
4840 * Ensure file content which local changes were based
4841 * on matches file content in the branch head.
4843 err = got_object_id_by_path(&id, repo, head_commit_id,
4846 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4847 err = got_error(ood_errcode);
4849 } else if (got_object_id_cmp(id, base_blob_id) != 0)
4850 err = got_error(ood_errcode);
4852 /* Require that added files don't exist in the branch head. */
4853 err = got_object_id_by_path(&id, repo, head_commit_id,
4855 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
4857 err = id ? got_error(ood_errcode) : NULL;
4864 const struct got_error *
4865 commit_worktree(struct got_object_id **new_commit_id,
4866 struct got_pathlist_head *commitable_paths,
4867 struct got_object_id *head_commit_id, struct got_worktree *worktree,
4868 const char *author, const char *committer,
4869 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4870 got_worktree_status_cb status_cb, void *status_arg,
4871 struct got_repository *repo)
4873 const struct got_error *err = NULL, *unlockerr = NULL;
4874 struct got_pathlist_entry *pe;
4875 const char *head_ref_name = NULL;
4876 struct got_commit_object *head_commit = NULL;
4877 struct got_reference *head_ref2 = NULL;
4878 struct got_object_id *head_commit_id2 = NULL;
4879 struct got_tree_object *head_tree = NULL;
4880 struct got_object_id *new_tree_id = NULL;
4882 struct got_object_id_queue parent_ids;
4883 struct got_object_qid *pid = NULL;
4884 char *logmsg = NULL;
4886 *new_commit_id = NULL;
4888 SIMPLEQ_INIT(&parent_ids);
4890 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
4894 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
4898 if (commit_msg_cb != NULL) {
4899 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
4904 if (logmsg == NULL || strlen(logmsg) == 0) {
4905 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
4909 /* Create blobs from added and modified files and record their IDs. */
4910 TAILQ_FOREACH(pe, commitable_paths, entry) {
4911 struct got_commitable *ct = pe->data;
4914 /* Blobs for staged files already exist. */
4915 if (ct->staged_status == GOT_STATUS_ADD ||
4916 ct->staged_status == GOT_STATUS_MODIFY)
4919 if (ct->status != GOT_STATUS_ADD &&
4920 ct->status != GOT_STATUS_MODIFY &&
4921 ct->status != GOT_STATUS_MODE_CHANGE)
4924 if (asprintf(&ondisk_path, "%s/%s",
4925 worktree->root_path, pe->path) == -1) {
4926 err = got_error_from_errno("asprintf");
4929 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
4936 * When comitting a symlink we convert "bad" symlinks (those
4937 * which point outside the work tree or into .got) to regular
4938 * files. This way, the post-commit work tree state matches
4939 * a fresh checkout of the tree which was committed.
4941 if (S_ISLNK(get_ct_file_mode(ct))) {
4942 struct got_blob_object *blob;
4943 err = got_object_open_as_blob(&blob, repo, ct->blob_id,
4949 err = install_symlink(worktree, ondisk_path, ct->path,
4950 get_ct_file_mode(ct), GOT_DEFAULT_FILE_MODE, blob,
4951 0, 0, repo, NULL, NULL);
4952 got_object_blob_close(blob);
4961 /* Recursively write new tree objects. */
4962 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
4963 commitable_paths, status_cb, status_arg, repo);
4967 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
4970 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
4971 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
4972 1, author, time(NULL), committer, time(NULL), logmsg, repo);
4973 got_object_qid_free(pid);
4979 /* Check if a concurrent commit to our branch has occurred. */
4980 head_ref_name = got_worktree_get_head_ref_name(worktree);
4981 if (head_ref_name == NULL) {
4982 err = got_error_from_errno("got_worktree_get_head_ref_name");
4985 /* Lock the reference here to prevent concurrent modification. */
4986 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
4989 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
4992 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
4993 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
4996 /* Update branch head in repository. */
4997 err = got_ref_change_ref(head_ref2, *new_commit_id);
5000 err = got_ref_write(head_ref2, repo);
5004 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5008 err = ref_base_commit(worktree, repo);
5013 got_object_tree_close(head_tree);
5015 got_object_commit_close(head_commit);
5016 free(head_commit_id2);
5018 unlockerr = got_ref_unlock(head_ref2);
5019 if (unlockerr && err == NULL)
5021 got_ref_close(head_ref2);
5026 static const struct got_error *
5027 check_path_is_commitable(const char *path,
5028 struct got_pathlist_head *commitable_paths)
5030 struct got_pathlist_entry *cpe = NULL;
5031 size_t path_len = strlen(path);
5033 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5034 struct got_commitable *ct = cpe->data;
5035 const char *ct_path = ct->path;
5037 while (ct_path[0] == '/')
5040 if (strcmp(path, ct_path) == 0 ||
5041 got_path_is_child(ct_path, path, path_len))
5046 return got_error_path(path, GOT_ERR_BAD_PATH);
5051 static const struct got_error *
5052 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5054 int *have_staged_files = arg;
5056 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5057 *have_staged_files = 1;
5058 return got_error(GOT_ERR_CANCELLED);
5064 static const struct got_error *
5065 check_non_staged_files(struct got_fileindex *fileindex,
5066 struct got_pathlist_head *paths)
5068 struct got_pathlist_entry *pe;
5069 struct got_fileindex_entry *ie;
5071 TAILQ_FOREACH(pe, paths, entry) {
5072 if (pe->path[0] == '\0')
5074 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5076 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5077 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5078 return got_error_path(pe->path,
5079 GOT_ERR_FILE_NOT_STAGED);
5085 const struct got_error *
5086 got_worktree_commit(struct got_object_id **new_commit_id,
5087 struct got_worktree *worktree, struct got_pathlist_head *paths,
5088 const char *author, const char *committer,
5089 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5090 got_worktree_status_cb status_cb, void *status_arg,
5091 struct got_repository *repo)
5093 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5094 struct got_fileindex *fileindex = NULL;
5095 char *fileindex_path = NULL;
5096 struct got_pathlist_head commitable_paths;
5097 struct collect_commitables_arg cc_arg;
5098 struct got_pathlist_entry *pe;
5099 struct got_reference *head_ref = NULL;
5100 struct got_object_id *head_commit_id = NULL;
5101 int have_staged_files = 0;
5103 *new_commit_id = NULL;
5105 TAILQ_INIT(&commitable_paths);
5107 err = lock_worktree(worktree, LOCK_EX);
5111 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5115 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5119 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5123 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5124 &have_staged_files);
5125 if (err && err->code != GOT_ERR_CANCELLED)
5127 if (have_staged_files) {
5128 err = check_non_staged_files(fileindex, paths);
5133 cc_arg.commitable_paths = &commitable_paths;
5134 cc_arg.worktree = worktree;
5136 cc_arg.have_staged_files = have_staged_files;
5137 TAILQ_FOREACH(pe, paths, entry) {
5138 err = worktree_status(worktree, pe->path, fileindex, repo,
5139 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5144 if (TAILQ_EMPTY(&commitable_paths)) {
5145 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5149 TAILQ_FOREACH(pe, paths, entry) {
5150 err = check_path_is_commitable(pe->path, &commitable_paths);
5155 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5156 struct got_commitable *ct = pe->data;
5157 const char *ct_path = ct->in_repo_path;
5159 while (ct_path[0] == '/')
5161 err = check_out_of_date(ct_path, ct->status,
5162 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5163 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5169 err = commit_worktree(new_commit_id, &commitable_paths,
5170 head_commit_id, worktree, author, committer,
5171 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5175 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5176 fileindex, have_staged_files);
5177 sync_err = sync_fileindex(fileindex, fileindex_path);
5178 if (sync_err && err == NULL)
5182 got_fileindex_free(fileindex);
5183 free(fileindex_path);
5184 unlockerr = lock_worktree(worktree, LOCK_SH);
5185 if (unlockerr && err == NULL)
5187 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5188 struct got_commitable *ct = pe->data;
5189 free_commitable(ct);
5191 got_pathlist_free(&commitable_paths);
5196 got_commitable_get_path(struct got_commitable *ct)
5202 got_commitable_get_status(struct got_commitable *ct)
5207 struct check_rebase_ok_arg {
5208 struct got_worktree *worktree;
5209 struct got_repository *repo;
5212 static const struct got_error *
5213 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5215 const struct got_error *err = NULL;
5216 struct check_rebase_ok_arg *a = arg;
5217 unsigned char status;
5221 /* Reject rebase of a work tree with mixed base commits. */
5222 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5223 SHA1_DIGEST_LENGTH))
5224 return got_error(GOT_ERR_MIXED_COMMITS);
5226 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5228 return got_error_from_errno("asprintf");
5230 /* Reject rebase of a work tree with modified or staged files. */
5231 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5236 if (status != GOT_STATUS_NO_CHANGE)
5237 return got_error(GOT_ERR_MODIFIED);
5238 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5239 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5244 const struct got_error *
5245 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5246 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5247 struct got_worktree *worktree, struct got_reference *branch,
5248 struct got_repository *repo)
5250 const struct got_error *err = NULL;
5251 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5252 char *branch_ref_name = NULL;
5253 char *fileindex_path = NULL;
5254 struct check_rebase_ok_arg ok_arg;
5255 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5256 struct got_object_id *wt_branch_tip = NULL;
5258 *new_base_branch_ref = NULL;
5262 err = lock_worktree(worktree, LOCK_EX);
5266 err = open_fileindex(fileindex, &fileindex_path, worktree);
5270 ok_arg.worktree = worktree;
5272 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5277 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5281 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5285 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5289 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5294 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5297 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5298 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5302 err = got_ref_alloc_symref(new_base_branch_ref,
5303 new_base_branch_ref_name, wt_branch);
5306 err = got_ref_write(*new_base_branch_ref, repo);
5310 /* TODO Lock original branch's ref while rebasing? */
5312 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5316 err = got_ref_write(branch_ref, repo);
5320 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5321 worktree->base_commit_id);
5324 err = got_ref_write(*tmp_branch, repo);
5328 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5332 free(fileindex_path);
5333 free(tmp_branch_name);
5334 free(new_base_branch_ref_name);
5335 free(branch_ref_name);
5337 got_ref_close(branch_ref);
5339 got_ref_close(wt_branch);
5340 free(wt_branch_tip);
5342 if (*new_base_branch_ref) {
5343 got_ref_close(*new_base_branch_ref);
5344 *new_base_branch_ref = NULL;
5347 got_ref_close(*tmp_branch);
5351 got_fileindex_free(*fileindex);
5354 lock_worktree(worktree, LOCK_SH);
5359 const struct got_error *
5360 got_worktree_rebase_continue(struct got_object_id **commit_id,
5361 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5362 struct got_reference **branch, struct got_fileindex **fileindex,
5363 struct got_worktree *worktree, struct got_repository *repo)
5365 const struct got_error *err;
5366 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5367 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5368 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5369 char *fileindex_path = NULL;
5370 int have_staged_files = 0;
5373 *new_base_branch = NULL;
5378 err = lock_worktree(worktree, LOCK_EX);
5382 err = open_fileindex(fileindex, &fileindex_path, worktree);
5386 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5387 &have_staged_files);
5388 if (err && err->code != GOT_ERR_CANCELLED)
5390 if (have_staged_files) {
5391 err = got_error(GOT_ERR_STAGED_PATHS);
5395 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5399 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5403 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5407 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5411 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5415 err = got_ref_open(branch, repo,
5416 got_ref_get_symref_target(branch_ref), 0);
5420 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5424 err = got_ref_resolve(commit_id, repo, commit_ref);
5428 err = got_ref_open(new_base_branch, repo,
5429 new_base_branch_ref_name, 0);
5433 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5437 free(commit_ref_name);
5438 free(branch_ref_name);
5439 free(fileindex_path);
5441 got_ref_close(commit_ref);
5443 got_ref_close(branch_ref);
5448 got_ref_close(*tmp_branch);
5451 if (*new_base_branch) {
5452 got_ref_close(*new_base_branch);
5453 *new_base_branch = NULL;
5456 got_ref_close(*branch);
5460 got_fileindex_free(*fileindex);
5463 lock_worktree(worktree, LOCK_SH);
5468 const struct got_error *
5469 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5471 const struct got_error *err;
5472 char *tmp_branch_name = NULL;
5474 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5478 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5479 free(tmp_branch_name);
5483 static const struct got_error *
5484 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5485 char **logmsg, void *arg)
5491 static const struct got_error *
5492 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5493 const char *path, struct got_object_id *blob_id,
5494 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5495 int dirfd, const char *de_name)
5500 struct collect_merged_paths_arg {
5501 got_worktree_checkout_cb progress_cb;
5503 struct got_pathlist_head *merged_paths;
5506 static const struct got_error *
5507 collect_merged_paths(void *arg, unsigned char status, const char *path)
5509 const struct got_error *err;
5510 struct collect_merged_paths_arg *a = arg;
5512 struct got_pathlist_entry *new;
5514 err = (*a->progress_cb)(a->progress_arg, status, path);
5518 if (status != GOT_STATUS_MERGE &&
5519 status != GOT_STATUS_ADD &&
5520 status != GOT_STATUS_DELETE &&
5521 status != GOT_STATUS_CONFLICT)
5526 return got_error_from_errno("strdup");
5528 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5529 if (err || new == NULL)
5535 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5537 struct got_pathlist_entry *pe;
5539 TAILQ_FOREACH(pe, merged_paths, entry)
5540 free((char *)pe->path);
5542 got_pathlist_free(merged_paths);
5545 static const struct got_error *
5546 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5547 int is_rebase, struct got_repository *repo)
5549 const struct got_error *err;
5550 struct got_reference *commit_ref = NULL;
5552 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5554 if (err->code != GOT_ERR_NOT_REF)
5556 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
5559 err = got_ref_write(commit_ref, repo);
5562 } else if (is_rebase) {
5563 struct got_object_id *stored_id;
5566 err = got_ref_resolve(&stored_id, repo, commit_ref);
5569 cmp = got_object_id_cmp(commit_id, stored_id);
5572 err = got_error(GOT_ERR_REBASE_COMMITID);
5578 got_ref_close(commit_ref);
5582 static const struct got_error *
5583 rebase_merge_files(struct got_pathlist_head *merged_paths,
5584 const char *commit_ref_name, struct got_worktree *worktree,
5585 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
5586 struct got_object_id *commit_id, 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 struct got_reference *commit_ref = NULL;
5592 struct collect_merged_paths_arg cmp_arg;
5593 char *fileindex_path;
5595 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5597 err = get_fileindex_path(&fileindex_path, worktree);
5601 cmp_arg.progress_cb = progress_cb;
5602 cmp_arg.progress_arg = progress_arg;
5603 cmp_arg.merged_paths = merged_paths;
5604 err = merge_files(worktree, fileindex, fileindex_path,
5605 parent_commit_id, commit_id, repo, collect_merged_paths,
5606 &cmp_arg, cancel_cb, cancel_arg);
5608 got_ref_close(commit_ref);
5612 const struct got_error *
5613 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
5614 struct got_worktree *worktree, struct got_fileindex *fileindex,
5615 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5616 struct got_repository *repo,
5617 got_worktree_checkout_cb progress_cb, void *progress_arg,
5618 got_cancel_cb cancel_cb, void *cancel_arg)
5620 const struct got_error *err;
5621 char *commit_ref_name;
5623 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5627 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
5631 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5632 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5633 progress_arg, cancel_cb, cancel_arg);
5635 free(commit_ref_name);
5639 const struct got_error *
5640 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
5641 struct got_worktree *worktree, struct got_fileindex *fileindex,
5642 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5643 struct got_repository *repo,
5644 got_worktree_checkout_cb progress_cb, void *progress_arg,
5645 got_cancel_cb cancel_cb, void *cancel_arg)
5647 const struct got_error *err;
5648 char *commit_ref_name;
5650 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5654 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
5658 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5659 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5660 progress_arg, cancel_cb, cancel_arg);
5662 free(commit_ref_name);
5666 static const struct got_error *
5667 rebase_commit(struct got_object_id **new_commit_id,
5668 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
5669 struct got_worktree *worktree, struct got_fileindex *fileindex,
5670 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
5671 const char *new_logmsg, struct got_repository *repo)
5673 const struct got_error *err, *sync_err;
5674 struct got_pathlist_head commitable_paths;
5675 struct collect_commitables_arg cc_arg;
5676 char *fileindex_path = NULL;
5677 struct got_reference *head_ref = NULL;
5678 struct got_object_id *head_commit_id = NULL;
5679 char *logmsg = NULL;
5681 TAILQ_INIT(&commitable_paths);
5682 *new_commit_id = NULL;
5684 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5686 err = get_fileindex_path(&fileindex_path, worktree);
5690 cc_arg.commitable_paths = &commitable_paths;
5691 cc_arg.worktree = worktree;
5693 cc_arg.have_staged_files = 0;
5695 * If possible get the status of individual files directly to
5696 * avoid crawling the entire work tree once per rebased commit.
5697 * TODO: Ideally, merged_paths would contain a list of commitables
5698 * we could use so we could skip worktree_status() entirely.
5701 struct got_pathlist_entry *pe;
5702 TAILQ_FOREACH(pe, merged_paths, entry) {
5703 err = worktree_status(worktree, pe->path, fileindex,
5704 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
5710 err = worktree_status(worktree, "", fileindex, repo,
5711 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5716 if (TAILQ_EMPTY(&commitable_paths)) {
5717 /* No-op change; commit will be elided. */
5718 err = got_ref_delete(commit_ref, repo);
5721 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5725 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5729 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5734 logmsg = strdup(new_logmsg);
5735 if (logmsg == NULL) {
5736 err = got_error_from_errno("strdup");
5740 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
5745 /* NB: commit_worktree will call free(logmsg) */
5746 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
5747 worktree, got_object_commit_get_author(orig_commit),
5748 got_object_commit_get_committer(orig_commit),
5749 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
5753 err = got_ref_change_ref(tmp_branch, *new_commit_id);
5757 err = got_ref_delete(commit_ref, repo);
5761 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5763 sync_err = sync_fileindex(fileindex, fileindex_path);
5764 if (sync_err && err == NULL)
5767 free(fileindex_path);
5768 free(head_commit_id);
5770 got_ref_close(head_ref);
5772 free(*new_commit_id);
5773 *new_commit_id = NULL;
5778 const struct got_error *
5779 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
5780 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5781 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5782 struct got_commit_object *orig_commit,
5783 struct got_object_id *orig_commit_id, struct got_repository *repo)
5785 const struct got_error *err;
5786 char *commit_ref_name;
5787 struct got_reference *commit_ref = NULL;
5788 struct got_object_id *commit_id = NULL;
5790 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5794 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5797 err = got_ref_resolve(&commit_id, repo, commit_ref);
5800 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5801 err = got_error(GOT_ERR_REBASE_COMMITID);
5805 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5806 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
5809 got_ref_close(commit_ref);
5810 free(commit_ref_name);
5815 const struct got_error *
5816 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
5817 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5818 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5819 struct got_commit_object *orig_commit,
5820 struct got_object_id *orig_commit_id, const char *new_logmsg,
5821 struct got_repository *repo)
5823 const struct got_error *err;
5824 char *commit_ref_name;
5825 struct got_reference *commit_ref = NULL;
5827 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5831 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5835 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5836 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
5839 got_ref_close(commit_ref);
5840 free(commit_ref_name);
5844 const struct got_error *
5845 got_worktree_rebase_postpone(struct got_worktree *worktree,
5846 struct got_fileindex *fileindex)
5849 got_fileindex_free(fileindex);
5850 return lock_worktree(worktree, LOCK_SH);
5853 static const struct got_error *
5854 delete_ref(const char *name, struct got_repository *repo)
5856 const struct got_error *err;
5857 struct got_reference *ref;
5859 err = got_ref_open(&ref, repo, name, 0);
5861 if (err->code == GOT_ERR_NOT_REF)
5866 err = got_ref_delete(ref, repo);
5871 static const struct got_error *
5872 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
5874 const struct got_error *err;
5875 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5876 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5878 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5881 err = delete_ref(tmp_branch_name, repo);
5885 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5888 err = delete_ref(new_base_branch_ref_name, repo);
5892 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5895 err = delete_ref(branch_ref_name, repo);
5899 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5902 err = delete_ref(commit_ref_name, repo);
5907 free(tmp_branch_name);
5908 free(new_base_branch_ref_name);
5909 free(branch_ref_name);
5910 free(commit_ref_name);
5914 const struct got_error *
5915 got_worktree_rebase_complete(struct got_worktree *worktree,
5916 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
5917 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
5918 struct got_repository *repo)
5920 const struct got_error *err, *unlockerr;
5921 struct got_object_id *new_head_commit_id = NULL;
5923 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5927 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
5931 err = got_ref_write(rebased_branch, repo);
5935 err = got_worktree_set_head_ref(worktree, rebased_branch);
5939 err = delete_rebase_refs(worktree, repo);
5942 got_fileindex_free(fileindex);
5943 free(new_head_commit_id);
5944 unlockerr = lock_worktree(worktree, LOCK_SH);
5945 if (unlockerr && err == NULL)
5950 const struct got_error *
5951 got_worktree_rebase_abort(struct got_worktree *worktree,
5952 struct got_fileindex *fileindex, struct got_repository *repo,
5953 struct got_reference *new_base_branch,
5954 got_worktree_checkout_cb progress_cb, void *progress_arg)
5956 const struct got_error *err, *unlockerr, *sync_err;
5957 struct got_reference *resolved = NULL;
5958 struct got_object_id *commit_id = NULL;
5959 char *fileindex_path = NULL;
5960 struct revert_file_args rfa;
5961 struct got_object_id *tree_id = NULL;
5963 err = lock_worktree(worktree, LOCK_EX);
5967 err = got_ref_open(&resolved, repo,
5968 got_ref_get_symref_target(new_base_branch), 0);
5972 err = got_worktree_set_head_ref(worktree, resolved);
5977 * XXX commits to the base branch could have happened while
5978 * we were busy rebasing; should we store the original commit ID
5979 * when rebase begins and read it back here?
5981 err = got_ref_resolve(&commit_id, repo, resolved);
5985 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5989 err = got_object_id_by_path(&tree_id, repo,
5990 worktree->base_commit_id, worktree->path_prefix);
5994 err = delete_rebase_refs(worktree, repo);
5998 err = get_fileindex_path(&fileindex_path, worktree);
6002 rfa.worktree = worktree;
6003 rfa.fileindex = fileindex;
6004 rfa.progress_cb = progress_cb;
6005 rfa.progress_arg = progress_arg;
6006 rfa.patch_cb = NULL;
6007 rfa.patch_arg = NULL;
6009 err = worktree_status(worktree, "", fileindex, repo,
6010 revert_file, &rfa, NULL, NULL, 0, 0);
6014 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6015 repo, progress_cb, progress_arg, NULL, NULL);
6017 sync_err = sync_fileindex(fileindex, fileindex_path);
6018 if (sync_err && err == NULL)
6021 got_ref_close(resolved);
6025 got_fileindex_free(fileindex);
6026 free(fileindex_path);
6028 unlockerr = lock_worktree(worktree, LOCK_SH);
6029 if (unlockerr && err == NULL)
6034 const struct got_error *
6035 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6036 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6037 struct got_fileindex **fileindex, struct got_worktree *worktree,
6038 struct got_repository *repo)
6040 const struct got_error *err = NULL;
6041 char *tmp_branch_name = NULL;
6042 char *branch_ref_name = NULL;
6043 char *base_commit_ref_name = NULL;
6044 char *fileindex_path = NULL;
6045 struct check_rebase_ok_arg ok_arg;
6046 struct got_reference *wt_branch = NULL;
6047 struct got_reference *base_commit_ref = NULL;
6051 *base_commit_id = NULL;
6054 err = lock_worktree(worktree, LOCK_EX);
6058 err = open_fileindex(fileindex, &fileindex_path, worktree);
6062 ok_arg.worktree = worktree;
6064 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6069 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6073 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6077 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6082 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6087 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6091 err = got_ref_write(*branch_ref, repo);
6095 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6096 worktree->base_commit_id);
6099 err = got_ref_write(base_commit_ref, repo);
6102 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6103 if (*base_commit_id == NULL) {
6104 err = got_error_from_errno("got_object_id_dup");
6108 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6109 worktree->base_commit_id);
6112 err = got_ref_write(*tmp_branch, repo);
6116 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6120 free(fileindex_path);
6121 free(tmp_branch_name);
6122 free(branch_ref_name);
6123 free(base_commit_ref_name);
6125 got_ref_close(wt_branch);
6128 got_ref_close(*branch_ref);
6132 got_ref_close(*tmp_branch);
6135 free(*base_commit_id);
6137 got_fileindex_free(*fileindex);
6140 lock_worktree(worktree, LOCK_SH);
6145 const struct got_error *
6146 got_worktree_histedit_postpone(struct got_worktree *worktree,
6147 struct got_fileindex *fileindex)
6150 got_fileindex_free(fileindex);
6151 return lock_worktree(worktree, LOCK_SH);
6154 const struct got_error *
6155 got_worktree_histedit_in_progress(int *in_progress,
6156 struct got_worktree *worktree)
6158 const struct got_error *err;
6159 char *tmp_branch_name = NULL;
6161 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6165 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6166 free(tmp_branch_name);
6170 const struct got_error *
6171 got_worktree_histedit_continue(struct got_object_id **commit_id,
6172 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6173 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6174 struct got_worktree *worktree, struct got_repository *repo)
6176 const struct got_error *err;
6177 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6178 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6179 struct got_reference *commit_ref = NULL;
6180 struct got_reference *base_commit_ref = NULL;
6181 char *fileindex_path = NULL;
6182 int have_staged_files = 0;
6186 *base_commit_id = NULL;
6189 err = lock_worktree(worktree, LOCK_EX);
6193 err = open_fileindex(fileindex, &fileindex_path, worktree);
6197 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6198 &have_staged_files);
6199 if (err && err->code != GOT_ERR_CANCELLED)
6201 if (have_staged_files) {
6202 err = got_error(GOT_ERR_STAGED_PATHS);
6206 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6210 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6214 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6218 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6223 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6227 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6230 err = got_ref_resolve(commit_id, repo, commit_ref);
6234 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6237 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6241 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6245 free(commit_ref_name);
6246 free(branch_ref_name);
6247 free(fileindex_path);
6249 got_ref_close(commit_ref);
6250 if (base_commit_ref)
6251 got_ref_close(base_commit_ref);
6255 free(*base_commit_id);
6256 *base_commit_id = NULL;
6258 got_ref_close(*tmp_branch);
6262 got_fileindex_free(*fileindex);
6265 lock_worktree(worktree, LOCK_EX);
6270 static const struct got_error *
6271 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6273 const struct got_error *err;
6274 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6275 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6277 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6280 err = delete_ref(tmp_branch_name, repo);
6284 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6288 err = delete_ref(base_commit_ref_name, repo);
6292 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6295 err = delete_ref(branch_ref_name, repo);
6299 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6302 err = delete_ref(commit_ref_name, repo);
6306 free(tmp_branch_name);
6307 free(base_commit_ref_name);
6308 free(branch_ref_name);
6309 free(commit_ref_name);
6313 const struct got_error *
6314 got_worktree_histedit_abort(struct got_worktree *worktree,
6315 struct got_fileindex *fileindex, struct got_repository *repo,
6316 struct got_reference *branch, struct got_object_id *base_commit_id,
6317 got_worktree_checkout_cb progress_cb, void *progress_arg)
6319 const struct got_error *err, *unlockerr, *sync_err;
6320 struct got_reference *resolved = NULL;
6321 char *fileindex_path = NULL;
6322 struct got_object_id *tree_id = NULL;
6323 struct revert_file_args rfa;
6325 err = lock_worktree(worktree, LOCK_EX);
6329 err = got_ref_open(&resolved, repo,
6330 got_ref_get_symref_target(branch), 0);
6334 err = got_worktree_set_head_ref(worktree, resolved);
6338 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6342 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6343 worktree->path_prefix);
6347 err = delete_histedit_refs(worktree, repo);
6351 err = get_fileindex_path(&fileindex_path, worktree);
6355 rfa.worktree = worktree;
6356 rfa.fileindex = fileindex;
6357 rfa.progress_cb = progress_cb;
6358 rfa.progress_arg = progress_arg;
6359 rfa.patch_cb = NULL;
6360 rfa.patch_arg = NULL;
6362 err = worktree_status(worktree, "", fileindex, repo,
6363 revert_file, &rfa, NULL, NULL, 0, 0);
6367 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6368 repo, progress_cb, progress_arg, NULL, NULL);
6370 sync_err = sync_fileindex(fileindex, fileindex_path);
6371 if (sync_err && err == NULL)
6374 got_ref_close(resolved);
6376 free(fileindex_path);
6378 unlockerr = lock_worktree(worktree, LOCK_SH);
6379 if (unlockerr && err == NULL)
6384 const struct got_error *
6385 got_worktree_histedit_complete(struct got_worktree *worktree,
6386 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6387 struct got_reference *edited_branch, struct got_repository *repo)
6389 const struct got_error *err, *unlockerr;
6390 struct got_object_id *new_head_commit_id = NULL;
6391 struct got_reference *resolved = NULL;
6393 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6397 err = got_ref_open(&resolved, repo,
6398 got_ref_get_symref_target(edited_branch), 0);
6402 err = got_ref_change_ref(resolved, new_head_commit_id);
6406 err = got_ref_write(resolved, repo);
6410 err = got_worktree_set_head_ref(worktree, resolved);
6414 err = delete_histedit_refs(worktree, repo);
6417 got_fileindex_free(fileindex);
6418 free(new_head_commit_id);
6419 unlockerr = lock_worktree(worktree, LOCK_SH);
6420 if (unlockerr && err == NULL)
6425 const struct got_error *
6426 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6427 struct got_object_id *commit_id, struct got_repository *repo)
6429 const struct got_error *err;
6430 char *commit_ref_name;
6432 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6436 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6440 err = delete_ref(commit_ref_name, repo);
6442 free(commit_ref_name);
6446 const struct got_error *
6447 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6448 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6449 struct got_worktree *worktree, const char *refname,
6450 struct got_repository *repo)
6452 const struct got_error *err = NULL;
6453 char *fileindex_path = NULL;
6454 struct check_rebase_ok_arg ok_arg;
6458 *base_branch_ref = NULL;
6460 err = lock_worktree(worktree, LOCK_EX);
6464 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6465 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6466 "cannot integrate a branch into itself; "
6467 "update -b or different branch name required");
6471 err = open_fileindex(fileindex, &fileindex_path, worktree);
6475 /* Preconditions are the same as for rebase. */
6476 ok_arg.worktree = worktree;
6478 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6483 err = got_ref_open(branch_ref, repo, refname, 1);
6487 err = got_ref_open(base_branch_ref, repo,
6488 got_worktree_get_head_ref_name(worktree), 1);
6492 got_ref_close(*branch_ref);
6495 if (*base_branch_ref) {
6496 got_ref_close(*base_branch_ref);
6497 *base_branch_ref = NULL;
6500 got_fileindex_free(*fileindex);
6503 lock_worktree(worktree, LOCK_SH);
6508 const struct got_error *
6509 got_worktree_integrate_continue(struct got_worktree *worktree,
6510 struct got_fileindex *fileindex, struct got_repository *repo,
6511 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6512 got_worktree_checkout_cb progress_cb, void *progress_arg,
6513 got_cancel_cb cancel_cb, void *cancel_arg)
6515 const struct got_error *err = NULL, *sync_err, *unlockerr;
6516 char *fileindex_path = NULL;
6517 struct got_object_id *tree_id = NULL, *commit_id = NULL;
6519 err = get_fileindex_path(&fileindex_path, worktree);
6523 err = got_ref_resolve(&commit_id, repo, branch_ref);
6527 err = got_object_id_by_path(&tree_id, repo, commit_id,
6528 worktree->path_prefix);
6532 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6536 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6537 progress_cb, progress_arg, cancel_cb, cancel_arg);
6541 err = got_ref_change_ref(base_branch_ref, commit_id);
6545 err = got_ref_write(base_branch_ref, repo);
6547 sync_err = sync_fileindex(fileindex, fileindex_path);
6548 if (sync_err && err == NULL)
6552 unlockerr = got_ref_unlock(branch_ref);
6553 if (unlockerr && err == NULL)
6555 got_ref_close(branch_ref);
6557 unlockerr = got_ref_unlock(base_branch_ref);
6558 if (unlockerr && err == NULL)
6560 got_ref_close(base_branch_ref);
6562 got_fileindex_free(fileindex);
6563 free(fileindex_path);
6566 unlockerr = lock_worktree(worktree, LOCK_SH);
6567 if (unlockerr && err == NULL)
6572 const struct got_error *
6573 got_worktree_integrate_abort(struct got_worktree *worktree,
6574 struct got_fileindex *fileindex, struct got_repository *repo,
6575 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
6577 const struct got_error *err = NULL, *unlockerr = NULL;
6579 got_fileindex_free(fileindex);
6581 err = lock_worktree(worktree, LOCK_SH);
6583 unlockerr = got_ref_unlock(branch_ref);
6584 if (unlockerr && err == NULL)
6586 got_ref_close(branch_ref);
6588 unlockerr = got_ref_unlock(base_branch_ref);
6589 if (unlockerr && err == NULL)
6591 got_ref_close(base_branch_ref);
6596 struct check_stage_ok_arg {
6597 struct got_object_id *head_commit_id;
6598 struct got_worktree *worktree;
6599 struct got_fileindex *fileindex;
6600 struct got_repository *repo;
6604 const struct got_error *
6605 check_stage_ok(void *arg, unsigned char status,
6606 unsigned char staged_status, const char *relpath,
6607 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6608 struct got_object_id *commit_id, int dirfd, const char *de_name)
6610 struct check_stage_ok_arg *a = arg;
6611 const struct got_error *err = NULL;
6612 struct got_fileindex_entry *ie;
6613 struct got_object_id base_commit_id;
6614 struct got_object_id *base_commit_idp = NULL;
6615 char *in_repo_path = NULL, *p;
6617 if (status == GOT_STATUS_UNVERSIONED ||
6618 status == GOT_STATUS_NO_CHANGE)
6620 if (status == GOT_STATUS_NONEXISTENT)
6621 return got_error_set_errno(ENOENT, relpath);
6623 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6625 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6627 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
6628 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
6630 return got_error_from_errno("asprintf");
6632 if (got_fileindex_entry_has_commit(ie)) {
6633 memcpy(base_commit_id.sha1, ie->commit_sha1,
6634 SHA1_DIGEST_LENGTH);
6635 base_commit_idp = &base_commit_id;
6638 if (status == GOT_STATUS_CONFLICT) {
6639 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
6641 } else if (status != GOT_STATUS_ADD &&
6642 status != GOT_STATUS_MODIFY &&
6643 status != GOT_STATUS_DELETE) {
6644 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
6648 a->have_changes = 1;
6653 err = check_out_of_date(p, status, staged_status,
6654 blob_id, base_commit_idp, a->head_commit_id, a->repo,
6655 GOT_ERR_STAGE_OUT_OF_DATE);
6661 struct stage_path_arg {
6662 struct got_worktree *worktree;
6663 struct got_fileindex *fileindex;
6664 struct got_repository *repo;
6665 got_worktree_status_cb status_cb;
6667 got_worktree_patch_cb patch_cb;
6669 int staged_something;
6672 static const struct got_error *
6673 stage_path(void *arg, unsigned char status,
6674 unsigned char staged_status, const char *relpath,
6675 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6676 struct got_object_id *commit_id, int dirfd, const char *de_name)
6678 struct stage_path_arg *a = arg;
6679 const struct got_error *err = NULL;
6680 struct got_fileindex_entry *ie;
6681 char *ondisk_path = NULL, *path_content = NULL;
6683 struct got_object_id *new_staged_blob_id = NULL;
6685 if (status == GOT_STATUS_UNVERSIONED)
6688 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6690 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6692 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
6694 return got_error_from_errno("asprintf");
6697 case GOT_STATUS_ADD:
6698 case GOT_STATUS_MODIFY:
6700 if (status == GOT_STATUS_ADD) {
6701 int choice = GOT_PATCH_CHOICE_NONE;
6702 err = (*a->patch_cb)(&choice, a->patch_arg,
6703 status, ie->path, NULL, 1, 1);
6706 if (choice != GOT_PATCH_CHOICE_YES)
6709 err = create_patched_content(&path_content, 0,
6710 staged_blob_id ? staged_blob_id : blob_id,
6711 ondisk_path, dirfd, de_name, ie->path,
6712 a->repo, a->patch_cb, a->patch_arg);
6713 if (err || path_content == NULL)
6717 err = got_object_blob_create(&new_staged_blob_id,
6718 path_content ? path_content : ondisk_path, a->repo);
6721 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
6722 SHA1_DIGEST_LENGTH);
6723 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
6724 stage = GOT_FILEIDX_STAGE_ADD;
6726 stage = GOT_FILEIDX_STAGE_MODIFY;
6727 got_fileindex_entry_stage_set(ie, stage);
6728 a->staged_something = 1;
6729 if (a->status_cb == NULL)
6731 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6732 get_staged_status(ie), relpath, blob_id,
6733 new_staged_blob_id, NULL, dirfd, de_name);
6735 case GOT_STATUS_DELETE:
6736 if (staged_status == GOT_STATUS_DELETE)
6739 int choice = GOT_PATCH_CHOICE_NONE;
6740 err = (*a->patch_cb)(&choice, a->patch_arg, status,
6741 ie->path, NULL, 1, 1);
6744 if (choice == GOT_PATCH_CHOICE_NO)
6746 if (choice != GOT_PATCH_CHOICE_YES) {
6747 err = got_error(GOT_ERR_PATCH_CHOICE);
6751 stage = GOT_FILEIDX_STAGE_DELETE;
6752 got_fileindex_entry_stage_set(ie, stage);
6753 a->staged_something = 1;
6754 if (a->status_cb == NULL)
6756 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6757 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
6760 case GOT_STATUS_NO_CHANGE:
6762 case GOT_STATUS_CONFLICT:
6763 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
6765 case GOT_STATUS_NONEXISTENT:
6766 err = got_error_set_errno(ENOENT, relpath);
6769 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
6773 if (path_content && unlink(path_content) == -1 && err == NULL)
6774 err = got_error_from_errno2("unlink", path_content);
6777 free(new_staged_blob_id);
6781 const struct got_error *
6782 got_worktree_stage(struct got_worktree *worktree,
6783 struct got_pathlist_head *paths,
6784 got_worktree_status_cb status_cb, void *status_arg,
6785 got_worktree_patch_cb patch_cb, void *patch_arg,
6786 struct got_repository *repo)
6788 const struct got_error *err = NULL, *sync_err, *unlockerr;
6789 struct got_pathlist_entry *pe;
6790 struct got_fileindex *fileindex = NULL;
6791 char *fileindex_path = NULL;
6792 struct got_reference *head_ref = NULL;
6793 struct got_object_id *head_commit_id = NULL;
6794 struct check_stage_ok_arg oka;
6795 struct stage_path_arg spa;
6797 err = lock_worktree(worktree, LOCK_EX);
6801 err = got_ref_open(&head_ref, repo,
6802 got_worktree_get_head_ref_name(worktree), 0);
6805 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6808 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6812 /* Check pre-conditions before staging anything. */
6813 oka.head_commit_id = head_commit_id;
6814 oka.worktree = worktree;
6815 oka.fileindex = fileindex;
6817 oka.have_changes = 0;
6818 TAILQ_FOREACH(pe, paths, entry) {
6819 err = worktree_status(worktree, pe->path, fileindex, repo,
6820 check_stage_ok, &oka, NULL, NULL, 0, 0);
6824 if (!oka.have_changes) {
6825 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6829 spa.worktree = worktree;
6830 spa.fileindex = fileindex;
6832 spa.patch_cb = patch_cb;
6833 spa.patch_arg = patch_arg;
6834 spa.status_cb = status_cb;
6835 spa.status_arg = status_arg;
6836 spa.staged_something = 0;
6837 TAILQ_FOREACH(pe, paths, entry) {
6838 err = worktree_status(worktree, pe->path, fileindex, repo,
6839 stage_path, &spa, NULL, NULL, 0, 0);
6843 if (!spa.staged_something) {
6844 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6848 sync_err = sync_fileindex(fileindex, fileindex_path);
6849 if (sync_err && err == NULL)
6853 got_ref_close(head_ref);
6854 free(head_commit_id);
6855 free(fileindex_path);
6857 got_fileindex_free(fileindex);
6858 unlockerr = lock_worktree(worktree, LOCK_SH);
6859 if (unlockerr && err == NULL)
6864 struct unstage_path_arg {
6865 struct got_worktree *worktree;
6866 struct got_fileindex *fileindex;
6867 struct got_repository *repo;
6868 got_worktree_checkout_cb progress_cb;
6870 got_worktree_patch_cb patch_cb;
6874 static const struct got_error *
6875 create_unstaged_content(char **path_unstaged_content,
6876 char **path_new_staged_content, struct got_object_id *blob_id,
6877 struct got_object_id *staged_blob_id, const char *relpath,
6878 struct got_repository *repo,
6879 got_worktree_patch_cb patch_cb, void *patch_arg)
6881 const struct got_error *err;
6882 struct got_blob_object *blob = NULL, *staged_blob = NULL;
6883 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
6884 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
6885 struct stat sb1, sb2;
6886 struct got_diff_changes *changes = NULL;
6887 struct got_diff_state *ds = NULL;
6888 struct got_diff_args *args = NULL;
6889 struct got_diff_change *change;
6890 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
6891 int have_content = 0, have_rejected_content = 0;
6893 *path_unstaged_content = NULL;
6894 *path_new_staged_content = NULL;
6896 err = got_object_id_str(&label1, blob_id);
6899 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
6903 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
6907 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
6911 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
6915 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
6919 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
6923 if (stat(path1, &sb1) == -1) {
6924 err = got_error_from_errno2("stat", path1);
6928 if (stat(path2, &sb2) == -1) {
6929 err = got_error_from_errno2("stat", path2);
6933 err = got_diff_files(&changes, &ds, &args, &diff_flags,
6934 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
6938 err = got_opentemp_named(path_unstaged_content, &outfile,
6939 "got-unstaged-content");
6942 err = got_opentemp_named(path_new_staged_content, &rejectfile,
6943 "got-new-staged-content");
6947 if (fseek(f1, 0L, SEEK_SET) == -1) {
6948 err = got_ferror(f1, GOT_ERR_IO);
6951 if (fseek(f2, 0L, SEEK_SET) == -1) {
6952 err = got_ferror(f2, GOT_ERR_IO);
6955 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
6957 err = apply_or_reject_change(&choice, change, ++n,
6958 changes->nchanges, ds, args, diff_flags, relpath,
6959 f1, f2, &line_cur1, &line_cur2,
6960 outfile, rejectfile, patch_cb, patch_arg);
6963 if (choice == GOT_PATCH_CHOICE_YES)
6966 have_rejected_content = 1;
6967 if (choice == GOT_PATCH_CHOICE_QUIT)
6970 if (have_content || have_rejected_content)
6971 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
6972 outfile, rejectfile);
6976 got_object_blob_close(blob);
6978 got_object_blob_close(staged_blob);
6979 if (f1 && fclose(f1) == EOF && err == NULL)
6980 err = got_error_from_errno2("fclose", path1);
6981 if (f2 && fclose(f2) == EOF && err == NULL)
6982 err = got_error_from_errno2("fclose", path2);
6983 if (outfile && fclose(outfile) == EOF && err == NULL)
6984 err = got_error_from_errno2("fclose", *path_unstaged_content);
6985 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
6986 err = got_error_from_errno2("fclose", *path_new_staged_content);
6987 if (path1 && unlink(path1) == -1 && err == NULL)
6988 err = got_error_from_errno2("unlink", path1);
6989 if (path2 && unlink(path2) == -1 && err == NULL)
6990 err = got_error_from_errno2("unlink", path2);
6991 if (err || !have_content) {
6992 if (*path_unstaged_content &&
6993 unlink(*path_unstaged_content) == -1 && err == NULL)
6994 err = got_error_from_errno2("unlink",
6995 *path_unstaged_content);
6996 free(*path_unstaged_content);
6997 *path_unstaged_content = NULL;
6999 if (err || !have_rejected_content) {
7000 if (*path_new_staged_content &&
7001 unlink(*path_new_staged_content) == -1 && err == NULL)
7002 err = got_error_from_errno2("unlink",
7003 *path_new_staged_content);
7004 free(*path_new_staged_content);
7005 *path_new_staged_content = NULL;
7009 got_diff_state_free(ds);
7013 got_diff_free_changes(changes);
7019 static const struct got_error *
7020 unstage_path(void *arg, unsigned char status,
7021 unsigned char staged_status, const char *relpath,
7022 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7023 struct got_object_id *commit_id, int dirfd, const char *de_name)
7025 const struct got_error *err = NULL;
7026 struct unstage_path_arg *a = arg;
7027 struct got_fileindex_entry *ie;
7028 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7029 char *ondisk_path = NULL, *path_unstaged_content = NULL;
7030 char *path_new_staged_content = NULL;
7031 char *id_str = NULL, *label_orig = NULL;
7032 int local_changes_subsumed;
7035 if (staged_status != GOT_STATUS_ADD &&
7036 staged_status != GOT_STATUS_MODIFY &&
7037 staged_status != GOT_STATUS_DELETE)
7040 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7042 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7044 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7046 return got_error_from_errno("asprintf");
7048 err = got_object_id_str(&id_str,
7049 commit_id ? commit_id : a->worktree->base_commit_id);
7052 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7054 err = got_error_from_errno("asprintf");
7058 switch (staged_status) {
7059 case GOT_STATUS_MODIFY:
7060 err = got_object_open_as_blob(&blob_base, a->repo,
7065 case GOT_STATUS_ADD:
7067 if (staged_status == GOT_STATUS_ADD) {
7068 int choice = GOT_PATCH_CHOICE_NONE;
7069 err = (*a->patch_cb)(&choice, a->patch_arg,
7070 staged_status, ie->path, NULL, 1, 1);
7073 if (choice != GOT_PATCH_CHOICE_YES)
7076 err = create_unstaged_content(
7077 &path_unstaged_content,
7078 &path_new_staged_content, blob_id,
7079 staged_blob_id, ie->path, a->repo,
7080 a->patch_cb, a->patch_arg);
7081 if (err || path_unstaged_content == NULL)
7083 if (path_new_staged_content) {
7084 err = got_object_blob_create(
7086 path_new_staged_content,
7090 memcpy(ie->staged_blob_sha1,
7091 staged_blob_id->sha1,
7092 SHA1_DIGEST_LENGTH);
7094 err = merge_file(&local_changes_subsumed,
7095 a->worktree, blob_base, ondisk_path,
7096 relpath, got_fileindex_perms_to_st(ie),
7097 path_unstaged_content, label_orig,
7098 "unstaged", a->repo, a->progress_cb,
7101 path_new_staged_content == NULL)
7102 got_fileindex_entry_stage_set(ie,
7103 GOT_FILEIDX_STAGE_NONE);
7104 break; /* Done with this file. */
7107 err = got_object_open_as_blob(&blob_staged, a->repo,
7108 staged_blob_id, 8192);
7111 err = merge_blob(&local_changes_subsumed, a->worktree,
7112 blob_base, ondisk_path, relpath,
7113 got_fileindex_perms_to_st(ie), label_orig, blob_staged,
7114 commit_id ? commit_id : a->worktree->base_commit_id,
7115 a->repo, a->progress_cb, a->progress_arg);
7117 got_fileindex_entry_stage_set(ie,
7118 GOT_FILEIDX_STAGE_NONE);
7120 case GOT_STATUS_DELETE:
7122 int choice = GOT_PATCH_CHOICE_NONE;
7123 err = (*a->patch_cb)(&choice, a->patch_arg,
7124 staged_status, ie->path, NULL, 1, 1);
7127 if (choice == GOT_PATCH_CHOICE_NO)
7129 if (choice != GOT_PATCH_CHOICE_YES) {
7130 err = got_error(GOT_ERR_PATCH_CHOICE);
7134 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7135 err = get_file_status(&status, &sb, ie, ondisk_path,
7136 dirfd, de_name, a->repo);
7139 err = (*a->progress_cb)(a->progress_arg, status, relpath);
7144 if (path_unstaged_content &&
7145 unlink(path_unstaged_content) == -1 && err == NULL)
7146 err = got_error_from_errno2("unlink", path_unstaged_content);
7147 if (path_new_staged_content &&
7148 unlink(path_new_staged_content) == -1 && err == NULL)
7149 err = got_error_from_errno2("unlink", path_new_staged_content);
7150 free(path_unstaged_content);
7151 free(path_new_staged_content);
7153 got_object_blob_close(blob_base);
7155 got_object_blob_close(blob_staged);
7161 const struct got_error *
7162 got_worktree_unstage(struct got_worktree *worktree,
7163 struct got_pathlist_head *paths,
7164 got_worktree_checkout_cb progress_cb, void *progress_arg,
7165 got_worktree_patch_cb patch_cb, void *patch_arg,
7166 struct got_repository *repo)
7168 const struct got_error *err = NULL, *sync_err, *unlockerr;
7169 struct got_pathlist_entry *pe;
7170 struct got_fileindex *fileindex = NULL;
7171 char *fileindex_path = NULL;
7172 struct unstage_path_arg upa;
7174 err = lock_worktree(worktree, LOCK_EX);
7178 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7182 upa.worktree = worktree;
7183 upa.fileindex = fileindex;
7185 upa.progress_cb = progress_cb;
7186 upa.progress_arg = progress_arg;
7187 upa.patch_cb = patch_cb;
7188 upa.patch_arg = patch_arg;
7189 TAILQ_FOREACH(pe, paths, entry) {
7190 err = worktree_status(worktree, pe->path, fileindex, repo,
7191 unstage_path, &upa, NULL, NULL, 0, 0);
7196 sync_err = sync_fileindex(fileindex, fileindex_path);
7197 if (sync_err && err == NULL)
7200 free(fileindex_path);
7202 got_fileindex_free(fileindex);
7203 unlockerr = lock_worktree(worktree, LOCK_SH);
7204 if (unlockerr && err == NULL)