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;
1058 sb->st_mode = got_fileindex_perms_to_st(ie);
1060 sb->st_mode = GOT_DEFAULT_FILE_MODE;
1063 return got_error_from_errno2("lstat", abspath);
1066 if (!S_ISREG(sb->st_mode)) {
1067 *status = GOT_STATUS_OBSTRUCTED;
1074 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1075 *status = GOT_STATUS_DELETE;
1077 } else if (!got_fileindex_entry_has_blob(ie)) {
1078 *status = GOT_STATUS_ADD;
1082 if (!stat_info_differs(ie, sb))
1085 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1086 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1090 f = fopen(abspath, "r");
1092 err = got_error_from_errno2("fopen", abspath);
1095 hdrlen = got_object_blob_get_hdrlen(blob);
1097 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1098 err = got_object_blob_read_block(&blen, blob);
1101 /* Skip length of blob object header first time around. */
1102 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1103 if (flen == 0 && ferror(f)) {
1104 err = got_error_from_errno("fread");
1109 *status = GOT_STATUS_MODIFY;
1111 } else if (flen == 0) {
1113 *status = GOT_STATUS_MODIFY;
1115 } else if (blen - hdrlen == flen) {
1116 /* Skip blob object header first time around. */
1117 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1118 *status = GOT_STATUS_MODIFY;
1122 *status = GOT_STATUS_MODIFY;
1128 if (*status == GOT_STATUS_MODIFY) {
1130 err = get_modified_file_content_status(status, f);
1134 got_object_blob_close(blob);
1141 * Update timestamps in the file index if a file is unmodified and
1142 * we had to run a full content comparison to find out.
1144 static const struct got_error *
1145 sync_timestamps(char *ondisk_path, unsigned char status,
1146 struct got_fileindex_entry *ie, struct stat *sb)
1148 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1149 return got_fileindex_entry_update(ie, ondisk_path,
1150 ie->blob_sha1, ie->commit_sha1, 1);
1155 static const struct got_error *
1156 update_blob(struct got_worktree *worktree,
1157 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1158 struct got_tree_entry *te, const char *path,
1159 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1162 const struct got_error *err = NULL;
1163 struct got_blob_object *blob = NULL;
1165 unsigned char status = GOT_STATUS_NO_CHANGE;
1168 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1169 return got_error_from_errno("asprintf");
1171 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1175 if (status == GOT_STATUS_OBSTRUCTED) {
1176 err = (*progress_cb)(progress_arg, status, path);
1180 if (ie && status != GOT_STATUS_MISSING) {
1181 if (got_fileindex_entry_has_commit(ie) &&
1182 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1183 SHA1_DIGEST_LENGTH) == 0) {
1184 err = sync_timestamps(ondisk_path, status, ie, &sb);
1187 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1191 if (got_fileindex_entry_has_blob(ie) &&
1192 memcmp(ie->blob_sha1, te->id->sha1,
1193 SHA1_DIGEST_LENGTH) == 0) {
1194 err = sync_timestamps(ondisk_path, status, ie, &sb);
1199 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1203 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1204 int update_timestamps;
1205 struct got_blob_object *blob2 = NULL;
1206 if (got_fileindex_entry_has_blob(ie)) {
1207 struct got_object_id id2;
1208 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1209 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1213 err = merge_blob(&update_timestamps, worktree, blob2,
1214 ondisk_path, path, sb.st_mode, blob,
1215 worktree->base_commit_id, repo,
1216 progress_cb, progress_arg);
1218 got_object_blob_close(blob2);
1220 * Do not update timestamps of files with local changes.
1221 * Otherwise, a future status walk would treat them as
1222 * unmodified files again.
1224 err = got_fileindex_entry_update(ie, ondisk_path,
1225 blob->id.sha1, worktree->base_commit_id->sha1,
1227 } else if (status == GOT_STATUS_DELETE) {
1228 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1231 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1232 ondisk_path, path, blob, 0);
1236 err = install_blob(worktree, ondisk_path, path, te->mode,
1237 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1238 repo, progress_cb, progress_arg);
1241 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1242 ondisk_path, path, blob, 1);
1246 got_object_blob_close(blob);
1252 static const struct got_error *
1253 remove_ondisk_file(const char *root_path, const char *path)
1255 const struct got_error *err = NULL;
1256 char *ondisk_path = NULL;
1258 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1259 return got_error_from_errno("asprintf");
1261 if (unlink(ondisk_path) == -1) {
1262 if (errno != ENOENT)
1263 err = got_error_from_errno2("unlink", ondisk_path);
1265 char *parent = dirname(ondisk_path);
1266 while (parent && strcmp(parent, root_path) != 0) {
1267 if (rmdir(parent) == -1) {
1268 if (errno != ENOTEMPTY)
1269 err = got_error_from_errno2("rmdir",
1273 parent = dirname(parent);
1280 static const struct got_error *
1281 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1282 struct got_fileindex_entry *ie, struct got_repository *repo,
1283 got_worktree_checkout_cb progress_cb, void *progress_arg)
1285 const struct got_error *err = NULL;
1286 unsigned char status;
1290 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1292 return got_error_from_errno("asprintf");
1294 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1298 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1299 status == GOT_STATUS_ADD) {
1300 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1304 * Preserve the working file and change the deleted blob's
1305 * entry into a schedule-add entry.
1307 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1312 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1315 if (status == GOT_STATUS_NO_CHANGE) {
1316 err = remove_ondisk_file(worktree->root_path, ie->path);
1320 got_fileindex_entry_remove(fileindex, ie);
1326 struct diff_cb_arg {
1327 struct got_fileindex *fileindex;
1328 struct got_worktree *worktree;
1329 struct got_repository *repo;
1330 got_worktree_checkout_cb progress_cb;
1332 got_worktree_cancel_cb cancel_cb;
1336 static const struct got_error *
1337 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1338 struct got_tree_entry *te, const char *parent_path)
1340 struct diff_cb_arg *a = arg;
1342 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1343 return got_error(GOT_ERR_CANCELLED);
1345 return update_blob(a->worktree, a->fileindex, ie, te,
1346 ie->path, a->repo, a->progress_cb, a->progress_arg);
1349 static const struct got_error *
1350 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1352 struct diff_cb_arg *a = arg;
1354 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1355 return got_error(GOT_ERR_CANCELLED);
1357 return delete_blob(a->worktree, a->fileindex, ie,
1358 a->repo, a->progress_cb, a->progress_arg);
1361 static const struct got_error *
1362 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1364 struct diff_cb_arg *a = arg;
1365 const struct got_error *err;
1368 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1369 return got_error(GOT_ERR_CANCELLED);
1371 if (asprintf(&path, "%s%s%s", parent_path,
1372 parent_path[0] ? "/" : "", te->name)
1374 return got_error_from_errno("asprintf");
1376 if (S_ISDIR(te->mode))
1377 err = add_dir_on_disk(a->worktree, path);
1379 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1380 a->repo, a->progress_cb, a->progress_arg);
1386 static const struct got_error *
1387 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1389 const struct got_error *err = NULL;
1390 char *uuidstr = NULL;
1391 uint32_t uuid_status;
1395 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1396 if (uuid_status != uuid_s_ok)
1397 return got_error_uuid(uuid_status);
1399 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1401 err = got_error_from_errno("asprintf");
1408 const struct got_error *
1409 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1411 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1414 static const struct got_error *
1415 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1417 return get_ref_name(refname, worktree,
1418 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1421 static const struct got_error *
1422 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1424 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1427 static const struct got_error *
1428 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1430 return get_ref_name(refname, worktree,
1431 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1434 static const struct got_error *
1435 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1437 return get_ref_name(refname, worktree,
1438 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1441 static const struct got_error *
1442 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1444 return get_ref_name(refname, worktree,
1445 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1448 static const struct got_error *
1449 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1451 return get_ref_name(refname, worktree,
1452 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1455 static const struct got_error *
1456 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1458 return get_ref_name(refname, worktree,
1459 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1462 static const struct got_error *
1463 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1465 return get_ref_name(refname, worktree,
1466 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1469 const struct got_error *
1470 got_worktree_get_histedit_list_path(char **path, struct got_worktree *worktree)
1472 if (asprintf(path, "%s/%s/%s", worktree->root_path,
1473 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_LIST) == -1) {
1475 return got_error_from_errno("asprintf");
1481 * Prevent Git's garbage collector from deleting our base commit by
1482 * setting a reference to our base commit's ID.
1484 static const struct got_error *
1485 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1487 const struct got_error *err = NULL;
1488 struct got_reference *ref = NULL;
1491 err = got_worktree_get_base_ref_name(&refname, worktree);
1495 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1499 err = got_ref_write(ref, repo);
1507 static const struct got_error *
1508 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
1510 const struct got_error *err = NULL;
1512 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1513 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1514 err = got_error_from_errno("asprintf");
1515 *fileindex_path = NULL;
1521 static const struct got_error *
1522 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1523 struct got_worktree *worktree)
1525 const struct got_error *err = NULL;
1528 *fileindex_path = NULL;
1529 *fileindex = got_fileindex_alloc();
1530 if (*fileindex == NULL)
1531 return got_error_from_errno("got_fileindex_alloc");
1533 err = get_fileindex_path(fileindex_path, worktree);
1537 index = fopen(*fileindex_path, "rb");
1538 if (index == NULL) {
1539 if (errno != ENOENT)
1540 err = got_error_from_errno2("fopen", *fileindex_path);
1542 err = got_fileindex_read(*fileindex, index);
1543 if (fclose(index) != 0 && err == NULL)
1544 err = got_error_from_errno("fclose");
1548 free(*fileindex_path);
1549 *fileindex_path = NULL;
1550 got_fileindex_free(*fileindex);
1556 struct bump_base_commit_id_arg {
1557 struct got_object_id *base_commit_id;
1560 const char *entry_name;
1561 got_worktree_checkout_cb progress_cb;
1565 /* Bump base commit ID of all files within an updated part of the work tree. */
1566 static const struct got_error *
1567 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1569 const struct got_error *err;
1570 struct bump_base_commit_id_arg *a = arg;
1572 if (a->entry_name) {
1573 if (strcmp(ie->path, a->path) != 0)
1575 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1578 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1579 SHA1_DIGEST_LENGTH) == 0)
1582 if (a->progress_cb) {
1583 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1588 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1592 static const struct got_error *
1593 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1595 const struct got_error *err = NULL;
1596 char *new_fileindex_path = NULL;
1597 FILE *new_index = NULL;
1599 err = got_opentemp_named(&new_fileindex_path, &new_index,
1604 err = got_fileindex_write(fileindex, new_index);
1608 if (rename(new_fileindex_path, fileindex_path) != 0) {
1609 err = got_error_from_errno3("rename", new_fileindex_path,
1611 unlink(new_fileindex_path);
1616 free(new_fileindex_path);
1620 static const struct got_error *
1621 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1622 struct got_object_id **tree_id, const char *wt_relpath,
1623 struct got_worktree *worktree, struct got_repository *repo)
1625 const struct got_error *err = NULL;
1626 struct got_object_id *id = NULL;
1627 char *in_repo_path = NULL;
1628 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1630 *entry_type = GOT_OBJ_TYPE_ANY;
1631 *tree_relpath = NULL;
1634 if (wt_relpath[0] == '\0') {
1635 /* Check out all files within the work tree. */
1636 *entry_type = GOT_OBJ_TYPE_TREE;
1637 *tree_relpath = strdup("");
1638 if (*tree_relpath == NULL) {
1639 err = got_error_from_errno("strdup");
1642 err = got_object_id_by_path(tree_id, repo,
1643 worktree->base_commit_id, worktree->path_prefix);
1649 /* Check out a subset of files in the work tree. */
1651 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1652 is_root_wt ? "" : "/", wt_relpath) == -1) {
1653 err = got_error_from_errno("asprintf");
1657 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
1663 in_repo_path = NULL;
1665 err = got_object_get_type(entry_type, repo, id);
1669 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
1670 /* Check out a single file. */
1671 if (strchr(wt_relpath, '/') == NULL) {
1672 /* Check out a single file in work tree's root dir. */
1673 in_repo_path = strdup(worktree->path_prefix);
1674 if (in_repo_path == NULL) {
1675 err = got_error_from_errno("strdup");
1678 *tree_relpath = strdup("");
1679 if (*tree_relpath == NULL) {
1680 err = got_error_from_errno("strdup");
1684 /* Check out a single file in a subdirectory. */
1685 err = got_path_dirname(tree_relpath, wt_relpath);
1688 if (asprintf(&in_repo_path, "%s%s%s",
1689 worktree->path_prefix, is_root_wt ? "" : "/",
1690 *tree_relpath) == -1) {
1691 err = got_error_from_errno("asprintf");
1695 err = got_object_id_by_path(tree_id, repo,
1696 worktree->base_commit_id, in_repo_path);
1698 /* Check out all files within a subdirectory. */
1699 *tree_id = got_object_id_dup(id);
1700 if (*tree_id == NULL) {
1701 err = got_error_from_errno("got_object_id_dup");
1704 *tree_relpath = strdup(wt_relpath);
1705 if (*tree_relpath == NULL) {
1706 err = got_error_from_errno("strdup");
1714 *entry_type = GOT_OBJ_TYPE_ANY;
1715 free(*tree_relpath);
1716 *tree_relpath = NULL;
1723 static const struct got_error *
1724 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1725 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
1726 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1727 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1729 const struct got_error *err = NULL;
1730 struct got_commit_object *commit = NULL;
1731 struct got_tree_object *tree = NULL;
1732 struct got_fileindex_diff_tree_cb diff_cb;
1733 struct diff_cb_arg arg;
1735 err = ref_base_commit(worktree, repo);
1739 err = got_object_open_as_commit(&commit, repo,
1740 worktree->base_commit_id);
1744 err = got_object_open_as_tree(&tree, repo, tree_id);
1749 got_object_tree_find_entry(tree, entry_name) == NULL) {
1750 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1754 diff_cb.diff_old_new = diff_old_new;
1755 diff_cb.diff_old = diff_old;
1756 diff_cb.diff_new = diff_new;
1757 arg.fileindex = fileindex;
1758 arg.worktree = worktree;
1760 arg.progress_cb = progress_cb;
1761 arg.progress_arg = progress_arg;
1762 arg.cancel_cb = cancel_cb;
1763 arg.cancel_arg = cancel_arg;
1764 err = got_fileindex_diff_tree(fileindex, tree, relpath,
1765 entry_name, repo, &diff_cb, &arg);
1768 got_object_tree_close(tree);
1770 got_object_commit_close(commit);
1774 const struct got_error *
1775 got_worktree_checkout_files(struct got_worktree *worktree,
1776 struct got_pathlist_head *paths, struct got_repository *repo,
1777 got_worktree_checkout_cb progress_cb, void *progress_arg,
1778 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1780 const struct got_error *err = NULL, *sync_err, *unlockerr;
1781 struct got_commit_object *commit = NULL;
1782 struct got_tree_object *tree = NULL;
1783 struct got_fileindex *fileindex = NULL;
1784 char *fileindex_path = NULL;
1785 struct got_pathlist_entry *pe;
1786 struct tree_path_data {
1787 SIMPLEQ_ENTRY(tree_path_data) entry;
1788 struct got_object_id *tree_id;
1793 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
1795 SIMPLEQ_INIT(&tree_paths);
1797 err = lock_worktree(worktree, LOCK_EX);
1801 /* Map all specified paths to in-repository trees. */
1802 TAILQ_FOREACH(pe, paths, entry) {
1803 tpd = malloc(sizeof(*tpd));
1805 err = got_error_from_errno("malloc");
1809 err = find_tree_entry_for_checkout(&tpd->entry_type,
1810 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
1816 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
1817 err = got_path_basename(&tpd->entry_name, pe->path);
1825 tpd->entry_name = NULL;
1827 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
1831 * Read the file index.
1832 * Checking out files is supposed to be an idempotent operation.
1833 * If the on-disk file index is incomplete we will try to complete it.
1835 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1839 tpd = SIMPLEQ_FIRST(&tree_paths);
1840 TAILQ_FOREACH(pe, paths, entry) {
1841 struct bump_base_commit_id_arg bbc_arg;
1843 err = checkout_files(worktree, fileindex, tpd->relpath,
1844 tpd->tree_id, tpd->entry_name, repo,
1845 progress_cb, progress_arg, cancel_cb, cancel_arg);
1849 bbc_arg.base_commit_id = worktree->base_commit_id;
1850 bbc_arg.entry_name = tpd->entry_name;
1851 bbc_arg.path = pe->path;
1852 bbc_arg.path_len = strlen(pe->path);
1853 bbc_arg.progress_cb = progress_cb;
1854 bbc_arg.progress_arg = progress_arg;
1855 err = got_fileindex_for_each_entry_safe(fileindex,
1856 bump_base_commit_id, &bbc_arg);
1860 tpd = SIMPLEQ_NEXT(tpd, entry);
1862 sync_err = sync_fileindex(fileindex, fileindex_path);
1863 if (sync_err && err == NULL)
1866 free(fileindex_path);
1868 got_object_tree_close(tree);
1870 got_object_commit_close(commit);
1872 got_fileindex_free(fileindex);
1873 while (!SIMPLEQ_EMPTY(&tree_paths)) {
1874 tpd = SIMPLEQ_FIRST(&tree_paths);
1875 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
1880 unlockerr = lock_worktree(worktree, LOCK_SH);
1881 if (unlockerr && err == NULL)
1886 struct merge_file_cb_arg {
1887 struct got_worktree *worktree;
1888 struct got_fileindex *fileindex;
1889 got_worktree_checkout_cb progress_cb;
1891 got_worktree_cancel_cb cancel_cb;
1893 struct got_object_id *commit_id2;
1896 static const struct got_error *
1897 merge_file_cb(void *arg, struct got_blob_object *blob1,
1898 struct got_blob_object *blob2, struct got_object_id *id1,
1899 struct got_object_id *id2, const char *path1, const char *path2,
1900 struct got_repository *repo)
1902 static const struct got_error *err = NULL;
1903 struct merge_file_cb_arg *a = arg;
1904 struct got_fileindex_entry *ie;
1905 char *ondisk_path = NULL;
1907 unsigned char status;
1908 int local_changes_subsumed;
1910 if (blob1 && blob2) {
1911 ie = got_fileindex_entry_get(a->fileindex, path2);
1913 return (*a->progress_cb)(a->progress_arg,
1914 GOT_STATUS_MISSING, path2);
1916 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1918 return got_error_from_errno("asprintf");
1920 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1924 if (status == GOT_STATUS_DELETE) {
1925 err = (*a->progress_cb)(a->progress_arg,
1926 GOT_STATUS_MERGE, path2);
1929 if (status != GOT_STATUS_NO_CHANGE &&
1930 status != GOT_STATUS_MODIFY &&
1931 status != GOT_STATUS_CONFLICT &&
1932 status != GOT_STATUS_ADD) {
1933 err = (*a->progress_cb)(a->progress_arg, status, path2);
1937 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
1938 ondisk_path, path2, sb.st_mode, blob2, a->commit_id2, repo,
1939 a->progress_cb, a->progress_arg);
1941 ie = got_fileindex_entry_get(a->fileindex, path1);
1943 return (*a->progress_cb)(a->progress_arg,
1944 GOT_STATUS_MISSING, path2);
1946 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1948 return got_error_from_errno("asprintf");
1950 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1955 case GOT_STATUS_NO_CHANGE:
1956 err = (*a->progress_cb)(a->progress_arg,
1957 GOT_STATUS_DELETE, path1);
1960 err = remove_ondisk_file(a->worktree->root_path, path1);
1964 got_fileindex_entry_mark_deleted_from_disk(ie);
1966 case GOT_STATUS_DELETE:
1967 case GOT_STATUS_MISSING:
1968 err = (*a->progress_cb)(a->progress_arg,
1969 GOT_STATUS_DELETE, path1);
1973 got_fileindex_entry_mark_deleted_from_disk(ie);
1975 case GOT_STATUS_ADD:
1976 case GOT_STATUS_MODIFY:
1977 case GOT_STATUS_CONFLICT:
1978 err = (*a->progress_cb)(a->progress_arg,
1979 GOT_STATUS_CANNOT_DELETE, path1);
1983 case GOT_STATUS_OBSTRUCTED:
1984 err = (*a->progress_cb)(a->progress_arg, status, path1);
1992 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1994 return got_error_from_errno("asprintf");
1995 ie = got_fileindex_entry_get(a->fileindex, path2);
1997 err = get_file_status(&status, &sb, ie, ondisk_path,
2001 if (status != GOT_STATUS_NO_CHANGE &&
2002 status != GOT_STATUS_MODIFY &&
2003 status != GOT_STATUS_CONFLICT &&
2004 status != GOT_STATUS_ADD) {
2005 err = (*a->progress_cb)(a->progress_arg,
2009 err = merge_blob(&local_changes_subsumed, a->worktree,
2010 NULL, ondisk_path, path2, sb.st_mode, blob2,
2011 a->commit_id2, repo,
2012 a->progress_cb, a->progress_arg);
2013 if (status == GOT_STATUS_DELETE) {
2014 err = update_blob_fileindex_entry(a->worktree,
2015 a->fileindex, ie, ondisk_path, ie->path,
2021 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2022 err = install_blob(a->worktree, ondisk_path, path2,
2023 /* XXX get this from parent tree! */
2024 GOT_DEFAULT_FILE_MODE,
2025 sb.st_mode, blob2, 0, 0, repo,
2026 a->progress_cb, a->progress_arg);
2029 err = got_fileindex_entry_alloc(&ie,
2030 ondisk_path, path2, NULL, NULL);
2033 err = got_fileindex_entry_add(a->fileindex, ie);
2035 got_fileindex_entry_free(ie);
2045 struct check_merge_ok_arg {
2046 struct got_worktree *worktree;
2047 struct got_repository *repo;
2050 static const struct got_error *
2051 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2053 const struct got_error *err = NULL;
2054 struct check_merge_ok_arg *a = arg;
2055 unsigned char status;
2059 /* Reject merges into a work tree with mixed base commits. */
2060 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2061 SHA1_DIGEST_LENGTH))
2062 return got_error(GOT_ERR_MIXED_COMMITS);
2064 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2066 return got_error_from_errno("asprintf");
2068 /* Reject merges into a work tree with conflicted files. */
2069 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
2072 if (status == GOT_STATUS_CONFLICT)
2073 return got_error(GOT_ERR_CONFLICTS);
2078 static const struct got_error *
2079 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2080 const char *fileindex_path, struct got_object_id *commit_id1,
2081 struct got_object_id *commit_id2, struct got_repository *repo,
2082 got_worktree_checkout_cb progress_cb, void *progress_arg,
2083 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2085 const struct got_error *err = NULL, *sync_err;
2086 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2087 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2088 struct merge_file_cb_arg arg;
2091 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2092 worktree->path_prefix);
2096 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2101 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2102 worktree->path_prefix);
2106 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2110 arg.worktree = worktree;
2111 arg.fileindex = fileindex;
2112 arg.progress_cb = progress_cb;
2113 arg.progress_arg = progress_arg;
2114 arg.cancel_cb = cancel_cb;
2115 arg.cancel_arg = cancel_arg;
2116 arg.commit_id2 = commit_id2;
2117 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg);
2118 sync_err = sync_fileindex(fileindex, fileindex_path);
2119 if (sync_err && err == NULL)
2123 got_object_tree_close(tree1);
2125 got_object_tree_close(tree2);
2129 const struct got_error *
2130 got_worktree_merge_files(struct got_worktree *worktree,
2131 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2132 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2133 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2135 const struct got_error *err, *unlockerr;
2136 char *fileindex_path = NULL;
2137 struct got_fileindex *fileindex = NULL;
2138 struct check_merge_ok_arg mok_arg;
2140 err = lock_worktree(worktree, LOCK_EX);
2144 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2148 mok_arg.worktree = worktree;
2149 mok_arg.repo = repo;
2150 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2155 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2156 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2159 got_fileindex_free(fileindex);
2160 free(fileindex_path);
2161 unlockerr = lock_worktree(worktree, LOCK_SH);
2162 if (unlockerr && err == NULL)
2167 struct diff_dir_cb_arg {
2168 struct got_fileindex *fileindex;
2169 struct got_worktree *worktree;
2170 const char *status_path;
2171 size_t status_path_len;
2172 struct got_repository *repo;
2173 got_worktree_status_cb status_cb;
2175 got_worktree_cancel_cb cancel_cb;
2179 static const struct got_error *
2180 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2181 got_worktree_status_cb status_cb, void *status_arg,
2182 struct got_repository *repo)
2184 const struct got_error *err = NULL;
2185 unsigned char status = GOT_STATUS_NO_CHANGE;
2187 struct got_object_id blob_id, commit_id;
2189 err = get_file_status(&status, &sb, ie, abspath, repo);
2190 if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
2191 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2192 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2193 err = (*status_cb)(status_arg, status, ie->path, &blob_id,
2199 static const struct got_error *
2200 status_old_new(void *arg, struct got_fileindex_entry *ie,
2201 struct dirent *de, const char *parent_path)
2203 const struct got_error *err = NULL;
2204 struct diff_dir_cb_arg *a = arg;
2207 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2208 return got_error(GOT_ERR_CANCELLED);
2210 if (got_path_cmp(parent_path, a->status_path) != 0 &&
2211 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2214 if (parent_path[0]) {
2215 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2216 parent_path, de->d_name) == -1)
2217 return got_error_from_errno("asprintf");
2219 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2221 return got_error_from_errno("asprintf");
2224 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
2230 static const struct got_error *
2231 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2233 struct diff_dir_cb_arg *a = arg;
2234 struct got_object_id blob_id, commit_id;
2235 unsigned char status;
2237 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2238 return got_error(GOT_ERR_CANCELLED);
2240 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2243 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2244 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2245 if (got_fileindex_entry_has_file_on_disk(ie))
2246 status = GOT_STATUS_MISSING;
2248 status = GOT_STATUS_DELETE;
2249 return (*a->status_cb)(a->status_arg, status, ie->path, &blob_id,
2253 static const struct got_error *
2254 status_new(void *arg, struct dirent *de, const char *parent_path)
2256 const struct got_error *err = NULL;
2257 struct diff_dir_cb_arg *a = arg;
2260 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2261 return got_error(GOT_ERR_CANCELLED);
2263 if (de->d_type == DT_DIR)
2266 /* XXX ignore symlinks for now */
2267 if (de->d_type == DT_LNK)
2270 if (parent_path[0]) {
2271 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2272 return got_error_from_errno("asprintf");
2277 if (got_path_is_child(path, a->status_path, a->status_path_len))
2278 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2285 static const struct got_error *
2286 worktree_status(struct got_worktree *worktree, const char *path,
2287 struct got_fileindex *fileindex, struct got_repository *repo,
2288 got_worktree_status_cb status_cb, void *status_arg,
2289 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2291 const struct got_error *err = NULL;
2292 DIR *workdir = NULL;
2293 struct got_fileindex_diff_dir_cb fdiff_cb;
2294 struct diff_dir_cb_arg arg;
2295 char *ondisk_path = NULL;
2297 if (asprintf(&ondisk_path, "%s%s%s",
2298 worktree->root_path, path[0] ? "/" : "", path) == -1) {
2299 err = got_error_from_errno("asprintf");
2302 workdir = opendir(ondisk_path);
2303 if (workdir == NULL) {
2304 if (errno == ENOTDIR || errno == ENOENT) {
2305 err = report_file_status(
2306 got_fileindex_entry_get(fileindex, path),
2307 ondisk_path, status_cb, status_arg, repo);
2310 err = got_error_from_errno2("opendir", ondisk_path);
2314 fdiff_cb.diff_old_new = status_old_new;
2315 fdiff_cb.diff_old = status_old;
2316 fdiff_cb.diff_new = status_new;
2317 arg.fileindex = fileindex;
2318 arg.worktree = worktree;
2319 arg.status_path = path;
2320 arg.status_path_len = strlen(path);
2322 arg.status_cb = status_cb;
2323 arg.status_arg = status_arg;
2324 arg.cancel_cb = cancel_cb;
2325 arg.cancel_arg = cancel_arg;
2326 err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
2327 path, repo, &fdiff_cb, &arg);
2335 const struct got_error *
2336 got_worktree_status(struct got_worktree *worktree,
2337 struct got_pathlist_head *paths, struct got_repository *repo,
2338 got_worktree_status_cb status_cb, void *status_arg,
2339 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2341 const struct got_error *err = NULL;
2342 char *fileindex_path = NULL;
2343 struct got_fileindex *fileindex = NULL;
2344 struct got_pathlist_entry *pe;
2346 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2350 TAILQ_FOREACH(pe, paths, entry) {
2351 err = worktree_status(worktree, pe->path, fileindex, repo,
2352 status_cb, status_arg, cancel_cb, cancel_arg);
2356 free(fileindex_path);
2357 got_fileindex_free(fileindex);
2361 const struct got_error *
2362 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2365 const struct got_error *err = NULL;
2366 char *resolved, *cwd = NULL, *path = NULL;
2371 resolved = realpath(arg, NULL);
2372 if (resolved == NULL) {
2373 if (errno != ENOENT)
2374 return got_error_from_errno2("realpath", arg);
2375 cwd = getcwd(NULL, 0);
2377 return got_error_from_errno("getcwd");
2378 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
2379 err = got_error_from_errno("asprintf");
2384 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2385 strlen(got_worktree_get_root_path(worktree)))) {
2386 err = got_error(GOT_ERR_BAD_PATH);
2390 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2391 err = got_path_skip_common_ancestor(&path,
2392 got_worktree_get_root_path(worktree), resolved);
2398 err = got_error_from_errno("strdup");
2403 /* XXX status walk can't deal with trailing slash! */
2405 while (len > 0 && path[len - 1] == '/') {
2406 path[len - 1] = '\0';
2419 static const struct got_error *
2420 schedule_addition(const char *ondisk_path, struct got_fileindex *fileindex,
2421 const char *relpath, got_worktree_status_cb status_cb, void *status_arg,
2422 struct got_repository *repo)
2424 const struct got_error *err = NULL;
2425 struct got_fileindex_entry *ie;
2427 /* Re-adding an existing entry is a no-op. */
2428 if (got_fileindex_entry_get(fileindex, relpath) != NULL)
2431 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2435 err = got_fileindex_entry_add(fileindex, ie);
2437 got_fileindex_entry_free(ie);
2441 return report_file_status(ie, relpath, status_cb, status_arg, repo);
2444 const struct got_error *
2445 got_worktree_schedule_add(struct got_worktree *worktree,
2446 struct got_pathlist_head *ondisk_paths,
2447 got_worktree_status_cb status_cb, void *status_arg,
2448 struct got_repository *repo)
2450 struct got_fileindex *fileindex = NULL;
2451 char *fileindex_path = NULL;
2452 const struct got_error *err = NULL, *sync_err, *unlockerr;
2453 struct got_pathlist_entry *pe;
2455 err = lock_worktree(worktree, LOCK_EX);
2459 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2463 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2465 err = got_path_skip_common_ancestor(&relpath,
2466 got_worktree_get_root_path(worktree), pe->path);
2469 err = schedule_addition(pe->path, fileindex, relpath,
2470 status_cb, status_arg, repo);
2475 sync_err = sync_fileindex(fileindex, fileindex_path);
2476 if (sync_err && err == NULL)
2479 free(fileindex_path);
2481 got_fileindex_free(fileindex);
2482 unlockerr = lock_worktree(worktree, LOCK_SH);
2483 if (unlockerr && err == NULL)
2488 static const struct got_error *
2489 schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
2490 const char *relpath, int delete_local_mods,
2491 got_worktree_status_cb status_cb, void *status_arg,
2492 struct got_repository *repo)
2494 const struct got_error *err = NULL;
2495 struct got_fileindex_entry *ie = NULL;
2496 unsigned char status;
2499 ie = got_fileindex_entry_get(fileindex, relpath);
2501 return got_error(GOT_ERR_BAD_PATH);
2503 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2507 if (status != GOT_STATUS_NO_CHANGE) {
2508 if (status == GOT_STATUS_DELETE)
2509 return got_error_set_errno(ENOENT, ondisk_path);
2510 if (status != GOT_STATUS_MODIFY)
2511 return got_error(GOT_ERR_FILE_STATUS);
2512 if (!delete_local_mods)
2513 return got_error(GOT_ERR_FILE_MODIFIED);
2516 if (unlink(ondisk_path) != 0)
2517 return got_error_from_errno2("unlink", ondisk_path);
2519 got_fileindex_entry_mark_deleted_from_disk(ie);
2520 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2523 const struct got_error *
2524 got_worktree_schedule_delete(struct got_worktree *worktree,
2525 struct got_pathlist_head *ondisk_paths, int delete_local_mods,
2526 got_worktree_status_cb status_cb, void *status_arg,
2527 struct got_repository *repo)
2529 struct got_fileindex *fileindex = NULL;
2530 char *fileindex_path = NULL;
2531 const struct got_error *err = NULL, *sync_err, *unlockerr;
2532 struct got_pathlist_entry *pe;
2534 err = lock_worktree(worktree, LOCK_EX);
2538 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2542 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2544 err = got_path_skip_common_ancestor(&relpath,
2545 got_worktree_get_root_path(worktree), pe->path);
2548 err = schedule_for_deletion(pe->path, fileindex, relpath,
2549 delete_local_mods, status_cb, status_arg, repo);
2554 sync_err = sync_fileindex(fileindex, fileindex_path);
2555 if (sync_err && err == NULL)
2558 free(fileindex_path);
2560 got_fileindex_free(fileindex);
2561 unlockerr = lock_worktree(worktree, LOCK_SH);
2562 if (unlockerr && err == NULL)
2567 static const struct got_error *
2568 revert_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2569 const char *ondisk_path,
2570 got_worktree_checkout_cb progress_cb, void *progress_arg,
2571 struct got_repository *repo)
2573 const struct got_error *err = NULL;
2574 char *relpath = NULL, *parent_path = NULL;
2575 struct got_fileindex_entry *ie;
2576 struct got_tree_object *tree = NULL;
2577 struct got_object_id *tree_id = NULL;
2578 const struct got_tree_entry *te;
2579 char *tree_path = NULL, *te_name;
2580 struct got_blob_object *blob = NULL;
2581 unsigned char status;
2584 err = got_path_skip_common_ancestor(&relpath,
2585 got_worktree_get_root_path(worktree), ondisk_path);
2589 ie = got_fileindex_entry_get(fileindex, relpath);
2591 err = got_error(GOT_ERR_BAD_PATH);
2595 /* Construct in-repository path of tree which contains this blob. */
2596 err = got_path_dirname(&parent_path, ie->path);
2598 if (err->code != GOT_ERR_BAD_PATH)
2600 parent_path = strdup("/");
2601 if (parent_path == NULL) {
2602 err = got_error_from_errno("strdup");
2606 if (got_path_is_root_dir(worktree->path_prefix)) {
2607 tree_path = strdup(parent_path);
2608 if (tree_path == NULL) {
2609 err = got_error_from_errno("strdup");
2613 if (got_path_is_root_dir(parent_path)) {
2614 tree_path = strdup(worktree->path_prefix);
2615 if (tree_path == NULL) {
2616 err = got_error_from_errno("strdup");
2620 if (asprintf(&tree_path, "%s/%s",
2621 worktree->path_prefix, parent_path) == -1) {
2622 err = got_error_from_errno("asprintf");
2628 err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2633 err = got_object_open_as_tree(&tree, repo, tree_id);
2637 te_name = basename(ie->path);
2638 if (te_name == NULL) {
2639 err = got_error_from_errno2("basename", ie->path);
2643 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2647 te = got_object_tree_find_entry(tree, te_name);
2648 if (te == NULL && status != GOT_STATUS_ADD) {
2649 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2654 case GOT_STATUS_ADD:
2655 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2658 got_fileindex_entry_remove(fileindex, ie);
2660 case GOT_STATUS_DELETE:
2661 case GOT_STATUS_MODIFY:
2662 case GOT_STATUS_CONFLICT:
2663 case GOT_STATUS_MISSING: {
2664 struct got_object_id id;
2665 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2666 err = got_object_open_as_blob(&blob, repo, &id, 8192);
2669 err = install_blob(worktree, ondisk_path, ie->path,
2670 te->mode, sb.st_mode, blob, 0, 1, repo, progress_cb,
2674 if (status == GOT_STATUS_DELETE) {
2675 err = update_blob_fileindex_entry(worktree,
2676 fileindex, ie, ondisk_path, ie->path, blob, 1);
2690 got_object_blob_close(blob);
2692 got_object_tree_close(tree);
2697 const struct got_error *
2698 got_worktree_revert(struct got_worktree *worktree,
2699 struct got_pathlist_head *ondisk_paths,
2700 got_worktree_checkout_cb progress_cb, void *progress_arg,
2701 struct got_repository *repo)
2703 struct got_fileindex *fileindex = NULL;
2704 char *fileindex_path = NULL;
2705 const struct got_error *err = NULL, *unlockerr = NULL;
2706 const struct got_error *sync_err = NULL;
2707 struct got_pathlist_entry *pe;
2709 err = lock_worktree(worktree, LOCK_EX);
2713 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2717 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2718 err = revert_file(worktree, fileindex, pe->path,
2719 progress_cb, progress_arg, repo);
2723 sync_err = sync_fileindex(fileindex, fileindex_path);
2724 if (sync_err && err == NULL)
2727 free(fileindex_path);
2729 got_fileindex_free(fileindex);
2730 unlockerr = lock_worktree(worktree, LOCK_SH);
2731 if (unlockerr && err == NULL)
2737 free_commitable(struct got_commitable *ct)
2740 free(ct->in_repo_path);
2741 free(ct->ondisk_path);
2743 free(ct->base_blob_id);
2744 free(ct->base_commit_id);
2748 struct collect_commitables_arg {
2749 struct got_pathlist_head *commitable_paths;
2750 struct got_repository *repo;
2751 struct got_worktree *worktree;
2754 static const struct got_error *
2755 collect_commitables(void *arg, unsigned char status, const char *relpath,
2756 struct got_object_id *blob_id, struct got_object_id *commit_id)
2758 struct collect_commitables_arg *a = arg;
2759 const struct got_error *err = NULL;
2760 struct got_commitable *ct = NULL;
2761 struct got_pathlist_entry *new = NULL;
2762 char *parent_path = NULL, *path = NULL;
2765 if (status == GOT_STATUS_CONFLICT)
2766 return got_error(GOT_ERR_COMMIT_CONFLICT);
2768 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
2769 status != GOT_STATUS_DELETE)
2772 if (asprintf(&path, "/%s", relpath) == -1) {
2773 err = got_error_from_errno("asprintf");
2776 if (strcmp(path, "/") == 0) {
2777 parent_path = strdup("");
2778 if (parent_path == NULL)
2779 return got_error_from_errno("strdup");
2781 err = got_path_dirname(&parent_path, path);
2786 ct = calloc(1, sizeof(*ct));
2788 err = got_error_from_errno("calloc");
2792 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
2794 err = got_error_from_errno("asprintf");
2797 if (status == GOT_STATUS_DELETE) {
2798 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2800 if (lstat(ct->ondisk_path, &sb) != 0) {
2801 err = got_error_from_errno2("lstat", ct->ondisk_path);
2804 ct->mode = sb.st_mode;
2807 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
2808 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
2810 err = got_error_from_errno("asprintf");
2814 ct->status = status;
2815 ct->blob_id = NULL; /* will be filled in when blob gets created */
2816 if (ct->status != GOT_STATUS_ADD) {
2817 ct->base_blob_id = got_object_id_dup(blob_id);
2818 if (ct->base_blob_id == NULL) {
2819 err = got_error_from_errno("got_object_id_dup");
2822 ct->base_commit_id = got_object_id_dup(commit_id);
2823 if (ct->base_commit_id == NULL) {
2824 err = got_error_from_errno("got_object_id_dup");
2828 ct->path = strdup(path);
2829 if (ct->path == NULL) {
2830 err = got_error_from_errno("strdup");
2833 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
2835 if (ct && (err || new == NULL))
2836 free_commitable(ct);
2842 static const struct got_error *write_tree(struct got_object_id **,
2843 struct got_tree_object *, const char *, struct got_pathlist_head *,
2844 got_worktree_status_cb status_cb, void *status_arg,
2845 struct got_repository *);
2847 static const struct got_error *
2848 write_subtree(struct got_object_id **new_subtree_id,
2849 struct got_tree_entry *te, const char *parent_path,
2850 struct got_pathlist_head *commitable_paths,
2851 got_worktree_status_cb status_cb, void *status_arg,
2852 struct got_repository *repo)
2854 const struct got_error *err = NULL;
2855 struct got_tree_object *subtree;
2858 if (asprintf(&subpath, "%s%s%s", parent_path,
2859 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
2860 return got_error_from_errno("asprintf");
2862 err = got_object_open_as_tree(&subtree, repo, te->id);
2866 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
2867 status_cb, status_arg, repo);
2868 got_object_tree_close(subtree);
2873 static const struct got_error *
2874 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
2876 const struct got_error *err = NULL;
2877 char *ct_parent_path = NULL;
2881 if (strchr(ct->in_repo_path, '/') == NULL) {
2882 *match = got_path_is_root_dir(path);
2886 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
2889 *match = (strcmp(path, ct_parent_path) == 0);
2890 free(ct_parent_path);
2895 get_ct_file_mode(struct got_commitable *ct)
2897 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
2900 static const struct got_error *
2901 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
2902 struct got_tree_entry *te, struct got_commitable *ct)
2904 const struct got_error *err = NULL;
2908 err = got_object_tree_entry_dup(new_te, te);
2912 (*new_te)->mode = get_ct_file_mode(ct);
2914 free((*new_te)->id);
2915 (*new_te)->id = got_object_id_dup(ct->blob_id);
2916 if ((*new_te)->id == NULL) {
2917 err = got_error_from_errno("got_object_id_dup");
2921 if (err && *new_te) {
2922 got_object_tree_entry_close(*new_te);
2928 static const struct got_error *
2929 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2930 struct got_commitable *ct)
2932 const struct got_error *err = NULL;
2937 *new_te = calloc(1, sizeof(**new_te));
2938 if (*new_te == NULL)
2939 return got_error_from_errno("calloc");
2941 ct_name = basename(ct->path);
2942 if (ct_name == NULL) {
2943 err = got_error_from_errno2("basename", ct->path);
2946 (*new_te)->name = strdup(ct_name);
2947 if ((*new_te)->name == NULL) {
2948 err = got_error_from_errno("strdup");
2952 (*new_te)->mode = get_ct_file_mode(ct);
2954 (*new_te)->id = got_object_id_dup(ct->blob_id);
2955 if ((*new_te)->id == NULL) {
2956 err = got_error_from_errno("got_object_id_dup");
2960 if (err && *new_te) {
2961 got_object_tree_entry_close(*new_te);
2967 static const struct got_error *
2968 insert_tree_entry(struct got_tree_entry *new_te,
2969 struct got_pathlist_head *paths)
2971 const struct got_error *err = NULL;
2972 struct got_pathlist_entry *new_pe;
2974 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2978 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2982 static const struct got_error *
2983 report_ct_status(struct got_commitable *ct,
2984 got_worktree_status_cb status_cb, void *status_arg)
2986 const char *ct_path = ct->path;
2987 while (ct_path[0] == '/')
2989 return (*status_cb)(status_arg, ct->status, ct_path, ct->blob_id, NULL);
2992 static const struct got_error *
2993 match_modified_subtree(int *modified, struct got_tree_entry *te,
2994 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
2996 const struct got_error *err = NULL;
2997 struct got_pathlist_entry *pe;
3002 if (asprintf(&te_path, "%s%s%s", base_tree_path,
3003 got_path_is_root_dir(base_tree_path) ? "" : "/",
3005 return got_error_from_errno("asprintf");
3007 TAILQ_FOREACH(pe, commitable_paths, entry) {
3008 struct got_commitable *ct = pe->data;
3009 *modified = got_path_is_child(ct->in_repo_path, te_path,
3019 static const struct got_error *
3020 match_deleted_or_modified_ct(struct got_commitable **ctp,
3021 struct got_tree_entry *te, const char *base_tree_path,
3022 struct got_pathlist_head *commitable_paths)
3024 const struct got_error *err = NULL;
3025 struct got_pathlist_entry *pe;
3029 TAILQ_FOREACH(pe, commitable_paths, entry) {
3030 struct got_commitable *ct = pe->data;
3031 char *ct_name = NULL;
3034 if (ct->status != GOT_STATUS_MODIFY &&
3035 ct->status != GOT_STATUS_DELETE)
3038 if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
3041 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
3047 ct_name = basename(pe->path);
3048 if (ct_name == NULL)
3049 return got_error_from_errno2("basename", pe->path);
3051 if (strcmp(te->name, ct_name) != 0)
3061 static const struct got_error *
3062 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
3063 const char *child_path, const char *path_base_tree,
3064 struct got_pathlist_head *commitable_paths,
3065 got_worktree_status_cb status_cb, void *status_arg,
3066 struct got_repository *repo)
3068 const struct got_error *err = NULL;
3069 struct got_tree_entry *new_te;
3074 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
3075 got_path_is_root_dir(path_base_tree) ? "" : "/",
3077 return got_error_from_errno("asprintf");
3079 new_te = calloc(1, sizeof(*new_te));
3080 new_te->mode = S_IFDIR;
3081 new_te->name = strdup(child_path);
3082 if (new_te->name == NULL) {
3083 err = got_error_from_errno("strdup");
3084 got_object_tree_entry_close(new_te);
3087 err = write_tree(&new_te->id, NULL, subtree_path,
3088 commitable_paths, status_cb, status_arg, repo);
3090 got_object_tree_entry_close(new_te);
3100 static const struct got_error *
3101 write_tree(struct got_object_id **new_tree_id,
3102 struct got_tree_object *base_tree, const char *path_base_tree,
3103 struct got_pathlist_head *commitable_paths,
3104 got_worktree_status_cb status_cb, void *status_arg,
3105 struct got_repository *repo)
3107 const struct got_error *err = NULL;
3108 const struct got_tree_entries *base_entries = NULL;
3109 struct got_pathlist_head paths;
3110 struct got_tree_entries new_tree_entries;
3111 struct got_tree_entry *te, *new_te = NULL;
3112 struct got_pathlist_entry *pe;
3115 new_tree_entries.nentries = 0;
3116 SIMPLEQ_INIT(&new_tree_entries.head);
3118 /* Insert, and recurse into, newly added entries first. */
3119 TAILQ_FOREACH(pe, commitable_paths, entry) {
3120 struct got_commitable *ct = pe->data;
3121 char *child_path = NULL, *slash;
3123 if (ct->status != GOT_STATUS_ADD ||
3124 (ct->flags & GOT_COMMITABLE_ADDED))
3127 if (!got_path_is_child(pe->path, path_base_tree,
3128 strlen(path_base_tree)))
3131 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
3136 slash = strchr(child_path, '/');
3137 if (slash == NULL) {
3138 err = alloc_added_blob_tree_entry(&new_te, ct);
3141 err = report_ct_status(ct, status_cb, status_arg);
3144 ct->flags |= GOT_COMMITABLE_ADDED;
3145 err = insert_tree_entry(new_te, &paths);
3149 *slash = '\0'; /* trim trailing path components */
3150 if (base_tree == NULL ||
3151 got_object_tree_find_entry(base_tree, child_path)
3153 err = make_subtree_for_added_blob(&new_te,
3154 child_path, path_base_tree,
3155 commitable_paths, status_cb, status_arg,
3159 err = insert_tree_entry(new_te, &paths);
3167 /* Handle modified and deleted entries. */
3168 base_entries = got_object_tree_get_entries(base_tree);
3169 SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
3170 struct got_commitable *ct = NULL;
3172 if (S_ISDIR(te->mode)) {
3174 err = got_object_tree_entry_dup(&new_te, te);
3177 err = match_modified_subtree(&modified, te,
3178 path_base_tree, commitable_paths);
3181 /* Avoid recursion into unmodified subtrees. */
3184 err = write_subtree(&new_te->id, te,
3185 path_base_tree, commitable_paths,
3186 status_cb, status_arg, repo);
3190 err = insert_tree_entry(new_te, &paths);
3196 err = match_deleted_or_modified_ct(&ct, te,
3197 path_base_tree, commitable_paths);
3199 /* NB: Deleted entries get dropped here. */
3200 if (ct->status == GOT_STATUS_MODIFY) {
3201 err = alloc_modified_blob_tree_entry(
3205 err = insert_tree_entry(new_te, &paths);
3209 err = report_ct_status(ct, status_cb,
3214 /* Entry is unchanged; just copy it. */
3215 err = got_object_tree_entry_dup(&new_te, te);
3218 err = insert_tree_entry(new_te, &paths);
3225 /* Write new list of entries; deleted entries have been dropped. */
3226 TAILQ_FOREACH(pe, &paths, entry) {
3227 struct got_tree_entry *te = pe->data;
3228 new_tree_entries.nentries++;
3229 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
3231 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
3233 got_object_tree_entries_close(&new_tree_entries);
3234 got_pathlist_free(&paths);
3238 static const struct got_error *
3239 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
3240 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex)
3242 const struct got_error *err = NULL;
3243 struct got_pathlist_entry *pe;
3245 TAILQ_FOREACH(pe, commitable_paths, entry) {
3246 struct got_fileindex_entry *ie;
3247 struct got_commitable *ct = pe->data;
3249 ie = got_fileindex_entry_get(fileindex, pe->path);
3251 if (ct->status == GOT_STATUS_DELETE) {
3252 got_fileindex_entry_remove(fileindex, ie);
3253 got_fileindex_entry_free(ie);
3255 err = got_fileindex_entry_update(ie,
3256 ct->ondisk_path, ct->blob_id->sha1,
3257 new_base_commit_id->sha1, 1);
3259 err = got_fileindex_entry_alloc(&ie,
3260 ct->ondisk_path, pe->path, ct->blob_id->sha1,
3261 new_base_commit_id->sha1);
3264 err = got_fileindex_entry_add(fileindex, ie);
3272 static const struct got_error *
3273 check_ct_out_of_date(struct got_commitable *ct, struct got_repository *repo,
3274 struct got_object_id *head_commit_id)
3276 const struct got_error *err = NULL;
3277 struct got_object_id *id_in_head = NULL, *id = NULL;
3278 struct got_commit_object *commit = NULL;
3280 const char *ct_path = ct->in_repo_path;
3282 while (ct_path[0] == '/')
3286 * Ensure that no modifications were made to files *and their parents*
3287 * in commits between the file's base commit and the branch head.
3289 * Checking the parents is important for detecting conflicting tree
3290 * configurations (files or parent folders might have been moved,
3291 * deleted, added again, etc.). Such changes need to be merged with
3292 * local changes before a commit can occur.
3294 * The implication is that the file's (parent) entry in the root
3295 * directory must have the same ID in all relevant commits.
3297 if (ct->status != GOT_STATUS_ADD) {
3298 struct got_object_qid *pid;
3300 struct got_object_id *root_entry_id = NULL;
3302 /* Trivial case: base commit == head commit */
3303 if (got_object_id_cmp(ct->base_commit_id, head_commit_id) == 0)
3306 /* Compute the path to the root directory's entry. */
3307 path = strdup(ct_path);
3309 err = got_error_from_errno("strdup");
3312 slash = strchr(path, '/');
3316 err = got_object_open_as_commit(&commit, repo, head_commit_id);
3320 err = got_object_id_by_path(&root_entry_id, repo,
3321 head_commit_id, path);
3325 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3327 struct got_commit_object *pcommit;
3329 err = got_object_id_by_path(&id, repo, pid->id, path);
3331 if (err->code != GOT_ERR_NO_TREE_ENTRY)
3337 err = got_object_id_by_path(&id, repo, pid->id, path);
3341 if (got_object_id_cmp(id, root_entry_id) != 0) {
3342 err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
3346 if (got_object_id_cmp(pid->id, ct->base_commit_id) == 0)
3347 break; /* all relevant commits scanned */
3349 err = got_object_open_as_commit(&pcommit, repo,
3354 got_object_commit_close(commit);
3356 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(
3360 /* Require that added files don't exist in the branch head. */
3361 err = got_object_id_by_path(&id_in_head, repo, head_commit_id,
3363 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3365 err = id_in_head ? got_error(GOT_ERR_COMMIT_OUT_OF_DATE) : NULL;
3369 got_object_commit_close(commit);
3376 const struct got_error *
3377 commit_worktree(struct got_object_id **new_commit_id,
3378 struct got_pathlist_head *commitable_paths,
3379 struct got_object_id *head_commit_id, struct got_worktree *worktree,
3380 const char *ondisk_path, const char *author, const char *committer,
3381 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3382 got_worktree_status_cb status_cb, void *status_arg,
3383 struct got_repository *repo)
3385 const struct got_error *err = NULL, *unlockerr = NULL;
3386 struct got_pathlist_entry *pe;
3387 const char *head_ref_name = NULL;
3388 struct got_commit_object *head_commit = NULL;
3389 struct got_reference *head_ref2 = NULL;
3390 struct got_object_id *head_commit_id2 = NULL;
3391 struct got_tree_object *head_tree = NULL;
3392 struct got_object_id *new_tree_id = NULL;
3393 struct got_object_id_queue parent_ids;
3394 struct got_object_qid *pid = NULL;
3395 char *logmsg = NULL;
3397 *new_commit_id = NULL;
3399 SIMPLEQ_INIT(&parent_ids);
3401 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
3405 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
3409 if (commit_msg_cb != NULL) {
3410 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
3415 if (logmsg == NULL || strlen(logmsg) == 0) {
3416 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
3420 /* Create blobs from added and modified files and record their IDs. */
3421 TAILQ_FOREACH(pe, commitable_paths, entry) {
3422 struct got_commitable *ct = pe->data;
3425 if (ct->status != GOT_STATUS_ADD &&
3426 ct->status != GOT_STATUS_MODIFY)
3429 if (asprintf(&ondisk_path, "%s/%s",
3430 worktree->root_path, pe->path) == -1) {
3431 err = got_error_from_errno("asprintf");
3434 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
3440 /* Recursively write new tree objects. */
3441 err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
3442 status_cb, status_arg, repo);
3446 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
3449 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
3450 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
3451 1, author, time(NULL), committer, time(NULL), logmsg, repo);
3452 got_object_qid_free(pid);
3458 /* Check if a concurrent commit to our branch has occurred. */
3459 head_ref_name = got_worktree_get_head_ref_name(worktree);
3460 if (head_ref_name == NULL) {
3461 err = got_error_from_errno("got_worktree_get_head_ref_name");
3464 /* Lock the reference here to prevent concurrent modification. */
3465 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
3468 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
3471 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
3472 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
3475 /* Update branch head in repository. */
3476 err = got_ref_change_ref(head_ref2, *new_commit_id);
3479 err = got_ref_write(head_ref2, repo);
3483 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
3487 err = ref_base_commit(worktree, repo);
3492 got_object_tree_close(head_tree);
3494 got_object_commit_close(head_commit);
3495 free(head_commit_id2);
3497 unlockerr = got_ref_unlock(head_ref2);
3498 if (unlockerr && err == NULL)
3500 got_ref_close(head_ref2);
3505 const struct got_error *
3506 got_worktree_commit(struct got_object_id **new_commit_id,
3507 struct got_worktree *worktree, const char *ondisk_path,
3508 const char *author, const char *committer,
3509 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3510 got_worktree_status_cb status_cb, void *status_arg,
3511 struct got_repository *repo)
3513 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
3514 struct got_fileindex *fileindex = NULL;
3515 char *fileindex_path = NULL, *relpath = NULL;
3516 struct got_pathlist_head commitable_paths;
3517 struct collect_commitables_arg cc_arg;
3518 struct got_pathlist_entry *pe;
3519 struct got_reference *head_ref = NULL;
3520 struct got_object_id *head_commit_id = NULL;
3522 *new_commit_id = NULL;
3524 TAILQ_INIT(&commitable_paths);
3526 err = lock_worktree(worktree, LOCK_EX);
3530 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
3534 err = got_ref_resolve(&head_commit_id, repo, head_ref);
3539 if (strcmp(ondisk_path, worktree->root_path) == 0) {
3540 relpath = strdup("");
3541 if (relpath == NULL) {
3542 err = got_error_from_errno("strdup");
3546 err = got_path_skip_common_ancestor(&relpath,
3547 worktree->root_path, ondisk_path);
3553 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3557 cc_arg.commitable_paths = &commitable_paths;
3558 cc_arg.worktree = worktree;
3560 err = worktree_status(worktree, relpath ? relpath : "",
3561 fileindex, repo, collect_commitables, &cc_arg, NULL, NULL);
3565 if (TAILQ_EMPTY(&commitable_paths)) {
3566 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
3570 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3571 struct got_commitable *ct = pe->data;
3572 err = check_ct_out_of_date(ct, repo, head_commit_id);
3577 err = commit_worktree(new_commit_id, &commitable_paths,
3578 head_commit_id, worktree, ondisk_path, author, committer,
3579 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
3583 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
3585 sync_err = sync_fileindex(fileindex, fileindex_path);
3586 if (sync_err && err == NULL)
3590 got_fileindex_free(fileindex);
3591 free(fileindex_path);
3593 unlockerr = lock_worktree(worktree, LOCK_SH);
3594 if (unlockerr && err == NULL)
3596 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3597 struct got_commitable *ct = pe->data;
3598 free_commitable(ct);
3600 got_pathlist_free(&commitable_paths);
3605 got_commitable_get_path(struct got_commitable *ct)
3611 got_commitable_get_status(struct got_commitable *ct)
3616 struct check_rebase_ok_arg {
3617 struct got_worktree *worktree;
3618 struct got_repository *repo;
3621 static const struct got_error *
3622 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
3624 const struct got_error *err = NULL;
3625 struct check_rebase_ok_arg *a = arg;
3626 unsigned char status;
3630 /* Reject rebase of a work tree with mixed base commits. */
3631 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3632 SHA1_DIGEST_LENGTH))
3633 return got_error(GOT_ERR_MIXED_COMMITS);
3635 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3637 return got_error_from_errno("asprintf");
3639 /* Reject rebase of a work tree with modified or conflicted files. */
3640 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
3645 if (status != GOT_STATUS_NO_CHANGE)
3646 return got_error(GOT_ERR_MODIFIED);
3651 const struct got_error *
3652 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
3653 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
3654 struct got_worktree *worktree, struct got_reference *branch,
3655 struct got_repository *repo)
3657 const struct got_error *err = NULL;
3658 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
3659 char *branch_ref_name = NULL;
3660 char *fileindex_path = NULL;
3661 struct check_rebase_ok_arg ok_arg;
3662 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
3664 *new_base_branch_ref = NULL;
3668 err = lock_worktree(worktree, LOCK_EX);
3672 err = open_fileindex(fileindex, &fileindex_path, worktree);
3676 ok_arg.worktree = worktree;
3678 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
3683 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3687 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3691 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3695 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
3700 err = got_ref_alloc_symref(new_base_branch_ref,
3701 new_base_branch_ref_name, wt_branch);
3704 err = got_ref_write(*new_base_branch_ref, repo);
3708 /* TODO Lock original branch's ref while rebasing? */
3710 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
3714 err = got_ref_write(branch_ref, repo);
3718 err = got_ref_alloc(tmp_branch, tmp_branch_name,
3719 worktree->base_commit_id);
3722 err = got_ref_write(*tmp_branch, repo);
3726 err = got_worktree_set_head_ref(worktree, *tmp_branch);
3730 free(fileindex_path);
3731 free(tmp_branch_name);
3732 free(new_base_branch_ref_name);
3733 free(branch_ref_name);
3735 got_ref_close(branch_ref);
3737 got_ref_close(wt_branch);
3739 if (*new_base_branch_ref) {
3740 got_ref_close(*new_base_branch_ref);
3741 *new_base_branch_ref = NULL;
3744 got_ref_close(*tmp_branch);
3748 got_fileindex_free(*fileindex);
3751 lock_worktree(worktree, LOCK_SH);
3756 const struct got_error *
3757 got_worktree_rebase_continue(struct got_object_id **commit_id,
3758 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
3759 struct got_reference **branch, struct got_fileindex **fileindex,
3760 struct got_worktree *worktree, struct got_repository *repo)
3762 const struct got_error *err;
3763 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
3764 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
3765 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
3766 char *fileindex_path = NULL;
3769 *new_base_branch = NULL;
3774 err = lock_worktree(worktree, LOCK_EX);
3778 err = open_fileindex(fileindex, &fileindex_path, worktree);
3782 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3786 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3790 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3794 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3798 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
3802 err = got_ref_open(branch, repo,
3803 got_ref_get_symref_target(branch_ref), 0);
3807 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3811 err = got_ref_resolve(commit_id, repo, commit_ref);
3815 err = got_ref_open(new_base_branch, repo,
3816 new_base_branch_ref_name, 0);
3820 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
3824 free(commit_ref_name);
3825 free(branch_ref_name);
3826 free(fileindex_path);
3828 got_ref_close(commit_ref);
3830 got_ref_close(branch_ref);
3835 got_ref_close(*tmp_branch);
3838 if (*new_base_branch) {
3839 got_ref_close(*new_base_branch);
3840 *new_base_branch = NULL;
3843 got_ref_close(*branch);
3847 got_fileindex_free(*fileindex);
3850 lock_worktree(worktree, LOCK_SH);
3855 const struct got_error *
3856 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
3858 const struct got_error *err;
3859 char *tmp_branch_name = NULL;
3861 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3865 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
3866 free(tmp_branch_name);
3870 static const struct got_error *
3871 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
3872 char **logmsg, void *arg)
3878 static const struct got_error *
3879 rebase_status(void *arg, unsigned char status, const char *path,
3880 struct got_object_id *blob_id, struct got_object_id *commit_id)
3885 struct collect_merged_paths_arg {
3886 got_worktree_checkout_cb progress_cb;
3888 struct got_pathlist_head *merged_paths;
3891 static const struct got_error *
3892 collect_merged_paths(void *arg, unsigned char status, const char *path)
3894 const struct got_error *err;
3895 struct collect_merged_paths_arg *a = arg;
3897 struct got_pathlist_entry *new;
3899 err = (*a->progress_cb)(a->progress_arg, status, path);
3903 if (status != GOT_STATUS_MERGE &&
3904 status != GOT_STATUS_ADD &&
3905 status != GOT_STATUS_DELETE &&
3906 status != GOT_STATUS_CONFLICT)
3911 return got_error_from_errno("strdup");
3913 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
3914 if (err || new == NULL)
3920 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
3922 struct got_pathlist_entry *pe;
3924 TAILQ_FOREACH(pe, merged_paths, entry)
3925 free((char *)pe->path);
3927 got_pathlist_free(merged_paths);
3930 static const struct got_error *
3931 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
3932 struct got_repository *repo)
3934 const struct got_error *err;
3935 struct got_reference *commit_ref = NULL;
3937 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3939 if (err->code != GOT_ERR_NOT_REF)
3941 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
3944 err = got_ref_write(commit_ref, repo);
3948 struct got_object_id *stored_id;
3951 err = got_ref_resolve(&stored_id, repo, commit_ref);
3954 cmp = got_object_id_cmp(commit_id, stored_id);
3957 err = got_error(GOT_ERR_REBASE_COMMITID);
3963 got_ref_close(commit_ref);
3967 static const struct got_error *
3968 rebase_merge_files(struct got_pathlist_head *merged_paths,
3969 const char *commit_ref_name, struct got_worktree *worktree,
3970 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
3971 struct got_object_id *commit_id, struct got_repository *repo,
3972 got_worktree_checkout_cb progress_cb, void *progress_arg,
3973 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
3975 const struct got_error *err;
3976 struct got_reference *commit_ref = NULL;
3977 struct collect_merged_paths_arg cmp_arg;
3978 char *fileindex_path;
3980 /* Work tree is locked/unlocked during rebase preparation/teardown. */
3982 err = get_fileindex_path(&fileindex_path, worktree);
3986 cmp_arg.progress_cb = progress_cb;
3987 cmp_arg.progress_arg = progress_arg;
3988 cmp_arg.merged_paths = merged_paths;
3989 err = merge_files(worktree, fileindex, fileindex_path,
3990 parent_commit_id, commit_id, repo, collect_merged_paths,
3991 &cmp_arg, cancel_cb, cancel_arg);
3993 got_ref_close(commit_ref);
3997 const struct got_error *
3998 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
3999 struct got_worktree *worktree, struct got_fileindex *fileindex,
4000 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4001 struct got_repository *repo,
4002 got_worktree_checkout_cb progress_cb, void *progress_arg,
4003 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
4005 const struct got_error *err;
4006 char *commit_ref_name;
4008 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4012 err = store_commit_id(commit_ref_name, commit_id, repo);
4016 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4017 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4018 progress_arg, cancel_cb, cancel_arg);
4020 free(commit_ref_name);
4024 const struct got_error *
4025 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
4026 struct got_worktree *worktree, struct got_fileindex *fileindex,
4027 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4028 struct got_repository *repo,
4029 got_worktree_checkout_cb progress_cb, void *progress_arg,
4030 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
4032 const struct got_error *err;
4033 char *commit_ref_name;
4035 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4039 err = store_commit_id(commit_ref_name, commit_id, repo);
4043 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4044 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4045 progress_arg, cancel_cb, cancel_arg);
4047 free(commit_ref_name);
4051 static const struct got_error *
4052 rebase_commit(struct got_object_id **new_commit_id,
4053 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
4054 struct got_worktree *worktree, struct got_fileindex *fileindex,
4055 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
4056 const char *new_logmsg, struct got_repository *repo)
4058 const struct got_error *err, *sync_err;
4059 struct got_pathlist_head commitable_paths;
4060 struct collect_commitables_arg cc_arg;
4061 char *fileindex_path = NULL;
4062 struct got_reference *head_ref = NULL;
4063 struct got_object_id *head_commit_id = NULL;
4064 char *logmsg = NULL;
4066 TAILQ_INIT(&commitable_paths);
4067 *new_commit_id = NULL;
4069 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4071 err = get_fileindex_path(&fileindex_path, worktree);
4075 cc_arg.commitable_paths = &commitable_paths;
4076 cc_arg.worktree = worktree;
4079 * If possible get the status of individual files directly to
4080 * avoid crawling the entire work tree once per rebased commit.
4081 * TODO: Ideally, merged_paths would contain a list of commitables
4082 * we could use so we could skip worktree_status() entirely.
4085 struct got_pathlist_entry *pe;
4086 if (TAILQ_EMPTY(merged_paths)) {
4087 err = got_error(GOT_ERR_NO_MERGED_PATHS);
4090 TAILQ_FOREACH(pe, merged_paths, entry) {
4091 err = worktree_status(worktree, pe->path, fileindex,
4092 repo, collect_commitables, &cc_arg, NULL, NULL);
4097 err = worktree_status(worktree, "", fileindex, repo,
4098 collect_commitables, &cc_arg, NULL, NULL);
4103 if (TAILQ_EMPTY(&commitable_paths)) {
4104 /* No-op change; commit will be elided. */
4105 err = got_ref_delete(commit_ref, repo);
4108 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4112 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4116 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4121 logmsg = strdup(new_logmsg);
4123 logmsg = strdup(got_object_commit_get_logmsg(orig_commit));
4125 return got_error_from_errno("strdup");
4127 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
4128 worktree, NULL, got_object_commit_get_author(orig_commit),
4129 got_object_commit_get_committer(orig_commit),
4130 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
4134 err = got_ref_change_ref(tmp_branch, *new_commit_id);
4138 err = got_ref_delete(commit_ref, repo);
4142 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4144 sync_err = sync_fileindex(fileindex, fileindex_path);
4145 if (sync_err && err == NULL)
4148 free(fileindex_path);
4149 free(head_commit_id);
4151 got_ref_close(head_ref);
4153 free(*new_commit_id);
4154 *new_commit_id = NULL;
4159 const struct got_error *
4160 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
4161 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4162 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4163 struct got_commit_object *orig_commit,
4164 struct got_object_id *orig_commit_id, struct got_repository *repo)
4166 const struct got_error *err;
4167 char *commit_ref_name;
4168 struct got_reference *commit_ref = NULL;
4169 struct got_object_id *commit_id = NULL;
4171 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4175 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4178 err = got_ref_resolve(&commit_id, repo, commit_ref);
4181 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4182 err = got_error(GOT_ERR_REBASE_COMMITID);
4186 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4187 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
4190 got_ref_close(commit_ref);
4191 free(commit_ref_name);
4196 const struct got_error *
4197 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
4198 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4199 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4200 struct got_commit_object *orig_commit,
4201 struct got_object_id *orig_commit_id, const char *new_logmsg,
4202 struct got_repository *repo)
4204 const struct got_error *err;
4205 char *commit_ref_name;
4206 struct got_reference *commit_ref = NULL;
4207 struct got_object_id *commit_id = NULL;
4209 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4213 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4216 err = got_ref_resolve(&commit_id, repo, commit_ref);
4219 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4220 err = got_error(GOT_ERR_HISTEDIT_COMMITID);
4224 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4225 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
4228 got_ref_close(commit_ref);
4229 free(commit_ref_name);
4234 const struct got_error *
4235 got_worktree_rebase_postpone(struct got_worktree *worktree,
4236 struct got_fileindex *fileindex)
4239 got_fileindex_free(fileindex);
4240 return lock_worktree(worktree, LOCK_SH);
4243 static const struct got_error *
4244 delete_ref(const char *name, struct got_repository *repo)
4246 const struct got_error *err;
4247 struct got_reference *ref;
4249 err = got_ref_open(&ref, repo, name, 0);
4251 if (err->code == GOT_ERR_NOT_REF)
4256 err = got_ref_delete(ref, repo);
4261 static const struct got_error *
4262 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
4264 const struct got_error *err;
4265 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4266 char *branch_ref_name = NULL, *commit_ref_name = NULL;
4268 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4271 err = delete_ref(tmp_branch_name, repo);
4275 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4278 err = delete_ref(new_base_branch_ref_name, repo);
4282 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4285 err = delete_ref(branch_ref_name, repo);
4289 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4292 err = delete_ref(commit_ref_name, repo);
4297 free(tmp_branch_name);
4298 free(new_base_branch_ref_name);
4299 free(branch_ref_name);
4300 free(commit_ref_name);
4304 const struct got_error *
4305 got_worktree_rebase_complete(struct got_worktree *worktree,
4306 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
4307 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
4308 struct got_repository *repo)
4310 const struct got_error *err, *unlockerr;
4311 struct got_object_id *new_head_commit_id = NULL;
4313 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
4317 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
4321 err = got_ref_write(rebased_branch, repo);
4325 err = got_worktree_set_head_ref(worktree, rebased_branch);
4329 err = delete_rebase_refs(worktree, repo);
4332 got_fileindex_free(fileindex);
4333 free(new_head_commit_id);
4334 unlockerr = lock_worktree(worktree, LOCK_SH);
4335 if (unlockerr && err == NULL)
4340 struct collect_revertible_paths_arg {
4341 struct got_pathlist_head *revertible_paths;
4342 struct got_worktree *worktree;
4345 static const struct got_error *
4346 collect_revertible_paths(void *arg, unsigned char status, const char *relpath,
4347 struct got_object_id *blob_id, struct got_object_id *commit_id)
4349 struct collect_revertible_paths_arg *a = arg;
4350 const struct got_error *err = NULL;
4351 struct got_pathlist_entry *new = NULL;
4354 if (status != GOT_STATUS_ADD &&
4355 status != GOT_STATUS_DELETE &&
4356 status != GOT_STATUS_MODIFY &&
4357 status != GOT_STATUS_CONFLICT &&
4358 status != GOT_STATUS_MISSING)
4361 if (asprintf(&path, "%s/%s", a->worktree->root_path, relpath) == -1)
4362 return got_error_from_errno("asprintf");
4364 err = got_pathlist_insert(&new, a->revertible_paths, path, NULL);
4365 if (err || new == NULL)
4370 const struct got_error *
4371 got_worktree_rebase_abort(struct got_worktree *worktree,
4372 struct got_fileindex *fileindex, struct got_repository *repo,
4373 struct got_reference *new_base_branch,
4374 got_worktree_checkout_cb progress_cb, void *progress_arg)
4376 const struct got_error *err, *unlockerr, *sync_err;
4377 struct got_reference *resolved = NULL;
4378 struct got_object_id *commit_id = NULL;
4379 char *fileindex_path = NULL;
4380 struct got_pathlist_head revertible_paths;
4381 struct got_pathlist_entry *pe;
4382 struct collect_revertible_paths_arg crp_arg;
4383 struct got_object_id *tree_id = NULL;
4385 TAILQ_INIT(&revertible_paths);
4387 err = lock_worktree(worktree, LOCK_EX);
4391 err = got_ref_open(&resolved, repo,
4392 got_ref_get_symref_target(new_base_branch), 0);
4396 err = got_worktree_set_head_ref(worktree, resolved);
4401 * XXX commits to the base branch could have happened while
4402 * we were busy rebasing; should we store the original commit ID
4403 * when rebase begins and read it back here?
4405 err = got_ref_resolve(&commit_id, repo, resolved);
4409 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
4413 err = got_object_id_by_path(&tree_id, repo,
4414 worktree->base_commit_id, worktree->path_prefix);
4418 err = delete_rebase_refs(worktree, repo);
4422 err = get_fileindex_path(&fileindex_path, worktree);
4426 crp_arg.revertible_paths = &revertible_paths;
4427 crp_arg.worktree = worktree;
4428 err = worktree_status(worktree, "", fileindex, repo,
4429 collect_revertible_paths, &crp_arg, NULL, NULL);
4433 TAILQ_FOREACH(pe, &revertible_paths, entry) {
4434 err = revert_file(worktree, fileindex, pe->path,
4435 progress_cb, progress_arg, repo);
4440 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
4441 repo, progress_cb, progress_arg, NULL, NULL);
4443 sync_err = sync_fileindex(fileindex, fileindex_path);
4444 if (sync_err && err == NULL)
4447 got_ref_close(resolved);
4451 got_fileindex_free(fileindex);
4452 free(fileindex_path);
4453 TAILQ_FOREACH(pe, &revertible_paths, entry)
4454 free((char *)pe->path);
4455 got_pathlist_free(&revertible_paths);
4457 unlockerr = lock_worktree(worktree, LOCK_SH);
4458 if (unlockerr && err == NULL)
4463 const struct got_error *
4464 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
4465 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
4466 struct got_fileindex **fileindex, struct got_worktree *worktree,
4467 struct got_repository *repo)
4469 const struct got_error *err = NULL;
4470 char *tmp_branch_name = NULL;
4471 char *branch_ref_name = NULL;
4472 char *base_commit_ref_name = NULL;
4473 char *fileindex_path = NULL;
4474 struct check_rebase_ok_arg ok_arg;
4475 struct got_reference *wt_branch = NULL;
4476 struct got_reference *base_commit_ref = NULL;
4480 *base_commit_id = NULL;
4483 err = lock_worktree(worktree, LOCK_EX);
4487 err = open_fileindex(fileindex, &fileindex_path, worktree);
4491 ok_arg.worktree = worktree;
4493 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4498 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4502 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4506 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4511 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
4516 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
4520 err = got_ref_write(*branch_ref, repo);
4524 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
4525 worktree->base_commit_id);
4528 err = got_ref_write(base_commit_ref, repo);
4531 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
4532 if (*base_commit_id == NULL) {
4533 err = got_error_from_errno("got_object_id_dup");
4537 err = got_ref_alloc(tmp_branch, tmp_branch_name,
4538 worktree->base_commit_id);
4541 err = got_ref_write(*tmp_branch, repo);
4545 err = got_worktree_set_head_ref(worktree, *tmp_branch);
4549 free(fileindex_path);
4550 free(tmp_branch_name);
4551 free(branch_ref_name);
4552 free(base_commit_ref_name);
4554 got_ref_close(wt_branch);
4557 got_ref_close(*branch_ref);
4561 got_ref_close(*tmp_branch);
4564 free(*base_commit_id);
4566 got_fileindex_free(*fileindex);
4569 lock_worktree(worktree, LOCK_SH);
4574 const struct got_error *
4575 got_worktree_histedit_postpone(struct got_worktree *worktree,
4576 struct got_fileindex *fileindex)
4579 got_fileindex_free(fileindex);
4580 return lock_worktree(worktree, LOCK_SH);
4583 const struct got_error *
4584 got_worktree_histedit_in_progress(int *in_progress,
4585 struct got_worktree *worktree)
4587 const struct got_error *err;
4588 char *tmp_branch_name = NULL;
4590 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4594 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4595 free(tmp_branch_name);
4599 const struct got_error *
4600 got_worktree_histedit_continue(struct got_object_id **commit_id,
4601 struct got_reference **tmp_branch, struct got_reference **branch_ref,
4602 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
4603 struct got_worktree *worktree, struct got_repository *repo)
4605 const struct got_error *err;
4606 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
4607 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
4608 struct got_reference *commit_ref = NULL;
4609 struct got_reference *base_commit_ref = NULL;
4610 char *fileindex_path = NULL;
4614 *base_commit_id = NULL;
4617 err = lock_worktree(worktree, LOCK_EX);
4621 err = open_fileindex(fileindex, &fileindex_path, worktree);
4625 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4629 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4633 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4637 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4642 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
4646 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4649 err = got_ref_resolve(commit_id, repo, commit_ref);
4653 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
4656 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
4660 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4664 free(commit_ref_name);
4665 free(branch_ref_name);
4666 free(fileindex_path);
4668 got_ref_close(commit_ref);
4669 if (base_commit_ref)
4670 got_ref_close(base_commit_ref);
4674 free(*base_commit_id);
4675 *base_commit_id = NULL;
4677 got_ref_close(*tmp_branch);
4681 got_fileindex_free(*fileindex);
4684 lock_worktree(worktree, LOCK_EX);
4689 static const struct got_error *
4690 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
4692 const struct got_error *err;
4693 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
4694 char *branch_ref_name = NULL, *commit_ref_name = NULL;
4696 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4699 err = delete_ref(tmp_branch_name, repo);
4703 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4707 err = delete_ref(base_commit_ref_name, repo);
4711 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4714 err = delete_ref(branch_ref_name, repo);
4718 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4721 err = delete_ref(commit_ref_name, repo);
4725 free(tmp_branch_name);
4726 free(base_commit_ref_name);
4727 free(branch_ref_name);
4728 free(commit_ref_name);
4732 const struct got_error *
4733 got_worktree_histedit_abort(struct got_worktree *worktree,
4734 struct got_fileindex *fileindex, struct got_repository *repo,
4735 struct got_reference *branch, struct got_object_id *base_commit_id,
4736 got_worktree_checkout_cb progress_cb, void *progress_arg)
4738 const struct got_error *err, *unlockerr, *sync_err;
4739 struct got_reference *resolved = NULL;
4740 char *fileindex_path = NULL;
4741 struct got_pathlist_head revertible_paths;
4742 struct got_pathlist_entry *pe;
4743 struct collect_revertible_paths_arg crp_arg;
4744 struct got_object_id *tree_id = NULL;
4746 TAILQ_INIT(&revertible_paths);
4748 err = lock_worktree(worktree, LOCK_EX);
4752 err = got_ref_open(&resolved, repo,
4753 got_ref_get_symref_target(branch), 0);
4757 err = got_worktree_set_head_ref(worktree, resolved);
4761 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
4765 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
4766 worktree->path_prefix);
4770 err = delete_histedit_refs(worktree, repo);
4774 err = get_fileindex_path(&fileindex_path, worktree);
4778 crp_arg.revertible_paths = &revertible_paths;
4779 crp_arg.worktree = worktree;
4780 err = worktree_status(worktree, "", fileindex, repo,
4781 collect_revertible_paths, &crp_arg, NULL, NULL);
4785 TAILQ_FOREACH(pe, &revertible_paths, entry) {
4786 err = revert_file(worktree, fileindex, pe->path,
4787 progress_cb, progress_arg, repo);
4792 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
4793 repo, progress_cb, progress_arg, NULL, NULL);
4795 sync_err = sync_fileindex(fileindex, fileindex_path);
4796 if (sync_err && err == NULL)
4799 got_ref_close(resolved);
4801 free(fileindex_path);
4802 TAILQ_FOREACH(pe, &revertible_paths, entry)
4803 free((char *)pe->path);
4804 got_pathlist_free(&revertible_paths);
4806 unlockerr = lock_worktree(worktree, LOCK_SH);
4807 if (unlockerr && err == NULL)
4812 const struct got_error *
4813 got_worktree_histedit_complete(struct got_worktree *worktree,
4814 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4815 struct got_reference *edited_branch, struct got_repository *repo)
4817 const struct got_error *err, *unlockerr;
4818 struct got_object_id *new_head_commit_id = NULL;
4819 struct got_reference *resolved = NULL;
4821 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
4825 err = got_ref_open(&resolved, repo,
4826 got_ref_get_symref_target(edited_branch), 0);
4830 err = got_ref_change_ref(resolved, new_head_commit_id);
4834 err = got_ref_write(resolved, repo);
4838 err = got_worktree_set_head_ref(worktree, resolved);
4842 err = delete_histedit_refs(worktree, repo);
4845 got_fileindex_free(fileindex);
4846 free(new_head_commit_id);
4847 unlockerr = lock_worktree(worktree, LOCK_SH);
4848 if (unlockerr && err == NULL)
4853 const struct got_error *
4854 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
4855 struct got_object_id *commit_id, struct got_repository *repo)
4857 const struct got_error *err;
4858 char *commit_ref_name;
4860 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4864 err = store_commit_id(commit_ref_name, commit_id, repo);
4868 err = delete_ref(commit_ref_name, repo);
4870 free(commit_ref_name);