2 * Copyright (c) 2018, 2019 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/limits.h>
19 #include <sys/queue.h>
37 #include "got_error.h"
38 #include "got_repository.h"
39 #include "got_reference.h"
40 #include "got_object.h"
42 #include "got_worktree.h"
43 #include "got_opentemp.h"
46 #include "got_lib_worktree.h"
47 #include "got_lib_sha1.h"
48 #include "got_lib_fileindex.h"
49 #include "got_lib_inflate.h"
50 #include "got_lib_delta.h"
51 #include "got_lib_object.h"
52 #include "got_lib_object_parse.h"
53 #include "got_lib_object_create.h"
54 #include "got_lib_object_idset.h"
55 #include "got_lib_diff.h"
58 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 static const struct got_error *
62 create_meta_file(const char *path_got, const char *name, const char *content)
64 const struct got_error *err = NULL;
67 if (asprintf(&path, "%s/%s", path_got, name) == -1)
68 return got_error_from_errno("asprintf");
70 err = got_path_create_file(path, content);
75 static const struct got_error *
76 update_meta_file(const char *path_got, const char *name, const char *content)
78 const struct got_error *err = NULL;
83 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
84 err = got_error_from_errno("asprintf");
89 err = got_opentemp_named(&tmppath, &tmpfile, path);
94 int len = fprintf(tmpfile, "%s\n", content);
95 if (len != strlen(content) + 1) {
96 err = got_error_from_errno2("fprintf", tmppath);
101 if (rename(tmppath, path) != 0) {
102 err = got_error_from_errno3("rename", tmppath, path);
108 if (fclose(tmpfile) != 0 && err == NULL)
109 err = got_error_from_errno2("fclose", tmppath);
114 static const struct got_error *
115 read_meta_file(char **content, const char *path_got, const char *name)
117 const struct got_error *err = NULL;
125 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
126 err = got_error_from_errno("asprintf");
131 fd = open(path, O_RDONLY | O_NOFOLLOW);
134 err = got_error_path(path, GOT_ERR_WORKTREE_META);
136 err = got_error_from_errno2("open", path);
139 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
140 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
141 : got_error_from_errno2("flock", path));
145 if (fstat(fd, &sb) != 0) {
146 err = got_error_from_errno2("fstat", path);
149 *content = calloc(1, sb.st_size);
150 if (*content == NULL) {
151 err = got_error_from_errno("calloc");
155 n = read(fd, *content, sb.st_size);
156 if (n != sb.st_size) {
157 err = (n == -1 ? got_error_from_errno2("read", path) :
158 got_error_path(path, GOT_ERR_WORKTREE_META));
161 if ((*content)[sb.st_size - 1] != '\n') {
162 err = got_error_path(path, GOT_ERR_WORKTREE_META);
165 (*content)[sb.st_size - 1] = '\0';
168 if (fd != -1 && close(fd) == -1 && err == NULL)
169 err = got_error_from_errno2("close", path_got);
178 static const struct got_error *
179 write_head_ref(const char *path_got, struct got_reference *head_ref)
181 const struct got_error *err = NULL;
184 if (got_ref_is_symbolic(head_ref)) {
185 refstr = got_ref_to_str(head_ref);
187 return got_error_from_errno("got_ref_to_str");
189 refstr = strdup(got_ref_get_name(head_ref));
191 return got_error_from_errno("strdup");
193 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
198 const struct got_error *
199 got_worktree_init(const char *path, struct got_reference *head_ref,
200 const char *prefix, struct got_repository *repo)
202 const struct got_error *err = NULL;
203 struct got_object_id *commit_id = NULL;
205 uint32_t uuid_status;
207 char *path_got = NULL;
208 char *formatstr = NULL;
209 char *absprefix = NULL;
210 char *basestr = NULL;
211 char *uuidstr = NULL;
213 if (strcmp(path, got_repo_get_path(repo)) == 0) {
214 err = got_error(GOT_ERR_WORKTREE_REPO);
218 err = got_ref_resolve(&commit_id, repo, head_ref);
221 err = got_object_get_type(&obj_type, repo, commit_id);
224 if (obj_type != GOT_OBJ_TYPE_COMMIT)
225 return got_error(GOT_ERR_OBJ_TYPE);
227 if (!got_path_is_absolute(prefix)) {
228 if (asprintf(&absprefix, "/%s", prefix) == -1)
229 return got_error_from_errno("asprintf");
232 /* Create top-level directory (may already exist). */
233 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
234 err = got_error_from_errno2("mkdir", path);
238 /* Create .got directory (may already exist). */
239 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
240 err = got_error_from_errno("asprintf");
243 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
244 err = got_error_from_errno2("mkdir", path_got);
248 /* Create an empty lock file. */
249 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
253 /* Create an empty file index. */
254 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
258 /* Write the HEAD reference. */
259 err = write_head_ref(path_got, head_ref);
263 /* Record our base commit. */
264 err = got_object_id_str(&basestr, commit_id);
267 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
271 /* Store path to repository. */
272 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
273 got_repo_get_path(repo));
277 /* Store in-repository path prefix. */
278 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
279 absprefix ? absprefix : prefix);
284 uuid_create(&uuid, &uuid_status);
285 if (uuid_status != uuid_s_ok) {
286 err = got_error_uuid(uuid_status);
289 uuid_to_string(&uuid, &uuidstr, &uuid_status);
290 if (uuid_status != uuid_s_ok) {
291 err = got_error_uuid(uuid_status);
294 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
298 /* Stamp work tree with format file. */
299 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
300 err = got_error_from_errno("asprintf");
303 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
317 static const struct got_error *
318 open_worktree(struct got_worktree **worktree, const char *path)
320 const struct got_error *err = NULL;
322 char *formatstr = NULL;
323 char *uuidstr = NULL;
324 char *path_lock = NULL;
325 char *base_commit_id_str = NULL;
326 int version, fd = -1;
328 struct got_repository *repo = NULL;
329 uint32_t uuid_status;
333 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
334 err = got_error_from_errno("asprintf");
339 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
340 err = got_error_from_errno("asprintf");
345 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
347 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
348 : got_error_from_errno2("open", path_lock));
352 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
356 version = strtonum(formatstr, 1, INT_MAX, &errstr);
358 err = got_error_msg(GOT_ERR_WORKTREE_META,
359 "could not parse work tree format version number");
362 if (version != GOT_WORKTREE_FORMAT_VERSION) {
363 err = got_error(GOT_ERR_WORKTREE_VERS);
367 *worktree = calloc(1, sizeof(**worktree));
368 if (*worktree == NULL) {
369 err = got_error_from_errno("calloc");
372 (*worktree)->lockfd = -1;
374 (*worktree)->root_path = strdup(path);
375 if ((*worktree)->root_path == NULL) {
376 err = got_error_from_errno("strdup");
379 err = read_meta_file(&(*worktree)->repo_path, path_got,
380 GOT_WORKTREE_REPOSITORY);
384 err = read_meta_file(&(*worktree)->path_prefix, path_got,
385 GOT_WORKTREE_PATH_PREFIX);
389 err = read_meta_file(&base_commit_id_str, path_got,
390 GOT_WORKTREE_BASE_COMMIT);
394 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
397 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
398 if (uuid_status != uuid_s_ok) {
399 err = got_error_uuid(uuid_status);
403 err = got_repo_open(&repo, (*worktree)->repo_path);
407 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
412 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
413 GOT_WORKTREE_HEAD_REF);
416 got_repo_close(repo);
419 free(base_commit_id_str);
425 if (*worktree != NULL)
426 got_worktree_close(*worktree);
429 (*worktree)->lockfd = fd;
434 const struct got_error *
435 got_worktree_open(struct got_worktree **worktree, const char *path)
437 const struct got_error *err = NULL;
440 err = open_worktree(worktree, path);
441 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
445 path = dirname(path);
447 return got_error_from_errno2("dirname", path);
448 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
450 return got_error(GOT_ERR_NOT_WORKTREE);
453 const struct got_error *
454 got_worktree_close(struct got_worktree *worktree)
456 const struct got_error *err = NULL;
457 free(worktree->root_path);
458 free(worktree->repo_path);
459 free(worktree->path_prefix);
460 free(worktree->base_commit_id);
461 free(worktree->head_ref_name);
462 if (worktree->lockfd != -1)
463 if (close(worktree->lockfd) != 0)
464 err = got_error_from_errno2("close",
465 got_worktree_get_root_path(worktree));
471 got_worktree_get_root_path(struct got_worktree *worktree)
473 return worktree->root_path;
477 got_worktree_get_repo_path(struct got_worktree *worktree)
479 return worktree->repo_path;
483 got_worktree_get_path_prefix(struct got_worktree *worktree)
485 return worktree->path_prefix;
488 const struct got_error *
489 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
490 const char *path_prefix)
492 char *absprefix = NULL;
494 if (!got_path_is_absolute(path_prefix)) {
495 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
496 return got_error_from_errno("asprintf");
498 *match = (strcmp(absprefix ? absprefix : path_prefix,
499 worktree->path_prefix) == 0);
505 got_worktree_get_head_ref_name(struct got_worktree *worktree)
507 return worktree->head_ref_name;
510 const struct got_error *
511 got_worktree_set_head_ref(struct got_worktree *worktree,
512 struct got_reference *head_ref)
514 const struct got_error *err = NULL;
515 char *path_got = NULL, *head_ref_name = NULL;
517 if (asprintf(&path_got, "%s/%s", worktree->root_path,
518 GOT_WORKTREE_GOT_DIR) == -1) {
519 err = got_error_from_errno("asprintf");
524 head_ref_name = strdup(got_ref_get_name(head_ref));
525 if (head_ref_name == NULL) {
526 err = got_error_from_errno("strdup");
530 err = write_head_ref(path_got, head_ref);
534 free(worktree->head_ref_name);
535 worktree->head_ref_name = head_ref_name;
543 struct got_object_id *
544 got_worktree_get_base_commit_id(struct got_worktree *worktree)
546 return worktree->base_commit_id;
549 const struct got_error *
550 got_worktree_set_base_commit_id(struct got_worktree *worktree,
551 struct got_repository *repo, struct got_object_id *commit_id)
553 const struct got_error *err;
554 struct got_object *obj = NULL;
556 char *path_got = NULL;
558 if (asprintf(&path_got, "%s/%s", worktree->root_path,
559 GOT_WORKTREE_GOT_DIR) == -1) {
560 err = got_error_from_errno("asprintf");
565 err = got_object_open(&obj, repo, commit_id);
569 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
570 err = got_error(GOT_ERR_OBJ_TYPE);
574 /* Record our base commit. */
575 err = got_object_id_str(&id_str, commit_id);
578 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
582 free(worktree->base_commit_id);
583 worktree->base_commit_id = got_object_id_dup(commit_id);
584 if (worktree->base_commit_id == NULL) {
585 err = got_error_from_errno("got_object_id_dup");
590 got_object_close(obj);
596 static const struct got_error *
597 lock_worktree(struct got_worktree *worktree, int operation)
599 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
600 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
601 : got_error_from_errno2("flock",
602 got_worktree_get_root_path(worktree)));
606 static const struct got_error *
607 add_dir_on_disk(struct got_worktree *worktree, const char *path)
609 const struct got_error *err = NULL;
612 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
613 return got_error_from_errno("asprintf");
615 err = got_path_mkdir(abspath);
616 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
619 if (lstat(abspath, &sb) == -1) {
620 err = got_error_from_errno2("lstat", abspath);
621 } else if (!S_ISDIR(sb.st_mode)) {
622 /* TODO directory is obstructed; do something */
623 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
630 static const struct got_error *
631 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
633 const struct got_error *err = NULL;
636 size_t flen1 = 0, flen2 = 0;
641 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
642 if (flen1 == 0 && ferror(f1)) {
643 err = got_error_from_errno("fread");
646 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
647 if (flen2 == 0 && ferror(f2)) {
648 err = got_error_from_errno("fread");
655 } else if (flen2 == 0) {
659 } else if (flen1 == flen2) {
660 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
673 static const struct got_error *
674 check_files_equal(int *same, const char *f1_path, const char *f2_path)
676 const struct got_error *err = NULL;
679 FILE *f1 = NULL, *f2 = NULL;
683 if (lstat(f1_path, &sb) != 0) {
684 err = got_error_from_errno2("lstat", f1_path);
689 if (lstat(f2_path, &sb) != 0) {
690 err = got_error_from_errno2("lstat", f2_path);
695 if (size1 != size2) {
700 f1 = fopen(f1_path, "r");
702 return got_error_from_errno2("open", f1_path);
704 f2 = fopen(f2_path, "r");
706 err = got_error_from_errno2("open", f2_path);
710 err = check_file_contents_equal(same, f1, f2);
712 if (f1 && fclose(f1) != 0 && err == NULL)
713 err = got_error_from_errno("fclose");
714 if (f2 && fclose(f2) != 0 && err == NULL)
715 err = got_error_from_errno("fclose");
721 * Perform a 3-way merge where blob_orig acts as the common ancestor,
722 * blob_deriv acts as the first derived version, and the file on disk
723 * acts as the second derived version.
725 static const struct got_error *
726 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
727 struct got_blob_object *blob_orig, const char *ondisk_path,
728 const char *path, uint16_t st_mode, struct got_blob_object *blob_deriv,
729 struct got_object_id *deriv_base_commit_id,
730 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
733 const struct got_error *err = NULL;
735 FILE *f_deriv = NULL, *f_orig = NULL;
736 char *blob_deriv_path = NULL, *blob_orig_path = NULL;
737 char *merged_path = NULL, *base_path = NULL;
739 char *label_deriv = NULL;
743 *local_changes_subsumed = 0;
745 parent = dirname(ondisk_path);
747 return got_error_from_errno2("dirname", ondisk_path);
749 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
750 return got_error_from_errno("asprintf");
752 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
757 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
758 err = got_error_from_errno("asprintf");
763 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
766 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
772 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
773 err = got_error_from_errno("asprintf");
778 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
782 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
788 * If the file has no blob, this is an "add vs add" conflict,
789 * and we simply use an empty ancestor file to make both files
790 * appear in the merged result in their entirety.
794 err = got_object_id_str(&id_str, deriv_base_commit_id);
797 if (asprintf(&label_deriv, "commit %s", id_str) == -1) {
798 err = got_error_from_errno("asprintf");
802 err = got_merge_diff3(&overlapcnt, merged_fd, blob_deriv_path,
803 blob_orig_path, ondisk_path, label_deriv, path);
807 err = (*progress_cb)(progress_arg,
808 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
812 if (fsync(merged_fd) != 0) {
813 err = got_error_from_errno("fsync");
817 /* Check if a clean merge has subsumed all local changes. */
818 if (overlapcnt == 0) {
819 err = check_files_equal(local_changes_subsumed, blob_deriv_path,
825 if (chmod(merged_path, st_mode) != 0) {
826 err = got_error_from_errno2("chmod", merged_path);
830 if (rename(merged_path, ondisk_path) != 0) {
831 err = got_error_from_errno3("rename", merged_path,
838 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
839 err = got_error_from_errno("close");
840 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
841 err = got_error_from_errno("fclose");
842 if (f_orig && fclose(f_orig) != 0 && err == NULL)
843 err = got_error_from_errno("fclose");
846 if (blob_deriv_path) {
847 unlink(blob_deriv_path);
848 free(blob_deriv_path);
850 if (blob_orig_path) {
851 unlink(blob_orig_path);
852 free(blob_orig_path);
859 static const struct got_error *
860 update_blob_fileindex_entry(struct got_worktree *worktree,
861 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
862 const char *ondisk_path, const char *path, struct got_blob_object *blob,
863 int update_timestamps)
865 const struct got_error *err = NULL;
868 ie = got_fileindex_entry_get(fileindex, path);
870 err = got_fileindex_entry_update(ie, ondisk_path,
871 blob->id.sha1, worktree->base_commit_id->sha1,
874 struct got_fileindex_entry *new_ie;
875 err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
876 path, blob->id.sha1, worktree->base_commit_id->sha1);
878 err = got_fileindex_entry_add(fileindex, new_ie);
883 static const struct got_error *
884 install_blob(struct got_worktree *worktree, const char *ondisk_path,
885 const char *path, uint16_t te_mode, uint16_t st_mode,
886 struct got_blob_object *blob, int restoring_missing_file,
887 int reverting_versioned_file, struct got_repository *repo,
888 got_worktree_checkout_cb progress_cb, void *progress_arg)
890 const struct got_error *err = NULL;
894 char *tmppath = NULL;
896 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
897 GOT_DEFAULT_FILE_MODE);
899 if (errno == ENOENT) {
900 char *parent = dirname(path);
902 return got_error_from_errno2("dirname", path);
903 err = add_dir_on_disk(worktree, parent);
906 fd = open(ondisk_path,
907 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
908 GOT_DEFAULT_FILE_MODE);
910 return got_error_from_errno2("open",
912 } else if (errno == EEXIST) {
913 if (!S_ISREG(st_mode)) {
914 /* TODO file is obstructed; do something */
915 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
918 err = got_opentemp_named_fd(&tmppath, &fd,
925 return got_error_from_errno2("open", ondisk_path);
928 if (restoring_missing_file)
929 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
930 else if (reverting_versioned_file)
931 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
933 err = (*progress_cb)(progress_arg,
934 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
938 hdrlen = got_object_blob_get_hdrlen(blob);
940 const uint8_t *buf = got_object_blob_get_read_buf(blob);
941 err = got_object_blob_read_block(&len, blob);
945 /* Skip blob object header first time around. */
946 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
948 err = got_error_from_errno("write");
950 } else if (outlen != len - hdrlen) {
951 err = got_error(GOT_ERR_IO);
958 if (fsync(fd) != 0) {
959 err = got_error_from_errno("fsync");
964 if (rename(tmppath, ondisk_path) != 0) {
965 err = got_error_from_errno3("rename", tmppath,
972 if (te_mode & S_IXUSR) {
973 if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
974 err = got_error_from_errno2("chmod", ondisk_path);
978 if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
979 err = got_error_from_errno2("chmod", ondisk_path);
985 if (fd != -1 && close(fd) != 0 && err == NULL)
986 err = got_error_from_errno("close");
991 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
992 static const struct got_error *
993 get_modified_file_content_status(unsigned char *status, FILE *f)
995 const struct got_error *err = NULL;
996 const char *markers[3] = {
997 GOT_DIFF_CONFLICT_MARKER_BEGIN,
998 GOT_DIFF_CONFLICT_MARKER_SEP,
999 GOT_DIFF_CONFLICT_MARKER_END
1004 const char delim[3] = {'\0', '\0', '\0'};
1006 while (*status == GOT_STATUS_MODIFY) {
1007 line = fparseln(f, &len, NULL, delim, 0);
1011 err = got_ferror(f, GOT_ERR_IO);
1015 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1016 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1018 *status = GOT_STATUS_CONFLICT;
1028 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1030 return !(ie->ctime_sec == sb->st_ctime &&
1031 ie->ctime_nsec == sb->st_ctimensec &&
1032 ie->mtime_sec == sb->st_mtime &&
1033 ie->mtime_nsec == sb->st_mtimensec &&
1034 ie->size == (sb->st_size & 0xffffffff));
1037 static const struct got_error *
1038 get_file_status(unsigned char *status, struct stat *sb,
1039 struct got_fileindex_entry *ie, const char *abspath,
1040 struct got_repository *repo)
1042 const struct got_error *err = NULL;
1043 struct got_object_id id;
1047 struct got_blob_object *blob = NULL;
1050 *status = GOT_STATUS_NO_CHANGE;
1052 if (lstat(abspath, sb) == -1) {
1053 if (errno == ENOENT) {
1054 if (got_fileindex_entry_has_file_on_disk(ie))
1055 *status = GOT_STATUS_MISSING;
1057 *status = GOT_STATUS_DELETE;
1060 return got_error_from_errno2("lstat", abspath);
1063 if (!S_ISREG(sb->st_mode)) {
1064 *status = GOT_STATUS_OBSTRUCTED;
1068 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1069 *status = GOT_STATUS_DELETE;
1071 } else if (!got_fileindex_entry_has_blob(ie)) {
1072 *status = GOT_STATUS_ADD;
1076 if (!stat_info_differs(ie, sb))
1079 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1080 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1084 f = fopen(abspath, "r");
1086 err = got_error_from_errno2("fopen", abspath);
1089 hdrlen = got_object_blob_get_hdrlen(blob);
1091 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1092 err = got_object_blob_read_block(&blen, blob);
1095 /* Skip length of blob object header first time around. */
1096 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1097 if (flen == 0 && ferror(f)) {
1098 err = got_error_from_errno("fread");
1103 *status = GOT_STATUS_MODIFY;
1105 } else if (flen == 0) {
1107 *status = GOT_STATUS_MODIFY;
1109 } else if (blen - hdrlen == flen) {
1110 /* Skip blob object header first time around. */
1111 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1112 *status = GOT_STATUS_MODIFY;
1116 *status = GOT_STATUS_MODIFY;
1122 if (*status == GOT_STATUS_MODIFY) {
1124 err = get_modified_file_content_status(status, f);
1128 got_object_blob_close(blob);
1135 * Update timestamps in the file index if a file is unmodified and
1136 * we had to run a full content comparison to find out.
1138 static const struct got_error *
1139 sync_timestamps(char *ondisk_path, unsigned char status,
1140 struct got_fileindex_entry *ie, struct stat *sb)
1142 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1143 return got_fileindex_entry_update(ie, ondisk_path,
1144 ie->blob_sha1, ie->commit_sha1, 1);
1149 static const struct got_error *
1150 update_blob(struct got_worktree *worktree,
1151 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1152 struct got_tree_entry *te, const char *path,
1153 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1156 const struct got_error *err = NULL;
1157 struct got_blob_object *blob = NULL;
1159 unsigned char status = GOT_STATUS_NO_CHANGE;
1162 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1163 return got_error_from_errno("asprintf");
1166 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1169 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1170 sb.st_mode = got_fileindex_perms_to_st(ie);
1172 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1174 if (status == GOT_STATUS_OBSTRUCTED) {
1175 err = (*progress_cb)(progress_arg, status, path);
1179 if (ie && status != GOT_STATUS_MISSING) {
1180 if (got_fileindex_entry_has_commit(ie) &&
1181 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1182 SHA1_DIGEST_LENGTH) == 0) {
1183 err = sync_timestamps(ondisk_path, status, ie, &sb);
1186 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1190 if (got_fileindex_entry_has_blob(ie) &&
1191 memcmp(ie->blob_sha1, te->id->sha1,
1192 SHA1_DIGEST_LENGTH) == 0) {
1193 err = sync_timestamps(ondisk_path, status, ie, &sb);
1198 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1202 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1203 int update_timestamps;
1204 struct got_blob_object *blob2 = NULL;
1205 if (got_fileindex_entry_has_blob(ie)) {
1206 struct got_object_id id2;
1207 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1208 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1212 err = merge_blob(&update_timestamps, worktree, blob2,
1213 ondisk_path, path, sb.st_mode, blob,
1214 worktree->base_commit_id, repo,
1215 progress_cb, progress_arg);
1217 got_object_blob_close(blob2);
1219 * Do not update timestamps of files with local changes.
1220 * Otherwise, a future status walk would treat them as
1221 * unmodified files again.
1223 err = got_fileindex_entry_update(ie, ondisk_path,
1224 blob->id.sha1, worktree->base_commit_id->sha1,
1226 } else if (status == GOT_STATUS_DELETE) {
1227 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1230 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1231 ondisk_path, path, blob, 0);
1235 err = install_blob(worktree, ondisk_path, path, te->mode,
1236 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1237 repo, progress_cb, progress_arg);
1240 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1241 ondisk_path, path, blob, 1);
1245 got_object_blob_close(blob);
1251 static const struct got_error *
1252 remove_ondisk_file(const char *root_path, const char *path)
1254 const struct got_error *err = NULL;
1255 char *ondisk_path = NULL;
1257 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1258 return got_error_from_errno("asprintf");
1260 if (unlink(ondisk_path) == -1) {
1261 if (errno != ENOENT)
1262 err = got_error_from_errno2("unlink", ondisk_path);
1264 char *parent = dirname(ondisk_path);
1265 while (parent && strcmp(parent, root_path) != 0) {
1266 if (rmdir(parent) == -1) {
1267 if (errno != ENOTEMPTY)
1268 err = got_error_from_errno2("rmdir",
1272 parent = dirname(parent);
1279 static const struct got_error *
1280 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1281 struct got_fileindex_entry *ie, struct got_repository *repo,
1282 got_worktree_checkout_cb progress_cb, void *progress_arg)
1284 const struct got_error *err = NULL;
1285 unsigned char status;
1289 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1291 return got_error_from_errno("asprintf");
1293 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1297 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1298 status == GOT_STATUS_ADD) {
1299 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1303 * Preserve the working file and change the deleted blob's
1304 * entry into a schedule-add entry.
1306 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1311 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1314 if (status == GOT_STATUS_NO_CHANGE) {
1315 err = remove_ondisk_file(worktree->root_path, ie->path);
1319 got_fileindex_entry_remove(fileindex, ie);
1325 struct diff_cb_arg {
1326 struct got_fileindex *fileindex;
1327 struct got_worktree *worktree;
1328 struct got_repository *repo;
1329 got_worktree_checkout_cb progress_cb;
1331 got_worktree_cancel_cb cancel_cb;
1335 static const struct got_error *
1336 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1337 struct got_tree_entry *te, const char *parent_path)
1339 struct diff_cb_arg *a = arg;
1341 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1342 return got_error(GOT_ERR_CANCELLED);
1344 return update_blob(a->worktree, a->fileindex, ie, te,
1345 ie->path, a->repo, a->progress_cb, a->progress_arg);
1348 static const struct got_error *
1349 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1351 struct diff_cb_arg *a = arg;
1353 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1354 return got_error(GOT_ERR_CANCELLED);
1356 return delete_blob(a->worktree, a->fileindex, ie,
1357 a->repo, a->progress_cb, a->progress_arg);
1360 static const struct got_error *
1361 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1363 struct diff_cb_arg *a = arg;
1364 const struct got_error *err;
1367 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1368 return got_error(GOT_ERR_CANCELLED);
1370 if (asprintf(&path, "%s%s%s", parent_path,
1371 parent_path[0] ? "/" : "", te->name)
1373 return got_error_from_errno("asprintf");
1375 if (S_ISDIR(te->mode))
1376 err = add_dir_on_disk(a->worktree, path);
1378 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1379 a->repo, a->progress_cb, a->progress_arg);
1385 static const struct got_error *
1386 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1388 const struct got_error *err = NULL;
1389 char *uuidstr = NULL;
1390 uint32_t uuid_status;
1394 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1395 if (uuid_status != uuid_s_ok)
1396 return got_error_uuid(uuid_status);
1398 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1400 err = got_error_from_errno("asprintf");
1407 const struct got_error *
1408 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1410 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1413 static const struct got_error *
1414 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1416 return get_ref_name(refname, worktree,
1417 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1420 static const struct got_error *
1421 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1423 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1426 static const struct got_error *
1427 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1429 return get_ref_name(refname, worktree,
1430 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1433 static const struct got_error *
1434 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1436 return get_ref_name(refname, worktree,
1437 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1440 static const struct got_error *
1441 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1443 return get_ref_name(refname, worktree,
1444 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1447 static const struct got_error *
1448 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1450 return get_ref_name(refname, worktree,
1451 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1454 static const struct got_error *
1455 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1457 return get_ref_name(refname, worktree,
1458 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1461 static const struct got_error *
1462 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1464 return get_ref_name(refname, worktree,
1465 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1468 const struct got_error *
1469 got_worktree_get_histedit_script_path(char **path,
1470 struct got_worktree *worktree)
1472 if (asprintf(path, "%s/%s/%s", worktree->root_path,
1473 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
1475 return got_error_from_errno("asprintf");
1481 * Prevent Git's garbage collector from deleting our base commit by
1482 * setting a reference to our base commit's ID.
1484 static const struct got_error *
1485 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1487 const struct got_error *err = NULL;
1488 struct got_reference *ref = NULL;
1491 err = got_worktree_get_base_ref_name(&refname, worktree);
1495 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1499 err = got_ref_write(ref, repo);
1507 static const struct got_error *
1508 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
1510 const struct got_error *err = NULL;
1512 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1513 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1514 err = got_error_from_errno("asprintf");
1515 *fileindex_path = NULL;
1521 static const struct got_error *
1522 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1523 struct got_worktree *worktree)
1525 const struct got_error *err = NULL;
1528 *fileindex_path = NULL;
1529 *fileindex = got_fileindex_alloc();
1530 if (*fileindex == NULL)
1531 return got_error_from_errno("got_fileindex_alloc");
1533 err = get_fileindex_path(fileindex_path, worktree);
1537 index = fopen(*fileindex_path, "rb");
1538 if (index == NULL) {
1539 if (errno != ENOENT)
1540 err = got_error_from_errno2("fopen", *fileindex_path);
1542 err = got_fileindex_read(*fileindex, index);
1543 if (fclose(index) != 0 && err == NULL)
1544 err = got_error_from_errno("fclose");
1548 free(*fileindex_path);
1549 *fileindex_path = NULL;
1550 got_fileindex_free(*fileindex);
1556 struct bump_base_commit_id_arg {
1557 struct got_object_id *base_commit_id;
1560 const char *entry_name;
1561 got_worktree_checkout_cb progress_cb;
1565 /* Bump base commit ID of all files within an updated part of the work tree. */
1566 static const struct got_error *
1567 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1569 const struct got_error *err;
1570 struct bump_base_commit_id_arg *a = arg;
1572 if (a->entry_name) {
1573 if (strcmp(ie->path, a->path) != 0)
1575 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1578 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1579 SHA1_DIGEST_LENGTH) == 0)
1582 if (a->progress_cb) {
1583 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1588 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1592 static const struct got_error *
1593 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1595 const struct got_error *err = NULL;
1596 char *new_fileindex_path = NULL;
1597 FILE *new_index = NULL;
1599 err = got_opentemp_named(&new_fileindex_path, &new_index,
1604 err = got_fileindex_write(fileindex, new_index);
1608 if (rename(new_fileindex_path, fileindex_path) != 0) {
1609 err = got_error_from_errno3("rename", new_fileindex_path,
1611 unlink(new_fileindex_path);
1616 free(new_fileindex_path);
1620 static const struct got_error *
1621 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1622 struct got_object_id **tree_id, const char *wt_relpath,
1623 struct got_worktree *worktree, struct got_repository *repo)
1625 const struct got_error *err = NULL;
1626 struct got_object_id *id = NULL;
1627 char *in_repo_path = NULL;
1628 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1630 *entry_type = GOT_OBJ_TYPE_ANY;
1631 *tree_relpath = NULL;
1634 if (wt_relpath[0] == '\0') {
1635 /* Check out all files within the work tree. */
1636 *entry_type = GOT_OBJ_TYPE_TREE;
1637 *tree_relpath = strdup("");
1638 if (*tree_relpath == NULL) {
1639 err = got_error_from_errno("strdup");
1642 err = got_object_id_by_path(tree_id, repo,
1643 worktree->base_commit_id, worktree->path_prefix);
1649 /* Check out a subset of files in the work tree. */
1651 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1652 is_root_wt ? "" : "/", wt_relpath) == -1) {
1653 err = got_error_from_errno("asprintf");
1657 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
1663 in_repo_path = NULL;
1665 err = got_object_get_type(entry_type, repo, id);
1669 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
1670 /* Check out a single file. */
1671 if (strchr(wt_relpath, '/') == NULL) {
1672 /* Check out a single file in work tree's root dir. */
1673 in_repo_path = strdup(worktree->path_prefix);
1674 if (in_repo_path == NULL) {
1675 err = got_error_from_errno("strdup");
1678 *tree_relpath = strdup("");
1679 if (*tree_relpath == NULL) {
1680 err = got_error_from_errno("strdup");
1684 /* Check out a single file in a subdirectory. */
1685 err = got_path_dirname(tree_relpath, wt_relpath);
1688 if (asprintf(&in_repo_path, "%s%s%s",
1689 worktree->path_prefix, is_root_wt ? "" : "/",
1690 *tree_relpath) == -1) {
1691 err = got_error_from_errno("asprintf");
1695 err = got_object_id_by_path(tree_id, repo,
1696 worktree->base_commit_id, in_repo_path);
1698 /* Check out all files within a subdirectory. */
1699 *tree_id = got_object_id_dup(id);
1700 if (*tree_id == NULL) {
1701 err = got_error_from_errno("got_object_id_dup");
1704 *tree_relpath = strdup(wt_relpath);
1705 if (*tree_relpath == NULL) {
1706 err = got_error_from_errno("strdup");
1714 *entry_type = GOT_OBJ_TYPE_ANY;
1715 free(*tree_relpath);
1716 *tree_relpath = NULL;
1723 static const struct got_error *
1724 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1725 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
1726 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1727 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1729 const struct got_error *err = NULL;
1730 struct got_commit_object *commit = NULL;
1731 struct got_tree_object *tree = NULL;
1732 struct got_fileindex_diff_tree_cb diff_cb;
1733 struct diff_cb_arg arg;
1735 err = ref_base_commit(worktree, repo);
1739 err = got_object_open_as_commit(&commit, repo,
1740 worktree->base_commit_id);
1744 err = got_object_open_as_tree(&tree, repo, tree_id);
1749 got_object_tree_find_entry(tree, entry_name) == NULL) {
1750 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1754 diff_cb.diff_old_new = diff_old_new;
1755 diff_cb.diff_old = diff_old;
1756 diff_cb.diff_new = diff_new;
1757 arg.fileindex = fileindex;
1758 arg.worktree = worktree;
1760 arg.progress_cb = progress_cb;
1761 arg.progress_arg = progress_arg;
1762 arg.cancel_cb = cancel_cb;
1763 arg.cancel_arg = cancel_arg;
1764 err = got_fileindex_diff_tree(fileindex, tree, relpath,
1765 entry_name, repo, &diff_cb, &arg);
1768 got_object_tree_close(tree);
1770 got_object_commit_close(commit);
1774 const struct got_error *
1775 got_worktree_checkout_files(struct got_worktree *worktree,
1776 struct got_pathlist_head *paths, struct got_repository *repo,
1777 got_worktree_checkout_cb progress_cb, void *progress_arg,
1778 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1780 const struct got_error *err = NULL, *sync_err, *unlockerr;
1781 struct got_commit_object *commit = NULL;
1782 struct got_tree_object *tree = NULL;
1783 struct got_fileindex *fileindex = NULL;
1784 char *fileindex_path = NULL;
1785 struct got_pathlist_entry *pe;
1786 struct tree_path_data {
1787 SIMPLEQ_ENTRY(tree_path_data) entry;
1788 struct got_object_id *tree_id;
1793 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
1795 SIMPLEQ_INIT(&tree_paths);
1797 err = lock_worktree(worktree, LOCK_EX);
1801 /* Map all specified paths to in-repository trees. */
1802 TAILQ_FOREACH(pe, paths, entry) {
1803 tpd = malloc(sizeof(*tpd));
1805 err = got_error_from_errno("malloc");
1809 err = find_tree_entry_for_checkout(&tpd->entry_type,
1810 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
1816 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
1817 err = got_path_basename(&tpd->entry_name, pe->path);
1825 tpd->entry_name = NULL;
1827 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
1831 * Read the file index.
1832 * Checking out files is supposed to be an idempotent operation.
1833 * If the on-disk file index is incomplete we will try to complete it.
1835 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1839 tpd = SIMPLEQ_FIRST(&tree_paths);
1840 TAILQ_FOREACH(pe, paths, entry) {
1841 struct bump_base_commit_id_arg bbc_arg;
1843 err = checkout_files(worktree, fileindex, tpd->relpath,
1844 tpd->tree_id, tpd->entry_name, repo,
1845 progress_cb, progress_arg, cancel_cb, cancel_arg);
1849 bbc_arg.base_commit_id = worktree->base_commit_id;
1850 bbc_arg.entry_name = tpd->entry_name;
1851 bbc_arg.path = pe->path;
1852 bbc_arg.path_len = strlen(pe->path);
1853 bbc_arg.progress_cb = progress_cb;
1854 bbc_arg.progress_arg = progress_arg;
1855 err = got_fileindex_for_each_entry_safe(fileindex,
1856 bump_base_commit_id, &bbc_arg);
1860 tpd = SIMPLEQ_NEXT(tpd, entry);
1862 sync_err = sync_fileindex(fileindex, fileindex_path);
1863 if (sync_err && err == NULL)
1866 free(fileindex_path);
1868 got_object_tree_close(tree);
1870 got_object_commit_close(commit);
1872 got_fileindex_free(fileindex);
1873 while (!SIMPLEQ_EMPTY(&tree_paths)) {
1874 tpd = SIMPLEQ_FIRST(&tree_paths);
1875 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
1880 unlockerr = lock_worktree(worktree, LOCK_SH);
1881 if (unlockerr && err == NULL)
1886 struct merge_file_cb_arg {
1887 struct got_worktree *worktree;
1888 struct got_fileindex *fileindex;
1889 got_worktree_checkout_cb progress_cb;
1891 got_worktree_cancel_cb cancel_cb;
1893 struct got_object_id *commit_id2;
1896 static const struct got_error *
1897 merge_file_cb(void *arg, struct got_blob_object *blob1,
1898 struct got_blob_object *blob2, struct got_object_id *id1,
1899 struct got_object_id *id2, const char *path1, const char *path2,
1900 struct got_repository *repo)
1902 static const struct got_error *err = NULL;
1903 struct merge_file_cb_arg *a = arg;
1904 struct got_fileindex_entry *ie;
1905 char *ondisk_path = NULL;
1907 unsigned char status;
1908 int local_changes_subsumed;
1910 if (blob1 && blob2) {
1911 ie = got_fileindex_entry_get(a->fileindex, path2);
1913 return (*a->progress_cb)(a->progress_arg,
1914 GOT_STATUS_MISSING, path2);
1916 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1918 return got_error_from_errno("asprintf");
1920 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1924 if (status == GOT_STATUS_DELETE) {
1925 err = (*a->progress_cb)(a->progress_arg,
1926 GOT_STATUS_MERGE, path2);
1929 if (status != GOT_STATUS_NO_CHANGE &&
1930 status != GOT_STATUS_MODIFY &&
1931 status != GOT_STATUS_CONFLICT &&
1932 status != GOT_STATUS_ADD) {
1933 err = (*a->progress_cb)(a->progress_arg, status, path2);
1937 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
1938 ondisk_path, path2, sb.st_mode, blob2, a->commit_id2, repo,
1939 a->progress_cb, a->progress_arg);
1941 ie = got_fileindex_entry_get(a->fileindex, path1);
1943 return (*a->progress_cb)(a->progress_arg,
1944 GOT_STATUS_MISSING, path2);
1946 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1948 return got_error_from_errno("asprintf");
1950 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1955 case GOT_STATUS_NO_CHANGE:
1956 err = (*a->progress_cb)(a->progress_arg,
1957 GOT_STATUS_DELETE, path1);
1960 err = remove_ondisk_file(a->worktree->root_path, path1);
1964 got_fileindex_entry_mark_deleted_from_disk(ie);
1966 case GOT_STATUS_DELETE:
1967 case GOT_STATUS_MISSING:
1968 err = (*a->progress_cb)(a->progress_arg,
1969 GOT_STATUS_DELETE, path1);
1973 got_fileindex_entry_mark_deleted_from_disk(ie);
1975 case GOT_STATUS_ADD:
1976 case GOT_STATUS_MODIFY:
1977 case GOT_STATUS_CONFLICT:
1978 err = (*a->progress_cb)(a->progress_arg,
1979 GOT_STATUS_CANNOT_DELETE, path1);
1983 case GOT_STATUS_OBSTRUCTED:
1984 err = (*a->progress_cb)(a->progress_arg, status, path1);
1992 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1994 return got_error_from_errno("asprintf");
1995 ie = got_fileindex_entry_get(a->fileindex, path2);
1997 err = get_file_status(&status, &sb, ie, ondisk_path,
2001 if (status != GOT_STATUS_NO_CHANGE &&
2002 status != GOT_STATUS_MODIFY &&
2003 status != GOT_STATUS_CONFLICT &&
2004 status != GOT_STATUS_ADD) {
2005 err = (*a->progress_cb)(a->progress_arg,
2009 err = merge_blob(&local_changes_subsumed, a->worktree,
2010 NULL, ondisk_path, path2, sb.st_mode, blob2,
2011 a->commit_id2, repo,
2012 a->progress_cb, a->progress_arg);
2013 if (status == GOT_STATUS_DELETE) {
2014 err = update_blob_fileindex_entry(a->worktree,
2015 a->fileindex, ie, ondisk_path, ie->path,
2021 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2022 err = install_blob(a->worktree, ondisk_path, path2,
2023 /* XXX get this from parent tree! */
2024 GOT_DEFAULT_FILE_MODE,
2025 sb.st_mode, blob2, 0, 0, repo,
2026 a->progress_cb, a->progress_arg);
2029 err = got_fileindex_entry_alloc(&ie,
2030 ondisk_path, path2, NULL, NULL);
2033 err = got_fileindex_entry_add(a->fileindex, ie);
2035 got_fileindex_entry_free(ie);
2045 struct check_merge_ok_arg {
2046 struct got_worktree *worktree;
2047 struct got_repository *repo;
2050 static const struct got_error *
2051 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2053 const struct got_error *err = NULL;
2054 struct check_merge_ok_arg *a = arg;
2055 unsigned char status;
2059 /* Reject merges into a work tree with mixed base commits. */
2060 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2061 SHA1_DIGEST_LENGTH))
2062 return got_error(GOT_ERR_MIXED_COMMITS);
2064 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2066 return got_error_from_errno("asprintf");
2068 /* Reject merges into a work tree with conflicted files. */
2069 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
2072 if (status == GOT_STATUS_CONFLICT)
2073 return got_error(GOT_ERR_CONFLICTS);
2078 static const struct got_error *
2079 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2080 const char *fileindex_path, struct got_object_id *commit_id1,
2081 struct got_object_id *commit_id2, struct got_repository *repo,
2082 got_worktree_checkout_cb progress_cb, void *progress_arg,
2083 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2085 const struct got_error *err = NULL, *sync_err;
2086 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2087 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2088 struct merge_file_cb_arg arg;
2091 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2092 worktree->path_prefix);
2096 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2101 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2102 worktree->path_prefix);
2106 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2110 arg.worktree = worktree;
2111 arg.fileindex = fileindex;
2112 arg.progress_cb = progress_cb;
2113 arg.progress_arg = progress_arg;
2114 arg.cancel_cb = cancel_cb;
2115 arg.cancel_arg = cancel_arg;
2116 arg.commit_id2 = commit_id2;
2117 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2118 sync_err = sync_fileindex(fileindex, fileindex_path);
2119 if (sync_err && err == NULL)
2123 got_object_tree_close(tree1);
2125 got_object_tree_close(tree2);
2129 const struct got_error *
2130 got_worktree_merge_files(struct got_worktree *worktree,
2131 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2132 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2133 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2135 const struct got_error *err, *unlockerr;
2136 char *fileindex_path = NULL;
2137 struct got_fileindex *fileindex = NULL;
2138 struct check_merge_ok_arg mok_arg;
2140 err = lock_worktree(worktree, LOCK_EX);
2144 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2148 mok_arg.worktree = worktree;
2149 mok_arg.repo = repo;
2150 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2155 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2156 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2159 got_fileindex_free(fileindex);
2160 free(fileindex_path);
2161 unlockerr = lock_worktree(worktree, LOCK_SH);
2162 if (unlockerr && err == NULL)
2167 struct diff_dir_cb_arg {
2168 struct got_fileindex *fileindex;
2169 struct got_worktree *worktree;
2170 const char *status_path;
2171 size_t status_path_len;
2172 struct got_repository *repo;
2173 got_worktree_status_cb status_cb;
2175 got_worktree_cancel_cb cancel_cb;
2179 static const struct got_error *
2180 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2181 got_worktree_status_cb status_cb, void *status_arg,
2182 struct got_repository *repo)
2184 const struct got_error *err = NULL;
2185 unsigned char status = GOT_STATUS_NO_CHANGE;
2187 struct got_object_id blob_id, commit_id;
2189 err = get_file_status(&status, &sb, ie, abspath, repo);
2190 if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
2191 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2192 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2193 err = (*status_cb)(status_arg, status, ie->path, &blob_id,
2199 static const struct got_error *
2200 status_old_new(void *arg, struct got_fileindex_entry *ie,
2201 struct dirent *de, const char *parent_path)
2203 const struct got_error *err = NULL;
2204 struct diff_dir_cb_arg *a = arg;
2207 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2208 return got_error(GOT_ERR_CANCELLED);
2210 if (got_path_cmp(parent_path, a->status_path,
2211 strlen(parent_path), a->status_path_len) != 0 &&
2212 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2215 if (parent_path[0]) {
2216 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2217 parent_path, de->d_name) == -1)
2218 return got_error_from_errno("asprintf");
2220 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2222 return got_error_from_errno("asprintf");
2225 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
2231 static const struct got_error *
2232 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2234 struct diff_dir_cb_arg *a = arg;
2235 struct got_object_id blob_id, commit_id;
2236 unsigned char status;
2238 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2239 return got_error(GOT_ERR_CANCELLED);
2241 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2244 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2245 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2246 if (got_fileindex_entry_has_file_on_disk(ie))
2247 status = GOT_STATUS_MISSING;
2249 status = GOT_STATUS_DELETE;
2250 return (*a->status_cb)(a->status_arg, status, ie->path, &blob_id,
2254 static const struct got_error *
2255 status_new(void *arg, struct dirent *de, const char *parent_path)
2257 const struct got_error *err = NULL;
2258 struct diff_dir_cb_arg *a = arg;
2261 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2262 return got_error(GOT_ERR_CANCELLED);
2264 if (de->d_type == DT_DIR)
2267 /* XXX ignore symlinks for now */
2268 if (de->d_type == DT_LNK)
2271 if (parent_path[0]) {
2272 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2273 return got_error_from_errno("asprintf");
2278 if (got_path_is_child(path, a->status_path, a->status_path_len))
2279 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2286 static const struct got_error *
2287 report_single_file_status(const char *path, const char *ondisk_path,
2288 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2289 void *status_arg, struct got_repository *repo)
2291 struct got_fileindex_entry *ie;
2294 ie = got_fileindex_entry_get(fileindex, path);
2296 return report_file_status(ie, ondisk_path, status_cb,
2299 if (lstat(ondisk_path, &sb) == -1) {
2300 if (errno != ENOENT)
2301 return got_error_from_errno2("lstat", ondisk_path);
2305 if (S_ISREG(sb.st_mode))
2306 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED, path,
2312 static const struct got_error *
2313 worktree_status(struct got_worktree *worktree, const char *path,
2314 struct got_fileindex *fileindex, struct got_repository *repo,
2315 got_worktree_status_cb status_cb, void *status_arg,
2316 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2318 const struct got_error *err = NULL;
2319 DIR *workdir = NULL;
2320 struct got_fileindex_diff_dir_cb fdiff_cb;
2321 struct diff_dir_cb_arg arg;
2322 char *ondisk_path = NULL;
2324 if (asprintf(&ondisk_path, "%s%s%s",
2325 worktree->root_path, path[0] ? "/" : "", path) == -1)
2326 return got_error_from_errno("asprintf");
2328 workdir = opendir(ondisk_path);
2329 if (workdir == NULL) {
2330 if (errno != ENOTDIR && errno != ENOENT)
2331 err = got_error_from_errno2("opendir", ondisk_path);
2333 err = report_single_file_status(path, ondisk_path,
2334 fileindex, status_cb, status_arg, repo);
2336 fdiff_cb.diff_old_new = status_old_new;
2337 fdiff_cb.diff_old = status_old;
2338 fdiff_cb.diff_new = status_new;
2339 arg.fileindex = fileindex;
2340 arg.worktree = worktree;
2341 arg.status_path = path;
2342 arg.status_path_len = strlen(path);
2344 arg.status_cb = status_cb;
2345 arg.status_arg = status_arg;
2346 arg.cancel_cb = cancel_cb;
2347 arg.cancel_arg = cancel_arg;
2348 err = got_fileindex_diff_dir(fileindex, workdir,
2349 worktree->root_path, path, repo, &fdiff_cb, &arg);
2358 const struct got_error *
2359 got_worktree_status(struct got_worktree *worktree,
2360 struct got_pathlist_head *paths, struct got_repository *repo,
2361 got_worktree_status_cb status_cb, void *status_arg,
2362 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2364 const struct got_error *err = NULL;
2365 char *fileindex_path = NULL;
2366 struct got_fileindex *fileindex = NULL;
2367 struct got_pathlist_entry *pe;
2369 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2373 TAILQ_FOREACH(pe, paths, entry) {
2374 err = worktree_status(worktree, pe->path, fileindex, repo,
2375 status_cb, status_arg, cancel_cb, cancel_arg);
2379 free(fileindex_path);
2380 got_fileindex_free(fileindex);
2384 const struct got_error *
2385 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2388 const struct got_error *err = NULL;
2389 char *resolved, *cwd = NULL, *path = NULL;
2394 resolved = realpath(arg, NULL);
2395 if (resolved == NULL) {
2396 if (errno != ENOENT)
2397 return got_error_from_errno2("realpath", arg);
2398 cwd = getcwd(NULL, 0);
2400 return got_error_from_errno("getcwd");
2401 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
2402 err = got_error_from_errno("asprintf");
2407 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2408 strlen(got_worktree_get_root_path(worktree)))) {
2409 err = got_error(GOT_ERR_BAD_PATH);
2413 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2414 err = got_path_skip_common_ancestor(&path,
2415 got_worktree_get_root_path(worktree), resolved);
2421 err = got_error_from_errno("strdup");
2426 /* XXX status walk can't deal with trailing slash! */
2428 while (len > 0 && path[len - 1] == '/') {
2429 path[len - 1] = '\0';
2442 static const struct got_error *
2443 schedule_addition(const char *ondisk_path, struct got_fileindex *fileindex,
2444 const char *relpath, got_worktree_status_cb status_cb, void *status_arg,
2445 struct got_repository *repo)
2447 const struct got_error *err = NULL;
2448 struct got_fileindex_entry *ie;
2450 /* Re-adding an existing entry is a no-op. */
2451 if (got_fileindex_entry_get(fileindex, relpath) != NULL)
2454 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2458 err = got_fileindex_entry_add(fileindex, ie);
2460 got_fileindex_entry_free(ie);
2464 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2467 const struct got_error *
2468 got_worktree_schedule_add(struct got_worktree *worktree,
2469 struct got_pathlist_head *ondisk_paths,
2470 got_worktree_status_cb status_cb, void *status_arg,
2471 struct got_repository *repo)
2473 struct got_fileindex *fileindex = NULL;
2474 char *fileindex_path = NULL;
2475 const struct got_error *err = NULL, *sync_err, *unlockerr;
2476 struct got_pathlist_entry *pe;
2478 err = lock_worktree(worktree, LOCK_EX);
2482 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2486 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2488 err = got_path_skip_common_ancestor(&relpath,
2489 got_worktree_get_root_path(worktree), pe->path);
2492 err = schedule_addition(pe->path, fileindex, relpath,
2493 status_cb, status_arg, repo);
2498 sync_err = sync_fileindex(fileindex, fileindex_path);
2499 if (sync_err && err == NULL)
2502 free(fileindex_path);
2504 got_fileindex_free(fileindex);
2505 unlockerr = lock_worktree(worktree, LOCK_SH);
2506 if (unlockerr && err == NULL)
2511 static const struct got_error *
2512 schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
2513 const char *relpath, int delete_local_mods,
2514 got_worktree_status_cb status_cb, void *status_arg,
2515 struct got_repository *repo)
2517 const struct got_error *err = NULL;
2518 struct got_fileindex_entry *ie = NULL;
2519 unsigned char status;
2522 ie = got_fileindex_entry_get(fileindex, relpath);
2524 return got_error(GOT_ERR_BAD_PATH);
2526 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2530 if (status != GOT_STATUS_NO_CHANGE) {
2531 if (status == GOT_STATUS_DELETE)
2532 return got_error_set_errno(ENOENT, ondisk_path);
2533 if (status != GOT_STATUS_MODIFY)
2534 return got_error(GOT_ERR_FILE_STATUS);
2535 if (!delete_local_mods)
2536 return got_error(GOT_ERR_FILE_MODIFIED);
2539 if (unlink(ondisk_path) != 0)
2540 return got_error_from_errno2("unlink", ondisk_path);
2542 got_fileindex_entry_mark_deleted_from_disk(ie);
2543 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2546 const struct got_error *
2547 got_worktree_schedule_delete(struct got_worktree *worktree,
2548 struct got_pathlist_head *ondisk_paths, int delete_local_mods,
2549 got_worktree_status_cb status_cb, void *status_arg,
2550 struct got_repository *repo)
2552 struct got_fileindex *fileindex = NULL;
2553 char *fileindex_path = NULL;
2554 const struct got_error *err = NULL, *sync_err, *unlockerr;
2555 struct got_pathlist_entry *pe;
2557 err = lock_worktree(worktree, LOCK_EX);
2561 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2565 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2567 err = got_path_skip_common_ancestor(&relpath,
2568 got_worktree_get_root_path(worktree), pe->path);
2571 err = schedule_for_deletion(pe->path, fileindex, relpath,
2572 delete_local_mods, status_cb, status_arg, repo);
2577 sync_err = sync_fileindex(fileindex, fileindex_path);
2578 if (sync_err && err == NULL)
2581 free(fileindex_path);
2583 got_fileindex_free(fileindex);
2584 unlockerr = lock_worktree(worktree, LOCK_SH);
2585 if (unlockerr && err == NULL)
2590 static const struct got_error *
2591 revert_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2592 const char *ondisk_path,
2593 got_worktree_checkout_cb progress_cb, void *progress_arg,
2594 struct got_repository *repo)
2596 const struct got_error *err = NULL;
2597 char *relpath = NULL, *parent_path = NULL;
2598 struct got_fileindex_entry *ie;
2599 struct got_tree_object *tree = NULL;
2600 struct got_object_id *tree_id = NULL;
2601 const struct got_tree_entry *te;
2602 char *tree_path = NULL, *te_name;
2603 struct got_blob_object *blob = NULL;
2604 unsigned char status;
2607 err = got_path_skip_common_ancestor(&relpath,
2608 got_worktree_get_root_path(worktree), ondisk_path);
2612 ie = got_fileindex_entry_get(fileindex, relpath);
2614 err = got_error(GOT_ERR_BAD_PATH);
2618 /* Construct in-repository path of tree which contains this blob. */
2619 err = got_path_dirname(&parent_path, ie->path);
2621 if (err->code != GOT_ERR_BAD_PATH)
2623 parent_path = strdup("/");
2624 if (parent_path == NULL) {
2625 err = got_error_from_errno("strdup");
2629 if (got_path_is_root_dir(worktree->path_prefix)) {
2630 tree_path = strdup(parent_path);
2631 if (tree_path == NULL) {
2632 err = got_error_from_errno("strdup");
2636 if (got_path_is_root_dir(parent_path)) {
2637 tree_path = strdup(worktree->path_prefix);
2638 if (tree_path == NULL) {
2639 err = got_error_from_errno("strdup");
2643 if (asprintf(&tree_path, "%s/%s",
2644 worktree->path_prefix, parent_path) == -1) {
2645 err = got_error_from_errno("asprintf");
2651 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2655 err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2658 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
2659 status == GOT_STATUS_ADD))
2662 err = got_object_open_as_tree(&tree, repo, tree_id);
2666 te_name = basename(ie->path);
2667 if (te_name == NULL) {
2668 err = got_error_from_errno2("basename", ie->path);
2672 te = got_object_tree_find_entry(tree, te_name);
2673 if (te == NULL && status != GOT_STATUS_ADD) {
2674 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2680 case GOT_STATUS_ADD:
2681 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2684 got_fileindex_entry_remove(fileindex, ie);
2686 case GOT_STATUS_DELETE:
2687 case GOT_STATUS_MODIFY:
2688 case GOT_STATUS_CONFLICT:
2689 case GOT_STATUS_MISSING: {
2690 struct got_object_id id;
2691 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2692 err = got_object_open_as_blob(&blob, repo, &id, 8192);
2695 err = install_blob(worktree, ondisk_path, ie->path,
2696 te->mode, sb.st_mode, blob, 0, 1, repo, progress_cb,
2700 if (status == GOT_STATUS_DELETE) {
2701 err = update_blob_fileindex_entry(worktree,
2702 fileindex, ie, ondisk_path, ie->path, blob, 1);
2716 got_object_blob_close(blob);
2718 got_object_tree_close(tree);
2723 const struct got_error *
2724 got_worktree_revert(struct got_worktree *worktree,
2725 struct got_pathlist_head *ondisk_paths,
2726 got_worktree_checkout_cb progress_cb, void *progress_arg,
2727 struct got_repository *repo)
2729 struct got_fileindex *fileindex = NULL;
2730 char *fileindex_path = NULL;
2731 const struct got_error *err = NULL, *unlockerr = NULL;
2732 const struct got_error *sync_err = NULL;
2733 struct got_pathlist_entry *pe;
2735 err = lock_worktree(worktree, LOCK_EX);
2739 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2743 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2744 err = revert_file(worktree, fileindex, pe->path,
2745 progress_cb, progress_arg, repo);
2749 sync_err = sync_fileindex(fileindex, fileindex_path);
2750 if (sync_err && err == NULL)
2753 free(fileindex_path);
2755 got_fileindex_free(fileindex);
2756 unlockerr = lock_worktree(worktree, LOCK_SH);
2757 if (unlockerr && err == NULL)
2763 free_commitable(struct got_commitable *ct)
2766 free(ct->in_repo_path);
2767 free(ct->ondisk_path);
2769 free(ct->base_blob_id);
2770 free(ct->base_commit_id);
2774 struct collect_commitables_arg {
2775 struct got_pathlist_head *commitable_paths;
2776 struct got_repository *repo;
2777 struct got_worktree *worktree;
2780 static const struct got_error *
2781 collect_commitables(void *arg, unsigned char status, const char *relpath,
2782 struct got_object_id *blob_id, struct got_object_id *commit_id)
2784 struct collect_commitables_arg *a = arg;
2785 const struct got_error *err = NULL;
2786 struct got_commitable *ct = NULL;
2787 struct got_pathlist_entry *new = NULL;
2788 char *parent_path = NULL, *path = NULL;
2791 if (status == GOT_STATUS_CONFLICT)
2792 return got_error(GOT_ERR_COMMIT_CONFLICT);
2794 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
2795 status != GOT_STATUS_DELETE)
2798 if (asprintf(&path, "/%s", relpath) == -1) {
2799 err = got_error_from_errno("asprintf");
2802 if (strcmp(path, "/") == 0) {
2803 parent_path = strdup("");
2804 if (parent_path == NULL)
2805 return got_error_from_errno("strdup");
2807 err = got_path_dirname(&parent_path, path);
2812 ct = calloc(1, sizeof(*ct));
2814 err = got_error_from_errno("calloc");
2818 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
2820 err = got_error_from_errno("asprintf");
2823 if (status == GOT_STATUS_DELETE) {
2824 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2826 if (lstat(ct->ondisk_path, &sb) != 0) {
2827 err = got_error_from_errno2("lstat", ct->ondisk_path);
2830 ct->mode = sb.st_mode;
2833 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
2834 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
2836 err = got_error_from_errno("asprintf");
2840 ct->status = status;
2841 ct->blob_id = NULL; /* will be filled in when blob gets created */
2842 if (ct->status != GOT_STATUS_ADD) {
2843 ct->base_blob_id = got_object_id_dup(blob_id);
2844 if (ct->base_blob_id == NULL) {
2845 err = got_error_from_errno("got_object_id_dup");
2848 ct->base_commit_id = got_object_id_dup(commit_id);
2849 if (ct->base_commit_id == NULL) {
2850 err = got_error_from_errno("got_object_id_dup");
2854 ct->path = strdup(path);
2855 if (ct->path == NULL) {
2856 err = got_error_from_errno("strdup");
2859 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
2861 if (ct && (err || new == NULL))
2862 free_commitable(ct);
2868 static const struct got_error *write_tree(struct got_object_id **,
2869 struct got_tree_object *, const char *, struct got_pathlist_head *,
2870 got_worktree_status_cb status_cb, void *status_arg,
2871 struct got_repository *);
2873 static const struct got_error *
2874 write_subtree(struct got_object_id **new_subtree_id,
2875 struct got_tree_entry *te, const char *parent_path,
2876 struct got_pathlist_head *commitable_paths,
2877 got_worktree_status_cb status_cb, void *status_arg,
2878 struct got_repository *repo)
2880 const struct got_error *err = NULL;
2881 struct got_tree_object *subtree;
2884 if (asprintf(&subpath, "%s%s%s", parent_path,
2885 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
2886 return got_error_from_errno("asprintf");
2888 err = got_object_open_as_tree(&subtree, repo, te->id);
2892 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
2893 status_cb, status_arg, repo);
2894 got_object_tree_close(subtree);
2899 static const struct got_error *
2900 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
2902 const struct got_error *err = NULL;
2903 char *ct_parent_path = NULL;
2907 if (strchr(ct->in_repo_path, '/') == NULL) {
2908 *match = got_path_is_root_dir(path);
2912 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
2915 *match = (strcmp(path, ct_parent_path) == 0);
2916 free(ct_parent_path);
2921 get_ct_file_mode(struct got_commitable *ct)
2923 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
2926 static const struct got_error *
2927 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
2928 struct got_tree_entry *te, struct got_commitable *ct)
2930 const struct got_error *err = NULL;
2934 err = got_object_tree_entry_dup(new_te, te);
2938 (*new_te)->mode = get_ct_file_mode(ct);
2940 free((*new_te)->id);
2941 (*new_te)->id = got_object_id_dup(ct->blob_id);
2942 if ((*new_te)->id == NULL) {
2943 err = got_error_from_errno("got_object_id_dup");
2947 if (err && *new_te) {
2948 got_object_tree_entry_close(*new_te);
2954 static const struct got_error *
2955 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2956 struct got_commitable *ct)
2958 const struct got_error *err = NULL;
2963 *new_te = calloc(1, sizeof(**new_te));
2964 if (*new_te == NULL)
2965 return got_error_from_errno("calloc");
2967 ct_name = basename(ct->path);
2968 if (ct_name == NULL) {
2969 err = got_error_from_errno2("basename", ct->path);
2972 (*new_te)->name = strdup(ct_name);
2973 if ((*new_te)->name == NULL) {
2974 err = got_error_from_errno("strdup");
2978 (*new_te)->mode = get_ct_file_mode(ct);
2980 (*new_te)->id = got_object_id_dup(ct->blob_id);
2981 if ((*new_te)->id == NULL) {
2982 err = got_error_from_errno("got_object_id_dup");
2986 if (err && *new_te) {
2987 got_object_tree_entry_close(*new_te);
2993 static const struct got_error *
2994 insert_tree_entry(struct got_tree_entry *new_te,
2995 struct got_pathlist_head *paths)
2997 const struct got_error *err = NULL;
2998 struct got_pathlist_entry *new_pe;
3000 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
3004 return got_error(GOT_ERR_TREE_DUP_ENTRY);
3008 static const struct got_error *
3009 report_ct_status(struct got_commitable *ct,
3010 got_worktree_status_cb status_cb, void *status_arg)
3012 const char *ct_path = ct->path;
3013 while (ct_path[0] == '/')
3015 return (*status_cb)(status_arg, ct->status, ct_path, ct->blob_id, NULL);
3018 static const struct got_error *
3019 match_modified_subtree(int *modified, struct got_tree_entry *te,
3020 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
3022 const struct got_error *err = NULL;
3023 struct got_pathlist_entry *pe;
3028 if (asprintf(&te_path, "%s%s%s", base_tree_path,
3029 got_path_is_root_dir(base_tree_path) ? "" : "/",
3031 return got_error_from_errno("asprintf");
3033 TAILQ_FOREACH(pe, commitable_paths, entry) {
3034 struct got_commitable *ct = pe->data;
3035 *modified = got_path_is_child(ct->in_repo_path, te_path,
3045 static const struct got_error *
3046 match_deleted_or_modified_ct(struct got_commitable **ctp,
3047 struct got_tree_entry *te, const char *base_tree_path,
3048 struct got_pathlist_head *commitable_paths)
3050 const struct got_error *err = NULL;
3051 struct got_pathlist_entry *pe;
3055 TAILQ_FOREACH(pe, commitable_paths, entry) {
3056 struct got_commitable *ct = pe->data;
3057 char *ct_name = NULL;
3060 if (ct->status != GOT_STATUS_MODIFY &&
3061 ct->status != GOT_STATUS_DELETE)
3064 if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
3067 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
3073 ct_name = basename(pe->path);
3074 if (ct_name == NULL)
3075 return got_error_from_errno2("basename", pe->path);
3077 if (strcmp(te->name, ct_name) != 0)
3087 static const struct got_error *
3088 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
3089 const char *child_path, const char *path_base_tree,
3090 struct got_pathlist_head *commitable_paths,
3091 got_worktree_status_cb status_cb, void *status_arg,
3092 struct got_repository *repo)
3094 const struct got_error *err = NULL;
3095 struct got_tree_entry *new_te;
3100 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
3101 got_path_is_root_dir(path_base_tree) ? "" : "/",
3103 return got_error_from_errno("asprintf");
3105 new_te = calloc(1, sizeof(*new_te));
3106 new_te->mode = S_IFDIR;
3107 new_te->name = strdup(child_path);
3108 if (new_te->name == NULL) {
3109 err = got_error_from_errno("strdup");
3110 got_object_tree_entry_close(new_te);
3113 err = write_tree(&new_te->id, NULL, subtree_path,
3114 commitable_paths, status_cb, status_arg, repo);
3116 got_object_tree_entry_close(new_te);
3126 static const struct got_error *
3127 write_tree(struct got_object_id **new_tree_id,
3128 struct got_tree_object *base_tree, const char *path_base_tree,
3129 struct got_pathlist_head *commitable_paths,
3130 got_worktree_status_cb status_cb, void *status_arg,
3131 struct got_repository *repo)
3133 const struct got_error *err = NULL;
3134 const struct got_tree_entries *base_entries = NULL;
3135 struct got_pathlist_head paths;
3136 struct got_tree_entries new_tree_entries;
3137 struct got_tree_entry *te, *new_te = NULL;
3138 struct got_pathlist_entry *pe;
3141 new_tree_entries.nentries = 0;
3142 SIMPLEQ_INIT(&new_tree_entries.head);
3144 /* Insert, and recurse into, newly added entries first. */
3145 TAILQ_FOREACH(pe, commitable_paths, entry) {
3146 struct got_commitable *ct = pe->data;
3147 char *child_path = NULL, *slash;
3149 if (ct->status != GOT_STATUS_ADD ||
3150 (ct->flags & GOT_COMMITABLE_ADDED))
3153 if (!got_path_is_child(pe->path, path_base_tree,
3154 strlen(path_base_tree)))
3157 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
3162 slash = strchr(child_path, '/');
3163 if (slash == NULL) {
3164 err = alloc_added_blob_tree_entry(&new_te, ct);
3167 err = report_ct_status(ct, status_cb, status_arg);
3170 ct->flags |= GOT_COMMITABLE_ADDED;
3171 err = insert_tree_entry(new_te, &paths);
3175 *slash = '\0'; /* trim trailing path components */
3176 if (base_tree == NULL ||
3177 got_object_tree_find_entry(base_tree, child_path)
3179 err = make_subtree_for_added_blob(&new_te,
3180 child_path, path_base_tree,
3181 commitable_paths, status_cb, status_arg,
3185 err = insert_tree_entry(new_te, &paths);
3193 /* Handle modified and deleted entries. */
3194 base_entries = got_object_tree_get_entries(base_tree);
3195 SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
3196 struct got_commitable *ct = NULL;
3198 if (S_ISDIR(te->mode)) {
3200 err = got_object_tree_entry_dup(&new_te, te);
3203 err = match_modified_subtree(&modified, te,
3204 path_base_tree, commitable_paths);
3207 /* Avoid recursion into unmodified subtrees. */
3210 err = write_subtree(&new_te->id, te,
3211 path_base_tree, commitable_paths,
3212 status_cb, status_arg, repo);
3216 err = insert_tree_entry(new_te, &paths);
3222 err = match_deleted_or_modified_ct(&ct, te,
3223 path_base_tree, commitable_paths);
3225 /* NB: Deleted entries get dropped here. */
3226 if (ct->status == GOT_STATUS_MODIFY) {
3227 err = alloc_modified_blob_tree_entry(
3231 err = insert_tree_entry(new_te, &paths);
3235 err = report_ct_status(ct, status_cb,
3240 /* Entry is unchanged; just copy it. */
3241 err = got_object_tree_entry_dup(&new_te, te);
3244 err = insert_tree_entry(new_te, &paths);
3251 /* Write new list of entries; deleted entries have been dropped. */
3252 TAILQ_FOREACH(pe, &paths, entry) {
3253 struct got_tree_entry *te = pe->data;
3254 new_tree_entries.nentries++;
3255 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
3257 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
3259 got_object_tree_entries_close(&new_tree_entries);
3260 got_pathlist_free(&paths);
3264 static const struct got_error *
3265 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
3266 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex)
3268 const struct got_error *err = NULL;
3269 struct got_pathlist_entry *pe;
3271 TAILQ_FOREACH(pe, commitable_paths, entry) {
3272 struct got_fileindex_entry *ie;
3273 struct got_commitable *ct = pe->data;
3275 ie = got_fileindex_entry_get(fileindex, pe->path);
3277 if (ct->status == GOT_STATUS_DELETE) {
3278 got_fileindex_entry_remove(fileindex, ie);
3279 got_fileindex_entry_free(ie);
3281 err = got_fileindex_entry_update(ie,
3282 ct->ondisk_path, ct->blob_id->sha1,
3283 new_base_commit_id->sha1, 1);
3285 err = got_fileindex_entry_alloc(&ie,
3286 ct->ondisk_path, pe->path, ct->blob_id->sha1,
3287 new_base_commit_id->sha1);
3290 err = got_fileindex_entry_add(fileindex, ie);
3298 static const struct got_error *
3299 check_ct_out_of_date(struct got_commitable *ct, struct got_repository *repo,
3300 struct got_object_id *head_commit_id)
3302 const struct got_error *err = NULL;
3303 struct got_object_id *id = NULL;
3304 struct got_commit_object *commit = NULL;
3305 const char *ct_path = ct->in_repo_path;
3307 while (ct_path[0] == '/')
3310 if (ct->status != GOT_STATUS_ADD) {
3311 /* Trivial case: base commit == head commit */
3312 if (got_object_id_cmp(ct->base_commit_id, head_commit_id) == 0)
3315 * Ensure file content which local changes were based
3316 * on matches file content in the branch head.
3318 err = got_object_id_by_path(&id, repo, head_commit_id, ct_path);
3320 if (err->code != GOT_ERR_NO_TREE_ENTRY)
3322 err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
3324 } else if (got_object_id_cmp(id, ct->base_blob_id) != 0)
3325 err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
3327 /* Require that added files don't exist in the branch head. */
3328 err = got_object_id_by_path(&id, repo, head_commit_id, ct_path);
3329 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3331 err = id ? got_error(GOT_ERR_COMMIT_OUT_OF_DATE) : NULL;
3335 got_object_commit_close(commit);
3340 const struct got_error *
3341 commit_worktree(struct got_object_id **new_commit_id,
3342 struct got_pathlist_head *commitable_paths,
3343 struct got_object_id *head_commit_id, struct got_worktree *worktree,
3344 const char *author, const char *committer,
3345 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3346 got_worktree_status_cb status_cb, void *status_arg,
3347 struct got_repository *repo)
3349 const struct got_error *err = NULL, *unlockerr = NULL;
3350 struct got_pathlist_entry *pe;
3351 const char *head_ref_name = NULL;
3352 struct got_commit_object *head_commit = NULL;
3353 struct got_reference *head_ref2 = NULL;
3354 struct got_object_id *head_commit_id2 = NULL;
3355 struct got_tree_object *head_tree = NULL;
3356 struct got_object_id *new_tree_id = NULL;
3357 struct got_object_id_queue parent_ids;
3358 struct got_object_qid *pid = NULL;
3359 char *logmsg = NULL;
3361 *new_commit_id = NULL;
3363 SIMPLEQ_INIT(&parent_ids);
3365 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
3369 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
3373 if (commit_msg_cb != NULL) {
3374 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
3379 if (logmsg == NULL || strlen(logmsg) == 0) {
3380 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
3384 /* Create blobs from added and modified files and record their IDs. */
3385 TAILQ_FOREACH(pe, commitable_paths, entry) {
3386 struct got_commitable *ct = pe->data;
3389 if (ct->status != GOT_STATUS_ADD &&
3390 ct->status != GOT_STATUS_MODIFY)
3393 if (asprintf(&ondisk_path, "%s/%s",
3394 worktree->root_path, pe->path) == -1) {
3395 err = got_error_from_errno("asprintf");
3398 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
3404 /* Recursively write new tree objects. */
3405 err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
3406 status_cb, status_arg, repo);
3410 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
3413 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
3414 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
3415 1, author, time(NULL), committer, time(NULL), logmsg, repo);
3416 got_object_qid_free(pid);
3422 /* Check if a concurrent commit to our branch has occurred. */
3423 head_ref_name = got_worktree_get_head_ref_name(worktree);
3424 if (head_ref_name == NULL) {
3425 err = got_error_from_errno("got_worktree_get_head_ref_name");
3428 /* Lock the reference here to prevent concurrent modification. */
3429 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
3432 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
3435 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
3436 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
3439 /* Update branch head in repository. */
3440 err = got_ref_change_ref(head_ref2, *new_commit_id);
3443 err = got_ref_write(head_ref2, repo);
3447 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
3451 err = ref_base_commit(worktree, repo);
3456 got_object_tree_close(head_tree);
3458 got_object_commit_close(head_commit);
3459 free(head_commit_id2);
3461 unlockerr = got_ref_unlock(head_ref2);
3462 if (unlockerr && err == NULL)
3464 got_ref_close(head_ref2);
3469 static const struct got_error *
3470 check_path_is_commitable(const char *path,
3471 struct got_pathlist_head *commitable_paths)
3473 struct got_pathlist_entry *cpe = NULL;
3474 size_t path_len = strlen(path);
3476 TAILQ_FOREACH(cpe, commitable_paths, entry) {
3477 struct got_commitable *ct = cpe->data;
3478 const char *ct_path = ct->path;
3480 while (ct_path[0] == '/')
3483 if (strcmp(path, ct_path) == 0 ||
3484 got_path_is_child(ct_path, path, path_len))
3489 return got_error_path(path, GOT_ERR_BAD_PATH);
3494 const struct got_error *
3495 got_worktree_commit(struct got_object_id **new_commit_id,
3496 struct got_worktree *worktree, struct got_pathlist_head *paths,
3497 const char *author, const char *committer,
3498 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3499 got_worktree_status_cb status_cb, void *status_arg,
3500 struct got_repository *repo)
3502 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
3503 struct got_fileindex *fileindex = NULL;
3504 char *fileindex_path = NULL;
3505 struct got_pathlist_head commitable_paths;
3506 struct collect_commitables_arg cc_arg;
3507 struct got_pathlist_entry *pe;
3508 struct got_reference *head_ref = NULL;
3509 struct got_object_id *head_commit_id = NULL;
3511 *new_commit_id = NULL;
3513 TAILQ_INIT(&commitable_paths);
3515 err = lock_worktree(worktree, LOCK_EX);
3519 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
3523 err = got_ref_resolve(&head_commit_id, repo, head_ref);
3527 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3531 cc_arg.commitable_paths = &commitable_paths;
3532 cc_arg.worktree = worktree;
3534 TAILQ_FOREACH(pe, paths, entry) {
3535 err = worktree_status(worktree, pe->path, fileindex, repo,
3536 collect_commitables, &cc_arg, NULL, NULL);
3541 if (TAILQ_EMPTY(&commitable_paths)) {
3542 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
3546 TAILQ_FOREACH(pe, paths, entry) {
3547 err = check_path_is_commitable(pe->path, &commitable_paths);
3552 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3553 struct got_commitable *ct = pe->data;
3554 err = check_ct_out_of_date(ct, repo, head_commit_id);
3559 err = commit_worktree(new_commit_id, &commitable_paths,
3560 head_commit_id, worktree, author, committer,
3561 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
3565 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
3567 sync_err = sync_fileindex(fileindex, fileindex_path);
3568 if (sync_err && err == NULL)
3572 got_fileindex_free(fileindex);
3573 free(fileindex_path);
3574 unlockerr = lock_worktree(worktree, LOCK_SH);
3575 if (unlockerr && err == NULL)
3577 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3578 struct got_commitable *ct = pe->data;
3579 free_commitable(ct);
3581 got_pathlist_free(&commitable_paths);
3586 got_commitable_get_path(struct got_commitable *ct)
3592 got_commitable_get_status(struct got_commitable *ct)
3597 struct check_rebase_ok_arg {
3598 struct got_worktree *worktree;
3599 struct got_repository *repo;
3602 static const struct got_error *
3603 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
3605 const struct got_error *err = NULL;
3606 struct check_rebase_ok_arg *a = arg;
3607 unsigned char status;
3611 /* Reject rebase of a work tree with mixed base commits. */
3612 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3613 SHA1_DIGEST_LENGTH))
3614 return got_error(GOT_ERR_MIXED_COMMITS);
3616 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3618 return got_error_from_errno("asprintf");
3620 /* Reject rebase of a work tree with modified or conflicted files. */
3621 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
3626 if (status != GOT_STATUS_NO_CHANGE)
3627 return got_error(GOT_ERR_MODIFIED);
3632 const struct got_error *
3633 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
3634 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
3635 struct got_worktree *worktree, struct got_reference *branch,
3636 struct got_repository *repo)
3638 const struct got_error *err = NULL;
3639 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
3640 char *branch_ref_name = NULL;
3641 char *fileindex_path = NULL;
3642 struct check_rebase_ok_arg ok_arg;
3643 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
3645 *new_base_branch_ref = NULL;
3649 err = lock_worktree(worktree, LOCK_EX);
3653 err = open_fileindex(fileindex, &fileindex_path, worktree);
3657 ok_arg.worktree = worktree;
3659 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
3664 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3668 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3672 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3676 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
3681 err = got_ref_alloc_symref(new_base_branch_ref,
3682 new_base_branch_ref_name, wt_branch);
3685 err = got_ref_write(*new_base_branch_ref, repo);
3689 /* TODO Lock original branch's ref while rebasing? */
3691 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
3695 err = got_ref_write(branch_ref, repo);
3699 err = got_ref_alloc(tmp_branch, tmp_branch_name,
3700 worktree->base_commit_id);
3703 err = got_ref_write(*tmp_branch, repo);
3707 err = got_worktree_set_head_ref(worktree, *tmp_branch);
3711 free(fileindex_path);
3712 free(tmp_branch_name);
3713 free(new_base_branch_ref_name);
3714 free(branch_ref_name);
3716 got_ref_close(branch_ref);
3718 got_ref_close(wt_branch);
3720 if (*new_base_branch_ref) {
3721 got_ref_close(*new_base_branch_ref);
3722 *new_base_branch_ref = NULL;
3725 got_ref_close(*tmp_branch);
3729 got_fileindex_free(*fileindex);
3732 lock_worktree(worktree, LOCK_SH);
3737 const struct got_error *
3738 got_worktree_rebase_continue(struct got_object_id **commit_id,
3739 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
3740 struct got_reference **branch, struct got_fileindex **fileindex,
3741 struct got_worktree *worktree, struct got_repository *repo)
3743 const struct got_error *err;
3744 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
3745 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
3746 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
3747 char *fileindex_path = NULL;
3750 *new_base_branch = NULL;
3755 err = lock_worktree(worktree, LOCK_EX);
3759 err = open_fileindex(fileindex, &fileindex_path, worktree);
3763 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3767 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3771 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3775 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3779 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
3783 err = got_ref_open(branch, repo,
3784 got_ref_get_symref_target(branch_ref), 0);
3788 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3792 err = got_ref_resolve(commit_id, repo, commit_ref);
3796 err = got_ref_open(new_base_branch, repo,
3797 new_base_branch_ref_name, 0);
3801 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
3805 free(commit_ref_name);
3806 free(branch_ref_name);
3807 free(fileindex_path);
3809 got_ref_close(commit_ref);
3811 got_ref_close(branch_ref);
3816 got_ref_close(*tmp_branch);
3819 if (*new_base_branch) {
3820 got_ref_close(*new_base_branch);
3821 *new_base_branch = NULL;
3824 got_ref_close(*branch);
3828 got_fileindex_free(*fileindex);
3831 lock_worktree(worktree, LOCK_SH);
3836 const struct got_error *
3837 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
3839 const struct got_error *err;
3840 char *tmp_branch_name = NULL;
3842 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3846 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
3847 free(tmp_branch_name);
3851 static const struct got_error *
3852 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
3853 char **logmsg, void *arg)
3859 static const struct got_error *
3860 rebase_status(void *arg, unsigned char status, const char *path,
3861 struct got_object_id *blob_id, struct got_object_id *commit_id)
3866 struct collect_merged_paths_arg {
3867 got_worktree_checkout_cb progress_cb;
3869 struct got_pathlist_head *merged_paths;
3872 static const struct got_error *
3873 collect_merged_paths(void *arg, unsigned char status, const char *path)
3875 const struct got_error *err;
3876 struct collect_merged_paths_arg *a = arg;
3878 struct got_pathlist_entry *new;
3880 err = (*a->progress_cb)(a->progress_arg, status, path);
3884 if (status != GOT_STATUS_MERGE &&
3885 status != GOT_STATUS_ADD &&
3886 status != GOT_STATUS_DELETE &&
3887 status != GOT_STATUS_CONFLICT)
3892 return got_error_from_errno("strdup");
3894 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
3895 if (err || new == NULL)
3901 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
3903 struct got_pathlist_entry *pe;
3905 TAILQ_FOREACH(pe, merged_paths, entry)
3906 free((char *)pe->path);
3908 got_pathlist_free(merged_paths);
3911 static const struct got_error *
3912 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
3913 struct got_repository *repo)
3915 const struct got_error *err;
3916 struct got_reference *commit_ref = NULL;
3918 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3920 if (err->code != GOT_ERR_NOT_REF)
3922 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
3925 err = got_ref_write(commit_ref, repo);
3929 struct got_object_id *stored_id;
3932 err = got_ref_resolve(&stored_id, repo, commit_ref);
3935 cmp = got_object_id_cmp(commit_id, stored_id);
3938 err = got_error(GOT_ERR_REBASE_COMMITID);
3944 got_ref_close(commit_ref);
3948 static const struct got_error *
3949 rebase_merge_files(struct got_pathlist_head *merged_paths,
3950 const char *commit_ref_name, struct got_worktree *worktree,
3951 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
3952 struct got_object_id *commit_id, struct got_repository *repo,
3953 got_worktree_checkout_cb progress_cb, void *progress_arg,
3954 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
3956 const struct got_error *err;
3957 struct got_reference *commit_ref = NULL;
3958 struct collect_merged_paths_arg cmp_arg;
3959 char *fileindex_path;
3961 /* Work tree is locked/unlocked during rebase preparation/teardown. */
3963 err = get_fileindex_path(&fileindex_path, worktree);
3967 cmp_arg.progress_cb = progress_cb;
3968 cmp_arg.progress_arg = progress_arg;
3969 cmp_arg.merged_paths = merged_paths;
3970 err = merge_files(worktree, fileindex, fileindex_path,
3971 parent_commit_id, commit_id, repo, collect_merged_paths,
3972 &cmp_arg, cancel_cb, cancel_arg);
3974 got_ref_close(commit_ref);
3978 const struct got_error *
3979 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
3980 struct got_worktree *worktree, struct got_fileindex *fileindex,
3981 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
3982 struct got_repository *repo,
3983 got_worktree_checkout_cb progress_cb, void *progress_arg,
3984 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
3986 const struct got_error *err;
3987 char *commit_ref_name;
3989 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3993 err = store_commit_id(commit_ref_name, commit_id, repo);
3997 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
3998 fileindex, parent_commit_id, commit_id, repo, progress_cb,
3999 progress_arg, cancel_cb, cancel_arg);
4001 free(commit_ref_name);
4005 const struct got_error *
4006 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
4007 struct got_worktree *worktree, struct got_fileindex *fileindex,
4008 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4009 struct got_repository *repo,
4010 got_worktree_checkout_cb progress_cb, void *progress_arg,
4011 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
4013 const struct got_error *err;
4014 char *commit_ref_name;
4016 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4020 err = store_commit_id(commit_ref_name, commit_id, repo);
4024 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4025 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4026 progress_arg, cancel_cb, cancel_arg);
4028 free(commit_ref_name);
4032 static const struct got_error *
4033 rebase_commit(struct got_object_id **new_commit_id,
4034 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
4035 struct got_worktree *worktree, struct got_fileindex *fileindex,
4036 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
4037 const char *new_logmsg, struct got_repository *repo)
4039 const struct got_error *err, *sync_err;
4040 struct got_pathlist_head commitable_paths;
4041 struct collect_commitables_arg cc_arg;
4042 char *fileindex_path = NULL;
4043 struct got_reference *head_ref = NULL;
4044 struct got_object_id *head_commit_id = NULL;
4045 char *logmsg = NULL;
4047 TAILQ_INIT(&commitable_paths);
4048 *new_commit_id = NULL;
4050 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4052 err = get_fileindex_path(&fileindex_path, worktree);
4056 cc_arg.commitable_paths = &commitable_paths;
4057 cc_arg.worktree = worktree;
4060 * If possible get the status of individual files directly to
4061 * avoid crawling the entire work tree once per rebased commit.
4062 * TODO: Ideally, merged_paths would contain a list of commitables
4063 * we could use so we could skip worktree_status() entirely.
4066 struct got_pathlist_entry *pe;
4067 if (TAILQ_EMPTY(merged_paths)) {
4068 err = got_error(GOT_ERR_NO_MERGED_PATHS);
4071 TAILQ_FOREACH(pe, merged_paths, entry) {
4072 err = worktree_status(worktree, pe->path, fileindex,
4073 repo, collect_commitables, &cc_arg, NULL, NULL);
4078 err = worktree_status(worktree, "", fileindex, repo,
4079 collect_commitables, &cc_arg, NULL, NULL);
4084 if (TAILQ_EMPTY(&commitable_paths)) {
4085 /* No-op change; commit will be elided. */
4086 err = got_ref_delete(commit_ref, repo);
4089 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4093 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4097 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4102 logmsg = strdup(new_logmsg);
4104 logmsg = strdup(got_object_commit_get_logmsg(orig_commit));
4106 return got_error_from_errno("strdup");
4108 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
4109 worktree, got_object_commit_get_author(orig_commit),
4110 got_object_commit_get_committer(orig_commit),
4111 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
4115 err = got_ref_change_ref(tmp_branch, *new_commit_id);
4119 err = got_ref_delete(commit_ref, repo);
4123 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4125 sync_err = sync_fileindex(fileindex, fileindex_path);
4126 if (sync_err && err == NULL)
4129 free(fileindex_path);
4130 free(head_commit_id);
4132 got_ref_close(head_ref);
4134 free(*new_commit_id);
4135 *new_commit_id = NULL;
4140 const struct got_error *
4141 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
4142 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4143 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4144 struct got_commit_object *orig_commit,
4145 struct got_object_id *orig_commit_id, struct got_repository *repo)
4147 const struct got_error *err;
4148 char *commit_ref_name;
4149 struct got_reference *commit_ref = NULL;
4150 struct got_object_id *commit_id = NULL;
4152 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4156 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4159 err = got_ref_resolve(&commit_id, repo, commit_ref);
4162 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4163 err = got_error(GOT_ERR_REBASE_COMMITID);
4167 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4168 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
4171 got_ref_close(commit_ref);
4172 free(commit_ref_name);
4177 const struct got_error *
4178 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
4179 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4180 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4181 struct got_commit_object *orig_commit,
4182 struct got_object_id *orig_commit_id, const char *new_logmsg,
4183 struct got_repository *repo)
4185 const struct got_error *err;
4186 char *commit_ref_name;
4187 struct got_reference *commit_ref = NULL;
4188 struct got_object_id *commit_id = NULL;
4190 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4194 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4197 err = got_ref_resolve(&commit_id, repo, commit_ref);
4200 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4201 err = got_error(GOT_ERR_HISTEDIT_COMMITID);
4205 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4206 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
4209 got_ref_close(commit_ref);
4210 free(commit_ref_name);
4215 const struct got_error *
4216 got_worktree_rebase_postpone(struct got_worktree *worktree,
4217 struct got_fileindex *fileindex)
4220 got_fileindex_free(fileindex);
4221 return lock_worktree(worktree, LOCK_SH);
4224 static const struct got_error *
4225 delete_ref(const char *name, struct got_repository *repo)
4227 const struct got_error *err;
4228 struct got_reference *ref;
4230 err = got_ref_open(&ref, repo, name, 0);
4232 if (err->code == GOT_ERR_NOT_REF)
4237 err = got_ref_delete(ref, repo);
4242 static const struct got_error *
4243 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
4245 const struct got_error *err;
4246 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4247 char *branch_ref_name = NULL, *commit_ref_name = NULL;
4249 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4252 err = delete_ref(tmp_branch_name, repo);
4256 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4259 err = delete_ref(new_base_branch_ref_name, repo);
4263 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4266 err = delete_ref(branch_ref_name, repo);
4270 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4273 err = delete_ref(commit_ref_name, repo);
4278 free(tmp_branch_name);
4279 free(new_base_branch_ref_name);
4280 free(branch_ref_name);
4281 free(commit_ref_name);
4285 const struct got_error *
4286 got_worktree_rebase_complete(struct got_worktree *worktree,
4287 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
4288 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
4289 struct got_repository *repo)
4291 const struct got_error *err, *unlockerr;
4292 struct got_object_id *new_head_commit_id = NULL;
4294 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
4298 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
4302 err = got_ref_write(rebased_branch, repo);
4306 err = got_worktree_set_head_ref(worktree, rebased_branch);
4310 err = delete_rebase_refs(worktree, repo);
4313 got_fileindex_free(fileindex);
4314 free(new_head_commit_id);
4315 unlockerr = lock_worktree(worktree, LOCK_SH);
4316 if (unlockerr && err == NULL)
4321 struct collect_revertible_paths_arg {
4322 struct got_pathlist_head *revertible_paths;
4323 struct got_worktree *worktree;
4326 static const struct got_error *
4327 collect_revertible_paths(void *arg, unsigned char status, const char *relpath,
4328 struct got_object_id *blob_id, struct got_object_id *commit_id)
4330 struct collect_revertible_paths_arg *a = arg;
4331 const struct got_error *err = NULL;
4332 struct got_pathlist_entry *new = NULL;
4335 if (status != GOT_STATUS_ADD &&
4336 status != GOT_STATUS_DELETE &&
4337 status != GOT_STATUS_MODIFY &&
4338 status != GOT_STATUS_CONFLICT &&
4339 status != GOT_STATUS_MISSING)
4342 if (asprintf(&path, "%s/%s", a->worktree->root_path, relpath) == -1)
4343 return got_error_from_errno("asprintf");
4345 err = got_pathlist_insert(&new, a->revertible_paths, path, NULL);
4346 if (err || new == NULL)
4351 const struct got_error *
4352 got_worktree_rebase_abort(struct got_worktree *worktree,
4353 struct got_fileindex *fileindex, struct got_repository *repo,
4354 struct got_reference *new_base_branch,
4355 got_worktree_checkout_cb progress_cb, void *progress_arg)
4357 const struct got_error *err, *unlockerr, *sync_err;
4358 struct got_reference *resolved = NULL;
4359 struct got_object_id *commit_id = NULL;
4360 char *fileindex_path = NULL;
4361 struct got_pathlist_head revertible_paths;
4362 struct got_pathlist_entry *pe;
4363 struct collect_revertible_paths_arg crp_arg;
4364 struct got_object_id *tree_id = NULL;
4366 TAILQ_INIT(&revertible_paths);
4368 err = lock_worktree(worktree, LOCK_EX);
4372 err = got_ref_open(&resolved, repo,
4373 got_ref_get_symref_target(new_base_branch), 0);
4377 err = got_worktree_set_head_ref(worktree, resolved);
4382 * XXX commits to the base branch could have happened while
4383 * we were busy rebasing; should we store the original commit ID
4384 * when rebase begins and read it back here?
4386 err = got_ref_resolve(&commit_id, repo, resolved);
4390 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
4394 err = got_object_id_by_path(&tree_id, repo,
4395 worktree->base_commit_id, worktree->path_prefix);
4399 err = delete_rebase_refs(worktree, repo);
4403 err = get_fileindex_path(&fileindex_path, worktree);
4407 crp_arg.revertible_paths = &revertible_paths;
4408 crp_arg.worktree = worktree;
4409 err = worktree_status(worktree, "", fileindex, repo,
4410 collect_revertible_paths, &crp_arg, NULL, NULL);
4414 TAILQ_FOREACH(pe, &revertible_paths, entry) {
4415 err = revert_file(worktree, fileindex, pe->path,
4416 progress_cb, progress_arg, repo);
4421 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
4422 repo, progress_cb, progress_arg, NULL, NULL);
4424 sync_err = sync_fileindex(fileindex, fileindex_path);
4425 if (sync_err && err == NULL)
4428 got_ref_close(resolved);
4432 got_fileindex_free(fileindex);
4433 free(fileindex_path);
4434 TAILQ_FOREACH(pe, &revertible_paths, entry)
4435 free((char *)pe->path);
4436 got_pathlist_free(&revertible_paths);
4438 unlockerr = lock_worktree(worktree, LOCK_SH);
4439 if (unlockerr && err == NULL)
4444 const struct got_error *
4445 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
4446 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
4447 struct got_fileindex **fileindex, struct got_worktree *worktree,
4448 struct got_repository *repo)
4450 const struct got_error *err = NULL;
4451 char *tmp_branch_name = NULL;
4452 char *branch_ref_name = NULL;
4453 char *base_commit_ref_name = NULL;
4454 char *fileindex_path = NULL;
4455 struct check_rebase_ok_arg ok_arg;
4456 struct got_reference *wt_branch = NULL;
4457 struct got_reference *base_commit_ref = NULL;
4461 *base_commit_id = NULL;
4464 err = lock_worktree(worktree, LOCK_EX);
4468 err = open_fileindex(fileindex, &fileindex_path, worktree);
4472 ok_arg.worktree = worktree;
4474 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4479 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4483 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4487 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4492 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
4497 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
4501 err = got_ref_write(*branch_ref, repo);
4505 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
4506 worktree->base_commit_id);
4509 err = got_ref_write(base_commit_ref, repo);
4512 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
4513 if (*base_commit_id == NULL) {
4514 err = got_error_from_errno("got_object_id_dup");
4518 err = got_ref_alloc(tmp_branch, tmp_branch_name,
4519 worktree->base_commit_id);
4522 err = got_ref_write(*tmp_branch, repo);
4526 err = got_worktree_set_head_ref(worktree, *tmp_branch);
4530 free(fileindex_path);
4531 free(tmp_branch_name);
4532 free(branch_ref_name);
4533 free(base_commit_ref_name);
4535 got_ref_close(wt_branch);
4538 got_ref_close(*branch_ref);
4542 got_ref_close(*tmp_branch);
4545 free(*base_commit_id);
4547 got_fileindex_free(*fileindex);
4550 lock_worktree(worktree, LOCK_SH);
4555 const struct got_error *
4556 got_worktree_histedit_postpone(struct got_worktree *worktree,
4557 struct got_fileindex *fileindex)
4560 got_fileindex_free(fileindex);
4561 return lock_worktree(worktree, LOCK_SH);
4564 const struct got_error *
4565 got_worktree_histedit_in_progress(int *in_progress,
4566 struct got_worktree *worktree)
4568 const struct got_error *err;
4569 char *tmp_branch_name = NULL;
4571 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4575 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4576 free(tmp_branch_name);
4580 const struct got_error *
4581 got_worktree_histedit_continue(struct got_object_id **commit_id,
4582 struct got_reference **tmp_branch, struct got_reference **branch_ref,
4583 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
4584 struct got_worktree *worktree, struct got_repository *repo)
4586 const struct got_error *err;
4587 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
4588 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
4589 struct got_reference *commit_ref = NULL;
4590 struct got_reference *base_commit_ref = NULL;
4591 char *fileindex_path = NULL;
4595 *base_commit_id = NULL;
4598 err = lock_worktree(worktree, LOCK_EX);
4602 err = open_fileindex(fileindex, &fileindex_path, worktree);
4606 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4610 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4614 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4618 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4623 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
4627 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4630 err = got_ref_resolve(commit_id, repo, commit_ref);
4634 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
4637 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
4641 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4645 free(commit_ref_name);
4646 free(branch_ref_name);
4647 free(fileindex_path);
4649 got_ref_close(commit_ref);
4650 if (base_commit_ref)
4651 got_ref_close(base_commit_ref);
4655 free(*base_commit_id);
4656 *base_commit_id = NULL;
4658 got_ref_close(*tmp_branch);
4662 got_fileindex_free(*fileindex);
4665 lock_worktree(worktree, LOCK_EX);
4670 static const struct got_error *
4671 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
4673 const struct got_error *err;
4674 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
4675 char *branch_ref_name = NULL, *commit_ref_name = NULL;
4677 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4680 err = delete_ref(tmp_branch_name, repo);
4684 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4688 err = delete_ref(base_commit_ref_name, repo);
4692 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4695 err = delete_ref(branch_ref_name, repo);
4699 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4702 err = delete_ref(commit_ref_name, repo);
4706 free(tmp_branch_name);
4707 free(base_commit_ref_name);
4708 free(branch_ref_name);
4709 free(commit_ref_name);
4713 const struct got_error *
4714 got_worktree_histedit_abort(struct got_worktree *worktree,
4715 struct got_fileindex *fileindex, struct got_repository *repo,
4716 struct got_reference *branch, struct got_object_id *base_commit_id,
4717 got_worktree_checkout_cb progress_cb, void *progress_arg)
4719 const struct got_error *err, *unlockerr, *sync_err;
4720 struct got_reference *resolved = NULL;
4721 char *fileindex_path = NULL;
4722 struct got_pathlist_head revertible_paths;
4723 struct got_pathlist_entry *pe;
4724 struct collect_revertible_paths_arg crp_arg;
4725 struct got_object_id *tree_id = NULL;
4727 TAILQ_INIT(&revertible_paths);
4729 err = lock_worktree(worktree, LOCK_EX);
4733 err = got_ref_open(&resolved, repo,
4734 got_ref_get_symref_target(branch), 0);
4738 err = got_worktree_set_head_ref(worktree, resolved);
4742 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
4746 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
4747 worktree->path_prefix);
4751 err = delete_histedit_refs(worktree, repo);
4755 err = get_fileindex_path(&fileindex_path, worktree);
4759 crp_arg.revertible_paths = &revertible_paths;
4760 crp_arg.worktree = worktree;
4761 err = worktree_status(worktree, "", fileindex, repo,
4762 collect_revertible_paths, &crp_arg, NULL, NULL);
4766 TAILQ_FOREACH(pe, &revertible_paths, entry) {
4767 err = revert_file(worktree, fileindex, pe->path,
4768 progress_cb, progress_arg, repo);
4773 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
4774 repo, progress_cb, progress_arg, NULL, NULL);
4776 sync_err = sync_fileindex(fileindex, fileindex_path);
4777 if (sync_err && err == NULL)
4780 got_ref_close(resolved);
4782 free(fileindex_path);
4783 TAILQ_FOREACH(pe, &revertible_paths, entry)
4784 free((char *)pe->path);
4785 got_pathlist_free(&revertible_paths);
4787 unlockerr = lock_worktree(worktree, LOCK_SH);
4788 if (unlockerr && err == NULL)
4793 const struct got_error *
4794 got_worktree_histedit_complete(struct got_worktree *worktree,
4795 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4796 struct got_reference *edited_branch, struct got_repository *repo)
4798 const struct got_error *err, *unlockerr;
4799 struct got_object_id *new_head_commit_id = NULL;
4800 struct got_reference *resolved = NULL;
4802 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
4806 err = got_ref_open(&resolved, repo,
4807 got_ref_get_symref_target(edited_branch), 0);
4811 err = got_ref_change_ref(resolved, new_head_commit_id);
4815 err = got_ref_write(resolved, repo);
4819 err = got_worktree_set_head_ref(worktree, resolved);
4823 err = delete_histedit_refs(worktree, repo);
4826 got_fileindex_free(fileindex);
4827 free(new_head_commit_id);
4828 unlockerr = lock_worktree(worktree, LOCK_SH);
4829 if (unlockerr && err == NULL)
4834 const struct got_error *
4835 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
4836 struct got_object_id *commit_id, struct got_repository *repo)
4838 const struct got_error *err;
4839 char *commit_ref_name;
4841 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4845 err = store_commit_id(commit_ref_name, commit_id, repo);
4849 err = delete_ref(commit_ref_name, repo);
4851 free(commit_ref_name);