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(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 (lstat(path, &sb) != 0) {
146 err = got_error_from_errno2("lstat", 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(GOT_ERR_WORKTREE_META));
161 if ((*content)[sb.st_size - 1] != '\n') {
162 err = got_error(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(GOT_ERR_WORKTREE_META);
361 if (version != GOT_WORKTREE_FORMAT_VERSION) {
362 err = got_error(GOT_ERR_WORKTREE_VERS);
366 *worktree = calloc(1, sizeof(**worktree));
367 if (*worktree == NULL) {
368 err = got_error_from_errno("calloc");
371 (*worktree)->lockfd = -1;
373 (*worktree)->root_path = strdup(path);
374 if ((*worktree)->root_path == NULL) {
375 err = got_error_from_errno("strdup");
378 err = read_meta_file(&(*worktree)->repo_path, path_got,
379 GOT_WORKTREE_REPOSITORY);
383 err = read_meta_file(&(*worktree)->path_prefix, path_got,
384 GOT_WORKTREE_PATH_PREFIX);
388 err = read_meta_file(&base_commit_id_str, path_got,
389 GOT_WORKTREE_BASE_COMMIT);
393 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
396 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
397 if (uuid_status != uuid_s_ok) {
398 err = got_error_uuid(uuid_status);
402 err = got_repo_open(&repo, (*worktree)->repo_path);
406 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
411 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
412 GOT_WORKTREE_HEAD_REF);
415 got_repo_close(repo);
418 free(base_commit_id_str);
424 if (*worktree != NULL)
425 got_worktree_close(*worktree);
428 (*worktree)->lockfd = fd;
433 const struct got_error *
434 got_worktree_open(struct got_worktree **worktree, const char *path)
436 const struct got_error *err = NULL;
439 err = open_worktree(worktree, path);
440 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
444 path = dirname(path);
446 return got_error_from_errno2("dirname", path);
447 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
449 return got_error(GOT_ERR_NOT_WORKTREE);
452 const struct got_error *
453 got_worktree_close(struct got_worktree *worktree)
455 const struct got_error *err = NULL;
456 free(worktree->root_path);
457 free(worktree->repo_path);
458 free(worktree->path_prefix);
459 free(worktree->base_commit_id);
460 free(worktree->head_ref_name);
461 if (worktree->lockfd != -1)
462 if (close(worktree->lockfd) != 0)
463 err = got_error_from_errno2("close",
464 got_worktree_get_root_path(worktree));
470 got_worktree_get_root_path(struct got_worktree *worktree)
472 return worktree->root_path;
476 got_worktree_get_repo_path(struct got_worktree *worktree)
478 return worktree->repo_path;
482 got_worktree_get_path_prefix(struct got_worktree *worktree)
484 return worktree->path_prefix;
487 const struct got_error *
488 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
489 const char *path_prefix)
491 char *absprefix = NULL;
493 if (!got_path_is_absolute(path_prefix)) {
494 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
495 return got_error_from_errno("asprintf");
497 *match = (strcmp(absprefix ? absprefix : path_prefix,
498 worktree->path_prefix) == 0);
504 got_worktree_get_head_ref_name(struct got_worktree *worktree)
506 return worktree->head_ref_name;
509 const struct got_error *
510 got_worktree_set_head_ref(struct got_worktree *worktree,
511 struct got_reference *head_ref)
513 const struct got_error *err = NULL;
514 char *path_got = NULL, *head_ref_name = NULL;
516 if (asprintf(&path_got, "%s/%s", worktree->root_path,
517 GOT_WORKTREE_GOT_DIR) == -1) {
518 err = got_error_from_errno("asprintf");
523 head_ref_name = strdup(got_ref_get_name(head_ref));
524 if (head_ref_name == NULL) {
525 err = got_error_from_errno("strdup");
529 err = write_head_ref(path_got, head_ref);
533 free(worktree->head_ref_name);
534 worktree->head_ref_name = head_ref_name;
542 struct got_object_id *
543 got_worktree_get_base_commit_id(struct got_worktree *worktree)
545 return worktree->base_commit_id;
548 const struct got_error *
549 got_worktree_set_base_commit_id(struct got_worktree *worktree,
550 struct got_repository *repo, struct got_object_id *commit_id)
552 const struct got_error *err;
553 struct got_object *obj = NULL;
555 char *path_got = NULL;
557 if (asprintf(&path_got, "%s/%s", worktree->root_path,
558 GOT_WORKTREE_GOT_DIR) == -1) {
559 err = got_error_from_errno("asprintf");
564 err = got_object_open(&obj, repo, commit_id);
568 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
569 err = got_error(GOT_ERR_OBJ_TYPE);
573 /* Record our base commit. */
574 err = got_object_id_str(&id_str, commit_id);
577 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
581 free(worktree->base_commit_id);
582 worktree->base_commit_id = got_object_id_dup(commit_id);
583 if (worktree->base_commit_id == NULL) {
584 err = got_error_from_errno("got_object_id_dup");
589 got_object_close(obj);
595 static const struct got_error *
596 lock_worktree(struct got_worktree *worktree, int operation)
598 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
599 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
600 : got_error_from_errno2("flock",
601 got_worktree_get_root_path(worktree)));
605 static const struct got_error *
606 add_dir_on_disk(struct got_worktree *worktree, const char *path)
608 const struct got_error *err = NULL;
611 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
612 return got_error_from_errno("asprintf");
614 err = got_path_mkdir(abspath);
615 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
618 if (lstat(abspath, &sb) == -1) {
619 err = got_error_from_errno2("lstat", abspath);
620 } else if (!S_ISDIR(sb.st_mode)) {
621 /* TODO directory is obstructed; do something */
622 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
629 static const struct got_error *
630 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
632 const struct got_error *err = NULL;
635 size_t flen1 = 0, flen2 = 0;
640 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
641 if (flen1 == 0 && ferror(f1)) {
642 err = got_error_from_errno("fread");
645 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
646 if (flen2 == 0 && ferror(f2)) {
647 err = got_error_from_errno("fread");
654 } else if (flen2 == 0) {
658 } else if (flen1 == flen2) {
659 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
672 static const struct got_error *
673 check_files_equal(int *same, const char *f1_path, const char *f2_path)
675 const struct got_error *err = NULL;
678 FILE *f1 = NULL, *f2 = NULL;
682 if (lstat(f1_path, &sb) != 0) {
683 err = got_error_from_errno2("lstat", f1_path);
688 if (lstat(f2_path, &sb) != 0) {
689 err = got_error_from_errno2("lstat", f2_path);
694 if (size1 != size2) {
699 f1 = fopen(f1_path, "r");
701 return got_error_from_errno2("open", f1_path);
703 f2 = fopen(f2_path, "r");
705 err = got_error_from_errno2("open", f2_path);
709 err = check_file_contents_equal(same, f1, f2);
711 if (f1 && fclose(f1) != 0 && err == NULL)
712 err = got_error_from_errno("fclose");
713 if (f2 && fclose(f2) != 0 && err == NULL)
714 err = got_error_from_errno("fclose");
720 * Perform a 3-way merge where blob_orig acts as the common ancestor,
721 * blob_deriv acts as the first derived version, and the file on disk
722 * acts as the second derived version.
724 static const struct got_error *
725 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
726 struct got_blob_object *blob_orig, const char *ondisk_path,
727 const char *path, uint16_t st_mode, struct got_blob_object *blob_deriv,
728 struct got_object_id *deriv_base_commit_id,
729 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
732 const struct got_error *err = NULL;
734 FILE *f_deriv = NULL, *f_orig = NULL;
735 char *blob_deriv_path = NULL, *blob_orig_path = NULL;
736 char *merged_path = NULL, *base_path = NULL;
738 char *label_deriv = NULL;
742 *local_changes_subsumed = 0;
744 parent = dirname(ondisk_path);
746 return got_error_from_errno2("dirname", ondisk_path);
748 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
749 return got_error_from_errno("asprintf");
751 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
756 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
757 err = got_error_from_errno("asprintf");
762 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
765 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
771 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
772 err = got_error_from_errno("asprintf");
777 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
781 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
787 * If the file has no blob, this is an "add vs add" conflict,
788 * and we simply use an empty ancestor file to make both files
789 * appear in the merged result in their entirety.
793 err = got_object_id_str(&id_str, deriv_base_commit_id);
796 if (asprintf(&label_deriv, "commit %s", id_str) == -1) {
797 err = got_error_from_errno("asprintf");
801 err = got_merge_diff3(&overlapcnt, merged_fd, blob_deriv_path,
802 blob_orig_path, ondisk_path, label_deriv, path);
806 err = (*progress_cb)(progress_arg,
807 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
811 if (fsync(merged_fd) != 0) {
812 err = got_error_from_errno("fsync");
816 /* Check if a clean merge has subsumed all local changes. */
817 if (overlapcnt == 0) {
818 err = check_files_equal(local_changes_subsumed, blob_deriv_path,
824 if (chmod(merged_path, st_mode) != 0) {
825 err = got_error_from_errno2("chmod", merged_path);
829 if (rename(merged_path, ondisk_path) != 0) {
830 err = got_error_from_errno3("rename", merged_path,
837 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
838 err = got_error_from_errno("close");
839 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
840 err = got_error_from_errno("fclose");
841 if (f_orig && fclose(f_orig) != 0 && err == NULL)
842 err = got_error_from_errno("fclose");
845 if (blob_deriv_path) {
846 unlink(blob_deriv_path);
847 free(blob_deriv_path);
849 if (blob_orig_path) {
850 unlink(blob_orig_path);
851 free(blob_orig_path);
858 static const struct got_error *
859 update_blob_fileindex_entry(struct got_worktree *worktree,
860 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
861 const char *ondisk_path, const char *path, struct got_blob_object *blob,
862 int update_timestamps)
864 const struct got_error *err = NULL;
867 ie = got_fileindex_entry_get(fileindex, path);
869 err = got_fileindex_entry_update(ie, ondisk_path,
870 blob->id.sha1, worktree->base_commit_id->sha1,
873 struct got_fileindex_entry *new_ie;
874 err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
875 path, blob->id.sha1, worktree->base_commit_id->sha1);
877 err = got_fileindex_entry_add(fileindex, new_ie);
882 static const struct got_error *
883 install_blob(struct got_worktree *worktree, const char *ondisk_path,
884 const char *path, uint16_t te_mode, uint16_t st_mode,
885 struct got_blob_object *blob, int restoring_missing_file,
886 int reverting_versioned_file, struct got_repository *repo,
887 got_worktree_checkout_cb progress_cb, void *progress_arg)
889 const struct got_error *err = NULL;
893 char *tmppath = NULL;
895 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
896 GOT_DEFAULT_FILE_MODE);
898 if (errno == ENOENT) {
899 char *parent = dirname(path);
901 return got_error_from_errno2("dirname", path);
902 err = add_dir_on_disk(worktree, parent);
905 fd = open(ondisk_path,
906 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
907 GOT_DEFAULT_FILE_MODE);
909 return got_error_from_errno2("open",
911 } else if (errno == EEXIST) {
912 if (!S_ISREG(st_mode)) {
913 /* TODO file is obstructed; do something */
914 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
917 err = got_opentemp_named_fd(&tmppath, &fd,
924 return got_error_from_errno2("open", ondisk_path);
927 if (restoring_missing_file)
928 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
929 else if (reverting_versioned_file)
930 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
932 err = (*progress_cb)(progress_arg,
933 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
937 hdrlen = got_object_blob_get_hdrlen(blob);
939 const uint8_t *buf = got_object_blob_get_read_buf(blob);
940 err = got_object_blob_read_block(&len, blob);
944 /* Skip blob object header first time around. */
945 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
947 err = got_error_from_errno("write");
949 } else if (outlen != len - hdrlen) {
950 err = got_error(GOT_ERR_IO);
957 if (fsync(fd) != 0) {
958 err = got_error_from_errno("fsync");
963 if (rename(tmppath, ondisk_path) != 0) {
964 err = got_error_from_errno3("rename", tmppath,
971 if (te_mode & S_IXUSR) {
972 if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
973 err = got_error_from_errno2("chmod", ondisk_path);
977 if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
978 err = got_error_from_errno2("chmod", ondisk_path);
984 if (fd != -1 && close(fd) != 0 && err == NULL)
985 err = got_error_from_errno("close");
990 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
991 static const struct got_error *
992 get_modified_file_content_status(unsigned char *status, FILE *f)
994 const struct got_error *err = NULL;
995 const char *markers[3] = {
996 GOT_DIFF_CONFLICT_MARKER_BEGIN,
997 GOT_DIFF_CONFLICT_MARKER_SEP,
998 GOT_DIFF_CONFLICT_MARKER_END
1003 const char delim[3] = {'\0', '\0', '\0'};
1005 while (*status == GOT_STATUS_MODIFY) {
1006 line = fparseln(f, &len, NULL, delim, 0);
1010 err = got_ferror(f, GOT_ERR_IO);
1014 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1015 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1017 *status = GOT_STATUS_CONFLICT;
1027 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1029 return !(ie->ctime_sec == sb->st_ctime &&
1030 ie->ctime_nsec == sb->st_ctimensec &&
1031 ie->mtime_sec == sb->st_mtime &&
1032 ie->mtime_nsec == sb->st_mtimensec &&
1033 ie->size == (sb->st_size & 0xffffffff));
1036 static const struct got_error *
1037 get_file_status(unsigned char *status, struct stat *sb,
1038 struct got_fileindex_entry *ie, const char *abspath,
1039 struct got_repository *repo)
1041 const struct got_error *err = NULL;
1042 struct got_object_id id;
1046 struct got_blob_object *blob = NULL;
1049 *status = GOT_STATUS_NO_CHANGE;
1051 if (lstat(abspath, sb) == -1) {
1052 if (errno == ENOENT) {
1053 if (got_fileindex_entry_has_file_on_disk(ie))
1054 *status = GOT_STATUS_MISSING;
1056 *status = GOT_STATUS_DELETE;
1059 return got_error_from_errno2("lstat", abspath);
1062 if (!S_ISREG(sb->st_mode)) {
1063 *status = GOT_STATUS_OBSTRUCTED;
1067 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1068 *status = GOT_STATUS_DELETE;
1070 } else if (!got_fileindex_entry_has_blob(ie)) {
1071 *status = GOT_STATUS_ADD;
1075 if (!stat_info_differs(ie, sb))
1078 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1079 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1083 f = fopen(abspath, "r");
1085 err = got_error_from_errno2("fopen", abspath);
1088 hdrlen = got_object_blob_get_hdrlen(blob);
1090 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1091 err = got_object_blob_read_block(&blen, blob);
1094 /* Skip length of blob object header first time around. */
1095 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1096 if (flen == 0 && ferror(f)) {
1097 err = got_error_from_errno("fread");
1102 *status = GOT_STATUS_MODIFY;
1104 } else if (flen == 0) {
1106 *status = GOT_STATUS_MODIFY;
1108 } else if (blen - hdrlen == flen) {
1109 /* Skip blob object header first time around. */
1110 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1111 *status = GOT_STATUS_MODIFY;
1115 *status = GOT_STATUS_MODIFY;
1121 if (*status == GOT_STATUS_MODIFY) {
1123 err = get_modified_file_content_status(status, f);
1127 got_object_blob_close(blob);
1134 * Update timestamps in the file index if a file is unmodified and
1135 * we had to run a full content comparison to find out.
1137 static const struct got_error *
1138 sync_timestamps(char *ondisk_path, unsigned char status,
1139 struct got_fileindex_entry *ie, struct stat *sb)
1141 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1142 return got_fileindex_entry_update(ie, ondisk_path,
1143 ie->blob_sha1, ie->commit_sha1, 1);
1148 static const struct got_error *
1149 update_blob(struct got_worktree *worktree,
1150 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1151 struct got_tree_entry *te, const char *path,
1152 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1155 const struct got_error *err = NULL;
1156 struct got_blob_object *blob = NULL;
1158 unsigned char status = GOT_STATUS_NO_CHANGE;
1161 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1162 return got_error_from_errno("asprintf");
1165 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1168 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1169 sb.st_mode = got_fileindex_perms_to_st(ie);
1171 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1173 if (status == GOT_STATUS_OBSTRUCTED) {
1174 err = (*progress_cb)(progress_arg, status, path);
1178 if (ie && status != GOT_STATUS_MISSING) {
1179 if (got_fileindex_entry_has_commit(ie) &&
1180 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1181 SHA1_DIGEST_LENGTH) == 0) {
1182 err = sync_timestamps(ondisk_path, status, ie, &sb);
1185 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1189 if (got_fileindex_entry_has_blob(ie) &&
1190 memcmp(ie->blob_sha1, te->id->sha1,
1191 SHA1_DIGEST_LENGTH) == 0) {
1192 err = sync_timestamps(ondisk_path, status, ie, &sb);
1197 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1201 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1202 int update_timestamps;
1203 struct got_blob_object *blob2 = NULL;
1204 if (got_fileindex_entry_has_blob(ie)) {
1205 struct got_object_id id2;
1206 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1207 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1211 err = merge_blob(&update_timestamps, worktree, blob2,
1212 ondisk_path, path, sb.st_mode, blob,
1213 worktree->base_commit_id, repo,
1214 progress_cb, progress_arg);
1216 got_object_blob_close(blob2);
1218 * Do not update timestamps of files with local changes.
1219 * Otherwise, a future status walk would treat them as
1220 * unmodified files again.
1222 err = got_fileindex_entry_update(ie, ondisk_path,
1223 blob->id.sha1, worktree->base_commit_id->sha1,
1225 } else if (status == GOT_STATUS_DELETE) {
1226 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1229 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1230 ondisk_path, path, blob, 0);
1234 err = install_blob(worktree, ondisk_path, path, te->mode,
1235 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1236 repo, progress_cb, progress_arg);
1239 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1240 ondisk_path, path, blob, 1);
1244 got_object_blob_close(blob);
1250 static const struct got_error *
1251 remove_ondisk_file(const char *root_path, const char *path)
1253 const struct got_error *err = NULL;
1254 char *ondisk_path = NULL;
1256 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1257 return got_error_from_errno("asprintf");
1259 if (unlink(ondisk_path) == -1) {
1260 if (errno != ENOENT)
1261 err = got_error_from_errno2("unlink", ondisk_path);
1263 char *parent = dirname(ondisk_path);
1264 while (parent && strcmp(parent, root_path) != 0) {
1265 if (rmdir(parent) == -1) {
1266 if (errno != ENOTEMPTY)
1267 err = got_error_from_errno2("rmdir",
1271 parent = dirname(parent);
1278 static const struct got_error *
1279 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1280 struct got_fileindex_entry *ie, struct got_repository *repo,
1281 got_worktree_checkout_cb progress_cb, void *progress_arg)
1283 const struct got_error *err = NULL;
1284 unsigned char status;
1288 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1290 return got_error_from_errno("asprintf");
1292 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1296 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1297 status == GOT_STATUS_ADD) {
1298 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1302 * Preserve the working file and change the deleted blob's
1303 * entry into a schedule-add entry.
1305 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1310 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1313 if (status == GOT_STATUS_NO_CHANGE) {
1314 err = remove_ondisk_file(worktree->root_path, ie->path);
1318 got_fileindex_entry_remove(fileindex, ie);
1324 struct diff_cb_arg {
1325 struct got_fileindex *fileindex;
1326 struct got_worktree *worktree;
1327 struct got_repository *repo;
1328 got_worktree_checkout_cb progress_cb;
1330 got_worktree_cancel_cb cancel_cb;
1334 static const struct got_error *
1335 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1336 struct got_tree_entry *te, const char *parent_path)
1338 struct diff_cb_arg *a = arg;
1340 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1341 return got_error(GOT_ERR_CANCELLED);
1343 return update_blob(a->worktree, a->fileindex, ie, te,
1344 ie->path, a->repo, a->progress_cb, a->progress_arg);
1347 static const struct got_error *
1348 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1350 struct diff_cb_arg *a = arg;
1352 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1353 return got_error(GOT_ERR_CANCELLED);
1355 return delete_blob(a->worktree, a->fileindex, ie,
1356 a->repo, a->progress_cb, a->progress_arg);
1359 static const struct got_error *
1360 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1362 struct diff_cb_arg *a = arg;
1363 const struct got_error *err;
1366 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1367 return got_error(GOT_ERR_CANCELLED);
1369 if (asprintf(&path, "%s%s%s", parent_path,
1370 parent_path[0] ? "/" : "", te->name)
1372 return got_error_from_errno("asprintf");
1374 if (S_ISDIR(te->mode))
1375 err = add_dir_on_disk(a->worktree, path);
1377 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1378 a->repo, a->progress_cb, a->progress_arg);
1384 static const struct got_error *
1385 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1387 const struct got_error *err = NULL;
1388 char *uuidstr = NULL;
1389 uint32_t uuid_status;
1393 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1394 if (uuid_status != uuid_s_ok)
1395 return got_error_uuid(uuid_status);
1397 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1399 err = got_error_from_errno("asprintf");
1406 const struct got_error *
1407 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1409 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1412 static const struct got_error *
1413 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1415 return get_ref_name(refname, worktree,
1416 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1419 static const struct got_error *
1420 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1422 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1425 static const struct got_error *
1426 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1428 return get_ref_name(refname, worktree,
1429 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1432 static const struct got_error *
1433 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1435 return get_ref_name(refname, worktree,
1436 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1439 static const struct got_error *
1440 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1442 return get_ref_name(refname, worktree,
1443 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1446 static const struct got_error *
1447 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1449 return get_ref_name(refname, worktree,
1450 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1453 static const struct got_error *
1454 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1456 return get_ref_name(refname, worktree,
1457 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1460 static const struct got_error *
1461 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1463 return get_ref_name(refname, worktree,
1464 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1467 const struct got_error *
1468 got_worktree_get_histedit_list_path(char **path, struct got_worktree *worktree)
1470 if (asprintf(path, "%s/%s/%s", worktree->root_path,
1471 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_LIST) == -1) {
1473 return got_error_from_errno("asprintf");
1479 * Prevent Git's garbage collector from deleting our base commit by
1480 * setting a reference to our base commit's ID.
1482 static const struct got_error *
1483 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1485 const struct got_error *err = NULL;
1486 struct got_reference *ref = NULL;
1489 err = got_worktree_get_base_ref_name(&refname, worktree);
1493 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1497 err = got_ref_write(ref, repo);
1505 static const struct got_error *
1506 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
1508 const struct got_error *err = NULL;
1510 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1511 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1512 err = got_error_from_errno("asprintf");
1513 *fileindex_path = NULL;
1519 static const struct got_error *
1520 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1521 struct got_worktree *worktree)
1523 const struct got_error *err = NULL;
1526 *fileindex_path = NULL;
1527 *fileindex = got_fileindex_alloc();
1528 if (*fileindex == NULL)
1529 return got_error_from_errno("got_fileindex_alloc");
1531 err = get_fileindex_path(fileindex_path, worktree);
1535 index = fopen(*fileindex_path, "rb");
1536 if (index == NULL) {
1537 if (errno != ENOENT)
1538 err = got_error_from_errno2("fopen", *fileindex_path);
1540 err = got_fileindex_read(*fileindex, index);
1541 if (fclose(index) != 0 && err == NULL)
1542 err = got_error_from_errno("fclose");
1546 free(*fileindex_path);
1547 *fileindex_path = NULL;
1548 got_fileindex_free(*fileindex);
1554 struct bump_base_commit_id_arg {
1555 struct got_object_id *base_commit_id;
1558 const char *entry_name;
1559 got_worktree_checkout_cb progress_cb;
1563 /* Bump base commit ID of all files within an updated part of the work tree. */
1564 static const struct got_error *
1565 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1567 const struct got_error *err;
1568 struct bump_base_commit_id_arg *a = arg;
1570 if (a->entry_name) {
1571 if (strcmp(ie->path, a->path) != 0)
1573 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1576 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1577 SHA1_DIGEST_LENGTH) == 0)
1580 if (a->progress_cb) {
1581 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1586 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1590 static const struct got_error *
1591 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1593 const struct got_error *err = NULL;
1594 char *new_fileindex_path = NULL;
1595 FILE *new_index = NULL;
1597 err = got_opentemp_named(&new_fileindex_path, &new_index,
1602 err = got_fileindex_write(fileindex, new_index);
1606 if (rename(new_fileindex_path, fileindex_path) != 0) {
1607 err = got_error_from_errno3("rename", new_fileindex_path,
1609 unlink(new_fileindex_path);
1614 free(new_fileindex_path);
1618 static const struct got_error *
1619 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1620 struct got_object_id **tree_id, const char *wt_relpath,
1621 struct got_worktree *worktree, struct got_repository *repo)
1623 const struct got_error *err = NULL;
1624 struct got_object_id *id = NULL;
1625 char *in_repo_path = NULL;
1626 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1628 *entry_type = GOT_OBJ_TYPE_ANY;
1629 *tree_relpath = NULL;
1632 if (wt_relpath[0] == '\0') {
1633 /* Check out all files within the work tree. */
1634 *entry_type = GOT_OBJ_TYPE_TREE;
1635 *tree_relpath = strdup("");
1636 if (*tree_relpath == NULL) {
1637 err = got_error_from_errno("strdup");
1640 err = got_object_id_by_path(tree_id, repo,
1641 worktree->base_commit_id, worktree->path_prefix);
1647 /* Check out a subset of files in the work tree. */
1649 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1650 is_root_wt ? "" : "/", wt_relpath) == -1) {
1651 err = got_error_from_errno("asprintf");
1655 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
1661 in_repo_path = NULL;
1663 err = got_object_get_type(entry_type, repo, id);
1667 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
1668 /* Check out a single file. */
1669 if (strchr(wt_relpath, '/') == NULL) {
1670 /* Check out a single file in work tree's root dir. */
1671 in_repo_path = strdup(worktree->path_prefix);
1672 if (in_repo_path == NULL) {
1673 err = got_error_from_errno("strdup");
1676 *tree_relpath = strdup("");
1677 if (*tree_relpath == NULL) {
1678 err = got_error_from_errno("strdup");
1682 /* Check out a single file in a subdirectory. */
1683 err = got_path_dirname(tree_relpath, wt_relpath);
1686 if (asprintf(&in_repo_path, "%s%s%s",
1687 worktree->path_prefix, is_root_wt ? "" : "/",
1688 *tree_relpath) == -1) {
1689 err = got_error_from_errno("asprintf");
1693 err = got_object_id_by_path(tree_id, repo,
1694 worktree->base_commit_id, in_repo_path);
1696 /* Check out all files within a subdirectory. */
1697 *tree_id = got_object_id_dup(id);
1698 if (*tree_id == NULL) {
1699 err = got_error_from_errno("got_object_id_dup");
1702 *tree_relpath = strdup(wt_relpath);
1703 if (*tree_relpath == NULL) {
1704 err = got_error_from_errno("strdup");
1712 *entry_type = GOT_OBJ_TYPE_ANY;
1713 free(*tree_relpath);
1714 *tree_relpath = NULL;
1721 static const struct got_error *
1722 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1723 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
1724 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1725 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1727 const struct got_error *err = NULL;
1728 struct got_commit_object *commit = NULL;
1729 struct got_tree_object *tree = NULL;
1730 struct got_fileindex_diff_tree_cb diff_cb;
1731 struct diff_cb_arg arg;
1733 err = ref_base_commit(worktree, repo);
1737 err = got_object_open_as_commit(&commit, repo,
1738 worktree->base_commit_id);
1742 err = got_object_open_as_tree(&tree, repo, tree_id);
1747 got_object_tree_find_entry(tree, entry_name) == NULL) {
1748 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1752 diff_cb.diff_old_new = diff_old_new;
1753 diff_cb.diff_old = diff_old;
1754 diff_cb.diff_new = diff_new;
1755 arg.fileindex = fileindex;
1756 arg.worktree = worktree;
1758 arg.progress_cb = progress_cb;
1759 arg.progress_arg = progress_arg;
1760 arg.cancel_cb = cancel_cb;
1761 arg.cancel_arg = cancel_arg;
1762 err = got_fileindex_diff_tree(fileindex, tree, relpath,
1763 entry_name, repo, &diff_cb, &arg);
1766 got_object_tree_close(tree);
1768 got_object_commit_close(commit);
1772 const struct got_error *
1773 got_worktree_checkout_files(struct got_worktree *worktree,
1774 struct got_pathlist_head *paths, struct got_repository *repo,
1775 got_worktree_checkout_cb progress_cb, void *progress_arg,
1776 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1778 const struct got_error *err = NULL, *sync_err, *unlockerr;
1779 struct got_commit_object *commit = NULL;
1780 struct got_tree_object *tree = NULL;
1781 struct got_fileindex *fileindex = NULL;
1782 char *fileindex_path = NULL;
1783 struct got_pathlist_entry *pe;
1784 struct tree_path_data {
1785 SIMPLEQ_ENTRY(tree_path_data) entry;
1786 struct got_object_id *tree_id;
1791 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
1793 SIMPLEQ_INIT(&tree_paths);
1795 err = lock_worktree(worktree, LOCK_EX);
1799 /* Map all specified paths to in-repository trees. */
1800 TAILQ_FOREACH(pe, paths, entry) {
1801 tpd = malloc(sizeof(*tpd));
1803 err = got_error_from_errno("malloc");
1807 err = find_tree_entry_for_checkout(&tpd->entry_type,
1808 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
1814 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
1815 err = got_path_basename(&tpd->entry_name, pe->path);
1823 tpd->entry_name = NULL;
1825 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
1829 * Read the file index.
1830 * Checking out files is supposed to be an idempotent operation.
1831 * If the on-disk file index is incomplete we will try to complete it.
1833 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1837 tpd = SIMPLEQ_FIRST(&tree_paths);
1838 TAILQ_FOREACH(pe, paths, entry) {
1839 struct bump_base_commit_id_arg bbc_arg;
1841 err = checkout_files(worktree, fileindex, tpd->relpath,
1842 tpd->tree_id, tpd->entry_name, repo,
1843 progress_cb, progress_arg, cancel_cb, cancel_arg);
1847 bbc_arg.base_commit_id = worktree->base_commit_id;
1848 bbc_arg.entry_name = tpd->entry_name;
1849 bbc_arg.path = pe->path;
1850 bbc_arg.path_len = strlen(pe->path);
1851 bbc_arg.progress_cb = progress_cb;
1852 bbc_arg.progress_arg = progress_arg;
1853 err = got_fileindex_for_each_entry_safe(fileindex,
1854 bump_base_commit_id, &bbc_arg);
1858 tpd = SIMPLEQ_NEXT(tpd, entry);
1860 sync_err = sync_fileindex(fileindex, fileindex_path);
1861 if (sync_err && err == NULL)
1864 free(fileindex_path);
1866 got_object_tree_close(tree);
1868 got_object_commit_close(commit);
1870 got_fileindex_free(fileindex);
1871 while (!SIMPLEQ_EMPTY(&tree_paths)) {
1872 tpd = SIMPLEQ_FIRST(&tree_paths);
1873 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
1878 unlockerr = lock_worktree(worktree, LOCK_SH);
1879 if (unlockerr && err == NULL)
1884 struct merge_file_cb_arg {
1885 struct got_worktree *worktree;
1886 struct got_fileindex *fileindex;
1887 got_worktree_checkout_cb progress_cb;
1889 got_worktree_cancel_cb cancel_cb;
1891 struct got_object_id *commit_id2;
1894 static const struct got_error *
1895 merge_file_cb(void *arg, struct got_blob_object *blob1,
1896 struct got_blob_object *blob2, struct got_object_id *id1,
1897 struct got_object_id *id2, const char *path1, const char *path2,
1898 struct got_repository *repo)
1900 static const struct got_error *err = NULL;
1901 struct merge_file_cb_arg *a = arg;
1902 struct got_fileindex_entry *ie;
1903 char *ondisk_path = NULL;
1905 unsigned char status;
1906 int local_changes_subsumed;
1908 if (blob1 && blob2) {
1909 ie = got_fileindex_entry_get(a->fileindex, path2);
1911 return (*a->progress_cb)(a->progress_arg,
1912 GOT_STATUS_MISSING, path2);
1914 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1916 return got_error_from_errno("asprintf");
1918 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1922 if (status == GOT_STATUS_DELETE) {
1923 err = (*a->progress_cb)(a->progress_arg,
1924 GOT_STATUS_MERGE, path2);
1927 if (status != GOT_STATUS_NO_CHANGE &&
1928 status != GOT_STATUS_MODIFY &&
1929 status != GOT_STATUS_CONFLICT &&
1930 status != GOT_STATUS_ADD) {
1931 err = (*a->progress_cb)(a->progress_arg, status, path2);
1935 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
1936 ondisk_path, path2, sb.st_mode, blob2, a->commit_id2, repo,
1937 a->progress_cb, a->progress_arg);
1939 ie = got_fileindex_entry_get(a->fileindex, path1);
1941 return (*a->progress_cb)(a->progress_arg,
1942 GOT_STATUS_MISSING, path2);
1944 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1946 return got_error_from_errno("asprintf");
1948 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1953 case GOT_STATUS_NO_CHANGE:
1954 err = (*a->progress_cb)(a->progress_arg,
1955 GOT_STATUS_DELETE, path1);
1958 err = remove_ondisk_file(a->worktree->root_path, path1);
1962 got_fileindex_entry_mark_deleted_from_disk(ie);
1964 case GOT_STATUS_DELETE:
1965 case GOT_STATUS_MISSING:
1966 err = (*a->progress_cb)(a->progress_arg,
1967 GOT_STATUS_DELETE, path1);
1971 got_fileindex_entry_mark_deleted_from_disk(ie);
1973 case GOT_STATUS_ADD:
1974 case GOT_STATUS_MODIFY:
1975 case GOT_STATUS_CONFLICT:
1976 err = (*a->progress_cb)(a->progress_arg,
1977 GOT_STATUS_CANNOT_DELETE, path1);
1981 case GOT_STATUS_OBSTRUCTED:
1982 err = (*a->progress_cb)(a->progress_arg, status, path1);
1990 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1992 return got_error_from_errno("asprintf");
1993 ie = got_fileindex_entry_get(a->fileindex, path2);
1995 err = get_file_status(&status, &sb, ie, ondisk_path,
1999 if (status != GOT_STATUS_NO_CHANGE &&
2000 status != GOT_STATUS_MODIFY &&
2001 status != GOT_STATUS_CONFLICT &&
2002 status != GOT_STATUS_ADD) {
2003 err = (*a->progress_cb)(a->progress_arg,
2007 err = merge_blob(&local_changes_subsumed, a->worktree,
2008 NULL, ondisk_path, path2, sb.st_mode, blob2,
2009 a->commit_id2, repo,
2010 a->progress_cb, a->progress_arg);
2011 if (status == GOT_STATUS_DELETE) {
2012 err = update_blob_fileindex_entry(a->worktree,
2013 a->fileindex, ie, ondisk_path, ie->path,
2019 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2020 err = install_blob(a->worktree, ondisk_path, path2,
2021 /* XXX get this from parent tree! */
2022 GOT_DEFAULT_FILE_MODE,
2023 sb.st_mode, blob2, 0, 0, repo,
2024 a->progress_cb, a->progress_arg);
2027 err = got_fileindex_entry_alloc(&ie,
2028 ondisk_path, path2, NULL, NULL);
2031 err = got_fileindex_entry_add(a->fileindex, ie);
2033 got_fileindex_entry_free(ie);
2043 struct check_merge_ok_arg {
2044 struct got_worktree *worktree;
2045 struct got_repository *repo;
2048 static const struct got_error *
2049 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2051 const struct got_error *err = NULL;
2052 struct check_merge_ok_arg *a = arg;
2053 unsigned char status;
2057 /* Reject merges into a work tree with mixed base commits. */
2058 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2059 SHA1_DIGEST_LENGTH))
2060 return got_error(GOT_ERR_MIXED_COMMITS);
2062 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2064 return got_error_from_errno("asprintf");
2066 /* Reject merges into a work tree with conflicted files. */
2067 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
2070 if (status == GOT_STATUS_CONFLICT)
2071 return got_error(GOT_ERR_CONFLICTS);
2076 static const struct got_error *
2077 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2078 const char *fileindex_path, struct got_object_id *commit_id1,
2079 struct got_object_id *commit_id2, struct got_repository *repo,
2080 got_worktree_checkout_cb progress_cb, void *progress_arg,
2081 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2083 const struct got_error *err = NULL, *sync_err;
2084 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2085 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2086 struct merge_file_cb_arg arg;
2089 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2090 worktree->path_prefix);
2094 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2099 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2100 worktree->path_prefix);
2104 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2108 arg.worktree = worktree;
2109 arg.fileindex = fileindex;
2110 arg.progress_cb = progress_cb;
2111 arg.progress_arg = progress_arg;
2112 arg.cancel_cb = cancel_cb;
2113 arg.cancel_arg = cancel_arg;
2114 arg.commit_id2 = commit_id2;
2115 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2116 sync_err = sync_fileindex(fileindex, fileindex_path);
2117 if (sync_err && err == NULL)
2121 got_object_tree_close(tree1);
2123 got_object_tree_close(tree2);
2127 const struct got_error *
2128 got_worktree_merge_files(struct got_worktree *worktree,
2129 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2130 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2131 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2133 const struct got_error *err, *unlockerr;
2134 char *fileindex_path = NULL;
2135 struct got_fileindex *fileindex = NULL;
2136 struct check_merge_ok_arg mok_arg;
2138 err = lock_worktree(worktree, LOCK_EX);
2142 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2146 mok_arg.worktree = worktree;
2147 mok_arg.repo = repo;
2148 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2153 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2154 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2157 got_fileindex_free(fileindex);
2158 free(fileindex_path);
2159 unlockerr = lock_worktree(worktree, LOCK_SH);
2160 if (unlockerr && err == NULL)
2165 struct diff_dir_cb_arg {
2166 struct got_fileindex *fileindex;
2167 struct got_worktree *worktree;
2168 const char *status_path;
2169 size_t status_path_len;
2170 struct got_repository *repo;
2171 got_worktree_status_cb status_cb;
2173 got_worktree_cancel_cb cancel_cb;
2177 static const struct got_error *
2178 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2179 got_worktree_status_cb status_cb, void *status_arg,
2180 struct got_repository *repo)
2182 const struct got_error *err = NULL;
2183 unsigned char status = GOT_STATUS_NO_CHANGE;
2185 struct got_object_id blob_id, commit_id;
2187 err = get_file_status(&status, &sb, ie, abspath, repo);
2188 if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
2189 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2190 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2191 err = (*status_cb)(status_arg, status, ie->path, &blob_id,
2197 static const struct got_error *
2198 status_old_new(void *arg, struct got_fileindex_entry *ie,
2199 struct dirent *de, const char *parent_path)
2201 const struct got_error *err = NULL;
2202 struct diff_dir_cb_arg *a = arg;
2205 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2206 return got_error(GOT_ERR_CANCELLED);
2208 if (got_path_cmp(parent_path, a->status_path) != 0 &&
2209 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2212 if (parent_path[0]) {
2213 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2214 parent_path, de->d_name) == -1)
2215 return got_error_from_errno("asprintf");
2217 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2219 return got_error_from_errno("asprintf");
2222 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
2228 static const struct got_error *
2229 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2231 struct diff_dir_cb_arg *a = arg;
2232 struct got_object_id blob_id, commit_id;
2233 unsigned char status;
2235 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2236 return got_error(GOT_ERR_CANCELLED);
2238 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2241 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2242 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2243 if (got_fileindex_entry_has_file_on_disk(ie))
2244 status = GOT_STATUS_MISSING;
2246 status = GOT_STATUS_DELETE;
2247 return (*a->status_cb)(a->status_arg, status, ie->path, &blob_id,
2251 static const struct got_error *
2252 status_new(void *arg, struct dirent *de, const char *parent_path)
2254 const struct got_error *err = NULL;
2255 struct diff_dir_cb_arg *a = arg;
2258 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2259 return got_error(GOT_ERR_CANCELLED);
2261 if (de->d_type == DT_DIR)
2264 /* XXX ignore symlinks for now */
2265 if (de->d_type == DT_LNK)
2268 if (parent_path[0]) {
2269 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2270 return got_error_from_errno("asprintf");
2275 if (got_path_is_child(path, a->status_path, a->status_path_len))
2276 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2283 static const struct got_error *
2284 report_single_file_status(const char *path, const char *ondisk_path,
2285 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2286 void *status_arg, struct got_repository *repo)
2288 struct got_fileindex_entry *ie;
2291 ie = got_fileindex_entry_get(fileindex, path);
2293 return report_file_status(ie, ondisk_path, status_cb,
2296 if (lstat(ondisk_path, &sb) == -1) {
2297 if (errno != ENOENT)
2298 return got_error_from_errno2("lstat", ondisk_path);
2302 if (S_ISREG(sb.st_mode))
2303 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED, path,
2309 static const struct got_error *
2310 worktree_status(struct got_worktree *worktree, const char *path,
2311 struct got_fileindex *fileindex, struct got_repository *repo,
2312 got_worktree_status_cb status_cb, void *status_arg,
2313 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2315 const struct got_error *err = NULL;
2316 DIR *workdir = NULL;
2317 struct got_fileindex_diff_dir_cb fdiff_cb;
2318 struct diff_dir_cb_arg arg;
2319 char *ondisk_path = NULL;
2321 if (asprintf(&ondisk_path, "%s%s%s",
2322 worktree->root_path, path[0] ? "/" : "", path) == -1) {
2323 err = got_error_from_errno("asprintf");
2326 workdir = opendir(ondisk_path);
2327 if (workdir == NULL) {
2328 if (errno != ENOTDIR && errno != ENOENT)
2329 err = got_error_from_errno2("opendir", ondisk_path);
2331 err = report_single_file_status(path, ondisk_path,
2332 fileindex, status_cb, status_arg, repo);
2334 fdiff_cb.diff_old_new = status_old_new;
2335 fdiff_cb.diff_old = status_old;
2336 fdiff_cb.diff_new = status_new;
2337 arg.fileindex = fileindex;
2338 arg.worktree = worktree;
2339 arg.status_path = path;
2340 arg.status_path_len = strlen(path);
2342 arg.status_cb = status_cb;
2343 arg.status_arg = status_arg;
2344 arg.cancel_cb = cancel_cb;
2345 arg.cancel_arg = cancel_arg;
2346 err = got_fileindex_diff_dir(fileindex, workdir,
2347 worktree->root_path, path, repo, &fdiff_cb, &arg);
2356 const struct got_error *
2357 got_worktree_status(struct got_worktree *worktree,
2358 struct got_pathlist_head *paths, struct got_repository *repo,
2359 got_worktree_status_cb status_cb, void *status_arg,
2360 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2362 const struct got_error *err = NULL;
2363 char *fileindex_path = NULL;
2364 struct got_fileindex *fileindex = NULL;
2365 struct got_pathlist_entry *pe;
2367 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2371 TAILQ_FOREACH(pe, paths, entry) {
2372 err = worktree_status(worktree, pe->path, fileindex, repo,
2373 status_cb, status_arg, cancel_cb, cancel_arg);
2377 free(fileindex_path);
2378 got_fileindex_free(fileindex);
2382 const struct got_error *
2383 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2386 const struct got_error *err = NULL;
2387 char *resolved, *cwd = NULL, *path = NULL;
2392 resolved = realpath(arg, NULL);
2393 if (resolved == NULL) {
2394 if (errno != ENOENT)
2395 return got_error_from_errno2("realpath", arg);
2396 cwd = getcwd(NULL, 0);
2398 return got_error_from_errno("getcwd");
2399 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
2400 err = got_error_from_errno("asprintf");
2405 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2406 strlen(got_worktree_get_root_path(worktree)))) {
2407 err = got_error(GOT_ERR_BAD_PATH);
2411 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2412 err = got_path_skip_common_ancestor(&path,
2413 got_worktree_get_root_path(worktree), resolved);
2419 err = got_error_from_errno("strdup");
2424 /* XXX status walk can't deal with trailing slash! */
2426 while (len > 0 && path[len - 1] == '/') {
2427 path[len - 1] = '\0';
2440 static const struct got_error *
2441 schedule_addition(const char *ondisk_path, struct got_fileindex *fileindex,
2442 const char *relpath, got_worktree_status_cb status_cb, void *status_arg,
2443 struct got_repository *repo)
2445 const struct got_error *err = NULL;
2446 struct got_fileindex_entry *ie;
2448 /* Re-adding an existing entry is a no-op. */
2449 if (got_fileindex_entry_get(fileindex, relpath) != NULL)
2452 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2456 err = got_fileindex_entry_add(fileindex, ie);
2458 got_fileindex_entry_free(ie);
2462 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2465 const struct got_error *
2466 got_worktree_schedule_add(struct got_worktree *worktree,
2467 struct got_pathlist_head *ondisk_paths,
2468 got_worktree_status_cb status_cb, void *status_arg,
2469 struct got_repository *repo)
2471 struct got_fileindex *fileindex = NULL;
2472 char *fileindex_path = NULL;
2473 const struct got_error *err = NULL, *sync_err, *unlockerr;
2474 struct got_pathlist_entry *pe;
2476 err = lock_worktree(worktree, LOCK_EX);
2480 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2484 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2486 err = got_path_skip_common_ancestor(&relpath,
2487 got_worktree_get_root_path(worktree), pe->path);
2490 err = schedule_addition(pe->path, fileindex, relpath,
2491 status_cb, status_arg, repo);
2496 sync_err = sync_fileindex(fileindex, fileindex_path);
2497 if (sync_err && err == NULL)
2500 free(fileindex_path);
2502 got_fileindex_free(fileindex);
2503 unlockerr = lock_worktree(worktree, LOCK_SH);
2504 if (unlockerr && err == NULL)
2509 static const struct got_error *
2510 schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
2511 const char *relpath, int delete_local_mods,
2512 got_worktree_status_cb status_cb, void *status_arg,
2513 struct got_repository *repo)
2515 const struct got_error *err = NULL;
2516 struct got_fileindex_entry *ie = NULL;
2517 unsigned char status;
2520 ie = got_fileindex_entry_get(fileindex, relpath);
2522 return got_error(GOT_ERR_BAD_PATH);
2524 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2528 if (status != GOT_STATUS_NO_CHANGE) {
2529 if (status == GOT_STATUS_DELETE)
2530 return got_error_set_errno(ENOENT, ondisk_path);
2531 if (status != GOT_STATUS_MODIFY)
2532 return got_error(GOT_ERR_FILE_STATUS);
2533 if (!delete_local_mods)
2534 return got_error(GOT_ERR_FILE_MODIFIED);
2537 if (unlink(ondisk_path) != 0)
2538 return got_error_from_errno2("unlink", ondisk_path);
2540 got_fileindex_entry_mark_deleted_from_disk(ie);
2541 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2544 const struct got_error *
2545 got_worktree_schedule_delete(struct got_worktree *worktree,
2546 struct got_pathlist_head *ondisk_paths, int delete_local_mods,
2547 got_worktree_status_cb status_cb, void *status_arg,
2548 struct got_repository *repo)
2550 struct got_fileindex *fileindex = NULL;
2551 char *fileindex_path = NULL;
2552 const struct got_error *err = NULL, *sync_err, *unlockerr;
2553 struct got_pathlist_entry *pe;
2555 err = lock_worktree(worktree, LOCK_EX);
2559 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2563 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2565 err = got_path_skip_common_ancestor(&relpath,
2566 got_worktree_get_root_path(worktree), pe->path);
2569 err = schedule_for_deletion(pe->path, fileindex, relpath,
2570 delete_local_mods, status_cb, status_arg, repo);
2575 sync_err = sync_fileindex(fileindex, fileindex_path);
2576 if (sync_err && err == NULL)
2579 free(fileindex_path);
2581 got_fileindex_free(fileindex);
2582 unlockerr = lock_worktree(worktree, LOCK_SH);
2583 if (unlockerr && err == NULL)
2588 static const struct got_error *
2589 revert_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2590 const char *ondisk_path,
2591 got_worktree_checkout_cb progress_cb, void *progress_arg,
2592 struct got_repository *repo)
2594 const struct got_error *err = NULL;
2595 char *relpath = NULL, *parent_path = NULL;
2596 struct got_fileindex_entry *ie;
2597 struct got_tree_object *tree = NULL;
2598 struct got_object_id *tree_id = NULL;
2599 const struct got_tree_entry *te;
2600 char *tree_path = NULL, *te_name;
2601 struct got_blob_object *blob = NULL;
2602 unsigned char status;
2605 err = got_path_skip_common_ancestor(&relpath,
2606 got_worktree_get_root_path(worktree), ondisk_path);
2610 ie = got_fileindex_entry_get(fileindex, relpath);
2612 err = got_error(GOT_ERR_BAD_PATH);
2616 /* Construct in-repository path of tree which contains this blob. */
2617 err = got_path_dirname(&parent_path, ie->path);
2619 if (err->code != GOT_ERR_BAD_PATH)
2621 parent_path = strdup("/");
2622 if (parent_path == NULL) {
2623 err = got_error_from_errno("strdup");
2627 if (got_path_is_root_dir(worktree->path_prefix)) {
2628 tree_path = strdup(parent_path);
2629 if (tree_path == NULL) {
2630 err = got_error_from_errno("strdup");
2634 if (got_path_is_root_dir(parent_path)) {
2635 tree_path = strdup(worktree->path_prefix);
2636 if (tree_path == NULL) {
2637 err = got_error_from_errno("strdup");
2641 if (asprintf(&tree_path, "%s/%s",
2642 worktree->path_prefix, parent_path) == -1) {
2643 err = got_error_from_errno("asprintf");
2649 err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2654 err = got_object_open_as_tree(&tree, repo, tree_id);
2658 te_name = basename(ie->path);
2659 if (te_name == NULL) {
2660 err = got_error_from_errno2("basename", ie->path);
2664 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2668 te = got_object_tree_find_entry(tree, te_name);
2669 if (te == NULL && status != GOT_STATUS_ADD) {
2670 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2675 case GOT_STATUS_ADD:
2676 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2679 got_fileindex_entry_remove(fileindex, ie);
2681 case GOT_STATUS_DELETE:
2682 case GOT_STATUS_MODIFY:
2683 case GOT_STATUS_CONFLICT:
2684 case GOT_STATUS_MISSING: {
2685 struct got_object_id id;
2686 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2687 err = got_object_open_as_blob(&blob, repo, &id, 8192);
2690 err = install_blob(worktree, ondisk_path, ie->path,
2691 te->mode, sb.st_mode, blob, 0, 1, repo, progress_cb,
2695 if (status == GOT_STATUS_DELETE) {
2696 err = update_blob_fileindex_entry(worktree,
2697 fileindex, ie, ondisk_path, ie->path, blob, 1);
2711 got_object_blob_close(blob);
2713 got_object_tree_close(tree);
2718 const struct got_error *
2719 got_worktree_revert(struct got_worktree *worktree,
2720 struct got_pathlist_head *ondisk_paths,
2721 got_worktree_checkout_cb progress_cb, void *progress_arg,
2722 struct got_repository *repo)
2724 struct got_fileindex *fileindex = NULL;
2725 char *fileindex_path = NULL;
2726 const struct got_error *err = NULL, *unlockerr = NULL;
2727 const struct got_error *sync_err = NULL;
2728 struct got_pathlist_entry *pe;
2730 err = lock_worktree(worktree, LOCK_EX);
2734 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2738 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2739 err = revert_file(worktree, fileindex, pe->path,
2740 progress_cb, progress_arg, repo);
2744 sync_err = sync_fileindex(fileindex, fileindex_path);
2745 if (sync_err && err == NULL)
2748 free(fileindex_path);
2750 got_fileindex_free(fileindex);
2751 unlockerr = lock_worktree(worktree, LOCK_SH);
2752 if (unlockerr && err == NULL)
2758 free_commitable(struct got_commitable *ct)
2761 free(ct->in_repo_path);
2762 free(ct->ondisk_path);
2764 free(ct->base_blob_id);
2765 free(ct->base_commit_id);
2769 struct collect_commitables_arg {
2770 struct got_pathlist_head *commitable_paths;
2771 struct got_repository *repo;
2772 struct got_worktree *worktree;
2775 static const struct got_error *
2776 collect_commitables(void *arg, unsigned char status, const char *relpath,
2777 struct got_object_id *blob_id, struct got_object_id *commit_id)
2779 struct collect_commitables_arg *a = arg;
2780 const struct got_error *err = NULL;
2781 struct got_commitable *ct = NULL;
2782 struct got_pathlist_entry *new = NULL;
2783 char *parent_path = NULL, *path = NULL;
2786 if (status == GOT_STATUS_CONFLICT)
2787 return got_error(GOT_ERR_COMMIT_CONFLICT);
2789 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
2790 status != GOT_STATUS_DELETE)
2793 if (asprintf(&path, "/%s", relpath) == -1) {
2794 err = got_error_from_errno("asprintf");
2797 if (strcmp(path, "/") == 0) {
2798 parent_path = strdup("");
2799 if (parent_path == NULL)
2800 return got_error_from_errno("strdup");
2802 err = got_path_dirname(&parent_path, path);
2807 ct = calloc(1, sizeof(*ct));
2809 err = got_error_from_errno("calloc");
2813 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
2815 err = got_error_from_errno("asprintf");
2818 if (status == GOT_STATUS_DELETE) {
2819 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2821 if (lstat(ct->ondisk_path, &sb) != 0) {
2822 err = got_error_from_errno2("lstat", ct->ondisk_path);
2825 ct->mode = sb.st_mode;
2828 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
2829 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
2831 err = got_error_from_errno("asprintf");
2835 ct->status = status;
2836 ct->blob_id = NULL; /* will be filled in when blob gets created */
2837 if (ct->status != GOT_STATUS_ADD) {
2838 ct->base_blob_id = got_object_id_dup(blob_id);
2839 if (ct->base_blob_id == NULL) {
2840 err = got_error_from_errno("got_object_id_dup");
2843 ct->base_commit_id = got_object_id_dup(commit_id);
2844 if (ct->base_commit_id == NULL) {
2845 err = got_error_from_errno("got_object_id_dup");
2849 ct->path = strdup(path);
2850 if (ct->path == NULL) {
2851 err = got_error_from_errno("strdup");
2854 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
2856 if (ct && (err || new == NULL))
2857 free_commitable(ct);
2863 static const struct got_error *write_tree(struct got_object_id **,
2864 struct got_tree_object *, const char *, struct got_pathlist_head *,
2865 got_worktree_status_cb status_cb, void *status_arg,
2866 struct got_repository *);
2868 static const struct got_error *
2869 write_subtree(struct got_object_id **new_subtree_id,
2870 struct got_tree_entry *te, const char *parent_path,
2871 struct got_pathlist_head *commitable_paths,
2872 got_worktree_status_cb status_cb, void *status_arg,
2873 struct got_repository *repo)
2875 const struct got_error *err = NULL;
2876 struct got_tree_object *subtree;
2879 if (asprintf(&subpath, "%s%s%s", parent_path,
2880 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
2881 return got_error_from_errno("asprintf");
2883 err = got_object_open_as_tree(&subtree, repo, te->id);
2887 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
2888 status_cb, status_arg, repo);
2889 got_object_tree_close(subtree);
2894 static const struct got_error *
2895 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
2897 const struct got_error *err = NULL;
2898 char *ct_parent_path = NULL;
2902 if (strchr(ct->in_repo_path, '/') == NULL) {
2903 *match = got_path_is_root_dir(path);
2907 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
2910 *match = (strcmp(path, ct_parent_path) == 0);
2911 free(ct_parent_path);
2916 get_ct_file_mode(struct got_commitable *ct)
2918 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
2921 static const struct got_error *
2922 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
2923 struct got_tree_entry *te, struct got_commitable *ct)
2925 const struct got_error *err = NULL;
2929 err = got_object_tree_entry_dup(new_te, te);
2933 (*new_te)->mode = get_ct_file_mode(ct);
2935 free((*new_te)->id);
2936 (*new_te)->id = got_object_id_dup(ct->blob_id);
2937 if ((*new_te)->id == NULL) {
2938 err = got_error_from_errno("got_object_id_dup");
2942 if (err && *new_te) {
2943 got_object_tree_entry_close(*new_te);
2949 static const struct got_error *
2950 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2951 struct got_commitable *ct)
2953 const struct got_error *err = NULL;
2958 *new_te = calloc(1, sizeof(**new_te));
2959 if (*new_te == NULL)
2960 return got_error_from_errno("calloc");
2962 ct_name = basename(ct->path);
2963 if (ct_name == NULL) {
2964 err = got_error_from_errno2("basename", ct->path);
2967 (*new_te)->name = strdup(ct_name);
2968 if ((*new_te)->name == NULL) {
2969 err = got_error_from_errno("strdup");
2973 (*new_te)->mode = get_ct_file_mode(ct);
2975 (*new_te)->id = got_object_id_dup(ct->blob_id);
2976 if ((*new_te)->id == NULL) {
2977 err = got_error_from_errno("got_object_id_dup");
2981 if (err && *new_te) {
2982 got_object_tree_entry_close(*new_te);
2988 static const struct got_error *
2989 insert_tree_entry(struct got_tree_entry *new_te,
2990 struct got_pathlist_head *paths)
2992 const struct got_error *err = NULL;
2993 struct got_pathlist_entry *new_pe;
2995 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2999 return got_error(GOT_ERR_TREE_DUP_ENTRY);
3003 static const struct got_error *
3004 report_ct_status(struct got_commitable *ct,
3005 got_worktree_status_cb status_cb, void *status_arg)
3007 const char *ct_path = ct->path;
3008 while (ct_path[0] == '/')
3010 return (*status_cb)(status_arg, ct->status, ct_path, ct->blob_id, NULL);
3013 static const struct got_error *
3014 match_modified_subtree(int *modified, struct got_tree_entry *te,
3015 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
3017 const struct got_error *err = NULL;
3018 struct got_pathlist_entry *pe;
3023 if (asprintf(&te_path, "%s%s%s", base_tree_path,
3024 got_path_is_root_dir(base_tree_path) ? "" : "/",
3026 return got_error_from_errno("asprintf");
3028 TAILQ_FOREACH(pe, commitable_paths, entry) {
3029 struct got_commitable *ct = pe->data;
3030 *modified = got_path_is_child(ct->in_repo_path, te_path,
3040 static const struct got_error *
3041 match_deleted_or_modified_ct(struct got_commitable **ctp,
3042 struct got_tree_entry *te, const char *base_tree_path,
3043 struct got_pathlist_head *commitable_paths)
3045 const struct got_error *err = NULL;
3046 struct got_pathlist_entry *pe;
3050 TAILQ_FOREACH(pe, commitable_paths, entry) {
3051 struct got_commitable *ct = pe->data;
3052 char *ct_name = NULL;
3055 if (ct->status != GOT_STATUS_MODIFY &&
3056 ct->status != GOT_STATUS_DELETE)
3059 if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
3062 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
3068 ct_name = basename(pe->path);
3069 if (ct_name == NULL)
3070 return got_error_from_errno2("basename", pe->path);
3072 if (strcmp(te->name, ct_name) != 0)
3082 static const struct got_error *
3083 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
3084 const char *child_path, const char *path_base_tree,
3085 struct got_pathlist_head *commitable_paths,
3086 got_worktree_status_cb status_cb, void *status_arg,
3087 struct got_repository *repo)
3089 const struct got_error *err = NULL;
3090 struct got_tree_entry *new_te;
3095 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
3096 got_path_is_root_dir(path_base_tree) ? "" : "/",
3098 return got_error_from_errno("asprintf");
3100 new_te = calloc(1, sizeof(*new_te));
3101 new_te->mode = S_IFDIR;
3102 new_te->name = strdup(child_path);
3103 if (new_te->name == NULL) {
3104 err = got_error_from_errno("strdup");
3105 got_object_tree_entry_close(new_te);
3108 err = write_tree(&new_te->id, NULL, subtree_path,
3109 commitable_paths, status_cb, status_arg, repo);
3111 got_object_tree_entry_close(new_te);
3121 static const struct got_error *
3122 write_tree(struct got_object_id **new_tree_id,
3123 struct got_tree_object *base_tree, const char *path_base_tree,
3124 struct got_pathlist_head *commitable_paths,
3125 got_worktree_status_cb status_cb, void *status_arg,
3126 struct got_repository *repo)
3128 const struct got_error *err = NULL;
3129 const struct got_tree_entries *base_entries = NULL;
3130 struct got_pathlist_head paths;
3131 struct got_tree_entries new_tree_entries;
3132 struct got_tree_entry *te, *new_te = NULL;
3133 struct got_pathlist_entry *pe;
3136 new_tree_entries.nentries = 0;
3137 SIMPLEQ_INIT(&new_tree_entries.head);
3139 /* Insert, and recurse into, newly added entries first. */
3140 TAILQ_FOREACH(pe, commitable_paths, entry) {
3141 struct got_commitable *ct = pe->data;
3142 char *child_path = NULL, *slash;
3144 if (ct->status != GOT_STATUS_ADD ||
3145 (ct->flags & GOT_COMMITABLE_ADDED))
3148 if (!got_path_is_child(pe->path, path_base_tree,
3149 strlen(path_base_tree)))
3152 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
3157 slash = strchr(child_path, '/');
3158 if (slash == NULL) {
3159 err = alloc_added_blob_tree_entry(&new_te, ct);
3162 err = report_ct_status(ct, status_cb, status_arg);
3165 ct->flags |= GOT_COMMITABLE_ADDED;
3166 err = insert_tree_entry(new_te, &paths);
3170 *slash = '\0'; /* trim trailing path components */
3171 if (base_tree == NULL ||
3172 got_object_tree_find_entry(base_tree, child_path)
3174 err = make_subtree_for_added_blob(&new_te,
3175 child_path, path_base_tree,
3176 commitable_paths, status_cb, status_arg,
3180 err = insert_tree_entry(new_te, &paths);
3188 /* Handle modified and deleted entries. */
3189 base_entries = got_object_tree_get_entries(base_tree);
3190 SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
3191 struct got_commitable *ct = NULL;
3193 if (S_ISDIR(te->mode)) {
3195 err = got_object_tree_entry_dup(&new_te, te);
3198 err = match_modified_subtree(&modified, te,
3199 path_base_tree, commitable_paths);
3202 /* Avoid recursion into unmodified subtrees. */
3205 err = write_subtree(&new_te->id, te,
3206 path_base_tree, commitable_paths,
3207 status_cb, status_arg, repo);
3211 err = insert_tree_entry(new_te, &paths);
3217 err = match_deleted_or_modified_ct(&ct, te,
3218 path_base_tree, commitable_paths);
3220 /* NB: Deleted entries get dropped here. */
3221 if (ct->status == GOT_STATUS_MODIFY) {
3222 err = alloc_modified_blob_tree_entry(
3226 err = insert_tree_entry(new_te, &paths);
3230 err = report_ct_status(ct, status_cb,
3235 /* Entry is unchanged; just copy it. */
3236 err = got_object_tree_entry_dup(&new_te, te);
3239 err = insert_tree_entry(new_te, &paths);
3246 /* Write new list of entries; deleted entries have been dropped. */
3247 TAILQ_FOREACH(pe, &paths, entry) {
3248 struct got_tree_entry *te = pe->data;
3249 new_tree_entries.nentries++;
3250 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
3252 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
3254 got_object_tree_entries_close(&new_tree_entries);
3255 got_pathlist_free(&paths);
3259 static const struct got_error *
3260 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
3261 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex)
3263 const struct got_error *err = NULL;
3264 struct got_pathlist_entry *pe;
3266 TAILQ_FOREACH(pe, commitable_paths, entry) {
3267 struct got_fileindex_entry *ie;
3268 struct got_commitable *ct = pe->data;
3270 ie = got_fileindex_entry_get(fileindex, pe->path);
3272 if (ct->status == GOT_STATUS_DELETE) {
3273 got_fileindex_entry_remove(fileindex, ie);
3274 got_fileindex_entry_free(ie);
3276 err = got_fileindex_entry_update(ie,
3277 ct->ondisk_path, ct->blob_id->sha1,
3278 new_base_commit_id->sha1, 1);
3280 err = got_fileindex_entry_alloc(&ie,
3281 ct->ondisk_path, pe->path, ct->blob_id->sha1,
3282 new_base_commit_id->sha1);
3285 err = got_fileindex_entry_add(fileindex, ie);
3293 static const struct got_error *
3294 check_ct_out_of_date(struct got_commitable *ct, struct got_repository *repo,
3295 struct got_object_id *head_commit_id)
3297 const struct got_error *err = NULL;
3298 struct got_object_id *id_in_head = NULL, *id = NULL;
3299 struct got_commit_object *commit = NULL;
3301 const char *ct_path = ct->in_repo_path;
3303 while (ct_path[0] == '/')
3307 * Ensure that no modifications were made to files *and their parents*
3308 * in commits between the file's base commit and the branch head.
3310 * Checking the parents is important for detecting conflicting tree
3311 * configurations (files or parent folders might have been moved,
3312 * deleted, added again, etc.). Such changes need to be merged with
3313 * local changes before a commit can occur.
3315 * The implication is that the file's (parent) entry in the root
3316 * directory must have the same ID in all relevant commits.
3318 if (ct->status != GOT_STATUS_ADD) {
3319 struct got_object_qid *pid;
3321 struct got_object_id *root_entry_id = NULL;
3323 /* Trivial case: base commit == head commit */
3324 if (got_object_id_cmp(ct->base_commit_id, head_commit_id) == 0)
3327 /* Compute the path to the root directory's entry. */
3328 path = strdup(ct_path);
3330 err = got_error_from_errno("strdup");
3333 slash = strchr(path, '/');
3337 err = got_object_open_as_commit(&commit, repo, head_commit_id);
3341 err = got_object_id_by_path(&root_entry_id, repo,
3342 head_commit_id, path);
3346 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3348 struct got_commit_object *pcommit;
3350 err = got_object_id_by_path(&id, repo, pid->id, path);
3352 if (err->code != GOT_ERR_NO_TREE_ENTRY)
3358 err = got_object_id_by_path(&id, repo, pid->id, path);
3362 if (got_object_id_cmp(id, root_entry_id) != 0) {
3363 err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
3367 if (got_object_id_cmp(pid->id, ct->base_commit_id) == 0)
3368 break; /* all relevant commits scanned */
3370 err = got_object_open_as_commit(&pcommit, repo,
3375 got_object_commit_close(commit);
3377 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(
3381 /* Require that added files don't exist in the branch head. */
3382 err = got_object_id_by_path(&id_in_head, repo, head_commit_id,
3384 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3386 err = id_in_head ? got_error(GOT_ERR_COMMIT_OUT_OF_DATE) : NULL;
3390 got_object_commit_close(commit);
3397 const struct got_error *
3398 commit_worktree(struct got_object_id **new_commit_id,
3399 struct got_pathlist_head *commitable_paths,
3400 struct got_object_id *head_commit_id, struct got_worktree *worktree,
3401 const char *ondisk_path, const char *author, const char *committer,
3402 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3403 got_worktree_status_cb status_cb, void *status_arg,
3404 struct got_repository *repo)
3406 const struct got_error *err = NULL, *unlockerr = NULL;
3407 struct got_pathlist_entry *pe;
3408 const char *head_ref_name = NULL;
3409 struct got_commit_object *head_commit = NULL;
3410 struct got_reference *head_ref2 = NULL;
3411 struct got_object_id *head_commit_id2 = NULL;
3412 struct got_tree_object *head_tree = NULL;
3413 struct got_object_id *new_tree_id = NULL;
3414 struct got_object_id_queue parent_ids;
3415 struct got_object_qid *pid = NULL;
3416 char *logmsg = NULL;
3418 *new_commit_id = NULL;
3420 SIMPLEQ_INIT(&parent_ids);
3422 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
3426 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
3430 if (commit_msg_cb != NULL) {
3431 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
3436 if (logmsg == NULL || strlen(logmsg) == 0) {
3437 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
3441 /* Create blobs from added and modified files and record their IDs. */
3442 TAILQ_FOREACH(pe, commitable_paths, entry) {
3443 struct got_commitable *ct = pe->data;
3446 if (ct->status != GOT_STATUS_ADD &&
3447 ct->status != GOT_STATUS_MODIFY)
3450 if (asprintf(&ondisk_path, "%s/%s",
3451 worktree->root_path, pe->path) == -1) {
3452 err = got_error_from_errno("asprintf");
3455 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
3461 /* Recursively write new tree objects. */
3462 err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
3463 status_cb, status_arg, repo);
3467 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
3470 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
3471 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
3472 1, author, time(NULL), committer, time(NULL), logmsg, repo);
3473 got_object_qid_free(pid);
3479 /* Check if a concurrent commit to our branch has occurred. */
3480 head_ref_name = got_worktree_get_head_ref_name(worktree);
3481 if (head_ref_name == NULL) {
3482 err = got_error_from_errno("got_worktree_get_head_ref_name");
3485 /* Lock the reference here to prevent concurrent modification. */
3486 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
3489 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
3492 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
3493 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
3496 /* Update branch head in repository. */
3497 err = got_ref_change_ref(head_ref2, *new_commit_id);
3500 err = got_ref_write(head_ref2, repo);
3504 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
3508 err = ref_base_commit(worktree, repo);
3513 got_object_tree_close(head_tree);
3515 got_object_commit_close(head_commit);
3516 free(head_commit_id2);
3518 unlockerr = got_ref_unlock(head_ref2);
3519 if (unlockerr && err == NULL)
3521 got_ref_close(head_ref2);
3526 const struct got_error *
3527 got_worktree_commit(struct got_object_id **new_commit_id,
3528 struct got_worktree *worktree, const char *ondisk_path,
3529 const char *author, const char *committer,
3530 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3531 got_worktree_status_cb status_cb, void *status_arg,
3532 struct got_repository *repo)
3534 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
3535 struct got_fileindex *fileindex = NULL;
3536 char *fileindex_path = NULL, *relpath = NULL;
3537 struct got_pathlist_head commitable_paths;
3538 struct collect_commitables_arg cc_arg;
3539 struct got_pathlist_entry *pe;
3540 struct got_reference *head_ref = NULL;
3541 struct got_object_id *head_commit_id = NULL;
3543 *new_commit_id = NULL;
3545 TAILQ_INIT(&commitable_paths);
3547 err = lock_worktree(worktree, LOCK_EX);
3551 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
3555 err = got_ref_resolve(&head_commit_id, repo, head_ref);
3560 if (strcmp(ondisk_path, worktree->root_path) == 0) {
3561 relpath = strdup("");
3562 if (relpath == NULL) {
3563 err = got_error_from_errno("strdup");
3567 err = got_path_skip_common_ancestor(&relpath,
3568 worktree->root_path, ondisk_path);
3574 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3578 cc_arg.commitable_paths = &commitable_paths;
3579 cc_arg.worktree = worktree;
3581 err = worktree_status(worktree, relpath ? relpath : "",
3582 fileindex, repo, collect_commitables, &cc_arg, NULL, NULL);
3586 if (TAILQ_EMPTY(&commitable_paths)) {
3587 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
3591 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3592 struct got_commitable *ct = pe->data;
3593 err = check_ct_out_of_date(ct, repo, head_commit_id);
3598 err = commit_worktree(new_commit_id, &commitable_paths,
3599 head_commit_id, worktree, ondisk_path, author, committer,
3600 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
3604 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
3606 sync_err = sync_fileindex(fileindex, fileindex_path);
3607 if (sync_err && err == NULL)
3611 got_fileindex_free(fileindex);
3612 free(fileindex_path);
3614 unlockerr = lock_worktree(worktree, LOCK_SH);
3615 if (unlockerr && err == NULL)
3617 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3618 struct got_commitable *ct = pe->data;
3619 free_commitable(ct);
3621 got_pathlist_free(&commitable_paths);
3626 got_commitable_get_path(struct got_commitable *ct)
3632 got_commitable_get_status(struct got_commitable *ct)
3637 struct check_rebase_ok_arg {
3638 struct got_worktree *worktree;
3639 struct got_repository *repo;
3642 static const struct got_error *
3643 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
3645 const struct got_error *err = NULL;
3646 struct check_rebase_ok_arg *a = arg;
3647 unsigned char status;
3651 /* Reject rebase of a work tree with mixed base commits. */
3652 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3653 SHA1_DIGEST_LENGTH))
3654 return got_error(GOT_ERR_MIXED_COMMITS);
3656 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3658 return got_error_from_errno("asprintf");
3660 /* Reject rebase of a work tree with modified or conflicted files. */
3661 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
3666 if (status != GOT_STATUS_NO_CHANGE)
3667 return got_error(GOT_ERR_MODIFIED);
3672 const struct got_error *
3673 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
3674 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
3675 struct got_worktree *worktree, struct got_reference *branch,
3676 struct got_repository *repo)
3678 const struct got_error *err = NULL;
3679 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
3680 char *branch_ref_name = NULL;
3681 char *fileindex_path = NULL;
3682 struct check_rebase_ok_arg ok_arg;
3683 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
3685 *new_base_branch_ref = NULL;
3689 err = lock_worktree(worktree, LOCK_EX);
3693 err = open_fileindex(fileindex, &fileindex_path, worktree);
3697 ok_arg.worktree = worktree;
3699 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
3704 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3708 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3712 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3716 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
3721 err = got_ref_alloc_symref(new_base_branch_ref,
3722 new_base_branch_ref_name, wt_branch);
3725 err = got_ref_write(*new_base_branch_ref, repo);
3729 /* TODO Lock original branch's ref while rebasing? */
3731 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
3735 err = got_ref_write(branch_ref, repo);
3739 err = got_ref_alloc(tmp_branch, tmp_branch_name,
3740 worktree->base_commit_id);
3743 err = got_ref_write(*tmp_branch, repo);
3747 err = got_worktree_set_head_ref(worktree, *tmp_branch);
3751 free(fileindex_path);
3752 free(tmp_branch_name);
3753 free(new_base_branch_ref_name);
3754 free(branch_ref_name);
3756 got_ref_close(branch_ref);
3758 got_ref_close(wt_branch);
3760 if (*new_base_branch_ref) {
3761 got_ref_close(*new_base_branch_ref);
3762 *new_base_branch_ref = NULL;
3765 got_ref_close(*tmp_branch);
3769 got_fileindex_free(*fileindex);
3772 lock_worktree(worktree, LOCK_SH);
3777 const struct got_error *
3778 got_worktree_rebase_continue(struct got_object_id **commit_id,
3779 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
3780 struct got_reference **branch, struct got_fileindex **fileindex,
3781 struct got_worktree *worktree, struct got_repository *repo)
3783 const struct got_error *err;
3784 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
3785 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
3786 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
3787 char *fileindex_path = NULL;
3790 *new_base_branch = NULL;
3795 err = lock_worktree(worktree, LOCK_EX);
3799 err = open_fileindex(fileindex, &fileindex_path, worktree);
3803 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3807 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3811 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3815 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3819 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
3823 err = got_ref_open(branch, repo,
3824 got_ref_get_symref_target(branch_ref), 0);
3828 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3832 err = got_ref_resolve(commit_id, repo, commit_ref);
3836 err = got_ref_open(new_base_branch, repo,
3837 new_base_branch_ref_name, 0);
3841 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
3845 free(commit_ref_name);
3846 free(branch_ref_name);
3847 free(fileindex_path);
3849 got_ref_close(commit_ref);
3851 got_ref_close(branch_ref);
3856 got_ref_close(*tmp_branch);
3859 if (*new_base_branch) {
3860 got_ref_close(*new_base_branch);
3861 *new_base_branch = NULL;
3864 got_ref_close(*branch);
3868 got_fileindex_free(*fileindex);
3871 lock_worktree(worktree, LOCK_SH);
3876 const struct got_error *
3877 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
3879 const struct got_error *err;
3880 char *tmp_branch_name = NULL;
3882 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3886 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
3887 free(tmp_branch_name);
3891 static const struct got_error *
3892 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
3893 char **logmsg, void *arg)
3899 static const struct got_error *
3900 rebase_status(void *arg, unsigned char status, const char *path,
3901 struct got_object_id *blob_id, struct got_object_id *commit_id)
3906 struct collect_merged_paths_arg {
3907 got_worktree_checkout_cb progress_cb;
3909 struct got_pathlist_head *merged_paths;
3912 static const struct got_error *
3913 collect_merged_paths(void *arg, unsigned char status, const char *path)
3915 const struct got_error *err;
3916 struct collect_merged_paths_arg *a = arg;
3918 struct got_pathlist_entry *new;
3920 err = (*a->progress_cb)(a->progress_arg, status, path);
3924 if (status != GOT_STATUS_MERGE &&
3925 status != GOT_STATUS_ADD &&
3926 status != GOT_STATUS_DELETE &&
3927 status != GOT_STATUS_CONFLICT)
3932 return got_error_from_errno("strdup");
3934 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
3935 if (err || new == NULL)
3941 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
3943 struct got_pathlist_entry *pe;
3945 TAILQ_FOREACH(pe, merged_paths, entry)
3946 free((char *)pe->path);
3948 got_pathlist_free(merged_paths);
3951 static const struct got_error *
3952 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
3953 struct got_repository *repo)
3955 const struct got_error *err;
3956 struct got_reference *commit_ref = NULL;
3958 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3960 if (err->code != GOT_ERR_NOT_REF)
3962 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
3965 err = got_ref_write(commit_ref, repo);
3969 struct got_object_id *stored_id;
3972 err = got_ref_resolve(&stored_id, repo, commit_ref);
3975 cmp = got_object_id_cmp(commit_id, stored_id);
3978 err = got_error(GOT_ERR_REBASE_COMMITID);
3984 got_ref_close(commit_ref);
3988 static const struct got_error *
3989 rebase_merge_files(struct got_pathlist_head *merged_paths,
3990 const char *commit_ref_name, struct got_worktree *worktree,
3991 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
3992 struct got_object_id *commit_id, struct got_repository *repo,
3993 got_worktree_checkout_cb progress_cb, void *progress_arg,
3994 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
3996 const struct got_error *err;
3997 struct got_reference *commit_ref = NULL;
3998 struct collect_merged_paths_arg cmp_arg;
3999 char *fileindex_path;
4001 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4003 err = get_fileindex_path(&fileindex_path, worktree);
4007 cmp_arg.progress_cb = progress_cb;
4008 cmp_arg.progress_arg = progress_arg;
4009 cmp_arg.merged_paths = merged_paths;
4010 err = merge_files(worktree, fileindex, fileindex_path,
4011 parent_commit_id, commit_id, repo, collect_merged_paths,
4012 &cmp_arg, cancel_cb, cancel_arg);
4014 got_ref_close(commit_ref);
4018 const struct got_error *
4019 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
4020 struct got_worktree *worktree, struct got_fileindex *fileindex,
4021 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4022 struct got_repository *repo,
4023 got_worktree_checkout_cb progress_cb, void *progress_arg,
4024 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
4026 const struct got_error *err;
4027 char *commit_ref_name;
4029 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4033 err = store_commit_id(commit_ref_name, commit_id, repo);
4037 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4038 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4039 progress_arg, cancel_cb, cancel_arg);
4041 free(commit_ref_name);
4045 const struct got_error *
4046 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
4047 struct got_worktree *worktree, struct got_fileindex *fileindex,
4048 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4049 struct got_repository *repo,
4050 got_worktree_checkout_cb progress_cb, void *progress_arg,
4051 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
4053 const struct got_error *err;
4054 char *commit_ref_name;
4056 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4060 err = store_commit_id(commit_ref_name, commit_id, repo);
4064 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4065 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4066 progress_arg, cancel_cb, cancel_arg);
4068 free(commit_ref_name);
4072 static const struct got_error *
4073 rebase_commit(struct got_object_id **new_commit_id,
4074 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
4075 struct got_worktree *worktree, struct got_fileindex *fileindex,
4076 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
4077 const char *new_logmsg, struct got_repository *repo)
4079 const struct got_error *err, *sync_err;
4080 struct got_pathlist_head commitable_paths;
4081 struct collect_commitables_arg cc_arg;
4082 char *fileindex_path = NULL;
4083 struct got_reference *head_ref = NULL;
4084 struct got_object_id *head_commit_id = NULL;
4085 char *logmsg = NULL;
4087 TAILQ_INIT(&commitable_paths);
4088 *new_commit_id = NULL;
4090 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4092 err = get_fileindex_path(&fileindex_path, worktree);
4096 cc_arg.commitable_paths = &commitable_paths;
4097 cc_arg.worktree = worktree;
4100 * If possible get the status of individual files directly to
4101 * avoid crawling the entire work tree once per rebased commit.
4102 * TODO: Ideally, merged_paths would contain a list of commitables
4103 * we could use so we could skip worktree_status() entirely.
4106 struct got_pathlist_entry *pe;
4107 if (TAILQ_EMPTY(merged_paths)) {
4108 err = got_error(GOT_ERR_NO_MERGED_PATHS);
4111 TAILQ_FOREACH(pe, merged_paths, entry) {
4112 err = worktree_status(worktree, pe->path, fileindex,
4113 repo, collect_commitables, &cc_arg, NULL, NULL);
4118 err = worktree_status(worktree, "", fileindex, repo,
4119 collect_commitables, &cc_arg, NULL, NULL);
4124 if (TAILQ_EMPTY(&commitable_paths)) {
4125 /* No-op change; commit will be elided. */
4126 err = got_ref_delete(commit_ref, repo);
4129 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4133 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4137 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4142 logmsg = strdup(new_logmsg);
4144 logmsg = strdup(got_object_commit_get_logmsg(orig_commit));
4146 return got_error_from_errno("strdup");
4148 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
4149 worktree, NULL, got_object_commit_get_author(orig_commit),
4150 got_object_commit_get_committer(orig_commit),
4151 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
4155 err = got_ref_change_ref(tmp_branch, *new_commit_id);
4159 err = got_ref_delete(commit_ref, repo);
4163 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4165 sync_err = sync_fileindex(fileindex, fileindex_path);
4166 if (sync_err && err == NULL)
4169 free(fileindex_path);
4170 free(head_commit_id);
4172 got_ref_close(head_ref);
4174 free(*new_commit_id);
4175 *new_commit_id = NULL;
4180 const struct got_error *
4181 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
4182 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4183 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4184 struct got_commit_object *orig_commit,
4185 struct got_object_id *orig_commit_id, struct got_repository *repo)
4187 const struct got_error *err;
4188 char *commit_ref_name;
4189 struct got_reference *commit_ref = NULL;
4190 struct got_object_id *commit_id = NULL;
4192 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4196 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4199 err = got_ref_resolve(&commit_id, repo, commit_ref);
4202 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4203 err = got_error(GOT_ERR_REBASE_COMMITID);
4207 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4208 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
4211 got_ref_close(commit_ref);
4212 free(commit_ref_name);
4217 const struct got_error *
4218 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
4219 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4220 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4221 struct got_commit_object *orig_commit,
4222 struct got_object_id *orig_commit_id, const char *new_logmsg,
4223 struct got_repository *repo)
4225 const struct got_error *err;
4226 char *commit_ref_name;
4227 struct got_reference *commit_ref = NULL;
4228 struct got_object_id *commit_id = NULL;
4230 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4234 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4237 err = got_ref_resolve(&commit_id, repo, commit_ref);
4240 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4241 err = got_error(GOT_ERR_HISTEDIT_COMMITID);
4245 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4246 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
4249 got_ref_close(commit_ref);
4250 free(commit_ref_name);
4255 const struct got_error *
4256 got_worktree_rebase_postpone(struct got_worktree *worktree,
4257 struct got_fileindex *fileindex)
4260 got_fileindex_free(fileindex);
4261 return lock_worktree(worktree, LOCK_SH);
4264 static const struct got_error *
4265 delete_ref(const char *name, struct got_repository *repo)
4267 const struct got_error *err;
4268 struct got_reference *ref;
4270 err = got_ref_open(&ref, repo, name, 0);
4272 if (err->code == GOT_ERR_NOT_REF)
4277 err = got_ref_delete(ref, repo);
4282 static const struct got_error *
4283 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
4285 const struct got_error *err;
4286 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4287 char *branch_ref_name = NULL, *commit_ref_name = NULL;
4289 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4292 err = delete_ref(tmp_branch_name, repo);
4296 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4299 err = delete_ref(new_base_branch_ref_name, repo);
4303 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4306 err = delete_ref(branch_ref_name, repo);
4310 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4313 err = delete_ref(commit_ref_name, repo);
4318 free(tmp_branch_name);
4319 free(new_base_branch_ref_name);
4320 free(branch_ref_name);
4321 free(commit_ref_name);
4325 const struct got_error *
4326 got_worktree_rebase_complete(struct got_worktree *worktree,
4327 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
4328 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
4329 struct got_repository *repo)
4331 const struct got_error *err, *unlockerr;
4332 struct got_object_id *new_head_commit_id = NULL;
4334 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
4338 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
4342 err = got_ref_write(rebased_branch, repo);
4346 err = got_worktree_set_head_ref(worktree, rebased_branch);
4350 err = delete_rebase_refs(worktree, repo);
4353 got_fileindex_free(fileindex);
4354 free(new_head_commit_id);
4355 unlockerr = lock_worktree(worktree, LOCK_SH);
4356 if (unlockerr && err == NULL)
4361 struct collect_revertible_paths_arg {
4362 struct got_pathlist_head *revertible_paths;
4363 struct got_worktree *worktree;
4366 static const struct got_error *
4367 collect_revertible_paths(void *arg, unsigned char status, const char *relpath,
4368 struct got_object_id *blob_id, struct got_object_id *commit_id)
4370 struct collect_revertible_paths_arg *a = arg;
4371 const struct got_error *err = NULL;
4372 struct got_pathlist_entry *new = NULL;
4375 if (status != GOT_STATUS_ADD &&
4376 status != GOT_STATUS_DELETE &&
4377 status != GOT_STATUS_MODIFY &&
4378 status != GOT_STATUS_CONFLICT &&
4379 status != GOT_STATUS_MISSING)
4382 if (asprintf(&path, "%s/%s", a->worktree->root_path, relpath) == -1)
4383 return got_error_from_errno("asprintf");
4385 err = got_pathlist_insert(&new, a->revertible_paths, path, NULL);
4386 if (err || new == NULL)
4391 const struct got_error *
4392 got_worktree_rebase_abort(struct got_worktree *worktree,
4393 struct got_fileindex *fileindex, struct got_repository *repo,
4394 struct got_reference *new_base_branch,
4395 got_worktree_checkout_cb progress_cb, void *progress_arg)
4397 const struct got_error *err, *unlockerr, *sync_err;
4398 struct got_reference *resolved = NULL;
4399 struct got_object_id *commit_id = NULL;
4400 char *fileindex_path = NULL;
4401 struct got_pathlist_head revertible_paths;
4402 struct got_pathlist_entry *pe;
4403 struct collect_revertible_paths_arg crp_arg;
4404 struct got_object_id *tree_id = NULL;
4406 TAILQ_INIT(&revertible_paths);
4408 err = lock_worktree(worktree, LOCK_EX);
4412 err = got_ref_open(&resolved, repo,
4413 got_ref_get_symref_target(new_base_branch), 0);
4417 err = got_worktree_set_head_ref(worktree, resolved);
4422 * XXX commits to the base branch could have happened while
4423 * we were busy rebasing; should we store the original commit ID
4424 * when rebase begins and read it back here?
4426 err = got_ref_resolve(&commit_id, repo, resolved);
4430 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
4434 err = got_object_id_by_path(&tree_id, repo,
4435 worktree->base_commit_id, worktree->path_prefix);
4439 err = delete_rebase_refs(worktree, repo);
4443 err = get_fileindex_path(&fileindex_path, worktree);
4447 crp_arg.revertible_paths = &revertible_paths;
4448 crp_arg.worktree = worktree;
4449 err = worktree_status(worktree, "", fileindex, repo,
4450 collect_revertible_paths, &crp_arg, NULL, NULL);
4454 TAILQ_FOREACH(pe, &revertible_paths, entry) {
4455 err = revert_file(worktree, fileindex, pe->path,
4456 progress_cb, progress_arg, repo);
4461 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
4462 repo, progress_cb, progress_arg, NULL, NULL);
4464 sync_err = sync_fileindex(fileindex, fileindex_path);
4465 if (sync_err && err == NULL)
4468 got_ref_close(resolved);
4472 got_fileindex_free(fileindex);
4473 free(fileindex_path);
4474 TAILQ_FOREACH(pe, &revertible_paths, entry)
4475 free((char *)pe->path);
4476 got_pathlist_free(&revertible_paths);
4478 unlockerr = lock_worktree(worktree, LOCK_SH);
4479 if (unlockerr && err == NULL)
4484 const struct got_error *
4485 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
4486 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
4487 struct got_fileindex **fileindex, struct got_worktree *worktree,
4488 struct got_repository *repo)
4490 const struct got_error *err = NULL;
4491 char *tmp_branch_name = NULL;
4492 char *branch_ref_name = NULL;
4493 char *base_commit_ref_name = NULL;
4494 char *fileindex_path = NULL;
4495 struct check_rebase_ok_arg ok_arg;
4496 struct got_reference *wt_branch = NULL;
4497 struct got_reference *base_commit_ref = NULL;
4501 *base_commit_id = NULL;
4504 err = lock_worktree(worktree, LOCK_EX);
4508 err = open_fileindex(fileindex, &fileindex_path, worktree);
4512 ok_arg.worktree = worktree;
4514 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4519 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4523 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4527 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4532 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
4537 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
4541 err = got_ref_write(*branch_ref, repo);
4545 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
4546 worktree->base_commit_id);
4549 err = got_ref_write(base_commit_ref, repo);
4552 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
4553 if (*base_commit_id == NULL) {
4554 err = got_error_from_errno("got_object_id_dup");
4558 err = got_ref_alloc(tmp_branch, tmp_branch_name,
4559 worktree->base_commit_id);
4562 err = got_ref_write(*tmp_branch, repo);
4566 err = got_worktree_set_head_ref(worktree, *tmp_branch);
4570 free(fileindex_path);
4571 free(tmp_branch_name);
4572 free(branch_ref_name);
4573 free(base_commit_ref_name);
4575 got_ref_close(wt_branch);
4578 got_ref_close(*branch_ref);
4582 got_ref_close(*tmp_branch);
4585 free(*base_commit_id);
4587 got_fileindex_free(*fileindex);
4590 lock_worktree(worktree, LOCK_SH);
4595 const struct got_error *
4596 got_worktree_histedit_postpone(struct got_worktree *worktree,
4597 struct got_fileindex *fileindex)
4600 got_fileindex_free(fileindex);
4601 return lock_worktree(worktree, LOCK_SH);
4604 const struct got_error *
4605 got_worktree_histedit_in_progress(int *in_progress,
4606 struct got_worktree *worktree)
4608 const struct got_error *err;
4609 char *tmp_branch_name = NULL;
4611 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4615 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4616 free(tmp_branch_name);
4620 const struct got_error *
4621 got_worktree_histedit_continue(struct got_object_id **commit_id,
4622 struct got_reference **tmp_branch, struct got_reference **branch_ref,
4623 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
4624 struct got_worktree *worktree, struct got_repository *repo)
4626 const struct got_error *err;
4627 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
4628 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
4629 struct got_reference *commit_ref = NULL;
4630 struct got_reference *base_commit_ref = NULL;
4631 char *fileindex_path = NULL;
4635 *base_commit_id = NULL;
4638 err = lock_worktree(worktree, LOCK_EX);
4642 err = open_fileindex(fileindex, &fileindex_path, worktree);
4646 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4650 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4654 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4658 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4663 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
4667 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4670 err = got_ref_resolve(commit_id, repo, commit_ref);
4674 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
4677 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
4681 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4685 free(commit_ref_name);
4686 free(branch_ref_name);
4687 free(fileindex_path);
4689 got_ref_close(commit_ref);
4690 if (base_commit_ref)
4691 got_ref_close(base_commit_ref);
4695 free(*base_commit_id);
4696 *base_commit_id = NULL;
4698 got_ref_close(*tmp_branch);
4702 got_fileindex_free(*fileindex);
4705 lock_worktree(worktree, LOCK_EX);
4710 static const struct got_error *
4711 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
4713 const struct got_error *err;
4714 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
4715 char *branch_ref_name = NULL, *commit_ref_name = NULL;
4717 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4720 err = delete_ref(tmp_branch_name, repo);
4724 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4728 err = delete_ref(base_commit_ref_name, repo);
4732 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4735 err = delete_ref(branch_ref_name, repo);
4739 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4742 err = delete_ref(commit_ref_name, repo);
4746 free(tmp_branch_name);
4747 free(base_commit_ref_name);
4748 free(branch_ref_name);
4749 free(commit_ref_name);
4753 const struct got_error *
4754 got_worktree_histedit_abort(struct got_worktree *worktree,
4755 struct got_fileindex *fileindex, struct got_repository *repo,
4756 struct got_reference *branch, struct got_object_id *base_commit_id,
4757 got_worktree_checkout_cb progress_cb, void *progress_arg)
4759 const struct got_error *err, *unlockerr, *sync_err;
4760 struct got_reference *resolved = NULL;
4761 char *fileindex_path = NULL;
4762 struct got_pathlist_head revertible_paths;
4763 struct got_pathlist_entry *pe;
4764 struct collect_revertible_paths_arg crp_arg;
4765 struct got_object_id *tree_id = NULL;
4767 TAILQ_INIT(&revertible_paths);
4769 err = lock_worktree(worktree, LOCK_EX);
4773 err = got_ref_open(&resolved, repo,
4774 got_ref_get_symref_target(branch), 0);
4778 err = got_worktree_set_head_ref(worktree, resolved);
4782 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
4786 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
4787 worktree->path_prefix);
4791 err = delete_histedit_refs(worktree, repo);
4795 err = get_fileindex_path(&fileindex_path, worktree);
4799 crp_arg.revertible_paths = &revertible_paths;
4800 crp_arg.worktree = worktree;
4801 err = worktree_status(worktree, "", fileindex, repo,
4802 collect_revertible_paths, &crp_arg, NULL, NULL);
4806 TAILQ_FOREACH(pe, &revertible_paths, entry) {
4807 err = revert_file(worktree, fileindex, pe->path,
4808 progress_cb, progress_arg, repo);
4813 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
4814 repo, progress_cb, progress_arg, NULL, NULL);
4816 sync_err = sync_fileindex(fileindex, fileindex_path);
4817 if (sync_err && err == NULL)
4820 got_ref_close(resolved);
4822 free(fileindex_path);
4823 TAILQ_FOREACH(pe, &revertible_paths, entry)
4824 free((char *)pe->path);
4825 got_pathlist_free(&revertible_paths);
4827 unlockerr = lock_worktree(worktree, LOCK_SH);
4828 if (unlockerr && err == NULL)
4833 const struct got_error *
4834 got_worktree_histedit_complete(struct got_worktree *worktree,
4835 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4836 struct got_reference *edited_branch, struct got_repository *repo)
4838 const struct got_error *err, *unlockerr;
4839 struct got_object_id *new_head_commit_id = NULL;
4840 struct got_reference *resolved = NULL;
4842 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
4846 err = got_ref_open(&resolved, repo,
4847 got_ref_get_symref_target(edited_branch), 0);
4851 err = got_ref_change_ref(resolved, new_head_commit_id);
4855 err = got_ref_write(resolved, repo);
4859 err = got_worktree_set_head_ref(worktree, resolved);
4863 err = delete_histedit_refs(worktree, repo);
4866 got_fileindex_free(fileindex);
4867 free(new_head_commit_id);
4868 unlockerr = lock_worktree(worktree, LOCK_SH);
4869 if (unlockerr && err == NULL)
4874 const struct got_error *
4875 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
4876 struct got_object_id *commit_id, struct got_repository *repo)
4878 const struct got_error *err;
4879 char *commit_ref_name;
4881 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4885 err = store_commit_id(commit_ref_name, commit_id, repo);
4889 err = delete_ref(commit_ref_name, repo);
4891 free(commit_ref_name);