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) {
1054 if (got_fileindex_entry_has_file_on_disk(ie))
1055 *status = GOT_STATUS_MISSING;
1057 *status = GOT_STATUS_DELETE;
1059 ((ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT)
1060 & (S_IRWXU | S_IRWXG | S_IRWXO));
1062 sb->st_mode = GOT_DEFAULT_FILE_MODE;
1065 return got_error_from_errno2("lstat", abspath);
1068 if (!S_ISREG(sb->st_mode)) {
1069 *status = GOT_STATUS_OBSTRUCTED;
1076 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1077 *status = GOT_STATUS_DELETE;
1079 } else if (!got_fileindex_entry_has_blob(ie)) {
1080 *status = GOT_STATUS_ADD;
1084 if (!stat_info_differs(ie, sb))
1087 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1088 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1092 f = fopen(abspath, "r");
1094 err = got_error_from_errno2("fopen", abspath);
1097 hdrlen = got_object_blob_get_hdrlen(blob);
1099 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1100 err = got_object_blob_read_block(&blen, blob);
1103 /* Skip length of blob object header first time around. */
1104 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1105 if (flen == 0 && ferror(f)) {
1106 err = got_error_from_errno("fread");
1111 *status = GOT_STATUS_MODIFY;
1113 } else if (flen == 0) {
1115 *status = GOT_STATUS_MODIFY;
1117 } else if (blen - hdrlen == flen) {
1118 /* Skip blob object header first time around. */
1119 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1120 *status = GOT_STATUS_MODIFY;
1124 *status = GOT_STATUS_MODIFY;
1130 if (*status == GOT_STATUS_MODIFY) {
1132 err = get_modified_file_content_status(status, f);
1136 got_object_blob_close(blob);
1143 * Update timestamps in the file index if a file is unmodified and
1144 * we had to run a full content comparison to find out.
1146 static const struct got_error *
1147 sync_timestamps(char *ondisk_path, unsigned char status,
1148 struct got_fileindex_entry *ie, struct stat *sb)
1150 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1151 return got_fileindex_entry_update(ie, ondisk_path,
1152 ie->blob_sha1, ie->commit_sha1, 1);
1157 static const struct got_error *
1158 update_blob(struct got_worktree *worktree,
1159 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1160 struct got_tree_entry *te, const char *path,
1161 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1164 const struct got_error *err = NULL;
1165 struct got_blob_object *blob = NULL;
1167 unsigned char status = GOT_STATUS_NO_CHANGE;
1170 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1171 return got_error_from_errno("asprintf");
1173 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1177 if (status == GOT_STATUS_OBSTRUCTED) {
1178 err = (*progress_cb)(progress_arg, status, path);
1182 if (ie && status != GOT_STATUS_MISSING) {
1183 if (got_fileindex_entry_has_commit(ie) &&
1184 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1185 SHA1_DIGEST_LENGTH) == 0) {
1186 err = sync_timestamps(ondisk_path, status, ie, &sb);
1189 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1193 if (got_fileindex_entry_has_blob(ie) &&
1194 memcmp(ie->blob_sha1, te->id->sha1,
1195 SHA1_DIGEST_LENGTH) == 0) {
1196 err = sync_timestamps(ondisk_path, status, ie, &sb);
1201 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1205 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1206 int update_timestamps;
1207 struct got_blob_object *blob2 = NULL;
1208 if (got_fileindex_entry_has_blob(ie)) {
1209 struct got_object_id id2;
1210 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1211 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1215 err = merge_blob(&update_timestamps, worktree, blob2,
1216 ondisk_path, path, sb.st_mode, blob,
1217 worktree->base_commit_id, repo,
1218 progress_cb, progress_arg);
1220 got_object_blob_close(blob2);
1222 * Do not update timestamps of files with local changes.
1223 * Otherwise, a future status walk would treat them as
1224 * unmodified files again.
1226 err = got_fileindex_entry_update(ie, ondisk_path,
1227 blob->id.sha1, worktree->base_commit_id->sha1,
1229 } else if (status == GOT_STATUS_DELETE) {
1230 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1233 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1234 ondisk_path, path, blob, 0);
1238 err = install_blob(worktree, ondisk_path, path, te->mode,
1239 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1240 repo, progress_cb, progress_arg);
1243 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1244 ondisk_path, path, blob, 1);
1248 got_object_blob_close(blob);
1254 static const struct got_error *
1255 remove_ondisk_file(const char *root_path, const char *path)
1257 const struct got_error *err = NULL;
1258 char *ondisk_path = NULL;
1260 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1261 return got_error_from_errno("asprintf");
1263 if (unlink(ondisk_path) == -1) {
1264 if (errno != ENOENT)
1265 err = got_error_from_errno2("unlink", ondisk_path);
1267 char *parent = dirname(ondisk_path);
1268 while (parent && strcmp(parent, root_path) != 0) {
1269 if (rmdir(parent) == -1) {
1270 if (errno != ENOTEMPTY)
1271 err = got_error_from_errno2("rmdir",
1275 parent = dirname(parent);
1282 static const struct got_error *
1283 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1284 struct got_fileindex_entry *ie, struct got_repository *repo,
1285 got_worktree_checkout_cb progress_cb, void *progress_arg)
1287 const struct got_error *err = NULL;
1288 unsigned char status;
1292 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1294 return got_error_from_errno("asprintf");
1296 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1300 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1301 status == GOT_STATUS_ADD) {
1302 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1306 * Preserve the working file and change the deleted blob's
1307 * entry into a schedule-add entry.
1309 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1314 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1317 if (status == GOT_STATUS_NO_CHANGE) {
1318 err = remove_ondisk_file(worktree->root_path, ie->path);
1322 got_fileindex_entry_remove(fileindex, ie);
1328 struct diff_cb_arg {
1329 struct got_fileindex *fileindex;
1330 struct got_worktree *worktree;
1331 struct got_repository *repo;
1332 got_worktree_checkout_cb progress_cb;
1334 got_worktree_cancel_cb cancel_cb;
1338 static const struct got_error *
1339 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1340 struct got_tree_entry *te, const char *parent_path)
1342 struct diff_cb_arg *a = arg;
1344 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1345 return got_error(GOT_ERR_CANCELLED);
1347 return update_blob(a->worktree, a->fileindex, ie, te,
1348 ie->path, a->repo, a->progress_cb, a->progress_arg);
1351 static const struct got_error *
1352 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1354 struct diff_cb_arg *a = arg;
1356 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1357 return got_error(GOT_ERR_CANCELLED);
1359 return delete_blob(a->worktree, a->fileindex, ie,
1360 a->repo, a->progress_cb, a->progress_arg);
1363 static const struct got_error *
1364 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1366 struct diff_cb_arg *a = arg;
1367 const struct got_error *err;
1370 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1371 return got_error(GOT_ERR_CANCELLED);
1373 if (asprintf(&path, "%s%s%s", parent_path,
1374 parent_path[0] ? "/" : "", te->name)
1376 return got_error_from_errno("asprintf");
1378 if (S_ISDIR(te->mode))
1379 err = add_dir_on_disk(a->worktree, path);
1381 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1382 a->repo, a->progress_cb, a->progress_arg);
1388 static const struct got_error *
1389 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1391 const struct got_error *err = NULL;
1392 char *uuidstr = NULL;
1393 uint32_t uuid_status;
1397 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1398 if (uuid_status != uuid_s_ok)
1399 return got_error_uuid(uuid_status);
1401 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1403 err = got_error_from_errno("asprintf");
1410 const struct got_error *
1411 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1413 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1416 static const struct got_error *
1417 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1419 return get_ref_name(refname, worktree,
1420 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1423 static const struct got_error *
1424 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1426 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1429 static const struct got_error *
1430 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1432 return get_ref_name(refname, worktree,
1433 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1436 static const struct got_error *
1437 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1439 return get_ref_name(refname, worktree,
1440 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1443 static const struct got_error *
1444 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1446 return get_ref_name(refname, worktree,
1447 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1450 static const struct got_error *
1451 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1453 return get_ref_name(refname, worktree,
1454 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1457 static const struct got_error *
1458 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1460 return get_ref_name(refname, worktree,
1461 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1464 static const struct got_error *
1465 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1467 return get_ref_name(refname, worktree,
1468 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1471 const struct got_error *
1472 got_worktree_get_histedit_list_path(char **path, struct got_worktree *worktree)
1474 if (asprintf(path, "%s/%s/%s", worktree->root_path,
1475 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_LIST) == -1) {
1477 return got_error_from_errno("asprintf");
1483 * Prevent Git's garbage collector from deleting our base commit by
1484 * setting a reference to our base commit's ID.
1486 static const struct got_error *
1487 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1489 const struct got_error *err = NULL;
1490 struct got_reference *ref = NULL;
1493 err = got_worktree_get_base_ref_name(&refname, worktree);
1497 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1501 err = got_ref_write(ref, repo);
1509 static const struct got_error *
1510 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
1512 const struct got_error *err = NULL;
1514 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1515 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1516 err = got_error_from_errno("asprintf");
1517 *fileindex_path = NULL;
1523 static const struct got_error *
1524 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1525 struct got_worktree *worktree)
1527 const struct got_error *err = NULL;
1530 *fileindex_path = NULL;
1531 *fileindex = got_fileindex_alloc();
1532 if (*fileindex == NULL)
1533 return got_error_from_errno("got_fileindex_alloc");
1535 err = get_fileindex_path(fileindex_path, worktree);
1539 index = fopen(*fileindex_path, "rb");
1540 if (index == NULL) {
1541 if (errno != ENOENT)
1542 err = got_error_from_errno2("fopen", *fileindex_path);
1544 err = got_fileindex_read(*fileindex, index);
1545 if (fclose(index) != 0 && err == NULL)
1546 err = got_error_from_errno("fclose");
1550 free(*fileindex_path);
1551 *fileindex_path = NULL;
1552 got_fileindex_free(*fileindex);
1558 struct bump_base_commit_id_arg {
1559 struct got_object_id *base_commit_id;
1562 const char *entry_name;
1563 got_worktree_checkout_cb progress_cb;
1567 /* Bump base commit ID of all files within an updated part of the work tree. */
1568 static const struct got_error *
1569 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1571 const struct got_error *err;
1572 struct bump_base_commit_id_arg *a = arg;
1574 if (a->entry_name) {
1575 if (strcmp(ie->path, a->path) != 0)
1577 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1580 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1581 SHA1_DIGEST_LENGTH) == 0)
1584 if (a->progress_cb) {
1585 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1590 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1594 static const struct got_error *
1595 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1597 const struct got_error *err = NULL;
1598 char *new_fileindex_path = NULL;
1599 FILE *new_index = NULL;
1601 err = got_opentemp_named(&new_fileindex_path, &new_index,
1606 err = got_fileindex_write(fileindex, new_index);
1610 if (rename(new_fileindex_path, fileindex_path) != 0) {
1611 err = got_error_from_errno3("rename", new_fileindex_path,
1613 unlink(new_fileindex_path);
1618 free(new_fileindex_path);
1622 static const struct got_error *
1623 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1624 struct got_object_id **tree_id, const char *wt_relpath,
1625 struct got_worktree *worktree, struct got_repository *repo)
1627 const struct got_error *err = NULL;
1628 struct got_object_id *id = NULL;
1629 char *in_repo_path = NULL;
1630 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1632 *entry_type = GOT_OBJ_TYPE_ANY;
1633 *tree_relpath = NULL;
1636 if (wt_relpath[0] == '\0') {
1637 /* Check out all files within the work tree. */
1638 *entry_type = GOT_OBJ_TYPE_TREE;
1639 *tree_relpath = strdup("");
1640 if (*tree_relpath == NULL) {
1641 err = got_error_from_errno("strdup");
1644 err = got_object_id_by_path(tree_id, repo,
1645 worktree->base_commit_id, worktree->path_prefix);
1651 /* Check out a subset of files in the work tree. */
1653 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1654 is_root_wt ? "" : "/", wt_relpath) == -1) {
1655 err = got_error_from_errno("asprintf");
1659 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
1665 in_repo_path = NULL;
1667 err = got_object_get_type(entry_type, repo, id);
1671 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
1672 /* Check out a single file. */
1673 if (strchr(wt_relpath, '/') == NULL) {
1674 /* Check out a single file in work tree's root dir. */
1675 in_repo_path = strdup(worktree->path_prefix);
1676 if (in_repo_path == NULL) {
1677 err = got_error_from_errno("strdup");
1680 *tree_relpath = strdup("");
1681 if (*tree_relpath == NULL) {
1682 err = got_error_from_errno("strdup");
1686 /* Check out a single file in a subdirectory. */
1687 err = got_path_dirname(tree_relpath, wt_relpath);
1690 if (asprintf(&in_repo_path, "%s%s%s",
1691 worktree->path_prefix, is_root_wt ? "" : "/",
1692 *tree_relpath) == -1) {
1693 err = got_error_from_errno("asprintf");
1697 err = got_object_id_by_path(tree_id, repo,
1698 worktree->base_commit_id, in_repo_path);
1700 /* Check out all files within a subdirectory. */
1701 *tree_id = got_object_id_dup(id);
1702 if (*tree_id == NULL) {
1703 err = got_error_from_errno("got_object_id_dup");
1706 *tree_relpath = strdup(wt_relpath);
1707 if (*tree_relpath == NULL) {
1708 err = got_error_from_errno("strdup");
1716 *entry_type = GOT_OBJ_TYPE_ANY;
1717 free(*tree_relpath);
1718 *tree_relpath = NULL;
1725 static const struct got_error *
1726 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1727 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
1728 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1729 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1731 const struct got_error *err = NULL;
1732 struct got_commit_object *commit = NULL;
1733 struct got_tree_object *tree = NULL;
1734 struct got_fileindex_diff_tree_cb diff_cb;
1735 struct diff_cb_arg arg;
1737 err = ref_base_commit(worktree, repo);
1741 err = got_object_open_as_commit(&commit, repo,
1742 worktree->base_commit_id);
1746 err = got_object_open_as_tree(&tree, repo, tree_id);
1751 got_object_tree_find_entry(tree, entry_name) == NULL) {
1752 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1756 diff_cb.diff_old_new = diff_old_new;
1757 diff_cb.diff_old = diff_old;
1758 diff_cb.diff_new = diff_new;
1759 arg.fileindex = fileindex;
1760 arg.worktree = worktree;
1762 arg.progress_cb = progress_cb;
1763 arg.progress_arg = progress_arg;
1764 arg.cancel_cb = cancel_cb;
1765 arg.cancel_arg = cancel_arg;
1766 err = got_fileindex_diff_tree(fileindex, tree, relpath,
1767 entry_name, repo, &diff_cb, &arg);
1770 got_object_tree_close(tree);
1772 got_object_commit_close(commit);
1776 const struct got_error *
1777 got_worktree_checkout_files(struct got_worktree *worktree, const char *path,
1778 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1779 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1781 const struct got_error *err = NULL, *sync_err, *unlockerr;
1782 struct got_commit_object *commit = NULL;
1783 struct got_object_id *tree_id = NULL;
1784 struct got_tree_object *tree = NULL;
1785 struct got_fileindex *fileindex = NULL;
1786 char *fileindex_path = NULL;
1787 char *relpath = NULL, *entry_name = NULL;
1790 err = lock_worktree(worktree, LOCK_EX);
1794 err = find_tree_entry_for_checkout(&entry_type, &relpath, &tree_id,
1795 path, worktree, repo);
1799 if (entry_type == GOT_OBJ_TYPE_BLOB) {
1800 entry_name = basename(path);
1801 if (entry_name == NULL) {
1802 err = got_error_from_errno2("basename", path);
1808 * Read the file index.
1809 * Checking out files is supposed to be an idempotent operation.
1810 * If the on-disk file index is incomplete we will try to complete it.
1812 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1816 err = checkout_files(worktree, fileindex, relpath, tree_id, entry_name,
1817 repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
1819 struct bump_base_commit_id_arg bbc_arg;
1820 bbc_arg.base_commit_id = worktree->base_commit_id;
1821 bbc_arg.entry_name = entry_name;
1822 bbc_arg.path = path;
1823 bbc_arg.path_len = strlen(path);
1824 bbc_arg.progress_cb = progress_cb;
1825 bbc_arg.progress_arg = progress_arg;
1826 err = got_fileindex_for_each_entry_safe(fileindex,
1827 bump_base_commit_id, &bbc_arg);
1829 sync_err = sync_fileindex(fileindex, fileindex_path);
1830 if (sync_err && err == NULL)
1833 free(fileindex_path);
1836 got_object_tree_close(tree);
1838 got_object_commit_close(commit);
1840 got_fileindex_free(fileindex);
1841 unlockerr = lock_worktree(worktree, LOCK_SH);
1842 if (unlockerr && err == NULL)
1847 struct merge_file_cb_arg {
1848 struct got_worktree *worktree;
1849 struct got_fileindex *fileindex;
1850 got_worktree_checkout_cb progress_cb;
1852 got_worktree_cancel_cb cancel_cb;
1854 struct got_object_id *commit_id2;
1857 static const struct got_error *
1858 merge_file_cb(void *arg, struct got_blob_object *blob1,
1859 struct got_blob_object *blob2, struct got_object_id *id1,
1860 struct got_object_id *id2, const char *path1, const char *path2,
1861 struct got_repository *repo)
1863 static const struct got_error *err = NULL;
1864 struct merge_file_cb_arg *a = arg;
1865 struct got_fileindex_entry *ie;
1866 char *ondisk_path = NULL;
1868 unsigned char status;
1869 int local_changes_subsumed;
1871 if (blob1 && blob2) {
1872 ie = got_fileindex_entry_get(a->fileindex, path2);
1874 return (*a->progress_cb)(a->progress_arg,
1875 GOT_STATUS_MISSING, path2);
1877 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1879 return got_error_from_errno("asprintf");
1881 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1885 if (status == GOT_STATUS_DELETE) {
1886 err = (*a->progress_cb)(a->progress_arg,
1887 GOT_STATUS_MERGE, path2);
1890 if (status != GOT_STATUS_NO_CHANGE &&
1891 status != GOT_STATUS_MODIFY &&
1892 status != GOT_STATUS_CONFLICT &&
1893 status != GOT_STATUS_ADD) {
1894 err = (*a->progress_cb)(a->progress_arg, status, path2);
1898 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
1899 ondisk_path, path2, sb.st_mode, blob2, a->commit_id2, repo,
1900 a->progress_cb, a->progress_arg);
1902 ie = got_fileindex_entry_get(a->fileindex, path1);
1904 return (*a->progress_cb)(a->progress_arg,
1905 GOT_STATUS_MISSING, path2);
1907 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1909 return got_error_from_errno("asprintf");
1911 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1916 case GOT_STATUS_NO_CHANGE:
1917 err = (*a->progress_cb)(a->progress_arg,
1918 GOT_STATUS_DELETE, path1);
1921 err = remove_ondisk_file(a->worktree->root_path, path1);
1925 got_fileindex_entry_mark_deleted_from_disk(ie);
1927 case GOT_STATUS_DELETE:
1928 case GOT_STATUS_MISSING:
1929 err = (*a->progress_cb)(a->progress_arg,
1930 GOT_STATUS_DELETE, path1);
1934 got_fileindex_entry_mark_deleted_from_disk(ie);
1936 case GOT_STATUS_ADD:
1937 case GOT_STATUS_MODIFY:
1938 case GOT_STATUS_CONFLICT:
1939 err = (*a->progress_cb)(a->progress_arg,
1940 GOT_STATUS_CANNOT_DELETE, path1);
1944 case GOT_STATUS_OBSTRUCTED:
1945 err = (*a->progress_cb)(a->progress_arg, status, path1);
1953 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1955 return got_error_from_errno("asprintf");
1956 ie = got_fileindex_entry_get(a->fileindex, path2);
1958 err = get_file_status(&status, &sb, ie, ondisk_path,
1962 if (status != GOT_STATUS_NO_CHANGE &&
1963 status != GOT_STATUS_MODIFY &&
1964 status != GOT_STATUS_CONFLICT &&
1965 status != GOT_STATUS_ADD) {
1966 err = (*a->progress_cb)(a->progress_arg,
1970 err = merge_blob(&local_changes_subsumed, a->worktree,
1971 NULL, ondisk_path, path2, sb.st_mode, blob2,
1972 a->commit_id2, repo,
1973 a->progress_cb, a->progress_arg);
1974 if (status == GOT_STATUS_DELETE) {
1975 err = update_blob_fileindex_entry(a->worktree,
1976 a->fileindex, ie, ondisk_path, ie->path,
1982 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1983 err = install_blob(a->worktree, ondisk_path, path2,
1984 /* XXX get this from parent tree! */
1985 GOT_DEFAULT_FILE_MODE,
1986 sb.st_mode, blob2, 0, 0, repo,
1987 a->progress_cb, a->progress_arg);
1990 err = got_fileindex_entry_alloc(&ie,
1991 ondisk_path, path2, NULL, NULL);
1994 err = got_fileindex_entry_add(a->fileindex, ie);
1996 got_fileindex_entry_free(ie);
2006 struct check_merge_ok_arg {
2007 struct got_worktree *worktree;
2008 struct got_repository *repo;
2011 static const struct got_error *
2012 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2014 const struct got_error *err = NULL;
2015 struct check_merge_ok_arg *a = arg;
2016 unsigned char status;
2020 /* Reject merges into a work tree with mixed base commits. */
2021 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2022 SHA1_DIGEST_LENGTH))
2023 return got_error(GOT_ERR_MIXED_COMMITS);
2025 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2027 return got_error_from_errno("asprintf");
2029 /* Reject merges into a work tree with conflicted files. */
2030 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
2033 if (status == GOT_STATUS_CONFLICT)
2034 return got_error(GOT_ERR_CONFLICTS);
2039 static const struct got_error *
2040 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2041 const char *fileindex_path, struct got_object_id *commit_id1,
2042 struct got_object_id *commit_id2, struct got_repository *repo,
2043 got_worktree_checkout_cb progress_cb, void *progress_arg,
2044 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2046 const struct got_error *err = NULL, *sync_err;
2047 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2048 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2049 struct merge_file_cb_arg arg;
2052 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2053 worktree->path_prefix);
2057 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2062 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2063 worktree->path_prefix);
2067 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2071 arg.worktree = worktree;
2072 arg.fileindex = fileindex;
2073 arg.progress_cb = progress_cb;
2074 arg.progress_arg = progress_arg;
2075 arg.cancel_cb = cancel_cb;
2076 arg.cancel_arg = cancel_arg;
2077 arg.commit_id2 = commit_id2;
2078 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg);
2079 sync_err = sync_fileindex(fileindex, fileindex_path);
2080 if (sync_err && err == NULL)
2084 got_object_tree_close(tree1);
2086 got_object_tree_close(tree2);
2090 const struct got_error *
2091 got_worktree_merge_files(struct got_worktree *worktree,
2092 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2093 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2094 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2096 const struct got_error *err, *unlockerr;
2097 char *fileindex_path = NULL;
2098 struct got_fileindex *fileindex = NULL;
2099 struct check_merge_ok_arg mok_arg;
2101 err = lock_worktree(worktree, LOCK_EX);
2105 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2109 mok_arg.worktree = worktree;
2110 mok_arg.repo = repo;
2111 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2116 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2117 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2120 got_fileindex_free(fileindex);
2121 free(fileindex_path);
2122 unlockerr = lock_worktree(worktree, LOCK_SH);
2123 if (unlockerr && err == NULL)
2128 struct diff_dir_cb_arg {
2129 struct got_fileindex *fileindex;
2130 struct got_worktree *worktree;
2131 const char *status_path;
2132 size_t status_path_len;
2133 struct got_repository *repo;
2134 got_worktree_status_cb status_cb;
2136 got_worktree_cancel_cb cancel_cb;
2140 static const struct got_error *
2141 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2142 got_worktree_status_cb status_cb, void *status_arg,
2143 struct got_repository *repo)
2145 const struct got_error *err = NULL;
2146 unsigned char status = GOT_STATUS_NO_CHANGE;
2148 struct got_object_id blob_id, commit_id;
2150 err = get_file_status(&status, &sb, ie, abspath, repo);
2151 if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
2152 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2153 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2154 err = (*status_cb)(status_arg, status, ie->path, &blob_id,
2160 static const struct got_error *
2161 status_old_new(void *arg, struct got_fileindex_entry *ie,
2162 struct dirent *de, const char *parent_path)
2164 const struct got_error *err = NULL;
2165 struct diff_dir_cb_arg *a = arg;
2168 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2169 return got_error(GOT_ERR_CANCELLED);
2171 if (got_path_cmp(parent_path, a->status_path) != 0 &&
2172 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2175 if (parent_path[0]) {
2176 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2177 parent_path, de->d_name) == -1)
2178 return got_error_from_errno("asprintf");
2180 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2182 return got_error_from_errno("asprintf");
2185 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
2191 static const struct got_error *
2192 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2194 struct diff_dir_cb_arg *a = arg;
2195 struct got_object_id blob_id, commit_id;
2196 unsigned char status;
2198 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2199 return got_error(GOT_ERR_CANCELLED);
2201 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
2204 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2205 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2206 if (got_fileindex_entry_has_file_on_disk(ie))
2207 status = GOT_STATUS_MISSING;
2209 status = GOT_STATUS_DELETE;
2210 return (*a->status_cb)(a->status_arg, status, ie->path, &blob_id,
2214 static const struct got_error *
2215 status_new(void *arg, struct dirent *de, const char *parent_path)
2217 const struct got_error *err = NULL;
2218 struct diff_dir_cb_arg *a = arg;
2221 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2222 return got_error(GOT_ERR_CANCELLED);
2224 if (de->d_type == DT_DIR)
2227 /* XXX ignore symlinks for now */
2228 if (de->d_type == DT_LNK)
2231 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
2234 if (parent_path[0]) {
2235 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2236 return got_error_from_errno("asprintf");
2241 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path,
2248 static const struct got_error *
2249 worktree_status(struct got_worktree *worktree, const char *path,
2250 struct got_fileindex *fileindex, struct got_repository *repo,
2251 got_worktree_status_cb status_cb, void *status_arg,
2252 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2254 const struct got_error *err = NULL;
2255 DIR *workdir = NULL;
2256 struct got_fileindex_diff_dir_cb fdiff_cb;
2257 struct diff_dir_cb_arg arg;
2258 char *ondisk_path = NULL;
2260 if (asprintf(&ondisk_path, "%s%s%s",
2261 worktree->root_path, path[0] ? "/" : "", path) == -1) {
2262 err = got_error_from_errno("asprintf");
2265 workdir = opendir(ondisk_path);
2266 if (workdir == NULL) {
2267 if (errno == ENOTDIR || errno == ENOENT) {
2268 struct got_fileindex_entry *ie;
2269 ie = got_fileindex_entry_get(fileindex, path);
2271 err = got_error(GOT_ERR_BAD_PATH);
2274 err = report_file_status(ie, ondisk_path,
2275 status_cb, status_arg, repo);
2278 err = got_error_from_errno2("opendir", ondisk_path);
2282 fdiff_cb.diff_old_new = status_old_new;
2283 fdiff_cb.diff_old = status_old;
2284 fdiff_cb.diff_new = status_new;
2285 arg.fileindex = fileindex;
2286 arg.worktree = worktree;
2287 arg.status_path = path;
2288 arg.status_path_len = strlen(path);
2290 arg.status_cb = status_cb;
2291 arg.status_arg = status_arg;
2292 arg.cancel_cb = cancel_cb;
2293 arg.cancel_arg = cancel_arg;
2294 err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
2295 path, repo, &fdiff_cb, &arg);
2303 const struct got_error *
2304 got_worktree_status(struct got_worktree *worktree, const char *path,
2305 struct got_repository *repo, got_worktree_status_cb status_cb,
2306 void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2308 const struct got_error *err = NULL;
2309 char *fileindex_path = NULL;
2310 struct got_fileindex *fileindex = NULL;
2312 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2316 err = worktree_status(worktree, path, fileindex, repo,
2317 status_cb, status_arg, cancel_cb, cancel_arg);
2318 free(fileindex_path);
2319 got_fileindex_free(fileindex);
2323 const struct got_error *
2324 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2327 const struct got_error *err = NULL;
2328 char *resolved, *cwd = NULL, *path = NULL;
2333 resolved = realpath(arg, NULL);
2334 if (resolved == NULL) {
2335 if (errno != ENOENT)
2336 return got_error_from_errno2("realpath", arg);
2337 cwd = getcwd(NULL, 0);
2339 return got_error_from_errno("getcwd");
2340 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
2341 err = got_error_from_errno("asprintf");
2346 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2347 strlen(got_worktree_get_root_path(worktree)))) {
2348 err = got_error(GOT_ERR_BAD_PATH);
2352 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2353 err = got_path_skip_common_ancestor(&path,
2354 got_worktree_get_root_path(worktree), resolved);
2360 err = got_error_from_errno("strdup");
2365 /* XXX status walk can't deal with trailing slash! */
2367 while (path[len - 1] == '/') {
2368 path[len - 1] = '\0';
2381 static const struct got_error *
2382 schedule_addition(const char *ondisk_path, struct got_fileindex *fileindex,
2383 const char *relpath, got_worktree_status_cb status_cb, void *status_arg,
2384 struct got_repository *repo)
2386 const struct got_error *err = NULL;
2387 struct got_fileindex_entry *ie;
2389 /* Re-adding an existing entry is a no-op. */
2390 if (got_fileindex_entry_get(fileindex, relpath) != NULL)
2393 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2397 err = got_fileindex_entry_add(fileindex, ie);
2399 got_fileindex_entry_free(ie);
2403 return report_file_status(ie, relpath, status_cb, status_arg, repo);
2406 const struct got_error *
2407 got_worktree_schedule_add(struct got_worktree *worktree,
2408 struct got_pathlist_head *ondisk_paths,
2409 got_worktree_status_cb status_cb, void *status_arg,
2410 struct got_repository *repo)
2412 struct got_fileindex *fileindex = NULL;
2413 char *fileindex_path = NULL;
2414 const struct got_error *err = NULL, *sync_err, *unlockerr;
2415 struct got_pathlist_entry *pe;
2417 err = lock_worktree(worktree, LOCK_EX);
2421 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2425 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2427 err = got_path_skip_common_ancestor(&relpath,
2428 got_worktree_get_root_path(worktree), pe->path);
2431 err = schedule_addition(pe->path, fileindex, relpath,
2432 status_cb, status_arg, repo);
2437 sync_err = sync_fileindex(fileindex, fileindex_path);
2438 if (sync_err && err == NULL)
2441 free(fileindex_path);
2443 got_fileindex_free(fileindex);
2444 unlockerr = lock_worktree(worktree, LOCK_SH);
2445 if (unlockerr && err == NULL)
2450 static const struct got_error *
2451 schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
2452 const char *relpath, int delete_local_mods,
2453 got_worktree_status_cb status_cb, void *status_arg,
2454 struct got_repository *repo)
2456 const struct got_error *err = NULL;
2457 struct got_fileindex_entry *ie = NULL;
2458 unsigned char status;
2461 ie = got_fileindex_entry_get(fileindex, relpath);
2463 return got_error(GOT_ERR_BAD_PATH);
2465 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2469 if (status != GOT_STATUS_NO_CHANGE) {
2470 if (status == GOT_STATUS_DELETE)
2471 return got_error_set_errno(ENOENT, ondisk_path);
2472 if (status != GOT_STATUS_MODIFY)
2473 return got_error(GOT_ERR_FILE_STATUS);
2474 if (!delete_local_mods)
2475 return got_error(GOT_ERR_FILE_MODIFIED);
2478 if (unlink(ondisk_path) != 0)
2479 return got_error_from_errno2("unlink", ondisk_path);
2481 got_fileindex_entry_mark_deleted_from_disk(ie);
2482 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2485 const struct got_error *
2486 got_worktree_schedule_delete(struct got_worktree *worktree,
2487 struct got_pathlist_head *ondisk_paths, int delete_local_mods,
2488 got_worktree_status_cb status_cb, void *status_arg,
2489 struct got_repository *repo)
2491 struct got_fileindex *fileindex = NULL;
2492 char *fileindex_path = NULL;
2493 const struct got_error *err = NULL, *sync_err, *unlockerr;
2494 struct got_pathlist_entry *pe;
2496 err = lock_worktree(worktree, LOCK_EX);
2500 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2504 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2506 err = got_path_skip_common_ancestor(&relpath,
2507 got_worktree_get_root_path(worktree), pe->path);
2510 err = schedule_for_deletion(pe->path, fileindex, relpath,
2511 delete_local_mods, status_cb, status_arg, repo);
2516 sync_err = sync_fileindex(fileindex, fileindex_path);
2517 if (sync_err && err == NULL)
2520 free(fileindex_path);
2522 got_fileindex_free(fileindex);
2523 unlockerr = lock_worktree(worktree, LOCK_SH);
2524 if (unlockerr && err == NULL)
2529 static const struct got_error *
2530 revert_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2531 const char *ondisk_path,
2532 got_worktree_checkout_cb progress_cb, void *progress_arg,
2533 struct got_repository *repo)
2535 const struct got_error *err = NULL;
2536 char *relpath = NULL, *parent_path = NULL;
2537 struct got_fileindex_entry *ie;
2538 struct got_tree_object *tree = NULL;
2539 struct got_object_id *tree_id = NULL;
2540 const struct got_tree_entry *te;
2541 char *tree_path = NULL, *te_name;
2542 struct got_blob_object *blob = NULL;
2543 unsigned char status;
2546 err = got_path_skip_common_ancestor(&relpath,
2547 got_worktree_get_root_path(worktree), ondisk_path);
2551 ie = got_fileindex_entry_get(fileindex, relpath);
2553 err = got_error(GOT_ERR_BAD_PATH);
2557 /* Construct in-repository path of tree which contains this blob. */
2558 err = got_path_dirname(&parent_path, ie->path);
2560 if (err->code != GOT_ERR_BAD_PATH)
2562 parent_path = strdup("/");
2563 if (parent_path == NULL) {
2564 err = got_error_from_errno("strdup");
2568 if (got_path_is_root_dir(worktree->path_prefix)) {
2569 tree_path = strdup(parent_path);
2570 if (tree_path == NULL) {
2571 err = got_error_from_errno("strdup");
2575 if (got_path_is_root_dir(parent_path)) {
2576 tree_path = strdup(worktree->path_prefix);
2577 if (tree_path == NULL) {
2578 err = got_error_from_errno("strdup");
2582 if (asprintf(&tree_path, "%s/%s",
2583 worktree->path_prefix, parent_path) == -1) {
2584 err = got_error_from_errno("asprintf");
2590 err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2595 err = got_object_open_as_tree(&tree, repo, tree_id);
2599 te_name = basename(ie->path);
2600 if (te_name == NULL) {
2601 err = got_error_from_errno2("basename", ie->path);
2605 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2609 te = got_object_tree_find_entry(tree, te_name);
2610 if (te == NULL && status != GOT_STATUS_ADD) {
2611 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2616 case GOT_STATUS_ADD:
2617 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2620 got_fileindex_entry_remove(fileindex, ie);
2622 case GOT_STATUS_DELETE:
2623 case GOT_STATUS_MODIFY:
2624 case GOT_STATUS_CONFLICT:
2625 case GOT_STATUS_MISSING: {
2626 struct got_object_id id;
2627 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2628 err = got_object_open_as_blob(&blob, repo, &id, 8192);
2631 err = install_blob(worktree, ondisk_path, ie->path,
2632 te->mode, sb.st_mode, blob, 0, 1, repo, progress_cb,
2636 if (status == GOT_STATUS_DELETE) {
2637 err = update_blob_fileindex_entry(worktree,
2638 fileindex, ie, ondisk_path, ie->path, blob, 1);
2652 got_object_blob_close(blob);
2654 got_object_tree_close(tree);
2659 const struct got_error *
2660 got_worktree_revert(struct got_worktree *worktree,
2661 struct got_pathlist_head *ondisk_paths,
2662 got_worktree_checkout_cb progress_cb, void *progress_arg,
2663 struct got_repository *repo)
2665 struct got_fileindex *fileindex = NULL;
2666 char *fileindex_path = NULL;
2667 const struct got_error *err = NULL, *unlockerr = NULL;
2668 const struct got_error *sync_err = NULL;
2669 struct got_pathlist_entry *pe;
2671 err = lock_worktree(worktree, LOCK_EX);
2675 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2679 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2680 err = revert_file(worktree, fileindex, pe->path,
2681 progress_cb, progress_arg, repo);
2685 sync_err = sync_fileindex(fileindex, fileindex_path);
2686 if (sync_err && err == NULL)
2689 free(fileindex_path);
2691 got_fileindex_free(fileindex);
2692 unlockerr = lock_worktree(worktree, LOCK_SH);
2693 if (unlockerr && err == NULL)
2699 free_commitable(struct got_commitable *ct)
2702 free(ct->in_repo_path);
2703 free(ct->ondisk_path);
2705 free(ct->base_blob_id);
2706 free(ct->base_commit_id);
2710 struct collect_commitables_arg {
2711 struct got_pathlist_head *commitable_paths;
2712 struct got_repository *repo;
2713 struct got_worktree *worktree;
2716 static const struct got_error *
2717 collect_commitables(void *arg, unsigned char status, const char *relpath,
2718 struct got_object_id *blob_id, struct got_object_id *commit_id)
2720 struct collect_commitables_arg *a = arg;
2721 const struct got_error *err = NULL;
2722 struct got_commitable *ct = NULL;
2723 struct got_pathlist_entry *new = NULL;
2724 char *parent_path = NULL, *path = NULL;
2727 if (status == GOT_STATUS_CONFLICT)
2728 return got_error(GOT_ERR_COMMIT_CONFLICT);
2730 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
2731 status != GOT_STATUS_DELETE)
2734 if (asprintf(&path, "/%s", relpath) == -1) {
2735 err = got_error_from_errno("asprintf");
2738 if (strcmp(path, "/") == 0) {
2739 parent_path = strdup("");
2740 if (parent_path == NULL)
2741 return got_error_from_errno("strdup");
2743 err = got_path_dirname(&parent_path, path);
2748 ct = calloc(1, sizeof(*ct));
2750 err = got_error_from_errno("calloc");
2754 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
2756 err = got_error_from_errno("asprintf");
2759 if (status == GOT_STATUS_DELETE) {
2760 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2762 if (lstat(ct->ondisk_path, &sb) != 0) {
2763 err = got_error_from_errno2("lstat", ct->ondisk_path);
2766 ct->mode = sb.st_mode;
2769 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
2770 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
2772 err = got_error_from_errno("asprintf");
2776 ct->status = status;
2777 ct->blob_id = NULL; /* will be filled in when blob gets created */
2778 if (ct->status != GOT_STATUS_ADD) {
2779 ct->base_blob_id = got_object_id_dup(blob_id);
2780 if (ct->base_blob_id == NULL) {
2781 err = got_error_from_errno("got_object_id_dup");
2784 ct->base_commit_id = got_object_id_dup(commit_id);
2785 if (ct->base_commit_id == NULL) {
2786 err = got_error_from_errno("got_object_id_dup");
2790 ct->path = strdup(path);
2791 if (ct->path == NULL) {
2792 err = got_error_from_errno("strdup");
2795 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
2797 if (ct && (err || new == NULL))
2798 free_commitable(ct);
2804 static const struct got_error *write_tree(struct got_object_id **,
2805 struct got_tree_object *, const char *, struct got_pathlist_head *,
2806 got_worktree_status_cb status_cb, void *status_arg,
2807 struct got_repository *);
2809 static const struct got_error *
2810 write_subtree(struct got_object_id **new_subtree_id,
2811 struct got_tree_entry *te, const char *parent_path,
2812 struct got_pathlist_head *commitable_paths,
2813 got_worktree_status_cb status_cb, void *status_arg,
2814 struct got_repository *repo)
2816 const struct got_error *err = NULL;
2817 struct got_tree_object *subtree;
2820 if (asprintf(&subpath, "%s%s%s", parent_path,
2821 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
2822 return got_error_from_errno("asprintf");
2824 err = got_object_open_as_tree(&subtree, repo, te->id);
2828 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
2829 status_cb, status_arg, repo);
2830 got_object_tree_close(subtree);
2835 static const struct got_error *
2836 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
2838 const struct got_error *err = NULL;
2839 char *ct_parent_path = NULL;
2843 if (strchr(ct->in_repo_path, '/') == NULL) {
2844 *match = got_path_is_root_dir(path);
2848 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
2851 *match = (strcmp(path, ct_parent_path) == 0);
2852 free(ct_parent_path);
2857 get_ct_file_mode(struct got_commitable *ct)
2859 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
2862 static const struct got_error *
2863 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
2864 struct got_tree_entry *te, struct got_commitable *ct)
2866 const struct got_error *err = NULL;
2870 err = got_object_tree_entry_dup(new_te, te);
2874 (*new_te)->mode = get_ct_file_mode(ct);
2876 free((*new_te)->id);
2877 (*new_te)->id = got_object_id_dup(ct->blob_id);
2878 if ((*new_te)->id == NULL) {
2879 err = got_error_from_errno("got_object_id_dup");
2883 if (err && *new_te) {
2884 got_object_tree_entry_close(*new_te);
2890 static const struct got_error *
2891 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2892 struct got_commitable *ct)
2894 const struct got_error *err = NULL;
2899 *new_te = calloc(1, sizeof(**new_te));
2900 if (*new_te == NULL)
2901 return got_error_from_errno("calloc");
2903 ct_name = basename(ct->path);
2904 if (ct_name == NULL) {
2905 err = got_error_from_errno2("basename", ct->path);
2908 (*new_te)->name = strdup(ct_name);
2909 if ((*new_te)->name == NULL) {
2910 err = got_error_from_errno("strdup");
2914 (*new_te)->mode = get_ct_file_mode(ct);
2916 (*new_te)->id = got_object_id_dup(ct->blob_id);
2917 if ((*new_te)->id == NULL) {
2918 err = got_error_from_errno("got_object_id_dup");
2922 if (err && *new_te) {
2923 got_object_tree_entry_close(*new_te);
2929 static const struct got_error *
2930 insert_tree_entry(struct got_tree_entry *new_te,
2931 struct got_pathlist_head *paths)
2933 const struct got_error *err = NULL;
2934 struct got_pathlist_entry *new_pe;
2936 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2940 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2944 static const struct got_error *
2945 report_ct_status(struct got_commitable *ct,
2946 got_worktree_status_cb status_cb, void *status_arg)
2948 const char *ct_path = ct->path;
2949 while (ct_path[0] == '/')
2951 return (*status_cb)(status_arg, ct->status, ct_path, ct->blob_id, NULL);
2954 static const struct got_error *
2955 match_modified_subtree(int *modified, struct got_tree_entry *te,
2956 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
2958 const struct got_error *err = NULL;
2959 struct got_pathlist_entry *pe;
2964 if (asprintf(&te_path, "%s%s%s", base_tree_path,
2965 got_path_is_root_dir(base_tree_path) ? "" : "/",
2967 return got_error_from_errno("asprintf");
2969 TAILQ_FOREACH(pe, commitable_paths, entry) {
2970 struct got_commitable *ct = pe->data;
2971 *modified = got_path_is_child(ct->in_repo_path, te_path,
2981 static const struct got_error *
2982 match_deleted_or_modified_ct(struct got_commitable **ctp,
2983 struct got_tree_entry *te, const char *base_tree_path,
2984 struct got_pathlist_head *commitable_paths)
2986 const struct got_error *err = NULL;
2987 struct got_pathlist_entry *pe;
2991 TAILQ_FOREACH(pe, commitable_paths, entry) {
2992 struct got_commitable *ct = pe->data;
2993 char *ct_name = NULL;
2996 if (ct->status != GOT_STATUS_MODIFY &&
2997 ct->status != GOT_STATUS_DELETE)
3000 if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
3003 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
3009 ct_name = basename(pe->path);
3010 if (ct_name == NULL)
3011 return got_error_from_errno2("basename", pe->path);
3013 if (strcmp(te->name, ct_name) != 0)
3023 static const struct got_error *
3024 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
3025 const char *child_path, const char *path_base_tree,
3026 struct got_pathlist_head *commitable_paths,
3027 got_worktree_status_cb status_cb, void *status_arg,
3028 struct got_repository *repo)
3030 const struct got_error *err = NULL;
3031 struct got_tree_entry *new_te;
3036 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
3037 got_path_is_root_dir(path_base_tree) ? "" : "/",
3039 return got_error_from_errno("asprintf");
3041 new_te = calloc(1, sizeof(*new_te));
3042 new_te->mode = S_IFDIR;
3043 new_te->name = strdup(child_path);
3044 if (new_te->name == NULL) {
3045 err = got_error_from_errno("strdup");
3046 got_object_tree_entry_close(new_te);
3049 err = write_tree(&new_te->id, NULL, subtree_path,
3050 commitable_paths, status_cb, status_arg, repo);
3052 got_object_tree_entry_close(new_te);
3062 static const struct got_error *
3063 write_tree(struct got_object_id **new_tree_id,
3064 struct got_tree_object *base_tree, const char *path_base_tree,
3065 struct got_pathlist_head *commitable_paths,
3066 got_worktree_status_cb status_cb, void *status_arg,
3067 struct got_repository *repo)
3069 const struct got_error *err = NULL;
3070 const struct got_tree_entries *base_entries = NULL;
3071 struct got_pathlist_head paths;
3072 struct got_tree_entries new_tree_entries;
3073 struct got_tree_entry *te, *new_te = NULL;
3074 struct got_pathlist_entry *pe;
3077 new_tree_entries.nentries = 0;
3078 SIMPLEQ_INIT(&new_tree_entries.head);
3080 /* Insert, and recurse into, newly added entries first. */
3081 TAILQ_FOREACH(pe, commitable_paths, entry) {
3082 struct got_commitable *ct = pe->data;
3083 char *child_path = NULL, *slash;
3085 if (ct->status != GOT_STATUS_ADD ||
3086 (ct->flags & GOT_COMMITABLE_ADDED))
3089 if (!got_path_is_child(pe->path, path_base_tree,
3090 strlen(path_base_tree)))
3093 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
3098 slash = strchr(child_path, '/');
3099 if (slash == NULL) {
3100 err = alloc_added_blob_tree_entry(&new_te, ct);
3103 err = report_ct_status(ct, status_cb, status_arg);
3106 ct->flags |= GOT_COMMITABLE_ADDED;
3107 err = insert_tree_entry(new_te, &paths);
3111 *slash = '\0'; /* trim trailing path components */
3112 if (base_tree == NULL ||
3113 got_object_tree_find_entry(base_tree, child_path)
3115 err = make_subtree_for_added_blob(&new_te,
3116 child_path, path_base_tree,
3117 commitable_paths, status_cb, status_arg,
3121 err = insert_tree_entry(new_te, &paths);
3129 /* Handle modified and deleted entries. */
3130 base_entries = got_object_tree_get_entries(base_tree);
3131 SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
3132 struct got_commitable *ct = NULL;
3134 if (S_ISDIR(te->mode)) {
3136 err = got_object_tree_entry_dup(&new_te, te);
3139 err = match_modified_subtree(&modified, te,
3140 path_base_tree, commitable_paths);
3143 /* Avoid recursion into unmodified subtrees. */
3146 err = write_subtree(&new_te->id, te,
3147 path_base_tree, commitable_paths,
3148 status_cb, status_arg, repo);
3152 err = insert_tree_entry(new_te, &paths);
3158 err = match_deleted_or_modified_ct(&ct, te,
3159 path_base_tree, commitable_paths);
3161 /* NB: Deleted entries get dropped here. */
3162 if (ct->status == GOT_STATUS_MODIFY) {
3163 err = alloc_modified_blob_tree_entry(
3167 err = insert_tree_entry(new_te, &paths);
3171 err = report_ct_status(ct, status_cb,
3176 /* Entry is unchanged; just copy it. */
3177 err = got_object_tree_entry_dup(&new_te, te);
3180 err = insert_tree_entry(new_te, &paths);
3187 /* Write new list of entries; deleted entries have been dropped. */
3188 TAILQ_FOREACH(pe, &paths, entry) {
3189 struct got_tree_entry *te = pe->data;
3190 new_tree_entries.nentries++;
3191 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
3193 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
3195 got_object_tree_entries_close(&new_tree_entries);
3196 got_pathlist_free(&paths);
3200 static const struct got_error *
3201 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
3202 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex)
3204 const struct got_error *err = NULL;
3205 struct got_pathlist_entry *pe;
3207 TAILQ_FOREACH(pe, commitable_paths, entry) {
3208 struct got_fileindex_entry *ie;
3209 struct got_commitable *ct = pe->data;
3211 ie = got_fileindex_entry_get(fileindex, pe->path);
3213 if (ct->status == GOT_STATUS_DELETE) {
3214 got_fileindex_entry_remove(fileindex, ie);
3215 got_fileindex_entry_free(ie);
3217 err = got_fileindex_entry_update(ie,
3218 ct->ondisk_path, ct->blob_id->sha1,
3219 new_base_commit_id->sha1, 1);
3221 err = got_fileindex_entry_alloc(&ie,
3222 ct->ondisk_path, pe->path, ct->blob_id->sha1,
3223 new_base_commit_id->sha1);
3226 err = got_fileindex_entry_add(fileindex, ie);
3234 static const struct got_error *
3235 check_ct_out_of_date(struct got_commitable *ct, struct got_repository *repo,
3236 struct got_object_id *head_commit_id)
3238 const struct got_error *err = NULL;
3239 struct got_object_id *id_in_head = NULL, *id = NULL;
3240 struct got_commit_object *commit = NULL;
3242 const char *ct_path = ct->in_repo_path;
3244 while (ct_path[0] == '/')
3248 * Ensure that no modifications were made to files *and their parents*
3249 * in commits between the file's base commit and the branch head.
3251 * Checking the parents is important for detecting conflicting tree
3252 * configurations (files or parent folders might have been moved,
3253 * deleted, added again, etc.). Such changes need to be merged with
3254 * local changes before a commit can occur.
3256 * The implication is that the file's (parent) entry in the root
3257 * directory must have the same ID in all relevant commits.
3259 if (ct->status != GOT_STATUS_ADD) {
3260 struct got_object_qid *pid;
3262 struct got_object_id *root_entry_id = NULL;
3264 /* Trivial case: base commit == head commit */
3265 if (got_object_id_cmp(ct->base_commit_id, head_commit_id) == 0)
3268 /* Compute the path to the root directory's entry. */
3269 path = strdup(ct_path);
3271 err = got_error_from_errno("strdup");
3274 slash = strchr(path, '/');
3278 err = got_object_open_as_commit(&commit, repo, head_commit_id);
3282 err = got_object_id_by_path(&root_entry_id, repo,
3283 head_commit_id, path);
3287 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3289 struct got_commit_object *pcommit;
3291 err = got_object_id_by_path(&id, repo, pid->id, path);
3293 if (err->code != GOT_ERR_NO_TREE_ENTRY)
3299 err = got_object_id_by_path(&id, repo, pid->id, path);
3303 if (got_object_id_cmp(id, root_entry_id) != 0) {
3304 err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
3308 if (got_object_id_cmp(pid->id, ct->base_commit_id) == 0)
3309 break; /* all relevant commits scanned */
3311 err = got_object_open_as_commit(&pcommit, repo,
3316 got_object_commit_close(commit);
3318 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(
3322 /* Require that added files don't exist in the branch head. */
3323 err = got_object_id_by_path(&id_in_head, repo, head_commit_id,
3325 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3327 err = id_in_head ? got_error(GOT_ERR_COMMIT_OUT_OF_DATE) : NULL;
3331 got_object_commit_close(commit);
3338 const struct got_error *
3339 commit_worktree(struct got_object_id **new_commit_id,
3340 struct got_pathlist_head *commitable_paths,
3341 struct got_object_id *head_commit_id, struct got_worktree *worktree,
3342 const char *ondisk_path, const char *author, const char *committer,
3343 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3344 got_worktree_status_cb status_cb, void *status_arg,
3345 struct got_repository *repo)
3347 const struct got_error *err = NULL, *unlockerr = NULL;
3348 struct got_pathlist_entry *pe;
3349 const char *head_ref_name = NULL;
3350 struct got_commit_object *head_commit = NULL;
3351 struct got_reference *head_ref2 = NULL;
3352 struct got_object_id *head_commit_id2 = NULL;
3353 struct got_tree_object *head_tree = NULL;
3354 struct got_object_id *new_tree_id = NULL;
3355 struct got_object_id_queue parent_ids;
3356 struct got_object_qid *pid = NULL;
3357 char *logmsg = NULL;
3359 *new_commit_id = NULL;
3361 SIMPLEQ_INIT(&parent_ids);
3363 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
3367 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
3371 if (commit_msg_cb != NULL) {
3372 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
3377 if (logmsg == NULL || strlen(logmsg) == 0) {
3378 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
3382 /* Create blobs from added and modified files and record their IDs. */
3383 TAILQ_FOREACH(pe, commitable_paths, entry) {
3384 struct got_commitable *ct = pe->data;
3387 if (ct->status != GOT_STATUS_ADD &&
3388 ct->status != GOT_STATUS_MODIFY)
3391 if (asprintf(&ondisk_path, "%s/%s",
3392 worktree->root_path, pe->path) == -1) {
3393 err = got_error_from_errno("asprintf");
3396 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
3402 /* Recursively write new tree objects. */
3403 err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
3404 status_cb, status_arg, repo);
3408 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
3411 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
3412 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
3413 1, author, time(NULL), committer, time(NULL), logmsg, repo);
3414 got_object_qid_free(pid);
3420 /* Check if a concurrent commit to our branch has occurred. */
3421 head_ref_name = got_worktree_get_head_ref_name(worktree);
3422 if (head_ref_name == NULL) {
3423 err = got_error_from_errno("got_worktree_get_head_ref_name");
3426 /* Lock the reference here to prevent concurrent modification. */
3427 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
3430 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
3433 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
3434 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
3437 /* Update branch head in repository. */
3438 err = got_ref_change_ref(head_ref2, *new_commit_id);
3441 err = got_ref_write(head_ref2, repo);
3445 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
3449 err = ref_base_commit(worktree, repo);
3454 got_object_tree_close(head_tree);
3456 got_object_commit_close(head_commit);
3457 free(head_commit_id2);
3459 unlockerr = got_ref_unlock(head_ref2);
3460 if (unlockerr && err == NULL)
3462 got_ref_close(head_ref2);
3467 const struct got_error *
3468 got_worktree_commit(struct got_object_id **new_commit_id,
3469 struct got_worktree *worktree, const char *ondisk_path,
3470 const char *author, const char *committer,
3471 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3472 got_worktree_status_cb status_cb, void *status_arg,
3473 struct got_repository *repo)
3475 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
3476 struct got_fileindex *fileindex = NULL;
3477 char *fileindex_path = NULL, *relpath = NULL;
3478 struct got_pathlist_head commitable_paths;
3479 struct collect_commitables_arg cc_arg;
3480 struct got_pathlist_entry *pe;
3481 struct got_reference *head_ref = NULL;
3482 struct got_object_id *head_commit_id = NULL;
3484 *new_commit_id = NULL;
3486 TAILQ_INIT(&commitable_paths);
3488 err = lock_worktree(worktree, LOCK_EX);
3492 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
3496 err = got_ref_resolve(&head_commit_id, repo, head_ref);
3501 err = got_path_skip_common_ancestor(&relpath,
3502 worktree->root_path, ondisk_path);
3507 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3511 cc_arg.commitable_paths = &commitable_paths;
3512 cc_arg.worktree = worktree;
3514 err = worktree_status(worktree, relpath ? relpath : "",
3515 fileindex, repo, collect_commitables, &cc_arg, NULL, NULL);
3519 if (TAILQ_EMPTY(&commitable_paths)) {
3520 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
3524 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3525 struct got_commitable *ct = pe->data;
3526 err = check_ct_out_of_date(ct, repo, head_commit_id);
3531 err = commit_worktree(new_commit_id, &commitable_paths,
3532 head_commit_id, worktree, ondisk_path, author, committer,
3533 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
3537 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
3539 sync_err = sync_fileindex(fileindex, fileindex_path);
3540 if (sync_err && err == NULL)
3544 got_fileindex_free(fileindex);
3545 free(fileindex_path);
3547 unlockerr = lock_worktree(worktree, LOCK_SH);
3548 if (unlockerr && err == NULL)
3550 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3551 struct got_commitable *ct = pe->data;
3552 free_commitable(ct);
3554 got_pathlist_free(&commitable_paths);
3559 got_commitable_get_path(struct got_commitable *ct)
3565 got_commitable_get_status(struct got_commitable *ct)
3570 struct check_rebase_ok_arg {
3571 struct got_worktree *worktree;
3572 struct got_repository *repo;
3573 int rebase_in_progress;
3576 static const struct got_error *
3577 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
3579 const struct got_error *err = NULL;
3580 struct check_rebase_ok_arg *a = arg;
3581 unsigned char status;
3585 if (!a->rebase_in_progress) {
3586 /* Reject rebase of a work tree with mixed base commits. */
3587 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3588 SHA1_DIGEST_LENGTH))
3589 return got_error(GOT_ERR_MIXED_COMMITS);
3592 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3594 return got_error_from_errno("asprintf");
3596 /* Reject rebase of a work tree with modified or conflicted files. */
3597 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
3602 if (a->rebase_in_progress) {
3603 if (status == GOT_STATUS_CONFLICT)
3604 return got_error(GOT_ERR_CONFLICTS);
3605 } else if (status != GOT_STATUS_NO_CHANGE)
3606 return got_error(GOT_ERR_MODIFIED);
3611 const struct got_error *
3612 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
3613 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
3614 struct got_worktree *worktree, struct got_reference *branch,
3615 struct got_repository *repo)
3617 const struct got_error *err = NULL;
3618 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
3619 char *branch_ref_name = NULL;
3620 char *fileindex_path = NULL;
3621 struct check_rebase_ok_arg ok_arg;
3622 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
3624 *new_base_branch_ref = NULL;
3628 err = lock_worktree(worktree, LOCK_EX);
3632 err = open_fileindex(fileindex, &fileindex_path, worktree);
3636 ok_arg.worktree = worktree;
3638 ok_arg.rebase_in_progress = 0;
3639 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
3644 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3648 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3652 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3656 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
3661 err = got_ref_alloc_symref(new_base_branch_ref,
3662 new_base_branch_ref_name, wt_branch);
3665 err = got_ref_write(*new_base_branch_ref, repo);
3669 /* TODO Lock original branch's ref while rebasing? */
3671 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
3675 err = got_ref_write(branch_ref, repo);
3679 err = got_ref_alloc(tmp_branch, tmp_branch_name,
3680 worktree->base_commit_id);
3683 err = got_ref_write(*tmp_branch, repo);
3687 err = got_worktree_set_head_ref(worktree, *tmp_branch);
3691 free(fileindex_path);
3692 free(tmp_branch_name);
3693 free(new_base_branch_ref_name);
3694 free(branch_ref_name);
3696 got_ref_close(branch_ref);
3698 got_ref_close(wt_branch);
3700 if (*new_base_branch_ref) {
3701 got_ref_close(*new_base_branch_ref);
3702 *new_base_branch_ref = NULL;
3705 got_ref_close(*tmp_branch);
3709 got_fileindex_free(*fileindex);
3712 lock_worktree(worktree, LOCK_SH);
3717 const struct got_error *
3718 got_worktree_rebase_continue(struct got_object_id **commit_id,
3719 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
3720 struct got_reference **branch, struct got_fileindex **fileindex,
3721 struct got_worktree *worktree, struct got_repository *repo)
3723 const struct got_error *err;
3724 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
3725 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
3726 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
3727 char *fileindex_path = NULL;
3730 *new_base_branch = NULL;
3735 err = lock_worktree(worktree, LOCK_EX);
3739 err = open_fileindex(fileindex, &fileindex_path, worktree);
3743 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3747 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3751 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3755 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3759 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
3763 err = got_ref_open(branch, repo,
3764 got_ref_get_symref_target(branch_ref), 0);
3768 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3772 err = got_ref_resolve(commit_id, repo, commit_ref);
3776 err = got_ref_open(new_base_branch, repo,
3777 new_base_branch_ref_name, 0);
3781 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
3785 free(commit_ref_name);
3786 free(branch_ref_name);
3787 free(fileindex_path);
3789 got_ref_close(commit_ref);
3791 got_ref_close(branch_ref);
3796 got_ref_close(*tmp_branch);
3799 if (*new_base_branch) {
3800 got_ref_close(*new_base_branch);
3801 *new_base_branch = NULL;
3804 got_ref_close(*branch);
3808 got_fileindex_free(*fileindex);
3811 lock_worktree(worktree, LOCK_SH);
3816 const struct got_error *
3817 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
3819 const struct got_error *err;
3820 char *tmp_branch_name = NULL;
3822 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3826 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
3827 free(tmp_branch_name);
3831 static const struct got_error *
3832 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
3833 char **logmsg, void *arg)
3839 static const struct got_error *
3840 rebase_status(void *arg, unsigned char status, const char *path,
3841 struct got_object_id *blob_id, struct got_object_id *commit_id)
3846 struct collect_merged_paths_arg {
3847 got_worktree_checkout_cb progress_cb;
3849 struct got_pathlist_head *merged_paths;
3852 static const struct got_error *
3853 collect_merged_paths(void *arg, unsigned char status, const char *path)
3855 const struct got_error *err;
3856 struct collect_merged_paths_arg *a = arg;
3858 struct got_pathlist_entry *new;
3860 err = (*a->progress_cb)(a->progress_arg, status, path);
3864 if (status != GOT_STATUS_MERGE &&
3865 status != GOT_STATUS_ADD &&
3866 status != GOT_STATUS_DELETE &&
3867 status != GOT_STATUS_CONFLICT)
3872 return got_error_from_errno("strdup");
3874 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
3875 if (err || new == NULL)
3881 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
3883 struct got_pathlist_entry *pe;
3885 TAILQ_FOREACH(pe, merged_paths, entry)
3886 free((char *)pe->path);
3888 got_pathlist_free(merged_paths);
3891 static const struct got_error *
3892 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
3893 struct got_repository *repo)
3895 const struct got_error *err;
3896 struct got_reference *commit_ref = NULL;
3898 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3900 if (err->code != GOT_ERR_NOT_REF)
3902 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
3905 err = got_ref_write(commit_ref, repo);
3909 struct got_object_id *stored_id;
3912 err = got_ref_resolve(&stored_id, repo, commit_ref);
3915 cmp = got_object_id_cmp(commit_id, stored_id);
3918 err = got_error(GOT_ERR_REBASE_COMMITID);
3924 got_ref_close(commit_ref);
3928 static const struct got_error *
3929 rebase_merge_files(struct got_pathlist_head *merged_paths,
3930 const char *commit_ref_name, struct got_worktree *worktree,
3931 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
3932 struct got_object_id *commit_id, struct got_repository *repo,
3933 got_worktree_checkout_cb progress_cb, void *progress_arg,
3934 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
3936 const struct got_error *err;
3937 struct got_reference *commit_ref = NULL;
3938 struct collect_merged_paths_arg cmp_arg;
3939 char *fileindex_path;
3941 /* Work tree is locked/unlocked during rebase preparation/teardown. */
3943 err = get_fileindex_path(&fileindex_path, worktree);
3947 cmp_arg.progress_cb = progress_cb;
3948 cmp_arg.progress_arg = progress_arg;
3949 cmp_arg.merged_paths = merged_paths;
3950 err = merge_files(worktree, fileindex, fileindex_path,
3951 parent_commit_id, commit_id, repo, collect_merged_paths,
3952 &cmp_arg, cancel_cb, cancel_arg);
3954 got_ref_close(commit_ref);
3958 const struct got_error *
3959 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
3960 struct got_worktree *worktree, struct got_fileindex *fileindex,
3961 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
3962 struct got_repository *repo,
3963 got_worktree_checkout_cb progress_cb, void *progress_arg,
3964 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
3966 const struct got_error *err;
3967 char *commit_ref_name;
3969 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3973 err = store_commit_id(commit_ref_name, commit_id, repo);
3977 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
3978 fileindex, parent_commit_id, commit_id, repo, progress_cb,
3979 progress_arg, cancel_cb, cancel_arg);
3981 free(commit_ref_name);
3985 const struct got_error *
3986 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
3987 struct got_worktree *worktree, struct got_fileindex *fileindex,
3988 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
3989 struct got_repository *repo,
3990 got_worktree_checkout_cb progress_cb, void *progress_arg,
3991 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
3993 const struct got_error *err;
3994 char *commit_ref_name;
3996 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4000 err = store_commit_id(commit_ref_name, commit_id, repo);
4004 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4005 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4006 progress_arg, cancel_cb, cancel_arg);
4008 free(commit_ref_name);
4012 static const struct got_error *
4013 rebase_commit(struct got_object_id **new_commit_id,
4014 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
4015 struct got_worktree *worktree, struct got_fileindex *fileindex,
4016 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
4017 const char *new_logmsg, struct got_repository *repo)
4019 const struct got_error *err, *sync_err;
4020 struct got_pathlist_head commitable_paths;
4021 struct collect_commitables_arg cc_arg;
4022 char *fileindex_path = NULL;
4023 struct got_reference *head_ref = NULL;
4024 struct got_object_id *head_commit_id = NULL;
4025 char *logmsg = NULL;
4027 TAILQ_INIT(&commitable_paths);
4028 *new_commit_id = NULL;
4030 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4032 err = get_fileindex_path(&fileindex_path, worktree);
4036 cc_arg.commitable_paths = &commitable_paths;
4037 cc_arg.worktree = worktree;
4040 * If possible get the status of individual files directly to
4041 * avoid crawling the entire work tree once per rebased commit.
4042 * TODO: Ideally, merged_paths would contain a list of commitables
4043 * we could use so we could skip worktree_status() entirely.
4046 struct got_pathlist_entry *pe;
4047 if (TAILQ_EMPTY(merged_paths)) {
4048 err = got_error(GOT_ERR_NO_MERGED_PATHS);
4051 TAILQ_FOREACH(pe, merged_paths, entry) {
4052 err = worktree_status(worktree, pe->path, fileindex,
4053 repo, collect_commitables, &cc_arg, NULL, NULL);
4058 err = worktree_status(worktree, "", fileindex, repo,
4059 collect_commitables, &cc_arg, NULL, NULL);
4064 if (TAILQ_EMPTY(&commitable_paths)) {
4065 /* No-op change; commit will be elided. */
4066 err = got_ref_delete(commit_ref, repo);
4069 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4073 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4077 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4082 logmsg = strdup(new_logmsg);
4084 logmsg = strdup(got_object_commit_get_logmsg(orig_commit));
4086 return got_error_from_errno("strdup");
4088 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
4089 worktree, NULL, got_object_commit_get_author(orig_commit),
4090 got_object_commit_get_committer(orig_commit),
4091 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
4095 err = got_ref_change_ref(tmp_branch, *new_commit_id);
4099 err = got_ref_delete(commit_ref, repo);
4103 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4105 sync_err = sync_fileindex(fileindex, fileindex_path);
4106 if (sync_err && err == NULL)
4109 free(fileindex_path);
4110 free(head_commit_id);
4112 got_ref_close(head_ref);
4114 free(*new_commit_id);
4115 *new_commit_id = NULL;
4120 const struct got_error *
4121 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
4122 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4123 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4124 struct got_commit_object *orig_commit,
4125 struct got_object_id *orig_commit_id, struct got_repository *repo)
4127 const struct got_error *err;
4128 char *commit_ref_name;
4129 struct got_reference *commit_ref = NULL;
4130 struct got_object_id *commit_id = NULL;
4132 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4136 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4139 err = got_ref_resolve(&commit_id, repo, commit_ref);
4142 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4143 err = got_error(GOT_ERR_REBASE_COMMITID);
4147 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4148 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
4151 got_ref_close(commit_ref);
4152 free(commit_ref_name);
4157 const struct got_error *
4158 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
4159 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4160 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4161 struct got_commit_object *orig_commit,
4162 struct got_object_id *orig_commit_id, const char *new_logmsg,
4163 struct got_repository *repo)
4165 const struct got_error *err;
4166 char *commit_ref_name;
4167 struct got_reference *commit_ref = NULL;
4168 struct got_object_id *commit_id = NULL;
4170 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4174 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4177 err = got_ref_resolve(&commit_id, repo, commit_ref);
4180 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4181 err = got_error(GOT_ERR_HISTEDIT_COMMITID);
4185 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4186 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
4189 got_ref_close(commit_ref);
4190 free(commit_ref_name);
4195 const struct got_error *
4196 got_worktree_rebase_postpone(struct got_worktree *worktree,
4197 struct got_fileindex *fileindex)
4200 got_fileindex_free(fileindex);
4201 return lock_worktree(worktree, LOCK_SH);
4204 static const struct got_error *
4205 delete_ref(const char *name, struct got_repository *repo)
4207 const struct got_error *err;
4208 struct got_reference *ref;
4210 err = got_ref_open(&ref, repo, name, 0);
4212 if (err->code == GOT_ERR_NOT_REF)
4217 err = got_ref_delete(ref, repo);
4222 static const struct got_error *
4223 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
4225 const struct got_error *err;
4226 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4227 char *branch_ref_name = NULL, *commit_ref_name = NULL;
4229 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4232 err = delete_ref(tmp_branch_name, repo);
4236 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4239 err = delete_ref(new_base_branch_ref_name, repo);
4243 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4246 err = delete_ref(branch_ref_name, repo);
4250 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4253 err = delete_ref(commit_ref_name, repo);
4258 free(tmp_branch_name);
4259 free(new_base_branch_ref_name);
4260 free(branch_ref_name);
4261 free(commit_ref_name);
4265 const struct got_error *
4266 got_worktree_rebase_complete(struct got_worktree *worktree,
4267 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
4268 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
4269 struct got_repository *repo)
4271 const struct got_error *err, *unlockerr;
4272 struct got_object_id *new_head_commit_id = NULL;
4274 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
4278 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
4282 err = got_ref_write(rebased_branch, repo);
4286 err = got_worktree_set_head_ref(worktree, rebased_branch);
4290 err = delete_rebase_refs(worktree, repo);
4293 got_fileindex_free(fileindex);
4294 free(new_head_commit_id);
4295 unlockerr = lock_worktree(worktree, LOCK_SH);
4296 if (unlockerr && err == NULL)
4301 struct collect_revertible_paths_arg {
4302 struct got_pathlist_head *revertible_paths;
4303 struct got_worktree *worktree;
4306 static const struct got_error *
4307 collect_revertible_paths(void *arg, unsigned char status, const char *relpath,
4308 struct got_object_id *blob_id, struct got_object_id *commit_id)
4310 struct collect_revertible_paths_arg *a = arg;
4311 const struct got_error *err = NULL;
4312 struct got_pathlist_entry *new = NULL;
4315 if (status != GOT_STATUS_ADD &&
4316 status != GOT_STATUS_DELETE &&
4317 status != GOT_STATUS_MODIFY &&
4318 status != GOT_STATUS_CONFLICT &&
4319 status != GOT_STATUS_MISSING)
4322 if (asprintf(&path, "%s/%s", a->worktree->root_path, relpath) == -1)
4323 return got_error_from_errno("asprintf");
4325 err = got_pathlist_insert(&new, a->revertible_paths, path, NULL);
4326 if (err || new == NULL)
4331 const struct got_error *
4332 got_worktree_rebase_abort(struct got_worktree *worktree,
4333 struct got_fileindex *fileindex, struct got_repository *repo,
4334 struct got_reference *new_base_branch,
4335 got_worktree_checkout_cb progress_cb, void *progress_arg)
4337 const struct got_error *err, *unlockerr, *sync_err;
4338 struct got_reference *resolved = NULL;
4339 struct got_object_id *commit_id = NULL;
4340 char *fileindex_path = NULL;
4341 struct got_pathlist_head revertible_paths;
4342 struct got_pathlist_entry *pe;
4343 struct collect_revertible_paths_arg crp_arg;
4344 struct got_object_id *tree_id = NULL;
4346 TAILQ_INIT(&revertible_paths);
4348 err = lock_worktree(worktree, LOCK_EX);
4352 err = got_ref_open(&resolved, repo,
4353 got_ref_get_symref_target(new_base_branch), 0);
4357 err = got_worktree_set_head_ref(worktree, resolved);
4362 * XXX commits to the base branch could have happened while
4363 * we were busy rebasing; should we store the original commit ID
4364 * when rebase begins and read it back here?
4366 err = got_ref_resolve(&commit_id, repo, resolved);
4370 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
4374 err = got_object_id_by_path(&tree_id, repo,
4375 worktree->base_commit_id, worktree->path_prefix);
4379 err = delete_rebase_refs(worktree, repo);
4383 err = get_fileindex_path(&fileindex_path, worktree);
4387 crp_arg.revertible_paths = &revertible_paths;
4388 crp_arg.worktree = worktree;
4389 err = worktree_status(worktree, "", fileindex, repo,
4390 collect_revertible_paths, &crp_arg, NULL, NULL);
4394 TAILQ_FOREACH(pe, &revertible_paths, entry) {
4395 err = revert_file(worktree, fileindex, pe->path,
4396 progress_cb, progress_arg, repo);
4401 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
4402 repo, progress_cb, progress_arg, NULL, NULL);
4404 sync_err = sync_fileindex(fileindex, fileindex_path);
4405 if (sync_err && err == NULL)
4408 got_ref_close(resolved);
4412 got_fileindex_free(fileindex);
4413 free(fileindex_path);
4414 TAILQ_FOREACH(pe, &revertible_paths, entry)
4415 free((char *)pe->path);
4416 got_pathlist_free(&revertible_paths);
4418 unlockerr = lock_worktree(worktree, LOCK_SH);
4419 if (unlockerr && err == NULL)
4424 const struct got_error *
4425 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
4426 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
4427 struct got_fileindex **fileindex, struct got_worktree *worktree,
4428 struct got_repository *repo)
4430 const struct got_error *err = NULL;
4431 char *tmp_branch_name = NULL;
4432 char *branch_ref_name = NULL;
4433 char *base_commit_ref_name = NULL;
4434 char *fileindex_path = NULL;
4435 struct check_rebase_ok_arg ok_arg;
4436 struct got_reference *wt_branch = NULL;
4437 struct got_reference *base_commit_ref = NULL;
4441 *base_commit_id = NULL;
4444 err = lock_worktree(worktree, LOCK_EX);
4448 err = open_fileindex(fileindex, &fileindex_path, worktree);
4452 ok_arg.worktree = worktree;
4454 ok_arg.rebase_in_progress = 0;
4455 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4460 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4464 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4468 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4473 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
4478 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
4482 err = got_ref_write(*branch_ref, repo);
4486 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
4487 worktree->base_commit_id);
4490 err = got_ref_write(base_commit_ref, repo);
4493 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
4494 if (*base_commit_id == NULL) {
4495 err = got_error_from_errno("got_object_id_dup");
4499 err = got_ref_alloc(tmp_branch, tmp_branch_name,
4500 worktree->base_commit_id);
4503 err = got_ref_write(*tmp_branch, repo);
4507 err = got_worktree_set_head_ref(worktree, *tmp_branch);
4511 free(fileindex_path);
4512 free(tmp_branch_name);
4513 free(branch_ref_name);
4514 free(base_commit_ref_name);
4516 got_ref_close(wt_branch);
4519 got_ref_close(*branch_ref);
4523 got_ref_close(*tmp_branch);
4526 free(*base_commit_id);
4528 got_fileindex_free(*fileindex);
4531 lock_worktree(worktree, LOCK_SH);
4536 const struct got_error *
4537 got_worktree_histedit_postpone(struct got_worktree *worktree,
4538 struct got_fileindex *fileindex)
4541 got_fileindex_free(fileindex);
4542 return lock_worktree(worktree, LOCK_SH);
4545 const struct got_error *
4546 got_worktree_histedit_in_progress(int *in_progress,
4547 struct got_worktree *worktree)
4549 const struct got_error *err;
4550 char *tmp_branch_name = NULL;
4552 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4556 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4557 free(tmp_branch_name);
4561 const struct got_error *
4562 got_worktree_histedit_continue(struct got_object_id **commit_id,
4563 struct got_reference **tmp_branch, struct got_reference **branch_ref,
4564 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
4565 struct got_worktree *worktree, struct got_repository *repo)
4567 const struct got_error *err;
4568 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
4569 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
4570 struct got_reference *commit_ref = NULL;
4571 struct got_reference *base_commit_ref = NULL;
4572 char *fileindex_path = NULL;
4576 *base_commit_id = NULL;
4579 err = lock_worktree(worktree, LOCK_EX);
4583 err = open_fileindex(fileindex, &fileindex_path, worktree);
4587 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4591 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4595 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4599 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4604 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
4608 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4611 err = got_ref_resolve(commit_id, repo, commit_ref);
4615 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
4618 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
4622 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4626 free(commit_ref_name);
4627 free(branch_ref_name);
4628 free(fileindex_path);
4630 got_ref_close(commit_ref);
4631 if (base_commit_ref)
4632 got_ref_close(base_commit_ref);
4636 free(*base_commit_id);
4637 *base_commit_id = NULL;
4639 got_ref_close(*tmp_branch);
4643 got_fileindex_free(*fileindex);
4646 lock_worktree(worktree, LOCK_EX);
4651 static const struct got_error *
4652 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
4654 const struct got_error *err;
4655 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
4656 char *branch_ref_name = NULL, *commit_ref_name = NULL;
4658 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4661 err = delete_ref(tmp_branch_name, repo);
4665 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4669 err = delete_ref(base_commit_ref_name, repo);
4673 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4676 err = delete_ref(branch_ref_name, repo);
4680 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4683 err = delete_ref(commit_ref_name, repo);
4687 free(tmp_branch_name);
4688 free(base_commit_ref_name);
4689 free(branch_ref_name);
4690 free(commit_ref_name);
4694 const struct got_error *
4695 got_worktree_histedit_abort(struct got_worktree *worktree,
4696 struct got_fileindex *fileindex, struct got_repository *repo,
4697 struct got_reference *branch, struct got_object_id *base_commit_id,
4698 got_worktree_checkout_cb progress_cb, void *progress_arg)
4700 const struct got_error *err, *unlockerr, *sync_err;
4701 struct got_reference *resolved = NULL;
4702 char *fileindex_path = NULL;
4703 struct got_pathlist_head revertible_paths;
4704 struct got_pathlist_entry *pe;
4705 struct collect_revertible_paths_arg crp_arg;
4706 struct got_object_id *tree_id = NULL;
4708 TAILQ_INIT(&revertible_paths);
4710 err = lock_worktree(worktree, LOCK_EX);
4714 err = got_ref_open(&resolved, repo,
4715 got_ref_get_symref_target(branch), 0);
4719 err = got_worktree_set_head_ref(worktree, resolved);
4723 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
4727 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
4728 worktree->path_prefix);
4732 err = delete_histedit_refs(worktree, repo);
4736 err = get_fileindex_path(&fileindex_path, worktree);
4740 crp_arg.revertible_paths = &revertible_paths;
4741 crp_arg.worktree = worktree;
4742 err = worktree_status(worktree, "", fileindex, repo,
4743 collect_revertible_paths, &crp_arg, NULL, NULL);
4747 TAILQ_FOREACH(pe, &revertible_paths, entry) {
4748 err = revert_file(worktree, fileindex, pe->path,
4749 progress_cb, progress_arg, repo);
4754 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
4755 repo, progress_cb, progress_arg, NULL, NULL);
4757 sync_err = sync_fileindex(fileindex, fileindex_path);
4758 if (sync_err && err == NULL)
4761 got_ref_close(resolved);
4763 free(fileindex_path);
4764 TAILQ_FOREACH(pe, &revertible_paths, entry)
4765 free((char *)pe->path);
4766 got_pathlist_free(&revertible_paths);
4768 unlockerr = lock_worktree(worktree, LOCK_SH);
4769 if (unlockerr && err == NULL)
4774 const struct got_error *
4775 got_worktree_histedit_complete(struct got_worktree *worktree,
4776 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4777 struct got_reference *edited_branch, struct got_repository *repo)
4779 const struct got_error *err, *unlockerr;
4780 struct got_object_id *new_head_commit_id = NULL;
4781 struct got_reference *resolved = NULL;
4783 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
4787 err = got_ref_open(&resolved, repo,
4788 got_ref_get_symref_target(edited_branch), 0);
4792 err = got_ref_change_ref(resolved, new_head_commit_id);
4796 err = got_ref_write(resolved, repo);
4800 err = got_worktree_set_head_ref(worktree, resolved);
4804 err = delete_histedit_refs(worktree, repo);
4807 got_fileindex_free(fileindex);
4808 free(new_head_commit_id);
4809 unlockerr = lock_worktree(worktree, LOCK_SH);
4810 if (unlockerr && err == NULL)
4815 const struct got_error *
4816 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
4817 struct got_object_id *commit_id, struct got_repository *repo)
4819 const struct got_error *err;
4820 char *commit_ref_name;
4822 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4826 err = store_commit_id(commit_ref_name, commit_id, repo);
4830 err = delete_ref(commit_ref_name, repo);
4832 free(commit_ref_name);